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