evdev.c 22 KB

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