uinput.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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_init_slots(dev, 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_PROPBIT:
  553. retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
  554. break;
  555. case UI_SET_PHYS:
  556. if (udev->state == UIST_CREATED) {
  557. retval = -EINVAL;
  558. goto out;
  559. }
  560. length = strnlen_user(p, 1024);
  561. if (length <= 0) {
  562. retval = -EFAULT;
  563. break;
  564. }
  565. kfree(udev->dev->phys);
  566. udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
  567. if (!phys) {
  568. retval = -ENOMEM;
  569. break;
  570. }
  571. if (copy_from_user(phys, p, length)) {
  572. udev->dev->phys = NULL;
  573. kfree(phys);
  574. retval = -EFAULT;
  575. break;
  576. }
  577. phys[length - 1] = '\0';
  578. break;
  579. case UI_BEGIN_FF_UPLOAD:
  580. retval = uinput_ff_upload_from_user(p, &ff_up);
  581. if (retval)
  582. break;
  583. req = uinput_request_find(udev, ff_up.request_id);
  584. if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
  585. retval = -EINVAL;
  586. break;
  587. }
  588. ff_up.retval = 0;
  589. ff_up.effect = *req->u.upload.effect;
  590. if (req->u.upload.old)
  591. ff_up.old = *req->u.upload.old;
  592. else
  593. memset(&ff_up.old, 0, sizeof(struct ff_effect));
  594. retval = uinput_ff_upload_to_user(p, &ff_up);
  595. break;
  596. case UI_BEGIN_FF_ERASE:
  597. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  598. retval = -EFAULT;
  599. break;
  600. }
  601. req = uinput_request_find(udev, ff_erase.request_id);
  602. if (!req || req->code != UI_FF_ERASE) {
  603. retval = -EINVAL;
  604. break;
  605. }
  606. ff_erase.retval = 0;
  607. ff_erase.effect_id = req->u.effect_id;
  608. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  609. retval = -EFAULT;
  610. break;
  611. }
  612. break;
  613. case UI_END_FF_UPLOAD:
  614. retval = uinput_ff_upload_from_user(p, &ff_up);
  615. if (retval)
  616. break;
  617. req = uinput_request_find(udev, ff_up.request_id);
  618. if (!req || req->code != UI_FF_UPLOAD ||
  619. !req->u.upload.effect) {
  620. retval = -EINVAL;
  621. break;
  622. }
  623. req->retval = ff_up.retval;
  624. uinput_request_done(udev, req);
  625. break;
  626. case UI_END_FF_ERASE:
  627. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  628. retval = -EFAULT;
  629. break;
  630. }
  631. req = uinput_request_find(udev, ff_erase.request_id);
  632. if (!req || req->code != UI_FF_ERASE) {
  633. retval = -EINVAL;
  634. break;
  635. }
  636. req->retval = ff_erase.retval;
  637. uinput_request_done(udev, req);
  638. break;
  639. default:
  640. retval = -EINVAL;
  641. }
  642. out:
  643. mutex_unlock(&udev->mutex);
  644. return retval;
  645. }
  646. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  647. {
  648. return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
  649. }
  650. #ifdef CONFIG_COMPAT
  651. static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  652. {
  653. return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
  654. }
  655. #endif
  656. static const struct file_operations uinput_fops = {
  657. .owner = THIS_MODULE,
  658. .open = uinput_open,
  659. .release = uinput_release,
  660. .read = uinput_read,
  661. .write = uinput_write,
  662. .poll = uinput_poll,
  663. .unlocked_ioctl = uinput_ioctl,
  664. #ifdef CONFIG_COMPAT
  665. .compat_ioctl = uinput_compat_ioctl,
  666. #endif
  667. .llseek = no_llseek,
  668. };
  669. static struct miscdevice uinput_misc = {
  670. .fops = &uinput_fops,
  671. .minor = UINPUT_MINOR,
  672. .name = UINPUT_NAME,
  673. };
  674. MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
  675. MODULE_ALIAS("devname:" UINPUT_NAME);
  676. static int __init uinput_init(void)
  677. {
  678. return misc_register(&uinput_misc);
  679. }
  680. static void __exit uinput_exit(void)
  681. {
  682. misc_deregister(&uinput_misc);
  683. }
  684. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  685. MODULE_DESCRIPTION("User level driver support for input subsystem");
  686. MODULE_LICENSE("GPL");
  687. MODULE_VERSION("0.3");
  688. module_init(uinput_init);
  689. module_exit(uinput_exit);