input.c 27 KB

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