xref: /freebsd/sys/dev/hid/hconf.c (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
5  * Copyright (c) 2020 Andriy Gapon <avg@FreeBSD.org>
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  * Digitizer configuration top-level collection support.
34  * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections
35  */
36 
37 #include "opt_hid.h"
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 #include <sys/sx.h>
48 
49 #define	HID_DEBUG_VAR	hconf_debug
50 #include <dev/hid/hid.h>
51 #include <dev/hid/hidbus.h>
52 
53 #include <dev/hid/hconf.h>
54 
55 #ifdef HID_DEBUG
56 static int hconf_debug = 0;
57 
58 static SYSCTL_NODE(_hw_hid, OID_AUTO, hconf, CTLFLAG_RW, 0,
59     "Digitizer configuration top-level collection");
60 SYSCTL_INT(_hw_hid_hconf, OID_AUTO, debug, CTLFLAG_RWTUN,
61     &hconf_debug, 1, "Debug level");
62 #endif
63 
64 enum feature_control_type {
65 	INPUT_MODE = 0,
66 	SURFACE_SWITCH,
67 	BUTTONS_SWITCH,
68 	CONTROLS_COUNT
69 };
70 
71 struct feature_control_descr {
72 	const char	*name;
73 	const char	*descr;
74 	uint16_t	usage;
75 	u_int		value;
76 } feature_control_descrs[] = {
77 	[INPUT_MODE] = {
78 		.name = "input_mode",
79 		.descr = "HID device input mode: 0 = mouse, 3 = touchpad",
80 		.usage = HUD_INPUT_MODE,
81 		.value = HCONF_INPUT_MODE_MOUSE,
82 	},
83 	[SURFACE_SWITCH] = {
84 		.name = "surface_switch",
85 		.descr = "Enable / disable switch for surface: 1 = on, 0 = off",
86 		.usage = HUD_SURFACE_SWITCH,
87 		.value = 1,
88 	},
89 	[BUTTONS_SWITCH] = {
90 		.name = "buttons_switch",
91 		.descr = "Enable / disable switch for buttons: 1 = on, 0 = off",
92 		.usage = HUD_BUTTONS_SWITCH,
93 		.value = 1,
94 	},
95 };
96 
97 struct feature_control {
98 	u_int			val;
99 	struct hid_location	loc;
100 	hid_size_t		rlen;
101 	uint8_t			rid;
102 };
103 
104 struct hconf_softc {
105 	device_t		dev;
106 	struct sx		lock;
107 
108 	struct feature_control	feature_controls[CONTROLS_COUNT];
109 };
110 
111 static device_probe_t		hconf_probe;
112 static device_attach_t		hconf_attach;
113 static device_detach_t		hconf_detach;
114 static device_resume_t		hconf_resume;
115 
116 static devclass_t hconf_devclass;
117 
118 static device_method_t hconf_methods[] = {
119 
120 	DEVMETHOD(device_probe,		hconf_probe),
121 	DEVMETHOD(device_attach,	hconf_attach),
122 	DEVMETHOD(device_detach,	hconf_detach),
123 	DEVMETHOD(device_resume,	hconf_resume),
124 
125 	DEVMETHOD_END
126 };
127 
128 static driver_t hconf_driver = {
129 	.name = "hconf",
130 	.methods = hconf_methods,
131 	.size = sizeof(struct hconf_softc),
132 };
133 
134 static const struct hid_device_id hconf_devs[] = {
135 	{ HID_TLC(HUP_DIGITIZERS, HUD_CONFIG) },
136 };
137 
138 static int
139 hconf_set_feature_control(struct hconf_softc *sc, int ctrl_id, u_int val)
140 {
141 	struct feature_control *fc;
142 	uint8_t *fbuf;
143 	int error;
144 	int i;
145 
146 	KASSERT(ctrl_id >= 0 && ctrl_id < CONTROLS_COUNT,
147 	    ("impossible ctrl id %d", ctrl_id));
148 	fc = &sc->feature_controls[ctrl_id];
149 	if (fc->rlen <= 1)
150 		return (ENXIO);
151 
152 	fbuf = malloc(fc->rlen, M_TEMP, M_WAITOK | M_ZERO);
153 	sx_xlock(&sc->lock);
154 
155 	/*
156 	 * Assume the report is write-only. Then we have to check for other
157 	 * controls that may share the same report and set their bits as well.
158 	 */
159 	bzero(fbuf + 1, fc->rlen - 1);
160 	for (i = 0; i < nitems(sc->feature_controls); i++) {
161 		struct feature_control *ofc = &sc->feature_controls[i];
162 
163 		/* Skip unrelated report IDs. */
164 		if (ofc->rid != fc->rid)
165 			continue;
166 		KASSERT(fc->rlen == ofc->rlen,
167 		    ("different lengths for report %d: %d vs %d\n",
168 		    fc->rid, fc->rlen, ofc->rlen));
169 		hid_put_udata(fbuf + 1, ofc->rlen - 1, &ofc->loc,
170 		    i == ctrl_id ? val : ofc->val);
171 	}
172 
173 	fbuf[0] = fc->rid;
174 
175 	error = hid_set_report(sc->dev, fbuf, fc->rlen,
176 	    HID_FEATURE_REPORT, fc->rid);
177 	if (error == 0)
178 		fc->val = val;
179 
180 	sx_unlock(&sc->lock);
181 	free(fbuf, M_TEMP);
182 
183 	return (error);
184 }
185 
186 static int
187 hconf_feature_control_handler(SYSCTL_HANDLER_ARGS)
188 {
189 	struct feature_control *fc;
190 	struct hconf_softc *sc = arg1;
191 	int ctrl_id = arg2;
192 	u_int value;
193 	int error;
194 
195 	if (ctrl_id < 0 || ctrl_id >= CONTROLS_COUNT)
196 		return (ENXIO);
197 
198 	fc = &sc->feature_controls[ctrl_id];
199 	value = fc->val;
200 	error = sysctl_handle_int(oidp, &value, 0, req);
201 	if (error != 0 || req->newptr == NULL)
202 		return (error);
203 
204 	error = hconf_set_feature_control(sc, ctrl_id, value);
205 	if (error != 0) {
206 		DPRINTF("Failed to set %s: %d\n",
207 		    feature_control_descrs[ctrl_id].name, error);
208 	}
209 	return (0);
210 }
211 
212 
213 static int
214 hconf_parse_feature(struct feature_control *fc, uint8_t tlc_index,
215     uint16_t usage, void *d_ptr, hid_size_t d_len)
216 {
217 	uint32_t flags;
218 
219 	if (!hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_DIGITIZERS, usage),
220 	    hid_feature, tlc_index, 0, &fc->loc, &flags, &fc->rid, NULL))
221 		return (ENOENT);
222 
223 	if ((flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
224 		return (EINVAL);
225 
226 	fc->rlen = hid_report_size(d_ptr, d_len, hid_feature, fc->rid);
227 	return (0);
228 }
229 
230 static int
231 hconf_probe(device_t dev)
232 {
233 	int error;
234 
235 	error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hconf_devs);
236 	if (error != 0)
237 		return (error);
238 
239 	hidbus_set_desc(dev, "Configuration");
240 
241 	return (BUS_PROBE_DEFAULT);
242 }
243 
244 static int
245 hconf_attach(device_t dev)
246 {
247 	struct hconf_softc *sc = device_get_softc(dev);
248 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
249 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
250 	void *d_ptr;
251 	hid_size_t d_len;
252 	uint8_t tlc_index;
253 	int error;
254 	int i;
255 
256 	error = hid_get_report_descr(dev, &d_ptr, &d_len);
257 	if (error) {
258 		device_printf(dev, "could not retrieve report descriptor from "
259 		    "device: %d\n", error);
260 		return (ENXIO);
261 	}
262 
263 	sc->dev = dev;
264 	sx_init(&sc->lock, device_get_nameunit(dev));
265 
266 	tlc_index = hidbus_get_index(dev);
267 	for (i = 0; i < nitems(sc->feature_controls); i++) {
268 		(void)hconf_parse_feature(&sc->feature_controls[i], tlc_index,
269 		    feature_control_descrs[i].usage, d_ptr, d_len);
270 		if (sc->feature_controls[i].rlen > 1) {
271 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
272 			    feature_control_descrs[i].name,
273 			    CTLTYPE_UINT | CTLFLAG_RW,
274 			    sc, i, hconf_feature_control_handler, "I",
275 			    feature_control_descrs[i].descr);
276 		}
277 		sc->feature_controls[i].val = feature_control_descrs[i].value;
278 	}
279 
280 	return (0);
281 }
282 
283 static int
284 hconf_detach(device_t dev)
285 {
286 	struct hconf_softc *sc = device_get_softc(dev);
287 
288 	sx_destroy(&sc->lock);
289 
290 	return (0);
291 }
292 
293 static int
294 hconf_resume(device_t dev)
295 {
296 	struct hconf_softc *sc = device_get_softc(dev);
297 	int error;
298 	int i;
299 
300 	for (i = 0; i < nitems(sc->feature_controls); i++) {
301 		if (sc->feature_controls[i].rlen < 2)
302 			continue;
303 		/* Do not update usages to default value */
304 		if (sc->feature_controls[i].val ==
305 		    feature_control_descrs[i].value)
306 			continue;
307 		error = hconf_set_feature_control(sc, i,
308 		    sc->feature_controls[i].val);
309 		if (error != 0) {
310 			DPRINTF("Failed to restore %s: %d\n",
311 			    feature_control_descrs[i].name, error);
312 		}
313 	}
314 
315 	return (0);
316 }
317 
318 int
319 hconf_set_input_mode(device_t dev, enum hconf_input_mode mode)
320 {
321 	struct hconf_softc *sc = device_get_softc(dev);
322 
323 	return (hconf_set_feature_control(sc, INPUT_MODE, mode));
324 }
325 
326 DRIVER_MODULE(hconf, hidbus, hconf_driver, hconf_devclass, NULL, 0);
327 MODULE_DEPEND(hconf, hidbus, 1, 1, 1);
328 MODULE_DEPEND(hconf, hid, 1, 1, 1);
329 MODULE_VERSION(hconf, 1);
330 HID_PNP_INFO(hconf_devs);
331