evdev.c 24 KB

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