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