From patchwork Thu Jun 21 16:49:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 139554 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp2310884lji; Thu, 21 Jun 2018 09:49:40 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJsz9xGwW0zH55T6NjcP20CrPfsimupy2rmku9MZ/8W2tAwvtSjHOAw8F8zB4f2fGx7gw1A X-Received: by 2002:a65:4b82:: with SMTP id t2-v6mr22628433pgq.175.1529599780257; Thu, 21 Jun 2018 09:49:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529599780; cv=none; d=google.com; s=arc-20160816; b=MKkOx4OMLEcRDqhkPaUd0/Dlvq06juMwLNbmZzBUYJrs2jZ9TfLiA5ReadI1N20l+I HMZoBennnxVwH6Xa+FvGh2XJpH4Q8AabDouxPL0Ma5/ctl8i00338ayrxkErmtWZLKKc tbXDssa5dFDtSyrUFgJVTEU5/j/HP396A9DB8jSmXWrIeJ2ItApvx99Dosr37hDlgq5B qAEOM/68COTJsDxJfCN0I3L9PylTXDkbvNUcs5kt4RRKbjW9JpdTxJ+fF2+9TzoJOq8K RPemC0iU40v2ovCQ73rXCu+3I1y5mPnHtufkE3LUqupW2vB1lb9ElAJEmcHCpwgkxvYW YBEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=/M3ZYmIiUb72NshsSY9WcWhZaHJkGW1ZMVzJBdlW4Jw=; b=yeCgAaS/F7XqAphNT2/EPBCXsG1tSpEhi9ViAt0Exnr2j+OdOlBTbxXb5EJNVmGN7x 3YpCZcVrMTzsbbQu2xTO9FAO37gOLsJSDpYvpovctVUWWDItPt7mHS5m8JINOt6PEHrk 1FetqOHvPq6RwX5s/qKa2KIHC1z59uDmZuFqFQmijeEPkQ6jl8LyfUaQUItH2KrI4CNv Ct0gEC5Ofc7oNSRc301+GwK9EUUvybm58OpId8tNkPLAHSoQIC1x7wveljt5KPfEj0yZ vMoaVT41I2MPgp7BNP4e4+OyDk9dPQ6N6qa5NuFK1wvASODzZY76+WeIqPZjiUBAzYfn Rpdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=j1s9Z2QV; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n7-v6si4443054pgr.31.2018.06.21.09.49.39; Thu, 21 Jun 2018 09:49:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=j1s9Z2QV; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933454AbeFUQtg (ORCPT + 5 others); Thu, 21 Jun 2018 12:49:36 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:46725 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933360AbeFUQte (ORCPT ); Thu, 21 Jun 2018 12:49:34 -0400 Received: by mail-wr0-f193.google.com with SMTP id v13-v6so3881265wrp.13 for ; Thu, 21 Jun 2018 09:49:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=z/7tMszVqVb0rXHfIPy4ceKHIfp18OEtdrNVZAinI1I=; b=j1s9Z2QVj7NLPDCct+0fq1kBQCk7nk4we45mRH7TO/n2PVG/lyWe4UFUhJq6v3sGg+ paoyMZI3zKue031fgQfUk2ELBqUQtT4ipgE3k1WpIFnSj1hhD3tOuAVnUm6EjHQPFA8C LkYxF95DsBAoGInUjmqvGymUQz+2mZgVYo8Yg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=z/7tMszVqVb0rXHfIPy4ceKHIfp18OEtdrNVZAinI1I=; b=TnXpAvsAYKfVfzWEqQvl0zPG26ULEgQhyMv++ZGtdyLfXDpXg+pICYtI8XzMYnhl8w 7YYjrrk0Q+kJHPwvAgUSIpAdTNdAqQ9g4tRkgpZE3DA5V7ozYmWQf3A9a3CT2MKTTFRC cEmvLZDgw3c08s416BuYPuQHcDdAb8zEwy3ISaJ1W6uIiUHIO2ADkMrVsN4eIbGZJE9A JdCjT58X7a2LhIjV8OvMRxHlDOPs1v4J5qteOf3qyuqhD6rrJ2x8FS4stAg5xhm2BHNG 8zyuFC0Fg06907t4aWzLz6OhymxYj/9mI3HXuwJ184M8rxtJ+B6W13wT/NFe9i5NRi2t V39g== X-Gm-Message-State: APt69E1LEs9XE6Wfk6ph/rFg/k4p+E5tKYi29zeVlap1AswUNwDTVomI z8nCy6o+RjSz6b9g781wsD+anQ== X-Received: by 2002:adf:cd85:: with SMTP id q5-v6mr21430892wrj.275.1529599772561; Thu, 21 Jun 2018 09:49:32 -0700 (PDT) Received: from lpoulain-ThinkPad-T470p.home (LFbn-1-2080-109.w90-76.abo.wanadoo.fr. [90.76.145.109]) by smtp.gmail.com with ESMTPSA id c18-v6sm7954024wrq.17.2018.06.21.09.49.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 21 Jun 2018 09:49:31 -0700 (PDT) From: Loic Poulain To: johan@kernel.org Cc: linux-usb@vger.kernel.org, srinivas.kandagatla@linaro.org, Loic Poulain Subject: [PATCH v2] USB: serial: ftdi_sio: Add MTP NVM support Date: Thu, 21 Jun 2018 18:49:30 +0200 Message-Id: <1529599770-18133-1-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 --- v2: Use ifdef instead of IS_ENABLED error message in case of nvmem registering failure Fix space/tab in Kconfig drivers/usb/serial/Kconfig | 13 ++++- drivers/usb/serial/ftdi_sio.c | 111 ++++++++++++++++++++++++++++++++++++++++++ drivers/usb/serial/ftdi_sio.h | 28 +++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) -- 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 diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig index 533f127..f05af5f 100644 --- a/drivers/usb/serial/Kconfig +++ b/drivers/usb/serial/Kconfig @@ -153,7 +153,7 @@ config USB_SERIAL_CYPRESS_M8 Supported microcontrollers in the CY4601 family are: CY7C63741 CY7C63742 CY7C63743 CY7C64013 - + To compile this driver as a module, choose M here: the module will be called cypress_m8. @@ -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..9e242e8 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -40,6 +40,7 @@ #include #include #include +#include #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,104 @@ static int get_lsr_info(struct usb_serial_port *port, return 0; } +#ifdef CONFIG_USB_SERIAL_FTDI_SIO_NVMEM + +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; + + while (bytes) { /* bytes value is always a multiple of 2 */ + 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; + + while (bytes) { /* bytes value is always a multiple of 2 */ + 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 nvmem_config nvmconf = {}; + + switch (priv->chip_type) { + case FTX: + nvmconf.size = 2048; + break; + case FT232RL: + nvmconf.size = 128; + break; + default: + return 0; + } + + nvmconf.word_size = 2; + nvmconf.stride = 2; + nvmconf.read_only = false; + nvmconf.priv = port; + nvmconf.dev = &port->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; + } + + 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 +1915,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 +2039,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 */