input.c 18 KB

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