uinput.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. if (test_bit(ABS_MT_SLOT, dev->absbit)) {
  337. int nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
  338. input_mt_create_slots(dev, nslot);
  339. input_set_events_per_packet(dev, 6 * nslot);
  340. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  341. input_set_events_per_packet(dev, 60);
  342. }
  343. }
  344. udev->state = UIST_SETUP_COMPLETE;
  345. retval = count;
  346. exit:
  347. kfree(user_dev);
  348. return retval;
  349. }
  350. static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
  351. {
  352. struct input_event ev;
  353. if (count < input_event_size())
  354. return -EINVAL;
  355. if (input_event_from_user(buffer, &ev))
  356. return -EFAULT;
  357. input_event(udev->dev, ev.type, ev.code, ev.value);
  358. return input_event_size();
  359. }
  360. static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  361. {
  362. struct uinput_device *udev = file->private_data;
  363. int retval;
  364. retval = mutex_lock_interruptible(&udev->mutex);
  365. if (retval)
  366. return retval;
  367. retval = udev->state == UIST_CREATED ?
  368. uinput_inject_event(udev, buffer, count) :
  369. uinput_setup_device(udev, buffer, count);
  370. mutex_unlock(&udev->mutex);
  371. return retval;
  372. }
  373. static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  374. {
  375. struct uinput_device *udev = file->private_data;
  376. int retval = 0;
  377. if (udev->state != UIST_CREATED)
  378. return -ENODEV;
  379. if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
  380. return -EAGAIN;
  381. retval = wait_event_interruptible(udev->waitq,
  382. udev->head != udev->tail || udev->state != UIST_CREATED);
  383. if (retval)
  384. return retval;
  385. retval = mutex_lock_interruptible(&udev->mutex);
  386. if (retval)
  387. return retval;
  388. if (udev->state != UIST_CREATED) {
  389. retval = -ENODEV;
  390. goto out;
  391. }
  392. while (udev->head != udev->tail && retval + input_event_size() <= count) {
  393. if (input_event_to_user(buffer + retval, &udev->buff[udev->tail])) {
  394. retval = -EFAULT;
  395. goto out;
  396. }
  397. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  398. retval += input_event_size();
  399. }
  400. out:
  401. mutex_unlock(&udev->mutex);
  402. return retval;
  403. }
  404. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  405. {
  406. struct uinput_device *udev = file->private_data;
  407. poll_wait(file, &udev->waitq, wait);
  408. if (udev->head != udev->tail)
  409. return POLLIN | POLLRDNORM;
  410. return 0;
  411. }
  412. static int uinput_release(struct inode *inode, struct file *file)
  413. {
  414. struct uinput_device *udev = file->private_data;
  415. uinput_destroy_device(udev);
  416. kfree(udev);
  417. return 0;
  418. }
  419. #ifdef CONFIG_COMPAT
  420. struct uinput_ff_upload_compat {
  421. int request_id;
  422. int retval;
  423. struct ff_effect_compat effect;
  424. struct ff_effect_compat old;
  425. };
  426. static int uinput_ff_upload_to_user(char __user *buffer,
  427. const struct uinput_ff_upload *ff_up)
  428. {
  429. if (INPUT_COMPAT_TEST) {
  430. struct uinput_ff_upload_compat ff_up_compat;
  431. ff_up_compat.request_id = ff_up->request_id;
  432. ff_up_compat.retval = ff_up->retval;
  433. /*
  434. * It so happens that the pointer that gives us the trouble
  435. * is the last field in the structure. Since we don't support
  436. * custom waveforms in uinput anyway we can just copy the whole
  437. * thing (to the compat size) and ignore the pointer.
  438. */
  439. memcpy(&ff_up_compat.effect, &ff_up->effect,
  440. sizeof(struct ff_effect_compat));
  441. memcpy(&ff_up_compat.old, &ff_up->old,
  442. sizeof(struct ff_effect_compat));
  443. if (copy_to_user(buffer, &ff_up_compat,
  444. sizeof(struct uinput_ff_upload_compat)))
  445. return -EFAULT;
  446. } else {
  447. if (copy_to_user(buffer, ff_up,
  448. sizeof(struct uinput_ff_upload)))
  449. return -EFAULT;
  450. }
  451. return 0;
  452. }
  453. static int uinput_ff_upload_from_user(const char __user *buffer,
  454. struct uinput_ff_upload *ff_up)
  455. {
  456. if (INPUT_COMPAT_TEST) {
  457. struct uinput_ff_upload_compat ff_up_compat;
  458. if (copy_from_user(&ff_up_compat, buffer,
  459. sizeof(struct uinput_ff_upload_compat)))
  460. return -EFAULT;
  461. ff_up->request_id = ff_up_compat.request_id;
  462. ff_up->retval = ff_up_compat.retval;
  463. memcpy(&ff_up->effect, &ff_up_compat.effect,
  464. sizeof(struct ff_effect_compat));
  465. memcpy(&ff_up->old, &ff_up_compat.old,
  466. sizeof(struct ff_effect_compat));
  467. } else {
  468. if (copy_from_user(ff_up, buffer,
  469. sizeof(struct uinput_ff_upload)))
  470. return -EFAULT;
  471. }
  472. return 0;
  473. }
  474. #else
  475. static int uinput_ff_upload_to_user(char __user *buffer,
  476. const struct uinput_ff_upload *ff_up)
  477. {
  478. if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. static int uinput_ff_upload_from_user(const char __user *buffer,
  483. struct uinput_ff_upload *ff_up)
  484. {
  485. if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
  486. return -EFAULT;
  487. return 0;
  488. }
  489. #endif
  490. #define uinput_set_bit(_arg, _bit, _max) \
  491. ({ \
  492. int __ret = 0; \
  493. if (udev->state == UIST_CREATED) \
  494. __ret = -EINVAL; \
  495. else if ((_arg) > (_max)) \
  496. __ret = -EINVAL; \
  497. else set_bit((_arg), udev->dev->_bit); \
  498. __ret; \
  499. })
  500. static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
  501. unsigned long arg, void __user *p)
  502. {
  503. int retval;
  504. struct uinput_device *udev = file->private_data;
  505. struct uinput_ff_upload ff_up;
  506. struct uinput_ff_erase ff_erase;
  507. struct uinput_request *req;
  508. int length;
  509. char *phys;
  510. retval = mutex_lock_interruptible(&udev->mutex);
  511. if (retval)
  512. return retval;
  513. if (!udev->dev) {
  514. retval = uinput_allocate_device(udev);
  515. if (retval)
  516. goto out;
  517. }
  518. switch (cmd) {
  519. case UI_DEV_CREATE:
  520. retval = uinput_create_device(udev);
  521. break;
  522. case UI_DEV_DESTROY:
  523. uinput_destroy_device(udev);
  524. break;
  525. case UI_SET_EVBIT:
  526. retval = uinput_set_bit(arg, evbit, EV_MAX);
  527. break;
  528. case UI_SET_KEYBIT:
  529. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  530. break;
  531. case UI_SET_RELBIT:
  532. retval = uinput_set_bit(arg, relbit, REL_MAX);
  533. break;
  534. case UI_SET_ABSBIT:
  535. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  536. break;
  537. case UI_SET_MSCBIT:
  538. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  539. break;
  540. case UI_SET_LEDBIT:
  541. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  542. break;
  543. case UI_SET_SNDBIT:
  544. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  545. break;
  546. case UI_SET_FFBIT:
  547. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  548. break;
  549. case UI_SET_SWBIT:
  550. retval = uinput_set_bit(arg, swbit, SW_MAX);
  551. break;
  552. case UI_SET_PHYS:
  553. if (udev->state == UIST_CREATED) {
  554. retval = -EINVAL;
  555. goto out;
  556. }
  557. length = strnlen_user(p, 1024);
  558. if (length <= 0) {
  559. retval = -EFAULT;
  560. break;
  561. }
  562. kfree(udev->dev->phys);
  563. udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
  564. if (!phys) {
  565. retval = -ENOMEM;
  566. break;
  567. }
  568. if (copy_from_user(phys, p, length)) {
  569. udev->dev->phys = NULL;
  570. kfree(phys);
  571. retval = -EFAULT;
  572. break;
  573. }
  574. phys[length - 1] = '\0';
  575. break;
  576. case UI_BEGIN_FF_UPLOAD:
  577. retval = uinput_ff_upload_from_user(p, &ff_up);
  578. if (retval)
  579. break;
  580. req = uinput_request_find(udev, ff_up.request_id);
  581. if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
  582. retval = -EINVAL;
  583. break;
  584. }
  585. ff_up.retval = 0;
  586. ff_up.effect = *req->u.upload.effect;
  587. if (req->u.upload.old)
  588. ff_up.old = *req->u.upload.old;
  589. else
  590. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  591. retval = uinput_ff_upload_to_user(p, &ff_up);
  592. break;
  593. case UI_BEGIN_FF_ERASE:
  594. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  595. retval = -EFAULT;
  596. break;
  597. }
  598. req = uinput_request_find(udev, ff_erase.request_id);
  599. if (!req || req->code != UI_FF_ERASE) {
  600. retval = -EINVAL;
  601. break;
  602. }
  603. ff_erase.retval = 0;
  604. ff_erase.effect_id = req->u.effect_id;
  605. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  606. retval = -EFAULT;
  607. break;
  608. }
  609. break;
  610. case UI_END_FF_UPLOAD:
  611. retval = uinput_ff_upload_from_user(p, &ff_up);
  612. if (retval)
  613. break;
  614. req = uinput_request_find(udev, ff_up.request_id);
  615. if (!req || req->code != UI_FF_UPLOAD ||
  616. !req->u.upload.effect) {
  617. retval = -EINVAL;
  618. break;
  619. }
  620. req->retval = ff_up.retval;
  621. uinput_request_done(udev, req);
  622. break;
  623. case UI_END_FF_ERASE:
  624. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  625. retval = -EFAULT;
  626. break;
  627. }
  628. req = uinput_request_find(udev, ff_erase.request_id);
  629. if (!req || req->code != UI_FF_ERASE) {
  630. retval = -EINVAL;
  631. break;
  632. }
  633. req->retval = ff_erase.retval;
  634. uinput_request_done(udev, req);
  635. break;
  636. default:
  637. retval = -EINVAL;
  638. }
  639. out:
  640. mutex_unlock(&udev->mutex);
  641. return retval;
  642. }
  643. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  644. {
  645. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  646. }
  647. #ifdef CONFIG_COMPAT
  648. static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  649. {
  650. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  651. }
  652. #endif
  653. static const struct file_operations uinput_fops = {
  654. .owner = THIS_MODULE,
  655. .open = uinput_open,
  656. .release = uinput_release,
  657. .read = uinput_read,
  658. .write = uinput_write,
  659. .poll = uinput_poll,
  660. .unlocked_ioctl = uinput_ioctl,
  661. #ifdef CONFIG_COMPAT
  662. .compat_ioctl = uinput_compat_ioctl,
  663. #endif
  664. };
  665. static struct miscdevice uinput_misc = {
  666. .fops = &uinput_fops,
  667. .minor = UINPUT_MINOR,
  668. .name = UINPUT_NAME,
  669. };
  670. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  671. MODULE_ALIAS("devname:" UINPUT_NAME);
  672. static int __init uinput_init(void)
  673. {
  674. return misc_register(&uinput_misc);
  675. }
  676. static void __exit uinput_exit(void)
  677. {
  678. misc_deregister(&uinput_misc);
  679. }
  680. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  681. MODULE_DESCRIPTION("User level driver support for input subsystem");
  682. MODULE_LICENSE("GPL");
  683. MODULE_VERSION("0.3");
  684. module_init(uinput_init);
  685. module_exit(uinput_exit);