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_mapping(struct hid_device *hdev,
86 struct hid_field *field,
87 struct hid_usage *usage,
91 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
92 struct uclogic_params *params = &drvdata->params;
94 /* Discard invalid pen usages */
95 if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
98 /* Let hid-core decide what to do */
102 static int uclogic_input_configured(struct hid_device *hdev,
103 struct hid_input *hi)
105 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
106 struct uclogic_params *params = &drvdata->params;
108 const char *suffix = NULL;
109 struct hid_field *field;
112 const struct uclogic_params_frame *frame;
114 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
119 * If this is the input corresponding to the pen report
120 * in need of tweaking.
122 if (hi->report->id == params->pen.id) {
123 /* Remember the input device so we can simulate events */
124 drvdata->pen_input = hi->input;
127 /* If it's one of the frame devices */
128 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
129 frame = ¶ms->frame_list[i];
130 if (hi->report->id == frame->id) {
131 /* Assign custom suffix, if any */
132 suffix = frame->suffix;
134 * Disable EV_MSC reports for touch ring interfaces to
135 * make the Wacom driver pickup touch ring extents
137 if (frame->touch_byte > 0)
138 __clear_bit(EV_MSC, hi->input->evbit);
143 field = hi->report->field[0];
145 switch (field->application) {
146 case HID_GD_KEYBOARD:
158 case HID_CP_CONSUMER_CONTROL:
159 suffix = "Consumer Control";
161 case HID_GD_SYSTEM_CONTROL:
162 suffix = "System Control";
168 len = strlen(hdev->name) + 2 + strlen(suffix);
169 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
171 snprintf(name, len, "%s %s", hdev->name, suffix);
172 hi->input->name = name;
179 static int uclogic_probe(struct hid_device *hdev,
180 const struct hid_device_id *id)
183 struct uclogic_drvdata *drvdata = NULL;
184 bool params_initialized = false;
186 if (!hid_is_usb(hdev))
190 * libinput requires the pad interface to be on a different node
191 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
193 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
195 /* Allocate and assign driver data */
196 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
197 if (drvdata == NULL) {
201 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
202 drvdata->re_state = U8_MAX;
203 hid_set_drvdata(hdev, drvdata);
205 /* Initialize the device and retrieve interface parameters */
206 rc = uclogic_params_init(&drvdata->params, hdev);
208 hid_err(hdev, "failed probing parameters: %d\n", rc);
211 params_initialized = true;
212 hid_dbg(hdev, "parameters:\n");
213 uclogic_params_hid_dbg(hdev, &drvdata->params);
214 if (drvdata->params.invalid) {
215 hid_info(hdev, "interface is invalid, ignoring\n");
220 /* Generate replacement report descriptor */
221 rc = uclogic_params_get_desc(&drvdata->params,
223 &drvdata->desc_size);
226 "failed generating replacement report descriptor: %d\n",
231 rc = hid_parse(hdev);
233 hid_err(hdev, "parse failed\n");
237 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
239 hid_err(hdev, "hw start failed\n");
245 /* Assume "remove" might not be called if "probe" failed */
246 if (params_initialized)
247 uclogic_params_cleanup(&drvdata->params);
252 static int uclogic_resume(struct hid_device *hdev)
255 struct uclogic_params params;
257 /* Re-initialize the device, but discard parameters */
258 rc = uclogic_params_init(¶ms, hdev);
260 hid_err(hdev, "failed to re-initialize the device\n");
262 uclogic_params_cleanup(¶ms);
269 * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
271 * @drvdata: Driver data.
272 * @data: Report data buffer, can be modified.
273 * @size: Report data size, bytes.
276 * Negative value on error (stops event delivery), zero for success.
278 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
281 struct uclogic_params_pen *pen = &drvdata->params.pen;
283 WARN_ON(drvdata == NULL);
284 WARN_ON(data == NULL && size != 0);
286 /* If in-range reports are inverted */
288 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
289 /* Invert the in-range bit */
293 * If report contains fragmented high-resolution pen
296 if (size >= 10 && pen->fragmented_hires) {
297 u8 pressure_low_byte;
298 u8 pressure_high_byte;
300 /* Lift pressure bytes */
301 pressure_low_byte = data[6];
302 pressure_high_byte = data[7];
304 * Move Y coord to make space for high-order X
309 /* Move high-order X coord byte */
311 /* Move high-order Y coord byte */
313 /* Place pressure bytes */
314 data[8] = pressure_low_byte;
315 data[9] = pressure_high_byte;
317 /* If we need to emulate in-range detection */
318 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
319 /* Set in-range bit */
321 /* (Re-)start in-range timeout */
322 mod_timer(&drvdata->inrange_timer,
323 jiffies + msecs_to_jiffies(100));
325 /* If we report tilt and Y direction is flipped */
326 if (size >= 12 && pen->tilt_y_flipped)
327 data[11] = -data[11];
333 * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
335 * @drvdata: Driver data.
336 * @frame: The parameters of the frame controls to handle.
337 * @data: Report data buffer, can be modified.
338 * @size: Report data size, bytes.
341 * Negative value on error (stops event delivery), zero for success.
343 static int uclogic_raw_event_frame(
344 struct uclogic_drvdata *drvdata,
345 const struct uclogic_params_frame *frame,
348 WARN_ON(drvdata == NULL);
349 WARN_ON(data == NULL && size != 0);
351 /* If need to, and can, set pad device ID for Wacom drivers */
352 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
353 /* If we also have a touch ring and the finger left it */
354 if (frame->touch_byte > 0 && frame->touch_byte < size &&
355 data[frame->touch_byte] == 0) {
356 data[frame->dev_id_byte] = 0;
358 data[frame->dev_id_byte] = 0xf;
362 /* If need to, and can, read rotary encoder state change */
363 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
364 unsigned int byte = frame->re_lsb / 8;
365 unsigned int bit = frame->re_lsb % 8;
368 u8 prev_state = drvdata->re_state;
369 /* Read Gray-coded state */
370 u8 state = (data[byte] >> bit) & 0x3;
371 /* Encode state change into 2-bit signed integer */
372 if ((prev_state == 1 && state == 0) ||
373 (prev_state == 2 && state == 3)) {
375 } else if ((prev_state == 2 && state == 0) ||
376 (prev_state == 1 && state == 3)) {
382 data[byte] = (data[byte] & ~((u8)3 << bit)) |
385 drvdata->re_state = state;
388 /* If need to, and can, transform the touch ring reports */
389 if (frame->touch_byte > 0 && frame->touch_byte < size) {
390 __s8 value = data[frame->touch_byte];
393 if (frame->touch_flip_at != 0) {
394 value = frame->touch_flip_at - value;
396 value = frame->touch_max + value;
398 data[frame->touch_byte] = value - 1;
402 /* If need to, and can, transform the bitmap dial reports */
403 if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
404 if (data[frame->bitmap_dial_byte] == 2)
405 data[frame->bitmap_dial_byte] = -1;
411 static int uclogic_raw_event(struct hid_device *hdev,
412 struct hid_report *report,
415 unsigned int report_id = report->id;
416 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
417 struct uclogic_params *params = &drvdata->params;
418 struct uclogic_params_pen_subreport *subreport;
419 struct uclogic_params_pen_subreport *subreport_list_end;
422 /* Do not handle anything but input reports */
423 if (report->type != HID_INPUT_REPORT)
427 /* Tweak pen reports, if necessary */
428 if ((report_id == params->pen.id) && (size >= 2)) {
430 params->pen.subreport_list +
431 ARRAY_SIZE(params->pen.subreport_list);
432 /* Try to match a subreport */
433 for (subreport = params->pen.subreport_list;
434 subreport < subreport_list_end; subreport++) {
435 if (subreport->value != 0 &&
436 subreport->value == data[1]) {
440 /* If a subreport matched */
441 if (subreport < subreport_list_end) {
442 /* Change to subreport ID, and restart */
443 report_id = data[0] = subreport->id;
446 return uclogic_raw_event_pen(drvdata, data, size);
450 /* Tweak frame control reports, if necessary */
451 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
452 if (report_id == params->frame_list[i].id) {
453 return uclogic_raw_event_frame(
454 drvdata, ¶ms->frame_list[i],
465 static void uclogic_remove(struct hid_device *hdev)
467 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
469 del_timer_sync(&drvdata->inrange_timer);
471 kfree(drvdata->desc_ptr);
472 uclogic_params_cleanup(&drvdata->params);
475 static const struct hid_device_id uclogic_devices[] = {
476 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
477 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
478 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
479 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
480 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
481 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
482 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
483 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
484 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
485 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
486 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
487 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
488 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
489 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
490 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
491 USB_DEVICE_ID_HUION_TABLET) },
492 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
493 USB_DEVICE_ID_HUION_TABLET2) },
494 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
495 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
496 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
497 USB_DEVICE_ID_HUION_TABLET) },
498 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
499 USB_DEVICE_ID_YIYNOVA_TABLET) },
500 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
501 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
502 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
503 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
504 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
505 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
506 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
507 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
508 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
509 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
510 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
511 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
512 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
513 USB_DEVICE_ID_UGEE_TABLET_G5) },
514 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
515 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
516 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
517 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
518 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
519 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
520 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
521 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
522 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
523 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
524 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
525 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
526 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
527 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
530 MODULE_DEVICE_TABLE(hid, uclogic_devices);
532 static struct hid_driver uclogic_driver = {
534 .id_table = uclogic_devices,
535 .probe = uclogic_probe,
536 .remove = uclogic_remove,
537 .report_fixup = uclogic_report_fixup,
538 .raw_event = uclogic_raw_event,
539 .input_mapping = uclogic_input_mapping,
540 .input_configured = uclogic_input_configured,
542 .resume = uclogic_resume,
543 .reset_resume = uclogic_resume,
546 module_hid_driver(uclogic_driver);
548 MODULE_AUTHOR("Martin Rusko");
549 MODULE_AUTHOR("Nikolai Kondrashov");
550 MODULE_LICENSE("GPL");