evdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #include <linux/compat.h>
  23. struct evdev {
  24. int exist;
  25. int open;
  26. int minor;
  27. char name[16];
  28. struct input_handle handle;
  29. wait_queue_head_t wait;
  30. struct evdev_list *grab;
  31. struct list_head list;
  32. };
  33. struct evdev_list {
  34. struct input_event buffer[EVDEV_BUFFER_SIZE];
  35. int head;
  36. int tail;
  37. struct fasync_struct *fasync;
  38. struct evdev *evdev;
  39. struct list_head node;
  40. };
  41. static struct evdev *evdev_table[EVDEV_MINORS];
  42. static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  43. {
  44. struct evdev *evdev = handle->private;
  45. struct evdev_list *list;
  46. if (evdev->grab) {
  47. list = evdev->grab;
  48. do_gettimeofday(&list->buffer[list->head].time);
  49. list->buffer[list->head].type = type;
  50. list->buffer[list->head].code = code;
  51. list->buffer[list->head].value = value;
  52. list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
  53. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  54. } else
  55. list_for_each_entry(list, &evdev->list, node) {
  56. do_gettimeofday(&list->buffer[list->head].time);
  57. list->buffer[list->head].type = type;
  58. list->buffer[list->head].code = code;
  59. list->buffer[list->head].value = value;
  60. list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
  61. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  62. }
  63. wake_up_interruptible(&evdev->wait);
  64. }
  65. static int evdev_fasync(int fd, struct file *file, int on)
  66. {
  67. int retval;
  68. struct evdev_list *list = file->private_data;
  69. retval = fasync_helper(fd, file, on, &list->fasync);
  70. return retval < 0 ? retval : 0;
  71. }
  72. static int evdev_flush(struct file * file)
  73. {
  74. struct evdev_list *list = file->private_data;
  75. if (!list->evdev->exist) return -ENODEV;
  76. return input_flush_device(&list->evdev->handle, file);
  77. }
  78. static void evdev_free(struct evdev *evdev)
  79. {
  80. evdev_table[evdev->minor] = NULL;
  81. kfree(evdev);
  82. }
  83. static int evdev_release(struct inode * inode, struct file * file)
  84. {
  85. struct evdev_list *list = file->private_data;
  86. if (list->evdev->grab == list) {
  87. input_release_device(&list->evdev->handle);
  88. list->evdev->grab = NULL;
  89. }
  90. evdev_fasync(-1, file, 0);
  91. list_del(&list->node);
  92. if (!--list->evdev->open) {
  93. if (list->evdev->exist)
  94. input_close_device(&list->evdev->handle);
  95. else
  96. evdev_free(list->evdev);
  97. }
  98. kfree(list);
  99. return 0;
  100. }
  101. static int evdev_open(struct inode * inode, struct file * file)
  102. {
  103. struct evdev_list *list;
  104. int i = iminor(inode) - EVDEV_MINOR_BASE;
  105. int accept_err;
  106. if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
  107. return -ENODEV;
  108. if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
  109. return accept_err;
  110. if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
  111. return -ENOMEM;
  112. memset(list, 0, sizeof(struct evdev_list));
  113. list->evdev = evdev_table[i];
  114. list_add_tail(&list->node, &evdev_table[i]->list);
  115. file->private_data = list;
  116. if (!list->evdev->open++)
  117. if (list->evdev->exist)
  118. input_open_device(&list->evdev->handle);
  119. return 0;
  120. }
  121. #ifdef CONFIG_COMPAT
  122. struct input_event_compat {
  123. struct compat_timeval time;
  124. __u16 type;
  125. __u16 code;
  126. __s32 value;
  127. };
  128. #ifdef CONFIG_X86_64
  129. # define COMPAT_TEST test_thread_flag(TIF_IA32)
  130. #elif defined(CONFIG_IA64)
  131. # define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
  132. #elif defined(CONFIG_ARCH_S390)
  133. # define COMPAT_TEST test_thread_flag(TIF_31BIT)
  134. #elif defined(CONFIG_MIPS)
  135. # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
  136. #else
  137. # define COMPAT_TEST test_thread_flag(TIF_32BIT)
  138. #endif
  139. static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  140. {
  141. struct evdev_list *list = file->private_data;
  142. struct input_event_compat event;
  143. int retval = 0;
  144. while (retval < count) {
  145. if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
  146. return -EFAULT;
  147. input_event(list->evdev->handle.dev, event.type, event.code, event.value);
  148. retval += sizeof(struct input_event_compat);
  149. }
  150. return retval;
  151. }
  152. #endif
  153. static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  154. {
  155. struct evdev_list *list = file->private_data;
  156. struct input_event event;
  157. int retval = 0;
  158. if (!list->evdev->exist) return -ENODEV;
  159. #ifdef CONFIG_COMPAT
  160. if (COMPAT_TEST)
  161. return evdev_write_compat(file, buffer, count, ppos);
  162. #endif
  163. while (retval < count) {
  164. if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
  165. return -EFAULT;
  166. input_event(list->evdev->handle.dev, event.type, event.code, event.value);
  167. retval += sizeof(struct input_event);
  168. }
  169. return retval;
  170. }
  171. #ifdef CONFIG_COMPAT
  172. static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  173. {
  174. struct evdev_list *list = file->private_data;
  175. int retval;
  176. if (count < sizeof(struct input_event_compat))
  177. return -EINVAL;
  178. if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
  179. return -EAGAIN;
  180. retval = wait_event_interruptible(list->evdev->wait,
  181. list->head != list->tail || (!list->evdev->exist));
  182. if (retval)
  183. return retval;
  184. if (!list->evdev->exist)
  185. return -ENODEV;
  186. while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
  187. struct input_event *event = (struct input_event *) list->buffer + list->tail;
  188. struct input_event_compat event_compat;
  189. event_compat.time.tv_sec = event->time.tv_sec;
  190. event_compat.time.tv_usec = event->time.tv_usec;
  191. event_compat.type = event->type;
  192. event_compat.code = event->code;
  193. event_compat.value = event->value;
  194. if (copy_to_user(buffer + retval, &event_compat,
  195. sizeof(struct input_event_compat))) return -EFAULT;
  196. list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
  197. retval += sizeof(struct input_event_compat);
  198. }
  199. return retval;
  200. }
  201. #endif
  202. static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  203. {
  204. struct evdev_list *list = file->private_data;
  205. int retval;
  206. #ifdef CONFIG_COMPAT
  207. if (COMPAT_TEST)
  208. return evdev_read_compat(file, buffer, count, ppos);
  209. #endif
  210. if (count < sizeof(struct input_event))
  211. return -EINVAL;
  212. if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
  213. return -EAGAIN;
  214. retval = wait_event_interruptible(list->evdev->wait,
  215. list->head != list->tail || (!list->evdev->exist));
  216. if (retval)
  217. return retval;
  218. if (!list->evdev->exist)
  219. return -ENODEV;
  220. while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
  221. if (copy_to_user(buffer + retval, list->buffer + list->tail,
  222. sizeof(struct input_event))) return -EFAULT;
  223. list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
  224. retval += sizeof(struct input_event);
  225. }
  226. return retval;
  227. }
  228. /* No kernel lock - fine */
  229. static unsigned int evdev_poll(struct file *file, poll_table *wait)
  230. {
  231. struct evdev_list *list = file->private_data;
  232. poll_wait(file, &list->evdev->wait, wait);
  233. return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
  234. (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
  235. }
  236. static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  237. {
  238. struct evdev_list *list = file->private_data;
  239. struct evdev *evdev = list->evdev;
  240. struct input_dev *dev = evdev->handle.dev;
  241. struct input_absinfo abs;
  242. void __user *p = (void __user *)arg;
  243. int __user *ip = (int __user *)arg;
  244. int i, t, u, v;
  245. if (!evdev->exist) return -ENODEV;
  246. switch (cmd) {
  247. case EVIOCGVERSION:
  248. return put_user(EV_VERSION, ip);
  249. case EVIOCGID:
  250. return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
  251. case EVIOCGKEYCODE:
  252. if (get_user(t, ip)) return -EFAULT;
  253. if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
  254. if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
  255. return 0;
  256. case EVIOCSKEYCODE:
  257. if (get_user(t, ip)) return -EFAULT;
  258. if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
  259. if (get_user(v, ip + 1)) return -EFAULT;
  260. if (v < 0 || v > KEY_MAX) return -EINVAL;
  261. if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8))) return -EINVAL;
  262. u = SET_INPUT_KEYCODE(dev, t, v);
  263. clear_bit(u, dev->keybit);
  264. set_bit(v, dev->keybit);
  265. for (i = 0; i < dev->keycodemax; i++)
  266. if (INPUT_KEYCODE(dev,i) == u)
  267. set_bit(u, dev->keybit);
  268. return 0;
  269. case EVIOCSFF:
  270. if (dev->upload_effect) {
  271. struct ff_effect effect;
  272. int err;
  273. if (copy_from_user(&effect, p, sizeof(effect)))
  274. return -EFAULT;
  275. err = dev->upload_effect(dev, &effect);
  276. if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
  277. return -EFAULT;
  278. return err;
  279. }
  280. else return -ENOSYS;
  281. case EVIOCRMFF:
  282. if (dev->erase_effect) {
  283. return dev->erase_effect(dev, (int)arg);
  284. }
  285. else return -ENOSYS;
  286. case EVIOCGEFFECTS:
  287. if (put_user(dev->ff_effects_max, ip))
  288. return -EFAULT;
  289. return 0;
  290. case EVIOCGRAB:
  291. if (arg) {
  292. if (evdev->grab)
  293. return -EBUSY;
  294. if (input_grab_device(&evdev->handle))
  295. return -EBUSY;
  296. evdev->grab = list;
  297. return 0;
  298. } else {
  299. if (evdev->grab != list)
  300. return -EINVAL;
  301. input_release_device(&evdev->handle);
  302. evdev->grab = NULL;
  303. return 0;
  304. }
  305. default:
  306. if (_IOC_TYPE(cmd) != 'E')
  307. return -EINVAL;
  308. if (_IOC_DIR(cmd) == _IOC_READ) {
  309. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
  310. long *bits;
  311. int len;
  312. switch (_IOC_NR(cmd) & EV_MAX) {
  313. case 0: bits = dev->evbit; len = EV_MAX; break;
  314. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  315. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  316. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  317. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  318. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  319. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  320. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  321. case EV_SW: bits = dev->swbit; len = SW_MAX; break;
  322. default: return -EINVAL;
  323. }
  324. len = NBITS(len) * sizeof(long);
  325. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  326. return copy_to_user(p, bits, len) ? -EFAULT : len;
  327. }
  328. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
  329. int len;
  330. len = NBITS(KEY_MAX) * sizeof(long);
  331. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  332. return copy_to_user(p, dev->key, len) ? -EFAULT : len;
  333. }
  334. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
  335. int len;
  336. len = NBITS(LED_MAX) * sizeof(long);
  337. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  338. return copy_to_user(p, dev->led, len) ? -EFAULT : len;
  339. }
  340. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
  341. int len;
  342. len = NBITS(SND_MAX) * sizeof(long);
  343. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  344. return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
  345. }
  346. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) {
  347. int len;
  348. len = NBITS(SW_MAX) * sizeof(long);
  349. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  350. return copy_to_user(p, dev->sw, len) ? -EFAULT : len;
  351. }
  352. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
  353. int len;
  354. if (!dev->name) return -ENOENT;
  355. len = strlen(dev->name) + 1;
  356. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  357. return copy_to_user(p, dev->name, len) ? -EFAULT : len;
  358. }
  359. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
  360. int len;
  361. if (!dev->phys) return -ENOENT;
  362. len = strlen(dev->phys) + 1;
  363. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  364. return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
  365. }
  366. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
  367. int len;
  368. if (!dev->uniq) return -ENOENT;
  369. len = strlen(dev->uniq) + 1;
  370. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  371. return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
  372. }
  373. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  374. int t = _IOC_NR(cmd) & ABS_MAX;
  375. abs.value = dev->abs[t];
  376. abs.minimum = dev->absmin[t];
  377. abs.maximum = dev->absmax[t];
  378. abs.fuzz = dev->absfuzz[t];
  379. abs.flat = dev->absflat[t];
  380. if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
  381. return -EFAULT;
  382. return 0;
  383. }
  384. }
  385. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  386. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  387. int t = _IOC_NR(cmd) & ABS_MAX;
  388. if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
  389. return -EFAULT;
  390. dev->abs[t] = abs.value;
  391. dev->absmin[t] = abs.minimum;
  392. dev->absmax[t] = abs.maximum;
  393. dev->absfuzz[t] = abs.fuzz;
  394. dev->absflat[t] = abs.flat;
  395. return 0;
  396. }
  397. }
  398. }
  399. return -EINVAL;
  400. }
  401. #ifdef CONFIG_COMPAT
  402. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  403. #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
  404. #define OFF_COMPAT(x) ((x)%BITS_PER_LONG_COMPAT)
  405. #define BIT_COMPAT(x) (1UL<<OFF_COMPAT(x))
  406. #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
  407. #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
  408. #ifdef __BIG_ENDIAN
  409. #define bit_to_user(bit, max) \
  410. do { \
  411. int i; \
  412. int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
  413. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
  414. for (i = 0; i < len / sizeof(compat_long_t); i++) \
  415. if (copy_to_user((compat_long_t __user *) p + i, \
  416. (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
  417. sizeof(compat_long_t))) \
  418. return -EFAULT; \
  419. return len; \
  420. } while (0)
  421. #else
  422. #define bit_to_user(bit, max) \
  423. do { \
  424. int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
  425. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
  426. return copy_to_user(p, (bit), len) ? -EFAULT : len; \
  427. } while (0)
  428. #endif
  429. static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  430. {
  431. struct evdev_list *list = file->private_data;
  432. struct evdev *evdev = list->evdev;
  433. struct input_dev *dev = evdev->handle.dev;
  434. struct input_absinfo abs;
  435. void __user *p = compat_ptr(arg);
  436. if (!evdev->exist) return -ENODEV;
  437. switch (cmd) {
  438. case EVIOCGVERSION:
  439. case EVIOCGID:
  440. case EVIOCGKEYCODE:
  441. case EVIOCSKEYCODE:
  442. case EVIOCSFF:
  443. case EVIOCRMFF:
  444. case EVIOCGEFFECTS:
  445. case EVIOCGRAB:
  446. return evdev_ioctl(file, cmd, (unsigned long) p);
  447. default:
  448. if (_IOC_TYPE(cmd) != 'E')
  449. return -EINVAL;
  450. if (_IOC_DIR(cmd) == _IOC_READ) {
  451. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
  452. long *bits;
  453. int max;
  454. switch (_IOC_NR(cmd) & EV_MAX) {
  455. case 0: bits = dev->evbit; max = EV_MAX; break;
  456. case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
  457. case EV_REL: bits = dev->relbit; max = REL_MAX; break;
  458. case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
  459. case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
  460. case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
  461. case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
  462. case EV_FF: bits = dev->ffbit; max = FF_MAX; break;
  463. default: return -EINVAL;
  464. }
  465. bit_to_user(bits, max);
  466. }
  467. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
  468. bit_to_user(dev->key, KEY_MAX);
  469. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
  470. bit_to_user(dev->led, LED_MAX);
  471. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
  472. bit_to_user(dev->snd, SND_MAX);
  473. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
  474. int len;
  475. if (!dev->name) return -ENOENT;
  476. len = strlen(dev->name) + 1;
  477. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  478. return copy_to_user(p, dev->name, len) ? -EFAULT : len;
  479. }
  480. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
  481. int len;
  482. if (!dev->phys) return -ENOENT;
  483. len = strlen(dev->phys) + 1;
  484. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  485. return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
  486. }
  487. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
  488. int len;
  489. if (!dev->uniq) return -ENOENT;
  490. len = strlen(dev->uniq) + 1;
  491. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  492. return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
  493. }
  494. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  495. int t = _IOC_NR(cmd) & ABS_MAX;
  496. abs.value = dev->abs[t];
  497. abs.minimum = dev->absmin[t];
  498. abs.maximum = dev->absmax[t];
  499. abs.fuzz = dev->absfuzz[t];
  500. abs.flat = dev->absflat[t];
  501. if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
  502. return -EFAULT;
  503. return 0;
  504. }
  505. }
  506. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  507. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  508. int t = _IOC_NR(cmd) & ABS_MAX;
  509. if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
  510. return -EFAULT;
  511. dev->abs[t] = abs.value;
  512. dev->absmin[t] = abs.minimum;
  513. dev->absmax[t] = abs.maximum;
  514. dev->absfuzz[t] = abs.fuzz;
  515. dev->absflat[t] = abs.flat;
  516. return 0;
  517. }
  518. }
  519. }
  520. return -EINVAL;
  521. }
  522. #endif
  523. static struct file_operations evdev_fops = {
  524. .owner = THIS_MODULE,
  525. .read = evdev_read,
  526. .write = evdev_write,
  527. .poll = evdev_poll,
  528. .open = evdev_open,
  529. .release = evdev_release,
  530. .unlocked_ioctl = evdev_ioctl,
  531. #ifdef CONFIG_COMPAT
  532. .compat_ioctl = evdev_ioctl_compat,
  533. #endif
  534. .fasync = evdev_fasync,
  535. .flush = evdev_flush
  536. };
  537. static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
  538. {
  539. struct evdev *evdev;
  540. int minor;
  541. for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
  542. if (minor == EVDEV_MINORS) {
  543. printk(KERN_ERR "evdev: no more free evdev devices\n");
  544. return NULL;
  545. }
  546. if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
  547. return NULL;
  548. memset(evdev, 0, sizeof(struct evdev));
  549. INIT_LIST_HEAD(&evdev->list);
  550. init_waitqueue_head(&evdev->wait);
  551. evdev->exist = 1;
  552. evdev->minor = minor;
  553. evdev->handle.dev = dev;
  554. evdev->handle.name = evdev->name;
  555. evdev->handle.handler = handler;
  556. evdev->handle.private = evdev;
  557. sprintf(evdev->name, "event%d", minor);
  558. evdev_table[minor] = evdev;
  559. devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  560. S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
  561. class_device_create(input_class,
  562. MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  563. dev->dev, "event%d", minor);
  564. return &evdev->handle;
  565. }
  566. static void evdev_disconnect(struct input_handle *handle)
  567. {
  568. struct evdev *evdev = handle->private;
  569. struct evdev_list *list;
  570. class_device_destroy(input_class,
  571. MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
  572. devfs_remove("input/event%d", evdev->minor);
  573. evdev->exist = 0;
  574. if (evdev->open) {
  575. input_close_device(handle);
  576. wake_up_interruptible(&evdev->wait);
  577. list_for_each_entry(list, &evdev->list, node)
  578. kill_fasync(&list->fasync, SIGIO, POLL_HUP);
  579. } else
  580. evdev_free(evdev);
  581. }
  582. static struct input_device_id evdev_ids[] = {
  583. { .driver_info = 1 }, /* Matches all devices */
  584. { }, /* Terminating zero entry */
  585. };
  586. MODULE_DEVICE_TABLE(input, evdev_ids);
  587. static struct input_handler evdev_handler = {
  588. .event = evdev_event,
  589. .connect = evdev_connect,
  590. .disconnect = evdev_disconnect,
  591. .fops = &evdev_fops,
  592. .minor = EVDEV_MINOR_BASE,
  593. .name = "evdev",
  594. .id_table = evdev_ids,
  595. };
  596. static int __init evdev_init(void)
  597. {
  598. input_register_handler(&evdev_handler);
  599. return 0;
  600. }
  601. static void __exit evdev_exit(void)
  602. {
  603. input_unregister_handler(&evdev_handler);
  604. }
  605. module_init(evdev_init);
  606. module_exit(evdev_exit);
  607. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  608. MODULE_DESCRIPTION("Input driver event char devices");
  609. MODULE_LICENSE("GPL");