Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// 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>
19#include <linux/timer.h>
20#include "usbhid/usbhid.h"
21#include "hid-uclogic-params.h"
22
23#include "hid-ids.h"
24
25/* Driver data */
26struct uclogic_drvdata {
27 /* 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;
36 /* Pen input device */
37 struct input_dev *pen_input;
38 /* In-range timer */
39 struct timer_list inrange_timer;
40 /* Last rotary encoder state, or U8_MAX for none */
41 u8 re_state;
42};
43
44/**
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
72static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
73 unsigned int *rsize)
74{
75 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
76
77 if (drvdata->desc_ptr != NULL) {
78 rdesc = drvdata->desc_ptr;
79 *rsize = drvdata->desc_size;
80 }
81 return rdesc;
82}
83
84static 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)
90{
91 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
92 struct uclogic_params *params = &drvdata->params;
93
94 /* Discard invalid pen usages */
95 if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
96 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{
105 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
106 struct uclogic_params *params = &drvdata->params;
107 char *name;
108 const char *suffix = NULL;
109 struct hid_field *field;
110 size_t len;
111 size_t i;
112 const struct uclogic_params_frame *frame;
113
114 /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
115 if (!hi->report)
116 return 0;
117
118 /*
119 * If this is the input corresponding to the pen report
120 * in need of tweaking.
121 */
122 if (hi->report->id == params->pen.id) {
123 /* Remember the input device so we can simulate events */
124 drvdata->pen_input = hi->input;
125 }
126
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;
133 /*
134 * Disable EV_MSC reports for touch ring interfaces to
135 * make the Wacom driver pickup touch ring extents
136 */
137 if (frame->touch_byte > 0)
138 __clear_bit(EV_MSC, hi->input->evbit);
139 }
140 }
141
142 if (!suffix) {
143 field = hi->report->field[0];
144
145 switch (field->application) {
146 case HID_GD_KEYBOARD:
147 suffix = "Keyboard";
148 break;
149 case HID_GD_MOUSE:
150 suffix = "Mouse";
151 break;
152 case HID_GD_KEYPAD:
153 suffix = "Pad";
154 break;
155 case HID_DG_PEN:
156 case HID_DG_DIGITIZER:
157 suffix = "Pen";
158 break;
159 case HID_CP_CONSUMER_CONTROL:
160 suffix = "Consumer Control";
161 break;
162 case HID_GD_SYSTEM_CONTROL:
163 suffix = "System Control";
164 break;
165 }
166 }
167
168 if (suffix) {
169 len = strlen(hdev->name) + 2 + strlen(suffix);
170 name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
171 if (name) {
172 snprintf(name, len, "%s %s", hdev->name, suffix);
173 hi->input->name = name;
174 }
175 }
176
177 return 0;
178}
179
180static int uclogic_probe(struct hid_device *hdev,
181 const struct hid_device_id *id)
182{
183 int rc;
184 struct uclogic_drvdata *drvdata = NULL;
185 bool params_initialized = false;
186
187 if (!hid_is_usb(hdev))
188 return -EINVAL;
189
190 /*
191 * libinput requires the pad interface to be on a different node
192 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
193 */
194 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
195
196 /* Allocate and assign driver data */
197 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
198 if (drvdata == NULL) {
199 rc = -ENOMEM;
200 goto failure;
201 }
202 timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
203 drvdata->re_state = U8_MAX;
204 hid_set_drvdata(hdev, drvdata);
205
206 /* Initialize the device and retrieve interface parameters */
207 rc = uclogic_params_init(&drvdata->params, hdev);
208 if (rc != 0) {
209 hid_err(hdev, "failed probing parameters: %d\n", rc);
210 goto failure;
211 }
212 params_initialized = true;
213 hid_dbg(hdev, "parameters:\n");
214 uclogic_params_hid_dbg(hdev, &drvdata->params);
215 if (drvdata->params.invalid) {
216 hid_info(hdev, "interface is invalid, ignoring\n");
217 rc = -ENODEV;
218 goto failure;
219 }
220
221 /* Generate replacement report descriptor */
222 rc = uclogic_params_get_desc(&drvdata->params,
223 &drvdata->desc_ptr,
224 &drvdata->desc_size);
225 if (rc) {
226 hid_err(hdev,
227 "failed generating replacement report descriptor: %d\n",
228 rc);
229 goto failure;
230 }
231
232 rc = hid_parse(hdev);
233 if (rc) {
234 hid_err(hdev, "parse failed\n");
235 goto failure;
236 }
237
238 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
239 if (rc) {
240 hid_err(hdev, "hw start failed\n");
241 goto failure;
242 }
243
244 return 0;
245failure:
246 /* Assume "remove" might not be called if "probe" failed */
247 if (params_initialized)
248 uclogic_params_cleanup(&drvdata->params);
249 return rc;
250}
251
252#ifdef CONFIG_PM
253static int uclogic_resume(struct hid_device *hdev)
254{
255 int rc;
256 struct uclogic_params params;
257
258 /* Re-initialize the device, but discard parameters */
259 rc = uclogic_params_init(¶ms, hdev);
260 if (rc != 0)
261 hid_err(hdev, "failed to re-initialize the device\n");
262 else
263 uclogic_params_cleanup(¶ms);
264
265 return rc;
266}
267#endif
268
269/**
270 * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
271 *
272 * @drvdata: Driver data.
273 * @data: Report data buffer, can be modified.
274 * @size: Report data size, bytes.
275 *
276 * Returns:
277 * Negative value on error (stops event delivery), zero for success.
278 */
279static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
280 u8 *data, int size)
281{
282 struct uclogic_params_pen *pen = &drvdata->params.pen;
283
284 WARN_ON(drvdata == NULL);
285 WARN_ON(data == NULL && size != 0);
286
287 /* If in-range reports are inverted */
288 if (pen->inrange ==
289 UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
290 /* Invert the in-range bit */
291 data[1] ^= 0x40;
292 }
293 /*
294 * If report contains fragmented high-resolution pen
295 * coordinates
296 */
297 if (size >= 10 && pen->fragmented_hires) {
298 u8 pressure_low_byte;
299 u8 pressure_high_byte;
300
301 /* Lift pressure bytes */
302 pressure_low_byte = data[6];
303 pressure_high_byte = data[7];
304 /*
305 * Move Y coord to make space for high-order X
306 * coord byte
307 */
308 data[6] = data[5];
309 data[5] = data[4];
310 /* Move high-order X coord byte */
311 data[4] = data[8];
312 /* Move high-order Y coord byte */
313 data[7] = data[9];
314 /* Place pressure bytes */
315 data[8] = pressure_low_byte;
316 data[9] = pressure_high_byte;
317 }
318 /* If we need to emulate in-range detection */
319 if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
320 /* Set in-range bit */
321 data[1] |= 0x40;
322 /* (Re-)start in-range timeout */
323 mod_timer(&drvdata->inrange_timer,
324 jiffies + msecs_to_jiffies(100));
325 }
326 /* If we report tilt and Y direction is flipped */
327 if (size >= 12 && pen->tilt_y_flipped)
328 data[11] = -data[11];
329
330 return 0;
331}
332
333/**
334 * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
335 *
336 * @drvdata: Driver data.
337 * @frame: The parameters of the frame controls to handle.
338 * @data: Report data buffer, can be modified.
339 * @size: Report data size, bytes.
340 *
341 * Returns:
342 * Negative value on error (stops event delivery), zero for success.
343 */
344static int uclogic_raw_event_frame(
345 struct uclogic_drvdata *drvdata,
346 const struct uclogic_params_frame *frame,
347 u8 *data, int size)
348{
349 WARN_ON(drvdata == NULL);
350 WARN_ON(data == NULL && size != 0);
351
352 /* If need to, and can, set pad device ID for Wacom drivers */
353 if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
354 /* If we also have a touch ring and the finger left it */
355 if (frame->touch_byte > 0 && frame->touch_byte < size &&
356 data[frame->touch_byte] == 0) {
357 data[frame->dev_id_byte] = 0;
358 } else {
359 data[frame->dev_id_byte] = 0xf;
360 }
361 }
362
363 /* If need to, and can, read rotary encoder state change */
364 if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
365 unsigned int byte = frame->re_lsb / 8;
366 unsigned int bit = frame->re_lsb % 8;
367
368 u8 change;
369 u8 prev_state = drvdata->re_state;
370 /* Read Gray-coded state */
371 u8 state = (data[byte] >> bit) & 0x3;
372 /* Encode state change into 2-bit signed integer */
373 if ((prev_state == 1 && state == 0) ||
374 (prev_state == 2 && state == 3)) {
375 change = 1;
376 } else if ((prev_state == 2 && state == 0) ||
377 (prev_state == 1 && state == 3)) {
378 change = 3;
379 } else {
380 change = 0;
381 }
382 /* Write change */
383 data[byte] = (data[byte] & ~((u8)3 << bit)) |
384 (change << bit);
385 /* Remember state */
386 drvdata->re_state = state;
387 }
388
389 /* If need to, and can, transform the touch ring reports */
390 if (frame->touch_byte > 0 && frame->touch_byte < size) {
391 __s8 value = data[frame->touch_byte];
392
393 if (value != 0) {
394 if (frame->touch_flip_at != 0) {
395 value = frame->touch_flip_at - value;
396 if (value <= 0)
397 value = frame->touch_max + value;
398 }
399 data[frame->touch_byte] = value - 1;
400 }
401 }
402
403 /* If need to, and can, transform the bitmap dial reports */
404 if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
405 if (data[frame->bitmap_dial_byte] == 2)
406 data[frame->bitmap_dial_byte] = -1;
407 }
408
409 return 0;
410}
411
412static int uclogic_raw_event(struct hid_device *hdev,
413 struct hid_report *report,
414 u8 *data, int size)
415{
416 unsigned int report_id = report->id;
417 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
418 struct uclogic_params *params = &drvdata->params;
419 struct uclogic_params_pen_subreport *subreport;
420 struct uclogic_params_pen_subreport *subreport_list_end;
421 size_t i;
422
423 /* Do not handle anything but input reports */
424 if (report->type != HID_INPUT_REPORT)
425 return 0;
426
427 while (true) {
428 /* Tweak pen reports, if necessary */
429 if ((report_id == params->pen.id) && (size >= 2)) {
430 subreport_list_end =
431 params->pen.subreport_list +
432 ARRAY_SIZE(params->pen.subreport_list);
433 /* Try to match a subreport */
434 for (subreport = params->pen.subreport_list;
435 subreport < subreport_list_end; subreport++) {
436 if (subreport->value != 0 &&
437 subreport->value == data[1]) {
438 break;
439 }
440 }
441 /* If a subreport matched */
442 if (subreport < subreport_list_end) {
443 /* Change to subreport ID, and restart */
444 report_id = data[0] = subreport->id;
445 continue;
446 } else {
447 return uclogic_raw_event_pen(drvdata, data, size);
448 }
449 }
450
451 /* Tweak frame control reports, if necessary */
452 for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
453 if (report_id == params->frame_list[i].id) {
454 return uclogic_raw_event_frame(
455 drvdata, ¶ms->frame_list[i],
456 data, size);
457 }
458 }
459
460 break;
461 }
462
463 return 0;
464}
465
466static void uclogic_remove(struct hid_device *hdev)
467{
468 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
469
470 del_timer_sync(&drvdata->inrange_timer);
471 hid_hw_stop(hdev);
472 kfree(drvdata->desc_ptr);
473 uclogic_params_cleanup(&drvdata->params);
474}
475
476static const struct hid_device_id uclogic_devices[] = {
477 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
478 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
479 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
480 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
481 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
482 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
483 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
484 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
485 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
486 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
487 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
488 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
489 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
490 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
491 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
492 USB_DEVICE_ID_HUION_TABLET) },
493 { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
494 USB_DEVICE_ID_HUION_TABLET2) },
495 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
496 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
497 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
498 USB_DEVICE_ID_HUION_TABLET) },
499 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
500 USB_DEVICE_ID_YIYNOVA_TABLET) },
501 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
502 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
503 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
504 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
505 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
506 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
507 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
508 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
509 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
510 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
511 { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
512 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
513 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
514 USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
515 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
516 USB_DEVICE_ID_UGEE_TABLET_G5) },
517 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
518 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
519 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
520 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
521 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
522 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
523 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
524 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
525 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
526 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
527 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
528 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
529 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
530 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
531 { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
532 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
533 { }
534};
535MODULE_DEVICE_TABLE(hid, uclogic_devices);
536
537static struct hid_driver uclogic_driver = {
538 .name = "uclogic",
539 .id_table = uclogic_devices,
540 .probe = uclogic_probe,
541 .remove = uclogic_remove,
542 .report_fixup = uclogic_report_fixup,
543 .raw_event = uclogic_raw_event,
544 .input_mapping = uclogic_input_mapping,
545 .input_configured = uclogic_input_configured,
546#ifdef CONFIG_PM
547 .resume = uclogic_resume,
548 .reset_resume = uclogic_resume,
549#endif
550};
551module_hid_driver(uclogic_driver);
552
553MODULE_AUTHOR("Martin Rusko");
554MODULE_AUTHOR("Nikolai Kondrashov");
555MODULE_LICENSE("GPL");