evdev.c 24 KB

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