uinput.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 ((dev->absmax[cnt] <= dev->absmin[cnt])) {
  253. printk(KERN_DEBUG
  254. "%s: invalid abs[%02x] min:%d max:%d\n",
  255. UINPUT_NAME, cnt,
  256. dev->absmin[cnt], dev->absmax[cnt]);
  257. retval = -EINVAL;
  258. break;
  259. }
  260. if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
  261. printk(KERN_DEBUG
  262. "%s: absflat[%02x] out of range: %d "
  263. "(min:%d/max:%d)\n",
  264. UINPUT_NAME, cnt, dev->absflat[cnt],
  265. dev->absmin[cnt], dev->absmax[cnt]);
  266. retval = -EINVAL;
  267. break;
  268. }
  269. }
  270. return retval;
  271. }
  272. static int uinput_allocate_device(struct uinput_device *udev)
  273. {
  274. udev->dev = input_allocate_device();
  275. if (!udev->dev)
  276. return -ENOMEM;
  277. udev->dev->event = uinput_dev_event;
  278. input_set_drvdata(udev->dev, udev);
  279. return 0;
  280. }
  281. static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
  282. {
  283. struct uinput_user_dev *user_dev;
  284. struct input_dev *dev;
  285. char *name;
  286. int size;
  287. int retval;
  288. if (count != sizeof(struct uinput_user_dev))
  289. return -EINVAL;
  290. if (!udev->dev) {
  291. retval = uinput_allocate_device(udev);
  292. if (retval)
  293. return retval;
  294. }
  295. dev = udev->dev;
  296. user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
  297. if (!user_dev)
  298. return -ENOMEM;
  299. if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
  300. retval = -EFAULT;
  301. goto exit;
  302. }
  303. udev->ff_effects_max = user_dev->ff_effects_max;
  304. size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
  305. if (!size) {
  306. retval = -EINVAL;
  307. goto exit;
  308. }
  309. kfree(dev->name);
  310. dev->name = name = kmalloc(size, GFP_KERNEL);
  311. if (!name) {
  312. retval = -ENOMEM;
  313. goto exit;
  314. }
  315. strlcpy(name, user_dev->name, size);
  316. dev->id.bustype = user_dev->id.bustype;
  317. dev->id.vendor = user_dev->id.vendor;
  318. dev->id.product = user_dev->id.product;
  319. dev->id.version = user_dev->id.version;
  320. size = sizeof(int) * ABS_CNT;
  321. memcpy(dev->absmax, user_dev->absmax, size);
  322. memcpy(dev->absmin, user_dev->absmin, size);
  323. memcpy(dev->absfuzz, user_dev->absfuzz, size);
  324. memcpy(dev->absflat, user_dev->absflat, size);
  325. /* check if absmin/absmax/absfuzz/absflat are filled as
  326. * told in Documentation/input/input-programming.txt */
  327. if (test_bit(EV_ABS, dev->evbit)) {
  328. retval = uinput_validate_absbits(dev);
  329. if (retval < 0)
  330. goto exit;
  331. }
  332. udev->state = UIST_SETUP_COMPLETE;
  333. retval = count;
  334. exit:
  335. kfree(user_dev);
  336. return retval;
  337. }
  338. static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
  339. {
  340. struct input_event ev;
  341. if (count < input_event_size())
  342. return -EINVAL;
  343. if (input_event_from_user(buffer, &ev))
  344. return -EFAULT;
  345. input_event(udev->dev, ev.type, ev.code, ev.value);
  346. return input_event_size();
  347. }
  348. static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  349. {
  350. struct uinput_device *udev = file->private_data;
  351. int retval;
  352. retval = mutex_lock_interruptible(&udev->mutex);
  353. if (retval)
  354. return retval;
  355. retval = udev->state == UIST_CREATED ?
  356. uinput_inject_event(udev, buffer, count) :
  357. uinput_setup_device(udev, buffer, count);
  358. mutex_unlock(&udev->mutex);
  359. return retval;
  360. }
  361. static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  362. {
  363. struct uinput_device *udev = file->private_data;
  364. int retval = 0;
  365. if (udev->state != UIST_CREATED)
  366. return -ENODEV;
  367. if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
  368. return -EAGAIN;
  369. retval = wait_event_interruptible(udev->waitq,
  370. udev->head != udev->tail || udev->state != UIST_CREATED);
  371. if (retval)
  372. return retval;
  373. retval = mutex_lock_interruptible(&udev->mutex);
  374. if (retval)
  375. return retval;
  376. if (udev->state != UIST_CREATED) {
  377. retval = -ENODEV;
  378. goto out;
  379. }
  380. while (udev->head != udev->tail && retval + input_event_size() <= count) {
  381. if (input_event_to_user(buffer + retval, &udev->buff[udev->tail])) {
  382. retval = -EFAULT;
  383. goto out;
  384. }
  385. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  386. retval += input_event_size();
  387. }
  388. out:
  389. mutex_unlock(&udev->mutex);
  390. return retval;
  391. }
  392. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  393. {
  394. struct uinput_device *udev = file->private_data;
  395. poll_wait(file, &udev->waitq, wait);
  396. if (udev->head != udev->tail)
  397. return POLLIN | POLLRDNORM;
  398. return 0;
  399. }
  400. static int uinput_release(struct inode *inode, struct file *file)
  401. {
  402. struct uinput_device *udev = file->private_data;
  403. uinput_destroy_device(udev);
  404. kfree(udev);
  405. return 0;
  406. }
  407. #ifdef CONFIG_COMPAT
  408. struct uinput_ff_upload_compat {
  409. int request_id;
  410. int retval;
  411. struct ff_effect_compat effect;
  412. struct ff_effect_compat old;
  413. };
  414. static int uinput_ff_upload_to_user(char __user *buffer,
  415. const struct uinput_ff_upload *ff_up)
  416. {
  417. if (INPUT_COMPAT_TEST) {
  418. struct uinput_ff_upload_compat ff_up_compat;
  419. ff_up_compat.request_id = ff_up->request_id;
  420. ff_up_compat.retval = ff_up->retval;
  421. /*
  422. * It so happens that the pointer that gives us the trouble
  423. * is the last field in the structure. Since we don't support
  424. * custom waveforms in uinput anyway we can just copy the whole
  425. * thing (to the compat size) and ignore the pointer.
  426. */
  427. memcpy(&ff_up_compat.effect, &ff_up->effect,
  428. sizeof(struct ff_effect_compat));
  429. memcpy(&ff_up_compat.old, &ff_up->old,
  430. sizeof(struct ff_effect_compat));
  431. if (copy_to_user(buffer, &ff_up_compat,
  432. sizeof(struct uinput_ff_upload_compat)))
  433. return -EFAULT;
  434. } else {
  435. if (copy_to_user(buffer, ff_up,
  436. sizeof(struct uinput_ff_upload)))
  437. return -EFAULT;
  438. }
  439. return 0;
  440. }
  441. static int uinput_ff_upload_from_user(const char __user *buffer,
  442. struct uinput_ff_upload *ff_up)
  443. {
  444. if (INPUT_COMPAT_TEST) {
  445. struct uinput_ff_upload_compat ff_up_compat;
  446. if (copy_from_user(&ff_up_compat, buffer,
  447. sizeof(struct uinput_ff_upload_compat)))
  448. return -EFAULT;
  449. ff_up->request_id = ff_up_compat.request_id;
  450. ff_up->retval = ff_up_compat.retval;
  451. memcpy(&ff_up->effect, &ff_up_compat.effect,
  452. sizeof(struct ff_effect_compat));
  453. memcpy(&ff_up->old, &ff_up_compat.old,
  454. sizeof(struct ff_effect_compat));
  455. } else {
  456. if (copy_from_user(ff_up, buffer,
  457. sizeof(struct uinput_ff_upload)))
  458. return -EFAULT;
  459. }
  460. return 0;
  461. }
  462. #else
  463. static int uinput_ff_upload_to_user(char __user *buffer,
  464. const struct uinput_ff_upload *ff_up)
  465. {
  466. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  467. return -EFAULT;
  468. return 0;
  469. }
  470. static int uinput_ff_upload_from_user(const char __user *buffer,
  471. struct uinput_ff_upload *ff_up)
  472. {
  473. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  474. return -EFAULT;
  475. return 0;
  476. }
  477. #endif
  478. #define uinput_set_bit(_arg, _bit, _max) \
  479. ({ \
  480. int __ret = 0; \
  481. if (udev->state == UIST_CREATED) \
  482. __ret = -EINVAL; \
  483. else if ((_arg) > (_max)) \
  484. __ret = -EINVAL; \
  485. else set_bit((_arg), udev->dev->_bit); \
  486. __ret; \
  487. })
  488. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  489. unsigned long arg, void __user *p)
  490. {
  491. int retval;
  492. struct uinput_device *udev = file->private_data;
  493. struct uinput_ff_upload ff_up;
  494. struct uinput_ff_erase ff_erase;
  495. struct uinput_request *req;
  496. int length;
  497. char *phys;
  498. retval = mutex_lock_interruptible(&udev->mutex);
  499. if (retval)
  500. return retval;
  501. if (!udev->dev) {
  502. retval = uinput_allocate_device(udev);
  503. if (retval)
  504. goto out;
  505. }
  506. switch (cmd) {
  507. case UI_DEV_CREATE:
  508. retval = uinput_create_device(udev);
  509. break;
  510. case UI_DEV_DESTROY:
  511. uinput_destroy_device(udev);
  512. break;
  513. case UI_SET_EVBIT:
  514. retval = uinput_set_bit(arg, evbit, EV_MAX);
  515. break;
  516. case UI_SET_KEYBIT:
  517. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  518. break;
  519. case UI_SET_RELBIT:
  520. retval = uinput_set_bit(arg, relbit, REL_MAX);
  521. break;
  522. case UI_SET_ABSBIT:
  523. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  524. break;
  525. case UI_SET_MSCBIT:
  526. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  527. break;
  528. case UI_SET_LEDBIT:
  529. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  530. break;
  531. case UI_SET_SNDBIT:
  532. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  533. break;
  534. case UI_SET_FFBIT:
  535. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  536. break;
  537. case UI_SET_SWBIT:
  538. retval = uinput_set_bit(arg, swbit, SW_MAX);
  539. break;
  540. case UI_SET_PHYS:
  541. if (udev->state == UIST_CREATED) {
  542. retval = -EINVAL;
  543. goto out;
  544. }
  545. length = strnlen_user(p, 1024);
  546. if (length <= 0) {
  547. retval = -EFAULT;
  548. break;
  549. }
  550. kfree(udev->dev->phys);
  551. udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
  552. if (!phys) {
  553. retval = -ENOMEM;
  554. break;
  555. }
  556. if (copy_from_user(phys, p, length)) {
  557. udev->dev->phys = NULL;
  558. kfree(phys);
  559. retval = -EFAULT;
  560. break;
  561. }
  562. phys[length - 1] = '\0';
  563. break;
  564. case UI_BEGIN_FF_UPLOAD:
  565. retval = uinput_ff_upload_from_user(p, &ff_up);
  566. if (retval)
  567. break;
  568. req = uinput_request_find(udev, ff_up.request_id);
  569. if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
  570. retval = -EINVAL;
  571. break;
  572. }
  573. ff_up.retval = 0;
  574. ff_up.effect = *req->u.upload.effect;
  575. if (req->u.upload.old)
  576. ff_up.old = *req->u.upload.old;
  577. else
  578. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  579. retval = uinput_ff_upload_to_user(p, &ff_up);
  580. break;
  581. case UI_BEGIN_FF_ERASE:
  582. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  583. retval = -EFAULT;
  584. break;
  585. }
  586. req = uinput_request_find(udev, ff_erase.request_id);
  587. if (!req || req->code != UI_FF_ERASE) {
  588. retval = -EINVAL;
  589. break;
  590. }
  591. ff_erase.retval = 0;
  592. ff_erase.effect_id = req->u.effect_id;
  593. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  594. retval = -EFAULT;
  595. break;
  596. }
  597. break;
  598. case UI_END_FF_UPLOAD:
  599. retval = uinput_ff_upload_from_user(p, &ff_up);
  600. if (retval)
  601. break;
  602. req = uinput_request_find(udev, ff_up.request_id);
  603. if (!req || req->code != UI_FF_UPLOAD ||
  604. !req->u.upload.effect) {
  605. retval = -EINVAL;
  606. break;
  607. }
  608. req->retval = ff_up.retval;
  609. uinput_request_done(udev, req);
  610. break;
  611. case UI_END_FF_ERASE:
  612. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  613. retval = -EFAULT;
  614. break;
  615. }
  616. req = uinput_request_find(udev, ff_erase.request_id);
  617. if (!req || req->code != UI_FF_ERASE) {
  618. retval = -EINVAL;
  619. break;
  620. }
  621. req->retval = ff_erase.retval;
  622. uinput_request_done(udev, req);
  623. break;
  624. default:
  625. retval = -EINVAL;
  626. }
  627. out:
  628. mutex_unlock(&udev->mutex);
  629. return retval;
  630. }
  631. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  632. {
  633. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  634. }
  635. #ifdef CONFIG_COMPAT
  636. static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  637. {
  638. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  639. }
  640. #endif
  641. static const struct file_operations uinput_fops = {
  642. .owner = THIS_MODULE,
  643. .open = uinput_open,
  644. .release = uinput_release,
  645. .read = uinput_read,
  646. .write = uinput_write,
  647. .poll = uinput_poll,
  648. .unlocked_ioctl = uinput_ioctl,
  649. #ifdef CONFIG_COMPAT
  650. .compat_ioctl = uinput_compat_ioctl,
  651. #endif
  652. };
  653. static struct miscdevice uinput_misc = {
  654. .fops = &uinput_fops,
  655. .minor = UINPUT_MINOR,
  656. .name = UINPUT_NAME,
  657. };
  658. static int __init uinput_init(void)
  659. {
  660. return misc_register(&uinput_misc);
  661. }
  662. static void __exit uinput_exit(void)
  663. {
  664. misc_deregister(&uinput_misc);
  665. }
  666. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  667. MODULE_DESCRIPTION("User level driver support for input subsystem");
  668. MODULE_LICENSE("GPL");
  669. MODULE_VERSION("0.3");
  670. module_init(uinput_init);
  671. module_exit(uinput_exit);