evdev.c 20 KB

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