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