@@ -99,31 +99,41 @@ use crate::{acpi, device, of, str::CStr, try_pin_init, types::Opaque, ThisModule
9999use core:: pin:: Pin ;
100100use pin_init:: { pin_data, pinned_drop, PinInit } ;
101101
102+ /// Trait describing the layout of a specific device driver.
103+ ///
104+ /// This trait describes the layout of a specific driver structure, such as `struct pci_driver` or
105+ /// `struct platform_driver`.
106+ ///
107+ /// # Safety
108+ ///
109+ /// Implementors must guarantee that:
110+ /// - `DriverType` is `repr(C)`.
111+ pub unsafe trait DriverLayout {
112+ /// The specific driver type embedding a `struct device_driver`.
113+ type DriverType : Default ;
114+ }
115+
102116/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
103117/// Amba, etc.) to provide the corresponding subsystem specific implementation to register /
104- /// unregister a driver of the particular type (`RegType `).
118+ /// unregister a driver of the particular type (`DriverType `).
105119///
106- /// For instance, the PCI subsystem would set `RegType ` to `bindings::pci_driver` and call
120+ /// For instance, the PCI subsystem would set `DriverType ` to `bindings::pci_driver` and call
107121/// `bindings::__pci_register_driver` from `RegistrationOps::register` and
108122/// `bindings::pci_unregister_driver` from `RegistrationOps::unregister`.
109123///
110124/// # Safety
111125///
112- /// A call to [`RegistrationOps::unregister`] for a given instance of `RegType` is only valid if a
113- /// preceding call to [`RegistrationOps::register`] has been successful.
114- pub unsafe trait RegistrationOps {
115- /// The type that holds information about the registration. This is typically a struct defined
116- /// by the C portion of the kernel.
117- type RegType : Default ;
118-
126+ /// A call to [`RegistrationOps::unregister`] for a given instance of `DriverType` is only valid if
127+ /// a preceding call to [`RegistrationOps::register`] has been successful.
128+ pub unsafe trait RegistrationOps : DriverLayout {
119129 /// Registers a driver.
120130 ///
121131 /// # Safety
122132 ///
123133 /// On success, `reg` must remain pinned and valid until the matching call to
124134 /// [`RegistrationOps::unregister`].
125135 unsafe fn register (
126- reg : & Opaque < Self :: RegType > ,
136+ reg : & Opaque < Self :: DriverType > ,
127137 name : & ' static CStr ,
128138 module : & ' static ThisModule ,
129139 ) -> Result ;
@@ -134,7 +144,7 @@ pub unsafe trait RegistrationOps {
134144 ///
135145 /// Must only be called after a preceding successful call to [`RegistrationOps::register`] for
136146 /// the same `reg`.
137- unsafe fn unregister ( reg : & Opaque < Self :: RegType > ) ;
147+ unsafe fn unregister ( reg : & Opaque < Self :: DriverType > ) ;
138148}
139149
140150/// A [`Registration`] is a generic type that represents the registration of some driver type (e.g.
@@ -146,7 +156,7 @@ pub unsafe trait RegistrationOps {
146156#[ pin_data( PinnedDrop ) ]
147157pub struct Registration < T : RegistrationOps > {
148158 #[ pin]
149- reg : Opaque < T :: RegType > ,
159+ reg : Opaque < T :: DriverType > ,
150160}
151161
152162// SAFETY: `Registration` has no fields or methods accessible via `&Registration`, so it is safe to
@@ -161,13 +171,13 @@ impl<T: RegistrationOps> Registration<T> {
161171 /// Creates a new instance of the registration object.
162172 pub fn new ( name : & ' static CStr , module : & ' static ThisModule ) -> impl PinInit < Self , Error > {
163173 try_pin_init ! ( Self {
164- reg <- Opaque :: try_ffi_init( |ptr: * mut T :: RegType | {
174+ reg <- Opaque :: try_ffi_init( |ptr: * mut T :: DriverType | {
165175 // SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write.
166- unsafe { ptr. write( T :: RegType :: default ( ) ) } ;
176+ unsafe { ptr. write( T :: DriverType :: default ( ) ) } ;
167177
168178 // SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write, and it has
169179 // just been initialised above, so it's also valid for read.
170- let drv = unsafe { & * ( ptr as * const Opaque <T :: RegType >) } ;
180+ let drv = unsafe { & * ( ptr as * const Opaque <T :: DriverType >) } ;
171181
172182 // SAFETY: `drv` is guaranteed to be pinned until `T::unregister`.
173183 unsafe { T :: register( drv, name, module) }
0 commit comments