evdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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' || _IOC_DIR(cmd) != _IOC_READ)
  304. return -EINVAL;
  305. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
  306. long *bits;
  307. int len;
  308. switch (_IOC_NR(cmd) & EV_MAX) {
  309. case 0: bits = dev->evbit; len = EV_MAX; break;
  310. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  311. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  312. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  313. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  314. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  315. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  316. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  317. default: return -EINVAL;
  318. }
  319. len = NBITS(len) * sizeof(long);
  320. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  321. return copy_to_user(p, bits, len) ? -EFAULT : len;
  322. }
  323. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
  324. int len;
  325. len = NBITS(KEY_MAX) * sizeof(long);
  326. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  327. return copy_to_user(p, dev->key, len) ? -EFAULT : len;
  328. }
  329. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
  330. int len;
  331. len = NBITS(LED_MAX) * sizeof(long);
  332. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  333. return copy_to_user(p, dev->led, len) ? -EFAULT : len;
  334. }
  335. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
  336. int len;
  337. len = NBITS(SND_MAX) * sizeof(long);
  338. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  339. return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
  340. }
  341. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
  342. int len;
  343. if (!dev->name) return -ENOENT;
  344. len = strlen(dev->name) + 1;
  345. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  346. return copy_to_user(p, dev->name, len) ? -EFAULT : len;
  347. }
  348. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
  349. int len;
  350. if (!dev->phys) return -ENOENT;
  351. len = strlen(dev->phys) + 1;
  352. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  353. return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
  354. }
  355. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
  356. int len;
  357. if (!dev->uniq) return -ENOENT;
  358. len = strlen(dev->uniq) + 1;
  359. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  360. return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
  361. }
  362. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  363. int t = _IOC_NR(cmd) & ABS_MAX;
  364. abs.value = dev->abs[t];
  365. abs.minimum = dev->absmin[t];
  366. abs.maximum = dev->absmax[t];
  367. abs.fuzz = dev->absfuzz[t];
  368. abs.flat = dev->absflat[t];
  369. if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
  370. return -EFAULT;
  371. return 0;
  372. }
  373. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  374. int t = _IOC_NR(cmd) & ABS_MAX;
  375. if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
  376. return -EFAULT;
  377. dev->abs[t] = abs.value;
  378. dev->absmin[t] = abs.minimum;
  379. dev->absmax[t] = abs.maximum;
  380. dev->absfuzz[t] = abs.fuzz;
  381. dev->absflat[t] = abs.flat;
  382. return 0;
  383. }
  384. }
  385. return -EINVAL;
  386. }
  387. #ifdef CONFIG_COMPAT
  388. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  389. #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
  390. #define OFF_COMPAT(x) ((x)%BITS_PER_LONG_COMPAT)
  391. #define BIT_COMPAT(x) (1UL<<OFF_COMPAT(x))
  392. #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
  393. #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
  394. static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  395. {
  396. struct evdev_list *list = file->private_data;
  397. struct evdev *evdev = list->evdev;
  398. struct input_dev *dev = evdev->handle.dev;
  399. struct input_absinfo abs;
  400. void __user *p = compat_ptr(arg);
  401. #ifdef __BIG_ENDIAN
  402. int i;
  403. #endif
  404. if (!evdev->exist) return -ENODEV;
  405. switch (cmd) {
  406. case EVIOCGVERSION:
  407. case EVIOCGID:
  408. case EVIOCGKEYCODE:
  409. case EVIOCSKEYCODE:
  410. case EVIOCSFF:
  411. case EVIOCRMFF:
  412. case EVIOCGEFFECTS:
  413. case EVIOCGRAB:
  414. return evdev_ioctl(file, cmd, (unsigned long) p);
  415. default:
  416. if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
  417. return -EINVAL;
  418. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
  419. long *bits;
  420. int len;
  421. switch (_IOC_NR(cmd) & EV_MAX) {
  422. case 0: bits = dev->evbit; len = EV_MAX; break;
  423. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  424. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  425. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  426. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  427. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  428. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  429. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  430. default: return -EINVAL;
  431. }
  432. len = NBITS_COMPAT(len) * sizeof(compat_long_t);
  433. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  434. #ifdef __BIG_ENDIAN
  435. for (i = 0; i < len / sizeof(compat_long_t); i++)
  436. if (copy_to_user((compat_long_t*) p + i,
  437. (compat_long_t*) bits + i + 1 - ((i % 2) << 1),
  438. sizeof(compat_long_t)))
  439. return -EFAULT;
  440. return len;
  441. #else
  442. return copy_to_user(p, bits, len) ? -EFAULT : len;
  443. #endif
  444. }
  445. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
  446. int len;
  447. len = NBITS_COMPAT(KEY_MAX) * sizeof(compat_long_t);
  448. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  449. #ifdef __BIG_ENDIAN
  450. for (i = 0; i < len / sizeof(compat_long_t); i++)
  451. if (copy_to_user((compat_long_t*) p + i,
  452. (compat_long_t*) dev->key + i + 1 - ((i % 2) << 1),
  453. sizeof(compat_long_t)))
  454. return -EFAULT;
  455. return len;
  456. #else
  457. return copy_to_user(p, dev->key, len) ? -EFAULT : len;
  458. #endif
  459. }
  460. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
  461. int len;
  462. len = NBITS_COMPAT(LED_MAX) * sizeof(compat_long_t);
  463. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  464. #ifdef __BIG_ENDIAN
  465. for (i = 0; i < len / sizeof(compat_long_t); i++)
  466. if (copy_to_user((compat_long_t*) p + i,
  467. (compat_long_t*) dev->led + i + 1 - ((i % 2) << 1),
  468. sizeof(compat_long_t)))
  469. return -EFAULT;
  470. return len;
  471. #else
  472. return copy_to_user(p, dev->led, len) ? -EFAULT : len;
  473. #endif
  474. }
  475. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
  476. int len;
  477. len = NBITS_COMPAT(SND_MAX) * sizeof(compat_long_t);
  478. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  479. #ifdef __BIG_ENDIAN
  480. for (i = 0; i < len / sizeof(compat_long_t); i++)
  481. if (copy_to_user((compat_long_t*) p + i,
  482. (compat_long_t*) dev->snd + i + 1 - ((i % 2) << 1),
  483. sizeof(compat_long_t)))
  484. return -EFAULT;
  485. return len;
  486. #else
  487. return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
  488. #endif
  489. }
  490. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
  491. int len;
  492. if (!dev->name) return -ENOENT;
  493. len = strlen(dev->name) + 1;
  494. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  495. return copy_to_user(p, dev->name, len) ? -EFAULT : len;
  496. }
  497. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
  498. int len;
  499. if (!dev->phys) return -ENOENT;
  500. len = strlen(dev->phys) + 1;
  501. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  502. return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
  503. }
  504. if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
  505. int len;
  506. if (!dev->uniq) return -ENOENT;
  507. len = strlen(dev->uniq) + 1;
  508. if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
  509. return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
  510. }
  511. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  512. int t = _IOC_NR(cmd) & ABS_MAX;
  513. abs.value = dev->abs[t];
  514. abs.minimum = dev->absmin[t];
  515. abs.maximum = dev->absmax[t];
  516. abs.fuzz = dev->absfuzz[t];
  517. abs.flat = dev->absflat[t];
  518. if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
  519. return -EFAULT;
  520. return 0;
  521. }
  522. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  523. int t = _IOC_NR(cmd) & ABS_MAX;
  524. if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
  525. return -EFAULT;
  526. dev->abs[t] = abs.value;
  527. dev->absmin[t] = abs.minimum;
  528. dev->absmax[t] = abs.maximum;
  529. dev->absfuzz[t] = abs.fuzz;
  530. dev->absflat[t] = abs.flat;
  531. return 0;
  532. }
  533. }
  534. return -EINVAL;
  535. }
  536. #endif
  537. static struct file_operations evdev_fops = {
  538. .owner = THIS_MODULE,
  539. .read = evdev_read,
  540. .write = evdev_write,
  541. .poll = evdev_poll,
  542. .open = evdev_open,
  543. .release = evdev_release,
  544. .unlocked_ioctl = evdev_ioctl,
  545. #ifdef CONFIG_COMPAT
  546. .compat_ioctl = evdev_ioctl_compat,
  547. #endif
  548. .fasync = evdev_fasync,
  549. .flush = evdev_flush
  550. };
  551. static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
  552. {
  553. struct evdev *evdev;
  554. int minor;
  555. for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
  556. if (minor == EVDEV_MINORS) {
  557. printk(KERN_ERR "evdev: no more free evdev devices\n");
  558. return NULL;
  559. }
  560. if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
  561. return NULL;
  562. memset(evdev, 0, sizeof(struct evdev));
  563. INIT_LIST_HEAD(&evdev->list);
  564. init_waitqueue_head(&evdev->wait);
  565. evdev->exist = 1;
  566. evdev->minor = minor;
  567. evdev->handle.dev = dev;
  568. evdev->handle.name = evdev->name;
  569. evdev->handle.handler = handler;
  570. evdev->handle.private = evdev;
  571. sprintf(evdev->name, "event%d", minor);
  572. evdev_table[minor] = evdev;
  573. devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  574. S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
  575. class_simple_device_add(input_class,
  576. MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
  577. dev->dev, "event%d", minor);
  578. return &evdev->handle;
  579. }
  580. static void evdev_disconnect(struct input_handle *handle)
  581. {
  582. struct evdev *evdev = handle->private;
  583. struct evdev_list *list;
  584. class_simple_device_remove(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
  585. devfs_remove("input/event%d", evdev->minor);
  586. evdev->exist = 0;
  587. if (evdev->open) {
  588. input_close_device(handle);
  589. wake_up_interruptible(&evdev->wait);
  590. list_for_each_entry(list, &evdev->list, node)
  591. kill_fasync(&list->fasync, SIGIO, POLL_HUP);
  592. } else
  593. evdev_free(evdev);
  594. }
  595. static struct input_device_id evdev_ids[] = {
  596. { .driver_info = 1 }, /* Matches all devices */
  597. { }, /* Terminating zero entry */
  598. };
  599. MODULE_DEVICE_TABLE(input, evdev_ids);
  600. static struct input_handler evdev_handler = {
  601. .event = evdev_event,
  602. .connect = evdev_connect,
  603. .disconnect = evdev_disconnect,
  604. .fops = &evdev_fops,
  605. .minor = EVDEV_MINOR_BASE,
  606. .name = "evdev",
  607. .id_table = evdev_ids,
  608. };
  609. static int __init evdev_init(void)
  610. {
  611. input_register_handler(&evdev_handler);
  612. return 0;
  613. }
  614. static void __exit evdev_exit(void)
  615. {
  616. input_unregister_handler(&evdev_handler);
  617. }
  618. module_init(evdev_init);
  619. module_exit(evdev_exit);
  620. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  621. MODULE_DESCRIPTION("Input driver event char devices");
  622. MODULE_LICENSE("GPL");