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