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