jcs's openbsd hax
openbsd
at jcs 456 lines 13 kB view raw
1/* $OpenBSD: uvisor.c,v 1.55 2026/02/15 19:10:15 jcs Exp $ */ 2/* $NetBSD: uvisor.c,v 1.21 2003/08/03 21:59:26 nathanw Exp $ */ 3 4/* 5 * Copyright (c) 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * Handspring Visor (Palmpilot compatible PDA) driver 36 */ 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/device.h> 41#include <sys/tty.h> 42 43#include <dev/usb/usb.h> 44 45#include <dev/usb/usbdi.h> 46#include <dev/usb/usbdevs.h> 47 48#include <dev/usb/ucomvar.h> 49 50#ifdef UVISOR_DEBUG 51#define DPRINTF(x) if (uvisordebug) printf x 52#define DPRINTFN(n,x) if (uvisordebug>(n)) printf x 53int uvisordebug = 0; 54#else 55#define DPRINTF(x) 56#define DPRINTFN(n,x) 57#endif 58 59#define UVISOR_IFACE_INDEX 0 60 61/* From the Linux driver */ 62/* 63 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that 64 * are available to be transferred to the host for the specified endpoint. 65 * Currently this is not used, and always returns 0x0001 66 */ 67#define UVISOR_REQUEST_BYTES_AVAILABLE 0x01 68 69/* 70 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host 71 * is now closing the pipe. An empty packet is sent in response. 72 */ 73#define UVISOR_CLOSE_NOTIFICATION 0x02 74 75/* 76 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to 77 * get the endpoints used by the connection. 78 */ 79#define UVISOR_GET_CONNECTION_INFORMATION 0x03 80 81 82/* 83 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format 84 */ 85#define UVISOR_MAX_CONN 8 86struct uvisor_connection_info { 87 uWord num_ports; 88 struct { 89 uByte port_function_id; 90 uByte port; 91 } connections[UVISOR_MAX_CONN]; 92}; 93#define UVISOR_CONNECTION_INFO_SIZE 18 94 95/* struct uvisor_connection_info.connection[x].port_function_id defines: */ 96#define UVISOR_FUNCTION_GENERIC 0x00 97#define UVISOR_FUNCTION_DEBUGGER 0x01 98#define UVISOR_FUNCTION_HOTSYNC 0x02 99#define UVISOR_FUNCTION_CONSOLE 0x03 100#define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04 101 102/* 103 * Unknown PalmOS stuff. 104 */ 105#define UVISOR_GET_PALM_INFORMATION 0x04 106#define UVISOR_GET_PALM_INFORMATION_LEN 0x44 107 108struct uvisor_palm_connection_info { 109 uByte num_ports; 110 uByte endpoint_numbers_different; 111 uWord reserved1; 112 struct { 113 uDWord port_function_id; 114 uByte port; 115 uByte end_point_info; 116 uWord reserved; 117 } connections[UVISOR_MAX_CONN]; 118}; 119 120#define UVISORIBUFSIZE 64 121#define UVISOROBUFSIZE 1024 122 123struct uvisor_softc { 124 struct device sc_dev; /* base device */ 125 struct usbd_device *sc_udev; /* device */ 126 struct usbd_interface *sc_iface; /* interface */ 127/* 128 * added sc_vendor for later interrogation in failed initialisations 129 */ 130 int sc_vendor; /* USB device vendor */ 131 132 struct device *sc_subdevs[UVISOR_MAX_CONN]; 133 int sc_numcon; 134 135 u_int16_t sc_flags; 136}; 137 138usbd_status uvisor_init(struct uvisor_softc *, 139 struct uvisor_connection_info *, 140 struct uvisor_palm_connection_info *); 141 142void uvisor_close(void *, int); 143 144 145const struct ucom_methods uvisor_methods = { 146 NULL, 147 NULL, 148 NULL, 149 NULL, 150 NULL, 151 uvisor_close, 152 NULL, 153 NULL, 154}; 155 156struct uvisor_type { 157 struct usb_devno uv_dev; 158 u_int16_t uv_flags; 159#define PALM4 0x0001 160#define VISOR 0x0002 161#define NOFRE 0x0004 162#define CLIE4 (VISOR|NOFRE) 163}; 164static const struct uvisor_type uvisor_devs[] = { 165 {{ USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000 }, PALM4 }, 166 {{ USB_VENDOR_ALPHASMART, USB_PRODUCT_ALPHASMART_DANA }, PALM4 }, 167 {{ USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA }, PALM4 }, 168 {{ USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE3600 }, PALM4 }, 169 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR }, 170 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 }, 171 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, VISOR }, 172 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 }, 173 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 }, 174 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 }, 175 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 }, 176 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 }, 177 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 }, 178 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 }, 179 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 }, 180 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 }, 181 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE_31 }, PALM4 }, 182 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 }, 183 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 }, 184 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 }, 185 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 }, 186 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TJ25 }, PALM4 }, 187/* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/ 188 {{ USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC }, PALM4 }, 189}; 190#define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p)) 191 192int uvisor_match(struct device *, void *, void *); 193void uvisor_attach(struct device *, struct device *, void *); 194int uvisor_detach(struct device *, int); 195 196struct cfdriver uvisor_cd = { 197 NULL, "uvisor", DV_DULL 198}; 199 200const struct cfattach uvisor_ca = { 201 sizeof(struct uvisor_softc), uvisor_match, uvisor_attach, uvisor_detach 202}; 203 204int 205uvisor_match(struct device *parent, void *match, void *aux) 206{ 207 struct usb_attach_arg *uaa = aux; 208 209 if (uaa->iface == NULL) 210 return (UMATCH_NONE); 211 212 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n", 213 uaa->vendor, uaa->product)); 214 215 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ? 216 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 217} 218 219void 220uvisor_attach(struct device *parent, struct device *self, void *aux) 221{ 222 struct uvisor_softc *sc = (struct uvisor_softc *)self; 223 struct usb_attach_arg *uaa = aux; 224 struct usbd_device *dev = uaa->device; 225 struct usbd_interface *iface; 226 usb_interface_descriptor_t *id; 227 struct uvisor_connection_info coninfo; 228 struct uvisor_palm_connection_info palmconinfo; 229 usb_endpoint_descriptor_t *ed; 230 int i, j, hasin, hasout, port; 231 usbd_status err; 232 struct ucom_attach_args uca; 233 234 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc)); 235 236 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface); 237 if (err) { 238 printf(": failed to get interface, err=%s\n", 239 usbd_errstr(err)); 240 goto bad; 241 } 242 243 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags; 244 sc->sc_vendor = uaa->vendor; 245 246 if ((sc->sc_flags & (VISOR | PALM4)) == 0) { 247 printf("%s: device is neither visor nor palm\n", 248 sc->sc_dev.dv_xname); 249 goto bad; 250 } 251 252 id = usbd_get_interface_descriptor(iface); 253 254 sc->sc_udev = dev; 255 sc->sc_iface = iface; 256 257 uca.ibufsize = UVISORIBUFSIZE; 258 uca.obufsize = UVISOROBUFSIZE; 259 uca.ibufsizepad = UVISORIBUFSIZE; 260 uca.opkthdrlen = 0; 261 uca.device = dev; 262 uca.iface = iface; 263 uca.methods = &uvisor_methods; 264 uca.arg = sc; 265 266 err = uvisor_init(sc, &coninfo, &palmconinfo); 267 if (err) { 268 printf("%s: init failed, %s\n", sc->sc_dev.dv_xname, 269 usbd_errstr(err)); 270 goto bad; 271 } 272 273 if (sc->sc_flags & VISOR) { 274 sc->sc_numcon = UGETW(coninfo.num_ports); 275 if (sc->sc_numcon > UVISOR_MAX_CONN) 276 sc->sc_numcon = UVISOR_MAX_CONN; 277 278 /* Attach a ucom for each connection. */ 279 for (i = 0; i < sc->sc_numcon; ++i) { 280 switch (coninfo.connections[i].port_function_id) { 281 case UVISOR_FUNCTION_GENERIC: 282 uca.info = "Generic"; 283 break; 284 case UVISOR_FUNCTION_DEBUGGER: 285 uca.info = "Debugger"; 286 break; 287 case UVISOR_FUNCTION_HOTSYNC: 288 uca.info = "HotSync"; 289 break; 290 case UVISOR_FUNCTION_REMOTE_FILE_SYS: 291 uca.info = "Remote File System"; 292 break; 293 default: 294 uca.info = "unknown"; 295 break; 296 } 297 port = coninfo.connections[i].port; 298 uca.portno = port; 299 uca.bulkin = port | UE_DIR_IN; 300 uca.bulkout = port | UE_DIR_OUT; 301 /* Verify that endpoints exist. */ 302 hasin = 0; 303 hasout = 0; 304 for (j = 0; j < id->bNumEndpoints; j++) { 305 ed = usbd_interface2endpoint_descriptor(iface, j); 306 if (ed == NULL) 307 break; 308 if (UE_GET_ADDR(ed->bEndpointAddress) == port && 309 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 310 if (UE_GET_DIR(ed->bEndpointAddress) 311 == UE_DIR_IN) 312 hasin++; 313 else 314 hasout++; 315 } 316 } 317 if (hasin == 1 && hasout == 1) 318 sc->sc_subdevs[i] = config_found_sm(self, &uca, 319 ucomprint, ucomsubmatch); 320 else 321 printf("%s: no proper endpoints for port %d (%d,%d)\n", 322 sc->sc_dev.dv_xname, port, hasin, hasout); 323 } 324 } else { 325 sc->sc_numcon = palmconinfo.num_ports; 326 if (sc->sc_numcon > UVISOR_MAX_CONN) 327 sc->sc_numcon = UVISOR_MAX_CONN; 328 329 /* Attach a ucom for each connection. */ 330 for (i = 0; i < sc->sc_numcon; ++i) { 331 /* 332 * XXX this should copy out 4-char string from the 333 * XXX port_function_id, but where would the string go? 334 * XXX uca.info is a const char *, not an array. 335 */ 336 uca.info = "sync"; 337 uca.portno = i; 338 if (palmconinfo.endpoint_numbers_different) { 339 port = palmconinfo.connections[i].end_point_info; 340 uca.bulkin = (port >> 4) | UE_DIR_IN; 341 uca.bulkout = (port & 0xf) | UE_DIR_OUT; 342 } else { 343 port = palmconinfo.connections[i].port; 344 uca.bulkin = port | UE_DIR_IN; 345 uca.bulkout = port | UE_DIR_OUT; 346 } 347 sc->sc_subdevs[i] = config_found_sm(self, &uca, 348 ucomprint, ucomsubmatch); 349 } 350 } 351 352 return; 353 354bad: 355 DPRINTF(("uvisor_attach: ATTACH ERROR\n")); 356 usbd_deactivate(sc->sc_udev); 357} 358 359int 360uvisor_detach(struct device *self, int flags) 361{ 362 struct uvisor_softc *sc = (struct uvisor_softc *)self; 363 int rv = 0; 364 int i; 365 366 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags)); 367 for (i = 0; i < sc->sc_numcon; i++) { 368 if (sc->sc_subdevs[i] != NULL) { 369 rv |= config_detach(sc->sc_subdevs[i], flags); 370 sc->sc_subdevs[i] = NULL; 371 } 372 } 373 374 return (rv); 375} 376 377usbd_status 378uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci, 379 struct uvisor_palm_connection_info *cpi) 380{ 381 usbd_status err = 0; 382 usb_device_request_t req; 383 int actlen; 384 uWord avail; 385 386 if (sc->sc_flags & PALM4) { 387 DPRINTF(("uvisor_init: getting Palm connection info\n")); 388 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 389 req.bRequest = UVISOR_GET_PALM_INFORMATION; 390 USETW(req.wValue, 0); 391 USETW(req.wIndex, 0); 392 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN); 393 err = usbd_do_request_flags(sc->sc_udev, &req, cpi, 394 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 395 if (err == USBD_STALLED && sc->sc_vendor == USB_VENDOR_SONY) { 396 /* some sony clie devices stall on palm4 requests, 397 * switch them over to using visor. dont do free space 398 * checks on them since they dont like them either. 399 */ 400 DPRINTF(("switching role for CLIE probe\n")); 401 sc->sc_flags = CLIE4; 402 err = 0; 403 } 404 if (err) 405 return (err); 406 } 407 408 if (sc->sc_flags & VISOR) { 409 DPRINTF(("uvisor_init: getting Visor connection info\n")); 410 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 411 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION; 412 USETW(req.wValue, 0); 413 USETW(req.wIndex, 0); 414 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 415 err = usbd_do_request_flags(sc->sc_udev, &req, ci, 416 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 417 if (err) 418 return (err); 419 } 420 421 if (sc->sc_flags & NOFRE) 422 return (err); 423 424 DPRINTF(("uvisor_init: getting available bytes\n")); 425 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 426 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE; 427 USETW(req.wValue, 0); 428 USETW(req.wIndex, 5); 429 USETW(req.wLength, sizeof avail); 430 err = usbd_do_request(sc->sc_udev, &req, &avail); 431 if (err) 432 return (err); 433 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail))); 434 DPRINTF(("uvisor_init: done\n")); 435 return (err); 436} 437 438void 439uvisor_close(void *addr, int portno) 440{ 441 struct uvisor_softc *sc = addr; 442 usb_device_request_t req; 443 struct uvisor_connection_info coninfo; /* XXX ? */ 444 int actlen; 445 446 if (usbd_is_dying(sc->sc_udev)) 447 return; 448 449 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */ 450 req.bRequest = UVISOR_CLOSE_NOTIFICATION; 451 USETW(req.wValue, 0); 452 USETW(req.wIndex, 0); 453 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 454 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo, 455 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 456}