rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-usb@vger.kernel.org
Subject: Re: [PATCH 1/2] rust: usb: add basic USB abstractions
Date: Tue, 23 Sep 2025 16:38:34 +0200	[thread overview]
Message-ID: <DD0994IZMBVQ.2HZOA2ZMWT2I@kernel.org> (raw)
In-Reply-To: <2025092326-banked-resubmit-67c0@gregkh>

On Tue Sep 23, 2025 at 4:30 PM CEST, Greg Kroah-Hartman wrote:
> On Tue, Sep 23, 2025 at 04:03:01PM +0200, Danilo Krummrich wrote:
>> On Tue Sep 23, 2025 at 3:31 PM CEST, Daniel Almeida wrote:
>> >>> +/// A USB device.
>> >>> +///
>> >>> +/// This structure represents the Rust abstraction for a C [`struct usb_device`].
>> >>> +/// The implementation abstracts the usage of a C [`struct usb_device`] passed in
>> >>> +/// from the C side.
>> >>> +///
>> >>> +/// # Invariants
>> >>> +///
>> >>> +/// A [`Device`] instance represents a valid [`struct usb_device`] created by the C portion of the
>> >>> +/// kernel.
>> >>> +///
>> >>> +/// [`struct usb_device`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_device
>> >>> +#[repr(transparent)]
>> >>> +pub struct Device<Ctx: device::DeviceContext = device::Normal>(
>> >>> +    Opaque<bindings::usb_device>,
>> >>> +    PhantomData<Ctx>,
>> >>> +);
>> >> 
>> >> What do you use the struct usb_device abstraction for? I only see the sample
>> >> driver probing a USB interface instead.
>> >
>> > What I was brainstorming with Greg is to submit this initial support, and then
>> > follow up with all the other abstractions needed to implement a Rust version of
>> > usb-skeleton.c. IIUC, the plan is to submit any fixes as follow-ups, as we're
>> > close to the merge window.
>> >
>> > struct usb_device would be used for the skeleton driver, so we should keep it if
>> > we're following the plan above, IMHO.
>> 
>> Yes, it's clearly required for the raw accessors for submitting URBs, e.g.
>> usb_fill_bulk_urb(), usb_submit_urb(), etc.
>> 
>> But I'm not sure you actually have to expose a representation of a struct
>> usb_device (with device context information) publically for that. It seems to me
>> that this can all be contained within the abstraction.
>> 
>> For instance, the public API could look like this:
>> 
>> 	let urb = intf.urb_create()?;
>> 	urb.fill_bulk(buffer, callback_fn, ...)?;
>> 	urb.submit();
>> 
>> The urb_create() method of a usb::Interface can derive the struct usb_device
>> from the struct usb_interface internally and store it in the Urb structure, i.e.
>> no need to let drivers mess with this.
>> 
>> So, I think for this part it makes more sense to first work out the other
>> APIs before exposing things speculatively.
>> 
>> I also just spotted this:
>> 
>> 	impl<Ctx: device::DeviceContext> AsRef<Device<Ctx>> for Interface<Ctx> {
>> 	    fn as_ref(&self) -> &Device<Ctx> {
>> 	        // SAFETY: `self.as_raw()` is valid by the type invariants. For a valid interface,
>> 	        // the helper should always return a valid USB device pointer.
>> 	        let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
>> 	
>> 	        // SAFETY: The helper returns a valid interface pointer that shares the
>> 	        // same `DeviceContext`.
>> 	        unsafe { &*(usb_dev.cast()) }
>> 	    }
>> 	}
>> 
>> which I think is wrong. You can't derive the device context of a usb::Interface
>> for a usb::Device generically. You probably can for the Bound context, but not
>> for the Core context.
>> 
>> But honestly, I'm even unsure for the Bound context.
>> 
>> @Greg: Can we guarantee that a struct usb_device is always bound as long as one
>> of its interfaces is still bound?
>
> Bound to what?

Well, that's kinda my point. :)

Having a &usb::Device<Bound> would mean that for the lifetime of the reference
it is guaranteed that the usb::Device is bound to its USB device driver
(struct usb_device_driver).

The code above establishes that you can get a &usb::Device<Bound> from a
&usb::Interface<Bound>, i.e. an interface that is bound to a USB driver
(struct usb_driver).

It also does establish the same with other device contexts, such as the Core
context.

Despite the question whether this is sematically useful, I doubt that this is
a correct assumption to take.

  reply	other threads:[~2025-09-23 14:38 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 18:18 [PATCH 0/2] rust: usb: add initial USB abstractions Daniel Almeida
2025-08-25 18:18 ` [PATCH 1/2] rust: usb: add basic " Daniel Almeida
2025-08-25 20:49   ` Benno Lossin
2025-08-25 21:03     ` Daniel Almeida
2025-09-23 13:21   ` Danilo Krummrich
2025-09-23 13:31     ` Daniel Almeida
2025-09-23 14:03       ` Danilo Krummrich
2025-09-23 14:30         ` Greg Kroah-Hartman
2025-09-23 14:38           ` Danilo Krummrich [this message]
2025-09-23 14:52             ` Greg Kroah-Hartman
2025-09-23 15:06               ` Danilo Krummrich
2025-09-23 14:58             ` Alan Stern
2025-09-23 14:13     ` Greg Kroah-Hartman
2025-09-23 14:16       ` Oliver Neukum
2025-09-23 14:22         ` Greg Kroah-Hartman
2025-09-23 14:25           ` Danilo Krummrich
2025-09-23 14:37             ` Greg Kroah-Hartman
2025-09-23 14:42               ` Danilo Krummrich
2025-09-23 14:49                 ` Greg Kroah-Hartman
2025-09-23 15:46                   ` Danilo Krummrich
2025-09-23 14:18       ` Danilo Krummrich
2025-08-25 18:18 ` [PATCH 2/2] samples: rust: add a USB driver sample Daniel Almeida
2025-09-06 11:14   ` Greg Kroah-Hartman
2025-09-06 12:04     ` Daniel Almeida
2025-09-06 12:10       ` Greg Kroah-Hartman
2025-09-06 12:41         ` Daniel Almeida
2025-09-06 13:07           ` Greg Kroah-Hartman
2025-09-06 14:49             ` Alan Stern
2025-09-06 14:56             ` Daniel Almeida
2025-09-06 13:22           ` Danilo Krummrich
2025-09-06 14:50             ` Daniel Almeida
2025-09-06 15:22               ` Danilo Krummrich
2025-09-06 15:46                 ` Daniel Almeida
2025-09-06 15:48                   ` Danilo Krummrich
2025-09-09 11:19                   ` Simon Neuenhausen
2025-09-09 12:12                     ` Daniel Almeida
2025-09-09 13:25                       ` Greg Kroah-Hartman
2025-09-09 12:14                     ` Greg Kroah-Hartman
2025-09-09 13:05                       ` Simon Neuenhausen
2025-08-25 20:32 ` [PATCH 0/2] rust: usb: add initial USB abstractions Greg Kroah-Hartman
2025-09-23 12:05 ` Greg Kroah-Hartman
2025-09-23 12:29   ` Alice Ryhl
2025-09-23 12:31     ` Greg Kroah-Hartman
2025-09-23 12:34     ` Daniel Almeida
2025-09-23 12:41       ` Greg Kroah-Hartman
2025-09-23 12:55       ` Miguel Ojeda
2025-09-23 12:56   ` Miguel Ojeda
2025-09-23 13:24     ` Daniel Almeida
2025-09-23 21:29       ` Miguel Ojeda
2025-09-25 12:52 ` Greg Kroah-Hartman
2025-09-25 12:58   ` Daniel Almeida
2025-09-25 13:29   ` Danilo Krummrich
2025-09-25 17:38     ` Greg Kroah-Hartman

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=DD0994IZMBVQ.2HZOA2ZMWT2I@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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;
as well as URLs for NNTP newsgroup(s).