18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  HID driver for UC-Logic devices not fully compliant with HID standard
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2010-2014 Nikolai Kondrashov
68c2ecf20Sopenharmony_ci *  Copyright (c) 2013 Martin Rusko
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
118c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free
128c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option)
138c2ecf20Sopenharmony_ci * any later version.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/hid.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/timer.h>
208c2ecf20Sopenharmony_ci#include "usbhid/usbhid.h"
218c2ecf20Sopenharmony_ci#include "hid-uclogic-params.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "hid-ids.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Driver data */
268c2ecf20Sopenharmony_cistruct uclogic_drvdata {
278c2ecf20Sopenharmony_ci	/* Interface parameters */
288c2ecf20Sopenharmony_ci	struct uclogic_params params;
298c2ecf20Sopenharmony_ci	/* Pointer to the replacement report descriptor. NULL if none. */
308c2ecf20Sopenharmony_ci	__u8 *desc_ptr;
318c2ecf20Sopenharmony_ci	/*
328c2ecf20Sopenharmony_ci	 * Size of the replacement report descriptor.
338c2ecf20Sopenharmony_ci	 * Only valid if desc_ptr is not NULL
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	unsigned int desc_size;
368c2ecf20Sopenharmony_ci	/* Pen input device */
378c2ecf20Sopenharmony_ci	struct input_dev *pen_input;
388c2ecf20Sopenharmony_ci	/* In-range timer */
398c2ecf20Sopenharmony_ci	struct timer_list inrange_timer;
408c2ecf20Sopenharmony_ci	/* Last rotary encoder state, or U8_MAX for none */
418c2ecf20Sopenharmony_ci	u8 re_state;
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * uclogic_inrange_timeout - handle pen in-range state timeout.
468c2ecf20Sopenharmony_ci * Emulate input events normally generated when pen goes out of range for
478c2ecf20Sopenharmony_ci * tablets which don't report that.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * @t:	The timer the timeout handler is attached to, stored in a struct
508c2ecf20Sopenharmony_ci *	uclogic_drvdata.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic void uclogic_inrange_timeout(struct timer_list *t)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
558c2ecf20Sopenharmony_ci							inrange_timer);
568c2ecf20Sopenharmony_ci	struct input_dev *input = drvdata->pen_input;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (input == NULL)
598c2ecf20Sopenharmony_ci		return;
608c2ecf20Sopenharmony_ci	input_report_abs(input, ABS_PRESSURE, 0);
618c2ecf20Sopenharmony_ci	/* If BTN_TOUCH state is changing */
628c2ecf20Sopenharmony_ci	if (test_bit(BTN_TOUCH, input->key)) {
638c2ecf20Sopenharmony_ci		input_event(input, EV_MSC, MSC_SCAN,
648c2ecf20Sopenharmony_ci				/* Digitizer Tip Switch usage */
658c2ecf20Sopenharmony_ci				0xd0042);
668c2ecf20Sopenharmony_ci		input_report_key(input, BTN_TOUCH, 0);
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	input_report_key(input, BTN_TOOL_PEN, 0);
698c2ecf20Sopenharmony_ci	input_sync(input);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
738c2ecf20Sopenharmony_ci					unsigned int *rsize)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (drvdata->desc_ptr != NULL) {
788c2ecf20Sopenharmony_ci		rdesc = drvdata->desc_ptr;
798c2ecf20Sopenharmony_ci		*rsize = drvdata->desc_size;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci	return rdesc;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int uclogic_input_mapping(struct hid_device *hdev,
858c2ecf20Sopenharmony_ci				 struct hid_input *hi,
868c2ecf20Sopenharmony_ci				 struct hid_field *field,
878c2ecf20Sopenharmony_ci				 struct hid_usage *usage,
888c2ecf20Sopenharmony_ci				 unsigned long **bit,
898c2ecf20Sopenharmony_ci				 int *max)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
928c2ecf20Sopenharmony_ci	struct uclogic_params *params = &drvdata->params;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* discard the unused pen interface */
958c2ecf20Sopenharmony_ci	if (params->pen_unused && (field->application == HID_DG_PEN))
968c2ecf20Sopenharmony_ci		return -1;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/* let hid-core decide what to do */
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int uclogic_input_configured(struct hid_device *hdev,
1038c2ecf20Sopenharmony_ci		struct hid_input *hi)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
1068c2ecf20Sopenharmony_ci	struct uclogic_params *params = &drvdata->params;
1078c2ecf20Sopenharmony_ci	char *name;
1088c2ecf20Sopenharmony_ci	const char *suffix = NULL;
1098c2ecf20Sopenharmony_ci	struct hid_field *field;
1108c2ecf20Sopenharmony_ci	size_t len;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* no report associated (HID_QUIRK_MULTI_INPUT not set) */
1138c2ecf20Sopenharmony_ci	if (!hi->report)
1148c2ecf20Sopenharmony_ci		return 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/*
1178c2ecf20Sopenharmony_ci	 * If this is the input corresponding to the pen report
1188c2ecf20Sopenharmony_ci	 * in need of tweaking.
1198c2ecf20Sopenharmony_ci	 */
1208c2ecf20Sopenharmony_ci	if (hi->report->id == params->pen.id) {
1218c2ecf20Sopenharmony_ci		/* Remember the input device so we can simulate events */
1228c2ecf20Sopenharmony_ci		drvdata->pen_input = hi->input;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	field = hi->report->field[0];
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	switch (field->application) {
1288c2ecf20Sopenharmony_ci	case HID_GD_KEYBOARD:
1298c2ecf20Sopenharmony_ci		suffix = "Keyboard";
1308c2ecf20Sopenharmony_ci		break;
1318c2ecf20Sopenharmony_ci	case HID_GD_MOUSE:
1328c2ecf20Sopenharmony_ci		suffix = "Mouse";
1338c2ecf20Sopenharmony_ci		break;
1348c2ecf20Sopenharmony_ci	case HID_GD_KEYPAD:
1358c2ecf20Sopenharmony_ci		suffix = "Pad";
1368c2ecf20Sopenharmony_ci		break;
1378c2ecf20Sopenharmony_ci	case HID_DG_PEN:
1388c2ecf20Sopenharmony_ci		suffix = "Pen";
1398c2ecf20Sopenharmony_ci		break;
1408c2ecf20Sopenharmony_ci	case HID_CP_CONSUMER_CONTROL:
1418c2ecf20Sopenharmony_ci		suffix = "Consumer Control";
1428c2ecf20Sopenharmony_ci		break;
1438c2ecf20Sopenharmony_ci	case HID_GD_SYSTEM_CONTROL:
1448c2ecf20Sopenharmony_ci		suffix = "System Control";
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (suffix) {
1498c2ecf20Sopenharmony_ci		len = strlen(hdev->name) + 2 + strlen(suffix);
1508c2ecf20Sopenharmony_ci		name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
1518c2ecf20Sopenharmony_ci		if (name) {
1528c2ecf20Sopenharmony_ci			snprintf(name, len, "%s %s", hdev->name, suffix);
1538c2ecf20Sopenharmony_ci			hi->input->name = name;
1548c2ecf20Sopenharmony_ci		}
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int uclogic_probe(struct hid_device *hdev,
1618c2ecf20Sopenharmony_ci		const struct hid_device_id *id)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int rc;
1648c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = NULL;
1658c2ecf20Sopenharmony_ci	bool params_initialized = false;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (!hid_is_usb(hdev))
1688c2ecf20Sopenharmony_ci		return -EINVAL;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/*
1718c2ecf20Sopenharmony_ci	 * libinput requires the pad interface to be on a different node
1728c2ecf20Sopenharmony_ci	 * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
1738c2ecf20Sopenharmony_ci	 */
1748c2ecf20Sopenharmony_ci	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1758c2ecf20Sopenharmony_ci	hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* Allocate and assign driver data */
1788c2ecf20Sopenharmony_ci	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1798c2ecf20Sopenharmony_ci	if (drvdata == NULL) {
1808c2ecf20Sopenharmony_ci		rc = -ENOMEM;
1818c2ecf20Sopenharmony_ci		goto failure;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
1848c2ecf20Sopenharmony_ci	drvdata->re_state = U8_MAX;
1858c2ecf20Sopenharmony_ci	hid_set_drvdata(hdev, drvdata);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* Initialize the device and retrieve interface parameters */
1888c2ecf20Sopenharmony_ci	rc = uclogic_params_init(&drvdata->params, hdev);
1898c2ecf20Sopenharmony_ci	if (rc != 0) {
1908c2ecf20Sopenharmony_ci		hid_err(hdev, "failed probing parameters: %d\n", rc);
1918c2ecf20Sopenharmony_ci		goto failure;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci	params_initialized = true;
1948c2ecf20Sopenharmony_ci	hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
1958c2ecf20Sopenharmony_ci		UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
1968c2ecf20Sopenharmony_ci	if (drvdata->params.invalid) {
1978c2ecf20Sopenharmony_ci		hid_info(hdev, "interface is invalid, ignoring\n");
1988c2ecf20Sopenharmony_ci		rc = -ENODEV;
1998c2ecf20Sopenharmony_ci		goto failure;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/* Generate replacement report descriptor */
2038c2ecf20Sopenharmony_ci	rc = uclogic_params_get_desc(&drvdata->params,
2048c2ecf20Sopenharmony_ci				     &drvdata->desc_ptr,
2058c2ecf20Sopenharmony_ci				     &drvdata->desc_size);
2068c2ecf20Sopenharmony_ci	if (rc) {
2078c2ecf20Sopenharmony_ci		hid_err(hdev,
2088c2ecf20Sopenharmony_ci			"failed generating replacement report descriptor: %d\n",
2098c2ecf20Sopenharmony_ci			rc);
2108c2ecf20Sopenharmony_ci		goto failure;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	rc = hid_parse(hdev);
2148c2ecf20Sopenharmony_ci	if (rc) {
2158c2ecf20Sopenharmony_ci		hid_err(hdev, "parse failed\n");
2168c2ecf20Sopenharmony_ci		goto failure;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2208c2ecf20Sopenharmony_ci	if (rc) {
2218c2ecf20Sopenharmony_ci		hid_err(hdev, "hw start failed\n");
2228c2ecf20Sopenharmony_ci		goto failure;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return 0;
2268c2ecf20Sopenharmony_cifailure:
2278c2ecf20Sopenharmony_ci	/* Assume "remove" might not be called if "probe" failed */
2288c2ecf20Sopenharmony_ci	if (params_initialized)
2298c2ecf20Sopenharmony_ci		uclogic_params_cleanup(&drvdata->params);
2308c2ecf20Sopenharmony_ci	return rc;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
2348c2ecf20Sopenharmony_cistatic int uclogic_resume(struct hid_device *hdev)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	int rc;
2378c2ecf20Sopenharmony_ci	struct uclogic_params params;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Re-initialize the device, but discard parameters */
2408c2ecf20Sopenharmony_ci	rc = uclogic_params_init(&params, hdev);
2418c2ecf20Sopenharmony_ci	if (rc != 0)
2428c2ecf20Sopenharmony_ci		hid_err(hdev, "failed to re-initialize the device\n");
2438c2ecf20Sopenharmony_ci	else
2448c2ecf20Sopenharmony_ci		uclogic_params_cleanup(&params);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return rc;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci#endif
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic int uclogic_raw_event(struct hid_device *hdev,
2518c2ecf20Sopenharmony_ci				struct hid_report *report,
2528c2ecf20Sopenharmony_ci				u8 *data, int size)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
2558c2ecf20Sopenharmony_ci	struct uclogic_params *params = &drvdata->params;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* Tweak pen reports, if necessary */
2588c2ecf20Sopenharmony_ci	if (!params->pen_unused &&
2598c2ecf20Sopenharmony_ci	    (report->type == HID_INPUT_REPORT) &&
2608c2ecf20Sopenharmony_ci	    (report->id == params->pen.id) &&
2618c2ecf20Sopenharmony_ci	    (size >= 2)) {
2628c2ecf20Sopenharmony_ci		/* If it's the "virtual" frame controls report */
2638c2ecf20Sopenharmony_ci		if (params->frame.id != 0 &&
2648c2ecf20Sopenharmony_ci		    data[1] & params->pen_frame_flag) {
2658c2ecf20Sopenharmony_ci			/* Change to virtual frame controls report ID */
2668c2ecf20Sopenharmony_ci			data[0] = params->frame.id;
2678c2ecf20Sopenharmony_ci			return 0;
2688c2ecf20Sopenharmony_ci		}
2698c2ecf20Sopenharmony_ci		/* If in-range reports are inverted */
2708c2ecf20Sopenharmony_ci		if (params->pen.inrange ==
2718c2ecf20Sopenharmony_ci			UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
2728c2ecf20Sopenharmony_ci			/* Invert the in-range bit */
2738c2ecf20Sopenharmony_ci			data[1] ^= 0x40;
2748c2ecf20Sopenharmony_ci		}
2758c2ecf20Sopenharmony_ci		/*
2768c2ecf20Sopenharmony_ci		 * If report contains fragmented high-resolution pen
2778c2ecf20Sopenharmony_ci		 * coordinates
2788c2ecf20Sopenharmony_ci		 */
2798c2ecf20Sopenharmony_ci		if (size >= 10 && params->pen.fragmented_hires) {
2808c2ecf20Sopenharmony_ci			u8 pressure_low_byte;
2818c2ecf20Sopenharmony_ci			u8 pressure_high_byte;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci			/* Lift pressure bytes */
2848c2ecf20Sopenharmony_ci			pressure_low_byte = data[6];
2858c2ecf20Sopenharmony_ci			pressure_high_byte = data[7];
2868c2ecf20Sopenharmony_ci			/*
2878c2ecf20Sopenharmony_ci			 * Move Y coord to make space for high-order X
2888c2ecf20Sopenharmony_ci			 * coord byte
2898c2ecf20Sopenharmony_ci			 */
2908c2ecf20Sopenharmony_ci			data[6] = data[5];
2918c2ecf20Sopenharmony_ci			data[5] = data[4];
2928c2ecf20Sopenharmony_ci			/* Move high-order X coord byte */
2938c2ecf20Sopenharmony_ci			data[4] = data[8];
2948c2ecf20Sopenharmony_ci			/* Move high-order Y coord byte */
2958c2ecf20Sopenharmony_ci			data[7] = data[9];
2968c2ecf20Sopenharmony_ci			/* Place pressure bytes */
2978c2ecf20Sopenharmony_ci			data[8] = pressure_low_byte;
2988c2ecf20Sopenharmony_ci			data[9] = pressure_high_byte;
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci		/* If we need to emulate in-range detection */
3018c2ecf20Sopenharmony_ci		if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
3028c2ecf20Sopenharmony_ci			/* Set in-range bit */
3038c2ecf20Sopenharmony_ci			data[1] |= 0x40;
3048c2ecf20Sopenharmony_ci			/* (Re-)start in-range timeout */
3058c2ecf20Sopenharmony_ci			mod_timer(&drvdata->inrange_timer,
3068c2ecf20Sopenharmony_ci					jiffies + msecs_to_jiffies(100));
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Tweak frame control reports, if necessary */
3118c2ecf20Sopenharmony_ci	if ((report->type == HID_INPUT_REPORT) &&
3128c2ecf20Sopenharmony_ci	    (report->id == params->frame.id)) {
3138c2ecf20Sopenharmony_ci		/* If need to, and can, set pad device ID for Wacom drivers */
3148c2ecf20Sopenharmony_ci		if (params->frame.dev_id_byte > 0 &&
3158c2ecf20Sopenharmony_ci		    params->frame.dev_id_byte < size) {
3168c2ecf20Sopenharmony_ci			data[params->frame.dev_id_byte] = 0xf;
3178c2ecf20Sopenharmony_ci		}
3188c2ecf20Sopenharmony_ci		/* If need to, and can, read rotary encoder state change */
3198c2ecf20Sopenharmony_ci		if (params->frame.re_lsb > 0 &&
3208c2ecf20Sopenharmony_ci		    params->frame.re_lsb / 8 < size) {
3218c2ecf20Sopenharmony_ci			unsigned int byte = params->frame.re_lsb / 8;
3228c2ecf20Sopenharmony_ci			unsigned int bit = params->frame.re_lsb % 8;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci			u8 change;
3258c2ecf20Sopenharmony_ci			u8 prev_state = drvdata->re_state;
3268c2ecf20Sopenharmony_ci			/* Read Gray-coded state */
3278c2ecf20Sopenharmony_ci			u8 state = (data[byte] >> bit) & 0x3;
3288c2ecf20Sopenharmony_ci			/* Encode state change into 2-bit signed integer */
3298c2ecf20Sopenharmony_ci			if ((prev_state == 1 && state == 0) ||
3308c2ecf20Sopenharmony_ci			    (prev_state == 2 && state == 3)) {
3318c2ecf20Sopenharmony_ci				change = 1;
3328c2ecf20Sopenharmony_ci			} else if ((prev_state == 2 && state == 0) ||
3338c2ecf20Sopenharmony_ci				   (prev_state == 1 && state == 3)) {
3348c2ecf20Sopenharmony_ci				change = 3;
3358c2ecf20Sopenharmony_ci			} else {
3368c2ecf20Sopenharmony_ci				change = 0;
3378c2ecf20Sopenharmony_ci			}
3388c2ecf20Sopenharmony_ci			/* Write change */
3398c2ecf20Sopenharmony_ci			data[byte] = (data[byte] & ~((u8)3 << bit)) |
3408c2ecf20Sopenharmony_ci					(change << bit);
3418c2ecf20Sopenharmony_ci			/* Remember state */
3428c2ecf20Sopenharmony_ci			drvdata->re_state = state;
3438c2ecf20Sopenharmony_ci		}
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return 0;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic void uclogic_remove(struct hid_device *hdev)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	del_timer_sync(&drvdata->inrange_timer);
3548c2ecf20Sopenharmony_ci	hid_hw_stop(hdev);
3558c2ecf20Sopenharmony_ci	kfree(drvdata->desc_ptr);
3568c2ecf20Sopenharmony_ci	uclogic_params_cleanup(&drvdata->params);
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic const struct hid_device_id uclogic_devices[] = {
3608c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3618c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
3628c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3638c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
3648c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3658c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
3668c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3678c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
3688c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3698c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
3708c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3718c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
3728c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3738c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
3748c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
3758c2ecf20Sopenharmony_ci				USB_DEVICE_ID_HUION_TABLET) },
3768c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_HUION,
3778c2ecf20Sopenharmony_ci				USB_DEVICE_ID_HUION_HS64) },
3788c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3798c2ecf20Sopenharmony_ci				USB_DEVICE_ID_HUION_TABLET) },
3808c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3818c2ecf20Sopenharmony_ci				USB_DEVICE_ID_YIYNOVA_TABLET) },
3828c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3838c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
3848c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3858c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
3868c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3878c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
3888c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
3898c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
3908c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
3918c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
3928c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
3938c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
3948c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
3958c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_TABLET_G5) },
3968c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
3978c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_TABLET_EX07S) },
3988c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
3998c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
4008c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
4018c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
4028c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
4038c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
4048c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
4058c2ecf20Sopenharmony_ci				USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
4068c2ecf20Sopenharmony_ci	{ }
4078c2ecf20Sopenharmony_ci};
4088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, uclogic_devices);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic struct hid_driver uclogic_driver = {
4118c2ecf20Sopenharmony_ci	.name = "uclogic",
4128c2ecf20Sopenharmony_ci	.id_table = uclogic_devices,
4138c2ecf20Sopenharmony_ci	.probe = uclogic_probe,
4148c2ecf20Sopenharmony_ci	.remove = uclogic_remove,
4158c2ecf20Sopenharmony_ci	.report_fixup = uclogic_report_fixup,
4168c2ecf20Sopenharmony_ci	.raw_event = uclogic_raw_event,
4178c2ecf20Sopenharmony_ci	.input_mapping = uclogic_input_mapping,
4188c2ecf20Sopenharmony_ci	.input_configured = uclogic_input_configured,
4198c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4208c2ecf20Sopenharmony_ci	.resume	          = uclogic_resume,
4218c2ecf20Sopenharmony_ci	.reset_resume     = uclogic_resume,
4228c2ecf20Sopenharmony_ci#endif
4238c2ecf20Sopenharmony_ci};
4248c2ecf20Sopenharmony_cimodule_hid_driver(uclogic_driver);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Rusko");
4278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nikolai Kondrashov");
4288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
429