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