1 /* $NetBSD: uts.c,v 1.19 2026/11/05 01:17:54 nat Exp $ */ 2 3 /* 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Pierre Pronchery (khorben (at) defora.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * USB generic Touch Screen driver. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: uts.c,v 1.19 2026/11/05 01:17:54 nat Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_usb.h" 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/device.h> 47 #include <sys/ioctl.h> 48 #include <sys/vnode.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbhid.h> 52 53 #include <dev/usb/usbdi.h> 54 #include <dev/usb/usbdi_util.h> 55 #include <dev/usb/usbdevs.h> 56 #include <dev/usb/usb_quirks.h> 57 #include <dev/usb/uhidev.h> 58 #include <dev/hid/hid.h> 59 60 #include <dev/wscons/wsconsio.h> 61 #include <dev/wscons/wsmousevar.h> 62 #include <dev/wscons/tpcalibvar.h> 63 64 #ifdef UTS_DEBUG 65 #define DPRINTF(x) if (utsdebug) printf x 66 #define DPRINTFN(n,x) if (utsdebug>(n)) printf x 67 int utsdebug = 0; 68 #else 69 #define DPRINTF(x) 70 #define DPRINTFN(n,x) 71 #endif 72 73 74 struct uts_softc { 75 device_t sc_dev; 76 struct uhidev *sc_hdev; 77 struct usbd_device *sc_udev; 78 79 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z; 80 struct hid_location sc_loc_btn; 81 82 bool sc_alwayson; 83 84 int sc_enabled; 85 86 int flags; /* device configuration */ 87 #define UTS_ABS 0x1 /* absolute position */ 88 89 uint32_t sc_buttons; /* touchscreen button status */ 90 device_t sc_wsmousedev; 91 struct tpcalib_softc sc_tpcalib; /* calibration */ 92 struct wsmouse_calibcoords sc_calibcoords; 93 94 char sc_dying; 95 }; 96 97 #define TSCREEN_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 98 99 Static void uts_intr(void *, void *, u_int); 100 101 Static int uts_enable(void *); 102 Static void uts_disable(void *); 103 Static int uts_ioctl(void *, u_long, void *, int, struct lwp *); 104 105 Static const struct wsmouse_accessops uts_accessops = { 106 uts_enable, 107 uts_ioctl, 108 uts_disable, 109 }; 110 111 Static int uts_match(device_t, cfdata_t, void *); 112 Static void uts_attach(device_t, device_t, void *); 113 Static void uts_childdet(device_t, device_t); 114 Static int uts_detach(device_t, int); 115 Static int uts_activate(device_t, enum devact); 116 117 118 119 CFATTACH_DECL2_NEW(uts, sizeof(struct uts_softc), uts_match, uts_attach, 120 uts_detach, uts_activate, NULL, uts_childdet); 121 122 Static int 123 uts_match(device_t parent, cfdata_t match, void *aux) 124 { 125 struct uhidev_attach_arg *uha = aux; 126 int size; 127 void *desc; 128 129 uhidev_get_report_desc(uha->parent, &desc, &size); 130 if (!hid_is_collection(desc, size, uha->reportid, 131 HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCH_SCREEN)) && 132 !hid_is_collection(desc, size, uha->reportid, 133 HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER))) 134 return UMATCH_NONE; 135 136 return UMATCH_IFACECLASS; 137 } 138 139 Static void 140 uts_attach(device_t parent, device_t self, void *aux) 141 { 142 struct uts_softc *sc = device_private(self); 143 struct uhidev_attach_arg *uha = aux; 144 struct wsmousedev_attach_args a; 145 int error, size; 146 void *desc; 147 uint32_t flags; 148 struct hid_data * d; 149 struct hid_item item; 150 uint32_t quirks; 151 152 aprint_normal("\n"); 153 154 sc->sc_dev = self; 155 sc->sc_hdev = uha->parent; 156 sc->sc_udev = uha->uiaa->uiaa_device; 157 158 quirks = usbd_get_quirks(sc->sc_udev)->uq_flags; 159 if (quirks & UQ_ALWAYS_ON) 160 sc->sc_alwayson = true; 161 162 uhidev_get_report_desc(uha->parent, &desc, &size); 163 164 if (!pmf_device_register(self, NULL, NULL)) 165 aprint_error_dev(self, "couldn't establish power handler\n"); 166 167 /* requires HID usage Generic_Desktop:X */ 168 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 169 uha->reportid, hid_input, &sc->sc_loc_x, &flags)) { 170 aprint_error_dev(sc->sc_dev, 171 "touchscreen has no X report\n"); 172 return; 173 } 174 switch (flags & TSCREEN_FLAGS_MASK) { 175 case 0: 176 sc->flags |= UTS_ABS; 177 break; 178 case HIO_RELATIVE: 179 break; 180 default: 181 aprint_error_dev(sc->sc_dev, 182 "X report 0x%04x not supported\n", flags); 183 return; 184 } 185 186 /* requires HID usage Generic_Desktop:Y */ 187 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 188 uha->reportid, hid_input, &sc->sc_loc_y, &flags)) { 189 aprint_error_dev(sc->sc_dev, 190 "touchscreen has no Y report\n"); 191 return; 192 } 193 switch (flags & TSCREEN_FLAGS_MASK) { 194 case 0: 195 sc->flags |= UTS_ABS; 196 break; 197 case HIO_RELATIVE: 198 break; 199 default: 200 aprint_error_dev(sc->sc_dev, 201 "Y report 0x%04x not supported\n", flags); 202 return; 203 } 204 205 /* requires HID usage Digitizer:Tip_Switch */ 206 if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH), 207 uha->reportid, hid_input, &sc->sc_loc_btn, 0)) { 208 aprint_error_dev(sc->sc_dev, 209 "touchscreen has no tip switch report\n"); 210 return; 211 } 212 213 /* requires HID usage Digitizer:In_Range */ 214 if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE), 215 uha->reportid, hid_input, &sc->sc_loc_z, &flags)) { 216 if (uha->uiaa->uiaa_vendor == USB_VENDOR_ELAN) { 217 /* 218 * XXX 219 * ELAN touchscreens error out here but still return 220 * valid data 221 */ 222 aprint_debug_dev(sc->sc_dev, 223 "ELAN touchscreen found, working around bug.\n"); 224 } else { 225 aprint_error_dev(sc->sc_dev, 226 "touchscreen has no range report\n"); 227 return; 228 } 229 } 230 231 /* multi-touch support would need HUD_CONTACTID and HUD_CONTACTMAX */ 232 233 #ifdef UTS_DEBUG 234 DPRINTF(("uts_attach: sc=%p\n", sc)); 235 DPRINTF(("uts_attach: X\t%d/%d\n", 236 sc->sc_loc_x.pos, sc->sc_loc_x.size)); 237 DPRINTF(("uts_attach: Y\t%d/%d\n", 238 sc->sc_loc_y.pos, sc->sc_loc_y.size)); 239 DPRINTF(("uts_attach: Z\t%d/%d\n", 240 sc->sc_loc_z.pos, sc->sc_loc_z.size)); 241 #endif 242 243 a.accessops = &uts_accessops; 244 a.accesscookie = sc; 245 246 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE); 247 248 /* calibrate the touchscreen */ 249 memset(&sc->sc_calibcoords, 0, sizeof(sc->sc_calibcoords)); 250 sc->sc_calibcoords.maxx = 4095; 251 sc->sc_calibcoords.maxy = 4095; 252 sc->sc_calibcoords.samplelen = WSMOUSE_CALIBCOORDS_RESET; 253 d = hid_start_parse(desc, size, hid_input); 254 if (d != NULL) { 255 while (hid_get_item(d, &item)) { 256 if (item.kind != hid_input 257 || HID_GET_USAGE_PAGE(item.usage) != HUP_GENERIC_DESKTOP 258 || item.report_ID != uha->reportid) 259 continue; 260 if (HID_GET_USAGE(item.usage) == HUG_X) { 261 sc->sc_calibcoords.minx = item.logical_minimum; 262 sc->sc_calibcoords.maxx = item.logical_maximum; 263 } 264 if (HID_GET_USAGE(item.usage) == HUG_Y) { 265 sc->sc_calibcoords.miny = item.logical_minimum; 266 sc->sc_calibcoords.maxy = item.logical_maximum; 267 } 268 } 269 hid_end_parse(d); 270 } 271 tpcalib_init(&sc->sc_tpcalib); 272 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS, 273 (void *)&sc->sc_calibcoords, 0, 0); 274 275 if (sc->sc_alwayson) { 276 error = uhidev_open(sc->sc_hdev, &uts_intr, sc); 277 if (error != 0) { 278 aprint_error_dev(self, 279 "WARNING: couldn't open always-on device\n"); 280 sc->sc_alwayson = false; 281 } 282 } 283 return; 284 } 285 286 Static int 287 uts_detach(device_t self, int flags) 288 { 289 struct uts_softc *sc = device_private(self); 290 int error; 291 292 DPRINTF(("uts_detach: sc=%p flags=%d\n", sc, flags)); 293 294 if (sc->sc_alwayson) 295 uhidev_close(sc->sc_hdev); 296 297 error = config_detach_children(self, flags); 298 if (error) 299 return error; 300 301 pmf_device_deregister(self); 302 return 0; 303 } 304 305 Static void 306 uts_childdet(device_t self, device_t child) 307 { 308 struct uts_softc *sc = device_private(self); 309 310 KASSERT(sc->sc_wsmousedev == child); 311 sc->sc_wsmousedev = NULL; 312 } 313 314 Static int 315 uts_activate(device_t self, enum devact act) 316 { 317 struct uts_softc *sc = device_private(self); 318 319 switch (act) { 320 case DVACT_DEACTIVATE: 321 sc->sc_dying = 1; 322 return 0; 323 default: 324 return EOPNOTSUPP; 325 } 326 } 327 328 Static int 329 uts_enable(void *v) 330 { 331 struct uts_softc *sc = v; 332 int error; 333 334 DPRINTFN(1,("uts_enable: sc=%p\n", sc)); 335 336 if (sc->sc_dying) 337 return EIO; 338 339 if (sc->sc_enabled) 340 return EBUSY; 341 342 error = 0; 343 if (!sc->sc_alwayson) { 344 error = uhidev_open(sc->sc_hdev, &uts_intr, sc); 345 if (error) 346 sc->sc_enabled = 0; 347 } 348 349 if (error != 0) 350 return error; 351 352 sc->sc_enabled = 1; 353 sc->sc_buttons = 0; 354 355 return 0; 356 } 357 358 Static void 359 uts_disable(void *v) 360 { 361 struct uts_softc *sc = v; 362 363 DPRINTFN(1,("uts_disable: sc=%p\n", sc)); 364 365 if (!sc->sc_enabled) 366 return; 367 368 sc->sc_enabled = 0; 369 if (!sc->sc_alwayson) 370 uhidev_close(sc->sc_hdev); 371 } 372 373 Static int 374 uts_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 375 { 376 struct uts_softc *sc = v; 377 378 switch (cmd) { 379 case WSMOUSEIO_GTYPE: 380 if (sc->flags & UTS_ABS) 381 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 382 else 383 *(u_int *)data = WSMOUSE_TYPE_USB; 384 return 0; 385 case WSMOUSEIO_SCALIBCOORDS: 386 case WSMOUSEIO_GCALIBCOORDS: 387 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l); 388 } 389 390 return EPASSTHROUGH; 391 } 392 393 Static void 394 uts_intr(void *cookie, void *ibuf, u_int len) 395 { 396 struct uts_softc *sc = cookie; 397 int dx, dy, dz; 398 uint32_t buttons = 0; 399 int flags, s; 400 401 DPRINTFN(5,("uts_intr: len=%d\n", len)); 402 403 flags = WSMOUSE_INPUT_DELTA | WSMOUSE_INPUT_ABSOLUTE_Z; 404 405 dx = hid_get_data(ibuf, &sc->sc_loc_x); 406 if (sc->flags & UTS_ABS) { 407 flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y); 408 dy = hid_get_data(ibuf, &sc->sc_loc_y); 409 tpcalib_trans(&sc->sc_tpcalib, dx, dy, &dx, &dy); 410 } else 411 dy = -hid_get_data(ibuf, &sc->sc_loc_y); 412 413 dz = hid_get_data(ibuf, &sc->sc_loc_z); 414 415 if (hid_get_data(ibuf, &sc->sc_loc_btn)) 416 buttons |= 1; 417 418 if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) { 419 DPRINTFN(10,("uts_intr: x:%d y:%d z:%d buttons:%#x\n", 420 dx, dy, dz, buttons)); 421 sc->sc_buttons = buttons; 422 if (sc->sc_wsmousedev != NULL) { 423 s = spltty(); 424 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz, 0, 425 flags); 426 splx(s); 427 } 428 } 429 } 430