1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * Digitizer configuration top-level collection support. 33 * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections 34 */ 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/sysctl.h> 43 #include <sys/systm.h> 44 #include <sys/sx.h> 45 46 #define HID_DEBUG_VAR hconf_debug 47 #include <dev/hid/hid.h> 48 #include <dev/hid/hidbus.h> 49 50 #include <dev/hid/hconf.h> 51 52 #ifdef HID_DEBUG 53 static int hconf_debug = 0; 54 55 static SYSCTL_NODE(_hw_hid, OID_AUTO, hconf, CTLFLAG_RW, 0, 56 "Digitizer configuration top-level collection"); 57 SYSCTL_INT(_hw_hid_hconf, OID_AUTO, debug, CTLFLAG_RWTUN, 58 &hconf_debug, 1, "Debug level"); 59 #endif 60 61 enum feature_control_type { 62 INPUT_MODE = 0, 63 SURFACE_SWITCH, 64 BUTTONS_SWITCH, 65 CONTROLS_COUNT 66 }; 67 68 struct feature_control_descr { 69 const char *name; 70 const char *descr; 71 uint16_t usage; 72 } feature_control_descrs[] = { 73 [INPUT_MODE] = { 74 .name = "input_mode", 75 .descr = "HID device input mode: 0 = mouse, 3 = touchpad", 76 .usage = HUD_INPUT_MODE 77 }, 78 [SURFACE_SWITCH] = { 79 .name = "surface_switch", 80 .descr = "Enable / disable switch for surface: 1 = on, 0 = off", 81 .usage = HUD_SURFACE_SWITCH 82 }, 83 [BUTTONS_SWITCH] = { 84 .name = "buttons_switch", 85 .descr = "Enable / disable switch for buttons: 1 = on, 0 = off", 86 .usage = HUD_BUTTONS_SWITCH 87 }, 88 }; 89 90 struct feature_control { 91 u_int val; 92 struct hid_location loc; 93 hid_size_t rlen; 94 uint8_t rid; 95 }; 96 97 struct hconf_softc { 98 device_t dev; 99 struct sx lock; 100 101 struct feature_control feature_controls[CONTROLS_COUNT]; 102 }; 103 104 static device_probe_t hconf_probe; 105 static device_attach_t hconf_attach; 106 static device_detach_t hconf_detach; 107 static device_resume_t hconf_resume; 108 109 static devclass_t hconf_devclass; 110 111 static device_method_t hconf_methods[] = { 112 113 DEVMETHOD(device_probe, hconf_probe), 114 DEVMETHOD(device_attach, hconf_attach), 115 DEVMETHOD(device_detach, hconf_detach), 116 DEVMETHOD(device_resume, hconf_resume), 117 118 DEVMETHOD_END 119 }; 120 121 static driver_t hconf_driver = { 122 .name = "hconf", 123 .methods = hconf_methods, 124 .size = sizeof(struct hconf_softc), 125 }; 126 127 static const struct hid_device_id hconf_devs[] = { 128 { HID_TLC(HUP_DIGITIZERS, HUD_CONFIG) }, 129 }; 130 131 static int 132 hconf_set_feature_control(struct hconf_softc *sc, int ctrl_id, u_int val) 133 { 134 struct feature_control *fc; 135 uint8_t *fbuf; 136 int error; 137 int i; 138 139 KASSERT(ctrl_id >= 0 && ctrl_id < CONTROLS_COUNT, 140 ("impossible ctrl id %d", ctrl_id)); 141 fc = &sc->feature_controls[ctrl_id]; 142 if (fc->rlen <= 1) 143 return (ENXIO); 144 145 fbuf = malloc(fc->rlen, M_TEMP, M_WAITOK | M_ZERO); 146 sx_xlock(&sc->lock); 147 148 /* Reports are not strictly required to be readable */ 149 error = hid_get_report(sc->dev, fbuf, fc->rlen, NULL, 150 HID_FEATURE_REPORT, fc->rid); 151 152 /* 153 * If the report is write-only, then we have to check for other controls 154 * that may share the same report and set their bits as well. 155 */ 156 if (error != 0) { 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]; 160 161 /* Skip unrelated report IDs. */ 162 if (ofc->rid != fc->rid) 163 continue; 164 /* Skip self. */ 165 if (ofc == fc) 166 continue; 167 KASSERT(fc->rlen == ofc->rlen, 168 ("different lengths for report %d: %d vs %d\n", 169 fc->rid, fc->rlen, ofc->rlen)); 170 hid_put_udata(fbuf + 1, ofc->rlen - 1, &ofc->loc, 171 ofc->val); 172 } 173 } 174 175 fbuf[0] = fc->rid; 176 hid_put_udata(fbuf + 1, fc->rlen - 1, &fc->loc, val); 177 178 error = hid_set_report(sc->dev, fbuf, fc->rlen, 179 HID_FEATURE_REPORT, fc->rid); 180 if (error == 0) 181 fc->val = val; 182 183 sx_unlock(&sc->lock); 184 free(fbuf, M_TEMP); 185 186 return (error); 187 } 188 189 static int 190 hconf_feature_control_handler(SYSCTL_HANDLER_ARGS) 191 { 192 struct feature_control *fc; 193 struct hconf_softc *sc = arg1; 194 int ctrl_id = arg2; 195 u_int value; 196 int error; 197 198 if (ctrl_id < 0 || ctrl_id >= CONTROLS_COUNT) 199 return (ENXIO); 200 201 fc = &sc->feature_controls[ctrl_id]; 202 value = fc->val; 203 error = sysctl_handle_int(oidp, &value, 0, req); 204 if (error != 0 || req->newptr == NULL) 205 return (error); 206 207 error = hconf_set_feature_control(sc, ctrl_id, value); 208 if (error != 0) { 209 DPRINTF("Failed to set %s: %d\n", 210 feature_control_descrs[ctrl_id].name, error); 211 } 212 return (0); 213 } 214 215 216 static int 217 hconf_parse_feature(struct feature_control *fc, uint8_t tlc_index, 218 uint16_t usage, void *d_ptr, hid_size_t d_len) 219 { 220 uint32_t flags; 221 222 if (!hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_DIGITIZERS, usage), 223 hid_feature, tlc_index, 0, &fc->loc, &flags, &fc->rid, NULL)) 224 return (ENOENT); 225 226 if ((flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE) 227 return (EINVAL); 228 229 fc->rlen = hid_report_size(d_ptr, d_len, hid_feature, fc->rid); 230 return (0); 231 } 232 233 static int 234 hconf_probe(device_t dev) 235 { 236 int error; 237 238 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hconf_devs); 239 if (error != 0) 240 return (error); 241 242 hidbus_set_desc(dev, "Configuration"); 243 244 return (BUS_PROBE_DEFAULT); 245 } 246 247 static int 248 hconf_attach(device_t dev) 249 { 250 struct hconf_softc *sc = device_get_softc(dev); 251 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev); 252 struct sysctl_oid *tree = device_get_sysctl_tree(dev); 253 void *d_ptr; 254 hid_size_t d_len; 255 uint8_t tlc_index; 256 int error; 257 int i; 258 259 error = hid_get_report_descr(dev, &d_ptr, &d_len); 260 if (error) { 261 device_printf(dev, "could not retrieve report descriptor from " 262 "device: %d\n", error); 263 return (ENXIO); 264 } 265 266 sc->dev = dev; 267 sx_init(&sc->lock, device_get_nameunit(dev)); 268 269 tlc_index = hidbus_get_index(dev); 270 for (i = 0; i < nitems(sc->feature_controls); i++) { 271 (void)hconf_parse_feature(&sc->feature_controls[i], tlc_index, 272 feature_control_descrs[i].usage, d_ptr, d_len); 273 if (sc->feature_controls[i].rlen > 1) { 274 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 275 feature_control_descrs[i].name, 276 CTLTYPE_UINT | CTLFLAG_RW, 277 sc, i, hconf_feature_control_handler, "I", 278 feature_control_descrs[i].descr); 279 } 280 } 281 282 /* Fully enable (at least, try to). */ 283 (void)hconf_set_feature_control(sc, SURFACE_SWITCH, 1); 284 (void)hconf_set_feature_control(sc, BUTTONS_SWITCH, 1); 285 return (0); 286 } 287 288 static int 289 hconf_detach(device_t dev) 290 { 291 struct hconf_softc *sc = device_get_softc(dev); 292 293 sx_destroy(&sc->lock); 294 295 return (0); 296 } 297 298 static int 299 hconf_resume(device_t dev) 300 { 301 struct hconf_softc *sc = device_get_softc(dev); 302 int error; 303 int i; 304 305 for (i = 0; i < nitems(sc->feature_controls); i++) { 306 if (sc->feature_controls[i].rlen < 2) 307 continue; 308 error = hconf_set_feature_control(sc, i, 309 sc->feature_controls[i].val); 310 if (error != 0) { 311 DPRINTF("Failed to restore %s: %d\n", 312 feature_control_descrs[i].name, error); 313 } 314 } 315 316 return (0); 317 } 318 319 int 320 hconf_set_input_mode(device_t dev, enum hconf_input_mode mode) 321 { 322 struct hconf_softc *sc = device_get_softc(dev); 323 324 return (hconf_set_feature_control(sc, INPUT_MODE, mode)); 325 } 326 327 DRIVER_MODULE(hconf, hidbus, hconf_driver, hconf_devclass, NULL, 0); 328 MODULE_DEPEND(hconf, hidbus, 1, 1, 1); 329 MODULE_DEPEND(hconf, hid, 1, 1, 1); 330 MODULE_VERSION(hconf, 1); 331 HID_PNP_INFO(hconf_devs); 332