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