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