xref: /openbmc/linux/drivers/hid/hid-input.c (revision e2aa5e65)
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 0x0d9: map_key_clear(KEY_EMOJI_PICKER);	break;
996 
997 		case 0x0e0: map_abs_clear(ABS_VOLUME);		break;
998 		case 0x0e2: map_key_clear(KEY_MUTE);		break;
999 		case 0x0e5: map_key_clear(KEY_BASSBOOST);	break;
1000 		case 0x0e9: map_key_clear(KEY_VOLUMEUP);	break;
1001 		case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);	break;
1002 		case 0x0f5: map_key_clear(KEY_SLOW);		break;
1003 
1004 		case 0x181: map_key_clear(KEY_BUTTONCONFIG);	break;
1005 		case 0x182: map_key_clear(KEY_BOOKMARKS);	break;
1006 		case 0x183: map_key_clear(KEY_CONFIG);		break;
1007 		case 0x184: map_key_clear(KEY_WORDPROCESSOR);	break;
1008 		case 0x185: map_key_clear(KEY_EDITOR);		break;
1009 		case 0x186: map_key_clear(KEY_SPREADSHEET);	break;
1010 		case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);	break;
1011 		case 0x188: map_key_clear(KEY_PRESENTATION);	break;
1012 		case 0x189: map_key_clear(KEY_DATABASE);	break;
1013 		case 0x18a: map_key_clear(KEY_MAIL);		break;
1014 		case 0x18b: map_key_clear(KEY_NEWS);		break;
1015 		case 0x18c: map_key_clear(KEY_VOICEMAIL);	break;
1016 		case 0x18d: map_key_clear(KEY_ADDRESSBOOK);	break;
1017 		case 0x18e: map_key_clear(KEY_CALENDAR);	break;
1018 		case 0x18f: map_key_clear(KEY_TASKMANAGER);	break;
1019 		case 0x190: map_key_clear(KEY_JOURNAL);		break;
1020 		case 0x191: map_key_clear(KEY_FINANCE);		break;
1021 		case 0x192: map_key_clear(KEY_CALC);		break;
1022 		case 0x193: map_key_clear(KEY_PLAYER);		break;
1023 		case 0x194: map_key_clear(KEY_FILE);		break;
1024 		case 0x196: map_key_clear(KEY_WWW);		break;
1025 		case 0x199: map_key_clear(KEY_CHAT);		break;
1026 		case 0x19c: map_key_clear(KEY_LOGOFF);		break;
1027 		case 0x19e: map_key_clear(KEY_COFFEE);		break;
1028 		case 0x19f: map_key_clear(KEY_CONTROLPANEL);		break;
1029 		case 0x1a2: map_key_clear(KEY_APPSELECT);		break;
1030 		case 0x1a3: map_key_clear(KEY_NEXT);		break;
1031 		case 0x1a4: map_key_clear(KEY_PREVIOUS);	break;
1032 		case 0x1a6: map_key_clear(KEY_HELP);		break;
1033 		case 0x1a7: map_key_clear(KEY_DOCUMENTS);	break;
1034 		case 0x1ab: map_key_clear(KEY_SPELLCHECK);	break;
1035 		case 0x1ae: map_key_clear(KEY_KEYBOARD);	break;
1036 		case 0x1b1: map_key_clear(KEY_SCREENSAVER);		break;
1037 		case 0x1b4: map_key_clear(KEY_FILE);		break;
1038 		case 0x1b6: map_key_clear(KEY_IMAGES);		break;
1039 		case 0x1b7: map_key_clear(KEY_AUDIO);		break;
1040 		case 0x1b8: map_key_clear(KEY_VIDEO);		break;
1041 		case 0x1bc: map_key_clear(KEY_MESSENGER);	break;
1042 		case 0x1bd: map_key_clear(KEY_INFO);		break;
1043 		case 0x1cb: map_key_clear(KEY_ASSISTANT);	break;
1044 		case 0x201: map_key_clear(KEY_NEW);		break;
1045 		case 0x202: map_key_clear(KEY_OPEN);		break;
1046 		case 0x203: map_key_clear(KEY_CLOSE);		break;
1047 		case 0x204: map_key_clear(KEY_EXIT);		break;
1048 		case 0x207: map_key_clear(KEY_SAVE);		break;
1049 		case 0x208: map_key_clear(KEY_PRINT);		break;
1050 		case 0x209: map_key_clear(KEY_PROPS);		break;
1051 		case 0x21a: map_key_clear(KEY_UNDO);		break;
1052 		case 0x21b: map_key_clear(KEY_COPY);		break;
1053 		case 0x21c: map_key_clear(KEY_CUT);		break;
1054 		case 0x21d: map_key_clear(KEY_PASTE);		break;
1055 		case 0x21f: map_key_clear(KEY_FIND);		break;
1056 		case 0x221: map_key_clear(KEY_SEARCH);		break;
1057 		case 0x222: map_key_clear(KEY_GOTO);		break;
1058 		case 0x223: map_key_clear(KEY_HOMEPAGE);	break;
1059 		case 0x224: map_key_clear(KEY_BACK);		break;
1060 		case 0x225: map_key_clear(KEY_FORWARD);		break;
1061 		case 0x226: map_key_clear(KEY_STOP);		break;
1062 		case 0x227: map_key_clear(KEY_REFRESH);		break;
1063 		case 0x22a: map_key_clear(KEY_BOOKMARKS);	break;
1064 		case 0x22d: map_key_clear(KEY_ZOOMIN);		break;
1065 		case 0x22e: map_key_clear(KEY_ZOOMOUT);		break;
1066 		case 0x22f: map_key_clear(KEY_ZOOMRESET);	break;
1067 		case 0x232: map_key_clear(KEY_FULL_SCREEN);	break;
1068 		case 0x233: map_key_clear(KEY_SCROLLUP);	break;
1069 		case 0x234: map_key_clear(KEY_SCROLLDOWN);	break;
1070 		case 0x238: /* AC Pan */
1071 			set_bit(REL_HWHEEL, input->relbit);
1072 			map_rel(REL_HWHEEL_HI_RES);
1073 			break;
1074 		case 0x23d: map_key_clear(KEY_EDIT);		break;
1075 		case 0x25f: map_key_clear(KEY_CANCEL);		break;
1076 		case 0x269: map_key_clear(KEY_INSERT);		break;
1077 		case 0x26a: map_key_clear(KEY_DELETE);		break;
1078 		case 0x279: map_key_clear(KEY_REDO);		break;
1079 
1080 		case 0x289: map_key_clear(KEY_REPLY);		break;
1081 		case 0x28b: map_key_clear(KEY_FORWARDMAIL);	break;
1082 		case 0x28c: map_key_clear(KEY_SEND);		break;
1083 
1084 		case 0x29d: map_key_clear(KEY_KBD_LAYOUT_NEXT);	break;
1085 
1086 		case 0x2c7: map_key_clear(KEY_KBDINPUTASSIST_PREV);		break;
1087 		case 0x2c8: map_key_clear(KEY_KBDINPUTASSIST_NEXT);		break;
1088 		case 0x2c9: map_key_clear(KEY_KBDINPUTASSIST_PREVGROUP);		break;
1089 		case 0x2ca: map_key_clear(KEY_KBDINPUTASSIST_NEXTGROUP);		break;
1090 		case 0x2cb: map_key_clear(KEY_KBDINPUTASSIST_ACCEPT);	break;
1091 		case 0x2cc: map_key_clear(KEY_KBDINPUTASSIST_CANCEL);	break;
1092 
1093 		case 0x29f: map_key_clear(KEY_SCALE);		break;
1094 
1095 		default: map_key_clear(KEY_UNKNOWN);
1096 		}
1097 		break;
1098 
1099 	case HID_UP_GENDEVCTRLS:
1100 		switch (usage->hid) {
1101 		case HID_DC_BATTERYSTRENGTH:
1102 			hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
1103 			usage->type = EV_PWR;
1104 			return;
1105 		}
1106 		goto unknown;
1107 
1108 	case HID_UP_BATTERY:
1109 		switch (usage->hid) {
1110 		case HID_BAT_ABSOLUTESTATEOFCHARGE:
1111 			hidinput_setup_battery(device, HID_INPUT_REPORT, field, true);
1112 			usage->type = EV_PWR;
1113 			return;
1114 		}
1115 		goto unknown;
1116 
1117 	case HID_UP_HPVENDOR:	/* Reported on a Dutch layout HP5308 */
1118 		set_bit(EV_REP, input->evbit);
1119 		switch (usage->hid & HID_USAGE) {
1120 		case 0x021: map_key_clear(KEY_PRINT);           break;
1121 		case 0x070: map_key_clear(KEY_HP);		break;
1122 		case 0x071: map_key_clear(KEY_CAMERA);		break;
1123 		case 0x072: map_key_clear(KEY_SOUND);		break;
1124 		case 0x073: map_key_clear(KEY_QUESTION);	break;
1125 		case 0x080: map_key_clear(KEY_EMAIL);		break;
1126 		case 0x081: map_key_clear(KEY_CHAT);		break;
1127 		case 0x082: map_key_clear(KEY_SEARCH);		break;
1128 		case 0x083: map_key_clear(KEY_CONNECT);	        break;
1129 		case 0x084: map_key_clear(KEY_FINANCE);		break;
1130 		case 0x085: map_key_clear(KEY_SPORT);		break;
1131 		case 0x086: map_key_clear(KEY_SHOP);	        break;
1132 		default:    goto ignore;
1133 		}
1134 		break;
1135 
1136 	case HID_UP_HPVENDOR2:
1137 		set_bit(EV_REP, input->evbit);
1138 		switch (usage->hid & HID_USAGE) {
1139 		case 0x001: map_key_clear(KEY_MICMUTE);		break;
1140 		case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN);	break;
1141 		case 0x004: map_key_clear(KEY_BRIGHTNESSUP);	break;
1142 		default:    goto ignore;
1143 		}
1144 		break;
1145 
1146 	case HID_UP_MSVENDOR:
1147 		goto ignore;
1148 
1149 	case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
1150 		set_bit(EV_REP, input->evbit);
1151 		goto ignore;
1152 
1153 	case HID_UP_LOGIVENDOR:
1154 		/* intentional fallback */
1155 	case HID_UP_LOGIVENDOR2:
1156 		/* intentional fallback */
1157 	case HID_UP_LOGIVENDOR3:
1158 		goto ignore;
1159 
1160 	case HID_UP_PID:
1161 		switch (usage->hid & HID_USAGE) {
1162 		case 0xa4: map_key_clear(BTN_DEAD);	break;
1163 		default: goto ignore;
1164 		}
1165 		break;
1166 
1167 	default:
1168 	unknown:
1169 		if (field->report_size == 1) {
1170 			if (field->report->type == HID_OUTPUT_REPORT) {
1171 				map_led(LED_MISC);
1172 				break;
1173 			}
1174 			map_key(BTN_MISC);
1175 			break;
1176 		}
1177 		if (field->flags & HID_MAIN_ITEM_RELATIVE) {
1178 			map_rel(REL_MISC);
1179 			break;
1180 		}
1181 		map_abs(ABS_MISC);
1182 		break;
1183 	}
1184 
1185 mapped:
1186 	/* Mapping failed, bail out */
1187 	if (!bit)
1188 		return;
1189 
1190 	if (device->driver->input_mapped &&
1191 	    device->driver->input_mapped(device, hidinput, field, usage,
1192 					 &bit, &max) < 0) {
1193 		/*
1194 		 * The driver indicated that no further generic handling
1195 		 * of the usage is desired.
1196 		 */
1197 		return;
1198 	}
1199 
1200 	set_bit(usage->type, input->evbit);
1201 
1202 	/*
1203 	 * This part is *really* controversial:
1204 	 * - HID aims at being generic so we should do our best to export
1205 	 *   all incoming events
1206 	 * - HID describes what events are, so there is no reason for ABS_X
1207 	 *   to be mapped to ABS_Y
1208 	 * - HID is using *_MISC+N as a default value, but nothing prevents
1209 	 *   *_MISC+N to overwrite a legitimate even, which confuses userspace
1210 	 *   (for instance ABS_MISC + 7 is ABS_MT_SLOT, which has a different
1211 	 *   processing)
1212 	 *
1213 	 * If devices still want to use this (at their own risk), they will
1214 	 * have to use the quirk HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE, but
1215 	 * the default should be a reliable mapping.
1216 	 */
1217 	while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
1218 		if (device->quirks & HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE) {
1219 			usage->code = find_next_zero_bit(bit,
1220 							 max + 1,
1221 							 usage->code);
1222 		} else {
1223 			device->status |= HID_STAT_DUP_DETECTED;
1224 			goto ignore;
1225 		}
1226 	}
1227 
1228 	if (usage->code > max)
1229 		goto ignore;
1230 
1231 	if (usage->type == EV_ABS) {
1232 
1233 		int a = field->logical_minimum;
1234 		int b = field->logical_maximum;
1235 
1236 		if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
1237 			a = field->logical_minimum = 0;
1238 			b = field->logical_maximum = 255;
1239 		}
1240 
1241 		if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
1242 			input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
1243 		else	input_set_abs_params(input, usage->code, a, b, 0, 0);
1244 
1245 		input_abs_set_res(input, usage->code,
1246 				  hidinput_calc_abs_res(field, usage->code));
1247 
1248 		/* use a larger default input buffer for MT devices */
1249 		if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
1250 			input_set_events_per_packet(input, 60);
1251 	}
1252 
1253 	if (usage->type == EV_ABS &&
1254 	    (usage->hat_min < usage->hat_max || usage->hat_dir)) {
1255 		int i;
1256 		for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
1257 			input_set_abs_params(input, i, -1, 1, 0, 0);
1258 			set_bit(i, input->absbit);
1259 		}
1260 		if (usage->hat_dir && !field->dpad)
1261 			field->dpad = usage->code;
1262 	}
1263 
1264 	/* for those devices which produce Consumer volume usage as relative,
1265 	 * we emulate pressing volumeup/volumedown appropriate number of times
1266 	 * in hidinput_hid_event()
1267 	 */
1268 	if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
1269 			(usage->code == ABS_VOLUME)) {
1270 		set_bit(KEY_VOLUMEUP, input->keybit);
1271 		set_bit(KEY_VOLUMEDOWN, input->keybit);
1272 	}
1273 
1274 	if (usage->type == EV_KEY) {
1275 		set_bit(EV_MSC, input->evbit);
1276 		set_bit(MSC_SCAN, input->mscbit);
1277 	}
1278 
1279 	return;
1280 
1281 ignore:
1282 	usage->type = 0;
1283 	usage->code = 0;
1284 }
1285 
1286 static void hidinput_handle_scroll(struct hid_usage *usage,
1287 				   struct input_dev *input,
1288 				   __s32 value)
1289 {
1290 	int code;
1291 	int hi_res, lo_res;
1292 
1293 	if (value == 0)
1294 		return;
1295 
1296 	if (usage->code == REL_WHEEL_HI_RES)
1297 		code = REL_WHEEL;
1298 	else
1299 		code = REL_HWHEEL;
1300 
1301 	/*
1302 	 * Windows reports one wheel click as value 120. Where a high-res
1303 	 * scroll wheel is present, a fraction of 120 is reported instead.
1304 	 * Our REL_WHEEL_HI_RES axis does the same because all HW must
1305 	 * adhere to the 120 expectation.
1306 	 */
1307 	hi_res = value * 120/usage->resolution_multiplier;
1308 
1309 	usage->wheel_accumulated += hi_res;
1310 	lo_res = usage->wheel_accumulated/120;
1311 	if (lo_res)
1312 		usage->wheel_accumulated -= lo_res * 120;
1313 
1314 	input_event(input, EV_REL, code, lo_res);
1315 	input_event(input, EV_REL, usage->code, hi_res);
1316 }
1317 
1318 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
1319 {
1320 	struct input_dev *input;
1321 	unsigned *quirks = &hid->quirks;
1322 
1323 	if (!usage->type)
1324 		return;
1325 
1326 	if (usage->type == EV_PWR) {
1327 		hidinput_update_battery(hid, value);
1328 		return;
1329 	}
1330 
1331 	if (!field->hidinput)
1332 		return;
1333 
1334 	input = field->hidinput->input;
1335 
1336 	if (usage->type == EV_ABS &&
1337 	    (((*quirks & HID_QUIRK_X_INVERT) && usage->code == ABS_X) ||
1338 	     ((*quirks & HID_QUIRK_Y_INVERT) && usage->code == ABS_Y))) {
1339 		value = field->logical_maximum - value;
1340 	}
1341 
1342 	if (usage->hat_min < usage->hat_max || usage->hat_dir) {
1343 		int hat_dir = usage->hat_dir;
1344 		if (!hat_dir)
1345 			hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
1346 		if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
1347 		input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
1348 		input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
1349 		return;
1350 	}
1351 
1352 	if (usage->hid == HID_DG_INVERT) {
1353 		*quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
1354 		return;
1355 	}
1356 
1357 	if (usage->hid == HID_DG_INRANGE) {
1358 		if (value) {
1359 			input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
1360 			return;
1361 		}
1362 		input_event(input, usage->type, usage->code, 0);
1363 		input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
1364 		return;
1365 	}
1366 
1367 	if (usage->hid == HID_DG_TIPPRESSURE && (*quirks & HID_QUIRK_NOTOUCH)) {
1368 		int a = field->logical_minimum;
1369 		int b = field->logical_maximum;
1370 		input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
1371 	}
1372 
1373 	if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
1374 		dbg_hid("Maximum Effects - %d\n",value);
1375 		return;
1376 	}
1377 
1378 	if (usage->hid == (HID_UP_PID | 0x7fUL)) {
1379 		dbg_hid("PID Pool Report\n");
1380 		return;
1381 	}
1382 
1383 	if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
1384 		return;
1385 
1386 	if ((usage->type == EV_REL) && (usage->code == REL_WHEEL_HI_RES ||
1387 					usage->code == REL_HWHEEL_HI_RES)) {
1388 		hidinput_handle_scroll(usage, input, value);
1389 		return;
1390 	}
1391 
1392 	if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
1393 			(usage->code == ABS_VOLUME)) {
1394 		int count = abs(value);
1395 		int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
1396 		int i;
1397 
1398 		for (i = 0; i < count; i++) {
1399 			input_event(input, EV_KEY, direction, 1);
1400 			input_sync(input);
1401 			input_event(input, EV_KEY, direction, 0);
1402 			input_sync(input);
1403 		}
1404 		return;
1405 	}
1406 
1407 	/*
1408 	 * Ignore out-of-range values as per HID specification,
1409 	 * section 5.10 and 6.2.25, when NULL state bit is present.
1410 	 * When it's not, clamp the value to match Microsoft's input
1411 	 * driver as mentioned in "Required HID usages for digitizers":
1412 	 * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
1413 	 *
1414 	 * The logical_minimum < logical_maximum check is done so that we
1415 	 * don't unintentionally discard values sent by devices which
1416 	 * don't specify logical min and max.
1417 	 */
1418 	if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
1419 	    (field->logical_minimum < field->logical_maximum)) {
1420 		if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
1421 		    (value < field->logical_minimum ||
1422 		     value > field->logical_maximum)) {
1423 			dbg_hid("Ignoring out-of-range value %x\n", value);
1424 			return;
1425 		}
1426 		value = clamp(value,
1427 			      field->logical_minimum,
1428 			      field->logical_maximum);
1429 	}
1430 
1431 	/*
1432 	 * Ignore reports for absolute data if the data didn't change. This is
1433 	 * not only an optimization but also fixes 'dead' key reports. Some
1434 	 * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
1435 	 * 0x31 and 0x32) report multiple keys, even though a localized keyboard
1436 	 * can only have one of them physically available. The 'dead' keys
1437 	 * report constant 0. As all map to the same keycode, they'd confuse
1438 	 * the input layer. If we filter the 'dead' keys on the HID level, we
1439 	 * skip the keycode translation and only forward real events.
1440 	 */
1441 	if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
1442 	                      HID_MAIN_ITEM_BUFFERED_BYTE)) &&
1443 			      (field->flags & HID_MAIN_ITEM_VARIABLE) &&
1444 	    usage->usage_index < field->maxusage &&
1445 	    value == field->value[usage->usage_index])
1446 		return;
1447 
1448 	/* report the usage code as scancode if the key status has changed */
1449 	if (usage->type == EV_KEY &&
1450 	    (!test_bit(usage->code, input->key)) == value)
1451 		input_event(input, EV_MSC, MSC_SCAN, usage->hid);
1452 
1453 	input_event(input, usage->type, usage->code, value);
1454 
1455 	if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
1456 	    usage->type == EV_KEY && value) {
1457 		input_sync(input);
1458 		input_event(input, usage->type, usage->code, 0);
1459 	}
1460 }
1461 
1462 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
1463 {
1464 	struct hid_input *hidinput;
1465 
1466 	if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
1467 		return;
1468 
1469 	list_for_each_entry(hidinput, &hid->inputs, list)
1470 		input_sync(hidinput->input);
1471 }
1472 EXPORT_SYMBOL_GPL(hidinput_report_event);
1473 
1474 static int hidinput_find_field(struct hid_device *hid, unsigned int type,
1475 			       unsigned int code, struct hid_field **field)
1476 {
1477 	struct hid_report *report;
1478 	int i, j;
1479 
1480 	list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
1481 		for (i = 0; i < report->maxfield; i++) {
1482 			*field = report->field[i];
1483 			for (j = 0; j < (*field)->maxusage; j++)
1484 				if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1485 					return j;
1486 		}
1487 	}
1488 	return -1;
1489 }
1490 
1491 struct hid_field *hidinput_get_led_field(struct hid_device *hid)
1492 {
1493 	struct hid_report *report;
1494 	struct hid_field *field;
1495 	int i, j;
1496 
1497 	list_for_each_entry(report,
1498 			    &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1499 			    list) {
1500 		for (i = 0; i < report->maxfield; i++) {
1501 			field = report->field[i];
1502 			for (j = 0; j < field->maxusage; j++)
1503 				if (field->usage[j].type == EV_LED)
1504 					return field;
1505 		}
1506 	}
1507 	return NULL;
1508 }
1509 EXPORT_SYMBOL_GPL(hidinput_get_led_field);
1510 
1511 unsigned int hidinput_count_leds(struct hid_device *hid)
1512 {
1513 	struct hid_report *report;
1514 	struct hid_field *field;
1515 	int i, j;
1516 	unsigned int count = 0;
1517 
1518 	list_for_each_entry(report,
1519 			    &hid->report_enum[HID_OUTPUT_REPORT].report_list,
1520 			    list) {
1521 		for (i = 0; i < report->maxfield; i++) {
1522 			field = report->field[i];
1523 			for (j = 0; j < field->maxusage; j++)
1524 				if (field->usage[j].type == EV_LED &&
1525 				    field->value[j])
1526 					count += 1;
1527 		}
1528 	}
1529 	return count;
1530 }
1531 EXPORT_SYMBOL_GPL(hidinput_count_leds);
1532 
1533 static void hidinput_led_worker(struct work_struct *work)
1534 {
1535 	struct hid_device *hid = container_of(work, struct hid_device,
1536 					      led_work);
1537 	struct hid_field *field;
1538 	struct hid_report *report;
1539 	int ret;
1540 	u32 len;
1541 	__u8 *buf;
1542 
1543 	field = hidinput_get_led_field(hid);
1544 	if (!field)
1545 		return;
1546 
1547 	/*
1548 	 * field->report is accessed unlocked regarding HID core. So there might
1549 	 * be another incoming SET-LED request from user-space, which changes
1550 	 * the LED state while we assemble our outgoing buffer. However, this
1551 	 * doesn't matter as hid_output_report() correctly converts it into a
1552 	 * boolean value no matter what information is currently set on the LED
1553 	 * field (even garbage). So the remote device will always get a valid
1554 	 * request.
1555 	 * And in case we send a wrong value, a next led worker is spawned
1556 	 * for every SET-LED request so the following worker will send the
1557 	 * correct value, guaranteed!
1558 	 */
1559 
1560 	report = field->report;
1561 
1562 	/* use custom SET_REPORT request if possible (asynchronous) */
1563 	if (hid->ll_driver->request)
1564 		return hid->ll_driver->request(hid, report, HID_REQ_SET_REPORT);
1565 
1566 	/* fall back to generic raw-output-report */
1567 	len = hid_report_len(report);
1568 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
1569 	if (!buf)
1570 		return;
1571 
1572 	hid_output_report(report, buf);
1573 	/* synchronous output report */
1574 	ret = hid_hw_output_report(hid, buf, len);
1575 	if (ret == -ENOSYS)
1576 		hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
1577 				HID_REQ_SET_REPORT);
1578 	kfree(buf);
1579 }
1580 
1581 static int hidinput_input_event(struct input_dev *dev, unsigned int type,
1582 				unsigned int code, int value)
1583 {
1584 	struct hid_device *hid = input_get_drvdata(dev);
1585 	struct hid_field *field;
1586 	int offset;
1587 
1588 	if (type == EV_FF)
1589 		return input_ff_event(dev, type, code, value);
1590 
1591 	if (type != EV_LED)
1592 		return -1;
1593 
1594 	if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
1595 		hid_warn(dev, "event field not found\n");
1596 		return -1;
1597 	}
1598 
1599 	hid_set_field(field, offset, value);
1600 
1601 	schedule_work(&hid->led_work);
1602 	return 0;
1603 }
1604 
1605 static int hidinput_open(struct input_dev *dev)
1606 {
1607 	struct hid_device *hid = input_get_drvdata(dev);
1608 
1609 	return hid_hw_open(hid);
1610 }
1611 
1612 static void hidinput_close(struct input_dev *dev)
1613 {
1614 	struct hid_device *hid = input_get_drvdata(dev);
1615 
1616 	hid_hw_close(hid);
1617 }
1618 
1619 static bool __hidinput_change_resolution_multipliers(struct hid_device *hid,
1620 		struct hid_report *report, bool use_logical_max)
1621 {
1622 	struct hid_usage *usage;
1623 	bool update_needed = false;
1624 	bool get_report_completed = false;
1625 	int i, j;
1626 
1627 	if (report->maxfield == 0)
1628 		return false;
1629 
1630 	for (i = 0; i < report->maxfield; i++) {
1631 		__s32 value = use_logical_max ?
1632 			      report->field[i]->logical_maximum :
1633 			      report->field[i]->logical_minimum;
1634 
1635 		/* There is no good reason for a Resolution
1636 		 * Multiplier to have a count other than 1.
1637 		 * Ignore that case.
1638 		 */
1639 		if (report->field[i]->report_count != 1)
1640 			continue;
1641 
1642 		for (j = 0; j < report->field[i]->maxusage; j++) {
1643 			usage = &report->field[i]->usage[j];
1644 
1645 			if (usage->hid != HID_GD_RESOLUTION_MULTIPLIER)
1646 				continue;
1647 
1648 			/*
1649 			 * If we have more than one feature within this
1650 			 * report we need to fill in the bits from the
1651 			 * others before we can overwrite the ones for the
1652 			 * Resolution Multiplier.
1653 			 *
1654 			 * But if we're not allowed to read from the device,
1655 			 * we just bail. Such a device should not exist
1656 			 * anyway.
1657 			 */
1658 			if (!get_report_completed && report->maxfield > 1) {
1659 				if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
1660 					return update_needed;
1661 
1662 				hid_hw_request(hid, report, HID_REQ_GET_REPORT);
1663 				hid_hw_wait(hid);
1664 				get_report_completed = true;
1665 			}
1666 
1667 			report->field[i]->value[j] = value;
1668 			update_needed = true;
1669 		}
1670 	}
1671 
1672 	return update_needed;
1673 }
1674 
1675 static void hidinput_change_resolution_multipliers(struct hid_device *hid)
1676 {
1677 	struct hid_report_enum *rep_enum;
1678 	struct hid_report *rep;
1679 	int ret;
1680 
1681 	rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1682 	list_for_each_entry(rep, &rep_enum->report_list, list) {
1683 		bool update_needed = __hidinput_change_resolution_multipliers(hid,
1684 								     rep, true);
1685 
1686 		if (update_needed) {
1687 			ret = __hid_request(hid, rep, HID_REQ_SET_REPORT);
1688 			if (ret) {
1689 				__hidinput_change_resolution_multipliers(hid,
1690 								    rep, false);
1691 				return;
1692 			}
1693 		}
1694 	}
1695 
1696 	/* refresh our structs */
1697 	hid_setup_resolution_multiplier(hid);
1698 }
1699 
1700 static void report_features(struct hid_device *hid)
1701 {
1702 	struct hid_driver *drv = hid->driver;
1703 	struct hid_report_enum *rep_enum;
1704 	struct hid_report *rep;
1705 	struct hid_usage *usage;
1706 	int i, j;
1707 
1708 	rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1709 	list_for_each_entry(rep, &rep_enum->report_list, list)
1710 		for (i = 0; i < rep->maxfield; i++) {
1711 			/* Ignore if report count is out of bounds. */
1712 			if (rep->field[i]->report_count < 1)
1713 				continue;
1714 
1715 			for (j = 0; j < rep->field[i]->maxusage; j++) {
1716 				usage = &rep->field[i]->usage[j];
1717 
1718 				/* Verify if Battery Strength feature is available */
1719 				if (usage->hid == HID_DC_BATTERYSTRENGTH)
1720 					hidinput_setup_battery(hid, HID_FEATURE_REPORT,
1721 							       rep->field[i], false);
1722 
1723 				if (drv->feature_mapping)
1724 					drv->feature_mapping(hid, rep->field[i], usage);
1725 			}
1726 		}
1727 }
1728 
1729 static struct hid_input *hidinput_allocate(struct hid_device *hid,
1730 					   unsigned int application)
1731 {
1732 	struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1733 	struct input_dev *input_dev = input_allocate_device();
1734 	const char *suffix = NULL;
1735 	size_t suffix_len, name_len;
1736 
1737 	if (!hidinput || !input_dev)
1738 		goto fail;
1739 
1740 	if ((hid->quirks & HID_QUIRK_INPUT_PER_APP) &&
1741 	    hid->maxapplication > 1) {
1742 		switch (application) {
1743 		case HID_GD_KEYBOARD:
1744 			suffix = "Keyboard";
1745 			break;
1746 		case HID_GD_KEYPAD:
1747 			suffix = "Keypad";
1748 			break;
1749 		case HID_GD_MOUSE:
1750 			suffix = "Mouse";
1751 			break;
1752 		case HID_DG_PEN:
1753 			/*
1754 			 * yes, there is an issue here:
1755 			 *  DG_PEN -> "Stylus"
1756 			 *  DG_STYLUS -> "Pen"
1757 			 * But changing this now means users with config snippets
1758 			 * will have to change it and the test suite will not be happy.
1759 			 */
1760 			suffix = "Stylus";
1761 			break;
1762 		case HID_DG_STYLUS:
1763 			suffix = "Pen";
1764 			break;
1765 		case HID_DG_TOUCHSCREEN:
1766 			suffix = "Touchscreen";
1767 			break;
1768 		case HID_DG_TOUCHPAD:
1769 			suffix = "Touchpad";
1770 			break;
1771 		case HID_GD_SYSTEM_CONTROL:
1772 			suffix = "System Control";
1773 			break;
1774 		case HID_CP_CONSUMER_CONTROL:
1775 			suffix = "Consumer Control";
1776 			break;
1777 		case HID_GD_WIRELESS_RADIO_CTLS:
1778 			suffix = "Wireless Radio Control";
1779 			break;
1780 		case HID_GD_SYSTEM_MULTIAXIS:
1781 			suffix = "System Multi Axis";
1782 			break;
1783 		default:
1784 			break;
1785 		}
1786 	}
1787 
1788 	if (suffix) {
1789 		name_len = strlen(hid->name);
1790 		suffix_len = strlen(suffix);
1791 		if ((name_len < suffix_len) ||
1792 		    strcmp(hid->name + name_len - suffix_len, suffix)) {
1793 			hidinput->name = kasprintf(GFP_KERNEL, "%s %s",
1794 						   hid->name, suffix);
1795 			if (!hidinput->name)
1796 				goto fail;
1797 		}
1798 	}
1799 
1800 	input_set_drvdata(input_dev, hid);
1801 	input_dev->event = hidinput_input_event;
1802 	input_dev->open = hidinput_open;
1803 	input_dev->close = hidinput_close;
1804 	input_dev->setkeycode = hidinput_setkeycode;
1805 	input_dev->getkeycode = hidinput_getkeycode;
1806 
1807 	input_dev->name = hidinput->name ? hidinput->name : hid->name;
1808 	input_dev->phys = hid->phys;
1809 	input_dev->uniq = hid->uniq;
1810 	input_dev->id.bustype = hid->bus;
1811 	input_dev->id.vendor  = hid->vendor;
1812 	input_dev->id.product = hid->product;
1813 	input_dev->id.version = hid->version;
1814 	input_dev->dev.parent = &hid->dev;
1815 
1816 	hidinput->input = input_dev;
1817 	hidinput->application = application;
1818 	list_add_tail(&hidinput->list, &hid->inputs);
1819 
1820 	INIT_LIST_HEAD(&hidinput->reports);
1821 
1822 	return hidinput;
1823 
1824 fail:
1825 	kfree(hidinput);
1826 	input_free_device(input_dev);
1827 	hid_err(hid, "Out of memory during hid input probe\n");
1828 	return NULL;
1829 }
1830 
1831 static bool hidinput_has_been_populated(struct hid_input *hidinput)
1832 {
1833 	int i;
1834 	unsigned long r = 0;
1835 
1836 	for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
1837 		r |= hidinput->input->evbit[i];
1838 
1839 	for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
1840 		r |= hidinput->input->keybit[i];
1841 
1842 	for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
1843 		r |= hidinput->input->relbit[i];
1844 
1845 	for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
1846 		r |= hidinput->input->absbit[i];
1847 
1848 	for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
1849 		r |= hidinput->input->mscbit[i];
1850 
1851 	for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
1852 		r |= hidinput->input->ledbit[i];
1853 
1854 	for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
1855 		r |= hidinput->input->sndbit[i];
1856 
1857 	for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
1858 		r |= hidinput->input->ffbit[i];
1859 
1860 	for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
1861 		r |= hidinput->input->swbit[i];
1862 
1863 	return !!r;
1864 }
1865 
1866 static void hidinput_cleanup_hidinput(struct hid_device *hid,
1867 		struct hid_input *hidinput)
1868 {
1869 	struct hid_report *report;
1870 	int i, k;
1871 
1872 	list_del(&hidinput->list);
1873 	input_free_device(hidinput->input);
1874 	kfree(hidinput->name);
1875 
1876 	for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1877 		if (k == HID_OUTPUT_REPORT &&
1878 			hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1879 			continue;
1880 
1881 		list_for_each_entry(report, &hid->report_enum[k].report_list,
1882 				    list) {
1883 
1884 			for (i = 0; i < report->maxfield; i++)
1885 				if (report->field[i]->hidinput == hidinput)
1886 					report->field[i]->hidinput = NULL;
1887 		}
1888 	}
1889 
1890 	kfree(hidinput);
1891 }
1892 
1893 static struct hid_input *hidinput_match(struct hid_report *report)
1894 {
1895 	struct hid_device *hid = report->device;
1896 	struct hid_input *hidinput;
1897 
1898 	list_for_each_entry(hidinput, &hid->inputs, list) {
1899 		if (hidinput->report &&
1900 		    hidinput->report->id == report->id)
1901 			return hidinput;
1902 	}
1903 
1904 	return NULL;
1905 }
1906 
1907 static struct hid_input *hidinput_match_application(struct hid_report *report)
1908 {
1909 	struct hid_device *hid = report->device;
1910 	struct hid_input *hidinput;
1911 
1912 	list_for_each_entry(hidinput, &hid->inputs, list) {
1913 		if (hidinput->application == report->application)
1914 			return hidinput;
1915 
1916 		/*
1917 		 * Keep SystemControl and ConsumerControl applications together
1918 		 * with the main keyboard, if present.
1919 		 */
1920 		if ((report->application == HID_GD_SYSTEM_CONTROL ||
1921 		     report->application == HID_CP_CONSUMER_CONTROL) &&
1922 		    hidinput->application == HID_GD_KEYBOARD) {
1923 			return hidinput;
1924 		}
1925 	}
1926 
1927 	return NULL;
1928 }
1929 
1930 static inline void hidinput_configure_usages(struct hid_input *hidinput,
1931 					     struct hid_report *report)
1932 {
1933 	int i, j;
1934 
1935 	for (i = 0; i < report->maxfield; i++)
1936 		for (j = 0; j < report->field[i]->maxusage; j++)
1937 			hidinput_configure_usage(hidinput, report->field[i],
1938 						 report->field[i]->usage + j);
1939 }
1940 
1941 /*
1942  * Register the input device; print a message.
1943  * Configure the input layer interface
1944  * Read all reports and initialize the absolute field values.
1945  */
1946 
1947 int hidinput_connect(struct hid_device *hid, unsigned int force)
1948 {
1949 	struct hid_driver *drv = hid->driver;
1950 	struct hid_report *report;
1951 	struct hid_input *next, *hidinput = NULL;
1952 	unsigned int application;
1953 	int i, k;
1954 
1955 	INIT_LIST_HEAD(&hid->inputs);
1956 	INIT_WORK(&hid->led_work, hidinput_led_worker);
1957 
1958 	hid->status &= ~HID_STAT_DUP_DETECTED;
1959 
1960 	if (!force) {
1961 		for (i = 0; i < hid->maxcollection; i++) {
1962 			struct hid_collection *col = &hid->collection[i];
1963 			if (col->type == HID_COLLECTION_APPLICATION ||
1964 					col->type == HID_COLLECTION_PHYSICAL)
1965 				if (IS_INPUT_APPLICATION(col->usage))
1966 					break;
1967 		}
1968 
1969 		if (i == hid->maxcollection)
1970 			return -1;
1971 	}
1972 
1973 	report_features(hid);
1974 
1975 	for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1976 		if (k == HID_OUTPUT_REPORT &&
1977 			hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1978 			continue;
1979 
1980 		list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1981 
1982 			if (!report->maxfield)
1983 				continue;
1984 
1985 			application = report->application;
1986 
1987 			/*
1988 			 * Find the previous hidinput report attached
1989 			 * to this report id.
1990 			 */
1991 			if (hid->quirks & HID_QUIRK_MULTI_INPUT)
1992 				hidinput = hidinput_match(report);
1993 			else if (hid->maxapplication > 1 &&
1994 				 (hid->quirks & HID_QUIRK_INPUT_PER_APP))
1995 				hidinput = hidinput_match_application(report);
1996 
1997 			if (!hidinput) {
1998 				hidinput = hidinput_allocate(hid, application);
1999 				if (!hidinput)
2000 					goto out_unwind;
2001 			}
2002 
2003 			hidinput_configure_usages(hidinput, report);
2004 
2005 			if (hid->quirks & HID_QUIRK_MULTI_INPUT)
2006 				hidinput->report = report;
2007 
2008 			list_add_tail(&report->hidinput_list,
2009 				      &hidinput->reports);
2010 		}
2011 	}
2012 
2013 	hidinput_change_resolution_multipliers(hid);
2014 
2015 	list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
2016 		if (drv->input_configured &&
2017 		    drv->input_configured(hid, hidinput))
2018 			goto out_unwind;
2019 
2020 		if (!hidinput_has_been_populated(hidinput)) {
2021 			/* no need to register an input device not populated */
2022 			hidinput_cleanup_hidinput(hid, hidinput);
2023 			continue;
2024 		}
2025 
2026 		if (input_register_device(hidinput->input))
2027 			goto out_unwind;
2028 		hidinput->registered = true;
2029 	}
2030 
2031 	if (list_empty(&hid->inputs)) {
2032 		hid_err(hid, "No inputs registered, leaving\n");
2033 		goto out_unwind;
2034 	}
2035 
2036 	if (hid->status & HID_STAT_DUP_DETECTED)
2037 		hid_dbg(hid,
2038 			"Some usages could not be mapped, please use HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE if this is legitimate.\n");
2039 
2040 	return 0;
2041 
2042 out_unwind:
2043 	/* unwind the ones we already registered */
2044 	hidinput_disconnect(hid);
2045 
2046 	return -1;
2047 }
2048 EXPORT_SYMBOL_GPL(hidinput_connect);
2049 
2050 void hidinput_disconnect(struct hid_device *hid)
2051 {
2052 	struct hid_input *hidinput, *next;
2053 
2054 	hidinput_cleanup_battery(hid);
2055 
2056 	list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
2057 		list_del(&hidinput->list);
2058 		if (hidinput->registered)
2059 			input_unregister_device(hidinput->input);
2060 		else
2061 			input_free_device(hidinput->input);
2062 		kfree(hidinput->name);
2063 		kfree(hidinput);
2064 	}
2065 
2066 	/* led_work is spawned by input_dev callbacks, but doesn't access the
2067 	 * parent input_dev at all. Once all input devices are removed, we
2068 	 * know that led_work will never get restarted, so we can cancel it
2069 	 * synchronously and are safe. */
2070 	cancel_work_sync(&hid->led_work);
2071 }
2072 EXPORT_SYMBOL_GPL(hidinput_disconnect);
2073