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