Home | History | Annotate | Line # | Download | only in usb
uvisor.c revision 1.36
      1 /*	$NetBSD: uvisor.c,v 1.36 2007/10/25 19:32:15 plunky 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  * 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.36 2007/10/25 19:32:15 plunky 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 
     56 #include <dev/usb/usbdi.h>
     57 #include <dev/usb/usbdi_util.h>
     58 #include <dev/usb/usbdevs.h>
     59 
     60 #include <dev/usb/ucomvar.h>
     61 
     62 #ifdef UVISOR_DEBUG
     63 #define DPRINTF(x)	if (uvisordebug) printf x
     64 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
     65 int uvisordebug = 0;
     66 #else
     67 #define DPRINTF(x)
     68 #define DPRINTFN(n,x)
     69 #endif
     70 
     71 #define UVISOR_CONFIG_INDEX	0
     72 #define UVISOR_IFACE_INDEX	0
     73 
     74 /* From the Linux driver */
     75 /*
     76  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
     77  * are available to be transfered to the host for the specified endpoint.
     78  * Currently this is not used, and always returns 0x0001
     79  */
     80 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
     81 
     82 /*
     83  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
     84  * is now closing the pipe. An empty packet is sent in response.
     85  */
     86 #define UVISOR_CLOSE_NOTIFICATION		0x02
     87 
     88 /*
     89  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
     90  * get the endpoints used by the connection.
     91  */
     92 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
     93 
     94 
     95 /*
     96  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
     97  */
     98 #define UVISOR_MAX_CONN 8
     99 struct uvisor_connection_info {
    100 	uWord	num_ports;
    101 	struct {
    102 		uByte	port_function_id;
    103 		uByte	port;
    104 	} connections[UVISOR_MAX_CONN];
    105 };
    106 #define UVISOR_CONNECTION_INFO_SIZE 18
    107 
    108 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
    109 #define UVISOR_FUNCTION_GENERIC		0x00
    110 #define UVISOR_FUNCTION_DEBUGGER	0x01
    111 #define UVISOR_FUNCTION_HOTSYNC		0x02
    112 #define UVISOR_FUNCTION_CONSOLE		0x03
    113 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
    114 
    115 /*
    116  * Unknown PalmOS stuff.
    117  */
    118 #define UVISOR_GET_PALM_INFORMATION		0x04
    119 #define UVISOR_GET_PALM_INFORMATION_LEN		0x44
    120 
    121 struct uvisor_palm_connection_info {
    122 	uByte	num_ports;
    123 	uByte	endpoint_numbers_different;
    124 	uWord	reserved1;
    125 	struct {
    126 		uDWord	port_function_id;
    127 		uByte	port;
    128 		uByte	end_point_info;
    129 		uWord	reserved;
    130 	} connections[UVISOR_MAX_CONN];
    131 };
    132 
    133 
    134 
    135 #define UVISORIBUFSIZE 64
    136 #define UVISOROBUFSIZE 1024
    137 
    138 struct uvisor_softc {
    139 	USBBASEDEVICE		sc_dev;		/* base device */
    140 	usbd_device_handle	sc_udev;	/* device */
    141 	usbd_interface_handle	sc_iface;	/* interface */
    142 
    143 	device_ptr_t		sc_subdevs[UVISOR_MAX_CONN];
    144 	int			sc_numcon;
    145 
    146 	u_int16_t		sc_flags;
    147 
    148 	u_char			sc_dying;
    149 };
    150 
    151 Static usbd_status uvisor_init(struct uvisor_softc *,
    152 			       struct uvisor_connection_info *,
    153 			       struct uvisor_palm_connection_info *);
    154 
    155 Static void uvisor_close(void *, int);
    156 
    157 
    158 struct ucom_methods uvisor_methods = {
    159 	NULL,
    160 	NULL,
    161 	NULL,
    162 	NULL,
    163 	NULL,
    164 	uvisor_close,
    165 	NULL,
    166 	NULL,
    167 };
    168 
    169 struct uvisor_type {
    170 	struct usb_devno	uv_dev;
    171 	u_int16_t		uv_flags;
    172 #define PALM4	0x0001
    173 #define VISOR	0x0002
    174 
    175 };
    176 static const struct uvisor_type uvisor_devs[] = {
    177 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
    178 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
    179 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
    180 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
    181 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
    182 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
    183 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
    184 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
    185 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
    186 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
    187 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
    188 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
    189 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
    190 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
    191 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
    192 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
    193 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
    194 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
    195 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
    196 };
    197 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
    198 
    199 USB_DECLARE_DRIVER(uvisor);
    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 	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 int
    395 uvisor_detach(device_ptr_t self, int flags)
    396 {
    397 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
    398 	int rv = 0;
    399 	int i;
    400 
    401 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
    402 	sc->sc_dying = 1;
    403 	for (i = 0; i < sc->sc_numcon; i++) {
    404 		if (sc->sc_subdevs[i] != NULL) {
    405 			rv |= config_detach(sc->sc_subdevs[i], flags);
    406 			sc->sc_subdevs[i] = NULL;
    407 		}
    408 	}
    409 
    410 	if (sc->sc_udev)
    411 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    412 				   USBDEV(sc->sc_dev));
    413 
    414 
    415 	return (rv);
    416 }
    417 
    418 usbd_status
    419 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
    420     struct uvisor_palm_connection_info *cpi)
    421 {
    422 	usbd_status err;
    423 	usb_device_request_t req;
    424 	int actlen;
    425 	uWord avail;
    426 
    427 	if (sc->sc_flags & VISOR) {
    428 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
    429 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    430 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
    431 		USETW(req.wValue, 0);
    432 		USETW(req.wIndex, 0);
    433 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    434 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
    435 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    436 		if (err)
    437 			return (err);
    438 	}
    439 
    440 	if (sc->sc_flags & PALM4) {
    441 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
    442 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    443 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
    444 		USETW(req.wValue, 0);
    445 		USETW(req.wIndex, 0);
    446 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
    447 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
    448 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    449 		if (err)
    450 			return (err);
    451 	}
    452 
    453 	DPRINTF(("uvisor_init: getting available bytes\n"));
    454 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    455 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
    456 	USETW(req.wValue, 0);
    457 	USETW(req.wIndex, 5);
    458 	USETW(req.wLength, sizeof avail);
    459 	err = usbd_do_request(sc->sc_udev, &req, &avail);
    460 	if (err)
    461 		return (err);
    462 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
    463 
    464 	DPRINTF(("uvisor_init: done\n"));
    465 	return (err);
    466 }
    467 
    468 void
    469 uvisor_close(void *addr, int portno)
    470 {
    471 	struct uvisor_softc *sc = addr;
    472 	usb_device_request_t req;
    473 	struct uvisor_connection_info coninfo; /* XXX ? */
    474 	int actlen;
    475 
    476 	if (sc->sc_dying)
    477 		return;
    478 
    479 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
    480 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
    481 	USETW(req.wValue, 0);
    482 	USETW(req.wIndex, 0);
    483 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    484 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    485 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    486 }
    487