1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID support for Linux
5 * Copyright (c) 1999 Andreas Gal
6 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8 * Copyright (c) 2006-2012 Jiri Kosina
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
22 #include <linux/spinlock.h>
23 #include <asm/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 #include <linux/async.h>
32 #include <linux/hid.h>
33 #include <linux/hiddev.h>
34 #include <linux/hid-debug.h>
35 #include <linux/hidraw.h>
43 #define DRIVER_DESC "HID core driver"
46 module_param_named(debug, hid_debug, int, 0600);
47 MODULE_PARM_DESC(debug, "toggle HID debugging messages");
48 EXPORT_SYMBOL_GPL(hid_debug);
50 static int hid_ignore_special_drivers = 0;
51 module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
52 MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
55 * Register a new report for a device.
58 struct hid_report *hid_register_report(struct hid_device *device,
59 unsigned int type, unsigned int id,
60 unsigned int application)
62 struct hid_report_enum *report_enum = device->report_enum + type;
63 struct hid_report *report;
65 if (id >= HID_MAX_IDS)
67 if (report_enum->report_id_hash[id])
68 return report_enum->report_id_hash[id];
70 report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
75 report_enum->numbered = 1;
80 report->device = device;
81 report->application = application;
82 report_enum->report_id_hash[id] = report;
84 list_add_tail(&report->list, &report_enum->report_list);
88 EXPORT_SYMBOL_GPL(hid_register_report);
91 * Register a new field for this report.
94 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
96 struct hid_field *field;
98 if (report->maxfield == HID_MAX_FIELDS) {
99 hid_err(report->device, "too many fields in report\n");
103 field = kzalloc((sizeof(struct hid_field) +
104 usages * sizeof(struct hid_usage) +
105 values * sizeof(unsigned)), GFP_KERNEL);
109 field->index = report->maxfield++;
110 report->field[field->index] = field;
111 field->usage = (struct hid_usage *)(field + 1);
112 field->value = (s32 *)(field->usage + usages);
113 field->report = report;
119 * Open a collection. The type/usage is pushed on the stack.
122 static int open_collection(struct hid_parser *parser, unsigned type)
124 struct hid_collection *collection;
126 int collection_index;
128 usage = parser->local.usage[0];
130 if (parser->collection_stack_ptr == parser->collection_stack_size) {
131 unsigned int *collection_stack;
132 unsigned int new_size = parser->collection_stack_size +
133 HID_COLLECTION_STACK_SIZE;
135 collection_stack = krealloc(parser->collection_stack,
136 new_size * sizeof(unsigned int),
138 if (!collection_stack)
141 parser->collection_stack = collection_stack;
142 parser->collection_stack_size = new_size;
145 if (parser->device->maxcollection == parser->device->collection_size) {
146 collection = kmalloc(
147 array3_size(sizeof(struct hid_collection),
148 parser->device->collection_size,
151 if (collection == NULL) {
152 hid_err(parser->device, "failed to reallocate collection array\n");
155 memcpy(collection, parser->device->collection,
156 sizeof(struct hid_collection) *
157 parser->device->collection_size);
158 memset(collection + parser->device->collection_size, 0,
159 sizeof(struct hid_collection) *
160 parser->device->collection_size);
161 kfree(parser->device->collection);
162 parser->device->collection = collection;
163 parser->device->collection_size *= 2;
166 parser->collection_stack[parser->collection_stack_ptr++] =
167 parser->device->maxcollection;
169 collection_index = parser->device->maxcollection++;
170 collection = parser->device->collection + collection_index;
171 collection->type = type;
172 collection->usage = usage;
173 collection->level = parser->collection_stack_ptr - 1;
174 collection->parent_idx = (collection->level == 0) ? -1 :
175 parser->collection_stack[collection->level - 1];
177 if (type == HID_COLLECTION_APPLICATION)
178 parser->device->maxapplication++;
184 * Close a collection.
187 static int close_collection(struct hid_parser *parser)
189 if (!parser->collection_stack_ptr) {
190 hid_err(parser->device, "collection stack underflow\n");
193 parser->collection_stack_ptr--;
198 * Climb up the stack, search for the specified collection type
199 * and return the usage.
202 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
204 struct hid_collection *collection = parser->device->collection;
207 for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
208 unsigned index = parser->collection_stack[n];
209 if (collection[index].type == type)
210 return collection[index].usage;
212 return 0; /* we know nothing about this usage type */
216 * Add a usage to the temporary parser table.
219 static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
221 if (parser->local.usage_index >= HID_MAX_USAGES) {
222 hid_err(parser->device, "usage index exceeded\n");
225 parser->local.usage[parser->local.usage_index] = usage;
226 parser->local.usage_size[parser->local.usage_index] = size;
227 parser->local.collection_index[parser->local.usage_index] =
228 parser->collection_stack_ptr ?
229 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
230 parser->local.usage_index++;
235 * Register a new field for this report.
238 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
240 struct hid_report *report;
241 struct hid_field *field;
245 unsigned int application;
247 application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
249 report = hid_register_report(parser->device, report_type,
250 parser->global.report_id, application);
252 hid_err(parser->device, "hid_register_report failed\n");
256 /* Handle both signed and unsigned cases properly */
257 if ((parser->global.logical_minimum < 0 &&
258 parser->global.logical_maximum <
259 parser->global.logical_minimum) ||
260 (parser->global.logical_minimum >= 0 &&
261 (__u32)parser->global.logical_maximum <
262 (__u32)parser->global.logical_minimum)) {
263 dbg_hid("logical range invalid 0x%x 0x%x\n",
264 parser->global.logical_minimum,
265 parser->global.logical_maximum);
269 offset = report->size;
270 report->size += parser->global.report_size * parser->global.report_count;
272 if (!parser->local.usage_index) /* Ignore padding fields */
275 usages = max_t(unsigned, parser->local.usage_index,
276 parser->global.report_count);
278 field = hid_register_field(report, usages, parser->global.report_count);
282 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
283 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
284 field->application = application;
286 for (i = 0; i < usages; i++) {
288 /* Duplicate the last usage we parsed if we have excess values */
289 if (i >= parser->local.usage_index)
290 j = parser->local.usage_index - 1;
291 field->usage[i].hid = parser->local.usage[j];
292 field->usage[i].collection_index =
293 parser->local.collection_index[j];
294 field->usage[i].usage_index = i;
295 field->usage[i].resolution_multiplier = 1;
298 field->maxusage = usages;
299 field->flags = flags;
300 field->report_offset = offset;
301 field->report_type = report_type;
302 field->report_size = parser->global.report_size;
303 field->report_count = parser->global.report_count;
304 field->logical_minimum = parser->global.logical_minimum;
305 field->logical_maximum = parser->global.logical_maximum;
306 field->physical_minimum = parser->global.physical_minimum;
307 field->physical_maximum = parser->global.physical_maximum;
308 field->unit_exponent = parser->global.unit_exponent;
309 field->unit = parser->global.unit;
315 * Read data value from item.
318 static u32 item_udata(struct hid_item *item)
320 switch (item->size) {
321 case 1: return item->data.u8;
322 case 2: return item->data.u16;
323 case 4: return item->data.u32;
328 static s32 item_sdata(struct hid_item *item)
330 switch (item->size) {
331 case 1: return item->data.s8;
332 case 2: return item->data.s16;
333 case 4: return item->data.s32;
339 * Process a global item.
342 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
346 case HID_GLOBAL_ITEM_TAG_PUSH:
348 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
349 hid_err(parser->device, "global environment stack overflow\n");
353 memcpy(parser->global_stack + parser->global_stack_ptr++,
354 &parser->global, sizeof(struct hid_global));
357 case HID_GLOBAL_ITEM_TAG_POP:
359 if (!parser->global_stack_ptr) {
360 hid_err(parser->device, "global environment stack underflow\n");
364 memcpy(&parser->global, parser->global_stack +
365 --parser->global_stack_ptr, sizeof(struct hid_global));
368 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
369 parser->global.usage_page = item_udata(item);
372 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
373 parser->global.logical_minimum = item_sdata(item);
376 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
377 if (parser->global.logical_minimum < 0)
378 parser->global.logical_maximum = item_sdata(item);
380 parser->global.logical_maximum = item_udata(item);
383 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
384 parser->global.physical_minimum = item_sdata(item);
387 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
388 if (parser->global.physical_minimum < 0)
389 parser->global.physical_maximum = item_sdata(item);
391 parser->global.physical_maximum = item_udata(item);
394 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
395 /* Many devices provide unit exponent as a two's complement
396 * nibble due to the common misunderstanding of HID
397 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
398 * both this and the standard encoding. */
399 raw_value = item_sdata(item);
400 if (!(raw_value & 0xfffffff0))
401 parser->global.unit_exponent = hid_snto32(raw_value, 4);
403 parser->global.unit_exponent = raw_value;
406 case HID_GLOBAL_ITEM_TAG_UNIT:
407 parser->global.unit = item_udata(item);
410 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
411 parser->global.report_size = item_udata(item);
412 if (parser->global.report_size > 256) {
413 hid_err(parser->device, "invalid report_size %d\n",
414 parser->global.report_size);
419 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
420 parser->global.report_count = item_udata(item);
421 if (parser->global.report_count > HID_MAX_USAGES) {
422 hid_err(parser->device, "invalid report_count %d\n",
423 parser->global.report_count);
428 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
429 parser->global.report_id = item_udata(item);
430 if (parser->global.report_id == 0 ||
431 parser->global.report_id >= HID_MAX_IDS) {
432 hid_err(parser->device, "report_id %u is invalid\n",
433 parser->global.report_id);
439 hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
445 * Process a local item.
448 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
454 data = item_udata(item);
457 case HID_LOCAL_ITEM_TAG_DELIMITER:
461 * We treat items before the first delimiter
462 * as global to all usage sets (branch 0).
463 * In the moment we process only these global
464 * items and the first delimiter set.
466 if (parser->local.delimiter_depth != 0) {
467 hid_err(parser->device, "nested delimiters\n");
470 parser->local.delimiter_depth++;
471 parser->local.delimiter_branch++;
473 if (parser->local.delimiter_depth < 1) {
474 hid_err(parser->device, "bogus close delimiter\n");
477 parser->local.delimiter_depth--;
481 case HID_LOCAL_ITEM_TAG_USAGE:
483 if (parser->local.delimiter_branch > 1) {
484 dbg_hid("alternative usage ignored\n");
488 return hid_add_usage(parser, data, item->size);
490 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
492 if (parser->local.delimiter_branch > 1) {
493 dbg_hid("alternative usage ignored\n");
497 parser->local.usage_minimum = data;
500 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
502 if (parser->local.delimiter_branch > 1) {
503 dbg_hid("alternative usage ignored\n");
507 count = data - parser->local.usage_minimum;
508 if (count + parser->local.usage_index >= HID_MAX_USAGES) {
510 * We do not warn if the name is not set, we are
511 * actually pre-scanning the device.
513 if (dev_name(&parser->device->dev))
514 hid_warn(parser->device,
515 "ignoring exceeding usage max\n");
516 data = HID_MAX_USAGES - parser->local.usage_index +
517 parser->local.usage_minimum - 1;
519 hid_err(parser->device,
520 "no more usage index available\n");
525 for (n = parser->local.usage_minimum; n <= data; n++)
526 if (hid_add_usage(parser, n, item->size)) {
527 dbg_hid("hid_add_usage failed\n");
534 dbg_hid("unknown local item tag 0x%x\n", item->tag);
541 * Concatenate Usage Pages into Usages where relevant:
542 * As per specification, 6.2.2.8: "When the parser encounters a main item it
543 * concatenates the last declared Usage Page with a Usage to form a complete
547 static void hid_concatenate_usage_page(struct hid_parser *parser)
551 for (i = 0; i < parser->local.usage_index; i++)
552 if (parser->local.usage_size[i] <= 2)
553 parser->local.usage[i] += parser->global.usage_page << 16;
557 * Process a main item.
560 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
565 hid_concatenate_usage_page(parser);
567 data = item_udata(item);
570 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
571 ret = open_collection(parser, data & 0xff);
573 case HID_MAIN_ITEM_TAG_END_COLLECTION:
574 ret = close_collection(parser);
576 case HID_MAIN_ITEM_TAG_INPUT:
577 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
579 case HID_MAIN_ITEM_TAG_OUTPUT:
580 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
582 case HID_MAIN_ITEM_TAG_FEATURE:
583 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
586 hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
590 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
596 * Process a reserved item.
599 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
601 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
606 * Free a report and all registered fields. The field->usage and
607 * field->value table's are allocated behind the field, so we need
608 * only to free(field) itself.
611 static void hid_free_report(struct hid_report *report)
615 for (n = 0; n < report->maxfield; n++)
616 kfree(report->field[n]);
621 * Close report. This function returns the device
622 * state to the point prior to hid_open_report().
624 static void hid_close_report(struct hid_device *device)
628 for (i = 0; i < HID_REPORT_TYPES; i++) {
629 struct hid_report_enum *report_enum = device->report_enum + i;
631 for (j = 0; j < HID_MAX_IDS; j++) {
632 struct hid_report *report = report_enum->report_id_hash[j];
634 hid_free_report(report);
636 memset(report_enum, 0, sizeof(*report_enum));
637 INIT_LIST_HEAD(&report_enum->report_list);
640 kfree(device->rdesc);
641 device->rdesc = NULL;
644 kfree(device->collection);
645 device->collection = NULL;
646 device->collection_size = 0;
647 device->maxcollection = 0;
648 device->maxapplication = 0;
650 device->status &= ~HID_STAT_PARSED;
654 * Free a device structure, all reports, and all fields.
657 static void hid_device_release(struct device *dev)
659 struct hid_device *hid = to_hid_device(dev);
661 hid_close_report(hid);
662 kfree(hid->dev_rdesc);
667 * Fetch a report description item from the data stream. We support long
668 * items, though they are not used yet.
671 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
675 if ((end - start) <= 0)
680 item->type = (b >> 2) & 3;
681 item->tag = (b >> 4) & 15;
683 if (item->tag == HID_ITEM_TAG_LONG) {
685 item->format = HID_ITEM_FORMAT_LONG;
687 if ((end - start) < 2)
690 item->size = *start++;
691 item->tag = *start++;
693 if ((end - start) < item->size)
696 item->data.longdata = start;
701 item->format = HID_ITEM_FORMAT_SHORT;
704 switch (item->size) {
709 if ((end - start) < 1)
711 item->data.u8 = *start++;
715 if ((end - start) < 2)
717 item->data.u16 = get_unaligned_le16(start);
718 start = (__u8 *)((__le16 *)start + 1);
723 if ((end - start) < 4)
725 item->data.u32 = get_unaligned_le32(start);
726 start = (__u8 *)((__le32 *)start + 1);
733 static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
735 struct hid_device *hid = parser->device;
737 if (usage == HID_DG_CONTACTID)
738 hid->group = HID_GROUP_MULTITOUCH;
741 static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
743 if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
744 parser->global.report_size == 8)
745 parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
748 static void hid_scan_collection(struct hid_parser *parser, unsigned type)
750 struct hid_device *hid = parser->device;
753 if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
754 type == HID_COLLECTION_PHYSICAL)
755 hid->group = HID_GROUP_SENSOR_HUB;
757 if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
758 hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
759 hid->group == HID_GROUP_MULTITOUCH)
760 hid->group = HID_GROUP_GENERIC;
762 if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
763 for (i = 0; i < parser->local.usage_index; i++)
764 if (parser->local.usage[i] == HID_GD_POINTER)
765 parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
767 if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
768 parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
771 static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
776 hid_concatenate_usage_page(parser);
778 data = item_udata(item);
781 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
782 hid_scan_collection(parser, data & 0xff);
784 case HID_MAIN_ITEM_TAG_END_COLLECTION:
786 case HID_MAIN_ITEM_TAG_INPUT:
787 /* ignore constant inputs, they will be ignored by hid-input */
788 if (data & HID_MAIN_ITEM_CONSTANT)
790 for (i = 0; i < parser->local.usage_index; i++)
791 hid_scan_input_usage(parser, parser->local.usage[i]);
793 case HID_MAIN_ITEM_TAG_OUTPUT:
795 case HID_MAIN_ITEM_TAG_FEATURE:
796 for (i = 0; i < parser->local.usage_index; i++)
797 hid_scan_feature_usage(parser, parser->local.usage[i]);
801 /* Reset the local parser environment */
802 memset(&parser->local, 0, sizeof(parser->local));
808 * Scan a report descriptor before the device is added to the bus.
809 * Sets device groups and other properties that determine what driver
812 static int hid_scan_report(struct hid_device *hid)
814 struct hid_parser *parser;
815 struct hid_item item;
816 __u8 *start = hid->dev_rdesc;
817 __u8 *end = start + hid->dev_rsize;
818 static int (*dispatch_type[])(struct hid_parser *parser,
819 struct hid_item *item) = {
826 parser = vzalloc(sizeof(struct hid_parser));
830 parser->device = hid;
831 hid->group = HID_GROUP_GENERIC;
834 * The parsing is simpler than the one in hid_open_report() as we should
835 * be robust against hid errors. Those errors will be raised by
836 * hid_open_report() anyway.
838 while ((start = fetch_item(start, end, &item)) != NULL)
839 dispatch_type[item.type](parser, &item);
842 * Handle special flags set during scanning.
844 if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
845 (hid->group == HID_GROUP_MULTITOUCH))
846 hid->group = HID_GROUP_MULTITOUCH_WIN_8;
849 * Vendor specific handlings
851 switch (hid->vendor) {
852 case USB_VENDOR_ID_WACOM:
853 hid->group = HID_GROUP_WACOM;
855 case USB_VENDOR_ID_SYNAPTICS:
856 if (hid->group == HID_GROUP_GENERIC)
857 if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
858 && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
860 * hid-rmi should take care of them,
863 hid->group = HID_GROUP_RMI;
867 kfree(parser->collection_stack);
873 * hid_parse_report - parse device report
875 * @device: hid device
876 * @start: report start
879 * Allocate the device report as read by the bus driver. This function should
880 * only be called from parse() in ll drivers.
882 int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
884 hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
887 hid->dev_rsize = size;
890 EXPORT_SYMBOL_GPL(hid_parse_report);
892 static const char * const hid_report_names[] = {
895 "HID_FEATURE_REPORT",
898 * hid_validate_values - validate existing device report's value indexes
900 * @device: hid device
901 * @type: which report type to examine
902 * @id: which report ID to examine (0 for first)
903 * @field_index: which report field to examine
904 * @report_counts: expected number of values
906 * Validate the number of values in a given field of a given report, after
909 struct hid_report *hid_validate_values(struct hid_device *hid,
910 unsigned int type, unsigned int id,
911 unsigned int field_index,
912 unsigned int report_counts)
914 struct hid_report *report;
916 if (type > HID_FEATURE_REPORT) {
917 hid_err(hid, "invalid HID report type %u\n", type);
921 if (id >= HID_MAX_IDS) {
922 hid_err(hid, "invalid HID report id %u\n", id);
927 * Explicitly not using hid_get_report() here since it depends on
928 * ->numbered being checked, which may not always be the case when
929 * drivers go to access report values.
933 * Validating on id 0 means we should examine the first
934 * report in the list.
937 hid->report_enum[type].report_list.next,
938 struct hid_report, list);
940 report = hid->report_enum[type].report_id_hash[id];
943 hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
946 if (report->maxfield <= field_index) {
947 hid_err(hid, "not enough fields in %s %u\n",
948 hid_report_names[type], id);
951 if (report->field[field_index]->report_count < report_counts) {
952 hid_err(hid, "not enough values in %s %u field %u\n",
953 hid_report_names[type], id, field_index);
958 EXPORT_SYMBOL_GPL(hid_validate_values);
960 static int hid_calculate_multiplier(struct hid_device *hid,
961 struct hid_field *multiplier)
964 __s32 v = *multiplier->value;
965 __s32 lmin = multiplier->logical_minimum;
966 __s32 lmax = multiplier->logical_maximum;
967 __s32 pmin = multiplier->physical_minimum;
968 __s32 pmax = multiplier->physical_maximum;
971 * "Because OS implementations will generally divide the control's
972 * reported count by the Effective Resolution Multiplier, designers
973 * should take care not to establish a potential Effective
974 * Resolution Multiplier of zero."
975 * HID Usage Table, v1.12, Section 4.3.1, p31
977 if (lmax - lmin == 0)
980 * Handling the unit exponent is left as an exercise to whoever
981 * finds a device where that exponent is not 0.
983 m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
984 if (unlikely(multiplier->unit_exponent != 0)) {
986 "unsupported Resolution Multiplier unit exponent %d\n",
987 multiplier->unit_exponent);
990 /* There are no devices with an effective multiplier > 255 */
991 if (unlikely(m == 0 || m > 255 || m < -255)) {
992 hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
999 static void hid_apply_multiplier_to_field(struct hid_device *hid,
1000 struct hid_field *field,
1001 struct hid_collection *multiplier_collection,
1002 int effective_multiplier)
1004 struct hid_collection *collection;
1005 struct hid_usage *usage;
1009 * If multiplier_collection is NULL, the multiplier applies
1010 * to all fields in the report.
1011 * Otherwise, it is the Logical Collection the multiplier applies to
1012 * but our field may be in a subcollection of that collection.
1014 for (i = 0; i < field->maxusage; i++) {
1015 usage = &field->usage[i];
1017 collection = &hid->collection[usage->collection_index];
1018 while (collection->parent_idx != -1 &&
1019 collection != multiplier_collection)
1020 collection = &hid->collection[collection->parent_idx];
1022 if (collection->parent_idx != -1 ||
1023 multiplier_collection == NULL)
1024 usage->resolution_multiplier = effective_multiplier;
1029 static void hid_apply_multiplier(struct hid_device *hid,
1030 struct hid_field *multiplier)
1032 struct hid_report_enum *rep_enum;
1033 struct hid_report *rep;
1034 struct hid_field *field;
1035 struct hid_collection *multiplier_collection;
1036 int effective_multiplier;
1040 * "The Resolution Multiplier control must be contained in the same
1041 * Logical Collection as the control(s) to which it is to be applied.
1042 * If no Resolution Multiplier is defined, then the Resolution
1043 * Multiplier defaults to 1. If more than one control exists in a
1044 * Logical Collection, the Resolution Multiplier is associated with
1045 * all controls in the collection. If no Logical Collection is
1046 * defined, the Resolution Multiplier is associated with all
1047 * controls in the report."
1048 * HID Usage Table, v1.12, Section 4.3.1, p30
1050 * Thus, search from the current collection upwards until we find a
1051 * logical collection. Then search all fields for that same parent
1052 * collection. Those are the fields the multiplier applies to.
1054 * If we have more than one multiplier, it will overwrite the
1055 * applicable fields later.
1057 multiplier_collection = &hid->collection[multiplier->usage->collection_index];
1058 while (multiplier_collection->parent_idx != -1 &&
1059 multiplier_collection->type != HID_COLLECTION_LOGICAL)
1060 multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
1062 effective_multiplier = hid_calculate_multiplier(hid, multiplier);
1064 rep_enum = &hid->report_enum[HID_INPUT_REPORT];
1065 list_for_each_entry(rep, &rep_enum->report_list, list) {
1066 for (i = 0; i < rep->maxfield; i++) {
1067 field = rep->field[i];
1068 hid_apply_multiplier_to_field(hid, field,
1069 multiplier_collection,
1070 effective_multiplier);
1076 * hid_setup_resolution_multiplier - set up all resolution multipliers
1078 * @device: hid device
1080 * Search for all Resolution Multiplier Feature Reports and apply their
1081 * value to all matching Input items. This only updates the internal struct
1084 * The Resolution Multiplier is applied by the hardware. If the multiplier
1085 * is anything other than 1, the hardware will send pre-multiplied events
1086 * so that the same physical interaction generates an accumulated
1087 * accumulated_value = value * * multiplier
1088 * This may be achieved by sending
1089 * - "value * multiplier" for each event, or
1090 * - "value" but "multiplier" times as frequently, or
1091 * - a combination of the above
1092 * The only guarantee is that the same physical interaction always generates
1093 * an accumulated 'value * multiplier'.
1095 * This function must be called before any event processing and after
1096 * any SetRequest to the Resolution Multiplier.
1098 void hid_setup_resolution_multiplier(struct hid_device *hid)
1100 struct hid_report_enum *rep_enum;
1101 struct hid_report *rep;
1102 struct hid_usage *usage;
1105 rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1106 list_for_each_entry(rep, &rep_enum->report_list, list) {
1107 for (i = 0; i < rep->maxfield; i++) {
1108 /* Ignore if report count is out of bounds. */
1109 if (rep->field[i]->report_count < 1)
1112 for (j = 0; j < rep->field[i]->maxusage; j++) {
1113 usage = &rep->field[i]->usage[j];
1114 if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
1115 hid_apply_multiplier(hid,
1121 EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
1124 * hid_open_report - open a driver-specific device report
1126 * @device: hid device
1128 * Parse a report description into a hid_device structure. Reports are
1129 * enumerated, fields are attached to these reports.
1130 * 0 returned on success, otherwise nonzero error value.
1132 * This function (or the equivalent hid_parse() macro) should only be
1133 * called from probe() in drivers, before starting the device.
1135 int hid_open_report(struct hid_device *device)
1137 struct hid_parser *parser;
1138 struct hid_item item;
1144 static int (*dispatch_type[])(struct hid_parser *parser,
1145 struct hid_item *item) = {
1152 if (WARN_ON(device->status & HID_STAT_PARSED))
1155 start = device->dev_rdesc;
1156 if (WARN_ON(!start))
1158 size = device->dev_rsize;
1160 buf = kmemdup(start, size, GFP_KERNEL);
1164 if (device->driver->report_fixup)
1165 start = device->driver->report_fixup(device, buf, &size);
1169 start = kmemdup(start, size, GFP_KERNEL);
1174 device->rdesc = start;
1175 device->rsize = size;
1177 parser = vzalloc(sizeof(struct hid_parser));
1183 parser->device = device;
1187 device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1188 sizeof(struct hid_collection), GFP_KERNEL);
1189 if (!device->collection) {
1193 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1196 while ((start = fetch_item(start, end, &item)) != NULL) {
1198 if (item.format != HID_ITEM_FORMAT_SHORT) {
1199 hid_err(device, "unexpected long global item\n");
1203 if (dispatch_type[item.type](parser, &item)) {
1204 hid_err(device, "item %u %u %u %u parsing failed\n",
1205 item.format, (unsigned)item.size,
1206 (unsigned)item.type, (unsigned)item.tag);
1211 if (parser->collection_stack_ptr) {
1212 hid_err(device, "unbalanced collection at end of report description\n");
1215 if (parser->local.delimiter_depth) {
1216 hid_err(device, "unbalanced delimiter at end of report description\n");
1221 * fetch initial values in case the device's
1222 * default multiplier isn't the recommended 1
1224 hid_setup_resolution_multiplier(device);
1226 kfree(parser->collection_stack);
1228 device->status |= HID_STAT_PARSED;
1234 hid_err(device, "item fetching failed at offset %d\n", (int)(end - start));
1236 kfree(parser->collection_stack);
1239 hid_close_report(device);
1242 EXPORT_SYMBOL_GPL(hid_open_report);
1245 * Convert a signed n-bit integer to signed 32-bit integer. Common
1246 * cases are done through the compiler, the screwed things has to be
1250 static s32 snto32(__u32 value, unsigned n)
1253 case 8: return ((__s8)value);
1254 case 16: return ((__s16)value);
1255 case 32: return ((__s32)value);
1257 return value & (1 << (n - 1)) ? value | (~0U << n) : value;
1260 s32 hid_snto32(__u32 value, unsigned n)
1262 return snto32(value, n);
1264 EXPORT_SYMBOL_GPL(hid_snto32);
1267 * Convert a signed 32-bit integer to a signed n-bit integer.
1270 static u32 s32ton(__s32 value, unsigned n)
1272 s32 a = value >> (n - 1);
1274 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
1275 return value & ((1 << n) - 1);
1279 * Extract/implement a data field from/to a little endian report (bit array).
1281 * Code sort-of follows HID spec:
1282 * http://www.usb.org/developers/hidpage/HID1_11.pdf
1284 * While the USB HID spec allows unlimited length bit fields in "report
1285 * descriptors", most devices never use more than 16 bits.
1286 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
1287 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
1290 static u32 __extract(u8 *report, unsigned offset, int n)
1292 unsigned int idx = offset / 8;
1293 unsigned int bit_nr = 0;
1294 unsigned int bit_shift = offset % 8;
1295 int bits_to_copy = 8 - bit_shift;
1297 u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
1300 value |= ((u32)report[idx] >> bit_shift) << bit_nr;
1302 bit_nr += bits_to_copy;
1308 return value & mask;
1311 u32 hid_field_extract(const struct hid_device *hid, u8 *report,
1312 unsigned offset, unsigned n)
1315 hid_warn(hid, "hid_field_extract() called with n (%d) > 256! (%s)\n",
1320 return __extract(report, offset, n);
1322 EXPORT_SYMBOL_GPL(hid_field_extract);
1325 * "implement" : set bits in a little endian bit stream.
1326 * Same concepts as "extract" (see comments above).
1327 * The data mangled in the bit stream remains in little endian
1328 * order the whole time. It make more sense to talk about
1329 * endianness of register values by considering a register
1330 * a "cached" copy of the little endian bit stream.
1333 static void __implement(u8 *report, unsigned offset, int n, u32 value)
1335 unsigned int idx = offset / 8;
1336 unsigned int bit_shift = offset % 8;
1337 int bits_to_set = 8 - bit_shift;
1339 while (n - bits_to_set >= 0) {
1340 report[idx] &= ~(0xff << bit_shift);
1341 report[idx] |= value << bit_shift;
1342 value >>= bits_to_set;
1351 u8 bit_mask = ((1U << n) - 1);
1352 report[idx] &= ~(bit_mask << bit_shift);
1353 report[idx] |= value << bit_shift;
1357 static void implement(const struct hid_device *hid, u8 *report,
1358 unsigned offset, unsigned n, u32 value)
1360 if (unlikely(n > 32)) {
1361 hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
1362 __func__, n, current->comm);
1364 } else if (n < 32) {
1365 u32 m = (1U << n) - 1;
1367 if (unlikely(value > m)) {
1369 "%s() called with too large value %d (n: %d)! (%s)\n",
1370 __func__, value, n, current->comm);
1376 __implement(report, offset, n, value);
1380 * Search an array for a value.
1383 static int search(__s32 *array, __s32 value, unsigned n)
1386 if (*array++ == value)
1393 * hid_match_report - check if driver's raw_event should be called
1396 * @report_type: type to match against
1398 * compare hid->driver->report_table->report_type to report->type
1400 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
1402 const struct hid_report_id *id = hid->driver->report_table;
1404 if (!id) /* NULL means all */
1407 for (; id->report_type != HID_TERMINATOR; id++)
1408 if (id->report_type == HID_ANY_ID ||
1409 id->report_type == report->type)
1415 * hid_match_usage - check if driver's event should be called
1418 * @usage: usage to match against
1420 * compare hid->driver->usage_table->usage_{type,code} to
1421 * usage->usage_{type,code}
1423 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
1425 const struct hid_usage_id *id = hid->driver->usage_table;
1427 if (!id) /* NULL means all */
1430 for (; id->usage_type != HID_ANY_ID - 1; id++)
1431 if ((id->usage_hid == HID_ANY_ID ||
1432 id->usage_hid == usage->hid) &&
1433 (id->usage_type == HID_ANY_ID ||
1434 id->usage_type == usage->type) &&
1435 (id->usage_code == HID_ANY_ID ||
1436 id->usage_code == usage->code))
1441 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
1442 struct hid_usage *usage, __s32 value, int interrupt)
1444 struct hid_driver *hdrv = hid->driver;
1447 if (!list_empty(&hid->debug_list))
1448 hid_dump_input(hid, usage, value);
1450 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
1451 ret = hdrv->event(hid, field, usage, value);
1454 hid_err(hid, "%s's event failed with %d\n",
1460 if (hid->claimed & HID_CLAIMED_INPUT)
1461 hidinput_hid_event(hid, field, usage, value);
1462 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
1463 hid->hiddev_hid_event(hid, field, usage, value);
1467 * Analyse a received field, and fetch the data from it. The field
1468 * content is stored for next report processing (we do differential
1469 * reporting to the layer).
1472 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
1473 __u8 *data, int interrupt)
1476 unsigned count = field->report_count;
1477 unsigned offset = field->report_offset;
1478 unsigned size = field->report_size;
1479 __s32 min = field->logical_minimum;
1480 __s32 max = field->logical_maximum;
1483 value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
1487 for (n = 0; n < count; n++) {
1489 value[n] = min < 0 ?
1490 snto32(hid_field_extract(hid, data, offset + n * size,
1492 hid_field_extract(hid, data, offset + n * size, size);
1494 /* Ignore report if ErrorRollOver */
1495 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1496 value[n] >= min && value[n] <= max &&
1497 value[n] - min < field->maxusage &&
1498 field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1502 for (n = 0; n < count; n++) {
1504 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1505 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1509 if (field->value[n] >= min && field->value[n] <= max
1510 && field->value[n] - min < field->maxusage
1511 && field->usage[field->value[n] - min].hid
1512 && search(value, field->value[n], count))
1513 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1515 if (value[n] >= min && value[n] <= max
1516 && value[n] - min < field->maxusage
1517 && field->usage[value[n] - min].hid
1518 && search(field->value, value[n], count))
1519 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1522 memcpy(field->value, value, count * sizeof(__s32));
1528 * Output the field into the report.
1531 static void hid_output_field(const struct hid_device *hid,
1532 struct hid_field *field, __u8 *data)
1534 unsigned count = field->report_count;
1535 unsigned offset = field->report_offset;
1536 unsigned size = field->report_size;
1539 for (n = 0; n < count; n++) {
1540 if (field->logical_minimum < 0) /* signed values */
1541 implement(hid, data, offset + n * size, size,
1542 s32ton(field->value[n], size));
1543 else /* unsigned values */
1544 implement(hid, data, offset + n * size, size,
1550 * Create a report. 'data' has to be allocated using
1551 * hid_alloc_report_buf() so that it has proper size.
1554 void hid_output_report(struct hid_report *report, __u8 *data)
1559 *data++ = report->id;
1561 memset(data, 0, ((report->size - 1) >> 3) + 1);
1562 for (n = 0; n < report->maxfield; n++)
1563 hid_output_field(report->device, report->field[n], data);
1565 EXPORT_SYMBOL_GPL(hid_output_report);
1568 * Allocator for buffer that is going to be passed to hid_output_report()
1570 u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1573 * 7 extra bytes are necessary to achieve proper functionality
1574 * of implement() working on 8 byte chunks
1577 u32 len = hid_report_len(report) + 7;
1579 return kmalloc(len, flags);
1581 EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1584 * Set a field value. The report this field belongs to has to be
1585 * created and transferred to the device, to set this value in the
1589 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1596 size = field->report_size;
1598 hid_dump_input(field->report->device, field->usage + offset, value);
1600 if (offset >= field->report_count) {
1601 hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
1602 offset, field->report_count);
1605 if (field->logical_minimum < 0) {
1606 if (value != snto32(s32ton(value, size), size)) {
1607 hid_err(field->report->device, "value %d is out of range\n", value);
1611 field->value[offset] = value;
1614 EXPORT_SYMBOL_GPL(hid_set_field);
1616 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1619 struct hid_report *report;
1620 unsigned int n = 0; /* Normally report number is 0 */
1622 /* Device uses numbered reports, data[0] is report number */
1623 if (report_enum->numbered)
1626 report = report_enum->report_id_hash[n];
1628 dbg_hid("undefined report_id %u received\n", n);
1634 * Implement a generic .request() callback, using .raw_request()
1635 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
1637 int __hid_request(struct hid_device *hid, struct hid_report *report,
1644 buf = hid_alloc_report_buf(report, GFP_KERNEL);
1648 len = hid_report_len(report);
1650 if (reqtype == HID_REQ_SET_REPORT)
1651 hid_output_report(report, buf);
1653 ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
1654 report->type, reqtype);
1656 dbg_hid("unable to complete request: %d\n", ret);
1660 if (reqtype == HID_REQ_GET_REPORT)
1661 hid_input_report(hid, report->type, buf, ret, 0);
1669 EXPORT_SYMBOL_GPL(__hid_request);
1671 int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
1674 struct hid_report_enum *report_enum = hid->report_enum + type;
1675 struct hid_report *report;
1676 struct hid_driver *hdrv;
1678 u32 rsize, csize = size;
1682 report = hid_get_report(report_enum, data);
1686 if (report_enum->numbered) {
1691 rsize = ((report->size - 1) >> 3) + 1;
1693 if (rsize > HID_MAX_BUFFER_SIZE)
1694 rsize = HID_MAX_BUFFER_SIZE;
1696 if (csize < rsize) {
1697 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1699 memset(cdata + csize, 0, rsize - csize);
1702 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1703 hid->hiddev_report_event(hid, report);
1704 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1705 ret = hidraw_report_event(hid, data, size);
1710 if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
1711 for (a = 0; a < report->maxfield; a++)
1712 hid_input_field(hid, report->field[a], cdata, interrupt);
1714 if (hdrv && hdrv->report)
1715 hdrv->report(hid, report);
1718 if (hid->claimed & HID_CLAIMED_INPUT)
1719 hidinput_report_event(hid, report);
1723 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1726 * hid_input_report - report data from lower layer (usb, bt...)
1729 * @type: HID report type (HID_*_REPORT)
1730 * @data: report contents
1731 * @size: size of data parameter
1732 * @interrupt: distinguish between interrupt and control transfers
1734 * This is data entry for lower layers.
1736 int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
1738 struct hid_report_enum *report_enum;
1739 struct hid_driver *hdrv;
1740 struct hid_report *report;
1746 if (down_trylock(&hid->driver_input_lock))
1753 report_enum = hid->report_enum + type;
1757 dbg_hid("empty report\n");
1762 /* Avoid unnecessary overhead if debugfs is disabled */
1763 if (!list_empty(&hid->debug_list))
1764 hid_dump_report(hid, type, data, size);
1766 report = hid_get_report(report_enum, data);
1773 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1774 ret = hdrv->raw_event(hid, report, data, size);
1779 ret = hid_report_raw_event(hid, type, data, size, interrupt);
1782 up(&hid->driver_input_lock);
1785 EXPORT_SYMBOL_GPL(hid_input_report);
1787 bool hid_match_one_id(const struct hid_device *hdev,
1788 const struct hid_device_id *id)
1790 return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
1791 (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
1792 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1793 (id->product == HID_ANY_ID || id->product == hdev->product);
1796 const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
1797 const struct hid_device_id *id)
1799 for (; id->bus; id++)
1800 if (hid_match_one_id(hdev, id))
1806 static const struct hid_device_id hid_hiddev_list[] = {
1807 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1808 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1812 static bool hid_hiddev(struct hid_device *hdev)
1814 return !!hid_match_id(hdev, hid_hiddev_list);
1819 read_report_descriptor(struct file *filp, struct kobject *kobj,
1820 struct bin_attribute *attr,
1821 char *buf, loff_t off, size_t count)
1823 struct device *dev = kobj_to_dev(kobj);
1824 struct hid_device *hdev = to_hid_device(dev);
1826 if (off >= hdev->rsize)
1829 if (off + count > hdev->rsize)
1830 count = hdev->rsize - off;
1832 memcpy(buf, hdev->rdesc + off, count);
1838 show_country(struct device *dev, struct device_attribute *attr,
1841 struct hid_device *hdev = to_hid_device(dev);
1843 return sprintf(buf, "%02x\n", hdev->country & 0xff);
1846 static struct bin_attribute dev_bin_attr_report_desc = {
1847 .attr = { .name = "report_descriptor", .mode = 0444 },
1848 .read = read_report_descriptor,
1849 .size = HID_MAX_DESCRIPTOR_SIZE,
1852 static const struct device_attribute dev_attr_country = {
1853 .attr = { .name = "country", .mode = 0444 },
1854 .show = show_country,
1857 int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1859 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1860 "Joystick", "Gamepad", "Keyboard", "Keypad",
1861 "Multi-Axis Controller"
1863 const char *type, *bus;
1869 if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
1870 connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
1871 if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
1872 connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
1873 if (hdev->bus != BUS_USB)
1874 connect_mask &= ~HID_CONNECT_HIDDEV;
1875 if (hid_hiddev(hdev))
1876 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1878 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1879 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1880 hdev->claimed |= HID_CLAIMED_INPUT;
1882 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1883 !hdev->hiddev_connect(hdev,
1884 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1885 hdev->claimed |= HID_CLAIMED_HIDDEV;
1886 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1887 hdev->claimed |= HID_CLAIMED_HIDRAW;
1889 if (connect_mask & HID_CONNECT_DRIVER)
1890 hdev->claimed |= HID_CLAIMED_DRIVER;
1892 /* Drivers with the ->raw_event callback set are not required to connect
1893 * to any other listener. */
1894 if (!hdev->claimed && !hdev->driver->raw_event) {
1895 hid_err(hdev, "device has no listeners, quitting\n");
1899 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1900 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1901 hdev->ff_init(hdev);
1904 if (hdev->claimed & HID_CLAIMED_INPUT)
1905 len += sprintf(buf + len, "input");
1906 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1907 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1908 ((struct hiddev *)hdev->hiddev)->minor);
1909 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1910 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1911 ((struct hidraw *)hdev->hidraw)->minor);
1914 for (i = 0; i < hdev->maxcollection; i++) {
1915 struct hid_collection *col = &hdev->collection[i];
1916 if (col->type == HID_COLLECTION_APPLICATION &&
1917 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1918 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1919 type = types[col->usage & 0xffff];
1924 switch (hdev->bus) {
1938 ret = device_create_file(&hdev->dev, &dev_attr_country);
1941 "can't create sysfs country code attribute err: %d\n", ret);
1943 hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1944 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1945 type, hdev->name, hdev->phys);
1949 EXPORT_SYMBOL_GPL(hid_connect);
1951 void hid_disconnect(struct hid_device *hdev)
1953 device_remove_file(&hdev->dev, &dev_attr_country);
1954 if (hdev->claimed & HID_CLAIMED_INPUT)
1955 hidinput_disconnect(hdev);
1956 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1957 hdev->hiddev_disconnect(hdev);
1958 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1959 hidraw_disconnect(hdev);
1962 EXPORT_SYMBOL_GPL(hid_disconnect);
1965 * hid_hw_start - start underlying HW
1967 * @connect_mask: which outputs to connect, see HID_CONNECT_*
1969 * Call this in probe function *after* hid_parse. This will setup HW
1970 * buffers and start the device (if not defeirred to device open).
1971 * hid_hw_stop must be called if this was successful.
1973 int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1977 error = hdev->ll_driver->start(hdev);
1982 error = hid_connect(hdev, connect_mask);
1984 hdev->ll_driver->stop(hdev);
1991 EXPORT_SYMBOL_GPL(hid_hw_start);
1994 * hid_hw_stop - stop underlying HW
1997 * This is usually called from remove function or from probe when something
1998 * failed and hid_hw_start was called already.
2000 void hid_hw_stop(struct hid_device *hdev)
2002 hid_disconnect(hdev);
2003 hdev->ll_driver->stop(hdev);
2005 EXPORT_SYMBOL_GPL(hid_hw_stop);
2008 * hid_hw_open - signal underlying HW to start delivering events
2011 * Tell underlying HW to start delivering events from the device.
2012 * This function should be called sometime after successful call
2013 * to hid_hw_start().
2015 int hid_hw_open(struct hid_device *hdev)
2019 ret = mutex_lock_killable(&hdev->ll_open_lock);
2023 if (!hdev->ll_open_count++) {
2024 ret = hdev->ll_driver->open(hdev);
2026 hdev->ll_open_count--;
2029 mutex_unlock(&hdev->ll_open_lock);
2032 EXPORT_SYMBOL_GPL(hid_hw_open);
2035 * hid_hw_close - signal underlaying HW to stop delivering events
2039 * This function indicates that we are not interested in the events
2040 * from this device anymore. Delivery of events may or may not stop,
2041 * depending on the number of users still outstanding.
2043 void hid_hw_close(struct hid_device *hdev)
2045 mutex_lock(&hdev->ll_open_lock);
2046 if (!--hdev->ll_open_count)
2047 hdev->ll_driver->close(hdev);
2048 mutex_unlock(&hdev->ll_open_lock);
2050 EXPORT_SYMBOL_GPL(hid_hw_close);
2053 struct list_head list;
2054 struct hid_device_id id;
2058 * store_new_id - add a new HID device ID to this driver and re-probe devices
2059 * @driver: target device driver
2060 * @buf: buffer for scanning device ID data
2061 * @count: input size
2063 * Adds a new dynamic hid device ID to this driver,
2064 * and causes the driver to probe for all devices again.
2066 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
2069 struct hid_driver *hdrv = to_hid_driver(drv);
2070 struct hid_dynid *dynid;
2071 __u32 bus, vendor, product;
2072 unsigned long driver_data = 0;
2075 ret = sscanf(buf, "%x %x %x %lx",
2076 &bus, &vendor, &product, &driver_data);
2080 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
2084 dynid->id.bus = bus;
2085 dynid->id.group = HID_GROUP_ANY;
2086 dynid->id.vendor = vendor;
2087 dynid->id.product = product;
2088 dynid->id.driver_data = driver_data;
2090 spin_lock(&hdrv->dyn_lock);
2091 list_add_tail(&dynid->list, &hdrv->dyn_list);
2092 spin_unlock(&hdrv->dyn_lock);
2094 ret = driver_attach(&hdrv->driver);
2096 return ret ? : count;
2098 static DRIVER_ATTR_WO(new_id);
2100 static struct attribute *hid_drv_attrs[] = {
2101 &driver_attr_new_id.attr,
2104 ATTRIBUTE_GROUPS(hid_drv);
2106 static void hid_free_dynids(struct hid_driver *hdrv)
2108 struct hid_dynid *dynid, *n;
2110 spin_lock(&hdrv->dyn_lock);
2111 list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
2112 list_del(&dynid->list);
2115 spin_unlock(&hdrv->dyn_lock);
2118 const struct hid_device_id *hid_match_device(struct hid_device *hdev,
2119 struct hid_driver *hdrv)
2121 struct hid_dynid *dynid;
2123 spin_lock(&hdrv->dyn_lock);
2124 list_for_each_entry(dynid, &hdrv->dyn_list, list) {
2125 if (hid_match_one_id(hdev, &dynid->id)) {
2126 spin_unlock(&hdrv->dyn_lock);
2130 spin_unlock(&hdrv->dyn_lock);
2132 return hid_match_id(hdev, hdrv->id_table);
2134 EXPORT_SYMBOL_GPL(hid_match_device);
2136 static int hid_bus_match(struct device *dev, struct device_driver *drv)
2138 struct hid_driver *hdrv = to_hid_driver(drv);
2139 struct hid_device *hdev = to_hid_device(dev);
2141 return hid_match_device(hdev, hdrv) != NULL;
2145 * hid_compare_device_paths - check if both devices share the same path
2146 * @hdev_a: hid device
2147 * @hdev_b: hid device
2148 * @separator: char to use as separator
2150 * Check if two devices share the same path up to the last occurrence of
2151 * the separator char. Both paths must exist (i.e., zero-length paths
2154 bool hid_compare_device_paths(struct hid_device *hdev_a,
2155 struct hid_device *hdev_b, char separator)
2157 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
2158 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
2160 if (n1 != n2 || n1 <= 0 || n2 <= 0)
2163 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
2165 EXPORT_SYMBOL_GPL(hid_compare_device_paths);
2167 static int hid_device_probe(struct device *dev)
2169 struct hid_driver *hdrv = to_hid_driver(dev->driver);
2170 struct hid_device *hdev = to_hid_device(dev);
2171 const struct hid_device_id *id;
2174 if (down_interruptible(&hdev->driver_input_lock)) {
2178 hdev->io_started = false;
2180 clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
2182 if (!hdev->driver) {
2183 id = hid_match_device(hdev, hdrv);
2190 if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
2196 * hid-generic implements .match(), so if
2197 * hid_ignore_special_drivers is set, we can safely
2200 if (hid_ignore_special_drivers) {
2206 /* reset the quirks that has been previously set */
2207 hdev->quirks = hid_lookup_quirk(hdev);
2208 hdev->driver = hdrv;
2210 ret = hdrv->probe(hdev, id);
2211 } else { /* default probe */
2212 ret = hid_open_report(hdev);
2214 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2217 hid_close_report(hdev);
2218 hdev->driver = NULL;
2222 if (!hdev->io_started)
2223 up(&hdev->driver_input_lock);
2228 static int hid_device_remove(struct device *dev)
2230 struct hid_device *hdev = to_hid_device(dev);
2231 struct hid_driver *hdrv;
2234 if (down_interruptible(&hdev->driver_input_lock)) {
2238 hdev->io_started = false;
2240 hdrv = hdev->driver;
2244 else /* default remove */
2246 hid_close_report(hdev);
2247 hdev->driver = NULL;
2250 if (!hdev->io_started)
2251 up(&hdev->driver_input_lock);
2256 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
2259 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
2261 return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
2262 hdev->bus, hdev->group, hdev->vendor, hdev->product);
2264 static DEVICE_ATTR_RO(modalias);
2266 static struct attribute *hid_dev_attrs[] = {
2267 &dev_attr_modalias.attr,
2270 static struct bin_attribute *hid_dev_bin_attrs[] = {
2271 &dev_bin_attr_report_desc,
2274 static const struct attribute_group hid_dev_group = {
2275 .attrs = hid_dev_attrs,
2276 .bin_attrs = hid_dev_bin_attrs,
2278 __ATTRIBUTE_GROUPS(hid_dev);
2280 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
2282 struct hid_device *hdev = to_hid_device(dev);
2284 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
2285 hdev->bus, hdev->vendor, hdev->product))
2288 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
2291 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
2294 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
2297 if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
2298 hdev->bus, hdev->group, hdev->vendor, hdev->product))
2304 struct bus_type hid_bus_type = {
2306 .dev_groups = hid_dev_groups,
2307 .drv_groups = hid_drv_groups,
2308 .match = hid_bus_match,
2309 .probe = hid_device_probe,
2310 .remove = hid_device_remove,
2311 .uevent = hid_uevent,
2313 EXPORT_SYMBOL(hid_bus_type);
2315 int hid_add_device(struct hid_device *hdev)
2317 static atomic_t id = ATOMIC_INIT(0);
2320 if (WARN_ON(hdev->status & HID_STAT_ADDED))
2323 hdev->quirks = hid_lookup_quirk(hdev);
2325 /* we need to kill them here, otherwise they will stay allocated to
2326 * wait for coming driver */
2327 if (hid_ignore(hdev))
2331 * Check for the mandatory transport channel.
2333 if (!hdev->ll_driver->raw_request) {
2334 hid_err(hdev, "transport driver missing .raw_request()\n");
2339 * Read the device report descriptor once and use as template
2340 * for the driver-specific modifications.
2342 ret = hdev->ll_driver->parse(hdev);
2345 if (!hdev->dev_rdesc)
2349 * Scan generic devices for group information
2351 if (hid_ignore_special_drivers) {
2352 hdev->group = HID_GROUP_GENERIC;
2353 } else if (!hdev->group &&
2354 !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
2355 ret = hid_scan_report(hdev);
2357 hid_warn(hdev, "bad device descriptor (%d)\n", ret);
2360 /* XXX hack, any other cleaner solution after the driver core
2361 * is converted to allow more than 20 bytes as the device name? */
2362 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
2363 hdev->vendor, hdev->product, atomic_inc_return(&id));
2366 * Try loading the module for the device before the add, so that we do
2367 * not first have hid-generic binding only to have it replaced
2368 * immediately afterwards with a specialized driver.
2370 if (!current_is_async())
2371 request_module("hid:b%04Xg%04Xv%08Xp%08X", hdev->bus,
2372 hdev->group, hdev->vendor, hdev->product);
2374 hid_debug_register(hdev, dev_name(&hdev->dev));
2375 ret = device_add(&hdev->dev);
2377 hdev->status |= HID_STAT_ADDED;
2379 hid_debug_unregister(hdev);
2383 EXPORT_SYMBOL_GPL(hid_add_device);
2386 * hid_allocate_device - allocate new hid device descriptor
2388 * Allocate and initialize hid device, so that hid_destroy_device might be
2391 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
2394 struct hid_device *hid_allocate_device(void)
2396 struct hid_device *hdev;
2399 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
2401 return ERR_PTR(ret);
2403 device_initialize(&hdev->dev);
2404 hdev->dev.release = hid_device_release;
2405 hdev->dev.bus = &hid_bus_type;
2406 device_enable_async_suspend(&hdev->dev);
2408 hid_close_report(hdev);
2410 init_waitqueue_head(&hdev->debug_wait);
2411 INIT_LIST_HEAD(&hdev->debug_list);
2412 spin_lock_init(&hdev->debug_list_lock);
2413 sema_init(&hdev->driver_input_lock, 1);
2414 mutex_init(&hdev->ll_open_lock);
2418 EXPORT_SYMBOL_GPL(hid_allocate_device);
2420 static void hid_remove_device(struct hid_device *hdev)
2422 if (hdev->status & HID_STAT_ADDED) {
2423 device_del(&hdev->dev);
2424 hid_debug_unregister(hdev);
2425 hdev->status &= ~HID_STAT_ADDED;
2427 kfree(hdev->dev_rdesc);
2428 hdev->dev_rdesc = NULL;
2429 hdev->dev_rsize = 0;
2433 * hid_destroy_device - free previously allocated device
2437 * If you allocate hid_device through hid_allocate_device, you should ever
2438 * free by this function.
2440 void hid_destroy_device(struct hid_device *hdev)
2442 hid_remove_device(hdev);
2443 put_device(&hdev->dev);
2445 EXPORT_SYMBOL_GPL(hid_destroy_device);
2448 static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
2450 struct hid_driver *hdrv = data;
2451 struct hid_device *hdev = to_hid_device(dev);
2453 if (hdev->driver == hdrv &&
2454 !hdrv->match(hdev, hid_ignore_special_drivers) &&
2455 !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
2456 return device_reprobe(dev);
2461 static int __hid_bus_driver_added(struct device_driver *drv, void *data)
2463 struct hid_driver *hdrv = to_hid_driver(drv);
2466 bus_for_each_dev(&hid_bus_type, NULL, hdrv,
2467 __hid_bus_reprobe_drivers);
2473 static int __bus_removed_driver(struct device_driver *drv, void *data)
2475 return bus_rescan_devices(&hid_bus_type);
2478 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
2479 const char *mod_name)
2483 hdrv->driver.name = hdrv->name;
2484 hdrv->driver.bus = &hid_bus_type;
2485 hdrv->driver.owner = owner;
2486 hdrv->driver.mod_name = mod_name;
2488 INIT_LIST_HEAD(&hdrv->dyn_list);
2489 spin_lock_init(&hdrv->dyn_lock);
2491 ret = driver_register(&hdrv->driver);
2494 bus_for_each_drv(&hid_bus_type, NULL, NULL,
2495 __hid_bus_driver_added);
2499 EXPORT_SYMBOL_GPL(__hid_register_driver);
2501 void hid_unregister_driver(struct hid_driver *hdrv)
2503 driver_unregister(&hdrv->driver);
2504 hid_free_dynids(hdrv);
2506 bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
2508 EXPORT_SYMBOL_GPL(hid_unregister_driver);
2510 int hid_check_keys_pressed(struct hid_device *hid)
2512 struct hid_input *hidinput;
2515 if (!(hid->claimed & HID_CLAIMED_INPUT))
2518 list_for_each_entry(hidinput, &hid->inputs, list) {
2519 for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
2520 if (hidinput->input->key[i])
2527 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
2529 static int __init hid_init(void)
2534 pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
2535 "debugfs is now used for inspecting the device (report descriptor, reports)\n");
2537 ret = bus_register(&hid_bus_type);
2539 pr_err("can't register hid bus\n");
2543 ret = hidraw_init();
2551 bus_unregister(&hid_bus_type);
2556 static void __exit hid_exit(void)
2560 bus_unregister(&hid_bus_type);
2561 hid_quirks_exit(HID_BUS_ANY);
2564 module_init(hid_init);
2565 module_exit(hid_exit);
2567 MODULE_AUTHOR("Andreas Gal");
2568 MODULE_AUTHOR("Vojtech Pavlik");
2569 MODULE_AUTHOR("Jiri Kosina");
2570 MODULE_LICENSE("GPL");