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>
31 #include <linux/hid.h>
32 #include <linux/hiddev.h>
33 #include <linux/hid-debug.h>
39 #define DRIVER_VERSION "v2.6"
40 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
41 #define DRIVER_DESC "HID core driver"
42 #define DRIVER_LICENSE "GPL"
45 * Register a new report for a device.
48 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
50 struct hid_report_enum *report_enum = device->report_enum + type;
51 struct hid_report *report;
53 if (report_enum->report_id_hash[id])
54 return report_enum->report_id_hash[id];
56 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
60 report_enum->numbered = 1;
65 report->device = device;
66 report_enum->report_id_hash[id] = report;
68 list_add_tail(&report->list, &report_enum->report_list);
74 * Register a new field for this report.
77 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
79 struct hid_field *field;
81 if (report->maxfield == HID_MAX_FIELDS) {
82 dbg("too many fields in report");
86 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
87 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
89 field->index = report->maxfield++;
90 report->field[field->index] = field;
91 field->usage = (struct hid_usage *)(field + 1);
92 field->value = (unsigned *)(field->usage + usages);
93 field->report = report;
99 * Open a collection. The type/usage is pushed on the stack.
102 static int open_collection(struct hid_parser *parser, unsigned type)
104 struct hid_collection *collection;
107 usage = parser->local.usage[0];
109 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
110 dbg("collection stack overflow");
114 if (parser->device->maxcollection == parser->device->collection_size) {
115 collection = kmalloc(sizeof(struct hid_collection) *
116 parser->device->collection_size * 2, GFP_KERNEL);
117 if (collection == NULL) {
118 dbg("failed to reallocate collection array");
121 memcpy(collection, parser->device->collection,
122 sizeof(struct hid_collection) *
123 parser->device->collection_size);
124 memset(collection + parser->device->collection_size, 0,
125 sizeof(struct hid_collection) *
126 parser->device->collection_size);
127 kfree(parser->device->collection);
128 parser->device->collection = collection;
129 parser->device->collection_size *= 2;
132 parser->collection_stack[parser->collection_stack_ptr++] =
133 parser->device->maxcollection;
135 collection = parser->device->collection +
136 parser->device->maxcollection++;
137 collection->type = type;
138 collection->usage = usage;
139 collection->level = parser->collection_stack_ptr - 1;
141 if (type == HID_COLLECTION_APPLICATION)
142 parser->device->maxapplication++;
148 * Close a collection.
151 static int close_collection(struct hid_parser *parser)
153 if (!parser->collection_stack_ptr) {
154 dbg("collection stack underflow");
157 parser->collection_stack_ptr--;
162 * Climb up the stack, search for the specified collection type
163 * and return the usage.
166 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
169 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
170 if (parser->device->collection[parser->collection_stack[n]].type == type)
171 return parser->device->collection[parser->collection_stack[n]].usage;
172 return 0; /* we know nothing about this usage type */
176 * Add a usage to the temporary parser table.
179 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
181 if (parser->local.usage_index >= HID_MAX_USAGES) {
182 dbg("usage index exceeded");
185 parser->local.usage[parser->local.usage_index] = usage;
186 parser->local.collection_index[parser->local.usage_index] =
187 parser->collection_stack_ptr ?
188 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
189 parser->local.usage_index++;
194 * Register a new field for this report.
197 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
199 struct hid_report *report;
200 struct hid_field *field;
205 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
206 dbg("hid_register_report failed");
210 if (parser->global.logical_maximum < parser->global.logical_minimum) {
211 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
215 offset = report->size;
216 report->size += parser->global.report_size * parser->global.report_count;
218 if (!parser->local.usage_index) /* Ignore padding fields */
221 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
223 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
226 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
227 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
228 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
230 for (i = 0; i < usages; i++) {
232 /* Duplicate the last usage we parsed if we have excess values */
233 if (i >= parser->local.usage_index)
234 j = parser->local.usage_index - 1;
235 field->usage[i].hid = parser->local.usage[j];
236 field->usage[i].collection_index =
237 parser->local.collection_index[j];
240 field->maxusage = usages;
241 field->flags = flags;
242 field->report_offset = offset;
243 field->report_type = report_type;
244 field->report_size = parser->global.report_size;
245 field->report_count = parser->global.report_count;
246 field->logical_minimum = parser->global.logical_minimum;
247 field->logical_maximum = parser->global.logical_maximum;
248 field->physical_minimum = parser->global.physical_minimum;
249 field->physical_maximum = parser->global.physical_maximum;
250 field->unit_exponent = parser->global.unit_exponent;
251 field->unit = parser->global.unit;
257 * Read data value from item.
260 static u32 item_udata(struct hid_item *item)
262 switch (item->size) {
263 case 1: return item->data.u8;
264 case 2: return item->data.u16;
265 case 4: return item->data.u32;
270 static s32 item_sdata(struct hid_item *item)
272 switch (item->size) {
273 case 1: return item->data.s8;
274 case 2: return item->data.s16;
275 case 4: return item->data.s32;
281 * Process a global item.
284 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
288 case HID_GLOBAL_ITEM_TAG_PUSH:
290 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
291 dbg("global enviroment stack overflow");
295 memcpy(parser->global_stack + parser->global_stack_ptr++,
296 &parser->global, sizeof(struct hid_global));
299 case HID_GLOBAL_ITEM_TAG_POP:
301 if (!parser->global_stack_ptr) {
302 dbg("global enviroment stack underflow");
306 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
307 sizeof(struct hid_global));
310 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
311 parser->global.usage_page = item_udata(item);
314 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
315 parser->global.logical_minimum = item_sdata(item);
318 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
319 if (parser->global.logical_minimum < 0)
320 parser->global.logical_maximum = item_sdata(item);
322 parser->global.logical_maximum = item_udata(item);
325 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
326 parser->global.physical_minimum = item_sdata(item);
329 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
330 if (parser->global.physical_minimum < 0)
331 parser->global.physical_maximum = item_sdata(item);
333 parser->global.physical_maximum = item_udata(item);
336 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
337 parser->global.unit_exponent = item_sdata(item);
340 case HID_GLOBAL_ITEM_TAG_UNIT:
341 parser->global.unit = item_udata(item);
344 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
345 if ((parser->global.report_size = item_udata(item)) > 32) {
346 dbg("invalid report_size %d", parser->global.report_size);
351 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
352 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
353 dbg("invalid report_count %d", parser->global.report_count);
358 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
359 if ((parser->global.report_id = item_udata(item)) == 0) {
360 dbg("report_id 0 is invalid");
366 dbg("unknown global tag 0x%x", item->tag);
372 * Process a local item.
375 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
380 if (item->size == 0) {
381 dbg("item data expected for local item");
385 data = item_udata(item);
389 case HID_LOCAL_ITEM_TAG_DELIMITER:
393 * We treat items before the first delimiter
394 * as global to all usage sets (branch 0).
395 * In the moment we process only these global
396 * items and the first delimiter set.
398 if (parser->local.delimiter_depth != 0) {
399 dbg("nested delimiters");
402 parser->local.delimiter_depth++;
403 parser->local.delimiter_branch++;
405 if (parser->local.delimiter_depth < 1) {
406 dbg("bogus close delimiter");
409 parser->local.delimiter_depth--;
413 case HID_LOCAL_ITEM_TAG_USAGE:
415 if (parser->local.delimiter_branch > 1) {
416 dbg("alternative usage ignored");
421 data = (parser->global.usage_page << 16) + data;
423 return hid_add_usage(parser, data);
425 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
427 if (parser->local.delimiter_branch > 1) {
428 dbg("alternative usage ignored");
433 data = (parser->global.usage_page << 16) + data;
435 parser->local.usage_minimum = data;
438 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
440 if (parser->local.delimiter_branch > 1) {
441 dbg("alternative usage ignored");
446 data = (parser->global.usage_page << 16) + data;
448 for (n = parser->local.usage_minimum; n <= data; n++)
449 if (hid_add_usage(parser, n)) {
450 dbg("hid_add_usage failed\n");
457 dbg("unknown local item tag 0x%x", item->tag);
464 * Process a main item.
467 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
472 data = item_udata(item);
475 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
476 ret = open_collection(parser, data & 0xff);
478 case HID_MAIN_ITEM_TAG_END_COLLECTION:
479 ret = close_collection(parser);
481 case HID_MAIN_ITEM_TAG_INPUT:
482 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
484 case HID_MAIN_ITEM_TAG_OUTPUT:
485 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
487 case HID_MAIN_ITEM_TAG_FEATURE:
488 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
491 dbg("unknown main item tag 0x%x", item->tag);
495 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
501 * Process a reserved item.
504 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
506 dbg("reserved item type, tag 0x%x", item->tag);
511 * Free a report and all registered fields. The field->usage and
512 * field->value table's are allocated behind the field, so we need
513 * only to free(field) itself.
516 static void hid_free_report(struct hid_report *report)
520 for (n = 0; n < report->maxfield; n++)
521 kfree(report->field[n]);
526 * Free a device structure, all reports, and all fields.
529 void hid_free_device(struct hid_device *device)
533 for (i = 0; i < HID_REPORT_TYPES; i++) {
534 struct hid_report_enum *report_enum = device->report_enum + i;
536 for (j = 0; j < 256; j++) {
537 struct hid_report *report = report_enum->report_id_hash[j];
539 hid_free_report(report);
543 kfree(device->rdesc);
544 kfree(device->collection);
547 EXPORT_SYMBOL_GPL(hid_free_device);
550 * Fetch a report description item from the data stream. We support long
551 * items, though they are not used yet.
554 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
558 if ((end - start) <= 0)
563 item->type = (b >> 2) & 3;
564 item->tag = (b >> 4) & 15;
566 if (item->tag == HID_ITEM_TAG_LONG) {
568 item->format = HID_ITEM_FORMAT_LONG;
570 if ((end - start) < 2)
573 item->size = *start++;
574 item->tag = *start++;
576 if ((end - start) < item->size)
579 item->data.longdata = start;
584 item->format = HID_ITEM_FORMAT_SHORT;
587 switch (item->size) {
593 if ((end - start) < 1)
595 item->data.u8 = *start++;
599 if ((end - start) < 2)
601 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
602 start = (__u8 *)((__le16 *)start + 1);
607 if ((end - start) < 4)
609 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
610 start = (__u8 *)((__le32 *)start + 1);
618 * Parse a report description into a hid_device structure. Reports are
619 * enumerated, fields are attached to these reports.
622 struct hid_device *hid_parse_report(__u8 *start, unsigned size)
624 struct hid_device *device;
625 struct hid_parser *parser;
626 struct hid_item item;
629 static int (*dispatch_type[])(struct hid_parser *parser,
630 struct hid_item *item) = {
637 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
640 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
641 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
645 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
647 for (i = 0; i < HID_REPORT_TYPES; i++)
648 INIT_LIST_HEAD(&device->report_enum[i].report_list);
650 if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
651 kfree(device->collection);
655 memcpy(device->rdesc, start, size);
656 device->rsize = size;
658 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
659 kfree(device->rdesc);
660 kfree(device->collection);
664 parser->device = device;
667 while ((start = fetch_item(start, end, &item)) != NULL) {
669 if (item.format != HID_ITEM_FORMAT_SHORT) {
670 dbg("unexpected long global item");
671 kfree(device->collection);
672 hid_free_device(device);
677 if (dispatch_type[item.type](parser, &item)) {
678 dbg("item %u %u %u %u parsing failed\n",
679 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
680 kfree(device->collection);
681 hid_free_device(device);
687 if (parser->collection_stack_ptr) {
688 dbg("unbalanced collection at end of report description");
689 kfree(device->collection);
690 hid_free_device(device);
694 if (parser->local.delimiter_depth) {
695 dbg("unbalanced delimiter at end of report description");
696 kfree(device->collection);
697 hid_free_device(device);
706 dbg("item fetching failed at offset %d\n", (int)(end - start));
707 kfree(device->collection);
708 hid_free_device(device);
712 EXPORT_SYMBOL_GPL(hid_parse_report);
715 * Convert a signed n-bit integer to signed 32-bit integer. Common
716 * cases are done through the compiler, the screwed things has to be
720 static s32 snto32(__u32 value, unsigned n)
723 case 8: return ((__s8)value);
724 case 16: return ((__s16)value);
725 case 32: return ((__s32)value);
727 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
731 * Convert a signed 32-bit integer to a signed n-bit integer.
734 static u32 s32ton(__s32 value, unsigned n)
736 s32 a = value >> (n - 1);
738 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
739 return value & ((1 << n) - 1);
743 * Extract/implement a data field from/to a little endian report (bit array).
745 * Code sort-of follows HID spec:
746 * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
748 * While the USB HID spec allows unlimited length bit fields in "report
749 * descriptors", most devices never use more than 16 bits.
750 * One model of UPS is claimed to report "LINEV" as a 32-bit field.
751 * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
754 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
760 report += offset >> 3; /* adjust byte index */
761 offset &= 7; /* now only need bit offset into one byte */
762 x = get_unaligned((u64 *) report);
764 x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
769 * "implement" : set bits in a little endian bit stream.
770 * Same concepts as "extract" (see comments above).
771 * The data mangled in the bit stream remains in little endian
772 * order the whole time. It make more sense to talk about
773 * endianness of register values by considering a register
774 * a "cached" copy of the little endiad bit stream.
776 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
779 u64 m = (1ULL << n) - 1;
786 report += offset >> 3;
789 x = get_unaligned((u64 *)report);
790 x &= cpu_to_le64(~(m << offset));
791 x |= cpu_to_le64(((u64) value) << offset);
792 put_unaligned(x, (u64 *) report);
796 * Search an array for a value.
799 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
802 if (*array++ == value)
808 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
810 hid_dump_input(usage, value);
811 if (hid->claimed & HID_CLAIMED_INPUT)
812 hidinput_hid_event(hid, field, usage, value);
813 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
814 hid->hiddev_hid_event(hid, field, usage, value);
818 * Analyse a received field, and fetch the data from it. The field
819 * content is stored for next report processing (we do differential
820 * reporting to the layer).
823 void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
826 unsigned count = field->report_count;
827 unsigned offset = field->report_offset;
828 unsigned size = field->report_size;
829 __s32 min = field->logical_minimum;
830 __s32 max = field->logical_maximum;
833 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
836 for (n = 0; n < count; n++) {
838 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
839 extract(data, offset + n * size, size);
841 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
842 && value[n] >= min && value[n] <= max
843 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
847 for (n = 0; n < count; n++) {
849 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
850 hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
854 if (field->value[n] >= min && field->value[n] <= max
855 && field->usage[field->value[n] - min].hid
856 && search(value, field->value[n], count))
857 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
859 if (value[n] >= min && value[n] <= max
860 && field->usage[value[n] - min].hid
861 && search(field->value, value[n], count))
862 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
865 memcpy(field->value, value, count * sizeof(__s32));
869 EXPORT_SYMBOL_GPL(hid_input_field);
872 * Output the field into the report.
875 static void hid_output_field(struct hid_field *field, __u8 *data)
877 unsigned count = field->report_count;
878 unsigned offset = field->report_offset;
879 unsigned size = field->report_size;
882 /* make sure the unused bits in the last byte are zeros */
883 if (count > 0 && size > 0)
884 data[(count*size-1)/8] = 0;
886 for (n = 0; n < count; n++) {
887 if (field->logical_minimum < 0) /* signed values */
888 implement(data, offset + n * size, size, s32ton(field->value[n], size));
889 else /* unsigned values */
890 implement(data, offset + n * size, size, field->value[n]);
898 void hid_output_report(struct hid_report *report, __u8 *data)
903 *data++ = report->id;
905 for (n = 0; n < report->maxfield; n++)
906 hid_output_field(report->field[n], data);
908 EXPORT_SYMBOL_GPL(hid_output_report);
911 * Set a field value. The report this field belongs to has to be
912 * created and transferred to the device, to set this value in the
916 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
918 unsigned size = field->report_size;
920 hid_dump_input(field->usage + offset, value);
922 if (offset >= field->report_count) {
923 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
924 hid_dump_field(field, 8);
927 if (field->logical_minimum < 0) {
928 if (value != snto32(s32ton(value, size), size)) {
929 dbg("value %d is out of range", value);
933 field->value[offset] = value;
936 EXPORT_SYMBOL_GPL(hid_set_field);
938 int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
940 struct hid_report_enum *report_enum = hid->report_enum + type;
941 struct hid_report *report;
952 #ifdef CONFIG_HID_DEBUG
953 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
956 n = 0; /* Normally report number is 0 */
957 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
962 #ifdef CONFIG_HID_DEBUG
965 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size);
966 for (i = 0; i < size; i++)
967 printk(" %02x", data[i]);
972 if (!(report = report_enum->report_id_hash[n])) {
973 dbg("undefined report_id %d received", n);
977 rsize = ((report->size - 1) >> 3) + 1;
980 dbg("report %d is too short, (%d < %d)", report->id, size, rsize);
984 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
985 hid->hiddev_report_event(hid, report);
987 for (n = 0; n < report->maxfield; n++)
988 hid_input_field(hid, report->field[n], data, interrupt);
990 if (hid->claimed & HID_CLAIMED_INPUT)
991 hidinput_report_event(hid, report);
995 EXPORT_SYMBOL_GPL(hid_input_report);
997 MODULE_LICENSE(DRIVER_LICENSE);