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