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