Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / input / hid-core.c
blob7662cf4e262152de1958c315959ce8b0555633a0
1 /*
2 * USB HID support for Linux
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
6 */
8 /*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/list.h>
21 #include <linux/mm.h>
22 #include <linux/smp_lock.h>
23 #include <linux/spinlock.h>
24 #include <asm/unaligned.h>
25 #include <asm/byteorder.h>
26 #include <linux/input.h>
27 #include <linux/wait.h>
29 #undef DEBUG
30 #undef DEBUG_DATA
32 #include <linux/usb.h>
34 #include "hid.h"
35 #include <linux/hiddev.h>
38 * Version Information
41 #define DRIVER_VERSION "v2.01"
42 #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
43 #define DRIVER_DESC "USB HID core driver"
44 #define DRIVER_LICENSE "GPL"
46 static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
49 * Module parameters.
52 static unsigned int hid_mousepoll_interval;
53 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
57 * Register a new report for a device.
60 static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
62 struct hid_report_enum *report_enum = device->report_enum + type;
63 struct hid_report *report;
65 if (report_enum->report_id_hash[id])
66 return report_enum->report_id_hash[id];
68 if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
69 return NULL;
70 memset(report, 0, sizeof(struct hid_report));
72 if (id != 0)
73 report_enum->numbered = 1;
75 report->id = id;
76 report->type = type;
77 report->size = 0;
78 report->device = device;
79 report_enum->report_id_hash[id] = report;
81 list_add_tail(&report->list, &report_enum->report_list);
83 return report;
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");
96 return NULL;
99 if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
100 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
102 memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
103 + values * sizeof(unsigned));
105 field->index = report->maxfield++;
106 report->field[field->index] = field;
107 field->usage = (struct hid_usage *)(field + 1);
108 field->value = (unsigned *)(field->usage + usages);
109 field->report = report;
111 return field;
115 * Open a collection. The type/usage is pushed on the stack.
118 static int open_collection(struct hid_parser *parser, unsigned type)
120 struct hid_collection *collection;
121 unsigned usage;
123 usage = parser->local.usage[0];
125 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
126 dbg("collection stack overflow");
127 return -1;
130 if (parser->device->maxcollection == parser->device->collection_size) {
131 collection = kmalloc(sizeof(struct hid_collection) *
132 parser->device->collection_size * 2, GFP_KERNEL);
133 if (collection == NULL) {
134 dbg("failed to reallocate collection array");
135 return -1;
137 memcpy(collection, parser->device->collection,
138 sizeof(struct hid_collection) *
139 parser->device->collection_size);
140 memset(collection + parser->device->collection_size, 0,
141 sizeof(struct hid_collection) *
142 parser->device->collection_size);
143 kfree(parser->device->collection);
144 parser->device->collection = collection;
145 parser->device->collection_size *= 2;
148 parser->collection_stack[parser->collection_stack_ptr++] =
149 parser->device->maxcollection;
151 collection = parser->device->collection +
152 parser->device->maxcollection++;
153 collection->type = type;
154 collection->usage = usage;
155 collection->level = parser->collection_stack_ptr - 1;
157 if (type == HID_COLLECTION_APPLICATION)
158 parser->device->maxapplication++;
160 return 0;
164 * Close a collection.
167 static int close_collection(struct hid_parser *parser)
169 if (!parser->collection_stack_ptr) {
170 dbg("collection stack underflow");
171 return -1;
173 parser->collection_stack_ptr--;
174 return 0;
178 * Climb up the stack, search for the specified collection type
179 * and return the usage.
182 static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
184 int n;
185 for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
186 if (parser->device->collection[parser->collection_stack[n]].type == type)
187 return parser->device->collection[parser->collection_stack[n]].usage;
188 return 0; /* we know nothing about this usage type */
192 * Add a usage to the temporary parser table.
195 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
197 if (parser->local.usage_index >= HID_MAX_USAGES) {
198 dbg("usage index exceeded");
199 return -1;
201 parser->local.usage[parser->local.usage_index] = usage;
202 parser->local.collection_index[parser->local.usage_index] =
203 parser->collection_stack_ptr ?
204 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
205 parser->local.usage_index++;
206 return 0;
210 * Register a new field for this report.
213 static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
215 struct hid_report *report;
216 struct hid_field *field;
217 int usages;
218 unsigned offset;
219 int i;
221 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
222 dbg("hid_register_report failed");
223 return -1;
226 if (parser->global.logical_maximum < parser->global.logical_minimum) {
227 dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
228 return -1;
231 offset = report->size;
232 report->size += parser->global.report_size * parser->global.report_count;
234 if (!parser->local.usage_index) /* Ignore padding fields */
235 return 0;
237 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
239 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
240 return 0;
242 field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
243 field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
244 field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
246 for (i = 0; i < usages; i++) {
247 int j = i;
248 /* Duplicate the last usage we parsed if we have excess values */
249 if (i >= parser->local.usage_index)
250 j = parser->local.usage_index - 1;
251 field->usage[i].hid = parser->local.usage[j];
252 field->usage[i].collection_index =
253 parser->local.collection_index[j];
256 field->maxusage = usages;
257 field->flags = flags;
258 field->report_offset = offset;
259 field->report_type = report_type;
260 field->report_size = parser->global.report_size;
261 field->report_count = parser->global.report_count;
262 field->logical_minimum = parser->global.logical_minimum;
263 field->logical_maximum = parser->global.logical_maximum;
264 field->physical_minimum = parser->global.physical_minimum;
265 field->physical_maximum = parser->global.physical_maximum;
266 field->unit_exponent = parser->global.unit_exponent;
267 field->unit = parser->global.unit;
269 return 0;
273 * Read data value from item.
276 static __inline__ __u32 item_udata(struct hid_item *item)
278 switch (item->size) {
279 case 1: return item->data.u8;
280 case 2: return item->data.u16;
281 case 4: return item->data.u32;
283 return 0;
286 static __inline__ __s32 item_sdata(struct hid_item *item)
288 switch (item->size) {
289 case 1: return item->data.s8;
290 case 2: return item->data.s16;
291 case 4: return item->data.s32;
293 return 0;
297 * Process a global item.
300 static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
302 switch (item->tag) {
304 case HID_GLOBAL_ITEM_TAG_PUSH:
306 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
307 dbg("global enviroment stack overflow");
308 return -1;
311 memcpy(parser->global_stack + parser->global_stack_ptr++,
312 &parser->global, sizeof(struct hid_global));
313 return 0;
315 case HID_GLOBAL_ITEM_TAG_POP:
317 if (!parser->global_stack_ptr) {
318 dbg("global enviroment stack underflow");
319 return -1;
322 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
323 sizeof(struct hid_global));
324 return 0;
326 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
327 parser->global.usage_page = item_udata(item);
328 return 0;
330 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
331 parser->global.logical_minimum = item_sdata(item);
332 return 0;
334 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
335 if (parser->global.logical_minimum < 0)
336 parser->global.logical_maximum = item_sdata(item);
337 else
338 parser->global.logical_maximum = item_udata(item);
339 return 0;
341 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
342 parser->global.physical_minimum = item_sdata(item);
343 return 0;
345 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
346 if (parser->global.physical_minimum < 0)
347 parser->global.physical_maximum = item_sdata(item);
348 else
349 parser->global.physical_maximum = item_udata(item);
350 return 0;
352 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
353 parser->global.unit_exponent = item_sdata(item);
354 return 0;
356 case HID_GLOBAL_ITEM_TAG_UNIT:
357 parser->global.unit = item_udata(item);
358 return 0;
360 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
361 if ((parser->global.report_size = item_udata(item)) > 32) {
362 dbg("invalid report_size %d", parser->global.report_size);
363 return -1;
365 return 0;
367 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
368 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
369 dbg("invalid report_count %d", parser->global.report_count);
370 return -1;
372 return 0;
374 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
375 if ((parser->global.report_id = item_udata(item)) == 0) {
376 dbg("report_id 0 is invalid");
377 return -1;
379 return 0;
381 default:
382 dbg("unknown global tag 0x%x", item->tag);
383 return -1;
388 * Process a local item.
391 static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
393 __u32 data;
394 unsigned n;
396 if (item->size == 0) {
397 dbg("item data expected for local item");
398 return -1;
401 data = item_udata(item);
403 switch (item->tag) {
405 case HID_LOCAL_ITEM_TAG_DELIMITER:
407 if (data) {
409 * We treat items before the first delimiter
410 * as global to all usage sets (branch 0).
411 * In the moment we process only these global
412 * items and the first delimiter set.
414 if (parser->local.delimiter_depth != 0) {
415 dbg("nested delimiters");
416 return -1;
418 parser->local.delimiter_depth++;
419 parser->local.delimiter_branch++;
420 } else {
421 if (parser->local.delimiter_depth < 1) {
422 dbg("bogus close delimiter");
423 return -1;
425 parser->local.delimiter_depth--;
427 return 1;
429 case HID_LOCAL_ITEM_TAG_USAGE:
431 if (parser->local.delimiter_branch > 1) {
432 dbg("alternative usage ignored");
433 return 0;
436 if (item->size <= 2)
437 data = (parser->global.usage_page << 16) + data;
439 return hid_add_usage(parser, data);
441 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
443 if (parser->local.delimiter_branch > 1) {
444 dbg("alternative usage ignored");
445 return 0;
448 if (item->size <= 2)
449 data = (parser->global.usage_page << 16) + data;
451 parser->local.usage_minimum = data;
452 return 0;
454 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
456 if (parser->local.delimiter_branch > 1) {
457 dbg("alternative usage ignored");
458 return 0;
461 if (item->size <= 2)
462 data = (parser->global.usage_page << 16) + data;
464 for (n = parser->local.usage_minimum; n <= data; n++)
465 if (hid_add_usage(parser, n)) {
466 dbg("hid_add_usage failed\n");
467 return -1;
469 return 0;
471 default:
473 dbg("unknown local item tag 0x%x", item->tag);
474 return 0;
476 return 0;
480 * Process a main item.
483 static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
485 __u32 data;
486 int ret;
488 data = item_udata(item);
490 switch (item->tag) {
491 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
492 ret = open_collection(parser, data & 0xff);
493 break;
494 case HID_MAIN_ITEM_TAG_END_COLLECTION:
495 ret = close_collection(parser);
496 break;
497 case HID_MAIN_ITEM_TAG_INPUT:
498 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
499 break;
500 case HID_MAIN_ITEM_TAG_OUTPUT:
501 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
502 break;
503 case HID_MAIN_ITEM_TAG_FEATURE:
504 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
505 break;
506 default:
507 dbg("unknown main item tag 0x%x", item->tag);
508 ret = 0;
511 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
513 return ret;
517 * Process a reserved item.
520 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
522 dbg("reserved item type, tag 0x%x", item->tag);
523 return 0;
527 * Free a report and all registered fields. The field->usage and
528 * field->value table's are allocated behind the field, so we need
529 * only to free(field) itself.
532 static void hid_free_report(struct hid_report *report)
534 unsigned n;
536 for (n = 0; n < report->maxfield; n++)
537 kfree(report->field[n]);
538 kfree(report);
542 * Free a device structure, all reports, and all fields.
545 static void hid_free_device(struct hid_device *device)
547 unsigned i,j;
549 hid_ff_exit(device);
551 for (i = 0; i < HID_REPORT_TYPES; i++) {
552 struct hid_report_enum *report_enum = device->report_enum + i;
554 for (j = 0; j < 256; j++) {
555 struct hid_report *report = report_enum->report_id_hash[j];
556 if (report)
557 hid_free_report(report);
561 if (device->rdesc)
562 kfree(device->rdesc);
563 kfree(device);
567 * Fetch a report description item from the data stream. We support long
568 * items, though they are not used yet.
571 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
573 u8 b;
575 if ((end - start) <= 0)
576 return NULL;
578 b = *start++;
580 item->type = (b >> 2) & 3;
581 item->tag = (b >> 4) & 15;
583 if (item->tag == HID_ITEM_TAG_LONG) {
585 item->format = HID_ITEM_FORMAT_LONG;
587 if ((end - start) < 2)
588 return NULL;
590 item->size = *start++;
591 item->tag = *start++;
593 if ((end - start) < item->size)
594 return NULL;
596 item->data.longdata = start;
597 start += item->size;
598 return start;
601 item->format = HID_ITEM_FORMAT_SHORT;
602 item->size = b & 3;
604 switch (item->size) {
606 case 0:
607 return start;
609 case 1:
610 if ((end - start) < 1)
611 return NULL;
612 item->data.u8 = *start++;
613 return start;
615 case 2:
616 if ((end - start) < 2)
617 return NULL;
618 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
619 start = (__u8 *)((__le16 *)start + 1);
620 return start;
622 case 3:
623 item->size++;
624 if ((end - start) < 4)
625 return NULL;
626 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
627 start = (__u8 *)((__le32 *)start + 1);
628 return start;
631 return NULL;
635 * Parse a report description into a hid_device structure. Reports are
636 * enumerated, fields are attached to these reports.
639 static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
641 struct hid_device *device;
642 struct hid_parser *parser;
643 struct hid_item item;
644 __u8 *end;
645 unsigned i;
646 static int (*dispatch_type[])(struct hid_parser *parser,
647 struct hid_item *item) = {
648 hid_parser_main,
649 hid_parser_global,
650 hid_parser_local,
651 hid_parser_reserved
654 if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
655 return NULL;
656 memset(device, 0, sizeof(struct hid_device));
658 if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
659 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
660 kfree(device);
661 return NULL;
663 memset(device->collection, 0, sizeof(struct hid_collection) *
664 HID_DEFAULT_NUM_COLLECTIONS);
665 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
667 for (i = 0; i < HID_REPORT_TYPES; i++)
668 INIT_LIST_HEAD(&device->report_enum[i].report_list);
670 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
671 kfree(device->collection);
672 kfree(device);
673 return NULL;
675 memcpy(device->rdesc, start, size);
676 device->rsize = size;
678 if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
679 kfree(device->rdesc);
680 kfree(device->collection);
681 kfree(device);
682 return NULL;
684 memset(parser, 0, sizeof(struct hid_parser));
685 parser->device = device;
687 end = start + size;
688 while ((start = fetch_item(start, end, &item)) != NULL) {
690 if (item.format != HID_ITEM_FORMAT_SHORT) {
691 dbg("unexpected long global item");
692 kfree(device->collection);
693 hid_free_device(device);
694 kfree(parser);
695 return NULL;
698 if (dispatch_type[item.type](parser, &item)) {
699 dbg("item %u %u %u %u parsing failed\n",
700 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
701 kfree(device->collection);
702 hid_free_device(device);
703 kfree(parser);
704 return NULL;
707 if (start == end) {
708 if (parser->collection_stack_ptr) {
709 dbg("unbalanced collection at end of report description");
710 kfree(device->collection);
711 hid_free_device(device);
712 kfree(parser);
713 return NULL;
715 if (parser->local.delimiter_depth) {
716 dbg("unbalanced delimiter at end of report description");
717 kfree(device->collection);
718 hid_free_device(device);
719 kfree(parser);
720 return NULL;
722 kfree(parser);
723 return device;
727 dbg("item fetching failed at offset %d\n", (int)(end - start));
728 kfree(device->collection);
729 hid_free_device(device);
730 kfree(parser);
731 return NULL;
735 * Convert a signed n-bit integer to signed 32-bit integer. Common
736 * cases are done through the compiler, the screwed things has to be
737 * done by hand.
740 static __inline__ __s32 snto32(__u32 value, unsigned n)
742 switch (n) {
743 case 8: return ((__s8)value);
744 case 16: return ((__s16)value);
745 case 32: return ((__s32)value);
747 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
751 * Convert a signed 32-bit integer to a signed n-bit integer.
754 static __inline__ __u32 s32ton(__s32 value, unsigned n)
756 __s32 a = value >> (n - 1);
757 if (a && a != -1)
758 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((__le64*)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((__le64*)report)
776 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
777 | cpu_to_le64((__u64)value << offset), (__le64*)report);
781 * Search an array for a value.
784 static __inline__ int search(__s32 *array, __s32 value, unsigned n)
786 while (n--) {
787 if (*array++ == value)
788 return 0;
790 return -1;
793 static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs)
795 hid_dump_input(usage, value);
796 if (hid->claimed & HID_CLAIMED_INPUT)
797 hidinput_hid_event(hid, field, usage, value, regs);
798 if (hid->claimed & HID_CLAIMED_HIDDEV)
799 hiddev_hid_event(hid, field, usage, value, regs);
803 * Analyse a received field, and fetch the data from it. The field
804 * content is stored for next report processing (we do differential
805 * reporting to the layer).
808 static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, struct pt_regs *regs)
810 unsigned n;
811 unsigned count = field->report_count;
812 unsigned offset = field->report_offset;
813 unsigned size = field->report_size;
814 __s32 min = field->logical_minimum;
815 __s32 max = field->logical_maximum;
816 __s32 *value;
818 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
819 return;
821 for (n = 0; n < count; n++) {
823 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
824 extract(data, offset + n * size, size);
826 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
827 && value[n] >= min && value[n] <= max
828 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
829 goto exit;
832 for (n = 0; n < count; n++) {
834 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
835 hid_process_event(hid, field, &field->usage[n], value[n], regs);
836 continue;
839 if (field->value[n] >= min && field->value[n] <= max
840 && field->usage[field->value[n] - min].hid
841 && search(value, field->value[n], count))
842 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, regs);
844 if (value[n] >= min && value[n] <= max
845 && field->usage[value[n] - min].hid
846 && search(field->value, value[n], count))
847 hid_process_event(hid, field, &field->usage[value[n] - min], 1, regs);
850 memcpy(field->value, value, count * sizeof(__s32));
851 exit:
852 kfree(value);
855 static int hid_input_report(int type, struct urb *urb, struct pt_regs *regs)
857 struct hid_device *hid = urb->context;
858 struct hid_report_enum *report_enum = hid->report_enum + type;
859 u8 *data = urb->transfer_buffer;
860 int len = urb->actual_length;
861 struct hid_report *report;
862 int n, size;
864 if (!len) {
865 dbg("empty report");
866 return -1;
869 #ifdef DEBUG_DATA
870 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
871 #endif
873 n = 0; /* Normally report number is 0 */
874 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
875 n = *data++;
876 len--;
879 #ifdef DEBUG_DATA
881 int i;
882 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
883 for (i = 0; i < len; i++)
884 printk(" %02x", data[i]);
885 printk("\n");
887 #endif
889 if (!(report = report_enum->report_id_hash[n])) {
890 dbg("undefined report_id %d received", n);
891 return -1;
894 size = ((report->size - 1) >> 3) + 1;
896 if (len < size)
897 dbg("report %d is too short, (%d < %d)", report->id, len, size);
899 if (hid->claimed & HID_CLAIMED_HIDDEV)
900 hiddev_report_event(hid, report);
902 for (n = 0; n < report->maxfield; n++)
903 hid_input_field(hid, report->field[n], data, regs);
905 if (hid->claimed & HID_CLAIMED_INPUT)
906 hidinput_report_event(hid, report);
908 return 0;
912 * Input interrupt completion handler.
915 static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
917 struct hid_device *hid = urb->context;
918 int status;
920 switch (urb->status) {
921 case 0: /* success */
922 hid_input_report(HID_INPUT_REPORT, urb, regs);
923 break;
924 case -ECONNRESET: /* unlink */
925 case -ENOENT:
926 case -EPERM:
927 case -ESHUTDOWN: /* unplug */
928 case -EILSEQ: /* unplug timeout on uhci */
929 return;
930 case -ETIMEDOUT: /* NAK */
931 break;
932 default: /* error */
933 warn("input irq status %d received", urb->status);
936 status = usb_submit_urb(urb, SLAB_ATOMIC);
937 if (status)
938 err("can't resubmit intr, %s-%s/input%d, status %d",
939 hid->dev->bus->bus_name, hid->dev->devpath,
940 hid->ifnum, status);
944 * Output the field into the report.
947 static void hid_output_field(struct hid_field *field, __u8 *data)
949 unsigned count = field->report_count;
950 unsigned offset = field->report_offset;
951 unsigned size = field->report_size;
952 unsigned n;
954 for (n = 0; n < count; n++) {
955 if (field->logical_minimum < 0) /* signed values */
956 implement(data, offset + n * size, size, s32ton(field->value[n], size));
957 else /* unsigned values */
958 implement(data, offset + n * size, size, field->value[n]);
963 * Create a report.
966 static void hid_output_report(struct hid_report *report, __u8 *data)
968 unsigned n;
970 if (report->id > 0)
971 *data++ = report->id;
973 for (n = 0; n < report->maxfield; n++)
974 hid_output_field(report->field[n], data);
978 * Set a field value. The report this field belongs to has to be
979 * created and transferred to the device, to set this value in the
980 * device.
983 int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
985 unsigned size = field->report_size;
987 hid_dump_input(field->usage + offset, value);
989 if (offset >= field->report_count) {
990 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
991 hid_dump_field(field, 8);
992 return -1;
994 if (field->logical_minimum < 0) {
995 if (value != snto32(s32ton(value, size), size)) {
996 dbg("value %d is out of range", value);
997 return -1;
1000 field->value[offset] = value;
1001 return 0;
1005 * Find a report field with a specified HID usage.
1008 struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
1010 struct hid_report *report;
1011 int i;
1013 list_for_each_entry(report, &hid->report_enum[type].report_list, list)
1014 for (i = 0; i < report->maxfield; i++)
1015 if (report->field[i]->logical == wanted_usage)
1016 return report->field[i];
1017 return NULL;
1020 static int hid_submit_out(struct hid_device *hid)
1022 struct hid_report *report;
1024 report = hid->out[hid->outtail];
1026 hid_output_report(report, hid->outbuf);
1027 hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1028 hid->urbout->dev = hid->dev;
1030 dbg("submitting out urb");
1032 if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
1033 err("usb_submit_urb(out) failed");
1034 return -1;
1037 return 0;
1040 static int hid_submit_ctrl(struct hid_device *hid)
1042 struct hid_report *report;
1043 unsigned char dir;
1044 int len;
1046 report = hid->ctrl[hid->ctrltail].report;
1047 dir = hid->ctrl[hid->ctrltail].dir;
1049 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1050 if (dir == USB_DIR_OUT) {
1051 hid_output_report(report, hid->ctrlbuf);
1052 hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
1053 hid->urbctrl->transfer_buffer_length = len;
1054 } else {
1055 int maxpacket, padlen;
1057 hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
1058 maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
1059 if (maxpacket > 0) {
1060 padlen = (len + maxpacket - 1) / maxpacket;
1061 padlen *= maxpacket;
1062 if (padlen > HID_BUFFER_SIZE)
1063 padlen = HID_BUFFER_SIZE;
1064 } else
1065 padlen = 0;
1066 hid->urbctrl->transfer_buffer_length = padlen;
1068 hid->urbctrl->dev = hid->dev;
1070 hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
1071 hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
1072 hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1073 hid->cr->wIndex = cpu_to_le16(hid->ifnum);
1074 hid->cr->wLength = cpu_to_le16(len);
1076 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
1077 hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
1078 hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
1080 if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
1081 err("usb_submit_urb(ctrl) failed");
1082 return -1;
1085 return 0;
1089 * Output interrupt completion handler.
1092 static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1094 struct hid_device *hid = urb->context;
1095 unsigned long flags;
1096 int unplug = 0;
1098 switch (urb->status) {
1099 case 0: /* success */
1100 case -ESHUTDOWN: /* unplug */
1101 case -EILSEQ: /* unplug timeout on uhci */
1102 unplug = 1;
1103 case -ECONNRESET: /* unlink */
1104 case -ENOENT:
1105 break;
1106 default: /* error */
1107 warn("output irq status %d received", urb->status);
1110 spin_lock_irqsave(&hid->outlock, flags);
1112 if (unplug)
1113 hid->outtail = hid->outhead;
1114 else
1115 hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1117 if (hid->outhead != hid->outtail) {
1118 if (hid_submit_out(hid)) {
1119 clear_bit(HID_OUT_RUNNING, &hid->iofl);;
1120 wake_up(&hid->wait);
1122 spin_unlock_irqrestore(&hid->outlock, flags);
1123 return;
1126 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1127 spin_unlock_irqrestore(&hid->outlock, flags);
1128 wake_up(&hid->wait);
1132 * Control pipe completion handler.
1135 static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1137 struct hid_device *hid = urb->context;
1138 unsigned long flags;
1139 int unplug = 0;
1141 spin_lock_irqsave(&hid->ctrllock, flags);
1143 switch (urb->status) {
1144 case 0: /* success */
1145 if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
1146 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, regs);
1147 case -ESHUTDOWN: /* unplug */
1148 case -EILSEQ: /* unplug timectrl on uhci */
1149 unplug = 1;
1150 case -ECONNRESET: /* unlink */
1151 case -ENOENT:
1152 case -EPIPE: /* report not available */
1153 break;
1154 default: /* error */
1155 warn("ctrl urb status %d received", urb->status);
1158 if (unplug)
1159 hid->ctrltail = hid->ctrlhead;
1160 else
1161 hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1163 if (hid->ctrlhead != hid->ctrltail) {
1164 if (hid_submit_ctrl(hid)) {
1165 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1166 wake_up(&hid->wait);
1168 spin_unlock_irqrestore(&hid->ctrllock, flags);
1169 return;
1172 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1173 spin_unlock_irqrestore(&hid->ctrllock, flags);
1174 wake_up(&hid->wait);
1177 void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
1179 int head;
1180 unsigned long flags;
1182 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
1183 return;
1185 if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1187 spin_lock_irqsave(&hid->outlock, flags);
1189 if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
1190 spin_unlock_irqrestore(&hid->outlock, flags);
1191 warn("output queue full");
1192 return;
1195 hid->out[hid->outhead] = report;
1196 hid->outhead = head;
1198 if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1199 if (hid_submit_out(hid))
1200 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1202 spin_unlock_irqrestore(&hid->outlock, flags);
1203 return;
1206 spin_lock_irqsave(&hid->ctrllock, flags);
1208 if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
1209 spin_unlock_irqrestore(&hid->ctrllock, flags);
1210 warn("control queue full");
1211 return;
1214 hid->ctrl[hid->ctrlhead].report = report;
1215 hid->ctrl[hid->ctrlhead].dir = dir;
1216 hid->ctrlhead = head;
1218 if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
1219 if (hid_submit_ctrl(hid))
1220 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1222 spin_unlock_irqrestore(&hid->ctrllock, flags);
1225 int hid_wait_io(struct hid_device *hid)
1227 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
1228 !test_bit(HID_OUT_RUNNING, &hid->iofl)),
1229 10*HZ)) {
1230 dbg("timeout waiting for ctrl or out queue to clear");
1231 return -1;
1234 return 0;
1237 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1238 unsigned char type, void *buf, int size)
1240 int result, retries = 4;
1242 memset(buf,0,size); // Make sure we parse really received data
1244 do {
1245 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1246 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
1247 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
1248 retries--;
1249 } while (result < size && retries);
1250 return result;
1253 int hid_open(struct hid_device *hid)
1255 if (hid->open++)
1256 return 0;
1258 hid->urbin->dev = hid->dev;
1260 if (usb_submit_urb(hid->urbin, GFP_KERNEL))
1261 return -EIO;
1263 return 0;
1266 void hid_close(struct hid_device *hid)
1268 if (!--hid->open)
1269 usb_kill_urb(hid->urbin);
1273 * Initialize all reports
1276 void hid_init_reports(struct hid_device *hid)
1278 struct hid_report *report;
1279 int err, ret;
1281 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) {
1282 int size = ((report->size - 1) >> 3) + 1 + hid->report_enum[HID_INPUT_REPORT].numbered;
1283 if (size > HID_BUFFER_SIZE) size = HID_BUFFER_SIZE;
1284 if (size > hid->urbin->transfer_buffer_length)
1285 hid->urbin->transfer_buffer_length = size;
1286 hid_submit_report(hid, report, USB_DIR_IN);
1289 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
1290 hid_submit_report(hid, report, USB_DIR_IN);
1292 err = 0;
1293 ret = hid_wait_io(hid);
1294 while (ret) {
1295 err |= ret;
1296 if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
1297 usb_kill_urb(hid->urbctrl);
1298 if (test_bit(HID_OUT_RUNNING, &hid->iofl))
1299 usb_kill_urb(hid->urbout);
1300 ret = hid_wait_io(hid);
1303 if (err)
1304 warn("timeout initializing reports\n");
1306 usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0),
1307 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
1308 hid->ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
1311 #define USB_VENDOR_ID_WACOM 0x056a
1312 #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
1313 #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1314 #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
1315 #define USB_DEVICE_ID_WACOM_PL 0x0030
1316 #define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
1317 #define USB_DEVICE_ID_WACOM_VOLITO 0x0060
1318 #define USB_DEVICE_ID_WACOM_PTU 0x0003
1320 #define USB_VENDOR_ID_KBGEAR 0x084e
1321 #define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
1323 #define USB_VENDOR_ID_AIPTEK 0x08ca
1324 #define USB_DEVICE_ID_AIPTEK_01 0x0001
1325 #define USB_DEVICE_ID_AIPTEK_10 0x0010
1326 #define USB_DEVICE_ID_AIPTEK_20 0x0020
1327 #define USB_DEVICE_ID_AIPTEK_21 0x0021
1328 #define USB_DEVICE_ID_AIPTEK_22 0x0022
1329 #define USB_DEVICE_ID_AIPTEK_23 0x0023
1330 #define USB_DEVICE_ID_AIPTEK_24 0x0024
1332 #define USB_VENDOR_ID_GRIFFIN 0x077d
1333 #define USB_DEVICE_ID_POWERMATE 0x0410
1334 #define USB_DEVICE_ID_SOUNDKNOB 0x04AA
1336 #define USB_VENDOR_ID_ATEN 0x0557
1337 #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
1338 #define USB_DEVICE_ID_ATEN_CS124U 0x2202
1339 #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
1340 #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
1341 #define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
1343 #define USB_VENDOR_ID_TOPMAX 0x0663
1344 #define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
1346 #define USB_VENDOR_ID_HAPP 0x078b
1347 #define USB_DEVICE_ID_UGCI_DRIVING 0x0010
1348 #define USB_DEVICE_ID_UGCI_FLYING 0x0020
1349 #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
1351 #define USB_VENDOR_ID_MGE 0x0463
1352 #define USB_DEVICE_ID_MGE_UPS 0xffff
1353 #define USB_DEVICE_ID_MGE_UPS1 0x0001
1355 #define USB_VENDOR_ID_ONTRAK 0x0a07
1356 #define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
1358 #define USB_VENDOR_ID_TANGTOP 0x0d3d
1359 #define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
1361 #define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1362 #define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1364 #define USB_VENDOR_ID_A4TECH 0x09da
1365 #define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
1367 #define USB_VENDOR_ID_CYPRESS 0x04b4
1368 #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
1369 #define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
1371 #define USB_VENDOR_ID_BERKSHIRE 0x0c98
1372 #define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
1374 #define USB_VENDOR_ID_ALPS 0x0433
1375 #define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
1377 #define USB_VENDOR_ID_SAITEK 0x06a3
1378 #define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
1380 #define USB_VENDOR_ID_NEC 0x073e
1381 #define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
1383 #define USB_VENDOR_ID_CHIC 0x05fe
1384 #define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
1386 #define USB_VENDOR_ID_GLAB 0x06c2
1387 #define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
1388 #define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
1389 #define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
1390 #define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
1391 #define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
1393 #define USB_VENDOR_ID_WISEGROUP 0x0925
1394 #define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
1395 #define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
1397 #define USB_VENDOR_ID_CODEMERCS 0x07c0
1398 #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
1399 #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
1400 #define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
1401 #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
1403 #define USB_VENDOR_ID_DELORME 0x1163
1404 #define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
1406 #define USB_VENDOR_ID_MCC 0x09db
1407 #define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
1408 #define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
1410 #define USB_VENDOR_ID_CHICONY 0x04f2
1411 #define USB_DEVICE_ID_CHICONY_USBHUB_KB 0x0100
1413 #define USB_VENDOR_ID_BTC 0x046e
1414 #define USB_DEVICE_ID_BTC_KEYBOARD 0x5303
1418 * Alphabetically sorted blacklist by quirk type.
1421 static struct hid_blacklist {
1422 __u16 idVendor;
1423 __u16 idProduct;
1424 unsigned quirks;
1425 } hid_blacklist[] = {
1427 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1428 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1429 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1430 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1431 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1432 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1433 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1434 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
1435 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
1436 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
1437 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
1438 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
1439 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
1440 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
1441 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1442 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1443 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1444 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
1445 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
1446 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
1447 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1448 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1449 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1450 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
1451 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
1452 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1453 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1454 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1455 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1456 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1457 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1458 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1459 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1460 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1461 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1462 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1463 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1464 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1465 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1466 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1467 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1468 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1469 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1470 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1471 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1472 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1473 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1474 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1475 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1476 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1477 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1478 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1479 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1480 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1481 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1482 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1483 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
1484 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
1485 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1486 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1488 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1489 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1490 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1491 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1492 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1493 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
1494 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
1495 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1497 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
1498 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
1500 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
1501 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
1502 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1503 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1504 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1505 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1506 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
1507 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1509 { 0, 0 }
1512 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1514 if (!(hid->inbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->inbuf_dma)))
1515 return -1;
1516 if (!(hid->outbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->outbuf_dma)))
1517 return -1;
1518 if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
1519 return -1;
1520 if (!(hid->ctrlbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
1521 return -1;
1523 return 0;
1526 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1528 if (hid->inbuf)
1529 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->inbuf, hid->inbuf_dma);
1530 if (hid->outbuf)
1531 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->outbuf, hid->outbuf_dma);
1532 if (hid->cr)
1533 usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
1534 if (hid->ctrlbuf)
1535 usb_buffer_free(dev, HID_BUFFER_SIZE, hid->ctrlbuf, hid->ctrlbuf_dma);
1538 static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1540 struct usb_host_interface *interface = intf->cur_altsetting;
1541 struct usb_device *dev = interface_to_usbdev (intf);
1542 struct hid_descriptor *hdesc;
1543 struct hid_device *hid;
1544 unsigned quirks = 0, rsize = 0;
1545 char *buf, *rdesc;
1546 int n;
1548 for (n = 0; hid_blacklist[n].idVendor; n++)
1549 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1550 (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1551 quirks = hid_blacklist[n].quirks;
1553 if (quirks & HID_QUIRK_IGNORE)
1554 return NULL;
1556 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
1557 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1558 dbg("class descriptor not present\n");
1559 return NULL;
1562 for (n = 0; n < hdesc->bNumDescriptors; n++)
1563 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1564 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1566 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1567 dbg("weird size of report descriptor (%u)", rsize);
1568 return NULL;
1571 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1572 dbg("couldn't allocate rdesc memory");
1573 return NULL;
1576 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1577 dbg("reading report descriptor failed");
1578 kfree(rdesc);
1579 return NULL;
1582 #ifdef DEBUG_DATA
1583 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1584 for (n = 0; n < rsize; n++)
1585 printk(" %02x", (unsigned char) rdesc[n]);
1586 printk("\n");
1587 #endif
1589 if (!(hid = hid_parse_report(rdesc, n))) {
1590 dbg("parsing report descriptor failed");
1591 kfree(rdesc);
1592 return NULL;
1595 kfree(rdesc);
1596 hid->quirks = quirks;
1598 if (hid_alloc_buffers(dev, hid)) {
1599 hid_free_buffers(dev, hid);
1600 goto fail;
1603 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1605 struct usb_endpoint_descriptor *endpoint;
1606 int pipe;
1607 int interval;
1609 endpoint = &interface->endpoint[n].desc;
1610 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1611 continue;
1613 /* handle potential highspeed HID correctly */
1614 interval = endpoint->bInterval;
1615 if (dev->speed == USB_SPEED_HIGH)
1616 interval = 1 << (interval - 1);
1618 /* Change the polling interval of mice. */
1619 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1620 interval = hid_mousepoll_interval;
1622 if (endpoint->bEndpointAddress & USB_DIR_IN) {
1623 if (hid->urbin)
1624 continue;
1625 if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1626 goto fail;
1627 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1628 usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, 0,
1629 hid_irq_in, hid, interval);
1630 hid->urbin->transfer_dma = hid->inbuf_dma;
1631 hid->urbin->transfer_flags |=(URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
1632 } else {
1633 if (hid->urbout)
1634 continue;
1635 if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1636 goto fail;
1637 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1638 usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
1639 hid_irq_out, hid, interval);
1640 hid->urbout->transfer_dma = hid->outbuf_dma;
1641 hid->urbout->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
1645 if (!hid->urbin) {
1646 err("couldn't find an input interrupt endpoint");
1647 goto fail;
1650 init_waitqueue_head(&hid->wait);
1652 spin_lock_init(&hid->outlock);
1653 spin_lock_init(&hid->ctrllock);
1655 hid->version = le16_to_cpu(hdesc->bcdHID);
1656 hid->country = hdesc->bCountryCode;
1657 hid->dev = dev;
1658 hid->intf = intf;
1659 hid->ifnum = interface->desc.bInterfaceNumber;
1661 hid->name[0] = 0;
1663 if (!(buf = kmalloc(64, GFP_KERNEL)))
1664 goto fail;
1666 if (dev->manufacturer) {
1667 strcat(hid->name, dev->manufacturer);
1668 if (dev->product)
1669 snprintf(hid->name, 64, "%s %s", hid->name, dev->product);
1670 } else if (dev->product) {
1671 snprintf(hid->name, 128, "%s", dev->product);
1672 } else
1673 snprintf(hid->name, 128, "%04x:%04x",
1674 le16_to_cpu(dev->descriptor.idVendor),
1675 le16_to_cpu(dev->descriptor.idProduct));
1677 usb_make_path(dev, buf, 64);
1678 snprintf(hid->phys, 64, "%s/input%d", buf,
1679 intf->altsetting[0].desc.bInterfaceNumber);
1681 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1682 hid->uniq[0] = 0;
1684 kfree(buf);
1686 hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1687 if (!hid->urbctrl)
1688 goto fail;
1689 usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
1690 hid->ctrlbuf, 1, hid_ctrl, hid);
1691 hid->urbctrl->setup_dma = hid->cr_dma;
1692 hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
1693 hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP | URB_ASYNC_UNLINK);
1695 return hid;
1697 fail:
1699 if (hid->urbin)
1700 usb_free_urb(hid->urbin);
1701 if (hid->urbout)
1702 usb_free_urb(hid->urbout);
1703 if (hid->urbctrl)
1704 usb_free_urb(hid->urbctrl);
1705 hid_free_buffers(dev, hid);
1706 hid_free_device(hid);
1708 return NULL;
1711 static void hid_disconnect(struct usb_interface *intf)
1713 struct hid_device *hid = usb_get_intfdata (intf);
1715 if (!hid)
1716 return;
1718 usb_set_intfdata(intf, NULL);
1719 usb_kill_urb(hid->urbin);
1720 usb_kill_urb(hid->urbout);
1721 usb_kill_urb(hid->urbctrl);
1723 if (hid->claimed & HID_CLAIMED_INPUT)
1724 hidinput_disconnect(hid);
1725 if (hid->claimed & HID_CLAIMED_HIDDEV)
1726 hiddev_disconnect(hid);
1728 usb_free_urb(hid->urbin);
1729 usb_free_urb(hid->urbctrl);
1730 if (hid->urbout)
1731 usb_free_urb(hid->urbout);
1733 hid_free_buffers(hid->dev, hid);
1734 hid_free_device(hid);
1737 static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1739 struct hid_device *hid;
1740 char path[64];
1741 int i;
1742 char *c;
1744 dbg("HID probe called for ifnum %d",
1745 intf->altsetting->desc.bInterfaceNumber);
1747 if (!(hid = usb_hid_configure(intf)))
1748 return -EIO;
1750 hid_init_reports(hid);
1751 hid_dump_device(hid);
1753 if (!hidinput_connect(hid))
1754 hid->claimed |= HID_CLAIMED_INPUT;
1755 if (!hiddev_connect(hid))
1756 hid->claimed |= HID_CLAIMED_HIDDEV;
1758 usb_set_intfdata(intf, hid);
1760 if (!hid->claimed) {
1761 printk ("HID device not claimed by input or hiddev\n");
1762 hid_disconnect(intf);
1763 return -EIO;
1766 printk(KERN_INFO);
1768 if (hid->claimed & HID_CLAIMED_INPUT)
1769 printk("input");
1770 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1771 printk(",");
1772 if (hid->claimed & HID_CLAIMED_HIDDEV)
1773 printk("hiddev%d", hid->minor);
1775 c = "Device";
1776 for (i = 0; i < hid->maxcollection; i++) {
1777 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1778 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1779 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1780 c = hid_types[hid->collection[i].usage & 0xffff];
1781 break;
1785 usb_make_path(interface_to_usbdev(intf), path, 63);
1787 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1788 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1790 return 0;
1793 static int hid_suspend(struct usb_interface *intf, u32 state)
1795 struct hid_device *hid = usb_get_intfdata (intf);
1797 usb_kill_urb(hid->urbin);
1798 intf->dev.power.power_state = state;
1799 dev_dbg(&intf->dev, "suspend\n");
1800 return 0;
1803 static int hid_resume(struct usb_interface *intf)
1805 struct hid_device *hid = usb_get_intfdata (intf);
1806 int status;
1808 intf->dev.power.power_state = PM_SUSPEND_ON;
1809 if (hid->open)
1810 status = usb_submit_urb(hid->urbin, GFP_NOIO);
1811 else
1812 status = 0;
1813 dev_dbg(&intf->dev, "resume status %d\n", status);
1814 return status;
1817 static struct usb_device_id hid_usb_ids [] = {
1818 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1819 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1820 { } /* Terminating entry */
1823 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1825 static struct usb_driver hid_driver = {
1826 .owner = THIS_MODULE,
1827 .name = "usbhid",
1828 .probe = hid_probe,
1829 .disconnect = hid_disconnect,
1830 .suspend = hid_suspend,
1831 .resume = hid_resume,
1832 .id_table = hid_usb_ids,
1835 static int __init hid_init(void)
1837 int retval;
1838 retval = hiddev_init();
1839 if (retval)
1840 goto hiddev_init_fail;
1841 retval = usb_register(&hid_driver);
1842 if (retval)
1843 goto usb_register_fail;
1844 info(DRIVER_VERSION ":" DRIVER_DESC);
1846 return 0;
1847 usb_register_fail:
1848 hiddev_exit();
1849 hiddev_init_fail:
1850 return retval;
1853 static void __exit hid_exit(void)
1855 usb_deregister(&hid_driver);
1856 hiddev_exit();
1859 module_init(hid_init);
1860 module_exit(hid_exit);
1862 MODULE_AUTHOR(DRIVER_AUTHOR);
1863 MODULE_DESCRIPTION(DRIVER_DESC);
1864 MODULE_LICENSE(DRIVER_LICENSE);