evdev.c 20 KB

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