xref: /linux/drivers/hid/hid-uclogic-core.c (revision 2112b49eaa5c5cffc22ddd8b3b09f7d20bf13819)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  HID driver for UC-Logic devices not fully compliant with HID standard
4  *
5  *  Copyright (c) 2010-2014 Nikolai Kondrashov
6  *  Copyright (c) 2013 Martin Rusko
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  */
15 
16 #include <linux/device.h>
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/timer.h>
20 #include "usbhid/usbhid.h"
21 #include "hid-uclogic-params.h"
22 
23 #include "hid-ids.h"
24 
25 /* Driver data */
26 struct uclogic_drvdata {
27 	/* Interface parameters */
28 	struct uclogic_params params;
29 	/* Pointer to the replacement report descriptor. NULL if none. */
30 	__u8 *desc_ptr;
31 	/*
32 	 * Size of the replacement report descriptor.
33 	 * Only valid if desc_ptr is not NULL
34 	 */
35 	unsigned int desc_size;
36 	/* Pen input device */
37 	struct input_dev *pen_input;
38 	/* In-range timer */
39 	struct timer_list inrange_timer;
40 	/* Last rotary encoder state, or U8_MAX for none */
41 	u8 re_state;
42 };
43 
44 /**
45  * uclogic_inrange_timeout - handle pen in-range state timeout.
46  * Emulate input events normally generated when pen goes out of range for
47  * tablets which don't report that.
48  *
49  * @t:	The timer the timeout handler is attached to, stored in a struct
50  *	uclogic_drvdata.
51  */
52 static void uclogic_inrange_timeout(struct timer_list *t)
53 {
54 	struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
55 							inrange_timer);
56 	struct input_dev *input = drvdata->pen_input;
57 
58 	if (input == NULL)
59 		return;
60 	input_report_abs(input, ABS_PRESSURE, 0);
61 	/* If BTN_TOUCH state is changing */
62 	if (test_bit(BTN_TOUCH, input->key)) {
63 		input_event(input, EV_MSC, MSC_SCAN,
64 				/* Digitizer Tip Switch usage */
65 				0xd0042);
66 		input_report_key(input, BTN_TOUCH, 0);
67 	}
68 	input_report_key(input, BTN_TOOL_PEN, 0);
69 	input_sync(input);
70 }
71 
72 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
73 					unsigned int *rsize)
74 {
75 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
76 
77 	if (drvdata->desc_ptr != NULL) {
78 		rdesc = drvdata->desc_ptr;
79 		*rsize = drvdata->desc_size;
80 	}
81 	return rdesc;
82 }
83 
84 static int uclogic_input_configured(struct hid_device *hdev,
85 		struct hid_input *hi)
86 {
87 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
88 	struct uclogic_params *params = &drvdata->params;
89 	char *name;
90 	const char *suffix = NULL;
91 	struct hid_field *field;
92 	size_t len;
93 	size_t i;
94 	const struct uclogic_params_frame *frame;
95 
96 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
97 	if (!hi->report)
98 		return 0;
99 
100 	/*
101 	 * If this is the input corresponding to the pen report
102 	 * in need of tweaking.
103 	 */
104 	if (hi->report->id == params->pen.id) {
105 		/* Remember the input device so we can simulate events */
106 		drvdata->pen_input = hi->input;
107 	}
108 
109 	/* If it's one of the frame devices */
110 	for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
111 		frame = &params->frame_list[i];
112 		if (hi->report->id == frame->id) {
113 			/* Assign custom suffix, if any */
114 			suffix = frame->suffix;
115 			/*
116 			 * Disable EV_MSC reports for touch ring interfaces to
117 			 * make the Wacom driver pickup touch ring extents
118 			 */
119 			if (frame->touch_ring_byte > 0)
120 				__clear_bit(EV_MSC, hi->input->evbit);
121 		}
122 	}
123 
124 	if (!suffix) {
125 		field = hi->report->field[0];
126 
127 		switch (field->application) {
128 		case HID_GD_KEYBOARD:
129 			suffix = "Keyboard";
130 			break;
131 		case HID_GD_MOUSE:
132 			suffix = "Mouse";
133 			break;
134 		case HID_GD_KEYPAD:
135 			suffix = "Pad";
136 			break;
137 		case HID_DG_PEN:
138 			suffix = "Pen";
139 			break;
140 		case HID_CP_CONSUMER_CONTROL:
141 			suffix = "Consumer Control";
142 			break;
143 		case HID_GD_SYSTEM_CONTROL:
144 			suffix = "System Control";
145 			break;
146 		}
147 	}
148 
149 	if (suffix) {
150 		len = strlen(hdev->name) + 2 + strlen(suffix);
151 		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
152 		if (name) {
153 			snprintf(name, len, "%s %s", hdev->name, suffix);
154 			hi->input->name = name;
155 		}
156 	}
157 
158 	return 0;
159 }
160 
161 static int uclogic_probe(struct hid_device *hdev,
162 		const struct hid_device_id *id)
163 {
164 	int rc;
165 	struct uclogic_drvdata *drvdata = NULL;
166 	bool params_initialized = false;
167 
168 	if (!hid_is_usb(hdev))
169 		return -EINVAL;
170 
171 	/*
172 	 * libinput requires the pad interface to be on a different node
173 	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
174 	 */
175 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
176 
177 	/* Allocate and assign driver data */
178 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
179 	if (drvdata == NULL) {
180 		rc = -ENOMEM;
181 		goto failure;
182 	}
183 	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
184 	drvdata->re_state = U8_MAX;
185 	hid_set_drvdata(hdev, drvdata);
186 
187 	/* Initialize the device and retrieve interface parameters */
188 	rc = uclogic_params_init(&drvdata->params, hdev);
189 	if (rc != 0) {
190 		hid_err(hdev, "failed probing parameters: %d\n", rc);
191 		goto failure;
192 	}
193 	params_initialized = true;
194 	hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
195 		UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
196 	if (drvdata->params.invalid) {
197 		hid_info(hdev, "interface is invalid, ignoring\n");
198 		rc = -ENODEV;
199 		goto failure;
200 	}
201 
202 	/* Generate replacement report descriptor */
203 	rc = uclogic_params_get_desc(&drvdata->params,
204 				     &drvdata->desc_ptr,
205 				     &drvdata->desc_size);
206 	if (rc) {
207 		hid_err(hdev,
208 			"failed generating replacement report descriptor: %d\n",
209 			rc);
210 		goto failure;
211 	}
212 
213 	rc = hid_parse(hdev);
214 	if (rc) {
215 		hid_err(hdev, "parse failed\n");
216 		goto failure;
217 	}
218 
219 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
220 	if (rc) {
221 		hid_err(hdev, "hw start failed\n");
222 		goto failure;
223 	}
224 
225 	return 0;
226 failure:
227 	/* Assume "remove" might not be called if "probe" failed */
228 	if (params_initialized)
229 		uclogic_params_cleanup(&drvdata->params);
230 	return rc;
231 }
232 
233 #ifdef CONFIG_PM
234 static int uclogic_resume(struct hid_device *hdev)
235 {
236 	int rc;
237 	struct uclogic_params params;
238 
239 	/* Re-initialize the device, but discard parameters */
240 	rc = uclogic_params_init(&params, hdev);
241 	if (rc != 0)
242 		hid_err(hdev, "failed to re-initialize the device\n");
243 	else
244 		uclogic_params_cleanup(&params);
245 
246 	return rc;
247 }
248 #endif
249 
250 /**
251  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
252  *
253  * @drvdata:	Driver data.
254  * @data:	Report data buffer, can be modified.
255  * @size:	Report data size, bytes.
256  *
257  * Returns:
258  *	Negative value on error (stops event delivery), zero for success.
259  */
260 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
261 					u8 *data, int size)
262 {
263 	struct uclogic_params_pen *pen = &drvdata->params.pen;
264 
265 	WARN_ON(drvdata == NULL);
266 	WARN_ON(data == NULL && size != 0);
267 
268 	/* If in-range reports are inverted */
269 	if (pen->inrange ==
270 		UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
271 		/* Invert the in-range bit */
272 		data[1] ^= 0x40;
273 	}
274 	/*
275 	 * If report contains fragmented high-resolution pen
276 	 * coordinates
277 	 */
278 	if (size >= 10 && pen->fragmented_hires) {
279 		u8 pressure_low_byte;
280 		u8 pressure_high_byte;
281 
282 		/* Lift pressure bytes */
283 		pressure_low_byte = data[6];
284 		pressure_high_byte = data[7];
285 		/*
286 		 * Move Y coord to make space for high-order X
287 		 * coord byte
288 		 */
289 		data[6] = data[5];
290 		data[5] = data[4];
291 		/* Move high-order X coord byte */
292 		data[4] = data[8];
293 		/* Move high-order Y coord byte */
294 		data[7] = data[9];
295 		/* Place pressure bytes */
296 		data[8] = pressure_low_byte;
297 		data[9] = pressure_high_byte;
298 	}
299 	/* If we need to emulate in-range detection */
300 	if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
301 		/* Set in-range bit */
302 		data[1] |= 0x40;
303 		/* (Re-)start in-range timeout */
304 		mod_timer(&drvdata->inrange_timer,
305 				jiffies + msecs_to_jiffies(100));
306 	}
307 	/* If we report tilt and Y direction is flipped */
308 	if (size >= 12 && pen->tilt_y_flipped)
309 		data[11] = -data[11];
310 
311 	return 0;
312 }
313 
314 /**
315  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
316  *
317  * @drvdata:	Driver data.
318  * @frame:	The parameters of the frame controls to handle.
319  * @data:	Report data buffer, can be modified.
320  * @size:	Report data size, bytes.
321  *
322  * Returns:
323  *	Negative value on error (stops event delivery), zero for success.
324  */
325 static int uclogic_raw_event_frame(
326 		struct uclogic_drvdata *drvdata,
327 		const struct uclogic_params_frame *frame,
328 		u8 *data, int size)
329 {
330 	WARN_ON(drvdata == NULL);
331 	WARN_ON(data == NULL && size != 0);
332 
333 	/* If need to, and can, set pad device ID for Wacom drivers */
334 	if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
335 		/* If we also have a touch ring and the finger left it */
336 		if (frame->touch_ring_byte > 0 &&
337 		    frame->touch_ring_byte < size &&
338 		    data[frame->touch_ring_byte] == 0) {
339 			data[frame->dev_id_byte] = 0;
340 		} else {
341 			data[frame->dev_id_byte] = 0xf;
342 		}
343 	}
344 
345 	/* If need to, and can, read rotary encoder state change */
346 	if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
347 		unsigned int byte = frame->re_lsb / 8;
348 		unsigned int bit = frame->re_lsb % 8;
349 
350 		u8 change;
351 		u8 prev_state = drvdata->re_state;
352 		/* Read Gray-coded state */
353 		u8 state = (data[byte] >> bit) & 0x3;
354 		/* Encode state change into 2-bit signed integer */
355 		if ((prev_state == 1 && state == 0) ||
356 		    (prev_state == 2 && state == 3)) {
357 			change = 1;
358 		} else if ((prev_state == 2 && state == 0) ||
359 			   (prev_state == 1 && state == 3)) {
360 			change = 3;
361 		} else {
362 			change = 0;
363 		}
364 		/* Write change */
365 		data[byte] = (data[byte] & ~((u8)3 << bit)) |
366 				(change << bit);
367 		/* Remember state */
368 		drvdata->re_state = state;
369 	}
370 
371 	/* If need to, and can, transform the touch ring reports */
372 	if (frame->touch_ring_byte > 0 && frame->touch_ring_byte < size &&
373 	    frame->touch_ring_flip_at != 0) {
374 		__s8 value = data[frame->touch_ring_byte];
375 
376 		if (value != 0) {
377 			value = frame->touch_ring_flip_at - value;
378 			if (value < 0)
379 				value = frame->touch_ring_max + value;
380 
381 			data[frame->touch_ring_byte] = value;
382 		}
383 	}
384 
385 	/* If need to, and can, transform the bitmap dial reports */
386 	if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
387 		if (data[frame->bitmap_dial_byte] == 2)
388 			data[frame->bitmap_dial_byte] = -1;
389 	}
390 
391 	return 0;
392 }
393 
394 static int uclogic_raw_event(struct hid_device *hdev,
395 				struct hid_report *report,
396 				u8 *data, int size)
397 {
398 	unsigned int report_id = report->id;
399 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
400 	struct uclogic_params *params = &drvdata->params;
401 	struct uclogic_params_pen_subreport *subreport;
402 	struct uclogic_params_pen_subreport *subreport_list_end;
403 	size_t i;
404 
405 	/* Do not handle anything but input reports */
406 	if (report->type != HID_INPUT_REPORT)
407 		return 0;
408 
409 	while (true) {
410 		/* Tweak pen reports, if necessary */
411 		if ((report_id == params->pen.id) && (size >= 2)) {
412 			subreport_list_end =
413 				params->pen.subreport_list +
414 				ARRAY_SIZE(params->pen.subreport_list);
415 			/* Try to match a subreport */
416 			for (subreport = params->pen.subreport_list;
417 			     subreport < subreport_list_end; subreport++) {
418 				if (subreport->value != 0 &&
419 				    subreport->value == data[1]) {
420 					break;
421 				}
422 			}
423 			/* If a subreport matched */
424 			if (subreport < subreport_list_end) {
425 				/* Change to subreport ID, and restart */
426 				report_id = data[0] = subreport->id;
427 				continue;
428 			} else {
429 				return uclogic_raw_event_pen(drvdata, data, size);
430 			}
431 		}
432 
433 		/* Tweak frame control reports, if necessary */
434 		for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
435 			if (report_id == params->frame_list[i].id) {
436 				return uclogic_raw_event_frame(
437 					drvdata, &params->frame_list[i],
438 					data, size);
439 			}
440 		}
441 
442 		break;
443 	}
444 
445 	return 0;
446 }
447 
448 static void uclogic_remove(struct hid_device *hdev)
449 {
450 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
451 
452 	del_timer_sync(&drvdata->inrange_timer);
453 	hid_hw_stop(hdev);
454 	kfree(drvdata->desc_ptr);
455 	uclogic_params_cleanup(&drvdata->params);
456 }
457 
458 static const struct hid_device_id uclogic_devices[] = {
459 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
460 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
461 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
462 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
463 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
464 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
465 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
466 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
467 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
468 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
469 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
470 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
471 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
472 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
473 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
474 				USB_DEVICE_ID_HUION_TABLET) },
475 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
476 				USB_DEVICE_ID_HUION_TABLET2) },
477 	{ HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
478 				USB_DEVICE_ID_TRUST_PANORA_TABLET) },
479 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
480 				USB_DEVICE_ID_HUION_TABLET) },
481 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
482 				USB_DEVICE_ID_YIYNOVA_TABLET) },
483 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
484 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
485 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
486 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
487 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
488 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
489 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
490 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
491 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
492 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
493 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
494 				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
495 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
496 				USB_DEVICE_ID_UGEE_TABLET_G5) },
497 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
498 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
499 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
500 				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
501 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
502 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
503 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
504 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
505 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
506 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
507 	{ }
508 };
509 MODULE_DEVICE_TABLE(hid, uclogic_devices);
510 
511 static struct hid_driver uclogic_driver = {
512 	.name = "uclogic",
513 	.id_table = uclogic_devices,
514 	.probe = uclogic_probe,
515 	.remove = uclogic_remove,
516 	.report_fixup = uclogic_report_fixup,
517 	.raw_event = uclogic_raw_event,
518 	.input_configured = uclogic_input_configured,
519 #ifdef CONFIG_PM
520 	.resume	          = uclogic_resume,
521 	.reset_resume     = uclogic_resume,
522 #endif
523 };
524 module_hid_driver(uclogic_driver);
525 
526 MODULE_AUTHOR("Martin Rusko");
527 MODULE_AUTHOR("Nikolai Kondrashov");
528 MODULE_LICENSE("GPL");
529