video: tegra: dc: Add quick for Vizio P series
[linux-3.10.git] / drivers / hid / hid-input.c
1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *  Copyright (c) 2006-2010 Jiri Kosina
4  *  Copyright (c) 2014 NVIDIA Corporation. All rights reserved.
5  *
6  *  HID to Linux Input mapping
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32
33 #include <linux/hid.h>
34 #include <linux/hid-debug.h>
35
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/hid-input.h>
38
39 #include "hid-ids.h"
40
41 #define unk     KEY_UNKNOWN
42
43 static const unsigned char hid_keyboard[256] = {
44           0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
45          50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
46           4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
47          27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
48          65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
49         105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
50          72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
51         191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
52         115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
53         122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
54         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
55         unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
56         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
57         unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
58          29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
59         150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
60 };
61
62 static const struct {
63         __s32 x;
64         __s32 y;
65 }  hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
66
67 #define map_abs(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
68 #define map_rel(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
69 #define map_key(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
70 #define map_led(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
71
72 #define map_abs_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
73                 &max, EV_ABS, (c))
74 #define map_key_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
75                 &max, EV_KEY, (c))
76
77 static bool match_scancode(struct hid_usage *usage,
78                            unsigned int cur_idx, unsigned int scancode)
79 {
80         return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
81 }
82
83 static bool match_keycode(struct hid_usage *usage,
84                           unsigned int cur_idx, unsigned int keycode)
85 {
86         /*
87          * We should exclude unmapped usages when doing lookup by keycode.
88          */
89         return (usage->type == EV_KEY && usage->code == keycode);
90 }
91
92 static bool match_index(struct hid_usage *usage,
93                         unsigned int cur_idx, unsigned int idx)
94 {
95         return cur_idx == idx;
96 }
97
98 typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
99                                 unsigned int cur_idx, unsigned int val);
100
101 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
102                                            hid_usage_cmp_t match,
103                                            unsigned int value,
104                                            unsigned int *usage_idx)
105 {
106         unsigned int i, j, k, cur_idx = 0;
107         struct hid_report *report;
108         struct hid_usage *usage;
109
110         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
111                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
112                         for (i = 0; i < report->maxfield; i++) {
113                                 for (j = 0; j < report->field[i]->maxusage; j++) {
114                                         usage = report->field[i]->usage + j;
115                                         if (usage->type == EV_KEY || usage->type == 0) {
116                                                 if (match(usage, cur_idx, value)) {
117                                                         if (usage_idx)
118                                                                 *usage_idx = cur_idx;
119                                                         return usage;
120                                                 }
121                                                 cur_idx++;
122                                         }
123                                 }
124                         }
125                 }
126         }
127         return NULL;
128 }
129
130 static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
131                                         const struct input_keymap_entry *ke,
132                                         unsigned int *index)
133 {
134         struct hid_usage *usage;
135         unsigned int scancode;
136
137         if (ke->flags & INPUT_KEYMAP_BY_INDEX)
138                 usage = hidinput_find_key(hid, match_index, ke->index, index);
139         else if (input_scancode_to_scalar(ke, &scancode) == 0)
140                 usage = hidinput_find_key(hid, match_scancode, scancode, index);
141         else
142                 usage = NULL;
143
144         return usage;
145 }
146
147 static int hidinput_getkeycode(struct input_dev *dev,
148                                struct input_keymap_entry *ke)
149 {
150         struct hid_device *hid = input_get_drvdata(dev);
151         struct hid_usage *usage;
152         unsigned int scancode, index;
153
154         usage = hidinput_locate_usage(hid, ke, &index);
155         if (usage) {
156                 ke->keycode = usage->type == EV_KEY ?
157                                 usage->code : KEY_RESERVED;
158                 ke->index = index;
159                 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
160                 ke->len = sizeof(scancode);
161                 memcpy(ke->scancode, &scancode, sizeof(scancode));
162                 return 0;
163         }
164
165         return -EINVAL;
166 }
167
168 static int hidinput_setkeycode(struct input_dev *dev,
169                                const struct input_keymap_entry *ke,
170                                unsigned int *old_keycode)
171 {
172         struct hid_device *hid = input_get_drvdata(dev);
173         struct hid_usage *usage;
174
175         usage = hidinput_locate_usage(hid, ke, NULL);
176         if (usage) {
177                 *old_keycode = usage->type == EV_KEY ?
178                                 usage->code : KEY_RESERVED;
179                 usage->code = ke->keycode;
180
181                 clear_bit(*old_keycode, dev->keybit);
182                 set_bit(usage->code, dev->keybit);
183                 dbg_hid("Assigned keycode %d to HID usage code %x\n",
184                         usage->code, usage->hid);
185
186                 /*
187                  * Set the keybit for the old keycode if the old keycode is used
188                  * by another key
189                  */
190                 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
191                         set_bit(*old_keycode, dev->keybit);
192
193                 return 0;
194         }
195
196         return -EINVAL;
197 }
198
199 /**
200  * hidinput_calc_abs_res - calculate an absolute axis resolution
201  * @field: the HID report field to calculate resolution for
202  * @code: axis code
203  *
204  * The formula is:
205  *                         (logical_maximum - logical_minimum)
206  * resolution = ----------------------------------------------------------
207  *              (physical_maximum - physical_minimum) * 10 ^ unit_exponent
208  *
209  * as seen in the HID specification v1.11 6.2.2.7 Global Items.
210  *
211  * Only exponent 1 length units are processed. Centimeters and inches are
212  * converted to millimeters. Degrees are converted to radians.
213  */
214 __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
215 {
216         __s32 unit_exponent = field->unit_exponent;
217         __s32 logical_extents = field->logical_maximum -
218                                         field->logical_minimum;
219         __s32 physical_extents = field->physical_maximum -
220                                         field->physical_minimum;
221         __s32 prev;
222
223         /* Check if the extents are sane */
224         if (logical_extents <= 0 || physical_extents <= 0)
225                 return 0;
226
227         /*
228          * Verify and convert units.
229          * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
230          */
231         switch (code) {
232         case ABS_X:
233         case ABS_Y:
234         case ABS_Z:
235         case ABS_MT_POSITION_X:
236         case ABS_MT_POSITION_Y:
237         case ABS_MT_TOOL_X:
238         case ABS_MT_TOOL_Y:
239         case ABS_MT_TOUCH_MAJOR:
240         case ABS_MT_TOUCH_MINOR:
241                 if (field->unit & 0xffffff00)           /* Not a length */
242                         return 0;
243                 unit_exponent += hid_snto32(field->unit >> 4, 4) - 1;
244                 switch (field->unit & 0xf) {
245                 case 0x1:                               /* If centimeters */
246                         /* Convert to millimeters */
247                         unit_exponent += 1;
248                         break;
249                 case 0x3:                               /* If inches */
250                         /* Convert to millimeters */
251                         prev = physical_extents;
252                         physical_extents *= 254;
253                         if (physical_extents < prev)
254                                 return 0;
255                         unit_exponent -= 1;
256                         break;
257                 default:
258                         return 0;
259                 }
260                 break;
261
262         case ABS_RX:
263         case ABS_RY:
264         case ABS_RZ:
265         case ABS_TILT_X:
266         case ABS_TILT_Y:
267                 if (field->unit == 0x14) {              /* If degrees */
268                         /* Convert to radians */
269                         prev = logical_extents;
270                         logical_extents *= 573;
271                         if (logical_extents < prev)
272                                 return 0;
273                         unit_exponent += 1;
274                 } else if (field->unit != 0x12) {       /* If not radians */
275                         return 0;
276                 }
277                 break;
278
279         default:
280                 return 0;
281         }
282
283         /* Apply negative unit exponent */
284         for (; unit_exponent < 0; unit_exponent++) {
285                 prev = logical_extents;
286                 logical_extents *= 10;
287                 if (logical_extents < prev)
288                         return 0;
289         }
290         /* Apply positive unit exponent */
291         for (; unit_exponent > 0; unit_exponent--) {
292                 prev = physical_extents;
293                 physical_extents *= 10;
294                 if (physical_extents < prev)
295                         return 0;
296         }
297
298         /* Calculate resolution */
299         return DIV_ROUND_CLOSEST(logical_extents, physical_extents);
300 }
301 EXPORT_SYMBOL_GPL(hidinput_calc_abs_res);
302
303 #ifdef CONFIG_HID_BATTERY_STRENGTH
304 static enum power_supply_property hidinput_battery_props[] = {
305         POWER_SUPPLY_PROP_PRESENT,
306         POWER_SUPPLY_PROP_ONLINE,
307         POWER_SUPPLY_PROP_CAPACITY,
308         POWER_SUPPLY_PROP_MODEL_NAME,
309         POWER_SUPPLY_PROP_STATUS,
310         POWER_SUPPLY_PROP_SCOPE,
311 };
312
313 #define HID_BATTERY_QUIRK_PERCENT       (1 << 0) /* always reports percent */
314 #define HID_BATTERY_QUIRK_FEATURE       (1 << 1) /* ask for feature report */
315
316 static const struct hid_device_id hid_battery_quirks[] = {
317         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
318                         USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
319         HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
320         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
321                                USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
322           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
323         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
324                                USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
325           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
326         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
327                 USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
328           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
329         {}
330 };
331
332 static unsigned find_battery_quirk(struct hid_device *hdev)
333 {
334         unsigned quirks = 0;
335         const struct hid_device_id *match;
336
337         match = hid_match_id(hdev, hid_battery_quirks);
338         if (match != NULL)
339                 quirks = match->driver_data;
340
341         return quirks;
342 }
343
344 static int hidinput_get_battery_property(struct power_supply *psy,
345                                          enum power_supply_property prop,
346                                          union power_supply_propval *val)
347 {
348         struct hid_device *dev = container_of(psy, struct hid_device, battery);
349         int ret = 0;
350         __u8 *buf;
351
352         switch (prop) {
353         case POWER_SUPPLY_PROP_PRESENT:
354         case POWER_SUPPLY_PROP_ONLINE:
355                 val->intval = 1;
356                 break;
357
358         case POWER_SUPPLY_PROP_CAPACITY:
359
360                 buf = kmalloc(2 * sizeof(__u8), GFP_KERNEL);
361                 if (!buf) {
362                         ret = -ENOMEM;
363                         break;
364                 }
365                 ret = dev->hid_get_raw_report(dev, dev->battery_report_id,
366                                               buf, 2,
367                                               dev->battery_report_type);
368
369                 if (ret != 2) {
370                         if (ret >= 0)
371                                 ret = -EINVAL;
372                         kfree(buf);
373                         break;
374                 }
375
376                 if (dev->battery_min < dev->battery_max &&
377                     buf[1] >= dev->battery_min &&
378                     buf[1] <= dev->battery_max)
379                         val->intval = (100 * (buf[1] - dev->battery_min)) /
380                                 (dev->battery_max - dev->battery_min);
381                 kfree(buf);
382                 break;
383
384         case POWER_SUPPLY_PROP_MODEL_NAME:
385                 val->strval = dev->name;
386                 break;
387
388         case POWER_SUPPLY_PROP_STATUS:
389                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
390                 break;
391
392         case POWER_SUPPLY_PROP_SCOPE:
393                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
394                 break;
395
396         default:
397                 ret = -EINVAL;
398                 break;
399         }
400
401         return ret;
402 }
403
404 static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type, struct hid_field *field)
405 {
406         struct power_supply *battery = &dev->battery;
407         int ret;
408         unsigned quirks;
409         s32 min, max;
410
411         if (field->usage->hid != HID_DC_BATTERYSTRENGTH)
412                 return false;   /* no match */
413
414         if (battery->name != NULL)
415                 goto out;       /* already initialized? */
416
417         battery->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
418         if (battery->name == NULL)
419                 goto out;
420
421         battery->type = POWER_SUPPLY_TYPE_BATTERY;
422         battery->properties = hidinput_battery_props;
423         battery->num_properties = ARRAY_SIZE(hidinput_battery_props);
424         battery->use_for_apm = 0;
425         battery->get_property = hidinput_get_battery_property;
426
427         quirks = find_battery_quirk(dev);
428
429         hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
430                 dev->bus, dev->vendor, dev->product, dev->version, quirks);
431
432         min = field->logical_minimum;
433         max = field->logical_maximum;
434
435         if (quirks & HID_BATTERY_QUIRK_PERCENT) {
436                 min = 0;
437                 max = 100;
438         }
439
440         if (quirks & HID_BATTERY_QUIRK_FEATURE)
441                 report_type = HID_FEATURE_REPORT;
442
443         dev->battery_min = min;
444         dev->battery_max = max;
445         dev->battery_report_type = report_type;
446         dev->battery_report_id = field->report->id;
447
448         ret = power_supply_register(&dev->dev, battery);
449         if (ret != 0) {
450                 hid_warn(dev, "can't register power supply: %d\n", ret);
451                 kfree(battery->name);
452                 battery->name = NULL;
453         }
454
455         power_supply_powers(battery, &dev->dev);
456
457 out:
458         return true;
459 }
460
461 static void hidinput_cleanup_battery(struct hid_device *dev)
462 {
463         if (!dev->battery.name)
464                 return;
465
466         power_supply_unregister(&dev->battery);
467         kfree(dev->battery.name);
468         dev->battery.name = NULL;
469 }
470 #else  /* !CONFIG_HID_BATTERY_STRENGTH */
471 static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
472                                    struct hid_field *field)
473 {
474         return false;
475 }
476
477 static void hidinput_cleanup_battery(struct hid_device *dev)
478 {
479 }
480 #endif  /* CONFIG_HID_BATTERY_STRENGTH */
481
482 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
483                                      struct hid_usage *usage)
484 {
485         struct input_dev *input = hidinput->input;
486         struct hid_device *device = input_get_drvdata(input);
487         int max = 0, code;
488         unsigned long *bit = NULL;
489
490         field->hidinput = hidinput;
491
492         if (field->flags & HID_MAIN_ITEM_CONSTANT)
493                 goto ignore;
494
495         /* Ignore if report count is out of bounds. */
496         if (field->report_count < 1)
497                 goto ignore;
498
499         /* only LED usages are supported in output fields */
500         if (field->report_type == HID_OUTPUT_REPORT &&
501                         (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
502                 goto ignore;
503         }
504
505         if (device->driver->input_mapping) {
506                 int ret = device->driver->input_mapping(device, hidinput, field,
507                                 usage, &bit, &max);
508                 if (ret > 0)
509                         goto mapped;
510                 if (ret < 0)
511                         goto ignore;
512         }
513
514         switch (usage->hid & HID_USAGE_PAGE) {
515         case HID_UP_UNDEFINED:
516                 goto ignore;
517
518         case HID_UP_KEYBOARD:
519                 set_bit(EV_REP, input->evbit);
520
521                 if ((usage->hid & HID_USAGE) < 256) {
522                         if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
523                         map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
524                 } else
525                         map_key(KEY_UNKNOWN);
526
527                 break;
528
529         case HID_UP_BUTTON:
530                 code = ((usage->hid - 1) & HID_USAGE);
531
532                 switch (field->application) {
533                 case HID_GD_MOUSE:
534                 case HID_GD_POINTER:  code += BTN_MOUSE; break;
535                 case HID_GD_JOYSTICK:
536                                 if (code <= 0xf)
537                                         code += BTN_JOYSTICK;
538                                 else
539                                         code += BTN_TRIGGER_HAPPY - 0x10;
540                                 break;
541                 case HID_GD_GAMEPAD:
542                                 if (code <= 0xf)
543                                         code += BTN_GAMEPAD;
544                                 else
545                                         code += BTN_TRIGGER_HAPPY - 0x10;
546                                 break;
547                 default:
548                         switch (field->physical) {
549                         case HID_GD_MOUSE:
550                         case HID_GD_POINTER:  code += BTN_MOUSE; break;
551                         case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
552                         case HID_GD_GAMEPAD:  code += BTN_GAMEPAD; break;
553                         default:              code += BTN_MISC;
554                         }
555                 }
556
557                 map_key(code);
558                 break;
559
560         case HID_UP_SIMULATION:
561                 switch (usage->hid & 0xffff) {
562                 case 0xba: map_abs(ABS_RUDDER);   break;
563                 case 0xbb: map_abs(ABS_THROTTLE); break;
564                 case 0xc4: map_abs(ABS_GAS);      break;
565                 case 0xc5: map_abs(ABS_BRAKE);    break;
566                 case 0xc8: map_abs(ABS_WHEEL);    break;
567                 default:   goto ignore;
568                 }
569                 break;
570
571         case HID_UP_GENDESK:
572                 if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
573                         switch (usage->hid & 0xf) {
574                         case 0x1: map_key_clear(KEY_POWER);  break;
575                         case 0x2: map_key_clear(KEY_SLEEP);  break;
576                         case 0x3: map_key_clear(KEY_WAKEUP); break;
577                         case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
578                         case 0x5: map_key_clear(KEY_MENU); break;
579                         case 0x6: map_key_clear(KEY_PROG1); break;
580                         case 0x7: map_key_clear(KEY_HELP); break;
581                         case 0x8: map_key_clear(KEY_EXIT); break;
582                         case 0x9: map_key_clear(KEY_SELECT); break;
583                         case 0xa: map_key_clear(KEY_RIGHT); break;
584                         case 0xb: map_key_clear(KEY_LEFT); break;
585                         case 0xc: map_key_clear(KEY_UP); break;
586                         case 0xd: map_key_clear(KEY_DOWN); break;
587                         case 0xe: map_key_clear(KEY_POWER2); break;
588                         case 0xf: map_key_clear(KEY_RESTART); break;
589                         default: goto unknown;
590                         }
591                         break;
592                 }
593
594                 if ((usage->hid & 0xf0) == 0x90) {      /* D-pad */
595                         switch (usage->hid) {
596                         case HID_GD_UP:    usage->hat_dir = 1; break;
597                         case HID_GD_DOWN:  usage->hat_dir = 5; break;
598                         case HID_GD_RIGHT: usage->hat_dir = 3; break;
599                         case HID_GD_LEFT:  usage->hat_dir = 7; break;
600                         default: goto unknown;
601                         }
602                         if (field->dpad) {
603                                 map_abs(field->dpad);
604                                 goto ignore;
605                         }
606                         map_abs(ABS_HAT0X);
607                         break;
608                 }
609
610                 switch (usage->hid) {
611                 /* These usage IDs map directly to the usage codes. */
612                 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
613                 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
614                 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
615                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
616                                 map_rel(usage->hid & 0xf);
617                         else
618                                 map_abs(usage->hid & 0xf);
619                         break;
620
621                 case HID_GD_HATSWITCH:
622                         usage->hat_min = field->logical_minimum;
623                         usage->hat_max = field->logical_maximum;
624                         map_abs(ABS_HAT0X);
625                         break;
626
627                 case HID_GD_START:      map_key_clear(BTN_START);       break;
628                 case HID_GD_SELECT:     map_key_clear(BTN_SELECT);      break;
629
630                 default: goto unknown;
631                 }
632
633                 break;
634
635         case HID_UP_LED:
636                 switch (usage->hid & 0xffff) {                /* HID-Value:                   */
637                 case 0x01:  map_led (LED_NUML);     break;    /*   "Num Lock"                 */
638                 case 0x02:  map_led (LED_CAPSL);    break;    /*   "Caps Lock"                */
639                 case 0x03:  map_led (LED_SCROLLL);  break;    /*   "Scroll Lock"              */
640                 case 0x04:  map_led (LED_COMPOSE);  break;    /*   "Compose"                  */
641                 case 0x05:  map_led (LED_KANA);     break;    /*   "Kana"                     */
642                 case 0x27:  map_led (LED_SLEEP);    break;    /*   "Stand-By"                 */
643                 case 0x4c:  map_led (LED_SUSPEND);  break;    /*   "System Suspend"           */
644                 case 0x09:  map_led (LED_MUTE);     break;    /*   "Mute"                     */
645                 case 0x4b:  map_led (LED_MISC);     break;    /*   "Generic Indicator"        */
646                 case 0x19:  map_led (LED_MAIL);     break;    /*   "Message Waiting"          */
647                 case 0x4d:  map_led (LED_CHARGING); break;    /*   "External Power Connected" */
648
649                 default: goto ignore;
650                 }
651                 break;
652
653         case HID_UP_DIGITIZER:
654                 switch (usage->hid & 0xff) {
655                 case 0x00: /* Undefined */
656                         goto ignore;
657
658                 case 0x30: /* TipPressure */
659                         if (!test_bit(BTN_TOUCH, input->keybit)) {
660                                 device->quirks |= HID_QUIRK_NOTOUCH;
661                                 set_bit(EV_KEY, input->evbit);
662                                 set_bit(BTN_TOUCH, input->keybit);
663                         }
664                         map_abs_clear(ABS_PRESSURE);
665                         break;
666
667                 case 0x32: /* InRange */
668                         switch (field->physical & 0xff) {
669                         case 0x21: map_key(BTN_TOOL_MOUSE); break;
670                         case 0x22: map_key(BTN_TOOL_FINGER); break;
671                         default: map_key(BTN_TOOL_PEN); break;
672                         }
673                         break;
674
675                 case 0x3c: /* Invert */
676                         map_key_clear(BTN_TOOL_RUBBER);
677                         break;
678
679                 case 0x3d: /* X Tilt */
680                         map_abs_clear(ABS_TILT_X);
681                         break;
682
683                 case 0x3e: /* Y Tilt */
684                         map_abs_clear(ABS_TILT_Y);
685                         break;
686
687                 case 0x33: /* Touch */
688                 case 0x42: /* TipSwitch */
689                 case 0x43: /* TipSwitch2 */
690                         device->quirks &= ~HID_QUIRK_NOTOUCH;
691                         map_key_clear(BTN_TOUCH);
692                         break;
693
694                 case 0x44: /* BarrelSwitch */
695                         map_key_clear(BTN_STYLUS);
696                         break;
697
698                 case 0x46: /* TabletPick */
699                         map_key_clear(BTN_STYLUS2);
700                         break;
701
702                 default:  goto unknown;
703                 }
704                 break;
705
706         case HID_UP_CONSUMER:   /* USB HUT v1.12, pages 75-84 */
707                 switch (usage->hid & HID_USAGE) {
708                 case 0x000: goto ignore;
709                 case 0x030: map_key_clear(KEY_POWER);           break;
710                 case 0x031: map_key_clear(KEY_RESTART);         break;
711                 case 0x032: map_key_clear(KEY_SLEEP);           break;
712                 case 0x034: map_key_clear(KEY_SLEEP);           break;
713                 case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE);  break;
714                 case 0x036: map_key_clear(BTN_MISC);            break;
715
716                 case 0x040: map_key_clear(KEY_MENU);            break; /* Menu */
717                 case 0x041: map_key_clear(KEY_SELECT);          break; /* Menu Pick */
718                 case 0x042: map_key_clear(KEY_UP);              break; /* Menu Up */
719                 case 0x043: map_key_clear(KEY_DOWN);            break; /* Menu Down */
720                 case 0x044: map_key_clear(KEY_LEFT);            break; /* Menu Left */
721                 case 0x045: map_key_clear(KEY_RIGHT);           break; /* Menu Right */
722                 case 0x046: map_key_clear(KEY_ESC);             break; /* Menu Escape */
723                 case 0x047: map_key_clear(KEY_KPPLUS);          break; /* Menu Value Increase */
724                 case 0x048: map_key_clear(KEY_KPMINUS);         break; /* Menu Value Decrease */
725
726                 case 0x060: map_key_clear(KEY_INFO);            break; /* Data On Screen */
727                 case 0x061: map_key_clear(KEY_SUBTITLE);        break; /* Closed Caption */
728                 case 0x063: map_key_clear(KEY_VCR);             break; /* VCR/TV */
729                 case 0x065: map_key_clear(KEY_CAMERA);          break; /* Snapshot */
730                 case 0x069: map_key_clear(KEY_RED);             break;
731                 case 0x06a: map_key_clear(KEY_GREEN);           break;
732                 case 0x06b: map_key_clear(KEY_BLUE);            break;
733                 case 0x06c: map_key_clear(KEY_YELLOW);          break;
734                 case 0x06d: map_key_clear(KEY_ZOOM);            break;
735
736                 case 0x06f: map_key_clear(KEY_BRIGHTNESSUP);            break;
737                 case 0x070: map_key_clear(KEY_BRIGHTNESSDOWN);          break;
738                 case 0x072: map_key_clear(KEY_BRIGHTNESS_TOGGLE);       break;
739                 case 0x073: map_key_clear(KEY_BRIGHTNESS_MIN);          break;
740                 case 0x074: map_key_clear(KEY_BRIGHTNESS_MAX);          break;
741                 case 0x075: map_key_clear(KEY_BRIGHTNESS_AUTO);         break;
742
743                 case 0x082: map_key_clear(KEY_VIDEO_NEXT);      break;
744                 case 0x083: map_key_clear(KEY_LAST);            break;
745                 case 0x084: map_key_clear(KEY_ENTER);           break;
746                 case 0x088: map_key_clear(KEY_PC);              break;
747                 case 0x089: map_key_clear(KEY_TV);              break;
748                 case 0x08a: map_key_clear(KEY_WWW);             break;
749                 case 0x08b: map_key_clear(KEY_DVD);             break;
750                 case 0x08c: map_key_clear(KEY_PHONE);           break;
751                 case 0x08d: map_key_clear(KEY_PROGRAM);         break;
752                 case 0x08e: map_key_clear(KEY_VIDEOPHONE);      break;
753                 case 0x08f: map_key_clear(KEY_GAMES);           break;
754                 case 0x090: map_key_clear(KEY_MEMO);            break;
755                 case 0x091: map_key_clear(KEY_CD);              break;
756                 case 0x092: map_key_clear(KEY_VCR);             break;
757                 case 0x093: map_key_clear(KEY_TUNER);           break;
758                 case 0x094: map_key_clear(KEY_EXIT);            break;
759                 case 0x095: map_key_clear(KEY_HELP);            break;
760                 case 0x096: map_key_clear(KEY_TAPE);            break;
761                 case 0x097: map_key_clear(KEY_TV2);             break;
762                 case 0x098: map_key_clear(KEY_SAT);             break;
763                 case 0x09a: map_key_clear(KEY_PVR);             break;
764
765                 case 0x09c: map_key_clear(KEY_CHANNELUP);       break;
766                 case 0x09d: map_key_clear(KEY_CHANNELDOWN);     break;
767                 case 0x0a0: map_key_clear(KEY_VCR2);            break;
768
769                 case 0x0b0: map_key_clear(KEY_PLAY);            break;
770                 case 0x0b1: map_key_clear(KEY_PAUSE);           break;
771                 case 0x0b2: map_key_clear(KEY_RECORD);          break;
772                 case 0x0b3: map_key_clear(KEY_FASTFORWARD);     break;
773                 case 0x0b4: map_key_clear(KEY_REWIND);          break;
774                 case 0x0b5: map_key_clear(KEY_NEXTSONG);        break;
775                 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
776                 case 0x0b7: map_key_clear(KEY_STOPCD);          break;
777                 case 0x0b8: map_key_clear(KEY_EJECTCD);         break;
778                 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
779                 case 0x0b9: map_key_clear(KEY_SHUFFLE);         break;
780                 case 0x0bf: map_key_clear(KEY_SLOW);            break;
781
782                 case 0x0cd: map_key_clear(KEY_PLAYPAUSE);       break;
783                 case 0x0cf: map_key_clear(KEY_VOICECOMMAND);    break;
784                 case 0x0e0: map_abs_clear(ABS_VOLUME);          break;
785                 case 0x0e2: map_key_clear(KEY_MUTE);            break;
786                 case 0x0e5: map_key_clear(KEY_BASSBOOST);       break;
787                 case 0x0e9: map_key_clear(KEY_VOLUMEUP);        break;
788                 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);      break;
789                 case 0x0f5: map_key_clear(KEY_SLOW);            break;
790
791                 case 0x181: map_key_clear(KEY_BUTTONCONFIG);    break;
792                 case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
793                 case 0x183: map_key_clear(KEY_CONFIG);          break;
794                 case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
795                 case 0x185: map_key_clear(KEY_EDITOR);          break;
796                 case 0x186: map_key_clear(KEY_SPREADSHEET);     break;
797                 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);  break;
798                 case 0x188: map_key_clear(KEY_PRESENTATION);    break;
799                 case 0x189: map_key_clear(KEY_DATABASE);        break;
800                 case 0x18a: map_key_clear(KEY_MAIL);            break;
801                 case 0x18b: map_key_clear(KEY_NEWS);            break;
802                 case 0x18c: map_key_clear(KEY_VOICEMAIL);       break;
803                 case 0x18d: map_key_clear(KEY_ADDRESSBOOK);     break;
804                 case 0x18e: map_key_clear(KEY_CALENDAR);        break;
805                 case 0x18f: map_key_clear(KEY_TASKMANAGER);     break;
806                 case 0x190: map_key_clear(KEY_JOURNAL);         break;
807                 case 0x191: map_key_clear(KEY_FINANCE);         break;
808                 case 0x192: map_key_clear(KEY_CALC);            break;
809                 case 0x193: map_key_clear(KEY_PLAYER);          break;
810                 case 0x194: map_key_clear(KEY_FILE);            break;
811                 case 0x196: map_key_clear(KEY_WWW);             break;
812                 case 0x199: map_key_clear(KEY_CHAT);            break;
813                 case 0x19c: map_key_clear(KEY_LOGOFF);          break;
814                 case 0x19e: map_key_clear(KEY_COFFEE);          break;
815                 case 0x19f: map_key_clear(KEY_CONTROLPANEL);            break;
816                 case 0x1a2: map_key_clear(KEY_APPSELECT);               break;
817                 case 0x1a3: map_key_clear(KEY_NEXT);            break;
818                 case 0x1a4: map_key_clear(KEY_PREVIOUS);        break;
819                 case 0x1a6: map_key_clear(KEY_HELP);            break;
820                 case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
821                 case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
822                 case 0x1ae: map_key_clear(KEY_KEYBOARD);        break;
823                 case 0x1b1: map_key_clear(KEY_SCREENSAVER);             break;
824                 case 0x1b4: map_key_clear(KEY_FILE);            break;
825                 case 0x1b6: map_key_clear(KEY_IMAGES);          break;
826                 case 0x1b7: map_key_clear(KEY_AUDIO);           break;
827                 case 0x1b8: map_key_clear(KEY_VIDEO);           break;
828                 case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
829                 case 0x1bd: map_key_clear(KEY_INFO);            break;
830                 case 0x201: map_key_clear(KEY_NEW);             break;
831                 case 0x202: map_key_clear(KEY_OPEN);            break;
832                 case 0x203: map_key_clear(KEY_CLOSE);           break;
833                 case 0x204: map_key_clear(KEY_EXIT);            break;
834                 case 0x207: map_key_clear(KEY_SAVE);            break;
835                 case 0x208: map_key_clear(KEY_PRINT);           break;
836                 case 0x209: map_key_clear(KEY_PROPS);           break;
837                 case 0x21a: map_key_clear(KEY_UNDO);            break;
838                 case 0x21b: map_key_clear(KEY_COPY);            break;
839                 case 0x21c: map_key_clear(KEY_CUT);             break;
840                 case 0x21d: map_key_clear(KEY_PASTE);           break;
841                 case 0x21f: map_key_clear(KEY_FIND);            break;
842                 case 0x221: map_key_clear(KEY_SEARCH);          break;
843                 case 0x222: map_key_clear(KEY_GOTO);            break;
844                 case 0x223: map_key_clear(KEY_HOMEPAGE);        break;
845                 case 0x224: map_key_clear(KEY_BACK);            break;
846                 case 0x225: map_key_clear(KEY_FORWARD);         break;
847                 case 0x226: map_key_clear(KEY_STOP);            break;
848                 case 0x227: map_key_clear(KEY_REFRESH);         break;
849                 case 0x22a: map_key_clear(KEY_BOOKMARKS);       break;
850                 case 0x22d: map_key_clear(KEY_ZOOMIN);          break;
851                 case 0x22e: map_key_clear(KEY_ZOOMOUT);         break;
852                 case 0x22f: map_key_clear(KEY_ZOOMRESET);       break;
853                 case 0x233: map_key_clear(KEY_SCROLLUP);        break;
854                 case 0x234: map_key_clear(KEY_SCROLLDOWN);      break;
855                 case 0x238: map_rel(REL_HWHEEL);                break;
856                 case 0x23d: map_key_clear(KEY_EDIT);            break;
857                 case 0x25f: map_key_clear(KEY_CANCEL);          break;
858                 case 0x269: map_key_clear(KEY_INSERT);          break;
859                 case 0x26a: map_key_clear(KEY_DELETE);          break;
860                 case 0x279: map_key_clear(KEY_REDO);            break;
861
862                 case 0x289: map_key_clear(KEY_REPLY);           break;
863                 case 0x28b: map_key_clear(KEY_FORWARDMAIL);     break;
864                 case 0x28c: map_key_clear(KEY_SEND);            break;
865
866                 default:    goto ignore;
867                 }
868                 break;
869
870         case HID_UP_GENDEVCTRLS:
871                 if (hidinput_setup_battery(device, HID_INPUT_REPORT, field))
872                         goto ignore;
873                 else
874                         goto unknown;
875                 break;
876
877         case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
878                 set_bit(EV_REP, input->evbit);
879                 switch (usage->hid & HID_USAGE) {
880                 case 0x021: map_key_clear(KEY_PRINT);           break;
881                 case 0x070: map_key_clear(KEY_HP);              break;
882                 case 0x071: map_key_clear(KEY_CAMERA);          break;
883                 case 0x072: map_key_clear(KEY_SOUND);           break;
884                 case 0x073: map_key_clear(KEY_QUESTION);        break;
885                 case 0x080: map_key_clear(KEY_EMAIL);           break;
886                 case 0x081: map_key_clear(KEY_CHAT);            break;
887                 case 0x082: map_key_clear(KEY_SEARCH);          break;
888                 case 0x083: map_key_clear(KEY_CONNECT);         break;
889                 case 0x084: map_key_clear(KEY_FINANCE);         break;
890                 case 0x085: map_key_clear(KEY_SPORT);           break;
891                 case 0x086: map_key_clear(KEY_SHOP);            break;
892                 default:    goto ignore;
893                 }
894                 break;
895
896         case HID_UP_HPVENDOR2:
897                 set_bit(EV_REP, input->evbit);
898                 switch (usage->hid & HID_USAGE) {
899                 case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN);  break;
900                 case 0x004: map_key_clear(KEY_BRIGHTNESSUP);    break;
901                 default:    goto ignore;
902                 }
903                 break;
904
905         case HID_UP_MSVENDOR:
906                 goto ignore;
907
908         case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
909                 set_bit(EV_REP, input->evbit);
910                 goto ignore;
911
912         case HID_UP_LOGIVENDOR:
913                 goto ignore;
914
915         case HID_UP_PID:
916                 switch (usage->hid & HID_USAGE) {
917                 case 0xa4: map_key_clear(BTN_DEAD);     break;
918                 default: goto ignore;
919                 }
920                 break;
921
922         default:
923         unknown:
924                 if (field->report_size == 1) {
925                         if (field->report->type == HID_OUTPUT_REPORT) {
926                                 map_led(LED_MISC);
927                                 break;
928                         }
929                         map_key(BTN_MISC);
930                         break;
931                 }
932                 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
933                         map_rel(REL_MISC);
934                         break;
935                 }
936                 map_abs(ABS_MISC);
937                 break;
938         }
939
940 mapped:
941         if (device->driver->input_mapped && device->driver->input_mapped(device,
942                                 hidinput, field, usage, &bit, &max) < 0)
943                 goto ignore;
944
945         set_bit(usage->type, input->evbit);
946
947         while (usage->code <= max && test_and_set_bit(usage->code, bit))
948                 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
949
950         if (usage->code > max)
951                 goto ignore;
952
953
954         if (usage->type == EV_ABS) {
955
956                 int a = field->logical_minimum;
957                 int b = field->logical_maximum;
958
959                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
960                         a = field->logical_minimum = 0;
961                         b = field->logical_maximum = 255;
962                 }
963
964                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
965                         input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
966                 else    input_set_abs_params(input, usage->code, a, b, 0, 0);
967
968                 input_abs_set_res(input, usage->code,
969                                   hidinput_calc_abs_res(field, usage->code));
970
971                 /* use a larger default input buffer for MT devices */
972                 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
973                         input_set_events_per_packet(input, 60);
974         }
975
976         if (usage->type == EV_ABS &&
977             (usage->hat_min < usage->hat_max || usage->hat_dir)) {
978                 int i;
979                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
980                         input_set_abs_params(input, i, -1, 1, 0, 0);
981                         set_bit(i, input->absbit);
982                 }
983                 if (usage->hat_dir && !field->dpad)
984                         field->dpad = usage->code;
985         }
986
987         /* for those devices which produce Consumer volume usage as relative,
988          * we emulate pressing volumeup/volumedown appropriate number of times
989          * in hidinput_hid_event()
990          */
991         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
992                         (usage->code == ABS_VOLUME)) {
993                 set_bit(KEY_VOLUMEUP, input->keybit);
994                 set_bit(KEY_VOLUMEDOWN, input->keybit);
995         }
996
997         if (usage->type == EV_KEY) {
998                 set_bit(EV_MSC, input->evbit);
999                 set_bit(MSC_SCAN, input->mscbit);
1000         }
1001
1002 ignore:
1003         return;
1004
1005 }
1006
1007 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
1008 {
1009         struct input_dev *input;
1010         unsigned *quirks = &hid->quirks;
1011
1012         if (!field->hidinput)
1013                 return;
1014
1015         input = field->hidinput->input;
1016
1017         if (!usage->type)
1018                 return;
1019
1020         trace_hidinput_hid_event("hid_event");
1021
1022         if (usage->hat_min < usage->hat_max || usage->hat_dir) {
1023                 int hat_dir = usage->hat_dir;
1024                 if (!hat_dir)
1025                         hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
1026                 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
1027                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
1028                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
1029                 return;
1030         }
1031
1032         if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
1033                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
1034                 return;
1035         }
1036
1037         if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
1038                 if (value) {
1039                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
1040                         return;
1041                 }
1042                 input_event(input, usage->type, usage->code, 0);
1043                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
1044                 return;
1045         }
1046
1047         if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
1048                 int a = field->logical_minimum;
1049                 int b = field->logical_maximum;
1050                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
1051         }
1052
1053         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
1054                 dbg_hid("Maximum Effects - %d\n",value);
1055                 return;
1056         }
1057
1058         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
1059                 dbg_hid("PID Pool Report\n");
1060                 return;
1061         }
1062
1063         if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
1064                 return;
1065
1066         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
1067                         (usage->code == ABS_VOLUME)) {
1068                 int count = abs(value);
1069                 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
1070                 int i;
1071
1072                 for (i = 0; i < count; i++) {
1073                         input_event(input, EV_KEY, direction, 1);
1074                         input_sync(input);
1075                         input_event(input, EV_KEY, direction, 0);
1076                         input_sync(input);
1077                 }
1078                 return;
1079         }
1080
1081         /*
1082          * Ignore out-of-range values as per HID specification,
1083          * section 5.10 and 6.2.25
1084          */
1085         if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
1086             (value < field->logical_minimum ||
1087              value > field->logical_maximum)) {
1088                 dbg_hid("Ignoring out-of-range value %x\n", value);
1089                 return;
1090         }
1091
1092         /*
1093          * Ignore reports for absolute data if the data didn't change. This is
1094          * not only an optimization but also fixes 'dead' key reports. Some
1095          * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
1096          * 0x31 and 0x32) report multiple keys, even though a localized keyboard
1097          * can only have one of them physically available. The 'dead' keys
1098          * report constant 0. As all map to the same keycode, they'd confuse
1099          * the input layer. If we filter the 'dead' keys on the HID level, we
1100          * skip the keycode translation and only forward real events.
1101          */
1102         if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
1103                               HID_MAIN_ITEM_BUFFERED_BYTE)) &&
1104                               (field->flags & HID_MAIN_ITEM_VARIABLE) &&
1105             usage->usage_index < field->maxusage &&
1106             value == field->value[usage->usage_index])
1107                 return;
1108
1109         /* report the usage code as scancode if the key status has changed */
1110         if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
1111                 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
1112
1113         input_event(input, usage->type, usage->code, value);
1114
1115         if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
1116                 input_event(input, usage->type, usage->code, 0);
1117 }
1118
1119 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
1120 {
1121         struct hid_input *hidinput;
1122
1123         if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
1124                 return;
1125
1126         list_for_each_entry(hidinput, &hid->inputs, list)
1127                 input_sync(hidinput->input);
1128 }
1129 EXPORT_SYMBOL_GPL(hidinput_report_event);
1130
1131 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1132 {
1133         struct hid_report *report;
1134         int i, j;
1135
1136         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
1137                 for (i = 0; i < report->maxfield; i++) {
1138                         *field = report->field[i];
1139                         for (j = 0; j < (*field)->maxusage; j++)
1140                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1141                                         return j;
1142                 }
1143         }
1144         return -1;
1145 }
1146 EXPORT_SYMBOL_GPL(hidinput_find_field);
1147
1148 struct hid_field *hidinput_get_led_field(struct hid_device *hid)
1149 {
1150         struct hid_report *report;
1151         struct hid_field *field;
1152         int i, j;
1153
1154         list_for_each_entry(report,
1155                             &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1156                             list) {
1157                 for (i = 0; i < report->maxfield; i++) {
1158                         field = report->field[i];
1159                         for (j = 0; j < field->maxusage; j++)
1160                                 if (field->usage[j].type == EV_LED)
1161                                         return field;
1162                 }
1163         }
1164         return NULL;
1165 }
1166 EXPORT_SYMBOL_GPL(hidinput_get_led_field);
1167
1168 unsigned int hidinput_count_leds(struct hid_device *hid)
1169 {
1170         struct hid_report *report;
1171         struct hid_field *field;
1172         int i, j;
1173         unsigned int count = 0;
1174
1175         list_for_each_entry(report,
1176                             &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1177                             list) {
1178                 for (i = 0; i < report->maxfield; i++) {
1179                         field = report->field[i];
1180                         for (j = 0; j < field->maxusage; j++)
1181                                 if (field->usage[j].type == EV_LED &&
1182                                     field->value[j])
1183                                         count += 1;
1184                 }
1185         }
1186         return count;
1187 }
1188 EXPORT_SYMBOL_GPL(hidinput_count_leds);
1189
1190 static int hidinput_open(struct input_dev *dev)
1191 {
1192         struct hid_device *hid = input_get_drvdata(dev);
1193
1194         return hid_hw_open(hid);
1195 }
1196
1197 static void hidinput_close(struct input_dev *dev)
1198 {
1199         struct hid_device *hid = input_get_drvdata(dev);
1200
1201         hid_hw_close(hid);
1202 }
1203
1204 static void report_features(struct hid_device *hid)
1205 {
1206         struct hid_driver *drv = hid->driver;
1207         struct hid_report_enum *rep_enum;
1208         struct hid_report *rep;
1209         int i, j;
1210
1211         rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1212         list_for_each_entry(rep, &rep_enum->report_list, list)
1213                 for (i = 0; i < rep->maxfield; i++) {
1214                         /* Ignore if report count is out of bounds. */
1215                         if (rep->field[i]->report_count < 1)
1216                                 continue;
1217
1218                         for (j = 0; j < rep->field[i]->maxusage; j++) {
1219                                 /* Verify if Battery Strength feature is available */
1220                                 hidinput_setup_battery(hid, HID_FEATURE_REPORT, rep->field[i]);
1221
1222                                 if (drv->feature_mapping)
1223                                         drv->feature_mapping(hid, rep->field[i],
1224                                                              rep->field[i]->usage + j);
1225                         }
1226                 }
1227 }
1228
1229 static struct hid_input *hidinput_allocate(struct hid_device *hid)
1230 {
1231         struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1232         struct input_dev *input_dev = input_allocate_device();
1233         if (!hidinput || !input_dev) {
1234                 kfree(hidinput);
1235                 input_free_device(input_dev);
1236                 hid_err(hid, "Out of memory during hid input probe\n");
1237                 return NULL;
1238         }
1239
1240         input_set_drvdata(input_dev, hid);
1241         input_dev->event = hid->ll_driver->hidinput_input_event;
1242         input_dev->open = hidinput_open;
1243         input_dev->close = hidinput_close;
1244         input_dev->setkeycode = hidinput_setkeycode;
1245         input_dev->getkeycode = hidinput_getkeycode;
1246
1247         input_dev->name = hid->name;
1248         input_dev->phys = hid->phys;
1249         input_dev->uniq = hid->uniq;
1250         input_dev->id.bustype = hid->bus;
1251         input_dev->id.vendor  = hid->vendor;
1252         input_dev->id.product = hid->product;
1253         input_dev->id.version = hid->version;
1254         input_dev->dev.parent = hid->dev.parent;
1255         hidinput->input = input_dev;
1256         list_add_tail(&hidinput->list, &hid->inputs);
1257
1258         return hidinput;
1259 }
1260
1261 static bool hidinput_has_been_populated(struct hid_input *hidinput)
1262 {
1263         int i;
1264         unsigned long r = 0;
1265
1266         for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
1267                 r |= hidinput->input->evbit[i];
1268
1269         for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
1270                 r |= hidinput->input->keybit[i];
1271
1272         for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
1273                 r |= hidinput->input->relbit[i];
1274
1275         for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
1276                 r |= hidinput->input->absbit[i];
1277
1278         for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
1279                 r |= hidinput->input->mscbit[i];
1280
1281         for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
1282                 r |= hidinput->input->ledbit[i];
1283
1284         for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
1285                 r |= hidinput->input->sndbit[i];
1286
1287         for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
1288                 r |= hidinput->input->ffbit[i];
1289
1290         for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
1291                 r |= hidinput->input->swbit[i];
1292
1293         return !!r;
1294 }
1295
1296 static void hidinput_cleanup_hidinput(struct hid_device *hid,
1297                 struct hid_input *hidinput)
1298 {
1299         struct hid_report *report;
1300         int i, k;
1301
1302         list_del(&hidinput->list);
1303         input_free_device(hidinput->input);
1304
1305         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1306                 if (k == HID_OUTPUT_REPORT &&
1307                         hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1308                         continue;
1309
1310                 list_for_each_entry(report, &hid->report_enum[k].report_list,
1311                                     list) {
1312
1313                         for (i = 0; i < report->maxfield; i++)
1314                                 if (report->field[i]->hidinput == hidinput)
1315                                         report->field[i]->hidinput = NULL;
1316                 }
1317         }
1318
1319         kfree(hidinput);
1320 }
1321
1322 /*
1323  * Register the input device; print a message.
1324  * Configure the input layer interface
1325  * Read all reports and initialize the absolute field values.
1326  */
1327
1328 int hidinput_connect(struct hid_device *hid, unsigned int force)
1329 {
1330         struct hid_driver *drv = hid->driver;
1331         struct hid_report *report;
1332         struct hid_input *hidinput = NULL;
1333         int i, j, k;
1334
1335         INIT_LIST_HEAD(&hid->inputs);
1336
1337         if (!force) {
1338                 for (i = 0; i < hid->maxcollection; i++) {
1339                         struct hid_collection *col = &hid->collection[i];
1340                         if (col->type == HID_COLLECTION_APPLICATION ||
1341                                         col->type == HID_COLLECTION_PHYSICAL)
1342                                 if (IS_INPUT_APPLICATION(col->usage))
1343                                         break;
1344                 }
1345
1346                 if (i == hid->maxcollection)
1347                         return -1;
1348         }
1349
1350         report_features(hid);
1351
1352         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1353                 if (k == HID_OUTPUT_REPORT &&
1354                         hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1355                         continue;
1356
1357                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1358
1359                         if (!report->maxfield)
1360                                 continue;
1361
1362                         if (!hidinput) {
1363                                 hidinput = hidinput_allocate(hid);
1364                                 if (!hidinput)
1365                                         goto out_unwind;
1366                         }
1367
1368                         for (i = 0; i < report->maxfield; i++)
1369                                 for (j = 0; j < report->field[i]->maxusage; j++)
1370                                         hidinput_configure_usage(hidinput, report->field[i],
1371                                                                  report->field[i]->usage + j);
1372
1373                         if ((hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
1374                             !hidinput_has_been_populated(hidinput))
1375                                 continue;
1376
1377                         if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
1378                                 /* This will leave hidinput NULL, so that it
1379                                  * allocates another one if we have more inputs on
1380                                  * the same interface. Some devices (e.g. Happ's
1381                                  * UGCI) cram a lot of unrelated inputs into the
1382                                  * same interface. */
1383                                 hidinput->report = report;
1384                                 if (drv->input_configured &&
1385                                     drv->input_configured(hid, hidinput))
1386                                         goto out_cleanup;
1387                                 if (input_register_device(hidinput->input))
1388                                         goto out_cleanup;
1389                                 hidinput = NULL;
1390                         }
1391                 }
1392         }
1393
1394         if (hidinput && (hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
1395             !hidinput_has_been_populated(hidinput)) {
1396                 /* no need to register an input device not populated */
1397                 hidinput_cleanup_hidinput(hid, hidinput);
1398                 hidinput = NULL;
1399         }
1400
1401         if (list_empty(&hid->inputs)) {
1402                 hid_err(hid, "No inputs registered, leaving\n");
1403                 goto out_unwind;
1404         }
1405
1406         if (hidinput) {
1407                 if (drv->input_configured &&
1408                     drv->input_configured(hid, hidinput))
1409                         goto out_cleanup;
1410                 if (input_register_device(hidinput->input))
1411                         goto out_cleanup;
1412         }
1413
1414         return 0;
1415
1416 out_cleanup:
1417         list_del(&hidinput->list);
1418         input_free_device(hidinput->input);
1419         kfree(hidinput);
1420 out_unwind:
1421         /* unwind the ones we already registered */
1422         hidinput_disconnect(hid);
1423
1424         return -1;
1425 }
1426 EXPORT_SYMBOL_GPL(hidinput_connect);
1427
1428 void hidinput_disconnect(struct hid_device *hid)
1429 {
1430         struct hid_input *hidinput, *next;
1431
1432         hidinput_cleanup_battery(hid);
1433
1434         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
1435                 list_del(&hidinput->list);
1436                 input_unregister_device(hidinput->input);
1437                 kfree(hidinput);
1438         }
1439 }
1440 EXPORT_SYMBOL_GPL(hidinput_disconnect);
1441