evdev.c 19 KB

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