rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Danilo Krummrich <dakr@kernel.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:13:24 +0200	[thread overview]
Message-ID: <2025092356-rounding-eligibly-c4b7@gregkh> (raw)
In-Reply-To: <DD07LUJXNZN9.3RHH9NJNRFVNN@kernel.org>

On Tue, Sep 23, 2025 at 03:21:09PM +0200, Danilo Krummrich wrote:
> On Mon Aug 25, 2025 at 8:18 PM CEST, Daniel Almeida wrote:
> > +/// The USB driver trait.
> > +///
> > +/// # Examples
> > +///
> > +///```
> > +/// # use kernel::{bindings, device::Core, usb};
> > +/// use kernel::prelude::*;
> > +///
> > +/// struct MyDriver;
> > +///
> > +/// kernel::usb_device_table!(
> > +///     USB_TABLE,
> > +///     MODULE_USB_TABLE,
> > +///     <MyDriver as usb::Driver>::IdInfo,
> > +///     [
> > +///         (usb::DeviceId::from_id(0x1234, 0x5678), ()),
> > +///         (usb::DeviceId::from_id(0xabcd, 0xef01), ()),
> > +///     ]
> > +/// );
> > +///
> > +/// impl usb::Driver for MyDriver {
> > +///     type IdInfo = ();
> > +///     const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
> > +///
> > +///     fn probe(
> > +///         _interface: &usb::Interface<Core>,
> > +///         _id: &usb::DeviceId,
> > +///         _info: &Self::IdInfo,
> > +///     ) -> Result<Pin<KBox<Self>>> {
> > +///         Err(ENODEV)
> > +///     }
> > +///
> > +///     fn disconnect(_interface: &usb::Interface<Core>, _data: Pin<&Self>) {}
> > +/// }
> > +///```
> > +pub trait Driver {
> > +    /// The type holding information about each one of the device ids supported by the driver.
> > +    type IdInfo: 'static;
> > +
> > +    /// The table of device ids supported by the driver.
> > +    const ID_TABLE: IdTable<Self::IdInfo>;
> > +
> > +    /// USB driver probe.
> > +    ///
> > +    /// Called when a new USB interface is bound to this driver.
> > +    /// Implementers should attempt to initialize the interface here.
> > +    fn probe(
> > +        interface: &Interface<device::Core>,
> > +        id: &DeviceId,
> > +        id_info: &Self::IdInfo,
> > +    ) -> Result<Pin<KBox<Self>>>;
> > +
> > +    /// USB driver disconnect.
> > +    ///
> > +    /// Called when the USB interface is about to be unbound from this driver.
> > +    fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self>);
> 
> I think this callback should be optional, like all the other unbind() we have in
> other busses.
> 
> @Greg: Why is this called disconnect() in the C code instead of remove()?

I don't know, naming is hard, and it was the first, or second, user of
the driver model we ever created :)

> For Rust, I feel like we should align to the unbind() terminology we use
> everywhere else (for the same reasons we chose unbind() over remove() for other
> busses as well).
> 
> We went with unbind(), since the "raw" remove() (or disconnect()) callback does
> more, i.e. it first calls into unbind() and then drops the driver's private data
> for this specific device.
> 
> So, the unbind() callback that goes to the driver is only meant as a place for
> drivers to perform operations to tear down the device. Resources are freed
> subsequently when the driver's private data is dropped.

Yes, we should probably match these up with what PCI does here in the
end, that would be good.

> > +/// 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.

Functions like usb_fill_bulk_urb() takes a pointer to a usb_device, not
an interface.  Yes, we should fix that, but that "mistake" dates way way
way back to the original USB api decades ago.  So much so that I didn't
even remember that we used that pointer there :)

So it's ok to wrap this for now, it will be needed.

thanks,

greg k-h

  parent reply	other threads:[~2025-09-23 14:13 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
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 [this message]
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=2025092356-rounding-eligibly-c4b7@gregkh \
    --to=gregkh@linuxfoundation.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=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --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).