Merge branch 'next' into for-linus
[pandora-kernel.git] / drivers / hid / hid-input.c
1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *  Copyright (c) 2006-2010 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 #define unk     KEY_UNKNOWN
36
37 static const unsigned char hid_keyboard[256] = {
38           0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
39          50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
40           4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
41          27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
42          65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
43         105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
44          72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
45         191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
46         115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
47         122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
48         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
49         unk,unk,unk,unk,unk,unk,179,180,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          29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
53         150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
54 };
55
56 static const struct {
57         __s32 x;
58         __s32 y;
59 }  hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
60
61 #define map_abs(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
62 #define map_rel(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
63 #define map_key(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
64 #define map_led(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
65
66 #define map_abs_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
67                 &max, EV_ABS, (c))
68 #define map_key_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
69                 &max, EV_KEY, (c))
70
71 static bool match_scancode(struct hid_usage *usage,
72                            unsigned int cur_idx, unsigned int scancode)
73 {
74         return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
75 }
76
77 static bool match_keycode(struct hid_usage *usage,
78                           unsigned int cur_idx, unsigned int keycode)
79 {
80         /*
81          * We should exclude unmapped usages when doing lookup by keycode.
82          */
83         return (usage->type == EV_KEY && usage->code == keycode);
84 }
85
86 static bool match_index(struct hid_usage *usage,
87                         unsigned int cur_idx, unsigned int idx)
88 {
89         return cur_idx == idx;
90 }
91
92 typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
93                                 unsigned int cur_idx, unsigned int val);
94
95 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
96                                            hid_usage_cmp_t match,
97                                            unsigned int value,
98                                            unsigned int *usage_idx)
99 {
100         unsigned int i, j, k, cur_idx = 0;
101         struct hid_report *report;
102         struct hid_usage *usage;
103
104         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
105                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
106                         for (i = 0; i < report->maxfield; i++) {
107                                 for (j = 0; j < report->field[i]->maxusage; j++) {
108                                         usage = report->field[i]->usage + j;
109                                         if (usage->type == EV_KEY || usage->type == 0) {
110                                                 if (match(usage, cur_idx, value)) {
111                                                         if (usage_idx)
112                                                                 *usage_idx = cur_idx;
113                                                         return usage;
114                                                 }
115                                                 cur_idx++;
116                                         }
117                                 }
118                         }
119                 }
120         }
121         return NULL;
122 }
123
124 static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
125                                         const struct input_keymap_entry *ke,
126                                         unsigned int *index)
127 {
128         struct hid_usage *usage;
129         unsigned int scancode;
130
131         if (ke->flags & INPUT_KEYMAP_BY_INDEX)
132                 usage = hidinput_find_key(hid, match_index, ke->index, index);
133         else if (input_scancode_to_scalar(ke, &scancode) == 0)
134                 usage = hidinput_find_key(hid, match_scancode, scancode, index);
135         else
136                 usage = NULL;
137
138         return usage;
139 }
140
141 static int hidinput_getkeycode(struct input_dev *dev,
142                                struct input_keymap_entry *ke)
143 {
144         struct hid_device *hid = input_get_drvdata(dev);
145         struct hid_usage *usage;
146         unsigned int scancode, index;
147
148         usage = hidinput_locate_usage(hid, ke, &index);
149         if (usage) {
150                 ke->keycode = usage->type == EV_KEY ?
151                                 usage->code : KEY_RESERVED;
152                 ke->index = index;
153                 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
154                 ke->len = sizeof(scancode);
155                 memcpy(ke->scancode, &scancode, sizeof(scancode));
156                 return 0;
157         }
158
159         return -EINVAL;
160 }
161
162 static int hidinput_setkeycode(struct input_dev *dev,
163                                const struct input_keymap_entry *ke,
164                                unsigned int *old_keycode)
165 {
166         struct hid_device *hid = input_get_drvdata(dev);
167         struct hid_usage *usage;
168
169         usage = hidinput_locate_usage(hid, ke, NULL);
170         if (usage) {
171                 *old_keycode = usage->type == EV_KEY ?
172                                 usage->code : KEY_RESERVED;
173                 usage->code = ke->keycode;
174
175                 clear_bit(*old_keycode, dev->keybit);
176                 set_bit(usage->code, dev->keybit);
177                 dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n",
178                         usage->code, usage->hid);
179
180                 /*
181                  * Set the keybit for the old keycode if the old keycode is used
182                  * by another key
183                  */
184                 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
185                         set_bit(*old_keycode, dev->keybit);
186
187                 return 0;
188         }
189
190         return -EINVAL;
191 }
192
193
194 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
195                                      struct hid_usage *usage)
196 {
197         struct input_dev *input = hidinput->input;
198         struct hid_device *device = input_get_drvdata(input);
199         int max = 0, code;
200         unsigned long *bit = NULL;
201
202         field->hidinput = hidinput;
203
204         if (field->flags & HID_MAIN_ITEM_CONSTANT)
205                 goto ignore;
206
207         /* only LED usages are supported in output fields */
208         if (field->report_type == HID_OUTPUT_REPORT &&
209                         (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
210                 goto ignore;
211         }
212
213         if (device->driver->input_mapping) {
214                 int ret = device->driver->input_mapping(device, hidinput, field,
215                                 usage, &bit, &max);
216                 if (ret > 0)
217                         goto mapped;
218                 if (ret < 0)
219                         goto ignore;
220         }
221
222         switch (usage->hid & HID_USAGE_PAGE) {
223         case HID_UP_UNDEFINED:
224                 goto ignore;
225
226         case HID_UP_KEYBOARD:
227                 set_bit(EV_REP, input->evbit);
228
229                 if ((usage->hid & HID_USAGE) < 256) {
230                         if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
231                         map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
232                 } else
233                         map_key(KEY_UNKNOWN);
234
235                 break;
236
237         case HID_UP_BUTTON:
238                 code = ((usage->hid - 1) & HID_USAGE);
239
240                 switch (field->application) {
241                 case HID_GD_MOUSE:
242                 case HID_GD_POINTER:  code += 0x110; break;
243                 case HID_GD_JOYSTICK:
244                                 if (code <= 0xf)
245                                         code += BTN_JOYSTICK;
246                                 else
247                                         code += BTN_TRIGGER_HAPPY;
248                                 break;
249                 case HID_GD_GAMEPAD:  code += 0x130; break;
250                 default:
251                         switch (field->physical) {
252                         case HID_GD_MOUSE:
253                         case HID_GD_POINTER:  code += 0x110; break;
254                         case HID_GD_JOYSTICK: code += 0x120; break;
255                         case HID_GD_GAMEPAD:  code += 0x130; break;
256                         default:              code += 0x100;
257                         }
258                 }
259
260                 map_key(code);
261                 break;
262
263         case HID_UP_SIMULATION:
264                 switch (usage->hid & 0xffff) {
265                 case 0xba: map_abs(ABS_RUDDER);   break;
266                 case 0xbb: map_abs(ABS_THROTTLE); break;
267                 case 0xc4: map_abs(ABS_GAS);      break;
268                 case 0xc5: map_abs(ABS_BRAKE);    break;
269                 case 0xc8: map_abs(ABS_WHEEL);    break;
270                 default:   goto ignore;
271                 }
272                 break;
273
274         case HID_UP_GENDESK:
275                 if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
276                         switch (usage->hid & 0xf) {
277                         case 0x1: map_key_clear(KEY_POWER);  break;
278                         case 0x2: map_key_clear(KEY_SLEEP);  break;
279                         case 0x3: map_key_clear(KEY_WAKEUP); break;
280                         default: goto unknown;
281                         }
282                         break;
283                 }
284
285                 if ((usage->hid & 0xf0) == 0x90) {      /* D-pad */
286                         switch (usage->hid) {
287                         case HID_GD_UP:    usage->hat_dir = 1; break;
288                         case HID_GD_DOWN:  usage->hat_dir = 5; break;
289                         case HID_GD_RIGHT: usage->hat_dir = 3; break;
290                         case HID_GD_LEFT:  usage->hat_dir = 7; break;
291                         default: goto unknown;
292                         }
293                         if (field->dpad) {
294                                 map_abs(field->dpad);
295                                 goto ignore;
296                         }
297                         map_abs(ABS_HAT0X);
298                         break;
299                 }
300
301                 switch (usage->hid) {
302                 /* These usage IDs map directly to the usage codes. */
303                 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
304                 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
305                 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
306                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
307                                 map_rel(usage->hid & 0xf);
308                         else
309                                 map_abs(usage->hid & 0xf);
310                         break;
311
312                 case HID_GD_HATSWITCH:
313                         usage->hat_min = field->logical_minimum;
314                         usage->hat_max = field->logical_maximum;
315                         map_abs(ABS_HAT0X);
316                         break;
317
318                 case HID_GD_START:      map_key_clear(BTN_START);       break;
319                 case HID_GD_SELECT:     map_key_clear(BTN_SELECT);      break;
320
321                 default: goto unknown;
322                 }
323
324                 break;
325
326         case HID_UP_LED:
327                 switch (usage->hid & 0xffff) {                /* HID-Value:                   */
328                 case 0x01:  map_led (LED_NUML);     break;    /*   "Num Lock"                 */
329                 case 0x02:  map_led (LED_CAPSL);    break;    /*   "Caps Lock"                */
330                 case 0x03:  map_led (LED_SCROLLL);  break;    /*   "Scroll Lock"              */
331                 case 0x04:  map_led (LED_COMPOSE);  break;    /*   "Compose"                  */
332                 case 0x05:  map_led (LED_KANA);     break;    /*   "Kana"                     */
333                 case 0x27:  map_led (LED_SLEEP);    break;    /*   "Stand-By"                 */
334                 case 0x4c:  map_led (LED_SUSPEND);  break;    /*   "System Suspend"           */
335                 case 0x09:  map_led (LED_MUTE);     break;    /*   "Mute"                     */
336                 case 0x4b:  map_led (LED_MISC);     break;    /*   "Generic Indicator"        */
337                 case 0x19:  map_led (LED_MAIL);     break;    /*   "Message Waiting"          */
338                 case 0x4d:  map_led (LED_CHARGING); break;    /*   "External Power Connected" */
339
340                 default: goto ignore;
341                 }
342                 break;
343
344         case HID_UP_DIGITIZER:
345                 switch (usage->hid & 0xff) {
346                 case 0x00: /* Undefined */
347                         goto ignore;
348
349                 case 0x30: /* TipPressure */
350                         if (!test_bit(BTN_TOUCH, input->keybit)) {
351                                 device->quirks |= HID_QUIRK_NOTOUCH;
352                                 set_bit(EV_KEY, input->evbit);
353                                 set_bit(BTN_TOUCH, input->keybit);
354                         }
355                         map_abs_clear(ABS_PRESSURE);
356                         break;
357
358                 case 0x32: /* InRange */
359                         switch (field->physical & 0xff) {
360                         case 0x21: map_key(BTN_TOOL_MOUSE); break;
361                         case 0x22: map_key(BTN_TOOL_FINGER); break;
362                         default: map_key(BTN_TOOL_PEN); break;
363                         }
364                         break;
365
366                 case 0x3c: /* Invert */
367                         map_key_clear(BTN_TOOL_RUBBER);
368                         break;
369
370                 case 0x33: /* Touch */
371                 case 0x42: /* TipSwitch */
372                 case 0x43: /* TipSwitch2 */
373                         device->quirks &= ~HID_QUIRK_NOTOUCH;
374                         map_key_clear(BTN_TOUCH);
375                         break;
376
377                 case 0x44: /* BarrelSwitch */
378                         map_key_clear(BTN_STYLUS);
379                         break;
380
381                 default:  goto unknown;
382                 }
383                 break;
384
385         case HID_UP_CONSUMER:   /* USB HUT v1.1, pages 56-62 */
386                 switch (usage->hid & HID_USAGE) {
387                 case 0x000: goto ignore;
388                 case 0x034: map_key_clear(KEY_SLEEP);           break;
389                 case 0x036: map_key_clear(BTN_MISC);            break;
390
391                 case 0x040: map_key_clear(KEY_MENU);            break;
392                 case 0x045: map_key_clear(KEY_RADIO);           break;
393
394                 case 0x083: map_key_clear(KEY_LAST);            break;
395                 case 0x088: map_key_clear(KEY_PC);              break;
396                 case 0x089: map_key_clear(KEY_TV);              break;
397                 case 0x08a: map_key_clear(KEY_WWW);             break;
398                 case 0x08b: map_key_clear(KEY_DVD);             break;
399                 case 0x08c: map_key_clear(KEY_PHONE);           break;
400                 case 0x08d: map_key_clear(KEY_PROGRAM);         break;
401                 case 0x08e: map_key_clear(KEY_VIDEOPHONE);      break;
402                 case 0x08f: map_key_clear(KEY_GAMES);           break;
403                 case 0x090: map_key_clear(KEY_MEMO);            break;
404                 case 0x091: map_key_clear(KEY_CD);              break;
405                 case 0x092: map_key_clear(KEY_VCR);             break;
406                 case 0x093: map_key_clear(KEY_TUNER);           break;
407                 case 0x094: map_key_clear(KEY_EXIT);            break;
408                 case 0x095: map_key_clear(KEY_HELP);            break;
409                 case 0x096: map_key_clear(KEY_TAPE);            break;
410                 case 0x097: map_key_clear(KEY_TV2);             break;
411                 case 0x098: map_key_clear(KEY_SAT);             break;
412                 case 0x09a: map_key_clear(KEY_PVR);             break;
413
414                 case 0x09c: map_key_clear(KEY_CHANNELUP);       break;
415                 case 0x09d: map_key_clear(KEY_CHANNELDOWN);     break;
416                 case 0x0a0: map_key_clear(KEY_VCR2);            break;
417
418                 case 0x0b0: map_key_clear(KEY_PLAY);            break;
419                 case 0x0b1: map_key_clear(KEY_PAUSE);           break;
420                 case 0x0b2: map_key_clear(KEY_RECORD);          break;
421                 case 0x0b3: map_key_clear(KEY_FASTFORWARD);     break;
422                 case 0x0b4: map_key_clear(KEY_REWIND);          break;
423                 case 0x0b5: map_key_clear(KEY_NEXTSONG);        break;
424                 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
425                 case 0x0b7: map_key_clear(KEY_STOPCD);          break;
426                 case 0x0b8: map_key_clear(KEY_EJECTCD);         break;
427                 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
428
429                 case 0x0cd: map_key_clear(KEY_PLAYPAUSE);       break;
430                 case 0x0e0: map_abs_clear(ABS_VOLUME);          break;
431                 case 0x0e2: map_key_clear(KEY_MUTE);            break;
432                 case 0x0e5: map_key_clear(KEY_BASSBOOST);       break;
433                 case 0x0e9: map_key_clear(KEY_VOLUMEUP);        break;
434                 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);      break;
435
436                 case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
437                 case 0x183: map_key_clear(KEY_CONFIG);          break;
438                 case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
439                 case 0x185: map_key_clear(KEY_EDITOR);          break;
440                 case 0x186: map_key_clear(KEY_SPREADSHEET);     break;
441                 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);  break;
442                 case 0x188: map_key_clear(KEY_PRESENTATION);    break;
443                 case 0x189: map_key_clear(KEY_DATABASE);        break;
444                 case 0x18a: map_key_clear(KEY_MAIL);            break;
445                 case 0x18b: map_key_clear(KEY_NEWS);            break;
446                 case 0x18c: map_key_clear(KEY_VOICEMAIL);       break;
447                 case 0x18d: map_key_clear(KEY_ADDRESSBOOK);     break;
448                 case 0x18e: map_key_clear(KEY_CALENDAR);        break;
449                 case 0x191: map_key_clear(KEY_FINANCE);         break;
450                 case 0x192: map_key_clear(KEY_CALC);            break;
451                 case 0x194: map_key_clear(KEY_FILE);            break;
452                 case 0x196: map_key_clear(KEY_WWW);             break;
453                 case 0x199: map_key_clear(KEY_CHAT);            break;
454                 case 0x19c: map_key_clear(KEY_LOGOFF);          break;
455                 case 0x19e: map_key_clear(KEY_COFFEE);          break;
456                 case 0x1a6: map_key_clear(KEY_HELP);            break;
457                 case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
458                 case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
459                 case 0x1b6: map_key_clear(KEY_MEDIA);           break;
460                 case 0x1b7: map_key_clear(KEY_SOUND);           break;
461                 case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
462                 case 0x1bd: map_key_clear(KEY_INFO);            break;
463                 case 0x201: map_key_clear(KEY_NEW);             break;
464                 case 0x202: map_key_clear(KEY_OPEN);            break;
465                 case 0x203: map_key_clear(KEY_CLOSE);           break;
466                 case 0x204: map_key_clear(KEY_EXIT);            break;
467                 case 0x207: map_key_clear(KEY_SAVE);            break;
468                 case 0x208: map_key_clear(KEY_PRINT);           break;
469                 case 0x209: map_key_clear(KEY_PROPS);           break;
470                 case 0x21a: map_key_clear(KEY_UNDO);            break;
471                 case 0x21b: map_key_clear(KEY_COPY);            break;
472                 case 0x21c: map_key_clear(KEY_CUT);             break;
473                 case 0x21d: map_key_clear(KEY_PASTE);           break;
474                 case 0x21f: map_key_clear(KEY_FIND);            break;
475                 case 0x221: map_key_clear(KEY_SEARCH);          break;
476                 case 0x222: map_key_clear(KEY_GOTO);            break;
477                 case 0x223: map_key_clear(KEY_HOMEPAGE);        break;
478                 case 0x224: map_key_clear(KEY_BACK);            break;
479                 case 0x225: map_key_clear(KEY_FORWARD);         break;
480                 case 0x226: map_key_clear(KEY_STOP);            break;
481                 case 0x227: map_key_clear(KEY_REFRESH);         break;
482                 case 0x22a: map_key_clear(KEY_BOOKMARKS);       break;
483                 case 0x22d: map_key_clear(KEY_ZOOMIN);          break;
484                 case 0x22e: map_key_clear(KEY_ZOOMOUT);         break;
485                 case 0x22f: map_key_clear(KEY_ZOOMRESET);       break;
486                 case 0x233: map_key_clear(KEY_SCROLLUP);        break;
487                 case 0x234: map_key_clear(KEY_SCROLLDOWN);      break;
488                 case 0x238: map_rel(REL_HWHEEL);                break;
489                 case 0x25f: map_key_clear(KEY_CANCEL);          break;
490                 case 0x279: map_key_clear(KEY_REDO);            break;
491
492                 case 0x289: map_key_clear(KEY_REPLY);           break;
493                 case 0x28b: map_key_clear(KEY_FORWARDMAIL);     break;
494                 case 0x28c: map_key_clear(KEY_SEND);            break;
495
496                 default:    goto ignore;
497                 }
498                 break;
499
500         case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
501                 set_bit(EV_REP, input->evbit);
502                 switch (usage->hid & HID_USAGE) {
503                 case 0x021: map_key_clear(KEY_PRINT);           break;
504                 case 0x070: map_key_clear(KEY_HP);              break;
505                 case 0x071: map_key_clear(KEY_CAMERA);          break;
506                 case 0x072: map_key_clear(KEY_SOUND);           break;
507                 case 0x073: map_key_clear(KEY_QUESTION);        break;
508                 case 0x080: map_key_clear(KEY_EMAIL);           break;
509                 case 0x081: map_key_clear(KEY_CHAT);            break;
510                 case 0x082: map_key_clear(KEY_SEARCH);          break;
511                 case 0x083: map_key_clear(KEY_CONNECT);         break;
512                 case 0x084: map_key_clear(KEY_FINANCE);         break;
513                 case 0x085: map_key_clear(KEY_SPORT);           break;
514                 case 0x086: map_key_clear(KEY_SHOP);            break;
515                 default:    goto ignore;
516                 }
517                 break;
518
519         case HID_UP_MSVENDOR:
520                 goto ignore;
521
522         case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
523                 set_bit(EV_REP, input->evbit);
524                 goto ignore;
525
526         case HID_UP_LOGIVENDOR:
527                 goto ignore;
528
529         case HID_UP_PID:
530                 switch (usage->hid & HID_USAGE) {
531                 case 0xa4: map_key_clear(BTN_DEAD);     break;
532                 default: goto ignore;
533                 }
534                 break;
535
536         default:
537         unknown:
538                 if (field->report_size == 1) {
539                         if (field->report->type == HID_OUTPUT_REPORT) {
540                                 map_led(LED_MISC);
541                                 break;
542                         }
543                         map_key(BTN_MISC);
544                         break;
545                 }
546                 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
547                         map_rel(REL_MISC);
548                         break;
549                 }
550                 map_abs(ABS_MISC);
551                 break;
552         }
553
554 mapped:
555         if (device->driver->input_mapped && device->driver->input_mapped(device,
556                                 hidinput, field, usage, &bit, &max) < 0)
557                 goto ignore;
558
559         set_bit(usage->type, input->evbit);
560
561         while (usage->code <= max && test_and_set_bit(usage->code, bit))
562                 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
563
564         if (usage->code > max)
565                 goto ignore;
566
567
568         if (usage->type == EV_ABS) {
569
570                 int a = field->logical_minimum;
571                 int b = field->logical_maximum;
572
573                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
574                         a = field->logical_minimum = 0;
575                         b = field->logical_maximum = 255;
576                 }
577
578                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
579                         input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
580                 else    input_set_abs_params(input, usage->code, a, b, 0, 0);
581
582                 /* use a larger default input buffer for MT devices */
583                 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
584                         input_set_events_per_packet(input, 60);
585         }
586
587         if (usage->type == EV_ABS &&
588             (usage->hat_min < usage->hat_max || usage->hat_dir)) {
589                 int i;
590                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
591                         input_set_abs_params(input, i, -1, 1, 0, 0);
592                         set_bit(i, input->absbit);
593                 }
594                 if (usage->hat_dir && !field->dpad)
595                         field->dpad = usage->code;
596         }
597
598         /* for those devices which produce Consumer volume usage as relative,
599          * we emulate pressing volumeup/volumedown appropriate number of times
600          * in hidinput_hid_event()
601          */
602         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
603                         (usage->code == ABS_VOLUME)) {
604                 set_bit(KEY_VOLUMEUP, input->keybit);
605                 set_bit(KEY_VOLUMEDOWN, input->keybit);
606         }
607
608         if (usage->type == EV_KEY) {
609                 set_bit(EV_MSC, input->evbit);
610                 set_bit(MSC_SCAN, input->mscbit);
611         }
612
613 ignore:
614         return;
615
616 }
617
618 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
619 {
620         struct input_dev *input;
621         unsigned *quirks = &hid->quirks;
622
623         if (!field->hidinput)
624                 return;
625
626         input = field->hidinput->input;
627
628         if (!usage->type)
629                 return;
630
631         if (usage->hat_min < usage->hat_max || usage->hat_dir) {
632                 int hat_dir = usage->hat_dir;
633                 if (!hat_dir)
634                         hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
635                 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
636                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
637                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
638                 return;
639         }
640
641         if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
642                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
643                 return;
644         }
645
646         if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
647                 if (value) {
648                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
649                         return;
650                 }
651                 input_event(input, usage->type, usage->code, 0);
652                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
653                 return;
654         }
655
656         if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
657                 int a = field->logical_minimum;
658                 int b = field->logical_maximum;
659                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
660         }
661
662         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
663                 dbg_hid("Maximum Effects - %d\n",value);
664                 return;
665         }
666
667         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
668                 dbg_hid("PID Pool Report\n");
669                 return;
670         }
671
672         if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
673                 return;
674
675         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
676                         (usage->code == ABS_VOLUME)) {
677                 int count = abs(value);
678                 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
679                 int i;
680
681                 for (i = 0; i < count; i++) {
682                         input_event(input, EV_KEY, direction, 1);
683                         input_sync(input);
684                         input_event(input, EV_KEY, direction, 0);
685                         input_sync(input);
686                 }
687                 return;
688         }
689
690         /* report the usage code as scancode if the key status has changed */
691         if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
692                 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
693
694         input_event(input, usage->type, usage->code, value);
695
696         if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
697                 input_event(input, usage->type, usage->code, 0);
698 }
699
700 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
701 {
702         struct hid_input *hidinput;
703
704         list_for_each_entry(hidinput, &hid->inputs, list)
705                 input_sync(hidinput->input);
706 }
707 EXPORT_SYMBOL_GPL(hidinput_report_event);
708
709 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
710 {
711         struct hid_report *report;
712         int i, j;
713
714         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
715                 for (i = 0; i < report->maxfield; i++) {
716                         *field = report->field[i];
717                         for (j = 0; j < (*field)->maxusage; j++)
718                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
719                                         return j;
720                 }
721         }
722         return -1;
723 }
724 EXPORT_SYMBOL_GPL(hidinput_find_field);
725
726 static int hidinput_open(struct input_dev *dev)
727 {
728         struct hid_device *hid = input_get_drvdata(dev);
729
730         return hid->ll_driver->open(hid);
731 }
732
733 static void hidinput_close(struct input_dev *dev)
734 {
735         struct hid_device *hid = input_get_drvdata(dev);
736
737         hid->ll_driver->close(hid);
738 }
739
740 /*
741  * Register the input device; print a message.
742  * Configure the input layer interface
743  * Read all reports and initialize the absolute field values.
744  */
745
746 int hidinput_connect(struct hid_device *hid, unsigned int force)
747 {
748         struct hid_report *report;
749         struct hid_input *hidinput = NULL;
750         struct input_dev *input_dev;
751         int i, j, k;
752         int max_report_type = HID_OUTPUT_REPORT;
753
754         INIT_LIST_HEAD(&hid->inputs);
755
756         if (!force) {
757                 for (i = 0; i < hid->maxcollection; i++) {
758                         struct hid_collection *col = &hid->collection[i];
759                         if (col->type == HID_COLLECTION_APPLICATION ||
760                                         col->type == HID_COLLECTION_PHYSICAL)
761                                 if (IS_INPUT_APPLICATION(col->usage))
762                                         break;
763                 }
764
765                 if (i == hid->maxcollection)
766                         return -1;
767         }
768
769         if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
770                 max_report_type = HID_INPUT_REPORT;
771
772         for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
773                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
774
775                         if (!report->maxfield)
776                                 continue;
777
778                         if (!hidinput) {
779                                 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
780                                 input_dev = input_allocate_device();
781                                 if (!hidinput || !input_dev) {
782                                         kfree(hidinput);
783                                         input_free_device(input_dev);
784                                         err_hid("Out of memory during hid input probe");
785                                         goto out_unwind;
786                                 }
787
788                                 input_set_drvdata(input_dev, hid);
789                                 input_dev->event =
790                                         hid->ll_driver->hidinput_input_event;
791                                 input_dev->open = hidinput_open;
792                                 input_dev->close = hidinput_close;
793                                 input_dev->setkeycode_new = hidinput_setkeycode;
794                                 input_dev->getkeycode_new = hidinput_getkeycode;
795
796                                 input_dev->name = hid->name;
797                                 input_dev->phys = hid->phys;
798                                 input_dev->uniq = hid->uniq;
799                                 input_dev->id.bustype = hid->bus;
800                                 input_dev->id.vendor  = hid->vendor;
801                                 input_dev->id.product = hid->product;
802                                 input_dev->id.version = hid->version;
803                                 input_dev->dev.parent = hid->dev.parent;
804                                 hidinput->input = input_dev;
805                                 list_add_tail(&hidinput->list, &hid->inputs);
806                         }
807
808                         for (i = 0; i < report->maxfield; i++)
809                                 for (j = 0; j < report->field[i]->maxusage; j++)
810                                         hidinput_configure_usage(hidinput, report->field[i],
811                                                                  report->field[i]->usage + j);
812
813                         if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
814                                 /* This will leave hidinput NULL, so that it
815                                  * allocates another one if we have more inputs on
816                                  * the same interface. Some devices (e.g. Happ's
817                                  * UGCI) cram a lot of unrelated inputs into the
818                                  * same interface. */
819                                 hidinput->report = report;
820                                 if (input_register_device(hidinput->input))
821                                         goto out_cleanup;
822                                 hidinput = NULL;
823                         }
824                 }
825
826         if (hidinput && input_register_device(hidinput->input))
827                 goto out_cleanup;
828
829         return 0;
830
831 out_cleanup:
832         input_free_device(hidinput->input);
833         kfree(hidinput);
834 out_unwind:
835         /* unwind the ones we already registered */
836         hidinput_disconnect(hid);
837
838         return -1;
839 }
840 EXPORT_SYMBOL_GPL(hidinput_connect);
841
842 void hidinput_disconnect(struct hid_device *hid)
843 {
844         struct hid_input *hidinput, *next;
845
846         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
847                 list_del(&hidinput->list);
848                 input_unregister_device(hidinput->input);
849                 kfree(hidinput);
850         }
851 }
852 EXPORT_SYMBOL_GPL(hidinput_disconnect);
853