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
39 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
49 #define HID_DEBUG_VAR hconf_debug
50 #include <dev/hid/hid.h>
51 #include <dev/hid/hidbus.h>
53 #include <dev/hid/hconf.h>
56 static int hconf_debug = 0;
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");
64 enum feature_control_type {
71 struct feature_control_descr {
76 } feature_control_descrs[] = {
79 .descr = "HID device input mode: 0 = mouse, 3 = touchpad",
80 .usage = HUD_INPUT_MODE,
81 .value = HCONF_INPUT_MODE_MOUSE,
84 .name = "surface_switch",
85 .descr = "Enable / disable switch for surface: 1 = on, 0 = off",
86 .usage = HUD_SURFACE_SWITCH,
90 .name = "buttons_switch",
91 .descr = "Enable / disable switch for buttons: 1 = on, 0 = off",
92 .usage = HUD_BUTTONS_SWITCH,
97 struct feature_control {
99 struct hid_location loc;
108 struct feature_control feature_controls[CONTROLS_COUNT];
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;
116 static devclass_t hconf_devclass;
118 static device_method_t hconf_methods[] = {
120 DEVMETHOD(device_probe, hconf_probe),
121 DEVMETHOD(device_attach, hconf_attach),
122 DEVMETHOD(device_detach, hconf_detach),
123 DEVMETHOD(device_resume, hconf_resume),
128 static driver_t hconf_driver = {
130 .methods = hconf_methods,
131 .size = sizeof(struct hconf_softc),
134 static const struct hid_device_id hconf_devs[] = {
135 { HID_TLC(HUP_DIGITIZERS, HUD_CONFIG) },
139 hconf_set_feature_control(struct hconf_softc *sc, int ctrl_id, u_int val)
141 struct feature_control *fc;
146 KASSERT(ctrl_id >= 0 && ctrl_id < CONTROLS_COUNT,
147 ("impossible ctrl id %d", ctrl_id));
148 fc = &sc->feature_controls[ctrl_id];
152 fbuf = malloc(fc->rlen, M_TEMP, M_WAITOK | M_ZERO);
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.
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];
163 /* Skip unrelated report IDs. */
164 if (ofc->rid != fc->rid)
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);
175 error = hid_set_report(sc->dev, fbuf, fc->rlen,
176 HID_FEATURE_REPORT, fc->rid);
180 sx_unlock(&sc->lock);
187 hconf_feature_control_handler(SYSCTL_HANDLER_ARGS)
189 struct feature_control *fc;
190 struct hconf_softc *sc = arg1;
195 if (ctrl_id < 0 || ctrl_id >= CONTROLS_COUNT)
198 fc = &sc->feature_controls[ctrl_id];
200 error = sysctl_handle_int(oidp, &value, 0, req);
201 if (error != 0 || req->newptr == NULL)
204 error = hconf_set_feature_control(sc, ctrl_id, value);
206 DPRINTF("Failed to set %s: %d\n",
207 feature_control_descrs[ctrl_id].name, error);
214 hconf_parse_feature(struct feature_control *fc, uint8_t tlc_index,
215 uint16_t usage, void *d_ptr, hid_size_t d_len)
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))
223 if ((flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
226 fc->rlen = hid_report_size(d_ptr, d_len, hid_feature, fc->rid);
231 hconf_probe(device_t dev)
235 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hconf_devs);
239 hidbus_set_desc(dev, "Configuration");
241 return (BUS_PROBE_DEFAULT);
245 hconf_attach(device_t dev)
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);
256 error = hid_get_report_descr(dev, &d_ptr, &d_len);
258 device_printf(dev, "could not retrieve report descriptor from "
259 "device: %d\n", error);
264 sx_init(&sc->lock, device_get_nameunit(dev));
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);
277 sc->feature_controls[i].val = feature_control_descrs[i].value;
284 hconf_detach(device_t dev)
286 struct hconf_softc *sc = device_get_softc(dev);
288 sx_destroy(&sc->lock);
294 hconf_resume(device_t dev)
296 struct hconf_softc *sc = device_get_softc(dev);
300 for (i = 0; i < nitems(sc->feature_controls); i++) {
301 if (sc->feature_controls[i].rlen < 2)
303 /* Do not update usages to default value */
304 if (sc->feature_controls[i].val ==
305 feature_control_descrs[i].value)
307 error = hconf_set_feature_control(sc, i,
308 sc->feature_controls[i].val);
310 DPRINTF("Failed to restore %s: %d\n",
311 feature_control_descrs[i].name, error);
319 hconf_set_input_mode(device_t dev, enum hconf_input_mode mode)
321 struct hconf_softc *sc = device_get_softc(dev);
323 return (hconf_set_feature_control(sc, INPUT_MODE, mode));
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);