evdev.c 28 KB

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