2 * $Id: hid-input.c,v 1.5 2001/05/23 09:25:02 vojtech Exp $
4 * Copyright (c) 2000-2001 Vojtech Pavlik
6 * USB HID to Linux Input mapping module
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/kernel.h>
35 #include <linux/input.h>
36 #include <linux/usb.h>
40 #define unk KEY_UNKNOWN
42 static unsigned char hid_keyboard[256] = {
43 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
44 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
45 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
46 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
47 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
48 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
49 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
50 120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
51 115,114,unk,unk,unk,124,unk,181,182,183,184,185,186,187,188,189,
52 190,191,192,193,194,195,196,197,198,unk,unk,unk,unk,unk,unk,unk,
53 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
54 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
55 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
56 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
57 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
58 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
64 } hid_hat_to_axis[] = {{0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
66 static struct input_dev *find_input(struct hid_device *hid, struct hid_field *field)
69 struct hid_input *hidinput;
71 list_for_each (lh, &hid->inputs) {
74 hidinput = list_entry(lh, struct hid_input, list);
76 if (! hidinput->report)
79 for (i = 0; i < hidinput->report->maxfield; i++)
80 if (hidinput->report->field[i] == field)
81 return &hidinput->input;
84 /* Assume we only have one input and use it */
85 if (!list_empty(&hid->inputs)) {
86 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
87 return &hidinput->input;
90 /* This is really a bug */
94 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
95 struct hid_usage *usage)
97 struct input_dev *input = &hidinput->input;
98 struct hid_device *device = hidinput->input.private;
102 switch (usage->hid & HID_USAGE_PAGE) {
104 case HID_UP_KEYBOARD:
106 set_bit(EV_REP, input->evbit);
107 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
109 if ((usage->hid & HID_USAGE) < 256) {
110 if (!(usage->code = hid_keyboard[usage->hid & HID_USAGE]))
112 clear_bit(usage->code, bit);
114 usage->code = KEY_UNKNOWN;
120 usage->code = ((usage->hid - 1) & 0xf) + 0x100;
121 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
123 switch (field->application) {
124 case HID_GD_GAMEPAD: usage->code += 0x10;
125 case HID_GD_JOYSTICK: usage->code += 0x10;
126 case HID_GD_MOUSE: usage->code += 0x10; break;
128 if (field->physical == HID_GD_POINTER)
136 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
137 switch (usage->hid & 0xf) {
138 case 0x1: usage->code = KEY_POWER; break;
139 case 0x2: usage->code = KEY_SLEEP; break;
140 case 0x3: usage->code = KEY_WAKEUP; break;
141 default: usage->code = KEY_UNKNOWN; break;
143 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
147 usage->code = usage->hid & 0xf;
149 if (field->report_size == 1) {
150 usage->code = BTN_MISC;
151 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
155 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
156 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
160 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
162 if (usage->hid == HID_GD_HATSWITCH) {
163 usage->code = ABS_HAT0X;
164 usage->hat_min = field->logical_minimum;
165 usage->hat_max = field->logical_maximum;
171 usage->code = (usage->hid - 1) & 0xf;
172 usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
175 case HID_UP_DIGITIZER:
177 switch (usage->hid & 0xff) {
179 case 0x30: /* TipPressure */
181 if (!test_bit(BTN_TOUCH, input->keybit)) {
182 device->quirks |= HID_QUIRK_NOTOUCH;
183 set_bit(EV_KEY, input->evbit);
184 set_bit(BTN_TOUCH, input->keybit);
186 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
187 usage->code = ABS_PRESSURE;
188 clear_bit(usage->code, bit);
191 case 0x32: /* InRange */
193 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
194 switch (field->physical & 0xff) {
195 case 0x21: usage->code = BTN_TOOL_MOUSE; break;
196 case 0x22: usage->code = BTN_TOOL_FINGER; break;
197 default: usage->code = BTN_TOOL_PEN; break;
201 case 0x3c: /* Invert */
203 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
204 usage->code = BTN_TOOL_RUBBER;
205 clear_bit(usage->code, bit);
208 case 0x33: /* Touch */
209 case 0x42: /* TipSwitch */
210 case 0x43: /* TipSwitch2 */
212 device->quirks &= ~HID_QUIRK_NOTOUCH;
213 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
214 usage->code = BTN_TOUCH;
215 clear_bit(usage->code, bit);
218 case 0x44: /* BarrelSwitch */
220 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
221 usage->code = BTN_STYLUS;
222 clear_bit(usage->code, bit);
225 default: goto unknown;
229 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
231 switch (usage->hid & HID_USAGE) {
232 case 0x000: usage->code = 0; break;
233 case 0x034: usage->code = KEY_SLEEP; break;
234 case 0x036: usage->code = BTN_MISC; break;
235 case 0x08a: usage->code = KEY_WWW; break;
236 case 0x095: usage->code = KEY_HELP; break;
238 case 0x0b4: usage->code = KEY_REWIND; break;
239 case 0x0b5: usage->code = KEY_NEXTSONG; break;
240 case 0x0b6: usage->code = KEY_PREVIOUSSONG; break;
241 case 0x0b7: usage->code = KEY_STOPCD; break;
242 case 0x0b8: usage->code = KEY_EJECTCD; break;
243 case 0x0cd: usage->code = KEY_PLAYPAUSE; break;
245 case 0x0e2: usage->code = KEY_MUTE; break;
246 case 0x0e9: usage->code = KEY_VOLUMEUP; break;
247 case 0x0ea: usage->code = KEY_VOLUMEDOWN; break;
249 case 0x183: usage->code = KEY_CONFIG; break;
250 case 0x18a: usage->code = KEY_MAIL; break;
251 case 0x192: usage->code = KEY_CALC; break;
252 case 0x194: usage->code = KEY_FILE; break;
254 case 0x21a: usage->code = KEY_UNDO; break;
255 case 0x21b: usage->code = KEY_COPY; break;
256 case 0x21c: usage->code = KEY_CUT; break;
257 case 0x21d: usage->code = KEY_PASTE; break;
259 case 0x221: usage->code = KEY_FIND; break;
260 case 0x223: usage->code = KEY_HOMEPAGE; break;
261 case 0x224: usage->code = KEY_BACK; break;
262 case 0x225: usage->code = KEY_FORWARD; break;
263 case 0x226: usage->code = KEY_STOP; break;
264 case 0x227: usage->code = KEY_REFRESH; break;
265 case 0x22a: usage->code = KEY_BOOKMARKS; break;
267 default: usage->code = KEY_UNKNOWN; break;
271 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
277 if (field->report_size == 1) {
279 if (field->report->type == HID_OUTPUT_REPORT) {
280 usage->code = LED_MISC;
281 usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
285 usage->code = BTN_MISC;
286 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
290 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
291 usage->code = REL_MISC;
292 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
296 usage->code = ABS_MISC;
297 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
301 set_bit(usage->type, input->evbit);
303 while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
304 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
307 if (usage->code > max) return;
309 if (usage->type == EV_ABS) {
310 int a = field->logical_minimum;
311 int b = field->logical_maximum;
313 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
314 a = field->logical_minimum = 0;
315 b = field->logical_maximum = 255;
318 input->absmin[usage->code] = a;
319 input->absmax[usage->code] = b;
320 input->absfuzz[usage->code] = 0;
321 input->absflat[usage->code] = 0;
323 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK) {
324 input->absfuzz[usage->code] = (b - a) >> 8;
325 input->absflat[usage->code] = (b - a) >> 4;
329 if (usage->hat_min != usage->hat_max) {
331 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
332 input->absmax[i] = 1;
333 input->absmin[i] = -1;
334 input->absfuzz[i] = 0;
335 input->absflat[i] = 0;
337 set_bit(usage->code + 1, input->absbit);
341 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
343 struct input_dev *input = find_input(hid, field);
344 int *quirks = &hid->quirks;
349 if (usage->hat_min != usage->hat_max) {
350 value = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
351 if (value < 0 || value > 8) value = 0;
352 input_event(input, usage->type, usage->code , hid_hat_to_axis[value].x);
353 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[value].y);
357 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
358 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
362 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
364 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
367 input_event(input, usage->type, usage->code, 0);
368 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
372 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
373 int a = field->logical_minimum;
374 int b = field->logical_maximum;
375 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
378 if((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UKNOWN */
381 input_event(input, usage->type, usage->code, value);
383 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
384 input_event(input, usage->type, usage->code, 0);
387 static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
389 struct hid_device *hid = dev->private;
390 struct hid_field *field = NULL;
393 if ((offset = hid_find_field(hid, type, code, &field)) == -1) {
394 warn("event field not found");
398 hid_set_field(field, offset, value);
399 hid_write_report(hid, field->report);
404 static int hidinput_open(struct input_dev *dev)
406 struct hid_device *hid = dev->private;
407 return hid_open(hid);
410 static void hidinput_close(struct input_dev *dev)
412 struct hid_device *hid = dev->private;
417 * Register the input device; print a message.
418 * Configure the input layer interface
419 * Read all reports and initalize the absoulte field values.
422 int hidinput_connect(struct hid_device *hid)
424 struct usb_device *dev = hid->dev;
425 struct hid_report_enum *report_enum;
426 struct hid_report *report;
427 struct list_head *list;
428 struct hid_input *hidinput = NULL;
431 INIT_LIST_HEAD(&hid->inputs);
433 for (i = 0; i < hid->maxcollection; i++)
434 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
435 IS_INPUT_APPLICATION(hid->collection[i].usage))
438 if (i == hid->maxcollection)
441 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
442 report_enum = hid->report_enum + k;
443 list = report_enum->report_list.next;
444 while (list != &report_enum->report_list) {
445 report = (struct hid_report *) list;
447 if (!report->maxfield) {
453 hidinput = kmalloc(sizeof(*hidinput), GFP_KERNEL);
455 err("Out of memory during hid input probe");
458 memset(hidinput, 0, sizeof(*hidinput));
459 list_add_tail(&hidinput->list, &hid->inputs);
461 hidinput->input.private = hid;
462 hidinput->input.event = hidinput_input_event;
463 hidinput->input.open = hidinput_open;
464 hidinput->input.close = hidinput_close;
466 hidinput->input.name = hid->name;
467 hidinput->input.idbus = BUS_USB;
468 hidinput->input.idvendor = dev->descriptor.idVendor;
469 hidinput->input.idproduct = dev->descriptor.idProduct;
470 hidinput->input.idversion = dev->descriptor.bcdDevice;
473 for (i = 0; i < report->maxfield; i++)
474 for (j = 0; j < report->field[i]->maxusage; j++)
475 hidinput_configure_usage(hidinput, report->field[i],
476 report->field[i]->usage + j);
478 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
479 /* This will leave hidinput NULL, so that it
480 * allocates another one if we have more inputs on
481 * the same interface. Some devices (e.g. Happ's
482 * UGCI) cram a lot of unrelated inputs into the
484 hidinput->report = report;
485 input_register_device(&hidinput->input);
494 input_register_device(&hidinput->input);
499 void hidinput_disconnect(struct hid_device *hid)
501 struct list_head *lh, *next;
502 struct hid_input *hidinput;
504 list_for_each_safe (lh, next, &hid->inputs) {
505 hidinput = list_entry(lh, struct hid_input, list);
506 input_unregister_device(&hidinput->input);
507 list_del(&hidinput->list);