* [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes
@ 2026-08-22 21:39 Michael Zaidman
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
` (13 more replies)
0 siblings, 14 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
Mainline supports only the FT260's I2C bridge. This series adds the
UART/TTY interface and GPIO on top of it, and carries the I2C fixes made
alongside them - two years of out-of-tree work [1], now replayed upstream.
It picks up work that stalled twice: Christina Quast's serial driver,
posted as v4 and v5 and never applied, and my own pre-UART GPIO series
from 2023. Patches 1 and 3 link both threads. This is a new series rather
than a v6, being much more than the serial driver now.
The patches keep the order and the granularity in which the work was
developed, so that authorship stays visible: squashing would fold several
people's changes under one name. The UART and I2C changes also interleave
in the code, so regrouping them by interface would mean a rewrite and a
full retest. Only adjacent commits were squashed. Patch 1 is Christina's,
patch 10 is Rio Liu's.
Tested on a UMFT260EV1A with a 24LC512 EEPROM. I2C - reads and writes
that cross the 60-byte write chunk and the 60/180-byte read chunk caps,
SMBus byte-data and block reads, and the shortened read timeout with the
abort path of patch 13. UART - a 2.8 MB pattern in both directions at
115200 and 1.5 Mbaud against an FT2232 peer, without flow control and
with XON/XOFF and RTS/CTS, plus DTR/RTS toggling over TIOCM*. GPIO -
through libgpiod and legacy sysfs. Every commit builds with W=1, and
checkpatch --strict reports no errors and no warnings across the series.
[1] https://github.com/MichaelZaidman/hid-ft260
Christina Quast (1):
HID: ft260: add serial driver
Michael Zaidman (11):
HID: ft260: uart: bring-up fixes
HID: ft260: add GPIO support on top of UART
HID: ft260: i2c: reduce driver module loading time
HID: ft260: i2c: silence sysfs store big-numbers
HID: ft260: i2c: reduce bus-error message severity
HID: ft260: uart: enable flow control
HID: ft260: uart: add modem pins control via ioctl
HID: ft260: gpio: group sysfs attrs per HID interface
HID: ft260: i2c: fix large write transaction failure
HID: ft260: workaround for TN_189 errata endpoint STALL after
enumeration
HID: ft260: i2c: abort in-flight transfers with STOP before reset
Rio Liu (1):
HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity
drivers/hid/Kconfig | 10 +-
drivers/hid/hid-ft260.c | 1967 ++++++++++++++++++++++++++++++++++++---
2 files changed, 1830 insertions(+), 147 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 01/13] HID: ft260: add serial driver
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 7:49 ` Linus Walleij
2026-08-25 8:12 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 02/13] HID: ft260: uart: bring-up fixes Michael Zaidman
` (12 subsequent siblings)
13 siblings, 2 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Daniel Beer, Michael Zaidman
From: Christina Quast <contact@christina-quast.de>
Add a TTY serial interface (/dev/ttyFTx) for FT260 USB HID devices,
providing baud rate configuration, data transfer, and termios settings
without requiring userspace HID report formatting.
The FT260 exposes I2C and UART through separate HID interfaces. Only
I2C was supported previously; UART required FTDI hidraw example code.
This is a port of the v4 patch, adapted to the I2C hardening that has
since been merged upstream: input-report length validation, the
read_buf/raw_event race fix with read_lock, and SMBus block-read
START/STOP handling. HID_FT260 now depends on TTY because the UART code
is part of this driver.
Routing UART reports through ft260_raw_event() also made v4 fall through
the report dispatch after an I2C input report had already been consumed,
so every I2C input report was additionally reported as unhandled. This
regression is fixed here.
Ported from:
https://lore.kernel.org/all/20231218093153.192268-1-contact@christina-quast.de/
The follow-up v5 series stalled without being applied:
https://lore.kernel.org/linux-input/20240216-ft260_review5-v5-1-36db44673ac7@christina-quast.de/
Signed-off-by: Christina Quast <contact@christina-quast.de>
Co-developed-by: Daniel Beer <daniel.beer@igorinstitute.com>
Signed-off-by: Daniel Beer <daniel.beer@igorinstitute.com>
Co-developed-by: Michael Zaidman <michael.zaidman@gmail.com>
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/Kconfig | 10 +-
drivers/hid/hid-ft260.c | 842 +++++++++++++++++++++++++++++++++++++---
2 files changed, 790 insertions(+), 62 deletions(-)
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index aa7fa11a0197..e898adb331ba 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -386,12 +386,12 @@ config HID_EZKEY
Support for Ezkey BTC 8193 keyboard.
config HID_FT260
- tristate "FTDI FT260 USB HID to I2C host support"
- depends on USB_HID && HIDRAW && I2C
+ tristate "FTDI FT260 USB HID to I2C/UART host support"
+ depends on USB_HID && HIDRAW && I2C && TTY
help
- Provides I2C host adapter functionality over USB-HID through FT260
- device. The customizable USB descriptor fields are exposed as sysfs
- attributes.
+ Provides I2C host adapter and UART/TTY functionality over USB-HID
+ through the FT260 device. The customizable USB descriptor fields
+ are exposed as sysfs attributes.
To compile this driver as a module, choose M here: the module
will be called hid-ft260.
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 64c9c940db46..3faedfc5c413 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -13,6 +13,16 @@
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/usb.h>
+#include <linux/serial.h>
+#include <linux/serial_core.h>
+#include <linux/kfifo.h>
+#include <linux/tty_flip.h>
+#include <linux/minmax.h>
+#include <linux/unaligned.h>
+
+#define UART_COUNT_MAX 4 /* Number of UARTs this driver can handle */
+#define FIFO_SIZE 256
+#define TTY_WAKEUP_WATERMARK (FIFO_SIZE / 2)
#ifdef DEBUG
static int ft260_debug = 1;
@@ -30,6 +40,7 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define FT260_REPORT_MAX_LENGTH (64)
#define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
+#define FT260_UART_DATA_REPORT_ID(len) (FT260_UART_REPORT_MIN + (len - 1) / 4)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
@@ -43,7 +54,7 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
* or optoe limit the i2c reads to 128 bytes. To not block other drivers out
* of I2C for potentially troublesome amounts of time, we select the maximum
* read payload length to be 180 bytes.
-*/
+ */
#define FT260_RD_DATA_MAX (180)
#define FT260_WR_DATA_MAX (60)
@@ -81,7 +92,8 @@ enum {
FT260_UART_INTERRUPT_STATUS = 0xB1,
FT260_UART_STATUS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
- FT260_UART_REPORT = 0xF0,
+ FT260_UART_REPORT_MIN = 0xF0,
+ FT260_UART_REPORT_MAX = 0xFE,
};
/* Feature Out */
@@ -132,6 +144,13 @@ enum {
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
+/* Return values for ft260_get_interface_type func */
+enum {
+ FT260_IFACE_NONE,
+ FT260_IFACE_I2C,
+ FT260_IFACE_UART
+};
+
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
/* Feature In reports */
@@ -220,12 +239,59 @@ struct ft260_i2c_read_request_report {
__le16 length; /* data payload length */
} __packed;
-struct ft260_i2c_input_report {
- u8 report; /* FT260_I2C_REPORT */
+struct ft260_input_report {
+ u8 report; /* FT260_I2C_REPORT or FT260_UART_REPORT */
u8 length; /* data payload length */
u8 data[2]; /* data payload */
} __packed;
+/* UART reports */
+struct ft260_uart_write_request_report {
+ u8 report; /* FT260_UART_REPORT */
+ u8 length; /* data payload length */
+ u8 data[] __counted_by(length); /* variable data payload */
+} __packed;
+
+struct ft260_configure_uart_request {
+ u8 report; /* FT260_SYSTEM_SETTINGS */
+ u8 request; /* FT260_SET_UART_CONFIG */
+ u8 flow_ctrl; /* 0: OFF, 1: RTS_CTS, 2: DTR_DSR */
+ /* 3: XON_XOFF, 4: No flow ctrl */
+ /* The baudrate field is unaligned: */
+ __le32 baudrate; /* little endian, 9600 = 0x2580, 19200 = 0x4B00 */
+ u8 data_bit; /* 7 or 8 */
+ u8 parity; /* 0: no parity, 1: odd, 2: even, 3: high, 4: low */
+ u8 stop_bit; /* 0: one stop bit, 2: 2 stop bits */
+ u8 breaking; /* 0: no break */
+} __packed;
+
+/* UART interface configuration */
+enum {
+ FT260_CFG_FLOW_CTRL_OFF = 0x00,
+ FT260_CFG_FLOW_CTRL_RTS_CTS = 0x01,
+ FT260_CFG_FLOW_CTRL_DTR_DSR = 0x02,
+ FT260_CFG_FLOW_CTRL_XON_XOFF = 0x03,
+ FT260_CFG_FLOW_CTRL_NONE = 0x04,
+
+ FT260_CFG_DATA_BITS_7 = 0x07,
+ FT260_CFG_DATA_BITS_8 = 0x08,
+
+ FT260_CFG_PAR_NO = 0x00,
+ FT260_CFG_PAR_ODD = 0x01,
+ FT260_CFG_PAR_EVEN = 0x02,
+ FT260_CFG_PAR_HIGH = 0x03,
+ FT260_CFG_PAR_LOW = 0x04,
+
+ FT260_CFG_STOP_ONE_BIT = 0x00,
+ FT260_CFG_STOP_TWO_BIT = 0x02,
+
+ FT260_CFG_BREAKING_NO = 0x00,
+ FT260_CFG_BEAKING_YES = 0x01,
+
+ FT260_CFG_BAUD_MIN = 1200,
+ FT260_CFG_BAUD_MAX = 12000000,
+};
+
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
@@ -236,6 +302,23 @@ MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
+
+ bool ft260_is_serial;
+ struct list_head device_list;
+
+ /* tty_port lifetime is equal to device lifetime */
+ struct tty_port port;
+ unsigned int index;
+ struct kfifo xmit_fifo;
+ /* write_lock: lock to serialize access to xmit fifo */
+ spinlock_t write_lock;
+ struct uart_icount icount;
+
+ struct timer_list wakeup_timer;
+ struct work_struct wakeup_work;
+ bool reschedule_work;
+
+
struct completion wait;
struct mutex lock;
u8 write_buf[FT260_REPORT_MAX_LENGTH];
@@ -379,7 +462,7 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
- hid_err(hdev, "%s: failed to start transfer, ret %d\n",
+ hid_dbg(hdev, "%s: failed to start transfer, ret %d\n",
__func__, ret);
ft260_i2c_reset(hdev);
return ret;
@@ -609,7 +692,7 @@ static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
else
read_off = *msgs[0].buf;
- pr_info("%s: off %#x rlen %d wlen %d\n", __func__,
+ ft260_dbg("%s: off %#x rlen %d wlen %d\n", __func__,
read_off, rd_len, wr_len);
}
@@ -826,7 +909,7 @@ static int ft260_get_system_config(struct hid_device *hdev,
return 0;
}
-static int ft260_is_interface_enabled(struct hid_device *hdev)
+static int ft260_get_interface_type(struct hid_device *hdev, struct ft260_device *dev)
{
struct ft260_get_system_status_report cfg;
struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
@@ -843,21 +926,27 @@ static int ft260_is_interface_enabled(struct hid_device *hdev)
ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
+ dev->ft260_is_serial = false;
+
switch (cfg.chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
- if (interface == 1)
- hid_info(hdev, "uart interface is not supported\n");
- else
- ret = 1;
+ if (interface == 1) {
+ ret = FT260_IFACE_UART;
+ dev->ft260_is_serial = true;
+ } else {
+ ret = FT260_IFACE_I2C;
+ }
break;
case FT260_MODE_UART:
- hid_info(hdev, "uart interface is not supported\n");
+ ret = FT260_IFACE_UART;
+ dev->ft260_is_serial = true;
break;
case FT260_MODE_I2C:
- ret = 1;
+ ret = FT260_IFACE_I2C;
break;
}
+
return ret;
}
@@ -1001,6 +1090,583 @@ static const struct attribute_group ft260_attr_group = {
}
};
+/***
+ * START Serial dev part
+ */
+static DEFINE_MUTEX(ft260_uart_list_lock);
+static LIST_HEAD(ft260_uart_device_list);
+
+static struct ft260_device *ft260_dev_by_index(int index)
+{
+ struct ft260_device *port;
+
+ list_for_each_entry(port, &ft260_uart_device_list, device_list) {
+ if (index == port->index)
+ return port;
+ }
+ return NULL;
+}
+
+static int ft260_uart_add_port(struct ft260_device *port)
+{
+ int index = 0, ret = 0;
+ struct ft260_device *dev;
+
+ spin_lock_init(&port->write_lock);
+ if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
+ return -ENOMEM;
+
+ mutex_lock(&ft260_uart_list_lock);
+ list_for_each_entry(dev, &ft260_uart_device_list, device_list) {
+ if (dev->index != index)
+ break;
+ index++;
+ }
+
+ port->index = index;
+ list_add(&port->device_list, &ft260_uart_device_list);
+ mutex_unlock(&ft260_uart_list_lock);
+
+ return ret;
+}
+
+static void ft260_uart_port_put(struct ft260_device *port)
+{
+ tty_port_put(&port->port);
+}
+
+static void ft260_uart_port_remove(struct ft260_device *port)
+{
+ timer_delete_sync(&port->wakeup_timer);
+
+ mutex_lock(&ft260_uart_list_lock);
+ list_del(&port->device_list);
+ mutex_unlock(&ft260_uart_list_lock);
+
+ spin_lock(&port->write_lock);
+ kfifo_free(&port->xmit_fifo);
+ spin_unlock(&port->write_lock);
+
+ mutex_lock(&port->port.mutex);
+ port->reschedule_work = false;
+ tty_port_tty_hangup(&port->port, false);
+ mutex_unlock(&port->port.mutex);
+
+ ft260_uart_port_put(port);
+}
+
+static struct ft260_device *ft260_uart_port_get(unsigned int index)
+{
+ struct ft260_device *port;
+
+ if (index >= UART_COUNT_MAX)
+ return NULL;
+
+ mutex_lock(&ft260_uart_list_lock);
+ port = ft260_dev_by_index(index);
+ if (port)
+ tty_port_get(&port->port);
+ mutex_unlock(&ft260_uart_list_lock);
+
+ return port;
+}
+
+static int ft260_uart_open(struct tty_struct *tty, struct file *filp)
+{
+ int ret;
+ struct ft260_device *port = tty->driver_data;
+
+ ret = tty_port_open(&port->port, tty, filp);
+
+ return ret;
+}
+
+static void ft260_uart_close(struct tty_struct *tty, struct file *filp)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ tty_port_close(&port->port, tty, filp);
+}
+
+static void ft260_uart_hangup(struct tty_struct *tty)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ tty_port_hangup(&port->port);
+}
+
+static int ft260_uart_transmit_chars(struct ft260_device *port)
+{
+ struct hid_device *hdev = port->hdev;
+ struct kfifo *xmit = &port->xmit_fifo;
+ struct tty_struct *tty;
+ struct ft260_uart_write_request_report *rep;
+ int len, data_len, ret = 0;
+
+ tty = tty_port_tty_get(&port->port);
+
+ data_len = kfifo_len(xmit);
+ if (!tty || !data_len) {
+ ret = -EINVAL;
+ goto tty_out;
+ }
+
+ rep = (struct ft260_uart_write_request_report *)port->write_buf;
+
+ do {
+ len = min(data_len, FT260_WR_DATA_MAX);
+
+ rep->report = FT260_UART_DATA_REPORT_ID(len);
+ rep->length = len;
+
+ len = kfifo_out_locked(xmit, rep->data, len, &port->write_lock);
+
+ ret = ft260_hid_output_report(hdev, (u8 *)rep, len + sizeof(*rep));
+ if (ret < 0) {
+ hid_err(hdev, "Failed to start transfer, ret %d\n", ret);
+ goto tty_out;
+ }
+
+ data_len -= len;
+ port->icount.tx += len;
+ } while (data_len > 0);
+
+ len = kfifo_len(xmit);
+ if ((FIFO_SIZE - len) > TTY_WAKEUP_WATERMARK)
+ tty_wakeup(tty);
+
+ ret = 0;
+
+tty_out:
+ tty_kref_put(tty);
+ return ret;
+}
+
+static int ft260_uart_receive_chars(struct ft260_device *port,
+ u8 *data, u8 length)
+{
+ struct hid_device *hdev = port->hdev;
+ int ret = 0;
+
+ if (length > FT260_RD_DATA_MAX) {
+ hid_err(hdev, "Received too much data (%d)\n", length);
+ return -EBADR;
+ }
+
+ ret = tty_insert_flip_string(&port->port, data, length);
+ if (ret != length)
+ hid_err(hdev, "%d char not inserted to flip buffer\n", length - ret);
+ port->icount.rx += ret;
+
+ if (ret)
+ tty_flip_buffer_push(&port->port);
+
+ return ret;
+}
+
+static ssize_t ft260_uart_write(struct tty_struct *tty, const u8 *buf,
+ size_t count)
+{
+ struct ft260_device *port = tty->driver_data;
+ struct hid_device *hdev = port->hdev;
+ int len, ret;
+
+ len = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
+ ft260_dbg("count: %zu, len: %d", count, len);
+
+ ret = ft260_uart_transmit_chars(port);
+ if (ret < 0) {
+ hid_dbg(hdev, "Failed to transmit chars: %d\n", ret);
+ return 0;
+ }
+
+ ret = kfifo_len(&port->xmit_fifo);
+ if (ret > 0) {
+ hid_dbg(hdev, "Failed to all kfifo data bytes\n");
+ ft260_dbg("return: %d", len - ret);
+ return len - ret;
+ }
+
+ return len;
+}
+
+static unsigned int ft260_uart_write_room(struct tty_struct *tty)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
+}
+
+static unsigned int ft260_uart_chars_in_buffer(struct tty_struct *tty)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ return kfifo_len(&port->xmit_fifo);
+}
+
+static int ft260_uart_change_speed(struct ft260_device *port,
+ struct ktermios *termios,
+ struct ktermios *old)
+{
+ struct hid_device *hdev = port->hdev;
+ unsigned int baud;
+ struct ft260_configure_uart_request req;
+ int ret;
+
+ memset(&req, 0, sizeof(req));
+
+ req.report = FT260_SYSTEM_SETTINGS;
+ req.request = FT260_SET_UART_CONFIG;
+
+ switch (termios->c_cflag & CSIZE) {
+ case CS7:
+ req.data_bit = FT260_CFG_DATA_BITS_7;
+ break;
+ case CS5:
+ case CS6:
+ hid_err(hdev, "Invalid data bit size, setting to default (8 bit)\n");
+ req.data_bit = FT260_CFG_DATA_BITS_8;
+ termios->c_cflag &= ~CSIZE;
+ termios->c_cflag |= CS8;
+ break;
+ default:
+ case CS8:
+ req.data_bit = FT260_CFG_DATA_BITS_8;
+ break;
+ }
+
+ req.stop_bit = (termios->c_cflag & CSTOPB) ?
+ FT260_CFG_STOP_TWO_BIT : FT260_CFG_STOP_ONE_BIT;
+
+ if (termios->c_cflag & PARENB) {
+ req.parity = (termios->c_cflag & PARODD) ?
+ FT260_CFG_PAR_ODD : FT260_CFG_PAR_EVEN;
+ } else {
+ req.parity = FT260_CFG_PAR_NO;
+ }
+
+ baud = tty_termios_baud_rate(termios);
+ if (baud == 0 || baud < FT260_CFG_BAUD_MIN || baud > FT260_CFG_BAUD_MAX) {
+ struct tty_struct *tty = tty_port_tty_get(&port->port);
+
+ hid_err(hdev, "Invalid baud rate %d\n", baud);
+ baud = 9600;
+ tty_encode_baud_rate(tty, baud, baud);
+ tty_kref_put(tty);
+ }
+ put_unaligned_le32(cpu_to_le32(baud), &req.baudrate);
+
+ if (termios->c_cflag & CRTSCTS)
+ req.flow_ctrl = FT260_CFG_FLOW_CTRL_RTS_CTS;
+ else
+ req.flow_ctrl = FT260_CFG_FLOW_CTRL_OFF;
+
+ ft260_dbg("Configured termios: flow control: %d, baudrate: %d, ",
+ req.flow_ctrl, baud);
+ ft260_dbg("data_bit: %d, parity: %d, stop_bit: %d, breaking: %d\n",
+ req.data_bit, req.parity,
+ req.stop_bit, req.breaking);
+
+ req.flow_ctrl = FT260_CFG_FLOW_CTRL_NONE;
+ req.breaking = FT260_CFG_BREAKING_NO;
+
+ ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
+ if (ret < 0)
+ hid_err(hdev, "ft260_hid_feature_report_set failed: %d\n", ret);
+
+ return ret;
+}
+
+static int ft260_uart_get_icount(struct tty_struct *tty,
+ struct serial_icounter_struct *icount)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ memcpy(icount, &port->icount, sizeof(struct uart_icount));
+
+ return 0;
+}
+
+static void ft260_uart_set_termios(struct tty_struct *tty,
+ const struct ktermios *old_termios)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ ft260_uart_change_speed(port, &tty->termios, NULL);
+}
+
+static int ft260_uart_install(struct tty_driver *driver, struct tty_struct *tty)
+{
+ int idx = tty->index;
+ struct ft260_device *port = ft260_uart_port_get(idx);
+ int ret = tty_standard_install(driver, tty);
+
+ if (ret == 0)
+ /* This is the ref ft260_uart_port get provided */
+ tty->driver_data = port;
+ else
+ ft260_uart_port_put(port);
+
+ return ret;
+}
+
+static void ft260_uart_cleanup(struct tty_struct *tty)
+{
+ struct ft260_device *port = tty->driver_data;
+
+ tty->driver_data = NULL; /* Bug trap */
+ ft260_uart_port_put(port);
+}
+
+static int ft260_uart_proc_show(struct seq_file *m, void *v)
+{
+ int i;
+
+ seq_printf(m, "ft260 info:1.0 driver%s%s revision:%s\n",
+ "", "", "");
+ for (i = 0; i < UART_COUNT_MAX; i++) {
+ struct ft260_device *port = ft260_uart_port_get(i);
+
+ if (port) {
+ seq_printf(m, "%d: uart:FT260", i);
+ if (capable(CAP_SYS_ADMIN)) {
+ seq_printf(m, " tx:%d rx:%d",
+ port->icount.tx, port->icount.rx);
+ if (port->icount.frame)
+ seq_printf(m, " fe:%d",
+ port->icount.frame);
+ if (port->icount.parity)
+ seq_printf(m, " pe:%d",
+ port->icount.parity);
+ if (port->icount.brk)
+ seq_printf(m, " brk:%d",
+ port->icount.brk);
+ if (port->icount.overrun)
+ seq_printf(m, " oe:%d",
+ port->icount.overrun);
+ if (port->icount.cts)
+ seq_printf(m, " cts:%d",
+ port->icount.cts);
+ if (port->icount.dsr)
+ seq_printf(m, " dsr:%d",
+ port->icount.dsr);
+ if (port->icount.rng)
+ seq_printf(m, " rng:%d",
+ port->icount.rng);
+ if (port->icount.dcd)
+ seq_printf(m, " dcd:%d",
+ port->icount.dcd);
+ }
+ ft260_uart_port_put(port);
+ seq_putc(m, '\n');
+ }
+ }
+ return 0;
+}
+
+static const struct tty_operations ft260_uart_ops = {
+ .open = ft260_uart_open,
+ .close = ft260_uart_close,
+ .write = ft260_uart_write,
+ .write_room = ft260_uart_write_room,
+ .chars_in_buffer = ft260_uart_chars_in_buffer,
+ .set_termios = ft260_uart_set_termios,
+ .hangup = ft260_uart_hangup,
+ .install = ft260_uart_install,
+ .cleanup = ft260_uart_cleanup,
+ .proc_show = ft260_uart_proc_show,
+ .get_icount = ft260_uart_get_icount,
+};
+
+/* The FT260 has a "power saving mode" that causes the device to switch
+ * to a 30 kHz oscillator if there's no activity for 5 seconds.
+ * Unfortunately this mode can only be disabled by reprogramming
+ * internal fuses, which requires an additional programming voltage.
+ *
+ * One effect of this mode is to cause data loss on a fast UART that
+ * transmits after being idle for longer than 5 seconds. We work around
+ * this by sending a dummy report at least once per 4 seconds if the
+ * UART is in use.
+ */
+static void ft260_uart_start_wakeup(struct timer_list *t)
+{
+ struct ft260_device *dev =
+ container_of(t, struct ft260_device, wakeup_timer);
+
+ if (dev->reschedule_work) {
+ schedule_work(&dev->wakeup_work);
+ mod_timer(&dev->wakeup_timer, jiffies +
+ msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS));
+ }
+}
+
+static void ft260_uart_do_wakeup(struct work_struct *work)
+{
+ struct ft260_device *dev =
+ container_of(work, struct ft260_device, wakeup_work);
+ struct ft260_get_chip_version_report version;
+ int ret;
+
+ if (dev->reschedule_work) {
+ ret = ft260_hid_feature_report_get(dev->hdev, FT260_CHIP_VERSION,
+ (u8 *)&version, sizeof(version));
+ if (ret < 0)
+ hid_err(dev->hdev,
+ "%s: failed to start transfer, ret %d\n",
+ __func__, ret);
+ }
+}
+
+static void ft260_uart_shutdown(struct tty_port *tport)
+{
+ struct ft260_device *port =
+ container_of(tport, struct ft260_device, port);
+
+ port->reschedule_work = false;
+}
+
+static int ft260_uart_activate(struct tty_port *tport, struct tty_struct *tty)
+{
+ struct ft260_device *port =
+ container_of(tport, struct ft260_device, port);
+
+ /*
+ * Set the TTY IO error marker - we will only clear this
+ * once we have successfully opened the port.
+ */
+ set_bit(TTY_IO_ERROR, &tty->flags);
+
+ spin_lock(&port->write_lock);
+ kfifo_reset(&port->xmit_fifo);
+ spin_unlock(&port->write_lock);
+
+ ft260_uart_change_speed(port, &tty->termios, NULL);
+ clear_bit(TTY_IO_ERROR, &tty->flags);
+
+ if (port->reschedule_work) {
+ mod_timer(&port->wakeup_timer, jiffies +
+ msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS));
+ }
+
+ return 0;
+}
+
+static void ft260_uart_port_destroy(struct tty_port *tport)
+{
+ struct ft260_device *port =
+ container_of(tport, struct ft260_device, port);
+
+ kfree(port);
+}
+
+static const struct tty_port_operations ft260_uart_port_ops = {
+ .shutdown = ft260_uart_shutdown,
+ .activate = ft260_uart_activate,
+ .destruct = ft260_uart_port_destroy,
+};
+
+static struct tty_driver *ft260_tty_driver;
+
+static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
+{
+ int ret;
+
+ hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
+ hdev->version >> 8, hdev->version & 0xff, hdev->name,
+ hdev->phys);
+
+ hid_set_drvdata(hdev, dev);
+ dev->hdev = hdev;
+ dev->adap.owner = THIS_MODULE;
+ dev->adap.class = I2C_CLASS_HWMON;
+ dev->adap.algo = &ft260_i2c_algo;
+ dev->adap.quirks = &ft260_i2c_quirks;
+ dev->adap.dev.parent = &hdev->dev;
+ snprintf(dev->adap.name, sizeof(dev->adap.name),
+ "FT260 usb-i2c bridge");
+
+ ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
+ if (ret)
+ ft260_i2c_reset(hdev);
+
+ i2c_set_adapdata(&dev->adap, dev);
+ ret = i2c_add_adapter(&dev->adap);
+ if (ret) {
+ hid_err(hdev, "failed to add i2c adapter\n");
+ return ret;
+ }
+
+ ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
+ if (ret < 0) {
+ hid_err(hdev, "failed to create sysfs attrs\n");
+ goto err_i2c_free;
+ }
+ return 0;
+
+err_i2c_free:
+ i2c_del_adapter(&dev->adap);
+ return ret;
+}
+
+static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
+{
+ struct ft260_configure_uart_request req;
+ int ret;
+ struct device *devt;
+
+ INIT_WORK(&dev->wakeup_work, ft260_uart_do_wakeup);
+ // FIXME: Do I need that if I have cancel_work_sync?
+ // FIXME: are all kfifo access secured by lock? with irq or not?
+ dev->reschedule_work = false;
+ /* Work not started at this point */
+ timer_setup(&dev->wakeup_timer, ft260_uart_start_wakeup, 0);
+
+ tty_port_init(&dev->port);
+ dev->port.ops = &ft260_uart_port_ops;
+
+ ret = ft260_uart_add_port(dev);
+ if (ret) {
+ hid_err(hdev, "failed to add port\n");
+ return ret;
+ }
+ devt = tty_port_register_device_attr(&dev->port,
+ ft260_tty_driver,
+ dev->index, &hdev->dev,
+ dev, NULL);
+ if (IS_ERR(devt)) {
+ hid_err(hdev, "failed to register tty port\n");
+ ret = PTR_ERR(devt);
+ goto err_register_tty;
+ }
+ hid_info(hdev, "Registering device /dev/%s%d\n",
+ ft260_tty_driver->name, dev->index);
+
+ /* Send Feature Report to Configure FT260 as UART 9600-8-N-1 */
+ req.report = FT260_SYSTEM_SETTINGS;
+ req.request = FT260_SET_UART_CONFIG;
+ req.flow_ctrl = FT260_CFG_FLOW_CTRL_NONE;
+ put_unaligned_le32(cpu_to_le32(9600), &req.baudrate);
+ req.data_bit = FT260_CFG_DATA_BITS_8;
+ req.parity = FT260_CFG_PAR_NO;
+ req.stop_bit = FT260_CFG_STOP_ONE_BIT;
+ req.breaking = FT260_CFG_BREAKING_NO;
+
+ ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
+ if (ret < 0) {
+ hid_err(hdev, "ft260_hid_feature_report_set failed: %d\n",
+ ret);
+ goto err_hid_report;
+ }
+
+ return 0;
+
+err_hid_report:
+ tty_port_unregister_device(&dev->port, ft260_tty_driver, dev->index);
+err_register_tty:
+ ft260_uart_port_remove(dev);
+ return ret;
+}
+
static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
@@ -1010,20 +1676,26 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
if (!hid_is_usb(hdev))
return -EINVAL;
- dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
- if (!dev)
- return -ENOMEM;
+ /* We cannot used devm_kzalloc here, because port has to survive until
+ * destroy function call
+ */
+ dev = kzalloc_obj(*dev, GFP_KERNEL);
+ if (!dev) {
+ ret = -ENOMEM;
+ goto alloc_fail;
+ }
+ hid_set_drvdata(hdev, dev);
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "failed to parse HID\n");
- return ret;
+ goto hid_fail;
}
ret = hid_hw_start(hdev, 0);
if (ret) {
hid_err(hdev, "failed to start HID HW\n");
- return ret;
+ goto hid_fail;
}
ret = hid_hw_open(hdev);
@@ -1043,53 +1715,36 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
version.chip_code[0], version.chip_code[1],
version.chip_code[2], version.chip_code[3]);
- ret = ft260_is_interface_enabled(hdev);
- if (ret <= 0)
+ ret = ft260_get_interface_type(hdev, dev);
+ if (ret <= FT260_IFACE_NONE)
goto err_hid_close;
- hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
- hdev->version >> 8, hdev->version & 0xff, hdev->name,
- hdev->phys);
-
hid_set_drvdata(hdev, dev);
dev->hdev = hdev;
- dev->adap.owner = THIS_MODULE;
- dev->adap.class = I2C_CLASS_HWMON;
- dev->adap.algo = &ft260_i2c_algo;
- dev->adap.quirks = &ft260_i2c_quirks;
- dev->adap.dev.parent = &hdev->dev;
- snprintf(dev->adap.name, sizeof(dev->adap.name),
- "FT260 usb-i2c bridge");
mutex_init(&dev->lock);
spin_lock_init(&dev->read_lock);
init_completion(&dev->wait);
- ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
- if (ret)
- ft260_i2c_reset(hdev);
-
- i2c_set_adapdata(&dev->adap, dev);
- ret = i2c_add_adapter(&dev->adap);
- if (ret) {
- hid_err(hdev, "failed to add i2c adapter\n");
- goto err_hid_close;
- }
-
- ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
- if (ret < 0) {
- hid_err(hdev, "failed to create sysfs attrs\n");
- goto err_i2c_free;
+ if (!dev->ft260_is_serial) {
+ ret = ft260_i2c_probe(hdev, dev);
+ if (ret)
+ goto err_hid_close;
+ } else {
+ ret = ft260_uart_probe(hdev, dev);
+ if (ret)
+ goto err_hid_close;
}
return 0;
-err_i2c_free:
- i2c_del_adapter(&dev->adap);
err_hid_close:
hid_hw_close(hdev);
err_hid_stop:
hid_hw_stop(hdev);
+hid_fail:
+ kfree(dev);
+alloc_fail:
return ret;
}
@@ -1100,8 +1755,18 @@ static void ft260_remove(struct hid_device *hdev)
if (!dev)
return;
- sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
- i2c_del_adapter(&dev->adap);
+ if (dev->ft260_is_serial) {
+ // FIXME:
+ cancel_work_sync(&dev->wakeup_work);
+ tty_port_unregister_device(&dev->port, ft260_tty_driver,
+ dev->index);
+ ft260_uart_port_remove(dev);
+ /* dev still needed, so we will free it in _destroy func */
+ } else {
+ sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
+ i2c_del_adapter(&dev->adap);
+ kfree(dev);
+ }
hid_hw_close(hdev);
hid_hw_stop(hdev);
@@ -1111,10 +1776,10 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int size)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
- struct ft260_i2c_input_report *xfer = (void *)data;
+ struct ft260_input_report *xfer = (void *)data;
unsigned long irqflags;
- if (size < offsetof(struct ft260_i2c_input_report, data)) {
+ if (size < offsetof(struct ft260_input_report, data)) {
hid_err(hdev, "short report %d\n", size);
return -1;
}
@@ -1127,7 +1792,7 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
xfer->report, xfer->length, size);
if (xfer->length > size -
- offsetof(struct ft260_i2c_input_report, data)) {
+ offsetof(struct ft260_input_report, data)) {
hid_err(hdev, "report %#02x: length %d exceeds HID report size\n",
xfer->report, xfer->length);
return -1;
@@ -1157,9 +1822,17 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
if (complete_read)
complete(&dev->wait);
- } else {
- hid_err(hdev, "unhandled report %#02x\n", xfer->report);
+ return 0;
+
+ } else if (xfer->length > FT260_RD_DATA_MAX) {
+ hid_err(hdev, "received data too long (%d)\n", xfer->length);
+ return -EBADR;
+ } else if (xfer->report >= FT260_UART_REPORT_MIN &&
+ xfer->report <= FT260_UART_REPORT_MAX) {
+ return ft260_uart_receive_chars(dev, xfer->data, xfer->length);
}
+ hid_err(hdev, "unhandled report %#02x\n", xfer->report);
+
return 0;
}
@@ -1171,7 +1844,62 @@ static struct hid_driver ft260_driver = {
.raw_event = ft260_raw_event,
};
-module_hid_driver(ft260_driver);
-MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
+static int __init ft260_driver_init(void)
+{
+ int ret;
+
+ ft260_tty_driver = tty_alloc_driver(UART_COUNT_MAX,
+ TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
+ if (IS_ERR(ft260_tty_driver)) {
+ pr_err("tty_alloc_driver failed: %d\n",
+ (int)PTR_ERR(ft260_tty_driver));
+ return PTR_ERR(ft260_tty_driver);
+ }
+
+ ft260_tty_driver->driver_name = "ft260_ser";
+ ft260_tty_driver->name = "ttyFT";
+ ft260_tty_driver->major = 0;
+ ft260_tty_driver->minor_start = 0;
+ ft260_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
+ ft260_tty_driver->subtype = SERIAL_TYPE_NORMAL;
+ ft260_tty_driver->init_termios = tty_std_termios;
+ ft260_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
+ ft260_tty_driver->init_termios.c_ispeed = 9600;
+ ft260_tty_driver->init_termios.c_ospeed = 9600;
+ tty_set_operations(ft260_tty_driver, &ft260_uart_ops);
+
+ ret = tty_register_driver(ft260_tty_driver);
+ if (ret) {
+ pr_err("tty_register_driver failed: %d\n", ret);
+ goto err_reg_driver;
+ }
+
+ ret = hid_register_driver(&(ft260_driver));
+ if (ret) {
+ pr_err("hid_register_driver failed: %d\n", ret);
+ goto err_reg_hid;
+ }
+
+ return 0;
+
+err_reg_hid:
+ tty_unregister_driver(ft260_tty_driver);
+err_reg_driver:
+ tty_driver_kref_put(ft260_tty_driver);
+
+ return ret;
+}
+
+static void __exit ft260_driver_exit(void)
+{
+ hid_unregister_driver(&(ft260_driver));
+ tty_unregister_driver(ft260_tty_driver);
+ tty_driver_kref_put(ft260_tty_driver);
+}
+
+module_init(ft260_driver_init);
+module_exit(ft260_driver_exit);
+
+MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge and TTY driver");
MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
MODULE_LICENSE("GPL v2");
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 02/13] HID: ft260: uart: bring-up fixes
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 03/13] HID: ft260: add GPIO support on top of UART Michael Zaidman
` (11 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
Fix the UART path so the serial interface added previously works
reliably in practice. This squashes the contiguous UART bring-up
fixes that followed the initial serial-driver addition.
The UART and I2C paths shared write_buf without proper separation,
so concurrent use could corrupt transfers on both channels. Give
UART its own TX buffer. The write request report is now built in
that fixed per-port buffer, so its payload member becomes a fixed
FT260_WR_UART_DATA_MAX (62) array rather than a __counted_by()
flexible array, which describes a runtime-counted allocation this
path no longer makes.
The FT260 enters power-save after ~5s idle, which drops RX data at
higher baud rates. Keep a timed dummy wakeup, but only when needed:
skip it below 4800 baud and when EEPROM disables power-save; wake
earlier before RX; avoid configuring baud or disabling the workaround
twice; and arm the timer when a tty session starts.
The received report length was validated against the actual HID report
size only on the I2C path. The UART path instead bounded the
device-supplied length against FT260_RD_DATA_MAX (180), which is the
maximum I2C read payload, not a bound on the report buffer. As a UART
payload cannot exceed FT260_WR_UART_DATA_MAX (62) bytes, a report
claiming more would make ft260_uart_receive_chars() push data past the
end of the report buffer into the tty flip buffer. Validate the length
against the received report size once, before dispatching on the report
type, and drop the two now-redundant FT260_RD_DATA_MAX checks.
Also fix RX data loss after reopening the device, improve TX
throughput, simplify HID interface-type detection, use kfifo_avail
for write-room, and ignore spurious UART interrupt status (0xb1)
reports instead of treating them as errors.
Also includes UART cleanup and refactoring to match the rest of the
driver style (including replacing kfifo_*_locked aliases with
kfifo_*_spinlocked).
This replaces the UART bring-up fix series:
https://lore.kernel.org/all/20240210215147.77629-1-michael.zaidman@gmail.com/
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 340 ++++++++++++++++++++++------------------
1 file changed, 188 insertions(+), 152 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 3faedfc5c413..b6c408267a68 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
+ * FTDI FT260 USB HID to I2C/UART host bridge
*
* Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
*
@@ -20,10 +20,6 @@
#include <linux/minmax.h>
#include <linux/unaligned.h>
-#define UART_COUNT_MAX 4 /* Number of UARTs this driver can handle */
-#define FIFO_SIZE 256
-#define TTY_WAKEUP_WATERMARK (FIFO_SIZE / 2)
-
#ifdef DEBUG
static int ft260_debug = 1;
#else
@@ -38,9 +34,12 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
pr_info("%s: " format, __func__, ##arg); \
} while (0)
-#define FT260_REPORT_MAX_LENGTH (64)
-#define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
-#define FT260_UART_DATA_REPORT_ID(len) (FT260_UART_REPORT_MIN + (len - 1) / 4)
+#define FT260_REPORT_MAX_LEN (64)
+#define FT260_DATA_REPORT_ID(min, len) (min + (len - 1) / 4)
+#define FT260_I2C_DATA_REPORT_ID(len) \
+ FT260_DATA_REPORT_ID(FT260_I2C_REPORT_MIN, len)
+#define FT260_UART_DATA_REPORT_ID(len) \
+ FT260_DATA_REPORT_ID(FT260_UART_REPORT_MIN, len)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
@@ -56,7 +55,8 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
-#define FT260_WR_DATA_MAX (60)
+#define FT260_WR_I2C_DATA_MAX (60)
+#define FT260_WR_UART_DATA_MAX (62)
/*
* Device interface configuration.
@@ -90,7 +90,7 @@ enum {
FT260_I2C_REPORT_MAX = 0xDE,
FT260_GPIO = 0xB0,
FT260_UART_INTERRUPT_STATUS = 0xB1,
- FT260_UART_STATUS = 0xE0,
+ FT260_UART_SETTINGS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
FT260_UART_REPORT_MIN = 0xF0,
FT260_UART_REPORT_MAX = 0xFE,
@@ -144,7 +144,7 @@ enum {
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
-/* Return values for ft260_get_interface_type func */
+/* USB interface type values */
enum {
FT260_IFACE_NONE,
FT260_IFACE_I2C,
@@ -190,6 +190,18 @@ struct ft260_get_i2c_status_report {
u8 reserved;
} __packed;
+struct ft260_get_uart_settings_report {
+ u8 report; /* FT260_UART_SETTINGS */
+ u8 flow_ctrl; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
+ /* 3 - XON_XOFF, 4 - No flow control */
+ /* The baudrate field is unaligned */
+ __le32 baudrate; /* little endian, 9600 = 0x2580, 19200 = 0x4B00 */
+ u8 data_bit; /* 7 or 8 */
+ u8 parity; /* 0: no parity, 1: odd, 2: even, 3: high, 4: low */
+ u8 stop_bit; /* 0: one stop bit, 2: 2 stop bits */
+ u8 breaking; /* 0: no break */
+} __packed;
+
/* Feature Out reports */
struct ft260_set_system_clock_report {
@@ -229,7 +241,7 @@ struct ft260_i2c_write_request_report {
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
u8 length; /* data payload length */
- u8 data[FT260_WR_DATA_MAX]; /* data payload */
+ u8 data[FT260_WR_I2C_DATA_MAX]; /* data payload */
} __packed;
struct ft260_i2c_read_request_report {
@@ -246,18 +258,19 @@ struct ft260_input_report {
} __packed;
/* UART reports */
+
struct ft260_uart_write_request_report {
u8 report; /* FT260_UART_REPORT */
u8 length; /* data payload length */
- u8 data[] __counted_by(length); /* variable data payload */
+ u8 data[FT260_WR_UART_DATA_MAX]; /* data payload */
} __packed;
-struct ft260_configure_uart_request {
+struct ft260_configure_uart_request_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_CONFIG */
u8 flow_ctrl; /* 0: OFF, 1: RTS_CTS, 2: DTR_DSR */
/* 3: XON_XOFF, 4: No flow ctrl */
- /* The baudrate field is unaligned: */
+ /* The baudrate field is unaligned */
__le32 baudrate; /* little endian, 9600 = 0x2580, 19200 = 0x4B00 */
u8 data_bit; /* 7 or 8 */
u8 parity; /* 0: no parity, 1: odd, 2: even, 3: high, 4: low */
@@ -292,6 +305,11 @@ enum {
FT260_CFG_BAUD_MAX = 12000000,
};
+#define FT260_UART_EN_PW_SAVE_BAUD (4800)
+
+#define UART_COUNT_MAX (4) /* Number of supported UARTs */
+#define XMIT_FIFO_SIZE (PAGE_SIZE)
+
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
@@ -302,26 +320,22 @@ MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
-
- bool ft260_is_serial;
+ int iface_type;
struct list_head device_list;
-
- /* tty_port lifetime is equal to device lifetime */
struct tty_port port;
+ /* tty port index */
unsigned int index;
struct kfifo xmit_fifo;
- /* write_lock: lock to serialize access to xmit fifo */
- spinlock_t write_lock;
+ spinlock_t xmit_fifo_lock;
struct uart_icount icount;
-
struct timer_list wakeup_timer;
struct work_struct wakeup_work;
bool reschedule_work;
-
-
+ bool power_saving_en;
struct completion wait;
struct mutex lock;
- u8 write_buf[FT260_REPORT_MAX_LENGTH];
+ u8 i2c_wr_buf[FT260_REPORT_MAX_LEN];
+ u8 uart_wr_buf[FT260_REPORT_MAX_LEN];
unsigned long need_wakeup_at;
/* Protects read_buf, read_idx and read_len against ft260_raw_event() */
spinlock_t read_lock;
@@ -332,8 +346,7 @@ struct ft260_device {
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
- unsigned char report_id, u8 *data,
- size_t len)
+ u8 report_id, u8 *data, size_t len)
{
u8 *buf;
int ret;
@@ -462,8 +475,6 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
- hid_dbg(hdev, "%s: failed to start transfer, ret %d\n",
- __func__, ret);
ft260_i2c_reset(hdev);
return ret;
}
@@ -505,7 +516,7 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int ret, wr_len, idx = 0;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
- (struct ft260_i2c_write_request_report *)dev->write_buf;
+ (struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
if (len < 1)
return -EINVAL;
@@ -513,12 +524,12 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
rep->flag = FT260_FLAG_START;
do {
- if (len <= FT260_WR_DATA_MAX) {
+ if (len <= FT260_WR_I2C_DATA_MAX) {
wr_len = len;
if (flag == FT260_FLAG_START_STOP)
rep->flag |= FT260_FLAG_STOP;
} else {
- wr_len = FT260_WR_DATA_MAX;
+ wr_len = FT260_WR_I2C_DATA_MAX;
}
rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
@@ -554,7 +565,7 @@ static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
int len = 4;
struct ft260_i2c_write_request_report *rep =
- (struct ft260_i2c_write_request_report *)dev->write_buf;
+ (struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
if (data_len >= sizeof(rep->data))
return -EINVAL;
@@ -574,6 +585,8 @@ static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
rep->report, addr, cmd, rep->length, len);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
+ if (ret < 0)
+ hid_err(dev->hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
@@ -692,8 +705,7 @@ static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
else
read_off = *msgs[0].buf;
- ft260_dbg("%s: off %#x rlen %d wlen %d\n", __func__,
- read_off, rd_len, wr_len);
+ ft260_dbg("off %#x rlen %d wlen %d\n", read_off, rd_len, wr_len);
}
ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
@@ -926,27 +938,25 @@ static int ft260_get_interface_type(struct hid_device *hdev, struct ft260_device
ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
- dev->ft260_is_serial = false;
+ dev->power_saving_en = cfg.power_saving_en;
switch (cfg.chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
- if (interface == 1) {
+ if (interface == 1)
ret = FT260_IFACE_UART;
- dev->ft260_is_serial = true;
- } else {
+ else
ret = FT260_IFACE_I2C;
- }
break;
case FT260_MODE_UART:
ret = FT260_IFACE_UART;
- dev->ft260_is_serial = true;
break;
case FT260_MODE_I2C:
ret = FT260_IFACE_I2C;
break;
}
+ dev->iface_type = ret;
return ret;
}
@@ -1090,12 +1100,36 @@ static const struct attribute_group ft260_attr_group = {
}
};
-/***
- * START Serial dev part
- */
static DEFINE_MUTEX(ft260_uart_list_lock);
static LIST_HEAD(ft260_uart_device_list);
+static void ft260_uart_wakeup(struct ft260_device *dev);
+
+static int ft260_get_uart_settings(struct hid_device *hdev,
+ struct ft260_get_uart_settings_report *cfg)
+{
+ int ret;
+ int len = sizeof(struct ft260_get_uart_settings_report);
+
+ ret = ft260_hid_feature_report_get(hdev, FT260_UART_SETTINGS,
+ (u8 *)cfg, len);
+ if (ret < 0) {
+ hid_err(hdev, "failed to retrieve uart settings\n");
+ return ret;
+ }
+ return 0;
+}
+
+static void ft260_uart_wakeup_workaraund_enable(struct ft260_device *port,
+ bool enable)
+{
+ if (port->power_saving_en) {
+ port->reschedule_work = enable;
+ ft260_dbg("%s wakeup workaround",
+ enable ? "activate" : "deactivate");
+ }
+}
+
static struct ft260_device *ft260_dev_by_index(int index)
{
struct ft260_device *port;
@@ -1112,8 +1146,8 @@ static int ft260_uart_add_port(struct ft260_device *port)
int index = 0, ret = 0;
struct ft260_device *dev;
- spin_lock_init(&port->write_lock);
- if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
+ spin_lock_init(&port->xmit_fifo_lock);
+ if (kfifo_alloc(&port->xmit_fifo, XMIT_FIFO_SIZE, GFP_KERNEL))
return -ENOMEM;
mutex_lock(&ft260_uart_list_lock);
@@ -1143,19 +1177,18 @@ static void ft260_uart_port_remove(struct ft260_device *port)
list_del(&port->device_list);
mutex_unlock(&ft260_uart_list_lock);
- spin_lock(&port->write_lock);
+ spin_lock(&port->xmit_fifo_lock);
kfifo_free(&port->xmit_fifo);
- spin_unlock(&port->write_lock);
+ spin_unlock(&port->xmit_fifo_lock);
mutex_lock(&port->port.mutex);
- port->reschedule_work = false;
tty_port_tty_hangup(&port->port, false);
mutex_unlock(&port->port.mutex);
ft260_uart_port_put(port);
}
-static struct ft260_device *ft260_uart_port_get(unsigned int index)
+static struct ft260_device *ft260_uart_port_get(int index)
{
struct ft260_device *port;
@@ -1211,30 +1244,24 @@ static int ft260_uart_transmit_chars(struct ft260_device *port)
goto tty_out;
}
- rep = (struct ft260_uart_write_request_report *)port->write_buf;
+ rep = (struct ft260_uart_write_request_report *)port->uart_wr_buf;
do {
- len = min(data_len, FT260_WR_DATA_MAX);
+ len = min(data_len, FT260_WR_UART_DATA_MAX);
rep->report = FT260_UART_DATA_REPORT_ID(len);
rep->length = len;
- len = kfifo_out_locked(xmit, rep->data, len, &port->write_lock);
+ len = kfifo_out_spinlocked(xmit, rep->data, len, &port->xmit_fifo_lock);
- ret = ft260_hid_output_report(hdev, (u8 *)rep, len + sizeof(*rep));
- if (ret < 0) {
- hid_err(hdev, "Failed to start transfer, ret %d\n", ret);
+ ret = ft260_hid_output_report(hdev, (u8 *)rep, len + 2);
+ if (ret < 0)
goto tty_out;
- }
data_len -= len;
port->icount.tx += len;
} while (data_len > 0);
- len = kfifo_len(xmit);
- if ((FIFO_SIZE - len) > TTY_WAKEUP_WATERMARK)
- tty_wakeup(tty);
-
ret = 0;
tty_out:
@@ -1242,20 +1269,14 @@ static int ft260_uart_transmit_chars(struct ft260_device *port)
return ret;
}
-static int ft260_uart_receive_chars(struct ft260_device *port,
- u8 *data, u8 length)
+static int ft260_uart_receive_chars(struct ft260_device *port, u8 *data, u8 length)
{
- struct hid_device *hdev = port->hdev;
- int ret = 0;
-
- if (length > FT260_RD_DATA_MAX) {
- hid_err(hdev, "Received too much data (%d)\n", length);
- return -EBADR;
- }
+ int ret;
ret = tty_insert_flip_string(&port->port, data, length);
if (ret != length)
- hid_err(hdev, "%d char not inserted to flip buffer\n", length - ret);
+ ft260_dbg("%d char not inserted to flip buf\n", length - ret);
+
port->icount.rx += ret;
if (ret)
@@ -1265,26 +1286,26 @@ static int ft260_uart_receive_chars(struct ft260_device *port,
}
static ssize_t ft260_uart_write(struct tty_struct *tty, const u8 *buf,
- size_t count)
+ size_t cnt)
{
struct ft260_device *port = tty->driver_data;
- struct hid_device *hdev = port->hdev;
- int len, ret;
+ ssize_t len, ret, diff;
- len = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
- ft260_dbg("count: %zu, len: %d", count, len);
+ len = kfifo_in_spinlocked(&port->xmit_fifo, buf, cnt,
+ &port->xmit_fifo_lock);
+ ft260_dbg("count: %zu, len: %zd", cnt, len);
ret = ft260_uart_transmit_chars(port);
if (ret < 0) {
- hid_dbg(hdev, "Failed to transmit chars: %d\n", ret);
+ ft260_dbg("failed to transmit %zd\n", ret);
return 0;
}
ret = kfifo_len(&port->xmit_fifo);
if (ret > 0) {
- hid_dbg(hdev, "Failed to all kfifo data bytes\n");
- ft260_dbg("return: %d", len - ret);
- return len - ret;
+ diff = len - ret;
+ ft260_dbg("failed to send %zd out of %zd bytes\n", diff, len);
+ return diff;
}
return len;
@@ -1294,7 +1315,7 @@ static unsigned int ft260_uart_write_room(struct tty_struct *tty)
{
struct ft260_device *port = tty->driver_data;
- return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
+ return kfifo_avail(&port->xmit_fifo);
}
static unsigned int ft260_uart_chars_in_buffer(struct tty_struct *tty)
@@ -1310,7 +1331,8 @@ static int ft260_uart_change_speed(struct ft260_device *port,
{
struct hid_device *hdev = port->hdev;
unsigned int baud;
- struct ft260_configure_uart_request req;
+ struct ft260_configure_uart_request_report req;
+ bool wakeup_workaraund = false;
int ret;
memset(&req, 0, sizeof(req));
@@ -1324,7 +1346,7 @@ static int ft260_uart_change_speed(struct ft260_device *port,
break;
case CS5:
case CS6:
- hid_err(hdev, "Invalid data bit size, setting to default (8 bit)\n");
+ hid_err(hdev, "invalid data bit size, setting a default\n");
req.data_bit = FT260_CFG_DATA_BITS_8;
termios->c_cflag &= ~CSIZE;
termios->c_cflag |= CS8;
@@ -1349,11 +1371,17 @@ static int ft260_uart_change_speed(struct ft260_device *port,
if (baud == 0 || baud < FT260_CFG_BAUD_MIN || baud > FT260_CFG_BAUD_MAX) {
struct tty_struct *tty = tty_port_tty_get(&port->port);
- hid_err(hdev, "Invalid baud rate %d\n", baud);
+ hid_err(hdev, "invalid baud rate %d\n", baud);
baud = 9600;
tty_encode_baud_rate(tty, baud, baud);
tty_kref_put(tty);
}
+
+ if (baud > FT260_UART_EN_PW_SAVE_BAUD)
+ wakeup_workaraund = true;
+
+ ft260_uart_wakeup_workaraund_enable(port, wakeup_workaraund);
+
put_unaligned_le32(cpu_to_le32(baud), &req.baudrate);
if (termios->c_cflag & CRTSCTS)
@@ -1361,7 +1389,7 @@ static int ft260_uart_change_speed(struct ft260_device *port,
else
req.flow_ctrl = FT260_CFG_FLOW_CTRL_OFF;
- ft260_dbg("Configured termios: flow control: %d, baudrate: %d, ",
+ ft260_dbg("configured termios: flow control: %d, baudrate: %d, ",
req.flow_ctrl, baud);
ft260_dbg("data_bit: %d, parity: %d, stop_bit: %d, breaking: %d\n",
req.data_bit, req.parity,
@@ -1372,7 +1400,7 @@ static int ft260_uart_change_speed(struct ft260_device *port,
ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
if (ret < 0)
- hid_err(hdev, "ft260_hid_feature_report_set failed: %d\n", ret);
+ hid_err(hdev, "failed to change termios: %d\n", ret);
return ret;
}
@@ -1422,8 +1450,8 @@ static int ft260_uart_proc_show(struct seq_file *m, void *v)
{
int i;
- seq_printf(m, "ft260 info:1.0 driver%s%s revision:%s\n",
- "", "", "");
+ seq_printf(m, "ft260 info:1.0 driver%s%s revision:%s\n", "", "", "");
+
for (i = 0; i < UART_COUNT_MAX; i++) {
struct ft260_device *port = ft260_uart_port_get(i);
@@ -1478,15 +1506,16 @@ static const struct tty_operations ft260_uart_ops = {
.get_icount = ft260_uart_get_icount,
};
-/* The FT260 has a "power saving mode" that causes the device to switch
+/*
+ * The FT260 has a "power saving mode" that causes the device to switch
* to a 30 kHz oscillator if there's no activity for 5 seconds.
- * Unfortunately this mode can only be disabled by reprogramming
+ * Unfortunately, this mode can only be disabled by reprogramming
* internal fuses, which requires an additional programming voltage.
*
- * One effect of this mode is to cause data loss on a fast UART that
- * transmits after being idle for longer than 5 seconds. We work around
- * this by sending a dummy report at least once per 4 seconds if the
- * UART is in use.
+ * One effect of this mode is to cause data loss on an Rx line at baud
+ * rates higher than 4800 after being idle for longer than 5 seconds.
+ * We work around this by sending a dummy report at least once per 4.8
+ * seconds if the UART is in use.
*/
static void ft260_uart_start_wakeup(struct timer_list *t)
{
@@ -1500,53 +1529,68 @@ static void ft260_uart_start_wakeup(struct timer_list *t)
}
}
-static void ft260_uart_do_wakeup(struct work_struct *work)
+static void ft260_uart_wakeup(struct ft260_device *dev)
{
- struct ft260_device *dev =
- container_of(work, struct ft260_device, wakeup_work);
- struct ft260_get_chip_version_report version;
+ struct ft260_get_chip_version_report ver;
int ret;
if (dev->reschedule_work) {
ret = ft260_hid_feature_report_get(dev->hdev, FT260_CHIP_VERSION,
- (u8 *)&version, sizeof(version));
+ (u8 *)&ver, sizeof(ver));
if (ret < 0)
- hid_err(dev->hdev,
- "%s: failed to start transfer, ret %d\n",
- __func__, ret);
+ hid_err(dev->hdev, "%s: failed with %d\n", __func__, ret);
}
}
-static void ft260_uart_shutdown(struct tty_port *tport)
+static void ft260_uart_do_wakeup(struct work_struct *work)
+{
+ struct ft260_device *dev =
+ container_of(work, struct ft260_device, wakeup_work);
+
+ ft260_uart_wakeup(dev);
+}
+
+static void ft260_uart_port_shutdown(struct tty_port *tport)
{
struct ft260_device *port =
container_of(tport, struct ft260_device, port);
- port->reschedule_work = false;
+ ft260_uart_wakeup_workaraund_enable(port, false);
}
-static int ft260_uart_activate(struct tty_port *tport, struct tty_struct *tty)
+static int ft260_uart_port_activate(struct tty_port *tport, struct tty_struct *tty)
{
- struct ft260_device *port =
- container_of(tport, struct ft260_device, port);
+ int ret;
+ int baudrate;
+ struct ft260_get_uart_settings_report cfg;
+ struct ft260_device *port = container_of(tport, struct ft260_device, port);
- /*
- * Set the TTY IO error marker - we will only clear this
- * once we have successfully opened the port.
- */
set_bit(TTY_IO_ERROR, &tty->flags);
- spin_lock(&port->write_lock);
+ spin_lock(&port->xmit_fifo_lock);
kfifo_reset(&port->xmit_fifo);
- spin_unlock(&port->write_lock);
+ spin_unlock(&port->xmit_fifo_lock);
- ft260_uart_change_speed(port, &tty->termios, NULL);
clear_bit(TTY_IO_ERROR, &tty->flags);
- if (port->reschedule_work) {
- mod_timer(&port->wakeup_timer, jiffies +
- msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS));
- }
+ /*
+ * The port setting may remain intact after session termination.
+ * Then, when reopening the port without configuring the port
+ * setting, we need to retrieve the baud rate from the device to
+ * reactivate the wakeup workaround if needed.
+ */
+ ret = ft260_get_uart_settings(port->hdev, &cfg);
+ if (ret)
+ return ret;
+
+ baudrate = get_unaligned_le32(&cfg.baudrate);
+ if (baudrate > FT260_UART_EN_PW_SAVE_BAUD)
+ ft260_uart_wakeup_workaraund_enable(port, true);
+
+ ft260_dbg("configured baudrate = %d", baudrate);
+
+ mod_timer(&port->wakeup_timer, jiffies +
+ msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS));
return 0;
}
@@ -1560,8 +1604,8 @@ static void ft260_uart_port_destroy(struct tty_port *tport)
}
static const struct tty_port_operations ft260_uart_port_ops = {
- .shutdown = ft260_uart_shutdown,
- .activate = ft260_uart_activate,
+ .shutdown = ft260_uart_port_shutdown,
+ .activate = ft260_uart_port_activate,
.destruct = ft260_uart_port_destroy,
};
@@ -1610,14 +1654,13 @@ static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
{
- struct ft260_configure_uart_request req;
+ struct ft260_configure_uart_request_report req;
int ret;
struct device *devt;
INIT_WORK(&dev->wakeup_work, ft260_uart_do_wakeup);
- // FIXME: Do I need that if I have cancel_work_sync?
// FIXME: are all kfifo access secured by lock? with irq or not?
- dev->reschedule_work = false;
+ ft260_uart_wakeup_workaraund_enable(dev, true);
/* Work not started at this point */
timer_setup(&dev->wakeup_timer, ft260_uart_start_wakeup, 0);
@@ -1638,10 +1681,10 @@ static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
ret = PTR_ERR(devt);
goto err_register_tty;
}
- hid_info(hdev, "Registering device /dev/%s%d\n",
+ hid_info(hdev, "registering device /dev/%s%d\n",
ft260_tty_driver->name, dev->index);
- /* Send Feature Report to Configure FT260 as UART 9600-8-N-1 */
+ /* Configure UART to 9600n8 */
req.report = FT260_SYSTEM_SETTINGS;
req.request = FT260_SET_UART_CONFIG;
req.flow_ctrl = FT260_CFG_FLOW_CTRL_NONE;
@@ -1653,8 +1696,7 @@ static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
if (ret < 0) {
- hid_err(hdev, "ft260_hid_feature_report_set failed: %d\n",
- ret);
+ hid_err(hdev, "failed to configure uart: %d\n", ret);
goto err_hid_report;
}
@@ -1675,9 +1717,9 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
if (!hid_is_usb(hdev))
return -EINVAL;
-
- /* We cannot used devm_kzalloc here, because port has to survive until
- * destroy function call
+ /*
+ * We cannot use devm_kzalloc here because the port has to survive
+ * until destroy function call.
*/
dev = kzalloc_obj(*dev, GFP_KERNEL);
if (!dev) {
@@ -1726,15 +1768,12 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
spin_lock_init(&dev->read_lock);
init_completion(&dev->wait);
- if (!dev->ft260_is_serial) {
+ if (ret == FT260_IFACE_I2C)
ret = ft260_i2c_probe(hdev, dev);
- if (ret)
- goto err_hid_close;
- } else {
+ else
ret = ft260_uart_probe(hdev, dev);
- if (ret)
- goto err_hid_close;
- }
+ if (ret)
+ goto err_hid_close;
return 0;
@@ -1755,13 +1794,12 @@ static void ft260_remove(struct hid_device *hdev)
if (!dev)
return;
- if (dev->ft260_is_serial) {
- // FIXME:
+ if (dev->iface_type == FT260_IFACE_UART) {
cancel_work_sync(&dev->wakeup_work);
tty_port_unregister_device(&dev->port, ft260_tty_driver,
dev->index);
ft260_uart_port_remove(dev);
- /* dev still needed, so we will free it in _destroy func */
+ /* dev is still needed, so we will free it in _destroy func */
} else {
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
@@ -1784,6 +1822,12 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
return -1;
}
+ if (xfer->length > size - offsetof(struct ft260_input_report, data)) {
+ hid_err(hdev, "report %#02x: length %d exceeds HID report size\n",
+ xfer->report, xfer->length);
+ return -1;
+ }
+
if (xfer->report >= FT260_I2C_REPORT_MIN &&
xfer->report <= FT260_I2C_REPORT_MAX) {
bool complete_read;
@@ -1791,13 +1835,6 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
ft260_dbg("i2c resp: rep %#02x len %d size %d\n",
xfer->report, xfer->length, size);
- if (xfer->length > size -
- offsetof(struct ft260_input_report, data)) {
- hid_err(hdev, "report %#02x: length %d exceeds HID report size\n",
- xfer->report, xfer->length);
- return -1;
- }
-
/*
* Hold read_lock so a timed-out ft260_i2c_read() cannot
* clear read_buf between the NULL check and the memcpy.
@@ -1824,12 +1861,11 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
return 0;
- } else if (xfer->length > FT260_RD_DATA_MAX) {
- hid_err(hdev, "received data too long (%d)\n", xfer->length);
- return -EBADR;
} else if (xfer->report >= FT260_UART_REPORT_MIN &&
xfer->report <= FT260_UART_REPORT_MAX) {
return ft260_uart_receive_chars(dev, xfer->data, xfer->length);
+ } else if (xfer->report == FT260_UART_INTERRUPT_STATUS) {
+ return 0;
}
hid_err(hdev, "unhandled report %#02x\n", xfer->report);
@@ -1874,7 +1910,7 @@ static int __init ft260_driver_init(void)
goto err_reg_driver;
}
- ret = hid_register_driver(&(ft260_driver));
+ ret = hid_register_driver(&ft260_driver);
if (ret) {
pr_err("hid_register_driver failed: %d\n", ret);
goto err_reg_hid;
@@ -1892,7 +1928,7 @@ static int __init ft260_driver_init(void)
static void __exit ft260_driver_exit(void)
{
- hid_unregister_driver(&(ft260_driver));
+ hid_unregister_driver(&ft260_driver);
tty_unregister_driver(ft260_tty_driver);
tty_driver_kref_put(ft260_tty_driver);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 03/13] HID: ft260: add GPIO support on top of UART
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
2026-08-22 21:39 ` [PATCH 02/13] HID: ft260: uart: bring-up fixes Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 7:44 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 04/13] HID: ft260: i2c: reduce driver module loading time Michael Zaidman
` (10 subsequent siblings)
13 siblings, 1 reply; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
Add gpiochip support for the FT260 multifunctional pins, including
GPIO2/GPIOA/GPIOG function selection via sysfs, and enable the
available GPIOs according to the active UART mode.
This supersedes the earlier pre-UART GPIO series:
https://lore.kernel.org/lkml/20230211115752.26276-2-michael.zaidman@gmail.com/
The gpio_chip.set callback returns int to match the current gpiolib
API, based on the adaptation originally contributed by Rio Liu
<rio@r26.me>.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/Kconfig | 10 +-
drivers/hid/hid-ft260.c | 660 +++++++++++++++++++++++++++++++++++-----
2 files changed, 586 insertions(+), 84 deletions(-)
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index e898adb331ba..8e161c440346 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -386,12 +386,12 @@ config HID_EZKEY
Support for Ezkey BTC 8193 keyboard.
config HID_FT260
- tristate "FTDI FT260 USB HID to I2C/UART host support"
- depends on USB_HID && HIDRAW && I2C && TTY
+ tristate "FTDI FT260 USB HID to I2C/UART/GPIO host support"
+ depends on USB_HID && HIDRAW && I2C && TTY && GPIOLIB
help
- Provides I2C host adapter and UART/TTY functionality over USB-HID
- through the FT260 device. The customizable USB descriptor fields
- are exposed as sysfs attributes.
+ Provides I2C host adapter, UART/TTY, and GPIO functionality over
+ USB-HID through the FT260 device. The customizable USB descriptor
+ fields are exposed as sysfs attributes.
To compile this driver as a module, choose M here: the module
will be called hid-ft260.
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index b6c408267a68..cc3b8f5791de 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -19,6 +19,7 @@
#include <linux/tty_flip.h>
#include <linux/minmax.h>
#include <linux/unaligned.h>
+#include <linux/gpio/driver.h>
#ifdef DEBUG
static int ft260_debug = 1;
@@ -57,6 +58,11 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define FT260_RD_DATA_MAX (180)
#define FT260_WR_I2C_DATA_MAX (60)
#define FT260_WR_UART_DATA_MAX (62)
+#define FT260_GPIOCHIP "ft260_gpio"
+#define FT260_GPIO_MAX (6)
+#define FT260_GPIO_EX_MAX (8)
+#define FT260_GPIO_TOTAL (FT260_GPIO_MAX + FT260_GPIO_EX_MAX)
+#define FT260_GPIO_MASK (~(0xffff << FT260_GPIO_TOTAL))
/*
* Device interface configuration.
@@ -151,7 +157,69 @@ enum {
FT260_IFACE_UART
};
-#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
+/* Multi-function pin functions */
+enum {
+ FT260_MFPIN_GPIO = 0x00,
+ FT260_MFPIN_SUSPOUT = 0x01,
+ FT260_MFPIN_PWREN = 0x02,
+ FT260_MFPIN_TX_ACTIVE = 0x03,
+ FT260_MFPIN_TX_LED = 0x04,
+ FT260_MFPIN_RX_LED = 0x05,
+ FT260_MFPIN_BCD_DET = 0x06,
+};
+
+enum {
+ FT260_GPIO_VALUE = 0x00,
+ FT260_GPIO_DIRECTION = 0x01,
+ FT260_GPIO_DIR_INPUT = 0x00,
+ FT260_GPIO_DIR_OUTPUT = 0x01,
+};
+
+/* GPIO offsets */
+enum {
+ FT260_GPIO_0 = (1 << 0),
+ FT260_GPIO_1 = (1 << 1),
+ FT260_GPIO_2 = (1 << 2),
+ FT260_GPIO_3 = (1 << 3),
+ FT260_GPIO_4 = (1 << 4),
+ FT260_GPIO_5 = (1 << 5),
+ FT260_GPIO_A = (1 << (FT260_GPIO_MAX + 0)),
+ FT260_GPIO_B = (1 << (FT260_GPIO_MAX + 1)),
+ FT260_GPIO_C = (1 << (FT260_GPIO_MAX + 2)),
+ FT260_GPIO_D = (1 << (FT260_GPIO_MAX + 3)),
+ FT260_GPIO_E = (1 << (FT260_GPIO_MAX + 4)),
+ FT260_GPIO_F = (1 << (FT260_GPIO_MAX + 5)),
+ FT260_GPIO_G = (1 << (FT260_GPIO_MAX + 6)),
+ FT260_GPIO_H = (1 << (FT260_GPIO_MAX + 7)),
+};
+
+/* GPIO groups */
+enum {
+ FT260_GPIO_WAKEUP = (FT260_GPIO_3),
+ FT260_GPIO_I2C_DEFAULT = (FT260_GPIO_0 | FT260_GPIO_1),
+ FT260_GPIO_UART_RX_TX = (FT260_GPIO_C | FT260_GPIO_D),
+ FT260_GPIO_UART_DCD_RI = (FT260_GPIO_4 | FT260_GPIO_5),
+ FT260_GPIO_UART_RTS_CTS = (FT260_GPIO_B | FT260_GPIO_E),
+ FT260_GPIO_UART_DTR_DSR = (FT260_GPIO_F | FT260_GPIO_H),
+ FT260_GPIO_UART_MODE_0_SET = (FT260_GPIO_UART_RX_TX |
+ FT260_GPIO_UART_DCD_RI |
+ FT260_GPIO_UART_RTS_CTS |
+ FT260_GPIO_UART_DTR_DSR),
+ FT260_GPIO_UART_MODE_1_SET = (FT260_GPIO_UART_DTR_DSR),
+ FT260_GPIO_UART_MODE_2_SET = (FT260_GPIO_UART_RTS_CTS),
+ FT260_GPIO_UART_MODE_3_SET = (FT260_GPIO_UART_RTS_CTS |
+ FT260_GPIO_UART_DTR_DSR),
+ FT260_GPIO_UART_MODE_4_SET = (FT260_GPIO_UART_MODE_3_SET),
+ FT260_GPIO_UART_DEFAULT = (FT260_GPIO_UART_MODE_0_SET),
+ FT260_GPIO_UART_MODE_1_CLR = (FT260_GPIO_UART_RX_TX |
+ FT260_GPIO_UART_RTS_CTS),
+ FT260_GPIO_UART_MODE_2_CLR = (FT260_GPIO_UART_RX_TX |
+ FT260_GPIO_UART_DTR_DSR),
+ FT260_GPIO_UART_MODE_3_CLR = (FT260_GPIO_UART_RX_TX),
+ FT260_GPIO_UART_MODES = (5),
+};
+
+#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | (report_id))
/* Feature In reports */
@@ -171,10 +239,10 @@ struct ft260_get_system_status_report {
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
- u8 gpio2_function; /* 0 - GPIO, 1 - SUSPOUT, */
+ u8 gpio2_func; /* 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN, 4 - TX_LED */
- u8 gpioA_function; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
- u8 gpioG_function; /* 0 - GPIO, 2 - PWREN, */
+ u8 gpioa_func; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
+ u8 gpiog_func; /* 0 - GPIO, 2 - PWREN, */
/* 5 - RX_LED, 6 - BCD_DET */
u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
@@ -202,6 +270,18 @@ struct ft260_get_uart_settings_report {
u8 breaking; /* 0: no break */
} __packed;
+struct ft260_gpio_state {
+ u8 vals; /* GPIO[0-5] values in bits 0 - 5 */
+ u8 dirs; /* GPIO[0-5] directions, 0 - in, 1 - out */
+ u8 ex_vals; /* GPIO[A-H] values in bits 0 - 7 */
+ u8 ex_dirs; /* GPIO[A-H] directions, 0 - in, 1 - out */
+} __packed;
+
+struct ft260_gpio_read_request_report {
+ u8 report; /* FT260_GPIO */
+ struct ft260_gpio_state gpio;
+} __packed;
+
/* Feature Out reports */
struct ft260_set_system_clock_report {
@@ -223,6 +303,12 @@ struct ft260_set_uart_mode_report {
/* 3 - XON_XOFF, 4 - No flow control */
} __packed;
+struct ft260_set_uart_dcd_ri_report {
+ u8 report; /* FT260_SYSTEM_SETTINGS */
+ u8 request; /* FT260_ENABLE_UART_DCD_RI */
+ u8 uart_dcd_ri; /* Pins func: 0 - GPIO4,GPIO5, 1 - DCD,RI */
+} __packed;
+
struct ft260_set_i2c_reset_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_RESET */
@@ -234,6 +320,33 @@ struct ft260_set_i2c_speed_report {
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
} __packed;
+struct ft260_set_gpio2_func_report {
+ u8 report; /* FT260_SYSTEM_SETTINGS */
+ u8 request; /* FT260_SELECT_GPIO2_FUNC */
+ u8 gpio2_func; /* Pin func: 0 - GPIO, 1 - SUSPOUT, */
+ /* 2 - PWREN# (active-low), 4 - TX_LED */
+} __packed;
+
+struct ft260_set_gpioa_func_report {
+ u8 report; /* FT260_SYSTEM_SETTINGS */
+ u8 request; /* FT260_SELECT_GPIOA_FUNC */
+ u8 gpioa_func; /* Pin func: 0 - GPIO, */
+ /* 3 - TX_ACTIVE, 4 - TX_LED */
+} __packed;
+
+struct ft260_set_gpiog_func_report {
+ u8 report; /* FT260_SYSTEM_SETTINGS */
+ u8 request; /* FT260_SELECT_GPIOG_FUNC */
+ u8 gpiog_func; /* Pin func: 0 - GPIO, */
+ /* 2 - PWREN# (active-low), */
+ /* 5 - RX_LED, 6 - BCD_DET */
+} __packed;
+
+struct ft260_gpio_write_request_report {
+ u8 report; /* FT260_GPIO */
+ struct ft260_gpio_state gpio;
+} __packed;
+
/* Data transfer reports */
struct ft260_i2c_write_request_report {
@@ -280,32 +393,32 @@ struct ft260_configure_uart_request_report {
/* UART interface configuration */
enum {
- FT260_CFG_FLOW_CTRL_OFF = 0x00,
- FT260_CFG_FLOW_CTRL_RTS_CTS = 0x01,
- FT260_CFG_FLOW_CTRL_DTR_DSR = 0x02,
- FT260_CFG_FLOW_CTRL_XON_XOFF = 0x03,
- FT260_CFG_FLOW_CTRL_NONE = 0x04,
+ FT260_UART_CFG_FLOW_CTRL_OFF = 0x00,
+ FT260_UART_CFG_FLOW_CTRL_RTS_CTS = 0x01,
+ FT260_UART_CFG_FLOW_CTRL_DTR_DSR = 0x02,
+ FT260_UART_CFG_FLOW_CTRL_XON_XOFF = 0x03,
+ FT260_UART_CFG_FLOW_CTRL_NONE = 0x04,
- FT260_CFG_DATA_BITS_7 = 0x07,
- FT260_CFG_DATA_BITS_8 = 0x08,
+ FT260_UART_CFG_DATA_BITS_7 = 0x07,
+ FT260_UART_CFG_DATA_BITS_8 = 0x08,
- FT260_CFG_PAR_NO = 0x00,
- FT260_CFG_PAR_ODD = 0x01,
- FT260_CFG_PAR_EVEN = 0x02,
- FT260_CFG_PAR_HIGH = 0x03,
- FT260_CFG_PAR_LOW = 0x04,
+ FT260_UART_CFG_PAR_NO = 0x00,
+ FT260_UART_CFG_PAR_ODD = 0x01,
+ FT260_UART_CFG_PAR_EVEN = 0x02,
+ FT260_UART_CFG_PAR_HIGH = 0x03,
+ FT260_UART_CFG_PAR_LOW = 0x04,
- FT260_CFG_STOP_ONE_BIT = 0x00,
- FT260_CFG_STOP_TWO_BIT = 0x02,
+ FT260_UART_CFG_STOP_ONE_BIT = 0x00,
+ FT260_UART_CFG_STOP_TWO_BIT = 0x02,
- FT260_CFG_BREAKING_NO = 0x00,
- FT260_CFG_BEAKING_YES = 0x01,
+ FT260_UART_CFG_BREAKING_NO = 0x00,
+ FT260_UART_CFG_BEAKING_YES = 0x01,
- FT260_CFG_BAUD_MIN = 1200,
- FT260_CFG_BAUD_MAX = 12000000,
+ FT260_UART_CFG_BAUD_MIN = 1200,
+ FT260_UART_CFG_BAUD_MAX = 12000000,
};
-#define FT260_UART_EN_PW_SAVE_BAUD (4800)
+#define FT260_UART_EN_PW_SAVE_BAUD (4800)
#define UART_COUNT_MAX (4) /* Number of supported UARTs */
#define XMIT_FIFO_SIZE (PAGE_SIZE)
@@ -321,6 +434,7 @@ struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
int iface_type;
+ int iface_id;
struct list_head device_list;
struct tty_port port;
/* tty port index */
@@ -343,6 +457,10 @@ struct ft260_device {
u16 read_idx;
u16 read_len;
u16 clock;
+ u16 gpio_en;
+ struct gpio_chip *gc;
+ struct ft260_gpio_state gpio;
+ u16 gpio_uart_mode[FT260_GPIO_UART_MODES];
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
@@ -375,8 +493,6 @@ static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
if (!buf)
return -ENOMEM;
- buf[0] = FT260_SYSTEM_SETTINGS;
-
ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
HID_REQ_SET_REPORT);
@@ -389,6 +505,7 @@ static int ft260_i2c_reset(struct hid_device *hdev)
struct ft260_set_i2c_reset_report report;
int ret;
+ report.report = FT260_SYSTEM_SETTINGS;
report.request = FT260_SET_I2C_RESET;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
@@ -906,6 +1023,322 @@ static const struct i2c_algorithm ft260_i2c_algo = {
.functionality = ft260_functionality,
};
+static void ft260_gpio_en_set(struct ft260_device *dev, u16 bitmap)
+{
+ dev->gpio_en |= bitmap & FT260_GPIO_MASK;
+}
+
+static void ft260_gpio_en_clr(struct ft260_device *dev, u16 bitmap)
+{
+ dev->gpio_en &= ~bitmap & FT260_GPIO_MASK;
+}
+
+static void ft260_gpio_en_update(struct hid_device *hdev, u8 req, u8 value)
+{
+ u16 bitmap;
+ struct ft260_device *dev = hid_get_drvdata(hdev);
+
+ switch (req) {
+
+ case FT260_SET_I2C_MODE:
+ bitmap = FT260_GPIO_I2C_DEFAULT;
+ break;
+ case FT260_SET_UART_MODE:
+ switch (value) {
+ case FT260_UART_CFG_FLOW_CTRL_OFF:
+ bitmap = (u16)FT260_GPIO_MASK;
+ break;
+ case FT260_UART_CFG_FLOW_CTRL_RTS_CTS:
+ bitmap = FT260_GPIO_UART_MODE_1_CLR;
+ break;
+ case FT260_UART_CFG_FLOW_CTRL_DTR_DSR:
+ bitmap = FT260_GPIO_UART_MODE_2_CLR;
+ break;
+ case FT260_UART_CFG_FLOW_CTRL_XON_XOFF:
+ case FT260_UART_CFG_FLOW_CTRL_NONE:
+ bitmap = FT260_GPIO_UART_MODE_3_CLR;
+ break;
+ default:
+ return;
+ }
+ ft260_gpio_en_clr(dev, bitmap);
+ bitmap = dev->gpio_uart_mode[value];
+ ft260_gpio_en_set(dev, bitmap);
+ goto exit;
+
+ case FT260_ENABLE_UART_DCD_RI:
+ bitmap = FT260_GPIO_UART_DCD_RI;
+ break;
+
+ case FT260_SELECT_GPIO2_FUNC:
+ bitmap = FT260_GPIO_2;
+ break;
+ case FT260_SELECT_GPIOA_FUNC:
+ bitmap = FT260_GPIO_A;
+ break;
+ case FT260_SELECT_GPIOG_FUNC:
+ bitmap = FT260_GPIO_G;
+ break;
+ default:
+ return;
+ }
+
+ if (value == FT260_MFPIN_GPIO)
+ ft260_gpio_en_set(dev, bitmap);
+ else
+ ft260_gpio_en_clr(dev, bitmap);
+exit:
+ hid_info(hdev, "enabled GPIOs: %04x\n", dev->gpio_en);
+}
+
+static int ft260_gpio_set(struct gpio_chip *gc, u32 offset, int value)
+{
+ int ret = 0;
+ struct ft260_gpio_write_request_report rep;
+ struct ft260_device *dev = gpiochip_get_data(gc);
+ struct hid_device *hdev = dev->hdev;
+
+ if (offset >= FT260_GPIO_TOTAL) {
+ hid_err(hdev, "%s: invalid offset %d\n", __func__, offset);
+ return -EINVAL;
+ }
+
+ ft260_dbg("offset %d val %d\n", offset, value);
+
+ mutex_lock(&dev->lock);
+
+ if (!(dev->gpio_en & (1 << offset))) {
+ hid_err(hdev, "%s: wrong pin function %d\n", __func__, offset);
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ rep.report = FT260_GPIO;
+ rep.gpio = dev->gpio;
+
+ if (offset < FT260_GPIO_MAX) {
+ if (value)
+ rep.gpio.vals |= !!value << offset;
+ else
+ rep.gpio.vals &= ~(1 << offset);
+ } else {
+ offset = offset - FT260_GPIO_MAX;
+ if (value)
+ rep.gpio.ex_vals |= !!value << offset;
+ else
+ rep.gpio.ex_vals &= ~(1 << offset);
+ }
+
+ ft260_dbg("dirs %#02x vals %#02x ex_dir %#02x ex_vals %#02x\n",
+ rep.gpio.dirs, rep.gpio.vals,
+ rep.gpio.ex_dirs, rep.gpio.ex_vals);
+
+ ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, sizeof(rep));
+ if (unlikely(ret < 0)) {
+ hid_err(hdev, "%s: cannot set GPIO: %d\n", __func__, ret);
+ goto exit;
+ }
+
+ dev->gpio = rep.gpio;
+ ret = 0;
+exit:
+ mutex_unlock(&dev->lock);
+ return ret;
+}
+
+static int ft260_gpio_direction_set(struct gpio_chip *gc, u32 offset,
+ int value, int direction)
+{
+ int ret;
+ struct ft260_gpio_read_request_report buf;
+ struct ft260_gpio_write_request_report *rep;
+ struct ft260_device *dev = gpiochip_get_data(gc);
+ struct hid_device *hdev = dev->hdev;
+
+ if (offset >= FT260_GPIO_TOTAL) {
+ hid_err(hdev, "%s: invalid offset %d\n", __func__, offset);
+ return -EINVAL;
+ }
+
+ ft260_dbg("offset %d val %d direction %d\n", offset, value, direction);
+
+ mutex_lock(&dev->lock);
+
+ if (!(dev->gpio_en & (1 << offset))) {
+ hid_err(hdev, "%s: wrong pin function %d\n", __func__, offset);
+ ret = -EIO;
+ goto exit;
+ }
+
+ ret = ft260_hid_feature_report_get(hdev, FT260_GPIO, (u8 *)&buf, sizeof(buf));
+ if (unlikely(ret < 0)) {
+ hid_err(hdev, "%s: cannot get GPIO: %d\n", __func__, ret);
+ goto exit;
+ }
+
+ rep = (struct ft260_gpio_write_request_report *)&buf;
+
+ if (direction == FT260_GPIO_DIR_OUTPUT)
+ if (offset < FT260_GPIO_MAX)
+ rep->gpio.dirs |= 1 << offset;
+ else
+ rep->gpio.ex_dirs |= 1 << (offset - FT260_GPIO_MAX);
+ else
+ if (offset < FT260_GPIO_MAX)
+ rep->gpio.dirs &= ~(1 << offset);
+ else
+ rep->gpio.ex_dirs &= ~(1 << (offset - FT260_GPIO_MAX));
+
+ ft260_dbg("dirs %#02x val %#02x ex_dirs %#02x ex_vals %#02x\n",
+ rep->gpio.dirs, rep->gpio.vals,
+ rep->gpio.ex_dirs, rep->gpio.ex_vals);
+
+ ret = ft260_hid_feature_report_set(hdev, (u8 *)rep, sizeof(*rep));
+ if (unlikely(ret < 0)) {
+ hid_err(hdev, "%s: cannot set GPIO: %d\n", __func__, ret);
+ goto exit;
+ }
+
+ dev->gpio = rep->gpio;
+ mutex_unlock(&dev->lock);
+
+ if (direction == FT260_GPIO_DIR_OUTPUT)
+ ft260_gpio_set(gc, offset, value);
+
+ return 0;
+exit:
+ mutex_unlock(&dev->lock);
+ return ret;
+}
+
+static int ft260_gpio_direction_output(struct gpio_chip *gc,
+ u32 offset, int value)
+{
+ return ft260_gpio_direction_set(gc, offset, value,
+ FT260_GPIO_DIR_OUTPUT);
+}
+
+static int ft260_gpio_direction_input(struct gpio_chip *gc, u32 offset)
+{
+ return ft260_gpio_direction_set(gc, offset, 0,
+ FT260_GPIO_DIR_INPUT);
+}
+
+static int ft260_gpio_get_all(struct gpio_chip *gc, int item)
+{
+ int ret;
+ struct ft260_gpio_read_request_report rep;
+ struct ft260_device *dev = gpiochip_get_data(gc);
+ struct hid_device *hdev = dev->hdev;
+
+ ret = ft260_hid_feature_report_get(hdev, FT260_GPIO, (u8 *)&rep, sizeof(rep));
+ if (unlikely(ret < 0)) {
+ hid_err(hdev, "%s: cannot get GPIO: %d\n", __func__, ret);
+ goto exit;
+ }
+
+ if (item == FT260_GPIO_VALUE)
+ ret = (rep.gpio.ex_vals << FT260_GPIO_MAX) | rep.gpio.vals;
+ else
+ ret = (rep.gpio.ex_dirs << FT260_GPIO_MAX) | rep.gpio.dirs;
+exit:
+ return ret;
+}
+
+static int ft260_gpio_get_direction(struct gpio_chip *gc, u32 offset)
+{
+ int ret = ft260_gpio_get_all(gc, FT260_GPIO_DIRECTION);
+
+ if (ret < 0)
+ return ret;
+ return !((ret >> offset) & 1);
+}
+
+static int ft260_gpio_get(struct gpio_chip *gc, u32 offset)
+{
+ int ret = ft260_gpio_get_all(gc, FT260_GPIO_VALUE);
+
+ if (ret < 0)
+ return ret;
+ return (ret >> offset) & 1;
+}
+
+static int ft260_gpio_init(struct ft260_device *dev,
+ struct ft260_get_system_status_report *cfg)
+{
+ int ret;
+ int label_sz;
+ char *label;
+ struct ft260_get_chip_version_report ver;
+ struct hid_device *hdev = dev->hdev;
+ char prefix[] = "ft260_";
+ u8 mode = cfg->chip_mode;
+
+ hid_info(hdev, "initialize gpio chip\n");
+
+ dev->gpio_uart_mode[0] = (u16)FT260_GPIO_UART_MODE_0_SET;
+ dev->gpio_uart_mode[1] = (u16)FT260_GPIO_UART_MODE_1_SET;
+ dev->gpio_uart_mode[2] = (u16)FT260_GPIO_UART_MODE_2_SET;
+ dev->gpio_uart_mode[3] = (u16)FT260_GPIO_UART_MODE_3_SET;
+ dev->gpio_uart_mode[4] = (u16)FT260_GPIO_UART_MODE_4_SET;
+
+ if (mode == FT260_MODE_ALL)
+ mode = FT260_MODE_BOTH;
+
+ if (mode & FT260_MODE_UART)
+ dev->gpio_en |= dev->gpio_uart_mode[cfg->uart_mode];
+ else
+ dev->gpio_en |= FT260_GPIO_UART_DEFAULT;
+
+ if (!(mode & FT260_MODE_I2C))
+ dev->gpio_en |= FT260_GPIO_I2C_DEFAULT;
+
+ if (cfg->gpio2_func == FT260_MFPIN_GPIO)
+ dev->gpio_en |= FT260_GPIO_2;
+ if (cfg->enable_wakeup_int == FT260_MFPIN_GPIO)
+ dev->gpio_en |= FT260_GPIO_3;
+ if (cfg->gpioa_func == FT260_MFPIN_GPIO)
+ dev->gpio_en |= FT260_GPIO_A;
+ if (cfg->gpiog_func == FT260_MFPIN_GPIO)
+ dev->gpio_en |= FT260_GPIO_G;
+
+ hid_info(hdev, "enabled GPIOs: %04x\n", dev->gpio_en);
+
+ dev->gc = devm_kzalloc(&hdev->dev, sizeof(*dev->gc), GFP_KERNEL);
+ if (!dev->gc)
+ return -ENOMEM;
+
+ label_sz = strlen(dev_name(&hdev->dev)) + strlen(prefix) + 1;
+ label = devm_kzalloc(&hdev->dev, label_sz, GFP_KERNEL);
+ if (!label) {
+ ret = -ENOMEM;
+ goto exit;
+ }
+ snprintf(label, label_sz, "%s%s", prefix, dev_name(&hdev->dev));
+ hid_info(hdev, "initialize gpio chip on %s\n", label);
+
+ dev->gc->label = label;
+ dev->gc->direction_input = ft260_gpio_direction_input;
+ dev->gc->direction_output = ft260_gpio_direction_output;
+ dev->gc->get_direction = ft260_gpio_get_direction;
+ dev->gc->set = ft260_gpio_set;
+ dev->gc->get = ft260_gpio_get;
+ dev->gc->base = -1;
+ dev->gc->ngpio = FT260_GPIO_TOTAL;
+ dev->gc->can_sleep = true;
+ dev->gc->parent = &hdev->dev;
+
+ /* Wakeup chip */
+ (void)ft260_hid_feature_report_get(dev->hdev, FT260_CHIP_VERSION,
+ (u8 *)&ver, sizeof(ver));
+
+ ret = devm_gpiochip_add_data(&hdev->dev, dev->gc, dev);
+ if (ret < 0)
+ hid_err(hdev, "cannot add GPIO chip %d\n", ret);
+exit:
+ return ret;
+}
+
static int ft260_get_system_config(struct hid_device *hdev,
struct ft260_get_system_status_report *cfg)
{
@@ -921,29 +1354,36 @@ static int ft260_get_system_config(struct hid_device *hdev,
return 0;
}
-static int ft260_get_interface_type(struct hid_device *hdev, struct ft260_device *dev)
+static int ft260_get_interface_type(struct ft260_device *dev,
+ struct ft260_get_system_status_report *cfg)
+
{
- struct ft260_get_system_status_report cfg;
- struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
- int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
int ret;
+ struct hid_device *hdev = dev->hdev;
+ struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
- ret = ft260_get_system_config(hdev, &cfg);
+ dev->iface_id = usbif->cur_altsetting->desc.bInterfaceNumber;
+
+ ret = ft260_get_system_config(hdev, cfg);
if (ret < 0)
return ret;
- ft260_dbg("interface: 0x%02x\n", interface);
- ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
- ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
- ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
- ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
+ ft260_dbg("interface: 0x%02x\n", dev->iface_id);
+ ft260_dbg("chip mode: 0x%02x\n", cfg->chip_mode);
+ ft260_dbg("clock_ctl: 0x%02x\n", cfg->clock_ctl);
+ ft260_dbg("i2c_enable: 0x%02x\n", cfg->i2c_enable);
+ ft260_dbg("uart_mode: 0x%02x\n", cfg->uart_mode);
+ ft260_dbg("gpio2_func: 0x%02x\n", cfg->gpio2_func);
+ ft260_dbg("gpioA_func: 0x%02x\n", cfg->gpioa_func);
+ ft260_dbg("gpioG_func: 0x%02x\n", cfg->gpiog_func);
+ ft260_dbg("wakeup_int: 0x%02x\n", cfg->enable_wakeup_int);
- dev->power_saving_en = cfg.power_saving_en;
+ dev->power_saving_en = cfg->power_saving_en;
- switch (cfg.chip_mode) {
+ switch (cfg->chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
- if (interface == 1)
+ if (dev->iface_id == 1)
ret = FT260_IFACE_UART;
else
ret = FT260_IFACE_I2C;
@@ -984,6 +1424,10 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
}
+static void ft260_attr_dummy_func(struct hid_device *hdev, u8 req, u16 value)
+{
+}
+
#define FT260_ATTR_SHOW(name, reptype, id, type, func) \
static ssize_t name##_show(struct device *kdev, \
struct device_attribute *attr, char *buf) \
@@ -1004,37 +1448,42 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
FT260_I2C_STATUS, __le16, ft260_word_show)
-#define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, func) \
+#define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, strtou, func) \
static ssize_t name##_store(struct device *kdev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
+ struct ft260_device *dev = hid_get_drvdata(hdev); \
type name; \
int ret; \
\
- if (!func(buf, 10, (ctype *)&name)) { \
+ if (!strtou(buf, 10, (ctype *)&name)) { \
rep.name = name; \
rep.report = id; \
rep.request = req; \
+ mutex_lock(&dev->lock); \
ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
sizeof(rep)); \
- if (!ret) \
- ret = count; \
+ if (ret < 0) \
+ hid_err(hdev, "%s: failed!\n", __func__); \
+ else \
+ func(hdev, req, name); \
+ mutex_unlock(&dev->lock); \
} else { \
ret = -EINVAL; \
} \
return ret; \
}
-#define FT260_BYTE_ATTR_STORE(name, reptype, req) \
+#define FT260_BYTE_ATTR_STORE(name, reptype, req, func) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
- u8, u8, kstrtou8)
+ u8, u8, kstrtou8, func)
-#define FT260_WORD_ATTR_STORE(name, reptype, req) \
+#define FT260_WORD_ATTR_STORE(name, reptype, req, func) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
- __le16, u16, kstrtou16)
+ __le16, u16, kstrtou16, func)
FT260_SSTAT_ATTR_SHOW(chip_mode);
static DEVICE_ATTR_RO(chip_mode);
@@ -1048,27 +1497,46 @@ static DEVICE_ATTR_RO(suspend_status);
FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
static DEVICE_ATTR_RO(hid_over_i2c_en);
+FT260_SSTAT_ATTR_SHOW(gpio2_func);
+FT260_BYTE_ATTR_STORE(gpio2_func, ft260_set_gpio2_func_report,
+ FT260_SELECT_GPIO2_FUNC, ft260_gpio_en_update);
+static DEVICE_ATTR_RW(gpio2_func);
+
+FT260_SSTAT_ATTR_SHOW(gpioa_func);
+FT260_BYTE_ATTR_STORE(gpioa_func, ft260_set_gpioa_func_report,
+ FT260_SELECT_GPIOA_FUNC, ft260_gpio_en_update);
+static DEVICE_ATTR_RW(gpioa_func);
+
+FT260_SSTAT_ATTR_SHOW(gpiog_func);
+FT260_BYTE_ATTR_STORE(gpiog_func, ft260_set_gpiog_func_report,
+ FT260_SELECT_GPIOG_FUNC, ft260_gpio_en_update);
+static DEVICE_ATTR_RW(gpiog_func);
+
FT260_SSTAT_ATTR_SHOW(power_saving_en);
static DEVICE_ATTR_RO(power_saving_en);
FT260_SSTAT_ATTR_SHOW(i2c_enable);
FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
- FT260_SET_I2C_MODE);
+ FT260_SET_I2C_MODE, ft260_gpio_en_update);
static DEVICE_ATTR_RW(i2c_enable);
FT260_SSTAT_ATTR_SHOW(uart_mode);
FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
- FT260_SET_UART_MODE);
+ FT260_SET_UART_MODE, ft260_gpio_en_update);
static DEVICE_ATTR_RW(uart_mode);
+FT260_BYTE_ATTR_STORE(uart_dcd_ri, ft260_set_uart_dcd_ri_report,
+ FT260_ENABLE_UART_DCD_RI, ft260_gpio_en_update);
+static DEVICE_ATTR_WO(uart_dcd_ri);
+
FT260_SSTAT_ATTR_SHOW(clock_ctl);
FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
- FT260_SET_CLOCK);
+ FT260_SET_CLOCK, ft260_attr_dummy_func);
static DEVICE_ATTR_RW(clock_ctl);
FT260_I2CST_ATTR_SHOW(clock);
FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
- FT260_SET_I2C_CLOCK_SPEED);
+ FT260_SET_I2C_CLOCK_SPEED, ft260_attr_dummy_func);
static DEVICE_ATTR_RW(clock);
static ssize_t i2c_reset_store(struct device *kdev,
@@ -1092,7 +1560,11 @@ static const struct attribute_group ft260_attr_group = {
&dev_attr_hid_over_i2c_en.attr,
&dev_attr_power_saving_en.attr,
&dev_attr_i2c_enable.attr,
+ &dev_attr_gpio2_func.attr,
+ &dev_attr_gpioa_func.attr,
+ &dev_attr_gpiog_func.attr,
&dev_attr_uart_mode.attr,
+ &dev_attr_uart_dcd_ri.attr,
&dev_attr_clock_ctl.attr,
&dev_attr_i2c_reset.attr,
&dev_attr_clock.attr,
@@ -1342,33 +1814,33 @@ static int ft260_uart_change_speed(struct ft260_device *port,
switch (termios->c_cflag & CSIZE) {
case CS7:
- req.data_bit = FT260_CFG_DATA_BITS_7;
+ req.data_bit = FT260_UART_CFG_DATA_BITS_7;
break;
case CS5:
case CS6:
hid_err(hdev, "invalid data bit size, setting a default\n");
- req.data_bit = FT260_CFG_DATA_BITS_8;
+ req.data_bit = FT260_UART_CFG_DATA_BITS_8;
termios->c_cflag &= ~CSIZE;
termios->c_cflag |= CS8;
break;
default:
case CS8:
- req.data_bit = FT260_CFG_DATA_BITS_8;
+ req.data_bit = FT260_UART_CFG_DATA_BITS_8;
break;
}
req.stop_bit = (termios->c_cflag & CSTOPB) ?
- FT260_CFG_STOP_TWO_BIT : FT260_CFG_STOP_ONE_BIT;
+ FT260_UART_CFG_STOP_TWO_BIT : FT260_UART_CFG_STOP_ONE_BIT;
if (termios->c_cflag & PARENB) {
req.parity = (termios->c_cflag & PARODD) ?
- FT260_CFG_PAR_ODD : FT260_CFG_PAR_EVEN;
+ FT260_UART_CFG_PAR_ODD : FT260_UART_CFG_PAR_EVEN;
} else {
- req.parity = FT260_CFG_PAR_NO;
+ req.parity = FT260_UART_CFG_PAR_NO;
}
baud = tty_termios_baud_rate(termios);
- if (baud == 0 || baud < FT260_CFG_BAUD_MIN || baud > FT260_CFG_BAUD_MAX) {
+ if (baud == 0 || baud < FT260_UART_CFG_BAUD_MIN || baud > FT260_UART_CFG_BAUD_MAX) {
struct tty_struct *tty = tty_port_tty_get(&port->port);
hid_err(hdev, "invalid baud rate %d\n", baud);
@@ -1385,9 +1857,9 @@ static int ft260_uart_change_speed(struct ft260_device *port,
put_unaligned_le32(cpu_to_le32(baud), &req.baudrate);
if (termios->c_cflag & CRTSCTS)
- req.flow_ctrl = FT260_CFG_FLOW_CTRL_RTS_CTS;
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_RTS_CTS;
else
- req.flow_ctrl = FT260_CFG_FLOW_CTRL_OFF;
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_OFF;
ft260_dbg("configured termios: flow control: %d, baudrate: %d, ",
req.flow_ctrl, baud);
@@ -1395,12 +1867,18 @@ static int ft260_uart_change_speed(struct ft260_device *port,
req.data_bit, req.parity,
req.stop_bit, req.breaking);
- req.flow_ctrl = FT260_CFG_FLOW_CTRL_NONE;
- req.breaking = FT260_CFG_BREAKING_NO;
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_NONE;
+ req.breaking = FT260_UART_CFG_BREAKING_NO;
+
+ mutex_lock(&port->lock);
ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
if (ret < 0)
hid_err(hdev, "failed to change termios: %d\n", ret);
+ else
+ ft260_gpio_en_update(hdev, FT260_SET_UART_MODE, req.flow_ctrl);
+
+ mutex_unlock(&port->lock);
return ret;
}
@@ -1611,9 +2089,11 @@ static const struct tty_port_operations ft260_uart_port_ops = {
static struct tty_driver *ft260_tty_driver;
-static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
+static int ft260_i2c_probe(struct ft260_device *dev,
+ struct ft260_get_system_status_report *cfg)
{
int ret;
+ struct hid_device *hdev = dev->hdev;
hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
hdev->version >> 8, hdev->version & 0xff, hdev->name,
@@ -1640,11 +2120,16 @@ static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
return ret;
}
+ ret = ft260_gpio_init(dev, cfg);
+ if (ret)
+ goto err_i2c_free;
+
ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_i2c_free;
}
+
return 0;
err_i2c_free:
@@ -1652,14 +2137,15 @@ static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
return ret;
}
-static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
+static int ft260_uart_probe(struct ft260_device *dev,
+ struct ft260_get_system_status_report *cfg)
{
struct ft260_configure_uart_request_report req;
- int ret;
+ struct hid_device *hdev = dev->hdev;
struct device *devt;
+ int ret;
INIT_WORK(&dev->wakeup_work, ft260_uart_do_wakeup);
- // FIXME: are all kfifo access secured by lock? with irq or not?
ft260_uart_wakeup_workaraund_enable(dev, true);
/* Work not started at this point */
timer_setup(&dev->wakeup_timer, ft260_uart_start_wakeup, 0);
@@ -1687,12 +2173,12 @@ static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
/* Configure UART to 9600n8 */
req.report = FT260_SYSTEM_SETTINGS;
req.request = FT260_SET_UART_CONFIG;
- req.flow_ctrl = FT260_CFG_FLOW_CTRL_NONE;
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_NONE;
put_unaligned_le32(cpu_to_le32(9600), &req.baudrate);
- req.data_bit = FT260_CFG_DATA_BITS_8;
- req.parity = FT260_CFG_PAR_NO;
- req.stop_bit = FT260_CFG_STOP_ONE_BIT;
- req.breaking = FT260_CFG_BREAKING_NO;
+ req.data_bit = FT260_UART_CFG_DATA_BITS_8;
+ req.parity = FT260_UART_CFG_PAR_NO;
+ req.stop_bit = FT260_UART_CFG_STOP_ONE_BIT;
+ req.breaking = FT260_UART_CFG_BREAKING_NO;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
if (ret < 0) {
@@ -1700,6 +2186,20 @@ static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
goto err_hid_report;
}
+ cfg->uart_mode = FT260_UART_CFG_FLOW_CTRL_NONE;
+
+ if (dev->iface_id == 0) {
+ ret = ft260_gpio_init(dev, cfg);
+ if (ret)
+ goto err_hid_report;
+
+ ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
+ if (ret < 0) {
+ hid_err(hdev, "failed to create sysfs attrs\n");
+ goto err_hid_report;
+ }
+ }
+
return 0;
err_hid_report:
@@ -1713,6 +2213,7 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
struct ft260_get_chip_version_report version;
+ struct ft260_get_system_status_report cfg;
int ret;
if (!hid_is_usb(hdev))
@@ -1727,6 +2228,7 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
goto alloc_fail;
}
hid_set_drvdata(hdev, dev);
+ dev->hdev = hdev;
ret = hid_parse(hdev);
if (ret) {
@@ -1757,21 +2259,18 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
version.chip_code[0], version.chip_code[1],
version.chip_code[2], version.chip_code[3]);
- ret = ft260_get_interface_type(hdev, dev);
- if (ret <= FT260_IFACE_NONE)
- goto err_hid_close;
-
- hid_set_drvdata(hdev, dev);
- dev->hdev = hdev;
-
mutex_init(&dev->lock);
spin_lock_init(&dev->read_lock);
init_completion(&dev->wait);
+ ret = ft260_get_interface_type(dev, &cfg);
+ if (ret <= FT260_IFACE_NONE)
+ goto err_hid_close;
+
if (ret == FT260_IFACE_I2C)
- ret = ft260_i2c_probe(hdev, dev);
+ ret = ft260_i2c_probe(dev, &cfg);
else
- ret = ft260_uart_probe(hdev, dev);
+ ret = ft260_uart_probe(dev, &cfg);
if (ret)
goto err_hid_close;
@@ -1800,6 +2299,9 @@ static void ft260_remove(struct hid_device *hdev)
dev->index);
ft260_uart_port_remove(dev);
/* dev is still needed, so we will free it in _destroy func */
+ if (dev->iface_id == 0)
+ sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
+
} else {
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 04/13] HID: ft260: i2c: reduce driver module loading time
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (2 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 03/13] HID: ft260: add GPIO support on top of UART Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 05/13] HID: ft260: i2c: silence sysfs store big-numbers Michael Zaidman
` (9 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
The HWMON class I2C client drivers register the address range to scan
during the I2C master module probing. For example, the lm75 module
registers eight bus addresses scanned during the ft260 module loading.
In the case of ft260, the bus is scanned by ft260_i2c_read, but due to
an excessive read completion timeout, it took about 40 seconds for the
ft260 driver to scan the lm75 address range before exiting the probe
function.
Adjust the i2c_read timeout so it depends on the amount of data to
read, reducing the lm75 address range scan time to about 200ms.
The 25 ms per-report budget was checked on a UMFT260EV1A with a 24LC512
EEPROM by shrinking the timeout until reads began to fail: a 60-byte
read still completes at 9 ms and times out at 7 ms, against 5.4 ms of
wire time at 100 kHz, so the fixed USB, HID and scheduling overhead
costs a few milliseconds. At the chip's slowest 60 kHz clock that chunk
needs about 9 ms on the wire and the 180-byte chunk about 27 ms, so both
keep roughly a factor of two under the 25 ms and 75 ms limits and the
timeouts do not need retuning for slower bus clocks.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index cc3b8f5791de..4435a39fce23 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -56,6 +56,13 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
+
+/* Time in ms to wait for a single report read data transfer completion */
+#define FT260_RD_ONE_REPORT_TO (25)
+
+/* Time in ms to wait for a multi-report read data transfer completion */
+#define FT260_RD_MULTI_REPORT_TO (FT260_RD_ONE_REPORT_TO * FT260_RD_DATA_MAX / 60)
+
#define FT260_WR_I2C_DATA_MAX (60)
#define FT260_WR_UART_DATA_MAX (62)
#define FT260_GPIOCHIP "ft260_gpio"
@@ -713,7 +720,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
{
u16 rd_len;
u16 rd_data_max = 60;
- int timeout, ret = 0;
+ int timeout, timeout_jiffies, ret = 0;
struct ft260_i2c_read_request_report rep;
struct hid_device *hdev = dev->hdev;
unsigned long irqflags;
@@ -732,10 +739,12 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
flag = 0; /* no fresh START - continue current transaction */
do {
if (len <= rd_data_max) {
+ timeout = FT260_RD_ONE_REPORT_TO;
rd_len = len;
if (want_stop)
flag |= FT260_FLAG_STOP;
} else {
+ timeout = FT260_RD_MULTI_REPORT_TO;
rd_len = rd_data_max;
}
rd_data_max = FT260_RD_DATA_MAX;
@@ -762,8 +771,8 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
goto ft260_i2c_read_exit;
}
- timeout = msecs_to_jiffies(5000);
- if (!wait_for_completion_timeout(&dev->wait, timeout)) {
+ timeout_jiffies = msecs_to_jiffies(timeout);
+ if (!wait_for_completion_timeout(&dev->wait, timeout_jiffies)) {
ret = -ETIMEDOUT;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 05/13] HID: ft260: i2c: silence sysfs store big-numbers
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (3 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 04/13] HID: ft260: i2c: reduce driver module loading time Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 06/13] HID: ft260: i2c: reduce bus-error message severity Michael Zaidman
` (8 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
FT260_ATTR_STORE returned the feature-report transfer length from
ft260_hid_feature_report_set() instead of the sysfs write size. For
values like 1000, userspace writes more bytes than the HID report
length, so the VFS retries the remainder and reports "Invalid argument"
even though the attribute was applied correctly:
$ echo 1000 > .../clock
bash: line 1: echo: write error: Invalid argument
$ cat .../clock
1000
Return count after a successful parse so the full write is consumed.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 4435a39fce23..8db896f164cd 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -1479,6 +1479,7 @@ static void ft260_attr_dummy_func(struct hid_device *hdev, u8 req, u16 value)
hid_err(hdev, "%s: failed!\n", __func__); \
else \
func(hdev, req, name); \
+ ret = count; \
mutex_unlock(&dev->lock); \
} else { \
ret = -EINVAL; \
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 06/13] HID: ft260: i2c: reduce bus-error message severity
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (4 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 05/13] HID: ft260: i2c: silence sysfs store big-numbers Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 07/13] HID: ft260: uart: enable flow control Michael Zaidman
` (7 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
The driver logged errors when the FT260 reported an I2C bus error or when
ft260_i2c_write / ft260_smbus_write failed after such a status. That was
meant to flag unexpected failures, but bus scans (i2cdetect, HWMON
discovery, transceiver monitoring, and similar) hit missing devices
routinely and should not spam dmesg with either:
i2c bus error: %#02x
ft260_i2c_write: failed with -5
Keep returning -EIO, but print those via ft260_dbg. Still use hid_err in
ft260_hid_output_report_check_status for actual HID/USB transport
failures, which are genuine errors.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 8db896f164cd..1ed3701eaaf7 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -565,7 +565,7 @@ static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
* to 1, bit 1 is also set to 1.
*/
if (report.bus_status & FT260_I2C_STATUS_ERROR) {
- hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
+ ft260_dbg("i2c bus error: %#02x\n", report.bus_status);
return -EIO;
}
@@ -599,6 +599,7 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
+ hid_err(hdev, "%s: failed with %d\n", __func__, ret);
ft260_i2c_reset(hdev);
return ret;
}
@@ -638,7 +639,6 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
int ret, wr_len, idx = 0;
- struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
@@ -669,7 +669,7 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
wr_len + 4);
if (ret < 0) {
- hid_err(hdev, "%s: failed with %d\n", __func__, ret);
+ ft260_dbg("%s: failed with %d\n", __func__, ret);
return ret;
}
@@ -685,7 +685,7 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
u8 *data, u8 data_len, u8 flag)
{
- int ret = 0;
+ int ret;
int len = 4;
struct ft260_i2c_write_request_report *rep =
@@ -710,7 +710,7 @@ static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
if (ret < 0)
- hid_err(dev->hdev, "%s: failed with %d\n", __func__, ret);
+ ft260_dbg("%s: failed with %d\n", __func__, ret);
return ret;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 07/13] HID: ft260: uart: enable flow control
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (5 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 06/13] HID: ft260: i2c: reduce bus-error message severity Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
` (6 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
The FT260 is a full-speed USB device. Interrupt transfers poll at 1 ms
with a 64-byte max packet size, so the theoretical UART payload ceiling
is about 60 kB/s (~600 kbaud in 8N1). The chip advertises baud rates
above 1M and small transfers can work there, but large transfers are not
reliable above 460800 baud without flow control.
Stop forcing FT260_UART_CFG_FLOW_CTRL_NONE after termios setup. Honor
CRTSCTS (RTS/CTS) and IXON/IXOFF (XON/XOFF) when configuring the UART.
Tested at 1500000 baud both directions against an FT2232H with XON/XOFF
and with RTS/CTS (~2.9 MB transfers, no data corruption).
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 1ed3701eaaf7..1da42b8756d8 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -1868,8 +1868,12 @@ static int ft260_uart_change_speed(struct ft260_device *port,
if (termios->c_cflag & CRTSCTS)
req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_RTS_CTS;
+ else if (termios->c_iflag & (IXON | IXOFF))
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_XON_XOFF;
else
- req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_OFF;
+ req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_NONE;
+
+ req.breaking = FT260_UART_CFG_BREAKING_NO;
ft260_dbg("configured termios: flow control: %d, baudrate: %d, ",
req.flow_ctrl, baud);
@@ -1877,9 +1881,6 @@ static int ft260_uart_change_speed(struct ft260_device *port,
req.data_bit, req.parity,
req.stop_bit, req.breaking);
- req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_NONE;
- req.breaking = FT260_UART_CFG_BREAKING_NO;
-
mutex_lock(&port->lock);
ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (6 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 07/13] HID: ft260: uart: enable flow control Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 8:08 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface Michael Zaidman
` (5 subsequent siblings)
13 siblings, 1 reply; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
Add TIOCMGET/TIOCMSET (and thus TIOCMBIS/TIOCMBIC) support so userspace
can drive DTR/RTS when the modem pins are in GPIO mode. This enables
programming an ESP32 over the FT260 with esptool, which resets the chip
via those lines.
Also key GPIO sysfs registration off chip_mode rather than USB interface
number 0, and include DCD/RI in UART mode 3/4 GPIO sets so the modem
mask matches the pins esptool needs.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 151 ++++++++++++++++++++++++++++++++++------
1 file changed, 130 insertions(+), 21 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 1da42b8756d8..554d8e3ddb6d 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -16,6 +16,7 @@
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/kfifo.h>
+#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/minmax.h>
#include <linux/unaligned.h>
@@ -182,7 +183,7 @@ enum {
FT260_GPIO_DIR_OUTPUT = 0x01,
};
-/* GPIO offsets */
+/* GPIO bit masks */
enum {
FT260_GPIO_0 = (1 << 0),
FT260_GPIO_1 = (1 << 1),
@@ -215,7 +216,8 @@ enum {
FT260_GPIO_UART_MODE_1_SET = (FT260_GPIO_UART_DTR_DSR),
FT260_GPIO_UART_MODE_2_SET = (FT260_GPIO_UART_RTS_CTS),
FT260_GPIO_UART_MODE_3_SET = (FT260_GPIO_UART_RTS_CTS |
- FT260_GPIO_UART_DTR_DSR),
+ FT260_GPIO_UART_DTR_DSR |
+ FT260_GPIO_UART_DCD_RI),
FT260_GPIO_UART_MODE_4_SET = (FT260_GPIO_UART_MODE_3_SET),
FT260_GPIO_UART_DEFAULT = (FT260_GPIO_UART_MODE_0_SET),
FT260_GPIO_UART_MODE_1_CLR = (FT260_GPIO_UART_RX_TX |
@@ -226,6 +228,16 @@ enum {
FT260_GPIO_UART_MODES = (5),
};
+/* UART modem control GPIO offsets */
+enum {
+ FT260_GPIO_UART_DCD = (4),
+ FT260_GPIO_UART_RI = (5),
+ FT260_GPIO_UART_RTS = (7),
+ FT260_GPIO_UART_CTS = (10),
+ FT260_GPIO_UART_DTR = (11),
+ FT260_GPIO_UART_DSR = (13),
+};
+
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | (report_id))
/* Feature In reports */
@@ -440,6 +452,7 @@ MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
+ int chip_mode;
int iface_type;
int iface_id;
struct list_head device_list;
@@ -1097,7 +1110,27 @@ static void ft260_gpio_en_update(struct hid_device *hdev, u8 req, u8 value)
else
ft260_gpio_en_clr(dev, bitmap);
exit:
- hid_info(hdev, "enabled GPIOs: %04x\n", dev->gpio_en);
+ hid_info(hdev, "enabled GPIOs: %04x, bitmap %04x\n",
+ dev->gpio_en, bitmap);
+}
+
+static void ft260_gpio_output_cfg(struct ft260_gpio_state *gpio,
+ u32 offset, int value)
+{
+ if (offset < FT260_GPIO_MAX) {
+ gpio->dirs |= 1 << offset;
+ if (value)
+ gpio->vals |= !!value << offset;
+ else
+ gpio->vals &= ~(1 << offset);
+ } else {
+ offset = offset - FT260_GPIO_MAX;
+ gpio->ex_dirs |= 1 << offset;
+ if (value)
+ gpio->ex_vals |= !!value << offset;
+ else
+ gpio->ex_vals &= ~(1 << offset);
+ }
}
static int ft260_gpio_set(struct gpio_chip *gc, u32 offset, int value)
@@ -1138,7 +1171,7 @@ static int ft260_gpio_set(struct gpio_chip *gc, u32 offset, int value)
rep.gpio.ex_vals &= ~(1 << offset);
}
- ft260_dbg("dirs %#02x vals %#02x ex_dir %#02x ex_vals %#02x\n",
+ ft260_dbg("dirs %#02x vals %#02x ex_dirs %#02x ex_vals %#02x\n",
rep.gpio.dirs, rep.gpio.vals,
rep.gpio.ex_dirs, rep.gpio.ex_vals);
@@ -1283,8 +1316,6 @@ static int ft260_gpio_init(struct ft260_device *dev,
char prefix[] = "ft260_";
u8 mode = cfg->chip_mode;
- hid_info(hdev, "initialize gpio chip\n");
-
dev->gpio_uart_mode[0] = (u16)FT260_GPIO_UART_MODE_0_SET;
dev->gpio_uart_mode[1] = (u16)FT260_GPIO_UART_MODE_1_SET;
dev->gpio_uart_mode[2] = (u16)FT260_GPIO_UART_MODE_2_SET;
@@ -1324,7 +1355,6 @@ static int ft260_gpio_init(struct ft260_device *dev,
goto exit;
}
snprintf(label, label_sz, "%s%s", prefix, dev_name(&hdev->dev));
- hid_info(hdev, "initialize gpio chip on %s\n", label);
dev->gc->label = label;
dev->gc->direction_input = ft260_gpio_direction_input;
@@ -1388,6 +1418,7 @@ static int ft260_get_interface_type(struct ft260_device *dev,
ft260_dbg("wakeup_int: 0x%02x\n", cfg->enable_wakeup_int);
dev->power_saving_en = cfg->power_saving_en;
+ dev->chip_mode = cfg->chip_mode;
switch (cfg->chip_mode) {
case FT260_MODE_ALL:
@@ -1405,7 +1436,6 @@ static int ft260_get_interface_type(struct ft260_device *dev,
break;
}
- dev->iface_type = ret;
return ret;
}
@@ -1912,6 +1942,78 @@ static void ft260_uart_set_termios(struct tty_struct *tty,
ft260_uart_change_speed(port, &tty->termios, NULL);
}
+static int ft260_uart_tiocmget(struct tty_struct *tty)
+{
+ struct ft260_device *port = tty->driver_data;
+ int gpios;
+
+ /*
+ * The modem lines control via ioctl is allowed only
+ * when the related pins are in the gpio mode.
+ */
+ int mask = FT260_GPIO_UART_MODE_4_SET;
+
+ if ((port->gpio_en & mask) != mask) {
+ ft260_dbg("Illegal gpio config %#02x != mask %#04x\n",
+ port->gpio_en, mask);
+ return 0;
+ }
+
+ gpios = ft260_gpio_get_all(port->gc, FT260_GPIO_VALUE);
+
+ return (((gpios & FT260_GPIO_B) ? TIOCM_RTS : 0) |
+ ((gpios & FT260_GPIO_E) ? TIOCM_CTS : 0) |
+ ((gpios & FT260_GPIO_F) ? TIOCM_DTR : 0) |
+ ((gpios & FT260_GPIO_H) ? TIOCM_DSR : 0) |
+ ((gpios & FT260_GPIO_4) ? TIOCM_CAR : 0) |
+ ((gpios & FT260_GPIO_5) ? TIOCM_RNG : 0));
+}
+
+static int ft260_uart_tiocmset(struct tty_struct *tty,
+ unsigned int set, unsigned int clear)
+{
+ int ret;
+ struct ft260_device *port = tty->driver_data;
+ struct ft260_gpio_write_request_report rep;
+ struct hid_device *hdev = port->hdev;
+ int mask = FT260_GPIO_UART_MODE_4_SET;
+
+ if ((port->gpio_en & mask) != mask) {
+ ft260_dbg("Illegal gpio config %#02x != mask %#04x\n",
+ port->gpio_en, mask);
+ return 0;
+ }
+ rep.report = FT260_GPIO;
+ rep.gpio = port->gpio;
+
+ mutex_lock(&port->lock);
+
+ if (set & TIOCM_RTS)
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 1);
+ if (set & TIOCM_DTR)
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 1);
+ if (clear & TIOCM_RTS)
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 0);
+ if (clear & TIOCM_DTR)
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 0);
+
+ ft260_dbg("dirs %#02x vals %#02x ex_dirs %#02x ex_vals %#02x\n",
+ rep.gpio.dirs, rep.gpio.vals,
+ rep.gpio.ex_dirs, rep.gpio.ex_vals);
+
+ ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, sizeof(rep));
+ if (unlikely(ret < 0)) {
+ hid_err(hdev, "%s: cannot set GPIO: %d\n", __func__, ret);
+ mutex_unlock(&port->lock);
+ return -EIO;
+ }
+
+ port->gpio = rep.gpio;
+ mutex_unlock(&port->lock);
+
+ return 0;
+}
+
static int ft260_uart_install(struct tty_driver *driver, struct tty_struct *tty)
{
int idx = tty->index;
@@ -1988,6 +2090,8 @@ static const struct tty_operations ft260_uart_ops = {
.write_room = ft260_uart_write_room,
.chars_in_buffer = ft260_uart_chars_in_buffer,
.set_termios = ft260_uart_set_termios,
+ .tiocmget = ft260_uart_tiocmget,
+ .tiocmset = ft260_uart_tiocmset,
.hangup = ft260_uart_hangup,
.install = ft260_uart_install,
.cleanup = ft260_uart_cleanup,
@@ -2131,14 +2235,16 @@ static int ft260_i2c_probe(struct ft260_device *dev,
return ret;
}
- ret = ft260_gpio_init(dev, cfg);
- if (ret)
- goto err_i2c_free;
+ if (cfg->chip_mode == FT260_MODE_I2C) {
+ ret = ft260_gpio_init(dev, cfg);
+ if (ret)
+ goto err_i2c_free;
- ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
- if (ret < 0) {
- hid_err(hdev, "failed to create sysfs attrs\n");
- goto err_i2c_free;
+ ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
+ if (ret < 0) {
+ hid_err(hdev, "failed to create sysfs attrs\n");
+ goto err_i2c_free;
+ }
}
return 0;
@@ -2199,7 +2305,8 @@ static int ft260_uart_probe(struct ft260_device *dev,
cfg->uart_mode = FT260_UART_CFG_FLOW_CTRL_NONE;
- if (dev->iface_id == 0) {
+ if (dev->chip_mode & FT260_MODE_UART ||
+ dev->chip_mode == FT260_MODE_ALL) {
ret = ft260_gpio_init(dev, cfg);
if (ret)
goto err_hid_report;
@@ -2274,11 +2381,11 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
spin_lock_init(&dev->read_lock);
init_completion(&dev->wait);
- ret = ft260_get_interface_type(dev, &cfg);
- if (ret <= FT260_IFACE_NONE)
+ dev->iface_type = ft260_get_interface_type(dev, &cfg);
+ if (dev->iface_type <= FT260_IFACE_NONE)
goto err_hid_close;
- if (ret == FT260_IFACE_I2C)
+ if (dev->iface_type == FT260_IFACE_I2C)
ret = ft260_i2c_probe(dev, &cfg);
else
ret = ft260_uart_probe(dev, &cfg);
@@ -2310,11 +2417,13 @@ static void ft260_remove(struct hid_device *hdev)
dev->index);
ft260_uart_port_remove(dev);
/* dev is still needed, so we will free it in _destroy func */
- if (dev->iface_id == 0)
+ if (dev->chip_mode & FT260_MODE_UART ||
+ dev->chip_mode == FT260_MODE_ALL)
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
} else {
- sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
+ if (dev->chip_mode == FT260_MODE_I2C)
+ sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
kfree(dev);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (7 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 8:13 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity Michael Zaidman
` (4 subsequent siblings)
13 siblings, 1 reply; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
Split the single sysfs attribute group into per-interface groups based
on chip mode (DCNF0/DCNF1):
- I2C-only (mode 1): full I2C + GPIO attributes on the I2C HID iface
- Dual mode (0/3): I2C-facing attrs on the I2C iface; GPIO and UART
attrs on the UART iface
- UART-only (mode 2): GPIO and UART attrs on the UART iface
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 58 +++++++++++++++++++++++++++++++++++------
1 file changed, 50 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 554d8e3ddb6d..0a1875a1a7ec 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -1592,7 +1592,7 @@ static ssize_t i2c_reset_store(struct device *kdev,
}
static DEVICE_ATTR_WO(i2c_reset);
-static const struct attribute_group ft260_attr_group = {
+static const struct attribute_group ft260_i2c_chip_mode_1_attr_group = {
.attrs = (struct attribute *[]) {
&dev_attr_chip_mode.attr,
&dev_attr_pwren_status.attr,
@@ -1603,11 +1603,35 @@ static const struct attribute_group ft260_attr_group = {
&dev_attr_gpio2_func.attr,
&dev_attr_gpioa_func.attr,
&dev_attr_gpiog_func.attr,
+ &dev_attr_clock_ctl.attr,
+ &dev_attr_i2c_reset.attr,
+ &dev_attr_clock.attr,
+ NULL
+ }
+};
+
+static const struct attribute_group ft260_i2c_chip_mode_0_3_attr_group = {
+ .attrs = (struct attribute *[]) {
+ &dev_attr_hid_over_i2c_en.attr,
+ &dev_attr_i2c_reset.attr,
+ &dev_attr_clock.attr,
+ NULL
+ }
+};
+
+static const struct attribute_group ft260_uart_chip_mode_0_2_3_attr_group = {
+ .attrs = (struct attribute *[]) {
+ &dev_attr_chip_mode.attr,
+ &dev_attr_pwren_status.attr,
+ &dev_attr_suspend_status.attr,
+ &dev_attr_power_saving_en.attr,
+ &dev_attr_gpio2_func.attr,
+ &dev_attr_gpioa_func.attr,
+ &dev_attr_gpiog_func.attr,
&dev_attr_uart_mode.attr,
&dev_attr_uart_dcd_ri.attr,
&dev_attr_clock_ctl.attr,
&dev_attr_i2c_reset.attr,
- &dev_attr_clock.attr,
NULL
}
};
@@ -2240,7 +2264,16 @@ static int ft260_i2c_probe(struct ft260_device *dev,
if (ret)
goto err_i2c_free;
- ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
+ ret = sysfs_create_group(&hdev->dev.kobj,
+ &ft260_i2c_chip_mode_1_attr_group);
+ if (ret < 0) {
+ hid_err(hdev, "failed to create sysfs attrs\n");
+ goto err_i2c_free;
+ }
+ } else if (cfg->chip_mode == FT260_MODE_ALL ||
+ cfg->chip_mode == FT260_MODE_BOTH) {
+ ret = sysfs_create_group(&hdev->dev.kobj,
+ &ft260_i2c_chip_mode_0_3_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_i2c_free;
@@ -2306,12 +2339,14 @@ static int ft260_uart_probe(struct ft260_device *dev,
cfg->uart_mode = FT260_UART_CFG_FLOW_CTRL_NONE;
if (dev->chip_mode & FT260_MODE_UART ||
- dev->chip_mode == FT260_MODE_ALL) {
+ dev->chip_mode == FT260_MODE_ALL ||
+ dev->chip_mode == FT260_MODE_BOTH) {
ret = ft260_gpio_init(dev, cfg);
if (ret)
goto err_hid_report;
- ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
+ ret = sysfs_create_group(&hdev->dev.kobj,
+ &ft260_uart_chip_mode_0_2_3_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_hid_report;
@@ -2418,12 +2453,19 @@ static void ft260_remove(struct hid_device *hdev)
ft260_uart_port_remove(dev);
/* dev is still needed, so we will free it in _destroy func */
if (dev->chip_mode & FT260_MODE_UART ||
- dev->chip_mode == FT260_MODE_ALL)
- sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
+ dev->chip_mode == FT260_MODE_ALL ||
+ dev->chip_mode == FT260_MODE_BOTH)
+ sysfs_remove_group(&hdev->dev.kobj,
+ &ft260_uart_chip_mode_0_2_3_attr_group);
} else {
if (dev->chip_mode == FT260_MODE_I2C)
- sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
+ sysfs_remove_group(&hdev->dev.kobj,
+ &ft260_i2c_chip_mode_1_attr_group);
+ else if (dev->chip_mode == FT260_MODE_ALL ||
+ dev->chip_mode == FT260_MODE_BOTH)
+ sysfs_remove_group(&hdev->dev.kobj,
+ &ft260_i2c_chip_mode_0_3_attr_group);
i2c_del_adapter(&dev->adap);
kfree(dev);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (8 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 8:16 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 11/13] HID: ft260: i2c: fix large write transaction failure Michael Zaidman
` (3 subsequent siblings)
13 siblings, 1 reply; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
From: Rio Liu <rio@r26.me>
The FT260 modem control GPIOs for RTS, CTS, DTR, and DSR are active-low.
Invert the TIOCMGET sense and the TIOCMSET drive levels for those pins
so userspace ioctl state matches the electrical lines. Leave DCD and RI
unchanged.
Signed-off-by: Rio Liu <rio@r26.me>
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 0a1875a1a7ec..a2f4b6e5a16b 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -1985,10 +1985,10 @@ static int ft260_uart_tiocmget(struct tty_struct *tty)
gpios = ft260_gpio_get_all(port->gc, FT260_GPIO_VALUE);
- return (((gpios & FT260_GPIO_B) ? TIOCM_RTS : 0) |
- ((gpios & FT260_GPIO_E) ? TIOCM_CTS : 0) |
- ((gpios & FT260_GPIO_F) ? TIOCM_DTR : 0) |
- ((gpios & FT260_GPIO_H) ? TIOCM_DSR : 0) |
+ return ((!(gpios & FT260_GPIO_B) ? TIOCM_RTS : 0) |
+ (!(gpios & FT260_GPIO_E) ? TIOCM_CTS : 0) |
+ (!(gpios & FT260_GPIO_F) ? TIOCM_DTR : 0) |
+ (!(gpios & FT260_GPIO_H) ? TIOCM_DSR : 0) |
((gpios & FT260_GPIO_4) ? TIOCM_CAR : 0) |
((gpios & FT260_GPIO_5) ? TIOCM_RNG : 0));
}
@@ -2013,13 +2013,13 @@ static int ft260_uart_tiocmset(struct tty_struct *tty,
mutex_lock(&port->lock);
if (set & TIOCM_RTS)
- ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 1);
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 0);
if (set & TIOCM_DTR)
- ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 1);
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 0);
if (clear & TIOCM_RTS)
- ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 0);
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_RTS, 1);
if (clear & TIOCM_DTR)
- ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 0);
+ ft260_gpio_output_cfg(&rep.gpio, FT260_GPIO_UART_DTR, 1);
ft260_dbg("dirs %#02x vals %#02x ex_dirs %#02x ex_vals %#02x\n",
rep.gpio.dirs, rep.gpio.vals,
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 11/13] HID: ft260: i2c: fix large write transaction failure
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (9 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 12/13] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration Michael Zaidman
` (2 subsequent siblings)
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman, Chris Keeser
Fixes a regression from commit 5afac727defa ("HID: ft260: missed NACK
from busy device"), which waited for bus-idle after every HID write
report except when the report flag was exactly FT260_FLAG_START.
Multi-report I2C writes keep the bus busy between chunks for atomicity.
Treating those middle reports like a final STOP caused ft260_xfer_status()
to spin on -EAGAIN after the second HID report and fail large writes.
Write-then-read combined transfers still worked.
Pass the bus-busy check policy from the caller: ft260_i2c_write() and
ft260_smbus_write() wait for idle only when the report they send carries
STOP; intermediate chunks and the command phase of a combined transfer
do not.
The SMBus side matters for register reads. The command phase is written
with FT260_FLAG_START and no STOP, because a repeated START read follows
and the controller deliberately keeps the bus busy to keep the
transaction atomic. Checking the busy bit there makes
ft260_xfer_status() return -EAGAIN on every retry and the command write
fail with -EIO, which breaks probing of clients that read registers:
ads1x19 6-0040: Failed to read config register: -5
leds-pca963x: probe of 6-0008 failed with error -5
Fixes: 5afac727defa ("HID: ft260: missed NACK from busy device")
Reported-by: Chris Keeser <chriskeeser@meta.com>
Closes: https://github.com/MichaelZaidman/hid-ft260/issues/35
Tested-by: Chris Keeser <chriskeeser@meta.com>
Reported-by: Bruno Giacomazzi <brunoceg1@gmail.com>
Closes: https://github.com/MichaelZaidman/hid-ft260/issues/42
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 35 ++++++++++++++++-------------------
1 file changed, 16 insertions(+), 19 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index a2f4b6e5a16b..36687c086b40 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -566,9 +566,14 @@ static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
}
dev->clock = le16_to_cpu(report.clock);
- ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
- dev->clock);
+ ft260_dbg("bus_status %#02x, clock %u, bus_busy %#02x\n",
+ report.bus_status, dev->clock, bus_busy);
+ /*
+ * Do not check the busy bit for combined transactions
+ * since the controller keeps the bus busy between writing
+ * and reading IOs to ensure an atomic operation.
+ */
if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
return -EAGAIN;
@@ -602,13 +607,10 @@ static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
- u8 *data, int len)
+ u8 *data, int len, u8 bus_busy)
{
- u8 bus_busy;
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
- struct ft260_i2c_write_request_report *rep =
- (struct ft260_i2c_write_request_report *)data;
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
@@ -625,16 +627,6 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
ft260_dbg("wait %d usec, len %d\n", usec, len);
}
- /*
- * Do not check the busy bit for combined transactions
- * since the controller keeps the bus busy between writing
- * and reading IOs to ensure an atomic operation.
- */
- if (rep->flag == FT260_FLAG_START)
- bus_busy = 0;
- else
- bus_busy = FT260_I2C_STATUS_BUS_BUSY;
-
do {
ret = ft260_xfer_status(dev, bus_busy);
if (ret != -EAGAIN)
@@ -651,6 +643,7 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
+ u8 bus_busy = 0;
int ret, wr_len, idx = 0;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
@@ -663,8 +656,10 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
do {
if (len <= FT260_WR_I2C_DATA_MAX) {
wr_len = len;
- if (flag == FT260_FLAG_START_STOP)
+ if (flag == FT260_FLAG_START_STOP) {
rep->flag |= FT260_FLAG_STOP;
+ bus_busy = FT260_I2C_STATUS_BUS_BUSY;
+ }
} else {
wr_len = FT260_WR_I2C_DATA_MAX;
}
@@ -680,7 +675,7 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
- wr_len + 4);
+ wr_len + 4, bus_busy);
if (ret < 0) {
ft260_dbg("%s: failed with %d\n", __func__, ret);
return ret;
@@ -721,7 +716,9 @@ static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
- ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
+ ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len,
+ (flag & FT260_FLAG_STOP) ?
+ FT260_I2C_STATUS_BUS_BUSY : 0);
if (ret < 0)
ft260_dbg("%s: failed with %d\n", __func__, ret);
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 12/13] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (10 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 11/13] HID: ft260: i2c: fix large write transaction failure Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-22 21:39 ` [PATCH 13/13] HID: ft260: i2c: abort in-flight transfers with STOP before reset Michael Zaidman
2026-08-25 8:21 ` [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Linus Walleij
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman, Andreas Boose
FTDI errata TN_189 (Section 2.1) documents a silicon bug where the
FT260's USB interrupt endpoints are occasionally halted right after
enumeration. When this happens, Clear-Feature ENDPOINT_HALT does not
recover the endpoint and the only known recovery is a USB device reset.
This patch implements an in-driver workaround:
1. ft260_check_intr_ep_health() observes the STALL by attempting an
actual interrupt IN transfer. The FT260 does not honestly report
its halt state via USB_REQ_GET_STATUS (returns 0 even when
STALLed; confirmed separately by FTDI engineering with a USB
analyzer trace), so we cannot rely on it; instead we let the
host controller return -EPIPE when it sees the STALL handshake.
2. ft260_check_dev_responsive() catches the broader broken state
where the interrupt endpoint may look healthy but the device
still fails to respond to control transfers. A USB_REQ_GET_STATUS
to the device with a short 500 ms timeout fails fast on a broken
device, preventing later probe stages from hanging on usbhid's
default 10 s timeouts and starving the usb_hub_wq workqueue.
3. When either check fails, probe schedules a deferred work item
and returns -ENODEV so that hub_event releases the device lock
quickly. The work item retries usb_lock_device_for_reset() up to
10 times (~10 s; each attempt already polls for up to one second)
before giving up, then calls usb_reset_device() and explicitly
unbinds/rebinds all USB interfaces to force usbhid to recreate
the HID devices and trigger a fresh ft260_probe(). The
unbind+rebind step is needed because usbhid's pre_reset and
post_reset both return 0, so usb_reset_device() alone keeps
usbhid bound to stale HID device state.
FTDI engineering tested this on a Raspberry Pi 4 Model B Rev 1.5
running Linux 6.12.62-v8+ on an xhci_hcd host, with the FT260 connected
at full-speed through a downstream USB 2.0 hub. Across 28,684
re-enumeration cycles, 350 cycles triggered the recovery path. Two of
those required two consecutive USB resets before the device returned.
All 28,684 cycles recovered to a fully functional state with I2C and
UART working end-to-end.
Reported-by: Andreas Boose <Andreas.Boose@almex.de>
Closes: https://github.com/MichaelZaidman/hid-ft260/issues/40
Link: https://ftdichip.com/wp-content/uploads/2026/05/TN_189-FT260-Errata-Technical-Note.pdf
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 229 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 229 insertions(+)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 36687c086b40..9ae688f6208f 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -2359,15 +2359,227 @@ static int ft260_uart_probe(struct ft260_device *dev,
return ret;
}
+/*
+ * FT260 errata TN_189 Section 2.1: the USB interrupt endpoints are
+ * occasionally halted right after enumeration. When this happens:
+ * - Standard Clear-Feature ENDPOINT_HALT does not recover the endpoint
+ * - Subsequent communication with the device is dead
+ * - The only known recovery is a USB device reset
+ *
+ * A separate finding from FTDI engineering (confirmed by USB analyzer
+ * trace while testing this workaround) is that the FT260 does NOT
+ * honestly report the halt state via USB_REQ_GET_STATUS: it returns 0
+ * even when the endpoint is STALLed. Detection must therefore observe
+ * the STALL handshake at the host controller level rather than ask
+ * the device.
+ *
+ * Recovery is performed by a deferred work item that resets the USB
+ * device and unbinds/rebinds all interfaces to force usbhid to
+ * destroy stale HID devices and create fresh ones, which triggers a
+ * new ft260_probe() that succeeds.
+ *
+ * https://ftdichip.com/wp-content/uploads/2026/05/TN_189-FT260-Errata-Technical-Note.pdf
+ */
+struct ft260_reset_work {
+ struct work_struct work;
+ struct usb_interface *usbif;
+};
+
+static void ft260_reset_and_rebind(struct work_struct *ws)
+{
+ struct ft260_reset_work *rw =
+ container_of(ws, struct ft260_reset_work, work);
+ struct usb_interface *usbif = rw->usbif;
+ struct usb_device *usbdev = interface_to_usbdev(usbif);
+ struct usb_host_config *actconfig;
+ int ret, i, attempt;
+
+ /*
+ * Retry the device lock for up to ~10 seconds. The lock is held
+ * by hub_event for the duration of device enumeration; with the
+ * fast-fail responsiveness check in probe, both interfaces should
+ * abort within ~1-2 seconds, after which the lock becomes free.
+ * Each usb_lock_device_for_reset() attempt already polls for up to
+ * one second internally.
+ */
+ for (attempt = 0; attempt < 10; attempt++) {
+ ret = usb_lock_device_for_reset(usbdev, NULL);
+ if (ret >= 0)
+ break;
+ if (ret == -ENODEV || ret == -EHOSTUNREACH) {
+ dev_dbg(&usbif->dev,
+ "device gone before reset (%d), abort\n", ret);
+ goto out;
+ }
+ /* -EBUSY: someone else holds the lock; retry. */
+ }
+ if (ret < 0) {
+ dev_err(&usbif->dev,
+ "failed to acquire USB device lock for reset after %d attempts: %d\n",
+ attempt, ret);
+ goto out;
+ }
+
+ ret = usb_reset_device(usbdev);
+ if (ret < 0) {
+ dev_err(&usbif->dev, "USB reset failed: %d\n", ret);
+ usb_unlock_device(usbdev);
+ goto out;
+ }
+
+ /*
+ * usb_reset_device() keeps usbhid bound (its pre_reset/post_reset
+ * both return 0) and does not re-trigger HID-level driver probing.
+ * Unbind and rebind all USB interfaces to force usbhid to destroy
+ * stale HID devices and create new ones, which triggers fresh
+ * ft260_probe() calls.
+ */
+ actconfig = usbdev->actconfig;
+ for (i = 0; actconfig && i < actconfig->desc.bNumInterfaces; i++) {
+ struct usb_interface *intf = actconfig->interface[i];
+
+ if (intf && intf->dev.driver)
+ device_release_driver(&intf->dev);
+ }
+ for (i = 0; actconfig && i < actconfig->desc.bNumInterfaces; i++) {
+ struct usb_interface *intf = actconfig->interface[i];
+
+ if (!intf)
+ continue;
+ ret = device_attach(&intf->dev);
+ if (ret < 0)
+ dev_err(&intf->dev,
+ "failed to rebind USB interface: %d\n", ret);
+ }
+
+ usb_unlock_device(usbdev);
+out:
+ usb_put_intf(usbif);
+ kfree(rw);
+}
+
+static int ft260_schedule_reset(struct usb_interface *usbif)
+{
+ struct ft260_reset_work *rw;
+
+ rw = kmalloc_obj(*rw, GFP_KERNEL);
+ if (!rw)
+ return -ENOMEM;
+
+ usb_get_intf(usbif);
+ rw->usbif = usbif;
+ INIT_WORK(&rw->work, ft260_reset_and_rebind);
+ schedule_work(&rw->work);
+
+ return 0;
+}
+
+/*
+ * Detect whether the device's interrupt IN endpoint is in the STALL
+ * state described by TN_189. GET_STATUS is unreliable on the FT260
+ * (returns 0 even when halted, confirmed by FTDI with a USB analyzer
+ * trace), so observe the STALL handshake by attempting an actual
+ * interrupt IN transfer. The host controller returns -EPIPE when it
+ * receives a STALL handshake.
+ *
+ * Must be called before hid_hw_open() so it does not race against
+ * usbhid's own interrupt IN URB.
+ */
+static int ft260_check_intr_ep_health(struct hid_device *hdev)
+{
+ struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
+ struct usb_device *usbdev = interface_to_usbdev(usbif);
+ struct usb_host_interface *iface_desc = usbif->cur_altsetting;
+ struct usb_endpoint_descriptor *ep = NULL;
+ unsigned int pipe;
+ u8 *buf;
+ int ret, actual_length, i;
+
+ for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
+ if (usb_endpoint_is_int_in(&iface_desc->endpoint[i].desc)) {
+ ep = &iface_desc->endpoint[i].desc;
+ break;
+ }
+ }
+ if (!ep)
+ return 0;
+
+ buf = kmalloc(FT260_REPORT_MAX_LEN, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ pipe = usb_rcvintpipe(usbdev, ep->bEndpointAddress);
+ ret = usb_interrupt_msg(usbdev, pipe, buf, FT260_REPORT_MAX_LEN,
+ &actual_length, 100);
+ kfree(buf);
+
+ if (ret == -EPIPE) {
+ hid_warn(hdev,
+ "interrupt IN ep %#x halted (TN_189 errata), scheduling USB reset and rebind\n",
+ ep->bEndpointAddress);
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+/*
+ * Quick check that the device responds to a standard control transfer.
+ * When the FT260 is in the buggy post-enumeration state, control
+ * transfers initiated by later probe stages (chip version retrieval,
+ * UART/I2C configuration, etc.) can hang for very long periods,
+ * starving the usb_hub_wq workqueue and preventing the reset work
+ * from acquiring the device lock.
+ *
+ * Issue USB_REQ_GET_STATUS to the device (any compliant USB device
+ * must answer immediately) with a short explicit timeout. If it
+ * fails, treat the device as broken and bail out before reaching
+ * anything that can block.
+ *
+ * The interrupt-endpoint health check above only catches STALLs on
+ * the interrupt IN path; this check catches the broader broken state
+ * that affects the other interface even when its interrupt endpoint
+ * happens to look healthy.
+ */
+static int ft260_check_dev_responsive(struct hid_device *hdev)
+{
+ struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
+ struct usb_device *usbdev = interface_to_usbdev(usbif);
+ __le16 *status;
+ int ret;
+
+ status = kmalloc_obj(*status, GFP_KERNEL);
+ if (!status)
+ return -ENOMEM;
+
+ ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
+ USB_REQ_GET_STATUS,
+ USB_DIR_IN | USB_RECIP_DEVICE,
+ 0, 0, status, sizeof(*status), 500);
+ kfree(status);
+
+ if (ret < 0) {
+ hid_warn(hdev,
+ "device unresponsive to GET_STATUS (%d), suspected TN_189 errata, scheduling USB reset and rebind\n",
+ ret);
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
+ struct usb_interface *usbif;
struct ft260_get_chip_version_report version;
struct ft260_get_system_status_report cfg;
int ret;
if (!hid_is_usb(hdev))
return -EINVAL;
+
+ usbif = to_usb_interface(hdev->dev.parent);
/*
* We cannot use devm_kzalloc here because the port has to survive
* until destroy function call.
@@ -2392,6 +2604,23 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
goto hid_fail;
}
+ /*
+ * TN_189 errata workaround: bail out fast on a broken device so
+ * that hub_event releases the device lock quickly, allowing the
+ * scheduled reset work to acquire it and recover the device.
+ */
+ ret = ft260_check_intr_ep_health(hdev);
+ if (ret) {
+ ft260_schedule_reset(usbif);
+ goto err_hid_stop;
+ }
+
+ ret = ft260_check_dev_responsive(hdev);
+ if (ret) {
+ ft260_schedule_reset(usbif);
+ goto err_hid_stop;
+ }
+
ret = hid_hw_open(hdev);
if (ret) {
hid_err(hdev, "failed to open HID HW\n");
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 13/13] HID: ft260: i2c: abort in-flight transfers with STOP before reset
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (11 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 12/13] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration Michael Zaidman
@ 2026-08-22 21:39 ` Michael Zaidman
2026-08-25 8:21 ` [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Linus Walleij
13 siblings, 0 replies; 21+ messages in thread
From: Michael Zaidman @ 2026-08-22 21:39 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Linus Walleij, Bartosz Golaszewski, Germain Hebert, Rio Liu,
Bruno Giacomazzi, Christina Quast, linux-input, linux-gpio,
linux-i2c, linux-kernel, Michael Zaidman
A transfer that ends without STOP leaves the FT260 I2C controller
with BUS_BUSY set. I2C reset (AN_394 section 4.4.15) restarts the
master but, with the driver still bound, status stays 0x60
(CTRL_IDLE | BUS_BUSY) even when SCL and SDA are pulled up. The
next START then fails with 0x72 (arbitration lost + error) and
further resets do not recover; only a USB replug did.
Add ft260_i2c_abort(): a STOP-only write (flag 0x04, zero payload)
then FT260_SET_I2C_RESET. Use it wherever the host tears down an
open transaction:
- read wait timeout
- read status check failure after the HID report arrived
- write status poll failure after the report was sent
- SMBus block read with an invalid count (count phase has no STOP)
Leave ft260_i2c_reset() alone for HID output failure (the request
may never have reached the controller), sysfs i2c_reset, and probe.
Tested on a UMFT260EV1A with a 24LC512 at 0x51. Five 1 ms read
timeouts each followed by STOP+reset left bus_status 0x20; raising
the timeout to 25 ms on the same loaded module then read 16 bytes
successfully.
This matters more with the 25/75 ms completion timeout from
"HID: ft260: i2c: reduce driver module loading time", which makes
read timeouts more likely than mainline's 5000 ms wait.
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 43 ++++++++++++++++++++++++++++++++++-------
1 file changed, 36 insertions(+), 7 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 9ae688f6208f..a35a1af2d7e5 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -538,6 +538,33 @@ static int ft260_i2c_reset(struct hid_device *hdev)
return ret;
}
+static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
+ size_t len);
+
+/*
+ * STOP with no START and no payload. Used by ft260_i2c_abort() when
+ * tearing down an in-flight transfer: I2C reset alone can leave
+ * BUS_BUSY set even when SCL/SDA are idle, and the next START then
+ * fails with arbitration lost.
+ */
+static int ft260_i2c_stop(struct hid_device *hdev, u8 addr)
+{
+ u8 buf[4] = {
+ FT260_I2C_REPORT_MIN,
+ addr,
+ FT260_FLAG_STOP,
+ 0,
+ };
+
+ return ft260_hid_output_report(hdev, buf, sizeof(buf));
+}
+
+static int ft260_i2c_abort(struct hid_device *hdev, u8 addr)
+{
+ ft260_i2c_stop(hdev, addr);
+ return ft260_i2c_reset(hdev);
+}
+
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
{
struct hid_device *hdev = dev->hdev;
@@ -607,7 +634,8 @@ static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
- u8 *data, int len, u8 bus_busy)
+ u8 *data, int len, u8 addr,
+ u8 bus_busy)
{
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
@@ -636,7 +664,7 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
if (ret == 0)
return 0;
- ft260_i2c_reset(hdev);
+ ft260_i2c_abort(hdev, addr);
return -EIO;
}
@@ -675,7 +703,8 @@ static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
- wr_len + 4, bus_busy);
+ wr_len + 4, addr,
+ bus_busy);
if (ret < 0) {
ft260_dbg("%s: failed with %d\n", __func__, ret);
return ret;
@@ -716,7 +745,7 @@ static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
- ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len,
+ ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len, addr,
(flag & FT260_FLAG_STOP) ?
FT260_I2C_STATUS_BUS_BUSY : 0);
if (ret < 0)
@@ -784,7 +813,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
timeout_jiffies = msecs_to_jiffies(timeout);
if (!wait_for_completion_timeout(&dev->wait, timeout_jiffies)) {
ret = -ETIMEDOUT;
- ft260_i2c_reset(hdev);
+ ft260_i2c_abort(hdev, addr);
goto ft260_i2c_read_exit;
}
@@ -798,7 +827,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
ret = ft260_xfer_status(dev, bus_busy);
if (ret < 0) {
ret = -EIO;
- ft260_i2c_reset(hdev);
+ ft260_i2c_abort(hdev, addr);
goto ft260_i2c_read_exit;
}
@@ -982,7 +1011,7 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
hid_warn(hdev,
"smbus block read: invalid count %u from slave 0x%02x\n",
count, addr);
- ft260_i2c_reset(hdev);
+ ft260_i2c_abort(hdev, addr);
ret = -EPROTO;
goto smbus_exit;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH 03/13] HID: ft260: add GPIO support on top of UART
2026-08-22 21:39 ` [PATCH 03/13] HID: ft260: add GPIO support on top of UART Michael Zaidman
@ 2026-08-25 7:44 ` Linus Walleij
0 siblings, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 7:44 UTC (permalink / raw)
To: Michael Zaidman
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel
Hi Michael,
thanks for your patch!
It seems this patch title should just be
"HID: ft260: add GPIO support"
the "on top of UART" part seems to be more about
which order you did the patches and that is about
development process, not technical content, we don't
put that into Subject.
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> Add gpiochip support for the FT260 multifunctional pins, including
> GPIO2/GPIOA/GPIOG function selection via sysfs, and enable the
> available GPIOs according to the active UART mode.
>
> This supersedes the earlier pre-UART GPIO series:
> https://lore.kernel.org/lkml/20230211115752.26276-2-michael.zaidman@gmail.com/
>
> The gpio_chip.set callback returns int to match the current gpiolib
> API, based on the adaptation originally contributed by Rio Liu
> <rio@r26.me>.
>
> Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
You probably want:
#include <linus/bits.h>
(...)
> static int ft260_debug = 1;
> @@ -57,6 +58,11 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
> #define FT260_RD_DATA_MAX (180)
> #define FT260_WR_I2C_DATA_MAX (60)
> #define FT260_WR_UART_DATA_MAX (62)
> +#define FT260_GPIOCHIP "ft260_gpio"
> +#define FT260_GPIO_MAX (6)
> +#define FT260_GPIO_EX_MAX (8)
So 6 GPIOs is "max" and then there are extended GPIOs and then there is
a new max which is 8?
I would give these defines some easier to understand name such as:
#define FT260_BASE_GPIOS 6
#define FT260_EXTENDED_GPIOS 8
> +#define FT260_GPIO_TOTAL (FT260_GPIO_MAX + FT260_GPIO_EX_MAX)
> +#define FT260_GPIO_MASK (~(0xffff << FT260_GPIO_TOTAL))
This macro seems to be using funky arithmetic with bits
rolling out on the left.
What about just:
#define FT260_GPIO_MASK GENMASK(FT260_GPIO_TOTAL, 0)
> +/* GPIO offsets */
> +enum {
Is this *really* an enum? I feel an enum should be something like
consecutive or sparse integer ranges.
What about just using #define for those.
> + FT260_GPIO_0 = (1 << 0),
> + FT260_GPIO_1 = (1 << 1),
#define FT259_GPIO_0 BIT(0)
#define FT259_GPIO_1 BIT(1)
(...)
> + FT260_GPIO_A = (1 << (FT260_GPIO_MAX + 0)),
> + FT260_GPIO_B = (1 << (FT260_GPIO_MAX + 1)),
Those are the extended GPIOs I guess? Mention
that in the macro name? Also you know the bit number.
Do this:
#define FT260_GPIO_EXT_A BIT(7)
#define FT260_GPIO_EXT_B BIT(8)
> +/* GPIO groups */
> +enum {
> + FT260_GPIO_WAKEUP = (FT260_GPIO_3),
> + FT260_GPIO_I2C_DEFAULT = (FT260_GPIO_0 | FT260_GPIO_1),
> + FT260_GPIO_UART_RX_TX = (FT260_GPIO_C | FT260_GPIO_D),
> + FT260_GPIO_UART_DCD_RI = (FT260_GPIO_4 | FT260_GPIO_5),
> + FT260_GPIO_UART_RTS_CTS = (FT260_GPIO_B | FT260_GPIO_E),
> + FT260_GPIO_UART_DTR_DSR = (FT260_GPIO_F | FT260_GPIO_H),
> + FT260_GPIO_UART_MODE_0_SET = (FT260_GPIO_UART_RX_TX |
> + FT260_GPIO_UART_DCD_RI |
> + FT260_GPIO_UART_RTS_CTS |
> + FT260_GPIO_UART_DTR_DSR),
> + FT260_GPIO_UART_MODE_1_SET = (FT260_GPIO_UART_DTR_DSR),
> + FT260_GPIO_UART_MODE_2_SET = (FT260_GPIO_UART_RTS_CTS),
> + FT260_GPIO_UART_MODE_3_SET = (FT260_GPIO_UART_RTS_CTS |
> + FT260_GPIO_UART_DTR_DSR),
> + FT260_GPIO_UART_MODE_4_SET = (FT260_GPIO_UART_MODE_3_SET),
> + FT260_GPIO_UART_DEFAULT = (FT260_GPIO_UART_MODE_0_SET),
> + FT260_GPIO_UART_MODE_1_CLR = (FT260_GPIO_UART_RX_TX |
> + FT260_GPIO_UART_RTS_CTS),
> + FT260_GPIO_UART_MODE_2_CLR = (FT260_GPIO_UART_RX_TX |
> + FT260_GPIO_UART_DTR_DSR),
> + FT260_GPIO_UART_MODE_3_CLR = (FT260_GPIO_UART_RX_TX),
> + FT260_GPIO_UART_MODES = (5),
Use #defines for all of this as well.
> +struct ft260_gpio_state {
> + u8 vals; /* GPIO[0-5] values in bits 0 - 5 */
> + u8 dirs; /* GPIO[0-5] directions, 0 - in, 1 - out */
> + u8 ex_vals; /* GPIO[A-H] values in bits 0 - 7 */
> + u8 ex_dirs; /* GPIO[A-H] directions, 0 - in, 1 - out */
> +} __packed;
So instead of creating your own cache of all registers,
have you considered just using regmap for this?
Regmap is essentially a register cache.
> /* UART interface configuration */
> enum {
> - FT260_CFG_FLOW_CTRL_OFF = 0x00,
> - FT260_CFG_FLOW_CTRL_RTS_CTS = 0x01,
> - FT260_CFG_FLOW_CTRL_DTR_DSR = 0x02,
> - FT260_CFG_FLOW_CTRL_XON_XOFF = 0x03,
> - FT260_CFG_FLOW_CTRL_NONE = 0x04,
> + FT260_UART_CFG_FLOW_CTRL_OFF = 0x00,
> + FT260_UART_CFG_FLOW_CTRL_RTS_CTS = 0x01,
> + FT260_UART_CFG_FLOW_CTRL_DTR_DSR = 0x02,
> + FT260_UART_CFG_FLOW_CTRL_XON_XOFF = 0x03,
> + FT260_UART_CFG_FLOW_CTRL_NONE = 0x04,
This looks like names you could have already assigned
in the UART patch and then the diff would be less noisy.
> - FT260_CFG_DATA_BITS_7 = 0x07,
> - FT260_CFG_DATA_BITS_8 = 0x08,
> + FT260_UART_CFG_DATA_BITS_7 = 0x07,
> + FT260_UART_CFG_DATA_BITS_8 = 0x08,
Dito.
> -#define FT260_UART_EN_PW_SAVE_BAUD (4800)
> +#define FT260_UART_EN_PW_SAVE_BAUD (4800)
Dito.
> +static void ft260_gpio_en_set(struct ft260_device *dev, u16 bitmap)
> +{
> + dev->gpio_en |= bitmap & FT260_GPIO_MASK;
> +}
> +
> +static void ft260_gpio_en_clr(struct ft260_device *dev, u16 bitmap)
> +{
> + dev->gpio_en &= ~bitmap & FT260_GPIO_MASK;
> +}
Linux has bitmap manipulation functions in
<linus/bitmap.h>, use these instead of reinventing the wheel.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 01/13] HID: ft260: add serial driver
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
@ 2026-08-25 7:49 ` Linus Walleij
2026-08-25 8:12 ` Linus Walleij
1 sibling, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 7:49 UTC (permalink / raw)
To: Michael Zaidman
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel, Daniel Beer
Hi Michael, Christina,
thanks for your patch!
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> From: Christina Quast <contact@christina-quast.de>
>
> Add a TTY serial interface (/dev/ttyFTx) for FT260 USB HID devices,
> providing baud rate configuration, data transfer, and termios settings
> without requiring userspace HID report formatting.
>
> The FT260 exposes I2C and UART through separate HID interfaces. Only
> I2C was supported previously; UART required FTDI hidraw example code.
>
> This is a port of the v4 patch, adapted to the I2C hardening that has
> since been merged upstream: input-report length validation, the
> read_buf/raw_event race fix with read_lock, and SMBus block-read
> START/STOP handling. HID_FT260 now depends on TTY because the UART code
> is part of this driver.
>
> Routing UART reports through ft260_raw_event() also made v4 fall through
> the report dispatch after an I2C input report had already been consumed,
> so every I2C input report was additionally reported as unhandled. This
> regression is fixed here.
>
> Ported from:
> https://lore.kernel.org/all/20231218093153.192268-1-contact@christina-quast.de/
>
> The follow-up v5 series stalled without being applied:
> https://lore.kernel.org/linux-input/20240216-ft260_review5-v5-1-36db44673ac7@christina-quast.de/
>
> Signed-off-by: Christina Quast <contact@christina-quast.de>
> Co-developed-by: Daniel Beer <daniel.beer@igorinstitute.com>
> Signed-off-by: Daniel Beer <daniel.beer@igorinstitute.com>
> Co-developed-by: Michael Zaidman <michael.zaidman@gmail.com>
> Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
(...)
> +/* UART interface configuration */
> +enum {
> + FT260_CFG_FLOW_CTRL_OFF = 0x00,
> + FT260_CFG_FLOW_CTRL_RTS_CTS = 0x01,
> + FT260_CFG_FLOW_CTRL_DTR_DSR = 0x02,
> + FT260_CFG_FLOW_CTRL_XON_XOFF = 0x03,
> + FT260_CFG_FLOW_CTRL_NONE = 0x04,
> +
> + FT260_CFG_DATA_BITS_7 = 0x07,
> + FT260_CFG_DATA_BITS_8 = 0x08,
> +
> + FT260_CFG_PAR_NO = 0x00,
> + FT260_CFG_PAR_ODD = 0x01,
> + FT260_CFG_PAR_EVEN = 0x02,
> + FT260_CFG_PAR_HIGH = 0x03,
> + FT260_CFG_PAR_LOW = 0x04,
> +
> + FT260_CFG_STOP_ONE_BIT = 0x00,
> + FT260_CFG_STOP_TWO_BIT = 0x02,
> +
> + FT260_CFG_BREAKING_NO = 0x00,
> + FT260_CFG_BEAKING_YES = 0x01,
> +
> + FT260_CFG_BAUD_MIN = 1200,
> + FT260_CFG_BAUD_MAX = 12000000,
> +};
What kind of "enum" is this that re-assigns 0x00, 0x01 etc to
several enumerators?
Either this is four different enums or you should just do a
series of #defines instead if they are all really coming from
the same place, because using an enum like this does not
make sense: it's like numering pears, apples, oranges
and peaches in the same enum or something.
Also use the UART_ infix as mentioned in the GPIO patch
so you don't need to immediately change it in the next
patch.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl
2026-08-22 21:39 ` [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
@ 2026-08-25 8:08 ` Linus Walleij
0 siblings, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 8:08 UTC (permalink / raw)
To: Michael Zaidman
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel
Hi Michael,
thanks for your patch!
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> Add TIOCMGET/TIOCMSET (and thus TIOCMBIS/TIOCMBIC) support so userspace
> can drive DTR/RTS when the modem pins are in GPIO mode. This enables
> programming an ESP32 over the FT260 with esptool, which resets the chip
> via those lines.
>
> Also key GPIO sysfs registration off chip_mode rather than USB interface
> number 0, and include DCD/RI in UART mode 3/4 GPIO sets so the modem
> mask matches the pins esptool needs.
>
> Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
I'm not the authortative expert on modem control using GPIO,
but what I think you should do is to
select GPIOLIB
select SERIAL_MCTRL_GPIO
On Kconfig, so that gpiolib is always available and you can use
the generic modem control helpers for modem control over GPIO.
This can be a bit delicate in this case since the gpiochip that you
use for mctrl is also registered in this driver, so you need to
register the gpiochip *first*, then add a look-up table for the
GPIOs, then register this modem control.
I don't think this has been done before for this type of device
so it could be a bit tricky, but it is probably the right thing to
do.
#include <linux/gpio/machine.h>
#include <linux/gpio/consumer.h>
Then look in e.g. drivers/mfd/sm501.c which is an
MFD device that register a gpiochip and then consume
GPIOs from itself. In the function
sm501_register_gpio_i2c_instance() this driver registers
a table of dynamic gpios associated with a separate subdevice.
A separate subdevice probably isn't needed but
something like this will be needed, albeit with the gpiochip
spawn off the main device for FT260.
The core idea is that the serial modem control should look
up the GPIOs from its own gpiochip and use the MCTRL
library helpers, then this should result in very little and
compact code that is easy to read.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 01/13] HID: ft260: add serial driver
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
2026-08-25 7:49 ` Linus Walleij
@ 2026-08-25 8:12 ` Linus Walleij
1 sibling, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 8:12 UTC (permalink / raw)
To: Michael Zaidman, Greg Kroah-Hartman, Jiri Slaby, linux-serial
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel, Daniel Beer
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
>
> From: Christina Quast <contact@christina-quast.de>
>
> Add a TTY serial interface (/dev/ttyFTx) for FT260 USB HID devices,
> providing baud rate configuration, data transfer, and termios settings
> without requiring userspace HID report formatting.
You need to CC the TTY maintainers and linux-serial on patches like this,
just keep in mind when resending, thanks!
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface
2026-08-22 21:39 ` [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface Michael Zaidman
@ 2026-08-25 8:13 ` Linus Walleij
0 siblings, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 8:13 UTC (permalink / raw)
To: Michael Zaidman
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel
Hi Michael,
thanks for your patch!
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> Split the single sysfs attribute group into per-interface groups based
> on chip mode (DCNF0/DCNF1):
>
> - I2C-only (mode 1): full I2C + GPIO attributes on the I2C HID iface
> - Dual mode (0/3): I2C-facing attrs on the I2C iface; GPIO and UART
> attrs on the UART iface
> - UART-only (mode 2): GPIO and UART attrs on the UART iface
>
> Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
Drop "gpio" from the subject of this patch, it's all about sysfs
nothung else.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity
2026-08-22 21:39 ` [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity Michael Zaidman
@ 2026-08-25 8:16 ` Linus Walleij
0 siblings, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 8:16 UTC (permalink / raw)
To: Michael Zaidman
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel
Hi Michael,
thanks for your patch!
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> From: Rio Liu <rio@r26.me>
>
> The FT260 modem control GPIOs for RTS, CTS, DTR, and DSR are active-low.
> Invert the TIOCMGET sense and the TIOCMSET drive levels for those pins
> so userspace ioctl state matches the electrical lines. Leave DCD and RI
> unchanged.
>
> Signed-off-by: Rio Liu <rio@r26.me>
> Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
Isn't this just fixing a patch earlier in the same series?
Just fold it in with the patch adding this.
Note: GPIOlib has it's own polarity inversion handling, when
you rewrite the serial modem control to use the helpers,
the gpio table will include a flag for polarity inversion, and
then you just put it there.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
` (12 preceding siblings ...)
2026-08-22 21:39 ` [PATCH 13/13] HID: ft260: i2c: abort in-flight transfers with STOP before reset Michael Zaidman
@ 2026-08-25 8:21 ` Linus Walleij
13 siblings, 0 replies; 21+ messages in thread
From: Linus Walleij @ 2026-08-25 8:21 UTC (permalink / raw)
To: Michael Zaidman, Lee Jones
Cc: Jiri Kosina, Benjamin Tissoires, Bartosz Golaszewski,
Germain Hebert, Rio Liu, Bruno Giacomazzi, Christina Quast,
linux-input, linux-gpio, linux-i2c, linux-kernel
Hi Michael,
thanks for your patch!
On Sat, Aug 22, 2026 at 11:40 PM Michael Zaidman
<michael.zaidman@gmail.com> wrote:
> Mainline supports only the FT260's I2C bridge. This series adds the
> UART/TTY interface and GPIO on top of it, and carries the I2C fixes made
> alongside them - two years of out-of-tree work [1], now replayed upstream.
(...)
> HID: ft260: add serial driver
(...)
> HID: ft260: add GPIO support on top of UART
This comment isn't pertaining to this one device in particular.
I'm not a HID maintainer but this trend to make HID devices spawn
serial and GPIO devices sort of turns HID into the new MFD, but
I suppose Jiri and Lee has discussed this phenomenon in the past?
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 21+ messages in thread
end of thread, other threads:[~2026-08-25 8:21 UTC | newest]
Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-22 21:39 [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
2026-08-22 21:39 ` [PATCH 01/13] HID: ft260: add serial driver Michael Zaidman
2026-08-25 7:49 ` Linus Walleij
2026-08-25 8:12 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 02/13] HID: ft260: uart: bring-up fixes Michael Zaidman
2026-08-22 21:39 ` [PATCH 03/13] HID: ft260: add GPIO support on top of UART Michael Zaidman
2026-08-25 7:44 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 04/13] HID: ft260: i2c: reduce driver module loading time Michael Zaidman
2026-08-22 21:39 ` [PATCH 05/13] HID: ft260: i2c: silence sysfs store big-numbers Michael Zaidman
2026-08-22 21:39 ` [PATCH 06/13] HID: ft260: i2c: reduce bus-error message severity Michael Zaidman
2026-08-22 21:39 ` [PATCH 07/13] HID: ft260: uart: enable flow control Michael Zaidman
2026-08-22 21:39 ` [PATCH 08/13] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
2026-08-25 8:08 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 09/13] HID: ft260: gpio: group sysfs attrs per HID interface Michael Zaidman
2026-08-25 8:13 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 10/13] HID: ft260: uart: fix active-low RTS/CTS/DTR/DSR polarity Michael Zaidman
2026-08-25 8:16 ` Linus Walleij
2026-08-22 21:39 ` [PATCH 11/13] HID: ft260: i2c: fix large write transaction failure Michael Zaidman
2026-08-22 21:39 ` [PATCH 12/13] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration Michael Zaidman
2026-08-22 21:39 ` [PATCH 13/13] HID: ft260: i2c: abort in-flight transfers with STOP before reset Michael Zaidman
2026-08-25 8:21 ` [PATCH 00/13] HID: ft260: add UART and GPIO support, plus I2C fixes Linus Walleij
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).