input.c 27 KB

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