1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * HID driver for UC-Logic devices not fully compliant with HID standard 4 * 5 * Copyright (c) 2010-2014 Nikolai Kondrashov 6 * Copyright (c) 2013 Martin Rusko 7 */ 8 9 /* 10 * This program is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the Free 12 * Software Foundation; either version 2 of the License, or (at your option) 13 * any later version. 14 */ 15 16 #include <linux/device.h> 17 #include <linux/hid.h> 18 #include <linux/module.h> 19 #include <linux/timer.h> 20 #include "usbhid/usbhid.h" 21 #include "hid-uclogic-params.h" 22 23 #include "hid-ids.h" 24 25 /** 26 * uclogic_inrange_timeout - handle pen in-range state timeout. 27 * Emulate input events normally generated when pen goes out of range for 28 * tablets which don't report that. 29 * 30 * @t: The timer the timeout handler is attached to, stored in a struct 31 * uclogic_drvdata. 32 */ 33 static void uclogic_inrange_timeout(struct timer_list *t) 34 { 35 struct uclogic_drvdata *drvdata = timer_container_of(drvdata, t, 36 inrange_timer); 37 struct input_dev *input = drvdata->pen_input; 38 39 if (input == NULL) 40 return; 41 input_report_abs(input, ABS_PRESSURE, 0); 42 /* If BTN_TOUCH state is changing */ 43 if (test_bit(BTN_TOUCH, input->key)) { 44 input_event(input, EV_MSC, MSC_SCAN, 45 /* Digitizer Tip Switch usage */ 46 0xd0042); 47 input_report_key(input, BTN_TOUCH, 0); 48 } 49 input_report_key(input, BTN_TOOL_PEN, 0); 50 input_sync(input); 51 } 52 53 static const __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc, 54 unsigned int *rsize) 55 { 56 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 57 58 if (drvdata->desc_ptr != NULL) { 59 *rsize = drvdata->desc_size; 60 return drvdata->desc_ptr; 61 } 62 return rdesc; 63 } 64 65 /* Buttons considered valid tablet pad inputs. */ 66 static const unsigned int uclogic_extra_input_mapping[] = { 67 BTN_0, 68 BTN_1, 69 BTN_2, 70 BTN_3, 71 BTN_4, 72 BTN_5, 73 BTN_6, 74 BTN_7, 75 BTN_8, 76 BTN_RIGHT, 77 BTN_MIDDLE, 78 BTN_SIDE, 79 BTN_EXTRA, 80 BTN_FORWARD, 81 BTN_BACK, 82 BTN_B, 83 BTN_A, 84 BTN_BASE, 85 BTN_BASE2, 86 BTN_X 87 }; 88 89 static int uclogic_input_mapping(struct hid_device *hdev, 90 struct hid_input *hi, 91 struct hid_field *field, 92 struct hid_usage *usage, 93 unsigned long **bit, 94 int *max) 95 { 96 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 97 struct uclogic_params *params = &drvdata->params; 98 99 if (field->application == HID_GD_KEYPAD) { 100 /* 101 * Remap input buttons to sensible ones that are not invalid. 102 * This only affects previous behavior for devices with more than ten or so buttons. 103 */ 104 const int key = (usage->hid & HID_USAGE) - 1; 105 106 if (key < ARRAY_SIZE(uclogic_extra_input_mapping)) { 107 hid_map_usage(hi, 108 usage, 109 bit, 110 max, 111 EV_KEY, 112 uclogic_extra_input_mapping[key]); 113 return 1; 114 } 115 } else if (field->application == HID_DG_PEN) { 116 /* Discard invalid pen usages */ 117 if (params->pen.usage_invalid) 118 return -1; 119 } 120 121 /* Let hid-core decide what to do */ 122 return 0; 123 } 124 125 static int uclogic_input_configured(struct hid_device *hdev, 126 struct hid_input *hi) 127 { 128 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 129 struct uclogic_params *params = &drvdata->params; 130 const char *suffix = NULL; 131 struct hid_field *field; 132 size_t i; 133 const struct uclogic_params_frame *frame; 134 135 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */ 136 if (!hi->report) 137 return 0; 138 139 /* 140 * If this is the input corresponding to the pen report 141 * in need of tweaking. 142 */ 143 if (hi->report->id == params->pen.id) { 144 /* Remember the input device so we can simulate events */ 145 drvdata->pen_input = hi->input; 146 } 147 148 /* If it's one of the frame devices */ 149 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 150 frame = ¶ms->frame_list[i]; 151 if (hi->report->id == frame->id) { 152 /* Assign custom suffix, if any */ 153 suffix = frame->suffix; 154 /* 155 * Disable EV_MSC reports for touch ring interfaces to 156 * make the Wacom driver pickup touch ring extents 157 */ 158 if (frame->touch_byte > 0) 159 __clear_bit(EV_MSC, hi->input->evbit); 160 } 161 } 162 163 if (!suffix) { 164 field = hi->report->field[0]; 165 166 switch (field->application) { 167 case HID_GD_KEYBOARD: 168 suffix = "Keyboard"; 169 break; 170 case HID_GD_MOUSE: 171 suffix = "Mouse"; 172 break; 173 case HID_GD_KEYPAD: 174 suffix = "Pad"; 175 break; 176 case HID_DG_PEN: 177 case HID_DG_DIGITIZER: 178 suffix = "Pen"; 179 break; 180 case HID_CP_CONSUMER_CONTROL: 181 suffix = "Consumer Control"; 182 break; 183 case HID_GD_SYSTEM_CONTROL: 184 suffix = "System Control"; 185 break; 186 } 187 } else { 188 hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 189 "%s %s", hdev->name, suffix); 190 if (!hi->input->name) 191 return -ENOMEM; 192 } 193 194 return 0; 195 } 196 197 static int uclogic_probe(struct hid_device *hdev, 198 const struct hid_device_id *id) 199 { 200 int rc; 201 struct uclogic_drvdata *drvdata = NULL; 202 bool params_initialized = false; 203 204 if (!hid_is_usb(hdev)) 205 return -EINVAL; 206 207 /* 208 * libinput requires the pad interface to be on a different node 209 * than the pen, so use QUIRK_MULTI_INPUT for all tablets. 210 */ 211 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 212 hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE; 213 214 /* Allocate and assign driver data */ 215 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 216 if (drvdata == NULL) { 217 rc = -ENOMEM; 218 goto failure; 219 } 220 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0); 221 drvdata->re_state = U8_MAX; 222 drvdata->quirks = id->driver_data; 223 hid_set_drvdata(hdev, drvdata); 224 225 /* Initialize the device and retrieve interface parameters */ 226 rc = uclogic_params_init(&drvdata->params, hdev); 227 if (rc != 0) { 228 hid_err(hdev, "failed probing parameters: %d\n", rc); 229 goto failure; 230 } 231 params_initialized = true; 232 hid_dbg(hdev, "parameters:\n"); 233 uclogic_params_hid_dbg(hdev, &drvdata->params); 234 if (drvdata->params.invalid) { 235 hid_info(hdev, "interface is invalid, ignoring\n"); 236 rc = -ENODEV; 237 goto failure; 238 } 239 240 /* Generate replacement report descriptor */ 241 rc = uclogic_params_get_desc(&drvdata->params, 242 &drvdata->desc_ptr, 243 &drvdata->desc_size); 244 if (rc) { 245 hid_err(hdev, 246 "failed generating replacement report descriptor: %d\n", 247 rc); 248 goto failure; 249 } 250 251 rc = hid_parse(hdev); 252 if (rc) { 253 hid_err(hdev, "parse failed\n"); 254 goto failure; 255 } 256 257 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 258 if (rc) { 259 hid_err(hdev, "hw start failed\n"); 260 goto failure; 261 } 262 263 return 0; 264 failure: 265 /* Assume "remove" might not be called if "probe" failed */ 266 if (params_initialized) 267 uclogic_params_cleanup(&drvdata->params); 268 return rc; 269 } 270 271 #ifdef CONFIG_PM 272 static int uclogic_resume(struct hid_device *hdev) 273 { 274 int rc; 275 struct uclogic_params params; 276 277 /* Re-initialize the device, but discard parameters */ 278 rc = uclogic_params_init(¶ms, hdev); 279 if (rc != 0) 280 hid_err(hdev, "failed to re-initialize the device\n"); 281 else 282 uclogic_params_cleanup(¶ms); 283 284 return rc; 285 } 286 #endif 287 288 /** 289 * uclogic_exec_event_hook - if the received event is hooked schedules the 290 * associated work. 291 * 292 * @p: Tablet interface report parameters. 293 * @event: Raw event. 294 * @size: The size of event. 295 * 296 * Returns: 297 * Whether the event was hooked or not. 298 */ 299 static bool uclogic_exec_event_hook(struct uclogic_params *p, u8 *event, int size) 300 { 301 struct uclogic_raw_event_hook *curr; 302 303 if (!p->event_hooks) 304 return false; 305 306 list_for_each_entry(curr, &p->event_hooks->list, list) { 307 if (curr->size == size && memcmp(curr->event, event, size) == 0) { 308 schedule_work(&curr->work); 309 return true; 310 } 311 } 312 313 return false; 314 } 315 316 /** 317 * uclogic_raw_event_pen - handle raw pen events (pen HID reports). 318 * 319 * @drvdata: Driver data. 320 * @data: Report data buffer, can be modified. 321 * @size: Report data size, bytes. 322 * 323 * Returns: 324 * Negative value on error (stops event delivery), zero for success. 325 */ 326 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata, 327 u8 *data, int size) 328 { 329 struct uclogic_params_pen *pen = &drvdata->params.pen; 330 331 WARN_ON(drvdata == NULL); 332 WARN_ON(data == NULL && size != 0); 333 334 /* If in-range reports are inverted */ 335 if (pen->inrange == 336 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 337 /* Invert the in-range bit */ 338 data[1] ^= 0x40; 339 } 340 /* 341 * If report contains fragmented high-resolution pen 342 * coordinates 343 */ 344 if (size >= 10 && pen->fragmented_hires) { 345 u8 pressure_low_byte; 346 u8 pressure_high_byte; 347 348 /* Lift pressure bytes */ 349 pressure_low_byte = data[6]; 350 pressure_high_byte = data[7]; 351 /* 352 * Move Y coord to make space for high-order X 353 * coord byte 354 */ 355 data[6] = data[5]; 356 data[5] = data[4]; 357 /* Move high-order X coord byte */ 358 data[4] = data[8]; 359 /* Move high-order Y coord byte */ 360 data[7] = data[9]; 361 /* Place pressure bytes */ 362 data[8] = pressure_low_byte; 363 data[9] = pressure_high_byte; 364 } 365 /* If we need to emulate in-range detection */ 366 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) { 367 /* Set in-range bit */ 368 data[1] |= 0x40; 369 /* (Re-)start in-range timeout */ 370 mod_timer(&drvdata->inrange_timer, 371 jiffies + msecs_to_jiffies(100)); 372 } 373 /* If we report tilt and Y direction is flipped */ 374 if (size >= 12 && pen->tilt_y_flipped) 375 data[11] = -data[11]; 376 377 return 0; 378 } 379 380 /** 381 * uclogic_raw_event_frame - handle raw frame events (frame HID reports). 382 * 383 * @drvdata: Driver data. 384 * @frame: The parameters of the frame controls to handle. 385 * @data: Report data buffer, can be modified. 386 * @size: Report data size, bytes. 387 * 388 * Returns: 389 * Negative value on error (stops event delivery), zero for success. 390 */ 391 static int uclogic_raw_event_frame( 392 struct uclogic_drvdata *drvdata, 393 const struct uclogic_params_frame *frame, 394 u8 *data, int size) 395 { 396 WARN_ON(drvdata == NULL); 397 WARN_ON(data == NULL && size != 0); 398 399 /* If need to, and can, set pad device ID for Wacom drivers */ 400 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) { 401 /* If we also have a touch ring and the finger left it */ 402 if (frame->touch_byte > 0 && frame->touch_byte < size && 403 data[frame->touch_byte] == 0) { 404 data[frame->dev_id_byte] = 0; 405 } else { 406 data[frame->dev_id_byte] = 0xf; 407 } 408 } 409 410 /* If need to, and can, read rotary encoder state change */ 411 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) { 412 unsigned int byte = frame->re_lsb / 8; 413 unsigned int bit = frame->re_lsb % 8; 414 415 u8 change; 416 u8 prev_state = drvdata->re_state; 417 /* Read Gray-coded state */ 418 u8 state = (data[byte] >> bit) & 0x3; 419 /* Encode state change into 2-bit signed integer */ 420 if ((prev_state == 1 && state == 0) || 421 (prev_state == 2 && state == 3)) { 422 change = 1; 423 } else if ((prev_state == 2 && state == 0) || 424 (prev_state == 1 && state == 3)) { 425 change = 3; 426 } else { 427 change = 0; 428 } 429 /* Write change */ 430 data[byte] = (data[byte] & ~((u8)3 << bit)) | 431 (change << bit); 432 /* Remember state */ 433 drvdata->re_state = state; 434 } 435 436 /* If need to, and can, transform the touch ring reports */ 437 if (frame->touch_byte > 0 && frame->touch_byte < size) { 438 __s8 value = data[frame->touch_byte]; 439 440 if (value != 0) { 441 if (frame->touch_flip_at != 0) { 442 value = frame->touch_flip_at - value; 443 if (value <= 0) 444 value = frame->touch_max + value; 445 } 446 data[frame->touch_byte] = value - 1; 447 } 448 } 449 450 /* If need to, and can, transform the bitmap dial reports */ 451 if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) { 452 switch (data[frame->bitmap_dial_byte]) { 453 case 2: 454 data[frame->bitmap_dial_byte] = -1; 455 break; 456 457 /* Everything below here is for tablets that shove multiple dials into 1 byte */ 458 case 16: 459 data[frame->bitmap_dial_byte] = 0; 460 data[frame->bitmap_second_dial_destination_byte] = 1; 461 break; 462 463 case 32: 464 data[frame->bitmap_dial_byte] = 0; 465 data[frame->bitmap_second_dial_destination_byte] = -1; 466 break; 467 } 468 } 469 470 return 0; 471 } 472 473 static int uclogic_raw_event(struct hid_device *hdev, 474 struct hid_report *report, 475 u8 *data, int size) 476 { 477 unsigned int report_id = report->id; 478 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 479 struct uclogic_params *params = &drvdata->params; 480 struct uclogic_params_pen_subreport *subreport; 481 struct uclogic_params_pen_subreport *subreport_list_end; 482 size_t i; 483 484 /* Do not handle anything but input reports */ 485 if (report->type != HID_INPUT_REPORT) 486 return 0; 487 488 if (uclogic_exec_event_hook(params, data, size)) 489 return 0; 490 491 while (true) { 492 /* Tweak pen reports, if necessary */ 493 if ((report_id == params->pen.id) && (size >= 2)) { 494 subreport_list_end = 495 params->pen.subreport_list + 496 ARRAY_SIZE(params->pen.subreport_list); 497 /* Try to match a subreport */ 498 for (subreport = params->pen.subreport_list; 499 subreport < subreport_list_end; subreport++) { 500 if (subreport->value != 0 && 501 subreport->value == data[1]) { 502 break; 503 } 504 } 505 /* If a subreport matched */ 506 if (subreport < subreport_list_end) { 507 /* Change to subreport ID, and restart */ 508 report_id = data[0] = subreport->id; 509 continue; 510 } else { 511 return uclogic_raw_event_pen(drvdata, data, size); 512 } 513 } 514 515 /* Tweak frame control reports, if necessary */ 516 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 517 if (report_id == params->frame_list[i].id) { 518 return uclogic_raw_event_frame( 519 drvdata, ¶ms->frame_list[i], 520 data, size); 521 } 522 } 523 524 break; 525 } 526 527 return 0; 528 } 529 530 static void uclogic_remove(struct hid_device *hdev) 531 { 532 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 533 534 timer_delete_sync(&drvdata->inrange_timer); 535 hid_hw_stop(hdev); 536 kfree(drvdata->desc_ptr); 537 uclogic_params_cleanup(&drvdata->params); 538 } 539 540 static const struct hid_device_id uclogic_devices[] = { 541 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 542 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) }, 543 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 544 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) }, 545 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 546 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) }, 547 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 548 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) }, 549 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 550 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) }, 551 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 552 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) }, 553 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 554 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) }, 555 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 556 USB_DEVICE_ID_HUION_TABLET) }, 557 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 558 USB_DEVICE_ID_HUION_TABLET2) }, 559 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST, 560 USB_DEVICE_ID_TRUST_PANORA_TABLET) }, 561 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 562 USB_DEVICE_ID_HUION_TABLET) }, 563 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 564 USB_DEVICE_ID_YIYNOVA_TABLET) }, 565 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 566 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) }, 567 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 568 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) }, 569 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 570 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) }, 571 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 572 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) }, 573 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 574 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) }, 575 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 576 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) }, 577 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 578 USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) }, 579 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 580 USB_DEVICE_ID_UGEE_TABLET_G5) }, 581 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 582 USB_DEVICE_ID_UGEE_TABLET_EX07S) }, 583 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 584 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) }, 585 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 586 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) }, 587 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 588 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) }, 589 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 590 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) }, 591 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 592 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) }, 593 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 594 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) }, 595 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 596 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW), 597 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK }, 598 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 599 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) }, 600 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 601 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW), 602 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK }, 603 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 604 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) }, 605 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 606 USB_DEVICE_ID_UGEE_XPPEN_TABLET_22R_PRO) }, 607 { } 608 }; 609 MODULE_DEVICE_TABLE(hid, uclogic_devices); 610 611 static struct hid_driver uclogic_driver = { 612 .name = "uclogic", 613 .id_table = uclogic_devices, 614 .probe = uclogic_probe, 615 .remove = uclogic_remove, 616 .report_fixup = uclogic_report_fixup, 617 .raw_event = uclogic_raw_event, 618 .input_mapping = uclogic_input_mapping, 619 .input_configured = uclogic_input_configured, 620 #ifdef CONFIG_PM 621 .resume = uclogic_resume, 622 .reset_resume = uclogic_resume, 623 #endif 624 }; 625 module_hid_driver(uclogic_driver); 626 627 MODULE_AUTHOR("Martin Rusko"); 628 MODULE_AUTHOR("Nikolai Kondrashov"); 629 MODULE_DESCRIPTION("HID driver for UC-Logic devices not fully compliant with HID standard"); 630 MODULE_LICENSE("GPL"); 631 MODULE_DESCRIPTION("HID driver for UC-Logic devices not fully compliant with HID standard"); 632 633 #ifdef CONFIG_HID_KUNIT_TEST 634 #include "hid-uclogic-core-test.c" 635 #endif 636