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_mapping(struct hid_device *hdev, 85 struct hid_input *hi, 86 struct hid_field *field, 87 struct hid_usage *usage, 88 unsigned long **bit, 89 int *max) 90 { 91 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 92 struct uclogic_params *params = &drvdata->params; 93 94 /* discard the unused pen interface */ 95 if (params->pen_unused && (field->application == HID_DG_PEN)) 96 return -1; 97 98 /* let hid-core decide what to do */ 99 return 0; 100 } 101 102 static int uclogic_input_configured(struct hid_device *hdev, 103 struct hid_input *hi) 104 { 105 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 106 struct uclogic_params *params = &drvdata->params; 107 char *name; 108 const char *suffix = NULL; 109 struct hid_field *field; 110 size_t len; 111 112 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */ 113 if (!hi->report) 114 return 0; 115 116 /* 117 * If this is the input corresponding to the pen report 118 * in need of tweaking. 119 */ 120 if (hi->report->id == params->pen.id) { 121 /* Remember the input device so we can simulate events */ 122 drvdata->pen_input = hi->input; 123 } 124 125 field = hi->report->field[0]; 126 127 switch (field->application) { 128 case HID_GD_KEYBOARD: 129 suffix = "Keyboard"; 130 break; 131 case HID_GD_MOUSE: 132 suffix = "Mouse"; 133 break; 134 case HID_GD_KEYPAD: 135 suffix = "Pad"; 136 break; 137 case HID_DG_PEN: 138 suffix = "Pen"; 139 break; 140 case HID_CP_CONSUMER_CONTROL: 141 suffix = "Consumer Control"; 142 break; 143 case HID_GD_SYSTEM_CONTROL: 144 suffix = "System Control"; 145 break; 146 } 147 148 if (suffix) { 149 len = strlen(hdev->name) + 2 + strlen(suffix); 150 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL); 151 if (name) { 152 snprintf(name, len, "%s %s", hdev->name, suffix); 153 hi->input->name = name; 154 } 155 } 156 157 return 0; 158 } 159 160 static int uclogic_probe(struct hid_device *hdev, 161 const struct hid_device_id *id) 162 { 163 int rc; 164 struct uclogic_drvdata *drvdata = NULL; 165 bool params_initialized = false; 166 167 if (!hid_is_usb(hdev)) 168 return -EINVAL; 169 170 /* 171 * libinput requires the pad interface to be on a different node 172 * than the pen, so use QUIRK_MULTI_INPUT for all tablets. 173 */ 174 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 175 176 /* Allocate and assign driver data */ 177 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 178 if (drvdata == NULL) { 179 rc = -ENOMEM; 180 goto failure; 181 } 182 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0); 183 drvdata->re_state = U8_MAX; 184 hid_set_drvdata(hdev, drvdata); 185 186 /* Initialize the device and retrieve interface parameters */ 187 rc = uclogic_params_init(&drvdata->params, hdev); 188 if (rc != 0) { 189 hid_err(hdev, "failed probing parameters: %d\n", rc); 190 goto failure; 191 } 192 params_initialized = true; 193 hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR, 194 UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params)); 195 if (drvdata->params.invalid) { 196 hid_info(hdev, "interface is invalid, ignoring\n"); 197 rc = -ENODEV; 198 goto failure; 199 } 200 201 /* Generate replacement report descriptor */ 202 rc = uclogic_params_get_desc(&drvdata->params, 203 &drvdata->desc_ptr, 204 &drvdata->desc_size); 205 if (rc) { 206 hid_err(hdev, 207 "failed generating replacement report descriptor: %d\n", 208 rc); 209 goto failure; 210 } 211 212 rc = hid_parse(hdev); 213 if (rc) { 214 hid_err(hdev, "parse failed\n"); 215 goto failure; 216 } 217 218 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 219 if (rc) { 220 hid_err(hdev, "hw start failed\n"); 221 goto failure; 222 } 223 224 return 0; 225 failure: 226 /* Assume "remove" might not be called if "probe" failed */ 227 if (params_initialized) 228 uclogic_params_cleanup(&drvdata->params); 229 return rc; 230 } 231 232 #ifdef CONFIG_PM 233 static int uclogic_resume(struct hid_device *hdev) 234 { 235 int rc; 236 struct uclogic_params params; 237 238 /* Re-initialize the device, but discard parameters */ 239 rc = uclogic_params_init(¶ms, hdev); 240 if (rc != 0) 241 hid_err(hdev, "failed to re-initialize the device\n"); 242 else 243 uclogic_params_cleanup(¶ms); 244 245 return rc; 246 } 247 #endif 248 249 /** 250 * uclogic_raw_event_pen - handle raw pen events (pen HID reports). 251 * 252 * @drvdata: Driver data. 253 * @data: Report data buffer, can be modified. 254 * @size: Report data size, bytes. 255 * 256 * Returns: 257 * Negative value on error (stops event delivery), zero for success. 258 */ 259 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata, 260 u8 *data, int size) 261 { 262 struct uclogic_params_pen *pen = &drvdata->params.pen; 263 264 WARN_ON(drvdata == NULL); 265 WARN_ON(data == NULL && size != 0); 266 267 /* If in-range reports are inverted */ 268 if (pen->inrange == 269 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 270 /* Invert the in-range bit */ 271 data[1] ^= 0x40; 272 } 273 /* 274 * If report contains fragmented high-resolution pen 275 * coordinates 276 */ 277 if (size >= 10 && pen->fragmented_hires) { 278 u8 pressure_low_byte; 279 u8 pressure_high_byte; 280 281 /* Lift pressure bytes */ 282 pressure_low_byte = data[6]; 283 pressure_high_byte = data[7]; 284 /* 285 * Move Y coord to make space for high-order X 286 * coord byte 287 */ 288 data[6] = data[5]; 289 data[5] = data[4]; 290 /* Move high-order X coord byte */ 291 data[4] = data[8]; 292 /* Move high-order Y coord byte */ 293 data[7] = data[9]; 294 /* Place pressure bytes */ 295 data[8] = pressure_low_byte; 296 data[9] = pressure_high_byte; 297 } 298 /* If we need to emulate in-range detection */ 299 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) { 300 /* Set in-range bit */ 301 data[1] |= 0x40; 302 /* (Re-)start in-range timeout */ 303 mod_timer(&drvdata->inrange_timer, 304 jiffies + msecs_to_jiffies(100)); 305 } 306 /* If we report tilt and Y direction is flipped */ 307 if (size >= 12 && pen->tilt_y_flipped) 308 data[11] = -data[11]; 309 310 return 0; 311 } 312 313 /** 314 * uclogic_raw_event_frame - handle raw frame events (frame HID reports). 315 * 316 * @drvdata: Driver data. 317 * @data: Report data buffer, can be modified. 318 * @size: Report data size, bytes. 319 * 320 * Returns: 321 * Negative value on error (stops event delivery), zero for success. 322 */ 323 static int uclogic_raw_event_frame(struct uclogic_drvdata *drvdata, 324 u8 *data, int size) 325 { 326 struct uclogic_params_frame *frame = &drvdata->params.frame; 327 328 WARN_ON(drvdata == NULL); 329 WARN_ON(data == NULL && size != 0); 330 331 /* If need to, and can, set pad device ID for Wacom drivers */ 332 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) { 333 data[frame->dev_id_byte] = 0xf; 334 } 335 /* If need to, and can, read rotary encoder state change */ 336 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) { 337 unsigned int byte = frame->re_lsb / 8; 338 unsigned int bit = frame->re_lsb % 8; 339 340 u8 change; 341 u8 prev_state = drvdata->re_state; 342 /* Read Gray-coded state */ 343 u8 state = (data[byte] >> bit) & 0x3; 344 /* Encode state change into 2-bit signed integer */ 345 if ((prev_state == 1 && state == 0) || 346 (prev_state == 2 && state == 3)) { 347 change = 1; 348 } else if ((prev_state == 2 && state == 0) || 349 (prev_state == 1 && state == 3)) { 350 change = 3; 351 } else { 352 change = 0; 353 } 354 /* Write change */ 355 data[byte] = (data[byte] & ~((u8)3 << bit)) | 356 (change << bit); 357 /* Remember state */ 358 drvdata->re_state = state; 359 } 360 361 return 0; 362 } 363 364 static int uclogic_raw_event(struct hid_device *hdev, 365 struct hid_report *report, 366 u8 *data, int size) 367 { 368 unsigned int report_id = report->id; 369 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 370 struct uclogic_params *params = &drvdata->params; 371 372 /* Do not handle anything but input reports */ 373 if (report->type != HID_INPUT_REPORT) 374 return 0; 375 376 /* Tweak pen reports, if necessary */ 377 if (!params->pen_unused && 378 (report_id == params->pen.id) && 379 (size >= 2)) { 380 /* If it's the "virtual" frame controls report */ 381 if (params->frame.id != 0 && 382 data[1] & params->pen_frame_flag) { 383 /* Change to virtual frame controls report ID */ 384 report_id = data[0] = params->frame.id; 385 } else { 386 return uclogic_raw_event_pen(drvdata, data, size); 387 } 388 } 389 390 /* Tweak frame control reports, if necessary */ 391 if (report_id == params->frame.id) 392 return uclogic_raw_event_frame(drvdata, data, size); 393 394 return 0; 395 } 396 397 static void uclogic_remove(struct hid_device *hdev) 398 { 399 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 400 401 del_timer_sync(&drvdata->inrange_timer); 402 hid_hw_stop(hdev); 403 kfree(drvdata->desc_ptr); 404 uclogic_params_cleanup(&drvdata->params); 405 } 406 407 static const struct hid_device_id uclogic_devices[] = { 408 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 409 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) }, 410 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 411 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) }, 412 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 413 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) }, 414 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 415 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) }, 416 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 417 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) }, 418 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 419 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) }, 420 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 421 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) }, 422 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 423 USB_DEVICE_ID_HUION_TABLET) }, 424 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 425 USB_DEVICE_ID_HUION_TABLET2) }, 426 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST, 427 USB_DEVICE_ID_TRUST_PANORA_TABLET) }, 428 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 429 USB_DEVICE_ID_HUION_TABLET) }, 430 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 431 USB_DEVICE_ID_YIYNOVA_TABLET) }, 432 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 433 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) }, 434 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 435 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) }, 436 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 437 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) }, 438 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 439 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) }, 440 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 441 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) }, 442 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 443 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) }, 444 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 445 USB_DEVICE_ID_UGEE_TABLET_G5) }, 446 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 447 USB_DEVICE_ID_UGEE_TABLET_EX07S) }, 448 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 449 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) }, 450 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 451 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) }, 452 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 453 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) }, 454 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 455 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) }, 456 { } 457 }; 458 MODULE_DEVICE_TABLE(hid, uclogic_devices); 459 460 static struct hid_driver uclogic_driver = { 461 .name = "uclogic", 462 .id_table = uclogic_devices, 463 .probe = uclogic_probe, 464 .remove = uclogic_remove, 465 .report_fixup = uclogic_report_fixup, 466 .raw_event = uclogic_raw_event, 467 .input_mapping = uclogic_input_mapping, 468 .input_configured = uclogic_input_configured, 469 #ifdef CONFIG_PM 470 .resume = uclogic_resume, 471 .reset_resume = uclogic_resume, 472 #endif 473 }; 474 module_hid_driver(uclogic_driver); 475 476 MODULE_AUTHOR("Martin Rusko"); 477 MODULE_AUTHOR("Nikolai Kondrashov"); 478 MODULE_LICENSE("GPL"); 479