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