Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.59.6.11
      1  1.59.6.11     skrll /*	$NetBSD: uftdi.c,v 1.59.6.11 2016/12/05 10:55:18 skrll 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.59.6.11     skrll __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.59.6.11 2016/12/05 10:55:18 skrll Exp $");
     34  1.59.6.11     skrll 
     35  1.59.6.11     skrll #ifdef _KERNEL_OPT
     36  1.59.6.11     skrll #include "opt_usb.h"
     37  1.59.6.11     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.1  augustss #define UFTDI_CONFIG_INDEX	0
     66        1.1  augustss #define UFTDI_IFACE_INDEX	0
     67       1.41  nisimura #define UFTDI_MAX_PORTS		4
     68        1.1  augustss 
     69        1.1  augustss /*
     70       1.53  riastrad  * These are the default number of bytes transferred per frame if the
     71       1.53  riastrad  * endpoint doesn't tell us.  The output buffer size is a hard limit
     72       1.53  riastrad  * for devices that use a 6-bit size encoding.
     73        1.1  augustss  */
     74        1.1  augustss #define UFTDIIBUFSIZE 64
     75        1.1  augustss #define UFTDIOBUFSIZE 64
     76        1.1  augustss 
     77       1.53  riastrad /*
     78       1.53  riastrad  * Magic constants!  Where do these come from?  They're what Linux uses...
     79       1.53  riastrad  */
     80       1.53  riastrad #define	UFTDI_MAX_IBUFSIZE	512
     81       1.53  riastrad #define	UFTDI_MAX_OBUFSIZE	256
     82       1.53  riastrad 
     83        1.1  augustss struct uftdi_softc {
     84       1.47    dyoung 	device_t		sc_dev;		/* base device */
     85   1.59.6.6     skrll 	struct usbd_device *	sc_udev;	/* device */
     86   1.59.6.6     skrll 	struct usbd_interface *	sc_iface[UFTDI_MAX_PORTS];	/* interface */
     87        1.1  augustss 
     88       1.10       scw 	enum uftdi_type		sc_type;
     89       1.10       scw 	u_int			sc_hdrlen;
     90       1.32       riz 	u_int			sc_numports;
     91       1.41  nisimura 	u_int			sc_chiptype;
     92       1.10       scw 
     93        1.1  augustss 	u_char			sc_msr;
     94        1.1  augustss 	u_char			sc_lsr;
     95        1.1  augustss 
     96       1.47    dyoung 	device_t		sc_subdev[UFTDI_MAX_PORTS];
     97        1.1  augustss 
     98        1.1  augustss 	u_char			sc_dying;
     99        1.8    ichiro 
    100        1.8    ichiro 	u_int			last_lcr;
    101       1.41  nisimura 
    102        1.1  augustss };
    103        1.1  augustss 
    104   1.59.6.4     skrll Static void	uftdi_get_status(void *, int, u_char *, u_char *);
    105        1.2  augustss Static void	uftdi_set(void *, int, int, int);
    106        1.2  augustss Static int	uftdi_param(void *, int, struct termios *);
    107   1.59.6.4     skrll Static int	uftdi_open(void *, int);
    108   1.59.6.4     skrll Static void	uftdi_read(void *, int, u_char **, uint32_t *);
    109   1.59.6.4     skrll Static void	uftdi_write(void *, int, u_char *, u_char *,
    110   1.59.6.4     skrll 			    uint32_t *);
    111   1.59.6.4     skrll Static void	uftdi_break(void *, int, int);
    112        1.1  augustss 
    113        1.1  augustss struct ucom_methods uftdi_methods = {
    114   1.59.6.5     skrll 	.ucom_get_status = uftdi_get_status,
    115   1.59.6.5     skrll 	.ucom_set = uftdi_set,
    116   1.59.6.5     skrll 	.ucom_param = uftdi_param,
    117   1.59.6.5     skrll 	.ucom_ioctl = NULL,
    118   1.59.6.5     skrll 	.ucom_open = uftdi_open,
    119   1.59.6.5     skrll 	.ucom_close = NULL,
    120   1.59.6.5     skrll 	.ucom_read = uftdi_read,
    121   1.59.6.5     skrll 	.ucom_write = uftdi_write,
    122        1.1  augustss };
    123        1.1  augustss 
    124   1.59.6.1     skrll /*
    125       1.26  christos  * The devices default to UFTDI_TYPE_8U232AM.
    126       1.47    dyoung  * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
    127       1.26  christos  */
    128       1.26  christos static const struct usb_devno uftdi_devs[] = {
    129       1.26  christos 	{ USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
    130       1.26  christos 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
    131       1.26  christos 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
    132       1.59   msaitoh 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X },
    133       1.55   reinoud 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
    134       1.56   mlelstv 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL },
    135       1.30       riz 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
    136       1.42  nisimura 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
    137       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
    138       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
    139       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
    140       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
    141       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
    142       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
    143       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
    144       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
    145       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
    146       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
    147       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
    148       1.45    martin 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
    149       1.45    martin 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
    150       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
    151       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
    152       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
    153       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
    154       1.27   xtraeme 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
    155       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
    156       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
    157       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
    158       1.26  christos 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
    159       1.27   xtraeme 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
    160       1.52      matt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
    161       1.57   cheusov 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE },
    162       1.54  jakllsch 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
    163       1.48      matt 	{ USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
    164       1.26  christos 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
    165       1.26  christos 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
    166   1.59.6.8     skrll 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
    167       1.40      taca 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
    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.38      cube int uftdi_match(device_t, cfdata_t, void *);
    180       1.35    dyoung void uftdi_attach(device_t, device_t, void *);
    181       1.35    dyoung void uftdi_childdet(device_t, device_t);
    182       1.35    dyoung int uftdi_detach(device_t, int);
    183       1.35    dyoung int uftdi_activate(device_t, enum devact);
    184       1.35    dyoung extern struct cfdriver uftdi_cd;
    185       1.38      cube CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
    186       1.35    dyoung     uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
    187        1.1  augustss 
    188   1.59.6.1     skrll int
    189       1.47    dyoung uftdi_match(device_t parent, cfdata_t match, void *aux)
    190        1.1  augustss {
    191       1.47    dyoung 	struct usb_attach_arg *uaa = aux;
    192       1.11  augustss 
    193        1.1  augustss 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
    194   1.59.6.7     skrll 		     uaa->uaa_vendor, uaa->uaa_product));
    195        1.1  augustss 
    196   1.59.6.7     skrll 	return uftdi_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    197   1.59.6.3     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    198        1.1  augustss }
    199        1.1  augustss 
    200   1.59.6.1     skrll void
    201       1.47    dyoung uftdi_attach(device_t parent, device_t self, void *aux)
    202        1.1  augustss {
    203       1.47    dyoung 	struct uftdi_softc *sc = device_private(self);
    204       1.47    dyoung 	struct usb_attach_arg *uaa = aux;
    205   1.59.6.7     skrll 	struct usbd_device *dev = uaa->uaa_device;
    206   1.59.6.6     skrll 	struct usbd_interface *iface;
    207       1.41  nisimura 	usb_device_descriptor_t *ddesc;
    208        1.1  augustss 	usb_interface_descriptor_t *id;
    209        1.1  augustss 	usb_endpoint_descriptor_t *ed;
    210       1.24  augustss 	char *devinfop;
    211       1.38      cube 	const char *devname = device_xname(self);
    212       1.32       riz 	int i,idx;
    213        1.1  augustss 	usbd_status err;
    214   1.59.6.9     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.1  augustss 	/* Move the device into the configured state. */
    226        1.1  augustss 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
    227        1.1  augustss 	if (err) {
    228       1.38      cube 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    229        1.1  augustss 		       devname, usbd_errstr(err));
    230        1.1  augustss 		goto bad;
    231        1.1  augustss 	}
    232        1.1  augustss 
    233       1.38      cube 	sc->sc_dev = self;
    234        1.1  augustss 	sc->sc_udev = dev;
    235       1.32       riz 	sc->sc_numports = 1;
    236       1.41  nisimura 	sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
    237       1.41  nisimura 	sc->sc_hdrlen = 0;
    238   1.59.6.7     skrll 	if (uaa->uaa_vendor == USB_VENDOR_FTDI
    239   1.59.6.7     skrll 	    && uaa->uaa_product == USB_PRODUCT_FTDI_SERIAL_8U100AX) {
    240       1.41  nisimura 		sc->sc_type = UFTDI_TYPE_SIO;
    241       1.41  nisimura 		sc->sc_hdrlen = 1;
    242       1.41  nisimura 	}
    243       1.41  nisimura 
    244       1.41  nisimura 	ddesc = usbd_get_device_descriptor(dev);
    245       1.41  nisimura 	sc->sc_chiptype = UGETW(ddesc->bcdDevice);
    246       1.41  nisimura 	switch (sc->sc_chiptype) {
    247       1.41  nisimura 	case 0x500: /* 2232D */
    248       1.41  nisimura 	case 0x700: /* 2232H */
    249       1.41  nisimura 		sc->sc_numports = 2;
    250       1.41  nisimura 		break;
    251       1.41  nisimura 	case 0x800: /* 4232H */
    252       1.41  nisimura 		sc->sc_numports = 4;
    253       1.41  nisimura 		break;
    254       1.41  nisimura 	case 0x200: /* 232/245AM */
    255       1.41  nisimura 	case 0x400: /* 232/245BL */
    256       1.41  nisimura 	case 0x600: /* 232/245R */
    257       1.41  nisimura 	default:
    258       1.10       scw 		break;
    259       1.10       scw 	}
    260       1.10       scw 
    261       1.32       riz 	for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) {
    262       1.32       riz 		err = usbd_device2interface_handle(dev, idx, &iface);
    263       1.32       riz 		if (err) {
    264       1.38      cube 			aprint_error(
    265       1.38      cube 			    "\n%s: failed to get interface idx=%d, err=%s\n",
    266       1.38      cube 			    devname, idx, usbd_errstr(err));
    267        1.1  augustss 			goto bad;
    268        1.1  augustss 		}
    269       1.11  augustss 
    270       1.32       riz 		id = usbd_get_interface_descriptor(iface);
    271       1.32       riz 
    272       1.32       riz 		sc->sc_iface[idx] = iface;
    273       1.32       riz 
    274   1.59.6.9     skrll 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    275   1.59.6.9     skrll 		ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0;
    276       1.32       riz 		for (i = 0; i < id->bNumEndpoints; i++) {
    277       1.32       riz 			int addr, dir, attr;
    278       1.32       riz 			ed = usbd_interface2endpoint_descriptor(iface, i);
    279       1.32       riz 			if (ed == NULL) {
    280       1.38      cube 				aprint_error_dev(self,
    281       1.38      cube 				    "could not read endpoint descriptor: %s\n",
    282       1.38      cube 				    usbd_errstr(err));
    283       1.32       riz 				goto bad;
    284       1.32       riz 			}
    285       1.32       riz 
    286       1.32       riz 			addr = ed->bEndpointAddress;
    287       1.32       riz 			dir = UE_GET_DIR(ed->bEndpointAddress);
    288       1.32       riz 			attr = ed->bmAttributes & UE_XFERTYPE;
    289       1.53  riastrad 			if (dir == UE_DIR_IN && attr == UE_BULK) {
    290   1.59.6.9     skrll 				ucaa.ucaa_bulkin = addr;
    291   1.59.6.9     skrll 				ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
    292   1.59.6.9     skrll 				if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
    293   1.59.6.9     skrll 					ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
    294       1.53  riastrad 			} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    295   1.59.6.9     skrll 				ucaa.ucaa_bulkout = addr;
    296   1.59.6.9     skrll 				ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
    297       1.53  riastrad 				    - sc->sc_hdrlen;
    298   1.59.6.9     skrll 				if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
    299   1.59.6.9     skrll 					ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE;
    300       1.53  riastrad 				/* Limit length if we have a 6-bit header.  */
    301       1.53  riastrad 				if ((sc->sc_hdrlen > 0) &&
    302   1.59.6.9     skrll 				    (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
    303   1.59.6.9     skrll 					ucaa.ucaa_obufsize = UFTDIOBUFSIZE;
    304       1.53  riastrad 			} else {
    305       1.38      cube 				aprint_error_dev(self,
    306       1.38      cube 				    "unexpected endpoint\n");
    307       1.32       riz 				goto bad;
    308       1.32       riz 			}
    309       1.32       riz 		}
    310   1.59.6.9     skrll 		if (ucaa.ucaa_bulkin == -1) {
    311       1.38      cube 			aprint_error_dev(self,
    312       1.38      cube 			    "Could not find data bulk in\n");
    313        1.1  augustss 			goto bad;
    314        1.1  augustss 		}
    315   1.59.6.9     skrll 		if (ucaa.ucaa_bulkout == -1) {
    316       1.38      cube 			aprint_error_dev(self,
    317       1.38      cube 			    "Could not find data bulk out\n");
    318       1.32       riz 			goto bad;
    319       1.32       riz 		}
    320       1.32       riz 
    321   1.59.6.9     skrll 		ucaa.ucaa_portno = FTDI_PIT_SIOA + idx;
    322   1.59.6.9     skrll 		/* ucaa_bulkin, ucaa_bulkout set above */
    323   1.59.6.9     skrll 		if (ucaa.ucaa_ibufsize == 0)
    324   1.59.6.9     skrll 			ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
    325   1.59.6.9     skrll 		ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
    326   1.59.6.9     skrll 		if (ucaa.ucaa_obufsize == 0)
    327   1.59.6.9     skrll 			ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    328   1.59.6.9     skrll 		ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
    329   1.59.6.9     skrll 		ucaa.ucaa_device = dev;
    330   1.59.6.9     skrll 		ucaa.ucaa_iface = iface;
    331   1.59.6.9     skrll 		ucaa.ucaa_methods = &uftdi_methods;
    332   1.59.6.9     skrll 		ucaa.ucaa_arg = sc;
    333   1.59.6.9     skrll 		ucaa.ucaa_info = NULL;
    334       1.32       riz 
    335       1.53  riastrad 		DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n",
    336   1.59.6.9     skrll 			ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
    337   1.59.6.9     skrll 			ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
    338       1.41  nisimura 		sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL,
    339   1.59.6.9     skrll 		    &ucaa, ucomprint, ucomsubmatch);
    340        1.1  augustss 	}
    341        1.1  augustss 
    342  1.59.6.10     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    343        1.5  augustss 
    344       1.47    dyoung 	return;
    345        1.1  augustss 
    346        1.1  augustss bad:
    347        1.1  augustss 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    348        1.1  augustss 	sc->sc_dying = 1;
    349       1.47    dyoung 	return;
    350        1.1  augustss }
    351        1.1  augustss 
    352        1.1  augustss int
    353       1.38      cube uftdi_activate(device_t self, enum devact act)
    354        1.1  augustss {
    355       1.38      cube 	struct uftdi_softc *sc = device_private(self);
    356        1.1  augustss 
    357        1.1  augustss 	switch (act) {
    358        1.1  augustss 	case DVACT_DEACTIVATE:
    359        1.1  augustss 		sc->sc_dying = 1;
    360       1.44    dyoung 		return 0;
    361       1.44    dyoung 	default:
    362       1.44    dyoung 		return EOPNOTSUPP;
    363        1.1  augustss 	}
    364        1.1  augustss }
    365        1.1  augustss 
    366       1.35    dyoung void
    367       1.35    dyoung uftdi_childdet(device_t self, device_t child)
    368       1.35    dyoung {
    369       1.35    dyoung 	int i;
    370       1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    371       1.35    dyoung 
    372       1.35    dyoung 	for (i = 0; i < sc->sc_numports; i++) {
    373       1.35    dyoung 		if (sc->sc_subdev[i] == child)
    374       1.35    dyoung 			break;
    375       1.35    dyoung 	}
    376       1.35    dyoung 	KASSERT(i < sc->sc_numports);
    377       1.35    dyoung 	sc->sc_subdev[i] = NULL;
    378       1.35    dyoung }
    379       1.35    dyoung 
    380        1.1  augustss int
    381       1.35    dyoung uftdi_detach(device_t self, int flags)
    382        1.1  augustss {
    383       1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    384       1.32       riz 	int i;
    385        1.1  augustss 
    386        1.1  augustss 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    387        1.1  augustss 	sc->sc_dying = 1;
    388       1.35    dyoung 	for (i=0; i < sc->sc_numports; i++) {
    389       1.35    dyoung 		if (sc->sc_subdev[i] != NULL)
    390       1.32       riz 			config_detach(sc->sc_subdev[i], flags);
    391       1.35    dyoung 	}
    392        1.5  augustss 
    393  1.59.6.10     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    394        1.5  augustss 
    395   1.59.6.3     skrll 	return 0;
    396        1.1  augustss }
    397        1.1  augustss 
    398        1.1  augustss Static int
    399        1.2  augustss uftdi_open(void *vsc, int portno)
    400        1.1  augustss {
    401        1.1  augustss 	struct uftdi_softc *sc = vsc;
    402        1.1  augustss 	usb_device_request_t req;
    403        1.1  augustss 	usbd_status err;
    404        1.1  augustss 	struct termios t;
    405        1.1  augustss 
    406        1.1  augustss 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    407        1.1  augustss 
    408        1.1  augustss 	if (sc->sc_dying)
    409   1.59.6.3     skrll 		return EIO;
    410        1.1  augustss 
    411        1.1  augustss 	/* Perform a full reset on the device */
    412        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    413        1.1  augustss 	req.bRequest = FTDI_SIO_RESET;
    414        1.1  augustss 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    415        1.1  augustss 	USETW(req.wIndex, portno);
    416        1.1  augustss 	USETW(req.wLength, 0);
    417        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    418        1.1  augustss 	if (err)
    419   1.59.6.3     skrll 		return EIO;
    420        1.1  augustss 
    421        1.1  augustss 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    422        1.1  augustss 	t.c_ospeed = 9600;
    423        1.1  augustss 	t.c_cflag = CSTOPB | CS8;
    424        1.1  augustss 	(void)uftdi_param(sc, portno, &t);
    425        1.1  augustss 
    426        1.1  augustss 	/* Turn on RTS/CTS flow control */
    427        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    428        1.1  augustss 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    429        1.1  augustss 	USETW(req.wValue, 0);
    430        1.1  augustss 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    431        1.1  augustss 	USETW(req.wLength, 0);
    432        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    433        1.1  augustss 	if (err)
    434   1.59.6.3     skrll 		return EIO;
    435        1.1  augustss 
    436   1.59.6.3     skrll 	return 0;
    437        1.1  augustss }
    438        1.1  augustss 
    439        1.1  augustss Static void
    440   1.59.6.1     skrll uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
    441        1.1  augustss {
    442        1.1  augustss 	struct uftdi_softc *sc = vsc;
    443        1.1  augustss 	u_char msr, lsr;
    444        1.1  augustss 
    445        1.1  augustss 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    446        1.1  augustss 		     *count));
    447        1.1  augustss 
    448        1.1  augustss 	msr = FTDI_GET_MSR(*ptr);
    449        1.1  augustss 	lsr = FTDI_GET_LSR(*ptr);
    450        1.1  augustss 
    451        1.1  augustss #ifdef UFTDI_DEBUG
    452        1.1  augustss 	if (*count != 2)
    453        1.1  augustss 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    454        1.1  augustss 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    455        1.1  augustss #endif
    456        1.1  augustss 
    457        1.1  augustss 	if (sc->sc_msr != msr ||
    458        1.1  augustss 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    459        1.1  augustss 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    460        1.1  augustss 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    461        1.1  augustss 			 lsr, sc->sc_lsr));
    462        1.1  augustss 		sc->sc_msr = msr;
    463        1.1  augustss 		sc->sc_lsr = lsr;
    464       1.38      cube 		ucom_status_change(device_private(sc->sc_subdev[portno-1]));
    465        1.1  augustss 	}
    466        1.1  augustss 
    467       1.46       scw 	/* Adjust buffer pointer to skip status prefix */
    468        1.1  augustss 	*ptr += 2;
    469        1.1  augustss }
    470        1.1  augustss 
    471        1.1  augustss Static void
    472   1.59.6.1     skrll uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
    473        1.1  augustss {
    474       1.10       scw 	struct uftdi_softc *sc = vsc;
    475       1.10       scw 
    476        1.1  augustss 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    477        1.1  augustss 		     vsc, portno, *count, from[0]));
    478        1.1  augustss 
    479        1.1  augustss 	/* Make length tag and copy data */
    480       1.10       scw 	if (sc->sc_hdrlen > 0)
    481       1.10       scw 		*to = FTDI_OUT_TAG(*count, portno);
    482       1.10       scw 
    483       1.10       scw 	memcpy(to + sc->sc_hdrlen, from, *count);
    484       1.10       scw 	*count += sc->sc_hdrlen;
    485        1.1  augustss }
    486        1.1  augustss 
    487        1.1  augustss Static void
    488        1.2  augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
    489        1.1  augustss {
    490        1.1  augustss 	struct uftdi_softc *sc = vsc;
    491        1.1  augustss 	usb_device_request_t req;
    492        1.1  augustss 	int ctl;
    493        1.1  augustss 
    494        1.1  augustss 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    495        1.1  augustss 		 reg, onoff));
    496        1.1  augustss 
    497        1.1  augustss 	switch (reg) {
    498        1.1  augustss 	case UCOM_SET_DTR:
    499        1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    500        1.1  augustss 		break;
    501        1.1  augustss 	case UCOM_SET_RTS:
    502        1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    503        1.1  augustss 		break;
    504        1.1  augustss 	case UCOM_SET_BREAK:
    505        1.8    ichiro 		uftdi_break(sc, portno, onoff);
    506        1.1  augustss 		return;
    507        1.1  augustss 	default:
    508        1.1  augustss 		return;
    509        1.1  augustss 	}
    510        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    511        1.1  augustss 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    512        1.1  augustss 	USETW(req.wValue, ctl);
    513        1.1  augustss 	USETW(req.wIndex, portno);
    514        1.1  augustss 	USETW(req.wLength, 0);
    515        1.1  augustss 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    516        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    517        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    518        1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    519        1.1  augustss }
    520        1.1  augustss 
    521        1.1  augustss Static int
    522        1.2  augustss uftdi_param(void *vsc, int portno, struct termios *t)
    523        1.1  augustss {
    524        1.1  augustss 	struct uftdi_softc *sc = vsc;
    525        1.1  augustss 	usb_device_request_t req;
    526        1.1  augustss 	usbd_status err;
    527       1.12       scw 	int rate, data, flow;
    528        1.1  augustss 
    529        1.1  augustss 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    530        1.1  augustss 
    531        1.1  augustss 	if (sc->sc_dying)
    532   1.59.6.3     skrll 		return EIO;
    533        1.1  augustss 
    534       1.56   mlelstv 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    535       1.56   mlelstv 	req.bRequest = FTDI_SIO_SET_BITMODE;
    536       1.56   mlelstv 	USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
    537       1.56   mlelstv 	USETW(req.wIndex, portno);
    538       1.56   mlelstv 	USETW(req.wLength, 0);
    539       1.56   mlelstv 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    540       1.56   mlelstv 	if (err)
    541   1.59.6.3     skrll 		return EIO;
    542       1.56   mlelstv 
    543       1.10       scw 	switch (sc->sc_type) {
    544       1.10       scw 	case UFTDI_TYPE_SIO:
    545       1.10       scw 		switch (t->c_ospeed) {
    546       1.10       scw 		case 300: rate = ftdi_sio_b300; break;
    547       1.10       scw 		case 600: rate = ftdi_sio_b600; break;
    548       1.10       scw 		case 1200: rate = ftdi_sio_b1200; break;
    549       1.10       scw 		case 2400: rate = ftdi_sio_b2400; break;
    550       1.10       scw 		case 4800: rate = ftdi_sio_b4800; break;
    551       1.10       scw 		case 9600: rate = ftdi_sio_b9600; break;
    552       1.10       scw 		case 19200: rate = ftdi_sio_b19200; break;
    553       1.10       scw 		case 38400: rate = ftdi_sio_b38400; break;
    554       1.10       scw 		case 57600: rate = ftdi_sio_b57600; break;
    555       1.10       scw 		case 115200: rate = ftdi_sio_b115200; break;
    556       1.10       scw 		default:
    557   1.59.6.3     skrll 			return EINVAL;
    558       1.10       scw 		}
    559       1.10       scw 		break;
    560       1.10       scw 
    561       1.10       scw 	case UFTDI_TYPE_8U232AM:
    562       1.10       scw 		switch(t->c_ospeed) {
    563       1.10       scw 		case 300: rate = ftdi_8u232am_b300; break;
    564       1.10       scw 		case 600: rate = ftdi_8u232am_b600; break;
    565       1.10       scw 		case 1200: rate = ftdi_8u232am_b1200; break;
    566       1.10       scw 		case 2400: rate = ftdi_8u232am_b2400; break;
    567       1.10       scw 		case 4800: rate = ftdi_8u232am_b4800; break;
    568       1.10       scw 		case 9600: rate = ftdi_8u232am_b9600; break;
    569       1.10       scw 		case 19200: rate = ftdi_8u232am_b19200; break;
    570       1.10       scw 		case 38400: rate = ftdi_8u232am_b38400; break;
    571       1.10       scw 		case 57600: rate = ftdi_8u232am_b57600; break;
    572       1.10       scw 		case 115200: rate = ftdi_8u232am_b115200; break;
    573       1.10       scw 		case 230400: rate = ftdi_8u232am_b230400; break;
    574       1.10       scw 		case 460800: rate = ftdi_8u232am_b460800; break;
    575       1.10       scw 		case 921600: rate = ftdi_8u232am_b921600; break;
    576       1.10       scw 		default:
    577   1.59.6.3     skrll 			return EINVAL;
    578       1.10       scw 		}
    579       1.10       scw 		break;
    580       1.16   mycroft 
    581       1.15  christos 	default:
    582   1.59.6.3     skrll 		return EINVAL;
    583        1.1  augustss 	}
    584        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    585        1.1  augustss 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    586        1.1  augustss 	USETW(req.wValue, rate);
    587        1.1  augustss 	USETW(req.wIndex, portno);
    588        1.1  augustss 	USETW(req.wLength, 0);
    589        1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    590        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    591        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    592        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    593        1.1  augustss 	if (err)
    594   1.59.6.3     skrll 		return EIO;
    595        1.1  augustss 
    596        1.1  augustss 	if (ISSET(t->c_cflag, CSTOPB))
    597        1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    598        1.1  augustss 	else
    599        1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    600        1.1  augustss 	if (ISSET(t->c_cflag, PARENB)) {
    601        1.1  augustss 		if (ISSET(t->c_cflag, PARODD))
    602        1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    603        1.1  augustss 		else
    604        1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    605        1.1  augustss 	} else
    606        1.1  augustss 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    607        1.1  augustss 	switch (ISSET(t->c_cflag, CSIZE)) {
    608        1.1  augustss 	case CS5:
    609        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(5);
    610        1.1  augustss 		break;
    611        1.1  augustss 	case CS6:
    612        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(6);
    613        1.1  augustss 		break;
    614        1.1  augustss 	case CS7:
    615        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(7);
    616        1.1  augustss 		break;
    617        1.1  augustss 	case CS8:
    618        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(8);
    619        1.1  augustss 		break;
    620        1.1  augustss 	}
    621        1.8    ichiro 	sc->last_lcr = data;
    622        1.8    ichiro 
    623        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    624        1.1  augustss 	req.bRequest = FTDI_SIO_SET_DATA;
    625        1.1  augustss 	USETW(req.wValue, data);
    626        1.1  augustss 	USETW(req.wIndex, portno);
    627        1.1  augustss 	USETW(req.wLength, 0);
    628        1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    629        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    630        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    631       1.12       scw 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    632       1.12       scw 	if (err)
    633   1.59.6.3     skrll 		return EIO;
    634       1.12       scw 
    635       1.12       scw 	if (ISSET(t->c_cflag, CRTSCTS)) {
    636       1.12       scw 		flow = FTDI_SIO_RTS_CTS_HS;
    637       1.12       scw 		USETW(req.wValue, 0);
    638       1.58   mlelstv 	} else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
    639       1.12       scw 		flow = FTDI_SIO_XON_XOFF_HS;
    640       1.12       scw 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    641       1.12       scw 	} else {
    642       1.12       scw 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    643       1.12       scw 		USETW(req.wValue, 0);
    644       1.12       scw 	}
    645       1.12       scw 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    646       1.12       scw 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    647       1.12       scw 	USETW2(req.wIndex, flow, portno);
    648       1.12       scw 	USETW(req.wLength, 0);
    649        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    650        1.1  augustss 	if (err)
    651   1.59.6.3     skrll 		return EIO;
    652        1.1  augustss 
    653   1.59.6.3     skrll 	return 0;
    654        1.1  augustss }
    655        1.1  augustss 
    656        1.1  augustss void
    657       1.29  christos uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    658        1.1  augustss {
    659        1.1  augustss 	struct uftdi_softc *sc = vsc;
    660        1.1  augustss 
    661        1.1  augustss 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    662        1.1  augustss 		 sc->sc_msr, sc->sc_lsr));
    663        1.1  augustss 
    664        1.1  augustss 	if (msr != NULL)
    665        1.1  augustss 		*msr = sc->sc_msr;
    666        1.1  augustss 	if (lsr != NULL)
    667        1.1  augustss 		*lsr = sc->sc_lsr;
    668        1.8    ichiro }
    669        1.8    ichiro 
    670        1.8    ichiro void
    671        1.8    ichiro uftdi_break(void *vsc, int portno, int onoff)
    672        1.8    ichiro {
    673        1.8    ichiro 	struct uftdi_softc *sc = vsc;
    674        1.9    ichiro 	usb_device_request_t req;
    675        1.8    ichiro 	int data;
    676        1.8    ichiro 
    677        1.8    ichiro 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    678       1.11  augustss 		  onoff));
    679        1.8    ichiro 
    680        1.8    ichiro 	if (onoff) {
    681        1.8    ichiro 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    682        1.8    ichiro 	} else {
    683        1.8    ichiro 		data = sc->last_lcr;
    684        1.8    ichiro 	}
    685        1.8    ichiro 
    686        1.8    ichiro 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    687        1.8    ichiro 	req.bRequest = FTDI_SIO_SET_DATA;
    688        1.8    ichiro 	USETW(req.wValue, data);
    689        1.8    ichiro 	USETW(req.wIndex, portno);
    690        1.8    ichiro 	USETW(req.wLength, 0);
    691        1.8    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    692        1.1  augustss }
    693