mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH] i2c: core: default read events to 0xff in i2c_slave_event()
@ 2026-12-04  5:29 Eric Yun Lin Chiang
  0 siblings, 0 replies; only message in thread
From: Eric Yun Lin Chiang @ 2026-12-04  5:29 UTC (permalink / raw)
  To: Andi Shyti; +Cc: linux-i2c, linux-kernel

After I2C_SLAVE_READ_REQUESTED and I2C_SLAVE_READ_PROCESSED the bus
driver sends whatever *val holds. If the backend does not set it, that
is whatever the bus driver had there: for i2c-aspeed and i2c-npcm7xx an
uninitialised stack byte.

Backends have missed this more than once. 2a14e91b6d76 ("mctp i2c:
initialise event handler read bytes") fixed it for mctp-i2c, but an
early return in the same callback still skips it, and ipmb_dev_int has
no read case at all. Each needs its own fix, and the next backend can
make the same mistake.

Set *val to 0xff, the idle level of the bus, before the callback for
the two read events, so a backend that leaves it unchanged sends 0xff.
A backend that sets a value is unaffected, and write events are not
touched. Update slave-interface.rst to say so.

Signed-off-by: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
---
Sending this as RFC because I am not sure the core is the right place:
it hides the mistake rather than flagging it, and some may prefer to
keep fixing backends one by one. The two backend fixes are on the list
and are useful for stable either way:
https://lore.kernel.org/all/20261203063440.3753378-1-ericyunlinchiang@gmail.com/
https://lore.kernel.org/all/20261204024210.3013397-1-ericyunlinchiang@gmail.com/

Tested: a local KUnit test (UML, KASAN) on i2c_slave_event() with a
backend that ignores read events: *val preset to 0xa5 stays 0xa5
without this patch and becomes 0xff with it; a backend that sets 0x5a
still gets 0x5a sent, and WRITE_RECEIVED data reaches the backend
unchanged. Also built for ARM (aspeed_g5_defconfig with I2C_SLAVE and
MCTP_TRANSPORT_I2C) and booted on QEMU ast2600-evb with an mctp-i2c
target bound, with no new warnings. Not tested on hardware.

This change was prepared with the help of an AI coding assistant
(Claude); I reviewed it and stand behind it.

 Documentation/i2c/slave-interface.rst |  7 +++++--
 drivers/i2c/i2c-core-slave.c          | 12 +++++++++++-
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/Documentation/i2c/slave-interface.rst b/Documentation/i2c/slave-interface.rst
index 3f0d320bc..0d38f82e4 100644
--- a/Documentation/i2c/slave-interface.rst
+++ b/Documentation/i2c/slave-interface.rst
@@ -84,7 +84,9 @@ a retry of the transmission.
 
 * I2C_SLAVE_READ_REQUESTED (mandatory)
 
-  'val': backend returns first byte to be sent
+  'val': backend returns first byte to be sent. The core sets it to 0xff
+  before calling the backend, so 0xff is sent if the backend leaves it
+  unchanged.
 
   'ret': always 0
 
@@ -104,7 +106,8 @@ should be nacked.
 
 * I2C_SLAVE_READ_PROCESSED (mandatory)
 
-  'val': backend returns next byte to be sent
+  'val': backend returns next byte to be sent. As for
+  I2C_SLAVE_READ_REQUESTED, the core sets it to 0xff first.
 
   'ret': always 0
 
diff --git a/drivers/i2c/i2c-core-slave.c b/drivers/i2c/i2c-core-slave.c
index bebb7ba67..6fb34e388 100644
--- a/drivers/i2c/i2c-core-slave.c
+++ b/drivers/i2c/i2c-core-slave.c
@@ -86,7 +86,17 @@ EXPORT_SYMBOL_GPL(i2c_slave_unregister);
 int i2c_slave_event(struct i2c_client *client,
 		    enum i2c_slave_event event, u8 *val)
 {
-	int ret = client->slave_cb(client, event, val);
+	int ret;
+
+	/*
+	 * The bus driver sends whatever *val holds after a read event. Give it a
+	 * defined value, the idle level of the bus, in case the backend does not
+	 * set one.
+	 */
+	if (event == I2C_SLAVE_READ_REQUESTED || event == I2C_SLAVE_READ_PROCESSED)
+		*val = 0xff;
+
+	ret = client->slave_cb(client, event, val);
 
 	if (trace_i2c_slave_enabled())
 		trace_call__i2c_slave(client, event, val, ret);
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04  5:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  5:29 [RFC PATCH] i2c: core: default read events to 0xff in i2c_slave_event() Eric Yun Lin Chiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®