2
* $Id: hid-core.c,v 1.1 2004-12-02 16:55:40 ron Exp $
4
* Copyright (c) 1999 Andreas Gal
5
* Copyright (c) 2000-2001 Vojtech Pavlik
7
* USB HID support for Linux
13
* This program is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
* Should you need to contact me, the author, you can do so either by
28
* e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29
* Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
32
#include <linux/autoconf.h>
33
#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
38
#include <linux/modversions.h>
41
#include <linux/module.h>
42
#include <linux/slab.h>
43
#include <linux/init.h>
44
#include <linux/kernel.h>
45
#include <linux/sched.h>
46
#include <linux/list.h>
48
#include <linux/smp_lock.h>
49
#include <linux/spinlock.h>
50
#include <asm/unaligned.h>
51
#include <linux/input.h>
56
#include <linux/usb.h>
59
#include <linux/hiddev.h>
65
#define DRIVER_VERSION "v1.8.1"
66
#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
67
#define DRIVER_DESC "USB HID support drivers"
69
static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
70
"Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
73
* Register a new report for a device.
76
static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
78
struct hid_report_enum *report_enum = device->report_enum + type;
79
struct hid_report *report;
81
if (report_enum->report_id_hash[id])
82
return report_enum->report_id_hash[id];
84
if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
86
memset(report, 0, sizeof(struct hid_report));
88
if (id != 0) report_enum->numbered = 1;
93
report->device = device;
94
report_enum->report_id_hash[id] = report;
96
list_add_tail(&report->list, &report_enum->report_list);
102
* Register a new field for this report.
105
static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
107
struct hid_field *field;
109
if (report->maxfield == HID_MAX_FIELDS) {
110
dbg("too many fields in report");
114
if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
115
+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
117
memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
118
+ values * sizeof(unsigned));
120
report->field[report->maxfield] = field;
121
field->usage = (struct hid_usage *)(field + 1);
122
field->value = (unsigned *)(field->usage + usages);
123
field->report = report;
124
field->index = report->maxfield++;
130
* Open a collection. The type/usage is pushed on the stack.
133
static int open_collection(struct hid_parser *parser, unsigned type)
135
struct hid_collection *collection;
138
usage = parser->local.usage[0];
140
if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
141
dbg("collection stack overflow");
145
if (parser->device->maxcollection == parser->device->collection_size) {
146
collection = kmalloc(sizeof(struct hid_collection) *
147
parser->device->collection_size * 2,
149
if (collection == NULL) {
150
dbg("failed to reallocate collection array");
153
memcpy(collection, parser->device->collection,
154
sizeof(struct hid_collection) *
155
parser->device->collection_size);
156
memset(collection + parser->device->collection_size, 0,
157
sizeof(struct hid_collection) *
158
parser->device->collection_size);
159
kfree(parser->device->collection);
160
parser->device->collection = collection;
161
parser->device->collection_size *= 2;
164
parser->collection_stack[parser->collection_stack_ptr++] =
165
parser->device->maxcollection;
167
collection = parser->device->collection +
168
parser->device->maxcollection++;
170
collection->type = type;
171
collection->usage = usage;
172
collection->level = parser->collection_stack_ptr - 1;
174
if (type == HID_COLLECTION_APPLICATION)
175
parser->device->maxapplication++;
181
* Close a collection.
184
static int close_collection(struct hid_parser *parser)
186
if (!parser->collection_stack_ptr) {
187
dbg("collection stack underflow");
190
parser->collection_stack_ptr--;
195
* Climb up the stack, search for the specified collection type
196
* and return the usage.
199
static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
202
for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
203
if (parser->device->collection[parser->collection_stack[n]].type == type)
204
return parser->device->collection[parser->collection_stack[n]].usage;
206
return 0; /* we know nothing about this usage type */
210
* Add a usage to the temporary parser table.
213
static int hid_add_usage(struct hid_parser *parser, unsigned usage)
215
if (parser->local.usage_index >= HID_MAX_USAGES) {
216
dbg("usage index exceeded");
219
parser->local.usage[parser->local.usage_index] = usage;
220
parser->local.collection_index[parser->local.usage_index] =
221
parser->collection_stack_ptr ?
222
parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
223
parser->local.usage_index++;
229
* Register a new field for this report.
232
static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
234
struct hid_report *report;
235
struct hid_field *field;
240
if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
241
dbg("hid_register_report failed");
245
if (parser->global.logical_maximum < parser->global.logical_minimum) {
246
dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
250
usages = parser->local.usage_index;
252
offset = report->size;
253
report->size += parser->global.report_size * parser->global.report_count;
255
if (usages < parser->global.report_count)
256
usages = parser->global.report_count;
259
return 0; /* ignore padding fields */
261
if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
264
field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
265
field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
266
field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
268
for (i = 0; i < usages; i++) {
270
/* Duplicate the last usage we parsed if we have excess values */
271
if (i >= parser->local.usage_index)
272
j = parser->local.usage_index - 1;
273
field->usage[i].hid = parser->local.usage[j];
274
field->usage[i].collection_index =
275
parser->local.collection_index[j];
278
field->maxusage = usages;
279
field->flags = flags;
280
field->report_offset = offset;
281
field->report_type = report_type;
282
field->report_size = parser->global.report_size;
283
field->report_count = parser->global.report_count;
284
field->logical_minimum = parser->global.logical_minimum;
285
field->logical_maximum = parser->global.logical_maximum;
286
field->physical_minimum = parser->global.physical_minimum;
287
field->physical_maximum = parser->global.physical_maximum;
288
field->unit_exponent = parser->global.unit_exponent;
289
field->unit = parser->global.unit;
295
* Read data value from item.
298
static __inline__ __u32 item_udata(struct hid_item *item)
300
switch (item->size) {
301
case 1: return item->data.u8;
302
case 2: return item->data.u16;
303
case 4: return item->data.u32;
308
static __inline__ __s32 item_sdata(struct hid_item *item)
310
switch (item->size) {
311
case 1: return item->data.s8;
312
case 2: return item->data.s16;
313
case 4: return item->data.s32;
319
* Process a global item.
322
static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
326
case HID_GLOBAL_ITEM_TAG_PUSH:
328
if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
329
dbg("global enviroment stack overflow");
333
memcpy(parser->global_stack + parser->global_stack_ptr++,
334
&parser->global, sizeof(struct hid_global));
337
case HID_GLOBAL_ITEM_TAG_POP:
339
if (!parser->global_stack_ptr) {
340
dbg("global enviroment stack underflow");
344
memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
345
sizeof(struct hid_global));
348
case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
349
parser->global.usage_page = item_udata(item);
352
case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
353
parser->global.logical_minimum = item_sdata(item);
356
case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
357
if (parser->global.logical_minimum < 0)
358
parser->global.logical_maximum = item_sdata(item);
360
parser->global.logical_maximum = item_udata(item);
363
case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
364
parser->global.physical_minimum = item_sdata(item);
367
case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
368
if (parser->global.physical_minimum < 0)
369
parser->global.physical_maximum = item_sdata(item);
371
parser->global.physical_maximum = item_udata(item);
374
case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
375
parser->global.unit_exponent = item_udata(item);
378
case HID_GLOBAL_ITEM_TAG_UNIT:
379
parser->global.unit = item_udata(item);
382
case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
383
if ((parser->global.report_size = item_udata(item)) > 32) {
384
dbg("invalid report_size %d", parser->global.report_size);
389
case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
390
if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
391
dbg("invalid report_count %d", parser->global.report_count);
396
case HID_GLOBAL_ITEM_TAG_REPORT_ID:
397
if ((parser->global.report_id = item_udata(item)) == 0) {
398
dbg("report_id 0 is invalid");
404
dbg("unknown global tag 0x%x", item->tag);
410
* Process a local item.
413
static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
418
if (item->size == 0) {
419
dbg("item data expected for local item");
423
data = item_udata(item);
427
case HID_LOCAL_ITEM_TAG_DELIMITER:
431
* We treat items before the first delimiter
432
* as global to all usage sets (branch 0).
433
* In the moment we process only these global
434
* items and the first delimiter set.
436
if (parser->local.delimiter_depth != 0) {
437
dbg("nested delimiters");
440
parser->local.delimiter_depth++;
441
parser->local.delimiter_branch++;
443
if (parser->local.delimiter_depth < 1) {
444
dbg("bogus close delimiter");
447
parser->local.delimiter_depth--;
451
case HID_LOCAL_ITEM_TAG_USAGE:
453
if (parser->local.delimiter_branch > 1) {
454
dbg("alternative usage ignored");
459
data = (parser->global.usage_page << 16) + data;
461
return hid_add_usage(parser, data);
463
case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
465
if (parser->local.delimiter_branch > 1) {
466
dbg("alternative usage ignored");
471
data = (parser->global.usage_page << 16) + data;
473
parser->local.usage_minimum = data;
476
case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
478
if (parser->local.delimiter_branch > 1) {
479
dbg("alternative usage ignored");
484
data = (parser->global.usage_page << 16) + data;
486
for (n = parser->local.usage_minimum; n <= data; n++)
487
if (hid_add_usage(parser, n)) {
488
dbg("hid_add_usage failed\n");
495
dbg("unknown local item tag 0x%x", item->tag);
502
* Process a main item.
505
static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
510
data = item_udata(item);
513
case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
514
ret = open_collection(parser, data & 0xff);
516
case HID_MAIN_ITEM_TAG_END_COLLECTION:
517
ret = close_collection(parser);
519
case HID_MAIN_ITEM_TAG_INPUT:
520
ret = hid_add_field(parser, HID_INPUT_REPORT, data);
522
case HID_MAIN_ITEM_TAG_OUTPUT:
523
ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
525
case HID_MAIN_ITEM_TAG_FEATURE:
526
ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
529
dbg("unknown main item tag 0x%x", item->tag);
533
memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
539
* Process a reserved item.
542
static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
544
dbg("reserved item type, tag 0x%x", item->tag);
549
* Free a report and all registered fields. The field->usage and
550
* field->value table's are allocated behind the field, so we need
551
* only to free(field) itself.
554
static void hid_free_report(struct hid_report *report)
558
for (n = 0; n < report->maxfield; n++)
559
kfree(report->field[n]);
566
* Free a device structure, all reports, and all fields.
569
static void hid_free_device(struct hid_device *device)
573
for (i = 0; i < HID_REPORT_TYPES; i++) {
574
struct hid_report_enum *report_enum = device->report_enum + i;
576
for (j = 0; j < 256; j++) {
577
struct hid_report *report = report_enum->report_id_hash[j];
578
if (report) hid_free_report(report);
582
if (device->rdesc) kfree(device->rdesc);
583
if (device->collection) kfree(device->collection);
587
* Fetch a report description item from the data stream. We support long
588
* items, though they are not used yet.
591
static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
593
if ((end - start) > 0) {
596
item->type = (b >> 2) & 3;
597
item->tag = (b >> 4) & 15;
599
if (item->tag == HID_ITEM_TAG_LONG) {
601
item->format = HID_ITEM_FORMAT_LONG;
603
if ((end - start) >= 2) {
605
item->size = *start++;
606
item->tag = *start++;
608
if ((end - start) >= item->size) {
609
item->data.longdata = start;
616
item->format = HID_ITEM_FORMAT_SHORT;
618
switch (item->size) {
624
if ((end - start) >= 1) {
625
item->data.u8 = *start++;
631
if ((end - start) >= 2) {
632
item->data.u16 = le16_to_cpu(get_unaligned((__u16*)start));
633
start = (__u8 *)((__u16 *)start + 1);
639
if ((end - start) >= 4) {
640
item->data.u32 = le32_to_cpu(get_unaligned((__u32*)start));
641
start = (__u8 *)((__u32 *)start + 1);
651
* Parse a report description into a hid_device structure. Reports are
652
* enumerated, fields are attached to these reports.
655
static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
657
struct hid_device *device;
658
struct hid_parser *parser;
659
struct hid_item item;
662
static int (*dispatch_type[])(struct hid_parser *parser,
663
struct hid_item *item) = {
670
if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
672
memset(device, 0, sizeof(struct hid_device));
674
if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
675
HID_DEFAULT_NUM_COLLECTIONS,
680
memset(device->collection, 0, sizeof(struct hid_collection) *
681
HID_DEFAULT_NUM_COLLECTIONS);
682
device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
684
for (i = 0; i < HID_REPORT_TYPES; i++)
685
INIT_LIST_HEAD(&device->report_enum[i].report_list);
687
if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
688
kfree(device->collection);
692
memcpy(device->rdesc, start, size);
693
device->rsize = size;
695
if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
696
kfree(device->rdesc);
697
kfree(device->collection);
701
memset(parser, 0, sizeof(struct hid_parser));
702
parser->device = device;
705
while ((start = fetch_item(start, end, &item)) != 0) {
706
if (item.format != HID_ITEM_FORMAT_SHORT) {
707
dbg("unexpected long global item");
708
hid_free_device(device);
712
if (dispatch_type[item.type](parser, &item)) {
713
dbg("item %u %u %u %u parsing failed\n",
714
item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
715
hid_free_device(device);
721
if (parser->collection_stack_ptr) {
722
dbg("unbalanced collection at end of report description");
723
hid_free_device(device);
727
if (parser->local.delimiter_depth) {
728
dbg("unbalanced delimiter at end of report description");
729
hid_free_device(device);
738
dbg("item fetching failed at offset %d\n", (int)(end - start));
739
hid_free_device(device);
745
* Convert a signed n-bit integer to signed 32-bit integer. Common
746
* cases are done through the compiler, the screwed things has to be
750
static __inline__ __s32 snto32(__u32 value, unsigned n)
753
case 8: return ((__s8)value);
754
case 16: return ((__s16)value);
755
case 32: return ((__s32)value);
757
return value & (1 << (n - 1)) ? value | (-1 << n) : value;
761
* Convert a signed 32-bit integer to a signed n-bit integer.
764
static __inline__ __u32 s32ton(__s32 value, unsigned n)
766
__s32 a = value >> (n - 1);
767
if (a && a != -1) return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
768
return value & ((1 << n) - 1);
772
* Extract/implement a data field from/to a report.
775
static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
777
report += (offset >> 5) << 2; offset &= 31;
778
return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
781
static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
783
report += (offset >> 5) << 2; offset &= 31;
784
put_unaligned((get_unaligned((__u64*)report)
785
& cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
786
| cpu_to_le64((__u64)value << offset), (__u64*)report);
790
* Search an array for a value.
793
static __inline__ int search(__s32 *array, __s32 value, unsigned n)
795
while (n--) if (*array++ == value) return 0;
799
static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
801
hid_dump_input(usage, value);
802
if (hid->claimed & HID_CLAIMED_INPUT)
803
hidinput_hid_event(hid, field, usage, value);
804
if (hid->claimed & HID_CLAIMED_HIDDEV)
805
hiddev_hid_event(hid, field, usage, value);
810
* Analyse a received field, and fetch the data from it. The field
811
* content is stored for next report processing (we do differential
812
* reporting to the layer).
815
static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data)
818
unsigned count = field->report_count;
819
unsigned offset = field->report_offset;
820
unsigned size = field->report_size;
821
__s32 min = field->logical_minimum;
822
__s32 max = field->logical_maximum;
823
__s32 value[count]; /* WARNING: gcc specific */
825
for (n = 0; n < count; n++) {
827
value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
828
extract(data, offset + n * size, size);
830
if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
831
&& value[n] >= min && value[n] <= max
832
&& field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
836
for (n = 0; n < count; n++) {
838
if (HID_MAIN_ITEM_VARIABLE & field->flags) {
840
if (field->flags & HID_MAIN_ITEM_RELATIVE) {
841
if (!value[n]) continue;
843
if (value[n] == field->value[n]) continue;
845
hid_process_event(hid, field, &field->usage[n], value[n]);
849
if (field->value[n] >= min && field->value[n] <= max
850
&& field->usage[field->value[n] - min].hid
851
&& search(value, field->value[n], count))
852
hid_process_event(hid, field, &field->usage[field->value[n] - min], 0);
854
if (value[n] >= min && value[n] <= max
855
&& field->usage[value[n] - min].hid
856
&& search(field->value, value[n], count))
857
hid_process_event(hid, field, &field->usage[value[n] - min], 1);
860
memcpy(field->value, value, count * sizeof(__s32));
863
static int hid_input_report(int type, u8 *data, int len, struct hid_device *hid)
865
struct hid_report_enum *report_enum = hid->report_enum + type;
866
struct hid_report *report;
875
printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
878
n = 0; /* Normally report number is 0 */
879
if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
884
if (!(report = report_enum->report_id_hash[n])) {
885
dbg("undefined report_id %d received", n);
887
printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
888
for (n = 0; n < len; n++)
889
printk(" %02x", data[n]);
896
if (hid->claimed & HID_CLAIMED_HIDDEV)
897
hiddev_report_event(hid, report);
899
size = ((report->size - 1) >> 3) + 1;
904
dbg("report %d is too short, (%d < %d)", report->id, len, size);
909
* Some low-speed devices have large reports and maxpacketsize 8.
910
* We buffer the data in that case and parse it when we got it all.
911
* Works only for unnumbered reports. Doesn't make sense for numbered
912
* reports anyway - then they don't need to be large.
916
if (!(report->data = kmalloc(size, GFP_ATOMIC))) {
917
dbg("couldn't allocate report buffer");
921
if (report->idx + len > size) {
922
dbg("report data buffer overflow");
927
memcpy(report->data + report->idx, data, len);
930
if (report->idx < size)
936
for (n = 0; n < report->maxfield; n++)
937
hid_input_field(hid, report->field[n], data);
944
* Interrupt input handler.
947
static void hid_irq(struct urb *urb)
950
dbg("nonzero status in irq %d", urb->status);
954
hid_input_report(HID_INPUT_REPORT, urb->transfer_buffer, urb->actual_length, urb->context);
958
* hid_read_report() reads in report values without waiting for an irq urb.
961
void hid_read_report(struct hid_device *hid, struct hid_report *report)
963
int len = ((report->size - 1) >> 3) + 1 + hid->report_enum[report->type].numbered;
967
if (hid->quirks & HID_QUIRK_NOGET)
970
if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, data, len)) != len) {
971
dbg("reading report type %d id %d failed len %d read %d", report->type + 1, report->id, len, read);
975
hid_input_report(report->type, data, len, hid);
979
* Output the field into the report.
982
static void hid_output_field(struct hid_field *field, __u8 *data)
984
unsigned count = field->report_count;
985
unsigned offset = field->report_offset;
986
unsigned size = field->report_size;
989
for (n = 0; n < count; n++) {
990
if (field->logical_minimum < 0) /* signed values */
991
implement(data, offset + n * size, size, s32ton(field->value[n], size));
992
else /* unsigned values */
993
implement(data, offset + n * size, size, field->value[n]);
1001
void hid_output_report(struct hid_report *report, __u8 *data)
1004
for (n = 0; n < report->maxfield; n++)
1005
hid_output_field(report->field[n], data);
1009
* Set a field value. The report this field belongs to has to be
1010
* created and transfered to the device, to set this value in the
1014
int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1016
unsigned size = field->report_size;
1018
hid_dump_input(field->usage + offset, value);
1020
if (offset >= field->report_count) {
1021
dbg("offset exceeds report_count");
1024
if (field->logical_minimum < 0) {
1025
if (value != snto32(s32ton(value, size), size)) {
1026
dbg("value %d is out of range", value);
1030
if ( (value > field->logical_maximum)
1031
|| (value < field->logical_minimum)) {
1032
dbg("value %d is invalid", value);
1035
field->value[offset] = value;
1039
int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1041
struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1042
struct list_head *list = report_enum->report_list.next;
1045
while (list != &report_enum->report_list) {
1046
struct hid_report *report = (struct hid_report *) list;
1048
for (i = 0; i < report->maxfield; i++) {
1049
*field = report->field[i];
1050
for (j = 0; j < (*field)->maxusage; j++)
1051
if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1058
static int hid_submit_out(struct hid_device *hid)
1060
hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.wLength);
1061
hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
1062
hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
1063
hid->urbout.dev = hid->dev;
1065
if (usb_submit_urb(&hid->urbout)) {
1066
err("usb_submit_urb(out) failed");
1073
static void hid_ctrl(struct urb *urb)
1075
struct hid_device *hid = urb->context;
1078
warn("ctrl urb status %d received", urb->status);
1080
hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1082
if (hid->outhead != hid->outtail)
1083
hid_submit_out(hid);
1086
void hid_write_report(struct hid_device *hid, struct hid_report *report)
1088
if (hid->report_enum[report->type].numbered) {
1089
hid->out[hid->outhead].buffer[0] = report->id;
1090
hid_output_report(report, hid->out[hid->outhead].buffer + 1);
1091
hid->out[hid->outhead].dr.wLength = cpu_to_le16(((report->size + 7) >> 3) + 1);
1093
hid_output_report(report, hid->out[hid->outhead].buffer);
1094
hid->out[hid->outhead].dr.wLength = cpu_to_le16((report->size + 7) >> 3);
1097
hid->out[hid->outhead].dr.wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1099
hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1101
if (hid->outhead == hid->outtail)
1102
hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1104
if (hid->urbout.status != -EINPROGRESS)
1105
hid_submit_out(hid);
1108
int hid_open(struct hid_device *hid)
1113
hid->urb.dev = hid->dev;
1115
if (usb_submit_urb(&hid->urb))
1121
void hid_close(struct hid_device *hid)
1124
usb_unlink_urb(&hid->urb);
1128
* Initialize all readable reports
1130
void hid_init_reports(struct hid_device *hid)
1133
struct hid_report *report;
1134
struct hid_report_enum *report_enum;
1135
struct list_head *list;
1137
for (i = 0; i < HID_REPORT_TYPES; i++) {
1138
if (i == HID_FEATURE_REPORT || i == HID_INPUT_REPORT) {
1139
report_enum = hid->report_enum + i;
1140
list = report_enum->report_list.next;
1141
while (list != &report_enum->report_list) {
1142
report = (struct hid_report *) list;
1143
hid_read_report(hid, report);
1144
usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
1151
#define USB_VENDOR_ID_WACOM 0x056a
1152
#define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
1153
#define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1154
#define USB_DEVICE_ID_WACOM_INTUOS 0x0020
1155
#define USB_DEVICE_ID_WACOM_PL 0x0030
1156
#define USB_DEVICE_ID_WACOM_INTUOS2 0x0040
1157
#define USB_DEVICE_ID_WACOM_VOLITO 0x0060
1158
#define USB_DEVICE_ID_WACOM_PTU 0x0003
1159
#define USB_DEVICE_ID_WACOM_INTUOS3 0x00B0
1161
#define USB_VENDOR_ID_KBGEAR 0x084e
1162
#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
1164
#define USB_VENDOR_ID_AIPTEK 0x08ca
1165
#define USB_DEVICE_ID_AIPTEK_01 0x0001
1166
#define USB_DEVICE_ID_AIPTEK_10 0x0010
1167
#define USB_DEVICE_ID_AIPTEK_20 0x0020
1168
#define USB_DEVICE_ID_AIPTEK_21 0x0021
1169
#define USB_DEVICE_ID_AIPTEK_22 0x0022
1170
#define USB_DEVICE_ID_AIPTEK_23 0x0023
1171
#define USB_DEVICE_ID_AIPTEK_24 0x0024
1173
#define USB_VENDOR_ID_ATEN 0x0557
1174
#define USB_DEVICE_ID_ATEN_UC100KM 0x2004
1175
#define USB_DEVICE_ID_ATEN_CS124U 0x2202
1176
#define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
1177
#define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
1179
#define USB_VENDOR_ID_TOPMAX 0x0663
1180
#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
1182
#define USB_VENDOR_ID_HAPP 0x078b
1183
#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
1184
#define USB_DEVICE_ID_UGCI_FLYING 0x0020
1185
#define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
1187
#define USB_VENDOR_ID_GRIFFIN 0x077d
1188
#define USB_DEVICE_ID_POWERMATE 0x0410 /* Griffin PowerMate */
1189
#define USB_DEVICE_ID_SOUNDKNOB 0x04AA /* Griffin SoundKnob */
1191
#define USB_VENDOR_ID_ONTRAK 0x0a07
1192
#define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
1194
#define USB_VENDOR_ID_TANGTOP 0x0d3d
1195
#define USB_DEVICE_ID_TANGTOP_USBPS2 0x0001
1197
#define USB_VENDOR_ID_OKI 0x070a
1198
#define USB_VENDOR_ID_OKI_MULITI 0x0007
1200
#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1201
#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
1203
#define USB_VENDOR_ID_MGE 0x0463
1204
#define USB_DEVICE_ID_MGE_UPS 0xffff
1205
#define USB_DEVICE_ID_MGE_UPS1 0x0001
1207
#define USB_VENDOR_ID_NEC 0x073e
1208
#define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
1210
struct hid_blacklist {
1214
} hid_blacklist[] = {
1215
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1216
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1217
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1218
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1219
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1220
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1221
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1222
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1223
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1224
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1225
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1226
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1227
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1228
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1229
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1230
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
1231
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1232
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1233
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1234
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1235
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1236
/* Intuos2 6x8 reports as 0x47 instead of 0x42 */
1237
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1238
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
1239
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1240
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1241
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
1242
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
1243
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
1244
{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
1245
{ USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1246
{ USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1247
{ USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1248
{ USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1249
{ USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1250
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1251
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1252
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1253
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1254
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1255
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1256
{ USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1257
{ USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1258
{ USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1259
{ USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1260
{ USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1261
{ USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1262
{ USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1263
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1264
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1265
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1266
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1267
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1268
{ USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1269
{ USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1270
{ USB_VENDOR_ID_OKI, USB_VENDOR_ID_OKI_MULITI, HID_QUIRK_NOGET },
1271
{ USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1272
{ USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1273
{ USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1274
{ USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1278
static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum)
1280
struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
1281
struct hid_descriptor *hdesc;
1282
struct hid_device *hid;
1283
unsigned quirks = 0, rsize = 0;
1287
for (n = 0; hid_blacklist[n].idVendor; n++)
1288
if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1289
(hid_blacklist[n].idProduct == dev->descriptor.idProduct))
1290
quirks = hid_blacklist[n].quirks;
1292
if (quirks & HID_QUIRK_IGNORE)
1295
if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc) && ((!interface->bNumEndpoints) ||
1296
usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc))) {
1297
dbg("class descriptor not present\n");
1301
for (n = 0; n < hdesc->bNumDescriptors; n++)
1302
if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
1303
rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1305
if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1306
dbg("weird size of report descriptor (%u)", rsize);
1313
if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
1314
dbg("reading report descriptor failed");
1319
printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1320
for (n = 0; n < rsize; n++)
1321
printk(" %02x", (unsigned) rdesc[n]);
1325
if (!(hid = hid_parse_report(rdesc, rsize))) {
1326
dbg("parsing report descriptor failed");
1331
hid->quirks = quirks;
1333
for (n = 0; n < interface->bNumEndpoints; n++) {
1335
struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
1338
if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
1341
if (!(endpoint->bEndpointAddress & 0x80)) /* Not an input endpoint */
1344
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1345
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1347
FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, endpoint->bInterval);
1352
if (n == interface->bNumEndpoints) {
1353
dbg("couldn't find an input interrupt endpoint");
1354
hid_free_device(hid);
1358
hid->version = hdesc->bcdHID;
1359
hid->country = hdesc->bCountryCode;
1361
hid->ifnum = interface->bInterfaceNumber;
1363
for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
1364
hid->out[n].dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1365
hid->out[n].dr.bRequest = USB_REQ_SET_REPORT;
1366
hid->out[n].dr.wIndex = cpu_to_le16(hid->ifnum);
1371
if (!(buf = kmalloc(63, GFP_KERNEL)))
1374
if (usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) {
1375
strcat(hid->name, buf);
1376
if (usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
1377
sprintf(hid->name, "%s %s", hid->name, buf);
1379
sprintf(hid->name, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
1383
FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
1384
(void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
1387
* Some devices don't like this and crash. I don't know of any devices
1388
* needing this, so it is disabled for now.
1392
if (interface->bInterfaceSubClass == 1)
1393
usb_set_protocol(dev, hid->ifnum, 1);
1399
static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
1400
const struct usb_device_id *id)
1402
struct hid_device *hid;
1406
dbg("HID probe called for ifnum %d", ifnum);
1408
if (!(hid = usb_hid_configure(dev, ifnum)))
1411
hid_init_reports(hid);
1412
hid_dump_device(hid);
1414
if (!hidinput_connect(hid))
1415
hid->claimed |= HID_CLAIMED_INPUT;
1416
if (!hiddev_connect(hid))
1417
hid->claimed |= HID_CLAIMED_HIDDEV;
1420
if (hid->claimed & HID_CLAIMED_INPUT)
1422
if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1424
if (hid->claimed & HID_CLAIMED_HIDDEV)
1425
printk("hiddev%d", hid->minor);
1428
for (i = 0; i < hid->maxcollection; i++) {
1429
if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1430
(hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1431
(hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1432
c = hid_types[hid->collection[i].usage & 0xffff];
1437
printk(": USB HID v%x.%02x %s [%s] on usb%d:%d.%d\n",
1438
hid->version >> 8, hid->version & 0xff, c, hid->name,
1439
dev->bus->busnum, dev->devnum, ifnum);
1444
static void hid_disconnect(struct usb_device *dev, void *ptr)
1446
struct hid_device *hid = ptr;
1448
dbg("cleanup called");
1449
usb_unlink_urb(&hid->urb);
1450
if (hid->claimed & HID_CLAIMED_INPUT)
1451
hidinput_disconnect(hid);
1452
if (hid->claimed & HID_CLAIMED_HIDDEV)
1453
hiddev_disconnect(hid);
1454
hid_free_device(hid);
1457
static struct usb_device_id hid_usb_ids [] = {
1458
{ match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
1459
bInterfaceClass: USB_INTERFACE_CLASS_HID },
1460
{ } /* Terminating entry */
1463
MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1465
static struct usb_driver hid_driver = {
1468
disconnect: hid_disconnect,
1469
id_table: hid_usb_ids,
1472
static int __init hid_init(void)
1475
usb_register(&hid_driver);
1476
info(DRIVER_VERSION " " DRIVER_AUTHOR);
1482
static void __exit hid_exit(void)
1484
usb_deregister(&hid_driver);
1488
module_init(hid_init);
1489
module_exit(hid_exit);
1491
MODULE_AUTHOR( DRIVER_AUTHOR );
1492
MODULE_DESCRIPTION( DRIVER_DESC );
1493
MODULE_LICENSE("GPL");