evdev.c 33 KB

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