1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for UC-Logic devices not fully compliant with HID standard
5 * Copyright (c) 2010-2014 Nikolai Kondrashov
6 * Copyright (c) 2013 Martin Rusko
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)
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"
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.
30 * @t: The timer the timeout handler is attached to, stored in a struct
33 static void uclogic_inrange_timeout(struct timer_list *t)
35 struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
37 struct input_dev *input = drvdata->pen_input;
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 */
47 input_report_key(input, BTN_TOUCH, 0);
49 input_report_key(input, BTN_TOOL_PEN, 0);
53 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
56 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
58 if (drvdata->desc_ptr != NULL) {
59 rdesc = drvdata->desc_ptr;
60 *rsize = drvdata->desc_size;
65 static int uclogic_input_mapping(struct hid_device *hdev,
67 struct hid_field *field,
68 struct hid_usage *usage,
72 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
73 struct uclogic_params *params = &drvdata->params;
75 /* Discard invalid pen usages */
76 if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
79 /* Let hid-core decide what to do */
83 static int uclogic_input_configured(struct hid_device *hdev,
86 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
87 struct uclogic_params *params = &drvdata->params;
89 const char *suffix = NULL;
90 struct hid_field *field;
93 const struct uclogic_params_frame *frame;
95 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
100 * If this is the input corresponding to the pen report
101 * in need of tweaking.
103 if (hi->report->id == params->pen.id) {
104 /* Remember the input device so we can simulate events */
105 drvdata->pen_input = hi->input;
108 /* If it's one of the frame devices */
109 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
110 frame = ¶ms->frame_list[i];
111 if (hi->report->id == frame->id) {
112 /* Assign custom suffix, if any */
113 suffix = frame->suffix;
115 * Disable EV_MSC reports for touch ring interfaces to
116 * make the Wacom driver pickup touch ring extents
118 if (frame->touch_byte > 0)
119 __clear_bit(EV_MSC, hi->input->evbit);
124 field = hi->report->field[0];
126 switch (field->application) {
127 case HID_GD_KEYBOARD:
137 case HID_DG_DIGITIZER:
140 case HID_CP_CONSUMER_CONTROL:
141 suffix = "Consumer Control";
143 case HID_GD_SYSTEM_CONTROL:
144 suffix = "System Control";
150 len = strlen(hdev->name) + 2 + strlen(suffix);
151 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
153 snprintf(name, len, "%s %s", hdev->name, suffix);
154 hi->input->name = name;
161 static int uclogic_probe(struct hid_device *hdev,
162 const struct hid_device_id *id)
165 struct uclogic_drvdata *drvdata = NULL;
166 bool params_initialized = false;
168 if (!hid_is_usb(hdev))
172 * libinput requires the pad interface to be on a different node
173 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
175 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
176 hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
178 /* Allocate and assign driver data */
179 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
180 if (drvdata == NULL) {
184 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
185 drvdata->re_state = U8_MAX;
186 drvdata->quirks = id->driver_data;
187 hid_set_drvdata(hdev, drvdata);
189 /* Initialize the device and retrieve interface parameters */
190 rc = uclogic_params_init(&drvdata->params, hdev);
192 hid_err(hdev, "failed probing parameters: %d\n", rc);
195 params_initialized = true;
196 hid_dbg(hdev, "parameters:\n");
197 uclogic_params_hid_dbg(hdev, &drvdata->params);
198 if (drvdata->params.invalid) {
199 hid_info(hdev, "interface is invalid, ignoring\n");
204 /* Generate replacement report descriptor */
205 rc = uclogic_params_get_desc(&drvdata->params,
207 &drvdata->desc_size);
210 "failed generating replacement report descriptor: %d\n",
215 rc = hid_parse(hdev);
217 hid_err(hdev, "parse failed\n");
221 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
223 hid_err(hdev, "hw start failed\n");
229 /* Assume "remove" might not be called if "probe" failed */
230 if (params_initialized)
231 uclogic_params_cleanup(&drvdata->params);
236 static int uclogic_resume(struct hid_device *hdev)
239 struct uclogic_params params;
241 /* Re-initialize the device, but discard parameters */
242 rc = uclogic_params_init(¶ms, hdev);
244 hid_err(hdev, "failed to re-initialize the device\n");
246 uclogic_params_cleanup(¶ms);
253 * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
255 * @drvdata: Driver data.
256 * @data: Report data buffer, can be modified.
257 * @size: Report data size, bytes.
260 * Negative value on error (stops event delivery), zero for success.
262 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
265 struct uclogic_params_pen *pen = &drvdata->params.pen;
267 WARN_ON(drvdata == NULL);
268 WARN_ON(data == NULL && size != 0);
270 /* If in-range reports are inverted */
272 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
273 /* Invert the in-range bit */
277 * If report contains fragmented high-resolution pen
280 if (size >= 10 && pen->fragmented_hires) {
281 u8 pressure_low_byte;
282 u8 pressure_high_byte;
284 /* Lift pressure bytes */
285 pressure_low_byte = data[6];
286 pressure_high_byte = data[7];
288 * Move Y coord to make space for high-order X
293 /* Move high-order X coord byte */
295 /* Move high-order Y coord byte */
297 /* Place pressure bytes */
298 data[8] = pressure_low_byte;
299 data[9] = pressure_high_byte;
301 /* If we need to emulate in-range detection */
302 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
303 /* Set in-range bit */
305 /* (Re-)start in-range timeout */
306 mod_timer(&drvdata->inrange_timer,
307 jiffies + msecs_to_jiffies(100));
309 /* If we report tilt and Y direction is flipped */
310 if (size >= 12 && pen->tilt_y_flipped)
311 data[11] = -data[11];
317 * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
319 * @drvdata: Driver data.
320 * @frame: The parameters of the frame controls to handle.
321 * @data: Report data buffer, can be modified.
322 * @size: Report data size, bytes.
325 * Negative value on error (stops event delivery), zero for success.
327 static int uclogic_raw_event_frame(
328 struct uclogic_drvdata *drvdata,
329 const struct uclogic_params_frame *frame,
332 WARN_ON(drvdata == NULL);
333 WARN_ON(data == NULL && size != 0);
335 /* If need to, and can, set pad device ID for Wacom drivers */
336 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
337 /* If we also have a touch ring and the finger left it */
338 if (frame->touch_byte > 0 && frame->touch_byte < size &&
339 data[frame->touch_byte] == 0) {
340 data[frame->dev_id_byte] = 0;
342 data[frame->dev_id_byte] = 0xf;
346 /* If need to, and can, read rotary encoder state change */
347 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
348 unsigned int byte = frame->re_lsb / 8;
349 unsigned int bit = frame->re_lsb % 8;
352 u8 prev_state = drvdata->re_state;
353 /* Read Gray-coded state */
354 u8 state = (data[byte] >> bit) & 0x3;
355 /* Encode state change into 2-bit signed integer */
356 if ((prev_state == 1 && state == 0) ||
357 (prev_state == 2 && state == 3)) {
359 } else if ((prev_state == 2 && state == 0) ||
360 (prev_state == 1 && state == 3)) {
366 data[byte] = (data[byte] & ~((u8)3 << bit)) |
369 drvdata->re_state = state;
372 /* If need to, and can, transform the touch ring reports */
373 if (frame->touch_byte > 0 && frame->touch_byte < size) {
374 __s8 value = data[frame->touch_byte];
377 if (frame->touch_flip_at != 0) {
378 value = frame->touch_flip_at - value;
380 value = frame->touch_max + value;
382 data[frame->touch_byte] = value - 1;
386 /* If need to, and can, transform the bitmap dial reports */
387 if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
388 if (data[frame->bitmap_dial_byte] == 2)
389 data[frame->bitmap_dial_byte] = -1;
395 static int uclogic_raw_event(struct hid_device *hdev,
396 struct hid_report *report,
399 unsigned int report_id = report->id;
400 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
401 struct uclogic_params *params = &drvdata->params;
402 struct uclogic_params_pen_subreport *subreport;
403 struct uclogic_params_pen_subreport *subreport_list_end;
406 /* Do not handle anything but input reports */
407 if (report->type != HID_INPUT_REPORT)
411 /* Tweak pen reports, if necessary */
412 if ((report_id == params->pen.id) && (size >= 2)) {
414 params->pen.subreport_list +
415 ARRAY_SIZE(params->pen.subreport_list);
416 /* Try to match a subreport */
417 for (subreport = params->pen.subreport_list;
418 subreport < subreport_list_end; subreport++) {
419 if (subreport->value != 0 &&
420 subreport->value == data[1]) {
424 /* If a subreport matched */
425 if (subreport < subreport_list_end) {
426 /* Change to subreport ID, and restart */
427 report_id = data[0] = subreport->id;
430 return uclogic_raw_event_pen(drvdata, data, size);
434 /* Tweak frame control reports, if necessary */
435 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
436 if (report_id == params->frame_list[i].id) {
437 return uclogic_raw_event_frame(
438 drvdata, ¶ms->frame_list[i],
449 static void uclogic_remove(struct hid_device *hdev)
451 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
453 del_timer_sync(&drvdata->inrange_timer);
455 kfree(drvdata->desc_ptr);
456 uclogic_params_cleanup(&drvdata->params);
459 static const struct hid_device_id uclogic_devices[] = {
460 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
461 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
462 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
463 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
464 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
465 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
466 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
467 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
468 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
469 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
470 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
471 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
472 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
473 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
474 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
475 USB_DEVICE_ID_HUION_TABLET) },
476 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
477 USB_DEVICE_ID_HUION_TABLET2) },
478 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
479 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
480 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
481 USB_DEVICE_ID_HUION_TABLET) },
482 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
483 USB_DEVICE_ID_YIYNOVA_TABLET) },
484 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
485 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
486 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
487 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
488 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
489 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
490 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
491 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
492 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
493 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
494 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
495 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
496 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
497 USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
498 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
499 USB_DEVICE_ID_UGEE_TABLET_G5) },
500 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
501 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
502 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
503 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
504 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
505 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
506 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
507 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
508 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
509 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
510 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
511 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) },
512 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
513 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
514 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
515 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
516 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
517 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
518 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
519 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
520 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
521 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
522 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
523 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
526 MODULE_DEVICE_TABLE(hid, uclogic_devices);
528 static struct hid_driver uclogic_driver = {
530 .id_table = uclogic_devices,
531 .probe = uclogic_probe,
532 .remove = uclogic_remove,
533 .report_fixup = uclogic_report_fixup,
534 .raw_event = uclogic_raw_event,
535 .input_mapping = uclogic_input_mapping,
536 .input_configured = uclogic_input_configured,
538 .resume = uclogic_resume,
539 .reset_resume = uclogic_resume,
542 module_hid_driver(uclogic_driver);
544 MODULE_AUTHOR("Martin Rusko");
545 MODULE_AUTHOR("Nikolai Kondrashov");
546 MODULE_LICENSE("GPL");