hid-icade.c 5.9 KB

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