evdev.c 20 KB

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