From 31f48889ad8a6e14cc6d7a8272d3eaef3936ac71 Mon Sep 17 00:00:00 2001 From: Josef Karthauser Date: Sun, 11 Aug 2002 23:32:33 +0000 Subject: [PATCH] Add the uftdi ucom driver which supports the following adapters: Inland UAS111 QVS USC-1000 HP USB-Serial adapter shipped with some HP laptops Submitted by: takawata MFC After: 7 days --- share/man/man4/Makefile | 1 + share/man/man4/uftdi.4 | 80 +++++ sys/conf/files | 1 + sys/dev/usb/uftdi.c | 611 +++++++++++++++++++++++++++++++++++++ sys/dev/usb/uftdireg.h | 348 +++++++++++++++++++++ sys/modules/Makefile | 1 + sys/modules/uftdi/Makefile | 10 + 7 files changed, 1052 insertions(+) create mode 100644 share/man/man4/uftdi.4 create mode 100644 sys/dev/usb/uftdi.c create mode 100644 sys/dev/usb/uftdireg.h create mode 100644 sys/modules/uftdi/Makefile diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile index 879c85c4fe70..8ca16826579b 100644 --- a/share/man/man4/Makefile +++ b/share/man/man4/Makefile @@ -184,6 +184,7 @@ MAN= aac.4 \ ucom.4 \ udbp.4 \ udp.4 \ + uftdi.4 \ ugen.4 \ uhci.4 \ uhid.4 \ diff --git a/share/man/man4/uftdi.4 b/share/man/man4/uftdi.4 new file mode 100644 index 000000000000..c93889d67357 --- /dev/null +++ b/share/man/man4/uftdi.4 @@ -0,0 +1,80 @@ +.\" $NetBSD: uftdi.4,v 1.5 2002/02/07 03:15:08 ross Exp $ +.\" +.\" Copyright (c) 2000 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Lennart Augustsson. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the NetBSD +.\" Foundation, Inc. and its contributors. +.\" 4. Neither the name of The NetBSD Foundation nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd April 13, 2000 +.Dt UFTDI 4 +.Os +.Sh NAME +.Nm uftdi +.Nd USB support for serial adapters based on the FT8U100AX and FT8U232AM +.Sh SYNOPSIS +.Cd "uftdi* at uhub?" +.Cd "ucom* at uftdi?" +.Sh HARDWARE +The +.Nm +driver supports the following adapters: +.Pp +.Bl -item -offset indent -compact +.It +Inland UAS111 +.It +QVS USC-1000 +.It +HP USB-Serial adapter shipped with some HP laptops +.El +.Sh DESCRIPTION +The +.Nm +driver provides support for various serial adapters based on the FTDI +FT8U100AX and FT8U232AM chips. +.Pp +The device is accessed through the +.Xr ucom 4 +driver which makes it behave like a +.Xr tty 4 . +.Sh SEE ALSO +.Xr tty 4 , +.Xr ucom 4 , +.Xr usb 4 +.Sh HISTORY +The +.Nm +driver +appeared in +.Nx 1.5 . diff --git a/sys/conf/files b/sys/conf/files index fb0c8908cdaf..28f246b161f3 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -642,6 +642,7 @@ dev/usb/ohci.c optional ohci dev/usb/ucom.c optional ucom dev/usb/udbp.c optional udbp dev/usb/ufm.c optional ufm +dev/usb/uftdi.c optional uftdi ucom dev/usb/ugen.c optional ugen dev/usb/uhci.c optional uhci dev/usb/uhid.c optional uhid diff --git a/sys/dev/usb/uftdi.c b/sys/dev/usb/uftdi.c new file mode 100644 index 000000000000..c7179d4c5464 --- /dev/null +++ b/sys/dev/usb/uftdi.c @@ -0,0 +1,611 @@ +/* $NetBSD: uftdi.c,v 1.12 2002/07/18 14:44:10 scw Exp $ */ +/* $FreeBSD$ */ + +/* + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Lennart Augustsson (lennart@augustsson.net). + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * FTDI FT8U100AX serial adapter driver + */ + +/* + * XXX This driver will not support multiple serial ports. + * XXX The ucom layer needs to be extended first. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __FreeBSD_version >= 500014 +#include +#else +#include +#endif + +#include +#include + +#include +#include +#include + +#include + +#include + +#ifdef UFTDI_DEBUG +#define DPRINTF(x) if (uftdidebug) printf x +#define DPRINTFN(n,x) if (uftdidebug>(n)) printf x +int uftdidebug = 0; +#else +#define DPRINTF(x) +#define DPRINTFN(n,x) +#endif + +#define UFTDI_CONFIG_INDEX 0 +#define UFTDI_IFACE_INDEX 0 + + +/* + * These are the maximum number of bytes transferred per frame. + * The output buffer size cannot be increased due to the size encoding. + */ +#define UFTDIIBUFSIZE 64 +#define UFTDIOBUFSIZE 64 + +struct uftdi_softc { + struct ucom_softc sc_ucom; + + usbd_interface_handle sc_iface; /* interface */ + + enum uftdi_type sc_type; + u_int sc_hdrlen; + + u_char sc_msr; + u_char sc_lsr; + + u_char sc_dying; + + u_int last_lcr; +}; + +Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr); +Static void uftdi_set(void *, int, int, int); +Static int uftdi_param(void *, int, struct termios *); +Static int uftdi_open(void *sc, int portno); +Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count); +Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from, + u_int32_t *count); +Static void uftdi_break(void *sc, int portno, int onoff); + +struct ucom_callback uftdi_callback = { + uftdi_get_status, + uftdi_set, + uftdi_param, + NULL, + uftdi_open, + NULL, + uftdi_read, + uftdi_write, +}; + +USB_MATCH(uftdi) +{ + USB_MATCH_START(uftdi, uaa); + + if (uaa->iface != NULL) + return (UMATCH_NONE); + + DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n", + uaa->vendor, uaa->product)); + + if (uaa->vendor == USB_VENDOR_FTDI && + (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX || + uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM)) + return (UMATCH_VENDOR_PRODUCT); + + return (UMATCH_NONE); +} + +USB_ATTACH(uftdi) +{ + USB_ATTACH_START(uftdi, sc, uaa); + usbd_device_handle dev = uaa->device; + usbd_interface_handle iface; + usb_interface_descriptor_t *id; + usb_endpoint_descriptor_t *ed; + char *devinfo; + const char *devname; + int i; + usbd_status err; + struct ucom_softc *ucom = &sc->sc_ucom; + DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); + devinfo = malloc(1024, M_USBDEV, M_WAITOK); + + ucom->sc_dev = self; + ucom->sc_udev = dev; + + devname = USBDEVNAME(ucom->sc_dev); + + /* Move the device into the configured state. */ + err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1); + if (err) { + printf("\n%s: failed to set configuration, err=%s\n", + devname, usbd_errstr(err)); + goto bad; + } + + err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface); + if (err) { + printf("\n%s: failed to get interface, err=%s\n", + devname, usbd_errstr(err)); + goto bad; + } + + usbd_devinfo(dev, 0, devinfo); + /* USB_ATTACH_SETUP;*/ + printf("%s: %s\n", devname, devinfo); + + id = usbd_get_interface_descriptor(iface); + ucom->sc_iface = iface; + switch( uaa->product ){ + case USB_PRODUCT_FTDI_SERIAL_8U100AX: + sc->sc_type = UFTDI_TYPE_SIO; + sc->sc_hdrlen = 1; + break; + case USB_PRODUCT_FTDI_SERIAL_8U232AM: + sc->sc_type = UFTDI_TYPE_8U232AM; + sc->sc_hdrlen = 0; + break; + + default: /* Can't happen */ + goto bad; + } + + ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1; + + for (i = 0; i < id->bNumEndpoints; i++) { + int addr, dir, attr; + ed = usbd_interface2endpoint_descriptor(iface, i); + if (ed == NULL) { + printf("%s: could not read endpoint descriptor" + ": %s\n", devname, usbd_errstr(err)); + goto bad; + } + + addr = ed->bEndpointAddress; + dir = UE_GET_DIR(ed->bEndpointAddress); + attr = ed->bmAttributes & UE_XFERTYPE; + if (dir == UE_DIR_IN && attr == UE_BULK) + ucom->sc_bulkin_no = addr; + else if (dir == UE_DIR_OUT && attr == UE_BULK) + ucom->sc_bulkout_no = addr; + else { + printf("%s: unexpected endpoint\n", devname); + goto bad; + } + } + if (ucom->sc_bulkin_no == -1) { + printf("%s: Could not find data bulk in\n", + devname); + goto bad; + } + if (ucom->sc_bulkout_no == -1) { + printf("%s: Could not find data bulk out\n", + devname); + goto bad; + } + ucom->sc_parent = sc; + ucom->sc_portno = FTDI_PIT_SIOA; + /* bulkin, bulkout set above */ + + ucom->sc_ibufsize = UFTDIIBUFSIZE; + ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen; + ucom->sc_ibufsizepad = UFTDIIBUFSIZE; + ucom->sc_opkthdrlen = sc->sc_hdrlen; + + + ucom->sc_callback = &uftdi_callback; +#if 0 + usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev, + USBDEV(ucom->sc_dev)); +#endif + DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no)); + ucom_attach(&sc->sc_ucom); + free(devinfo, M_USBDEV); + + USB_ATTACH_SUCCESS_RETURN; + +bad: + DPRINTF(("uftdi_attach: ATTACH ERROR\n")); + ucom->sc_dying = 1; + free(devinfo, M_USBDEV); + + USB_ATTACH_ERROR_RETURN; +} +#if 0 +int +uftdi_activate(device_ptr_t self, enum devact act) +{ + struct uftdi_softc *sc = (struct uftdi_softc *)self; + int rv = 0; + + switch (act) { + case DVACT_ACTIVATE: + return (EOPNOTSUPP); + break; + + case DVACT_DEACTIVATE: + if (sc->sc_subdev != NULL) + rv = config_deactivate(sc->sc_subdev); + sc->sc_dying = 1; + break; + } + return (rv); +} +#endif +#if 1 +USB_DETACH(uftdi) +{ + USB_DETACH_START(uftdi, sc); + + int rv = 0; + + DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags)); + sc->sc_dying = 1; + rv = ucom_detach(&sc->sc_ucom); + + return rv; +} +#endif +Static int +uftdi_open(void *vsc, int portno) +{ + struct uftdi_softc *sc = vsc; + struct ucom_softc *ucom = (struct ucom_softc *) vsc; + usb_device_request_t req; + usbd_status err; + struct termios t; + + DPRINTF(("uftdi_open: sc=%p\n", sc)); + + if (sc->sc_dying) + return (EIO); + + /* Perform a full reset on the device */ + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_RESET; + USETW(req.wValue, FTDI_SIO_RESET_SIO); + USETW(req.wIndex, portno); + USETW(req.wLength, 0); + err = usbd_do_request(ucom->sc_udev, &req, NULL); + if (err) + return (EIO); + + /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ + t.c_ospeed = 9600; + t.c_cflag = CSTOPB | CS8; + (void)uftdi_param(sc, portno, &t); + + /* Turn on RTS/CTS flow control */ + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_SET_FLOW_CTRL; + USETW(req.wValue, 0); + USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); + USETW(req.wLength, 0); + err = usbd_do_request(ucom->sc_udev, &req, NULL); + if (err) + return (EIO); + + return (0); +} + +Static void +uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count) +{ + struct uftdi_softc *sc = vsc; + u_char msr, lsr; + + DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, + *count)); + + msr = FTDI_GET_MSR(*ptr); + lsr = FTDI_GET_LSR(*ptr); + +#ifdef UFTDI_DEBUG + if (*count != 2) + DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" + "0x%02x\n", sc, portno, *count, (*ptr)[2])); +#endif + + if (sc->sc_msr != msr || + (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { + DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " + "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, + lsr, sc->sc_lsr)); + sc->sc_msr = msr; + sc->sc_lsr = lsr; + ucom_status_change(&sc->sc_ucom); + } + + /* Pick up status and adjust data part. */ + *ptr += 2; + *count -= 2; +} + +Static void +uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count) +{ + struct uftdi_softc *sc = vsc; + + DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", + vsc, portno, *count, from[0])); + + /* Make length tag and copy data */ + if (sc->sc_hdrlen > 0) + *to = FTDI_OUT_TAG(*count, portno); + + memcpy(to + sc->sc_hdrlen, from, *count); + *count += sc->sc_hdrlen; +} + +Static void +uftdi_set(void *vsc, int portno, int reg, int onoff) +{ + struct uftdi_softc *sc = vsc; + struct ucom_softc *ucom = vsc; + usb_device_request_t req; + int ctl; + + DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, + reg, onoff)); + + switch (reg) { + case UCOM_SET_DTR: + ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; + break; + case UCOM_SET_RTS: + ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; + break; + case UCOM_SET_BREAK: + uftdi_break(sc, portno, onoff); + return; + default: + return; + } + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_MODEM_CTRL; + USETW(req.wValue, ctl); + USETW(req.wIndex, portno); + USETW(req.wLength, 0); + DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " + "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, + UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); + (void)usbd_do_request(ucom->sc_udev, &req, NULL); +} + +Static int +uftdi_param(void *vsc, int portno, struct termios *t) +{ + struct uftdi_softc *sc = vsc; + struct ucom_softc *ucom = vsc; + usb_device_request_t req; + usbd_status err; + int rate=0, data, flow; + + DPRINTF(("uftdi_param: sc=%p\n", sc)); + + if (sc->sc_dying) + return (EIO); + + switch (sc->sc_type) { + case UFTDI_TYPE_SIO: + switch (t->c_ospeed) { + case 300: rate = ftdi_sio_b300; break; + case 600: rate = ftdi_sio_b600; break; + case 1200: rate = ftdi_sio_b1200; break; + case 2400: rate = ftdi_sio_b2400; break; + case 4800: rate = ftdi_sio_b4800; break; + case 9600: rate = ftdi_sio_b9600; break; + case 19200: rate = ftdi_sio_b19200; break; + case 38400: rate = ftdi_sio_b38400; break; + case 57600: rate = ftdi_sio_b57600; break; + case 115200: rate = ftdi_sio_b115200; break; + default: + return (EINVAL); + } + break; + + case UFTDI_TYPE_8U232AM: + switch(t->c_ospeed) { + case 300: rate = ftdi_8u232am_b300; break; + case 600: rate = ftdi_8u232am_b600; break; + case 1200: rate = ftdi_8u232am_b1200; break; + case 2400: rate = ftdi_8u232am_b2400; break; + case 4800: rate = ftdi_8u232am_b4800; break; + case 9600: rate = ftdi_8u232am_b9600; break; + case 19200: rate = ftdi_8u232am_b19200; break; + case 38400: rate = ftdi_8u232am_b38400; break; + case 57600: rate = ftdi_8u232am_b57600; break; + case 115200: rate = ftdi_8u232am_b115200; break; + case 230400: rate = ftdi_8u232am_b230400; break; + case 460800: rate = ftdi_8u232am_b460800; break; + case 921600: rate = ftdi_8u232am_b921600; break; + default: + return (EINVAL); + } + break; + } + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_SET_BAUD_RATE; + USETW(req.wValue, rate); + USETW(req.wIndex, portno); + USETW(req.wLength, 0); + DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " + "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, + UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); + err = usbd_do_request(ucom->sc_udev, &req, NULL); + if (err) + return (EIO); + + if (ISSET(t->c_cflag, CSTOPB)) + data = FTDI_SIO_SET_DATA_STOP_BITS_2; + else + data = FTDI_SIO_SET_DATA_STOP_BITS_1; + if (ISSET(t->c_cflag, PARENB)) { + if (ISSET(t->c_cflag, PARODD)) + data |= FTDI_SIO_SET_DATA_PARITY_ODD; + else + data |= FTDI_SIO_SET_DATA_PARITY_EVEN; + } else + data |= FTDI_SIO_SET_DATA_PARITY_NONE; + switch (ISSET(t->c_cflag, CSIZE)) { + case CS5: + data |= FTDI_SIO_SET_DATA_BITS(5); + break; + case CS6: + data |= FTDI_SIO_SET_DATA_BITS(6); + break; + case CS7: + data |= FTDI_SIO_SET_DATA_BITS(7); + break; + case CS8: + data |= FTDI_SIO_SET_DATA_BITS(8); + break; + } + sc->last_lcr = data; + + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_SET_DATA; + USETW(req.wValue, data); + USETW(req.wIndex, portno); + USETW(req.wLength, 0); + DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " + "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, + UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); + err = usbd_do_request(ucom->sc_udev, &req, NULL); + if (err) + return (EIO); + + if (ISSET(t->c_cflag, CRTSCTS)) { + flow = FTDI_SIO_RTS_CTS_HS; + USETW(req.wValue, 0); + } else if (ISSET(t->c_iflag, IXON|IXOFF)) { + flow = FTDI_SIO_XON_XOFF_HS; + USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]); + } else { + flow = FTDI_SIO_DISABLE_FLOW_CTRL; + USETW(req.wValue, 0); + } + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_SET_FLOW_CTRL; + USETW2(req.wIndex, flow, portno); + USETW(req.wLength, 0); + err = usbd_do_request(ucom->sc_udev, &req, NULL); + if (err) + return (EIO); + + return (0); +} + +void +uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) +{ + struct uftdi_softc *sc = vsc; + + DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", + sc->sc_msr, sc->sc_lsr)); + + if (msr != NULL) + *msr = sc->sc_msr; + if (lsr != NULL) + *lsr = sc->sc_lsr; +} + +void +uftdi_break(void *vsc, int portno, int onoff) +{ + struct uftdi_softc *sc = vsc; + struct ucom_softc *ucom = vsc; + + usb_device_request_t req; + int data; + + DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno, + onoff)); + + if (onoff) { + data = sc->last_lcr | FTDI_SIO_SET_BREAK; + } else { + data = sc->last_lcr; + } + + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = FTDI_SIO_SET_DATA; + USETW(req.wValue, data); + USETW(req.wIndex, portno); + USETW(req.wLength, 0); + (void)usbd_do_request(ucom->sc_udev, &req, NULL); +} + +Static device_method_t uftdi_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, uftdi_match), + DEVMETHOD(device_attach, uftdi_attach), + DEVMETHOD(device_detach, uftdi_detach), + + { 0, 0 } +}; + +Static driver_t uftdi_driver = { + "uftdi", + uftdi_methods, + sizeof (struct uftdi_softc) +}; + +DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0); +MODULE_DEPEND(uftdi, usb, 1, 1, 1); +MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER); diff --git a/sys/dev/usb/uftdireg.h b/sys/dev/usb/uftdireg.h new file mode 100644 index 000000000000..15d47f9ef634 --- /dev/null +++ b/sys/dev/usb/uftdireg.h @@ -0,0 +1,348 @@ +/* $NetBSD: uftdireg.h,v 1.6 2002/07/11 21:14:28 augustss Exp $ */ +/* $FreeBSD$ */ + +/* + * Definitions for the FTDI USB Single Port Serial Converter - + * known as FTDI_SIO (Serial Input/Output application of the chipset) + * + * The device is based on the FTDI FT8U100AX chip. It has a DB25 on one side, + * USB on the other. + * + * Thanx to FTDI (http://www.ftdi.co.uk) for so kindly providing details + * of the protocol required to talk to the device and ongoing assistence + * during development. + * + * Bill Ryder - bryder@sgi.com of Silicon Graphics, Inc. is the original + * author of this file. + */ +/* Modified by Lennart Augustsson */ + +/* Vendor Request Interface */ +#define FTDI_SIO_RESET 0 /* Reset the port */ +#define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */ +#define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */ +#define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */ +#define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the port */ +#define FTDI_SIO_GET_STATUS 5 /* Retrieve current value of status reg */ +#define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */ +#define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */ + +/* Port Identifier Table */ +#define FTDI_PIT_DEFAULT 0 /* SIOA */ +#define FTDI_PIT_SIOA 1 /* SIOA */ +#define FTDI_PIT_SIOB 2 /* SIOB */ +#define FTDI_PIT_PARALLEL 3 /* Parallel */ + +enum uftdi_type { + UFTDI_TYPE_SIO, + UFTDI_TYPE_8U232AM +}; + +/* + * BmRequestType: 0100 0000B + * bRequest: FTDI_SIO_RESET + * wValue: Control Value + * 0 = Reset SIO + * 1 = Purge RX buffer + * 2 = Purge TX buffer + * wIndex: Port + * wLength: 0 + * Data: None + * + * The Reset SIO command has this effect: + * + * Sets flow control set to 'none' + * Event char = 0x0d + * Event trigger = disabled + * Purge RX buffer + * Purge TX buffer + * Clear DTR + * Clear RTS + * baud and data format not reset + * + * The Purge RX and TX buffer commands affect nothing except the buffers + * + */ +/* FTDI_SIO_RESET */ +#define FTDI_SIO_RESET_SIO 0 +#define FTDI_SIO_RESET_PURGE_RX 1 +#define FTDI_SIO_RESET_PURGE_TX 2 + + +/* + * BmRequestType: 0100 0000B + * bRequest: FTDI_SIO_SET_BAUDRATE + * wValue: BaudRate value - see below + * wIndex: Port + * wLength: 0 + * Data: None + */ +/* FTDI_SIO_SET_BAUDRATE */ +enum { + ftdi_sio_b300 = 0, + ftdi_sio_b600 = 1, + ftdi_sio_b1200 = 2, + ftdi_sio_b2400 = 3, + ftdi_sio_b4800 = 4, + ftdi_sio_b9600 = 5, + ftdi_sio_b19200 = 6, + ftdi_sio_b38400 = 7, + ftdi_sio_b57600 = 8, + ftdi_sio_b115200 = 9 +}; + +enum { + ftdi_8u232am_b300 = 0x2710, + ftdi_8u232am_b600 = 0x1388, + ftdi_8u232am_b1200 = 0x09c4, + ftdi_8u232am_b2400 = 0x04e2, + ftdi_8u232am_b4800 = 0x0271, + ftdi_8u232am_b9600 = 0x4138, + ftdi_8u232am_b19200 = 0x809c, + ftdi_8u232am_b38400 = 0xc04e, + ftdi_8u232am_b57600 = 0x0034, + ftdi_8u232am_b115200 = 0x001a, + ftdi_8u232am_b230400 = 0x000d, + ftdi_8u232am_b460800 = 0x4006, + ftdi_8u232am_b921600 = 0x8003 +}; + +/* + * BmRequestType: 0100 0000B + * bRequest: FTDI_SIO_SET_DATA + * wValue: Data characteristics (see below) + * wIndex: Port + * wLength: 0 + * Data: No + * + * Data characteristics + * + * B0..7 Number of data bits + * B8..10 Parity + * 0 = None + * 1 = Odd + * 2 = Even + * 3 = Mark + * 4 = Space + * B11..13 Stop Bits + * 0 = 1 + * 1 = 1.5 + * 2 = 2 + * B14..15 Reserved + * + */ +/* FTDI_SIO_SET_DATA */ +#define FTDI_SIO_SET_DATA_BITS(n) (n) +#define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8) +#define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8) +#define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8) +#define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8) +#define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8) +#define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11) +#define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11) +#define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11) +#define FTDI_SIO_SET_BREAK (0x1 << 14) + + +/* + * BmRequestType: 0100 0000B + * bRequest: FTDI_SIO_MODEM_CTRL + * wValue: ControlValue (see below) + * wIndex: Port + * wLength: 0 + * Data: None + * + * NOTE: If the device is in RTS/CTS flow control, the RTS set by this + * command will be IGNORED without an error being returned + * Also - you can not set DTR and RTS with one control message + * + * ControlValue + * B0 DTR state + * 0 = reset + * 1 = set + * B1 RTS state + * 0 = reset + * 1 = set + * B2..7 Reserved + * B8 DTR state enable + * 0 = ignore + * 1 = use DTR state + * B9 RTS state enable + * 0 = ignore + * 1 = use RTS state + * B10..15 Reserved + */ +/* FTDI_SIO_MODEM_CTRL */ +#define FTDI_SIO_SET_DTR_MASK 0x1 +#define FTDI_SIO_SET_DTR_HIGH (1 | ( FTDI_SIO_SET_DTR_MASK << 8)) +#define FTDI_SIO_SET_DTR_LOW (0 | ( FTDI_SIO_SET_DTR_MASK << 8)) +#define FTDI_SIO_SET_RTS_MASK 0x2 +#define FTDI_SIO_SET_RTS_HIGH (2 | ( FTDI_SIO_SET_RTS_MASK << 8)) +#define FTDI_SIO_SET_RTS_LOW (0 | ( FTDI_SIO_SET_RTS_MASK << 8)) + + +/* + * BmRequestType: 0100 0000b + * bRequest: FTDI_SIO_SET_FLOW_CTRL + * wValue: Xoff/Xon + * wIndex: Protocol/Port - hIndex is protocl / lIndex is port + * wLength: 0 + * Data: None + * + * hIndex protocol is: + * B0 Output handshaking using RTS/CTS + * 0 = disabled + * 1 = enabled + * B1 Output handshaking using DTR/DSR + * 0 = disabled + * 1 = enabled + * B2 Xon/Xoff handshaking + * 0 = disabled + * 1 = enabled + * + * A value of zero in the hIndex field disables handshaking + * + * If Xon/Xoff handshaking is specified, the hValue field should contain the + * XOFF character and the lValue field contains the XON character. + */ +/* FTDI_SIO_SET_FLOW_CTRL */ +#define FTDI_SIO_DISABLE_FLOW_CTRL 0x0 +#define FTDI_SIO_RTS_CTS_HS 0x1 +#define FTDI_SIO_DTR_DSR_HS 0x2 +#define FTDI_SIO_XON_XOFF_HS 0x4 + + +/* + * BmRequestType: 0100 0000b + * bRequest: FTDI_SIO_SET_EVENT_CHAR + * wValue: Event Char + * wIndex: Port + * wLength: 0 + * Data: None + * + * wValue: + * B0..7 Event Character + * B8 Event Character Processing + * 0 = disabled + * 1 = enabled + * B9..15 Reserved + * + * FTDI_SIO_SET_EVENT_CHAR + * + * Set the special event character for the specified communications port. + * If the device sees this character it will immediately return the + * data read so far - rather than wait 40ms or until 62 bytes are read + * which is what normally happens. + */ + + + +/* + * BmRequestType: 0100 0000b + * bRequest: FTDI_SIO_SET_ERROR_CHAR + * wValue: Error Char + * wIndex: Port + * wLength: 0 + * Data: None + * + * Error Char + * B0..7 Error Character + * B8 Error Character Processing + * 0 = disabled + * 1 = enabled + * B9..15 Reserved + * + * + * FTDI_SIO_SET_ERROR_CHAR + * Set the parity error replacement character for the specified communications + * port. + */ + + +/* + * BmRequestType: 1100 0000b + * bRequest: FTDI_SIO_GET_MODEM_STATUS + * wValue: zero + * wIndex: Port + * wLength: 1 + * Data: Status + * + * One byte of data is returned + * B0..3 0 + * B4 CTS + * 0 = inactive + * 1 = active + * B5 DSR + * 0 = inactive + * 1 = active + * B6 Ring Indicator (RI) + * 0 = inactive + * 1 = active + * B7 Receive Line Signal Detect (RLSD) + * 0 = inactive + * 1 = active + * + * FTDI_SIO_GET_MODEM_STATUS + * Retrieve the current value of the modem status register. + */ +#define FTDI_SIO_CTS_MASK 0x10 +#define FTDI_SIO_DSR_MASK 0x20 +#define FTDI_SIO_RI_MASK 0x40 +#define FTDI_SIO_RLSD_MASK 0x80 + + + +/* + * + * DATA FORMAT + * + * IN Endpoint + * + * The device reserves the first two bytes of data on this endpoint to contain + * the current values of the modem and line status registers. In the absence of + * data, the device generates a message consisting of these two status bytes + * every 40 ms. + * + * Byte 0: Modem Status + * NOTE: 4 upper bits have same layout as the MSR register in a 16550 + * + * Offset Description + * B0..3 Port + * B4 Clear to Send (CTS) + * B5 Data Set Ready (DSR) + * B6 Ring Indicator (RI) + * B7 Receive Line Signal Detect (RLSD) + * + * Byte 1: Line Status + * NOTE: same layout as the LSR register in a 16550 + * + * Offset Description + * B0 Data Ready (DR) + * B1 Overrun Error (OE) + * B2 Parity Error (PE) + * B3 Framing Error (FE) + * B4 Break Interrupt (BI) + * B5 Transmitter Holding Register (THRE) + * B6 Transmitter Empty (TEMT) + * B7 Error in RCVR FIFO + * + * + * OUT Endpoint + * + * This device reserves the first bytes of data on this endpoint contain the + * length and port identifier of the message. For the FTDI USB Serial converter + * the port identifier is always 1. + * + * Byte 0: Port & length + * + * Offset Description + * B0..1 Port + * B2..7 Length of message - (not including Byte 0) + * + */ +#define FTDI_PORT_MASK 0x0f +#define FTDI_MSR_MASK 0xf0 +#define FTDI_GET_MSR(p) (((p)[0]) & FTDI_MSR_MASK) +#define FTDI_GET_LSR(p) ((p)[1]) +#define FTDI_LSR_MASK (~0x60) /* interesting bits */ +#define FTDI_OUT_TAG(len, port) (((len) << 2) | (port)) diff --git a/sys/modules/Makefile b/sys/modules/Makefile index 0db326512323..29ad973d2142 100644 --- a/sys/modules/Makefile +++ b/sys/modules/Makefile @@ -94,6 +94,7 @@ SUBDIR= 3dfx \ udbp \ ufm \ udf \ + uftdi \ ugen \ uhid \ ukbd \ diff --git a/sys/modules/uftdi/Makefile b/sys/modules/uftdi/Makefile new file mode 100644 index 000000000000..7a942338f4a6 --- /dev/null +++ b/sys/modules/uftdi/Makefile @@ -0,0 +1,10 @@ +# $FreeBSD$ + +S= ${.CURDIR}/../.. +.PATH: $S/dev/usb + +KMOD= uftdi +SRCS= uftdi.c uftdireg.h opt_usb.h device_if.h bus_if.h vnode_if.h +NOMAN= + +.include