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 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/hid.h>
35 #include <linux/hiddev.h>
41 #define DRIVER_VERSION "v2.6"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
43 #define DRIVER_DESC "HID core driver"
44 #define DRIVER_LICENSE "GPL"
47 * Register a new report for a device.
50 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
52 struct hid_report_enum *report_enum = device->report_enum + type;
53 struct hid_report *report;
55 if (report_enum->report_id_hash[id])
56 return report_enum->report_id_hash[id];
58 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
62 report_enum->numbered = 1;
67 report->device = device;
68 report_enum->report_id_hash[id] = report;
70 list_add_tail(&report->list, &report_enum->report_list);
76 * Register a new field for this report.
79 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
81 struct hid_field *field;
83 if (report->maxfield == HID_MAX_FIELDS) {
84 dbg("too many fields in report");
88 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
89 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
91 field->index = report->maxfield++;
92 report->field[field->index] = field;
93 field->usage = (struct hid_usage *)(field + 1);
94 field->value = (unsigned *)(field->usage + usages);
95 field->report = report;
101 * Open a collection. The type/usage is pushed on the stack.
104 static int open_collection(struct hid_parser *parser, unsigned type)
106 struct hid_collection *collection;
109 usage = parser->local.usage[0];
111 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
112 dbg("collection stack overflow");
116 if (parser->device->maxcollection == parser->device->collection_size) {
117 collection = kmalloc(sizeof(struct hid_collection) *
118 parser->device->collection_size * 2, GFP_KERNEL);
119 if (collection == NULL) {
120 dbg("failed to reallocate collection array");
123 memcpy(collection, parser->device->collection,
124 sizeof(struct hid_collection) *
125 parser->device->collection_size);
126 memset(collection + parser->device->collection_size, 0,
127 sizeof(struct hid_collection) *
128 parser->device->collection_size);
129 kfree(parser->device->collection);
130 parser->device->collection = collection;
131 parser->device->collection_size *= 2;
134 parser->collection_stack[parser->collection_stack_ptr++] =
135 parser->device->maxcollection;
137 collection = parser->device->collection +
138 parser->device->maxcollection++;
139 collection->type = type;
140 collection->usage = usage;
141 collection->level = parser->collection_stack_ptr - 1;
143 if (type == HID_COLLECTION_APPLICATION)
144 parser->device->maxapplication++;
150 * Close a collection.
153 static int close_collection(struct hid_parser *parser)
155 if (!parser->collection_stack_ptr) {
156 dbg("collection stack underflow");
159 parser->collection_stack_ptr--;
164 * Climb up the stack, search for the specified collection type
165 * and return the usage.
168 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
171 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
172 if (parser->device->collection[parser->collection_stack[n]].type == type)
173 return parser->device->collection[parser->collection_stack[n]].usage;
174 return 0; /* we know nothing about this usage type */
178 * Add a usage to the temporary parser table.
181 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
183 if (parser->local.usage_index >= HID_MAX_USAGES) {
184 dbg("usage index exceeded");
187 parser->local.usage[parser->local.usage_index] = usage;
188 parser->local.collection_index[parser->local.usage_index] =
189 parser->collection_stack_ptr ?
190 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
191 parser->local.usage_index++;
196 * Register a new field for this report.
199 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
201 struct hid_report *report;
202 struct hid_field *field;
207 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
208 dbg("hid_register_report failed");
212 if (parser->global.logical_maximum < parser->global.logical_minimum) {
213 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
217 offset = report->size;
218 report->size += parser->global.report_size * parser->global.report_count;
220 if (!parser->local.usage_index) /* Ignore padding fields */
223 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
225 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
228 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
229 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
230 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
232 for (i = 0; i < usages; i++) {
234 /* Duplicate the last usage we parsed if we have excess values */
235 if (i >= parser->local.usage_index)
236 j = parser->local.usage_index - 1;
237 field->usage[i].hid = parser->local.usage[j];
238 field->usage[i].collection_index =
239 parser->local.collection_index[j];
242 field->maxusage = usages;
243 field->flags = flags;
244 field->report_offset = offset;
245 field->report_type = report_type;
246 field->report_size = parser->global.report_size;
247 field->report_count = parser->global.report_count;
248 field->logical_minimum = parser->global.logical_minimum;
249 field->logical_maximum = parser->global.logical_maximum;
250 field->physical_minimum = parser->global.physical_minimum;
251 field->physical_maximum = parser->global.physical_maximum;
252 field->unit_exponent = parser->global.unit_exponent;
253 field->unit = parser->global.unit;
259 * Read data value from item.
262 static u32 item_udata(struct hid_item *item)
264 switch (item->size) {
265 case 1: return item->data.u8;
266 case 2: return item->data.u16;
267 case 4: return item->data.u32;
272 static s32 item_sdata(struct hid_item *item)
274 switch (item->size) {
275 case 1: return item->data.s8;
276 case 2: return item->data.s16;
277 case 4: return item->data.s32;
283 * Process a global item.
286 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
290 case HID_GLOBAL_ITEM_TAG_PUSH:
292 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
293 dbg("global enviroment stack overflow");
297 memcpy(parser->global_stack + parser->global_stack_ptr++,
298 &parser->global, sizeof(struct hid_global));
301 case HID_GLOBAL_ITEM_TAG_POP:
303 if (!parser->global_stack_ptr) {
304 dbg("global enviroment stack underflow");
308 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
309 sizeof(struct hid_global));
312 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
313 parser->global.usage_page = item_udata(item);
316 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
317 parser->global.logical_minimum = item_sdata(item);
320 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
321 if (parser->global.logical_minimum < 0)
322 parser->global.logical_maximum = item_sdata(item);
324 parser->global.logical_maximum = item_udata(item);
327 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
328 parser->global.physical_minimum = item_sdata(item);
331 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
332 if (parser->global.physical_minimum < 0)
333 parser->global.physical_maximum = item_sdata(item);
335 parser->global.physical_maximum = item_udata(item);
338 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
339 parser->global.unit_exponent = item_sdata(item);
342 case HID_GLOBAL_ITEM_TAG_UNIT:
343 parser->global.unit = item_udata(item);
346 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
347 if ((parser->global.report_size = item_udata(item)) > 32) {
348 dbg("invalid report_size %d", parser->global.report_size);
353 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
354 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
355 dbg("invalid report_count %d", parser->global.report_count);
360 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
361 if ((parser->global.report_id = item_udata(item)) == 0) {
362 dbg("report_id 0 is invalid");
368 dbg("unknown global tag 0x%x", item->tag);
374 * Process a local item.
377 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
382 if (item->size == 0) {
383 dbg("item data expected for local item");
387 data = item_udata(item);
391 case HID_LOCAL_ITEM_TAG_DELIMITER:
395 * We treat items before the first delimiter
396 * as global to all usage sets (branch 0).
397 * In the moment we process only these global
398 * items and the first delimiter set.
400 if (parser->local.delimiter_depth != 0) {
401 dbg("nested delimiters");
404 parser->local.delimiter_depth++;
405 parser->local.delimiter_branch++;
407 if (parser->local.delimiter_depth < 1) {
408 dbg("bogus close delimiter");
411 parser->local.delimiter_depth--;
415 case HID_LOCAL_ITEM_TAG_USAGE:
417 if (parser->local.delimiter_branch > 1) {
418 dbg("alternative usage ignored");
423 data = (parser->global.usage_page << 16) + data;
425 return hid_add_usage(parser, data);
427 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
429 if (parser->local.delimiter_branch > 1) {
430 dbg("alternative usage ignored");
435 data = (parser->global.usage_page << 16) + data;
437 parser->local.usage_minimum = data;
440 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
442 if (parser->local.delimiter_branch > 1) {
443 dbg("alternative usage ignored");
448 data = (parser->global.usage_page << 16) + data;
450 for (n = parser->local.usage_minimum; n <= data; n++)
451 if (hid_add_usage(parser, n)) {
452 dbg("hid_add_usage failed\n");
459 dbg("unknown local item tag 0x%x", item->tag);
466 * Process a main item.
469 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
474 data = item_udata(item);
477 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
478 ret = open_collection(parser, data & 0xff);
480 case HID_MAIN_ITEM_TAG_END_COLLECTION:
481 ret = close_collection(parser);
483 case HID_MAIN_ITEM_TAG_INPUT:
484 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
486 case HID_MAIN_ITEM_TAG_OUTPUT:
487 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
489 case HID_MAIN_ITEM_TAG_FEATURE:
490 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
493 dbg("unknown main item tag 0x%x", item->tag);
497 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
503 * Process a reserved item.
506 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
508 dbg("reserved item type, tag 0x%x", item->tag);
513 * Free a report and all registered fields. The field->usage and
514 * field->value table's are allocated behind the field, so we need
515 * only to free(field) itself.
518 static void hid_free_report(struct hid_report *report)
522 for (n = 0; n < report->maxfield; n++)
523 kfree(report->field[n]);
528 * Free a device structure, all reports, and all fields.
531 void hid_free_device(struct hid_device *device)
535 for (i = 0; i < HID_REPORT_TYPES; i++) {
536 struct hid_report_enum *report_enum = device->report_enum + i;
538 for (j = 0; j < 256; j++) {
539 struct hid_report *report = report_enum->report_id_hash[j];
541 hid_free_report(report);
545 kfree(device->rdesc);
546 kfree(device->collection);
549 EXPORT_SYMBOL_GPL(hid_free_device);
552 * Fetch a report description item from the data stream. We support long
553 * items, though they are not used yet.
556 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
560 if ((end - start) <= 0)
565 item->type = (b >> 2) & 3;
566 item->tag = (b >> 4) & 15;
568 if (item->tag == HID_ITEM_TAG_LONG) {
570 item->format = HID_ITEM_FORMAT_LONG;
572 if ((end - start) < 2)
575 item->size = *start++;
576 item->tag = *start++;
578 if ((end - start) < item->size)
581 item->data.longdata = start;
586 item->format = HID_ITEM_FORMAT_SHORT;
589 switch (item->size) {
595 if ((end - start) < 1)
597 item->data.u8 = *start++;
601 if ((end - start) < 2)
603 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
604 start = (__u8 *)((__le16 *)start + 1);
609 if ((end - start) < 4)
611 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
612 start = (__u8 *)((__le32 *)start + 1);
620 * Parse a report description into a hid_device structure. Reports are
621 * enumerated, fields are attached to these reports.
624 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
626 struct hid_device *device;
627 struct hid_parser *parser;
628 struct hid_item item;
631 static int (*dispatch_type[])(struct hid_parser *parser,
632 struct hid_item *item) = {
639 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
642 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
643 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
647 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
649 for (i = 0; i < HID_REPORT_TYPES; i++)
650 INIT_LIST_HEAD(&device->report_enum[i].report_list);
652 if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
653 kfree(device->collection);
657 memcpy(device->rdesc, start, size);
658 device->rsize = size;
660 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
661 kfree(device->rdesc);
662 kfree(device->collection);
666 parser->device = device;
669 while ((start = fetch_item(start, end, &item)) != NULL) {
671 if (item.format != HID_ITEM_FORMAT_SHORT) {
672 dbg("unexpected long global item");
673 kfree(device->collection);
674 hid_free_device(device);
679 if (dispatch_type[item.type](parser, &item)) {
680 dbg("item %u %u %u %u parsing failed\n",
681 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
682 kfree(device->collection);
683 hid_free_device(device);
689 if (parser->collection_stack_ptr) {
690 dbg("unbalanced collection at end of report description");
691 kfree(device->collection);
692 hid_free_device(device);
696 if (parser->local.delimiter_depth) {
697 dbg("unbalanced delimiter at end of report description");
698 kfree(device->collection);
699 hid_free_device(device);
708 dbg("item fetching failed at offset %d\n", (int)(end - start));
709 kfree(device->collection);
710 hid_free_device(device);
714 EXPORT_SYMBOL_GPL(hid_parse_report);
717 * Convert a signed n-bit integer to signed 32-bit integer. Common
718 * cases are done through the compiler, the screwed things has to be
722 static s32 snto32(__u32 value, unsigned n)
725 case 8: return ((__s8)value);
726 case 16: return ((__s16)value);
727 case 32: return ((__s32)value);
729 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
733 * Convert a signed 32-bit integer to a signed n-bit integer.
736 static u32 s32ton(__s32 value, unsigned n)
738 s32 a = value >> (n - 1);
740 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
741 return value & ((1 << n) - 1);
745 * Extract/implement a data field from/to a little endian report (bit array).
747 * Code sort-of follows HID spec:
748 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
750 * While the USB HID spec allows unlimited length bit fields in "report
751 * descriptors", most devices never use more than 16 bits.
752 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
753 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
756 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
762 report += offset >> 3; /* adjust byte index */
763 offset &= 7; /* now only need bit offset into one byte */
764 x = get_unaligned((u64 *) report);
766 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
771 * "implement" : set bits in a little endian bit stream.
772 * Same concepts as "extract" (see comments above).
773 * The data mangled in the bit stream remains in little endian
774 * order the whole time. It make more sense to talk about
775 * endianness of register values by considering a register
776 * a "cached" copy of the little endiad bit stream.
778 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
781 u64 m = (1ULL << n) - 1;
788 report += offset >> 3;
791 x = get_unaligned((u64 *)report);
792 x &= cpu_to_le64(~(m << offset));
793 x |= cpu_to_le64(((u64) value) << offset);
794 put_unaligned(x, (u64 *) report);
798 * Search an array for a value.
801 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
804 if (*array++ == value)
810 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
812 hid_dump_input(usage, value);
813 if (hid->claimed & HID_CLAIMED_INPUT)
814 hidinput_hid_event(hid, field, usage, value);
815 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
816 hid->hiddev_hid_event(hid, field, usage, value);
820 * Analyse a received field, and fetch the data from it. The field
821 * content is stored for next report processing (we do differential
822 * reporting to the layer).
825 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
828 unsigned count = field->report_count;
829 unsigned offset = field->report_offset;
830 unsigned size = field->report_size;
831 __s32 min = field->logical_minimum;
832 __s32 max = field->logical_maximum;
835 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
838 for (n = 0; n < count; n++) {
840 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
841 extract(data, offset + n * size, size);
843 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
844 && value[n] >= min && value[n] <= max
845 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
849 for (n = 0; n < count; n++) {
851 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
852 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
856 if (field->value[n] >= min && field->value[n] <= max
857 && field->usage[field->value[n] - min].hid
858 && search(value, field->value[n], count))
859 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
861 if (value[n] >= min && value[n] <= max
862 && field->usage[value[n] - min].hid
863 && search(field->value, value[n], count))
864 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
867 memcpy(field->value, value, count * sizeof(__s32));
871 EXPORT_SYMBOL_GPL(hid_input_field);
874 * Output the field into the report.
877 static void hid_output_field(struct hid_field *field, __u8 *data)
879 unsigned count = field->report_count;
880 unsigned offset = field->report_offset;
881 unsigned size = field->report_size;
884 /* make sure the unused bits in the last byte are zeros */
885 if (count > 0 && size > 0)
886 data[(count*size-1)/8] = 0;
888 for (n = 0; n < count; n++) {
889 if (field->logical_minimum < 0) /* signed values */
890 implement(data, offset + n * size, size, s32ton(field->value[n], size));
891 else /* unsigned values */
892 implement(data, offset + n * size, size, field->value[n]);
900 void hid_output_report(struct hid_report *report, __u8 *data)
905 *data++ = report->id;
907 for (n = 0; n < report->maxfield; n++)
908 hid_output_field(report->field[n], data);
910 EXPORT_SYMBOL_GPL(hid_output_report);
913 * Set a field value. The report this field belongs to has to be
914 * created and transferred to the device, to set this value in the
918 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
920 unsigned size = field->report_size;
922 hid_dump_input(field->usage + offset, value);
924 if (offset >= field->report_count) {
925 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
926 hid_dump_field(field, 8);
929 if (field->logical_minimum < 0) {
930 if (value != snto32(s32ton(value, size), size)) {
931 dbg("value %d is out of range", value);
935 field->value[offset] = value;
938 EXPORT_SYMBOL_GPL(hid_set_field);
940 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
942 struct hid_report_enum *report_enum = hid->report_enum + type;
943 struct hid_report *report;
955 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
958 n = 0; /* Normally report number is 0 */
959 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
967 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size);
968 for (i = 0; i < size; i++)
969 printk(" %02x", data[i]);
974 if (!(report = report_enum->report_id_hash[n])) {
975 dbg("undefined report_id %d received", n);
979 rsize = ((report->size - 1) >> 3) + 1;
982 dbg("report %d is too short, (%d < %d)", report->id, size, rsize);
986 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
987 hid->hiddev_report_event(hid, report);
989 for (n = 0; n < report->maxfield; n++)
990 hid_input_field(hid, report->field[n], data, interrupt);
992 if (hid->claimed & HID_CLAIMED_INPUT)
993 hidinput_report_event(hid, report);
997 EXPORT_SYMBOL_GPL(hid_input_report);
999 MODULE_LICENSE(DRIVER_LICENSE);