uinput.c 26 KB

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