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