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