2 * $Id: hid-input.c,v 1.2 2002/04/23 00:59:25 rdamazio Exp $
4 * Copyright (c) 2000-2001 Vojtech Pavlik
6 * USB HID to Linux Input mapping
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32 #include <linux/input.h>
33 #include <linux/usb.h>
37 #define unk KEY_UNKNOWN
39 static unsigned char hid_keyboard[256] = {
40 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
41 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
42 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
43 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
44 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
45 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
46 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
47 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
48 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
49 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
50 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
51 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
52 unk,unk,unk,unk,unk,unk,unk,unk,unk,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 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
55 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
61 } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
63 static struct input_dev *find_input(struct hid_device *hid, struct hid_field *field)
66 struct hid_input *hidinput;
68 list_for_each (lh, &hid->inputs) {
71 hidinput = list_entry(lh, struct hid_input, list);
73 if (! hidinput->report)
76 for (i = 0; i < hidinput->report->maxfield; i++)
77 if (hidinput->report->field[i] == field)
78 return &hidinput->input;
81 /* Assume we only have one input and use it */
82 if (!list_empty(&hid->inputs)) {
83 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
84 return &hidinput->input;
87 /* This is really a bug */
91 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
92 struct hid_usage *usage)
94 struct input_dev *input = &hidinput->input;
95 struct hid_device *device = hidinput->input.private;
100 switch (usage->hid & HID_USAGE_PAGE) {
102 case HID_UP_KEYBOARD:
104 set_bit(EV_REP, input->evbit);
105 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
107 if ((usage->hid & HID_USAGE) < 256) {
108 if (!(usage->code = hid_keyboard[usage->hid & HID_USAGE]))
110 clear_bit(usage->code, bit);
112 usage->code = KEY_UNKNOWN;
118 usage->code = ((usage->hid - 1) & 0xf) + 0x100;
119 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
121 switch (field->application) {
122 case HID_GD_GAMEPAD: usage->code += 0x10;
123 case HID_GD_JOYSTICK: usage->code += 0x10;
124 case HID_GD_MOUSE: usage->code += 0x10; break;
126 if (field->physical == HID_GD_POINTER)
134 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
135 switch (usage->hid & 0xf) {
136 case 0x1: usage->code = KEY_POWER; break;
137 case 0x2: usage->code = KEY_SLEEP; break;
138 case 0x3: usage->code = KEY_WAKEUP; break;
139 default: usage->code = KEY_UNKNOWN; break;
141 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
145 usage->code = usage->hid & 0xf;
147 if (field->report_size == 1) {
148 usage->code = BTN_MISC;
149 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
153 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
154 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
158 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
160 if (usage->hid == HID_GD_HATSWITCH) {
161 usage->code = ABS_HAT0X;
162 usage->hat_min = field->logical_minimum;
163 usage->hat_max = field->logical_maximum;
169 usage->code = (usage->hid - 1) & 0xf;
170 usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
173 case HID_UP_DIGITIZER:
175 switch (usage->hid & 0xff) {
177 case 0x30: /* TipPressure */
179 if (!test_bit(BTN_TOUCH, input->keybit)) {
180 device->quirks |= HID_QUIRK_NOTOUCH;
181 set_bit(EV_KEY, input->evbit);
182 set_bit(BTN_TOUCH, input->keybit);
184 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
185 usage->code = ABS_PRESSURE;
186 clear_bit(usage->code, bit);
189 case 0x32: /* InRange */
191 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
192 switch (field->physical & 0xff) {
193 case 0x21: usage->code = BTN_TOOL_MOUSE; break;
194 case 0x22: usage->code = BTN_TOOL_FINGER; break;
195 default: usage->code = BTN_TOOL_PEN; break;
199 case 0x3c: /* Invert */
201 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
202 usage->code = BTN_TOOL_RUBBER;
203 clear_bit(usage->code, bit);
206 case 0x33: /* Touch */
207 case 0x42: /* TipSwitch */
208 case 0x43: /* TipSwitch2 */
210 device->quirks &= ~HID_QUIRK_NOTOUCH;
211 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
212 usage->code = BTN_TOUCH;
213 clear_bit(usage->code, bit);
216 case 0x44: /* BarrelSwitch */
218 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
219 usage->code = BTN_STYLUS;
220 clear_bit(usage->code, bit);
223 default: goto unknown;
227 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
229 set_bit(EV_REP, input->evbit);
230 switch (usage->hid & HID_USAGE) {
231 case 0x000: usage->code = 0; break;
232 case 0x034: usage->code = KEY_SLEEP; break;
233 case 0x036: usage->code = BTN_MISC; break;
234 case 0x08a: usage->code = KEY_WWW; break;
235 case 0x095: usage->code = KEY_HELP; break;
237 case 0x0b0: usage->code = KEY_PLAY; break;
238 case 0x0b1: usage->code = KEY_PAUSE; break;
239 case 0x0b2: usage->code = KEY_RECORD; break;
240 case 0x0b3: usage->code = KEY_FASTFORWARD; break;
241 case 0x0b4: usage->code = KEY_REWIND; break;
242 case 0x0b5: usage->code = KEY_NEXTSONG; break;
243 case 0x0b6: usage->code = KEY_PREVIOUSSONG; break;
244 case 0x0b7: usage->code = KEY_STOPCD; break;
245 case 0x0b8: usage->code = KEY_EJECTCD; break;
246 case 0x0cd: usage->code = KEY_PLAYPAUSE; break;
247 case 0x0e0: is_abs = 1;
248 usage->code = ABS_VOLUME;
250 case 0x0e2: usage->code = KEY_MUTE; break;
251 case 0x0e5: usage->code = KEY_BASSBOOST; break;
252 case 0x0e9: usage->code = KEY_VOLUMEUP; break;
253 case 0x0ea: usage->code = KEY_VOLUMEDOWN; break;
255 case 0x183: usage->code = KEY_CONFIG; break;
256 case 0x18a: usage->code = KEY_MAIL; break;
257 case 0x192: usage->code = KEY_CALC; break;
258 case 0x194: usage->code = KEY_FILE; break;
259 case 0x21a: usage->code = KEY_UNDO; break;
260 case 0x21b: usage->code = KEY_COPY; break;
261 case 0x21c: usage->code = KEY_CUT; break;
262 case 0x21d: usage->code = KEY_PASTE; break;
264 case 0x221: usage->code = KEY_FIND; break;
265 case 0x223: usage->code = KEY_HOMEPAGE; break;
266 case 0x224: usage->code = KEY_BACK; break;
267 case 0x225: usage->code = KEY_FORWARD; break;
268 case 0x226: usage->code = KEY_STOP; break;
269 case 0x227: usage->code = KEY_REFRESH; break;
270 case 0x22a: usage->code = KEY_BOOKMARKS; break;
272 default: usage->code = KEY_UNKNOWN; break;
276 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
278 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
282 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
284 set_bit(EV_REP, input->evbit);
285 switch (usage->hid & HID_USAGE) {
286 case 0x021: usage->code = KEY_PRINT; break;
287 case 0x070: usage->code = KEY_HP; break;
288 case 0x071: usage->code = KEY_CAMERA; break;
289 case 0x072: usage->code = KEY_SOUND; break;
290 case 0x073: usage->code = KEY_QUESTION; break;
292 case 0x080: usage->code = KEY_EMAIL; break;
293 case 0x081: usage->code = KEY_CHAT; break;
294 case 0x082: usage->code = KEY_SEARCH; break;
295 case 0x083: usage->code = KEY_CONNECT; break;
296 case 0x084: usage->code = KEY_FINANCE; break;
297 case 0x085: usage->code = KEY_SPORT; break;
298 case 0x086: usage->code = KEY_SHOP; break;
300 default: usage->code = KEY_UNKNOWN; break;
304 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
309 usage->type = EV_FF; bit = input->ffbit; max = FF_MAX;
311 switch(usage->hid & HID_USAGE) {
312 case 0x26: set_bit(FF_CONSTANT, input->ffbit); break;
313 case 0x27: set_bit(FF_RAMP, input->ffbit); break;
314 case 0x28: set_bit(FF_CUSTOM, input->ffbit); break;
315 case 0x30: set_bit(FF_SQUARE, input->ffbit);
316 set_bit(FF_PERIODIC, input->ffbit); break;
317 case 0x31: set_bit(FF_SINE, input->ffbit);
318 set_bit(FF_PERIODIC, input->ffbit); break;
319 case 0x32: set_bit(FF_TRIANGLE, input->ffbit);
320 set_bit(FF_PERIODIC, input->ffbit); break;
321 case 0x33: set_bit(FF_SAW_UP, input->ffbit);
322 set_bit(FF_PERIODIC, input->ffbit); break;
323 case 0x34: set_bit(FF_SAW_DOWN, input->ffbit);
324 set_bit(FF_PERIODIC, input->ffbit); break;
325 case 0x40: set_bit(FF_SPRING, input->ffbit); break;
326 case 0x41: set_bit(FF_DAMPER, input->ffbit); break;
327 case 0x42: set_bit(FF_INERTIA , input->ffbit); break;
328 case 0x43: set_bit(FF_FRICTION, input->ffbit); break;
329 case 0x7e: usage->code = FF_GAIN; break;
330 case 0x83: /* Simultaneous Effects Max */
331 input->ff_effects_max = (field->value[0]);
332 dbg("Maximum Effects - %d",input->ff_effects_max);
334 case 0x98: /* Device Control */
335 usage->code = FF_AUTOCENTER; break;
336 case 0xa4: /* Safety Switch */
337 usage->code = BTN_DEAD;
339 usage->type = EV_KEY;
341 dbg("Safety Switch Report\n");
343 case 0x9f: /* Device Paused */
344 case 0xa0: /* Actuators Enabled */
345 dbg("Not telling the input API about ");
346 resolv_usage(usage->hid);
352 resolv_usage(usage->hid);
354 if (field->report_size == 1) {
356 if (field->report->type == HID_OUTPUT_REPORT) {
357 usage->code = LED_MISC;
358 usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
362 usage->code = BTN_MISC;
363 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
367 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
368 usage->code = REL_MISC;
369 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
373 usage->code = ABS_MISC;
374 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
378 set_bit(usage->type, input->evbit);
379 if ((usage->type == EV_REL)
380 && (device->quirks & (HID_QUIRK_2WHEEL_MOUSE_HACK_BACK
381 | HID_QUIRK_2WHEEL_MOUSE_HACK_EXTRA))
382 && (usage->code == REL_WHEEL)) {
383 set_bit(REL_HWHEEL, bit);
386 while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
387 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
390 if (usage->code > max)
393 if (usage->type == EV_ABS) {
394 int a = field->logical_minimum;
395 int b = field->logical_maximum;
397 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
398 a = field->logical_minimum = 0;
399 b = field->logical_maximum = 255;
402 input->absmin[usage->code] = a;
403 input->absmax[usage->code] = b;
404 input->absfuzz[usage->code] = 0;
405 input->absflat[usage->code] = 0;
407 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK) {
408 input->absfuzz[usage->code] = (b - a) >> 8;
409 input->absflat[usage->code] = (b - a) >> 4;
413 if (usage->hat_min != usage->hat_max) {
415 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
416 input->absmax[i] = 1;
417 input->absmin[i] = -1;
418 input->absfuzz[i] = 0;
419 input->absflat[i] = 0;
421 set_bit(usage->code + 1, input->absbit);
425 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs)
427 struct input_dev *input = find_input(hid, field);
428 int *quirks = &hid->quirks;
433 input_regs(input, regs);
435 if (((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_EXTRA) && (usage->code == BTN_EXTRA))
436 || ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_BACK) && (usage->code == BTN_BACK))) {
438 hid->quirks |= HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
440 hid->quirks &= ~HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
444 if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_ON)
445 && (usage->code == REL_WHEEL)) {
446 input_event(input, usage->type, REL_HWHEEL, value);
450 if (usage->hat_min != usage->hat_max ) { /* FIXME: hat_max can be 0 and hat_min 1 */
451 value = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
452 if (value < 0 || value > 8) value = 0;
453 input_event(input, usage->type, usage->code , hid_hat_to_axis[value].x);
454 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[value].y);
458 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
459 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
463 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
465 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
468 input_event(input, usage->type, usage->code, 0);
469 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
473 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
474 int a = field->logical_minimum;
475 int b = field->logical_maximum;
476 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
479 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
480 input->ff_effects_max = value;
481 dbg("Maximum Effects - %d",input->ff_effects_max);
484 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
485 dbg("PID Pool Report\n");
489 if((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
492 input_event(input, usage->type, usage->code, value);
494 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
495 input_event(input, usage->type, usage->code, 0);
498 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
500 struct list_head *lh;
501 struct hid_input *hidinput;
503 list_for_each (lh, &hid->inputs) {
504 hidinput = list_entry(lh, struct hid_input, list);
505 input_sync(&hidinput->input);
509 static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
511 struct hid_device *hid = dev->private;
512 struct hid_field *field = NULL;
516 return hid_ff_event(hid, dev, type, code, value);
518 if ((offset = hid_find_field(hid, type, code, &field)) == -1) {
519 warn("event field not found");
523 hid_set_field(field, offset, value);
524 hid_submit_report(hid, field->report, USB_DIR_OUT);
529 static int hidinput_open(struct input_dev *dev)
531 struct hid_device *hid = dev->private;
532 return hid_open(hid);
535 static void hidinput_close(struct input_dev *dev)
537 struct hid_device *hid = dev->private;
542 * Register the input device; print a message.
543 * Configure the input layer interface
544 * Read all reports and initialize the absolute field values.
547 int hidinput_connect(struct hid_device *hid)
549 struct usb_device *dev = hid->dev;
550 struct hid_report_enum *report_enum;
551 struct hid_report *report;
552 struct list_head *list;
553 struct hid_input *hidinput = NULL;
556 INIT_LIST_HEAD(&hid->inputs);
558 for (i = 0; i < hid->maxcollection; i++)
559 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
560 IS_INPUT_APPLICATION(hid->collection[i].usage))
563 if (i == hid->maxcollection)
566 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
567 report_enum = hid->report_enum + k;
568 list = report_enum->report_list.next;
569 while (list != &report_enum->report_list) {
570 report = (struct hid_report *) list;
572 if (!report->maxfield) {
578 hidinput = kmalloc(sizeof(*hidinput), GFP_KERNEL);
580 err("Out of memory during hid input probe");
583 memset(hidinput, 0, sizeof(*hidinput));
585 list_add_tail(&hidinput->list, &hid->inputs);
587 hidinput->input.private = hid;
588 hidinput->input.event = hidinput_input_event;
589 hidinput->input.open = hidinput_open;
590 hidinput->input.close = hidinput_close;
592 hidinput->input.name = hid->name;
593 hidinput->input.phys = hid->phys;
594 hidinput->input.uniq = hid->uniq;
595 hidinput->input.id.bustype = BUS_USB;
596 hidinput->input.id.vendor = dev->descriptor.idVendor;
597 hidinput->input.id.product = dev->descriptor.idProduct;
598 hidinput->input.id.version = dev->descriptor.bcdDevice;
599 hidinput->input.dev = &hid->intf->dev;
602 for (i = 0; i < report->maxfield; i++)
603 for (j = 0; j < report->field[i]->maxusage; j++)
604 hidinput_configure_usage(hidinput, report->field[i],
605 report->field[i]->usage + j);
607 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
608 /* This will leave hidinput NULL, so that it
609 * allocates another one if we have more inputs on
610 * the same interface. Some devices (e.g. Happ's
611 * UGCI) cram a lot of unrelated inputs into the
613 hidinput->report = report;
614 input_register_device(&hidinput->input);
622 /* This only gets called when we are a single-input (most of the
623 * time). IOW, not a HID_QUIRK_MULTI_INPUT. The hid_ff_init() is
624 * only useful in this case, and not for multi-input quirks. */
627 input_register_device(&hidinput->input);
633 void hidinput_disconnect(struct hid_device *hid)
635 struct list_head *lh, *next;
636 struct hid_input *hidinput;
638 list_for_each_safe (lh, next, &hid->inputs) {
639 hidinput = list_entry(lh, struct hid_input, list);
640 input_unregister_device(&hidinput->input);
641 list_del(&hidinput->list);