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_exec_event_hook - if the received event is hooked schedules the
256 * @p: Tablet interface report parameters.
258 * @size: The size of event.
261 * Whether the event was hooked or not.
263 static bool uclogic_exec_event_hook(struct uclogic_params *p, u8 *event, int size)
265 struct uclogic_raw_event_hook *curr;
270 list_for_each_entry(curr, &p->event_hooks->list, list) {
271 if (curr->size == size && memcmp(curr->event, event, size) == 0) {
272 schedule_work(&curr->work);
281 * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
283 * @drvdata: Driver data.
284 * @data: Report data buffer, can be modified.
285 * @size: Report data size, bytes.
288 * Negative value on error (stops event delivery), zero for success.
290 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
293 struct uclogic_params_pen *pen = &drvdata->params.pen;
295 WARN_ON(drvdata == NULL);
296 WARN_ON(data == NULL && size != 0);
298 /* If in-range reports are inverted */
300 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
301 /* Invert the in-range bit */
305 * If report contains fragmented high-resolution pen
308 if (size >= 10 && pen->fragmented_hires) {
309 u8 pressure_low_byte;
310 u8 pressure_high_byte;
312 /* Lift pressure bytes */
313 pressure_low_byte = data[6];
314 pressure_high_byte = data[7];
316 * Move Y coord to make space for high-order X
321 /* Move high-order X coord byte */
323 /* Move high-order Y coord byte */
325 /* Place pressure bytes */
326 data[8] = pressure_low_byte;
327 data[9] = pressure_high_byte;
329 /* If we need to emulate in-range detection */
330 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
331 /* Set in-range bit */
333 /* (Re-)start in-range timeout */
334 mod_timer(&drvdata->inrange_timer,
335 jiffies + msecs_to_jiffies(100));
337 /* If we report tilt and Y direction is flipped */
338 if (size >= 12 && pen->tilt_y_flipped)
339 data[11] = -data[11];
345 * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
347 * @drvdata: Driver data.
348 * @frame: The parameters of the frame controls to handle.
349 * @data: Report data buffer, can be modified.
350 * @size: Report data size, bytes.
353 * Negative value on error (stops event delivery), zero for success.
355 static int uclogic_raw_event_frame(
356 struct uclogic_drvdata *drvdata,
357 const struct uclogic_params_frame *frame,
360 WARN_ON(drvdata == NULL);
361 WARN_ON(data == NULL && size != 0);
363 /* If need to, and can, set pad device ID for Wacom drivers */
364 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
365 /* If we also have a touch ring and the finger left it */
366 if (frame->touch_byte > 0 && frame->touch_byte < size &&
367 data[frame->touch_byte] == 0) {
368 data[frame->dev_id_byte] = 0;
370 data[frame->dev_id_byte] = 0xf;
374 /* If need to, and can, read rotary encoder state change */
375 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
376 unsigned int byte = frame->re_lsb / 8;
377 unsigned int bit = frame->re_lsb % 8;
380 u8 prev_state = drvdata->re_state;
381 /* Read Gray-coded state */
382 u8 state = (data[byte] >> bit) & 0x3;
383 /* Encode state change into 2-bit signed integer */
384 if ((prev_state == 1 && state == 0) ||
385 (prev_state == 2 && state == 3)) {
387 } else if ((prev_state == 2 && state == 0) ||
388 (prev_state == 1 && state == 3)) {
394 data[byte] = (data[byte] & ~((u8)3 << bit)) |
397 drvdata->re_state = state;
400 /* If need to, and can, transform the touch ring reports */
401 if (frame->touch_byte > 0 && frame->touch_byte < size) {
402 __s8 value = data[frame->touch_byte];
405 if (frame->touch_flip_at != 0) {
406 value = frame->touch_flip_at - value;
408 value = frame->touch_max + value;
410 data[frame->touch_byte] = value - 1;
414 /* If need to, and can, transform the bitmap dial reports */
415 if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
416 if (data[frame->bitmap_dial_byte] == 2)
417 data[frame->bitmap_dial_byte] = -1;
423 static int uclogic_raw_event(struct hid_device *hdev,
424 struct hid_report *report,
427 unsigned int report_id = report->id;
428 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
429 struct uclogic_params *params = &drvdata->params;
430 struct uclogic_params_pen_subreport *subreport;
431 struct uclogic_params_pen_subreport *subreport_list_end;
434 /* Do not handle anything but input reports */
435 if (report->type != HID_INPUT_REPORT)
438 if (uclogic_exec_event_hook(params, data, size))
442 /* Tweak pen reports, if necessary */
443 if ((report_id == params->pen.id) && (size >= 2)) {
445 params->pen.subreport_list +
446 ARRAY_SIZE(params->pen.subreport_list);
447 /* Try to match a subreport */
448 for (subreport = params->pen.subreport_list;
449 subreport < subreport_list_end; subreport++) {
450 if (subreport->value != 0 &&
451 subreport->value == data[1]) {
455 /* If a subreport matched */
456 if (subreport < subreport_list_end) {
457 /* Change to subreport ID, and restart */
458 report_id = data[0] = subreport->id;
461 return uclogic_raw_event_pen(drvdata, data, size);
465 /* Tweak frame control reports, if necessary */
466 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
467 if (report_id == params->frame_list[i].id) {
468 return uclogic_raw_event_frame(
469 drvdata, ¶ms->frame_list[i],
480 static void uclogic_remove(struct hid_device *hdev)
482 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
484 del_timer_sync(&drvdata->inrange_timer);
486 kfree(drvdata->desc_ptr);
487 uclogic_params_cleanup(&drvdata->params);
490 static const struct hid_device_id uclogic_devices[] = {
491 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
492 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
493 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
494 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
495 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
496 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
497 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
498 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
499 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
500 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
501 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
502 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
503 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
504 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
505 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
506 USB_DEVICE_ID_HUION_TABLET) },
507 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
508 USB_DEVICE_ID_HUION_TABLET2) },
509 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
510 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
511 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
512 USB_DEVICE_ID_HUION_TABLET) },
513 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
514 USB_DEVICE_ID_YIYNOVA_TABLET) },
515 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
516 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
517 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
518 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
519 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
520 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
521 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
522 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
523 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
524 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
525 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
526 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
527 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
528 USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
529 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
530 USB_DEVICE_ID_UGEE_TABLET_G5) },
531 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
532 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
533 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
534 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
535 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
536 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
537 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
538 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
539 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
540 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
541 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
542 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) },
543 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
544 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
545 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
546 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
547 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
548 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
549 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
550 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
551 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
552 .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
553 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
554 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
557 MODULE_DEVICE_TABLE(hid, uclogic_devices);
559 static struct hid_driver uclogic_driver = {
561 .id_table = uclogic_devices,
562 .probe = uclogic_probe,
563 .remove = uclogic_remove,
564 .report_fixup = uclogic_report_fixup,
565 .raw_event = uclogic_raw_event,
566 .input_mapping = uclogic_input_mapping,
567 .input_configured = uclogic_input_configured,
569 .resume = uclogic_resume,
570 .reset_resume = uclogic_resume,
573 module_hid_driver(uclogic_driver);
575 MODULE_AUTHOR("Martin Rusko");
576 MODULE_AUTHOR("Nikolai Kondrashov");
577 MODULE_LICENSE("GPL");
579 #ifdef CONFIG_HID_KUNIT_TEST
580 #include "hid-uclogic-core-test.c"