uinput.c 23 KB

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