2 * USB 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 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/sched.h>
22 #include <linux/list.h>
24 #include <linux/smp_lock.h>
25 #include <linux/spinlock.h>
26 #include <asm/unaligned.h>
27 #include <asm/byteorder.h>
28 #include <linux/input.h>
29 #include <linux/wait.h>
34 #include <linux/usb.h>
36 #include <linux/hid.h>
37 #include <linux/hiddev.h>
43 #define DRIVER_VERSION "v2.6"
44 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
45 #define DRIVER_DESC "USB HID core driver"
46 #define DRIVER_LICENSE "GPL"
52 static unsigned int hid_mousepoll_interval;
53 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
57 * Register a new report for a device.
60 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
62 struct hid_report_enum *report_enum = device->report_enum + type;
63 struct hid_report *report;
65 if (report_enum->report_id_hash[id])
66 return report_enum->report_id_hash[id];
68 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
72 report_enum->numbered = 1;
77 report->device = device;
78 report_enum->report_id_hash[id] = report;
80 list_add_tail(&report->list, &report_enum->report_list);
86 * Register a new field for this report.
89 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
91 struct hid_field *field;
93 if (report->maxfield == HID_MAX_FIELDS) {
94 dbg("too many fields in report");
98 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
99 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
101 field->index = report->maxfield++;
102 report->field[field->index] = field;
103 field->usage = (struct hid_usage *)(field + 1);
104 field->value = (unsigned *)(field->usage + usages);
105 field->report = report;
111 * Open a collection. The type/usage is pushed on the stack.
114 static int open_collection(struct hid_parser *parser, unsigned type)
116 struct hid_collection *collection;
119 usage = parser->local.usage[0];
121 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
122 dbg("collection stack overflow");
126 if (parser->device->maxcollection == parser->device->collection_size) {
127 collection = kmalloc(sizeof(struct hid_collection) *
128 parser->device->collection_size * 2, GFP_KERNEL);
129 if (collection == NULL) {
130 dbg("failed to reallocate collection array");
133 memcpy(collection, parser->device->collection,
134 sizeof(struct hid_collection) *
135 parser->device->collection_size);
136 memset(collection + parser->device->collection_size, 0,
137 sizeof(struct hid_collection) *
138 parser->device->collection_size);
139 kfree(parser->device->collection);
140 parser->device->collection = collection;
141 parser->device->collection_size *= 2;
144 parser->collection_stack[parser->collection_stack_ptr++] =
145 parser->device->maxcollection;
147 collection = parser->device->collection +
148 parser->device->maxcollection++;
149 collection->type = type;
150 collection->usage = usage;
151 collection->level = parser->collection_stack_ptr - 1;
153 if (type == HID_COLLECTION_APPLICATION)
154 parser->device->maxapplication++;
160 * Close a collection.
163 static int close_collection(struct hid_parser *parser)
165 if (!parser->collection_stack_ptr) {
166 dbg("collection stack underflow");
169 parser->collection_stack_ptr--;
174 * Climb up the stack, search for the specified collection type
175 * and return the usage.
178 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
181 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
182 if (parser->device->collection[parser->collection_stack[n]].type == type)
183 return parser->device->collection[parser->collection_stack[n]].usage;
184 return 0; /* we know nothing about this usage type */
188 * Add a usage to the temporary parser table.
191 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
193 if (parser->local.usage_index >= HID_MAX_USAGES) {
194 dbg("usage index exceeded");
197 parser->local.usage[parser->local.usage_index] = usage;
198 parser->local.collection_index[parser->local.usage_index] =
199 parser->collection_stack_ptr ?
200 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
201 parser->local.usage_index++;
206 * Register a new field for this report.
209 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
211 struct hid_report *report;
212 struct hid_field *field;
217 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
218 dbg("hid_register_report failed");
222 if (parser->global.logical_maximum < parser->global.logical_minimum) {
223 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
227 offset = report->size;
228 report->size += parser->global.report_size * parser->global.report_count;
230 if (!parser->local.usage_index) /* Ignore padding fields */
233 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
235 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
238 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
239 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
240 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
242 for (i = 0; i < usages; i++) {
244 /* Duplicate the last usage we parsed if we have excess values */
245 if (i >= parser->local.usage_index)
246 j = parser->local.usage_index - 1;
247 field->usage[i].hid = parser->local.usage[j];
248 field->usage[i].collection_index =
249 parser->local.collection_index[j];
252 field->maxusage = usages;
253 field->flags = flags;
254 field->report_offset = offset;
255 field->report_type = report_type;
256 field->report_size = parser->global.report_size;
257 field->report_count = parser->global.report_count;
258 field->logical_minimum = parser->global.logical_minimum;
259 field->logical_maximum = parser->global.logical_maximum;
260 field->physical_minimum = parser->global.physical_minimum;
261 field->physical_maximum = parser->global.physical_maximum;
262 field->unit_exponent = parser->global.unit_exponent;
263 field->unit = parser->global.unit;
269 * Read data value from item.
272 static u32 item_udata(struct hid_item *item)
274 switch (item->size) {
275 case 1: return item->data.u8;
276 case 2: return item->data.u16;
277 case 4: return item->data.u32;
282 static s32 item_sdata(struct hid_item *item)
284 switch (item->size) {
285 case 1: return item->data.s8;
286 case 2: return item->data.s16;
287 case 4: return item->data.s32;
293 * Process a global item.
296 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
300 case HID_GLOBAL_ITEM_TAG_PUSH:
302 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
303 dbg("global enviroment stack overflow");
307 memcpy(parser->global_stack + parser->global_stack_ptr++,
308 &parser->global, sizeof(struct hid_global));
311 case HID_GLOBAL_ITEM_TAG_POP:
313 if (!parser->global_stack_ptr) {
314 dbg("global enviroment stack underflow");
318 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
319 sizeof(struct hid_global));
322 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
323 parser->global.usage_page = item_udata(item);
326 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
327 parser->global.logical_minimum = item_sdata(item);
330 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
331 if (parser->global.logical_minimum < 0)
332 parser->global.logical_maximum = item_sdata(item);
334 parser->global.logical_maximum = item_udata(item);
337 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
338 parser->global.physical_minimum = item_sdata(item);
341 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
342 if (parser->global.physical_minimum < 0)
343 parser->global.physical_maximum = item_sdata(item);
345 parser->global.physical_maximum = item_udata(item);
348 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
349 parser->global.unit_exponent = item_sdata(item);
352 case HID_GLOBAL_ITEM_TAG_UNIT:
353 parser->global.unit = item_udata(item);
356 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
357 if ((parser->global.report_size = item_udata(item)) > 32) {
358 dbg("invalid report_size %d", parser->global.report_size);
363 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
364 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
365 dbg("invalid report_count %d", parser->global.report_count);
370 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
371 if ((parser->global.report_id = item_udata(item)) == 0) {
372 dbg("report_id 0 is invalid");
378 dbg("unknown global tag 0x%x", item->tag);
384 * Process a local item.
387 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
392 if (item->size == 0) {
393 dbg("item data expected for local item");
397 data = item_udata(item);
401 case HID_LOCAL_ITEM_TAG_DELIMITER:
405 * We treat items before the first delimiter
406 * as global to all usage sets (branch 0).
407 * In the moment we process only these global
408 * items and the first delimiter set.
410 if (parser->local.delimiter_depth != 0) {
411 dbg("nested delimiters");
414 parser->local.delimiter_depth++;
415 parser->local.delimiter_branch++;
417 if (parser->local.delimiter_depth < 1) {
418 dbg("bogus close delimiter");
421 parser->local.delimiter_depth--;
425 case HID_LOCAL_ITEM_TAG_USAGE:
427 if (parser->local.delimiter_branch > 1) {
428 dbg("alternative usage ignored");
433 data = (parser->global.usage_page << 16) + data;
435 return hid_add_usage(parser, data);
437 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
439 if (parser->local.delimiter_branch > 1) {
440 dbg("alternative usage ignored");
445 data = (parser->global.usage_page << 16) + data;
447 parser->local.usage_minimum = data;
450 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
452 if (parser->local.delimiter_branch > 1) {
453 dbg("alternative usage ignored");
458 data = (parser->global.usage_page << 16) + data;
460 for (n = parser->local.usage_minimum; n <= data; n++)
461 if (hid_add_usage(parser, n)) {
462 dbg("hid_add_usage failed\n");
469 dbg("unknown local item tag 0x%x", item->tag);
476 * Process a main item.
479 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
484 data = item_udata(item);
487 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
488 ret = open_collection(parser, data & 0xff);
490 case HID_MAIN_ITEM_TAG_END_COLLECTION:
491 ret = close_collection(parser);
493 case HID_MAIN_ITEM_TAG_INPUT:
494 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
496 case HID_MAIN_ITEM_TAG_OUTPUT:
497 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
499 case HID_MAIN_ITEM_TAG_FEATURE:
500 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
503 dbg("unknown main item tag 0x%x", item->tag);
507 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
513 * Process a reserved item.
516 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
518 dbg("reserved item type, tag 0x%x", item->tag);
523 * Free a report and all registered fields. The field->usage and
524 * field->value table's are allocated behind the field, so we need
525 * only to free(field) itself.
528 static void hid_free_report(struct hid_report *report)
532 for (n = 0; n < report->maxfield; n++)
533 kfree(report->field[n]);
538 * Free a device structure, all reports, and all fields.
541 static void hid_free_device(struct hid_device *device)
545 for (i = 0; i < HID_REPORT_TYPES; i++) {
546 struct hid_report_enum *report_enum = device->report_enum + i;
548 for (j = 0; j < 256; j++) {
549 struct hid_report *report = report_enum->report_id_hash[j];
551 hid_free_report(report);
555 kfree(device->rdesc);
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 = le16_to_cpu(get_unaligned((__le16*)start));
612 start = (__u8 *)((__le16 *)start + 1);
617 if ((end - start) < 4)
619 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
620 start = (__u8 *)((__le32 *)start + 1);
628 * Parse a report description into a hid_device structure. Reports are
629 * enumerated, fields are attached to these reports.
632 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
634 struct hid_device *device;
635 struct hid_parser *parser;
636 struct hid_item item;
639 static int (*dispatch_type[])(struct hid_parser *parser,
640 struct hid_item *item) = {
647 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
650 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
651 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
655 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
657 for (i = 0; i < HID_REPORT_TYPES; i++)
658 INIT_LIST_HEAD(&device->report_enum[i].report_list);
660 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
661 kfree(device->collection);
665 memcpy(device->rdesc, start, size);
666 device->rsize = size;
668 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
669 kfree(device->rdesc);
670 kfree(device->collection);
674 parser->device = device;
677 while ((start = fetch_item(start, end, &item)) != NULL) {
679 if (item.format != HID_ITEM_FORMAT_SHORT) {
680 dbg("unexpected long global item");
681 kfree(device->collection);
682 hid_free_device(device);
687 if (dispatch_type[item.type](parser, &item)) {
688 dbg("item %u %u %u %u parsing failed\n",
689 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
690 kfree(device->collection);
691 hid_free_device(device);
697 if (parser->collection_stack_ptr) {
698 dbg("unbalanced collection at end of report description");
699 kfree(device->collection);
700 hid_free_device(device);
704 if (parser->local.delimiter_depth) {
705 dbg("unbalanced delimiter at end of report description");
706 kfree(device->collection);
707 hid_free_device(device);
716 dbg("item fetching failed at offset %d\n", (int)(end - start));
717 kfree(device->collection);
718 hid_free_device(device);
724 * Convert a signed n-bit integer to signed 32-bit integer. Common
725 * cases are done through the compiler, the screwed things has to be
729 static s32 snto32(__u32 value, unsigned n)
732 case 8: return ((__s8)value);
733 case 16: return ((__s16)value);
734 case 32: return ((__s32)value);
736 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
740 * Convert a signed 32-bit integer to a signed n-bit integer.
743 static u32 s32ton(__s32 value, unsigned n)
745 s32 a = value >> (n - 1);
747 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
748 return value & ((1 << n) - 1);
752 * Extract/implement a data field from/to a little endian report (bit array).
754 * Code sort-of follows HID spec:
755 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
757 * While the USB HID spec allows unlimited length bit fields in "report
758 * descriptors", most devices never use more than 16 bits.
759 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
760 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
763 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
769 report += offset >> 3; /* adjust byte index */
770 offset &= 7; /* now only need bit offset into one byte */
771 x = get_unaligned((u64 *) report);
773 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
778 * "implement" : set bits in a little endian bit stream.
779 * Same concepts as "extract" (see comments above).
780 * The data mangled in the bit stream remains in little endian
781 * order the whole time. It make more sense to talk about
782 * endianness of register values by considering a register
783 * a "cached" copy of the little endiad bit stream.
785 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
788 u64 m = (1ULL << n) - 1;
795 report += offset >> 3;
798 x = get_unaligned((u64 *)report);
799 x &= cpu_to_le64(~(m << offset));
800 x |= cpu_to_le64(((u64) value) << offset);
801 put_unaligned(x, (u64 *) report);
805 * Search an array for a value.
808 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
811 if (*array++ == value)
817 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
819 hid_dump_input(usage, value);
820 if (hid->claimed & HID_CLAIMED_INPUT)
821 hidinput_hid_event(hid, field, usage, value);
822 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
823 hiddev_hid_event(hid, field, usage, value);
827 * Analyse a received field, and fetch the data from it. The field
828 * content is stored for next report processing (we do differential
829 * reporting to the layer).
832 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
835 unsigned count = field->report_count;
836 unsigned offset = field->report_offset;
837 unsigned size = field->report_size;
838 __s32 min = field->logical_minimum;
839 __s32 max = field->logical_maximum;
842 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
845 for (n = 0; n < count; n++) {
847 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
848 extract(data, offset + n * size, size);
850 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
851 && value[n] >= min && value[n] <= max
852 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
856 for (n = 0; n < count; n++) {
858 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
859 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
863 if (field->value[n] >= min && field->value[n] <= max
864 && field->usage[field->value[n] - min].hid
865 && search(value, field->value[n], count))
866 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
868 if (value[n] >= min && value[n] <= max
869 && field->usage[value[n] - min].hid
870 && search(field->value, value[n], count))
871 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
874 memcpy(field->value, value, count * sizeof(__s32));
881 * Output the field into the report.
884 static void hid_output_field(struct hid_field *field, __u8 *data)
886 unsigned count = field->report_count;
887 unsigned offset = field->report_offset;
888 unsigned size = field->report_size;
891 for (n = 0; n < count; n++) {
892 if (field->logical_minimum < 0) /* signed values */
893 implement(data, offset + n * size, size, s32ton(field->value[n], size));
894 else /* unsigned values */
895 implement(data, offset + n * size, size, field->value[n]);
903 static void hid_output_report(struct hid_report *report, __u8 *data)
908 *data++ = report->id;
910 for (n = 0; n < report->maxfield; n++)
911 hid_output_field(report->field[n], data);
915 * Set a field value. The report this field belongs to has to be
916 * created and transferred to the device, to set this value in the
920 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
922 unsigned size = field->report_size;
924 hid_dump_input(field->usage + offset, value);
926 if (offset >= field->report_count) {
927 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
928 hid_dump_field(field, 8);
931 if (field->logical_minimum < 0) {
932 if (value != snto32(s32ton(value, size), size)) {
933 dbg("value %d is out of range", value);
937 field->value[offset] = value;