evdev.c 28 KB

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