uinput.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
  24. * - added force feedback support
  25. * - added UI_SET_PHYS
  26. * 0.1 20/06/2002
  27. * - first public version
  28. */
  29. #include <linux/poll.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/input.h>
  34. #include <linux/smp_lock.h>
  35. #include <linux/fs.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/uinput.h>
  38. static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  39. {
  40. struct uinput_device *udev;
  41. udev = dev->private;
  42. udev->buff[udev->head].type = type;
  43. udev->buff[udev->head].code = code;
  44. udev->buff[udev->head].value = value;
  45. do_gettimeofday(&udev->buff[udev->head].time);
  46. udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
  47. wake_up_interruptible(&udev->waitq);
  48. return 0;
  49. }
  50. static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
  51. {
  52. /* Atomically allocate an ID for the given request. Returns 0 on success. */
  53. int id;
  54. int err = -1;
  55. spin_lock(&udev->requests_lock);
  56. for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
  57. if (!udev->requests[id]) {
  58. request->id = id;
  59. udev->requests[id] = request;
  60. err = 0;
  61. break;
  62. }
  63. spin_unlock(&udev->requests_lock);
  64. return err;
  65. }
  66. static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
  67. {
  68. /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
  69. if (id >= UINPUT_NUM_REQUESTS || id < 0)
  70. return NULL;
  71. return udev->requests[id];
  72. }
  73. static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
  74. {
  75. /* Allocate slot. If none are available right away, wait. */
  76. return wait_event_interruptible(udev->requests_waitq,
  77. !uinput_request_alloc_id(udev, request));
  78. }
  79. static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
  80. {
  81. /* Mark slot as available */
  82. udev->requests[request->id] = NULL;
  83. wake_up(&udev->requests_waitq);
  84. complete(&request->done);
  85. }
  86. static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
  87. {
  88. /* Tell our userspace app about this new request by queueing an input event */
  89. uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
  90. /* Wait for the request to complete */
  91. wait_for_completion(&request->done);
  92. return request->retval;
  93. }
  94. static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
  95. {
  96. struct uinput_request request;
  97. int retval;
  98. if (!test_bit(EV_FF, dev->evbit))
  99. return -ENOSYS;
  100. request.id = -1;
  101. init_completion(&request.done);
  102. request.code = UI_FF_UPLOAD;
  103. request.u.effect = effect;
  104. retval = uinput_request_reserve_slot(dev->private, &request);
  105. if (!retval)
  106. retval = uinput_request_submit(dev, &request);
  107. return retval;
  108. }
  109. static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
  110. {
  111. struct uinput_request request;
  112. int retval;
  113. if (!test_bit(EV_FF, dev->evbit))
  114. return -ENOSYS;
  115. request.id = -1;
  116. init_completion(&request.done);
  117. request.code = UI_FF_ERASE;
  118. request.u.effect_id = effect_id;
  119. retval = uinput_request_reserve_slot(dev->private, &request);
  120. if (!retval)
  121. retval = uinput_request_submit(dev, &request);
  122. return retval;
  123. }
  124. static void uinput_destroy_device(struct uinput_device *udev)
  125. {
  126. const char *name, *phys;
  127. if (udev->dev) {
  128. name = udev->dev->name;
  129. phys = udev->dev->phys;
  130. if (udev->state == UIST_CREATED)
  131. input_unregister_device(udev->dev);
  132. else
  133. input_free_device(udev->dev);
  134. kfree(name);
  135. kfree(phys);
  136. udev->dev = NULL;
  137. }
  138. udev->state = UIST_NEW_DEVICE;
  139. }
  140. static int uinput_create_device(struct uinput_device *udev)
  141. {
  142. int error;
  143. if (udev->state != UIST_SETUP_COMPLETE) {
  144. printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
  145. return -EINVAL;
  146. }
  147. error = input_register_device(udev->dev);
  148. if (error) {
  149. uinput_destroy_device(udev);
  150. return error;
  151. }
  152. udev->state = UIST_CREATED;
  153. return 0;
  154. }
  155. static int uinput_open(struct inode *inode, struct file *file)
  156. {
  157. struct uinput_device *newdev;
  158. newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
  159. if (!newdev)
  160. return -ENOMEM;
  161. init_MUTEX(&newdev->sem);
  162. spin_lock_init(&newdev->requests_lock);
  163. init_waitqueue_head(&newdev->requests_waitq);
  164. init_waitqueue_head(&newdev->waitq);
  165. newdev->state = UIST_NEW_DEVICE;
  166. file->private_data = newdev;
  167. return 0;
  168. }
  169. static int uinput_validate_absbits(struct input_dev *dev)
  170. {
  171. unsigned int cnt;
  172. int retval = 0;
  173. for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
  174. if (!test_bit(cnt, dev->absbit))
  175. continue;
  176. if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
  177. printk(KERN_DEBUG
  178. "%s: invalid abs[%02x] min:%d max:%d\n",
  179. UINPUT_NAME, cnt,
  180. dev->absmin[cnt], dev->absmax[cnt]);
  181. retval = -EINVAL;
  182. break;
  183. }
  184. if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
  185. printk(KERN_DEBUG
  186. "%s: absflat[%02x] out of range: %d "
  187. "(min:%d/max:%d)\n",
  188. UINPUT_NAME, cnt, dev->absflat[cnt],
  189. dev->absmin[cnt], dev->absmax[cnt]);
  190. retval = -EINVAL;
  191. break;
  192. }
  193. }
  194. return retval;
  195. }
  196. static int uinput_allocate_device(struct uinput_device *udev)
  197. {
  198. udev->dev = input_allocate_device();
  199. if (!udev->dev)
  200. return -ENOMEM;
  201. udev->dev->event = uinput_dev_event;
  202. udev->dev->upload_effect = uinput_dev_upload_effect;
  203. udev->dev->erase_effect = uinput_dev_erase_effect;
  204. udev->dev->private = udev;
  205. return 0;
  206. }
  207. static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
  208. {
  209. struct uinput_user_dev *user_dev;
  210. struct input_dev *dev;
  211. char *name;
  212. int size;
  213. int retval;
  214. if (count != sizeof(struct uinput_user_dev))
  215. return -EINVAL;
  216. if (!udev->dev) {
  217. retval = uinput_allocate_device(udev);
  218. if (retval)
  219. return retval;
  220. }
  221. dev = udev->dev;
  222. user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
  223. if (!user_dev)
  224. return -ENOMEM;
  225. if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
  226. retval = -EFAULT;
  227. goto exit;
  228. }
  229. size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
  230. if (!size) {
  231. retval = -EINVAL;
  232. goto exit;
  233. }
  234. kfree(dev->name);
  235. dev->name = name = kmalloc(size, GFP_KERNEL);
  236. if (!name) {
  237. retval = -ENOMEM;
  238. goto exit;
  239. }
  240. strlcpy(name, user_dev->name, size);
  241. dev->id.bustype = user_dev->id.bustype;
  242. dev->id.vendor = user_dev->id.vendor;
  243. dev->id.product = user_dev->id.product;
  244. dev->id.version = user_dev->id.version;
  245. dev->ff_effects_max = user_dev->ff_effects_max;
  246. size = sizeof(int) * (ABS_MAX + 1);
  247. memcpy(dev->absmax, user_dev->absmax, size);
  248. memcpy(dev->absmin, user_dev->absmin, size);
  249. memcpy(dev->absfuzz, user_dev->absfuzz, size);
  250. memcpy(dev->absflat, user_dev->absflat, size);
  251. /* check if absmin/absmax/absfuzz/absflat are filled as
  252. * told in Documentation/input/input-programming.txt */
  253. if (test_bit(EV_ABS, dev->evbit)) {
  254. retval = uinput_validate_absbits(dev);
  255. if (retval < 0)
  256. goto exit;
  257. }
  258. udev->state = UIST_SETUP_COMPLETE;
  259. retval = count;
  260. exit:
  261. kfree(user_dev);
  262. return retval;
  263. }
  264. static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
  265. {
  266. struct input_event ev;
  267. if (count != sizeof(struct input_event))
  268. return -EINVAL;
  269. if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
  270. return -EFAULT;
  271. input_event(udev->dev, ev.type, ev.code, ev.value);
  272. return sizeof(struct input_event);
  273. }
  274. static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  275. {
  276. struct uinput_device *udev = file->private_data;
  277. int retval;
  278. retval = down_interruptible(&udev->sem);
  279. if (retval)
  280. return retval;
  281. retval = udev->state == UIST_CREATED ?
  282. uinput_inject_event(udev, buffer, count) :
  283. uinput_setup_device(udev, buffer, count);
  284. up(&udev->sem);
  285. return retval;
  286. }
  287. static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  288. {
  289. struct uinput_device *udev = file->private_data;
  290. int retval = 0;
  291. if (udev->state != UIST_CREATED)
  292. return -ENODEV;
  293. if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
  294. return -EAGAIN;
  295. retval = wait_event_interruptible(udev->waitq,
  296. udev->head != udev->tail || udev->state != UIST_CREATED);
  297. if (retval)
  298. return retval;
  299. retval = down_interruptible(&udev->sem);
  300. if (retval)
  301. return retval;
  302. if (udev->state != UIST_CREATED) {
  303. retval = -ENODEV;
  304. goto out;
  305. }
  306. while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
  307. if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
  308. retval = -EFAULT;
  309. goto out;
  310. }
  311. udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
  312. retval += sizeof(struct input_event);
  313. }
  314. out:
  315. up(&udev->sem);
  316. return retval;
  317. }
  318. static unsigned int uinput_poll(struct file *file, poll_table *wait)
  319. {
  320. struct uinput_device *udev = file->private_data;
  321. poll_wait(file, &udev->waitq, wait);
  322. if (udev->head != udev->tail)
  323. return POLLIN | POLLRDNORM;
  324. return 0;
  325. }
  326. static int uinput_release(struct inode *inode, struct file *file)
  327. {
  328. struct uinput_device *udev = file->private_data;
  329. uinput_destroy_device(udev);
  330. kfree(udev);
  331. return 0;
  332. }
  333. #define uinput_set_bit(_arg, _bit, _max) \
  334. ({ \
  335. int __ret = 0; \
  336. if (udev->state == UIST_CREATED) \
  337. __ret = -EINVAL; \
  338. else if ((_arg) > (_max)) \
  339. __ret = -EINVAL; \
  340. else set_bit((_arg), udev->dev->_bit); \
  341. __ret; \
  342. })
  343. static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  344. {
  345. int retval;
  346. struct uinput_device *udev;
  347. void __user *p = (void __user *)arg;
  348. struct uinput_ff_upload ff_up;
  349. struct uinput_ff_erase ff_erase;
  350. struct uinput_request *req;
  351. int length;
  352. char *phys;
  353. udev = file->private_data;
  354. retval = down_interruptible(&udev->sem);
  355. if (retval)
  356. return retval;
  357. if (!udev->dev) {
  358. retval = uinput_allocate_device(udev);
  359. if (retval)
  360. goto out;
  361. }
  362. switch (cmd) {
  363. case UI_DEV_CREATE:
  364. retval = uinput_create_device(udev);
  365. break;
  366. case UI_DEV_DESTROY:
  367. uinput_destroy_device(udev);
  368. break;
  369. case UI_SET_EVBIT:
  370. retval = uinput_set_bit(arg, evbit, EV_MAX);
  371. break;
  372. case UI_SET_KEYBIT:
  373. retval = uinput_set_bit(arg, keybit, KEY_MAX);
  374. break;
  375. case UI_SET_RELBIT:
  376. retval = uinput_set_bit(arg, relbit, REL_MAX);
  377. break;
  378. case UI_SET_ABSBIT:
  379. retval = uinput_set_bit(arg, absbit, ABS_MAX);
  380. break;
  381. case UI_SET_MSCBIT:
  382. retval = uinput_set_bit(arg, mscbit, MSC_MAX);
  383. break;
  384. case UI_SET_LEDBIT:
  385. retval = uinput_set_bit(arg, ledbit, LED_MAX);
  386. break;
  387. case UI_SET_SNDBIT:
  388. retval = uinput_set_bit(arg, sndbit, SND_MAX);
  389. break;
  390. case UI_SET_FFBIT:
  391. retval = uinput_set_bit(arg, ffbit, FF_MAX);
  392. break;
  393. case UI_SET_SWBIT:
  394. retval = uinput_set_bit(arg, swbit, SW_MAX);
  395. break;
  396. case UI_SET_PHYS:
  397. if (udev->state == UIST_CREATED) {
  398. retval = -EINVAL;
  399. goto out;
  400. }
  401. length = strnlen_user(p, 1024);
  402. if (length <= 0) {
  403. retval = -EFAULT;
  404. break;
  405. }
  406. kfree(udev->dev->phys);
  407. udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
  408. if (!phys) {
  409. retval = -ENOMEM;
  410. break;
  411. }
  412. if (copy_from_user(phys, p, length)) {
  413. udev->dev->phys = NULL;
  414. kfree(phys);
  415. retval = -EFAULT;
  416. break;
  417. }
  418. phys[length - 1] = '\0';
  419. break;
  420. case UI_BEGIN_FF_UPLOAD:
  421. if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
  422. retval = -EFAULT;
  423. break;
  424. }
  425. req = uinput_request_find(udev, ff_up.request_id);
  426. if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
  427. retval = -EINVAL;
  428. break;
  429. }
  430. ff_up.retval = 0;
  431. memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
  432. if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
  433. retval = -EFAULT;
  434. break;
  435. }
  436. break;
  437. case UI_BEGIN_FF_ERASE:
  438. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  439. retval = -EFAULT;
  440. break;
  441. }
  442. req = uinput_request_find(udev, ff_erase.request_id);
  443. if (!(req && req->code == UI_FF_ERASE)) {
  444. retval = -EINVAL;
  445. break;
  446. }
  447. ff_erase.retval = 0;
  448. ff_erase.effect_id = req->u.effect_id;
  449. if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
  450. retval = -EFAULT;
  451. break;
  452. }
  453. break;
  454. case UI_END_FF_UPLOAD:
  455. if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
  456. retval = -EFAULT;
  457. break;
  458. }
  459. req = uinput_request_find(udev, ff_up.request_id);
  460. if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
  461. retval = -EINVAL;
  462. break;
  463. }
  464. req->retval = ff_up.retval;
  465. memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
  466. uinput_request_done(udev, req);
  467. break;
  468. case UI_END_FF_ERASE:
  469. if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
  470. retval = -EFAULT;
  471. break;
  472. }
  473. req = uinput_request_find(udev, ff_erase.request_id);
  474. if (!(req && req->code == UI_FF_ERASE)) {
  475. retval = -EINVAL;
  476. break;
  477. }
  478. req->retval = ff_erase.retval;
  479. uinput_request_done(udev, req);
  480. break;
  481. default:
  482. retval = -EINVAL;
  483. }
  484. out:
  485. up(&udev->sem);
  486. return retval;
  487. }
  488. static struct file_operations uinput_fops = {
  489. .owner = THIS_MODULE,
  490. .open = uinput_open,
  491. .release = uinput_release,
  492. .read = uinput_read,
  493. .write = uinput_write,
  494. .poll = uinput_poll,
  495. .unlocked_ioctl = uinput_ioctl,
  496. };
  497. static struct miscdevice uinput_misc = {
  498. .fops = &uinput_fops,
  499. .minor = UINPUT_MINOR,
  500. .name = UINPUT_NAME,
  501. };
  502. static int __init uinput_init(void)
  503. {
  504. return misc_register(&uinput_misc);
  505. }
  506. static void __exit uinput_exit(void)
  507. {
  508. misc_deregister(&uinput_misc);
  509. }
  510. MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
  511. MODULE_DESCRIPTION("User level driver support for input subsystem");
  512. MODULE_LICENSE("GPL");
  513. module_init(uinput_init);
  514. module_exit(uinput_exit);