linux/drivers/hid/wacom_sys.c
<<
>>
Prefs
   1/*
   2 * drivers/input/tablet/wacom_sys.c
   3 *
   4 *  USB Wacom tablet support - system specific code
   5 */
   6
   7/*
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include "wacom_wac.h"
  15#include "wacom.h"
  16#include <linux/input/mt.h>
  17
  18#define WAC_MSG_RETRIES         5
  19#define WAC_CMD_RETRIES         10
  20
  21#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
  22#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
  23#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
  24
  25static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
  26                            size_t size, unsigned int retries)
  27{
  28        int retval;
  29
  30        do {
  31                retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  32                                HID_REQ_GET_REPORT);
  33        } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  34
  35        if (retval < 0)
  36                hid_err(hdev, "wacom_get_report: ran out of retries "
  37                        "(last error = %d)\n", retval);
  38
  39        return retval;
  40}
  41
  42static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
  43                            size_t size, unsigned int retries)
  44{
  45        int retval;
  46
  47        do {
  48                retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  49                                HID_REQ_SET_REPORT);
  50        } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  51
  52        if (retval < 0)
  53                hid_err(hdev, "wacom_set_report: ran out of retries "
  54                        "(last error = %d)\n", retval);
  55
  56        return retval;
  57}
  58
  59static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  60                u8 *raw_data, int size)
  61{
  62        struct wacom *wacom = hid_get_drvdata(hdev);
  63
  64        if (size > WACOM_PKGLEN_MAX)
  65                return 1;
  66
  67        memcpy(wacom->wacom_wac.data, raw_data, size);
  68
  69        wacom_wac_irq(&wacom->wacom_wac, size);
  70
  71        return 0;
  72}
  73
  74static int wacom_open(struct input_dev *dev)
  75{
  76        struct wacom *wacom = input_get_drvdata(dev);
  77
  78        return hid_hw_open(wacom->hdev);
  79}
  80
  81static void wacom_close(struct input_dev *dev)
  82{
  83        struct wacom *wacom = input_get_drvdata(dev);
  84
  85        /*
  86         * wacom->hdev should never be null, but surprisingly, I had the case
  87         * once while unplugging the Wacom Wireless Receiver.
  88         */
  89        if (wacom->hdev)
  90                hid_hw_close(wacom->hdev);
  91}
  92
  93/*
  94 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
  95 */
  96static int wacom_calc_hid_res(int logical_extents, int physical_extents,
  97                               unsigned unit, int exponent)
  98{
  99        struct hid_field field = {
 100                .logical_maximum = logical_extents,
 101                .physical_maximum = physical_extents,
 102                .unit = unit,
 103                .unit_exponent = exponent,
 104        };
 105
 106        return hidinput_calc_abs_res(&field, ABS_X);
 107}
 108
 109static void wacom_feature_mapping(struct hid_device *hdev,
 110                struct hid_field *field, struct hid_usage *usage)
 111{
 112        struct wacom *wacom = hid_get_drvdata(hdev);
 113        struct wacom_features *features = &wacom->wacom_wac.features;
 114        struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
 115        unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
 116        u8 *data;
 117        int ret;
 118        u32 n;
 119
 120        switch (equivalent_usage) {
 121        case HID_DG_CONTACTMAX:
 122                /* leave touch_max as is if predefined */
 123                if (!features->touch_max) {
 124                        /* read manually */
 125                        data = kzalloc(2, GFP_KERNEL);
 126                        if (!data)
 127                                break;
 128                        data[0] = field->report->id;
 129                        ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
 130                                                data, 2, WAC_CMD_RETRIES);
 131                        if (ret == 2) {
 132                                features->touch_max = data[1];
 133                        } else {
 134                                features->touch_max = 16;
 135                                hid_warn(hdev, "wacom_feature_mapping: "
 136                                         "could not get HID_DG_CONTACTMAX, "
 137                                         "defaulting to %d\n",
 138                                          features->touch_max);
 139                        }
 140                        kfree(data);
 141                }
 142                break;
 143        case HID_DG_INPUTMODE:
 144                /* Ignore if value index is out of bounds. */
 145                if (usage->usage_index >= field->report_count) {
 146                        dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
 147                        break;
 148                }
 149
 150                hid_data->inputmode = field->report->id;
 151                hid_data->inputmode_index = usage->usage_index;
 152                break;
 153
 154        case HID_UP_DIGITIZER:
 155                if (field->report->id == 0x0B &&
 156                    (field->application == WACOM_HID_G9_PEN ||
 157                     field->application == WACOM_HID_G11_PEN)) {
 158                        wacom->wacom_wac.mode_report = field->report->id;
 159                        wacom->wacom_wac.mode_value = 0;
 160                }
 161                break;
 162
 163        case WACOM_HID_WD_DATAMODE:
 164                wacom->wacom_wac.mode_report = field->report->id;
 165                wacom->wacom_wac.mode_value = 2;
 166                break;
 167
 168        case WACOM_HID_UP_G9:
 169        case WACOM_HID_UP_G11:
 170                if (field->report->id == 0x03 &&
 171                    (field->application == WACOM_HID_G9_TOUCHSCREEN ||
 172                     field->application == WACOM_HID_G11_TOUCHSCREEN)) {
 173                        wacom->wacom_wac.mode_report = field->report->id;
 174                        wacom->wacom_wac.mode_value = 0;
 175                }
 176                break;
 177        case WACOM_HID_WD_OFFSETLEFT:
 178        case WACOM_HID_WD_OFFSETTOP:
 179        case WACOM_HID_WD_OFFSETRIGHT:
 180        case WACOM_HID_WD_OFFSETBOTTOM:
 181                /* read manually */
 182                n = hid_report_len(field->report);
 183                data = hid_alloc_report_buf(field->report, GFP_KERNEL);
 184                if (!data)
 185                        break;
 186                data[0] = field->report->id;
 187                ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
 188                                        data, n, WAC_CMD_RETRIES);
 189                if (ret == n) {
 190                        ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
 191                                                   data, n, 0);
 192                } else {
 193                        hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
 194                                 __func__);
 195                }
 196                kfree(data);
 197                break;
 198        }
 199
 200        if (hdev->vendor == USB_VENDOR_ID_WACOM &&
 201            hdev->product == 0x4200 /* Dell Canvas 27 */ &&
 202            field->application == HID_UP_MSVENDOR) {
 203                wacom->wacom_wac.mode_report = field->report->id;
 204                wacom->wacom_wac.mode_value = 2;
 205        }
 206}
 207
 208/*
 209 * Interface Descriptor of wacom devices can be incomplete and
 210 * inconsistent so wacom_features table is used to store stylus
 211 * device's packet lengths, various maximum values, and tablet
 212 * resolution based on product ID's.
 213 *
 214 * For devices that contain 2 interfaces, wacom_features table is
 215 * inaccurate for the touch interface.  Since the Interface Descriptor
 216 * for touch interfaces has pretty complete data, this function exists
 217 * to query tablet for this missing information instead of hard coding in
 218 * an additional table.
 219 *
 220 * A typical Interface Descriptor for a stylus will contain a
 221 * boot mouse application collection that is not of interest and this
 222 * function will ignore it.
 223 *
 224 * It also contains a digitizer application collection that also is not
 225 * of interest since any information it contains would be duplicate
 226 * of what is in wacom_features. Usually it defines a report of an array
 227 * of bytes that could be used as max length of the stylus packet returned.
 228 * If it happens to define a Digitizer-Stylus Physical Collection then
 229 * the X and Y logical values contain valid data but it is ignored.
 230 *
 231 * A typical Interface Descriptor for a touch interface will contain a
 232 * Digitizer-Finger Physical Collection which will define both logical
 233 * X/Y maximum as well as the physical size of tablet. Since touch
 234 * interfaces haven't supported pressure or distance, this is enough
 235 * information to override invalid values in the wacom_features table.
 236 *
 237 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
 238 * data. We deal with them after returning from this function.
 239 */
 240static void wacom_usage_mapping(struct hid_device *hdev,
 241                struct hid_field *field, struct hid_usage *usage)
 242{
 243        struct wacom *wacom = hid_get_drvdata(hdev);
 244        struct wacom_features *features = &wacom->wacom_wac.features;
 245        bool finger = WACOM_FINGER_FIELD(field);
 246        bool pen = WACOM_PEN_FIELD(field);
 247
 248        /*
 249        * Requiring Stylus Usage will ignore boot mouse
 250        * X/Y values and some cases of invalid Digitizer X/Y
 251        * values commonly reported.
 252        */
 253        if (pen)
 254                features->device_type |= WACOM_DEVICETYPE_PEN;
 255        else if (finger)
 256                features->device_type |= WACOM_DEVICETYPE_TOUCH;
 257        else
 258                return;
 259
 260        /*
 261         * Bamboo models do not support HID_DG_CONTACTMAX.
 262         * And, Bamboo Pen only descriptor contains touch.
 263         */
 264        if (features->type > BAMBOO_PT) {
 265                /* ISDv4 touch devices at least supports one touch point */
 266                if (finger && !features->touch_max)
 267                        features->touch_max = 1;
 268        }
 269
 270        /*
 271         * ISDv4 devices which predate HID's adoption of the
 272         * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
 273         * position instead. We can accurately detect if a
 274         * usage with that value should be HID_DG_BARRELSWITCH2
 275         * based on the surrounding usages, which have remained
 276         * constant across generations.
 277         */
 278        if (features->type == HID_GENERIC &&
 279            usage->hid == 0x000D0000 &&
 280            field->application == HID_DG_PEN &&
 281            field->physical == HID_DG_STYLUS) {
 282                int i = usage->usage_index;
 283
 284                if (i-4 >= 0 && i+1 < field->maxusage &&
 285                    field->usage[i-4].hid == HID_DG_TIPSWITCH &&
 286                    field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
 287                    field->usage[i-2].hid == HID_DG_ERASER &&
 288                    field->usage[i-1].hid == HID_DG_INVERT &&
 289                    field->usage[i+1].hid == HID_DG_INRANGE) {
 290                        usage->hid = HID_DG_BARRELSWITCH2;
 291                }
 292        }
 293
 294        /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
 295        if (hdev->vendor == USB_VENDOR_ID_WACOM &&
 296            hdev->product == 0x0358 &&
 297            WACOM_PEN_FIELD(field) &&
 298            wacom_equivalent_usage(usage->hid) == HID_GD_Y) {
 299                field->logical_maximum = 43200;
 300        }
 301
 302        switch (usage->hid) {
 303        case HID_GD_X:
 304                features->x_max = field->logical_maximum;
 305                if (finger) {
 306                        features->x_phy = field->physical_maximum;
 307                        if ((features->type != BAMBOO_PT) &&
 308                            (features->type != BAMBOO_TOUCH)) {
 309                                features->unit = field->unit;
 310                                features->unitExpo = field->unit_exponent;
 311                        }
 312                }
 313                break;
 314        case HID_GD_Y:
 315                features->y_max = field->logical_maximum;
 316                if (finger) {
 317                        features->y_phy = field->physical_maximum;
 318                        if ((features->type != BAMBOO_PT) &&
 319                            (features->type != BAMBOO_TOUCH)) {
 320                                features->unit = field->unit;
 321                                features->unitExpo = field->unit_exponent;
 322                        }
 323                }
 324                break;
 325        case HID_DG_TIPPRESSURE:
 326                if (pen)
 327                        features->pressure_max = field->logical_maximum;
 328                break;
 329        }
 330
 331        if (features->type == HID_GENERIC)
 332                wacom_wac_usage_mapping(hdev, field, usage);
 333}
 334
 335static void wacom_post_parse_hid(struct hid_device *hdev,
 336                                 struct wacom_features *features)
 337{
 338        struct wacom *wacom = hid_get_drvdata(hdev);
 339        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
 340
 341        if (features->type == HID_GENERIC) {
 342                /* Any last-minute generic device setup */
 343                if (wacom_wac->has_mode_change) {
 344                        if (wacom_wac->is_direct_mode)
 345                                features->device_type |= WACOM_DEVICETYPE_DIRECT;
 346                        else
 347                                features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
 348                }
 349
 350                if (features->touch_max > 1) {
 351                        if (features->device_type & WACOM_DEVICETYPE_DIRECT)
 352                                input_mt_init_slots(wacom_wac->touch_input,
 353                                                    wacom_wac->features.touch_max,
 354                                                    INPUT_MT_DIRECT);
 355                        else
 356                                input_mt_init_slots(wacom_wac->touch_input,
 357                                                    wacom_wac->features.touch_max,
 358                                                    INPUT_MT_POINTER);
 359                }
 360        }
 361}
 362
 363static void wacom_parse_hid(struct hid_device *hdev,
 364                           struct wacom_features *features)
 365{
 366        struct hid_report_enum *rep_enum;
 367        struct hid_report *hreport;
 368        int i, j;
 369
 370        /* check features first */
 371        rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
 372        list_for_each_entry(hreport, &rep_enum->report_list, list) {
 373                for (i = 0; i < hreport->maxfield; i++) {
 374                        /* Ignore if report count is out of bounds. */
 375                        if (hreport->field[i]->report_count < 1)
 376                                continue;
 377
 378                        for (j = 0; j < hreport->field[i]->maxusage; j++) {
 379                                wacom_feature_mapping(hdev, hreport->field[i],
 380                                                hreport->field[i]->usage + j);
 381                        }
 382                }
 383        }
 384
 385        /* now check the input usages */
 386        rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
 387        list_for_each_entry(hreport, &rep_enum->report_list, list) {
 388
 389                if (!hreport->maxfield)
 390                        continue;
 391
 392                for (i = 0; i < hreport->maxfield; i++)
 393                        for (j = 0; j < hreport->field[i]->maxusage; j++)
 394                                wacom_usage_mapping(hdev, hreport->field[i],
 395                                                hreport->field[i]->usage + j);
 396        }
 397
 398        wacom_post_parse_hid(hdev, features);
 399}
 400
 401static int wacom_hid_set_device_mode(struct hid_device *hdev)
 402{
 403        struct wacom *wacom = hid_get_drvdata(hdev);
 404        struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
 405        struct hid_report *r;
 406        struct hid_report_enum *re;
 407
 408        if (hid_data->inputmode < 0)
 409                return 0;
 410
 411        re = &(hdev->report_enum[HID_FEATURE_REPORT]);
 412        r = re->report_id_hash[hid_data->inputmode];
 413        if (r) {
 414                r->field[0]->value[hid_data->inputmode_index] = 2;
 415                hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
 416        }
 417        return 0;
 418}
 419
 420static int wacom_set_device_mode(struct hid_device *hdev,
 421                                 struct wacom_wac *wacom_wac)
 422{
 423        u8 *rep_data;
 424        struct hid_report *r;
 425        struct hid_report_enum *re;
 426        u32 length;
 427        int error = -ENOMEM, limit = 0;
 428
 429        if (wacom_wac->mode_report < 0)
 430                return 0;
 431
 432        re = &(hdev->report_enum[HID_FEATURE_REPORT]);
 433        r = re->report_id_hash[wacom_wac->mode_report];
 434        if (!r)
 435                return -EINVAL;
 436
 437        rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
 438        if (!rep_data)
 439                return -ENOMEM;
 440
 441        length = hid_report_len(r);
 442
 443        do {
 444                rep_data[0] = wacom_wac->mode_report;
 445                rep_data[1] = wacom_wac->mode_value;
 446
 447                error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
 448                                         length, 1);
 449                if (error >= 0)
 450                        error = wacom_get_report(hdev, HID_FEATURE_REPORT,
 451                                                 rep_data, length, 1);
 452        } while (error >= 0 &&
 453                 rep_data[1] != wacom_wac->mode_report &&
 454                 limit++ < WAC_MSG_RETRIES);
 455
 456        kfree(rep_data);
 457
 458        return error < 0 ? error : 0;
 459}
 460
 461static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
 462                struct wacom_features *features)
 463{
 464        struct wacom *wacom = hid_get_drvdata(hdev);
 465        int ret;
 466        u8 rep_data[2];
 467
 468        switch (features->type) {
 469        case GRAPHIRE_BT:
 470                rep_data[0] = 0x03;
 471                rep_data[1] = 0x00;
 472                ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
 473                                        3);
 474
 475                if (ret >= 0) {
 476                        rep_data[0] = speed == 0 ? 0x05 : 0x06;
 477                        rep_data[1] = 0x00;
 478
 479                        ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
 480                                                rep_data, 2, 3);
 481
 482                        if (ret >= 0) {
 483                                wacom->wacom_wac.bt_high_speed = speed;
 484                                return 0;
 485                        }
 486                }
 487
 488                /*
 489                 * Note that if the raw queries fail, it's not a hard failure
 490                 * and it is safe to continue
 491                 */
 492                hid_warn(hdev, "failed to poke device, command %d, err %d\n",
 493                         rep_data[0], ret);
 494                break;
 495        case INTUOS4WL:
 496                if (speed == 1)
 497                        wacom->wacom_wac.bt_features &= ~0x20;
 498                else
 499                        wacom->wacom_wac.bt_features |= 0x20;
 500
 501                rep_data[0] = 0x03;
 502                rep_data[1] = wacom->wacom_wac.bt_features;
 503
 504                ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
 505                                        1);
 506                if (ret >= 0)
 507                        wacom->wacom_wac.bt_high_speed = speed;
 508                break;
 509        }
 510
 511        return 0;
 512}
 513
 514/*
 515 * Switch the tablet into its most-capable mode. Wacom tablets are
 516 * typically configured to power-up in a mode which sends mouse-like
 517 * reports to the OS. To get absolute position, pressure data, etc.
 518 * from the tablet, it is necessary to switch the tablet out of this
 519 * mode and into one which sends the full range of tablet data.
 520 */
 521static int _wacom_query_tablet_data(struct wacom *wacom)
 522{
 523        struct hid_device *hdev = wacom->hdev;
 524        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
 525        struct wacom_features *features = &wacom_wac->features;
 526
 527        if (hdev->bus == BUS_BLUETOOTH)
 528                return wacom_bt_query_tablet_data(hdev, 1, features);
 529
 530        if (features->type != HID_GENERIC) {
 531                if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
 532                        if (features->type > TABLETPC) {
 533                                /* MT Tablet PC touch */
 534                                wacom_wac->mode_report = 3;
 535                                wacom_wac->mode_value = 4;
 536                        } else if (features->type == WACOM_24HDT) {
 537                                wacom_wac->mode_report = 18;
 538                                wacom_wac->mode_value = 2;
 539                        } else if (features->type == WACOM_27QHDT) {
 540                                wacom_wac->mode_report = 131;
 541                                wacom_wac->mode_value = 2;
 542                        } else if (features->type == BAMBOO_PAD) {
 543                                wacom_wac->mode_report = 2;
 544                                wacom_wac->mode_value = 2;
 545                        }
 546                } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
 547                        if (features->type <= BAMBOO_PT) {
 548                                wacom_wac->mode_report = 2;
 549                                wacom_wac->mode_value = 2;
 550                        }
 551                }
 552        }
 553
 554        wacom_set_device_mode(hdev, wacom_wac);
 555
 556        if (features->type == HID_GENERIC)
 557                return wacom_hid_set_device_mode(hdev);
 558
 559        return 0;
 560}
 561
 562static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
 563                                         struct wacom_features *features)
 564{
 565        struct wacom *wacom = hid_get_drvdata(hdev);
 566        struct usb_interface *intf = wacom->intf;
 567
 568        /* default features */
 569        features->x_fuzz = 4;
 570        features->y_fuzz = 4;
 571        features->pressure_fuzz = 0;
 572        features->distance_fuzz = 1;
 573        features->tilt_fuzz = 1;
 574
 575        /*
 576         * The wireless device HID is basic and layout conflicts with
 577         * other tablets (monitor and touch interface can look like pen).
 578         * Skip the query for this type and modify defaults based on
 579         * interface number.
 580         */
 581        if (features->type == WIRELESS) {
 582                if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
 583                        features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
 584                else
 585                        features->device_type = WACOM_DEVICETYPE_NONE;
 586                return;
 587        }
 588
 589        wacom_parse_hid(hdev, features);
 590}
 591
 592struct wacom_hdev_data {
 593        struct list_head list;
 594        struct kref kref;
 595        struct hid_device *dev;
 596        struct wacom_shared shared;
 597};
 598
 599static LIST_HEAD(wacom_udev_list);
 600static DEFINE_MUTEX(wacom_udev_list_lock);
 601
 602static bool compare_device_paths(struct hid_device *hdev_a,
 603                struct hid_device *hdev_b, char separator)
 604{
 605        int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
 606        int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
 607
 608        if (n1 != n2 || n1 <= 0 || n2 <= 0)
 609                return false;
 610
 611        return !strncmp(hdev_a->phys, hdev_b->phys, n1);
 612}
 613
 614static bool wacom_are_sibling(struct hid_device *hdev,
 615                struct hid_device *sibling)
 616{
 617        struct wacom *wacom = hid_get_drvdata(hdev);
 618        struct wacom_features *features = &wacom->wacom_wac.features;
 619        struct wacom *sibling_wacom = hid_get_drvdata(sibling);
 620        struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
 621        __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
 622        __u32 oPid = features->oPid ? features->oPid : hdev->product;
 623
 624        /* The defined oVid/oPid must match that of the sibling */
 625        if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
 626                return false;
 627        if (features->oPid != HID_ANY_ID && sibling->product != oPid)
 628                return false;
 629
 630        /*
 631         * Devices with the same VID/PID must share the same physical
 632         * device path, while those with different VID/PID must share
 633         * the same physical parent device path.
 634         */
 635        if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
 636                if (!compare_device_paths(hdev, sibling, '/'))
 637                        return false;
 638        } else {
 639                if (!compare_device_paths(hdev, sibling, '.'))
 640                        return false;
 641        }
 642
 643        /* Skip the remaining heuristics unless you are a HID_GENERIC device */
 644        if (features->type != HID_GENERIC)
 645                return true;
 646
 647        /*
 648         * Direct-input devices may not be siblings of indirect-input
 649         * devices.
 650         */
 651        if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
 652            !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
 653                return false;
 654
 655        /*
 656         * Indirect-input devices may not be siblings of direct-input
 657         * devices.
 658         */
 659        if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
 660            (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
 661                return false;
 662
 663        /* Pen devices may only be siblings of touch devices */
 664        if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
 665            !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
 666                return false;
 667
 668        /* Touch devices may only be siblings of pen devices */
 669        if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
 670            !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
 671                return false;
 672
 673        /*
 674         * No reason could be found for these two devices to NOT be
 675         * siblings, so there's a good chance they ARE siblings
 676         */
 677        return true;
 678}
 679
 680static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
 681{
 682        struct wacom_hdev_data *data;
 683
 684        /* Try to find an already-probed interface from the same device */
 685        list_for_each_entry(data, &wacom_udev_list, list) {
 686                if (compare_device_paths(hdev, data->dev, '/')) {
 687                        kref_get(&data->kref);
 688                        return data;
 689                }
 690        }
 691
 692        /* Fallback to finding devices that appear to be "siblings" */
 693        list_for_each_entry(data, &wacom_udev_list, list) {
 694                if (wacom_are_sibling(hdev, data->dev)) {
 695                        kref_get(&data->kref);
 696                        return data;
 697                }
 698        }
 699
 700        return NULL;
 701}
 702
 703static void wacom_release_shared_data(struct kref *kref)
 704{
 705        struct wacom_hdev_data *data =
 706                container_of(kref, struct wacom_hdev_data, kref);
 707
 708        mutex_lock(&wacom_udev_list_lock);
 709        list_del(&data->list);
 710        mutex_unlock(&wacom_udev_list_lock);
 711
 712        kfree(data);
 713}
 714
 715static void wacom_remove_shared_data(void *res)
 716{
 717        struct wacom *wacom = res;
 718        struct wacom_hdev_data *data;
 719        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
 720
 721        if (wacom_wac->shared) {
 722                data = container_of(wacom_wac->shared, struct wacom_hdev_data,
 723                                    shared);
 724
 725                if (wacom_wac->shared->touch == wacom->hdev)
 726                        wacom_wac->shared->touch = NULL;
 727                else if (wacom_wac->shared->pen == wacom->hdev)
 728                        wacom_wac->shared->pen = NULL;
 729
 730                kref_put(&data->kref, wacom_release_shared_data);
 731                wacom_wac->shared = NULL;
 732        }
 733}
 734
 735static int wacom_add_shared_data(struct hid_device *hdev)
 736{
 737        struct wacom *wacom = hid_get_drvdata(hdev);
 738        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
 739        struct wacom_hdev_data *data;
 740        int retval = 0;
 741
 742        mutex_lock(&wacom_udev_list_lock);
 743
 744        data = wacom_get_hdev_data(hdev);
 745        if (!data) {
 746                data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
 747                if (!data) {
 748                        retval = -ENOMEM;
 749                        goto out;
 750                }
 751
 752                kref_init(&data->kref);
 753                data->dev = hdev;
 754                list_add_tail(&data->list, &wacom_udev_list);
 755        }
 756
 757        wacom_wac->shared = &data->shared;
 758
 759        retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
 760        if (retval) {
 761                mutex_unlock(&wacom_udev_list_lock);
 762                wacom_remove_shared_data(wacom);
 763                return retval;
 764        }
 765
 766        if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
 767                wacom_wac->shared->touch = hdev;
 768        else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
 769                wacom_wac->shared->pen = hdev;
 770
 771out:
 772        mutex_unlock(&wacom_udev_list_lock);
 773        return retval;
 774}
 775
 776static int wacom_led_control(struct wacom *wacom)
 777{
 778        unsigned char *buf;
 779        int retval;
 780        unsigned char report_id = WAC_CMD_LED_CONTROL;
 781        int buf_size = 9;
 782
 783        if (!hid_get_drvdata(wacom->hdev))
 784                return -ENODEV;
 785
 786        if (!wacom->led.groups)
 787                return -ENOTSUPP;
 788
 789        if (wacom->wacom_wac.features.type == REMOTE)
 790                return -ENOTSUPP;
 791
 792        if (wacom->wacom_wac.pid) { /* wireless connected */
 793                report_id = WAC_CMD_WL_LED_CONTROL;
 794                buf_size = 13;
 795        }
 796        else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
 797                report_id = WAC_CMD_WL_INTUOSP2;
 798                buf_size = 51;
 799        }
 800        buf = kzalloc(buf_size, GFP_KERNEL);
 801        if (!buf)
 802                return -ENOMEM;
 803
 804        if (wacom->wacom_wac.features.type == HID_GENERIC) {
 805                buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
 806                buf[1] = wacom->led.llv;
 807                buf[2] = wacom->led.groups[0].select & 0x03;
 808
 809        } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
 810            wacom->wacom_wac.features.type <= INTUOSPL)) {
 811                /*
 812                 * Touch Ring and crop mark LED luminance may take on
 813                 * one of four values:
 814                 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
 815                 */
 816                int ring_led = wacom->led.groups[0].select & 0x03;
 817                int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
 818                int crop_lum = 0;
 819                unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
 820
 821                buf[0] = report_id;
 822                if (wacom->wacom_wac.pid) {
 823                        wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
 824                                         buf, buf_size, WAC_CMD_RETRIES);
 825                        buf[0] = report_id;
 826                        buf[4] = led_bits;
 827                } else
 828                        buf[1] = led_bits;
 829        }
 830        else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
 831                buf[0] = report_id;
 832                buf[4] = 100; // Power Connection LED (ORANGE)
 833                buf[5] = 100; // BT Connection LED (BLUE)
 834                buf[6] = 100; // Paper Mode (RED?)
 835                buf[7] = 100; // Paper Mode (GREEN?)
 836                buf[8] = 100; // Paper Mode (BLUE?)
 837                buf[9] = wacom->led.llv;
 838                buf[10] = wacom->led.groups[0].select & 0x03;
 839        }
 840        else {
 841                int led = wacom->led.groups[0].select | 0x4;
 842
 843                if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
 844                    wacom->wacom_wac.features.type == WACOM_24HD)
 845                        led |= (wacom->led.groups[1].select << 4) | 0x40;
 846
 847                buf[0] = report_id;
 848                buf[1] = led;
 849                buf[2] = wacom->led.llv;
 850                buf[3] = wacom->led.hlv;
 851                buf[4] = wacom->led.img_lum;
 852        }
 853
 854        retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
 855                                  WAC_CMD_RETRIES);
 856        kfree(buf);
 857
 858        return retval;
 859}
 860
 861static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
 862                const unsigned len, const void *img)
 863{
 864        unsigned char *buf;
 865        int i, retval;
 866        const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
 867
 868        buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
 869        if (!buf)
 870                return -ENOMEM;
 871
 872        /* Send 'start' command */
 873        buf[0] = WAC_CMD_ICON_START;
 874        buf[1] = 1;
 875        retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
 876                                  WAC_CMD_RETRIES);
 877        if (retval < 0)
 878                goto out;
 879
 880        buf[0] = xfer_id;
 881        buf[1] = button_id & 0x07;
 882        for (i = 0; i < 4; i++) {
 883                buf[2] = i;
 884                memcpy(buf + 3, img + i * chunk_len, chunk_len);
 885
 886                retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
 887                                          buf, chunk_len + 3, WAC_CMD_RETRIES);
 888                if (retval < 0)
 889                        break;
 890        }
 891
 892        /* Send 'stop' */
 893        buf[0] = WAC_CMD_ICON_START;
 894        buf[1] = 0;
 895        wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
 896                         WAC_CMD_RETRIES);
 897
 898out:
 899        kfree(buf);
 900        return retval;
 901}
 902
 903static ssize_t wacom_led_select_store(struct device *dev, int set_id,
 904                                      const char *buf, size_t count)
 905{
 906        struct hid_device *hdev = to_hid_device(dev);
 907        struct wacom *wacom = hid_get_drvdata(hdev);
 908        unsigned int id;
 909        int err;
 910
 911        err = kstrtouint(buf, 10, &id);
 912        if (err)
 913                return err;
 914
 915        mutex_lock(&wacom->lock);
 916
 917        wacom->led.groups[set_id].select = id & 0x3;
 918        err = wacom_led_control(wacom);
 919
 920        mutex_unlock(&wacom->lock);
 921
 922        return err < 0 ? err : count;
 923}
 924
 925#define DEVICE_LED_SELECT_ATTR(SET_ID)                                  \
 926static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,     \
 927        struct device_attribute *attr, const char *buf, size_t count)   \
 928{                                                                       \
 929        return wacom_led_select_store(dev, SET_ID, buf, count);         \
 930}                                                                       \
 931static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,      \
 932        struct device_attribute *attr, char *buf)                       \
 933{                                                                       \
 934        struct hid_device *hdev = to_hid_device(dev);\
 935        struct wacom *wacom = hid_get_drvdata(hdev);                    \
 936        return scnprintf(buf, PAGE_SIZE, "%d\n",                        \
 937                         wacom->led.groups[SET_ID].select);             \
 938}                                                                       \
 939static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,       \
 940                    wacom_led##SET_ID##_select_show,                    \
 941                    wacom_led##SET_ID##_select_store)
 942
 943DEVICE_LED_SELECT_ATTR(0);
 944DEVICE_LED_SELECT_ATTR(1);
 945
 946static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
 947                                     const char *buf, size_t count)
 948{
 949        unsigned int value;
 950        int err;
 951
 952        err = kstrtouint(buf, 10, &value);
 953        if (err)
 954                return err;
 955
 956        mutex_lock(&wacom->lock);
 957
 958        *dest = value & 0x7f;
 959        err = wacom_led_control(wacom);
 960
 961        mutex_unlock(&wacom->lock);
 962
 963        return err < 0 ? err : count;
 964}
 965
 966#define DEVICE_LUMINANCE_ATTR(name, field)                              \
 967static ssize_t wacom_##name##_luminance_store(struct device *dev,       \
 968        struct device_attribute *attr, const char *buf, size_t count)   \
 969{                                                                       \
 970        struct hid_device *hdev = to_hid_device(dev);\
 971        struct wacom *wacom = hid_get_drvdata(hdev);                    \
 972                                                                        \
 973        return wacom_luminance_store(wacom, &wacom->led.field,          \
 974                                     buf, count);                       \
 975}                                                                       \
 976static ssize_t wacom_##name##_luminance_show(struct device *dev,        \
 977        struct device_attribute *attr, char *buf)                       \
 978{                                                                       \
 979        struct wacom *wacom = dev_get_drvdata(dev);                     \
 980        return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);     \
 981}                                                                       \
 982static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,                  \
 983                   wacom_##name##_luminance_show,                       \
 984                   wacom_##name##_luminance_store)
 985
 986DEVICE_LUMINANCE_ATTR(status0, llv);
 987DEVICE_LUMINANCE_ATTR(status1, hlv);
 988DEVICE_LUMINANCE_ATTR(buttons, img_lum);
 989
 990static ssize_t wacom_button_image_store(struct device *dev, int button_id,
 991                                        const char *buf, size_t count)
 992{
 993        struct hid_device *hdev = to_hid_device(dev);
 994        struct wacom *wacom = hid_get_drvdata(hdev);
 995        int err;
 996        unsigned len;
 997        u8 xfer_id;
 998
 999        if (hdev->bus == BUS_BLUETOOTH) {
1000                len = 256;
1001                xfer_id = WAC_CMD_ICON_BT_XFER;
1002        } else {
1003                len = 1024;
1004                xfer_id = WAC_CMD_ICON_XFER;
1005        }
1006
1007        if (count != len)
1008                return -EINVAL;
1009
1010        mutex_lock(&wacom->lock);
1011
1012        err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1013
1014        mutex_unlock(&wacom->lock);
1015
1016        return err < 0 ? err : count;
1017}
1018
1019#define DEVICE_BTNIMG_ATTR(BUTTON_ID)                                   \
1020static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,      \
1021        struct device_attribute *attr, const char *buf, size_t count)   \
1022{                                                                       \
1023        return wacom_button_image_store(dev, BUTTON_ID, buf, count);    \
1024}                                                                       \
1025static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,        \
1026                   NULL, wacom_btnimg##BUTTON_ID##_store)
1027
1028DEVICE_BTNIMG_ATTR(0);
1029DEVICE_BTNIMG_ATTR(1);
1030DEVICE_BTNIMG_ATTR(2);
1031DEVICE_BTNIMG_ATTR(3);
1032DEVICE_BTNIMG_ATTR(4);
1033DEVICE_BTNIMG_ATTR(5);
1034DEVICE_BTNIMG_ATTR(6);
1035DEVICE_BTNIMG_ATTR(7);
1036
1037static struct attribute *cintiq_led_attrs[] = {
1038        &dev_attr_status_led0_select.attr,
1039        &dev_attr_status_led1_select.attr,
1040        NULL
1041};
1042
1043static struct attribute_group cintiq_led_attr_group = {
1044        .name = "wacom_led",
1045        .attrs = cintiq_led_attrs,
1046};
1047
1048static struct attribute *intuos4_led_attrs[] = {
1049        &dev_attr_status0_luminance.attr,
1050        &dev_attr_status1_luminance.attr,
1051        &dev_attr_status_led0_select.attr,
1052        &dev_attr_buttons_luminance.attr,
1053        &dev_attr_button0_rawimg.attr,
1054        &dev_attr_button1_rawimg.attr,
1055        &dev_attr_button2_rawimg.attr,
1056        &dev_attr_button3_rawimg.attr,
1057        &dev_attr_button4_rawimg.attr,
1058        &dev_attr_button5_rawimg.attr,
1059        &dev_attr_button6_rawimg.attr,
1060        &dev_attr_button7_rawimg.attr,
1061        NULL
1062};
1063
1064static struct attribute_group intuos4_led_attr_group = {
1065        .name = "wacom_led",
1066        .attrs = intuos4_led_attrs,
1067};
1068
1069static struct attribute *intuos5_led_attrs[] = {
1070        &dev_attr_status0_luminance.attr,
1071        &dev_attr_status_led0_select.attr,
1072        NULL
1073};
1074
1075static struct attribute_group intuos5_led_attr_group = {
1076        .name = "wacom_led",
1077        .attrs = intuos5_led_attrs,
1078};
1079
1080static struct attribute *generic_led_attrs[] = {
1081        &dev_attr_status0_luminance.attr,
1082        &dev_attr_status_led0_select.attr,
1083        NULL
1084};
1085
1086static struct attribute_group generic_led_attr_group = {
1087        .name = "wacom_led",
1088        .attrs = generic_led_attrs,
1089};
1090
1091struct wacom_sysfs_group_devres {
1092        struct attribute_group *group;
1093        struct kobject *root;
1094};
1095
1096static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1097{
1098        struct wacom_sysfs_group_devres *devres = res;
1099        struct kobject *kobj = devres->root;
1100
1101        dev_dbg(dev, "%s: dropping reference to %s\n",
1102                __func__, devres->group->name);
1103        sysfs_remove_group(kobj, devres->group);
1104}
1105
1106static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1107                                           struct kobject *root,
1108                                           struct attribute_group *group)
1109{
1110        struct wacom_sysfs_group_devres *devres;
1111        int error;
1112
1113        devres = devres_alloc(wacom_devm_sysfs_group_release,
1114                              sizeof(struct wacom_sysfs_group_devres),
1115                              GFP_KERNEL);
1116        if (!devres)
1117                return -ENOMEM;
1118
1119        devres->group = group;
1120        devres->root = root;
1121
1122        error = sysfs_create_group(devres->root, group);
1123        if (error)
1124                return error;
1125
1126        devres_add(&wacom->hdev->dev, devres);
1127
1128        return 0;
1129}
1130
1131static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1132                                         struct attribute_group *group)
1133{
1134        return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1135                                               group);
1136}
1137
1138static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1139                                  struct wacom_led *led, unsigned int group,
1140                                  unsigned int id, bool read_only)
1141{
1142        led->group = group;
1143        led->id = id;
1144        led->wacom = wacom;
1145        led->llv = wacom->led.llv;
1146        led->hlv = wacom->led.hlv;
1147
1148        return 0;
1149}
1150
1151static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1152                                                   struct wacom *wacom,
1153                                                   int group_id, int count,
1154                                                   bool read_only)
1155{
1156        struct wacom_led *leds;
1157        int i, error;
1158
1159        if (group_id >= wacom->led.count || count <= 0)
1160                return -EINVAL;
1161
1162        if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1163                return -ENOMEM;
1164
1165        leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1166        if (!leds) {
1167                error = -ENOMEM;
1168                goto err;
1169        }
1170
1171        wacom->led.groups[group_id].leds = leds;
1172        wacom->led.groups[group_id].count = count;
1173
1174        for (i = 0; i < count; i++) {
1175                error = wacom_led_register_one(dev, wacom, &leds[i],
1176                                               group_id, i, read_only);
1177                if (error)
1178                        goto err;
1179        }
1180
1181        devres_remove_group(dev, &wacom->led.groups[group_id]);
1182        return 0;
1183
1184err:
1185        devres_release_group(dev, &wacom->led.groups[group_id]);
1186        return error;
1187}
1188
1189static void wacom_led_groups_release(void *data)
1190{
1191        struct wacom *wacom = data;
1192
1193        wacom->led.groups = NULL;
1194        wacom->led.count = 0;
1195}
1196
1197static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1198{
1199        struct device *dev = &wacom->hdev->dev;
1200        struct wacom_group_leds *groups;
1201        int error;
1202
1203        groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1204                              GFP_KERNEL);
1205        if (!groups)
1206                return -ENOMEM;
1207
1208        error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1209        if (error)
1210                return error;
1211
1212        wacom->led.groups = groups;
1213        wacom->led.count = count;
1214
1215        return 0;
1216}
1217
1218static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1219                                         int led_per_group, bool read_only)
1220{
1221        struct device *dev;
1222        int i, error;
1223
1224        if (!wacom->wacom_wac.pad_input)
1225                return -EINVAL;
1226
1227        dev = &wacom->wacom_wac.pad_input->dev;
1228
1229        error = wacom_led_groups_allocate(wacom, group_count);
1230        if (error)
1231                return error;
1232
1233        for (i = 0; i < group_count; i++) {
1234                error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1235                                                                led_per_group,
1236                                                                read_only);
1237                if (error)
1238                        return error;
1239        }
1240
1241        return 0;
1242}
1243
1244int wacom_initialize_leds(struct wacom *wacom)
1245{
1246        int error;
1247
1248        if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1249                return 0;
1250
1251        /* Initialize default values */
1252        switch (wacom->wacom_wac.features.type) {
1253        case HID_GENERIC:
1254                if (!wacom->generic_has_leds)
1255                        return 0;
1256                wacom->led.llv = 100;
1257                wacom->led.max_llv = 100;
1258
1259                error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1260                if (error) {
1261                        hid_err(wacom->hdev,
1262                                "cannot create leds err: %d\n", error);
1263                        return error;
1264                }
1265
1266                error = wacom_devm_sysfs_create_group(wacom,
1267                                                      &generic_led_attr_group);
1268                break;
1269
1270        case INTUOS4S:
1271        case INTUOS4:
1272        case INTUOS4WL:
1273        case INTUOS4L:
1274                wacom->led.llv = 10;
1275                wacom->led.hlv = 20;
1276                wacom->led.max_llv = 127;
1277                wacom->led.max_hlv = 127;
1278                wacom->led.img_lum = 10;
1279
1280                error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1281                if (error) {
1282                        hid_err(wacom->hdev,
1283                                "cannot create leds err: %d\n", error);
1284                        return error;
1285                }
1286
1287                error = wacom_devm_sysfs_create_group(wacom,
1288                                                      &intuos4_led_attr_group);
1289                break;
1290
1291        case WACOM_24HD:
1292        case WACOM_21UX2:
1293                wacom->led.llv = 0;
1294                wacom->led.hlv = 0;
1295                wacom->led.img_lum = 0;
1296
1297                error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1298                if (error) {
1299                        hid_err(wacom->hdev,
1300                                "cannot create leds err: %d\n", error);
1301                        return error;
1302                }
1303
1304                error = wacom_devm_sysfs_create_group(wacom,
1305                                                      &cintiq_led_attr_group);
1306                break;
1307
1308        case INTUOS5S:
1309        case INTUOS5:
1310        case INTUOS5L:
1311        case INTUOSPS:
1312        case INTUOSPM:
1313        case INTUOSPL:
1314                wacom->led.llv = 32;
1315                wacom->led.max_llv = 96;
1316
1317                error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1318                if (error) {
1319                        hid_err(wacom->hdev,
1320                                "cannot create leds err: %d\n", error);
1321                        return error;
1322                }
1323
1324                error = wacom_devm_sysfs_create_group(wacom,
1325                                                      &intuos5_led_attr_group);
1326                break;
1327
1328        case INTUOSP2_BT:
1329                wacom->led.llv = 50;
1330                error = wacom_led_groups_allocate(wacom, 4);
1331                if (error) {
1332                        hid_err(wacom->hdev,
1333                                "cannot create leds err: %d\n", error);
1334                        return error;
1335                }
1336                return 0;
1337
1338        case REMOTE:
1339                wacom->led.llv = 255;
1340                wacom->led.max_llv = 255;
1341                error = wacom_led_groups_allocate(wacom, 5);
1342                if (error) {
1343                        hid_err(wacom->hdev,
1344                                "cannot create leds err: %d\n", error);
1345                        return error;
1346                }
1347                return 0;
1348
1349        default:
1350                return 0;
1351        }
1352
1353        if (error) {
1354                hid_err(wacom->hdev,
1355                        "cannot create sysfs group err: %d\n", error);
1356                return error;
1357        }
1358
1359        return 0;
1360}
1361
1362static void wacom_init_work(struct work_struct *work)
1363{
1364        struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1365
1366        _wacom_query_tablet_data(wacom);
1367        wacom_led_control(wacom);
1368}
1369
1370static void wacom_query_tablet_data(struct wacom *wacom)
1371{
1372        schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1373}
1374
1375static enum power_supply_property wacom_battery_props[] = {
1376        POWER_SUPPLY_PROP_MODEL_NAME,
1377        POWER_SUPPLY_PROP_PRESENT,
1378        POWER_SUPPLY_PROP_STATUS,
1379        POWER_SUPPLY_PROP_SCOPE,
1380        POWER_SUPPLY_PROP_CAPACITY
1381};
1382
1383static int wacom_battery_get_property(struct power_supply *psy,
1384                                      enum power_supply_property psp,
1385                                      union power_supply_propval *val)
1386{
1387        struct wacom_battery *battery = power_supply_get_drvdata(psy);
1388        int ret = 0;
1389
1390        switch (psp) {
1391                case POWER_SUPPLY_PROP_MODEL_NAME:
1392                        val->strval = battery->wacom->wacom_wac.name;
1393                        break;
1394                case POWER_SUPPLY_PROP_PRESENT:
1395                        val->intval = battery->bat_connected;
1396                        break;
1397                case POWER_SUPPLY_PROP_SCOPE:
1398                        val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1399                        break;
1400                case POWER_SUPPLY_PROP_CAPACITY:
1401                        val->intval = battery->battery_capacity;
1402                        break;
1403                case POWER_SUPPLY_PROP_STATUS:
1404                        if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1405                                val->intval = battery->bat_status;
1406                        else if (battery->bat_charging)
1407                                val->intval = POWER_SUPPLY_STATUS_CHARGING;
1408                        else if (battery->battery_capacity == 100 &&
1409                                    battery->ps_connected)
1410                                val->intval = POWER_SUPPLY_STATUS_FULL;
1411                        else if (battery->ps_connected)
1412                                val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1413                        else
1414                                val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1415                        break;
1416                default:
1417                        ret = -EINVAL;
1418                        break;
1419        }
1420
1421        return ret;
1422}
1423
1424static int __wacom_initialize_battery(struct wacom *wacom,
1425                                      struct wacom_battery *battery)
1426{
1427        static atomic_t battery_no = ATOMIC_INIT(0);
1428        struct device *dev = &wacom->hdev->dev;
1429        struct power_supply_config psy_cfg = { .drv_data = battery, };
1430        struct power_supply *ps_bat;
1431        struct power_supply_desc *bat_desc = &battery->bat_desc;
1432        unsigned long n;
1433        int error;
1434
1435        if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1436                return -ENOMEM;
1437
1438        battery->wacom = wacom;
1439
1440        n = atomic_inc_return(&battery_no) - 1;
1441
1442        bat_desc->properties = wacom_battery_props;
1443        bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1444        bat_desc->get_property = wacom_battery_get_property;
1445        sprintf(battery->bat_name, "wacom_battery_%ld", n);
1446        bat_desc->name = battery->bat_name;
1447        bat_desc->type = POWER_SUPPLY_TYPE_USB;
1448        bat_desc->use_for_apm = 0;
1449
1450        ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1451        if (IS_ERR(ps_bat)) {
1452                error = PTR_ERR(ps_bat);
1453                goto err;
1454        }
1455
1456        power_supply_powers(ps_bat, &wacom->hdev->dev);
1457
1458        battery->battery = ps_bat;
1459
1460        devres_close_group(dev, bat_desc);
1461        return 0;
1462
1463err:
1464        devres_release_group(dev, bat_desc);
1465        return error;
1466}
1467
1468static int wacom_initialize_battery(struct wacom *wacom)
1469{
1470        if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1471                return __wacom_initialize_battery(wacom, &wacom->battery);
1472
1473        return 0;
1474}
1475
1476static void wacom_destroy_battery(struct wacom *wacom)
1477{
1478        if (wacom->battery.battery) {
1479                devres_release_group(&wacom->hdev->dev,
1480                                     &wacom->battery.bat_desc);
1481                wacom->battery.battery = NULL;
1482        }
1483}
1484
1485static ssize_t wacom_show_speed(struct device *dev,
1486                                struct device_attribute
1487                                *attr, char *buf)
1488{
1489        struct hid_device *hdev = to_hid_device(dev);
1490        struct wacom *wacom = hid_get_drvdata(hdev);
1491
1492        return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1493}
1494
1495static ssize_t wacom_store_speed(struct device *dev,
1496                                struct device_attribute *attr,
1497                                const char *buf, size_t count)
1498{
1499        struct hid_device *hdev = to_hid_device(dev);
1500        struct wacom *wacom = hid_get_drvdata(hdev);
1501        u8 new_speed;
1502
1503        if (kstrtou8(buf, 0, &new_speed))
1504                return -EINVAL;
1505
1506        if (new_speed != 0 && new_speed != 1)
1507                return -EINVAL;
1508
1509        wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1510
1511        return count;
1512}
1513
1514static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1515                wacom_show_speed, wacom_store_speed);
1516
1517
1518static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1519                                      struct kobj_attribute *kattr,
1520                                      char *buf, int index)
1521{
1522        struct device *dev = kobj_to_dev(kobj->parent);
1523        struct hid_device *hdev = to_hid_device(dev);
1524        struct wacom *wacom = hid_get_drvdata(hdev);
1525        u8 mode;
1526
1527        mode = wacom->led.groups[index].select;
1528        return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1529}
1530
1531#define DEVICE_EKR_ATTR_GROUP(SET_ID)                                   \
1532static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,   \
1533                               struct kobj_attribute *kattr, char *buf) \
1534{                                                                       \
1535        return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);        \
1536}                                                                       \
1537static struct kobj_attribute remote##SET_ID##_mode_attr = {             \
1538        .attr = {.name = "remote_mode",                                 \
1539                .mode = DEV_ATTR_RO_PERM},                              \
1540        .show = wacom_show_remote##SET_ID##_mode,                       \
1541};                                                                      \
1542static struct attribute *remote##SET_ID##_serial_attrs[] = {            \
1543        &remote##SET_ID##_mode_attr.attr,                               \
1544        NULL                                                            \
1545};                                                                      \
1546static struct attribute_group remote##SET_ID##_serial_group = {         \
1547        .name = NULL,                                                   \
1548        .attrs = remote##SET_ID##_serial_attrs,                         \
1549}
1550
1551DEVICE_EKR_ATTR_GROUP(0);
1552DEVICE_EKR_ATTR_GROUP(1);
1553DEVICE_EKR_ATTR_GROUP(2);
1554DEVICE_EKR_ATTR_GROUP(3);
1555DEVICE_EKR_ATTR_GROUP(4);
1556
1557static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1558                                          int index)
1559{
1560        int error = 0;
1561        struct wacom_remote *remote = wacom->remote;
1562
1563        remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1564                                                          GFP_KERNEL,
1565                                                          "%d", serial);
1566        if (!remote->remotes[index].group.name)
1567                return -ENOMEM;
1568
1569        error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1570                                                &remote->remotes[index].group);
1571        if (error) {
1572                remote->remotes[index].group.name = NULL;
1573                hid_err(wacom->hdev,
1574                        "cannot create sysfs group err: %d\n", error);
1575                return error;
1576        }
1577
1578        return 0;
1579}
1580
1581static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1582{
1583        const size_t buf_size = 2;
1584        unsigned char *buf;
1585        int retval;
1586
1587        buf = kzalloc(buf_size, GFP_KERNEL);
1588        if (!buf)
1589                return -ENOMEM;
1590
1591        buf[0] = WAC_CMD_DELETE_PAIRING;
1592        buf[1] = selector;
1593
1594        retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1595                                  buf_size, WAC_CMD_RETRIES);
1596        kfree(buf);
1597
1598        return retval;
1599}
1600
1601static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1602                                         struct kobj_attribute *attr,
1603                                         const char *buf, size_t count)
1604{
1605        unsigned char selector = 0;
1606        struct device *dev = kobj_to_dev(kobj->parent);
1607        struct hid_device *hdev = to_hid_device(dev);
1608        struct wacom *wacom = hid_get_drvdata(hdev);
1609        int err;
1610
1611        if (!strncmp(buf, "*\n", 2)) {
1612                selector = WAC_CMD_UNPAIR_ALL;
1613        } else {
1614                hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1615                         buf);
1616                return -1;
1617        }
1618
1619        mutex_lock(&wacom->lock);
1620
1621        err = wacom_cmd_unpair_remote(wacom, selector);
1622        mutex_unlock(&wacom->lock);
1623
1624        return err < 0 ? err : count;
1625}
1626
1627static struct kobj_attribute unpair_remote_attr = {
1628        .attr = {.name = "unpair_remote", .mode = 0200},
1629        .store = wacom_store_unpair_remote,
1630};
1631
1632static const struct attribute *remote_unpair_attrs[] = {
1633        &unpair_remote_attr.attr,
1634        NULL
1635};
1636
1637static void wacom_remotes_destroy(void *data)
1638{
1639        struct wacom *wacom = data;
1640        struct wacom_remote *remote = wacom->remote;
1641
1642        if (!remote)
1643                return;
1644
1645        kobject_put(remote->remote_dir);
1646        kfifo_free(&remote->remote_fifo);
1647        wacom->remote = NULL;
1648}
1649
1650static int wacom_initialize_remotes(struct wacom *wacom)
1651{
1652        int error = 0;
1653        struct wacom_remote *remote;
1654        int i;
1655
1656        if (wacom->wacom_wac.features.type != REMOTE)
1657                return 0;
1658
1659        remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1660                              GFP_KERNEL);
1661        if (!remote)
1662                return -ENOMEM;
1663
1664        wacom->remote = remote;
1665
1666        spin_lock_init(&remote->remote_lock);
1667
1668        error = kfifo_alloc(&remote->remote_fifo,
1669                        5 * sizeof(struct wacom_remote_data),
1670                        GFP_KERNEL);
1671        if (error) {
1672                hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1673                return -ENOMEM;
1674        }
1675
1676        remote->remotes[0].group = remote0_serial_group;
1677        remote->remotes[1].group = remote1_serial_group;
1678        remote->remotes[2].group = remote2_serial_group;
1679        remote->remotes[3].group = remote3_serial_group;
1680        remote->remotes[4].group = remote4_serial_group;
1681
1682        remote->remote_dir = kobject_create_and_add("wacom_remote",
1683                                                    &wacom->hdev->dev.kobj);
1684        if (!remote->remote_dir)
1685                return -ENOMEM;
1686
1687        error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1688
1689        if (error) {
1690                hid_err(wacom->hdev,
1691                        "cannot create sysfs group err: %d\n", error);
1692                return error;
1693        }
1694
1695        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1696                wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1697                remote->remotes[i].serial = 0;
1698        }
1699
1700        error = devm_add_action_or_reset(&wacom->hdev->dev,
1701                                         wacom_remotes_destroy, wacom);
1702        if (error)
1703                return error;
1704
1705        return 0;
1706}
1707
1708static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1709{
1710        struct input_dev *input_dev;
1711        struct hid_device *hdev = wacom->hdev;
1712        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1713
1714        input_dev = devm_input_allocate_device(&hdev->dev);
1715        if (!input_dev)
1716                return NULL;
1717
1718        input_dev->name = wacom_wac->features.name;
1719        input_dev->phys = hdev->phys;
1720        input_dev->dev.parent = &hdev->dev;
1721        input_dev->open = wacom_open;
1722        input_dev->close = wacom_close;
1723        input_dev->uniq = hdev->uniq;
1724        input_dev->id.bustype = hdev->bus;
1725        input_dev->id.vendor  = hdev->vendor;
1726        input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1727        input_dev->id.version = hdev->version;
1728        input_set_drvdata(input_dev, wacom);
1729
1730        return input_dev;
1731}
1732
1733static int wacom_allocate_inputs(struct wacom *wacom)
1734{
1735        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1736
1737        wacom_wac->pen_input = wacom_allocate_input(wacom);
1738        wacom_wac->touch_input = wacom_allocate_input(wacom);
1739        wacom_wac->pad_input = wacom_allocate_input(wacom);
1740        if (!wacom_wac->pen_input ||
1741            !wacom_wac->touch_input ||
1742            !wacom_wac->pad_input)
1743                return -ENOMEM;
1744
1745        wacom_wac->pen_input->name = wacom_wac->pen_name;
1746        wacom_wac->touch_input->name = wacom_wac->touch_name;
1747        wacom_wac->pad_input->name = wacom_wac->pad_name;
1748
1749        return 0;
1750}
1751
1752static int wacom_register_inputs(struct wacom *wacom)
1753{
1754        struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1755        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1756        int error = 0;
1757
1758        pen_input_dev = wacom_wac->pen_input;
1759        touch_input_dev = wacom_wac->touch_input;
1760        pad_input_dev = wacom_wac->pad_input;
1761
1762        if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1763                return -EINVAL;
1764
1765        error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1766        if (error) {
1767                /* no pen in use on this interface */
1768                input_free_device(pen_input_dev);
1769                wacom_wac->pen_input = NULL;
1770                pen_input_dev = NULL;
1771        } else {
1772                error = input_register_device(pen_input_dev);
1773                if (error)
1774                        goto fail;
1775        }
1776
1777        error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1778        if (error) {
1779                /* no touch in use on this interface */
1780                input_free_device(touch_input_dev);
1781                wacom_wac->touch_input = NULL;
1782                touch_input_dev = NULL;
1783        } else {
1784                error = input_register_device(touch_input_dev);
1785                if (error)
1786                        goto fail;
1787        }
1788
1789        error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1790        if (error) {
1791                /* no pad in use on this interface */
1792                input_free_device(pad_input_dev);
1793                wacom_wac->pad_input = NULL;
1794                pad_input_dev = NULL;
1795        } else {
1796                error = input_register_device(pad_input_dev);
1797                if (error)
1798                        goto fail;
1799        }
1800
1801        return 0;
1802
1803fail:
1804        wacom_wac->pad_input = NULL;
1805        wacom_wac->touch_input = NULL;
1806        wacom_wac->pen_input = NULL;
1807        return error;
1808}
1809
1810/*
1811 * Not all devices report physical dimensions from HID.
1812 * Compute the default from hardcoded logical dimension
1813 * and resolution before driver overwrites them.
1814 */
1815static void wacom_set_default_phy(struct wacom_features *features)
1816{
1817        if (features->x_resolution) {
1818                features->x_phy = (features->x_max * 100) /
1819                                        features->x_resolution;
1820                features->y_phy = (features->y_max * 100) /
1821                                        features->y_resolution;
1822        }
1823}
1824
1825static void wacom_calculate_res(struct wacom_features *features)
1826{
1827        /* set unit to "100th of a mm" for devices not reported by HID */
1828        if (!features->unit) {
1829                features->unit = 0x11;
1830                features->unitExpo = -3;
1831        }
1832
1833        features->x_resolution = wacom_calc_hid_res(features->x_max,
1834                                                    features->x_phy,
1835                                                    features->unit,
1836                                                    features->unitExpo);
1837        features->y_resolution = wacom_calc_hid_res(features->y_max,
1838                                                    features->y_phy,
1839                                                    features->unit,
1840                                                    features->unitExpo);
1841}
1842
1843void wacom_battery_work(struct work_struct *work)
1844{
1845        struct wacom *wacom = container_of(work, struct wacom, battery_work);
1846
1847        if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1848             !wacom->battery.battery) {
1849                wacom_initialize_battery(wacom);
1850        }
1851        else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1852                 wacom->battery.battery) {
1853                wacom_destroy_battery(wacom);
1854        }
1855}
1856
1857static size_t wacom_compute_pktlen(struct hid_device *hdev)
1858{
1859        struct hid_report_enum *report_enum;
1860        struct hid_report *report;
1861        size_t size = 0;
1862
1863        report_enum = hdev->report_enum + HID_INPUT_REPORT;
1864
1865        list_for_each_entry(report, &report_enum->report_list, list) {
1866                size_t report_size = hid_report_len(report);
1867                if (report_size > size)
1868                        size = report_size;
1869        }
1870
1871        return size;
1872}
1873
1874static void wacom_update_name(struct wacom *wacom, const char *suffix)
1875{
1876        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1877        struct wacom_features *features = &wacom_wac->features;
1878        char name[WACOM_NAME_MAX];
1879
1880        /* Generic devices name unspecified */
1881        if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1882                char *product_name = wacom->hdev->name;
1883
1884                if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
1885                        struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
1886                        struct usb_device *dev = interface_to_usbdev(intf);
1887                        product_name = dev->product;
1888                }
1889
1890                if (wacom->hdev->bus == BUS_I2C) {
1891                        snprintf(name, sizeof(name), "%s %X",
1892                                 features->name, wacom->hdev->product);
1893                } else if (strstr(product_name, "Wacom") ||
1894                           strstr(product_name, "wacom") ||
1895                           strstr(product_name, "WACOM")) {
1896                        strlcpy(name, product_name, sizeof(name));
1897                } else {
1898                        snprintf(name, sizeof(name), "Wacom %s", product_name);
1899                }
1900
1901                /* strip out excess whitespaces */
1902                while (1) {
1903                        char *gap = strstr(name, "  ");
1904                        if (gap == NULL)
1905                                break;
1906                        /* shift everything including the terminator */
1907                        memmove(gap, gap+1, strlen(gap));
1908                }
1909
1910                /* get rid of trailing whitespace */
1911                if (name[strlen(name)-1] == ' ')
1912                        name[strlen(name)-1] = '\0';
1913        } else {
1914                strlcpy(name, features->name, sizeof(name));
1915        }
1916
1917        snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1918                 name, suffix);
1919
1920        /* Append the device type to the name */
1921        snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1922                "%s%s Pen", name, suffix);
1923        snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1924                "%s%s Finger", name, suffix);
1925        snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1926                "%s%s Pad", name, suffix);
1927}
1928
1929static void wacom_release_resources(struct wacom *wacom)
1930{
1931        struct hid_device *hdev = wacom->hdev;
1932
1933        if (!wacom->resources)
1934                return;
1935
1936        devres_release_group(&hdev->dev, wacom);
1937
1938        wacom->resources = false;
1939
1940        wacom->wacom_wac.pen_input = NULL;
1941        wacom->wacom_wac.touch_input = NULL;
1942        wacom->wacom_wac.pad_input = NULL;
1943}
1944
1945static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
1946{
1947        if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
1948                wacom_wac->shared->type = wacom_wac->features.type;
1949                wacom_wac->shared->touch_input = wacom_wac->touch_input;
1950        }
1951
1952        if (wacom_wac->has_mute_touch_switch) {
1953                wacom_wac->shared->has_mute_touch_switch = true;
1954                wacom_wac->shared->is_touch_on = true;
1955        }
1956
1957        if (wacom_wac->shared->has_mute_touch_switch &&
1958            wacom_wac->shared->touch_input) {
1959                set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
1960                input_set_capability(wacom_wac->shared->touch_input, EV_SW,
1961                                     SW_MUTE_DEVICE);
1962        }
1963}
1964
1965static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1966{
1967        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1968        struct wacom_features *features = &wacom_wac->features;
1969        struct hid_device *hdev = wacom->hdev;
1970        int error;
1971        unsigned int connect_mask = HID_CONNECT_HIDRAW;
1972
1973        features->pktlen = wacom_compute_pktlen(hdev);
1974        if (features->pktlen > WACOM_PKGLEN_MAX)
1975                return -EINVAL;
1976
1977        if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1978                return -ENOMEM;
1979
1980        wacom->resources = true;
1981
1982        error = wacom_allocate_inputs(wacom);
1983        if (error)
1984                goto fail;
1985
1986        /*
1987         * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1988         * into debug mode for the touch part.
1989         * We ignore the other interfaces.
1990         */
1991        if (features->type == BAMBOO_PAD) {
1992                if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1993                        features->type = HID_GENERIC;
1994                } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1995                           (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1996                        error = -ENODEV;
1997                        goto fail;
1998                }
1999        }
2000
2001        /* set the default size in case we do not get them from hid */
2002        wacom_set_default_phy(features);
2003
2004        /* Retrieve the physical and logical size for touch devices */
2005        wacom_retrieve_hid_descriptor(hdev, features);
2006        wacom_setup_device_quirks(wacom);
2007
2008        if (features->device_type == WACOM_DEVICETYPE_NONE &&
2009            features->type != WIRELESS) {
2010                error = features->type == HID_GENERIC ? -ENODEV : 0;
2011
2012                dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2013                         hdev->name,
2014                         error ? "Ignoring" : "Assuming pen");
2015
2016                if (error)
2017                        goto fail;
2018
2019                features->device_type |= WACOM_DEVICETYPE_PEN;
2020        }
2021
2022        wacom_calculate_res(features);
2023
2024        wacom_update_name(wacom, wireless ? " (WL)" : "");
2025
2026        /* pen only Bamboo neither support touch nor pad */
2027        if ((features->type == BAMBOO_PEN) &&
2028            ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2029            (features->device_type & WACOM_DEVICETYPE_PAD))) {
2030                error = -ENODEV;
2031                goto fail;
2032        }
2033
2034        error = wacom_add_shared_data(hdev);
2035        if (error)
2036                goto fail;
2037
2038        if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2039             (features->quirks & WACOM_QUIRK_BATTERY)) {
2040                error = wacom_initialize_battery(wacom);
2041                if (error)
2042                        goto fail;
2043        }
2044
2045        error = wacom_register_inputs(wacom);
2046        if (error)
2047                goto fail;
2048
2049        if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2050                error = wacom_initialize_leds(wacom);
2051                if (error)
2052                        goto fail;
2053
2054                error = wacom_initialize_remotes(wacom);
2055                if (error)
2056                        goto fail;
2057        }
2058
2059        if (features->type == HID_GENERIC)
2060                connect_mask |= HID_CONNECT_DRIVER;
2061
2062        /* Regular HID work starts now */
2063        error = hid_hw_start(hdev, connect_mask);
2064        if (error) {
2065                hid_err(hdev, "hw start failed\n");
2066                goto fail;
2067        }
2068
2069        if (!wireless) {
2070                /* Note that if query fails it is not a hard failure */
2071                wacom_query_tablet_data(wacom);
2072        }
2073
2074        /* touch only Bamboo doesn't support pen */
2075        if ((features->type == BAMBOO_TOUCH) &&
2076            (features->device_type & WACOM_DEVICETYPE_PEN)) {
2077                cancel_delayed_work_sync(&wacom->init_work);
2078                _wacom_query_tablet_data(wacom);
2079                error = -ENODEV;
2080                goto fail_quirks;
2081        }
2082
2083        if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2084                error = hid_hw_open(hdev);
2085
2086        wacom_set_shared_values(wacom_wac);
2087        devres_close_group(&hdev->dev, wacom);
2088
2089        return 0;
2090
2091fail_quirks:
2092        hid_hw_stop(hdev);
2093fail:
2094        wacom_release_resources(wacom);
2095        return error;
2096}
2097
2098static void wacom_wireless_work(struct work_struct *work)
2099{
2100        struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2101        struct usb_device *usbdev = wacom->usbdev;
2102        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2103        struct hid_device *hdev1, *hdev2;
2104        struct wacom *wacom1, *wacom2;
2105        struct wacom_wac *wacom_wac1, *wacom_wac2;
2106        int error;
2107
2108        /*
2109         * Regardless if this is a disconnect or a new tablet,
2110         * remove any existing input and battery devices.
2111         */
2112
2113        wacom_destroy_battery(wacom);
2114
2115        /* Stylus interface */
2116        hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2117        wacom1 = hid_get_drvdata(hdev1);
2118        wacom_wac1 = &(wacom1->wacom_wac);
2119        wacom_release_resources(wacom1);
2120
2121        /* Touch interface */
2122        hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2123        wacom2 = hid_get_drvdata(hdev2);
2124        wacom_wac2 = &(wacom2->wacom_wac);
2125        wacom_release_resources(wacom2);
2126
2127        if (wacom_wac->pid == 0) {
2128                hid_info(wacom->hdev, "wireless tablet disconnected\n");
2129        } else {
2130                const struct hid_device_id *id = wacom_ids;
2131
2132                hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2133                         wacom_wac->pid);
2134
2135                while (id->bus) {
2136                        if (id->vendor == USB_VENDOR_ID_WACOM &&
2137                            id->product == wacom_wac->pid)
2138                                break;
2139                        id++;
2140                }
2141
2142                if (!id->bus) {
2143                        hid_info(wacom->hdev, "ignoring unknown PID.\n");
2144                        return;
2145                }
2146
2147                /* Stylus interface */
2148                wacom_wac1->features =
2149                        *((struct wacom_features *)id->driver_data);
2150
2151                wacom_wac1->pid = wacom_wac->pid;
2152                hid_hw_stop(hdev1);
2153                error = wacom_parse_and_register(wacom1, true);
2154                if (error)
2155                        goto fail;
2156
2157                /* Touch interface */
2158                if (wacom_wac1->features.touch_max ||
2159                    (wacom_wac1->features.type >= INTUOSHT &&
2160                    wacom_wac1->features.type <= BAMBOO_PT)) {
2161                        wacom_wac2->features =
2162                                *((struct wacom_features *)id->driver_data);
2163                        wacom_wac2->pid = wacom_wac->pid;
2164                        hid_hw_stop(hdev2);
2165                        error = wacom_parse_and_register(wacom2, true);
2166                        if (error)
2167                                goto fail;
2168                }
2169
2170                strlcpy(wacom_wac->name, wacom_wac1->name,
2171                        sizeof(wacom_wac->name));
2172                error = wacom_initialize_battery(wacom);
2173                if (error)
2174                        goto fail;
2175        }
2176
2177        return;
2178
2179fail:
2180        wacom_release_resources(wacom1);
2181        wacom_release_resources(wacom2);
2182        return;
2183}
2184
2185static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2186{
2187        struct wacom_remote *remote = wacom->remote;
2188        u32 serial = remote->remotes[index].serial;
2189        int i;
2190        unsigned long flags;
2191
2192        spin_lock_irqsave(&remote->remote_lock, flags);
2193        remote->remotes[index].registered = false;
2194        spin_unlock_irqrestore(&remote->remote_lock, flags);
2195
2196        if (remote->remotes[index].battery.battery)
2197                devres_release_group(&wacom->hdev->dev,
2198                                     &remote->remotes[index].battery.bat_desc);
2199
2200        if (remote->remotes[index].group.name)
2201                devres_release_group(&wacom->hdev->dev,
2202                                     &remote->remotes[index]);
2203
2204        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2205                if (remote->remotes[i].serial == serial) {
2206                        remote->remotes[i].serial = 0;
2207                        remote->remotes[i].group.name = NULL;
2208                        remote->remotes[i].registered = false;
2209                        remote->remotes[i].battery.battery = NULL;
2210                        wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2211                }
2212        }
2213}
2214
2215static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2216                                   unsigned int index)
2217{
2218        struct wacom_remote *remote = wacom->remote;
2219        struct device *dev = &wacom->hdev->dev;
2220        int error, k;
2221
2222        /* A remote can pair more than once with an EKR,
2223         * check to make sure this serial isn't already paired.
2224         */
2225        for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2226                if (remote->remotes[k].serial == serial)
2227                        break;
2228        }
2229
2230        if (k < WACOM_MAX_REMOTES) {
2231                remote->remotes[index].serial = serial;
2232                return 0;
2233        }
2234
2235        if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2236                return -ENOMEM;
2237
2238        error = wacom_remote_create_attr_group(wacom, serial, index);
2239        if (error)
2240                goto fail;
2241
2242        remote->remotes[index].input = wacom_allocate_input(wacom);
2243        if (!remote->remotes[index].input) {
2244                error = -ENOMEM;
2245                goto fail;
2246        }
2247        remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2248        remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2249
2250        if (!remote->remotes[index].input->name) {
2251                error = -EINVAL;
2252                goto fail;
2253        }
2254
2255        error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2256                                                   &wacom->wacom_wac);
2257        if (error)
2258                goto fail;
2259
2260        remote->remotes[index].serial = serial;
2261
2262        error = input_register_device(remote->remotes[index].input);
2263        if (error)
2264                goto fail;
2265
2266        error = wacom_led_groups_alloc_and_register_one(
2267                                        &remote->remotes[index].input->dev,
2268                                        wacom, index, 3, true);
2269        if (error)
2270                goto fail;
2271
2272        remote->remotes[index].registered = true;
2273
2274        devres_close_group(dev, &remote->remotes[index]);
2275        return 0;
2276
2277fail:
2278        devres_release_group(dev, &remote->remotes[index]);
2279        remote->remotes[index].serial = 0;
2280        return error;
2281}
2282
2283static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2284{
2285        struct wacom_remote *remote = wacom->remote;
2286        int error;
2287
2288        if (!remote->remotes[index].registered)
2289                return 0;
2290
2291        if (remote->remotes[index].battery.battery)
2292                return 0;
2293
2294        if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2295                return 0;
2296
2297        error = __wacom_initialize_battery(wacom,
2298                                        &wacom->remote->remotes[index].battery);
2299        if (error)
2300                return error;
2301
2302        return 0;
2303}
2304
2305static void wacom_remote_work(struct work_struct *work)
2306{
2307        struct wacom *wacom = container_of(work, struct wacom, remote_work);
2308        struct wacom_remote *remote = wacom->remote;
2309        struct wacom_remote_data data;
2310        unsigned long flags;
2311        unsigned int count;
2312        u32 serial;
2313        int i;
2314
2315        spin_lock_irqsave(&remote->remote_lock, flags);
2316
2317        count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2318
2319        if (count != sizeof(data)) {
2320                hid_err(wacom->hdev,
2321                        "workitem triggered without status available\n");
2322                spin_unlock_irqrestore(&remote->remote_lock, flags);
2323                return;
2324        }
2325
2326        if (!kfifo_is_empty(&remote->remote_fifo))
2327                wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2328
2329        spin_unlock_irqrestore(&remote->remote_lock, flags);
2330
2331        for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2332                serial = data.remote[i].serial;
2333                if (data.remote[i].connected) {
2334
2335                        if (remote->remotes[i].serial == serial) {
2336                                wacom_remote_attach_battery(wacom, i);
2337                                continue;
2338                        }
2339
2340                        if (remote->remotes[i].serial)
2341                                wacom_remote_destroy_one(wacom, i);
2342
2343                        wacom_remote_create_one(wacom, serial, i);
2344
2345                } else if (remote->remotes[i].serial) {
2346                        wacom_remote_destroy_one(wacom, i);
2347                }
2348        }
2349}
2350
2351static void wacom_mode_change_work(struct work_struct *work)
2352{
2353        struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2354        struct wacom_shared *shared = wacom->wacom_wac.shared;
2355        struct wacom *wacom1 = NULL;
2356        struct wacom *wacom2 = NULL;
2357        bool is_direct = wacom->wacom_wac.is_direct_mode;
2358        int error = 0;
2359
2360        if (shared->pen) {
2361                wacom1 = hid_get_drvdata(shared->pen);
2362                wacom_release_resources(wacom1);
2363                hid_hw_stop(wacom1->hdev);
2364                wacom1->wacom_wac.has_mode_change = true;
2365                wacom1->wacom_wac.is_direct_mode = is_direct;
2366        }
2367
2368        if (shared->touch) {
2369                wacom2 = hid_get_drvdata(shared->touch);
2370                wacom_release_resources(wacom2);
2371                hid_hw_stop(wacom2->hdev);
2372                wacom2->wacom_wac.has_mode_change = true;
2373                wacom2->wacom_wac.is_direct_mode = is_direct;
2374        }
2375
2376        if (wacom1) {
2377                error = wacom_parse_and_register(wacom1, false);
2378                if (error)
2379                        return;
2380        }
2381
2382        if (wacom2) {
2383                error = wacom_parse_and_register(wacom2, false);
2384                if (error)
2385                        return;
2386        }
2387
2388        return;
2389}
2390
2391static int wacom_probe(struct hid_device *hdev,
2392                const struct hid_device_id *id)
2393{
2394        struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2395        struct usb_device *dev = interface_to_usbdev(intf);
2396        struct wacom *wacom;
2397        struct wacom_wac *wacom_wac;
2398        struct wacom_features *features;
2399        int error;
2400
2401        if (!id->driver_data)
2402                return -EINVAL;
2403
2404        hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2405
2406        /* hid-core sets this quirk for the boot interface */
2407        hdev->quirks &= ~HID_QUIRK_NOGET;
2408
2409        wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2410        if (!wacom)
2411                return -ENOMEM;
2412
2413        hid_set_drvdata(hdev, wacom);
2414        wacom->hdev = hdev;
2415
2416        wacom_wac = &wacom->wacom_wac;
2417        wacom_wac->features = *((struct wacom_features *)id->driver_data);
2418        features = &wacom_wac->features;
2419
2420        if (features->check_for_hid_type && features->hid_type != hdev->type) {
2421                error = -ENODEV;
2422                goto fail;
2423        }
2424
2425        wacom_wac->hid_data.inputmode = -1;
2426        wacom_wac->mode_report = -1;
2427
2428        wacom->usbdev = dev;
2429        wacom->intf = intf;
2430        mutex_init(&wacom->lock);
2431        INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2432        INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2433        INIT_WORK(&wacom->battery_work, wacom_battery_work);
2434        INIT_WORK(&wacom->remote_work, wacom_remote_work);
2435        INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2436
2437        /* ask for the report descriptor to be loaded by HID */
2438        error = hid_parse(hdev);
2439        if (error) {
2440                hid_err(hdev, "parse failed\n");
2441                goto fail;
2442        }
2443
2444        error = wacom_parse_and_register(wacom, false);
2445        if (error)
2446                goto fail;
2447
2448        if (hdev->bus == BUS_BLUETOOTH) {
2449                error = device_create_file(&hdev->dev, &dev_attr_speed);
2450                if (error)
2451                        hid_warn(hdev,
2452                                 "can't create sysfs speed attribute err: %d\n",
2453                                 error);
2454        }
2455
2456        return 0;
2457
2458fail:
2459        hid_set_drvdata(hdev, NULL);
2460        return error;
2461}
2462
2463static void wacom_remove(struct hid_device *hdev)
2464{
2465        struct wacom *wacom = hid_get_drvdata(hdev);
2466        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2467        struct wacom_features *features = &wacom_wac->features;
2468
2469        if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2470                hid_hw_close(hdev);
2471
2472        hid_hw_stop(hdev);
2473
2474        cancel_delayed_work_sync(&wacom->init_work);
2475        cancel_work_sync(&wacom->wireless_work);
2476        cancel_work_sync(&wacom->battery_work);
2477        cancel_work_sync(&wacom->remote_work);
2478        cancel_work_sync(&wacom->mode_change_work);
2479        if (hdev->bus == BUS_BLUETOOTH)
2480                device_remove_file(&hdev->dev, &dev_attr_speed);
2481
2482        /* make sure we don't trigger the LEDs */
2483        wacom_led_groups_release(wacom);
2484
2485        if (wacom->wacom_wac.features.type != REMOTE)
2486                wacom_release_resources(wacom);
2487
2488        hid_set_drvdata(hdev, NULL);
2489}
2490
2491#ifdef CONFIG_PM
2492static int wacom_resume(struct hid_device *hdev)
2493{
2494        struct wacom *wacom = hid_get_drvdata(hdev);
2495
2496        mutex_lock(&wacom->lock);
2497
2498        /* switch to wacom mode first */
2499        _wacom_query_tablet_data(wacom);
2500        wacom_led_control(wacom);
2501
2502        mutex_unlock(&wacom->lock);
2503
2504        return 0;
2505}
2506
2507static int wacom_reset_resume(struct hid_device *hdev)
2508{
2509        return wacom_resume(hdev);
2510}
2511#endif /* CONFIG_PM */
2512
2513static struct hid_driver wacom_driver = {
2514        .name =         "wacom",
2515        .id_table =     wacom_ids,
2516        .probe =        wacom_probe,
2517        .remove =       wacom_remove,
2518        .report =       wacom_wac_report,
2519#ifdef CONFIG_PM
2520        .resume =       wacom_resume,
2521        .reset_resume = wacom_reset_resume,
2522#endif
2523        .raw_event =    wacom_raw_event,
2524};
2525module_hid_driver(wacom_driver);
2526
2527MODULE_VERSION(DRIVER_VERSION);
2528MODULE_AUTHOR(DRIVER_AUTHOR);
2529MODULE_DESCRIPTION(DRIVER_DESC);
2530MODULE_LICENSE("GPL");
2531