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