1 /* $OpenBSD: uvisor.c,v 1.16 2003/08/05 14:51:18 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 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * Handspring Visor (Palmpilot compatible PDA) driver 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/device.h> 49 #include <sys/conf.h> 50 #include <sys/tty.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbhid.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 59 #include <dev/usb/ucomvar.h> 60 61 #ifdef UVISOR_DEBUG 62 #define DPRINTF(x) if (uvisordebug) printf x 63 #define DPRINTFN(n,x) if (uvisordebug>(n)) printf x 64 int uvisordebug = 0; 65 #else 66 #define DPRINTF(x) 67 #define DPRINTFN(n,x) 68 #endif 69 70 #define UVISOR_CONFIG_INDEX 0 71 #define UVISOR_IFACE_INDEX 0 72 73 /* From the Linux driver */ 74 /* 75 * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that 76 * are available to be transfered to the host for the specified endpoint. 77 * Currently this is not used, and always returns 0x0001 78 */ 79 #define UVISOR_REQUEST_BYTES_AVAILABLE 0x01 80 81 /* 82 * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host 83 * is now closing the pipe. An empty packet is sent in response. 84 */ 85 #define UVISOR_CLOSE_NOTIFICATION 0x02 86 87 /* 88 * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to 89 * get the endpoints used by the connection. 90 */ 91 #define UVISOR_GET_CONNECTION_INFORMATION 0x03 92 93 94 /* 95 * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format 96 */ 97 #define UVISOR_MAX_CONN 8 98 struct uvisor_connection_info { 99 uWord num_ports; 100 struct { 101 uByte port_function_id; 102 uByte port; 103 } connections[UVISOR_MAX_CONN]; 104 }; 105 #define UVISOR_CONNECTION_INFO_SIZE 18 106 107 /* struct uvisor_connection_info.connection[x].port_function_id defines: */ 108 #define UVISOR_FUNCTION_GENERIC 0x00 109 #define UVISOR_FUNCTION_DEBUGGER 0x01 110 #define UVISOR_FUNCTION_HOTSYNC 0x02 111 #define UVISOR_FUNCTION_CONSOLE 0x03 112 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04 113 114 /* 115 * Unknown PalmOS stuff. 116 */ 117 #define UVISOR_GET_PALM_INFORMATION 0x04 118 #define UVISOR_GET_PALM_INFORMATION_LEN 0x44 119 120 struct uvisor_palm_connection_info { 121 uByte num_ports; 122 uByte endpoint_numbers_different; 123 uWord reserved1; 124 struct { 125 uDWord port_function_id; 126 uByte port; 127 uByte end_point_info; 128 uWord reserved; 129 } connections[UVISOR_MAX_CONN]; 130 }; 131 132 #define UVISORIBUFSIZE 64 133 #define UVISOROBUFSIZE 1024 134 135 struct uvisor_softc { 136 USBBASEDEVICE sc_dev; /* base device */ 137 usbd_device_handle sc_udev; /* device */ 138 usbd_interface_handle sc_iface; /* interface */ 139 140 device_ptr_t sc_subdevs[UVISOR_MAX_CONN]; 141 int sc_numcon; 142 143 u_int16_t sc_flags; 144 145 u_char sc_dying; 146 }; 147 148 Static usbd_status uvisor_init(struct uvisor_softc *, 149 struct uvisor_connection_info *, 150 struct uvisor_palm_connection_info *); 151 152 Static void uvisor_close(void *, int); 153 154 155 struct ucom_methods uvisor_methods = { 156 NULL, 157 NULL, 158 NULL, 159 NULL, 160 NULL, 161 uvisor_close, 162 NULL, 163 NULL, 164 }; 165 166 struct uvisor_type { 167 struct usb_devno uv_dev; 168 u_int16_t uv_flags; 169 #define PALM4 0x0001 170 #define VISOR 0x0002 171 }; 172 static const struct uvisor_type uvisor_devs[] = { 173 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR }, 174 {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 }, 175 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 }, 176 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 }, 177 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 }, 178 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 }, 179 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 }, 180 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 }, 181 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 }, 182 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 }, 183 {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 }, 184 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 }, 185 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 }, 186 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 }, 187 {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 }, 188 /* {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/ 189 }; 190 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p)) 191 192 USB_DECLARE_DRIVER(uvisor); 193 194 USB_MATCH(uvisor) 195 { 196 USB_MATCH_START(uvisor, uaa); 197 198 if (uaa->iface != NULL) 199 return (UMATCH_NONE); 200 201 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n", 202 uaa->vendor, uaa->product)); 203 204 return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ? 205 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 206 } 207 208 USB_ATTACH(uvisor) 209 { 210 USB_ATTACH_START(uvisor, sc, uaa); 211 usbd_device_handle dev = uaa->device; 212 usbd_interface_handle iface; 213 usb_interface_descriptor_t *id; 214 struct uvisor_connection_info coninfo; 215 struct uvisor_palm_connection_info palmconinfo; 216 usb_endpoint_descriptor_t *ed; 217 char devinfo[1024]; 218 char *devname = USBDEVNAME(sc->sc_dev); 219 int i, j, hasin, hasout, port; 220 usbd_status err; 221 struct ucom_attach_args uca; 222 223 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc)); 224 225 /* Move the device into the configured state. */ 226 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1); 227 if (err) { 228 printf("\n%s: failed to set configuration, err=%s\n", 229 devname, usbd_errstr(err)); 230 goto bad; 231 } 232 233 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface); 234 if (err) { 235 printf("\n%s: failed to get interface, err=%s\n", 236 devname, usbd_errstr(err)); 237 goto bad; 238 } 239 240 usbd_devinfo(dev, 0, devinfo, sizeof devinfo); 241 USB_ATTACH_SETUP; 242 printf("%s: %s\n", devname, devinfo); 243 244 sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags; 245 246 if ((sc->sc_flags & (VISOR | PALM4)) == 0) { 247 printf("%s: init failed, device type is neither visor nor palm\n", 248 USBDEVNAME(sc->sc_dev)); 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", USBDEVNAME(sc->sc_dev), 269 usbd_errstr(err)); 270 goto bad; 271 } 272 273 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 274 USBDEV(sc->sc_dev)); 275 276 if (sc->sc_flags & VISOR) { 277 sc->sc_numcon = UGETW(coninfo.num_ports); 278 if (sc->sc_numcon > UVISOR_MAX_CONN) 279 sc->sc_numcon = UVISOR_MAX_CONN; 280 281 /* Attach a ucom for each connection. */ 282 for (i = 0; i < sc->sc_numcon; ++i) { 283 switch (coninfo.connections[i].port_function_id) { 284 case UVISOR_FUNCTION_GENERIC: 285 uca.info = "Generic"; 286 break; 287 case UVISOR_FUNCTION_DEBUGGER: 288 uca.info = "Debugger"; 289 break; 290 case UVISOR_FUNCTION_HOTSYNC: 291 uca.info = "HotSync"; 292 break; 293 case UVISOR_FUNCTION_REMOTE_FILE_SYS: 294 uca.info = "Remote File System"; 295 break; 296 default: 297 uca.info = "unknown"; 298 break; 299 } 300 port = coninfo.connections[i].port; 301 uca.portno = port; 302 uca.bulkin = port | UE_DIR_IN; 303 uca.bulkout = port | UE_DIR_OUT; 304 /* Verify that endpoints exist. */ 305 hasin = 0; 306 hasout = 0; 307 for (j = 0; j < id->bNumEndpoints; j++) { 308 ed = usbd_interface2endpoint_descriptor(iface, j); 309 if (ed == NULL) 310 break; 311 if (UE_GET_ADDR(ed->bEndpointAddress) == port && 312 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 313 if (UE_GET_DIR(ed->bEndpointAddress) 314 == UE_DIR_IN) 315 hasin++; 316 else 317 hasout++; 318 } 319 } 320 if (hasin == 1 && hasout == 1) 321 sc->sc_subdevs[i] = config_found_sm(self, &uca, 322 ucomprint, ucomsubmatch); 323 else 324 printf("%s: no proper endpoints for port %d (%d,%d)\n", 325 USBDEVNAME(sc->sc_dev), port, hasin, hasout); 326 } 327 } else { 328 sc->sc_numcon = palmconinfo.num_ports; 329 if (sc->sc_numcon > UVISOR_MAX_CONN) 330 sc->sc_numcon = UVISOR_MAX_CONN; 331 332 /* Attach a ucom for each connection. */ 333 for (i = 0; i < sc->sc_numcon; ++i) { 334 /* 335 * XXX this should copy out 4-char string from the 336 * XXX port_function_id, but where would the string go? 337 * XXX uca.info is a const char *, not an array. 338 */ 339 uca.info = "sync"; 340 uca.portno = i; 341 if (palmconinfo.endpoint_numbers_different) { 342 port = palmconinfo.connections[i].end_point_info; 343 uca.bulkin = (port >> 4) | UE_DIR_IN; 344 uca.bulkout = (port & 0xf) | UE_DIR_OUT; 345 } else { 346 port = palmconinfo.connections[i].port; 347 uca.bulkin = port | UE_DIR_IN; 348 uca.bulkout = port | UE_DIR_OUT; 349 } 350 sc->sc_subdevs[i] = config_found_sm(self, &uca, 351 ucomprint, ucomsubmatch); 352 } 353 } 354 355 USB_ATTACH_SUCCESS_RETURN; 356 357 bad: 358 DPRINTF(("uvisor_attach: ATTACH ERROR\n")); 359 sc->sc_dying = 1; 360 USB_ATTACH_ERROR_RETURN; 361 } 362 363 int 364 uvisor_activate(device_ptr_t self, enum devact act) 365 { 366 struct uvisor_softc *sc = (struct uvisor_softc *)self; 367 int rv = 0; 368 int i; 369 370 switch (act) { 371 case DVACT_ACTIVATE: 372 return (EOPNOTSUPP); 373 break; 374 375 case DVACT_DEACTIVATE: 376 for (i = 0; i < sc->sc_numcon; i++) 377 if (sc->sc_subdevs[i] != NULL) 378 rv = config_deactivate(sc->sc_subdevs[i]); 379 sc->sc_dying = 1; 380 break; 381 } 382 return (rv); 383 } 384 385 int 386 uvisor_detach(device_ptr_t self, int flags) 387 { 388 struct uvisor_softc *sc = (struct uvisor_softc *)self; 389 int rv = 0; 390 int i; 391 392 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags)); 393 sc->sc_dying = 1; 394 for (i = 0; i < sc->sc_numcon; i++) { 395 if (sc->sc_subdevs[i] != NULL) { 396 rv |= config_detach(sc->sc_subdevs[i], flags); 397 sc->sc_subdevs[i] = NULL; 398 } 399 } 400 401 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 402 USBDEV(sc->sc_dev)); 403 404 return (rv); 405 } 406 407 usbd_status 408 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci, 409 struct uvisor_palm_connection_info *cpi) 410 { 411 usbd_status err; 412 usb_device_request_t req; 413 int actlen; 414 uWord avail; 415 416 if (sc->sc_flags & VISOR) { 417 DPRINTF(("uvisor_init: getting Visor connection info\n")); 418 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 419 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION; 420 USETW(req.wValue, 0); 421 USETW(req.wIndex, 0); 422 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 423 err = usbd_do_request_flags(sc->sc_udev, &req, ci, 424 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 425 if (err) 426 return (err); 427 } 428 429 if (sc->sc_flags & PALM4) { 430 DPRINTF(("uvisor_init: getting Palm connection info\n")); 431 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 432 req.bRequest = UVISOR_GET_PALM_INFORMATION; 433 USETW(req.wValue, 0); 434 USETW(req.wIndex, 0); 435 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN); 436 err = usbd_do_request_flags(sc->sc_udev, &req, cpi, 437 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 438 if (err) 439 return (err); 440 } 441 442 DPRINTF(("uvisor_init: getting available bytes\n")); 443 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 444 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE; 445 USETW(req.wValue, 0); 446 USETW(req.wIndex, 5); 447 USETW(req.wLength, sizeof avail); 448 err = usbd_do_request(sc->sc_udev, &req, &avail); 449 if (err) 450 return (err); 451 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail))); 452 453 DPRINTF(("uvisor_init: done\n")); 454 return (err); 455 } 456 457 void 458 uvisor_close(void *addr, int portno) 459 { 460 struct uvisor_softc *sc = addr; 461 usb_device_request_t req; 462 struct uvisor_connection_info coninfo; /* XXX ? */ 463 int actlen; 464 465 if (sc->sc_dying) 466 return; 467 468 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */ 469 req.bRequest = UVISOR_CLOSE_NOTIFICATION; 470 USETW(req.wValue, 0); 471 USETW(req.wIndex, 0); 472 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 473 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo, 474 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 475 } 476