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
  17#define WAC_MSG_RETRIES         5
  18
  19#define WAC_CMD_WL_LED_CONTROL  0x03
  20#define WAC_CMD_LED_CONTROL     0x20
  21#define WAC_CMD_ICON_START      0x21
  22#define WAC_CMD_ICON_XFER       0x23
  23#define WAC_CMD_ICON_BT_XFER    0x26
  24#define WAC_CMD_RETRIES         10
  25
  26#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
  27#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
  28
  29static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
  30                            size_t size, unsigned int retries)
  31{
  32        int retval;
  33
  34        do {
  35                retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  36                                HID_REQ_GET_REPORT);
  37        } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
  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 == -EPIPE) && --retries);
  51
  52        return retval;
  53}
  54
  55static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  56                u8 *raw_data, int size)
  57{
  58        struct wacom *wacom = hid_get_drvdata(hdev);
  59
  60        if (size > WACOM_PKGLEN_MAX)
  61                return 1;
  62
  63        memcpy(wacom->wacom_wac.data, raw_data, size);
  64
  65        wacom_wac_irq(&wacom->wacom_wac, size);
  66
  67        return 0;
  68}
  69
  70static int wacom_open(struct input_dev *dev)
  71{
  72        struct wacom *wacom = input_get_drvdata(dev);
  73        int retval;
  74
  75        mutex_lock(&wacom->lock);
  76        retval = hid_hw_open(wacom->hdev);
  77        mutex_unlock(&wacom->lock);
  78
  79        return retval;
  80}
  81
  82static void wacom_close(struct input_dev *dev)
  83{
  84        struct wacom *wacom = input_get_drvdata(dev);
  85
  86        mutex_lock(&wacom->lock);
  87        hid_hw_close(wacom->hdev);
  88        mutex_unlock(&wacom->lock);
  89}
  90
  91/*
  92 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
  93 */
  94static int wacom_calc_hid_res(int logical_extents, int physical_extents,
  95                               unsigned unit, int exponent)
  96{
  97        struct hid_field field = {
  98                .logical_maximum = logical_extents,
  99                .physical_maximum = physical_extents,
 100                .unit = unit,
 101                .unit_exponent = exponent,
 102        };
 103
 104        return hidinput_calc_abs_res(&field, ABS_X);
 105}
 106
 107static void wacom_feature_mapping(struct hid_device *hdev,
 108                struct hid_field *field, struct hid_usage *usage)
 109{
 110        struct wacom *wacom = hid_get_drvdata(hdev);
 111        struct wacom_features *features = &wacom->wacom_wac.features;
 112        struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
 113        u8 *data;
 114        int ret;
 115
 116        switch (usage->hid) {
 117        case HID_DG_CONTACTMAX:
 118                /* leave touch_max as is if predefined */
 119                if (!features->touch_max) {
 120                        /* read manually */
 121                        data = kzalloc(2, GFP_KERNEL);
 122                        if (!data)
 123                                break;
 124                        data[0] = field->report->id;
 125                        ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
 126                                                data, 2, 0);
 127                        if (ret == 2)
 128                                features->touch_max = data[1];
 129                        kfree(data);
 130                }
 131                break;
 132        case HID_DG_INPUTMODE:
 133                /* Ignore if value index is out of bounds. */
 134                if (usage->usage_index >= field->report_count) {
 135                        dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
 136                        break;
 137                }
 138
 139                hid_data->inputmode = field->report->id;
 140                hid_data->inputmode_index = usage->usage_index;
 141                break;
 142        }
 143}
 144
 145/*
 146 * Interface Descriptor of wacom devices can be incomplete and
 147 * inconsistent so wacom_features table is used to store stylus
 148 * device's packet lengths, various maximum values, and tablet
 149 * resolution based on product ID's.
 150 *
 151 * For devices that contain 2 interfaces, wacom_features table is
 152 * inaccurate for the touch interface.  Since the Interface Descriptor
 153 * for touch interfaces has pretty complete data, this function exists
 154 * to query tablet for this missing information instead of hard coding in
 155 * an additional table.
 156 *
 157 * A typical Interface Descriptor for a stylus will contain a
 158 * boot mouse application collection that is not of interest and this
 159 * function will ignore it.
 160 *
 161 * It also contains a digitizer application collection that also is not
 162 * of interest since any information it contains would be duplicate
 163 * of what is in wacom_features. Usually it defines a report of an array
 164 * of bytes that could be used as max length of the stylus packet returned.
 165 * If it happens to define a Digitizer-Stylus Physical Collection then
 166 * the X and Y logical values contain valid data but it is ignored.
 167 *
 168 * A typical Interface Descriptor for a touch interface will contain a
 169 * Digitizer-Finger Physical Collection which will define both logical
 170 * X/Y maximum as well as the physical size of tablet. Since touch
 171 * interfaces haven't supported pressure or distance, this is enough
 172 * information to override invalid values in the wacom_features table.
 173 *
 174 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
 175 * data. We deal with them after returning from this function.
 176 */
 177static void wacom_usage_mapping(struct hid_device *hdev,
 178                struct hid_field *field, struct hid_usage *usage)
 179{
 180        struct wacom *wacom = hid_get_drvdata(hdev);
 181        struct wacom_features *features = &wacom->wacom_wac.features;
 182        bool finger = (field->logical == HID_DG_FINGER) ||
 183                      (field->physical == HID_DG_FINGER);
 184        bool pen = (field->logical == HID_DG_STYLUS) ||
 185                   (field->physical == HID_DG_STYLUS);
 186
 187        /*
 188        * Requiring Stylus Usage will ignore boot mouse
 189        * X/Y values and some cases of invalid Digitizer X/Y
 190        * values commonly reported.
 191        */
 192        if (!pen && !finger)
 193                return;
 194
 195        if (finger && !features->touch_max)
 196                /* touch device at least supports one touch point */
 197                features->touch_max = 1;
 198
 199        switch (usage->hid) {
 200        case HID_GD_X:
 201                features->x_max = field->logical_maximum;
 202                if (finger) {
 203                        features->device_type = BTN_TOOL_FINGER;
 204                        features->x_phy = field->physical_maximum;
 205                        if (features->type != BAMBOO_PT) {
 206                                features->unit = field->unit;
 207                                features->unitExpo = field->unit_exponent;
 208                        }
 209                } else {
 210                        features->device_type = BTN_TOOL_PEN;
 211                }
 212                break;
 213        case HID_GD_Y:
 214                features->y_max = field->logical_maximum;
 215                if (finger) {
 216                        features->y_phy = field->physical_maximum;
 217                        if (features->type != BAMBOO_PT) {
 218                                features->unit = field->unit;
 219                                features->unitExpo = field->unit_exponent;
 220                        }
 221                }
 222                break;
 223        case HID_DG_TIPPRESSURE:
 224                if (pen)
 225                        features->pressure_max = field->logical_maximum;
 226                break;
 227        }
 228
 229        if (features->type == HID_GENERIC)
 230                wacom_wac_usage_mapping(hdev, field, usage);
 231}
 232
 233static void wacom_parse_hid(struct hid_device *hdev,
 234                           struct wacom_features *features)
 235{
 236        struct hid_report_enum *rep_enum;
 237        struct hid_report *hreport;
 238        int i, j;
 239
 240        /* check features first */
 241        rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
 242        list_for_each_entry(hreport, &rep_enum->report_list, list) {
 243                for (i = 0; i < hreport->maxfield; i++) {
 244                        /* Ignore if report count is out of bounds. */
 245                        if (hreport->field[i]->report_count < 1)
 246                                continue;
 247
 248                        for (j = 0; j < hreport->field[i]->maxusage; j++) {
 249                                wacom_feature_mapping(hdev, hreport->field[i],
 250                                                hreport->field[i]->usage + j);
 251                        }
 252                }
 253        }
 254
 255        /* now check the input usages */
 256        rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
 257        list_for_each_entry(hreport, &rep_enum->report_list, list) {
 258
 259                if (!hreport->maxfield)
 260                        continue;
 261
 262                for (i = 0; i < hreport->maxfield; i++)
 263                        for (j = 0; j < hreport->field[i]->maxusage; j++)
 264                                wacom_usage_mapping(hdev, hreport->field[i],
 265                                                hreport->field[i]->usage + j);
 266        }
 267}
 268
 269static int wacom_hid_set_device_mode(struct hid_device *hdev)
 270{
 271        struct wacom *wacom = hid_get_drvdata(hdev);
 272        struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
 273        struct hid_report *r;
 274        struct hid_report_enum *re;
 275
 276        if (hid_data->inputmode < 0)
 277                return 0;
 278
 279        re = &(hdev->report_enum[HID_FEATURE_REPORT]);
 280        r = re->report_id_hash[hid_data->inputmode];
 281        if (r) {
 282                r->field[0]->value[hid_data->inputmode_index] = 2;
 283                hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
 284        }
 285        return 0;
 286}
 287
 288static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
 289                int length, int mode)
 290{
 291        unsigned char *rep_data;
 292        int error = -ENOMEM, limit = 0;
 293
 294        rep_data = kzalloc(length, GFP_KERNEL);
 295        if (!rep_data)
 296                return error;
 297
 298        do {
 299                rep_data[0] = report_id;
 300                rep_data[1] = mode;
 301
 302                error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
 303                                         length, 1);
 304                if (error >= 0)
 305                        error = wacom_get_report(hdev, HID_FEATURE_REPORT,
 306                                                 rep_data, length, 1);
 307        } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
 308
 309        kfree(rep_data);
 310
 311        return error < 0 ? error : 0;
 312}
 313
 314static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
 315                struct wacom_features *features)
 316{
 317        struct wacom *wacom = hid_get_drvdata(hdev);
 318        int ret;
 319        u8 rep_data[2];
 320
 321        switch (features->type) {
 322        case GRAPHIRE_BT:
 323                rep_data[0] = 0x03;
 324                rep_data[1] = 0x00;
 325                ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
 326                                        3);
 327
 328                if (ret >= 0) {
 329                        rep_data[0] = speed == 0 ? 0x05 : 0x06;
 330                        rep_data[1] = 0x00;
 331
 332                        ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
 333                                                rep_data, 2, 3);
 334
 335                        if (ret >= 0) {
 336                                wacom->wacom_wac.bt_high_speed = speed;
 337                                return 0;
 338                        }
 339                }
 340
 341                /*
 342                 * Note that if the raw queries fail, it's not a hard failure
 343                 * and it is safe to continue
 344                 */
 345                hid_warn(hdev, "failed to poke device, command %d, err %d\n",
 346                         rep_data[0], ret);
 347                break;
 348        case INTUOS4WL:
 349                if (speed == 1)
 350                        wacom->wacom_wac.bt_features &= ~0x20;
 351                else
 352                        wacom->wacom_wac.bt_features |= 0x20;
 353
 354                rep_data[0] = 0x03;
 355                rep_data[1] = wacom->wacom_wac.bt_features;
 356
 357                ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
 358                                        1);
 359                if (ret >= 0)
 360                        wacom->wacom_wac.bt_high_speed = speed;
 361                break;
 362        }
 363
 364        return 0;
 365}
 366
 367/*
 368 * Switch the tablet into its most-capable mode. Wacom tablets are
 369 * typically configured to power-up in a mode which sends mouse-like
 370 * reports to the OS. To get absolute position, pressure data, etc.
 371 * from the tablet, it is necessary to switch the tablet out of this
 372 * mode and into one which sends the full range of tablet data.
 373 */
 374static int wacom_query_tablet_data(struct hid_device *hdev,
 375                struct wacom_features *features)
 376{
 377        if (hdev->bus == BUS_BLUETOOTH)
 378                return wacom_bt_query_tablet_data(hdev, 1, features);
 379
 380        if (features->type == HID_GENERIC)
 381                return wacom_hid_set_device_mode(hdev);
 382
 383        if (features->device_type == BTN_TOOL_FINGER) {
 384                if (features->type > TABLETPC) {
 385                        /* MT Tablet PC touch */
 386                        return wacom_set_device_mode(hdev, 3, 4, 4);
 387                }
 388                else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
 389                        return wacom_set_device_mode(hdev, 18, 3, 2);
 390                }
 391        } else if (features->device_type == BTN_TOOL_PEN) {
 392                if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
 393                        return wacom_set_device_mode(hdev, 2, 2, 2);
 394                }
 395        }
 396
 397        return 0;
 398}
 399
 400static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
 401                                         struct wacom_features *features)
 402{
 403        struct wacom *wacom = hid_get_drvdata(hdev);
 404        struct usb_interface *intf = wacom->intf;
 405
 406        /* default features */
 407        features->device_type = BTN_TOOL_PEN;
 408        features->x_fuzz = 4;
 409        features->y_fuzz = 4;
 410        features->pressure_fuzz = 0;
 411        features->distance_fuzz = 0;
 412
 413        /*
 414         * The wireless device HID is basic and layout conflicts with
 415         * other tablets (monitor and touch interface can look like pen).
 416         * Skip the query for this type and modify defaults based on
 417         * interface number.
 418         */
 419        if (features->type == WIRELESS) {
 420                if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
 421                        features->device_type = 0;
 422                } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
 423                        features->device_type = BTN_TOOL_FINGER;
 424                        features->pktlen = WACOM_PKGLEN_BBTOUCH3;
 425                }
 426        }
 427
 428        /* only devices that support touch need to retrieve the info */
 429        if (features->type < BAMBOO_PT)
 430                return;
 431
 432        wacom_parse_hid(hdev, features);
 433}
 434
 435struct wacom_hdev_data {
 436        struct list_head list;
 437        struct kref kref;
 438        struct hid_device *dev;
 439        struct wacom_shared shared;
 440};
 441
 442static LIST_HEAD(wacom_udev_list);
 443static DEFINE_MUTEX(wacom_udev_list_lock);
 444
 445static bool wacom_are_sibling(struct hid_device *hdev,
 446                struct hid_device *sibling)
 447{
 448        struct wacom *wacom = hid_get_drvdata(hdev);
 449        struct wacom_features *features = &wacom->wacom_wac.features;
 450        int vid = features->oVid;
 451        int pid = features->oPid;
 452        int n1,n2;
 453
 454        if (vid == 0 && pid == 0) {
 455                vid = hdev->vendor;
 456                pid = hdev->product;
 457        }
 458
 459        if (vid != sibling->vendor || pid != sibling->product)
 460                return false;
 461
 462        /* Compare the physical path. */
 463        n1 = strrchr(hdev->phys, '.') - hdev->phys;
 464        n2 = strrchr(sibling->phys, '.') - sibling->phys;
 465        if (n1 != n2 || n1 <= 0 || n2 <= 0)
 466                return false;
 467
 468        return !strncmp(hdev->phys, sibling->phys, n1);
 469}
 470
 471static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
 472{
 473        struct wacom_hdev_data *data;
 474
 475        list_for_each_entry(data, &wacom_udev_list, list) {
 476                if (wacom_are_sibling(hdev, data->dev)) {
 477                        kref_get(&data->kref);
 478                        return data;
 479                }
 480        }
 481
 482        return NULL;
 483}
 484
 485static int wacom_add_shared_data(struct hid_device *hdev)
 486{
 487        struct wacom *wacom = hid_get_drvdata(hdev);
 488        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
 489        struct wacom_hdev_data *data;
 490        int retval = 0;
 491
 492        mutex_lock(&wacom_udev_list_lock);
 493
 494        data = wacom_get_hdev_data(hdev);
 495        if (!data) {
 496                data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
 497                if (!data) {
 498                        retval = -ENOMEM;
 499                        goto out;
 500                }
 501
 502                kref_init(&data->kref);
 503                data->dev = hdev;
 504                list_add_tail(&data->list, &wacom_udev_list);
 505        }
 506
 507        wacom_wac->shared = &data->shared;
 508
 509out:
 510        mutex_unlock(&wacom_udev_list_lock);
 511        return retval;
 512}
 513
 514static void wacom_release_shared_data(struct kref *kref)
 515{
 516        struct wacom_hdev_data *data =
 517                container_of(kref, struct wacom_hdev_data, kref);
 518
 519        mutex_lock(&wacom_udev_list_lock);
 520        list_del(&data->list);
 521        mutex_unlock(&wacom_udev_list_lock);
 522
 523        kfree(data);
 524}
 525
 526static void wacom_remove_shared_data(struct wacom_wac *wacom)
 527{
 528        struct wacom_hdev_data *data;
 529
 530        if (wacom->shared) {
 531                data = container_of(wacom->shared, struct wacom_hdev_data, shared);
 532                kref_put(&data->kref, wacom_release_shared_data);
 533                wacom->shared = NULL;
 534        }
 535}
 536
 537static int wacom_led_control(struct wacom *wacom)
 538{
 539        unsigned char *buf;
 540        int retval;
 541        unsigned char report_id = WAC_CMD_LED_CONTROL;
 542        int buf_size = 9;
 543
 544        if (wacom->wacom_wac.pid) { /* wireless connected */
 545                report_id = WAC_CMD_WL_LED_CONTROL;
 546                buf_size = 13;
 547        }
 548        buf = kzalloc(buf_size, GFP_KERNEL);
 549        if (!buf)
 550                return -ENOMEM;
 551
 552        if (wacom->wacom_wac.features.type >= INTUOS5S &&
 553            wacom->wacom_wac.features.type <= INTUOSPL) {
 554                /*
 555                 * Touch Ring and crop mark LED luminance may take on
 556                 * one of four values:
 557                 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
 558                 */
 559                int ring_led = wacom->led.select[0] & 0x03;
 560                int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
 561                int crop_lum = 0;
 562                unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
 563
 564                buf[0] = report_id;
 565                if (wacom->wacom_wac.pid) {
 566                        wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
 567                                         buf, buf_size, WAC_CMD_RETRIES);
 568                        buf[0] = report_id;
 569                        buf[4] = led_bits;
 570                } else
 571                        buf[1] = led_bits;
 572        }
 573        else {
 574                int led = wacom->led.select[0] | 0x4;
 575
 576                if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
 577                    wacom->wacom_wac.features.type == WACOM_24HD)
 578                        led |= (wacom->led.select[1] << 4) | 0x40;
 579
 580                buf[0] = report_id;
 581                buf[1] = led;
 582                buf[2] = wacom->led.llv;
 583                buf[3] = wacom->led.hlv;
 584                buf[4] = wacom->led.img_lum;
 585        }
 586
 587        retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
 588                                  WAC_CMD_RETRIES);
 589        kfree(buf);
 590
 591        return retval;
 592}
 593
 594static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
 595                const unsigned len, const void *img)
 596{
 597        unsigned char *buf;
 598        int i, retval;
 599        const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
 600
 601        buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
 602        if (!buf)
 603                return -ENOMEM;
 604
 605        /* Send 'start' command */
 606        buf[0] = WAC_CMD_ICON_START;
 607        buf[1] = 1;
 608        retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
 609                                  WAC_CMD_RETRIES);
 610        if (retval < 0)
 611                goto out;
 612
 613        buf[0] = xfer_id;
 614        buf[1] = button_id & 0x07;
 615        for (i = 0; i < 4; i++) {
 616                buf[2] = i;
 617                memcpy(buf + 3, img + i * chunk_len, chunk_len);
 618
 619                retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
 620                                          buf, chunk_len + 3, WAC_CMD_RETRIES);
 621                if (retval < 0)
 622                        break;
 623        }
 624
 625        /* Send 'stop' */
 626        buf[0] = WAC_CMD_ICON_START;
 627        buf[1] = 0;
 628        wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
 629                         WAC_CMD_RETRIES);
 630
 631out:
 632        kfree(buf);
 633        return retval;
 634}
 635
 636static ssize_t wacom_led_select_store(struct device *dev, int set_id,
 637                                      const char *buf, size_t count)
 638{
 639        struct hid_device *hdev = container_of(dev, struct hid_device, dev);
 640        struct wacom *wacom = hid_get_drvdata(hdev);
 641        unsigned int id;
 642        int err;
 643
 644        err = kstrtouint(buf, 10, &id);
 645        if (err)
 646                return err;
 647
 648        mutex_lock(&wacom->lock);
 649
 650        wacom->led.select[set_id] = id & 0x3;
 651        err = wacom_led_control(wacom);
 652
 653        mutex_unlock(&wacom->lock);
 654
 655        return err < 0 ? err : count;
 656}
 657
 658#define DEVICE_LED_SELECT_ATTR(SET_ID)                                  \
 659static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,     \
 660        struct device_attribute *attr, const char *buf, size_t count)   \
 661{                                                                       \
 662        return wacom_led_select_store(dev, SET_ID, buf, count);         \
 663}                                                                       \
 664static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,      \
 665        struct device_attribute *attr, char *buf)                       \
 666{                                                                       \
 667        struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
 668        struct wacom *wacom = hid_get_drvdata(hdev);                    \
 669        return scnprintf(buf, PAGE_SIZE, "%d\n",                        \
 670                         wacom->led.select[SET_ID]);                    \
 671}                                                                       \
 672static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,       \
 673                    wacom_led##SET_ID##_select_show,                    \
 674                    wacom_led##SET_ID##_select_store)
 675
 676DEVICE_LED_SELECT_ATTR(0);
 677DEVICE_LED_SELECT_ATTR(1);
 678
 679static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
 680                                     const char *buf, size_t count)
 681{
 682        unsigned int value;
 683        int err;
 684
 685        err = kstrtouint(buf, 10, &value);
 686        if (err)
 687                return err;
 688
 689        mutex_lock(&wacom->lock);
 690
 691        *dest = value & 0x7f;
 692        err = wacom_led_control(wacom);
 693
 694        mutex_unlock(&wacom->lock);
 695
 696        return err < 0 ? err : count;
 697}
 698
 699#define DEVICE_LUMINANCE_ATTR(name, field)                              \
 700static ssize_t wacom_##name##_luminance_store(struct device *dev,       \
 701        struct device_attribute *attr, const char *buf, size_t count)   \
 702{                                                                       \
 703        struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
 704        struct wacom *wacom = hid_get_drvdata(hdev);                    \
 705                                                                        \
 706        return wacom_luminance_store(wacom, &wacom->led.field,          \
 707                                     buf, count);                       \
 708}                                                                       \
 709static ssize_t wacom_##name##_luminance_show(struct device *dev,        \
 710        struct device_attribute *attr, char *buf)                       \
 711{                                                                       \
 712        struct wacom *wacom = dev_get_drvdata(dev);                     \
 713        return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);     \
 714}                                                                       \
 715static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,                  \
 716                   wacom_##name##_luminance_show,                       \
 717                   wacom_##name##_luminance_store)
 718
 719DEVICE_LUMINANCE_ATTR(status0, llv);
 720DEVICE_LUMINANCE_ATTR(status1, hlv);
 721DEVICE_LUMINANCE_ATTR(buttons, img_lum);
 722
 723static ssize_t wacom_button_image_store(struct device *dev, int button_id,
 724                                        const char *buf, size_t count)
 725{
 726        struct hid_device *hdev = container_of(dev, struct hid_device, dev);
 727        struct wacom *wacom = hid_get_drvdata(hdev);
 728        int err;
 729        unsigned len;
 730        u8 xfer_id;
 731
 732        if (hdev->bus == BUS_BLUETOOTH) {
 733                len = 256;
 734                xfer_id = WAC_CMD_ICON_BT_XFER;
 735        } else {
 736                len = 1024;
 737                xfer_id = WAC_CMD_ICON_XFER;
 738        }
 739
 740        if (count != len)
 741                return -EINVAL;
 742
 743        mutex_lock(&wacom->lock);
 744
 745        err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
 746
 747        mutex_unlock(&wacom->lock);
 748
 749        return err < 0 ? err : count;
 750}
 751
 752#define DEVICE_BTNIMG_ATTR(BUTTON_ID)                                   \
 753static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,      \
 754        struct device_attribute *attr, const char *buf, size_t count)   \
 755{                                                                       \
 756        return wacom_button_image_store(dev, BUTTON_ID, buf, count);    \
 757}                                                                       \
 758static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,        \
 759                   NULL, wacom_btnimg##BUTTON_ID##_store)
 760
 761DEVICE_BTNIMG_ATTR(0);
 762DEVICE_BTNIMG_ATTR(1);
 763DEVICE_BTNIMG_ATTR(2);
 764DEVICE_BTNIMG_ATTR(3);
 765DEVICE_BTNIMG_ATTR(4);
 766DEVICE_BTNIMG_ATTR(5);
 767DEVICE_BTNIMG_ATTR(6);
 768DEVICE_BTNIMG_ATTR(7);
 769
 770static struct attribute *cintiq_led_attrs[] = {
 771        &dev_attr_status_led0_select.attr,
 772        &dev_attr_status_led1_select.attr,
 773        NULL
 774};
 775
 776static struct attribute_group cintiq_led_attr_group = {
 777        .name = "wacom_led",
 778        .attrs = cintiq_led_attrs,
 779};
 780
 781static struct attribute *intuos4_led_attrs[] = {
 782        &dev_attr_status0_luminance.attr,
 783        &dev_attr_status1_luminance.attr,
 784        &dev_attr_status_led0_select.attr,
 785        &dev_attr_buttons_luminance.attr,
 786        &dev_attr_button0_rawimg.attr,
 787        &dev_attr_button1_rawimg.attr,
 788        &dev_attr_button2_rawimg.attr,
 789        &dev_attr_button3_rawimg.attr,
 790        &dev_attr_button4_rawimg.attr,
 791        &dev_attr_button5_rawimg.attr,
 792        &dev_attr_button6_rawimg.attr,
 793        &dev_attr_button7_rawimg.attr,
 794        NULL
 795};
 796
 797static struct attribute_group intuos4_led_attr_group = {
 798        .name = "wacom_led",
 799        .attrs = intuos4_led_attrs,
 800};
 801
 802static struct attribute *intuos5_led_attrs[] = {
 803        &dev_attr_status0_luminance.attr,
 804        &dev_attr_status_led0_select.attr,
 805        NULL
 806};
 807
 808static struct attribute_group intuos5_led_attr_group = {
 809        .name = "wacom_led",
 810        .attrs = intuos5_led_attrs,
 811};
 812
 813static int wacom_initialize_leds(struct wacom *wacom)
 814{
 815        int error;
 816
 817        /* Initialize default values */
 818        switch (wacom->wacom_wac.features.type) {
 819        case INTUOS4S:
 820        case INTUOS4:
 821        case INTUOS4WL:
 822        case INTUOS4L:
 823                wacom->led.select[0] = 0;
 824                wacom->led.select[1] = 0;
 825                wacom->led.llv = 10;
 826                wacom->led.hlv = 20;
 827                wacom->led.img_lum = 10;
 828                error = sysfs_create_group(&wacom->hdev->dev.kobj,
 829                                           &intuos4_led_attr_group);
 830                break;
 831
 832        case WACOM_24HD:
 833        case WACOM_21UX2:
 834                wacom->led.select[0] = 0;
 835                wacom->led.select[1] = 0;
 836                wacom->led.llv = 0;
 837                wacom->led.hlv = 0;
 838                wacom->led.img_lum = 0;
 839
 840                error = sysfs_create_group(&wacom->hdev->dev.kobj,
 841                                           &cintiq_led_attr_group);
 842                break;
 843
 844        case INTUOS5S:
 845        case INTUOS5:
 846        case INTUOS5L:
 847        case INTUOSPS:
 848        case INTUOSPM:
 849        case INTUOSPL:
 850                if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
 851                        wacom->led.select[0] = 0;
 852                        wacom->led.select[1] = 0;
 853                        wacom->led.llv = 32;
 854                        wacom->led.hlv = 0;
 855                        wacom->led.img_lum = 0;
 856
 857                        error = sysfs_create_group(&wacom->hdev->dev.kobj,
 858                                                  &intuos5_led_attr_group);
 859                } else
 860                        return 0;
 861                break;
 862
 863        default:
 864                return 0;
 865        }
 866
 867        if (error) {
 868                hid_err(wacom->hdev,
 869                        "cannot create sysfs group err: %d\n", error);
 870                return error;
 871        }
 872        wacom_led_control(wacom);
 873        wacom->led_initialized = true;
 874
 875        return 0;
 876}
 877
 878static void wacom_destroy_leds(struct wacom *wacom)
 879{
 880        if (!wacom->led_initialized)
 881                return;
 882
 883        wacom->led_initialized = false;
 884
 885        switch (wacom->wacom_wac.features.type) {
 886        case INTUOS4S:
 887        case INTUOS4:
 888        case INTUOS4WL:
 889        case INTUOS4L:
 890                sysfs_remove_group(&wacom->hdev->dev.kobj,
 891                                   &intuos4_led_attr_group);
 892                break;
 893
 894        case WACOM_24HD:
 895        case WACOM_21UX2:
 896                sysfs_remove_group(&wacom->hdev->dev.kobj,
 897                                   &cintiq_led_attr_group);
 898                break;
 899
 900        case INTUOS5S:
 901        case INTUOS5:
 902        case INTUOS5L:
 903        case INTUOSPS:
 904        case INTUOSPM:
 905        case INTUOSPL:
 906                if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
 907                        sysfs_remove_group(&wacom->hdev->dev.kobj,
 908                                           &intuos5_led_attr_group);
 909                break;
 910        }
 911}
 912
 913static enum power_supply_property wacom_battery_props[] = {
 914        POWER_SUPPLY_PROP_STATUS,
 915        POWER_SUPPLY_PROP_SCOPE,
 916        POWER_SUPPLY_PROP_CAPACITY
 917};
 918
 919static enum power_supply_property wacom_ac_props[] = {
 920        POWER_SUPPLY_PROP_PRESENT,
 921        POWER_SUPPLY_PROP_ONLINE,
 922        POWER_SUPPLY_PROP_SCOPE,
 923};
 924
 925static int wacom_battery_get_property(struct power_supply *psy,
 926                                      enum power_supply_property psp,
 927                                      union power_supply_propval *val)
 928{
 929        struct wacom *wacom = container_of(psy, struct wacom, battery);
 930        int ret = 0;
 931
 932        switch (psp) {
 933                case POWER_SUPPLY_PROP_SCOPE:
 934                        val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 935                        break;
 936                case POWER_SUPPLY_PROP_CAPACITY:
 937                        val->intval =
 938                                wacom->wacom_wac.battery_capacity;
 939                        break;
 940                case POWER_SUPPLY_PROP_STATUS:
 941                        if (wacom->wacom_wac.bat_charging)
 942                                val->intval = POWER_SUPPLY_STATUS_CHARGING;
 943                        else if (wacom->wacom_wac.battery_capacity == 100 &&
 944                                    wacom->wacom_wac.ps_connected)
 945                                val->intval = POWER_SUPPLY_STATUS_FULL;
 946                        else
 947                                val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 948                        break;
 949                default:
 950                        ret = -EINVAL;
 951                        break;
 952        }
 953
 954        return ret;
 955}
 956
 957static int wacom_ac_get_property(struct power_supply *psy,
 958                                enum power_supply_property psp,
 959                                union power_supply_propval *val)
 960{
 961        struct wacom *wacom = container_of(psy, struct wacom, ac);
 962        int ret = 0;
 963
 964        switch (psp) {
 965        case POWER_SUPPLY_PROP_PRESENT:
 966                /* fall through */
 967        case POWER_SUPPLY_PROP_ONLINE:
 968                val->intval = wacom->wacom_wac.ps_connected;
 969                break;
 970        case POWER_SUPPLY_PROP_SCOPE:
 971                val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 972                break;
 973        default:
 974                ret = -EINVAL;
 975                break;
 976        }
 977        return ret;
 978}
 979
 980static int wacom_initialize_battery(struct wacom *wacom)
 981{
 982        static atomic_t battery_no = ATOMIC_INIT(0);
 983        int error;
 984        unsigned long n;
 985
 986        if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
 987                n = atomic_inc_return(&battery_no) - 1;
 988
 989                wacom->battery.properties = wacom_battery_props;
 990                wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
 991                wacom->battery.get_property = wacom_battery_get_property;
 992                sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
 993                wacom->battery.name = wacom->wacom_wac.bat_name;
 994                wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
 995                wacom->battery.use_for_apm = 0;
 996
 997                wacom->ac.properties = wacom_ac_props;
 998                wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
 999                wacom->ac.get_property = wacom_ac_get_property;
1000                sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1001                wacom->ac.name = wacom->wacom_wac.ac_name;
1002                wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1003                wacom->ac.use_for_apm = 0;
1004
1005                error = power_supply_register(&wacom->hdev->dev,
1006                                              &wacom->battery);
1007                if (error)
1008                        return error;
1009
1010                power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1011
1012                error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1013                if (error) {
1014                        power_supply_unregister(&wacom->battery);
1015                        return error;
1016                }
1017
1018                power_supply_powers(&wacom->ac, &wacom->hdev->dev);
1019        }
1020
1021        return 0;
1022}
1023
1024static void wacom_destroy_battery(struct wacom *wacom)
1025{
1026        if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1027             wacom->battery.dev) {
1028                power_supply_unregister(&wacom->battery);
1029                wacom->battery.dev = NULL;
1030                power_supply_unregister(&wacom->ac);
1031                wacom->ac.dev = NULL;
1032        }
1033}
1034
1035static ssize_t wacom_show_speed(struct device *dev,
1036                                struct device_attribute
1037                                *attr, char *buf)
1038{
1039        struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1040        struct wacom *wacom = hid_get_drvdata(hdev);
1041
1042        return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1043}
1044
1045static ssize_t wacom_store_speed(struct device *dev,
1046                                struct device_attribute *attr,
1047                                const char *buf, size_t count)
1048{
1049        struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1050        struct wacom *wacom = hid_get_drvdata(hdev);
1051        u8 new_speed;
1052
1053        if (kstrtou8(buf, 0, &new_speed))
1054                return -EINVAL;
1055
1056        if (new_speed != 0 && new_speed != 1)
1057                return -EINVAL;
1058
1059        wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1060
1061        return count;
1062}
1063
1064static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1065                wacom_show_speed, wacom_store_speed);
1066
1067static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1068{
1069        struct input_dev *input_dev;
1070        struct hid_device *hdev = wacom->hdev;
1071        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1072
1073        input_dev = input_allocate_device();
1074        if (!input_dev)
1075                return NULL;
1076
1077        input_dev->name = wacom_wac->name;
1078        input_dev->phys = hdev->phys;
1079        input_dev->dev.parent = &hdev->dev;
1080        input_dev->open = wacom_open;
1081        input_dev->close = wacom_close;
1082        input_dev->uniq = hdev->uniq;
1083        input_dev->id.bustype = hdev->bus;
1084        input_dev->id.vendor  = hdev->vendor;
1085        input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1086        input_dev->id.version = hdev->version;
1087        input_set_drvdata(input_dev, wacom);
1088
1089        return input_dev;
1090}
1091
1092static void wacom_free_inputs(struct wacom *wacom)
1093{
1094        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1095
1096        if (wacom_wac->input)
1097                input_free_device(wacom_wac->input);
1098        if (wacom_wac->pad_input)
1099                input_free_device(wacom_wac->pad_input);
1100        wacom_wac->input = NULL;
1101        wacom_wac->pad_input = NULL;
1102}
1103
1104static int wacom_allocate_inputs(struct wacom *wacom)
1105{
1106        struct input_dev *input_dev, *pad_input_dev;
1107        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1108
1109        input_dev = wacom_allocate_input(wacom);
1110        pad_input_dev = wacom_allocate_input(wacom);
1111        if (!input_dev || !pad_input_dev) {
1112                wacom_free_inputs(wacom);
1113                return -ENOMEM;
1114        }
1115
1116        wacom_wac->input = input_dev;
1117        wacom_wac->pad_input = pad_input_dev;
1118        wacom_wac->pad_input->name = wacom_wac->pad_name;
1119
1120        return 0;
1121}
1122
1123static void wacom_clean_inputs(struct wacom *wacom)
1124{
1125        if (wacom->wacom_wac.input) {
1126                if (wacom->wacom_wac.input_registered)
1127                        input_unregister_device(wacom->wacom_wac.input);
1128                else
1129                        input_free_device(wacom->wacom_wac.input);
1130        }
1131        if (wacom->wacom_wac.pad_input) {
1132                if (wacom->wacom_wac.input_registered)
1133                        input_unregister_device(wacom->wacom_wac.pad_input);
1134                else
1135                        input_free_device(wacom->wacom_wac.pad_input);
1136        }
1137        wacom->wacom_wac.input = NULL;
1138        wacom->wacom_wac.pad_input = NULL;
1139        wacom_destroy_leds(wacom);
1140}
1141
1142static int wacom_register_inputs(struct wacom *wacom)
1143{
1144        struct input_dev *input_dev, *pad_input_dev;
1145        struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1146        int error;
1147
1148        input_dev = wacom_wac->input;
1149        pad_input_dev = wacom_wac->pad_input;
1150
1151        if (!input_dev || !pad_input_dev)
1152                return -EINVAL;
1153
1154        error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1155        if (error)
1156                return error;
1157
1158        error = input_register_device(input_dev);
1159        if (error)
1160                return error;
1161
1162        error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1163        if (error) {
1164                /* no pad in use on this interface */
1165                input_free_device(pad_input_dev);
1166                wacom_wac->pad_input = NULL;
1167                pad_input_dev = NULL;
1168        } else {
1169                error = input_register_device(pad_input_dev);
1170                if (error)
1171                        goto fail_register_pad_input;
1172
1173                error = wacom_initialize_leds(wacom);
1174                if (error)
1175                        goto fail_leds;
1176        }
1177
1178        wacom_wac->input_registered = true;
1179
1180        return 0;
1181
1182fail_leds:
1183        input_unregister_device(pad_input_dev);
1184        pad_input_dev = NULL;
1185fail_register_pad_input:
1186        input_unregister_device(input_dev);
1187        wacom_wac->input = NULL;
1188        return error;
1189}
1190
1191static void wacom_wireless_work(struct work_struct *work)
1192{
1193        struct wacom *wacom = container_of(work, struct wacom, work);
1194        struct usb_device *usbdev = wacom->usbdev;
1195        struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1196        struct hid_device *hdev1, *hdev2;
1197        struct wacom *wacom1, *wacom2;
1198        struct wacom_wac *wacom_wac1, *wacom_wac2;
1199        int error;
1200
1201        /*
1202         * Regardless if this is a disconnect or a new tablet,
1203         * remove any existing input and battery devices.
1204         */
1205
1206        wacom_destroy_battery(wacom);
1207
1208        /* Stylus interface */
1209        hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1210        wacom1 = hid_get_drvdata(hdev1);
1211        wacom_wac1 = &(wacom1->wacom_wac);
1212        wacom_clean_inputs(wacom1);
1213
1214        /* Touch interface */
1215        hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1216        wacom2 = hid_get_drvdata(hdev2);
1217        wacom_wac2 = &(wacom2->wacom_wac);
1218        wacom_clean_inputs(wacom2);
1219
1220        if (wacom_wac->pid == 0) {
1221                hid_info(wacom->hdev, "wireless tablet disconnected\n");
1222                wacom_wac1->shared->type = 0;
1223        } else {
1224                const struct hid_device_id *id = wacom_ids;
1225
1226                hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1227                         wacom_wac->pid);
1228
1229                while (id->bus) {
1230                        if (id->vendor == USB_VENDOR_ID_WACOM &&
1231                            id->product == wacom_wac->pid)
1232                                break;
1233                        id++;
1234                }
1235
1236                if (!id->bus) {
1237                        hid_info(wacom->hdev, "ignoring unknown PID.\n");
1238                        return;
1239                }
1240
1241                /* Stylus interface */
1242                wacom_wac1->features =
1243                        *((struct wacom_features *)id->driver_data);
1244                wacom_wac1->features.device_type = BTN_TOOL_PEN;
1245                snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1246                         wacom_wac1->features.name);
1247                snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1248                         wacom_wac1->features.name);
1249                wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1250                wacom_wac1->shared->type = wacom_wac1->features.type;
1251                wacom_wac1->pid = wacom_wac->pid;
1252                error = wacom_allocate_inputs(wacom1) ||
1253                        wacom_register_inputs(wacom1);
1254                if (error)
1255                        goto fail;
1256
1257                /* Touch interface */
1258                if (wacom_wac1->features.touch_max ||
1259                    wacom_wac1->features.type == INTUOSHT) {
1260                        wacom_wac2->features =
1261                                *((struct wacom_features *)id->driver_data);
1262                        wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1263                        wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1264                        wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1265                        if (wacom_wac2->features.touch_max)
1266                                snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1267                                         "%s (WL) Finger",wacom_wac2->features.name);
1268                        else
1269                                snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1270                                         "%s (WL) Pad",wacom_wac2->features.name);
1271                        snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1272                                 "%s (WL) Pad", wacom_wac2->features.name);
1273                        wacom_wac2->pid = wacom_wac->pid;
1274                        error = wacom_allocate_inputs(wacom2) ||
1275                                wacom_register_inputs(wacom2);
1276                        if (error)
1277                                goto fail;
1278
1279                        if (wacom_wac1->features.type == INTUOSHT &&
1280                            wacom_wac1->features.touch_max)
1281                                wacom_wac->shared->touch_input = wacom_wac2->input;
1282                }
1283
1284                error = wacom_initialize_battery(wacom);
1285                if (error)
1286                        goto fail;
1287        }
1288
1289        return;
1290
1291fail:
1292        wacom_clean_inputs(wacom1);
1293        wacom_clean_inputs(wacom2);
1294        return;
1295}
1296
1297/*
1298 * Not all devices report physical dimensions from HID.
1299 * Compute the default from hardcoded logical dimension
1300 * and resolution before driver overwrites them.
1301 */
1302static void wacom_set_default_phy(struct wacom_features *features)
1303{
1304        if (features->x_resolution) {
1305                features->x_phy = (features->x_max * 100) /
1306                                        features->x_resolution;
1307                features->y_phy = (features->y_max * 100) /
1308                                        features->y_resolution;
1309        }
1310}
1311
1312static void wacom_calculate_res(struct wacom_features *features)
1313{
1314        features->x_resolution = wacom_calc_hid_res(features->x_max,
1315                                                    features->x_phy,
1316                                                    features->unit,
1317                                                    features->unitExpo);
1318        features->y_resolution = wacom_calc_hid_res(features->y_max,
1319                                                    features->y_phy,
1320                                                    features->unit,
1321                                                    features->unitExpo);
1322}
1323
1324static int wacom_hid_report_len(struct hid_report *report)
1325{
1326        /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1327        return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1328}
1329
1330static size_t wacom_compute_pktlen(struct hid_device *hdev)
1331{
1332        struct hid_report_enum *report_enum;
1333        struct hid_report *report;
1334        size_t size = 0;
1335
1336        report_enum = hdev->report_enum + HID_INPUT_REPORT;
1337
1338        list_for_each_entry(report, &report_enum->report_list, list) {
1339                size_t report_size = wacom_hid_report_len(report);
1340                if (report_size > size)
1341                        size = report_size;
1342        }
1343
1344        return size;
1345}
1346
1347static int wacom_probe(struct hid_device *hdev,
1348                const struct hid_device_id *id)
1349{
1350        struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1351        struct usb_device *dev = interface_to_usbdev(intf);
1352        struct wacom *wacom;
1353        struct wacom_wac *wacom_wac;
1354        struct wacom_features *features;
1355        int error;
1356        unsigned int connect_mask = HID_CONNECT_HIDRAW;
1357
1358        if (!id->driver_data)
1359                return -EINVAL;
1360
1361        hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1362
1363        wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1364        if (!wacom)
1365                return -ENOMEM;
1366
1367        hid_set_drvdata(hdev, wacom);
1368        wacom->hdev = hdev;
1369
1370        /* ask for the report descriptor to be loaded by HID */
1371        error = hid_parse(hdev);
1372        if (error) {
1373                hid_err(hdev, "parse failed\n");
1374                goto fail_parse;
1375        }
1376
1377        wacom_wac = &wacom->wacom_wac;
1378        wacom_wac->features = *((struct wacom_features *)id->driver_data);
1379        features = &wacom_wac->features;
1380        features->pktlen = wacom_compute_pktlen(hdev);
1381        if (features->pktlen > WACOM_PKGLEN_MAX) {
1382                error = -EINVAL;
1383                goto fail_pktlen;
1384        }
1385
1386        if (features->check_for_hid_type && features->hid_type != hdev->type) {
1387                error = -ENODEV;
1388                goto fail_type;
1389        }
1390
1391        wacom->usbdev = dev;
1392        wacom->intf = intf;
1393        mutex_init(&wacom->lock);
1394        INIT_WORK(&wacom->work, wacom_wireless_work);
1395
1396        if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1397                error = wacom_allocate_inputs(wacom);
1398                if (error)
1399                        goto fail_allocate_inputs;
1400        }
1401
1402        /* set the default size in case we do not get them from hid */
1403        wacom_set_default_phy(features);
1404
1405        /* Retrieve the physical and logical size for touch devices */
1406        wacom_retrieve_hid_descriptor(hdev, features);
1407
1408        /*
1409         * Intuos5 has no useful data about its touch interface in its
1410         * HID descriptor. If this is the touch interface (PacketSize
1411         * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1412         */
1413        if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1414                if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1415                        features->device_type = BTN_TOOL_FINGER;
1416
1417                        features->x_max = 4096;
1418                        features->y_max = 4096;
1419                } else {
1420                        features->device_type = BTN_TOOL_PEN;
1421                }
1422        }
1423
1424        /*
1425         * Same thing for Bamboo 3rd gen.
1426         */
1427        if ((features->type == BAMBOO_PT) &&
1428            (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1429            (features->device_type == BTN_TOOL_PEN)) {
1430                features->device_type = BTN_TOOL_FINGER;
1431
1432                features->x_max = 4096;
1433                features->y_max = 4096;
1434        }
1435
1436        if (hdev->bus == BUS_BLUETOOTH)
1437                features->quirks |= WACOM_QUIRK_BATTERY;
1438
1439        wacom_setup_device_quirks(features);
1440
1441        /* set unit to "100th of a mm" for devices not reported by HID */
1442        if (!features->unit) {
1443                features->unit = 0x11;
1444                features->unitExpo = -3;
1445        }
1446        wacom_calculate_res(features);
1447
1448        strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1449        snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1450                "%s Pad", features->name);
1451
1452        if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1453                /* Append the device type to the name */
1454                if (features->device_type != BTN_TOOL_FINGER)
1455                        strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1456                else if (features->touch_max)
1457                        strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1458                else
1459                        strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1460
1461                error = wacom_add_shared_data(hdev);
1462                if (error)
1463                        goto fail_shared_data;
1464        }
1465
1466        if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1467             (features->quirks & WACOM_QUIRK_BATTERY)) {
1468                error = wacom_initialize_battery(wacom);
1469                if (error)
1470                        goto fail_battery;
1471        }
1472
1473        if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1474                error = wacom_register_inputs(wacom);
1475                if (error)
1476                        goto fail_register_inputs;
1477        }
1478
1479        if (hdev->bus == BUS_BLUETOOTH) {
1480                error = device_create_file(&hdev->dev, &dev_attr_speed);
1481                if (error)
1482                        hid_warn(hdev,
1483                                 "can't create sysfs speed attribute err: %d\n",
1484                                 error);
1485        }
1486
1487        if (features->type == HID_GENERIC)
1488                connect_mask |= HID_CONNECT_DRIVER;
1489
1490        /* Regular HID work starts now */
1491        error = hid_hw_start(hdev, connect_mask);
1492        if (error) {
1493                hid_err(hdev, "hw start failed\n");
1494                goto fail_hw_start;
1495        }
1496
1497        /* Note that if query fails it is not a hard failure */
1498        wacom_query_tablet_data(hdev, features);
1499
1500        if (features->quirks & WACOM_QUIRK_MONITOR)
1501                error = hid_hw_open(hdev);
1502
1503        if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1504                if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1505                        wacom_wac->shared->touch_input = wacom_wac->input;
1506        }
1507
1508        return 0;
1509
1510fail_hw_start:
1511        if (hdev->bus == BUS_BLUETOOTH)
1512                device_remove_file(&hdev->dev, &dev_attr_speed);
1513fail_register_inputs:
1514        wacom_clean_inputs(wacom);
1515        wacom_destroy_battery(wacom);
1516fail_battery:
1517        wacom_remove_shared_data(wacom_wac);
1518fail_shared_data:
1519        wacom_clean_inputs(wacom);
1520fail_allocate_inputs:
1521fail_type:
1522fail_pktlen:
1523fail_parse:
1524        kfree(wacom);
1525        hid_set_drvdata(hdev, NULL);
1526        return error;
1527}
1528
1529static void wacom_remove(struct hid_device *hdev)
1530{
1531        struct wacom *wacom = hid_get_drvdata(hdev);
1532
1533        hid_hw_stop(hdev);
1534
1535        cancel_work_sync(&wacom->work);
1536        wacom_clean_inputs(wacom);
1537        if (hdev->bus == BUS_BLUETOOTH)
1538                device_remove_file(&hdev->dev, &dev_attr_speed);
1539        wacom_destroy_battery(wacom);
1540        wacom_remove_shared_data(&wacom->wacom_wac);
1541
1542        hid_set_drvdata(hdev, NULL);
1543        kfree(wacom);
1544}
1545
1546#ifdef CONFIG_PM
1547static int wacom_resume(struct hid_device *hdev)
1548{
1549        struct wacom *wacom = hid_get_drvdata(hdev);
1550        struct wacom_features *features = &wacom->wacom_wac.features;
1551
1552        mutex_lock(&wacom->lock);
1553
1554        /* switch to wacom mode first */
1555        wacom_query_tablet_data(hdev, features);
1556        wacom_led_control(wacom);
1557
1558        mutex_unlock(&wacom->lock);
1559
1560        return 0;
1561}
1562
1563static int wacom_reset_resume(struct hid_device *hdev)
1564{
1565        return wacom_resume(hdev);
1566}
1567#endif /* CONFIG_PM */
1568
1569static struct hid_driver wacom_driver = {
1570        .name =         "wacom",
1571        .id_table =     wacom_ids,
1572        .probe =        wacom_probe,
1573        .remove =       wacom_remove,
1574        .event =        wacom_wac_event,
1575        .report =       wacom_wac_report,
1576#ifdef CONFIG_PM
1577        .resume =       wacom_resume,
1578        .reset_resume = wacom_reset_resume,
1579#endif
1580        .raw_event =    wacom_raw_event,
1581};
1582module_hid_driver(wacom_driver);
1583
1584MODULE_VERSION(DRIVER_VERSION);
1585MODULE_AUTHOR(DRIVER_AUTHOR);
1586MODULE_DESCRIPTION(DRIVER_DESC);
1587MODULE_LICENSE(DRIVER_LICENSE);
1588