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