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