From: Michael Zaidman <michael.zaidman@gmail.com>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: Linus Walleij <linusw@kernel.org>,
Bartosz Golaszewski <brgl@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>,
Raman Varabets <kernel-linux-20260610-80b7ab08@raman.v1.sg>,
Germain Hebert <germain.hebert@ca.abb.com>,
Christina Quast <contact@christina-quast.de>,
Rio Liu <rio@r26.me>, Bruno Giacomazzi <brunoceg1@gmail.com>,
linux-input@vger.kernel.org, linux-serial@vger.kernel.org,
linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org,
linux-kernel@vger.kernel.org,
Michael Zaidman <michael.zaidman@gmail.com>
Subject: [PATCH v2 15/15] HID: ft260: i2c: recover a bus held by a slave
Date: Sat, 31 Oct 2026 00:36:37 +0200 [thread overview]
Message-ID: <20261030223637.86907-16-michael.zaidman@gmail.com> (raw)
In-Reply-To: <20261030223637.86907-1-michael.zaidman@gmail.com>
An SMBus block read takes the first byte the slave sends as the block
length. An EEPROM has no length byte, it answers with stored data, and
ft260_smbus_xfer() rejects a value above I2C_SMBUS_BLOCK_MAX. That
count phase carries no STOP, so the slave keeps SDA low while it waits
for the next clock. FT260_SET_I2C_RESET restarts the controller but
does not clock the bus, the chip cannot put a STOP on the line, and
every later START ends in arbitration lost.
A 24c02 holds 0x68 at offset 0x80, so on that bus
i2cget -y 0 0x50 0x80 s
ft260 0003:0403:6030.0011: smbus block read: invalid count 104 from slave 0x50
killed the bus: every transfer after it failed with bus_status 0x72.
The sysfs i2c_reset, unbind and bind of the HID interface, and rmmod
followed by insmod all left the status at 0x72.
Nine SCL pulses free the slave. Switching the I2C controller off gives
GPIO0 and GPIO1 back their GPIO function, so i2c_generic_scl_recovery()
can clock the line and make a STOP. ft260_i2c_abort() reads the status
after the reset and calls i2c_recover_bus() when BUS_BUSY is still set.
The error bit is no use there: the chip reports 0x60 right after the
reset and only shows 0x72 at the next START.
ft260_gpio_pin_set() and ft260_gpio_read_all() are the bodies of
ft260_gpio_direction_set() and ft260_gpio_get_all(), split out because
the recovery needs them without the gpio_en check, SCL and SDA are no
GPIOs while I2C runs, and without dev->lock, which the abort path
already holds.
With the patch the same i2cget still fails with -EPROTO and the next
i2ctransfer succeeds. A full i2cdetect scan of 112 addresses logs no
recovery, so an address NAK does not take this path.
Fixes: 2d68d67151eb ("HID: ft260: fix SMBus block read protocol handling")
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
---
drivers/hid/hid-ft260.c | 193 +++++++++++++++++++++++++++++++++-------
1 file changed, 162 insertions(+), 31 deletions(-)
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index cd1117c170e1..faa1e12cd2d5 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -188,6 +188,8 @@ enum {
FT260_GPIO_DIRECTION = 0x01,
FT260_GPIO_DIR_INPUT = 0x00,
FT260_GPIO_DIR_OUTPUT = 0x01,
+ FT260_GPIO_SCL_OFFSET = 0, /* GPIO0 */
+ FT260_GPIO_SDA_OFFSET = 1, /* GPIO1 */
};
/* GPIO bit masks */
@@ -554,6 +556,22 @@ static int ft260_i2c_reset(struct hid_device *hdev)
return 0;
}
+/*
+ * Enable or disable the I2C controller. With the controller disabled
+ * GPIO0 and GPIO1 carry their GPIO function instead of SCL and SDA.
+ */
+static int ft260_i2c_set_mode(struct hid_device *hdev, u8 enable)
+{
+ struct ft260_set_i2c_mode_report report;
+
+ report.report = FT260_SYSTEM_SETTINGS;
+ report.request = FT260_SET_I2C_MODE;
+ report.i2c_enable = enable;
+
+ return ft260_hid_feature_report_set(hdev, (u8 *)&report,
+ sizeof(report));
+}
+
static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
size_t len);
@@ -575,10 +593,46 @@ static int ft260_i2c_stop(struct hid_device *hdev, u8 addr)
return ft260_hid_output_report(hdev, buf, sizeof(buf));
}
-static int ft260_i2c_abort(struct hid_device *hdev, u8 addr)
+static int ft260_i2c_bus_status(struct ft260_device *dev)
{
- ft260_i2c_stop(hdev, addr);
- return ft260_i2c_reset(hdev);
+ struct ft260_get_i2c_status_report report;
+ int ret;
+
+ ret = ft260_hid_feature_report_get(dev->hdev, FT260_I2C_STATUS,
+ (u8 *)&report, sizeof(report));
+ if (ret < 0)
+ return ret;
+
+ return report.bus_status;
+}
+
+/*
+ * I2C reset does not clock the bus. A slave left in the middle of a
+ * byte keeps SDA low, the chip cannot put a STOP on the bus, and every
+ * later START fails with arbitration lost. The chip reports this as
+ * BUS_BUSY once the reset is done; only driving SCL as a GPIO frees the
+ * slave, so fall back to bus recovery.
+ */
+static int ft260_i2c_abort(struct ft260_device *dev, u8 addr)
+{
+ int ret;
+
+ ft260_i2c_stop(dev->hdev, addr);
+
+ ret = ft260_i2c_reset(dev->hdev);
+ if (ret < 0)
+ return ret;
+
+ ret = ft260_i2c_bus_status(dev);
+ if (ret < 0)
+ return ret;
+
+ if (ret & FT260_I2C_STATUS_BUS_BUSY) {
+ ft260_dbg("bus_status %#02x, recovering\n", ret);
+ return i2c_recover_bus(&dev->adap);
+ }
+
+ return 0;
}
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
@@ -680,7 +734,7 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,
if (ret == 0)
return 0;
- ft260_i2c_abort(hdev, addr);
+ ft260_i2c_abort(dev, addr);
return -EIO;
}
@@ -829,7 +883,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_abort(hdev, addr);
+ ft260_i2c_abort(dev, addr);
goto ft260_i2c_read_exit;
}
@@ -843,7 +897,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_abort(hdev, addr);
+ ft260_i2c_abort(dev, addr);
goto ft260_i2c_read_exit;
}
@@ -1027,7 +1081,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_abort(hdev, addr);
+ ft260_i2c_abort(dev, addr);
ret = -EPROTO;
goto smbus_exit;
}
@@ -1237,34 +1291,23 @@ static int ft260_gpio_set(struct gpio_chip *gc, u32 offset, int value)
return ret;
}
-static int ft260_gpio_direction_set(struct gpio_chip *gc, u32 offset,
- int value, int direction)
+/*
+ * Set the direction and the value of one pin. Takes no lock and does
+ * not look at gpio_en, so the I2C bus recovery can drive SCL and SDA
+ * while it holds dev->lock and while the pins are not GPIOs.
+ */
+static int ft260_gpio_pin_set(struct ft260_device *dev, 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 & BIT(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;
+ return ret;
}
rep = (struct ft260_gpio_write_request_report *)&buf;
@@ -1298,12 +1341,36 @@ static int ft260_gpio_direction_set(struct gpio_chip *gc, u32 offset,
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;
+ return ret;
}
dev->gpio = rep->gpio;
- ret = 0;
-exit:
+ return 0;
+}
+
+static int ft260_gpio_direction_set(struct gpio_chip *gc, u32 offset,
+ int value, int direction)
+{
+ int ret;
+ 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 & BIT(offset))) {
+ hid_err(hdev, "%s: wrong pin function %d\n", __func__, offset);
+ ret = -EIO;
+ } else {
+ ret = ft260_gpio_pin_set(dev, offset, value, direction);
+ }
+
mutex_unlock(&dev->lock);
return ret;
}
@@ -1321,11 +1388,10 @@ static int ft260_gpio_direction_input(struct gpio_chip *gc, u32 offset)
FT260_GPIO_DIR_INPUT);
}
-static int ft260_gpio_get_all(struct gpio_chip *gc, int item)
+static int ft260_gpio_read_all(struct ft260_device *dev, 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));
@@ -1342,6 +1408,11 @@ static int ft260_gpio_get_all(struct gpio_chip *gc, int item)
return ret;
}
+static int ft260_gpio_get_all(struct gpio_chip *gc, int item)
+{
+ return ft260_gpio_read_all(gpiochip_get_data(gc), item);
+}
+
static int ft260_gpio_get_direction(struct gpio_chip *gc, u32 offset)
{
int ret = ft260_gpio_get_all(gc, FT260_GPIO_DIRECTION);
@@ -1360,6 +1431,65 @@ static int ft260_gpio_get(struct gpio_chip *gc, u32 offset)
return (ret >> offset) & 1;
}
+/*
+ * I2C bus recovery. Turning the I2C controller off gives GPIO0 and
+ * GPIO1 back their GPIO function, so i2c_generic_scl_recovery() can
+ * clock the slave out of the byte it holds and put a STOP on the bus.
+ * A pin is released by making it an input, the bus pull-ups then drive
+ * it high.
+ */
+static void ft260_i2c_set_pin(struct i2c_adapter *adap, u32 offset, int val)
+{
+ struct ft260_device *dev = i2c_get_adapdata(adap);
+
+ ft260_gpio_pin_set(dev, offset, 0,
+ val ? FT260_GPIO_DIR_INPUT : FT260_GPIO_DIR_OUTPUT);
+}
+
+static void ft260_i2c_set_scl(struct i2c_adapter *adap, int val)
+{
+ ft260_i2c_set_pin(adap, FT260_GPIO_SCL_OFFSET, val);
+}
+
+static void ft260_i2c_set_sda(struct i2c_adapter *adap, int val)
+{
+ ft260_i2c_set_pin(adap, FT260_GPIO_SDA_OFFSET, val);
+}
+
+static int ft260_i2c_get_scl(struct i2c_adapter *adap)
+{
+ struct ft260_device *dev = i2c_get_adapdata(adap);
+ int ret = ft260_gpio_read_all(dev, FT260_GPIO_VALUE);
+
+ if (ret < 0)
+ return ret;
+
+ return (ret >> FT260_GPIO_SCL_OFFSET) & 1;
+}
+
+static void ft260_i2c_prepare_recovery(struct i2c_adapter *adap)
+{
+ struct ft260_device *dev = i2c_get_adapdata(adap);
+
+ ft260_i2c_set_mode(dev->hdev, 0);
+}
+
+static void ft260_i2c_unprepare_recovery(struct i2c_adapter *adap)
+{
+ struct ft260_device *dev = i2c_get_adapdata(adap);
+
+ ft260_i2c_set_mode(dev->hdev, 1);
+}
+
+static struct i2c_bus_recovery_info ft260_i2c_recovery = {
+ .recover_bus = i2c_generic_scl_recovery,
+ .set_scl = ft260_i2c_set_scl,
+ .get_scl = ft260_i2c_get_scl,
+ .set_sda = ft260_i2c_set_sda,
+ .prepare_recovery = ft260_i2c_prepare_recovery,
+ .unprepare_recovery = ft260_i2c_unprepare_recovery,
+};
+
static int ft260_gpio_init(struct ft260_device *dev,
struct ft260_get_system_status_report *cfg)
{
@@ -2331,6 +2461,7 @@ static int ft260_i2c_probe(struct ft260_device *dev,
dev->adap.class = I2C_CLASS_HWMON;
dev->adap.algo = &ft260_i2c_algo;
dev->adap.quirks = &ft260_i2c_quirks;
+ dev->adap.bus_recovery_info = &ft260_i2c_recovery;
dev->adap.dev.parent = &hdev->dev;
snprintf(dev->adap.name, sizeof(dev->adap.name),
"FT260 usb-i2c bridge");
--
2.43.0
next prev parent reply other threads:[~2026-10-30 22:37 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 22:36 [PATCH v2 00/15] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 01/15] HID: ft260: add serial driver Michael Zaidman
2026-10-30 23:00 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 02/15] HID: ft260: uart: fix the " Michael Zaidman
2026-10-30 23:03 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 03/15] HID: ft260: add GPIO support Michael Zaidman
2026-10-30 22:53 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 04/15] HID: ft260: i2c: reduce driver module loading time Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 05/15] HID: ft260: i2c: silence sysfs store big-numbers Michael Zaidman
2026-10-30 22:47 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 06/15] HID: ft260: i2c: reduce bus-error message severity Michael Zaidman
2026-10-30 22:44 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 07/15] HID: ft260: uart: enable flow control Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 08/15] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
2026-10-30 22:55 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 09/15] HID: ft260: group sysfs attrs per HID interface Michael Zaidman
2026-10-30 22:48 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 10/15] HID: ft260: i2c: fix large write transaction failure Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 11/15] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration Michael Zaidman
2026-10-30 22:59 ` sashiko-bot
2026-10-30 22:36 ` [PATCH v2 12/15] HID: ft260: i2c: abort in-flight transfers with STOP before reset Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 13/15] HID: ft260: i2c: reject an I2C clock outside the supported range Michael Zaidman
2026-10-30 22:36 ` [PATCH v2 14/15] HID: ft260: i2c: fix i2c_reset sysfs store return value Michael Zaidman
2026-10-30 22:36 ` Michael Zaidman [this message]
2026-10-30 22:59 ` [PATCH v2 15/15] HID: ft260: i2c: recover a bus held by a slave sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030223637.86907-16-michael.zaidman@gmail.com \
--to=michael.zaidman@gmail.com \
--cc=bentiss@kernel.org \
--cc=brgl@kernel.org \
--cc=brunoceg1@gmail.com \
--cc=contact@christina-quast.de \
--cc=germain.hebert@ca.abb.com \
--cc=gregkh@linuxfoundation.org \
--cc=jikos@kernel.org \
--cc=jirislaby@kernel.org \
--cc=kernel-linux-20260610-80b7ab08@raman.v1.sg \
--cc=linusw@kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=rio@r26.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox