input.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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/seq_file.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/poll.h>
  22. #include <linux/device.h>
  23. #include <linux/mutex.h>
  24. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  25. MODULE_DESCRIPTION("Input core");
  26. MODULE_LICENSE("GPL");
  27. EXPORT_SYMBOL(input_allocate_device);
  28. EXPORT_SYMBOL(input_register_device);
  29. EXPORT_SYMBOL(input_unregister_device);
  30. EXPORT_SYMBOL(input_register_handler);
  31. EXPORT_SYMBOL(input_unregister_handler);
  32. EXPORT_SYMBOL(input_grab_device);
  33. EXPORT_SYMBOL(input_release_device);
  34. EXPORT_SYMBOL(input_open_device);
  35. EXPORT_SYMBOL(input_close_device);
  36. EXPORT_SYMBOL(input_accept_process);
  37. EXPORT_SYMBOL(input_flush_device);
  38. EXPORT_SYMBOL(input_event);
  39. EXPORT_SYMBOL_GPL(input_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 = mutex_lock_interruptible(&dev->mutex);
  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. mutex_unlock(&dev->mutex);
  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. mutex_lock(&dev->mutex);
  188. if (!--dev->users && dev->close)
  189. dev->close(dev);
  190. handle->open--;
  191. mutex_unlock(&dev->mutex);
  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. #ifdef CONFIG_PROC_FS
  234. static struct proc_dir_entry *proc_bus_input_dir;
  235. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  236. static int input_devices_state;
  237. static inline void input_wakeup_procfs_readers(void)
  238. {
  239. input_devices_state++;
  240. wake_up(&input_devices_poll_wait);
  241. }
  242. static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
  243. {
  244. int state = input_devices_state;
  245. poll_wait(file, &input_devices_poll_wait, wait);
  246. if (state != input_devices_state)
  247. return POLLIN | POLLRDNORM;
  248. return 0;
  249. }
  250. static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
  251. {
  252. struct list_head *node;
  253. loff_t i = 0;
  254. list_for_each(node, list)
  255. if (i++ == *pos)
  256. return node;
  257. return NULL;
  258. }
  259. static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
  260. {
  261. if (element->next == list)
  262. return NULL;
  263. ++(*pos);
  264. return element->next;
  265. }
  266. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  267. {
  268. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  269. return list_get_nth_element(&input_dev_list, pos);
  270. }
  271. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  272. {
  273. return list_get_next_element(&input_dev_list, v, pos);
  274. }
  275. static void input_devices_seq_stop(struct seq_file *seq, void *v)
  276. {
  277. /* release lock here */
  278. }
  279. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  280. unsigned long *bitmap, int max)
  281. {
  282. int i;
  283. for (i = NBITS(max) - 1; i > 0; i--)
  284. if (bitmap[i])
  285. break;
  286. seq_printf(seq, "B: %s=", name);
  287. for (; i >= 0; i--)
  288. seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  289. seq_putc(seq, '\n');
  290. }
  291. static int input_devices_seq_show(struct seq_file *seq, void *v)
  292. {
  293. struct input_dev *dev = container_of(v, struct input_dev, node);
  294. const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  295. struct input_handle *handle;
  296. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  297. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  298. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  299. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  300. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  301. seq_printf(seq, "H: Handlers=");
  302. list_for_each_entry(handle, &dev->h_list, d_node)
  303. seq_printf(seq, "%s ", handle->name);
  304. seq_putc(seq, '\n');
  305. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  306. if (test_bit(EV_KEY, dev->evbit))
  307. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  308. if (test_bit(EV_REL, dev->evbit))
  309. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  310. if (test_bit(EV_ABS, dev->evbit))
  311. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  312. if (test_bit(EV_MSC, dev->evbit))
  313. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  314. if (test_bit(EV_LED, dev->evbit))
  315. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  316. if (test_bit(EV_SND, dev->evbit))
  317. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  318. if (test_bit(EV_FF, dev->evbit))
  319. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  320. if (test_bit(EV_SW, dev->evbit))
  321. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  322. seq_putc(seq, '\n');
  323. kfree(path);
  324. return 0;
  325. }
  326. static struct seq_operations input_devices_seq_ops = {
  327. .start = input_devices_seq_start,
  328. .next = input_devices_seq_next,
  329. .stop = input_devices_seq_stop,
  330. .show = input_devices_seq_show,
  331. };
  332. static int input_proc_devices_open(struct inode *inode, struct file *file)
  333. {
  334. return seq_open(file, &input_devices_seq_ops);
  335. }
  336. static struct file_operations input_devices_fileops = {
  337. .owner = THIS_MODULE,
  338. .open = input_proc_devices_open,
  339. .poll = input_proc_devices_poll,
  340. .read = seq_read,
  341. .llseek = seq_lseek,
  342. .release = seq_release,
  343. };
  344. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  345. {
  346. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  347. seq->private = (void *)(unsigned long)*pos;
  348. return list_get_nth_element(&input_handler_list, pos);
  349. }
  350. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  351. {
  352. seq->private = (void *)(unsigned long)(*pos + 1);
  353. return list_get_next_element(&input_handler_list, v, pos);
  354. }
  355. static void input_handlers_seq_stop(struct seq_file *seq, void *v)
  356. {
  357. /* release lock here */
  358. }
  359. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  360. {
  361. struct input_handler *handler = container_of(v, struct input_handler, node);
  362. seq_printf(seq, "N: Number=%ld Name=%s",
  363. (unsigned long)seq->private, handler->name);
  364. if (handler->fops)
  365. seq_printf(seq, " Minor=%d", handler->minor);
  366. seq_putc(seq, '\n');
  367. return 0;
  368. }
  369. static struct seq_operations input_handlers_seq_ops = {
  370. .start = input_handlers_seq_start,
  371. .next = input_handlers_seq_next,
  372. .stop = input_handlers_seq_stop,
  373. .show = input_handlers_seq_show,
  374. };
  375. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  376. {
  377. return seq_open(file, &input_handlers_seq_ops);
  378. }
  379. static struct file_operations input_handlers_fileops = {
  380. .owner = THIS_MODULE,
  381. .open = input_proc_handlers_open,
  382. .read = seq_read,
  383. .llseek = seq_lseek,
  384. .release = seq_release,
  385. };
  386. static int __init input_proc_init(void)
  387. {
  388. struct proc_dir_entry *entry;
  389. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  390. if (!proc_bus_input_dir)
  391. return -ENOMEM;
  392. proc_bus_input_dir->owner = THIS_MODULE;
  393. entry = create_proc_entry("devices", 0, proc_bus_input_dir);
  394. if (!entry)
  395. goto fail1;
  396. entry->owner = THIS_MODULE;
  397. entry->proc_fops = &input_devices_fileops;
  398. entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
  399. if (!entry)
  400. goto fail2;
  401. entry->owner = THIS_MODULE;
  402. entry->proc_fops = &input_handlers_fileops;
  403. return 0;
  404. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  405. fail1: remove_proc_entry("input", proc_bus);
  406. return -ENOMEM;
  407. }
  408. static void input_proc_exit(void)
  409. {
  410. remove_proc_entry("devices", proc_bus_input_dir);
  411. remove_proc_entry("handlers", proc_bus_input_dir);
  412. remove_proc_entry("input", proc_bus);
  413. }
  414. #else /* !CONFIG_PROC_FS */
  415. static inline void input_wakeup_procfs_readers(void) { }
  416. static inline int input_proc_init(void) { return 0; }
  417. static inline void input_proc_exit(void) { }
  418. #endif
  419. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  420. static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
  421. { \
  422. struct input_dev *input_dev = to_input_dev(dev); \
  423. int retval; \
  424. \
  425. retval = mutex_lock_interruptible(&input_dev->mutex); \
  426. if (retval) \
  427. return retval; \
  428. \
  429. retval = scnprintf(buf, PAGE_SIZE, \
  430. "%s\n", input_dev->name ? input_dev->name : ""); \
  431. \
  432. mutex_unlock(&input_dev->mutex); \
  433. \
  434. return retval; \
  435. } \
  436. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
  437. INPUT_DEV_STRING_ATTR_SHOW(name);
  438. INPUT_DEV_STRING_ATTR_SHOW(phys);
  439. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  440. static int input_print_modalias_bits(char *buf, int size,
  441. char name, unsigned long *bm,
  442. unsigned int min_bit, unsigned int max_bit)
  443. {
  444. int len = 0, i;
  445. len += snprintf(buf, max(size, 0), "%c", name);
  446. for (i = min_bit; i < max_bit; i++)
  447. if (bm[LONG(i)] & BIT(i))
  448. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  449. return len;
  450. }
  451. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  452. int add_cr)
  453. {
  454. int len;
  455. len = snprintf(buf, max(size, 0),
  456. "input:b%04Xv%04Xp%04Xe%04X-",
  457. id->id.bustype, id->id.vendor,
  458. id->id.product, id->id.version);
  459. len += input_print_modalias_bits(buf + len, size - len,
  460. 'e', id->evbit, 0, EV_MAX);
  461. len += input_print_modalias_bits(buf + len, size - len,
  462. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  463. len += input_print_modalias_bits(buf + len, size - len,
  464. 'r', id->relbit, 0, REL_MAX);
  465. len += input_print_modalias_bits(buf + len, size - len,
  466. 'a', id->absbit, 0, ABS_MAX);
  467. len += input_print_modalias_bits(buf + len, size - len,
  468. 'm', id->mscbit, 0, MSC_MAX);
  469. len += input_print_modalias_bits(buf + len, size - len,
  470. 'l', id->ledbit, 0, LED_MAX);
  471. len += input_print_modalias_bits(buf + len, size - len,
  472. 's', id->sndbit, 0, SND_MAX);
  473. len += input_print_modalias_bits(buf + len, size - len,
  474. 'f', id->ffbit, 0, FF_MAX);
  475. len += input_print_modalias_bits(buf + len, size - len,
  476. 'w', id->swbit, 0, SW_MAX);
  477. if (add_cr)
  478. len += snprintf(buf + len, max(size - len, 0), "\n");
  479. return len;
  480. }
  481. static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
  482. {
  483. struct input_dev *id = to_input_dev(dev);
  484. ssize_t len;
  485. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  486. return max_t(int, len, PAGE_SIZE);
  487. }
  488. static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  489. static struct attribute *input_dev_attrs[] = {
  490. &class_device_attr_name.attr,
  491. &class_device_attr_phys.attr,
  492. &class_device_attr_uniq.attr,
  493. &class_device_attr_modalias.attr,
  494. NULL
  495. };
  496. static struct attribute_group input_dev_attr_group = {
  497. .attrs = input_dev_attrs,
  498. };
  499. #define INPUT_DEV_ID_ATTR(name) \
  500. static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
  501. { \
  502. struct input_dev *input_dev = to_input_dev(dev); \
  503. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  504. } \
  505. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
  506. INPUT_DEV_ID_ATTR(bustype);
  507. INPUT_DEV_ID_ATTR(vendor);
  508. INPUT_DEV_ID_ATTR(product);
  509. INPUT_DEV_ID_ATTR(version);
  510. static struct attribute *input_dev_id_attrs[] = {
  511. &class_device_attr_bustype.attr,
  512. &class_device_attr_vendor.attr,
  513. &class_device_attr_product.attr,
  514. &class_device_attr_version.attr,
  515. NULL
  516. };
  517. static struct attribute_group input_dev_id_attr_group = {
  518. .name = "id",
  519. .attrs = input_dev_id_attrs,
  520. };
  521. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  522. int max, int add_cr)
  523. {
  524. int i;
  525. int len = 0;
  526. for (i = NBITS(max) - 1; i > 0; i--)
  527. if (bitmap[i])
  528. break;
  529. for (; i >= 0; i--)
  530. len += snprintf(buf + len, max(buf_size - len, 0),
  531. "%lx%s", bitmap[i], i > 0 ? " " : "");
  532. if (add_cr)
  533. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  534. return len;
  535. }
  536. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  537. static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
  538. { \
  539. struct input_dev *input_dev = to_input_dev(dev); \
  540. int len = input_print_bitmap(buf, PAGE_SIZE, \
  541. input_dev->bm##bit, ev##_MAX, 1); \
  542. return min_t(int, len, PAGE_SIZE); \
  543. } \
  544. static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
  545. INPUT_DEV_CAP_ATTR(EV, ev);
  546. INPUT_DEV_CAP_ATTR(KEY, key);
  547. INPUT_DEV_CAP_ATTR(REL, rel);
  548. INPUT_DEV_CAP_ATTR(ABS, abs);
  549. INPUT_DEV_CAP_ATTR(MSC, msc);
  550. INPUT_DEV_CAP_ATTR(LED, led);
  551. INPUT_DEV_CAP_ATTR(SND, snd);
  552. INPUT_DEV_CAP_ATTR(FF, ff);
  553. INPUT_DEV_CAP_ATTR(SW, sw);
  554. static struct attribute *input_dev_caps_attrs[] = {
  555. &class_device_attr_ev.attr,
  556. &class_device_attr_key.attr,
  557. &class_device_attr_rel.attr,
  558. &class_device_attr_abs.attr,
  559. &class_device_attr_msc.attr,
  560. &class_device_attr_led.attr,
  561. &class_device_attr_snd.attr,
  562. &class_device_attr_ff.attr,
  563. &class_device_attr_sw.attr,
  564. NULL
  565. };
  566. static struct attribute_group input_dev_caps_attr_group = {
  567. .name = "capabilities",
  568. .attrs = input_dev_caps_attrs,
  569. };
  570. static void input_dev_release(struct class_device *class_dev)
  571. {
  572. struct input_dev *dev = to_input_dev(class_dev);
  573. kfree(dev);
  574. module_put(THIS_MODULE);
  575. }
  576. /*
  577. * Input uevent interface - loading event handlers based on
  578. * device bitfields.
  579. */
  580. static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
  581. char *buffer, int buffer_size, int *cur_len,
  582. const char *name, unsigned long *bitmap, int max)
  583. {
  584. if (*cur_index >= num_envp - 1)
  585. return -ENOMEM;
  586. envp[*cur_index] = buffer + *cur_len;
  587. *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
  588. if (*cur_len >= buffer_size)
  589. return -ENOMEM;
  590. *cur_len += input_print_bitmap(buffer + *cur_len,
  591. max(buffer_size - *cur_len, 0),
  592. bitmap, max, 0) + 1;
  593. if (*cur_len > buffer_size)
  594. return -ENOMEM;
  595. (*cur_index)++;
  596. return 0;
  597. }
  598. static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
  599. char *buffer, int buffer_size, int *cur_len,
  600. struct input_dev *dev)
  601. {
  602. if (*cur_index >= num_envp - 1)
  603. return -ENOMEM;
  604. envp[*cur_index] = buffer + *cur_len;
  605. *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
  606. "MODALIAS=");
  607. if (*cur_len >= buffer_size)
  608. return -ENOMEM;
  609. *cur_len += input_print_modalias(buffer + *cur_len,
  610. max(buffer_size - *cur_len, 0),
  611. dev, 0) + 1;
  612. if (*cur_len > buffer_size)
  613. return -ENOMEM;
  614. (*cur_index)++;
  615. return 0;
  616. }
  617. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  618. do { \
  619. int err = add_uevent_var(envp, num_envp, &i, \
  620. buffer, buffer_size, &len, \
  621. fmt, val); \
  622. if (err) \
  623. return err; \
  624. } while (0)
  625. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  626. do { \
  627. int err = input_add_uevent_bm_var(envp, num_envp, &i, \
  628. buffer, buffer_size, &len, \
  629. name, bm, max); \
  630. if (err) \
  631. return err; \
  632. } while (0)
  633. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  634. do { \
  635. int err = input_add_uevent_modalias_var(envp, \
  636. num_envp, &i, \
  637. buffer, buffer_size, &len, \
  638. dev); \
  639. if (err) \
  640. return err; \
  641. } while (0)
  642. static int input_dev_uevent(struct class_device *cdev, char **envp,
  643. int num_envp, char *buffer, int buffer_size)
  644. {
  645. struct input_dev *dev = to_input_dev(cdev);
  646. int i = 0;
  647. int len = 0;
  648. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  649. dev->id.bustype, dev->id.vendor,
  650. dev->id.product, dev->id.version);
  651. if (dev->name)
  652. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  653. if (dev->phys)
  654. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  655. if (dev->uniq)
  656. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  657. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  658. if (test_bit(EV_KEY, dev->evbit))
  659. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  660. if (test_bit(EV_REL, dev->evbit))
  661. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  662. if (test_bit(EV_ABS, dev->evbit))
  663. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  664. if (test_bit(EV_MSC, dev->evbit))
  665. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  666. if (test_bit(EV_LED, dev->evbit))
  667. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  668. if (test_bit(EV_SND, dev->evbit))
  669. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  670. if (test_bit(EV_FF, dev->evbit))
  671. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  672. if (test_bit(EV_SW, dev->evbit))
  673. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  674. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  675. envp[i] = NULL;
  676. return 0;
  677. }
  678. struct class input_class = {
  679. .name = "input",
  680. .release = input_dev_release,
  681. .uevent = input_dev_uevent,
  682. };
  683. struct input_dev *input_allocate_device(void)
  684. {
  685. struct input_dev *dev;
  686. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  687. if (dev) {
  688. dev->dynalloc = 1;
  689. dev->cdev.class = &input_class;
  690. class_device_initialize(&dev->cdev);
  691. INIT_LIST_HEAD(&dev->h_list);
  692. INIT_LIST_HEAD(&dev->node);
  693. }
  694. return dev;
  695. }
  696. int input_register_device(struct input_dev *dev)
  697. {
  698. static atomic_t input_no = ATOMIC_INIT(0);
  699. struct input_handle *handle;
  700. struct input_handler *handler;
  701. struct input_device_id *id;
  702. const char *path;
  703. int error;
  704. if (!dev->dynalloc) {
  705. printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
  706. "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
  707. dev->name ? dev->name : "<Unknown>");
  708. return -EINVAL;
  709. }
  710. mutex_init(&dev->mutex);
  711. set_bit(EV_SYN, dev->evbit);
  712. /*
  713. * If delay and period are pre-set by the driver, then autorepeating
  714. * is handled by the driver itself and we don't do it in input.c.
  715. */
  716. init_timer(&dev->timer);
  717. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  718. dev->timer.data = (long) dev;
  719. dev->timer.function = input_repeat_key;
  720. dev->rep[REP_DELAY] = 250;
  721. dev->rep[REP_PERIOD] = 33;
  722. }
  723. INIT_LIST_HEAD(&dev->h_list);
  724. list_add_tail(&dev->node, &input_dev_list);
  725. dev->cdev.class = &input_class;
  726. snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
  727. "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
  728. error = class_device_add(&dev->cdev);
  729. if (error)
  730. return error;
  731. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
  732. if (error)
  733. goto fail1;
  734. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  735. if (error)
  736. goto fail2;
  737. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  738. if (error)
  739. goto fail3;
  740. __module_get(THIS_MODULE);
  741. path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  742. printk(KERN_INFO "input: %s as %s\n",
  743. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  744. kfree(path);
  745. list_for_each_entry(handler, &input_handler_list, node)
  746. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  747. if ((id = input_match_device(handler->id_table, dev)))
  748. if ((handle = handler->connect(handler, dev, id)))
  749. input_link_handle(handle);
  750. input_wakeup_procfs_readers();
  751. return 0;
  752. fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  753. fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
  754. fail1: class_device_del(&dev->cdev);
  755. return error;
  756. }
  757. void input_unregister_device(struct input_dev *dev)
  758. {
  759. struct list_head * node, * next;
  760. if (!dev) return;
  761. del_timer_sync(&dev->timer);
  762. list_for_each_safe(node, next, &dev->h_list) {
  763. struct input_handle * handle = to_handle(node);
  764. list_del_init(&handle->d_node);
  765. list_del_init(&handle->h_node);
  766. handle->handler->disconnect(handle);
  767. }
  768. list_del_init(&dev->node);
  769. sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  770. sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  771. sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
  772. class_device_unregister(&dev->cdev);
  773. input_wakeup_procfs_readers();
  774. }
  775. void input_register_handler(struct input_handler *handler)
  776. {
  777. struct input_dev *dev;
  778. struct input_handle *handle;
  779. struct input_device_id *id;
  780. if (!handler) return;
  781. INIT_LIST_HEAD(&handler->h_list);
  782. if (handler->fops != NULL)
  783. input_table[handler->minor >> 5] = handler;
  784. list_add_tail(&handler->node, &input_handler_list);
  785. list_for_each_entry(dev, &input_dev_list, node)
  786. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  787. if ((id = input_match_device(handler->id_table, dev)))
  788. if ((handle = handler->connect(handler, dev, id)))
  789. input_link_handle(handle);
  790. input_wakeup_procfs_readers();
  791. }
  792. void input_unregister_handler(struct input_handler *handler)
  793. {
  794. struct list_head * node, * next;
  795. list_for_each_safe(node, next, &handler->h_list) {
  796. struct input_handle * handle = to_handle_h(node);
  797. list_del_init(&handle->h_node);
  798. list_del_init(&handle->d_node);
  799. handler->disconnect(handle);
  800. }
  801. list_del_init(&handler->node);
  802. if (handler->fops != NULL)
  803. input_table[handler->minor >> 5] = NULL;
  804. input_wakeup_procfs_readers();
  805. }
  806. static int input_open_file(struct inode *inode, struct file *file)
  807. {
  808. struct input_handler *handler = input_table[iminor(inode) >> 5];
  809. const struct file_operations *old_fops, *new_fops = NULL;
  810. int err;
  811. /* No load-on-demand here? */
  812. if (!handler || !(new_fops = fops_get(handler->fops)))
  813. return -ENODEV;
  814. /*
  815. * That's _really_ odd. Usually NULL ->open means "nothing special",
  816. * not "no device". Oh, well...
  817. */
  818. if (!new_fops->open) {
  819. fops_put(new_fops);
  820. return -ENODEV;
  821. }
  822. old_fops = file->f_op;
  823. file->f_op = new_fops;
  824. err = new_fops->open(inode, file);
  825. if (err) {
  826. fops_put(file->f_op);
  827. file->f_op = fops_get(old_fops);
  828. }
  829. fops_put(old_fops);
  830. return err;
  831. }
  832. static struct file_operations input_fops = {
  833. .owner = THIS_MODULE,
  834. .open = input_open_file,
  835. };
  836. static int __init input_init(void)
  837. {
  838. int err;
  839. err = class_register(&input_class);
  840. if (err) {
  841. printk(KERN_ERR "input: unable to register input_dev class\n");
  842. return err;
  843. }
  844. err = input_proc_init();
  845. if (err)
  846. goto fail1;
  847. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  848. if (err) {
  849. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  850. goto fail2;
  851. }
  852. return 0;
  853. fail2: input_proc_exit();
  854. fail1: class_unregister(&input_class);
  855. return err;
  856. }
  857. static void __exit input_exit(void)
  858. {
  859. input_proc_exit();
  860. unregister_chrdev(INPUT_MAJOR, "input");
  861. class_unregister(&input_class);
  862. }
  863. subsys_initcall(input_init);
  864. module_exit(input_exit);