input.c 27 KB

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