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