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