evdev.c 20 KB

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