linux-old/drivers/usb/hid-input.c
<<
>>
Prefs
   1/*
   2 * $Id: hid-input.c,v 1.5 2001/05/23 09:25:02 vojtech Exp $
   3 *
   4 *  Copyright (c) 2000-2001 Vojtech Pavlik
   5 *
   6 *  USB HID to Linux Input mapping module
   7 *
   8 *  Sponsored by SuSE
   9 */
  10
  11/*
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License as published by
  14 * the Free Software Foundation; either version 2 of the License, or
  15 * (at your option) any later version.
  16 *
  17 * This program is distributed in the hope that it will be useful,
  18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 * GNU General Public License for more details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25 *
  26 * Should you need to contact me, the author, you can do so either by
  27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  29 */
  30
  31#include <linux/module.h>
  32#include <linux/slab.h>
  33#include <linux/init.h>
  34#include <linux/kernel.h>
  35#include <linux/input.h>
  36#include <linux/usb.h>
  37
  38#include "hid.h"
  39
  40#define unk     KEY_UNKNOWN
  41
  42static unsigned char hid_keyboard[256] = {
  43          0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
  44         50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
  45          4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
  46         27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
  47         65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
  48        105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
  49         72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
  50        120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
  51        115,114,unk,unk,unk,124,unk,181,182,183,184,185,186,187,188,189,
  52        190,191,192,193,194,195,196,197,198,unk,unk,unk,unk,unk,unk,unk,
  53        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  54        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  55        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  56        unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  57         29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
  58        150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
  59};
  60
  61static struct {
  62        __s32 x;
  63        __s32 y;
  64}  hid_hat_to_axis[] = {{0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  65
  66static struct input_dev *find_input(struct hid_device *hid, struct hid_field *field)
  67{
  68        struct list_head *lh;
  69        struct hid_input *hidinput;
  70
  71        list_for_each (lh, &hid->inputs) {
  72                int i;
  73
  74                hidinput = list_entry(lh, struct hid_input, list);
  75
  76                if (! hidinput->report)
  77                        continue;
  78
  79                for (i = 0; i < hidinput->report->maxfield; i++)
  80                        if (hidinput->report->field[i] == field)
  81                                return &hidinput->input;
  82        }
  83
  84        /* Assume we only have one input and use it */
  85        if (!list_empty(&hid->inputs)) {
  86                hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  87                return &hidinput->input;
  88        }
  89
  90        /* This is really a bug */
  91        return NULL;
  92}
  93
  94static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  95                                     struct hid_usage *usage)
  96{
  97        struct input_dev *input = &hidinput->input;
  98        struct hid_device *device = hidinput->input.private;
  99        int max;
 100        unsigned long *bit;
 101
 102        switch (usage->hid & HID_USAGE_PAGE) {
 103
 104                case HID_UP_KEYBOARD:
 105
 106                        set_bit(EV_REP, input->evbit);
 107                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 108
 109                        if ((usage->hid & HID_USAGE) < 256) {
 110                                if (!(usage->code = hid_keyboard[usage->hid & HID_USAGE]))
 111                                        return;
 112                                clear_bit(usage->code, bit);
 113                        } else
 114                                usage->code = KEY_UNKNOWN;
 115
 116                        break;
 117
 118                case HID_UP_BUTTON:
 119
 120                        usage->code = ((usage->hid - 1) & 0xf) + 0x100;
 121                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 122
 123                        switch (field->application) {
 124                                case HID_GD_GAMEPAD:  usage->code += 0x10;
 125                                case HID_GD_JOYSTICK: usage->code += 0x10;
 126                                case HID_GD_MOUSE:    usage->code += 0x10; break;
 127                                default:
 128                                        if (field->physical == HID_GD_POINTER)
 129                                                usage->code += 0x10;
 130                                        break;
 131                        }
 132                        break;
 133
 134                case HID_UP_GENDESK:
 135
 136                        if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
 137                                switch (usage->hid & 0xf) {
 138                                        case 0x1: usage->code = KEY_POWER;  break;
 139                                        case 0x2: usage->code = KEY_SLEEP;  break;
 140                                        case 0x3: usage->code = KEY_WAKEUP; break;
 141                                        default: usage->code = KEY_UNKNOWN; break;
 142                                }
 143                                usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 144                                break;
 145                        }
 146
 147                        usage->code = usage->hid & 0xf;
 148
 149                        if (field->report_size == 1) {
 150                                usage->code = BTN_MISC;
 151                                usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 152                                break;
 153                        }
 154
 155                        if (field->flags & HID_MAIN_ITEM_RELATIVE) {
 156                                usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
 157                                break;
 158                        }
 159
 160                        usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
 161
 162                        if (usage->hid == HID_GD_HATSWITCH) {
 163                                usage->code = ABS_HAT0X;
 164                                usage->hat_min = field->logical_minimum;
 165                                usage->hat_max = field->logical_maximum;
 166                        }
 167                        break;
 168
 169                case HID_UP_LED:
 170
 171                        usage->code = (usage->hid - 1) & 0xf;
 172                        usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
 173                        break;
 174
 175                case HID_UP_DIGITIZER:
 176
 177                        switch (usage->hid & 0xff) {
 178
 179                                case 0x30: /* TipPressure */
 180
 181                                        if (!test_bit(BTN_TOUCH, input->keybit)) {
 182                                                device->quirks |= HID_QUIRK_NOTOUCH;
 183                                                set_bit(EV_KEY, input->evbit);
 184                                                set_bit(BTN_TOUCH, input->keybit);
 185                                        }
 186                                        usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
 187                                        usage->code = ABS_PRESSURE;
 188                                        clear_bit(usage->code, bit);
 189                                        break;
 190
 191                                case 0x32: /* InRange */
 192
 193                                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 194                                        switch (field->physical & 0xff) {
 195                                                case 0x21: usage->code = BTN_TOOL_MOUSE; break;
 196                                                case 0x22: usage->code = BTN_TOOL_FINGER; break;
 197                                                default: usage->code = BTN_TOOL_PEN; break;
 198                                        }
 199                                        break;
 200
 201                                case 0x3c: /* Invert */
 202
 203                                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 204                                        usage->code = BTN_TOOL_RUBBER;
 205                                        clear_bit(usage->code, bit);
 206                                        break;
 207
 208                                case 0x33: /* Touch */
 209                                case 0x42: /* TipSwitch */
 210                                case 0x43: /* TipSwitch2 */
 211
 212                                        device->quirks &= ~HID_QUIRK_NOTOUCH;
 213                                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 214                                        usage->code = BTN_TOUCH;
 215                                        clear_bit(usage->code, bit);
 216                                        break;
 217
 218                                case 0x44: /* BarrelSwitch */
 219
 220                                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 221                                        usage->code = BTN_STYLUS;
 222                                        clear_bit(usage->code, bit);
 223                                        break;
 224
 225                                default:  goto unknown;
 226                        }
 227                        break;
 228
 229                case HID_UP_CONSUMER:   /* USB HUT v1.1, pages 56-62 */
 230
 231                        switch (usage->hid & HID_USAGE) {
 232                                case 0x000: usage->code = 0; break;
 233                                case 0x034: usage->code = KEY_SLEEP;            break;
 234                                case 0x036: usage->code = BTN_MISC;             break;
 235                                case 0x08a: usage->code = KEY_WWW;              break;
 236                                case 0x095: usage->code = KEY_HELP;             break;
 237
 238                                case 0x0b4: usage->code = KEY_REWIND;           break;
 239                                case 0x0b5: usage->code = KEY_NEXTSONG;         break;
 240                                case 0x0b6: usage->code = KEY_PREVIOUSSONG;     break;
 241                                case 0x0b7: usage->code = KEY_STOPCD;           break;
 242                                case 0x0b8: usage->code = KEY_EJECTCD;          break;
 243                                case 0x0cd: usage->code = KEY_PLAYPAUSE;        break;
 244
 245                                case 0x0e2: usage->code = KEY_MUTE;             break;
 246                                case 0x0e9: usage->code = KEY_VOLUMEUP;         break;
 247                                case 0x0ea: usage->code = KEY_VOLUMEDOWN;       break;
 248
 249                                case 0x183: usage->code = KEY_CONFIG;           break;
 250                                case 0x18a: usage->code = KEY_MAIL;             break;
 251                                case 0x192: usage->code = KEY_CALC;             break;
 252                                case 0x194: usage->code = KEY_FILE;             break;
 253
 254                                case 0x21a: usage->code = KEY_UNDO;             break;
 255                                case 0x21b: usage->code = KEY_COPY;             break;
 256                                case 0x21c: usage->code = KEY_CUT;              break;
 257                                case 0x21d: usage->code = KEY_PASTE;            break;
 258
 259                                case 0x221: usage->code = KEY_FIND;             break;
 260                                case 0x223: usage->code = KEY_HOMEPAGE;         break;
 261                                case 0x224: usage->code = KEY_BACK;             break;
 262                                case 0x225: usage->code = KEY_FORWARD;          break;
 263                                case 0x226: usage->code = KEY_STOP;             break;
 264                                case 0x227: usage->code = KEY_REFRESH;          break;
 265                                case 0x22a: usage->code = KEY_BOOKMARKS;        break;
 266
 267                                default:    usage->code = KEY_UNKNOWN;          break;
 268
 269                        }
 270
 271                        usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 272                        break;
 273
 274                default:
 275                unknown:
 276
 277                        if (field->report_size == 1) {
 278
 279                                if (field->report->type == HID_OUTPUT_REPORT) {
 280                                        usage->code = LED_MISC;
 281                                        usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
 282                                        break;
 283                                }
 284
 285                                usage->code = BTN_MISC;
 286                                usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
 287                                break;
 288                        }
 289
 290                        if (field->flags & HID_MAIN_ITEM_RELATIVE) {
 291                                usage->code = REL_MISC;
 292                                usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
 293                                break;
 294                        }
 295
 296                        usage->code = ABS_MISC;
 297                        usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
 298                        break;
 299        }
 300
 301        set_bit(usage->type, input->evbit);
 302
 303        while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
 304                usage->code = find_next_zero_bit(bit, max + 1, usage->code);
 305        }
 306
 307        if (usage->code > max) return;
 308
 309        if (usage->type == EV_ABS) {
 310                int a = field->logical_minimum;
 311                int b = field->logical_maximum;
 312
 313                if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
 314                        a = field->logical_minimum = 0;
 315                        b = field->logical_maximum = 255;
 316                }
 317
 318                input->absmin[usage->code] = a;
 319                input->absmax[usage->code] = b;
 320                input->absfuzz[usage->code] = 0;
 321                input->absflat[usage->code] = 0;
 322
 323                if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK) {
 324                        input->absfuzz[usage->code] = (b - a) >> 8;
 325                        input->absflat[usage->code] = (b - a) >> 4;
 326                }
 327        }
 328
 329        if (usage->hat_min != usage->hat_max) {
 330                int i;
 331                for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
 332                        input->absmax[i] = 1;
 333                        input->absmin[i] = -1;
 334                        input->absfuzz[i] = 0;
 335                        input->absflat[i] = 0;
 336                }
 337                set_bit(usage->code + 1, input->absbit);
 338        }
 339}
 340
 341void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
 342{
 343        struct input_dev *input = find_input(hid, field);
 344        int *quirks = &hid->quirks;
 345
 346        if (!input)
 347                return;
 348
 349        if (usage->hat_min != usage->hat_max) {
 350                value = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
 351                if (value < 0 || value > 8) value = 0;
 352                input_event(input, usage->type, usage->code    , hid_hat_to_axis[value].x);
 353                input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[value].y);
 354                return;
 355        }
 356
 357        if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
 358                *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
 359                return;
 360        }
 361
 362        if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
 363                if (value) {
 364                        input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
 365                        return;
 366                }
 367                input_event(input, usage->type, usage->code, 0);
 368                input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
 369                return;
 370        }
 371
 372        if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
 373                int a = field->logical_minimum;
 374                int b = field->logical_maximum;
 375                input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
 376        }
 377
 378        if((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UKNOWN */
 379                return;
 380
 381        input_event(input, usage->type, usage->code, value);
 382
 383        if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
 384                input_event(input, usage->type, usage->code, 0);
 385}
 386
 387static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
 388{
 389        struct hid_device *hid = dev->private;
 390        struct hid_field *field = NULL;
 391        int offset;
 392
 393        if ((offset = hid_find_field(hid, type, code, &field)) == -1) {
 394                warn("event field not found");
 395                return -1;
 396        }
 397
 398        hid_set_field(field, offset, value);
 399        hid_write_report(hid, field->report);
 400
 401        return 0;
 402}
 403
 404static int hidinput_open(struct input_dev *dev)
 405{
 406        struct hid_device *hid = dev->private;
 407        return hid_open(hid);
 408}
 409
 410static void hidinput_close(struct input_dev *dev)
 411{
 412        struct hid_device *hid = dev->private;
 413        hid_close(hid);
 414}
 415
 416/*
 417 * Register the input device; print a message.
 418 * Configure the input layer interface
 419 * Read all reports and initalize the absoulte field values.
 420 */
 421
 422int hidinput_connect(struct hid_device *hid)
 423{
 424        struct usb_device *dev = hid->dev;
 425        struct hid_report_enum *report_enum;
 426        struct hid_report *report;
 427        struct list_head *list;
 428        struct hid_input *hidinput = NULL;
 429        int i, j, k;
 430
 431        INIT_LIST_HEAD(&hid->inputs);
 432
 433        for (i = 0; i < hid->maxcollection; i++)
 434                if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
 435                    IS_INPUT_APPLICATION(hid->collection[i].usage))
 436                        break;
 437
 438        if (i == hid->maxcollection)
 439                return -1;
 440
 441        for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
 442                report_enum = hid->report_enum + k;
 443                list = report_enum->report_list.next;
 444                while (list != &report_enum->report_list) {
 445                        report = (struct hid_report *) list;
 446
 447                        if (!report->maxfield) {
 448                                list = list->next;
 449                                continue;
 450                        }
 451
 452                        if (!hidinput) {
 453                                hidinput = kmalloc(sizeof(*hidinput), GFP_KERNEL);
 454                                if (!hidinput) {
 455                                        err("Out of memory during hid input probe");
 456                                        return -1;
 457                                }
 458                                memset(hidinput, 0, sizeof(*hidinput));
 459                                list_add_tail(&hidinput->list, &hid->inputs);
 460
 461                                hidinput->input.private = hid;
 462                                hidinput->input.event = hidinput_input_event;
 463                                hidinput->input.open = hidinput_open;
 464                                hidinput->input.close = hidinput_close;
 465
 466                                hidinput->input.name = hid->name;
 467                                hidinput->input.idbus = BUS_USB;
 468                                hidinput->input.idvendor = dev->descriptor.idVendor;
 469                                hidinput->input.idproduct = dev->descriptor.idProduct;
 470                                hidinput->input.idversion = dev->descriptor.bcdDevice;
 471                        }
 472
 473                        for (i = 0; i < report->maxfield; i++)
 474                                for (j = 0; j < report->field[i]->maxusage; j++)
 475                                        hidinput_configure_usage(hidinput, report->field[i],
 476                                                                 report->field[i]->usage + j);
 477
 478                        if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
 479                                /* This will leave hidinput NULL, so that it
 480                                 * allocates another one if we have more inputs on
 481                                 * the same interface. Some devices (e.g. Happ's
 482                                 * UGCI) cram a lot of unrelated inputs into the
 483                                 * same interface. */
 484                                hidinput->report = report;
 485                                input_register_device(&hidinput->input);
 486                                hidinput = NULL;
 487                        }
 488
 489                        list = list->next;
 490                }
 491        }
 492
 493        if (hidinput)
 494                input_register_device(&hidinput->input);
 495
 496        return 0;
 497}
 498
 499void hidinput_disconnect(struct hid_device *hid)
 500{
 501        struct list_head *lh, *next;
 502        struct hid_input *hidinput;
 503
 504        list_for_each_safe (lh, next, &hid->inputs) {
 505                hidinput = list_entry(lh, struct hid_input, list);
 506                input_unregister_device(&hidinput->input);
 507                list_del(&hidinput->list);
 508                kfree(hidinput);
 509        }
 510}
 511
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.