xref: /linux/drivers/hid/hid-uclogic-core.c (revision d170e8e02729ad3bc4924005cec1ad38409d82af)
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 
25ff0c13d6SNikolai Kondrashov /* Driver data */
26ff0c13d6SNikolai Kondrashov struct uclogic_drvdata {
279614219eSNikolai Kondrashov 	/* Interface parameters */
289614219eSNikolai Kondrashov 	struct uclogic_params params;
299614219eSNikolai Kondrashov 	/* Pointer to the replacement report descriptor. NULL if none. */
309614219eSNikolai Kondrashov 	__u8 *desc_ptr;
319614219eSNikolai Kondrashov 	/*
329614219eSNikolai Kondrashov 	 * Size of the replacement report descriptor.
339614219eSNikolai Kondrashov 	 * Only valid if desc_ptr is not NULL
349614219eSNikolai Kondrashov 	 */
359614219eSNikolai Kondrashov 	unsigned int desc_size;
3601309e29SNikolai Kondrashov 	/* Pen input device */
3701309e29SNikolai Kondrashov 	struct input_dev *pen_input;
3801309e29SNikolai Kondrashov 	/* In-range timer */
3901309e29SNikolai Kondrashov 	struct timer_list inrange_timer;
408a47670cSNikolai Kondrashov 	/* Last rotary encoder state, or U8_MAX for none */
418a47670cSNikolai Kondrashov 	u8 re_state;
42ff0c13d6SNikolai Kondrashov };
43ff0c13d6SNikolai Kondrashov 
4401309e29SNikolai Kondrashov /**
4501309e29SNikolai Kondrashov  * uclogic_inrange_timeout - handle pen in-range state timeout.
4601309e29SNikolai Kondrashov  * Emulate input events normally generated when pen goes out of range for
4701309e29SNikolai Kondrashov  * tablets which don't report that.
4801309e29SNikolai Kondrashov  *
4901309e29SNikolai Kondrashov  * @t:	The timer the timeout handler is attached to, stored in a struct
5001309e29SNikolai Kondrashov  *	uclogic_drvdata.
5101309e29SNikolai Kondrashov  */
5201309e29SNikolai Kondrashov static void uclogic_inrange_timeout(struct timer_list *t)
5301309e29SNikolai Kondrashov {
5401309e29SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
5501309e29SNikolai Kondrashov 							inrange_timer);
5601309e29SNikolai Kondrashov 	struct input_dev *input = drvdata->pen_input;
5701309e29SNikolai Kondrashov 
5801309e29SNikolai Kondrashov 	if (input == NULL)
5901309e29SNikolai Kondrashov 		return;
6001309e29SNikolai Kondrashov 	input_report_abs(input, ABS_PRESSURE, 0);
6101309e29SNikolai Kondrashov 	/* If BTN_TOUCH state is changing */
6201309e29SNikolai Kondrashov 	if (test_bit(BTN_TOUCH, input->key)) {
6301309e29SNikolai Kondrashov 		input_event(input, EV_MSC, MSC_SCAN,
6401309e29SNikolai Kondrashov 				/* Digitizer Tip Switch usage */
6501309e29SNikolai Kondrashov 				0xd0042);
6601309e29SNikolai Kondrashov 		input_report_key(input, BTN_TOUCH, 0);
6701309e29SNikolai Kondrashov 	}
6801309e29SNikolai Kondrashov 	input_report_key(input, BTN_TOOL_PEN, 0);
6901309e29SNikolai Kondrashov 	input_sync(input);
7001309e29SNikolai Kondrashov }
7101309e29SNikolai Kondrashov 
72ff0c13d6SNikolai Kondrashov static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
73ff0c13d6SNikolai Kondrashov 					unsigned int *rsize)
74ff0c13d6SNikolai Kondrashov {
75ff0c13d6SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
76ff0c13d6SNikolai Kondrashov 
779614219eSNikolai Kondrashov 	if (drvdata->desc_ptr != NULL) {
789614219eSNikolai Kondrashov 		rdesc = drvdata->desc_ptr;
799614219eSNikolai Kondrashov 		*rsize = drvdata->desc_size;
809614219eSNikolai Kondrashov 	}
81ff0c13d6SNikolai Kondrashov 	return rdesc;
82ff0c13d6SNikolai Kondrashov }
83ff0c13d6SNikolai Kondrashov 
84ff0c13d6SNikolai Kondrashov static int uclogic_input_configured(struct hid_device *hdev,
85ff0c13d6SNikolai Kondrashov 		struct hid_input *hi)
86ff0c13d6SNikolai Kondrashov {
8701309e29SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
8801309e29SNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
89ff0c13d6SNikolai Kondrashov 	char *name;
90ff0c13d6SNikolai Kondrashov 	const char *suffix = NULL;
91ff0c13d6SNikolai Kondrashov 	struct hid_field *field;
92ff0c13d6SNikolai Kondrashov 	size_t len;
93*d170e8e0SNikolai Kondrashov 	size_t i;
94*d170e8e0SNikolai Kondrashov 	const struct uclogic_params_frame *frame;
95ff0c13d6SNikolai Kondrashov 
96ff0c13d6SNikolai Kondrashov 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
97ff0c13d6SNikolai Kondrashov 	if (!hi->report)
98ff0c13d6SNikolai Kondrashov 		return 0;
99ff0c13d6SNikolai Kondrashov 
10001309e29SNikolai Kondrashov 	/*
10101309e29SNikolai Kondrashov 	 * If this is the input corresponding to the pen report
10201309e29SNikolai Kondrashov 	 * in need of tweaking.
10301309e29SNikolai Kondrashov 	 */
10401309e29SNikolai Kondrashov 	if (hi->report->id == params->pen.id) {
10501309e29SNikolai Kondrashov 		/* Remember the input device so we can simulate events */
10601309e29SNikolai Kondrashov 		drvdata->pen_input = hi->input;
10701309e29SNikolai Kondrashov 	}
10801309e29SNikolai Kondrashov 
109*d170e8e0SNikolai Kondrashov 	/* If it's one of the frame devices */
110*d170e8e0SNikolai Kondrashov 	for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
111*d170e8e0SNikolai Kondrashov 		frame = &params->frame_list[i];
112*d170e8e0SNikolai Kondrashov 		if (hi->report->id == frame->id) {
113*d170e8e0SNikolai Kondrashov 			/*
114*d170e8e0SNikolai Kondrashov 			 * Disable EV_MSC reports for touch ring interfaces to
115*d170e8e0SNikolai Kondrashov 			 * make the Wacom driver pickup touch ring extents
116*d170e8e0SNikolai Kondrashov 			 */
117*d170e8e0SNikolai Kondrashov 			if (frame->touch_ring_byte > 0)
118*d170e8e0SNikolai Kondrashov 				__clear_bit(EV_MSC, hi->input->evbit);
119*d170e8e0SNikolai Kondrashov 		}
120*d170e8e0SNikolai Kondrashov 	}
121*d170e8e0SNikolai Kondrashov 
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:
135ff0c13d6SNikolai Kondrashov 		suffix = "Pen";
136ff0c13d6SNikolai Kondrashov 		break;
137ff0c13d6SNikolai Kondrashov 	case HID_CP_CONSUMER_CONTROL:
138ff0c13d6SNikolai Kondrashov 		suffix = "Consumer Control";
139ff0c13d6SNikolai Kondrashov 		break;
140ff0c13d6SNikolai Kondrashov 	case HID_GD_SYSTEM_CONTROL:
141ff0c13d6SNikolai Kondrashov 		suffix = "System Control";
142ff0c13d6SNikolai Kondrashov 		break;
143ff0c13d6SNikolai Kondrashov 	}
144ff0c13d6SNikolai Kondrashov 
145ff0c13d6SNikolai Kondrashov 	if (suffix) {
146ff0c13d6SNikolai Kondrashov 		len = strlen(hdev->name) + 2 + strlen(suffix);
147ff0c13d6SNikolai Kondrashov 		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
148ff0c13d6SNikolai Kondrashov 		if (name) {
149ff0c13d6SNikolai Kondrashov 			snprintf(name, len, "%s %s", hdev->name, suffix);
150ff0c13d6SNikolai Kondrashov 			hi->input->name = name;
151ff0c13d6SNikolai Kondrashov 		}
152ff0c13d6SNikolai Kondrashov 	}
153ff0c13d6SNikolai Kondrashov 
154ff0c13d6SNikolai Kondrashov 	return 0;
155ff0c13d6SNikolai Kondrashov }
156ff0c13d6SNikolai Kondrashov 
157ff0c13d6SNikolai Kondrashov static int uclogic_probe(struct hid_device *hdev,
158ff0c13d6SNikolai Kondrashov 		const struct hid_device_id *id)
159ff0c13d6SNikolai Kondrashov {
160ff0c13d6SNikolai Kondrashov 	int rc;
1619614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = NULL;
1629614219eSNikolai Kondrashov 	bool params_initialized = false;
163ff0c13d6SNikolai Kondrashov 
16493020953SGreg Kroah-Hartman 	if (!hid_is_usb(hdev))
16593020953SGreg Kroah-Hartman 		return -EINVAL;
16693020953SGreg Kroah-Hartman 
167ff0c13d6SNikolai Kondrashov 	/*
168ff0c13d6SNikolai Kondrashov 	 * libinput requires the pad interface to be on a different node
169ff0c13d6SNikolai Kondrashov 	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
170ff0c13d6SNikolai Kondrashov 	 */
171ff0c13d6SNikolai Kondrashov 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
172ff0c13d6SNikolai Kondrashov 
173ff0c13d6SNikolai Kondrashov 	/* Allocate and assign driver data */
174ff0c13d6SNikolai Kondrashov 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1759614219eSNikolai Kondrashov 	if (drvdata == NULL) {
1769614219eSNikolai Kondrashov 		rc = -ENOMEM;
1779614219eSNikolai Kondrashov 		goto failure;
1789614219eSNikolai Kondrashov 	}
17901309e29SNikolai Kondrashov 	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
1808a47670cSNikolai Kondrashov 	drvdata->re_state = U8_MAX;
181ff0c13d6SNikolai Kondrashov 	hid_set_drvdata(hdev, drvdata);
182ff0c13d6SNikolai Kondrashov 
1839614219eSNikolai Kondrashov 	/* Initialize the device and retrieve interface parameters */
1849614219eSNikolai Kondrashov 	rc = uclogic_params_init(&drvdata->params, hdev);
1859614219eSNikolai Kondrashov 	if (rc != 0) {
1869614219eSNikolai Kondrashov 		hid_err(hdev, "failed probing parameters: %d\n", rc);
1879614219eSNikolai Kondrashov 		goto failure;
188ff0c13d6SNikolai Kondrashov 	}
1899614219eSNikolai Kondrashov 	params_initialized = true;
1909614219eSNikolai Kondrashov 	hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
1919614219eSNikolai Kondrashov 		UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
1929614219eSNikolai Kondrashov 	if (drvdata->params.invalid) {
1939614219eSNikolai Kondrashov 		hid_info(hdev, "interface is invalid, ignoring\n");
1949614219eSNikolai Kondrashov 		rc = -ENODEV;
1959614219eSNikolai Kondrashov 		goto failure;
1969614219eSNikolai Kondrashov 	}
197ff0c13d6SNikolai Kondrashov 
1989614219eSNikolai Kondrashov 	/* Generate replacement report descriptor */
1999614219eSNikolai Kondrashov 	rc = uclogic_params_get_desc(&drvdata->params,
2009614219eSNikolai Kondrashov 				     &drvdata->desc_ptr,
2019614219eSNikolai Kondrashov 				     &drvdata->desc_size);
202ff0c13d6SNikolai Kondrashov 	if (rc) {
2039614219eSNikolai Kondrashov 		hid_err(hdev,
2049614219eSNikolai Kondrashov 			"failed generating replacement report descriptor: %d\n",
2059614219eSNikolai Kondrashov 			rc);
2069614219eSNikolai Kondrashov 		goto failure;
207ff0c13d6SNikolai Kondrashov 	}
208ff0c13d6SNikolai Kondrashov 
209ff0c13d6SNikolai Kondrashov 	rc = hid_parse(hdev);
210ff0c13d6SNikolai Kondrashov 	if (rc) {
211ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "parse failed\n");
2129614219eSNikolai Kondrashov 		goto failure;
213ff0c13d6SNikolai Kondrashov 	}
214ff0c13d6SNikolai Kondrashov 
215ff0c13d6SNikolai Kondrashov 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
216ff0c13d6SNikolai Kondrashov 	if (rc) {
217ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "hw start failed\n");
2189614219eSNikolai Kondrashov 		goto failure;
2199614219eSNikolai Kondrashov 	}
2209614219eSNikolai Kondrashov 
2219614219eSNikolai Kondrashov 	return 0;
2229614219eSNikolai Kondrashov failure:
2239614219eSNikolai Kondrashov 	/* Assume "remove" might not be called if "probe" failed */
2249614219eSNikolai Kondrashov 	if (params_initialized)
2259614219eSNikolai Kondrashov 		uclogic_params_cleanup(&drvdata->params);
226ff0c13d6SNikolai Kondrashov 	return rc;
227ff0c13d6SNikolai Kondrashov }
228ff0c13d6SNikolai Kondrashov 
229251b4275SNikolai Kondrashov #ifdef CONFIG_PM
230251b4275SNikolai Kondrashov static int uclogic_resume(struct hid_device *hdev)
231251b4275SNikolai Kondrashov {
232251b4275SNikolai Kondrashov 	int rc;
233251b4275SNikolai Kondrashov 	struct uclogic_params params;
234251b4275SNikolai Kondrashov 
235251b4275SNikolai Kondrashov 	/* Re-initialize the device, but discard parameters */
236251b4275SNikolai Kondrashov 	rc = uclogic_params_init(&params, hdev);
237251b4275SNikolai Kondrashov 	if (rc != 0)
238251b4275SNikolai Kondrashov 		hid_err(hdev, "failed to re-initialize the device\n");
239251b4275SNikolai Kondrashov 	else
240251b4275SNikolai Kondrashov 		uclogic_params_cleanup(&params);
241251b4275SNikolai Kondrashov 
242251b4275SNikolai Kondrashov 	return rc;
243251b4275SNikolai Kondrashov }
244251b4275SNikolai Kondrashov #endif
245251b4275SNikolai Kondrashov 
2467e418667SNikolai Kondrashov /**
2477e418667SNikolai Kondrashov  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
2487e418667SNikolai Kondrashov  *
2497e418667SNikolai Kondrashov  * @drvdata:	Driver data.
2507e418667SNikolai Kondrashov  * @data:	Report data buffer, can be modified.
2517e418667SNikolai Kondrashov  * @size:	Report data size, bytes.
2527e418667SNikolai Kondrashov  *
2537e418667SNikolai Kondrashov  * Returns:
2547e418667SNikolai Kondrashov  *	Negative value on error (stops event delivery), zero for success.
2557e418667SNikolai Kondrashov  */
2567e418667SNikolai Kondrashov static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
257ff0c13d6SNikolai Kondrashov 					u8 *data, int size)
258ff0c13d6SNikolai Kondrashov {
2597f12dd24SNikolai Kondrashov 	struct uclogic_params_pen *pen = &drvdata->params.pen;
260ff0c13d6SNikolai Kondrashov 
2617e418667SNikolai Kondrashov 	WARN_ON(drvdata == NULL);
2627e418667SNikolai Kondrashov 	WARN_ON(data == NULL && size != 0);
2637e418667SNikolai Kondrashov 
2649614219eSNikolai Kondrashov 	/* If in-range reports are inverted */
2657f12dd24SNikolai Kondrashov 	if (pen->inrange ==
2669614219eSNikolai Kondrashov 		UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
267ff0c13d6SNikolai Kondrashov 		/* Invert the in-range bit */
268ff0c13d6SNikolai Kondrashov 		data[1] ^= 0x40;
269ff0c13d6SNikolai Kondrashov 	}
27059f2e0fcSNikolai Kondrashov 	/*
27159f2e0fcSNikolai Kondrashov 	 * If report contains fragmented high-resolution pen
27259f2e0fcSNikolai Kondrashov 	 * coordinates
27359f2e0fcSNikolai Kondrashov 	 */
2747f12dd24SNikolai Kondrashov 	if (size >= 10 && pen->fragmented_hires) {
27559f2e0fcSNikolai Kondrashov 		u8 pressure_low_byte;
27659f2e0fcSNikolai Kondrashov 		u8 pressure_high_byte;
27759f2e0fcSNikolai Kondrashov 
27859f2e0fcSNikolai Kondrashov 		/* Lift pressure bytes */
27959f2e0fcSNikolai Kondrashov 		pressure_low_byte = data[6];
28059f2e0fcSNikolai Kondrashov 		pressure_high_byte = data[7];
28159f2e0fcSNikolai Kondrashov 		/*
28259f2e0fcSNikolai Kondrashov 		 * Move Y coord to make space for high-order X
28359f2e0fcSNikolai Kondrashov 		 * coord byte
28459f2e0fcSNikolai Kondrashov 		 */
28559f2e0fcSNikolai Kondrashov 		data[6] = data[5];
28659f2e0fcSNikolai Kondrashov 		data[5] = data[4];
28759f2e0fcSNikolai Kondrashov 		/* Move high-order X coord byte */
28859f2e0fcSNikolai Kondrashov 		data[4] = data[8];
28959f2e0fcSNikolai Kondrashov 		/* Move high-order Y coord byte */
29059f2e0fcSNikolai Kondrashov 		data[7] = data[9];
29159f2e0fcSNikolai Kondrashov 		/* Place pressure bytes */
29259f2e0fcSNikolai Kondrashov 		data[8] = pressure_low_byte;
29359f2e0fcSNikolai Kondrashov 		data[9] = pressure_high_byte;
29459f2e0fcSNikolai Kondrashov 	}
29501309e29SNikolai Kondrashov 	/* If we need to emulate in-range detection */
2967f12dd24SNikolai Kondrashov 	if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
29701309e29SNikolai Kondrashov 		/* Set in-range bit */
29801309e29SNikolai Kondrashov 		data[1] |= 0x40;
29901309e29SNikolai Kondrashov 		/* (Re-)start in-range timeout */
30001309e29SNikolai Kondrashov 		mod_timer(&drvdata->inrange_timer,
30101309e29SNikolai Kondrashov 				jiffies + msecs_to_jiffies(100));
30201309e29SNikolai Kondrashov 	}
3031324c5acSNikolai Kondrashov 	/* If we report tilt and Y direction is flipped */
3047f12dd24SNikolai Kondrashov 	if (size >= 12 && pen->tilt_y_flipped)
3051324c5acSNikolai Kondrashov 		data[11] = -data[11];
3067e418667SNikolai Kondrashov 
3077e418667SNikolai Kondrashov 	return 0;
3089614219eSNikolai Kondrashov }
309ff0c13d6SNikolai Kondrashov 
3107e418667SNikolai Kondrashov /**
3117e418667SNikolai Kondrashov  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
3127e418667SNikolai Kondrashov  *
3137e418667SNikolai Kondrashov  * @drvdata:	Driver data.
314337fa051SNikolai Kondrashov  * @frame:	The parameters of the frame controls to handle.
3157e418667SNikolai Kondrashov  * @data:	Report data buffer, can be modified.
3167e418667SNikolai Kondrashov  * @size:	Report data size, bytes.
3177e418667SNikolai Kondrashov  *
3187e418667SNikolai Kondrashov  * Returns:
3197e418667SNikolai Kondrashov  *	Negative value on error (stops event delivery), zero for success.
3207e418667SNikolai Kondrashov  */
321337fa051SNikolai Kondrashov static int uclogic_raw_event_frame(
322337fa051SNikolai Kondrashov 		struct uclogic_drvdata *drvdata,
323337fa051SNikolai Kondrashov 		const struct uclogic_params_frame *frame,
3247e418667SNikolai Kondrashov 		u8 *data, int size)
3257e418667SNikolai Kondrashov {
3267e418667SNikolai Kondrashov 	WARN_ON(drvdata == NULL);
3277e418667SNikolai Kondrashov 	WARN_ON(data == NULL && size != 0);
3287e418667SNikolai Kondrashov 
329fde44ac5SNikolai Kondrashov 	/* If need to, and can, set pad device ID for Wacom drivers */
3307f12dd24SNikolai Kondrashov 	if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
331*d170e8e0SNikolai Kondrashov 		/* If we also have a touch ring and the finger left it */
332*d170e8e0SNikolai Kondrashov 		if (frame->touch_ring_byte > 0 &&
333*d170e8e0SNikolai Kondrashov 		    frame->touch_ring_byte < size &&
334*d170e8e0SNikolai Kondrashov 		    data[frame->touch_ring_byte] == 0) {
335*d170e8e0SNikolai Kondrashov 			data[frame->dev_id_byte] = 0;
336*d170e8e0SNikolai Kondrashov 		} else {
3377f12dd24SNikolai Kondrashov 			data[frame->dev_id_byte] = 0xf;
338fde44ac5SNikolai Kondrashov 		}
339*d170e8e0SNikolai Kondrashov 	}
340*d170e8e0SNikolai Kondrashov 
3418a47670cSNikolai Kondrashov 	/* If need to, and can, read rotary encoder state change */
3427f12dd24SNikolai Kondrashov 	if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
3437f12dd24SNikolai Kondrashov 		unsigned int byte = frame->re_lsb / 8;
3447f12dd24SNikolai Kondrashov 		unsigned int bit = frame->re_lsb % 8;
3458a47670cSNikolai Kondrashov 
3468a47670cSNikolai Kondrashov 		u8 change;
3478a47670cSNikolai Kondrashov 		u8 prev_state = drvdata->re_state;
3488a47670cSNikolai Kondrashov 		/* Read Gray-coded state */
3498a47670cSNikolai Kondrashov 		u8 state = (data[byte] >> bit) & 0x3;
3508a47670cSNikolai Kondrashov 		/* Encode state change into 2-bit signed integer */
3518a47670cSNikolai Kondrashov 		if ((prev_state == 1 && state == 0) ||
3528a47670cSNikolai Kondrashov 		    (prev_state == 2 && state == 3)) {
3538a47670cSNikolai Kondrashov 			change = 1;
3548a47670cSNikolai Kondrashov 		} else if ((prev_state == 2 && state == 0) ||
3558a47670cSNikolai Kondrashov 			   (prev_state == 1 && state == 3)) {
3568a47670cSNikolai Kondrashov 			change = 3;
3578a47670cSNikolai Kondrashov 		} else {
3588a47670cSNikolai Kondrashov 			change = 0;
3598a47670cSNikolai Kondrashov 		}
3608a47670cSNikolai Kondrashov 		/* Write change */
3618a47670cSNikolai Kondrashov 		data[byte] = (data[byte] & ~((u8)3 << bit)) |
3628a47670cSNikolai Kondrashov 				(change << bit);
3638a47670cSNikolai Kondrashov 		/* Remember state */
3648a47670cSNikolai Kondrashov 		drvdata->re_state = state;
3658a47670cSNikolai Kondrashov 	}
3667e418667SNikolai Kondrashov 
367*d170e8e0SNikolai Kondrashov 	/* If need to, and can, transform the touch ring reports */
368*d170e8e0SNikolai Kondrashov 	if (frame->touch_ring_byte > 0 && frame->touch_ring_byte < size &&
369*d170e8e0SNikolai Kondrashov 	    frame->touch_ring_flip_at != 0) {
370*d170e8e0SNikolai Kondrashov 		__s8 value = data[frame->touch_ring_byte];
371*d170e8e0SNikolai Kondrashov 
372*d170e8e0SNikolai Kondrashov 		if (value != 0) {
373*d170e8e0SNikolai Kondrashov 			value = frame->touch_ring_flip_at - value;
374*d170e8e0SNikolai Kondrashov 			if (value < 0)
375*d170e8e0SNikolai Kondrashov 				value = frame->touch_ring_max + value;
376*d170e8e0SNikolai Kondrashov 
377*d170e8e0SNikolai Kondrashov 			data[frame->touch_ring_byte] = value;
378*d170e8e0SNikolai Kondrashov 		}
379*d170e8e0SNikolai Kondrashov 	}
380*d170e8e0SNikolai Kondrashov 
3817e418667SNikolai Kondrashov 	return 0;
382fde44ac5SNikolai Kondrashov }
383fde44ac5SNikolai Kondrashov 
3847e418667SNikolai Kondrashov static int uclogic_raw_event(struct hid_device *hdev,
3857e418667SNikolai Kondrashov 				struct hid_report *report,
3867e418667SNikolai Kondrashov 				u8 *data, int size)
3877e418667SNikolai Kondrashov {
388044fa816SNikolai Kondrashov 	unsigned int report_id = report->id;
3897e418667SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
3907e418667SNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
3918b013098SNikolai Kondrashov 	struct uclogic_params_pen_subreport *subreport;
3928b013098SNikolai Kondrashov 	struct uclogic_params_pen_subreport *subreport_list_end;
393337fa051SNikolai Kondrashov 	size_t i;
3947e418667SNikolai Kondrashov 
395fea53b9fSNikolai Kondrashov 	/* Do not handle anything but input reports */
396fea53b9fSNikolai Kondrashov 	if (report->type != HID_INPUT_REPORT)
397fea53b9fSNikolai Kondrashov 		return 0;
398fea53b9fSNikolai Kondrashov 
3998b013098SNikolai Kondrashov 	while (true) {
4007e418667SNikolai Kondrashov 		/* Tweak pen reports, if necessary */
401606dadc1SNikolai Kondrashov 		if ((report_id == params->pen.id) && (size >= 2)) {
4028b013098SNikolai Kondrashov 			subreport_list_end =
4038b013098SNikolai Kondrashov 				params->pen.subreport_list +
4048b013098SNikolai Kondrashov 				ARRAY_SIZE(params->pen.subreport_list);
4058b013098SNikolai Kondrashov 			/* Try to match a subreport */
4068b013098SNikolai Kondrashov 			for (subreport = params->pen.subreport_list;
407e6be956fSNikolai Kondrashov 			     subreport < subreport_list_end; subreport++) {
408e6be956fSNikolai Kondrashov 				if (subreport->value != 0 &&
409e6be956fSNikolai Kondrashov 				    subreport->value == data[1]) {
410e6be956fSNikolai Kondrashov 					break;
411e6be956fSNikolai Kondrashov 				}
412e6be956fSNikolai Kondrashov 			}
4138b013098SNikolai Kondrashov 			/* If a subreport matched */
4148b013098SNikolai Kondrashov 			if (subreport < subreport_list_end) {
4158b013098SNikolai Kondrashov 				/* Change to subreport ID, and restart */
4168b013098SNikolai Kondrashov 				report_id = data[0] = subreport->id;
4178b013098SNikolai Kondrashov 				continue;
418044fa816SNikolai Kondrashov 			} else {
4197e418667SNikolai Kondrashov 				return uclogic_raw_event_pen(drvdata, data, size);
4207e418667SNikolai Kondrashov 			}
421044fa816SNikolai Kondrashov 		}
4227e418667SNikolai Kondrashov 
4237e418667SNikolai Kondrashov 		/* Tweak frame control reports, if necessary */
424337fa051SNikolai Kondrashov 		for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
425337fa051SNikolai Kondrashov 			if (report_id == params->frame_list[i].id) {
426337fa051SNikolai Kondrashov 				return uclogic_raw_event_frame(
427337fa051SNikolai Kondrashov 					drvdata, &params->frame_list[i],
428337fa051SNikolai Kondrashov 					data, size);
429337fa051SNikolai Kondrashov 			}
430337fa051SNikolai Kondrashov 		}
4317e418667SNikolai Kondrashov 
4328b013098SNikolai Kondrashov 		break;
4338b013098SNikolai Kondrashov 	}
4348b013098SNikolai Kondrashov 
435ff0c13d6SNikolai Kondrashov 	return 0;
436ff0c13d6SNikolai Kondrashov }
437ff0c13d6SNikolai Kondrashov 
4389614219eSNikolai Kondrashov static void uclogic_remove(struct hid_device *hdev)
4399614219eSNikolai Kondrashov {
4409614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
4419614219eSNikolai Kondrashov 
44201309e29SNikolai Kondrashov 	del_timer_sync(&drvdata->inrange_timer);
4439614219eSNikolai Kondrashov 	hid_hw_stop(hdev);
4449614219eSNikolai Kondrashov 	kfree(drvdata->desc_ptr);
4459614219eSNikolai Kondrashov 	uclogic_params_cleanup(&drvdata->params);
4469614219eSNikolai Kondrashov }
4479614219eSNikolai Kondrashov 
448ff0c13d6SNikolai Kondrashov static const struct hid_device_id uclogic_devices[] = {
449ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
450ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
451ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
452ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
453ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
454ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
455ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
456ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
457ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
458ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
459ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
460ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
461ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
462ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
4639614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
4649614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
465315ffcc9SKyle Godbey 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
46685e86071SNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET2) },
467f7271b2aSCristian Klein 	{ HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
468f7271b2aSCristian Klein 				USB_DEVICE_ID_TRUST_PANORA_TABLET) },
4699614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4709614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
4719614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4729614219eSNikolai Kondrashov 				USB_DEVICE_ID_YIYNOVA_TABLET) },
4739614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4749614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
4759614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4769614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
4779614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4780c15efe9SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
4790c15efe9SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4809614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
4819614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
4829614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
483022fc531SMartijn van de Streek 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
484022fc531SMartijn van de Streek 				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
4859614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
486e902ed93SNikolai Kondrashov 				USB_DEVICE_ID_UGEE_TABLET_G5) },
487e902ed93SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
4889614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
489c3e5a67cSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
49088bb346dSWang Xuerui 				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
49188bb346dSWang Xuerui 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
492c3e5a67cSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
493492a9e9aSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
494492a9e9aSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
49508367be1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
49608367be1SNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
497ff0c13d6SNikolai Kondrashov 	{ }
498ff0c13d6SNikolai Kondrashov };
499ff0c13d6SNikolai Kondrashov MODULE_DEVICE_TABLE(hid, uclogic_devices);
500ff0c13d6SNikolai Kondrashov 
501ff0c13d6SNikolai Kondrashov static struct hid_driver uclogic_driver = {
502ff0c13d6SNikolai Kondrashov 	.name = "uclogic",
503ff0c13d6SNikolai Kondrashov 	.id_table = uclogic_devices,
504ff0c13d6SNikolai Kondrashov 	.probe = uclogic_probe,
5059614219eSNikolai Kondrashov 	.remove = uclogic_remove,
506ff0c13d6SNikolai Kondrashov 	.report_fixup = uclogic_report_fixup,
507ff0c13d6SNikolai Kondrashov 	.raw_event = uclogic_raw_event,
508ff0c13d6SNikolai Kondrashov 	.input_configured = uclogic_input_configured,
509251b4275SNikolai Kondrashov #ifdef CONFIG_PM
510251b4275SNikolai Kondrashov 	.resume	          = uclogic_resume,
511251b4275SNikolai Kondrashov 	.reset_resume     = uclogic_resume,
512251b4275SNikolai Kondrashov #endif
513ff0c13d6SNikolai Kondrashov };
514ff0c13d6SNikolai Kondrashov module_hid_driver(uclogic_driver);
515ff0c13d6SNikolai Kondrashov 
516ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Martin Rusko");
517ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Nikolai Kondrashov");
518ff0c13d6SNikolai Kondrashov MODULE_LICENSE("GPL");
519