Skip to content

Commit cc80dbb

Browse files
dwlsalmeidagregkh
authored andcommitted
samples: rust: add a USB driver sample
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ force USB = y for now - gregkh ] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent e7e2296 commit cc80dbb

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

samples/rust/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ config SAMPLE_RUST_DRIVER_PLATFORM
8383

8484
If unsure, say N.
8585

86+
config SAMPLE_RUST_DRIVER_USB
87+
tristate "USB Driver"
88+
depends on USB = y
89+
help
90+
This option builds the Rust USB driver sample.
91+
92+
To compile this as a module, choose M here:
93+
the module will be called rust_driver_usb.
94+
95+
If unsure, say N.
96+
8697
config SAMPLE_RUST_DRIVER_FAUX
8798
tristate "Faux Driver"
8899
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
77
obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o
88
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
99
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
10+
obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o
1011
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
1112
obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
1213
obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o

samples/rust/rust_driver_usb.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
3+
4+
//! Rust USB driver sample.
5+
6+
use kernel::{device, device::Core, prelude::*, sync::aref::ARef, usb};
7+
8+
struct SampleDriver {
9+
_intf: ARef<usb::Interface>,
10+
}
11+
12+
kernel::usb_device_table!(
13+
USB_TABLE,
14+
MODULE_USB_TABLE,
15+
<SampleDriver as usb::Driver>::IdInfo,
16+
[(usb::DeviceId::from_id(0x1234, 0x5678), ()),]
17+
);
18+
19+
impl usb::Driver for SampleDriver {
20+
type IdInfo = ();
21+
const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
22+
23+
fn probe(
24+
intf: &usb::Interface<Core>,
25+
_id: &usb::DeviceId,
26+
_info: &Self::IdInfo,
27+
) -> Result<Pin<KBox<Self>>> {
28+
let dev: &device::Device<Core> = intf.as_ref();
29+
dev_info!(dev, "Rust USB driver sample probed\n");
30+
31+
let drvdata = KBox::new(Self { _intf: intf.into() }, GFP_KERNEL)?;
32+
Ok(drvdata.into())
33+
}
34+
35+
fn disconnect(intf: &usb::Interface<Core>, _data: Pin<&Self>) {
36+
let dev: &device::Device<Core> = intf.as_ref();
37+
dev_info!(dev, "Rust USB driver sample disconnected\n");
38+
}
39+
}
40+
41+
kernel::module_usb_driver! {
42+
type: SampleDriver,
43+
name: "rust_driver_usb",
44+
authors: ["Daniel Almeida"],
45+
description: "Rust USB driver sample",
46+
license: "GPL v2",
47+
}

0 commit comments

Comments
 (0)