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