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