2 * ION iCade input driver
4 * Copyright (c) 2012 Bastien Nocera <hadess@hadess.net>
5 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/device.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
41 /* The translation code uses HID usage instead of input layer
42 * keys. This code generates a lookup table that makes
45 * #include <linux/input.h>
49 * #define unk KEY_UNKNOWN
51 * < copy of hid_keyboard[] from hid-input.c >
53 * struct icade_key_translation {
59 * static const struct icade_key_translation icade_keys[] = {
60 * { KEY_W, "KEY_UP", 1 },
61 * { KEY_E, "KEY_UP", 0 },
62 * { KEY_D, "KEY_RIGHT", 1 },
63 * { KEY_C, "KEY_RIGHT", 0 },
64 * { KEY_X, "KEY_DOWN", 1 },
65 * { KEY_Z, "KEY_DOWN", 0 },
66 * { KEY_A, "KEY_LEFT", 1 },
67 * { KEY_Q, "KEY_LEFT", 0 },
68 * { KEY_Y, "BTN_A", 1 },
69 * { KEY_T, "BTN_A", 0 },
70 * { KEY_H, "BTN_B", 1 },
71 * { KEY_R, "BTN_B", 0 },
72 * { KEY_U, "BTN_C", 1 },
73 * { KEY_F, "BTN_C", 0 },
74 * { KEY_J, "BTN_X", 1 },
75 * { KEY_N, "BTN_X", 0 },
76 * { KEY_I, "BTN_Y", 1 },
77 * { KEY_M, "BTN_Y", 0 },
78 * { KEY_K, "BTN_Z", 1 },
79 * { KEY_P, "BTN_Z", 0 },
80 * { KEY_O, "BTN_THUMBL", 1 },
81 * { KEY_G, "BTN_THUMBL", 0 },
82 * { KEY_L, "BTN_THUMBR", 1 },
83 * { KEY_V, "BTN_THUMBR", 0 },
89 * usage_for_key (int key)
92 * for (i = 0; i < 256; i++) {
93 * if (hid_keyboard[i] == key)
99 * int main (int argc, char **argv)
101 * const struct icade_key_translation *trans;
104 * for (trans = icade_keys; trans->from; trans++) {
105 * int usage = usage_for_key (trans->from);
106 * max_usage = usage > max_usage ? usage : max_usage;
109 * printf ("#define ICADE_MAX_USAGE %d\n\n", max_usage);
110 * printf ("struct icade_key {\n");
111 * printf ("\tu16 to;\n");
112 * printf ("\tu8 press:1;\n");
114 * printf ("static const struct icade_key "
115 * "icade_usage_table[%d] = {\n", max_usage + 1);
116 * for (trans = icade_keys; trans->from; trans++) {
117 * printf ("\t[%d] = { %s, %d },\n",
118 * usage_for_key (trans->from), trans->to, trans->press);
126 #define ICADE_MAX_USAGE 29
133 static const struct icade_key icade_usage_table[30] = {
134 [26] = { KEY_UP, 1 },
136 [7] = { KEY_RIGHT, 1 },
137 [6] = { KEY_RIGHT, 0 },
138 [27] = { KEY_DOWN, 1 },
139 [29] = { KEY_DOWN, 0 },
140 [4] = { KEY_LEFT, 1 },
141 [20] = { KEY_LEFT, 0 },
154 [18] = { BTN_THUMBL, 1 },
155 [10] = { BTN_THUMBL, 0 },
156 [15] = { BTN_THUMBR, 1 },
157 [25] = { BTN_THUMBR, 0 },
160 static const struct icade_key *icade_find_translation(u16 from)
162 if (from < 0 || from > ICADE_MAX_USAGE)
164 return &icade_usage_table[from];
167 static int icade_event(struct hid_device *hdev, struct hid_field *field,
168 struct hid_usage *usage, __s32 value)
170 const struct icade_key *trans;
172 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
176 /* We ignore the fake key up, and act only on key down */
180 trans = icade_find_translation(usage->hid & HID_USAGE);
185 input_event(field->hidinput->input, usage->type,
186 trans->to, trans->press);
191 static int icade_input_mapping(struct hid_device *hdev, struct hid_input *hi,
192 struct hid_field *field, struct hid_usage *usage,
193 unsigned long **bit, int *max)
195 const struct icade_key *trans;
197 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_KEYBOARD) {
198 trans = icade_find_translation(usage->hid & HID_USAGE);
203 hid_map_usage(hi, usage, bit, max, EV_KEY, trans->to);
204 set_bit(trans->to, hi->input->keybit);
214 static int icade_input_mapped(struct hid_device *hdev, struct hid_input *hi,
215 struct hid_field *field, struct hid_usage *usage,
216 unsigned long **bit, int *max)
218 if (usage->type == EV_KEY)
219 set_bit(usage->type, hi->input->evbit);
224 static const struct hid_device_id icade_devices[] = {
225 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) },
229 MODULE_DEVICE_TABLE(hid, icade_devices);
231 static struct hid_driver icade_driver = {
233 .id_table = icade_devices,
234 .event = icade_event,
235 .input_mapped = icade_input_mapped,
236 .input_mapping = icade_input_mapping,
239 static int __init icade_init(void)
243 ret = hid_register_driver(&icade_driver);
245 pr_err("can't register icade driver\n");
250 static void __exit icade_exit(void)
252 hid_unregister_driver(&icade_driver);
255 module_init(icade_init);
256 module_exit(icade_exit);
257 MODULE_LICENSE("GPL");
258 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
259 MODULE_DESCRIPTION("ION iCade input driver");