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