xref: /linux/drivers/hid/hid-uclogic-core.c (revision e724e7aaf9ca794670a4d4931af7a7e24e37fec3)
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 /**
26  * uclogic_inrange_timeout - handle pen in-range state timeout.
27  * Emulate input events normally generated when pen goes out of range for
28  * tablets which don't report that.
29  *
30  * @t:	The timer the timeout handler is attached to, stored in a struct
31  *	uclogic_drvdata.
32  */
33 static void uclogic_inrange_timeout(struct timer_list *t)
34 {
35 	struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
36 							inrange_timer);
37 	struct input_dev *input = drvdata->pen_input;
38 
39 	if (input == NULL)
40 		return;
41 	input_report_abs(input, ABS_PRESSURE, 0);
42 	/* If BTN_TOUCH state is changing */
43 	if (test_bit(BTN_TOUCH, input->key)) {
44 		input_event(input, EV_MSC, MSC_SCAN,
45 				/* Digitizer Tip Switch usage */
46 				0xd0042);
47 		input_report_key(input, BTN_TOUCH, 0);
48 	}
49 	input_report_key(input, BTN_TOOL_PEN, 0);
50 	input_sync(input);
51 }
52 
53 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
54 					unsigned int *rsize)
55 {
56 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
57 
58 	if (drvdata->desc_ptr != NULL) {
59 		rdesc = drvdata->desc_ptr;
60 		*rsize = drvdata->desc_size;
61 	}
62 	return rdesc;
63 }
64 
65 static int uclogic_input_mapping(struct hid_device *hdev,
66 				 struct hid_input *hi,
67 				 struct hid_field *field,
68 				 struct hid_usage *usage,
69 				 unsigned long **bit,
70 				 int *max)
71 {
72 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
73 	struct uclogic_params *params = &drvdata->params;
74 
75 	/* Discard invalid pen usages */
76 	if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
77 		return -1;
78 
79 	/* Let hid-core decide what to do */
80 	return 0;
81 }
82 
83 static int uclogic_input_configured(struct hid_device *hdev,
84 		struct hid_input *hi)
85 {
86 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
87 	struct uclogic_params *params = &drvdata->params;
88 	char *name;
89 	const char *suffix = NULL;
90 	struct hid_field *field;
91 	size_t len;
92 	size_t i;
93 	const struct uclogic_params_frame *frame;
94 
95 	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
96 	if (!hi->report)
97 		return 0;
98 
99 	/*
100 	 * If this is the input corresponding to the pen report
101 	 * in need of tweaking.
102 	 */
103 	if (hi->report->id == params->pen.id) {
104 		/* Remember the input device so we can simulate events */
105 		drvdata->pen_input = hi->input;
106 	}
107 
108 	/* If it's one of the frame devices */
109 	for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
110 		frame = &params->frame_list[i];
111 		if (hi->report->id == frame->id) {
112 			/* Assign custom suffix, if any */
113 			suffix = frame->suffix;
114 			/*
115 			 * Disable EV_MSC reports for touch ring interfaces to
116 			 * make the Wacom driver pickup touch ring extents
117 			 */
118 			if (frame->touch_byte > 0)
119 				__clear_bit(EV_MSC, hi->input->evbit);
120 		}
121 	}
122 
123 	if (!suffix) {
124 		field = hi->report->field[0];
125 
126 		switch (field->application) {
127 		case HID_GD_KEYBOARD:
128 			suffix = "Keyboard";
129 			break;
130 		case HID_GD_MOUSE:
131 			suffix = "Mouse";
132 			break;
133 		case HID_GD_KEYPAD:
134 			suffix = "Pad";
135 			break;
136 		case HID_DG_PEN:
137 		case HID_DG_DIGITIZER:
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 	hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
177 
178 	/* Allocate and assign driver data */
179 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
180 	if (drvdata == NULL) {
181 		rc = -ENOMEM;
182 		goto failure;
183 	}
184 	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
185 	drvdata->re_state = U8_MAX;
186 	drvdata->quirks = id->driver_data;
187 	hid_set_drvdata(hdev, drvdata);
188 
189 	/* Initialize the device and retrieve interface parameters */
190 	rc = uclogic_params_init(&drvdata->params, hdev);
191 	if (rc != 0) {
192 		hid_err(hdev, "failed probing parameters: %d\n", rc);
193 		goto failure;
194 	}
195 	params_initialized = true;
196 	hid_dbg(hdev, "parameters:\n");
197 	uclogic_params_hid_dbg(hdev, &drvdata->params);
198 	if (drvdata->params.invalid) {
199 		hid_info(hdev, "interface is invalid, ignoring\n");
200 		rc = -ENODEV;
201 		goto failure;
202 	}
203 
204 	/* Generate replacement report descriptor */
205 	rc = uclogic_params_get_desc(&drvdata->params,
206 				     &drvdata->desc_ptr,
207 				     &drvdata->desc_size);
208 	if (rc) {
209 		hid_err(hdev,
210 			"failed generating replacement report descriptor: %d\n",
211 			rc);
212 		goto failure;
213 	}
214 
215 	rc = hid_parse(hdev);
216 	if (rc) {
217 		hid_err(hdev, "parse failed\n");
218 		goto failure;
219 	}
220 
221 	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
222 	if (rc) {
223 		hid_err(hdev, "hw start failed\n");
224 		goto failure;
225 	}
226 
227 	return 0;
228 failure:
229 	/* Assume "remove" might not be called if "probe" failed */
230 	if (params_initialized)
231 		uclogic_params_cleanup(&drvdata->params);
232 	return rc;
233 }
234 
235 #ifdef CONFIG_PM
236 static int uclogic_resume(struct hid_device *hdev)
237 {
238 	int rc;
239 	struct uclogic_params params;
240 
241 	/* Re-initialize the device, but discard parameters */
242 	rc = uclogic_params_init(&params, hdev);
243 	if (rc != 0)
244 		hid_err(hdev, "failed to re-initialize the device\n");
245 	else
246 		uclogic_params_cleanup(&params);
247 
248 	return rc;
249 }
250 #endif
251 
252 /**
253  * uclogic_exec_event_hook - if the received event is hooked schedules the
254  * associated work.
255  *
256  * @p:		Tablet interface report parameters.
257  * @event:	Raw event.
258  * @size:	The size of event.
259  *
260  * Returns:
261  *	Whether the event was hooked or not.
262  */
263 static bool uclogic_exec_event_hook(struct uclogic_params *p, u8 *event, int size)
264 {
265 	struct uclogic_raw_event_hook *curr;
266 
267 	if (!p->event_hooks)
268 		return false;
269 
270 	list_for_each_entry(curr, &p->event_hooks->list, list) {
271 		if (curr->size == size && memcmp(curr->event, event, size) == 0) {
272 			schedule_work(&curr->work);
273 			return true;
274 		}
275 	}
276 
277 	return false;
278 }
279 
280 /**
281  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
282  *
283  * @drvdata:	Driver data.
284  * @data:	Report data buffer, can be modified.
285  * @size:	Report data size, bytes.
286  *
287  * Returns:
288  *	Negative value on error (stops event delivery), zero for success.
289  */
290 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
291 					u8 *data, int size)
292 {
293 	struct uclogic_params_pen *pen = &drvdata->params.pen;
294 
295 	WARN_ON(drvdata == NULL);
296 	WARN_ON(data == NULL && size != 0);
297 
298 	/* If in-range reports are inverted */
299 	if (pen->inrange ==
300 		UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
301 		/* Invert the in-range bit */
302 		data[1] ^= 0x40;
303 	}
304 	/*
305 	 * If report contains fragmented high-resolution pen
306 	 * coordinates
307 	 */
308 	if (size >= 10 && pen->fragmented_hires) {
309 		u8 pressure_low_byte;
310 		u8 pressure_high_byte;
311 
312 		/* Lift pressure bytes */
313 		pressure_low_byte = data[6];
314 		pressure_high_byte = data[7];
315 		/*
316 		 * Move Y coord to make space for high-order X
317 		 * coord byte
318 		 */
319 		data[6] = data[5];
320 		data[5] = data[4];
321 		/* Move high-order X coord byte */
322 		data[4] = data[8];
323 		/* Move high-order Y coord byte */
324 		data[7] = data[9];
325 		/* Place pressure bytes */
326 		data[8] = pressure_low_byte;
327 		data[9] = pressure_high_byte;
328 	}
329 	/* If we need to emulate in-range detection */
330 	if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
331 		/* Set in-range bit */
332 		data[1] |= 0x40;
333 		/* (Re-)start in-range timeout */
334 		mod_timer(&drvdata->inrange_timer,
335 				jiffies + msecs_to_jiffies(100));
336 	}
337 	/* If we report tilt and Y direction is flipped */
338 	if (size >= 12 && pen->tilt_y_flipped)
339 		data[11] = -data[11];
340 
341 	return 0;
342 }
343 
344 /**
345  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
346  *
347  * @drvdata:	Driver data.
348  * @frame:	The parameters of the frame controls to handle.
349  * @data:	Report data buffer, can be modified.
350  * @size:	Report data size, bytes.
351  *
352  * Returns:
353  *	Negative value on error (stops event delivery), zero for success.
354  */
355 static int uclogic_raw_event_frame(
356 		struct uclogic_drvdata *drvdata,
357 		const struct uclogic_params_frame *frame,
358 		u8 *data, int size)
359 {
360 	WARN_ON(drvdata == NULL);
361 	WARN_ON(data == NULL && size != 0);
362 
363 	/* If need to, and can, set pad device ID for Wacom drivers */
364 	if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
365 		/* If we also have a touch ring and the finger left it */
366 		if (frame->touch_byte > 0 && frame->touch_byte < size &&
367 		    data[frame->touch_byte] == 0) {
368 			data[frame->dev_id_byte] = 0;
369 		} else {
370 			data[frame->dev_id_byte] = 0xf;
371 		}
372 	}
373 
374 	/* If need to, and can, read rotary encoder state change */
375 	if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
376 		unsigned int byte = frame->re_lsb / 8;
377 		unsigned int bit = frame->re_lsb % 8;
378 
379 		u8 change;
380 		u8 prev_state = drvdata->re_state;
381 		/* Read Gray-coded state */
382 		u8 state = (data[byte] >> bit) & 0x3;
383 		/* Encode state change into 2-bit signed integer */
384 		if ((prev_state == 1 && state == 0) ||
385 		    (prev_state == 2 && state == 3)) {
386 			change = 1;
387 		} else if ((prev_state == 2 && state == 0) ||
388 			   (prev_state == 1 && state == 3)) {
389 			change = 3;
390 		} else {
391 			change = 0;
392 		}
393 		/* Write change */
394 		data[byte] = (data[byte] & ~((u8)3 << bit)) |
395 				(change << bit);
396 		/* Remember state */
397 		drvdata->re_state = state;
398 	}
399 
400 	/* If need to, and can, transform the touch ring reports */
401 	if (frame->touch_byte > 0 && frame->touch_byte < size) {
402 		__s8 value = data[frame->touch_byte];
403 
404 		if (value != 0) {
405 			if (frame->touch_flip_at != 0) {
406 				value = frame->touch_flip_at - value;
407 				if (value <= 0)
408 					value = frame->touch_max + value;
409 			}
410 			data[frame->touch_byte] = value - 1;
411 		}
412 	}
413 
414 	/* If need to, and can, transform the bitmap dial reports */
415 	if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
416 		if (data[frame->bitmap_dial_byte] == 2)
417 			data[frame->bitmap_dial_byte] = -1;
418 	}
419 
420 	return 0;
421 }
422 
423 static int uclogic_raw_event(struct hid_device *hdev,
424 				struct hid_report *report,
425 				u8 *data, int size)
426 {
427 	unsigned int report_id = report->id;
428 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
429 	struct uclogic_params *params = &drvdata->params;
430 	struct uclogic_params_pen_subreport *subreport;
431 	struct uclogic_params_pen_subreport *subreport_list_end;
432 	size_t i;
433 
434 	/* Do not handle anything but input reports */
435 	if (report->type != HID_INPUT_REPORT)
436 		return 0;
437 
438 	if (uclogic_exec_event_hook(params, data, size))
439 		return 0;
440 
441 	while (true) {
442 		/* Tweak pen reports, if necessary */
443 		if ((report_id == params->pen.id) && (size >= 2)) {
444 			subreport_list_end =
445 				params->pen.subreport_list +
446 				ARRAY_SIZE(params->pen.subreport_list);
447 			/* Try to match a subreport */
448 			for (subreport = params->pen.subreport_list;
449 			     subreport < subreport_list_end; subreport++) {
450 				if (subreport->value != 0 &&
451 				    subreport->value == data[1]) {
452 					break;
453 				}
454 			}
455 			/* If a subreport matched */
456 			if (subreport < subreport_list_end) {
457 				/* Change to subreport ID, and restart */
458 				report_id = data[0] = subreport->id;
459 				continue;
460 			} else {
461 				return uclogic_raw_event_pen(drvdata, data, size);
462 			}
463 		}
464 
465 		/* Tweak frame control reports, if necessary */
466 		for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
467 			if (report_id == params->frame_list[i].id) {
468 				return uclogic_raw_event_frame(
469 					drvdata, &params->frame_list[i],
470 					data, size);
471 			}
472 		}
473 
474 		break;
475 	}
476 
477 	return 0;
478 }
479 
480 static void uclogic_remove(struct hid_device *hdev)
481 {
482 	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
483 
484 	del_timer_sync(&drvdata->inrange_timer);
485 	hid_hw_stop(hdev);
486 	kfree(drvdata->desc_ptr);
487 	uclogic_params_cleanup(&drvdata->params);
488 }
489 
490 static const struct hid_device_id uclogic_devices[] = {
491 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
492 				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
493 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
494 				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
495 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
496 				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
497 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
498 				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
499 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
500 				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
501 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
502 				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
503 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
504 				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
505 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
506 				USB_DEVICE_ID_HUION_TABLET) },
507 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
508 				USB_DEVICE_ID_HUION_TABLET2) },
509 	{ HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
510 				USB_DEVICE_ID_TRUST_PANORA_TABLET) },
511 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
512 				USB_DEVICE_ID_HUION_TABLET) },
513 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
514 				USB_DEVICE_ID_YIYNOVA_TABLET) },
515 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
516 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
517 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
518 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
519 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
520 				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
521 	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
522 				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
523 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
524 				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
525 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
526 				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
527 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
528 				USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
529 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
530 				USB_DEVICE_ID_UGEE_TABLET_G5) },
531 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
532 				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
533 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
534 				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
535 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
536 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
537 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
538 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
539 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
540 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
541 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
542 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) },
543 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
544 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
545 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
546 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
547 		.driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
548 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
549 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
550 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
551 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
552 		.driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
553 	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
554 				USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
555 	{ }
556 };
557 MODULE_DEVICE_TABLE(hid, uclogic_devices);
558 
559 static struct hid_driver uclogic_driver = {
560 	.name = "uclogic",
561 	.id_table = uclogic_devices,
562 	.probe = uclogic_probe,
563 	.remove = uclogic_remove,
564 	.report_fixup = uclogic_report_fixup,
565 	.raw_event = uclogic_raw_event,
566 	.input_mapping = uclogic_input_mapping,
567 	.input_configured = uclogic_input_configured,
568 #ifdef CONFIG_PM
569 	.resume	          = uclogic_resume,
570 	.reset_resume     = uclogic_resume,
571 #endif
572 };
573 module_hid_driver(uclogic_driver);
574 
575 MODULE_AUTHOR("Martin Rusko");
576 MODULE_AUTHOR("Nikolai Kondrashov");
577 MODULE_LICENSE("GPL");
578 
579 #ifdef CONFIG_HID_KUNIT_TEST
580 #include "hid-uclogic-core-test.c"
581 #endif
582