evdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Event char devices, giving access to raw input device events.
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #define EVDEV_MINOR_BASE 64
  11. #define EVDEV_MINORS 32
  12. #define EVDEV_BUFFER_SIZE 64
  13. #include <linux/poll.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/input.h>
  18. #include <linux/major.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/device.h>
  21. #include <linux/devfs_fs_kernel.h>
  22. struct evdev {
  23. int exist;
  24. int open;
  25. int minor;
  26. char name[16];
  27. struct input_handle handle;
  28. wait_queue_head_t wait;
  29. struct evdev_list *grab;
  30. struct list_head list;
  31. };
  32. struct evdev_list {
  33. struct input_event buffer[EVDEV_BUFFER_SIZE];
  34. int head;
  35. int tail;
  36. struct fasync_struct *fasync;
  37. struct evdev *evdev;
  38. struct list_head node;
  39. };
  40. static struct evdev *evdev_table[EVDEV_MINORS];
  41. static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  42. {
  43. struct evdev *evdev = handle->private;
  44. struct evdev_list *list;
  45. if (evdev->grab) {
  46. list = evdev->grab;
  47. do_gettimeofday(&list->buffer[list->head].time);
  48. list->buffer[list->head].type = type;
  49. list->buffer[list->head].code = code;
  50. list->buffer[list->head].value = value;
  51. list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
  52. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  53. } else
  54. list_for_each_entry(list, &evdev->list, node) {
  55. do_gettimeofday(&list->buffer[list->head].time);
  56. list->buffer[list->head].type = type;
  57. list->buffer[list->head].code = code;
  58. list->buffer[list->head].value = value;
  59. list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
  60. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  61. }
  62. wake_up_interruptible(&evdev->wait);
  63. }
  64. static int evdev_fasync(int fd, struct file *file, int on)
  65. {
  66. int retval;
  67. struct evdev_list *list = file->private_data;
  68. retval = fasync_helper(fd, file, on, &list->fasync);
  69. return retval < 0 ? retval : 0;
  70. }
  71. static int evdev_flush(struct file * file)
  72. {
  73. struct evdev_list *list = file->private_data;
  74. if (!list->evdev->exist) return -ENODEV;
  75. return input_flush_device(&list->evdev->handle, file);
  76. }
  77. static void evdev_free(struct evdev *evdev)
  78. {
  79. evdev_table[evdev->minor] = NULL;
  80. kfree(evdev);
  81. }
  82. static int evdev_release(struct inode * inode, struct file * file)
  83. {
  84. struct evdev_list *list = file->private_data;
  85. if (list->evdev->grab == list) {
  86. input_release_device(&list->evdev->handle);
  87. list->evdev->grab = NULL;
  88. }
  89. evdev_fasync(-1, file, 0);
  90. list_del(&list->node);
  91. if (!--list->evdev->open) {
  92. if (list->evdev->exist)
  93. input_close_device(&list->evdev->handle);
  94. else
  95. evdev_free(list->evdev);
  96. }
  97. kfree(list);
  98. return 0;
  99. }
  100. static int evdev_open(struct inode * inode, struct file * file)
  101. {
  102. struct evdev_list *list;
  103. int i = iminor(inode) - EVDEV_MINOR_BASE;
  104. int accept_err;
  105. if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
  106. return -ENODEV;
  107. if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
  108. return accept_err;
  109. if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
  110. return -ENOMEM;
  111. memset(list, 0, sizeof(struct evdev_list));
  112. list->evdev = evdev_table[i];
  113. list_add_tail(&list->node, &evdev_table[i]->list);
  114. file->private_data = list;
  115. if (!list->evdev->open++)
  116. if (list->evdev->exist)
  117. input_open_device(&list->evdev->handle);
  118. return 0;
  119. }
  120. static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  121. {
  122. struct evdev_list *list = file->private_data;
  123. struct input_event event;
  124. int retval = 0;
  125. if (!list->evdev->exist) return -ENODEV;
  126. while (retval < count) {
  127. if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
  128. return -EFAULT;
  129. input_event(list->evdev->handle.dev, event.type, event.code, event.value);
  130. retval += sizeof(struct input_event);
  131. }
  132. return retval;
  133. }
  134. static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  135. {
  136. struct evdev_list *list = file->private_data;
  137. int retval;
  138. if (count < sizeof(struct input_event))
  139. return -EINVAL;
  140. if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
  141. return -EAGAIN;
  142. retval = wait_event_interruptible(list->evdev->wait,
  143. list->head != list->tail || (!list->evdev->exist));
  144. if (retval)
  145. return retval;
  146. if (!list->evdev->exist)
  147. return -ENODEV;
  148. while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
  149. if (copy_to_user(buffer + retval, list->buffer + list->tail,
  150. sizeof(struct input_event))) return -EFAULT;
  151. list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
  152. retval += sizeof(struct input_event);
  153. }
  154. return retval;
  155. }
  156. /* No kernel lock - fine */
  157. static unsigned int evdev_poll(struct file *file, poll_table *wait)
  158. {
  159. struct evdev_list *list = file->private_data;
  160. poll_wait(file, &list->evdev->wait, wait);
  161. return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
  162. (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
  163. }
  164. static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  165. {
  166. struct evdev_list *list = file->private_data;
  167. struct evdev *evdev = list->evdev;
  168. struct input_dev *dev = evdev->handle.dev;
  169. struct input_absinfo abs;
  170. void __user *p = (void __user *)arg;
  171. int __user *ip = (int __user *)arg;
  172. int i, t, u, v;
  173. if (!evdev->exist) return -ENODEV;
  174. switch (cmd) {
  175. case EVIOCGVERSION:
  176. return put_user(EV_VERSION, ip);
  177. case EVIOCGID:
  178. return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
  179. case EVIOCGKEYCODE:
  180. if (get_user(t, ip)) return -EFAULT;
  181. if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
  182. if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
  183. return 0;
  184. case EVIOCSKEYCODE:
  185. if (get_user(t, ip)) return -EFAULT;
  186. if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
  187. if (get_user(v, ip + 1)) return -EFAULT;
  188. if (v < 0 || v > KEY_MAX) return -EINVAL;
  189. u = SET_INPUT_KEYCODE(dev, t, v);
  190. clear_bit(u, dev->keybit);
  191. set_bit(v, dev->keybit);
  192. for (i = 0; i < dev->keycodemax; i++)
  193. if (INPUT_KEYCODE(dev,i) == u)
  194. set_bit(u, dev->keybit);
  195. return 0;
  196. case EVIOCSFF:
  197. if (dev->upload_effect) {
  198. struct ff_effect effect;
  199. int err;
  200. if (copy_from_user(&effect, p, sizeof(effect)))
  201. return -EFAULT;
  202. err = dev->upload_effect(dev, &effect);
  203. if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
  204. return -EFAULT;
  205. return err;
  206. }
  207. else return -ENOSYS;
  208. case EVIOCRMFF:
  209. if (dev->erase_effect) {
  210. return dev->erase_effect(dev, (int)arg);
  211. }
  212. else return -ENOSYS;
  213. case EVIOCGEFFECTS:
  214. if (put_user(dev->ff_effects_max, ip))
  215. return -EFAULT;
  216. return 0;
  217. case EVIOCGRAB:
  218. if (arg) {
  219. if (evdev->grab)
  220. return -EBUSY;
  221. if (input_grab_device(&evdev->handle))
  222. return -EBUSY;
  223. evdev->grab = list;
  224. return 0;
  225. } else {
  226. if (evdev->grab != list)
  227. return -EINVAL;
  228. input_release_device(&evdev->handle);
  229. evdev->grab = NULL;
  230. return 0;
  231. }
  232. default:
  233. if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
  234. return -EINVAL;
  235. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
  236. long *bits;
  237. int len;
  238. switch (_IOC_NR(cmd) & EV_MAX) {
  239. case 0: bits = dev->evbit; len = EV_MAX; break;
  240. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  241. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  242. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  243. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  244. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  245. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  246. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  247. default: return -EINVAL;
  248. }
  249. len = NBITS(len) * sizeof(long);
  250. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  251. return copy_to_user(p, bits, len) ? -EFAULT : len;
  252. }
  253. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
  254. int len;
  255. len = NBITS(KEY_MAX) * sizeof(long);
  256. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  257. return copy_to_user(p, dev->key, len) ? -EFAULT : len;
  258. }
  259. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
  260. int len;
  261. len = NBITS(LED_MAX) * sizeof(long);
  262. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  263. return copy_to_user(p, dev->led, len) ? -EFAULT : len;
  264. }
  265. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
  266. int len;
  267. len = NBITS(SND_MAX) * sizeof(long);
  268. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  269. return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
  270. }
  271. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
  272. int len;
  273. if (!dev->name) return -ENOENT;
  274. len = strlen(dev->name) + 1;
  275. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  276. return copy_to_user(p, dev->name, len) ? -EFAULT : len;
  277. }
  278. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
  279. int len;
  280. if (!dev->phys) return -ENOENT;
  281. len = strlen(dev->phys) + 1;
  282. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  283. return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
  284. }
  285. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
  286. int len;
  287. if (!dev->uniq) return -ENOENT;
  288. len = strlen(dev->uniq) + 1;
  289. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  290. return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
  291. }
  292. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  293. int t = _IOC_NR(cmd) & ABS_MAX;
  294. abs.value = dev->abs[t];
  295. abs.minimum = dev->absmin[t];
  296. abs.maximum = dev->absmax[t];
  297. abs.fuzz = dev->absfuzz[t];
  298. abs.flat = dev->absflat[t];
  299. if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
  300. return -EFAULT;
  301. return 0;
  302. }
  303. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  304. int t = _IOC_NR(cmd) & ABS_MAX;
  305. if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
  306. return -EFAULT;
  307. dev->abs[t] = abs.value;
  308. dev->absmin[t] = abs.minimum;
  309. dev->absmax[t] = abs.maximum;
  310. dev->absfuzz[t] = abs.fuzz;
  311. dev->absflat[t] = abs.flat;
  312. return 0;
  313. }
  314. }
  315. return -EINVAL;
  316. }
  317. static struct file_operations evdev_fops = {
  318. .owner = THIS_MODULE,
  319. .read = evdev_read,
  320. .write = evdev_write,
  321. .poll = evdev_poll,
  322. .open = evdev_open,
  323. .release = evdev_release,
  324. .ioctl = evdev_ioctl,
  325. .fasync = evdev_fasync,
  326. .flush = evdev_flush
  327. };
  328. static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
  329. {
  330. struct evdev *evdev;
  331. int minor;
  332. for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
  333. if (minor == EVDEV_MINORS) {
  334. printk(KERN_ERR "evdev: no more free evdev devices\n");
  335. return NULL;
  336. }
  337. if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
  338. return NULL;
  339. memset(evdev, 0, sizeof(struct evdev));
  340. INIT_LIST_HEAD(&evdev->list);
  341. init_waitqueue_head(&evdev->wait);
  342. evdev->exist = 1;
  343. evdev->minor = minor;
  344. evdev->handle.dev = dev;
  345. evdev->handle.name = evdev->name;
  346. evdev->handle.handler = handler;
  347. evdev->handle.private = evdev;
  348. sprintf(evdev->name, "event%d", minor);
  349. evdev_table[minor] = evdev;
  350. devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  351. S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
  352. class_device_create(input_class,
  353. MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  354. dev->dev, "event%d", minor);
  355. return &evdev->handle;
  356. }
  357. static void evdev_disconnect(struct input_handle *handle)
  358. {
  359. struct evdev *evdev = handle->private;
  360. struct evdev_list *list;
  361. class_device_destroy(input_class,
  362. MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
  363. devfs_remove("input/event%d", evdev->minor);
  364. evdev->exist = 0;
  365. if (evdev->open) {
  366. input_close_device(handle);
  367. wake_up_interruptible(&evdev->wait);
  368. list_for_each_entry(list, &evdev->list, node)
  369. kill_fasync(&list->fasync, SIGIO, POLL_HUP);
  370. } else
  371. evdev_free(evdev);
  372. }
  373. static struct input_device_id evdev_ids[] = {
  374. { .driver_info = 1 }, /* Matches all devices */
  375. { }, /* Terminating zero entry */
  376. };
  377. MODULE_DEVICE_TABLE(input, evdev_ids);
  378. static struct input_handler evdev_handler = {
  379. .event = evdev_event,
  380. .connect = evdev_connect,
  381. .disconnect = evdev_disconnect,
  382. .fops = &evdev_fops,
  383. .minor = EVDEV_MINOR_BASE,
  384. .name = "evdev",
  385. .id_table = evdev_ids,
  386. };
  387. static int __init evdev_init(void)
  388. {
  389. input_register_handler(&evdev_handler);
  390. return 0;
  391. }
  392. static void __exit evdev_exit(void)
  393. {
  394. input_unregister_handler(&evdev_handler);
  395. }
  396. module_init(evdev_init);
  397. module_exit(evdev_exit);
  398. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  399. MODULE_DESCRIPTION("Input driver event char devices");
  400. MODULE_LICENSE("GPL");