Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.64
      1  1.64      maya /*	$NetBSD: uftdi.c,v 1.64 2016/12/15 17:21:21 maya 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.64      maya __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.64 2016/12/15 17:21:21 maya 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.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.61     skrll 	struct usbd_device *	sc_udev;	/* device */
     86  1.61     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.61     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.61     skrll Static int	uftdi_open(void *, int);
    108  1.61     skrll Static void	uftdi_read(void *, int, u_char **, uint32_t *);
    109  1.61     skrll Static void	uftdi_write(void *, int, u_char *, u_char *,
    110  1.61     skrll 			    uint32_t *);
    111  1.61     skrll Static void	uftdi_break(void *, int, int);
    112   1.1  augustss 
    113   1.1  augustss 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_ioctl = NULL,
    118  1.61     skrll 	.ucom_open = uftdi_open,
    119  1.61     skrll 	.ucom_close = NULL,
    120  1.61     skrll 	.ucom_read = uftdi_read,
    121  1.61     skrll 	.ucom_write = uftdi_write,
    122   1.1  augustss };
    123   1.1  augustss 
    124  1.61     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.60    nonaka 	{ 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.61     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.61     skrll 		     uaa->uaa_vendor, uaa->uaa_product));
    195   1.1  augustss 
    196  1.61     skrll 	return uftdi_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    197  1.61     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    198   1.1  augustss }
    199   1.1  augustss 
    200  1.61     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.61     skrll 	struct usbd_device *dev = uaa->uaa_device;
    206  1.61     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.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.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.61     skrll 	if (uaa->uaa_vendor == USB_VENDOR_FTDI
    239  1.61     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.61     skrll 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    275  1.61     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.61     skrll 				ucaa.ucaa_bulkin = addr;
    291  1.61     skrll 				ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
    292  1.61     skrll 				if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
    293  1.61     skrll 					ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
    294  1.53  riastrad 			} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    295  1.61     skrll 				ucaa.ucaa_bulkout = addr;
    296  1.61     skrll 				ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
    297  1.53  riastrad 				    - sc->sc_hdrlen;
    298  1.61     skrll 				if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
    299  1.61     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.61     skrll 				    (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
    303  1.61     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.61     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.61     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.61     skrll 		ucaa.ucaa_portno = FTDI_PIT_SIOA + idx;
    322  1.61     skrll 		/* ucaa_bulkin, ucaa_bulkout set above */
    323  1.61     skrll 		if (ucaa.ucaa_ibufsize == 0)
    324  1.61     skrll 			ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
    325  1.61     skrll 		ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
    326  1.61     skrll 		if (ucaa.ucaa_obufsize == 0)
    327  1.61     skrll 			ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    328  1.61     skrll 		ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
    329  1.61     skrll 		ucaa.ucaa_device = dev;
    330  1.61     skrll 		ucaa.ucaa_iface = iface;
    331  1.61     skrll 		ucaa.ucaa_methods = &uftdi_methods;
    332  1.61     skrll 		ucaa.ucaa_arg = sc;
    333  1.61     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.61     skrll 			ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
    337  1.61     skrll 			ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
    338  1.41  nisimura 		sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL,
    339  1.61     skrll 		    &ucaa, ucomprint, ucomsubmatch);
    340   1.1  augustss 	}
    341   1.1  augustss 
    342  1.62   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    343   1.5  augustss 
    344  1.64      maya 	if (!pmf_device_register(self, NULL, NULL))
    345  1.64      maya 		aprint_error_dev(self, "couldn't establish power handler\n");
    346  1.64      maya 
    347  1.47    dyoung 	return;
    348   1.1  augustss 
    349   1.1  augustss bad:
    350   1.1  augustss 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    351   1.1  augustss 	sc->sc_dying = 1;
    352  1.47    dyoung 	return;
    353   1.1  augustss }
    354   1.1  augustss 
    355   1.1  augustss int
    356  1.38      cube uftdi_activate(device_t self, enum devact act)
    357   1.1  augustss {
    358  1.38      cube 	struct uftdi_softc *sc = device_private(self);
    359   1.1  augustss 
    360   1.1  augustss 	switch (act) {
    361   1.1  augustss 	case DVACT_DEACTIVATE:
    362   1.1  augustss 		sc->sc_dying = 1;
    363  1.44    dyoung 		return 0;
    364  1.44    dyoung 	default:
    365  1.44    dyoung 		return EOPNOTSUPP;
    366   1.1  augustss 	}
    367   1.1  augustss }
    368   1.1  augustss 
    369  1.35    dyoung void
    370  1.35    dyoung uftdi_childdet(device_t self, device_t child)
    371  1.35    dyoung {
    372  1.35    dyoung 	int i;
    373  1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    374  1.35    dyoung 
    375  1.35    dyoung 	for (i = 0; i < sc->sc_numports; i++) {
    376  1.35    dyoung 		if (sc->sc_subdev[i] == child)
    377  1.35    dyoung 			break;
    378  1.35    dyoung 	}
    379  1.35    dyoung 	KASSERT(i < sc->sc_numports);
    380  1.35    dyoung 	sc->sc_subdev[i] = NULL;
    381  1.35    dyoung }
    382  1.35    dyoung 
    383   1.1  augustss int
    384  1.35    dyoung uftdi_detach(device_t self, int flags)
    385   1.1  augustss {
    386  1.35    dyoung 	struct uftdi_softc *sc = device_private(self);
    387  1.32       riz 	int i;
    388   1.1  augustss 
    389   1.1  augustss 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    390   1.1  augustss 	sc->sc_dying = 1;
    391  1.35    dyoung 	for (i=0; i < sc->sc_numports; i++) {
    392  1.35    dyoung 		if (sc->sc_subdev[i] != NULL)
    393  1.32       riz 			config_detach(sc->sc_subdev[i], flags);
    394  1.35    dyoung 	}
    395   1.5  augustss 
    396  1.62   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    397   1.5  augustss 
    398  1.61     skrll 	return 0;
    399   1.1  augustss }
    400   1.1  augustss 
    401   1.1  augustss Static int
    402   1.2  augustss uftdi_open(void *vsc, int portno)
    403   1.1  augustss {
    404   1.1  augustss 	struct uftdi_softc *sc = vsc;
    405   1.1  augustss 	usb_device_request_t req;
    406   1.1  augustss 	usbd_status err;
    407   1.1  augustss 	struct termios t;
    408   1.1  augustss 
    409   1.1  augustss 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    410   1.1  augustss 
    411   1.1  augustss 	if (sc->sc_dying)
    412  1.61     skrll 		return EIO;
    413   1.1  augustss 
    414   1.1  augustss 	/* Perform a full reset on the device */
    415   1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    416   1.1  augustss 	req.bRequest = FTDI_SIO_RESET;
    417   1.1  augustss 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    418   1.1  augustss 	USETW(req.wIndex, portno);
    419   1.1  augustss 	USETW(req.wLength, 0);
    420   1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    421   1.1  augustss 	if (err)
    422  1.61     skrll 		return EIO;
    423   1.1  augustss 
    424   1.1  augustss 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    425   1.1  augustss 	t.c_ospeed = 9600;
    426   1.1  augustss 	t.c_cflag = CSTOPB | CS8;
    427   1.1  augustss 	(void)uftdi_param(sc, portno, &t);
    428   1.1  augustss 
    429   1.1  augustss 	/* Turn on RTS/CTS flow control */
    430   1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    431   1.1  augustss 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    432   1.1  augustss 	USETW(req.wValue, 0);
    433   1.1  augustss 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    434   1.1  augustss 	USETW(req.wLength, 0);
    435   1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    436   1.1  augustss 	if (err)
    437  1.61     skrll 		return EIO;
    438   1.1  augustss 
    439  1.61     skrll 	return 0;
    440   1.1  augustss }
    441   1.1  augustss 
    442   1.1  augustss Static void
    443  1.61     skrll uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
    444   1.1  augustss {
    445   1.1  augustss 	struct uftdi_softc *sc = vsc;
    446   1.1  augustss 	u_char msr, lsr;
    447   1.1  augustss 
    448   1.1  augustss 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    449   1.1  augustss 		     *count));
    450   1.1  augustss 
    451   1.1  augustss 	msr = FTDI_GET_MSR(*ptr);
    452   1.1  augustss 	lsr = FTDI_GET_LSR(*ptr);
    453   1.1  augustss 
    454   1.1  augustss #ifdef UFTDI_DEBUG
    455   1.1  augustss 	if (*count != 2)
    456   1.1  augustss 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    457   1.1  augustss 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    458   1.1  augustss #endif
    459   1.1  augustss 
    460   1.1  augustss 	if (sc->sc_msr != msr ||
    461   1.1  augustss 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    462   1.1  augustss 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    463   1.1  augustss 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    464   1.1  augustss 			 lsr, sc->sc_lsr));
    465   1.1  augustss 		sc->sc_msr = msr;
    466   1.1  augustss 		sc->sc_lsr = lsr;
    467  1.38      cube 		ucom_status_change(device_private(sc->sc_subdev[portno-1]));
    468   1.1  augustss 	}
    469   1.1  augustss 
    470  1.46       scw 	/* Adjust buffer pointer to skip status prefix */
    471   1.1  augustss 	*ptr += 2;
    472   1.1  augustss }
    473   1.1  augustss 
    474   1.1  augustss Static void
    475  1.61     skrll uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
    476   1.1  augustss {
    477  1.10       scw 	struct uftdi_softc *sc = vsc;
    478  1.10       scw 
    479   1.1  augustss 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    480   1.1  augustss 		     vsc, portno, *count, from[0]));
    481   1.1  augustss 
    482   1.1  augustss 	/* Make length tag and copy data */
    483  1.10       scw 	if (sc->sc_hdrlen > 0)
    484  1.10       scw 		*to = FTDI_OUT_TAG(*count, portno);
    485  1.10       scw 
    486  1.10       scw 	memcpy(to + sc->sc_hdrlen, from, *count);
    487  1.10       scw 	*count += sc->sc_hdrlen;
    488   1.1  augustss }
    489   1.1  augustss 
    490   1.1  augustss Static void
    491   1.2  augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
    492   1.1  augustss {
    493   1.1  augustss 	struct uftdi_softc *sc = vsc;
    494   1.1  augustss 	usb_device_request_t req;
    495   1.1  augustss 	int ctl;
    496   1.1  augustss 
    497   1.1  augustss 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    498   1.1  augustss 		 reg, onoff));
    499   1.1  augustss 
    500   1.1  augustss 	switch (reg) {
    501   1.1  augustss 	case UCOM_SET_DTR:
    502   1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    503   1.1  augustss 		break;
    504   1.1  augustss 	case UCOM_SET_RTS:
    505   1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    506   1.1  augustss 		break;
    507   1.1  augustss 	case UCOM_SET_BREAK:
    508   1.8    ichiro 		uftdi_break(sc, portno, onoff);
    509   1.1  augustss 		return;
    510   1.1  augustss 	default:
    511   1.1  augustss 		return;
    512   1.1  augustss 	}
    513   1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    514   1.1  augustss 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    515   1.1  augustss 	USETW(req.wValue, ctl);
    516   1.1  augustss 	USETW(req.wIndex, portno);
    517   1.1  augustss 	USETW(req.wLength, 0);
    518   1.1  augustss 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    519   1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    520   1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    521   1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    522   1.1  augustss }
    523   1.1  augustss 
    524   1.1  augustss Static int
    525   1.2  augustss uftdi_param(void *vsc, int portno, struct termios *t)
    526   1.1  augustss {
    527   1.1  augustss 	struct uftdi_softc *sc = vsc;
    528   1.1  augustss 	usb_device_request_t req;
    529   1.1  augustss 	usbd_status err;
    530  1.12       scw 	int rate, data, flow;
    531   1.1  augustss 
    532   1.1  augustss 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    533   1.1  augustss 
    534   1.1  augustss 	if (sc->sc_dying)
    535  1.61     skrll 		return EIO;
    536   1.1  augustss 
    537  1.56   mlelstv 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    538  1.56   mlelstv 	req.bRequest = FTDI_SIO_SET_BITMODE;
    539  1.56   mlelstv 	USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
    540  1.56   mlelstv 	USETW(req.wIndex, portno);
    541  1.56   mlelstv 	USETW(req.wLength, 0);
    542  1.56   mlelstv 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    543  1.56   mlelstv 	if (err)
    544  1.61     skrll 		return EIO;
    545  1.56   mlelstv 
    546  1.10       scw 	switch (sc->sc_type) {
    547  1.10       scw 	case UFTDI_TYPE_SIO:
    548  1.10       scw 		switch (t->c_ospeed) {
    549  1.10       scw 		case 300: rate = ftdi_sio_b300; break;
    550  1.10       scw 		case 600: rate = ftdi_sio_b600; break;
    551  1.10       scw 		case 1200: rate = ftdi_sio_b1200; break;
    552  1.10       scw 		case 2400: rate = ftdi_sio_b2400; break;
    553  1.10       scw 		case 4800: rate = ftdi_sio_b4800; break;
    554  1.10       scw 		case 9600: rate = ftdi_sio_b9600; break;
    555  1.10       scw 		case 19200: rate = ftdi_sio_b19200; break;
    556  1.10       scw 		case 38400: rate = ftdi_sio_b38400; break;
    557  1.10       scw 		case 57600: rate = ftdi_sio_b57600; break;
    558  1.10       scw 		case 115200: rate = ftdi_sio_b115200; break;
    559  1.10       scw 		default:
    560  1.61     skrll 			return EINVAL;
    561  1.10       scw 		}
    562  1.10       scw 		break;
    563  1.10       scw 
    564  1.10       scw 	case UFTDI_TYPE_8U232AM:
    565  1.10       scw 		switch(t->c_ospeed) {
    566  1.10       scw 		case 300: rate = ftdi_8u232am_b300; break;
    567  1.10       scw 		case 600: rate = ftdi_8u232am_b600; break;
    568  1.10       scw 		case 1200: rate = ftdi_8u232am_b1200; break;
    569  1.10       scw 		case 2400: rate = ftdi_8u232am_b2400; break;
    570  1.10       scw 		case 4800: rate = ftdi_8u232am_b4800; break;
    571  1.10       scw 		case 9600: rate = ftdi_8u232am_b9600; break;
    572  1.10       scw 		case 19200: rate = ftdi_8u232am_b19200; break;
    573  1.10       scw 		case 38400: rate = ftdi_8u232am_b38400; break;
    574  1.10       scw 		case 57600: rate = ftdi_8u232am_b57600; break;
    575  1.10       scw 		case 115200: rate = ftdi_8u232am_b115200; break;
    576  1.10       scw 		case 230400: rate = ftdi_8u232am_b230400; break;
    577  1.10       scw 		case 460800: rate = ftdi_8u232am_b460800; break;
    578  1.10       scw 		case 921600: rate = ftdi_8u232am_b921600; break;
    579  1.10       scw 		default:
    580  1.61     skrll 			return EINVAL;
    581  1.10       scw 		}
    582  1.10       scw 		break;
    583  1.16   mycroft 
    584  1.15  christos 	default:
    585  1.61     skrll 		return EINVAL;
    586   1.1  augustss 	}
    587   1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    588   1.1  augustss 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    589   1.1  augustss 	USETW(req.wValue, rate);
    590   1.1  augustss 	USETW(req.wIndex, portno);
    591   1.1  augustss 	USETW(req.wLength, 0);
    592   1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    593   1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    594   1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    595   1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    596   1.1  augustss 	if (err)
    597  1.61     skrll 		return EIO;
    598   1.1  augustss 
    599   1.1  augustss 	if (ISSET(t->c_cflag, CSTOPB))
    600   1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    601   1.1  augustss 	else
    602   1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    603   1.1  augustss 	if (ISSET(t->c_cflag, PARENB)) {
    604   1.1  augustss 		if (ISSET(t->c_cflag, PARODD))
    605   1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    606   1.1  augustss 		else
    607   1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    608   1.1  augustss 	} else
    609   1.1  augustss 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    610   1.1  augustss 	switch (ISSET(t->c_cflag, CSIZE)) {
    611   1.1  augustss 	case CS5:
    612   1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(5);
    613   1.1  augustss 		break;
    614   1.1  augustss 	case CS6:
    615   1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(6);
    616   1.1  augustss 		break;
    617   1.1  augustss 	case CS7:
    618   1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(7);
    619   1.1  augustss 		break;
    620   1.1  augustss 	case CS8:
    621   1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(8);
    622   1.1  augustss 		break;
    623   1.1  augustss 	}
    624   1.8    ichiro 	sc->last_lcr = data;
    625   1.8    ichiro 
    626   1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    627   1.1  augustss 	req.bRequest = FTDI_SIO_SET_DATA;
    628   1.1  augustss 	USETW(req.wValue, data);
    629   1.1  augustss 	USETW(req.wIndex, portno);
    630   1.1  augustss 	USETW(req.wLength, 0);
    631   1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    632   1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    633   1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    634  1.12       scw 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    635  1.12       scw 	if (err)
    636  1.61     skrll 		return EIO;
    637  1.12       scw 
    638  1.12       scw 	if (ISSET(t->c_cflag, CRTSCTS)) {
    639  1.12       scw 		flow = FTDI_SIO_RTS_CTS_HS;
    640  1.12       scw 		USETW(req.wValue, 0);
    641  1.58   mlelstv 	} else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
    642  1.12       scw 		flow = FTDI_SIO_XON_XOFF_HS;
    643  1.12       scw 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    644  1.12       scw 	} else {
    645  1.12       scw 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    646  1.12       scw 		USETW(req.wValue, 0);
    647  1.12       scw 	}
    648  1.12       scw 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    649  1.12       scw 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    650  1.12       scw 	USETW2(req.wIndex, flow, portno);
    651  1.12       scw 	USETW(req.wLength, 0);
    652   1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    653   1.1  augustss 	if (err)
    654  1.61     skrll 		return EIO;
    655   1.1  augustss 
    656  1.61     skrll 	return 0;
    657   1.1  augustss }
    658   1.1  augustss 
    659   1.1  augustss void
    660  1.29  christos uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    661   1.1  augustss {
    662   1.1  augustss 	struct uftdi_softc *sc = vsc;
    663   1.1  augustss 
    664   1.1  augustss 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    665   1.1  augustss 		 sc->sc_msr, sc->sc_lsr));
    666   1.1  augustss 
    667   1.1  augustss 	if (msr != NULL)
    668   1.1  augustss 		*msr = sc->sc_msr;
    669   1.1  augustss 	if (lsr != NULL)
    670   1.1  augustss 		*lsr = sc->sc_lsr;
    671   1.8    ichiro }
    672   1.8    ichiro 
    673   1.8    ichiro void
    674   1.8    ichiro uftdi_break(void *vsc, int portno, int onoff)
    675   1.8    ichiro {
    676   1.8    ichiro 	struct uftdi_softc *sc = vsc;
    677   1.9    ichiro 	usb_device_request_t req;
    678   1.8    ichiro 	int data;
    679   1.8    ichiro 
    680   1.8    ichiro 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    681  1.11  augustss 		  onoff));
    682   1.8    ichiro 
    683   1.8    ichiro 	if (onoff) {
    684   1.8    ichiro 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    685   1.8    ichiro 	} else {
    686   1.8    ichiro 		data = sc->last_lcr;
    687   1.8    ichiro 	}
    688   1.8    ichiro 
    689   1.8    ichiro 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    690   1.8    ichiro 	req.bRequest = FTDI_SIO_SET_DATA;
    691   1.8    ichiro 	USETW(req.wValue, data);
    692   1.8    ichiro 	USETW(req.wIndex, portno);
    693   1.8    ichiro 	USETW(req.wLength, 0);
    694   1.8    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    695   1.1  augustss }
    696