uinput.c 20 KB

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