evdev.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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");