1 /* $OpenBSD: uts.c,v 1.37 2016/02/10 05:49:50 guenther 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 #define UTS_CONFIG_INDEX 0 48 49 struct tsscale { 50 int minx, maxx; 51 int miny, maxy; 52 int swapxy; 53 int resx, resy; 54 } def_scale = { 55 67, 1931, 102, 1937, 0, 1024, 768 56 }; 57 58 struct uts_softc { 59 struct device sc_dev; 60 struct usbd_device *sc_udev; 61 struct usbd_interface *sc_iface; 62 int sc_iface_number; 63 int sc_product; 64 int sc_vendor; 65 66 int sc_intr_number; 67 struct usbd_pipe *sc_intr_pipe; 68 u_char *sc_ibuf; 69 int sc_isize; 70 u_int8_t sc_pkts; 71 72 struct device *sc_wsmousedev; 73 74 int sc_enabled; 75 int sc_buttons; 76 int sc_oldx; 77 int sc_oldy; 78 int sc_rawmode; 79 80 struct tsscale sc_tsscale; 81 }; 82 83 struct uts_pos { 84 int down; 85 int x; 86 int y; 87 int z; /* touch pressure */ 88 }; 89 90 const struct usb_devno uts_devs[] = { 91 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_ITM_TOUCH }, 92 { USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL }, 93 { USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL2 }, 94 { USB_VENDOR_GUNZE, USB_PRODUCT_GUNZE_TOUCHPANEL } 95 }; 96 97 void uts_intr(struct usbd_xfer *, void *, usbd_status); 98 void uts_get_pos(void *addr, struct uts_pos *tp); 99 100 int uts_enable(void *); 101 void uts_disable(void *); 102 int uts_ioctl(void *, u_long, caddr_t, int, struct proc *); 103 104 const struct wsmouse_accessops uts_accessops = { 105 uts_enable, 106 uts_ioctl, 107 uts_disable, 108 }; 109 110 int uts_match(struct device *, void *, void *); 111 void uts_attach(struct device *, struct device *, void *); 112 int uts_detach(struct device *, int); 113 int uts_activate(struct device *, int); 114 115 struct cfdriver uts_cd = { 116 NULL, "uts", DV_DULL 117 }; 118 119 const struct cfattach uts_ca = { 120 sizeof(struct uts_softc), 121 uts_match, 122 uts_attach, 123 uts_detach, 124 uts_activate, 125 }; 126 127 int 128 uts_match(struct device *parent, void *match, void *aux) 129 { 130 struct usb_attach_arg *uaa = aux; 131 usb_interface_descriptor_t *id; 132 133 if (uaa->iface == NULL) 134 return (UMATCH_NONE); 135 136 /* Some eGalax touch screens are HID devices. ignore them */ 137 id = usbd_get_interface_descriptor(uaa->iface); 138 if (id != NULL && id->bInterfaceClass == UICLASS_HID) 139 return (UMATCH_NONE); 140 141 return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ? 142 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 143 } 144 145 void 146 uts_attach(struct device *parent, struct device *self, void *aux) 147 { 148 struct uts_softc *sc = (struct uts_softc *)self; 149 struct usb_attach_arg *uaa = aux; 150 usb_config_descriptor_t *cdesc; 151 usb_interface_descriptor_t *id; 152 usb_endpoint_descriptor_t *ed; 153 struct wsmousedev_attach_args a; 154 int i; 155 156 sc->sc_udev = uaa->device; 157 sc->sc_product = uaa->product; 158 sc->sc_vendor = uaa->vendor; 159 sc->sc_intr_number = -1; 160 sc->sc_intr_pipe = NULL; 161 sc->sc_enabled = sc->sc_isize = 0; 162 163 /* Copy the default scalue values to each softc */ 164 bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale)); 165 166 /* Move the device into the configured state. */ 167 if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) { 168 printf("%s: could not set configuartion no\n", 169 sc->sc_dev.dv_xname); 170 usbd_deactivate(sc->sc_udev); 171 return; 172 } 173 174 /* get the config descriptor */ 175 cdesc = usbd_get_config_descriptor(sc->sc_udev); 176 if (cdesc == NULL) { 177 printf("%s: failed to get configuration descriptor\n", 178 sc->sc_dev.dv_xname); 179 usbd_deactivate(sc->sc_udev); 180 return; 181 } 182 183 /* get the interface */ 184 if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) { 185 printf("%s: failed to get interface\n", 186 sc->sc_dev.dv_xname); 187 usbd_deactivate(sc->sc_udev); 188 return; 189 } 190 191 /* Find the interrupt endpoint */ 192 id = usbd_get_interface_descriptor(sc->sc_iface); 193 sc->sc_iface_number = id->bInterfaceNumber; 194 195 for (i = 0; i < id->bNumEndpoints; i++) { 196 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 197 if (ed == NULL) { 198 printf("%s: no endpoint descriptor for %d\n", 199 sc->sc_dev.dv_xname, i); 200 usbd_deactivate(sc->sc_udev); 201 return; 202 } 203 204 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 205 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 206 sc->sc_intr_number = ed->bEndpointAddress; 207 sc->sc_isize = UGETW(ed->wMaxPacketSize); 208 } 209 } 210 211 if (sc->sc_intr_number== -1) { 212 printf("%s: Could not find interrupt in\n", 213 sc->sc_dev.dv_xname); 214 usbd_deactivate(sc->sc_udev); 215 return; 216 } 217 218 a.accessops = &uts_accessops; 219 a.accesscookie = sc; 220 221 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 222 } 223 224 int 225 uts_detach(struct device *self, int flags) 226 { 227 struct uts_softc *sc = (struct uts_softc *)self; 228 int rv = 0; 229 230 if (sc->sc_intr_pipe != NULL) { 231 usbd_abort_pipe(sc->sc_intr_pipe); 232 usbd_close_pipe(sc->sc_intr_pipe); 233 sc->sc_intr_pipe = NULL; 234 } 235 236 if (sc->sc_wsmousedev != NULL) { 237 rv = config_detach(sc->sc_wsmousedev, flags); 238 sc->sc_wsmousedev = NULL; 239 } 240 241 return (rv); 242 } 243 244 int 245 uts_activate(struct device *self, int act) 246 { 247 struct uts_softc *sc = (struct uts_softc *)self; 248 int rv = 0; 249 250 switch (act) { 251 case DVACT_DEACTIVATE: 252 if (sc->sc_wsmousedev != NULL) 253 rv = config_deactivate(sc->sc_wsmousedev); 254 usbd_deactivate(sc->sc_udev); 255 break; 256 } 257 258 return (rv); 259 } 260 261 int 262 uts_enable(void *v) 263 { 264 struct uts_softc *sc = v; 265 int err; 266 267 if (usbd_is_dying(sc->sc_udev)) 268 return (EIO); 269 270 if (sc->sc_enabled) 271 return (EBUSY); 272 273 if (sc->sc_isize == 0) 274 return (0); 275 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 276 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 277 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf, 278 sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL); 279 if (err) { 280 free(sc->sc_ibuf, M_USBDEV, 0); 281 sc->sc_intr_pipe = NULL; 282 return (EIO); 283 } 284 285 sc->sc_enabled = 1; 286 sc->sc_buttons = 0; 287 288 return (0); 289 } 290 291 void 292 uts_disable(void *v) 293 { 294 struct uts_softc *sc = v; 295 296 if (!sc->sc_enabled) { 297 printf("uts_disable: already disabled!\n"); 298 return; 299 } 300 301 /* Disable interrupts. */ 302 if (sc->sc_intr_pipe != NULL) { 303 usbd_abort_pipe(sc->sc_intr_pipe); 304 usbd_close_pipe(sc->sc_intr_pipe); 305 sc->sc_intr_pipe = NULL; 306 } 307 308 if (sc->sc_ibuf != NULL) { 309 free(sc->sc_ibuf, M_USBDEV, 0); 310 sc->sc_ibuf = NULL; 311 } 312 313 sc->sc_enabled = 0; 314 } 315 316 int 317 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l) 318 { 319 int error = 0; 320 struct uts_softc *sc = v; 321 struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data; 322 323 DPRINTF(("uts_ioctl(%d, '%c', %d)\n", 324 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff)); 325 326 switch (cmd) { 327 case WSMOUSEIO_SCALIBCOORDS: 328 if (!(wsmc->minx >= -32768 && wsmc->maxx >= 0 && 329 wsmc->miny >= -32768 && wsmc->maxy >= 0 && 330 wsmc->resx >= 0 && wsmc->resy >= 0 && 331 wsmc->minx < 32768 && wsmc->maxx < 32768 && 332 wsmc->miny < 32768 && wsmc->maxy < 32768 && 333 (wsmc->maxx - wsmc->minx) != 0 && 334 (wsmc->maxy - wsmc->miny) != 0 && 335 wsmc->resx < 32768 && wsmc->resy < 32768 && 336 wsmc->swapxy >= 0 && wsmc->swapxy <= 1 && 337 wsmc->samplelen >= 0 && wsmc->samplelen <= 1)) 338 return (EINVAL); 339 340 sc->sc_tsscale.minx = wsmc->minx; 341 sc->sc_tsscale.maxx = wsmc->maxx; 342 sc->sc_tsscale.miny = wsmc->miny; 343 sc->sc_tsscale.maxy = wsmc->maxy; 344 sc->sc_tsscale.swapxy = wsmc->swapxy; 345 sc->sc_tsscale.resx = wsmc->resx; 346 sc->sc_tsscale.resy = wsmc->resy; 347 sc->sc_rawmode = wsmc->samplelen; 348 break; 349 case WSMOUSEIO_GCALIBCOORDS: 350 wsmc->minx = sc->sc_tsscale.minx; 351 wsmc->maxx = sc->sc_tsscale.maxx; 352 wsmc->miny = sc->sc_tsscale.miny; 353 wsmc->maxy = sc->sc_tsscale.maxy; 354 wsmc->swapxy = sc->sc_tsscale.swapxy; 355 wsmc->resx = sc->sc_tsscale.resx; 356 wsmc->resy = sc->sc_tsscale.resy; 357 wsmc->samplelen = sc->sc_rawmode; 358 break; 359 case WSMOUSEIO_GTYPE: 360 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 361 break; 362 default: 363 error = ENOTTY; 364 break; 365 } 366 367 return (error); 368 } 369 370 void 371 uts_get_pos(void *addr, struct uts_pos *tp) 372 { 373 struct uts_softc *sc = addr; 374 u_char *p = sc->sc_ibuf; 375 int down, x, y, z; 376 377 switch (sc->sc_product) { 378 case USB_PRODUCT_FTDI_ITM_TOUCH: 379 down = (~p[7] & 0x20); 380 x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f); 381 /* Invert the Y coordinate */ 382 y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f)); 383 z = ((p[2] & 0x1) << 7) | (p[5] & 0x7f); 384 sc->sc_pkts = 0x8; 385 break; 386 case USB_PRODUCT_EGALAX_TPANEL: 387 case USB_PRODUCT_EGALAX_TPANEL2: 388 /* 389 * eGalax and Gunze USB touch panels have the same device ID, 390 * so decide upon the vendor ID. 391 */ 392 switch (sc->sc_vendor) { 393 case USB_VENDOR_EGALAX: 394 down = (p[0] & 0x01); 395 /* Invert the X coordinate */ 396 x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f)); 397 y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f); 398 z = down; 399 sc->sc_pkts = 0x5; 400 break; 401 case USB_VENDOR_GUNZE: 402 down = (~p[7] & 0x20); 403 /* Invert the X coordinate */ 404 x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f)); 405 y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f); 406 z = (down != 0); 407 sc->sc_pkts = 0x4; 408 break; 409 } 410 break; 411 } 412 413 DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n", 414 sc->sc_dev.dv_xname, down, sc->sc_pkts)); 415 416 /* x/y values are not reliable if there is no pressure */ 417 if (down) { 418 if (sc->sc_tsscale.swapxy && !sc->sc_rawmode) { 419 /* Swap X/Y-Axis */ 420 tp->y = x; 421 tp->x = y; 422 } else { 423 tp->x = x; 424 tp->y = y; 425 } 426 if (!sc->sc_rawmode && 427 (sc->sc_tsscale.maxx - sc->sc_tsscale.minx) != 0 && 428 (sc->sc_tsscale.maxy - sc->sc_tsscale.miny) != 0) { 429 /* Scale down to the screen resolution. */ 430 tp->x = ((tp->x - sc->sc_tsscale.minx) * 431 sc->sc_tsscale.resx) / 432 (sc->sc_tsscale.maxx - sc->sc_tsscale.minx); 433 tp->y = ((tp->y - sc->sc_tsscale.miny) * 434 sc->sc_tsscale.resy) / 435 (sc->sc_tsscale.maxy - sc->sc_tsscale.miny); 436 } 437 } else { 438 tp->x = sc->sc_oldx; 439 tp->y = sc->sc_oldy; 440 } 441 tp->z = z; 442 tp->down = down; 443 } 444 445 void 446 uts_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 447 { 448 struct uts_softc *sc = addr; 449 u_int32_t len; 450 int s; 451 struct uts_pos tp; 452 453 if (status == USBD_CANCELLED) 454 return; 455 456 if (status != USBD_NORMAL_COMPLETION) { 457 printf("%s: status %d\n", sc->sc_dev.dv_xname, status); 458 if (status == USBD_STALLED) 459 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 460 return; 461 } 462 463 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 464 465 s = spltty(); 466 467 uts_get_pos(sc, &tp); 468 469 if (len != sc->sc_pkts) { 470 DPRINTF(("%s: bad input length %d != %d\n", 471 sc->sc_dev.dv_xname, len, sc->sc_isize)); 472 splx(s); 473 return; 474 } 475 476 DPRINTF(("%s: tp.down = %d, tp.z = %d, tp.x = %d, tp.y = %d\n", 477 sc->sc_dev.dv_xname, tp.down, tp.z, tp.x, tp.y)); 478 479 wsmouse_input(sc->sc_wsmousedev, tp.down, tp.x, tp.y, tp.z, 0, 480 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y | 481 WSMOUSE_INPUT_ABSOLUTE_Z); 482 sc->sc_oldy = tp.y; 483 sc->sc_oldx = tp.x; 484 485 splx(s); 486 } 487