blob: e3cd0321c59c7185f5109386747b67215a71ac0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
Michael Haboustakbf0964d2005-09-05 00:12:01 -05005 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
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)
13 * any later version.
14 */
15
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>
22#include <linux/mm.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>
29
30#undef DEBUG
31#undef DEBUG_DATA
32
33#include <linux/usb.h>
34
35#include "hid.h"
36#include <linux/hiddev.h>
37
38/*
39 * Version Information
40 */
41
Michael Haboustakbf0964d2005-09-05 00:12:01 -050042#define DRIVER_VERSION "v2.6"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
44#define DRIVER_DESC "USB HID core driver"
45#define DRIVER_LICENSE "GPL"
46
47static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
49/*
50 * Module parameters.
51 */
52
53static unsigned int hid_mousepoll_interval;
54module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
55MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
56
57/*
58 * Register a new report for a device.
59 */
60
61static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
62{
63 struct hid_report_enum *report_enum = device->report_enum + type;
64 struct hid_report *report;
65
66 if (report_enum->report_id_hash[id])
67 return report_enum->report_id_hash[id];
68
Oliver Neukumbbdb7da2006-01-06 20:54:29 +010069 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (id != 0)
73 report_enum->numbered = 1;
74
75 report->id = id;
76 report->type = type;
77 report->size = 0;
78 report->device = device;
79 report_enum->report_id_hash[id] = report;
80
81 list_add_tail(&report->list, &report_enum->report_list);
82
83 return report;
84}
85
86/*
87 * Register a new field for this report.
88 */
89
90static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
91{
92 struct hid_field *field;
93
94 if (report->maxfield == HID_MAX_FIELDS) {
95 dbg("too many fields in report");
96 return NULL;
97 }
98
Oliver Neukumbbdb7da2006-01-06 20:54:29 +010099 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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;
107
108 return field;
109}
110
111/*
112 * Open a collection. The type/usage is pushed on the stack.
113 */
114
115static int open_collection(struct hid_parser *parser, unsigned type)
116{
117 struct hid_collection *collection;
118 unsigned usage;
119
120 usage = parser->local.usage[0];
121
122 if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
123 dbg("collection stack overflow");
124 return -1;
125 }
126
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");
132 return -1;
133 }
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;
143 }
144
145 parser->collection_stack[parser->collection_stack_ptr++] =
146 parser->device->maxcollection;
147
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;
153
154 if (type == HID_COLLECTION_APPLICATION)
155 parser->device->maxapplication++;
156
157 return 0;
158}
159
160/*
161 * Close a collection.
162 */
163
164static int close_collection(struct hid_parser *parser)
165{
166 if (!parser->collection_stack_ptr) {
167 dbg("collection stack underflow");
168 return -1;
169 }
170 parser->collection_stack_ptr--;
171 return 0;
172}
173
174/*
175 * Climb up the stack, search for the specified collection type
176 * and return the usage.
177 */
178
179static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
180{
181 int n;
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 */
186}
187
188/*
189 * Add a usage to the temporary parser table.
190 */
191
192static int hid_add_usage(struct hid_parser *parser, unsigned usage)
193{
194 if (parser->local.usage_index >= HID_MAX_USAGES) {
195 dbg("usage index exceeded");
196 return -1;
197 }
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++;
203 return 0;
204}
205
206/*
207 * Register a new field for this report.
208 */
209
210static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
211{
212 struct hid_report *report;
213 struct hid_field *field;
214 int usages;
215 unsigned offset;
216 int i;
217
218 if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
219 dbg("hid_register_report failed");
220 return -1;
221 }
222
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);
225 return -1;
226 }
227
228 offset = report->size;
229 report->size += parser->global.report_size * parser->global.report_count;
230
231 if (!parser->local.usage_index) /* Ignore padding fields */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 usages = max_t(int, parser->local.usage_index, parser->global.report_count);
235
236 if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
237 return 0;
238
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);
242
243 for (i = 0; i < usages; i++) {
244 int j = 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];
251 }
252
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;
265
266 return 0;
267}
268
269/*
270 * Read data value from item.
271 */
272
273static __inline__ __u32 item_udata(struct hid_item *item)
274{
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;
279 }
280 return 0;
281}
282
283static __inline__ __s32 item_sdata(struct hid_item *item)
284{
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;
289 }
290 return 0;
291}
292
293/*
294 * Process a global item.
295 */
296
297static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
298{
299 switch (item->tag) {
300
301 case HID_GLOBAL_ITEM_TAG_PUSH:
302
303 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
304 dbg("global enviroment stack overflow");
305 return -1;
306 }
307
308 memcpy(parser->global_stack + parser->global_stack_ptr++,
309 &parser->global, sizeof(struct hid_global));
310 return 0;
311
312 case HID_GLOBAL_ITEM_TAG_POP:
313
314 if (!parser->global_stack_ptr) {
315 dbg("global enviroment stack underflow");
316 return -1;
317 }
318
319 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
320 sizeof(struct hid_global));
321 return 0;
322
323 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
324 parser->global.usage_page = item_udata(item);
325 return 0;
326
327 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
328 parser->global.logical_minimum = item_sdata(item);
329 return 0;
330
331 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
332 if (parser->global.logical_minimum < 0)
333 parser->global.logical_maximum = item_sdata(item);
334 else
335 parser->global.logical_maximum = item_udata(item);
336 return 0;
337
338 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
339 parser->global.physical_minimum = item_sdata(item);
340 return 0;
341
342 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
343 if (parser->global.physical_minimum < 0)
344 parser->global.physical_maximum = item_sdata(item);
345 else
346 parser->global.physical_maximum = item_udata(item);
347 return 0;
348
349 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
350 parser->global.unit_exponent = item_sdata(item);
351 return 0;
352
353 case HID_GLOBAL_ITEM_TAG_UNIT:
354 parser->global.unit = item_udata(item);
355 return 0;
356
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);
360 return -1;
361 }
362 return 0;
363
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);
367 return -1;
368 }
369 return 0;
370
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");
374 return -1;
375 }
376 return 0;
377
378 default:
379 dbg("unknown global tag 0x%x", item->tag);
380 return -1;
381 }
382}
383
384/*
385 * Process a local item.
386 */
387
388static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
389{
390 __u32 data;
391 unsigned n;
392
393 if (item->size == 0) {
394 dbg("item data expected for local item");
395 return -1;
396 }
397
398 data = item_udata(item);
399
400 switch (item->tag) {
401
402 case HID_LOCAL_ITEM_TAG_DELIMITER:
403
404 if (data) {
405 /*
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.
410 */
411 if (parser->local.delimiter_depth != 0) {
412 dbg("nested delimiters");
413 return -1;
414 }
415 parser->local.delimiter_depth++;
416 parser->local.delimiter_branch++;
417 } else {
418 if (parser->local.delimiter_depth < 1) {
419 dbg("bogus close delimiter");
420 return -1;
421 }
422 parser->local.delimiter_depth--;
423 }
424 return 1;
425
426 case HID_LOCAL_ITEM_TAG_USAGE:
427
428 if (parser->local.delimiter_branch > 1) {
429 dbg("alternative usage ignored");
430 return 0;
431 }
432
433 if (item->size <= 2)
434 data = (parser->global.usage_page << 16) + data;
435
436 return hid_add_usage(parser, data);
437
438 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
439
440 if (parser->local.delimiter_branch > 1) {
441 dbg("alternative usage ignored");
442 return 0;
443 }
444
445 if (item->size <= 2)
446 data = (parser->global.usage_page << 16) + data;
447
448 parser->local.usage_minimum = data;
449 return 0;
450
451 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
452
453 if (parser->local.delimiter_branch > 1) {
454 dbg("alternative usage ignored");
455 return 0;
456 }
457
458 if (item->size <= 2)
459 data = (parser->global.usage_page << 16) + data;
460
461 for (n = parser->local.usage_minimum; n <= data; n++)
462 if (hid_add_usage(parser, n)) {
463 dbg("hid_add_usage failed\n");
464 return -1;
465 }
466 return 0;
467
468 default:
469
470 dbg("unknown local item tag 0x%x", item->tag);
471 return 0;
472 }
473 return 0;
474}
475
476/*
477 * Process a main item.
478 */
479
480static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
481{
482 __u32 data;
483 int ret;
484
485 data = item_udata(item);
486
487 switch (item->tag) {
488 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
489 ret = open_collection(parser, data & 0xff);
490 break;
491 case HID_MAIN_ITEM_TAG_END_COLLECTION:
492 ret = close_collection(parser);
493 break;
494 case HID_MAIN_ITEM_TAG_INPUT:
495 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
496 break;
497 case HID_MAIN_ITEM_TAG_OUTPUT:
498 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
499 break;
500 case HID_MAIN_ITEM_TAG_FEATURE:
501 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
502 break;
503 default:
504 dbg("unknown main item tag 0x%x", item->tag);
505 ret = 0;
506 }
507
508 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
509
510 return ret;
511}
512
513/*
514 * Process a reserved item.
515 */
516
517static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
518{
519 dbg("reserved item type, tag 0x%x", item->tag);
520 return 0;
521}
522
523/*
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.
527 */
528
529static void hid_free_report(struct hid_report *report)
530{
531 unsigned n;
532
533 for (n = 0; n < report->maxfield; n++)
534 kfree(report->field[n]);
535 kfree(report);
536}
537
538/*
539 * Free a device structure, all reports, and all fields.
540 */
541
542static void hid_free_device(struct hid_device *device)
543{
544 unsigned i,j;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for (i = 0; i < HID_REPORT_TYPES; i++) {
547 struct hid_report_enum *report_enum = device->report_enum + i;
548
549 for (j = 0; j < 256; j++) {
550 struct hid_report *report = report_enum->report_id_hash[j];
551 if (report)
552 hid_free_report(report);
553 }
554 }
555
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700556 kfree(device->rdesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 kfree(device);
558}
559
560/*
561 * Fetch a report description item from the data stream. We support long
562 * items, though they are not used yet.
563 */
564
565static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
566{
567 u8 b;
568
569 if ((end - start) <= 0)
570 return NULL;
571
572 b = *start++;
573
574 item->type = (b >> 2) & 3;
575 item->tag = (b >> 4) & 15;
576
577 if (item->tag == HID_ITEM_TAG_LONG) {
578
579 item->format = HID_ITEM_FORMAT_LONG;
580
581 if ((end - start) < 2)
582 return NULL;
583
584 item->size = *start++;
585 item->tag = *start++;
586
587 if ((end - start) < item->size)
588 return NULL;
589
590 item->data.longdata = start;
591 start += item->size;
592 return start;
593 }
594
595 item->format = HID_ITEM_FORMAT_SHORT;
596 item->size = b & 3;
597
598 switch (item->size) {
599
600 case 0:
601 return start;
602
603 case 1:
604 if ((end - start) < 1)
605 return NULL;
606 item->data.u8 = *start++;
607 return start;
608
609 case 2:
610 if ((end - start) < 2)
611 return NULL;
612 item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
613 start = (__u8 *)((__le16 *)start + 1);
614 return start;
615
616 case 3:
617 item->size++;
618 if ((end - start) < 4)
619 return NULL;
620 item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
621 start = (__u8 *)((__le32 *)start + 1);
622 return start;
623 }
624
625 return NULL;
626}
627
628/*
629 * Parse a report description into a hid_device structure. Reports are
630 * enumerated, fields are attached to these reports.
631 */
632
633static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
634{
635 struct hid_device *device;
636 struct hid_parser *parser;
637 struct hid_item item;
638 __u8 *end;
639 unsigned i;
640 static int (*dispatch_type[])(struct hid_parser *parser,
641 struct hid_item *item) = {
642 hid_parser_main,
643 hid_parser_global,
644 hid_parser_local,
645 hid_parser_reserved
646 };
647
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100648 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100651 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
653 kfree(device);
654 return NULL;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
657
658 for (i = 0; i < HID_REPORT_TYPES; i++)
659 INIT_LIST_HEAD(&device->report_enum[i].report_list);
660
661 if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
662 kfree(device->collection);
663 kfree(device);
664 return NULL;
665 }
666 memcpy(device->rdesc, start, size);
667 device->rsize = size;
668
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100669 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 kfree(device->rdesc);
671 kfree(device->collection);
672 kfree(device);
673 return NULL;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 parser->device = device;
676
677 end = start + size;
678 while ((start = fetch_item(start, end, &item)) != NULL) {
679
680 if (item.format != HID_ITEM_FORMAT_SHORT) {
681 dbg("unexpected long global item");
682 kfree(device->collection);
683 hid_free_device(device);
684 kfree(parser);
685 return NULL;
686 }
687
688 if (dispatch_type[item.type](parser, &item)) {
689 dbg("item %u %u %u %u parsing failed\n",
690 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
691 kfree(device->collection);
692 hid_free_device(device);
693 kfree(parser);
694 return NULL;
695 }
696
697 if (start == end) {
698 if (parser->collection_stack_ptr) {
699 dbg("unbalanced collection at end of report description");
700 kfree(device->collection);
701 hid_free_device(device);
702 kfree(parser);
703 return NULL;
704 }
705 if (parser->local.delimiter_depth) {
706 dbg("unbalanced delimiter at end of report description");
707 kfree(device->collection);
708 hid_free_device(device);
709 kfree(parser);
710 return NULL;
711 }
712 kfree(parser);
713 return device;
714 }
715 }
716
717 dbg("item fetching failed at offset %d\n", (int)(end - start));
718 kfree(device->collection);
719 hid_free_device(device);
720 kfree(parser);
721 return NULL;
722}
723
724/*
725 * Convert a signed n-bit integer to signed 32-bit integer. Common
726 * cases are done through the compiler, the screwed things has to be
727 * done by hand.
728 */
729
730static __inline__ __s32 snto32(__u32 value, unsigned n)
731{
732 switch (n) {
733 case 8: return ((__s8)value);
734 case 16: return ((__s16)value);
735 case 32: return ((__s32)value);
736 }
737 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
738}
739
740/*
741 * Convert a signed 32-bit integer to a signed n-bit integer.
742 */
743
744static __inline__ __u32 s32ton(__s32 value, unsigned n)
745{
746 __s32 a = value >> (n - 1);
747 if (a && a != -1)
748 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
749 return value & ((1 << n) - 1);
750}
751
752/*
753 * Extract/implement a data field from/to a report.
754 */
755
756static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
757{
758 report += (offset >> 5) << 2; offset &= 31;
Adam Kropelinbef37682005-05-29 02:30:08 -0500759 return (le64_to_cpu(get_unaligned((__le64*)report)) >> offset) & ((1ULL << n) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
763{
764 report += (offset >> 5) << 2; offset &= 31;
765 put_unaligned((get_unaligned((__le64*)report)
766 & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
767 | cpu_to_le64((__u64)value << offset), (__le64*)report);
768}
769
770/*
771 * Search an array for a value.
772 */
773
774static __inline__ int search(__s32 *array, __s32 value, unsigned n)
775{
776 while (n--) {
777 if (*array++ == value)
778 return 0;
779 }
780 return -1;
781}
782
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500783static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 hid_dump_input(usage, value);
786 if (hid->claimed & HID_CLAIMED_INPUT)
787 hidinput_hid_event(hid, field, usage, value, regs);
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500788 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 hiddev_hid_event(hid, field, usage, value, regs);
790}
791
792/*
793 * Analyse a received field, and fetch the data from it. The field
794 * content is stored for next report processing (we do differential
795 * reporting to the layer).
796 */
797
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500798static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 unsigned n;
801 unsigned count = field->report_count;
802 unsigned offset = field->report_offset;
803 unsigned size = field->report_size;
804 __s32 min = field->logical_minimum;
805 __s32 max = field->logical_maximum;
806 __s32 *value;
807
808 if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
809 return;
810
811 for (n = 0; n < count; n++) {
812
813 value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
814 extract(data, offset + n * size, size);
815
816 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
817 && value[n] >= min && value[n] <= max
818 && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
819 goto exit;
820 }
821
822 for (n = 0; n < count; n++) {
823
824 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500825 hid_process_event(hid, field, &field->usage[n], value[n], interrupt, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 continue;
827 }
828
829 if (field->value[n] >= min && field->value[n] <= max
830 && field->usage[field->value[n] - min].hid
831 && search(value, field->value[n], count))
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500832 hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (value[n] >= min && value[n] <= max
835 && field->usage[value[n] - min].hid
836 && search(field->value, value[n], count))
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500837 hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 memcpy(field->value, value, count * sizeof(__s32));
841exit:
842 kfree(value);
843}
844
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500845static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct hid_device *hid = urb->context;
848 struct hid_report_enum *report_enum = hid->report_enum + type;
849 u8 *data = urb->transfer_buffer;
850 int len = urb->actual_length;
851 struct hid_report *report;
852 int n, size;
853
854 if (!len) {
855 dbg("empty report");
856 return -1;
857 }
858
859#ifdef DEBUG_DATA
860 printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
861#endif
862
863 n = 0; /* Normally report number is 0 */
864 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
865 n = *data++;
866 len--;
867 }
868
869#ifdef DEBUG_DATA
870 {
871 int i;
872 printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
873 for (i = 0; i < len; i++)
874 printk(" %02x", data[i]);
875 printk("\n");
876 }
877#endif
878
879 if (!(report = report_enum->report_id_hash[n])) {
880 dbg("undefined report_id %d received", n);
881 return -1;
882 }
883
884 size = ((report->size - 1) >> 3) + 1;
885
Adam Kropelincd610452005-12-13 17:03:39 -0800886 if (len < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dbg("report %d is too short, (%d < %d)", report->id, len, size);
Adam Kropelincd610452005-12-13 17:03:39 -0800888 memset(data + len, 0, size - len);
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (hid->claimed & HID_CLAIMED_HIDDEV)
892 hiddev_report_event(hid, report);
893
894 for (n = 0; n < report->maxfield; n++)
Adam Kropelinbc5d0482005-07-11 01:09:32 -0500895 hid_input_field(hid, report->field[n], data, interrupt, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (hid->claimed & HID_CLAIMED_INPUT)
898 hidinput_report_event(hid, report);
899
900 return 0;
901}
902
903/*
Alan Sternaef4e262006-01-31 12:58:38 -0500904 * Input submission and I/O error handler.
905 */
906
907static void hid_io_error(struct hid_device *hid);
908
909/* Start up the input URB */
910static int hid_start_in(struct hid_device *hid)
911{
912 unsigned long flags;
913 int rc = 0;
914
915 spin_lock_irqsave(&hid->inlock, flags);
916 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
917 !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
918 rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
919 if (rc != 0)
920 clear_bit(HID_IN_RUNNING, &hid->iofl);
921 }
922 spin_unlock_irqrestore(&hid->inlock, flags);
923 return rc;
924}
925
926/* I/O retry timer routine */
927static void hid_retry_timeout(unsigned long _hid)
928{
929 struct hid_device *hid = (struct hid_device *) _hid;
930
931 dev_dbg(&hid->intf->dev, "retrying intr urb\n");
932 if (hid_start_in(hid))
933 hid_io_error(hid);
934}
935
936/* Workqueue routine to reset the device */
937static void hid_reset(void *_hid)
938{
939 struct hid_device *hid = (struct hid_device *) _hid;
940 int rc_lock, rc;
941
942 dev_dbg(&hid->intf->dev, "resetting device\n");
943 rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
944 if (rc_lock >= 0) {
Alan Sterndf9a1f42006-06-01 13:55:28 -0400945 rc = usb_reset_composite_device(hid->dev, hid->intf);
Alan Sternaef4e262006-01-31 12:58:38 -0500946 if (rc_lock)
947 usb_unlock_device(hid->dev);
948 }
949 clear_bit(HID_RESET_PENDING, &hid->iofl);
950
Alan Sterndf9a1f42006-06-01 13:55:28 -0400951 switch (rc) {
952 case 0:
953 if (!test_bit(HID_IN_RUNNING, &hid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500954 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400955 break;
956 default:
Alan Sternaef4e262006-01-31 12:58:38 -0500957 err("can't reset device, %s-%s/input%d, status %d",
958 hid->dev->bus->bus_name,
959 hid->dev->devpath,
960 hid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400961 /* FALLTHROUGH */
962 case -EHOSTUNREACH:
963 case -ENODEV:
964 case -EINTR:
965 break;
966 }
Alan Sternaef4e262006-01-31 12:58:38 -0500967}
968
969/* Main I/O error handler */
970static void hid_io_error(struct hid_device *hid)
971{
972 unsigned long flags;
973
974 spin_lock_irqsave(&hid->inlock, flags);
975
976 /* Stop when disconnected */
977 if (usb_get_intfdata(hid->intf) == NULL)
978 goto done;
979
980 /* When an error occurs, retry at increasing intervals */
981 if (hid->retry_delay == 0) {
982 hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
983 hid->stop_retry = jiffies + msecs_to_jiffies(1000);
984 } else if (hid->retry_delay < 100)
985 hid->retry_delay *= 2;
986
987 if (time_after(jiffies, hid->stop_retry)) {
988
989 /* Retries failed, so do a port reset */
990 if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
991 if (schedule_work(&hid->reset_work))
992 goto done;
993 clear_bit(HID_RESET_PENDING, &hid->iofl);
994 }
995 }
996
997 mod_timer(&hid->io_retry,
998 jiffies + msecs_to_jiffies(hid->retry_delay));
999done:
1000 spin_unlock_irqrestore(&hid->inlock, flags);
1001}
1002
1003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Input interrupt completion handler.
1005 */
1006
1007static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
1008{
1009 struct hid_device *hid = urb->context;
1010 int status;
1011
1012 switch (urb->status) {
1013 case 0: /* success */
Alan Sternaef4e262006-01-31 12:58:38 -05001014 hid->retry_delay = 0;
Adam Kropelinbc5d0482005-07-11 01:09:32 -05001015 hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 case -ECONNRESET: /* unlink */
1018 case -ENOENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 case -ESHUTDOWN: /* unplug */
Alan Sternaef4e262006-01-31 12:58:38 -05001020 clear_bit(HID_IN_RUNNING, &hid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return;
Alan Sternaef4e262006-01-31 12:58:38 -05001022 case -EILSEQ: /* protocol error or unplug */
1023 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 case -ETIMEDOUT: /* NAK */
Alan Sternaef4e262006-01-31 12:58:38 -05001025 clear_bit(HID_IN_RUNNING, &hid->iofl);
1026 hid_io_error(hid);
1027 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 default: /* error */
1029 warn("input irq status %d received", urb->status);
1030 }
1031
1032 status = usb_submit_urb(urb, SLAB_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -05001033 if (status) {
1034 clear_bit(HID_IN_RUNNING, &hid->iofl);
1035 if (status != -EPERM) {
1036 err("can't resubmit intr, %s-%s/input%d, status %d",
1037 hid->dev->bus->bus_name,
1038 hid->dev->devpath,
1039 hid->ifnum, status);
1040 hid_io_error(hid);
1041 }
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*
1046 * Output the field into the report.
1047 */
1048
1049static void hid_output_field(struct hid_field *field, __u8 *data)
1050{
1051 unsigned count = field->report_count;
1052 unsigned offset = field->report_offset;
1053 unsigned size = field->report_size;
1054 unsigned n;
1055
1056 for (n = 0; n < count; n++) {
1057 if (field->logical_minimum < 0) /* signed values */
1058 implement(data, offset + n * size, size, s32ton(field->value[n], size));
1059 else /* unsigned values */
1060 implement(data, offset + n * size, size, field->value[n]);
1061 }
1062}
1063
1064/*
1065 * Create a report.
1066 */
1067
1068static void hid_output_report(struct hid_report *report, __u8 *data)
1069{
1070 unsigned n;
1071
1072 if (report->id > 0)
1073 *data++ = report->id;
1074
1075 for (n = 0; n < report->maxfield; n++)
1076 hid_output_field(report->field[n], data);
1077}
1078
1079/*
1080 * Set a field value. The report this field belongs to has to be
1081 * created and transferred to the device, to set this value in the
1082 * device.
1083 */
1084
1085int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1086{
1087 unsigned size = field->report_size;
1088
1089 hid_dump_input(field->usage + offset, value);
1090
1091 if (offset >= field->report_count) {
1092 dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
1093 hid_dump_field(field, 8);
1094 return -1;
1095 }
1096 if (field->logical_minimum < 0) {
1097 if (value != snto32(s32ton(value, size), size)) {
1098 dbg("value %d is out of range", value);
1099 return -1;
1100 }
1101 }
1102 field->value[offset] = value;
1103 return 0;
1104}
1105
1106/*
1107 * Find a report field with a specified HID usage.
1108 */
Adrian Bunkf287cae2006-08-04 23:00:02 -04001109#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type)
1111{
1112 struct hid_report *report;
1113 int i;
1114
1115 list_for_each_entry(report, &hid->report_enum[type].report_list, list)
1116 for (i = 0; i < report->maxfield; i++)
1117 if (report->field[i]->logical == wanted_usage)
1118 return report->field[i];
1119 return NULL;
1120}
Adrian Bunkf287cae2006-08-04 23:00:02 -04001121#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123static int hid_submit_out(struct hid_device *hid)
1124{
1125 struct hid_report *report;
1126
1127 report = hid->out[hid->outtail];
1128
1129 hid_output_report(report, hid->outbuf);
1130 hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1131 hid->urbout->dev = hid->dev;
1132
1133 dbg("submitting out urb");
1134
1135 if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
1136 err("usb_submit_urb(out) failed");
1137 return -1;
1138 }
1139
1140 return 0;
1141}
1142
1143static int hid_submit_ctrl(struct hid_device *hid)
1144{
1145 struct hid_report *report;
1146 unsigned char dir;
1147 int len;
1148
1149 report = hid->ctrl[hid->ctrltail].report;
1150 dir = hid->ctrl[hid->ctrltail].dir;
1151
1152 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1153 if (dir == USB_DIR_OUT) {
1154 hid_output_report(report, hid->ctrlbuf);
1155 hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0);
1156 hid->urbctrl->transfer_buffer_length = len;
1157 } else {
1158 int maxpacket, padlen;
1159
1160 hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0);
1161 maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0);
1162 if (maxpacket > 0) {
1163 padlen = (len + maxpacket - 1) / maxpacket;
1164 padlen *= maxpacket;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001165 if (padlen > hid->bufsize)
1166 padlen = hid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else
1168 padlen = 0;
1169 hid->urbctrl->transfer_buffer_length = padlen;
1170 }
1171 hid->urbctrl->dev = hid->dev;
1172
1173 hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
1174 hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
1175 hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1176 hid->cr->wIndex = cpu_to_le16(hid->ifnum);
1177 hid->cr->wLength = cpu_to_le16(len);
1178
1179 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
1180 hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
1181 hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength);
1182
1183 if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
1184 err("usb_submit_urb(ctrl) failed");
1185 return -1;
1186 }
1187
1188 return 0;
1189}
1190
1191/*
1192 * Output interrupt completion handler.
1193 */
1194
1195static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1196{
1197 struct hid_device *hid = urb->context;
1198 unsigned long flags;
1199 int unplug = 0;
1200
1201 switch (urb->status) {
1202 case 0: /* success */
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -05001203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -05001206 case -EILSEQ: /* protocol error or unplug */
1207 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 case -ECONNRESET: /* unlink */
1209 case -ENOENT:
1210 break;
1211 default: /* error */
1212 warn("output irq status %d received", urb->status);
1213 }
1214
1215 spin_lock_irqsave(&hid->outlock, flags);
1216
1217 if (unplug)
1218 hid->outtail = hid->outhead;
1219 else
1220 hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1221
1222 if (hid->outhead != hid->outtail) {
1223 if (hid_submit_out(hid)) {
Alexey Dobriyan53b35312006-03-24 03:16:13 -08001224 clear_bit(HID_OUT_RUNNING, &hid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 wake_up(&hid->wait);
1226 }
1227 spin_unlock_irqrestore(&hid->outlock, flags);
1228 return;
1229 }
1230
1231 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1232 spin_unlock_irqrestore(&hid->outlock, flags);
1233 wake_up(&hid->wait);
1234}
1235
1236/*
1237 * Control pipe completion handler.
1238 */
1239
1240static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1241{
1242 struct hid_device *hid = urb->context;
1243 unsigned long flags;
1244 int unplug = 0;
1245
1246 spin_lock_irqsave(&hid->ctrllock, flags);
1247
1248 switch (urb->status) {
1249 case 0: /* success */
1250 if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
Adam Kropelinbc5d0482005-07-11 01:09:32 -05001251 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -05001252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -05001255 case -EILSEQ: /* protocol error or unplug */
1256 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 case -ECONNRESET: /* unlink */
1258 case -ENOENT:
1259 case -EPIPE: /* report not available */
1260 break;
1261 default: /* error */
1262 warn("ctrl urb status %d received", urb->status);
1263 }
1264
1265 if (unplug)
1266 hid->ctrltail = hid->ctrlhead;
1267 else
1268 hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1269
1270 if (hid->ctrlhead != hid->ctrltail) {
1271 if (hid_submit_ctrl(hid)) {
1272 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1273 wake_up(&hid->wait);
1274 }
1275 spin_unlock_irqrestore(&hid->ctrllock, flags);
1276 return;
1277 }
1278
1279 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1280 spin_unlock_irqrestore(&hid->ctrllock, flags);
1281 wake_up(&hid->wait);
1282}
1283
1284void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
1285{
1286 int head;
1287 unsigned long flags;
1288
1289 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
1290 return;
1291
1292 if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1293
1294 spin_lock_irqsave(&hid->outlock, flags);
1295
1296 if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
1297 spin_unlock_irqrestore(&hid->outlock, flags);
1298 warn("output queue full");
1299 return;
1300 }
1301
1302 hid->out[hid->outhead] = report;
1303 hid->outhead = head;
1304
1305 if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1306 if (hid_submit_out(hid))
1307 clear_bit(HID_OUT_RUNNING, &hid->iofl);
1308
1309 spin_unlock_irqrestore(&hid->outlock, flags);
1310 return;
1311 }
1312
1313 spin_lock_irqsave(&hid->ctrllock, flags);
1314
1315 if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
1316 spin_unlock_irqrestore(&hid->ctrllock, flags);
1317 warn("control queue full");
1318 return;
1319 }
1320
1321 hid->ctrl[hid->ctrlhead].report = report;
1322 hid->ctrl[hid->ctrlhead].dir = dir;
1323 hid->ctrlhead = head;
1324
1325 if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
1326 if (hid_submit_ctrl(hid))
1327 clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1328
1329 spin_unlock_irqrestore(&hid->ctrllock, flags);
1330}
1331
1332int hid_wait_io(struct hid_device *hid)
1333{
1334 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &hid->iofl) &&
1335 !test_bit(HID_OUT_RUNNING, &hid->iofl)),
1336 10*HZ)) {
1337 dbg("timeout waiting for ctrl or out queue to clear");
1338 return -1;
1339 }
1340
1341 return 0;
1342}
1343
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001344static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
1345{
Vojtech Pavlik71387bd72005-05-29 02:28:14 -05001346 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001347 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
1348 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1352 unsigned char type, void *buf, int size)
1353{
1354 int result, retries = 4;
1355
1356 memset(buf,0,size); // Make sure we parse really received data
1357
1358 do {
1359 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1360 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
1361 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
1362 retries--;
1363 } while (result < size && retries);
1364 return result;
1365}
1366
1367int hid_open(struct hid_device *hid)
1368{
Alan Sternaef4e262006-01-31 12:58:38 -05001369 ++hid->open;
1370 if (hid_start_in(hid))
1371 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return 0;
1373}
1374
1375void hid_close(struct hid_device *hid)
1376{
1377 if (!--hid->open)
1378 usb_kill_urb(hid->urbin);
1379}
1380
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001381#define USB_VENDOR_ID_PANJIT 0x134c
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*
1384 * Initialize all reports
1385 */
1386
1387void hid_init_reports(struct hid_device *hid)
1388{
1389 struct hid_report *report;
1390 int err, ret;
1391
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001392 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 hid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
1396 hid_submit_report(hid, report, USB_DIR_IN);
1397
1398 err = 0;
1399 ret = hid_wait_io(hid);
1400 while (ret) {
1401 err |= ret;
1402 if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
1403 usb_kill_urb(hid->urbctrl);
1404 if (test_bit(HID_OUT_RUNNING, &hid->iofl))
1405 usb_kill_urb(hid->urbout);
1406 ret = hid_wait_io(hid);
1407 }
1408
1409 if (err)
Olaf Heringde289fd2006-01-06 12:45:28 +01001410 warn("timeout initializing reports");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413#define USB_VENDOR_ID_WACOM 0x056a
1414#define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
1415#define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1416#define USB_DEVICE_ID_WACOM_INTUOS 0x0020
1417#define USB_DEVICE_ID_WACOM_PL 0x0030
1418#define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
1419#define USB_DEVICE_ID_WACOM_VOLITO 0x0060
1420#define USB_DEVICE_ID_WACOM_PTU 0x0003
Ping Cheng5ce04822005-05-05 15:12:57 -07001421#define USB_DEVICE_ID_WACOM_INTUOS3 0x00B0
1422#define USB_DEVICE_ID_WACOM_CINTIQ 0x003F
Ping Cheng116d75b2005-11-17 09:46:33 -08001423#define USB_DEVICE_ID_WACOM_DTF 0x00C0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Stephane VOLTZ53880542005-06-06 02:22:37 -05001425#define USB_VENDOR_ID_ACECAD 0x0460
1426#define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
1427#define USB_DEVICE_ID_ACECAD_302 0x0008
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429#define USB_VENDOR_ID_KBGEAR 0x084e
1430#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
1431
1432#define USB_VENDOR_ID_AIPTEK 0x08ca
1433#define USB_DEVICE_ID_AIPTEK_01 0x0001
1434#define USB_DEVICE_ID_AIPTEK_10 0x0010
1435#define USB_DEVICE_ID_AIPTEK_20 0x0020
1436#define USB_DEVICE_ID_AIPTEK_21 0x0021
1437#define USB_DEVICE_ID_AIPTEK_22 0x0022
1438#define USB_DEVICE_ID_AIPTEK_23 0x0023
1439#define USB_DEVICE_ID_AIPTEK_24 0x0024
1440
1441#define USB_VENDOR_ID_GRIFFIN 0x077d
1442#define USB_DEVICE_ID_POWERMATE 0x0410
1443#define USB_DEVICE_ID_SOUNDKNOB 0x04AA
1444
1445#define USB_VENDOR_ID_ATEN 0x0557
1446#define USB_DEVICE_ID_ATEN_UC100KM 0x2004
1447#define USB_DEVICE_ID_ATEN_CS124U 0x2202
1448#define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
1449#define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
1450#define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
1451
1452#define USB_VENDOR_ID_TOPMAX 0x0663
1453#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
1454
1455#define USB_VENDOR_ID_HAPP 0x078b
1456#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
1457#define USB_DEVICE_ID_UGCI_FLYING 0x0020
1458#define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
1459
1460#define USB_VENDOR_ID_MGE 0x0463
1461#define USB_DEVICE_ID_MGE_UPS 0xffff
1462#define USB_DEVICE_ID_MGE_UPS1 0x0001
1463
1464#define USB_VENDOR_ID_ONTRAK 0x0a07
1465#define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1468#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1469
1470#define USB_VENDOR_ID_A4TECH 0x09da
1471#define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
1472
Vojtech Pavlik61cdecd2005-09-05 00:13:32 -05001473#define USB_VENDOR_ID_AASHIMA 0x06d6
Luca T6345fdf2005-07-11 01:08:40 -05001474#define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
Vojtech Pavlik61cdecd2005-09-05 00:13:32 -05001475#define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
Luca T6345fdf2005-07-11 01:08:40 -05001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477#define USB_VENDOR_ID_CYPRESS 0x04b4
1478#define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
1479#define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
Brian Schau7d252582005-09-05 01:57:41 -05001480#define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482#define USB_VENDOR_ID_BERKSHIRE 0x0c98
1483#define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
1484
1485#define USB_VENDOR_ID_ALPS 0x0433
1486#define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
1487
1488#define USB_VENDOR_ID_SAITEK 0x06a3
1489#define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
1490
1491#define USB_VENDOR_ID_NEC 0x073e
1492#define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
1493
1494#define USB_VENDOR_ID_CHIC 0x05fe
1495#define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
1496
1497#define USB_VENDOR_ID_GLAB 0x06c2
1498#define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
1499#define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
1500#define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
1501#define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
1502#define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
1503
1504#define USB_VENDOR_ID_WISEGROUP 0x0925
1505#define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
1506#define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
Andrew Fullere65335e2006-02-25 09:52:27 -05001507#define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509#define USB_VENDOR_ID_CODEMERCS 0x07c0
1510#define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
1511#define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
1512#define USB_DEVICE_ID_CODEMERCS_IOW48 0x1502
1513#define USB_DEVICE_ID_CODEMERCS_IOW28 0x1503
1514
1515#define USB_VENDOR_ID_DELORME 0x1163
1516#define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
Lonnie Mendezdc1d1002005-05-10 00:17:17 -05001517#define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519#define USB_VENDOR_ID_MCC 0x09db
1520#define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
1521#define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
1522
Greg Kroah-Hartman4871d3b2005-06-02 22:18:12 -07001523#define USB_VENDOR_ID_VERNIER 0x08f7
1524#define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
1525#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
1526#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
1527#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
1528
Michael Hund8fd6db42005-06-27 22:44:22 +02001529#define USB_VENDOR_ID_LD 0x0f11
Michael Hundba3e66e2006-02-02 09:36:43 +01001530#define USB_DEVICE_ID_LD_CASSY 0x1000
1531#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
1532#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
1533#define USB_DEVICE_ID_LD_JWM 0x1080
1534#define USB_DEVICE_ID_LD_DMMP 0x1081
1535#define USB_DEVICE_ID_LD_UMIP 0x1090
1536#define USB_DEVICE_ID_LD_XRAY1 0x1100
1537#define USB_DEVICE_ID_LD_XRAY2 0x1101
1538#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
1539#define USB_DEVICE_ID_LD_COM3LAB 0x2000
1540#define USB_DEVICE_ID_LD_TELEPORT 0x2010
1541#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
1542#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
1543#define USB_DEVICE_ID_LD_MACHINETEST 0x2040
Michael Hund8fd6db42005-06-27 22:44:22 +02001544
Vojtech Pavlikc58de6d2005-09-05 00:13:15 -05001545#define USB_VENDOR_ID_APPLE 0x05ac
Bart Masseya82e49b2006-05-08 14:40:13 -07001546#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001548#define USB_VENDOR_ID_CHERRY 0x046a
1549#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
1550
Henk Vergonet5cd330f2006-05-15 12:34:43 +02001551#define USB_VENDOR_ID_YEALINK 0x6993
1552#define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/*
1554 * Alphabetically sorted blacklist by quirk type.
1555 */
1556
Arjan van de Ven4c4c9432005-11-29 09:43:42 +01001557static const struct hid_blacklist {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 __u16 idVendor;
1559 __u16 idProduct;
1560 unsigned quirks;
1561} hid_blacklist[] = {
1562
1563 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1564 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1565 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1566 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1567 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1568 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1569 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1570 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
1571 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40, HID_QUIRK_IGNORE },
1572 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24, HID_QUIRK_IGNORE },
1573 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW48, HID_QUIRK_IGNORE },
1574 { USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28, HID_QUIRK_IGNORE },
1575 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
Brian Schau7d252582005-09-05 01:57:41 -05001576 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
Lonnie Mendezdc1d1002005-05-10 00:17:17 -05001578 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1580 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1581 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
1582 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
1583 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
1584 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
1585 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1586 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1587 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
Michael Hundba3e66e2006-02-02 09:36:43 +01001588 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
1589 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
1590 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
1591 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
1592 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
1593 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
1594 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
1595 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
1596 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
1597 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
1598 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
1599 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
1600 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
1601 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
1603 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
1604 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1605 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1606 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1607 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1608 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1609 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1610 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1611 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
Greg Kroah-Hartman4871d3b2005-06-02 22:18:12 -07001612 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
1613 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
1614 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
1615 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1617 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1618 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1619 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1620 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1621 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1622 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1623 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1624 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1625 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1626 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1627 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1628 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1629 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1630 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1631 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1632 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
Ping Cheng116d75b2005-11-17 09:46:33 -08001633 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 7, HID_QUIRK_IGNORE },
1634 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 8, HID_QUIRK_IGNORE },
1635 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 9, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1637 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1638 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1639 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1640 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1641 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1642 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
Ping Cheng116d75b2005-11-17 09:46:33 -08001643 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 1, HID_QUIRK_IGNORE },
1644 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 2, HID_QUIRK_IGNORE },
1645 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 3, HID_QUIRK_IGNORE },
1646 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO + 4, HID_QUIRK_IGNORE },
1647 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 5, HID_QUIRK_IGNORE },
1648 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 6, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
Ping Cheng5ce04822005-05-05 15:12:57 -07001650 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
1651 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
1652 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
Ping Cheng999a6a62006-03-29 16:34:16 -08001653 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 3, HID_QUIRK_IGNORE },
1654 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 4, HID_QUIRK_IGNORE },
Ping Cheng116d75b2005-11-17 09:46:33 -08001655 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 5, HID_QUIRK_IGNORE },
Ping Cheng5ce04822005-05-05 15:12:57 -07001656 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_CINTIQ, HID_QUIRK_IGNORE },
Ping Cheng116d75b2005-11-17 09:46:33 -08001657 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_DTF, HID_QUIRK_IGNORE },
Ping Cheng999a6a62006-03-29 16:34:16 -08001658 { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_DTF + 3, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
1660 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
Henk Vergonet5cd330f2006-05-15 12:34:43 +02001661 { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Stephane VOLTZ53880542005-06-06 02:22:37 -05001663 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
1664 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1667 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1668 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1669 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1670 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
Andrew Fullere65335e2006-02-25 09:52:27 -05001671 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Bart Masseya82e49b2006-05-08 14:40:13 -07001673 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
1675 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
1676
Luca T6345fdf2005-07-11 01:08:40 -05001677 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
Vojtech Pavlik61cdecd2005-09-05 00:13:32 -05001678 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
1680 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
1681 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1682 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1683 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
1684 { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1685 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
1686 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1687
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001688 { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_CYMOTION },
1689
Michael Hanselmanneab9edd2006-01-14 10:08:06 -05001690 { USB_VENDOR_ID_APPLE, 0x020E, HID_QUIRK_POWERBOOK_HAS_FN },
1691 { USB_VENDOR_ID_APPLE, 0x020F, HID_QUIRK_POWERBOOK_HAS_FN },
1692 { USB_VENDOR_ID_APPLE, 0x0214, HID_QUIRK_POWERBOOK_HAS_FN },
1693 { USB_VENDOR_ID_APPLE, 0x0215, HID_QUIRK_POWERBOOK_HAS_FN },
1694 { USB_VENDOR_ID_APPLE, 0x0216, HID_QUIRK_POWERBOOK_HAS_FN },
Rene Rebe5c218e32006-06-04 11:39:09 +02001695 { USB_VENDOR_ID_APPLE, 0x0217, HID_QUIRK_POWERBOOK_HAS_FN },
1696 { USB_VENDOR_ID_APPLE, 0x0218, HID_QUIRK_POWERBOOK_HAS_FN },
1697 { USB_VENDOR_ID_APPLE, 0x0219, HID_QUIRK_POWERBOOK_HAS_FN },
Michael Hanselmanneab9edd2006-01-14 10:08:06 -05001698 { USB_VENDOR_ID_APPLE, 0x030A, HID_QUIRK_POWERBOOK_HAS_FN },
1699 { USB_VENDOR_ID_APPLE, 0x030B, HID_QUIRK_POWERBOOK_HAS_FN },
1700
Daniel Ritz1d3e2022006-03-29 22:41:07 +02001701 { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
1702 { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
1703 { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
1704 { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 { 0, 0 }
1707};
1708
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001709/*
1710 * Traverse the supplied list of reports and find the longest
1711 */
1712static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
1713{
1714 struct hid_report *report;
1715 int size;
1716
1717 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
1718 size = ((report->size - 1) >> 3) + 1;
1719 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
1720 size++;
1721 if (*max < size)
1722 *max = size;
1723 }
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1727{
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001728 if (!(hid->inbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->inbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -1;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001730 if (!(hid->outbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->outbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -1;
1732 if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
1733 return -1;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001734 if (!(hid->ctrlbuf = usb_buffer_alloc(dev, hid->bufsize, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return -1;
1736
1737 return 0;
1738}
1739
1740static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1741{
1742 if (hid->inbuf)
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001743 usb_buffer_free(dev, hid->bufsize, hid->inbuf, hid->inbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (hid->outbuf)
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001745 usb_buffer_free(dev, hid->bufsize, hid->outbuf, hid->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (hid->cr)
1747 usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
1748 if (hid->ctrlbuf)
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001749 usb_buffer_free(dev, hid->bufsize, hid->ctrlbuf, hid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001752/*
1753 * Cherry Cymotion keyboard have an invalid HID report descriptor,
1754 * that needs fixing before we can parse it.
1755 */
1756
1757static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1758{
1759 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1760 info("Fixing up Cherry Cymotion report descriptor");
1761 rdesc[11] = rdesc[16] = 0xff;
1762 rdesc[12] = rdesc[17] = 0x03;
1763 }
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1767{
1768 struct usb_host_interface *interface = intf->cur_altsetting;
1769 struct usb_device *dev = interface_to_usbdev (intf);
1770 struct hid_descriptor *hdesc;
1771 struct hid_device *hid;
1772 unsigned quirks = 0, rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001773 char *rdesc;
1774 int n, len, insize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 for (n = 0; hid_blacklist[n].idVendor; n++)
1777 if ((hid_blacklist[n].idVendor == le16_to_cpu(dev->descriptor.idVendor)) &&
1778 (hid_blacklist[n].idProduct == le16_to_cpu(dev->descriptor.idProduct)))
1779 quirks = hid_blacklist[n].quirks;
1780
Alan Stern0f28b552006-05-15 14:49:04 -04001781 /* Many keyboards and mice don't like to be polled for reports,
1782 * so we will always set the HID_QUIRK_NOGET flag for them. */
1783 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1784 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1785 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1786 quirks |= HID_QUIRK_NOGET;
1787 }
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (quirks & HID_QUIRK_IGNORE)
1790 return NULL;
1791
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001792 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1793 (!interface->desc.bNumEndpoints ||
1794 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1795 dbg("class descriptor not present\n");
1796 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
1799 for (n = 0; n < hdesc->bNumDescriptors; n++)
1800 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1801 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1802
1803 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1804 dbg("weird size of report descriptor (%u)", rsize);
1805 return NULL;
1806 }
1807
1808 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1809 dbg("couldn't allocate rdesc memory");
1810 return NULL;
1811 }
1812
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001813 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1816 dbg("reading report descriptor failed");
1817 kfree(rdesc);
1818 return NULL;
1819 }
1820
Vojtech Pavlik940824b2006-01-14 00:25:39 -05001821 if ((quirks & HID_QUIRK_CYMOTION))
1822 hid_fixup_cymotion_descriptor(rdesc, rsize);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824#ifdef DEBUG_DATA
1825 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1826 for (n = 0; n < rsize; n++)
1827 printk(" %02x", (unsigned char) rdesc[n]);
1828 printk("\n");
1829#endif
1830
1831 if (!(hid = hid_parse_report(rdesc, n))) {
1832 dbg("parsing report descriptor failed");
1833 kfree(rdesc);
1834 return NULL;
1835 }
1836
1837 kfree(rdesc);
1838 hid->quirks = quirks;
1839
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001840 hid->bufsize = HID_MIN_BUFFER_SIZE;
1841 hid_find_max_report(hid, HID_INPUT_REPORT, &hid->bufsize);
1842 hid_find_max_report(hid, HID_OUTPUT_REPORT, &hid->bufsize);
1843 hid_find_max_report(hid, HID_FEATURE_REPORT, &hid->bufsize);
1844
1845 if (hid->bufsize > HID_MAX_BUFFER_SIZE)
1846 hid->bufsize = HID_MAX_BUFFER_SIZE;
1847
1848 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1849
1850 if (insize > HID_MAX_BUFFER_SIZE)
1851 insize = HID_MAX_BUFFER_SIZE;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (hid_alloc_buffers(dev, hid)) {
1854 hid_free_buffers(dev, hid);
1855 goto fail;
1856 }
1857
1858 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1859
1860 struct usb_endpoint_descriptor *endpoint;
1861 int pipe;
1862 int interval;
1863
1864 endpoint = &interface->endpoint[n].desc;
1865 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1866 continue;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* Change the polling interval of mice. */
1871 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1872 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (endpoint->bEndpointAddress & USB_DIR_IN) {
1875 if (hid->urbin)
1876 continue;
1877 if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1878 goto fail;
1879 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001880 usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 hid_irq_in, hid, interval);
1882 hid->urbin->transfer_dma = hid->inbuf_dma;
Alan Sternb375a042005-07-29 16:11:07 -04001883 hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 } else {
1885 if (hid->urbout)
1886 continue;
1887 if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1888 goto fail;
1889 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1890 usb_fill_int_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
1891 hid_irq_out, hid, interval);
1892 hid->urbout->transfer_dma = hid->outbuf_dma;
Alan Sternb375a042005-07-29 16:11:07 -04001893 hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 }
1896
1897 if (!hid->urbin) {
1898 err("couldn't find an input interrupt endpoint");
1899 goto fail;
1900 }
1901
1902 init_waitqueue_head(&hid->wait);
1903
Alan Sternaef4e262006-01-31 12:58:38 -05001904 INIT_WORK(&hid->reset_work, hid_reset, hid);
1905 setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
1906
1907 spin_lock_init(&hid->inlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 spin_lock_init(&hid->outlock);
1909 spin_lock_init(&hid->ctrllock);
1910
1911 hid->version = le16_to_cpu(hdesc->bcdHID);
1912 hid->country = hdesc->bCountryCode;
1913 hid->dev = dev;
1914 hid->intf = intf;
1915 hid->ifnum = interface->desc.bInterfaceNumber;
1916
1917 hid->name[0] = 0;
1918
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001919 if (dev->manufacturer)
1920 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001922 if (dev->product) {
1923 if (dev->manufacturer)
1924 strlcat(hid->name, " ", sizeof(hid->name));
1925 strlcat(hid->name, dev->product, sizeof(hid->name));
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001928 if (!strlen(hid->name))
1929 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1930 le16_to_cpu(dev->descriptor.idVendor),
1931 le16_to_cpu(dev->descriptor.idProduct));
1932
1933 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1934 strlcat(hid->phys, "/input", sizeof(hid->phys));
1935 len = strlen(hid->phys);
1936 if (len < sizeof(hid->phys) - 1)
1937 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1938 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1941 hid->uniq[0] = 0;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1944 if (!hid->urbctrl)
1945 goto fail;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
1948 hid->ctrlbuf, 1, hid_ctrl, hid);
1949 hid->urbctrl->setup_dma = hid->cr_dma;
1950 hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
Alan Sternb375a042005-07-29 16:11:07 -04001951 hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 return hid;
1954
1955fail:
1956
1957 if (hid->urbin)
1958 usb_free_urb(hid->urbin);
1959 if (hid->urbout)
1960 usb_free_urb(hid->urbout);
1961 if (hid->urbctrl)
1962 usb_free_urb(hid->urbctrl);
1963 hid_free_buffers(dev, hid);
1964 hid_free_device(hid);
1965
1966 return NULL;
1967}
1968
1969static void hid_disconnect(struct usb_interface *intf)
1970{
1971 struct hid_device *hid = usb_get_intfdata (intf);
1972
1973 if (!hid)
1974 return;
1975
Alan Sternaef4e262006-01-31 12:58:38 -05001976 spin_lock_irq(&hid->inlock); /* Sync with error handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 usb_set_intfdata(intf, NULL);
Alan Sternaef4e262006-01-31 12:58:38 -05001978 spin_unlock_irq(&hid->inlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 usb_kill_urb(hid->urbin);
1980 usb_kill_urb(hid->urbout);
1981 usb_kill_urb(hid->urbctrl);
1982
Alan Sternaef4e262006-01-31 12:58:38 -05001983 del_timer_sync(&hid->io_retry);
1984 flush_scheduled_work();
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (hid->claimed & HID_CLAIMED_INPUT)
1987 hidinput_disconnect(hid);
1988 if (hid->claimed & HID_CLAIMED_HIDDEV)
1989 hiddev_disconnect(hid);
1990
1991 usb_free_urb(hid->urbin);
1992 usb_free_urb(hid->urbctrl);
1993 if (hid->urbout)
1994 usb_free_urb(hid->urbout);
1995
1996 hid_free_buffers(hid->dev, hid);
1997 hid_free_device(hid);
1998}
1999
2000static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
2001{
2002 struct hid_device *hid;
2003 char path[64];
2004 int i;
2005 char *c;
2006
2007 dbg("HID probe called for ifnum %d",
2008 intf->altsetting->desc.bInterfaceNumber);
2009
2010 if (!(hid = usb_hid_configure(intf)))
Stelian Pop479f6ea2005-06-22 17:53:28 +02002011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 hid_init_reports(hid);
2014 hid_dump_device(hid);
2015
2016 if (!hidinput_connect(hid))
2017 hid->claimed |= HID_CLAIMED_INPUT;
2018 if (!hiddev_connect(hid))
2019 hid->claimed |= HID_CLAIMED_HIDDEV;
2020
2021 usb_set_intfdata(intf, hid);
2022
2023 if (!hid->claimed) {
2024 printk ("HID device not claimed by input or hiddev\n");
2025 hid_disconnect(intf);
Stelian Pop479f6ea2005-06-22 17:53:28 +02002026 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
2029 printk(KERN_INFO);
2030
2031 if (hid->claimed & HID_CLAIMED_INPUT)
2032 printk("input");
2033 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
2034 printk(",");
2035 if (hid->claimed & HID_CLAIMED_HIDDEV)
2036 printk("hiddev%d", hid->minor);
2037
2038 c = "Device";
2039 for (i = 0; i < hid->maxcollection; i++) {
2040 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
2041 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
2042 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
2043 c = hid_types[hid->collection[i].usage & 0xffff];
2044 break;
2045 }
2046 }
2047
2048 usb_make_path(interface_to_usbdev(intf), path, 63);
2049
2050 printk(": USB HID v%x.%02x %s [%s] on %s\n",
2051 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
2052
2053 return 0;
2054}
2055
David Brownell27d72e82005-04-18 17:39:22 -07002056static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct hid_device *hid = usb_get_intfdata (intf);
2059
Alan Sternaef4e262006-01-31 12:58:38 -05002060 spin_lock_irq(&hid->inlock); /* Sync with error handler */
2061 set_bit(HID_SUSPENDED, &hid->iofl);
2062 spin_unlock_irq(&hid->inlock);
2063 del_timer(&hid->io_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 usb_kill_urb(hid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 dev_dbg(&intf->dev, "suspend\n");
2066 return 0;
2067}
2068
2069static int hid_resume(struct usb_interface *intf)
2070{
2071 struct hid_device *hid = usb_get_intfdata (intf);
2072 int status;
2073
Alan Sternaef4e262006-01-31 12:58:38 -05002074 clear_bit(HID_SUSPENDED, &hid->iofl);
Alan Sterndf9a1f42006-06-01 13:55:28 -04002075 hid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05002076 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 dev_dbg(&intf->dev, "resume status %d\n", status);
2078 return status;
2079}
2080
Alan Sterndf9a1f42006-06-01 13:55:28 -04002081/* Treat USB reset pretty much the same as suspend/resume */
2082static void hid_pre_reset(struct usb_interface *intf)
2083{
2084 /* FIXME: What if the interface is already suspended? */
2085 hid_suspend(intf, PMSG_ON);
2086}
2087
2088static void hid_post_reset(struct usb_interface *intf)
2089{
2090 struct usb_device *dev = interface_to_usbdev (intf);
2091
2092 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
2093 /* FIXME: Any more reinitialization needed? */
2094
2095 hid_resume(intf);
2096}
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098static struct usb_device_id hid_usb_ids [] = {
2099 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
2100 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
2101 { } /* Terminating entry */
2102};
2103
2104MODULE_DEVICE_TABLE (usb, hid_usb_ids);
2105
2106static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 .name = "usbhid",
2108 .probe = hid_probe,
2109 .disconnect = hid_disconnect,
2110 .suspend = hid_suspend,
2111 .resume = hid_resume,
Alan Sterndf9a1f42006-06-01 13:55:28 -04002112 .pre_reset = hid_pre_reset,
2113 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .id_table = hid_usb_ids,
2115};
2116
2117static int __init hid_init(void)
2118{
2119 int retval;
2120 retval = hiddev_init();
2121 if (retval)
2122 goto hiddev_init_fail;
2123 retval = usb_register(&hid_driver);
2124 if (retval)
2125 goto usb_register_fail;
2126 info(DRIVER_VERSION ":" DRIVER_DESC);
2127
2128 return 0;
2129usb_register_fail:
2130 hiddev_exit();
2131hiddev_init_fail:
2132 return retval;
2133}
2134
2135static void __exit hid_exit(void)
2136{
2137 usb_deregister(&hid_driver);
2138 hiddev_exit();
2139}
2140
2141module_init(hid_init);
2142module_exit(hid_exit);
2143
2144MODULE_AUTHOR(DRIVER_AUTHOR);
2145MODULE_DESCRIPTION(DRIVER_DESC);
2146MODULE_LICENSE(DRIVER_LICENSE);