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/list.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
27 #include <linux/input.h>
28 #include <linux/wait.h>
30 #include <linux/hid.h>
31 #include <linux/hiddev.h>
32 #include <linux/hid-debug.h>
38 #define DRIVER_VERSION "v2.6"
39 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
40 #define DRIVER_DESC "HID core driver"
41 #define DRIVER_LICENSE "GPL"
44 * Register a new report for a device.
47 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
49 struct hid_report_enum *report_enum = device->report_enum + type;
50 struct hid_report *report;
52 if (report_enum->report_id_hash[id])
53 return report_enum->report_id_hash[id];
55 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
59 report_enum->numbered = 1;
64 report->device = device;
65 report_enum->report_id_hash[id] = report;
67 list_add_tail(&report->list, &report_enum->report_list);
73 * Register a new field for this report.
76 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
78 struct hid_field *field;
80 if (report->maxfield == HID_MAX_FIELDS) {
81 dbg("too many fields in report");
85 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
86 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
88 field->index = report->maxfield++;
89 report->field[field->index] = field;
90 field->usage = (struct hid_usage *)(field + 1);
91 field->value = (unsigned *)(field->usage + usages);
92 field->report = report;
98 * Open a collection. The type/usage is pushed on the stack.
101 static int open_collection(struct hid_parser *parser, unsigned type)
103 struct hid_collection *collection;
106 usage = parser->local.usage[0];
108 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
109 dbg("collection stack overflow");
113 if (parser->device->maxcollection == parser->device->collection_size) {
114 collection = kmalloc(sizeof(struct hid_collection) *
115 parser->device->collection_size * 2, GFP_KERNEL);
116 if (collection == NULL) {
117 dbg("failed to reallocate collection array");
120 memcpy(collection, parser->device->collection,
121 sizeof(struct hid_collection) *
122 parser->device->collection_size);
123 memset(collection + parser->device->collection_size, 0,
124 sizeof(struct hid_collection) *
125 parser->device->collection_size);
126 kfree(parser->device->collection);
127 parser->device->collection = collection;
128 parser->device->collection_size *= 2;
131 parser->collection_stack[parser->collection_stack_ptr++] =
132 parser->device->maxcollection;
134 collection = parser->device->collection +
135 parser->device->maxcollection++;
136 collection->type = type;
137 collection->usage = usage;
138 collection->level = parser->collection_stack_ptr - 1;
140 if (type == HID_COLLECTION_APPLICATION)
141 parser->device->maxapplication++;
147 * Close a collection.
150 static int close_collection(struct hid_parser *parser)
152 if (!parser->collection_stack_ptr) {
153 dbg("collection stack underflow");
156 parser->collection_stack_ptr--;
161 * Climb up the stack, search for the specified collection type
162 * and return the usage.
165 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
168 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
169 if (parser->device->collection[parser->collection_stack[n]].type == type)
170 return parser->device->collection[parser->collection_stack[n]].usage;
171 return 0; /* we know nothing about this usage type */
175 * Add a usage to the temporary parser table.
178 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
180 if (parser->local.usage_index >= HID_MAX_USAGES) {
181 dbg("usage index exceeded");
184 parser->local.usage[parser->local.usage_index] = usage;
185 parser->local.collection_index[parser->local.usage_index] =
186 parser->collection_stack_ptr ?
187 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
188 parser->local.usage_index++;
193 * Register a new field for this report.
196 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
198 struct hid_report *report;
199 struct hid_field *field;
204 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
205 dbg("hid_register_report failed");
209 if (parser->global.logical_maximum < parser->global.logical_minimum) {
210 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
214 offset = report->size;
215 report->size += parser->global.report_size * parser->global.report_count;
217 if (!parser->local.usage_index) /* Ignore padding fields */
220 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
222 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
225 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
226 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
227 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
229 for (i = 0; i < usages; i++) {
231 /* Duplicate the last usage we parsed if we have excess values */
232 if (i >= parser->local.usage_index)
233 j = parser->local.usage_index - 1;
234 field->usage[i].hid = parser->local.usage[j];
235 field->usage[i].collection_index =
236 parser->local.collection_index[j];
239 field->maxusage = usages;
240 field->flags = flags;
241 field->report_offset = offset;
242 field->report_type = report_type;
243 field->report_size = parser->global.report_size;
244 field->report_count = parser->global.report_count;
245 field->logical_minimum = parser->global.logical_minimum;
246 field->logical_maximum = parser->global.logical_maximum;
247 field->physical_minimum = parser->global.physical_minimum;
248 field->physical_maximum = parser->global.physical_maximum;
249 field->unit_exponent = parser->global.unit_exponent;
250 field->unit = parser->global.unit;
256 * Read data value from item.
259 static u32 item_udata(struct hid_item *item)
261 switch (item->size) {
262 case 1: return item->data.u8;
263 case 2: return item->data.u16;
264 case 4: return item->data.u32;
269 static s32 item_sdata(struct hid_item *item)
271 switch (item->size) {
272 case 1: return item->data.s8;
273 case 2: return item->data.s16;
274 case 4: return item->data.s32;
280 * Process a global item.
283 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
287 case HID_GLOBAL_ITEM_TAG_PUSH:
289 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
290 dbg("global enviroment stack overflow");
294 memcpy(parser->global_stack + parser->global_stack_ptr++,
295 &parser->global, sizeof(struct hid_global));
298 case HID_GLOBAL_ITEM_TAG_POP:
300 if (!parser->global_stack_ptr) {
301 dbg("global enviroment stack underflow");
305 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
306 sizeof(struct hid_global));
309 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
310 parser->global.usage_page = item_udata(item);
313 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
314 parser->global.logical_minimum = item_sdata(item);
317 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
318 if (parser->global.logical_minimum < 0)
319 parser->global.logical_maximum = item_sdata(item);
321 parser->global.logical_maximum = item_udata(item);
324 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
325 parser->global.physical_minimum = item_sdata(item);
328 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
329 if (parser->global.physical_minimum < 0)
330 parser->global.physical_maximum = item_sdata(item);
332 parser->global.physical_maximum = item_udata(item);
335 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
336 parser->global.unit_exponent = item_sdata(item);
339 case HID_GLOBAL_ITEM_TAG_UNIT:
340 parser->global.unit = item_udata(item);
343 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
344 if ((parser->global.report_size = item_udata(item)) > 32) {
345 dbg("invalid report_size %d", parser->global.report_size);
350 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
351 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
352 dbg("invalid report_count %d", parser->global.report_count);
357 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
358 if ((parser->global.report_id = item_udata(item)) == 0) {
359 dbg("report_id 0 is invalid");
365 dbg("unknown global tag 0x%x", item->tag);
371 * Process a local item.
374 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
379 if (item->size == 0) {
380 dbg("item data expected for local item");
384 data = item_udata(item);
388 case HID_LOCAL_ITEM_TAG_DELIMITER:
392 * We treat items before the first delimiter
393 * as global to all usage sets (branch 0).
394 * In the moment we process only these global
395 * items and the first delimiter set.
397 if (parser->local.delimiter_depth != 0) {
398 dbg("nested delimiters");
401 parser->local.delimiter_depth++;
402 parser->local.delimiter_branch++;
404 if (parser->local.delimiter_depth < 1) {
405 dbg("bogus close delimiter");
408 parser->local.delimiter_depth--;
412 case HID_LOCAL_ITEM_TAG_USAGE:
414 if (parser->local.delimiter_branch > 1) {
415 dbg("alternative usage ignored");
420 data = (parser->global.usage_page << 16) + data;
422 return hid_add_usage(parser, data);
424 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
426 if (parser->local.delimiter_branch > 1) {
427 dbg("alternative usage ignored");
432 data = (parser->global.usage_page << 16) + data;
434 parser->local.usage_minimum = data;
437 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
439 if (parser->local.delimiter_branch > 1) {
440 dbg("alternative usage ignored");
445 data = (parser->global.usage_page << 16) + data;
447 for (n = parser->local.usage_minimum; n <= data; n++)
448 if (hid_add_usage(parser, n)) {
449 dbg("hid_add_usage failed\n");
456 dbg("unknown local item tag 0x%x", item->tag);
463 * Process a main item.
466 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
471 data = item_udata(item);
474 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
475 ret = open_collection(parser, data & 0xff);
477 case HID_MAIN_ITEM_TAG_END_COLLECTION:
478 ret = close_collection(parser);
480 case HID_MAIN_ITEM_TAG_INPUT:
481 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
483 case HID_MAIN_ITEM_TAG_OUTPUT:
484 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
486 case HID_MAIN_ITEM_TAG_FEATURE:
487 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
490 dbg("unknown main item tag 0x%x", item->tag);
494 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
500 * Process a reserved item.
503 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
505 dbg("reserved item type, tag 0x%x", item->tag);
510 * Free a report and all registered fields. The field->usage and
511 * field->value table's are allocated behind the field, so we need
512 * only to free(field) itself.
515 static void hid_free_report(struct hid_report *report)
519 for (n = 0; n < report->maxfield; n++)
520 kfree(report->field[n]);
525 * Free a device structure, all reports, and all fields.
528 void hid_free_device(struct hid_device *device)
532 for (i = 0; i < HID_REPORT_TYPES; i++) {
533 struct hid_report_enum *report_enum = device->report_enum + i;
535 for (j = 0; j < 256; j++) {
536 struct hid_report *report = report_enum->report_id_hash[j];
538 hid_free_report(report);
542 kfree(device->rdesc);
543 kfree(device->collection);
546 EXPORT_SYMBOL_GPL(hid_free_device);
549 * Fetch a report description item from the data stream. We support long
550 * items, though they are not used yet.
553 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
557 if ((end - start) <= 0)
562 item->type = (b >> 2) & 3;
563 item->tag = (b >> 4) & 15;
565 if (item->tag == HID_ITEM_TAG_LONG) {
567 item->format = HID_ITEM_FORMAT_LONG;
569 if ((end - start) < 2)
572 item->size = *start++;
573 item->tag = *start++;
575 if ((end - start) < item->size)
578 item->data.longdata = start;
583 item->format = HID_ITEM_FORMAT_SHORT;
586 switch (item->size) {
592 if ((end - start) < 1)
594 item->data.u8 = *start++;
598 if ((end - start) < 2)
600 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
601 start = (__u8 *)((__le16 *)start + 1);
606 if ((end - start) < 4)
608 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
609 start = (__u8 *)((__le32 *)start + 1);
617 * Parse a report description into a hid_device structure. Reports are
618 * enumerated, fields are attached to these reports.
621 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
623 struct hid_device *device;
624 struct hid_parser *parser;
625 struct hid_item item;
628 static int (*dispatch_type[])(struct hid_parser *parser,
629 struct hid_item *item) = {
636 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
639 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
640 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
644 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
646 for (i = 0; i < HID_REPORT_TYPES; i++)
647 INIT_LIST_HEAD(&device->report_enum[i].report_list);
649 if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
650 kfree(device->collection);
654 memcpy(device->rdesc, start, size);
655 device->rsize = size;
657 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
658 kfree(device->rdesc);
659 kfree(device->collection);
663 parser->device = device;
666 while ((start = fetch_item(start, end, &item)) != NULL) {
668 if (item.format != HID_ITEM_FORMAT_SHORT) {
669 dbg("unexpected long global item");
670 hid_free_device(device);
675 if (dispatch_type[item.type](parser, &item)) {
676 dbg("item %u %u %u %u parsing failed\n",
677 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
678 hid_free_device(device);
684 if (parser->collection_stack_ptr) {
685 dbg("unbalanced collection at end of report description");
686 hid_free_device(device);
690 if (parser->local.delimiter_depth) {
691 dbg("unbalanced delimiter at end of report description");
692 hid_free_device(device);
701 dbg("item fetching failed at offset %d\n", (int)(end - start));
702 hid_free_device(device);
706 EXPORT_SYMBOL_GPL(hid_parse_report);
709 * Convert a signed n-bit integer to signed 32-bit integer. Common
710 * cases are done through the compiler, the screwed things has to be
714 static s32 snto32(__u32 value, unsigned n)
717 case 8: return ((__s8)value);
718 case 16: return ((__s16)value);
719 case 32: return ((__s32)value);
721 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
725 * Convert a signed 32-bit integer to a signed n-bit integer.
728 static u32 s32ton(__s32 value, unsigned n)
730 s32 a = value >> (n - 1);
732 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
733 return value & ((1 << n) - 1);
737 * Extract/implement a data field from/to a little endian report (bit array).
739 * Code sort-of follows HID spec:
740 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
742 * While the USB HID spec allows unlimited length bit fields in "report
743 * descriptors", most devices never use more than 16 bits.
744 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
745 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
748 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
754 report += offset >> 3; /* adjust byte index */
755 offset &= 7; /* now only need bit offset into one byte */
756 x = get_unaligned((u64 *) 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;
780 report += offset >> 3;
783 x = get_unaligned((u64 *)report);
784 x &= cpu_to_le64(~(m << offset));
785 x |= cpu_to_le64(((u64) value) << offset);
786 put_unaligned(x, (u64 *) report);
790 * Search an array for a value.
793 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
796 if (*array++ == value)
802 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
804 hid_dump_input(usage, value);
805 if (hid->claimed & HID_CLAIMED_INPUT)
806 hidinput_hid_event(hid, field, usage, value);
807 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
808 hid->hiddev_hid_event(hid, field, usage, value);
812 * Analyse a received field, and fetch the data from it. The field
813 * content is stored for next report processing (we do differential
814 * reporting to the layer).
817 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
820 unsigned count = field->report_count;
821 unsigned offset = field->report_offset;
822 unsigned size = field->report_size;
823 __s32 min = field->logical_minimum;
824 __s32 max = field->logical_maximum;
827 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
830 for (n = 0; n < count; n++) {
832 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
833 extract(data, offset + n * size, size);
835 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
836 && value[n] >= min && value[n] <= max
837 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
841 for (n = 0; n < count; n++) {
843 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
844 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
848 if (field->value[n] >= min && field->value[n] <= max
849 && field->usage[field->value[n] - min].hid
850 && search(value, field->value[n], count))
851 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
853 if (value[n] >= min && value[n] <= max
854 && field->usage[value[n] - min].hid
855 && search(field->value, value[n], count))
856 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
859 memcpy(field->value, value, count * sizeof(__s32));
863 EXPORT_SYMBOL_GPL(hid_input_field);
866 * Output the field into the report.
869 static void hid_output_field(struct hid_field *field, __u8 *data)
871 unsigned count = field->report_count;
872 unsigned offset = field->report_offset;
873 unsigned size = field->report_size;
876 /* make sure the unused bits in the last byte are zeros */
877 if (count > 0 && size > 0)
878 data[(offset+count*size-1)/8] = 0;
880 for (n = 0; n < count; n++) {
881 if (field->logical_minimum < 0) /* signed values */
882 implement(data, offset + n * size, size, s32ton(field->value[n], size));
883 else /* unsigned values */
884 implement(data, offset + n * size, size, field->value[n]);
892 void hid_output_report(struct hid_report *report, __u8 *data)
897 *data++ = report->id;
899 for (n = 0; n < report->maxfield; n++)
900 hid_output_field(report->field[n], data);
902 EXPORT_SYMBOL_GPL(hid_output_report);
905 * Set a field value. The report this field belongs to has to be
906 * created and transferred to the device, to set this value in the
910 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
912 unsigned size = field->report_size;
914 hid_dump_input(field->usage + offset, value);
916 if (offset >= field->report_count) {
917 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
918 hid_dump_field(field, 8);
921 if (field->logical_minimum < 0) {
922 if (value != snto32(s32ton(value, size), size)) {
923 dbg("value %d is out of range", value);
927 field->value[offset] = value;
930 EXPORT_SYMBOL_GPL(hid_set_field);
932 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
934 struct hid_report_enum *report_enum = hid->report_enum + type;
935 struct hid_report *report;
946 #ifdef CONFIG_HID_DEBUG
947 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
950 n = 0; /* Normally report number is 0 */
951 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
956 #ifdef CONFIG_HID_DEBUG
959 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size);
960 for (i = 0; i < size; i++)
961 printk(" %02x", data[i]);
966 if (!(report = report_enum->report_id_hash[n])) {
967 dbg("undefined report_id %d received", n);
971 rsize = ((report->size - 1) >> 3) + 1;
974 dbg("report %d is too short, (%d < %d)", report->id, size, rsize);
978 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
979 hid->hiddev_report_event(hid, report);
981 for (n = 0; n < report->maxfield; n++)
982 hid_input_field(hid, report->field[n], data, interrupt);
984 if (hid->claimed & HID_CLAIMED_INPUT)
985 hidinput_report_event(hid, report);
989 EXPORT_SYMBOL_GPL(hid_input_report);
991 MODULE_LICENSE(DRIVER_LICENSE);