uinput.c 14 KB

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