1 /* $NetBSD: uvisor.c,v 1.13 2002/02/11 15:11:49 augustss 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.13 2002/02/11 15:11:49 augustss 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 110 /* struct uvisor_connection_info.connection[x].port_function_id defines: */ 111 #define UVISOR_FUNCTION_GENERIC 0x00 112 #define UVISOR_FUNCTION_DEBUGGER 0x01 113 #define UVISOR_FUNCTION_HOTSYNC 0x02 114 #define UVISOR_FUNCTION_CONSOLE 0x03 115 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04 116 117 118 #define UVISORIBUFSIZE 1024 119 #define UVISOROBUFSIZE 1024 120 121 struct uvisor_softc { 122 USBBASEDEVICE sc_dev; /* base device */ 123 usbd_device_handle sc_udev; /* device */ 124 usbd_interface_handle sc_iface; /* interface */ 125 126 device_ptr_t sc_subdevs[UVISOR_MAX_CONN]; 127 int sc_numcon; 128 129 u_char sc_dying; 130 }; 131 132 Static usbd_status uvisor_init(struct uvisor_softc *, 133 struct uvisor_connection_info *); 134 135 Static void uvisor_close(void *, int); 136 137 138 struct ucom_methods uvisor_methods = { 139 NULL, 140 NULL, 141 NULL, 142 NULL, 143 NULL, 144 uvisor_close, 145 NULL, 146 NULL, 147 }; 148 149 USB_DECLARE_DRIVER(uvisor); 150 151 USB_MATCH(uvisor) 152 { 153 USB_MATCH_START(uvisor, uaa); 154 155 if (uaa->iface != NULL) 156 return (UMATCH_NONE); 157 158 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n", 159 uaa->vendor, uaa->product)); 160 161 if (uaa->vendor == USB_VENDOR_HANDSPRING && 162 uaa->product == USB_PRODUCT_HANDSPRING_VISOR) 163 return (UMATCH_VENDOR_PRODUCT); 164 165 return (UMATCH_NONE); 166 } 167 168 USB_ATTACH(uvisor) 169 { 170 USB_ATTACH_START(uvisor, sc, uaa); 171 usbd_device_handle dev = uaa->device; 172 usbd_interface_handle iface; 173 usb_interface_descriptor_t *id; 174 struct uvisor_connection_info coninfo; 175 usb_endpoint_descriptor_t *ed; 176 char devinfo[1024]; 177 char *devname = USBDEVNAME(sc->sc_dev); 178 int i, j, hasin, hasout, port; 179 usbd_status err; 180 struct ucom_attach_args uca; 181 182 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc)); 183 184 /* Move the device into the configured state. */ 185 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1); 186 if (err) { 187 printf("\n%s: failed to set configuration, err=%s\n", 188 devname, usbd_errstr(err)); 189 goto bad; 190 } 191 192 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface); 193 if (err) { 194 printf("\n%s: failed to get interface, err=%s\n", 195 devname, usbd_errstr(err)); 196 goto bad; 197 } 198 199 usbd_devinfo(dev, 0, devinfo); 200 USB_ATTACH_SETUP; 201 printf("%s: %s\n", devname, devinfo); 202 203 id = usbd_get_interface_descriptor(iface); 204 205 sc->sc_udev = dev; 206 sc->sc_iface = iface; 207 208 uca.ibufsize = UVISORIBUFSIZE; 209 uca.obufsize = UVISOROBUFSIZE; 210 uca.ibufsizepad = UVISORIBUFSIZE; 211 uca.opkthdrlen = 0; 212 uca.device = dev; 213 uca.iface = iface; 214 uca.methods = &uvisor_methods; 215 uca.arg = sc; 216 217 err = uvisor_init(sc, &coninfo); 218 if (err) { 219 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev), 220 usbd_errstr(err)); 221 goto bad; 222 } 223 224 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 225 USBDEV(sc->sc_dev)); 226 227 sc->sc_numcon = UGETW(coninfo.num_ports); 228 if (sc->sc_numcon > UVISOR_MAX_CONN) 229 sc->sc_numcon = UVISOR_MAX_CONN; 230 231 /* Attach a ucom for each connection. */ 232 for (i = 0; i < sc->sc_numcon; ++i) { 233 switch (coninfo.connections[i].port_function_id) { 234 case UVISOR_FUNCTION_GENERIC: 235 uca.info = "Generic"; 236 break; 237 case UVISOR_FUNCTION_DEBUGGER: 238 uca.info = "Debugger"; 239 break; 240 case UVISOR_FUNCTION_HOTSYNC: 241 uca.info = "HotSync"; 242 break; 243 case UVISOR_FUNCTION_REMOTE_FILE_SYS: 244 uca.info = "Remote File System"; 245 break; 246 default: 247 uca.info = "unknown"; 248 break; 249 } 250 port = coninfo.connections[i].port; 251 uca.portno = port; 252 uca.bulkin = port | UE_DIR_IN; 253 uca.bulkout = port | UE_DIR_OUT; 254 /* Verify that endpoints exist. */ 255 for (hasin = hasout = j = 0; j < id->bNumEndpoints; j++) { 256 ed = usbd_interface2endpoint_descriptor(iface, j); 257 if (ed == NULL) 258 break; 259 if (UE_GET_ADDR(ed->bEndpointAddress) == port && 260 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 261 if (UE_GET_DIR(ed->bEndpointAddress) 262 == UE_DIR_IN) 263 hasin++; 264 else 265 hasout++; 266 } 267 } 268 if (hasin == 1 && hasout == 1) 269 sc->sc_subdevs[i] = config_found_sm(self, &uca, 270 ucomprint, ucomsubmatch); 271 else 272 printf("%s: no proper endpoints for port %d (%d,%d)\n", 273 USBDEVNAME(sc->sc_dev), port, hasin, hasout); 274 } 275 276 USB_ATTACH_SUCCESS_RETURN; 277 278 bad: 279 DPRINTF(("uvisor_attach: ATTACH ERROR\n")); 280 sc->sc_dying = 1; 281 USB_ATTACH_ERROR_RETURN; 282 } 283 284 int 285 uvisor_activate(device_ptr_t self, enum devact act) 286 { 287 struct uvisor_softc *sc = (struct uvisor_softc *)self; 288 int rv = 0; 289 int i; 290 291 switch (act) { 292 case DVACT_ACTIVATE: 293 return (EOPNOTSUPP); 294 break; 295 296 case DVACT_DEACTIVATE: 297 for (i = 0; i < sc->sc_numcon; i++) 298 if (sc->sc_subdevs[i] != NULL) 299 rv = config_deactivate(sc->sc_subdevs[i]); 300 sc->sc_dying = 1; 301 break; 302 } 303 return (rv); 304 } 305 306 int 307 uvisor_detach(device_ptr_t self, int flags) 308 { 309 struct uvisor_softc *sc = (struct uvisor_softc *)self; 310 int rv = 0; 311 int i; 312 313 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags)); 314 sc->sc_dying = 1; 315 for (i = 0; i < sc->sc_numcon; i++) { 316 if (sc->sc_subdevs[i] != NULL) { 317 rv |= config_detach(sc->sc_subdevs[i], flags); 318 sc->sc_subdevs[i] = NULL; 319 } 320 } 321 322 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 323 USBDEV(sc->sc_dev)); 324 325 326 return (rv); 327 } 328 329 usbd_status 330 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci) 331 { 332 usbd_status err; 333 usb_device_request_t req; 334 int actlen; 335 uWord avail; 336 337 DPRINTF(("uvisor_init: getting connection info\n")); 338 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 339 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION; 340 USETW(req.wValue, 0); 341 USETW(req.wIndex, 0); 342 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 343 err = usbd_do_request_flags(sc->sc_udev, &req, ci, 344 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 345 if (err) 346 return (err); 347 348 DPRINTF(("uvisor_init: getting available bytes\n")); 349 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; 350 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE; 351 USETW(req.wValue, 0); 352 USETW(req.wIndex, 5); 353 USETW(req.wLength, sizeof avail); 354 err = usbd_do_request(sc->sc_udev, &req, &avail); 355 if (err) 356 return (err); 357 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail))); 358 359 DPRINTF(("uvisor_init: done\n")); 360 return (err); 361 } 362 363 void 364 uvisor_close(void *addr, int portno) 365 { 366 struct uvisor_softc *sc = addr; 367 usb_device_request_t req; 368 struct uvisor_connection_info coninfo; /* XXX ? */ 369 int actlen; 370 371 if (sc->sc_dying) 372 return; 373 374 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */ 375 req.bRequest = UVISOR_CLOSE_NOTIFICATION; 376 USETW(req.wValue, 0); 377 USETW(req.wIndex, 0); 378 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE); 379 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo, 380 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 381 } 382