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