xref: /freebsd/sys/dev/hid/hpen.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
5  * Copyright (c) 2019 Greg V <greg@unrelenting.technology>
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 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Generic / MS Windows compatible HID pen tablet driver:
34  * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/required-hid-top-level-collections
35  *
36  * Tested on: Wacom WCOM50C1 (Google Pixelbook "eve")
37  */
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/sysctl.h>
45 
46 #include <dev/evdev/input.h>
47 #include <dev/evdev/evdev.h>
48 
49 #include <dev/hid/hid.h>
50 #include <dev/hid/hidbus.h>
51 #include <dev/hid/hidmap.h>
52 #include <dev/hid/hidrdesc.h>
53 
54 #include "usbdevs.h"
55 
56 static const uint8_t	hpen_graphire_report_descr[] =
57 			   { HID_GRAPHIRE_REPORT_DESCR() };
58 static const uint8_t	hpen_graphire3_4x5_report_descr[] =
59 			   { HID_GRAPHIRE3_4X5_REPORT_DESCR() };
60 
61 static hidmap_cb_t	hpen_battery_strenght_cb;
62 static hidmap_cb_t	hpen_final_digi_cb;
63 static hidmap_cb_t	hpen_final_pen_cb;
64 
65 #define HPEN_MAP_BUT(usage, code)	\
66 	HIDMAP_KEY(HUP_DIGITIZERS, HUD_##usage, code)
67 #define HPEN_MAP_ABS(usage, code)	\
68 	HIDMAP_ABS(HUP_DIGITIZERS, HUD_##usage, code)
69 #define HPEN_MAP_ABS_GD(usage, code)	\
70 	HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code)
71 #define HPEN_MAP_ABS_CB(usage, cb)	\
72 	HIDMAP_ABS_CB(HUP_DIGITIZERS, HUD_##usage, &cb)
73 
74 /* Generic map digitizer page map according to hut1_12v2.pdf */
75 static const struct hidmap_item hpen_map_pen[] = {
76     { HPEN_MAP_ABS_GD(X,		ABS_X),		  .required = true },
77     { HPEN_MAP_ABS_GD(Y,		ABS_Y),		  .required = true },
78     { HPEN_MAP_ABS(   TIP_PRESSURE,	ABS_PRESSURE) },
79     { HPEN_MAP_ABS(   X_TILT,		ABS_TILT_X) },
80     { HPEN_MAP_ABS(   Y_TILT,		ABS_TILT_Y) },
81     { HPEN_MAP_ABS(   CONTACTID,	0), 		  .forbidden = true },
82     { HPEN_MAP_ABS(   CONTACTCOUNT,	0), 		  .forbidden = true },
83     { HPEN_MAP_ABS_CB(BATTERY_STRENGTH,	hpen_battery_strenght_cb) },
84     { HPEN_MAP_BUT(   TOUCH,		BTN_TOUCH) },
85     { HPEN_MAP_BUT(   TIP_SWITCH,	BTN_TOUCH) },
86     { HPEN_MAP_BUT(   SEC_TIP_SWITCH,	BTN_TOUCH) },
87     { HPEN_MAP_BUT(   BARREL_SWITCH,	BTN_STYLUS) },
88     { HPEN_MAP_BUT(   INVERT,		BTN_TOOL_RUBBER) },
89     { HPEN_MAP_BUT(   ERASER,		BTN_TOUCH) },
90     { HPEN_MAP_BUT(   TABLET_PICK,	BTN_STYLUS2) },
91     { HPEN_MAP_BUT(   SEC_BARREL_SWITCH,BTN_STYLUS2) },
92     { HIDMAP_FINAL_CB(			&hpen_final_pen_cb) },
93 };
94 
95 static const struct hidmap_item hpen_map_stylus[] = {
96     { HPEN_MAP_BUT(   IN_RANGE,		BTN_TOOL_PEN) },
97 };
98 static const struct hidmap_item hpen_map_finger[] = {
99     { HPEN_MAP_BUT(   IN_RANGE,		BTN_TOOL_FINGER) },
100 };
101 
102 static const struct hid_device_id hpen_devs[] = {
103 	{ HID_TLC(HUP_DIGITIZERS, HUD_DIGITIZER) },
104 	{ HID_TLC(HUP_DIGITIZERS, HUD_PEN) },
105 	{ HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN),
106 	  HID_BVP(BUS_USB, USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL) },
107 };
108 
109 /* Do not autoload legacy pen driver for all touchscreen */
110 static const struct hid_device_id hpen_devs_no_load[] = {
111 	{ HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) },
112 };
113 
114 static int
115 hpen_battery_strenght_cb(HIDMAP_CB_ARGS)
116 {
117 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
118 	int32_t data;
119 
120 	switch (HIDMAP_CB_GET_STATE()) {
121 	case HIDMAP_CB_IS_ATTACHING:
122 		evdev_support_event(evdev, EV_PWR);
123 		/* TODO */
124 		break;
125 	case HIDMAP_CB_IS_RUNNING:
126 		data = ctx.data;
127 		/* TODO */
128 		break;
129 	default:
130 		break;
131 	}
132 
133 	return (0);
134 }
135 
136 static int
137 hpen_final_pen_cb(HIDMAP_CB_ARGS)
138 {
139 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
140 
141 	if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
142 		if (hidbus_get_usage(HIDMAP_CB_GET_DEV()) ==
143 		    HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER))
144 			evdev_support_prop(evdev, INPUT_PROP_POINTER);
145 		else
146 			evdev_support_prop(evdev, INPUT_PROP_DIRECT);
147 	}
148 
149 	/* Do not execute callback at interrupt handler and detach */
150 	return (ENOSYS);
151 }
152 
153 static void
154 hpen_identify(driver_t *driver, device_t parent)
155 {
156 	const struct hid_device_info *hw = hid_get_device_info(parent);
157 
158 	/* the report descriptor for the Wacom Graphire is broken */
159 	if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM) {
160 		switch (hw->idProduct) {
161 		case USB_PRODUCT_WACOM_GRAPHIRE:
162 			hid_set_report_descr(parent,
163 			    hpen_graphire_report_descr,
164 			    sizeof(hpen_graphire_report_descr));
165 			break;
166 
167 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
168 			hid_set_report_descr(parent,
169 			    hpen_graphire3_4x5_report_descr,
170 			    sizeof(hpen_graphire3_4x5_report_descr));
171 			break;
172 		}
173 	}
174 }
175 
176 static int
177 hpen_probe(device_t dev)
178 {
179 	struct hidmap *hm = device_get_softc(dev);
180 	const char *desc;
181 	void *d_ptr;
182 	hid_size_t d_len;
183 	int error;
184 
185 	if (HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs_no_load) != 0) {
186 		error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs);
187 		if (error != 0)
188 			return (error);
189 	}
190 
191 	hidmap_set_dev(hm, dev);
192 
193 	/* Check if report descriptor belongs to a HID pen device */
194 	error = HIDMAP_ADD_MAP(hm, hpen_map_pen, NULL);
195 	if (error != 0)
196 		return (error);
197 
198 	if (hid_get_report_descr(dev, &d_ptr, &d_len) != 0)
199 		return (ENXIO);
200 
201 	if (hidbus_is_collection(d_ptr, d_len,
202 	    HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER), hidbus_get_index(dev))) {
203 		HIDMAP_ADD_MAP(hm, hpen_map_finger, NULL);
204 		desc = "TouchScreen";
205 	} else {
206 		HIDMAP_ADD_MAP(hm, hpen_map_stylus, NULL);
207 		desc = "Pen";
208 	}
209 	if (hidbus_get_usage(dev) == HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER))
210 		desc = "Digitizer";
211 
212 	hidbus_set_desc(dev, desc);
213 
214 	return (BUS_PROBE_DEFAULT);
215 }
216 
217 static int
218 hpen_attach(device_t dev)
219 {
220 	const struct hid_device_info *hw = hid_get_device_info(dev);
221 	struct hidmap *hm = device_get_softc(dev);
222 	int error;
223 
224 	if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM &&
225 	    hw->idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
226 		/*
227 		 * The Graphire3 needs 0x0202 to be written to
228 		 * feature report ID 2 before it'll start
229 		 * returning digitizer data.
230 		 */
231 		static const uint8_t reportbuf[3] = {2, 2, 2};
232 		error = hid_set_report(dev, reportbuf, sizeof(reportbuf),
233 		    HID_FEATURE_REPORT, reportbuf[0]);
234 		if (error)
235 			device_printf(dev, "set feature report failed, "
236 			    "error=%d (ignored)\n", error);
237 	}
238 
239 	return (hidmap_attach(hm));
240 }
241 
242 static int
243 hpen_detach(device_t dev)
244 {
245 	return (hidmap_detach(device_get_softc(dev)));
246 }
247 
248 
249 static devclass_t hpen_devclass;
250 static device_method_t hpen_methods[] = {
251 	DEVMETHOD(device_identify,	hpen_identify),
252 	DEVMETHOD(device_probe,		hpen_probe),
253 	DEVMETHOD(device_attach,	hpen_attach),
254 	DEVMETHOD(device_detach,	hpen_detach),
255 
256 	DEVMETHOD_END
257 };
258 
259 DEFINE_CLASS_0(hpen, hpen_driver, hpen_methods, sizeof(struct hidmap));
260 DRIVER_MODULE(hpen, hidbus, hpen_driver, hpen_devclass, NULL, 0);
261 MODULE_DEPEND(hpen, hid, 1, 1, 1);
262 MODULE_DEPEND(hpen, hidbus, 1, 1, 1);
263 MODULE_DEPEND(hpen, hidmap, 1, 1, 1);
264 MODULE_DEPEND(hpen, evdev, 1, 1, 1);
265 MODULE_VERSION(hpen, 1);
266 HID_PNP_INFO(hpen_devs);
267