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 struct uclogic_drvdata {
27 /* Interface parameters */
28 struct uclogic_params params;
29 /* Pointer to the replacement report descriptor. NULL if none. */
32 * Size of the replacement report descriptor.
33 * Only valid if desc_ptr is not NULL
35 unsigned int desc_size;
36 /* Pen input device */
37 struct input_dev *pen_input;
39 struct timer_list inrange_timer;
40 /* Last rotary encoder state, or U8_MAX for none */
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.
49 * @t: The timer the timeout handler is attached to, stored in a struct
52 static void uclogic_inrange_timeout(struct timer_list *t)
54 struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
56 struct input_dev *input = drvdata->pen_input;
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 */
66 input_report_key(input, BTN_TOUCH, 0);
68 input_report_key(input, BTN_TOOL_PEN, 0);
72 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
75 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
77 if (drvdata->desc_ptr != NULL) {
78 rdesc = drvdata->desc_ptr;
79 *rsize = drvdata->desc_size;
84 static int uclogic_input_configured(struct hid_device *hdev,
87 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
88 struct uclogic_params *params = &drvdata->params;
90 const char *suffix = NULL;
91 struct hid_field *field;
94 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
99 * If this is the input corresponding to the pen report
100 * in need of tweaking.
102 if (hi->report->id == params->pen.id) {
103 /* Remember the input device so we can simulate events */
104 drvdata->pen_input = hi->input;
107 field = hi->report->field[0];
109 switch (field->application) {
110 case HID_GD_KEYBOARD:
122 case HID_CP_CONSUMER_CONTROL:
123 suffix = "Consumer Control";
125 case HID_GD_SYSTEM_CONTROL:
126 suffix = "System Control";
131 len = strlen(hdev->name) + 2 + strlen(suffix);
132 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
134 snprintf(name, len, "%s %s", hdev->name, suffix);
135 hi->input->name = name;
142 static int uclogic_probe(struct hid_device *hdev,
143 const struct hid_device_id *id)
146 struct uclogic_drvdata *drvdata = NULL;
147 bool params_initialized = false;
149 if (!hid_is_usb(hdev))
153 * libinput requires the pad interface to be on a different node
154 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
156 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
158 /* Allocate and assign driver data */
159 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
160 if (drvdata == NULL) {
164 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
165 drvdata->re_state = U8_MAX;
166 hid_set_drvdata(hdev, drvdata);
168 /* Initialize the device and retrieve interface parameters */
169 rc = uclogic_params_init(&drvdata->params, hdev);
171 hid_err(hdev, "failed probing parameters: %d\n", rc);
174 params_initialized = true;
175 hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
176 UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
177 if (drvdata->params.invalid) {
178 hid_info(hdev, "interface is invalid, ignoring\n");
183 /* Generate replacement report descriptor */
184 rc = uclogic_params_get_desc(&drvdata->params,
186 &drvdata->desc_size);
189 "failed generating replacement report descriptor: %d\n",
194 rc = hid_parse(hdev);
196 hid_err(hdev, "parse failed\n");
200 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
202 hid_err(hdev, "hw start failed\n");
208 /* Assume "remove" might not be called if "probe" failed */
209 if (params_initialized)
210 uclogic_params_cleanup(&drvdata->params);
215 static int uclogic_resume(struct hid_device *hdev)
218 struct uclogic_params params;
220 /* Re-initialize the device, but discard parameters */
221 rc = uclogic_params_init(¶ms, hdev);
223 hid_err(hdev, "failed to re-initialize the device\n");
225 uclogic_params_cleanup(¶ms);
232 * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
234 * @drvdata: Driver data.
235 * @data: Report data buffer, can be modified.
236 * @size: Report data size, bytes.
239 * Negative value on error (stops event delivery), zero for success.
241 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
244 struct uclogic_params_pen *pen = &drvdata->params.pen;
246 WARN_ON(drvdata == NULL);
247 WARN_ON(data == NULL && size != 0);
249 /* If in-range reports are inverted */
251 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
252 /* Invert the in-range bit */
256 * If report contains fragmented high-resolution pen
259 if (size >= 10 && pen->fragmented_hires) {
260 u8 pressure_low_byte;
261 u8 pressure_high_byte;
263 /* Lift pressure bytes */
264 pressure_low_byte = data[6];
265 pressure_high_byte = data[7];
267 * Move Y coord to make space for high-order X
272 /* Move high-order X coord byte */
274 /* Move high-order Y coord byte */
276 /* Place pressure bytes */
277 data[8] = pressure_low_byte;
278 data[9] = pressure_high_byte;
280 /* If we need to emulate in-range detection */
281 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
282 /* Set in-range bit */
284 /* (Re-)start in-range timeout */
285 mod_timer(&drvdata->inrange_timer,
286 jiffies + msecs_to_jiffies(100));
288 /* If we report tilt and Y direction is flipped */
289 if (size >= 12 && pen->tilt_y_flipped)
290 data[11] = -data[11];
296 * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
298 * @drvdata: Driver data.
299 * @frame: The parameters of the frame controls to handle.
300 * @data: Report data buffer, can be modified.
301 * @size: Report data size, bytes.
304 * Negative value on error (stops event delivery), zero for success.
306 static int uclogic_raw_event_frame(
307 struct uclogic_drvdata *drvdata,
308 const struct uclogic_params_frame *frame,
311 WARN_ON(drvdata == NULL);
312 WARN_ON(data == NULL && size != 0);
314 /* If need to, and can, set pad device ID for Wacom drivers */
315 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
316 data[frame->dev_id_byte] = 0xf;
318 /* If need to, and can, read rotary encoder state change */
319 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
320 unsigned int byte = frame->re_lsb / 8;
321 unsigned int bit = frame->re_lsb % 8;
324 u8 prev_state = drvdata->re_state;
325 /* Read Gray-coded state */
326 u8 state = (data[byte] >> bit) & 0x3;
327 /* Encode state change into 2-bit signed integer */
328 if ((prev_state == 1 && state == 0) ||
329 (prev_state == 2 && state == 3)) {
331 } else if ((prev_state == 2 && state == 0) ||
332 (prev_state == 1 && state == 3)) {
338 data[byte] = (data[byte] & ~((u8)3 << bit)) |
341 drvdata->re_state = state;
347 static int uclogic_raw_event(struct hid_device *hdev,
348 struct hid_report *report,
351 unsigned int report_id = report->id;
352 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
353 struct uclogic_params *params = &drvdata->params;
354 struct uclogic_params_pen_subreport *subreport;
355 struct uclogic_params_pen_subreport *subreport_list_end;
358 /* Do not handle anything but input reports */
359 if (report->type != HID_INPUT_REPORT)
363 /* Tweak pen reports, if necessary */
364 if ((report_id == params->pen.id) && (size >= 2)) {
366 params->pen.subreport_list +
367 ARRAY_SIZE(params->pen.subreport_list);
368 /* Try to match a subreport */
369 for (subreport = params->pen.subreport_list;
370 subreport < subreport_list_end; subreport++) {
371 if (subreport->value != 0 &&
372 subreport->value == data[1]) {
376 /* If a subreport matched */
377 if (subreport < subreport_list_end) {
378 /* Change to subreport ID, and restart */
379 report_id = data[0] = subreport->id;
382 return uclogic_raw_event_pen(drvdata, data, size);
386 /* Tweak frame control reports, if necessary */
387 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
388 if (report_id == params->frame_list[i].id) {
389 return uclogic_raw_event_frame(
390 drvdata, ¶ms->frame_list[i],
401 static void uclogic_remove(struct hid_device *hdev)
403 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
405 del_timer_sync(&drvdata->inrange_timer);
407 kfree(drvdata->desc_ptr);
408 uclogic_params_cleanup(&drvdata->params);
411 static const struct hid_device_id uclogic_devices[] = {
412 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
413 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
414 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
415 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
416 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
417 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
418 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
419 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
420 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
421 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
422 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
423 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
424 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
425 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
426 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
427 USB_DEVICE_ID_HUION_TABLET) },
428 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
429 USB_DEVICE_ID_HUION_TABLET2) },
430 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
431 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
432 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
433 USB_DEVICE_ID_HUION_TABLET) },
434 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
435 USB_DEVICE_ID_YIYNOVA_TABLET) },
436 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
437 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
438 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
439 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
440 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
441 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
442 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
443 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
444 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
445 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
446 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
447 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
448 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
449 USB_DEVICE_ID_UGEE_TABLET_G5) },
450 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
451 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
452 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
453 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
454 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
455 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
456 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
457 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
458 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
459 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
462 MODULE_DEVICE_TABLE(hid, uclogic_devices);
464 static struct hid_driver uclogic_driver = {
466 .id_table = uclogic_devices,
467 .probe = uclogic_probe,
468 .remove = uclogic_remove,
469 .report_fixup = uclogic_report_fixup,
470 .raw_event = uclogic_raw_event,
471 .input_configured = uclogic_input_configured,
473 .resume = uclogic_resume,
474 .reset_resume = uclogic_resume,
477 module_hid_driver(uclogic_driver);
479 MODULE_AUTHOR("Martin Rusko");
480 MODULE_AUTHOR("Nikolai Kondrashov");
481 MODULE_LICENSE("GPL");