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