evdev.c 24 KB

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