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