evdev.c 22 KB

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