Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Aug 2015 16:17:01 +0000 (UTC)
From:      Ian Lepore <ian@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r287035 - in stable/10: share/man/man4 sys/dev/usb sys/dev/usb/serial
Message-ID:  <201508231617.t7NGH1NL091218@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ian
Date: Sun Aug 23 16:17:00 2015
New Revision: 287035
URL: https://svnweb.freebsd.org/changeset/base/287035

Log:
  MFC r264394, r286382, r286385, r286389:
  
    Add support to the uftdi driver for reading and writing the serial eeprom
    that can be attached to the chips, via ioctl() calls.
  
    Return the current ftdi bitbang mode with the UFTDIIOC_GET_BITMODE ioctl.
  
    Document the recently added get-bitmode and eeprom read/write functionality.

Modified:
  stable/10/share/man/man4/uftdi.4
  stable/10/sys/dev/usb/serial/uftdi.c
  stable/10/sys/dev/usb/serial/uftdi_reg.h
  stable/10/sys/dev/usb/uftdiio.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/share/man/man4/uftdi.4
==============================================================================
--- stable/10/share/man/man4/uftdi.4	Sun Aug 23 15:20:49 2015	(r287034)
+++ stable/10/share/man/man4/uftdi.4	Sun Aug 23 16:17:00 2015	(r287035)
@@ -29,7 +29,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd March 31, 2014
+.Dd August 6, 2015
 .Dt UFTDI 4
 .Os
 .Sh NAME
@@ -110,6 +110,11 @@ The
 must be one of the
 .Va uftdi_bitmodes
 values.
+Setting
+.Va mode
+to
+.Dv UFTDI_BITMODE_NONE
+returns the channel to standard UART mode.
 .Bd -literal
 enum uftdi_bitmodes
 {
@@ -136,20 +141,23 @@ the desired mode, then you
 .Xr read 2
 and
 .Xr write 2
-data which either reflects pin state or is interpreted 
+data which either reflects pin state or is interpreted
 as MPSSE commands and parameters, depending on the mode.
 .It Dv UFTDIIOC_GET_BITMODE Pq Vt "struct uftdi_bitmode"
-Return the state of the bitbang pins at the time of the call in the
+Return the current bitbang mode in the
+.Va mode
+member, and the state of the DBUS0..DBUS7 pins at the time
+of the call in the
 .Va iomask
 member.
-The
-.Va mode
-member is unused.
+The pin state can be read while the chip is in any mode, including
+.Dv UFTDI_BITMODE_NONE
+(UART) mode.
 .It Dv UFTDIIOC_SET_ERROR_CHAR Pq Vt int
 Set the character which is inserted into the buffer to mark
 the point of an error such as FIFO overflow.
 .It Dv UFTDIIOC_SET_EVENT_CHAR Pq Vt int
-Set the character which causes a partial FIFO full of data 
+Set the character which causes a partial FIFO full of data
 to be returned immediately even if the FIFO is not full.
 .It Dv UFTDIIOC_SET_LATENCY Pq Vt int
 Set the amount of time to wait for a full FIFO,
@@ -164,6 +172,55 @@ This is the
 .Va bcdDevice
 value from the
 .Va usb_device_descriptor .
+.It Dv UFTDIIOC_READ_EEPROM Pq Vt "struct uftdi_eeio"
+Read one or more words from the configuration eeprom.
+The FTDI chip performs eeprom I/O in 16-bit words.
+Set
+.Va offset
+and
+.Va length
+to values evenly divisible by two before the call, and the
+.Va data
+array will contain the requested values from eeprom after the call.
+.Bd -literal
+struct uftdi_eeio
+{
+	uint16_t offset;
+	uint16_t length;
+	uint16_t data[64];
+};
+.Ed
+.Pp
+The FT232R chip has an internal eeprom.
+An external serial eeprom is optional on other FTDI chips.
+The eeprom may contain 64, 128, or 256 words, 
+depending on the part used.
+Multiple calls may be needed to read or write the larger parts.
+When no eeprom is present, all words in the returned data are 0xffff.
+An erased eeprom also reads as all 0xffff.
+.It Dv UFTDIIOC_WRITE_EEPROM Pq Vt "struct uftdi_eeio"
+Write one or more words to the configuration eeprom.
+The
+.Va uftdi_eeio
+values are as described for
+.Dv UFTDIIOC_READ_EEPROM .
+.Pp
+The FTDI chip does a blind write to the eeprom, and it will appear
+to succeed even when no eeprom is present.
+To ensure a good write you must read back and verify the data.
+It is
+.Em not
+necessary to erase before writing.
+Any position within the eeprom can be overwritten at any time.
+.It Dv UFTDIIOC_ERASE_EEPROM Pq Vt int
+Erase the entire eeprom.
+This is useful primarily for test and debugging, as there is no
+need to erase before writing.
+To help prevent accidental erasure caused by calling the wrong
+ioctl, you must pass the special value
+.Dv UFTDI_CONFIRM_ERASE
+as the argument to this ioctl.
+.El
 .Sh HARDWARE
 The
 .Nm

Modified: stable/10/sys/dev/usb/serial/uftdi.c
==============================================================================
--- stable/10/sys/dev/usb/serial/uftdi.c	Sun Aug 23 15:20:49 2015	(r287034)
+++ stable/10/sys/dev/usb/serial/uftdi.c	Sun Aug 23 16:17:00 2015	(r287035)
@@ -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 
 		    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);
@@ -1091,6 +1092,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);
@@ -1682,6 +1684,7 @@ uftdi_set_bitmode(struct ucom_softc *uco
 {
 	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;
@@ -1694,11 +1697,15 @@ uftdi_set_bitmode(struct ucom_softc *uco
 	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;
@@ -1710,6 +1717,7 @@ uftdi_get_bitmode(struct ucom_softc *uco
 	USETW(req.wLength, 1);
 	USETW(req.wValue,  0);
 
+	*bitmode = sc->sc_bitmode;
 	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, iomask));
 }
 
@@ -1792,6 +1800,82 @@ uftdi_set_error_char(struct ucom_softc *
 }
 
 static int
+uftdi_read_eeprom(struct ucom_softc *ucom, struct uftdi_eeio *eeio)
+{
+	struct uftdi_softc *sc = ucom->sc_parent;
+	usb_device_request_t req;
+	usb_error_t err;
+	uint16_t widx, wlength, woffset;
+
+	/* Offset and length must both be evenly divisible by two. */
+	if ((eeio->offset | eeio->length) & 0x01)
+		return (EINVAL);
+
+	woffset = eeio->offset / 2U;
+	wlength = eeio->length / 2U;
+	for (widx = 0; widx < wlength; widx++) {
+		req.bmRequestType = UT_READ_VENDOR_DEVICE;
+		req.bRequest = FTDI_SIO_READ_EEPROM;
+		USETW(req.wIndex, widx + woffset);
+		USETW(req.wLength, 2);
+		USETW(req.wValue, 0);
+		err = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req,
+		    &eeio->data[widx]);
+		if (err != USB_ERR_NORMAL_COMPLETION)
+			return (err);
+	}
+	return (USB_ERR_NORMAL_COMPLETION);
+}
+
+static int
+uftdi_write_eeprom(struct ucom_softc *ucom, struct uftdi_eeio *eeio)
+{
+	struct uftdi_softc *sc = ucom->sc_parent;
+	usb_device_request_t req;
+	usb_error_t err;
+	uint16_t widx, wlength, woffset;
+
+	/* Offset and length must both be evenly divisible by two. */
+	if ((eeio->offset | eeio->length) & 0x01)
+		return (EINVAL);
+
+	woffset = eeio->offset / 2U;
+	wlength = eeio->length / 2U;
+	for (widx = 0; widx < wlength; widx++) {
+		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+		req.bRequest = FTDI_SIO_WRITE_EEPROM;
+		USETW(req.wIndex, widx + woffset);
+		USETW(req.wLength, 0);
+		USETW(req.wValue, eeio->data[widx]);
+		err = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL);
+		if (err != USB_ERR_NORMAL_COMPLETION)
+			return (err);
+	}
+	return (USB_ERR_NORMAL_COMPLETION);
+}
+
+static int
+uftdi_erase_eeprom(struct ucom_softc *ucom, int confirmation)
+{
+	struct uftdi_softc *sc = ucom->sc_parent;
+	usb_device_request_t req;
+	usb_error_t err;
+
+	/* Small effort to prevent accidental erasure. */
+	if (confirmation != UFTDI_CONFIRM_ERASE)
+		return (EINVAL);
+
+	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+	req.bRequest = FTDI_SIO_ERASE_EEPROM;
+	USETW(req.wIndex, 0);
+	USETW(req.wLength, 0);
+	USETW(req.wValue, 0);
+	err = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL);
+
+	return (err);
+}
+
+static int
 uftdi_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
     int flag, struct thread *td)
 {
@@ -1816,7 +1900,7 @@ uftdi_ioctl(struct ucom_softc *ucom, uin
 		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));
@@ -1834,6 +1918,15 @@ uftdi_ioctl(struct ucom_softc *ucom, uin
 		*(int *)data = sc->sc_bcdDevice;
 		err = 0;
 		break;
+	case UFTDIIOC_READ_EEPROM:
+		err = uftdi_read_eeprom(ucom, (struct uftdi_eeio *)data);
+		break;
+	case UFTDIIOC_WRITE_EEPROM:
+		err = uftdi_write_eeprom(ucom, (struct uftdi_eeio *)data);
+		break;
+	case UFTDIIOC_ERASE_EEPROM:
+		err = uftdi_erase_eeprom(ucom, *(int *)data);
+		break;
 	default:
 		return (ENOIOCTL);
 	}

Modified: stable/10/sys/dev/usb/serial/uftdi_reg.h
==============================================================================
--- stable/10/sys/dev/usb/serial/uftdi_reg.h	Sun Aug 23 15:20:49 2015	(r287034)
+++ stable/10/sys/dev/usb/serial/uftdi_reg.h	Sun Aug 23 16:17:00 2015	(r287035)
@@ -31,7 +31,10 @@
 #define	FTDI_SIO_SET_LATENCY	9	/* Set the latency timer */
 #define	FTDI_SIO_GET_LATENCY	10	/* Read the latency timer */
 #define	FTDI_SIO_SET_BITMODE	11	/* Set the bit bang I/O mode */
-#define	FTDI_SIO_GET_BITMODE	12	/* Read pin states in bit bang mode */
+#define	FTDI_SIO_GET_BITMODE	12	/* Read pin states from any mode */
+#define	FTDI_SIO_READ_EEPROM	144	/* Read eeprom word */
+#define	FTDI_SIO_WRITE_EEPROM	145	/* Write eeprom word */
+#define	FTDI_SIO_ERASE_EEPROM	146	/* Erase entire eeprom */
 
 /* Port Identifier Table */
 #define	FTDI_PIT_DEFAULT 	0	/* SIOA */

Modified: stable/10/sys/dev/usb/uftdiio.h
==============================================================================
--- stable/10/sys/dev/usb/uftdiio.h	Sun Aug 23 15:20:49 2015	(r287034)
+++ stable/10/sys/dev/usb/uftdiio.h	Sun Aug 23 16:17:00 2015	(r287035)
@@ -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
 {
@@ -61,6 +62,26 @@ struct uftdi_bitmode
 	uint8_t iomask;
 };
 
+/*
+ * For UFTDIIOC_READ_EEPROM, UFTDIIOC_WRITE_EEPROM:
+ *
+ * IO is done in 16-bit words at the chip level; offset and length are in bytes,
+ * but must each be evenly divisible by two.
+ *
+ * It is not necessary to erase before writing.  For the FT232R device (only)
+ * you must set the latency timer to 0x77 before doing a series of eeprom writes
+ * (and restore it to the prior value when done).
+ */
+struct uftdi_eeio
+{
+	uint16_t offset;
+	uint16_t length;
+	uint16_t data[64];
+};
+
+/* Pass this value to confirm that eeprom erase request is not accidental. */
+#define	UFTDI_CONFIRM_ERASE	0x26139108
+
 #define	UFTDIIOC_RESET_IO	_IO('c', 0)	/* Reset config, flush fifos.*/
 #define	UFTDIIOC_RESET_RX	_IO('c', 1)	/* Flush input fifo. */
 #define	UFTDIIOC_RESET_TX	_IO('c', 2)	/* Flush output fifo. */
@@ -71,5 +92,8 @@ struct uftdi_bitmode
 #define	UFTDIIOC_SET_LATENCY	_IOW('c', 7, int)	/* 1-255 ms */
 #define	UFTDIIOC_GET_LATENCY	_IOR('c', 8, int)
 #define	UFTDIIOC_GET_HWREV	_IOR('c', 9, int)
+#define	UFTDIIOC_READ_EEPROM	_IOWR('c', 10, struct uftdi_eeio)
+#define	UFTDIIOC_WRITE_EEPROM	_IOW('c', 11, struct uftdi_eeio)
+#define	UFTDIIOC_ERASE_EEPROM	_IOW('c', 12, int)
 
 #endif



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201508231617.t7NGH1NL091218>