uinput.c 26 KB

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