From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDB7C1FCF41; Mon, 7 Jul 2025 12:02:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751889730; cv=none; b=KLWq6xUFzOTh17hKwE97K2y3YQ/H3v5SevUXbgXm4tXyFkhSqlLiBF+Kf9cgNRz8cCu/GxGIrKU5sO01qZBiFTNsu9N2Eb0WHzAcqgfX+jBrZl0m/W6VRdrQapfz4blJfcqW8mndguaQc+YmROvA7L0xFsPS/llP6VkyvmlAV8U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751889730; c=relaxed/simple; bh=kVFoEUGp2UNblRLb6XHbJyewGsxkZT1YXp1RdETPjgA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MaGLz6/eu34iuabuAWJFb2N2p4ukNDEvsVsEMcqAIPn32zIyMFX4uaZ/kDL2tLWr5tONGyF6dzmiTmPVws61xrF6njzAh55psMJa+X8yCgqyEn2HJAC8Fz/1s8BwWHmB+8NuM776z+SlaIe3XQpFaq3ZR9h8A/2tspcjWdsZHzc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KDKAXIhz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KDKAXIhz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C767C4CEE3; Mon, 7 Jul 2025 12:02:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751889730; bh=kVFoEUGp2UNblRLb6XHbJyewGsxkZT1YXp1RdETPjgA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=KDKAXIhzKK8nhyJsT1ghan+q23bZK33/gYBQZL9JnKbtsq8KKMS+FrxY1bLcWYHD/ z80rAiXnLR53tqaCiw/W2KhnhQY8bB7nnstEZr1P6Ve5i6oYF4I4OTTK/6Q3grAVsC E/Mm1Sbintz3rLtIO7ZOmG5edqv4KkUrASMdIZEbiJNbx3d+iF3v5GeTegBeiFJRGs Jtu1ucNnrHR4t1eETXsFnRY6dc121wka+ovWmiU4x/BGpKM65+0aSJS53ARECgDD61 I83/3HkMrradwCjf9AwlMhMonNPswOpgLuLevANe+LCWLrjB/olbCdMQLiWNMeulIG PxNotJYXYv4uw== Date: Mon, 7 Jul 2025 14:02:03 +0200 From: Danilo Krummrich To: Igor Korotin Cc: Miguel Ojeda , Alex Gaynor , Wolfram Sang , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: Re: [PATCH v2 2/4] rust: i2c: add manual I2C device creation abstractions Message-ID: References: <20250704153332.1193214-1-igor.korotin.linux@gmail.com> <20250704153912.1197034-1-igor.korotin.linux@gmail.com> <954a40a4-2f5a-4ef8-84ea-3d2ba2c85cfe@gmail.com> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <954a40a4-2f5a-4ef8-84ea-3d2ba2c85cfe@gmail.com> On Mon, Jul 07, 2025 at 12:20:15PM +0100, Igor Korotin wrote: > > > On 7/4/25 20:54, Danilo Krummrich wrote: > > On Fri, Jul 04, 2025 at 04:39:12PM +0100, Igor Korotin wrote: > >> -pub struct Device( > >> +pub struct Device( > >> Opaque, > >> PhantomData, > >> + PhantomData, > >> ); > > > > I see what you're doing here, but I think you're thinking this way too > > complicated. > > > > I recommend not to reuse the Device type to register a new I2C client device, > > it's adding too much complexity without any real value. > > > > You also don't want the DeviceContext types for a device registration, since the > > registration will never have any other DeviceContext than device::Normal (see > > also my comment on the sample module). > > > > DeviceContext types are only useful for &Device (i.e. references) given out for > > a specific scope, such as probe(), remove(), etc. > > > > The only thing you really want to do is to register a new I2C client device, get > > a i2c::Registration instance and call i2c_unregister_device() when the > > i2c::Registration is dropped. > > > > This is exactly the same use-case as we have in the auxiliary bus. I highly > > recommend looking at what auxiliary::Registration does [1]. > > > > Also note that if you want a reference to the device in the i2c::Registration, > > you can also add a i2c::Registration::device() method that returns an > > &i2c::Device, which through into() you can obtain an ARef from. > > > > [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/auxiliary.rs?h=v6.16-rc4#n299 > > I took a quick look at the auxiliary Registration abstraction and I see > that it is not applicable for I2C subsystem. The issue here is that I2C > C code doesn't provide with an API that can registers an I2C client from > already existing `struct i2c_client`. I don't see why the following wouldn't work: struct Registration(NonNull); impl Registration { pub fn new(adp: &I2cAdapterRef, info: &I2cBoardInfo) -> Result { // SAFETY: [...] let cli = unsafe { bindings::i2c_new_client_device(adp.as_raw(), info.as_raw()) }; // Handle ERR_PTR() Self(NonNull::new(cli)) } } impl Drop for Registration { fn drop(&mut self) { // SAFETY: [...] unsafe { bindings::i2c_unregister_device(self.as_ptr()) }; } } And in you sample driver you can still the exactly the same as you did before: struct SampleDriver { _reg: i2c::Registration, } impl kernel::Module for SampleDriver { fn init(_module: &'static ThisModule) -> Result { let adapter = i2c::I2cAdapterRef::get(0).ok_or(EINVAL)?; let reg = i2c::Registration::new(&adapter, &BOARD_INFO)?; Ok(Self { _reg: reg }) } } Note that you can also combine you two sample drivers into one by doing the above *and* register a and I2C driver that probes against your device registration. :)