uinput.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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(ABS_MT_SLOT, dev->absbit)) {
  223. nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  224. error = input_mt_init_slots(dev, nslot, 0);
  225. if (error)
  226. goto fail1;
  227. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  228. input_set_events_per_packet(dev, 60);
  229. }
  230. if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
  231. printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
  232. UINPUT_NAME);
  233. error = -EINVAL;
  234. goto fail1;
  235. }
  236. if (udev->ff_effects_max) {
  237. error = input_ff_create(dev, udev->ff_effects_max);
  238. if (error)
  239. goto fail1;
  240. dev->ff->upload = uinput_dev_upload_effect;
  241. dev->ff->erase = uinput_dev_erase_effect;
  242. dev->ff->playback = uinput_dev_playback;
  243. dev->ff->set_gain = uinput_dev_set_gain;
  244. dev->ff->set_autocenter = uinput_dev_set_autocenter;
  245. }
  246. error = input_register_device(udev->dev);
  247. if (error)
  248. goto fail2;
  249. udev->state = UIST_CREATED;
  250. return 0;
  251. fail2: input_ff_destroy(dev);
  252. fail1: uinput_destroy_device(udev);
  253. return error;
  254. }
  255. static int uinput_open(struct inode *inode, struct file *file)
  256. {
  257. struct uinput_device *newdev;
  258. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  259. if (!newdev)
  260. return -ENOMEM;
  261. mutex_init(&newdev->mutex);
  262. spin_lock_init(&newdev->requests_lock);
  263. init_waitqueue_head(&newdev->requests_waitq);
  264. init_waitqueue_head(&newdev->waitq);
  265. newdev->state = UIST_NEW_DEVICE;
  266. file->private_data = newdev;
  267. nonseekable_open(inode, file);
  268. return 0;
  269. }
  270. static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
  271. const struct input_absinfo *abs)
  272. {
  273. int min, max;
  274. min = abs->minimum;
  275. max = abs->maximum;
  276. if ((min != 0 || max != 0) && max <= min) {
  277. printk(KERN_DEBUG
  278. "%s: invalid abs[%02x] min:%d max:%d\n",
  279. UINPUT_NAME, code, min, max);
  280. return -EINVAL;
  281. }
  282. if (abs->flat > max - min) {
  283. printk(KERN_DEBUG
  284. "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
  285. UINPUT_NAME, code, abs->flat, min, max);
  286. return -EINVAL;
  287. }
  288. return 0;
  289. }
  290. static int uinput_validate_absbits(struct input_dev *dev)
  291. {
  292. unsigned int cnt;
  293. int error;
  294. if (!test_bit(EV_ABS, dev->evbit))
  295. return 0;
  296. /*
  297. * Check if absmin/absmax/absfuzz/absflat are sane.
  298. */
  299. for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
  300. if (!dev->absinfo)
  301. return -EINVAL;
  302. error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
  303. if (error)
  304. return error;
  305. }
  306. return 0;
  307. }
  308. static int uinput_allocate_device(struct uinput_device *udev)
  309. {
  310. udev->dev = input_allocate_device();
  311. if (!udev->dev)
  312. return -ENOMEM;
  313. udev->dev->event = uinput_dev_event;
  314. input_set_drvdata(udev->dev, udev);
  315. return 0;
  316. }
  317. static int uinput_dev_setup(struct uinput_device *udev,
  318. struct uinput_setup __user *arg)
  319. {
  320. struct uinput_setup setup;
  321. struct input_dev *dev;
  322. if (udev->state == UIST_CREATED)
  323. return -EINVAL;
  324. if (copy_from_user(&setup, arg, sizeof(setup)))
  325. return -EFAULT;
  326. if (!setup.name[0])
  327. return -EINVAL;
  328. dev = udev->dev;
  329. dev->id = setup.id;
  330. udev->ff_effects_max = setup.ff_effects_max;
  331. kfree(dev->name);
  332. dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
  333. if (!dev->name)
  334. return -ENOMEM;
  335. udev->state = UIST_SETUP_COMPLETE;
  336. return 0;
  337. }
  338. static int uinput_abs_setup(struct uinput_device *udev,
  339. struct uinput_setup __user *arg, size_t size)
  340. {
  341. struct uinput_abs_setup setup = {};
  342. struct input_dev *dev;
  343. int error;
  344. if (size > sizeof(setup))
  345. return -E2BIG;
  346. if (udev->state == UIST_CREATED)
  347. return -EINVAL;
  348. if (copy_from_user(&setup, arg, size))
  349. return -EFAULT;
  350. if (setup.code > ABS_MAX)
  351. return -ERANGE;
  352. dev = udev->dev;
  353. error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
  354. if (error)
  355. return error;
  356. input_alloc_absinfo(dev);
  357. if (!dev->absinfo)
  358. return -ENOMEM;
  359. set_bit(setup.code, dev->absbit);
  360. dev->absinfo[setup.code] = setup.absinfo;
  361. return 0;
  362. }
  363. /* legacy setup via write() */
  364. static int uinput_setup_device_legacy(struct uinput_device *udev,
  365. const char __user *buffer, size_t count)
  366. {
  367. struct uinput_user_dev *user_dev;
  368. struct input_dev *dev;
  369. int i;
  370. int retval;
  371. if (count != sizeof(struct uinput_user_dev))
  372. return -EINVAL;
  373. if (!udev->dev) {
  374. retval = uinput_allocate_device(udev);
  375. if (retval)
  376. return retval;
  377. }
  378. dev = udev->dev;
  379. user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
  380. if (IS_ERR(user_dev))
  381. return PTR_ERR(user_dev);
  382. udev->ff_effects_max = user_dev->ff_effects_max;
  383. /* Ensure name is filled in */
  384. if (!user_dev->name[0]) {
  385. retval = -EINVAL;
  386. goto exit;
  387. }
  388. kfree(dev->name);
  389. dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
  390. GFP_KERNEL);
  391. if (!dev->name) {
  392. retval = -ENOMEM;
  393. goto exit;
  394. }
  395. dev->id.bustype = user_dev->id.bustype;
  396. dev->id.vendor = user_dev->id.vendor;
  397. dev->id.product = user_dev->id.product;
  398. dev->id.version = user_dev->id.version;
  399. for (i = 0; i < ABS_CNT; i++) {
  400. input_abs_set_max(dev, i, user_dev->absmax[i]);
  401. input_abs_set_min(dev, i, user_dev->absmin[i]);
  402. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  403. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  404. }
  405. retval = uinput_validate_absbits(dev);
  406. if (retval < 0)
  407. goto exit;
  408. udev->state = UIST_SETUP_COMPLETE;
  409. retval = count;
  410. exit:
  411. kfree(user_dev);
  412. return retval;
  413. }
  414. static ssize_t uinput_inject_events(struct uinput_device *udev,
  415. const char __user *buffer, size_t count)
  416. {
  417. struct input_event ev;
  418. size_t bytes = 0;
  419. if (count != 0 && count < input_event_size())
  420. return -EINVAL;
  421. while (bytes + input_event_size() <= count) {
  422. /*
  423. * Note that even if some events were fetched successfully
  424. * we are still going to return EFAULT instead of partial
  425. * count to let userspace know that it got it's buffers
  426. * all wrong.
  427. */
  428. if (input_event_from_user(buffer + bytes, &ev))
  429. return -EFAULT;
  430. input_event(udev->dev, ev.type, ev.code, ev.value);
  431. bytes += input_event_size();
  432. }
  433. return bytes;
  434. }
  435. static ssize_t uinput_write(struct file *file, const char __user *buffer,
  436. size_t count, loff_t *ppos)
  437. {
  438. struct uinput_device *udev = file->private_data;
  439. int retval;
  440. if (count == 0)
  441. return 0;
  442. retval = mutex_lock_interruptible(&udev->mutex);
  443. if (retval)
  444. return retval;
  445. retval = udev->state == UIST_CREATED ?
  446. uinput_inject_events(udev, buffer, count) :
  447. uinput_setup_device_legacy(udev, buffer, count);
  448. mutex_unlock(&udev->mutex);
  449. return retval;
  450. }
  451. static bool uinput_fetch_next_event(struct uinput_device *udev,
  452. struct input_event *event)
  453. {
  454. bool have_event;
  455. spin_lock_irq(&udev->dev->event_lock);
  456. have_event = udev->head != udev->tail;
  457. if (have_event) {
  458. *event = udev->buff[udev->tail];
  459. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  460. }
  461. spin_unlock_irq(&udev->dev->event_lock);
  462. return have_event;
  463. }
  464. static ssize_t uinput_events_to_user(struct uinput_device *udev,
  465. char __user *buffer, size_t count)
  466. {
  467. struct input_event event;
  468. size_t read = 0;
  469. while (read + input_event_size() <= count &&
  470. uinput_fetch_next_event(udev, &event)) {
  471. if (input_event_to_user(buffer + read, &event))
  472. return -EFAULT;
  473. read += input_event_size();
  474. }
  475. return read;
  476. }
  477. static ssize_t uinput_read(struct file *file, char __user *buffer,
  478. size_t count, loff_t *ppos)
  479. {
  480. struct uinput_device *udev = file->private_data;
  481. ssize_t retval;
  482. if (count != 0 && count < input_event_size())
  483. return -EINVAL;
  484. do {
  485. retval = mutex_lock_interruptible(&udev->mutex);
  486. if (retval)
  487. return retval;
  488. if (udev->state != UIST_CREATED)
  489. retval = -ENODEV;
  490. else if (udev->head == udev->tail &&
  491. (file->f_flags & O_NONBLOCK))
  492. retval = -EAGAIN;
  493. else
  494. retval = uinput_events_to_user(udev, buffer, count);
  495. mutex_unlock(&udev->mutex);
  496. if (retval || count == 0)
  497. break;
  498. if (!(file->f_flags & O_NONBLOCK))
  499. retval = wait_event_interruptible(udev->waitq,
  500. udev->head != udev->tail ||
  501. udev->state != UIST_CREATED);
  502. } while (retval == 0);
  503. return retval;
  504. }
  505. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  506. {
  507. struct uinput_device *udev = file->private_data;
  508. poll_wait(file, &udev->waitq, wait);
  509. if (udev->head != udev->tail)
  510. return POLLIN | POLLRDNORM;
  511. return 0;
  512. }
  513. static int uinput_release(struct inode *inode, struct file *file)
  514. {
  515. struct uinput_device *udev = file->private_data;
  516. uinput_destroy_device(udev);
  517. kfree(udev);
  518. return 0;
  519. }
  520. #ifdef CONFIG_COMPAT
  521. struct uinput_ff_upload_compat {
  522. __u32 request_id;
  523. __s32 retval;
  524. struct ff_effect_compat effect;
  525. struct ff_effect_compat old;
  526. };
  527. static int uinput_ff_upload_to_user(char __user *buffer,
  528. const struct uinput_ff_upload *ff_up)
  529. {
  530. if (in_compat_syscall()) {
  531. struct uinput_ff_upload_compat ff_up_compat;
  532. ff_up_compat.request_id = ff_up->request_id;
  533. ff_up_compat.retval = ff_up->retval;
  534. /*
  535. * It so happens that the pointer that gives us the trouble
  536. * is the last field in the structure. Since we don't support
  537. * custom waveforms in uinput anyway we can just copy the whole
  538. * thing (to the compat size) and ignore the pointer.
  539. */
  540. memcpy(&ff_up_compat.effect, &ff_up->effect,
  541. sizeof(struct ff_effect_compat));
  542. memcpy(&ff_up_compat.old, &ff_up->old,
  543. sizeof(struct ff_effect_compat));
  544. if (copy_to_user(buffer, &ff_up_compat,
  545. sizeof(struct uinput_ff_upload_compat)))
  546. return -EFAULT;
  547. } else {
  548. if (copy_to_user(buffer, ff_up,
  549. sizeof(struct uinput_ff_upload)))
  550. return -EFAULT;
  551. }
  552. return 0;
  553. }
  554. static int uinput_ff_upload_from_user(const char __user *buffer,
  555. struct uinput_ff_upload *ff_up)
  556. {
  557. if (in_compat_syscall()) {
  558. struct uinput_ff_upload_compat ff_up_compat;
  559. if (copy_from_user(&ff_up_compat, buffer,
  560. sizeof(struct uinput_ff_upload_compat)))
  561. return -EFAULT;
  562. ff_up->request_id = ff_up_compat.request_id;
  563. ff_up->retval = ff_up_compat.retval;
  564. memcpy(&ff_up->effect, &ff_up_compat.effect,
  565. sizeof(struct ff_effect_compat));
  566. memcpy(&ff_up->old, &ff_up_compat.old,
  567. sizeof(struct ff_effect_compat));
  568. } else {
  569. if (copy_from_user(ff_up, buffer,
  570. sizeof(struct uinput_ff_upload)))
  571. return -EFAULT;
  572. }
  573. return 0;
  574. }
  575. #else
  576. static int uinput_ff_upload_to_user(char __user *buffer,
  577. const struct uinput_ff_upload *ff_up)
  578. {
  579. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  580. return -EFAULT;
  581. return 0;
  582. }
  583. static int uinput_ff_upload_from_user(const char __user *buffer,
  584. struct uinput_ff_upload *ff_up)
  585. {
  586. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  587. return -EFAULT;
  588. return 0;
  589. }
  590. #endif
  591. #define uinput_set_bit(_arg, _bit, _max) \
  592. ({ \
  593. int __ret = 0; \
  594. if (udev->state == UIST_CREATED) \
  595. __ret = -EINVAL; \
  596. else if ((_arg) > (_max)) \
  597. __ret = -EINVAL; \
  598. else set_bit((_arg), udev->dev->_bit); \
  599. __ret; \
  600. })
  601. static int uinput_str_to_user(void __user *dest, const char *str,
  602. unsigned int maxlen)
  603. {
  604. char __user *p = dest;
  605. int len, ret;
  606. if (!str)
  607. return -ENOENT;
  608. if (maxlen == 0)
  609. return -EINVAL;
  610. len = strlen(str) + 1;
  611. if (len > maxlen)
  612. len = maxlen;
  613. ret = copy_to_user(p, str, len);
  614. if (ret)
  615. return -EFAULT;
  616. /* force terminating '\0' */
  617. ret = put_user(0, p + len - 1);
  618. return ret ? -EFAULT : len;
  619. }
  620. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  621. unsigned long arg, void __user *p)
  622. {
  623. int retval;
  624. struct uinput_device *udev = file->private_data;
  625. struct uinput_ff_upload ff_up;
  626. struct uinput_ff_erase ff_erase;
  627. struct uinput_request *req;
  628. char *phys;
  629. const char *name;
  630. unsigned int size;
  631. retval = mutex_lock_interruptible(&udev->mutex);
  632. if (retval)
  633. return retval;
  634. if (!udev->dev) {
  635. retval = uinput_allocate_device(udev);
  636. if (retval)
  637. goto out;
  638. }
  639. switch (cmd) {
  640. case UI_GET_VERSION:
  641. if (put_user(UINPUT_VERSION,
  642. (unsigned int __user *)p))
  643. retval = -EFAULT;
  644. goto out;
  645. case UI_DEV_CREATE:
  646. retval = uinput_create_device(udev);
  647. goto out;
  648. case UI_DEV_DESTROY:
  649. uinput_destroy_device(udev);
  650. goto out;
  651. case UI_DEV_SETUP:
  652. retval = uinput_dev_setup(udev, p);
  653. goto out;
  654. /* UI_ABS_SETUP is handled in the variable size ioctls */
  655. case UI_SET_EVBIT:
  656. retval = uinput_set_bit(arg, evbit, EV_MAX);
  657. goto out;
  658. case UI_SET_KEYBIT:
  659. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  660. goto out;
  661. case UI_SET_RELBIT:
  662. retval = uinput_set_bit(arg, relbit, REL_MAX);
  663. goto out;
  664. case UI_SET_ABSBIT:
  665. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  666. goto out;
  667. case UI_SET_MSCBIT:
  668. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  669. goto out;
  670. case UI_SET_LEDBIT:
  671. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  672. goto out;
  673. case UI_SET_SNDBIT:
  674. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  675. goto out;
  676. case UI_SET_FFBIT:
  677. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  678. goto out;
  679. case UI_SET_SWBIT:
  680. retval = uinput_set_bit(arg, swbit, SW_MAX);
  681. goto out;
  682. case UI_SET_PROPBIT:
  683. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  684. goto out;
  685. case UI_SET_PHYS:
  686. if (udev->state == UIST_CREATED) {
  687. retval = -EINVAL;
  688. goto out;
  689. }
  690. phys = strndup_user(p, 1024);
  691. if (IS_ERR(phys)) {
  692. retval = PTR_ERR(phys);
  693. goto out;
  694. }
  695. kfree(udev->dev->phys);
  696. udev->dev->phys = phys;
  697. goto out;
  698. case UI_BEGIN_FF_UPLOAD:
  699. retval = uinput_ff_upload_from_user(p, &ff_up);
  700. if (retval)
  701. goto out;
  702. req = uinput_request_find(udev, ff_up.request_id);
  703. if (!req || req->code != UI_FF_UPLOAD ||
  704. !req->u.upload.effect) {
  705. retval = -EINVAL;
  706. goto out;
  707. }
  708. ff_up.retval = 0;
  709. ff_up.effect = *req->u.upload.effect;
  710. if (req->u.upload.old)
  711. ff_up.old = *req->u.upload.old;
  712. else
  713. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  714. retval = uinput_ff_upload_to_user(p, &ff_up);
  715. goto out;
  716. case UI_BEGIN_FF_ERASE:
  717. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  718. retval = -EFAULT;
  719. goto out;
  720. }
  721. req = uinput_request_find(udev, ff_erase.request_id);
  722. if (!req || req->code != UI_FF_ERASE) {
  723. retval = -EINVAL;
  724. goto out;
  725. }
  726. ff_erase.retval = 0;
  727. ff_erase.effect_id = req->u.effect_id;
  728. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  729. retval = -EFAULT;
  730. goto out;
  731. }
  732. goto out;
  733. case UI_END_FF_UPLOAD:
  734. retval = uinput_ff_upload_from_user(p, &ff_up);
  735. if (retval)
  736. goto out;
  737. req = uinput_request_find(udev, ff_up.request_id);
  738. if (!req || req->code != UI_FF_UPLOAD ||
  739. !req->u.upload.effect) {
  740. retval = -EINVAL;
  741. goto out;
  742. }
  743. req->retval = ff_up.retval;
  744. uinput_request_done(udev, req);
  745. goto out;
  746. case UI_END_FF_ERASE:
  747. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  748. retval = -EFAULT;
  749. goto out;
  750. }
  751. req = uinput_request_find(udev, ff_erase.request_id);
  752. if (!req || req->code != UI_FF_ERASE) {
  753. retval = -EINVAL;
  754. goto out;
  755. }
  756. req->retval = ff_erase.retval;
  757. uinput_request_done(udev, req);
  758. goto out;
  759. }
  760. size = _IOC_SIZE(cmd);
  761. /* Now check variable-length commands */
  762. switch (cmd & ~IOCSIZE_MASK) {
  763. case UI_GET_SYSNAME(0):
  764. if (udev->state != UIST_CREATED) {
  765. retval = -ENOENT;
  766. goto out;
  767. }
  768. name = dev_name(&udev->dev->dev);
  769. retval = uinput_str_to_user(p, name, size);
  770. goto out;
  771. case UI_ABS_SETUP & ~IOCSIZE_MASK:
  772. retval = uinput_abs_setup(udev, p, size);
  773. goto out;
  774. }
  775. retval = -EINVAL;
  776. out:
  777. mutex_unlock(&udev->mutex);
  778. return retval;
  779. }
  780. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  781. {
  782. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  783. }
  784. #ifdef CONFIG_COMPAT
  785. #define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
  786. static long uinput_compat_ioctl(struct file *file,
  787. unsigned int cmd, unsigned long arg)
  788. {
  789. if (cmd == UI_SET_PHYS_COMPAT)
  790. cmd = UI_SET_PHYS;
  791. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  792. }
  793. #endif
  794. static const struct file_operations uinput_fops = {
  795. .owner = THIS_MODULE,
  796. .open = uinput_open,
  797. .release = uinput_release,
  798. .read = uinput_read,
  799. .write = uinput_write,
  800. .poll = uinput_poll,
  801. .unlocked_ioctl = uinput_ioctl,
  802. #ifdef CONFIG_COMPAT
  803. .compat_ioctl = uinput_compat_ioctl,
  804. #endif
  805. .llseek = no_llseek,
  806. };
  807. static struct miscdevice uinput_misc = {
  808. .fops = &uinput_fops,
  809. .minor = UINPUT_MINOR,
  810. .name = UINPUT_NAME,
  811. };
  812. module_misc_device(uinput_misc);
  813. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  814. MODULE_ALIAS("devname:" UINPUT_NAME);
  815. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  816. MODULE_DESCRIPTION("User level driver support for input subsystem");
  817. MODULE_LICENSE("GPL");
  818. MODULE_VERSION("0.3");