input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * The input core
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/input.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/major.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/kobject_uevent.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/poll.h>
  22. #include <linux/device.h>
  23. #include <linux/devfs_fs_kernel.h>
  24. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  25. MODULE_DESCRIPTION("Input core");
  26. MODULE_LICENSE("GPL");
  27. EXPORT_SYMBOL(input_register_device);
  28. EXPORT_SYMBOL(input_unregister_device);
  29. EXPORT_SYMBOL(input_register_handler);
  30. EXPORT_SYMBOL(input_unregister_handler);
  31. EXPORT_SYMBOL(input_grab_device);
  32. EXPORT_SYMBOL(input_release_device);
  33. EXPORT_SYMBOL(input_open_device);
  34. EXPORT_SYMBOL(input_close_device);
  35. EXPORT_SYMBOL(input_accept_process);
  36. EXPORT_SYMBOL(input_flush_device);
  37. EXPORT_SYMBOL(input_event);
  38. EXPORT_SYMBOL(input_class);
  39. #define INPUT_DEVICES 256
  40. static LIST_HEAD(input_dev_list);
  41. static LIST_HEAD(input_handler_list);
  42. static struct input_handler *input_table[8];
  43. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  44. {
  45. struct input_handle *handle;
  46. if (type > EV_MAX || !test_bit(type, dev->evbit))
  47. return;
  48. add_input_randomness(type, code, value);
  49. switch (type) {
  50. case EV_SYN:
  51. switch (code) {
  52. case SYN_CONFIG:
  53. if (dev->event) dev->event(dev, type, code, value);
  54. break;
  55. case SYN_REPORT:
  56. if (dev->sync) return;
  57. dev->sync = 1;
  58. break;
  59. }
  60. break;
  61. case EV_KEY:
  62. if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  63. return;
  64. if (value == 2)
  65. break;
  66. change_bit(code, dev->key);
  67. if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
  68. dev->repeat_key = code;
  69. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  70. }
  71. break;
  72. case EV_SW:
  73. if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
  74. return;
  75. change_bit(code, dev->sw);
  76. break;
  77. case EV_ABS:
  78. if (code > ABS_MAX || !test_bit(code, dev->absbit))
  79. return;
  80. if (dev->absfuzz[code]) {
  81. if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
  82. (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
  83. return;
  84. if ((value > dev->abs[code] - dev->absfuzz[code]) &&
  85. (value < dev->abs[code] + dev->absfuzz[code]))
  86. value = (dev->abs[code] * 3 + value) >> 2;
  87. if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
  88. (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
  89. value = (dev->abs[code] + value) >> 1;
  90. }
  91. if (dev->abs[code] == value)
  92. return;
  93. dev->abs[code] = value;
  94. break;
  95. case EV_REL:
  96. if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
  97. return;
  98. break;
  99. case EV_MSC:
  100. if (code > MSC_MAX || !test_bit(code, dev->mscbit))
  101. return;
  102. if (dev->event) dev->event(dev, type, code, value);
  103. break;
  104. case EV_LED:
  105. if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
  106. return;
  107. change_bit(code, dev->led);
  108. if (dev->event) dev->event(dev, type, code, value);
  109. break;
  110. case EV_SND:
  111. if (code > SND_MAX || !test_bit(code, dev->sndbit))
  112. return;
  113. if (dev->event) dev->event(dev, type, code, value);
  114. break;
  115. case EV_REP:
  116. if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
  117. dev->rep[code] = value;
  118. if (dev->event) dev->event(dev, type, code, value);
  119. break;
  120. case EV_FF:
  121. if (dev->event) dev->event(dev, type, code, value);
  122. break;
  123. }
  124. if (type != EV_SYN)
  125. dev->sync = 0;
  126. if (dev->grab)
  127. dev->grab->handler->event(dev->grab, type, code, value);
  128. else
  129. list_for_each_entry(handle, &dev->h_list, d_node)
  130. if (handle->open)
  131. handle->handler->event(handle, type, code, value);
  132. }
  133. static void input_repeat_key(unsigned long data)
  134. {
  135. struct input_dev *dev = (void *) data;
  136. if (!test_bit(dev->repeat_key, dev->key))
  137. return;
  138. input_event(dev, EV_KEY, dev->repeat_key, 2);
  139. input_sync(dev);
  140. if (dev->rep[REP_PERIOD])
  141. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
  142. }
  143. int input_accept_process(struct input_handle *handle, struct file *file)
  144. {
  145. if (handle->dev->accept)
  146. return handle->dev->accept(handle->dev, file);
  147. return 0;
  148. }
  149. int input_grab_device(struct input_handle *handle)
  150. {
  151. if (handle->dev->grab)
  152. return -EBUSY;
  153. handle->dev->grab = handle;
  154. return 0;
  155. }
  156. void input_release_device(struct input_handle *handle)
  157. {
  158. if (handle->dev->grab == handle)
  159. handle->dev->grab = NULL;
  160. }
  161. int input_open_device(struct input_handle *handle)
  162. {
  163. struct input_dev *dev = handle->dev;
  164. int err;
  165. err = down_interruptible(&dev->sem);
  166. if (err)
  167. return err;
  168. handle->open++;
  169. if (!dev->users++ && dev->open)
  170. err = dev->open(dev);
  171. if (err)
  172. handle->open--;
  173. up(&dev->sem);
  174. return err;
  175. }
  176. int input_flush_device(struct input_handle* handle, struct file* file)
  177. {
  178. if (handle->dev->flush)
  179. return handle->dev->flush(handle->dev, file);
  180. return 0;
  181. }
  182. void input_close_device(struct input_handle *handle)
  183. {
  184. struct input_dev *dev = handle->dev;
  185. input_release_device(handle);
  186. down(&dev->sem);
  187. if (!--dev->users && dev->close)
  188. dev->close(dev);
  189. handle->open--;
  190. up(&dev->sem);
  191. }
  192. static void input_link_handle(struct input_handle *handle)
  193. {
  194. list_add_tail(&handle->d_node, &handle->dev->h_list);
  195. list_add_tail(&handle->h_node, &handle->handler->h_list);
  196. }
  197. #define MATCH_BIT(bit, max) \
  198. for (i = 0; i < NBITS(max); i++) \
  199. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  200. break; \
  201. if (i != NBITS(max)) \
  202. continue;
  203. static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
  204. {
  205. int i;
  206. for (; id->flags || id->driver_info; id++) {
  207. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  208. if (id->id.bustype != dev->id.bustype)
  209. continue;
  210. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  211. if (id->id.vendor != dev->id.vendor)
  212. continue;
  213. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  214. if (id->id.product != dev->id.product)
  215. continue;
  216. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  217. if (id->id.version != dev->id.version)
  218. continue;
  219. MATCH_BIT(evbit, EV_MAX);
  220. MATCH_BIT(keybit, KEY_MAX);
  221. MATCH_BIT(relbit, REL_MAX);
  222. MATCH_BIT(absbit, ABS_MAX);
  223. MATCH_BIT(mscbit, MSC_MAX);
  224. MATCH_BIT(ledbit, LED_MAX);
  225. MATCH_BIT(sndbit, SND_MAX);
  226. MATCH_BIT(ffbit, FF_MAX);
  227. MATCH_BIT(swbit, SW_MAX);
  228. return id;
  229. }
  230. return NULL;
  231. }
  232. /*
  233. * Input hotplugging interface - loading event handlers based on
  234. * device bitfields.
  235. */
  236. #ifdef CONFIG_HOTPLUG
  237. /*
  238. * Input hotplugging invokes what /proc/sys/kernel/hotplug says
  239. * (normally /sbin/hotplug) when input devices get added or removed.
  240. *
  241. * This invokes a user mode policy agent, typically helping to load driver
  242. * or other modules, configure the device, and more. Drivers can provide
  243. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  244. *
  245. */
  246. #define SPRINTF_BIT_A(bit, name, max) \
  247. do { \
  248. envp[i++] = scratch; \
  249. scratch += sprintf(scratch, name); \
  250. for (j = NBITS(max) - 1; j >= 0; j--) \
  251. if (dev->bit[j]) break; \
  252. for (; j >= 0; j--) \
  253. scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
  254. scratch++; \
  255. } while (0)
  256. #define SPRINTF_BIT_A2(bit, name, max, ev) \
  257. do { \
  258. if (test_bit(ev, dev->evbit)) \
  259. SPRINTF_BIT_A(bit, name, max); \
  260. } while (0)
  261. static void input_call_hotplug(char *verb, struct input_dev *dev)
  262. {
  263. char *argv[3], **envp, *buf, *scratch;
  264. int i = 0, j, value;
  265. if (!hotplug_path[0]) {
  266. printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
  267. return;
  268. }
  269. if (in_interrupt()) {
  270. printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
  271. return;
  272. }
  273. if (!current->fs->root) {
  274. printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
  275. return;
  276. }
  277. if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
  278. printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
  279. return;
  280. }
  281. if (!(buf = kmalloc(1024, GFP_KERNEL))) {
  282. kfree (envp);
  283. printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
  284. return;
  285. }
  286. argv[0] = hotplug_path;
  287. argv[1] = "input";
  288. argv[2] = NULL;
  289. envp[i++] = "HOME=/";
  290. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  291. scratch = buf;
  292. envp[i++] = scratch;
  293. scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
  294. envp[i++] = scratch;
  295. scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
  296. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
  297. if (dev->name) {
  298. envp[i++] = scratch;
  299. scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
  300. }
  301. if (dev->phys) {
  302. envp[i++] = scratch;
  303. scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
  304. }
  305. SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
  306. SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
  307. SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
  308. SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
  309. SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
  310. SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
  311. SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
  312. SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
  313. SPRINTF_BIT_A2(swbit, "SW=", SW_MAX, EV_SW);
  314. envp[i++] = NULL;
  315. #ifdef INPUT_DEBUG
  316. printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
  317. argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
  318. #endif
  319. value = call_usermodehelper(argv [0], argv, envp, 0);
  320. kfree(buf);
  321. kfree(envp);
  322. #ifdef INPUT_DEBUG
  323. if (value != 0)
  324. printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
  325. #endif
  326. }
  327. #endif
  328. #ifdef CONFIG_PROC_FS
  329. static struct proc_dir_entry *proc_bus_input_dir;
  330. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  331. static int input_devices_state;
  332. static inline void input_wakeup_procfs_readers(void)
  333. {
  334. input_devices_state++;
  335. wake_up(&input_devices_poll_wait);
  336. }
  337. static unsigned int input_devices_poll(struct file *file, poll_table *wait)
  338. {
  339. int state = input_devices_state;
  340. poll_wait(file, &input_devices_poll_wait, wait);
  341. if (state != input_devices_state)
  342. return POLLIN | POLLRDNORM;
  343. return 0;
  344. }
  345. #define SPRINTF_BIT_B(bit, name, max) \
  346. do { \
  347. len += sprintf(buf + len, "B: %s", name); \
  348. for (i = NBITS(max) - 1; i >= 0; i--) \
  349. if (dev->bit[i]) break; \
  350. for (; i >= 0; i--) \
  351. len += sprintf(buf + len, "%lx ", dev->bit[i]); \
  352. len += sprintf(buf + len, "\n"); \
  353. } while (0)
  354. #define SPRINTF_BIT_B2(bit, name, max, ev) \
  355. do { \
  356. if (test_bit(ev, dev->evbit)) \
  357. SPRINTF_BIT_B(bit, name, max); \
  358. } while (0)
  359. static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  360. {
  361. struct input_dev *dev;
  362. struct input_handle *handle;
  363. off_t at = 0;
  364. int i, len, cnt = 0;
  365. list_for_each_entry(dev, &input_dev_list, node) {
  366. len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  367. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  368. len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  369. len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  370. len += sprintf(buf + len, "H: Handlers=");
  371. list_for_each_entry(handle, &dev->h_list, d_node)
  372. len += sprintf(buf + len, "%s ", handle->name);
  373. len += sprintf(buf + len, "\n");
  374. SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
  375. SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
  376. SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
  377. SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
  378. SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
  379. SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
  380. SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
  381. SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF);
  382. SPRINTF_BIT_B2(swbit, "SW=", SW_MAX, EV_SW);
  383. len += sprintf(buf + len, "\n");
  384. at += len;
  385. if (at >= pos) {
  386. if (!*start) {
  387. *start = buf + (pos - (at - len));
  388. cnt = at - pos;
  389. } else cnt += len;
  390. buf += len;
  391. if (cnt >= count)
  392. break;
  393. }
  394. }
  395. if (&dev->node == &input_dev_list)
  396. *eof = 1;
  397. return (count > cnt) ? cnt : count;
  398. }
  399. static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  400. {
  401. struct input_handler *handler;
  402. off_t at = 0;
  403. int len = 0, cnt = 0;
  404. int i = 0;
  405. list_for_each_entry(handler, &input_handler_list, node) {
  406. if (handler->fops)
  407. len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
  408. i++, handler->name, handler->minor);
  409. else
  410. len = sprintf(buf, "N: Number=%d Name=%s\n",
  411. i++, handler->name);
  412. at += len;
  413. if (at >= pos) {
  414. if (!*start) {
  415. *start = buf + (pos - (at - len));
  416. cnt = at - pos;
  417. } else cnt += len;
  418. buf += len;
  419. if (cnt >= count)
  420. break;
  421. }
  422. }
  423. if (&handler->node == &input_handler_list)
  424. *eof = 1;
  425. return (count > cnt) ? cnt : count;
  426. }
  427. static struct file_operations input_fileops;
  428. static int __init input_proc_init(void)
  429. {
  430. struct proc_dir_entry *entry;
  431. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  432. if (!proc_bus_input_dir)
  433. return -ENOMEM;
  434. proc_bus_input_dir->owner = THIS_MODULE;
  435. entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
  436. if (!entry)
  437. goto fail1;
  438. entry->owner = THIS_MODULE;
  439. input_fileops = *entry->proc_fops;
  440. entry->proc_fops = &input_fileops;
  441. entry->proc_fops->poll = input_devices_poll;
  442. entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
  443. if (!entry)
  444. goto fail2;
  445. entry->owner = THIS_MODULE;
  446. return 0;
  447. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  448. fail1: remove_proc_entry("input", proc_bus);
  449. return -ENOMEM;
  450. }
  451. static void input_proc_exit(void)
  452. {
  453. remove_proc_entry("devices", proc_bus_input_dir);
  454. remove_proc_entry("handlers", proc_bus_input_dir);
  455. remove_proc_entry("input", proc_bus);
  456. }
  457. #else /* !CONFIG_PROC_FS */
  458. static inline void input_wakeup_procfs_readers(void) { }
  459. static inline int input_proc_init(void) { return 0; }
  460. static inline void input_proc_exit(void) { }
  461. #endif
  462. void input_register_device(struct input_dev *dev)
  463. {
  464. struct input_handle *handle;
  465. struct input_handler *handler;
  466. struct input_device_id *id;
  467. set_bit(EV_SYN, dev->evbit);
  468. init_MUTEX(&dev->sem);
  469. /*
  470. * If delay and period are pre-set by the driver, then autorepeating
  471. * is handled by the driver itself and we don't do it in input.c.
  472. */
  473. init_timer(&dev->timer);
  474. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  475. dev->timer.data = (long) dev;
  476. dev->timer.function = input_repeat_key;
  477. dev->rep[REP_DELAY] = 250;
  478. dev->rep[REP_PERIOD] = 33;
  479. }
  480. INIT_LIST_HEAD(&dev->h_list);
  481. list_add_tail(&dev->node, &input_dev_list);
  482. list_for_each_entry(handler, &input_handler_list, node)
  483. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  484. if ((id = input_match_device(handler->id_table, dev)))
  485. if ((handle = handler->connect(handler, dev, id)))
  486. input_link_handle(handle);
  487. #ifdef CONFIG_HOTPLUG
  488. input_call_hotplug("add", dev);
  489. #endif
  490. input_wakeup_procfs_readers();
  491. }
  492. void input_unregister_device(struct input_dev *dev)
  493. {
  494. struct list_head * node, * next;
  495. if (!dev) return;
  496. del_timer_sync(&dev->timer);
  497. list_for_each_safe(node, next, &dev->h_list) {
  498. struct input_handle * handle = to_handle(node);
  499. list_del_init(&handle->d_node);
  500. list_del_init(&handle->h_node);
  501. handle->handler->disconnect(handle);
  502. }
  503. #ifdef CONFIG_HOTPLUG
  504. input_call_hotplug("remove", dev);
  505. #endif
  506. list_del_init(&dev->node);
  507. input_wakeup_procfs_readers();
  508. }
  509. void input_register_handler(struct input_handler *handler)
  510. {
  511. struct input_dev *dev;
  512. struct input_handle *handle;
  513. struct input_device_id *id;
  514. if (!handler) return;
  515. INIT_LIST_HEAD(&handler->h_list);
  516. if (handler->fops != NULL)
  517. input_table[handler->minor >> 5] = handler;
  518. list_add_tail(&handler->node, &input_handler_list);
  519. list_for_each_entry(dev, &input_dev_list, node)
  520. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  521. if ((id = input_match_device(handler->id_table, dev)))
  522. if ((handle = handler->connect(handler, dev, id)))
  523. input_link_handle(handle);
  524. input_wakeup_procfs_readers();
  525. }
  526. void input_unregister_handler(struct input_handler *handler)
  527. {
  528. struct list_head * node, * next;
  529. list_for_each_safe(node, next, &handler->h_list) {
  530. struct input_handle * handle = to_handle_h(node);
  531. list_del_init(&handle->h_node);
  532. list_del_init(&handle->d_node);
  533. handler->disconnect(handle);
  534. }
  535. list_del_init(&handler->node);
  536. if (handler->fops != NULL)
  537. input_table[handler->minor >> 5] = NULL;
  538. input_wakeup_procfs_readers();
  539. }
  540. static int input_open_file(struct inode *inode, struct file *file)
  541. {
  542. struct input_handler *handler = input_table[iminor(inode) >> 5];
  543. struct file_operations *old_fops, *new_fops = NULL;
  544. int err;
  545. /* No load-on-demand here? */
  546. if (!handler || !(new_fops = fops_get(handler->fops)))
  547. return -ENODEV;
  548. /*
  549. * That's _really_ odd. Usually NULL ->open means "nothing special",
  550. * not "no device". Oh, well...
  551. */
  552. if (!new_fops->open) {
  553. fops_put(new_fops);
  554. return -ENODEV;
  555. }
  556. old_fops = file->f_op;
  557. file->f_op = new_fops;
  558. err = new_fops->open(inode, file);
  559. if (err) {
  560. fops_put(file->f_op);
  561. file->f_op = fops_get(old_fops);
  562. }
  563. fops_put(old_fops);
  564. return err;
  565. }
  566. static struct file_operations input_fops = {
  567. .owner = THIS_MODULE,
  568. .open = input_open_file,
  569. };
  570. struct class *input_class;
  571. static int __init input_init(void)
  572. {
  573. int err;
  574. input_class = class_create(THIS_MODULE, "input");
  575. if (IS_ERR(input_class)) {
  576. printk(KERN_ERR "input: unable to register input class\n");
  577. return PTR_ERR(input_class);
  578. }
  579. err = input_proc_init();
  580. if (err)
  581. goto fail1;
  582. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  583. if (err) {
  584. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  585. goto fail2;
  586. }
  587. err = devfs_mk_dir("input");
  588. if (err)
  589. goto fail3;
  590. return 0;
  591. fail3: unregister_chrdev(INPUT_MAJOR, "input");
  592. fail2: input_proc_exit();
  593. fail1: class_destroy(input_class);
  594. return err;
  595. }
  596. static void __exit input_exit(void)
  597. {
  598. input_proc_exit();
  599. devfs_remove("input");
  600. unregister_chrdev(INPUT_MAJOR, "input");
  601. class_destroy(input_class);
  602. }
  603. subsys_initcall(input_init);
  604. module_exit(input_exit);