input.c 18 KB

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