From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A99FA3E9C2B for ; Tue, 7 Jul 2026 15:17:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783437460; cv=none; b=BJm+y2C+SLYhS/lgvRCyERiswZFD4J9i1elf2Jgo0ST98Gvo96H5SQ7SChS+13Tnf286vGA3F205/1atxFCZKvHd5Az1hzd2UjNVL8tvqoFC3dLdVq8FPQRDSRSq31SujV8usYkoV15pg0DwO5J+j5R9toYaTjD39ufFc/6+Qu8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783437460; c=relaxed/simple; bh=Z8LhBsjXItHEMNM4TR044coWHllc6oZ/YAXNQH0osuE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o//BqmPANzvQlIAzV/8yuTKtEOm98jueUNYYWB5naAksdtOsp2n5G7wvEVcCJgty2V5mcqgmulLQzTWPv1VAnQfHTVPEVT3UI63R3JfAzCt6mxiyrNWsfAdLkV7gksOtI54tM1a1DBowAdeVHylK+hWAmxgHOjnH5U57G2r4jBo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EyWWuGpZ; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EyWWuGpZ" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-847a483e344so994432b3a.0 for ; Tue, 07 Jul 2026 08:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783437458; x=1784042258; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=etAGJOjl2S+5CmKbtIQYaUZTk3Wz8jaGhbuGeikcUyQ=; b=EyWWuGpZAPFSRo1a12o9SZ9hCZtWtVAvQrnHhxfX6tLi5L16QHHPQl37udCWjER/Yx HyW8sJnz26QxTs8TQ5QJaNPtfhcZ+r23smfexk0uDc47i8cwcmiDBa8XpGkBrRPNm1QI vF/ilvYlhvGujpE6Ex0h1usOUwxwM5t8pDJnwLdHmJNBPnvKC6aU9AVrn9jRgQOQQ8I6 HvBSVnuyoUruTs3v1+zmBgwwrEGcr9TfEw+SCiJldkSRJKkaKUlIIliqJy4L52nlhsFN Kqh0g+OsQsBMRzs4JJgkNZCzM128eD+IW7XHSA+8yC5xTXoXsSvRX5ImYs4TH7SNenGc A/wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783437458; x=1784042258; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=etAGJOjl2S+5CmKbtIQYaUZTk3Wz8jaGhbuGeikcUyQ=; b=Dc0O4aj8/YuzBN+1FtNNz9IEn1ZjG/tPrKgErTUc3RFnKiAFS35S7YUw0XIMMODq+Q wGgdUFj4bT6+/syJUJ6a2wvKNKNBC2VAG5l9TBfeLigzSGkTNDCzR8UaxNFBOUbiJdEZ e7+pSoU1BpnoZcr+hG7ZodOAWroXXHbV59kJiSxFC1SNw5+Ch9Rm0xzR7A2xNXj1vzby rDb77wN0DVwxPJfjSin5dEMzoCmYSHMj26j/UsJnUHe1DuLeXQydL6dBFe1mP11O684M 8n8PWz4YaQew0ZtySgucsy6ieNGFcb1ClolzIxGN6bBN/kVcLvr8sYa7YVUhjaPd5tlC XB0g== X-Forwarded-Encrypted: i=1; AHgh+Rqst6/zX4W6OiHKc7eF6gbpNLT3sPKmQhr91h/yEsuRu76foiB9HdMACVqaUHWevdyqYsNU2+nrVx1Iy18=@vger.kernel.org X-Gm-Message-State: AOJu0YywyL7GUGaRHi1034OCoQFlE+55IoSQbTek7Y/STWL0ItKvlDPP +LmxT0eZ58mttsDqGfCJ3qSDIoVlghAXkVtmD15Q1Kq/g3lm7zpQnPcu X-Gm-Gg: AfdE7ck9tWlvvJaSlgGTseJ/itplHY3lVYDW7IWRh2wc11gsCUuhpWWIcXzAbcW3rdL dxt9tQOT0ZXwEBbpH8dFAhV4D3xsGjXz4svXVROSzyy1vcPrABB8YcN8ZEY7xM1aim1bWioUp6W 8eL1icoePm3m0a1oIC2C//9aMbLIxiKavXVHiJtKQxWSJCokI85uhXYAz0XGeSMUDHUBY2F/Stq CJByezd3KKtN/s/gaXwRz31vop2imLMUVaHL3T6/9HQ+jo2AmkIf45rjvKk5myCMWiSzdqMEZPh ZO7Vavp7lvyYEkZTN++pBuj9F6be6CVJ6z3lI0wYJK8/NFA+DqDzM0Ip+FC4KXyshtoAcwPzjvu J61XgeRXbDclSKZRPLvQDVbvQVejA9WrNgSnXDrgXlBIG2B0WoD929eVh7a241tkiPUHwdU1HOD DjYbm8gtAJYqY1u1pV69IX/nb+AybLkOrspDvkSJsdMqy42IOoDZfF9by5bAhQtQG6pg== X-Received: by 2002:a05:6a00:1816:b0:845:3033:6cbb with SMTP id d2e1a72fcca58-8482f1f0eecmr3099600b3a.11.1783437458045; Tue, 07 Jul 2026 08:17:38 -0700 (PDT) Received: from localhost.localdomain ([2001:448a:2003:63bf:6d1c:a790:1205:96eb]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-847f6d4e741sm5724397b3a.28.2026.07.07.08.17.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 07 Jul 2026 08:17:37 -0700 (PDT) From: Muchamad Coirul Anwar To: jic23@kernel.org, lars@metafoo.de Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, andi.shyti@kernel.org, wsa+renesas@sang-engineering.com, ojeda@kernel.org, dakr@kernel.org, igor.korotin@linux.dev, branstj@gmail.com, Muchamad Coirul Anwar Subject: [RFC PATCH v4 1/3] i2c: rust: implement SMBus read abstraction via kernel::io::Io for I2cClient Date: Tue, 7 Jul 2026 22:15:40 +0700 Message-ID: <20260707151542.91997-2-muchamadcoirulanwar@gmail.com> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20260707151542.91997-1-muchamadcoirulanwar@gmail.com> References: <20260707151542.91997-1-muchamadcoirulanwar@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Implement the Io trait for I2cClient, providing SMBus byte and word read/write operations with automatic offset validation via io_addr(). I2cClient implements the generic Io trait rather than exposing standalone SMBus methods, following the direction established in [1] and [2]. The underlying calls are still i2c_smbus_read_byte_data and i2c_smbus_read_word_data. I2cClient now implements IoCapable and IoCapable with maxsize=256 (SMBus command byte range 0x00-0xFF, not the 7-bit device address which is handled by the I2C core at adapter level). Link: https://lore.kernel.org/rust-for-linux/20260131-i2c-adapter-v1-4-5a436e34cd1a@gmail.com/ Link: https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?h=driver-core-testing&id=121d87b28e1d9061d3aaa156c43a627d3cb5e620 Signed-off-by: Muchamad Coirul Anwar --- rust/kernel/i2c.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs index 624b971ca8b0..31c7216d0299 100644 --- a/rust/kernel/i2c.rs +++ b/rust/kernel/i2c.rs @@ -14,6 +14,7 @@ devres::Devres, driver, error::*, + io::{Io, IoCapable}, of, prelude::*, sync::aref::{ @@ -601,3 +602,91 @@ unsafe impl Send for Registration {} // SAFETY: `Registration` offers no interior mutability (no mutation through &self // and no mutable access is exposed) unsafe impl Sync for Registration {} + +impl IoCapable for I2cClient { + unsafe fn io_read(&self, address: usize) -> u8 { + // SAFETY: `self.as_raw()` returns a valid `struct i2c_client` pointer + // (type invariant). `address` was pre-validated by io_addr() before + // this function is called (trait contract). + let ret = unsafe { bindings::i2c_smbus_read_byte_data(self.as_raw(), address as u8) }; + + // NOTE: Error is lost here. This is only called via try_read() which + // first validates bounds via io_addr(). For I2C, the caller should + // always use try_read8() which provides proper error handling. + ret as u8 + } + + unsafe fn io_write(&self, value: u8, address: usize) { + // SAFETY: `self.as_raw()` returns a valid `struct i2c_client` pointer. + // `address` pre-validated by io_addr(). + unsafe { bindings::i2c_smbus_write_byte_data(self.as_raw(), address as u8, value) }; + // NOTE: Return value is ignored. `IoCapable` trait signature does not + // support error returns. Use with caution. + } +} + +impl IoCapable for I2cClient { + unsafe fn io_read(&self, address: usize) -> u16 { + // SAFETY: `self.as_raw()` returns a valid `struct i2c_client` pointer. + // `address` pre-validated by io_addr(). + let ret = unsafe { bindings::i2c_smbus_read_word_data(self.as_raw(), address as u8) }; + + // NOTE: Error is lost here. See u8 implementation note. + ret as u16 + } + + unsafe fn io_write(&self, value: u16, address: usize) { + // SAFETY: `self.as_raw()` returns a valid `struct i2c_client` pointer. + // `address` pre-validated by io_addr(). + unsafe { bindings::i2c_smbus_write_word_data(self.as_raw(), address as u8, value) }; + // NOTE: Return value is ignored. + } +} + +impl Io for I2cClient { + #[inline] + fn addr(&self) -> usize { + 0 + } + + /// SMBus command byte range: 0x00-0xFF (256 possible register addresses). + /// This is NOT the 7-bit device address; that is handled by the I2C core. + #[inline] + fn maxsize(&self) -> usize { + 256 + } + + #[inline] + fn try_read8(&self, offset: usize) -> Result + where + Self: IoCapable, + { + let reg = self.io_addr::(offset)? as u8; + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct i2c_client` + // as guaranteed by the type invariant of `I2cClient`. `reg` is bounds-checked + // by `io_addr()` above (offset + 1 <= 256). + let ret = unsafe { bindings::i2c_smbus_read_byte_data(self.as_raw(), reg) }; + if ret < 0 { + Err(Error::from_errno(ret)) + } else { + Ok(ret as u8) + } + } + + #[inline] + fn try_read16(&self, offset: usize) -> Result + where + Self: IoCapable, + { + let reg = self.io_addr::(offset)? as u8; + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct i2c_client` + // as guaranteed by the type invariant of `I2cClient`. `reg` is bounds-checked + // by `io_addr()` above (offset + 2 <= 256). + let ret = unsafe { bindings::i2c_smbus_read_word_data(self.as_raw(), reg) }; + if ret < 0 { + Err(Error::from_errno(ret)) + } else { + Ok(ret as u16) + } + } +} -- 2.50.0