input.c 29 KB

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