diff mbox series

[v3] USB: serial: ftdi_sio: Add MTP NVM support

Message ID 1529677321-2341-1-git-send-email-loic.poulain@linaro.org
State Superseded
Headers show
Series [v3] USB: serial: ftdi_sio: Add MTP NVM support | expand

Commit Message

Loic Poulain June 22, 2018, 2:22 p.m. UTC
Most of FTDI's devices have an EEPROM which records FTDI devices
configuration setting (e.g. the VID, PID, I/O config...) and user
data. FT230R chip integrates a 128-byte eeprom, FT230X a 2048-byte
eeprom...

This patch adds support for FTDI EEPROM read/write via USB control
transfers and register a new nvm device to the nvmem core.

This permits to expose the eeprom as a sysfs file, allowing userspace
to read/modify FTDI configuration and its user data without having to
rely on a specific userspace USB driver.

Moreover, any upcoming new tentative to add CBUS GPIO support could
integrate CBUS EEPROM configuration reading in order to determine
which of the CBUS pins are available as GPIO.

Signed-off-by: Loic Poulain <loic.poulain@linaro.org>

---
 v2: Use ifdef instead of IS_ENABLED
     error message in case of nvmem registering failure
     Fix space/tab in Kconfig
 v3: Make nvmem a child of the usb dev instead of the serial port
     Add macros defining eeprom sizes
     Check read/write size is a nultiple of the eeprom word-size
     Remove useless change in Kconfig

 drivers/usb/serial/Kconfig    |  11 ++++
 drivers/usb/serial/ftdi_sio.c | 123 ++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/serial/ftdi_sio.h |  28 ++++++++++
 3 files changed, 162 insertions(+)

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Andy Shevchenko June 25, 2018, 7:36 a.m. UTC | #1
On Fri, Jun 22, 2018 at 5:22 PM, Loic Poulain <loic.poulain@linaro.org> wrote:
> Most of FTDI's devices have an EEPROM which records FTDI devices

> configuration setting (e.g. the VID, PID, I/O config...) and user

> data.


> FT230R chip integrates a 128-byte eeprom, FT230X a 2048-byte

> eeprom...


This sounds unfinished. What is the continuation of the sentence?

>

> This patch adds support for FTDI EEPROM read/write via USB control

> transfers and register a new nvm device to the nvmem core.

>

> This permits to expose the eeprom as a sysfs file, allowing userspace

> to read/modify FTDI configuration and its user data without having to

> rely on a specific userspace USB driver.

>

> Moreover, any upcoming new tentative to add CBUS GPIO support could

> integrate CBUS EEPROM configuration reading in order to determine

> which of the CBUS pins are available as GPIO.


In some cases you are using EEPROM, in the rest, eeprom. What the difference?

> +config USB_SERIAL_FTDI_SIO_NVMEM

> +       bool "FTDI MTP non-volatile memory support"

> +       depends on USB_SERIAL_FTDI_SIO

> +       select NVMEM


> +       default y


First of all, I didn't find an evidence why it should be y...

> +       help

> +         Say yes here to add support for the MTP non-volatile memory

> +         present on FTDI. Most of FTDI's devices have an EEPROM which

> +         records FTDI device's configuration setting as well as user

> +         data.


...second, the help semantics is inconsistent with default.

>  #include <linux/usb.h>

>  #include <linux/serial.h>

>  #include <linux/usb/serial.h>

> +#include <linux/nvmem-provider.h>


Perhaps squeeze it into most ordered part and keep there an order?

> +#define EEPROM_SZ_FTX 2048 /* cf FTDI AN_201 */

> +#define EEPROM_SZ_FT232RL 128  /* cf FTDI AN_121 */


These defines looks too particular, shouldn't be this a part of driver
data / platform data / device properties?
Also, above I think is already defined in a common way in linux/sizes.h.

> +static int write_eeprom(void *priv, unsigned int off, void *_val, size_t bytes)


Leading _ in the name looks weird here, since it's not a macro.
(and inconsistent with below function definitions)

> +               val = buf[0] + (buf[1] << 8);


get_unaligned_le16() ?


> +       if (register_eeprom(port)) {


> +               dev_err(&port->dev, "Unable to register FTDI EEPROM\n");

> +               /* non-fatal */


Doesn't register_eeprom() have some error messaging already?
Btw, can we rename it to be less generic, like adding a prefix?

> +       }


-- 
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Loic Poulain June 25, 2018, 8:35 a.m. UTC | #2
Hi Andy,

On 25 June 2018 at 09:36, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> On Fri, Jun 22, 2018 at 5:22 PM, Loic Poulain <loic.poulain@linaro.org> wrote:

>> Most of FTDI's devices have an EEPROM which records FTDI devices

>> configuration setting (e.g. the VID, PID, I/O config...) and user

>> data.

>

>> FT230R chip integrates a 128-byte eeprom, FT230X a 2048-byte

>> eeprom...

>

> This sounds unfinished. What is the continuation of the sentence?

>

>>

>> This patch adds support for FTDI EEPROM read/write via USB control

>> transfers and register a new nvm device to the nvmem core.

>>

>> This permits to expose the eeprom as a sysfs file, allowing userspace

>> to read/modify FTDI configuration and its user data without having to

>> rely on a specific userspace USB driver.

>>

>> Moreover, any upcoming new tentative to add CBUS GPIO support could

>> integrate CBUS EEPROM configuration reading in order to determine

>> which of the CBUS pins are available as GPIO.

>

> In some cases you are using EEPROM, in the rest, eeprom. What the difference?


I'll unify this.

>

>> +config USB_SERIAL_FTDI_SIO_NVMEM

>> +       bool "FTDI MTP non-volatile memory support"

>> +       depends on USB_SERIAL_FTDI_SIO

>> +       select NVMEM

>

>> +       default y

>

> First of all, I didn't find an evidence why it should be y...


Yes, I'll fix this.

>

>> +       help

>> +         Say yes here to add support for the MTP non-volatile memory

>> +         present on FTDI. Most of FTDI's devices have an EEPROM which

>> +         records FTDI device's configuration setting as well as user

>> +         data.

>

> ...second, the help semantics is inconsistent with default.

>

>>  #include <linux/usb.h>

>>  #include <linux/serial.h>

>>  #include <linux/usb/serial.h>

>> +#include <linux/nvmem-provider.h>

>

> Perhaps squeeze it into most ordered part and keep there an order?


OK

>

>> +#define EEPROM_SZ_FTX 2048 /* cf FTDI AN_201 */

>> +#define EEPROM_SZ_FT232RL 128  /* cf FTDI AN_121 */

>

> These defines looks too particular, shouldn't be this a part of driver

> data / platform data / device properties?


I'm not sure where to move this since it depends on chip type which is
detected at runtime. I don't think ftdi_sio_quirk is the right place.
So maybe I could just skip the defines for now and come back to:

switch (priv->chip_type) {
    case FTX:
        nvmconf.size = SZ_2K;
        break;
...


> Also, above I think is already defined in a common way in linux/sizes.h.


Correct

>

>> +static int write_eeprom(void *priv, unsigned int off, void *_val, size_t bytes)

>

> Leading _ in the name looks weird here, since it's not a macro.

> (and inconsistent with below function definitions)

>

>> +               val = buf[0] + (buf[1] << 8);

>

> get_unaligned_le16() ?

>

>

>> +       if (register_eeprom(port)) {

>

>> +               dev_err(&port->dev, "Unable to register FTDI EEPROM\n");

>> +               /* non-fatal */

>

> Doesn't register_eeprom() have some error messaging already?


I can move this into register_eeprom for consistency.

> Btw, can we rename it to be less generic, like adding a prefix?


OK, I'll add ftdi_ prefix.

>

>> +       }

>

> --

> With Best Regards,

> Andy Shevchenko


Thanks,
Loic
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Johan Hovold June 25, 2018, 8:40 a.m. UTC | #3
On Mon, Jun 25, 2018 at 10:35:31AM +0200, Loic Poulain wrote:
> Hi Andy,

> 

> On 25 June 2018 at 09:36, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> > On Fri, Jun 22, 2018 at 5:22 PM, Loic Poulain <loic.poulain@linaro.org> wrote:


> >> +#define EEPROM_SZ_FTX 2048 /* cf FTDI AN_201 */

> >> +#define EEPROM_SZ_FT232RL 128  /* cf FTDI AN_121 */

> >

> > These defines looks too particular, shouldn't be this a part of driver

> > data / platform data / device properties?

> 

> I'm not sure where to move this since it depends on chip type which is

> detected at runtime. I don't think ftdi_sio_quirk is the right place.

> So maybe I could just skip the defines for now and come back to:

> 

> switch (priv->chip_type) {

>     case FTX:

>         nvmconf.size = SZ_2K;

>         break;

> ...


Yes, please do that. The EEPROM_SZ_ defines you added in this (?)
revision add no value.

Thanks,
Johan
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index 533f127..ee9230c 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -181,6 +181,17 @@  config USB_SERIAL_FTDI_SIO
 	  To compile this driver as a module, choose M here: the
 	  module will be called ftdi_sio.
 
+config USB_SERIAL_FTDI_SIO_NVMEM
+	bool "FTDI MTP non-volatile memory support"
+	depends on USB_SERIAL_FTDI_SIO
+	select NVMEM
+	default y
+	help
+	  Say yes here to add support for the MTP non-volatile memory
+	  present on FTDI. Most of FTDI's devices have an EEPROM which
+	  records FTDI device's configuration setting as well as user
+	  data.
+
 config USB_SERIAL_VISOR
 	tristate "USB Handspring Visor / Palm m50x / Sony Clie Driver"
 	help
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 7ea221d..8183b02 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -40,6 +40,7 @@ 
 #include <linux/usb.h>
 #include <linux/serial.h>
 #include <linux/usb/serial.h>
+#include <linux/nvmem-provider.h>
 #include "ftdi_sio.h"
 #include "ftdi_sio_ids.h"
 
@@ -73,6 +74,8 @@  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() */
+
+	struct nvmem_device *eeprom;
 };
 
 /* struct ftdi_sio_quirk is used by devices requiring special attention. */
@@ -1529,6 +1532,116 @@  static int get_lsr_info(struct usb_serial_port *port,
 	return 0;
 }
 
+#ifdef CONFIG_USB_SERIAL_FTDI_SIO_NVMEM
+
+#define EEPROM_SZ_FTX 2048 /* cf FTDI AN_201 */
+#define EEPROM_SZ_FT232RL 128  /* cf FTDI AN_121 */
+
+static int write_eeprom(void *priv, unsigned int off, void *_val, size_t bytes)
+{
+	struct usb_serial_port *port = priv;
+	struct usb_serial *serial = port->serial;
+	struct usb_device *udev = serial->dev;
+	unsigned char *buf = _val;
+
+	if (bytes % 2) /* 16-bit eeprom */
+		return -EINVAL;
+
+	while (bytes) {
+		uint16_t val;
+		int rv;
+
+		val = buf[0] + (buf[1] << 8);
+
+		rv = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+				     FTDI_SIO_WRITE_EEPROM_REQUEST,
+				     FTDI_SIO_WRITE_EEPROM_REQUEST_TYPE,
+				     val, off / 2, NULL, 0, WDR_TIMEOUT);
+		if (rv < 0)
+			return rv;
+
+		off += 2;
+		buf += 2;
+		bytes -= 2;
+	}
+
+	return 0;
+}
+
+static int read_eeprom(void *priv, unsigned int off, void *val, size_t bytes)
+{
+	struct usb_serial_port *port = priv;
+	struct usb_serial *serial = port->serial;
+	struct usb_device *udev = serial->dev;
+	unsigned char *buf = val;
+
+	if (bytes % 2) /* 16-bit eeprom */
+		return -EINVAL;
+
+	while (bytes) {
+		int rv;
+
+		rv = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+				     FTDI_SIO_READ_EEPROM_REQUEST,
+				     FTDI_SIO_READ_EEPROM_REQUEST_TYPE,
+				     0, off / 2, buf, 2, WDR_TIMEOUT);
+		if (rv < 0)
+			return rv;
+
+		off += 2;
+		buf += 2;
+		bytes -= 2;
+	}
+
+	return 0;
+}
+
+static int register_eeprom(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+	struct usb_device *udev = port->serial->dev;
+	struct nvmem_config nvmconf = {};
+
+	switch (priv->chip_type) {
+	case FTX:
+		nvmconf.size = EEPROM_SZ_FTX;
+		break;
+	case FT232RL:
+		nvmconf.size = EEPROM_SZ_FT232RL;
+		break;
+	default:
+		return 0;
+	}
+
+	nvmconf.word_size = 2;
+	nvmconf.stride = 2;
+	nvmconf.read_only = false;
+	nvmconf.priv = port;
+	nvmconf.dev = &udev->dev;
+	nvmconf.reg_read = read_eeprom;
+	nvmconf.reg_write = write_eeprom;
+	nvmconf.owner = THIS_MODULE;
+
+	priv->eeprom = nvmem_register(&nvmconf);
+	if (IS_ERR(priv->eeprom)) {
+		priv->eeprom = NULL;
+		return -ENOMEM;
+	}
+
+	dev_info(&udev->dev, "Registered %d-byte FTDI EEPROM\n", nvmconf.size);
+
+	return 0;
+}
+
+static void unregister_eeprom(struct usb_serial_port *port)
+{
+	struct ftdi_private *priv = usb_get_serial_port_data(port);
+
+	if (priv->eeprom)
+		nvmem_unregister(priv->eeprom);
+}
+
+#endif /* CONFIG_USB_SERIAL_FTDI_SIO_NVMEM */
 
 /* Determine type of FTDI chip based on USB config and descriptor. */
 static void ftdi_determine_type(struct usb_serial_port *port)
@@ -1814,6 +1927,13 @@  static int ftdi_sio_port_probe(struct usb_serial_port *port)
 		priv->latency = 16;
 	write_latency_timer(port);
 	create_sysfs_attrs(port);
+#ifdef CONFIG_USB_SERIAL_FTDI_SIO_NVMEM
+	if (register_eeprom(port)) {
+		dev_err(&port->dev, "Unable to register FTDI EEPROM\n");
+		/* non-fatal */
+	}
+#endif
+
 	return 0;
 }
 
@@ -1931,6 +2051,9 @@  static int ftdi_sio_port_remove(struct usb_serial_port *port)
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
 
+#ifdef CONFIG_USB_SERIAL_FTDI_SIO_NVMEM
+	unregister_eeprom(port);
+#endif
 	remove_sysfs_attrs(port);
 
 	kfree(priv);
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index dcd0b6e..9eab961 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -36,6 +36,8 @@ 
 #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_READ_EEPROM		0x90 /* Read eeprom */
+#define FTDI_SIO_WRITE_EEPROM		0x91 /* Write eeprom */
 
 /* Interface indices for FT2232, FT2232H and FT4232H devices */
 #define INTERFACE_A		1
@@ -400,6 +402,32 @@  enum ftdi_sio_baudrate {
  *
  */
 
+ /* FTDI_SIO_READ_EEPROM */
+#define FTDI_SIO_READ_EEPROM_REQUEST_TYPE 0xc0
+#define FTDI_SIO_READ_EEPROM_REQUEST FTDI_SIO_READ_EEPROM
+/*
+ *  BmRequestType:   1100 0000b
+ *  bRequest:        FTDI_SIO_READ_EEPROM
+ *  wValue:          0
+ *  wIndex:          Word Index
+ *  wLength:         2
+ *  Data:            return data (a word)
+ *
+ */
+
+/* FTDI_SIO_WRITE_EEPROM */
+#define FTDI_SIO_WRITE_EEPROM_REQUEST_TYPE 0x40
+#define FTDI_SIO_WRITE_EEPROM_REQUEST FTDI_SIO_WRITE_EEPROM
+/*
+ *  BmRequestType:   0100 0000b
+ *  bRequest:        FTDI_SIO_WRITE_EEPROM
+ *  wValue:          Data (word)
+ *  wIndex:          Word Index
+ *  wLength:         0
+ *  Data:            None
+ *
+ */
+
 /* FTDI_SIO_GET_MODEM_STATUS */
 /* Retrieve the current value of the modem status register */