Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.24.2.4
      1  1.24.2.4      yamt /*	$NetBSD: uftdi.c,v 1.24.2.4 2008/02/27 08:36:47 yamt 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  * 3. All advertising materials mentioning features or use of this software
     19       1.1  augustss  *    must display the following acknowledgement:
     20       1.1  augustss  *        This product includes software developed by the NetBSD
     21       1.1  augustss  *        Foundation, Inc. and its contributors.
     22       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  augustss  *    contributors may be used to endorse or promote products derived
     24       1.1  augustss  *    from this software without specific prior written permission.
     25       1.1  augustss  *
     26       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  augustss  */
     38       1.1  augustss 
     39       1.1  augustss /*
     40       1.1  augustss  * FTDI FT8U100AX serial adapter driver
     41       1.1  augustss  */
     42       1.1  augustss 
     43       1.7     lukem #include <sys/cdefs.h>
     44  1.24.2.4      yamt __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.24.2.4 2008/02/27 08:36:47 yamt Exp $");
     45       1.1  augustss 
     46       1.1  augustss #include <sys/param.h>
     47       1.1  augustss #include <sys/systm.h>
     48       1.1  augustss #include <sys/kernel.h>
     49       1.1  augustss #include <sys/device.h>
     50       1.1  augustss #include <sys/conf.h>
     51       1.1  augustss #include <sys/tty.h>
     52       1.1  augustss 
     53       1.1  augustss #include <dev/usb/usb.h>
     54       1.1  augustss 
     55       1.1  augustss #include <dev/usb/usbdi.h>
     56       1.1  augustss #include <dev/usb/usbdi_util.h>
     57       1.1  augustss #include <dev/usb/usbdevs.h>
     58       1.1  augustss 
     59       1.1  augustss #include <dev/usb/ucomvar.h>
     60       1.1  augustss 
     61       1.1  augustss #include <dev/usb/uftdireg.h>
     62       1.1  augustss 
     63       1.1  augustss #ifdef UFTDI_DEBUG
     64       1.1  augustss #define DPRINTF(x)	if (uftdidebug) printf x
     65       1.1  augustss #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
     66       1.4  augustss int uftdidebug = 0;
     67       1.1  augustss #else
     68       1.1  augustss #define DPRINTF(x)
     69       1.1  augustss #define DPRINTFN(n,x)
     70       1.1  augustss #endif
     71       1.1  augustss 
     72       1.1  augustss #define UFTDI_CONFIG_INDEX	0
     73       1.1  augustss #define UFTDI_IFACE_INDEX	0
     74  1.24.2.2      yamt #define UFTDI_MAX_PORTS		2
     75       1.1  augustss 
     76       1.1  augustss /*
     77       1.1  augustss  * These are the maximum number of bytes transferred per frame.
     78       1.1  augustss  * The output buffer size cannot be increased due to the size encoding.
     79       1.1  augustss  */
     80       1.1  augustss #define UFTDIIBUFSIZE 64
     81       1.1  augustss #define UFTDIOBUFSIZE 64
     82       1.1  augustss 
     83       1.1  augustss struct uftdi_softc {
     84       1.1  augustss 	USBBASEDEVICE		sc_dev;		/* base device */
     85       1.1  augustss 	usbd_device_handle	sc_udev;	/* device */
     86  1.24.2.2      yamt 	usbd_interface_handle	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.24.2.2      yamt 	u_int			sc_numports;
     91      1.10       scw 
     92       1.1  augustss 	u_char			sc_msr;
     93       1.1  augustss 	u_char			sc_lsr;
     94       1.1  augustss 
     95  1.24.2.2      yamt 	device_ptr_t		sc_subdev[UFTDI_MAX_PORTS];
     96       1.1  augustss 
     97       1.1  augustss 	u_char			sc_dying;
     98       1.8    ichiro 
     99       1.8    ichiro 	u_int			last_lcr;
    100       1.1  augustss };
    101       1.1  augustss 
    102       1.2  augustss Static void	uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
    103       1.2  augustss Static void	uftdi_set(void *, int, int, int);
    104       1.2  augustss Static int	uftdi_param(void *, int, struct termios *);
    105       1.2  augustss Static int	uftdi_open(void *sc, int portno);
    106       1.2  augustss Static void	uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
    107       1.2  augustss Static void	uftdi_write(void *sc, int portno, u_char *to, u_char *from,
    108       1.2  augustss 			    u_int32_t *count);
    109       1.8    ichiro Static void	uftdi_break(void *sc, int portno, int onoff);
    110       1.1  augustss 
    111       1.1  augustss struct ucom_methods uftdi_methods = {
    112       1.1  augustss 	uftdi_get_status,
    113       1.1  augustss 	uftdi_set,
    114       1.1  augustss 	uftdi_param,
    115       1.1  augustss 	NULL,
    116       1.1  augustss 	uftdi_open,
    117       1.1  augustss 	NULL,
    118       1.1  augustss 	uftdi_read,
    119       1.1  augustss 	uftdi_write,
    120       1.1  augustss };
    121       1.1  augustss 
    122  1.24.2.1      yamt /*
    123  1.24.2.1      yamt  * The devices default to UFTDI_TYPE_8U232AM.
    124  1.24.2.1      yamt  * Remember to update USB_ATTACH if it should be UFTDI_TYPE_SIO instead
    125  1.24.2.1      yamt  */
    126  1.24.2.1      yamt static const struct usb_devno uftdi_devs[] = {
    127  1.24.2.1      yamt 	{ USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
    128  1.24.2.1      yamt 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
    129  1.24.2.1      yamt 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
    130  1.24.2.2      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
    131  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
    132  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
    133  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
    134  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
    135  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
    136  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
    137  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
    138  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
    139  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
    140  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
    141  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
    142  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
    143  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
    144  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
    145  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
    146  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
    147  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
    148  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
    149  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
    150  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
    151  1.24.2.1      yamt 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
    152  1.24.2.1      yamt 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
    153  1.24.2.1      yamt 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
    154  1.24.2.1      yamt 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
    155  1.24.2.1      yamt 	{ USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
    156  1.24.2.1      yamt };
    157  1.24.2.1      yamt #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
    158  1.24.2.1      yamt 
    159  1.24.2.4      yamt int uftdi_match(device_t, struct cfdata *, void *);
    160  1.24.2.4      yamt void uftdi_attach(device_t, device_t, void *);
    161  1.24.2.4      yamt void uftdi_childdet(device_t, device_t);
    162  1.24.2.4      yamt int uftdi_detach(device_t, int);
    163  1.24.2.4      yamt int uftdi_activate(device_t, enum devact);
    164  1.24.2.4      yamt extern struct cfdriver uftdi_cd;
    165  1.24.2.4      yamt CFATTACH_DECL2(uftdi, sizeof(struct uftdi_softc), uftdi_match,
    166  1.24.2.4      yamt     uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
    167       1.1  augustss 
    168       1.1  augustss USB_MATCH(uftdi)
    169       1.1  augustss {
    170       1.1  augustss 	USB_MATCH_START(uftdi, uaa);
    171      1.11  augustss 
    172       1.1  augustss 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
    173       1.1  augustss 		     uaa->vendor, uaa->product));
    174       1.1  augustss 
    175  1.24.2.1      yamt         return (uftdi_lookup(uaa->vendor, uaa->product) != NULL ?
    176  1.24.2.1      yamt                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    177       1.1  augustss }
    178       1.1  augustss 
    179       1.1  augustss USB_ATTACH(uftdi)
    180       1.1  augustss {
    181       1.1  augustss 	USB_ATTACH_START(uftdi, sc, uaa);
    182       1.1  augustss 	usbd_device_handle dev = uaa->device;
    183       1.1  augustss 	usbd_interface_handle iface;
    184       1.1  augustss 	usb_interface_descriptor_t *id;
    185       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    186      1.24  augustss 	char *devinfop;
    187       1.1  augustss 	char *devname = USBDEVNAME(sc->sc_dev);
    188  1.24.2.2      yamt 	int i,idx;
    189       1.1  augustss 	usbd_status err;
    190       1.1  augustss 	struct ucom_attach_args uca;
    191       1.1  augustss 
    192       1.1  augustss 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
    193       1.1  augustss 
    194       1.1  augustss 	/* Move the device into the configured state. */
    195       1.1  augustss 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
    196       1.1  augustss 	if (err) {
    197       1.1  augustss 		printf("\n%s: failed to set configuration, err=%s\n",
    198       1.1  augustss 		       devname, usbd_errstr(err));
    199       1.1  augustss 		goto bad;
    200       1.1  augustss 	}
    201       1.1  augustss 
    202      1.24  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    203       1.1  augustss 	USB_ATTACH_SETUP;
    204      1.24  augustss 	printf("%s: %s\n", devname, devinfop);
    205      1.24  augustss 	usbd_devinfo_free(devinfop);
    206       1.1  augustss 
    207       1.1  augustss 	sc->sc_udev = dev;
    208  1.24.2.2      yamt 	sc->sc_numports = 1;
    209  1.24.2.1      yamt 	switch( uaa->vendor ) {
    210  1.24.2.1      yamt 	case USB_VENDOR_FTDI:
    211  1.24.2.1      yamt 		switch (uaa->product) {
    212  1.24.2.1      yamt 		case USB_PRODUCT_FTDI_SERIAL_8U100AX:
    213  1.24.2.1      yamt 			sc->sc_type = UFTDI_TYPE_SIO;
    214  1.24.2.1      yamt 			sc->sc_hdrlen = 1;
    215  1.24.2.1      yamt 			break;
    216  1.24.2.2      yamt 		case USB_PRODUCT_FTDI_SERIAL_2232C:
    217  1.24.2.2      yamt 			sc->sc_numports = 2;
    218  1.24.2.2      yamt 			/* FALLTHROUGH */
    219  1.24.2.1      yamt 		default:		/* Most uftdi devices are 8U232AM */
    220  1.24.2.1      yamt 			sc->sc_type = UFTDI_TYPE_8U232AM;
    221  1.24.2.1      yamt 			sc->sc_hdrlen = 0;
    222  1.24.2.1      yamt 		}
    223      1.10       scw 		break;
    224  1.24.2.1      yamt 	default:		/* Most uftdi devices are 8U232AM */
    225      1.10       scw 		sc->sc_type = UFTDI_TYPE_8U232AM;
    226      1.10       scw 		sc->sc_hdrlen = 0;
    227      1.10       scw 	}
    228      1.10       scw 
    229  1.24.2.2      yamt 	for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) {
    230  1.24.2.2      yamt 		err = usbd_device2interface_handle(dev, idx, &iface);
    231  1.24.2.2      yamt 		if (err) {
    232  1.24.2.2      yamt 			printf("\n%s: failed to get interface idx=%d, err=%s\n",
    233  1.24.2.2      yamt 				   devname, idx, usbd_errstr(err));
    234       1.1  augustss 			goto bad;
    235       1.1  augustss 		}
    236      1.11  augustss 
    237  1.24.2.2      yamt 		id = usbd_get_interface_descriptor(iface);
    238  1.24.2.2      yamt 
    239  1.24.2.2      yamt 		sc->sc_iface[idx] = iface;
    240  1.24.2.2      yamt 
    241  1.24.2.2      yamt 		uca.bulkin = uca.bulkout = -1;
    242  1.24.2.2      yamt 		for (i = 0; i < id->bNumEndpoints; i++) {
    243  1.24.2.2      yamt 			int addr, dir, attr;
    244  1.24.2.2      yamt 			ed = usbd_interface2endpoint_descriptor(iface, i);
    245  1.24.2.2      yamt 			if (ed == NULL) {
    246  1.24.2.2      yamt 				printf("%s: could not read endpoint descriptor"
    247  1.24.2.2      yamt 					   ": %s\n", devname, usbd_errstr(err));
    248  1.24.2.2      yamt 				goto bad;
    249  1.24.2.2      yamt 			}
    250  1.24.2.2      yamt 
    251  1.24.2.2      yamt 			addr = ed->bEndpointAddress;
    252  1.24.2.2      yamt 			dir = UE_GET_DIR(ed->bEndpointAddress);
    253  1.24.2.2      yamt 			attr = ed->bmAttributes & UE_XFERTYPE;
    254  1.24.2.2      yamt 			if (dir == UE_DIR_IN && attr == UE_BULK)
    255  1.24.2.2      yamt 				uca.bulkin = addr;
    256  1.24.2.2      yamt 			else if (dir == UE_DIR_OUT && attr == UE_BULK)
    257  1.24.2.2      yamt 				uca.bulkout = addr;
    258  1.24.2.2      yamt 			else {
    259  1.24.2.2      yamt 				printf("%s: unexpected endpoint\n", devname);
    260  1.24.2.2      yamt 				goto bad;
    261  1.24.2.2      yamt 			}
    262  1.24.2.2      yamt 		}
    263  1.24.2.2      yamt 		if (uca.bulkin == -1) {
    264  1.24.2.2      yamt 			printf("%s: Could not find data bulk in\n",
    265  1.24.2.2      yamt 				   USBDEVNAME(sc->sc_dev));
    266  1.24.2.2      yamt 			goto bad;
    267  1.24.2.2      yamt 		}
    268  1.24.2.2      yamt 		if (uca.bulkout == -1) {
    269  1.24.2.2      yamt 			printf("%s: Could not find data bulk out\n",
    270  1.24.2.2      yamt 				   USBDEVNAME(sc->sc_dev));
    271       1.1  augustss 			goto bad;
    272       1.1  augustss 		}
    273      1.11  augustss 
    274  1.24.2.2      yamt 		uca.portno = FTDI_PIT_SIOA + idx;
    275  1.24.2.2      yamt 		/* bulkin, bulkout set above */
    276  1.24.2.2      yamt 		uca.ibufsize = UFTDIIBUFSIZE;
    277  1.24.2.2      yamt 		uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    278  1.24.2.2      yamt 		uca.ibufsizepad = UFTDIIBUFSIZE;
    279  1.24.2.2      yamt 		uca.opkthdrlen = sc->sc_hdrlen;
    280  1.24.2.2      yamt 		uca.device = dev;
    281  1.24.2.2      yamt 		uca.iface = iface;
    282  1.24.2.2      yamt 		uca.methods = &uftdi_methods;
    283  1.24.2.2      yamt 		uca.arg = sc;
    284  1.24.2.2      yamt 		uca.info = NULL;
    285  1.24.2.2      yamt 
    286  1.24.2.2      yamt 		DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    287  1.24.2.2      yamt 		sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL, &uca,
    288  1.24.2.2      yamt 											ucomprint, ucomsubmatch);
    289  1.24.2.2      yamt 	}
    290       1.1  augustss 
    291       1.5  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    292       1.5  augustss 			   USBDEV(sc->sc_dev));
    293       1.5  augustss 
    294       1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    295       1.1  augustss 
    296       1.1  augustss bad:
    297       1.1  augustss 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    298       1.1  augustss 	sc->sc_dying = 1;
    299       1.1  augustss 	USB_ATTACH_ERROR_RETURN;
    300       1.1  augustss }
    301       1.1  augustss 
    302       1.1  augustss int
    303       1.2  augustss uftdi_activate(device_ptr_t self, enum devact act)
    304       1.1  augustss {
    305       1.1  augustss 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
    306  1.24.2.2      yamt 	int rv = 0,i;
    307       1.1  augustss 
    308       1.1  augustss 	switch (act) {
    309       1.1  augustss 	case DVACT_ACTIVATE:
    310       1.1  augustss 		return (EOPNOTSUPP);
    311       1.1  augustss 
    312       1.1  augustss 	case DVACT_DEACTIVATE:
    313  1.24.2.2      yamt 		for (i=0; i < sc->sc_numports; i++)
    314  1.24.2.2      yamt 			if (sc->sc_subdev[i] != NULL)
    315  1.24.2.2      yamt 				rv = config_deactivate(sc->sc_subdev[i]);
    316       1.1  augustss 		sc->sc_dying = 1;
    317       1.1  augustss 		break;
    318       1.1  augustss 	}
    319       1.1  augustss 	return (rv);
    320       1.1  augustss }
    321       1.1  augustss 
    322  1.24.2.4      yamt void
    323  1.24.2.4      yamt uftdi_childdet(device_t self, device_t child)
    324  1.24.2.4      yamt {
    325  1.24.2.4      yamt 	int i;
    326  1.24.2.4      yamt 	struct uftdi_softc *sc = device_private(self);
    327  1.24.2.4      yamt 
    328  1.24.2.4      yamt 	for (i = 0; i < sc->sc_numports; i++) {
    329  1.24.2.4      yamt 		if (sc->sc_subdev[i] == child)
    330  1.24.2.4      yamt 			break;
    331  1.24.2.4      yamt 	}
    332  1.24.2.4      yamt 	KASSERT(i < sc->sc_numports);
    333  1.24.2.4      yamt 	sc->sc_subdev[i] = NULL;
    334  1.24.2.4      yamt }
    335  1.24.2.4      yamt 
    336       1.1  augustss int
    337  1.24.2.4      yamt uftdi_detach(device_t self, int flags)
    338       1.1  augustss {
    339  1.24.2.4      yamt 	struct uftdi_softc *sc = device_private(self);
    340  1.24.2.2      yamt 	int i;
    341       1.1  augustss 
    342       1.1  augustss 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    343       1.1  augustss 	sc->sc_dying = 1;
    344  1.24.2.4      yamt 	for (i=0; i < sc->sc_numports; i++) {
    345  1.24.2.4      yamt 		if (sc->sc_subdev[i] != NULL)
    346  1.24.2.2      yamt 			config_detach(sc->sc_subdev[i], flags);
    347  1.24.2.4      yamt 	}
    348       1.5  augustss 
    349       1.5  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    350       1.5  augustss 			   USBDEV(sc->sc_dev));
    351       1.5  augustss 
    352       1.1  augustss 	return (0);
    353       1.1  augustss }
    354       1.1  augustss 
    355       1.1  augustss Static int
    356       1.2  augustss uftdi_open(void *vsc, int portno)
    357       1.1  augustss {
    358       1.1  augustss 	struct uftdi_softc *sc = vsc;
    359       1.1  augustss 	usb_device_request_t req;
    360       1.1  augustss 	usbd_status err;
    361       1.1  augustss 	struct termios t;
    362       1.1  augustss 
    363       1.1  augustss 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    364       1.1  augustss 
    365       1.1  augustss 	if (sc->sc_dying)
    366       1.1  augustss 		return (EIO);
    367       1.1  augustss 
    368       1.1  augustss 	/* Perform a full reset on the device */
    369       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    370       1.1  augustss 	req.bRequest = FTDI_SIO_RESET;
    371       1.1  augustss 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    372       1.1  augustss 	USETW(req.wIndex, portno);
    373       1.1  augustss 	USETW(req.wLength, 0);
    374       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    375       1.1  augustss 	if (err)
    376       1.1  augustss 		return (EIO);
    377       1.1  augustss 
    378       1.1  augustss 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    379       1.1  augustss 	t.c_ospeed = 9600;
    380       1.1  augustss 	t.c_cflag = CSTOPB | CS8;
    381       1.1  augustss 	(void)uftdi_param(sc, portno, &t);
    382       1.1  augustss 
    383       1.1  augustss 	/* Turn on RTS/CTS flow control */
    384       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    385       1.1  augustss 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    386       1.1  augustss 	USETW(req.wValue, 0);
    387       1.1  augustss 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    388       1.1  augustss 	USETW(req.wLength, 0);
    389       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    390       1.1  augustss 	if (err)
    391       1.1  augustss 		return (EIO);
    392       1.1  augustss 
    393       1.1  augustss 	return (0);
    394       1.1  augustss }
    395       1.1  augustss 
    396       1.1  augustss Static void
    397       1.2  augustss uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
    398       1.1  augustss {
    399       1.1  augustss 	struct uftdi_softc *sc = vsc;
    400       1.1  augustss 	u_char msr, lsr;
    401       1.1  augustss 
    402       1.1  augustss 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    403       1.1  augustss 		     *count));
    404       1.1  augustss 
    405       1.1  augustss 	msr = FTDI_GET_MSR(*ptr);
    406       1.1  augustss 	lsr = FTDI_GET_LSR(*ptr);
    407       1.1  augustss 
    408       1.1  augustss #ifdef UFTDI_DEBUG
    409       1.1  augustss 	if (*count != 2)
    410       1.1  augustss 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    411       1.1  augustss 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    412       1.1  augustss #endif
    413       1.1  augustss 
    414       1.1  augustss 	if (sc->sc_msr != msr ||
    415       1.1  augustss 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    416       1.1  augustss 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    417       1.1  augustss 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    418       1.1  augustss 			 lsr, sc->sc_lsr));
    419       1.1  augustss 		sc->sc_msr = msr;
    420       1.1  augustss 		sc->sc_lsr = lsr;
    421  1.24.2.2      yamt 		ucom_status_change((struct ucom_softc *)sc->sc_subdev[portno-1]);
    422       1.1  augustss 	}
    423       1.1  augustss 
    424       1.1  augustss 	/* Pick up status and adjust data part. */
    425       1.1  augustss 	*ptr += 2;
    426       1.1  augustss 	*count -= 2;
    427       1.1  augustss }
    428       1.1  augustss 
    429       1.1  augustss Static void
    430       1.2  augustss uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
    431       1.1  augustss {
    432      1.10       scw 	struct uftdi_softc *sc = vsc;
    433      1.10       scw 
    434       1.1  augustss 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    435       1.1  augustss 		     vsc, portno, *count, from[0]));
    436       1.1  augustss 
    437       1.1  augustss 	/* Make length tag and copy data */
    438      1.10       scw 	if (sc->sc_hdrlen > 0)
    439      1.10       scw 		*to = FTDI_OUT_TAG(*count, portno);
    440      1.10       scw 
    441      1.10       scw 	memcpy(to + sc->sc_hdrlen, from, *count);
    442      1.10       scw 	*count += sc->sc_hdrlen;
    443       1.1  augustss }
    444       1.1  augustss 
    445       1.1  augustss Static void
    446       1.2  augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
    447       1.1  augustss {
    448       1.1  augustss 	struct uftdi_softc *sc = vsc;
    449       1.1  augustss 	usb_device_request_t req;
    450       1.1  augustss 	int ctl;
    451       1.1  augustss 
    452       1.1  augustss 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    453       1.1  augustss 		 reg, onoff));
    454       1.1  augustss 
    455       1.1  augustss 	switch (reg) {
    456       1.1  augustss 	case UCOM_SET_DTR:
    457       1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    458       1.1  augustss 		break;
    459       1.1  augustss 	case UCOM_SET_RTS:
    460       1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    461       1.1  augustss 		break;
    462       1.1  augustss 	case UCOM_SET_BREAK:
    463       1.8    ichiro 		uftdi_break(sc, portno, onoff);
    464       1.1  augustss 		return;
    465       1.1  augustss 	default:
    466       1.1  augustss 		return;
    467       1.1  augustss 	}
    468       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    469       1.1  augustss 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    470       1.1  augustss 	USETW(req.wValue, ctl);
    471       1.1  augustss 	USETW(req.wIndex, portno);
    472       1.1  augustss 	USETW(req.wLength, 0);
    473       1.1  augustss 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    474       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    475       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    476       1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    477       1.1  augustss }
    478       1.1  augustss 
    479       1.1  augustss Static int
    480       1.2  augustss uftdi_param(void *vsc, int portno, struct termios *t)
    481       1.1  augustss {
    482       1.1  augustss 	struct uftdi_softc *sc = vsc;
    483       1.1  augustss 	usb_device_request_t req;
    484       1.1  augustss 	usbd_status err;
    485      1.12       scw 	int rate, data, flow;
    486       1.1  augustss 
    487       1.1  augustss 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    488       1.1  augustss 
    489       1.1  augustss 	if (sc->sc_dying)
    490       1.1  augustss 		return (EIO);
    491       1.1  augustss 
    492      1.10       scw 	switch (sc->sc_type) {
    493      1.10       scw 	case UFTDI_TYPE_SIO:
    494      1.10       scw 		switch (t->c_ospeed) {
    495      1.10       scw 		case 300: rate = ftdi_sio_b300; break;
    496      1.10       scw 		case 600: rate = ftdi_sio_b600; break;
    497      1.10       scw 		case 1200: rate = ftdi_sio_b1200; break;
    498      1.10       scw 		case 2400: rate = ftdi_sio_b2400; break;
    499      1.10       scw 		case 4800: rate = ftdi_sio_b4800; break;
    500      1.10       scw 		case 9600: rate = ftdi_sio_b9600; break;
    501      1.10       scw 		case 19200: rate = ftdi_sio_b19200; break;
    502      1.10       scw 		case 38400: rate = ftdi_sio_b38400; break;
    503      1.10       scw 		case 57600: rate = ftdi_sio_b57600; break;
    504      1.10       scw 		case 115200: rate = ftdi_sio_b115200; break;
    505      1.10       scw 		default:
    506      1.10       scw 			return (EINVAL);
    507      1.10       scw 		}
    508      1.10       scw 		break;
    509      1.10       scw 
    510      1.10       scw 	case UFTDI_TYPE_8U232AM:
    511      1.10       scw 		switch(t->c_ospeed) {
    512      1.10       scw 		case 300: rate = ftdi_8u232am_b300; break;
    513      1.10       scw 		case 600: rate = ftdi_8u232am_b600; break;
    514      1.10       scw 		case 1200: rate = ftdi_8u232am_b1200; break;
    515      1.10       scw 		case 2400: rate = ftdi_8u232am_b2400; break;
    516      1.10       scw 		case 4800: rate = ftdi_8u232am_b4800; break;
    517      1.10       scw 		case 9600: rate = ftdi_8u232am_b9600; break;
    518      1.10       scw 		case 19200: rate = ftdi_8u232am_b19200; break;
    519      1.10       scw 		case 38400: rate = ftdi_8u232am_b38400; break;
    520      1.10       scw 		case 57600: rate = ftdi_8u232am_b57600; break;
    521      1.10       scw 		case 115200: rate = ftdi_8u232am_b115200; break;
    522      1.10       scw 		case 230400: rate = ftdi_8u232am_b230400; break;
    523      1.10       scw 		case 460800: rate = ftdi_8u232am_b460800; break;
    524      1.10       scw 		case 921600: rate = ftdi_8u232am_b921600; break;
    525      1.10       scw 		default:
    526      1.10       scw 			return (EINVAL);
    527      1.10       scw 		}
    528      1.10       scw 		break;
    529      1.16   mycroft 
    530      1.15  christos 	default:
    531      1.16   mycroft 		return (EINVAL);
    532       1.1  augustss 	}
    533       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    534       1.1  augustss 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    535       1.1  augustss 	USETW(req.wValue, rate);
    536       1.1  augustss 	USETW(req.wIndex, portno);
    537       1.1  augustss 	USETW(req.wLength, 0);
    538       1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    539       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    540       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    541       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    542       1.1  augustss 	if (err)
    543       1.1  augustss 		return (EIO);
    544       1.1  augustss 
    545       1.1  augustss 	if (ISSET(t->c_cflag, CSTOPB))
    546       1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    547       1.1  augustss 	else
    548       1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    549       1.1  augustss 	if (ISSET(t->c_cflag, PARENB)) {
    550       1.1  augustss 		if (ISSET(t->c_cflag, PARODD))
    551       1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    552       1.1  augustss 		else
    553       1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    554       1.1  augustss 	} else
    555       1.1  augustss 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    556       1.1  augustss 	switch (ISSET(t->c_cflag, CSIZE)) {
    557       1.1  augustss 	case CS5:
    558       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(5);
    559       1.1  augustss 		break;
    560       1.1  augustss 	case CS6:
    561       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(6);
    562       1.1  augustss 		break;
    563       1.1  augustss 	case CS7:
    564       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(7);
    565       1.1  augustss 		break;
    566       1.1  augustss 	case CS8:
    567       1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(8);
    568       1.1  augustss 		break;
    569       1.1  augustss 	}
    570       1.8    ichiro 	sc->last_lcr = data;
    571       1.8    ichiro 
    572       1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    573       1.1  augustss 	req.bRequest = FTDI_SIO_SET_DATA;
    574       1.1  augustss 	USETW(req.wValue, data);
    575       1.1  augustss 	USETW(req.wIndex, portno);
    576       1.1  augustss 	USETW(req.wLength, 0);
    577       1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    578       1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    579       1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    580      1.12       scw 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    581      1.12       scw 	if (err)
    582      1.12       scw 		return (EIO);
    583      1.12       scw 
    584      1.12       scw 	if (ISSET(t->c_cflag, CRTSCTS)) {
    585      1.12       scw 		flow = FTDI_SIO_RTS_CTS_HS;
    586      1.12       scw 		USETW(req.wValue, 0);
    587      1.12       scw 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
    588      1.12       scw 		flow = FTDI_SIO_XON_XOFF_HS;
    589      1.12       scw 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    590      1.12       scw 	} else {
    591      1.12       scw 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    592      1.12       scw 		USETW(req.wValue, 0);
    593      1.12       scw 	}
    594      1.12       scw 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    595      1.12       scw 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    596      1.12       scw 	USETW2(req.wIndex, flow, portno);
    597      1.12       scw 	USETW(req.wLength, 0);
    598       1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    599       1.1  augustss 	if (err)
    600       1.1  augustss 		return (EIO);
    601       1.1  augustss 
    602       1.1  augustss 	return (0);
    603       1.1  augustss }
    604       1.1  augustss 
    605       1.1  augustss void
    606       1.2  augustss uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    607       1.1  augustss {
    608       1.1  augustss 	struct uftdi_softc *sc = vsc;
    609       1.1  augustss 
    610       1.1  augustss 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    611       1.1  augustss 		 sc->sc_msr, sc->sc_lsr));
    612       1.1  augustss 
    613       1.1  augustss 	if (msr != NULL)
    614       1.1  augustss 		*msr = sc->sc_msr;
    615       1.1  augustss 	if (lsr != NULL)
    616       1.1  augustss 		*lsr = sc->sc_lsr;
    617       1.8    ichiro }
    618       1.8    ichiro 
    619       1.8    ichiro void
    620       1.8    ichiro uftdi_break(void *vsc, int portno, int onoff)
    621       1.8    ichiro {
    622       1.8    ichiro 	struct uftdi_softc *sc = vsc;
    623       1.9    ichiro 	usb_device_request_t req;
    624       1.8    ichiro 	int data;
    625       1.8    ichiro 
    626       1.8    ichiro 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    627      1.11  augustss 		  onoff));
    628       1.8    ichiro 
    629       1.8    ichiro 	if (onoff) {
    630       1.8    ichiro 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    631       1.8    ichiro 	} else {
    632       1.8    ichiro 		data = sc->last_lcr;
    633       1.8    ichiro 	}
    634       1.8    ichiro 
    635       1.8    ichiro 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    636       1.8    ichiro 	req.bRequest = FTDI_SIO_SET_DATA;
    637       1.8    ichiro 	USETW(req.wValue, data);
    638       1.8    ichiro 	USETW(req.wIndex, portno);
    639       1.8    ichiro 	USETW(req.wLength, 0);
    640       1.8    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    641       1.1  augustss }
    642