linux/drivers/hid/wacom_wac.c
<<
>>
Prefs
   1/*
   2 * drivers/input/tablet/wacom_wac.c
   3 *
   4 *  USB Wacom tablet support - Wacom specific code
   5 *
   6 */
   7
   8/*
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 */
  14
  15#include "wacom_wac.h"
  16#include "wacom.h"
  17#include <linux/input/mt.h>
  18
  19/* resolution for penabled devices */
  20#define WACOM_PL_RES            20
  21#define WACOM_PENPRTN_RES       40
  22#define WACOM_VOLITO_RES        50
  23#define WACOM_GRAPHIRE_RES      80
  24#define WACOM_INTUOS_RES        100
  25#define WACOM_INTUOS3_RES       200
  26
  27/* Newer Cintiq and DTU have an offset between tablet and screen areas */
  28#define WACOM_DTU_OFFSET        200
  29#define WACOM_CINTIQ_OFFSET     400
  30
  31/*
  32 * Scale factor relating reported contact size to logical contact area.
  33 * 2^14/pi is a good approximation on Intuos5 and 3rd-gen Bamboo
  34 */
  35#define WACOM_CONTACT_AREA_SCALE 2607
  36
  37static bool touch_arbitration = 1;
  38module_param(touch_arbitration, bool, 0644);
  39MODULE_PARM_DESC(touch_arbitration, " on (Y) off (N)");
  40
  41static void wacom_report_numbered_buttons(struct input_dev *input_dev,
  42                                int button_count, int mask);
  43
  44static int wacom_numbered_button_to_key(int n);
  45
  46/*
  47 * Percent of battery capacity for Graphire.
  48 * 8th value means AC online and show 100% capacity.
  49 */
  50static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
  51
  52/*
  53 * Percent of battery capacity for Intuos4 WL, AC has a separate bit.
  54 */
  55static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
  56
  57static void __wacom_notify_battery(struct wacom_battery *battery,
  58                                   int bat_status, int bat_capacity,
  59                                   bool bat_charging, bool bat_connected,
  60                                   bool ps_connected)
  61{
  62        bool changed = battery->bat_status       != bat_status    ||
  63                       battery->battery_capacity != bat_capacity  ||
  64                       battery->bat_charging     != bat_charging  ||
  65                       battery->bat_connected    != bat_connected ||
  66                       battery->ps_connected     != ps_connected;
  67
  68        if (changed) {
  69                battery->bat_status = bat_status;
  70                battery->battery_capacity = bat_capacity;
  71                battery->bat_charging = bat_charging;
  72                battery->bat_connected = bat_connected;
  73                battery->ps_connected = ps_connected;
  74
  75                if (battery->battery)
  76                        power_supply_changed(battery->battery);
  77        }
  78}
  79
  80static void wacom_notify_battery(struct wacom_wac *wacom_wac,
  81        int bat_status, int bat_capacity, bool bat_charging,
  82        bool bat_connected, bool ps_connected)
  83{
  84        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
  85
  86        __wacom_notify_battery(&wacom->battery, bat_status, bat_capacity,
  87                               bat_charging, bat_connected, ps_connected);
  88}
  89
  90static int wacom_penpartner_irq(struct wacom_wac *wacom)
  91{
  92        unsigned char *data = wacom->data;
  93        struct input_dev *input = wacom->pen_input;
  94
  95        switch (data[0]) {
  96        case 1:
  97                if (data[5] & 0x80) {
  98                        wacom->tool[0] = (data[5] & 0x20) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  99                        wacom->id[0] = (data[5] & 0x20) ? ERASER_DEVICE_ID : STYLUS_DEVICE_ID;
 100                        input_report_key(input, wacom->tool[0], 1);
 101                        input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
 102                        input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
 103                        input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
 104                        input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
 105                        input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -127));
 106                        input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
 107                } else {
 108                        input_report_key(input, wacom->tool[0], 0);
 109                        input_report_abs(input, ABS_MISC, 0); /* report tool id */
 110                        input_report_abs(input, ABS_PRESSURE, -1);
 111                        input_report_key(input, BTN_TOUCH, 0);
 112                }
 113                break;
 114
 115        case 2:
 116                input_report_key(input, BTN_TOOL_PEN, 1);
 117                input_report_abs(input, ABS_MISC, STYLUS_DEVICE_ID); /* report tool id */
 118                input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
 119                input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
 120                input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
 121                input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
 122                input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
 123                break;
 124
 125        default:
 126                dev_dbg(input->dev.parent,
 127                        "%s: received unknown report #%d\n", __func__, data[0]);
 128                return 0;
 129        }
 130
 131        return 1;
 132}
 133
 134static int wacom_pl_irq(struct wacom_wac *wacom)
 135{
 136        struct wacom_features *features = &wacom->features;
 137        unsigned char *data = wacom->data;
 138        struct input_dev *input = wacom->pen_input;
 139        int prox, pressure;
 140
 141        if (data[0] != WACOM_REPORT_PENABLED) {
 142                dev_dbg(input->dev.parent,
 143                        "%s: received unknown report #%d\n", __func__, data[0]);
 144                return 0;
 145        }
 146
 147        prox = data[1] & 0x40;
 148
 149        if (!wacom->id[0]) {
 150                if ((data[0] & 0x10) || (data[4] & 0x20)) {
 151                        wacom->tool[0] = BTN_TOOL_RUBBER;
 152                        wacom->id[0] = ERASER_DEVICE_ID;
 153                }
 154                else {
 155                        wacom->tool[0] = BTN_TOOL_PEN;
 156                        wacom->id[0] = STYLUS_DEVICE_ID;
 157                }
 158        }
 159
 160        /* If the eraser is in prox, STYLUS2 is always set. If we
 161         * mis-detected the type and notice that STYLUS2 isn't set
 162         * then force the eraser out of prox and let the pen in.
 163         */
 164        if (wacom->tool[0] == BTN_TOOL_RUBBER && !(data[4] & 0x20)) {
 165                input_report_key(input, BTN_TOOL_RUBBER, 0);
 166                input_report_abs(input, ABS_MISC, 0);
 167                input_sync(input);
 168                wacom->tool[0] = BTN_TOOL_PEN;
 169                wacom->id[0] = STYLUS_DEVICE_ID;
 170        }
 171
 172        if (prox) {
 173                pressure = (signed char)((data[7] << 1) | ((data[4] >> 2) & 1));
 174                if (features->pressure_max > 255)
 175                        pressure = (pressure << 1) | ((data[4] >> 6) & 1);
 176                pressure += (features->pressure_max + 1) / 2;
 177
 178                input_report_abs(input, ABS_X, data[3] | (data[2] << 7) | ((data[1] & 0x03) << 14));
 179                input_report_abs(input, ABS_Y, data[6] | (data[5] << 7) | ((data[4] & 0x03) << 14));
 180                input_report_abs(input, ABS_PRESSURE, pressure);
 181
 182                input_report_key(input, BTN_TOUCH, data[4] & 0x08);
 183                input_report_key(input, BTN_STYLUS, data[4] & 0x10);
 184                /* Only allow the stylus2 button to be reported for the pen tool. */
 185                input_report_key(input, BTN_STYLUS2, (wacom->tool[0] == BTN_TOOL_PEN) && (data[4] & 0x20));
 186        }
 187
 188        if (!prox)
 189                wacom->id[0] = 0;
 190        input_report_key(input, wacom->tool[0], prox);
 191        input_report_abs(input, ABS_MISC, wacom->id[0]);
 192        return 1;
 193}
 194
 195static int wacom_ptu_irq(struct wacom_wac *wacom)
 196{
 197        unsigned char *data = wacom->data;
 198        struct input_dev *input = wacom->pen_input;
 199
 200        if (data[0] != WACOM_REPORT_PENABLED) {
 201                dev_dbg(input->dev.parent,
 202                        "%s: received unknown report #%d\n", __func__, data[0]);
 203                return 0;
 204        }
 205
 206        if (data[1] & 0x04) {
 207                input_report_key(input, BTN_TOOL_RUBBER, data[1] & 0x20);
 208                input_report_key(input, BTN_TOUCH, data[1] & 0x08);
 209                wacom->id[0] = ERASER_DEVICE_ID;
 210        } else {
 211                input_report_key(input, BTN_TOOL_PEN, data[1] & 0x20);
 212                input_report_key(input, BTN_TOUCH, data[1] & 0x01);
 213                wacom->id[0] = STYLUS_DEVICE_ID;
 214        }
 215        input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
 216        input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
 217        input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
 218        input_report_abs(input, ABS_PRESSURE, le16_to_cpup((__le16 *)&data[6]));
 219        input_report_key(input, BTN_STYLUS, data[1] & 0x02);
 220        input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
 221        return 1;
 222}
 223
 224static int wacom_dtu_irq(struct wacom_wac *wacom)
 225{
 226        unsigned char *data = wacom->data;
 227        struct input_dev *input = wacom->pen_input;
 228        int prox = data[1] & 0x20;
 229
 230        dev_dbg(input->dev.parent,
 231                "%s: received report #%d", __func__, data[0]);
 232
 233        if (prox) {
 234                /* Going into proximity select tool */
 235                wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
 236                if (wacom->tool[0] == BTN_TOOL_PEN)
 237                        wacom->id[0] = STYLUS_DEVICE_ID;
 238                else
 239                        wacom->id[0] = ERASER_DEVICE_ID;
 240        }
 241        input_report_key(input, BTN_STYLUS, data[1] & 0x02);
 242        input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
 243        input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
 244        input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
 245        input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x01) << 8) | data[6]);
 246        input_report_key(input, BTN_TOUCH, data[1] & 0x05);
 247        if (!prox) /* out-prox */
 248                wacom->id[0] = 0;
 249        input_report_key(input, wacom->tool[0], prox);
 250        input_report_abs(input, ABS_MISC, wacom->id[0]);
 251        return 1;
 252}
 253
 254static int wacom_dtus_irq(struct wacom_wac *wacom)
 255{
 256        char *data = wacom->data;
 257        struct input_dev *input = wacom->pen_input;
 258        unsigned short prox, pressure = 0;
 259
 260        if (data[0] != WACOM_REPORT_DTUS && data[0] != WACOM_REPORT_DTUSPAD) {
 261                dev_dbg(input->dev.parent,
 262                        "%s: received unknown report #%d", __func__, data[0]);
 263                return 0;
 264        } else if (data[0] == WACOM_REPORT_DTUSPAD) {
 265                input = wacom->pad_input;
 266                input_report_key(input, BTN_0, (data[1] & 0x01));
 267                input_report_key(input, BTN_1, (data[1] & 0x02));
 268                input_report_key(input, BTN_2, (data[1] & 0x04));
 269                input_report_key(input, BTN_3, (data[1] & 0x08));
 270                input_report_abs(input, ABS_MISC,
 271                                 data[1] & 0x0f ? PAD_DEVICE_ID : 0);
 272                return 1;
 273        } else {
 274                prox = data[1] & 0x80;
 275                if (prox) {
 276                        switch ((data[1] >> 3) & 3) {
 277                        case 1: /* Rubber */
 278                                wacom->tool[0] = BTN_TOOL_RUBBER;
 279                                wacom->id[0] = ERASER_DEVICE_ID;
 280                                break;
 281
 282                        case 2: /* Pen */
 283                                wacom->tool[0] = BTN_TOOL_PEN;
 284                                wacom->id[0] = STYLUS_DEVICE_ID;
 285                                break;
 286                        }
 287                }
 288
 289                input_report_key(input, BTN_STYLUS, data[1] & 0x20);
 290                input_report_key(input, BTN_STYLUS2, data[1] & 0x40);
 291                input_report_abs(input, ABS_X, get_unaligned_be16(&data[3]));
 292                input_report_abs(input, ABS_Y, get_unaligned_be16(&data[5]));
 293                pressure = ((data[1] & 0x03) << 8) | (data[2] & 0xff);
 294                input_report_abs(input, ABS_PRESSURE, pressure);
 295                input_report_key(input, BTN_TOUCH, pressure > 10);
 296
 297                if (!prox) /* out-prox */
 298                        wacom->id[0] = 0;
 299                input_report_key(input, wacom->tool[0], prox);
 300                input_report_abs(input, ABS_MISC, wacom->id[0]);
 301                return 1;
 302        }
 303}
 304
 305static int wacom_graphire_irq(struct wacom_wac *wacom)
 306{
 307        struct wacom_features *features = &wacom->features;
 308        unsigned char *data = wacom->data;
 309        struct input_dev *input = wacom->pen_input;
 310        struct input_dev *pad_input = wacom->pad_input;
 311        int battery_capacity, ps_connected;
 312        int prox;
 313        int rw = 0;
 314        int retval = 0;
 315
 316        if (features->type == GRAPHIRE_BT) {
 317                if (data[0] != WACOM_REPORT_PENABLED_BT) {
 318                        dev_dbg(input->dev.parent,
 319                                "%s: received unknown report #%d\n", __func__,
 320                                data[0]);
 321                        goto exit;
 322                }
 323        } else if (data[0] != WACOM_REPORT_PENABLED) {
 324                dev_dbg(input->dev.parent,
 325                        "%s: received unknown report #%d\n", __func__, data[0]);
 326                goto exit;
 327        }
 328
 329        prox = data[1] & 0x80;
 330        if (prox || wacom->id[0]) {
 331                if (prox) {
 332                        switch ((data[1] >> 5) & 3) {
 333
 334                        case 0: /* Pen */
 335                                wacom->tool[0] = BTN_TOOL_PEN;
 336                                wacom->id[0] = STYLUS_DEVICE_ID;
 337                                break;
 338
 339                        case 1: /* Rubber */
 340                                wacom->tool[0] = BTN_TOOL_RUBBER;
 341                                wacom->id[0] = ERASER_DEVICE_ID;
 342                                break;
 343
 344                        case 2: /* Mouse with wheel */
 345                                input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
 346                                /* fall through */
 347
 348                        case 3: /* Mouse without wheel */
 349                                wacom->tool[0] = BTN_TOOL_MOUSE;
 350                                wacom->id[0] = CURSOR_DEVICE_ID;
 351                                break;
 352                        }
 353                }
 354                input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
 355                input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
 356                if (wacom->tool[0] != BTN_TOOL_MOUSE) {
 357                        if (features->type == GRAPHIRE_BT)
 358                                input_report_abs(input, ABS_PRESSURE, data[6] |
 359                                        (((__u16) (data[1] & 0x08)) << 5));
 360                        else
 361                                input_report_abs(input, ABS_PRESSURE, data[6] |
 362                                        ((data[7] & 0x03) << 8));
 363                        input_report_key(input, BTN_TOUCH, data[1] & 0x01);
 364                        input_report_key(input, BTN_STYLUS, data[1] & 0x02);
 365                        input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
 366                } else {
 367                        input_report_key(input, BTN_LEFT, data[1] & 0x01);
 368                        input_report_key(input, BTN_RIGHT, data[1] & 0x02);
 369                        if (features->type == WACOM_G4 ||
 370                                        features->type == WACOM_MO) {
 371                                input_report_abs(input, ABS_DISTANCE, data[6] & 0x3f);
 372                                rw = (data[7] & 0x04) - (data[7] & 0x03);
 373                        } else if (features->type == GRAPHIRE_BT) {
 374                                /* Compute distance between mouse and tablet */
 375                                rw = 44 - (data[6] >> 2);
 376                                rw = clamp_val(rw, 0, 31);
 377                                input_report_abs(input, ABS_DISTANCE, rw);
 378                                if (((data[1] >> 5) & 3) == 2) {
 379                                        /* Mouse with wheel */
 380                                        input_report_key(input, BTN_MIDDLE,
 381                                                        data[1] & 0x04);
 382                                        rw = (data[6] & 0x01) ? -1 :
 383                                                (data[6] & 0x02) ? 1 : 0;
 384                                } else {
 385                                        rw = 0;
 386                                }
 387                        } else {
 388                                input_report_abs(input, ABS_DISTANCE, data[7] & 0x3f);
 389                                rw = -(signed char)data[6];
 390                        }
 391                        input_report_rel(input, REL_WHEEL, rw);
 392                }
 393
 394                if (!prox)
 395                        wacom->id[0] = 0;
 396                input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
 397                input_report_key(input, wacom->tool[0], prox);
 398                input_sync(input); /* sync last event */
 399        }
 400
 401        /* send pad data */
 402        switch (features->type) {
 403        case WACOM_G4:
 404                prox = data[7] & 0xf8;
 405                if (prox || wacom->id[1]) {
 406                        wacom->id[1] = PAD_DEVICE_ID;
 407                        input_report_key(pad_input, BTN_BACK, (data[7] & 0x40));
 408                        input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x80));
 409                        rw = ((data[7] & 0x18) >> 3) - ((data[7] & 0x20) >> 3);
 410                        input_report_rel(pad_input, REL_WHEEL, rw);
 411                        if (!prox)
 412                                wacom->id[1] = 0;
 413                        input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
 414                        retval = 1;
 415                }
 416                break;
 417
 418        case WACOM_MO:
 419                prox = (data[7] & 0xf8) || data[8];
 420                if (prox || wacom->id[1]) {
 421                        wacom->id[1] = PAD_DEVICE_ID;
 422                        input_report_key(pad_input, BTN_BACK, (data[7] & 0x08));
 423                        input_report_key(pad_input, BTN_LEFT, (data[7] & 0x20));
 424                        input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x10));
 425                        input_report_key(pad_input, BTN_RIGHT, (data[7] & 0x40));
 426                        input_report_abs(pad_input, ABS_WHEEL, (data[8] & 0x7f));
 427                        if (!prox)
 428                                wacom->id[1] = 0;
 429                        input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
 430                        retval = 1;
 431                }
 432                break;
 433        case GRAPHIRE_BT:
 434                prox = data[7] & 0x03;
 435                if (prox || wacom->id[1]) {
 436                        wacom->id[1] = PAD_DEVICE_ID;
 437                        input_report_key(pad_input, BTN_0, (data[7] & 0x02));
 438                        input_report_key(pad_input, BTN_1, (data[7] & 0x01));
 439                        if (!prox)
 440                                wacom->id[1] = 0;
 441                        input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
 442                        retval = 1;
 443                }
 444                break;
 445        }
 446
 447        /* Store current battery capacity and power supply state */
 448        if (features->type == GRAPHIRE_BT) {
 449                rw = (data[7] >> 2 & 0x07);
 450                battery_capacity = batcap_gr[rw];
 451                ps_connected = rw == 7;
 452                wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
 453                                     battery_capacity, ps_connected, 1,
 454                                     ps_connected);
 455        }
 456exit:
 457        return retval;
 458}
 459
 460static void wacom_intuos_schedule_prox_event(struct wacom_wac *wacom_wac)
 461{
 462        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
 463        struct wacom_features *features = &wacom_wac->features;
 464        struct hid_report *r;
 465        struct hid_report_enum *re;
 466
 467        re = &(wacom->hdev->report_enum[HID_FEATURE_REPORT]);
 468        if (features->type == INTUOSHT2)
 469                r = re->report_id_hash[WACOM_REPORT_INTUOSHT2_ID];
 470        else
 471                r = re->report_id_hash[WACOM_REPORT_INTUOS_ID1];
 472        if (r) {
 473                hid_hw_request(wacom->hdev, r, HID_REQ_GET_REPORT);
 474        }
 475}
 476
 477static int wacom_intuos_pad(struct wacom_wac *wacom)
 478{
 479        struct wacom_features *features = &wacom->features;
 480        unsigned char *data = wacom->data;
 481        struct input_dev *input = wacom->pad_input;
 482        int i;
 483        int buttons = 0, nbuttons = features->numbered_buttons;
 484        int keys = 0, nkeys = 0;
 485        int ring1 = 0, ring2 = 0;
 486        int strip1 = 0, strip2 = 0;
 487        bool prox = false;
 488
 489        /* pad packets. Works as a second tool and is always in prox */
 490        if (!(data[0] == WACOM_REPORT_INTUOSPAD || data[0] == WACOM_REPORT_INTUOS5PAD ||
 491              data[0] == WACOM_REPORT_CINTIQPAD))
 492                return 0;
 493
 494        if (features->type >= INTUOS4S && features->type <= INTUOS4L) {
 495                buttons = (data[3] << 1) | (data[2] & 0x01);
 496                ring1 = data[1];
 497        } else if (features->type == DTK) {
 498                buttons = data[6];
 499        } else if (features->type == WACOM_13HD) {
 500                buttons = (data[4] << 1) | (data[3] & 0x01);
 501        } else if (features->type == WACOM_24HD) {
 502                buttons = (data[8] << 8) | data[6];
 503                ring1 = data[1];
 504                ring2 = data[2];
 505
 506                /*
 507                 * Three "buttons" are available on the 24HD which are
 508                 * physically implemented as a touchstrip. Each button
 509                 * is approximately 3 bits wide with a 2 bit spacing.
 510                 * The raw touchstrip bits are stored at:
 511                 *    ((data[3] & 0x1f) << 8) | data[4])
 512                 */
 513                nkeys = 3;
 514                keys = ((data[3] & 0x1C) ? 1<<2 : 0) |
 515                       ((data[4] & 0xE0) ? 1<<1 : 0) |
 516                       ((data[4] & 0x07) ? 1<<0 : 0);
 517        } else if (features->type == WACOM_27QHD) {
 518                nkeys = 3;
 519                keys = data[2] & 0x07;
 520
 521                input_report_abs(input, ABS_X, be16_to_cpup((__be16 *)&data[4]));
 522                input_report_abs(input, ABS_Y, be16_to_cpup((__be16 *)&data[6]));
 523                input_report_abs(input, ABS_Z, be16_to_cpup((__be16 *)&data[8]));
 524        } else if (features->type == CINTIQ_HYBRID) {
 525                /*
 526                 * Do not send hardware buttons under Android. They
 527                 * are already sent to the system through GPIO (and
 528                 * have different meaning).
 529                 *
 530                 * d-pad right  -> data[4] & 0x10
 531                 * d-pad up     -> data[4] & 0x20
 532                 * d-pad left   -> data[4] & 0x40
 533                 * d-pad down   -> data[4] & 0x80
 534                 * d-pad center -> data[3] & 0x01
 535                 */
 536                buttons = (data[4] << 1) | (data[3] & 0x01);
 537        } else if (features->type == CINTIQ_COMPANION_2) {
 538                /* d-pad right  -> data[4] & 0x10
 539                 * d-pad up     -> data[4] & 0x20
 540                 * d-pad left   -> data[4] & 0x40
 541                 * d-pad down   -> data[4] & 0x80
 542                 * d-pad center -> data[3] & 0x01
 543                 */
 544                buttons = ((data[2] >> 4) << 7) |
 545                          ((data[1] & 0x04) << 6) |
 546                          ((data[2] & 0x0F) << 2) |
 547                          (data[1] & 0x03);
 548        } else if (features->type >= INTUOS5S && features->type <= INTUOSPL) {
 549                /*
 550                 * ExpressKeys on Intuos5/Intuos Pro have a capacitive sensor in
 551                 * addition to the mechanical switch. Switch data is
 552                 * stored in data[4], capacitive data in data[5].
 553                 *
 554                 * Touch ring mode switch (data[3]) has no capacitive sensor
 555                 */
 556                buttons = (data[4] << 1) | (data[3] & 0x01);
 557                ring1 = data[2];
 558        } else {
 559                if (features->type == WACOM_21UX2 || features->type == WACOM_22HD) {
 560                        buttons = (data[8] << 10) | ((data[7] & 0x01) << 9) |
 561                                  (data[6] << 1) | (data[5] & 0x01);
 562
 563                        if (features->type == WACOM_22HD) {
 564                                nkeys = 3;
 565                                keys = data[9] & 0x07;
 566                        }
 567                } else {
 568                        buttons = ((data[6] & 0x10) << 5)  |
 569                                  ((data[5] & 0x10) << 4)  |
 570                                  ((data[6] & 0x0F) << 4)  |
 571                                  (data[5] & 0x0F);
 572                }
 573                strip1 = ((data[1] & 0x1f) << 8) | data[2];
 574                strip2 = ((data[3] & 0x1f) << 8) | data[4];
 575        }
 576
 577        prox = (buttons & ~(~0 << nbuttons)) | (keys & ~(~0 << nkeys)) |
 578               (ring1 & 0x80) | (ring2 & 0x80) | strip1 | strip2;
 579
 580        wacom_report_numbered_buttons(input, nbuttons, buttons);
 581
 582        for (i = 0; i < nkeys; i++)
 583                input_report_key(input, KEY_PROG1 + i, keys & (1 << i));
 584
 585        input_report_abs(input, ABS_RX, strip1);
 586        input_report_abs(input, ABS_RY, strip2);
 587
 588        input_report_abs(input, ABS_WHEEL,    (ring1 & 0x80) ? (ring1 & 0x7f) : 0);
 589        input_report_abs(input, ABS_THROTTLE, (ring2 & 0x80) ? (ring2 & 0x7f) : 0);
 590
 591        input_report_key(input, wacom->tool[1], prox ? 1 : 0);
 592        input_report_abs(input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
 593
 594        input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
 595
 596        return 1;
 597}
 598
 599static int wacom_intuos_id_mangle(int tool_id)
 600{
 601        return (tool_id & ~0xFFF) << 4 | (tool_id & 0xFFF);
 602}
 603
 604static int wacom_intuos_get_tool_type(int tool_id)
 605{
 606        int tool_type;
 607
 608        switch (tool_id) {
 609        case 0x812: /* Inking pen */
 610        case 0x801: /* Intuos3 Inking pen */
 611        case 0x12802: /* Intuos4/5 Inking Pen */
 612        case 0x012:
 613                tool_type = BTN_TOOL_PENCIL;
 614                break;
 615
 616        case 0x822: /* Pen */
 617        case 0x842:
 618        case 0x852:
 619        case 0x823: /* Intuos3 Grip Pen */
 620        case 0x813: /* Intuos3 Classic Pen */
 621        case 0x885: /* Intuos3 Marker Pen */
 622        case 0x802: /* Intuos4/5 13HD/24HD General Pen */
 623        case 0x804: /* Intuos4/5 13HD/24HD Marker Pen */
 624        case 0x8e2: /* IntuosHT2 pen */
 625        case 0x022:
 626        case 0x10804: /* Intuos4/5 13HD/24HD Art Pen */
 627        case 0x14802: /* Intuos4/5 13HD/24HD Classic Pen */
 628        case 0x16802: /* Cintiq 13HD Pro Pen */
 629        case 0x18802: /* DTH2242 Pen */
 630        case 0x10802: /* Intuos4/5 13HD/24HD General Pen */
 631                tool_type = BTN_TOOL_PEN;
 632                break;
 633
 634        case 0x832: /* Stroke pen */
 635        case 0x032:
 636                tool_type = BTN_TOOL_BRUSH;
 637                break;
 638
 639        case 0x007: /* Mouse 4D and 2D */
 640        case 0x09c:
 641        case 0x094:
 642        case 0x017: /* Intuos3 2D Mouse */
 643        case 0x806: /* Intuos4 Mouse */
 644                tool_type = BTN_TOOL_MOUSE;
 645                break;
 646
 647        case 0x096: /* Lens cursor */
 648        case 0x097: /* Intuos3 Lens cursor */
 649        case 0x006: /* Intuos4 Lens cursor */
 650                tool_type = BTN_TOOL_LENS;
 651                break;
 652
 653        case 0x82a: /* Eraser */
 654        case 0x84a:
 655        case 0x85a:
 656        case 0x91a:
 657        case 0xd1a:
 658        case 0x0fa:
 659        case 0x82b: /* Intuos3 Grip Pen Eraser */
 660        case 0x81b: /* Intuos3 Classic Pen Eraser */
 661        case 0x91b: /* Intuos3 Airbrush Eraser */
 662        case 0x80c: /* Intuos4/5 13HD/24HD Marker Pen Eraser */
 663        case 0x80a: /* Intuos4/5 13HD/24HD General Pen Eraser */
 664        case 0x90a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
 665        case 0x1480a: /* Intuos4/5 13HD/24HD Classic Pen Eraser */
 666        case 0x1090a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
 667        case 0x1080c: /* Intuos4/5 13HD/24HD Art Pen Eraser */
 668        case 0x1680a: /* Cintiq 13HD Pro Pen Eraser */
 669        case 0x1880a: /* DTH2242 Eraser */
 670        case 0x1080a: /* Intuos4/5 13HD/24HD General Pen Eraser */
 671                tool_type = BTN_TOOL_RUBBER;
 672                break;
 673
 674        case 0xd12:
 675        case 0x912:
 676        case 0x112:
 677        case 0x913: /* Intuos3 Airbrush */
 678        case 0x902: /* Intuos4/5 13HD/24HD Airbrush */
 679        case 0x10902: /* Intuos4/5 13HD/24HD Airbrush */
 680                tool_type = BTN_TOOL_AIRBRUSH;
 681                break;
 682
 683        default: /* Unknown tool */
 684                tool_type = BTN_TOOL_PEN;
 685                break;
 686        }
 687        return tool_type;
 688}
 689
 690static int wacom_intuos_inout(struct wacom_wac *wacom)
 691{
 692        struct wacom_features *features = &wacom->features;
 693        unsigned char *data = wacom->data;
 694        struct input_dev *input = wacom->pen_input;
 695        int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
 696
 697        if (!(((data[1] & 0xfc) == 0xc0) ||  /* in prox */
 698            ((data[1] & 0xfe) == 0x20) ||    /* in range */
 699            ((data[1] & 0xfe) == 0x80)))     /* out prox */
 700                return 0;
 701
 702        /* Enter report */
 703        if ((data[1] & 0xfc) == 0xc0) {
 704                /* serial number of the tool */
 705                wacom->serial[idx] = ((data[3] & 0x0f) << 28) +
 706                        (data[4] << 20) + (data[5] << 12) +
 707                        (data[6] << 4) + (data[7] >> 4);
 708
 709                wacom->id[idx] = (data[2] << 4) | (data[3] >> 4) |
 710                     ((data[7] & 0x0f) << 16) | ((data[8] & 0xf0) << 8);
 711
 712                wacom->tool[idx] = wacom_intuos_get_tool_type(wacom->id[idx]);
 713
 714                wacom->shared->stylus_in_proximity = true;
 715                return 1;
 716        }
 717
 718        /* in Range */
 719        if ((data[1] & 0xfe) == 0x20) {
 720                if (features->type != INTUOSHT2)
 721                        wacom->shared->stylus_in_proximity = true;
 722
 723                /* in Range while exiting */
 724                if (wacom->reporting_data) {
 725                        input_report_key(input, BTN_TOUCH, 0);
 726                        input_report_abs(input, ABS_PRESSURE, 0);
 727                        input_report_abs(input, ABS_DISTANCE, wacom->features.distance_max);
 728                        return 2;
 729                }
 730                return 1;
 731        }
 732
 733        /* Exit report */
 734        if ((data[1] & 0xfe) == 0x80) {
 735                wacom->shared->stylus_in_proximity = false;
 736                wacom->reporting_data = false;
 737
 738                /* don't report exit if we don't know the ID */
 739                if (!wacom->id[idx])
 740                        return 1;
 741
 742                /*
 743                 * Reset all states otherwise we lose the initial states
 744                 * when in-prox next time
 745                 */
 746                input_report_abs(input, ABS_X, 0);
 747                input_report_abs(input, ABS_Y, 0);
 748                input_report_abs(input, ABS_DISTANCE, 0);
 749                input_report_abs(input, ABS_TILT_X, 0);
 750                input_report_abs(input, ABS_TILT_Y, 0);
 751                if (wacom->tool[idx] >= BTN_TOOL_MOUSE) {
 752                        input_report_key(input, BTN_LEFT, 0);
 753                        input_report_key(input, BTN_MIDDLE, 0);
 754                        input_report_key(input, BTN_RIGHT, 0);
 755                        input_report_key(input, BTN_SIDE, 0);
 756                        input_report_key(input, BTN_EXTRA, 0);
 757                        input_report_abs(input, ABS_THROTTLE, 0);
 758                        input_report_abs(input, ABS_RZ, 0);
 759                } else {
 760                        input_report_abs(input, ABS_PRESSURE, 0);
 761                        input_report_key(input, BTN_STYLUS, 0);
 762                        input_report_key(input, BTN_STYLUS2, 0);
 763                        input_report_key(input, BTN_TOUCH, 0);
 764                        input_report_abs(input, ABS_WHEEL, 0);
 765                        if (features->type >= INTUOS3S)
 766                                input_report_abs(input, ABS_Z, 0);
 767                }
 768                input_report_key(input, wacom->tool[idx], 0);
 769                input_report_abs(input, ABS_MISC, 0); /* reset tool id */
 770                input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
 771                wacom->id[idx] = 0;
 772                return 2;
 773        }
 774
 775        return 0;
 776}
 777
 778static inline bool report_touch_events(struct wacom_wac *wacom)
 779{
 780        return (touch_arbitration ? !wacom->shared->stylus_in_proximity : 1);
 781}
 782
 783static inline bool delay_pen_events(struct wacom_wac *wacom)
 784{
 785        return (wacom->shared->touch_down && touch_arbitration);
 786}
 787
 788static int wacom_intuos_general(struct wacom_wac *wacom)
 789{
 790        struct wacom_features *features = &wacom->features;
 791        unsigned char *data = wacom->data;
 792        struct input_dev *input = wacom->pen_input;
 793        int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
 794        unsigned char type = (data[1] >> 1) & 0x0F;
 795        unsigned int x, y, distance, t;
 796
 797        if (data[0] != WACOM_REPORT_PENABLED && data[0] != WACOM_REPORT_CINTIQ &&
 798                data[0] != WACOM_REPORT_INTUOS_PEN)
 799                return 0;
 800
 801        if (delay_pen_events(wacom))
 802                return 1;
 803
 804        /* don't report events if we don't know the tool ID */
 805        if (!wacom->id[idx]) {
 806                /* but reschedule a read of the current tool */
 807                wacom_intuos_schedule_prox_event(wacom);
 808                return 1;
 809        }
 810
 811        /*
 812         * don't report events for invalid data
 813         */
 814        /* older I4 styli don't work with new Cintiqs */
 815        if ((!((wacom->id[idx] >> 16) & 0x01) &&
 816                        (features->type == WACOM_21UX2)) ||
 817            /* Only large Intuos support Lense Cursor */
 818            (wacom->tool[idx] == BTN_TOOL_LENS &&
 819                (features->type == INTUOS3 ||
 820                 features->type == INTUOS3S ||
 821                 features->type == INTUOS4 ||
 822                 features->type == INTUOS4S ||
 823                 features->type == INTUOS5 ||
 824                 features->type == INTUOS5S ||
 825                 features->type == INTUOSPM ||
 826                 features->type == INTUOSPS)) ||
 827           /* Cintiq doesn't send data when RDY bit isn't set */
 828           (features->type == CINTIQ && !(data[1] & 0x40)))
 829                return 1;
 830
 831        x = (be16_to_cpup((__be16 *)&data[2]) << 1) | ((data[9] >> 1) & 1);
 832        y = (be16_to_cpup((__be16 *)&data[4]) << 1) | (data[9] & 1);
 833        distance = data[9] >> 2;
 834        if (features->type < INTUOS3S) {
 835                x >>= 1;
 836                y >>= 1;
 837                distance >>= 1;
 838        }
 839        input_report_abs(input, ABS_X, x);
 840        input_report_abs(input, ABS_Y, y);
 841        input_report_abs(input, ABS_DISTANCE, distance);
 842
 843        switch (type) {
 844        case 0x00:
 845        case 0x01:
 846        case 0x02:
 847        case 0x03:
 848                /* general pen packet */
 849                t = (data[6] << 3) | ((data[7] & 0xC0) >> 5) | (data[1] & 1);
 850                if (features->pressure_max < 2047)
 851                        t >>= 1;
 852                input_report_abs(input, ABS_PRESSURE, t);
 853                if (features->type != INTUOSHT2) {
 854                    input_report_abs(input, ABS_TILT_X,
 855                                 (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
 856                    input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
 857                }
 858                input_report_key(input, BTN_STYLUS, data[1] & 2);
 859                input_report_key(input, BTN_STYLUS2, data[1] & 4);
 860                input_report_key(input, BTN_TOUCH, t > 10);
 861                break;
 862
 863        case 0x0a:
 864                /* airbrush second packet */
 865                input_report_abs(input, ABS_WHEEL,
 866                                (data[6] << 2) | ((data[7] >> 6) & 3));
 867                input_report_abs(input, ABS_TILT_X,
 868                                 (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
 869                input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
 870                break;
 871
 872        case 0x05:
 873                /* Rotation packet */
 874                if (features->type >= INTUOS3S) {
 875                        /* I3 marker pen rotation */
 876                        t = (data[6] << 3) | ((data[7] >> 5) & 7);
 877                        t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
 878                                ((t-1) / 2 + 450)) : (450 - t / 2) ;
 879                        input_report_abs(input, ABS_Z, t);
 880                } else {
 881                        /* 4D mouse 2nd packet */
 882                        t = (data[6] << 3) | ((data[7] >> 5) & 7);
 883                        input_report_abs(input, ABS_RZ, (data[7] & 0x20) ?
 884                                ((t - 1) / 2) : -t / 2);
 885                }
 886                break;
 887
 888        case 0x04:
 889                /* 4D mouse 1st packet */
 890                input_report_key(input, BTN_LEFT,   data[8] & 0x01);
 891                input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
 892                input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
 893
 894                input_report_key(input, BTN_SIDE,   data[8] & 0x20);
 895                input_report_key(input, BTN_EXTRA,  data[8] & 0x10);
 896                t = (data[6] << 2) | ((data[7] >> 6) & 3);
 897                input_report_abs(input, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
 898                break;
 899
 900        case 0x06:
 901                /* I4 mouse */
 902                input_report_key(input, BTN_LEFT,   data[6] & 0x01);
 903                input_report_key(input, BTN_MIDDLE, data[6] & 0x02);
 904                input_report_key(input, BTN_RIGHT,  data[6] & 0x04);
 905                input_report_rel(input, REL_WHEEL, ((data[7] & 0x80) >> 7)
 906                                 - ((data[7] & 0x40) >> 6));
 907                input_report_key(input, BTN_SIDE,   data[6] & 0x08);
 908                input_report_key(input, BTN_EXTRA,  data[6] & 0x10);
 909
 910                input_report_abs(input, ABS_TILT_X,
 911                        (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
 912                input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
 913                break;
 914
 915        case 0x08:
 916                if (wacom->tool[idx] == BTN_TOOL_MOUSE) {
 917                        /* 2D mouse packet */
 918                        input_report_key(input, BTN_LEFT,   data[8] & 0x04);
 919                        input_report_key(input, BTN_MIDDLE, data[8] & 0x08);
 920                        input_report_key(input, BTN_RIGHT,  data[8] & 0x10);
 921                        input_report_rel(input, REL_WHEEL, (data[8] & 0x01)
 922                                         - ((data[8] & 0x02) >> 1));
 923
 924                        /* I3 2D mouse side buttons */
 925                        if (features->type >= INTUOS3S && features->type <= INTUOS3L) {
 926                                input_report_key(input, BTN_SIDE,   data[8] & 0x40);
 927                                input_report_key(input, BTN_EXTRA,  data[8] & 0x20);
 928                        }
 929                }
 930                else if (wacom->tool[idx] == BTN_TOOL_LENS) {
 931                        /* Lens cursor packets */
 932                        input_report_key(input, BTN_LEFT,   data[8] & 0x01);
 933                        input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
 934                        input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
 935                        input_report_key(input, BTN_SIDE,   data[8] & 0x10);
 936                        input_report_key(input, BTN_EXTRA,  data[8] & 0x08);
 937                }
 938                break;
 939
 940        case 0x07:
 941        case 0x09:
 942        case 0x0b:
 943        case 0x0c:
 944        case 0x0d:
 945        case 0x0e:
 946        case 0x0f:
 947                /* unhandled */
 948                break;
 949        }
 950
 951        input_report_abs(input, ABS_MISC,
 952                         wacom_intuos_id_mangle(wacom->id[idx])); /* report tool id */
 953        input_report_key(input, wacom->tool[idx], 1);
 954        input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
 955        wacom->reporting_data = true;
 956        return 2;
 957}
 958
 959static int wacom_intuos_irq(struct wacom_wac *wacom)
 960{
 961        unsigned char *data = wacom->data;
 962        struct input_dev *input = wacom->pen_input;
 963        int result;
 964
 965        if (data[0] != WACOM_REPORT_PENABLED &&
 966            data[0] != WACOM_REPORT_INTUOS_ID1 &&
 967            data[0] != WACOM_REPORT_INTUOS_ID2 &&
 968            data[0] != WACOM_REPORT_INTUOSPAD &&
 969            data[0] != WACOM_REPORT_INTUOS_PEN &&
 970            data[0] != WACOM_REPORT_CINTIQ &&
 971            data[0] != WACOM_REPORT_CINTIQPAD &&
 972            data[0] != WACOM_REPORT_INTUOS5PAD) {
 973                dev_dbg(input->dev.parent,
 974                        "%s: received unknown report #%d\n", __func__, data[0]);
 975                return 0;
 976        }
 977
 978        /* process pad events */
 979        result = wacom_intuos_pad(wacom);
 980        if (result)
 981                return result;
 982
 983        /* process in/out prox events */
 984        result = wacom_intuos_inout(wacom);
 985        if (result)
 986                return result - 1;
 987
 988        /* process general packets */
 989        result = wacom_intuos_general(wacom);
 990        if (result)
 991                return result - 1;
 992
 993        return 0;
 994}
 995
 996static int wacom_remote_irq(struct wacom_wac *wacom_wac, size_t len)
 997{
 998        unsigned char *data = wacom_wac->data;
 999        struct input_dev *input;
1000        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1001        struct wacom_remote *remote = wacom->remote;
1002        int bat_charging, bat_percent, touch_ring_mode;
1003        __u32 serial;
1004        int i, index = -1;
1005        unsigned long flags;
1006
1007        if (data[0] != WACOM_REPORT_REMOTE) {
1008                hid_dbg(wacom->hdev, "%s: received unknown report #%d",
1009                        __func__, data[0]);
1010                return 0;
1011        }
1012
1013        serial = data[3] + (data[4] << 8) + (data[5] << 16);
1014        wacom_wac->id[0] = PAD_DEVICE_ID;
1015
1016        spin_lock_irqsave(&remote->remote_lock, flags);
1017
1018        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1019                if (remote->remotes[i].serial == serial) {
1020                        index = i;
1021                        break;
1022                }
1023        }
1024
1025        if (index < 0 || !remote->remotes[index].registered)
1026                goto out;
1027
1028        input = remote->remotes[index].input;
1029
1030        input_report_key(input, BTN_0, (data[9] & 0x01));
1031        input_report_key(input, BTN_1, (data[9] & 0x02));
1032        input_report_key(input, BTN_2, (data[9] & 0x04));
1033        input_report_key(input, BTN_3, (data[9] & 0x08));
1034        input_report_key(input, BTN_4, (data[9] & 0x10));
1035        input_report_key(input, BTN_5, (data[9] & 0x20));
1036        input_report_key(input, BTN_6, (data[9] & 0x40));
1037        input_report_key(input, BTN_7, (data[9] & 0x80));
1038
1039        input_report_key(input, BTN_8, (data[10] & 0x01));
1040        input_report_key(input, BTN_9, (data[10] & 0x02));
1041        input_report_key(input, BTN_A, (data[10] & 0x04));
1042        input_report_key(input, BTN_B, (data[10] & 0x08));
1043        input_report_key(input, BTN_C, (data[10] & 0x10));
1044        input_report_key(input, BTN_X, (data[10] & 0x20));
1045        input_report_key(input, BTN_Y, (data[10] & 0x40));
1046        input_report_key(input, BTN_Z, (data[10] & 0x80));
1047
1048        input_report_key(input, BTN_BASE, (data[11] & 0x01));
1049        input_report_key(input, BTN_BASE2, (data[11] & 0x02));
1050
1051        if (data[12] & 0x80)
1052                input_report_abs(input, ABS_WHEEL, (data[12] & 0x7f));
1053        else
1054                input_report_abs(input, ABS_WHEEL, 0);
1055
1056        bat_percent = data[7] & 0x7f;
1057        bat_charging = !!(data[7] & 0x80);
1058
1059        if (data[9] | data[10] | (data[11] & 0x03) | data[12])
1060                input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
1061        else
1062                input_report_abs(input, ABS_MISC, 0);
1063
1064        input_event(input, EV_MSC, MSC_SERIAL, serial);
1065
1066        input_sync(input);
1067
1068        /*Which mode select (LED light) is currently on?*/
1069        touch_ring_mode = (data[11] & 0xC0) >> 6;
1070
1071        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1072                if (remote->remotes[i].serial == serial)
1073                        wacom->led.groups[i].select = touch_ring_mode;
1074        }
1075
1076        __wacom_notify_battery(&remote->remotes[index].battery,
1077                                WACOM_POWER_SUPPLY_STATUS_AUTO, bat_percent,
1078                                bat_charging, 1, bat_charging);
1079
1080out:
1081        spin_unlock_irqrestore(&remote->remote_lock, flags);
1082        return 0;
1083}
1084
1085static void wacom_remote_status_irq(struct wacom_wac *wacom_wac, size_t len)
1086{
1087        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1088        unsigned char *data = wacom_wac->data;
1089        struct wacom_remote *remote = wacom->remote;
1090        struct wacom_remote_data remote_data;
1091        unsigned long flags;
1092        int i, ret;
1093
1094        if (data[0] != WACOM_REPORT_DEVICE_LIST)
1095                return;
1096
1097        memset(&remote_data, 0, sizeof(struct wacom_remote_data));
1098
1099        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1100                int j = i * 6;
1101                int serial = (data[j+6] << 16) + (data[j+5] << 8) + data[j+4];
1102                bool connected = data[j+2];
1103
1104                remote_data.remote[i].serial = serial;
1105                remote_data.remote[i].connected = connected;
1106        }
1107
1108        spin_lock_irqsave(&remote->remote_lock, flags);
1109
1110        ret = kfifo_in(&remote->remote_fifo, &remote_data, sizeof(remote_data));
1111        if (ret != sizeof(remote_data)) {
1112                spin_unlock_irqrestore(&remote->remote_lock, flags);
1113                hid_err(wacom->hdev, "Can't queue Remote status event.\n");
1114                return;
1115        }
1116
1117        spin_unlock_irqrestore(&remote->remote_lock, flags);
1118
1119        wacom_schedule_work(wacom_wac, WACOM_WORKER_REMOTE);
1120}
1121
1122static int int_dist(int x1, int y1, int x2, int y2)
1123{
1124        int x = x2 - x1;
1125        int y = y2 - y1;
1126
1127        return int_sqrt(x*x + y*y);
1128}
1129
1130static void wacom_intuos_bt_process_data(struct wacom_wac *wacom,
1131                unsigned char *data)
1132{
1133        memcpy(wacom->data, data, 10);
1134        wacom_intuos_irq(wacom);
1135
1136        input_sync(wacom->pen_input);
1137        if (wacom->pad_input)
1138                input_sync(wacom->pad_input);
1139}
1140
1141static int wacom_intuos_bt_irq(struct wacom_wac *wacom, size_t len)
1142{
1143        unsigned char data[WACOM_PKGLEN_MAX];
1144        int i = 1;
1145        unsigned power_raw, battery_capacity, bat_charging, ps_connected;
1146
1147        memcpy(data, wacom->data, len);
1148
1149        switch (data[0]) {
1150        case 0x04:
1151                wacom_intuos_bt_process_data(wacom, data + i);
1152                i += 10;
1153                /* fall through */
1154        case 0x03:
1155                wacom_intuos_bt_process_data(wacom, data + i);
1156                i += 10;
1157                wacom_intuos_bt_process_data(wacom, data + i);
1158                i += 10;
1159                power_raw = data[i];
1160                bat_charging = (power_raw & 0x08) ? 1 : 0;
1161                ps_connected = (power_raw & 0x10) ? 1 : 0;
1162                battery_capacity = batcap_i4[power_raw & 0x07];
1163                wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1164                                     battery_capacity, bat_charging,
1165                                     battery_capacity || bat_charging,
1166                                     ps_connected);
1167                break;
1168        default:
1169                dev_dbg(wacom->pen_input->dev.parent,
1170                                "Unknown report: %d,%d size:%zu\n",
1171                                data[0], data[1], len);
1172                return 0;
1173        }
1174        return 0;
1175}
1176
1177static int wacom_wac_finger_count_touches(struct wacom_wac *wacom)
1178{
1179        struct input_dev *input = wacom->touch_input;
1180        unsigned touch_max = wacom->features.touch_max;
1181        int count = 0;
1182        int i;
1183
1184        if (!touch_max)
1185                return 0;
1186
1187        if (touch_max == 1)
1188                return test_bit(BTN_TOUCH, input->key) &&
1189                        report_touch_events(wacom);
1190
1191        for (i = 0; i < input->mt->num_slots; i++) {
1192                struct input_mt_slot *ps = &input->mt->slots[i];
1193                int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
1194                if (id >= 0)
1195                        count++;
1196        }
1197
1198        return count;
1199}
1200
1201static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
1202{
1203        const int pen_frame_len = 14;
1204        const int pen_frames = 7;
1205
1206        struct input_dev *pen_input = wacom->pen_input;
1207        unsigned char *data = wacom->data;
1208        int i;
1209
1210        wacom->serial[0] = get_unaligned_le64(&data[99]);
1211        wacom->id[0]     = get_unaligned_le16(&data[107]);
1212        if (wacom->serial[0] >> 52 == 1) {
1213                /* Add back in missing bits of ID for non-USI pens */
1214                wacom->id[0] |= (wacom->serial[0] >> 32) & 0xFFFFF;
1215        }
1216        wacom->tool[0]   = wacom_intuos_get_tool_type(wacom_intuos_id_mangle(wacom->id[0]));
1217
1218        for (i = 0; i < pen_frames; i++) {
1219                unsigned char *frame = &data[i*pen_frame_len + 1];
1220                bool valid = frame[0] & 0x80;
1221                bool prox = frame[0] & 0x40;
1222                bool range = frame[0] & 0x20;
1223
1224                if (!valid)
1225                        continue;
1226
1227                if (range) {
1228                        /* Fix rotation alignment: userspace expects zero at left */
1229                        int16_t rotation = (int16_t)get_unaligned_le16(&frame[9]);
1230                        rotation += 1800/4;
1231                        if (rotation > 899)
1232                                rotation -= 1800;
1233
1234                        input_report_abs(pen_input, ABS_X, get_unaligned_le16(&frame[1]));
1235                        input_report_abs(pen_input, ABS_Y, get_unaligned_le16(&frame[3]));
1236                        input_report_abs(pen_input, ABS_TILT_X, (char)frame[7]);
1237                        input_report_abs(pen_input, ABS_TILT_Y, (char)frame[8]);
1238                        input_report_abs(pen_input, ABS_Z, rotation);
1239                        input_report_abs(pen_input, ABS_WHEEL, get_unaligned_le16(&frame[11]));
1240                }
1241                input_report_abs(pen_input, ABS_PRESSURE, get_unaligned_le16(&frame[5]));
1242                input_report_abs(pen_input, ABS_DISTANCE, range ? frame[13] : wacom->features.distance_max);
1243
1244                input_report_key(pen_input, BTN_TOUCH, frame[0] & 0x01);
1245                input_report_key(pen_input, BTN_STYLUS, frame[0] & 0x02);
1246                input_report_key(pen_input, BTN_STYLUS2, frame[0] & 0x04);
1247
1248                input_report_key(pen_input, wacom->tool[0], prox);
1249                input_event(pen_input, EV_MSC, MSC_SERIAL, wacom->serial[0]);
1250                input_report_abs(pen_input, ABS_MISC,
1251                                 wacom_intuos_id_mangle(wacom->id[0])); /* report tool id */
1252
1253                wacom->shared->stylus_in_proximity = prox;
1254
1255                input_sync(pen_input);
1256        }
1257}
1258
1259static void wacom_intuos_pro2_bt_touch(struct wacom_wac *wacom)
1260{
1261        const int finger_touch_len = 8;
1262        const int finger_frames = 4;
1263        const int finger_frame_len = 43;
1264
1265        struct input_dev *touch_input = wacom->touch_input;
1266        unsigned char *data = wacom->data;
1267        int num_contacts_left = 5;
1268        int i, j;
1269
1270        for (i = 0; i < finger_frames; i++) {
1271                unsigned char *frame = &data[i*finger_frame_len + 109];
1272                int current_num_contacts = frame[0] & 0x7F;
1273                int contacts_to_send;
1274
1275                if (!(frame[0] & 0x80))
1276                        continue;
1277
1278                /*
1279                 * First packet resets the counter since only the first
1280                 * packet in series will have non-zero current_num_contacts.
1281                 */
1282                if (current_num_contacts)
1283                        wacom->num_contacts_left = current_num_contacts;
1284
1285                contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1286
1287                for (j = 0; j < contacts_to_send; j++) {
1288                        unsigned char *touch = &frame[j*finger_touch_len + 1];
1289                        int slot = input_mt_get_slot_by_key(touch_input, touch[0]);
1290                        int x = get_unaligned_le16(&touch[2]);
1291                        int y = get_unaligned_le16(&touch[4]);
1292                        int w = touch[6] * input_abs_get_res(touch_input, ABS_MT_POSITION_X);
1293                        int h = touch[7] * input_abs_get_res(touch_input, ABS_MT_POSITION_Y);
1294
1295                        if (slot < 0)
1296                                continue;
1297
1298                        input_mt_slot(touch_input, slot);
1299                        input_mt_report_slot_state(touch_input, MT_TOOL_FINGER, touch[1] & 0x01);
1300                        input_report_abs(touch_input, ABS_MT_POSITION_X, x);
1301                        input_report_abs(touch_input, ABS_MT_POSITION_Y, y);
1302                        input_report_abs(touch_input, ABS_MT_TOUCH_MAJOR, max(w, h));
1303                        input_report_abs(touch_input, ABS_MT_TOUCH_MINOR, min(w, h));
1304                        input_report_abs(touch_input, ABS_MT_ORIENTATION, w > h);
1305                }
1306
1307                input_mt_sync_frame(touch_input);
1308
1309                wacom->num_contacts_left -= contacts_to_send;
1310                if (wacom->num_contacts_left <= 0) {
1311                        wacom->num_contacts_left = 0;
1312                        wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1313                }
1314        }
1315
1316        input_report_switch(touch_input, SW_MUTE_DEVICE, !(data[281] >> 7));
1317        input_sync(touch_input);
1318}
1319
1320static void wacom_intuos_pro2_bt_pad(struct wacom_wac *wacom)
1321{
1322        struct input_dev *pad_input = wacom->pad_input;
1323        unsigned char *data = wacom->data;
1324
1325        int buttons = (data[282] << 1) | ((data[281] >> 6) & 0x01);
1326        int ring = data[285] & 0x7F;
1327        bool ringstatus = data[285] & 0x80;
1328        bool prox = buttons || ringstatus;
1329
1330        /* Fix touchring data: userspace expects 0 at left and increasing clockwise */
1331        ring = 71 - ring;
1332        ring += 3*72/16;
1333        if (ring > 71)
1334                ring -= 72;
1335
1336        wacom_report_numbered_buttons(pad_input, 9, buttons);
1337
1338        input_report_abs(pad_input, ABS_WHEEL, ringstatus ? ring : 0);
1339
1340        input_report_key(pad_input, wacom->tool[1], prox ? 1 : 0);
1341        input_report_abs(pad_input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
1342        input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1343
1344        input_sync(pad_input);
1345}
1346
1347static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
1348{
1349        unsigned char *data = wacom->data;
1350
1351        bool chg = data[284] & 0x80;
1352        int battery_status = data[284] & 0x7F;
1353
1354        wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1355                             battery_status, chg, 1, chg);
1356}
1357
1358static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
1359{
1360        unsigned char *data = wacom->data;
1361
1362        if (data[0] != 0x80) {
1363                dev_dbg(wacom->pen_input->dev.parent,
1364                        "%s: received unknown report #%d\n", __func__, data[0]);
1365                return 0;
1366        }
1367
1368        wacom_intuos_pro2_bt_pen(wacom);
1369        wacom_intuos_pro2_bt_touch(wacom);
1370        wacom_intuos_pro2_bt_pad(wacom);
1371        wacom_intuos_pro2_bt_battery(wacom);
1372        return 0;
1373}
1374
1375static int wacom_24hdt_irq(struct wacom_wac *wacom)
1376{
1377        struct input_dev *input = wacom->touch_input;
1378        unsigned char *data = wacom->data;
1379        int i;
1380        int current_num_contacts = data[61];
1381        int contacts_to_send = 0;
1382        int num_contacts_left = 4; /* maximum contacts per packet */
1383        int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1384        int y_offset = 2;
1385
1386        if (wacom->features.type == WACOM_27QHDT) {
1387                current_num_contacts = data[63];
1388                num_contacts_left = 10;
1389                byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1390                y_offset = 0;
1391        }
1392
1393        /*
1394         * First packet resets the counter since only the first
1395         * packet in series will have non-zero current_num_contacts.
1396         */
1397        if (current_num_contacts)
1398                wacom->num_contacts_left = current_num_contacts;
1399
1400        contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1401
1402        for (i = 0; i < contacts_to_send; i++) {
1403                int offset = (byte_per_packet * i) + 1;
1404                bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1405                int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
1406
1407                if (slot < 0)
1408                        continue;
1409                input_mt_slot(input, slot);
1410                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1411
1412                if (touch) {
1413                        int t_x = get_unaligned_le16(&data[offset + 2]);
1414                        int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
1415
1416                        input_report_abs(input, ABS_MT_POSITION_X, t_x);
1417                        input_report_abs(input, ABS_MT_POSITION_Y, t_y);
1418
1419                        if (wacom->features.type != WACOM_27QHDT) {
1420                                int c_x = get_unaligned_le16(&data[offset + 4]);
1421                                int c_y = get_unaligned_le16(&data[offset + 8]);
1422                                int w = get_unaligned_le16(&data[offset + 10]);
1423                                int h = get_unaligned_le16(&data[offset + 12]);
1424
1425                                input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1426                                input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1427                                                 min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1428                                input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1429                                input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1430                        }
1431                }
1432        }
1433        input_mt_sync_frame(input);
1434
1435        wacom->num_contacts_left -= contacts_to_send;
1436        if (wacom->num_contacts_left <= 0) {
1437                wacom->num_contacts_left = 0;
1438                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1439        }
1440        return 1;
1441}
1442
1443static int wacom_mt_touch(struct wacom_wac *wacom)
1444{
1445        struct input_dev *input = wacom->touch_input;
1446        unsigned char *data = wacom->data;
1447        int i;
1448        int current_num_contacts = data[2];
1449        int contacts_to_send = 0;
1450        int x_offset = 0;
1451
1452        /* MTTPC does not support Height and Width */
1453        if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
1454                x_offset = -4;
1455
1456        /*
1457         * First packet resets the counter since only the first
1458         * packet in series will have non-zero current_num_contacts.
1459         */
1460        if (current_num_contacts)
1461                wacom->num_contacts_left = current_num_contacts;
1462
1463        /* There are at most 5 contacts per packet */
1464        contacts_to_send = min(5, wacom->num_contacts_left);
1465
1466        for (i = 0; i < contacts_to_send; i++) {
1467                int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1468                bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1469                int id = get_unaligned_le16(&data[offset + 1]);
1470                int slot = input_mt_get_slot_by_key(input, id);
1471
1472                if (slot < 0)
1473                        continue;
1474
1475                input_mt_slot(input, slot);
1476                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1477                if (touch) {
1478                        int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1479                        int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1480                        input_report_abs(input, ABS_MT_POSITION_X, x);
1481                        input_report_abs(input, ABS_MT_POSITION_Y, y);
1482                }
1483        }
1484        input_mt_sync_frame(input);
1485
1486        wacom->num_contacts_left -= contacts_to_send;
1487        if (wacom->num_contacts_left <= 0) {
1488                wacom->num_contacts_left = 0;
1489                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1490        }
1491        return 1;
1492}
1493
1494static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1495{
1496        struct input_dev *input = wacom->touch_input;
1497        unsigned char *data = wacom->data;
1498        int i;
1499
1500        for (i = 0; i < 2; i++) {
1501                int p = data[1] & (1 << i);
1502                bool touch = p && report_touch_events(wacom);
1503
1504                input_mt_slot(input, i);
1505                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1506                if (touch) {
1507                        int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1508                        int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1509
1510                        input_report_abs(input, ABS_MT_POSITION_X, x);
1511                        input_report_abs(input, ABS_MT_POSITION_Y, y);
1512                }
1513        }
1514        input_mt_sync_frame(input);
1515
1516        /* keep touch state for pen event */
1517        wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1518
1519        return 1;
1520}
1521
1522static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
1523{
1524        unsigned char *data = wacom->data;
1525        struct input_dev *input = wacom->touch_input;
1526        bool prox = report_touch_events(wacom);
1527        int x = 0, y = 0;
1528
1529        if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1530                return 0;
1531
1532        if (len == WACOM_PKGLEN_TPC1FG) {
1533                prox = prox && (data[0] & 0x01);
1534                x = get_unaligned_le16(&data[1]);
1535                y = get_unaligned_le16(&data[3]);
1536        } else if (len == WACOM_PKGLEN_TPC1FG_B) {
1537                prox = prox && (data[2] & 0x01);
1538                x = get_unaligned_le16(&data[3]);
1539                y = get_unaligned_le16(&data[5]);
1540        } else {
1541                prox = prox && (data[1] & 0x01);
1542                x = le16_to_cpup((__le16 *)&data[2]);
1543                y = le16_to_cpup((__le16 *)&data[4]);
1544        }
1545
1546        if (prox) {
1547                input_report_abs(input, ABS_X, x);
1548                input_report_abs(input, ABS_Y, y);
1549        }
1550        input_report_key(input, BTN_TOUCH, prox);
1551
1552        /* keep touch state for pen events */
1553        wacom->shared->touch_down = prox;
1554
1555        return 1;
1556}
1557
1558static int wacom_tpc_pen(struct wacom_wac *wacom)
1559{
1560        unsigned char *data = wacom->data;
1561        struct input_dev *input = wacom->pen_input;
1562        bool prox = data[1] & 0x20;
1563
1564        if (!wacom->shared->stylus_in_proximity) /* first in prox */
1565                /* Going into proximity select tool */
1566                wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1567
1568        /* keep pen state for touch events */
1569        wacom->shared->stylus_in_proximity = prox;
1570
1571        /* send pen events only when touch is up or forced out
1572         * or touch arbitration is off
1573         */
1574        if (!delay_pen_events(wacom)) {
1575                input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1576                input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1577                input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1578                input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
1579                input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
1580                input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1581                input_report_key(input, wacom->tool[0], prox);
1582                return 1;
1583        }
1584
1585        return 0;
1586}
1587
1588static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1589{
1590        unsigned char *data = wacom->data;
1591
1592        if (wacom->pen_input) {
1593                dev_dbg(wacom->pen_input->dev.parent,
1594                        "%s: received report #%d\n", __func__, data[0]);
1595
1596                if (len == WACOM_PKGLEN_PENABLED ||
1597                    data[0] == WACOM_REPORT_PENABLED)
1598                        return wacom_tpc_pen(wacom);
1599        }
1600        else if (wacom->touch_input) {
1601                dev_dbg(wacom->touch_input->dev.parent,
1602                        "%s: received report #%d\n", __func__, data[0]);
1603
1604                switch (len) {
1605                case WACOM_PKGLEN_TPC1FG:
1606                        return wacom_tpc_single_touch(wacom, len);
1607
1608                case WACOM_PKGLEN_TPC2FG:
1609                        return wacom_tpc_mt_touch(wacom);
1610
1611                default:
1612                        switch (data[0]) {
1613                        case WACOM_REPORT_TPC1FG:
1614                        case WACOM_REPORT_TPCHID:
1615                        case WACOM_REPORT_TPCST:
1616                        case WACOM_REPORT_TPC1FGE:
1617                                return wacom_tpc_single_touch(wacom, len);
1618
1619                        case WACOM_REPORT_TPCMT:
1620                        case WACOM_REPORT_TPCMT2:
1621                                return wacom_mt_touch(wacom);
1622
1623                        }
1624                }
1625        }
1626
1627        return 0;
1628}
1629
1630static int wacom_offset_rotation(struct input_dev *input, struct hid_usage *usage,
1631                                 int value, int num, int denom)
1632{
1633        struct input_absinfo *abs = &input->absinfo[usage->code];
1634        int range = (abs->maximum - abs->minimum + 1);
1635
1636        value += num*range/denom;
1637        if (value > abs->maximum)
1638                value -= range;
1639        else if (value < abs->minimum)
1640                value += range;
1641        return value;
1642}
1643
1644int wacom_equivalent_usage(int usage)
1645{
1646        if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1647                int subpage = (usage & 0xFF00) << 8;
1648                int subusage = (usage & 0xFF);
1649
1650                if (subpage == WACOM_HID_SP_PAD ||
1651                    subpage == WACOM_HID_SP_BUTTON ||
1652                    subpage == WACOM_HID_SP_DIGITIZER ||
1653                    subpage == WACOM_HID_SP_DIGITIZERINFO ||
1654                    usage == WACOM_HID_WD_SENSE ||
1655                    usage == WACOM_HID_WD_SERIALHI ||
1656                    usage == WACOM_HID_WD_TOOLTYPE ||
1657                    usage == WACOM_HID_WD_DISTANCE ||
1658                    usage == WACOM_HID_WD_TOUCHSTRIP ||
1659                    usage == WACOM_HID_WD_TOUCHSTRIP2 ||
1660                    usage == WACOM_HID_WD_TOUCHRING ||
1661                    usage == WACOM_HID_WD_TOUCHRINGSTATUS) {
1662                        return usage;
1663                }
1664
1665                if (subpage == HID_UP_UNDEFINED)
1666                        subpage = HID_UP_DIGITIZER;
1667
1668                return subpage | subusage;
1669        }
1670
1671        if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1672                int subpage = (usage & 0xFF00) << 8;
1673                int subusage = (usage & 0xFF);
1674
1675                if (subpage == HID_UP_UNDEFINED)
1676                        subpage = WACOM_HID_SP_DIGITIZER;
1677
1678                return subpage | subusage;
1679        }
1680
1681        return usage;
1682}
1683
1684static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1685                struct hid_field *field, __u8 type, __u16 code, int fuzz)
1686{
1687        struct wacom *wacom = input_get_drvdata(input);
1688        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1689        struct wacom_features *features = &wacom_wac->features;
1690        int fmin = field->logical_minimum;
1691        int fmax = field->logical_maximum;
1692        unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
1693        int resolution_code = code;
1694
1695        if (equivalent_usage == HID_DG_TWIST) {
1696                resolution_code = ABS_RZ;
1697        }
1698
1699        if (equivalent_usage == HID_GD_X) {
1700                fmin += features->offset_left;
1701                fmax -= features->offset_right;
1702        }
1703        if (equivalent_usage == HID_GD_Y) {
1704                fmin += features->offset_top;
1705                fmax -= features->offset_bottom;
1706        }
1707
1708        usage->type = type;
1709        usage->code = code;
1710
1711        set_bit(type, input->evbit);
1712
1713        switch (type) {
1714        case EV_ABS:
1715                input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1716                input_abs_set_res(input, code,
1717                                  hidinput_calc_abs_res(field, resolution_code));
1718                break;
1719        case EV_KEY:
1720                input_set_capability(input, EV_KEY, code);
1721                break;
1722        case EV_MSC:
1723                input_set_capability(input, EV_MSC, code);
1724                break;
1725        case EV_SW:
1726                input_set_capability(input, EV_SW, code);
1727                break;
1728        }
1729}
1730
1731static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
1732                struct hid_field *field, struct hid_usage *usage)
1733{
1734        struct wacom *wacom = hid_get_drvdata(hdev);
1735        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1736        struct wacom_features *features = &wacom_wac->features;
1737        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1738
1739        switch (equivalent_usage) {
1740        case HID_DG_BATTERYSTRENGTH:
1741        case WACOM_HID_WD_BATTERY_LEVEL:
1742        case WACOM_HID_WD_BATTERY_CHARGING:
1743                features->quirks |= WACOM_QUIRK_BATTERY;
1744                break;
1745        }
1746}
1747
1748static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1749                struct hid_usage *usage, __s32 value)
1750{
1751        struct wacom *wacom = hid_get_drvdata(hdev);
1752        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1753        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1754
1755        switch (equivalent_usage) {
1756        case HID_DG_BATTERYSTRENGTH:
1757                if (value == 0) {
1758                        wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1759                }
1760                else {
1761                        value = value * 100 / (field->logical_maximum - field->logical_minimum);
1762                        wacom_wac->hid_data.battery_capacity = value;
1763                        wacom_wac->hid_data.bat_connected = 1;
1764                        wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1765                }
1766                break;
1767        case WACOM_HID_WD_BATTERY_LEVEL:
1768                value = value * 100 / (field->logical_maximum - field->logical_minimum);
1769                wacom_wac->hid_data.battery_capacity = value;
1770                wacom_wac->hid_data.bat_connected = 1;
1771                wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1772                break;
1773        case WACOM_HID_WD_BATTERY_CHARGING:
1774                wacom_wac->hid_data.bat_charging = value;
1775                wacom_wac->hid_data.ps_connected = value;
1776                wacom_wac->hid_data.bat_connected = 1;
1777                wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1778                break;
1779        }
1780}
1781
1782static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1783                struct hid_report *report)
1784{
1785        return;
1786}
1787
1788static void wacom_wac_battery_report(struct hid_device *hdev,
1789                struct hid_report *report)
1790{
1791        struct wacom *wacom = hid_get_drvdata(hdev);
1792        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1793        struct wacom_features *features = &wacom_wac->features;
1794
1795        if (features->quirks & WACOM_QUIRK_BATTERY) {
1796                int status = wacom_wac->hid_data.bat_status;
1797                int capacity = wacom_wac->hid_data.battery_capacity;
1798                bool charging = wacom_wac->hid_data.bat_charging;
1799                bool connected = wacom_wac->hid_data.bat_connected;
1800                bool powered = wacom_wac->hid_data.ps_connected;
1801
1802                wacom_notify_battery(wacom_wac, status, capacity, charging,
1803                                     connected, powered);
1804        }
1805}
1806
1807static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1808                struct hid_field *field, struct hid_usage *usage)
1809{
1810        struct wacom *wacom = hid_get_drvdata(hdev);
1811        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1812        struct wacom_features *features = &wacom_wac->features;
1813        struct input_dev *input = wacom_wac->pad_input;
1814        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1815
1816        switch (equivalent_usage) {
1817        case WACOM_HID_WD_ACCELEROMETER_X:
1818                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1819                wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
1820                features->device_type |= WACOM_DEVICETYPE_PAD;
1821                break;
1822        case WACOM_HID_WD_ACCELEROMETER_Y:
1823                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1824                wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
1825                features->device_type |= WACOM_DEVICETYPE_PAD;
1826                break;
1827        case WACOM_HID_WD_ACCELEROMETER_Z:
1828                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1829                wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
1830                features->device_type |= WACOM_DEVICETYPE_PAD;
1831                break;
1832        case WACOM_HID_WD_BUTTONCENTER:
1833                wacom->generic_has_leds = true;
1834                /* fall through */
1835        case WACOM_HID_WD_BUTTONHOME:
1836        case WACOM_HID_WD_BUTTONUP:
1837        case WACOM_HID_WD_BUTTONDOWN:
1838        case WACOM_HID_WD_BUTTONLEFT:
1839        case WACOM_HID_WD_BUTTONRIGHT:
1840                wacom_map_usage(input, usage, field, EV_KEY,
1841                                wacom_numbered_button_to_key(features->numbered_buttons),
1842                                0);
1843                features->numbered_buttons++;
1844                features->device_type |= WACOM_DEVICETYPE_PAD;
1845                break;
1846        case WACOM_HID_WD_TOUCHONOFF:
1847        case WACOM_HID_WD_MUTE_DEVICE:
1848                /*
1849                 * This usage, which is used to mute touch events, comes
1850                 * from the pad packet, but is reported on the touch
1851                 * interface. Because the touch interface may not have
1852                 * been created yet, we cannot call wacom_map_usage(). In
1853                 * order to process this usage when we receive it, we set
1854                 * the usage type and code directly.
1855                 */
1856                wacom_wac->has_mute_touch_switch = true;
1857                usage->type = EV_SW;
1858                usage->code = SW_MUTE_DEVICE;
1859                features->device_type |= WACOM_DEVICETYPE_PAD;
1860                break;
1861        case WACOM_HID_WD_TOUCHSTRIP:
1862                wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
1863                features->device_type |= WACOM_DEVICETYPE_PAD;
1864                break;
1865        case WACOM_HID_WD_TOUCHSTRIP2:
1866                wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
1867                features->device_type |= WACOM_DEVICETYPE_PAD;
1868                break;
1869        case WACOM_HID_WD_TOUCHRING:
1870                wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
1871                features->device_type |= WACOM_DEVICETYPE_PAD;
1872                break;
1873        case WACOM_HID_WD_TOUCHRINGSTATUS:
1874                /*
1875                 * Only set up type/code association. Completely mapping
1876                 * this usage may overwrite the axis resolution and range.
1877                 */
1878                usage->type = EV_ABS;
1879                usage->code = ABS_WHEEL;
1880                set_bit(EV_ABS, input->evbit);
1881                features->device_type |= WACOM_DEVICETYPE_PAD;
1882                break;
1883        case WACOM_HID_WD_BUTTONCONFIG:
1884                wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
1885                features->device_type |= WACOM_DEVICETYPE_PAD;
1886                break;
1887        case WACOM_HID_WD_ONSCREEN_KEYBOARD:
1888                wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
1889                features->device_type |= WACOM_DEVICETYPE_PAD;
1890                break;
1891        case WACOM_HID_WD_CONTROLPANEL:
1892                wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
1893                features->device_type |= WACOM_DEVICETYPE_PAD;
1894                break;
1895        case WACOM_HID_WD_MODE_CHANGE:
1896                /* do not overwrite previous data */
1897                if (!wacom_wac->has_mode_change) {
1898                        wacom_wac->has_mode_change = true;
1899                        wacom_wac->is_direct_mode = true;
1900                }
1901                features->device_type |= WACOM_DEVICETYPE_PAD;
1902                break;
1903        }
1904
1905        switch (equivalent_usage & 0xfffffff0) {
1906        case WACOM_HID_WD_EXPRESSKEY00:
1907                wacom_map_usage(input, usage, field, EV_KEY,
1908                                wacom_numbered_button_to_key(features->numbered_buttons),
1909                                0);
1910                features->numbered_buttons++;
1911                features->device_type |= WACOM_DEVICETYPE_PAD;
1912                break;
1913        }
1914}
1915
1916static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
1917                struct hid_usage *usage, __s32 value)
1918{
1919        struct wacom *wacom = hid_get_drvdata(hdev);
1920        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1921        struct input_dev *input = wacom_wac->pad_input;
1922        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1923        bool do_report = false;
1924
1925        /*
1926         * Avoid reporting this event and setting inrange_state if this usage
1927         * hasn't been mapped.
1928         */
1929        if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
1930                return;
1931
1932        if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
1933                if (usage->hid != WACOM_HID_WD_TOUCHRING)
1934                        wacom_wac->hid_data.inrange_state |= value;
1935        }
1936
1937        switch (equivalent_usage) {
1938        case WACOM_HID_WD_TOUCHRING:
1939                /*
1940                 * Userspace expects touchrings to increase in value with
1941                 * clockwise gestures and have their zero point at the
1942                 * tablet's left. HID events "should" be clockwise-
1943                 * increasing and zero at top, though the MobileStudio
1944                 * Pro and 2nd-gen Intuos Pro don't do this...
1945                 */
1946                if (hdev->vendor == 0x56a &&
1947                    (hdev->product == 0x34d || hdev->product == 0x34e ||  /* MobileStudio Pro */
1948                     hdev->product == 0x357 || hdev->product == 0x358)) { /* Intuos Pro 2 */
1949                        value = (field->logical_maximum - value);
1950
1951                        if (hdev->product == 0x357 || hdev->product == 0x358)
1952                                value = wacom_offset_rotation(input, usage, value, 3, 16);
1953                        else if (hdev->product == 0x34d || hdev->product == 0x34e)
1954                                value = wacom_offset_rotation(input, usage, value, 1, 2);
1955                }
1956                else {
1957                        value = wacom_offset_rotation(input, usage, value, 1, 4);
1958                }
1959                do_report = true;
1960                break;
1961        case WACOM_HID_WD_TOUCHRINGSTATUS:
1962                if (!value)
1963                        input_event(input, usage->type, usage->code, 0);
1964                break;
1965
1966        case WACOM_HID_WD_MUTE_DEVICE:
1967        case WACOM_HID_WD_TOUCHONOFF:
1968                if (wacom_wac->shared->touch_input) {
1969                        bool *is_touch_on = &wacom_wac->shared->is_touch_on;
1970
1971                        if (equivalent_usage == WACOM_HID_WD_MUTE_DEVICE && value)
1972                                *is_touch_on = !(*is_touch_on);
1973                        else if (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)
1974                                *is_touch_on = value;
1975
1976                        input_report_switch(wacom_wac->shared->touch_input,
1977                                            SW_MUTE_DEVICE, !(*is_touch_on));
1978                        input_sync(wacom_wac->shared->touch_input);
1979                }
1980                break;
1981        case WACOM_HID_WD_MODE_CHANGE:
1982                if (wacom_wac->is_direct_mode != value) {
1983                        wacom_wac->is_direct_mode = value;
1984                        wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
1985                }
1986                break;
1987        default:
1988                do_report = true;
1989                break;
1990        }
1991
1992        if (do_report) {
1993                input_event(input, usage->type, usage->code, value);
1994                if (value)
1995                        wacom_wac->hid_data.pad_input_event_flag = true;
1996        }
1997}
1998
1999static void wacom_wac_pad_pre_report(struct hid_device *hdev,
2000                struct hid_report *report)
2001{
2002        struct wacom *wacom = hid_get_drvdata(hdev);
2003        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2004
2005        wacom_wac->hid_data.inrange_state = 0;
2006}
2007
2008static void wacom_wac_pad_report(struct hid_device *hdev,
2009                struct hid_report *report)
2010{
2011        struct wacom *wacom = hid_get_drvdata(hdev);
2012        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2013        struct input_dev *input = wacom_wac->pad_input;
2014        bool active = wacom_wac->hid_data.inrange_state != 0;
2015
2016        /* report prox for expresskey events */
2017        if ((wacom_equivalent_usage(report->field[0]->physical) == HID_DG_TABLETFUNCTIONKEY) &&
2018            wacom_wac->hid_data.pad_input_event_flag) {
2019                input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
2020                input_sync(input);
2021                if (!active)
2022                        wacom_wac->hid_data.pad_input_event_flag = false;
2023        }
2024
2025}
2026
2027static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
2028                struct hid_field *field, struct hid_usage *usage)
2029{
2030        struct wacom *wacom = hid_get_drvdata(hdev);
2031        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2032        struct wacom_features *features = &wacom_wac->features;
2033        struct input_dev *input = wacom_wac->pen_input;
2034        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2035
2036        switch (equivalent_usage) {
2037        case HID_GD_X:
2038                wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2039                break;
2040        case HID_GD_Y:
2041                wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2042                break;
2043        case WACOM_HID_WD_DISTANCE:
2044        case HID_GD_Z:
2045                wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
2046                break;
2047        case HID_DG_TIPPRESSURE:
2048                wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
2049                break;
2050        case HID_DG_INRANGE:
2051                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2052                break;
2053        case HID_DG_INVERT:
2054                wacom_map_usage(input, usage, field, EV_KEY,
2055                                BTN_TOOL_RUBBER, 0);
2056                break;
2057        case HID_DG_TILT_X:
2058                wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
2059                break;
2060        case HID_DG_TILT_Y:
2061                wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
2062                break;
2063        case HID_DG_TWIST:
2064                wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2065                break;
2066        case HID_DG_ERASER:
2067        case HID_DG_TIPSWITCH:
2068                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2069                break;
2070        case HID_DG_BARRELSWITCH:
2071                wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
2072                break;
2073        case HID_DG_BARRELSWITCH2:
2074                wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
2075                break;
2076        case HID_DG_TOOLSERIALNUMBER:
2077                wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
2078                break;
2079        case WACOM_HID_WD_SENSE:
2080                features->quirks |= WACOM_QUIRK_SENSE;
2081                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2082                break;
2083        case WACOM_HID_WD_SERIALHI:
2084                wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
2085                set_bit(EV_KEY, input->evbit);
2086                input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
2087                input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2088                input_set_capability(input, EV_KEY, BTN_TOOL_BRUSH);
2089                input_set_capability(input, EV_KEY, BTN_TOOL_PENCIL);
2090                input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
2091                if (!(features->device_type & WACOM_DEVICETYPE_DIRECT)) {
2092                        input_set_capability(input, EV_KEY, BTN_TOOL_MOUSE);
2093                        input_set_capability(input, EV_KEY, BTN_TOOL_LENS);
2094                }
2095                break;
2096        case WACOM_HID_WD_FINGERWHEEL:
2097                wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2098                break;
2099        }
2100}
2101
2102static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
2103                struct hid_usage *usage, __s32 value)
2104{
2105        struct wacom *wacom = hid_get_drvdata(hdev);
2106        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2107        struct wacom_features *features = &wacom_wac->features;
2108        struct input_dev *input = wacom_wac->pen_input;
2109        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2110
2111        switch (equivalent_usage) {
2112        case HID_GD_Z:
2113                /*
2114                 * HID_GD_Z "should increase as the control's position is
2115                 * moved from high to low", while ABS_DISTANCE instead
2116                 * increases in value as the tool moves from low to high.
2117                 */
2118                value = field->logical_maximum - value;
2119                break;
2120        case HID_DG_INRANGE:
2121                wacom_wac->hid_data.inrange_state = value;
2122                if (!(features->quirks & WACOM_QUIRK_SENSE))
2123                        wacom_wac->hid_data.sense_state = value;
2124                return;
2125        case HID_DG_INVERT:
2126                wacom_wac->hid_data.invert_state = value;
2127                return;
2128        case HID_DG_ERASER:
2129        case HID_DG_TIPSWITCH:
2130                wacom_wac->hid_data.tipswitch |= value;
2131                return;
2132        case HID_DG_BARRELSWITCH:
2133                wacom_wac->hid_data.barrelswitch = value;
2134                return;
2135        case HID_DG_BARRELSWITCH2:
2136                wacom_wac->hid_data.barrelswitch2 = value;
2137                return;
2138        case HID_DG_TOOLSERIALNUMBER:
2139                if (value) {
2140                        wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
2141                        wacom_wac->serial[0] |= (__u32)value;
2142                }
2143                return;
2144        case HID_DG_TWIST:
2145                /*
2146                 * Userspace expects pen twist to have its zero point when
2147                 * the buttons/finger is on the tablet's left. HID values
2148                 * are zero when buttons are toward the top.
2149                 */
2150                value = wacom_offset_rotation(input, usage, value, 1, 4);
2151                break;
2152        case WACOM_HID_WD_SENSE:
2153                wacom_wac->hid_data.sense_state = value;
2154                return;
2155        case WACOM_HID_WD_SERIALHI:
2156                if (value) {
2157                        wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
2158                        wacom_wac->serial[0] |= ((__u64)value) << 32;
2159                        /*
2160                         * Non-USI EMR devices may contain additional tool type
2161                         * information here. See WACOM_HID_WD_TOOLTYPE case for
2162                         * more details.
2163                         */
2164                        if (value >> 20 == 1) {
2165                                wacom_wac->id[0] |= value & 0xFFFFF;
2166                        }
2167                }
2168                return;
2169        case WACOM_HID_WD_TOOLTYPE:
2170                /*
2171                 * Some devices (MobileStudio Pro, and possibly later
2172                 * devices as well) do not return the complete tool
2173                 * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2174                 * bitwise OR so the complete value can be built
2175                 * up over time :(
2176                 */
2177                wacom_wac->id[0] |= value;
2178                return;
2179        case WACOM_HID_WD_OFFSETLEFT:
2180                if (features->offset_left && value != features->offset_left)
2181                        hid_warn(hdev, "%s: overriding existing left offset "
2182                                 "%d -> %d\n", __func__, value,
2183                                 features->offset_left);
2184                features->offset_left = value;
2185                return;
2186        case WACOM_HID_WD_OFFSETRIGHT:
2187                if (features->offset_right && value != features->offset_right)
2188                        hid_warn(hdev, "%s: overriding existing right offset "
2189                                 "%d -> %d\n", __func__, value,
2190                                 features->offset_right);
2191                features->offset_right = value;
2192                return;
2193        case WACOM_HID_WD_OFFSETTOP:
2194                if (features->offset_top && value != features->offset_top)
2195                        hid_warn(hdev, "%s: overriding existing top offset "
2196                                 "%d -> %d\n", __func__, value,
2197                                 features->offset_top);
2198                features->offset_top = value;
2199                return;
2200        case WACOM_HID_WD_OFFSETBOTTOM:
2201                if (features->offset_bottom && value != features->offset_bottom)
2202                        hid_warn(hdev, "%s: overriding existing bottom offset "
2203                                 "%d -> %d\n", __func__, value,
2204                                 features->offset_bottom);
2205                features->offset_bottom = value;
2206                return;
2207        }
2208
2209        /* send pen events only when touch is up or forced out
2210         * or touch arbitration is off
2211         */
2212        if (!usage->type || delay_pen_events(wacom_wac))
2213                return;
2214
2215        /* send pen events only when the pen is in/entering/leaving proximity */
2216        if (!wacom_wac->hid_data.inrange_state && !wacom_wac->tool[0])
2217                return;
2218
2219        input_event(input, usage->type, usage->code, value);
2220}
2221
2222static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2223                struct hid_report *report)
2224{
2225        return;
2226}
2227
2228static void wacom_wac_pen_report(struct hid_device *hdev,
2229                struct hid_report *report)
2230{
2231        struct wacom *wacom = hid_get_drvdata(hdev);
2232        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2233        struct input_dev *input = wacom_wac->pen_input;
2234        bool prox = wacom_wac->hid_data.inrange_state;
2235        bool range = wacom_wac->hid_data.sense_state;
2236
2237        if (!wacom_wac->tool[0] && prox) { /* first in prox */
2238                /* Going into proximity select tool */
2239                if (wacom_wac->hid_data.invert_state)
2240                        wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2241                else if (wacom_wac->id[0])
2242                        wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2243                else
2244                        wacom_wac->tool[0] = BTN_TOOL_PEN;
2245        }
2246
2247        /* keep pen state for touch events */
2248        wacom_wac->shared->stylus_in_proximity = range;
2249
2250        if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
2251                int id = wacom_wac->id[0];
2252                int sw_state = wacom_wac->hid_data.barrelswitch |
2253                               (wacom_wac->hid_data.barrelswitch2 << 1);
2254
2255                input_report_key(input, BTN_STYLUS, sw_state == 1);
2256                input_report_key(input, BTN_STYLUS2, sw_state == 2);
2257                input_report_key(input, BTN_STYLUS3, sw_state == 3);
2258
2259                /*
2260                 * Non-USI EMR tools should have their IDs mangled to
2261                 * match the legacy behavior of wacom_intuos_general
2262                 */
2263                if (wacom_wac->serial[0] >> 52 == 1)
2264                        id = wacom_intuos_id_mangle(id);
2265
2266                /*
2267                 * To ensure compatibility with xf86-input-wacom, we should
2268                 * report the BTN_TOOL_* event prior to the ABS_MISC or
2269                 * MSC_SERIAL events.
2270                 */
2271                input_report_key(input, BTN_TOUCH,
2272                                wacom_wac->hid_data.tipswitch);
2273                input_report_key(input, wacom_wac->tool[0], prox);
2274                if (wacom_wac->serial[0]) {
2275                        input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
2276                        input_report_abs(input, ABS_MISC, prox ? id : 0);
2277                }
2278
2279                wacom_wac->hid_data.tipswitch = false;
2280
2281                input_sync(input);
2282        }
2283
2284        if (!prox) {
2285                wacom_wac->tool[0] = 0;
2286                wacom_wac->id[0] = 0;
2287                wacom_wac->serial[0] = 0;
2288        }
2289}
2290
2291static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2292                struct hid_field *field, struct hid_usage *usage)
2293{
2294        struct wacom *wacom = hid_get_drvdata(hdev);
2295        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2296        struct input_dev *input = wacom_wac->touch_input;
2297        unsigned touch_max = wacom_wac->features.touch_max;
2298        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2299
2300        switch (equivalent_usage) {
2301        case HID_GD_X:
2302                if (touch_max == 1)
2303                        wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2304                else
2305                        wacom_map_usage(input, usage, field, EV_ABS,
2306                                        ABS_MT_POSITION_X, 4);
2307                break;
2308        case HID_GD_Y:
2309                if (touch_max == 1)
2310                        wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2311                else
2312                        wacom_map_usage(input, usage, field, EV_ABS,
2313                                        ABS_MT_POSITION_Y, 4);
2314                break;
2315        case HID_DG_WIDTH:
2316        case HID_DG_HEIGHT:
2317                wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2318                wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2319                input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2320                break;
2321        case HID_DG_TIPSWITCH:
2322                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2323                break;
2324        case HID_DG_CONTACTCOUNT:
2325                wacom_wac->hid_data.cc_report = field->report->id;
2326                wacom_wac->hid_data.cc_index = field->index;
2327                wacom_wac->hid_data.cc_value_index = usage->usage_index;
2328                break;
2329        case HID_DG_CONTACTID:
2330                if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2331                        /*
2332                         * The HID descriptor for G11 sensors leaves logical
2333                         * maximum set to '1' despite it being a multitouch
2334                         * device. Override to a sensible number.
2335                         */
2336                        field->logical_maximum = 255;
2337                }
2338                break;
2339        }
2340}
2341
2342static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2343                struct input_dev *input)
2344{
2345        struct hid_data *hid_data = &wacom_wac->hid_data;
2346        bool mt = wacom_wac->features.touch_max > 1;
2347        bool prox = hid_data->tipswitch &&
2348                    report_touch_events(wacom_wac);
2349
2350        if (wacom_wac->shared->has_mute_touch_switch &&
2351            !wacom_wac->shared->is_touch_on) {
2352                if (!wacom_wac->shared->touch_down)
2353                        return;
2354                prox = 0;
2355        }
2356
2357        wacom_wac->hid_data.num_received++;
2358        if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2359                return;
2360
2361        if (mt) {
2362                int slot;
2363
2364                slot = input_mt_get_slot_by_key(input, hid_data->id);
2365                input_mt_slot(input, slot);
2366                input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2367        }
2368        else {
2369                input_report_key(input, BTN_TOUCH, prox);
2370        }
2371
2372        if (prox) {
2373                input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2374                                 hid_data->x);
2375                input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2376                                 hid_data->y);
2377
2378                if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2379                        input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2380                        input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2381                        if (hid_data->width != hid_data->height)
2382                                input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2383                }
2384        }
2385}
2386
2387static void wacom_wac_finger_event(struct hid_device *hdev,
2388                struct hid_field *field, struct hid_usage *usage, __s32 value)
2389{
2390        struct wacom *wacom = hid_get_drvdata(hdev);
2391        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2392        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2393
2394        switch (equivalent_usage) {
2395        case HID_GD_X:
2396                wacom_wac->hid_data.x = value;
2397                break;
2398        case HID_GD_Y:
2399                wacom_wac->hid_data.y = value;
2400                break;
2401        case HID_DG_WIDTH:
2402                wacom_wac->hid_data.width = value;
2403                break;
2404        case HID_DG_HEIGHT:
2405                wacom_wac->hid_data.height = value;
2406                break;
2407        case HID_DG_CONTACTID:
2408                wacom_wac->hid_data.id = value;
2409                break;
2410        case HID_DG_TIPSWITCH:
2411                wacom_wac->hid_data.tipswitch = value;
2412                break;
2413        }
2414
2415
2416        if (usage->usage_index + 1 == field->report_count) {
2417                if (equivalent_usage == wacom_wac->hid_data.last_slot_field)
2418                        wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2419        }
2420}
2421
2422static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2423                struct hid_report *report)
2424{
2425        struct wacom *wacom = hid_get_drvdata(hdev);
2426        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2427        struct hid_data* hid_data = &wacom_wac->hid_data;
2428        int i;
2429
2430        for (i = 0; i < report->maxfield; i++) {
2431                struct hid_field *field = report->field[i];
2432                int j;
2433
2434                for (j = 0; j < field->maxusage; j++) {
2435                        struct hid_usage *usage = &field->usage[j];
2436                        unsigned int equivalent_usage =
2437                                wacom_equivalent_usage(usage->hid);
2438
2439                        switch (equivalent_usage) {
2440                        case HID_GD_X:
2441                        case HID_GD_Y:
2442                        case HID_DG_WIDTH:
2443                        case HID_DG_HEIGHT:
2444                        case HID_DG_CONTACTID:
2445                        case HID_DG_INRANGE:
2446                        case HID_DG_INVERT:
2447                        case HID_DG_TIPSWITCH:
2448                                hid_data->last_slot_field = equivalent_usage;
2449                                break;
2450                        case HID_DG_CONTACTCOUNT:
2451                                hid_data->cc_report = report->id;
2452                                hid_data->cc_index = i;
2453                                hid_data->cc_value_index = j;
2454                                break;
2455                        }
2456                }
2457        }
2458
2459        if (hid_data->cc_report != 0 &&
2460            hid_data->cc_index >= 0) {
2461                struct hid_field *field = report->field[hid_data->cc_index];
2462                int value = field->value[hid_data->cc_value_index];
2463                if (value)
2464                        hid_data->num_expected = value;
2465        }
2466        else {
2467                hid_data->num_expected = wacom_wac->features.touch_max;
2468        }
2469}
2470
2471static void wacom_wac_finger_report(struct hid_device *hdev,
2472                struct hid_report *report)
2473{
2474        struct wacom *wacom = hid_get_drvdata(hdev);
2475        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2476        struct input_dev *input = wacom_wac->touch_input;
2477        unsigned touch_max = wacom_wac->features.touch_max;
2478
2479        /* If more packets of data are expected, give us a chance to
2480         * process them rather than immediately syncing a partial
2481         * update.
2482         */
2483        if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2484                return;
2485
2486        if (touch_max > 1)
2487                input_mt_sync_frame(input);
2488
2489        input_sync(input);
2490        wacom_wac->hid_data.num_received = 0;
2491
2492        /* keep touch state for pen event */
2493        wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2494}
2495
2496void wacom_wac_usage_mapping(struct hid_device *hdev,
2497                struct hid_field *field, struct hid_usage *usage)
2498{
2499        struct wacom *wacom = hid_get_drvdata(hdev);
2500        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2501        struct wacom_features *features = &wacom_wac->features;
2502
2503        if (WACOM_DIRECT_DEVICE(field))
2504                features->device_type |= WACOM_DEVICETYPE_DIRECT;
2505
2506        /* usage tests must precede field tests */
2507        if (WACOM_BATTERY_USAGE(usage))
2508                wacom_wac_battery_usage_mapping(hdev, field, usage);
2509        else if (WACOM_PAD_FIELD(field))
2510                wacom_wac_pad_usage_mapping(hdev, field, usage);
2511        else if (WACOM_PEN_FIELD(field))
2512                wacom_wac_pen_usage_mapping(hdev, field, usage);
2513        else if (WACOM_FINGER_FIELD(field))
2514                wacom_wac_finger_usage_mapping(hdev, field, usage);
2515}
2516
2517void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2518                struct hid_usage *usage, __s32 value)
2519{
2520        struct wacom *wacom = hid_get_drvdata(hdev);
2521
2522        if (wacom->wacom_wac.features.type != HID_GENERIC)
2523                return;
2524
2525        if (value > field->logical_maximum || value < field->logical_minimum)
2526                return;
2527
2528        /* usage tests must precede field tests */
2529        if (WACOM_BATTERY_USAGE(usage))
2530                wacom_wac_battery_event(hdev, field, usage, value);
2531        else if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2532                wacom_wac_pad_event(hdev, field, usage, value);
2533        else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2534                wacom_wac_pen_event(hdev, field, usage, value);
2535        else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2536                wacom_wac_finger_event(hdev, field, usage, value);
2537}
2538
2539static void wacom_report_events(struct hid_device *hdev, struct hid_report *report)
2540{
2541        int r;
2542
2543        for (r = 0; r < report->maxfield; r++) {
2544                struct hid_field *field;
2545                unsigned count, n;
2546
2547                field = report->field[r];
2548                count = field->report_count;
2549
2550                if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2551                        continue;
2552
2553                for (n = 0; n < count; n++)
2554                        wacom_wac_event(hdev, field, &field->usage[n], field->value[n]);
2555        }
2556}
2557
2558void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2559{
2560        struct wacom *wacom = hid_get_drvdata(hdev);
2561        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2562        struct hid_field *field = report->field[0];
2563
2564        if (wacom_wac->features.type != HID_GENERIC)
2565                return;
2566
2567        wacom_wac_battery_pre_report(hdev, report);
2568
2569        if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2570                wacom_wac_pad_pre_report(hdev, report);
2571        else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2572                wacom_wac_pen_pre_report(hdev, report);
2573        else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2574                wacom_wac_finger_pre_report(hdev, report);
2575
2576        wacom_report_events(hdev, report);
2577
2578        /*
2579         * Non-input reports may be sent prior to the device being
2580         * completely initialized. Since only their events need
2581         * to be processed, exit after 'wacom_report_events' has
2582         * been called to prevent potential crashes in the report-
2583         * processing functions.
2584         */
2585        if (report->type != HID_INPUT_REPORT)
2586                return;
2587
2588        wacom_wac_battery_report(hdev, report);
2589
2590        if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2591                wacom_wac_pad_report(hdev, report);
2592        else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2593                wacom_wac_pen_report(hdev, report);
2594        else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2595                wacom_wac_finger_report(hdev, report);
2596}
2597
2598static int wacom_bpt_touch(struct wacom_wac *wacom)
2599{
2600        struct wacom_features *features = &wacom->features;
2601        struct input_dev *input = wacom->touch_input;
2602        struct input_dev *pad_input = wacom->pad_input;
2603        unsigned char *data = wacom->data;
2604        int i;
2605
2606        if (data[0] != 0x02)
2607            return 0;
2608
2609        for (i = 0; i < 2; i++) {
2610                int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
2611                bool touch = report_touch_events(wacom)
2612                           && (data[offset + 3] & 0x80);
2613
2614                input_mt_slot(input, i);
2615                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2616                if (touch) {
2617                        int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
2618                        int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
2619                        if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
2620                                x <<= 5;
2621                                y <<= 5;
2622                        }
2623                        input_report_abs(input, ABS_MT_POSITION_X, x);
2624                        input_report_abs(input, ABS_MT_POSITION_Y, y);
2625                }
2626        }
2627
2628        input_mt_sync_frame(input);
2629
2630        input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
2631        input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
2632        input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
2633        input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
2634        wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2635
2636        return 1;
2637}
2638
2639static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
2640{
2641        struct wacom_features *features = &wacom->features;
2642        struct input_dev *input = wacom->touch_input;
2643        bool touch = data[1] & 0x80;
2644        int slot = input_mt_get_slot_by_key(input, data[0]);
2645
2646        if (slot < 0)
2647                return;
2648
2649        touch = touch && report_touch_events(wacom);
2650
2651        input_mt_slot(input, slot);
2652        input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2653
2654        if (touch) {
2655                int x = (data[2] << 4) | (data[4] >> 4);
2656                int y = (data[3] << 4) | (data[4] & 0x0f);
2657                int width, height;
2658
2659                if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
2660                        width  = data[5] * 100;
2661                        height = data[6] * 100;
2662                } else {
2663                        /*
2664                         * "a" is a scaled-down area which we assume is
2665                         * roughly circular and which can be described as:
2666                         * a=(pi*r^2)/C.
2667                         */
2668                        int a = data[5];
2669                        int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
2670                        int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
2671                        width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
2672                        height = width * y_res / x_res;
2673                }
2674
2675                input_report_abs(input, ABS_MT_POSITION_X, x);
2676                input_report_abs(input, ABS_MT_POSITION_Y, y);
2677                input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
2678                input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
2679        }
2680}
2681
2682static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
2683{
2684        struct input_dev *input = wacom->pad_input;
2685        struct wacom_features *features = &wacom->features;
2686
2687        if (features->type == INTUOSHT || features->type == INTUOSHT2) {
2688                input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
2689                input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
2690        } else {
2691                input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
2692                input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
2693        }
2694        input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
2695        input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
2696}
2697
2698static int wacom_bpt3_touch(struct wacom_wac *wacom)
2699{
2700        unsigned char *data = wacom->data;
2701        int count = data[1] & 0x07;
2702        int  touch_changed = 0, i;
2703
2704        if (data[0] != 0x02)
2705            return 0;
2706
2707        /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
2708        for (i = 0; i < count; i++) {
2709                int offset = (8 * i) + 2;
2710                int msg_id = data[offset];
2711
2712                if (msg_id >= 2 && msg_id <= 17) {
2713                        wacom_bpt3_touch_msg(wacom, data + offset);
2714                        touch_changed++;
2715                } else if (msg_id == 128)
2716                        wacom_bpt3_button_msg(wacom, data + offset);
2717
2718        }
2719
2720        /* only update touch if we actually have a touchpad and touch data changed */
2721        if (wacom->touch_input && touch_changed) {
2722                input_mt_sync_frame(wacom->touch_input);
2723                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2724        }
2725
2726        return 1;
2727}
2728
2729static int wacom_bpt_pen(struct wacom_wac *wacom)
2730{
2731        struct wacom_features *features = &wacom->features;
2732        struct input_dev *input = wacom->pen_input;
2733        unsigned char *data = wacom->data;
2734        int prox = 0, x = 0, y = 0, p = 0, d = 0, pen = 0, btn1 = 0, btn2 = 0;
2735
2736        if (data[0] != WACOM_REPORT_PENABLED)
2737            return 0;
2738
2739        prox = (data[1] & 0x20) == 0x20;
2740
2741        /*
2742         * All reports shared between PEN and RUBBER tool must be
2743         * forced to a known starting value (zero) when transitioning to
2744         * out-of-prox.
2745         *
2746         * If not reset then, to userspace, it will look like lost events
2747         * if new tool comes in-prox with same values as previous tool sent.
2748         *
2749         * Hardware does report zero in most out-of-prox cases but not all.
2750         */
2751        if (!wacom->shared->stylus_in_proximity) {
2752                if (data[1] & 0x08) {
2753                        wacom->tool[0] = BTN_TOOL_RUBBER;
2754                        wacom->id[0] = ERASER_DEVICE_ID;
2755                } else {
2756                        wacom->tool[0] = BTN_TOOL_PEN;
2757                        wacom->id[0] = STYLUS_DEVICE_ID;
2758                }
2759        }
2760
2761        wacom->shared->stylus_in_proximity = prox;
2762        if (delay_pen_events(wacom))
2763                return 0;
2764
2765        if (prox) {
2766                x = le16_to_cpup((__le16 *)&data[2]);
2767                y = le16_to_cpup((__le16 *)&data[4]);
2768                p = le16_to_cpup((__le16 *)&data[6]);
2769                /*
2770                 * Convert distance from out prox to distance from tablet.
2771                 * distance will be greater than distance_max once
2772                 * touching and applying pressure; do not report negative
2773                 * distance.
2774                 */
2775                if (data[8] <= features->distance_max)
2776                        d = features->distance_max - data[8];
2777
2778                pen = data[1] & 0x01;
2779                btn1 = data[1] & 0x02;
2780                btn2 = data[1] & 0x04;
2781        } else {
2782                wacom->id[0] = 0;
2783        }
2784
2785        input_report_key(input, BTN_TOUCH, pen);
2786        input_report_key(input, BTN_STYLUS, btn1);
2787        input_report_key(input, BTN_STYLUS2, btn2);
2788
2789        input_report_abs(input, ABS_X, x);
2790        input_report_abs(input, ABS_Y, y);
2791        input_report_abs(input, ABS_PRESSURE, p);
2792        input_report_abs(input, ABS_DISTANCE, d);
2793
2794        input_report_key(input, wacom->tool[0], prox); /* PEN or RUBBER */
2795        input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
2796
2797        return 1;
2798}
2799
2800static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
2801{
2802        struct wacom_features *features = &wacom->features;
2803
2804        if ((features->type == INTUOSHT2) &&
2805            (features->device_type & WACOM_DEVICETYPE_PEN))
2806                return wacom_intuos_irq(wacom);
2807        else if (len == WACOM_PKGLEN_BBTOUCH)
2808                return wacom_bpt_touch(wacom);
2809        else if (len == WACOM_PKGLEN_BBTOUCH3)
2810                return wacom_bpt3_touch(wacom);
2811        else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
2812                return wacom_bpt_pen(wacom);
2813
2814        return 0;
2815}
2816
2817static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
2818                unsigned char *data)
2819{
2820        unsigned char prefix;
2821
2822        /*
2823         * We need to reroute the event from the debug interface to the
2824         * pen interface.
2825         * We need to add the report ID to the actual pen report, so we
2826         * temporary overwrite the first byte to prevent having to kzalloc/kfree
2827         * and memcpy the report.
2828         */
2829        prefix = data[0];
2830        data[0] = WACOM_REPORT_BPAD_PEN;
2831
2832        /*
2833         * actually reroute the event.
2834         * No need to check if wacom->shared->pen is valid, hid_input_report()
2835         * will check for us.
2836         */
2837        hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
2838                         WACOM_PKGLEN_PENABLED, 1);
2839
2840        data[0] = prefix;
2841}
2842
2843static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
2844                unsigned char *data)
2845{
2846        struct input_dev *input = wacom->touch_input;
2847        unsigned char *finger_data, prefix;
2848        unsigned id;
2849        int x, y;
2850        bool valid;
2851
2852        prefix = data[0];
2853
2854        for (id = 0; id < wacom->features.touch_max; id++) {
2855                valid = !!(prefix & BIT(id)) &&
2856                        report_touch_events(wacom);
2857
2858                input_mt_slot(input, id);
2859                input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
2860
2861                if (!valid)
2862                        continue;
2863
2864                finger_data = data + 1 + id * 3;
2865                x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
2866                y = (finger_data[2] << 4) | (finger_data[1] >> 4);
2867
2868                input_report_abs(input, ABS_MT_POSITION_X, x);
2869                input_report_abs(input, ABS_MT_POSITION_Y, y);
2870        }
2871
2872        input_mt_sync_frame(input);
2873
2874        input_report_key(input, BTN_LEFT, prefix & 0x40);
2875        input_report_key(input, BTN_RIGHT, prefix & 0x80);
2876
2877        /* keep touch state for pen event */
2878        wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
2879
2880        return 1;
2881}
2882
2883static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
2884{
2885        unsigned char *data = wacom->data;
2886
2887        if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
2888              (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
2889            (data[0] != WACOM_REPORT_BPAD_TOUCH))
2890                return 0;
2891
2892        if (data[1] & 0x01)
2893                wacom_bamboo_pad_pen_event(wacom, &data[1]);
2894
2895        if (data[1] & 0x02)
2896                return wacom_bamboo_pad_touch_event(wacom, &data[9]);
2897
2898        return 0;
2899}
2900
2901static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
2902{
2903        unsigned char *data = wacom->data;
2904        int connected;
2905
2906        if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
2907                return 0;
2908
2909        connected = data[1] & 0x01;
2910        if (connected) {
2911                int pid, battery, charging;
2912
2913                if ((wacom->shared->type == INTUOSHT ||
2914                    wacom->shared->type == INTUOSHT2) &&
2915                    wacom->shared->touch_input &&
2916                    wacom->shared->touch_max) {
2917                        input_report_switch(wacom->shared->touch_input,
2918                                        SW_MUTE_DEVICE, data[5] & 0x40);
2919                        input_sync(wacom->shared->touch_input);
2920                }
2921
2922                pid = get_unaligned_be16(&data[6]);
2923                battery = (data[5] & 0x3f) * 100 / 31;
2924                charging = !!(data[5] & 0x80);
2925                if (wacom->pid != pid) {
2926                        wacom->pid = pid;
2927                        wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2928                }
2929
2930                wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
2931                                     battery, charging, 1, 0);
2932
2933        } else if (wacom->pid != 0) {
2934                /* disconnected while previously connected */
2935                wacom->pid = 0;
2936                wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2937                wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
2938        }
2939
2940        return 0;
2941}
2942
2943static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
2944{
2945        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
2946        struct wacom_features *features = &wacom_wac->features;
2947        unsigned char *data = wacom_wac->data;
2948
2949        if (data[0] != WACOM_REPORT_USB)
2950                return 0;
2951
2952        if ((features->type == INTUOSHT ||
2953            features->type == INTUOSHT2) &&
2954            wacom_wac->shared->touch_input &&
2955            features->touch_max) {
2956                input_report_switch(wacom_wac->shared->touch_input,
2957                                    SW_MUTE_DEVICE, data[8] & 0x40);
2958                input_sync(wacom_wac->shared->touch_input);
2959        }
2960
2961        if (data[9] & 0x02) { /* wireless module is attached */
2962                int battery = (data[8] & 0x3f) * 100 / 31;
2963                bool charging = !!(data[8] & 0x80);
2964
2965                wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
2966                                     battery, charging, battery || charging, 1);
2967
2968                if (!wacom->battery.battery &&
2969                    !(features->quirks & WACOM_QUIRK_BATTERY)) {
2970                        features->quirks |= WACOM_QUIRK_BATTERY;
2971                        wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2972                }
2973        }
2974        else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
2975                 wacom->battery.battery) {
2976                features->quirks &= ~WACOM_QUIRK_BATTERY;
2977                wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2978                wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
2979        }
2980        return 0;
2981}
2982
2983void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
2984{
2985        bool sync;
2986
2987        switch (wacom_wac->features.type) {
2988        case PENPARTNER:
2989                sync = wacom_penpartner_irq(wacom_wac);
2990                break;
2991
2992        case PL:
2993                sync = wacom_pl_irq(wacom_wac);
2994                break;
2995
2996        case WACOM_G4:
2997        case GRAPHIRE:
2998        case GRAPHIRE_BT:
2999        case WACOM_MO:
3000                sync = wacom_graphire_irq(wacom_wac);
3001                break;
3002
3003        case PTU:
3004                sync = wacom_ptu_irq(wacom_wac);
3005                break;
3006
3007        case DTU:
3008                sync = wacom_dtu_irq(wacom_wac);
3009                break;
3010
3011        case DTUS:
3012        case DTUSX:
3013                sync = wacom_dtus_irq(wacom_wac);
3014                break;
3015
3016        case INTUOS:
3017        case INTUOS3S:
3018        case INTUOS3:
3019        case INTUOS3L:
3020        case INTUOS4S:
3021        case INTUOS4:
3022        case INTUOS4L:
3023        case CINTIQ:
3024        case WACOM_BEE:
3025        case WACOM_13HD:
3026        case WACOM_21UX2:
3027        case WACOM_22HD:
3028        case WACOM_24HD:
3029        case WACOM_27QHD:
3030        case DTK:
3031        case CINTIQ_HYBRID:
3032        case CINTIQ_COMPANION_2:
3033                sync = wacom_intuos_irq(wacom_wac);
3034                break;
3035
3036        case INTUOS4WL:
3037                sync = wacom_intuos_bt_irq(wacom_wac, len);
3038                break;
3039
3040        case WACOM_24HDT:
3041        case WACOM_27QHDT:
3042                sync = wacom_24hdt_irq(wacom_wac);
3043                break;
3044
3045        case INTUOS5S:
3046        case INTUOS5:
3047        case INTUOS5L:
3048        case INTUOSPS:
3049        case INTUOSPM:
3050        case INTUOSPL:
3051                if (len == WACOM_PKGLEN_BBTOUCH3)
3052                        sync = wacom_bpt3_touch(wacom_wac);
3053                else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3054                        sync = wacom_status_irq(wacom_wac, len);
3055                else
3056                        sync = wacom_intuos_irq(wacom_wac);
3057                break;
3058
3059        case INTUOSP2_BT:
3060                sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
3061                break;
3062
3063        case TABLETPC:
3064        case TABLETPCE:
3065        case TABLETPC2FG:
3066        case MTSCREEN:
3067        case MTTPC:
3068        case MTTPC_B:
3069                sync = wacom_tpc_irq(wacom_wac, len);
3070                break;
3071
3072        case BAMBOO_PT:
3073        case BAMBOO_PEN:
3074        case BAMBOO_TOUCH:
3075        case INTUOSHT:
3076        case INTUOSHT2:
3077                if (wacom_wac->data[0] == WACOM_REPORT_USB)
3078                        sync = wacom_status_irq(wacom_wac, len);
3079                else
3080                        sync = wacom_bpt_irq(wacom_wac, len);
3081                break;
3082
3083        case BAMBOO_PAD:
3084                sync = wacom_bamboo_pad_irq(wacom_wac, len);
3085                break;
3086
3087        case WIRELESS:
3088                sync = wacom_wireless_irq(wacom_wac, len);
3089                break;
3090
3091        case REMOTE:
3092                sync = false;
3093                if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
3094                        wacom_remote_status_irq(wacom_wac, len);
3095                else
3096                        sync = wacom_remote_irq(wacom_wac, len);
3097                break;
3098
3099        default:
3100                sync = false;
3101                break;
3102        }
3103
3104        if (sync) {
3105                if (wacom_wac->pen_input)
3106                        input_sync(wacom_wac->pen_input);
3107                if (wacom_wac->touch_input)
3108                        input_sync(wacom_wac->touch_input);
3109                if (wacom_wac->pad_input)
3110                        input_sync(wacom_wac->pad_input);
3111        }
3112}
3113
3114static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
3115{
3116        struct input_dev *input_dev = wacom_wac->pen_input;
3117
3118        input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3119
3120        __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3121        __set_bit(BTN_STYLUS, input_dev->keybit);
3122        __set_bit(BTN_STYLUS2, input_dev->keybit);
3123
3124        input_set_abs_params(input_dev, ABS_DISTANCE,
3125                             0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
3126}
3127
3128static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3129{
3130        struct input_dev *input_dev = wacom_wac->pen_input;
3131        struct wacom_features *features = &wacom_wac->features;
3132
3133        wacom_setup_basic_pro_pen(wacom_wac);
3134
3135        __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3136        __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3137        __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3138        __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3139
3140        input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
3141        input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
3142        input_abs_set_res(input_dev, ABS_TILT_X, 57);
3143        input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
3144        input_abs_set_res(input_dev, ABS_TILT_Y, 57);
3145}
3146
3147static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3148{
3149        struct input_dev *input_dev = wacom_wac->pen_input;
3150
3151        input_set_capability(input_dev, EV_REL, REL_WHEEL);
3152
3153        wacom_setup_cintiq(wacom_wac);
3154
3155        __set_bit(BTN_LEFT, input_dev->keybit);
3156        __set_bit(BTN_RIGHT, input_dev->keybit);
3157        __set_bit(BTN_MIDDLE, input_dev->keybit);
3158        __set_bit(BTN_SIDE, input_dev->keybit);
3159        __set_bit(BTN_EXTRA, input_dev->keybit);
3160        __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3161        __set_bit(BTN_TOOL_LENS, input_dev->keybit);
3162
3163        input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
3164        input_abs_set_res(input_dev, ABS_RZ, 287);
3165        input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3166}
3167
3168void wacom_setup_device_quirks(struct wacom *wacom)
3169{
3170        struct wacom_features *features = &wacom->wacom_wac.features;
3171
3172        /* The pen and pad share the same interface on most devices */
3173        if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3174            features->type == DTUS ||
3175            (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
3176                if (features->device_type & WACOM_DEVICETYPE_PEN)
3177                        features->device_type |= WACOM_DEVICETYPE_PAD;
3178        }
3179
3180        /* touch device found but size is not defined. use default */
3181        if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
3182                features->x_max = 1023;
3183                features->y_max = 1023;
3184        }
3185
3186        /*
3187         * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3188         * touch interface in its HID descriptor. If this is the touch
3189         * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3190         * tablet values.
3191         */
3192        if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3193                (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
3194                if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3195                        if (features->touch_max)
3196                                features->device_type |= WACOM_DEVICETYPE_TOUCH;
3197                        if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
3198                                features->device_type |= WACOM_DEVICETYPE_PAD;
3199
3200                        features->x_max = 4096;
3201                        features->y_max = 4096;
3202                }
3203                else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3204                        features->device_type |= WACOM_DEVICETYPE_PAD;
3205                }
3206        }
3207
3208        /*
3209         * Hack for the Bamboo One:
3210         * the device presents a PAD/Touch interface as most Bamboos and even
3211         * sends ghosts PAD data on it. However, later, we must disable this
3212         * ghost interface, and we can not detect it unless we set it here
3213         * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3214         */
3215        if (features->type == BAMBOO_PEN &&
3216            features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3217                features->device_type |= WACOM_DEVICETYPE_PAD;
3218
3219        /*
3220         * Raw Wacom-mode pen and touch events both come from interface
3221         * 0, whose HID descriptor has an application usage of 0xFF0D
3222         * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
3223         * out through the HID_GENERIC device created for interface 1,
3224         * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
3225         */
3226        if (features->type == BAMBOO_PAD)
3227                features->device_type = WACOM_DEVICETYPE_TOUCH;
3228
3229        if (features->type == REMOTE)
3230                features->device_type = WACOM_DEVICETYPE_PAD;
3231
3232        if (features->type == INTUOSP2_BT) {
3233                features->device_type |= WACOM_DEVICETYPE_PEN |
3234                                         WACOM_DEVICETYPE_PAD |
3235                                         WACOM_DEVICETYPE_TOUCH;
3236                features->quirks |= WACOM_QUIRK_BATTERY;
3237        }
3238
3239        switch (features->type) {
3240        case PL:
3241        case DTU:
3242        case DTUS:
3243        case DTUSX:
3244        case WACOM_21UX2:
3245        case WACOM_22HD:
3246        case DTK:
3247        case WACOM_24HD:
3248        case WACOM_27QHD:
3249        case CINTIQ_HYBRID:
3250        case CINTIQ_COMPANION_2:
3251        case CINTIQ:
3252        case WACOM_BEE:
3253        case WACOM_13HD:
3254        case WACOM_24HDT:
3255        case WACOM_27QHDT:
3256        case TABLETPC:
3257        case TABLETPCE:
3258        case TABLETPC2FG:
3259        case MTSCREEN:
3260        case MTTPC:
3261        case MTTPC_B:
3262                features->device_type |= WACOM_DEVICETYPE_DIRECT;
3263                break;
3264        }
3265
3266        if (wacom->hdev->bus == BUS_BLUETOOTH)
3267                features->quirks |= WACOM_QUIRK_BATTERY;
3268
3269        /* quirk for bamboo touch with 2 low res touches */
3270        if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
3271            features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3272                features->x_max <<= 5;
3273                features->y_max <<= 5;
3274                features->x_fuzz <<= 5;
3275                features->y_fuzz <<= 5;
3276                features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
3277        }
3278
3279        if (features->type == WIRELESS) {
3280                if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
3281                        features->quirks |= WACOM_QUIRK_BATTERY;
3282                }
3283        }
3284
3285        if (features->type == REMOTE)
3286                features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
3287}
3288
3289int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
3290                                   struct wacom_wac *wacom_wac)
3291{
3292        struct wacom_features *features = &wacom_wac->features;
3293
3294        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3295
3296        if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3297                return -ENODEV;
3298
3299        if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3300                __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3301        else
3302                __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3303
3304        if (features->type == HID_GENERIC) {
3305                /* setup has already been done; apply otherwise-undetectible quirks */
3306                input_set_capability(input_dev, EV_KEY, BTN_STYLUS3);
3307                return 0;
3308        }
3309
3310        __set_bit(BTN_TOUCH, input_dev->keybit);
3311        __set_bit(ABS_MISC, input_dev->absbit);
3312
3313        input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3314                             features->x_max - features->offset_right,
3315                             features->x_fuzz, 0);
3316        input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3317                             features->y_max - features->offset_bottom,
3318                             features->y_fuzz, 0);
3319        input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3320                features->pressure_max, features->pressure_fuzz, 0);
3321
3322        /* penabled devices have fixed resolution for each model */
3323        input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3324        input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3325
3326        switch (features->type) {
3327        case GRAPHIRE_BT:
3328                __clear_bit(ABS_MISC, input_dev->absbit);
3329
3330        case WACOM_MO:
3331        case WACOM_G4:
3332                input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3333                                              features->distance_max,
3334                                              features->distance_fuzz, 0);
3335                /* fall through */
3336
3337        case GRAPHIRE:
3338                input_set_capability(input_dev, EV_REL, REL_WHEEL);
3339
3340                __set_bit(BTN_LEFT, input_dev->keybit);
3341                __set_bit(BTN_RIGHT, input_dev->keybit);
3342                __set_bit(BTN_MIDDLE, input_dev->keybit);
3343
3344                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3345                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3346                __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3347                __set_bit(BTN_STYLUS, input_dev->keybit);
3348                __set_bit(BTN_STYLUS2, input_dev->keybit);
3349                break;
3350
3351        case WACOM_27QHD:
3352        case WACOM_24HD:
3353        case DTK:
3354        case WACOM_22HD:
3355        case WACOM_21UX2:
3356        case WACOM_BEE:
3357        case CINTIQ:
3358        case WACOM_13HD:
3359        case CINTIQ_HYBRID:
3360        case CINTIQ_COMPANION_2:
3361                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3362                input_abs_set_res(input_dev, ABS_Z, 287);
3363                wacom_setup_cintiq(wacom_wac);
3364                break;
3365
3366        case INTUOS3:
3367        case INTUOS3L:
3368        case INTUOS3S:
3369        case INTUOS4:
3370        case INTUOS4WL:
3371        case INTUOS4L:
3372        case INTUOS4S:
3373                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3374                input_abs_set_res(input_dev, ABS_Z, 287);
3375                /* fall through */
3376
3377        case INTUOS:
3378                wacom_setup_intuos(wacom_wac);
3379                break;
3380
3381        case INTUOS5:
3382        case INTUOS5L:
3383        case INTUOSPM:
3384        case INTUOSPL:
3385        case INTUOS5S:
3386        case INTUOSPS:
3387        case INTUOSP2_BT:
3388                input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3389                                      features->distance_max,
3390                                      features->distance_fuzz, 0);
3391
3392                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3393                input_abs_set_res(input_dev, ABS_Z, 287);
3394
3395                wacom_setup_intuos(wacom_wac);
3396                break;
3397
3398        case WACOM_24HDT:
3399        case WACOM_27QHDT:
3400        case MTSCREEN:
3401        case MTTPC:
3402        case MTTPC_B:
3403        case TABLETPC2FG:
3404        case TABLETPC:
3405        case TABLETPCE:
3406                __clear_bit(ABS_MISC, input_dev->absbit);
3407                /* fall through */
3408
3409        case DTUS:
3410        case DTUSX:
3411        case PL:
3412        case DTU:
3413                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3414                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3415                __set_bit(BTN_STYLUS, input_dev->keybit);
3416                __set_bit(BTN_STYLUS2, input_dev->keybit);
3417                break;
3418
3419        case PTU:
3420                __set_bit(BTN_STYLUS2, input_dev->keybit);
3421                /* fall through */
3422
3423        case PENPARTNER:
3424                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3425                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3426                __set_bit(BTN_STYLUS, input_dev->keybit);
3427                break;
3428
3429        case INTUOSHT:
3430        case BAMBOO_PT:
3431        case BAMBOO_PEN:
3432        case INTUOSHT2:
3433                if (features->type == INTUOSHT2) {
3434                        wacom_setup_basic_pro_pen(wacom_wac);
3435                } else {
3436                        __clear_bit(ABS_MISC, input_dev->absbit);
3437                        __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3438                        __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3439                        __set_bit(BTN_STYLUS, input_dev->keybit);
3440                        __set_bit(BTN_STYLUS2, input_dev->keybit);
3441                        input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3442                                      features->distance_max,
3443                                      features->distance_fuzz, 0);
3444                }
3445                break;
3446        case BAMBOO_PAD:
3447                __clear_bit(ABS_MISC, input_dev->absbit);
3448                break;
3449        }
3450        return 0;
3451}
3452
3453int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3454                                         struct wacom_wac *wacom_wac)
3455{
3456        struct wacom_features *features = &wacom_wac->features;
3457
3458        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3459
3460        if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3461                return -ENODEV;
3462
3463        if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3464                __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3465        else
3466                __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3467
3468        if (features->type == HID_GENERIC)
3469                /* setup has already been done */
3470                return 0;
3471
3472        __set_bit(BTN_TOUCH, input_dev->keybit);
3473
3474        if (features->touch_max == 1) {
3475                input_set_abs_params(input_dev, ABS_X, 0,
3476                        features->x_max, features->x_fuzz, 0);
3477                input_set_abs_params(input_dev, ABS_Y, 0,
3478                        features->y_max, features->y_fuzz, 0);
3479                input_abs_set_res(input_dev, ABS_X,
3480                                  features->x_resolution);
3481                input_abs_set_res(input_dev, ABS_Y,
3482                                  features->y_resolution);
3483        }
3484        else if (features->touch_max > 1) {
3485                input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3486                        features->x_max, features->x_fuzz, 0);
3487                input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3488                        features->y_max, features->y_fuzz, 0);
3489                input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3490                                  features->x_resolution);
3491                input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3492                                  features->y_resolution);
3493        }
3494
3495        switch (features->type) {
3496        case INTUOSP2_BT:
3497                input_dev->evbit[0] |= BIT_MASK(EV_SW);
3498                __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3499
3500                if (wacom_wac->shared->touch->product == 0x361) {
3501                        input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3502                                             0, 12440, 4, 0);
3503                        input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3504                                             0, 8640, 4, 0);
3505                }
3506                else if (wacom_wac->shared->touch->product == 0x360) {
3507                        input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3508                                             0, 8960, 4, 0);
3509                        input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3510                                             0, 5920, 4, 0);
3511                }
3512                input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3513                input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3514
3515                /* fall through */
3516
3517        case INTUOS5:
3518        case INTUOS5L:
3519        case INTUOSPM:
3520        case INTUOSPL:
3521        case INTUOS5S:
3522        case INTUOSPS:
3523                input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3524                input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
3525                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3526                break;
3527
3528        case WACOM_24HDT:
3529                input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3530                input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
3531                input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
3532                input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
3533                /* fall through */
3534
3535        case WACOM_27QHDT:
3536        case MTSCREEN:
3537        case MTTPC:
3538        case MTTPC_B:
3539        case TABLETPC2FG:
3540                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
3541                /*fall through */
3542
3543        case TABLETPC:
3544        case TABLETPCE:
3545                break;
3546
3547        case INTUOSHT:
3548        case INTUOSHT2:
3549                input_dev->evbit[0] |= BIT_MASK(EV_SW);
3550                __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3551                /* fall through */
3552
3553        case BAMBOO_PT:
3554        case BAMBOO_TOUCH:
3555                if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3556                        input_set_abs_params(input_dev,
3557                                     ABS_MT_TOUCH_MAJOR,
3558                                     0, features->x_max, 0, 0);
3559                        input_set_abs_params(input_dev,
3560                                     ABS_MT_TOUCH_MINOR,
3561                                     0, features->y_max, 0, 0);
3562                }
3563                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3564                break;
3565
3566        case BAMBOO_PAD:
3567                input_mt_init_slots(input_dev, features->touch_max,
3568                                    INPUT_MT_POINTER);
3569                __set_bit(BTN_LEFT, input_dev->keybit);
3570                __set_bit(BTN_RIGHT, input_dev->keybit);
3571                break;
3572        }
3573        return 0;
3574}
3575
3576static int wacom_numbered_button_to_key(int n)
3577{
3578        if (n < 10)
3579                return BTN_0 + n;
3580        else if (n < 16)
3581                return BTN_A + (n-10);
3582        else if (n < 18)
3583                return BTN_BASE + (n-16);
3584        else
3585                return 0;
3586}
3587
3588static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
3589                                int button_count)
3590{
3591        int i;
3592
3593        for (i = 0; i < button_count; i++) {
3594                int key = wacom_numbered_button_to_key(i);
3595
3596                if (key)
3597                        __set_bit(key, input_dev->keybit);
3598        }
3599}
3600
3601static void wacom_report_numbered_buttons(struct input_dev *input_dev,
3602                                int button_count, int mask)
3603{
3604        int i;
3605
3606        for (i = 0; i < button_count; i++) {
3607                int key = wacom_numbered_button_to_key(i);
3608
3609                if (key)
3610                        input_report_key(input_dev, key, mask & (1 << i));
3611        }
3612}
3613
3614int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
3615                                   struct wacom_wac *wacom_wac)
3616{
3617        struct wacom_features *features = &wacom_wac->features;
3618
3619        if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
3620                features->device_type |= WACOM_DEVICETYPE_PAD;
3621
3622        if (!(features->device_type & WACOM_DEVICETYPE_PAD))
3623                return -ENODEV;
3624
3625        if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
3626                return -ENODEV;
3627
3628        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3629
3630        /* kept for making legacy xf86-input-wacom working with the wheels */
3631        __set_bit(ABS_MISC, input_dev->absbit);
3632
3633        /* kept for making legacy xf86-input-wacom accepting the pad */
3634        if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
3635              input_dev->absinfo[ABS_X].maximum)))
3636                input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
3637        if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
3638              input_dev->absinfo[ABS_Y].maximum)))
3639                input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
3640
3641        /* kept for making udev and libwacom accepting the pad */
3642        __set_bit(BTN_STYLUS, input_dev->keybit);
3643
3644        wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
3645
3646        switch (features->type) {
3647
3648        case CINTIQ_HYBRID:
3649        case CINTIQ_COMPANION_2:
3650        case DTK:
3651        case DTUS:
3652        case GRAPHIRE_BT:
3653                break;
3654
3655        case WACOM_MO:
3656                __set_bit(BTN_BACK, input_dev->keybit);
3657                __set_bit(BTN_LEFT, input_dev->keybit);
3658                __set_bit(BTN_FORWARD, input_dev->keybit);
3659                __set_bit(BTN_RIGHT, input_dev->keybit);
3660                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3661                break;
3662
3663        case WACOM_G4:
3664                __set_bit(BTN_BACK, input_dev->keybit);
3665                __set_bit(BTN_FORWARD, input_dev->keybit);
3666                input_set_capability(input_dev, EV_REL, REL_WHEEL);
3667                break;
3668
3669        case WACOM_24HD:
3670                __set_bit(KEY_PROG1, input_dev->keybit);
3671                __set_bit(KEY_PROG2, input_dev->keybit);
3672                __set_bit(KEY_PROG3, input_dev->keybit);
3673
3674                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3675                input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
3676                break;
3677
3678        case WACOM_27QHD:
3679                __set_bit(KEY_PROG1, input_dev->keybit);
3680                __set_bit(KEY_PROG2, input_dev->keybit);
3681                __set_bit(KEY_PROG3, input_dev->keybit);
3682                input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
3683                input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
3684                input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
3685                input_abs_set_res(input_dev, ABS_Y, 1024);
3686                input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
3687                input_abs_set_res(input_dev, ABS_Z, 1024);
3688                __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
3689                break;
3690
3691        case WACOM_22HD:
3692                __set_bit(KEY_PROG1, input_dev->keybit);
3693                __set_bit(KEY_PROG2, input_dev->keybit);
3694                __set_bit(KEY_PROG3, input_dev->keybit);
3695                /* fall through */
3696
3697        case WACOM_21UX2:
3698        case WACOM_BEE:
3699        case CINTIQ:
3700                input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
3701                input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
3702                break;
3703
3704        case WACOM_13HD:
3705                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3706                break;
3707
3708        case INTUOS3:
3709        case INTUOS3L:
3710                input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
3711                /* fall through */
3712
3713        case INTUOS3S:
3714                input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
3715                break;
3716
3717        case INTUOS5:
3718        case INTUOS5L:
3719        case INTUOSPM:
3720        case INTUOSPL:
3721        case INTUOS5S:
3722        case INTUOSPS:
3723        case INTUOSP2_BT:
3724                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3725                break;
3726
3727        case INTUOS4WL:
3728                /*
3729                 * For Bluetooth devices, the udev rule does not work correctly
3730                 * for pads unless we add a stylus capability, which forces
3731                 * ID_INPUT_TABLET to be set.
3732                 */
3733                __set_bit(BTN_STYLUS, input_dev->keybit);
3734                /* fall through */
3735
3736        case INTUOS4:
3737        case INTUOS4L:
3738        case INTUOS4S:
3739                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3740                break;
3741
3742        case INTUOSHT:
3743        case BAMBOO_PT:
3744        case BAMBOO_TOUCH:
3745        case INTUOSHT2:
3746                __clear_bit(ABS_MISC, input_dev->absbit);
3747
3748                __set_bit(BTN_LEFT, input_dev->keybit);
3749                __set_bit(BTN_FORWARD, input_dev->keybit);
3750                __set_bit(BTN_BACK, input_dev->keybit);
3751                __set_bit(BTN_RIGHT, input_dev->keybit);
3752
3753                break;
3754
3755        case REMOTE:
3756                input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3757                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3758                break;
3759
3760        case HID_GENERIC:
3761                break;
3762
3763        default:
3764                /* no pad supported */
3765                return -ENODEV;
3766        }
3767        return 0;
3768}
3769
3770static const struct wacom_features wacom_features_0x00 =
3771        { "Wacom Penpartner", 5040, 3780, 255, 0,
3772          PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
3773static const struct wacom_features wacom_features_0x10 =
3774        { "Wacom Graphire", 10206, 7422, 511, 63,
3775          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3776static const struct wacom_features wacom_features_0x81 =
3777        { "Wacom Graphire BT", 16704, 12064, 511, 32,
3778          GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
3779static const struct wacom_features wacom_features_0x11 =
3780        { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
3781          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3782static const struct wacom_features wacom_features_0x12 =
3783        { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
3784          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3785static const struct wacom_features wacom_features_0x13 =
3786        { "Wacom Graphire3", 10208, 7424, 511, 63,
3787          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3788static const struct wacom_features wacom_features_0x14 =
3789        { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
3790          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3791static const struct wacom_features wacom_features_0x15 =
3792        { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
3793          WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3794static const struct wacom_features wacom_features_0x16 =
3795        { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
3796          WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3797static const struct wacom_features wacom_features_0x17 =
3798        { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
3799          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3800static const struct wacom_features wacom_features_0x18 =
3801        { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
3802          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3803static const struct wacom_features wacom_features_0x19 =
3804        { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
3805          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3806static const struct wacom_features wacom_features_0x60 =
3807        { "Wacom Volito", 5104, 3712, 511, 63,
3808          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3809static const struct wacom_features wacom_features_0x61 =
3810        { "Wacom PenStation2", 3250, 2320, 255, 63,
3811          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3812static const struct wacom_features wacom_features_0x62 =
3813        { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
3814          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3815static const struct wacom_features wacom_features_0x63 =
3816        { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
3817          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3818static const struct wacom_features wacom_features_0x64 =
3819        { "Wacom PenPartner2", 3250, 2320, 511, 63,
3820          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3821static const struct wacom_features wacom_features_0x65 =
3822        { "Wacom Bamboo", 14760, 9225, 511, 63,
3823          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3824static const struct wacom_features wacom_features_0x69 =
3825        { "Wacom Bamboo1", 5104, 3712, 511, 63,
3826          GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
3827static const struct wacom_features wacom_features_0x6A =
3828        { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
3829          GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3830static const struct wacom_features wacom_features_0x6B =
3831        { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
3832          GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3833static const struct wacom_features wacom_features_0x20 =
3834        { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
3835          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3836static const struct wacom_features wacom_features_0x21 =
3837        { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
3838          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3839static const struct wacom_features wacom_features_0x22 =
3840        { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
3841          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3842static const struct wacom_features wacom_features_0x23 =
3843        { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
3844          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3845static const struct wacom_features wacom_features_0x24 =
3846        { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
3847          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3848static const struct wacom_features wacom_features_0x30 =
3849        { "Wacom PL400", 5408, 4056, 255, 0,
3850          PL, WACOM_PL_RES, WACOM_PL_RES };
3851static const struct wacom_features wacom_features_0x31 =
3852        { "Wacom PL500", 6144, 4608, 255, 0,
3853          PL, WACOM_PL_RES, WACOM_PL_RES };
3854static const struct wacom_features wacom_features_0x32 =
3855        { "Wacom PL600", 6126, 4604, 255, 0,
3856          PL, WACOM_PL_RES, WACOM_PL_RES };
3857static const struct wacom_features wacom_features_0x33 =
3858        { "Wacom PL600SX", 6260, 5016, 255, 0,
3859          PL, WACOM_PL_RES, WACOM_PL_RES };
3860static const struct wacom_features wacom_features_0x34 =
3861        { "Wacom PL550", 6144, 4608, 511, 0,
3862          PL, WACOM_PL_RES, WACOM_PL_RES };
3863static const struct wacom_features wacom_features_0x35 =
3864        { "Wacom PL800", 7220, 5780, 511, 0,
3865          PL, WACOM_PL_RES, WACOM_PL_RES };
3866static const struct wacom_features wacom_features_0x37 =
3867        { "Wacom PL700", 6758, 5406, 511, 0,
3868          PL, WACOM_PL_RES, WACOM_PL_RES };
3869static const struct wacom_features wacom_features_0x38 =
3870        { "Wacom PL510", 6282, 4762, 511, 0,
3871          PL, WACOM_PL_RES, WACOM_PL_RES };
3872static const struct wacom_features wacom_features_0x39 =
3873        { "Wacom DTU710", 34080, 27660, 511, 0,
3874          PL, WACOM_PL_RES, WACOM_PL_RES };
3875static const struct wacom_features wacom_features_0xC4 =
3876        { "Wacom DTF521", 6282, 4762, 511, 0,
3877          PL, WACOM_PL_RES, WACOM_PL_RES };
3878static const struct wacom_features wacom_features_0xC0 =
3879        { "Wacom DTF720", 6858, 5506, 511, 0,
3880          PL, WACOM_PL_RES, WACOM_PL_RES };
3881static const struct wacom_features wacom_features_0xC2 =
3882        { "Wacom DTF720a", 6858, 5506, 511, 0,
3883          PL, WACOM_PL_RES, WACOM_PL_RES };
3884static const struct wacom_features wacom_features_0x03 =
3885        { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
3886          PTU, WACOM_PL_RES, WACOM_PL_RES };
3887static const struct wacom_features wacom_features_0x41 =
3888        { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
3889          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3890static const struct wacom_features wacom_features_0x42 =
3891        { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
3892          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3893static const struct wacom_features wacom_features_0x43 =
3894        { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
3895          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3896static const struct wacom_features wacom_features_0x44 =
3897        { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
3898          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3899static const struct wacom_features wacom_features_0x45 =
3900        { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
3901          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3902static const struct wacom_features wacom_features_0xB0 =
3903        { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
3904          INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3905static const struct wacom_features wacom_features_0xB1 =
3906        { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
3907          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3908static const struct wacom_features wacom_features_0xB2 =
3909        { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
3910          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3911static const struct wacom_features wacom_features_0xB3 =
3912        { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
3913          INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3914static const struct wacom_features wacom_features_0xB4 =
3915        { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
3916          INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3917static const struct wacom_features wacom_features_0xB5 =
3918        { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
3919          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3920static const struct wacom_features wacom_features_0xB7 =
3921        { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
3922          INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3923static const struct wacom_features wacom_features_0xB8 =
3924        { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
3925          INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3926static const struct wacom_features wacom_features_0xB9 =
3927        { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
3928          INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3929static const struct wacom_features wacom_features_0xBA =
3930        { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
3931          INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3932static const struct wacom_features wacom_features_0xBB =
3933        { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
3934          INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3935static const struct wacom_features wacom_features_0xBC =
3936        { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3937          INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3938static const struct wacom_features wacom_features_0xBD =
3939        { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3940          INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3941static const struct wacom_features wacom_features_0x26 =
3942        { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
3943          INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
3944static const struct wacom_features wacom_features_0x27 =
3945        { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
3946          INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3947static const struct wacom_features wacom_features_0x28 =
3948        { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
3949          INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3950static const struct wacom_features wacom_features_0x29 =
3951        { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
3952          INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3953static const struct wacom_features wacom_features_0x2A =
3954        { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
3955          INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3956static const struct wacom_features wacom_features_0x314 =
3957        { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
3958          INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
3959          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3960static const struct wacom_features wacom_features_0x315 =
3961        { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
3962          INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3963          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3964static const struct wacom_features wacom_features_0x317 =
3965        { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
3966          INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3967          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3968static const struct wacom_features wacom_features_0xF4 =
3969        { "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
3970          WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3971          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3972          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3973static const struct wacom_features wacom_features_0xF8 =
3974        { "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
3975          WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3976          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3977          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3978          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
3979static const struct wacom_features wacom_features_0xF6 =
3980        { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
3981          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
3982          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3983static const struct wacom_features wacom_features_0x32A =
3984        { "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
3985          WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3986          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3987          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3988static const struct wacom_features wacom_features_0x32B =
3989        { "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
3990          WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3991          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3992          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3993          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
3994static const struct wacom_features wacom_features_0x32C =
3995        { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
3996          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
3997static const struct wacom_features wacom_features_0x3F =
3998        { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
3999          CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4000static const struct wacom_features wacom_features_0xC5 =
4001        { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
4002          WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4003static const struct wacom_features wacom_features_0xC6 =
4004        { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
4005          WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4006static const struct wacom_features wacom_features_0x304 =
4007        { "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
4008          WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4009          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4010          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4011static const struct wacom_features wacom_features_0x333 =
4012        { "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
4013          WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4014          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4015          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4016          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
4017static const struct wacom_features wacom_features_0x335 =
4018        { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
4019          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
4020          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4021static const struct wacom_features wacom_features_0xC7 =
4022        { "Wacom DTU1931", 37832, 30305, 511, 0,
4023          PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4024static const struct wacom_features wacom_features_0xCE =
4025        { "Wacom DTU2231", 47864, 27011, 511, 0,
4026          DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4027          .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
4028static const struct wacom_features wacom_features_0xF0 =
4029        { "Wacom DTU1631", 34623, 19553, 511, 0,
4030          DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4031static const struct wacom_features wacom_features_0xFB =
4032        { "Wacom DTU1031", 22096, 13960, 511, 0,
4033          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4034          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4035          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4036static const struct wacom_features wacom_features_0x32F =
4037        { "Wacom DTU1031X", 22672, 12928, 511, 0,
4038          DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
4039          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4040          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4041static const struct wacom_features wacom_features_0x336 =
4042        { "Wacom DTU1141", 23672, 13403, 1023, 0,
4043          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4044          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4045          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4046static const struct wacom_features wacom_features_0x57 =
4047        { "Wacom DTK2241", 95840, 54260, 2047, 63,
4048          DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4049          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4050          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4051static const struct wacom_features wacom_features_0x59 = /* Pen */
4052        { "Wacom DTH2242", 95840, 54260, 2047, 63,
4053          DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4054          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4055          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4056          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4057static const struct wacom_features wacom_features_0x5D = /* Touch */
4058        { "Wacom DTH2242",       .type = WACOM_24HDT,
4059          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4060          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4061static const struct wacom_features wacom_features_0xCC =
4062        { "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
4063          WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4064          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4065          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4066static const struct wacom_features wacom_features_0xFA =
4067        { "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
4068          WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4069          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4070          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4071static const struct wacom_features wacom_features_0x5B =
4072        { "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
4073          WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4074          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4075          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4076          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
4077static const struct wacom_features wacom_features_0x5E =
4078        { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
4079          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4080          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4081static const struct wacom_features wacom_features_0x90 =
4082        { "Wacom ISDv4 90", 26202, 16325, 255, 0,
4083          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4084static const struct wacom_features wacom_features_0x93 =
4085        { "Wacom ISDv4 93", 26202, 16325, 255, 0,
4086          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4087static const struct wacom_features wacom_features_0x97 =
4088        { "Wacom ISDv4 97", 26202, 16325, 511, 0,
4089          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4090static const struct wacom_features wacom_features_0x9A =
4091        { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
4092          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4093static const struct wacom_features wacom_features_0x9F =
4094        { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
4095          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4096static const struct wacom_features wacom_features_0xE2 =
4097        { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4098          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4099static const struct wacom_features wacom_features_0xE3 =
4100        { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4101          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4102static const struct wacom_features wacom_features_0xE5 =
4103        { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4104          MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4105static const struct wacom_features wacom_features_0xE6 =
4106        { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4107          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4108static const struct wacom_features wacom_features_0xEC =
4109        { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
4110          TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4111static const struct wacom_features wacom_features_0xED =
4112        { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
4113          TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4114static const struct wacom_features wacom_features_0xEF =
4115        { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
4116          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4117static const struct wacom_features wacom_features_0x100 =
4118        { "Wacom ISDv4 100", 26202, 16325, 255, 0,
4119          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4120static const struct wacom_features wacom_features_0x101 =
4121        { "Wacom ISDv4 101", 26202, 16325, 255, 0,
4122          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4123static const struct wacom_features wacom_features_0x10D =
4124        { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4125          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4126static const struct wacom_features wacom_features_0x10E =
4127        { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4128          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4129static const struct wacom_features wacom_features_0x10F =
4130        { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4131          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4132static const struct wacom_features wacom_features_0x116 =
4133        { "Wacom ISDv4 116", 26202, 16325, 255, 0,
4134          TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4135static const struct wacom_features wacom_features_0x12C =
4136        { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
4137          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4138static const struct wacom_features wacom_features_0x4001 =
4139        { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4140          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4141static const struct wacom_features wacom_features_0x4004 =
4142        { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4143          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4144static const struct wacom_features wacom_features_0x5000 =
4145        { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4146          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4147static const struct wacom_features wacom_features_0x5002 =
4148        { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4149          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4150static const struct wacom_features wacom_features_0x47 =
4151        { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4152          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4153static const struct wacom_features wacom_features_0x84 =
4154        { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
4155static const struct wacom_features wacom_features_0xD0 =
4156        { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
4157          BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4158static const struct wacom_features wacom_features_0xD1 =
4159        { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4160          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4161static const struct wacom_features wacom_features_0xD2 =
4162        { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4163          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4164static const struct wacom_features wacom_features_0xD3 =
4165        { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4166          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4167static const struct wacom_features wacom_features_0xD4 =
4168        { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
4169          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4170static const struct wacom_features wacom_features_0xD5 =
4171        { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
4172          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4173static const struct wacom_features wacom_features_0xD6 =
4174        { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4175          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4176static const struct wacom_features wacom_features_0xD7 =
4177        { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4178          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4179static const struct wacom_features wacom_features_0xD8 =
4180        { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4181          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4182static const struct wacom_features wacom_features_0xDA =
4183        { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4184          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4185static const struct wacom_features wacom_features_0xDB =
4186        { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4187          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4188static const struct wacom_features wacom_features_0xDD =
4189        { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4190          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4191static const struct wacom_features wacom_features_0xDE =
4192        { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4193          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4194static const struct wacom_features wacom_features_0xDF =
4195        { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4196          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4197static const struct wacom_features wacom_features_0x300 =
4198        { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
4199          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4200static const struct wacom_features wacom_features_0x301 =
4201        { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
4202          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4203static const struct wacom_features wacom_features_0x302 =
4204        { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4205          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4206          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4207static const struct wacom_features wacom_features_0x303 =
4208        { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4209          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4210          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4211static const struct wacom_features wacom_features_0x30E =
4212        { "Wacom Intuos S", 15200, 9500, 1023, 31,
4213          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4214          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4215static const struct wacom_features wacom_features_0x6004 =
4216        { "ISD-V4", 12800, 8000, 255, 0,
4217          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4218static const struct wacom_features wacom_features_0x307 =
4219        { "Wacom ISDv5 307", 59552, 33848, 2047, 63,
4220          CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4221          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4222          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4223          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
4224static const struct wacom_features wacom_features_0x309 =
4225        { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
4226          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4227          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4228static const struct wacom_features wacom_features_0x30A =
4229        { "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
4230          CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4231          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4232          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4233          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4234static const struct wacom_features wacom_features_0x30C =
4235        { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4236          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4237          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4238static const struct wacom_features wacom_features_0x318 =
4239        { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4240          .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4241static const struct wacom_features wacom_features_0x319 =
4242        { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4243          .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4244static const struct wacom_features wacom_features_0x325 =
4245        { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4246          CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4247          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4248          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4249          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4250static const struct wacom_features wacom_features_0x326 = /* Touch */
4251        { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4252          .oPid = 0x325 };
4253static const struct wacom_features wacom_features_0x323 =
4254        { "Wacom Intuos P M", 21600, 13500, 1023, 31,
4255          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4256          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4257static const struct wacom_features wacom_features_0x331 =
4258        { "Wacom Express Key Remote", .type = REMOTE,
4259          .numbered_buttons = 18, .check_for_hid_type = true,
4260          .hid_type = HID_TYPE_USBNONE };
4261static const struct wacom_features wacom_features_0x33B =
4262        { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4263          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4264          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4265static const struct wacom_features wacom_features_0x33C =
4266        { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4267          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4268          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4269static const struct wacom_features wacom_features_0x33D =
4270        { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4271          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4272          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4273static const struct wacom_features wacom_features_0x33E =
4274        { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4275          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4276          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4277static const struct wacom_features wacom_features_0x343 =
4278        { "Wacom DTK1651", 34816, 19759, 1023, 0,
4279          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4280          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4281          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4282static const struct wacom_features wacom_features_0x360 =
4283        { "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
4284          INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4285static const struct wacom_features wacom_features_0x361 =
4286        { "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
4287          INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4288
4289static const struct wacom_features wacom_features_HID_ANY_ID =
4290        { "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
4291
4292#define USB_DEVICE_WACOM(prod)                                          \
4293        HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4294        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4295
4296#define BT_DEVICE_WACOM(prod)                                           \
4297        HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4298        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4299
4300#define I2C_DEVICE_WACOM(prod)                                          \
4301        HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4302        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4303
4304#define USB_DEVICE_LENOVO(prod)                                 \
4305        HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),                     \
4306        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4307
4308const struct hid_device_id wacom_ids[] = {
4309        { USB_DEVICE_WACOM(0x00) },
4310        { USB_DEVICE_WACOM(0x03) },
4311        { USB_DEVICE_WACOM(0x10) },
4312        { USB_DEVICE_WACOM(0x11) },
4313        { USB_DEVICE_WACOM(0x12) },
4314        { USB_DEVICE_WACOM(0x13) },
4315        { USB_DEVICE_WACOM(0x14) },
4316        { USB_DEVICE_WACOM(0x15) },
4317        { USB_DEVICE_WACOM(0x16) },
4318        { USB_DEVICE_WACOM(0x17) },
4319        { USB_DEVICE_WACOM(0x18) },
4320        { USB_DEVICE_WACOM(0x19) },
4321        { USB_DEVICE_WACOM(0x20) },
4322        { USB_DEVICE_WACOM(0x21) },
4323        { USB_DEVICE_WACOM(0x22) },
4324        { USB_DEVICE_WACOM(0x23) },
4325        { USB_DEVICE_WACOM(0x24) },
4326        { USB_DEVICE_WACOM(0x26) },
4327        { USB_DEVICE_WACOM(0x27) },
4328        { USB_DEVICE_WACOM(0x28) },
4329        { USB_DEVICE_WACOM(0x29) },
4330        { USB_DEVICE_WACOM(0x2A) },
4331        { USB_DEVICE_WACOM(0x30) },
4332        { USB_DEVICE_WACOM(0x31) },
4333        { USB_DEVICE_WACOM(0x32) },
4334        { USB_DEVICE_WACOM(0x33) },
4335        { USB_DEVICE_WACOM(0x34) },
4336        { USB_DEVICE_WACOM(0x35) },
4337        { USB_DEVICE_WACOM(0x37) },
4338        { USB_DEVICE_WACOM(0x38) },
4339        { USB_DEVICE_WACOM(0x39) },
4340        { USB_DEVICE_WACOM(0x3F) },
4341        { USB_DEVICE_WACOM(0x41) },
4342        { USB_DEVICE_WACOM(0x42) },
4343        { USB_DEVICE_WACOM(0x43) },
4344        { USB_DEVICE_WACOM(0x44) },
4345        { USB_DEVICE_WACOM(0x45) },
4346        { USB_DEVICE_WACOM(0x47) },
4347        { USB_DEVICE_WACOM(0x57) },
4348        { USB_DEVICE_WACOM(0x59) },
4349        { USB_DEVICE_WACOM(0x5B) },
4350        { USB_DEVICE_WACOM(0x5D) },
4351        { USB_DEVICE_WACOM(0x5E) },
4352        { USB_DEVICE_WACOM(0x60) },
4353        { USB_DEVICE_WACOM(0x61) },
4354        { USB_DEVICE_WACOM(0x62) },
4355        { USB_DEVICE_WACOM(0x63) },
4356        { USB_DEVICE_WACOM(0x64) },
4357        { USB_DEVICE_WACOM(0x65) },
4358        { USB_DEVICE_WACOM(0x69) },
4359        { USB_DEVICE_WACOM(0x6A) },
4360        { USB_DEVICE_WACOM(0x6B) },
4361        { BT_DEVICE_WACOM(0x81) },
4362        { USB_DEVICE_WACOM(0x84) },
4363        { USB_DEVICE_WACOM(0x90) },
4364        { USB_DEVICE_WACOM(0x93) },
4365        { USB_DEVICE_WACOM(0x97) },
4366        { USB_DEVICE_WACOM(0x9A) },
4367        { USB_DEVICE_WACOM(0x9F) },
4368        { USB_DEVICE_WACOM(0xB0) },
4369        { USB_DEVICE_WACOM(0xB1) },
4370        { USB_DEVICE_WACOM(0xB2) },
4371        { USB_DEVICE_WACOM(0xB3) },
4372        { USB_DEVICE_WACOM(0xB4) },
4373        { USB_DEVICE_WACOM(0xB5) },
4374        { USB_DEVICE_WACOM(0xB7) },
4375        { USB_DEVICE_WACOM(0xB8) },
4376        { USB_DEVICE_WACOM(0xB9) },
4377        { USB_DEVICE_WACOM(0xBA) },
4378        { USB_DEVICE_WACOM(0xBB) },
4379        { USB_DEVICE_WACOM(0xBC) },
4380        { BT_DEVICE_WACOM(0xBD) },
4381        { USB_DEVICE_WACOM(0xC0) },
4382        { USB_DEVICE_WACOM(0xC2) },
4383        { USB_DEVICE_WACOM(0xC4) },
4384        { USB_DEVICE_WACOM(0xC5) },
4385        { USB_DEVICE_WACOM(0xC6) },
4386        { USB_DEVICE_WACOM(0xC7) },
4387        { USB_DEVICE_WACOM(0xCC) },
4388        { USB_DEVICE_WACOM(0xCE) },
4389        { USB_DEVICE_WACOM(0xD0) },
4390        { USB_DEVICE_WACOM(0xD1) },
4391        { USB_DEVICE_WACOM(0xD2) },
4392        { USB_DEVICE_WACOM(0xD3) },
4393        { USB_DEVICE_WACOM(0xD4) },
4394        { USB_DEVICE_WACOM(0xD5) },
4395        { USB_DEVICE_WACOM(0xD6) },
4396        { USB_DEVICE_WACOM(0xD7) },
4397        { USB_DEVICE_WACOM(0xD8) },
4398        { USB_DEVICE_WACOM(0xDA) },
4399        { USB_DEVICE_WACOM(0xDB) },
4400        { USB_DEVICE_WACOM(0xDD) },
4401        { USB_DEVICE_WACOM(0xDE) },
4402        { USB_DEVICE_WACOM(0xDF) },
4403        { USB_DEVICE_WACOM(0xE2) },
4404        { USB_DEVICE_WACOM(0xE3) },
4405        { USB_DEVICE_WACOM(0xE5) },
4406        { USB_DEVICE_WACOM(0xE6) },
4407        { USB_DEVICE_WACOM(0xEC) },
4408        { USB_DEVICE_WACOM(0xED) },
4409        { USB_DEVICE_WACOM(0xEF) },
4410        { USB_DEVICE_WACOM(0xF0) },
4411        { USB_DEVICE_WACOM(0xF4) },
4412        { USB_DEVICE_WACOM(0xF6) },
4413        { USB_DEVICE_WACOM(0xF8) },
4414        { USB_DEVICE_WACOM(0xFA) },
4415        { USB_DEVICE_WACOM(0xFB) },
4416        { USB_DEVICE_WACOM(0x100) },
4417        { USB_DEVICE_WACOM(0x101) },
4418        { USB_DEVICE_WACOM(0x10D) },
4419        { USB_DEVICE_WACOM(0x10E) },
4420        { USB_DEVICE_WACOM(0x10F) },
4421        { USB_DEVICE_WACOM(0x116) },
4422        { USB_DEVICE_WACOM(0x12C) },
4423        { USB_DEVICE_WACOM(0x300) },
4424        { USB_DEVICE_WACOM(0x301) },
4425        { USB_DEVICE_WACOM(0x302) },
4426        { USB_DEVICE_WACOM(0x303) },
4427        { USB_DEVICE_WACOM(0x304) },
4428        { USB_DEVICE_WACOM(0x307) },
4429        { USB_DEVICE_WACOM(0x309) },
4430        { USB_DEVICE_WACOM(0x30A) },
4431        { USB_DEVICE_WACOM(0x30C) },
4432        { USB_DEVICE_WACOM(0x30E) },
4433        { USB_DEVICE_WACOM(0x314) },
4434        { USB_DEVICE_WACOM(0x315) },
4435        { USB_DEVICE_WACOM(0x317) },
4436        { USB_DEVICE_WACOM(0x318) },
4437        { USB_DEVICE_WACOM(0x319) },
4438        { USB_DEVICE_WACOM(0x323) },
4439        { USB_DEVICE_WACOM(0x325) },
4440        { USB_DEVICE_WACOM(0x326) },
4441        { USB_DEVICE_WACOM(0x32A) },
4442        { USB_DEVICE_WACOM(0x32B) },
4443        { USB_DEVICE_WACOM(0x32C) },
4444        { USB_DEVICE_WACOM(0x32F) },
4445        { USB_DEVICE_WACOM(0x331) },
4446        { USB_DEVICE_WACOM(0x333) },
4447        { USB_DEVICE_WACOM(0x335) },
4448        { USB_DEVICE_WACOM(0x336) },
4449        { USB_DEVICE_WACOM(0x33B) },
4450        { USB_DEVICE_WACOM(0x33C) },
4451        { USB_DEVICE_WACOM(0x33D) },
4452        { USB_DEVICE_WACOM(0x33E) },
4453        { USB_DEVICE_WACOM(0x343) },
4454        { BT_DEVICE_WACOM(0x360) },
4455        { BT_DEVICE_WACOM(0x361) },
4456        { USB_DEVICE_WACOM(0x4001) },
4457        { USB_DEVICE_WACOM(0x4004) },
4458        { USB_DEVICE_WACOM(0x5000) },
4459        { USB_DEVICE_WACOM(0x5002) },
4460        { USB_DEVICE_LENOVO(0x6004) },
4461
4462        { USB_DEVICE_WACOM(HID_ANY_ID) },
4463        { I2C_DEVICE_WACOM(HID_ANY_ID) },
4464        { BT_DEVICE_WACOM(HID_ANY_ID) },
4465        { }
4466};
4467MODULE_DEVICE_TABLE(hid, wacom_ids);
4468