evdev.c 28 KB

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