input.c 24 KB

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