xref: /linux/drivers/hid/hid-uclogic-core.c (revision 9614219e9310ef19e66719bf37f9f68919bac08e)
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>
19ff0c13d6SNikolai Kondrashov #include "usbhid/usbhid.h"
20*9614219eSNikolai Kondrashov #include "hid-uclogic-params.h"
21ff0c13d6SNikolai Kondrashov 
22ff0c13d6SNikolai Kondrashov #include "hid-ids.h"
23ff0c13d6SNikolai Kondrashov 
24ff0c13d6SNikolai Kondrashov /* Driver data */
25ff0c13d6SNikolai Kondrashov struct uclogic_drvdata {
26*9614219eSNikolai Kondrashov 	/* Interface parameters */
27*9614219eSNikolai Kondrashov 	struct uclogic_params params;
28*9614219eSNikolai Kondrashov 	/* Pointer to the replacement report descriptor. NULL if none. */
29*9614219eSNikolai Kondrashov 	__u8 *desc_ptr;
30*9614219eSNikolai Kondrashov 	/*
31*9614219eSNikolai Kondrashov 	 * Size of the replacement report descriptor.
32*9614219eSNikolai Kondrashov 	 * Only valid if desc_ptr is not NULL
33*9614219eSNikolai Kondrashov 	 */
34*9614219eSNikolai Kondrashov 	unsigned int desc_size;
35ff0c13d6SNikolai Kondrashov };
36ff0c13d6SNikolai Kondrashov 
37ff0c13d6SNikolai Kondrashov static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
38ff0c13d6SNikolai Kondrashov 					unsigned int *rsize)
39ff0c13d6SNikolai Kondrashov {
40ff0c13d6SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
41ff0c13d6SNikolai Kondrashov 
42*9614219eSNikolai Kondrashov 	if (drvdata->desc_ptr != NULL) {
43*9614219eSNikolai Kondrashov 		rdesc = drvdata->desc_ptr;
44*9614219eSNikolai Kondrashov 		*rsize = drvdata->desc_size;
45*9614219eSNikolai Kondrashov 	}
46ff0c13d6SNikolai Kondrashov 	return rdesc;
47ff0c13d6SNikolai Kondrashov }
48ff0c13d6SNikolai Kondrashov 
49*9614219eSNikolai Kondrashov static int uclogic_input_mapping(struct hid_device *hdev,
50*9614219eSNikolai Kondrashov 				 struct hid_input *hi,
51*9614219eSNikolai Kondrashov 				 struct hid_field *field,
52*9614219eSNikolai Kondrashov 				 struct hid_usage *usage,
53*9614219eSNikolai Kondrashov 				 unsigned long **bit,
54*9614219eSNikolai Kondrashov 				 int *max)
55ff0c13d6SNikolai Kondrashov {
56ff0c13d6SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
57*9614219eSNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
58ff0c13d6SNikolai Kondrashov 
59ff0c13d6SNikolai Kondrashov 	/* discard the unused pen interface */
60*9614219eSNikolai Kondrashov 	if (params->pen_unused && (field->application == HID_DG_PEN))
61ff0c13d6SNikolai Kondrashov 		return -1;
62ff0c13d6SNikolai Kondrashov 
63ff0c13d6SNikolai Kondrashov 	/* let hid-core decide what to do */
64ff0c13d6SNikolai Kondrashov 	return 0;
65ff0c13d6SNikolai Kondrashov }
66ff0c13d6SNikolai Kondrashov 
67ff0c13d6SNikolai Kondrashov static int uclogic_input_configured(struct hid_device *hdev,
68ff0c13d6SNikolai Kondrashov 		struct hid_input *hi)
69ff0c13d6SNikolai Kondrashov {
70ff0c13d6SNikolai Kondrashov 	char *name;
71ff0c13d6SNikolai Kondrashov 	const char *suffix = NULL;
72ff0c13d6SNikolai Kondrashov 	struct hid_field *field;
73ff0c13d6SNikolai Kondrashov 	size_t len;
74ff0c13d6SNikolai Kondrashov 
75ff0c13d6SNikolai Kondrashov 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
76ff0c13d6SNikolai Kondrashov 	if (!hi->report)
77ff0c13d6SNikolai Kondrashov 		return 0;
78ff0c13d6SNikolai Kondrashov 
79ff0c13d6SNikolai Kondrashov 	field = hi->report->field[0];
80ff0c13d6SNikolai Kondrashov 
81ff0c13d6SNikolai Kondrashov 	switch (field->application) {
82ff0c13d6SNikolai Kondrashov 	case HID_GD_KEYBOARD:
83ff0c13d6SNikolai Kondrashov 		suffix = "Keyboard";
84ff0c13d6SNikolai Kondrashov 		break;
85ff0c13d6SNikolai Kondrashov 	case HID_GD_MOUSE:
86ff0c13d6SNikolai Kondrashov 		suffix = "Mouse";
87ff0c13d6SNikolai Kondrashov 		break;
88ff0c13d6SNikolai Kondrashov 	case HID_GD_KEYPAD:
89ff0c13d6SNikolai Kondrashov 		suffix = "Pad";
90ff0c13d6SNikolai Kondrashov 		break;
91ff0c13d6SNikolai Kondrashov 	case HID_DG_PEN:
92ff0c13d6SNikolai Kondrashov 		suffix = "Pen";
93ff0c13d6SNikolai Kondrashov 		break;
94ff0c13d6SNikolai Kondrashov 	case HID_CP_CONSUMER_CONTROL:
95ff0c13d6SNikolai Kondrashov 		suffix = "Consumer Control";
96ff0c13d6SNikolai Kondrashov 		break;
97ff0c13d6SNikolai Kondrashov 	case HID_GD_SYSTEM_CONTROL:
98ff0c13d6SNikolai Kondrashov 		suffix = "System Control";
99ff0c13d6SNikolai Kondrashov 		break;
100ff0c13d6SNikolai Kondrashov 	}
101ff0c13d6SNikolai Kondrashov 
102ff0c13d6SNikolai Kondrashov 	if (suffix) {
103ff0c13d6SNikolai Kondrashov 		len = strlen(hdev->name) + 2 + strlen(suffix);
104ff0c13d6SNikolai Kondrashov 		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
105ff0c13d6SNikolai Kondrashov 		if (name) {
106ff0c13d6SNikolai Kondrashov 			snprintf(name, len, "%s %s", hdev->name, suffix);
107ff0c13d6SNikolai Kondrashov 			hi->input->name = name;
108ff0c13d6SNikolai Kondrashov 		}
109ff0c13d6SNikolai Kondrashov 	}
110ff0c13d6SNikolai Kondrashov 
111ff0c13d6SNikolai Kondrashov 	return 0;
112ff0c13d6SNikolai Kondrashov }
113ff0c13d6SNikolai Kondrashov 
114ff0c13d6SNikolai Kondrashov static int uclogic_probe(struct hid_device *hdev,
115ff0c13d6SNikolai Kondrashov 		const struct hid_device_id *id)
116ff0c13d6SNikolai Kondrashov {
117ff0c13d6SNikolai Kondrashov 	int rc;
118*9614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = NULL;
119*9614219eSNikolai Kondrashov 	bool params_initialized = false;
120ff0c13d6SNikolai Kondrashov 
121ff0c13d6SNikolai Kondrashov 	/*
122ff0c13d6SNikolai Kondrashov 	 * libinput requires the pad interface to be on a different node
123ff0c13d6SNikolai Kondrashov 	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
124ff0c13d6SNikolai Kondrashov 	 */
125ff0c13d6SNikolai Kondrashov 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
126ff0c13d6SNikolai Kondrashov 
127ff0c13d6SNikolai Kondrashov 	/* Allocate and assign driver data */
128ff0c13d6SNikolai Kondrashov 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
129*9614219eSNikolai Kondrashov 	if (drvdata == NULL) {
130*9614219eSNikolai Kondrashov 		rc = -ENOMEM;
131*9614219eSNikolai Kondrashov 		goto failure;
132*9614219eSNikolai Kondrashov 	}
133ff0c13d6SNikolai Kondrashov 	hid_set_drvdata(hdev, drvdata);
134ff0c13d6SNikolai Kondrashov 
135*9614219eSNikolai Kondrashov 	/* Initialize the device and retrieve interface parameters */
136*9614219eSNikolai Kondrashov 	rc = uclogic_params_init(&drvdata->params, hdev);
137*9614219eSNikolai Kondrashov 	if (rc != 0) {
138*9614219eSNikolai Kondrashov 		hid_err(hdev, "failed probing parameters: %d\n", rc);
139*9614219eSNikolai Kondrashov 		goto failure;
140ff0c13d6SNikolai Kondrashov 	}
141*9614219eSNikolai Kondrashov 	params_initialized = true;
142*9614219eSNikolai Kondrashov 	hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
143*9614219eSNikolai Kondrashov 		UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
144*9614219eSNikolai Kondrashov 	if (drvdata->params.invalid) {
145*9614219eSNikolai Kondrashov 		hid_info(hdev, "interface is invalid, ignoring\n");
146*9614219eSNikolai Kondrashov 		rc = -ENODEV;
147*9614219eSNikolai Kondrashov 		goto failure;
148*9614219eSNikolai Kondrashov 	}
149ff0c13d6SNikolai Kondrashov 
150*9614219eSNikolai Kondrashov 	/* Generate replacement report descriptor */
151*9614219eSNikolai Kondrashov 	rc = uclogic_params_get_desc(&drvdata->params,
152*9614219eSNikolai Kondrashov 				     &drvdata->desc_ptr,
153*9614219eSNikolai Kondrashov 				     &drvdata->desc_size);
154ff0c13d6SNikolai Kondrashov 	if (rc) {
155*9614219eSNikolai Kondrashov 		hid_err(hdev,
156*9614219eSNikolai Kondrashov 			"failed generating replacement report descriptor: %d\n",
157*9614219eSNikolai Kondrashov 			rc);
158*9614219eSNikolai Kondrashov 		goto failure;
159ff0c13d6SNikolai Kondrashov 	}
160ff0c13d6SNikolai Kondrashov 
161ff0c13d6SNikolai Kondrashov 	rc = hid_parse(hdev);
162ff0c13d6SNikolai Kondrashov 	if (rc) {
163ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "parse failed\n");
164*9614219eSNikolai Kondrashov 		goto failure;
165ff0c13d6SNikolai Kondrashov 	}
166ff0c13d6SNikolai Kondrashov 
167ff0c13d6SNikolai Kondrashov 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
168ff0c13d6SNikolai Kondrashov 	if (rc) {
169ff0c13d6SNikolai Kondrashov 		hid_err(hdev, "hw start failed\n");
170*9614219eSNikolai Kondrashov 		goto failure;
171*9614219eSNikolai Kondrashov 	}
172*9614219eSNikolai Kondrashov 
173*9614219eSNikolai Kondrashov 	return 0;
174*9614219eSNikolai Kondrashov failure:
175*9614219eSNikolai Kondrashov 	/* Assume "remove" might not be called if "probe" failed */
176*9614219eSNikolai Kondrashov 	if (params_initialized)
177*9614219eSNikolai Kondrashov 		uclogic_params_cleanup(&drvdata->params);
178ff0c13d6SNikolai Kondrashov 	return rc;
179ff0c13d6SNikolai Kondrashov }
180ff0c13d6SNikolai Kondrashov 
181*9614219eSNikolai Kondrashov static int uclogic_raw_event(struct hid_device *hdev,
182*9614219eSNikolai Kondrashov 				struct hid_report *report,
183ff0c13d6SNikolai Kondrashov 				u8 *data, int size)
184ff0c13d6SNikolai Kondrashov {
185ff0c13d6SNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
186*9614219eSNikolai Kondrashov 	struct uclogic_params *params = &drvdata->params;
187ff0c13d6SNikolai Kondrashov 
188*9614219eSNikolai Kondrashov 	/* Tweak pen reports, if necessary */
189*9614219eSNikolai Kondrashov 	if (!params->pen_unused &&
190*9614219eSNikolai Kondrashov 	    (report->type == HID_INPUT_REPORT) &&
191*9614219eSNikolai Kondrashov 	    (report->id == params->pen.id) &&
192ff0c13d6SNikolai Kondrashov 	    (size >= 2)) {
193*9614219eSNikolai Kondrashov 		/* If it's the "virtual" frame controls report */
194*9614219eSNikolai Kondrashov 		if (params->frame.id != 0 &&
195*9614219eSNikolai Kondrashov 		    data[1] & params->pen_frame_flag) {
196*9614219eSNikolai Kondrashov 			/* Change to virtual frame controls report ID */
197*9614219eSNikolai Kondrashov 			data[0] = params->frame.id;
198*9614219eSNikolai Kondrashov 			return 0;
199*9614219eSNikolai Kondrashov 		}
200*9614219eSNikolai Kondrashov 		/* If in-range reports are inverted */
201*9614219eSNikolai Kondrashov 		if (params->pen.inrange ==
202*9614219eSNikolai Kondrashov 			UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
203ff0c13d6SNikolai Kondrashov 			/* Invert the in-range bit */
204ff0c13d6SNikolai Kondrashov 			data[1] ^= 0x40;
205ff0c13d6SNikolai Kondrashov 		}
206*9614219eSNikolai Kondrashov 	}
207ff0c13d6SNikolai Kondrashov 
208ff0c13d6SNikolai Kondrashov 	return 0;
209ff0c13d6SNikolai Kondrashov }
210ff0c13d6SNikolai Kondrashov 
211*9614219eSNikolai Kondrashov static void uclogic_remove(struct hid_device *hdev)
212*9614219eSNikolai Kondrashov {
213*9614219eSNikolai Kondrashov 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
214*9614219eSNikolai Kondrashov 
215*9614219eSNikolai Kondrashov 	hid_hw_stop(hdev);
216*9614219eSNikolai Kondrashov 	kfree(drvdata->desc_ptr);
217*9614219eSNikolai Kondrashov 	uclogic_params_cleanup(&drvdata->params);
218*9614219eSNikolai Kondrashov }
219*9614219eSNikolai Kondrashov 
220ff0c13d6SNikolai Kondrashov static const struct hid_device_id uclogic_devices[] = {
221ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
222ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
223ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
224ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
225ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
226ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
227ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
228ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
229ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
230ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
231ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
232ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
233ff0c13d6SNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
234ff0c13d6SNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
235*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
236*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
237*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
238*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_HUION_TABLET) },
239*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
240*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_YIYNOVA_TABLET) },
241*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
242*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
243*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
244*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
245*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
246*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
247*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
248*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
249*9614219eSNikolai Kondrashov 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
250*9614219eSNikolai Kondrashov 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
251ff0c13d6SNikolai Kondrashov 	{ }
252ff0c13d6SNikolai Kondrashov };
253ff0c13d6SNikolai Kondrashov MODULE_DEVICE_TABLE(hid, uclogic_devices);
254ff0c13d6SNikolai Kondrashov 
255ff0c13d6SNikolai Kondrashov static struct hid_driver uclogic_driver = {
256ff0c13d6SNikolai Kondrashov 	.name = "uclogic",
257ff0c13d6SNikolai Kondrashov 	.id_table = uclogic_devices,
258ff0c13d6SNikolai Kondrashov 	.probe = uclogic_probe,
259*9614219eSNikolai Kondrashov 	.remove = uclogic_remove,
260ff0c13d6SNikolai Kondrashov 	.report_fixup = uclogic_report_fixup,
261ff0c13d6SNikolai Kondrashov 	.raw_event = uclogic_raw_event,
262ff0c13d6SNikolai Kondrashov 	.input_mapping = uclogic_input_mapping,
263ff0c13d6SNikolai Kondrashov 	.input_configured = uclogic_input_configured,
264ff0c13d6SNikolai Kondrashov };
265ff0c13d6SNikolai Kondrashov module_hid_driver(uclogic_driver);
266ff0c13d6SNikolai Kondrashov 
267ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Martin Rusko");
268ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Nikolai Kondrashov");
269ff0c13d6SNikolai Kondrashov MODULE_LICENSE("GPL");
270