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