2 * HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
23 #include <linux/spinlock.h>
24 #include <asm/unaligned.h>
25 #include <asm/byteorder.h>
26 #include <linux/input.h>
27 #include <linux/wait.h>
28 #include <linux/vmalloc.h>
29 #include <linux/sched.h>
31 #include <linux/hid.h>
32 #include <linux/hiddev.h>
33 #include <linux/hid-debug.h>
34 #include <linux/hidraw.h>
42 #define DRIVER_VERSION "v2.6"
43 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
44 #define DRIVER_DESC "HID core driver"
45 #define DRIVER_LICENSE "GPL"
47 #ifdef CONFIG_HID_DEBUG
49 module_param_named(debug, hid_debug, int, 0600);
50 MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
51 EXPORT_SYMBOL_GPL(hid_debug);
55 * Register a new report for a device.
58 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
60 struct hid_report_enum *report_enum = device->report_enum + type;
61 struct hid_report *report;
63 if (report_enum->report_id_hash[id])
64 return report_enum->report_id_hash[id];
66 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
70 report_enum->numbered = 1;
75 report->device = device;
76 report_enum->report_id_hash[id] = report;
78 list_add_tail(&report->list, &report_enum->report_list);
84 * Register a new field for this report.
87 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
89 struct hid_field *field;
91 if (report->maxfield == HID_MAX_FIELDS) {
92 dbg_hid("too many fields in report\n");
96 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
99 field->index = report->maxfield++;
100 report->field[field->index] = field;
101 field->usage = (struct hid_usage *)(field + 1);
102 field->value = (s32 *)(field->usage + usages);
103 field->report = report;
109 * Open a collection. The type/usage is pushed on the stack.
112 static int open_collection(struct hid_parser *parser, unsigned type)
114 struct hid_collection *collection;
117 usage = parser->local.usage[0];
119 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
120 dbg_hid("collection stack overflow\n");
124 if (parser->device->maxcollection == parser->device->collection_size) {
125 collection = kmalloc(sizeof(struct hid_collection) *
126 parser->device->collection_size * 2, GFP_KERNEL);
127 if (collection == NULL) {
128 dbg_hid("failed to reallocate collection array\n");
131 memcpy(collection, parser->device->collection,
132 sizeof(struct hid_collection) *
133 parser->device->collection_size);
134 memset(collection + parser->device->collection_size, 0,
135 sizeof(struct hid_collection) *
136 parser->device->collection_size);
137 kfree(parser->device->collection);
138 parser->device->collection = collection;
139 parser->device->collection_size *= 2;
142 parser->collection_stack[parser->collection_stack_ptr++] =
143 parser->device->maxcollection;
145 collection = parser->device->collection +
146 parser->device->maxcollection++;
147 collection->type = type;
148 collection->usage = usage;
149 collection->level = parser->collection_stack_ptr - 1;
151 if (type == HID_COLLECTION_APPLICATION)
152 parser->device->maxapplication++;
158 * Close a collection.
161 static int close_collection(struct hid_parser *parser)
163 if (!parser->collection_stack_ptr) {
164 dbg_hid("collection stack underflow\n");
167 parser->collection_stack_ptr--;
172 * Climb up the stack, search for the specified collection type
173 * and return the usage.
176 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
179 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180 if (parser->device->collection[parser->collection_stack[n]].type == type)
181 return parser->device->collection[parser->collection_stack[n]].usage;
182 return 0; /* we know nothing about this usage type */
186 * Add a usage to the temporary parser table.
189 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
191 if (parser->local.usage_index >= HID_MAX_USAGES) {
192 dbg_hid("usage index exceeded\n");
195 parser->local.usage[parser->local.usage_index] = usage;
196 parser->local.collection_index[parser->local.usage_index] =
197 parser->collection_stack_ptr ?
198 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199 parser->local.usage_index++;
204 * Register a new field for this report.
207 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
209 struct hid_report *report;
210 struct hid_field *field;
215 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
216 dbg_hid("hid_register_report failed\n");
220 if (parser->global.logical_maximum < parser->global.logical_minimum) {
221 dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
225 offset = report->size;
226 report->size += parser->global.report_size * parser->global.report_count;
228 if (!parser->local.usage_index) /* Ignore padding fields */
231 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
233 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
236 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
237 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
238 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
240 for (i = 0; i < usages; i++) {
242 /* Duplicate the last usage we parsed if we have excess values */
243 if (i >= parser->local.usage_index)
244 j = parser->local.usage_index - 1;
245 field->usage[i].hid = parser->local.usage[j];
246 field->usage[i].collection_index =
247 parser->local.collection_index[j];
250 field->maxusage = usages;
251 field->flags = flags;
252 field->report_offset = offset;
253 field->report_type = report_type;
254 field->report_size = parser->global.report_size;
255 field->report_count = parser->global.report_count;
256 field->logical_minimum = parser->global.logical_minimum;
257 field->logical_maximum = parser->global.logical_maximum;
258 field->physical_minimum = parser->global.physical_minimum;
259 field->physical_maximum = parser->global.physical_maximum;
260 field->unit_exponent = parser->global.unit_exponent;
261 field->unit = parser->global.unit;
267 * Read data value from item.
270 static u32 item_udata(struct hid_item *item)
272 switch (item->size) {
273 case 1: return item->data.u8;
274 case 2: return item->data.u16;
275 case 4: return item->data.u32;
280 static s32 item_sdata(struct hid_item *item)
282 switch (item->size) {
283 case 1: return item->data.s8;
284 case 2: return item->data.s16;
285 case 4: return item->data.s32;
291 * Process a global item.
294 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
298 case HID_GLOBAL_ITEM_TAG_PUSH:
300 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
301 dbg_hid("global enviroment stack overflow\n");
305 memcpy(parser->global_stack + parser->global_stack_ptr++,
306 &parser->global, sizeof(struct hid_global));
309 case HID_GLOBAL_ITEM_TAG_POP:
311 if (!parser->global_stack_ptr) {
312 dbg_hid("global enviroment stack underflow\n");
316 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
317 sizeof(struct hid_global));
320 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321 parser->global.usage_page = item_udata(item);
324 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325 parser->global.logical_minimum = item_sdata(item);
328 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329 if (parser->global.logical_minimum < 0)
330 parser->global.logical_maximum = item_sdata(item);
332 parser->global.logical_maximum = item_udata(item);
335 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
336 parser->global.physical_minimum = item_sdata(item);
339 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
340 if (parser->global.physical_minimum < 0)
341 parser->global.physical_maximum = item_sdata(item);
343 parser->global.physical_maximum = item_udata(item);
346 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
347 parser->global.unit_exponent = item_sdata(item);
350 case HID_GLOBAL_ITEM_TAG_UNIT:
351 parser->global.unit = item_udata(item);
354 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
355 if ((parser->global.report_size = item_udata(item)) > 32) {
356 dbg_hid("invalid report_size %d\n", parser->global.report_size);
361 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
362 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
363 dbg_hid("invalid report_count %d\n", parser->global.report_count);
368 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
369 if ((parser->global.report_id = item_udata(item)) == 0) {
370 dbg_hid("report_id 0 is invalid\n");
376 dbg_hid("unknown global tag 0x%x\n", item->tag);
382 * Process a local item.
385 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
390 if (item->size == 0) {
391 dbg_hid("item data expected for local item\n");
395 data = item_udata(item);
399 case HID_LOCAL_ITEM_TAG_DELIMITER:
403 * We treat items before the first delimiter
404 * as global to all usage sets (branch 0).
405 * In the moment we process only these global
406 * items and the first delimiter set.
408 if (parser->local.delimiter_depth != 0) {
409 dbg_hid("nested delimiters\n");
412 parser->local.delimiter_depth++;
413 parser->local.delimiter_branch++;
415 if (parser->local.delimiter_depth < 1) {
416 dbg_hid("bogus close delimiter\n");
419 parser->local.delimiter_depth--;
423 case HID_LOCAL_ITEM_TAG_USAGE:
425 if (parser->local.delimiter_branch > 1) {
426 dbg_hid("alternative usage ignored\n");
431 data = (parser->global.usage_page << 16) + data;
433 return hid_add_usage(parser, data);
435 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
437 if (parser->local.delimiter_branch > 1) {
438 dbg_hid("alternative usage ignored\n");
443 data = (parser->global.usage_page << 16) + data;
445 parser->local.usage_minimum = data;
448 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
450 if (parser->local.delimiter_branch > 1) {
451 dbg_hid("alternative usage ignored\n");
456 data = (parser->global.usage_page << 16) + data;
458 for (n = parser->local.usage_minimum; n <= data; n++)
459 if (hid_add_usage(parser, n)) {
460 dbg_hid("hid_add_usage failed\n");
467 dbg_hid("unknown local item tag 0x%x\n", item->tag);
474 * Process a main item.
477 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
482 data = item_udata(item);
485 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
486 ret = open_collection(parser, data & 0xff);
488 case HID_MAIN_ITEM_TAG_END_COLLECTION:
489 ret = close_collection(parser);
491 case HID_MAIN_ITEM_TAG_INPUT:
492 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
494 case HID_MAIN_ITEM_TAG_OUTPUT:
495 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
497 case HID_MAIN_ITEM_TAG_FEATURE:
498 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
501 dbg_hid("unknown main item tag 0x%x\n", item->tag);
505 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
511 * Process a reserved item.
514 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
516 dbg_hid("reserved item type, tag 0x%x\n", item->tag);
521 * Free a report and all registered fields. The field->usage and
522 * field->value table's are allocated behind the field, so we need
523 * only to free(field) itself.
526 static void hid_free_report(struct hid_report *report)
530 for (n = 0; n < report->maxfield; n++)
531 kfree(report->field[n]);
536 * Free a device structure, all reports, and all fields.
539 static void hid_device_release(struct device *dev)
541 struct hid_device *device = container_of(dev, struct hid_device, dev);
544 for (i = 0; i < HID_REPORT_TYPES; i++) {
545 struct hid_report_enum *report_enum = device->report_enum + i;
547 for (j = 0; j < 256; j++) {
548 struct hid_report *report = report_enum->report_id_hash[j];
550 hid_free_report(report);
554 kfree(device->rdesc);
555 kfree(device->collection);
560 * Fetch a report description item from the data stream. We support long
561 * items, though they are not used yet.
564 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
568 if ((end - start) <= 0)
573 item->type = (b >> 2) & 3;
574 item->tag = (b >> 4) & 15;
576 if (item->tag == HID_ITEM_TAG_LONG) {
578 item->format = HID_ITEM_FORMAT_LONG;
580 if ((end - start) < 2)
583 item->size = *start++;
584 item->tag = *start++;
586 if ((end - start) < item->size)
589 item->data.longdata = start;
594 item->format = HID_ITEM_FORMAT_SHORT;
597 switch (item->size) {
603 if ((end - start) < 1)
605 item->data.u8 = *start++;
609 if ((end - start) < 2)
611 item->data.u16 = get_unaligned_le16(start);
612 start = (__u8 *)((__le16 *)start + 1);
617 if ((end - start) < 4)
619 item->data.u32 = get_unaligned_le32(start);
620 start = (__u8 *)((__le32 *)start + 1);
628 * hid_parse_report - parse device report
630 * @device: hid device
631 * @start: report start
634 * Parse a report description into a hid_device structure. Reports are
635 * enumerated, fields are attached to these reports.
636 * 0 returned on success, otherwise nonzero error value.
638 int hid_parse_report(struct hid_device *device, __u8 *start,
641 struct hid_parser *parser;
642 struct hid_item item;
645 static int (*dispatch_type[])(struct hid_parser *parser,
646 struct hid_item *item) = {
653 if (device->driver->report_fixup)
654 device->driver->report_fixup(device, start, size);
656 device->rdesc = kmalloc(size, GFP_KERNEL);
657 if (device->rdesc == NULL)
659 memcpy(device->rdesc, start, size);
660 device->rsize = size;
662 parser = vmalloc(sizeof(struct hid_parser));
668 memset(parser, 0, sizeof(struct hid_parser));
669 parser->device = device;
673 while ((start = fetch_item(start, end, &item)) != NULL) {
675 if (item.format != HID_ITEM_FORMAT_SHORT) {
676 dbg_hid("unexpected long global item\n");
680 if (dispatch_type[item.type](parser, &item)) {
681 dbg_hid("item %u %u %u %u parsing failed\n",
682 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
687 if (parser->collection_stack_ptr) {
688 dbg_hid("unbalanced collection at end of report description\n");
691 if (parser->local.delimiter_depth) {
692 dbg_hid("unbalanced delimiter at end of report description\n");
700 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
705 EXPORT_SYMBOL_GPL(hid_parse_report);
708 * Convert a signed n-bit integer to signed 32-bit integer. Common
709 * cases are done through the compiler, the screwed things has to be
713 static s32 snto32(__u32 value, unsigned n)
716 case 8: return ((__s8)value);
717 case 16: return ((__s16)value);
718 case 32: return ((__s32)value);
720 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
724 * Convert a signed 32-bit integer to a signed n-bit integer.
727 static u32 s32ton(__s32 value, unsigned n)
729 s32 a = value >> (n - 1);
731 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
732 return value & ((1 << n) - 1);
736 * Extract/implement a data field from/to a little endian report (bit array).
738 * Code sort-of follows HID spec:
739 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
741 * While the USB HID spec allows unlimited length bit fields in "report
742 * descriptors", most devices never use more than 16 bits.
743 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
744 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
747 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
752 printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
755 report += offset >> 3; /* adjust byte index */
756 offset &= 7; /* now only need bit offset into one byte */
757 x = get_unaligned_le64(report);
758 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
763 * "implement" : set bits in a little endian bit stream.
764 * Same concepts as "extract" (see comments above).
765 * The data mangled in the bit stream remains in little endian
766 * order the whole time. It make more sense to talk about
767 * endianness of register values by considering a register
768 * a "cached" copy of the little endiad bit stream.
770 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
773 u64 m = (1ULL << n) - 1;
776 printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
780 printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
781 value, current->comm);
785 report += offset >> 3;
788 x = get_unaligned_le64(report);
790 x |= ((u64)value) << offset;
791 put_unaligned_le64(x, report);
795 * Search an array for a value.
798 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
801 if (*array++ == value)
808 * hid_match_report - check if driver's raw_event should be called
811 * @report_type: type to match against
813 * compare hid->driver->report_table->report_type to report->type
815 static int hid_match_report(struct hid_device *hid, struct hid_report *report)
817 const struct hid_report_id *id = hid->driver->report_table;
819 if (!id) /* NULL means all */
822 for (; id->report_type != HID_TERMINATOR; id++)
823 if (id->report_type == HID_ANY_ID ||
824 id->report_type == report->type)
830 * hid_match_usage - check if driver's event should be called
833 * @usage: usage to match against
835 * compare hid->driver->usage_table->usage_{type,code} to
836 * usage->usage_{type,code}
838 static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
840 const struct hid_usage_id *id = hid->driver->usage_table;
842 if (!id) /* NULL means all */
845 for (; id->usage_type != HID_ANY_ID - 1; id++)
846 if ((id->usage_hid == HID_ANY_ID ||
847 id->usage_hid == usage->hid) &&
848 (id->usage_type == HID_ANY_ID ||
849 id->usage_type == usage->type) &&
850 (id->usage_code == HID_ANY_ID ||
851 id->usage_code == usage->code))
856 static void hid_process_event(struct hid_device *hid, struct hid_field *field,
857 struct hid_usage *usage, __s32 value, int interrupt)
859 struct hid_driver *hdrv = hid->driver;
862 hid_dump_input(usage, value);
864 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
865 ret = hdrv->event(hid, field, usage, value);
868 dbg_hid("%s's event failed with %d\n",
874 if (hid->claimed & HID_CLAIMED_INPUT)
875 hidinput_hid_event(hid, field, usage, value);
876 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
877 hid->hiddev_hid_event(hid, field, usage, value);
881 * Analyse a received field, and fetch the data from it. The field
882 * content is stored for next report processing (we do differential
883 * reporting to the layer).
886 static void hid_input_field(struct hid_device *hid, struct hid_field *field,
887 __u8 *data, int interrupt)
890 unsigned count = field->report_count;
891 unsigned offset = field->report_offset;
892 unsigned size = field->report_size;
893 __s32 min = field->logical_minimum;
894 __s32 max = field->logical_maximum;
897 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
900 for (n = 0; n < count; n++) {
902 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
903 extract(data, offset + n * size, size);
905 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
906 && value[n] >= min && value[n] <= max
907 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
911 for (n = 0; n < count; n++) {
913 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
914 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
918 if (field->value[n] >= min && field->value[n] <= max
919 && field->usage[field->value[n] - min].hid
920 && search(value, field->value[n], count))
921 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
923 if (value[n] >= min && value[n] <= max
924 && field->usage[value[n] - min].hid
925 && search(field->value, value[n], count))
926 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
929 memcpy(field->value, value, count * sizeof(__s32));
935 * Output the field into the report.
938 static void hid_output_field(struct hid_field *field, __u8 *data)
940 unsigned count = field->report_count;
941 unsigned offset = field->report_offset;
942 unsigned size = field->report_size;
943 unsigned bitsused = offset + count * size;
946 /* make sure the unused bits in the last byte are zeros */
947 if (count > 0 && size > 0 && (bitsused % 8) != 0)
948 data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
950 for (n = 0; n < count; n++) {
951 if (field->logical_minimum < 0) /* signed values */
952 implement(data, offset + n * size, size, s32ton(field->value[n], size));
953 else /* unsigned values */
954 implement(data, offset + n * size, size, field->value[n]);
962 void hid_output_report(struct hid_report *report, __u8 *data)
967 *data++ = report->id;
969 for (n = 0; n < report->maxfield; n++)
970 hid_output_field(report->field[n], data);
972 EXPORT_SYMBOL_GPL(hid_output_report);
975 * Set a field value. The report this field belongs to has to be
976 * created and transferred to the device, to set this value in the
980 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
982 unsigned size = field->report_size;
984 hid_dump_input(field->usage + offset, value);
986 if (offset >= field->report_count) {
987 dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
988 hid_dump_field(field, 8);
991 if (field->logical_minimum < 0) {
992 if (value != snto32(s32ton(value, size), size)) {
993 dbg_hid("value %d is out of range\n", value);
997 field->value[offset] = value;
1000 EXPORT_SYMBOL_GPL(hid_set_field);
1002 static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1005 struct hid_report *report;
1006 unsigned int n = 0; /* Normally report number is 0 */
1008 /* Device uses numbered reports, data[0] is report number */
1009 if (report_enum->numbered)
1012 report = report_enum->report_id_hash[n];
1014 dbg_hid("undefined report_id %u received\n", n);
1019 void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
1022 struct hid_report_enum *report_enum = hid->report_enum + type;
1023 struct hid_report *report;
1025 int rsize, csize = size;
1028 report = hid_get_report(report_enum, data);
1032 if (report_enum->numbered) {
1037 rsize = ((report->size - 1) >> 3) + 1;
1039 if (csize < rsize) {
1040 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
1042 memset(cdata + csize, 0, rsize - csize);
1045 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
1046 hid->hiddev_report_event(hid, report);
1047 if (hid->claimed & HID_CLAIMED_HIDRAW) {
1048 /* numbered reports need to be passed with the report num */
1049 if (report_enum->numbered)
1050 hidraw_report_event(hid, data - 1, size + 1);
1052 hidraw_report_event(hid, data, size);
1055 for (a = 0; a < report->maxfield; a++)
1056 hid_input_field(hid, report->field[a], cdata, interrupt);
1058 if (hid->claimed & HID_CLAIMED_INPUT)
1059 hidinput_report_event(hid, report);
1061 EXPORT_SYMBOL_GPL(hid_report_raw_event);
1064 * hid_input_report - report data from lower layer (usb, bt...)
1067 * @type: HID report type (HID_*_REPORT)
1068 * @data: report contents
1069 * @size: size of data parameter
1070 * @interrupt: called from atomic?
1072 * This is data entry for lower layers.
1074 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1076 struct hid_report_enum *report_enum = hid->report_enum + type;
1077 struct hid_driver *hdrv = hid->driver;
1078 struct hid_report *report;
1082 if (!hid || !hid->driver)
1086 dbg_hid("empty report\n");
1090 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1092 report = hid_get_report(report_enum, data);
1096 /* dump the report */
1097 dbg_hid("report %d (size %u) = ", report->id, size);
1098 for (i = 0; i < size; i++)
1099 dbg_hid_line(" %02x", data[i]);
1102 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1103 ret = hdrv->raw_event(hid, report, data, size);
1105 return ret < 0 ? ret : 0;
1108 hid_report_raw_event(hid, type, data, size, interrupt);
1112 EXPORT_SYMBOL_GPL(hid_input_report);
1114 static bool hid_match_one_id(struct hid_device *hdev,
1115 const struct hid_device_id *id)
1117 return id->bus == hdev->bus &&
1118 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1119 (id->product == HID_ANY_ID || id->product == hdev->product);
1122 static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1123 const struct hid_device_id *id)
1125 for (; id->bus; id++)
1126 if (hid_match_one_id(hdev, id))
1132 static const struct hid_device_id hid_blacklist[] = {
1133 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1134 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1135 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1136 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1137 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1138 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1139 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1140 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD) },
1141 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1142 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1143 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3) },
1144 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150) },
1145 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1146 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1150 static int hid_bus_match(struct device *dev, struct device_driver *drv)
1152 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1153 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1155 if (!hid_match_id(hdev, hdrv->id_table))
1158 /* generic wants all non-blacklisted */
1159 if (!strncmp(hdrv->name, "generic-", 8))
1160 return !hid_match_id(hdev, hid_blacklist);
1165 static int hid_device_probe(struct device *dev)
1167 struct hid_driver *hdrv = container_of(dev->driver,
1168 struct hid_driver, driver);
1169 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1170 const struct hid_device_id *id;
1173 if (!hdev->driver) {
1174 id = hid_match_id(hdev, hdrv->id_table);
1178 hdev->driver = hdrv;
1180 ret = hdrv->probe(hdev, id);
1181 } else { /* default probe */
1182 ret = hid_parse(hdev);
1184 ret = hid_hw_start(hdev);
1187 hdev->driver = NULL;
1192 static int hid_device_remove(struct device *dev)
1194 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1195 struct hid_driver *hdrv = hdev->driver;
1200 else /* default remove */
1202 hdev->driver = NULL;
1208 static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1210 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1212 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1213 hdev->bus, hdev->vendor, hdev->product))
1216 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1219 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1222 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1225 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1226 hdev->bus, hdev->vendor, hdev->product))
1232 static struct bus_type hid_bus_type = {
1234 .match = hid_bus_match,
1235 .probe = hid_device_probe,
1236 .remove = hid_device_remove,
1237 .uevent = hid_uevent,
1240 int hid_add_device(struct hid_device *hdev)
1242 static atomic_t id = ATOMIC_INIT(0);
1245 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1248 /* XXX hack, any other cleaner solution < 20 bus_id bytes? */
1249 sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
1250 hdev->vendor, hdev->product, atomic_inc_return(&id));
1252 ret = device_add(&hdev->dev);
1254 hdev->status |= HID_STAT_ADDED;
1258 EXPORT_SYMBOL_GPL(hid_add_device);
1261 * hid_allocate_device - allocate new hid device descriptor
1263 * Allocate and initialize hid device, so that hid_destroy_device might be
1266 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1269 struct hid_device *hid_allocate_device(void)
1271 struct hid_device *hdev;
1275 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1277 return ERR_PTR(ret);
1279 device_initialize(&hdev->dev);
1280 hdev->dev.release = hid_device_release;
1281 hdev->dev.bus = &hid_bus_type;
1283 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1284 sizeof(struct hid_collection), GFP_KERNEL);
1285 if (hdev->collection == NULL)
1287 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1289 for (i = 0; i < HID_REPORT_TYPES; i++)
1290 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1294 put_device(&hdev->dev);
1295 return ERR_PTR(ret);
1297 EXPORT_SYMBOL_GPL(hid_allocate_device);
1299 static void hid_remove_device(struct hid_device *hdev)
1301 if (hdev->status & HID_STAT_ADDED) {
1302 device_del(&hdev->dev);
1303 hdev->status &= ~HID_STAT_ADDED;
1308 * hid_destroy_device - free previously allocated device
1312 * If you allocate hid_device through hid_allocate_device, you should ever
1313 * free by this function.
1315 void hid_destroy_device(struct hid_device *hdev)
1317 hid_remove_device(hdev);
1318 put_device(&hdev->dev);
1320 EXPORT_SYMBOL_GPL(hid_destroy_device);
1322 int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1323 const char *mod_name)
1325 hdrv->driver.name = hdrv->name;
1326 hdrv->driver.bus = &hid_bus_type;
1327 hdrv->driver.owner = owner;
1328 hdrv->driver.mod_name = mod_name;
1330 return driver_register(&hdrv->driver);
1332 EXPORT_SYMBOL_GPL(__hid_register_driver);
1334 void hid_unregister_driver(struct hid_driver *hdrv)
1336 driver_unregister(&hdrv->driver);
1338 EXPORT_SYMBOL_GPL(hid_unregister_driver);
1340 static int __init hid_init(void)
1344 ret = bus_register(&hid_bus_type);
1346 printk(KERN_ERR "HID: can't register hid bus\n");
1350 ret = hidraw_init();
1356 bus_unregister(&hid_bus_type);
1361 static void __exit hid_exit(void)
1364 bus_unregister(&hid_bus_type);
1367 module_init(hid_init);
1368 module_exit(hid_exit);
1370 MODULE_LICENSE(DRIVER_LICENSE);