sparse-keymap.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Generic support for sparse keymaps
  3. *
  4. * Copyright (c) 2009 Dmitry Torokhov
  5. *
  6. * Derived from wistron button driver:
  7. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  8. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  9. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published by
  13. * the Free Software Foundation.
  14. */
  15. #include <linux/input.h>
  16. #include <linux/input/sparse-keymap.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  20. MODULE_DESCRIPTION("Generic support for sparse keymaps");
  21. MODULE_LICENSE("GPL v2");
  22. static unsigned int sparse_keymap_get_key_index(struct input_dev *dev,
  23. const struct key_entry *k)
  24. {
  25. struct key_entry *key;
  26. unsigned int idx = 0;
  27. for (key = dev->keycode; key->type != KE_END; key++) {
  28. if (key->type == KE_KEY) {
  29. if (key == k)
  30. break;
  31. idx++;
  32. }
  33. }
  34. return idx;
  35. }
  36. static struct key_entry *sparse_keymap_entry_by_index(struct input_dev *dev,
  37. unsigned int index)
  38. {
  39. struct key_entry *key;
  40. unsigned int key_cnt = 0;
  41. for (key = dev->keycode; key->type != KE_END; key++)
  42. if (key->type == KE_KEY)
  43. if (key_cnt++ == index)
  44. return key;
  45. return NULL;
  46. }
  47. /**
  48. * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
  49. * @dev: Input device using sparse keymap
  50. * @code: Scan code
  51. *
  52. * This function is used to perform &struct key_entry lookup in an
  53. * input device using sparse keymap.
  54. */
  55. struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
  56. unsigned int code)
  57. {
  58. struct key_entry *key;
  59. for (key = dev->keycode; key->type != KE_END; key++)
  60. if (code == key->code)
  61. return key;
  62. return NULL;
  63. }
  64. EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
  65. /**
  66. * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
  67. * @dev: Input device using sparse keymap
  68. * @keycode: Key code
  69. *
  70. * This function is used to perform &struct key_entry lookup in an
  71. * input device using sparse keymap.
  72. */
  73. struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
  74. unsigned int keycode)
  75. {
  76. struct key_entry *key;
  77. for (key = dev->keycode; key->type != KE_END; key++)
  78. if (key->type == KE_KEY && keycode == key->keycode)
  79. return key;
  80. return NULL;
  81. }
  82. EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
  83. static struct key_entry *sparse_keymap_locate(struct input_dev *dev,
  84. const struct input_keymap_entry *ke)
  85. {
  86. struct key_entry *key;
  87. unsigned int scancode;
  88. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  89. key = sparse_keymap_entry_by_index(dev, ke->index);
  90. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  91. key = sparse_keymap_entry_from_scancode(dev, scancode);
  92. else
  93. key = NULL;
  94. return key;
  95. }
  96. static int sparse_keymap_getkeycode(struct input_dev *dev,
  97. struct input_keymap_entry *ke)
  98. {
  99. const struct key_entry *key;
  100. if (dev->keycode) {
  101. key = sparse_keymap_locate(dev, ke);
  102. if (key && key->type == KE_KEY) {
  103. ke->keycode = key->keycode;
  104. if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
  105. ke->index =
  106. sparse_keymap_get_key_index(dev, key);
  107. ke->len = sizeof(key->code);
  108. memcpy(ke->scancode, &key->code, sizeof(key->code));
  109. return 0;
  110. }
  111. }
  112. return -EINVAL;
  113. }
  114. static int sparse_keymap_setkeycode(struct input_dev *dev,
  115. const struct input_keymap_entry *ke,
  116. unsigned int *old_keycode)
  117. {
  118. struct key_entry *key;
  119. if (dev->keycode) {
  120. key = sparse_keymap_locate(dev, ke);
  121. if (key && key->type == KE_KEY) {
  122. *old_keycode = key->keycode;
  123. key->keycode = ke->keycode;
  124. set_bit(ke->keycode, dev->keybit);
  125. if (!sparse_keymap_entry_from_keycode(dev, *old_keycode))
  126. clear_bit(*old_keycode, dev->keybit);
  127. return 0;
  128. }
  129. }
  130. return -EINVAL;
  131. }
  132. /**
  133. * sparse_keymap_setup - set up sparse keymap for an input device
  134. * @dev: Input device
  135. * @keymap: Keymap in form of array of &key_entry structures ending
  136. * with %KE_END type entry
  137. * @setup: Function that can be used to adjust keymap entries
  138. * depending on device's needs, may be %NULL
  139. *
  140. * The function calculates size and allocates copy of the original
  141. * keymap after which sets up input device event bits appropriately.
  142. * The allocated copy of the keymap is automatically freed when it
  143. * is no longer needed.
  144. */
  145. int sparse_keymap_setup(struct input_dev *dev,
  146. const struct key_entry *keymap,
  147. int (*setup)(struct input_dev *, struct key_entry *))
  148. {
  149. size_t map_size = 1; /* to account for the last KE_END entry */
  150. const struct key_entry *e;
  151. struct key_entry *map, *entry;
  152. int i;
  153. int error;
  154. for (e = keymap; e->type != KE_END; e++)
  155. map_size++;
  156. map = devm_kmemdup(&dev->dev, keymap, map_size * sizeof(*map),
  157. GFP_KERNEL);
  158. if (!map)
  159. return -ENOMEM;
  160. for (i = 0; i < map_size; i++) {
  161. entry = &map[i];
  162. if (setup) {
  163. error = setup(dev, entry);
  164. if (error)
  165. return error;
  166. }
  167. switch (entry->type) {
  168. case KE_KEY:
  169. __set_bit(EV_KEY, dev->evbit);
  170. __set_bit(entry->keycode, dev->keybit);
  171. break;
  172. case KE_SW:
  173. case KE_VSW:
  174. __set_bit(EV_SW, dev->evbit);
  175. __set_bit(entry->sw.code, dev->swbit);
  176. break;
  177. }
  178. }
  179. if (test_bit(EV_KEY, dev->evbit)) {
  180. __set_bit(KEY_UNKNOWN, dev->keybit);
  181. __set_bit(EV_MSC, dev->evbit);
  182. __set_bit(MSC_SCAN, dev->mscbit);
  183. }
  184. dev->keycode = map;
  185. dev->keycodemax = map_size;
  186. dev->getkeycode = sparse_keymap_getkeycode;
  187. dev->setkeycode = sparse_keymap_setkeycode;
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(sparse_keymap_setup);
  191. /**
  192. * sparse_keymap_report_entry - report event corresponding to given key entry
  193. * @dev: Input device for which event should be reported
  194. * @ke: key entry describing event
  195. * @value: Value that should be reported (ignored by %KE_SW entries)
  196. * @autorelease: Signals whether release event should be emitted for %KE_KEY
  197. * entries right after reporting press event, ignored by all other
  198. * entries
  199. *
  200. * This function is used to report input event described by given
  201. * &struct key_entry.
  202. */
  203. void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
  204. unsigned int value, bool autorelease)
  205. {
  206. switch (ke->type) {
  207. case KE_KEY:
  208. input_event(dev, EV_MSC, MSC_SCAN, ke->code);
  209. input_report_key(dev, ke->keycode, value);
  210. input_sync(dev);
  211. if (value && autorelease) {
  212. input_report_key(dev, ke->keycode, 0);
  213. input_sync(dev);
  214. }
  215. break;
  216. case KE_SW:
  217. value = ke->sw.value;
  218. /* fall through */
  219. case KE_VSW:
  220. input_report_switch(dev, ke->sw.code, value);
  221. input_sync(dev);
  222. break;
  223. }
  224. }
  225. EXPORT_SYMBOL(sparse_keymap_report_entry);
  226. /**
  227. * sparse_keymap_report_event - report event corresponding to given scancode
  228. * @dev: Input device using sparse keymap
  229. * @code: Scan code
  230. * @value: Value that should be reported (ignored by %KE_SW entries)
  231. * @autorelease: Signals whether release event should be emitted for %KE_KEY
  232. * entries right after reporting press event, ignored by all other
  233. * entries
  234. *
  235. * This function is used to perform lookup in an input device using sparse
  236. * keymap and report corresponding event. Returns %true if lookup was
  237. * successful and %false otherwise.
  238. */
  239. bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
  240. unsigned int value, bool autorelease)
  241. {
  242. const struct key_entry *ke =
  243. sparse_keymap_entry_from_scancode(dev, code);
  244. struct key_entry unknown_ke;
  245. if (ke) {
  246. sparse_keymap_report_entry(dev, ke, value, autorelease);
  247. return true;
  248. }
  249. /* Report an unknown key event as a debugging aid */
  250. unknown_ke.type = KE_KEY;
  251. unknown_ke.code = code;
  252. unknown_ke.keycode = KEY_UNKNOWN;
  253. sparse_keymap_report_entry(dev, &unknown_ke, value, true);
  254. return false;
  255. }
  256. EXPORT_SYMBOL(sparse_keymap_report_event);