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