input.c 27 KB

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