summaryrefslogtreecommitdiff
path: root/rust/kernel/usb.rs
diff options
context:
space:
mode:
authorMarkus Probst <markus.probst@posteo.de>2025-10-27 20:06:03 +0000
committerDanilo Krummrich <dakr@kernel.org>2025-11-18 11:00:51 +1300
commite4addc7cc2dfcc19f1c8c8e47f3834b22cb21559 (patch)
tree3b64a849d11179a07ddce4592af810735b250041 /rust/kernel/usb.rs
parent13ae55e24a854efd33eb84bbd19a74b933228ccd (diff)
rust: Add trait to convert a device reference to a bus device reference
Implement the `AsBusDevice` trait for converting a `Device` reference to a bus device reference for all bus devices. The `AsBusDevice` trait allows abstractions to provide the bus device in class device callbacks. It must not be used by drivers and is intended for bus and class device abstractions only. Signed-off-by: Markus Probst <markus.probst@posteo.de> Link: https://patch.msgid.link/20251027200547.1038967-2-markus.probst@posteo.de [ * Remove unused import. * Change visibility of AsBusDevice to public. * Fix build for USB. * Add impl for I2cClient. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel/usb.rs')
-rw-r--r--rust/kernel/usb.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index 534e3ded5442..d10b65e9fb6a 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -15,7 +15,14 @@ use crate::{
types::{AlwaysRefCounted, Opaque},
ThisModule,
};
-use core::{marker::PhantomData, mem::MaybeUninit, ptr::NonNull};
+use core::{
+ marker::PhantomData,
+ mem::{
+ offset_of,
+ MaybeUninit, //
+ },
+ ptr::NonNull,
+};
/// An adapter for the registration of USB drivers.
pub struct Adapter<T: Driver>(T);
@@ -324,6 +331,12 @@ impl<Ctx: device::DeviceContext> Interface<Ctx> {
}
}
+// SAFETY: `usb::Interface` is a transparent wrapper of `struct usb_interface`.
+// The offset is guaranteed to point to a valid device field inside `usb::Interface`.
+unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Interface<Ctx> {
+ const OFFSET: usize = offset_of!(bindings::usb_interface, dev);
+}
+
// SAFETY: `Interface` is a transparent wrapper of a type that doesn't depend on
// `Interface`'s generic argument.
kernel::impl_device_context_deref!(unsafe { Interface });