Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.23.10.1
      1  1.23.10.1    bouyer /*	$NetBSD: uftdi.c,v 1.23.10.1 2007/03/10 11:44:24 bouyer 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.1  augustss /*
     44        1.1  augustss  * XXX This driver will not support multiple serial ports.
     45        1.1  augustss  * XXX The ucom layer needs to be extended first.
     46        1.1  augustss  */
     47        1.7     lukem 
     48        1.7     lukem #include <sys/cdefs.h>
     49  1.23.10.1    bouyer __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.23.10.1 2007/03/10 11:44:24 bouyer Exp $");
     50        1.1  augustss 
     51        1.1  augustss #include <sys/param.h>
     52        1.1  augustss #include <sys/systm.h>
     53        1.1  augustss #include <sys/kernel.h>
     54        1.1  augustss #include <sys/device.h>
     55        1.1  augustss #include <sys/conf.h>
     56        1.1  augustss #include <sys/tty.h>
     57        1.1  augustss 
     58        1.1  augustss #include <dev/usb/usb.h>
     59        1.1  augustss #include <dev/usb/usbhid.h>
     60        1.1  augustss 
     61        1.1  augustss #include <dev/usb/usbdi.h>
     62        1.1  augustss #include <dev/usb/usbdi_util.h>
     63        1.1  augustss #include <dev/usb/usbdevs.h>
     64        1.1  augustss 
     65        1.1  augustss #include <dev/usb/ucomvar.h>
     66        1.1  augustss 
     67        1.1  augustss #include <dev/usb/uftdireg.h>
     68        1.1  augustss 
     69        1.1  augustss #ifdef UFTDI_DEBUG
     70        1.1  augustss #define DPRINTF(x)	if (uftdidebug) printf x
     71        1.1  augustss #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
     72        1.4  augustss int uftdidebug = 0;
     73        1.1  augustss #else
     74        1.1  augustss #define DPRINTF(x)
     75        1.1  augustss #define DPRINTFN(n,x)
     76        1.1  augustss #endif
     77        1.1  augustss 
     78        1.1  augustss #define UFTDI_CONFIG_INDEX	0
     79        1.1  augustss #define UFTDI_IFACE_INDEX	0
     80        1.1  augustss 
     81        1.1  augustss 
     82        1.1  augustss /*
     83        1.1  augustss  * These are the maximum number of bytes transferred per frame.
     84        1.1  augustss  * The output buffer size cannot be increased due to the size encoding.
     85        1.1  augustss  */
     86        1.1  augustss #define UFTDIIBUFSIZE 64
     87        1.1  augustss #define UFTDIOBUFSIZE 64
     88        1.1  augustss 
     89        1.1  augustss struct uftdi_softc {
     90        1.1  augustss 	USBBASEDEVICE		sc_dev;		/* base device */
     91        1.1  augustss 	usbd_device_handle	sc_udev;	/* device */
     92        1.1  augustss 	usbd_interface_handle	sc_iface;	/* interface */
     93        1.1  augustss 
     94       1.10       scw 	enum uftdi_type		sc_type;
     95       1.10       scw 	u_int			sc_hdrlen;
     96       1.10       scw 
     97        1.1  augustss 	u_char			sc_msr;
     98        1.1  augustss 	u_char			sc_lsr;
     99        1.1  augustss 
    100        1.1  augustss 	device_ptr_t		sc_subdev;
    101        1.1  augustss 
    102        1.1  augustss 	u_char			sc_dying;
    103        1.8    ichiro 
    104        1.8    ichiro 	u_int			last_lcr;
    105        1.1  augustss };
    106        1.1  augustss 
    107        1.2  augustss Static void	uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
    108        1.2  augustss Static void	uftdi_set(void *, int, int, int);
    109        1.2  augustss Static int	uftdi_param(void *, int, struct termios *);
    110        1.2  augustss Static int	uftdi_open(void *sc, int portno);
    111        1.2  augustss Static void	uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
    112        1.2  augustss Static void	uftdi_write(void *sc, int portno, u_char *to, u_char *from,
    113        1.2  augustss 			    u_int32_t *count);
    114        1.8    ichiro Static void	uftdi_break(void *sc, int portno, int onoff);
    115        1.1  augustss 
    116        1.1  augustss struct ucom_methods uftdi_methods = {
    117        1.1  augustss 	uftdi_get_status,
    118        1.1  augustss 	uftdi_set,
    119        1.1  augustss 	uftdi_param,
    120        1.1  augustss 	NULL,
    121        1.1  augustss 	uftdi_open,
    122        1.1  augustss 	NULL,
    123        1.1  augustss 	uftdi_read,
    124        1.1  augustss 	uftdi_write,
    125        1.1  augustss };
    126        1.1  augustss 
    127        1.1  augustss USB_DECLARE_DRIVER(uftdi);
    128        1.1  augustss 
    129        1.1  augustss USB_MATCH(uftdi)
    130        1.1  augustss {
    131        1.1  augustss 	USB_MATCH_START(uftdi, uaa);
    132       1.11  augustss 
    133        1.1  augustss 	if (uaa->iface != NULL)
    134        1.1  augustss 		return (UMATCH_NONE);
    135        1.1  augustss 
    136        1.1  augustss 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
    137        1.1  augustss 		     uaa->vendor, uaa->product));
    138        1.1  augustss 
    139        1.1  augustss 	if (uaa->vendor == USB_VENDOR_FTDI &&
    140       1.10       scw 	    (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
    141       1.17  augustss 	     uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM ||
    142  1.23.10.1    bouyer 	     uaa->product == USB_PRODUCT_FTDI_SERIAL_2232C ||
    143       1.18  augustss 	     uaa->product == USB_PRODUCT_FTDI_SEMC_DSS20 ||
    144       1.19  augustss 	     uaa->product == USB_PRODUCT_FTDI_LCD_LK202_24_USB ||
    145       1.20  augustss 	     uaa->product == USB_PRODUCT_FTDI_LCD_MX200_USB ||
    146       1.20  augustss 	     uaa->product == USB_PRODUCT_FTDI_CFA_631))
    147        1.1  augustss 		return (UMATCH_VENDOR_PRODUCT);
    148        1.1  augustss 
    149       1.23       scw 	if (uaa->vendor == USB_VENDOR_SEALEVEL &&
    150       1.23       scw 	    uaa->product == USB_PRODUCT_SEALEVEL_USBSERIAL)
    151       1.23       scw 		return (UMATCH_VENDOR_PRODUCT);
    152       1.23       scw 
    153        1.1  augustss 	return (UMATCH_NONE);
    154        1.1  augustss }
    155        1.1  augustss 
    156        1.1  augustss USB_ATTACH(uftdi)
    157        1.1  augustss {
    158        1.1  augustss 	USB_ATTACH_START(uftdi, sc, uaa);
    159        1.1  augustss 	usbd_device_handle dev = uaa->device;
    160        1.1  augustss 	usbd_interface_handle iface;
    161        1.1  augustss 	usb_interface_descriptor_t *id;
    162        1.1  augustss 	usb_endpoint_descriptor_t *ed;
    163        1.1  augustss 	char devinfo[1024];
    164        1.1  augustss 	char *devname = USBDEVNAME(sc->sc_dev);
    165        1.1  augustss 	int i;
    166        1.1  augustss 	usbd_status err;
    167        1.1  augustss 	struct ucom_attach_args uca;
    168        1.1  augustss 
    169        1.1  augustss 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
    170        1.1  augustss 
    171        1.1  augustss 	/* Move the device into the configured state. */
    172        1.1  augustss 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
    173        1.1  augustss 	if (err) {
    174        1.1  augustss 		printf("\n%s: failed to set configuration, err=%s\n",
    175        1.1  augustss 		       devname, usbd_errstr(err));
    176        1.1  augustss 		goto bad;
    177        1.1  augustss 	}
    178        1.1  augustss 
    179        1.1  augustss 	err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
    180        1.1  augustss 	if (err) {
    181        1.1  augustss 		printf("\n%s: failed to get interface, err=%s\n",
    182        1.1  augustss 		       devname, usbd_errstr(err));
    183        1.1  augustss 		goto bad;
    184        1.1  augustss 	}
    185        1.1  augustss 
    186       1.21    itojun 	usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
    187        1.1  augustss 	USB_ATTACH_SETUP;
    188        1.1  augustss 	printf("%s: %s\n", devname, devinfo);
    189        1.1  augustss 
    190        1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    191        1.1  augustss 
    192        1.1  augustss 	sc->sc_udev = dev;
    193        1.1  augustss 	sc->sc_iface = iface;
    194        1.1  augustss 
    195       1.10       scw 	switch (uaa->product) {
    196       1.10       scw 	case USB_PRODUCT_FTDI_SERIAL_8U100AX:
    197       1.10       scw 		sc->sc_type = UFTDI_TYPE_SIO;
    198       1.10       scw 		sc->sc_hdrlen = 1;
    199       1.10       scw 		break;
    200       1.10       scw 
    201       1.18  augustss 	case USB_PRODUCT_FTDI_SEMC_DSS20:
    202       1.10       scw 	case USB_PRODUCT_FTDI_SERIAL_8U232AM:
    203       1.17  augustss 	case USB_PRODUCT_FTDI_LCD_LK202_24_USB:
    204       1.19  augustss 	case USB_PRODUCT_FTDI_LCD_MX200_USB:
    205       1.20  augustss 	case USB_PRODUCT_FTDI_CFA_631:
    206       1.23       scw 	case USB_PRODUCT_SEALEVEL_USBSERIAL:
    207       1.10       scw 		sc->sc_type = UFTDI_TYPE_8U232AM;
    208       1.10       scw 		sc->sc_hdrlen = 0;
    209       1.10       scw 		break;
    210       1.10       scw 
    211       1.10       scw 	default:		/* Can't happen */
    212       1.10       scw 		goto bad;
    213       1.10       scw 	}
    214       1.10       scw 
    215        1.1  augustss 	uca.bulkin = uca.bulkout = -1;
    216        1.1  augustss 	for (i = 0; i < id->bNumEndpoints; i++) {
    217        1.1  augustss 		int addr, dir, attr;
    218        1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    219        1.1  augustss 		if (ed == NULL) {
    220        1.1  augustss 			printf("%s: could not read endpoint descriptor"
    221        1.1  augustss 			       ": %s\n", devname, usbd_errstr(err));
    222        1.1  augustss 			goto bad;
    223        1.1  augustss 		}
    224       1.11  augustss 
    225        1.1  augustss 		addr = ed->bEndpointAddress;
    226        1.1  augustss 		dir = UE_GET_DIR(ed->bEndpointAddress);
    227        1.1  augustss 		attr = ed->bmAttributes & UE_XFERTYPE;
    228        1.1  augustss 		if (dir == UE_DIR_IN && attr == UE_BULK)
    229        1.1  augustss 			uca.bulkin = addr;
    230        1.1  augustss 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
    231        1.1  augustss 			uca.bulkout = addr;
    232        1.1  augustss 		else {
    233        1.1  augustss 			printf("%s: unexpected endpoint\n", devname);
    234        1.1  augustss 			goto bad;
    235        1.1  augustss 		}
    236        1.1  augustss 	}
    237        1.1  augustss 	if (uca.bulkin == -1) {
    238        1.1  augustss 		printf("%s: Could not find data bulk in\n",
    239        1.1  augustss 		       USBDEVNAME(sc->sc_dev));
    240        1.1  augustss 		goto bad;
    241        1.1  augustss 	}
    242        1.1  augustss 	if (uca.bulkout == -1) {
    243        1.1  augustss 		printf("%s: Could not find data bulk out\n",
    244        1.1  augustss 		       USBDEVNAME(sc->sc_dev));
    245        1.1  augustss 		goto bad;
    246        1.1  augustss 	}
    247       1.11  augustss 
    248        1.1  augustss 	uca.portno = FTDI_PIT_SIOA;
    249        1.1  augustss 	/* bulkin, bulkout set above */
    250        1.1  augustss 	uca.ibufsize = UFTDIIBUFSIZE;
    251       1.10       scw 	uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
    252        1.1  augustss 	uca.ibufsizepad = UFTDIIBUFSIZE;
    253       1.10       scw 	uca.opkthdrlen = sc->sc_hdrlen;
    254        1.1  augustss 	uca.device = dev;
    255        1.1  augustss 	uca.iface = iface;
    256        1.1  augustss 	uca.methods = &uftdi_methods;
    257        1.1  augustss 	uca.arg = sc;
    258        1.6  augustss 	uca.info = NULL;
    259        1.1  augustss 
    260        1.5  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    261        1.5  augustss 			   USBDEV(sc->sc_dev));
    262        1.5  augustss 
    263        1.1  augustss 	DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    264       1.22  drochner 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    265       1.22  drochner 					    ucomprint, ucomsubmatch);
    266        1.1  augustss 
    267        1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    268        1.1  augustss 
    269        1.1  augustss bad:
    270        1.1  augustss 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    271        1.1  augustss 	sc->sc_dying = 1;
    272        1.1  augustss 	USB_ATTACH_ERROR_RETURN;
    273        1.1  augustss }
    274        1.1  augustss 
    275        1.1  augustss int
    276        1.2  augustss uftdi_activate(device_ptr_t self, enum devact act)
    277        1.1  augustss {
    278        1.1  augustss 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
    279        1.1  augustss 	int rv = 0;
    280        1.1  augustss 
    281        1.1  augustss 	switch (act) {
    282        1.1  augustss 	case DVACT_ACTIVATE:
    283        1.1  augustss 		return (EOPNOTSUPP);
    284        1.1  augustss 
    285        1.1  augustss 	case DVACT_DEACTIVATE:
    286        1.1  augustss 		if (sc->sc_subdev != NULL)
    287        1.1  augustss 			rv = config_deactivate(sc->sc_subdev);
    288        1.1  augustss 		sc->sc_dying = 1;
    289        1.1  augustss 		break;
    290        1.1  augustss 	}
    291        1.1  augustss 	return (rv);
    292        1.1  augustss }
    293        1.1  augustss 
    294        1.1  augustss int
    295        1.2  augustss uftdi_detach(device_ptr_t self, int flags)
    296        1.1  augustss {
    297        1.1  augustss 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
    298        1.1  augustss 
    299        1.1  augustss 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    300        1.1  augustss 	sc->sc_dying = 1;
    301        1.1  augustss 	if (sc->sc_subdev != NULL) {
    302       1.14    simonb 		config_detach(sc->sc_subdev, flags);
    303        1.1  augustss 		sc->sc_subdev = NULL;
    304        1.1  augustss 	}
    305        1.5  augustss 
    306        1.5  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    307        1.5  augustss 			   USBDEV(sc->sc_dev));
    308        1.5  augustss 
    309        1.1  augustss 	return (0);
    310        1.1  augustss }
    311        1.1  augustss 
    312        1.1  augustss Static int
    313        1.2  augustss uftdi_open(void *vsc, int portno)
    314        1.1  augustss {
    315        1.1  augustss 	struct uftdi_softc *sc = vsc;
    316        1.1  augustss 	usb_device_request_t req;
    317        1.1  augustss 	usbd_status err;
    318        1.1  augustss 	struct termios t;
    319        1.1  augustss 
    320        1.1  augustss 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    321        1.1  augustss 
    322        1.1  augustss 	if (sc->sc_dying)
    323        1.1  augustss 		return (EIO);
    324        1.1  augustss 
    325        1.1  augustss 	/* Perform a full reset on the device */
    326        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    327        1.1  augustss 	req.bRequest = FTDI_SIO_RESET;
    328        1.1  augustss 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    329        1.1  augustss 	USETW(req.wIndex, portno);
    330        1.1  augustss 	USETW(req.wLength, 0);
    331        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    332        1.1  augustss 	if (err)
    333        1.1  augustss 		return (EIO);
    334        1.1  augustss 
    335        1.1  augustss 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    336        1.1  augustss 	t.c_ospeed = 9600;
    337        1.1  augustss 	t.c_cflag = CSTOPB | CS8;
    338        1.1  augustss 	(void)uftdi_param(sc, portno, &t);
    339        1.1  augustss 
    340        1.1  augustss 	/* Turn on RTS/CTS flow control */
    341        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    342        1.1  augustss 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    343        1.1  augustss 	USETW(req.wValue, 0);
    344        1.1  augustss 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    345        1.1  augustss 	USETW(req.wLength, 0);
    346        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    347        1.1  augustss 	if (err)
    348        1.1  augustss 		return (EIO);
    349        1.1  augustss 
    350        1.1  augustss 	return (0);
    351        1.1  augustss }
    352        1.1  augustss 
    353        1.1  augustss Static void
    354        1.2  augustss uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
    355        1.1  augustss {
    356        1.1  augustss 	struct uftdi_softc *sc = vsc;
    357        1.1  augustss 	u_char msr, lsr;
    358        1.1  augustss 
    359        1.1  augustss 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    360        1.1  augustss 		     *count));
    361        1.1  augustss 
    362        1.1  augustss 	msr = FTDI_GET_MSR(*ptr);
    363        1.1  augustss 	lsr = FTDI_GET_LSR(*ptr);
    364        1.1  augustss 
    365        1.1  augustss #ifdef UFTDI_DEBUG
    366        1.1  augustss 	if (*count != 2)
    367        1.1  augustss 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    368        1.1  augustss 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    369        1.1  augustss #endif
    370        1.1  augustss 
    371        1.1  augustss 	if (sc->sc_msr != msr ||
    372        1.1  augustss 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    373        1.1  augustss 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    374        1.1  augustss 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    375        1.1  augustss 			 lsr, sc->sc_lsr));
    376        1.1  augustss 		sc->sc_msr = msr;
    377        1.1  augustss 		sc->sc_lsr = lsr;
    378        1.1  augustss 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    379        1.1  augustss 	}
    380        1.1  augustss 
    381        1.1  augustss 	/* Pick up status and adjust data part. */
    382        1.1  augustss 	*ptr += 2;
    383        1.1  augustss 	*count -= 2;
    384        1.1  augustss }
    385        1.1  augustss 
    386        1.1  augustss Static void
    387        1.2  augustss uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
    388        1.1  augustss {
    389       1.10       scw 	struct uftdi_softc *sc = vsc;
    390       1.10       scw 
    391        1.1  augustss 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    392        1.1  augustss 		     vsc, portno, *count, from[0]));
    393        1.1  augustss 
    394        1.1  augustss 	/* Make length tag and copy data */
    395       1.10       scw 	if (sc->sc_hdrlen > 0)
    396       1.10       scw 		*to = FTDI_OUT_TAG(*count, portno);
    397       1.10       scw 
    398       1.10       scw 	memcpy(to + sc->sc_hdrlen, from, *count);
    399       1.10       scw 	*count += sc->sc_hdrlen;
    400        1.1  augustss }
    401        1.1  augustss 
    402        1.1  augustss Static void
    403        1.2  augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
    404        1.1  augustss {
    405        1.1  augustss 	struct uftdi_softc *sc = vsc;
    406        1.1  augustss 	usb_device_request_t req;
    407        1.1  augustss 	int ctl;
    408        1.1  augustss 
    409        1.1  augustss 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    410        1.1  augustss 		 reg, onoff));
    411        1.1  augustss 
    412        1.1  augustss 	switch (reg) {
    413        1.1  augustss 	case UCOM_SET_DTR:
    414        1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    415        1.1  augustss 		break;
    416        1.1  augustss 	case UCOM_SET_RTS:
    417        1.1  augustss 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    418        1.1  augustss 		break;
    419        1.1  augustss 	case UCOM_SET_BREAK:
    420        1.8    ichiro 		uftdi_break(sc, portno, onoff);
    421        1.1  augustss 		return;
    422        1.1  augustss 	default:
    423        1.1  augustss 		return;
    424        1.1  augustss 	}
    425        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    426        1.1  augustss 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    427        1.1  augustss 	USETW(req.wValue, ctl);
    428        1.1  augustss 	USETW(req.wIndex, portno);
    429        1.1  augustss 	USETW(req.wLength, 0);
    430        1.1  augustss 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    431        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    432        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    433        1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    434        1.1  augustss }
    435        1.1  augustss 
    436        1.1  augustss Static int
    437        1.2  augustss uftdi_param(void *vsc, int portno, struct termios *t)
    438        1.1  augustss {
    439        1.1  augustss 	struct uftdi_softc *sc = vsc;
    440        1.1  augustss 	usb_device_request_t req;
    441        1.1  augustss 	usbd_status err;
    442       1.12       scw 	int rate, data, flow;
    443        1.1  augustss 
    444        1.1  augustss 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    445        1.1  augustss 
    446        1.1  augustss 	if (sc->sc_dying)
    447        1.1  augustss 		return (EIO);
    448        1.1  augustss 
    449       1.10       scw 	switch (sc->sc_type) {
    450       1.10       scw 	case UFTDI_TYPE_SIO:
    451       1.10       scw 		switch (t->c_ospeed) {
    452       1.10       scw 		case 300: rate = ftdi_sio_b300; break;
    453       1.10       scw 		case 600: rate = ftdi_sio_b600; break;
    454       1.10       scw 		case 1200: rate = ftdi_sio_b1200; break;
    455       1.10       scw 		case 2400: rate = ftdi_sio_b2400; break;
    456       1.10       scw 		case 4800: rate = ftdi_sio_b4800; break;
    457       1.10       scw 		case 9600: rate = ftdi_sio_b9600; break;
    458       1.10       scw 		case 19200: rate = ftdi_sio_b19200; break;
    459       1.10       scw 		case 38400: rate = ftdi_sio_b38400; break;
    460       1.10       scw 		case 57600: rate = ftdi_sio_b57600; break;
    461       1.10       scw 		case 115200: rate = ftdi_sio_b115200; break;
    462       1.10       scw 		default:
    463       1.10       scw 			return (EINVAL);
    464       1.10       scw 		}
    465       1.10       scw 		break;
    466       1.10       scw 
    467       1.10       scw 	case UFTDI_TYPE_8U232AM:
    468       1.10       scw 		switch(t->c_ospeed) {
    469       1.10       scw 		case 300: rate = ftdi_8u232am_b300; break;
    470       1.10       scw 		case 600: rate = ftdi_8u232am_b600; break;
    471       1.10       scw 		case 1200: rate = ftdi_8u232am_b1200; break;
    472       1.10       scw 		case 2400: rate = ftdi_8u232am_b2400; break;
    473       1.10       scw 		case 4800: rate = ftdi_8u232am_b4800; break;
    474       1.10       scw 		case 9600: rate = ftdi_8u232am_b9600; break;
    475       1.10       scw 		case 19200: rate = ftdi_8u232am_b19200; break;
    476       1.10       scw 		case 38400: rate = ftdi_8u232am_b38400; break;
    477       1.10       scw 		case 57600: rate = ftdi_8u232am_b57600; break;
    478       1.10       scw 		case 115200: rate = ftdi_8u232am_b115200; break;
    479       1.10       scw 		case 230400: rate = ftdi_8u232am_b230400; break;
    480       1.10       scw 		case 460800: rate = ftdi_8u232am_b460800; break;
    481       1.10       scw 		case 921600: rate = ftdi_8u232am_b921600; break;
    482       1.10       scw 		default:
    483       1.10       scw 			return (EINVAL);
    484       1.10       scw 		}
    485       1.10       scw 		break;
    486       1.16   mycroft 
    487       1.15  christos 	default:
    488       1.16   mycroft 		return (EINVAL);
    489        1.1  augustss 	}
    490        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    491        1.1  augustss 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    492        1.1  augustss 	USETW(req.wValue, rate);
    493        1.1  augustss 	USETW(req.wIndex, portno);
    494        1.1  augustss 	USETW(req.wLength, 0);
    495        1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    496        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    497        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    498        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    499        1.1  augustss 	if (err)
    500        1.1  augustss 		return (EIO);
    501        1.1  augustss 
    502        1.1  augustss 	if (ISSET(t->c_cflag, CSTOPB))
    503        1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    504        1.1  augustss 	else
    505        1.1  augustss 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    506        1.1  augustss 	if (ISSET(t->c_cflag, PARENB)) {
    507        1.1  augustss 		if (ISSET(t->c_cflag, PARODD))
    508        1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    509        1.1  augustss 		else
    510        1.1  augustss 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    511        1.1  augustss 	} else
    512        1.1  augustss 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    513        1.1  augustss 	switch (ISSET(t->c_cflag, CSIZE)) {
    514        1.1  augustss 	case CS5:
    515        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(5);
    516        1.1  augustss 		break;
    517        1.1  augustss 	case CS6:
    518        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(6);
    519        1.1  augustss 		break;
    520        1.1  augustss 	case CS7:
    521        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(7);
    522        1.1  augustss 		break;
    523        1.1  augustss 	case CS8:
    524        1.1  augustss 		data |= FTDI_SIO_SET_DATA_BITS(8);
    525        1.1  augustss 		break;
    526        1.1  augustss 	}
    527        1.8    ichiro 	sc->last_lcr = data;
    528        1.8    ichiro 
    529        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    530        1.1  augustss 	req.bRequest = FTDI_SIO_SET_DATA;
    531        1.1  augustss 	USETW(req.wValue, data);
    532        1.1  augustss 	USETW(req.wIndex, portno);
    533        1.1  augustss 	USETW(req.wLength, 0);
    534        1.1  augustss 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    535        1.1  augustss 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    536        1.1  augustss 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    537       1.12       scw 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    538       1.12       scw 	if (err)
    539       1.12       scw 		return (EIO);
    540       1.12       scw 
    541       1.12       scw 	if (ISSET(t->c_cflag, CRTSCTS)) {
    542       1.12       scw 		flow = FTDI_SIO_RTS_CTS_HS;
    543       1.12       scw 		USETW(req.wValue, 0);
    544       1.12       scw 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
    545       1.12       scw 		flow = FTDI_SIO_XON_XOFF_HS;
    546       1.12       scw 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
    547       1.12       scw 	} else {
    548       1.12       scw 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
    549       1.12       scw 		USETW(req.wValue, 0);
    550       1.12       scw 	}
    551       1.12       scw 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    552       1.12       scw 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    553       1.12       scw 	USETW2(req.wIndex, flow, portno);
    554       1.12       scw 	USETW(req.wLength, 0);
    555        1.1  augustss 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    556        1.1  augustss 	if (err)
    557        1.1  augustss 		return (EIO);
    558        1.1  augustss 
    559        1.1  augustss 	return (0);
    560        1.1  augustss }
    561        1.1  augustss 
    562        1.1  augustss void
    563        1.2  augustss uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    564        1.1  augustss {
    565        1.1  augustss 	struct uftdi_softc *sc = vsc;
    566        1.1  augustss 
    567        1.1  augustss 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    568        1.1  augustss 		 sc->sc_msr, sc->sc_lsr));
    569        1.1  augustss 
    570        1.1  augustss 	if (msr != NULL)
    571        1.1  augustss 		*msr = sc->sc_msr;
    572        1.1  augustss 	if (lsr != NULL)
    573        1.1  augustss 		*lsr = sc->sc_lsr;
    574        1.8    ichiro }
    575        1.8    ichiro 
    576        1.8    ichiro void
    577        1.8    ichiro uftdi_break(void *vsc, int portno, int onoff)
    578        1.8    ichiro {
    579        1.8    ichiro 	struct uftdi_softc *sc = vsc;
    580        1.9    ichiro 	usb_device_request_t req;
    581        1.8    ichiro 	int data;
    582        1.8    ichiro 
    583        1.8    ichiro 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
    584       1.11  augustss 		  onoff));
    585        1.8    ichiro 
    586        1.8    ichiro 	if (onoff) {
    587        1.8    ichiro 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
    588        1.8    ichiro 	} else {
    589        1.8    ichiro 		data = sc->last_lcr;
    590        1.8    ichiro 	}
    591        1.8    ichiro 
    592        1.8    ichiro 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    593        1.8    ichiro 	req.bRequest = FTDI_SIO_SET_DATA;
    594        1.8    ichiro 	USETW(req.wValue, data);
    595        1.8    ichiro 	USETW(req.wIndex, portno);
    596        1.8    ichiro 	USETW(req.wLength, 0);
    597        1.8    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    598        1.1  augustss }
    599