uinput.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
  24. * - updated ff support for the changes in kernel interface
  25. * - added MODULE_VERSION
  26. * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
  27. * - added force feedback support
  28. * - added UI_SET_PHYS
  29. * 0.1 20/06/2002
  30. * - first public version
  31. */
  32. #include <linux/poll.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/fs.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/uinput.h>
  40. #include "../input-compat.h"
  41. static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  42. {
  43. struct uinput_device *udev = input_get_drvdata(dev);
  44. udev->buff[udev->head].type = type;
  45. udev->buff[udev->head].code = code;
  46. udev->buff[udev->head].value = value;
  47. do_gettimeofday(&udev->buff[udev->head].time);
  48. udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
  49. wake_up_interruptible(&udev->waitq);
  50. return 0;
  51. }
  52. /* Atomically allocate an ID for the given request. Returns 0 on success. */
  53. static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
  54. {
  55. int id;
  56. int err = -1;
  57. spin_lock(&udev->requests_lock);
  58. for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
  59. if (!udev->requests[id]) {
  60. request->id = id;
  61. udev->requests[id] = request;
  62. err = 0;
  63. break;
  64. }
  65. }
  66. spin_unlock(&udev->requests_lock);
  67. return err;
  68. }
  69. static struct uinput_request *uinput_request_find(struct uinput_device *udev, int id)
  70. {
  71. /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
  72. if (id >= UINPUT_NUM_REQUESTS || id < 0)
  73. return NULL;
  74. return udev->requests[id];
  75. }
  76. static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
  77. {
  78. /* Allocate slot. If none are available right away, wait. */
  79. return wait_event_interruptible(udev->requests_waitq,
  80. !uinput_request_alloc_id(udev, request));
  81. }
  82. static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
  83. {
  84. /* Mark slot as available */
  85. udev->requests[request->id] = NULL;
  86. wake_up(&udev->requests_waitq);
  87. complete(&request->done);
  88. }
  89. static int uinput_request_submit(struct uinput_device *udev, struct uinput_request *request)
  90. {
  91. int retval;
  92. retval = uinput_request_reserve_slot(udev, request);
  93. if (retval)
  94. return retval;
  95. retval = mutex_lock_interruptible(&udev->mutex);
  96. if (retval)
  97. return retval;
  98. if (udev->state != UIST_CREATED) {
  99. retval = -ENODEV;
  100. goto out;
  101. }
  102. /* Tell our userspace app about this new request by queueing an input event */
  103. uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
  104. out:
  105. mutex_unlock(&udev->mutex);
  106. return retval;
  107. }
  108. /*
  109. * Fail all ouitstanding requests so handlers don't wait for the userspace
  110. * to finish processing them.
  111. */
  112. static void uinput_flush_requests(struct uinput_device *udev)
  113. {
  114. struct uinput_request *request;
  115. int i;
  116. spin_lock(&udev->requests_lock);
  117. for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
  118. request = udev->requests[i];
  119. if (request) {
  120. request->retval = -ENODEV;
  121. uinput_request_done(udev, request);
  122. }
  123. }
  124. spin_unlock(&udev->requests_lock);
  125. }
  126. static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
  127. {
  128. uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
  129. }
  130. static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
  131. {
  132. uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
  133. }
  134. static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
  135. {
  136. return uinput_dev_event(dev, EV_FF, effect_id, value);
  137. }
  138. static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
  139. {
  140. struct uinput_device *udev = input_get_drvdata(dev);
  141. struct uinput_request request;
  142. int retval;
  143. /*
  144. * uinput driver does not currently support periodic effects with
  145. * custom waveform since it does not have a way to pass buffer of
  146. * samples (custom_data) to userspace. If ever there is a device
  147. * supporting custom waveforms we would need to define an additional
  148. * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
  149. */
  150. if (effect->type == FF_PERIODIC &&
  151. effect->u.periodic.waveform == FF_CUSTOM)
  152. return -EINVAL;
  153. request.id = -1;
  154. init_completion(&request.done);
  155. request.code = UI_FF_UPLOAD;
  156. request.u.upload.effect = effect;
  157. request.u.upload.old = old;
  158. retval = uinput_request_submit(udev, &request);
  159. if (!retval) {
  160. wait_for_completion(&request.done);
  161. retval = request.retval;
  162. }
  163. return retval;
  164. }
  165. static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
  166. {
  167. struct uinput_device *udev = input_get_drvdata(dev);
  168. struct uinput_request request;
  169. int retval;
  170. if (!test_bit(EV_FF, dev->evbit))
  171. return -ENOSYS;
  172. request.id = -1;
  173. init_completion(&request.done);
  174. request.code = UI_FF_ERASE;
  175. request.u.effect_id = effect_id;
  176. retval = uinput_request_submit(udev, &request);
  177. if (!retval) {
  178. wait_for_completion(&request.done);
  179. retval = request.retval;
  180. }
  181. return retval;
  182. }
  183. static void uinput_destroy_device(struct uinput_device *udev)
  184. {
  185. const char *name, *phys;
  186. struct input_dev *dev = udev->dev;
  187. enum uinput_state old_state = udev->state;
  188. udev->state = UIST_NEW_DEVICE;
  189. if (dev) {
  190. name = dev->name;
  191. phys = dev->phys;
  192. if (old_state == UIST_CREATED) {
  193. uinput_flush_requests(udev);
  194. input_unregister_device(dev);
  195. } else {
  196. input_free_device(dev);
  197. }
  198. kfree(name);
  199. kfree(phys);
  200. udev->dev = NULL;
  201. }
  202. }
  203. static int uinput_create_device(struct uinput_device *udev)
  204. {
  205. struct input_dev *dev = udev->dev;
  206. int error;
  207. if (udev->state != UIST_SETUP_COMPLETE) {
  208. printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
  209. return -EINVAL;
  210. }
  211. if (udev->ff_effects_max) {
  212. error = input_ff_create(dev, udev->ff_effects_max);
  213. if (error)
  214. goto fail1;
  215. dev->ff->upload = uinput_dev_upload_effect;
  216. dev->ff->erase = uinput_dev_erase_effect;
  217. dev->ff->playback = uinput_dev_playback;
  218. dev->ff->set_gain = uinput_dev_set_gain;
  219. dev->ff->set_autocenter = uinput_dev_set_autocenter;
  220. }
  221. error = input_register_device(udev->dev);
  222. if (error)
  223. goto fail2;
  224. udev->state = UIST_CREATED;
  225. return 0;
  226. fail2: input_ff_destroy(dev);
  227. fail1: uinput_destroy_device(udev);
  228. return error;
  229. }
  230. static int uinput_open(struct inode *inode, struct file *file)
  231. {
  232. struct uinput_device *newdev;
  233. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  234. if (!newdev)
  235. return -ENOMEM;
  236. mutex_init(&newdev->mutex);
  237. spin_lock_init(&newdev->requests_lock);
  238. init_waitqueue_head(&newdev->requests_waitq);
  239. init_waitqueue_head(&newdev->waitq);
  240. newdev->state = UIST_NEW_DEVICE;
  241. file->private_data = newdev;
  242. nonseekable_open(inode, file);
  243. return 0;
  244. }
  245. static int uinput_validate_absbits(struct input_dev *dev)
  246. {
  247. unsigned int cnt;
  248. int retval = 0;
  249. for (cnt = 0; cnt < ABS_CNT; cnt++) {
  250. if (!test_bit(cnt, dev->absbit))
  251. continue;
  252. if (input_abs_get_max(dev, cnt) <= input_abs_get_min(dev, cnt)) {
  253. printk(KERN_DEBUG
  254. "%s: invalid abs[%02x] min:%d max:%d\n",
  255. UINPUT_NAME, cnt,
  256. input_abs_get_min(dev, cnt),
  257. input_abs_get_max(dev, cnt));
  258. retval = -EINVAL;
  259. break;
  260. }
  261. if (input_abs_get_flat(dev, cnt) >
  262. input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
  263. printk(KERN_DEBUG
  264. "%s: abs_flat #%02x out of range: %d "
  265. "(min:%d/max:%d)\n",
  266. UINPUT_NAME, cnt,
  267. input_abs_get_flat(dev, cnt),
  268. input_abs_get_min(dev, cnt),
  269. input_abs_get_max(dev, cnt));
  270. retval = -EINVAL;
  271. break;
  272. }
  273. }
  274. return retval;
  275. }
  276. static int uinput_allocate_device(struct uinput_device *udev)
  277. {
  278. udev->dev = input_allocate_device();
  279. if (!udev->dev)
  280. return -ENOMEM;
  281. udev->dev->event = uinput_dev_event;
  282. input_set_drvdata(udev->dev, udev);
  283. return 0;
  284. }
  285. static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
  286. {
  287. struct uinput_user_dev *user_dev;
  288. struct input_dev *dev;
  289. char *name;
  290. int i, size;
  291. int retval;
  292. if (count != sizeof(struct uinput_user_dev))
  293. return -EINVAL;
  294. if (!udev->dev) {
  295. retval = uinput_allocate_device(udev);
  296. if (retval)
  297. return retval;
  298. }
  299. dev = udev->dev;
  300. user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
  301. if (!user_dev)
  302. return -ENOMEM;
  303. if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
  304. retval = -EFAULT;
  305. goto exit;
  306. }
  307. udev->ff_effects_max = user_dev->ff_effects_max;
  308. size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
  309. if (!size) {
  310. retval = -EINVAL;
  311. goto exit;
  312. }
  313. kfree(dev->name);
  314. dev->name = name = kmalloc(size, GFP_KERNEL);
  315. if (!name) {
  316. retval = -ENOMEM;
  317. goto exit;
  318. }
  319. strlcpy(name, user_dev->name, size);
  320. dev->id.bustype = user_dev->id.bustype;
  321. dev->id.vendor = user_dev->id.vendor;
  322. dev->id.product = user_dev->id.product;
  323. dev->id.version = user_dev->id.version;
  324. for (i = 0; i < ABS_CNT; i++) {
  325. input_abs_set_max(dev, i, user_dev->absmax[i]);
  326. input_abs_set_min(dev, i, user_dev->absmin[i]);
  327. input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
  328. input_abs_set_flat(dev, i, user_dev->absflat[i]);
  329. }
  330. /* check if absmin/absmax/absfuzz/absflat are filled as
  331. * told in Documentation/input/input-programming.txt */
  332. if (test_bit(EV_ABS, dev->evbit)) {
  333. retval = uinput_validate_absbits(dev);
  334. if (retval < 0)
  335. goto exit;
  336. }
  337. udev->state = UIST_SETUP_COMPLETE;
  338. retval = count;
  339. exit:
  340. kfree(user_dev);
  341. return retval;
  342. }
  343. static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
  344. {
  345. struct input_event ev;
  346. if (count < input_event_size())
  347. return -EINVAL;
  348. if (input_event_from_user(buffer, &ev))
  349. return -EFAULT;
  350. input_event(udev->dev, ev.type, ev.code, ev.value);
  351. return input_event_size();
  352. }
  353. static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  354. {
  355. struct uinput_device *udev = file->private_data;
  356. int retval;
  357. retval = mutex_lock_interruptible(&udev->mutex);
  358. if (retval)
  359. return retval;
  360. retval = udev->state == UIST_CREATED ?
  361. uinput_inject_event(udev, buffer, count) :
  362. uinput_setup_device(udev, buffer, count);
  363. mutex_unlock(&udev->mutex);
  364. return retval;
  365. }
  366. static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  367. {
  368. struct uinput_device *udev = file->private_data;
  369. int retval = 0;
  370. if (udev->state != UIST_CREATED)
  371. return -ENODEV;
  372. if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
  373. return -EAGAIN;
  374. retval = wait_event_interruptible(udev->waitq,
  375. udev->head != udev->tail || udev->state != UIST_CREATED);
  376. if (retval)
  377. return retval;
  378. retval = mutex_lock_interruptible(&udev->mutex);
  379. if (retval)
  380. return retval;
  381. if (udev->state != UIST_CREATED) {
  382. retval = -ENODEV;
  383. goto out;
  384. }
  385. while (udev->head != udev->tail && retval + input_event_size() <= count) {
  386. if (input_event_to_user(buffer + retval, &udev->buff[udev->tail])) {
  387. retval = -EFAULT;
  388. goto out;
  389. }
  390. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  391. retval += input_event_size();
  392. }
  393. out:
  394. mutex_unlock(&udev->mutex);
  395. return retval;
  396. }
  397. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  398. {
  399. struct uinput_device *udev = file->private_data;
  400. poll_wait(file, &udev->waitq, wait);
  401. if (udev->head != udev->tail)
  402. return POLLIN | POLLRDNORM;
  403. return 0;
  404. }
  405. static int uinput_release(struct inode *inode, struct file *file)
  406. {
  407. struct uinput_device *udev = file->private_data;
  408. uinput_destroy_device(udev);
  409. kfree(udev);
  410. return 0;
  411. }
  412. #ifdef CONFIG_COMPAT
  413. struct uinput_ff_upload_compat {
  414. int request_id;
  415. int retval;
  416. struct ff_effect_compat effect;
  417. struct ff_effect_compat old;
  418. };
  419. static int uinput_ff_upload_to_user(char __user *buffer,
  420. const struct uinput_ff_upload *ff_up)
  421. {
  422. if (INPUT_COMPAT_TEST) {
  423. struct uinput_ff_upload_compat ff_up_compat;
  424. ff_up_compat.request_id = ff_up->request_id;
  425. ff_up_compat.retval = ff_up->retval;
  426. /*
  427. * It so happens that the pointer that gives us the trouble
  428. * is the last field in the structure. Since we don't support
  429. * custom waveforms in uinput anyway we can just copy the whole
  430. * thing (to the compat size) and ignore the pointer.
  431. */
  432. memcpy(&ff_up_compat.effect, &ff_up->effect,
  433. sizeof(struct ff_effect_compat));
  434. memcpy(&ff_up_compat.old, &ff_up->old,
  435. sizeof(struct ff_effect_compat));
  436. if (copy_to_user(buffer, &ff_up_compat,
  437. sizeof(struct uinput_ff_upload_compat)))
  438. return -EFAULT;
  439. } else {
  440. if (copy_to_user(buffer, ff_up,
  441. sizeof(struct uinput_ff_upload)))
  442. return -EFAULT;
  443. }
  444. return 0;
  445. }
  446. static int uinput_ff_upload_from_user(const char __user *buffer,
  447. struct uinput_ff_upload *ff_up)
  448. {
  449. if (INPUT_COMPAT_TEST) {
  450. struct uinput_ff_upload_compat ff_up_compat;
  451. if (copy_from_user(&ff_up_compat, buffer,
  452. sizeof(struct uinput_ff_upload_compat)))
  453. return -EFAULT;
  454. ff_up->request_id = ff_up_compat.request_id;
  455. ff_up->retval = ff_up_compat.retval;
  456. memcpy(&ff_up->effect, &ff_up_compat.effect,
  457. sizeof(struct ff_effect_compat));
  458. memcpy(&ff_up->old, &ff_up_compat.old,
  459. sizeof(struct ff_effect_compat));
  460. } else {
  461. if (copy_from_user(ff_up, buffer,
  462. sizeof(struct uinput_ff_upload)))
  463. return -EFAULT;
  464. }
  465. return 0;
  466. }
  467. #else
  468. static int uinput_ff_upload_to_user(char __user *buffer,
  469. const struct uinput_ff_upload *ff_up)
  470. {
  471. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  472. return -EFAULT;
  473. return 0;
  474. }
  475. static int uinput_ff_upload_from_user(const char __user *buffer,
  476. struct uinput_ff_upload *ff_up)
  477. {
  478. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. #endif
  483. #define uinput_set_bit(_arg, _bit, _max) \
  484. ({ \
  485. int __ret = 0; \
  486. if (udev->state == UIST_CREATED) \
  487. __ret = -EINVAL; \
  488. else if ((_arg) > (_max)) \
  489. __ret = -EINVAL; \
  490. else set_bit((_arg), udev->dev->_bit); \
  491. __ret; \
  492. })
  493. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  494. unsigned long arg, void __user *p)
  495. {
  496. int retval;
  497. struct uinput_device *udev = file->private_data;
  498. struct uinput_ff_upload ff_up;
  499. struct uinput_ff_erase ff_erase;
  500. struct uinput_request *req;
  501. int length;
  502. char *phys;
  503. retval = mutex_lock_interruptible(&udev->mutex);
  504. if (retval)
  505. return retval;
  506. if (!udev->dev) {
  507. retval = uinput_allocate_device(udev);
  508. if (retval)
  509. goto out;
  510. }
  511. switch (cmd) {
  512. case UI_DEV_CREATE:
  513. retval = uinput_create_device(udev);
  514. break;
  515. case UI_DEV_DESTROY:
  516. uinput_destroy_device(udev);
  517. break;
  518. case UI_SET_EVBIT:
  519. retval = uinput_set_bit(arg, evbit, EV_MAX);
  520. break;
  521. case UI_SET_KEYBIT:
  522. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  523. break;
  524. case UI_SET_RELBIT:
  525. retval = uinput_set_bit(arg, relbit, REL_MAX);
  526. break;
  527. case UI_SET_ABSBIT:
  528. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  529. break;
  530. case UI_SET_MSCBIT:
  531. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  532. break;
  533. case UI_SET_LEDBIT:
  534. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  535. break;
  536. case UI_SET_SNDBIT:
  537. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  538. break;
  539. case UI_SET_FFBIT:
  540. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  541. break;
  542. case UI_SET_SWBIT:
  543. retval = uinput_set_bit(arg, swbit, SW_MAX);
  544. break;
  545. case UI_SET_PHYS:
  546. if (udev->state == UIST_CREATED) {
  547. retval = -EINVAL;
  548. goto out;
  549. }
  550. length = strnlen_user(p, 1024);
  551. if (length <= 0) {
  552. retval = -EFAULT;
  553. break;
  554. }
  555. kfree(udev->dev->phys);
  556. udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
  557. if (!phys) {
  558. retval = -ENOMEM;
  559. break;
  560. }
  561. if (copy_from_user(phys, p, length)) {
  562. udev->dev->phys = NULL;
  563. kfree(phys);
  564. retval = -EFAULT;
  565. break;
  566. }
  567. phys[length - 1] = '\0';
  568. break;
  569. case UI_BEGIN_FF_UPLOAD:
  570. retval = uinput_ff_upload_from_user(p, &ff_up);
  571. if (retval)
  572. break;
  573. req = uinput_request_find(udev, ff_up.request_id);
  574. if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
  575. retval = -EINVAL;
  576. break;
  577. }
  578. ff_up.retval = 0;
  579. ff_up.effect = *req->u.upload.effect;
  580. if (req->u.upload.old)
  581. ff_up.old = *req->u.upload.old;
  582. else
  583. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  584. retval = uinput_ff_upload_to_user(p, &ff_up);
  585. break;
  586. case UI_BEGIN_FF_ERASE:
  587. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  588. retval = -EFAULT;
  589. break;
  590. }
  591. req = uinput_request_find(udev, ff_erase.request_id);
  592. if (!req || req->code != UI_FF_ERASE) {
  593. retval = -EINVAL;
  594. break;
  595. }
  596. ff_erase.retval = 0;
  597. ff_erase.effect_id = req->u.effect_id;
  598. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  599. retval = -EFAULT;
  600. break;
  601. }
  602. break;
  603. case UI_END_FF_UPLOAD:
  604. retval = uinput_ff_upload_from_user(p, &ff_up);
  605. if (retval)
  606. break;
  607. req = uinput_request_find(udev, ff_up.request_id);
  608. if (!req || req->code != UI_FF_UPLOAD ||
  609. !req->u.upload.effect) {
  610. retval = -EINVAL;
  611. break;
  612. }
  613. req->retval = ff_up.retval;
  614. uinput_request_done(udev, req);
  615. break;
  616. case UI_END_FF_ERASE:
  617. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  618. retval = -EFAULT;
  619. break;
  620. }
  621. req = uinput_request_find(udev, ff_erase.request_id);
  622. if (!req || req->code != UI_FF_ERASE) {
  623. retval = -EINVAL;
  624. break;
  625. }
  626. req->retval = ff_erase.retval;
  627. uinput_request_done(udev, req);
  628. break;
  629. default:
  630. retval = -EINVAL;
  631. }
  632. out:
  633. mutex_unlock(&udev->mutex);
  634. return retval;
  635. }
  636. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  637. {
  638. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  639. }
  640. #ifdef CONFIG_COMPAT
  641. static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  642. {
  643. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  644. }
  645. #endif
  646. static const struct file_operations uinput_fops = {
  647. .owner = THIS_MODULE,
  648. .open = uinput_open,
  649. .release = uinput_release,
  650. .read = uinput_read,
  651. .write = uinput_write,
  652. .poll = uinput_poll,
  653. .unlocked_ioctl = uinput_ioctl,
  654. #ifdef CONFIG_COMPAT
  655. .compat_ioctl = uinput_compat_ioctl,
  656. #endif
  657. };
  658. static struct miscdevice uinput_misc = {
  659. .fops = &uinput_fops,
  660. .minor = UINPUT_MINOR,
  661. .name = UINPUT_NAME,
  662. };
  663. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  664. MODULE_ALIAS("devname:" UINPUT_NAME);
  665. static int __init uinput_init(void)
  666. {
  667. return misc_register(&uinput_misc);
  668. }
  669. static void __exit uinput_exit(void)
  670. {
  671. misc_deregister(&uinput_misc);
  672. }
  673. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  674. MODULE_DESCRIPTION("User level driver support for input subsystem");
  675. MODULE_LICENSE("GPL");
  676. MODULE_VERSION("0.3");
  677. module_init(uinput_init);
  678. module_exit(uinput_exit);