Merge tag 'v5.15.62' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / hid / hid-input.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2000-2001 Vojtech Pavlik
4  *  Copyright (c) 2006-2010 Jiri Kosina
5  *
6  *  HID to Linux Input mapping
7  */
8
9 /*
10  *
11  * Should you need to contact me, the author, you can do so either by
12  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
13  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
14  */
15
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19
20 #include <linux/hid.h>
21 #include <linux/hid-debug.h>
22
23 #include "hid-ids.h"
24
25 #define unk     KEY_UNKNOWN
26
27 static const unsigned char hid_keyboard[256] = {
28           0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
29          50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
30           4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
31          27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
32          65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
33         105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
34          72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
35         191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
36         115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
37         122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
38         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
39         unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
40         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
41         unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
42          29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
43         150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
44 };
45
46 static const struct {
47         __s32 x;
48         __s32 y;
49 }  hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
50
51 #define map_abs(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
52 #define map_rel(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
53 #define map_key(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
54 #define map_led(c)      hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
55
56 #define map_abs_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
57                 &max, EV_ABS, (c))
58 #define map_key_clear(c)        hid_map_usage_clear(hidinput, usage, &bit, \
59                 &max, EV_KEY, (c))
60
61 static bool match_scancode(struct hid_usage *usage,
62                            unsigned int cur_idx, unsigned int scancode)
63 {
64         return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
65 }
66
67 static bool match_keycode(struct hid_usage *usage,
68                           unsigned int cur_idx, unsigned int keycode)
69 {
70         /*
71          * We should exclude unmapped usages when doing lookup by keycode.
72          */
73         return (usage->type == EV_KEY && usage->code == keycode);
74 }
75
76 static bool match_index(struct hid_usage *usage,
77                         unsigned int cur_idx, unsigned int idx)
78 {
79         return cur_idx == idx;
80 }
81
82 typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
83                                 unsigned int cur_idx, unsigned int val);
84
85 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
86                                            hid_usage_cmp_t match,
87                                            unsigned int value,
88                                            unsigned int *usage_idx)
89 {
90         unsigned int i, j, k, cur_idx = 0;
91         struct hid_report *report;
92         struct hid_usage *usage;
93
94         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
95                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
96                         for (i = 0; i < report->maxfield; i++) {
97                                 for (j = 0; j < report->field[i]->maxusage; j++) {
98                                         usage = report->field[i]->usage + j;
99                                         if (usage->type == EV_KEY || usage->type == 0) {
100                                                 if (match(usage, cur_idx, value)) {
101                                                         if (usage_idx)
102                                                                 *usage_idx = cur_idx;
103                                                         return usage;
104                                                 }
105                                                 cur_idx++;
106                                         }
107                                 }
108                         }
109                 }
110         }
111         return NULL;
112 }
113
114 static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
115                                         const struct input_keymap_entry *ke,
116                                         unsigned int *index)
117 {
118         struct hid_usage *usage;
119         unsigned int scancode;
120
121         if (ke->flags & INPUT_KEYMAP_BY_INDEX)
122                 usage = hidinput_find_key(hid, match_index, ke->index, index);
123         else if (input_scancode_to_scalar(ke, &scancode) == 0)
124                 usage = hidinput_find_key(hid, match_scancode, scancode, index);
125         else
126                 usage = NULL;
127
128         return usage;
129 }
130
131 static int hidinput_getkeycode(struct input_dev *dev,
132                                struct input_keymap_entry *ke)
133 {
134         struct hid_device *hid = input_get_drvdata(dev);
135         struct hid_usage *usage;
136         unsigned int scancode, index;
137
138         usage = hidinput_locate_usage(hid, ke, &index);
139         if (usage) {
140                 ke->keycode = usage->type == EV_KEY ?
141                                 usage->code : KEY_RESERVED;
142                 ke->index = index;
143                 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
144                 ke->len = sizeof(scancode);
145                 memcpy(ke->scancode, &scancode, sizeof(scancode));
146                 return 0;
147         }
148
149         return -EINVAL;
150 }
151
152 static int hidinput_setkeycode(struct input_dev *dev,
153                                const struct input_keymap_entry *ke,
154                                unsigned int *old_keycode)
155 {
156         struct hid_device *hid = input_get_drvdata(dev);
157         struct hid_usage *usage;
158
159         usage = hidinput_locate_usage(hid, ke, NULL);
160         if (usage) {
161                 *old_keycode = usage->type == EV_KEY ?
162                                 usage->code : KEY_RESERVED;
163                 usage->type = EV_KEY;
164                 usage->code = ke->keycode;
165
166                 clear_bit(*old_keycode, dev->keybit);
167                 set_bit(usage->code, dev->keybit);
168                 dbg_hid("Assigned keycode %d to HID usage code %x\n",
169                         usage->code, usage->hid);
170
171                 /*
172                  * Set the keybit for the old keycode if the old keycode is used
173                  * by another key
174                  */
175                 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
176                         set_bit(*old_keycode, dev->keybit);
177
178                 return 0;
179         }
180
181         return -EINVAL;
182 }
183
184
185 /**
186  * hidinput_calc_abs_res - calculate an absolute axis resolution
187  * @field: the HID report field to calculate resolution for
188  * @code: axis code
189  *
190  * The formula is:
191  *                         (logical_maximum - logical_minimum)
192  * resolution = ----------------------------------------------------------
193  *              (physical_maximum - physical_minimum) * 10 ^ unit_exponent
194  *
195  * as seen in the HID specification v1.11 6.2.2.7 Global Items.
196  *
197  * Only exponent 1 length units are processed. Centimeters and inches are
198  * converted to millimeters. Degrees are converted to radians.
199  */
200 __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
201 {
202         __s32 unit_exponent = field->unit_exponent;
203         __s32 logical_extents = field->logical_maximum -
204                                         field->logical_minimum;
205         __s32 physical_extents = field->physical_maximum -
206                                         field->physical_minimum;
207         __s32 prev;
208
209         /* Check if the extents are sane */
210         if (logical_extents <= 0 || physical_extents <= 0)
211                 return 0;
212
213         /*
214          * Verify and convert units.
215          * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
216          */
217         switch (code) {
218         case ABS_X:
219         case ABS_Y:
220         case ABS_Z:
221         case ABS_MT_POSITION_X:
222         case ABS_MT_POSITION_Y:
223         case ABS_MT_TOOL_X:
224         case ABS_MT_TOOL_Y:
225         case ABS_MT_TOUCH_MAJOR:
226         case ABS_MT_TOUCH_MINOR:
227                 if (field->unit == 0x11) {              /* If centimeters */
228                         /* Convert to millimeters */
229                         unit_exponent += 1;
230                 } else if (field->unit == 0x13) {       /* If inches */
231                         /* Convert to millimeters */
232                         prev = physical_extents;
233                         physical_extents *= 254;
234                         if (physical_extents < prev)
235                                 return 0;
236                         unit_exponent -= 1;
237                 } else {
238                         return 0;
239                 }
240                 break;
241
242         case ABS_RX:
243         case ABS_RY:
244         case ABS_RZ:
245         case ABS_WHEEL:
246         case ABS_TILT_X:
247         case ABS_TILT_Y:
248                 if (field->unit == 0x14) {              /* If degrees */
249                         /* Convert to radians */
250                         prev = logical_extents;
251                         logical_extents *= 573;
252                         if (logical_extents < prev)
253                                 return 0;
254                         unit_exponent += 1;
255                 } else if (field->unit != 0x12) {       /* If not radians */
256                         return 0;
257                 }
258                 break;
259
260         default:
261                 return 0;
262         }
263
264         /* Apply negative unit exponent */
265         for (; unit_exponent < 0; unit_exponent++) {
266                 prev = logical_extents;
267                 logical_extents *= 10;
268                 if (logical_extents < prev)
269                         return 0;
270         }
271         /* Apply positive unit exponent */
272         for (; unit_exponent > 0; unit_exponent--) {
273                 prev = physical_extents;
274                 physical_extents *= 10;
275                 if (physical_extents < prev)
276                         return 0;
277         }
278
279         /* Calculate resolution */
280         return DIV_ROUND_CLOSEST(logical_extents, physical_extents);
281 }
282 EXPORT_SYMBOL_GPL(hidinput_calc_abs_res);
283
284 #ifdef CONFIG_HID_BATTERY_STRENGTH
285 static enum power_supply_property hidinput_battery_props[] = {
286         POWER_SUPPLY_PROP_PRESENT,
287         POWER_SUPPLY_PROP_ONLINE,
288         POWER_SUPPLY_PROP_CAPACITY,
289         POWER_SUPPLY_PROP_MODEL_NAME,
290         POWER_SUPPLY_PROP_STATUS,
291         POWER_SUPPLY_PROP_SCOPE,
292 };
293
294 #define HID_BATTERY_QUIRK_PERCENT       (1 << 0) /* always reports percent */
295 #define HID_BATTERY_QUIRK_FEATURE       (1 << 1) /* ask for feature report */
296 #define HID_BATTERY_QUIRK_IGNORE        (1 << 2) /* completely ignore the battery */
297
298 static const struct hid_device_id hid_battery_quirks[] = {
299         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
300                 USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
301           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
302         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
303                 USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
304           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
305         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
306                 USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
307           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
308         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
309                                USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
310           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
311         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
312                 USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
313           HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
314         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM,
315                 USB_DEVICE_ID_ELECOM_BM084),
316           HID_BATTERY_QUIRK_IGNORE },
317         { HID_USB_DEVICE(USB_VENDOR_ID_SYMBOL,
318                 USB_DEVICE_ID_SYMBOL_SCANNER_3),
319           HID_BATTERY_QUIRK_IGNORE },
320         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
321                 USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD),
322           HID_BATTERY_QUIRK_IGNORE },
323         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
324                 USB_DEVICE_ID_LOGITECH_DINOVO_EDGE_KBD),
325           HID_BATTERY_QUIRK_IGNORE },
326         { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_ASUS_UX550_TOUCHSCREEN),
327           HID_BATTERY_QUIRK_IGNORE },
328         { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_ASUS_UX550VE_TOUCHSCREEN),
329           HID_BATTERY_QUIRK_IGNORE },
330         { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_ENVY_X360_15T_DR100),
331           HID_BATTERY_QUIRK_IGNORE },
332         { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_SPECTRE_X360_15),
333           HID_BATTERY_QUIRK_IGNORE },
334         { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_SURFACE_GO_TOUCHSCREEN),
335           HID_BATTERY_QUIRK_IGNORE },
336         { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_SURFACE_GO2_TOUCHSCREEN),
337           HID_BATTERY_QUIRK_IGNORE },
338         {}
339 };
340
341 static unsigned find_battery_quirk(struct hid_device *hdev)
342 {
343         unsigned quirks = 0;
344         const struct hid_device_id *match;
345
346         match = hid_match_id(hdev, hid_battery_quirks);
347         if (match != NULL)
348                 quirks = match->driver_data;
349
350         return quirks;
351 }
352
353 static int hidinput_scale_battery_capacity(struct hid_device *dev,
354                                            int value)
355 {
356         if (dev->battery_min < dev->battery_max &&
357             value >= dev->battery_min && value <= dev->battery_max)
358                 value = ((value - dev->battery_min) * 100) /
359                         (dev->battery_max - dev->battery_min);
360
361         return value;
362 }
363
364 static int hidinput_query_battery_capacity(struct hid_device *dev)
365 {
366         u8 *buf;
367         int ret;
368
369         buf = kmalloc(4, GFP_KERNEL);
370         if (!buf)
371                 return -ENOMEM;
372
373         ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 4,
374                                  dev->battery_report_type, HID_REQ_GET_REPORT);
375         if (ret < 2) {
376                 kfree(buf);
377                 return -ENODATA;
378         }
379
380         ret = hidinput_scale_battery_capacity(dev, buf[1]);
381         kfree(buf);
382         return ret;
383 }
384
385 static int hidinput_get_battery_property(struct power_supply *psy,
386                                          enum power_supply_property prop,
387                                          union power_supply_propval *val)
388 {
389         struct hid_device *dev = power_supply_get_drvdata(psy);
390         int value;
391         int ret = 0;
392
393         switch (prop) {
394         case POWER_SUPPLY_PROP_PRESENT:
395         case POWER_SUPPLY_PROP_ONLINE:
396                 val->intval = 1;
397                 break;
398
399         case POWER_SUPPLY_PROP_CAPACITY:
400                 if (dev->battery_status != HID_BATTERY_REPORTED &&
401                     !dev->battery_avoid_query) {
402                         value = hidinput_query_battery_capacity(dev);
403                         if (value < 0)
404                                 return value;
405                 } else  {
406                         value = dev->battery_capacity;
407                 }
408
409                 val->intval = value;
410                 break;
411
412         case POWER_SUPPLY_PROP_MODEL_NAME:
413                 val->strval = dev->name;
414                 break;
415
416         case POWER_SUPPLY_PROP_STATUS:
417                 if (dev->battery_status != HID_BATTERY_REPORTED &&
418                     !dev->battery_avoid_query) {
419                         value = hidinput_query_battery_capacity(dev);
420                         if (value < 0)
421                                 return value;
422
423                         dev->battery_capacity = value;
424                         dev->battery_status = HID_BATTERY_QUERIED;
425                 }
426
427                 if (dev->battery_status == HID_BATTERY_UNKNOWN)
428                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
429                 else
430                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
431                 break;
432
433         case POWER_SUPPLY_PROP_SCOPE:
434                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
435                 break;
436
437         default:
438                 ret = -EINVAL;
439                 break;
440         }
441
442         return ret;
443 }
444
445 static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
446                                   struct hid_field *field, bool is_percentage)
447 {
448         struct power_supply_desc *psy_desc;
449         struct power_supply_config psy_cfg = { .drv_data = dev, };
450         unsigned quirks;
451         s32 min, max;
452         int error;
453
454         if (dev->battery)
455                 return 0;       /* already initialized? */
456
457         quirks = find_battery_quirk(dev);
458
459         hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
460                 dev->bus, dev->vendor, dev->product, dev->version, quirks);
461
462         if (quirks & HID_BATTERY_QUIRK_IGNORE)
463                 return 0;
464
465         psy_desc = kzalloc(sizeof(*psy_desc), GFP_KERNEL);
466         if (!psy_desc)
467                 return -ENOMEM;
468
469         psy_desc->name = kasprintf(GFP_KERNEL, "hid-%s-battery",
470                                    strlen(dev->uniq) ?
471                                         dev->uniq : dev_name(&dev->dev));
472         if (!psy_desc->name) {
473                 error = -ENOMEM;
474                 goto err_free_mem;
475         }
476
477         psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
478         psy_desc->properties = hidinput_battery_props;
479         psy_desc->num_properties = ARRAY_SIZE(hidinput_battery_props);
480         psy_desc->use_for_apm = 0;
481         psy_desc->get_property = hidinput_get_battery_property;
482
483         min = field->logical_minimum;
484         max = field->logical_maximum;
485
486         if (is_percentage || (quirks & HID_BATTERY_QUIRK_PERCENT)) {
487                 min = 0;
488                 max = 100;
489         }
490
491         if (quirks & HID_BATTERY_QUIRK_FEATURE)
492                 report_type = HID_FEATURE_REPORT;
493
494         dev->battery_min = min;
495         dev->battery_max = max;
496         dev->battery_report_type = report_type;
497         dev->battery_report_id = field->report->id;
498
499         /*
500          * Stylus is normally not connected to the device and thus we
501          * can't query the device and get meaningful battery strength.
502          * We have to wait for the device to report it on its own.
503          */
504         dev->battery_avoid_query = report_type == HID_INPUT_REPORT &&
505                                    field->physical == HID_DG_STYLUS;
506
507         dev->battery = power_supply_register(&dev->dev, psy_desc, &psy_cfg);
508         if (IS_ERR(dev->battery)) {
509                 error = PTR_ERR(dev->battery);
510                 hid_warn(dev, "can't register power supply: %d\n", error);
511                 goto err_free_name;
512         }
513
514         power_supply_powers(dev->battery, &dev->dev);
515         return 0;
516
517 err_free_name:
518         kfree(psy_desc->name);
519 err_free_mem:
520         kfree(psy_desc);
521         dev->battery = NULL;
522         return error;
523 }
524
525 static void hidinput_cleanup_battery(struct hid_device *dev)
526 {
527         const struct power_supply_desc *psy_desc;
528
529         if (!dev->battery)
530                 return;
531
532         psy_desc = dev->battery->desc;
533         power_supply_unregister(dev->battery);
534         kfree(psy_desc->name);
535         kfree(psy_desc);
536         dev->battery = NULL;
537 }
538
539 static void hidinput_update_battery(struct hid_device *dev, int value)
540 {
541         int capacity;
542
543         if (!dev->battery)
544                 return;
545
546         if (value == 0 || value < dev->battery_min || value > dev->battery_max)
547                 return;
548
549         capacity = hidinput_scale_battery_capacity(dev, value);
550
551         if (dev->battery_status != HID_BATTERY_REPORTED ||
552             capacity != dev->battery_capacity ||
553             ktime_after(ktime_get_coarse(), dev->battery_ratelimit_time)) {
554                 dev->battery_capacity = capacity;
555                 dev->battery_status = HID_BATTERY_REPORTED;
556                 dev->battery_ratelimit_time =
557                         ktime_add_ms(ktime_get_coarse(), 30 * 1000);
558                 power_supply_changed(dev->battery);
559         }
560 }
561 #else  /* !CONFIG_HID_BATTERY_STRENGTH */
562 static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
563                                   struct hid_field *field, bool is_percentage)
564 {
565         return 0;
566 }
567
568 static void hidinput_cleanup_battery(struct hid_device *dev)
569 {
570 }
571
572 static void hidinput_update_battery(struct hid_device *dev, int value)
573 {
574 }
575 #endif  /* CONFIG_HID_BATTERY_STRENGTH */
576
577 static bool hidinput_field_in_collection(struct hid_device *device, struct hid_field *field,
578                                          unsigned int type, unsigned int usage)
579 {
580         struct hid_collection *collection;
581
582         collection = &device->collection[field->usage->collection_index];
583
584         return collection->type == type && collection->usage == usage;
585 }
586
587 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
588                                      struct hid_usage *usage)
589 {
590         struct input_dev *input = hidinput->input;
591         struct hid_device *device = input_get_drvdata(input);
592         int max = 0, code;
593         unsigned long *bit = NULL;
594
595         field->hidinput = hidinput;
596
597         if (field->flags & HID_MAIN_ITEM_CONSTANT)
598                 goto ignore;
599
600         /* Ignore if report count is out of bounds. */
601         if (field->report_count < 1)
602                 goto ignore;
603
604         /* only LED usages are supported in output fields */
605         if (field->report_type == HID_OUTPUT_REPORT &&
606                         (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
607                 goto ignore;
608         }
609
610         if (device->driver->input_mapping) {
611                 int ret = device->driver->input_mapping(device, hidinput, field,
612                                 usage, &bit, &max);
613                 if (ret > 0)
614                         goto mapped;
615                 if (ret < 0)
616                         goto ignore;
617         }
618
619         switch (usage->hid & HID_USAGE_PAGE) {
620         case HID_UP_UNDEFINED:
621                 goto ignore;
622
623         case HID_UP_KEYBOARD:
624                 set_bit(EV_REP, input->evbit);
625
626                 if ((usage->hid & HID_USAGE) < 256) {
627                         if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
628                         map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
629                 } else
630                         map_key(KEY_UNKNOWN);
631
632                 break;
633
634         case HID_UP_BUTTON:
635                 code = ((usage->hid - 1) & HID_USAGE);
636
637                 switch (field->application) {
638                 case HID_GD_MOUSE:
639                 case HID_GD_POINTER:  code += BTN_MOUSE; break;
640                 case HID_GD_JOYSTICK:
641                                 if (code <= 0xf)
642                                         code += BTN_JOYSTICK;
643                                 else
644                                         code += BTN_TRIGGER_HAPPY - 0x10;
645                                 break;
646                 case HID_GD_GAMEPAD:
647                                 if (code <= 0xf)
648                                         code += BTN_GAMEPAD;
649                                 else
650                                         code += BTN_TRIGGER_HAPPY - 0x10;
651                                 break;
652                 case HID_CP_CONSUMER_CONTROL:
653                                 if (hidinput_field_in_collection(device, field,
654                                                                  HID_COLLECTION_NAMED_ARRAY,
655                                                                  HID_CP_PROGRAMMABLEBUTTONS)) {
656                                         if (code <= 0x1d)
657                                                 code += KEY_MACRO1;
658                                         else
659                                                 code += BTN_TRIGGER_HAPPY - 0x1e;
660                                         break;
661                                 }
662                                 fallthrough;
663                 default:
664                         switch (field->physical) {
665                         case HID_GD_MOUSE:
666                         case HID_GD_POINTER:  code += BTN_MOUSE; break;
667                         case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
668                         case HID_GD_GAMEPAD:  code += BTN_GAMEPAD; break;
669                         default:              code += BTN_MISC;
670                         }
671                 }
672
673                 map_key(code);
674                 break;
675
676         case HID_UP_SIMULATION:
677                 switch (usage->hid & 0xffff) {
678                 case 0xba: map_abs(ABS_RUDDER);   break;
679                 case 0xbb: map_abs(ABS_THROTTLE); break;
680                 case 0xc4: map_abs(ABS_GAS);      break;
681                 case 0xc5: map_abs(ABS_BRAKE);    break;
682                 case 0xc8: map_abs(ABS_WHEEL);    break;
683                 default:   goto ignore;
684                 }
685                 break;
686
687         case HID_UP_GENDESK:
688                 if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
689                         switch (usage->hid & 0xf) {
690                         case 0x1: map_key_clear(KEY_POWER);  break;
691                         case 0x2: map_key_clear(KEY_SLEEP);  break;
692                         case 0x3: map_key_clear(KEY_WAKEUP); break;
693                         case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
694                         case 0x5: map_key_clear(KEY_MENU); break;
695                         case 0x6: map_key_clear(KEY_PROG1); break;
696                         case 0x7: map_key_clear(KEY_HELP); break;
697                         case 0x8: map_key_clear(KEY_EXIT); break;
698                         case 0x9: map_key_clear(KEY_SELECT); break;
699                         case 0xa: map_key_clear(KEY_RIGHT); break;
700                         case 0xb: map_key_clear(KEY_LEFT); break;
701                         case 0xc: map_key_clear(KEY_UP); break;
702                         case 0xd: map_key_clear(KEY_DOWN); break;
703                         case 0xe: map_key_clear(KEY_POWER2); break;
704                         case 0xf: map_key_clear(KEY_RESTART); break;
705                         default: goto unknown;
706                         }
707                         break;
708                 }
709
710                 if ((usage->hid & 0xf0) == 0xb0) {      /* SC - Display */
711                         switch (usage->hid & 0xf) {
712                         case 0x05: map_key_clear(KEY_SWITCHVIDEOMODE); break;
713                         default: goto ignore;
714                         }
715                         break;
716                 }
717
718                 /*
719                  * Some lazy vendors declare 255 usages for System Control,
720                  * leading to the creation of ABS_X|Y axis and too many others.
721                  * It wouldn't be a problem if joydev doesn't consider the
722                  * device as a joystick then.
723                  */
724                 if (field->application == HID_GD_SYSTEM_CONTROL)
725                         goto ignore;
726
727                 if ((usage->hid & 0xf0) == 0x90) {      /* D-pad */
728                         switch (usage->hid) {
729                         case HID_GD_UP:    usage->hat_dir = 1; break;
730                         case HID_GD_DOWN:  usage->hat_dir = 5; break;
731                         case HID_GD_RIGHT: usage->hat_dir = 3; break;
732                         case HID_GD_LEFT:  usage->hat_dir = 7; break;
733                         default: goto unknown;
734                         }
735                         if (field->dpad) {
736                                 map_abs(field->dpad);
737                                 goto ignore;
738                         }
739                         map_abs(ABS_HAT0X);
740                         break;
741                 }
742
743                 switch (usage->hid) {
744                 /* These usage IDs map directly to the usage codes. */
745                 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
746                 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
747                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
748                                 map_rel(usage->hid & 0xf);
749                         else
750                                 map_abs_clear(usage->hid & 0xf);
751                         break;
752
753                 case HID_GD_WHEEL:
754                         if (field->flags & HID_MAIN_ITEM_RELATIVE) {
755                                 set_bit(REL_WHEEL, input->relbit);
756                                 map_rel(REL_WHEEL_HI_RES);
757                         } else {
758                                 map_abs(usage->hid & 0xf);
759                         }
760                         break;
761                 case HID_GD_SLIDER: case HID_GD_DIAL:
762                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
763                                 map_rel(usage->hid & 0xf);
764                         else
765                                 map_abs(usage->hid & 0xf);
766                         break;
767
768                 case HID_GD_HATSWITCH:
769                         usage->hat_min = field->logical_minimum;
770                         usage->hat_max = field->logical_maximum;
771                         map_abs(ABS_HAT0X);
772                         break;
773
774                 case HID_GD_START:      map_key_clear(BTN_START);       break;
775                 case HID_GD_SELECT:     map_key_clear(BTN_SELECT);      break;
776
777                 case HID_GD_RFKILL_BTN:
778                         /* MS wireless radio ctl extension, also check CA */
779                         if (field->application == HID_GD_WIRELESS_RADIO_CTLS) {
780                                 map_key_clear(KEY_RFKILL);
781                                 /* We need to simulate the btn release */
782                                 field->flags |= HID_MAIN_ITEM_RELATIVE;
783                                 break;
784                         }
785                         goto unknown;
786
787                 default: goto unknown;
788                 }
789
790                 break;
791
792         case HID_UP_LED:
793                 switch (usage->hid & 0xffff) {                /* HID-Value:                   */
794                 case 0x01:  map_led (LED_NUML);     break;    /*   "Num Lock"                 */
795                 case 0x02:  map_led (LED_CAPSL);    break;    /*   "Caps Lock"                */
796                 case 0x03:  map_led (LED_SCROLLL);  break;    /*   "Scroll Lock"              */
797                 case 0x04:  map_led (LED_COMPOSE);  break;    /*   "Compose"                  */
798                 case 0x05:  map_led (LED_KANA);     break;    /*   "Kana"                     */
799                 case 0x27:  map_led (LED_SLEEP);    break;    /*   "Stand-By"                 */
800                 case 0x4c:  map_led (LED_SUSPEND);  break;    /*   "System Suspend"           */
801                 case 0x09:  map_led (LED_MUTE);     break;    /*   "Mute"                     */
802                 case 0x4b:  map_led (LED_MISC);     break;    /*   "Generic Indicator"        */
803                 case 0x19:  map_led (LED_MAIL);     break;    /*   "Message Waiting"          */
804                 case 0x4d:  map_led (LED_CHARGING); break;    /*   "External Power Connected" */
805
806                 default: goto ignore;
807                 }
808                 break;
809
810         case HID_UP_DIGITIZER:
811                 if ((field->application & 0xff) == 0x01) /* Digitizer */
812                         __set_bit(INPUT_PROP_POINTER, input->propbit);
813                 else if ((field->application & 0xff) == 0x02) /* Pen */
814                         __set_bit(INPUT_PROP_DIRECT, input->propbit);
815
816                 switch (usage->hid & 0xff) {
817                 case 0x00: /* Undefined */
818                         goto ignore;
819
820                 case 0x30: /* TipPressure */
821                         if (!test_bit(BTN_TOUCH, input->keybit)) {
822                                 device->quirks |= HID_QUIRK_NOTOUCH;
823                                 set_bit(EV_KEY, input->evbit);
824                                 set_bit(BTN_TOUCH, input->keybit);
825                         }
826                         map_abs_clear(ABS_PRESSURE);
827                         break;
828
829                 case 0x32: /* InRange */
830                         switch (field->physical & 0xff) {
831                         case 0x21: map_key(BTN_TOOL_MOUSE); break;
832                         case 0x22: map_key(BTN_TOOL_FINGER); break;
833                         default: map_key(BTN_TOOL_PEN); break;
834                         }
835                         break;
836
837                 case 0x3b: /* Battery Strength */
838                         hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
839                         usage->type = EV_PWR;
840                         return;
841
842                 case 0x3c: /* Invert */
843                         map_key_clear(BTN_TOOL_RUBBER);
844                         break;
845
846                 case 0x3d: /* X Tilt */
847                         map_abs_clear(ABS_TILT_X);
848                         break;
849
850                 case 0x3e: /* Y Tilt */
851                         map_abs_clear(ABS_TILT_Y);
852                         break;
853
854                 case 0x33: /* Touch */
855                 case 0x42: /* TipSwitch */
856                 case 0x43: /* TipSwitch2 */
857                         device->quirks &= ~HID_QUIRK_NOTOUCH;
858                         map_key_clear(BTN_TOUCH);
859                         break;
860
861                 case 0x44: /* BarrelSwitch */
862                         map_key_clear(BTN_STYLUS);
863                         break;
864
865                 case 0x45: /* ERASER */
866                         /*
867                          * This event is reported when eraser tip touches the surface.
868                          * Actual eraser (BTN_TOOL_RUBBER) is set by Invert usage when
869                          * tool gets in proximity.
870                          */
871                         map_key_clear(BTN_TOUCH);
872                         break;
873
874                 case 0x46: /* TabletPick */
875                 case 0x5a: /* SecondaryBarrelSwitch */
876                         map_key_clear(BTN_STYLUS2);
877                         break;
878
879                 case 0x5b: /* TransducerSerialNumber */
880                         usage->type = EV_MSC;
881                         usage->code = MSC_SERIAL;
882                         bit = input->mscbit;
883                         max = MSC_MAX;
884                         break;
885
886                 default:  goto unknown;
887                 }
888                 break;
889
890         case HID_UP_TELEPHONY:
891                 switch (usage->hid & HID_USAGE) {
892                 case 0x2f: map_key_clear(KEY_MICMUTE);          break;
893                 case 0xb0: map_key_clear(KEY_NUMERIC_0);        break;
894                 case 0xb1: map_key_clear(KEY_NUMERIC_1);        break;
895                 case 0xb2: map_key_clear(KEY_NUMERIC_2);        break;
896                 case 0xb3: map_key_clear(KEY_NUMERIC_3);        break;
897                 case 0xb4: map_key_clear(KEY_NUMERIC_4);        break;
898                 case 0xb5: map_key_clear(KEY_NUMERIC_5);        break;
899                 case 0xb6: map_key_clear(KEY_NUMERIC_6);        break;
900                 case 0xb7: map_key_clear(KEY_NUMERIC_7);        break;
901                 case 0xb8: map_key_clear(KEY_NUMERIC_8);        break;
902                 case 0xb9: map_key_clear(KEY_NUMERIC_9);        break;
903                 case 0xba: map_key_clear(KEY_NUMERIC_STAR);     break;
904                 case 0xbb: map_key_clear(KEY_NUMERIC_POUND);    break;
905                 case 0xbc: map_key_clear(KEY_NUMERIC_A);        break;
906                 case 0xbd: map_key_clear(KEY_NUMERIC_B);        break;
907                 case 0xbe: map_key_clear(KEY_NUMERIC_C);        break;
908                 case 0xbf: map_key_clear(KEY_NUMERIC_D);        break;
909                 default: goto ignore;
910                 }
911                 break;
912
913         case HID_UP_CONSUMER:   /* USB HUT v1.12, pages 75-84 */
914                 switch (usage->hid & HID_USAGE) {
915                 case 0x000: goto ignore;
916                 case 0x030: map_key_clear(KEY_POWER);           break;
917                 case 0x031: map_key_clear(KEY_RESTART);         break;
918                 case 0x032: map_key_clear(KEY_SLEEP);           break;
919                 case 0x034: map_key_clear(KEY_SLEEP);           break;
920                 case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE);  break;
921                 case 0x036: map_key_clear(BTN_MISC);            break;
922
923                 case 0x040: map_key_clear(KEY_MENU);            break; /* Menu */
924                 case 0x041: map_key_clear(KEY_SELECT);          break; /* Menu Pick */
925                 case 0x042: map_key_clear(KEY_UP);              break; /* Menu Up */
926                 case 0x043: map_key_clear(KEY_DOWN);            break; /* Menu Down */
927                 case 0x044: map_key_clear(KEY_LEFT);            break; /* Menu Left */
928                 case 0x045: map_key_clear(KEY_RIGHT);           break; /* Menu Right */
929                 case 0x046: map_key_clear(KEY_ESC);             break; /* Menu Escape */
930                 case 0x047: map_key_clear(KEY_KPPLUS);          break; /* Menu Value Increase */
931                 case 0x048: map_key_clear(KEY_KPMINUS);         break; /* Menu Value Decrease */
932
933                 case 0x060: map_key_clear(KEY_INFO);            break; /* Data On Screen */
934                 case 0x061: map_key_clear(KEY_SUBTITLE);        break; /* Closed Caption */
935                 case 0x063: map_key_clear(KEY_VCR);             break; /* VCR/TV */
936                 case 0x065: map_key_clear(KEY_CAMERA);          break; /* Snapshot */
937                 case 0x069: map_key_clear(KEY_RED);             break;
938                 case 0x06a: map_key_clear(KEY_GREEN);           break;
939                 case 0x06b: map_key_clear(KEY_BLUE);            break;
940                 case 0x06c: map_key_clear(KEY_YELLOW);          break;
941                 case 0x06d: map_key_clear(KEY_ASPECT_RATIO);    break;
942
943                 case 0x06f: map_key_clear(KEY_BRIGHTNESSUP);            break;
944                 case 0x070: map_key_clear(KEY_BRIGHTNESSDOWN);          break;
945                 case 0x072: map_key_clear(KEY_BRIGHTNESS_TOGGLE);       break;
946                 case 0x073: map_key_clear(KEY_BRIGHTNESS_MIN);          break;
947                 case 0x074: map_key_clear(KEY_BRIGHTNESS_MAX);          break;
948                 case 0x075: map_key_clear(KEY_BRIGHTNESS_AUTO);         break;
949
950                 case 0x079: map_key_clear(KEY_KBDILLUMUP);      break;
951                 case 0x07a: map_key_clear(KEY_KBDILLUMDOWN);    break;
952                 case 0x07c: map_key_clear(KEY_KBDILLUMTOGGLE);  break;
953
954                 case 0x082: map_key_clear(KEY_VIDEO_NEXT);      break;
955                 case 0x083: map_key_clear(KEY_LAST);            break;
956                 case 0x084: map_key_clear(KEY_ENTER);           break;
957                 case 0x088: map_key_clear(KEY_PC);              break;
958                 case 0x089: map_key_clear(KEY_TV);              break;
959                 case 0x08a: map_key_clear(KEY_WWW);             break;
960                 case 0x08b: map_key_clear(KEY_DVD);             break;
961                 case 0x08c: map_key_clear(KEY_PHONE);           break;
962                 case 0x08d: map_key_clear(KEY_PROGRAM);         break;
963                 case 0x08e: map_key_clear(KEY_VIDEOPHONE);      break;
964                 case 0x08f: map_key_clear(KEY_GAMES);           break;
965                 case 0x090: map_key_clear(KEY_MEMO);            break;
966                 case 0x091: map_key_clear(KEY_CD);              break;
967                 case 0x092: map_key_clear(KEY_VCR);             break;
968                 case 0x093: map_key_clear(KEY_TUNER);           break;
969                 case 0x094: map_key_clear(KEY_EXIT);            break;
970                 case 0x095: map_key_clear(KEY_HELP);            break;
971                 case 0x096: map_key_clear(KEY_TAPE);            break;
972                 case 0x097: map_key_clear(KEY_TV2);             break;
973                 case 0x098: map_key_clear(KEY_SAT);             break;
974                 case 0x09a: map_key_clear(KEY_PVR);             break;
975
976                 case 0x09c: map_key_clear(KEY_CHANNELUP);       break;
977                 case 0x09d: map_key_clear(KEY_CHANNELDOWN);     break;
978                 case 0x0a0: map_key_clear(KEY_VCR2);            break;
979
980                 case 0x0b0: map_key_clear(KEY_PLAY);            break;
981                 case 0x0b1: map_key_clear(KEY_PAUSE);           break;
982                 case 0x0b2: map_key_clear(KEY_RECORD);          break;
983                 case 0x0b3: map_key_clear(KEY_FASTFORWARD);     break;
984                 case 0x0b4: map_key_clear(KEY_REWIND);          break;
985                 case 0x0b5: map_key_clear(KEY_NEXTSONG);        break;
986                 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
987                 case 0x0b7: map_key_clear(KEY_STOPCD);          break;
988                 case 0x0b8: map_key_clear(KEY_EJECTCD);         break;
989                 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
990                 case 0x0b9: map_key_clear(KEY_SHUFFLE);         break;
991                 case 0x0bf: map_key_clear(KEY_SLOW);            break;
992
993                 case 0x0cd: map_key_clear(KEY_PLAYPAUSE);       break;
994                 case 0x0cf: map_key_clear(KEY_VOICECOMMAND);    break;
995
996                 case 0x0d8: map_key_clear(KEY_DICTATE);         break;
997                 case 0x0d9: map_key_clear(KEY_EMOJI_PICKER);    break;
998
999                 case 0x0e0: map_abs_clear(ABS_VOLUME);          break;
1000                 case 0x0e2: map_key_clear(KEY_MUTE);            break;
1001                 case 0x0e5: map_key_clear(KEY_BASSBOOST);       break;
1002                 case 0x0e9: map_key_clear(KEY_VOLUMEUP);        break;
1003                 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);      break;
1004                 case 0x0f5: map_key_clear(KEY_SLOW);            break;
1005
1006                 case 0x181: map_key_clear(KEY_BUTTONCONFIG);    break;
1007                 case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
1008                 case 0x183: map_key_clear(KEY_CONFIG);          break;
1009                 case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
1010                 case 0x185: map_key_clear(KEY_EDITOR);          break;
1011                 case 0x186: map_key_clear(KEY_SPREADSHEET);     break;
1012                 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);  break;
1013                 case 0x188: map_key_clear(KEY_PRESENTATION);    break;
1014                 case 0x189: map_key_clear(KEY_DATABASE);        break;
1015                 case 0x18a: map_key_clear(KEY_MAIL);            break;
1016                 case 0x18b: map_key_clear(KEY_NEWS);            break;
1017                 case 0x18c: map_key_clear(KEY_VOICEMAIL);       break;
1018                 case 0x18d: map_key_clear(KEY_ADDRESSBOOK);     break;
1019                 case 0x18e: map_key_clear(KEY_CALENDAR);        break;
1020                 case 0x18f: map_key_clear(KEY_TASKMANAGER);     break;
1021                 case 0x190: map_key_clear(KEY_JOURNAL);         break;
1022                 case 0x191: map_key_clear(KEY_FINANCE);         break;
1023                 case 0x192: map_key_clear(KEY_CALC);            break;
1024                 case 0x193: map_key_clear(KEY_PLAYER);          break;
1025                 case 0x194: map_key_clear(KEY_FILE);            break;
1026                 case 0x196: map_key_clear(KEY_WWW);             break;
1027                 case 0x199: map_key_clear(KEY_CHAT);            break;
1028                 case 0x19c: map_key_clear(KEY_LOGOFF);          break;
1029                 case 0x19e: map_key_clear(KEY_COFFEE);          break;
1030                 case 0x19f: map_key_clear(KEY_CONTROLPANEL);            break;
1031                 case 0x1a2: map_key_clear(KEY_APPSELECT);               break;
1032                 case 0x1a3: map_key_clear(KEY_NEXT);            break;
1033                 case 0x1a4: map_key_clear(KEY_PREVIOUS);        break;
1034                 case 0x1a6: map_key_clear(KEY_HELP);            break;
1035                 case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
1036                 case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
1037                 case 0x1ae: map_key_clear(KEY_KEYBOARD);        break;
1038                 case 0x1b1: map_key_clear(KEY_SCREENSAVER);             break;
1039                 case 0x1b4: map_key_clear(KEY_FILE);            break;
1040                 case 0x1b6: map_key_clear(KEY_IMAGES);          break;
1041                 case 0x1b7: map_key_clear(KEY_AUDIO);           break;
1042                 case 0x1b8: map_key_clear(KEY_VIDEO);           break;
1043                 case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
1044                 case 0x1bd: map_key_clear(KEY_INFO);            break;
1045                 case 0x1cb: map_key_clear(KEY_ASSISTANT);       break;
1046                 case 0x201: map_key_clear(KEY_NEW);             break;
1047                 case 0x202: map_key_clear(KEY_OPEN);            break;
1048                 case 0x203: map_key_clear(KEY_CLOSE);           break;
1049                 case 0x204: map_key_clear(KEY_EXIT);            break;
1050                 case 0x207: map_key_clear(KEY_SAVE);            break;
1051                 case 0x208: map_key_clear(KEY_PRINT);           break;
1052                 case 0x209: map_key_clear(KEY_PROPS);           break;
1053                 case 0x21a: map_key_clear(KEY_UNDO);            break;
1054                 case 0x21b: map_key_clear(KEY_COPY);            break;
1055                 case 0x21c: map_key_clear(KEY_CUT);             break;
1056                 case 0x21d: map_key_clear(KEY_PASTE);           break;
1057                 case 0x21f: map_key_clear(KEY_FIND);            break;
1058                 case 0x221: map_key_clear(KEY_SEARCH);          break;
1059                 case 0x222: map_key_clear(KEY_GOTO);            break;
1060                 case 0x223: map_key_clear(KEY_HOMEPAGE);        break;
1061                 case 0x224: map_key_clear(KEY_BACK);            break;
1062                 case 0x225: map_key_clear(KEY_FORWARD);         break;
1063                 case 0x226: map_key_clear(KEY_STOP);            break;
1064                 case 0x227: map_key_clear(KEY_REFRESH);         break;
1065                 case 0x22a: map_key_clear(KEY_BOOKMARKS);       break;
1066                 case 0x22d: map_key_clear(KEY_ZOOMIN);          break;
1067                 case 0x22e: map_key_clear(KEY_ZOOMOUT);         break;
1068                 case 0x22f: map_key_clear(KEY_ZOOMRESET);       break;
1069                 case 0x232: map_key_clear(KEY_FULL_SCREEN);     break;
1070                 case 0x233: map_key_clear(KEY_SCROLLUP);        break;
1071                 case 0x234: map_key_clear(KEY_SCROLLDOWN);      break;
1072                 case 0x238: /* AC Pan */
1073                         set_bit(REL_HWHEEL, input->relbit);
1074                         map_rel(REL_HWHEEL_HI_RES);
1075                         break;
1076                 case 0x23d: map_key_clear(KEY_EDIT);            break;
1077                 case 0x25f: map_key_clear(KEY_CANCEL);          break;
1078                 case 0x269: map_key_clear(KEY_INSERT);          break;
1079                 case 0x26a: map_key_clear(KEY_DELETE);          break;
1080                 case 0x279: map_key_clear(KEY_REDO);            break;
1081
1082                 case 0x289: map_key_clear(KEY_REPLY);           break;
1083                 case 0x28b: map_key_clear(KEY_FORWARDMAIL);     break;
1084                 case 0x28c: map_key_clear(KEY_SEND);            break;
1085
1086                 case 0x29d: map_key_clear(KEY_KBD_LAYOUT_NEXT); break;
1087
1088                 case 0x2a2: map_key_clear(KEY_ALL_APPLICATIONS);        break;
1089
1090                 case 0x2c7: map_key_clear(KEY_KBDINPUTASSIST_PREV);             break;
1091                 case 0x2c8: map_key_clear(KEY_KBDINPUTASSIST_NEXT);             break;
1092                 case 0x2c9: map_key_clear(KEY_KBDINPUTASSIST_PREVGROUP);                break;
1093                 case 0x2ca: map_key_clear(KEY_KBDINPUTASSIST_NEXTGROUP);                break;
1094                 case 0x2cb: map_key_clear(KEY_KBDINPUTASSIST_ACCEPT);   break;
1095                 case 0x2cc: map_key_clear(KEY_KBDINPUTASSIST_CANCEL);   break;
1096
1097                 case 0x29f: map_key_clear(KEY_SCALE);           break;
1098
1099                 default: map_key_clear(KEY_UNKNOWN);
1100                 }
1101                 break;
1102
1103         case HID_UP_GENDEVCTRLS:
1104                 switch (usage->hid) {
1105                 case HID_DC_BATTERYSTRENGTH:
1106                         hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
1107                         usage->type = EV_PWR;
1108                         return;
1109                 }
1110                 goto unknown;
1111
1112         case HID_UP_BATTERY:
1113                 switch (usage->hid) {
1114                 case HID_BAT_ABSOLUTESTATEOFCHARGE:
1115                         hidinput_setup_battery(device, HID_INPUT_REPORT, field, true);
1116                         usage->type = EV_PWR;
1117                         return;
1118                 }
1119                 goto unknown;
1120
1121         case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
1122                 set_bit(EV_REP, input->evbit);
1123                 switch (usage->hid & HID_USAGE) {
1124                 case 0x021: map_key_clear(KEY_PRINT);           break;
1125                 case 0x070: map_key_clear(KEY_HP);              break;
1126                 case 0x071: map_key_clear(KEY_CAMERA);          break;
1127                 case 0x072: map_key_clear(KEY_SOUND);           break;
1128                 case 0x073: map_key_clear(KEY_QUESTION);        break;
1129                 case 0x080: map_key_clear(KEY_EMAIL);           break;
1130                 case 0x081: map_key_clear(KEY_CHAT);            break;
1131                 case 0x082: map_key_clear(KEY_SEARCH);          break;
1132                 case 0x083: map_key_clear(KEY_CONNECT);         break;
1133                 case 0x084: map_key_clear(KEY_FINANCE);         break;
1134                 case 0x085: map_key_clear(KEY_SPORT);           break;
1135                 case 0x086: map_key_clear(KEY_SHOP);            break;
1136                 default:    goto ignore;
1137                 }
1138                 break;
1139
1140         case HID_UP_HPVENDOR2:
1141                 set_bit(EV_REP, input->evbit);
1142                 switch (usage->hid & HID_USAGE) {
1143                 case 0x001: map_key_clear(KEY_MICMUTE);         break;
1144                 case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN);  break;
1145                 case 0x004: map_key_clear(KEY_BRIGHTNESSUP);    break;
1146                 default:    goto ignore;
1147                 }
1148                 break;
1149
1150         case HID_UP_MSVENDOR:
1151                 goto ignore;
1152
1153         case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
1154                 set_bit(EV_REP, input->evbit);
1155                 goto ignore;
1156
1157         case HID_UP_LOGIVENDOR:
1158                 /* intentional fallback */
1159         case HID_UP_LOGIVENDOR2:
1160                 /* intentional fallback */
1161         case HID_UP_LOGIVENDOR3:
1162                 goto ignore;
1163
1164         case HID_UP_PID:
1165                 switch (usage->hid & HID_USAGE) {
1166                 case 0xa4: map_key_clear(BTN_DEAD);     break;
1167                 default: goto ignore;
1168                 }
1169                 break;
1170
1171         default:
1172         unknown:
1173                 if (field->report_size == 1) {
1174                         if (field->report->type == HID_OUTPUT_REPORT) {
1175                                 map_led(LED_MISC);
1176                                 break;
1177                         }
1178                         map_key(BTN_MISC);
1179                         break;
1180                 }
1181                 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
1182                         map_rel(REL_MISC);
1183                         break;
1184                 }
1185                 map_abs(ABS_MISC);
1186                 break;
1187         }
1188
1189 mapped:
1190         /* Mapping failed, bail out */
1191         if (!bit)
1192                 return;
1193
1194         if (device->driver->input_mapped &&
1195             device->driver->input_mapped(device, hidinput, field, usage,
1196                                          &bit, &max) < 0) {
1197                 /*
1198                  * The driver indicated that no further generic handling
1199                  * of the usage is desired.
1200                  */
1201                 return;
1202         }
1203
1204         set_bit(usage->type, input->evbit);
1205
1206         /*
1207          * This part is *really* controversial:
1208          * - HID aims at being generic so we should do our best to export
1209          *   all incoming events
1210          * - HID describes what events are, so there is no reason for ABS_X
1211          *   to be mapped to ABS_Y
1212          * - HID is using *_MISC+N as a default value, but nothing prevents
1213          *   *_MISC+N to overwrite a legitimate even, which confuses userspace
1214          *   (for instance ABS_MISC + 7 is ABS_MT_SLOT, which has a different
1215          *   processing)
1216          *
1217          * If devices still want to use this (at their own risk), they will
1218          * have to use the quirk HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE, but
1219          * the default should be a reliable mapping.
1220          */
1221         while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
1222                 if (device->quirks & HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE) {
1223                         usage->code = find_next_zero_bit(bit,
1224                                                          max + 1,
1225                                                          usage->code);
1226                 } else {
1227                         device->status |= HID_STAT_DUP_DETECTED;
1228                         goto ignore;
1229                 }
1230         }
1231
1232         if (usage->code > max)
1233                 goto ignore;
1234
1235         if (usage->type == EV_ABS) {
1236
1237                 int a = field->logical_minimum;
1238                 int b = field->logical_maximum;
1239
1240                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
1241                         a = field->logical_minimum = 0;
1242                         b = field->logical_maximum = 255;
1243                 }
1244
1245                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
1246                         input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
1247                 else    input_set_abs_params(input, usage->code, a, b, 0, 0);
1248
1249                 input_abs_set_res(input, usage->code,
1250                                   hidinput_calc_abs_res(field, usage->code));
1251
1252                 /* use a larger default input buffer for MT devices */
1253                 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
1254                         input_set_events_per_packet(input, 60);
1255         }
1256
1257         if (usage->type == EV_ABS &&
1258             (usage->hat_min < usage->hat_max || usage->hat_dir)) {
1259                 int i;
1260                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
1261                         input_set_abs_params(input, i, -1, 1, 0, 0);
1262                         set_bit(i, input->absbit);
1263                 }
1264                 if (usage->hat_dir && !field->dpad)
1265                         field->dpad = usage->code;
1266         }
1267
1268         /* for those devices which produce Consumer volume usage as relative,
1269          * we emulate pressing volumeup/volumedown appropriate number of times
1270          * in hidinput_hid_event()
1271          */
1272         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
1273                         (usage->code == ABS_VOLUME)) {
1274                 set_bit(KEY_VOLUMEUP, input->keybit);
1275                 set_bit(KEY_VOLUMEDOWN, input->keybit);
1276         }
1277
1278         if (usage->type == EV_KEY) {
1279                 set_bit(EV_MSC, input->evbit);
1280                 set_bit(MSC_SCAN, input->mscbit);
1281         }
1282
1283         return;
1284
1285 ignore:
1286         usage->type = 0;
1287         usage->code = 0;
1288 }
1289
1290 static void hidinput_handle_scroll(struct hid_usage *usage,
1291                                    struct input_dev *input,
1292                                    __s32 value)
1293 {
1294         int code;
1295         int hi_res, lo_res;
1296
1297         if (value == 0)
1298                 return;
1299
1300         if (usage->code == REL_WHEEL_HI_RES)
1301                 code = REL_WHEEL;
1302         else
1303                 code = REL_HWHEEL;
1304
1305         /*
1306          * Windows reports one wheel click as value 120. Where a high-res
1307          * scroll wheel is present, a fraction of 120 is reported instead.
1308          * Our REL_WHEEL_HI_RES axis does the same because all HW must
1309          * adhere to the 120 expectation.
1310          */
1311         hi_res = value * 120/usage->resolution_multiplier;
1312
1313         usage->wheel_accumulated += hi_res;
1314         lo_res = usage->wheel_accumulated/120;
1315         if (lo_res)
1316                 usage->wheel_accumulated -= lo_res * 120;
1317
1318         input_event(input, EV_REL, code, lo_res);
1319         input_event(input, EV_REL, usage->code, hi_res);
1320 }
1321
1322 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
1323 {
1324         struct input_dev *input;
1325         unsigned *quirks = &hid->quirks;
1326
1327         if (!usage->type)
1328                 return;
1329
1330         if (usage->type == EV_PWR) {
1331                 hidinput_update_battery(hid, value);
1332                 return;
1333         }
1334
1335         if (!field->hidinput)
1336                 return;
1337
1338         input = field->hidinput->input;
1339
1340         if (usage->type == EV_ABS &&
1341             (((*quirks & HID_QUIRK_X_INVERT) && usage->code == ABS_X) ||
1342              ((*quirks & HID_QUIRK_Y_INVERT) && usage->code == ABS_Y))) {
1343                 value = field->logical_maximum - value;
1344         }
1345
1346         if (usage->hat_min < usage->hat_max || usage->hat_dir) {
1347                 int hat_dir = usage->hat_dir;
1348                 if (!hat_dir)
1349                         hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
1350                 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
1351                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
1352                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
1353                 return;
1354         }
1355
1356         if (usage->hid == HID_DG_INVERT) {
1357                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
1358                 return;
1359         }
1360
1361         if (usage->hid == HID_DG_INRANGE) {
1362                 if (value) {
1363                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
1364                         return;
1365                 }
1366                 input_event(input, usage->type, usage->code, 0);
1367                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
1368                 return;
1369         }
1370
1371         if (usage->hid == HID_DG_TIPPRESSURE && (*quirks & HID_QUIRK_NOTOUCH)) {
1372                 int a = field->logical_minimum;
1373                 int b = field->logical_maximum;
1374                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
1375         }
1376
1377         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
1378                 dbg_hid("Maximum Effects - %d\n",value);
1379                 return;
1380         }
1381
1382         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
1383                 dbg_hid("PID Pool Report\n");
1384                 return;
1385         }
1386
1387         if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
1388                 return;
1389
1390         if ((usage->type == EV_REL) && (usage->code == REL_WHEEL_HI_RES ||
1391                                         usage->code == REL_HWHEEL_HI_RES)) {
1392                 hidinput_handle_scroll(usage, input, value);
1393                 return;
1394         }
1395
1396         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
1397                         (usage->code == ABS_VOLUME)) {
1398                 int count = abs(value);
1399                 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
1400                 int i;
1401
1402                 for (i = 0; i < count; i++) {
1403                         input_event(input, EV_KEY, direction, 1);
1404                         input_sync(input);
1405                         input_event(input, EV_KEY, direction, 0);
1406                         input_sync(input);
1407                 }
1408                 return;
1409         }
1410
1411         /*
1412          * Ignore out-of-range values as per HID specification,
1413          * section 5.10 and 6.2.25, when NULL state bit is present.
1414          * When it's not, clamp the value to match Microsoft's input
1415          * driver as mentioned in "Required HID usages for digitizers":
1416          * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
1417          *
1418          * The logical_minimum < logical_maximum check is done so that we
1419          * don't unintentionally discard values sent by devices which
1420          * don't specify logical min and max.
1421          */
1422         if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
1423             (field->logical_minimum < field->logical_maximum)) {
1424                 if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
1425                     (value < field->logical_minimum ||
1426                      value > field->logical_maximum)) {
1427                         dbg_hid("Ignoring out-of-range value %x\n", value);
1428                         return;
1429                 }
1430                 value = clamp(value,
1431                               field->logical_minimum,
1432                               field->logical_maximum);
1433         }
1434
1435         /*
1436          * Ignore reports for absolute data if the data didn't change. This is
1437          * not only an optimization but also fixes 'dead' key reports. Some
1438          * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
1439          * 0x31 and 0x32) report multiple keys, even though a localized keyboard
1440          * can only have one of them physically available. The 'dead' keys
1441          * report constant 0. As all map to the same keycode, they'd confuse
1442          * the input layer. If we filter the 'dead' keys on the HID level, we
1443          * skip the keycode translation and only forward real events.
1444          */
1445         if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
1446                               HID_MAIN_ITEM_BUFFERED_BYTE)) &&
1447                               (field->flags & HID_MAIN_ITEM_VARIABLE) &&
1448             usage->usage_index < field->maxusage &&
1449             value == field->value[usage->usage_index])
1450                 return;
1451
1452         /* report the usage code as scancode if the key status has changed */
1453         if (usage->type == EV_KEY &&
1454             (!test_bit(usage->code, input->key)) == value)
1455                 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
1456
1457         input_event(input, usage->type, usage->code, value);
1458
1459         if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
1460             usage->type == EV_KEY && value) {
1461                 input_sync(input);
1462                 input_event(input, usage->type, usage->code, 0);
1463         }
1464 }
1465
1466 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
1467 {
1468         struct hid_input *hidinput;
1469
1470         if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
1471                 return;
1472
1473         list_for_each_entry(hidinput, &hid->inputs, list)
1474                 input_sync(hidinput->input);
1475 }
1476 EXPORT_SYMBOL_GPL(hidinput_report_event);
1477
1478 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1479 {
1480         struct hid_report *report;
1481         int i, j;
1482
1483         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
1484                 for (i = 0; i < report->maxfield; i++) {
1485                         *field = report->field[i];
1486                         for (j = 0; j < (*field)->maxusage; j++)
1487                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1488                                         return j;
1489                 }
1490         }
1491         return -1;
1492 }
1493 EXPORT_SYMBOL_GPL(hidinput_find_field);
1494
1495 struct hid_field *hidinput_get_led_field(struct hid_device *hid)
1496 {
1497         struct hid_report *report;
1498         struct hid_field *field;
1499         int i, j;
1500
1501         list_for_each_entry(report,
1502                             &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1503                             list) {
1504                 for (i = 0; i < report->maxfield; i++) {
1505                         field = report->field[i];
1506                         for (j = 0; j < field->maxusage; j++)
1507                                 if (field->usage[j].type == EV_LED)
1508                                         return field;
1509                 }
1510         }
1511         return NULL;
1512 }
1513 EXPORT_SYMBOL_GPL(hidinput_get_led_field);
1514
1515 unsigned int hidinput_count_leds(struct hid_device *hid)
1516 {
1517         struct hid_report *report;
1518         struct hid_field *field;
1519         int i, j;
1520         unsigned int count = 0;
1521
1522         list_for_each_entry(report,
1523                             &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1524                             list) {
1525                 for (i = 0; i < report->maxfield; i++) {
1526                         field = report->field[i];
1527                         for (j = 0; j < field->maxusage; j++)
1528                                 if (field->usage[j].type == EV_LED &&
1529                                     field->value[j])
1530                                         count += 1;
1531                 }
1532         }
1533         return count;
1534 }
1535 EXPORT_SYMBOL_GPL(hidinput_count_leds);
1536
1537 static void hidinput_led_worker(struct work_struct *work)
1538 {
1539         struct hid_device *hid = container_of(work, struct hid_device,
1540                                               led_work);
1541         struct hid_field *field;
1542         struct hid_report *report;
1543         int ret;
1544         u32 len;
1545         __u8 *buf;
1546
1547         field = hidinput_get_led_field(hid);
1548         if (!field)
1549                 return;
1550
1551         /*
1552          * field->report is accessed unlocked regarding HID core. So there might
1553          * be another incoming SET-LED request from user-space, which changes
1554          * the LED state while we assemble our outgoing buffer. However, this
1555          * doesn't matter as hid_output_report() correctly converts it into a
1556          * boolean value no matter what information is currently set on the LED
1557          * field (even garbage). So the remote device will always get a valid
1558          * request.
1559          * And in case we send a wrong value, a next led worker is spawned
1560          * for every SET-LED request so the following worker will send the
1561          * correct value, guaranteed!
1562          */
1563
1564         report = field->report;
1565
1566         /* use custom SET_REPORT request if possible (asynchronous) */
1567         if (hid->ll_driver->request)
1568                 return hid->ll_driver->request(hid, report, HID_REQ_SET_REPORT);
1569
1570         /* fall back to generic raw-output-report */
1571         len = hid_report_len(report);
1572         buf = hid_alloc_report_buf(report, GFP_KERNEL);
1573         if (!buf)
1574                 return;
1575
1576         hid_output_report(report, buf);
1577         /* synchronous output report */
1578         ret = hid_hw_output_report(hid, buf, len);
1579         if (ret == -ENOSYS)
1580                 hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
1581                                 HID_REQ_SET_REPORT);
1582         kfree(buf);
1583 }
1584
1585 static int hidinput_input_event(struct input_dev *dev, unsigned int type,
1586                                 unsigned int code, int value)
1587 {
1588         struct hid_device *hid = input_get_drvdata(dev);
1589         struct hid_field *field;
1590         int offset;
1591
1592         if (type == EV_FF)
1593                 return input_ff_event(dev, type, code, value);
1594
1595         if (type != EV_LED)
1596                 return -1;
1597
1598         if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
1599                 hid_warn(dev, "event field not found\n");
1600                 return -1;
1601         }
1602
1603         hid_set_field(field, offset, value);
1604
1605         schedule_work(&hid->led_work);
1606         return 0;
1607 }
1608
1609 static int hidinput_open(struct input_dev *dev)
1610 {
1611         struct hid_device *hid = input_get_drvdata(dev);
1612
1613         return hid_hw_open(hid);
1614 }
1615
1616 static void hidinput_close(struct input_dev *dev)
1617 {
1618         struct hid_device *hid = input_get_drvdata(dev);
1619
1620         hid_hw_close(hid);
1621 }
1622
1623 static bool __hidinput_change_resolution_multipliers(struct hid_device *hid,
1624                 struct hid_report *report, bool use_logical_max)
1625 {
1626         struct hid_usage *usage;
1627         bool update_needed = false;
1628         bool get_report_completed = false;
1629         int i, j;
1630
1631         if (report->maxfield == 0)
1632                 return false;
1633
1634         for (i = 0; i < report->maxfield; i++) {
1635                 __s32 value = use_logical_max ?
1636                               report->field[i]->logical_maximum :
1637                               report->field[i]->logical_minimum;
1638
1639                 /* There is no good reason for a Resolution
1640                  * Multiplier to have a count other than 1.
1641                  * Ignore that case.
1642                  */
1643                 if (report->field[i]->report_count != 1)
1644                         continue;
1645
1646                 for (j = 0; j < report->field[i]->maxusage; j++) {
1647                         usage = &report->field[i]->usage[j];
1648
1649                         if (usage->hid != HID_GD_RESOLUTION_MULTIPLIER)
1650                                 continue;
1651
1652                         /*
1653                          * If we have more than one feature within this
1654                          * report we need to fill in the bits from the
1655                          * others before we can overwrite the ones for the
1656                          * Resolution Multiplier.
1657                          *
1658                          * But if we're not allowed to read from the device,
1659                          * we just bail. Such a device should not exist
1660                          * anyway.
1661                          */
1662                         if (!get_report_completed && report->maxfield > 1) {
1663                                 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
1664                                         return update_needed;
1665
1666                                 hid_hw_request(hid, report, HID_REQ_GET_REPORT);
1667                                 hid_hw_wait(hid);
1668                                 get_report_completed = true;
1669                         }
1670
1671                         report->field[i]->value[j] = value;
1672                         update_needed = true;
1673                 }
1674         }
1675
1676         return update_needed;
1677 }
1678
1679 static void hidinput_change_resolution_multipliers(struct hid_device *hid)
1680 {
1681         struct hid_report_enum *rep_enum;
1682         struct hid_report *rep;
1683         int ret;
1684
1685         rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1686         list_for_each_entry(rep, &rep_enum->report_list, list) {
1687                 bool update_needed = __hidinput_change_resolution_multipliers(hid,
1688                                                                      rep, true);
1689
1690                 if (update_needed) {
1691                         ret = __hid_request(hid, rep, HID_REQ_SET_REPORT);
1692                         if (ret) {
1693                                 __hidinput_change_resolution_multipliers(hid,
1694                                                                     rep, false);
1695                                 return;
1696                         }
1697                 }
1698         }
1699
1700         /* refresh our structs */
1701         hid_setup_resolution_multiplier(hid);
1702 }
1703
1704 static void report_features(struct hid_device *hid)
1705 {
1706         struct hid_driver *drv = hid->driver;
1707         struct hid_report_enum *rep_enum;
1708         struct hid_report *rep;
1709         struct hid_usage *usage;
1710         int i, j;
1711
1712         rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1713         list_for_each_entry(rep, &rep_enum->report_list, list)
1714                 for (i = 0; i < rep->maxfield; i++) {
1715                         /* Ignore if report count is out of bounds. */
1716                         if (rep->field[i]->report_count < 1)
1717                                 continue;
1718
1719                         for (j = 0; j < rep->field[i]->maxusage; j++) {
1720                                 usage = &rep->field[i]->usage[j];
1721
1722                                 /* Verify if Battery Strength feature is available */
1723                                 if (usage->hid == HID_DC_BATTERYSTRENGTH)
1724                                         hidinput_setup_battery(hid, HID_FEATURE_REPORT,
1725                                                                rep->field[i], false);
1726
1727                                 if (drv->feature_mapping)
1728                                         drv->feature_mapping(hid, rep->field[i], usage);
1729                         }
1730                 }
1731 }
1732
1733 static struct hid_input *hidinput_allocate(struct hid_device *hid,
1734                                            unsigned int application)
1735 {
1736         struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1737         struct input_dev *input_dev = input_allocate_device();
1738         const char *suffix = NULL;
1739         size_t suffix_len, name_len;
1740
1741         if (!hidinput || !input_dev)
1742                 goto fail;
1743
1744         if ((hid->quirks & HID_QUIRK_INPUT_PER_APP) &&
1745             hid->maxapplication > 1) {
1746                 switch (application) {
1747                 case HID_GD_KEYBOARD:
1748                         suffix = "Keyboard";
1749                         break;
1750                 case HID_GD_KEYPAD:
1751                         suffix = "Keypad";
1752                         break;
1753                 case HID_GD_MOUSE:
1754                         suffix = "Mouse";
1755                         break;
1756                 case HID_DG_STYLUS:
1757                         suffix = "Pen";
1758                         break;
1759                 case HID_DG_TOUCHSCREEN:
1760                         suffix = "Touchscreen";
1761                         break;
1762                 case HID_DG_TOUCHPAD:
1763                         suffix = "Touchpad";
1764                         break;
1765                 case HID_GD_SYSTEM_CONTROL:
1766                         suffix = "System Control";
1767                         break;
1768                 case HID_CP_CONSUMER_CONTROL:
1769                         suffix = "Consumer Control";
1770                         break;
1771                 case HID_GD_WIRELESS_RADIO_CTLS:
1772                         suffix = "Wireless Radio Control";
1773                         break;
1774                 case HID_GD_SYSTEM_MULTIAXIS:
1775                         suffix = "System Multi Axis";
1776                         break;
1777                 default:
1778                         break;
1779                 }
1780         }
1781
1782         if (suffix) {
1783                 name_len = strlen(hid->name);
1784                 suffix_len = strlen(suffix);
1785                 if ((name_len < suffix_len) ||
1786                     strcmp(hid->name + name_len - suffix_len, suffix)) {
1787                         hidinput->name = kasprintf(GFP_KERNEL, "%s %s",
1788                                                    hid->name, suffix);
1789                         if (!hidinput->name)
1790                                 goto fail;
1791                 }
1792         }
1793
1794         input_set_drvdata(input_dev, hid);
1795         input_dev->event = hidinput_input_event;
1796         input_dev->open = hidinput_open;
1797         input_dev->close = hidinput_close;
1798         input_dev->setkeycode = hidinput_setkeycode;
1799         input_dev->getkeycode = hidinput_getkeycode;
1800
1801         input_dev->name = hidinput->name ? hidinput->name : hid->name;
1802         input_dev->phys = hid->phys;
1803         input_dev->uniq = hid->uniq;
1804         input_dev->id.bustype = hid->bus;
1805         input_dev->id.vendor  = hid->vendor;
1806         input_dev->id.product = hid->product;
1807         input_dev->id.version = hid->version;
1808         input_dev->dev.parent = &hid->dev;
1809
1810         hidinput->input = input_dev;
1811         hidinput->application = application;
1812         list_add_tail(&hidinput->list, &hid->inputs);
1813
1814         INIT_LIST_HEAD(&hidinput->reports);
1815
1816         return hidinput;
1817
1818 fail:
1819         kfree(hidinput);
1820         input_free_device(input_dev);
1821         hid_err(hid, "Out of memory during hid input probe\n");
1822         return NULL;
1823 }
1824
1825 static bool hidinput_has_been_populated(struct hid_input *hidinput)
1826 {
1827         int i;
1828         unsigned long r = 0;
1829
1830         for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
1831                 r |= hidinput->input->evbit[i];
1832
1833         for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
1834                 r |= hidinput->input->keybit[i];
1835
1836         for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
1837                 r |= hidinput->input->relbit[i];
1838
1839         for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
1840                 r |= hidinput->input->absbit[i];
1841
1842         for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
1843                 r |= hidinput->input->mscbit[i];
1844
1845         for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
1846                 r |= hidinput->input->ledbit[i];
1847
1848         for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
1849                 r |= hidinput->input->sndbit[i];
1850
1851         for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
1852                 r |= hidinput->input->ffbit[i];
1853
1854         for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
1855                 r |= hidinput->input->swbit[i];
1856
1857         return !!r;
1858 }
1859
1860 static void hidinput_cleanup_hidinput(struct hid_device *hid,
1861                 struct hid_input *hidinput)
1862 {
1863         struct hid_report *report;
1864         int i, k;
1865
1866         list_del(&hidinput->list);
1867         input_free_device(hidinput->input);
1868         kfree(hidinput->name);
1869
1870         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1871                 if (k == HID_OUTPUT_REPORT &&
1872                         hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1873                         continue;
1874
1875                 list_for_each_entry(report, &hid->report_enum[k].report_list,
1876                                     list) {
1877
1878                         for (i = 0; i < report->maxfield; i++)
1879                                 if (report->field[i]->hidinput == hidinput)
1880                                         report->field[i]->hidinput = NULL;
1881                 }
1882         }
1883
1884         kfree(hidinput);
1885 }
1886
1887 static struct hid_input *hidinput_match(struct hid_report *report)
1888 {
1889         struct hid_device *hid = report->device;
1890         struct hid_input *hidinput;
1891
1892         list_for_each_entry(hidinput, &hid->inputs, list) {
1893                 if (hidinput->report &&
1894                     hidinput->report->id == report->id)
1895                         return hidinput;
1896         }
1897
1898         return NULL;
1899 }
1900
1901 static struct hid_input *hidinput_match_application(struct hid_report *report)
1902 {
1903         struct hid_device *hid = report->device;
1904         struct hid_input *hidinput;
1905
1906         list_for_each_entry(hidinput, &hid->inputs, list) {
1907                 if (hidinput->application == report->application)
1908                         return hidinput;
1909
1910                 /*
1911                  * Keep SystemControl and ConsumerControl applications together
1912                  * with the main keyboard, if present.
1913                  */
1914                 if ((report->application == HID_GD_SYSTEM_CONTROL ||
1915                      report->application == HID_CP_CONSUMER_CONTROL) &&
1916                     hidinput->application == HID_GD_KEYBOARD) {
1917                         return hidinput;
1918                 }
1919         }
1920
1921         return NULL;
1922 }
1923
1924 static inline void hidinput_configure_usages(struct hid_input *hidinput,
1925                                              struct hid_report *report)
1926 {
1927         int i, j;
1928
1929         for (i = 0; i < report->maxfield; i++)
1930                 for (j = 0; j < report->field[i]->maxusage; j++)
1931                         hidinput_configure_usage(hidinput, report->field[i],
1932                                                  report->field[i]->usage + j);
1933 }
1934
1935 /*
1936  * Register the input device; print a message.
1937  * Configure the input layer interface
1938  * Read all reports and initialize the absolute field values.
1939  */
1940
1941 int hidinput_connect(struct hid_device *hid, unsigned int force)
1942 {
1943         struct hid_driver *drv = hid->driver;
1944         struct hid_report *report;
1945         struct hid_input *next, *hidinput = NULL;
1946         unsigned int application;
1947         int i, k;
1948
1949         INIT_LIST_HEAD(&hid->inputs);
1950         INIT_WORK(&hid->led_work, hidinput_led_worker);
1951
1952         hid->status &= ~HID_STAT_DUP_DETECTED;
1953
1954         if (!force) {
1955                 for (i = 0; i < hid->maxcollection; i++) {
1956                         struct hid_collection *col = &hid->collection[i];
1957                         if (col->type == HID_COLLECTION_APPLICATION ||
1958                                         col->type == HID_COLLECTION_PHYSICAL)
1959                                 if (IS_INPUT_APPLICATION(col->usage))
1960                                         break;
1961                 }
1962
1963                 if (i == hid->maxcollection)
1964                         return -1;
1965         }
1966
1967         report_features(hid);
1968
1969         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1970                 if (k == HID_OUTPUT_REPORT &&
1971                         hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1972                         continue;
1973
1974                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1975
1976                         if (!report->maxfield)
1977                                 continue;
1978
1979                         application = report->application;
1980
1981                         /*
1982                          * Find the previous hidinput report attached
1983                          * to this report id.
1984                          */
1985                         if (hid->quirks & HID_QUIRK_MULTI_INPUT)
1986                                 hidinput = hidinput_match(report);
1987                         else if (hid->maxapplication > 1 &&
1988                                  (hid->quirks & HID_QUIRK_INPUT_PER_APP))
1989                                 hidinput = hidinput_match_application(report);
1990
1991                         if (!hidinput) {
1992                                 hidinput = hidinput_allocate(hid, application);
1993                                 if (!hidinput)
1994                                         goto out_unwind;
1995                         }
1996
1997                         hidinput_configure_usages(hidinput, report);
1998
1999                         if (hid->quirks & HID_QUIRK_MULTI_INPUT)
2000                                 hidinput->report = report;
2001
2002                         list_add_tail(&report->hidinput_list,
2003                                       &hidinput->reports);
2004                 }
2005         }
2006
2007         hidinput_change_resolution_multipliers(hid);
2008
2009         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
2010                 if (drv->input_configured &&
2011                     drv->input_configured(hid, hidinput))
2012                         goto out_unwind;
2013
2014                 if (!hidinput_has_been_populated(hidinput)) {
2015                         /* no need to register an input device not populated */
2016                         hidinput_cleanup_hidinput(hid, hidinput);
2017                         continue;
2018                 }
2019
2020                 if (input_register_device(hidinput->input))
2021                         goto out_unwind;
2022                 hidinput->registered = true;
2023         }
2024
2025         if (list_empty(&hid->inputs)) {
2026                 hid_err(hid, "No inputs registered, leaving\n");
2027                 goto out_unwind;
2028         }
2029
2030         if (hid->status & HID_STAT_DUP_DETECTED)
2031                 hid_dbg(hid,
2032                         "Some usages could not be mapped, please use HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE if this is legitimate.\n");
2033
2034         return 0;
2035
2036 out_unwind:
2037         /* unwind the ones we already registered */
2038         hidinput_disconnect(hid);
2039
2040         return -1;
2041 }
2042 EXPORT_SYMBOL_GPL(hidinput_connect);
2043
2044 void hidinput_disconnect(struct hid_device *hid)
2045 {
2046         struct hid_input *hidinput, *next;
2047
2048         hidinput_cleanup_battery(hid);
2049
2050         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
2051                 list_del(&hidinput->list);
2052                 if (hidinput->registered)
2053                         input_unregister_device(hidinput->input);
2054                 else
2055                         input_free_device(hidinput->input);
2056                 kfree(hidinput->name);
2057                 kfree(hidinput);
2058         }
2059
2060         /* led_work is spawned by input_dev callbacks, but doesn't access the
2061          * parent input_dev at all. Once all input devices are removed, we
2062          * know that led_work will never get restarted, so we can cancel it
2063          * synchronously and are safe. */
2064         cancel_work_sync(&hid->led_work);
2065 }
2066 EXPORT_SYMBOL_GPL(hidinput_disconnect);