uinput.c 18 KB

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