Home | History | Annotate | Line # | Download | only in usb
uvisor.c revision 1.56
      1 /*	$NetBSD: uvisor.c,v 1.56 2021/04/24 23:36:59 thorpej 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.56 2021/04/24 23:36:59 thorpej Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_usb.h"
     42 #endif
     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 
     53 #include <dev/usb/usbdi.h>
     54 #include <dev/usb/usbdi_util.h>
     55 #include <dev/usb/usbdevs.h>
     56 
     57 #include <dev/usb/ucomvar.h>
     58 
     59 #ifdef UVISOR_DEBUG
     60 #define DPRINTF(x)	if (uvisordebug) printf x
     61 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
     62 int uvisordebug = 0;
     63 #else
     64 #define DPRINTF(x)
     65 #define DPRINTFN(n,x)
     66 #endif
     67 
     68 #define UVISOR_CONFIG_INDEX	0
     69 #define UVISOR_IFACE_INDEX	0
     70 
     71 /* From the Linux driver */
     72 /*
     73  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
     74  * are available to be transferred to the host for the specified endpoint.
     75  * Currently this is not used, and always returns 0x0001
     76  */
     77 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
     78 
     79 /*
     80  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
     81  * is now closing the pipe. An empty packet is sent in response.
     82  */
     83 #define UVISOR_CLOSE_NOTIFICATION		0x02
     84 
     85 /*
     86  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
     87  * get the endpoints used by the connection.
     88  */
     89 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
     90 
     91 
     92 /*
     93  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
     94  */
     95 #define UVISOR_MAX_CONN 8
     96 struct uvisor_connection_info {
     97 	uWord	num_ports;
     98 	struct {
     99 		uByte	port_function_id;
    100 		uByte	port;
    101 	} connections[UVISOR_MAX_CONN];
    102 };
    103 #define UVISOR_CONNECTION_INFO_SIZE 18
    104 
    105 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
    106 #define UVISOR_FUNCTION_GENERIC		0x00
    107 #define UVISOR_FUNCTION_DEBUGGER	0x01
    108 #define UVISOR_FUNCTION_HOTSYNC		0x02
    109 #define UVISOR_FUNCTION_CONSOLE		0x03
    110 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
    111 
    112 /*
    113  * Unknown PalmOS stuff.
    114  */
    115 #define UVISOR_GET_PALM_INFORMATION		0x04
    116 #define UVISOR_GET_PALM_INFORMATION_LEN		0x44
    117 
    118 struct uvisor_palm_connection_info {
    119 	uByte	num_ports;
    120 	uByte	endpoint_numbers_different;
    121 	uWord	reserved1;
    122 	struct {
    123 		uDWord	port_function_id;
    124 		uByte	port;
    125 		uByte	end_point_info;
    126 		uWord	reserved;
    127 	} connections[UVISOR_MAX_CONN];
    128 };
    129 
    130 
    131 
    132 #define UVISORIBUFSIZE 64
    133 #define UVISOROBUFSIZE 1024
    134 
    135 struct uvisor_softc {
    136 	device_t		sc_dev;		/* base device */
    137 	struct usbd_device *	sc_udev;	/* device */
    138 	struct usbd_interface *	sc_iface;	/* interface */
    139 
    140 	device_t		sc_subdevs[UVISOR_MAX_CONN];
    141 	int			sc_numcon;
    142 
    143 	uint16_t		sc_flags;
    144 
    145 	bool			sc_dying;
    146 };
    147 
    148 static usbd_status uvisor_init(struct uvisor_softc *,
    149 			       struct uvisor_connection_info *,
    150 			       struct uvisor_palm_connection_info *);
    151 
    152 static int uvisor_open(void *, int);
    153 static void uvisor_close(void *, int);
    154 
    155 static const struct ucom_methods uvisor_methods = {
    156 	.ucom_open = uvisor_open,
    157 	.ucom_close = uvisor_close,
    158 };
    159 
    160 struct uvisor_type {
    161 	struct usb_devno	uv_dev;
    162 	uint16_t		uv_flags;
    163 #define PALM4	0x0001
    164 #define VISOR	0x0002
    165 
    166 };
    167 static const struct uvisor_type uvisor_devs[] = {
    168 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
    169 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
    170 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
    171 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
    172 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
    173 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
    174 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
    175 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
    176 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
    177 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
    178 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
    179 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
    180 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
    181 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
    182 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
    183 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
    184 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
    185 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
    186 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
    187 };
    188 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
    189 
    190 static int	uvisor_match(device_t, cfdata_t, void *);
    191 static void	uvisor_attach(device_t, device_t, void *);
    192 static void	uvisor_childdet(device_t, device_t);
    193 static int	uvisor_detach(device_t, int);
    194 
    195 CFATTACH_DECL2_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
    196     uvisor_attach, uvisor_detach, NULL, NULL, uvisor_childdet);
    197 
    198 static int
    199 uvisor_match(device_t parent, cfdata_t match, void *aux)
    200 {
    201 	struct usb_attach_arg *uaa = aux;
    202 
    203 	DPRINTFN(20,("uvisor: vendor=%#x, product=%#x\n",
    204 		     uaa->uaa_vendor, uaa->uaa_product));
    205 
    206 	return uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    207 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    208 }
    209 
    210 static void
    211 uvisor_attach(device_t parent, device_t self, void *aux)
    212 {
    213 	struct uvisor_softc *sc = device_private(self);
    214 	struct usb_attach_arg *uaa = aux;
    215 	struct usbd_device *dev = uaa->uaa_device;
    216 	struct usbd_interface *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 ucaa;
    226 
    227 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
    228 
    229 	sc->sc_dev = self;
    230 	sc->sc_dying = false;
    231 
    232 	aprint_naive("\n");
    233 	aprint_normal("\n");
    234 
    235 	devinfop = usbd_devinfo_alloc(dev, 0);
    236 	aprint_normal_dev(self, "%s\n", devinfop);
    237 	usbd_devinfo_free(devinfop);
    238 
    239 	/* Move the device into the configured state. */
    240 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
    241 	if (err) {
    242 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    243 		       devname, usbd_errstr(err));
    244 		goto bad;
    245 	}
    246 
    247 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
    248 	if (err) {
    249 		aprint_error("\n%s: failed to get interface, err=%s\n",
    250 		       devname, usbd_errstr(err));
    251 		goto bad;
    252 	}
    253 
    254 	sc->sc_flags = uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags;
    255 
    256 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
    257 		aprint_error_dev(self,
    258 		    "init failed, device type is neither visor nor palm\n");
    259 		goto bad;
    260 	}
    261 
    262 	id = usbd_get_interface_descriptor(iface);
    263 
    264 	sc->sc_udev = dev;
    265 	sc->sc_iface = iface;
    266 
    267 	ucaa.ucaa_ibufsize = UVISORIBUFSIZE;
    268 	ucaa.ucaa_obufsize = UVISOROBUFSIZE;
    269 	ucaa.ucaa_ibufsizepad = UVISORIBUFSIZE;
    270 	ucaa.ucaa_opkthdrlen = 0;
    271 	ucaa.ucaa_device = dev;
    272 	ucaa.ucaa_iface = iface;
    273 	ucaa.ucaa_methods = &uvisor_methods;
    274 	ucaa.ucaa_arg = sc;
    275 
    276 	err = uvisor_init(sc, &coninfo, &palmconinfo);
    277 	if (err) {
    278 		aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
    279 		goto bad;
    280 	}
    281 
    282 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    283 
    284 	if (sc->sc_flags & VISOR) {
    285 		sc->sc_numcon = UGETW(coninfo.num_ports);
    286 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    287 			sc->sc_numcon = UVISOR_MAX_CONN;
    288 
    289 		/* Attach a ucom for each connection. */
    290 		for (i = 0; i < sc->sc_numcon; ++i) {
    291 			switch (coninfo.connections[i].port_function_id) {
    292 			case UVISOR_FUNCTION_GENERIC:
    293 				ucaa.ucaa_info = "Generic";
    294 				break;
    295 			case UVISOR_FUNCTION_DEBUGGER:
    296 				ucaa.ucaa_info = "Debugger";
    297 				break;
    298 			case UVISOR_FUNCTION_HOTSYNC:
    299 				ucaa.ucaa_info = "HotSync";
    300 				break;
    301 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
    302 				ucaa.ucaa_info = "Remote File System";
    303 				break;
    304 			default:
    305 				ucaa.ucaa_info = "unknown";
    306 				break;
    307 			}
    308 			port = coninfo.connections[i].port;
    309 			ucaa.ucaa_portno = port;
    310 			ucaa.ucaa_bulkin = port | UE_DIR_IN;
    311 			ucaa.ucaa_bulkout = port | UE_DIR_OUT;
    312 			/* Verify that endpoints exist. */
    313 			hasin = 0;
    314 			hasout = 0;
    315 			for (j = 0; j < id->bNumEndpoints; j++) {
    316 				ed = usbd_interface2endpoint_descriptor(iface, j);
    317 				if (ed == NULL)
    318 					break;
    319 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
    320 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    321 					if (UE_GET_DIR(ed->bEndpointAddress)
    322 					    == UE_DIR_IN)
    323 						hasin++;
    324 					else
    325 						hasout++;
    326 				}
    327 			}
    328 			if (hasin == 1 && hasout == 1)
    329 				sc->sc_subdevs[i] =
    330 				    config_found(self, &ucaa, ucomprint,
    331 						 CFARG_SUBMATCH, ucomsubmatch,
    332 						 CFARG_EOL);
    333 			else
    334 				aprint_error_dev(self,
    335 				    "no proper endpoints for port %d (%d,%d)\n",
    336 				    port, hasin, hasout);
    337 		}
    338 
    339 	} else {
    340 		sc->sc_numcon = palmconinfo.num_ports;
    341 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    342 			sc->sc_numcon = UVISOR_MAX_CONN;
    343 
    344 		/* Attach a ucom for each connection. */
    345 		for (i = 0; i < sc->sc_numcon; ++i) {
    346 			/*
    347 			 * XXX this should copy out 4-char string from the
    348 			 * XXX port_function_id, but where would the string go?
    349 			 * XXX ucaa.ucaa_info is a const char *, not an array.
    350 			 */
    351 			ucaa.ucaa_info = "sync";
    352 			ucaa.ucaa_portno = i;
    353 			if (palmconinfo.endpoint_numbers_different) {
    354 				port = palmconinfo.connections[i].end_point_info;
    355 				ucaa.ucaa_bulkin = (port >> 4) | UE_DIR_IN;
    356 				ucaa.ucaa_bulkout = (port & 0xf) | UE_DIR_OUT;
    357 			} else {
    358 				port = palmconinfo.connections[i].port;
    359 				ucaa.ucaa_bulkin = port | UE_DIR_IN;
    360 				ucaa.ucaa_bulkout = port | UE_DIR_OUT;
    361 			}
    362 			sc->sc_subdevs[i] =
    363 			    config_found(self, &ucaa, ucomprint,
    364 					 CFARG_SUBMATCH, ucomsubmatch,
    365 					 CFARG_EOL);
    366 		}
    367 	}
    368 
    369 	return;
    370 
    371 bad:
    372 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
    373 	sc->sc_dying = true;
    374 	return;
    375 }
    376 
    377 static void
    378 uvisor_childdet(device_t self, device_t child)
    379 {
    380 	int i;
    381 	struct uvisor_softc *sc = device_private(self);
    382 
    383 	for (i = 0; i < sc->sc_numcon; i++) {
    384 		if (sc->sc_subdevs[i] == child)
    385 			break;
    386 	}
    387 	KASSERT(i < sc->sc_numcon);
    388 	sc->sc_subdevs[i] = NULL;
    389 }
    390 
    391 static int
    392 uvisor_detach(device_t self, int flags)
    393 {
    394 	struct uvisor_softc *sc = device_private(self);
    395 	int rv = 0;
    396 	int i;
    397 
    398 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
    399 
    400 	sc->sc_dying = true;
    401 
    402 	for (i = 0; i < sc->sc_numcon; i++) {
    403 		if (sc->sc_subdevs[i] != NULL) {
    404 			rv |= config_detach(sc->sc_subdevs[i], flags);
    405 			sc->sc_subdevs[i] = NULL;
    406 		}
    407 	}
    408 	if (sc->sc_udev != NULL)
    409 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    410 		    sc->sc_dev);
    411 
    412 	return rv;
    413 }
    414 
    415 static usbd_status
    416 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
    417     struct uvisor_palm_connection_info *cpi)
    418 {
    419 	usbd_status err;
    420 	usb_device_request_t req;
    421 	int actlen;
    422 	uWord avail;
    423 
    424 	if (sc->sc_flags & VISOR) {
    425 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
    426 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    427 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
    428 		USETW(req.wValue, 0);
    429 		USETW(req.wIndex, 0);
    430 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    431 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
    432 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    433 		if (err)
    434 			return err;
    435 	}
    436 
    437 	if (sc->sc_flags & PALM4) {
    438 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
    439 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    440 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
    441 		USETW(req.wValue, 0);
    442 		USETW(req.wIndex, 0);
    443 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
    444 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
    445 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    446 		if (err)
    447 			return err;
    448 	}
    449 
    450 	DPRINTF(("uvisor_init: getting available bytes\n"));
    451 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    452 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
    453 	USETW(req.wValue, 0);
    454 	USETW(req.wIndex, 5);
    455 	USETW(req.wLength, sizeof(avail));
    456 	err = usbd_do_request(sc->sc_udev, &req, &avail);
    457 	if (err)
    458 		return err;
    459 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
    460 
    461 	DPRINTF(("uvisor_init: done\n"));
    462 	return err;
    463 }
    464 
    465 static int
    466 uvisor_open(void *arg, int portno)
    467 {
    468 	struct uvisor_softc *sc = arg;
    469 
    470 	if (sc->sc_dying)
    471 		return EIO;
    472 
    473 	return 0;
    474 }
    475 
    476 void
    477 uvisor_close(void *addr, int portno)
    478 {
    479 	struct uvisor_softc *sc = addr;
    480 	usb_device_request_t req;
    481 	struct uvisor_connection_info coninfo; /* XXX ? */
    482 	int actlen;
    483 
    484 	if (sc->sc_dying)
    485 		return;
    486 
    487 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
    488 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
    489 	USETW(req.wValue, 0);
    490 	USETW(req.wIndex, 0);
    491 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    492 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    493 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    494 }
    495