projects/linux·blob master · refs

3290 lines · 81.6 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * HID support for Linux
4 *
5 * Copyright (c) 1999 Andreas Gal
6 * Copyright (c) 2000-2005 Vojtech Pavlik <[email protected]>
7 * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
8 * Copyright (c) 2006-2012 Jiri Kosina
9 */
10
11/*
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/mm.h>
22#include <linux/spinlock.h>
23#include <linux/unaligned.h>
24#include <asm/byteorder.h>
25#include <linux/input.h>
26#include <linux/wait.h>
27#include <linux/vmalloc.h>
28#include <linux/sched.h>
29#include <linux/semaphore.h>
30
31#include <linux/hid.h>
32#include <linux/hiddev.h>
33#include <linux/hid-debug.h>
34#include <linux/hidraw.h>
35
36#include "hid-ids.h"
37
38/*
39 * Version Information
40 */
41
42#define DRIVER_DESC "HID core driver"
43
44static int hid_ignore_special_drivers = 0;
45module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
46MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
47
48/*
49 * Convert a signed n-bit integer to signed 32-bit integer.
50 */
51
52static s32 snto32(__u32 value, unsigned int n)
53{
54 if (!value || !n)
55 return 0;
56
57 if (n > 32)
58 n = 32;
59
60 return sign_extend32(value, n - 1);
61}
62
63/*
64 * Convert a signed 32-bit integer to a signed n-bit integer.
65 */
66
67static u32 s32ton(__s32 value, unsigned int n)
68{
69 s32 a;
70
71 if (!value || !n)
72 return 0;
73
74 if (n > 32)
75 n = 32;
76
77 a = value >> (n - 1);
78 if (a && a != -1)
79 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
80 return value & ((1 << n) - 1);
81}
82
83/*
84 * Register a new report for a device.
85 */
86
87struct hid_report *hid_register_report(struct hid_device *device,
88 enum hid_report_type type, unsigned int id,
89 unsigned int application)
90{
91 struct hid_report_enum *report_enum = device->report_enum + type;
92 struct hid_report *report;
93
94 if (id >= HID_MAX_IDS)
95 return NULL;
96 if (report_enum->report_id_hash[id])
97 return report_enum->report_id_hash[id];
98
99 report = kzalloc_obj(struct hid_report);
100 if (!report)
101 return NULL;
102
103 if (id != 0)
104 report_enum->numbered = 1;
105
106 report->id = id;
107 report->type = type;
108 report->size = 0;
109 report->device = device;
110 report->application = application;
111 report_enum->report_id_hash[id] = report;
112
113 list_add_tail(&report->list, &report_enum->report_list);
114 INIT_LIST_HEAD(&report->field_entry_list);
115
116 return report;
117}
118EXPORT_SYMBOL_GPL(hid_register_report);
119
120/*
121 * Register a new field for this report.
122 */
123
124static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages)
125{
126 struct hid_field *field;
127
128 if (report->maxfield == HID_MAX_FIELDS) {
129 hid_err(report->device, "too many fields in report\n");
130 return NULL;
131 }
132
133 field = kvzalloc((sizeof(struct hid_field) +
134 usages * sizeof(struct hid_usage) +
135 3 * usages * sizeof(unsigned int)), GFP_KERNEL);
136 if (!field)
137 return NULL;
138
139 field->index = report->maxfield++;
140 report->field[field->index] = field;
141 field->usage = (struct hid_usage *)(field + 1);
142 field->value = (s32 *)(field->usage + usages);
143 field->new_value = (s32 *)(field->value + usages);
144 field->usages_priorities = (s32 *)(field->new_value + usages);
145 field->report = report;
146
147 return field;
148}
149
150/*
151 * Open a collection. The type/usage is pushed on the stack.
152 */
153
154static int open_collection(struct hid_parser *parser, unsigned type)
155{
156 struct hid_collection *collection;
157 unsigned usage;
158 int collection_index;
159
160 usage = parser->local.usage[0];
161
162 if (parser->collection_stack_ptr == parser->collection_stack_size) {
163 unsigned int *collection_stack;
164 unsigned int new_size = parser->collection_stack_size +
165 HID_COLLECTION_STACK_SIZE;
166
167 collection_stack = krealloc(parser->collection_stack,
168 new_size * sizeof(unsigned int),
169 GFP_KERNEL);
170 if (!collection_stack)
171 return -ENOMEM;
172
173 parser->collection_stack = collection_stack;
174 parser->collection_stack_size = new_size;
175 }
176
177 if (parser->device->maxcollection == parser->device->collection_size) {
178 collection = kmalloc(
179 array3_size(sizeof(struct hid_collection),
180 parser->device->collection_size,
181 2),
182 GFP_KERNEL);
183 if (collection == NULL) {
184 hid_err(parser->device, "failed to reallocate collection array\n");
185 return -ENOMEM;
186 }
187 memcpy(collection, parser->device->collection,
188 sizeof(struct hid_collection) *
189 parser->device->collection_size);
190 memset(collection + parser->device->collection_size, 0,
191 sizeof(struct hid_collection) *
192 parser->device->collection_size);
193 kfree(parser->device->collection);
194 parser->device->collection = collection;
195 parser->device->collection_size *= 2;
196 }
197
198 parser->collection_stack[parser->collection_stack_ptr++] =
199 parser->device->maxcollection;
200
201 collection_index = parser->device->maxcollection++;
202 collection = parser->device->collection + collection_index;
203 collection->type = type;
204 collection->usage = usage;
205 collection->level = parser->collection_stack_ptr - 1;
206 collection->parent_idx = (collection->level == 0) ? -1 :
207 parser->collection_stack[collection->level - 1];
208
209 if (type == HID_COLLECTION_APPLICATION)
210 parser->device->maxapplication++;
211
212 return 0;
213}
214
215/*
216 * Close a collection.
217 */
218
219static int close_collection(struct hid_parser *parser)
220{
221 if (!parser->collection_stack_ptr) {
222 hid_err(parser->device, "collection stack underflow\n");
223 return -EINVAL;
224 }
225 parser->collection_stack_ptr--;
226 return 0;
227}
228
229/*
230 * Climb up the stack, search for the specified collection type
231 * and return the usage.
232 */
233
234static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
235{
236 struct hid_collection *collection = parser->device->collection;
237 int n;
238
239 for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
240 unsigned index = parser->collection_stack[n];
241 if (collection[index].type == type)
242 return collection[index].usage;
243 }
244 return 0; /* we know nothing about this usage type */
245}
246
247/*
248 * Concatenate usage which defines 16 bits or less with the
249 * currently defined usage page to form a 32 bit usage
250 */
251
252static void complete_usage(struct hid_parser *parser, unsigned int index)
253{
254 parser->local.usage[index] &= 0xFFFF;
255 parser->local.usage[index] |=
256 (parser->global.usage_page & 0xFFFF) << 16;
257}
258
259/*
260 * Add a usage to the temporary parser table.
261 */
262
263static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
264{
265 if (parser->local.usage_index >= HID_MAX_USAGES) {
266 hid_err(parser->device, "usage index exceeded\n");
267 return -1;
268 }
269 parser->local.usage[parser->local.usage_index] = usage;
270
271 /*
272 * If Usage item only includes usage id, concatenate it with
273 * currently defined usage page
274 */
275 if (size <= 2)
276 complete_usage(parser, parser->local.usage_index);
277
278 parser->local.usage_size[parser->local.usage_index] = size;
279 parser->local.collection_index[parser->local.usage_index] =
280 parser->collection_stack_ptr ?
281 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
282 parser->local.usage_index++;
283 return 0;
284}
285
286/*
287 * Register a new field for this report.
288 */
289
290static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
291{
292 struct hid_report *report;
293 struct hid_field *field;
294 unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
295 unsigned int usages;
296 unsigned int offset;
297 unsigned int i;
298 unsigned int application;
299
300 application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
301
302 report = hid_register_report(parser->device, report_type,
303 parser->global.report_id, application);
304 if (!report) {
305 hid_err(parser->device, "hid_register_report failed\n");
306 return -1;
307 }
308
309 /* Handle both signed and unsigned cases properly */
310 if ((parser->global.logical_minimum < 0 &&
311 parser->global.logical_maximum <
312 parser->global.logical_minimum) ||
313 (parser->global.logical_minimum >= 0 &&
314 (__u32)parser->global.logical_maximum <
315 (__u32)parser->global.logical_minimum)) {
316 dbg_hid("logical range invalid 0x%x 0x%x\n",
317 parser->global.logical_minimum,
318 parser->global.logical_maximum);
319 return -1;
320 }
321
322 offset = report->size;
323 report->size += parser->global.report_size * parser->global.report_count;
324
325 if (parser->device->ll_driver->max_buffer_size)
326 max_buffer_size = parser->device->ll_driver->max_buffer_size;
327
328 /* Total size check: Allow for possible report index byte */
329 if (report->size > (max_buffer_size - 1) << 3) {
330 hid_err(parser->device, "report is too long\n");
331 return -1;
332 }
333
334 if (!parser->local.usage_index) /* Ignore padding fields */
335 return 0;
336
337 usages = max_t(unsigned, parser->local.usage_index,
338 parser->global.report_count);
339
340 field = hid_register_field(report, usages);
341 if (!field)
342 return 0;
343
344 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
345 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
346 field->application = application;
347
348 for (i = 0; i < usages; i++) {
349 unsigned j = i;
350 /* Duplicate the last usage we parsed if we have excess values */
351 if (i >= parser->local.usage_index)
352 j = parser->local.usage_index - 1;
353 field->usage[i].hid = parser->local.usage[j];
354 field->usage[i].collection_index =
355 parser->local.collection_index[j];
356 field->usage[i].usage_index = i;
357 field->usage[i].resolution_multiplier = 1;
358 }
359
360 field->maxusage = usages;
361 field->flags = flags;
362 field->report_offset = offset;
363 field->report_type = report_type;
364 field->report_size = parser->global.report_size;
365 field->report_count = parser->global.report_count;
366 field->logical_minimum = parser->global.logical_minimum;
367 field->logical_maximum = parser->global.logical_maximum;
368 field->physical_minimum = parser->global.physical_minimum;
369 field->physical_maximum = parser->global.physical_maximum;
370 field->unit_exponent = parser->global.unit_exponent;
371 field->unit = parser->global.unit;
372
373 return 0;
374}
375
376/*
377 * Read data value from item.
378 */
379
380static u32 item_udata(struct hid_item *item)
381{
382 if (item->format != HID_ITEM_FORMAT_SHORT)
383 return 0;
384
385 switch (item->size) {
386 case 1: return item->data.u8;
387 case 2: return item->data.u16;
388 case 4: return item->data.u32;
389 }
390 return 0;
391}
392
393static s32 item_sdata(struct hid_item *item)
394{
395 if (item->format != HID_ITEM_FORMAT_SHORT)
396 return 0;
397
398 switch (item->size) {
399 case 1: return item->data.s8;
400 case 2: return item->data.s16;
401 case 4: return item->data.s32;
402 }
403 return 0;
404}
405
406/*
407 * Process a global item.
408 */
409
410static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
411{
412 __s32 raw_value;
413 switch (item->tag) {
414 case HID_GLOBAL_ITEM_TAG_PUSH:
415
416 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
417 hid_err(parser->device, "global environment stack overflow\n");
418 return -1;
419 }
420
421 memcpy(parser->global_stack + parser->global_stack_ptr++,
422 &parser->global, sizeof(struct hid_global));
423 return 0;
424
425 case HID_GLOBAL_ITEM_TAG_POP:
426
427 if (!parser->global_stack_ptr) {
428 hid_err(parser->device, "global environment stack underflow\n");
429 return -1;
430 }
431
432 memcpy(&parser->global, parser->global_stack +
433 --parser->global_stack_ptr, sizeof(struct hid_global));
434 return 0;
435
436 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
437 parser->global.usage_page = item_udata(item);
438 return 0;
439
440 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
441 parser->global.logical_minimum = item_sdata(item);
442 return 0;
443
444 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
445 if (parser->global.logical_minimum < 0)
446 parser->global.logical_maximum = item_sdata(item);
447 else
448 parser->global.logical_maximum = item_udata(item);
449 return 0;
450
451 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
452 parser->global.physical_minimum = item_sdata(item);
453 return 0;
454
455 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
456 if (parser->global.physical_minimum < 0)
457 parser->global.physical_maximum = item_sdata(item);
458 else
459 parser->global.physical_maximum = item_udata(item);
460 return 0;
461
462 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
463 /* Many devices provide unit exponent as a two's complement
464 * nibble due to the common misunderstanding of HID
465 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
466 * both this and the standard encoding. */
467 raw_value = item_sdata(item);
468 if (!(raw_value & 0xfffffff0))
469 parser->global.unit_exponent = snto32(raw_value, 4);
470 else
471 parser->global.unit_exponent = raw_value;
472 return 0;
473
474 case HID_GLOBAL_ITEM_TAG_UNIT:
475 parser->global.unit = item_udata(item);
476 return 0;
477
478 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
479 parser->global.report_size = item_udata(item);
480 if (parser->global.report_size > 256) {
481 hid_err(parser->device, "invalid report_size %d\n",
482 parser->global.report_size);
483 return -1;
484 }
485 return 0;
486
487 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
488 parser->global.report_count = item_udata(item);
489 if (parser->global.report_count > HID_MAX_USAGES) {
490 hid_err(parser->device, "invalid report_count %d\n",
491 parser->global.report_count);
492 return -1;
493 }
494 return 0;
495
496 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
497 parser->global.report_id = item_udata(item);
498 if (parser->global.report_id == 0 ||
499 parser->global.report_id >= HID_MAX_IDS) {
500 hid_err(parser->device, "report_id %u is invalid\n",
501 parser->global.report_id);
502 return -1;
503 }
504 return 0;
505
506 default:
507 hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
508 return -1;
509 }
510}
511
512/*
513 * Process a local item.
514 */
515
516static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
517{
518 __u32 data;
519 unsigned n;
520 __u32 count;
521
522 data = item_udata(item);
523
524 switch (item->tag) {
525 case HID_LOCAL_ITEM_TAG_DELIMITER:
526
527 if (data) {
528 /*
529 * We treat items before the first delimiter
530 * as global to all usage sets (branch 0).
531 * In the moment we process only these global
532 * items and the first delimiter set.
533 */
534 if (parser->local.delimiter_depth != 0) {
535 hid_err(parser->device, "nested delimiters\n");
536 return -1;
537 }
538 parser->local.delimiter_depth++;
539 parser->local.delimiter_branch++;
540 } else {
541 if (parser->local.delimiter_depth < 1) {
542 hid_err(parser->device, "bogus close delimiter\n");
543 return -1;
544 }
545 parser->local.delimiter_depth--;
546 }
547 return 0;
548
549 case HID_LOCAL_ITEM_TAG_USAGE:
550
551 if (parser->local.delimiter_branch > 1) {
552 dbg_hid("alternative usage ignored\n");
553 return 0;
554 }
555
556 return hid_add_usage(parser, data, item->size);
557
558 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
559
560 if (parser->local.delimiter_branch > 1) {
561 dbg_hid("alternative usage ignored\n");
562 return 0;
563 }
564
565 parser->local.usage_minimum = data;
566 return 0;
567
568 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
569
570 if (parser->local.delimiter_branch > 1) {
571 dbg_hid("alternative usage ignored\n");
572 return 0;
573 }
574
575 count = data - parser->local.usage_minimum;
576 if (count + parser->local.usage_index >= HID_MAX_USAGES) {
577 /*
578 * We do not warn if the name is not set, we are
579 * actually pre-scanning the device.
580 */
581 if (dev_name(&parser->device->dev))
582 hid_warn(parser->device,
583 "ignoring exceeding usage max\n");
584 data = HID_MAX_USAGES - parser->local.usage_index +
585 parser->local.usage_minimum - 1;
586 if (data <= 0) {
587 hid_err(parser->device,
588 "no more usage index available\n");
589 return -1;
590 }
591 }
592
593 for (n = parser->local.usage_minimum; n <= data; n++)
594 if (hid_add_usage(parser, n, item->size)) {
595 dbg_hid("hid_add_usage failed\n");
596 return -1;
597 }
598 return 0;
599
600 default:
601
602 dbg_hid("unknown local item tag 0x%x\n", item->tag);
603 return 0;
604 }
605 return 0;
606}
607
608/*
609 * Concatenate Usage Pages into Usages where relevant:
610 * As per specification, 6.2.2.8: "When the parser encounters a main item it
611 * concatenates the last declared Usage Page with a Usage to form a complete
612 * usage value."
613 */
614
615static void hid_concatenate_last_usage_page(struct hid_parser *parser)
616{
617 int i;
618 unsigned int usage_page;
619 unsigned int current_page;
620
621 if (!parser->local.usage_index)
622 return;
623
624 usage_page = parser->global.usage_page;
625
626 /*
627 * Concatenate usage page again only if last declared Usage Page
628 * has not been already used in previous usages concatenation
629 */
630 for (i = parser->local.usage_index - 1; i >= 0; i--) {
631 if (parser->local.usage_size[i] > 2)
632 /* Ignore extended usages */
633 continue;
634
635 current_page = parser->local.usage[i] >> 16;
636 if (current_page == usage_page)
637 break;
638
639 complete_usage(parser, i);
640 }
641}
642
643/*
644 * Process a main item.
645 */
646
647static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
648{
649 __u32 data;
650 int ret;
651
652 hid_concatenate_last_usage_page(parser);
653
654 data = item_udata(item);
655
656 switch (item->tag) {
657 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
658 ret = open_collection(parser, data & 0xff);
659 break;
660 case HID_MAIN_ITEM_TAG_END_COLLECTION:
661 ret = close_collection(parser);
662 break;
663 case HID_MAIN_ITEM_TAG_INPUT:
664 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
665 break;
666 case HID_MAIN_ITEM_TAG_OUTPUT:
667 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
668 break;
669 case HID_MAIN_ITEM_TAG_FEATURE:
670 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
671 break;
672 default:
673 if (item->tag >= HID_MAIN_ITEM_TAG_RESERVED_MIN &&
674 item->tag <= HID_MAIN_ITEM_TAG_RESERVED_MAX)
675 hid_warn_ratelimited(parser->device, "reserved main item tag 0x%x\n", item->tag);
676 else
677 hid_warn_ratelimited(parser->device, "unknown main item tag 0x%x\n", item->tag);
678 ret = 0;
679 }
680
681 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
682
683 return ret;
684}
685
686/*
687 * Process a reserved item.
688 */
689
690static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
691{
692 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
693 return 0;
694}
695
696/*
697 * Free a report and all registered fields. The field->usage and
698 * field->value table's are allocated behind the field, so we need
699 * only to free(field) itself.
700 */
701
702static void hid_free_report(struct hid_report *report)
703{
704 unsigned n;
705
706 kfree(report->field_entries);
707
708 for (n = 0; n < report->maxfield; n++)
709 kvfree(report->field[n]);
710 kfree(report);
711}
712
713/*
714 * Close report. This function returns the device
715 * state to the point prior to hid_open_report().
716 */
717static void hid_close_report(struct hid_device *device)
718{
719 unsigned i, j;
720
721 for (i = 0; i < HID_REPORT_TYPES; i++) {
722 struct hid_report_enum *report_enum = device->report_enum + i;
723
724 for (j = 0; j < HID_MAX_IDS; j++) {
725 struct hid_report *report = report_enum->report_id_hash[j];
726 if (report)
727 hid_free_report(report);
728 }
729 memset(report_enum, 0, sizeof(*report_enum));
730 INIT_LIST_HEAD(&report_enum->report_list);
731 }
732
733 /*
734 * If the HID driver had a rdesc_fixup() callback, dev->rdesc
735 * will be allocated by hid-core and needs to be freed.
736 * Otherwise, it is either equal to dev_rdesc or bpf_rdesc, in
737 * which cases it'll be freed later on device removal or destroy.
738 */
739 if (device->rdesc != device->dev_rdesc && device->rdesc != device->bpf_rdesc)
740 kfree(device->rdesc);
741 device->rdesc = NULL;
742 device->rsize = 0;
743
744 kfree(device->collection);
745 device->collection = NULL;
746 device->collection_size = 0;
747 device->maxcollection = 0;
748 device->maxapplication = 0;
749
750 device->status &= ~HID_STAT_PARSED;
751}
752
753static inline void hid_free_bpf_rdesc(struct hid_device *hdev)
754{
755 /* bpf_rdesc is either equal to dev_rdesc or allocated by call_hid_bpf_rdesc_fixup() */
756 if (hdev->bpf_rdesc != hdev->dev_rdesc)
757 kfree(hdev->bpf_rdesc);
758 hdev->bpf_rdesc = NULL;
759}
760
761/*
762 * Free a device structure, all reports, and all fields.
763 */
764
765void hiddev_free(struct kref *ref)
766{
767 struct hid_device *hid = container_of(ref, struct hid_device, ref);
768
769 hid_close_report(hid);
770 hid_free_bpf_rdesc(hid);
771 kfree(hid->dev_rdesc);
772 kfree(hid);
773}
774
775static void hid_device_release(struct device *dev)
776{
777 struct hid_device *hid = to_hid_device(dev);
778
779 kref_put(&hid->ref, hiddev_free);
780}
781
782/*
783 * Fetch a report description item from the data stream. We support long
784 * items, though they are not used yet.
785 */
786
787static const u8 *fetch_item(const __u8 *start, const __u8 *end, struct hid_item *item)
788{
789 u8 b;
790
791 if ((end - start) <= 0)
792 return NULL;
793
794 b = *start++;
795
796 item->type = (b >> 2) & 3;
797 item->tag = (b >> 4) & 15;
798
799 if (item->tag == HID_ITEM_TAG_LONG) {
800
801 item->format = HID_ITEM_FORMAT_LONG;
802
803 if ((end - start) < 2)
804 return NULL;
805
806 item->size = *start++;
807 item->tag = *start++;
808
809 if ((end - start) < item->size)
810 return NULL;
811
812 item->data.longdata = start;
813 start += item->size;
814 return start;
815 }
816
817 item->format = HID_ITEM_FORMAT_SHORT;
818 item->size = BIT(b & 3) >> 1; /* 0, 1, 2, 3 -> 0, 1, 2, 4 */
819
820 if (end - start < item->size)
821 return NULL;
822
823 switch (item->size) {
824 case 0:
825 break;
826
827 case 1:
828 item->data.u8 = *start;
829 break;
830
831 case 2:
832 item->data.u16 = get_unaligned_le16(start);
833 break;
834
835 case 4:
836 item->data.u32 = get_unaligned_le32(start);
837 break;
838 }
839
840 return start + item->size;
841}
842
843static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
844{
845 struct hid_device *hid = parser->device;
846
847 if (usage == HID_DG_CONTACTID)
848 hid->group = HID_GROUP_MULTITOUCH;
849}
850
851static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
852{
853 if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
854 parser->global.report_size == 8)
855 parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
856
857 if (usage == 0xff0000c6 && parser->global.report_count == 1 &&
858 parser->global.report_size == 8)
859 parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
860}
861
862static void hid_scan_collection(struct hid_parser *parser, unsigned type)
863{
864 struct hid_device *hid = parser->device;
865 int i;
866
867 if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
868 (type == HID_COLLECTION_PHYSICAL ||
869 type == HID_COLLECTION_APPLICATION))
870 hid->group = HID_GROUP_SENSOR_HUB;
871
872 if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
873 hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
874 hid->group == HID_GROUP_MULTITOUCH)
875 hid->group = HID_GROUP_GENERIC;
876
877 if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
878 for (i = 0; i < parser->local.usage_index; i++)
879 if (parser->local.usage[i] == HID_GD_POINTER)
880 parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
881
882 if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
883 parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
884
885 if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
886 for (i = 0; i < parser->local.usage_index; i++)
887 if (parser->local.usage[i] ==
888 (HID_UP_GOOGLEVENDOR | 0x0001))
889 parser->device->group =
890 HID_GROUP_VIVALDI;
891}
892
893static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
894{
895 __u32 data;
896 int i;
897
898 hid_concatenate_last_usage_page(parser);
899
900 data = item_udata(item);
901
902 switch (item->tag) {
903 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
904 hid_scan_collection(parser, data & 0xff);
905 break;
906 case HID_MAIN_ITEM_TAG_END_COLLECTION:
907 break;
908 case HID_MAIN_ITEM_TAG_INPUT:
909 /* ignore constant inputs, they will be ignored by hid-input */
910 if (data & HID_MAIN_ITEM_CONSTANT)
911 break;
912 for (i = 0; i < parser->local.usage_index; i++)
913 hid_scan_input_usage(parser, parser->local.usage[i]);
914 break;
915 case HID_MAIN_ITEM_TAG_OUTPUT:
916 break;
917 case HID_MAIN_ITEM_TAG_FEATURE:
918 for (i = 0; i < parser->local.usage_index; i++)
919 hid_scan_feature_usage(parser, parser->local.usage[i]);
920 break;
921 }
922
923 /* Reset the local parser environment */
924 memset(&parser->local, 0, sizeof(parser->local));
925
926 return 0;
927}
928
929/*
930 * Scan a report descriptor before the device is added to the bus.
931 * Sets device groups and other properties that determine what driver
932 * to load.
933 */
934static int hid_scan_report(struct hid_device *hid)
935{
936 struct hid_item item;
937 const __u8 *start = hid->dev_rdesc;
938 const __u8 *end = start + hid->dev_rsize;
939 static int (*dispatch_type[])(struct hid_parser *parser,
940 struct hid_item *item) = {
941 hid_scan_main,
942 hid_parser_global,
943 hid_parser_local,
944 hid_parser_reserved
945 };
946
947 struct hid_parser *parser __free(kvfree) = vzalloc(sizeof(*parser));
948 if (!parser)
949 return -ENOMEM;
950
951 parser->device = hid;
952 hid->group = HID_GROUP_GENERIC;
953
954 /*
955 * In case we are re-scanning after a BPF has been loaded,
956 * we need to use the bpf report descriptor, not the original one.
957 */
958 if (hid->bpf_rdesc && hid->bpf_rsize) {
959 start = hid->bpf_rdesc;
960 end = start + hid->bpf_rsize;
961 }
962
963 /*
964 * The parsing is simpler than the one in hid_open_report() as we should
965 * be robust against hid errors. Those errors will be raised by
966 * hid_open_report() anyway.
967 */
968 while ((start = fetch_item(start, end, &item)) != NULL)
969 dispatch_type[item.type](parser, &item);
970
971 /*
972 * Handle special flags set during scanning.
973 */
974 if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
975 (hid->group == HID_GROUP_MULTITOUCH))
976 hid->group = HID_GROUP_MULTITOUCH_WIN_8;
977
978 /*
979 * Vendor specific handlings
980 */
981 switch (hid->vendor) {
982 case USB_VENDOR_ID_WACOM:
983 hid->group = HID_GROUP_WACOM;
984 break;
985 case USB_VENDOR_ID_SYNAPTICS:
986 if (hid->group == HID_GROUP_GENERIC)
987 if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
988 && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
989 /*
990 * hid-rmi should take care of them,
991 * not hid-generic
992 */
993 hid->group = HID_GROUP_RMI;
994 break;
995 }
996
997 kfree(parser->collection_stack);
998 return 0;
999}
1000
1001/**
1002 * hid_parse_report - parse device report
1003 *
1004 * @hid: hid device
1005 * @start: report start
1006 * @size: report size
1007 *
1008 * Allocate the device report as read by the bus driver. This function should
1009 * only be called from parse() in ll drivers.
1010 */
1011int hid_parse_report(struct hid_device *hid, const __u8 *start, unsigned size)
1012{
1013 hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
1014 if (!hid->dev_rdesc)
1015 return -ENOMEM;
1016 hid->dev_rsize = size;
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(hid_parse_report);
1020
1021static const char * const hid_report_names[] = {
1022 "HID_INPUT_REPORT",
1023 "HID_OUTPUT_REPORT",
1024 "HID_FEATURE_REPORT",
1025};
1026/**
1027 * hid_validate_values - validate existing device report's value indexes
1028 *
1029 * @hid: hid device
1030 * @type: which report type to examine
1031 * @id: which report ID to examine (0 for first)
1032 * @field_index: which report field to examine
1033 * @report_counts: expected number of values
1034 *
1035 * Validate the number of values in a given field of a given report, after
1036 * parsing.
1037 */
1038struct hid_report *hid_validate_values(struct hid_device *hid,
1039 enum hid_report_type type, unsigned int id,
1040 unsigned int field_index,
1041 unsigned int report_counts)
1042{
1043 struct hid_report *report;
1044
1045 if (type > HID_FEATURE_REPORT) {
1046 hid_err(hid, "invalid HID report type %u\n", type);
1047 return NULL;
1048 }
1049
1050 if (id >= HID_MAX_IDS) {
1051 hid_err(hid, "invalid HID report id %u\n", id);
1052 return NULL;
1053 }
1054
1055 /*
1056 * Explicitly not using hid_get_report() here since it depends on
1057 * ->numbered being checked, which may not always be the case when
1058 * drivers go to access report values.
1059 */
1060 if (id == 0) {
1061 /*
1062 * Validating on id 0 means we should examine the first
1063 * report in the list.
1064 */
1065 report = list_first_entry_or_null(
1066 &hid->report_enum[type].report_list,
1067 struct hid_report, list);
1068 } else {
1069 report = hid->report_enum[type].report_id_hash[id];
1070 }
1071 if (!report) {
1072 hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
1073 return NULL;
1074 }
1075 if (report->maxfield <= field_index) {
1076 hid_err(hid, "not enough fields in %s %u\n",
1077 hid_report_names[type], id);
1078 return NULL;
1079 }
1080 if (report->field[field_index]->report_count < report_counts) {
1081 hid_err(hid, "not enough values in %s %u field %u\n",
1082 hid_report_names[type], id, field_index);
1083 return NULL;
1084 }
1085 return report;
1086}
1087EXPORT_SYMBOL_GPL(hid_validate_values);
1088
1089static int hid_calculate_multiplier(struct hid_device *hid,
1090 struct hid_field *multiplier)
1091{
1092 int m;
1093 __s32 v = *multiplier->value;
1094 __s32 lmin = multiplier->logical_minimum;
1095 __s32 lmax = multiplier->logical_maximum;
1096 __s32 pmin = multiplier->physical_minimum;
1097 __s32 pmax = multiplier->physical_maximum;
1098
1099 /*
1100 * "Because OS implementations will generally divide the control's
1101 * reported count by the Effective Resolution Multiplier, designers
1102 * should take care not to establish a potential Effective
1103 * Resolution Multiplier of zero."
1104 * HID Usage Table, v1.12, Section 4.3.1, p31
1105 */
1106 if (lmax - lmin == 0)
1107 return 1;
1108 /*
1109 * Handling the unit exponent is left as an exercise to whoever
1110 * finds a device where that exponent is not 0.
1111 */
1112 m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
1113 if (unlikely(multiplier->unit_exponent != 0)) {
1114 hid_warn(hid,
1115 "unsupported Resolution Multiplier unit exponent %d\n",
1116 multiplier->unit_exponent);
1117 }
1118
1119 /* There are no devices with an effective multiplier > 255 */
1120 if (unlikely(m == 0 || m > 255 || m < -255)) {
1121 hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
1122 m = 1;
1123 }
1124
1125 return m;
1126}
1127
1128static void hid_apply_multiplier_to_field(struct hid_device *hid,
1129 struct hid_field *field,
1130 struct hid_collection *multiplier_collection,
1131 int effective_multiplier)
1132{
1133 struct hid_collection *collection;
1134 struct hid_usage *usage;
1135 int i;
1136
1137 /*
1138 * If multiplier_collection is NULL, the multiplier applies
1139 * to all fields in the report.
1140 * Otherwise, it is the Logical Collection the multiplier applies to
1141 * but our field may be in a subcollection of that collection.
1142 */
1143 for (i = 0; i < field->maxusage; i++) {
1144 usage = &field->usage[i];
1145
1146 collection = &hid->collection[usage->collection_index];
1147 while (collection->parent_idx != -1 &&
1148 collection != multiplier_collection)
1149 collection = &hid->collection[collection->parent_idx];
1150
1151 if (collection->parent_idx != -1 ||
1152 multiplier_collection == NULL)
1153 usage->resolution_multiplier = effective_multiplier;
1154
1155 }
1156}
1157
1158static void hid_apply_multiplier(struct hid_device *hid,
1159 struct hid_field *multiplier)
1160{
1161 struct hid_report_enum *rep_enum;
1162 struct hid_report *rep;
1163 struct hid_field *field;
1164 struct hid_collection *multiplier_collection;
1165 int effective_multiplier;
1166 int i;
1167
1168 /*
1169 * "The Resolution Multiplier control must be contained in the same
1170 * Logical Collection as the control(s) to which it is to be applied.
1171 * If no Resolution Multiplier is defined, then the Resolution
1172 * Multiplier defaults to 1. If more than one control exists in a
1173 * Logical Collection, the Resolution Multiplier is associated with
1174 * all controls in the collection. If no Logical Collection is
1175 * defined, the Resolution Multiplier is associated with all
1176 * controls in the report."
1177 * HID Usage Table, v1.12, Section 4.3.1, p30
1178 *
1179 * Thus, search from the current collection upwards until we find a
1180 * logical collection. Then search all fields for that same parent
1181 * collection. Those are the fields the multiplier applies to.
1182 *
1183 * If we have more than one multiplier, it will overwrite the
1184 * applicable fields later.
1185 */
1186 multiplier_collection = &hid->collection[multiplier->usage->collection_index];
1187 while (multiplier_collection->parent_idx != -1 &&
1188 multiplier_collection->type != HID_COLLECTION_LOGICAL)
1189 multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
1190 if (multiplier_collection->type != HID_COLLECTION_LOGICAL)
1191 multiplier_collection = NULL;
1192
1193 effective_multiplier = hid_calculate_multiplier(hid, multiplier);
1194
1195 rep_enum = &hid->report_enum[HID_INPUT_REPORT];
1196 list_for_each_entry(rep, &rep_enum->report_list, list) {
1197 for (i = 0; i < rep->maxfield; i++) {
1198 field = rep->field[i];
1199 hid_apply_multiplier_to_field(hid, field,
1200 multiplier_collection,
1201 effective_multiplier);
1202 }
1203 }
1204}
1205
1206/*
1207 * hid_setup_resolution_multiplier - set up all resolution multipliers
1208 *
1209 * @device: hid device
1210 *
1211 * Search for all Resolution Multiplier Feature Reports and apply their
1212 * value to all matching Input items. This only updates the internal struct
1213 * fields.
1214 *
1215 * The Resolution Multiplier is applied by the hardware. If the multiplier
1216 * is anything other than 1, the hardware will send pre-multiplied events
1217 * so that the same physical interaction generates an accumulated
1218 * accumulated_value = value * * multiplier
1219 * This may be achieved by sending
1220 * - "value * multiplier" for each event, or
1221 * - "value" but "multiplier" times as frequently, or
1222 * - a combination of the above
1223 * The only guarantee is that the same physical interaction always generates
1224 * an accumulated 'value * multiplier'.
1225 *
1226 * This function must be called before any event processing and after
1227 * any SetRequest to the Resolution Multiplier.
1228 */
1229void hid_setup_resolution_multiplier(struct hid_device *hid)
1230{
1231 struct hid_report_enum *rep_enum;
1232 struct hid_report *rep;
1233 struct hid_usage *usage;
1234 int i, j;
1235
1236 rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1237 list_for_each_entry(rep, &rep_enum->report_list, list) {
1238 for (i = 0; i < rep->maxfield; i++) {
1239 /* Ignore if report count is out of bounds. */
1240 if (rep->field[i]->report_count < 1)
1241 continue;
1242
1243 for (j = 0; j < rep->field[i]->maxusage; j++) {
1244 usage = &rep->field[i]->usage[j];
1245 if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
1246 hid_apply_multiplier(hid,
1247 rep->field[i]);
1248 }
1249 }
1250 }
1251}
1252EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
1253
1254static int hid_parse_collections(struct hid_device *device)
1255{
1256 struct hid_item item;
1257 const u8 *start = device->rdesc;
1258 const u8 *end = start + device->rsize;
1259 const u8 *next;
1260 int ret;
1261 static typeof(hid_parser_main) (* const dispatch_type[]) = {
1262 hid_parser_main,
1263 hid_parser_global,
1264 hid_parser_local,
1265 hid_parser_reserved
1266 };
1267
1268 struct hid_parser *parser __free(kvfree) = vzalloc(sizeof(*parser));
1269 if (!parser)
1270 return -ENOMEM;
1271
1272 parser->device = device;
1273
1274 device->collection = kzalloc_objs(*device->collection,
1275 HID_DEFAULT_NUM_COLLECTIONS);
1276 if (!device->collection)
1277 return -ENOMEM;
1278
1279 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1280 for (unsigned int i = 0; i < HID_DEFAULT_NUM_COLLECTIONS; i++)
1281 device->collection[i].parent_idx = -1;
1282
1283 ret = -EINVAL;
1284 if (start == end) {
1285 hid_err(device, "rejecting 0-sized report descriptor\n");
1286 goto out;
1287 }
1288
1289 while ((next = fetch_item(start, end, &item)) != NULL) {
1290 start = next;
1291
1292 if (item.format != HID_ITEM_FORMAT_SHORT) {
1293 hid_err(device, "unexpected long global item\n");
1294 goto out;
1295 }
1296
1297 if (dispatch_type[item.type](parser, &item)) {
1298 hid_err(device, "item %u %u %u %u parsing failed\n",
1299 item.format,
1300 (unsigned int)item.size,
1301 (unsigned int)item.type,
1302 (unsigned int)item.tag);
1303 goto out;
1304 }
1305 }
1306
1307 if (start != end) {
1308 hid_err(device, "item fetching failed at offset %u/%u\n",
1309 device->rsize - (unsigned int)(end - start),
1310 device->rsize);
1311 goto out;
1312 }
1313
1314 if (parser->collection_stack_ptr) {
1315 hid_err(device, "unbalanced collection at end of report description\n");
1316 goto out;
1317 }
1318
1319 if (parser->local.delimiter_depth) {
1320 hid_err(device, "unbalanced delimiter at end of report description\n");
1321 goto out;
1322 }
1323
1324 /*
1325 * fetch initial values in case the device's
1326 * default multiplier isn't the recommended 1
1327 */
1328 hid_setup_resolution_multiplier(device);
1329
1330 device->status |= HID_STAT_PARSED;
1331 ret = 0;
1332
1333out:
1334 kfree(parser->collection_stack);
1335 return ret;
1336}
1337
1338/**
1339 * hid_open_report - open a driver-specific device report
1340 *
1341 * @device: hid device
1342 *
1343 * Parse a report description into a hid_device structure. Reports are
1344 * enumerated, fields are attached to these reports.
1345 * 0 returned on success, otherwise nonzero error value.
1346 *
1347 * This function (or the equivalent hid_parse() macro) should only be
1348 * called from probe() in drivers, before starting the device.
1349 */
1350int hid_open_report(struct hid_device *device)
1351{
1352 unsigned int size;
1353 const u8 *start;
1354 int error;
1355
1356 if (WARN_ON(device->status & HID_STAT_PARSED))
1357 return -EBUSY;
1358
1359 start = device->bpf_rdesc;
1360 if (WARN_ON(!start))
1361 return -ENODEV;
1362 size = device->bpf_rsize;
1363
1364 if (device->driver->report_fixup) {
1365 /*
1366 * device->driver->report_fixup() needs to work
1367 * on a copy of our report descriptor so it can
1368 * change it.
1369 */
1370 u8 *buf __free(kfree) = kmemdup(start, size, GFP_KERNEL);
1371
1372 if (!buf)
1373 return -ENOMEM;
1374
1375 start = device->driver->report_fixup(device, buf, &size);
1376
1377 /*
1378 * The second kmemdup is required in case report_fixup() returns
1379 * a static read-only memory, but we have no idea if that memory
1380 * needs to be cleaned up or not at the end.
1381 */
1382 start = kmemdup(start, size, GFP_KERNEL);
1383 if (!start)
1384 return -ENOMEM;
1385 }
1386
1387 device->rdesc = start;
1388 device->rsize = size;
1389
1390 error = hid_parse_collections(device);
1391 if (error) {
1392 hid_close_report(device);
1393 return error;
1394 }
1395
1396 return 0;
1397}
1398EXPORT_SYMBOL_GPL(hid_open_report);
1399
1400/*
1401 * Extract/implement a data field from/to a little endian report (bit array).
1402 *
1403 * Code sort-of follows HID spec:
1404 * http://www.usb.org/developers/hidpage/HID1_11.pdf
1405 *
1406 * While the USB HID spec allows unlimited length bit fields in "report
1407 * descriptors", most devices never use more than 16 bits.
1408 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1409 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1410 */
1411
1412static u32 __extract(u8 *report, unsigned offset, int n)
1413{
1414 unsigned int idx = offset / 8;
1415 unsigned int bit_nr = 0;
1416 unsigned int bit_shift = offset % 8;
1417 int bits_to_copy = 8 - bit_shift;
1418 u32 value = 0;
1419 u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
1420
1421 while (n > 0) {
1422 value |= ((u32)report[idx] >> bit_shift) << bit_nr;
1423 n -= bits_to_copy;
1424 bit_nr += bits_to_copy;
1425 bits_to_copy = 8;
1426 bit_shift = 0;
1427 idx++;
1428 }
1429
1430 return value & mask;
1431}
1432
1433u32 hid_field_extract(const struct hid_device *hid, u8 *report,
1434 unsigned offset, unsigned n)
1435{
1436 if (n > 32) {
1437 hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
1438 __func__, n, current->comm);
1439 n = 32;
1440 }
1441
1442 return __extract(report, offset, n);
1443}
1444EXPORT_SYMBOL_GPL(hid_field_extract);
1445
1446/*
1447 * "implement" : set bits in a little endian bit stream.
1448 * Same concepts as "extract" (see comments above).
1449 * The data mangled in the bit stream remains in little endian
1450 * order the whole time. It make more sense to talk about
1451 * endianness of register values by considering a register
1452 * a "cached" copy of the little endian bit stream.
1453 */
1454
1455static void __implement(u8 *report, unsigned offset, int n, u32 value)
1456{
1457 unsigned int idx = offset / 8;
1458 unsigned int bit_shift = offset % 8;
1459 int bits_to_set = 8 - bit_shift;
1460
1461 while (n - bits_to_set >= 0) {
1462 report[idx] &= ~(0xff << bit_shift);
1463 report[idx] |= value << bit_shift;
1464 value >>= bits_to_set;
1465 n -= bits_to_set;
1466 bits_to_set = 8;
1467 bit_shift = 0;
1468 idx++;
1469 }
1470
1471 /* last nibble */
1472 if (n) {
1473 u8 bit_mask = ((1U << n) - 1);
1474 report[idx] &= ~(bit_mask << bit_shift);
1475 report[idx] |= value << bit_shift;
1476 }
1477}
1478
1479static void implement(const struct hid_device *hid, u8 *report,
1480 unsigned offset, unsigned n, u32 value)
1481{
1482 if (unlikely(n > 32)) {
1483 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1484 __func__, n, current->comm);
1485 n = 32;
1486 } else if (n < 32) {
1487 u32 m = (1U << n) - 1;
1488
1489 if (unlikely(value > m)) {
1490 hid_warn(hid,
1491 "%s() called with too large value %d (n: %d)! (%s)\n",
1492 __func__, value, n, current->comm);
1493 value &= m;
1494 }
1495 }
1496
1497 __implement(report, offset, n, value);
1498}
1499
1500/*
1501 * Search an array for a value.
1502 */
1503
1504static int search(__s32 *array, __s32 value, unsigned n)
1505{
1506 while (n--) {
1507 if (*array++ == value)
1508 return 0;
1509 }
1510 return -1;
1511}
1512
1513/**
1514 * hid_match_report - check if driver's raw_event should be called
1515 *
1516 * @hid: hid device
1517 * @report: hid report to match against
1518 *
1519 * compare hid->driver->report_table->report_type to report->type
1520 */
1521static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1522{
1523 const struct hid_report_id *id = hid->driver->report_table;
1524
1525 if (!id) /* NULL means all */
1526 return 1;
1527
1528 for (; id->report_type != HID_TERMINATOR; id++)
1529 if (id->report_type == HID_ANY_ID ||
1530 id->report_type == report->type)
1531 return 1;
1532 return 0;
1533}
1534
1535/**
1536 * hid_match_usage - check if driver's event should be called
1537 *
1538 * @hid: hid device
1539 * @usage: usage to match against
1540 *
1541 * compare hid->driver->usage_table->usage_{type,code} to
1542 * usage->usage_{type,code}
1543 */
1544static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1545{
1546 const struct hid_usage_id *id = hid->driver->usage_table;
1547
1548 if (!id) /* NULL means all */
1549 return 1;
1550
1551 for (; id->usage_type != HID_ANY_ID - 1; id++)
1552 if ((id->usage_hid == HID_ANY_ID ||
1553 id->usage_hid == usage->hid) &&
1554 (id->usage_type == HID_ANY_ID ||
1555 id->usage_type == usage->type) &&
1556 (id->usage_code == HID_ANY_ID ||
1557 id->usage_code == usage->code))
1558 return 1;
1559 return 0;
1560}
1561
1562static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1563 struct hid_usage *usage, __s32 value, int interrupt)
1564{
1565 struct hid_driver *hdrv = hid->driver;
1566 int ret;
1567
1568 if (!list_empty(&hid->debug_list))
1569 hid_dump_input(hid, usage, value);
1570
1571 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1572 ret = hdrv->event(hid, field, usage, value);
1573 if (ret != 0) {
1574 if (ret < 0)
1575 hid_err(hid, "%s's event failed with %d\n",
1576 hdrv->name, ret);
1577 return;
1578 }
1579 }
1580
1581 if (hid->claimed & HID_CLAIMED_INPUT)
1582 hidinput_hid_event(hid, field, usage, value);
1583 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1584 hid->hiddev_hid_event(hid, field, usage, value);
1585}
1586
1587/*
1588 * Checks if the given value is valid within this field
1589 */
1590static inline int hid_array_value_is_valid(struct hid_field *field,
1591 __s32 value)
1592{
1593 __s32 min = field->logical_minimum;
1594
1595 /*
1596 * Value needs to be between logical min and max, and
1597 * (value - min) is used as an index in the usage array.
1598 * This array is of size field->maxusage
1599 */
1600 return value >= min &&
1601 value <= field->logical_maximum &&
1602 value - min < field->maxusage;
1603}
1604
1605/*
1606 * Fetch the field from the data. The field content is stored for next
1607 * report processing (we do differential reporting to the layer).
1608 */
1609static void hid_input_fetch_field(struct hid_device *hid,
1610 struct hid_field *field,
1611 __u8 *data)
1612{
1613 unsigned n;
1614 unsigned count = field->report_count;
1615 unsigned offset = field->report_offset;
1616 unsigned size = field->report_size;
1617 __s32 min = field->logical_minimum;
1618 __s32 *value;
1619
1620 value = field->new_value;
1621 memset(value, 0, count * sizeof(__s32));
1622 field->ignored = false;
1623
1624 for (n = 0; n < count; n++) {
1625
1626 value[n] = min < 0 ?
1627 snto32(hid_field_extract(hid, data, offset + n * size,
1628 size), size) :
1629 hid_field_extract(hid, data, offset + n * size, size);
1630
1631 /* Ignore report if ErrorRollOver */
1632 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1633 hid_array_value_is_valid(field, value[n]) &&
1634 field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) {
1635 field->ignored = true;
1636 return;
1637 }
1638 }
1639}
1640
1641/*
1642 * Process a received variable field.
1643 */
1644
1645static void hid_input_var_field(struct hid_device *hid,
1646 struct hid_field *field,
1647 int interrupt)
1648{
1649 unsigned int count = field->report_count;
1650 __s32 *value = field->new_value;
1651 unsigned int n;
1652
1653 for (n = 0; n < count; n++)
1654 hid_process_event(hid,
1655 field,
1656 &field->usage[n],
1657 value[n],
1658 interrupt);
1659
1660 memcpy(field->value, value, count * sizeof(__s32));
1661}
1662
1663/*
1664 * Process a received array field. The field content is stored for
1665 * next report processing (we do differential reporting to the layer).
1666 */
1667
1668static void hid_input_array_field(struct hid_device *hid,
1669 struct hid_field *field,
1670 int interrupt)
1671{
1672 unsigned int n;
1673 unsigned int count = field->report_count;
1674 __s32 min = field->logical_minimum;
1675 __s32 *value;
1676
1677 value = field->new_value;
1678
1679 /* ErrorRollOver */
1680 if (field->ignored)
1681 return;
1682
1683 for (n = 0; n < count; n++) {
1684 if (hid_array_value_is_valid(field, field->value[n]) &&
1685 search(value, field->value[n], count))
1686 hid_process_event(hid,
1687 field,
1688 &field->usage[field->value[n] - min],
1689 0,
1690 interrupt);
1691
1692 if (hid_array_value_is_valid(field, value[n]) &&
1693 search(field->value, value[n], count))
1694 hid_process_event(hid,
1695 field,
1696 &field->usage[value[n] - min],
1697 1,
1698 interrupt);
1699 }
1700
1701 memcpy(field->value, value, count * sizeof(__s32));
1702}
1703
1704/*
1705 * Analyse a received report, and fetch the data from it. The field
1706 * content is stored for next report processing (we do differential
1707 * reporting to the layer).
1708 */
1709static void hid_process_report(struct hid_device *hid,
1710 struct hid_report *report,
1711 __u8 *data,
1712 int interrupt)
1713{
1714 unsigned int a;
1715 struct hid_field_entry *entry;
1716 struct hid_field *field;
1717
1718 /* first retrieve all incoming values in data */
1719 for (a = 0; a < report->maxfield; a++)
1720 hid_input_fetch_field(hid, report->field[a], data);
1721
1722 if (!list_empty(&report->field_entry_list)) {
1723 /* INPUT_REPORT, we have a priority list of fields */
1724 list_for_each_entry(entry,
1725 &report->field_entry_list,
1726 list) {
1727 field = entry->field;
1728
1729 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1730 hid_process_event(hid,
1731 field,
1732 &field->usage[entry->index],
1733 field->new_value[entry->index],
1734 interrupt);
1735 else
1736 hid_input_array_field(hid, field, interrupt);
1737 }
1738
1739 /* we need to do the memcpy at the end for var items */
1740 for (a = 0; a < report->maxfield; a++) {
1741 field = report->field[a];
1742
1743 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1744 memcpy(field->value, field->new_value,
1745 field->report_count * sizeof(__s32));
1746 }
1747 } else {
1748 /* FEATURE_REPORT, regular processing */
1749 for (a = 0; a < report->maxfield; a++) {
1750 field = report->field[a];
1751
1752 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1753 hid_input_var_field(hid, field, interrupt);
1754 else
1755 hid_input_array_field(hid, field, interrupt);
1756 }
1757 }
1758}
1759
1760/*
1761 * Insert a given usage_index in a field in the list
1762 * of processed usages in the report.
1763 *
1764 * The elements of lower priority score are processed
1765 * first.
1766 */
1767static void __hid_insert_field_entry(struct hid_device *hid,
1768 struct hid_report *report,
1769 struct hid_field_entry *entry,
1770 struct hid_field *field,
1771 unsigned int usage_index)
1772{
1773 struct hid_field_entry *next;
1774
1775 entry->field = field;
1776 entry->index = usage_index;
1777 entry->priority = field->usages_priorities[usage_index];
1778
1779 /* insert the element at the correct position */
1780 list_for_each_entry(next,
1781 &report->field_entry_list,
1782 list) {
1783 /*
1784 * the priority of our element is strictly higher
1785 * than the next one, insert it before
1786 */
1787 if (entry->priority > next->priority) {
1788 list_add_tail(&entry->list, &next->list);
1789 return;
1790 }
1791 }
1792
1793 /* lowest priority score: insert at the end */
1794 list_add_tail(&entry->list, &report->field_entry_list);
1795}
1796
1797static void hid_report_process_ordering(struct hid_device *hid,
1798 struct hid_report *report)
1799{
1800 struct hid_field *field;
1801 struct hid_field_entry *entries;
1802 unsigned int a, u, usages;
1803 unsigned int count = 0;
1804
1805 /* count the number of individual fields in the report */
1806 for (a = 0; a < report->maxfield; a++) {
1807 field = report->field[a];
1808
1809 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1810 count += field->report_count;
1811 else
1812 count++;
1813 }
1814
1815 /* allocate the memory to process the fields */
1816 entries = kzalloc_objs(*entries, count);
1817 if (!entries)
1818 return;
1819
1820 report->field_entries = entries;
1821
1822 /*
1823 * walk through all fields in the report and
1824 * store them by priority order in report->field_entry_list
1825 *
1826 * - Var elements are individualized (field + usage_index)
1827 * - Arrays are taken as one, we can not chose an order for them
1828 */
1829 usages = 0;
1830 for (a = 0; a < report->maxfield; a++) {
1831 field = report->field[a];
1832
1833 if (field->flags & HID_MAIN_ITEM_VARIABLE) {
1834 for (u = 0; u < field->report_count; u++) {
1835 __hid_insert_field_entry(hid, report,
1836 &entries[usages],
1837 field, u);
1838 usages++;
1839 }
1840 } else {
1841 __hid_insert_field_entry(hid, report, &entries[usages],
1842 field, 0);
1843 usages++;
1844 }
1845 }
1846}
1847
1848static void hid_process_ordering(struct hid_device *hid)
1849{
1850 struct hid_report *report;
1851 struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT];
1852
1853 list_for_each_entry(report, &report_enum->report_list, list)
1854 hid_report_process_ordering(hid, report);
1855}
1856
1857/*
1858 * Output the field into the report.
1859 */
1860
1861static void hid_output_field(const struct hid_device *hid,
1862 struct hid_field *field, __u8 *data)
1863{
1864 unsigned count = field->report_count;
1865 unsigned offset = field->report_offset;
1866 unsigned size = field->report_size;
1867 unsigned n;
1868
1869 for (n = 0; n < count; n++) {
1870 if (field->logical_minimum < 0) /* signed values */
1871 implement(hid, data, offset + n * size, size,
1872 s32ton(field->value[n], size));
1873 else /* unsigned values */
1874 implement(hid, data, offset + n * size, size,
1875 field->value[n]);
1876 }
1877}
1878
1879/*
1880 * Compute the size of a report.
1881 */
1882static size_t hid_compute_report_size(struct hid_report *report)
1883{
1884 if (report->size)
1885 return ((report->size - 1) >> 3) + 1;
1886
1887 return 0;
1888}
1889
1890/*
1891 * Create a report. 'data' has to be allocated using
1892 * hid_alloc_report_buf() so that it has proper size.
1893 */
1894
1895void hid_output_report(struct hid_report *report, __u8 *data)
1896{
1897 unsigned n;
1898
1899 if (report->id > 0)
1900 *data++ = report->id;
1901
1902 memset(data, 0, hid_compute_report_size(report));
1903 for (n = 0; n < report->maxfield; n++)
1904 hid_output_field(report->device, report->field[n], data);
1905}
1906EXPORT_SYMBOL_GPL(hid_output_report);
1907
1908/*
1909 * Allocator for buffer that is going to be passed to hid_output_report()
1910 */
1911u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1912{
1913 /*
1914 * 7 extra bytes are necessary to achieve proper functionality
1915 * of implement() working on 8 byte chunks
1916 * 1 extra byte for the report ID if it is null (not used) so
1917 * we can reserve that extra byte in the first position of the buffer
1918 * when sending it to .raw_request()
1919 */
1920
1921 u32 len = hid_report_len(report) + 7 + (report->id == 0);
1922
1923 return kzalloc(len, flags);
1924}
1925EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1926
1927/*
1928 * Set a field value. The report this field belongs to has to be
1929 * created and transferred to the device, to set this value in the
1930 * device.
1931 */
1932
1933int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1934{
1935 unsigned size;
1936
1937 if (!field)
1938 return -1;
1939
1940 size = field->report_size;
1941
1942 if (offset >= field->report_count) {
1943 hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1944 offset, field->report_count);
1945 return -1;
1946 }
1947
1948 hid_dump_input(field->report->device, field->usage + offset, value);
1949
1950 if (field->logical_minimum < 0) {
1951 if (value != snto32(s32ton(value, size), size)) {
1952 hid_err(field->report->device, "value %d is out of range\n", value);
1953 return -1;
1954 }
1955 }
1956 field->value[offset] = value;
1957 return 0;
1958}
1959EXPORT_SYMBOL_GPL(hid_set_field);
1960
1961struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
1962 unsigned int application, unsigned int usage)
1963{
1964 struct list_head *report_list = &hdev->report_enum[report_type].report_list;
1965 struct hid_report *report;
1966 int i, j;
1967
1968 list_for_each_entry(report, report_list, list) {
1969 if (report->application != application)
1970 continue;
1971
1972 for (i = 0; i < report->maxfield; i++) {
1973 struct hid_field *field = report->field[i];
1974
1975 for (j = 0; j < field->maxusage; j++) {
1976 if (field->usage[j].hid == usage)
1977 return field;
1978 }
1979 }
1980 }
1981
1982 return NULL;
1983}
1984EXPORT_SYMBOL_GPL(hid_find_field);
1985
1986static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1987 const u8 *data)
1988{
1989 struct hid_report *report;
1990 unsigned int n = 0; /* Normally report number is 0 */
1991
1992 /* Device uses numbered reports, data[0] is report number */
1993 if (report_enum->numbered)
1994 n = *data;
1995
1996 report = report_enum->report_id_hash[n];
1997 if (report == NULL)
1998 dbg_hid("undefined report_id %u received\n", n);
1999
2000 return report;
2001}
2002
2003/*
2004 * Implement a generic .request() callback, using .raw_request()
2005 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
2006 */
2007int __hid_request(struct hid_device *hid, struct hid_report *report,
2008 enum hid_class_request reqtype)
2009{
2010 u8 *data_buf;
2011 int ret;
2012 u32 len;
2013
2014 u8 *buf __free(kfree) = hid_alloc_report_buf(report, GFP_KERNEL);
2015 if (!buf)
2016 return -ENOMEM;
2017
2018 data_buf = buf;
2019 len = hid_report_len(report);
2020
2021 if (report->id == 0) {
2022 /* reserve the first byte for the report ID */
2023 data_buf++;
2024 len++;
2025 }
2026
2027 if (reqtype == HID_REQ_SET_REPORT)
2028 hid_output_report(report, data_buf);
2029
2030 ret = hid_hw_raw_request(hid, report->id, buf, len, report->type, reqtype);
2031 if (ret < 0) {
2032 dbg_hid("unable to complete request: %d\n", ret);
2033 return ret;
2034 }
2035
2036 if (reqtype == HID_REQ_GET_REPORT)
2037 hid_input_report(hid, report->type, buf, ret, 0);
2038
2039 return 0;
2040}
2041EXPORT_SYMBOL_GPL(__hid_request);
2042
2043int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data,
2044 size_t bufsize, u32 size, int interrupt)
2045{
2046 struct hid_report_enum *report_enum = hid->report_enum + type;
2047 struct hid_report *report;
2048 struct hid_driver *hdrv;
2049 int max_buffer_size = HID_MAX_BUFFER_SIZE;
2050 u32 rsize, csize = size;
2051 size_t bsize = bufsize;
2052 u8 *cdata = data;
2053 int ret = 0;
2054
2055 if (report_enum->numbered && (size < 1 || bufsize < 1)) {
2056 hid_warn_ratelimited(hid,
2057 "Event data for numbered report is too short (%d vs %zu)\n",
2058 size, bufsize);
2059 return -EINVAL;
2060 }
2061
2062 report = hid_get_report(report_enum, data);
2063 if (!report)
2064 return 0;
2065
2066 if (unlikely(bsize < csize)) {
2067 hid_warn_ratelimited(hid, "Event data for report %d is incorrect (%d vs %zu)\n",
2068 report->id, csize, bsize);
2069 return -EINVAL;
2070 }
2071
2072 if (report_enum->numbered) {
2073 cdata++;
2074 csize--;
2075 bsize--;
2076 }
2077
2078 rsize = hid_compute_report_size(report);
2079
2080 if (hid->ll_driver->max_buffer_size)
2081 max_buffer_size = hid->ll_driver->max_buffer_size;
2082
2083 if (report_enum->numbered && rsize >= max_buffer_size)
2084 rsize = max_buffer_size - 1;
2085 else if (rsize > max_buffer_size)
2086 rsize = max_buffer_size;
2087
2088 if (bsize < rsize) {
2089 hid_warn_ratelimited(hid, "Event data for report %d was too short (%d vs %zu)\n",
2090 report->id, rsize, bsize);
2091 return -EINVAL;
2092 }
2093
2094 if (csize < rsize) {
2095 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
2096 csize, rsize);
2097 memset(cdata + csize, 0, rsize - csize);
2098 }
2099
2100 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
2101 hid->hiddev_report_event(hid, report);
2102 if (hid->claimed & HID_CLAIMED_HIDRAW) {
2103 ret = hidraw_report_event(hid, data, size);
2104 if (ret)
2105 return ret;
2106 }
2107
2108 if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
2109 hid_process_report(hid, report, cdata, interrupt);
2110 hdrv = hid->driver;
2111 if (hdrv && hdrv->report)
2112 hdrv->report(hid, report);
2113 }
2114
2115 if (hid->claimed & HID_CLAIMED_INPUT)
2116 hidinput_report_event(hid, report);
2117
2118 return ret;
2119}
2120EXPORT_SYMBOL_GPL(hid_report_raw_event);
2121
2122
2123static int __hid_input_report(struct hid_device *hid, enum hid_report_type type,
2124 u8 *data, size_t bufsize, u32 size, int interrupt, u64 source,
2125 bool from_bpf, bool lock_already_taken)
2126{
2127 struct hid_report_enum *report_enum;
2128 struct hid_driver *hdrv;
2129 struct hid_report *report;
2130 int ret = 0;
2131
2132 if (!hid)
2133 return -ENODEV;
2134
2135 ret = down_trylock(&hid->driver_input_lock);
2136 if (lock_already_taken && !ret) {
2137 up(&hid->driver_input_lock);
2138 return -EINVAL;
2139 } else if (!lock_already_taken && ret) {
2140 return -EBUSY;
2141 }
2142
2143 if (!hid->driver) {
2144 ret = -ENODEV;
2145 goto unlock;
2146 }
2147 report_enum = hid->report_enum + type;
2148 hdrv = hid->driver;
2149
2150 data = dispatch_hid_bpf_device_event(hid, type, data, &bufsize, &size, interrupt,
2151 source, from_bpf);
2152 if (IS_ERR(data)) {
2153 ret = PTR_ERR(data);
2154 goto unlock;
2155 }
2156
2157 if (!size) {
2158 dbg_hid("empty report\n");
2159 ret = -1;
2160 goto unlock;
2161 }
2162
2163 /* Avoid unnecessary overhead if debugfs is disabled */
2164 if (!list_empty(&hid->debug_list))
2165 hid_dump_report(hid, type, data, size);
2166
2167 report = hid_get_report(report_enum, data);
2168
2169 if (!report) {
2170 ret = -1;
2171 goto unlock;
2172 }
2173
2174 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
2175 ret = hdrv->raw_event(hid, report, data, size);
2176 if (ret < 0)
2177 goto unlock;
2178 }
2179
2180 ret = hid_report_raw_event(hid, type, data, bufsize, size, interrupt);
2181
2182unlock:
2183 if (!lock_already_taken)
2184 up(&hid->driver_input_lock);
2185 return ret;
2186}
2187
2188/**
2189 * hid_input_report - report data from lower layer (usb, bt...)
2190 *
2191 * @hid: hid device
2192 * @type: HID report type (HID_*_REPORT)
2193 * @data: report contents
2194 * @size: size of data parameter
2195 * @interrupt: distinguish between interrupt and control transfers
2196 *
2197 * This is data entry for lower layers.
2198 * Legacy, please use hid_safe_input_report() instead.
2199 */
2200int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
2201 int interrupt)
2202{
2203 return __hid_input_report(hid, type, data, size, size, interrupt, 0,
2204 false, /* from_bpf */
2205 false /* lock_already_taken */);
2206}
2207EXPORT_SYMBOL_GPL(hid_input_report);
2208
2209/**
2210 * hid_safe_input_report - report data from lower layer (usb, bt...)
2211 *
2212 * @hid: hid device
2213 * @type: HID report type (HID_*_REPORT)
2214 * @data: report contents
2215 * @bufsize: allocated size of the data buffer
2216 * @size: useful size of data parameter
2217 * @interrupt: distinguish between interrupt and control transfers
2218 *
2219 * This is data entry for lower layers.
2220 * Please use this function instead of the non safe version because we provide
2221 * here the size of the buffer, allowing hid-core to make smarter decisions
2222 * regarding the incoming buffer.
2223 */
2224int hid_safe_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data,
2225 size_t bufsize, u32 size, int interrupt)
2226{
2227 return __hid_input_report(hid, type, data, bufsize, size, interrupt, 0,
2228 false, /* from_bpf */
2229 false /* lock_already_taken */);
2230}
2231EXPORT_SYMBOL_GPL(hid_safe_input_report);
2232
2233bool hid_match_one_id(const struct hid_device *hdev,
2234 const struct hid_device_id *id)
2235{
2236 return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
2237 (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
2238 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
2239 (id->product == HID_ANY_ID || id->product == hdev->product);
2240}
2241
2242const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
2243 const struct hid_device_id *id)
2244{
2245 for (; id->bus; id++)
2246 if (hid_match_one_id(hdev, id))
2247 return id;
2248
2249 return NULL;
2250}
2251EXPORT_SYMBOL_GPL(hid_match_id);
2252
2253static const struct hid_device_id hid_hiddev_list[] = {
2254 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
2255 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
2256 { }
2257};
2258
2259static bool hid_hiddev(struct hid_device *hdev)
2260{
2261 return !!hid_match_id(hdev, hid_hiddev_list);
2262}
2263
2264
2265static ssize_t
2266report_descriptor_read(struct file *filp, struct kobject *kobj,
2267 const struct bin_attribute *attr,
2268 char *buf, loff_t off, size_t count)
2269{
2270 struct device *dev = kobj_to_dev(kobj);
2271 struct hid_device *hdev = to_hid_device(dev);
2272
2273 if (off >= hdev->rsize)
2274 return 0;
2275
2276 if (off + count > hdev->rsize)
2277 count = hdev->rsize - off;
2278
2279 memcpy(buf, hdev->rdesc + off, count);
2280
2281 return count;
2282}
2283
2284static ssize_t
2285country_show(struct device *dev, struct device_attribute *attr,
2286 char *buf)
2287{
2288 struct hid_device *hdev = to_hid_device(dev);
2289
2290 return sprintf(buf, "%02x\n", hdev->country & 0xff);
2291}
2292
2293static const BIN_ATTR_RO(report_descriptor, HID_MAX_DESCRIPTOR_SIZE);
2294
2295static const DEVICE_ATTR_RO(country);
2296
2297int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
2298{
2299 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
2300 "Joystick", "Gamepad", "Keyboard", "Keypad",
2301 "Multi-Axis Controller"
2302 };
2303 const char *type, *bus;
2304 char buf[64] = "";
2305 unsigned int i;
2306 int len;
2307 int ret;
2308
2309 ret = hid_bpf_connect_device(hdev);
2310 if (ret)
2311 return ret;
2312
2313 if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
2314 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
2315 if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
2316 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
2317 if (hdev->bus != BUS_USB)
2318 connect_mask &= ~HID_CONNECT_HIDDEV;
2319 if (hid_hiddev(hdev))
2320 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2321
2322 if ((connect_mask & HID_CONNECT_HIDINPUT) &&
2323 !hidinput_connect(hdev, connect_mask))
2324 hdev->claimed |= HID_CLAIMED_INPUT;
2325
2326 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
2327 !hdev->hiddev_connect(hdev,
2328 connect_mask & HID_CONNECT_HIDDEV_FORCE))
2329 hdev->claimed |= HID_CLAIMED_HIDDEV;
2330 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
2331 hdev->claimed |= HID_CLAIMED_HIDRAW;
2332
2333 if (connect_mask & HID_CONNECT_DRIVER)
2334 hdev->claimed |= HID_CLAIMED_DRIVER;
2335
2336 /* Drivers with the ->raw_event callback set are not required to connect
2337 * to any other listener. */
2338 if (!hdev->claimed && !hdev->driver->raw_event) {
2339 hid_err(hdev, "device has no listeners, quitting\n");
2340 return -ENODEV;
2341 }
2342
2343 hid_process_ordering(hdev);
2344
2345 len = 0;
2346 if (hdev->claimed & HID_CLAIMED_INPUT)
2347 len += sprintf(buf + len, "input");
2348 if (hdev->claimed & HID_CLAIMED_HIDDEV)
2349 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
2350 ((struct hiddev *)hdev->hiddev)->minor);
2351 if (hdev->claimed & HID_CLAIMED_HIDRAW)
2352 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
2353 ((struct hidraw *)hdev->hidraw)->minor);
2354
2355 type = "Device";
2356 for (i = 0; i < hdev->maxcollection; i++) {
2357 struct hid_collection *col = &hdev->collection[i];
2358 if (col->type == HID_COLLECTION_APPLICATION &&
2359 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
2360 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
2361 type = types[col->usage & 0xffff];
2362 break;
2363 }
2364 }
2365
2366 switch (hdev->bus) {
2367 case BUS_USB:
2368 bus = "USB";
2369 break;
2370 case BUS_BLUETOOTH:
2371 bus = "BLUETOOTH";
2372 break;
2373 case BUS_I2C:
2374 bus = "I2C";
2375 break;
2376 case BUS_SDW:
2377 bus = "SOUNDWIRE";
2378 break;
2379 case BUS_VIRTUAL:
2380 bus = "VIRTUAL";
2381 break;
2382 case BUS_INTEL_ISHTP:
2383 case BUS_AMD_SFH:
2384 bus = "SENSOR HUB";
2385 break;
2386 default:
2387 bus = "<UNKNOWN>";
2388 }
2389
2390 ret = device_create_file(&hdev->dev, &dev_attr_country);
2391 if (ret)
2392 hid_warn(hdev,
2393 "can't create sysfs country code attribute err: %d\n", ret);
2394
2395 hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
2396 buf, bus, hdev->version >> 8, hdev->version & 0xff,
2397 type, hdev->name, hdev->phys);
2398
2399 return 0;
2400}
2401EXPORT_SYMBOL_GPL(hid_connect);
2402
2403void hid_disconnect(struct hid_device *hdev)
2404{
2405 device_remove_file(&hdev->dev, &dev_attr_country);
2406 if (hdev->claimed & HID_CLAIMED_INPUT)
2407 hidinput_disconnect(hdev);
2408 if (hdev->claimed & HID_CLAIMED_HIDDEV)
2409 hdev->hiddev_disconnect(hdev);
2410 if (hdev->claimed & HID_CLAIMED_HIDRAW)
2411 hidraw_disconnect(hdev);
2412 hdev->claimed = 0;
2413
2414 hid_bpf_disconnect_device(hdev);
2415}
2416EXPORT_SYMBOL_GPL(hid_disconnect);
2417
2418/**
2419 * hid_hw_start - start underlying HW
2420 * @hdev: hid device
2421 * @connect_mask: which outputs to connect, see HID_CONNECT_*
2422 *
2423 * Call this in probe function *after* hid_parse. This will setup HW
2424 * buffers and start the device (if not defeirred to device open).
2425 * hid_hw_stop must be called if this was successful.
2426 */
2427int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
2428{
2429 int error;
2430
2431 error = hdev->ll_driver->start(hdev);
2432 if (error)
2433 return error;
2434
2435 if (connect_mask) {
2436 error = hid_connect(hdev, connect_mask);
2437 if (error) {
2438 hdev->ll_driver->stop(hdev);
2439 return error;
2440 }
2441 }
2442
2443 return 0;
2444}
2445EXPORT_SYMBOL_GPL(hid_hw_start);
2446
2447/**
2448 * hid_hw_stop - stop underlying HW
2449 * @hdev: hid device
2450 *
2451 * This is usually called from remove function or from probe when something
2452 * failed and hid_hw_start was called already.
2453 *
2454 * If the caller enabled HID input via hid_device_io_start() and is unwinding
2455 * without an explicit hid_device_io_stop(), quiesce input first so that
2456 * in-flight reports cannot reach handlers (e.g. hidraw_report_event) whose
2457 * backing objects hid_disconnect() is about to free.
2458 */
2459void hid_hw_stop(struct hid_device *hdev)
2460{
2461 if (hdev->io_started)
2462 hid_device_io_stop(hdev);
2463 hid_disconnect(hdev);
2464 hdev->ll_driver->stop(hdev);
2465}
2466EXPORT_SYMBOL_GPL(hid_hw_stop);
2467
2468/**
2469 * hid_hw_open - signal underlying HW to start delivering events
2470 * @hdev: hid device
2471 *
2472 * Tell underlying HW to start delivering events from the device.
2473 * This function should be called sometime after successful call
2474 * to hid_hw_start().
2475 */
2476int hid_hw_open(struct hid_device *hdev)
2477{
2478 int ret;
2479
2480 ret = mutex_lock_killable(&hdev->ll_open_lock);
2481 if (ret)
2482 return ret;
2483
2484 if (!hdev->ll_open_count++) {
2485 ret = hdev->ll_driver->open(hdev);
2486 if (ret)
2487 hdev->ll_open_count--;
2488
2489 if (hdev->driver->on_hid_hw_open)
2490 hdev->driver->on_hid_hw_open(hdev);
2491 }
2492
2493 mutex_unlock(&hdev->ll_open_lock);
2494 return ret;
2495}
2496EXPORT_SYMBOL_GPL(hid_hw_open);
2497
2498/**
2499 * hid_hw_close - signal underlaying HW to stop delivering events
2500 *
2501 * @hdev: hid device
2502 *
2503 * This function indicates that we are not interested in the events
2504 * from this device anymore. Delivery of events may or may not stop,
2505 * depending on the number of users still outstanding.
2506 */
2507void hid_hw_close(struct hid_device *hdev)
2508{
2509 mutex_lock(&hdev->ll_open_lock);
2510 if (!--hdev->ll_open_count) {
2511 hdev->ll_driver->close(hdev);
2512
2513 if (hdev->driver->on_hid_hw_close)
2514 hdev->driver->on_hid_hw_close(hdev);
2515 }
2516 mutex_unlock(&hdev->ll_open_lock);
2517}
2518EXPORT_SYMBOL_GPL(hid_hw_close);
2519
2520/**
2521 * hid_hw_request - send report request to device
2522 *
2523 * @hdev: hid device
2524 * @report: report to send
2525 * @reqtype: hid request type
2526 */
2527void hid_hw_request(struct hid_device *hdev,
2528 struct hid_report *report, enum hid_class_request reqtype)
2529{
2530 if (hdev->ll_driver->request)
2531 return hdev->ll_driver->request(hdev, report, reqtype);
2532
2533 __hid_request(hdev, report, reqtype);
2534}
2535EXPORT_SYMBOL_GPL(hid_hw_request);
2536
2537int __hid_hw_raw_request(struct hid_device *hdev,
2538 unsigned char reportnum, __u8 *buf,
2539 size_t len, enum hid_report_type rtype,
2540 enum hid_class_request reqtype,
2541 u64 source, bool from_bpf)
2542{
2543 unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2544 int ret;
2545
2546 if (hdev->ll_driver->max_buffer_size)
2547 max_buffer_size = hdev->ll_driver->max_buffer_size;
2548
2549 if (len < 1 || len > max_buffer_size || !buf)
2550 return -EINVAL;
2551
2552 ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype,
2553 reqtype, source, from_bpf);
2554 if (ret)
2555 return ret;
2556
2557 return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
2558 rtype, reqtype);
2559}
2560
2561/**
2562 * hid_hw_raw_request - send report request to device
2563 *
2564 * @hdev: hid device
2565 * @reportnum: report ID
2566 * @buf: in/out data to transfer
2567 * @len: length of buf
2568 * @rtype: HID report type
2569 * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT
2570 *
2571 * Return: count of data transferred, negative if error
2572 *
2573 * Same behavior as hid_hw_request, but with raw buffers instead.
2574 */
2575int hid_hw_raw_request(struct hid_device *hdev,
2576 unsigned char reportnum, __u8 *buf,
2577 size_t len, enum hid_report_type rtype, enum hid_class_request reqtype)
2578{
2579 return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0, false);
2580}
2581EXPORT_SYMBOL_GPL(hid_hw_raw_request);
2582
2583int __hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len, u64 source,
2584 bool from_bpf)
2585{
2586 unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2587 int ret;
2588
2589 if (hdev->ll_driver->max_buffer_size)
2590 max_buffer_size = hdev->ll_driver->max_buffer_size;
2591
2592 if (len < 1 || len > max_buffer_size || !buf)
2593 return -EINVAL;
2594
2595 ret = dispatch_hid_bpf_output_report(hdev, buf, len, source, from_bpf);
2596 if (ret)
2597 return ret;
2598
2599 if (hdev->ll_driver->output_report)
2600 return hdev->ll_driver->output_report(hdev, buf, len);
2601
2602 return -ENOSYS;
2603}
2604
2605/**
2606 * hid_hw_output_report - send output report to device
2607 *
2608 * @hdev: hid device
2609 * @buf: raw data to transfer
2610 * @len: length of buf
2611 *
2612 * Return: count of data transferred, negative if error
2613 */
2614int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len)
2615{
2616 return __hid_hw_output_report(hdev, buf, len, 0, false);
2617}
2618EXPORT_SYMBOL_GPL(hid_hw_output_report);
2619
2620#ifdef CONFIG_PM
2621int hid_driver_suspend(struct hid_device *hdev, pm_message_t state)
2622{
2623 if (hdev->driver && hdev->driver->suspend)
2624 return hdev->driver->suspend(hdev, state);
2625
2626 return 0;
2627}
2628EXPORT_SYMBOL_GPL(hid_driver_suspend);
2629
2630int hid_driver_reset_resume(struct hid_device *hdev)
2631{
2632 if (hdev->driver && hdev->driver->reset_resume)
2633 return hdev->driver->reset_resume(hdev);
2634
2635 return 0;
2636}
2637EXPORT_SYMBOL_GPL(hid_driver_reset_resume);
2638
2639int hid_driver_resume(struct hid_device *hdev)
2640{
2641 if (hdev->driver && hdev->driver->resume)
2642 return hdev->driver->resume(hdev);
2643
2644 return 0;
2645}
2646EXPORT_SYMBOL_GPL(hid_driver_resume);
2647#endif /* CONFIG_PM */
2648
2649struct hid_dynid {
2650 struct list_head list;
2651 struct hid_device_id id;
2652};
2653
2654/**
2655 * new_id_store - add a new HID device ID to this driver and re-probe devices
2656 * @drv: target device driver
2657 * @buf: buffer for scanning device ID data
2658 * @count: input size
2659 *
2660 * Adds a new dynamic hid device ID to this driver,
2661 * and causes the driver to probe for all devices again.
2662 */
2663static ssize_t new_id_store(struct device_driver *drv, const char *buf,
2664 size_t count)
2665{
2666 struct hid_driver *hdrv = to_hid_driver(drv);
2667 struct hid_dynid *dynid;
2668 __u32 bus, vendor, product;
2669 unsigned long driver_data = 0;
2670 int ret;
2671
2672 ret = sscanf(buf, "%x %x %x %lx",
2673 &bus, &vendor, &product, &driver_data);
2674 if (ret < 3)
2675 return -EINVAL;
2676
2677 dynid = kzalloc_obj(*dynid);
2678 if (!dynid)
2679 return -ENOMEM;
2680
2681 dynid->id.bus = bus;
2682 dynid->id.group = HID_GROUP_ANY;
2683 dynid->id.vendor = vendor;
2684 dynid->id.product = product;
2685 dynid->id.driver_data = driver_data;
2686
2687 spin_lock(&hdrv->dyn_lock);
2688 list_add_tail(&dynid->list, &hdrv->dyn_list);
2689 spin_unlock(&hdrv->dyn_lock);
2690
2691 ret = driver_attach(&hdrv->driver);
2692
2693 return ret ? : count;
2694}
2695static DRIVER_ATTR_WO(new_id);
2696
2697static struct attribute *hid_drv_attrs[] = {
2698 &driver_attr_new_id.attr,
2699 NULL,
2700};
2701ATTRIBUTE_GROUPS(hid_drv);
2702
2703static void hid_free_dynids(struct hid_driver *hdrv)
2704{
2705 struct hid_dynid *dynid, *n;
2706
2707 spin_lock(&hdrv->dyn_lock);
2708 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
2709 list_del(&dynid->list);
2710 kfree(dynid);
2711 }
2712 spin_unlock(&hdrv->dyn_lock);
2713}
2714
2715const struct hid_device_id *hid_match_device(struct hid_device *hdev,
2716 struct hid_driver *hdrv)
2717{
2718 struct hid_dynid *dynid;
2719
2720 spin_lock(&hdrv->dyn_lock);
2721 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
2722 if (hid_match_one_id(hdev, &dynid->id)) {
2723 spin_unlock(&hdrv->dyn_lock);
2724 return &dynid->id;
2725 }
2726 }
2727 spin_unlock(&hdrv->dyn_lock);
2728
2729 return hid_match_id(hdev, hdrv->id_table);
2730}
2731EXPORT_SYMBOL_GPL(hid_match_device);
2732
2733static int hid_bus_match(struct device *dev, const struct device_driver *drv)
2734{
2735 struct hid_driver *hdrv = to_hid_driver(drv);
2736 struct hid_device *hdev = to_hid_device(dev);
2737
2738 return hid_match_device(hdev, hdrv) != NULL;
2739}
2740
2741/**
2742 * hid_compare_device_paths - check if both devices share the same path
2743 * @hdev_a: hid device
2744 * @hdev_b: hid device
2745 * @separator: char to use as separator
2746 *
2747 * Check if two devices share the same path up to the last occurrence of
2748 * the separator char. Both paths must exist (i.e., zero-length paths
2749 * don't match).
2750 */
2751bool hid_compare_device_paths(struct hid_device *hdev_a,
2752 struct hid_device *hdev_b, char separator)
2753{
2754 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
2755 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
2756
2757 if (n1 != n2 || n1 <= 0 || n2 <= 0)
2758 return false;
2759
2760 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
2761}
2762EXPORT_SYMBOL_GPL(hid_compare_device_paths);
2763
2764static bool hid_check_device_match(struct hid_device *hdev,
2765 struct hid_driver *hdrv,
2766 const struct hid_device_id **id)
2767{
2768 *id = hid_match_device(hdev, hdrv);
2769 if (!*id)
2770 return false;
2771
2772 if (hdrv->match)
2773 return hdrv->match(hdev, hid_ignore_special_drivers);
2774
2775 /*
2776 * hid-generic implements .match(), so we must be dealing with a
2777 * different HID driver here, and can simply check if
2778 * hid_ignore_special_drivers or HID_QUIRK_IGNORE_SPECIAL_DRIVER
2779 * are set or not.
2780 */
2781 return !hid_ignore_special_drivers && !(hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER);
2782}
2783
2784static void hid_set_group(struct hid_device *hdev)
2785{
2786 int ret;
2787
2788 if (hid_ignore_special_drivers) {
2789 hdev->group = HID_GROUP_GENERIC;
2790 } else if (!hdev->group &&
2791 !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2792 ret = hid_scan_report(hdev);
2793 if (ret)
2794 hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2795 }
2796}
2797
2798static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
2799{
2800 const struct hid_device_id *id;
2801 int ret;
2802
2803 if (!hdev->bpf_rsize) {
2804 /* we keep a reference to the currently scanned report descriptor */
2805 const __u8 *original_rdesc = hdev->bpf_rdesc;
2806
2807 if (!original_rdesc)
2808 original_rdesc = hdev->dev_rdesc;
2809
2810 /* in case a bpf program gets detached, we need to free the old one */
2811 hid_free_bpf_rdesc(hdev);
2812
2813 /* keep this around so we know we called it once */
2814 hdev->bpf_rsize = hdev->dev_rsize;
2815
2816 /* call_hid_bpf_rdesc_fixup will always return a valid pointer */
2817 hdev->bpf_rdesc = call_hid_bpf_rdesc_fixup(hdev, hdev->dev_rdesc,
2818 &hdev->bpf_rsize);
2819
2820 /* the report descriptor changed, we need to re-scan it */
2821 if (original_rdesc != hdev->bpf_rdesc) {
2822 hdev->group = 0;
2823 hid_set_group(hdev);
2824 }
2825 }
2826
2827 if (!hid_check_device_match(hdev, hdrv, &id))
2828 return -ENODEV;
2829
2830 hdev->devres_group_id = devres_open_group(&hdev->dev, NULL, GFP_KERNEL);
2831 if (!hdev->devres_group_id)
2832 return -ENOMEM;
2833
2834 /* reset the quirks that has been previously set */
2835 hdev->quirks = hid_lookup_quirk(hdev);
2836 hdev->driver = hdrv;
2837
2838 if (hdrv->probe) {
2839 ret = hdrv->probe(hdev, id);
2840 } else { /* default probe */
2841 ret = hid_open_report(hdev);
2842 if (!ret)
2843 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2844 }
2845
2846 /*
2847 * Note that we are not closing the devres group opened above so
2848 * even resources that were attached to the device after probe is
2849 * run are released when hid_device_remove() is executed. This is
2850 * needed as some drivers would allocate additional resources,
2851 * for example when updating firmware.
2852 */
2853
2854 if (ret) {
2855 if (hdev->io_started)
2856 hid_device_io_stop(hdev);
2857 devres_release_group(&hdev->dev, hdev->devres_group_id);
2858 hid_close_report(hdev);
2859 hdev->driver = NULL;
2860 }
2861
2862 return ret;
2863}
2864
2865static int hid_device_probe(struct device *dev)
2866{
2867 struct hid_device *hdev = to_hid_device(dev);
2868 struct hid_driver *hdrv = to_hid_driver(dev->driver);
2869 int ret = 0;
2870
2871 if (down_interruptible(&hdev->driver_input_lock))
2872 return -EINTR;
2873
2874 hdev->io_started = false;
2875 clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
2876
2877 if (!hdev->driver)
2878 ret = __hid_device_probe(hdev, hdrv);
2879
2880 if (!hdev->io_started)
2881 up(&hdev->driver_input_lock);
2882
2883 return ret;
2884}
2885
2886static void hid_device_remove(struct device *dev)
2887{
2888 struct hid_device *hdev = to_hid_device(dev);
2889 struct hid_driver *hdrv;
2890
2891 down(&hdev->driver_input_lock);
2892 hdev->io_started = false;
2893
2894 hdrv = hdev->driver;
2895 if (hdrv) {
2896 if (hdrv->remove)
2897 hdrv->remove(hdev);
2898 else /* default remove */
2899 hid_hw_stop(hdev);
2900
2901 /* Release all devres resources allocated by the driver */
2902 devres_release_group(&hdev->dev, hdev->devres_group_id);
2903
2904 hid_close_report(hdev);
2905 hdev->driver = NULL;
2906 }
2907
2908 if (!hdev->io_started)
2909 up(&hdev->driver_input_lock);
2910}
2911
2912static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
2913 char *buf)
2914{
2915 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
2916
2917 return sysfs_emit(buf, "hid:b%04Xg%04Xv%08Xp%08X\n",
2918 hdev->bus, hdev->group, hdev->vendor, hdev->product);
2919}
2920static DEVICE_ATTR_RO(modalias);
2921
2922/*
2923 * Expose this as bustype instead of bus as
2924 * that's the name the input subsystem uses
2925 */
2926static ssize_t bustype_show(struct device *dev, struct device_attribute *a,
2927 char *buf)
2928{
2929 struct hid_device *hdev = to_hid_device(dev);
2930
2931 return sysfs_emit(buf, "%04x\n", hdev->bus);
2932}
2933static DEVICE_ATTR_RO(bustype);
2934
2935#define HID_DEV_ID_ATTR(name) \
2936static ssize_t name##_show(struct device *dev, \
2937 struct device_attribute *attr, \
2938 char *buf) \
2939{ \
2940 struct hid_device *hdev = to_hid_device(dev); \
2941 \
2942 return sysfs_emit(buf, "%04x\n", hdev->name); \
2943} \
2944static DEVICE_ATTR_RO(name)
2945
2946HID_DEV_ID_ATTR(vendor);
2947HID_DEV_ID_ATTR(product);
2948HID_DEV_ID_ATTR(version);
2949
2950static struct attribute *hid_dev_id_attrs[] = {
2951 &dev_attr_bustype.attr,
2952 &dev_attr_vendor.attr,
2953 &dev_attr_product.attr,
2954 &dev_attr_version.attr,
2955 NULL
2956};
2957static const struct attribute_group hid_dev_id_attr_group = {
2958 .name = "id",
2959 .attrs = hid_dev_id_attrs,
2960};
2961static struct attribute *hid_dev_attrs[] = {
2962 &dev_attr_modalias.attr,
2963 NULL,
2964};
2965static const struct bin_attribute *hid_dev_bin_attrs[] = {
2966 &bin_attr_report_descriptor,
2967 NULL
2968};
2969static const struct attribute_group hid_dev_group = {
2970 .attrs = hid_dev_attrs,
2971 .bin_attrs = hid_dev_bin_attrs,
2972};
2973static const struct attribute_group *hid_dev_groups[] = {
2974 &hid_dev_group,
2975 &hid_dev_id_attr_group,
2976 NULL
2977};
2978
2979static int hid_uevent(const struct device *dev, struct kobj_uevent_env *env)
2980{
2981 const struct hid_device *hdev = to_hid_device(dev);
2982
2983 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
2984 hdev->bus, hdev->vendor, hdev->product))
2985 return -ENOMEM;
2986
2987 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
2988 return -ENOMEM;
2989
2990 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
2991 return -ENOMEM;
2992
2993 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2994 return -ENOMEM;
2995
2996 if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2997 hdev->bus, hdev->group, hdev->vendor, hdev->product))
2998 return -ENOMEM;
2999 if (hdev->firmware_version) {
3000 if (add_uevent_var(env, "HID_FIRMWARE_VERSION=0x%04llX",
3001 hdev->firmware_version))
3002 return -ENOMEM;
3003 }
3004
3005 return 0;
3006}
3007
3008const struct bus_type hid_bus_type = {
3009 .name = "hid",
3010 .dev_groups = hid_dev_groups,
3011 .drv_groups = hid_drv_groups,
3012 .match = hid_bus_match,
3013 .probe = hid_device_probe,
3014 .remove = hid_device_remove,
3015 .uevent = hid_uevent,
3016};
3017EXPORT_SYMBOL(hid_bus_type);
3018
3019int hid_add_device(struct hid_device *hdev)
3020{
3021 static atomic_t id = ATOMIC_INIT(0);
3022 int ret;
3023
3024 if (WARN_ON(hdev->status & HID_STAT_ADDED))
3025 return -EBUSY;
3026
3027 hdev->quirks = hid_lookup_quirk(hdev);
3028
3029 /* we need to kill them here, otherwise they will stay allocated to
3030 * wait for coming driver */
3031 if (hid_ignore(hdev))
3032 return -ENODEV;
3033
3034 /*
3035 * Check for the mandatory transport channel.
3036 */
3037 if (!hdev->ll_driver->raw_request) {
3038 hid_err(hdev, "transport driver missing .raw_request()\n");
3039 return -EINVAL;
3040 }
3041
3042 /*
3043 * Read the device report descriptor once and use as template
3044 * for the driver-specific modifications.
3045 */
3046 ret = hdev->ll_driver->parse(hdev);
3047 if (ret)
3048 return ret;
3049 if (!hdev->dev_rdesc)
3050 return -ENODEV;
3051
3052 /*
3053 * Scan generic devices for group information
3054 */
3055 hid_set_group(hdev);
3056
3057 hdev->id = atomic_inc_return(&id);
3058
3059 /* XXX hack, any other cleaner solution after the driver core
3060 * is converted to allow more than 20 bytes as the device name? */
3061 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
3062 hdev->vendor, hdev->product, hdev->id);
3063
3064 hid_debug_register(hdev, dev_name(&hdev->dev));
3065 ret = device_add(&hdev->dev);
3066 if (!ret)
3067 hdev->status |= HID_STAT_ADDED;
3068 else
3069 hid_debug_unregister(hdev);
3070
3071 return ret;
3072}
3073EXPORT_SYMBOL_GPL(hid_add_device);
3074
3075/**
3076 * hid_allocate_device - allocate new hid device descriptor
3077 *
3078 * Allocate and initialize hid device, so that hid_destroy_device might be
3079 * used to free it.
3080 *
3081 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
3082 * error value.
3083 */
3084struct hid_device *hid_allocate_device(void)
3085{
3086 struct hid_device *hdev;
3087 int ret = -ENOMEM;
3088
3089 hdev = kzalloc_obj(*hdev);
3090 if (hdev == NULL)
3091 return ERR_PTR(ret);
3092
3093 device_initialize(&hdev->dev);
3094 hdev->dev.release = hid_device_release;
3095 hdev->dev.bus = &hid_bus_type;
3096 device_enable_async_suspend(&hdev->dev);
3097
3098 hid_close_report(hdev);
3099
3100 init_waitqueue_head(&hdev->debug_wait);
3101 INIT_LIST_HEAD(&hdev->debug_list);
3102 spin_lock_init(&hdev->debug_list_lock);
3103 sema_init(&hdev->driver_input_lock, 1);
3104 mutex_init(&hdev->ll_open_lock);
3105 kref_init(&hdev->ref);
3106
3107#ifdef CONFIG_HID_BATTERY_STRENGTH
3108 INIT_LIST_HEAD(&hdev->batteries);
3109#endif
3110
3111 ret = hid_bpf_device_init(hdev);
3112 if (ret)
3113 goto out_err;
3114
3115 return hdev;
3116
3117out_err:
3118 hid_destroy_device(hdev);
3119 return ERR_PTR(ret);
3120}
3121EXPORT_SYMBOL_GPL(hid_allocate_device);
3122
3123static void hid_remove_device(struct hid_device *hdev)
3124{
3125 if (hdev->status & HID_STAT_ADDED) {
3126 device_del(&hdev->dev);
3127 hid_debug_unregister(hdev);
3128 hdev->status &= ~HID_STAT_ADDED;
3129 }
3130 hid_free_bpf_rdesc(hdev);
3131 kfree(hdev->dev_rdesc);
3132 hdev->dev_rdesc = NULL;
3133 hdev->dev_rsize = 0;
3134 hdev->bpf_rsize = 0;
3135}
3136
3137/**
3138 * hid_destroy_device - free previously allocated device
3139 *
3140 * @hdev: hid device
3141 *
3142 * If you allocate hid_device through hid_allocate_device, you should ever
3143 * free by this function.
3144 */
3145void hid_destroy_device(struct hid_device *hdev)
3146{
3147 hid_bpf_destroy_device(hdev);
3148 hid_remove_device(hdev);
3149 put_device(&hdev->dev);
3150}
3151EXPORT_SYMBOL_GPL(hid_destroy_device);
3152
3153
3154static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
3155{
3156 struct hid_driver *hdrv = data;
3157 struct hid_device *hdev = to_hid_device(dev);
3158
3159 if (hdev->driver == hdrv &&
3160 !hdrv->match(hdev, hid_ignore_special_drivers) &&
3161 !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
3162 return device_reprobe(dev);
3163
3164 return 0;
3165}
3166
3167static int __hid_bus_driver_added(struct device_driver *drv, void *data)
3168{
3169 struct hid_driver *hdrv = to_hid_driver(drv);
3170
3171 if (hdrv->match) {
3172 bus_for_each_dev(&hid_bus_type, NULL, hdrv,
3173 __hid_bus_reprobe_drivers);
3174 }
3175
3176 return 0;
3177}
3178
3179static int __bus_removed_driver(struct device_driver *drv, void *data)
3180{
3181 return bus_rescan_devices(&hid_bus_type);
3182}
3183
3184int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
3185 const char *mod_name)
3186{
3187 int ret;
3188
3189 hdrv->driver.name = hdrv->name;
3190 hdrv->driver.bus = &hid_bus_type;
3191 hdrv->driver.owner = owner;
3192 hdrv->driver.mod_name = mod_name;
3193
3194 INIT_LIST_HEAD(&hdrv->dyn_list);
3195 spin_lock_init(&hdrv->dyn_lock);
3196
3197 ret = driver_register(&hdrv->driver);
3198
3199 if (ret == 0)
3200 bus_for_each_drv(&hid_bus_type, NULL, NULL,
3201 __hid_bus_driver_added);
3202
3203 return ret;
3204}
3205EXPORT_SYMBOL_GPL(__hid_register_driver);
3206
3207void hid_unregister_driver(struct hid_driver *hdrv)
3208{
3209 driver_unregister(&hdrv->driver);
3210 hid_free_dynids(hdrv);
3211
3212 bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
3213}
3214EXPORT_SYMBOL_GPL(hid_unregister_driver);
3215
3216int hid_check_keys_pressed(struct hid_device *hid)
3217{
3218 struct hid_input *hidinput;
3219 int i;
3220
3221 if (!(hid->claimed & HID_CLAIMED_INPUT))
3222 return 0;
3223
3224 list_for_each_entry(hidinput, &hid->inputs, list) {
3225 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
3226 if (hidinput->input->key[i])
3227 return 1;
3228 }
3229
3230 return 0;
3231}
3232EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
3233
3234#ifdef CONFIG_HID_BPF
3235static const struct hid_ops __hid_ops = {
3236 .hid_get_report = hid_get_report,
3237 .hid_hw_raw_request = __hid_hw_raw_request,
3238 .hid_hw_output_report = __hid_hw_output_report,
3239 .hid_input_report = __hid_input_report,
3240 .owner = THIS_MODULE,
3241 .bus_type = &hid_bus_type,
3242};
3243#endif
3244
3245static int __init hid_init(void)
3246{
3247 int ret;
3248
3249 ret = bus_register(&hid_bus_type);
3250 if (ret) {
3251 pr_err("can't register hid bus\n");
3252 goto err;
3253 }
3254
3255#ifdef CONFIG_HID_BPF
3256 hid_ops = &__hid_ops;
3257#endif
3258
3259 ret = hidraw_init();
3260 if (ret)
3261 goto err_bus;
3262
3263 hid_debug_init();
3264
3265 return 0;
3266err_bus:
3267 bus_unregister(&hid_bus_type);
3268err:
3269 return ret;
3270}
3271
3272static void __exit hid_exit(void)
3273{
3274#ifdef CONFIG_HID_BPF
3275 hid_ops = NULL;
3276#endif
3277 hid_debug_exit();
3278 hidraw_exit();
3279 bus_unregister(&hid_bus_type);
3280 hid_quirks_exit(HID_BUS_ANY);
3281}
3282
3283module_init(hid_init);
3284module_exit(hid_exit);
3285
3286MODULE_AUTHOR("Andreas Gal");
3287MODULE_AUTHOR("Vojtech Pavlik");
3288MODULE_AUTHOR("Jiri Kosina");
3289MODULE_DESCRIPTION("HID support for Linux");
3290MODULE_LICENSE("GPL");