hid-icade.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ION iCade input driver
  4. *
  5. * Copyright (c) 2012 Bastien Nocera <[email protected]>
  6. * Copyright (c) 2012 Benjamin Tissoires <[email protected]>
  7. */
  8. /*
  9. */
  10. #include <linux/device.h>
  11. #include <linux/hid.h>
  12. #include <linux/module.h>
  13. #include "hid-ids.h"
  14. /*
  15. * ↑ A C Y L
  16. * ← →
  17. * ↓ B X Z R
  18. *
  19. *
  20. * UP ON,OFF = w,e
  21. * RT ON,OFF = d,c
  22. * DN ON,OFF = x,z
  23. * LT ON,OFF = a,q
  24. * A ON,OFF = y,t
  25. * B ON,OFF = h,r
  26. * C ON,OFF = u,f
  27. * X ON,OFF = j,n
  28. * Y ON,OFF = i,m
  29. * Z ON,OFF = k,p
  30. * L ON,OFF = o,g
  31. * R ON,OFF = l,v
  32. */
  33. /* The translation code uses HID usage instead of input layer
  34. * keys. This code generates a lookup table that makes
  35. * translation quick.
  36. *
  37. * #include <linux/input.h>
  38. * #include <stdio.h>
  39. * #include <assert.h>
  40. *
  41. * #define unk KEY_UNKNOWN
  42. *
  43. * < copy of hid_keyboard[] from hid-input.c >
  44. *
  45. * struct icade_key_translation {
  46. * int from;
  47. * const char *to;
  48. * int press;
  49. * };
  50. *
  51. * static const struct icade_key_translation icade_keys[] = {
  52. * { KEY_W, "KEY_UP", 1 },
  53. * { KEY_E, "KEY_UP", 0 },
  54. * { KEY_D, "KEY_RIGHT", 1 },
  55. * { KEY_C, "KEY_RIGHT", 0 },
  56. * { KEY_X, "KEY_DOWN", 1 },
  57. * { KEY_Z, "KEY_DOWN", 0 },
  58. * { KEY_A, "KEY_LEFT", 1 },
  59. * { KEY_Q, "KEY_LEFT", 0 },
  60. * { KEY_Y, "BTN_A", 1 },
  61. * { KEY_T, "BTN_A", 0 },
  62. * { KEY_H, "BTN_B", 1 },
  63. * { KEY_R, "BTN_B", 0 },
  64. * { KEY_U, "BTN_C", 1 },
  65. * { KEY_F, "BTN_C", 0 },
  66. * { KEY_J, "BTN_X", 1 },
  67. * { KEY_N, "BTN_X", 0 },
  68. * { KEY_I, "BTN_Y", 1 },
  69. * { KEY_M, "BTN_Y", 0 },
  70. * { KEY_K, "BTN_Z", 1 },
  71. * { KEY_P, "BTN_Z", 0 },
  72. * { KEY_O, "BTN_THUMBL", 1 },
  73. * { KEY_G, "BTN_THUMBL", 0 },
  74. * { KEY_L, "BTN_THUMBR", 1 },
  75. * { KEY_V, "BTN_THUMBR", 0 },
  76. *
  77. * { }
  78. * };
  79. *
  80. * static int
  81. * usage_for_key (int key)
  82. * {
  83. * int i;
  84. * for (i = 0; i < 256; i++) {
  85. * if (hid_keyboard[i] == key)
  86. * return i;
  87. * }
  88. * assert(0);
  89. * }
  90. *
  91. * int main (int argc, char **argv)
  92. * {
  93. * const struct icade_key_translation *trans;
  94. * int max_usage = 0;
  95. *
  96. * for (trans = icade_keys; trans->from; trans++) {
  97. * int usage = usage_for_key (trans->from);
  98. * max_usage = usage > max_usage ? usage : max_usage;
  99. * }
  100. *
  101. * printf ("#define ICADE_MAX_USAGE %d\n\n", max_usage);
  102. * printf ("struct icade_key {\n");
  103. * printf ("\tu16 to;\n");
  104. * printf ("\tu8 press:1;\n");
  105. * printf ("};\n\n");
  106. * printf ("static const struct icade_key "
  107. * "icade_usage_table[%d] = {\n", max_usage + 1);
  108. * for (trans = icade_keys; trans->from; trans++) {
  109. * printf ("\t[%d] = { %s, %d },\n",
  110. * usage_for_key (trans->from), trans->to, trans->press);
  111. * }
  112. * printf ("};\n");
  113. *
  114. * return 0;
  115. * }
  116. */
  117. #define ICADE_MAX_USAGE 29
  118. struct icade_key {
  119. u16 to;
  120. u8 press:1;
  121. };
  122. static const struct icade_key icade_usage_table[30] = {
  123. [26] = { KEY_UP, 1 },
  124. [8] = { KEY_UP, 0 },
  125. [7] = { KEY_RIGHT, 1 },
  126. [6] = { KEY_RIGHT, 0 },
  127. [27] = { KEY_DOWN, 1 },
  128. [29] = { KEY_DOWN, 0 },
  129. [4] = { KEY_LEFT, 1 },
  130. [20] = { KEY_LEFT, 0 },
  131. [28] = { BTN_A, 1 },
  132. [23] = { BTN_A, 0 },
  133. [11] = { BTN_B, 1 },
  134. [21] = { BTN_B, 0 },
  135. [24] = { BTN_C, 1 },
  136. [9] = { BTN_C, 0 },
  137. [13] = { BTN_X, 1 },
  138. [17] = { BTN_X, 0 },
  139. [12] = { BTN_Y, 1 },
  140. [16] = { BTN_Y, 0 },
  141. [14] = { BTN_Z, 1 },
  142. [19] = { BTN_Z, 0 },
  143. [18] = { BTN_THUMBL, 1 },
  144. [10] = { BTN_THUMBL, 0 },
  145. [15] = { BTN_THUMBR, 1 },
  146. [25] = { BTN_THUMBR, 0 },
  147. };
  148. static const struct icade_key *icade_find_translation(u16 from)
  149. {
  150. if (from > ICADE_MAX_USAGE)
  151. return NULL;
  152. return &icade_usage_table[from];
  153. }
  154. static int icade_event(struct hid_device *hdev, struct hid_field *field,
  155. struct hid_usage *usage, __s32 value)
  156. {
  157. const struct icade_key *trans;
  158. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  159. !usage->type)
  160. return 0;
  161. /* We ignore the fake key up, and act only on key down */
  162. if (!value)
  163. return 1;
  164. trans = icade_find_translation(usage->hid & HID_USAGE);
  165. if (!trans)
  166. return 1;
  167. input_event(field->hidinput->input, usage->type,
  168. trans->to, trans->press);
  169. return 1;
  170. }
  171. static int icade_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  172. struct hid_field *field, struct hid_usage *usage,
  173. unsigned long **bit, int *max)
  174. {
  175. const struct icade_key *trans;
  176. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_KEYBOARD) {
  177. trans = icade_find_translation(usage->hid & HID_USAGE);
  178. if (!trans)
  179. return -1;
  180. hid_map_usage(hi, usage, bit, max, EV_KEY, trans->to);
  181. set_bit(trans->to, hi->input->keybit);
  182. return 1;
  183. }
  184. /* ignore others */
  185. return -1;
  186. }
  187. static int icade_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  188. struct hid_field *field, struct hid_usage *usage,
  189. unsigned long **bit, int *max)
  190. {
  191. if (usage->type == EV_KEY)
  192. set_bit(usage->type, hi->input->evbit);
  193. return -1;
  194. }
  195. static const struct hid_device_id icade_devices[] = {
  196. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(hid, icade_devices);
  200. static struct hid_driver icade_driver = {
  201. .name = "icade",
  202. .id_table = icade_devices,
  203. .event = icade_event,
  204. .input_mapped = icade_input_mapped,
  205. .input_mapping = icade_input_mapping,
  206. };
  207. module_hid_driver(icade_driver);
  208. MODULE_LICENSE("GPL");
  209. MODULE_AUTHOR("Bastien Nocera <[email protected]>");
  210. MODULE_DESCRIPTION("ION iCade input driver");