Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.22
      1 /* $NetBSD: uslsa.c,v 1.22 2016/11/25 12:56:29 skrll 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 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.22 2016/11/25 12:56:29 skrll Exp $");
     62 
     63 #ifdef _KERNEL_OPT
     64 #include "opt_usb.h"
     65 #endif
     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 #include <dev/usb/uslsareg.h>
     83 
     84 #include <fs/unicode.h>
     85 
     86 #ifdef USLSA_DEBUG
     87 #define DPRINTF(x)	if (uslsadebug) device_printf x
     88 int uslsadebug = 0;
     89 #else
     90 #define DPRINTF(x)
     91 #endif
     92 
     93 struct uslsa_softc {
     94 	device_t		sc_dev;		/* base device */
     95 	device_t		sc_subdev;	/* ucom device */
     96 	struct usbd_device *	sc_udev;	/* usb device */
     97 	struct usbd_interface *	sc_iface;	/* interface */
     98 	uint8_t			sc_ifnum;	/* interface number */
     99 	bool			sc_dying;	/* disconnecting */
    100 };
    101 
    102 static void uslsa_get_status(void *sc, int, u_char *, u_char *);
    103 static void uslsa_set(void *, int, int, int);
    104 static int uslsa_param(void *, int, struct termios *);
    105 static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *);
    106 
    107 static int uslsa_open(void *, int);
    108 static void uslsa_close(void *, int);
    109 
    110 static int uslsa_usbd_errno(usbd_status);
    111 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
    112 static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
    113 
    114 static const struct ucom_methods uslsa_methods = {
    115 	.ucom_get_status = uslsa_get_status,
    116 	.ucom_set = uslsa_set,
    117 	.ucom_param = uslsa_param,
    118 	.ucom_ioctl = uslsa_ioctl,
    119 	.ucom_open = uslsa_open,
    120 	.ucom_close = uslsa_close,
    121 	.ucom_read = NULL,
    122 	.ucom_write = NULL,
    123 };
    124 
    125 #define USLSA_CONFIG_INDEX	0
    126 #define USLSA_IFACE_INDEX	0
    127 #define USLSA_BUFSIZE		256
    128 
    129 static const struct usb_devno uslsa_devs[] = {
    130         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
    131         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
    132         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
    133         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
    134         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
    135         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
    136         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
    137         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
    138         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
    139         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
    140         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
    141         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
    142         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
    143         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
    144         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    145         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    146         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
    147 };
    148 
    149 static int uslsa_match(device_t, cfdata_t, void *);
    150 static void uslsa_attach(device_t, device_t, void *);
    151 static void uslsa_childdet(device_t, device_t);
    152 static int uslsa_detach(device_t, int);
    153 static int uslsa_activate(device_t, enum devact);
    154 
    155 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    156     uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
    157 
    158 static int
    159 uslsa_match(device_t parent, cfdata_t match, void *aux)
    160 {
    161 	const struct usbif_attach_arg *uiaa = aux;
    162 
    163 	if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)
    164 	    != NULL)
    165 		return UMATCH_VENDOR_PRODUCT;
    166 	else
    167 		return UMATCH_NONE;
    168 }
    169 
    170 static void
    171 uslsa_attach(device_t parent, device_t self, void *aux)
    172 {
    173 	struct uslsa_softc *sc;
    174 	const struct usbif_attach_arg *uiaa = aux;
    175 	const usb_interface_descriptor_t *id;
    176 	const usb_endpoint_descriptor_t *ed;
    177 	char *devinfop;
    178 	struct ucom_attach_args ucaa;
    179 	int i;
    180 
    181 	sc = device_private(self);
    182 
    183 	sc->sc_dev = self;
    184 	sc->sc_udev = uiaa->uiaa_device;
    185 	sc->sc_iface = uiaa->uiaa_iface;
    186 
    187 	aprint_naive("\n");
    188 	aprint_normal("\n");
    189 
    190 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    191 	aprint_normal_dev(self, "%s\n", devinfop);
    192 	usbd_devinfo_free(devinfop);
    193 
    194 	id = usbd_get_interface_descriptor(sc->sc_iface);
    195 
    196 	sc->sc_ifnum = id->bInterfaceNumber;
    197 
    198 	ucaa.ucaa_info = "Silicon Labs CP210x";
    199 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    200 	ucaa.ucaa_ibufsize = USLSA_BUFSIZE;
    201 	ucaa.ucaa_obufsize = USLSA_BUFSIZE;
    202 	ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE;
    203 	ucaa.ucaa_opkthdrlen = 0;
    204 	ucaa.ucaa_device = sc->sc_udev;
    205 	ucaa.ucaa_iface = sc->sc_iface;
    206 	ucaa.ucaa_methods = &uslsa_methods;
    207 	ucaa.ucaa_arg = sc;
    208 
    209 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    210 
    211 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    212 	for (i = 0; i < id->bNumEndpoints; i++) {
    213 		int addr, dir, attr;
    214 
    215 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    216 		if (ed == NULL) {
    217 			aprint_error_dev(self,
    218 			    "could not read endpoint descriptor\n");
    219 			sc->sc_dying = true;
    220 			return;
    221 		}
    222 		addr = ed->bEndpointAddress;
    223 		dir = UE_GET_DIR(ed->bEndpointAddress);
    224 		attr = ed->bmAttributes & UE_XFERTYPE;
    225 		if (dir == UE_DIR_IN && attr == UE_BULK) {
    226 			ucaa.ucaa_bulkin = addr;
    227 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    228 			ucaa.ucaa_bulkout = addr;
    229 		} else {
    230 			aprint_error_dev(self, "unexpected endpoint\n");
    231 		}
    232 	}
    233 	aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
    234 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
    235 	if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) {
    236 		aprint_error_dev(self, "could not find endpoints\n");
    237 		sc->sc_dying = true;
    238 		return;
    239 	}
    240 
    241 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    242 	                                    ucomprint, ucomsubmatch);
    243 
    244 	return;
    245 }
    246 
    247 static int
    248 uslsa_activate(device_t self, enum devact act)
    249 {
    250 	struct uslsa_softc *sc = device_private(self);
    251 
    252 	switch (act) {
    253 	case DVACT_DEACTIVATE:
    254 		sc->sc_dying = true;
    255 		return 0;
    256 	default:
    257 		return EOPNOTSUPP;
    258 	}
    259 }
    260 
    261 static void
    262 uslsa_childdet(device_t self, device_t child)
    263 {
    264 	struct uslsa_softc *sc = device_private(self);
    265 
    266 	KASSERT(sc->sc_subdev == child);
    267 	sc->sc_subdev = NULL;
    268 }
    269 
    270 static int
    271 uslsa_detach(device_t self, int flags)
    272 {
    273 	struct uslsa_softc *sc = device_private(self);
    274 	int rv = 0;
    275 
    276 	DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
    277 
    278 	sc->sc_dying = true;
    279 
    280 	if (sc->sc_subdev != NULL) {
    281 		rv = config_detach(sc->sc_subdev, flags);
    282 	}
    283 
    284 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    285 
    286 	return (rv);
    287 }
    288 
    289 static int
    290 uslsa_usbd_errno(usbd_status status)
    291 {
    292 	switch (status) {
    293 	case USBD_NORMAL_COMPLETION:
    294 		return 0;
    295 	case USBD_STALLED:
    296 		return EINVAL;
    297 	default:
    298 		return EIO;
    299 	}
    300 }
    301 
    302 static void
    303 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    304 {
    305 	struct uslsa_softc *sc;
    306 	usb_device_request_t req;
    307 	usbd_status status;
    308 	uint8_t mdmsts;
    309 
    310 	sc = vsc;
    311 
    312 	DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
    313 
    314 	if (sc->sc_dying) {
    315 		return;
    316 	}
    317 
    318 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    319 	req.bRequest = SLSA_R_GET_MDMSTS;
    320 	USETW(req.wValue, 0);
    321 	USETW(req.wIndex, sc->sc_ifnum);
    322 	USETW(req.wLength, SLSA_RL_GET_MDMSTS);
    323 
    324 	status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
    325 	if (status != USBD_NORMAL_COMPLETION) {
    326 		device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
    327 		    __func__, usbd_errstr(status));
    328 		return;
    329 	}
    330 
    331 	DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
    332 
    333 	if (lsr != NULL) {
    334 		*lsr = 0;
    335 	}
    336 
    337 	if (msr != NULL) {
    338 		*msr = 0;
    339 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
    340 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
    341 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
    342 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
    343 	}
    344 }
    345 
    346 static void
    347 uslsa_set(void *vsc, int portno, int reg, int onoff)
    348 {
    349 	struct uslsa_softc *sc;
    350 
    351 	sc = vsc;
    352 
    353 	DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno,
    354 	    reg, onoff));
    355 
    356 	if (sc->sc_dying) {
    357 		return;
    358 	}
    359 
    360 	switch (reg) {
    361 	case UCOM_SET_DTR:
    362 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    363 		    SLSA_RV_SET_MHS_DTR_MASK |
    364 		    (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
    365 			device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
    366 		}
    367 		break;
    368 	case UCOM_SET_RTS:
    369 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    370 		    SLSA_RV_SET_MHS_RTS_MASK |
    371 		    (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
    372 			device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
    373 		}
    374 		break;
    375 	case UCOM_SET_BREAK:
    376 		if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
    377 		    (onoff ? SLSA_RV_SET_BREAK_ENABLE :
    378 		     SLSA_RV_SET_BREAK_DISABLE))) {
    379 			device_printf(sc->sc_dev, "SET_BREAK failed\n");
    380 		}
    381 		break;
    382 	default:
    383 		break;
    384 	}
    385 }
    386 
    387 static int
    388 uslsa_param(void *vsc, int portno, struct termios *t)
    389 {
    390 	struct uslsa_softc *sc;
    391 	usb_device_request_t req;
    392 	usbd_status status;
    393 	uint16_t value;
    394 	uint32_t baud;
    395 	int ret;
    396 
    397 	sc = vsc;
    398 
    399 	DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
    400 
    401 	if (sc->sc_dying) {
    402 		return EIO;
    403 	}
    404 
    405 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    406 	req.bRequest = SLSA_R_SET_BAUDRATE;
    407 	USETW(req.wValue, 0);
    408 	USETW(req.wIndex, sc->sc_ifnum);
    409 	USETW(req.wLength, 4);
    410 
    411 	baud = t->c_ospeed;
    412 	status = usbd_do_request(sc->sc_udev, &req, &baud);
    413 	if (status != USBD_NORMAL_COMPLETION) {
    414 		/* fallback method for devices that don't know SET_BAUDRATE */
    415 		/* hope we calculate it right */
    416 		device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s,"
    417 				" using set bauddiv\n",
    418 		    __func__, baud, usbd_errstr(status));
    419 
    420 		value = SLSA_RV_BAUDDIV(t->c_ospeed);
    421 		if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value))
    422 		    != 0) {
    423 			device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
    424 			       __func__);
    425 			return ret;
    426 		}
    427 	}
    428 
    429 	value = 0;
    430 
    431 	if (ISSET(t->c_cflag, CSTOPB)) {
    432 		value |= SLSA_RV_LINE_CTL_STOP_2;
    433 	} else {
    434 		value |= SLSA_RV_LINE_CTL_STOP_1;
    435 	}
    436 
    437 	if (ISSET(t->c_cflag, PARENB)) {
    438 		if (ISSET(t->c_cflag, PARODD)) {
    439 			value |= SLSA_RV_LINE_CTL_PARITY_ODD;
    440 		} else {
    441 			value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
    442 		}
    443 	} else {
    444 		value |= SLSA_RV_LINE_CTL_PARITY_NONE;
    445 	}
    446 
    447 	switch (ISSET(t->c_cflag, CSIZE)) {
    448 	case CS5:
    449 		value |= SLSA_RV_LINE_CTL_LEN_5;
    450 		break;
    451 	case CS6:
    452 		value |= SLSA_RV_LINE_CTL_LEN_6;
    453 		break;
    454 	case CS7:
    455 		value |= SLSA_RV_LINE_CTL_LEN_7;
    456 		break;
    457 	case CS8:
    458 		value |= SLSA_RV_LINE_CTL_LEN_8;
    459 		break;
    460 	}
    461 
    462 	DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
    463 	    __func__, value));
    464 	if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
    465 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    466 		return ret;
    467 	}
    468 
    469 	if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
    470 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    471 	}
    472 
    473 	return ret;
    474 }
    475 
    476 static int
    477 uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
    478 {
    479 	struct uslsa_softc *sc;
    480 
    481 	sc = vsc;
    482 
    483 	switch (cmd) {
    484 	case TIOCMGET:
    485 		ucom_status_change(device_private(sc->sc_subdev));
    486 		return EPASSTHROUGH;
    487 	default:
    488 		return EPASSTHROUGH;
    489 	}
    490 
    491 	return 0;
    492 }
    493 
    494 static int
    495 uslsa_open(void *vsc, int portno)
    496 {
    497 	struct uslsa_softc *sc;
    498 
    499 	sc = vsc;
    500 
    501 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    502 
    503 	if (sc->sc_dying) {
    504 		return EIO;
    505 	}
    506 
    507 	return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
    508 	    SLSA_RV_IFC_ENABLE_ENABLE);
    509 }
    510 
    511 static void
    512 uslsa_close(void *vsc, int portno)
    513 {
    514 	struct uslsa_softc *sc;
    515 
    516 	sc = vsc;
    517 
    518 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    519 
    520 	if (sc->sc_dying) {
    521 		return;
    522 	}
    523 
    524 	(void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
    525 	    SLSA_RV_IFC_ENABLE_DISABLE);
    526 }
    527 
    528 static int
    529 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    530 {
    531 	usb_device_request_t req;
    532 	usbd_status status;
    533 
    534 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    535 	req.bRequest = request;
    536 	USETW(req.wValue, value);
    537 	USETW(req.wIndex, sc->sc_ifnum);
    538 	USETW(req.wLength, 0);
    539 
    540 	status = usbd_do_request(sc->sc_udev, &req, NULL);
    541 
    542 	return uslsa_usbd_errno(status);
    543 }
    544 
    545 static int
    546 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    547 {
    548 	struct slsa_fcs fcs;
    549 	usb_device_request_t req;
    550 	uint32_t ulControlHandshake;
    551 	uint32_t ulFlowReplace;
    552 	usbd_status status;
    553 
    554 	DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
    555 
    556 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    557 	req.bRequest = SLSA_R_GET_FLOW;
    558 	USETW(req.wValue, 0);
    559 	USETW(req.wIndex, sc->sc_ifnum);
    560 	USETW(req.wLength, SLSA_RL_GET_FLOW);
    561 
    562 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    563 	if (status != USBD_NORMAL_COMPLETION) {
    564 		device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
    565 			__func__, usbd_errstr(status));
    566 		return uslsa_usbd_errno(status);
    567 	}
    568 
    569 	ulControlHandshake = le32toh(fcs.ulControlHandshake);
    570 	ulFlowReplace = le32toh(fcs.ulFlowReplace);
    571 
    572 	if (ISSET(cflag, CRTSCTS)) {
    573 		ulControlHandshake =
    574 		    SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
    575 		ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
    576 	} else {
    577 		ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
    578 		ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
    579 	}
    580 
    581 	fcs.ulControlHandshake = htole32(ulControlHandshake);
    582 	fcs.ulFlowReplace = htole32(ulFlowReplace);
    583 
    584 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    585 	req.bRequest = SLSA_R_SET_FLOW;
    586 	USETW(req.wValue, 0);
    587 	USETW(req.wIndex, sc->sc_ifnum);
    588 	USETW(req.wLength, SLSA_RL_SET_FLOW);
    589 
    590 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    591 
    592 	return uslsa_usbd_errno(status);
    593 }
    594