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