evdev.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #define EVDEV_MINOR_BASE 64
  12. #define EVDEV_MINORS 32
  13. #define EVDEV_MIN_BUFFER_SIZE 64U
  14. #define EVDEV_BUF_PACKETS 8
  15. #include <linux/poll.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/input.h>
  21. #include <linux/major.h>
  22. #include <linux/device.h>
  23. #include "input-compat.h"
  24. struct evdev {
  25. int open;
  26. int minor;
  27. struct input_handle handle;
  28. wait_queue_head_t wait;
  29. struct evdev_client __rcu *grab;
  30. struct list_head client_list;
  31. spinlock_t client_lock; /* protects client_list */
  32. struct mutex mutex;
  33. struct device dev;
  34. bool exist;
  35. };
  36. struct evdev_client {
  37. int head;
  38. int tail;
  39. spinlock_t buffer_lock; /* protects access to buffer, head and tail */
  40. struct fasync_struct *fasync;
  41. struct evdev *evdev;
  42. struct list_head node;
  43. int bufsize;
  44. struct input_event buffer[];
  45. };
  46. static struct evdev *evdev_table[EVDEV_MINORS];
  47. static DEFINE_MUTEX(evdev_table_mutex);
  48. static void evdev_pass_event(struct evdev_client *client,
  49. struct input_event *event)
  50. {
  51. /*
  52. * Interrupts are disabled, just acquire the lock.
  53. * Make sure we don't leave with the client buffer
  54. * "empty" by having client->head == client->tail.
  55. */
  56. spin_lock(&client->buffer_lock);
  57. do {
  58. client->buffer[client->head++] = *event;
  59. client->head &= client->bufsize - 1;
  60. } while (client->head == client->tail);
  61. spin_unlock(&client->buffer_lock);
  62. if (event->type == EV_SYN)
  63. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  64. }
  65. /*
  66. * Pass incoming event to all connected clients.
  67. */
  68. static void evdev_event(struct input_handle *handle,
  69. unsigned int type, unsigned int code, int value)
  70. {
  71. struct evdev *evdev = handle->private;
  72. struct evdev_client *client;
  73. struct input_event event;
  74. do_gettimeofday(&event.time);
  75. event.type = type;
  76. event.code = code;
  77. event.value = value;
  78. rcu_read_lock();
  79. client = rcu_dereference(evdev->grab);
  80. if (client)
  81. evdev_pass_event(client, &event);
  82. else
  83. list_for_each_entry_rcu(client, &evdev->client_list, node)
  84. evdev_pass_event(client, &event);
  85. rcu_read_unlock();
  86. wake_up_interruptible(&evdev->wait);
  87. }
  88. static int evdev_fasync(int fd, struct file *file, int on)
  89. {
  90. struct evdev_client *client = file->private_data;
  91. return fasync_helper(fd, file, on, &client->fasync);
  92. }
  93. static int evdev_flush(struct file *file, fl_owner_t id)
  94. {
  95. struct evdev_client *client = file->private_data;
  96. struct evdev *evdev = client->evdev;
  97. int retval;
  98. retval = mutex_lock_interruptible(&evdev->mutex);
  99. if (retval)
  100. return retval;
  101. if (!evdev->exist)
  102. retval = -ENODEV;
  103. else
  104. retval = input_flush_device(&evdev->handle, file);
  105. mutex_unlock(&evdev->mutex);
  106. return retval;
  107. }
  108. static void evdev_free(struct device *dev)
  109. {
  110. struct evdev *evdev = container_of(dev, struct evdev, dev);
  111. input_put_device(evdev->handle.dev);
  112. kfree(evdev);
  113. }
  114. /*
  115. * Grabs an event device (along with underlying input device).
  116. * This function is called with evdev->mutex taken.
  117. */
  118. static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
  119. {
  120. int error;
  121. if (evdev->grab)
  122. return -EBUSY;
  123. error = input_grab_device(&evdev->handle);
  124. if (error)
  125. return error;
  126. rcu_assign_pointer(evdev->grab, client);
  127. synchronize_rcu();
  128. return 0;
  129. }
  130. static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
  131. {
  132. if (evdev->grab != client)
  133. return -EINVAL;
  134. rcu_assign_pointer(evdev->grab, NULL);
  135. synchronize_rcu();
  136. input_release_device(&evdev->handle);
  137. return 0;
  138. }
  139. static void evdev_attach_client(struct evdev *evdev,
  140. struct evdev_client *client)
  141. {
  142. spin_lock(&evdev->client_lock);
  143. list_add_tail_rcu(&client->node, &evdev->client_list);
  144. spin_unlock(&evdev->client_lock);
  145. synchronize_rcu();
  146. }
  147. static void evdev_detach_client(struct evdev *evdev,
  148. struct evdev_client *client)
  149. {
  150. spin_lock(&evdev->client_lock);
  151. list_del_rcu(&client->node);
  152. spin_unlock(&evdev->client_lock);
  153. synchronize_rcu();
  154. }
  155. static int evdev_open_device(struct evdev *evdev)
  156. {
  157. int retval;
  158. retval = mutex_lock_interruptible(&evdev->mutex);
  159. if (retval)
  160. return retval;
  161. if (!evdev->exist)
  162. retval = -ENODEV;
  163. else if (!evdev->open++) {
  164. retval = input_open_device(&evdev->handle);
  165. if (retval)
  166. evdev->open--;
  167. }
  168. mutex_unlock(&evdev->mutex);
  169. return retval;
  170. }
  171. static void evdev_close_device(struct evdev *evdev)
  172. {
  173. mutex_lock(&evdev->mutex);
  174. if (evdev->exist && !--evdev->open)
  175. input_close_device(&evdev->handle);
  176. mutex_unlock(&evdev->mutex);
  177. }
  178. /*
  179. * Wake up users waiting for IO so they can disconnect from
  180. * dead device.
  181. */
  182. static void evdev_hangup(struct evdev *evdev)
  183. {
  184. struct evdev_client *client;
  185. spin_lock(&evdev->client_lock);
  186. list_for_each_entry(client, &evdev->client_list, node)
  187. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  188. spin_unlock(&evdev->client_lock);
  189. wake_up_interruptible(&evdev->wait);
  190. }
  191. static int evdev_release(struct inode *inode, struct file *file)
  192. {
  193. struct evdev_client *client = file->private_data;
  194. struct evdev *evdev = client->evdev;
  195. mutex_lock(&evdev->mutex);
  196. if (evdev->grab == client)
  197. evdev_ungrab(evdev, client);
  198. mutex_unlock(&evdev->mutex);
  199. evdev_detach_client(evdev, client);
  200. kfree(client);
  201. evdev_close_device(evdev);
  202. put_device(&evdev->dev);
  203. return 0;
  204. }
  205. static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
  206. {
  207. unsigned int n_events =
  208. max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
  209. EVDEV_MIN_BUFFER_SIZE);
  210. return roundup_pow_of_two(n_events);
  211. }
  212. static int evdev_open(struct inode *inode, struct file *file)
  213. {
  214. struct evdev *evdev;
  215. struct evdev_client *client;
  216. int i = iminor(inode) - EVDEV_MINOR_BASE;
  217. unsigned int bufsize;
  218. int error;
  219. if (i >= EVDEV_MINORS)
  220. return -ENODEV;
  221. error = mutex_lock_interruptible(&evdev_table_mutex);
  222. if (error)
  223. return error;
  224. evdev = evdev_table[i];
  225. if (evdev)
  226. get_device(&evdev->dev);
  227. mutex_unlock(&evdev_table_mutex);
  228. if (!evdev)
  229. return -ENODEV;
  230. bufsize = evdev_compute_buffer_size(evdev->handle.dev);
  231. client = kzalloc(sizeof(struct evdev_client) +
  232. bufsize * sizeof(struct input_event),
  233. GFP_KERNEL);
  234. if (!client) {
  235. error = -ENOMEM;
  236. goto err_put_evdev;
  237. }
  238. client->bufsize = bufsize;
  239. spin_lock_init(&client->buffer_lock);
  240. client->evdev = evdev;
  241. evdev_attach_client(evdev, client);
  242. error = evdev_open_device(evdev);
  243. if (error)
  244. goto err_free_client;
  245. file->private_data = client;
  246. nonseekable_open(inode, file);
  247. return 0;
  248. err_free_client:
  249. evdev_detach_client(evdev, client);
  250. kfree(client);
  251. err_put_evdev:
  252. put_device(&evdev->dev);
  253. return error;
  254. }
  255. static ssize_t evdev_write(struct file *file, const char __user *buffer,
  256. size_t count, loff_t *ppos)
  257. {
  258. struct evdev_client *client = file->private_data;
  259. struct evdev *evdev = client->evdev;
  260. struct input_event event;
  261. int retval;
  262. if (count < input_event_size())
  263. return -EINVAL;
  264. retval = mutex_lock_interruptible(&evdev->mutex);
  265. if (retval)
  266. return retval;
  267. if (!evdev->exist) {
  268. retval = -ENODEV;
  269. goto out;
  270. }
  271. do {
  272. if (input_event_from_user(buffer + retval, &event)) {
  273. retval = -EFAULT;
  274. goto out;
  275. }
  276. retval += input_event_size();
  277. input_inject_event(&evdev->handle,
  278. event.type, event.code, event.value);
  279. } while (retval + input_event_size() <= count);
  280. out:
  281. mutex_unlock(&evdev->mutex);
  282. return retval;
  283. }
  284. static int evdev_fetch_next_event(struct evdev_client *client,
  285. struct input_event *event)
  286. {
  287. int have_event;
  288. spin_lock_irq(&client->buffer_lock);
  289. have_event = client->head != client->tail;
  290. if (have_event) {
  291. *event = client->buffer[client->tail++];
  292. client->tail &= client->bufsize - 1;
  293. }
  294. spin_unlock_irq(&client->buffer_lock);
  295. return have_event;
  296. }
  297. static ssize_t evdev_read(struct file *file, char __user *buffer,
  298. size_t count, loff_t *ppos)
  299. {
  300. struct evdev_client *client = file->private_data;
  301. struct evdev *evdev = client->evdev;
  302. struct input_event event;
  303. int retval;
  304. if (count < input_event_size())
  305. return -EINVAL;
  306. if (client->head == client->tail && evdev->exist &&
  307. (file->f_flags & O_NONBLOCK))
  308. return -EAGAIN;
  309. retval = wait_event_interruptible(evdev->wait,
  310. client->head != client->tail || !evdev->exist);
  311. if (retval)
  312. return retval;
  313. if (!evdev->exist)
  314. return -ENODEV;
  315. while (retval + input_event_size() <= count &&
  316. evdev_fetch_next_event(client, &event)) {
  317. if (input_event_to_user(buffer + retval, &event))
  318. return -EFAULT;
  319. retval += input_event_size();
  320. }
  321. return retval;
  322. }
  323. /* No kernel lock - fine */
  324. static unsigned int evdev_poll(struct file *file, poll_table *wait)
  325. {
  326. struct evdev_client *client = file->private_data;
  327. struct evdev *evdev = client->evdev;
  328. unsigned int mask;
  329. poll_wait(file, &evdev->wait, wait);
  330. mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
  331. if (client->head != client->tail)
  332. mask |= POLLIN | POLLRDNORM;
  333. return mask;
  334. }
  335. #ifdef CONFIG_COMPAT
  336. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  337. #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
  338. #ifdef __BIG_ENDIAN
  339. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  340. unsigned int maxlen, void __user *p, int compat)
  341. {
  342. int len, i;
  343. if (compat) {
  344. len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
  345. if (len > maxlen)
  346. len = maxlen;
  347. for (i = 0; i < len / sizeof(compat_long_t); i++)
  348. if (copy_to_user((compat_long_t __user *) p + i,
  349. (compat_long_t *) bits +
  350. i + 1 - ((i % 2) << 1),
  351. sizeof(compat_long_t)))
  352. return -EFAULT;
  353. } else {
  354. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  355. if (len > maxlen)
  356. len = maxlen;
  357. if (copy_to_user(p, bits, len))
  358. return -EFAULT;
  359. }
  360. return len;
  361. }
  362. #else
  363. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  364. unsigned int maxlen, void __user *p, int compat)
  365. {
  366. int len = compat ?
  367. BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
  368. BITS_TO_LONGS(maxbit) * sizeof(long);
  369. if (len > maxlen)
  370. len = maxlen;
  371. return copy_to_user(p, bits, len) ? -EFAULT : len;
  372. }
  373. #endif /* __BIG_ENDIAN */
  374. #else
  375. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  376. unsigned int maxlen, void __user *p, int compat)
  377. {
  378. int len = BITS_TO_LONGS(maxbit) * sizeof(long);
  379. if (len > maxlen)
  380. len = maxlen;
  381. return copy_to_user(p, bits, len) ? -EFAULT : len;
  382. }
  383. #endif /* CONFIG_COMPAT */
  384. static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
  385. {
  386. int len;
  387. if (!str)
  388. return -ENOENT;
  389. len = strlen(str) + 1;
  390. if (len > maxlen)
  391. len = maxlen;
  392. return copy_to_user(p, str, len) ? -EFAULT : len;
  393. }
  394. #define OLD_KEY_MAX 0x1ff
  395. static int handle_eviocgbit(struct input_dev *dev,
  396. unsigned int type, unsigned int size,
  397. void __user *p, int compat_mode)
  398. {
  399. static unsigned long keymax_warn_time;
  400. unsigned long *bits;
  401. int len;
  402. switch (type) {
  403. case 0: bits = dev->evbit; len = EV_MAX; break;
  404. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  405. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  406. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  407. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  408. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  409. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  410. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  411. case EV_SW: bits = dev->swbit; len = SW_MAX; break;
  412. default: return -EINVAL;
  413. }
  414. /*
  415. * Work around bugs in userspace programs that like to do
  416. * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
  417. * should be in bytes, not in bits.
  418. */
  419. if (type == EV_KEY && size == OLD_KEY_MAX) {
  420. len = OLD_KEY_MAX;
  421. if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
  422. pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
  423. "limiting output to %zu bytes. See "
  424. "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
  425. OLD_KEY_MAX,
  426. BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
  427. }
  428. return bits_to_user(bits, len, size, p, compat_mode);
  429. }
  430. #undef OLD_KEY_MAX
  431. static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
  432. {
  433. struct input_keymap_entry ke = {
  434. .len = sizeof(unsigned int),
  435. .flags = 0,
  436. };
  437. int __user *ip = (int __user *)p;
  438. int error;
  439. /* legacy case */
  440. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  441. return -EFAULT;
  442. error = input_get_keycode(dev, &ke);
  443. if (error)
  444. return error;
  445. if (put_user(ke.keycode, ip + 1))
  446. return -EFAULT;
  447. return 0;
  448. }
  449. static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
  450. {
  451. struct input_keymap_entry ke;
  452. int error;
  453. if (copy_from_user(&ke, p, sizeof(ke)))
  454. return -EFAULT;
  455. error = input_get_keycode(dev, &ke);
  456. if (error)
  457. return error;
  458. if (copy_to_user(p, &ke, sizeof(ke)))
  459. return -EFAULT;
  460. return 0;
  461. }
  462. static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
  463. {
  464. struct input_keymap_entry ke = {
  465. .len = sizeof(unsigned int),
  466. .flags = 0,
  467. };
  468. int __user *ip = (int __user *)p;
  469. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  470. return -EFAULT;
  471. if (get_user(ke.keycode, ip + 1))
  472. return -EFAULT;
  473. return input_set_keycode(dev, &ke);
  474. }
  475. static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
  476. {
  477. struct input_keymap_entry ke;
  478. if (copy_from_user(&ke, p, sizeof(ke)))
  479. return -EFAULT;
  480. if (ke.len > sizeof(ke.scancode))
  481. return -EINVAL;
  482. return input_set_keycode(dev, &ke);
  483. }
  484. static long evdev_do_ioctl(struct file *file, unsigned int cmd,
  485. void __user *p, int compat_mode)
  486. {
  487. struct evdev_client *client = file->private_data;
  488. struct evdev *evdev = client->evdev;
  489. struct input_dev *dev = evdev->handle.dev;
  490. struct input_absinfo abs;
  491. struct ff_effect effect;
  492. int __user *ip = (int __user *)p;
  493. unsigned int i, t, u, v;
  494. unsigned int size;
  495. int error;
  496. /* First we check for fixed-length commands */
  497. switch (cmd) {
  498. case EVIOCGVERSION:
  499. return put_user(EV_VERSION, ip);
  500. case EVIOCGID:
  501. if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
  502. return -EFAULT;
  503. return 0;
  504. case EVIOCGREP:
  505. if (!test_bit(EV_REP, dev->evbit))
  506. return -ENOSYS;
  507. if (put_user(dev->rep[REP_DELAY], ip))
  508. return -EFAULT;
  509. if (put_user(dev->rep[REP_PERIOD], ip + 1))
  510. return -EFAULT;
  511. return 0;
  512. case EVIOCSREP:
  513. if (!test_bit(EV_REP, dev->evbit))
  514. return -ENOSYS;
  515. if (get_user(u, ip))
  516. return -EFAULT;
  517. if (get_user(v, ip + 1))
  518. return -EFAULT;
  519. input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
  520. input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
  521. return 0;
  522. case EVIOCRMFF:
  523. return input_ff_erase(dev, (int)(unsigned long) p, file);
  524. case EVIOCGEFFECTS:
  525. i = test_bit(EV_FF, dev->evbit) ?
  526. dev->ff->max_effects : 0;
  527. if (put_user(i, ip))
  528. return -EFAULT;
  529. return 0;
  530. case EVIOCGRAB:
  531. if (p)
  532. return evdev_grab(evdev, client);
  533. else
  534. return evdev_ungrab(evdev, client);
  535. case EVIOCGKEYCODE:
  536. return evdev_handle_get_keycode(dev, p);
  537. case EVIOCSKEYCODE:
  538. return evdev_handle_set_keycode(dev, p);
  539. case EVIOCGKEYCODE_V2:
  540. return evdev_handle_get_keycode_v2(dev, p);
  541. case EVIOCSKEYCODE_V2:
  542. return evdev_handle_set_keycode_v2(dev, p);
  543. }
  544. size = _IOC_SIZE(cmd);
  545. /* Now check variable-length commands */
  546. #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
  547. switch (EVIOC_MASK_SIZE(cmd)) {
  548. case EVIOCGPROP(0):
  549. return bits_to_user(dev->propbit, INPUT_PROP_MAX,
  550. size, p, compat_mode);
  551. case EVIOCGKEY(0):
  552. return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
  553. case EVIOCGLED(0):
  554. return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
  555. case EVIOCGSND(0):
  556. return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
  557. case EVIOCGSW(0):
  558. return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
  559. case EVIOCGNAME(0):
  560. return str_to_user(dev->name, size, p);
  561. case EVIOCGPHYS(0):
  562. return str_to_user(dev->phys, size, p);
  563. case EVIOCGUNIQ(0):
  564. return str_to_user(dev->uniq, size, p);
  565. case EVIOC_MASK_SIZE(EVIOCSFF):
  566. if (input_ff_effect_from_user(p, size, &effect))
  567. return -EFAULT;
  568. error = input_ff_upload(dev, &effect, file);
  569. if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
  570. return -EFAULT;
  571. return error;
  572. }
  573. /* Multi-number variable-length handlers */
  574. if (_IOC_TYPE(cmd) != 'E')
  575. return -EINVAL;
  576. if (_IOC_DIR(cmd) == _IOC_READ) {
  577. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
  578. return handle_eviocgbit(dev,
  579. _IOC_NR(cmd) & EV_MAX, size,
  580. p, compat_mode);
  581. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  582. if (!dev->absinfo)
  583. return -EINVAL;
  584. t = _IOC_NR(cmd) & ABS_MAX;
  585. abs = dev->absinfo[t];
  586. if (copy_to_user(p, &abs, min_t(size_t,
  587. size, sizeof(struct input_absinfo))))
  588. return -EFAULT;
  589. return 0;
  590. }
  591. }
  592. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  593. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  594. if (!dev->absinfo)
  595. return -EINVAL;
  596. t = _IOC_NR(cmd) & ABS_MAX;
  597. if (copy_from_user(&abs, p, min_t(size_t,
  598. size, sizeof(struct input_absinfo))))
  599. return -EFAULT;
  600. if (size < sizeof(struct input_absinfo))
  601. abs.resolution = 0;
  602. /* We can't change number of reserved MT slots */
  603. if (t == ABS_MT_SLOT)
  604. return -EINVAL;
  605. /*
  606. * Take event lock to ensure that we are not
  607. * changing device parameters in the middle
  608. * of event.
  609. */
  610. spin_lock_irq(&dev->event_lock);
  611. dev->absinfo[t] = abs;
  612. spin_unlock_irq(&dev->event_lock);
  613. return 0;
  614. }
  615. }
  616. return -EINVAL;
  617. }
  618. static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
  619. void __user *p, int compat_mode)
  620. {
  621. struct evdev_client *client = file->private_data;
  622. struct evdev *evdev = client->evdev;
  623. int retval;
  624. retval = mutex_lock_interruptible(&evdev->mutex);
  625. if (retval)
  626. return retval;
  627. if (!evdev->exist) {
  628. retval = -ENODEV;
  629. goto out;
  630. }
  631. retval = evdev_do_ioctl(file, cmd, p, compat_mode);
  632. out:
  633. mutex_unlock(&evdev->mutex);
  634. return retval;
  635. }
  636. static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  637. {
  638. return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
  639. }
  640. #ifdef CONFIG_COMPAT
  641. static long evdev_ioctl_compat(struct file *file,
  642. unsigned int cmd, unsigned long arg)
  643. {
  644. return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
  645. }
  646. #endif
  647. static const struct file_operations evdev_fops = {
  648. .owner = THIS_MODULE,
  649. .read = evdev_read,
  650. .write = evdev_write,
  651. .poll = evdev_poll,
  652. .open = evdev_open,
  653. .release = evdev_release,
  654. .unlocked_ioctl = evdev_ioctl,
  655. #ifdef CONFIG_COMPAT
  656. .compat_ioctl = evdev_ioctl_compat,
  657. #endif
  658. .fasync = evdev_fasync,
  659. .flush = evdev_flush,
  660. .llseek = no_llseek,
  661. };
  662. static int evdev_install_chrdev(struct evdev *evdev)
  663. {
  664. /*
  665. * No need to do any locking here as calls to connect and
  666. * disconnect are serialized by the input core
  667. */
  668. evdev_table[evdev->minor] = evdev;
  669. return 0;
  670. }
  671. static void evdev_remove_chrdev(struct evdev *evdev)
  672. {
  673. /*
  674. * Lock evdev table to prevent race with evdev_open()
  675. */
  676. mutex_lock(&evdev_table_mutex);
  677. evdev_table[evdev->minor] = NULL;
  678. mutex_unlock(&evdev_table_mutex);
  679. }
  680. /*
  681. * Mark device non-existent. This disables writes, ioctls and
  682. * prevents new users from opening the device. Already posted
  683. * blocking reads will stay, however new ones will fail.
  684. */
  685. static void evdev_mark_dead(struct evdev *evdev)
  686. {
  687. mutex_lock(&evdev->mutex);
  688. evdev->exist = false;
  689. mutex_unlock(&evdev->mutex);
  690. }
  691. static void evdev_cleanup(struct evdev *evdev)
  692. {
  693. struct input_handle *handle = &evdev->handle;
  694. evdev_mark_dead(evdev);
  695. evdev_hangup(evdev);
  696. evdev_remove_chrdev(evdev);
  697. /* evdev is marked dead so no one else accesses evdev->open */
  698. if (evdev->open) {
  699. input_flush_device(handle, NULL);
  700. input_close_device(handle);
  701. }
  702. }
  703. /*
  704. * Create new evdev device. Note that input core serializes calls
  705. * to connect and disconnect so we don't need to lock evdev_table here.
  706. */
  707. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
  708. const struct input_device_id *id)
  709. {
  710. struct evdev *evdev;
  711. int minor;
  712. int error;
  713. for (minor = 0; minor < EVDEV_MINORS; minor++)
  714. if (!evdev_table[minor])
  715. break;
  716. if (minor == EVDEV_MINORS) {
  717. pr_err("no more free evdev devices\n");
  718. return -ENFILE;
  719. }
  720. evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
  721. if (!evdev)
  722. return -ENOMEM;
  723. INIT_LIST_HEAD(&evdev->client_list);
  724. spin_lock_init(&evdev->client_lock);
  725. mutex_init(&evdev->mutex);
  726. init_waitqueue_head(&evdev->wait);
  727. dev_set_name(&evdev->dev, "event%d", minor);
  728. evdev->exist = true;
  729. evdev->minor = minor;
  730. evdev->handle.dev = input_get_device(dev);
  731. evdev->handle.name = dev_name(&evdev->dev);
  732. evdev->handle.handler = handler;
  733. evdev->handle.private = evdev;
  734. evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
  735. evdev->dev.class = &input_class;
  736. evdev->dev.parent = &dev->dev;
  737. evdev->dev.release = evdev_free;
  738. device_initialize(&evdev->dev);
  739. error = input_register_handle(&evdev->handle);
  740. if (error)
  741. goto err_free_evdev;
  742. error = evdev_install_chrdev(evdev);
  743. if (error)
  744. goto err_unregister_handle;
  745. error = device_add(&evdev->dev);
  746. if (error)
  747. goto err_cleanup_evdev;
  748. return 0;
  749. err_cleanup_evdev:
  750. evdev_cleanup(evdev);
  751. err_unregister_handle:
  752. input_unregister_handle(&evdev->handle);
  753. err_free_evdev:
  754. put_device(&evdev->dev);
  755. return error;
  756. }
  757. static void evdev_disconnect(struct input_handle *handle)
  758. {
  759. struct evdev *evdev = handle->private;
  760. device_del(&evdev->dev);
  761. evdev_cleanup(evdev);
  762. input_unregister_handle(handle);
  763. put_device(&evdev->dev);
  764. }
  765. static const struct input_device_id evdev_ids[] = {
  766. { .driver_info = 1 }, /* Matches all devices */
  767. { }, /* Terminating zero entry */
  768. };
  769. MODULE_DEVICE_TABLE(input, evdev_ids);
  770. static struct input_handler evdev_handler = {
  771. .event = evdev_event,
  772. .connect = evdev_connect,
  773. .disconnect = evdev_disconnect,
  774. .fops = &evdev_fops,
  775. .minor = EVDEV_MINOR_BASE,
  776. .name = "evdev",
  777. .id_table = evdev_ids,
  778. };
  779. static int __init evdev_init(void)
  780. {
  781. return input_register_handler(&evdev_handler);
  782. }
  783. static void __exit evdev_exit(void)
  784. {
  785. input_unregister_handler(&evdev_handler);
  786. }
  787. module_init(evdev_init);
  788. module_exit(evdev_exit);
  789. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  790. MODULE_DESCRIPTION("Input driver event char devices");
  791. MODULE_LICENSE("GPL");