Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.16
      1 /* $NetBSD: uslsa.c,v 1.16 2011/12/23 00:51:49 jakllsch 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.16 2011/12/23 00:51:49 jakllsch 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 
    105 #define USLSA_REQ_SET_FLOW	0x07
    106 #define USLSA_REQ_GET_FLOW	0x08
    107 
    108 #define USLSA_REQ_SET_MODEM	0x13
    109 #define USLSA_REQ_GET_MODEM	0x14
    110 
    111 #define USLSA_REQ_SET_MISC	0x19
    112 #define USLSA_REQ_GET_MISC	0x20
    113 
    114 #define USLSA_STATE_DISABLE	0x0000
    115 #define USLSA_STATE_ENABLE	0x0001
    116 
    117 #define USLSA_BPS(b)	(3686400/b)
    118 
    119 #define USLSA_DPS_DATA_MASK		0x0f00
    120 #define	USLSA_DPS_DATA_FIVE		0x0500
    121 #define	USLSA_DPS_DATA_SIX		0x0600
    122 #define	USLSA_DPS_DATA_SEVEN		0x0700
    123 #define	USLSA_DPS_DATA_EIGHT		0x0800
    124 #define	USLSA_DPS_DATA_NINE		0x0900
    125 
    126 #define USLSA_DPS_PARITY_MASK		0x00f0
    127 #define USLSA_DPS_PARITY_SPACE		0x0040
    128 #define USLSA_DPS_PARITY_MARK		0x0030
    129 #define USLSA_DPS_PARITY_EVEN		0x0020
    130 #define USLSA_DPS_PARITY_ODD		0x0010
    131 #define USLSA_DPS_PARITY_NONE		0x0000
    132 
    133 #define USLSA_DPS_STOP_MASK		0x000f
    134 #define USLSA_DPS_STOP_TWO		0x0002
    135 #define USLSA_DPS_STOP_ONE_FIVE		0x0001
    136 #define USLSA_DPS_STOP_ONE		0x0000
    137 
    138 #define USLSA_BREAK_DISABLE	0x0000
    139 #define USLSA_BREAK_ENABLE	0x0001
    140 
    141 #define USLSA_FLOW_SET_RTS	0x0200
    142 #define USLSA_FLOW_SET_DTR	0x0100
    143 #define USLSA_FLOW_MSR_MASK	0x00f0
    144 #define USLSA_FLOW_MSR_DCD	0x0080
    145 #define USLSA_FLOW_MSR_RI	0x0040
    146 #define USLSA_FLOW_MSR_DSR	0x0020
    147 #define USLSA_FLOW_MSR_CTS	0x0010
    148 #define USLSA_FLOW_RTS		0x0002
    149 #define USLSA_FLOW_DTR		0x0001
    150 
    151 struct uslsa_softc {
    152 	device_t		sc_dev;		/* base device */
    153 	usbd_device_handle	sc_udev;	/* device */
    154 	usbd_interface_handle	sc_iface;	/* interface */
    155 
    156 	device_t		sc_subdev;	/* ucom device */
    157 
    158 	u_char			sc_dying;	/* disconnecting */
    159 
    160 	u_char			sc_lsr;		/* local status register */
    161 	u_char			sc_msr;		/* uslsa status register */
    162 };
    163 
    164 static void uslsa_get_status(void *sc, int, u_char *, u_char *);
    165 static void uslsa_set(void *, int, int, int);
    166 static int uslsa_param(void *, int, struct termios *);
    167 static int uslsa_open(void *, int);
    168 static void uslsa_close(void *, int);
    169 
    170 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
    171 static void uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
    172 
    173 static const struct ucom_methods uslsa_methods = {
    174 	uslsa_get_status,
    175 	uslsa_set,
    176 	uslsa_param,
    177 	NULL,
    178 	uslsa_open,
    179 	uslsa_close,
    180 	NULL,
    181 	NULL,
    182 };
    183 
    184 #define USLSA_CONFIG_INDEX	0
    185 #define USLSA_IFACE_INDEX	0
    186 #define USLSA_BUFSIZE		256
    187 
    188 static const struct usb_devno uslsa_devs[] = {
    189         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
    190         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
    191         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
    192         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
    193         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
    194         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
    195         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
    196         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
    197         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
    198         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
    199         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
    200         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
    201         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
    202         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
    203         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    204         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    205         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
    206 };
    207 #define uslsa_lookup(v, p) usb_lookup(uslsa_devs, v, p)
    208 
    209 static int uslsa_match(device_t, cfdata_t, void *);
    210 static void uslsa_attach(device_t, device_t, void *);
    211 static void uslsa_childdet(device_t, device_t);
    212 static int uslsa_detach(device_t, int);
    213 static int uslsa_activate(device_t, enum devact);
    214 
    215 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    216     uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
    217 
    218 static int
    219 uslsa_match(device_t parent, cfdata_t match, void *aux)
    220 {
    221 	struct usb_attach_arg *uaa = aux;
    222 
    223 	return (uslsa_lookup(uaa->vendor, uaa->product) != NULL ?
    224 	        UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    225 }
    226 
    227 static void
    228 uslsa_attach(device_t parent, device_t self, void *aux)
    229 {
    230 	struct uslsa_softc *sc = device_private(self);
    231 	struct usb_attach_arg *uaa = aux;
    232 	usbd_device_handle dev = uaa->device;
    233 	usbd_interface_handle iface;
    234 	usb_interface_descriptor_t *id;
    235 	usb_endpoint_descriptor_t *ed;
    236 	char *devinfop;
    237 	usbd_status err;
    238 	struct ucom_attach_args uca;
    239 	int i;
    240 
    241 	sc->sc_dev = self;
    242 
    243 	DPRINTFN(10, ("\nuslsa_attach: sc=%p\n", sc));
    244 
    245 	aprint_naive("\n");
    246 	aprint_normal("\n");
    247 
    248 	devinfop = usbd_devinfo_alloc(dev, 0);
    249 	aprint_normal_dev(self, "%s\n", devinfop);
    250 	usbd_devinfo_free(devinfop);
    251 
    252 	/* Move the device into the configured state. */
    253 	err = usbd_set_config_index(dev, USLSA_CONFIG_INDEX, 1);
    254 	if (err) {
    255 		aprint_error_dev(self, "failed to set configuration, err=%s\n",
    256 		   usbd_errstr(err));
    257 		goto bad;
    258 	}
    259 
    260 	err = usbd_device2interface_handle(dev, USLSA_IFACE_INDEX, &iface);
    261 	if (err) {
    262 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    263 		   usbd_errstr(err));
    264 		goto bad;
    265 	}
    266 
    267 	id = usbd_get_interface_descriptor(iface);
    268 
    269 	sc->sc_udev = dev;
    270 	sc->sc_iface = iface;
    271 
    272 	uca.info = "Silicon Labs CP210x";
    273 	uca.portno = UCOM_UNK_PORTNO;
    274 	uca.ibufsize = USLSA_BUFSIZE;
    275 	uca.obufsize = USLSA_BUFSIZE;
    276 	uca.ibufsizepad = USLSA_BUFSIZE;
    277 	uca.opkthdrlen = 0;
    278 	uca.device = dev;
    279 	uca.iface = iface;
    280 	uca.methods = &uslsa_methods;
    281 	uca.arg = sc;
    282 
    283 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    284 	                   sc->sc_dev);
    285 
    286 	uca.bulkin = uca.bulkout = -1;
    287 	for (i = 0; i < id->bNumEndpoints; i++) {
    288 		int addr, dir, attr;
    289 
    290 		ed = usbd_interface2endpoint_descriptor(iface, i);
    291 		if (ed == NULL) {
    292 			aprint_error_dev(self,
    293 			    "could not read endpoint descriptor: %s\n",
    294 			    usbd_errstr(err));
    295 			goto bad;
    296 		}
    297 		addr = ed->bEndpointAddress;
    298 		dir = UE_GET_DIR(ed->bEndpointAddress);
    299 		attr = ed->bmAttributes & UE_XFERTYPE;
    300 		if (dir == UE_DIR_IN && attr == UE_BULK)
    301 			uca.bulkin = addr;
    302 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
    303 			uca.bulkout = addr;
    304 		else
    305 			aprint_error_dev(self, "unexpected endpoint\n");
    306 	}
    307 	if (uca.bulkin == -1) {
    308 		aprint_error_dev(self, "Could not find data bulk in\n");
    309 		goto bad;
    310 	}
    311 	if (uca.bulkout == -1) {
    312 		aprint_error_dev(self, "Could not find data bulk out\n");
    313 		goto bad;
    314 	}
    315 
    316 	DPRINTF(("uslsa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    317 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    318 	                                    ucomprint, ucomsubmatch);
    319 
    320 	return;
    321 
    322 bad:
    323 	DPRINTF(("uslsa_attach: ATTACH ERROR\n"));
    324 	sc->sc_dying = 1;
    325 	return;
    326 }
    327 
    328 static int
    329 uslsa_activate(device_t self, enum devact act)
    330 {
    331 	struct uslsa_softc *sc = device_private(self);
    332 
    333 	switch (act) {
    334 	case DVACT_DEACTIVATE:
    335 		sc->sc_dying = 1;
    336 		return 0;
    337 	default:
    338 		return EOPNOTSUPP;
    339 	}
    340 }
    341 
    342 static void
    343 uslsa_childdet(device_t self, device_t child)
    344 {
    345 	struct uslsa_softc *sc = device_private(self);
    346 
    347 	KASSERT(sc->sc_subdev == child);
    348 	sc->sc_subdev = NULL;
    349 }
    350 
    351 static int
    352 uslsa_detach(device_t self, int flags)
    353 {
    354 	struct uslsa_softc *sc = device_private(self);
    355 	int rv = 0;
    356 
    357 	DPRINTF(("uslsa_detach: sc=%p flags=%d\n", sc, flags));
    358 
    359 	sc->sc_dying = 1;
    360 
    361 	if (sc->sc_subdev != NULL)
    362 		rv = config_detach(sc->sc_subdev, flags);
    363 
    364 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    365 	                   sc->sc_dev);
    366 
    367 	return (rv);
    368 }
    369 
    370 static void
    371 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    372 {
    373 	struct uslsa_softc *sc;
    374 	usb_device_request_t req;
    375 	usbd_status err;
    376 	int actlen;
    377 	uint16_t flowreg;
    378 
    379 	sc = vsc;
    380 
    381 	DPRINTF(("uslsa_get_status:\n"));
    382 
    383 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    384 	req.bRequest = USLSA_REQ_GET_FLOW;
    385 	USETW(req.wValue, 0);
    386 	USETW(req.wIndex, 0);
    387 	USETW(req.wLength, sizeof(flowreg));
    388 
    389 	err = usbd_do_request_flags(sc->sc_udev, &req, &flowreg,
    390 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    391 	if (err)
    392 		printf("%s: uslsa_get_status: %s\n",
    393 		    device_xname(sc->sc_dev), usbd_errstr(err));
    394 
    395 	DPRINTF(("uslsa_get_status: flowreg=0x%x\n", flowreg));
    396 
    397 	sc->sc_msr = (u_char)(USLSA_FLOW_MSR_MASK & flowreg);
    398 
    399 	if (lsr != NULL)
    400 		*lsr = sc->sc_lsr;
    401 	if (msr != NULL)
    402 		*msr = sc->sc_msr;
    403 }
    404 
    405 static void
    406 uslsa_set(void *vsc, int portno, int reg, int onoff)
    407 {
    408 	struct uslsa_softc *sc;
    409 
    410 	sc = vsc;
    411 
    412 	DPRINTF(("uslsa_set: sc=%p, port=%d reg=%d onoff=%d\n", sc, portno,
    413 	         reg, onoff));
    414 
    415 	switch (reg) {
    416 	case UCOM_SET_DTR:
    417 		if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
    418 			(onoff ? (USLSA_FLOW_DTR | USLSA_FLOW_SET_DTR) :
    419 			    USLSA_FLOW_SET_DTR)))
    420 			printf("%s: uslsa_set_dtr failed\n",
    421 			       device_xname(sc->sc_dev));
    422 		break;
    423 	case UCOM_SET_RTS:
    424 		if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
    425 			(onoff ? (USLSA_FLOW_RTS | USLSA_FLOW_SET_RTS) :
    426 			    USLSA_FLOW_SET_RTS)))
    427 			printf("%s: uslsa_set_rts failed\n",
    428 			       device_xname(sc->sc_dev));
    429 		break;
    430 	case UCOM_SET_BREAK:
    431 		if (uslsa_request_set(sc, USLSA_REQ_SET_BREAK,
    432 			(onoff ? USLSA_BREAK_ENABLE :
    433 			    USLSA_BREAK_DISABLE)))
    434 			printf("%s: uslsa_set_break failed\n",
    435 			       device_xname(sc->sc_dev));
    436 		break;
    437 	default:
    438 		break;
    439 	}
    440 }
    441 
    442 static int
    443 uslsa_param(void *vsc, int portno, struct termios * t)
    444 {
    445 	struct uslsa_softc *sc;
    446 	uint16_t data;
    447 
    448 	sc = vsc;
    449 
    450 	DPRINTF(("uslsa_param: sc=%p\n", sc));
    451 
    452 	switch (t->c_ospeed) {
    453 	case B600:
    454 	case B1200:
    455 	case B2400:
    456 	case B4800:
    457 	case B9600:
    458 	case B19200:
    459 	case B38400:
    460 	case B57600:
    461 	case B115200:
    462 	case B230400:
    463 	case B460800:
    464 	case B921600:
    465 		data = USLSA_BPS(t->c_ospeed);
    466 		break;
    467 	default:
    468 		printf("%s: uslsa_param: unsupported data rate, "
    469 		       "forcing default of 115200bps\n",
    470 		       device_xname(sc->sc_dev));
    471 		data = USLSA_BPS(B115200);
    472 	};
    473 
    474 	if (uslsa_request_set(sc, USLSA_REQ_SET_BPS, data))
    475 		printf("%s: uslsa_param: setting data rate failed\n",
    476 		       device_xname(sc->sc_dev));
    477 
    478 	data = 0;
    479 
    480 	if (ISSET(t->c_cflag, CSTOPB))
    481 		data |= USLSA_DPS_STOP_TWO;
    482 	else
    483 		data |= USLSA_DPS_STOP_ONE;
    484 
    485 	if (ISSET(t->c_cflag, PARENB)) {
    486 		if (ISSET(t->c_cflag, PARODD))
    487 			data |= USLSA_DPS_PARITY_ODD;
    488 		else
    489 			data |= USLSA_DPS_PARITY_EVEN;
    490 	} else
    491 		data |= USLSA_DPS_PARITY_NONE;
    492 
    493 	switch (ISSET(t->c_cflag, CSIZE)) {
    494 	case CS5:
    495 		data |= USLSA_DPS_DATA_FIVE;
    496 		break;
    497 	case CS6:
    498 		data |= USLSA_DPS_DATA_SIX;
    499 		break;
    500 	case CS7:
    501 		data |= USLSA_DPS_DATA_SEVEN;
    502 		break;
    503 	case CS8:
    504 		data |= USLSA_DPS_DATA_EIGHT;
    505 		break;
    506 	}
    507 
    508 	DPRINTF(("uslsa_param: setting DPS register to 0x%x\n", data));
    509 	if (uslsa_request_set(sc, USLSA_REQ_SET_DPS, data))
    510 		printf("%s: setting DPS register failed: invalid argument\n",
    511 		       device_xname(sc->sc_dev));
    512 
    513 	uslsa_set_flow(sc, t->c_cflag, t->c_iflag);
    514 
    515 	return 0;
    516 }
    517 
    518 
    519 static int
    520 uslsa_open(void *vsc, int portno)
    521 {
    522 	struct uslsa_softc *sc;
    523 
    524 	sc = vsc;
    525 
    526 	DPRINTF(("uslsa_open: sc=%p\n", sc));
    527 
    528 	if (sc->sc_dying)
    529 		return (EIO);
    530 
    531 	if (uslsa_request_set(sc, USLSA_REQ_SET_STATE, USLSA_STATE_ENABLE))
    532 		return (EIO);
    533 
    534 	return 0;
    535 }
    536 
    537 static void
    538 uslsa_close(void *vsc, int portno)
    539 {
    540 	struct uslsa_softc *sc;
    541 
    542 	sc = vsc;
    543 
    544 	if (sc->sc_dying)
    545 		return;
    546 
    547 	DPRINTF(("uslsa_close: sc=%p\n", sc));
    548 
    549 	if (uslsa_request_set(sc, USLSA_REQ_SET_STATE,
    550 	    USLSA_STATE_DISABLE))
    551 		printf("%s: disable-on-close failed\n",
    552 		       device_xname(sc->sc_dev));
    553 }
    554 
    555 /*
    556  * uslsa_request_set(), wrapper for doing sets with usbd_do_request()
    557  */
    558 static int
    559 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    560 {
    561 	usb_device_request_t req;
    562 	usbd_status err;
    563 
    564 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    565 	req.bRequest = request;
    566 	USETW(req.wValue, value);
    567 	USETW(req.wIndex, 0);
    568 	USETW(req.wLength, 0);
    569 
    570 	err = usbd_do_request(sc->sc_udev, &req, 0);
    571 	if (err)
    572 		printf("%s: uslsa_request: %s\n",
    573 		       device_xname(sc->sc_dev), usbd_errstr(err));
    574 	return err;
    575 }
    576 
    577 /*
    578  * uslsa_set_flow() does some magic to set up hardware flow control
    579  */
    580 static void
    581 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    582 {
    583 	uint8_t mysterydata[16];
    584 	usb_device_request_t req;
    585 	usbd_status err;
    586 
    587 	DPRINTF(("uslsa_set_flow: cflag = 0x%x, iflag = 0x%x\n",
    588 	         cflag, iflag));
    589 
    590 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    591 	req.bRequest = USLSA_REQ_GET_MODEM;
    592 	USETW(req.wValue, 0);
    593 	USETW(req.wIndex, 0);
    594 	USETW(req.wLength, 16);
    595 
    596 	err = usbd_do_request(sc->sc_udev, &req, mysterydata);
    597 	if (err)
    598 		printf("%s: uslsa_set_flow: %s\n",
    599 		       device_xname(sc->sc_dev), usbd_errstr(err));
    600 
    601 	if (ISSET(cflag, CRTSCTS)) {
    602 		mysterydata[0] &= ~0x7b;
    603 		mysterydata[0] |= 0x09;
    604 		mysterydata[4] = 0x80;
    605 	} else {
    606 		mysterydata[0] &= ~0x7b;
    607 		mysterydata[0] |= 0x01;
    608 		mysterydata[4] = 0x40;
    609 	}
    610 
    611 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    612 	req.bRequest = USLSA_REQ_SET_MODEM;
    613 	USETW(req.wValue, 0);
    614 	USETW(req.wIndex, 0);
    615 	USETW(req.wLength, 16);
    616 
    617 	err = usbd_do_request(sc->sc_udev, &req, mysterydata);
    618 	if (err)
    619 		printf("%s: uslsa_set_flow: %s\n",
    620 		       device_xname(sc->sc_dev), usbd_errstr(err));
    621 }
    622