Skip to contentSkip to conversation

USB: serial: ftdi_sio: implement GPIO support for FT-X devices

PATCH Superseded Karoly Pados 2 participants 4 messages Sep 10, 2018 – Sep 18, 2018

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 USB: serial: ftdi_sio: implement GPIO support for FT-X devices

This patch allows using the CBUS pins of FT-X devices as GPIO in CBUS bitbanging mode. There is no conflict between the GPIO and VCP functionality in this mode. Tested on FT230X and FT231X.

As there is no way to request the current CBUS register configuration from the device, all CBUS pins are set to a known state when the first GPIO is requested. This allows using libftdi to set the GPIO pins before loading this module for UART functionality, a behavior that existing applications might be relying upon (though no specific case is known to the authors of this patch).

---

Changelog:
- v2: Fix compile error when CONFIG_GPIOLIB is not defined.
- v3: Incorporate review feedback.

Though there is no code copied, libftdi was used as
a reference. ftdi_read_eeprom is based on Loic Poulain's patch.

I've removed --strict from checkpatch.pl invocation, yet it still gives me those warnings it shouldn't. Maybe I have a different version? Now in v3 I also get two new I'm not sure how to deal with. Rest of text below same as before.

I noticed there have been numerous historic attempts by other people at adding GPIO support to ftdi_sio. One tried adding it in some very application-specific way, another one tried to use the mfd framework, the tried using a GPIO mode which is mutually exclusive to VCP, another one did not implement the review results etc... I hope I learned from those attempts and am planning to respond to reviews. Also, this patch follows the rough structure of the GPIO support for cp210x devices that have already been accepted, in the hope that it is a good starting point.

This patch uses CBUS bitbanging mode, which works nicely in parallel with the VCP function. The other modes do not, and so IMHO it does not make sense to try adding them to this same module.

For this device, whenever changing the state of any single pin, all the others need to be written too. This means in order to change any pin, we need to know the current state of all the others. So when using GPIO, we need to have a known starting state for all pins, but there seems to be no way to retrieve the existing GPIO configuration (directions and output register values). The way I handle this in this patch is that when requesting a GPIO for the first time, the module initializes all pins to a known default state (to inputs). Input was chosen, because a potentially floating pin is better than a potential driver conflict, because the latter could result in hardware damage. However, if the user does not request a GPIO, the CBUS pins are not initialized, avoiding unnecessarily changing hardware state. I figured I cannot rely on the default power-on state of the device for this as the user might have used libftdi before loading our module. When the module is unloaded, CBUS bitbanging mode is exited if it were us who entered it earlier.

 drivers/usb/serial/ftdi_sio.c | 318 +++++++++++++++++++++++++++++++++-
 drivers/usb/serial/ftdi_sio.h |  27 ++-
 2 files changed, 343 insertions(+), 2 deletions(-)
drivers/usb/serial/ftdi_sio.c +317 -1
index b5cef322826f..00b6c6abdd09 100644
@@ -40,6 +40,9 @@
 #include <linux/usb.h>
 #include <linux/serial.h>
 #include <linux/usb/serial.h>
+#if defined(CONFIG_GPIOLIB)
+#include <linux/gpio/driver.h>
+#endif
 #include "ftdi_sio.h"
 #include "ftdi_sio_ids.h"
 
@@ -72,6 +75,14 @@ struct ftdi_private {
 	unsigned int latency;		/* latency setting in use */
 	unsigned short max_packet_size;
 	struct mutex cfg_lock; /* Avoid mess by parallel calls of config ioctl() and change_speed() */
+#if defined(CONFIG_GPIOLIB)
+	struct gpio_chip gc;
+	bool	gpio_registered;  /* is the gpiochip in kernel registered */
+	bool	gpio_used;	  /* true if the user requested a gpio */
+	u8	gpio_altfunc;	  /* which pins are in gpio mode */
+	u8	gpio_input;	  /* pin directions cache */
+	u8	gpio_value;	  /* pin value for outputs */
+#endif
 };
 
 /* struct ftdi_sio_quirk is used by devices requiring special attention. */
@@ -1766,6 +1777,301 @@ static void remove_sysfs_attrs(struct usb_serial_port *port)
 
 }
 
+#if defined(CONFIG_GPIOLIB)
+
+static int ftdi_set_bitmode_req(struct usb_serial_port *port, u8 mode)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	int result;
+	u16 val;
+
+	/* device's direction polarity is different from kernel's */
+	u8 direction = (~priv->gpio_input) & 0x0f;
+
+	val = (mode << 8) | (direction << 4) | (priv->gpio_value & 0x0f);
+	result = usb_control_msg(serial->dev,
+				 usb_sndctrlpipe(serial->dev, 0),
+				 FTDI_SIO_SET_BITMODE_REQUEST,
+				 FTDI_SIO_SET_BITMODE_REQUEST_TYPE, val,
+				 priv->interface, NULL, 0, WDR_TIMEOUT);
+
+	if (result < 0) {
+		dev_err(&serial->interface->dev,
+			"bitmode request failed for value: %d\n",
+			val);
+	}
+
+	return result;
+}
+
+static int ftdi_set_cbus_pins(struct usb_serial_port *port)
+{
+	return ftdi_set_bitmode_req(port, FTDI_SIO_BITMODE_CBUS);
+}
+
+static int ftdi_exit_cbus_mode(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input = 0;
+	priv->gpio_value = 0;
+	return ftdi_set_bitmode_req(port, FTDI_SIO_BITMODE_RESET);
+}
+
+static int ftdi_gpio_request(struct gpio_chip *gc, unsigned int offset)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	int result;
+
+	if (priv->gpio_altfunc & BIT(offset))
+		return -ENODEV;
+
+	if (!priv->gpio_used) {
+		/* Set default pin states, as we cannot get them from device */
+		priv->gpio_input = 0xff;
+		priv->gpio_value = 0x00;
+		result = ftdi_set_cbus_pins(port);
+		if (result)
+			return result;
+
+		priv->gpio_used = true;
+	}
+
+	return 0;
+}
+
+static int ftdi_gpio_get(struct gpio_chip *gc, unsigned int gpio)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	unsigned char *rcvbuf;
+	int result;
+
+	rcvbuf = kmalloc(1, GFP_KERNEL);
+	if (!rcvbuf)
+		return -ENOMEM;
+
+	result = usb_control_msg(serial->dev,
+				 usb_rcvctrlpipe(serial->dev, 0),
+				 FTDI_SIO_READ_PINS_REQUEST,
+				 FTDI_SIO_READ_PINS_REQUEST_TYPE, 0,
+				 priv->interface, rcvbuf, 1, WDR_TIMEOUT);
+	if (result < 0) {
+	} else if (result < 1)
+		result = -EIO;
+	else
+		result = !!(rcvbuf[0] & BIT(gpio));
+
+	kfree(rcvbuf);
+
+	return result;
+}
+
+static void ftdi_gpio_set(struct gpio_chip *gc,
+			      unsigned int gpio,
+			      int value)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	int result;
+
+	if (value)
+		priv->gpio_value |= BIT(gpio);
+	else
+		priv->gpio_value &= ~BIT(gpio);
+
+	result = ftdi_set_cbus_pins(port);
+	if (result < 0) {
+		dev_err(&port->serial->interface->dev,
+			"failed to set GPIO value: %d\n",
+			result);
+	}
+}
+
+static int ftdi_gpio_direction_get(struct gpio_chip *gc,
+				       unsigned int gpio)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	return priv->gpio_input & BIT(gpio);
+}
+
+static int ftdi_gpio_direction_input(struct gpio_chip *gc,
+					 unsigned int gpio)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input |= BIT(gpio);
+
+	return ftdi_set_cbus_pins(port);
+}
+
+static int ftdi_gpio_direction_output(struct gpio_chip *gc,
+					  unsigned int gpio,
+					  int value)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input &= ~BIT(gpio);
+	if (value)
+		priv->gpio_value |= BIT(gpio);
+	else
+		priv->gpio_value &= ~BIT(gpio);
+
+	return ftdi_set_cbus_pins(port);
+}
+
+/* Returns the number of bytes read */
+static int ftdi_read_eeprom(struct usb_serial *serial,
+			    void *dst,	/* must be kmalloc'd using GFP_KERNEL*/
+			    u16 addr,	/* must be aligned to 16 bits */
+			    u16 nbytes)	/* must be a multiple of 16 bits */
+{
+	int read = 0;
+
+	/* Argument checks */
+	if (addr % 2 != 0)
+		return -EINVAL;
+	if (nbytes % 2 != 0)
+		return -EINVAL;
+
+	/* Read EEPROM two bytes at a time */
+	while (read < nbytes) {
+		int rv;
+
+		rv = usb_control_msg(serial->dev,
+				     usb_rcvctrlpipe(serial->dev, 0),
+				     FTDI_SIO_READ_EEPROM_REQUEST,
+				     FTDI_SIO_READ_EEPROM_REQUEST_TYPE,
+				     0, (addr + read) / 2, dst + read, 2,
+				     WDR_TIMEOUT);
+		if (rv < 0)
+			return rv;
+
+		read += rv;
+
+		if (rv < 2)
+			break;
+	}
+
+	return read;
+}
+
+static int ftx_gpioconf_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	const u16 cbus_cfg_addr = 0x1a;
+	const u16 cbus_cfg_size = 8;
+	u8 *cbus_cfg_buf;
+	int result;
+	u8 i;
+
+	/* Read a part of device EEPROM */
+	cbus_cfg_buf = kmalloc(cbus_cfg_size, GFP_KERNEL);
+	if (!cbus_cfg_buf)
+		return -ENOMEM;
+
+	result = ftdi_read_eeprom(serial, cbus_cfg_buf,
+				  cbus_cfg_addr, cbus_cfg_size);
+
+	if (result < 0)
+		goto out_free;
+	else if (result < 8) {
+		result = -EIO;
+		goto out_free;
+	} else
+		result = 0;
+
+	/* Chip-type guessing logic based on libftdi. */
+	priv->gc.ngpio = 4;  /* FT230X, FT231X */
+	if (le16_to_cpu(serial->dev->descriptor.bcdDevice) != 0x1000)
+		priv->gc.ngpio = 1;  /* FT234XD */
+
+	/* Determine which pins are configured for CBUS bitbanging */
+	priv->gpio_altfunc = 0xff;
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if (cbus_cfg_buf[i] == FTDI_SIO_CBUS_MUX_GPIO)
+			priv->gpio_altfunc &= ~BIT(i);
+	}
+
+out_free:
+	kfree(cbus_cfg_buf);
+
+	return result;
+}
+
+static int ftdi_gpio_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	int result;
+
+	/* Device-specific initializations */
+	switch (priv->chip_type) {
+	case FTX:
+		result = ftx_gpioconf_init(port);
+		break;
+	default:
+		return 0;
+	}
+
+	if (result < 0)
+		return result;
+
+	/* Register GPIO chip to kernel */
+	priv->gc.label = "ftdi-cbus";
+	priv->gc.request = ftdi_gpio_request;
+	priv->gc.get_direction = ftdi_gpio_direction_get;
+	priv->gc.direction_input = ftdi_gpio_direction_input;
+	priv->gc.direction_output = ftdi_gpio_direction_output;
+	priv->gc.get = ftdi_gpio_get;
+	priv->gc.set = ftdi_gpio_set;
+	priv->gc.owner = THIS_MODULE;
+	priv->gc.parent = &serial->interface->dev;
+	priv->gc.base = -1;
+	priv->gc.can_sleep = true;
+
+	result = gpiochip_add_data(&priv->gc, port);
+	if (!result)
+		priv->gpio_registered = true;
+
+	return result;
+}
+
+static void ftdi_gpio_remove(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	if (priv->gpio_used) {
+		/* Remark: Exiting CBUS-mode does not reset pin states too */
+		ftdi_exit_cbus_mode(port);
+		priv->gpio_used = false;
+	}
+
+	if (priv->gpio_registered) {
+		gpiochip_remove(&priv->gc);
+		priv->gpio_registered = false;
+	}
+}
+
+#else
+
+static int ftdi_gpio_init(struct usb_serial_port *port)
+{
+	return 0;
+}
+
+static void ftdi_gpio_remove(struct usb_serial_port *port) { }
+
+#endif
+
 /*
  * ***************************************************************************
  * FTDI driver specific functions
@@ -1794,7 +2100,7 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv;
 	const struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial);
-
+	int result;
 
 	priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
 	if (!priv)
@@ -1813,6 +2119,14 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
 		priv->latency = 16;
 	write_latency_timer(port);
 	create_sysfs_attrs(port);
+
+	result = ftdi_gpio_init(port);
+	if (result < 0) {
+		dev_err(&port->serial->interface->dev,
+			"GPIO initialisation failed: %d\n",
+			result);
+	}
+
 	return 0;
 }
 
@@ -1930,6 +2244,8 @@ static int ftdi_sio_port_remove(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
 
+	ftdi_gpio_remove(port);
+
 	remove_sysfs_attrs(port);
 
 	kfree(priv);
drivers/usb/serial/ftdi_sio.h +26 -1
index dcd0b6e05baf..5a296e9e87a2 100644
@@ -35,7 +35,10 @@
 #define FTDI_SIO_SET_EVENT_CHAR		6 /* Set the event character */
 #define FTDI_SIO_SET_ERROR_CHAR		7 /* Set the error character */
 #define FTDI_SIO_SET_LATENCY_TIMER	9 /* Set the latency timer */
-#define FTDI_SIO_GET_LATENCY_TIMER	10 /* Get the latency timer */
+#define FTDI_SIO_GET_LATENCY_TIMER	0x0a /* Get the latency timer */
+#define FTDI_SIO_SET_BITMODE		0x0b /* Set CBUS GPIO pin mode */
+#define FTDI_SIO_READ_PINS		0x0c /* Read immediate value of pins */
+#define FTDI_SIO_READ_EEPROM		0x90 /* Read EEPROM */
 
 /* Interface indices for FT2232, FT2232H and FT4232H devices */
 #define INTERFACE_A		1
@@ -433,6 +436,28 @@ enum ftdi_sio_baudrate {
  *         1 = active
  */
 
+/* FTDI_SIO_SET_BITMODE */
+#define FTDI_SIO_SET_BITMODE_REQUEST_TYPE 0x40
+#define FTDI_SIO_SET_BITMODE_REQUEST FTDI_SIO_SET_BITMODE
+
+/* Possible bitmodes for FTDI_SIO_SET_BITMODE_REQUEST */
+#define FTDI_SIO_BITMODE_RESET		0x00
+#define FTDI_SIO_BITMODE_CBUS		0x20
+
+/* FTDI_SIO_READ_PINS */
+#define FTDI_SIO_READ_PINS_REQUEST_TYPE 0xc0
+#define FTDI_SIO_READ_PINS_REQUEST FTDI_SIO_READ_PINS
+
+/*
+ * FTDI_SIO_READ_EEPROM
+ *
+ * EEPROM format found in FTDI AN_201, "FT-X MTP memory Configuration",
+ * http://www.ftdichip.com/Support/Documents/AppNotes/AN_201_FT-X%20MTP%20Memory%20Configuration.pdf
+ */
+#define FTDI_SIO_READ_EEPROM_REQUEST_TYPE 0xc0
+#define FTDI_SIO_READ_EEPROM_REQUEST FTDI_SIO_READ_EEPROM
+
+#define FTDI_SIO_CBUS_MUX_GPIO		8
 
 
 /* Descriptors returned by the device
maintainer
#1 lore raw
Quoted 11 lines · Karoly Pados

This patch allows using the CBUS pins of FT-X devices as GPIO in CBUS bitbanging mode. There is no conflict between the GPIO and VCP functionality in this mode. Tested on FT230X and FT231X.

As there is no way to request the current CBUS register configuration from the device, all CBUS pins are set to a known state when the first GPIO is requested. This allows using libftdi to set the GPIO pins before loading this module for UART functionality, a behavior that existing applications might be relying upon (though no specific case is known to the authors of this patch).

Quoted 6 lines

---

Changelog:
- v2: Fix compile error when CONFIG_GPIOLIB is not defined.
- v3: Incorporate review feedback.

Next time, please be more specific about what you changed here.

Quoted 7 lines

Though there is no code copied, libftdi was used as
a reference. ftdi_read_eeprom is based on Loic Poulain's patch.

I've removed --strict from checkpatch.pl invocation, yet it still gives me those warnings it shouldn't. Maybe I have a different version? Now in v3 I also get two new I'm not sure how to deal with. Rest of text below same as before.

Yeah, the two new ones I get too, and they need to be fixed. More below.

Quoted 39 lines

I noticed there have been numerous historic attempts by other people at adding GPIO support to ftdi_sio. One tried adding it in some very application-specific way, another one tried to use the mfd framework, the tried using a GPIO mode which is mutually exclusive to VCP, another one did not implement the review results etc... I hope I learned from those attempts and am planning to respond to reviews. Also, this patch follows the rough structure of the GPIO support for cp210x devices that have already been accepted, in the hope that it is a good starting point.

This patch uses CBUS bitbanging mode, which works nicely in parallel with the VCP function. The other modes do not, and so IMHO it does not make sense to try adding them to this same module.

For this device, whenever changing the state of any single pin, all the others need to be written too. This means in order to change any pin, we need to know the current state of all the others. So when using GPIO, we need to have a known starting state for all pins, but there seems to be no way to retrieve the existing GPIO configuration (directions and output register values). The way I handle this in this patch is that when requesting a GPIO for the first time, the module initializes all pins to a known default state (to inputs). Input was chosen, because a potentially floating pin is better than a potential driver conflict, because the latter could result in hardware damage. However, if the user does not request a GPIO, the CBUS pins are not initialized, avoiding unnecessarily changing hardware state. I figured I cannot rely on the default power-on state of the device for this as the user might have used libftdi before loading our module. When the module is unloaded, CBUS bitbanging mode is exited if it were us who entered it earlier.

 drivers/usb/serial/ftdi_sio.c | 318 +++++++++++++++++++++++++++++++++-
 drivers/usb/serial/ftdi_sio.h |  27 ++-
 2 files changed, 343 insertions(+), 2 deletions(-)
drivers/usb/serial/ftdi_sio.c +0 -0
index b5cef322826f..00b6c6abdd09 100644

drivers/usb/serial/ftdi_sio.c line 45

Quoted 6 lines
drivers/usb/serial/ftdi_sio.c +3 -0
@@ -40,6 +40,9 @@
 #include <linux/usb.h>
 #include <linux/serial.h>
 #include <linux/usb/serial.h>
+#if defined(CONFIG_GPIOLIB)
+#include <linux/gpio/driver.h>
+#endif

Hmm. I already commented on this in v1.

drivers/usb/serial/ftdi_sio.c struct ftdi_private { · line 83

Quoted 13 lines
drivers/usb/serial/ftdi_sio.c +6 -0
 #include "ftdi_sio.h"
 #include "ftdi_sio_ids.h"
 
@@ -72,6 +75,14 @@ struct ftdi_private {
 	unsigned int latency;		/* latency setting in use */
 	unsigned short max_packet_size;
 	struct mutex cfg_lock; /* Avoid mess by parallel calls of config ioctl() and change_speed() */
+#if defined(CONFIG_GPIOLIB)
+	struct gpio_chip gc;
+	bool	gpio_registered;  /* is the gpiochip in kernel registered */
+	bool	gpio_used;	  /* true if the user requested a gpio */
+	u8	gpio_altfunc;	  /* which pins are in gpio mode */
+	u8	gpio_input;	  /* pin directions cache */

And I asked you to invert this one (i.e. replace with gpio_output).

Quoted 18 lines
drivers/usb/serial/ftdi_sio.c +11 -0
+	u8	gpio_value;	  /* pin value for outputs */
+#endif
 };
 
 /* struct ftdi_sio_quirk is used by devices requiring special attention. */
@@ -1766,6 +1777,301 @@ static void remove_sysfs_attrs(struct usb_serial_port *port)
 
 }
 
+#if defined(CONFIG_GPIOLIB)
+
+static int ftdi_set_bitmode_req(struct usb_serial_port *port, u8 mode)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	int result;
+	u16 val;
+

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1798

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+	/* device's direction polarity is different from kernel's */
+	u8 direction = (~priv->gpio_input) & 0x0f;
+
+	val = (mode << 8) | (direction << 4) | (priv->gpio_value & 0x0f);
+	result = usb_control_msg(serial->dev,
+				 usb_sndctrlpipe(serial->dev, 0),
+				 FTDI_SIO_SET_BITMODE_REQUEST,
+				 FTDI_SIO_SET_BITMODE_REQUEST_TYPE, val,
+				 priv->interface, NULL, 0, WDR_TIMEOUT);
+

And I asked you to remove such empty lines.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1801

Quoted 3 lines
drivers/usb/serial/ftdi_sio.c +3 -0
+	if (result < 0) {
+		dev_err(&serial->interface->dev,
+			"bitmode request failed for value: %d\n",

Use hex notation and include the errno:

	"bitmode request failed for value 0x%04x: %d\n", val, result
Quoted 54 lines
drivers/usb/serial/ftdi_sio.c +54 -0
+	}
+
+	return result;
+}
+
+static int ftdi_set_cbus_pins(struct usb_serial_port *port)
+{
+	return ftdi_set_bitmode_req(port, FTDI_SIO_BITMODE_CBUS);
+}
+
+static int ftdi_exit_cbus_mode(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input = 0;
+	priv->gpio_value = 0;
+	return ftdi_set_bitmode_req(port, FTDI_SIO_BITMODE_RESET);
+}
+
+static int ftdi_gpio_request(struct gpio_chip *gc, unsigned int offset)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	int result;
+
+	if (priv->gpio_altfunc & BIT(offset))
+		return -ENODEV;
+
+	if (!priv->gpio_used) {
+		/* Set default pin states, as we cannot get them from device */
+		priv->gpio_input = 0xff;
+		priv->gpio_value = 0x00;
+		result = ftdi_set_cbus_pins(port);
+		if (result)
+			return result;
+
+		priv->gpio_used = true;
+	}
+
+	return 0;
+}
+
+static int ftdi_gpio_get(struct gpio_chip *gc, unsigned int gpio)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	unsigned char *rcvbuf;
+	int result;
+
+	rcvbuf = kmalloc(1, GFP_KERNEL);
+	if (!rcvbuf)
+		return -ENOMEM;
+

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1866

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+	result = usb_control_msg(serial->dev,
+				 usb_rcvctrlpipe(serial->dev, 0),
+				 FTDI_SIO_READ_PINS_REQUEST,
+				 FTDI_SIO_READ_PINS_REQUEST_TYPE, 0,
+				 priv->interface, rcvbuf, 1, WDR_TIMEOUT);
+	if (result < 0) {
+	} else if (result < 1)
+		result = -EIO;
+	else
+		result = !!(rcvbuf[0] & BIT(gpio));

Implement this as

	if (result < 1) {
		if (result >= 0)
			result = -EIO;
	} else {
		result = ...
	}

which follows a common pattern and doesn't violate the coding style.

Quoted 15 lines
drivers/usb/serial/ftdi_sio.c +15 -0
+
+	kfree(rcvbuf);
+
+	return result;
+}
+
+static void ftdi_gpio_set(struct gpio_chip *gc,
+			      unsigned int gpio,
+			      int value)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	int result;
+
+	if (value)

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1891

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+		priv->gpio_value |= BIT(gpio);
+	else
+		priv->gpio_value &= ~BIT(gpio);
+
+	result = ftdi_set_cbus_pins(port);
+	if (result < 0) {
+		dev_err(&port->serial->interface->dev,
+			"failed to set GPIO value: %d\n",
+			result);
+	}

You could now drop the dev_err here if you want.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1895

Quoted 4 lines
drivers/usb/serial/ftdi_sio.c +4 -0
+}
+
+static int ftdi_gpio_direction_get(struct gpio_chip *gc,
+				       unsigned int gpio)

Looks like there's no longer any need for a line break above (and elsewhere?).

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1900

Quoted 5 lines
drivers/usb/serial/ftdi_sio.c +5 -0
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	return priv->gpio_input & BIT(gpio);

Return 0 or 1 here as most (all?) drivers do.

Quoted 22 lines
drivers/usb/serial/ftdi_sio.c +22 -0
+}
+
+static int ftdi_gpio_direction_input(struct gpio_chip *gc,
+					 unsigned int gpio)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input |= BIT(gpio);
+
+	return ftdi_set_cbus_pins(port);
+}
+
+static int ftdi_gpio_direction_output(struct gpio_chip *gc,
+					  unsigned int gpio,
+					  int value)
+{
+	struct usb_serial_port *port = gpiochip_get_data(gc);
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	priv->gpio_input &= ~BIT(gpio);
+	if (value)

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1932

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+		priv->gpio_value |= BIT(gpio);
+	else
+		priv->gpio_value &= ~BIT(gpio);
+
+	return ftdi_set_cbus_pins(port);
+}
+
+/* Returns the number of bytes read */
+static int ftdi_read_eeprom(struct usb_serial *serial,
+			    void *dst,	/* must be kmalloc'd using GFP_KERNEL*/

Whether GFP_KERNEL was used is not really relevant, but highlighting that the buffer needs to be DMA-able is good.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1934

Quoted 2 lines
drivers/usb/serial/ftdi_sio.c +2 -0
+			    u16 addr,	/* must be aligned to 16 bits */
+			    u16 nbytes)	/* must be a multiple of 16 bits */

I think checkpatch gets confused by you odd argument comments here. Use a kernel doc comment, if you want to be this specific instead.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1938

Quoted 4 lines
drivers/usb/serial/ftdi_sio.c +4 -0
+{
+	int read = 0;
+
+	/* Argument checks */

Comment not needed.

Quoted 12 lines
drivers/usb/serial/ftdi_sio.c +12 -0
+	if (addr % 2 != 0)
+		return -EINVAL;
+	if (nbytes % 2 != 0)
+		return -EINVAL;
+
+	/* Read EEPROM two bytes at a time */
+	while (read < nbytes) {
+		int rv;
+
+		rv = usb_control_msg(serial->dev,
+				     usb_rcvctrlpipe(serial->dev, 0),
+				     FTDI_SIO_READ_EEPROM_REQUEST,

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1960

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+				     FTDI_SIO_READ_EEPROM_REQUEST_TYPE,
+				     0, (addr + read) / 2, dst + read, 2,
+				     WDR_TIMEOUT);
+		if (rv < 0)
+			return rv;
+
+		read += rv;
+
+		if (rv < 2)
+			break;

Treat any short read as an error.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1963

Quoted 3 lines
drivers/usb/serial/ftdi_sio.c +3 -0
+	}
+
+	return read;

Just return 0 on success and always return as many bytes as was requested.

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+}
+
+static int ftx_gpioconf_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	const u16 cbus_cfg_addr = 0x1a;
+	const u16 cbus_cfg_size = 8;
+	u8 *cbus_cfg_buf;
+	int result;

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1983

Quoted 10 lines
drivers/usb/serial/ftdi_sio.c +10 -0
+	u8 i;
+
+	/* Read a part of device EEPROM */
+	cbus_cfg_buf = kmalloc(cbus_cfg_size, GFP_KERNEL);
+	if (!cbus_cfg_buf)
+		return -ENOMEM;
+
+	result = ftdi_read_eeprom(serial, cbus_cfg_buf,
+				  cbus_cfg_addr, cbus_cfg_size);
+

No empty line.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1987

Quoted 4 lines
drivers/usb/serial/ftdi_sio.c +4 -0
+	if (result < 0)
+		goto out_free;
+	else if (result < 8) {
+		result = -EIO;

Just handle this case in the helper.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1990

Quoted 3 lines
drivers/usb/serial/ftdi_sio.c +3 -0
+		goto out_free;
+	} else
+		result = 0;

Note that you need brackets on all branches as per the coding style.

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1995

Quoted 5 lines
drivers/usb/serial/ftdi_sio.c +5 -0
+
+	/* Chip-type guessing logic based on libftdi. */
+	priv->gc.ngpio = 4;  /* FT230X, FT231X */
+	if (le16_to_cpu(serial->dev->descriptor.bcdDevice) != 0x1000)
+		priv->gc.ngpio = 1;  /* FT234XD */

No known way to identify FT234XD here?

After taking a quick peek at libftdi, it seems we really have no clue how to detect these device types and 0x1000 could be for all FTX devices. Heck, the current kernel driver just assumes anything we don't recognise to be an FTX, something which would now hit this code path...

What devices did you and Loic have? Could you post the lsusb -v output for these? Perhaps someone with an FT234XD can chime in as well.

Quoted 120 lines
drivers/usb/serial/ftdi_sio.c +90 -1
+
+	/* Determine which pins are configured for CBUS bitbanging */
+	priv->gpio_altfunc = 0xff;
+	for (i = 0; i < priv->gc.ngpio; ++i) {
+		if (cbus_cfg_buf[i] == FTDI_SIO_CBUS_MUX_GPIO)
+			priv->gpio_altfunc &= ~BIT(i);
+	}
+
+out_free:
+	kfree(cbus_cfg_buf);
+
+	return result;
+}
+
+static int ftdi_gpio_init(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_serial *serial = port->serial;
+	int result;
+
+	/* Device-specific initializations */
+	switch (priv->chip_type) {
+	case FTX:
+		result = ftx_gpioconf_init(port);
+		break;
+	default:
+		return 0;
+	}
+
+	if (result < 0)
+		return result;
+
+	/* Register GPIO chip to kernel */
+	priv->gc.label = "ftdi-cbus";
+	priv->gc.request = ftdi_gpio_request;
+	priv->gc.get_direction = ftdi_gpio_direction_get;
+	priv->gc.direction_input = ftdi_gpio_direction_input;
+	priv->gc.direction_output = ftdi_gpio_direction_output;
+	priv->gc.get = ftdi_gpio_get;
+	priv->gc.set = ftdi_gpio_set;
+	priv->gc.owner = THIS_MODULE;
+	priv->gc.parent = &serial->interface->dev;
+	priv->gc.base = -1;
+	priv->gc.can_sleep = true;
+
+	result = gpiochip_add_data(&priv->gc, port);
+	if (!result)
+		priv->gpio_registered = true;
+
+	return result;
+}
+
+static void ftdi_gpio_remove(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	if (priv->gpio_used) {
+		/* Remark: Exiting CBUS-mode does not reset pin states too */
+		ftdi_exit_cbus_mode(port);
+		priv->gpio_used = false;
+	}
+
+	if (priv->gpio_registered) {
+		gpiochip_remove(&priv->gc);
+		priv->gpio_registered = false;
+	}
+}
+
+#else
+
+static int ftdi_gpio_init(struct usb_serial_port *port)
+{
+	return 0;
+}
+
+static void ftdi_gpio_remove(struct usb_serial_port *port) { }
+
+#endif
+
 /*
  * ***************************************************************************
  * FTDI driver specific functions
@@ -1794,7 +2100,7 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv;
 	const struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial);
-
+	int result;
 
 	priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
 	if (!priv)
@@ -1813,6 +2119,14 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
 		priv->latency = 16;
 	write_latency_timer(port);
 	create_sysfs_attrs(port);
+
+	result = ftdi_gpio_init(port);
+	if (result < 0) {
+		dev_err(&port->serial->interface->dev,
+			"GPIO initialisation failed: %d\n",
+			result);
+	}
+
 	return 0;
 }
 
@@ -1930,6 +2244,8 @@ static int ftdi_sio_port_remove(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
 
+	ftdi_gpio_remove(port);
+
 	remove_sysfs_attrs(port);
 
 	kfree(priv);
drivers/usb/serial/ftdi_sio.h +0 -0
index dcd0b6e05baf..5a296e9e87a2 100644

drivers/usb/serial/ftdi_sio.h line 39

Quoted 6 lines
drivers/usb/serial/ftdi_sio.h +2 -1
@@ -35,7 +35,10 @@
 #define FTDI_SIO_SET_EVENT_CHAR		6 /* Set the event character */
 #define FTDI_SIO_SET_ERROR_CHAR		7 /* Set the error character */
 #define FTDI_SIO_SET_LATENCY_TIMER	9 /* Set the latency timer */
-#define FTDI_SIO_GET_LATENCY_TIMER	10 /* Get the latency timer */
+#define FTDI_SIO_GET_LATENCY_TIMER	0x0a /* Get the latency timer */
+#define FTDI_SIO_SET_BITMODE		0x0b /* Set CBUS GPIO pin mode */

That comment is too specific. It's just used to set *a* bitbang mode, right?

Quoted 21 lines
drivers/usb/serial/ftdi_sio.h +14 -0
+#define FTDI_SIO_READ_PINS		0x0c /* Read immediate value of pins */
+#define FTDI_SIO_READ_EEPROM		0x90 /* Read EEPROM */
 
 /* Interface indices for FT2232, FT2232H and FT4232H devices */
 #define INTERFACE_A		1
@@ -433,6 +436,28 @@ enum ftdi_sio_baudrate {
  *         1 = active
  */
 
+/* FTDI_SIO_SET_BITMODE */
+#define FTDI_SIO_SET_BITMODE_REQUEST_TYPE 0x40
+#define FTDI_SIO_SET_BITMODE_REQUEST FTDI_SIO_SET_BITMODE
+
+/* Possible bitmodes for FTDI_SIO_SET_BITMODE_REQUEST */
+#define FTDI_SIO_BITMODE_RESET		0x00
+#define FTDI_SIO_BITMODE_CBUS		0x20
+
+/* FTDI_SIO_READ_PINS */
+#define FTDI_SIO_READ_PINS_REQUEST_TYPE 0xc0
+#define FTDI_SIO_READ_PINS_REQUEST FTDI_SIO_READ_PINS
+

drivers/usb/serial/ftdi_sio.h enum ftdi_sio_baudrate { · line 460

Quoted 10 lines
drivers/usb/serial/ftdi_sio.h +10 -0
+/*
+ * FTDI_SIO_READ_EEPROM
+ *
+ * EEPROM format found in FTDI AN_201, "FT-X MTP memory Configuration",
+ * http://www.ftdichip.com/Support/Documents/AppNotes/AN_201_FT-X%20MTP%20Memory%20Configuration.pdf
+ */
+#define FTDI_SIO_READ_EEPROM_REQUEST_TYPE 0xc0
+#define FTDI_SIO_READ_EEPROM_REQUEST FTDI_SIO_READ_EEPROM
+
+#define FTDI_SIO_CBUS_MUX_GPIO		8

Again, this one is FTX specific and warrants an FTX infix (instead of SIO).

Johan

#2 lore raw

Hi,

Thanks again for the review.

Quoted 6 lines

drivers/usb/serial/ftdi_sio.c line 45

Quoted 4 lines

#include <linux/usb/serial.h>

drivers/usb/serial/ftdi_sio.c +3 -0
+#if defined(CONFIG_GPIOLIB)
+#include <linux/gpio/driver.h>
+#endif

Hmm. I already commented on this in v1.

Yeah, and I changed it too, but I now realized I misunderstood your intentions. You want me to remove the conditional compilation completely, while I thought you just prefer the #if defined() style instead of #ifdef. A misunderstanding on my part.

Quoted 4 lines
Quoted 4 lines

#include "ftdi_sio.h"
#include "ftdi_sio_ids.h"

Quoted 12 lines
Quoted 10 lines
(quoted diff) +0 -0
@@ -72,6 +75,14 @@ struct ftdi_private {

unsigned int latency; /* latency setting in use */
unsigned short max_packet_size;
struct mutex cfg_lock; /* Avoid mess by parallel calls of config ioctl() and change_speed() */ +#if defined(CONFIG_GPIOLIB)
+ struct gpio_chip gc;
+ bool gpio_registered; /* is the gpiochip in kernel registered */
+ bool gpio_used; /* true if the user requested a gpio */
+ u8 gpio_altfunc; /* which pins are in gpio mode */
+ u8 gpio_input; /* pin directions cache */

And I asked you to invert this one (i.e. replace with gpio_output).

Later when we discussed and you replied to my comments, I interpreted your response it can stay this way. Another misunderstanding, sorry.

Quoted 7 lines

drivers/usb/serial/ftdi_sio.c static void remove_sysfs_attrs(struct usb_serial_port *port) · line 1932

Quoted 3 lines
drivers/usb/serial/ftdi_sio.c +3 -0
+/* Returns the number of bytes read */
+static int ftdi_read_eeprom(struct usb_serial *serial,
+ void *dst, /* must be kmalloc'd using GFP_KERNEL*/

Whether GFP_KERNEL was used is not really relevant, but highlighting that the buffer needs to be DMA-able is good.

Quoted 5 lines
Quoted 2 lines

+ u16 addr, /* must be aligned to 16 bits */
+ u16 nbytes) /* must be a multiple of 16 bits */

I think checkpatch gets confused by you odd argument comments here. Use a kernel doc comment, if you want to be this specific instead.

I'll just remove the comments on the parameters of this function. The sb_control_msg is a dead giveaway the buffer must be DMA-able, and the error checks at the start make the requirements on the other parameters also obvious.

Quoted 8 lines
Quoted 5 lines

+
+ /* Chip-type guessing logic based on libftdi. */
+ priv->gc.ngpio = 4; /* FT230X, FT231X */
+ if (le16_to_cpu(serial->dev->descriptor.bcdDevice) != 0x1000)
+ priv->gc.ngpio = 1; /* FT234XD */

No known way to identify FT234XD here?

Quoted 8 lines

After taking a quick peek at libftdi, it seems we really have no clue how to detect these device types and 0x1000 could be for all FTX devices. Heck, the current kernel driver just assumes anything we don't recognise to be an FTX, something which would now hit this code path...

What devices did you and Loic have? Could you post the lsusb -v output for these? Perhaps someone with an FT234XD can chime in as well.

No clue about this one. I only own FT230X and FT231X devices, but it looks like they cannot be told apart, except for eeprom strings which are reconfigurable by the user. I wouldn't rely on such things. Anyway, lsusb -v outputs attached.

Karoly

maintainer
#3 lore raw
Quoted 13 lines · Karoly Pados
Quoted 13 lines
Quoted 5 lines

+
+ /* Chip-type guessing logic based on libftdi. */
+ priv->gc.ngpio = 4; /* FT230X, FT231X */
+ if (le16_to_cpu(serial->dev->descriptor.bcdDevice) != 0x1000)
+ priv->gc.ngpio = 1; /* FT234XD */

No known way to identify FT234XD here?

After taking a quick peek at libftdi, it seems we really have no clue how to detect these device types and 0x1000 could be for all FTX devices. Heck, the current kernel driver just assumes anything we don't recognise to be an FTX, something which would now hit this code path...

Quoted 7 lines
Quoted 3 lines

What devices did you and Loic have? Could you post the lsusb -v output for these? Perhaps someone with an FT234XD can chime in as well.

No clue about this one. I only own FT230X and FT231X devices, but it looks like they cannot be told apart, except for eeprom strings which are reconfigurable by the user. I wouldn't rely on such things. Anyway, lsusb -v outputs attached.

Ok, thanks. I've dropped a mail to FTDI support to see if there's way to tell FT234XD apart from the other two. Otherwise we probably need to register all four pins for all FTX devices.

It seems reasonable to assume that the FT234XD CBUS0 pin is indeed managed as CBUS0 of the other two types (rather than say CBUS2), but I asked for confirmation on that too.

Johan