From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: "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>,
"Danilo Krummrich" <dakr@kernel.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-usb@vger.kernel.org
Subject: Re: [PATCH 2/2] samples: rust: add a USB driver sample
Date: Sat, 6 Sep 2025 13:14:57 +0200 [thread overview]
Message-ID: <2025090618-smudgy-cringing-a7a4@gregkh> (raw)
In-Reply-To: <20250825-b4-usb-v1-2-7aa024de7ae8@collabora.com>
On Mon, Aug 25, 2025 at 03:18:06PM -0300, Daniel Almeida wrote:
> In light of the newly-added Rust abstractions for USB devices and
> drivers, add a sample USB rust driver that serves both to showcase what
> is currently supported, as well as be the only user of the USB
> abstractions for now.
>
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
> ---
> samples/rust/Kconfig | 11 ++++++++++
> samples/rust/Makefile | 1 +
> samples/rust/rust_driver_usb.rs | 47 +++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 59 insertions(+)
>
> diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
> index 7f7371a004ee0a8f67dca99c836596709a70c4fa..fb222f93014c921b27a8a9a4293e90a2532faa82 100644
> --- a/samples/rust/Kconfig
> +++ b/samples/rust/Kconfig
> @@ -83,6 +83,17 @@ config SAMPLE_RUST_DRIVER_PLATFORM
>
> If unsure, say N.
>
> +config SAMPLE_RUST_DRIVER_USB
> + tristate "USB Driver"
> + depends on USB
> + help
> + This option builds the Rust USB driver sample.
> +
> + To compile this as a module, choose M here:
> + the module will be called rust_driver_usb.
> +
> + If unsure, say N.
> +
> config SAMPLE_RUST_DRIVER_FAUX
> tristate "Faux Driver"
> help
> diff --git a/samples/rust/Makefile b/samples/rust/Makefile
> index bd2faad63b4f3befe7d1ed5139fe25c7a8b6d7f6..4e7df8a5cd277d101920c4b89a3ac6648b372b28 100644
> --- a/samples/rust/Makefile
> +++ b/samples/rust/Makefile
> @@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
> obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o
> obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
> obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
> +obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o
> obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
> obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
> obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o
> diff --git a/samples/rust/rust_driver_usb.rs b/samples/rust/rust_driver_usb.rs
> new file mode 100644
> index 0000000000000000000000000000000000000000..5c396f421de7f972985e57af48e8a9da0c558973
> --- /dev/null
> +++ b/samples/rust/rust_driver_usb.rs
> @@ -0,0 +1,47 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
> +
> +//! Rust USB driver sample.
> +
> +use kernel::{device, device::Core, prelude::*, sync::aref::ARef, usb};
> +
> +struct SampleDriver {
> + _intf: ARef<usb::Interface>,
> +}
> +
> +kernel::usb_device_table!(
> + USB_TABLE,
> + MODULE_USB_TABLE,
> + <SampleDriver as usb::Driver>::IdInfo,
> + [(usb::DeviceId::from_id(0x1234, 0x5678), ()),]
> +);
> +
> +impl usb::Driver for SampleDriver {
> + type IdInfo = ();
> + const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
> +
> + fn probe(
> + intf: &usb::Interface<Core>,
> + _id: &usb::DeviceId,
> + _info: &Self::IdInfo,
> + ) -> Result<Pin<KBox<Self>>> {
> + let dev: &device::Device<Core> = intf.as_ref();
> + dev_info!(dev, "Rust USB driver sample probed\n");
> +
> + let drvdata = KBox::new(Self { _intf: intf.into() }, GFP_KERNEL)?;
> + Ok(drvdata.into())
> + }
> +
> + fn disconnect(intf: &usb::Interface<Core>, _data: Pin<&Self>) {
> + let dev: &device::Device<Core> = intf.as_ref();
> + dev_info!(dev, "Rust USB driver sample disconnected\n");
> + }
> +}
> +
> +kernel::module_usb_driver! {
> + type: SampleDriver,
> + name: "rust_driver_usb",
> + authors: ["Daniel Almeida"],
> + description: "Rust USB driver sample",
> + license: "GPL v2",
> +}
Sorry for the delay.
But these bindings really are only for a usb interface probe/disconnect
sequence, right? no real data flow at all?
I recommend looking at the usb-skeleton.c driver, and implementing that
as your sample driver for rust. That will ensure that you actually have
the correct apis implemented and the reference count logic working
properly. You have urb anchors and callbacks and other stuff as well to
ensure that you get right. That driver pretty much should handle
everything that you need to do to write a usb driver for any type of
"real" device.
thanks,
greg k-h
next prev parent reply other threads:[~2025-09-06 11:15 UTC|newest]
Thread overview: 18+ 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-08-25 18:18 ` [PATCH 2/2] samples: rust: add a USB driver sample Daniel Almeida
2025-09-06 11:14 ` Greg Kroah-Hartman [this message]
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-08-25 20:32 ` [PATCH 0/2] rust: usb: add initial USB abstractions 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=2025090618-smudgy-cringing-a7a4@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).