1 /* $OpenBSD: uts.c,v 1.22 2007/10/11 18:33:15 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 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 void 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 int i, found; 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 sc->sc_dying = 1; 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 sc->sc_dying = 1; 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 sc->sc_dying = 1; 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 found = 0; 195 196 for (i = 0; i < id->bNumEndpoints; i++) { 197 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 198 if (ed == NULL) { 199 printf("%s: no endpoint descriptor for %d\n", 200 sc->sc_dev.dv_xname, i); 201 sc->sc_dying = 1; 202 return; 203 } 204 205 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 206 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 207 sc->sc_intr_number = ed->bEndpointAddress; 208 sc->sc_isize = UGETW(ed->wMaxPacketSize); 209 } 210 } 211 212 if (sc->sc_intr_number== -1) { 213 printf("%s: Could not find interrupt in\n", 214 sc->sc_dev.dv_xname); 215 sc->sc_dying = 1; 216 return; 217 } 218 219 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, &sc->sc_dev); 220 221 a.accessops = &uts_accessops; 222 a.accesscookie = sc; 223 224 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 225 } 226 227 int 228 uts_detach(struct device *self, int flags) 229 { 230 struct uts_softc *sc = (struct uts_softc *)self; 231 int rv = 0; 232 233 if (sc->sc_intr_pipe != NULL) { 234 usbd_abort_pipe(sc->sc_intr_pipe); 235 usbd_close_pipe(sc->sc_intr_pipe); 236 sc->sc_intr_pipe = NULL; 237 } 238 239 sc->sc_dying = 1; 240 241 if (sc->sc_wsmousedev != NULL) { 242 rv = config_detach(sc->sc_wsmousedev, flags); 243 sc->sc_wsmousedev = NULL; 244 } 245 246 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev); 247 248 return (rv); 249 } 250 251 int 252 uts_activate(struct device *self, enum devact act) 253 { 254 struct uts_softc *sc = (struct uts_softc *)self; 255 int rv = 0; 256 257 switch (act) { 258 case DVACT_ACTIVATE: 259 break; 260 261 case DVACT_DEACTIVATE: 262 if (sc->sc_wsmousedev != NULL) 263 rv = config_deactivate(sc->sc_wsmousedev); 264 sc->sc_dying = 1; 265 break; 266 } 267 268 return (rv); 269 } 270 271 int 272 uts_enable(void *v) 273 { 274 struct uts_softc *sc = v; 275 int err; 276 277 if (sc->sc_dying) 278 return (EIO); 279 280 if (sc->sc_enabled) 281 return (EBUSY); 282 283 if (sc->sc_isize == 0) 284 return (0); 285 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 286 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 287 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf, 288 sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL); 289 if (err) { 290 free(sc->sc_ibuf, M_USBDEV); 291 sc->sc_intr_pipe = NULL; 292 return (EIO); 293 } 294 295 sc->sc_enabled = 1; 296 sc->sc_buttons = 0; 297 298 return (0); 299 } 300 301 void 302 uts_disable(void *v) 303 { 304 struct uts_softc *sc = v; 305 306 if (!sc->sc_enabled) { 307 printf("uts_disable: already disabled!\n"); 308 return; 309 } 310 311 /* Disable interrupts. */ 312 if (sc->sc_intr_pipe != NULL) { 313 usbd_abort_pipe(sc->sc_intr_pipe); 314 usbd_close_pipe(sc->sc_intr_pipe); 315 sc->sc_intr_pipe = NULL; 316 } 317 318 if (sc->sc_ibuf != NULL) { 319 free(sc->sc_ibuf, M_USBDEV); 320 sc->sc_ibuf = NULL; 321 } 322 323 sc->sc_enabled = 0; 324 } 325 326 int 327 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l) 328 { 329 int error = 0; 330 struct uts_softc *sc = v; 331 struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data; 332 333 DPRINTF(("uts_ioctl(%d, '%c', %d)\n", 334 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff)); 335 336 switch (cmd) { 337 case WSMOUSEIO_SCALIBCOORDS: 338 if (!(wsmc->minx >= 0 && wsmc->maxx >= 0 && 339 wsmc->miny >= 0 && wsmc->maxy >= 0 && 340 wsmc->resx >= 0 && wsmc->resy >= 0 && 341 wsmc->minx < 32768 && wsmc->maxx < 32768 && 342 wsmc->miny < 32768 && wsmc->maxy < 32768 && 343 wsmc->resx < 32768 && wsmc->resy < 32768 && 344 wsmc->swapxy >= 0 && wsmc->swapxy <= 1 && 345 wsmc->samplelen >= 0 && wsmc->samplelen <= 1)) 346 return (EINVAL); 347 348 sc->sc_tsscale.minx = wsmc->minx; 349 sc->sc_tsscale.maxx = wsmc->maxx; 350 sc->sc_tsscale.miny = wsmc->miny; 351 sc->sc_tsscale.maxy = wsmc->maxy; 352 sc->sc_tsscale.swapxy = wsmc->swapxy; 353 sc->sc_tsscale.resx = wsmc->resx; 354 sc->sc_tsscale.resy = wsmc->resy; 355 sc->sc_rawmode = wsmc->samplelen; 356 break; 357 case WSMOUSEIO_GCALIBCOORDS: 358 wsmc->minx = sc->sc_tsscale.minx; 359 wsmc->maxx = sc->sc_tsscale.maxx; 360 wsmc->miny = sc->sc_tsscale.miny; 361 wsmc->maxy = sc->sc_tsscale.maxy; 362 wsmc->swapxy = sc->sc_tsscale.swapxy; 363 wsmc->resx = sc->sc_tsscale.resx; 364 wsmc->resy = sc->sc_tsscale.resy; 365 wsmc->samplelen = sc->sc_rawmode; 366 break; 367 case WSMOUSEIO_GTYPE: 368 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 369 break; 370 default: 371 error = ENOTTY; 372 break; 373 } 374 375 return (error); 376 } 377 378 void 379 uts_get_pos(usbd_private_handle addr, struct uts_pos *tp) 380 { 381 struct uts_softc *sc = addr; 382 u_char *p = sc->sc_ibuf; 383 int down, x, y, z; 384 385 switch (sc->sc_product) { 386 case USB_PRODUCT_FTDI_ITM_TOUCH: 387 down = (~p[7] & 0x20); 388 x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f); 389 /* Invert the Y coordinate */ 390 y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f)); 391 z = ((p[2] & 0x1) << 7) | (p[5] & 0x7f); 392 sc->sc_pkts = 0x8; 393 break; 394 case USB_PRODUCT_EGALAX_TPANEL: 395 case USB_PRODUCT_EGALAX_TPANEL2: 396 /* 397 * eGalax and Gunze USB touch panels have the same device ID, 398 * so decide upon the vendor ID. 399 */ 400 switch (sc->sc_vendor) { 401 case USB_VENDOR_EGALAX: 402 down = (p[0] & 0x01); 403 /* Invert the X coordiate */ 404 x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f)); 405 y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f); 406 z = down; 407 sc->sc_pkts = 0x5; 408 break; 409 case USB_VENDOR_GUNZE: 410 down = (~p[7] & 0x20); 411 /* Invert the X coordinate */ 412 x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f)); 413 y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f); 414 z = (down != 0); 415 sc->sc_pkts = 0x4; 416 break; 417 } 418 break; 419 } 420 421 DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n", 422 sc->sc_dev.dv_xname, down, sc->sc_pkts)); 423 424 /* x/y values are not reliable if there is no pressure */ 425 if (down) { 426 if (sc->sc_tsscale.swapxy && !sc->sc_rawmode) { 427 /* Swap X/Y-Axis */ 428 tp->y = x; 429 tp->x = y; 430 } else { 431 tp->x = x; 432 tp->y = y; 433 } 434 if (!sc->sc_rawmode) { 435 /* Scale down to the screen resolution. */ 436 tp->x = ((tp->x - sc->sc_tsscale.minx) * 437 sc->sc_tsscale.resx) / 438 (sc->sc_tsscale.maxx - sc->sc_tsscale.minx); 439 tp->y = ((tp->y - sc->sc_tsscale.miny) * 440 sc->sc_tsscale.resy) / 441 (sc->sc_tsscale.maxy - sc->sc_tsscale.miny); 442 } 443 } else { 444 tp->x = sc->sc_oldx; 445 tp->y = sc->sc_oldy; 446 } 447 tp->z = z; 448 tp->down = down; 449 } 450 451 void 452 uts_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 453 { 454 struct uts_softc *sc = addr; 455 u_int32_t len; 456 int s; 457 struct uts_pos tp; 458 459 if (status == USBD_CANCELLED) 460 return; 461 462 if (status != USBD_NORMAL_COMPLETION) { 463 printf("%s: status %d\n", sc->sc_dev.dv_xname, status); 464 if (status == USBD_STALLED) 465 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 466 return; 467 } 468 469 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 470 471 s = spltty(); 472 473 uts_get_pos(sc, &tp); 474 475 if (len != sc->sc_pkts) { 476 DPRINTF(("%s: bad input length %d != %d\n", 477 sc->sc_dev.dv_xname, len, sc->sc_isize)); 478 splx(s); 479 return; 480 } 481 482 DPRINTF(("%s: tp.down = %d, tp.z = %d, tp.x = %d, tp.y = %d\n", 483 sc->sc_dev.dv_xname, tp.down, tp.z, tp.x, tp.y)); 484 485 wsmouse_input(sc->sc_wsmousedev, tp.down, tp.x, tp.y, tp.z, 0, 486 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y | 487 WSMOUSE_INPUT_ABSOLUTE_Z); 488 sc->sc_oldy = tp.y; 489 sc->sc_oldx = tp.x; 490 491 splx(s); 492 } 493