Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.64
      1 /*	$NetBSD: uftdi.c,v 1.64 2016/12/15 17:21:21 maya 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).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.64 2016/12/15 17:21:21 maya Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_usb.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/conf.h>
     44 #include <sys/tty.h>
     45 
     46 #include <dev/usb/usb.h>
     47 
     48 #include <dev/usb/usbdi.h>
     49 #include <dev/usb/usbdi_util.h>
     50 #include <dev/usb/usbdevs.h>
     51 
     52 #include <dev/usb/ucomvar.h>
     53 
     54 #include <dev/usb/uftdireg.h>
     55 
     56 #ifdef UFTDI_DEBUG
     57 #define DPRINTF(x)	if (uftdidebug) printf x
     58 #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
     59 int uftdidebug = 0;
     60 #else
     61 #define DPRINTF(x)
     62 #define DPRINTFN(n,x)
     63 #endif
     64 
     65 #define UFTDI_CONFIG_INDEX	0
     66 #define UFTDI_IFACE_INDEX	0
     67 #define UFTDI_MAX_PORTS		4
     68 
     69 /*
     70  * These are the default number of bytes transferred per frame if the
     71  * endpoint doesn't tell us.  The output buffer size is a hard limit
     72  * for devices that use a 6-bit size encoding.
     73  */
     74 #define UFTDIIBUFSIZE 64
     75 #define UFTDIOBUFSIZE 64
     76 
     77 /*
     78  * Magic constants!  Where do these come from?  They're what Linux uses...
     79  */
     80 #define	UFTDI_MAX_IBUFSIZE	512
     81 #define	UFTDI_MAX_OBUFSIZE	256
     82 
     83 struct uftdi_softc {
     84 	device_t		sc_dev;		/* base device */
     85 	struct usbd_device *	sc_udev;	/* device */
     86 	struct usbd_interface *	sc_iface[UFTDI_MAX_PORTS];	/* interface */
     87 
     88 	enum uftdi_type		sc_type;
     89 	u_int			sc_hdrlen;
     90 	u_int			sc_numports;
     91 	u_int			sc_chiptype;
     92 
     93 	u_char			sc_msr;
     94 	u_char			sc_lsr;
     95 
     96 	device_t		sc_subdev[UFTDI_MAX_PORTS];
     97 
     98 	u_char			sc_dying;
     99 
    100 	u_int			last_lcr;
    101 
    102 };
    103 
    104 Static void	uftdi_get_status(void *, int, u_char *, u_char *);
    105 Static void	uftdi_set(void *, int, int, int);
    106 Static int	uftdi_param(void *, int, struct termios *);
    107 Static int	uftdi_open(void *, int);
    108 Static void	uftdi_read(void *, int, u_char **, uint32_t *);
    109 Static void	uftdi_write(void *, int, u_char *, u_char *,
    110 			    uint32_t *);
    111 Static void	uftdi_break(void *, int, int);
    112 
    113 struct ucom_methods uftdi_methods = {
    114 	.ucom_get_status = uftdi_get_status,
    115 	.ucom_set = uftdi_set,
    116 	.ucom_param = uftdi_param,
    117 	.ucom_ioctl = NULL,
    118 	.ucom_open = uftdi_open,
    119 	.ucom_close = NULL,
    120 	.ucom_read = uftdi_read,
    121 	.ucom_write = uftdi_write,
    122 };
    123 
    124 /*
    125  * The devices default to UFTDI_TYPE_8U232AM.
    126  * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
    127  */
    128 static const struct usb_devno uftdi_devs[] = {
    129 	{ USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
    130 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
    131 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
    132 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X },
    133 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
    134 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL },
    135 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
    136 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
    137 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
    138 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
    139 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
    140 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
    141 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
    142 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
    143 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
    144 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
    145 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
    146 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
    147 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
    148 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
    149 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
    150 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
    151 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
    152 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
    153 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
    154 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
    155 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
    156 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
    157 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
    158 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
    159 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
    160 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
    161 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE },
    162 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
    163 	{ USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
    164 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
    165 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
    166 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
    167 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
    168 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
    169 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
    170 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
    171 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
    172 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
    173 	{ USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
    174 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
    175 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
    176 };
    177 #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
    178 
    179 int uftdi_match(device_t, cfdata_t, void *);
    180 void uftdi_attach(device_t, device_t, void *);
    181 void uftdi_childdet(device_t, device_t);
    182 int uftdi_detach(device_t, int);
    183 int uftdi_activate(device_t, enum devact);
    184 extern struct cfdriver uftdi_cd;
    185 CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
    186     uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
    187 
    188 int
    189 uftdi_match(device_t parent, cfdata_t match, void *aux)
    190 {
    191 	struct usb_attach_arg *uaa = aux;
    192 
    193 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
    194 		     uaa->uaa_vendor, uaa->uaa_product));
    195 
    196 	return uftdi_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    197 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    198 }
    199 
    200 void
    201 uftdi_attach(device_t parent, device_t self, void *aux)
    202 {
    203 	struct uftdi_softc *sc = device_private(self);
    204 	struct usb_attach_arg *uaa = aux;
    205 	struct usbd_device *dev = uaa->uaa_device;
    206 	struct usbd_interface *iface;
    207 	usb_device_descriptor_t *ddesc;
    208 	usb_interface_descriptor_t *id;
    209 	usb_endpoint_descriptor_t *ed;
    210 	char *devinfop;
    211 	const char *devname = device_xname(self);
    212 	int i,idx;
    213 	usbd_status err;
    214 	struct ucom_attach_args ucaa;
    215 
    216 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
    217 
    218 	aprint_naive("\n");
    219 	aprint_normal("\n");
    220 
    221 	devinfop = usbd_devinfo_alloc(dev, 0);
    222 	aprint_normal_dev(self, "%s\n", devinfop);
    223 	usbd_devinfo_free(devinfop);
    224 
    225 	/* Move the device into the configured state. */
    226 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
    227 	if (err) {
    228 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    229 		       devname, usbd_errstr(err));
    230 		goto bad;
    231 	}
    232 
    233 	sc->sc_dev = self;
    234 	sc->sc_udev = dev;
    235 	sc->sc_numports = 1;
    236 	sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
    237 	sc->sc_hdrlen = 0;
    238 	if (uaa->uaa_vendor == USB_VENDOR_FTDI
    239 	    && uaa->uaa_product == USB_PRODUCT_FTDI_SERIAL_8U100AX) {
    240 		sc->sc_type = UFTDI_TYPE_SIO;
    241 		sc->sc_hdrlen = 1;
    242 	}
    243 
    244 	ddesc = usbd_get_device_descriptor(dev);
    245 	sc->sc_chiptype = UGETW(ddesc->bcdDevice);
    246 	switch (sc->sc_chiptype) {
    247 	case 0x500: /* 2232D */
    248 	case 0x700: /* 2232H */
    249 		sc->sc_numports = 2;
    250 		break;
    251 	case 0x800: /* 4232H */
    252 		sc->sc_numports = 4;
    253 		break;
    254 	case 0x200: /* 232/245AM */
    255 	case 0x400: /* 232/245BL */
    256 	case 0x600: /* 232/245R */
    257 	default:
    258 		break;
    259 	}
    260 
    261 	for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) {
    262 		err = usbd_device2interface_handle(dev, idx, &iface);
    263 		if (err) {
    264 			aprint_error(
    265 			    "\n%s: failed to get interface idx=%d, err=%s\n",
    266 			    devname, idx, usbd_errstr(err));
    267 			goto bad;
    268 		}
    269 
    270 		id = usbd_get_interface_descriptor(iface);
    271 
    272 		sc->sc_iface[idx] = iface;
    273 
    274 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    275 		ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0;
    276 		for (i = 0; i < id->bNumEndpoints; i++) {
    277 			int addr, dir, attr;
    278 			ed = usbd_interface2endpoint_descriptor(iface, i);
    279 			if (ed == NULL) {
    280 				aprint_error_dev(self,
    281 				    "could not read endpoint descriptor: %s\n",
    282 				    usbd_errstr(err));
    283 				goto bad;
    284 			}
    285 
    286 			addr = ed->bEndpointAddress;
    287 			dir = UE_GET_DIR(ed->bEndpointAddress);
    288 			attr = ed->bmAttributes & UE_XFERTYPE;
    289 			if (dir == UE_DIR_IN && attr == UE_BULK) {
    290 				ucaa.ucaa_bulkin = addr;
    291 				ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
    292 				if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
    293 					ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
    294 			} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    295 				ucaa.ucaa_bulkout = addr;
    296 				ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
    297 				    - sc->sc_hdrlen;
    298 				if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
    299 					ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE;
    300 				/* Limit length if we have a 6-bit header.  */
    301 				if ((sc->sc_hdrlen > 0) &&
    302 				    (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
    303 					ucaa.ucaa_obufsize = UFTDIOBUFSIZE;
    304 			} else {
    305 				aprint_error_dev(self,
    306 				    "unexpected endpoint\n");
    307 				goto bad;
    308 			}
    309 		}
    310 		if (ucaa.ucaa_bulkin == -1) {
    311 			aprint_error_dev(self,
    312 			    "Could not find data bulk in\n");
    313 			goto bad;
    314 		}
    315 		if (ucaa.ucaa_bulkout == -1) {
    316 			aprint_error_dev(self,
    317 			    "Could not find data bulk out\n");
    318 			goto bad;
    319 		}
    320 
    321 		ucaa.ucaa_portno = FTDI_PIT_SIOA + idx;
    322 		/* ucaa_bulkin, ucaa_bulkout set above */
    323 		if (ucaa.ucaa_ibufsize == 0)
    324 			ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
    325 		ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
    326 		if (ucaa.ucaa_obufsize == 0)
    327 			ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    328 		ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
    329 		ucaa.ucaa_device = dev;
    330 		ucaa.ucaa_iface = iface;
    331 		ucaa.ucaa_methods = &uftdi_methods;
    332 		ucaa.ucaa_arg = sc;
    333 		ucaa.ucaa_info = NULL;
    334 
    335 		DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n",
    336 			ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
    337 			ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
    338 		sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL,
    339 		    &ucaa, ucomprint, ucomsubmatch);
    340 	}
    341 
    342 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    343 
    344 	if (!pmf_device_register(self, NULL, NULL))
    345 		aprint_error_dev(self, "couldn't establish power handler\n");
    346 
    347 	return;
    348 
    349 bad:
    350 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    351 	sc->sc_dying = 1;
    352 	return;
    353 }
    354 
    355 int
    356 uftdi_activate(device_t self, enum devact act)
    357 {
    358 	struct uftdi_softc *sc = device_private(self);
    359 
    360 	switch (act) {
    361 	case DVACT_DEACTIVATE:
    362 		sc->sc_dying = 1;
    363 		return 0;
    364 	default:
    365 		return EOPNOTSUPP;
    366 	}
    367 }
    368 
    369 void
    370 uftdi_childdet(device_t self, device_t child)
    371 {
    372 	int i;
    373 	struct uftdi_softc *sc = device_private(self);
    374 
    375 	for (i = 0; i < sc->sc_numports; i++) {
    376 		if (sc->sc_subdev[i] == child)
    377 			break;
    378 	}
    379 	KASSERT(i < sc->sc_numports);
    380 	sc->sc_subdev[i] = NULL;
    381 }
    382 
    383 int
    384 uftdi_detach(device_t self, int flags)
    385 {
    386 	struct uftdi_softc *sc = device_private(self);
    387 	int i;
    388 
    389 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    390 	sc->sc_dying = 1;
    391 	for (i=0; i < sc->sc_numports; i++) {
    392 		if (sc->sc_subdev[i] != NULL)
    393 			config_detach(sc->sc_subdev[i], flags);
    394 	}
    395 
    396 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    397 
    398 	return 0;
    399 }
    400 
    401 Static int
    402 uftdi_open(void *vsc, int portno)
    403 {
    404 	struct uftdi_softc *sc = vsc;
    405 	usb_device_request_t req;
    406 	usbd_status err;
    407 	struct termios t;
    408 
    409 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    410 
    411 	if (sc->sc_dying)
    412 		return EIO;
    413 
    414 	/* Perform a full reset on the device */
    415 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    416 	req.bRequest = FTDI_SIO_RESET;
    417 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    418 	USETW(req.wIndex, portno);
    419 	USETW(req.wLength, 0);
    420 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    421 	if (err)
    422 		return EIO;
    423 
    424 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    425 	t.c_ospeed = 9600;
    426 	t.c_cflag = CSTOPB | CS8;
    427 	(void)uftdi_param(sc, portno, &t);
    428 
    429 	/* Turn on RTS/CTS flow control */
    430 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    431 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    432 	USETW(req.wValue, 0);
    433 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    434 	USETW(req.wLength, 0);
    435 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    436 	if (err)
    437 		return EIO;
    438 
    439 	return 0;
    440 }
    441 
    442 Static void
    443 uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
    444 {
    445 	struct uftdi_softc *sc = vsc;
    446 	u_char msr, lsr;
    447 
    448 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    449 		     *count));
    450 
    451 	msr = FTDI_GET_MSR(*ptr);
    452 	lsr = FTDI_GET_LSR(*ptr);
    453 
    454 #ifdef UFTDI_DEBUG
    455 	if (*count != 2)
    456 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    457 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    458 #endif
    459 
    460 	if (sc->sc_msr != msr ||
    461 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    462 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    463 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    464 			 lsr, sc->sc_lsr));
    465 		sc->sc_msr = msr;
    466 		sc->sc_lsr = lsr;
    467 		ucom_status_change(device_private(sc->sc_subdev[portno-1]));
    468 	}
    469 
    470 	/* Adjust buffer pointer to skip status prefix */
    471 	*ptr += 2;
    472 }
    473 
    474 Static void
    475 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
    476 {
    477 	struct uftdi_softc *sc = vsc;
    478 
    479 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    480 		     vsc, portno, *count, from[0]));
    481 
    482 	/* Make length tag and copy data */
    483 	if (sc->sc_hdrlen > 0)
    484 		*to = FTDI_OUT_TAG(*count, portno);
    485 
    486 	memcpy(to + sc->sc_hdrlen, from, *count);
    487 	*count += sc->sc_hdrlen;
    488 }
    489 
    490 Static void
    491 uftdi_set(void *vsc, int portno, int reg, int onoff)
    492 {
    493 	struct uftdi_softc *sc = vsc;
    494 	usb_device_request_t req;
    495 	int ctl;
    496 
    497 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    498 		 reg, onoff));
    499 
    500 	switch (reg) {
    501 	case UCOM_SET_DTR:
    502 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    503 		break;
    504 	case UCOM_SET_RTS:
    505 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    506 		break;
    507 	case UCOM_SET_BREAK:
    508 		uftdi_break(sc, portno, onoff);
    509 		return;
    510 	default:
    511 		return;
    512 	}
    513 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    514 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    515 	USETW(req.wValue, ctl);
    516 	USETW(req.wIndex, portno);
    517 	USETW(req.wLength, 0);
    518 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    519 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    520 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    521 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    522 }
    523 
    524 Static int
    525 uftdi_param(void *vsc, int portno, struct termios *t)
    526 {
    527 	struct uftdi_softc *sc = vsc;
    528 	usb_device_request_t req;
    529 	usbd_status err;
    530 	int rate, data, flow;
    531 
    532 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    533 
    534 	if (sc->sc_dying)
    535 		return EIO;
    536 
    537 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    538 	req.bRequest = FTDI_SIO_SET_BITMODE;
    539 	USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
    540 	USETW(req.wIndex, portno);
    541 	USETW(req.wLength, 0);
    542 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    543 	if (err)
    544 		return EIO;
    545 
    546 	switch (sc->sc_type) {
    547 	case UFTDI_TYPE_SIO:
    548 		switch (t->c_ospeed) {
    549 		case 300: rate = ftdi_sio_b300; break;
    550 		case 600: rate = ftdi_sio_b600; break;
    551 		case 1200: rate = ftdi_sio_b1200; break;
    552 		case 2400: rate = ftdi_sio_b2400; break;
    553 		case 4800: rate = ftdi_sio_b4800; break;
    554 		case 9600: rate = ftdi_sio_b9600; break;
    555 		case 19200: rate = ftdi_sio_b19200; break;
    556 		case 38400: rate = ftdi_sio_b38400; break;
    557 		case 57600: rate = ftdi_sio_b57600; break;
    558 		case 115200: rate = ftdi_sio_b115200; break;
    559 		default:
    560 			return EINVAL;
    561 		}
    562 		break;
    563 
    564 	case UFTDI_TYPE_8U232AM:
    565 		switch(t->c_ospeed) {
    566 		case 300: rate = ftdi_8u232am_b300; break;
    567 		case 600: rate = ftdi_8u232am_b600; break;
    568 		case 1200: rate = ftdi_8u232am_b1200; break;
    569 		case 2400: rate = ftdi_8u232am_b2400; break;
    570 		case 4800: rate = ftdi_8u232am_b4800; break;
    571 		case 9600: rate = ftdi_8u232am_b9600; break;
    572 		case 19200: rate = ftdi_8u232am_b19200; break;
    573 		case 38400: rate = ftdi_8u232am_b38400; break;
    574 		case 57600: rate = ftdi_8u232am_b57600; break;
    575 		case 115200: rate = ftdi_8u232am_b115200; break;
    576 		case 230400: rate = ftdi_8u232am_b230400; break;
    577 		case 460800: rate = ftdi_8u232am_b460800; break;
    578 		case 921600: rate = ftdi_8u232am_b921600; break;
    579 		default:
    580 			return EINVAL;
    581 		}
    582 		break;
    583 
    584 	default:
    585 		return EINVAL;
    586 	}
    587 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    588 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    589 	USETW(req.wValue, rate);
    590 	USETW(req.wIndex, portno);
    591 	USETW(req.wLength, 0);
    592 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    593 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    594 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    595 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    596 	if (err)
    597 		return EIO;
    598 
    599 	if (ISSET(t->c_cflag, CSTOPB))
    600 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    601 	else
    602 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    603 	if (ISSET(t->c_cflag, PARENB)) {
    604 		if (ISSET(t->c_cflag, PARODD))
    605 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    606 		else
    607 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    608 	} else
    609 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    610 	switch (ISSET(t->c_cflag, CSIZE)) {
    611 	case CS5:
    612 		data |= FTDI_SIO_SET_DATA_BITS(5);
    613 		break;
    614 	case CS6:
    615 		data |= FTDI_SIO_SET_DATA_BITS(6);
    616 		break;
    617 	case CS7:
    618 		data |= FTDI_SIO_SET_DATA_BITS(7);
    619 		break;
    620 	case CS8:
    621 		data |= FTDI_SIO_SET_DATA_BITS(8);
    622 		break;
    623 	}
    624 	sc->last_lcr = data;
    625 
    626 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    627 	req.bRequest = FTDI_SIO_SET_DATA;
    628 	USETW(req.wValue, data);
    629 	USETW(req.wIndex, portno);
    630 	USETW(req.wLength, 0);
    631 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    632 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    633 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    634 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    635 	if (err)
    636 		return EIO;
    637 
    638 	if (ISSET(t->c_cflag, CRTSCTS)) {
    639 		flow = FTDI_SIO_RTS_CTS_HS;
    640 		USETW(req.wValue, 0);
    641 	} else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
    642 		flow = FTDI_SIO_XON_XOFF_HS;
    643 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    644 	} else {
    645 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    646 		USETW(req.wValue, 0);
    647 	}
    648 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    649 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    650 	USETW2(req.wIndex, flow, portno);
    651 	USETW(req.wLength, 0);
    652 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    653 	if (err)
    654 		return EIO;
    655 
    656 	return 0;
    657 }
    658 
    659 void
    660 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    661 {
    662 	struct uftdi_softc *sc = vsc;
    663 
    664 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    665 		 sc->sc_msr, sc->sc_lsr));
    666 
    667 	if (msr != NULL)
    668 		*msr = sc->sc_msr;
    669 	if (lsr != NULL)
    670 		*lsr = sc->sc_lsr;
    671 }
    672 
    673 void
    674 uftdi_break(void *vsc, int portno, int onoff)
    675 {
    676 	struct uftdi_softc *sc = vsc;
    677 	usb_device_request_t req;
    678 	int data;
    679 
    680 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    681 		  onoff));
    682 
    683 	if (onoff) {
    684 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    685 	} else {
    686 		data = sc->last_lcr;
    687 	}
    688 
    689 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    690 	req.bRequest = FTDI_SIO_SET_DATA;
    691 	USETW(req.wValue, data);
    692 	USETW(req.wIndex, portno);
    693 	USETW(req.wLength, 0);
    694 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    695 }
    696