evdev.c 20 KB

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