xref: /linux/drivers/hid/hid-letsketch.c (revision 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021 Hans de Goede <hdegoede@redhat.com>
4  *
5  * Driver for the LetSketch / VSON WP9620N drawing tablet.
6  * This drawing tablet is also sold under other brand names such as Case U,
7  * presumably this driver will work for all of them. But it has only been
8  * tested with a LetSketch WP9620N model.
9  *
10  * These tablets also work without a special HID driver, but then only part
11  * of the active area works and both the pad and stylus buttons are hardwired
12  * to special key-combos. E.g. the 2 stylus buttons send right mouse clicks /
13  * resp. "e" key presses.
14  *
15  * This device has 4 USB interfaces:
16  *
17  * Interface 0 EP 0x81 bootclass mouse, rdesc len 18, report id 0x08,
18  *                                                    Application(ff00.0001)
19  *  This interface sends raw event input reports in a custom format, but only
20  *  after doing the special dance from letsketch_probe(). After enabling this
21  *  interface the other 3 interfaces are disabled.
22  *
23  * Interface 1 EP 0x82 bootclass mouse, rdesc len 83, report id 0x0a, Tablet
24  *  This interface sends absolute events for the pen, including pressure,
25  *  but only for some part of the active area due to special "aspect ratio"
26  *  correction and only half by default since it assumes it will be used
27  *  with a phone in portraid mode, while using the tablet in landscape mode.
28  *  Also stylus + pad button events are not reported here.
29  *
30  * Interface 2 EP 0x83 bootclass keybd, rdesc len 64, report id none, Std Kbd
31  *  This interfaces send various hard-coded key-combos for the pad buttons
32  *  and "e" keypresses for the 2nd stylus button
33  *
34  * Interface 3 EP 0x84 bootclass mouse, rdesc len 75, report id 0x01, Std Mouse
35  *  This reports right-click mouse-button events for the 1st stylus button
36  */
37 #include <linux/device.h>
38 #include <linux/input.h>
39 #include <linux/hid.h>
40 #include <linux/module.h>
41 #include <linux/timer.h>
42 #include <linux/usb.h>
43 
44 #include <linux/unaligned.h>
45 
46 #include "hid-ids.h"
47 
48 #define LETSKETCH_RAW_IF		0
49 
50 #define LETSKETCH_RAW_DATA_LEN		12
51 #define LETSKETCH_RAW_REPORT_ID		8
52 
53 #define LETSKETCH_PAD_BUTTONS		5
54 
55 #define LETSKETCH_INFO_STR_IDX_BEGIN	0xc8
56 #define LETSKETCH_INFO_STR_IDX_END	0xca
57 
58 #define LETSKETCH_GET_STRING_RETRIES	5
59 
60 struct letsketch_data {
61 	struct hid_device *hdev;
62 	struct input_dev *input_tablet;
63 	struct input_dev *input_tablet_pad;
64 	struct timer_list inrange_timer;
65 };
66 
67 static int letsketch_open(struct input_dev *dev)
68 {
69 	struct letsketch_data *data = input_get_drvdata(dev);
70 
71 	return hid_hw_open(data->hdev);
72 }
73 
74 static void letsketch_close(struct input_dev *dev)
75 {
76 	struct letsketch_data *data = input_get_drvdata(dev);
77 
78 	hid_hw_close(data->hdev);
79 }
80 
81 static struct input_dev *letsketch_alloc_input_dev(struct letsketch_data *data)
82 {
83 	struct input_dev *input;
84 
85 	input = devm_input_allocate_device(&data->hdev->dev);
86 	if (!input)
87 		return NULL;
88 
89 	input->id.bustype = data->hdev->bus;
90 	input->id.vendor  = data->hdev->vendor;
91 	input->id.product = data->hdev->product;
92 	input->id.version = data->hdev->bus;
93 	input->phys = data->hdev->phys;
94 	input->uniq = data->hdev->uniq;
95 	input->open = letsketch_open;
96 	input->close = letsketch_close;
97 
98 	input_set_drvdata(input, data);
99 
100 	return input;
101 }
102 
103 static int letsketch_setup_input_tablet(struct letsketch_data *data)
104 {
105 	struct input_dev *input;
106 
107 	input = letsketch_alloc_input_dev(data);
108 	if (!input)
109 		return -ENOMEM;
110 
111 	input_set_abs_params(input, ABS_X, 0, 50800, 0, 0);
112 	input_set_abs_params(input, ABS_Y, 0, 31750, 0, 0);
113 	input_set_abs_params(input, ABS_PRESSURE, 0, 8192, 0, 0);
114 	input_abs_set_res(input, ABS_X, 240);
115 	input_abs_set_res(input, ABS_Y, 225);
116 	input_set_capability(input, EV_KEY, BTN_TOUCH);
117 	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
118 	input_set_capability(input, EV_KEY, BTN_STYLUS);
119 	input_set_capability(input, EV_KEY, BTN_STYLUS2);
120 
121 	/* All known brands selling this tablet use WP9620[N] as model name */
122 	input->name = "WP9620 Tablet";
123 
124 	data->input_tablet = input;
125 
126 	return input_register_device(data->input_tablet);
127 }
128 
129 static int letsketch_setup_input_tablet_pad(struct letsketch_data *data)
130 {
131 	struct input_dev *input;
132 	int i;
133 
134 	input = letsketch_alloc_input_dev(data);
135 	if (!input)
136 		return -ENOMEM;
137 
138 	for (i = 0; i < LETSKETCH_PAD_BUTTONS; i++)
139 		input_set_capability(input, EV_KEY, BTN_0 + i);
140 
141 	/*
142 	 * These are never send on the pad input_dev, but must be set
143 	 * on the Pad to make udev / libwacom happy.
144 	 */
145 	input_set_abs_params(input, ABS_X, 0, 1, 0, 0);
146 	input_set_abs_params(input, ABS_Y, 0, 1, 0, 0);
147 	input_set_capability(input, EV_KEY, BTN_STYLUS);
148 
149 	input->name = "WP9620 Pad";
150 
151 	data->input_tablet_pad = input;
152 
153 	return input_register_device(data->input_tablet_pad);
154 }
155 
156 static void letsketch_inrange_timeout(struct timer_list *t)
157 {
158 	struct letsketch_data *data = timer_container_of(data, t,
159 							 inrange_timer);
160 	struct input_dev *input = data->input_tablet;
161 
162 	input_report_key(input, BTN_TOOL_PEN, 0);
163 	input_sync(input);
164 }
165 
166 static int letsketch_raw_event(struct hid_device *hdev,
167 			       struct hid_report *report,
168 			       u8 *raw_data, int size)
169 {
170 	struct letsketch_data *data = hid_get_drvdata(hdev);
171 	struct input_dev *input;
172 	int i;
173 
174 	if (size != LETSKETCH_RAW_DATA_LEN || raw_data[0] != LETSKETCH_RAW_REPORT_ID)
175 		return 0;
176 
177 	switch (raw_data[1] & 0xf0) {
178 	case 0x80: /* Pen data */
179 		input = data->input_tablet;
180 		input_report_key(input, BTN_TOOL_PEN, 1);
181 		input_report_key(input, BTN_TOUCH, raw_data[1] & 0x01);
182 		input_report_key(input, BTN_STYLUS, raw_data[1] & 0x02);
183 		input_report_key(input, BTN_STYLUS2, raw_data[1] & 0x04);
184 		input_report_abs(input, ABS_X,
185 				 get_unaligned_le16(raw_data + 2));
186 		input_report_abs(input, ABS_Y,
187 				 get_unaligned_le16(raw_data + 4));
188 		input_report_abs(input, ABS_PRESSURE,
189 				 get_unaligned_le16(raw_data + 6));
190 		/*
191 		 * There is no out of range event, so use a timer for this
192 		 * when in range we get an event approx. every 8 ms.
193 		 */
194 		mod_timer(&data->inrange_timer, jiffies + msecs_to_jiffies(100));
195 		break;
196 	case 0xe0: /* Pad data */
197 		input = data->input_tablet_pad;
198 		for (i = 0; i < LETSKETCH_PAD_BUTTONS; i++)
199 			input_report_key(input, BTN_0 + i, raw_data[4] == (i + 1));
200 		break;
201 	default:
202 		hid_warn(data->hdev, "Warning unknown data header: 0x%02x\n",
203 			 raw_data[0]);
204 		return 0;
205 	}
206 
207 	input_sync(input);
208 	return 0;
209 }
210 
211 /*
212  * The tablets magic handshake to put it in raw mode relies on getting
213  * string descriptors. But the firmware is buggy and does not like it if
214  * we do this too fast. Even if we go slow sometimes the usb_string() call
215  * fails. Ignore errors and retry it a couple of times if necessary.
216  *
217  * Some firmware revisions (e.g. "Vision_V201_180530", sold as the Starcom
218  * GC-422) do not implement every string descriptor index the handshake
219  * reads. For those indices usb_string() keeps returning an empty string or
220  * -EPIPE (STALL). Report such an index as missing with -ENOENT once the
221  * retries are used up, so that the caller can skip it.
222  */
223 static int letsketch_get_string(struct usb_device *udev, int index, char *buf, int size)
224 {
225 	int i, ret;
226 
227 	for (i = 0; i < LETSKETCH_GET_STRING_RETRIES; i++) {
228 		usleep_range(5000, 7000);
229 		ret = usb_string(udev, index, buf, size);
230 		if (ret > 0)
231 			return 0;
232 	}
233 
234 	if (ret == 0 || ret == -EPIPE) {
235 		dev_dbg(&udev->dev, "String descriptor %d not implemented\n", index);
236 		return -ENOENT;
237 	}
238 
239 	dev_err(&udev->dev, "Max retries (%d) exceeded reading string descriptor %d\n",
240 		LETSKETCH_GET_STRING_RETRIES, index);
241 	return ret;
242 }
243 
244 static int letsketch_probe(struct hid_device *hdev, const struct hid_device_id *id)
245 {
246 	struct device *dev = &hdev->dev;
247 	struct letsketch_data *data;
248 	struct usb_interface *intf;
249 	struct usb_device *udev;
250 	char buf[256];
251 	int i, ret;
252 
253 	if (!hid_is_usb(hdev))
254 		return -ENODEV;
255 
256 	intf = to_usb_interface(hdev->dev.parent);
257 	if (intf->altsetting->desc.bInterfaceNumber != LETSKETCH_RAW_IF)
258 		return -ENODEV; /* Ignore the other interfaces */
259 
260 	udev = interface_to_usbdev(intf);
261 
262 	/*
263 	 * Instead of using a set-feature request, or even a custom USB ctrl
264 	 * message the tablet needs this elaborate magic reading of USB
265 	 * string descriptors to kick it into raw mode. This is what the
266 	 * Windows drivers are seen doing in an USB trace under Windows.
267 	 */
268 	for (i = LETSKETCH_INFO_STR_IDX_BEGIN; i <= LETSKETCH_INFO_STR_IDX_END; i++) {
269 		ret = letsketch_get_string(udev, i, buf, sizeof(buf));
270 		if (ret == -ENOENT)
271 			continue;
272 		if (ret)
273 			return ret;
274 
275 		hid_info(hdev, "Device info: %s\n", buf);
276 	}
277 
278 	for (i = 1; i <= 250; i++) {
279 		ret = letsketch_get_string(udev, i, buf, sizeof(buf));
280 		if (ret && ret != -ENOENT)
281 			return ret;
282 	}
283 
284 	ret = letsketch_get_string(udev, 0x64, buf, sizeof(buf));
285 	if (ret && ret != -ENOENT)
286 		return ret;
287 
288 	ret = letsketch_get_string(udev, LETSKETCH_INFO_STR_IDX_BEGIN, buf, sizeof(buf));
289 	if (ret && ret != -ENOENT)
290 		return ret;
291 
292 	/*
293 	 * The tablet should be in raw mode now, end with a final delay before
294 	 * doing further IO to the device.
295 	 */
296 	usleep_range(5000, 7000);
297 
298 	ret = hid_parse(hdev);
299 	if (ret)
300 		return ret;
301 
302 	data = devm_kzalloc_obj(dev, *data);
303 	if (!data)
304 		return -ENOMEM;
305 
306 	data->hdev = hdev;
307 	timer_setup(&data->inrange_timer, letsketch_inrange_timeout, 0);
308 	hid_set_drvdata(hdev, data);
309 
310 	ret = letsketch_setup_input_tablet(data);
311 	if (ret)
312 		goto err_shutdown_timer;
313 
314 	ret = letsketch_setup_input_tablet_pad(data);
315 	if (ret)
316 		goto err_shutdown_timer;
317 
318 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
319 	if (ret)
320 		goto err_shutdown_timer;
321 
322 	return 0;
323 
324 err_shutdown_timer:
325 	/*
326 	 * Drain any pending callback and permanently disable the timer
327 	 * before devm releases data: if hid_hw_start() enabled I/O on an
328 	 * always-poll-quirk device and then failed, raw_event may have
329 	 * armed the timer already.
330 	 */
331 	timer_shutdown_sync(&data->inrange_timer);
332 	return ret;
333 }
334 
335 static void letsketch_remove(struct hid_device *hdev)
336 {
337 	struct letsketch_data *data = hid_get_drvdata(hdev);
338 
339 	/*
340 	 * hid_hw_stop() synchronously kills the URBs that deliver
341 	 * raw_event(), so once it returns no path can re-arm
342 	 * inrange_timer.  timer_shutdown_sync() then drains any
343 	 * in-flight callback and permanently disables further
344 	 * mod_timer() calls before devm releases data.
345 	 */
346 	hid_hw_stop(hdev);
347 	timer_shutdown_sync(&data->inrange_timer);
348 }
349 
350 static const struct hid_device_id letsketch_devices[] = {
351 	{ HID_USB_DEVICE(USB_VENDOR_ID_LETSKETCH, USB_DEVICE_ID_WP9620N) },
352 	{ }
353 };
354 MODULE_DEVICE_TABLE(hid, letsketch_devices);
355 
356 static struct hid_driver letsketch_driver = {
357 	.name = "letsketch",
358 	.id_table = letsketch_devices,
359 	.probe = letsketch_probe,
360 	.remove = letsketch_remove,
361 	.raw_event = letsketch_raw_event,
362 };
363 module_hid_driver(letsketch_driver);
364 
365 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
366 MODULE_DESCRIPTION("Driver for the LetSketch / VSON WP9620N drawing tablet");
367 MODULE_LICENSE("GPL");
368