~ubuntu-branches/ubuntu/maverick/wacom-tools/maverick

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Timo Aaltonen
  • Date: 2009-03-24 10:06:54 UTC
  • mfrom: (1.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090324100654-u7x4rd2ked2gpksz
Tags: 1:0.8.2.2-0ubuntu1
* New upstream release. (LP: #355340)
* Patch 100_allow_eraser.patch dropped, included in this version.
* 100_fedora-fix-build.diff
  - Make the driver to build against xserver 1.6.
* 101_fedora-fix-mapping.diff
  - No button 0 in the server, all offsets have to begin at 1.
* 102_fedora-wcmmaxx.diff
  - Don't assign priv->wcmMaxX/Y back into common->wcmMaxX/Y.
* 103_fedora-hal-setup.diff
  - Add a hal callout program to set up the device.
  (LP: #215689, #356091)
* 104_revert-check.diff
  - Don't check the serial number, it breaks some devices.
* Modify 10-wacom.fdi to include changes from Fedora. Should allow
  hotplugging (built-in) serial tablets. (LP: #337112)
* rules:
  - Run autoreconf on build
  - xserver-xorg-input-wacom includes /usr/lib/hal/hal-setup-wacom
* control:
  - autoreconf: add automake, libtool to build-deps.
  - hal-setup-wacom: add libhal-dev to build-deps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: hid-core.c,v 1.1 2004-12-02 16:55:40 ron Exp $
3
 
 *
4
 
 *  Copyright (c) 1999 Andreas Gal
5
 
 *  Copyright (c) 2000-2001 Vojtech Pavlik
6
 
 *
7
 
 *  USB HID support for Linux
8
 
 *
9
 
 *  Sponsored by SuSE
10
 
 */
11
 
 
12
 
/*
13
 
 * This program is free software; you can redistribute it and/or modify
14
 
 * it under the terms of the GNU General Public License as published by
15
 
 * the Free Software Foundation; either version 2 of the License, or
16
 
 * (at your option) any later version.
17
 
 *
18
 
 * This program is distributed in the hope that it will be useful,
19
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
 * GNU General Public License for more details.
22
 
 *
23
 
 * You should have received a copy of the GNU General Public License
24
 
 * along with this program; if not, write to the Free Software
25
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
 
 *
27
 
 * Should you need to contact me, the author, you can do so either by
28
 
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29
 
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
30
 
 */
31
 
 
32
 
#include <linux/autoconf.h>
33
 
#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
34
 
#   define MODVERSIONS
35
 
#endif
36
 
 
37
 
#ifdef MODVERSIONS
38
 
#include <linux/modversions.h>
39
 
#endif
40
 
 
41
 
#include <linux/module.h>
42
 
#include <linux/slab.h>
43
 
#include <linux/init.h>
44
 
#include <linux/kernel.h>
45
 
#include <linux/sched.h>
46
 
#include <linux/list.h>
47
 
#include <linux/mm.h>
48
 
#include <linux/smp_lock.h>
49
 
#include <linux/spinlock.h>
50
 
#include <asm/unaligned.h>
51
 
#include <linux/input.h>
52
 
 
53
 
#undef DEBUG
54
 
#undef DEBUG_DATA
55
 
 
56
 
#include <linux/usb.h>
57
 
 
58
 
#include "hid.h"
59
 
#include <linux/hiddev.h>
60
 
 
61
 
/*
62
 
 * Version Information
63
 
 */
64
 
 
65
 
#define DRIVER_VERSION "v1.8.1"
66
 
#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
67
 
#define DRIVER_DESC "USB HID support drivers"
68
 
 
69
 
static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
70
 
                                "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
71
 
 
72
 
/*
73
 
 * Register a new report for a device.
74
 
 */
75
 
 
76
 
static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
77
 
{
78
 
        struct hid_report_enum *report_enum = device->report_enum + type;
79
 
        struct hid_report *report;
80
 
 
81
 
        if (report_enum->report_id_hash[id])
82
 
                return report_enum->report_id_hash[id];
83
 
 
84
 
        if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
85
 
                return NULL;
86
 
        memset(report, 0, sizeof(struct hid_report));
87
 
 
88
 
        if (id != 0) report_enum->numbered = 1;
89
 
 
90
 
        report->id = id;
91
 
        report->type = type;
92
 
        report->size = 0;
93
 
        report->device = device;
94
 
        report_enum->report_id_hash[id] = report;
95
 
 
96
 
        list_add_tail(&report->list, &report_enum->report_list);
97
 
 
98
 
        return report;
99
 
}
100
 
 
101
 
/*
102
 
 * Register a new field for this report.
103
 
 */
104
 
 
105
 
static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
106
 
{
107
 
        struct hid_field *field;
108
 
 
109
 
        if (report->maxfield == HID_MAX_FIELDS) {
110
 
                dbg("too many fields in report");
111
 
                return NULL;
112
 
        }
113
 
 
114
 
        if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
115
 
                + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
116
 
 
117
 
        memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
118
 
                + values * sizeof(unsigned));
119
 
 
120
 
        report->field[report->maxfield] = field;
121
 
        field->usage = (struct hid_usage *)(field + 1);
122
 
        field->value = (unsigned *)(field->usage + usages);
123
 
        field->report = report;
124
 
        field->index = report->maxfield++;
125
 
 
126
 
        return field;
127
 
}
128
 
 
129
 
/*
130
 
 * Open a collection. The type/usage is pushed on the stack.
131
 
 */
132
 
 
133
 
static int open_collection(struct hid_parser *parser, unsigned type)
134
 
{
135
 
        struct hid_collection *collection;
136
 
        unsigned usage;
137
 
 
138
 
        usage = parser->local.usage[0];
139
 
 
140
 
        if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
141
 
                dbg("collection stack overflow");
142
 
                return -1;
143
 
        }
144
 
 
145
 
        if (parser->device->maxcollection == parser->device->collection_size) {
146
 
                collection = kmalloc(sizeof(struct hid_collection) *
147
 
                                     parser->device->collection_size * 2,
148
 
                                     GFP_KERNEL);
149
 
                if (collection == NULL) {
150
 
                        dbg("failed to reallocate collection array");
151
 
                        return -1;
152
 
                }
153
 
                memcpy(collection, parser->device->collection,
154
 
                       sizeof(struct hid_collection) *
155
 
                       parser->device->collection_size);
156
 
                memset(collection + parser->device->collection_size, 0,
157
 
                       sizeof(struct hid_collection) *
158
 
                       parser->device->collection_size);
159
 
                kfree(parser->device->collection);
160
 
                parser->device->collection = collection;
161
 
                parser->device->collection_size *= 2;
162
 
        }
163
 
 
164
 
        parser->collection_stack[parser->collection_stack_ptr++] =
165
 
                parser->device->maxcollection;
166
 
 
167
 
        collection = parser->device->collection + 
168
 
                parser->device->maxcollection++;
169
 
 
170
 
        collection->type = type;
171
 
        collection->usage = usage;
172
 
        collection->level = parser->collection_stack_ptr - 1;
173
 
 
174
 
        if (type == HID_COLLECTION_APPLICATION)
175
 
                parser->device->maxapplication++;
176
 
 
177
 
        return 0;
178
 
}
179
 
 
180
 
/*
181
 
 * Close a collection.
182
 
 */
183
 
 
184
 
static int close_collection(struct hid_parser *parser)
185
 
{
186
 
        if (!parser->collection_stack_ptr) {
187
 
                dbg("collection stack underflow");
188
 
                return -1;
189
 
        }
190
 
        parser->collection_stack_ptr--;
191
 
        return 0;
192
 
}
193
 
 
194
 
/*
195
 
 * Climb up the stack, search for the specified collection type
196
 
 * and return the usage.
197
 
 */
198
 
 
199
 
static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
200
 
{
201
 
        int n;
202
 
        for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
203
 
                if (parser->device->collection[parser->collection_stack[n]].type == type)
204
 
                        return parser->device->collection[parser->collection_stack[n]].usage;
205
 
 
206
 
        return 0; /* we know nothing about this usage type */
207
 
}
208
 
 
209
 
/*
210
 
 * Add a usage to the temporary parser table.
211
 
 */
212
 
 
213
 
static int hid_add_usage(struct hid_parser *parser, unsigned usage)
214
 
{
215
 
        if (parser->local.usage_index >= HID_MAX_USAGES) {
216
 
                dbg("usage index exceeded");
217
 
                return -1;
218
 
        }
219
 
        parser->local.usage[parser->local.usage_index] = usage;
220
 
        parser->local.collection_index[parser->local.usage_index] =
221
 
                parser->collection_stack_ptr ? 
222
 
                parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
223
 
        parser->local.usage_index++;
224
 
 
225
 
        return 0;
226
 
}
227
 
 
228
 
/*
229
 
 * Register a new field for this report.
230
 
 */
231
 
 
232
 
static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
233
 
{
234
 
        struct hid_report *report;
235
 
        struct hid_field *field;
236
 
        int usages;
237
 
        unsigned offset;
238
 
        int i;
239
 
 
240
 
        if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
241
 
                dbg("hid_register_report failed");
242
 
                return -1;
243
 
        }
244
 
 
245
 
        if (parser->global.logical_maximum < parser->global.logical_minimum) {
246
 
                dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
247
 
                return -1;
248
 
        }
249
 
 
250
 
        usages = parser->local.usage_index;
251
 
 
252
 
        offset = report->size;
253
 
        report->size += parser->global.report_size * parser->global.report_count;
254
 
 
255
 
        if (usages < parser->global.report_count)
256
 
                usages = parser->global.report_count;
257
 
 
258
 
        if (usages == 0)
259
 
                return 0; /* ignore padding fields */
260
 
 
261
 
        if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
262
 
                return 0;
263
 
 
264
 
        field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
265
 
        field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
266
 
        field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
267
 
 
268
 
        for (i = 0; i < usages; i++) {
269
 
                int j = i;
270
 
                /* Duplicate the last usage we parsed if we have excess values */
271
 
                if (i >= parser->local.usage_index)
272
 
                        j = parser->local.usage_index - 1;
273
 
                field->usage[i].hid = parser->local.usage[j];
274
 
                field->usage[i].collection_index =
275
 
                        parser->local.collection_index[j];
276
 
        }
277
 
 
278
 
        field->maxusage = usages;
279
 
        field->flags = flags;
280
 
        field->report_offset = offset;
281
 
        field->report_type = report_type;
282
 
        field->report_size = parser->global.report_size;
283
 
        field->report_count = parser->global.report_count;
284
 
        field->logical_minimum = parser->global.logical_minimum;
285
 
        field->logical_maximum = parser->global.logical_maximum;
286
 
        field->physical_minimum = parser->global.physical_minimum;
287
 
        field->physical_maximum = parser->global.physical_maximum;
288
 
        field->unit_exponent = parser->global.unit_exponent;
289
 
        field->unit = parser->global.unit;
290
 
 
291
 
        return 0;
292
 
}
293
 
 
294
 
/*
295
 
 * Read data value from item.
296
 
 */
297
 
 
298
 
static __inline__ __u32 item_udata(struct hid_item *item)
299
 
{
300
 
        switch (item->size) {
301
 
                case 1: return item->data.u8;
302
 
                case 2: return item->data.u16;
303
 
                case 4: return item->data.u32;
304
 
        }
305
 
        return 0;
306
 
}
307
 
 
308
 
static __inline__ __s32 item_sdata(struct hid_item *item)
309
 
{
310
 
        switch (item->size) {
311
 
                case 1: return item->data.s8;
312
 
                case 2: return item->data.s16;
313
 
                case 4: return item->data.s32;
314
 
        }
315
 
        return 0;
316
 
}
317
 
 
318
 
/*
319
 
 * Process a global item.
320
 
 */
321
 
 
322
 
static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
323
 
{
324
 
        switch (item->tag) {
325
 
 
326
 
                case HID_GLOBAL_ITEM_TAG_PUSH:
327
 
 
328
 
                        if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
329
 
                                dbg("global enviroment stack overflow");
330
 
                                return -1;
331
 
                        }
332
 
 
333
 
                        memcpy(parser->global_stack + parser->global_stack_ptr++,
334
 
                                &parser->global, sizeof(struct hid_global));
335
 
                        return 0;
336
 
 
337
 
                case HID_GLOBAL_ITEM_TAG_POP:
338
 
 
339
 
                        if (!parser->global_stack_ptr) {
340
 
                                dbg("global enviroment stack underflow");
341
 
                                return -1;
342
 
                        }
343
 
 
344
 
                        memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
345
 
                                sizeof(struct hid_global));
346
 
                        return 0;
347
 
 
348
 
                case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
349
 
                        parser->global.usage_page = item_udata(item);
350
 
                        return 0;
351
 
 
352
 
                case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
353
 
                        parser->global.logical_minimum = item_sdata(item);
354
 
                        return 0;
355
 
 
356
 
                case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
357
 
                        if (parser->global.logical_minimum < 0)
358
 
                                parser->global.logical_maximum = item_sdata(item);
359
 
                        else
360
 
                                parser->global.logical_maximum = item_udata(item);
361
 
                        return 0;
362
 
 
363
 
                case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
364
 
                        parser->global.physical_minimum = item_sdata(item);
365
 
                        return 0;
366
 
 
367
 
                case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
368
 
                        if (parser->global.physical_minimum < 0)
369
 
                                parser->global.physical_maximum = item_sdata(item);
370
 
                        else
371
 
                                parser->global.physical_maximum = item_udata(item);
372
 
                        return 0;
373
 
 
374
 
                case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
375
 
                        parser->global.unit_exponent = item_udata(item);
376
 
                        return 0;
377
 
 
378
 
                case HID_GLOBAL_ITEM_TAG_UNIT:
379
 
                        parser->global.unit = item_udata(item);
380
 
                        return 0;
381
 
 
382
 
                case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
383
 
                        if ((parser->global.report_size = item_udata(item)) > 32) {
384
 
                                dbg("invalid report_size %d", parser->global.report_size);
385
 
                                return -1;
386
 
                        }
387
 
                        return 0;
388
 
 
389
 
                case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
390
 
                        if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
391
 
                                dbg("invalid report_count %d", parser->global.report_count);
392
 
                                return -1;
393
 
                        }
394
 
                        return 0;
395
 
 
396
 
                case HID_GLOBAL_ITEM_TAG_REPORT_ID:
397
 
                        if ((parser->global.report_id = item_udata(item)) == 0) {
398
 
                                dbg("report_id 0 is invalid");
399
 
                                return -1;
400
 
                        }
401
 
                        return 0;
402
 
 
403
 
                default:
404
 
                        dbg("unknown global tag 0x%x", item->tag);
405
 
                        return -1;
406
 
        }
407
 
}
408
 
 
409
 
/*
410
 
 * Process a local item.
411
 
 */
412
 
 
413
 
static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
414
 
{
415
 
        __u32 data;
416
 
        unsigned n;
417
 
 
418
 
        if (item->size == 0) {
419
 
                dbg("item data expected for local item");
420
 
                return -1;
421
 
        }
422
 
 
423
 
        data = item_udata(item);
424
 
 
425
 
        switch (item->tag) {
426
 
 
427
 
                case HID_LOCAL_ITEM_TAG_DELIMITER:
428
 
 
429
 
                        if (data) {
430
 
                                /*
431
 
                                 * We treat items before the first delimiter
432
 
                                 * as global to all usage sets (branch 0).
433
 
                                 * In the moment we process only these global
434
 
                                 * items and the first delimiter set.
435
 
                                 */
436
 
                                if (parser->local.delimiter_depth != 0) {
437
 
                                        dbg("nested delimiters");
438
 
                                        return -1;
439
 
                                }
440
 
                                parser->local.delimiter_depth++;
441
 
                                parser->local.delimiter_branch++;
442
 
                        } else {
443
 
                                if (parser->local.delimiter_depth < 1) {
444
 
                                        dbg("bogus close delimiter");
445
 
                                        return -1;
446
 
                                }
447
 
                                parser->local.delimiter_depth--;
448
 
                        }
449
 
                        return 1;
450
 
 
451
 
                case HID_LOCAL_ITEM_TAG_USAGE:
452
 
 
453
 
                        if (parser->local.delimiter_branch > 1) {
454
 
                                dbg("alternative usage ignored");
455
 
                                return 0;
456
 
                        }
457
 
 
458
 
                        if (item->size <= 2)
459
 
                                data = (parser->global.usage_page << 16) + data;
460
 
 
461
 
                        return hid_add_usage(parser, data);
462
 
 
463
 
                case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
464
 
 
465
 
                        if (parser->local.delimiter_branch > 1) {
466
 
                                dbg("alternative usage ignored");
467
 
                                return 0;
468
 
                        }
469
 
 
470
 
                        if (item->size <= 2)
471
 
                                data = (parser->global.usage_page << 16) + data;
472
 
 
473
 
                        parser->local.usage_minimum = data;
474
 
                        return 0;
475
 
 
476
 
                case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
477
 
 
478
 
                        if (parser->local.delimiter_branch > 1) {
479
 
                                dbg("alternative usage ignored");
480
 
                                return 0;
481
 
                        }
482
 
 
483
 
                        if (item->size <= 2)
484
 
                                data = (parser->global.usage_page << 16) + data;
485
 
 
486
 
                        for (n = parser->local.usage_minimum; n <= data; n++)
487
 
                                if (hid_add_usage(parser, n)) {
488
 
                                        dbg("hid_add_usage failed\n");
489
 
                                        return -1;
490
 
                                }
491
 
                        return 0;
492
 
 
493
 
                default:
494
 
 
495
 
                        dbg("unknown local item tag 0x%x", item->tag);
496
 
                        return 0;
497
 
        }
498
 
        return 0;
499
 
}
500
 
 
501
 
/*
502
 
 * Process a main item.
503
 
 */
504
 
 
505
 
static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
506
 
{
507
 
        __u32 data;
508
 
        int ret;
509
 
 
510
 
        data = item_udata(item);
511
 
 
512
 
        switch (item->tag) {
513
 
                case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
514
 
                        ret = open_collection(parser, data & 0xff);
515
 
                        break;
516
 
                case HID_MAIN_ITEM_TAG_END_COLLECTION:
517
 
                        ret = close_collection(parser);
518
 
                        break;
519
 
                case HID_MAIN_ITEM_TAG_INPUT:
520
 
                        ret = hid_add_field(parser, HID_INPUT_REPORT, data);
521
 
                        break;
522
 
                case HID_MAIN_ITEM_TAG_OUTPUT:
523
 
                        ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
524
 
                        break;
525
 
                case HID_MAIN_ITEM_TAG_FEATURE:
526
 
                        ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
527
 
                        break;
528
 
                default:
529
 
                        dbg("unknown main item tag 0x%x", item->tag);
530
 
                        ret = 0;
531
 
        }
532
 
 
533
 
        memset(&parser->local, 0, sizeof(parser->local));       /* Reset the local parser environment */
534
 
 
535
 
        return ret;
536
 
}
537
 
 
538
 
/*
539
 
 * Process a reserved item.
540
 
 */
541
 
 
542
 
static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
543
 
{
544
 
        dbg("reserved item type, tag 0x%x", item->tag);
545
 
        return 0;
546
 
}
547
 
 
548
 
/*
549
 
 * Free a report and all registered fields. The field->usage and
550
 
 * field->value table's are allocated behind the field, so we need
551
 
 * only to free(field) itself.
552
 
 */
553
 
 
554
 
static void hid_free_report(struct hid_report *report)
555
 
{
556
 
        unsigned n;
557
 
 
558
 
        for (n = 0; n < report->maxfield; n++)
559
 
                kfree(report->field[n]);
560
 
        if (report->data)
561
 
                kfree(report->data);
562
 
        kfree(report);
563
 
}
564
 
 
565
 
/*
566
 
 * Free a device structure, all reports, and all fields.
567
 
 */
568
 
 
569
 
static void hid_free_device(struct hid_device *device)
570
 
{
571
 
        unsigned i,j;
572
 
 
573
 
        for (i = 0; i < HID_REPORT_TYPES; i++) {
574
 
                struct hid_report_enum *report_enum = device->report_enum + i;
575
 
 
576
 
                for (j = 0; j < 256; j++) {
577
 
                        struct hid_report *report = report_enum->report_id_hash[j];
578
 
                        if (report) hid_free_report(report);
579
 
                }
580
 
        }
581
 
 
582
 
        if (device->rdesc) kfree(device->rdesc);
583
 
        if (device->collection) kfree(device->collection);
584
 
}
585
 
 
586
 
/*
587
 
 * Fetch a report description item from the data stream. We support long
588
 
 * items, though they are not used yet.
589
 
 */
590
 
 
591
 
static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
592
 
{
593
 
        if ((end - start) > 0) {
594
 
 
595
 
                __u8 b = *start++;
596
 
                item->type = (b >> 2) & 3;
597
 
                item->tag  = (b >> 4) & 15;
598
 
 
599
 
                if (item->tag == HID_ITEM_TAG_LONG) {
600
 
 
601
 
                        item->format = HID_ITEM_FORMAT_LONG;
602
 
 
603
 
                        if ((end - start) >= 2) {
604
 
 
605
 
                                item->size = *start++;
606
 
                                item->tag  = *start++;
607
 
 
608
 
                                if ((end - start) >= item->size) {
609
 
                                        item->data.longdata = start;
610
 
                                        start += item->size;
611
 
                                        return start;
612
 
                                }
613
 
                        }
614
 
                } else {
615
 
 
616
 
                        item->format = HID_ITEM_FORMAT_SHORT;
617
 
                        item->size = b & 3;
618
 
                        switch (item->size) {
619
 
 
620
 
                                case 0:
621
 
                                        return start;
622
 
 
623
 
                                case 1:
624
 
                                        if ((end - start) >= 1) {
625
 
                                                item->data.u8 = *start++;
626
 
                                                return start;
627
 
                                        }
628
 
                                        break;
629
 
 
630
 
                                case 2:
631
 
                                        if ((end - start) >= 2) {
632
 
                                                item->data.u16 = le16_to_cpu(get_unaligned((__u16*)start));
633
 
                                                start = (__u8 *)((__u16 *)start + 1);
634
 
                                                return start;
635
 
                                        }
636
 
 
637
 
                                case 3:
638
 
                                        item->size++;
639
 
                                        if ((end - start) >= 4) {
640
 
                                                item->data.u32 = le32_to_cpu(get_unaligned((__u32*)start));
641
 
                                                start = (__u8 *)((__u32 *)start + 1);
642
 
                                                return start;
643
 
                                        }
644
 
                        }
645
 
                }
646
 
        }
647
 
        return NULL;
648
 
}
649
 
 
650
 
/*
651
 
 * Parse a report description into a hid_device structure. Reports are
652
 
 * enumerated, fields are attached to these reports.
653
 
 */
654
 
 
655
 
static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
656
 
{
657
 
        struct hid_device *device;
658
 
        struct hid_parser *parser;
659
 
        struct hid_item item;
660
 
        __u8 *end;
661
 
        unsigned i;
662
 
        static int (*dispatch_type[])(struct hid_parser *parser,
663
 
                                      struct hid_item *item) = {
664
 
                hid_parser_main,
665
 
                hid_parser_global,
666
 
                hid_parser_local,
667
 
                hid_parser_reserved
668
 
        };
669
 
 
670
 
        if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
671
 
                return NULL;
672
 
        memset(device, 0, sizeof(struct hid_device));
673
 
 
674
 
        if (!(device->collection = kmalloc(sizeof(struct hid_collection) *
675
 
                                           HID_DEFAULT_NUM_COLLECTIONS,
676
 
                                           GFP_KERNEL))) {
677
 
                kfree(device);
678
 
                return NULL;
679
 
        }
680
 
        memset(device->collection, 0, sizeof(struct hid_collection) *
681
 
               HID_DEFAULT_NUM_COLLECTIONS);
682
 
        device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
683
 
 
684
 
        for (i = 0; i < HID_REPORT_TYPES; i++)
685
 
                INIT_LIST_HEAD(&device->report_enum[i].report_list);
686
 
 
687
 
        if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
688
 
                kfree(device->collection);
689
 
                kfree(device);
690
 
                return NULL;
691
 
        }
692
 
        memcpy(device->rdesc, start, size);
693
 
        device->rsize = size;
694
 
 
695
 
        if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
696
 
                kfree(device->rdesc);
697
 
                kfree(device->collection);
698
 
                kfree(device);
699
 
                return NULL;
700
 
        }
701
 
        memset(parser, 0, sizeof(struct hid_parser));
702
 
        parser->device = device;
703
 
 
704
 
        end = start + size;
705
 
        while ((start = fetch_item(start, end, &item)) != 0) {
706
 
                if (item.format != HID_ITEM_FORMAT_SHORT) {
707
 
                        dbg("unexpected long global item");
708
 
                        hid_free_device(device);
709
 
                        kfree(parser);
710
 
                        return NULL;
711
 
                }
712
 
                if (dispatch_type[item.type](parser, &item)) {
713
 
                        dbg("item %u %u %u %u parsing failed\n",
714
 
                                item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
715
 
                        hid_free_device(device);
716
 
                        kfree(parser);
717
 
                        return NULL;
718
 
                }
719
 
 
720
 
                if (start == end) {
721
 
                        if (parser->collection_stack_ptr) {
722
 
                                dbg("unbalanced collection at end of report description");
723
 
                                hid_free_device(device);
724
 
                                kfree(parser);
725
 
                                return NULL;
726
 
                        }
727
 
                        if (parser->local.delimiter_depth) {
728
 
                                dbg("unbalanced delimiter at end of report description");
729
 
                                hid_free_device(device);
730
 
                                kfree(parser);
731
 
                                return NULL;
732
 
                        }
733
 
                        kfree(parser);
734
 
                        return device;
735
 
                }
736
 
        }
737
 
 
738
 
        dbg("item fetching failed at offset %d\n", (int)(end - start));
739
 
        hid_free_device(device);
740
 
        kfree(parser);
741
 
        return NULL;
742
 
}
743
 
 
744
 
/*
745
 
 * Convert a signed n-bit integer to signed 32-bit integer. Common
746
 
 * cases are done through the compiler, the screwed things has to be
747
 
 * done by hand.
748
 
 */
749
 
 
750
 
static __inline__ __s32 snto32(__u32 value, unsigned n)
751
 
{
752
 
        switch (n) {
753
 
                case 8:  return ((__s8)value);
754
 
                case 16: return ((__s16)value);
755
 
                case 32: return ((__s32)value);
756
 
        }
757
 
        return value & (1 << (n - 1)) ? value | (-1 << n) : value;
758
 
}
759
 
 
760
 
/*
761
 
 * Convert a signed 32-bit integer to a signed n-bit integer.
762
 
 */
763
 
 
764
 
static __inline__ __u32 s32ton(__s32 value, unsigned n)
765
 
{
766
 
        __s32 a = value >> (n - 1);
767
 
        if (a && a != -1) return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
768
 
        return value & ((1 << n) - 1);
769
 
}
770
 
 
771
 
/*
772
 
 * Extract/implement a data field from/to a report.
773
 
 */
774
 
 
775
 
static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
776
 
{
777
 
        report += (offset >> 5) << 2; offset &= 31;
778
 
        return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
779
 
}
780
 
 
781
 
static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
782
 
{
783
 
        report += (offset >> 5) << 2; offset &= 31;
784
 
        put_unaligned((get_unaligned((__u64*)report)
785
 
                & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
786
 
                | cpu_to_le64((__u64)value << offset), (__u64*)report);
787
 
}
788
 
 
789
 
/*
790
 
 * Search an array for a value.
791
 
 */
792
 
 
793
 
static __inline__ int search(__s32 *array, __s32 value, unsigned n)
794
 
{
795
 
        while (n--) if (*array++ == value) return 0;
796
 
        return -1;
797
 
}
798
 
 
799
 
static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
800
 
{
801
 
        hid_dump_input(usage, value);
802
 
        if (hid->claimed & HID_CLAIMED_INPUT)
803
 
                hidinput_hid_event(hid, field, usage, value);
804
 
        if (hid->claimed & HID_CLAIMED_HIDDEV)
805
 
                hiddev_hid_event(hid, field, usage, value);
806
 
}
807
 
 
808
 
 
809
 
/*
810
 
 * Analyse a received field, and fetch the data from it. The field
811
 
 * content is stored for next report processing (we do differential
812
 
 * reporting to the layer).
813
 
 */
814
 
 
815
 
static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data)
816
 
{
817
 
        unsigned n;
818
 
        unsigned count = field->report_count;
819
 
        unsigned offset = field->report_offset;
820
 
        unsigned size = field->report_size;
821
 
        __s32 min = field->logical_minimum;
822
 
        __s32 max = field->logical_maximum;
823
 
        __s32 value[count]; /* WARNING: gcc specific */
824
 
 
825
 
        for (n = 0; n < count; n++) {
826
 
 
827
 
                        value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
828
 
                                                    extract(data, offset + n * size, size);
829
 
 
830
 
                        if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
831
 
                            && value[n] >= min && value[n] <= max
832
 
                            && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
833
 
                                return;
834
 
        }
835
 
 
836
 
        for (n = 0; n < count; n++) {
837
 
 
838
 
                if (HID_MAIN_ITEM_VARIABLE & field->flags) {
839
 
 
840
 
                        if (field->flags & HID_MAIN_ITEM_RELATIVE) {
841
 
                                if (!value[n]) continue;
842
 
                        } else {
843
 
                                if (value[n] == field->value[n]) continue;
844
 
                        }       
845
 
                        hid_process_event(hid, field, &field->usage[n], value[n]);
846
 
                        continue;
847
 
                }
848
 
 
849
 
                if (field->value[n] >= min && field->value[n] <= max
850
 
                        && field->usage[field->value[n] - min].hid
851
 
                        && search(value, field->value[n], count))
852
 
                                hid_process_event(hid, field, &field->usage[field->value[n] - min], 0);
853
 
 
854
 
                if (value[n] >= min && value[n] <= max
855
 
                        && field->usage[value[n] - min].hid
856
 
                        && search(field->value, value[n], count))
857
 
                                hid_process_event(hid, field, &field->usage[value[n] - min], 1);
858
 
        }
859
 
 
860
 
        memcpy(field->value, value, count * sizeof(__s32));
861
 
}
862
 
 
863
 
static int hid_input_report(int type, u8 *data, int len, struct hid_device *hid)
864
 
{
865
 
        struct hid_report_enum *report_enum = hid->report_enum + type;
866
 
        struct hid_report *report;
867
 
        int n, size;
868
 
 
869
 
        if (!len) {
870
 
                dbg("empty report");
871
 
                return -1;
872
 
        }
873
 
 
874
 
#ifdef DEBUG_DATA
875
 
        printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", len, report_enum->numbered ? "" : "un");
876
 
#endif
877
 
 
878
 
        n = 0;                          /* Normally report number is 0 */
879
 
        if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
880
 
                n = *data++;
881
 
                len--;
882
 
        }
883
 
 
884
 
        if (!(report = report_enum->report_id_hash[n])) {
885
 
                dbg("undefined report_id %d received", n);
886
 
#ifdef DEBUG
887
 
                        printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
888
 
                        for (n = 0; n < len; n++)
889
 
                                printk(" %02x", data[n]);
890
 
                        printk("\n");
891
 
#endif
892
 
 
893
 
                return -1;
894
 
        }
895
 
 
896
 
        if (hid->claimed & HID_CLAIMED_HIDDEV)
897
 
                hiddev_report_event(hid, report);
898
 
 
899
 
        size = ((report->size - 1) >> 3) + 1;
900
 
 
901
 
        if (len < size) {
902
 
 
903
 
                if (size <= 8) {
904
 
                        dbg("report %d is too short, (%d < %d)", report->id, len, size);
905
 
                        return -1;
906
 
                }
907
 
 
908
 
                /*
909
 
                 * Some low-speed devices have large reports and maxpacketsize 8.
910
 
                 * We buffer the data in that case and parse it when we got it all.
911
 
                 * Works only for unnumbered reports. Doesn't make sense for numbered
912
 
                 * reports anyway - then they don't need to be large.
913
 
                 */
914
 
 
915
 
                if (!report->data)
916
 
                        if (!(report->data = kmalloc(size, GFP_ATOMIC))) {
917
 
                                dbg("couldn't allocate report buffer");
918
 
                                return -1;
919
 
                        }
920
 
 
921
 
                if (report->idx + len > size) {
922
 
                        dbg("report data buffer overflow");
923
 
                        report->idx = 0;
924
 
                        return -1;
925
 
                }
926
 
 
927
 
                memcpy(report->data + report->idx, data, len);
928
 
                report->idx += len;
929
 
 
930
 
                if (report->idx < size)
931
 
                        return 0;
932
 
 
933
 
                data = report->data;
934
 
        }
935
 
 
936
 
        for (n = 0; n < report->maxfield; n++)
937
 
                hid_input_field(hid, report->field[n], data);
938
 
 
939
 
        report->idx = 0;
940
 
        return 0;
941
 
}
942
 
 
943
 
/*
944
 
 * Interrupt input handler.
945
 
 */
946
 
 
947
 
static void hid_irq(struct urb *urb)
948
 
{
949
 
        if (urb->status) {
950
 
                dbg("nonzero status in irq %d", urb->status);
951
 
                return;
952
 
        }
953
 
 
954
 
        hid_input_report(HID_INPUT_REPORT, urb->transfer_buffer, urb->actual_length, urb->context);
955
 
}
956
 
 
957
 
/*
958
 
 * hid_read_report() reads in report values without waiting for an irq urb.
959
 
 */
960
 
 
961
 
void hid_read_report(struct hid_device *hid, struct hid_report *report)
962
 
{
963
 
        int len = ((report->size - 1) >> 3) + 1 + hid->report_enum[report->type].numbered;
964
 
        u8 data[len];
965
 
        int read;
966
 
 
967
 
        if (hid->quirks & HID_QUIRK_NOGET)
968
 
                return;
969
 
 
970
 
        if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, data, len)) != len) {
971
 
                dbg("reading report type %d id %d failed len %d read %d", report->type + 1, report->id, len, read);
972
 
                return;
973
 
        }
974
 
 
975
 
        hid_input_report(report->type, data, len, hid);
976
 
}
977
 
 
978
 
/*
979
 
 * Output the field into the report.
980
 
 */
981
 
 
982
 
static void hid_output_field(struct hid_field *field, __u8 *data)
983
 
{
984
 
        unsigned count = field->report_count;
985
 
        unsigned offset = field->report_offset;
986
 
        unsigned size = field->report_size;
987
 
        unsigned n;
988
 
 
989
 
        for (n = 0; n < count; n++) {
990
 
                if (field->logical_minimum < 0) /* signed values */
991
 
                        implement(data, offset + n * size, size, s32ton(field->value[n], size));
992
 
                 else                           /* unsigned values */
993
 
                        implement(data, offset + n * size, size, field->value[n]);
994
 
        }
995
 
}
996
 
 
997
 
/*
998
 
 * Create a report.
999
 
 */
1000
 
 
1001
 
void hid_output_report(struct hid_report *report, __u8 *data)
1002
 
{
1003
 
        unsigned n;
1004
 
        for (n = 0; n < report->maxfield; n++)
1005
 
                hid_output_field(report->field[n], data);
1006
 
}
1007
 
 
1008
 
/*
1009
 
 * Set a field value. The report this field belongs to has to be
1010
 
 * created and transfered to the device, to set this value in the
1011
 
 * device.
1012
 
 */
1013
 
 
1014
 
int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1015
 
{
1016
 
        unsigned size = field->report_size;
1017
 
 
1018
 
        hid_dump_input(field->usage + offset, value);
1019
 
 
1020
 
        if (offset >= field->report_count) {
1021
 
                dbg("offset exceeds report_count");
1022
 
                return -1;
1023
 
        }
1024
 
        if (field->logical_minimum < 0) {
1025
 
                if (value != snto32(s32ton(value, size), size)) {
1026
 
                        dbg("value %d is out of range", value);
1027
 
                        return -1;
1028
 
                }
1029
 
        }
1030
 
        if (   (value > field->logical_maximum)
1031
 
            || (value < field->logical_minimum)) {
1032
 
                dbg("value %d is invalid", value);
1033
 
                return -1;
1034
 
        }
1035
 
        field->value[offset] = value;
1036
 
        return 0;
1037
 
}
1038
 
 
1039
 
int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1040
 
{
1041
 
        struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1042
 
        struct list_head *list = report_enum->report_list.next;
1043
 
        int i, j;
1044
 
 
1045
 
        while (list != &report_enum->report_list) {
1046
 
                struct hid_report *report = (struct hid_report *) list;
1047
 
                list = list->next;
1048
 
                for (i = 0; i < report->maxfield; i++) {
1049
 
                        *field = report->field[i];
1050
 
                        for (j = 0; j < (*field)->maxusage; j++)
1051
 
                                if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1052
 
                                        return j;
1053
 
                }
1054
 
        }
1055
 
        return -1;
1056
 
}
1057
 
 
1058
 
static int hid_submit_out(struct hid_device *hid)
1059
 
{
1060
 
        hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.wLength);
1061
 
        hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
1062
 
        hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
1063
 
        hid->urbout.dev = hid->dev;
1064
 
 
1065
 
        if (usb_submit_urb(&hid->urbout)) {
1066
 
                err("usb_submit_urb(out) failed");
1067
 
                return -1;
1068
 
        }
1069
 
 
1070
 
        return 0;
1071
 
}
1072
 
 
1073
 
static void hid_ctrl(struct urb *urb)
1074
 
{
1075
 
        struct hid_device *hid = urb->context;
1076
 
 
1077
 
        if (urb->status)
1078
 
                warn("ctrl urb status %d received", urb->status);
1079
 
 
1080
 
        hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1081
 
 
1082
 
        if (hid->outhead != hid->outtail)
1083
 
                hid_submit_out(hid);
1084
 
}
1085
 
 
1086
 
void hid_write_report(struct hid_device *hid, struct hid_report *report)
1087
 
{
1088
 
        if (hid->report_enum[report->type].numbered) {
1089
 
                hid->out[hid->outhead].buffer[0] = report->id;
1090
 
                hid_output_report(report, hid->out[hid->outhead].buffer + 1);
1091
 
                hid->out[hid->outhead].dr.wLength = cpu_to_le16(((report->size + 7) >> 3) + 1);
1092
 
        } else {
1093
 
                hid_output_report(report, hid->out[hid->outhead].buffer);
1094
 
                hid->out[hid->outhead].dr.wLength = cpu_to_le16((report->size + 7) >> 3);
1095
 
        }
1096
 
 
1097
 
        hid->out[hid->outhead].dr.wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
1098
 
 
1099
 
        hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1100
 
 
1101
 
        if (hid->outhead == hid->outtail)
1102
 
                hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1103
 
 
1104
 
        if (hid->urbout.status != -EINPROGRESS)
1105
 
                hid_submit_out(hid);
1106
 
}
1107
 
 
1108
 
int hid_open(struct hid_device *hid)
1109
 
{
1110
 
        if (hid->open++)
1111
 
                return 0;
1112
 
 
1113
 
        hid->urb.dev = hid->dev;
1114
 
 
1115
 
        if (usb_submit_urb(&hid->urb))
1116
 
                return -EIO;
1117
 
 
1118
 
        return 0;
1119
 
}
1120
 
 
1121
 
void hid_close(struct hid_device *hid)
1122
 
{
1123
 
        if (!--hid->open)
1124
 
                usb_unlink_urb(&hid->urb);
1125
 
}
1126
 
 
1127
 
/*
1128
 
 * Initialize all readable reports
1129
 
 */
1130
 
void hid_init_reports(struct hid_device *hid)
1131
 
{
1132
 
        int i;
1133
 
        struct hid_report *report;
1134
 
        struct hid_report_enum *report_enum;
1135
 
        struct list_head *list;
1136
 
 
1137
 
        for (i = 0; i < HID_REPORT_TYPES; i++) {
1138
 
                if (i == HID_FEATURE_REPORT || i == HID_INPUT_REPORT) {
1139
 
                        report_enum = hid->report_enum + i;
1140
 
                        list = report_enum->report_list.next;
1141
 
                        while (list != &report_enum->report_list) {
1142
 
                                report = (struct hid_report *) list;
1143
 
                                hid_read_report(hid, report);
1144
 
                                usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
1145
 
                                list = list->next;
1146
 
                        }
1147
 
                }
1148
 
        }
1149
 
}
1150
 
 
1151
 
#define USB_VENDOR_ID_WACOM             0x056a
1152
 
#define USB_DEVICE_ID_WACOM_PENPARTNER  0x0000
1153
 
#define USB_DEVICE_ID_WACOM_GRAPHIRE    0x0010
1154
 
#define USB_DEVICE_ID_WACOM_INTUOS      0x0020
1155
 
#define USB_DEVICE_ID_WACOM_PL          0x0030
1156
 
#define USB_DEVICE_ID_WACOM_INTUOS2     0x0040
1157
 
#define USB_DEVICE_ID_WACOM_VOLITO      0x0060
1158
 
#define USB_DEVICE_ID_WACOM_PTU         0x0003
1159
 
#define USB_DEVICE_ID_WACOM_INTUOS3     0x00B0
1160
 
 
1161
 
#define USB_VENDOR_ID_KBGEAR            0x084e
1162
 
#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO  0x1001
1163
 
 
1164
 
#define USB_VENDOR_ID_AIPTEK            0x08ca
1165
 
#define USB_DEVICE_ID_AIPTEK_01 0x0001
1166
 
#define USB_DEVICE_ID_AIPTEK_10 0x0010
1167
 
#define USB_DEVICE_ID_AIPTEK_20 0x0020
1168
 
#define USB_DEVICE_ID_AIPTEK_21 0x0021
1169
 
#define USB_DEVICE_ID_AIPTEK_22 0x0022
1170
 
#define USB_DEVICE_ID_AIPTEK_23 0x0023
1171
 
#define USB_DEVICE_ID_AIPTEK_24 0x0024
1172
 
 
1173
 
#define USB_VENDOR_ID_ATEN              0x0557
1174
 
#define USB_DEVICE_ID_ATEN_UC100KM      0x2004
1175
 
#define USB_DEVICE_ID_ATEN_CS124U       0x2202
1176
 
#define USB_DEVICE_ID_ATEN_2PORTKVM     0x2204
1177
 
#define USB_DEVICE_ID_ATEN_4PORTKVM     0x2205
1178
 
 
1179
 
#define USB_VENDOR_ID_TOPMAX            0x0663
1180
 
#define USB_DEVICE_ID_TOPMAX_COBRAPAD   0x0103
1181
 
 
1182
 
#define USB_VENDOR_ID_HAPP              0x078b
1183
 
#define USB_DEVICE_ID_UGCI_DRIVING      0x0010
1184
 
#define USB_DEVICE_ID_UGCI_FLYING       0x0020
1185
 
#define USB_DEVICE_ID_UGCI_FIGHTING     0x0030
1186
 
 
1187
 
#define USB_VENDOR_ID_GRIFFIN           0x077d
1188
 
#define USB_DEVICE_ID_POWERMATE         0x0410 /* Griffin PowerMate */
1189
 
#define USB_DEVICE_ID_SOUNDKNOB         0x04AA /* Griffin SoundKnob */
1190
 
 
1191
 
#define USB_VENDOR_ID_ONTRAK    0x0a07
1192
 
#define USB_DEVICE_ID_ONTRAK_ADU100     0x0064
1193
 
 
1194
 
#define USB_VENDOR_ID_TANGTOP           0x0d3d
1195
 
#define USB_DEVICE_ID_TANGTOP_USBPS2    0x0001
1196
 
 
1197
 
#define USB_VENDOR_ID_OKI               0x070a
1198
 
#define USB_VENDOR_ID_OKI_MULITI        0x0007
1199
 
 
1200
 
#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
1201
 
#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5      0x0100
1202
 
 
1203
 
#define USB_VENDOR_ID_MGE               0x0463
1204
 
#define USB_DEVICE_ID_MGE_UPS           0xffff
1205
 
#define USB_DEVICE_ID_MGE_UPS1          0x0001
1206
 
 
1207
 
#define USB_VENDOR_ID_NEC               0x073e
1208
 
#define USB_DEVICE_ID_NEC_USB_GAME_PAD  0x0301
1209
 
 
1210
 
struct hid_blacklist {
1211
 
        __u16 idVendor;
1212
 
        __u16 idProduct;
1213
 
        unsigned quirks;
1214
 
} hid_blacklist[] = {
1215
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
1216
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
1217
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
1218
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
1219
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
1220
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
1221
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
1222
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
1223
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
1224
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
1225
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
1226
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
1227
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
1228
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
1229
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
1230
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
1231
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
1232
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
1233
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
1234
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
1235
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 5, HID_QUIRK_IGNORE },
1236
 
        /* Intuos2 6x8 reports as 0x47 instead of 0x42 */
1237
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 7, HID_QUIRK_IGNORE },
1238
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_VOLITO, HID_QUIRK_IGNORE },
1239
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 3, HID_QUIRK_IGNORE },
1240
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 4, HID_QUIRK_IGNORE },
1241
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PTU, HID_QUIRK_IGNORE },
1242
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3, HID_QUIRK_IGNORE },
1243
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 1, HID_QUIRK_IGNORE },
1244
 
        { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS3 + 2, HID_QUIRK_IGNORE },
1245
 
        { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
1246
 
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
1247
 
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
1248
 
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
1249
 
        { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
1250
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
1251
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
1252
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
1253
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
1254
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
1255
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
1256
 
        { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
1257
 
        { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
1258
 
        { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
1259
 
        { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
1260
 
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1261
 
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1262
 
        { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD|HID_QUIRK_MULTI_INPUT },
1263
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
1264
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
1265
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
1266
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
1267
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
1268
 
        { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
1269
 
        { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1270
 
        { USB_VENDOR_ID_OKI, USB_VENDOR_ID_OKI_MULITI, HID_QUIRK_NOGET },
1271
 
        { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
1272
 
        { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
1273
 
        { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
1274
 
        { USB_VENDOR_ID_NEC, USB_DEVICE_ID_NEC_USB_GAME_PAD, HID_QUIRK_BADPAD },
1275
 
        { 0, 0 }
1276
 
};
1277
 
 
1278
 
static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum)
1279
 
{
1280
 
        struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
1281
 
        struct hid_descriptor *hdesc;
1282
 
        struct hid_device *hid;
1283
 
        unsigned quirks = 0, rsize = 0;
1284
 
        char *buf;
1285
 
        int n;
1286
 
 
1287
 
        for (n = 0; hid_blacklist[n].idVendor; n++)
1288
 
                if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1289
 
                        (hid_blacklist[n].idProduct == dev->descriptor.idProduct))
1290
 
                                quirks = hid_blacklist[n].quirks;
1291
 
 
1292
 
        if (quirks & HID_QUIRK_IGNORE)
1293
 
                return NULL;
1294
 
 
1295
 
        if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc) && ((!interface->bNumEndpoints) ||
1296
 
                usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc))) {
1297
 
                        dbg("class descriptor not present\n");
1298
 
                        return NULL;
1299
 
        }
1300
 
 
1301
 
        for (n = 0; n < hdesc->bNumDescriptors; n++)
1302
 
                if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
1303
 
                        rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1304
 
 
1305
 
        if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1306
 
                dbg("weird size of report descriptor (%u)", rsize);
1307
 
                return NULL;
1308
 
        }
1309
 
 
1310
 
        {
1311
 
                __u8 rdesc[rsize];
1312
 
 
1313
 
                if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
1314
 
                        dbg("reading report descriptor failed");
1315
 
                        return NULL;
1316
 
                }
1317
 
 
1318
 
#ifdef DEBUG_DATA
1319
 
                printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
1320
 
                for (n = 0; n < rsize; n++)
1321
 
                        printk(" %02x", (unsigned) rdesc[n]);
1322
 
                printk("\n");
1323
 
#endif
1324
 
 
1325
 
                if (!(hid = hid_parse_report(rdesc, rsize))) {
1326
 
                        dbg("parsing report descriptor failed");
1327
 
                        return NULL;
1328
 
                }
1329
 
        }
1330
 
 
1331
 
        hid->quirks = quirks;
1332
 
 
1333
 
        for (n = 0; n < interface->bNumEndpoints; n++) {
1334
 
 
1335
 
                struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
1336
 
                int pipe, maxp;
1337
 
 
1338
 
                if ((endpoint->bmAttributes & 3) != 3)          /* Not an interrupt endpoint */
1339
 
                        continue;
1340
 
 
1341
 
                if (!(endpoint->bEndpointAddress & 0x80))       /* Not an input endpoint */
1342
 
                        continue;
1343
 
 
1344
 
                pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1345
 
                maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1346
 
 
1347
 
                FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, endpoint->bInterval);
1348
 
 
1349
 
                break;
1350
 
        }
1351
 
 
1352
 
        if (n == interface->bNumEndpoints) {
1353
 
                dbg("couldn't find an input interrupt endpoint");
1354
 
                hid_free_device(hid);
1355
 
                return NULL;
1356
 
        }
1357
 
 
1358
 
        hid->version = hdesc->bcdHID;
1359
 
        hid->country = hdesc->bCountryCode;
1360
 
        hid->dev = dev;
1361
 
        hid->ifnum = interface->bInterfaceNumber;
1362
 
 
1363
 
        for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
1364
 
                hid->out[n].dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1365
 
                hid->out[n].dr.bRequest = USB_REQ_SET_REPORT;
1366
 
                hid->out[n].dr.wIndex = cpu_to_le16(hid->ifnum);
1367
 
        }
1368
 
 
1369
 
        hid->name[0] = 0;
1370
 
 
1371
 
        if (!(buf = kmalloc(63, GFP_KERNEL)))
1372
 
                return NULL;
1373
 
 
1374
 
        if (usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) {
1375
 
                strcat(hid->name, buf);
1376
 
                if (usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
1377
 
                        sprintf(hid->name, "%s %s", hid->name, buf);
1378
 
        } else
1379
 
                sprintf(hid->name, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
1380
 
 
1381
 
        kfree(buf);
1382
 
 
1383
 
        FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
1384
 
                (void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
1385
 
 
1386
 
/*
1387
 
 * Some devices don't like this and crash. I don't know of any devices
1388
 
 * needing this, so it is disabled for now.
1389
 
 */
1390
 
 
1391
 
#if 0
1392
 
        if (interface->bInterfaceSubClass == 1)
1393
 
                usb_set_protocol(dev, hid->ifnum, 1);
1394
 
#endif
1395
 
 
1396
 
        return hid;
1397
 
}
1398
 
 
1399
 
static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
1400
 
                       const struct usb_device_id *id)
1401
 
{
1402
 
        struct hid_device *hid;
1403
 
        int i;
1404
 
        char *c;
1405
 
 
1406
 
        dbg("HID probe called for ifnum %d", ifnum);
1407
 
 
1408
 
        if (!(hid = usb_hid_configure(dev, ifnum)))
1409
 
                return NULL;
1410
 
 
1411
 
        hid_init_reports(hid);
1412
 
        hid_dump_device(hid);
1413
 
 
1414
 
        if (!hidinput_connect(hid))
1415
 
                hid->claimed |= HID_CLAIMED_INPUT;
1416
 
        if (!hiddev_connect(hid))
1417
 
                hid->claimed |= HID_CLAIMED_HIDDEV;
1418
 
        printk(KERN_INFO);
1419
 
 
1420
 
        if (hid->claimed & HID_CLAIMED_INPUT)
1421
 
                printk("input");
1422
 
        if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
1423
 
                printk(",");
1424
 
        if (hid->claimed & HID_CLAIMED_HIDDEV)
1425
 
                printk("hiddev%d", hid->minor);
1426
 
 
1427
 
        c = "Device";
1428
 
        for (i = 0; i < hid->maxcollection; i++) {
1429
 
                if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1430
 
                   (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1431
 
                   (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1432
 
                        c = hid_types[hid->collection[i].usage & 0xffff];
1433
 
                        break;
1434
 
                }
1435
 
        }
1436
 
 
1437
 
        printk(": USB HID v%x.%02x %s [%s] on usb%d:%d.%d\n",
1438
 
                hid->version >> 8, hid->version & 0xff, c, hid->name,
1439
 
                dev->bus->busnum, dev->devnum, ifnum);
1440
 
 
1441
 
        return hid;
1442
 
}
1443
 
 
1444
 
static void hid_disconnect(struct usb_device *dev, void *ptr)
1445
 
{
1446
 
        struct hid_device *hid = ptr;
1447
 
 
1448
 
        dbg("cleanup called");
1449
 
        usb_unlink_urb(&hid->urb);
1450
 
        if (hid->claimed & HID_CLAIMED_INPUT)
1451
 
                hidinput_disconnect(hid);
1452
 
        if (hid->claimed & HID_CLAIMED_HIDDEV)
1453
 
                hiddev_disconnect(hid);
1454
 
        hid_free_device(hid);
1455
 
}
1456
 
 
1457
 
static struct usb_device_id hid_usb_ids [] = {
1458
 
        { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
1459
 
            bInterfaceClass: USB_INTERFACE_CLASS_HID },
1460
 
        { }                                             /* Terminating entry */
1461
 
};
1462
 
 
1463
 
MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1464
 
 
1465
 
static struct usb_driver hid_driver = {
1466
 
        name:           "hid",
1467
 
        probe:          hid_probe,
1468
 
        disconnect:     hid_disconnect,
1469
 
        id_table:       hid_usb_ids,
1470
 
};
1471
 
 
1472
 
static int __init hid_init(void)
1473
 
{
1474
 
        hiddev_init();
1475
 
        usb_register(&hid_driver);
1476
 
        info(DRIVER_VERSION " " DRIVER_AUTHOR);
1477
 
        info(DRIVER_DESC);
1478
 
 
1479
 
        return 0;
1480
 
}
1481
 
 
1482
 
static void __exit hid_exit(void)
1483
 
{
1484
 
        usb_deregister(&hid_driver);
1485
 
        hiddev_exit();
1486
 
}
1487
 
 
1488
 
module_init(hid_init);
1489
 
module_exit(hid_exit);
1490
 
 
1491
 
MODULE_AUTHOR( DRIVER_AUTHOR );
1492
 
MODULE_DESCRIPTION( DRIVER_DESC );
1493
 
MODULE_LICENSE("GPL");