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

« back to all changes in this revision

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