Subversion Repositories shark

Rev

Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 1
/*
2
 *  USB HID support for Linux
3
 *
4
 *  Copyright (c) 1999 Andreas Gal
5
 *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
6
 */
7
 
8
/*
9
 * This program is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License as published by the Free
11
 * Software Foundation; either version 2 of the License, or (at your option)
12
 * any later version.
13
 */
14
 
15
#include <linuxcomp.h> 
16
 
17
#include <linux/module.h>
18
#include <linux/slab.h>
19
#include <linux/init.h>
20
#include <linux/kernel.h>
21
#include <linux/sched.h>
22
#include <linux/list.h>
23
#include <linux/mm.h>
24
#include <linux/smp_lock.h>
25
#include <linux/spinlock.h>
26
#include <asm/unaligned.h>
27
#include <asm/byteorder.h>
28
#include <linux/input.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
 
42
#define DRIVER_VERSION "v2.0"
43
#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
44
#define DRIVER_DESC "USB HID core driver"
45
#define DRIVER_LICENSE "GPL"
46
 
47
static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48
                                "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
49
 
50
/*
51
 * Register a new report for a device.
52
 */
53
 
54
static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
55
{
56
        struct hid_report_enum *report_enum = device->report_enum + type;
57
        struct hid_report *report;
58
 
59
        if (report_enum->report_id_hash[id])
60
                return report_enum->report_id_hash[id];
61
 
62
        if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
63
                return NULL;
64
        memset(report, 0, sizeof(struct hid_report));
65
 
66
        if (id != 0)
67
                report_enum->numbered = 1;
68
 
69
        report->id = id;
70
        report->type = type;
71
        report->size = 0;
72
        report->device = device;
73
        report_enum->report_id_hash[id] = report;
74
 
75
        list_add_tail(&report->list, &report_enum->report_list);
76
 
77
        return report;
78
}
79
 
80
/*
81
 * Register a new field for this report.
82
 */
83
 
84
static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
85
{
86
        struct hid_field *field;
87
 
88
        if (report->maxfield == HID_MAX_FIELDS) {
89
                dbg("too many fields in report");
90
                return NULL;
91
        }
92
 
93
        if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
94
                + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
95
 
96
        memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
97
                + values * sizeof(unsigned));
98
 
99
        report->field[report->maxfield++] = field;
100
        field->usage = (struct hid_usage *)(field + 1);
101
        field->value = (unsigned *)(field->usage + usages);
102
        field->report = report;
103
 
104
        return field;
105
}
106
 
107
/*
108
 * Open a collection. The type/usage is pushed on the stack.
109
 */
110
 
111
static int open_collection(struct hid_parser *parser, unsigned type)
112
{
113
        struct hid_collection *collection;
114
        unsigned usage;
115
 
116
        usage = parser->local.usage[0];
117
 
118
        if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
119
                dbg("collection stack overflow");
120
                return -1;
121
        }
122
 
123
        if (parser->device->maxcollection == parser->device->collection_size) {
124
                collection = kmalloc(sizeof(struct hid_collection) *
125
                                     parser->device->collection_size * 2,
126
                                     GFP_KERNEL);
127
                if (collection == NULL) {
128
                        dbg("failed to reallocate collection array");
129
                        return -1;
130
                }
131
                memcpy(collection, parser->device->collection,
132
                       sizeof(struct hid_collection) *
133
                       parser->device->collection_size);
134
                memset(collection + parser->device->collection_size, 0,
135
                       sizeof(struct hid_collection) *
136
                       parser->device->collection_size);
137
                kfree(parser->device->collection);
138
                parser->device->collection = collection;
139
                parser->device->collection_size *= 2;
140
        }
141
 
142
        parser->collection_stack[parser->collection_stack_ptr++] =
143
                parser->device->maxcollection;
144
 
145
        collection = parser->device->collection +
146
                parser->device->maxcollection++;
147
        collection->type = type;
148
        collection->usage = usage;
149
        collection->level = parser->collection_stack_ptr - 1;
150
 
151
        if (type == HID_COLLECTION_APPLICATION)
152
                parser->device->maxapplication++;
153
 
154
        return 0;
155
}
156
 
157
/*
158
 * Close a collection.
159
 */
160
 
161
static int close_collection(struct hid_parser *parser)
162
{
163
        if (!parser->collection_stack_ptr) {
164
                dbg("collection stack underflow");
165
                return -1;
166
        }
167
        parser->collection_stack_ptr--;
168
        return 0;
169
}
170
 
171
/*
172
 * Climb up the stack, search for the specified collection type
173
 * and return the usage.
174
 */
175
 
176
static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
177
{
178
        int n;
179
        for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
180
                if (parser->device->collection[parser->collection_stack[n]].type == type)
181
                        return parser->device->collection[parser->collection_stack[n]].usage;
182
        return 0; /* we know nothing about this usage type */
183
}
184
 
185
/*
186
 * Add a usage to the temporary parser table.
187
 */
188
 
189
static int hid_add_usage(struct hid_parser *parser, unsigned usage)
190
{
191
        if (parser->local.usage_index >= HID_MAX_USAGES) {
192
                dbg("usage index exceeded");
193
                return -1;
194
        }
195
        parser->local.usage[parser->local.usage_index] = usage;
196
        parser->local.collection_index[parser->local.usage_index] =
197
                parser->collection_stack_ptr ?
198
                parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
199
        parser->local.usage_index++;
200
        return 0;
201
}
202
 
203
/*
204
 * Register a new field for this report.
205
 */
206
 
207
static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
208
{
209
        struct hid_report *report;
210
        struct hid_field *field;
211
        int usages;
212
        unsigned offset;
213
        int i;
214
 
215
        if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
216
                dbg("hid_register_report failed");
217
                return -1;
218
        }
219
 
220
        if (parser->global.logical_maximum < parser->global.logical_minimum) {
221
                dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
222
                return -1;
223
        }
224
        usages = parser->local.usage_index;
225
 
226
        offset = report->size;
227
        report->size += parser->global.report_size * parser->global.report_count;
228
 
229
        if (usages == 0)
230
                return 0; /* ignore padding fields */
231
 
232
        if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
233
                return 0;
234
 
235
        field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
236
        field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
237
        field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
238
 
239
        for (i = 0; i < usages; i++) {
240
                field->usage[i].hid = parser->local.usage[i];
241
                field->usage[i].collection_index =
242
                        parser->local.collection_index[i];
243
        }
244
 
245
        field->maxusage = usages;
246
        field->flags = flags;
247
        field->report_offset = offset;
248
        field->report_type = report_type;
249
        field->report_size = parser->global.report_size;
250
        field->report_count = parser->global.report_count;
251
        field->logical_minimum = parser->global.logical_minimum;
252
        field->logical_maximum = parser->global.logical_maximum;
253
        field->physical_minimum = parser->global.physical_minimum;
254
        field->physical_maximum = parser->global.physical_maximum;
255
        field->unit_exponent = parser->global.unit_exponent;
256
        field->unit = parser->global.unit;
257
 
258
        return 0;
259
}
260
 
261
/*
262
 * Read data value from item.
263
 */
264
 
265
static __inline__ __u32 item_udata(struct hid_item *item)
266
{
267
        switch (item->size) {
268
                case 1: return item->data.u8;
269
                case 2: return item->data.u16;
270
                case 4: return item->data.u32;
271
        }
272
        return 0;
273
}
274
 
275
static __inline__ __s32 item_sdata(struct hid_item *item)
276
{
277
        switch (item->size) {
278
                case 1: return item->data.s8;
279
                case 2: return item->data.s16;
280
                case 4: return item->data.s32;
281
        }
282
        return 0;
283
}
284
 
285
/*
286
 * Process a global item.
287
 */
288
 
289
static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
290
{
291
        switch (item->tag) {
292
 
293
                case HID_GLOBAL_ITEM_TAG_PUSH:
294
 
295
                        if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
296
                                dbg("global enviroment stack overflow");
297
                                return -1;
298
                        }
299
 
300
                        memcpy(parser->global_stack + parser->global_stack_ptr++,
301
                                &parser->global, sizeof(struct hid_global));
302
                        return 0;
303
 
304
                case HID_GLOBAL_ITEM_TAG_POP:
305
 
306
                        if (!parser->global_stack_ptr) {
307
                                dbg("global enviroment stack underflow");
308
                                return -1;
309
                        }
310
 
311
                        memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
312
                                sizeof(struct hid_global));
313
                        return 0;
314
 
315
                case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
316
                        parser->global.usage_page = item_udata(item);
317
                        return 0;
318
 
319
                case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
320
                        parser->global.logical_minimum = item_sdata(item);
321
                        return 0;
322
 
323
                case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
324
                        if (parser->global.logical_minimum < 0)
325
                                parser->global.logical_maximum = item_sdata(item);
326
                        else
327
                                parser->global.logical_maximum = item_udata(item);
328
                        return 0;
329
 
330
                case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
331
                        parser->global.physical_minimum = item_sdata(item);
332
                        return 0;
333
 
334
                case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
335
                        if (parser->global.physical_minimum < 0)
336
                                parser->global.physical_maximum = item_sdata(item);
337
                        else
338
                                parser->global.physical_maximum = item_udata(item);
339
                        return 0;
340
 
341
                case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
342
                        parser->global.unit_exponent = item_sdata(item);
343
                        return 0;
344
 
345
                case HID_GLOBAL_ITEM_TAG_UNIT:
346
                        parser->global.unit = item_udata(item);
347
                        return 0;
348
 
349
                case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
350
                        if ((parser->global.report_size = item_udata(item)) > 32) {
351
                                dbg("invalid report_size %d", parser->global.report_size);
352
                                return -1;
353
                        }
354
                        return 0;
355
 
356
                case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
357
                        if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
358
                                dbg("invalid report_count %d", parser->global.report_count);
359
                                return -1;
360
                        }
361
                        return 0;
362
 
363
                case HID_GLOBAL_ITEM_TAG_REPORT_ID:
364
                        if ((parser->global.report_id = item_udata(item)) == 0) {
365
                                dbg("report_id 0 is invalid");
366
                                return -1;
367
                        }
368
                        return 0;
369
 
370
                default:
371
                        dbg("unknown global tag 0x%x", item->tag);
372
                        return -1;
373
        }
374
}
375
 
376
/*
377
 * Process a local item.
378
 */
379
 
380
static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
381
{
382
        __u32 data;
383
        unsigned n;
384
 
385
        if (item->size == 0) {
386
                dbg("item data expected for local item");
387
                return -1;
388
        }
389
 
390
        data = item_udata(item);
391
 
392
        switch (item->tag) {
393
 
394
                case HID_LOCAL_ITEM_TAG_DELIMITER:
395
 
396
                        if (data) {
397
                                /*
398
                                 * We treat items before the first delimiter
399
                                 * as global to all usage sets (branch 0).
400
                                 * In the moment we process only these global
401
                                 * items and the first delimiter set.
402
                                 */
403
                                if (parser->local.delimiter_depth != 0) {
404
                                        dbg("nested delimiters");
405
                                        return -1;
406
                                }
407
                                parser->local.delimiter_depth++;
408
                                parser->local.delimiter_branch++;
409
                        } else {
410
                                if (parser->local.delimiter_depth < 1) {
411
                                        dbg("bogus close delimiter");
412
                                        return -1;
413
                                }
414
                                parser->local.delimiter_depth--;
415
                        }
416
                        return 1;
417
 
418
                case HID_LOCAL_ITEM_TAG_USAGE:
419
 
420
                        if (parser->local.delimiter_branch > 1) {
421
                                dbg("alternative usage ignored");
422
                                return 0;
423
                        }
424
 
425
                        if (item->size <= 2)
426
                                data = (parser->global.usage_page << 16) + data;
427
 
428
                        return hid_add_usage(parser, data);
429
 
430
                case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
431
 
432
                        if (parser->local.delimiter_branch > 1) {
433
                                dbg("alternative usage ignored");
434
                                return 0;
435
                        }
436
 
437
                        if (item->size <= 2)
438
                                data = (parser->global.usage_page << 16) + data;
439
 
440
                        parser->local.usage_minimum = data;
441
                        return 0;
442
 
443
                case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
444
 
445
                        if (parser->local.delimiter_branch > 1) {
446
                                dbg("alternative usage ignored");
447
                                return 0;
448
                        }
449
 
450
                        if (item->size <= 2)
451
                                data = (parser->global.usage_page << 16) + data;
452
 
453
                        for (n = parser->local.usage_minimum; n <= data; n++)
454
                                if (hid_add_usage(parser, n)) {
455
                                        dbg("hid_add_usage failed\n");
456
                                        return -1;
457
                                }
458
                        return 0;
459
 
460
                default:
461
 
462
                        dbg("unknown local item tag 0x%x", item->tag);
463
                        return 0;
464
        }
465
        return 0;
466
}
467
 
468
/*
469
 * Process a main item.
470
 */
471
 
472
static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
473
{
474
        __u32 data;
475
        int ret;
476
 
477
        data = item_udata(item);
478
 
479
        switch (item->tag) {
480
                case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
481
                        ret = open_collection(parser, data & 0xff);
482
                        break;
483
                case HID_MAIN_ITEM_TAG_END_COLLECTION:
484
                        ret = close_collection(parser);
485
                        break;
486
                case HID_MAIN_ITEM_TAG_INPUT:
487
                        ret = hid_add_field(parser, HID_INPUT_REPORT, data);
488
                        break;
489
                case HID_MAIN_ITEM_TAG_OUTPUT:
490
                        ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
491
                        break;
492
                case HID_MAIN_ITEM_TAG_FEATURE:
493
                        ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
494
                        break;
495
                default:
496
                        dbg("unknown main item tag 0x%x", item->tag);
497
                        ret = 0;
498
        }
499
 
500
        memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
501
 
502
        return ret;
503
}
504
 
505
/*
506
 * Process a reserved item.
507
 */
508
 
509
static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
510
{
511
        dbg("reserved item type, tag 0x%x", item->tag);
512
        return 0;
513
}
514
 
515
/*
516
 * Free a report and all registered fields. The field->usage and
517
 * field->value table's are allocated behind the field, so we need
518
 * only to free(field) itself.
519
 */
520
 
521
static void hid_free_report(struct hid_report *report)
522
{
523
        unsigned n;
524
 
525
        for (n = 0; n < report->maxfield; n++)
526
                kfree(report->field[n]);
527
        kfree(report);
528
}
529
 
530
/*
531
 * Free a device structure, all reports, and all fields.
532
 */
533
 
534
static void hid_free_device(struct hid_device *device)
535
{
536
        unsigned i,j;
537
 
538
        hid_ff_exit(device);
539
 
540
        for (i = 0; i < HID_REPORT_TYPES; i++) {
541
                struct hid_report_enum *report_enum = device->report_enum + i;
542
 
543
                for (j = 0; j < 256; j++) {
544
                        struct hid_report *report = report_enum->report_id_hash[j];
545
                        if (report)
546
                                hid_free_report(report);
547
                }
548
        }
549
 
550
        if (device->rdesc)
551
                kfree(device->rdesc);
552
        kfree(device);
553
}
554
 
555
/*
556
 * Fetch a report description item from the data stream. We support long
557
 * items, though they are not used yet.
558
 */
559
 
560
static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
561
{
562
        u8 b;
563
 
564
        if ((end - start) <= 0)
565
                return NULL;
566
 
567
        b = *start++;
568
 
569
        item->type = (b >> 2) & 3;
570
        item->tag  = (b >> 4) & 15;
571
 
572
        if (item->tag == HID_ITEM_TAG_LONG) {
573
 
574
                item->format = HID_ITEM_FORMAT_LONG;
575
 
576
                if ((end - start) < 2)
577
                        return NULL;
578
 
579
                item->size = *start++;
580
                item->tag  = *start++;
581
 
582
                if ((end - start) < item->size)
583
                        return NULL;
584
 
585
                item->data.longdata = start;
586
                start += item->size;
587
                return start;
588
        }
589
 
590
        item->format = HID_ITEM_FORMAT_SHORT;
591
        item->size = b & 3;
592
 
593
        switch (item->size) {
594
 
595
                case 0:
596
                        return start;
597
 
598
                case 1:
599
                        if ((end - start) < 1)
600
                                return NULL;
601
                        item->data.u8 = *start++;
602
                        return start;
603
 
604
                case 2:
605
                        if ((end - start) < 2)
606
                                return NULL;
1056 tullio 607
 
608
                        // changed for gcc4 compatibility
609
                        //item->data.u16 = le16_to_cpu(get_unaligned(((__u16*)start)++));
610
                        item->data.u16 = le16_to_cpu(get_unaligned(((__u16*)start)));
611
                        start += sizeof(u16);
846 giacomo 612
                        return start;
613
 
614
                case 3:
615
                        item->size++;
616
                        if ((end - start) < 4)
617
                                return NULL;
1056 tullio 618
 
619
                        // changed for gcc4 compatibility
620
                        //item->data.u32 = le32_to_cpu(get_unaligned(((__u32*)start)++));
621
                        item->data.u32 = le32_to_cpu(get_unaligned(((__u32*)start)));
622
                        start += sizeof(u32);
846 giacomo 623
                        return start;
624
        }
625
 
626
        return NULL;
627
}
628
 
629
/*
630
 * Parse a report description into a hid_device structure. Reports are
631
 * enumerated, fields are attached to these reports.
632
 */
633
 
634
static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
635
{
636
        struct hid_device *device;
637
        struct hid_parser *parser;
638
        struct hid_item item;
639
        __u8 *end;
640
        unsigned i;
641
        static int (*dispatch_type[])(struct hid_parser *parser,
642
                                      struct hid_item *item) = {
643
                hid_parser_main,
644
                hid_parser_global,
645
                hid_parser_local,
646
                hid_parser_reserved
647
        };
648
 
649
        if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
650
                return NULL;
651
        memset(device, 0, sizeof(struct hid_device));
652
 
653
        if (!(device->collection =kmalloc(sizeof(struct hid_collection) *
654
                                   HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
655
                kfree(device);
656
                return NULL;
657
        }
658
        memset(device->collection, 0, sizeof(struct hid_collection) *
659
               HID_DEFAULT_NUM_COLLECTIONS);
660
        device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
661
 
662
        for (i = 0; i < HID_REPORT_TYPES; i++)
663
                INIT_LIST_HEAD(&device->report_enum[i].report_list);
664
 
665
        if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
666
                kfree(device->collection);
667
                kfree(device);
668
                return NULL;
669
        }
670
        memcpy(device->rdesc, start, size);
671
        device->rsize = size;
672
 
673
        if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
674
                kfree(device->rdesc);
675
                kfree(device->collection);
676
                kfree(device);
677
                return NULL;
678
        }
679
        memset(parser, 0, sizeof(struct hid_parser));
680
        parser->device = device;
681
 
682
        end = start + size;
683
        while ((start = fetch_item(start, end, &item)) != 0) {
684
 
685
                if (item.format != HID_ITEM_FORMAT_SHORT) {
686
                        dbg("unexpected long global item");
687
                        kfree(device->collection);
688
                        hid_free_device(device);
689
                        kfree(parser);
690
                        return NULL;
691
                }
692
 
693
                if (dispatch_type[item.type](parser, &item)) {
694
                        dbg("item %u %u %u %u parsing failed\n",
695
                                item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
696
                        kfree(device->collection);
697
                        hid_free_device(device);
698
                        kfree(parser);
699
                        return NULL;
700
                }
701
 
702
                if (start == end) {
703
                        if (parser->collection_stack_ptr) {
704
                                dbg("unbalanced collection at end of report description");
705
                                kfree(device->collection);
706
                                hid_free_device(device);
707
                                kfree(parser);
708
                                return NULL;
709
                        }
710
                        if (parser->local.delimiter_depth) {
711
                                dbg("unbalanced delimiter at end of report description");
712
                                kfree(device->collection);
713
                                hid_free_device(device);
714
                                kfree(parser);
715
                                return NULL;
716
                        }
717
                        kfree(parser);
718
                        return device;
719
                }
720
        }
721
 
722
        dbg("item fetching failed at offset %d\n", (int)(end - start));
723
        kfree(device->collection);
724
        hid_free_device(device);
725
        kfree(parser);
726
        return NULL;
727
}
728
 
729
/*
730
 * Convert a signed n-bit integer to signed 32-bit integer. Common
731
 * cases are done through the compiler, the screwed things has to be
732
 * done by hand.
733
 */
734
 
735
static __inline__ __s32 snto32(__u32 value, unsigned n)
736
{
737
        switch (n) {
738
                case 8:  return ((__s8)value);
739
                case 16: return ((__s16)value);
740
                case 32: return ((__s32)value);
741
        }
742
        return value & (1 << (n - 1)) ? value | (-1 << n) : value;
743
}
744
 
745
/*
746
 * Convert a signed 32-bit integer to a signed n-bit integer.
747
 */
748
 
749
static __inline__ __u32 s32ton(__s32 value, unsigned n)
750
{
751
        __s32 a = value >> (n - 1);
752
        if (a && a != -1)
753
                return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
754
        return value & ((1 << n) - 1);
755
}
756
 
757
/*
758
 * Extract/implement a data field from/to a report.
759
 */
760
 
761
static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
762
{
763
        report += (offset >> 5) << 2; offset &= 31;
764
        return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
765
}
766
 
767
static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
768
{
769
        report += (offset >> 5) << 2; offset &= 31;
770
        put_unaligned((get_unaligned((__u64*)report)
771
                & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
772
                | cpu_to_le64((__u64)value << offset), (__u64*)report);
773
}
774
 
775
/*
776
 * Search an array for a value.
777
 */
778
 
779
static __inline__ int search(__s32 *array, __s32 value, unsigned n)
780
{
781
        while (n--) {
782
                if (*array++ == value)
783
                        return 0;
784
        }
785
        return -1;
786
}
787
 
788
static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs)
789
{
790
        hid_dump_input(usage, value);
791
        if (hid->claimed & HID_CLAIMED_INPUT)
792
                hidinput_hid_event(hid, field, usage, value, regs);
793
        if (hid->claimed & HID_CLAIMED_HIDDEV)
794
                hiddev_hid_event(hid, field, usage, value, regs);
795
}
796
 
797
/*
798
 * Analyse a received field, and fetch the data from it. The field
799
 * content is stored for next report processing (we do differential
800
 * reporting to the layer).
801
 */
802
 
803
static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, struct pt_regs *regs)
804
{
805
        unsigned n;
806
        unsigned count = field->report_count;
807
        unsigned offset = field->report_offset;
808
        unsigned size = field->report_size;
809
        __s32 min = field->logical_minimum;
810
        __s32 max = field->logical_maximum;
811
        __s32 value[count]; /* WARNING: gcc specific */
812
 
813
        for (n = 0; n < count; n++) {
814
 
815
                        value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
816
                                                    extract(data, offset + n * size, size);
817
 
818
                        if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
819
                            && value[n] >= min && value[n] <= max
820
                            && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
821
                                return;
822
        }
823
 
824
        for (n = 0; n < count; n++) {
825
 
826
                if (HID_MAIN_ITEM_VARIABLE & field->flags) {
827
 
828
                        if (field->flags & HID_MAIN_ITEM_RELATIVE) {
829
                                if (!value[n])
830
                                        continue;
831
                        } else {
832
                                if (value[n] == field->value[n])
833
                                        continue;
834
                        }      
835
                        hid_process_event(hid, field, &field->usage[n], value[n], regs);
836
                        continue;
837
                }
838
 
839
                if (field->value[n] >= min && field->value[n] <= max
840
                        && field->usage[field->value[n] - min].hid
841
                        && search(value, field->value[n], count))
842
                                hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, regs);
843
 
844
                if (value[n] >= min && value[n] <= max
845
                        && field->usage[value[n] - min].hid
846
                        && search(field->value, value[n], count))
847
                                hid_process_event(hid, field, &field->usage[value[n] - min], 1, regs);
848
        }
849
 
850
        memcpy(field->value, value, count * sizeof(__s32));
851
}
852
 
853
static int hid_input_report(int type, struct urb *urb, struct pt_regs *regs)
854
{
855
        struct hid_device *hid = urb->context;
856
        struct hid_report_enum *report_enum = hid->report_enum + type;
857
        u8 *data = urb->transfer_buffer;
858
        int len = urb->actual_length;
859
        struct hid_report *report;
860
        int n, size;
861
 
862
        if (!len) {
863
                dbg("empty report");
864
                return -1;
865
        }
866
 
867
#ifdef DEBUG_DATA
868
        printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
869
#endif
870
 
871
        n = 0;                          /* Normally report number is 0 */
872
        if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
873
                n = *data++;
874
                len--;
875
        }
876
 
877
#ifdef DEBUG_DATA
878
        {
879
                int i;
880
                printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, len);
881
                for (i = 0; i < len; i++)
882
                        printk(" %02x", data[i]);
883
                printk("\n");
884
        }
885
#endif
886
 
887
        if (!(report = report_enum->report_id_hash[n])) {
888
                dbg("undefined report_id %d received", n);
889
                return -1;
890
        }
891
 
892
        size = ((report->size - 1) >> 3) + 1;
893
 
894
        if (len < size) {
895
                dbg("report %d is too short, (%d < %d)", report->id, len, size);
896
                return -1;
897
        }
898
 
899
        if (hid->claimed & HID_CLAIMED_HIDDEV)
900
                hiddev_report_event(hid, report);
901
 
902
        for (n = 0; n < report->maxfield; n++)
903
                hid_input_field(hid, report->field[n], data, regs);
904
 
905
        if (hid->claimed & HID_CLAIMED_INPUT)
906
                hidinput_report_event(hid, report);
907
 
908
        return 0;
909
}
910
 
911
/*
912
 * Input interrupt completion handler.
913
 */
914
 
915
static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
916
{
917
        struct hid_device       *hid = urb->context;
918
        int                     status;
919
 
920
        switch (urb->status) {
921
        case 0:                 /* success */
922
                hid_input_report(HID_INPUT_REPORT, urb, regs);
923
                break;
924
        case -ECONNRESET:       /* unlink */
925
        case -ENOENT:
926
        case -ESHUTDOWN:
927
                return;
928
        default:                /* error */
929
                dbg("nonzero status in input irq %d", urb->status);
930
        }
931
 
932
        status = usb_submit_urb (urb, SLAB_ATOMIC);
933
        if (status)
934
                err ("can't resubmit intr, %s-%s/input%d, status %d",
935
                                hid->dev->bus->bus_name, hid->dev->devpath,
936
                                hid->ifnum, status);
937
}
938
 
939
/*
940
 * Output the field into the report.
941
 */
942
 
943
static void hid_output_field(struct hid_field *field, __u8 *data)
944
{
945
        unsigned count = field->report_count;
946
        unsigned offset = field->report_offset;
947
        unsigned size = field->report_size;
948
        unsigned n;
949
 
950
        for (n = 0; n < count; n++) {
951
                if (field->logical_minimum < 0) /* signed values */
952
                        implement(data, offset + n * size, size, s32ton(field->value[n], size));
953
                 else                           /* unsigned values */
954
                        implement(data, offset + n * size, size, field->value[n]);
955
        }
956
}
957
 
958
/*
959
 * Create a report.
960
 */
961
 
962
void hid_output_report(struct hid_report *report, __u8 *data)
963
{
964
        unsigned n;
965
 
966
        if (report->id > 0)
967
                *data++ = report->id;
968
 
969
        for (n = 0; n < report->maxfield; n++)
970
                hid_output_field(report->field[n], data);
971
}
972
 
973
/*
974
 * Set a field value. The report this field belongs to has to be
975
 * created and transferred to the device, to set this value in the
976
 * device.
977
 */
978
 
979
int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
980
{
981
        unsigned size = field->report_size;
982
 
983
        hid_dump_input(field->usage + offset, value);
984
 
985
        if (offset >= field->report_count) {
986
                dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
987
                hid_dump_field(field, 8);
988
                return -1;
989
        }
990
        if (field->logical_minimum < 0) {
991
                if (value != snto32(s32ton(value, size), size)) {
992
                        dbg("value %d is out of range", value);
993
                        return -1;
994
                }
995
        }
996
        field->value[offset] = value;
997
        return 0;
998
}
999
 
1000
int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1001
{
1002
        struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1003
        struct list_head *list = report_enum->report_list.next;
1004
        int i, j;
1005
 
1006
        while (list != &report_enum->report_list) {
1007
                struct hid_report *report = (struct hid_report *) list;
1008
                list = list->next;
1009
                for (i = 0; i < report->maxfield; i++) {
1010
                        *field = report->field[i];
1011
                        for (j = 0; j < (*field)->maxusage; j++)
1012
                                if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1013
                                        return j;
1014
                }
1015
        }
1016
        return -1;
1017
}
1018
 
1019
/*
1020
 * Find a report with a specified HID usage.
1021
 */
1022
 
1023
int hid_find_report_by_usage(struct hid_device *hid, __u32 wanted_usage, struct hid_report **report, int type)
1024
{
1025
        struct hid_report_enum *report_enum = hid->report_enum + type;
1026
        struct list_head *list = report_enum->report_list.next;
1027
        int i, j;
1028
 
1029
        while (list != &report_enum->report_list) {
1030
                *report = (struct hid_report *) list;
1031
                list = list->next;
1032
                for (i = 0; i < (*report)->maxfield; i++) {
1033
                        struct hid_field *field = (*report)->field[i];
1034
                        for (j = 0; j < field->maxusage; j++)
1035
                                if (field->logical == wanted_usage)
1036
                                        return j;
1037
                }
1038
        }
1039
        return -1;
1040
}
1041
 
1042
int hid_find_field_in_report(struct hid_report *report, __u32 wanted_usage, struct hid_field **field)
1043
{
1044
        int i, j;
1045
 
1046
        for (i = 0; i < report->maxfield; i++) {
1047
                *field = report->field[i];
1048
                for (j = 0; j < (*field)->maxusage; j++)
1049
                        if ((*field)->usage[j].hid == wanted_usage)
1050
                                return j;
1051
        }
1052
 
1053
        return -1;
1054
}
1055
 
1056
static int hid_submit_out(struct hid_device *hid)
1057
{
1058
        struct hid_report *report;
1059
 
1060
        report = hid->out[hid->outtail];
1061
 
1062
        hid_output_report(report, hid->outbuf);
1063
        hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1064
        hid->urbout->dev = hid->dev;
1065
 
1066
        dbg("submitting out urb");
1067
 
1068
        if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) {
1069
                err("usb_submit_urb(out) failed");
1070
                return -1;
1071
        }
1072
 
1073
        return 0;
1074
}
1075
 
1076
static int hid_submit_ctrl(struct hid_device *hid)
1077
{
1078
        struct hid_report *report;
1079
        unsigned char dir;
1080
 
1081
        report = hid->ctrl[hid->ctrltail].report;
1082
        dir = hid->ctrl[hid->ctrltail].dir;
1083
 
1084
        if (dir == USB_DIR_OUT)
1085
                hid_output_report(report, hid->ctrlbuf);
1086
 
1087
        hid->urbctrl->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1088
        hid->urbctrl->pipe = (dir == USB_DIR_OUT) ?  usb_sndctrlpipe(hid->dev, 0) : usb_rcvctrlpipe(hid->dev, 0);
1089
        hid->urbctrl->dev = hid->dev;
1090
 
1091
        hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
1092
        hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
1093
        hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1094
        hid->cr->wIndex = cpu_to_le16(hid->ifnum);
1095
        hid->cr->wLength = cpu_to_le16(hid->urbctrl->transfer_buffer_length);
1096
 
1097
        dbg("submitting ctrl urb");
1098
 
1099
        if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) {
1100
                err("usb_submit_urb(ctrl) failed");
1101
                return -1;
1102
        }
1103
 
1104
        return 0;
1105
}
1106
 
1107
/*
1108
 * Output interrupt completion handler.
1109
 */
1110
 
1111
static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1112
{
1113
        struct hid_device *hid = urb->context;
1114
        unsigned long flags;
1115
 
1116
        if (urb->status)
1117
                warn("output irq status %d received", urb->status);
1118
 
1119
        spin_lock_irqsave(&hid->outlock, flags);
1120
 
1121
        hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1122
 
1123
        if (hid->outhead != hid->outtail) {
1124
                hid_submit_out(hid);
1125
                spin_unlock_irqrestore(&hid->outlock, flags);
1126
                return;
1127
        }
1128
 
1129
        clear_bit(HID_OUT_RUNNING, &hid->iofl);
1130
 
1131
        spin_unlock_irqrestore(&hid->outlock, flags);
1132
 
1133
        wake_up(&hid->wait);
1134
}
1135
 
1136
/*
1137
 * Control pipe completion handler.
1138
 */
1139
 
1140
static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1141
{
1142
        struct hid_device *hid = urb->context;
1143
        unsigned long flags;
1144
 
1145
        if (urb->status)
1146
                warn("ctrl urb status %d received", urb->status);
1147
 
1148
        spin_lock_irqsave(&hid->ctrllock, flags);
1149
 
1150
        if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN)
1151
                hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, regs);
1152
 
1153
        hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1154
 
1155
        if (hid->ctrlhead != hid->ctrltail) {
1156
                hid_submit_ctrl(hid);
1157
                spin_unlock_irqrestore(&hid->ctrllock, flags);
1158
                return;
1159
        }
1160
 
1161
        clear_bit(HID_CTRL_RUNNING, &hid->iofl);
1162
 
1163
        spin_unlock_irqrestore(&hid->ctrllock, flags);
1164
 
1165
        wake_up(&hid->wait);
1166
}
1167
 
1168
void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
1169
{
1170
        int head;
1171
        unsigned long flags;
1172
 
1173
        if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
1174
                return;
1175
 
1176
        if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1177
 
1178
                spin_lock_irqsave(&hid->outlock, flags);
1179
 
1180
                if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) {
1181
                        spin_unlock_irqrestore(&hid->outlock, flags);
1182
                        warn("output queue full");
1183
                        return;
1184
                }
1185
 
1186
                hid->out[hid->outhead] = report;
1187
                hid->outhead = head;
1188
 
1189
                if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl))
1190
                        hid_submit_out(hid);
1191
 
1192
                spin_unlock_irqrestore(&hid->outlock, flags);
1193
                return;
1194
        }
1195
 
1196
        spin_lock_irqsave(&hid->ctrllock, flags);
1197
 
1198
        if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) {
1199
                spin_unlock_irqrestore(&hid->ctrllock, flags);
1200
                warn("control queue full");
1201
                return;
1202
        }
1203
 
1204
        hid->ctrl[hid->ctrlhead].report = report;
1205
        hid->ctrl[hid->ctrlhead].dir = dir;
1206
        hid->ctrlhead = head;
1207
 
1208
        if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl))
1209
                hid_submit_ctrl(hid);
1210
 
1211
        spin_unlock_irqrestore(&hid->ctrllock, flags);
1212
}
1213
 
1214
int hid_wait_io(struct hid_device *hid)
1215
{
1216
        DECLARE_WAITQUEUE(wait, current);
1217
  int timeout = 1*HZ;
1218
 
1219
        set_current_state(TASK_UNINTERRUPTIBLE);
1220
        add_wait_queue(&hid->wait, &wait);
1221
 
1222
  while (timeout && (test_bit(HID_CTRL_RUNNING, &hid->iofl) ||
1223
                           test_bit(HID_OUT_RUNNING, &hid->iofl)))
1224
    timeout = schedule_timeout(timeout);
1225
 
1226
        set_current_state(TASK_RUNNING);
1227
        remove_wait_queue(&hid->wait, &wait);
1228
 
1229
/*  if (!timeout) {
1230
                dbg("timeout waiting for ctrl or out queue to clear");
1231
                return -1;
1232
  } */
1233
        return 0;
1234
}
1235
 
1236
static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1237
                unsigned char type, void *buf, int size)
1238
{
1239
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1240
                USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
1241
                (type << 8), ifnum, buf, size, HZ * USB_CTRL_GET_TIMEOUT);
1242
}
1243
 
1244
int hid_open(struct hid_device *hid)
1245
{
1246
        if (hid->open++)
1247
                return 0;
1248
 
1249
        hid->urbin->dev = hid->dev;
1250
 
1251
        if (usb_submit_urb(hid->urbin, GFP_KERNEL))
1252
                return -EIO;
1253
 
1254
        return 0;
1255
}
1256
 
1257
void hid_close(struct hid_device *hid)
1258
{
1259
        if (!--hid->open)
1260
                usb_unlink_urb(hid->urbin);
1261
}
1262
 
1263
/*
1264
 * Initialize all reports
1265
 */
1266
 
1267
void hid_init_reports(struct hid_device *hid)
1268
{
1269
        struct hid_report_enum *report_enum;
1270
        struct hid_report *report;
1271
        struct list_head *list;
1272
        int len;
1273
        int err, ret;
1274
 
1275
        report_enum = hid->report_enum + HID_INPUT_REPORT;
1276
        list = report_enum->report_list.next;
1277
        while (list != &report_enum->report_list) {
1278
                report = (struct hid_report *) list;
1279
                hid_submit_report(hid, report, USB_DIR_IN);
1280
                list = list->next;
1281
        }
1282
 
1283
        report_enum = hid->report_enum + HID_FEATURE_REPORT;
1284
        list = report_enum->report_list.next;
1285
        while (list != &report_enum->report_list) {
1286
                report = (struct hid_report *) list;
1287
                hid_submit_report(hid, report, USB_DIR_IN);
1288
                list = list->next;
1289
        }
1290
 
1291
        err = 0;
1292
  while ((ret = hid_wait_io(hid))) {
1293
                err |= ret;
1294
                if (test_bit(HID_CTRL_RUNNING, &hid->iofl))
1295
                        usb_unlink_urb(hid->urbctrl);
1296
                if (test_bit(HID_OUT_RUNNING, &hid->iofl))
1297
                        usb_unlink_urb(hid->urbout);
1298
        }
1299
 
1300
        if (err)
1301
                warn("timeout initializing reports\n");
1302
 
1303
        report_enum = hid->report_enum + HID_INPUT_REPORT;
1304
        list = report_enum->report_list.next;
1305
        while (list != &report_enum->report_list) {
1306
                report = (struct hid_report *) list;
1307
                len = ((report->size - 1) >> 3) + 1 + report_enum->numbered;
1308
                if (len > hid->urbin->transfer_buffer_length)
1309
                        hid->urbin->transfer_buffer_length = len < HID_BUFFER_SIZE ? len : HID_BUFFER_SIZE;
1310
                usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0),
1311
                        0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE, report->id,
1312
                        hid->ifnum, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1313
                list = list->next;
1314
        }
1315
}
1316
 
1317
#define USB_VENDOR_ID_WACOM             0x056a
1318
#define USB_DEVICE_ID_WACOM_PENPARTNER  0x0000
1319
#define USB_DEVICE_ID_WACOM_GRAPHIRE    0x0010
1320
#define USB_DEVICE_ID_WACOM_INTUOS      0x0020
1321
#define USB_DEVICE_ID_WACOM_PL          0x0030
1322
#define USB_DEVICE_ID_WACOM_INTUOS2     0x0040
1323
 
1324
#define USB_VENDOR_ID_KBGEAR            0x084e
1325
#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO  0x1001
1326
 
1327
 
1328
#define USB_VENDOR_ID_AIPTEK            0x08ca
1329
#define USB_DEVICE_ID_AIPTEK_6000       0x0020
1330
 
1331
#define USB_VENDOR_ID_GRIFFIN           0x077d
1332
#define USB_DEVICE_ID_POWERMATE         0x0410
1333
#define USB_DEVICE_ID_SOUNDKNOB         0x04AA
1334
 
1335
#define USB_VENDOR_ID_ATEN             0x0557  
1336
#define USB_DEVICE_ID_ATEN_UC100KM     0x2004
1337
#define USB_DEVICE_ID_ATEN_CS124U      0x2202
1338
#define USB_DEVICE_ID_ATEN_2PORTKVM    0x2204
1339
#define USB_DEVICE_ID_ATEN_4PORTKVM    0x2205
1340
#define USB_DEVICE_ID_ATEN_4PORTKVMC   0x2208
1341
 
1342
#define USB_VENDOR_ID_TOPMAX           0x0663
1343
#define USB_DEVICE_ID_TOPMAX_COBRAPAD  0x0103
1344
 
1345
#define USB_VENDOR_ID_HAPP             0x078b
1346
#define USB_DEVICE_ID_UGCI_DRIVING     0x0010
1347
#define USB_DEVICE_ID_UGCI_FLYING      0x0020
1348
#define USB_DEVICE_ID_UGCI_FIGHTING    0x0030
1349
 
1350
#define USB_VENDOR_ID_MGE              0x0463
1351
#define USB_DEVICE_ID_MGE_UPS          0xffff
1352
#define USB_DEVICE_ID_MGE_UPS1         0x0001
1353
 
1354
#define USB_VENDOR_ID_ONTRAK            0x0a07
1355
#define USB_DEVICE_ID_ONTRAK_ADU100     0x0064
1356
 
1357
#define USB_VENDOR_ID_TANGTOP          0x0d3d
1358
#define USB_DEVICE_ID_TANGTOP_USBPS2   0x0001
1359
 
1360
#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1361
#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5      0x0100
1362
 
1363
#define USB_VENDOR_ID_A4TECH            0x09DA
1364
#define USB_DEVICE_ID_A4TECH_WCP32PU    0x0006
1365
 
1366
struct hid_blacklist {
1367
        __u16 idVendor;
1368
        __u16 idProduct;
1369
        unsigned quirks;
1370
} hid_blacklist[] = {
1371
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1372
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1373
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1374
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1375
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1376
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1377
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1378
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1379
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1380
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1381
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1382
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1383
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1384
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1385
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1386
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
1387
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1388
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1389
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1390
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1391
        { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1392
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_6000, HID_QUIRK_IGNORE },
1393
        { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1394
        { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1395
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1396
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1397
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1398
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1399
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1400
        { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_HIDDEV },
1401
        { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_HIDDEV },
1402
        { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1403
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1404
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1405
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1406
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1407
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1408
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1409
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1410
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1411
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1412
        { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1413
        { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1414
        { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK },
1415
        { 0, 0 }
1416
};
1417
 
1418
static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
1419
{
1420
        if (!(hid->inbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->inbuf_dma)))
1421
                return -1;
1422
        if (!(hid->outbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->outbuf_dma)))
1423
                return -1;
1424
        if (!(hid->cr = usb_buffer_alloc(dev, sizeof(*(hid->cr)), SLAB_ATOMIC, &hid->cr_dma)))
1425
                return -1;
1426
        if (!(hid->ctrlbuf = usb_buffer_alloc(dev, HID_BUFFER_SIZE, SLAB_ATOMIC, &hid->ctrlbuf_dma)))
1427
                return -1;
1428
 
1429
        return 0;
1430
}
1431
 
1432
static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1433
{
1434
        if (hid->inbuf)
1435
                usb_buffer_free(dev, HID_BUFFER_SIZE, hid->inbuf, hid->inbuf_dma);
1436
        if (hid->outbuf)
1437
                usb_buffer_free(dev, HID_BUFFER_SIZE, hid->outbuf, hid->outbuf_dma);
1438
        if (hid->cr)
1439
                usb_buffer_free(dev, sizeof(*(hid->cr)), hid->cr, hid->cr_dma);
1440
        if (hid->ctrlbuf)
1441
                usb_buffer_free(dev, HID_BUFFER_SIZE, hid->ctrlbuf, hid->ctrlbuf_dma);
1442
}
1443
 
1444
static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1445
{
1446
        struct usb_host_interface *interface = intf->altsetting + intf->act_altsetting;
1447
        struct usb_device *dev = interface_to_usbdev (intf);
1448
        struct hid_descriptor *hdesc;
1449
        struct hid_device *hid;
1450
        unsigned quirks = 0, rsize = 0;
1451
        char *buf, *rdesc;
1452
        int n;
1453
 
1454
        for (n = 0; hid_blacklist[n].idVendor; n++)
1455
                if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1456
                        (hid_blacklist[n].idProduct == dev->descriptor.idProduct))
1457
                                quirks = hid_blacklist[n].quirks;
1458
 
1459
        if (quirks & HID_QUIRK_IGNORE)
1460
                return NULL;
1461
 
1462
        if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && ((!interface->desc.bNumEndpoints) ||
1463
                usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
1464
                        dbg("class descriptor not present\n");
1465
                        return NULL;
1466
        }
1467
 
1468
        for (n = 0; n < hdesc->bNumDescriptors; n++)
1469
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1470
                        rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1471
 
1472
        if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1473
                dbg("weird size of report descriptor (%u)", rsize);
1474
                return NULL;
1475
        }
1476
 
1477
        if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1478
                dbg("couldn't allocate rdesc memory");
1479
                return NULL;
1480
        }
1481
 
1482
        if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
1483
                dbg("reading report descriptor failed");
1484
                kfree(rdesc);
1485
                return NULL;
1486
        }
1487
 
1488
#ifdef DEBUG_DATA
1489
        printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1490
        for (n = 0; n < rsize; n++)
1491
                printk(" %02x", (unsigned char) rdesc[n]);
1492
        printk("\n");
1493
#endif
1494
 
1495
        if (!(hid = hid_parse_report(rdesc, rsize))) {
1496
                dbg("parsing report descriptor failed");
1497
                kfree(rdesc);
1498
                return NULL;
1499
        }
1500
 
1501
        kfree(rdesc);
1502
        hid->quirks = quirks;
1503
 
1504
        if (hid_alloc_buffers(dev, hid)) {
1505
                hid_free_buffers(dev, hid);
1506
                goto fail;
1507
        }
1508
 
1509
        for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1510
 
1511
                struct usb_endpoint_descriptor *endpoint;
1512
                int pipe;
1513
 
1514
                endpoint = &interface->endpoint[n].desc;
1515
                if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
1516
                        continue;
1517
 
1518
                if (endpoint->bEndpointAddress & USB_DIR_IN) {
1519
                        if (hid->urbin)
1520
                                continue;
1521
                        if (!(hid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1522
                                goto fail;
1523
                        pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1524
                        usb_fill_int_urb(hid->urbin, dev, pipe, hid->inbuf, 0,
1525
                                         hid_irq_in, hid, endpoint->bInterval);
1526
                        hid->urbin->transfer_dma = hid->inbuf_dma;
1527
                        hid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1528
                } else {
1529
                        if (hid->urbout)
1530
                                continue;
1531
                        if (!(hid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1532
                                goto fail;
1533
                        pipe = usb_sndbulkpipe(dev, endpoint->bEndpointAddress);
1534
                        usb_fill_bulk_urb(hid->urbout, dev, pipe, hid->outbuf, 0,
1535
                                          hid_irq_out, hid);
1536
                        hid->urbout->transfer_dma = hid->outbuf_dma;
1537
                        hid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1538
                }
1539
        }
1540
 
1541
        if (!hid->urbin) {
1542
                err("couldn't find an input interrupt endpoint");
1543
                goto fail;
1544
        }
1545
 
1546
        init_waitqueue_head(&hid->wait);
1547
 
1548
        hid->outlock = SPIN_LOCK_UNLOCKED;
1549
        hid->ctrllock = SPIN_LOCK_UNLOCKED;
1550
 
1551
        hid->version = le16_to_cpu(hdesc->bcdHID);
1552
        hid->country = hdesc->bCountryCode;
1553
        hid->dev = dev;
1554
        hid->ifnum = interface->desc.bInterfaceNumber;
1555
 
1556
        hid->name[0] = 0;
1557
 
1558
        if (!(buf = kmalloc(64, GFP_KERNEL)))
1559
                goto fail;
1560
 
1561
        if (usb_string(dev, dev->descriptor.iManufacturer, buf, 64) > 0) {
1562
                strcat(hid->name, buf);
1563
                if (usb_string(dev, dev->descriptor.iProduct, buf, 64) > 0)
1564
                        snprintf26(hid->name, 64, "%s %s", hid->name, buf);
1565
        } else if (usb_string(dev, dev->descriptor.iProduct, buf, 128) > 0) {
1566
                        snprintf26(hid->name, 128, "%s", buf);
1567
        } else
1568
                snprintf26(hid->name, 128, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
1569
 
1570
        usb_make_path(dev, buf, 64);
1571
        snprintf26(hid->phys, 64, "%s/input%d", buf,
1572
                        intf->altsetting[0].desc.bInterfaceNumber);
1573
 
1574
        if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1575
                hid->uniq[0] = 0;
1576
 
1577
        kfree(buf);
1578
 
1579
        hid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1580
        if (!hid->urbctrl)
1581
                goto fail;
1582
        usb_fill_control_urb(hid->urbctrl, dev, 0, (void *) hid->cr,
1583
                             hid->ctrlbuf, 1, hid_ctrl, hid);
1584
        hid->urbctrl->setup_dma = hid->cr_dma;
1585
        hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
1586
        hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1587
                                | URB_NO_SETUP_DMA_MAP);
1588
 
1589
        return hid;
1590
 
1591
fail:
1592
 
1593
        if (hid->urbin)
1594
                usb_free_urb(hid->urbin);
1595
        if (hid->urbout)
1596
                usb_free_urb(hid->urbout);
1597
        if (hid->urbctrl)
1598
                usb_free_urb(hid->urbctrl);
1599
        hid_free_buffers(dev, hid);
1600
        hid_free_device(hid);
1601
 
1602
        return NULL;
1603
}
1604
 
1605
static void hid_disconnect(struct usb_interface *intf)
1606
{
1607
        struct hid_device *hid = usb_get_intfdata (intf);
1608
 
1609
        if (!hid)
1610
                return;
1611
 
1612
        usb_set_intfdata(intf, NULL);
1613
        usb_unlink_urb(hid->urbin);
1614
        usb_unlink_urb(hid->urbout);
1615
        usb_unlink_urb(hid->urbctrl);
1616
 
1617
        if (hid->claimed & HID_CLAIMED_INPUT)
1618
                hidinput_disconnect(hid);
1619
        if (hid->claimed & HID_CLAIMED_HIDDEV)
1620
                hiddev_disconnect(hid);
1621
 
1622
        usb_free_urb(hid->urbin);
1623
        usb_free_urb(hid->urbctrl);
1624
        if (hid->urbout)
1625
                usb_free_urb(hid->urbout);
1626
 
1627
        hid_free_buffers(hid->dev, hid);
1628
        hid_free_device(hid);
1629
}
1630
 
1631
static int hid_probe (struct usb_interface *intf, const struct usb_device_id *id)
1632
{
1633
        struct hid_device *hid;
1634
        char path[64];
1635
        int i;
1636
        char *c;
1637
 
1638
        dbg("HID probe called for ifnum %d",
1639
                        intf->altsetting->desc.bInterfaceNumber);
1640
 
1641
        if (!(hid = usb_hid_configure(intf)))
1642
                return -EIO;
1643
 
1644
  hid_init_reports(hid);
1645
        hid_dump_device(hid);
1646
 
1647
        if (!hidinput_connect(hid))
1648
                hid->claimed |= HID_CLAIMED_INPUT;
1649
        if (!hiddev_connect(hid))
1650
                hid->claimed |= HID_CLAIMED_HIDDEV;
1651
 
1652
        usb_set_intfdata(intf, hid);
1653
 
1654
        if (!hid->claimed) {
1655
                printk ("HID device not claimed by input or hiddev\n");
1656
                hid_disconnect(intf);
1657
                return -EIO;
1658
        }
1659
 
1660
        printk(KERN_INFO);
1661
 
1662
        if (hid->claimed & HID_CLAIMED_INPUT)
1663
                printk("input");
1664
        if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1665
                printk(",");
1666
        if (hid->claimed & HID_CLAIMED_HIDDEV)
1667
                printk("hiddev%d", hid->minor);
1668
 
1669
        c = "Device";
1670
        for (i = 0; i < hid->maxcollection; i++) {
1671
                if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1672
                    (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1673
                    (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1674
                        c = hid_types[hid->collection[i].usage & 0xffff];
1675
                        break;
1676
                }
1677
        }
1678
 
1679
        usb_make_path(interface_to_usbdev(intf), path, 63);
1680
 
1681
        printk(": USB HID v%x.%02x %s [%s] on %s\n",
1682
                hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1683
 
1684
        return 0;
1685
}
1686
 
1687
static struct usb_device_id hid_usb_ids [] = {
1688
        { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1689
            .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1690
        { }                                             /* Terminating entry */
1691
};
1692
 
1693
MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1694
 
1695
static struct usb_driver hid_driver = {
1696
        .owner =        THIS_MODULE,
1697
        .name =         "hid",
1698
        .probe =        hid_probe,
1699
        .disconnect =   hid_disconnect,
1700
        .id_table =     hid_usb_ids,
1701
};
1702
 
1703
/*static*/ int __init hid_init(void)
1704
{
1705
        int retval;
1706
        retval = hiddev_init();
1707
        if (retval)
1708
                goto hiddev_init_fail;
1709
        retval = usb_register(&hid_driver);
1710
        if (retval)
1711
                goto usb_register_fail;
1712
        info(DRIVER_VERSION ":" DRIVER_DESC);
1713
 
1714
        return 0;
1715
usb_register_fail:
1716
        hiddev_exit();
1717
hiddev_init_fail:
1718
        return retval;
1719
}
1720
 
1721
/*static*/ void __exit hid_exit(void)
1722
{
1723
        hiddev_exit();
1724
        usb_deregister(&hid_driver);
1725
}
1726
 
1727
module_init(hid_init);
1728
module_exit(hid_exit);
1729
 
1730
MODULE_AUTHOR(DRIVER_AUTHOR);
1731
MODULE_DESCRIPTION(DRIVER_DESC);
1732
MODULE_LICENSE(DRIVER_LICENSE);