~ubuntu-branches/debian/lenny/wacom-tools/lenny

« back to all changes in this revision

Viewing changes to linuxwacom/src/2.6.9/hid-core.c

  • Committer: Bazaar Package Importer
  • Author(s): Ron Lee
  • Date: 2004-12-10 16:12:07 UTC
  • Revision ID: james.westby@ubuntu.com-20041210161207-cw8urijtjsqvk2e3
Tags: upstream-0.6.6
ImportĀ upstreamĀ versionĀ 0.6.6

Show diffs side-by-side

added added

removed removed

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