From patchwork Tue Jun 26 12:54:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 139982 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5206273lji; Tue, 26 Jun 2018 05:54:55 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK+qp4PQdqSWHRv95u/vduRDUbGjZdhFQT95I9u2wVekDi88w6VW2rNPmxETHG4myeItkwV X-Received: by 2002:a17:902:b418:: with SMTP id x24-v6mr1544416plr.2.1530017695436; Tue, 26 Jun 2018 05:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530017695; cv=none; d=google.com; s=arc-20160816; b=EBqEFV8Z1SsDRHLvX5hqA5zU1srl3kHpB+YvpUJDZTH11JUQoyHsPw8UZ4asdtCgk7 kxre5leCRDBBwYTL736mLyjC0BMCQ5PRZpiossifWV8S4TryTMtoalCddZ1tsyEstSY6 lFq5nGaLyGHB0MNpd7P7OGEIZN4QMY30eczw1611pGJtllMB1tZf8tDMH37LKt9aDTn2 rnsInZSQLdtLUkOwYXvjDX/CuWWtBg5ECCs4SC4wvIzfgdcyOQ/y/WvKIx/CyB/mXcQP 5x+lQFFI2CJj4OeuUNr9+FzFJhE3g5ZfwbxAixhFEBOYsF7uYGOShhtP1xzO5iJP9c4L 1s9w== 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=ljmDZajDZ9vAWxbRt+Di7NaaweWd8HOAZa1g9/Xbry4=; b=pGERJq+gLKfMzEyVSE1B69BfugVEa1RpRcSEKAuzDArNytz3vf16fYhhsKq6Qr1sc0 8JwgdM/jqf27f5BDkmte1MY0vEv3QU7JiRDxE4ZWoPiBAx3rCB8tA6oya6c1SYDutUCO 4QEsKJT+mdGu9UhrD/KbfH+eK/vNmgvYcXFt16FPjQyibEEDrPkccLKarqO4UTXI8hhw BRY18IX/2YgmpgOEeAG8rNkJXW7qnMoFQs1hHVeOLsCFdqmFsh1GnFCGUZLs1TE4HXUR K74NZzOhBIlnF/q6DN+2NslztIX6DXr19aqodi5zXtaedUNfDMf674EtjmitLlNJpuCu OaLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=kQyWzXg5; 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 p88-v6si1546071pfj.294.2018.06.26.05.54.55; Tue, 26 Jun 2018 05:54:55 -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=kQyWzXg5; 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 S965027AbeFZMyx (ORCPT + 5 others); Tue, 26 Jun 2018 08:54:53 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:35723 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964803AbeFZMyx (ORCPT ); Tue, 26 Jun 2018 08:54:53 -0400 Received: by mail-wm0-f66.google.com with SMTP id z137-v6so1745241wmc.0 for ; Tue, 26 Jun 2018 05:54:52 -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=552k6KcaHXEI2qXODIETGPbL7C+EzRQKoIc804pWHlA=; b=kQyWzXg5HzFFwfkR1frSUkvU9l29UZt9lbz0SJpp4ajWDv1zFNlD7HojhaOdZgbboe IUbXJ6XLdzvV9afVXjMIDtg1Ph0azqQGrs2aPNmVKbkXeOmu3oYlbhfk1fJIcvV+5Zh5 HugTOqRa7umdJdmBo2aj2RJHmHjIDnTD6giUQ= 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=552k6KcaHXEI2qXODIETGPbL7C+EzRQKoIc804pWHlA=; b=TOITMWrfFS3f7MFmQIhkZLEVC4WCZ6rlmQq68r4XJAxCoGdFbrumgu1EyMQQ6vYJSx 6RWO+tzk3J8Z38RfJKPUsxTCWQ7RQra7h0qo/zs/RAk0khzYXcVOEtm5m8lPHe3RS0Nz 8QA+uzGw7dQo9d4DabbVMxx+wlyvG9LnEmc/vez/jI2D02KM1JHc8m/31W6Va74qtexG 11xdtcqTfVfXJ4y+/bP75GdHz3nwbSJASw5+irRGGYhAWKCTqfSqx0fBIGS/FJg6vSjD 9wxo8CIvCOarBzXETlfpFXzp+UtDRUXZE6QgwVmaS0UMLGJMYeDLd+zFmAu1SU5RpiB7 1IAA== X-Gm-Message-State: APt69E3+yB0a0jWSliITpc7BsYflROFx2N5NFQGy+O5oHhxtxFvCLaJq 3d1A42Fm5XxQ4lM8/irpbkOa1Q== X-Received: by 2002:a1c:2fc7:: with SMTP id v190-v6mr1502298wmv.151.1530017691833; Tue, 26 Jun 2018 05:54:51 -0700 (PDT) Received: from lpoulain-ThinkPad-T470p.home (AToulouse-655-1-762-165.w109-220.abo.wanadoo.fr. [109.220.142.165]) by smtp.gmail.com with ESMTPSA id u15-v6sm3217083wma.37.2018.06.26.05.54.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jun 2018 05:54:51 -0700 (PDT) From: Loic Poulain To: johan@kernel.org Cc: linux-usb@vger.kernel.org, srinivas.kandagatla@linaro.org, andy.shevchenko@gmail.com, ajaykuee@gmail.com, daniel.thompson@linaro.org, Loic Poulain Subject: [PATCH v6] USB: serial: ftdi_sio: Add MTP NVM support Date: Tue, 26 Jun 2018 14:54:48 +0200 Message-Id: <1530017688-1731-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. For example, FT232R and FTX chips have 128-byte and 2048-byte internal EEPROM respectively. 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. Reviewed-by: Andy Shevchenko 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 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 v4: Reword commit message Remove default-yes from Kconfig Change includes ordering Use default linux size defines Use get_unaligned_le16 helper Prepend EEPROM functions with ftdi_ Error message in ftdi_eeprom_register() v5: Fix missing linux/sizes header v6: Ordering new headers insertion Remove unecessary additional buf pointer from read/write_eeprom drivers/usb/serial/Kconfig | 10 ++++ drivers/usb/serial/ftdi_sio.c | 118 ++++++++++++++++++++++++++++++++++++++++++ drivers/usb/serial/ftdi_sio.h | 28 ++++++++++ 3 files changed, 156 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 Reviewed-by: Ajay Gupta diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig index 533f127..5747562 100644 --- a/drivers/usb/serial/Kconfig +++ b/drivers/usb/serial/Kconfig @@ -181,6 +181,16 @@ 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 + 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..34daa8c 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -34,12 +34,17 @@ #include #include #include +#include +#include #include #include #include #include #include #include + +#include + #include "ftdi_sio.h" #include "ftdi_sio_ids.h" @@ -73,6 +78,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 +1536,110 @@ static int get_lsr_info(struct usb_serial_port *port, return 0; } +#ifdef CONFIG_USB_SERIAL_FTDI_SIO_NVMEM + +static int ftdi_write_eeprom(void *priv, unsigned int off, void *val, + size_t bytes) +{ + struct usb_serial_port *port = priv; + struct usb_device *udev = port->serial->dev; + + if (bytes % 2) /* 16-bit eeprom */ + return -EINVAL; + + while (bytes) { + int rv; + + rv = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), + FTDI_SIO_WRITE_EEPROM_REQUEST, + FTDI_SIO_WRITE_EEPROM_REQUEST_TYPE, + get_unaligned_le16(val), off / 2, NULL, + 0, WDR_TIMEOUT); + if (rv < 0) + return rv; + + off += 2; + val += 2; + bytes -= 2; + } + + return 0; +} + +static int ftdi_read_eeprom(void *priv, unsigned int off, void *val, + size_t bytes) +{ + struct usb_serial_port *port = priv; + struct usb_device *udev = port->serial->dev; + + 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, val, 2, WDR_TIMEOUT); + if (rv < 0) + return rv; + + off += 2; + val += 2; + bytes -= 2; + } + + return 0; +} + +static int ftdi_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 = SZ_2K; + break; + case FT232RL: + nvmconf.size = SZ_128; + 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 = ftdi_read_eeprom; + nvmconf.reg_write = ftdi_write_eeprom; + nvmconf.owner = THIS_MODULE; + + priv->eeprom = nvmem_register(&nvmconf); + if (IS_ERR(priv->eeprom)) { + dev_err(&udev->dev, "Unable to register FTDI EEPROM\n"); + priv->eeprom = NULL; + return -ENOMEM; + } + + dev_info(&udev->dev, "Registered %d-byte FTDI EEPROM\n", nvmconf.size); + + return 0; +} + +static void ftdi_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 +1925,10 @@ 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 + ftdi_register_eeprom(port); +#endif + return 0; } @@ -1931,6 +2046,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 + ftdi_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 */