Home | History | Annotate | Line # | Download | only in usb
uvisor.c revision 1.50
      1 /*	$NetBSD: uvisor.c,v 1.50 2019/05/05 03:17:54 mrg 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.50 2019/05/05 03:17:54 mrg 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 transfered 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 	u_char			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 void uvisor_close(void *, int);
    153 
    154 
    155 struct ucom_methods uvisor_methods = {
    156 	.ucom_close = uvisor_close,
    157 };
    158 
    159 struct uvisor_type {
    160 	struct usb_devno	uv_dev;
    161 	uint16_t		uv_flags;
    162 #define PALM4	0x0001
    163 #define VISOR	0x0002
    164 
    165 };
    166 static const struct uvisor_type uvisor_devs[] = {
    167 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
    168 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
    169 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, PALM4 },
    170 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
    171 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
    172 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
    173 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
    174 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
    175 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
    176 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
    177 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
    178 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE31 }, PALM4 },
    179 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
    180 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
    181 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
    182 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
    183 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
    184 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_35 }, 0 },
    185 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
    186 };
    187 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
    188 
    189 int uvisor_match(device_t, cfdata_t, void *);
    190 void uvisor_attach(device_t, device_t, void *);
    191 void uvisor_childdet(device_t, device_t);
    192 int uvisor_detach(device_t, int);
    193 int uvisor_activate(device_t, enum devact);
    194 
    195 CFATTACH_DECL2_NEW(uvisor, sizeof(struct uvisor_softc), uvisor_match,
    196     uvisor_attach, uvisor_detach, uvisor_activate, NULL, uvisor_childdet);
    197 
    198 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=0x%x, product=0x%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 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 
    231 	aprint_naive("\n");
    232 	aprint_normal("\n");
    233 
    234 	devinfop = usbd_devinfo_alloc(dev, 0);
    235 	aprint_normal_dev(self, "%s\n", devinfop);
    236 	usbd_devinfo_free(devinfop);
    237 
    238 	/* Move the device into the configured state. */
    239 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
    240 	if (err) {
    241 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    242 		       devname, usbd_errstr(err));
    243 		goto bad;
    244 	}
    245 
    246 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
    247 	if (err) {
    248 		aprint_error("\n%s: failed to get interface, err=%s\n",
    249 		       devname, usbd_errstr(err));
    250 		goto bad;
    251 	}
    252 
    253 	sc->sc_flags = uvisor_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags;
    254 
    255 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
    256 		aprint_error_dev(self,
    257 		    "init failed, device type is neither visor nor palm\n");
    258 		goto bad;
    259 	}
    260 
    261 	id = usbd_get_interface_descriptor(iface);
    262 
    263 	sc->sc_udev = dev;
    264 	sc->sc_iface = iface;
    265 
    266 	ucaa.ucaa_ibufsize = UVISORIBUFSIZE;
    267 	ucaa.ucaa_obufsize = UVISOROBUFSIZE;
    268 	ucaa.ucaa_ibufsizepad = UVISORIBUFSIZE;
    269 	ucaa.ucaa_opkthdrlen = 0;
    270 	ucaa.ucaa_device = dev;
    271 	ucaa.ucaa_iface = iface;
    272 	ucaa.ucaa_methods = &uvisor_methods;
    273 	ucaa.ucaa_arg = sc;
    274 
    275 	err = uvisor_init(sc, &coninfo, &palmconinfo);
    276 	if (err) {
    277 		aprint_error_dev(self, "init failed, %s\n", usbd_errstr(err));
    278 		goto bad;
    279 	}
    280 
    281 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    282 
    283 	if (sc->sc_flags & VISOR) {
    284 		sc->sc_numcon = UGETW(coninfo.num_ports);
    285 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    286 			sc->sc_numcon = UVISOR_MAX_CONN;
    287 
    288 		/* Attach a ucom for each connection. */
    289 		for (i = 0; i < sc->sc_numcon; ++i) {
    290 			switch (coninfo.connections[i].port_function_id) {
    291 			case UVISOR_FUNCTION_GENERIC:
    292 				ucaa.ucaa_info = "Generic";
    293 				break;
    294 			case UVISOR_FUNCTION_DEBUGGER:
    295 				ucaa.ucaa_info = "Debugger";
    296 				break;
    297 			case UVISOR_FUNCTION_HOTSYNC:
    298 				ucaa.ucaa_info = "HotSync";
    299 				break;
    300 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
    301 				ucaa.ucaa_info = "Remote File System";
    302 				break;
    303 			default:
    304 				ucaa.ucaa_info = "unknown";
    305 				break;
    306 			}
    307 			port = coninfo.connections[i].port;
    308 			ucaa.ucaa_portno = port;
    309 			ucaa.ucaa_bulkin = port | UE_DIR_IN;
    310 			ucaa.ucaa_bulkout = port | UE_DIR_OUT;
    311 			/* Verify that endpoints exist. */
    312 			hasin = 0;
    313 			hasout = 0;
    314 			for (j = 0; j < id->bNumEndpoints; j++) {
    315 				ed = usbd_interface2endpoint_descriptor(iface, j);
    316 				if (ed == NULL)
    317 					break;
    318 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
    319 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    320 					if (UE_GET_DIR(ed->bEndpointAddress)
    321 					    == UE_DIR_IN)
    322 						hasin++;
    323 					else
    324 						hasout++;
    325 				}
    326 			}
    327 			if (hasin == 1 && hasout == 1)
    328 				sc->sc_subdevs[i] = config_found_sm_loc(self,
    329 					"ucombus", NULL, &ucaa,
    330 					ucomprint, ucomsubmatch);
    331 			else
    332 				aprint_error_dev(self,
    333 				    "no proper endpoints for port %d (%d,%d)\n",
    334 				    port, hasin, hasout);
    335 		}
    336 
    337 	} else {
    338 		sc->sc_numcon = palmconinfo.num_ports;
    339 		if (sc->sc_numcon > UVISOR_MAX_CONN)
    340 			sc->sc_numcon = UVISOR_MAX_CONN;
    341 
    342 		/* Attach a ucom for each connection. */
    343 		for (i = 0; i < sc->sc_numcon; ++i) {
    344 			/*
    345 			 * XXX this should copy out 4-char string from the
    346 			 * XXX port_function_id, but where would the string go?
    347 			 * XXX ucaa.ucaa_info is a const char *, not an array.
    348 			 */
    349 			ucaa.ucaa_info = "sync";
    350 			ucaa.ucaa_portno = i;
    351 			if (palmconinfo.endpoint_numbers_different) {
    352 				port = palmconinfo.connections[i].end_point_info;
    353 				ucaa.ucaa_bulkin = (port >> 4) | UE_DIR_IN;
    354 				ucaa.ucaa_bulkout = (port & 0xf) | UE_DIR_OUT;
    355 			} else {
    356 				port = palmconinfo.connections[i].port;
    357 				ucaa.ucaa_bulkin = port | UE_DIR_IN;
    358 				ucaa.ucaa_bulkout = port | UE_DIR_OUT;
    359 			}
    360 			sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus",
    361 				NULL, &ucaa, ucomprint, ucomsubmatch);
    362 
    363 
    364 		}
    365 	}
    366 
    367 	return;
    368 
    369 bad:
    370 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
    371 	sc->sc_dying = 1;
    372 	return;
    373 }
    374 
    375 int
    376 uvisor_activate(device_t self, enum devact act)
    377 {
    378 	struct uvisor_softc *sc = device_private(self);
    379 
    380 	switch (act) {
    381 	case DVACT_DEACTIVATE:
    382 		sc->sc_dying = 1;
    383 		return 0;
    384 	default:
    385 		return EOPNOTSUPP;
    386 	}
    387 }
    388 
    389 void
    390 uvisor_childdet(device_t self, device_t child)
    391 {
    392 	int i;
    393 	struct uvisor_softc *sc = device_private(self);
    394 
    395 	for (i = 0; i < sc->sc_numcon; i++) {
    396 		if (sc->sc_subdevs[i] == child)
    397 			break;
    398 	}
    399 	KASSERT(i < sc->sc_numcon);
    400 	sc->sc_subdevs[i] = NULL;
    401 }
    402 
    403 int
    404 uvisor_detach(device_t self, int flags)
    405 {
    406 	struct uvisor_softc *sc = device_private(self);
    407 	int rv = 0;
    408 	int i;
    409 
    410 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
    411 	sc->sc_dying = 1;
    412 	for (i = 0; i < sc->sc_numcon; i++) {
    413 		if (sc->sc_subdevs[i] != NULL)
    414 			rv |= config_detach(sc->sc_subdevs[i], flags);
    415 	}
    416 
    417 	if (sc->sc_udev)
    418 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    419 				   sc->sc_dev);
    420 
    421 
    422 	return rv;
    423 }
    424 
    425 usbd_status
    426 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
    427     struct uvisor_palm_connection_info *cpi)
    428 {
    429 	usbd_status err;
    430 	usb_device_request_t req;
    431 	int actlen;
    432 	uWord avail;
    433 
    434 	if (sc->sc_flags & VISOR) {
    435 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
    436 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    437 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
    438 		USETW(req.wValue, 0);
    439 		USETW(req.wIndex, 0);
    440 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    441 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
    442 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    443 		if (err)
    444 			return err;
    445 	}
    446 
    447 	if (sc->sc_flags & PALM4) {
    448 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
    449 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    450 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
    451 		USETW(req.wValue, 0);
    452 		USETW(req.wIndex, 0);
    453 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
    454 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
    455 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    456 		if (err)
    457 			return err;
    458 	}
    459 
    460 	DPRINTF(("uvisor_init: getting available bytes\n"));
    461 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
    462 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
    463 	USETW(req.wValue, 0);
    464 	USETW(req.wIndex, 5);
    465 	USETW(req.wLength, sizeof(avail));
    466 	err = usbd_do_request(sc->sc_udev, &req, &avail);
    467 	if (err)
    468 		return err;
    469 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
    470 
    471 	DPRINTF(("uvisor_init: done\n"));
    472 	return err;
    473 }
    474 
    475 void
    476 uvisor_close(void *addr, int portno)
    477 {
    478 	struct uvisor_softc *sc = addr;
    479 	usb_device_request_t req;
    480 	struct uvisor_connection_info coninfo; /* XXX ? */
    481 	int actlen;
    482 
    483 	if (sc->sc_dying)
    484 		return;
    485 
    486 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
    487 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
    488 	USETW(req.wValue, 0);
    489 	USETW(req.wIndex, 0);
    490 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
    491 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
    492 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    493 }
    494