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