input.c 27 KB

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