2 * $Id: hid-core.c,v 1.8 2001/05/23 12:02:18 vojtech Exp $
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2001 Vojtech Pavlik
7 * USB HID support for Linux
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/list.h>
39 #include <linux/smp_lock.h>
40 #include <linux/spinlock.h>
41 #include <asm/unaligned.h>
42 #include <linux/input.h>
47 #include <linux/usb.h>
50 #include <linux/hiddev.h>
56 #define DRIVER_VERSION "v1.8.1"
57 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
58 #define DRIVER_DESC "USB HID support drivers"
60 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
61 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
64 * Register a new report for a device.
67 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
69 struct hid_report_enum *report_enum = device->report_enum + type;
70 struct hid_report *report;
72 if (report_enum->report_id_hash[id])
73 return report_enum->report_id_hash[id];
75 if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
77 memset(report, 0, sizeof(struct hid_report));
79 if (id != 0) report_enum->numbered = 1;
84 report->device = device;
85 report_enum->report_id_hash[id] = report;
87 list_add_tail(&report->list, &report_enum->report_list);
93 * Register a new field for this report.
96 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
98 struct hid_field *field;
100 if (report->maxfield == HID_MAX_FIELDS) {
101 dbg("too many fields in report");
105 if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
106 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
108 memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
109 + values * sizeof(unsigned));
111 report->field[report->maxfield] = field;
112 field->usage = (struct hid_usage *)(field + 1);
113 field->value = (unsigned *)(field->usage + usages);
114 field->report = report;
115 field->index = report->maxfield++;
121 * Open a collection. The type/usage is pushed on the stack.
124 static int open_collection(struct hid_parser *parser, unsigned type)
126 struct hid_collection *collection;
129 usage = parser->local.usage[0];
131 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
132 dbg("collection stack overflow");
136 if (parser->device->maxcollection == parser->device->collection_size) {
137 collection = kmalloc(sizeof(struct hid_collection) *
138 parser->device->collection_size * 2,
140 if (collection == NULL) {
141 dbg("failed to reallocate collection array");
144 memcpy(collection, parser->device->collection,
145 sizeof(struct hid_collection) *
146 parser->device->collection_size);
147 memset(collection + parser->device->collection_size, 0,
148 sizeof(struct hid_collection) *
149 parser->device->collection_size);
150 kfree(parser->device->collection);
151 parser->device->collection = collection;
152 parser->device->collection_size *= 2;
155 parser->collection_stack[parser->collection_stack_ptr++] =
156 parser->device->maxcollection;
158 collection = parser->device->collection +
159 parser->device->maxcollection++;
161 collection->type = type;
162 collection->usage = usage;
163 collection->level = parser->collection_stack_ptr - 1;
165 if (type == HID_COLLECTION_APPLICATION)
166 parser->device->maxapplication++;
172 * Close a collection.
175 static int close_collection(struct hid_parser *parser)
177 if (!parser->collection_stack_ptr) {
178 dbg("collection stack underflow");
181 parser->collection_stack_ptr--;
186 * Climb up the stack, search for the specified collection type
187 * and return the usage.
190 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
193 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
194 if (parser->device->collection[parser->collection_stack[n]].type == type)
195 return parser->device->collection[parser->collection_stack[n]].usage;
197 return 0; /* we know nothing about this usage type */
201 * Add a usage to the temporary parser table.
204 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
206 if (parser->local.usage_index >= HID_MAX_USAGES) {
207 dbg("usage index exceeded");
210 parser->local.usage[parser->local.usage_index] = usage;
211 parser->local.collection_index[parser->local.usage_index] =
212 parser->collection_stack_ptr ?
213 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
214 parser->local.usage_index++;
220 * Register a new field for this report.
223 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
225 struct hid_report *report;
226 struct hid_field *field;
231 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
232 dbg("hid_register_report failed");
236 if (parser->global.logical_maximum < parser->global.logical_minimum) {
237 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
241 usages = parser->local.usage_index;
243 offset = report->size;
244 report->size += parser->global.report_size * parser->global.report_count;
246 if (usages < parser->global.report_count)
247 usages = parser->global.report_count;
250 return 0; /* ignore padding fields */
252 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
255 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
256 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
257 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
259 for (i = 0; i < usages; i++) {
261 /* Duplicate the last usage we parsed if we have excess values */
262 if (i >= parser->local.usage_index)
263 j = parser->local.usage_index - 1;
264 field->usage[i].hid = parser->local.usage[j];
265 field->usage[i].collection_index =
266 parser->local.collection_index[j];
269 field->maxusage = usages;
270 field->flags = flags;
271 field->report_offset = offset;
272 field->report_type = report_type;
273 field->report_size = parser->global.report_size;
274 field->report_count = parser->global.report_count;
275 field->logical_minimum = parser->global.logical_minimum;
276 field->logical_maximum = parser->global.logical_maximum;
277 field->physical_minimum = parser->global.physical_minimum;
278 field->physical_maximum = parser->global.physical_maximum;
279 field->unit_exponent = parser->global.unit_exponent;
280 field->unit = parser->global.unit;
286 * Read data value from item.
289 static __inline__ __u32 item_udata(struct hid_item *item)
291 switch (item->size) {
292 case 1: return item->data.u8;
293 case 2: return item->data.u16;
294 case 4: return item->data.u32;
299 static __inline__ __s32 item_sdata(struct hid_item *item)
301 switch (item->size) {
302 case 1: return item->data.s8;
303 case 2: return item->data.s16;
304 case 4: return item->data.s32;
310 * Process a global item.
313 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
317 case HID_GLOBAL_ITEM_TAG_PUSH:
319 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
320 dbg("global enviroment stack overflow");
324 memcpy(parser->global_stack + parser->global_stack_ptr++,
325 &parser->global, sizeof(struct hid_global));
328 case HID_GLOBAL_ITEM_TAG_POP:
330 if (!parser->global_stack_ptr) {
331 dbg("global enviroment stack underflow");
335 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
336 sizeof(struct hid_global));
339 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
340 parser->global.usage_page = item_udata(item);
343 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
344 parser->global.logical_minimum = item_sdata(item);
347 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
348 if (parser->global.logical_minimum < 0)
349 parser->global.logical_maximum = item_sdata(item);
351 parser->global.logical_maximum = item_udata(item);
354 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
355 parser->global.physical_minimum = item_sdata(item);
358 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
359 if (parser->global.physical_minimum < 0)
360 parser->global.physical_maximum = item_sdata(item);
362 parser->global.physical_maximum = item_udata(item);
365 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
366 parser->global.unit_exponent = item_udata(item);
369 case HID_GLOBAL_ITEM_TAG_UNIT:
370 parser->global.unit = item_udata(item);
373 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
374 if ((parser->global.report_size = item_udata(item)) > 32) {
375 dbg("invalid report_size %d", parser->global.report_size);
380 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
381 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
382 dbg("invalid report_count %d", parser->global.report_count);
387 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
388 if ((parser->global.report_id = item_udata(item)) == 0) {
389 dbg("report_id 0 is invalid");
395 dbg("unknown global tag 0x%x", item->tag);
401 * Process a local item.
404 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
409 if (item->size == 0) {
410 dbg("item data expected for local item");
414 data = item_udata(item);
418 case HID_LOCAL_ITEM_TAG_DELIMITER:
422 * We treat items before the first delimiter
423 * as global to all usage sets (branch 0).
424 * In the moment we process only these global
425 * items and the first delimiter set.
427 if (parser->local.delimiter_depth != 0) {
428 dbg("nested delimiters");
431 parser->local.delimiter_depth++;
432 parser->local.delimiter_branch++;
434 if (parser->local.delimiter_depth < 1) {
435 dbg("bogus close delimiter");
438 parser->local.delimiter_depth--;
442 case HID_LOCAL_ITEM_TAG_USAGE:
444 if (parser->local.delimiter_branch > 1) {
445 dbg("alternative usage ignored");
450 data = (parser->global.usage_page << 16) + data;
452 return hid_add_usage(parser, data);
454 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
456 if (parser->local.delimiter_branch > 1) {
457 dbg("alternative usage ignored");
462 data = (parser->global.usage_page << 16) + data;
464 parser->local.usage_minimum = data;
467 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
469 if (parser->local.delimiter_branch > 1) {
470 dbg("alternative usage ignored");
475 data = (parser->global.usage_page << 16) + data;
477 for (n = parser->local.usage_minimum; n <= data; n++)
478 if (hid_add_usage(parser, n)) {
479 dbg("hid_add_usage failed\n");
486 dbg("unknown local item tag 0x%x", item->tag);
493 * Process a main item.
496 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
501 data = item_udata(item);
504 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
505 ret = open_collection(parser, data & 0xff);
507 case HID_MAIN_ITEM_TAG_END_COLLECTION:
508 ret = close_collection(parser);
510 case HID_MAIN_ITEM_TAG_INPUT:
511 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
513 case HID_MAIN_ITEM_TAG_OUTPUT:
514 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
516 case HID_MAIN_ITEM_TAG_FEATURE:
517 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
520 dbg("unknown main item tag 0x%x", item->tag);
524 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
530 * Process a reserved item.
533 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
535 dbg("reserved item type, tag 0x%x", item->tag);
540 * Free a report and all registered fields. The field->usage and
541 * field->value table's are allocated behind the field, so we need
542 * only to free(field) itself.
545 static void hid_free_report(struct hid_report *report)
549 for (n = 0; n < report->maxfield; n++)
550 kfree(report->field[n]);
557 * Free a device structure, all reports, and all fields.
560 static void hid_free_device(struct hid_device *device)
564 for (i = 0; i < HID_REPORT_TYPES; i++) {
565 struct hid_report_enum *report_enum = device->report_enum + i;
567 for (j = 0; j < 256; j++) {
568 struct hid_report *report = report_enum->report_id_hash[j];
569 if (report) hid_free_report(report);
573 if (device->rdesc) kfree(device->rdesc);
574 if (device->collection) kfree(device->collection);
578 * Fetch a report description item from the data stream. We support long
579 * items, though they are not used yet.
582 static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
584 if ((end - start) > 0) {
587 item->type = (b >> 2) & 3;
588 item->tag = (b >> 4) & 15;
590 if (item->tag == HID_ITEM_TAG_LONG) {
592 item->format = HID_ITEM_FORMAT_LONG;
594 if ((end - start) >= 2) {
596 item->size = *start++;
597 item->tag = *start++;
599 if ((end - start) >= item->size) {
600 item->data.longdata = start;
607 item->format = HID_ITEM_FORMAT_SHORT;
609 switch (item->size) {
615 if ((end - start) >= 1) {
616 item->data.u8 = *start++;
622 if ((end - start) >= 2) {
623 item->data.u16 = le16_to_cpu(get_unaligned((__u16*)start));
624 start = (__u8 *)((__u16 *)start + 1);
630 if ((end - start) >= 4) {
631 item->data.u32 = le32_to_cpu(get_unaligned((__u32*)start));
632 start = (__u8 *)((__u32 *)start + 1);
642 * Parse a report description into a hid_device structure. Reports are
643 * enumerated, fields are attached to these reports.
646 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
648 struct hid_device *device;
649 struct hid_parser *parser;
650 struct hid_item item;
653 static int (*dispatch_type[])(struct hid_parser *parser,
654 struct hid_item *item) = {
661 if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
663 memset(device, 0, sizeof(struct hid_device));
665 if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
666 HID_DEFAULT_NUM_COLLECTIONS,
671 memset(device->collection, 0, sizeof(struct hid_collection) *
672 HID_DEFAULT_NUM_COLLECTIONS);
673 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
675 for (i = 0; i < HID_REPORT_TYPES; i++)
676 INIT_LIST_HEAD(&device->report_enum[i].report_list);
678 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
679 kfree(device->collection);
683 memcpy(device->rdesc, start, size);
684 device->rsize = size;
686 if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
687 kfree(device->rdesc);
688 kfree(device->collection);
692 memset(parser, 0, sizeof(struct hid_parser));
693 parser->device = device;
696 while ((start = fetch_item(start, end, &item)) != 0) {
697 if (item.format != HID_ITEM_FORMAT_SHORT) {
698 dbg("unexpected long global item");
699 hid_free_device(device);
703 if (dispatch_type[item.type](parser, &item)) {
704 dbg("item %u %u %u %u parsing failed\n",
705 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
706 hid_free_device(device);
712 if (parser->collection_stack_ptr) {
713 dbg("unbalanced collection at end of report description");
714 hid_free_device(device);
718 if (parser->local.delimiter_depth) {
719 dbg("unbalanced delimiter at end of report description");
720 hid_free_device(device);
729 dbg("item fetching failed at offset %d\n", (int)(end - start));
730 hid_free_device(device);
736 * Convert a signed n-bit integer to signed 32-bit integer. Common
737 * cases are done through the compiler, the screwed things has to be
741 static __inline__ __s32 snto32(__u32 value, unsigned n)
744 case 8: return ((__s8)value);
745 case 16: return ((__s16)value);
746 case 32: return ((__s32)value);
748 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
752 * Convert a signed 32-bit integer to a signed n-bit integer.
755 static __inline__ __u32 s32ton(__s32 value, unsigned n)
757 __s32 a = value >> (n - 1);
758 if (a && a != -1) return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
759 return value & ((1 << n) - 1);
763 * Extract/implement a data field from/to a report.
766 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
768 report += (offset >> 5) << 2; offset &= 31;
769 return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
772 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
774 report += (offset >> 5) << 2; offset &= 31;
775 put_unaligned((get_unaligned((__u64*)report)
776 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
777 | cpu_to_le64((__u64)value << offset), (__u64*)report);
781 * Search an array for a value.
784 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
786 while (n--) if (*array++ == value) return 0;
790 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
792 hid_dump_input(usage, value);
793 if (hid->claimed & HID_CLAIMED_INPUT)
794 hidinput_hid_event(hid, field, usage, value);
795 if (hid->claimed & HID_CLAIMED_HIDDEV)
796 hiddev_hid_event(hid, field, usage, value);
801 * Analyse a received field, and fetch the data from it. The field
802 * content is stored for next report processing (we do differential
803 * reporting to the layer).
806 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data)
809 unsigned count = field->report_count;
810 unsigned offset = field->report_offset;
811 unsigned size = field->report_size;
812 __s32 min = field->logical_minimum;
813 __s32 max = field->logical_maximum;
814 __s32 value[count]; /* WARNING: gcc specific */
816 for (n = 0; n < count; n++) {
818 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
819 extract(data, offset + n * size, size);
821 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
822 && value[n] >= min && value[n] <= max
823 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
827 for (n = 0; n < count; n++) {
829 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
831 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
832 if (!value[n]) continue;
834 if (value[n] == field->value[n]) continue;
836 hid_process_event(hid, field, &field->usage[n], value[n]);
840 if (field->value[n] >= min && field->value[n] <= max
841 && field->usage[field->value[n] - min].hid
842 && search(value, field->value[n], count))
843 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0);
845 if (value[n] >= min && value[n] <= max
846 && field->usage[value[n] - min].hid
847 && search(field->value, value[n], count))
848 hid_process_event(hid, field, &field->usage[value[n] - min], 1);
851 memcpy(field->value, value, count * sizeof(__s32));
854 static int hid_input_report(int type, u8 *data, int len, struct hid_device *hid)
856 struct hid_report_enum *report_enum = hid->report_enum + type;
857 struct hid_report *report;
866 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
869 n = 0; /* Normally report number is 0 */
870 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
875 if (!(report = report_enum->report_id_hash[n])) {
876 dbg("undefined report_id %d received", n);
878 printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
879 for (n = 0; n < len; n++)
880 printk(" %02x", data[n]);
887 if (hid->claimed & HID_CLAIMED_HIDDEV)
888 hiddev_report_event(hid, report);
890 size = ((report->size - 1) >> 3) + 1;
895 dbg("report %d is too short, (%d < %d)", report->id, len, size);
900 * Some low-speed devices have large reports and maxpacketsize 8.
901 * We buffer the data in that case and parse it when we got it all.
902 * Works only for unnumbered reports. Doesn't make sense for numbered
903 * reports anyway - then they don't need to be large.
907 if (!(report->data = kmalloc(size, GFP_ATOMIC))) {
908 dbg("couldn't allocate report buffer");
912 if (report->idx + len > size) {
913 dbg("report data buffer overflow");
918 memcpy(report->data + report->idx, data, len);
921 if (report->idx < size)
927 for (n = 0; n < report->maxfield; n++)
928 hid_input_field(hid, report->field[n], data);
935 * Interrupt input handler.
938 static void hid_irq(struct urb *urb)
941 dbg("nonzero status in irq %d", urb->status);
945 hid_input_report(HID_INPUT_REPORT, urb->transfer_buffer, urb->actual_length, urb->context);
949 * hid_read_report() reads in report values without waiting for an irq urb.
952 void hid_read_report(struct hid_device *hid, struct hid_report *report)
954 int len = ((report->size - 1) >> 3) + 1 + hid->report_enum[report->type].numbered;
958 if (hid->quirks & HID_QUIRK_NOGET)
961 if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, data, len)) != len) {
962 dbg("reading report type %d id %d failed len %d read %d", report->type + 1, report->id, len, read);
966 hid_input_report(report->type, data, len, hid);
970 * Output the field into the report.
973 static void hid_output_field(struct hid_field *field, __u8 *data)
975 unsigned count = field->report_count;
976 unsigned offset = field->report_offset;
977 unsigned size = field->report_size;
980 for (n = 0; n < count; n++) {
981 if (field->logical_minimum < 0) /* signed values */
982 implement(data, offset + n * size, size, s32ton(field->value[n], size));
983 else /* unsigned values */
984 implement(data, offset + n * size, size, field->value[n]);
992 void hid_output_report(struct hid_report *report, __u8 *data)
995 for (n = 0; n < report->maxfield; n++)
996 hid_output_field(report->field[n], data);
1000 * Set a field value. The report this field belongs to has to be
1001 * created and transfered to the device, to set this value in the
1005 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1007 unsigned size = field->report_size;
1009 hid_dump_input(field->usage + offset, value);
1011 if (offset >= field->report_count) {
1012 dbg("offset exceeds report_count");
1015 if (field->logical_minimum < 0) {
1016 if (value != snto32(s32ton(value, size), size)) {
1017 dbg("value %d is out of range", value);
1021 if ( (value > field->logical_maximum)
1022 || (value < field->logical_minimum)) {
1023 dbg("value %d is invalid", value);
1026 field->value[offset] = value;
1030 int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1032 struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1033 struct list_head *list = report_enum->report_list.next;
1036 while (list != &report_enum->report_list) {
1037 struct hid_report *report = (struct hid_report *) list;
1039 for (i = 0; i < report->maxfield; i++) {
1040 *field = report->field[i];
1041 for (j = 0; j < (*field)->maxusage; j++)
1042 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1049 static int hid_submit_out(struct hid_device *hid)
1051 hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.wLength);
1052 hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
1053 hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
1054 hid->urbout.dev = hid->dev;
1056 if (usb_submit_urb(&hid->urbout)) {
1057 err("usb_submit_urb(out) failed");
1064 static void hid_ctrl(struct urb *urb)
1066 struct hid_device *hid = urb->context;
1067 unsigned long flags;
1070 warn("ctrl urb status %d received", urb->status);
1072 spin_lock_irqsave(&hid->outlock, flags);
1074 hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1076 if (hid->outhead != hid->outtail) {
1077 if (hid_submit_out(hid)) {
1078 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1080 spin_unlock_irqrestore(&hid->outlock, flags);
1084 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1085 spin_unlock_irqrestore(&hid->outlock, flags);
1088 void hid_write_report(struct hid_device *hid, struct hid_report *report)
1090 unsigned long flags;
1092 if (hid->report_enum[report->type].numbered) {
1093 hid->out[hid->outhead].buffer[0] = report->id;
1094 hid_output_report(report, hid->out[hid->outhead].buffer + 1);
1095 hid->out[hid->outhead].dr.wLength = cpu_to_le16(((report->size + 7) >> 3) + 1);
1097 hid_output_report(report, hid->out[hid->outhead].buffer);
1098 hid->out[hid->outhead].dr.wLength = cpu_to_le16((report->size + 7) >> 3);
1101 hid->out[hid->outhead].dr.wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1103 spin_lock_irqsave(&hid->outlock, flags);
1105 hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1107 if (hid->outhead == hid->outtail)
1108 hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1110 if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1111 if (hid_submit_out(hid))
1112 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1114 spin_unlock_irqrestore(&hid->outlock, flags);
1117 int hid_open(struct hid_device *hid)
1122 hid->urb.dev = hid->dev;
1124 if (usb_submit_urb(&hid->urb))
1130 void hid_close(struct hid_device *hid)
1133 usb_unlink_urb(&hid->urb);
1137 * Initialize all readable reports
1139 void hid_init_reports(struct hid_device *hid)
1142 struct hid_report *report;
1143 struct hid_report_enum *report_enum;
1144 struct list_head *list;
1146 for (i = 0; i < HID_REPORT_TYPES; i++) {
1147 if (i == HID_FEATURE_REPORT || i == HID_INPUT_REPORT) {
1148 report_enum = hid->report_enum + i;
1149 list = report_enum->report_list.next;
1150 while (list != &report_enum->report_list) {
1151 report = (struct hid_report *) list;
1152 hid_read_report(hid, report);
1153 usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
1160 #define USB_VENDOR_ID_WACOM 0x056a
1161 #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
1162 #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1163 #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
1164 #define USB_DEVICE_ID_WACOM_PL 0x0030
1165 #define USB_DEVICE_ID_WACOM_INTUOS2 0x0041
1167 #define USB_VENDOR_ID_KBGEAR 0x084e
1168 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
1170 #define USB_VENDOR_ID_AIPTEK 0x08ca
1171 #define USB_DEVICE_ID_AIPTEK_01 0x0001
1172 #define USB_DEVICE_ID_AIPTEK_10 0x0010
1173 #define USB_DEVICE_ID_AIPTEK_20 0x0020
1174 #define USB_DEVICE_ID_AIPTEK_21 0x0021
1175 #define USB_DEVICE_ID_AIPTEK_22 0x0022
1176 #define USB_DEVICE_ID_AIPTEK_23 0x0023
1177 #define USB_DEVICE_ID_AIPTEK_24 0x0024
1179 #define USB_VENDOR_ID_ATEN 0x0557
1180 #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
1181 #define USB_DEVICE_ID_ATEN_CS124U 0x2202
1182 #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
1183 #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
1185 #define USB_VENDOR_ID_TOPMAX 0x0663
1186 #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
1188 #define USB_VENDOR_ID_HAPP 0x078b
1189 #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
1190 #define USB_DEVICE_ID_UGCI_FLYING 0x0020
1191 #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
1193 #define USB_VENDOR_ID_GRIFFIN 0x077d
1194 #define USB_DEVICE_ID_POWERMATE 0x0410 /* Griffin PowerMate */
1195 #define USB_DEVICE_ID_SOUNDKNOB 0x04AA /* Griffin SoundKnob */
1197 #define USB_VENDOR_ID_ONTRAK 0x0a07
1198 #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
1200 #define USB_VENDOR_ID_TANGTOP 0x0d3d
1201 #define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
1203 #define USB_VENDOR_ID_OKI 0x070a
1204 #define USB_VENDOR_ID_OKI_MULITI 0x0007
1206 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1207 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1209 #define USB_VENDOR_ID_MGE 0x0463
1210 #define USB_DEVICE_ID_MGE_UPS 0xffff
1211 #define USB_DEVICE_ID_MGE_UPS1 0x0001
1213 #define USB_VENDOR_ID_NEC 0x073e
1214 #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
1216 struct hid_blacklist {
1220 } hid_blacklist[] = {
1221 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1222 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1223 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1224 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1225 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1226 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1227 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1228 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1229 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1230 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1231 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1232 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1233 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1234 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1235 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1236 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
1237 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1238 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1239 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1240 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1241 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1242 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1243 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1244 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1245 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1246 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1247 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1248 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1249 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1250 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1251 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1252 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1253 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1254 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1255 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1256 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1257 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1258 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1259 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1260 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1261 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1262 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1263 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1264 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1265 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1266 { USB_VENDOR_ID_OKI, USB_VENDOR_ID_OKI_MULITI, HID_QUIRK_NOGET },
1267 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1268 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1269 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1270 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1274 static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum)
1276 struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
1277 struct hid_descriptor *hdesc;
1278 struct hid_device *hid;
1279 unsigned quirks = 0, rsize = 0;
1283 for (n = 0; hid_blacklist[n].idVendor; n++)
1284 if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1285 (hid_blacklist[n].idProduct == dev->descriptor.idProduct))
1286 quirks = hid_blacklist[n].quirks;
1288 if (quirks & HID_QUIRK_IGNORE)
1291 if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc) && ((!interface->bNumEndpoints) ||
1292 usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc))) {
1293 dbg("class descriptor not present\n");
1297 for (n = 0; n < hdesc->bNumDescriptors; n++)
1298 if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
1299 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1301 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1302 dbg("weird size of report descriptor (%u)", rsize);
1309 if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
1310 dbg("reading report descriptor failed");
1315 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1316 for (n = 0; n < rsize; n++)
1317 printk(" %02x", (unsigned) rdesc[n]);
1321 if (!(hid = hid_parse_report(rdesc, rsize))) {
1322 dbg("parsing report descriptor failed");
1327 hid->quirks = quirks;
1329 for (n = 0; n < interface->bNumEndpoints; n++) {
1331 struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
1332 int pipe, maxp, interval;
1334 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1337 if (!(endpoint->bEndpointAddress & 0x80)) /* Not an input endpoint */
1340 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1341 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1342 interval = endpoint->bInterval;
1343 if (dev->speed == USB_SPEED_HIGH)
1344 interval = 1 << (interval - 1);
1346 FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, interval);
1351 if (n == interface->bNumEndpoints) {
1352 dbg("couldn't find an input interrupt endpoint");
1353 hid_free_device(hid);
1357 spin_lock_init(&hid->outlock);
1359 hid->version = hdesc->bcdHID;
1360 hid->country = hdesc->bCountryCode;
1362 hid->ifnum = interface->bInterfaceNumber;
1364 for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
1365 hid->out[n].dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1366 hid->out[n].dr.bRequest = USB_REQ_SET_REPORT;
1367 hid->out[n].dr.wIndex = cpu_to_le16(hid->ifnum);
1372 if (!(buf = kmalloc(63, GFP_KERNEL)))
1375 if (usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) {
1376 strcat(hid->name, buf);
1377 if (usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
1378 sprintf(hid->name, "%s %s", hid->name, buf);
1380 sprintf(hid->name, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
1384 FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
1385 (void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
1388 * Some devices don't like this and crash. I don't know of any devices
1389 * needing this, so it is disabled for now.
1393 if (interface->bInterfaceSubClass == 1)
1394 usb_set_protocol(dev, hid->ifnum, 1);
1400 static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
1401 const struct usb_device_id *id)
1403 struct hid_device *hid;
1407 dbg("HID probe called for ifnum %d", ifnum);
1409 if (!(hid = usb_hid_configure(dev, ifnum)))
1412 hid_init_reports(hid);
1413 hid_dump_device(hid);
1415 if (!hidinput_connect(hid))
1416 hid->claimed |= HID_CLAIMED_INPUT;
1417 if (!hiddev_connect(hid))
1418 hid->claimed |= HID_CLAIMED_HIDDEV;
1421 if (hid->claimed & HID_CLAIMED_INPUT)
1423 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1425 if (hid->claimed & HID_CLAIMED_HIDDEV)
1426 printk("hiddev%d", hid->minor);
1429 for (i = 0; i < hid->maxcollection; i++) {
1430 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1431 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1432 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1433 c = hid_types[hid->collection[i].usage & 0xffff];
1438 printk(": USB HID v%x.%02x %s [%s] on usb%d:%d.%d\n",
1439 hid->version >> 8, hid->version & 0xff, c, hid->name,
1440 dev->bus->busnum, dev->devnum, ifnum);
1445 static void hid_disconnect(struct usb_device *dev, void *ptr)
1447 struct hid_device *hid = ptr;
1449 dbg("cleanup called");
1450 usb_unlink_urb(&hid->urb);
1451 if (hid->claimed & HID_CLAIMED_INPUT)
1452 hidinput_disconnect(hid);
1453 if (hid->claimed & HID_CLAIMED_HIDDEV)
1454 hiddev_disconnect(hid);
1455 hid_free_device(hid);
1458 static struct usb_device_id hid_usb_ids [] = {
1459 { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
1460 bInterfaceClass: USB_INTERFACE_CLASS_HID },
1461 { } /* Terminating entry */
1464 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1466 static struct usb_driver hid_driver = {
1469 disconnect: hid_disconnect,
1470 id_table: hid_usb_ids,
1473 static int __init hid_init(void)
1476 usb_register(&hid_driver);
1477 info(DRIVER_VERSION " " DRIVER_AUTHOR);
1483 static void __exit hid_exit(void)
1485 usb_deregister(&hid_driver);
1489 module_init(hid_init);
1490 module_exit(hid_exit);
1492 MODULE_AUTHOR( DRIVER_AUTHOR );
1493 MODULE_DESCRIPTION( DRIVER_DESC );
1494 MODULE_LICENSE("GPL");