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_mapping(struct hid_device *hdev,
85 				 struct hid_input *hi,
86 				 struct hid_field *field,
87 				 struct hid_usage *usage,
88 				 unsigned long **bit,
89 				 int *max)
90 {
91 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
92 	struct uclogic_params *params = &drvdata->params;
93 
94 	/* Discard invalid pen usages */
95 	if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
96 		return -1;
97 
98 	/* Let hid-core decide what to do */
99 	return 0;
100 }
101 
102 static int uclogic_input_configured(struct hid_device *hdev,
103 		struct hid_input *hi)
104 {
105 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
106 	struct uclogic_params *params = &drvdata->params;
107 	char *name;
108 	const char *suffix = NULL;
109 	struct hid_field *field;
110 	size_t len;
111 	size_t i;
112 	const struct uclogic_params_frame *frame;
113 
114 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
115 	if (!hi->report)
116 		return 0;
117 
118 	/*
119 	 * If this is the input corresponding to the pen report
120 	 * in need of tweaking.
121 	 */
122 	if (hi->report->id == params->pen.id) {
123 		/* Remember the input device so we can simulate events */
124 		drvdata->pen_input = hi->input;
125 	}
126 
127 	/* If it's one of the frame devices */
128 	for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
129 		frame = &params->frame_list[i];
130 		if (hi->report->id == frame->id) {
131 			/* Assign custom suffix, if any */
132 			suffix = frame->suffix;
133 			/*
134 			 * Disable EV_MSC reports for touch ring interfaces to
135 			 * make the Wacom driver pickup touch ring extents
136 			 */
137 			if (frame->touch_byte > 0)
138 				__clear_bit(EV_MSC, hi->input->evbit);
139 		}
140 	}
141 
142 	if (!suffix) {
143 		field = hi->report->field[0];
144 
145 		switch (field->application) {
146 		case HID_GD_KEYBOARD:
147 			suffix = "Keyboard";
148 			break;
149 		case HID_GD_MOUSE:
150 			suffix = "Mouse";
151 			break;
152 		case HID_GD_KEYPAD:
153 			suffix = "Pad";
154 			break;
155 		case HID_DG_PEN:
156 		case HID_DG_DIGITIZER:
157 			suffix = "Pen";
158 			break;
159 		case HID_CP_CONSUMER_CONTROL:
160 			suffix = "Consumer Control";
161 			break;
162 		case HID_GD_SYSTEM_CONTROL:
163 			suffix = "System Control";
164 			break;
165 		}
166 	}
167 
168 	if (suffix) {
169 		len = strlen(hdev->name) + 2 + strlen(suffix);
170 		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
171 		if (name) {
172 			snprintf(name, len, "%s %s", hdev->name, suffix);
173 			hi->input->name = name;
174 		}
175 	}
176 
177 	return 0;
178 }
179 
180 static int uclogic_probe(struct hid_device *hdev,
181 		const struct hid_device_id *id)
182 {
183 	int rc;
184 	struct uclogic_drvdata *drvdata = NULL;
185 	bool params_initialized = false;
186 
187 	if (!hid_is_usb(hdev))
188 		return -EINVAL;
189 
190 	/*
191 	 * libinput requires the pad interface to be on a different node
192 	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
193 	 */
194 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
195 	hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
196 
197 	/* Allocate and assign driver data */
198 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
199 	if (drvdata == NULL) {
200 		rc = -ENOMEM;
201 		goto failure;
202 	}
203 	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
204 	drvdata->re_state = U8_MAX;
205 	hid_set_drvdata(hdev, drvdata);
206 
207 	/* Initialize the device and retrieve interface parameters */
208 	rc = uclogic_params_init(&drvdata->params, hdev);
209 	if (rc != 0) {
210 		hid_err(hdev, "failed probing parameters: %d\n", rc);
211 		goto failure;
212 	}
213 	params_initialized = true;
214 	hid_dbg(hdev, "parameters:\n");
215 	uclogic_params_hid_dbg(hdev, &drvdata->params);
216 	if (drvdata->params.invalid) {
217 		hid_info(hdev, "interface is invalid, ignoring\n");
218 		rc = -ENODEV;
219 		goto failure;
220 	}
221 
222 	/* Generate replacement report descriptor */
223 	rc = uclogic_params_get_desc(&drvdata->params,
224 				     &drvdata->desc_ptr,
225 				     &drvdata->desc_size);
226 	if (rc) {
227 		hid_err(hdev,
228 			"failed generating replacement report descriptor: %d\n",
229 			rc);
230 		goto failure;
231 	}
232 
233 	rc = hid_parse(hdev);
234 	if (rc) {
235 		hid_err(hdev, "parse failed\n");
236 		goto failure;
237 	}
238 
239 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
240 	if (rc) {
241 		hid_err(hdev, "hw start failed\n");
242 		goto failure;
243 	}
244 
245 	return 0;
246 failure:
247 	/* Assume "remove" might not be called if "probe" failed */
248 	if (params_initialized)
249 		uclogic_params_cleanup(&drvdata->params);
250 	return rc;
251 }
252 
253 #ifdef CONFIG_PM
254 static int uclogic_resume(struct hid_device *hdev)
255 {
256 	int rc;
257 	struct uclogic_params params;
258 
259 	/* Re-initialize the device, but discard parameters */
260 	rc = uclogic_params_init(&params, hdev);
261 	if (rc != 0)
262 		hid_err(hdev, "failed to re-initialize the device\n");
263 	else
264 		uclogic_params_cleanup(&params);
265 
266 	return rc;
267 }
268 #endif
269 
270 /**
271  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
272  *
273  * @drvdata:	Driver data.
274  * @data:	Report data buffer, can be modified.
275  * @size:	Report data size, bytes.
276  *
277  * Returns:
278  *	Negative value on error (stops event delivery), zero for success.
279  */
280 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
281 					u8 *data, int size)
282 {
283 	struct uclogic_params_pen *pen = &drvdata->params.pen;
284 
285 	WARN_ON(drvdata == NULL);
286 	WARN_ON(data == NULL && size != 0);
287 
288 	/* If in-range reports are inverted */
289 	if (pen->inrange ==
290 		UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
291 		/* Invert the in-range bit */
292 		data[1] ^= 0x40;
293 	}
294 	/*
295 	 * If report contains fragmented high-resolution pen
296 	 * coordinates
297 	 */
298 	if (size >= 10 && pen->fragmented_hires) {
299 		u8 pressure_low_byte;
300 		u8 pressure_high_byte;
301 
302 		/* Lift pressure bytes */
303 		pressure_low_byte = data[6];
304 		pressure_high_byte = data[7];
305 		/*
306 		 * Move Y coord to make space for high-order X
307 		 * coord byte
308 		 */
309 		data[6] = data[5];
310 		data[5] = data[4];
311 		/* Move high-order X coord byte */
312 		data[4] = data[8];
313 		/* Move high-order Y coord byte */
314 		data[7] = data[9];
315 		/* Place pressure bytes */
316 		data[8] = pressure_low_byte;
317 		data[9] = pressure_high_byte;
318 	}
319 	/* If we need to emulate in-range detection */
320 	if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
321 		/* Set in-range bit */
322 		data[1] |= 0x40;
323 		/* (Re-)start in-range timeout */
324 		mod_timer(&drvdata->inrange_timer,
325 				jiffies + msecs_to_jiffies(100));
326 	}
327 	/* If we report tilt and Y direction is flipped */
328 	if (size >= 12 && pen->tilt_y_flipped)
329 		data[11] = -data[11];
330 
331 	return 0;
332 }
333 
334 /**
335  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
336  *
337  * @drvdata:	Driver data.
338  * @frame:	The parameters of the frame controls to handle.
339  * @data:	Report data buffer, can be modified.
340  * @size:	Report data size, bytes.
341  *
342  * Returns:
343  *	Negative value on error (stops event delivery), zero for success.
344  */
345 static int uclogic_raw_event_frame(
346 		struct uclogic_drvdata *drvdata,
347 		const struct uclogic_params_frame *frame,
348 		u8 *data, int size)
349 {
350 	WARN_ON(drvdata == NULL);
351 	WARN_ON(data == NULL && size != 0);
352 
353 	/* If need to, and can, set pad device ID for Wacom drivers */
354 	if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
355 		/* If we also have a touch ring and the finger left it */
356 		if (frame->touch_byte > 0 && frame->touch_byte < size &&
357 		    data[frame->touch_byte] == 0) {
358 			data[frame->dev_id_byte] = 0;
359 		} else {
360 			data[frame->dev_id_byte] = 0xf;
361 		}
362 	}
363 
364 	/* If need to, and can, read rotary encoder state change */
365 	if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
366 		unsigned int byte = frame->re_lsb / 8;
367 		unsigned int bit = frame->re_lsb % 8;
368 
369 		u8 change;
370 		u8 prev_state = drvdata->re_state;
371 		/* Read Gray-coded state */
372 		u8 state = (data[byte] >> bit) & 0x3;
373 		/* Encode state change into 2-bit signed integer */
374 		if ((prev_state == 1 && state == 0) ||
375 		    (prev_state == 2 && state == 3)) {
376 			change = 1;
377 		} else if ((prev_state == 2 && state == 0) ||
378 			   (prev_state == 1 && state == 3)) {
379 			change = 3;
380 		} else {
381 			change = 0;
382 		}
383 		/* Write change */
384 		data[byte] = (data[byte] & ~((u8)3 << bit)) |
385 				(change << bit);
386 		/* Remember state */
387 		drvdata->re_state = state;
388 	}
389 
390 	/* If need to, and can, transform the touch ring reports */
391 	if (frame->touch_byte > 0 && frame->touch_byte < size) {
392 		__s8 value = data[frame->touch_byte];
393 
394 		if (value != 0) {
395 			if (frame->touch_flip_at != 0) {
396 				value = frame->touch_flip_at - value;
397 				if (value <= 0)
398 					value = frame->touch_max + value;
399 			}
400 			data[frame->touch_byte] = value - 1;
401 		}
402 	}
403 
404 	/* If need to, and can, transform the bitmap dial reports */
405 	if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
406 		if (data[frame->bitmap_dial_byte] == 2)
407 			data[frame->bitmap_dial_byte] = -1;
408 	}
409 
410 	return 0;
411 }
412 
413 static int uclogic_raw_event(struct hid_device *hdev,
414 				struct hid_report *report,
415 				u8 *data, int size)
416 {
417 	unsigned int report_id = report->id;
418 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
419 	struct uclogic_params *params = &drvdata->params;
420 	struct uclogic_params_pen_subreport *subreport;
421 	struct uclogic_params_pen_subreport *subreport_list_end;
422 	size_t i;
423 
424 	/* Do not handle anything but input reports */
425 	if (report->type != HID_INPUT_REPORT)
426 		return 0;
427 
428 	while (true) {
429 		/* Tweak pen reports, if necessary */
430 		if ((report_id == params->pen.id) && (size >= 2)) {
431 			subreport_list_end =
432 				params->pen.subreport_list +
433 				ARRAY_SIZE(params->pen.subreport_list);
434 			/* Try to match a subreport */
435 			for (subreport = params->pen.subreport_list;
436 			     subreport < subreport_list_end; subreport++) {
437 				if (subreport->value != 0 &&
438 				    subreport->value == data[1]) {
439 					break;
440 				}
441 			}
442 			/* If a subreport matched */
443 			if (subreport < subreport_list_end) {
444 				/* Change to subreport ID, and restart */
445 				report_id = data[0] = subreport->id;
446 				continue;
447 			} else {
448 				return uclogic_raw_event_pen(drvdata, data, size);
449 			}
450 		}
451 
452 		/* Tweak frame control reports, if necessary */
453 		for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
454 			if (report_id == params->frame_list[i].id) {
455 				return uclogic_raw_event_frame(
456 					drvdata, &params->frame_list[i],
457 					data, size);
458 			}
459 		}
460 
461 		break;
462 	}
463 
464 	return 0;
465 }
466 
467 static void uclogic_remove(struct hid_device *hdev)
468 {
469 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
470 
471 	del_timer_sync(&drvdata->inrange_timer);
472 	hid_hw_stop(hdev);
473 	kfree(drvdata->desc_ptr);
474 	uclogic_params_cleanup(&drvdata->params);
475 }
476 
477 static const struct hid_device_id uclogic_devices[] = {
478 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
479 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
480 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
481 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
482 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
483 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
484 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
485 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
486 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
487 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
488 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
489 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
490 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
491 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
492 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
493 				USB_DEVICE_ID_HUION_TABLET) },
494 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
495 				USB_DEVICE_ID_HUION_TABLET2) },
496 	{ HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
497 				USB_DEVICE_ID_TRUST_PANORA_TABLET) },
498 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
499 				USB_DEVICE_ID_HUION_TABLET) },
500 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
501 				USB_DEVICE_ID_YIYNOVA_TABLET) },
502 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
503 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
504 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
505 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
506 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
507 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
508 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
509 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
510 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
511 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
512 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
513 				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
514 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
515 				USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
516 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
517 				USB_DEVICE_ID_UGEE_TABLET_G5) },
518 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
519 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
520 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
521 				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
522 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
523 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
524 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
525 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
526 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
527 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
528 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
529 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
530 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
531 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
532 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
533 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
534 	{ }
535 };
536 MODULE_DEVICE_TABLE(hid, uclogic_devices);
537 
538 static struct hid_driver uclogic_driver = {
539 	.name = "uclogic",
540 	.id_table = uclogic_devices,
541 	.probe = uclogic_probe,
542 	.remove = uclogic_remove,
543 	.report_fixup = uclogic_report_fixup,
544 	.raw_event = uclogic_raw_event,
545 	.input_mapping = uclogic_input_mapping,
546 	.input_configured = uclogic_input_configured,
547 #ifdef CONFIG_PM
548 	.resume	          = uclogic_resume,
549 	.reset_resume     = uclogic_resume,
550 #endif
551 };
552 module_hid_driver(uclogic_driver);
553 
554 MODULE_AUTHOR("Martin Rusko");
555 MODULE_AUTHOR("Nikolai Kondrashov");
556 MODULE_LICENSE("GPL");
557