1 /* $OpenBSD: uvisor.c,v 1.49 2014/07/12 21:24:33 mpi 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/kernel.h> 41 #include <sys/device.h> 42 #include <sys/conf.h> 43 #include <sys/tty.h> 44 45 #include <dev/usb/usb.h> 46 #include <dev/usb/usbhid.h> 47 48 #include <dev/usb/usbdi.h> 49 #include <dev/usb/usbdi_util.h> 50 #include <dev/usb/usbdevs.h> 51 52 #include <dev/usb/ucomvar.h> 53 54 #ifdef UVISOR_DEBUG 55 #define DPRINTF(x) if (uvisordebug) printf x 56 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x 57 int uvisordebug = 0; 58 #else 59 #define DPRINTF(x) 60 #define DPRINTFN(n,x) 61 #endif 62 63 #define UVISOR_CONFIG_INDEX 0 64 #define UVISOR_IFACE_INDEX 0 65 66 /* From the Linux driver */ 67 /* 68 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that 69 * are available to be transferred to the host for the specified endpoint. 70 * Currently this is not used, and always returns 0x0001 71 */ 72 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01 73 74 /* 75 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host 76 * is now closing the pipe. An empty packet is sent in response. 77 */ 78 #define UVISOR_CLOSE_NOTIFICATION 0x02 79 80 /* 81 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to 82 * get the endpoints used by the connection. 83 */ 84 #define UVISOR_GET_CONNECTION_INFORMATION 0x03 85 86 87 /* 88 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format 89 */ 90 #define UVISOR_MAX_CONN 8 91 struct uvisor_connection_info { 92 uWord num_ports; 93 struct { 94 uByte port_function_id; 95 uByte port; 96 } connections[UVISOR_MAX_CONN]; 97 }; 98 #define UVISOR_CONNECTION_INFO_SIZE 18 99 100 /* struct uvisor_connection_info.connection[x].port_function_id defines: */ 101 #define UVISOR_FUNCTION_GENERIC 0x00 102 #define UVISOR_FUNCTION_DEBUGGER 0x01 103 #define UVISOR_FUNCTION_HOTSYNC 0x02 104 #define UVISOR_FUNCTION_CONSOLE 0x03 105 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04 106 107 /* 108 * Unknown PalmOS stuff. 109 */ 110 #define UVISOR_GET_PALM_INFORMATION 0x04 111 #define UVISOR_GET_PALM_INFORMATION_LEN 0x44 112 113 struct uvisor_palm_connection_info { 114 uByte num_ports; 115 uByte endpoint_numbers_different; 116 uWord reserved1; 117 struct { 118 uDWord port_function_id; 119 uByte port; 120 uByte end_point_info; 121 uWord reserved; 122 } connections[UVISOR_MAX_CONN]; 123 }; 124 125 #define UVISORIBUFSIZE 64 126 #define UVISOROBUFSIZE 1024 127 128 struct uvisor_softc { 129 struct device sc_dev; /* base device */ 130 struct usbd_device *sc_udev; /* device */ 131 struct usbd_interface *sc_iface; /* interface */ 132 /* 133 * added sc_vendor for later interrogation in failed initialisations 134 */ 135 int sc_vendor; /* USB device vendor */ 136 137 struct device *sc_subdevs[UVISOR_MAX_CONN]; 138 int sc_numcon; 139 140 u_int16_t sc_flags; 141 }; 142 143 usbd_status uvisor_init(struct uvisor_softc *, 144 struct uvisor_connection_info *, 145 struct uvisor_palm_connection_info *); 146 147 void uvisor_close(void *, int); 148 149 150 struct ucom_methods uvisor_methods = { 151 NULL, 152 NULL, 153 NULL, 154 NULL, 155 NULL, 156 uvisor_close, 157 NULL, 158 NULL, 159 }; 160 161 struct uvisor_type { 162 struct usb_devno uv_dev; 163 u_int16_t uv_flags; 164 #define PALM4 0x0001 165 #define VISOR 0x0002 166 #define NOFRE 0x0004 167 #define CLIE4 (VISOR|NOFRE) 168 }; 169 static const struct uvisor_type uvisor_devs[] = { 170 {{ USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000 }, PALM4 }, 171 {{ USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA }, PALM4 }, 172 {{ USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE3600 }, PALM4 }, 173 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR }, 174 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 }, 175 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, VISOR }, 176 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 }, 177 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 }, 178 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 }, 179 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 }, 180 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 }, 181 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 }, 182 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 }, 183 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 }, 184 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 }, 185 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE_31 }, PALM4 }, 186 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 }, 187 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 }, 188 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 }, 189 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 }, 190 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TJ25 }, PALM4 }, 191 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/ 192 {{ USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC }, PALM4 }, 193 }; 194 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p)) 195 196 int uvisor_match(struct device *, void *, void *); 197 void uvisor_attach(struct device *, struct device *, void *); 198 int uvisor_detach(struct device *, int); 199 200 struct cfdriver uvisor_cd = { 201 NULL, "uvisor", DV_DULL 202 }; 203 204 const struct cfattach uvisor_ca = { 205 sizeof(struct uvisor_softc), uvisor_match, uvisor_attach, uvisor_detach 206 }; 207 208 int 209 uvisor_match(struct device *parent, void *match, void *aux) 210 { 211 struct usb_attach_arg *uaa = aux; 212 213 if (uaa->iface != NULL) 214 return (UMATCH_NONE); 215 216 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n", 217 uaa->vendor, uaa->product)); 218 219 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ? 220 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 221 } 222 223 void 224 uvisor_attach(struct device *parent, struct device *self, void *aux) 225 { 226 struct uvisor_softc *sc = (struct uvisor_softc *)self; 227 struct usb_attach_arg *uaa = aux; 228 struct usbd_device *dev = uaa->device; 229 struct usbd_interface *iface; 230 usb_interface_descriptor_t *id; 231 struct uvisor_connection_info coninfo; 232 struct uvisor_palm_connection_info palmconinfo; 233 usb_endpoint_descriptor_t *ed; 234 int i, j, hasin, hasout, port; 235 usbd_status err; 236 struct ucom_attach_args uca; 237 238 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc)); 239 240 /* Move the device into the configured state. */ 241 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1); 242 if (err) { 243 printf(": failed to set configuration, err=%s\n", 244 usbd_errstr(err)); 245 goto bad; 246 } 247 248 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface); 249 if (err) { 250 printf(": failed to get interface, err=%s\n", 251 usbd_errstr(err)); 252 goto bad; 253 } 254 255 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags; 256 sc->sc_vendor = uaa->vendor; 257 258 if ((sc->sc_flags & (VISOR | PALM4)) == 0) { 259 printf("%s: device is neither visor nor palm\n", 260 sc->sc_dev.dv_xname); 261 goto bad; 262 } 263 264 id = usbd_get_interface_descriptor(iface); 265 266 sc->sc_udev = dev; 267 sc->sc_iface = iface; 268 269 uca.ibufsize = UVISORIBUFSIZE; 270 uca.obufsize = UVISOROBUFSIZE; 271 uca.ibufsizepad = UVISORIBUFSIZE; 272 uca.opkthdrlen = 0; 273 uca.device = dev; 274 uca.iface = iface; 275 uca.methods = &uvisor_methods; 276 uca.arg = sc; 277 278 err = uvisor_init(sc, &coninfo, &palmconinfo); 279 if (err) { 280 printf("%s: init failed, %s\n", sc->sc_dev.dv_xname, 281 usbd_errstr(err)); 282 goto bad; 283 } 284 285 if (sc->sc_flags & VISOR) { 286 sc->sc_numcon = UGETW(coninfo.num_ports); 287 if (sc->sc_numcon > UVISOR_MAX_CONN) 288 sc->sc_numcon = UVISOR_MAX_CONN; 289 290 /* Attach a ucom for each connection. */ 291 for (i = 0; i < sc->sc_numcon; ++i) { 292 switch (coninfo.connections[i].port_function_id) { 293 case UVISOR_FUNCTION_GENERIC: 294 uca.info = "Generic"; 295 break; 296 case UVISOR_FUNCTION_DEBUGGER: 297 uca.info = "Debugger"; 298 break; 299 case UVISOR_FUNCTION_HOTSYNC: 300 uca.info = "HotSync"; 301 break; 302 case UVISOR_FUNCTION_REMOTE_FILE_SYS: 303 uca.info = "Remote File System"; 304 break; 305 default: 306 uca.info = "unknown"; 307 break; 308 } 309 port = coninfo.connections[i].port; 310 uca.portno = port; 311 uca.bulkin = port | UE_DIR_IN; 312 uca.bulkout = port | UE_DIR_OUT; 313 /* Verify that endpoints exist. */ 314 hasin = 0; 315 hasout = 0; 316 for (j = 0; j < id->bNumEndpoints; j++) { 317 ed = usbd_interface2endpoint_descriptor(iface, j); 318 if (ed == NULL) 319 break; 320 if (UE_GET_ADDR(ed->bEndpointAddress) == port && 321 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 322 if (UE_GET_DIR(ed->bEndpointAddress) 323 == UE_DIR_IN) 324 hasin++; 325 else 326 hasout++; 327 } 328 } 329 if (hasin == 1 && hasout == 1) 330 sc->sc_subdevs[i] = config_found_sm(self, &uca, 331 ucomprint, ucomsubmatch); 332 else 333 printf("%s: no proper endpoints for port %d (%d,%d)\n", 334 sc->sc_dev.dv_xname, port, hasin, hasout); 335 } 336 } else { 337 sc->sc_numcon = palmconinfo.num_ports; 338 if (sc->sc_numcon > UVISOR_MAX_CONN) 339 sc->sc_numcon = UVISOR_MAX_CONN; 340 341 /* Attach a ucom for each connection. */ 342 for (i = 0; i < sc->sc_numcon; ++i) { 343 /* 344 * XXX this should copy out 4-char string from the 345 * XXX port_function_id, but where would the string go? 346 * XXX uca.info is a const char *, not an array. 347 */ 348 uca.info = "sync"; 349 uca.portno = i; 350 if (palmconinfo.endpoint_numbers_different) { 351 port = palmconinfo.connections[i].end_point_info; 352 uca.bulkin = (port >> 4) | UE_DIR_IN; 353 uca.bulkout = (port & 0xf) | UE_DIR_OUT; 354 } else { 355 port = palmconinfo.connections[i].port; 356 uca.bulkin = port | UE_DIR_IN; 357 uca.bulkout = port | UE_DIR_OUT; 358 } 359 sc->sc_subdevs[i] = config_found_sm(self, &uca, 360 ucomprint, ucomsubmatch); 361 } 362 } 363 364 return; 365 366 bad: 367 DPRINTF(("uvisor_attach: ATTACH ERROR\n")); 368 usbd_deactivate(sc->sc_udev); 369 } 370 371 int 372 uvisor_detach(struct device *self, int flags) 373 { 374 struct uvisor_softc *sc = (struct uvisor_softc *)self; 375 int rv = 0; 376 int i; 377 378 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags)); 379 for (i = 0; i < sc->sc_numcon; i++) { 380 if (sc->sc_subdevs[i] != NULL) { 381 rv |= config_detach(sc->sc_subdevs[i], flags); 382 sc->sc_subdevs[i] = NULL; 383 } 384 } 385 386 return (rv); 387 } 388 389 usbd_status 390 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci, 391 struct uvisor_palm_connection_info *cpi) 392 { 393 usbd_status err = 0; 394 usb_device_request_t req; 395 int actlen; 396 uWord avail; 397 398 if (sc->sc_flags & PALM4) { 399 DPRINTF(("uvisor_init: getting Palm connection info\n")); 400 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 401 req.bRequest = UVISOR_GET_PALM_INFORMATION; 402 USETW(req.wValue, 0); 403 USETW(req.wIndex, 0); 404 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN); 405 err = usbd_do_request_flags(sc->sc_udev, &req, cpi, 406 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 407 if (err == USBD_STALLED && sc->sc_vendor == USB_VENDOR_SONY) { 408 /* some sony clie devices stall on palm4 requests, 409 * switch them over to using visor. dont do free space 410 * checks on them since they dont like them either. 411 */ 412 DPRINTF(("switching role for CLIE probe\n")); 413 sc->sc_flags = CLIE4; 414 err = 0; 415 } 416 if (err) 417 return (err); 418 } 419 420 if (sc->sc_flags & VISOR) { 421 DPRINTF(("uvisor_init: getting Visor connection info\n")); 422 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 423 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION; 424 USETW(req.wValue, 0); 425 USETW(req.wIndex, 0); 426 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 427 err = usbd_do_request_flags(sc->sc_udev, &req, ci, 428 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 429 if (err) 430 return (err); 431 } 432 433 if (sc->sc_flags & NOFRE) 434 return (err); 435 436 DPRINTF(("uvisor_init: getting available bytes\n")); 437 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 438 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE; 439 USETW(req.wValue, 0); 440 USETW(req.wIndex, 5); 441 USETW(req.wLength, sizeof avail); 442 err = usbd_do_request(sc->sc_udev, &req, &avail); 443 if (err) 444 return (err); 445 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail))); 446 DPRINTF(("uvisor_init: done\n")); 447 return (err); 448 } 449 450 void 451 uvisor_close(void *addr, int portno) 452 { 453 struct uvisor_softc *sc = addr; 454 usb_device_request_t req; 455 struct uvisor_connection_info coninfo; /* XXX ? */ 456 int actlen; 457 458 if (usbd_is_dying(sc->sc_udev)) 459 return; 460 461 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */ 462 req.bRequest = UVISOR_CLOSE_NOTIFICATION; 463 USETW(req.wValue, 0); 464 USETW(req.wIndex, 0); 465 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 466 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo, 467 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 468 } 469