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