evdev.c 28 KB

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