GNU Linux-libre 5.10.219-gnu1
[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 /* Driver data */
26 struct 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  */
52 static 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
72 static __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
84 static 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 the unused pen interface */
95         if (params->pen_unused && (field->application == HID_DG_PEN))
96                 return -1;
97
98         /* let hid-core decide what to do */
99         return 0;
100 }
101
102 static 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
112         /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
113         if (!hi->report)
114                 return 0;
115
116         /*
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
125         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
160 static int uclogic_probe(struct hid_device *hdev,
161                 const struct hid_device_id *id)
162 {
163         int rc;
164         struct uclogic_drvdata *drvdata = NULL;
165         bool params_initialized = false;
166
167         if (!hid_is_usb(hdev))
168                 return -EINVAL;
169
170         /*
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         hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
176
177         /* Allocate and assign driver data */
178         drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
179         if (drvdata == NULL) {
180                 rc = -ENOMEM;
181                 goto failure;
182         }
183         timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
184         drvdata->re_state = U8_MAX;
185         hid_set_drvdata(hdev, drvdata);
186
187         /* Initialize the device and retrieve interface parameters */
188         rc = uclogic_params_init(&drvdata->params, hdev);
189         if (rc != 0) {
190                 hid_err(hdev, "failed probing parameters: %d\n", rc);
191                 goto failure;
192         }
193         params_initialized = true;
194         hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
195                 UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
196         if (drvdata->params.invalid) {
197                 hid_info(hdev, "interface is invalid, ignoring\n");
198                 rc = -ENODEV;
199                 goto failure;
200         }
201
202         /* Generate replacement report descriptor */
203         rc = uclogic_params_get_desc(&drvdata->params,
204                                      &drvdata->desc_ptr,
205                                      &drvdata->desc_size);
206         if (rc) {
207                 hid_err(hdev,
208                         "failed generating replacement report descriptor: %d\n",
209                         rc);
210                 goto failure;
211         }
212
213         rc = hid_parse(hdev);
214         if (rc) {
215                 hid_err(hdev, "parse failed\n");
216                 goto failure;
217         }
218
219         rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
220         if (rc) {
221                 hid_err(hdev, "hw start failed\n");
222                 goto failure;
223         }
224
225         return 0;
226 failure:
227         /* Assume "remove" might not be called if "probe" failed */
228         if (params_initialized)
229                 uclogic_params_cleanup(&drvdata->params);
230         return rc;
231 }
232
233 #ifdef CONFIG_PM
234 static int uclogic_resume(struct hid_device *hdev)
235 {
236         int rc;
237         struct uclogic_params params;
238
239         /* Re-initialize the device, but discard parameters */
240         rc = uclogic_params_init(&params, hdev);
241         if (rc != 0)
242                 hid_err(hdev, "failed to re-initialize the device\n");
243         else
244                 uclogic_params_cleanup(&params);
245
246         return rc;
247 }
248 #endif
249
250 static int uclogic_raw_event(struct hid_device *hdev,
251                                 struct hid_report *report,
252                                 u8 *data, int size)
253 {
254         struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
255         struct uclogic_params *params = &drvdata->params;
256
257         /* Tweak pen reports, if necessary */
258         if (!params->pen_unused &&
259             (report->type == HID_INPUT_REPORT) &&
260             (report->id == params->pen.id) &&
261             (size >= 2)) {
262                 /* If it's the "virtual" frame controls report */
263                 if (params->frame.id != 0 &&
264                     data[1] & params->pen_frame_flag) {
265                         /* Change to virtual frame controls report ID */
266                         data[0] = params->frame.id;
267                         return 0;
268                 }
269                 /* If in-range reports are inverted */
270                 if (params->pen.inrange ==
271                         UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
272                         /* Invert the in-range bit */
273                         data[1] ^= 0x40;
274                 }
275                 /*
276                  * If report contains fragmented high-resolution pen
277                  * coordinates
278                  */
279                 if (size >= 10 && params->pen.fragmented_hires) {
280                         u8 pressure_low_byte;
281                         u8 pressure_high_byte;
282
283                         /* Lift pressure bytes */
284                         pressure_low_byte = data[6];
285                         pressure_high_byte = data[7];
286                         /*
287                          * Move Y coord to make space for high-order X
288                          * coord byte
289                          */
290                         data[6] = data[5];
291                         data[5] = data[4];
292                         /* Move high-order X coord byte */
293                         data[4] = data[8];
294                         /* Move high-order Y coord byte */
295                         data[7] = data[9];
296                         /* Place pressure bytes */
297                         data[8] = pressure_low_byte;
298                         data[9] = pressure_high_byte;
299                 }
300                 /* If we need to emulate in-range detection */
301                 if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
302                         /* Set in-range bit */
303                         data[1] |= 0x40;
304                         /* (Re-)start in-range timeout */
305                         mod_timer(&drvdata->inrange_timer,
306                                         jiffies + msecs_to_jiffies(100));
307                 }
308         }
309
310         /* Tweak frame control reports, if necessary */
311         if ((report->type == HID_INPUT_REPORT) &&
312             (report->id == params->frame.id)) {
313                 /* If need to, and can, set pad device ID for Wacom drivers */
314                 if (params->frame.dev_id_byte > 0 &&
315                     params->frame.dev_id_byte < size) {
316                         data[params->frame.dev_id_byte] = 0xf;
317                 }
318                 /* If need to, and can, read rotary encoder state change */
319                 if (params->frame.re_lsb > 0 &&
320                     params->frame.re_lsb / 8 < size) {
321                         unsigned int byte = params->frame.re_lsb / 8;
322                         unsigned int bit = params->frame.re_lsb % 8;
323
324                         u8 change;
325                         u8 prev_state = drvdata->re_state;
326                         /* Read Gray-coded state */
327                         u8 state = (data[byte] >> bit) & 0x3;
328                         /* Encode state change into 2-bit signed integer */
329                         if ((prev_state == 1 && state == 0) ||
330                             (prev_state == 2 && state == 3)) {
331                                 change = 1;
332                         } else if ((prev_state == 2 && state == 0) ||
333                                    (prev_state == 1 && state == 3)) {
334                                 change = 3;
335                         } else {
336                                 change = 0;
337                         }
338                         /* Write change */
339                         data[byte] = (data[byte] & ~((u8)3 << bit)) |
340                                         (change << bit);
341                         /* Remember state */
342                         drvdata->re_state = state;
343                 }
344         }
345
346         return 0;
347 }
348
349 static void uclogic_remove(struct hid_device *hdev)
350 {
351         struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
352
353         del_timer_sync(&drvdata->inrange_timer);
354         hid_hw_stop(hdev);
355         kfree(drvdata->desc_ptr);
356         uclogic_params_cleanup(&drvdata->params);
357 }
358
359 static const struct hid_device_id uclogic_devices[] = {
360         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
361                                 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
362         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
363                                 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
364         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
365                                 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
366         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
367                                 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
368         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
369                                 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
370         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
371                                 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
372         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
373                                 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
374         { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
375                                 USB_DEVICE_ID_HUION_TABLET) },
376         { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
377                                 USB_DEVICE_ID_HUION_HS64) },
378         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
379                                 USB_DEVICE_ID_HUION_TABLET) },
380         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
381                                 USB_DEVICE_ID_YIYNOVA_TABLET) },
382         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
383                                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
384         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
385                                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
386         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
387                                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
388         { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
389                                 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
390         { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
391                                 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
392         { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
393                                 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
394         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
395                                 USB_DEVICE_ID_UGEE_TABLET_G5) },
396         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
397                                 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
398         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
399                                 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
400         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
401                                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
402         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
403                                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
404         { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
405                                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
406         { }
407 };
408 MODULE_DEVICE_TABLE(hid, uclogic_devices);
409
410 static struct hid_driver uclogic_driver = {
411         .name = "uclogic",
412         .id_table = uclogic_devices,
413         .probe = uclogic_probe,
414         .remove = uclogic_remove,
415         .report_fixup = uclogic_report_fixup,
416         .raw_event = uclogic_raw_event,
417         .input_mapping = uclogic_input_mapping,
418         .input_configured = uclogic_input_configured,
419 #ifdef CONFIG_PM
420         .resume           = uclogic_resume,
421         .reset_resume     = uclogic_resume,
422 #endif
423 };
424 module_hid_driver(uclogic_driver);
425
426 MODULE_AUTHOR("Martin Rusko");
427 MODULE_AUTHOR("Nikolai Kondrashov");
428 MODULE_LICENSE("GPL");