evdev.c 27 KB

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