evdev.c 28 KB

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