Skip to main content

kernel/
auxiliary.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Abstractions for the auxiliary bus.
4//!
5//! C header: [`include/linux/auxiliary_bus.h`](srctree/include/linux/auxiliary_bus.h)
6
7use crate::{
8    bindings,
9    container_of,
10    device,
11    device_id::{
12        RawDeviceId,
13        RawDeviceIdIndex, //
14    },
15    devres::Devres,
16    driver,
17    error::{
18        from_result,
19        to_result, //
20    },
21    prelude::*,
22    types::Opaque,
23    ThisModule, //
24};
25use core::{
26    marker::PhantomData,
27    mem::offset_of,
28    ptr::{
29        addr_of_mut,
30        NonNull, //
31    },
32};
33
34/// An adapter for the registration of auxiliary drivers.
35pub struct Adapter<T: Driver>(T);
36
37// SAFETY:
38// - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
39// - `T` is the type of the driver's device private data.
40// - `struct auxiliary_driver` embeds a `struct device_driver`.
41// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
43    type DriverType = bindings::auxiliary_driver;
44    type DriverData = T;
45    const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
46}
47
48// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
49// a preceding call to `register` has been successful.
50unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
51    unsafe fn register(
52        adrv: &Opaque<Self::DriverType>,
53        name: &'static CStr,
54        module: &'static ThisModule,
55    ) -> Result {
56        // SAFETY: It's safe to set the fields of `struct auxiliary_driver` on initialization.
57        unsafe {
58            (*adrv.get()).name = name.as_char_ptr();
59            (*adrv.get()).probe = Some(Self::probe_callback);
60            (*adrv.get()).remove = Some(Self::remove_callback);
61            (*adrv.get()).id_table = T::ID_TABLE.as_ptr();
62        }
63
64        // SAFETY: `adrv` is guaranteed to be a valid `DriverType`.
65        to_result(unsafe {
66            bindings::__auxiliary_driver_register(adrv.get(), module.0, name.as_char_ptr())
67        })
68    }
69
70    unsafe fn unregister(adrv: &Opaque<Self::DriverType>) {
71        // SAFETY: `adrv` is guaranteed to be a valid `DriverType`.
72        unsafe { bindings::auxiliary_driver_unregister(adrv.get()) }
73    }
74}
75
76impl<T: Driver + 'static> Adapter<T> {
77    extern "C" fn probe_callback(
78        adev: *mut bindings::auxiliary_device,
79        id: *const bindings::auxiliary_device_id,
80    ) -> c_int {
81        // SAFETY: The auxiliary bus only ever calls the probe callback with a valid pointer to a
82        // `struct auxiliary_device`.
83        //
84        // INVARIANT: `adev` is valid for the duration of `probe_callback()`.
85        let adev = unsafe { &*adev.cast::<Device<device::CoreInternal>>() };
86
87        // SAFETY: `DeviceId` is a `#[repr(transparent)`] wrapper of `struct auxiliary_device_id`
88        // and does not add additional invariants, so it's safe to transmute.
89        let id = unsafe { &*id.cast::<DeviceId>() };
90        let info = T::ID_TABLE.info(id.index());
91
92        from_result(|| {
93            let data = T::probe(adev, info);
94
95            adev.as_ref().set_drvdata(data)?;
96            Ok(0)
97        })
98    }
99
100    extern "C" fn remove_callback(adev: *mut bindings::auxiliary_device) {
101        // SAFETY: The auxiliary bus only ever calls the probe callback with a valid pointer to a
102        // `struct auxiliary_device`.
103        //
104        // INVARIANT: `adev` is valid for the duration of `remove_callback()`.
105        let adev = unsafe { &*adev.cast::<Device<device::CoreInternal>>() };
106
107        // SAFETY: `remove_callback` is only ever called after a successful call to
108        // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
109        // and stored a `Pin<KBox<T>>`.
110        let data = unsafe { adev.as_ref().drvdata_borrow::<T>() };
111
112        T::unbind(adev, data);
113    }
114}
115
116/// Declares a kernel module that exposes a single auxiliary driver.
117#[macro_export]
118macro_rules! module_auxiliary_driver {
119    ($($f:tt)*) => {
120        $crate::module_driver!(<T>, $crate::auxiliary::Adapter<T>, { $($f)* });
121    };
122}
123
124/// Abstraction for `bindings::auxiliary_device_id`.
125#[repr(transparent)]
126#[derive(Clone, Copy)]
127pub struct DeviceId(bindings::auxiliary_device_id);
128
129impl DeviceId {
130    /// Create a new [`DeviceId`] from name.
131    pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
132        let name = name.to_bytes_with_nul();
133        let modname = modname.to_bytes_with_nul();
134
135        let mut id: bindings::auxiliary_device_id = pin_init::zeroed();
136        let mut i = 0;
137        while i < modname.len() {
138            id.name[i] = modname[i];
139            i += 1;
140        }
141
142        // Reuse the space of the NULL terminator.
143        id.name[i - 1] = b'.';
144
145        let mut j = 0;
146        while j < name.len() {
147            id.name[i] = name[j];
148            i += 1;
149            j += 1;
150        }
151
152        Self(id)
153    }
154}
155
156// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `auxiliary_device_id` and does not add
157// additional invariants, so it's safe to transmute to `RawType`.
158unsafe impl RawDeviceId for DeviceId {
159    type RawType = bindings::auxiliary_device_id;
160}
161
162// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field.
163unsafe impl RawDeviceIdIndex for DeviceId {
164    const DRIVER_DATA_OFFSET: usize =
165        core::mem::offset_of!(bindings::auxiliary_device_id, driver_data);
166
167    fn index(&self) -> usize {
168        self.0.driver_data
169    }
170}
171
172/// IdTable type for auxiliary drivers.
173pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
174
175/// Create a auxiliary `IdTable` with its alias for modpost.
176#[macro_export]
177macro_rules! auxiliary_device_table {
178    ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
179        const $table_name: $crate::device_id::IdArray<
180            $crate::auxiliary::DeviceId,
181            $id_info_type,
182            { $table_data.len() },
183        > = $crate::device_id::IdArray::new($table_data);
184
185        $crate::module_device_table!("auxiliary", $module_table_name, $table_name);
186    };
187}
188
189/// The auxiliary driver trait.
190///
191/// Drivers must implement this trait in order to get an auxiliary driver registered.
192pub trait Driver {
193    /// The type holding information about each device id supported by the driver.
194    ///
195    /// TODO: Use associated_type_defaults once stabilized:
196    ///
197    /// type IdInfo: 'static = ();
198    type IdInfo: 'static;
199
200    /// The table of device ids supported by the driver.
201    const ID_TABLE: IdTable<Self::IdInfo>;
202
203    /// Auxiliary driver probe.
204    ///
205    /// Called when an auxiliary device is matches a corresponding driver.
206    fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
207
208    /// Auxiliary driver unbind.
209    ///
210    /// Called when a [`Device`] is unbound from its bound [`Driver`]. Implementing this callback
211    /// is optional.
212    ///
213    /// This callback serves as a place for drivers to perform teardown operations that require a
214    /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
215    /// operations to gracefully tear down the device.
216    ///
217    /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
218    fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
219        let _ = (dev, this);
220    }
221}
222
223/// The auxiliary device representation.
224///
225/// This structure represents the Rust abstraction for a C `struct auxiliary_device`. The
226/// implementation abstracts the usage of an already existing C `struct auxiliary_device` within
227/// Rust code that we get passed from the C side.
228///
229/// # Invariants
230///
231/// A [`Device`] instance represents a valid `struct auxiliary_device` created by the C portion of
232/// the kernel.
233#[repr(transparent)]
234pub struct Device<Ctx: device::DeviceContext = device::Normal>(
235    Opaque<bindings::auxiliary_device>,
236    PhantomData<Ctx>,
237);
238
239impl<Ctx: device::DeviceContext> Device<Ctx> {
240    fn as_raw(&self) -> *mut bindings::auxiliary_device {
241        self.0.get()
242    }
243
244    /// Returns the auxiliary device' id.
245    pub fn id(&self) -> u32 {
246        // SAFETY: By the type invariant `self.as_raw()` is a valid pointer to a
247        // `struct auxiliary_device`.
248        unsafe { (*self.as_raw()).id }
249    }
250}
251
252impl Device<device::Bound> {
253    /// Returns a bound reference to the parent [`device::Device`].
254    pub fn parent(&self) -> &device::Device<device::Bound> {
255        let parent = (**self).parent();
256
257        // SAFETY: A bound auxiliary device always has a bound parent device.
258        unsafe { parent.as_bound() }
259    }
260}
261
262impl Device {
263    /// Returns a reference to the parent [`device::Device`].
264    pub fn parent(&self) -> &device::Device {
265        // SAFETY: A `struct auxiliary_device` always has a parent.
266        unsafe { self.as_ref().parent().unwrap_unchecked() }
267    }
268
269    extern "C" fn release(dev: *mut bindings::device) {
270        // SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device`
271        // embedded in `struct auxiliary_device`.
272        let adev = unsafe { container_of!(dev, bindings::auxiliary_device, dev) };
273
274        // SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via
275        // `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`.
276        let _ = unsafe { KBox::<Opaque<bindings::auxiliary_device>>::from_raw(adev.cast()) };
277    }
278}
279
280// SAFETY: `auxiliary::Device` is a transparent wrapper of `struct auxiliary_device`.
281// The offset is guaranteed to point to a valid device field inside `auxiliary::Device`.
282unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
283    const OFFSET: usize = offset_of!(bindings::auxiliary_device, dev);
284}
285
286// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
287// argument.
288kernel::impl_device_context_deref!(unsafe { Device });
289kernel::impl_device_context_into_aref!(Device);
290
291// SAFETY: Instances of `Device` are always reference-counted.
292unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
293    fn inc_ref(&self) {
294        // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
295        unsafe { bindings::get_device(self.as_ref().as_raw()) };
296    }
297
298    unsafe fn dec_ref(obj: NonNull<Self>) {
299        // CAST: `Self` a transparent wrapper of `bindings::auxiliary_device`.
300        let adev: *mut bindings::auxiliary_device = obj.cast().as_ptr();
301
302        // SAFETY: By the type invariant of `Self`, `adev` is a pointer to a valid
303        // `struct auxiliary_device`.
304        let dev = unsafe { addr_of_mut!((*adev).dev) };
305
306        // SAFETY: The safety requirements guarantee that the refcount is non-zero.
307        unsafe { bindings::put_device(dev) }
308    }
309}
310
311impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
312    fn as_ref(&self) -> &device::Device<Ctx> {
313        // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
314        // `struct auxiliary_device`.
315        let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
316
317        // SAFETY: `dev` points to a valid `struct device`.
318        unsafe { device::Device::from_raw(dev) }
319    }
320}
321
322// SAFETY: A `Device` is always reference-counted and can be released from any thread.
323unsafe impl Send for Device {}
324
325// SAFETY: `Device` can be shared among threads because all methods of `Device`
326// (i.e. `Device<Normal>) are thread safe.
327unsafe impl Sync for Device {}
328
329/// The registration of an auxiliary device.
330///
331/// This type represents the registration of a [`struct auxiliary_device`]. When its parent device
332/// is unbound, the corresponding auxiliary device will be unregistered from the system.
333///
334/// # Invariants
335///
336/// `self.0` always holds a valid pointer to an initialized and registered
337/// [`struct auxiliary_device`].
338pub struct Registration(NonNull<bindings::auxiliary_device>);
339
340impl Registration {
341    /// Create and register a new auxiliary device.
342    pub fn new<'a>(
343        parent: &'a device::Device<device::Bound>,
344        name: &'a CStr,
345        id: u32,
346        modname: &'a CStr,
347    ) -> impl PinInit<Devres<Self>, Error> + 'a {
348        pin_init::pin_init_scope(move || {
349            let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
350            let adev = boxed.get();
351
352            // SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization.
353            unsafe {
354                (*adev).dev.parent = parent.as_raw();
355                (*adev).dev.release = Some(Device::release);
356                (*adev).name = name.as_char_ptr();
357                (*adev).id = id;
358            }
359
360            // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
361            // which has not been initialized yet.
362            unsafe { bindings::auxiliary_device_init(adev) };
363
364            // Now that `adev` is initialized, leak the `Box`; the corresponding memory will be
365            // freed by `Device::release` when the last reference to the `struct auxiliary_device`
366            // is dropped.
367            let _ = KBox::into_raw(boxed);
368
369            // SAFETY:
370            // - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which
371            //   has been initialized,
372            // - `modname.as_char_ptr()` is a NULL terminated string.
373            let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
374            if ret != 0 {
375                // SAFETY: `adev` is guaranteed to be a valid pointer to a
376                // `struct auxiliary_device`, which has been initialized.
377                unsafe { bindings::auxiliary_device_uninit(adev) };
378
379                return Err(Error::from_errno(ret));
380            }
381
382            // INVARIANT: The device will remain registered until `auxiliary_device_delete()` is
383            // called, which happens in `Self::drop()`.
384            Ok(Devres::new(
385                parent,
386                // SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated
387                // successfully.
388                Self(unsafe { NonNull::new_unchecked(adev) }),
389            ))
390        })
391    }
392}
393
394impl Drop for Registration {
395    fn drop(&mut self) {
396        // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
397        // `struct auxiliary_device`.
398        unsafe { bindings::auxiliary_device_delete(self.0.as_ptr()) };
399
400        // This drops the reference we acquired through `auxiliary_device_init()`.
401        //
402        // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
403        // `struct auxiliary_device`.
404        unsafe { bindings::auxiliary_device_uninit(self.0.as_ptr()) };
405    }
406}
407
408// SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread.
409unsafe impl Send for Registration {}
410
411// SAFETY: `Registration` does not expose any methods or fields that need synchronization.
412unsafe impl Sync for Registration {}