evdev.c 22 KB

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