From a0465416233e9c948e8c8d5d05d7fa89aab5f188 Mon Sep 17 00:00:00 2001 From: Ian Lepore Date: Thu, 6 Aug 2015 19:47:04 +0000 Subject: [PATCH] Return the current ftdi bitbang mode with the UFTDIIOC_GET_BITMODE ioctl. The ftdi chip itself has a "get bitmode" command that doesn't actually return the current bitmode, just a snapshot of the gpio lines. The chip apparently has no way to provide the current bitmode. This implements the functionality at the driver level. The driver starts out assuming the chip is in UART mode (which it will be, coming out of reset) and keeps track of every successful set-bitmode operation so that it can always return the current mode with UFTDIIOC_GET_BITMODE. --- sys/dev/usb/serial/uftdi.c | 16 ++++++++++++---- sys/dev/usb/uftdiio.h | 7 ++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/sys/dev/usb/serial/uftdi.c b/sys/dev/usb/serial/uftdi.c index cf468044b049..b33df36d9276 100644 --- a/sys/dev/usb/serial/uftdi.c +++ b/sys/dev/usb/serial/uftdi.c @@ -161,6 +161,7 @@ struct uftdi_softc { uint8_t sc_hdrlen; uint8_t sc_msr; uint8_t sc_lsr; + uint8_t sc_bitmode; }; struct uftdi_param_config { @@ -196,7 +197,7 @@ static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); static int uftdi_reset(struct ucom_softc *, int); static int uftdi_set_bitmode(struct ucom_softc *, uint8_t, uint8_t); -static int uftdi_get_bitmode(struct ucom_softc *, uint8_t *); +static int uftdi_get_bitmode(struct ucom_softc *, uint8_t *, uint8_t *); static int uftdi_set_latency(struct ucom_softc *, int); static int uftdi_get_latency(struct ucom_softc *, int *); static int uftdi_set_event_char(struct ucom_softc *, int); @@ -1090,6 +1091,7 @@ uftdi_attach(device_t dev) sc->sc_udev = uaa->device; sc->sc_dev = dev; sc->sc_unit = device_get_unit(dev); + sc->sc_bitmode = UFTDI_BITMODE_NONE; device_set_usb_desc(dev); mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF); @@ -1681,6 +1683,7 @@ uftdi_set_bitmode(struct ucom_softc *ucom, uint8_t bitmode, uint8_t iomask) { struct uftdi_softc *sc = ucom->sc_parent; usb_device_request_t req; + int rv; req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = FTDI_SIO_SET_BITMODE; @@ -1693,11 +1696,15 @@ uftdi_set_bitmode(struct ucom_softc *ucom, uint8_t bitmode, uint8_t iomask) else USETW2(req.wValue, (1 << bitmode), iomask); - return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)); + rv = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL); + if (rv == USB_ERR_NORMAL_COMPLETION) + sc->sc_bitmode = bitmode; + + return (rv); } static int -uftdi_get_bitmode(struct ucom_softc *ucom, uint8_t *iomask) +uftdi_get_bitmode(struct ucom_softc *ucom, uint8_t *bitmode, uint8_t *iomask) { struct uftdi_softc *sc = ucom->sc_parent; usb_device_request_t req; @@ -1709,6 +1716,7 @@ uftdi_get_bitmode(struct ucom_softc *ucom, uint8_t *iomask) USETW(req.wLength, 1); USETW(req.wValue, 0); + *bitmode = sc->sc_bitmode; return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, iomask)); } @@ -1891,7 +1899,7 @@ uftdi_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data, break; case UFTDIIOC_GET_BITMODE: mode = (struct uftdi_bitmode *)data; - err = uftdi_get_bitmode(ucom, &mode->iomask); + err = uftdi_get_bitmode(ucom, &mode->mode, &mode->iomask); break; case UFTDIIOC_SET_LATENCY: err = uftdi_set_latency(ucom, *((int *)data)); diff --git a/sys/dev/usb/uftdiio.h b/sys/dev/usb/uftdiio.h index 4c6ecbc3f7ba..b6244831de32 100644 --- a/sys/dev/usb/uftdiio.h +++ b/sys/dev/usb/uftdiio.h @@ -43,7 +43,7 @@ enum uftdi_bitmodes UFTDI_BITMODE_CPU_EMUL = 3, UFTDI_BITMODE_FAST_SERIAL = 4, UFTDI_BITMODE_CBUS = 5, - UFTDI_BITMODE_NONE = 0xff, + UFTDI_BITMODE_NONE = 0xff, /* aka UART mode. */ }; /* @@ -52,8 +52,9 @@ enum uftdi_bitmodes * iomask = Mask of bits enabled for bitbang output. * * For UFTDIIOC_GET_BITMODE: - * mode = Unused. - * iomask = Returned snapshot of bitbang pin states at time of call. + * mode = Mode most recently set using UFTDIIOC_SET_BITMODE. + * iomask = Returned snapshot of DBUS0..DBUS7 pin states at time of call. + * Pin states can be read in any mode, not just bitbang modes. */ struct uftdi_bitmode {