evdev.c 33 KB

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