1ff0c13d6SNikolai Kondrashov // SPDX-License-Identifier: GPL-2.0+
2ff0c13d6SNikolai Kondrashov /*
3ff0c13d6SNikolai Kondrashov * HID driver for UC-Logic devices not fully compliant with HID standard
4ff0c13d6SNikolai Kondrashov *
5ff0c13d6SNikolai Kondrashov * Copyright (c) 2010-2014 Nikolai Kondrashov
6ff0c13d6SNikolai Kondrashov * Copyright (c) 2013 Martin Rusko
7ff0c13d6SNikolai Kondrashov */
8ff0c13d6SNikolai Kondrashov
9ff0c13d6SNikolai Kondrashov /*
10ff0c13d6SNikolai Kondrashov * This program is free software; you can redistribute it and/or modify it
11ff0c13d6SNikolai Kondrashov * under the terms of the GNU General Public License as published by the Free
12ff0c13d6SNikolai Kondrashov * Software Foundation; either version 2 of the License, or (at your option)
13ff0c13d6SNikolai Kondrashov * any later version.
14ff0c13d6SNikolai Kondrashov */
15ff0c13d6SNikolai Kondrashov
16ff0c13d6SNikolai Kondrashov #include <linux/device.h>
17ff0c13d6SNikolai Kondrashov #include <linux/hid.h>
18ff0c13d6SNikolai Kondrashov #include <linux/module.h>
1901309e29SNikolai Kondrashov #include <linux/timer.h>
20ff0c13d6SNikolai Kondrashov #include "usbhid/usbhid.h"
219614219eSNikolai Kondrashov #include "hid-uclogic-params.h"
22ff0c13d6SNikolai Kondrashov
23ff0c13d6SNikolai Kondrashov #include "hid-ids.h"
24ff0c13d6SNikolai Kondrashov
2501309e29SNikolai Kondrashov /**
2601309e29SNikolai Kondrashov * uclogic_inrange_timeout - handle pen in-range state timeout.
2701309e29SNikolai Kondrashov * Emulate input events normally generated when pen goes out of range for
2801309e29SNikolai Kondrashov * tablets which don't report that.
2901309e29SNikolai Kondrashov *
3001309e29SNikolai Kondrashov * @t: The timer the timeout handler is attached to, stored in a struct
3101309e29SNikolai Kondrashov * uclogic_drvdata.
3201309e29SNikolai Kondrashov */
uclogic_inrange_timeout(struct timer_list * t)3301309e29SNikolai Kondrashov static void uclogic_inrange_timeout(struct timer_list *t)
3401309e29SNikolai Kondrashov {
3501309e29SNikolai Kondrashov struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
3601309e29SNikolai Kondrashov inrange_timer);
3701309e29SNikolai Kondrashov struct input_dev *input = drvdata->pen_input;
3801309e29SNikolai Kondrashov
3901309e29SNikolai Kondrashov if (input == NULL)
4001309e29SNikolai Kondrashov return;
4101309e29SNikolai Kondrashov input_report_abs(input, ABS_PRESSURE, 0);
4201309e29SNikolai Kondrashov /* If BTN_TOUCH state is changing */
4301309e29SNikolai Kondrashov if (test_bit(BTN_TOUCH, input->key)) {
4401309e29SNikolai Kondrashov input_event(input, EV_MSC, MSC_SCAN,
4501309e29SNikolai Kondrashov /* Digitizer Tip Switch usage */
4601309e29SNikolai Kondrashov 0xd0042);
4701309e29SNikolai Kondrashov input_report_key(input, BTN_TOUCH, 0);
4801309e29SNikolai Kondrashov }
4901309e29SNikolai Kondrashov input_report_key(input, BTN_TOOL_PEN, 0);
5001309e29SNikolai Kondrashov input_sync(input);
5101309e29SNikolai Kondrashov }
5201309e29SNikolai Kondrashov
uclogic_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)53fe73965dSThomas Weißschuh static const __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
54ff0c13d6SNikolai Kondrashov unsigned int *rsize)
55ff0c13d6SNikolai Kondrashov {
56ff0c13d6SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
57ff0c13d6SNikolai Kondrashov
589614219eSNikolai Kondrashov if (drvdata->desc_ptr != NULL) {
599614219eSNikolai Kondrashov *rsize = drvdata->desc_size;
60*03f8dc1dSThomas Weißschuh return drvdata->desc_ptr;
619614219eSNikolai Kondrashov }
62ff0c13d6SNikolai Kondrashov return rdesc;
63ff0c13d6SNikolai Kondrashov }
64ff0c13d6SNikolai Kondrashov
uclogic_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)65f5927973SNikolai Kondrashov static int uclogic_input_mapping(struct hid_device *hdev,
66f5927973SNikolai Kondrashov struct hid_input *hi,
67f5927973SNikolai Kondrashov struct hid_field *field,
68f5927973SNikolai Kondrashov struct hid_usage *usage,
69f5927973SNikolai Kondrashov unsigned long **bit,
70f5927973SNikolai Kondrashov int *max)
71f5927973SNikolai Kondrashov {
72f5927973SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
73f5927973SNikolai Kondrashov struct uclogic_params *params = &drvdata->params;
74f5927973SNikolai Kondrashov
75f5927973SNikolai Kondrashov /* Discard invalid pen usages */
76f5927973SNikolai Kondrashov if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
77f5927973SNikolai Kondrashov return -1;
78f5927973SNikolai Kondrashov
79f5927973SNikolai Kondrashov /* Let hid-core decide what to do */
80f5927973SNikolai Kondrashov return 0;
81f5927973SNikolai Kondrashov }
82f5927973SNikolai Kondrashov
uclogic_input_configured(struct hid_device * hdev,struct hid_input * hi)83ff0c13d6SNikolai Kondrashov static int uclogic_input_configured(struct hid_device *hdev,
84ff0c13d6SNikolai Kondrashov struct hid_input *hi)
85ff0c13d6SNikolai Kondrashov {
8601309e29SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
8701309e29SNikolai Kondrashov struct uclogic_params *params = &drvdata->params;
88ff0c13d6SNikolai Kondrashov const char *suffix = NULL;
89ff0c13d6SNikolai Kondrashov struct hid_field *field;
90d170e8e0SNikolai Kondrashov size_t i;
91d170e8e0SNikolai Kondrashov const struct uclogic_params_frame *frame;
92ff0c13d6SNikolai Kondrashov
93ff0c13d6SNikolai Kondrashov /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
94ff0c13d6SNikolai Kondrashov if (!hi->report)
95ff0c13d6SNikolai Kondrashov return 0;
96ff0c13d6SNikolai Kondrashov
9701309e29SNikolai Kondrashov /*
9801309e29SNikolai Kondrashov * If this is the input corresponding to the pen report
9901309e29SNikolai Kondrashov * in need of tweaking.
10001309e29SNikolai Kondrashov */
10101309e29SNikolai Kondrashov if (hi->report->id == params->pen.id) {
10201309e29SNikolai Kondrashov /* Remember the input device so we can simulate events */
10301309e29SNikolai Kondrashov drvdata->pen_input = hi->input;
10401309e29SNikolai Kondrashov }
10501309e29SNikolai Kondrashov
106d170e8e0SNikolai Kondrashov /* If it's one of the frame devices */
107d170e8e0SNikolai Kondrashov for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
108d170e8e0SNikolai Kondrashov frame = ¶ms->frame_list[i];
109d170e8e0SNikolai Kondrashov if (hi->report->id == frame->id) {
110ee007036SNikolai Kondrashov /* Assign custom suffix, if any */
111ee007036SNikolai Kondrashov suffix = frame->suffix;
112d170e8e0SNikolai Kondrashov /*
113d170e8e0SNikolai Kondrashov * Disable EV_MSC reports for touch ring interfaces to
114d170e8e0SNikolai Kondrashov * make the Wacom driver pickup touch ring extents
115d170e8e0SNikolai Kondrashov */
116caf7e934SNikolai Kondrashov if (frame->touch_byte > 0)
117d170e8e0SNikolai Kondrashov __clear_bit(EV_MSC, hi->input->evbit);
118d170e8e0SNikolai Kondrashov }
119d170e8e0SNikolai Kondrashov }
120d170e8e0SNikolai Kondrashov
121ee007036SNikolai Kondrashov if (!suffix) {
122ff0c13d6SNikolai Kondrashov field = hi->report->field[0];
123ff0c13d6SNikolai Kondrashov
124ff0c13d6SNikolai Kondrashov switch (field->application) {
125ff0c13d6SNikolai Kondrashov case HID_GD_KEYBOARD:
126ff0c13d6SNikolai Kondrashov suffix = "Keyboard";
127ff0c13d6SNikolai Kondrashov break;
128ff0c13d6SNikolai Kondrashov case HID_GD_MOUSE:
129ff0c13d6SNikolai Kondrashov suffix = "Mouse";
130ff0c13d6SNikolai Kondrashov break;
131ff0c13d6SNikolai Kondrashov case HID_GD_KEYPAD:
132ff0c13d6SNikolai Kondrashov suffix = "Pad";
133ff0c13d6SNikolai Kondrashov break;
134ff0c13d6SNikolai Kondrashov case HID_DG_PEN:
1350977fda0SJosé Expósito case HID_DG_DIGITIZER:
136ff0c13d6SNikolai Kondrashov suffix = "Pen";
137ff0c13d6SNikolai Kondrashov break;
138ff0c13d6SNikolai Kondrashov case HID_CP_CONSUMER_CONTROL:
139ff0c13d6SNikolai Kondrashov suffix = "Consumer Control";
140ff0c13d6SNikolai Kondrashov break;
141ff0c13d6SNikolai Kondrashov case HID_GD_SYSTEM_CONTROL:
142ff0c13d6SNikolai Kondrashov suffix = "System Control";
143ff0c13d6SNikolai Kondrashov break;
144ff0c13d6SNikolai Kondrashov }
145ee007036SNikolai Kondrashov }
146ff0c13d6SNikolai Kondrashov
147dd613a4eSRahul Rameshbabu if (suffix)
148dd613a4eSRahul Rameshbabu hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
149dd613a4eSRahul Rameshbabu "%s %s", hdev->name, suffix);
150ff0c13d6SNikolai Kondrashov
151ff0c13d6SNikolai Kondrashov return 0;
152ff0c13d6SNikolai Kondrashov }
153ff0c13d6SNikolai Kondrashov
uclogic_probe(struct hid_device * hdev,const struct hid_device_id * id)154ff0c13d6SNikolai Kondrashov static int uclogic_probe(struct hid_device *hdev,
155ff0c13d6SNikolai Kondrashov const struct hid_device_id *id)
156ff0c13d6SNikolai Kondrashov {
157ff0c13d6SNikolai Kondrashov int rc;
1589614219eSNikolai Kondrashov struct uclogic_drvdata *drvdata = NULL;
1599614219eSNikolai Kondrashov bool params_initialized = false;
160ff0c13d6SNikolai Kondrashov
16193020953SGreg Kroah-Hartman if (!hid_is_usb(hdev))
16293020953SGreg Kroah-Hartman return -EINVAL;
16393020953SGreg Kroah-Hartman
164ff0c13d6SNikolai Kondrashov /*
165ff0c13d6SNikolai Kondrashov * libinput requires the pad interface to be on a different node
166ff0c13d6SNikolai Kondrashov * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
167ff0c13d6SNikolai Kondrashov */
168ff0c13d6SNikolai Kondrashov hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1693405a4beSJosé Expósito hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
170ff0c13d6SNikolai Kondrashov
171ff0c13d6SNikolai Kondrashov /* Allocate and assign driver data */
172ff0c13d6SNikolai Kondrashov drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1739614219eSNikolai Kondrashov if (drvdata == NULL) {
1749614219eSNikolai Kondrashov rc = -ENOMEM;
1759614219eSNikolai Kondrashov goto failure;
1769614219eSNikolai Kondrashov }
17701309e29SNikolai Kondrashov timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
1788a47670cSNikolai Kondrashov drvdata->re_state = U8_MAX;
17914b71e6aSJosé Expósito drvdata->quirks = id->driver_data;
180ff0c13d6SNikolai Kondrashov hid_set_drvdata(hdev, drvdata);
181ff0c13d6SNikolai Kondrashov
1829614219eSNikolai Kondrashov /* Initialize the device and retrieve interface parameters */
1839614219eSNikolai Kondrashov rc = uclogic_params_init(&drvdata->params, hdev);
1849614219eSNikolai Kondrashov if (rc != 0) {
1859614219eSNikolai Kondrashov hid_err(hdev, "failed probing parameters: %d\n", rc);
1869614219eSNikolai Kondrashov goto failure;
187ff0c13d6SNikolai Kondrashov }
1889614219eSNikolai Kondrashov params_initialized = true;
189a228809fSNikolai Kondrashov hid_dbg(hdev, "parameters:\n");
190a228809fSNikolai Kondrashov uclogic_params_hid_dbg(hdev, &drvdata->params);
1919614219eSNikolai Kondrashov if (drvdata->params.invalid) {
1929614219eSNikolai Kondrashov hid_info(hdev, "interface is invalid, ignoring\n");
1939614219eSNikolai Kondrashov rc = -ENODEV;
1949614219eSNikolai Kondrashov goto failure;
1959614219eSNikolai Kondrashov }
196ff0c13d6SNikolai Kondrashov
1979614219eSNikolai Kondrashov /* Generate replacement report descriptor */
1989614219eSNikolai Kondrashov rc = uclogic_params_get_desc(&drvdata->params,
1999614219eSNikolai Kondrashov &drvdata->desc_ptr,
2009614219eSNikolai Kondrashov &drvdata->desc_size);
201ff0c13d6SNikolai Kondrashov if (rc) {
2029614219eSNikolai Kondrashov hid_err(hdev,
2039614219eSNikolai Kondrashov "failed generating replacement report descriptor: %d\n",
2049614219eSNikolai Kondrashov rc);
2059614219eSNikolai Kondrashov goto failure;
206ff0c13d6SNikolai Kondrashov }
207ff0c13d6SNikolai Kondrashov
208ff0c13d6SNikolai Kondrashov rc = hid_parse(hdev);
209ff0c13d6SNikolai Kondrashov if (rc) {
210ff0c13d6SNikolai Kondrashov hid_err(hdev, "parse failed\n");
2119614219eSNikolai Kondrashov goto failure;
212ff0c13d6SNikolai Kondrashov }
213ff0c13d6SNikolai Kondrashov
214ff0c13d6SNikolai Kondrashov rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
215ff0c13d6SNikolai Kondrashov if (rc) {
216ff0c13d6SNikolai Kondrashov hid_err(hdev, "hw start failed\n");
2179614219eSNikolai Kondrashov goto failure;
2189614219eSNikolai Kondrashov }
2199614219eSNikolai Kondrashov
2209614219eSNikolai Kondrashov return 0;
2219614219eSNikolai Kondrashov failure:
2229614219eSNikolai Kondrashov /* Assume "remove" might not be called if "probe" failed */
2239614219eSNikolai Kondrashov if (params_initialized)
2249614219eSNikolai Kondrashov uclogic_params_cleanup(&drvdata->params);
225ff0c13d6SNikolai Kondrashov return rc;
226ff0c13d6SNikolai Kondrashov }
227ff0c13d6SNikolai Kondrashov
228251b4275SNikolai Kondrashov #ifdef CONFIG_PM
uclogic_resume(struct hid_device * hdev)229251b4275SNikolai Kondrashov static int uclogic_resume(struct hid_device *hdev)
230251b4275SNikolai Kondrashov {
231251b4275SNikolai Kondrashov int rc;
232251b4275SNikolai Kondrashov struct uclogic_params params;
233251b4275SNikolai Kondrashov
234251b4275SNikolai Kondrashov /* Re-initialize the device, but discard parameters */
235251b4275SNikolai Kondrashov rc = uclogic_params_init(¶ms, hdev);
236251b4275SNikolai Kondrashov if (rc != 0)
237251b4275SNikolai Kondrashov hid_err(hdev, "failed to re-initialize the device\n");
238251b4275SNikolai Kondrashov else
239251b4275SNikolai Kondrashov uclogic_params_cleanup(¶ms);
240251b4275SNikolai Kondrashov
241251b4275SNikolai Kondrashov return rc;
242251b4275SNikolai Kondrashov }
243251b4275SNikolai Kondrashov #endif
244251b4275SNikolai Kondrashov
2457e418667SNikolai Kondrashov /**
246a251d657SJosé Expósito * uclogic_exec_event_hook - if the received event is hooked schedules the
247a251d657SJosé Expósito * associated work.
248a251d657SJosé Expósito *
249a251d657SJosé Expósito * @p: Tablet interface report parameters.
250a251d657SJosé Expósito * @event: Raw event.
251a251d657SJosé Expósito * @size: The size of event.
252a251d657SJosé Expósito *
253a251d657SJosé Expósito * Returns:
254a251d657SJosé Expósito * Whether the event was hooked or not.
255a251d657SJosé Expósito */
uclogic_exec_event_hook(struct uclogic_params * p,u8 * event,int size)256a251d657SJosé Expósito static bool uclogic_exec_event_hook(struct uclogic_params *p, u8 *event, int size)
257a251d657SJosé Expósito {
258a251d657SJosé Expósito struct uclogic_raw_event_hook *curr;
259a251d657SJosé Expósito
260a251d657SJosé Expósito if (!p->event_hooks)
261a251d657SJosé Expósito return false;
262a251d657SJosé Expósito
263a251d657SJosé Expósito list_for_each_entry(curr, &p->event_hooks->list, list) {
264a251d657SJosé Expósito if (curr->size == size && memcmp(curr->event, event, size) == 0) {
265a251d657SJosé Expósito schedule_work(&curr->work);
266a251d657SJosé Expósito return true;
267a251d657SJosé Expósito }
268a251d657SJosé Expósito }
269a251d657SJosé Expósito
270a251d657SJosé Expósito return false;
271a251d657SJosé Expósito }
272a251d657SJosé Expósito
273a251d657SJosé Expósito /**
2747e418667SNikolai Kondrashov * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
2757e418667SNikolai Kondrashov *
2767e418667SNikolai Kondrashov * @drvdata: Driver data.
2777e418667SNikolai Kondrashov * @data: Report data buffer, can be modified.
2787e418667SNikolai Kondrashov * @size: Report data size, bytes.
2797e418667SNikolai Kondrashov *
2807e418667SNikolai Kondrashov * Returns:
2817e418667SNikolai Kondrashov * Negative value on error (stops event delivery), zero for success.
2827e418667SNikolai Kondrashov */
uclogic_raw_event_pen(struct uclogic_drvdata * drvdata,u8 * data,int size)2837e418667SNikolai Kondrashov static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
284ff0c13d6SNikolai Kondrashov u8 *data, int size)
285ff0c13d6SNikolai Kondrashov {
2867f12dd24SNikolai Kondrashov struct uclogic_params_pen *pen = &drvdata->params.pen;
287ff0c13d6SNikolai Kondrashov
2887e418667SNikolai Kondrashov WARN_ON(drvdata == NULL);
2897e418667SNikolai Kondrashov WARN_ON(data == NULL && size != 0);
2907e418667SNikolai Kondrashov
2919614219eSNikolai Kondrashov /* If in-range reports are inverted */
2927f12dd24SNikolai Kondrashov if (pen->inrange ==
2939614219eSNikolai Kondrashov UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
294ff0c13d6SNikolai Kondrashov /* Invert the in-range bit */
295ff0c13d6SNikolai Kondrashov data[1] ^= 0x40;
296ff0c13d6SNikolai Kondrashov }
29759f2e0fcSNikolai Kondrashov /*
29859f2e0fcSNikolai Kondrashov * If report contains fragmented high-resolution pen
29959f2e0fcSNikolai Kondrashov * coordinates
30059f2e0fcSNikolai Kondrashov */
3017f12dd24SNikolai Kondrashov if (size >= 10 && pen->fragmented_hires) {
30259f2e0fcSNikolai Kondrashov u8 pressure_low_byte;
30359f2e0fcSNikolai Kondrashov u8 pressure_high_byte;
30459f2e0fcSNikolai Kondrashov
30559f2e0fcSNikolai Kondrashov /* Lift pressure bytes */
30659f2e0fcSNikolai Kondrashov pressure_low_byte = data[6];
30759f2e0fcSNikolai Kondrashov pressure_high_byte = data[7];
30859f2e0fcSNikolai Kondrashov /*
30959f2e0fcSNikolai Kondrashov * Move Y coord to make space for high-order X
31059f2e0fcSNikolai Kondrashov * coord byte
31159f2e0fcSNikolai Kondrashov */
31259f2e0fcSNikolai Kondrashov data[6] = data[5];
31359f2e0fcSNikolai Kondrashov data[5] = data[4];
31459f2e0fcSNikolai Kondrashov /* Move high-order X coord byte */
31559f2e0fcSNikolai Kondrashov data[4] = data[8];
31659f2e0fcSNikolai Kondrashov /* Move high-order Y coord byte */
31759f2e0fcSNikolai Kondrashov data[7] = data[9];
31859f2e0fcSNikolai Kondrashov /* Place pressure bytes */
31959f2e0fcSNikolai Kondrashov data[8] = pressure_low_byte;
32059f2e0fcSNikolai Kondrashov data[9] = pressure_high_byte;
32159f2e0fcSNikolai Kondrashov }
32201309e29SNikolai Kondrashov /* If we need to emulate in-range detection */
3237f12dd24SNikolai Kondrashov if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
32401309e29SNikolai Kondrashov /* Set in-range bit */
32501309e29SNikolai Kondrashov data[1] |= 0x40;
32601309e29SNikolai Kondrashov /* (Re-)start in-range timeout */
32701309e29SNikolai Kondrashov mod_timer(&drvdata->inrange_timer,
32801309e29SNikolai Kondrashov jiffies + msecs_to_jiffies(100));
32901309e29SNikolai Kondrashov }
3301324c5acSNikolai Kondrashov /* If we report tilt and Y direction is flipped */
3317f12dd24SNikolai Kondrashov if (size >= 12 && pen->tilt_y_flipped)
3321324c5acSNikolai Kondrashov data[11] = -data[11];
3337e418667SNikolai Kondrashov
3347e418667SNikolai Kondrashov return 0;
3359614219eSNikolai Kondrashov }
336ff0c13d6SNikolai Kondrashov
3377e418667SNikolai Kondrashov /**
3387e418667SNikolai Kondrashov * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
3397e418667SNikolai Kondrashov *
3407e418667SNikolai Kondrashov * @drvdata: Driver data.
341337fa051SNikolai Kondrashov * @frame: The parameters of the frame controls to handle.
3427e418667SNikolai Kondrashov * @data: Report data buffer, can be modified.
3437e418667SNikolai Kondrashov * @size: Report data size, bytes.
3447e418667SNikolai Kondrashov *
3457e418667SNikolai Kondrashov * Returns:
3467e418667SNikolai Kondrashov * Negative value on error (stops event delivery), zero for success.
3477e418667SNikolai Kondrashov */
uclogic_raw_event_frame(struct uclogic_drvdata * drvdata,const struct uclogic_params_frame * frame,u8 * data,int size)348337fa051SNikolai Kondrashov static int uclogic_raw_event_frame(
349337fa051SNikolai Kondrashov struct uclogic_drvdata *drvdata,
350337fa051SNikolai Kondrashov const struct uclogic_params_frame *frame,
3517e418667SNikolai Kondrashov u8 *data, int size)
3527e418667SNikolai Kondrashov {
3537e418667SNikolai Kondrashov WARN_ON(drvdata == NULL);
3547e418667SNikolai Kondrashov WARN_ON(data == NULL && size != 0);
3557e418667SNikolai Kondrashov
356fde44ac5SNikolai Kondrashov /* If need to, and can, set pad device ID for Wacom drivers */
3577f12dd24SNikolai Kondrashov if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
358d170e8e0SNikolai Kondrashov /* If we also have a touch ring and the finger left it */
359caf7e934SNikolai Kondrashov if (frame->touch_byte > 0 && frame->touch_byte < size &&
360caf7e934SNikolai Kondrashov data[frame->touch_byte] == 0) {
361d170e8e0SNikolai Kondrashov data[frame->dev_id_byte] = 0;
362d170e8e0SNikolai Kondrashov } else {
3637f12dd24SNikolai Kondrashov data[frame->dev_id_byte] = 0xf;
364fde44ac5SNikolai Kondrashov }
365d170e8e0SNikolai Kondrashov }
366d170e8e0SNikolai Kondrashov
3678a47670cSNikolai Kondrashov /* If need to, and can, read rotary encoder state change */
3687f12dd24SNikolai Kondrashov if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
3697f12dd24SNikolai Kondrashov unsigned int byte = frame->re_lsb / 8;
3707f12dd24SNikolai Kondrashov unsigned int bit = frame->re_lsb % 8;
3718a47670cSNikolai Kondrashov
3728a47670cSNikolai Kondrashov u8 change;
3738a47670cSNikolai Kondrashov u8 prev_state = drvdata->re_state;
3748a47670cSNikolai Kondrashov /* Read Gray-coded state */
3758a47670cSNikolai Kondrashov u8 state = (data[byte] >> bit) & 0x3;
3768a47670cSNikolai Kondrashov /* Encode state change into 2-bit signed integer */
3778a47670cSNikolai Kondrashov if ((prev_state == 1 && state == 0) ||
3788a47670cSNikolai Kondrashov (prev_state == 2 && state == 3)) {
3798a47670cSNikolai Kondrashov change = 1;
3808a47670cSNikolai Kondrashov } else if ((prev_state == 2 && state == 0) ||
3818a47670cSNikolai Kondrashov (prev_state == 1 && state == 3)) {
3828a47670cSNikolai Kondrashov change = 3;
3838a47670cSNikolai Kondrashov } else {
3848a47670cSNikolai Kondrashov change = 0;
3858a47670cSNikolai Kondrashov }
3868a47670cSNikolai Kondrashov /* Write change */
3878a47670cSNikolai Kondrashov data[byte] = (data[byte] & ~((u8)3 << bit)) |
3888a47670cSNikolai Kondrashov (change << bit);
3898a47670cSNikolai Kondrashov /* Remember state */
3908a47670cSNikolai Kondrashov drvdata->re_state = state;
3918a47670cSNikolai Kondrashov }
3927e418667SNikolai Kondrashov
393d170e8e0SNikolai Kondrashov /* If need to, and can, transform the touch ring reports */
394fbc08b4eSNikolai Kondrashov if (frame->touch_byte > 0 && frame->touch_byte < size) {
395caf7e934SNikolai Kondrashov __s8 value = data[frame->touch_byte];
396d170e8e0SNikolai Kondrashov
397fbc08b4eSNikolai Kondrashov if (value != 0) {
398fbc08b4eSNikolai Kondrashov if (frame->touch_flip_at != 0) {
399fbc08b4eSNikolai Kondrashov value = frame->touch_flip_at - value;
400fbc08b4eSNikolai Kondrashov if (value <= 0)
401fbc08b4eSNikolai Kondrashov value = frame->touch_max + value;
402fbc08b4eSNikolai Kondrashov }
403fbc08b4eSNikolai Kondrashov data[frame->touch_byte] = value - 1;
404d170e8e0SNikolai Kondrashov }
405d170e8e0SNikolai Kondrashov }
406d170e8e0SNikolai Kondrashov
4072112b49eSNikolai Kondrashov /* If need to, and can, transform the bitmap dial reports */
4082112b49eSNikolai Kondrashov if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
4092112b49eSNikolai Kondrashov if (data[frame->bitmap_dial_byte] == 2)
4102112b49eSNikolai Kondrashov data[frame->bitmap_dial_byte] = -1;
4112112b49eSNikolai Kondrashov }
4122112b49eSNikolai Kondrashov
4137e418667SNikolai Kondrashov return 0;
414fde44ac5SNikolai Kondrashov }
415fde44ac5SNikolai Kondrashov
uclogic_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)4167e418667SNikolai Kondrashov static int uclogic_raw_event(struct hid_device *hdev,
4177e418667SNikolai Kondrashov struct hid_report *report,
4187e418667SNikolai Kondrashov u8 *data, int size)
4197e418667SNikolai Kondrashov {
420044fa816SNikolai Kondrashov unsigned int report_id = report->id;
4217e418667SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
4227e418667SNikolai Kondrashov struct uclogic_params *params = &drvdata->params;
4238b013098SNikolai Kondrashov struct uclogic_params_pen_subreport *subreport;
4248b013098SNikolai Kondrashov struct uclogic_params_pen_subreport *subreport_list_end;
425337fa051SNikolai Kondrashov size_t i;
4267e418667SNikolai Kondrashov
427fea53b9fSNikolai Kondrashov /* Do not handle anything but input reports */
428fea53b9fSNikolai Kondrashov if (report->type != HID_INPUT_REPORT)
429fea53b9fSNikolai Kondrashov return 0;
430fea53b9fSNikolai Kondrashov
431a251d657SJosé Expósito if (uclogic_exec_event_hook(params, data, size))
432a251d657SJosé Expósito return 0;
433a251d657SJosé Expósito
4348b013098SNikolai Kondrashov while (true) {
4357e418667SNikolai Kondrashov /* Tweak pen reports, if necessary */
436606dadc1SNikolai Kondrashov if ((report_id == params->pen.id) && (size >= 2)) {
4378b013098SNikolai Kondrashov subreport_list_end =
4388b013098SNikolai Kondrashov params->pen.subreport_list +
4398b013098SNikolai Kondrashov ARRAY_SIZE(params->pen.subreport_list);
4408b013098SNikolai Kondrashov /* Try to match a subreport */
4418b013098SNikolai Kondrashov for (subreport = params->pen.subreport_list;
442e6be956fSNikolai Kondrashov subreport < subreport_list_end; subreport++) {
443e6be956fSNikolai Kondrashov if (subreport->value != 0 &&
444e6be956fSNikolai Kondrashov subreport->value == data[1]) {
445e6be956fSNikolai Kondrashov break;
446e6be956fSNikolai Kondrashov }
447e6be956fSNikolai Kondrashov }
4488b013098SNikolai Kondrashov /* If a subreport matched */
4498b013098SNikolai Kondrashov if (subreport < subreport_list_end) {
4508b013098SNikolai Kondrashov /* Change to subreport ID, and restart */
4518b013098SNikolai Kondrashov report_id = data[0] = subreport->id;
4528b013098SNikolai Kondrashov continue;
453044fa816SNikolai Kondrashov } else {
4547e418667SNikolai Kondrashov return uclogic_raw_event_pen(drvdata, data, size);
4557e418667SNikolai Kondrashov }
456044fa816SNikolai Kondrashov }
4577e418667SNikolai Kondrashov
4587e418667SNikolai Kondrashov /* Tweak frame control reports, if necessary */
459337fa051SNikolai Kondrashov for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
460337fa051SNikolai Kondrashov if (report_id == params->frame_list[i].id) {
461337fa051SNikolai Kondrashov return uclogic_raw_event_frame(
462337fa051SNikolai Kondrashov drvdata, ¶ms->frame_list[i],
463337fa051SNikolai Kondrashov data, size);
464337fa051SNikolai Kondrashov }
465337fa051SNikolai Kondrashov }
4667e418667SNikolai Kondrashov
4678b013098SNikolai Kondrashov break;
4688b013098SNikolai Kondrashov }
4698b013098SNikolai Kondrashov
470ff0c13d6SNikolai Kondrashov return 0;
471ff0c13d6SNikolai Kondrashov }
472ff0c13d6SNikolai Kondrashov
uclogic_remove(struct hid_device * hdev)4739614219eSNikolai Kondrashov static void uclogic_remove(struct hid_device *hdev)
4749614219eSNikolai Kondrashov {
4759614219eSNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
4769614219eSNikolai Kondrashov
47701309e29SNikolai Kondrashov del_timer_sync(&drvdata->inrange_timer);
4789614219eSNikolai Kondrashov hid_hw_stop(hdev);
4799614219eSNikolai Kondrashov kfree(drvdata->desc_ptr);
4809614219eSNikolai Kondrashov uclogic_params_cleanup(&drvdata->params);
4819614219eSNikolai Kondrashov }
4829614219eSNikolai Kondrashov
483ff0c13d6SNikolai Kondrashov static const struct hid_device_id uclogic_devices[] = {
484ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
485ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
486ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
487ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
488ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
489ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
490ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
491ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
492ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
493ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
494ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
495ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
496ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
497ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
4989614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
4999614219eSNikolai Kondrashov USB_DEVICE_ID_HUION_TABLET) },
500315ffcc9SKyle Godbey { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
50185e86071SNikolai Kondrashov USB_DEVICE_ID_HUION_TABLET2) },
502f7271b2aSCristian Klein { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
503f7271b2aSCristian Klein USB_DEVICE_ID_TRUST_PANORA_TABLET) },
5049614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5059614219eSNikolai Kondrashov USB_DEVICE_ID_HUION_TABLET) },
5069614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5079614219eSNikolai Kondrashov USB_DEVICE_ID_YIYNOVA_TABLET) },
5089614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5099614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
5109614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5119614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
5129614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5130c15efe9SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
5140c15efe9SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5159614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
5169614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
5179614219eSNikolai Kondrashov USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
518022fc531SMartijn van de Streek { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
519022fc531SMartijn van de Streek USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
5209614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5217495fb7eSJosé Expósito USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
5227495fb7eSJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
523e902ed93SNikolai Kondrashov USB_DEVICE_ID_UGEE_TABLET_G5) },
524e902ed93SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5259614219eSNikolai Kondrashov USB_DEVICE_ID_UGEE_TABLET_EX07S) },
526c3e5a67cSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
52788bb346dSWang Xuerui USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
52888bb346dSWang Xuerui { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
529c3e5a67cSNikolai Kondrashov USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
530492a9e9aSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
531492a9e9aSNikolai Kondrashov USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
53208367be1SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
53308367be1SNikolai Kondrashov USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
53461b1db5aSRoman Romanenko { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
535febb2c0dSJosé Expósito USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) },
536febb2c0dSJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5370cb1fc09SJosé Expósito USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
5380cb1fc09SJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5399266a881SJosé Expósito USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
5409266a881SJosé Expósito .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
5419266a881SJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
54293b40b3eSJosé Expósito USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
54393b40b3eSJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5447744ca57SJosé Expósito USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
5457744ca57SJosé Expósito .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
5467744ca57SJosé Expósito { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
54761b1db5aSRoman Romanenko USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
548ff0c13d6SNikolai Kondrashov { }
549ff0c13d6SNikolai Kondrashov };
550ff0c13d6SNikolai Kondrashov MODULE_DEVICE_TABLE(hid, uclogic_devices);
551ff0c13d6SNikolai Kondrashov
552ff0c13d6SNikolai Kondrashov static struct hid_driver uclogic_driver = {
553ff0c13d6SNikolai Kondrashov .name = "uclogic",
554ff0c13d6SNikolai Kondrashov .id_table = uclogic_devices,
555ff0c13d6SNikolai Kondrashov .probe = uclogic_probe,
5569614219eSNikolai Kondrashov .remove = uclogic_remove,
557ff0c13d6SNikolai Kondrashov .report_fixup = uclogic_report_fixup,
558ff0c13d6SNikolai Kondrashov .raw_event = uclogic_raw_event,
559f5927973SNikolai Kondrashov .input_mapping = uclogic_input_mapping,
560ff0c13d6SNikolai Kondrashov .input_configured = uclogic_input_configured,
561251b4275SNikolai Kondrashov #ifdef CONFIG_PM
562251b4275SNikolai Kondrashov .resume = uclogic_resume,
563251b4275SNikolai Kondrashov .reset_resume = uclogic_resume,
564251b4275SNikolai Kondrashov #endif
565ff0c13d6SNikolai Kondrashov };
566ff0c13d6SNikolai Kondrashov module_hid_driver(uclogic_driver);
567ff0c13d6SNikolai Kondrashov
568ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Martin Rusko");
569ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Nikolai Kondrashov");
5709d262f35SJeff Johnson MODULE_DESCRIPTION("HID driver for UC-Logic devices not fully compliant with HID standard");
571ff0c13d6SNikolai Kondrashov MODULE_LICENSE("GPL");
57222c0a46aSJosé Expósito MODULE_DESCRIPTION("HID driver for UC-Logic devices not fully compliant with HID standard");
573a251d657SJosé Expósito
574a251d657SJosé Expósito #ifdef CONFIG_HID_KUNIT_TEST
575a251d657SJosé Expósito #include "hid-uclogic-core-test.c"
576a251d657SJosé Expósito #endif
577