2 * USB HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.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>
33 #include <linux/usb.h>
36 #include <linux/hiddev.h>
42 #define DRIVER_VERSION "v2.6"
43 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
44 #define DRIVER_DESC "USB HID core driver"
45 #define DRIVER_LICENSE "GPL"
47 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
53 static unsigned int hid_mousepoll_interval;
54 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
55 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
58 * Register a new report for a device.
61 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
63 struct hid_report_enum *report_enum = device->report_enum + type;
64 struct hid_report *report;
66 if (report_enum->report_id_hash[id])
67 return report_enum->report_id_hash[id];
69 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
73 report_enum->numbered = 1;
78 report->device = device;
79 report_enum->report_id_hash[id] = report;
81 list_add_tail(&report->list, &report_enum->report_list);
87 * Register a new field for this report.
90 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
92 struct hid_field *field;
94 if (report->maxfield == HID_MAX_FIELDS) {
95 dbg("too many fields in report");
99 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
100 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
102 field->index = report->maxfield++;
103 report->field[field->index] = field;
104 field->usage = (struct hid_usage *)(field + 1);
105 field->value = (unsigned *)(field->usage + usages);
106 field->report = report;
112 * Open a collection. The type/usage is pushed on the stack.
115 static int open_collection(struct hid_parser *parser, unsigned type)
117 struct hid_collection *collection;
120 usage = parser->local.usage[0];
122 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
123 dbg("collection stack overflow");
127 if (parser->device->maxcollection == parser->device->collection_size) {
128 collection = kmalloc(sizeof(struct hid_collection) *
129 parser->device->collection_size * 2, GFP_KERNEL);
130 if (collection == NULL) {
131 dbg("failed to reallocate collection array");
134 memcpy(collection, parser->device->collection,
135 sizeof(struct hid_collection) *
136 parser->device->collection_size);
137 memset(collection + parser->device->collection_size, 0,
138 sizeof(struct hid_collection) *
139 parser->device->collection_size);
140 kfree(parser->device->collection);
141 parser->device->collection = collection;
142 parser->device->collection_size *= 2;
145 parser->collection_stack[parser->collection_stack_ptr++] =
146 parser->device->maxcollection;
148 collection = parser->device->collection +
149 parser->device->maxcollection++;
150 collection->type = type;
151 collection->usage = usage;
152 collection->level = parser->collection_stack_ptr - 1;
154 if (type == HID_COLLECTION_APPLICATION)
155 parser->device->maxapplication++;
161 * Close a collection.
164 static int close_collection(struct hid_parser *parser)
166 if (!parser->collection_stack_ptr) {
167 dbg("collection stack underflow");
170 parser->collection_stack_ptr--;
175 * Climb up the stack, search for the specified collection type
176 * and return the usage.
179 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
182 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
183 if (parser->device->collection[parser->collection_stack[n]].type == type)
184 return parser->device->collection[parser->collection_stack[n]].usage;
185 return 0; /* we know nothing about this usage type */
189 * Add a usage to the temporary parser table.
192 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
194 if (parser->local.usage_index >= HID_MAX_USAGES) {
195 dbg("usage index exceeded");
198 parser->local.usage[parser->local.usage_index] = usage;
199 parser->local.collection_index[parser->local.usage_index] =
200 parser->collection_stack_ptr ?
201 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
202 parser->local.usage_index++;
207 * Register a new field for this report.
210 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
212 struct hid_report *report;
213 struct hid_field *field;
218 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
219 dbg("hid_register_report failed");
223 if (parser->global.logical_maximum < parser->global.logical_minimum) {
224 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
228 offset = report->size;
229 report->size += parser->global.report_size * parser->global.report_count;
231 if (!parser->local.usage_index) /* Ignore padding fields */
234 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
236 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
239 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
240 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
241 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
243 for (i = 0; i < usages; i++) {
245 /* Duplicate the last usage we parsed if we have excess values */
246 if (i >= parser->local.usage_index)
247 j = parser->local.usage_index - 1;
248 field->usage[i].hid = parser->local.usage[j];
249 field->usage[i].collection_index =
250 parser->local.collection_index[j];
253 field->maxusage = usages;
254 field->flags = flags;
255 field->report_offset = offset;
256 field->report_type = report_type;
257 field->report_size = parser->global.report_size;
258 field->report_count = parser->global.report_count;
259 field->logical_minimum = parser->global.logical_minimum;
260 field->logical_maximum = parser->global.logical_maximum;
261 field->physical_minimum = parser->global.physical_minimum;
262 field->physical_maximum = parser->global.physical_maximum;
263 field->unit_exponent = parser->global.unit_exponent;
264 field->unit = parser->global.unit;
270 * Read data value from item.
273 static __inline__ __u32 item_udata(struct hid_item *item)
275 switch (item->size) {
276 case 1: return item->data.u8;
277 case 2: return item->data.u16;
278 case 4: return item->data.u32;
283 static __inline__ __s32 item_sdata(struct hid_item *item)
285 switch (item->size) {
286 case 1: return item->data.s8;
287 case 2: return item->data.s16;
288 case 4: return item->data.s32;
294 * Process a global item.
297 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
301 case HID_GLOBAL_ITEM_TAG_PUSH:
303 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
304 dbg("global enviroment stack overflow");
308 memcpy(parser->global_stack + parser->global_stack_ptr++,
309 &parser->global, sizeof(struct hid_global));
312 case HID_GLOBAL_ITEM_TAG_POP:
314 if (!parser->global_stack_ptr) {
315 dbg("global enviroment stack underflow");
319 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
320 sizeof(struct hid_global));
323 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
324 parser->global.usage_page = item_udata(item);
327 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
328 parser->global.logical_minimum = item_sdata(item);
331 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
332 if (parser->global.logical_minimum < 0)
333 parser->global.logical_maximum = item_sdata(item);
335 parser->global.logical_maximum = item_udata(item);
338 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
339 parser->global.physical_minimum = item_sdata(item);
342 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
343 if (parser->global.physical_minimum < 0)
344 parser->global.physical_maximum = item_sdata(item);
346 parser->global.physical_maximum = item_udata(item);
349 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
350 parser->global.unit_exponent = item_sdata(item);
353 case HID_GLOBAL_ITEM_TAG_UNIT:
354 parser->global.unit = item_udata(item);
357 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
358 if ((parser->global.report_size = item_udata(item)) > 32) {
359 dbg("invalid report_size %d", parser->global.report_size);
364 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
365 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
366 dbg("invalid report_count %d", parser->global.report_count);
371 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
372 if ((parser->global.report_id = item_udata(item)) == 0) {
373 dbg("report_id 0 is invalid");
379 dbg("unknown global tag 0x%x", item->tag);
385 * Process a local item.
388 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
393 if (item->size == 0) {
394 dbg("item data expected for local item");
398 data = item_udata(item);
402 case HID_LOCAL_ITEM_TAG_DELIMITER:
406 * We treat items before the first delimiter
407 * as global to all usage sets (branch 0).
408 * In the moment we process only these global
409 * items and the first delimiter set.
411 if (parser->local.delimiter_depth != 0) {
412 dbg("nested delimiters");
415 parser->local.delimiter_depth++;
416 parser->local.delimiter_branch++;
418 if (parser->local.delimiter_depth < 1) {
419 dbg("bogus close delimiter");
422 parser->local.delimiter_depth--;
426 case HID_LOCAL_ITEM_TAG_USAGE:
428 if (parser->local.delimiter_branch > 1) {
429 dbg("alternative usage ignored");
434 data = (parser->global.usage_page << 16) + data;
436 return hid_add_usage(parser, data);
438 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
440 if (parser->local.delimiter_branch > 1) {
441 dbg("alternative usage ignored");
446 data = (parser->global.usage_page << 16) + data;
448 parser->local.usage_minimum = data;
451 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
453 if (parser->local.delimiter_branch > 1) {
454 dbg("alternative usage ignored");
459 data = (parser->global.usage_page << 16) + data;
461 for (n = parser->local.usage_minimum; n <= data; n++)
462 if (hid_add_usage(parser, n)) {
463 dbg("hid_add_usage failed\n");
470 dbg("unknown local item tag 0x%x", item->tag);
477 * Process a main item.
480 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
485 data = item_udata(item);
488 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
489 ret = open_collection(parser, data & 0xff);
491 case HID_MAIN_ITEM_TAG_END_COLLECTION:
492 ret = close_collection(parser);
494 case HID_MAIN_ITEM_TAG_INPUT:
495 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
497 case HID_MAIN_ITEM_TAG_OUTPUT:
498 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
500 case HID_MAIN_ITEM_TAG_FEATURE:
501 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
504 dbg("unknown main item tag 0x%x", item->tag);
508 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
514 * Process a reserved item.
517 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
519 dbg("reserved item type, tag 0x%x", item->tag);
524 * Free a report and all registered fields. The field->usage and
525 * field->value table's are allocated behind the field, so we need
526 * only to free(field) itself.
529 static void hid_free_report(struct hid_report *report)
533 for (n = 0; n < report->maxfield; n++)
534 kfree(report->field[n]);
539 * Free a device structure, all reports, and all fields.
542 static void hid_free_device(struct hid_device *device)
548 for (i = 0; i < HID_REPORT_TYPES; i++) {
549 struct hid_report_enum *report_enum = device->report_enum + i;
551 for (j = 0; j < 256; j++) {
552 struct hid_report *report = report_enum->report_id_hash[j];
554 hid_free_report(report);
558 kfree(device->rdesc);
563 * Fetch a report description item from the data stream. We support long
564 * items, though they are not used yet.
567 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
571 if ((end - start) <= 0)
576 item->type = (b >> 2) & 3;
577 item->tag = (b >> 4) & 15;
579 if (item->tag == HID_ITEM_TAG_LONG) {
581 item->format = HID_ITEM_FORMAT_LONG;
583 if ((end - start) < 2)
586 item->size = *start++;
587 item->tag = *start++;
589 if ((end - start) < item->size)
592 item->data.longdata = start;
597 item->format = HID_ITEM_FORMAT_SHORT;
600 switch (item->size) {
606 if ((end - start) < 1)
608 item->data.u8 = *start++;
612 if ((end - start) < 2)
614 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
615 start = (__u8 *)((__le16 *)start + 1);
620 if ((end - start) < 4)
622 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
623 start = (__u8 *)((__le32 *)start + 1);
631 * Parse a report description into a hid_device structure. Reports are
632 * enumerated, fields are attached to these reports.
635 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
637 struct hid_device *device;
638 struct hid_parser *parser;
639 struct hid_item item;
642 static int (*dispatch_type[])(struct hid_parser *parser,
643 struct hid_item *item) = {
650 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
653 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
654 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
658 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
660 for (i = 0; i < HID_REPORT_TYPES; i++)
661 INIT_LIST_HEAD(&device->report_enum[i].report_list);
663 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
664 kfree(device->collection);
668 memcpy(device->rdesc, start, size);
669 device->rsize = size;
671 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
672 kfree(device->rdesc);
673 kfree(device->collection);
677 parser->device = device;
680 while ((start = fetch_item(start, end, &item)) != NULL) {
682 if (item.format != HID_ITEM_FORMAT_SHORT) {
683 dbg("unexpected long global item");
684 kfree(device->collection);
685 hid_free_device(device);
690 if (dispatch_type[item.type](parser, &item)) {
691 dbg("item %u %u %u %u parsing failed\n",
692 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
693 kfree(device->collection);
694 hid_free_device(device);
700 if (parser->collection_stack_ptr) {
701 dbg("unbalanced collection at end of report description");
702 kfree(device->collection);
703 hid_free_device(device);
707 if (parser->local.delimiter_depth) {
708 dbg("unbalanced delimiter at end of report description");
709 kfree(device->collection);
710 hid_free_device(device);
719 dbg("item fetching failed at offset %d\n", (int)(end - start));
720 kfree(device->collection);
721 hid_free_device(device);
727 * Convert a signed n-bit integer to signed 32-bit integer. Common
728 * cases are done through the compiler, the screwed things has to be
732 static __inline__ __s32 snto32(__u32 value, unsigned n)
735 case 8: return ((__s8)value);
736 case 16: return ((__s16)value);
737 case 32: return ((__s32)value);
739 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
743 * Convert a signed 32-bit integer to a signed n-bit integer.
746 static __inline__ __u32 s32ton(__s32 value, unsigned n)
748 __s32 a = value >> (n - 1);
750 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
751 return value & ((1 << n) - 1);
755 * Extract/implement a data field from/to a report.
758 static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
760 report += (offset >> 5) << 2; offset &= 31;
761 return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1ULL << n) - 1);
764 static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
766 report += (offset >> 5) << 2; offset &= 31;
767 put_unaligned((get_unaligned((__le64*)report)
768 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
769 | cpu_to_le64((__u64)value << offset), (__le64*)report);
773 * Search an array for a value.
776 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
779 if (*array++ == value)
785 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt, struct pt_regs *regs)
787 hid_dump_input(usage, value);
788 if (hid->claimed & HID_CLAIMED_INPUT)
789 hidinput_hid_event(hid, field, usage, value, regs);
790 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
791 hiddev_hid_event(hid, field, usage, value, regs);
795 * Analyse a received field, and fetch the data from it. The field
796 * content is stored for next report processing (we do differential
797 * reporting to the layer).
800 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt, struct pt_regs *regs)
803 unsigned count = field->report_count;
804 unsigned offset = field->report_offset;
805 unsigned size = field->report_size;
806 __s32 min = field->logical_minimum;
807 __s32 max = field->logical_maximum;
810 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
813 for (n = 0; n < count; n++) {
815 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
816 extract(data, offset + n * size, size);
818 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
819 && value[n] >= min && value[n] <= max
820 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
824 for (n = 0; n < count; n++) {
826 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
827 hid_process_event(hid, field, &field->usage[n], value[n], interrupt, regs);
831 if (field->value[n] >= min && field->value[n] <= max
832 && field->usage[field->value[n] - min].hid
833 && search(value, field->value[n], count))
834 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt, regs);
836 if (value[n] >= min && value[n] <= max
837 && field->usage[value[n] - min].hid
838 && search(field->value, value[n], count))
839 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt, regs);
842 memcpy(field->value, value, count * sizeof(__s32));
847 static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_regs *regs)
849 struct hid_device *hid = urb->context;
850 struct hid_report_enum *report_enum = hid->report_enum + type;
851 u8 *data = urb->transfer_buffer;
852 int len = urb->actual_length;
853 struct hid_report *report;
862 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
865 n = 0; /* Normally report number is 0 */
866 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
874 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
875 for (i = 0; i < len; i++)
876 printk(" %02x", data[i]);
881 if (!(report = report_enum->report_id_hash[n])) {
882 dbg("undefined report_id %d received", n);
886 size = ((report->size - 1) >> 3) + 1;
889 dbg("report %d is too short, (%d < %d)", report->id, len, size);
890 memset(data + len, 0, size - len);
893 if (hid->claimed & HID_CLAIMED_HIDDEV)
894 hiddev_report_event(hid, report);
896 for (n = 0; n < report->maxfield; n++)
897 hid_input_field(hid, report->field[n], data, interrupt, regs);
899 if (hid->claimed & HID_CLAIMED_INPUT)
900 hidinput_report_event(hid, report);
906 * Input submission and I/O error handler.
909 static void hid_io_error(struct hid_device *hid);
911 /* Start up the input URB */
912 static int hid_start_in(struct hid_device *hid)
917 spin_lock_irqsave(&hid->inlock, flags);
918 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
919 !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
920 rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
922 clear_bit(HID_IN_RUNNING, &hid->iofl);
924 spin_unlock_irqrestore(&hid->inlock, flags);
928 /* I/O retry timer routine */
929 static void hid_retry_timeout(unsigned long _hid)
931 struct hid_device *hid = (struct hid_device *) _hid;
933 dev_dbg(&hid->intf->dev, "retrying intr urb\n");
934 if (hid_start_in(hid))
938 /* Workqueue routine to reset the device */
939 static void hid_reset(void *_hid)
941 struct hid_device *hid = (struct hid_device *) _hid;
944 dev_dbg(&hid->intf->dev, "resetting device\n");
945 rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
947 rc = usb_reset_composite_device(hid->dev, hid->intf);
949 usb_unlock_device(hid->dev);
951 clear_bit(HID_RESET_PENDING, &hid->iofl);
955 if (!test_bit(HID_IN_RUNNING, &hid->iofl))
959 err("can't reset device, %s-%s/input%d, status %d",
960 hid->dev->bus->bus_name,
971 /* Main I/O error handler */
972 static void hid_io_error(struct hid_device *hid)
976 spin_lock_irqsave(&hid->inlock, flags);
978 /* Stop when disconnected */
979 if (usb_get_intfdata(hid->intf) == NULL)
982 /* When an error occurs, retry at increasing intervals */
983 if (hid->retry_delay == 0) {
984 hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
985 hid->stop_retry = jiffies + msecs_to_jiffies(1000);
986 } else if (hid->retry_delay < 100)
987 hid->retry_delay *= 2;
989 if (time_after(jiffies, hid->stop_retry)) {
991 /* Retries failed, so do a port reset */
992 if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
993 if (schedule_work(&hid->reset_work))
995 clear_bit(HID_RESET_PENDING, &hid->iofl);
999 mod_timer(&hid->io_retry,
1000 jiffies + msecs_to_jiffies(hid->retry_delay));
1002 spin_unlock_irqrestore(&hid->inlock, flags);
1006 * Input interrupt completion handler.
1009 static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
1011 struct hid_device *hid = urb->context;
1014 switch (urb->status) {
1015 case 0: /* success */
1016 hid->retry_delay = 0;
1017 hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
1019 case -ECONNRESET: /* unlink */
1021 case -ESHUTDOWN: /* unplug */
1022 clear_bit(HID_IN_RUNNING, &hid->iofl);
1024 case -EILSEQ: /* protocol error or unplug */
1025 case -EPROTO: /* protocol error or unplug */
1026 case -ETIMEDOUT: /* NAK */
1027 clear_bit(HID_IN_RUNNING, &hid->iofl);
1030 default: /* error */
1031 warn("input irq status %d received", urb->status);
1034 status = usb_submit_urb(urb, SLAB_ATOMIC);
1036 clear_bit(HID_IN_RUNNING, &hid->iofl);
1037 if (status != -EPERM) {
1038 err("can't resubmit intr, %s-%s/input%d, status %d",
1039 hid->dev->bus->bus_name,
1041 hid->ifnum, status);
1048 * Output the field into the report.
1051 static void hid_output_field(struct hid_field *field, __u8 *data)
1053 unsigned count = field->report_count;
1054 unsigned offset = field->report_offset;
1055 unsigned size = field->report_size;
1058 for (n = 0; n < count; n++) {
1059 if (field->logical_minimum < 0) /* signed values */
1060 implement(data, offset + n * size, size, s32ton(field->value[n], size));
1061 else /* unsigned values */
1062 implement(data, offset + n * size, size, field->value[n]);
1070 static void hid_output_report(struct hid_report *report, __u8 *data)
1075 *data++ = report->id;
1077 for (n = 0; n < report->maxfield; n++)
1078 hid_output_field(report->field[n], data);
1082 * Set a field value. The report this field belongs to has to be
1083 * created and transferred to the device, to set this value in the
1087 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1089 unsigned size = field->report_size;
1091 hid_dump_input(field->usage + offset, value);
1093 if (offset >= field->report_count) {
1094 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
1095 hid_dump_field(field, 8);
1098 if (field->logical_minimum < 0) {
1099 if (value != snto32(s32ton(value, size), size)) {
1100 dbg("value %d is out of range", value);
1104 field->value[offset] = value;
1109 * Find a report field with a specified HID usage.
1112 struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
1114 struct hid_report *report;
1117 list_for_each_entry(report, &hid->report_enum[type].report_list, list)
1118 for (i = 0; i < report->maxfield; i++)
1119 if (report->field[i]->logical == wanted_usage)
1120 return report->field[i];
1124 static int hid_submit_out(struct hid_device *hid)
1126 struct hid_report *report;
1128 report = hid->out[hid->outtail];
1130 hid_output_report(report, hid->outbuf);
1131 hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1132 hid->urbout->dev = hid->dev;
1134 dbg("submitting out urb");
1136 if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
1137 err("usb_submit_urb(out) failed");
1144 static int hid_submit_ctrl(struct hid_device *hid)
1146 struct hid_report *report;
1150 report = hid->ctrl[hid->ctrltail].report;
1151 dir = hid->ctrl[hid->ctrltail].dir;
1153 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1154 if (dir == USB_DIR_OUT) {
1155 hid_output_report(report, hid->ctrlbuf);
1156 hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
1157 hid->urbctrl->transfer_buffer_length = len;
1159 int maxpacket, padlen;
1161 hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
1162 maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
1163 if (maxpacket > 0) {
1164 padlen = (len + maxpacket - 1) / maxpacket;
1165 padlen *= maxpacket;
1166 if (padlen > hid->bufsize)
1167 padlen = hid->bufsize;
1170 hid->urbctrl->transfer_buffer_length = padlen;
1172 hid->urbctrl->dev = hid->dev;
1174 hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
1175 hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
1176 hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1177 hid->cr->wIndex = cpu_to_le16(hid->ifnum);
1178 hid->cr->wLength = cpu_to_le16(len);
1180 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
1181 hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
1182 hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
1184 if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
1185 err("usb_submit_urb(ctrl) failed");
1193 * Output interrupt completion handler.
1196 static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1198 struct hid_device *hid = urb->context;
1199 unsigned long flags;
1202 switch (urb->status) {
1203 case 0: /* success */
1205 case -ESHUTDOWN: /* unplug */
1207 case -EILSEQ: /* protocol error or unplug */
1208 case -EPROTO: /* protocol error or unplug */
1209 case -ECONNRESET: /* unlink */
1212 default: /* error */
1213 warn("output irq status %d received", urb->status);
1216 spin_lock_irqsave(&hid->outlock, flags);
1219 hid->outtail = hid->outhead;
1221 hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1223 if (hid->outhead != hid->outtail) {
1224 if (hid_submit_out(hid)) {
1225 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1226 wake_up(&hid->wait);
1228 spin_unlock_irqrestore(&hid->outlock, flags);
1232 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1233 spin_unlock_irqrestore(&hid->outlock, flags);
1234 wake_up(&hid->wait);
1238 * Control pipe completion handler.
1241 static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1243 struct hid_device *hid = urb->context;
1244 unsigned long flags;
1247 spin_lock_irqsave(&hid->ctrllock, flags);
1249 switch (urb->status) {
1250 case 0: /* success */
1251 if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
1252 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
1254 case -ESHUTDOWN: /* unplug */
1256 case -EILSEQ: /* protocol error or unplug */
1257 case -EPROTO: /* protocol error or unplug */
1258 case -ECONNRESET: /* unlink */
1260 case -EPIPE: /* report not available */
1262 default: /* error */
1263 warn("ctrl urb status %d received", urb->status);
1267 hid->ctrltail = hid->ctrlhead;
1269 hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1271 if (hid->ctrlhead != hid->ctrltail) {
1272 if (hid_submit_ctrl(hid)) {
1273 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1274 wake_up(&hid->wait);
1276 spin_unlock_irqrestore(&hid->ctrllock, flags);
1280 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1281 spin_unlock_irqrestore(&hid->ctrllock, flags);
1282 wake_up(&hid->wait);
1285 void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
1288 unsigned long flags;
1290 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
1293 if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1295 spin_lock_irqsave(&hid->outlock, flags);
1297 if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
1298 spin_unlock_irqrestore(&hid->outlock, flags);
1299 warn("output queue full");
1303 hid->out[hid->outhead] = report;
1304 hid->outhead = head;
1306 if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1307 if (hid_submit_out(hid))
1308 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1310 spin_unlock_irqrestore(&hid->outlock, flags);
1314 spin_lock_irqsave(&hid->ctrllock, flags);
1316 if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
1317 spin_unlock_irqrestore(&hid->ctrllock, flags);
1318 warn("control queue full");
1322 hid->ctrl[hid->ctrlhead].report = report;
1323 hid->ctrl[hid->ctrlhead].dir = dir;
1324 hid->ctrlhead = head;
1326 if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
1327 if (hid_submit_ctrl(hid))
1328 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1330 spin_unlock_irqrestore(&hid->ctrllock, flags);
1333 int hid_wait_io(struct hid_device *hid)
1335 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
1336 !test_bit(HID_OUT_RUNNING, &hid->iofl)),
1338 dbg("timeout waiting for ctrl or out queue to clear");
1345 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
1347 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1348 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
1349 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
1352 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1353 unsigned char type, void *buf, int size)
1355 int result, retries = 4;
1357 memset(buf,0,size); // Make sure we parse really received data
1360 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1361 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
1362 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
1364 } while (result < size && retries);
1368 int hid_open(struct hid_device *hid)
1371 if (hid_start_in(hid))
1376 void hid_close(struct hid_device *hid)
1379 usb_kill_urb(hid->urbin);
1382 #define USB_VENDOR_ID_PANJIT 0x134c
1385 * Initialize all reports
1388 void hid_init_reports(struct hid_device *hid)
1390 struct hid_report *report;
1393 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
1394 hid_submit_report(hid, report, USB_DIR_IN);
1396 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
1397 hid_submit_report(hid, report, USB_DIR_IN);
1400 ret = hid_wait_io(hid);
1403 if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
1404 usb_kill_urb(hid->urbctrl);
1405 if (test_bit(HID_OUT_RUNNING, &hid->iofl))
1406 usb_kill_urb(hid->urbout);
1407 ret = hid_wait_io(hid);
1411 warn("timeout initializing reports");
1414 #define USB_VENDOR_ID_WACOM 0x056a
1415 #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
1416 #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1417 #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
1418 #define USB_DEVICE_ID_WACOM_PL 0x0030
1419 #define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
1420 #define USB_DEVICE_ID_WACOM_VOLITO 0x0060
1421 #define USB_DEVICE_ID_WACOM_PTU 0x0003
1422 #define USB_DEVICE_ID_WACOM_INTUOS3 0x00B0
1423 #define USB_DEVICE_ID_WACOM_CINTIQ 0x003F
1424 #define USB_DEVICE_ID_WACOM_DTF 0x00C0
1426 #define USB_VENDOR_ID_ACECAD 0x0460
1427 #define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
1428 #define USB_DEVICE_ID_ACECAD_302 0x0008
1430 #define USB_VENDOR_ID_KBGEAR 0x084e
1431 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
1433 #define USB_VENDOR_ID_AIPTEK 0x08ca
1434 #define USB_DEVICE_ID_AIPTEK_01 0x0001
1435 #define USB_DEVICE_ID_AIPTEK_10 0x0010
1436 #define USB_DEVICE_ID_AIPTEK_20 0x0020
1437 #define USB_DEVICE_ID_AIPTEK_21 0x0021
1438 #define USB_DEVICE_ID_AIPTEK_22 0x0022
1439 #define USB_DEVICE_ID_AIPTEK_23 0x0023
1440 #define USB_DEVICE_ID_AIPTEK_24 0x0024
1442 #define USB_VENDOR_ID_GRIFFIN 0x077d
1443 #define USB_DEVICE_ID_POWERMATE 0x0410
1444 #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
1446 #define USB_VENDOR_ID_ATEN 0x0557
1447 #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
1448 #define USB_DEVICE_ID_ATEN_CS124U 0x2202
1449 #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
1450 #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
1451 #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
1453 #define USB_VENDOR_ID_TOPMAX 0x0663
1454 #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
1456 #define USB_VENDOR_ID_HAPP 0x078b
1457 #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
1458 #define USB_DEVICE_ID_UGCI_FLYING 0x0020
1459 #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
1461 #define USB_VENDOR_ID_MGE 0x0463
1462 #define USB_DEVICE_ID_MGE_UPS 0xffff
1463 #define USB_DEVICE_ID_MGE_UPS1 0x0001
1465 #define USB_VENDOR_ID_ONTRAK 0x0a07
1466 #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
1468 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1469 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1471 #define USB_VENDOR_ID_A4TECH 0x09da
1472 #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
1474 #define USB_VENDOR_ID_AASHIMA 0x06d6
1475 #define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
1476 #define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
1478 #define USB_VENDOR_ID_CYPRESS 0x04b4
1479 #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
1480 #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
1481 #define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
1483 #define USB_VENDOR_ID_BERKSHIRE 0x0c98
1484 #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
1486 #define USB_VENDOR_ID_ALPS 0x0433
1487 #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
1489 #define USB_VENDOR_ID_SAITEK 0x06a3
1490 #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
1492 #define USB_VENDOR_ID_NEC 0x073e
1493 #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
1495 #define USB_VENDOR_ID_CHIC 0x05fe
1496 #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
1498 #define USB_VENDOR_ID_GLAB 0x06c2
1499 #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
1500 #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
1501 #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
1502 #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
1503 #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
1505 #define USB_VENDOR_ID_WISEGROUP 0x0925
1506 #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
1507 #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
1508 #define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
1510 #define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
1511 #define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
1513 #define USB_VENDOR_ID_CODEMERCS 0x07c0
1514 #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
1515 #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
1516 #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
1517 #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
1519 #define USB_VENDOR_ID_DELORME 0x1163
1520 #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
1521 #define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
1523 #define USB_VENDOR_ID_MCC 0x09db
1524 #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
1525 #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
1527 #define USB_VENDOR_ID_VERNIER 0x08f7
1528 #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
1529 #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
1530 #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
1531 #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
1533 #define USB_VENDOR_ID_LD 0x0f11
1534 #define USB_DEVICE_ID_LD_CASSY 0x1000
1535 #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
1536 #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
1537 #define USB_DEVICE_ID_LD_JWM 0x1080
1538 #define USB_DEVICE_ID_LD_DMMP 0x1081
1539 #define USB_DEVICE_ID_LD_UMIP 0x1090
1540 #define USB_DEVICE_ID_LD_XRAY1 0x1100
1541 #define USB_DEVICE_ID_LD_XRAY2 0x1101
1542 #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
1543 #define USB_DEVICE_ID_LD_COM3LAB 0x2000
1544 #define USB_DEVICE_ID_LD_TELEPORT 0x2010
1545 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
1546 #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
1547 #define USB_DEVICE_ID_LD_MACHINETEST 0x2040
1549 #define USB_VENDOR_ID_APPLE 0x05ac
1550 #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
1552 #define USB_VENDOR_ID_CHERRY 0x046a
1553 #define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
1555 #define USB_VENDOR_ID_YEALINK 0x6993
1556 #define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
1558 * Alphabetically sorted blacklist by quirk type.
1561 static const struct hid_blacklist {
1565 } hid_blacklist[] = {
1567 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1568 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1569 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1570 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1571 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1572 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1573 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1574 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
1575 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
1576 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
1577 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
1578 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
1579 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
1580 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
1581 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
1582 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
1583 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1584 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1585 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1586 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
1587 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
1588 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
1589 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1590 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1591 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1592 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
1593 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
1594 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
1595 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
1596 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
1597 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
1598 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
1599 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
1600 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
1601 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
1602 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
1603 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
1604 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
1605 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
1606 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
1607 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
1608 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1609 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1610 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1611 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1612 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1613 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1614 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1615 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1616 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
1617 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
1618 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
1619 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
1620 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1621 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1622 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1623 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1624 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1625 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1626 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1627 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1628 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1629 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1630 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1631 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1632 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1633 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1634 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1635 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1636 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1637 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 7, HID_QUIRK_IGNORE },
1638 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 8, HID_QUIRK_IGNORE },
1639 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 9, HID_QUIRK_IGNORE },
1640 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1641 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1642 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1643 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1644 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1645 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1646 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
1647 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 1, HID_QUIRK_IGNORE },
1648 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 2, HID_QUIRK_IGNORE },
1649 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 3, HID_QUIRK_IGNORE },
1650 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 4, HID_QUIRK_IGNORE },
1651 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 5, HID_QUIRK_IGNORE },
1652 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 6, HID_QUIRK_IGNORE },
1653 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
1654 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
1655 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
1656 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
1657 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 3, HID_QUIRK_IGNORE },
1658 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 4, HID_QUIRK_IGNORE },
1659 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 5, HID_QUIRK_IGNORE },
1660 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_CINTIQ, HID_QUIRK_IGNORE },
1661 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_DTF, HID_QUIRK_IGNORE },
1662 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_DTF + 3, HID_QUIRK_IGNORE },
1663 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1664 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1665 { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
1667 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
1668 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
1670 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1671 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1672 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1673 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1674 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1675 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
1676 { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
1678 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
1679 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
1680 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
1682 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
1683 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
1684 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
1685 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
1686 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1687 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1688 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1689 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1690 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
1691 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1693 { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
1695 { USB_VENDOR_ID_APPLE, 0x020E, HID_QUIRK_POWERBOOK_HAS_FN },
1696 { USB_VENDOR_ID_APPLE, 0x020F, HID_QUIRK_POWERBOOK_HAS_FN },
1697 { USB_VENDOR_ID_APPLE, 0x0214, HID_QUIRK_POWERBOOK_HAS_FN },
1698 { USB_VENDOR_ID_APPLE, 0x0215, HID_QUIRK_POWERBOOK_HAS_FN },
1699 { USB_VENDOR_ID_APPLE, 0x0216, HID_QUIRK_POWERBOOK_HAS_FN },
1700 { USB_VENDOR_ID_APPLE, 0x0217, HID_QUIRK_POWERBOOK_HAS_FN },
1701 { USB_VENDOR_ID_APPLE, 0x0218, HID_QUIRK_POWERBOOK_HAS_FN },
1702 { USB_VENDOR_ID_APPLE, 0x0219, HID_QUIRK_POWERBOOK_HAS_FN },
1703 { USB_VENDOR_ID_APPLE, 0x030A, HID_QUIRK_POWERBOOK_HAS_FN },
1704 { USB_VENDOR_ID_APPLE, 0x030B, HID_QUIRK_POWERBOOK_HAS_FN },
1706 { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
1707 { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
1708 { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
1709 { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
1715 * Traverse the supplied list of reports and find the longest
1717 static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
1719 struct hid_report *report;
1722 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
1723 size = ((report->size - 1) >> 3) + 1;
1724 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
1731 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1733 if (!(hid->inbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->inbuf_dma)))
1735 if (!(hid->outbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->outbuf_dma)))
1737 if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
1739 if (!(hid->ctrlbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
1745 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1748 usb_buffer_free(dev, hid->bufsize, hid->inbuf, hid->inbuf_dma);
1750 usb_buffer_free(dev, hid->bufsize, hid->outbuf, hid->outbuf_dma);
1752 usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
1754 usb_buffer_free(dev, hid->bufsize, hid->ctrlbuf, hid->ctrlbuf_dma);
1758 * Cherry Cymotion keyboard have an invalid HID report descriptor,
1759 * that needs fixing before we can parse it.
1762 static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1764 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1765 info("Fixing up Cherry Cymotion report descriptor");
1766 rdesc[11] = rdesc[16] = 0xff;
1767 rdesc[12] = rdesc[17] = 0x03;
1771 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1773 struct usb_host_interface *interface = intf->cur_altsetting;
1774 struct usb_device *dev = interface_to_usbdev (intf);
1775 struct hid_descriptor *hdesc;
1776 struct hid_device *hid;
1777 unsigned quirks = 0, rsize = 0;
1779 int n, len, insize = 0;
1781 for (n = 0; hid_blacklist[n].idVendor; n++)
1782 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1783 (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1784 quirks = hid_blacklist[n].quirks;
1786 /* Many keyboards and mice don't like to be polled for reports,
1787 * so we will always set the HID_QUIRK_NOGET flag for them. */
1788 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1789 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1790 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1791 quirks |= HID_QUIRK_NOGET;
1794 if (quirks & HID_QUIRK_IGNORE)
1797 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1798 (!interface->desc.bNumEndpoints ||
1799 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1800 dbg("class descriptor not present\n");
1804 for (n = 0; n < hdesc->bNumDescriptors; n++)
1805 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1806 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1808 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1809 dbg("weird size of report descriptor (%u)", rsize);
1813 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1814 dbg("couldn't allocate rdesc memory");
1818 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1820 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1821 dbg("reading report descriptor failed");
1826 if ((quirks & HID_QUIRK_CYMOTION))
1827 hid_fixup_cymotion_descriptor(rdesc, rsize);
1830 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1831 for (n = 0; n < rsize; n++)
1832 printk(" %02x", (unsigned char) rdesc[n]);
1836 if (!(hid = hid_parse_report(rdesc, n))) {
1837 dbg("parsing report descriptor failed");
1843 hid->quirks = quirks;
1845 hid->bufsize = HID_MIN_BUFFER_SIZE;
1846 hid_find_max_report(hid, HID_INPUT_REPORT, &hid->bufsize);
1847 hid_find_max_report(hid, HID_OUTPUT_REPORT, &hid->bufsize);
1848 hid_find_max_report(hid, HID_FEATURE_REPORT, &hid->bufsize);
1850 if (hid->bufsize > HID_MAX_BUFFER_SIZE)
1851 hid->bufsize = HID_MAX_BUFFER_SIZE;
1853 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1855 if (insize > HID_MAX_BUFFER_SIZE)
1856 insize = HID_MAX_BUFFER_SIZE;
1858 if (hid_alloc_buffers(dev, hid)) {
1859 hid_free_buffers(dev, hid);
1863 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1865 struct usb_endpoint_descriptor *endpoint;
1869 endpoint = &interface->endpoint[n].desc;
1870 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1873 interval = endpoint->bInterval;
1875 /* Change the polling interval of mice. */
1876 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1877 interval = hid_mousepoll_interval;
1879 if (endpoint->bEndpointAddress & USB_DIR_IN) {
1882 if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1884 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1885 usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, insize,
1886 hid_irq_in, hid, interval);
1887 hid->urbin->transfer_dma = hid->inbuf_dma;
1888 hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1892 if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1894 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1895 usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
1896 hid_irq_out, hid, interval);
1897 hid->urbout->transfer_dma = hid->outbuf_dma;
1898 hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1903 err("couldn't find an input interrupt endpoint");
1907 init_waitqueue_head(&hid->wait);
1909 INIT_WORK(&hid->reset_work, hid_reset, hid);
1910 setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
1912 spin_lock_init(&hid->inlock);
1913 spin_lock_init(&hid->outlock);
1914 spin_lock_init(&hid->ctrllock);
1916 hid->version = le16_to_cpu(hdesc->bcdHID);
1917 hid->country = hdesc->bCountryCode;
1920 hid->ifnum = interface->desc.bInterfaceNumber;
1924 if (dev->manufacturer)
1925 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1928 if (dev->manufacturer)
1929 strlcat(hid->name, " ", sizeof(hid->name));
1930 strlcat(hid->name, dev->product, sizeof(hid->name));
1933 if (!strlen(hid->name))
1934 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1935 le16_to_cpu(dev->descriptor.idVendor),
1936 le16_to_cpu(dev->descriptor.idProduct));
1938 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1939 strlcat(hid->phys, "/input", sizeof(hid->phys));
1940 len = strlen(hid->phys);
1941 if (len < sizeof(hid->phys) - 1)
1942 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1943 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1945 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1948 hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1952 usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
1953 hid->ctrlbuf, 1, hid_ctrl, hid);
1954 hid->urbctrl->setup_dma = hid->cr_dma;
1955 hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
1956 hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
1963 usb_free_urb(hid->urbin);
1965 usb_free_urb(hid->urbout);
1967 usb_free_urb(hid->urbctrl);
1968 hid_free_buffers(dev, hid);
1969 hid_free_device(hid);
1974 static void hid_disconnect(struct usb_interface *intf)
1976 struct hid_device *hid = usb_get_intfdata (intf);
1981 spin_lock_irq(&hid->inlock); /* Sync with error handler */
1982 usb_set_intfdata(intf, NULL);
1983 spin_unlock_irq(&hid->inlock);
1984 usb_kill_urb(hid->urbin);
1985 usb_kill_urb(hid->urbout);
1986 usb_kill_urb(hid->urbctrl);
1988 del_timer_sync(&hid->io_retry);
1989 flush_scheduled_work();
1991 if (hid->claimed & HID_CLAIMED_INPUT)
1992 hidinput_disconnect(hid);
1993 if (hid->claimed & HID_CLAIMED_HIDDEV)
1994 hiddev_disconnect(hid);
1996 usb_free_urb(hid->urbin);
1997 usb_free_urb(hid->urbctrl);
1999 usb_free_urb(hid->urbout);
2001 hid_free_buffers(hid->dev, hid);
2002 hid_free_device(hid);
2005 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
2007 struct hid_device *hid;
2012 dbg("HID probe called for ifnum %d",
2013 intf->altsetting->desc.bInterfaceNumber);
2015 if (!(hid = usb_hid_configure(intf)))
2018 hid_init_reports(hid);
2019 hid_dump_device(hid);
2021 if (!hidinput_connect(hid))
2022 hid->claimed |= HID_CLAIMED_INPUT;
2023 if (!hiddev_connect(hid))
2024 hid->claimed |= HID_CLAIMED_HIDDEV;
2026 usb_set_intfdata(intf, hid);
2028 if (!hid->claimed) {
2029 printk ("HID device not claimed by input or hiddev\n");
2030 hid_disconnect(intf);
2036 if (hid->claimed & HID_CLAIMED_INPUT)
2038 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
2040 if (hid->claimed & HID_CLAIMED_HIDDEV)
2041 printk("hiddev%d", hid->minor);
2044 for (i = 0; i < hid->maxcollection; i++) {
2045 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
2046 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
2047 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
2048 c = hid_types[hid->collection[i].usage & 0xffff];
2053 usb_make_path(interface_to_usbdev(intf), path, 63);
2055 printk(": USB HID v%x.%02x %s [%s] on %s\n",
2056 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
2061 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
2063 struct hid_device *hid = usb_get_intfdata (intf);
2065 spin_lock_irq(&hid->inlock); /* Sync with error handler */
2066 set_bit(HID_SUSPENDED, &hid->iofl);
2067 spin_unlock_irq(&hid->inlock);
2068 del_timer(&hid->io_retry);
2069 usb_kill_urb(hid->urbin);
2070 dev_dbg(&intf->dev, "suspend\n");
2074 static int hid_resume(struct usb_interface *intf)
2076 struct hid_device *hid = usb_get_intfdata (intf);
2079 clear_bit(HID_SUSPENDED, &hid->iofl);
2080 hid->retry_delay = 0;
2081 status = hid_start_in(hid);
2082 dev_dbg(&intf->dev, "resume status %d\n", status);
2086 /* Treat USB reset pretty much the same as suspend/resume */
2087 static void hid_pre_reset(struct usb_interface *intf)
2089 /* FIXME: What if the interface is already suspended? */
2090 hid_suspend(intf, PMSG_ON);
2093 static void hid_post_reset(struct usb_interface *intf)
2095 struct usb_device *dev = interface_to_usbdev (intf);
2097 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
2098 /* FIXME: Any more reinitialization needed? */
2103 static struct usb_device_id hid_usb_ids [] = {
2104 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
2105 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
2106 { } /* Terminating entry */
2109 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
2111 static struct usb_driver hid_driver = {
2114 .disconnect = hid_disconnect,
2115 .suspend = hid_suspend,
2116 .resume = hid_resume,
2117 .pre_reset = hid_pre_reset,
2118 .post_reset = hid_post_reset,
2119 .id_table = hid_usb_ids,
2122 static int __init hid_init(void)
2125 retval = hiddev_init();
2127 goto hiddev_init_fail;
2128 retval = usb_register(&hid_driver);
2130 goto usb_register_fail;
2131 info(DRIVER_VERSION ":" DRIVER_DESC);
2140 static void __exit hid_exit(void)
2142 usb_deregister(&hid_driver);
2146 module_init(hid_init);
2147 module_exit(hid_exit);
2149 MODULE_AUTHOR(DRIVER_AUTHOR);
2150 MODULE_DESCRIPTION(DRIVER_DESC);
2151 MODULE_LICENSE(DRIVER_LICENSE);