uinput.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * User level driver support for input subsystem
  3. *
  4. * Heavily based on evdev.c by Vojtech Pavlik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
  21. *
  22. * Changes/Revisions:
  23. * 0.4 01/09/2014 (Benjamin Tissoires <benjamin.tissoires@redhat.com>)
  24. * - add UI_GET_SYSNAME ioctl
  25. * 0.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
  26. * - updated ff support for the changes in kernel interface
  27. * - added MODULE_VERSION
  28. * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
  29. * - added force feedback support
  30. * - added UI_SET_PHYS
  31. * 0.1 20/06/2002
  32. * - first public version
  33. */
  34. #include <uapi/linux/uinput.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/fs.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/input/mt.h>
  43. #include "../input-compat.h"
  44. #define UINPUT_NAME "uinput"
  45. #define UINPUT_BUFFER_SIZE 16
  46. #define UINPUT_NUM_REQUESTS 16
  47. enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED };
  48. struct uinput_request {
  49. unsigned int id;
  50. unsigned int code; /* UI_FF_UPLOAD, UI_FF_ERASE */
  51. int retval;
  52. struct completion done;
  53. union {
  54. unsigned int effect_id;
  55. struct {
  56. struct ff_effect *effect;
  57. struct ff_effect *old;
  58. } upload;
  59. } u;
  60. };
  61. struct uinput_device {
  62. struct input_dev *dev;
  63. struct mutex mutex;
  64. enum uinput_state state;
  65. wait_queue_head_t waitq;
  66. unsigned char ready;
  67. unsigned char head;
  68. unsigned char tail;
  69. struct input_event buff[UINPUT_BUFFER_SIZE];
  70. unsigned int ff_effects_max;
  71. struct uinput_request *requests[UINPUT_NUM_REQUESTS];
  72. wait_queue_head_t requests_waitq;
  73. spinlock_t requests_lock;
  74. };
  75. static int uinput_dev_event(struct input_dev *dev,
  76. unsigned int type, unsigned int code, int value)
  77. {
  78. struct uinput_device *udev = input_get_drvdata(dev);
  79. udev->buff[udev->head].type = type;
  80. udev->buff[udev->head].code = code;
  81. udev->buff[udev->head].value = value;
  82. do_gettimeofday(&udev->buff[udev->head].time);
  83. udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
  84. wake_up_interruptible(&udev->waitq);
  85. return 0;
  86. }
  87. /* Atomically allocate an ID for the given request. Returns 0 on success. */
  88. static bool uinput_request_alloc_id(struct uinput_device *udev,
  89. struct uinput_request *request)
  90. {
  91. unsigned int id;
  92. bool reserved = false;
  93. spin_lock(&udev->requests_lock);
  94. for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
  95. if (!udev->requests[id]) {
  96. request->id = id;
  97. udev->requests[id] = request;
  98. reserved = true;
  99. break;
  100. }
  101. }
  102. spin_unlock(&udev->requests_lock);
  103. return reserved;
  104. }
  105. static struct uinput_request *uinput_request_find(struct uinput_device *udev,
  106. unsigned int id)
  107. {
  108. /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
  109. if (id >= UINPUT_NUM_REQUESTS)
  110. return NULL;
  111. return udev->requests[id];
  112. }
  113. static int uinput_request_reserve_slot(struct uinput_device *udev,
  114. struct uinput_request *request)
  115. {
  116. /* Allocate slot. If none are available right away, wait. */
  117. return wait_event_interruptible(udev->requests_waitq,
  118. uinput_request_alloc_id(udev, request));
  119. }
  120. static void uinput_request_release_slot(struct uinput_device *udev,
  121. unsigned int id)
  122. {
  123. /* Mark slot as available */
  124. spin_lock(&udev->requests_lock);
  125. udev->requests[id] = NULL;
  126. spin_unlock(&udev->requests_lock);
  127. wake_up(&udev->requests_waitq);
  128. }
  129. static int uinput_request_send(struct uinput_device *udev,
  130. struct uinput_request *request)
  131. {
  132. int retval;
  133. retval = mutex_lock_interruptible(&udev->mutex);
  134. if (retval)
  135. return retval;
  136. if (udev->state != UIST_CREATED) {
  137. retval = -ENODEV;
  138. goto out;
  139. }
  140. init_completion(&request->done);
  141. /*
  142. * Tell our userspace application about this new request
  143. * by queueing an input event.
  144. */
  145. uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
  146. out:
  147. mutex_unlock(&udev->mutex);
  148. return retval;
  149. }
  150. static int uinput_request_submit(struct uinput_device *udev,
  151. struct uinput_request *request)
  152. {
  153. int retval;
  154. retval = uinput_request_reserve_slot(udev, request);
  155. if (retval)
  156. return retval;
  157. retval = uinput_request_send(udev, request);
  158. if (retval)
  159. goto out;
  160. if (!wait_for_completion_timeout(&request->done, 30 * HZ)) {
  161. retval = -ETIMEDOUT;
  162. goto out;
  163. }
  164. retval = request->retval;
  165. out:
  166. uinput_request_release_slot(udev, request->id);
  167. return retval;
  168. }
  169. /*
  170. * Fail all outstanding requests so handlers don't wait for the userspace
  171. * to finish processing them.
  172. */
  173. static void uinput_flush_requests(struct uinput_device *udev)
  174. {
  175. struct uinput_request *request;
  176. int i;
  177. spin_lock(&udev->requests_lock);
  178. for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
  179. request = udev->requests[i];
  180. if (request) {
  181. request->retval = -ENODEV;
  182. complete(&request->done);
  183. }
  184. }
  185. spin_unlock(&udev->requests_lock);
  186. }
  187. static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
  188. {
  189. uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
  190. }
  191. static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
  192. {
  193. uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
  194. }
  195. static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
  196. {
  197. return uinput_dev_event(dev, EV_FF, effect_id, value);
  198. }
  199. static int uinput_dev_upload_effect(struct input_dev *dev,
  200. struct ff_effect *effect,
  201. struct ff_effect *old)
  202. {
  203. struct uinput_device *udev = input_get_drvdata(dev);
  204. struct uinput_request request;
  205. /*
  206. * uinput driver does not currently support periodic effects with
  207. * custom waveform since it does not have a way to pass buffer of
  208. * samples (custom_data) to userspace. If ever there is a device
  209. * supporting custom waveforms we would need to define an additional
  210. * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
  211. */
  212. if (effect->type == FF_PERIODIC &&
  213. effect->u.periodic.waveform == FF_CUSTOM)
  214. return -EINVAL;
  215. request.code = UI_FF_UPLOAD;
  216. request.u.upload.effect = effect;
  217. request.u.upload.old = old;
  218. return uinput_request_submit(udev, &request);
  219. }
  220. static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
  221. {
  222. struct uinput_device *udev = input_get_drvdata(dev);
  223. struct uinput_request request;
  224. if (!test_bit(EV_FF, dev->evbit))
  225. return -ENOSYS;
  226. request.code = UI_FF_ERASE;
  227. request.u.effect_id = effect_id;
  228. return uinput_request_submit(udev, &request);
  229. }
  230. static int uinput_dev_flush(struct input_dev *dev, struct file *file)
  231. {
  232. /*
  233. * If we are called with file == NULL that means we are tearing
  234. * down the device, and therefore we can not handle FF erase
  235. * requests: either we are handling UI_DEV_DESTROY (and holding
  236. * the udev->mutex), or the file descriptor is closed and there is
  237. * nobody on the other side anymore.
  238. */
  239. return file ? input_ff_flush(dev, file) : 0;
  240. }
  241. static void uinput_destroy_device(struct uinput_device *udev)
  242. {
  243. const char *name, *phys;
  244. struct input_dev *dev = udev->dev;
  245. enum uinput_state old_state = udev->state;
  246. udev->state = UIST_NEW_DEVICE;
  247. if (dev) {
  248. name = dev->name;
  249. phys = dev->phys;
  250. if (old_state == UIST_CREATED) {
  251. uinput_flush_requests(udev);
  252. input_unregister_device(dev);
  253. } else {
  254. input_free_device(dev);
  255. }
  256. kfree(name);
  257. kfree(phys);
  258. udev->dev = NULL;
  259. }
  260. }
  261. static int uinput_create_device(struct uinput_device *udev)
  262. {
  263. struct input_dev *dev = udev->dev;
  264. int error, nslot;
  265. if (udev->state != UIST_SETUP_COMPLETE) {
  266. printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
  267. return -EINVAL;
  268. }
  269. if (test_bit(EV_ABS, dev->evbit)) {
  270. input_alloc_absinfo(dev);
  271. if (!dev->absinfo) {
  272. error = -EINVAL;
  273. goto fail1;
  274. }
  275. if (test_bit(ABS_MT_SLOT, dev->absbit)) {
  276. nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  277. error = input_mt_init_slots(dev, nslot, 0);
  278. if (error)
  279. goto fail1;
  280. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  281. input_set_events_per_packet(dev, 60);
  282. }
  283. }
  284. if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
  285. printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
  286. UINPUT_NAME);
  287. error = -EINVAL;
  288. goto fail1;
  289. }
  290. if (udev->ff_effects_max) {
  291. error = input_ff_create(dev, udev->ff_effects_max);
  292. if (error)
  293. goto fail1;
  294. dev->ff->upload = uinput_dev_upload_effect;
  295. dev->ff->erase = uinput_dev_erase_effect;
  296. dev->ff->playback = uinput_dev_playback;
  297. dev->ff->set_gain = uinput_dev_set_gain;
  298. dev->ff->set_autocenter = uinput_dev_set_autocenter;
  299. /*
  300. * The standard input_ff_flush() implementation does
  301. * not quite work for uinput as we can't reasonably
  302. * handle FF requests during device teardown.
  303. */
  304. dev->flush = uinput_dev_flush;
  305. }
  306. dev->event = uinput_dev_event;
  307. input_set_drvdata(udev->dev, udev);
  308. error = input_register_device(udev->dev);
  309. if (error)
  310. goto fail2;
  311. udev->state = UIST_CREATED;
  312. return 0;
  313. fail2: input_ff_destroy(dev);
  314. fail1: uinput_destroy_device(udev);
  315. return error;
  316. }
  317. static int uinput_open(struct inode *inode, struct file *file)
  318. {
  319. struct uinput_device *newdev;
  320. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  321. if (!newdev)
  322. return -ENOMEM;
  323. mutex_init(&newdev->mutex);
  324. spin_lock_init(&newdev->requests_lock);
  325. init_waitqueue_head(&newdev->requests_waitq);
  326. init_waitqueue_head(&newdev->waitq);
  327. newdev->state = UIST_NEW_DEVICE;
  328. file->private_data = newdev;
  329. nonseekable_open(inode, file);
  330. return 0;
  331. }
  332. static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
  333. const struct input_absinfo *abs)
  334. {
  335. int min, max;
  336. min = abs->minimum;
  337. max = abs->maximum;
  338. if ((min != 0 || max != 0) && max <= min) {
  339. printk(KERN_DEBUG
  340. "%s: invalid abs[%02x] min:%d max:%d\n",
  341. UINPUT_NAME, code, min, max);
  342. return -EINVAL;
  343. }
  344. if (abs->flat > max - min) {
  345. printk(KERN_DEBUG
  346. "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
  347. UINPUT_NAME, code, abs->flat, min, max);
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. static int uinput_validate_absbits(struct input_dev *dev)
  353. {
  354. unsigned int cnt;
  355. int error;
  356. if (!test_bit(EV_ABS, dev->evbit))
  357. return 0;
  358. /*
  359. * Check if absmin/absmax/absfuzz/absflat are sane.
  360. */
  361. for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
  362. if (!dev->absinfo)
  363. return -EINVAL;
  364. error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
  365. if (error)
  366. return error;
  367. }
  368. return 0;
  369. }
  370. static int uinput_dev_setup(struct uinput_device *udev,
  371. struct uinput_setup __user *arg)
  372. {
  373. struct uinput_setup setup;
  374. struct input_dev *dev;
  375. if (udev->state == UIST_CREATED)
  376. return -EINVAL;
  377. if (copy_from_user(&setup, arg, sizeof(setup)))
  378. return -EFAULT;
  379. if (!setup.name[0])
  380. return -EINVAL;
  381. dev = udev->dev;
  382. dev->id = setup.id;
  383. udev->ff_effects_max = setup.ff_effects_max;
  384. kfree(dev->name);
  385. dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
  386. if (!dev->name)
  387. return -ENOMEM;
  388. udev->state = UIST_SETUP_COMPLETE;
  389. return 0;
  390. }
  391. static int uinput_abs_setup(struct uinput_device *udev,
  392. struct uinput_setup __user *arg, size_t size)
  393. {
  394. struct uinput_abs_setup setup = {};
  395. struct input_dev *dev;
  396. int error;
  397. if (size > sizeof(setup))
  398. return -E2BIG;
  399. if (udev->state == UIST_CREATED)
  400. return -EINVAL;
  401. if (copy_from_user(&setup, arg, size))
  402. return -EFAULT;
  403. if (setup.code > ABS_MAX)
  404. return -ERANGE;
  405. dev = udev->dev;
  406. error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
  407. if (error)
  408. return error;
  409. input_alloc_absinfo(dev);
  410. if (!dev->absinfo)
  411. return -ENOMEM;
  412. set_bit(setup.code, dev->absbit);
  413. dev->absinfo[setup.code] = setup.absinfo;
  414. return 0;
  415. }
  416. /* legacy setup via write() */
  417. static int uinput_setup_device_legacy(struct uinput_device *udev,
  418. const char __user *buffer, size_t count)
  419. {
  420. struct uinput_user_dev *user_dev;
  421. struct input_dev *dev;
  422. int i;
  423. int retval;
  424. if (count != sizeof(struct uinput_user_dev))
  425. return -EINVAL;
  426. if (!udev->dev) {
  427. udev->dev = input_allocate_device();
  428. if (!udev->dev)
  429. return -ENOMEM;
  430. }
  431. dev = udev->dev;
  432. user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
  433. if (IS_ERR(user_dev))
  434. return PTR_ERR(user_dev);
  435. udev->ff_effects_max = user_dev->ff_effects_max;
  436. /* Ensure name is filled in */
  437. if (!user_dev->name[0]) {
  438. retval = -EINVAL;
  439. goto exit;
  440. }
  441. kfree(dev->name);
  442. dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
  443. GFP_KERNEL);
  444. if (!dev->name) {
  445. retval = -ENOMEM;
  446. goto exit;
  447. }
  448. dev->id.bustype = user_dev->id.bustype;
  449. dev->id.vendor = user_dev->id.vendor;
  450. dev->id.product = user_dev->id.product;
  451. dev->id.version = user_dev->id.version;
  452. for (i = 0; i < ABS_CNT; i++) {
  453. input_abs_set_max(dev, i, user_dev->absmax[i]);
  454. input_abs_set_min(dev, i, user_dev->absmin[i]);
  455. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  456. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  457. }
  458. retval = uinput_validate_absbits(dev);
  459. if (retval < 0)
  460. goto exit;
  461. udev->state = UIST_SETUP_COMPLETE;
  462. retval = count;
  463. exit:
  464. kfree(user_dev);
  465. return retval;
  466. }
  467. static ssize_t uinput_inject_events(struct uinput_device *udev,
  468. const char __user *buffer, size_t count)
  469. {
  470. struct input_event ev;
  471. size_t bytes = 0;
  472. if (count != 0 && count < input_event_size())
  473. return -EINVAL;
  474. while (bytes + input_event_size() <= count) {
  475. /*
  476. * Note that even if some events were fetched successfully
  477. * we are still going to return EFAULT instead of partial
  478. * count to let userspace know that it got it's buffers
  479. * all wrong.
  480. */
  481. if (input_event_from_user(buffer + bytes, &ev))
  482. return -EFAULT;
  483. input_event(udev->dev, ev.type, ev.code, ev.value);
  484. bytes += input_event_size();
  485. }
  486. return bytes;
  487. }
  488. static ssize_t uinput_write(struct file *file, const char __user *buffer,
  489. size_t count, loff_t *ppos)
  490. {
  491. struct uinput_device *udev = file->private_data;
  492. int retval;
  493. if (count == 0)
  494. return 0;
  495. retval = mutex_lock_interruptible(&udev->mutex);
  496. if (retval)
  497. return retval;
  498. retval = udev->state == UIST_CREATED ?
  499. uinput_inject_events(udev, buffer, count) :
  500. uinput_setup_device_legacy(udev, buffer, count);
  501. mutex_unlock(&udev->mutex);
  502. return retval;
  503. }
  504. static bool uinput_fetch_next_event(struct uinput_device *udev,
  505. struct input_event *event)
  506. {
  507. bool have_event;
  508. spin_lock_irq(&udev->dev->event_lock);
  509. have_event = udev->head != udev->tail;
  510. if (have_event) {
  511. *event = udev->buff[udev->tail];
  512. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  513. }
  514. spin_unlock_irq(&udev->dev->event_lock);
  515. return have_event;
  516. }
  517. static ssize_t uinput_events_to_user(struct uinput_device *udev,
  518. char __user *buffer, size_t count)
  519. {
  520. struct input_event event;
  521. size_t read = 0;
  522. while (read + input_event_size() <= count &&
  523. uinput_fetch_next_event(udev, &event)) {
  524. if (input_event_to_user(buffer + read, &event))
  525. return -EFAULT;
  526. read += input_event_size();
  527. }
  528. return read;
  529. }
  530. static ssize_t uinput_read(struct file *file, char __user *buffer,
  531. size_t count, loff_t *ppos)
  532. {
  533. struct uinput_device *udev = file->private_data;
  534. ssize_t retval;
  535. if (count != 0 && count < input_event_size())
  536. return -EINVAL;
  537. do {
  538. retval = mutex_lock_interruptible(&udev->mutex);
  539. if (retval)
  540. return retval;
  541. if (udev->state != UIST_CREATED)
  542. retval = -ENODEV;
  543. else if (udev->head == udev->tail &&
  544. (file->f_flags & O_NONBLOCK))
  545. retval = -EAGAIN;
  546. else
  547. retval = uinput_events_to_user(udev, buffer, count);
  548. mutex_unlock(&udev->mutex);
  549. if (retval || count == 0)
  550. break;
  551. if (!(file->f_flags & O_NONBLOCK))
  552. retval = wait_event_interruptible(udev->waitq,
  553. udev->head != udev->tail ||
  554. udev->state != UIST_CREATED);
  555. } while (retval == 0);
  556. return retval;
  557. }
  558. static __poll_t uinput_poll(struct file *file, poll_table *wait)
  559. {
  560. struct uinput_device *udev = file->private_data;
  561. poll_wait(file, &udev->waitq, wait);
  562. if (udev->head != udev->tail)
  563. return POLLIN | POLLRDNORM;
  564. return 0;
  565. }
  566. static int uinput_release(struct inode *inode, struct file *file)
  567. {
  568. struct uinput_device *udev = file->private_data;
  569. uinput_destroy_device(udev);
  570. kfree(udev);
  571. return 0;
  572. }
  573. #ifdef CONFIG_COMPAT
  574. struct uinput_ff_upload_compat {
  575. __u32 request_id;
  576. __s32 retval;
  577. struct ff_effect_compat effect;
  578. struct ff_effect_compat old;
  579. };
  580. static int uinput_ff_upload_to_user(char __user *buffer,
  581. const struct uinput_ff_upload *ff_up)
  582. {
  583. if (in_compat_syscall()) {
  584. struct uinput_ff_upload_compat ff_up_compat;
  585. ff_up_compat.request_id = ff_up->request_id;
  586. ff_up_compat.retval = ff_up->retval;
  587. /*
  588. * It so happens that the pointer that gives us the trouble
  589. * is the last field in the structure. Since we don't support
  590. * custom waveforms in uinput anyway we can just copy the whole
  591. * thing (to the compat size) and ignore the pointer.
  592. */
  593. memcpy(&ff_up_compat.effect, &ff_up->effect,
  594. sizeof(struct ff_effect_compat));
  595. memcpy(&ff_up_compat.old, &ff_up->old,
  596. sizeof(struct ff_effect_compat));
  597. if (copy_to_user(buffer, &ff_up_compat,
  598. sizeof(struct uinput_ff_upload_compat)))
  599. return -EFAULT;
  600. } else {
  601. if (copy_to_user(buffer, ff_up,
  602. sizeof(struct uinput_ff_upload)))
  603. return -EFAULT;
  604. }
  605. return 0;
  606. }
  607. static int uinput_ff_upload_from_user(const char __user *buffer,
  608. struct uinput_ff_upload *ff_up)
  609. {
  610. if (in_compat_syscall()) {
  611. struct uinput_ff_upload_compat ff_up_compat;
  612. if (copy_from_user(&ff_up_compat, buffer,
  613. sizeof(struct uinput_ff_upload_compat)))
  614. return -EFAULT;
  615. ff_up->request_id = ff_up_compat.request_id;
  616. ff_up->retval = ff_up_compat.retval;
  617. memcpy(&ff_up->effect, &ff_up_compat.effect,
  618. sizeof(struct ff_effect_compat));
  619. memcpy(&ff_up->old, &ff_up_compat.old,
  620. sizeof(struct ff_effect_compat));
  621. } else {
  622. if (copy_from_user(ff_up, buffer,
  623. sizeof(struct uinput_ff_upload)))
  624. return -EFAULT;
  625. }
  626. return 0;
  627. }
  628. #else
  629. static int uinput_ff_upload_to_user(char __user *buffer,
  630. const struct uinput_ff_upload *ff_up)
  631. {
  632. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  633. return -EFAULT;
  634. return 0;
  635. }
  636. static int uinput_ff_upload_from_user(const char __user *buffer,
  637. struct uinput_ff_upload *ff_up)
  638. {
  639. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  640. return -EFAULT;
  641. return 0;
  642. }
  643. #endif
  644. #define uinput_set_bit(_arg, _bit, _max) \
  645. ({ \
  646. int __ret = 0; \
  647. if (udev->state == UIST_CREATED) \
  648. __ret = -EINVAL; \
  649. else if ((_arg) > (_max)) \
  650. __ret = -EINVAL; \
  651. else set_bit((_arg), udev->dev->_bit); \
  652. __ret; \
  653. })
  654. static int uinput_str_to_user(void __user *dest, const char *str,
  655. unsigned int maxlen)
  656. {
  657. char __user *p = dest;
  658. int len, ret;
  659. if (!str)
  660. return -ENOENT;
  661. if (maxlen == 0)
  662. return -EINVAL;
  663. len = strlen(str) + 1;
  664. if (len > maxlen)
  665. len = maxlen;
  666. ret = copy_to_user(p, str, len);
  667. if (ret)
  668. return -EFAULT;
  669. /* force terminating '\0' */
  670. ret = put_user(0, p + len - 1);
  671. return ret ? -EFAULT : len;
  672. }
  673. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  674. unsigned long arg, void __user *p)
  675. {
  676. int retval;
  677. struct uinput_device *udev = file->private_data;
  678. struct uinput_ff_upload ff_up;
  679. struct uinput_ff_erase ff_erase;
  680. struct uinput_request *req;
  681. char *phys;
  682. const char *name;
  683. unsigned int size;
  684. retval = mutex_lock_interruptible(&udev->mutex);
  685. if (retval)
  686. return retval;
  687. if (!udev->dev) {
  688. udev->dev = input_allocate_device();
  689. if (!udev->dev) {
  690. retval = -ENOMEM;
  691. goto out;
  692. }
  693. }
  694. switch (cmd) {
  695. case UI_GET_VERSION:
  696. if (put_user(UINPUT_VERSION, (unsigned int __user *)p))
  697. retval = -EFAULT;
  698. goto out;
  699. case UI_DEV_CREATE:
  700. retval = uinput_create_device(udev);
  701. goto out;
  702. case UI_DEV_DESTROY:
  703. uinput_destroy_device(udev);
  704. goto out;
  705. case UI_DEV_SETUP:
  706. retval = uinput_dev_setup(udev, p);
  707. goto out;
  708. /* UI_ABS_SETUP is handled in the variable size ioctls */
  709. case UI_SET_EVBIT:
  710. retval = uinput_set_bit(arg, evbit, EV_MAX);
  711. goto out;
  712. case UI_SET_KEYBIT:
  713. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  714. goto out;
  715. case UI_SET_RELBIT:
  716. retval = uinput_set_bit(arg, relbit, REL_MAX);
  717. goto out;
  718. case UI_SET_ABSBIT:
  719. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  720. goto out;
  721. case UI_SET_MSCBIT:
  722. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  723. goto out;
  724. case UI_SET_LEDBIT:
  725. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  726. goto out;
  727. case UI_SET_SNDBIT:
  728. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  729. goto out;
  730. case UI_SET_FFBIT:
  731. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  732. goto out;
  733. case UI_SET_SWBIT:
  734. retval = uinput_set_bit(arg, swbit, SW_MAX);
  735. goto out;
  736. case UI_SET_PROPBIT:
  737. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  738. goto out;
  739. case UI_SET_PHYS:
  740. if (udev->state == UIST_CREATED) {
  741. retval = -EINVAL;
  742. goto out;
  743. }
  744. phys = strndup_user(p, 1024);
  745. if (IS_ERR(phys)) {
  746. retval = PTR_ERR(phys);
  747. goto out;
  748. }
  749. kfree(udev->dev->phys);
  750. udev->dev->phys = phys;
  751. goto out;
  752. case UI_BEGIN_FF_UPLOAD:
  753. retval = uinput_ff_upload_from_user(p, &ff_up);
  754. if (retval)
  755. goto out;
  756. req = uinput_request_find(udev, ff_up.request_id);
  757. if (!req || req->code != UI_FF_UPLOAD ||
  758. !req->u.upload.effect) {
  759. retval = -EINVAL;
  760. goto out;
  761. }
  762. ff_up.retval = 0;
  763. ff_up.effect = *req->u.upload.effect;
  764. if (req->u.upload.old)
  765. ff_up.old = *req->u.upload.old;
  766. else
  767. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  768. retval = uinput_ff_upload_to_user(p, &ff_up);
  769. goto out;
  770. case UI_BEGIN_FF_ERASE:
  771. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  772. retval = -EFAULT;
  773. goto out;
  774. }
  775. req = uinput_request_find(udev, ff_erase.request_id);
  776. if (!req || req->code != UI_FF_ERASE) {
  777. retval = -EINVAL;
  778. goto out;
  779. }
  780. ff_erase.retval = 0;
  781. ff_erase.effect_id = req->u.effect_id;
  782. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  783. retval = -EFAULT;
  784. goto out;
  785. }
  786. goto out;
  787. case UI_END_FF_UPLOAD:
  788. retval = uinput_ff_upload_from_user(p, &ff_up);
  789. if (retval)
  790. goto out;
  791. req = uinput_request_find(udev, ff_up.request_id);
  792. if (!req || req->code != UI_FF_UPLOAD ||
  793. !req->u.upload.effect) {
  794. retval = -EINVAL;
  795. goto out;
  796. }
  797. req->retval = ff_up.retval;
  798. complete(&req->done);
  799. goto out;
  800. case UI_END_FF_ERASE:
  801. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  802. retval = -EFAULT;
  803. goto out;
  804. }
  805. req = uinput_request_find(udev, ff_erase.request_id);
  806. if (!req || req->code != UI_FF_ERASE) {
  807. retval = -EINVAL;
  808. goto out;
  809. }
  810. req->retval = ff_erase.retval;
  811. complete(&req->done);
  812. goto out;
  813. }
  814. size = _IOC_SIZE(cmd);
  815. /* Now check variable-length commands */
  816. switch (cmd & ~IOCSIZE_MASK) {
  817. case UI_GET_SYSNAME(0):
  818. if (udev->state != UIST_CREATED) {
  819. retval = -ENOENT;
  820. goto out;
  821. }
  822. name = dev_name(&udev->dev->dev);
  823. retval = uinput_str_to_user(p, name, size);
  824. goto out;
  825. case UI_ABS_SETUP & ~IOCSIZE_MASK:
  826. retval = uinput_abs_setup(udev, p, size);
  827. goto out;
  828. }
  829. retval = -EINVAL;
  830. out:
  831. mutex_unlock(&udev->mutex);
  832. return retval;
  833. }
  834. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  835. {
  836. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  837. }
  838. #ifdef CONFIG_COMPAT
  839. #define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
  840. static long uinput_compat_ioctl(struct file *file,
  841. unsigned int cmd, unsigned long arg)
  842. {
  843. if (cmd == UI_SET_PHYS_COMPAT)
  844. cmd = UI_SET_PHYS;
  845. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  846. }
  847. #endif
  848. static const struct file_operations uinput_fops = {
  849. .owner = THIS_MODULE,
  850. .open = uinput_open,
  851. .release = uinput_release,
  852. .read = uinput_read,
  853. .write = uinput_write,
  854. .poll = uinput_poll,
  855. .unlocked_ioctl = uinput_ioctl,
  856. #ifdef CONFIG_COMPAT
  857. .compat_ioctl = uinput_compat_ioctl,
  858. #endif
  859. .llseek = no_llseek,
  860. };
  861. static struct miscdevice uinput_misc = {
  862. .fops = &uinput_fops,
  863. .minor = UINPUT_MINOR,
  864. .name = UINPUT_NAME,
  865. };
  866. module_misc_device(uinput_misc);
  867. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  868. MODULE_ALIAS("devname:" UINPUT_NAME);
  869. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  870. MODULE_DESCRIPTION("User level driver support for input subsystem");
  871. MODULE_LICENSE("GPL");
  872. MODULE_VERSION("0.3");