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