Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.6.4.2
      1 /* $NetBSD: uslsa.c,v 1.6.4.2 2009/05/04 08:13:22 yamt Exp $ */
      2 
      3 /* from ugensa.c */
      4 
      5 /*
      6  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Roland C. Dowdeswell <elric (at) netbsd.org>.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 2007, 2009 Jonathan A. Kollasch.
     36  * All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  *
     58  */
     59 
     60 /*
     61  * Craig Shelley's Linux driver was used for documentation.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.6.4.2 2009/05/04 08:13:22 yamt Exp $");
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/kernel.h>
     70 #include <sys/device.h>
     71 #include <sys/conf.h>
     72 #include <sys/tty.h>
     73 
     74 #include <dev/usb/usb.h>
     75 
     76 #include <dev/usb/usbdi.h>
     77 #include <dev/usb/usbdi_util.h>
     78 #include <dev/usb/usbdevs.h>
     79 
     80 #include <dev/usb/ucomvar.h>
     81 
     82 #ifdef DEBUG
     83 #define USLSA_DEBUG
     84 #endif
     85 
     86 #ifdef USLSA_DEBUG
     87 #define DPRINTF(x)	if (uslsadebug) printf x
     88 #define DPRINTFN(n,x)	if (uslsadebug>(n)) printf x
     89 int uslsadebug = 0;
     90 #else
     91 #define DPRINTF(x)
     92 #define DPRINTFN(n,x)
     93 #endif
     94 
     95 #define USLSA_REQ_SET_STATE	0x00
     96 
     97 #define USLSA_REQ_SET_BPS	0x01
     98 #define USLSA_REQ_GET_BPS	0x02
     99 
    100 #define USLSA_REQ_SET_DPS	0x03
    101 #define USLSA_REQ_GET_DPS	0x04
    102 
    103 #define USLSA_REQ_SET_BREAK	0x05
    104 #define USLSA_REQ_GET_BREAK	0x06
    105 
    106 #define USLSA_REQ_SET_FLOW	0x07
    107 #define USLSA_REQ_GET_FLOW	0x08
    108 
    109 #define USLSA_REQ_SET_MODEM	0x13
    110 #define USLSA_REQ_GET_MODEM	0x14
    111 
    112 #define USLSA_REQ_SET_MISC	0x19
    113 #define USLSA_REQ_GET_MISC	0x20
    114 
    115 #define USLSA_STATE_DISABLE	0x0000
    116 #define USLSA_STATE_ENABLE	0x0001
    117 
    118 #define USLSA_BPS(b)	(3686400/b)
    119 
    120 #define USLSA_DPS_DATA_MASK		0x0f00
    121 #define	USLSA_DPS_DATA_FIVE		0x0500
    122 #define	USLSA_DPS_DATA_SIX		0x0600
    123 #define	USLSA_DPS_DATA_SEVEN		0x0700
    124 #define	USLSA_DPS_DATA_EIGHT		0x0800
    125 #define	USLSA_DPS_DATA_NINE		0x0900
    126 
    127 #define USLSA_DPS_PARITY_MASK		0x00f0
    128 #define USLSA_DPS_PARITY_SPACE		0x0040
    129 #define USLSA_DPS_PARITY_MARK		0x0030
    130 #define USLSA_DPS_PARITY_EVEN		0x0020
    131 #define USLSA_DPS_PARITY_ODD		0x0010
    132 #define USLSA_DPS_PARITY_NONE		0x0000
    133 
    134 #define USLSA_DPS_STOP_MASK		0x000f
    135 #define USLSA_DPS_STOP_TWO		0x0002
    136 #define USLSA_DPS_STOP_ONE_FIVE		0x0001
    137 #define USLSA_DPS_STOP_ONE		0x0000
    138 
    139 #define USLSA_BREAK_DISABLE	0x0001
    140 #define USLSA_BREAK_ENABLE	0x0000
    141 
    142 #define USLSA_FLOW_SET_RTS	0x0200
    143 #define USLSA_FLOW_SET_DTR	0x0100
    144 #define USLSA_FLOW_MSR_MASK	0x00f0
    145 #define USLSA_FLOW_MSR_DCD	0x0080
    146 #define USLSA_FLOW_MSR_RI	0x0040
    147 #define USLSA_FLOW_MSR_DSR	0x0020
    148 #define USLSA_FLOW_MSR_CTS	0x0010
    149 #define USLSA_FLOW_RTS		0x0002
    150 #define USLSA_FLOW_DTR		0x0001
    151 
    152 struct uslsa_softc {
    153 	USBBASEDEVICE		sc_dev;		/* base device */
    154 	usbd_device_handle	sc_udev;	/* device */
    155 	usbd_interface_handle	sc_iface;	/* interface */
    156 
    157 	device_t		sc_subdev;	/* ucom device */
    158 
    159 	u_char			sc_dying;	/* disconnecting */
    160 
    161 	u_char			sc_lsr;		/* local status register */
    162 	u_char			sc_msr;		/* uslsa status register */
    163 };
    164 
    165 static void uslsa_get_status(void *sc, int, u_char *, u_char *);
    166 static void uslsa_set(void *, int, int, int);
    167 static int uslsa_param(void *, int, struct termios *);
    168 static int uslsa_open(void *, int);
    169 static void uslsa_close(void *, int);
    170 
    171 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
    172 static void uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
    173 
    174 struct ucom_methods uslsa_methods = {
    175 	uslsa_get_status,
    176 	uslsa_set,
    177 	uslsa_param,
    178 	NULL,
    179 	uslsa_open,
    180 	uslsa_close,
    181 	NULL,
    182 	NULL,
    183 };
    184 
    185 #define USLSA_CONFIG_INDEX	0
    186 #define USLSA_IFACE_INDEX	0
    187 #define USLSA_BUFSIZE		256
    188 
    189 static const struct usb_devno uslsa_devs[] = {
    190         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
    191         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
    192         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
    193         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
    194         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
    195         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
    196         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
    197         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
    198         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
    199         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
    200         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
    201         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
    202         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
    203         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
    204         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    205         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    206         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
    207 };
    208 #define uslsa_lookup(v, p) usb_lookup(uslsa_devs, v, p)
    209 
    210 int uslsa_match(device_t, cfdata_t, void *);
    211 void uslsa_attach(device_t, device_t, void *);
    212 void uslsa_childdet(device_t, device_t);
    213 int uslsa_detach(device_t, int);
    214 int uslsa_activate(device_t, enum devact);
    215 extern struct cfdriver uslsa_cd;
    216 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    217     uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
    218 
    219 USB_MATCH(uslsa)
    220 {
    221 	USB_MATCH_START(uslsa, uaa);
    222 
    223 	return (uslsa_lookup(uaa->vendor, uaa->product) != NULL ?
    224 	        UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    225 }
    226 
    227 USB_ATTACH(uslsa)
    228 {
    229 	USB_ATTACH_START(uslsa, sc, uaa);
    230 	usbd_device_handle dev = uaa->device;
    231 	usbd_interface_handle iface;
    232 	usb_interface_descriptor_t *id;
    233 	usb_endpoint_descriptor_t *ed;
    234 	char *devinfop;
    235 	const char *devname;
    236 	usbd_status err;
    237 	struct ucom_attach_args uca;
    238 	int i;
    239 
    240 	sc->sc_dev = self;
    241 	devname = USBDEVNAME(sc->sc_dev);
    242 
    243 	DPRINTFN(10, ("\nuslsa_attach: sc=%p\n", sc));
    244 
    245 	/* Move the device into the configured state. */
    246 	err = usbd_set_config_index(dev, USLSA_CONFIG_INDEX, 1);
    247 	if (err) {
    248 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    249 	 	       devname, usbd_errstr(err));
    250 		goto bad;
    251 	}
    252 
    253 	err = usbd_device2interface_handle(dev, USLSA_IFACE_INDEX, &iface);
    254 	if (err) {
    255 		aprint_error("\n%s: failed to get interface, err=%s\n",
    256 		       devname, usbd_errstr(err));
    257 		goto bad;
    258 	}
    259 
    260 	devinfop = usbd_devinfo_alloc(dev, 0);
    261 	USB_ATTACH_SETUP;
    262 	aprint_normal_dev(self, "%s\n", devinfop);
    263 	usbd_devinfo_free(devinfop);
    264 
    265 	id = usbd_get_interface_descriptor(iface);
    266 
    267 	sc->sc_udev = dev;
    268 	sc->sc_iface = iface;
    269 
    270 	uca.info = "Silicon Labs CP210x";
    271 	uca.portno = UCOM_UNK_PORTNO;
    272 	uca.ibufsize = USLSA_BUFSIZE;
    273 	uca.obufsize = USLSA_BUFSIZE;
    274 	uca.ibufsizepad = USLSA_BUFSIZE;
    275 	uca.opkthdrlen = 0;
    276 	uca.device = dev;
    277 	uca.iface = iface;
    278 	uca.methods = &uslsa_methods;
    279 	uca.arg = sc;
    280 
    281 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    282 	                   USBDEV(sc->sc_dev));
    283 
    284 	uca.bulkin = uca.bulkout = -1;
    285 	for (i = 0; i < id->bNumEndpoints; i++) {
    286 		int addr, dir, attr;
    287 
    288 		ed = usbd_interface2endpoint_descriptor(iface, i);
    289 		if (ed == NULL) {
    290 			aprint_error_dev(self,
    291 			    "could not read endpoint descriptor: %s\n",
    292 			    usbd_errstr(err));
    293 			goto bad;
    294 		}
    295 		addr = ed->bEndpointAddress;
    296 		dir = UE_GET_DIR(ed->bEndpointAddress);
    297 		attr = ed->bmAttributes & UE_XFERTYPE;
    298 		if (dir == UE_DIR_IN && attr == UE_BULK)
    299 			uca.bulkin = addr;
    300 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
    301 			uca.bulkout = addr;
    302 		else
    303 			aprint_error_dev(self, "unexpected endpoint\n");
    304 	}
    305 	if (uca.bulkin == -1) {
    306 		aprint_error_dev(self, "Could not find data bulk in\n");
    307 		goto bad;
    308 	}
    309 	if (uca.bulkout == -1) {
    310 		aprint_error_dev(self, "Could not find data bulk out\n");
    311 		goto bad;
    312 	}
    313 
    314 	DPRINTF(("uslsa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    315 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    316 	                                    ucomprint, ucomsubmatch);
    317 
    318 	USB_ATTACH_SUCCESS_RETURN;
    319 
    320 bad:
    321 	DPRINTF(("uslsa_attach: ATTACH ERROR\n"));
    322 	sc->sc_dying = 1;
    323 	USB_ATTACH_ERROR_RETURN;
    324 }
    325 
    326 int
    327 uslsa_activate(device_t self, enum devact act)
    328 {
    329 	struct uslsa_softc *sc = device_private(self);
    330 	int rv = 0;
    331 
    332 	switch (act) {
    333 	case DVACT_ACTIVATE:
    334 		return (EOPNOTSUPP);
    335 		break;
    336 
    337 	case DVACT_DEACTIVATE:
    338 		sc->sc_dying = 1;
    339 		if (sc->sc_subdev)
    340 			rv = config_deactivate(sc->sc_subdev);
    341 		break;
    342 	}
    343 	return (rv);
    344 }
    345 
    346 void
    347 uslsa_childdet(device_t self, device_t child)
    348 {
    349 	struct uslsa_softc *sc = device_private(self);
    350 
    351 	KASSERT(sc->sc_subdev == child);
    352 	sc->sc_subdev = NULL;
    353 }
    354 
    355 USB_DETACH(uslsa)
    356 {
    357 	USB_DETACH_START(uslsa, sc);
    358 	int rv = 0;
    359 
    360 	DPRINTF(("uslsa_detach: sc=%p flags=%d\n", sc, flags));
    361 
    362 	sc->sc_dying = 1;
    363 
    364 	if (sc->sc_subdev != NULL)
    365 		rv = config_detach(sc->sc_subdev, flags);
    366 
    367 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    368 	                   USBDEV(sc->sc_dev));
    369 
    370 	return (rv);
    371 }
    372 
    373 static void
    374 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    375 {
    376 	struct uslsa_softc *sc;
    377 	usb_device_request_t req;
    378 	usbd_status err;
    379 	int actlen;
    380 	uint16_t flowreg;
    381 
    382 	sc = vsc;
    383 
    384 	DPRINTF(("uslsa_get_status:\n"));
    385 
    386 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    387 	req.bRequest = USLSA_REQ_GET_FLOW;
    388 	USETW(req.wValue, 0);
    389 	USETW(req.wIndex, 0);
    390 	USETW(req.wLength, sizeof(flowreg));
    391 
    392 	err = usbd_do_request_flags(sc->sc_udev, &req, &flowreg,
    393 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    394 	if (err)
    395 		printf("%s: uslsa_get_status: %s\n",
    396 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    397 
    398 	DPRINTF(("uslsa_get_status: flowreg=0x%x\n", flowreg));
    399 
    400 	sc->sc_msr = (u_char)(USLSA_FLOW_MSR_MASK & flowreg);
    401 
    402 	if (lsr != NULL)
    403 		*lsr = sc->sc_lsr;
    404 	if (msr != NULL)
    405 		*msr = sc->sc_msr;
    406 }
    407 
    408 static void
    409 uslsa_set(void *vsc, int portno, int reg, int onoff)
    410 {
    411 	struct uslsa_softc *sc;
    412 
    413 	sc = vsc;
    414 
    415 	DPRINTF(("uslsa_set: sc=%p, port=%d reg=%d onoff=%d\n", sc, portno,
    416 	         reg, onoff));
    417 
    418 	switch (reg) {
    419 	case UCOM_SET_DTR:
    420 		if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
    421 			(onoff ? (USLSA_FLOW_DTR | USLSA_FLOW_SET_DTR) :
    422 			    USLSA_FLOW_SET_DTR)))
    423 			printf("%s: uslsa_set_dtr failed\n",
    424 			       USBDEVNAME(sc->sc_dev));
    425 		break;
    426 	case UCOM_SET_RTS:
    427 		if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
    428 			(onoff ? (USLSA_FLOW_RTS | USLSA_FLOW_SET_RTS) :
    429 			    USLSA_FLOW_SET_RTS)))
    430 			printf("%s: uslsa_set_rts failed\n",
    431 			       USBDEVNAME(sc->sc_dev));
    432 		break;
    433 	case UCOM_SET_BREAK:
    434 		if (uslsa_request_set(sc, USLSA_REQ_SET_BREAK,
    435 			(onoff ? USLSA_BREAK_ENABLE :
    436 			    USLSA_BREAK_DISABLE)))
    437 			printf("%s: uslsa_set_break failed\n",
    438 			       USBDEVNAME(sc->sc_dev));
    439 		break;
    440 	default:
    441 		break;
    442 	}
    443 }
    444 
    445 static int
    446 uslsa_param(void *vsc, int portno, struct termios * t)
    447 {
    448 	struct uslsa_softc *sc;
    449 	uint16_t data;
    450 
    451 	sc = vsc;
    452 
    453 	DPRINTF(("uslsa_param: sc=%p\n", sc));
    454 
    455 	switch (t->c_ospeed) {
    456 	case B600:
    457 	case B1200:
    458 	case B2400:
    459 	case B4800:
    460 	case B9600:
    461 	case B19200:
    462 	case B38400:
    463 	case B57600:
    464 	case B115200:
    465 	case B230400:
    466 	case B460800:
    467 	case B921600:
    468 		data = USLSA_BPS(t->c_ospeed);
    469 		break;
    470 	default:
    471 		printf("%s: uslsa_param: unsupported data rate, "
    472 		       "forcing default of 115200bps\n",
    473 		       USBDEVNAME(sc->sc_dev));
    474 		data = USLSA_BPS(B115200);
    475 	};
    476 
    477 	if (uslsa_request_set(sc, USLSA_REQ_SET_BPS, data))
    478 		printf("%s: uslsa_param: setting data rate failed\n",
    479 		       USBDEVNAME(sc->sc_dev));
    480 
    481 	data = 0;
    482 
    483 	if (ISSET(t->c_cflag, CSTOPB))
    484 		data |= USLSA_DPS_STOP_TWO;
    485 	else
    486 		data |= USLSA_DPS_STOP_ONE;
    487 
    488 	if (ISSET(t->c_cflag, PARENB)) {
    489 		if (ISSET(t->c_cflag, PARODD))
    490 			data |= USLSA_DPS_PARITY_ODD;
    491 		else
    492 			data |= USLSA_DPS_PARITY_EVEN;
    493 	} else
    494 		data |= USLSA_DPS_PARITY_NONE;
    495 
    496 	switch (ISSET(t->c_cflag, CSIZE)) {
    497 	case CS5:
    498 		data |= USLSA_DPS_DATA_FIVE;
    499 		break;
    500 	case CS6:
    501 		data |= USLSA_DPS_DATA_SIX;
    502 		break;
    503 	case CS7:
    504 		data |= USLSA_DPS_DATA_SEVEN;
    505 		break;
    506 	case CS8:
    507 		data |= USLSA_DPS_DATA_EIGHT;
    508 		break;
    509 	}
    510 
    511 	DPRINTF(("uslsa_param: setting DPS register to 0x%x\n", data));
    512 	if (uslsa_request_set(sc, USLSA_REQ_SET_DPS, data))
    513 		printf("%s: setting DPS register failed: invalid argument\n",
    514 		       USBDEVNAME(sc->sc_dev));
    515 
    516 	uslsa_set_flow(sc, t->c_cflag, t->c_iflag);
    517 
    518 	return 0;
    519 }
    520 
    521 
    522 static int
    523 uslsa_open(void *vsc, int portno)
    524 {
    525 	struct uslsa_softc *sc;
    526 
    527 	sc = vsc;
    528 
    529 	DPRINTF(("uslsa_open: sc=%p\n", sc));
    530 
    531 	if (sc->sc_dying)
    532 		return (EIO);
    533 
    534 	if (uslsa_request_set(sc, USLSA_REQ_SET_STATE, USLSA_STATE_ENABLE))
    535 		return (EIO);
    536 
    537 	return 0;
    538 }
    539 
    540 void
    541 uslsa_close(void *vsc, int portno)
    542 {
    543 	struct uslsa_softc *sc;
    544 
    545 	sc = vsc;
    546 
    547 	if (sc->sc_dying)
    548 		return;
    549 
    550 	DPRINTF(("uslsa_close: sc=%p\n", sc));
    551 
    552 	if (uslsa_request_set(sc, USLSA_REQ_SET_STATE,
    553 	    USLSA_STATE_DISABLE))
    554 		printf("%s: disable-on-close failed\n",
    555 		       USBDEVNAME(sc->sc_dev));
    556 }
    557 
    558 /*
    559  * uslsa_request_set(), wrapper for doing sets with usbd_do_request()
    560  */
    561 static int
    562 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    563 {
    564 	usb_device_request_t req;
    565 	usbd_status err;
    566 
    567 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    568 	req.bRequest = request;
    569 	USETW(req.wValue, value);
    570 	USETW(req.wIndex, 0);
    571 	USETW(req.wLength, 0);
    572 
    573 	err = usbd_do_request(sc->sc_udev, &req, 0);
    574 	if (err)
    575 		printf("%s: uslsa_request: %s\n",
    576 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    577 	return err;
    578 }
    579 
    580 /*
    581  * uslsa_set_flow() does some magic to set up hardware flow control
    582  */
    583 static void
    584 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    585 {
    586 	uint8_t mysterydata[16];
    587 	usb_device_request_t req;
    588 	usbd_status err;
    589 
    590 	DPRINTF(("uslsa_set_flow: cflag = 0x%x, iflag = 0x%x\n",
    591 	         cflag, iflag));
    592 
    593 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    594 	req.bRequest = USLSA_REQ_GET_MODEM;
    595 	USETW(req.wValue, 0);
    596 	USETW(req.wIndex, 0);
    597 	USETW(req.wLength, 16);
    598 
    599 	err = usbd_do_request(sc->sc_udev, &req, mysterydata);
    600 	if (err)
    601 		printf("%s: uslsa_set_flow: %s\n",
    602 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    603 
    604 	if (ISSET(cflag, CRTSCTS)) {
    605 		mysterydata[0] &= ~0x7b;
    606 		mysterydata[0] |= 0x09;
    607 		mysterydata[4] = 0x80;
    608 	} else {
    609 		mysterydata[0] &= ~0x7b;
    610 		mysterydata[0] |= 0x01;
    611 		mysterydata[4] = 0x40;
    612 	}
    613 
    614 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    615 	req.bRequest = USLSA_REQ_SET_MODEM;
    616 	USETW(req.wValue, 0);
    617 	USETW(req.wIndex, 0);
    618 	USETW(req.wLength, 16);
    619 
    620 	err = usbd_do_request(sc->sc_udev, &req, mysterydata);
    621 	if (err)
    622 		printf("%s: uslsa_set_flow: %s\n",
    623 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    624 }
    625