input.c 29 KB

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