evdev.c 28 KB

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