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