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