evdev.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. }
  456. out:
  457. mutex_unlock(&evdev->mutex);
  458. return retval;
  459. }
  460. static int evdev_fetch_next_event(struct evdev_client *client,
  461. struct input_event *event)
  462. {
  463. int have_event;
  464. spin_lock_irq(&client->buffer_lock);
  465. have_event = client->packet_head != client->tail;
  466. if (have_event) {
  467. *event = client->buffer[client->tail++];
  468. client->tail &= client->bufsize - 1;
  469. }
  470. spin_unlock_irq(&client->buffer_lock);
  471. return have_event;
  472. }
  473. static ssize_t evdev_read(struct file *file, char __user *buffer,
  474. size_t count, loff_t *ppos)
  475. {
  476. struct evdev_client *client = file->private_data;
  477. struct evdev *evdev = client->evdev;
  478. struct input_event event;
  479. size_t read = 0;
  480. int error;
  481. if (count != 0 && count < input_event_size())
  482. return -EINVAL;
  483. for (;;) {
  484. if (!evdev->exist || client->revoked)
  485. return -ENODEV;
  486. if (client->packet_head == client->tail &&
  487. (file->f_flags & O_NONBLOCK))
  488. return -EAGAIN;
  489. /*
  490. * count == 0 is special - no IO is done but we check
  491. * for error conditions (see above).
  492. */
  493. if (count == 0)
  494. break;
  495. while (read + input_event_size() <= count &&
  496. evdev_fetch_next_event(client, &event)) {
  497. if (input_event_to_user(buffer + read, &event))
  498. return -EFAULT;
  499. read += input_event_size();
  500. }
  501. if (read)
  502. break;
  503. if (!(file->f_flags & O_NONBLOCK)) {
  504. error = wait_event_interruptible(evdev->wait,
  505. client->packet_head != client->tail ||
  506. !evdev->exist || client->revoked);
  507. if (error)
  508. return error;
  509. }
  510. }
  511. return read;
  512. }
  513. /* No kernel lock - fine */
  514. static __poll_t evdev_poll(struct file *file, poll_table *wait)
  515. {
  516. struct evdev_client *client = file->private_data;
  517. struct evdev *evdev = client->evdev;
  518. __poll_t mask;
  519. poll_wait(file, &evdev->wait, wait);
  520. if (evdev->exist && !client->revoked)
  521. mask = EPOLLOUT | EPOLLWRNORM;
  522. else
  523. mask = EPOLLHUP | EPOLLERR;
  524. if (client->packet_head != client->tail)
  525. mask |= EPOLLIN | EPOLLRDNORM;
  526. return mask;
  527. }
  528. #ifdef CONFIG_COMPAT
  529. #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
  530. #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
  531. #ifdef __BIG_ENDIAN
  532. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  533. unsigned int maxlen, void __user *p, int compat)
  534. {
  535. int len, i;
  536. if (compat) {
  537. len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
  538. if (len > maxlen)
  539. len = maxlen;
  540. for (i = 0; i < len / sizeof(compat_long_t); i++)
  541. if (copy_to_user((compat_long_t __user *) p + i,
  542. (compat_long_t *) bits +
  543. i + 1 - ((i % 2) << 1),
  544. sizeof(compat_long_t)))
  545. return -EFAULT;
  546. } else {
  547. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  548. if (len > maxlen)
  549. len = maxlen;
  550. if (copy_to_user(p, bits, len))
  551. return -EFAULT;
  552. }
  553. return len;
  554. }
  555. static int bits_from_user(unsigned long *bits, unsigned int maxbit,
  556. unsigned int maxlen, const void __user *p, int compat)
  557. {
  558. int len, i;
  559. if (compat) {
  560. if (maxlen % sizeof(compat_long_t))
  561. return -EINVAL;
  562. len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
  563. if (len > maxlen)
  564. len = maxlen;
  565. for (i = 0; i < len / sizeof(compat_long_t); i++)
  566. if (copy_from_user((compat_long_t *) bits +
  567. i + 1 - ((i % 2) << 1),
  568. (compat_long_t __user *) p + i,
  569. sizeof(compat_long_t)))
  570. return -EFAULT;
  571. if (i % 2)
  572. *((compat_long_t *) bits + i - 1) = 0;
  573. } else {
  574. if (maxlen % sizeof(long))
  575. return -EINVAL;
  576. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  577. if (len > maxlen)
  578. len = maxlen;
  579. if (copy_from_user(bits, p, len))
  580. return -EFAULT;
  581. }
  582. return len;
  583. }
  584. #else
  585. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  586. unsigned int maxlen, void __user *p, int compat)
  587. {
  588. int len = compat ?
  589. BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
  590. BITS_TO_LONGS(maxbit) * sizeof(long);
  591. if (len > maxlen)
  592. len = maxlen;
  593. return copy_to_user(p, bits, len) ? -EFAULT : len;
  594. }
  595. static int bits_from_user(unsigned long *bits, unsigned int maxbit,
  596. unsigned int maxlen, const void __user *p, int compat)
  597. {
  598. size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
  599. int len;
  600. if (maxlen % chunk_size)
  601. return -EINVAL;
  602. len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
  603. len *= chunk_size;
  604. if (len > maxlen)
  605. len = maxlen;
  606. return copy_from_user(bits, p, len) ? -EFAULT : len;
  607. }
  608. #endif /* __BIG_ENDIAN */
  609. #else
  610. static int bits_to_user(unsigned long *bits, unsigned int maxbit,
  611. unsigned int maxlen, void __user *p, int compat)
  612. {
  613. int len = BITS_TO_LONGS(maxbit) * sizeof(long);
  614. if (len > maxlen)
  615. len = maxlen;
  616. return copy_to_user(p, bits, len) ? -EFAULT : len;
  617. }
  618. static int bits_from_user(unsigned long *bits, unsigned int maxbit,
  619. unsigned int maxlen, const void __user *p, int compat)
  620. {
  621. int len;
  622. if (maxlen % sizeof(long))
  623. return -EINVAL;
  624. len = BITS_TO_LONGS(maxbit) * sizeof(long);
  625. if (len > maxlen)
  626. len = maxlen;
  627. return copy_from_user(bits, p, len) ? -EFAULT : len;
  628. }
  629. #endif /* CONFIG_COMPAT */
  630. static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
  631. {
  632. int len;
  633. if (!str)
  634. return -ENOENT;
  635. len = strlen(str) + 1;
  636. if (len > maxlen)
  637. len = maxlen;
  638. return copy_to_user(p, str, len) ? -EFAULT : len;
  639. }
  640. static int handle_eviocgbit(struct input_dev *dev,
  641. unsigned int type, unsigned int size,
  642. void __user *p, int compat_mode)
  643. {
  644. unsigned long *bits;
  645. int len;
  646. switch (type) {
  647. case 0: bits = dev->evbit; len = EV_MAX; break;
  648. case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
  649. case EV_REL: bits = dev->relbit; len = REL_MAX; break;
  650. case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
  651. case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
  652. case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
  653. case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
  654. case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
  655. case EV_SW: bits = dev->swbit; len = SW_MAX; break;
  656. default: return -EINVAL;
  657. }
  658. return bits_to_user(bits, len, size, p, compat_mode);
  659. }
  660. static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
  661. {
  662. struct input_keymap_entry ke = {
  663. .len = sizeof(unsigned int),
  664. .flags = 0,
  665. };
  666. int __user *ip = (int __user *)p;
  667. int error;
  668. /* legacy case */
  669. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  670. return -EFAULT;
  671. error = input_get_keycode(dev, &ke);
  672. if (error)
  673. return error;
  674. if (put_user(ke.keycode, ip + 1))
  675. return -EFAULT;
  676. return 0;
  677. }
  678. static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
  679. {
  680. struct input_keymap_entry ke;
  681. int error;
  682. if (copy_from_user(&ke, p, sizeof(ke)))
  683. return -EFAULT;
  684. error = input_get_keycode(dev, &ke);
  685. if (error)
  686. return error;
  687. if (copy_to_user(p, &ke, sizeof(ke)))
  688. return -EFAULT;
  689. return 0;
  690. }
  691. static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
  692. {
  693. struct input_keymap_entry ke = {
  694. .len = sizeof(unsigned int),
  695. .flags = 0,
  696. };
  697. int __user *ip = (int __user *)p;
  698. if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
  699. return -EFAULT;
  700. if (get_user(ke.keycode, ip + 1))
  701. return -EFAULT;
  702. return input_set_keycode(dev, &ke);
  703. }
  704. static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
  705. {
  706. struct input_keymap_entry ke;
  707. if (copy_from_user(&ke, p, sizeof(ke)))
  708. return -EFAULT;
  709. if (ke.len > sizeof(ke.scancode))
  710. return -EINVAL;
  711. return input_set_keycode(dev, &ke);
  712. }
  713. /*
  714. * If we transfer state to the user, we should flush all pending events
  715. * of the same type from the client's queue. Otherwise, they might end up
  716. * with duplicate events, which can screw up client's state tracking.
  717. * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
  718. * event so user-space will notice missing events.
  719. *
  720. * LOCKING:
  721. * We need to take event_lock before buffer_lock to avoid dead-locks. But we
  722. * need the even_lock only to guarantee consistent state. We can safely release
  723. * it while flushing the queue. This allows input-core to handle filters while
  724. * we flush the queue.
  725. */
  726. static int evdev_handle_get_val(struct evdev_client *client,
  727. struct input_dev *dev, unsigned int type,
  728. unsigned long *bits, unsigned int maxbit,
  729. unsigned int maxlen, void __user *p,
  730. int compat)
  731. {
  732. int ret;
  733. unsigned long *mem;
  734. mem = bitmap_alloc(maxbit, GFP_KERNEL);
  735. if (!mem)
  736. return -ENOMEM;
  737. spin_lock_irq(&dev->event_lock);
  738. spin_lock(&client->buffer_lock);
  739. bitmap_copy(mem, bits, maxbit);
  740. spin_unlock(&dev->event_lock);
  741. __evdev_flush_queue(client, type);
  742. spin_unlock_irq(&client->buffer_lock);
  743. ret = bits_to_user(mem, maxbit, maxlen, p, compat);
  744. if (ret < 0)
  745. evdev_queue_syn_dropped(client);
  746. bitmap_free(mem);
  747. return ret;
  748. }
  749. static int evdev_handle_mt_request(struct input_dev *dev,
  750. unsigned int size,
  751. int __user *ip)
  752. {
  753. const struct input_mt *mt = dev->mt;
  754. unsigned int code;
  755. int max_slots;
  756. int i;
  757. if (get_user(code, &ip[0]))
  758. return -EFAULT;
  759. if (!mt || !input_is_mt_value(code))
  760. return -EINVAL;
  761. max_slots = (size - sizeof(__u32)) / sizeof(__s32);
  762. for (i = 0; i < mt->num_slots && i < max_slots; i++) {
  763. int value = input_mt_get_value(&mt->slots[i], code);
  764. if (put_user(value, &ip[1 + i]))
  765. return -EFAULT;
  766. }
  767. return 0;
  768. }
  769. static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
  770. struct file *file)
  771. {
  772. client->revoked = true;
  773. evdev_ungrab(evdev, client);
  774. input_flush_device(&evdev->handle, file);
  775. wake_up_interruptible(&evdev->wait);
  776. return 0;
  777. }
  778. /* must be called with evdev-mutex held */
  779. static int evdev_set_mask(struct evdev_client *client,
  780. unsigned int type,
  781. const void __user *codes,
  782. u32 codes_size,
  783. int compat)
  784. {
  785. unsigned long flags, *mask, *oldmask;
  786. size_t cnt;
  787. int error;
  788. /* we allow unknown types and 'codes_size > size' for forward-compat */
  789. cnt = evdev_get_mask_cnt(type);
  790. if (!cnt)
  791. return 0;
  792. mask = bitmap_zalloc(cnt, GFP_KERNEL);
  793. if (!mask)
  794. return -ENOMEM;
  795. error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
  796. if (error < 0) {
  797. bitmap_free(mask);
  798. return error;
  799. }
  800. spin_lock_irqsave(&client->buffer_lock, flags);
  801. oldmask = client->evmasks[type];
  802. client->evmasks[type] = mask;
  803. spin_unlock_irqrestore(&client->buffer_lock, flags);
  804. bitmap_free(oldmask);
  805. return 0;
  806. }
  807. /* must be called with evdev-mutex held */
  808. static int evdev_get_mask(struct evdev_client *client,
  809. unsigned int type,
  810. void __user *codes,
  811. u32 codes_size,
  812. int compat)
  813. {
  814. unsigned long *mask;
  815. size_t cnt, size, xfer_size;
  816. int i;
  817. int error;
  818. /* we allow unknown types and 'codes_size > size' for forward-compat */
  819. cnt = evdev_get_mask_cnt(type);
  820. size = sizeof(unsigned long) * BITS_TO_LONGS(cnt);
  821. xfer_size = min_t(size_t, codes_size, size);
  822. if (cnt > 0) {
  823. mask = client->evmasks[type];
  824. if (mask) {
  825. error = bits_to_user(mask, cnt - 1,
  826. xfer_size, codes, compat);
  827. if (error < 0)
  828. return error;
  829. } else {
  830. /* fake mask with all bits set */
  831. for (i = 0; i < xfer_size; i++)
  832. if (put_user(0xffU, (u8 __user *)codes + i))
  833. return -EFAULT;
  834. }
  835. }
  836. if (xfer_size < codes_size)
  837. if (clear_user(codes + xfer_size, codes_size - xfer_size))
  838. return -EFAULT;
  839. return 0;
  840. }
  841. static long evdev_do_ioctl(struct file *file, unsigned int cmd,
  842. void __user *p, int compat_mode)
  843. {
  844. struct evdev_client *client = file->private_data;
  845. struct evdev *evdev = client->evdev;
  846. struct input_dev *dev = evdev->handle.dev;
  847. struct input_absinfo abs;
  848. struct input_mask mask;
  849. struct ff_effect effect;
  850. int __user *ip = (int __user *)p;
  851. unsigned int i, t, u, v;
  852. unsigned int size;
  853. int error;
  854. /* First we check for fixed-length commands */
  855. switch (cmd) {
  856. case EVIOCGVERSION:
  857. return put_user(EV_VERSION, ip);
  858. case EVIOCGID:
  859. if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
  860. return -EFAULT;
  861. return 0;
  862. case EVIOCGREP:
  863. if (!test_bit(EV_REP, dev->evbit))
  864. return -ENOSYS;
  865. if (put_user(dev->rep[REP_DELAY], ip))
  866. return -EFAULT;
  867. if (put_user(dev->rep[REP_PERIOD], ip + 1))
  868. return -EFAULT;
  869. return 0;
  870. case EVIOCSREP:
  871. if (!test_bit(EV_REP, dev->evbit))
  872. return -ENOSYS;
  873. if (get_user(u, ip))
  874. return -EFAULT;
  875. if (get_user(v, ip + 1))
  876. return -EFAULT;
  877. input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
  878. input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
  879. return 0;
  880. case EVIOCRMFF:
  881. return input_ff_erase(dev, (int)(unsigned long) p, file);
  882. case EVIOCGEFFECTS:
  883. i = test_bit(EV_FF, dev->evbit) ?
  884. dev->ff->max_effects : 0;
  885. if (put_user(i, ip))
  886. return -EFAULT;
  887. return 0;
  888. case EVIOCGRAB:
  889. if (p)
  890. return evdev_grab(evdev, client);
  891. else
  892. return evdev_ungrab(evdev, client);
  893. case EVIOCREVOKE:
  894. if (p)
  895. return -EINVAL;
  896. else
  897. return evdev_revoke(evdev, client, file);
  898. case EVIOCGMASK: {
  899. void __user *codes_ptr;
  900. if (copy_from_user(&mask, p, sizeof(mask)))
  901. return -EFAULT;
  902. codes_ptr = (void __user *)(unsigned long)mask.codes_ptr;
  903. return evdev_get_mask(client,
  904. mask.type, codes_ptr, mask.codes_size,
  905. compat_mode);
  906. }
  907. case EVIOCSMASK: {
  908. const void __user *codes_ptr;
  909. if (copy_from_user(&mask, p, sizeof(mask)))
  910. return -EFAULT;
  911. codes_ptr = (const void __user *)(unsigned long)mask.codes_ptr;
  912. return evdev_set_mask(client,
  913. mask.type, codes_ptr, mask.codes_size,
  914. compat_mode);
  915. }
  916. case EVIOCSCLOCKID:
  917. if (copy_from_user(&i, p, sizeof(unsigned int)))
  918. return -EFAULT;
  919. return evdev_set_clk_type(client, i);
  920. case EVIOCGKEYCODE:
  921. return evdev_handle_get_keycode(dev, p);
  922. case EVIOCSKEYCODE:
  923. return evdev_handle_set_keycode(dev, p);
  924. case EVIOCGKEYCODE_V2:
  925. return evdev_handle_get_keycode_v2(dev, p);
  926. case EVIOCSKEYCODE_V2:
  927. return evdev_handle_set_keycode_v2(dev, p);
  928. }
  929. size = _IOC_SIZE(cmd);
  930. /* Now check variable-length commands */
  931. #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
  932. switch (EVIOC_MASK_SIZE(cmd)) {
  933. case EVIOCGPROP(0):
  934. return bits_to_user(dev->propbit, INPUT_PROP_MAX,
  935. size, p, compat_mode);
  936. case EVIOCGMTSLOTS(0):
  937. return evdev_handle_mt_request(dev, size, ip);
  938. case EVIOCGKEY(0):
  939. return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
  940. KEY_MAX, size, p, compat_mode);
  941. case EVIOCGLED(0):
  942. return evdev_handle_get_val(client, dev, EV_LED, dev->led,
  943. LED_MAX, size, p, compat_mode);
  944. case EVIOCGSND(0):
  945. return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
  946. SND_MAX, size, p, compat_mode);
  947. case EVIOCGSW(0):
  948. return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
  949. SW_MAX, size, p, compat_mode);
  950. case EVIOCGNAME(0):
  951. return str_to_user(dev->name, size, p);
  952. case EVIOCGPHYS(0):
  953. return str_to_user(dev->phys, size, p);
  954. case EVIOCGUNIQ(0):
  955. return str_to_user(dev->uniq, size, p);
  956. case EVIOC_MASK_SIZE(EVIOCSFF):
  957. if (input_ff_effect_from_user(p, size, &effect))
  958. return -EFAULT;
  959. error = input_ff_upload(dev, &effect, file);
  960. if (error)
  961. return error;
  962. if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
  963. return -EFAULT;
  964. return 0;
  965. }
  966. /* Multi-number variable-length handlers */
  967. if (_IOC_TYPE(cmd) != 'E')
  968. return -EINVAL;
  969. if (_IOC_DIR(cmd) == _IOC_READ) {
  970. if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
  971. return handle_eviocgbit(dev,
  972. _IOC_NR(cmd) & EV_MAX, size,
  973. p, compat_mode);
  974. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
  975. if (!dev->absinfo)
  976. return -EINVAL;
  977. t = _IOC_NR(cmd) & ABS_MAX;
  978. abs = dev->absinfo[t];
  979. if (copy_to_user(p, &abs, min_t(size_t,
  980. size, sizeof(struct input_absinfo))))
  981. return -EFAULT;
  982. return 0;
  983. }
  984. }
  985. if (_IOC_DIR(cmd) == _IOC_WRITE) {
  986. if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
  987. if (!dev->absinfo)
  988. return -EINVAL;
  989. t = _IOC_NR(cmd) & ABS_MAX;
  990. if (copy_from_user(&abs, p, min_t(size_t,
  991. size, sizeof(struct input_absinfo))))
  992. return -EFAULT;
  993. if (size < sizeof(struct input_absinfo))
  994. abs.resolution = 0;
  995. /* We can't change number of reserved MT slots */
  996. if (t == ABS_MT_SLOT)
  997. return -EINVAL;
  998. /*
  999. * Take event lock to ensure that we are not
  1000. * changing device parameters in the middle
  1001. * of event.
  1002. */
  1003. spin_lock_irq(&dev->event_lock);
  1004. dev->absinfo[t] = abs;
  1005. spin_unlock_irq(&dev->event_lock);
  1006. return 0;
  1007. }
  1008. }
  1009. return -EINVAL;
  1010. }
  1011. static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
  1012. void __user *p, int compat_mode)
  1013. {
  1014. struct evdev_client *client = file->private_data;
  1015. struct evdev *evdev = client->evdev;
  1016. int retval;
  1017. retval = mutex_lock_interruptible(&evdev->mutex);
  1018. if (retval)
  1019. return retval;
  1020. if (!evdev->exist || client->revoked) {
  1021. retval = -ENODEV;
  1022. goto out;
  1023. }
  1024. retval = evdev_do_ioctl(file, cmd, p, compat_mode);
  1025. out:
  1026. mutex_unlock(&evdev->mutex);
  1027. return retval;
  1028. }
  1029. static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1030. {
  1031. return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
  1032. }
  1033. #ifdef CONFIG_COMPAT
  1034. static long evdev_ioctl_compat(struct file *file,
  1035. unsigned int cmd, unsigned long arg)
  1036. {
  1037. return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
  1038. }
  1039. #endif
  1040. static const struct file_operations evdev_fops = {
  1041. .owner = THIS_MODULE,
  1042. .read = evdev_read,
  1043. .write = evdev_write,
  1044. .poll = evdev_poll,
  1045. .open = evdev_open,
  1046. .release = evdev_release,
  1047. .unlocked_ioctl = evdev_ioctl,
  1048. #ifdef CONFIG_COMPAT
  1049. .compat_ioctl = evdev_ioctl_compat,
  1050. #endif
  1051. .fasync = evdev_fasync,
  1052. .flush = evdev_flush,
  1053. .llseek = no_llseek,
  1054. };
  1055. /*
  1056. * Mark device non-existent. This disables writes, ioctls and
  1057. * prevents new users from opening the device. Already posted
  1058. * blocking reads will stay, however new ones will fail.
  1059. */
  1060. static void evdev_mark_dead(struct evdev *evdev)
  1061. {
  1062. mutex_lock(&evdev->mutex);
  1063. evdev->exist = false;
  1064. mutex_unlock(&evdev->mutex);
  1065. }
  1066. static void evdev_cleanup(struct evdev *evdev)
  1067. {
  1068. struct input_handle *handle = &evdev->handle;
  1069. evdev_mark_dead(evdev);
  1070. evdev_hangup(evdev);
  1071. /* evdev is marked dead so no one else accesses evdev->open */
  1072. if (evdev->open) {
  1073. input_flush_device(handle, NULL);
  1074. input_close_device(handle);
  1075. }
  1076. }
  1077. /*
  1078. * Create new evdev device. Note that input core serializes calls
  1079. * to connect and disconnect.
  1080. */
  1081. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
  1082. const struct input_device_id *id)
  1083. {
  1084. struct evdev *evdev;
  1085. int minor;
  1086. int dev_no;
  1087. int error;
  1088. minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
  1089. if (minor < 0) {
  1090. error = minor;
  1091. pr_err("failed to reserve new minor: %d\n", error);
  1092. return error;
  1093. }
  1094. evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
  1095. if (!evdev) {
  1096. error = -ENOMEM;
  1097. goto err_free_minor;
  1098. }
  1099. INIT_LIST_HEAD(&evdev->client_list);
  1100. spin_lock_init(&evdev->client_lock);
  1101. mutex_init(&evdev->mutex);
  1102. init_waitqueue_head(&evdev->wait);
  1103. evdev->exist = true;
  1104. dev_no = minor;
  1105. /* Normalize device number if it falls into legacy range */
  1106. if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
  1107. dev_no -= EVDEV_MINOR_BASE;
  1108. dev_set_name(&evdev->dev, "event%d", dev_no);
  1109. evdev->handle.dev = input_get_device(dev);
  1110. evdev->handle.name = dev_name(&evdev->dev);
  1111. evdev->handle.handler = handler;
  1112. evdev->handle.private = evdev;
  1113. evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  1114. evdev->dev.class = &input_class;
  1115. evdev->dev.parent = &dev->dev;
  1116. evdev->dev.release = evdev_free;
  1117. device_initialize(&evdev->dev);
  1118. error = input_register_handle(&evdev->handle);
  1119. if (error)
  1120. goto err_free_evdev;
  1121. cdev_init(&evdev->cdev, &evdev_fops);
  1122. error = cdev_device_add(&evdev->cdev, &evdev->dev);
  1123. if (error)
  1124. goto err_cleanup_evdev;
  1125. return 0;
  1126. err_cleanup_evdev:
  1127. evdev_cleanup(evdev);
  1128. input_unregister_handle(&evdev->handle);
  1129. err_free_evdev:
  1130. put_device(&evdev->dev);
  1131. err_free_minor:
  1132. input_free_minor(minor);
  1133. return error;
  1134. }
  1135. static void evdev_disconnect(struct input_handle *handle)
  1136. {
  1137. struct evdev *evdev = handle->private;
  1138. cdev_device_del(&evdev->cdev, &evdev->dev);
  1139. evdev_cleanup(evdev);
  1140. input_free_minor(MINOR(evdev->dev.devt));
  1141. input_unregister_handle(handle);
  1142. put_device(&evdev->dev);
  1143. }
  1144. static const struct input_device_id evdev_ids[] = {
  1145. { .driver_info = 1 }, /* Matches all devices */
  1146. { }, /* Terminating zero entry */
  1147. };
  1148. MODULE_DEVICE_TABLE(input, evdev_ids);
  1149. static struct input_handler evdev_handler = {
  1150. .event = evdev_event,
  1151. .events = evdev_events,
  1152. .connect = evdev_connect,
  1153. .disconnect = evdev_disconnect,
  1154. .legacy_minors = true,
  1155. .minor = EVDEV_MINOR_BASE,
  1156. .name = "evdev",
  1157. .id_table = evdev_ids,
  1158. };
  1159. static int __init evdev_init(void)
  1160. {
  1161. return input_register_handler(&evdev_handler);
  1162. }
  1163. static void __exit evdev_exit(void)
  1164. {
  1165. input_unregister_handler(&evdev_handler);
  1166. }
  1167. module_init(evdev_init);
  1168. module_exit(evdev_exit);
  1169. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  1170. MODULE_DESCRIPTION("Input driver event char devices");
  1171. MODULE_LICENSE("GPL");