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