2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
5 * Copyright (c) 2020 Andriy Gapon <avg@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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
37 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
47 #define HID_DEBUG_VAR hconf_debug
48 #include <dev/hid/hid.h>
49 #include <dev/hid/hidbus.h>
51 #include <dev/hid/hconf.h>
54 static int hconf_debug = 0;
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");
62 enum feature_control_type {
69 struct feature_control_descr {
74 } feature_control_descrs[] = {
77 .descr = "HID device input mode: 0 = mouse, 3 = touchpad",
78 .usage = HUD_INPUT_MODE,
79 .value = HCONF_INPUT_MODE_MOUSE,
82 .name = "surface_switch",
83 .descr = "Enable / disable switch for surface: 1 = on, 0 = off",
84 .usage = HUD_SURFACE_SWITCH,
88 .name = "buttons_switch",
89 .descr = "Enable / disable switch for buttons: 1 = on, 0 = off",
90 .usage = HUD_BUTTONS_SWITCH,
95 struct feature_control {
97 struct hid_location loc;
106 struct feature_control feature_controls[CONTROLS_COUNT];
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;
114 static devclass_t hconf_devclass;
116 static device_method_t hconf_methods[] = {
118 DEVMETHOD(device_probe, hconf_probe),
119 DEVMETHOD(device_attach, hconf_attach),
120 DEVMETHOD(device_detach, hconf_detach),
121 DEVMETHOD(device_resume, hconf_resume),
126 static driver_t hconf_driver = {
128 .methods = hconf_methods,
129 .size = sizeof(struct hconf_softc),
132 static const struct hid_device_id hconf_devs[] = {
133 { HID_TLC(HUP_DIGITIZERS, HUD_CONFIG) },
137 hconf_set_feature_control(struct hconf_softc *sc, int ctrl_id, u_int val)
139 struct feature_control *fc;
144 KASSERT(ctrl_id >= 0 && ctrl_id < CONTROLS_COUNT,
145 ("impossible ctrl id %d", ctrl_id));
146 fc = &sc->feature_controls[ctrl_id];
150 fbuf = malloc(fc->rlen, M_TEMP, M_WAITOK | M_ZERO);
154 * Assume the report is write-only. Then we have to check for other
155 * controls that may share the same report and set their bits as well.
157 bzero(fbuf + 1, fc->rlen - 1);
158 for (i = 0; i < nitems(sc->feature_controls); i++) {
159 struct feature_control *ofc = &sc->feature_controls[i];
161 /* Skip unrelated report IDs. */
162 if (ofc->rid != fc->rid)
164 KASSERT(fc->rlen == ofc->rlen,
165 ("different lengths for report %d: %d vs %d\n",
166 fc->rid, fc->rlen, ofc->rlen));
167 hid_put_udata(fbuf + 1, ofc->rlen - 1, &ofc->loc,
168 i == ctrl_id ? val : ofc->val);
173 error = hid_set_report(sc->dev, fbuf, fc->rlen,
174 HID_FEATURE_REPORT, fc->rid);
178 sx_unlock(&sc->lock);
185 hconf_feature_control_handler(SYSCTL_HANDLER_ARGS)
187 struct feature_control *fc;
188 struct hconf_softc *sc = arg1;
193 if (ctrl_id < 0 || ctrl_id >= CONTROLS_COUNT)
196 fc = &sc->feature_controls[ctrl_id];
198 error = sysctl_handle_int(oidp, &value, 0, req);
199 if (error != 0 || req->newptr == NULL)
202 error = hconf_set_feature_control(sc, ctrl_id, value);
204 DPRINTF("Failed to set %s: %d\n",
205 feature_control_descrs[ctrl_id].name, error);
212 hconf_parse_feature(struct feature_control *fc, uint8_t tlc_index,
213 uint16_t usage, void *d_ptr, hid_size_t d_len)
217 if (!hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_DIGITIZERS, usage),
218 hid_feature, tlc_index, 0, &fc->loc, &flags, &fc->rid, NULL))
221 if ((flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
224 fc->rlen = hid_report_size(d_ptr, d_len, hid_feature, fc->rid);
229 hconf_probe(device_t dev)
233 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hconf_devs);
237 hidbus_set_desc(dev, "Configuration");
239 return (BUS_PROBE_DEFAULT);
243 hconf_attach(device_t dev)
245 struct hconf_softc *sc = device_get_softc(dev);
246 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
247 struct sysctl_oid *tree = device_get_sysctl_tree(dev);
254 error = hid_get_report_descr(dev, &d_ptr, &d_len);
256 device_printf(dev, "could not retrieve report descriptor from "
257 "device: %d\n", error);
262 sx_init(&sc->lock, device_get_nameunit(dev));
264 tlc_index = hidbus_get_index(dev);
265 for (i = 0; i < nitems(sc->feature_controls); i++) {
266 (void)hconf_parse_feature(&sc->feature_controls[i], tlc_index,
267 feature_control_descrs[i].usage, d_ptr, d_len);
268 if (sc->feature_controls[i].rlen > 1) {
269 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
270 feature_control_descrs[i].name,
271 CTLTYPE_UINT | CTLFLAG_RW,
272 sc, i, hconf_feature_control_handler, "I",
273 feature_control_descrs[i].descr);
275 sc->feature_controls[i].val = feature_control_descrs[i].value;
282 hconf_detach(device_t dev)
284 struct hconf_softc *sc = device_get_softc(dev);
286 sx_destroy(&sc->lock);
292 hconf_resume(device_t dev)
294 struct hconf_softc *sc = device_get_softc(dev);
298 for (i = 0; i < nitems(sc->feature_controls); i++) {
299 if (sc->feature_controls[i].rlen < 2)
301 /* Do not update usages to default value */
302 if (sc->feature_controls[i].val ==
303 feature_control_descrs[i].value)
305 error = hconf_set_feature_control(sc, i,
306 sc->feature_controls[i].val);
308 DPRINTF("Failed to restore %s: %d\n",
309 feature_control_descrs[i].name, error);
317 hconf_set_input_mode(device_t dev, enum hconf_input_mode mode)
319 struct hconf_softc *sc = device_get_softc(dev);
321 return (hconf_set_feature_control(sc, INPUT_MODE, mode));
324 DRIVER_MODULE(hconf, hidbus, hconf_driver, hconf_devclass, NULL, 0);
325 MODULE_DEPEND(hconf, hidbus, 1, 1, 1);
326 MODULE_DEPEND(hconf, hid, 1, 1, 1);
327 MODULE_VERSION(hconf, 1);
328 HID_PNP_INFO(hconf_devs);