blob: d8ab0139e5cdae30d70191ae08d7a479849b04d2 [file] [log] [blame]
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +02001// 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>
Nikolai Kondrashov01309e22019-02-10 12:13:54 +020019#include <linux/timer.h>
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020020#include "usbhid/usbhid.h"
Nikolai Kondrashov96142192019-02-10 12:13:51 +020021#include "hid-uclogic-params.h"
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020022
23#include "hid-ids.h"
24
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020025/* Driver data */
26struct uclogic_drvdata {
Nikolai Kondrashov96142192019-02-10 12:13:51 +020027 /* 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;
Nikolai Kondrashov01309e22019-02-10 12:13:54 +020036 /* Pen input device */
37 struct input_dev *pen_input;
38 /* In-range timer */
39 struct timer_list inrange_timer;
Nikolai Kondrashov8a476702019-02-10 12:14:04 +020040 /* Last rotary encoder state, or U8_MAX for none */
41 u8 re_state;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020042};
43
Nikolai Kondrashov01309e22019-02-10 12:13:54 +020044/**
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 */
52static 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
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020072static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
73 unsigned int *rsize)
74{
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020075 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
76
Nikolai Kondrashov96142192019-02-10 12:13:51 +020077 if (drvdata->desc_ptr != NULL) {
78 rdesc = drvdata->desc_ptr;
79 *rsize = drvdata->desc_size;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020080 }
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020081 return rdesc;
82}
83
Nikolai Kondrashov96142192019-02-10 12:13:51 +020084static 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)
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020090{
91 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
Nikolai Kondrashov96142192019-02-10 12:13:51 +020092 struct uclogic_params *params = &drvdata->params;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020093
94 /* discard the unused pen interface */
Nikolai Kondrashov96142192019-02-10 12:13:51 +020095 if (params->pen_unused && (field->application == HID_DG_PEN))
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +020096 return -1;
97
98 /* let hid-core decide what to do */
99 return 0;
100}
101
102static int uclogic_input_configured(struct hid_device *hdev,
103 struct hid_input *hi)
104{
Nikolai Kondrashov01309e22019-02-10 12:13:54 +0200105 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
106 struct uclogic_params *params = &drvdata->params;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200107 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
Nikolai Kondrashov01309e22019-02-10 12:13:54 +0200116 /*
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
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200125 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
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200160static int uclogic_probe(struct hid_device *hdev,
161 const struct hid_device_id *id)
162{
163 int rc;
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200164 struct uclogic_drvdata *drvdata = NULL;
165 bool params_initialized = false;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200166
Greg Kroah-Hartman93020952021-12-01 19:35:03 +0100167 if (!hid_is_usb(hdev))
168 return -EINVAL;
169
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200170 /*
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);
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200178 if (drvdata == NULL) {
179 rc = -ENOMEM;
180 goto failure;
181 }
Nikolai Kondrashov01309e22019-02-10 12:13:54 +0200182 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
Nikolai Kondrashov8a476702019-02-10 12:14:04 +0200183 drvdata->re_state = U8_MAX;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200184 hid_set_drvdata(hdev, drvdata);
185
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200186 /* 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 }
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200200
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200201 /* 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;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200210 }
211
212 rc = hid_parse(hdev);
213 if (rc) {
214 hid_err(hdev, "parse failed\n");
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200215 goto failure;
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200216 }
217
218 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
219 if (rc) {
220 hid_err(hdev, "hw start failed\n");
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200221 goto failure;
222 }
223
224 return 0;
225failure:
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
Nikolai Kondrashov251b4272019-02-10 12:13:52 +0200232#ifdef CONFIG_PM
233static 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(&params, hdev);
240 if (rc != 0)
241 hid_err(hdev, "failed to re-initialize the device\n");
242 else
243 uclogic_params_cleanup(&params);
244
245 return rc;
246}
247#endif
248
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200249static int uclogic_raw_event(struct hid_device *hdev,
250 struct hid_report *report,
251 u8 *data, int size)
252{
253 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
254 struct uclogic_params *params = &drvdata->params;
255
256 /* Tweak pen reports, if necessary */
257 if (!params->pen_unused &&
258 (report->type == HID_INPUT_REPORT) &&
259 (report->id == params->pen.id) &&
260 (size >= 2)) {
261 /* If it's the "virtual" frame controls report */
262 if (params->frame.id != 0 &&
263 data[1] & params->pen_frame_flag) {
264 /* Change to virtual frame controls report ID */
265 data[0] = params->frame.id;
266 return 0;
267 }
268 /* If in-range reports are inverted */
269 if (params->pen.inrange ==
270 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
271 /* Invert the in-range bit */
272 data[1] ^= 0x40;
273 }
Nikolai Kondrashov59f2e0f2019-02-10 12:13:55 +0200274 /*
275 * If report contains fragmented high-resolution pen
276 * coordinates
277 */
278 if (size >= 10 && params->pen.fragmented_hires) {
279 u8 pressure_low_byte;
280 u8 pressure_high_byte;
281
282 /* Lift pressure bytes */
283 pressure_low_byte = data[6];
284 pressure_high_byte = data[7];
285 /*
286 * Move Y coord to make space for high-order X
287 * coord byte
288 */
289 data[6] = data[5];
290 data[5] = data[4];
291 /* Move high-order X coord byte */
292 data[4] = data[8];
293 /* Move high-order Y coord byte */
294 data[7] = data[9];
295 /* Place pressure bytes */
296 data[8] = pressure_low_byte;
297 data[9] = pressure_high_byte;
298 }
Nikolai Kondrashov01309e22019-02-10 12:13:54 +0200299 /* If we need to emulate in-range detection */
300 if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
301 /* Set in-range bit */
302 data[1] |= 0x40;
303 /* (Re-)start in-range timeout */
304 mod_timer(&drvdata->inrange_timer,
305 jiffies + msecs_to_jiffies(100));
306 }
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200307 }
308
Nikolai Kondrashovfde44ac2019-02-10 12:14:03 +0200309 /* Tweak frame control reports, if necessary */
310 if ((report->type == HID_INPUT_REPORT) &&
311 (report->id == params->frame.id)) {
312 /* If need to, and can, set pad device ID for Wacom drivers */
313 if (params->frame.dev_id_byte > 0 &&
314 params->frame.dev_id_byte < size) {
315 data[params->frame.dev_id_byte] = 0xf;
316 }
Nikolai Kondrashov8a476702019-02-10 12:14:04 +0200317 /* If need to, and can, read rotary encoder state change */
318 if (params->frame.re_lsb > 0 &&
319 params->frame.re_lsb / 8 < size) {
320 unsigned int byte = params->frame.re_lsb / 8;
321 unsigned int bit = params->frame.re_lsb % 8;
322
323 u8 change;
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)) {
330 change = 1;
331 } else if ((prev_state == 2 && state == 0) ||
332 (prev_state == 1 && state == 3)) {
333 change = 3;
334 } else {
335 change = 0;
336 }
337 /* Write change */
338 data[byte] = (data[byte] & ~((u8)3 << bit)) |
339 (change << bit);
340 /* Remember state */
341 drvdata->re_state = state;
342 }
Nikolai Kondrashovfde44ac2019-02-10 12:14:03 +0200343 }
344
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200345 return 0;
346}
347
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200348static void uclogic_remove(struct hid_device *hdev)
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200349{
350 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
351
Nikolai Kondrashov01309e22019-02-10 12:13:54 +0200352 del_timer_sync(&drvdata->inrange_timer);
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200353 hid_hw_stop(hdev);
354 kfree(drvdata->desc_ptr);
355 uclogic_params_cleanup(&drvdata->params);
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200356}
357
358static const struct hid_device_id uclogic_devices[] = {
359 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
360 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
361 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
362 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
363 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
364 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
365 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
366 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
367 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
368 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
369 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
370 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
371 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
372 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200373 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
374 USB_DEVICE_ID_HUION_TABLET) },
Kyle Godbey315ffcc92019-06-15 18:15:06 -0500375 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
376 USB_DEVICE_ID_HUION_HS64) },
Cristian Kleinf7271b22020-12-27 14:08:15 +0100377 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
378 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200379 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
380 USB_DEVICE_ID_HUION_TABLET) },
381 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
382 USB_DEVICE_ID_YIYNOVA_TABLET) },
383 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
384 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
385 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
386 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
387 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
Nikolai Kondrashov0c15efe2019-02-10 12:13:57 +0200388 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
389 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200390 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
391 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
392 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
Martijn van de Streek022fc532020-10-16 08:38:05 +0200393 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
394 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200395 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
Nikolai Kondrashove902ed92019-02-10 12:14:05 +0200396 USB_DEVICE_ID_UGEE_TABLET_G5) },
397 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200398 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
Nikolai Kondrashovc3e5a672019-02-10 12:14:00 +0200399 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
Wang Xuerui88bb3462019-06-08 17:23:24 +0800400 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
401 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
Nikolai Kondrashovc3e5a672019-02-10 12:14:00 +0200402 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
Nikolai Kondrashov492a9e92019-02-10 12:14:01 +0200403 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
404 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
Nikolai Kondrashov08367be172019-02-10 12:14:02 +0200405 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
406 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200407 { }
408};
409MODULE_DEVICE_TABLE(hid, uclogic_devices);
410
411static struct hid_driver uclogic_driver = {
412 .name = "uclogic",
413 .id_table = uclogic_devices,
414 .probe = uclogic_probe,
Nikolai Kondrashov96142192019-02-10 12:13:51 +0200415 .remove = uclogic_remove,
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200416 .report_fixup = uclogic_report_fixup,
417 .raw_event = uclogic_raw_event,
418 .input_mapping = uclogic_input_mapping,
419 .input_configured = uclogic_input_configured,
Nikolai Kondrashov251b4272019-02-10 12:13:52 +0200420#ifdef CONFIG_PM
421 .resume = uclogic_resume,
422 .reset_resume = uclogic_resume,
423#endif
Nikolai Kondrashovff0c13d2019-02-10 12:13:50 +0200424};
425module_hid_driver(uclogic_driver);
426
427MODULE_AUTHOR("Martin Rusko");
428MODULE_AUTHOR("Nikolai Kondrashov");
429MODULE_LICENSE("GPL");