input.c 30 KB

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