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