evdev.c 24 KB

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