Merge branches 'x86/xen', 'x86/build', 'x86/microcode', 'x86/mm-debug-v2', 'x86/memor...
[pandora-kernel.git] / drivers / hid / hid-input.c
1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *  Copyright (c) 2006-2007 Jiri Kosina
4  *
5  *  HID to Linux Input mapping
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * Should you need to contact me, the author, you can do so either by
24  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26  */
27
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31
32 #include <linux/hid.h>
33 #include <linux/hid-debug.h>
34
35 static int hid_apple_fnmode = 1;
36 module_param_named(pb_fnmode, hid_apple_fnmode, int, 0644);
37 MODULE_PARM_DESC(pb_fnmode,
38                 "Mode of fn key on Apple keyboards (0 = disabled, 1 = fkeyslast, 2 = fkeysfirst)");
39
40 #define unk     KEY_UNKNOWN
41
42 static const 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, 43, 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,183,184,185,186,187,188,189,190,
50         191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
51         115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
52         122,123, 90, 91, 85,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         unk,unk,unk,unk,unk,unk,179,180,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
59 };
60
61 /* extended mapping for certain Logitech hardware (Logitech cordless desktop LX500) */
62 #define LOGITECH_EXPANDED_KEYMAP_SIZE 80
63 static int logitech_expanded_keymap[LOGITECH_EXPANDED_KEYMAP_SIZE] = {
64           0,216,  0,213,175,156,  0,  0,  0,  0,
65         144,  0,  0,  0,  0,  0,  0,  0,  0,212,
66         174,167,152,161,112,  0,  0,  0,154,  0,
67           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
68           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
69           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
70           0,  0,  0,  0,  0,183,184,185,186,187,
71         188,189,190,191,192,193,194,  0,  0,  0
72 };
73
74 static const struct {
75         __s32 x;
76         __s32 y;
77 }  hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
78
79 #define map_abs(c)      do { usage->code = c; usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX; } while (0)
80 #define map_rel(c)      do { usage->code = c; usage->type = EV_REL; bit = input->relbit; max = REL_MAX; } while (0)
81 #define map_key(c)      do { usage->code = c; usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX; } while (0)
82 #define map_led(c)      do { usage->code = c; usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; } while (0)
83
84 #define map_abs_clear(c)        do { map_abs(c); clear_bit(c, bit); } while (0)
85 #define map_key_clear(c)        do { map_key(c); clear_bit(c, bit); } while (0)
86
87 #ifdef CONFIG_USB_HIDINPUT_POWERBOOK
88
89 struct hidinput_key_translation {
90         u16 from;
91         u16 to;
92         u8 flags;
93 };
94
95 #define APPLE_FLAG_FKEY 0x01
96
97 static struct hidinput_key_translation apple_fn_keys[] = {
98         { KEY_BACKSPACE, KEY_DELETE },
99         { KEY_F1,       KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
100         { KEY_F2,       KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
101         { KEY_F3,       KEY_FN_F5,          APPLE_FLAG_FKEY }, /* Exposé */
102         { KEY_F4,       KEY_FN_F4,          APPLE_FLAG_FKEY }, /* Dashboard */
103         { KEY_F5,       KEY_KBDILLUMDOWN,   APPLE_FLAG_FKEY },
104         { KEY_F6,       KEY_KBDILLUMUP,     APPLE_FLAG_FKEY },
105         { KEY_F7,       KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
106         { KEY_F8,       KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
107         { KEY_F9,       KEY_NEXTSONG,       APPLE_FLAG_FKEY },
108         { KEY_F10,      KEY_MUTE,           APPLE_FLAG_FKEY },
109         { KEY_F11,      KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
110         { KEY_F12,      KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
111         { KEY_UP,       KEY_PAGEUP },
112         { KEY_DOWN,     KEY_PAGEDOWN },
113         { KEY_LEFT,     KEY_HOME },
114         { KEY_RIGHT,    KEY_END },
115         { }
116 };
117
118 static struct hidinput_key_translation powerbook_fn_keys[] = {
119         { KEY_BACKSPACE, KEY_DELETE },
120         { KEY_F1,       KEY_BRIGHTNESSDOWN,     APPLE_FLAG_FKEY },
121         { KEY_F2,       KEY_BRIGHTNESSUP,       APPLE_FLAG_FKEY },
122         { KEY_F3,       KEY_MUTE,               APPLE_FLAG_FKEY },
123         { KEY_F4,       KEY_VOLUMEDOWN,         APPLE_FLAG_FKEY },
124         { KEY_F5,       KEY_VOLUMEUP,           APPLE_FLAG_FKEY },
125         { KEY_F6,       KEY_NUMLOCK,            APPLE_FLAG_FKEY },
126         { KEY_F7,       KEY_SWITCHVIDEOMODE,    APPLE_FLAG_FKEY },
127         { KEY_F8,       KEY_KBDILLUMTOGGLE,     APPLE_FLAG_FKEY },
128         { KEY_F9,       KEY_KBDILLUMDOWN,       APPLE_FLAG_FKEY },
129         { KEY_F10,      KEY_KBDILLUMUP,         APPLE_FLAG_FKEY },
130         { KEY_UP,       KEY_PAGEUP },
131         { KEY_DOWN,     KEY_PAGEDOWN },
132         { KEY_LEFT,     KEY_HOME },
133         { KEY_RIGHT,    KEY_END },
134         { }
135 };
136
137 static struct hidinput_key_translation powerbook_numlock_keys[] = {
138         { KEY_J,        KEY_KP1 },
139         { KEY_K,        KEY_KP2 },
140         { KEY_L,        KEY_KP3 },
141         { KEY_U,        KEY_KP4 },
142         { KEY_I,        KEY_KP5 },
143         { KEY_O,        KEY_KP6 },
144         { KEY_7,        KEY_KP7 },
145         { KEY_8,        KEY_KP8 },
146         { KEY_9,        KEY_KP9 },
147         { KEY_M,        KEY_KP0 },
148         { KEY_DOT,      KEY_KPDOT },
149         { KEY_SLASH,    KEY_KPPLUS },
150         { KEY_SEMICOLON, KEY_KPMINUS },
151         { KEY_P,        KEY_KPASTERISK },
152         { KEY_MINUS,    KEY_KPEQUAL },
153         { KEY_0,        KEY_KPSLASH },
154         { KEY_F6,       KEY_NUMLOCK },
155         { KEY_KPENTER,  KEY_KPENTER },
156         { KEY_BACKSPACE, KEY_BACKSPACE },
157         { }
158 };
159
160 static struct hidinput_key_translation apple_iso_keyboard[] = {
161         { KEY_GRAVE,    KEY_102ND },
162         { KEY_102ND,    KEY_GRAVE },
163         { }
164 };
165
166 static struct hidinput_key_translation *find_translation(struct hidinput_key_translation *table, u16 from)
167 {
168         struct hidinput_key_translation *trans;
169
170         /* Look for the translation */
171         for (trans = table; trans->from; trans++)
172                 if (trans->from == from)
173                         return trans;
174
175         return NULL;
176 }
177
178 int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
179                 struct hid_usage *usage, __s32 value)
180 {
181         struct hidinput_key_translation *trans;
182
183         if (usage->code == KEY_FN) {
184                 if (value) hid->quirks |=  HID_QUIRK_APPLE_FN_ON;
185                 else       hid->quirks &= ~HID_QUIRK_APPLE_FN_ON;
186
187                 input_event(input, usage->type, usage->code, value);
188
189                 return 1;
190         }
191
192         if (hid_apple_fnmode) {
193                 int do_translate;
194
195                 trans = find_translation((hid->product < 0x220 ||
196                                           hid->product >= 0x300) ?
197                                          powerbook_fn_keys : apple_fn_keys,
198                                          usage->code);
199                 if (trans) {
200                         if (test_bit(usage->code, hid->apple_pressed_fn))
201                                 do_translate = 1;
202                         else if (trans->flags & APPLE_FLAG_FKEY)
203                                 do_translate =
204                                         (hid_apple_fnmode == 2 &&  (hid->quirks & HID_QUIRK_APPLE_FN_ON)) ||
205                                         (hid_apple_fnmode == 1 && !(hid->quirks & HID_QUIRK_APPLE_FN_ON));
206                         else
207                                 do_translate = (hid->quirks & HID_QUIRK_APPLE_FN_ON);
208
209                         if (do_translate) {
210                                 if (value)
211                                         set_bit(usage->code, hid->apple_pressed_fn);
212                                 else
213                                         clear_bit(usage->code, hid->apple_pressed_fn);
214
215                                 input_event(input, usage->type, trans->to, value);
216
217                                 return 1;
218                         }
219                 }
220
221                 if (hid->quirks & HID_QUIRK_APPLE_NUMLOCK_EMULATION && (
222                                 test_bit(usage->code, hid->pb_pressed_numlock) ||
223                                 test_bit(LED_NUML, input->led))) {
224                         trans = find_translation(powerbook_numlock_keys, usage->code);
225
226                         if (trans) {
227                                 if (value)
228                                         set_bit(usage->code, hid->pb_pressed_numlock);
229                                 else
230                                         clear_bit(usage->code, hid->pb_pressed_numlock);
231
232                                 input_event(input, usage->type, trans->to, value);
233                         }
234
235                         return 1;
236                 }
237         }
238
239         if (hid->quirks & HID_QUIRK_APPLE_ISO_KEYBOARD) {
240                 trans = find_translation(apple_iso_keyboard, usage->code);
241                 if (trans) {
242                         input_event(input, usage->type, trans->to, value);
243                         return 1;
244                 }
245         }
246
247         return 0;
248 }
249
250 static void hidinput_apple_setup(struct input_dev *input)
251 {
252         struct hidinput_key_translation *trans;
253
254         set_bit(KEY_NUMLOCK, input->keybit);
255
256         /* Enable all needed keys */
257         for (trans = apple_fn_keys; trans->from; trans++)
258                 set_bit(trans->to, input->keybit);
259
260         for (trans = powerbook_fn_keys; trans->from; trans++)
261                 set_bit(trans->to, input->keybit);
262
263         for (trans = powerbook_numlock_keys; trans->from; trans++)
264                 set_bit(trans->to, input->keybit);
265
266         for (trans = apple_iso_keyboard; trans->from; trans++)
267                 set_bit(trans->to, input->keybit);
268
269 }
270 #else
271 inline int hidinput_apple_event(struct hid_device *hid,
272                                        struct input_dev *input,
273                                        struct hid_usage *usage, __s32 value)
274 {
275         return 0;
276 }
277
278 static inline void hidinput_apple_setup(struct input_dev *input)
279 {
280 }
281 #endif
282
283 static inline int match_scancode(int code, int scancode)
284 {
285         if (scancode == 0)
286                 return 1;
287         return ((code & (HID_USAGE_PAGE | HID_USAGE)) == scancode);
288 }
289
290 static inline int match_keycode(int code, int keycode)
291 {
292         if (keycode == 0)
293                 return 1;
294         return (code == keycode);
295 }
296
297 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
298                 int scancode, int keycode)
299 {
300         int i, j, k;
301         struct hid_report *report;
302         struct hid_usage *usage;
303
304         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
305                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
306                         for (i = 0; i < report->maxfield; i++) {
307                                 for ( j = 0; j < report->field[i]->maxusage; j++) {
308                                         usage = report->field[i]->usage + j;
309                                         if (usage->type == EV_KEY &&
310                                                 match_scancode(usage->hid, scancode) &&
311                                                 match_keycode(usage->code, keycode))
312                                                 return usage;
313                                 }
314                         }
315                 }
316         }
317         return NULL;
318 }
319
320 static int hidinput_getkeycode(struct input_dev *dev, int scancode,
321                                 int *keycode)
322 {
323         struct hid_device *hid = input_get_drvdata(dev);
324         struct hid_usage *usage;
325
326         usage = hidinput_find_key(hid, scancode, 0);
327         if (usage) {
328                 *keycode = usage->code;
329                 return 0;
330         }
331         return -EINVAL;
332 }
333
334 static int hidinput_setkeycode(struct input_dev *dev, int scancode,
335                                 int keycode)
336 {
337         struct hid_device *hid = input_get_drvdata(dev);
338         struct hid_usage *usage;
339         int old_keycode;
340
341         if (keycode < 0 || keycode > KEY_MAX)
342                 return -EINVAL;
343
344         usage = hidinput_find_key(hid, scancode, 0);
345         if (usage) {
346                 old_keycode = usage->code;
347                 usage->code = keycode;
348
349                 clear_bit(old_keycode, dev->keybit);
350                 set_bit(usage->code, dev->keybit);
351                 dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
352                 /* Set the keybit for the old keycode if the old keycode is used
353                  * by another key */
354                 if (hidinput_find_key (hid, 0, old_keycode))
355                         set_bit(old_keycode, dev->keybit);
356
357                 return 0;
358         }
359
360         return -EINVAL;
361 }
362
363
364 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
365                                      struct hid_usage *usage)
366 {
367         struct input_dev *input = hidinput->input;
368         struct hid_device *device = input_get_drvdata(input);
369         int max = 0, code, ret;
370         unsigned long *bit = NULL;
371
372         field->hidinput = hidinput;
373
374         dbg_hid("Mapping: ");
375         hid_resolv_usage(usage->hid);
376         dbg_hid_line(" ---> ");
377
378         if (field->flags & HID_MAIN_ITEM_CONSTANT)
379                 goto ignore;
380
381         /* only LED usages are supported in output fields */
382         if (field->report_type == HID_OUTPUT_REPORT &&
383                         (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
384                 dbg_hid_line(" [non-LED output field] ");
385                 goto ignore;
386         }
387
388         /* handle input mappings for quirky devices */
389         ret = hidinput_mapping_quirks(usage, input, &bit, &max);
390         if (ret)
391                 goto mapped;
392
393         switch (usage->hid & HID_USAGE_PAGE) {
394
395                 case HID_UP_UNDEFINED:
396                         goto ignore;
397
398                 case HID_UP_KEYBOARD:
399
400                         set_bit(EV_REP, input->evbit);
401
402                         if ((usage->hid & HID_USAGE) < 256) {
403                                 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
404                                 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
405                         } else
406                                 map_key(KEY_UNKNOWN);
407
408                         break;
409
410                 case HID_UP_BUTTON:
411
412                         code = ((usage->hid - 1) & 0xf);
413
414                         switch (field->application) {
415                                 case HID_GD_MOUSE:
416                                 case HID_GD_POINTER:  code += 0x110; break;
417                                 case HID_GD_JOYSTICK: code += 0x120; break;
418                                 case HID_GD_GAMEPAD:  code += 0x130; break;
419                                 default:
420                                         switch (field->physical) {
421                                                 case HID_GD_MOUSE:
422                                                 case HID_GD_POINTER:  code += 0x110; break;
423                                                 case HID_GD_JOYSTICK: code += 0x120; break;
424                                                 case HID_GD_GAMEPAD:  code += 0x130; break;
425                                                 default:              code += 0x100;
426                                         }
427                         }
428
429                         /* Special handling for Logitech Cordless Desktop */
430                         if (field->application != HID_GD_MOUSE) {
431                                 if (device->quirks & HID_QUIRK_LOGITECH_EXPANDED_KEYMAP) {
432                                         int hid = usage->hid & HID_USAGE;
433                                         if (hid < LOGITECH_EXPANDED_KEYMAP_SIZE && logitech_expanded_keymap[hid] != 0)
434                                                 code = logitech_expanded_keymap[hid];
435                                 }
436                         } else {
437                                 if (device->quirks & HID_QUIRK_LOGITECH_IGNORE_DOUBLED_WHEEL) {
438                                         int hid = usage->hid & HID_USAGE;
439                                         if (hid == 7 || hid == 8)
440                                                 goto ignore;
441                                 }
442                         }
443
444                         map_key(code);
445                         break;
446
447
448                 case HID_UP_SIMULATION:
449
450                         switch (usage->hid & 0xffff) {
451                                 case 0xba: map_abs(ABS_RUDDER);   break;
452                                 case 0xbb: map_abs(ABS_THROTTLE); break;
453                                 case 0xc4: map_abs(ABS_GAS);      break;
454                                 case 0xc5: map_abs(ABS_BRAKE);    break;
455                                 case 0xc8: map_abs(ABS_WHEEL);    break;
456                                 default:   goto ignore;
457                         }
458                         break;
459
460                 case HID_UP_GENDESK:
461
462                         if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
463                                 switch (usage->hid & 0xf) {
464                                         case 0x1: map_key_clear(KEY_POWER);  break;
465                                         case 0x2: map_key_clear(KEY_SLEEP);  break;
466                                         case 0x3: map_key_clear(KEY_WAKEUP); break;
467                                         default: goto unknown;
468                                 }
469                                 break;
470                         }
471
472                         if ((usage->hid & 0xf0) == 0x90) {      /* D-pad */
473                                 switch (usage->hid) {
474                                         case HID_GD_UP:    usage->hat_dir = 1; break;
475                                         case HID_GD_DOWN:  usage->hat_dir = 5; break;
476                                         case HID_GD_RIGHT: usage->hat_dir = 3; break;
477                                         case HID_GD_LEFT:  usage->hat_dir = 7; break;
478                                         default: goto unknown;
479                                 }
480                                 if (field->dpad) {
481                                         map_abs(field->dpad);
482                                         goto ignore;
483                                 }
484                                 map_abs(ABS_HAT0X);
485                                 break;
486                         }
487
488                         switch (usage->hid) {
489
490                                 /* These usage IDs map directly to the usage codes. */
491                                 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
492                                 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
493                                 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
494                                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
495                                                 map_rel(usage->hid & 0xf);
496                                         else
497                                                 map_abs(usage->hid & 0xf);
498                                         break;
499
500                                 case HID_GD_HATSWITCH:
501                                         usage->hat_min = field->logical_minimum;
502                                         usage->hat_max = field->logical_maximum;
503                                         map_abs(ABS_HAT0X);
504                                         break;
505
506                                 case HID_GD_START:      map_key_clear(BTN_START);       break;
507                                 case HID_GD_SELECT:     map_key_clear(BTN_SELECT);      break;
508
509                                 default: goto unknown;
510                         }
511
512                         break;
513
514                 case HID_UP_LED:
515
516                         switch (usage->hid & 0xffff) {                        /* HID-Value:                   */
517                                 case 0x01:  map_led (LED_NUML);     break;    /*   "Num Lock"                 */
518                                 case 0x02:  map_led (LED_CAPSL);    break;    /*   "Caps Lock"                */
519                                 case 0x03:  map_led (LED_SCROLLL);  break;    /*   "Scroll Lock"              */
520                                 case 0x04:  map_led (LED_COMPOSE);  break;    /*   "Compose"                  */
521                                 case 0x05:  map_led (LED_KANA);     break;    /*   "Kana"                     */
522                                 case 0x27:  map_led (LED_SLEEP);    break;    /*   "Stand-By"                 */
523                                 case 0x4c:  map_led (LED_SUSPEND);  break;    /*   "System Suspend"           */
524                                 case 0x09:  map_led (LED_MUTE);     break;    /*   "Mute"                     */
525                                 case 0x4b:  map_led (LED_MISC);     break;    /*   "Generic Indicator"        */
526                                 case 0x19:  map_led (LED_MAIL);     break;    /*   "Message Waiting"          */
527                                 case 0x4d:  map_led (LED_CHARGING); break;    /*   "External Power Connected" */
528
529                                 default: goto ignore;
530                         }
531                         break;
532
533                 case HID_UP_DIGITIZER:
534
535                         switch (usage->hid & 0xff) {
536
537                                 case 0x30: /* TipPressure */
538                                         if (!test_bit(BTN_TOUCH, input->keybit)) {
539                                                 device->quirks |= HID_QUIRK_NOTOUCH;
540                                                 set_bit(EV_KEY, input->evbit);
541                                                 set_bit(BTN_TOUCH, input->keybit);
542                                         }
543
544                                         map_abs_clear(ABS_PRESSURE);
545                                         break;
546
547                                 case 0x32: /* InRange */
548                                         switch (field->physical & 0xff) {
549                                                 case 0x21: map_key(BTN_TOOL_MOUSE); break;
550                                                 case 0x22: map_key(BTN_TOOL_FINGER); break;
551                                                 default: map_key(BTN_TOOL_PEN); break;
552                                         }
553                                         break;
554
555                                 case 0x3c: /* Invert */
556                                         map_key_clear(BTN_TOOL_RUBBER);
557                                         break;
558
559                                 case 0x33: /* Touch */
560                                 case 0x42: /* TipSwitch */
561                                 case 0x43: /* TipSwitch2 */
562                                         device->quirks &= ~HID_QUIRK_NOTOUCH;
563                                         map_key_clear(BTN_TOUCH);
564                                         break;
565
566                                 case 0x44: /* BarrelSwitch */
567                                         map_key_clear(BTN_STYLUS);
568                                         break;
569
570                                 default:  goto unknown;
571                         }
572                         break;
573
574                 case HID_UP_CONSUMER:   /* USB HUT v1.1, pages 56-62 */
575
576                         switch (usage->hid & HID_USAGE) {
577                                 case 0x000: goto ignore;
578                                 case 0x034: map_key_clear(KEY_SLEEP);           break;
579                                 case 0x036: map_key_clear(BTN_MISC);            break;
580
581                                 case 0x040: map_key_clear(KEY_MENU);            break;
582                                 case 0x045: map_key_clear(KEY_RADIO);           break;
583
584                                 case 0x083: map_key_clear(KEY_LAST);            break;
585                                 case 0x088: map_key_clear(KEY_PC);              break;
586                                 case 0x089: map_key_clear(KEY_TV);              break;
587                                 case 0x08a: map_key_clear(KEY_WWW);             break;
588                                 case 0x08b: map_key_clear(KEY_DVD);             break;
589                                 case 0x08c: map_key_clear(KEY_PHONE);           break;
590                                 case 0x08d: map_key_clear(KEY_PROGRAM);         break;
591                                 case 0x08e: map_key_clear(KEY_VIDEOPHONE);      break;
592                                 case 0x08f: map_key_clear(KEY_GAMES);           break;
593                                 case 0x090: map_key_clear(KEY_MEMO);            break;
594                                 case 0x091: map_key_clear(KEY_CD);              break;
595                                 case 0x092: map_key_clear(KEY_VCR);             break;
596                                 case 0x093: map_key_clear(KEY_TUNER);           break;
597                                 case 0x094: map_key_clear(KEY_EXIT);            break;
598                                 case 0x095: map_key_clear(KEY_HELP);            break;
599                                 case 0x096: map_key_clear(KEY_TAPE);            break;
600                                 case 0x097: map_key_clear(KEY_TV2);             break;
601                                 case 0x098: map_key_clear(KEY_SAT);             break;
602                                 case 0x09a: map_key_clear(KEY_PVR);             break;
603
604                                 case 0x09c: map_key_clear(KEY_CHANNELUP);       break;
605                                 case 0x09d: map_key_clear(KEY_CHANNELDOWN);     break;
606                                 case 0x0a0: map_key_clear(KEY_VCR2);            break;
607
608                                 case 0x0b0: map_key_clear(KEY_PLAY);            break;
609                                 case 0x0b1: map_key_clear(KEY_PAUSE);           break;
610                                 case 0x0b2: map_key_clear(KEY_RECORD);          break;
611                                 case 0x0b3: map_key_clear(KEY_FASTFORWARD);     break;
612                                 case 0x0b4: map_key_clear(KEY_REWIND);          break;
613                                 case 0x0b5: map_key_clear(KEY_NEXTSONG);        break;
614                                 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
615                                 case 0x0b7: map_key_clear(KEY_STOPCD);          break;
616                                 case 0x0b8: map_key_clear(KEY_EJECTCD);         break;
617                                 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
618
619                                 case 0x0cd: map_key_clear(KEY_PLAYPAUSE);       break;
620                                 case 0x0e0: map_abs_clear(ABS_VOLUME);          break;
621                                 case 0x0e2: map_key_clear(KEY_MUTE);            break;
622                                 case 0x0e5: map_key_clear(KEY_BASSBOOST);       break;
623                                 case 0x0e9: map_key_clear(KEY_VOLUMEUP);        break;
624                                 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);      break;
625
626                                 case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
627                                 case 0x183: map_key_clear(KEY_CONFIG);          break;
628                                 case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
629                                 case 0x185: map_key_clear(KEY_EDITOR);          break;
630                                 case 0x186: map_key_clear(KEY_SPREADSHEET);     break;
631                                 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);  break;
632                                 case 0x188: map_key_clear(KEY_PRESENTATION);    break;
633                                 case 0x189: map_key_clear(KEY_DATABASE);        break;
634                                 case 0x18a: map_key_clear(KEY_MAIL);            break;
635                                 case 0x18b: map_key_clear(KEY_NEWS);            break;
636                                 case 0x18c: map_key_clear(KEY_VOICEMAIL);       break;
637                                 case 0x18d: map_key_clear(KEY_ADDRESSBOOK);     break;
638                                 case 0x18e: map_key_clear(KEY_CALENDAR);        break;
639                                 case 0x191: map_key_clear(KEY_FINANCE);         break;
640                                 case 0x192: map_key_clear(KEY_CALC);            break;
641                                 case 0x194: map_key_clear(KEY_FILE);            break;
642                                 case 0x196: map_key_clear(KEY_WWW);             break;
643                                 case 0x19c: map_key_clear(KEY_LOGOFF);          break;
644                                 case 0x19e: map_key_clear(KEY_COFFEE);          break;
645                                 case 0x1a6: map_key_clear(KEY_HELP);            break;
646                                 case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
647                                 case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
648                                 case 0x1b6: map_key_clear(KEY_MEDIA);           break;
649                                 case 0x1b7: map_key_clear(KEY_SOUND);           break;
650                                 case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
651                                 case 0x1bd: map_key_clear(KEY_INFO);            break;
652                                 case 0x201: map_key_clear(KEY_NEW);             break;
653                                 case 0x202: map_key_clear(KEY_OPEN);            break;
654                                 case 0x203: map_key_clear(KEY_CLOSE);           break;
655                                 case 0x204: map_key_clear(KEY_EXIT);            break;
656                                 case 0x207: map_key_clear(KEY_SAVE);            break;
657                                 case 0x208: map_key_clear(KEY_PRINT);           break;
658                                 case 0x209: map_key_clear(KEY_PROPS);           break;
659                                 case 0x21a: map_key_clear(KEY_UNDO);            break;
660                                 case 0x21b: map_key_clear(KEY_COPY);            break;
661                                 case 0x21c: map_key_clear(KEY_CUT);             break;
662                                 case 0x21d: map_key_clear(KEY_PASTE);           break;
663                                 case 0x21f: map_key_clear(KEY_FIND);            break;
664                                 case 0x221: map_key_clear(KEY_SEARCH);          break;
665                                 case 0x222: map_key_clear(KEY_GOTO);            break;
666                                 case 0x223: map_key_clear(KEY_HOMEPAGE);        break;
667                                 case 0x224: map_key_clear(KEY_BACK);            break;
668                                 case 0x225: map_key_clear(KEY_FORWARD);         break;
669                                 case 0x226: map_key_clear(KEY_STOP);            break;
670                                 case 0x227: map_key_clear(KEY_REFRESH);         break;
671                                 case 0x22a: map_key_clear(KEY_BOOKMARKS);       break;
672                                 case 0x22d: map_key_clear(KEY_ZOOMIN);          break;
673                                 case 0x22e: map_key_clear(KEY_ZOOMOUT);         break;
674                                 case 0x22f: map_key_clear(KEY_ZOOMRESET);       break;
675                                 case 0x233: map_key_clear(KEY_SCROLLUP);        break;
676                                 case 0x234: map_key_clear(KEY_SCROLLDOWN);      break;
677                                 case 0x238: map_rel(REL_HWHEEL);                break;
678                                 case 0x25f: map_key_clear(KEY_CANCEL);          break;
679                                 case 0x279: map_key_clear(KEY_REDO);            break;
680
681                                 case 0x289: map_key_clear(KEY_REPLY);           break;
682                                 case 0x28b: map_key_clear(KEY_FORWARDMAIL);     break;
683                                 case 0x28c: map_key_clear(KEY_SEND);            break;
684
685                                 default:    goto ignore;
686                         }
687                         break;
688
689                 case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
690
691                         set_bit(EV_REP, input->evbit);
692                         switch (usage->hid & HID_USAGE) {
693                                 case 0x021: map_key_clear(KEY_PRINT);           break;
694                                 case 0x070: map_key_clear(KEY_HP);              break;
695                                 case 0x071: map_key_clear(KEY_CAMERA);          break;
696                                 case 0x072: map_key_clear(KEY_SOUND);           break;
697                                 case 0x073: map_key_clear(KEY_QUESTION);        break;
698                                 case 0x080: map_key_clear(KEY_EMAIL);           break;
699                                 case 0x081: map_key_clear(KEY_CHAT);            break;
700                                 case 0x082: map_key_clear(KEY_SEARCH);          break;
701                                 case 0x083: map_key_clear(KEY_CONNECT);         break;
702                                 case 0x084: map_key_clear(KEY_FINANCE);         break;
703                                 case 0x085: map_key_clear(KEY_SPORT);           break;
704                                 case 0x086: map_key_clear(KEY_SHOP);            break;
705                                 default:    goto ignore;
706                         }
707                         break;
708
709                 case HID_UP_MSVENDOR:
710
711                         goto ignore;
712
713                 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
714
715                         set_bit(EV_REP, input->evbit);
716                         switch(usage->hid & HID_USAGE) {
717                                 case 0x003:
718                                         /* The fn key on Apple USB keyboards */
719                                         map_key_clear(KEY_FN);
720                                         hidinput_apple_setup(input);
721                                         break;
722
723                                 default:    goto ignore;
724                         }
725                         break;
726
727                 case HID_UP_LOGIVENDOR:
728
729                         goto ignore;
730                 
731                 case HID_UP_PID:
732
733                         switch(usage->hid & HID_USAGE) {
734                                 case 0xa4: map_key_clear(BTN_DEAD);     break;
735                                 default: goto ignore;
736                         }
737                         break;
738
739                 default:
740                 unknown:
741                         if (field->report_size == 1) {
742                                 if (field->report->type == HID_OUTPUT_REPORT) {
743                                         map_led(LED_MISC);
744                                         break;
745                                 }
746                                 map_key(BTN_MISC);
747                                 break;
748                         }
749                         if (field->flags & HID_MAIN_ITEM_RELATIVE) {
750                                 map_rel(REL_MISC);
751                                 break;
752                         }
753                         map_abs(ABS_MISC);
754                         break;
755         }
756
757 mapped:
758         if (device->quirks & HID_QUIRK_MIGHTYMOUSE) {
759                 if (usage->hid == HID_GD_Z)
760                         map_rel(REL_HWHEEL);
761                 else if (usage->code == BTN_1)
762                         map_key(BTN_2);
763                 else if (usage->code == BTN_2)
764                         map_key(BTN_1);
765         }
766
767         if ((device->quirks & (HID_QUIRK_2WHEEL_MOUSE_HACK_7 | HID_QUIRK_2WHEEL_MOUSE_HACK_5 |
768                         HID_QUIRK_2WHEEL_MOUSE_HACK_B8)) && (usage->type == EV_REL) &&
769                         (usage->code == REL_WHEEL))
770                 set_bit(REL_HWHEEL, bit);
771
772         if (((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
773                 || ((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007)))
774                 goto ignore;
775
776         if ((device->quirks & HID_QUIRK_BAD_RELATIVE_KEYS) &&
777                 usage->type == EV_KEY && (field->flags & HID_MAIN_ITEM_RELATIVE))
778                 field->flags &= ~HID_MAIN_ITEM_RELATIVE;
779
780         set_bit(usage->type, input->evbit);
781
782         if (device->quirks & HID_QUIRK_DUPLICATE_USAGES &&
783                         (usage->type == EV_KEY ||
784                          usage->type == EV_REL ||
785                          usage->type == EV_ABS))
786                 clear_bit(usage->code, bit);
787
788         while (usage->code <= max && test_and_set_bit(usage->code, bit))
789                 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
790
791         if (usage->code > max)
792                 goto ignore;
793
794
795         if (usage->type == EV_ABS) {
796
797                 int a = field->logical_minimum;
798                 int b = field->logical_maximum;
799
800                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
801                         a = field->logical_minimum = 0;
802                         b = field->logical_maximum = 255;
803                 }
804
805                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
806                         input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
807                 else    input_set_abs_params(input, usage->code, a, b, 0, 0);
808
809         }
810
811         if (usage->type == EV_ABS &&
812             (usage->hat_min < usage->hat_max || usage->hat_dir)) {
813                 int i;
814                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
815                         input_set_abs_params(input, i, -1, 1, 0, 0);
816                         set_bit(i, input->absbit);
817                 }
818                 if (usage->hat_dir && !field->dpad)
819                         field->dpad = usage->code;
820         }
821
822         /* for those devices which produce Consumer volume usage as relative,
823          * we emulate pressing volumeup/volumedown appropriate number of times
824          * in hidinput_hid_event()
825          */
826         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
827                         (usage->code == ABS_VOLUME)) {
828                 set_bit(KEY_VOLUMEUP, input->keybit);
829                 set_bit(KEY_VOLUMEDOWN, input->keybit);
830         }
831
832         if (usage->type == EV_KEY) {
833                 set_bit(EV_MSC, input->evbit);
834                 set_bit(MSC_SCAN, input->mscbit);
835         }
836
837         hid_resolv_event(usage->type, usage->code);
838
839         dbg_hid_line("\n");
840
841         return;
842
843 ignore:
844         dbg_hid_line("IGNORED\n");
845         return;
846 }
847
848 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
849 {
850         struct input_dev *input;
851         unsigned *quirks = &hid->quirks;
852
853         if (!field->hidinput)
854                 return;
855
856         input = field->hidinput->input;
857
858         if (!usage->type)
859                 return;
860
861         /* handle input events for quirky devices */
862         if (hidinput_event_quirks(hid, field, usage, value))
863                 return;
864
865         if (usage->hat_min < usage->hat_max || usage->hat_dir) {
866                 int hat_dir = usage->hat_dir;
867                 if (!hat_dir)
868                         hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
869                 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
870                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
871                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
872                 return;
873         }
874
875         if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
876                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
877                 return;
878         }
879
880         if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
881                 if (value) {
882                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
883                         return;
884                 }
885                 input_event(input, usage->type, usage->code, 0);
886                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
887                 return;
888         }
889
890         if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
891                 int a = field->logical_minimum;
892                 int b = field->logical_maximum;
893                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
894         }
895
896         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
897                 dbg_hid("Maximum Effects - %d\n",value);
898                 return;
899         }
900
901         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
902                 dbg_hid("PID Pool Report\n");
903                 return;
904         }
905
906         if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
907                 return;
908
909         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
910                         (usage->code == ABS_VOLUME)) {
911                 int count = abs(value);
912                 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
913                 int i;
914
915                 for (i = 0; i < count; i++) {
916                         input_event(input, EV_KEY, direction, 1);
917                         input_sync(input);
918                         input_event(input, EV_KEY, direction, 0);
919                         input_sync(input);
920                 }
921                 return;
922         }
923
924         /* report the usage code as scancode if the key status has changed */
925         if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
926                 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
927
928         input_event(input, usage->type, usage->code, value);
929
930         if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
931                 input_event(input, usage->type, usage->code, 0);
932 }
933
934 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
935 {
936         struct hid_input *hidinput;
937
938         list_for_each_entry(hidinput, &hid->inputs, list)
939                 input_sync(hidinput->input);
940 }
941 EXPORT_SYMBOL_GPL(hidinput_report_event);
942
943 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
944 {
945         struct hid_report *report;
946         int i, j;
947
948         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
949                 for (i = 0; i < report->maxfield; i++) {
950                         *field = report->field[i];
951                         for (j = 0; j < (*field)->maxusage; j++)
952                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
953                                         return j;
954                 }
955         }
956         return -1;
957 }
958 EXPORT_SYMBOL_GPL(hidinput_find_field);
959
960 static int hidinput_open(struct input_dev *dev)
961 {
962         struct hid_device *hid = input_get_drvdata(dev);
963
964         return hid->hid_open(hid);
965 }
966
967 static void hidinput_close(struct input_dev *dev)
968 {
969         struct hid_device *hid = input_get_drvdata(dev);
970
971         hid->hid_close(hid);
972 }
973
974 /*
975  * Register the input device; print a message.
976  * Configure the input layer interface
977  * Read all reports and initialize the absolute field values.
978  */
979
980 int hidinput_connect(struct hid_device *hid)
981 {
982         struct hid_report *report;
983         struct hid_input *hidinput = NULL;
984         struct input_dev *input_dev;
985         int i, j, k;
986         int max_report_type = HID_OUTPUT_REPORT;
987
988         if (hid->quirks & HID_QUIRK_IGNORE_HIDINPUT)
989                 return -1;
990
991         INIT_LIST_HEAD(&hid->inputs);
992
993         for (i = 0; i < hid->maxcollection; i++)
994                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION ||
995                     hid->collection[i].type == HID_COLLECTION_PHYSICAL)
996                         if (IS_INPUT_APPLICATION(hid->collection[i].usage))
997                                 break;
998
999         if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDINPUT) == 0)
1000                 return -1;
1001
1002         if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1003                 max_report_type = HID_INPUT_REPORT;
1004
1005         for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
1006                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1007
1008                         if (!report->maxfield)
1009                                 continue;
1010
1011                         if (!hidinput) {
1012                                 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1013                                 input_dev = input_allocate_device();
1014                                 if (!hidinput || !input_dev) {
1015                                         kfree(hidinput);
1016                                         input_free_device(input_dev);
1017                                         err_hid("Out of memory during hid input probe");
1018                                         goto out_unwind;
1019                                 }
1020
1021                                 input_set_drvdata(input_dev, hid);
1022                                 input_dev->event = hid->hidinput_input_event;
1023                                 input_dev->open = hidinput_open;
1024                                 input_dev->close = hidinput_close;
1025                                 input_dev->setkeycode = hidinput_setkeycode;
1026                                 input_dev->getkeycode = hidinput_getkeycode;
1027
1028                                 input_dev->name = hid->name;
1029                                 input_dev->phys = hid->phys;
1030                                 input_dev->uniq = hid->uniq;
1031                                 input_dev->id.bustype = hid->bus;
1032                                 input_dev->id.vendor  = hid->vendor;
1033                                 input_dev->id.product = hid->product;
1034                                 input_dev->id.version = hid->version;
1035                                 input_dev->dev.parent = hid->dev;
1036                                 hidinput->input = input_dev;
1037                                 list_add_tail(&hidinput->list, &hid->inputs);
1038                         }
1039
1040                         for (i = 0; i < report->maxfield; i++)
1041                                 for (j = 0; j < report->field[i]->maxusage; j++)
1042                                         hidinput_configure_usage(hidinput, report->field[i],
1043                                                                  report->field[i]->usage + j);
1044
1045                         if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
1046                                 /* This will leave hidinput NULL, so that it
1047                                  * allocates another one if we have more inputs on
1048                                  * the same interface. Some devices (e.g. Happ's
1049                                  * UGCI) cram a lot of unrelated inputs into the
1050                                  * same interface. */
1051                                 hidinput->report = report;
1052                                 if (input_register_device(hidinput->input))
1053                                         goto out_cleanup;
1054                                 hidinput = NULL;
1055                         }
1056                 }
1057
1058         if (hidinput && input_register_device(hidinput->input))
1059                 goto out_cleanup;
1060
1061         return 0;
1062
1063 out_cleanup:
1064         input_free_device(hidinput->input);
1065         kfree(hidinput);
1066 out_unwind:
1067         /* unwind the ones we already registered */
1068         hidinput_disconnect(hid);
1069
1070         return -1;
1071 }
1072 EXPORT_SYMBOL_GPL(hidinput_connect);
1073
1074 void hidinput_disconnect(struct hid_device *hid)
1075 {
1076         struct hid_input *hidinput, *next;
1077
1078         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
1079                 list_del(&hidinput->list);
1080                 input_unregister_device(hidinput->input);
1081                 kfree(hidinput);
1082         }
1083 }
1084 EXPORT_SYMBOL_GPL(hidinput_disconnect);
1085