evdev.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. kvfree(client);
  343. evdev_close_device(evdev);
  344. return 0;
  345. }
  346. static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
  347. {
  348. unsigned int n_events =
  349. max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
  350. EVDEV_MIN_BUFFER_SIZE);
  351. return roundup_pow_of_two(n_events);
  352. }
  353. static int evdev_open(struct inode *inode, struct file *file)
  354. {
  355. struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
  356. unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
  357. unsigned int size = sizeof(struct evdev_client) +
  358. bufsize * sizeof(struct input_event);
  359. struct evdev_client *client;
  360. int error;
  361. client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  362. if (!client)
  363. client = vzalloc(size);
  364. if (!client)
  365. return -ENOMEM;
  366. client->bufsize = bufsize;
  367. spin_lock_init(&client->buffer_lock);
  368. client->evdev = evdev;
  369. evdev_attach_client(evdev, client);
  370. error = evdev_open_device(evdev);
  371. if (error)
  372. goto err_free_client;
  373. file->private_data = client;
  374. nonseekable_open(inode, file);
  375. return 0;
  376. err_free_client:
  377. evdev_detach_client(evdev, client);
  378. kvfree(client);
  379. return error;
  380. }
  381. static ssize_t evdev_write(struct file *file, const char __user *buffer,
  382. size_t count, loff_t *ppos)
  383. {
  384. struct evdev_client *client = file->private_data;
  385. struct evdev *evdev = client->evdev;
  386. struct input_event event;
  387. int retval = 0;
  388. if (count != 0 && count < input_event_size())
  389. return -EINVAL;
  390. retval = mutex_lock_interruptible(&evdev->mutex);
  391. if (retval)
  392. return retval;
  393. if (!evdev->exist || client->revoked) {
  394. retval = -ENODEV;
  395. goto out;
  396. }
  397. while (retval + input_event_size() <= count) {
  398. if (input_event_from_user(buffer + retval, &event)) {
  399. retval = -EFAULT;
  400. goto out;
  401. }
  402. retval += input_event_size();
  403. input_inject_event(&evdev->handle,
  404. event.type, event.code, event.value);
  405. }
  406. out:
  407. mutex_unlock(&evdev->mutex);
  408. return retval;
  409. }
  410. static int evdev_fetch_next_event(struct evdev_client *client,
  411. struct input_event *event)
  412. {
  413. int have_event;
  414. spin_lock_irq(&client->buffer_lock);
  415. have_event = client->packet_head != client->tail;
  416. if (have_event) {
  417. *event = client->buffer[client->tail++];
  418. client->tail &= client->bufsize - 1;
  419. }
  420. spin_unlock_irq(&client->buffer_lock);
  421. return have_event;
  422. }
  423. static ssize_t evdev_read(struct file *file, char __user *buffer,
  424. size_t count, loff_t *ppos)
  425. {
  426. struct evdev_client *client = file->private_data;
  427. struct evdev *evdev = client->evdev;
  428. struct input_event event;
  429. size_t read = 0;
  430. int error;
  431. if (count != 0 && count < input_event_size())
  432. return -EINVAL;
  433. for (;;) {
  434. if (!evdev->exist || client->revoked)
  435. return -ENODEV;
  436. if (client->packet_head == client->tail &&
  437. (file->f_flags & O_NONBLOCK))
  438. return -EAGAIN;
  439. /*
  440. * count == 0 is special - no IO is done but we check
  441. * for error conditions (see above).
  442. */
  443. if (count == 0)
  444. break;
  445. while (read + input_event_size() <= count &&
  446. evdev_fetch_next_event(client, &event)) {
  447. if (input_event_to_user(buffer + read, &event))
  448. return -EFAULT;
  449. read += input_event_size();
  450. }
  451. if (read)
  452. break;
  453. if (!(file->f_flags & O_NONBLOCK)) {
  454. error = wait_event_interruptible(evdev->wait,
  455. client->packet_head != client->tail ||
  456. !evdev->exist || client->revoked);
  457. if (error)
  458. return error;
  459. }
  460. }
  461. return read;
  462. }
  463. /* No kernel lock - fine */
  464. static unsigned int evdev_poll(struct file *file, poll_table *wait)
  465. {
  466. struct evdev_client *client = file->private_data;
  467. struct evdev *evdev = client->evdev;
  468. unsigned int mask;
  469. poll_wait(file, &evdev->wait, wait);
  470. if (evdev->exist && !client->revoked)
  471. mask = POLLOUT | POLLWRNORM;
  472. else
  473. mask = POLLHUP | POLLERR;
  474. if (client->packet_head != client->tail)
  475. mask |= POLLIN | POLLRDNORM;
  476. return mask;
  477. }
  478. #ifdef CONFIG_COMPAT
  479. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  480. #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
  481. #ifdef __BIG_ENDIAN
  482. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  483. unsigned int maxlen, void __user *p, int compat)
  484. {
  485. int len, i;
  486. if (compat) {
  487. len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
  488. if (len > maxlen)
  489. len = maxlen;
  490. for (i = 0; i < len / sizeof(compat_long_t); i++)
  491. if (copy_to_user((compat_long_t __user *) p + i,
  492. (compat_long_t *) bits +
  493. i + 1 - ((i % 2) << 1),
  494. sizeof(compat_long_t)))
  495. return -EFAULT;
  496. } else {
  497. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  498. if (len > maxlen)
  499. len = maxlen;
  500. if (copy_to_user(p, bits, len))
  501. return -EFAULT;
  502. }
  503. return len;
  504. }
  505. #else
  506. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  507. unsigned int maxlen, void __user *p, int compat)
  508. {
  509. int len = compat ?
  510. BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
  511. BITS_TO_LONGS(maxbit) * sizeof(long);
  512. if (len > maxlen)
  513. len = maxlen;
  514. return copy_to_user(p, bits, len) ? -EFAULT : len;
  515. }
  516. #endif /* __BIG_ENDIAN */
  517. #else
  518. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  519. unsigned int maxlen, void __user *p, int compat)
  520. {
  521. int len = BITS_TO_LONGS(maxbit) * sizeof(long);
  522. if (len > maxlen)
  523. len = maxlen;
  524. return copy_to_user(p, bits, len) ? -EFAULT : len;
  525. }
  526. #endif /* CONFIG_COMPAT */
  527. static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
  528. {
  529. int len;
  530. if (!str)
  531. return -ENOENT;
  532. len = strlen(str) + 1;
  533. if (len > maxlen)
  534. len = maxlen;
  535. return copy_to_user(p, str, len) ? -EFAULT : len;
  536. }
  537. static int handle_eviocgbit(struct input_dev *dev,
  538. unsigned int type, unsigned int size,
  539. void __user *p, int compat_mode)
  540. {
  541. unsigned long *bits;
  542. int len;
  543. switch (type) {
  544. case 0: bits = dev->evbit; len = EV_MAX; break;
  545. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  546. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  547. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  548. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  549. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  550. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  551. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  552. case EV_SW: bits = dev->swbit; len = SW_MAX; break;
  553. default: return -EINVAL;
  554. }
  555. return bits_to_user(bits, len, size, p, compat_mode);
  556. }
  557. static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
  558. {
  559. struct input_keymap_entry ke = {
  560. .len = sizeof(unsigned int),
  561. .flags = 0,
  562. };
  563. int __user *ip = (int __user *)p;
  564. int error;
  565. /* legacy case */
  566. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  567. return -EFAULT;
  568. error = input_get_keycode(dev, &ke);
  569. if (error)
  570. return error;
  571. if (put_user(ke.keycode, ip + 1))
  572. return -EFAULT;
  573. return 0;
  574. }
  575. static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
  576. {
  577. struct input_keymap_entry ke;
  578. int error;
  579. if (copy_from_user(&ke, p, sizeof(ke)))
  580. return -EFAULT;
  581. error = input_get_keycode(dev, &ke);
  582. if (error)
  583. return error;
  584. if (copy_to_user(p, &ke, sizeof(ke)))
  585. return -EFAULT;
  586. return 0;
  587. }
  588. static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
  589. {
  590. struct input_keymap_entry ke = {
  591. .len = sizeof(unsigned int),
  592. .flags = 0,
  593. };
  594. int __user *ip = (int __user *)p;
  595. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  596. return -EFAULT;
  597. if (get_user(ke.keycode, ip + 1))
  598. return -EFAULT;
  599. return input_set_keycode(dev, &ke);
  600. }
  601. static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
  602. {
  603. struct input_keymap_entry ke;
  604. if (copy_from_user(&ke, p, sizeof(ke)))
  605. return -EFAULT;
  606. if (ke.len > sizeof(ke.scancode))
  607. return -EINVAL;
  608. return input_set_keycode(dev, &ke);
  609. }
  610. /*
  611. * If we transfer state to the user, we should flush all pending events
  612. * of the same type from the client's queue. Otherwise, they might end up
  613. * with duplicate events, which can screw up client's state tracking.
  614. * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
  615. * event so user-space will notice missing events.
  616. *
  617. * LOCKING:
  618. * We need to take event_lock before buffer_lock to avoid dead-locks. But we
  619. * need the even_lock only to guarantee consistent state. We can safely release
  620. * it while flushing the queue. This allows input-core to handle filters while
  621. * we flush the queue.
  622. */
  623. static int evdev_handle_get_val(struct evdev_client *client,
  624. struct input_dev *dev, unsigned int type,
  625. unsigned long *bits, unsigned int maxbit,
  626. unsigned int maxlen, void __user *p,
  627. int compat)
  628. {
  629. int ret;
  630. unsigned long *mem;
  631. size_t len;
  632. len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
  633. mem = kmalloc(len, GFP_KERNEL);
  634. if (!mem)
  635. return -ENOMEM;
  636. spin_lock_irq(&dev->event_lock);
  637. spin_lock(&client->buffer_lock);
  638. memcpy(mem, bits, len);
  639. spin_unlock(&dev->event_lock);
  640. __evdev_flush_queue(client, type);
  641. spin_unlock_irq(&client->buffer_lock);
  642. ret = bits_to_user(mem, maxbit, maxlen, p, compat);
  643. if (ret < 0)
  644. evdev_queue_syn_dropped(client);
  645. kfree(mem);
  646. return ret;
  647. }
  648. static int evdev_handle_mt_request(struct input_dev *dev,
  649. unsigned int size,
  650. int __user *ip)
  651. {
  652. const struct input_mt *mt = dev->mt;
  653. unsigned int code;
  654. int max_slots;
  655. int i;
  656. if (get_user(code, &ip[0]))
  657. return -EFAULT;
  658. if (!mt || !input_is_mt_value(code))
  659. return -EINVAL;
  660. max_slots = (size - sizeof(__u32)) / sizeof(__s32);
  661. for (i = 0; i < mt->num_slots && i < max_slots; i++) {
  662. int value = input_mt_get_value(&mt->slots[i], code);
  663. if (put_user(value, &ip[1 + i]))
  664. return -EFAULT;
  665. }
  666. return 0;
  667. }
  668. static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
  669. struct file *file)
  670. {
  671. client->revoked = true;
  672. evdev_ungrab(evdev, client);
  673. input_flush_device(&evdev->handle, file);
  674. wake_up_interruptible(&evdev->wait);
  675. return 0;
  676. }
  677. static long evdev_do_ioctl(struct file *file, unsigned int cmd,
  678. void __user *p, int compat_mode)
  679. {
  680. struct evdev_client *client = file->private_data;
  681. struct evdev *evdev = client->evdev;
  682. struct input_dev *dev = evdev->handle.dev;
  683. struct input_absinfo abs;
  684. struct ff_effect effect;
  685. int __user *ip = (int __user *)p;
  686. unsigned int i, t, u, v;
  687. unsigned int size;
  688. int error;
  689. /* First we check for fixed-length commands */
  690. switch (cmd) {
  691. case EVIOCGVERSION:
  692. return put_user(EV_VERSION, ip);
  693. case EVIOCGID:
  694. if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
  695. return -EFAULT;
  696. return 0;
  697. case EVIOCGREP:
  698. if (!test_bit(EV_REP, dev->evbit))
  699. return -ENOSYS;
  700. if (put_user(dev->rep[REP_DELAY], ip))
  701. return -EFAULT;
  702. if (put_user(dev->rep[REP_PERIOD], ip + 1))
  703. return -EFAULT;
  704. return 0;
  705. case EVIOCSREP:
  706. if (!test_bit(EV_REP, dev->evbit))
  707. return -ENOSYS;
  708. if (get_user(u, ip))
  709. return -EFAULT;
  710. if (get_user(v, ip + 1))
  711. return -EFAULT;
  712. input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
  713. input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
  714. return 0;
  715. case EVIOCRMFF:
  716. return input_ff_erase(dev, (int)(unsigned long) p, file);
  717. case EVIOCGEFFECTS:
  718. i = test_bit(EV_FF, dev->evbit) ?
  719. dev->ff->max_effects : 0;
  720. if (put_user(i, ip))
  721. return -EFAULT;
  722. return 0;
  723. case EVIOCGRAB:
  724. if (p)
  725. return evdev_grab(evdev, client);
  726. else
  727. return evdev_ungrab(evdev, client);
  728. case EVIOCREVOKE:
  729. if (p)
  730. return -EINVAL;
  731. else
  732. return evdev_revoke(evdev, client, file);
  733. case EVIOCSCLOCKID:
  734. if (copy_from_user(&i, p, sizeof(unsigned int)))
  735. return -EFAULT;
  736. return evdev_set_clk_type(client, i);
  737. case EVIOCGKEYCODE:
  738. return evdev_handle_get_keycode(dev, p);
  739. case EVIOCSKEYCODE:
  740. return evdev_handle_set_keycode(dev, p);
  741. case EVIOCGKEYCODE_V2:
  742. return evdev_handle_get_keycode_v2(dev, p);
  743. case EVIOCSKEYCODE_V2:
  744. return evdev_handle_set_keycode_v2(dev, p);
  745. }
  746. size = _IOC_SIZE(cmd);
  747. /* Now check variable-length commands */
  748. #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
  749. switch (EVIOC_MASK_SIZE(cmd)) {
  750. case EVIOCGPROP(0):
  751. return bits_to_user(dev->propbit, INPUT_PROP_MAX,
  752. size, p, compat_mode);
  753. case EVIOCGMTSLOTS(0):
  754. return evdev_handle_mt_request(dev, size, ip);
  755. case EVIOCGKEY(0):
  756. return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
  757. KEY_MAX, size, p, compat_mode);
  758. case EVIOCGLED(0):
  759. return evdev_handle_get_val(client, dev, EV_LED, dev->led,
  760. LED_MAX, size, p, compat_mode);
  761. case EVIOCGSND(0):
  762. return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
  763. SND_MAX, size, p, compat_mode);
  764. case EVIOCGSW(0):
  765. return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
  766. SW_MAX, size, p, compat_mode);
  767. case EVIOCGNAME(0):
  768. return str_to_user(dev->name, size, p);
  769. case EVIOCGPHYS(0):
  770. return str_to_user(dev->phys, size, p);
  771. case EVIOCGUNIQ(0):
  772. return str_to_user(dev->uniq, size, p);
  773. case EVIOC_MASK_SIZE(EVIOCSFF):
  774. if (input_ff_effect_from_user(p, size, &effect))
  775. return -EFAULT;
  776. error = input_ff_upload(dev, &effect, file);
  777. if (error)
  778. return error;
  779. if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
  780. return -EFAULT;
  781. return 0;
  782. }
  783. /* Multi-number variable-length handlers */
  784. if (_IOC_TYPE(cmd) != 'E')
  785. return -EINVAL;
  786. if (_IOC_DIR(cmd) == _IOC_READ) {
  787. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
  788. return handle_eviocgbit(dev,
  789. _IOC_NR(cmd) & EV_MAX, size,
  790. p, compat_mode);
  791. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  792. if (!dev->absinfo)
  793. return -EINVAL;
  794. t = _IOC_NR(cmd) & ABS_MAX;
  795. abs = dev->absinfo[t];
  796. if (copy_to_user(p, &abs, min_t(size_t,
  797. size, sizeof(struct input_absinfo))))
  798. return -EFAULT;
  799. return 0;
  800. }
  801. }
  802. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  803. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  804. if (!dev->absinfo)
  805. return -EINVAL;
  806. t = _IOC_NR(cmd) & ABS_MAX;
  807. if (copy_from_user(&abs, p, min_t(size_t,
  808. size, sizeof(struct input_absinfo))))
  809. return -EFAULT;
  810. if (size < sizeof(struct input_absinfo))
  811. abs.resolution = 0;
  812. /* We can't change number of reserved MT slots */
  813. if (t == ABS_MT_SLOT)
  814. return -EINVAL;
  815. /*
  816. * Take event lock to ensure that we are not
  817. * changing device parameters in the middle
  818. * of event.
  819. */
  820. spin_lock_irq(&dev->event_lock);
  821. dev->absinfo[t] = abs;
  822. spin_unlock_irq(&dev->event_lock);
  823. return 0;
  824. }
  825. }
  826. return -EINVAL;
  827. }
  828. static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
  829. void __user *p, int compat_mode)
  830. {
  831. struct evdev_client *client = file->private_data;
  832. struct evdev *evdev = client->evdev;
  833. int retval;
  834. retval = mutex_lock_interruptible(&evdev->mutex);
  835. if (retval)
  836. return retval;
  837. if (!evdev->exist || client->revoked) {
  838. retval = -ENODEV;
  839. goto out;
  840. }
  841. retval = evdev_do_ioctl(file, cmd, p, compat_mode);
  842. out:
  843. mutex_unlock(&evdev->mutex);
  844. return retval;
  845. }
  846. static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  847. {
  848. return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
  849. }
  850. #ifdef CONFIG_COMPAT
  851. static long evdev_ioctl_compat(struct file *file,
  852. unsigned int cmd, unsigned long arg)
  853. {
  854. return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
  855. }
  856. #endif
  857. static const struct file_operations evdev_fops = {
  858. .owner = THIS_MODULE,
  859. .read = evdev_read,
  860. .write = evdev_write,
  861. .poll = evdev_poll,
  862. .open = evdev_open,
  863. .release = evdev_release,
  864. .unlocked_ioctl = evdev_ioctl,
  865. #ifdef CONFIG_COMPAT
  866. .compat_ioctl = evdev_ioctl_compat,
  867. #endif
  868. .fasync = evdev_fasync,
  869. .flush = evdev_flush,
  870. .llseek = no_llseek,
  871. };
  872. /*
  873. * Mark device non-existent. This disables writes, ioctls and
  874. * prevents new users from opening the device. Already posted
  875. * blocking reads will stay, however new ones will fail.
  876. */
  877. static void evdev_mark_dead(struct evdev *evdev)
  878. {
  879. mutex_lock(&evdev->mutex);
  880. evdev->exist = false;
  881. mutex_unlock(&evdev->mutex);
  882. }
  883. static void evdev_cleanup(struct evdev *evdev)
  884. {
  885. struct input_handle *handle = &evdev->handle;
  886. evdev_mark_dead(evdev);
  887. evdev_hangup(evdev);
  888. cdev_del(&evdev->cdev);
  889. /* evdev is marked dead so no one else accesses evdev->open */
  890. if (evdev->open) {
  891. input_flush_device(handle, NULL);
  892. input_close_device(handle);
  893. }
  894. }
  895. /*
  896. * Create new evdev device. Note that input core serializes calls
  897. * to connect and disconnect.
  898. */
  899. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
  900. const struct input_device_id *id)
  901. {
  902. struct evdev *evdev;
  903. int minor;
  904. int dev_no;
  905. int error;
  906. minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
  907. if (minor < 0) {
  908. error = minor;
  909. pr_err("failed to reserve new minor: %d\n", error);
  910. return error;
  911. }
  912. evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
  913. if (!evdev) {
  914. error = -ENOMEM;
  915. goto err_free_minor;
  916. }
  917. INIT_LIST_HEAD(&evdev->client_list);
  918. spin_lock_init(&evdev->client_lock);
  919. mutex_init(&evdev->mutex);
  920. init_waitqueue_head(&evdev->wait);
  921. evdev->exist = true;
  922. dev_no = minor;
  923. /* Normalize device number if it falls into legacy range */
  924. if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
  925. dev_no -= EVDEV_MINOR_BASE;
  926. dev_set_name(&evdev->dev, "event%d", dev_no);
  927. evdev->handle.dev = input_get_device(dev);
  928. evdev->handle.name = dev_name(&evdev->dev);
  929. evdev->handle.handler = handler;
  930. evdev->handle.private = evdev;
  931. evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  932. evdev->dev.class = &input_class;
  933. evdev->dev.parent = &dev->dev;
  934. evdev->dev.release = evdev_free;
  935. device_initialize(&evdev->dev);
  936. error = input_register_handle(&evdev->handle);
  937. if (error)
  938. goto err_free_evdev;
  939. cdev_init(&evdev->cdev, &evdev_fops);
  940. evdev->cdev.kobj.parent = &evdev->dev.kobj;
  941. error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
  942. if (error)
  943. goto err_unregister_handle;
  944. error = device_add(&evdev->dev);
  945. if (error)
  946. goto err_cleanup_evdev;
  947. return 0;
  948. err_cleanup_evdev:
  949. evdev_cleanup(evdev);
  950. err_unregister_handle:
  951. input_unregister_handle(&evdev->handle);
  952. err_free_evdev:
  953. put_device(&evdev->dev);
  954. err_free_minor:
  955. input_free_minor(minor);
  956. return error;
  957. }
  958. static void evdev_disconnect(struct input_handle *handle)
  959. {
  960. struct evdev *evdev = handle->private;
  961. device_del(&evdev->dev);
  962. evdev_cleanup(evdev);
  963. input_free_minor(MINOR(evdev->dev.devt));
  964. input_unregister_handle(handle);
  965. put_device(&evdev->dev);
  966. }
  967. static const struct input_device_id evdev_ids[] = {
  968. { .driver_info = 1 }, /* Matches all devices */
  969. { }, /* Terminating zero entry */
  970. };
  971. MODULE_DEVICE_TABLE(input, evdev_ids);
  972. static struct input_handler evdev_handler = {
  973. .event = evdev_event,
  974. .events = evdev_events,
  975. .connect = evdev_connect,
  976. .disconnect = evdev_disconnect,
  977. .legacy_minors = true,
  978. .minor = EVDEV_MINOR_BASE,
  979. .name = "evdev",
  980. .id_table = evdev_ids,
  981. };
  982. static int __init evdev_init(void)
  983. {
  984. return input_register_handler(&evdev_handler);
  985. }
  986. static void __exit evdev_exit(void)
  987. {
  988. input_unregister_handler(&evdev_handler);
  989. }
  990. module_init(evdev_init);
  991. module_exit(evdev_exit);
  992. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  993. MODULE_DESCRIPTION("Input driver event char devices");
  994. MODULE_LICENSE("GPL");