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