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 /* Driver data */ 26 struct uclogic_drvdata { 27 /* Interface parameters */ 28 struct uclogic_params params; 29 /* Pointer to the replacement report descriptor. NULL if none. */ 30 __u8 *desc_ptr; 31 /* 32 * Size of the replacement report descriptor. 33 * Only valid if desc_ptr is not NULL 34 */ 35 unsigned int desc_size; 36 /* Pen input device */ 37 struct input_dev *pen_input; 38 /* In-range timer */ 39 struct timer_list inrange_timer; 40 /* Last rotary encoder state, or U8_MAX for none */ 41 u8 re_state; 42 }; 43 44 /** 45 * uclogic_inrange_timeout - handle pen in-range state timeout. 46 * Emulate input events normally generated when pen goes out of range for 47 * tablets which don't report that. 48 * 49 * @t: The timer the timeout handler is attached to, stored in a struct 50 * uclogic_drvdata. 51 */ 52 static void uclogic_inrange_timeout(struct timer_list *t) 53 { 54 struct uclogic_drvdata *drvdata = from_timer(drvdata, t, 55 inrange_timer); 56 struct input_dev *input = drvdata->pen_input; 57 58 if (input == NULL) 59 return; 60 input_report_abs(input, ABS_PRESSURE, 0); 61 /* If BTN_TOUCH state is changing */ 62 if (test_bit(BTN_TOUCH, input->key)) { 63 input_event(input, EV_MSC, MSC_SCAN, 64 /* Digitizer Tip Switch usage */ 65 0xd0042); 66 input_report_key(input, BTN_TOUCH, 0); 67 } 68 input_report_key(input, BTN_TOOL_PEN, 0); 69 input_sync(input); 70 } 71 72 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc, 73 unsigned int *rsize) 74 { 75 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 76 77 if (drvdata->desc_ptr != NULL) { 78 rdesc = drvdata->desc_ptr; 79 *rsize = drvdata->desc_size; 80 } 81 return rdesc; 82 } 83 84 static int uclogic_input_configured(struct hid_device *hdev, 85 struct hid_input *hi) 86 { 87 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 88 struct uclogic_params *params = &drvdata->params; 89 char *name; 90 const char *suffix = NULL; 91 struct hid_field *field; 92 size_t len; 93 size_t i; 94 const struct uclogic_params_frame *frame; 95 96 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */ 97 if (!hi->report) 98 return 0; 99 100 /* 101 * If this is the input corresponding to the pen report 102 * in need of tweaking. 103 */ 104 if (hi->report->id == params->pen.id) { 105 /* Remember the input device so we can simulate events */ 106 drvdata->pen_input = hi->input; 107 } 108 109 /* If it's one of the frame devices */ 110 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 111 frame = ¶ms->frame_list[i]; 112 if (hi->report->id == frame->id) { 113 /* 114 * Disable EV_MSC reports for touch ring interfaces to 115 * make the Wacom driver pickup touch ring extents 116 */ 117 if (frame->touch_ring_byte > 0) 118 __clear_bit(EV_MSC, hi->input->evbit); 119 } 120 } 121 122 field = hi->report->field[0]; 123 124 switch (field->application) { 125 case HID_GD_KEYBOARD: 126 suffix = "Keyboard"; 127 break; 128 case HID_GD_MOUSE: 129 suffix = "Mouse"; 130 break; 131 case HID_GD_KEYPAD: 132 suffix = "Pad"; 133 break; 134 case HID_DG_PEN: 135 suffix = "Pen"; 136 break; 137 case HID_CP_CONSUMER_CONTROL: 138 suffix = "Consumer Control"; 139 break; 140 case HID_GD_SYSTEM_CONTROL: 141 suffix = "System Control"; 142 break; 143 } 144 145 if (suffix) { 146 len = strlen(hdev->name) + 2 + strlen(suffix); 147 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL); 148 if (name) { 149 snprintf(name, len, "%s %s", hdev->name, suffix); 150 hi->input->name = name; 151 } 152 } 153 154 return 0; 155 } 156 157 static int uclogic_probe(struct hid_device *hdev, 158 const struct hid_device_id *id) 159 { 160 int rc; 161 struct uclogic_drvdata *drvdata = NULL; 162 bool params_initialized = false; 163 164 if (!hid_is_usb(hdev)) 165 return -EINVAL; 166 167 /* 168 * libinput requires the pad interface to be on a different node 169 * than the pen, so use QUIRK_MULTI_INPUT for all tablets. 170 */ 171 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 172 173 /* Allocate and assign driver data */ 174 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 175 if (drvdata == NULL) { 176 rc = -ENOMEM; 177 goto failure; 178 } 179 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0); 180 drvdata->re_state = U8_MAX; 181 hid_set_drvdata(hdev, drvdata); 182 183 /* Initialize the device and retrieve interface parameters */ 184 rc = uclogic_params_init(&drvdata->params, hdev); 185 if (rc != 0) { 186 hid_err(hdev, "failed probing parameters: %d\n", rc); 187 goto failure; 188 } 189 params_initialized = true; 190 hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR, 191 UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params)); 192 if (drvdata->params.invalid) { 193 hid_info(hdev, "interface is invalid, ignoring\n"); 194 rc = -ENODEV; 195 goto failure; 196 } 197 198 /* Generate replacement report descriptor */ 199 rc = uclogic_params_get_desc(&drvdata->params, 200 &drvdata->desc_ptr, 201 &drvdata->desc_size); 202 if (rc) { 203 hid_err(hdev, 204 "failed generating replacement report descriptor: %d\n", 205 rc); 206 goto failure; 207 } 208 209 rc = hid_parse(hdev); 210 if (rc) { 211 hid_err(hdev, "parse failed\n"); 212 goto failure; 213 } 214 215 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 216 if (rc) { 217 hid_err(hdev, "hw start failed\n"); 218 goto failure; 219 } 220 221 return 0; 222 failure: 223 /* Assume "remove" might not be called if "probe" failed */ 224 if (params_initialized) 225 uclogic_params_cleanup(&drvdata->params); 226 return rc; 227 } 228 229 #ifdef CONFIG_PM 230 static int uclogic_resume(struct hid_device *hdev) 231 { 232 int rc; 233 struct uclogic_params params; 234 235 /* Re-initialize the device, but discard parameters */ 236 rc = uclogic_params_init(¶ms, hdev); 237 if (rc != 0) 238 hid_err(hdev, "failed to re-initialize the device\n"); 239 else 240 uclogic_params_cleanup(¶ms); 241 242 return rc; 243 } 244 #endif 245 246 /** 247 * uclogic_raw_event_pen - handle raw pen events (pen HID reports). 248 * 249 * @drvdata: Driver data. 250 * @data: Report data buffer, can be modified. 251 * @size: Report data size, bytes. 252 * 253 * Returns: 254 * Negative value on error (stops event delivery), zero for success. 255 */ 256 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata, 257 u8 *data, int size) 258 { 259 struct uclogic_params_pen *pen = &drvdata->params.pen; 260 261 WARN_ON(drvdata == NULL); 262 WARN_ON(data == NULL && size != 0); 263 264 /* If in-range reports are inverted */ 265 if (pen->inrange == 266 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 267 /* Invert the in-range bit */ 268 data[1] ^= 0x40; 269 } 270 /* 271 * If report contains fragmented high-resolution pen 272 * coordinates 273 */ 274 if (size >= 10 && pen->fragmented_hires) { 275 u8 pressure_low_byte; 276 u8 pressure_high_byte; 277 278 /* Lift pressure bytes */ 279 pressure_low_byte = data[6]; 280 pressure_high_byte = data[7]; 281 /* 282 * Move Y coord to make space for high-order X 283 * coord byte 284 */ 285 data[6] = data[5]; 286 data[5] = data[4]; 287 /* Move high-order X coord byte */ 288 data[4] = data[8]; 289 /* Move high-order Y coord byte */ 290 data[7] = data[9]; 291 /* Place pressure bytes */ 292 data[8] = pressure_low_byte; 293 data[9] = pressure_high_byte; 294 } 295 /* If we need to emulate in-range detection */ 296 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) { 297 /* Set in-range bit */ 298 data[1] |= 0x40; 299 /* (Re-)start in-range timeout */ 300 mod_timer(&drvdata->inrange_timer, 301 jiffies + msecs_to_jiffies(100)); 302 } 303 /* If we report tilt and Y direction is flipped */ 304 if (size >= 12 && pen->tilt_y_flipped) 305 data[11] = -data[11]; 306 307 return 0; 308 } 309 310 /** 311 * uclogic_raw_event_frame - handle raw frame events (frame HID reports). 312 * 313 * @drvdata: Driver data. 314 * @frame: The parameters of the frame controls to handle. 315 * @data: Report data buffer, can be modified. 316 * @size: Report data size, bytes. 317 * 318 * Returns: 319 * Negative value on error (stops event delivery), zero for success. 320 */ 321 static int uclogic_raw_event_frame( 322 struct uclogic_drvdata *drvdata, 323 const struct uclogic_params_frame *frame, 324 u8 *data, int size) 325 { 326 WARN_ON(drvdata == NULL); 327 WARN_ON(data == NULL && size != 0); 328 329 /* If need to, and can, set pad device ID for Wacom drivers */ 330 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) { 331 /* If we also have a touch ring and the finger left it */ 332 if (frame->touch_ring_byte > 0 && 333 frame->touch_ring_byte < size && 334 data[frame->touch_ring_byte] == 0) { 335 data[frame->dev_id_byte] = 0; 336 } else { 337 data[frame->dev_id_byte] = 0xf; 338 } 339 } 340 341 /* If need to, and can, read rotary encoder state change */ 342 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) { 343 unsigned int byte = frame->re_lsb / 8; 344 unsigned int bit = frame->re_lsb % 8; 345 346 u8 change; 347 u8 prev_state = drvdata->re_state; 348 /* Read Gray-coded state */ 349 u8 state = (data[byte] >> bit) & 0x3; 350 /* Encode state change into 2-bit signed integer */ 351 if ((prev_state == 1 && state == 0) || 352 (prev_state == 2 && state == 3)) { 353 change = 1; 354 } else if ((prev_state == 2 && state == 0) || 355 (prev_state == 1 && state == 3)) { 356 change = 3; 357 } else { 358 change = 0; 359 } 360 /* Write change */ 361 data[byte] = (data[byte] & ~((u8)3 << bit)) | 362 (change << bit); 363 /* Remember state */ 364 drvdata->re_state = state; 365 } 366 367 /* If need to, and can, transform the touch ring reports */ 368 if (frame->touch_ring_byte > 0 && frame->touch_ring_byte < size && 369 frame->touch_ring_flip_at != 0) { 370 __s8 value = data[frame->touch_ring_byte]; 371 372 if (value != 0) { 373 value = frame->touch_ring_flip_at - value; 374 if (value < 0) 375 value = frame->touch_ring_max + value; 376 377 data[frame->touch_ring_byte] = value; 378 } 379 } 380 381 return 0; 382 } 383 384 static int uclogic_raw_event(struct hid_device *hdev, 385 struct hid_report *report, 386 u8 *data, int size) 387 { 388 unsigned int report_id = report->id; 389 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 390 struct uclogic_params *params = &drvdata->params; 391 struct uclogic_params_pen_subreport *subreport; 392 struct uclogic_params_pen_subreport *subreport_list_end; 393 size_t i; 394 395 /* Do not handle anything but input reports */ 396 if (report->type != HID_INPUT_REPORT) 397 return 0; 398 399 while (true) { 400 /* Tweak pen reports, if necessary */ 401 if ((report_id == params->pen.id) && (size >= 2)) { 402 subreport_list_end = 403 params->pen.subreport_list + 404 ARRAY_SIZE(params->pen.subreport_list); 405 /* Try to match a subreport */ 406 for (subreport = params->pen.subreport_list; 407 subreport < subreport_list_end; subreport++) { 408 if (subreport->value != 0 && 409 subreport->value == data[1]) { 410 break; 411 } 412 } 413 /* If a subreport matched */ 414 if (subreport < subreport_list_end) { 415 /* Change to subreport ID, and restart */ 416 report_id = data[0] = subreport->id; 417 continue; 418 } else { 419 return uclogic_raw_event_pen(drvdata, data, size); 420 } 421 } 422 423 /* Tweak frame control reports, if necessary */ 424 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 425 if (report_id == params->frame_list[i].id) { 426 return uclogic_raw_event_frame( 427 drvdata, ¶ms->frame_list[i], 428 data, size); 429 } 430 } 431 432 break; 433 } 434 435 return 0; 436 } 437 438 static void uclogic_remove(struct hid_device *hdev) 439 { 440 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 441 442 del_timer_sync(&drvdata->inrange_timer); 443 hid_hw_stop(hdev); 444 kfree(drvdata->desc_ptr); 445 uclogic_params_cleanup(&drvdata->params); 446 } 447 448 static const struct hid_device_id uclogic_devices[] = { 449 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 450 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) }, 451 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 452 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) }, 453 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 454 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) }, 455 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 456 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) }, 457 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 458 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) }, 459 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 460 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) }, 461 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 462 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) }, 463 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 464 USB_DEVICE_ID_HUION_TABLET) }, 465 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 466 USB_DEVICE_ID_HUION_TABLET2) }, 467 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST, 468 USB_DEVICE_ID_TRUST_PANORA_TABLET) }, 469 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 470 USB_DEVICE_ID_HUION_TABLET) }, 471 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 472 USB_DEVICE_ID_YIYNOVA_TABLET) }, 473 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 474 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) }, 475 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 476 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) }, 477 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 478 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) }, 479 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 480 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) }, 481 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 482 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) }, 483 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 484 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) }, 485 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 486 USB_DEVICE_ID_UGEE_TABLET_G5) }, 487 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 488 USB_DEVICE_ID_UGEE_TABLET_EX07S) }, 489 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 490 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) }, 491 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 492 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) }, 493 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 494 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) }, 495 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 496 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) }, 497 { } 498 }; 499 MODULE_DEVICE_TABLE(hid, uclogic_devices); 500 501 static struct hid_driver uclogic_driver = { 502 .name = "uclogic", 503 .id_table = uclogic_devices, 504 .probe = uclogic_probe, 505 .remove = uclogic_remove, 506 .report_fixup = uclogic_report_fixup, 507 .raw_event = uclogic_raw_event, 508 .input_configured = uclogic_input_configured, 509 #ifdef CONFIG_PM 510 .resume = uclogic_resume, 511 .reset_resume = uclogic_resume, 512 #endif 513 }; 514 module_hid_driver(uclogic_driver); 515 516 MODULE_AUTHOR("Martin Rusko"); 517 MODULE_AUTHOR("Nikolai Kondrashov"); 518 MODULE_LICENSE("GPL"); 519