evdev.c 25 KB

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