xref: /freebsd/sys/dev/hid/hpen.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1d97d5c0cSVladimir Kondratyev /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d97d5c0cSVladimir Kondratyev  *
4d97d5c0cSVladimir Kondratyev  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
50b453151SVal Packett  * Copyright (c) 2019 Val Packett <val@packett.cool>
6d97d5c0cSVladimir Kondratyev  *
7d97d5c0cSVladimir Kondratyev  * Redistribution and use in source and binary forms, with or without
8d97d5c0cSVladimir Kondratyev  * modification, are permitted provided that the following conditions
9d97d5c0cSVladimir Kondratyev  * are met:
10d97d5c0cSVladimir Kondratyev  * 1. Redistributions of source code must retain the above copyright
11d97d5c0cSVladimir Kondratyev  *    notice, this list of conditions and the following disclaimer.
12d97d5c0cSVladimir Kondratyev  * 2. Redistributions in binary form must reproduce the above copyright
13d97d5c0cSVladimir Kondratyev  *    notice, this list of conditions and the following disclaimer in the
14d97d5c0cSVladimir Kondratyev  *    documentation and/or other materials provided with the distribution.
15d97d5c0cSVladimir Kondratyev  *
16d97d5c0cSVladimir Kondratyev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17d97d5c0cSVladimir Kondratyev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d97d5c0cSVladimir Kondratyev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d97d5c0cSVladimir Kondratyev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20d97d5c0cSVladimir Kondratyev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d97d5c0cSVladimir Kondratyev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d97d5c0cSVladimir Kondratyev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d97d5c0cSVladimir Kondratyev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d97d5c0cSVladimir Kondratyev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d97d5c0cSVladimir Kondratyev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d97d5c0cSVladimir Kondratyev  * SUCH DAMAGE.
27d97d5c0cSVladimir Kondratyev  */
28d97d5c0cSVladimir Kondratyev 
29d97d5c0cSVladimir Kondratyev #include <sys/cdefs.h>
30d97d5c0cSVladimir Kondratyev __FBSDID("$FreeBSD$");
31d97d5c0cSVladimir Kondratyev 
32d97d5c0cSVladimir Kondratyev /*
33d97d5c0cSVladimir Kondratyev  * Generic / MS Windows compatible HID pen tablet driver:
34d97d5c0cSVladimir Kondratyev  * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/required-hid-top-level-collections
35d97d5c0cSVladimir Kondratyev  *
36d97d5c0cSVladimir Kondratyev  * Tested on: Wacom WCOM50C1 (Google Pixelbook "eve")
37d97d5c0cSVladimir Kondratyev  */
38d97d5c0cSVladimir Kondratyev 
39d97d5c0cSVladimir Kondratyev #include <sys/param.h>
40d97d5c0cSVladimir Kondratyev #include <sys/bus.h>
41d97d5c0cSVladimir Kondratyev #include <sys/kernel.h>
42d97d5c0cSVladimir Kondratyev #include <sys/malloc.h>
43d97d5c0cSVladimir Kondratyev #include <sys/module.h>
44d97d5c0cSVladimir Kondratyev #include <sys/sysctl.h>
45d97d5c0cSVladimir Kondratyev 
46d97d5c0cSVladimir Kondratyev #include <dev/evdev/input.h>
47d97d5c0cSVladimir Kondratyev #include <dev/evdev/evdev.h>
48d97d5c0cSVladimir Kondratyev 
49d97d5c0cSVladimir Kondratyev #include <dev/hid/hid.h>
50d97d5c0cSVladimir Kondratyev #include <dev/hid/hidbus.h>
51d97d5c0cSVladimir Kondratyev #include <dev/hid/hidmap.h>
52d97d5c0cSVladimir Kondratyev #include <dev/hid/hidrdesc.h>
53d97d5c0cSVladimir Kondratyev 
54d97d5c0cSVladimir Kondratyev #include "usbdevs.h"
55d97d5c0cSVladimir Kondratyev 
56d97d5c0cSVladimir Kondratyev static const uint8_t	hpen_graphire_report_descr[] =
57d97d5c0cSVladimir Kondratyev 			   { HID_GRAPHIRE_REPORT_DESCR() };
58d97d5c0cSVladimir Kondratyev static const uint8_t	hpen_graphire3_4x5_report_descr[] =
59d97d5c0cSVladimir Kondratyev 			   { HID_GRAPHIRE3_4X5_REPORT_DESCR() };
60d97d5c0cSVladimir Kondratyev 
61d97d5c0cSVladimir Kondratyev static hidmap_cb_t	hpen_battery_strenght_cb;
62d97d5c0cSVladimir Kondratyev static hidmap_cb_t	hpen_final_pen_cb;
63d97d5c0cSVladimir Kondratyev 
64d97d5c0cSVladimir Kondratyev #define HPEN_MAP_BUT(usage, code)	\
65d97d5c0cSVladimir Kondratyev 	HIDMAP_KEY(HUP_DIGITIZERS, HUD_##usage, code)
66d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS(usage, code)	\
67d97d5c0cSVladimir Kondratyev 	HIDMAP_ABS(HUP_DIGITIZERS, HUD_##usage, code)
68d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS_GD(usage, code)	\
69d97d5c0cSVladimir Kondratyev 	HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code)
70d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS_CB(usage, cb)	\
71d97d5c0cSVladimir Kondratyev 	HIDMAP_ABS_CB(HUP_DIGITIZERS, HUD_##usage, &cb)
72d97d5c0cSVladimir Kondratyev 
73d97d5c0cSVladimir Kondratyev /* Generic map digitizer page map according to hut1_12v2.pdf */
74a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_pen[] = {
75d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS_GD(X,		ABS_X),		  .required = true },
76d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS_GD(Y,		ABS_Y),		  .required = true },
77d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS(   TIP_PRESSURE,	ABS_PRESSURE) },
78d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS(   X_TILT,		ABS_TILT_X) },
79d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS(   Y_TILT,		ABS_TILT_Y) },
80a36bdfc2SVladimir Kondratyev     { HPEN_MAP_ABS(   CONTACTID,	0), 		  .forbidden = true },
81a36bdfc2SVladimir Kondratyev     { HPEN_MAP_ABS(   CONTACTCOUNT,	0), 		  .forbidden = true },
82d97d5c0cSVladimir Kondratyev     { HPEN_MAP_ABS_CB(BATTERY_STRENGTH,	hpen_battery_strenght_cb) },
83d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   TOUCH,		BTN_TOUCH) },
84d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   TIP_SWITCH,	BTN_TOUCH) },
85d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   SEC_TIP_SWITCH,	BTN_TOUCH) },
86d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   BARREL_SWITCH,	BTN_STYLUS) },
87d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   INVERT,		BTN_TOOL_RUBBER) },
88d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   ERASER,		BTN_TOUCH) },
89d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   TABLET_PICK,	BTN_STYLUS2) },
90d97d5c0cSVladimir Kondratyev     { HPEN_MAP_BUT(   SEC_BARREL_SWITCH,BTN_STYLUS2) },
91a36bdfc2SVladimir Kondratyev     { HIDMAP_FINAL_CB(			&hpen_final_pen_cb) },
92d97d5c0cSVladimir Kondratyev };
93d97d5c0cSVladimir Kondratyev 
94a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_stylus[] = {
95a36bdfc2SVladimir Kondratyev     { HPEN_MAP_BUT(   IN_RANGE,		BTN_TOOL_PEN) },
96a36bdfc2SVladimir Kondratyev };
97a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_finger[] = {
98a36bdfc2SVladimir Kondratyev     { HPEN_MAP_BUT(   IN_RANGE,		BTN_TOOL_FINGER) },
99d97d5c0cSVladimir Kondratyev };
100d97d5c0cSVladimir Kondratyev 
101d97d5c0cSVladimir Kondratyev static const struct hid_device_id hpen_devs[] = {
102d97d5c0cSVladimir Kondratyev 	{ HID_TLC(HUP_DIGITIZERS, HUD_DIGITIZER) },
103d97d5c0cSVladimir Kondratyev 	{ HID_TLC(HUP_DIGITIZERS, HUD_PEN) },
104a36bdfc2SVladimir Kondratyev 	{ HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN),
105a36bdfc2SVladimir Kondratyev 	  HID_BVP(BUS_USB, USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL) },
106a36bdfc2SVladimir Kondratyev };
107a36bdfc2SVladimir Kondratyev 
108a36bdfc2SVladimir Kondratyev /* Do not autoload legacy pen driver for all touchscreen */
109a36bdfc2SVladimir Kondratyev static const struct hid_device_id hpen_devs_no_load[] = {
110a36bdfc2SVladimir Kondratyev 	{ HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) },
111d97d5c0cSVladimir Kondratyev };
112d97d5c0cSVladimir Kondratyev 
113d97d5c0cSVladimir Kondratyev static int
114d97d5c0cSVladimir Kondratyev hpen_battery_strenght_cb(HIDMAP_CB_ARGS)
115d97d5c0cSVladimir Kondratyev {
116d97d5c0cSVladimir Kondratyev 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
117d97d5c0cSVladimir Kondratyev 
118d97d5c0cSVladimir Kondratyev 	switch (HIDMAP_CB_GET_STATE()) {
119d97d5c0cSVladimir Kondratyev 	case HIDMAP_CB_IS_ATTACHING:
120d97d5c0cSVladimir Kondratyev 		evdev_support_event(evdev, EV_PWR);
121d97d5c0cSVladimir Kondratyev 		/* TODO */
122d97d5c0cSVladimir Kondratyev 		break;
123d97d5c0cSVladimir Kondratyev 	case HIDMAP_CB_IS_RUNNING:
124d97d5c0cSVladimir Kondratyev 		/* TODO */
12516079c72SRyan Libby 		break;
12616079c72SRyan Libby 	default:
12716079c72SRyan Libby 		break;
128d97d5c0cSVladimir Kondratyev 	}
129d97d5c0cSVladimir Kondratyev 
130d97d5c0cSVladimir Kondratyev 	return (0);
131d97d5c0cSVladimir Kondratyev }
132d97d5c0cSVladimir Kondratyev 
133d97d5c0cSVladimir Kondratyev static int
134d97d5c0cSVladimir Kondratyev hpen_final_pen_cb(HIDMAP_CB_ARGS)
135d97d5c0cSVladimir Kondratyev {
136d97d5c0cSVladimir Kondratyev 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
137d97d5c0cSVladimir Kondratyev 
138a36bdfc2SVladimir Kondratyev 	if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
139a36bdfc2SVladimir Kondratyev 		if (hidbus_get_usage(HIDMAP_CB_GET_DEV()) ==
140a36bdfc2SVladimir Kondratyev 		    HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER))
141a36bdfc2SVladimir Kondratyev 			evdev_support_prop(evdev, INPUT_PROP_POINTER);
142a36bdfc2SVladimir Kondratyev 		else
143d97d5c0cSVladimir Kondratyev 			evdev_support_prop(evdev, INPUT_PROP_DIRECT);
144a36bdfc2SVladimir Kondratyev 	}
145d97d5c0cSVladimir Kondratyev 
146d97d5c0cSVladimir Kondratyev 	/* Do not execute callback at interrupt handler and detach */
147d97d5c0cSVladimir Kondratyev 	return (ENOSYS);
148d97d5c0cSVladimir Kondratyev }
149d97d5c0cSVladimir Kondratyev 
150d97d5c0cSVladimir Kondratyev static void
151d97d5c0cSVladimir Kondratyev hpen_identify(driver_t *driver, device_t parent)
152d97d5c0cSVladimir Kondratyev {
153d97d5c0cSVladimir Kondratyev 	const struct hid_device_info *hw = hid_get_device_info(parent);
154d97d5c0cSVladimir Kondratyev 
155d97d5c0cSVladimir Kondratyev 	/* the report descriptor for the Wacom Graphire is broken */
156d97d5c0cSVladimir Kondratyev 	if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM) {
157d97d5c0cSVladimir Kondratyev 		switch (hw->idProduct) {
158d97d5c0cSVladimir Kondratyev 		case USB_PRODUCT_WACOM_GRAPHIRE:
159d97d5c0cSVladimir Kondratyev 			hid_set_report_descr(parent,
160d97d5c0cSVladimir Kondratyev 			    hpen_graphire_report_descr,
161d97d5c0cSVladimir Kondratyev 			    sizeof(hpen_graphire_report_descr));
162d97d5c0cSVladimir Kondratyev 			break;
163d97d5c0cSVladimir Kondratyev 
164d97d5c0cSVladimir Kondratyev 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
165d97d5c0cSVladimir Kondratyev 			hid_set_report_descr(parent,
166d97d5c0cSVladimir Kondratyev 			    hpen_graphire3_4x5_report_descr,
167d97d5c0cSVladimir Kondratyev 			    sizeof(hpen_graphire3_4x5_report_descr));
168d97d5c0cSVladimir Kondratyev 			break;
169d97d5c0cSVladimir Kondratyev 		}
170d97d5c0cSVladimir Kondratyev 	}
171d97d5c0cSVladimir Kondratyev }
172d97d5c0cSVladimir Kondratyev 
173d97d5c0cSVladimir Kondratyev static int
174d97d5c0cSVladimir Kondratyev hpen_probe(device_t dev)
175d97d5c0cSVladimir Kondratyev {
176d97d5c0cSVladimir Kondratyev 	struct hidmap *hm = device_get_softc(dev);
177a36bdfc2SVladimir Kondratyev 	const char *desc;
178a36bdfc2SVladimir Kondratyev 	void *d_ptr;
179a36bdfc2SVladimir Kondratyev 	hid_size_t d_len;
180d97d5c0cSVladimir Kondratyev 	int error;
181d97d5c0cSVladimir Kondratyev 
182a36bdfc2SVladimir Kondratyev 	if (HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs_no_load) != 0) {
183d97d5c0cSVladimir Kondratyev 		error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs);
184d97d5c0cSVladimir Kondratyev 		if (error != 0)
185d97d5c0cSVladimir Kondratyev 			return (error);
186a36bdfc2SVladimir Kondratyev 	}
187d97d5c0cSVladimir Kondratyev 
188d97d5c0cSVladimir Kondratyev 	hidmap_set_dev(hm, dev);
189d97d5c0cSVladimir Kondratyev 
190a36bdfc2SVladimir Kondratyev 	/* Check if report descriptor belongs to a HID pen device */
191a36bdfc2SVladimir Kondratyev 	error = HIDMAP_ADD_MAP(hm, hpen_map_pen, NULL);
192d97d5c0cSVladimir Kondratyev 	if (error != 0)
193d97d5c0cSVladimir Kondratyev 		return (error);
194d97d5c0cSVladimir Kondratyev 
195a36bdfc2SVladimir Kondratyev 	if (hid_get_report_descr(dev, &d_ptr, &d_len) != 0)
196a36bdfc2SVladimir Kondratyev 		return (ENXIO);
197a36bdfc2SVladimir Kondratyev 
198a36bdfc2SVladimir Kondratyev 	if (hidbus_is_collection(d_ptr, d_len,
199a36bdfc2SVladimir Kondratyev 	    HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER), hidbus_get_index(dev))) {
200a36bdfc2SVladimir Kondratyev 		HIDMAP_ADD_MAP(hm, hpen_map_finger, NULL);
201a36bdfc2SVladimir Kondratyev 		desc = "TouchScreen";
202a36bdfc2SVladimir Kondratyev 	} else {
203a36bdfc2SVladimir Kondratyev 		HIDMAP_ADD_MAP(hm, hpen_map_stylus, NULL);
204a36bdfc2SVladimir Kondratyev 		desc = "Pen";
205a36bdfc2SVladimir Kondratyev 	}
206a36bdfc2SVladimir Kondratyev 	if (hidbus_get_usage(dev) == HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER))
207a36bdfc2SVladimir Kondratyev 		desc = "Digitizer";
208a36bdfc2SVladimir Kondratyev 
209a36bdfc2SVladimir Kondratyev 	hidbus_set_desc(dev, desc);
210d97d5c0cSVladimir Kondratyev 
211d97d5c0cSVladimir Kondratyev 	return (BUS_PROBE_DEFAULT);
212d97d5c0cSVladimir Kondratyev }
213d97d5c0cSVladimir Kondratyev 
214d97d5c0cSVladimir Kondratyev static int
215d97d5c0cSVladimir Kondratyev hpen_attach(device_t dev)
216d97d5c0cSVladimir Kondratyev {
217d97d5c0cSVladimir Kondratyev 	const struct hid_device_info *hw = hid_get_device_info(dev);
218d97d5c0cSVladimir Kondratyev 	struct hidmap *hm = device_get_softc(dev);
219d97d5c0cSVladimir Kondratyev 	int error;
220d97d5c0cSVladimir Kondratyev 
221d97d5c0cSVladimir Kondratyev 	if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM &&
222d97d5c0cSVladimir Kondratyev 	    hw->idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
223d97d5c0cSVladimir Kondratyev 		/*
224d97d5c0cSVladimir Kondratyev 		 * The Graphire3 needs 0x0202 to be written to
225d97d5c0cSVladimir Kondratyev 		 * feature report ID 2 before it'll start
226d97d5c0cSVladimir Kondratyev 		 * returning digitizer data.
227d97d5c0cSVladimir Kondratyev 		 */
228d97d5c0cSVladimir Kondratyev 		static const uint8_t reportbuf[3] = {2, 2, 2};
229d97d5c0cSVladimir Kondratyev 		error = hid_set_report(dev, reportbuf, sizeof(reportbuf),
230d97d5c0cSVladimir Kondratyev 		    HID_FEATURE_REPORT, reportbuf[0]);
231d97d5c0cSVladimir Kondratyev 		if (error)
232d97d5c0cSVladimir Kondratyev 			device_printf(dev, "set feature report failed, "
233d97d5c0cSVladimir Kondratyev 			    "error=%d (ignored)\n", error);
234d97d5c0cSVladimir Kondratyev 	}
235d97d5c0cSVladimir Kondratyev 
236d97d5c0cSVladimir Kondratyev 	return (hidmap_attach(hm));
237d97d5c0cSVladimir Kondratyev }
238d97d5c0cSVladimir Kondratyev 
239d97d5c0cSVladimir Kondratyev static int
240d97d5c0cSVladimir Kondratyev hpen_detach(device_t dev)
241d97d5c0cSVladimir Kondratyev {
242d97d5c0cSVladimir Kondratyev 	return (hidmap_detach(device_get_softc(dev)));
243d97d5c0cSVladimir Kondratyev }
244d97d5c0cSVladimir Kondratyev 
245d97d5c0cSVladimir Kondratyev 
246d97d5c0cSVladimir Kondratyev static device_method_t hpen_methods[] = {
247d97d5c0cSVladimir Kondratyev 	DEVMETHOD(device_identify,	hpen_identify),
248d97d5c0cSVladimir Kondratyev 	DEVMETHOD(device_probe,		hpen_probe),
249d97d5c0cSVladimir Kondratyev 	DEVMETHOD(device_attach,	hpen_attach),
250d97d5c0cSVladimir Kondratyev 	DEVMETHOD(device_detach,	hpen_detach),
251d97d5c0cSVladimir Kondratyev 
252d97d5c0cSVladimir Kondratyev 	DEVMETHOD_END
253d97d5c0cSVladimir Kondratyev };
254d97d5c0cSVladimir Kondratyev 
255d97d5c0cSVladimir Kondratyev DEFINE_CLASS_0(hpen, hpen_driver, hpen_methods, sizeof(struct hidmap));
2567eeede15SJohn Baldwin DRIVER_MODULE(hpen, hidbus, hpen_driver, NULL, NULL);
257d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hid, 1, 1, 1);
258d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hidbus, 1, 1, 1);
259d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hidmap, 1, 1, 1);
260d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, evdev, 1, 1, 1);
261d97d5c0cSVladimir Kondratyev MODULE_VERSION(hpen, 1);
262d97d5c0cSVladimir Kondratyev HID_PNP_INFO(hpen_devs);
263