input.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  24. MODULE_DESCRIPTION("Input core");
  25. MODULE_LICENSE("GPL");
  26. EXPORT_SYMBOL(input_allocate_device);
  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. EXPORT_SYMBOL_GPL(input_dev_class);
  40. #define INPUT_DEVICES 256
  41. static LIST_HEAD(input_dev_list);
  42. static LIST_HEAD(input_handler_list);
  43. static struct input_handler *input_table[8];
  44. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  45. {
  46. struct input_handle *handle;
  47. if (type > EV_MAX || !test_bit(type, dev->evbit))
  48. return;
  49. add_input_randomness(type, code, value);
  50. switch (type) {
  51. case EV_SYN:
  52. switch (code) {
  53. case SYN_CONFIG:
  54. if (dev->event) dev->event(dev, type, code, value);
  55. break;
  56. case SYN_REPORT:
  57. if (dev->sync) return;
  58. dev->sync = 1;
  59. break;
  60. }
  61. break;
  62. case EV_KEY:
  63. if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  64. return;
  65. if (value == 2)
  66. break;
  67. change_bit(code, dev->key);
  68. if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
  69. dev->repeat_key = code;
  70. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  71. }
  72. break;
  73. case EV_SW:
  74. if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
  75. return;
  76. change_bit(code, dev->sw);
  77. break;
  78. case EV_ABS:
  79. if (code > ABS_MAX || !test_bit(code, dev->absbit))
  80. return;
  81. if (dev->absfuzz[code]) {
  82. if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
  83. (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
  84. return;
  85. if ((value > dev->abs[code] - dev->absfuzz[code]) &&
  86. (value < dev->abs[code] + dev->absfuzz[code]))
  87. value = (dev->abs[code] * 3 + value) >> 2;
  88. if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
  89. (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
  90. value = (dev->abs[code] + value) >> 1;
  91. }
  92. if (dev->abs[code] == value)
  93. return;
  94. dev->abs[code] = value;
  95. break;
  96. case EV_REL:
  97. if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
  98. return;
  99. break;
  100. case EV_MSC:
  101. if (code > MSC_MAX || !test_bit(code, dev->mscbit))
  102. return;
  103. if (dev->event) dev->event(dev, type, code, value);
  104. break;
  105. case EV_LED:
  106. if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
  107. return;
  108. change_bit(code, dev->led);
  109. if (dev->event) dev->event(dev, type, code, value);
  110. break;
  111. case EV_SND:
  112. if (code > SND_MAX || !test_bit(code, dev->sndbit))
  113. return;
  114. if (dev->event) dev->event(dev, type, code, value);
  115. break;
  116. case EV_REP:
  117. if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
  118. dev->rep[code] = value;
  119. if (dev->event) dev->event(dev, type, code, value);
  120. break;
  121. case EV_FF:
  122. if (dev->event) dev->event(dev, type, code, value);
  123. break;
  124. }
  125. if (type != EV_SYN)
  126. dev->sync = 0;
  127. if (dev->grab)
  128. dev->grab->handler->event(dev->grab, type, code, value);
  129. else
  130. list_for_each_entry(handle, &dev->h_list, d_node)
  131. if (handle->open)
  132. handle->handler->event(handle, type, code, value);
  133. }
  134. static void input_repeat_key(unsigned long data)
  135. {
  136. struct input_dev *dev = (void *) data;
  137. if (!test_bit(dev->repeat_key, dev->key))
  138. return;
  139. input_event(dev, EV_KEY, dev->repeat_key, 2);
  140. input_sync(dev);
  141. if (dev->rep[REP_PERIOD])
  142. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
  143. }
  144. int input_accept_process(struct input_handle *handle, struct file *file)
  145. {
  146. if (handle->dev->accept)
  147. return handle->dev->accept(handle->dev, file);
  148. return 0;
  149. }
  150. int input_grab_device(struct input_handle *handle)
  151. {
  152. if (handle->dev->grab)
  153. return -EBUSY;
  154. handle->dev->grab = handle;
  155. return 0;
  156. }
  157. void input_release_device(struct input_handle *handle)
  158. {
  159. if (handle->dev->grab == handle)
  160. handle->dev->grab = NULL;
  161. }
  162. int input_open_device(struct input_handle *handle)
  163. {
  164. struct input_dev *dev = handle->dev;
  165. int err;
  166. err = down_interruptible(&dev->sem);
  167. if (err)
  168. return err;
  169. handle->open++;
  170. if (!dev->users++ && dev->open)
  171. err = dev->open(dev);
  172. if (err)
  173. handle->open--;
  174. up(&dev->sem);
  175. return err;
  176. }
  177. int input_flush_device(struct input_handle* handle, struct file* file)
  178. {
  179. if (handle->dev->flush)
  180. return handle->dev->flush(handle->dev, file);
  181. return 0;
  182. }
  183. void input_close_device(struct input_handle *handle)
  184. {
  185. struct input_dev *dev = handle->dev;
  186. input_release_device(handle);
  187. down(&dev->sem);
  188. if (!--dev->users && dev->close)
  189. dev->close(dev);
  190. handle->open--;
  191. up(&dev->sem);
  192. }
  193. static void input_link_handle(struct input_handle *handle)
  194. {
  195. list_add_tail(&handle->d_node, &handle->dev->h_list);
  196. list_add_tail(&handle->h_node, &handle->handler->h_list);
  197. }
  198. #define MATCH_BIT(bit, max) \
  199. for (i = 0; i < NBITS(max); i++) \
  200. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  201. break; \
  202. if (i != NBITS(max)) \
  203. continue;
  204. static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
  205. {
  206. int i;
  207. for (; id->flags || id->driver_info; id++) {
  208. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  209. if (id->id.bustype != dev->id.bustype)
  210. continue;
  211. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  212. if (id->id.vendor != dev->id.vendor)
  213. continue;
  214. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  215. if (id->id.product != dev->id.product)
  216. continue;
  217. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  218. if (id->id.version != dev->id.version)
  219. continue;
  220. MATCH_BIT(evbit, EV_MAX);
  221. MATCH_BIT(keybit, KEY_MAX);
  222. MATCH_BIT(relbit, REL_MAX);
  223. MATCH_BIT(absbit, ABS_MAX);
  224. MATCH_BIT(mscbit, MSC_MAX);
  225. MATCH_BIT(ledbit, LED_MAX);
  226. MATCH_BIT(sndbit, SND_MAX);
  227. MATCH_BIT(ffbit, FF_MAX);
  228. MATCH_BIT(swbit, SW_MAX);
  229. return id;
  230. }
  231. return NULL;
  232. }
  233. /*
  234. * Input hotplugging interface - loading event handlers based on
  235. * device bitfields.
  236. */
  237. #ifdef CONFIG_HOTPLUG
  238. /*
  239. * Input hotplugging invokes what /proc/sys/kernel/hotplug says
  240. * (normally /sbin/hotplug) when input devices get added or removed.
  241. *
  242. * This invokes a user mode policy agent, typically helping to load driver
  243. * or other modules, configure the device, and more. Drivers can provide
  244. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  245. *
  246. */
  247. #define SPRINTF_BIT_A(bit, name, max) \
  248. do { \
  249. envp[i++] = scratch; \
  250. scratch += sprintf(scratch, name); \
  251. for (j = NBITS(max) - 1; j >= 0; j--) \
  252. if (dev->bit[j]) break; \
  253. for (; j >= 0; j--) \
  254. scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
  255. scratch++; \
  256. } while (0)
  257. #define SPRINTF_BIT_A2(bit, name, max, ev) \
  258. do { \
  259. if (test_bit(ev, dev->evbit)) \
  260. SPRINTF_BIT_A(bit, name, max); \
  261. } while (0)
  262. static void input_call_hotplug(char *verb, struct input_dev *dev)
  263. {
  264. char *argv[3], **envp, *buf, *scratch;
  265. int i = 0, j, value;
  266. if (!hotplug_path[0]) {
  267. printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
  268. return;
  269. }
  270. if (in_interrupt()) {
  271. printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
  272. return;
  273. }
  274. if (!current->fs->root) {
  275. printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
  276. return;
  277. }
  278. if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
  279. printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
  280. return;
  281. }
  282. if (!(buf = kmalloc(1024, GFP_KERNEL))) {
  283. kfree (envp);
  284. printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
  285. return;
  286. }
  287. argv[0] = hotplug_path;
  288. argv[1] = "input";
  289. argv[2] = NULL;
  290. envp[i++] = "HOME=/";
  291. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  292. scratch = buf;
  293. envp[i++] = scratch;
  294. scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
  295. envp[i++] = scratch;
  296. scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
  297. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
  298. if (dev->name) {
  299. envp[i++] = scratch;
  300. scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
  301. }
  302. if (dev->phys) {
  303. envp[i++] = scratch;
  304. scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
  305. }
  306. SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
  307. SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
  308. SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
  309. SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
  310. SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
  311. SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
  312. SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
  313. SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
  314. SPRINTF_BIT_A2(swbit, "SW=", SW_MAX, EV_SW);
  315. envp[i++] = NULL;
  316. #ifdef INPUT_DEBUG
  317. printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
  318. argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
  319. #endif
  320. value = call_usermodehelper(argv [0], argv, envp, 0);
  321. kfree(buf);
  322. kfree(envp);
  323. #ifdef INPUT_DEBUG
  324. if (value != 0)
  325. printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
  326. #endif
  327. }
  328. #endif
  329. static int input_print_bitmap(char *buf, unsigned long *bitmap, int max)
  330. {
  331. int i;
  332. int len = 0;
  333. for (i = NBITS(max) - 1; i > 0; i--)
  334. if (bitmap[i])
  335. break;
  336. for (; i >= 0; i--)
  337. len += sprintf(buf + len, "%lx%s", bitmap[i], i > 0 ? " " : "");
  338. len += sprintf(buf + len, "\n");
  339. return len;
  340. }
  341. #ifdef CONFIG_PROC_FS
  342. static struct proc_dir_entry *proc_bus_input_dir;
  343. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  344. static int input_devices_state;
  345. static inline void input_wakeup_procfs_readers(void)
  346. {
  347. input_devices_state++;
  348. wake_up(&input_devices_poll_wait);
  349. }
  350. static unsigned int input_devices_poll(struct file *file, poll_table *wait)
  351. {
  352. int state = input_devices_state;
  353. poll_wait(file, &input_devices_poll_wait, wait);
  354. if (state != input_devices_state)
  355. return POLLIN | POLLRDNORM;
  356. return 0;
  357. }
  358. #define SPRINTF_BIT_B(ev, bm) \
  359. do { \
  360. len += sprintf(buf + len, "B: %s=", #ev); \
  361. len += input_print_bitmap(buf + len, \
  362. dev->bm##bit, ev##_MAX); \
  363. } while (0)
  364. #define SPRINTF_BIT_B2(ev, bm) \
  365. do { \
  366. if (test_bit(EV_##ev, dev->evbit)) \
  367. SPRINTF_BIT_B(ev, bm); \
  368. } while (0)
  369. static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  370. {
  371. struct input_dev *dev;
  372. struct input_handle *handle;
  373. const char *path;
  374. off_t at = 0;
  375. int len, cnt = 0;
  376. list_for_each_entry(dev, &input_dev_list, node) {
  377. path = dev->dynalloc ? kobject_get_path(&dev->cdev.kobj, GFP_KERNEL) : NULL;
  378. len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  379. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  380. len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  381. len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  382. len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
  383. len += sprintf(buf + len, "H: Handlers=");
  384. list_for_each_entry(handle, &dev->h_list, d_node)
  385. len += sprintf(buf + len, "%s ", handle->name);
  386. len += sprintf(buf + len, "\n");
  387. SPRINTF_BIT_B(EV, ev);
  388. SPRINTF_BIT_B2(KEY, key);
  389. SPRINTF_BIT_B2(REL, rel);
  390. SPRINTF_BIT_B2(ABS, abs);
  391. SPRINTF_BIT_B2(MSC, msc);
  392. SPRINTF_BIT_B2(LED, led);
  393. SPRINTF_BIT_B2(SND, snd);
  394. SPRINTF_BIT_B2(FF, ff);
  395. SPRINTF_BIT_B2(SW, sw);
  396. len += sprintf(buf + len, "\n");
  397. at += len;
  398. if (at >= pos) {
  399. if (!*start) {
  400. *start = buf + (pos - (at - len));
  401. cnt = at - pos;
  402. } else cnt += len;
  403. buf += len;
  404. if (cnt >= count)
  405. break;
  406. }
  407. kfree(path);
  408. }
  409. if (&dev->node == &input_dev_list)
  410. *eof = 1;
  411. return (count > cnt) ? cnt : count;
  412. }
  413. static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  414. {
  415. struct input_handler *handler;
  416. off_t at = 0;
  417. int len = 0, cnt = 0;
  418. int i = 0;
  419. list_for_each_entry(handler, &input_handler_list, node) {
  420. if (handler->fops)
  421. len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
  422. i++, handler->name, handler->minor);
  423. else
  424. len = sprintf(buf, "N: Number=%d Name=%s\n",
  425. i++, handler->name);
  426. at += len;
  427. if (at >= pos) {
  428. if (!*start) {
  429. *start = buf + (pos - (at - len));
  430. cnt = at - pos;
  431. } else cnt += len;
  432. buf += len;
  433. if (cnt >= count)
  434. break;
  435. }
  436. }
  437. if (&handler->node == &input_handler_list)
  438. *eof = 1;
  439. return (count > cnt) ? cnt : count;
  440. }
  441. static struct file_operations input_fileops;
  442. static int __init input_proc_init(void)
  443. {
  444. struct proc_dir_entry *entry;
  445. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  446. if (!proc_bus_input_dir)
  447. return -ENOMEM;
  448. proc_bus_input_dir->owner = THIS_MODULE;
  449. entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
  450. if (!entry)
  451. goto fail1;
  452. entry->owner = THIS_MODULE;
  453. input_fileops = *entry->proc_fops;
  454. entry->proc_fops = &input_fileops;
  455. entry->proc_fops->poll = input_devices_poll;
  456. entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
  457. if (!entry)
  458. goto fail2;
  459. entry->owner = THIS_MODULE;
  460. return 0;
  461. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  462. fail1: remove_proc_entry("input", proc_bus);
  463. return -ENOMEM;
  464. }
  465. static void input_proc_exit(void)
  466. {
  467. remove_proc_entry("devices", proc_bus_input_dir);
  468. remove_proc_entry("handlers", proc_bus_input_dir);
  469. remove_proc_entry("input", proc_bus);
  470. }
  471. #else /* !CONFIG_PROC_FS */
  472. static inline void input_wakeup_procfs_readers(void) { }
  473. static inline int input_proc_init(void) { return 0; }
  474. static inline void input_proc_exit(void) { }
  475. #endif
  476. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  477. static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
  478. { \
  479. struct input_dev *input_dev = to_input_dev(dev); \
  480. int retval; \
  481. \
  482. retval = down_interruptible(&input_dev->sem); \
  483. if (retval) \
  484. return retval; \
  485. \
  486. retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
  487. \
  488. up(&input_dev->sem); \
  489. \
  490. return retval; \
  491. }
  492. INPUT_DEV_STRING_ATTR_SHOW(name);
  493. INPUT_DEV_STRING_ATTR_SHOW(phys);
  494. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  495. static struct class_device_attribute input_dev_attrs[] = {
  496. __ATTR(name, S_IRUGO, input_dev_show_name, NULL),
  497. __ATTR(phys, S_IRUGO, input_dev_show_phys, NULL),
  498. __ATTR(uniq, S_IRUGO, input_dev_show_uniq, NULL),
  499. __ATTR_NULL
  500. };
  501. #define INPUT_DEV_ID_ATTR(name) \
  502. static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
  503. { \
  504. struct input_dev *input_dev = to_input_dev(dev); \
  505. return sprintf(buf, "%04x\n", input_dev->id.name); \
  506. } \
  507. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
  508. INPUT_DEV_ID_ATTR(bustype);
  509. INPUT_DEV_ID_ATTR(vendor);
  510. INPUT_DEV_ID_ATTR(product);
  511. INPUT_DEV_ID_ATTR(version);
  512. static struct attribute *input_dev_id_attrs[] = {
  513. &class_device_attr_bustype.attr,
  514. &class_device_attr_vendor.attr,
  515. &class_device_attr_product.attr,
  516. &class_device_attr_version.attr,
  517. NULL
  518. };
  519. static struct attribute_group input_dev_id_attr_group = {
  520. .name = "id",
  521. .attrs = input_dev_id_attrs,
  522. };
  523. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  524. static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
  525. { \
  526. struct input_dev *input_dev = to_input_dev(dev); \
  527. return input_print_bitmap(buf, input_dev->bm##bit, ev##_MAX); \
  528. } \
  529. static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
  530. INPUT_DEV_CAP_ATTR(EV, ev);
  531. INPUT_DEV_CAP_ATTR(KEY, key);
  532. INPUT_DEV_CAP_ATTR(REL, rel);
  533. INPUT_DEV_CAP_ATTR(ABS, abs);
  534. INPUT_DEV_CAP_ATTR(MSC, msc);
  535. INPUT_DEV_CAP_ATTR(LED, led);
  536. INPUT_DEV_CAP_ATTR(SND, snd);
  537. INPUT_DEV_CAP_ATTR(FF, ff);
  538. INPUT_DEV_CAP_ATTR(SW, sw);
  539. static struct attribute *input_dev_caps_attrs[] = {
  540. &class_device_attr_ev.attr,
  541. &class_device_attr_key.attr,
  542. &class_device_attr_rel.attr,
  543. &class_device_attr_abs.attr,
  544. &class_device_attr_msc.attr,
  545. &class_device_attr_led.attr,
  546. &class_device_attr_snd.attr,
  547. &class_device_attr_ff.attr,
  548. &class_device_attr_sw.attr,
  549. NULL
  550. };
  551. static struct attribute_group input_dev_caps_attr_group = {
  552. .name = "capabilities",
  553. .attrs = input_dev_caps_attrs,
  554. };
  555. static void input_dev_release(struct class_device *class_dev)
  556. {
  557. struct input_dev *dev = to_input_dev(class_dev);
  558. kfree(dev);
  559. module_put(THIS_MODULE);
  560. }
  561. struct class input_dev_class = {
  562. .name = "input_dev",
  563. .release = input_dev_release,
  564. .class_dev_attrs = input_dev_attrs,
  565. };
  566. struct input_dev *input_allocate_device(void)
  567. {
  568. struct input_dev *dev;
  569. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  570. if (dev) {
  571. dev->dynalloc = 1;
  572. dev->cdev.class = &input_dev_class;
  573. class_device_initialize(&dev->cdev);
  574. INIT_LIST_HEAD(&dev->h_list);
  575. INIT_LIST_HEAD(&dev->node);
  576. }
  577. return dev;
  578. }
  579. static void input_register_classdevice(struct input_dev *dev)
  580. {
  581. static atomic_t input_no = ATOMIC_INIT(0);
  582. const char *path;
  583. __module_get(THIS_MODULE);
  584. dev->dev = dev->cdev.dev;
  585. snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
  586. "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
  587. path = kobject_get_path(&dev->cdev.class->subsys.kset.kobj, GFP_KERNEL);
  588. printk(KERN_INFO "input: %s/%s as %s\n",
  589. dev->name ? dev->name : "Unspecified device",
  590. path ? path : "", dev->cdev.class_id);
  591. kfree(path);
  592. class_device_add(&dev->cdev);
  593. sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  594. sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  595. }
  596. void input_register_device(struct input_dev *dev)
  597. {
  598. struct input_handle *handle;
  599. struct input_handler *handler;
  600. struct input_device_id *id;
  601. set_bit(EV_SYN, dev->evbit);
  602. init_MUTEX(&dev->sem);
  603. /*
  604. * If delay and period are pre-set by the driver, then autorepeating
  605. * is handled by the driver itself and we don't do it in input.c.
  606. */
  607. init_timer(&dev->timer);
  608. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  609. dev->timer.data = (long) dev;
  610. dev->timer.function = input_repeat_key;
  611. dev->rep[REP_DELAY] = 250;
  612. dev->rep[REP_PERIOD] = 33;
  613. }
  614. INIT_LIST_HEAD(&dev->h_list);
  615. list_add_tail(&dev->node, &input_dev_list);
  616. if (dev->dynalloc)
  617. input_register_classdevice(dev);
  618. list_for_each_entry(handler, &input_handler_list, node)
  619. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  620. if ((id = input_match_device(handler->id_table, dev)))
  621. if ((handle = handler->connect(handler, dev, id)))
  622. input_link_handle(handle);
  623. #ifdef CONFIG_HOTPLUG
  624. input_call_hotplug("add", dev);
  625. #endif
  626. input_wakeup_procfs_readers();
  627. }
  628. void input_unregister_device(struct input_dev *dev)
  629. {
  630. struct list_head * node, * next;
  631. if (!dev) return;
  632. del_timer_sync(&dev->timer);
  633. list_for_each_safe(node, next, &dev->h_list) {
  634. struct input_handle * handle = to_handle(node);
  635. list_del_init(&handle->d_node);
  636. list_del_init(&handle->h_node);
  637. handle->handler->disconnect(handle);
  638. }
  639. #ifdef CONFIG_HOTPLUG
  640. input_call_hotplug("remove", dev);
  641. #endif
  642. list_del_init(&dev->node);
  643. if (dev->dynalloc) {
  644. sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  645. sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  646. class_device_unregister(&dev->cdev);
  647. }
  648. input_wakeup_procfs_readers();
  649. }
  650. void input_register_handler(struct input_handler *handler)
  651. {
  652. struct input_dev *dev;
  653. struct input_handle *handle;
  654. struct input_device_id *id;
  655. if (!handler) return;
  656. INIT_LIST_HEAD(&handler->h_list);
  657. if (handler->fops != NULL)
  658. input_table[handler->minor >> 5] = handler;
  659. list_add_tail(&handler->node, &input_handler_list);
  660. list_for_each_entry(dev, &input_dev_list, node)
  661. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  662. if ((id = input_match_device(handler->id_table, dev)))
  663. if ((handle = handler->connect(handler, dev, id)))
  664. input_link_handle(handle);
  665. input_wakeup_procfs_readers();
  666. }
  667. void input_unregister_handler(struct input_handler *handler)
  668. {
  669. struct list_head * node, * next;
  670. list_for_each_safe(node, next, &handler->h_list) {
  671. struct input_handle * handle = to_handle_h(node);
  672. list_del_init(&handle->h_node);
  673. list_del_init(&handle->d_node);
  674. handler->disconnect(handle);
  675. }
  676. list_del_init(&handler->node);
  677. if (handler->fops != NULL)
  678. input_table[handler->minor >> 5] = NULL;
  679. input_wakeup_procfs_readers();
  680. }
  681. static int input_open_file(struct inode *inode, struct file *file)
  682. {
  683. struct input_handler *handler = input_table[iminor(inode) >> 5];
  684. struct file_operations *old_fops, *new_fops = NULL;
  685. int err;
  686. /* No load-on-demand here? */
  687. if (!handler || !(new_fops = fops_get(handler->fops)))
  688. return -ENODEV;
  689. /*
  690. * That's _really_ odd. Usually NULL ->open means "nothing special",
  691. * not "no device". Oh, well...
  692. */
  693. if (!new_fops->open) {
  694. fops_put(new_fops);
  695. return -ENODEV;
  696. }
  697. old_fops = file->f_op;
  698. file->f_op = new_fops;
  699. err = new_fops->open(inode, file);
  700. if (err) {
  701. fops_put(file->f_op);
  702. file->f_op = fops_get(old_fops);
  703. }
  704. fops_put(old_fops);
  705. return err;
  706. }
  707. static struct file_operations input_fops = {
  708. .owner = THIS_MODULE,
  709. .open = input_open_file,
  710. };
  711. struct class *input_class;
  712. static int __init input_init(void)
  713. {
  714. int err;
  715. err = class_register(&input_dev_class);
  716. if (err) {
  717. printk(KERN_ERR "input: unable to register input_dev class\n");
  718. return err;
  719. }
  720. input_class = class_create(THIS_MODULE, "input");
  721. if (IS_ERR(input_class)) {
  722. printk(KERN_ERR "input: unable to register input class\n");
  723. err = PTR_ERR(input_class);
  724. goto fail1;
  725. }
  726. err = input_proc_init();
  727. if (err)
  728. goto fail2;
  729. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  730. if (err) {
  731. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  732. goto fail3;
  733. }
  734. return 0;
  735. fail3: input_proc_exit();
  736. fail2: class_destroy(input_class);
  737. fail1: class_unregister(&input_dev_class);
  738. return err;
  739. }
  740. static void __exit input_exit(void)
  741. {
  742. input_proc_exit();
  743. unregister_chrdev(INPUT_MAJOR, "input");
  744. class_destroy(input_class);
  745. class_unregister(&input_dev_class);
  746. }
  747. subsys_initcall(input_init);
  748. module_exit(input_exit);