Home | History | Annotate | Line # | Download | only in usb
uvisor.c revision 1.43
      1 /*	$NetBSD: uvisor.c,v 1.43 2010/11/03 22:34:24 dyoung 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 (at) 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.43 2010/11/03 22:34:24 dyoung 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 	device_t		sc_dev;		/* base device */
    133 	usbd_device_handle	sc_udev;	/* device */
    134 	usbd_interface_handle	sc_iface;	/* interface */
    135 
    136 	device_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, cfdata_t, 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_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
    199     uvisor_attach, uvisor_detach, uvisor_activate, NULL, uvisor_childdet);
    200 
    201 int
    202 uvisor_match(device_t parent, cfdata_t match, void *aux)
    203 {
    204 	struct usb_attach_arg *uaa = aux;
    205 
    206 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
    207 		     uaa->vendor, uaa->product));
    208 
    209 	return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
    210 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    211 }
    212 
    213 void
    214 uvisor_attach(device_t parent, device_t self, void *aux)
    215 {
    216 	struct uvisor_softc *sc = device_private(self);
    217 	struct usb_attach_arg *uaa = aux;
    218 	usbd_device_handle dev = uaa->device;
    219 	usbd_interface_handle iface;
    220 	usb_interface_descriptor_t *id;
    221 	struct uvisor_connection_info coninfo;
    222 	struct uvisor_palm_connection_info palmconinfo;
    223 	usb_endpoint_descriptor_t *ed;
    224 	char *devinfop;
    225 	const char *devname = device_xname(self);
    226 	int i, j, hasin, hasout, port;
    227 	usbd_status err;
    228 	struct ucom_attach_args uca;
    229 
    230 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
    231 
    232 	sc->sc_dev = self;
    233 
    234 	aprint_naive("\n");
    235 	aprint_normal("\n");
    236 
    237 	devinfop = usbd_devinfo_alloc(dev, 0);
    238 	aprint_normal_dev(self, "%s\n", devinfop);
    239 	usbd_devinfo_free(devinfop);
    240 
    241 	/* Move the device into the configured state. */
    242 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
    243 	if (err) {
    244 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    245 		       devname, usbd_errstr(err));
    246 		goto bad;
    247 	}
    248 
    249 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
    250 	if (err) {
    251 		aprint_error("\n%s: failed to get interface, err=%s\n",
    252 		       devname, usbd_errstr(err));
    253 		goto bad;
    254 	}
    255 
    256 	sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
    257 
    258 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
    259 		aprint_error_dev(self,
    260 		    "init failed, device type is neither visor nor palm\n");
    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 		aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
    281 		goto bad;
    282 	}
    283 
    284 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    285 			   sc->sc_dev);
    286 
    287 	if (sc->sc_flags & VISOR) {
    288 		sc->sc_numcon = UGETW(coninfo.num_ports);
    289 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    290 			sc->sc_numcon = UVISOR_MAX_CONN;
    291 
    292 		/* Attach a ucom for each connection. */
    293 		for (i = 0; i < sc->sc_numcon; ++i) {
    294 			switch (coninfo.connections[i].port_function_id) {
    295 			case UVISOR_FUNCTION_GENERIC:
    296 				uca.info = "Generic";
    297 				break;
    298 			case UVISOR_FUNCTION_DEBUGGER:
    299 				uca.info = "Debugger";
    300 				break;
    301 			case UVISOR_FUNCTION_HOTSYNC:
    302 				uca.info = "HotSync";
    303 				break;
    304 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
    305 				uca.info = "Remote File System";
    306 				break;
    307 			default:
    308 				uca.info = "unknown";
    309 				break;
    310 			}
    311 			port = coninfo.connections[i].port;
    312 			uca.portno = port;
    313 			uca.bulkin = port | UE_DIR_IN;
    314 			uca.bulkout = port | UE_DIR_OUT;
    315 			/* Verify that endpoints exist. */
    316 			hasin = 0;
    317 			hasout = 0;
    318 			for (j = 0; j < id->bNumEndpoints; j++) {
    319 				ed = usbd_interface2endpoint_descriptor(iface, j);
    320 				if (ed == NULL)
    321 					break;
    322 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
    323 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    324 					if (UE_GET_DIR(ed->bEndpointAddress)
    325 					    == UE_DIR_IN)
    326 						hasin++;
    327 					else
    328 						hasout++;
    329 				}
    330 			}
    331 			if (hasin == 1 && hasout == 1)
    332 				sc->sc_subdevs[i] = config_found_sm_loc(self,
    333 					"ucombus", NULL, &uca,
    334 					ucomprint, ucomsubmatch);
    335 			else
    336 				aprint_error_dev(self,
    337 				    "no proper endpoints for port %d (%d,%d)\n",
    338 				    port, hasin, hasout);
    339 		}
    340 
    341 	} else {
    342 		sc->sc_numcon = palmconinfo.num_ports;
    343 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    344 			sc->sc_numcon = UVISOR_MAX_CONN;
    345 
    346 		/* Attach a ucom for each connection. */
    347 		for (i = 0; i < sc->sc_numcon; ++i) {
    348 			/*
    349 			 * XXX this should copy out 4-char string from the
    350 			 * XXX port_function_id, but where would the string go?
    351 			 * XXX uca.info is a const char *, not an array.
    352 			 */
    353 			uca.info = "sync";
    354 			uca.portno = i;
    355 			if (palmconinfo.endpoint_numbers_different) {
    356 				port = palmconinfo.connections[i].end_point_info;
    357 				uca.bulkin = (port >> 4) | UE_DIR_IN;
    358 				uca.bulkout = (port & 0xf) | UE_DIR_OUT;
    359 			} else {
    360 				port = palmconinfo.connections[i].port;
    361 				uca.bulkin = port | UE_DIR_IN;
    362 				uca.bulkout = port | UE_DIR_OUT;
    363 			}
    364 			sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus",
    365 				NULL, &uca, ucomprint, ucomsubmatch);
    366 
    367 
    368 		}
    369 	}
    370 
    371 	return;
    372 
    373 bad:
    374 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
    375 	sc->sc_dying = 1;
    376 	return;
    377 }
    378 
    379 int
    380 uvisor_activate(device_t self, enum devact act)
    381 {
    382 	struct uvisor_softc *sc = device_private(self);
    383 
    384 	switch (act) {
    385 	case DVACT_DEACTIVATE:
    386 		sc->sc_dying = 1;
    387 		return 0;
    388 	default:
    389 		return EOPNOTSUPP;
    390 	}
    391 }
    392 
    393 void
    394 uvisor_childdet(device_t self, device_t child)
    395 {
    396 	int i;
    397 	struct uvisor_softc *sc = device_private(self);
    398 
    399 	for (i = 0; i < sc->sc_numcon; i++) {
    400 		if (sc->sc_subdevs[i] == child)
    401 			break;
    402 	}
    403 	KASSERT(i < sc->sc_numcon);
    404 	sc->sc_subdevs[i] = NULL;
    405 }
    406 
    407 int
    408 uvisor_detach(device_t self, int flags)
    409 {
    410 	struct uvisor_softc *sc = device_private(self);
    411 	int rv = 0;
    412 	int i;
    413 
    414 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
    415 	sc->sc_dying = 1;
    416 	for (i = 0; i < sc->sc_numcon; i++) {
    417 		if (sc->sc_subdevs[i] != NULL)
    418 			rv |= config_detach(sc->sc_subdevs[i], flags);
    419 	}
    420 
    421 	if (sc->sc_udev)
    422 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    423 				   sc->sc_dev);
    424 
    425 
    426 	return (rv);
    427 }
    428 
    429 usbd_status
    430 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
    431     struct uvisor_palm_connection_info *cpi)
    432 {
    433 	usbd_status err;
    434 	usb_device_request_t req;
    435 	int actlen;
    436 	uWord avail;
    437 
    438 	if (sc->sc_flags & VISOR) {
    439 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
    440 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    441 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
    442 		USETW(req.wValue, 0);
    443 		USETW(req.wIndex, 0);
    444 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    445 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
    446 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    447 		if (err)
    448 			return (err);
    449 	}
    450 
    451 	if (sc->sc_flags & PALM4) {
    452 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
    453 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    454 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
    455 		USETW(req.wValue, 0);
    456 		USETW(req.wIndex, 0);
    457 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
    458 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
    459 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    460 		if (err)
    461 			return (err);
    462 	}
    463 
    464 	DPRINTF(("uvisor_init: getting available bytes\n"));
    465 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    466 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
    467 	USETW(req.wValue, 0);
    468 	USETW(req.wIndex, 5);
    469 	USETW(req.wLength, sizeof avail);
    470 	err = usbd_do_request(sc->sc_udev, &req, &avail);
    471 	if (err)
    472 		return (err);
    473 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
    474 
    475 	DPRINTF(("uvisor_init: done\n"));
    476 	return (err);
    477 }
    478 
    479 void
    480 uvisor_close(void *addr, int portno)
    481 {
    482 	struct uvisor_softc *sc = addr;
    483 	usb_device_request_t req;
    484 	struct uvisor_connection_info coninfo; /* XXX ? */
    485 	int actlen;
    486 
    487 	if (sc->sc_dying)
    488 		return;
    489 
    490 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
    491 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
    492 	USETW(req.wValue, 0);
    493 	USETW(req.wIndex, 0);
    494 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    495 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    496 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    497 }
    498