Home | History | Annotate | Line # | Download | only in usb
uvisor.c revision 1.1
      1 /*	$NetBSD: uvisor.c,v 1.1 2000/03/30 16:56:19 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 (augustss (at) carlstedt.se) 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 driver
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/conf.h>
     49 #include <sys/tty.h>
     50 
     51 #include <dev/usb/usb.h>
     52 #include <dev/usb/usbhid.h>
     53 
     54 #include <dev/usb/usbdi.h>
     55 #include <dev/usb/usbdi_util.h>
     56 #include <dev/usb/usbdevs.h>
     57 
     58 #include <dev/usb/ucomvar.h>
     59 
     60 #ifdef UVISOR_DEBUG
     61 #define DPRINTF(x)	if (uvisordebug) printf x
     62 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
     63 int uvisordebug = 10;
     64 #else
     65 #define DPRINTF(x)
     66 #define DPRINTFN(n,x)
     67 #endif
     68 
     69 #define UVISOR_CONFIG_INDEX	0
     70 #define UVISOR_IFACE_INDEX	0
     71 
     72 /* From the Linux driver */
     73 /*
     74  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
     75  * are available to be transfered to the host for the specified endpoint.
     76  * Currently this is not used, and always returns 0x0001
     77  */
     78 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
     79 
     80 /*
     81  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
     82  * is now closing the pipe. An empty packet is sent in response.
     83  */
     84 #define UVISOR_CLOSE_NOTIFICATION		0x02
     85 
     86 /*
     87  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
     88  * get the endpoints used by the connection.
     89  */
     90 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
     91 
     92 
     93 /*
     94  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
     95  */
     96 struct uvisor_connection_info {
     97 	uWord	num_ports;
     98 	struct {
     99 		uByte	port_function_id;
    100 		uByte	port;
    101 	} connections[8];
    102 };
    103 #define UVISOR_CONNECTION_INFO_SIZE 18
    104 
    105 
    106 /* struct uvisor_connection_info.connection[x].port defines: */
    107 #define UVISOR_ENDPOINT_1		0x01
    108 #define UVISOR_ENDPOINT_2		0x02
    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 struct uvisor_softc {
    119 	USBBASEDEVICE		sc_dev;		/* base device */
    120 	usbd_device_handle	sc_udev;	/* device */
    121 	usbd_interface_handle	sc_iface;	/* interface */
    122 
    123 	device_ptr_t		sc_subdev;
    124 
    125 	u_char			sc_dying;
    126 };
    127 
    128 Static usbd_status uvisor_init __P((struct uvisor_softc *));
    129 
    130 Static void uvisor_close __P((void *, int));
    131 
    132 
    133 struct ucom_methods uvisor_methods = {
    134 	NULL,
    135 	NULL,
    136 	NULL,
    137 	NULL,
    138 	NULL,
    139 	uvisor_close,
    140 };
    141 
    142 USB_DECLARE_DRIVER(uvisor);
    143 
    144 USB_MATCH(uvisor)
    145 {
    146 	USB_MATCH_START(uvisor, uaa);
    147 
    148 	if (uaa->iface != NULL)
    149 		return (UMATCH_NONE);
    150 
    151 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
    152 		     uaa->vendor, uaa->product));
    153 
    154 	if (uaa->vendor == USB_VENDOR_HANDSPRING &&
    155 	    uaa->product & USB_PRODUCT_HANDSPRING_VISOR)
    156 		return (UMATCH_VENDOR_PRODUCT);
    157 
    158 	return (UMATCH_NONE);
    159 }
    160 
    161 USB_ATTACH(uvisor)
    162 {
    163 	USB_ATTACH_START(uvisor, sc, uaa);
    164 	usbd_device_handle dev = uaa->device;
    165 	usbd_interface_handle iface;
    166 	usb_interface_descriptor_t *id;
    167 	usb_endpoint_descriptor_t *ed;
    168 	char devinfo[1024];
    169 	char *devname = USBDEVNAME(sc->sc_dev);
    170 	int i;
    171 	usbd_status err;
    172 	struct ucom_attach_args uca;
    173 
    174 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
    175 
    176 	/* Move the device into the configured state. */
    177 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
    178 	if (err) {
    179 		printf("\n%s: failed to set configuration, err=%s\n",
    180 		       devname, usbd_errstr(err));
    181 		goto bad;
    182 	}
    183 
    184 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
    185 	if (err) {
    186 		printf("\n%s: failed to get interface, err=%s\n",
    187 		       devname, usbd_errstr(err));
    188 		goto bad;
    189 	}
    190 
    191 	usbd_devinfo(dev, 0, devinfo);
    192 	USB_ATTACH_SETUP;
    193 	printf("%s: %s\n", devname, devinfo);
    194 
    195 	id = usbd_get_interface_descriptor(iface);
    196 	sc->sc_iface = iface;
    197 	sc->sc_udev = dev;
    198 
    199 	uca.methods = &uvisor_methods;
    200 	uca.arg = sc;
    201 	uca.iface = iface;
    202 
    203 	uca.bulkin = uca.bulkout = -1;
    204 	for (i = 0; i < id->bNumEndpoints; i++) {
    205 		int addr, dir, attr;
    206 		ed = usbd_interface2endpoint_descriptor(iface, i);
    207 		if (ed == NULL) {
    208 			printf("%s: could not read endpoint descriptor"
    209 			       ": %s\n", devname, usbd_errstr(err));
    210 			goto bad;
    211 		}
    212 
    213 		addr = ed->bEndpointAddress;
    214 		dir = UE_GET_DIR(ed->bEndpointAddress);
    215 		attr = ed->bmAttributes & UE_XFERTYPE;
    216 		if (dir == UE_DIR_IN && attr == UE_BULK)
    217 			uca.bulkin = addr;
    218 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
    219 			uca.bulkout = addr;
    220 		else {
    221 			printf("%s: unexpected endpoint\n", devname);
    222 			goto bad;
    223 		}
    224 	}
    225 	if (uca.bulkin == -1) {
    226 		printf("%s: Could not find data bulk in\n",
    227 		       USBDEVNAME(sc->sc_dev));
    228 		goto bad;
    229 	}
    230 	if (uca.bulkout == -1) {
    231 		printf("%s: Could not find data bulk out\n",
    232 		       USBDEVNAME(sc->sc_dev));
    233 		goto bad;
    234 	}
    235 
    236 	err = uvisor_init(sc);
    237 	if (err) {
    238 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
    239 		       usbd_errstr(err));
    240 		goto bad;
    241 	}
    242 
    243 	uca.portno = UCOM_UNK_PORTNO;
    244 	DPRINTF(("uvisor: in=%d out=%d\n", uca.bulkin, uca.bulkout));
    245 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    246 
    247 	USB_ATTACH_SUCCESS_RETURN;
    248 
    249 bad:
    250 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
    251 	sc->sc_dying = 1;
    252 	USB_ATTACH_ERROR_RETURN;
    253 }
    254 
    255 int
    256 uvisor_activate(self, act)
    257 	device_ptr_t self;
    258 	enum devact act;
    259 {
    260 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
    261 	int rv = 0;
    262 
    263 	switch (act) {
    264 	case DVACT_ACTIVATE:
    265 		return (EOPNOTSUPP);
    266 		break;
    267 
    268 	case DVACT_DEACTIVATE:
    269 		if (sc->sc_subdev != NULL)
    270 			rv = config_deactivate(sc->sc_subdev);
    271 		sc->sc_dying = 1;
    272 		break;
    273 	}
    274 	return (rv);
    275 }
    276 
    277 int
    278 uvisor_detach(self, flags)
    279 	device_ptr_t self;
    280 	int flags;
    281 {
    282 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
    283 	int rv = 0;
    284 
    285 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
    286 	sc->sc_dying = 1;
    287 	if (sc->sc_subdev != NULL) {
    288 		rv = config_detach(sc->sc_subdev, flags);
    289 		sc->sc_subdev = NULL;
    290 	}
    291 	return (0);
    292 }
    293 
    294 usbd_status
    295 uvisor_init(sc)
    296 	struct uvisor_softc *sc;
    297 {
    298 	usbd_status err;
    299 	usb_device_request_t req;
    300 	struct uvisor_connection_info coninfo;
    301 	int actlen;
    302 	uWord avail;
    303 
    304 	DPRINTF(("uvisor_init: getting connection info\n"));
    305 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    306 	req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
    307 	USETW(req.wValue, 0);
    308 	USETW(req.wIndex, 0);
    309 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    310 	err = usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    311 				    USBD_SHORT_XFER_OK, &actlen);
    312 	if (err)
    313 		return (err);
    314 
    315 #ifdef UVISOR_DEBUG
    316 	{
    317 		int i, np;
    318 		char *string;
    319 
    320 		np = UGETW(coninfo.num_ports);
    321 		printf("%s: Number of ports: %d", USBDEVNAME(sc->sc_dev), np);
    322 		for (i = 0; i < np; ++i) {
    323 			switch (coninfo.connections[i].port_function_id) {
    324 			case UVISOR_FUNCTION_GENERIC:
    325 				string = "Generic";
    326 				break;
    327 			case UVISOR_FUNCTION_DEBUGGER:
    328 				string = "Debugger";
    329 				break;
    330 			case UVISOR_FUNCTION_HOTSYNC:
    331 				string = "HotSync";
    332 				break;
    333 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
    334 				string = "Remote File System";
    335 				break;
    336 			default:
    337 				string = "unknown";
    338 				break;
    339 			}
    340 			printf("%s: port %d, is for %s", USBDEVNAME(sc->sc_dev),
    341 			    coninfo.connections[i].port, string);
    342 		}
    343 	}
    344 #endif
    345 
    346 	DPRINTF(("uvisor_init: getting available bytes\n"));
    347 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    348 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
    349 	USETW(req.wValue, 0);
    350 	USETW(req.wIndex, 5);
    351 	USETW(req.wLength, sizeof avail);
    352 	err = usbd_do_request(sc->sc_udev, &req, &avail);
    353 	if (err)
    354 		return (err);
    355 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
    356 
    357 	DPRINTF(("uvisor_init: done\n"));
    358 	return (err);
    359 }
    360 
    361 void
    362 uvisor_close(addr, portno)
    363 	void *addr;
    364 	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 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
    372 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
    373 	USETW(req.wValue, 0);
    374 	USETW(req.wIndex, 0);
    375 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    376 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    377 				    USBD_SHORT_XFER_OK, &actlen);
    378 }
    379