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