1 /* $OpenBSD: uts.c,v 1.40 2017/04/08 02:57:25 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Robert Nagy <robert@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/sockio.h> 21 #include <sys/mbuf.h> 22 #include <sys/kernel.h> 23 #include <sys/socket.h> 24 #include <sys/systm.h> 25 #include <sys/malloc.h> 26 #include <sys/timeout.h> 27 #include <sys/conf.h> 28 #include <sys/device.h> 29 #include <sys/endian.h> 30 31 #include <machine/intr.h> 32 33 #include <dev/usb/usb.h> 34 #include <dev/usb/usbdi.h> 35 #include <dev/usb/usbdi_util.h> 36 #include <dev/usb/usbdevs.h> 37 38 #include <dev/wscons/wsconsio.h> 39 #include <dev/wscons/wsmousevar.h> 40 41 #ifdef UTS_DEBUG 42 #define DPRINTF(x) do { printf x; } while (0) 43 #else 44 #define DPRINTF(x) 45 #endif 46 47 struct tsscale { 48 int minx, maxx; 49 int miny, maxy; 50 int swapxy; 51 int resx, resy; 52 } def_scale = { 53 67, 1931, 102, 1937, 0, 1024, 768 54 }; 55 56 struct uts_softc { 57 struct device sc_dev; 58 struct usbd_device *sc_udev; 59 struct usbd_interface *sc_iface; 60 int sc_iface_number; 61 int sc_product; 62 int sc_vendor; 63 64 int sc_intr_number; 65 struct usbd_pipe *sc_intr_pipe; 66 u_char *sc_ibuf; 67 int sc_isize; 68 u_int8_t sc_pkts; 69 70 struct device *sc_wsmousedev; 71 72 int sc_enabled; 73 int sc_buttons; 74 int sc_oldx; 75 int sc_oldy; 76 int sc_rawmode; 77 78 struct tsscale sc_tsscale; 79 }; 80 81 struct uts_pos { 82 int down; 83 int x; 84 int y; 85 int z; /* touch pressure */ 86 }; 87 88 const struct usb_devno uts_devs[] = { 89 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ITM_TOUCH }, 90 { USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL }, 91 { USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL2 }, 92 { USB_VENDOR_GUNZE, USB_PRODUCT_GUNZE_TOUCHPANEL } 93 }; 94 95 void uts_intr(struct usbd_xfer *, void *, usbd_status); 96 void uts_get_pos(void *addr, struct uts_pos *tp); 97 98 int uts_enable(void *); 99 void uts_disable(void *); 100 int uts_ioctl(void *, u_long, caddr_t, int, struct proc *); 101 102 const struct wsmouse_accessops uts_accessops = { 103 uts_enable, 104 uts_ioctl, 105 uts_disable, 106 }; 107 108 int uts_match(struct device *, void *, void *); 109 void uts_attach(struct device *, struct device *, void *); 110 int uts_detach(struct device *, int); 111 int uts_activate(struct device *, int); 112 113 struct cfdriver uts_cd = { 114 NULL, "uts", DV_DULL 115 }; 116 117 const struct cfattach uts_ca = { 118 sizeof(struct uts_softc), 119 uts_match, 120 uts_attach, 121 uts_detach, 122 uts_activate, 123 }; 124 125 int 126 uts_match(struct device *parent, void *match, void *aux) 127 { 128 struct usb_attach_arg *uaa = aux; 129 usb_interface_descriptor_t *id; 130 131 if (uaa->iface == NULL) 132 return (UMATCH_NONE); 133 134 /* Some eGalax touch screens are HID devices. ignore them */ 135 id = usbd_get_interface_descriptor(uaa->iface); 136 if (id != NULL && id->bInterfaceClass == UICLASS_HID) 137 return (UMATCH_NONE); 138 139 return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ? 140 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 141 } 142 143 void 144 uts_attach(struct device *parent, struct device *self, void *aux) 145 { 146 struct uts_softc *sc = (struct uts_softc *)self; 147 struct usb_attach_arg *uaa = aux; 148 usb_config_descriptor_t *cdesc; 149 usb_interface_descriptor_t *id; 150 usb_endpoint_descriptor_t *ed; 151 struct wsmousedev_attach_args a; 152 int i; 153 154 sc->sc_udev = uaa->device; 155 sc->sc_product = uaa->product; 156 sc->sc_vendor = uaa->vendor; 157 sc->sc_intr_number = -1; 158 sc->sc_intr_pipe = NULL; 159 sc->sc_enabled = sc->sc_isize = 0; 160 161 /* Copy the default scalue values to each softc */ 162 bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale)); 163 164 /* get the config descriptor */ 165 cdesc = usbd_get_config_descriptor(sc->sc_udev); 166 if (cdesc == NULL) { 167 printf("%s: failed to get configuration descriptor\n", 168 sc->sc_dev.dv_xname); 169 usbd_deactivate(sc->sc_udev); 170 return; 171 } 172 173 /* get the interface */ 174 if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) { 175 printf("%s: failed to get interface\n", 176 sc->sc_dev.dv_xname); 177 usbd_deactivate(sc->sc_udev); 178 return; 179 } 180 181 /* Find the interrupt endpoint */ 182 id = usbd_get_interface_descriptor(sc->sc_iface); 183 sc->sc_iface_number = id->bInterfaceNumber; 184 185 for (i = 0; i < id->bNumEndpoints; i++) { 186 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 187 if (ed == NULL) { 188 printf("%s: no endpoint descriptor for %d\n", 189 sc->sc_dev.dv_xname, i); 190 usbd_deactivate(sc->sc_udev); 191 return; 192 } 193 194 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 195 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 196 sc->sc_intr_number = ed->bEndpointAddress; 197 sc->sc_isize = UGETW(ed->wMaxPacketSize); 198 } 199 } 200 201 if (sc->sc_intr_number== -1) { 202 printf("%s: Could not find interrupt in\n", 203 sc->sc_dev.dv_xname); 204 usbd_deactivate(sc->sc_udev); 205 return; 206 } 207 208 a.accessops = &uts_accessops; 209 a.accesscookie = sc; 210 211 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 212 } 213 214 int 215 uts_detach(struct device *self, int flags) 216 { 217 struct uts_softc *sc = (struct uts_softc *)self; 218 int rv = 0; 219 220 if (sc->sc_intr_pipe != NULL) { 221 usbd_abort_pipe(sc->sc_intr_pipe); 222 usbd_close_pipe(sc->sc_intr_pipe); 223 sc->sc_intr_pipe = NULL; 224 } 225 226 if (sc->sc_wsmousedev != NULL) { 227 rv = config_detach(sc->sc_wsmousedev, flags); 228 sc->sc_wsmousedev = NULL; 229 } 230 231 return (rv); 232 } 233 234 int 235 uts_activate(struct device *self, int act) 236 { 237 struct uts_softc *sc = (struct uts_softc *)self; 238 int rv = 0; 239 240 switch (act) { 241 case DVACT_DEACTIVATE: 242 if (sc->sc_wsmousedev != NULL) 243 rv = config_deactivate(sc->sc_wsmousedev); 244 usbd_deactivate(sc->sc_udev); 245 break; 246 } 247 248 return (rv); 249 } 250 251 int 252 uts_enable(void *v) 253 { 254 struct uts_softc *sc = v; 255 int err; 256 257 if (usbd_is_dying(sc->sc_udev)) 258 return (EIO); 259 260 if (sc->sc_enabled) 261 return (EBUSY); 262 263 if (sc->sc_isize == 0) 264 return (0); 265 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 266 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 267 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf, 268 sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL); 269 if (err) { 270 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 271 sc->sc_intr_pipe = NULL; 272 return (EIO); 273 } 274 275 sc->sc_enabled = 1; 276 sc->sc_buttons = 0; 277 278 return (0); 279 } 280 281 void 282 uts_disable(void *v) 283 { 284 struct uts_softc *sc = v; 285 286 if (!sc->sc_enabled) { 287 printf("uts_disable: already disabled!\n"); 288 return; 289 } 290 291 /* Disable interrupts. */ 292 if (sc->sc_intr_pipe != NULL) { 293 usbd_abort_pipe(sc->sc_intr_pipe); 294 usbd_close_pipe(sc->sc_intr_pipe); 295 sc->sc_intr_pipe = NULL; 296 } 297 298 if (sc->sc_ibuf != NULL) { 299 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 300 sc->sc_ibuf = NULL; 301 } 302 303 sc->sc_enabled = 0; 304 } 305 306 int 307 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l) 308 { 309 int error = 0; 310 struct uts_softc *sc = v; 311 struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data; 312 313 DPRINTF(("uts_ioctl(%d, '%c', %d)\n", 314 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff)); 315 316 switch (cmd) { 317 case WSMOUSEIO_SCALIBCOORDS: 318 if (!(wsmc->minx >= -32768 && wsmc->maxx >= 0 && 319 wsmc->miny >= -32768 && wsmc->maxy >= 0 && 320 wsmc->resx >= 0 && wsmc->resy >= 0 && 321 wsmc->minx < 32768 && wsmc->maxx < 32768 && 322 wsmc->miny < 32768 && wsmc->maxy < 32768 && 323 (wsmc->maxx - wsmc->minx) != 0 && 324 (wsmc->maxy - wsmc->miny) != 0 && 325 wsmc->resx < 32768 && wsmc->resy < 32768 && 326 wsmc->swapxy >= 0 && wsmc->swapxy <= 1 && 327 wsmc->samplelen >= 0 && wsmc->samplelen <= 1)) 328 return (EINVAL); 329 330 sc->sc_tsscale.minx = wsmc->minx; 331 sc->sc_tsscale.maxx = wsmc->maxx; 332 sc->sc_tsscale.miny = wsmc->miny; 333 sc->sc_tsscale.maxy = wsmc->maxy; 334 sc->sc_tsscale.swapxy = wsmc->swapxy; 335 sc->sc_tsscale.resx = wsmc->resx; 336 sc->sc_tsscale.resy = wsmc->resy; 337 sc->sc_rawmode = wsmc->samplelen; 338 break; 339 case WSMOUSEIO_GCALIBCOORDS: 340 wsmc->minx = sc->sc_tsscale.minx; 341 wsmc->maxx = sc->sc_tsscale.maxx; 342 wsmc->miny = sc->sc_tsscale.miny; 343 wsmc->maxy = sc->sc_tsscale.maxy; 344 wsmc->swapxy = sc->sc_tsscale.swapxy; 345 wsmc->resx = sc->sc_tsscale.resx; 346 wsmc->resy = sc->sc_tsscale.resy; 347 wsmc->samplelen = sc->sc_rawmode; 348 break; 349 case WSMOUSEIO_GTYPE: 350 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 351 break; 352 default: 353 error = ENOTTY; 354 break; 355 } 356 357 return (error); 358 } 359 360 void 361 uts_get_pos(void *addr, struct uts_pos *tp) 362 { 363 struct uts_softc *sc = addr; 364 u_char *p = sc->sc_ibuf; 365 int down, x, y, z; 366 367 switch (sc->sc_product) { 368 case USB_PRODUCT_FTDI_ITM_TOUCH: 369 down = (~p[7] & 0x20); 370 x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f); 371 /* Invert the Y coordinate */ 372 y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f)); 373 z = ((p[2] & 0x1) << 7) | (p[5] & 0x7f); 374 sc->sc_pkts = 0x8; 375 break; 376 case USB_PRODUCT_EGALAX_TPANEL: 377 case USB_PRODUCT_EGALAX_TPANEL2: 378 /* 379 * eGalax and Gunze USB touch panels have the same device ID, 380 * so decide upon the vendor ID. 381 */ 382 switch (sc->sc_vendor) { 383 case USB_VENDOR_EGALAX: 384 down = (p[0] & 0x01); 385 /* Invert the X coordinate */ 386 x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f)); 387 y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f); 388 z = down; 389 sc->sc_pkts = 0x5; 390 break; 391 case USB_VENDOR_GUNZE: 392 down = (~p[7] & 0x20); 393 /* Invert the X coordinate */ 394 x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f)); 395 y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f); 396 z = (down != 0); 397 sc->sc_pkts = 0x4; 398 break; 399 } 400 break; 401 } 402 403 DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n", 404 sc->sc_dev.dv_xname, down, sc->sc_pkts)); 405 406 /* x/y values are not reliable if there is no pressure */ 407 if (down) { 408 if (sc->sc_tsscale.swapxy && !sc->sc_rawmode) { 409 /* Swap X/Y-Axis */ 410 tp->y = x; 411 tp->x = y; 412 } else { 413 tp->x = x; 414 tp->y = y; 415 } 416 if (!sc->sc_rawmode && 417 (sc->sc_tsscale.maxx - sc->sc_tsscale.minx) != 0 && 418 (sc->sc_tsscale.maxy - sc->sc_tsscale.miny) != 0) { 419 /* Scale down to the screen resolution. */ 420 tp->x = ((tp->x - sc->sc_tsscale.minx) * 421 sc->sc_tsscale.resx) / 422 (sc->sc_tsscale.maxx - sc->sc_tsscale.minx); 423 tp->y = ((tp->y - sc->sc_tsscale.miny) * 424 sc->sc_tsscale.resy) / 425 (sc->sc_tsscale.maxy - sc->sc_tsscale.miny); 426 } 427 } else { 428 tp->x = sc->sc_oldx; 429 tp->y = sc->sc_oldy; 430 } 431 tp->z = z; 432 tp->down = down; 433 } 434 435 void 436 uts_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 437 { 438 struct uts_softc *sc = addr; 439 u_int32_t len; 440 int s; 441 struct uts_pos tp; 442 443 if (status == USBD_CANCELLED) 444 return; 445 446 if (status != USBD_NORMAL_COMPLETION) { 447 printf("%s: status %d\n", sc->sc_dev.dv_xname, status); 448 if (status == USBD_STALLED) 449 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 450 return; 451 } 452 453 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 454 455 s = spltty(); 456 457 uts_get_pos(sc, &tp); 458 459 if (len != sc->sc_pkts) { 460 DPRINTF(("%s: bad input length %d != %d\n", 461 sc->sc_dev.dv_xname, len, sc->sc_isize)); 462 splx(s); 463 return; 464 } 465 466 DPRINTF(("%s: tp.down = %d, tp.z = %d, tp.x = %d, tp.y = %d\n", 467 sc->sc_dev.dv_xname, tp.down, tp.z, tp.x, tp.y)); 468 469 WSMOUSE_TOUCH(sc->sc_wsmousedev, tp.down, tp.x, tp.y, tp.z, 0); 470 sc->sc_oldy = tp.y; 471 sc->sc_oldx = tp.x; 472 473 splx(s); 474 } 475