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