xref: /linux/drivers/hid/hid-uclogic-core.c (revision 3405a4beaaa852f3ed2a5eb3b5149932d5c3779b)
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 
84f5927973SNikolai Kondrashov static int uclogic_input_mapping(struct hid_device *hdev,
85f5927973SNikolai Kondrashov 				 struct hid_input *hi,
86f5927973SNikolai Kondrashov 				 struct hid_field *field,
87f5927973SNikolai Kondrashov 				 struct hid_usage *usage,
88f5927973SNikolai Kondrashov 				 unsigned long **bit,
89f5927973SNikolai Kondrashov 				 int *max)
90f5927973SNikolai Kondrashov {
91f5927973SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
92f5927973SNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
93f5927973SNikolai Kondrashov 
94f5927973SNikolai Kondrashov 	/* Discard invalid pen usages */
95f5927973SNikolai Kondrashov 	if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
96f5927973SNikolai Kondrashov 		return -1;
97f5927973SNikolai Kondrashov 
98f5927973SNikolai Kondrashov 	/* Let hid-core decide what to do */
99f5927973SNikolai Kondrashov 	return 0;
100f5927973SNikolai Kondrashov }
101f5927973SNikolai Kondrashov 
102ff0c13d6SNikolai Kondrashov static int uclogic_input_configured(struct hid_device *hdev,
103ff0c13d6SNikolai Kondrashov 		struct hid_input *hi)
104ff0c13d6SNikolai Kondrashov {
10501309e29SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
10601309e29SNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
107ff0c13d6SNikolai Kondrashov 	char *name;
108ff0c13d6SNikolai Kondrashov 	const char *suffix = NULL;
109ff0c13d6SNikolai Kondrashov 	struct hid_field *field;
110ff0c13d6SNikolai Kondrashov 	size_t len;
111d170e8e0SNikolai Kondrashov 	size_t i;
112d170e8e0SNikolai Kondrashov 	const struct uclogic_params_frame *frame;
113ff0c13d6SNikolai Kondrashov 
114ff0c13d6SNikolai Kondrashov 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
115ff0c13d6SNikolai Kondrashov 	if (!hi->report)
116ff0c13d6SNikolai Kondrashov 		return 0;
117ff0c13d6SNikolai Kondrashov 
11801309e29SNikolai Kondrashov 	/*
11901309e29SNikolai Kondrashov 	 * If this is the input corresponding to the pen report
12001309e29SNikolai Kondrashov 	 * in need of tweaking.
12101309e29SNikolai Kondrashov 	 */
12201309e29SNikolai Kondrashov 	if (hi->report->id == params->pen.id) {
12301309e29SNikolai Kondrashov 		/* Remember the input device so we can simulate events */
12401309e29SNikolai Kondrashov 		drvdata->pen_input = hi->input;
12501309e29SNikolai Kondrashov 	}
12601309e29SNikolai Kondrashov 
127d170e8e0SNikolai Kondrashov 	/* If it's one of the frame devices */
128d170e8e0SNikolai Kondrashov 	for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
129d170e8e0SNikolai Kondrashov 		frame = &params->frame_list[i];
130d170e8e0SNikolai Kondrashov 		if (hi->report->id == frame->id) {
131ee007036SNikolai Kondrashov 			/* Assign custom suffix, if any */
132ee007036SNikolai Kondrashov 			suffix = frame->suffix;
133d170e8e0SNikolai Kondrashov 			/*
134d170e8e0SNikolai Kondrashov 			 * Disable EV_MSC reports for touch ring interfaces to
135d170e8e0SNikolai Kondrashov 			 * make the Wacom driver pickup touch ring extents
136d170e8e0SNikolai Kondrashov 			 */
137caf7e934SNikolai Kondrashov 			if (frame->touch_byte > 0)
138d170e8e0SNikolai Kondrashov 				__clear_bit(EV_MSC, hi->input->evbit);
139d170e8e0SNikolai Kondrashov 		}
140d170e8e0SNikolai Kondrashov 	}
141d170e8e0SNikolai Kondrashov 
142ee007036SNikolai Kondrashov 	if (!suffix) {
143ff0c13d6SNikolai Kondrashov 		field = hi->report->field[0];
144ff0c13d6SNikolai Kondrashov 
145ff0c13d6SNikolai Kondrashov 		switch (field->application) {
146ff0c13d6SNikolai Kondrashov 		case HID_GD_KEYBOARD:
147ff0c13d6SNikolai Kondrashov 			suffix = "Keyboard";
148ff0c13d6SNikolai Kondrashov 			break;
149ff0c13d6SNikolai Kondrashov 		case HID_GD_MOUSE:
150ff0c13d6SNikolai Kondrashov 			suffix = "Mouse";
151ff0c13d6SNikolai Kondrashov 			break;
152ff0c13d6SNikolai Kondrashov 		case HID_GD_KEYPAD:
153ff0c13d6SNikolai Kondrashov 			suffix = "Pad";
154ff0c13d6SNikolai Kondrashov 			break;
155ff0c13d6SNikolai Kondrashov 		case HID_DG_PEN:
1560977fda0SJosé Expósito 		case HID_DG_DIGITIZER:
157ff0c13d6SNikolai Kondrashov 			suffix = "Pen";
158ff0c13d6SNikolai Kondrashov 			break;
159ff0c13d6SNikolai Kondrashov 		case HID_CP_CONSUMER_CONTROL:
160ff0c13d6SNikolai Kondrashov 			suffix = "Consumer Control";
161ff0c13d6SNikolai Kondrashov 			break;
162ff0c13d6SNikolai Kondrashov 		case HID_GD_SYSTEM_CONTROL:
163ff0c13d6SNikolai Kondrashov 			suffix = "System Control";
164ff0c13d6SNikolai Kondrashov 			break;
165ff0c13d6SNikolai Kondrashov 		}
166ee007036SNikolai Kondrashov 	}
167ff0c13d6SNikolai Kondrashov 
168ff0c13d6SNikolai Kondrashov 	if (suffix) {
169ff0c13d6SNikolai Kondrashov 		len = strlen(hdev->name) + 2 + strlen(suffix);
170ff0c13d6SNikolai Kondrashov 		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
171ff0c13d6SNikolai Kondrashov 		if (name) {
172ff0c13d6SNikolai Kondrashov 			snprintf(name, len, "%s %s", hdev->name, suffix);
173ff0c13d6SNikolai Kondrashov 			hi->input->name = name;
174ff0c13d6SNikolai Kondrashov 		}
175ff0c13d6SNikolai Kondrashov 	}
176ff0c13d6SNikolai Kondrashov 
177ff0c13d6SNikolai Kondrashov 	return 0;
178ff0c13d6SNikolai Kondrashov }
179ff0c13d6SNikolai Kondrashov 
180ff0c13d6SNikolai Kondrashov static int uclogic_probe(struct hid_device *hdev,
181ff0c13d6SNikolai Kondrashov 		const struct hid_device_id *id)
182ff0c13d6SNikolai Kondrashov {
183ff0c13d6SNikolai Kondrashov 	int rc;
1849614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = NULL;
1859614219eSNikolai Kondrashov 	bool params_initialized = false;
186ff0c13d6SNikolai Kondrashov 
18793020953SGreg Kroah-Hartman 	if (!hid_is_usb(hdev))
18893020953SGreg Kroah-Hartman 		return -EINVAL;
18993020953SGreg Kroah-Hartman 
190ff0c13d6SNikolai Kondrashov 	/*
191ff0c13d6SNikolai Kondrashov 	 * libinput requires the pad interface to be on a different node
192ff0c13d6SNikolai Kondrashov 	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
193ff0c13d6SNikolai Kondrashov 	 */
194ff0c13d6SNikolai Kondrashov 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
195*3405a4beSJosé Expósito 	hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
196ff0c13d6SNikolai Kondrashov 
197ff0c13d6SNikolai Kondrashov 	/* Allocate and assign driver data */
198ff0c13d6SNikolai Kondrashov 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1999614219eSNikolai Kondrashov 	if (drvdata == NULL) {
2009614219eSNikolai Kondrashov 		rc = -ENOMEM;
2019614219eSNikolai Kondrashov 		goto failure;
2029614219eSNikolai Kondrashov 	}
20301309e29SNikolai Kondrashov 	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
2048a47670cSNikolai Kondrashov 	drvdata->re_state = U8_MAX;
205ff0c13d6SNikolai Kondrashov 	hid_set_drvdata(hdev, drvdata);
206ff0c13d6SNikolai Kondrashov 
2079614219eSNikolai Kondrashov 	/* Initialize the device and retrieve interface parameters */
2089614219eSNikolai Kondrashov 	rc = uclogic_params_init(&drvdata->params, hdev);
2099614219eSNikolai Kondrashov 	if (rc != 0) {
2109614219eSNikolai Kondrashov 		hid_err(hdev, "failed probing parameters: %d\n", rc);
2119614219eSNikolai Kondrashov 		goto failure;
212ff0c13d6SNikolai Kondrashov 	}
2139614219eSNikolai Kondrashov 	params_initialized = true;
214a228809fSNikolai Kondrashov 	hid_dbg(hdev, "parameters:\n");
215a228809fSNikolai Kondrashov 	uclogic_params_hid_dbg(hdev, &drvdata->params);
2169614219eSNikolai Kondrashov 	if (drvdata->params.invalid) {
2179614219eSNikolai Kondrashov 		hid_info(hdev, "interface is invalid, ignoring\n");
2189614219eSNikolai Kondrashov 		rc = -ENODEV;
2199614219eSNikolai Kondrashov 		goto failure;
2209614219eSNikolai Kondrashov 	}
221ff0c13d6SNikolai Kondrashov 
2229614219eSNikolai Kondrashov 	/* Generate replacement report descriptor */
2239614219eSNikolai Kondrashov 	rc = uclogic_params_get_desc(&drvdata->params,
2249614219eSNikolai Kondrashov 				     &drvdata->desc_ptr,
2259614219eSNikolai Kondrashov 				     &drvdata->desc_size);
226ff0c13d6SNikolai Kondrashov 	if (rc) {
2279614219eSNikolai Kondrashov 		hid_err(hdev,
2289614219eSNikolai Kondrashov 			"failed generating replacement report descriptor: %d\n",
2299614219eSNikolai Kondrashov 			rc);
2309614219eSNikolai Kondrashov 		goto failure;
231ff0c13d6SNikolai Kondrashov 	}
232ff0c13d6SNikolai Kondrashov 
233ff0c13d6SNikolai Kondrashov 	rc = hid_parse(hdev);
234ff0c13d6SNikolai Kondrashov 	if (rc) {
235ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "parse failed\n");
2369614219eSNikolai Kondrashov 		goto failure;
237ff0c13d6SNikolai Kondrashov 	}
238ff0c13d6SNikolai Kondrashov 
239ff0c13d6SNikolai Kondrashov 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
240ff0c13d6SNikolai Kondrashov 	if (rc) {
241ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "hw start failed\n");
2429614219eSNikolai Kondrashov 		goto failure;
2439614219eSNikolai Kondrashov 	}
2449614219eSNikolai Kondrashov 
2459614219eSNikolai Kondrashov 	return 0;
2469614219eSNikolai Kondrashov failure:
2479614219eSNikolai Kondrashov 	/* Assume "remove" might not be called if "probe" failed */
2489614219eSNikolai Kondrashov 	if (params_initialized)
2499614219eSNikolai Kondrashov 		uclogic_params_cleanup(&drvdata->params);
250ff0c13d6SNikolai Kondrashov 	return rc;
251ff0c13d6SNikolai Kondrashov }
252ff0c13d6SNikolai Kondrashov 
253251b4275SNikolai Kondrashov #ifdef CONFIG_PM
254251b4275SNikolai Kondrashov static int uclogic_resume(struct hid_device *hdev)
255251b4275SNikolai Kondrashov {
256251b4275SNikolai Kondrashov 	int rc;
257251b4275SNikolai Kondrashov 	struct uclogic_params params;
258251b4275SNikolai Kondrashov 
259251b4275SNikolai Kondrashov 	/* Re-initialize the device, but discard parameters */
260251b4275SNikolai Kondrashov 	rc = uclogic_params_init(&params, hdev);
261251b4275SNikolai Kondrashov 	if (rc != 0)
262251b4275SNikolai Kondrashov 		hid_err(hdev, "failed to re-initialize the device\n");
263251b4275SNikolai Kondrashov 	else
264251b4275SNikolai Kondrashov 		uclogic_params_cleanup(&params);
265251b4275SNikolai Kondrashov 
266251b4275SNikolai Kondrashov 	return rc;
267251b4275SNikolai Kondrashov }
268251b4275SNikolai Kondrashov #endif
269251b4275SNikolai Kondrashov 
2707e418667SNikolai Kondrashov /**
2717e418667SNikolai Kondrashov  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
2727e418667SNikolai Kondrashov  *
2737e418667SNikolai Kondrashov  * @drvdata:	Driver data.
2747e418667SNikolai Kondrashov  * @data:	Report data buffer, can be modified.
2757e418667SNikolai Kondrashov  * @size:	Report data size, bytes.
2767e418667SNikolai Kondrashov  *
2777e418667SNikolai Kondrashov  * Returns:
2787e418667SNikolai Kondrashov  *	Negative value on error (stops event delivery), zero for success.
2797e418667SNikolai Kondrashov  */
2807e418667SNikolai Kondrashov static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
281ff0c13d6SNikolai Kondrashov 					u8 *data, int size)
282ff0c13d6SNikolai Kondrashov {
2837f12dd24SNikolai Kondrashov 	struct uclogic_params_pen *pen = &drvdata->params.pen;
284ff0c13d6SNikolai Kondrashov 
2857e418667SNikolai Kondrashov 	WARN_ON(drvdata == NULL);
2867e418667SNikolai Kondrashov 	WARN_ON(data == NULL && size != 0);
2877e418667SNikolai Kondrashov 
2889614219eSNikolai Kondrashov 	/* If in-range reports are inverted */
2897f12dd24SNikolai Kondrashov 	if (pen->inrange ==
2909614219eSNikolai Kondrashov 		UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
291ff0c13d6SNikolai Kondrashov 		/* Invert the in-range bit */
292ff0c13d6SNikolai Kondrashov 		data[1] ^= 0x40;
293ff0c13d6SNikolai Kondrashov 	}
29459f2e0fcSNikolai Kondrashov 	/*
29559f2e0fcSNikolai Kondrashov 	 * If report contains fragmented high-resolution pen
29659f2e0fcSNikolai Kondrashov 	 * coordinates
29759f2e0fcSNikolai Kondrashov 	 */
2987f12dd24SNikolai Kondrashov 	if (size >= 10 && pen->fragmented_hires) {
29959f2e0fcSNikolai Kondrashov 		u8 pressure_low_byte;
30059f2e0fcSNikolai Kondrashov 		u8 pressure_high_byte;
30159f2e0fcSNikolai Kondrashov 
30259f2e0fcSNikolai Kondrashov 		/* Lift pressure bytes */
30359f2e0fcSNikolai Kondrashov 		pressure_low_byte = data[6];
30459f2e0fcSNikolai Kondrashov 		pressure_high_byte = data[7];
30559f2e0fcSNikolai Kondrashov 		/*
30659f2e0fcSNikolai Kondrashov 		 * Move Y coord to make space for high-order X
30759f2e0fcSNikolai Kondrashov 		 * coord byte
30859f2e0fcSNikolai Kondrashov 		 */
30959f2e0fcSNikolai Kondrashov 		data[6] = data[5];
31059f2e0fcSNikolai Kondrashov 		data[5] = data[4];
31159f2e0fcSNikolai Kondrashov 		/* Move high-order X coord byte */
31259f2e0fcSNikolai Kondrashov 		data[4] = data[8];
31359f2e0fcSNikolai Kondrashov 		/* Move high-order Y coord byte */
31459f2e0fcSNikolai Kondrashov 		data[7] = data[9];
31559f2e0fcSNikolai Kondrashov 		/* Place pressure bytes */
31659f2e0fcSNikolai Kondrashov 		data[8] = pressure_low_byte;
31759f2e0fcSNikolai Kondrashov 		data[9] = pressure_high_byte;
31859f2e0fcSNikolai Kondrashov 	}
31901309e29SNikolai Kondrashov 	/* If we need to emulate in-range detection */
3207f12dd24SNikolai Kondrashov 	if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
32101309e29SNikolai Kondrashov 		/* Set in-range bit */
32201309e29SNikolai Kondrashov 		data[1] |= 0x40;
32301309e29SNikolai Kondrashov 		/* (Re-)start in-range timeout */
32401309e29SNikolai Kondrashov 		mod_timer(&drvdata->inrange_timer,
32501309e29SNikolai Kondrashov 				jiffies + msecs_to_jiffies(100));
32601309e29SNikolai Kondrashov 	}
3271324c5acSNikolai Kondrashov 	/* If we report tilt and Y direction is flipped */
3287f12dd24SNikolai Kondrashov 	if (size >= 12 && pen->tilt_y_flipped)
3291324c5acSNikolai Kondrashov 		data[11] = -data[11];
3307e418667SNikolai Kondrashov 
3317e418667SNikolai Kondrashov 	return 0;
3329614219eSNikolai Kondrashov }
333ff0c13d6SNikolai Kondrashov 
3347e418667SNikolai Kondrashov /**
3357e418667SNikolai Kondrashov  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
3367e418667SNikolai Kondrashov  *
3377e418667SNikolai Kondrashov  * @drvdata:	Driver data.
338337fa051SNikolai Kondrashov  * @frame:	The parameters of the frame controls to handle.
3397e418667SNikolai Kondrashov  * @data:	Report data buffer, can be modified.
3407e418667SNikolai Kondrashov  * @size:	Report data size, bytes.
3417e418667SNikolai Kondrashov  *
3427e418667SNikolai Kondrashov  * Returns:
3437e418667SNikolai Kondrashov  *	Negative value on error (stops event delivery), zero for success.
3447e418667SNikolai Kondrashov  */
345337fa051SNikolai Kondrashov static int uclogic_raw_event_frame(
346337fa051SNikolai Kondrashov 		struct uclogic_drvdata *drvdata,
347337fa051SNikolai Kondrashov 		const struct uclogic_params_frame *frame,
3487e418667SNikolai Kondrashov 		u8 *data, int size)
3497e418667SNikolai Kondrashov {
3507e418667SNikolai Kondrashov 	WARN_ON(drvdata == NULL);
3517e418667SNikolai Kondrashov 	WARN_ON(data == NULL && size != 0);
3527e418667SNikolai Kondrashov 
353fde44ac5SNikolai Kondrashov 	/* If need to, and can, set pad device ID for Wacom drivers */
3547f12dd24SNikolai Kondrashov 	if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
355d170e8e0SNikolai Kondrashov 		/* If we also have a touch ring and the finger left it */
356caf7e934SNikolai Kondrashov 		if (frame->touch_byte > 0 && frame->touch_byte < size &&
357caf7e934SNikolai Kondrashov 		    data[frame->touch_byte] == 0) {
358d170e8e0SNikolai Kondrashov 			data[frame->dev_id_byte] = 0;
359d170e8e0SNikolai Kondrashov 		} else {
3607f12dd24SNikolai Kondrashov 			data[frame->dev_id_byte] = 0xf;
361fde44ac5SNikolai Kondrashov 		}
362d170e8e0SNikolai Kondrashov 	}
363d170e8e0SNikolai Kondrashov 
3648a47670cSNikolai Kondrashov 	/* If need to, and can, read rotary encoder state change */
3657f12dd24SNikolai Kondrashov 	if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
3667f12dd24SNikolai Kondrashov 		unsigned int byte = frame->re_lsb / 8;
3677f12dd24SNikolai Kondrashov 		unsigned int bit = frame->re_lsb % 8;
3688a47670cSNikolai Kondrashov 
3698a47670cSNikolai Kondrashov 		u8 change;
3708a47670cSNikolai Kondrashov 		u8 prev_state = drvdata->re_state;
3718a47670cSNikolai Kondrashov 		/* Read Gray-coded state */
3728a47670cSNikolai Kondrashov 		u8 state = (data[byte] >> bit) & 0x3;
3738a47670cSNikolai Kondrashov 		/* Encode state change into 2-bit signed integer */
3748a47670cSNikolai Kondrashov 		if ((prev_state == 1 && state == 0) ||
3758a47670cSNikolai Kondrashov 		    (prev_state == 2 && state == 3)) {
3768a47670cSNikolai Kondrashov 			change = 1;
3778a47670cSNikolai Kondrashov 		} else if ((prev_state == 2 && state == 0) ||
3788a47670cSNikolai Kondrashov 			   (prev_state == 1 && state == 3)) {
3798a47670cSNikolai Kondrashov 			change = 3;
3808a47670cSNikolai Kondrashov 		} else {
3818a47670cSNikolai Kondrashov 			change = 0;
3828a47670cSNikolai Kondrashov 		}
3838a47670cSNikolai Kondrashov 		/* Write change */
3848a47670cSNikolai Kondrashov 		data[byte] = (data[byte] & ~((u8)3 << bit)) |
3858a47670cSNikolai Kondrashov 				(change << bit);
3868a47670cSNikolai Kondrashov 		/* Remember state */
3878a47670cSNikolai Kondrashov 		drvdata->re_state = state;
3888a47670cSNikolai Kondrashov 	}
3897e418667SNikolai Kondrashov 
390d170e8e0SNikolai Kondrashov 	/* If need to, and can, transform the touch ring reports */
391fbc08b4eSNikolai Kondrashov 	if (frame->touch_byte > 0 && frame->touch_byte < size) {
392caf7e934SNikolai Kondrashov 		__s8 value = data[frame->touch_byte];
393d170e8e0SNikolai Kondrashov 
394fbc08b4eSNikolai Kondrashov 		if (value != 0) {
395fbc08b4eSNikolai Kondrashov 			if (frame->touch_flip_at != 0) {
396fbc08b4eSNikolai Kondrashov 				value = frame->touch_flip_at - value;
397fbc08b4eSNikolai Kondrashov 				if (value <= 0)
398fbc08b4eSNikolai Kondrashov 					value = frame->touch_max + value;
399fbc08b4eSNikolai Kondrashov 			}
400fbc08b4eSNikolai Kondrashov 			data[frame->touch_byte] = value - 1;
401d170e8e0SNikolai Kondrashov 		}
402d170e8e0SNikolai Kondrashov 	}
403d170e8e0SNikolai Kondrashov 
4042112b49eSNikolai Kondrashov 	/* If need to, and can, transform the bitmap dial reports */
4052112b49eSNikolai Kondrashov 	if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
4062112b49eSNikolai Kondrashov 		if (data[frame->bitmap_dial_byte] == 2)
4072112b49eSNikolai Kondrashov 			data[frame->bitmap_dial_byte] = -1;
4082112b49eSNikolai Kondrashov 	}
4092112b49eSNikolai Kondrashov 
4107e418667SNikolai Kondrashov 	return 0;
411fde44ac5SNikolai Kondrashov }
412fde44ac5SNikolai Kondrashov 
4137e418667SNikolai Kondrashov static int uclogic_raw_event(struct hid_device *hdev,
4147e418667SNikolai Kondrashov 				struct hid_report *report,
4157e418667SNikolai Kondrashov 				u8 *data, int size)
4167e418667SNikolai Kondrashov {
417044fa816SNikolai Kondrashov 	unsigned int report_id = report->id;
4187e418667SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
4197e418667SNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
4208b013098SNikolai Kondrashov 	struct uclogic_params_pen_subreport *subreport;
4218b013098SNikolai Kondrashov 	struct uclogic_params_pen_subreport *subreport_list_end;
422337fa051SNikolai Kondrashov 	size_t i;
4237e418667SNikolai Kondrashov 
424fea53b9fSNikolai Kondrashov 	/* Do not handle anything but input reports */
425fea53b9fSNikolai Kondrashov 	if (report->type != HID_INPUT_REPORT)
426fea53b9fSNikolai Kondrashov 		return 0;
427fea53b9fSNikolai Kondrashov 
4288b013098SNikolai Kondrashov 	while (true) {
4297e418667SNikolai Kondrashov 		/* Tweak pen reports, if necessary */
430606dadc1SNikolai Kondrashov 		if ((report_id == params->pen.id) && (size >= 2)) {
4318b013098SNikolai Kondrashov 			subreport_list_end =
4328b013098SNikolai Kondrashov 				params->pen.subreport_list +
4338b013098SNikolai Kondrashov 				ARRAY_SIZE(params->pen.subreport_list);
4348b013098SNikolai Kondrashov 			/* Try to match a subreport */
4358b013098SNikolai Kondrashov 			for (subreport = params->pen.subreport_list;
436e6be956fSNikolai Kondrashov 			     subreport < subreport_list_end; subreport++) {
437e6be956fSNikolai Kondrashov 				if (subreport->value != 0 &&
438e6be956fSNikolai Kondrashov 				    subreport->value == data[1]) {
439e6be956fSNikolai Kondrashov 					break;
440e6be956fSNikolai Kondrashov 				}
441e6be956fSNikolai Kondrashov 			}
4428b013098SNikolai Kondrashov 			/* If a subreport matched */
4438b013098SNikolai Kondrashov 			if (subreport < subreport_list_end) {
4448b013098SNikolai Kondrashov 				/* Change to subreport ID, and restart */
4458b013098SNikolai Kondrashov 				report_id = data[0] = subreport->id;
4468b013098SNikolai Kondrashov 				continue;
447044fa816SNikolai Kondrashov 			} else {
4487e418667SNikolai Kondrashov 				return uclogic_raw_event_pen(drvdata, data, size);
4497e418667SNikolai Kondrashov 			}
450044fa816SNikolai Kondrashov 		}
4517e418667SNikolai Kondrashov 
4527e418667SNikolai Kondrashov 		/* Tweak frame control reports, if necessary */
453337fa051SNikolai Kondrashov 		for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
454337fa051SNikolai Kondrashov 			if (report_id == params->frame_list[i].id) {
455337fa051SNikolai Kondrashov 				return uclogic_raw_event_frame(
456337fa051SNikolai Kondrashov 					drvdata, &params->frame_list[i],
457337fa051SNikolai Kondrashov 					data, size);
458337fa051SNikolai Kondrashov 			}
459337fa051SNikolai Kondrashov 		}
4607e418667SNikolai Kondrashov 
4618b013098SNikolai Kondrashov 		break;
4628b013098SNikolai Kondrashov 	}
4638b013098SNikolai Kondrashov 
464ff0c13d6SNikolai Kondrashov 	return 0;
465ff0c13d6SNikolai Kondrashov }
466ff0c13d6SNikolai Kondrashov 
4679614219eSNikolai Kondrashov static void uclogic_remove(struct hid_device *hdev)
4689614219eSNikolai Kondrashov {
4699614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
4709614219eSNikolai Kondrashov 
47101309e29SNikolai Kondrashov 	del_timer_sync(&drvdata->inrange_timer);
4729614219eSNikolai Kondrashov 	hid_hw_stop(hdev);
4739614219eSNikolai Kondrashov 	kfree(drvdata->desc_ptr);
4749614219eSNikolai Kondrashov 	uclogic_params_cleanup(&drvdata->params);
4759614219eSNikolai Kondrashov }
4769614219eSNikolai Kondrashov 
477ff0c13d6SNikolai Kondrashov static const struct hid_device_id uclogic_devices[] = {
478ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
479ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
480ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
481ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
482ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
483ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
484ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
485ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
486ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
487ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
488ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
489ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
490ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
491ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
4929614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
4939614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
494315ffcc9SKyle Godbey 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
49585e86071SNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET2) },
496f7271b2aSCristian Klein 	{ HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
497f7271b2aSCristian Klein 				USB_DEVICE_ID_TRUST_PANORA_TABLET) },
4989614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
4999614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
5009614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5019614219eSNikolai Kondrashov 				USB_DEVICE_ID_YIYNOVA_TABLET) },
5029614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5039614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
5049614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5059614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
5069614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5070c15efe9SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
5080c15efe9SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
5099614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
5109614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
5119614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
512022fc531SMartijn van de Streek 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
513022fc531SMartijn van de Streek 				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
5149614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5157495fb7eSJosé Expósito 				USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
5167495fb7eSJosé Expósito 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
517e902ed93SNikolai Kondrashov 				USB_DEVICE_ID_UGEE_TABLET_G5) },
518e902ed93SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5199614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
520c3e5a67cSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
52188bb346dSWang Xuerui 				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
52288bb346dSWang Xuerui 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
523c3e5a67cSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
524492a9e9aSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
525492a9e9aSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
52608367be1SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
52708367be1SNikolai Kondrashov 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
52861b1db5aSRoman Romanenko 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
5290cb1fc09SJosé Expósito 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
5300cb1fc09SJosé Expósito 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
53193b40b3eSJosé Expósito 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
53293b40b3eSJosé Expósito 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
53361b1db5aSRoman Romanenko 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
534ff0c13d6SNikolai Kondrashov 	{ }
535ff0c13d6SNikolai Kondrashov };
536ff0c13d6SNikolai Kondrashov MODULE_DEVICE_TABLE(hid, uclogic_devices);
537ff0c13d6SNikolai Kondrashov 
538ff0c13d6SNikolai Kondrashov static struct hid_driver uclogic_driver = {
539ff0c13d6SNikolai Kondrashov 	.name = "uclogic",
540ff0c13d6SNikolai Kondrashov 	.id_table = uclogic_devices,
541ff0c13d6SNikolai Kondrashov 	.probe = uclogic_probe,
5429614219eSNikolai Kondrashov 	.remove = uclogic_remove,
543ff0c13d6SNikolai Kondrashov 	.report_fixup = uclogic_report_fixup,
544ff0c13d6SNikolai Kondrashov 	.raw_event = uclogic_raw_event,
545f5927973SNikolai Kondrashov 	.input_mapping = uclogic_input_mapping,
546ff0c13d6SNikolai Kondrashov 	.input_configured = uclogic_input_configured,
547251b4275SNikolai Kondrashov #ifdef CONFIG_PM
548251b4275SNikolai Kondrashov 	.resume	          = uclogic_resume,
549251b4275SNikolai Kondrashov 	.reset_resume     = uclogic_resume,
550251b4275SNikolai Kondrashov #endif
551ff0c13d6SNikolai Kondrashov };
552ff0c13d6SNikolai Kondrashov module_hid_driver(uclogic_driver);
553ff0c13d6SNikolai Kondrashov 
554ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Martin Rusko");
555ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Nikolai Kondrashov");
556ff0c13d6SNikolai Kondrashov MODULE_LICENSE("GPL");
557