Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.67.4.3
      1  1.67.4.2    martin /*	$NetBSD: uftdi.c,v 1.67.4.3 2020/04/13 08:04:49 martin Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net).
      9       1.1  augustss  *
     10       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11       1.1  augustss  * modification, are permitted provided that the following conditions
     12       1.1  augustss  * are met:
     13       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18       1.1  augustss  *
     19       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  augustss  */
     31       1.1  augustss 
     32       1.7     lukem #include <sys/cdefs.h>
     33  1.67.4.2    martin __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.67.4.3 2020/04/13 08:04:49 martin Exp $");
     34      1.63     skrll 
     35      1.63     skrll #ifdef _KERNEL_OPT
     36      1.63     skrll #include "opt_usb.h"
     37      1.63     skrll #endif
     38       1.1  augustss 
     39       1.1  augustss #include <sys/param.h>
     40       1.1  augustss #include <sys/systm.h>
     41       1.1  augustss #include <sys/kernel.h>
     42       1.1  augustss #include <sys/device.h>
     43       1.1  augustss #include <sys/conf.h>
     44       1.1  augustss #include <sys/tty.h>
     45       1.1  augustss 
     46       1.1  augustss #include <dev/usb/usb.h>
     47       1.1  augustss 
     48       1.1  augustss #include <dev/usb/usbdi.h>
     49       1.1  augustss #include <dev/usb/usbdi_util.h>
     50       1.1  augustss #include <dev/usb/usbdevs.h>
     51       1.1  augustss 
     52       1.1  augustss #include <dev/usb/ucomvar.h>
     53       1.1  augustss 
     54       1.1  augustss #include <dev/usb/uftdireg.h>
     55       1.1  augustss 
     56       1.1  augustss #ifdef UFTDI_DEBUG
     57       1.1  augustss #define DPRINTF(x)	if (uftdidebug) printf x
     58       1.1  augustss #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
     59       1.4  augustss int uftdidebug = 0;
     60       1.1  augustss #else
     61       1.1  augustss #define DPRINTF(x)
     62       1.1  augustss #define DPRINTFN(n,x)
     63       1.1  augustss #endif
     64       1.1  augustss 
     65      1.67        ws #define UFTDI_CONFIG_NO		1
     66       1.1  augustss 
     67       1.1  augustss /*
     68      1.53  riastrad  * These are the default number of bytes transferred per frame if the
     69      1.53  riastrad  * endpoint doesn't tell us.  The output buffer size is a hard limit
     70      1.53  riastrad  * for devices that use a 6-bit size encoding.
     71       1.1  augustss  */
     72       1.1  augustss #define UFTDIIBUFSIZE 64
     73       1.1  augustss #define UFTDIOBUFSIZE 64
     74       1.1  augustss 
     75      1.53  riastrad /*
     76      1.53  riastrad  * Magic constants!  Where do these come from?  They're what Linux uses...
     77      1.53  riastrad  */
     78      1.53  riastrad #define	UFTDI_MAX_IBUFSIZE	512
     79      1.53  riastrad #define	UFTDI_MAX_OBUFSIZE	256
     80      1.53  riastrad 
     81       1.1  augustss struct uftdi_softc {
     82      1.47    dyoung 	device_t		sc_dev;		/* base device */
     83      1.61     skrll 	struct usbd_device *	sc_udev;	/* device */
     84      1.67        ws 	struct usbd_interface *	sc_iface;	/* interface */
     85      1.67        ws 	int			sc_iface_no;
     86       1.1  augustss 
     87      1.10       scw 	enum uftdi_type		sc_type;
     88  1.67.4.3    martin 	u_int			sc_flags;
     89  1.67.4.3    martin #define FLAGS_BAUDCLK_12M	0x00000001
     90  1.67.4.3    martin #define FLAGS_ROUNDOFF_232A	0x00000002
     91  1.67.4.3    martin #define FLAGS_BAUDBITS_HINDEX	0x00000004
     92      1.10       scw 	u_int			sc_hdrlen;
     93      1.41  nisimura 	u_int			sc_chiptype;
     94      1.10       scw 
     95       1.1  augustss 	u_char			sc_msr;
     96       1.1  augustss 	u_char			sc_lsr;
     97       1.1  augustss 
     98      1.67        ws 	device_t		sc_subdev;
     99       1.1  augustss 
    100  1.67.4.1  christos 	bool			sc_dying;
    101       1.8    ichiro 
    102       1.8    ichiro 	u_int			last_lcr;
    103       1.1  augustss };
    104       1.1  augustss 
    105  1.67.4.1  christos static void	uftdi_get_status(void *, int, u_char *, u_char *);
    106  1.67.4.1  christos static void	uftdi_set(void *, int, int, int);
    107  1.67.4.1  christos static int	uftdi_param(void *, int, struct termios *);
    108  1.67.4.1  christos static int	uftdi_open(void *, int);
    109  1.67.4.1  christos static void	uftdi_read(void *, int, u_char **, uint32_t *);
    110  1.67.4.1  christos static void	uftdi_write(void *, int, u_char *, u_char *, uint32_t *);
    111  1.67.4.1  christos static void	uftdi_break(void *, int, int);
    112       1.1  augustss 
    113  1.67.4.2    martin static const struct ucom_methods uftdi_methods = {
    114      1.61     skrll 	.ucom_get_status = uftdi_get_status,
    115      1.61     skrll 	.ucom_set = uftdi_set,
    116      1.61     skrll 	.ucom_param = uftdi_param,
    117      1.61     skrll 	.ucom_open = uftdi_open,
    118      1.61     skrll 	.ucom_read = uftdi_read,
    119      1.61     skrll 	.ucom_write = uftdi_write,
    120       1.1  augustss };
    121       1.1  augustss 
    122      1.61     skrll /*
    123      1.26  christos  * The devices default to UFTDI_TYPE_8U232AM.
    124      1.47    dyoung  * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
    125      1.26  christos  */
    126      1.26  christos static const struct usb_devno uftdi_devs[] = {
    127      1.26  christos 	{ USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
    128      1.26  christos 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
    129      1.26  christos 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
    130      1.59   msaitoh 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X },
    131      1.55   reinoud 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
    132      1.56   mlelstv 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL },
    133      1.30       riz 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
    134      1.42  nisimura 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
    135      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
    136      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
    137      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
    138      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
    139      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
    140      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
    141      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
    142      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
    143      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
    144      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
    145      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
    146      1.45    martin 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
    147      1.45    martin 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
    148      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
    149      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
    150      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
    151      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
    152      1.27   xtraeme 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
    153      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
    154      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
    155      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
    156      1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
    157      1.27   xtraeme 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
    158      1.52      matt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
    159      1.57   cheusov 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE },
    160      1.54  jakllsch 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
    161      1.48      matt 	{ USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
    162      1.26  christos 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
    163      1.26  christos 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
    164      1.60    nonaka 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
    165      1.40      taca 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
    166      1.65   jnemeth 	{ USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_CT57A },
    167      1.66  jakllsch 	{ USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_RTS03 },
    168      1.26  christos 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
    169      1.39     rmind 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
    170      1.39     rmind 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
    171      1.39     rmind 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
    172      1.39     rmind 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
    173      1.26  christos 	{ USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
    174      1.51    plunky 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
    175      1.51    plunky 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
    176      1.26  christos };
    177      1.26  christos #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
    178      1.26  christos 
    179  1.67.4.1  christos static int	uftdi_match(device_t, cfdata_t, void *);
    180  1.67.4.1  christos static void	uftdi_attach(device_t, device_t, void *);
    181  1.67.4.1  christos static void	uftdi_childdet(device_t, device_t);
    182  1.67.4.1  christos static int	uftdi_detach(device_t, int);
    183  1.67.4.1  christos 
    184      1.38      cube CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
    185  1.67.4.1  christos     uftdi_attach, uftdi_detach, NULL, NULL, uftdi_childdet);
    186       1.1  augustss 
    187  1.67.4.1  christos static int
    188      1.47    dyoung uftdi_match(device_t parent, cfdata_t match, void *aux)
    189       1.1  augustss {
    190      1.67        ws 	struct usbif_attach_arg *uiaa = aux;
    191      1.11  augustss 
    192  1.67.4.2    martin 	DPRINTFN(20,("uftdi: vendor=%#x, product=%#x\n",
    193      1.67        ws 		     uiaa->uiaa_vendor, uiaa->uiaa_product));
    194       1.1  augustss 
    195      1.67        ws 	if (uiaa->uiaa_configno != UFTDI_CONFIG_NO)
    196      1.67        ws 		return UMATCH_NONE;
    197      1.67        ws 
    198      1.67        ws 	return uftdi_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ?
    199      1.67        ws 		UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
    200       1.1  augustss }
    201       1.1  augustss 
    202  1.67.4.1  christos static void
    203      1.47    dyoung uftdi_attach(device_t parent, device_t self, void *aux)
    204       1.1  augustss {
    205      1.47    dyoung 	struct uftdi_softc *sc = device_private(self);
    206      1.67        ws 	struct usbif_attach_arg *uiaa = aux;
    207      1.67        ws 	struct usbd_device *dev = uiaa->uiaa_device;
    208      1.67        ws 	struct usbd_interface *iface = uiaa->uiaa_iface;
    209      1.41  nisimura 	usb_device_descriptor_t *ddesc;
    210       1.1  augustss 	usb_interface_descriptor_t *id;
    211       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    212      1.24  augustss 	char *devinfop;
    213      1.67        ws 	int i;
    214      1.61     skrll 	struct ucom_attach_args ucaa;
    215       1.1  augustss 
    216       1.1  augustss 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
    217       1.1  augustss 
    218      1.43    plunky 	aprint_naive("\n");
    219      1.43    plunky 	aprint_normal("\n");
    220      1.43    plunky 
    221      1.43    plunky 	devinfop = usbd_devinfo_alloc(dev, 0);
    222      1.43    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    223      1.43    plunky 	usbd_devinfo_free(devinfop);
    224      1.43    plunky 
    225      1.38      cube 	sc->sc_dev = self;
    226       1.1  augustss 	sc->sc_udev = dev;
    227  1.67.4.1  christos 	sc->sc_dying = false;
    228      1.67        ws 	sc->sc_iface_no = uiaa->uiaa_ifaceno;
    229      1.41  nisimura 	sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
    230      1.41  nisimura 	sc->sc_hdrlen = 0;
    231      1.41  nisimura 
    232      1.41  nisimura 	ddesc = usbd_get_device_descriptor(dev);
    233      1.41  nisimura 	sc->sc_chiptype = UGETW(ddesc->bcdDevice);
    234      1.11  augustss 
    235  1.67.4.3    martin 	switch (sc->sc_chiptype) {
    236  1.67.4.3    martin 	case 0x0200:
    237  1.67.4.3    martin 		if (ddesc->iSerialNumber != 0)
    238  1.67.4.3    martin 			sc->sc_flags |= FLAGS_ROUNDOFF_232A;
    239  1.67.4.3    martin 		ucaa.ucaa_portno = 0;
    240  1.67.4.3    martin 		break;
    241  1.67.4.3    martin 	case 0x0400:
    242  1.67.4.3    martin 		ucaa.ucaa_portno = 0;
    243  1.67.4.3    martin 		break;
    244  1.67.4.3    martin 	case 0x0500:
    245  1.67.4.3    martin 		sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
    246  1.67.4.3    martin 		ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
    247  1.67.4.3    martin 		break;
    248  1.67.4.3    martin 	case 0x0600:
    249  1.67.4.3    martin 		ucaa.ucaa_portno = 0;
    250  1.67.4.3    martin 		break;
    251  1.67.4.3    martin 	case 0x0700:
    252  1.67.4.3    martin 	case 0x0800:
    253  1.67.4.3    martin 	case 0x0900:
    254  1.67.4.3    martin 		sc->sc_flags |= FLAGS_BAUDCLK_12M;
    255  1.67.4.3    martin 		sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
    256  1.67.4.3    martin 		ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
    257  1.67.4.3    martin 		break;
    258  1.67.4.3    martin 	case 0x1000:
    259  1.67.4.3    martin 		sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
    260  1.67.4.3    martin 		ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
    261  1.67.4.3    martin 		break;
    262  1.67.4.3    martin 	default:
    263  1.67.4.3    martin 		if (sc->sc_chiptype < 0x0200) {
    264  1.67.4.3    martin 			sc->sc_type = UFTDI_TYPE_SIO;
    265  1.67.4.3    martin 			sc->sc_hdrlen = 1;
    266  1.67.4.3    martin 		}
    267  1.67.4.3    martin 		ucaa.ucaa_portno = 0;
    268  1.67.4.3    martin 		break;
    269  1.67.4.3    martin 	}
    270  1.67.4.3    martin 
    271      1.67        ws 	id = usbd_get_interface_descriptor(iface);
    272      1.32       riz 
    273      1.67        ws 	sc->sc_iface = iface;
    274      1.32       riz 
    275      1.67        ws 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    276      1.67        ws 	ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0;
    277      1.67        ws 	for (i = 0; i < id->bNumEndpoints; i++) {
    278      1.67        ws 		int addr, dir, attr;
    279      1.67        ws 		ed = usbd_interface2endpoint_descriptor(iface, i);
    280      1.67        ws 		if (ed == NULL) {
    281      1.38      cube 			aprint_error_dev(self,
    282      1.67        ws 			    "could not read endpoint descriptor\n");
    283       1.1  augustss 			goto bad;
    284       1.1  augustss 		}
    285      1.67        ws 
    286      1.67        ws 		addr = ed->bEndpointAddress;
    287      1.67        ws 		dir = UE_GET_DIR(ed->bEndpointAddress);
    288      1.67        ws 		attr = ed->bmAttributes & UE_XFERTYPE;
    289      1.67        ws 		if (dir == UE_DIR_IN && attr == UE_BULK) {
    290      1.67        ws 			ucaa.ucaa_bulkin = addr;
    291      1.67        ws 			ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
    292      1.67        ws 			if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
    293      1.67        ws 				ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
    294      1.67        ws 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    295      1.67        ws 			ucaa.ucaa_bulkout = addr;
    296      1.67        ws 			ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
    297      1.67        ws 			    - sc->sc_hdrlen;
    298      1.67        ws 			if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
    299      1.67        ws 				ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE;
    300      1.67        ws 			/* Limit length if we have a 6-bit header.  */
    301      1.67        ws 			if ((sc->sc_hdrlen > 0) &&
    302      1.67        ws 			    (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
    303      1.67        ws 				ucaa.ucaa_obufsize = UFTDIOBUFSIZE;
    304      1.67        ws 		} else {
    305      1.67        ws 			aprint_error_dev(self, "unexpected endpoint\n");
    306      1.32       riz 			goto bad;
    307      1.32       riz 		}
    308      1.67        ws 	}
    309      1.67        ws 	if (ucaa.ucaa_bulkin == -1) {
    310      1.67        ws 		aprint_error_dev(self, "Could not find data bulk in\n");
    311      1.67        ws 		goto bad;
    312      1.67        ws 	}
    313      1.67        ws 	if (ucaa.ucaa_bulkout == -1) {
    314      1.67        ws 		aprint_error_dev(self, "Could not find data bulk out\n");
    315      1.67        ws 		goto bad;
    316      1.67        ws 	}
    317      1.32       riz 
    318      1.67        ws 	/* ucaa_bulkin, ucaa_bulkout set above */
    319      1.67        ws 	if (ucaa.ucaa_ibufsize == 0)
    320      1.67        ws 		ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
    321      1.67        ws 	ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
    322      1.67        ws 	if (ucaa.ucaa_obufsize == 0)
    323      1.67        ws 		ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    324      1.67        ws 	ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
    325      1.67        ws 	ucaa.ucaa_device = dev;
    326      1.67        ws 	ucaa.ucaa_iface = iface;
    327      1.67        ws 	ucaa.ucaa_methods = &uftdi_methods;
    328      1.67        ws 	ucaa.ucaa_arg = sc;
    329      1.67        ws 	ucaa.ucaa_info = NULL;
    330      1.67        ws 
    331  1.67.4.2    martin 	DPRINTF(("uftdi: in=%#x out=%#x isize=%#x osize=%#x\n",
    332      1.67        ws 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
    333      1.67        ws 		ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
    334      1.67        ws 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL,
    335      1.67        ws 	    &ucaa, ucomprint, ucomsubmatch);
    336       1.1  augustss 
    337      1.62   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    338       1.5  augustss 
    339      1.64      maya 	if (!pmf_device_register(self, NULL, NULL))
    340      1.64      maya 		aprint_error_dev(self, "couldn't establish power handler\n");
    341      1.64      maya 
    342      1.47    dyoung 	return;
    343       1.1  augustss 
    344       1.1  augustss bad:
    345       1.1  augustss 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    346  1.67.4.1  christos 	sc->sc_dying = true;
    347      1.47    dyoung 	return;
    348       1.1  augustss }
    349       1.1  augustss 
    350  1.67.4.1  christos static void
    351      1.35    dyoung uftdi_childdet(device_t self, device_t child)
    352      1.35    dyoung {
    353      1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    354      1.35    dyoung 
    355      1.67        ws 	KASSERT(child == sc->sc_subdev);
    356      1.67        ws 	sc->sc_subdev = NULL;
    357      1.35    dyoung }
    358      1.35    dyoung 
    359  1.67.4.1  christos static int
    360      1.35    dyoung uftdi_detach(device_t self, int flags)
    361       1.1  augustss {
    362      1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    363  1.67.4.1  christos 	int rv = 0;
    364       1.1  augustss 
    365       1.1  augustss 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    366  1.67.4.1  christos 
    367  1.67.4.1  christos 	sc->sc_dying = true;
    368  1.67.4.1  christos 
    369  1.67.4.1  christos 	if (sc->sc_subdev != NULL) {
    370  1.67.4.1  christos 		rv = config_detach(sc->sc_subdev, flags);
    371  1.67.4.1  christos 		sc->sc_subdev = NULL;
    372  1.67.4.1  christos 	}
    373       1.5  augustss 
    374      1.62   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    375       1.5  augustss 
    376  1.67.4.1  christos 	return rv;
    377       1.1  augustss }
    378       1.1  augustss 
    379  1.67.4.1  christos static int
    380       1.2  augustss uftdi_open(void *vsc, int portno)
    381       1.1  augustss {
    382       1.1  augustss 	struct uftdi_softc *sc = vsc;
    383       1.1  augustss 	usb_device_request_t req;
    384       1.1  augustss 	usbd_status err;
    385       1.1  augustss 	struct termios t;
    386       1.1  augustss 
    387       1.1  augustss 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    388       1.1  augustss 
    389       1.1  augustss 	if (sc->sc_dying)
    390      1.61     skrll 		return EIO;
    391       1.1  augustss 
    392       1.1  augustss 	/* Perform a full reset on the device */
    393       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    394       1.1  augustss 	req.bRequest = FTDI_SIO_RESET;
    395       1.1  augustss 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    396       1.1  augustss 	USETW(req.wIndex, portno);
    397       1.1  augustss 	USETW(req.wLength, 0);
    398       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    399       1.1  augustss 	if (err)
    400      1.61     skrll 		return EIO;
    401       1.1  augustss 
    402       1.1  augustss 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    403       1.1  augustss 	t.c_ospeed = 9600;
    404       1.1  augustss 	t.c_cflag = CSTOPB | CS8;
    405       1.1  augustss 	(void)uftdi_param(sc, portno, &t);
    406       1.1  augustss 
    407       1.1  augustss 	/* Turn on RTS/CTS flow control */
    408       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    409       1.1  augustss 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    410       1.1  augustss 	USETW(req.wValue, 0);
    411       1.1  augustss 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    412       1.1  augustss 	USETW(req.wLength, 0);
    413       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    414       1.1  augustss 	if (err)
    415      1.61     skrll 		return EIO;
    416       1.1  augustss 
    417      1.61     skrll 	return 0;
    418       1.1  augustss }
    419       1.1  augustss 
    420  1.67.4.1  christos static void
    421      1.61     skrll uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
    422       1.1  augustss {
    423       1.1  augustss 	struct uftdi_softc *sc = vsc;
    424       1.1  augustss 	u_char msr, lsr;
    425       1.1  augustss 
    426       1.1  augustss 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    427       1.1  augustss 		     *count));
    428       1.1  augustss 
    429       1.1  augustss 	msr = FTDI_GET_MSR(*ptr);
    430       1.1  augustss 	lsr = FTDI_GET_LSR(*ptr);
    431       1.1  augustss 
    432       1.1  augustss #ifdef UFTDI_DEBUG
    433       1.1  augustss 	if (*count != 2)
    434       1.1  augustss 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    435       1.1  augustss 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    436       1.1  augustss #endif
    437       1.1  augustss 
    438       1.1  augustss 	if (sc->sc_msr != msr ||
    439       1.1  augustss 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    440       1.1  augustss 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    441       1.1  augustss 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    442       1.1  augustss 			 lsr, sc->sc_lsr));
    443       1.1  augustss 		sc->sc_msr = msr;
    444       1.1  augustss 		sc->sc_lsr = lsr;
    445      1.67        ws 		ucom_status_change(device_private(sc->sc_subdev));
    446       1.1  augustss 	}
    447       1.1  augustss 
    448      1.46       scw 	/* Adjust buffer pointer to skip status prefix */
    449       1.1  augustss 	*ptr += 2;
    450       1.1  augustss }
    451       1.1  augustss 
    452  1.67.4.1  christos static void
    453      1.61     skrll uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
    454       1.1  augustss {
    455      1.10       scw 	struct uftdi_softc *sc = vsc;
    456      1.10       scw 
    457       1.1  augustss 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    458       1.1  augustss 		     vsc, portno, *count, from[0]));
    459       1.1  augustss 
    460       1.1  augustss 	/* Make length tag and copy data */
    461      1.10       scw 	if (sc->sc_hdrlen > 0)
    462      1.10       scw 		*to = FTDI_OUT_TAG(*count, portno);
    463      1.10       scw 
    464      1.10       scw 	memcpy(to + sc->sc_hdrlen, from, *count);
    465      1.10       scw 	*count += sc->sc_hdrlen;
    466       1.1  augustss }
    467       1.1  augustss 
    468  1.67.4.1  christos static void
    469       1.2  augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
    470       1.1  augustss {
    471       1.1  augustss 	struct uftdi_softc *sc = vsc;
    472       1.1  augustss 	usb_device_request_t req;
    473       1.1  augustss 	int ctl;
    474       1.1  augustss 
    475       1.1  augustss 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    476       1.1  augustss 		 reg, onoff));
    477       1.1  augustss 
    478  1.67.4.1  christos 	if (sc->sc_dying)
    479  1.67.4.1  christos 		return;
    480  1.67.4.1  christos 
    481       1.1  augustss 	switch (reg) {
    482       1.1  augustss 	case UCOM_SET_DTR:
    483       1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    484       1.1  augustss 		break;
    485       1.1  augustss 	case UCOM_SET_RTS:
    486       1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    487       1.1  augustss 		break;
    488       1.1  augustss 	case UCOM_SET_BREAK:
    489       1.8    ichiro 		uftdi_break(sc, portno, onoff);
    490       1.1  augustss 		return;
    491       1.1  augustss 	default:
    492       1.1  augustss 		return;
    493       1.1  augustss 	}
    494       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    495       1.1  augustss 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    496       1.1  augustss 	USETW(req.wValue, ctl);
    497       1.1  augustss 	USETW(req.wIndex, portno);
    498       1.1  augustss 	USETW(req.wLength, 0);
    499       1.1  augustss 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    500       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    501       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    502       1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    503       1.1  augustss }
    504       1.1  augustss 
    505  1.67.4.3    martin /*
    506  1.67.4.3    martin  * Return true if the given speed is within operational tolerance of the target
    507  1.67.4.3    martin  * speed.  FTDI recommends that the hardware speed be within 3% of nominal.
    508  1.67.4.3    martin  */
    509  1.67.4.3    martin static inline bool
    510  1.67.4.3    martin uftdi_baud_within_tolerance(uint64_t speed, uint64_t target)
    511  1.67.4.3    martin {
    512  1.67.4.3    martin 	return ((speed >= (target * 100) / 103) &&
    513  1.67.4.3    martin 	    (speed <= (target * 100) / 97));
    514  1.67.4.3    martin }
    515  1.67.4.3    martin 
    516  1.67.4.3    martin static int
    517  1.67.4.3    martin uftdi_encode_baudrate(struct uftdi_softc *sc, int speed, int *rate, int *ratehi)
    518  1.67.4.3    martin {
    519  1.67.4.3    martin 	static const uint8_t encoded_fraction[8] = {
    520  1.67.4.3    martin 	    0, 3, 2, 4, 1, 5, 6, 7
    521  1.67.4.3    martin 	};
    522  1.67.4.3    martin 	static const uint8_t roundoff_232a[16] = {
    523  1.67.4.3    martin 	    0,  1,  0,  1,  0, -1,  2,  1,
    524  1.67.4.3    martin 	    0, -1, -2, -3,  4,  3,  2,  1,
    525  1.67.4.3    martin 	};
    526  1.67.4.3    martin 	uint32_t clk, divisor, fastclk_flag, frac, hwspeed;
    527  1.67.4.3    martin 
    528  1.67.4.3    martin 	/*
    529  1.67.4.3    martin 	 * If this chip has the fast clock capability and the speed is within
    530  1.67.4.3    martin 	 * range, use the 12MHz clock, otherwise the standard clock is 3MHz.
    531  1.67.4.3    martin 	 */
    532  1.67.4.3    martin 	if ((sc->sc_flags & FLAGS_BAUDCLK_12M) && speed >= 1200) {
    533  1.67.4.3    martin 		clk = 12000000;
    534  1.67.4.3    martin 		fastclk_flag = (1 << 17);
    535  1.67.4.3    martin 	} else {
    536  1.67.4.3    martin 		clk = 3000000;
    537  1.67.4.3    martin 		fastclk_flag = 0;
    538  1.67.4.3    martin 	}
    539  1.67.4.3    martin 
    540  1.67.4.3    martin 	/*
    541  1.67.4.3    martin 	 * Make sure the requested speed is reachable with the available clock
    542  1.67.4.3    martin 	 * and a 14-bit divisor.
    543  1.67.4.3    martin 	 */
    544  1.67.4.3    martin 	if (speed < (clk >> 14) || speed > clk)
    545  1.67.4.3    martin 		return -1;
    546  1.67.4.3    martin 
    547  1.67.4.3    martin 	/*
    548  1.67.4.3    martin 	 * Calculate the divisor, initially yielding a fixed point number with a
    549  1.67.4.3    martin 	 * 4-bit (1/16ths) fraction, then round it to the nearest fraction the
    550  1.67.4.3    martin 	 * hardware can handle.  When the integral part of the divisor is
    551  1.67.4.3    martin 	 * greater than one, the fractional part is in 1/8ths of the base clock.
    552  1.67.4.3    martin 	 * The FT8U232AM chips can handle only 0.125, 0.250, and 0.5 fractions.
    553  1.67.4.3    martin 	 * Later chips can handle all 1/8th fractions.
    554  1.67.4.3    martin 	 *
    555  1.67.4.3    martin 	 * If the integral part of the divisor is 1, a special rule applies: the
    556  1.67.4.3    martin 	 * fractional part can only be .0 or .5 (this is a limitation of the
    557  1.67.4.3    martin 	 * hardware).  We handle this by truncating the fraction rather than
    558  1.67.4.3    martin 	 * rounding, because this only applies to the two fastest speeds the
    559  1.67.4.3    martin 	 * chip can achieve and rounding doesn't matter, either you've asked for
    560  1.67.4.3    martin 	 * that exact speed or you've asked for something the chip can't do.
    561  1.67.4.3    martin 	 *
    562  1.67.4.3    martin 	 * For the FT8U232AM chips, use a roundoff table to adjust the result
    563  1.67.4.3    martin 	 * to the nearest 1/8th fraction that is supported by the hardware,
    564  1.67.4.3    martin 	 * leaving a fixed-point number with a 3-bit fraction which exactly
    565  1.67.4.3    martin 	 * represents the math the hardware divider will do.  For later-series
    566  1.67.4.3    martin 	 * chips that support all 8 fractional divisors, just round 16ths to
    567  1.67.4.3    martin 	 * 8ths by adding 1 and dividing by 2.
    568  1.67.4.3    martin 	 */
    569  1.67.4.3    martin 	divisor = (clk << 4) / speed;
    570  1.67.4.3    martin 	if ((divisor & 0xf) == 1)
    571  1.67.4.3    martin 		divisor &= 0xfffffff8;
    572  1.67.4.3    martin 	else if (sc->sc_flags & FLAGS_ROUNDOFF_232A)
    573  1.67.4.3    martin 		divisor += roundoff_232a[divisor & 0x0f];
    574  1.67.4.3    martin 	else
    575  1.67.4.3    martin 		divisor += 1;  /* Rounds odd 16ths up to next 8th. */
    576  1.67.4.3    martin 	divisor >>= 1;
    577  1.67.4.3    martin 
    578  1.67.4.3    martin 	/*
    579  1.67.4.3    martin 	 * Ensure the resulting hardware speed will be within operational
    580  1.67.4.3    martin 	 * tolerance (within 3% of nominal).
    581  1.67.4.3    martin 	 */
    582  1.67.4.3    martin 	hwspeed = (clk << 3) / divisor;
    583  1.67.4.3    martin 	if (!uftdi_baud_within_tolerance(hwspeed, speed))
    584  1.67.4.3    martin 		return -1;
    585  1.67.4.3    martin 
    586  1.67.4.3    martin 	/*
    587  1.67.4.3    martin 	 * Re-pack the divisor into hardware format. The lower 14-bits hold the
    588  1.67.4.3    martin 	 * integral part, while the upper bits specify the fraction by indexing
    589  1.67.4.3    martin 	 * a table of fractions within the hardware which is laid out as:
    590  1.67.4.3    martin 	 *    {0.0, 0.5, 0.25, 0.125, 0.325, 0.625, 0.725, 0.875}
    591  1.67.4.3    martin 	 * The A-series chips only have the first four table entries; the
    592  1.67.4.3    martin 	 * roundoff table logic above ensures that the fractional part for those
    593  1.67.4.3    martin 	 * chips will be one of the first four values.
    594  1.67.4.3    martin 	 *
    595  1.67.4.3    martin 	 * When the divisor is 1 a special encoding applies:  1.0 is encoded as
    596  1.67.4.3    martin 	 * 0.0, and 1.5 is encoded as 1.0.  The rounding logic above has already
    597  1.67.4.3    martin 	 * ensured that the fraction is either .0 or .5 if the integral is 1.
    598  1.67.4.3    martin 	 */
    599  1.67.4.3    martin 	frac = divisor & 0x07;
    600  1.67.4.3    martin 	divisor >>= 3;
    601  1.67.4.3    martin 	if (divisor == 1) {
    602  1.67.4.3    martin 		if (frac == 0)
    603  1.67.4.3    martin 			divisor = 0;	/* 1.0 becomes 0.0 */
    604  1.67.4.3    martin 		else
    605  1.67.4.3    martin 			frac = 0;	/* 1.5 becomes 1.0 */
    606  1.67.4.3    martin 	}
    607  1.67.4.3    martin 	divisor |= (encoded_fraction[frac] << 14) | fastclk_flag;
    608  1.67.4.3    martin 
    609  1.67.4.3    martin 	*rate = (uint16_t)divisor;
    610  1.67.4.3    martin 	*ratehi = (uint16_t)(divisor >> 16);
    611  1.67.4.3    martin 
    612  1.67.4.3    martin 	/*
    613  1.67.4.3    martin 	 * If this chip requires the baud bits to be in the high byte of the
    614  1.67.4.3    martin 	 * index word, move the bits up to that location.
    615  1.67.4.3    martin 	 */
    616  1.67.4.3    martin 	if (sc->sc_flags & FLAGS_BAUDBITS_HINDEX)
    617  1.67.4.3    martin 		*ratehi <<= 8;
    618  1.67.4.3    martin 
    619  1.67.4.3    martin 	return 0;
    620  1.67.4.3    martin }
    621  1.67.4.3    martin 
    622  1.67.4.1  christos static int
    623       1.2  augustss uftdi_param(void *vsc, int portno, struct termios *t)
    624       1.1  augustss {
    625       1.1  augustss 	struct uftdi_softc *sc = vsc;
    626       1.1  augustss 	usb_device_request_t req;
    627       1.1  augustss 	usbd_status err;
    628  1.67.4.3    martin 	int rate, ratehi, rerr, data, flow;
    629       1.1  augustss 
    630       1.1  augustss 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    631       1.1  augustss 
    632       1.1  augustss 	if (sc->sc_dying)
    633      1.61     skrll 		return EIO;
    634       1.1  augustss 
    635      1.56   mlelstv 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    636      1.56   mlelstv 	req.bRequest = FTDI_SIO_SET_BITMODE;
    637      1.56   mlelstv 	USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
    638      1.56   mlelstv 	USETW(req.wIndex, portno);
    639      1.56   mlelstv 	USETW(req.wLength, 0);
    640      1.56   mlelstv 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    641      1.56   mlelstv 	if (err)
    642      1.61     skrll 		return EIO;
    643      1.56   mlelstv 
    644      1.10       scw 	switch (sc->sc_type) {
    645      1.10       scw 	case UFTDI_TYPE_SIO:
    646      1.10       scw 		switch (t->c_ospeed) {
    647      1.10       scw 		case 300: rate = ftdi_sio_b300; break;
    648      1.10       scw 		case 600: rate = ftdi_sio_b600; break;
    649      1.10       scw 		case 1200: rate = ftdi_sio_b1200; break;
    650      1.10       scw 		case 2400: rate = ftdi_sio_b2400; break;
    651      1.10       scw 		case 4800: rate = ftdi_sio_b4800; break;
    652      1.10       scw 		case 9600: rate = ftdi_sio_b9600; break;
    653      1.10       scw 		case 19200: rate = ftdi_sio_b19200; break;
    654      1.10       scw 		case 38400: rate = ftdi_sio_b38400; break;
    655      1.10       scw 		case 57600: rate = ftdi_sio_b57600; break;
    656      1.10       scw 		case 115200: rate = ftdi_sio_b115200; break;
    657      1.10       scw 		default:
    658      1.61     skrll 			return EINVAL;
    659      1.10       scw 		}
    660  1.67.4.3    martin 		ratehi = 0;
    661      1.10       scw 		break;
    662      1.10       scw 	case UFTDI_TYPE_8U232AM:
    663  1.67.4.3    martin 		rerr = uftdi_encode_baudrate(sc, t->c_ospeed, &rate, &ratehi);
    664  1.67.4.3    martin 		if (rerr != 0)
    665      1.61     skrll 			return EINVAL;
    666      1.10       scw 		break;
    667      1.15  christos 	default:
    668      1.61     skrll 		return EINVAL;
    669       1.1  augustss 	}
    670       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    671       1.1  augustss 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    672       1.1  augustss 	USETW(req.wValue, rate);
    673  1.67.4.3    martin 	USETW(req.wIndex, portno | ratehi);
    674       1.1  augustss 	USETW(req.wLength, 0);
    675       1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    676       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    677       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    678       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    679       1.1  augustss 	if (err)
    680      1.61     skrll 		return EIO;
    681       1.1  augustss 
    682       1.1  augustss 	if (ISSET(t->c_cflag, CSTOPB))
    683       1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    684       1.1  augustss 	else
    685       1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    686       1.1  augustss 	if (ISSET(t->c_cflag, PARENB)) {
    687       1.1  augustss 		if (ISSET(t->c_cflag, PARODD))
    688       1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    689       1.1  augustss 		else
    690       1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    691       1.1  augustss 	} else
    692       1.1  augustss 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    693       1.1  augustss 	switch (ISSET(t->c_cflag, CSIZE)) {
    694       1.1  augustss 	case CS5:
    695       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(5);
    696       1.1  augustss 		break;
    697       1.1  augustss 	case CS6:
    698       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(6);
    699       1.1  augustss 		break;
    700       1.1  augustss 	case CS7:
    701       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(7);
    702       1.1  augustss 		break;
    703       1.1  augustss 	case CS8:
    704       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(8);
    705       1.1  augustss 		break;
    706       1.1  augustss 	}
    707       1.8    ichiro 	sc->last_lcr = data;
    708       1.8    ichiro 
    709       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    710       1.1  augustss 	req.bRequest = FTDI_SIO_SET_DATA;
    711       1.1  augustss 	USETW(req.wValue, data);
    712       1.1  augustss 	USETW(req.wIndex, portno);
    713       1.1  augustss 	USETW(req.wLength, 0);
    714       1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    715       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    716       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    717      1.12       scw 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    718      1.12       scw 	if (err)
    719      1.61     skrll 		return EIO;
    720      1.12       scw 
    721      1.12       scw 	if (ISSET(t->c_cflag, CRTSCTS)) {
    722      1.12       scw 		flow = FTDI_SIO_RTS_CTS_HS;
    723      1.12       scw 		USETW(req.wValue, 0);
    724      1.58   mlelstv 	} else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
    725      1.12       scw 		flow = FTDI_SIO_XON_XOFF_HS;
    726      1.12       scw 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    727      1.12       scw 	} else {
    728      1.12       scw 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    729      1.12       scw 		USETW(req.wValue, 0);
    730      1.12       scw 	}
    731      1.12       scw 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    732      1.12       scw 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    733      1.12       scw 	USETW2(req.wIndex, flow, portno);
    734      1.12       scw 	USETW(req.wLength, 0);
    735       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    736       1.1  augustss 	if (err)
    737      1.61     skrll 		return EIO;
    738       1.1  augustss 
    739      1.61     skrll 	return 0;
    740       1.1  augustss }
    741       1.1  augustss 
    742  1.67.4.1  christos static void
    743      1.29  christos uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    744       1.1  augustss {
    745       1.1  augustss 	struct uftdi_softc *sc = vsc;
    746       1.1  augustss 
    747       1.1  augustss 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    748       1.1  augustss 		 sc->sc_msr, sc->sc_lsr));
    749       1.1  augustss 
    750  1.67.4.1  christos 	if (sc->sc_dying)
    751  1.67.4.1  christos 		return;
    752  1.67.4.1  christos 
    753  1.67.4.1  christos 	*msr = sc->sc_msr;
    754  1.67.4.1  christos 	*lsr = sc->sc_lsr;
    755       1.8    ichiro }
    756       1.8    ichiro 
    757  1.67.4.1  christos static void
    758       1.8    ichiro uftdi_break(void *vsc, int portno, int onoff)
    759       1.8    ichiro {
    760       1.8    ichiro 	struct uftdi_softc *sc = vsc;
    761       1.9    ichiro 	usb_device_request_t req;
    762       1.8    ichiro 	int data;
    763       1.8    ichiro 
    764       1.8    ichiro 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    765      1.11  augustss 		  onoff));
    766       1.8    ichiro 
    767       1.8    ichiro 	if (onoff) {
    768       1.8    ichiro 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    769       1.8    ichiro 	} else {
    770       1.8    ichiro 		data = sc->last_lcr;
    771       1.8    ichiro 	}
    772       1.8    ichiro 
    773       1.8    ichiro 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    774       1.8    ichiro 	req.bRequest = FTDI_SIO_SET_DATA;
    775       1.8    ichiro 	USETW(req.wValue, data);
    776       1.8    ichiro 	USETW(req.wIndex, portno);
    777       1.8    ichiro 	USETW(req.wLength, 0);
    778       1.8    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    779       1.1  augustss }
    780