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