sparse-keymap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. MODULE_VERSION("0.1");
  23. static unsigned int sparse_keymap_get_key_index(struct input_dev *dev,
  24. const struct key_entry *k)
  25. {
  26. struct key_entry *key;
  27. unsigned int idx = 0;
  28. for (key = dev->keycode; key->type != KE_END; key++) {
  29. if (key->type == KE_KEY) {
  30. if (key == k)
  31. break;
  32. idx++;
  33. }
  34. }
  35. return idx;
  36. }
  37. static struct key_entry *sparse_keymap_entry_by_index(struct input_dev *dev,
  38. unsigned int index)
  39. {
  40. struct key_entry *key;
  41. unsigned int key_cnt = 0;
  42. for (key = dev->keycode; key->type != KE_END; key++)
  43. if (key->type == KE_KEY)
  44. if (key_cnt++ == index)
  45. return key;
  46. return NULL;
  47. }
  48. /**
  49. * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
  50. * @dev: Input device using sparse keymap
  51. * @code: Scan code
  52. *
  53. * This function is used to perform &struct key_entry lookup in an
  54. * input device using sparse keymap.
  55. */
  56. struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
  57. unsigned int code)
  58. {
  59. struct key_entry *key;
  60. for (key = dev->keycode; key->type != KE_END; key++)
  61. if (code == key->code)
  62. return key;
  63. return NULL;
  64. }
  65. EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
  66. /**
  67. * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
  68. * @dev: Input device using sparse keymap
  69. * @keycode: Key code
  70. *
  71. * This function is used to perform &struct key_entry lookup in an
  72. * input device using sparse keymap.
  73. */
  74. struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
  75. unsigned int keycode)
  76. {
  77. struct key_entry *key;
  78. for (key = dev->keycode; key->type != KE_END; key++)
  79. if (key->type == KE_KEY && keycode == key->keycode)
  80. return key;
  81. return NULL;
  82. }
  83. EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
  84. static struct key_entry *sparse_keymap_locate(struct input_dev *dev,
  85. const struct input_keymap_entry *ke)
  86. {
  87. struct key_entry *key;
  88. unsigned int scancode;
  89. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  90. key = sparse_keymap_entry_by_index(dev, ke->index);
  91. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  92. key = sparse_keymap_entry_from_scancode(dev, scancode);
  93. else
  94. key = NULL;
  95. return key;
  96. }
  97. static int sparse_keymap_getkeycode(struct input_dev *dev,
  98. struct input_keymap_entry *ke)
  99. {
  100. const struct key_entry *key;
  101. if (dev->keycode) {
  102. key = sparse_keymap_locate(dev, ke);
  103. if (key && key->type == KE_KEY) {
  104. ke->keycode = key->keycode;
  105. if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
  106. ke->index =
  107. sparse_keymap_get_key_index(dev, key);
  108. ke->len = sizeof(key->code);
  109. memcpy(ke->scancode, &key->code, sizeof(key->code));
  110. return 0;
  111. }
  112. }
  113. return -EINVAL;
  114. }
  115. static int sparse_keymap_setkeycode(struct input_dev *dev,
  116. const struct input_keymap_entry *ke,
  117. unsigned int *old_keycode)
  118. {
  119. struct key_entry *key;
  120. if (dev->keycode) {
  121. key = sparse_keymap_locate(dev, ke);
  122. if (key && key->type == KE_KEY) {
  123. *old_keycode = key->keycode;
  124. key->keycode = ke->keycode;
  125. set_bit(ke->keycode, dev->keybit);
  126. if (!sparse_keymap_entry_from_keycode(dev, *old_keycode))
  127. clear_bit(*old_keycode, dev->keybit);
  128. return 0;
  129. }
  130. }
  131. return -EINVAL;
  132. }
  133. /**
  134. * sparse_keymap_setup - set up sparse keymap for an input device
  135. * @dev: Input device
  136. * @keymap: Keymap in form of array of &key_entry structures ending
  137. * with %KE_END type entry
  138. * @setup: Function that can be used to adjust keymap entries
  139. * depending on device's deeds, may be %NULL
  140. *
  141. * The function calculates size and allocates copy of the original
  142. * keymap after which sets up input device event bits appropriately.
  143. * Before destroying input device allocated keymap should be freed
  144. * with a call to sparse_keymap_free().
  145. */
  146. int sparse_keymap_setup(struct input_dev *dev,
  147. const struct key_entry *keymap,
  148. int (*setup)(struct input_dev *, struct key_entry *))
  149. {
  150. size_t map_size = 1; /* to account for the last KE_END entry */
  151. const struct key_entry *e;
  152. struct key_entry *map, *entry;
  153. int i;
  154. int error;
  155. for (e = keymap; e->type != KE_END; e++)
  156. map_size++;
  157. map = kcalloc(map_size, sizeof(struct key_entry), GFP_KERNEL);
  158. if (!map)
  159. return -ENOMEM;
  160. memcpy(map, keymap, map_size * sizeof(struct key_entry));
  161. for (i = 0; i < map_size; i++) {
  162. entry = &map[i];
  163. if (setup) {
  164. error = setup(dev, entry);
  165. if (error)
  166. goto err_out;
  167. }
  168. switch (entry->type) {
  169. case KE_KEY:
  170. __set_bit(EV_KEY, dev->evbit);
  171. __set_bit(entry->keycode, dev->keybit);
  172. break;
  173. case KE_SW:
  174. case KE_VSW:
  175. __set_bit(EV_SW, dev->evbit);
  176. __set_bit(entry->sw.code, dev->swbit);
  177. break;
  178. }
  179. }
  180. if (test_bit(EV_KEY, dev->evbit)) {
  181. __set_bit(KEY_UNKNOWN, dev->keybit);
  182. __set_bit(EV_MSC, dev->evbit);
  183. __set_bit(MSC_SCAN, dev->mscbit);
  184. }
  185. dev->keycode = map;
  186. dev->keycodemax = map_size;
  187. dev->getkeycode = sparse_keymap_getkeycode;
  188. dev->setkeycode = sparse_keymap_setkeycode;
  189. return 0;
  190. err_out:
  191. kfree(map);
  192. return error;
  193. }
  194. EXPORT_SYMBOL(sparse_keymap_setup);
  195. /**
  196. * sparse_keymap_free - free memory allocated for sparse keymap
  197. * @dev: Input device using sparse keymap
  198. *
  199. * This function is used to free memory allocated by sparse keymap
  200. * in an input device that was set up by sparse_keymap_setup().
  201. * NOTE: It is safe to cal this function while input device is
  202. * still registered (however the drivers should care not to try to
  203. * use freed keymap and thus have to shut off interrupts/polling
  204. * before freeing the keymap).
  205. */
  206. void sparse_keymap_free(struct input_dev *dev)
  207. {
  208. unsigned long flags;
  209. /*
  210. * Take event lock to prevent racing with input_get_keycode()
  211. * and input_set_keycode() if we are called while input device
  212. * is still registered.
  213. */
  214. spin_lock_irqsave(&dev->event_lock, flags);
  215. kfree(dev->keycode);
  216. dev->keycode = NULL;
  217. dev->keycodemax = 0;
  218. spin_unlock_irqrestore(&dev->event_lock, flags);
  219. }
  220. EXPORT_SYMBOL(sparse_keymap_free);
  221. /**
  222. * sparse_keymap_report_entry - report event corresponding to given key entry
  223. * @dev: Input device for which event should be reported
  224. * @ke: key entry describing event
  225. * @value: Value that should be reported (ignored by %KE_SW entries)
  226. * @autorelease: Signals whether release event should be emitted for %KE_KEY
  227. * entries right after reporting press event, ignored by all other
  228. * entries
  229. *
  230. * This function is used to report input event described by given
  231. * &struct key_entry.
  232. */
  233. void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
  234. unsigned int value, bool autorelease)
  235. {
  236. switch (ke->type) {
  237. case KE_KEY:
  238. input_event(dev, EV_MSC, MSC_SCAN, ke->code);
  239. input_report_key(dev, ke->keycode, value);
  240. input_sync(dev);
  241. if (value && autorelease) {
  242. input_report_key(dev, ke->keycode, 0);
  243. input_sync(dev);
  244. }
  245. break;
  246. case KE_SW:
  247. value = ke->sw.value;
  248. /* fall through */
  249. case KE_VSW:
  250. input_report_switch(dev, ke->sw.code, value);
  251. break;
  252. }
  253. }
  254. EXPORT_SYMBOL(sparse_keymap_report_entry);
  255. /**
  256. * sparse_keymap_report_event - report event corresponding to given scancode
  257. * @dev: Input device using sparse keymap
  258. * @code: Scan code
  259. * @value: Value that should be reported (ignored by %KE_SW entries)
  260. * @autorelease: Signals whether release event should be emitted for %KE_KEY
  261. * entries right after reporting press event, ignored by all other
  262. * entries
  263. *
  264. * This function is used to perform lookup in an input device using sparse
  265. * keymap and report corresponding event. Returns %true if lookup was
  266. * successful and %false otherwise.
  267. */
  268. bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
  269. unsigned int value, bool autorelease)
  270. {
  271. const struct key_entry *ke =
  272. sparse_keymap_entry_from_scancode(dev, code);
  273. struct key_entry unknown_ke;
  274. if (ke) {
  275. sparse_keymap_report_entry(dev, ke, value, autorelease);
  276. return true;
  277. }
  278. /* Report an unknown key event as a debugging aid */
  279. unknown_ke.type = KE_KEY;
  280. unknown_ke.code = code;
  281. unknown_ke.keycode = KEY_UNKNOWN;
  282. sparse_keymap_report_entry(dev, &unknown_ke, value, true);
  283. return false;
  284. }
  285. EXPORT_SYMBOL(sparse_keymap_report_event);