evdev.c 22 KB

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