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                                /* fall through */
 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                /* fall through */
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
1431        int buttons = data[282] | ((data[281] & 0x40) << 2);
1432        int ring = data[285] & 0x7F;
1433        bool ringstatus = data[285] & 0x80;
1434        bool prox = buttons || ringstatus;
1435
1436        /* Fix touchring data: userspace expects 0 at left and increasing clockwise */
1437        ring = 71 - ring;
1438        ring += 3*72/16;
1439        if (ring > 71)
1440                ring -= 72;
1441
1442        wacom_report_numbered_buttons(pad_input, 9, buttons);
1443
1444        input_report_abs(pad_input, ABS_WHEEL, ringstatus ? ring : 0);
1445
1446        input_report_key(pad_input, wacom->tool[1], prox ? 1 : 0);
1447        input_report_abs(pad_input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
1448        input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1449
1450        input_sync(pad_input);
1451}
1452
1453static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
1454{
1455        unsigned char *data = wacom->data;
1456
1457        bool chg = data[284] & 0x80;
1458        int battery_status = data[284] & 0x7F;
1459
1460        wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1461                             battery_status, chg, 1, chg);
1462}
1463
1464static void wacom_intuos_gen3_bt_pad(struct wacom_wac *wacom)
1465{
1466        struct input_dev *pad_input = wacom->pad_input;
1467        unsigned char *data = wacom->data;
1468
1469        int buttons = data[44];
1470
1471        wacom_report_numbered_buttons(pad_input, 4, buttons);
1472
1473        input_report_key(pad_input, wacom->tool[1], buttons ? 1 : 0);
1474        input_report_abs(pad_input, ABS_MISC, buttons ? PAD_DEVICE_ID : 0);
1475        input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1476
1477        input_sync(pad_input);
1478}
1479
1480static void wacom_intuos_gen3_bt_battery(struct wacom_wac *wacom)
1481{
1482        unsigned char *data = wacom->data;
1483
1484        bool chg = data[45] & 0x80;
1485        int battery_status = data[45] & 0x7F;
1486
1487        wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1488                             battery_status, chg, 1, chg);
1489}
1490
1491static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
1492{
1493        unsigned char *data = wacom->data;
1494
1495        if (data[0] != 0x80 && data[0] != 0x81) {
1496                dev_dbg(wacom->pen_input->dev.parent,
1497                        "%s: received unknown report #%d\n", __func__, data[0]);
1498                return 0;
1499        }
1500
1501        wacom_intuos_pro2_bt_pen(wacom);
1502        if (wacom->features.type == INTUOSP2_BT ||
1503            wacom->features.type == INTUOSP2S_BT) {
1504                wacom_intuos_pro2_bt_touch(wacom);
1505                wacom_intuos_pro2_bt_pad(wacom);
1506                wacom_intuos_pro2_bt_battery(wacom);
1507        } else {
1508                wacom_intuos_gen3_bt_pad(wacom);
1509                wacom_intuos_gen3_bt_battery(wacom);
1510        }
1511        return 0;
1512}
1513
1514static int wacom_24hdt_irq(struct wacom_wac *wacom)
1515{
1516        struct input_dev *input = wacom->touch_input;
1517        unsigned char *data = wacom->data;
1518        int i;
1519        int current_num_contacts = data[61];
1520        int contacts_to_send = 0;
1521        int num_contacts_left = 4; /* maximum contacts per packet */
1522        int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1523        int y_offset = 2;
1524
1525        if (wacom->shared->has_mute_touch_switch &&
1526            !wacom->shared->is_touch_on) {
1527                if (!wacom->shared->touch_down)
1528                        return 0;
1529        }
1530
1531        if (wacom->features.type == WACOM_27QHDT) {
1532                current_num_contacts = data[63];
1533                num_contacts_left = 10;
1534                byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1535                y_offset = 0;
1536        }
1537
1538        /*
1539         * First packet resets the counter since only the first
1540         * packet in series will have non-zero current_num_contacts.
1541         */
1542        if (current_num_contacts)
1543                wacom->num_contacts_left = current_num_contacts;
1544
1545        contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1546
1547        for (i = 0; i < contacts_to_send; i++) {
1548                int offset = (byte_per_packet * i) + 1;
1549                bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1550                int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
1551
1552                if (slot < 0)
1553                        continue;
1554                input_mt_slot(input, slot);
1555                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1556
1557                if (touch) {
1558                        int t_x = get_unaligned_le16(&data[offset + 2]);
1559                        int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
1560
1561                        input_report_abs(input, ABS_MT_POSITION_X, t_x);
1562                        input_report_abs(input, ABS_MT_POSITION_Y, t_y);
1563
1564                        if (wacom->features.type != WACOM_27QHDT) {
1565                                int c_x = get_unaligned_le16(&data[offset + 4]);
1566                                int c_y = get_unaligned_le16(&data[offset + 8]);
1567                                int w = get_unaligned_le16(&data[offset + 10]);
1568                                int h = get_unaligned_le16(&data[offset + 12]);
1569
1570                                input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1571                                input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1572                                                 min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1573                                input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1574                                input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1575                        }
1576                }
1577        }
1578        input_mt_sync_frame(input);
1579
1580        wacom->num_contacts_left -= contacts_to_send;
1581        if (wacom->num_contacts_left <= 0) {
1582                wacom->num_contacts_left = 0;
1583                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1584        }
1585        return 1;
1586}
1587
1588static int wacom_mt_touch(struct wacom_wac *wacom)
1589{
1590        struct input_dev *input = wacom->touch_input;
1591        unsigned char *data = wacom->data;
1592        int i;
1593        int current_num_contacts = data[2];
1594        int contacts_to_send = 0;
1595        int x_offset = 0;
1596
1597        /* MTTPC does not support Height and Width */
1598        if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
1599                x_offset = -4;
1600
1601        /*
1602         * First packet resets the counter since only the first
1603         * packet in series will have non-zero current_num_contacts.
1604         */
1605        if (current_num_contacts)
1606                wacom->num_contacts_left = current_num_contacts;
1607
1608        /* There are at most 5 contacts per packet */
1609        contacts_to_send = min(5, wacom->num_contacts_left);
1610
1611        for (i = 0; i < contacts_to_send; i++) {
1612                int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1613                bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1614                int id = get_unaligned_le16(&data[offset + 1]);
1615                int slot = input_mt_get_slot_by_key(input, id);
1616
1617                if (slot < 0)
1618                        continue;
1619
1620                input_mt_slot(input, slot);
1621                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1622                if (touch) {
1623                        int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1624                        int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1625                        input_report_abs(input, ABS_MT_POSITION_X, x);
1626                        input_report_abs(input, ABS_MT_POSITION_Y, y);
1627                }
1628        }
1629        input_mt_sync_frame(input);
1630
1631        wacom->num_contacts_left -= contacts_to_send;
1632        if (wacom->num_contacts_left <= 0) {
1633                wacom->num_contacts_left = 0;
1634                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1635        }
1636        return 1;
1637}
1638
1639static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1640{
1641        struct input_dev *input = wacom->touch_input;
1642        unsigned char *data = wacom->data;
1643        int i;
1644
1645        for (i = 0; i < 2; i++) {
1646                int p = data[1] & (1 << i);
1647                bool touch = p && report_touch_events(wacom);
1648
1649                input_mt_slot(input, i);
1650                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1651                if (touch) {
1652                        int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1653                        int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1654
1655                        input_report_abs(input, ABS_MT_POSITION_X, x);
1656                        input_report_abs(input, ABS_MT_POSITION_Y, y);
1657                }
1658        }
1659        input_mt_sync_frame(input);
1660
1661        /* keep touch state for pen event */
1662        wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1663
1664        return 1;
1665}
1666
1667static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
1668{
1669        unsigned char *data = wacom->data;
1670        struct input_dev *input = wacom->touch_input;
1671        bool prox = report_touch_events(wacom);
1672        int x = 0, y = 0;
1673
1674        if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1675                return 0;
1676
1677        if (len == WACOM_PKGLEN_TPC1FG) {
1678                prox = prox && (data[0] & 0x01);
1679                x = get_unaligned_le16(&data[1]);
1680                y = get_unaligned_le16(&data[3]);
1681        } else if (len == WACOM_PKGLEN_TPC1FG_B) {
1682                prox = prox && (data[2] & 0x01);
1683                x = get_unaligned_le16(&data[3]);
1684                y = get_unaligned_le16(&data[5]);
1685        } else {
1686                prox = prox && (data[1] & 0x01);
1687                x = le16_to_cpup((__le16 *)&data[2]);
1688                y = le16_to_cpup((__le16 *)&data[4]);
1689        }
1690
1691        if (prox) {
1692                input_report_abs(input, ABS_X, x);
1693                input_report_abs(input, ABS_Y, y);
1694        }
1695        input_report_key(input, BTN_TOUCH, prox);
1696
1697        /* keep touch state for pen events */
1698        wacom->shared->touch_down = prox;
1699
1700        return 1;
1701}
1702
1703static int wacom_tpc_pen(struct wacom_wac *wacom)
1704{
1705        unsigned char *data = wacom->data;
1706        struct input_dev *input = wacom->pen_input;
1707        bool prox = data[1] & 0x20;
1708
1709        if (!wacom->shared->stylus_in_proximity) /* first in prox */
1710                /* Going into proximity select tool */
1711                wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1712
1713        /* keep pen state for touch events */
1714        wacom->shared->stylus_in_proximity = prox;
1715
1716        /* send pen events only when touch is up or forced out
1717         * or touch arbitration is off
1718         */
1719        if (!delay_pen_events(wacom)) {
1720                input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1721                input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1722                input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1723                input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
1724                input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
1725                input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1726                input_report_key(input, wacom->tool[0], prox);
1727                return 1;
1728        }
1729
1730        return 0;
1731}
1732
1733static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1734{
1735        unsigned char *data = wacom->data;
1736
1737        if (wacom->pen_input) {
1738                dev_dbg(wacom->pen_input->dev.parent,
1739                        "%s: received report #%d\n", __func__, data[0]);
1740
1741                if (len == WACOM_PKGLEN_PENABLED ||
1742                    data[0] == WACOM_REPORT_PENABLED)
1743                        return wacom_tpc_pen(wacom);
1744        }
1745        else if (wacom->touch_input) {
1746                dev_dbg(wacom->touch_input->dev.parent,
1747                        "%s: received report #%d\n", __func__, data[0]);
1748
1749                switch (len) {
1750                case WACOM_PKGLEN_TPC1FG:
1751                        return wacom_tpc_single_touch(wacom, len);
1752
1753                case WACOM_PKGLEN_TPC2FG:
1754                        return wacom_tpc_mt_touch(wacom);
1755
1756                default:
1757                        switch (data[0]) {
1758                        case WACOM_REPORT_TPC1FG:
1759                        case WACOM_REPORT_TPCHID:
1760                        case WACOM_REPORT_TPCST:
1761                        case WACOM_REPORT_TPC1FGE:
1762                                return wacom_tpc_single_touch(wacom, len);
1763
1764                        case WACOM_REPORT_TPCMT:
1765                        case WACOM_REPORT_TPCMT2:
1766                                return wacom_mt_touch(wacom);
1767
1768                        }
1769                }
1770        }
1771
1772        return 0;
1773}
1774
1775static int wacom_offset_rotation(struct input_dev *input, struct hid_usage *usage,
1776                                 int value, int num, int denom)
1777{
1778        struct input_absinfo *abs = &input->absinfo[usage->code];
1779        int range = (abs->maximum - abs->minimum + 1);
1780
1781        value += num*range/denom;
1782        if (value > abs->maximum)
1783                value -= range;
1784        else if (value < abs->minimum)
1785                value += range;
1786        return value;
1787}
1788
1789int wacom_equivalent_usage(int usage)
1790{
1791        if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1792                int subpage = (usage & 0xFF00) << 8;
1793                int subusage = (usage & 0xFF);
1794
1795                if (subpage == WACOM_HID_SP_PAD ||
1796                    subpage == WACOM_HID_SP_BUTTON ||
1797                    subpage == WACOM_HID_SP_DIGITIZER ||
1798                    subpage == WACOM_HID_SP_DIGITIZERINFO ||
1799                    usage == WACOM_HID_WD_SENSE ||
1800                    usage == WACOM_HID_WD_SERIALHI ||
1801                    usage == WACOM_HID_WD_TOOLTYPE ||
1802                    usage == WACOM_HID_WD_DISTANCE ||
1803                    usage == WACOM_HID_WD_TOUCHSTRIP ||
1804                    usage == WACOM_HID_WD_TOUCHSTRIP2 ||
1805                    usage == WACOM_HID_WD_TOUCHRING ||
1806                    usage == WACOM_HID_WD_TOUCHRINGSTATUS ||
1807                    usage == WACOM_HID_WD_REPORT_VALID) {
1808                        return usage;
1809                }
1810
1811                if (subpage == HID_UP_UNDEFINED)
1812                        subpage = HID_UP_DIGITIZER;
1813
1814                return subpage | subusage;
1815        }
1816
1817        if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1818                int subpage = (usage & 0xFF00) << 8;
1819                int subusage = (usage & 0xFF);
1820
1821                if (usage == WACOM_HID_WT_REPORT_VALID)
1822                        return usage;
1823
1824                if (subpage == HID_UP_UNDEFINED)
1825                        subpage = WACOM_HID_SP_DIGITIZER;
1826
1827                return subpage | subusage;
1828        }
1829
1830        return usage;
1831}
1832
1833static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1834                struct hid_field *field, __u8 type, __u16 code, int fuzz)
1835{
1836        struct wacom *wacom = input_get_drvdata(input);
1837        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1838        struct wacom_features *features = &wacom_wac->features;
1839        int fmin = field->logical_minimum;
1840        int fmax = field->logical_maximum;
1841        unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
1842        int resolution_code = code;
1843
1844        if (equivalent_usage == HID_DG_TWIST) {
1845                resolution_code = ABS_RZ;
1846        }
1847
1848        if (equivalent_usage == HID_GD_X) {
1849                fmin += features->offset_left;
1850                fmax -= features->offset_right;
1851        }
1852        if (equivalent_usage == HID_GD_Y) {
1853                fmin += features->offset_top;
1854                fmax -= features->offset_bottom;
1855        }
1856
1857        usage->type = type;
1858        usage->code = code;
1859
1860        set_bit(type, input->evbit);
1861
1862        switch (type) {
1863        case EV_ABS:
1864                input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1865                input_abs_set_res(input, code,
1866                                  hidinput_calc_abs_res(field, resolution_code));
1867                break;
1868        case EV_KEY:
1869                input_set_capability(input, EV_KEY, code);
1870                break;
1871        case EV_MSC:
1872                input_set_capability(input, EV_MSC, code);
1873                break;
1874        case EV_SW:
1875                input_set_capability(input, EV_SW, code);
1876                break;
1877        }
1878}
1879
1880static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
1881                struct hid_field *field, struct hid_usage *usage)
1882{
1883        struct wacom *wacom = hid_get_drvdata(hdev);
1884        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1885        struct wacom_features *features = &wacom_wac->features;
1886        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1887
1888        switch (equivalent_usage) {
1889        case HID_DG_BATTERYSTRENGTH:
1890        case WACOM_HID_WD_BATTERY_LEVEL:
1891        case WACOM_HID_WD_BATTERY_CHARGING:
1892                features->quirks |= WACOM_QUIRK_BATTERY;
1893                break;
1894        }
1895}
1896
1897static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1898                struct hid_usage *usage, __s32 value)
1899{
1900        struct wacom *wacom = hid_get_drvdata(hdev);
1901        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1902        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1903
1904        switch (equivalent_usage) {
1905        case HID_DG_BATTERYSTRENGTH:
1906                if (value == 0) {
1907                        wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1908                }
1909                else {
1910                        value = value * 100 / (field->logical_maximum - field->logical_minimum);
1911                        wacom_wac->hid_data.battery_capacity = value;
1912                        wacom_wac->hid_data.bat_connected = 1;
1913                        wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1914                }
1915                break;
1916        case WACOM_HID_WD_BATTERY_LEVEL:
1917                value = value * 100 / (field->logical_maximum - field->logical_minimum);
1918                wacom_wac->hid_data.battery_capacity = value;
1919                wacom_wac->hid_data.bat_connected = 1;
1920                wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1921                break;
1922        case WACOM_HID_WD_BATTERY_CHARGING:
1923                wacom_wac->hid_data.bat_charging = value;
1924                wacom_wac->hid_data.ps_connected = value;
1925                wacom_wac->hid_data.bat_connected = 1;
1926                wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1927                break;
1928        }
1929}
1930
1931static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1932                struct hid_report *report)
1933{
1934        return;
1935}
1936
1937static void wacom_wac_battery_report(struct hid_device *hdev,
1938                struct hid_report *report)
1939{
1940        struct wacom *wacom = hid_get_drvdata(hdev);
1941        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1942        struct wacom_features *features = &wacom_wac->features;
1943
1944        if (features->quirks & WACOM_QUIRK_BATTERY) {
1945                int status = wacom_wac->hid_data.bat_status;
1946                int capacity = wacom_wac->hid_data.battery_capacity;
1947                bool charging = wacom_wac->hid_data.bat_charging;
1948                bool connected = wacom_wac->hid_data.bat_connected;
1949                bool powered = wacom_wac->hid_data.ps_connected;
1950
1951                wacom_notify_battery(wacom_wac, status, capacity, charging,
1952                                     connected, powered);
1953        }
1954}
1955
1956static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1957                struct hid_field *field, struct hid_usage *usage)
1958{
1959        struct wacom *wacom = hid_get_drvdata(hdev);
1960        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1961        struct wacom_features *features = &wacom_wac->features;
1962        struct input_dev *input = wacom_wac->pad_input;
1963        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1964
1965        switch (equivalent_usage) {
1966        case WACOM_HID_WD_ACCELEROMETER_X:
1967                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1968                wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
1969                features->device_type |= WACOM_DEVICETYPE_PAD;
1970                break;
1971        case WACOM_HID_WD_ACCELEROMETER_Y:
1972                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1973                wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
1974                features->device_type |= WACOM_DEVICETYPE_PAD;
1975                break;
1976        case WACOM_HID_WD_ACCELEROMETER_Z:
1977                __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1978                wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
1979                features->device_type |= WACOM_DEVICETYPE_PAD;
1980                break;
1981        case WACOM_HID_WD_BUTTONCENTER:
1982        case WACOM_HID_WD_BUTTONHOME:
1983        case WACOM_HID_WD_BUTTONUP:
1984        case WACOM_HID_WD_BUTTONDOWN:
1985        case WACOM_HID_WD_BUTTONLEFT:
1986        case WACOM_HID_WD_BUTTONRIGHT:
1987                wacom_map_usage(input, usage, field, EV_KEY,
1988                                wacom_numbered_button_to_key(features->numbered_buttons),
1989                                0);
1990                features->numbered_buttons++;
1991                features->device_type |= WACOM_DEVICETYPE_PAD;
1992                break;
1993        case WACOM_HID_WD_TOUCHONOFF:
1994        case WACOM_HID_WD_MUTE_DEVICE:
1995                /*
1996                 * This usage, which is used to mute touch events, comes
1997                 * from the pad packet, but is reported on the touch
1998                 * interface. Because the touch interface may not have
1999                 * been created yet, we cannot call wacom_map_usage(). In
2000                 * order to process this usage when we receive it, we set
2001                 * the usage type and code directly.
2002                 */
2003                wacom_wac->has_mute_touch_switch = true;
2004                usage->type = EV_SW;
2005                usage->code = SW_MUTE_DEVICE;
2006                features->device_type |= WACOM_DEVICETYPE_PAD;
2007                break;
2008        case WACOM_HID_WD_TOUCHSTRIP:
2009                wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
2010                features->device_type |= WACOM_DEVICETYPE_PAD;
2011                break;
2012        case WACOM_HID_WD_TOUCHSTRIP2:
2013                wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
2014                features->device_type |= WACOM_DEVICETYPE_PAD;
2015                break;
2016        case WACOM_HID_WD_TOUCHRING:
2017                wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2018                features->device_type |= WACOM_DEVICETYPE_PAD;
2019                break;
2020        case WACOM_HID_WD_TOUCHRINGSTATUS:
2021                /*
2022                 * Only set up type/code association. Completely mapping
2023                 * this usage may overwrite the axis resolution and range.
2024                 */
2025                usage->type = EV_ABS;
2026                usage->code = ABS_WHEEL;
2027                set_bit(EV_ABS, input->evbit);
2028                features->device_type |= WACOM_DEVICETYPE_PAD;
2029                break;
2030        case WACOM_HID_WD_BUTTONCONFIG:
2031                wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
2032                features->device_type |= WACOM_DEVICETYPE_PAD;
2033                break;
2034        case WACOM_HID_WD_ONSCREEN_KEYBOARD:
2035                wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
2036                features->device_type |= WACOM_DEVICETYPE_PAD;
2037                break;
2038        case WACOM_HID_WD_CONTROLPANEL:
2039                wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
2040                features->device_type |= WACOM_DEVICETYPE_PAD;
2041                break;
2042        case WACOM_HID_WD_MODE_CHANGE:
2043                /* do not overwrite previous data */
2044                if (!wacom_wac->has_mode_change) {
2045                        wacom_wac->has_mode_change = true;
2046                        wacom_wac->is_direct_mode = true;
2047                }
2048                features->device_type |= WACOM_DEVICETYPE_PAD;
2049                break;
2050        }
2051
2052        switch (equivalent_usage & 0xfffffff0) {
2053        case WACOM_HID_WD_EXPRESSKEY00:
2054                wacom_map_usage(input, usage, field, EV_KEY,
2055                                wacom_numbered_button_to_key(features->numbered_buttons),
2056                                0);
2057                features->numbered_buttons++;
2058                features->device_type |= WACOM_DEVICETYPE_PAD;
2059                break;
2060        }
2061}
2062
2063static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
2064                struct hid_usage *usage, __s32 value)
2065{
2066        struct wacom *wacom = hid_get_drvdata(hdev);
2067        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2068        struct input_dev *input = wacom_wac->pad_input;
2069        struct wacom_features *features = &wacom_wac->features;
2070        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2071        int i;
2072        bool do_report = false;
2073
2074        /*
2075         * Avoid reporting this event and setting inrange_state if this usage
2076         * hasn't been mapped.
2077         */
2078        if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
2079                return;
2080
2081        if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
2082                if (usage->hid != WACOM_HID_WD_TOUCHRING)
2083                        wacom_wac->hid_data.inrange_state |= value;
2084        }
2085
2086        switch (equivalent_usage) {
2087        case WACOM_HID_WD_TOUCHRING:
2088                /*
2089                 * Userspace expects touchrings to increase in value with
2090                 * clockwise gestures and have their zero point at the
2091                 * tablet's left. HID events "should" be clockwise-
2092                 * increasing and zero at top, though the MobileStudio
2093                 * Pro and 2nd-gen Intuos Pro don't do this...
2094                 */
2095                if (hdev->vendor == 0x56a &&
2096                    (hdev->product == 0x34d || hdev->product == 0x34e ||  /* MobileStudio Pro */
2097                     hdev->product == 0x357 || hdev->product == 0x358 ||  /* Intuos Pro 2 */
2098                     hdev->product == 0x392 ||                            /* Intuos Pro 2 */
2099                     hdev->product == 0x398 || hdev->product == 0x399 ||  /* MobileStudio Pro */
2100                     hdev->product == 0x3AA)) {                           /* MobileStudio Pro */
2101                        value = (field->logical_maximum - value);
2102
2103                        if (hdev->product == 0x357 || hdev->product == 0x358 ||
2104                            hdev->product == 0x392)
2105                                value = wacom_offset_rotation(input, usage, value, 3, 16);
2106                        else if (hdev->product == 0x34d || hdev->product == 0x34e ||
2107                                 hdev->product == 0x398 || hdev->product == 0x399 ||
2108                                 hdev->product == 0x3AA)
2109                                value = wacom_offset_rotation(input, usage, value, 1, 2);
2110                }
2111                else {
2112                        value = wacom_offset_rotation(input, usage, value, 1, 4);
2113                }
2114                do_report = true;
2115                break;
2116        case WACOM_HID_WD_TOUCHRINGSTATUS:
2117                if (!value)
2118                        input_event(input, usage->type, usage->code, 0);
2119                break;
2120
2121        case WACOM_HID_WD_MUTE_DEVICE:
2122        case WACOM_HID_WD_TOUCHONOFF:
2123                if (wacom_wac->shared->touch_input) {
2124                        bool *is_touch_on = &wacom_wac->shared->is_touch_on;
2125
2126                        if (equivalent_usage == WACOM_HID_WD_MUTE_DEVICE && value)
2127                                *is_touch_on = !(*is_touch_on);
2128                        else if (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)
2129                                *is_touch_on = value;
2130
2131                        input_report_switch(wacom_wac->shared->touch_input,
2132                                            SW_MUTE_DEVICE, !(*is_touch_on));
2133                        input_sync(wacom_wac->shared->touch_input);
2134                }
2135                break;
2136
2137        case WACOM_HID_WD_MODE_CHANGE:
2138                if (wacom_wac->is_direct_mode != value) {
2139                        wacom_wac->is_direct_mode = value;
2140                        wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
2141                }
2142                break;
2143
2144        case WACOM_HID_WD_BUTTONCENTER:
2145                for (i = 0; i < wacom->led.count; i++)
2146                        wacom_update_led(wacom, features->numbered_buttons,
2147                                         value, i);
2148                 /* fall through*/
2149        default:
2150                do_report = true;
2151                break;
2152        }
2153
2154        if (do_report) {
2155                input_event(input, usage->type, usage->code, value);
2156                if (value)
2157                        wacom_wac->hid_data.pad_input_event_flag = true;
2158        }
2159}
2160
2161static void wacom_wac_pad_pre_report(struct hid_device *hdev,
2162                struct hid_report *report)
2163{
2164        struct wacom *wacom = hid_get_drvdata(hdev);
2165        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2166
2167        wacom_wac->hid_data.inrange_state = 0;
2168}
2169
2170static void wacom_wac_pad_report(struct hid_device *hdev,
2171                struct hid_report *report, struct hid_field *field)
2172{
2173        struct wacom *wacom = hid_get_drvdata(hdev);
2174        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2175        struct input_dev *input = wacom_wac->pad_input;
2176        bool active = wacom_wac->hid_data.inrange_state != 0;
2177
2178        /* report prox for expresskey events */
2179        if (wacom_wac->hid_data.pad_input_event_flag) {
2180                input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
2181                input_sync(input);
2182                if (!active)
2183                        wacom_wac->hid_data.pad_input_event_flag = false;
2184        }
2185}
2186
2187static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
2188                struct hid_field *field, struct hid_usage *usage)
2189{
2190        struct wacom *wacom = hid_get_drvdata(hdev);
2191        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2192        struct wacom_features *features = &wacom_wac->features;
2193        struct input_dev *input = wacom_wac->pen_input;
2194        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2195
2196        switch (equivalent_usage) {
2197        case HID_GD_X:
2198                wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2199                break;
2200        case HID_GD_Y:
2201                wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2202                break;
2203        case WACOM_HID_WD_DISTANCE:
2204        case HID_GD_Z:
2205                wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
2206                break;
2207        case HID_DG_TIPPRESSURE:
2208                wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
2209                break;
2210        case HID_DG_INRANGE:
2211                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2212                break;
2213        case HID_DG_INVERT:
2214                wacom_map_usage(input, usage, field, EV_KEY,
2215                                BTN_TOOL_RUBBER, 0);
2216                break;
2217        case HID_DG_TILT_X:
2218                wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
2219                break;
2220        case HID_DG_TILT_Y:
2221                wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
2222                break;
2223        case HID_DG_TWIST:
2224                wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2225                break;
2226        case HID_DG_ERASER:
2227        case HID_DG_TIPSWITCH:
2228                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2229                break;
2230        case HID_DG_BARRELSWITCH:
2231                wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
2232                break;
2233        case HID_DG_BARRELSWITCH2:
2234                wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
2235                break;
2236        case HID_DG_TOOLSERIALNUMBER:
2237                features->quirks |= WACOM_QUIRK_TOOLSERIAL;
2238                wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
2239                break;
2240        case WACOM_HID_WD_SENSE:
2241                features->quirks |= WACOM_QUIRK_SENSE;
2242                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2243                break;
2244        case WACOM_HID_WD_SERIALHI:
2245                wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
2246
2247                if (!(features->quirks & WACOM_QUIRK_AESPEN)) {
2248                        set_bit(EV_KEY, input->evbit);
2249                        input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
2250                        input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2251                        input_set_capability(input, EV_KEY, BTN_TOOL_BRUSH);
2252                        input_set_capability(input, EV_KEY, BTN_TOOL_PENCIL);
2253                        input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
2254                        if (!(features->device_type & WACOM_DEVICETYPE_DIRECT)) {
2255                                input_set_capability(input, EV_KEY, BTN_TOOL_MOUSE);
2256                                input_set_capability(input, EV_KEY, BTN_TOOL_LENS);
2257                        }
2258                }
2259                break;
2260        case WACOM_HID_WD_FINGERWHEEL:
2261                wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2262                break;
2263        }
2264}
2265
2266static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
2267                struct hid_usage *usage, __s32 value)
2268{
2269        struct wacom *wacom = hid_get_drvdata(hdev);
2270        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2271        struct wacom_features *features = &wacom_wac->features;
2272        struct input_dev *input = wacom_wac->pen_input;
2273        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2274
2275        if (wacom_wac->is_invalid_bt_frame)
2276                return;
2277
2278        switch (equivalent_usage) {
2279        case HID_GD_Z:
2280                /*
2281                 * HID_GD_Z "should increase as the control's position is
2282                 * moved from high to low", while ABS_DISTANCE instead
2283                 * increases in value as the tool moves from low to high.
2284                 */
2285                value = field->logical_maximum - value;
2286                break;
2287        case HID_DG_INRANGE:
2288                wacom_wac->hid_data.inrange_state = value;
2289                if (!(features->quirks & WACOM_QUIRK_SENSE))
2290                        wacom_wac->hid_data.sense_state = value;
2291                return;
2292        case HID_DG_INVERT:
2293                wacom_wac->hid_data.invert_state = value;
2294                return;
2295        case HID_DG_ERASER:
2296        case HID_DG_TIPSWITCH:
2297                wacom_wac->hid_data.tipswitch |= value;
2298                return;
2299        case HID_DG_BARRELSWITCH:
2300                wacom_wac->hid_data.barrelswitch = value;
2301                return;
2302        case HID_DG_BARRELSWITCH2:
2303                wacom_wac->hid_data.barrelswitch2 = value;
2304                return;
2305        case HID_DG_TOOLSERIALNUMBER:
2306                if (value) {
2307                        wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
2308                        wacom_wac->serial[0] |= wacom_s32tou(value, field->report_size);
2309                }
2310                return;
2311        case HID_DG_TWIST:
2312                /*
2313                 * Userspace expects pen twist to have its zero point when
2314                 * the buttons/finger is on the tablet's left. HID values
2315                 * are zero when buttons are toward the top.
2316                 */
2317                value = wacom_offset_rotation(input, usage, value, 1, 4);
2318                break;
2319        case WACOM_HID_WD_SENSE:
2320                wacom_wac->hid_data.sense_state = value;
2321                return;
2322        case WACOM_HID_WD_SERIALHI:
2323                if (value) {
2324                        __u32 raw_value = wacom_s32tou(value, field->report_size);
2325
2326                        wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
2327                        wacom_wac->serial[0] |= ((__u64)raw_value) << 32;
2328                        /*
2329                         * Non-USI EMR devices may contain additional tool type
2330                         * information here. See WACOM_HID_WD_TOOLTYPE case for
2331                         * more details.
2332                         */
2333                        if (value >> 20 == 1) {
2334                                wacom_wac->id[0] |= raw_value & 0xFFFFF;
2335                        }
2336                }
2337                return;
2338        case WACOM_HID_WD_TOOLTYPE:
2339                /*
2340                 * Some devices (MobileStudio Pro, and possibly later
2341                 * devices as well) do not return the complete tool
2342                 * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2343                 * bitwise OR so the complete value can be built
2344                 * up over time :(
2345                 */
2346                wacom_wac->id[0] |= wacom_s32tou(value, field->report_size);
2347                return;
2348        case WACOM_HID_WD_OFFSETLEFT:
2349                if (features->offset_left && value != features->offset_left)
2350                        hid_warn(hdev, "%s: overriding existing left offset "
2351                                 "%d -> %d\n", __func__, value,
2352                                 features->offset_left);
2353                features->offset_left = value;
2354                return;
2355        case WACOM_HID_WD_OFFSETRIGHT:
2356                if (features->offset_right && value != features->offset_right)
2357                        hid_warn(hdev, "%s: overriding existing right offset "
2358                                 "%d -> %d\n", __func__, value,
2359                                 features->offset_right);
2360                features->offset_right = value;
2361                return;
2362        case WACOM_HID_WD_OFFSETTOP:
2363                if (features->offset_top && value != features->offset_top)
2364                        hid_warn(hdev, "%s: overriding existing top offset "
2365                                 "%d -> %d\n", __func__, value,
2366                                 features->offset_top);
2367                features->offset_top = value;
2368                return;
2369        case WACOM_HID_WD_OFFSETBOTTOM:
2370                if (features->offset_bottom && value != features->offset_bottom)
2371                        hid_warn(hdev, "%s: overriding existing bottom offset "
2372                                 "%d -> %d\n", __func__, value,
2373                                 features->offset_bottom);
2374                features->offset_bottom = value;
2375                return;
2376        case WACOM_HID_WD_REPORT_VALID:
2377                wacom_wac->is_invalid_bt_frame = !value;
2378                return;
2379        }
2380
2381        /* send pen events only when touch is up or forced out
2382         * or touch arbitration is off
2383         */
2384        if (!usage->type || delay_pen_events(wacom_wac))
2385                return;
2386
2387        /* send pen events only when the pen is in range */
2388        if (wacom_wac->hid_data.inrange_state)
2389                input_event(input, usage->type, usage->code, value);
2390        else if (wacom_wac->shared->stylus_in_proximity && !wacom_wac->hid_data.sense_state)
2391                input_event(input, usage->type, usage->code, 0);
2392}
2393
2394static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2395                struct hid_report *report)
2396{
2397        struct wacom *wacom = hid_get_drvdata(hdev);
2398        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2399
2400        wacom_wac->is_invalid_bt_frame = false;
2401        return;
2402}
2403
2404static void wacom_wac_pen_report(struct hid_device *hdev,
2405                struct hid_report *report)
2406{
2407        struct wacom *wacom = hid_get_drvdata(hdev);
2408        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2409        struct input_dev *input = wacom_wac->pen_input;
2410        bool range = wacom_wac->hid_data.inrange_state;
2411        bool sense = wacom_wac->hid_data.sense_state;
2412
2413        if (wacom_wac->is_invalid_bt_frame)
2414                return;
2415
2416        if (!wacom_wac->tool[0] && range) { /* first in range */
2417                /* Going into range select tool */
2418                if (wacom_wac->hid_data.invert_state)
2419                        wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2420                else if (wacom_wac->id[0])
2421                        wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2422                else
2423                        wacom_wac->tool[0] = BTN_TOOL_PEN;
2424        }
2425
2426        /* keep pen state for touch events */
2427        wacom_wac->shared->stylus_in_proximity = sense;
2428
2429        if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
2430                int id = wacom_wac->id[0];
2431                int sw_state = wacom_wac->hid_data.barrelswitch |
2432                               (wacom_wac->hid_data.barrelswitch2 << 1);
2433
2434                input_report_key(input, BTN_STYLUS, sw_state == 1);
2435                input_report_key(input, BTN_STYLUS2, sw_state == 2);
2436                input_report_key(input, BTN_STYLUS3, sw_state == 3);
2437
2438                /*
2439                 * Non-USI EMR tools should have their IDs mangled to
2440                 * match the legacy behavior of wacom_intuos_general
2441                 */
2442                if (wacom_wac->serial[0] >> 52 == 1)
2443                        id = wacom_intuos_id_mangle(id);
2444
2445                /*
2446                 * To ensure compatibility with xf86-input-wacom, we should
2447                 * report the BTN_TOOL_* event prior to the ABS_MISC or
2448                 * MSC_SERIAL events.
2449                 */
2450                input_report_key(input, BTN_TOUCH,
2451                                wacom_wac->hid_data.tipswitch);
2452                input_report_key(input, wacom_wac->tool[0], sense);
2453                if (wacom_wac->serial[0]) {
2454                        input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
2455                        input_report_abs(input, ABS_MISC, sense ? id : 0);
2456                }
2457
2458                wacom_wac->hid_data.tipswitch = false;
2459
2460                input_sync(input);
2461        }
2462
2463        if (!sense) {
2464                wacom_wac->tool[0] = 0;
2465                wacom_wac->id[0] = 0;
2466                wacom_wac->serial[0] = 0;
2467        }
2468}
2469
2470static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2471                struct hid_field *field, struct hid_usage *usage)
2472{
2473        struct wacom *wacom = hid_get_drvdata(hdev);
2474        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2475        struct input_dev *input = wacom_wac->touch_input;
2476        unsigned touch_max = wacom_wac->features.touch_max;
2477        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2478
2479        switch (equivalent_usage) {
2480        case HID_GD_X:
2481                if (touch_max == 1)
2482                        wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2483                else
2484                        wacom_map_usage(input, usage, field, EV_ABS,
2485                                        ABS_MT_POSITION_X, 4);
2486                break;
2487        case HID_GD_Y:
2488                if (touch_max == 1)
2489                        wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2490                else
2491                        wacom_map_usage(input, usage, field, EV_ABS,
2492                                        ABS_MT_POSITION_Y, 4);
2493                break;
2494        case HID_DG_WIDTH:
2495        case HID_DG_HEIGHT:
2496                wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2497                wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2498                input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2499                break;
2500        case HID_DG_TIPSWITCH:
2501                wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2502                break;
2503        case HID_DG_CONTACTCOUNT:
2504                wacom_wac->hid_data.cc_report = field->report->id;
2505                wacom_wac->hid_data.cc_index = field->index;
2506                wacom_wac->hid_data.cc_value_index = usage->usage_index;
2507                break;
2508        case HID_DG_CONTACTID:
2509                if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2510                        /*
2511                         * The HID descriptor for G11 sensors leaves logical
2512                         * maximum set to '1' despite it being a multitouch
2513                         * device. Override to a sensible number.
2514                         */
2515                        field->logical_maximum = 255;
2516                }
2517                break;
2518        }
2519}
2520
2521static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2522                struct input_dev *input)
2523{
2524        struct hid_data *hid_data = &wacom_wac->hid_data;
2525        bool mt = wacom_wac->features.touch_max > 1;
2526        bool prox = hid_data->tipswitch &&
2527                    report_touch_events(wacom_wac);
2528
2529        if (wacom_wac->shared->has_mute_touch_switch &&
2530            !wacom_wac->shared->is_touch_on) {
2531                if (!wacom_wac->shared->touch_down)
2532                        return;
2533                prox = 0;
2534        }
2535
2536        wacom_wac->hid_data.num_received++;
2537        if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2538                return;
2539
2540        if (mt) {
2541                int slot;
2542
2543                slot = input_mt_get_slot_by_key(input, hid_data->id);
2544                input_mt_slot(input, slot);
2545                input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2546        }
2547        else {
2548                input_report_key(input, BTN_TOUCH, prox);
2549        }
2550
2551        if (prox) {
2552                input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2553                                 hid_data->x);
2554                input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2555                                 hid_data->y);
2556
2557                if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2558                        input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2559                        input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2560                        if (hid_data->width != hid_data->height)
2561                                input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2562                }
2563        }
2564}
2565
2566static void wacom_wac_finger_event(struct hid_device *hdev,
2567                struct hid_field *field, struct hid_usage *usage, __s32 value)
2568{
2569        struct wacom *wacom = hid_get_drvdata(hdev);
2570        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2571        unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2572        struct wacom_features *features = &wacom->wacom_wac.features;
2573
2574        if (wacom_wac->is_invalid_bt_frame)
2575                return;
2576
2577        switch (equivalent_usage) {
2578        case HID_GD_X:
2579                wacom_wac->hid_data.x = value;
2580                break;
2581        case HID_GD_Y:
2582                wacom_wac->hid_data.y = value;
2583                break;
2584        case HID_DG_WIDTH:
2585                wacom_wac->hid_data.width = value;
2586                break;
2587        case HID_DG_HEIGHT:
2588                wacom_wac->hid_data.height = value;
2589                break;
2590        case HID_DG_CONTACTID:
2591                wacom_wac->hid_data.id = value;
2592                break;
2593        case HID_DG_TIPSWITCH:
2594                wacom_wac->hid_data.tipswitch = value;
2595                break;
2596        case WACOM_HID_WT_REPORT_VALID:
2597                wacom_wac->is_invalid_bt_frame = !value;
2598                return;
2599        case HID_DG_CONTACTMAX:
2600                features->touch_max = value;
2601                return;
2602        }
2603
2604        if (usage->usage_index + 1 == field->report_count) {
2605                if (equivalent_usage == wacom_wac->hid_data.last_slot_field)
2606                        wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2607        }
2608}
2609
2610static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2611                struct hid_report *report)
2612{
2613        struct wacom *wacom = hid_get_drvdata(hdev);
2614        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2615        struct hid_data* hid_data = &wacom_wac->hid_data;
2616        int i;
2617
2618        wacom_wac->is_invalid_bt_frame = false;
2619
2620        for (i = 0; i < report->maxfield; i++) {
2621                struct hid_field *field = report->field[i];
2622                int j;
2623
2624                for (j = 0; j < field->maxusage; j++) {
2625                        struct hid_usage *usage = &field->usage[j];
2626                        unsigned int equivalent_usage =
2627                                wacom_equivalent_usage(usage->hid);
2628
2629                        switch (equivalent_usage) {
2630                        case HID_GD_X:
2631                        case HID_GD_Y:
2632                        case HID_DG_WIDTH:
2633                        case HID_DG_HEIGHT:
2634                        case HID_DG_CONTACTID:
2635                        case HID_DG_INRANGE:
2636                        case HID_DG_INVERT:
2637                        case HID_DG_TIPSWITCH:
2638                                hid_data->last_slot_field = equivalent_usage;
2639                                break;
2640                        }
2641                }
2642        }
2643}
2644
2645static void wacom_wac_finger_report(struct hid_device *hdev,
2646                struct hid_report *report)
2647{
2648        struct wacom *wacom = hid_get_drvdata(hdev);
2649        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2650        struct input_dev *input = wacom_wac->touch_input;
2651        unsigned touch_max = wacom_wac->features.touch_max;
2652        struct hid_data *hid_data = &wacom_wac->hid_data;
2653
2654        /* If more packets of data are expected, give us a chance to
2655         * process them rather than immediately syncing a partial
2656         * update.
2657         */
2658        if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2659                return;
2660
2661        if (touch_max > 1)
2662                input_mt_sync_frame(input);
2663
2664        input_sync(input);
2665        wacom_wac->hid_data.num_received = 0;
2666        hid_data->num_expected = 0;
2667
2668        /* keep touch state for pen event */
2669        wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2670}
2671
2672void wacom_wac_usage_mapping(struct hid_device *hdev,
2673                struct hid_field *field, struct hid_usage *usage)
2674{
2675        struct wacom *wacom = hid_get_drvdata(hdev);
2676        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2677        struct wacom_features *features = &wacom_wac->features;
2678
2679        if (WACOM_DIRECT_DEVICE(field))
2680                features->device_type |= WACOM_DEVICETYPE_DIRECT;
2681
2682        /* usage tests must precede field tests */
2683        if (WACOM_BATTERY_USAGE(usage))
2684                wacom_wac_battery_usage_mapping(hdev, field, usage);
2685        else if (WACOM_PAD_FIELD(field))
2686                wacom_wac_pad_usage_mapping(hdev, field, usage);
2687        else if (WACOM_PEN_FIELD(field))
2688                wacom_wac_pen_usage_mapping(hdev, field, usage);
2689        else if (WACOM_FINGER_FIELD(field))
2690                wacom_wac_finger_usage_mapping(hdev, field, usage);
2691}
2692
2693void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2694                struct hid_usage *usage, __s32 value)
2695{
2696        struct wacom *wacom = hid_get_drvdata(hdev);
2697
2698        if (wacom->wacom_wac.features.type != HID_GENERIC)
2699                return;
2700
2701        if (value > field->logical_maximum || value < field->logical_minimum)
2702                return;
2703
2704        /* usage tests must precede field tests */
2705        if (WACOM_BATTERY_USAGE(usage))
2706                wacom_wac_battery_event(hdev, field, usage, value);
2707        else if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2708                wacom_wac_pad_event(hdev, field, usage, value);
2709        else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2710                wacom_wac_pen_event(hdev, field, usage, value);
2711        else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2712                wacom_wac_finger_event(hdev, field, usage, value);
2713}
2714
2715static void wacom_report_events(struct hid_device *hdev,
2716                                struct hid_report *report, int collection_index,
2717                                int field_index)
2718{
2719        int r;
2720
2721        for (r = field_index; r < report->maxfield; r++) {
2722                struct hid_field *field;
2723                unsigned count, n;
2724
2725                field = report->field[r];
2726                count = field->report_count;
2727
2728                if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2729                        continue;
2730
2731                for (n = 0 ; n < count; n++) {
2732                        if (field->usage[n].collection_index == collection_index)
2733                                wacom_wac_event(hdev, field, &field->usage[n],
2734                                                field->value[n]);
2735                        else
2736                                return;
2737                }
2738        }
2739}
2740
2741static void wacom_set_num_expected(struct hid_device *hdev,
2742                                   struct hid_report *report,
2743                                   int collection_index,
2744                                   struct hid_field *field,
2745                                   int field_index)
2746{
2747        struct wacom *wacom = hid_get_drvdata(hdev);
2748        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2749        struct hid_data *hid_data = &wacom_wac->hid_data;
2750        unsigned int original_collection_level =
2751                hdev->collection[collection_index].level;
2752        bool end_collection = false;
2753        int i;
2754
2755        if (hid_data->num_expected)
2756                return;
2757
2758        // find the contact count value for this segment
2759        for (i = field_index; i < report->maxfield && !end_collection; i++) {
2760                struct hid_field *field = report->field[i];
2761                unsigned int field_level =
2762                        hdev->collection[field->usage[0].collection_index].level;
2763                unsigned int j;
2764
2765                if (field_level != original_collection_level)
2766                        continue;
2767
2768                for (j = 0; j < field->maxusage; j++) {
2769                        struct hid_usage *usage = &field->usage[j];
2770
2771                        if (usage->collection_index != collection_index) {
2772                                end_collection = true;
2773                                break;
2774                        }
2775                        if (wacom_equivalent_usage(usage->hid) == HID_DG_CONTACTCOUNT) {
2776                                hid_data->cc_report = report->id;
2777                                hid_data->cc_index = i;
2778                                hid_data->cc_value_index = j;
2779
2780                                if (hid_data->cc_report != 0 &&
2781                                    hid_data->cc_index >= 0) {
2782
2783                                        struct hid_field *field =
2784                                                report->field[hid_data->cc_index];
2785                                        int value =
2786                                                field->value[hid_data->cc_value_index];
2787
2788                                        if (value)
2789                                                hid_data->num_expected = value;
2790                                }
2791                        }
2792                }
2793        }
2794
2795        if (hid_data->cc_report == 0 || hid_data->cc_index < 0)
2796                hid_data->num_expected = wacom_wac->features.touch_max;
2797}
2798
2799static int wacom_wac_collection(struct hid_device *hdev, struct hid_report *report,
2800                         int collection_index, struct hid_field *field,
2801                         int field_index)
2802{
2803        struct wacom *wacom = hid_get_drvdata(hdev);
2804
2805        if (WACOM_FINGER_FIELD(field))
2806                wacom_set_num_expected(hdev, report, collection_index, field,
2807                                       field_index);
2808        wacom_report_events(hdev, report, collection_index, field_index);
2809
2810        /*
2811         * Non-input reports may be sent prior to the device being
2812         * completely initialized. Since only their events need
2813         * to be processed, exit after 'wacom_report_events' has
2814         * been called to prevent potential crashes in the report-
2815         * processing functions.
2816         */
2817        if (report->type != HID_INPUT_REPORT)
2818                return -1;
2819
2820        if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2821                wacom_wac_pen_report(hdev, report);
2822        else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2823                wacom_wac_finger_report(hdev, report);
2824
2825        return 0;
2826}
2827
2828void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2829{
2830        struct wacom *wacom = hid_get_drvdata(hdev);
2831        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2832        struct hid_field *field;
2833        bool pad_in_hid_field = false, pen_in_hid_field = false,
2834                finger_in_hid_field = false, true_pad = false;
2835        int r;
2836        int prev_collection = -1;
2837
2838        if (wacom_wac->features.type != HID_GENERIC)
2839                return;
2840
2841        for (r = 0; r < report->maxfield; r++) {
2842                field = report->field[r];
2843
2844                if (WACOM_PAD_FIELD(field))
2845                        pad_in_hid_field = true;
2846                if (WACOM_PEN_FIELD(field))
2847                        pen_in_hid_field = true;
2848                if (WACOM_FINGER_FIELD(field))
2849                        finger_in_hid_field = true;
2850                if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY)
2851                        true_pad = true;
2852        }
2853
2854        wacom_wac_battery_pre_report(hdev, report);
2855
2856        if (pad_in_hid_field && wacom->wacom_wac.pad_input)
2857                wacom_wac_pad_pre_report(hdev, report);
2858        if (pen_in_hid_field && wacom->wacom_wac.pen_input)
2859                wacom_wac_pen_pre_report(hdev, report);
2860        if (finger_in_hid_field && wacom->wacom_wac.touch_input)
2861                wacom_wac_finger_pre_report(hdev, report);
2862
2863        for (r = 0; r < report->maxfield; r++) {
2864                field = report->field[r];
2865
2866                if (field->usage[0].collection_index != prev_collection) {
2867                        if (wacom_wac_collection(hdev, report,
2868                                field->usage[0].collection_index, field, r) < 0)
2869                                return;
2870                        prev_collection = field->usage[0].collection_index;
2871                }
2872        }
2873
2874        wacom_wac_battery_report(hdev, report);
2875
2876        if (true_pad && wacom->wacom_wac.pad_input)
2877                wacom_wac_pad_report(hdev, report, field);
2878}
2879
2880static int wacom_bpt_touch(struct wacom_wac *wacom)
2881{
2882        struct wacom_features *features = &wacom->features;
2883        struct input_dev *input = wacom->touch_input;
2884        struct input_dev *pad_input = wacom->pad_input;
2885        unsigned char *data = wacom->data;
2886        int i;
2887
2888        if (data[0] != 0x02)
2889            return 0;
2890
2891        for (i = 0; i < 2; i++) {
2892                int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
2893                bool touch = report_touch_events(wacom)
2894                           && (data[offset + 3] & 0x80);
2895
2896                input_mt_slot(input, i);
2897                input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2898                if (touch) {
2899                        int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
2900                        int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
2901                        if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
2902                                x <<= 5;
2903                                y <<= 5;
2904                        }
2905                        input_report_abs(input, ABS_MT_POSITION_X, x);
2906                        input_report_abs(input, ABS_MT_POSITION_Y, y);
2907                }
2908        }
2909
2910        input_mt_sync_frame(input);
2911
2912        input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
2913        input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
2914        input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
2915        input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
2916        wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2917
2918        return 1;
2919}
2920
2921static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
2922{
2923        struct wacom_features *features = &wacom->features;
2924        struct input_dev *input = wacom->touch_input;
2925        bool touch = data[1] & 0x80;
2926        int slot = input_mt_get_slot_by_key(input, data[0]);
2927
2928        if (slot < 0)
2929                return;
2930
2931        touch = touch && report_touch_events(wacom);
2932
2933        input_mt_slot(input, slot);
2934        input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2935
2936        if (touch) {
2937                int x = (data[2] << 4) | (data[4] >> 4);
2938                int y = (data[3] << 4) | (data[4] & 0x0f);
2939                int width, height;
2940
2941                if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
2942                        width  = data[5] * 100;
2943                        height = data[6] * 100;
2944                } else {
2945                        /*
2946                         * "a" is a scaled-down area which we assume is
2947                         * roughly circular and which can be described as:
2948                         * a=(pi*r^2)/C.
2949                         */
2950                        int a = data[5];
2951                        int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
2952                        int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
2953                        width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
2954                        height = width * y_res / x_res;
2955                }
2956
2957                input_report_abs(input, ABS_MT_POSITION_X, x);
2958                input_report_abs(input, ABS_MT_POSITION_Y, y);
2959                input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
2960                input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
2961        }
2962}
2963
2964static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
2965{
2966        struct input_dev *input = wacom->pad_input;
2967        struct wacom_features *features = &wacom->features;
2968
2969        if (features->type == INTUOSHT || features->type == INTUOSHT2) {
2970                input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
2971                input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
2972        } else {
2973                input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
2974                input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
2975        }
2976        input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
2977        input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
2978}
2979
2980static int wacom_bpt3_touch(struct wacom_wac *wacom)
2981{
2982        unsigned char *data = wacom->data;
2983        int count = data[1] & 0x07;
2984        int  touch_changed = 0, i;
2985
2986        if (data[0] != 0x02)
2987            return 0;
2988
2989        /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
2990        for (i = 0; i < count; i++) {
2991                int offset = (8 * i) + 2;
2992                int msg_id = data[offset];
2993
2994                if (msg_id >= 2 && msg_id <= 17) {
2995                        wacom_bpt3_touch_msg(wacom, data + offset);
2996                        touch_changed++;
2997                } else if (msg_id == 128)
2998                        wacom_bpt3_button_msg(wacom, data + offset);
2999
3000        }
3001
3002        /* only update touch if we actually have a touchpad and touch data changed */
3003        if (wacom->touch_input && touch_changed) {
3004                input_mt_sync_frame(wacom->touch_input);
3005                wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3006        }
3007
3008        return 1;
3009}
3010
3011static int wacom_bpt_pen(struct wacom_wac *wacom)
3012{
3013        struct wacom_features *features = &wacom->features;
3014        struct input_dev *input = wacom->pen_input;
3015        unsigned char *data = wacom->data;
3016        int x = 0, y = 0, p = 0, d = 0;
3017        bool pen = false, btn1 = false, btn2 = false;
3018        bool range, prox, rdy;
3019
3020        if (data[0] != WACOM_REPORT_PENABLED)
3021            return 0;
3022
3023        range = (data[1] & 0x80) == 0x80;
3024        prox = (data[1] & 0x40) == 0x40;
3025        rdy = (data[1] & 0x20) == 0x20;
3026
3027        wacom->shared->stylus_in_proximity = range;
3028        if (delay_pen_events(wacom))
3029                return 0;
3030
3031        if (rdy) {
3032                p = le16_to_cpup((__le16 *)&data[6]);
3033                pen = data[1] & 0x01;
3034                btn1 = data[1] & 0x02;
3035                btn2 = data[1] & 0x04;
3036        }
3037        if (prox) {
3038                x = le16_to_cpup((__le16 *)&data[2]);
3039                y = le16_to_cpup((__le16 *)&data[4]);
3040
3041                if (data[1] & 0x08) {
3042                        wacom->tool[0] = BTN_TOOL_RUBBER;
3043                        wacom->id[0] = ERASER_DEVICE_ID;
3044                } else {
3045                        wacom->tool[0] = BTN_TOOL_PEN;
3046                        wacom->id[0] = STYLUS_DEVICE_ID;
3047                }
3048                wacom->reporting_data = true;
3049        }
3050        if (range) {
3051                /*
3052                 * Convert distance from out prox to distance from tablet.
3053                 * distance will be greater than distance_max once
3054                 * touching and applying pressure; do not report negative
3055                 * distance.
3056                 */
3057                if (data[8] <= features->distance_max)
3058                        d = features->distance_max - data[8];
3059        } else {
3060                wacom->id[0] = 0;
3061        }
3062
3063        if (wacom->reporting_data) {
3064                input_report_key(input, BTN_TOUCH, pen);
3065                input_report_key(input, BTN_STYLUS, btn1);
3066                input_report_key(input, BTN_STYLUS2, btn2);
3067
3068                if (prox || !range) {
3069                        input_report_abs(input, ABS_X, x);
3070                        input_report_abs(input, ABS_Y, y);
3071                }
3072                input_report_abs(input, ABS_PRESSURE, p);
3073                input_report_abs(input, ABS_DISTANCE, d);
3074
3075                input_report_key(input, wacom->tool[0], range); /* PEN or RUBBER */
3076                input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
3077        }
3078
3079        if (!range) {
3080                wacom->reporting_data = false;
3081        }
3082
3083        return 1;
3084}
3085
3086static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
3087{
3088        struct wacom_features *features = &wacom->features;
3089
3090        if ((features->type == INTUOSHT2) &&
3091            (features->device_type & WACOM_DEVICETYPE_PEN))
3092                return wacom_intuos_irq(wacom);
3093        else if (len == WACOM_PKGLEN_BBTOUCH)
3094                return wacom_bpt_touch(wacom);
3095        else if (len == WACOM_PKGLEN_BBTOUCH3)
3096                return wacom_bpt3_touch(wacom);
3097        else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
3098                return wacom_bpt_pen(wacom);
3099
3100        return 0;
3101}
3102
3103static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
3104                unsigned char *data)
3105{
3106        unsigned char prefix;
3107
3108        /*
3109         * We need to reroute the event from the debug interface to the
3110         * pen interface.
3111         * We need to add the report ID to the actual pen report, so we
3112         * temporary overwrite the first byte to prevent having to kzalloc/kfree
3113         * and memcpy the report.
3114         */
3115        prefix = data[0];
3116        data[0] = WACOM_REPORT_BPAD_PEN;
3117
3118        /*
3119         * actually reroute the event.
3120         * No need to check if wacom->shared->pen is valid, hid_input_report()
3121         * will check for us.
3122         */
3123        hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
3124                         WACOM_PKGLEN_PENABLED, 1);
3125
3126        data[0] = prefix;
3127}
3128
3129static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
3130                unsigned char *data)
3131{
3132        struct input_dev *input = wacom->touch_input;
3133        unsigned char *finger_data, prefix;
3134        unsigned id;
3135        int x, y;
3136        bool valid;
3137
3138        prefix = data[0];
3139
3140        for (id = 0; id < wacom->features.touch_max; id++) {
3141                valid = !!(prefix & BIT(id)) &&
3142                        report_touch_events(wacom);
3143
3144                input_mt_slot(input, id);
3145                input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
3146
3147                if (!valid)
3148                        continue;
3149
3150                finger_data = data + 1 + id * 3;
3151                x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
3152                y = (finger_data[2] << 4) | (finger_data[1] >> 4);
3153
3154                input_report_abs(input, ABS_MT_POSITION_X, x);
3155                input_report_abs(input, ABS_MT_POSITION_Y, y);
3156        }
3157
3158        input_mt_sync_frame(input);
3159
3160        input_report_key(input, BTN_LEFT, prefix & 0x40);
3161        input_report_key(input, BTN_RIGHT, prefix & 0x80);
3162
3163        /* keep touch state for pen event */
3164        wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
3165
3166        return 1;
3167}
3168
3169static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
3170{
3171        unsigned char *data = wacom->data;
3172
3173        if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
3174              (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
3175            (data[0] != WACOM_REPORT_BPAD_TOUCH))
3176                return 0;
3177
3178        if (data[1] & 0x01)
3179                wacom_bamboo_pad_pen_event(wacom, &data[1]);
3180
3181        if (data[1] & 0x02)
3182                return wacom_bamboo_pad_touch_event(wacom, &data[9]);
3183
3184        return 0;
3185}
3186
3187static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
3188{
3189        unsigned char *data = wacom->data;
3190        int connected;
3191
3192        if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
3193                return 0;
3194
3195        connected = data[1] & 0x01;
3196        if (connected) {
3197                int pid, battery, charging;
3198
3199                if ((wacom->shared->type == INTUOSHT ||
3200                    wacom->shared->type == INTUOSHT2) &&
3201                    wacom->shared->touch_input &&
3202                    wacom->shared->touch_max) {
3203                        input_report_switch(wacom->shared->touch_input,
3204                                        SW_MUTE_DEVICE, data[5] & 0x40);
3205                        input_sync(wacom->shared->touch_input);
3206                }
3207
3208                pid = get_unaligned_be16(&data[6]);
3209                battery = (data[5] & 0x3f) * 100 / 31;
3210                charging = !!(data[5] & 0x80);
3211                if (wacom->pid != pid) {
3212                        wacom->pid = pid;
3213                        wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3214                }
3215
3216                wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
3217                                     battery, charging, 1, 0);
3218
3219        } else if (wacom->pid != 0) {
3220                /* disconnected while previously connected */
3221                wacom->pid = 0;
3222                wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3223                wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3224        }
3225
3226        return 0;
3227}
3228
3229static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
3230{
3231        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
3232        struct wacom_features *features = &wacom_wac->features;
3233        unsigned char *data = wacom_wac->data;
3234
3235        if (data[0] != WACOM_REPORT_USB)
3236                return 0;
3237
3238        if ((features->type == INTUOSHT ||
3239            features->type == INTUOSHT2) &&
3240            wacom_wac->shared->touch_input &&
3241            features->touch_max) {
3242                input_report_switch(wacom_wac->shared->touch_input,
3243                                    SW_MUTE_DEVICE, data[8] & 0x40);
3244                input_sync(wacom_wac->shared->touch_input);
3245        }
3246
3247        if (data[9] & 0x02) { /* wireless module is attached */
3248                int battery = (data[8] & 0x3f) * 100 / 31;
3249                bool charging = !!(data[8] & 0x80);
3250
3251                wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
3252                                     battery, charging, battery || charging, 1);
3253
3254                if (!wacom->battery.battery &&
3255                    !(features->quirks & WACOM_QUIRK_BATTERY)) {
3256                        features->quirks |= WACOM_QUIRK_BATTERY;
3257                        wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3258                }
3259        }
3260        else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
3261                 wacom->battery.battery) {
3262                features->quirks &= ~WACOM_QUIRK_BATTERY;
3263                wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3264                wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3265        }
3266        return 0;
3267}
3268
3269void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
3270{
3271        bool sync;
3272
3273        switch (wacom_wac->features.type) {
3274        case PENPARTNER:
3275                sync = wacom_penpartner_irq(wacom_wac);
3276                break;
3277
3278        case PL:
3279                sync = wacom_pl_irq(wacom_wac);
3280                break;
3281
3282        case WACOM_G4:
3283        case GRAPHIRE:
3284        case GRAPHIRE_BT:
3285        case WACOM_MO:
3286                sync = wacom_graphire_irq(wacom_wac);
3287                break;
3288
3289        case PTU:
3290                sync = wacom_ptu_irq(wacom_wac);
3291                break;
3292
3293        case DTU:
3294                sync = wacom_dtu_irq(wacom_wac);
3295                break;
3296
3297        case DTUS:
3298        case DTUSX:
3299                sync = wacom_dtus_irq(wacom_wac);
3300                break;
3301
3302        case INTUOS:
3303        case INTUOS3S:
3304        case INTUOS3:
3305        case INTUOS3L:
3306        case INTUOS4S:
3307        case INTUOS4:
3308        case INTUOS4L:
3309        case CINTIQ:
3310        case WACOM_BEE:
3311        case WACOM_13HD:
3312        case WACOM_21UX2:
3313        case WACOM_22HD:
3314        case WACOM_24HD:
3315        case WACOM_27QHD:
3316        case DTK:
3317        case CINTIQ_HYBRID:
3318        case CINTIQ_COMPANION_2:
3319                sync = wacom_intuos_irq(wacom_wac);
3320                break;
3321
3322        case INTUOS4WL:
3323                sync = wacom_intuos_bt_irq(wacom_wac, len);
3324                break;
3325
3326        case WACOM_24HDT:
3327        case WACOM_27QHDT:
3328                sync = wacom_24hdt_irq(wacom_wac);
3329                break;
3330
3331        case INTUOS5S:
3332        case INTUOS5:
3333        case INTUOS5L:
3334        case INTUOSPS:
3335        case INTUOSPM:
3336        case INTUOSPL:
3337                if (len == WACOM_PKGLEN_BBTOUCH3)
3338                        sync = wacom_bpt3_touch(wacom_wac);
3339                else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3340                        sync = wacom_status_irq(wacom_wac, len);
3341                else
3342                        sync = wacom_intuos_irq(wacom_wac);
3343                break;
3344
3345        case INTUOSP2_BT:
3346        case INTUOSP2S_BT:
3347        case INTUOSHT3_BT:
3348                sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
3349                break;
3350
3351        case TABLETPC:
3352        case TABLETPCE:
3353        case TABLETPC2FG:
3354        case MTSCREEN:
3355        case MTTPC:
3356        case MTTPC_B:
3357                sync = wacom_tpc_irq(wacom_wac, len);
3358                break;
3359
3360        case BAMBOO_PT:
3361        case BAMBOO_PEN:
3362        case BAMBOO_TOUCH:
3363        case INTUOSHT:
3364        case INTUOSHT2:
3365                if (wacom_wac->data[0] == WACOM_REPORT_USB)
3366                        sync = wacom_status_irq(wacom_wac, len);
3367                else
3368                        sync = wacom_bpt_irq(wacom_wac, len);
3369                break;
3370
3371        case BAMBOO_PAD:
3372                sync = wacom_bamboo_pad_irq(wacom_wac, len);
3373                break;
3374
3375        case WIRELESS:
3376                sync = wacom_wireless_irq(wacom_wac, len);
3377                break;
3378
3379        case REMOTE:
3380                sync = false;
3381                if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
3382                        wacom_remote_status_irq(wacom_wac, len);
3383                else
3384                        sync = wacom_remote_irq(wacom_wac, len);
3385                break;
3386
3387        default:
3388                sync = false;
3389                break;
3390        }
3391
3392        if (sync) {
3393                if (wacom_wac->pen_input)
3394                        input_sync(wacom_wac->pen_input);
3395                if (wacom_wac->touch_input)
3396                        input_sync(wacom_wac->touch_input);
3397                if (wacom_wac->pad_input)
3398                        input_sync(wacom_wac->pad_input);
3399        }
3400}
3401
3402static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
3403{
3404        struct input_dev *input_dev = wacom_wac->pen_input;
3405
3406        input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3407
3408        __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3409        __set_bit(BTN_STYLUS, input_dev->keybit);
3410        __set_bit(BTN_STYLUS2, input_dev->keybit);
3411
3412        input_set_abs_params(input_dev, ABS_DISTANCE,
3413                             0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
3414}
3415
3416static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3417{
3418        struct input_dev *input_dev = wacom_wac->pen_input;
3419        struct wacom_features *features = &wacom_wac->features;
3420
3421        wacom_setup_basic_pro_pen(wacom_wac);
3422
3423        __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3424        __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3425        __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3426        __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3427
3428        input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
3429        input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
3430        input_abs_set_res(input_dev, ABS_TILT_X, 57);
3431        input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
3432        input_abs_set_res(input_dev, ABS_TILT_Y, 57);
3433}
3434
3435static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3436{
3437        struct input_dev *input_dev = wacom_wac->pen_input;
3438
3439        input_set_capability(input_dev, EV_REL, REL_WHEEL);
3440
3441        wacom_setup_cintiq(wacom_wac);
3442
3443        __set_bit(BTN_LEFT, input_dev->keybit);
3444        __set_bit(BTN_RIGHT, input_dev->keybit);
3445        __set_bit(BTN_MIDDLE, input_dev->keybit);
3446        __set_bit(BTN_SIDE, input_dev->keybit);
3447        __set_bit(BTN_EXTRA, input_dev->keybit);
3448        __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3449        __set_bit(BTN_TOOL_LENS, input_dev->keybit);
3450
3451        input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
3452        input_abs_set_res(input_dev, ABS_RZ, 287);
3453        input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3454}
3455
3456void wacom_setup_device_quirks(struct wacom *wacom)
3457{
3458        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
3459        struct wacom_features *features = &wacom->wacom_wac.features;
3460
3461        /* The pen and pad share the same interface on most devices */
3462        if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3463            features->type == DTUS ||
3464            (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
3465                if (features->device_type & WACOM_DEVICETYPE_PEN)
3466                        features->device_type |= WACOM_DEVICETYPE_PAD;
3467        }
3468
3469        /* touch device found but size is not defined. use default */
3470        if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
3471                features->x_max = 1023;
3472                features->y_max = 1023;
3473        }
3474
3475        /*
3476         * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3477         * touch interface in its HID descriptor. If this is the touch
3478         * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3479         * tablet values.
3480         */
3481        if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3482                (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
3483                if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3484                        if (features->touch_max)
3485                                features->device_type |= WACOM_DEVICETYPE_TOUCH;
3486                        if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
3487                                features->device_type |= WACOM_DEVICETYPE_PAD;
3488
3489                        if (features->type == INTUOSHT2) {
3490                                features->x_max = features->x_max / 10;
3491                                features->y_max = features->y_max / 10;
3492                        }
3493                        else {
3494                                features->x_max = 4096;
3495                                features->y_max = 4096;
3496                        }
3497                }
3498                else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3499                        features->device_type |= WACOM_DEVICETYPE_PAD;
3500                }
3501        }
3502
3503        /*
3504         * Hack for the Bamboo One:
3505         * the device presents a PAD/Touch interface as most Bamboos and even
3506         * sends ghosts PAD data on it. However, later, we must disable this
3507         * ghost interface, and we can not detect it unless we set it here
3508         * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3509         */
3510        if (features->type == BAMBOO_PEN &&
3511            features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3512                features->device_type |= WACOM_DEVICETYPE_PAD;
3513
3514        /*
3515         * Raw Wacom-mode pen and touch events both come from interface
3516         * 0, whose HID descriptor has an application usage of 0xFF0D
3517         * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
3518         * out through the HID_GENERIC device created for interface 1,
3519         * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
3520         */
3521        if (features->type == BAMBOO_PAD)
3522                features->device_type = WACOM_DEVICETYPE_TOUCH;
3523
3524        if (features->type == REMOTE)
3525                features->device_type = WACOM_DEVICETYPE_PAD;
3526
3527        if (features->type == INTUOSP2_BT ||
3528            features->type == INTUOSP2S_BT) {
3529                features->device_type |= WACOM_DEVICETYPE_PEN |
3530                                         WACOM_DEVICETYPE_PAD |
3531                                         WACOM_DEVICETYPE_TOUCH;
3532                features->quirks |= WACOM_QUIRK_BATTERY;
3533        }
3534
3535        if (features->type == INTUOSHT3_BT) {
3536                features->device_type |= WACOM_DEVICETYPE_PEN |
3537                                         WACOM_DEVICETYPE_PAD;
3538                features->quirks |= WACOM_QUIRK_BATTERY;
3539        }
3540
3541        switch (features->type) {
3542        case PL:
3543        case DTU:
3544        case DTUS:
3545        case DTUSX:
3546        case WACOM_21UX2:
3547        case WACOM_22HD:
3548        case DTK:
3549        case WACOM_24HD:
3550        case WACOM_27QHD:
3551        case CINTIQ_HYBRID:
3552        case CINTIQ_COMPANION_2:
3553        case CINTIQ:
3554        case WACOM_BEE:
3555        case WACOM_13HD:
3556        case WACOM_24HDT:
3557        case WACOM_27QHDT:
3558        case TABLETPC:
3559        case TABLETPCE:
3560        case TABLETPC2FG:
3561        case MTSCREEN:
3562        case MTTPC:
3563        case MTTPC_B:
3564                features->device_type |= WACOM_DEVICETYPE_DIRECT;
3565                break;
3566        }
3567
3568        if (wacom->hdev->bus == BUS_BLUETOOTH)
3569                features->quirks |= WACOM_QUIRK_BATTERY;
3570
3571        /* quirk for bamboo touch with 2 low res touches */
3572        if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
3573            features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3574                features->x_max <<= 5;
3575                features->y_max <<= 5;
3576                features->x_fuzz <<= 5;
3577                features->y_fuzz <<= 5;
3578                features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
3579        }
3580
3581        if (features->type == WIRELESS) {
3582                if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
3583                        features->quirks |= WACOM_QUIRK_BATTERY;
3584                }
3585        }
3586
3587        if (features->type == REMOTE)
3588                features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
3589
3590        /* HID descriptor for DTK-2451 / DTH-2452 claims to report lots
3591         * of things it shouldn't. Lets fix up the damage...
3592         */
3593        if (wacom->hdev->product == 0x382 || wacom->hdev->product == 0x37d) {
3594                features->quirks &= ~WACOM_QUIRK_TOOLSERIAL;
3595                __clear_bit(BTN_TOOL_BRUSH, wacom_wac->pen_input->keybit);
3596                __clear_bit(BTN_TOOL_PENCIL, wacom_wac->pen_input->keybit);
3597                __clear_bit(BTN_TOOL_AIRBRUSH, wacom_wac->pen_input->keybit);
3598                __clear_bit(ABS_Z, wacom_wac->pen_input->absbit);
3599                __clear_bit(ABS_DISTANCE, wacom_wac->pen_input->absbit);
3600                __clear_bit(ABS_TILT_X, wacom_wac->pen_input->absbit);
3601                __clear_bit(ABS_TILT_Y, wacom_wac->pen_input->absbit);
3602                __clear_bit(ABS_WHEEL, wacom_wac->pen_input->absbit);
3603                __clear_bit(ABS_MISC, wacom_wac->pen_input->absbit);
3604                __clear_bit(MSC_SERIAL, wacom_wac->pen_input->mscbit);
3605                __clear_bit(EV_MSC, wacom_wac->pen_input->evbit);
3606        }
3607}
3608
3609int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
3610                                   struct wacom_wac *wacom_wac)
3611{
3612        struct wacom_features *features = &wacom_wac->features;
3613
3614        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3615
3616        if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3617                return -ENODEV;
3618
3619        if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3620                __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3621        else
3622                __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3623
3624        if (features->type == HID_GENERIC) {
3625                /* setup has already been done; apply otherwise-undetectible quirks */
3626                input_set_capability(input_dev, EV_KEY, BTN_STYLUS3);
3627                return 0;
3628        }
3629
3630        __set_bit(BTN_TOUCH, input_dev->keybit);
3631        __set_bit(ABS_MISC, input_dev->absbit);
3632
3633        input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3634                             features->x_max - features->offset_right,
3635                             features->x_fuzz, 0);
3636        input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3637                             features->y_max - features->offset_bottom,
3638                             features->y_fuzz, 0);
3639        input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3640                features->pressure_max, features->pressure_fuzz, 0);
3641
3642        /* penabled devices have fixed resolution for each model */
3643        input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3644        input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3645
3646        switch (features->type) {
3647        case GRAPHIRE_BT:
3648                __clear_bit(ABS_MISC, input_dev->absbit);
3649                /* fall through */
3650
3651        case WACOM_MO:
3652        case WACOM_G4:
3653                input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3654                                              features->distance_max,
3655                                              features->distance_fuzz, 0);
3656                /* fall through */
3657
3658        case GRAPHIRE:
3659                input_set_capability(input_dev, EV_REL, REL_WHEEL);
3660
3661                __set_bit(BTN_LEFT, input_dev->keybit);
3662                __set_bit(BTN_RIGHT, input_dev->keybit);
3663                __set_bit(BTN_MIDDLE, input_dev->keybit);
3664
3665                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3666                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3667                __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3668                __set_bit(BTN_STYLUS, input_dev->keybit);
3669                __set_bit(BTN_STYLUS2, input_dev->keybit);
3670                break;
3671
3672        case WACOM_27QHD:
3673        case WACOM_24HD:
3674        case DTK:
3675        case WACOM_22HD:
3676        case WACOM_21UX2:
3677        case WACOM_BEE:
3678        case CINTIQ:
3679        case WACOM_13HD:
3680        case CINTIQ_HYBRID:
3681        case CINTIQ_COMPANION_2:
3682                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3683                input_abs_set_res(input_dev, ABS_Z, 287);
3684                wacom_setup_cintiq(wacom_wac);
3685                break;
3686
3687        case INTUOS3:
3688        case INTUOS3L:
3689        case INTUOS3S:
3690        case INTUOS4:
3691        case INTUOS4WL:
3692        case INTUOS4L:
3693        case INTUOS4S:
3694                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3695                input_abs_set_res(input_dev, ABS_Z, 287);
3696                /* fall through */
3697
3698        case INTUOS:
3699                wacom_setup_intuos(wacom_wac);
3700                break;
3701
3702        case INTUOS5:
3703        case INTUOS5L:
3704        case INTUOSPM:
3705        case INTUOSPL:
3706        case INTUOS5S:
3707        case INTUOSPS:
3708        case INTUOSP2_BT:
3709        case INTUOSP2S_BT:
3710                input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3711                                      features->distance_max,
3712                                      features->distance_fuzz, 0);
3713
3714                input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3715                input_abs_set_res(input_dev, ABS_Z, 287);
3716
3717                wacom_setup_intuos(wacom_wac);
3718                break;
3719
3720        case WACOM_24HDT:
3721        case WACOM_27QHDT:
3722        case MTSCREEN:
3723        case MTTPC:
3724        case MTTPC_B:
3725        case TABLETPC2FG:
3726        case TABLETPC:
3727        case TABLETPCE:
3728                __clear_bit(ABS_MISC, input_dev->absbit);
3729                /* fall through */
3730
3731        case DTUS:
3732        case DTUSX:
3733        case PL:
3734        case DTU:
3735                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3736                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3737                __set_bit(BTN_STYLUS, input_dev->keybit);
3738                __set_bit(BTN_STYLUS2, input_dev->keybit);
3739                break;
3740
3741        case PTU:
3742                __set_bit(BTN_STYLUS2, input_dev->keybit);
3743                /* fall through */
3744
3745        case PENPARTNER:
3746                __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3747                __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3748                __set_bit(BTN_STYLUS, input_dev->keybit);
3749                break;
3750
3751        case INTUOSHT:
3752        case BAMBOO_PT:
3753        case BAMBOO_PEN:
3754        case INTUOSHT2:
3755        case INTUOSHT3_BT:
3756                if (features->type == INTUOSHT2 ||
3757                    features->type == INTUOSHT3_BT) {
3758                        wacom_setup_basic_pro_pen(wacom_wac);
3759                } else {
3760                        __clear_bit(ABS_MISC, input_dev->absbit);
3761                        __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3762                        __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3763                        __set_bit(BTN_STYLUS, input_dev->keybit);
3764                        __set_bit(BTN_STYLUS2, input_dev->keybit);
3765                        input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3766                                      features->distance_max,
3767                                      features->distance_fuzz, 0);
3768                }
3769                break;
3770        case BAMBOO_PAD:
3771                __clear_bit(ABS_MISC, input_dev->absbit);
3772                break;
3773        }
3774        return 0;
3775}
3776
3777int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3778                                         struct wacom_wac *wacom_wac)
3779{
3780        struct wacom_features *features = &wacom_wac->features;
3781
3782        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3783
3784        if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3785                return -ENODEV;
3786
3787        if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3788                __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3789        else
3790                __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3791
3792        if (features->type == HID_GENERIC)
3793                /* setup has already been done */
3794                return 0;
3795
3796        __set_bit(BTN_TOUCH, input_dev->keybit);
3797
3798        if (features->touch_max == 1) {
3799                input_set_abs_params(input_dev, ABS_X, 0,
3800                        features->x_max, features->x_fuzz, 0);
3801                input_set_abs_params(input_dev, ABS_Y, 0,
3802                        features->y_max, features->y_fuzz, 0);
3803                input_abs_set_res(input_dev, ABS_X,
3804                                  features->x_resolution);
3805                input_abs_set_res(input_dev, ABS_Y,
3806                                  features->y_resolution);
3807        }
3808        else if (features->touch_max > 1) {
3809                input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3810                        features->x_max, features->x_fuzz, 0);
3811                input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3812                        features->y_max, features->y_fuzz, 0);
3813                input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3814                                  features->x_resolution);
3815                input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3816                                  features->y_resolution);
3817        }
3818
3819        switch (features->type) {
3820        case INTUOSP2_BT:
3821        case INTUOSP2S_BT:
3822                input_dev->evbit[0] |= BIT_MASK(EV_SW);
3823                __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3824
3825                if (wacom_wac->shared->touch->product == 0x361) {
3826                        input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3827                                             0, 12440, 4, 0);
3828                        input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3829                                             0, 8640, 4, 0);
3830                }
3831                else if (wacom_wac->shared->touch->product == 0x360) {
3832                        input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3833                                             0, 8960, 4, 0);
3834                        input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3835                                             0, 5920, 4, 0);
3836                }
3837                else if (wacom_wac->shared->touch->product == 0x393) {
3838                        input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3839                                             0, 6400, 4, 0);
3840                        input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3841                                             0, 4000, 4, 0);
3842                }
3843                input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3844                input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 40);
3845
3846                /* fall through */
3847
3848        case INTUOS5:
3849        case INTUOS5L:
3850        case INTUOSPM:
3851        case INTUOSPL:
3852        case INTUOS5S:
3853        case INTUOSPS:
3854                input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3855                input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
3856                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3857                break;
3858
3859        case WACOM_24HDT:
3860                input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3861                input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
3862                input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
3863                input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
3864                /* fall through */
3865
3866        case WACOM_27QHDT:
3867                if (wacom_wac->shared->touch->product == 0x32C ||
3868                    wacom_wac->shared->touch->product == 0xF6) {
3869                        input_dev->evbit[0] |= BIT_MASK(EV_SW);
3870                        __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3871                        wacom_wac->shared->has_mute_touch_switch = true;
3872                }
3873                /* fall through */
3874
3875        case MTSCREEN:
3876        case MTTPC:
3877        case MTTPC_B:
3878        case TABLETPC2FG:
3879                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
3880                /*fall through */
3881
3882        case TABLETPC:
3883        case TABLETPCE:
3884                break;
3885
3886        case INTUOSHT:
3887        case INTUOSHT2:
3888                input_dev->evbit[0] |= BIT_MASK(EV_SW);
3889                __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3890                /* fall through */
3891
3892        case BAMBOO_PT:
3893        case BAMBOO_TOUCH:
3894                if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3895                        input_set_abs_params(input_dev,
3896                                     ABS_MT_TOUCH_MAJOR,
3897                                     0, features->x_max, 0, 0);
3898                        input_set_abs_params(input_dev,
3899                                     ABS_MT_TOUCH_MINOR,
3900                                     0, features->y_max, 0, 0);
3901                }
3902                input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3903                break;
3904
3905        case BAMBOO_PAD:
3906                input_mt_init_slots(input_dev, features->touch_max,
3907                                    INPUT_MT_POINTER);
3908                __set_bit(BTN_LEFT, input_dev->keybit);
3909                __set_bit(BTN_RIGHT, input_dev->keybit);
3910                break;
3911        }
3912        return 0;
3913}
3914
3915static int wacom_numbered_button_to_key(int n)
3916{
3917        if (n < 10)
3918                return BTN_0 + n;
3919        else if (n < 16)
3920                return BTN_A + (n-10);
3921        else if (n < 18)
3922                return BTN_BASE + (n-16);
3923        else
3924                return 0;
3925}
3926
3927static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
3928                                int button_count)
3929{
3930        int i;
3931
3932        for (i = 0; i < button_count; i++) {
3933                int key = wacom_numbered_button_to_key(i);
3934
3935                if (key)
3936                        __set_bit(key, input_dev->keybit);
3937        }
3938}
3939
3940static void wacom_24hd_update_leds(struct wacom *wacom, int mask, int group)
3941{
3942        struct wacom_led *led;
3943        int i;
3944        bool updated = false;
3945
3946        /*
3947         * 24HD has LED group 1 to the left and LED group 0 to the right.
3948         * So group 0 matches the second half of the buttons and thus the mask
3949         * needs to be shifted.
3950         */
3951        if (group == 0)
3952                mask >>= 8;
3953
3954        for (i = 0; i < 3; i++) {
3955                led = wacom_led_find(wacom, group, i);
3956                if (!led) {
3957                        hid_err(wacom->hdev, "can't find LED %d in group %d\n",
3958                                i, group);
3959                        continue;
3960                }
3961                if (!updated && mask & BIT(i)) {
3962                        led->held = true;
3963                        led_trigger_event(&led->trigger, LED_FULL);
3964                } else {
3965                        led->held = false;
3966                }
3967        }
3968}
3969
3970static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
3971                                 int mask, int group)
3972{
3973        int group_button;
3974
3975        /*
3976         * 21UX2 has LED group 1 to the left and LED group 0
3977         * to the right. We need to reverse the group to match this
3978         * historical behavior.
3979         */
3980        if (wacom->wacom_wac.features.type == WACOM_21UX2)
3981                group = 1 - group;
3982
3983        group_button = group * (button_count/wacom->led.count);
3984
3985        if (wacom->wacom_wac.features.type == INTUOSP2_BT)
3986                group_button = 8;
3987
3988        return mask & (1 << group_button);
3989}
3990
3991static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
3992                             int group)
3993{
3994        struct wacom_led *led, *next_led;
3995        int cur;
3996        bool pressed;
3997
3998        if (wacom->wacom_wac.features.type == WACOM_24HD)
3999                return wacom_24hd_update_leds(wacom, mask, group);
4000
4001        pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
4002        cur = wacom->led.groups[group].select;
4003
4004        led = wacom_led_find(wacom, group, cur);
4005        if (!led) {
4006                hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
4007                        cur, group);
4008                return;
4009        }
4010
4011        if (!pressed) {
4012                led->held = false;
4013                return;
4014        }
4015
4016        if (led->held && pressed)
4017                return;
4018
4019        next_led = wacom_led_next(wacom, led);
4020        if (!next_led) {
4021                hid_err(wacom->hdev, "can't find next LED in group %d\n",
4022                        group);
4023                return;
4024        }
4025        if (next_led == led)
4026                return;
4027
4028        next_led->held = true;
4029        led_trigger_event(&next_led->trigger,
4030                          wacom_leds_brightness_get(next_led));
4031}
4032
4033static void wacom_report_numbered_buttons(struct input_dev *input_dev,
4034                                int button_count, int mask)
4035{
4036        struct wacom *wacom = input_get_drvdata(input_dev);
4037        int i;
4038
4039        for (i = 0; i < wacom->led.count; i++)
4040                wacom_update_led(wacom,  button_count, mask, i);
4041
4042        for (i = 0; i < button_count; i++) {
4043                int key = wacom_numbered_button_to_key(i);
4044
4045                if (key)
4046                        input_report_key(input_dev, key, mask & (1 << i));
4047        }
4048}
4049
4050int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
4051                                   struct wacom_wac *wacom_wac)
4052{
4053        struct wacom_features *features = &wacom_wac->features;
4054
4055        if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
4056                features->device_type |= WACOM_DEVICETYPE_PAD;
4057
4058        if (!(features->device_type & WACOM_DEVICETYPE_PAD))
4059                return -ENODEV;
4060
4061        if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
4062                return -ENODEV;
4063
4064        input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4065
4066        /* kept for making legacy xf86-input-wacom working with the wheels */
4067        __set_bit(ABS_MISC, input_dev->absbit);
4068
4069        /* kept for making legacy xf86-input-wacom accepting the pad */
4070        if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
4071              input_dev->absinfo[ABS_X].maximum)))
4072                input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
4073        if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
4074              input_dev->absinfo[ABS_Y].maximum)))
4075                input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
4076
4077        /* kept for making udev and libwacom accepting the pad */
4078        __set_bit(BTN_STYLUS, input_dev->keybit);
4079
4080        wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
4081
4082        switch (features->type) {
4083
4084        case CINTIQ_HYBRID:
4085        case CINTIQ_COMPANION_2:
4086        case DTK:
4087        case DTUS:
4088        case GRAPHIRE_BT:
4089                break;
4090
4091        case WACOM_MO:
4092                __set_bit(BTN_BACK, input_dev->keybit);
4093                __set_bit(BTN_LEFT, input_dev->keybit);
4094                __set_bit(BTN_FORWARD, input_dev->keybit);
4095                __set_bit(BTN_RIGHT, input_dev->keybit);
4096                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4097                break;
4098
4099        case WACOM_G4:
4100                __set_bit(BTN_BACK, input_dev->keybit);
4101                __set_bit(BTN_FORWARD, input_dev->keybit);
4102                input_set_capability(input_dev, EV_REL, REL_WHEEL);
4103                break;
4104
4105        case WACOM_24HD:
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_ONSCREEN_KEYBOARD, input_dev->keybit);
4111                __set_bit(KEY_INFO, input_dev->keybit);
4112
4113                if (!features->oPid)
4114                        __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4115
4116                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4117                input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
4118                break;
4119
4120        case WACOM_27QHD:
4121                __set_bit(KEY_PROG1, input_dev->keybit);
4122                __set_bit(KEY_PROG2, input_dev->keybit);
4123                __set_bit(KEY_PROG3, input_dev->keybit);
4124
4125                __set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4126                __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4127
4128                if (!features->oPid)
4129                        __set_bit(KEY_CONTROLPANEL, input_dev->keybit);
4130                input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
4131                input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
4132                input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
4133                input_abs_set_res(input_dev, ABS_Y, 1024);
4134                input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
4135                input_abs_set_res(input_dev, ABS_Z, 1024);
4136                __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
4137                break;
4138
4139        case WACOM_22HD:
4140                __set_bit(KEY_PROG1, input_dev->keybit);
4141                __set_bit(KEY_PROG2, input_dev->keybit);
4142                __set_bit(KEY_PROG3, input_dev->keybit);
4143
4144                __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4145                __set_bit(KEY_INFO, input_dev->keybit);
4146                /* fall through */
4147
4148        case WACOM_21UX2:
4149        case WACOM_BEE:
4150        case CINTIQ:
4151                input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4152                input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4153                break;
4154
4155        case WACOM_13HD:
4156                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4157                break;
4158
4159        case INTUOS3:
4160        case INTUOS3L:
4161                input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4162                /* fall through */
4163
4164        case INTUOS3S:
4165                input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4166                break;
4167
4168        case INTUOS5:
4169        case INTUOS5L:
4170        case INTUOSPM:
4171        case INTUOSPL:
4172        case INTUOS5S:
4173        case INTUOSPS:
4174        case INTUOSP2_BT:
4175        case INTUOSP2S_BT:
4176                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4177                break;
4178
4179        case INTUOS4WL:
4180                /*
4181                 * For Bluetooth devices, the udev rule does not work correctly
4182                 * for pads unless we add a stylus capability, which forces
4183                 * ID_INPUT_TABLET to be set.
4184                 */
4185                __set_bit(BTN_STYLUS, input_dev->keybit);
4186                /* fall through */
4187
4188        case INTUOS4:
4189        case INTUOS4L:
4190        case INTUOS4S:
4191                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4192                break;
4193
4194        case INTUOSHT:
4195        case BAMBOO_PT:
4196        case BAMBOO_TOUCH:
4197        case INTUOSHT2:
4198                __clear_bit(ABS_MISC, input_dev->absbit);
4199
4200                __set_bit(BTN_LEFT, input_dev->keybit);
4201                __set_bit(BTN_FORWARD, input_dev->keybit);
4202                __set_bit(BTN_BACK, input_dev->keybit);
4203                __set_bit(BTN_RIGHT, input_dev->keybit);
4204
4205                break;
4206
4207        case REMOTE:
4208                input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
4209                input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4210                break;
4211
4212        case INTUOSHT3_BT:
4213        case HID_GENERIC:
4214                break;
4215
4216        default:
4217                /* no pad supported */
4218                return -ENODEV;
4219        }
4220        return 0;
4221}
4222
4223static const struct wacom_features wacom_features_0x00 =
4224        { "Wacom Penpartner", 5040, 3780, 255, 0,
4225          PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4226static const struct wacom_features wacom_features_0x10 =
4227        { "Wacom Graphire", 10206, 7422, 511, 63,
4228          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4229static const struct wacom_features wacom_features_0x81 =
4230        { "Wacom Graphire BT", 16704, 12064, 511, 32,
4231          GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
4232static const struct wacom_features wacom_features_0x11 =
4233        { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
4234          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4235static const struct wacom_features wacom_features_0x12 =
4236        { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
4237          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4238static const struct wacom_features wacom_features_0x13 =
4239        { "Wacom Graphire3", 10208, 7424, 511, 63,
4240          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4241static const struct wacom_features wacom_features_0x14 =
4242        { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
4243          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4244static const struct wacom_features wacom_features_0x15 =
4245        { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
4246          WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4247static const struct wacom_features wacom_features_0x16 =
4248        { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
4249          WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4250static const struct wacom_features wacom_features_0x17 =
4251        { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
4252          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4253static const struct wacom_features wacom_features_0x18 =
4254        { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
4255          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4256static const struct wacom_features wacom_features_0x19 =
4257        { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
4258          GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4259static const struct wacom_features wacom_features_0x60 =
4260        { "Wacom Volito", 5104, 3712, 511, 63,
4261          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4262static const struct wacom_features wacom_features_0x61 =
4263        { "Wacom PenStation2", 3250, 2320, 255, 63,
4264          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4265static const struct wacom_features wacom_features_0x62 =
4266        { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
4267          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4268static const struct wacom_features wacom_features_0x63 =
4269        { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
4270          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4271static const struct wacom_features wacom_features_0x64 =
4272        { "Wacom PenPartner2", 3250, 2320, 511, 63,
4273          GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4274static const struct wacom_features wacom_features_0x65 =
4275        { "Wacom Bamboo", 14760, 9225, 511, 63,
4276          WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4277static const struct wacom_features wacom_features_0x69 =
4278        { "Wacom Bamboo1", 5104, 3712, 511, 63,
4279          GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4280static const struct wacom_features wacom_features_0x6A =
4281        { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
4282          GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4283static const struct wacom_features wacom_features_0x6B =
4284        { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
4285          GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4286static const struct wacom_features wacom_features_0x20 =
4287        { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
4288          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4289static const struct wacom_features wacom_features_0x21 =
4290        { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
4291          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4292static const struct wacom_features wacom_features_0x22 =
4293        { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
4294          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4295static const struct wacom_features wacom_features_0x23 =
4296        { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
4297          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4298static const struct wacom_features wacom_features_0x24 =
4299        { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
4300          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4301static const struct wacom_features wacom_features_0x30 =
4302        { "Wacom PL400", 5408, 4056, 255, 0,
4303          PL, WACOM_PL_RES, WACOM_PL_RES };
4304static const struct wacom_features wacom_features_0x31 =
4305        { "Wacom PL500", 6144, 4608, 255, 0,
4306          PL, WACOM_PL_RES, WACOM_PL_RES };
4307static const struct wacom_features wacom_features_0x32 =
4308        { "Wacom PL600", 6126, 4604, 255, 0,
4309          PL, WACOM_PL_RES, WACOM_PL_RES };
4310static const struct wacom_features wacom_features_0x33 =
4311        { "Wacom PL600SX", 6260, 5016, 255, 0,
4312          PL, WACOM_PL_RES, WACOM_PL_RES };
4313static const struct wacom_features wacom_features_0x34 =
4314        { "Wacom PL550", 6144, 4608, 511, 0,
4315          PL, WACOM_PL_RES, WACOM_PL_RES };
4316static const struct wacom_features wacom_features_0x35 =
4317        { "Wacom PL800", 7220, 5780, 511, 0,
4318          PL, WACOM_PL_RES, WACOM_PL_RES };
4319static const struct wacom_features wacom_features_0x37 =
4320        { "Wacom PL700", 6758, 5406, 511, 0,
4321          PL, WACOM_PL_RES, WACOM_PL_RES };
4322static const struct wacom_features wacom_features_0x38 =
4323        { "Wacom PL510", 6282, 4762, 511, 0,
4324          PL, WACOM_PL_RES, WACOM_PL_RES };
4325static const struct wacom_features wacom_features_0x39 =
4326        { "Wacom DTU710", 34080, 27660, 511, 0,
4327          PL, WACOM_PL_RES, WACOM_PL_RES };
4328static const struct wacom_features wacom_features_0xC4 =
4329        { "Wacom DTF521", 6282, 4762, 511, 0,
4330          PL, WACOM_PL_RES, WACOM_PL_RES };
4331static const struct wacom_features wacom_features_0xC0 =
4332        { "Wacom DTF720", 6858, 5506, 511, 0,
4333          PL, WACOM_PL_RES, WACOM_PL_RES };
4334static const struct wacom_features wacom_features_0xC2 =
4335        { "Wacom DTF720a", 6858, 5506, 511, 0,
4336          PL, WACOM_PL_RES, WACOM_PL_RES };
4337static const struct wacom_features wacom_features_0x03 =
4338        { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
4339          PTU, WACOM_PL_RES, WACOM_PL_RES };
4340static const struct wacom_features wacom_features_0x41 =
4341        { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
4342          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4343static const struct wacom_features wacom_features_0x42 =
4344        { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4345          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4346static const struct wacom_features wacom_features_0x43 =
4347        { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
4348          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4349static const struct wacom_features wacom_features_0x44 =
4350        { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
4351          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4352static const struct wacom_features wacom_features_0x45 =
4353        { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
4354          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4355static const struct wacom_features wacom_features_0xB0 =
4356        { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
4357          INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4358static const struct wacom_features wacom_features_0xB1 =
4359        { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
4360          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4361static const struct wacom_features wacom_features_0xB2 =
4362        { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
4363          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4364static const struct wacom_features wacom_features_0xB3 =
4365        { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
4366          INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4367static const struct wacom_features wacom_features_0xB4 =
4368        { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
4369          INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4370static const struct wacom_features wacom_features_0xB5 =
4371        { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
4372          INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4373static const struct wacom_features wacom_features_0xB7 =
4374        { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
4375          INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4376static const struct wacom_features wacom_features_0xB8 =
4377        { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
4378          INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4379static const struct wacom_features wacom_features_0xB9 =
4380        { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
4381          INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4382static const struct wacom_features wacom_features_0xBA =
4383        { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
4384          INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4385static const struct wacom_features wacom_features_0xBB =
4386        { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
4387          INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4388static const struct wacom_features wacom_features_0xBC =
4389        { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4390          INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4391static const struct wacom_features wacom_features_0xBD =
4392        { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4393          INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4394static const struct wacom_features wacom_features_0x26 =
4395        { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
4396          INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
4397static const struct wacom_features wacom_features_0x27 =
4398        { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
4399          INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4400static const struct wacom_features wacom_features_0x28 =
4401        { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
4402          INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4403static const struct wacom_features wacom_features_0x29 =
4404        { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
4405          INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4406static const struct wacom_features wacom_features_0x2A =
4407        { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
4408          INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4409static const struct wacom_features wacom_features_0x314 =
4410        { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
4411          INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
4412          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4413static const struct wacom_features wacom_features_0x315 =
4414        { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
4415          INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4416          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4417static const struct wacom_features wacom_features_0x317 =
4418        { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
4419          INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4420          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4421static const struct wacom_features wacom_features_0xF4 =
4422        { "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
4423          WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4424          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4425          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4426static const struct wacom_features wacom_features_0xF8 =
4427        { "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
4428          WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4429          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4430          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4431          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
4432static const struct wacom_features wacom_features_0xF6 =
4433        { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
4434          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
4435          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4436static const struct wacom_features wacom_features_0x32A =
4437        { "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
4438          WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4439          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4440          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4441static const struct wacom_features wacom_features_0x32B =
4442        { "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
4443          WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4444          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4445          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4446          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
4447static const struct wacom_features wacom_features_0x32C =
4448        { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
4449          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
4450static const struct wacom_features wacom_features_0x3F =
4451        { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
4452          CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4453static const struct wacom_features wacom_features_0xC5 =
4454        { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
4455          WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4456static const struct wacom_features wacom_features_0xC6 =
4457        { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
4458          WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4459static const struct wacom_features wacom_features_0x304 =
4460        { "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
4461          WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4462          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4463          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4464static const struct wacom_features wacom_features_0x333 =
4465        { "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
4466          WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4467          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4468          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4469          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
4470static const struct wacom_features wacom_features_0x335 =
4471        { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
4472          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
4473          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4474static const struct wacom_features wacom_features_0xC7 =
4475        { "Wacom DTU1931", 37832, 30305, 511, 0,
4476          PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4477static const struct wacom_features wacom_features_0xCE =
4478        { "Wacom DTU2231", 47864, 27011, 511, 0,
4479          DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4480          .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
4481static const struct wacom_features wacom_features_0xF0 =
4482        { "Wacom DTU1631", 34623, 19553, 511, 0,
4483          DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4484static const struct wacom_features wacom_features_0xFB =
4485        { "Wacom DTU1031", 22096, 13960, 511, 0,
4486          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4487          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4488          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4489static const struct wacom_features wacom_features_0x32F =
4490        { "Wacom DTU1031X", 22672, 12928, 511, 0,
4491          DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
4492          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4493          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4494static const struct wacom_features wacom_features_0x336 =
4495        { "Wacom DTU1141", 23672, 13403, 1023, 0,
4496          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4497          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4498          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4499static const struct wacom_features wacom_features_0x57 =
4500        { "Wacom DTK2241", 95840, 54260, 2047, 63,
4501          DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4502          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4503          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4504static const struct wacom_features wacom_features_0x59 = /* Pen */
4505        { "Wacom DTH2242", 95840, 54260, 2047, 63,
4506          DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4507          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4508          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4509          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4510static const struct wacom_features wacom_features_0x5D = /* Touch */
4511        { "Wacom DTH2242",       .type = WACOM_24HDT,
4512          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4513          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4514static const struct wacom_features wacom_features_0xCC =
4515        { "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
4516          WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4517          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4518          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4519static const struct wacom_features wacom_features_0xFA =
4520        { "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
4521          WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4522          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4523          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4524static const struct wacom_features wacom_features_0x5B =
4525        { "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
4526          WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4527          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4528          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4529          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
4530static const struct wacom_features wacom_features_0x5E =
4531        { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
4532          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4533          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4534static const struct wacom_features wacom_features_0x90 =
4535        { "Wacom ISDv4 90", 26202, 16325, 255, 0,
4536          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4537static const struct wacom_features wacom_features_0x93 =
4538        { "Wacom ISDv4 93", 26202, 16325, 255, 0,
4539          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4540static const struct wacom_features wacom_features_0x97 =
4541        { "Wacom ISDv4 97", 26202, 16325, 511, 0,
4542          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4543static const struct wacom_features wacom_features_0x9A =
4544        { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
4545          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4546static const struct wacom_features wacom_features_0x9F =
4547        { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
4548          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4549static const struct wacom_features wacom_features_0xE2 =
4550        { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4551          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4552static const struct wacom_features wacom_features_0xE3 =
4553        { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4554          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4555static const struct wacom_features wacom_features_0xE5 =
4556        { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4557          MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4558static const struct wacom_features wacom_features_0xE6 =
4559        { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4560          TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4561static const struct wacom_features wacom_features_0xEC =
4562        { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
4563          TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4564static const struct wacom_features wacom_features_0xED =
4565        { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
4566          TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4567static const struct wacom_features wacom_features_0xEF =
4568        { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
4569          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4570static const struct wacom_features wacom_features_0x100 =
4571        { "Wacom ISDv4 100", 26202, 16325, 255, 0,
4572          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4573static const struct wacom_features wacom_features_0x101 =
4574        { "Wacom ISDv4 101", 26202, 16325, 255, 0,
4575          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4576static const struct wacom_features wacom_features_0x10D =
4577        { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4578          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4579static const struct wacom_features wacom_features_0x10E =
4580        { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4581          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4582static const struct wacom_features wacom_features_0x10F =
4583        { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4584          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4585static const struct wacom_features wacom_features_0x116 =
4586        { "Wacom ISDv4 116", 26202, 16325, 255, 0,
4587          TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4588static const struct wacom_features wacom_features_0x12C =
4589        { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
4590          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4591static const struct wacom_features wacom_features_0x4001 =
4592        { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4593          MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4594static const struct wacom_features wacom_features_0x4004 =
4595        { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4596          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4597static const struct wacom_features wacom_features_0x5000 =
4598        { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4599          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4600static const struct wacom_features wacom_features_0x5002 =
4601        { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4602          MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4603static const struct wacom_features wacom_features_0x47 =
4604        { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4605          INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4606static const struct wacom_features wacom_features_0x84 =
4607        { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
4608static const struct wacom_features wacom_features_0xD0 =
4609        { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
4610          BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4611static const struct wacom_features wacom_features_0xD1 =
4612        { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4613          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4614static const struct wacom_features wacom_features_0xD2 =
4615        { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4616          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4617static const struct wacom_features wacom_features_0xD3 =
4618        { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4619          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4620static const struct wacom_features wacom_features_0xD4 =
4621        { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
4622          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4623static const struct wacom_features wacom_features_0xD5 =
4624        { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
4625          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4626static const struct wacom_features wacom_features_0xD6 =
4627        { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4628          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4629static const struct wacom_features wacom_features_0xD7 =
4630        { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4631          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4632static const struct wacom_features wacom_features_0xD8 =
4633        { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4634          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4635static const struct wacom_features wacom_features_0xDA =
4636        { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4637          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4638static const struct wacom_features wacom_features_0xDB =
4639        { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4640          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4641static const struct wacom_features wacom_features_0xDD =
4642        { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4643          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4644static const struct wacom_features wacom_features_0xDE =
4645        { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4646          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4647static const struct wacom_features wacom_features_0xDF =
4648        { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4649          BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4650static const struct wacom_features wacom_features_0x300 =
4651        { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
4652          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4653static const struct wacom_features wacom_features_0x301 =
4654        { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
4655          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4656static const struct wacom_features wacom_features_0x302 =
4657        { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4658          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4659          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4660static const struct wacom_features wacom_features_0x303 =
4661        { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4662          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4663          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4664static const struct wacom_features wacom_features_0x30E =
4665        { "Wacom Intuos S", 15200, 9500, 1023, 31,
4666          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4667          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4668static const struct wacom_features wacom_features_0x6004 =
4669        { "ISD-V4", 12800, 8000, 255, 0,
4670          TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4671static const struct wacom_features wacom_features_0x307 =
4672        { "Wacom ISDv5 307", 59552, 33848, 2047, 63,
4673          CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4674          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4675          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4676          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
4677static const struct wacom_features wacom_features_0x309 =
4678        { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
4679          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4680          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4681static const struct wacom_features wacom_features_0x30A =
4682        { "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
4683          CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4684          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4685          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4686          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4687static const struct wacom_features wacom_features_0x30C =
4688        { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4689          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4690          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4691static const struct wacom_features wacom_features_0x318 =
4692        { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4693          .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4694static const struct wacom_features wacom_features_0x319 =
4695        { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4696          .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4697static const struct wacom_features wacom_features_0x325 =
4698        { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4699          CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4700          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4701          WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4702          .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4703static const struct wacom_features wacom_features_0x326 = /* Touch */
4704        { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4705          .oPid = 0x325 };
4706static const struct wacom_features wacom_features_0x323 =
4707        { "Wacom Intuos P M", 21600, 13500, 1023, 31,
4708          INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4709          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4710static const struct wacom_features wacom_features_0x331 =
4711        { "Wacom Express Key Remote", .type = REMOTE,
4712          .numbered_buttons = 18, .check_for_hid_type = true,
4713          .hid_type = HID_TYPE_USBNONE };
4714static const struct wacom_features wacom_features_0x33B =
4715        { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4716          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4717          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4718static const struct wacom_features wacom_features_0x33C =
4719        { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4720          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4721          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4722static const struct wacom_features wacom_features_0x33D =
4723        { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4724          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4725          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4726static const struct wacom_features wacom_features_0x33E =
4727        { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4728          INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4729          .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4730static const struct wacom_features wacom_features_0x343 =
4731        { "Wacom DTK1651", 34816, 19759, 1023, 0,
4732          DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4733          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4734          WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4735static const struct wacom_features wacom_features_0x360 =
4736        { "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
4737          INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4738static const struct wacom_features wacom_features_0x361 =
4739        { "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
4740          INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4741static const struct wacom_features wacom_features_0x377 =
4742        { "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4743          INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4744static const struct wacom_features wacom_features_0x379 =
4745        { "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4746          INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4747static const struct wacom_features wacom_features_0x37A =
4748        { "Wacom One by Wacom S", 15200, 9500, 2047, 63,
4749          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4750static const struct wacom_features wacom_features_0x37B =
4751        { "Wacom One by Wacom M", 21600, 13500, 2047, 63,
4752          BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4753static const struct wacom_features wacom_features_0x393 =
4754        { "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4755          INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4756          .touch_max = 10 };
4757
4758static const struct wacom_features wacom_features_HID_ANY_ID =
4759        { "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
4760
4761#define USB_DEVICE_WACOM(prod)                                          \
4762        HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4763        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4764
4765#define BT_DEVICE_WACOM(prod)                                           \
4766        HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4767        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4768
4769#define I2C_DEVICE_WACOM(prod)                                          \
4770        HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4771        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4772
4773#define USB_DEVICE_LENOVO(prod)                                 \
4774        HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),                     \
4775        .driver_data = (kernel_ulong_t)&wacom_features_##prod
4776
4777const struct hid_device_id wacom_ids[] = {
4778        { USB_DEVICE_WACOM(0x00) },
4779        { USB_DEVICE_WACOM(0x03) },
4780        { USB_DEVICE_WACOM(0x10) },
4781        { USB_DEVICE_WACOM(0x11) },
4782        { USB_DEVICE_WACOM(0x12) },
4783        { USB_DEVICE_WACOM(0x13) },
4784        { USB_DEVICE_WACOM(0x14) },
4785        { USB_DEVICE_WACOM(0x15) },
4786        { USB_DEVICE_WACOM(0x16) },
4787        { USB_DEVICE_WACOM(0x17) },
4788        { USB_DEVICE_WACOM(0x18) },
4789        { USB_DEVICE_WACOM(0x19) },
4790        { USB_DEVICE_WACOM(0x20) },
4791        { USB_DEVICE_WACOM(0x21) },
4792        { USB_DEVICE_WACOM(0x22) },
4793        { USB_DEVICE_WACOM(0x23) },
4794        { USB_DEVICE_WACOM(0x24) },
4795        { USB_DEVICE_WACOM(0x26) },
4796        { USB_DEVICE_WACOM(0x27) },
4797        { USB_DEVICE_WACOM(0x28) },
4798        { USB_DEVICE_WACOM(0x29) },
4799        { USB_DEVICE_WACOM(0x2A) },
4800        { USB_DEVICE_WACOM(0x30) },
4801        { USB_DEVICE_WACOM(0x31) },
4802        { USB_DEVICE_WACOM(0x32) },
4803        { USB_DEVICE_WACOM(0x33) },
4804        { USB_DEVICE_WACOM(0x34) },
4805        { USB_DEVICE_WACOM(0x35) },
4806        { USB_DEVICE_WACOM(0x37) },
4807        { USB_DEVICE_WACOM(0x38) },
4808        { USB_DEVICE_WACOM(0x39) },
4809        { USB_DEVICE_WACOM(0x3F) },
4810        { USB_DEVICE_WACOM(0x41) },
4811        { USB_DEVICE_WACOM(0x42) },
4812        { USB_DEVICE_WACOM(0x43) },
4813        { USB_DEVICE_WACOM(0x44) },
4814        { USB_DEVICE_WACOM(0x45) },
4815        { USB_DEVICE_WACOM(0x47) },
4816        { USB_DEVICE_WACOM(0x57) },
4817        { USB_DEVICE_WACOM(0x59) },
4818        { USB_DEVICE_WACOM(0x5B) },
4819        { USB_DEVICE_WACOM(0x5D) },
4820        { USB_DEVICE_WACOM(0x5E) },
4821        { USB_DEVICE_WACOM(0x60) },
4822        { USB_DEVICE_WACOM(0x61) },
4823        { USB_DEVICE_WACOM(0x62) },
4824        { USB_DEVICE_WACOM(0x63) },
4825        { USB_DEVICE_WACOM(0x64) },
4826        { USB_DEVICE_WACOM(0x65) },
4827        { USB_DEVICE_WACOM(0x69) },
4828        { USB_DEVICE_WACOM(0x6A) },
4829        { USB_DEVICE_WACOM(0x6B) },
4830        { BT_DEVICE_WACOM(0x81) },
4831        { USB_DEVICE_WACOM(0x84) },
4832        { USB_DEVICE_WACOM(0x90) },
4833        { USB_DEVICE_WACOM(0x93) },
4834        { USB_DEVICE_WACOM(0x97) },
4835        { USB_DEVICE_WACOM(0x9A) },
4836        { USB_DEVICE_WACOM(0x9F) },
4837        { USB_DEVICE_WACOM(0xB0) },
4838        { USB_DEVICE_WACOM(0xB1) },
4839        { USB_DEVICE_WACOM(0xB2) },
4840        { USB_DEVICE_WACOM(0xB3) },
4841        { USB_DEVICE_WACOM(0xB4) },
4842        { USB_DEVICE_WACOM(0xB5) },
4843        { USB_DEVICE_WACOM(0xB7) },
4844        { USB_DEVICE_WACOM(0xB8) },
4845        { USB_DEVICE_WACOM(0xB9) },
4846        { USB_DEVICE_WACOM(0xBA) },
4847        { USB_DEVICE_WACOM(0xBB) },
4848        { USB_DEVICE_WACOM(0xBC) },
4849        { BT_DEVICE_WACOM(0xBD) },
4850        { USB_DEVICE_WACOM(0xC0) },
4851        { USB_DEVICE_WACOM(0xC2) },
4852        { USB_DEVICE_WACOM(0xC4) },
4853        { USB_DEVICE_WACOM(0xC5) },
4854        { USB_DEVICE_WACOM(0xC6) },
4855        { USB_DEVICE_WACOM(0xC7) },
4856        { USB_DEVICE_WACOM(0xCC) },
4857        { USB_DEVICE_WACOM(0xCE) },
4858        { USB_DEVICE_WACOM(0xD0) },
4859        { USB_DEVICE_WACOM(0xD1) },
4860        { USB_DEVICE_WACOM(0xD2) },
4861        { USB_DEVICE_WACOM(0xD3) },
4862        { USB_DEVICE_WACOM(0xD4) },
4863        { USB_DEVICE_WACOM(0xD5) },
4864        { USB_DEVICE_WACOM(0xD6) },
4865        { USB_DEVICE_WACOM(0xD7) },
4866        { USB_DEVICE_WACOM(0xD8) },
4867        { USB_DEVICE_WACOM(0xDA) },
4868        { USB_DEVICE_WACOM(0xDB) },
4869        { USB_DEVICE_WACOM(0xDD) },
4870        { USB_DEVICE_WACOM(0xDE) },
4871        { USB_DEVICE_WACOM(0xDF) },
4872        { USB_DEVICE_WACOM(0xE2) },
4873        { USB_DEVICE_WACOM(0xE3) },
4874        { USB_DEVICE_WACOM(0xE5) },
4875        { USB_DEVICE_WACOM(0xE6) },
4876        { USB_DEVICE_WACOM(0xEC) },
4877        { USB_DEVICE_WACOM(0xED) },
4878        { USB_DEVICE_WACOM(0xEF) },
4879        { USB_DEVICE_WACOM(0xF0) },
4880        { USB_DEVICE_WACOM(0xF4) },
4881        { USB_DEVICE_WACOM(0xF6) },
4882        { USB_DEVICE_WACOM(0xF8) },
4883        { USB_DEVICE_WACOM(0xFA) },
4884        { USB_DEVICE_WACOM(0xFB) },
4885        { USB_DEVICE_WACOM(0x100) },
4886        { USB_DEVICE_WACOM(0x101) },
4887        { USB_DEVICE_WACOM(0x10D) },
4888        { USB_DEVICE_WACOM(0x10E) },
4889        { USB_DEVICE_WACOM(0x10F) },
4890        { USB_DEVICE_WACOM(0x116) },
4891        { USB_DEVICE_WACOM(0x12C) },
4892        { USB_DEVICE_WACOM(0x300) },
4893        { USB_DEVICE_WACOM(0x301) },
4894        { USB_DEVICE_WACOM(0x302) },
4895        { USB_DEVICE_WACOM(0x303) },
4896        { USB_DEVICE_WACOM(0x304) },
4897        { USB_DEVICE_WACOM(0x307) },
4898        { USB_DEVICE_WACOM(0x309) },
4899        { USB_DEVICE_WACOM(0x30A) },
4900        { USB_DEVICE_WACOM(0x30C) },
4901        { USB_DEVICE_WACOM(0x30E) },
4902        { USB_DEVICE_WACOM(0x314) },
4903        { USB_DEVICE_WACOM(0x315) },
4904        { USB_DEVICE_WACOM(0x317) },
4905        { USB_DEVICE_WACOM(0x318) },
4906        { USB_DEVICE_WACOM(0x319) },
4907        { USB_DEVICE_WACOM(0x323) },
4908        { USB_DEVICE_WACOM(0x325) },
4909        { USB_DEVICE_WACOM(0x326) },
4910        { USB_DEVICE_WACOM(0x32A) },
4911        { USB_DEVICE_WACOM(0x32B) },
4912        { USB_DEVICE_WACOM(0x32C) },
4913        { USB_DEVICE_WACOM(0x32F) },
4914        { USB_DEVICE_WACOM(0x331) },
4915        { USB_DEVICE_WACOM(0x333) },
4916        { USB_DEVICE_WACOM(0x335) },
4917        { USB_DEVICE_WACOM(0x336) },
4918        { USB_DEVICE_WACOM(0x33B) },
4919        { USB_DEVICE_WACOM(0x33C) },
4920        { USB_DEVICE_WACOM(0x33D) },
4921        { USB_DEVICE_WACOM(0x33E) },
4922        { USB_DEVICE_WACOM(0x343) },
4923        { BT_DEVICE_WACOM(0x360) },
4924        { BT_DEVICE_WACOM(0x361) },
4925        { BT_DEVICE_WACOM(0x377) },
4926        { BT_DEVICE_WACOM(0x379) },
4927        { USB_DEVICE_WACOM(0x37A) },
4928        { USB_DEVICE_WACOM(0x37B) },
4929        { BT_DEVICE_WACOM(0x393) },
4930        { USB_DEVICE_WACOM(0x4001) },
4931        { USB_DEVICE_WACOM(0x4004) },
4932        { USB_DEVICE_WACOM(0x5000) },
4933        { USB_DEVICE_WACOM(0x5002) },
4934        { USB_DEVICE_LENOVO(0x6004) },
4935
4936        { USB_DEVICE_WACOM(HID_ANY_ID) },
4937        { I2C_DEVICE_WACOM(HID_ANY_ID) },
4938        { BT_DEVICE_WACOM(HID_ANY_ID) },
4939        { }
4940};
4941MODULE_DEVICE_TABLE(hid, wacom_ids);
4942