uinput.c 24 KB

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