xref: /linux/rust/kernel/platform.rs (revision eb3dad518e4da48ab6c6df16aa8895b8b0bd6ecf)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Abstractions for the platform bus.
4 //!
5 //! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h)
6 
7 use crate::{
8     acpi,
9     bindings,
10     container_of,
11     device::{
12         self,
13         Bound, //
14     },
15     driver,
16     error::{
17         from_result,
18         to_result, //
19     },
20     io::{
21         mem::IoRequest,
22         Resource, //
23     },
24     irq::{
25         self,
26         IrqRequest, //
27     },
28     of,
29     prelude::*,
30     types::Opaque,
31     ThisModule, //
32 };
33 
34 use core::{
35     marker::PhantomData,
36     mem::offset_of,
37     ptr::{
38         addr_of_mut,
39         NonNull, //
40     },
41 };
42 
43 /// An adapter for the registration of platform drivers.
44 pub struct Adapter<T: Driver>(T);
45 
46 // SAFETY:
47 // - `bindings::platform_driver` is a C type declared as `repr(C)`.
48 // - `T` is the type of the driver's device private data.
49 // - `struct platform_driver` embeds a `struct device_driver`.
50 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
51 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
52     type DriverType = bindings::platform_driver;
53     type DriverData = T;
54     const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
55 }
56 
57 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
58 // a preceding call to `register` has been successful.
59 unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
60     unsafe fn register(
61         pdrv: &Opaque<Self::DriverType>,
62         name: &'static CStr,
63         module: &'static ThisModule,
64     ) -> Result {
65         let of_table = match T::OF_ID_TABLE {
66             Some(table) => table.as_ptr(),
67             None => core::ptr::null(),
68         };
69 
70         let acpi_table = match T::ACPI_ID_TABLE {
71             Some(table) => table.as_ptr(),
72             None => core::ptr::null(),
73         };
74 
75         // SAFETY: It's safe to set the fields of `struct platform_driver` on initialization.
76         unsafe {
77             (*pdrv.get()).driver.name = name.as_char_ptr();
78             (*pdrv.get()).probe = Some(Self::probe_callback);
79             (*pdrv.get()).remove = Some(Self::remove_callback);
80             (*pdrv.get()).driver.of_match_table = of_table;
81             (*pdrv.get()).driver.acpi_match_table = acpi_table;
82         }
83 
84         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
85         to_result(unsafe { bindings::__platform_driver_register(pdrv.get(), module.0) })
86     }
87 
88     unsafe fn unregister(pdrv: &Opaque<Self::DriverType>) {
89         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
90         unsafe { bindings::platform_driver_unregister(pdrv.get()) };
91     }
92 }
93 
94 impl<T: Driver + 'static> Adapter<T> {
95     extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ffi::c_int {
96         // SAFETY: The platform bus only ever calls the probe callback with a valid pointer to a
97         // `struct platform_device`.
98         //
99         // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
100         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
101         let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
102 
103         from_result(|| {
104             let data = T::probe(pdev, info);
105 
106             pdev.as_ref().set_drvdata(data)?;
107             Ok(0)
108         })
109     }
110 
111     extern "C" fn remove_callback(pdev: *mut bindings::platform_device) {
112         // SAFETY: The platform bus only ever calls the remove callback with a valid pointer to a
113         // `struct platform_device`.
114         //
115         // INVARIANT: `pdev` is valid for the duration of `remove_callback()`.
116         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
117 
118         // SAFETY: `remove_callback` is only ever called after a successful call to
119         // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
120         // and stored a `Pin<KBox<T>>`.
121         let data = unsafe { pdev.as_ref().drvdata_borrow::<T>() };
122 
123         T::unbind(pdev, data);
124     }
125 }
126 
127 impl<T: Driver + 'static> driver::Adapter for Adapter<T> {
128     type IdInfo = T::IdInfo;
129 
130     fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
131         T::OF_ID_TABLE
132     }
133 
134     fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>> {
135         T::ACPI_ID_TABLE
136     }
137 }
138 
139 /// Declares a kernel module that exposes a single platform driver.
140 ///
141 /// # Examples
142 ///
143 /// ```ignore
144 /// kernel::module_platform_driver! {
145 ///     type: MyDriver,
146 ///     name: "Module name",
147 ///     authors: ["Author name"],
148 ///     description: "Description",
149 ///     license: "GPL v2",
150 /// }
151 /// ```
152 #[macro_export]
153 macro_rules! module_platform_driver {
154     ($($f:tt)*) => {
155         $crate::module_driver!(<T>, $crate::platform::Adapter<T>, { $($f)* });
156     };
157 }
158 
159 /// The platform driver trait.
160 ///
161 /// Drivers must implement this trait in order to get a platform driver registered.
162 ///
163 /// # Examples
164 ///
165 ///```
166 /// # use kernel::{
167 /// #     acpi,
168 /// #     bindings,
169 /// #     device::Core,
170 /// #     of,
171 /// #     platform,
172 /// # };
173 /// struct MyDriver;
174 ///
175 /// kernel::of_device_table!(
176 ///     OF_TABLE,
177 ///     MODULE_OF_TABLE,
178 ///     <MyDriver as platform::Driver>::IdInfo,
179 ///     [
180 ///         (of::DeviceId::new(c"test,device"), ())
181 ///     ]
182 /// );
183 ///
184 /// kernel::acpi_device_table!(
185 ///     ACPI_TABLE,
186 ///     MODULE_ACPI_TABLE,
187 ///     <MyDriver as platform::Driver>::IdInfo,
188 ///     [
189 ///         (acpi::DeviceId::new(c"LNUXBEEF"), ())
190 ///     ]
191 /// );
192 ///
193 /// impl platform::Driver for MyDriver {
194 ///     type IdInfo = ();
195 ///     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
196 ///     const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
197 ///
198 ///     fn probe(
199 ///         _pdev: &platform::Device<Core>,
200 ///         _id_info: Option<&Self::IdInfo>,
201 ///     ) -> impl PinInit<Self, Error> {
202 ///         Err(ENODEV)
203 ///     }
204 /// }
205 ///```
206 pub trait Driver: Send {
207     /// The type holding driver private data about each device id supported by the driver.
208     // TODO: Use associated_type_defaults once stabilized:
209     //
210     // ```
211     // type IdInfo: 'static = ();
212     // ```
213     type IdInfo: 'static;
214 
215     /// The table of OF device ids supported by the driver.
216     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
217 
218     /// The table of ACPI device ids supported by the driver.
219     const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = None;
220 
221     /// Platform driver probe.
222     ///
223     /// Called when a new platform device is added or discovered.
224     /// Implementers should attempt to initialize the device here.
225     fn probe(
226         dev: &Device<device::Core>,
227         id_info: Option<&Self::IdInfo>,
228     ) -> impl PinInit<Self, Error>;
229 
230     /// Platform driver unbind.
231     ///
232     /// Called when a [`Device`] is unbound from its bound [`Driver`]. Implementing this callback
233     /// is optional.
234     ///
235     /// This callback serves as a place for drivers to perform teardown operations that require a
236     /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
237     /// operations to gracefully tear down the device.
238     ///
239     /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
240     fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
241         let _ = (dev, this);
242     }
243 }
244 
245 /// The platform device representation.
246 ///
247 /// This structure represents the Rust abstraction for a C `struct platform_device`. The
248 /// implementation abstracts the usage of an already existing C `struct platform_device` within Rust
249 /// code that we get passed from the C side.
250 ///
251 /// # Invariants
252 ///
253 /// A [`Device`] instance represents a valid `struct platform_device` created by the C portion of
254 /// the kernel.
255 #[repr(transparent)]
256 pub struct Device<Ctx: device::DeviceContext = device::Normal>(
257     Opaque<bindings::platform_device>,
258     PhantomData<Ctx>,
259 );
260 
261 impl<Ctx: device::DeviceContext> Device<Ctx> {
262     fn as_raw(&self) -> *mut bindings::platform_device {
263         self.0.get()
264     }
265 
266     /// Returns the resource at `index`, if any.
267     pub fn resource_by_index(&self, index: u32) -> Option<&Resource> {
268         // SAFETY: `self.as_raw()` returns a valid pointer to a `struct platform_device`.
269         let resource = unsafe {
270             bindings::platform_get_resource(self.as_raw(), bindings::IORESOURCE_MEM, index)
271         };
272 
273         if resource.is_null() {
274             return None;
275         }
276 
277         // SAFETY: `resource` is a valid pointer to a `struct resource` as
278         // returned by `platform_get_resource`.
279         Some(unsafe { Resource::from_raw(resource) })
280     }
281 
282     /// Returns the resource with a given `name`, if any.
283     pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> {
284         // SAFETY: `self.as_raw()` returns a valid pointer to a `struct
285         // platform_device` and `name` points to a valid C string.
286         let resource = unsafe {
287             bindings::platform_get_resource_byname(
288                 self.as_raw(),
289                 bindings::IORESOURCE_MEM,
290                 name.as_char_ptr(),
291             )
292         };
293 
294         if resource.is_null() {
295             return None;
296         }
297 
298         // SAFETY: `resource` is a valid pointer to a `struct resource` as
299         // returned by `platform_get_resource`.
300         Some(unsafe { Resource::from_raw(resource) })
301     }
302 }
303 
304 impl Device<Bound> {
305     /// Returns an `IoRequest` for the resource at `index`, if any.
306     pub fn io_request_by_index(&self, index: u32) -> Option<IoRequest<'_>> {
307         self.resource_by_index(index)
308             // SAFETY: `resource` is a valid resource for `&self` during the
309             // lifetime of the `IoRequest`.
310             .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
311     }
312 
313     /// Returns an `IoRequest` for the resource with a given `name`, if any.
314     pub fn io_request_by_name(&self, name: &CStr) -> Option<IoRequest<'_>> {
315         self.resource_by_name(name)
316             // SAFETY: `resource` is a valid resource for `&self` during the
317             // lifetime of the `IoRequest`.
318             .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
319     }
320 }
321 
322 // SAFETY: `platform::Device` is a transparent wrapper of `struct platform_device`.
323 // The offset is guaranteed to point to a valid device field inside `platform::Device`.
324 unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
325     const OFFSET: usize = offset_of!(bindings::platform_device, dev);
326 }
327 
328 macro_rules! define_irq_accessor_by_index {
329     (
330         $(#[$meta:meta])* $fn_name:ident,
331         $request_fn:ident,
332         $reg_type:ident,
333         $handler_trait:ident
334     ) => {
335         $(#[$meta])*
336         pub fn $fn_name<'a, T: irq::$handler_trait + 'static>(
337             &'a self,
338             flags: irq::Flags,
339             index: u32,
340             name: &'static CStr,
341             handler: impl PinInit<T, Error> + 'a,
342         ) -> impl PinInit<irq::$reg_type<T>, Error> + 'a {
343             pin_init::pin_init_scope(move || {
344                 let request = self.$request_fn(index)?;
345 
346                 Ok(irq::$reg_type::<T>::new(
347                     request,
348                     flags,
349                     name,
350                     handler,
351                 ))
352             })
353         }
354     };
355 }
356 
357 macro_rules! define_irq_accessor_by_name {
358     (
359         $(#[$meta:meta])* $fn_name:ident,
360         $request_fn:ident,
361         $reg_type:ident,
362         $handler_trait:ident
363     ) => {
364         $(#[$meta])*
365         pub fn $fn_name<'a, T: irq::$handler_trait + 'static>(
366             &'a self,
367             flags: irq::Flags,
368             irq_name: &'a CStr,
369             name: &'static CStr,
370             handler: impl PinInit<T, Error> + 'a,
371         ) -> impl PinInit<irq::$reg_type<T>, Error> + 'a {
372             pin_init::pin_init_scope(move || {
373                 let request = self.$request_fn(irq_name)?;
374 
375                 Ok(irq::$reg_type::<T>::new(
376                     request,
377                     flags,
378                     name,
379                     handler,
380                 ))
381             })
382         }
383     };
384 }
385 
386 impl Device<Bound> {
387     /// Returns an [`IrqRequest`] for the IRQ at the given index, if any.
388     pub fn irq_by_index(&self, index: u32) -> Result<IrqRequest<'_>> {
389         // SAFETY: `self.as_raw` returns a valid pointer to a `struct platform_device`.
390         let irq = unsafe { bindings::platform_get_irq(self.as_raw(), index) };
391 
392         if irq < 0 {
393             return Err(Error::from_errno(irq));
394         }
395 
396         // SAFETY: `irq` is guaranteed to be a valid IRQ number for `&self`.
397         Ok(unsafe { IrqRequest::new(self.as_ref(), irq as u32) })
398     }
399 
400     /// Returns an [`IrqRequest`] for the IRQ at the given index, but does not
401     /// print an error if the IRQ cannot be obtained.
402     pub fn optional_irq_by_index(&self, index: u32) -> Result<IrqRequest<'_>> {
403         // SAFETY: `self.as_raw` returns a valid pointer to a `struct platform_device`.
404         let irq = unsafe { bindings::platform_get_irq_optional(self.as_raw(), index) };
405 
406         if irq < 0 {
407             return Err(Error::from_errno(irq));
408         }
409 
410         // SAFETY: `irq` is guaranteed to be a valid IRQ number for `&self`.
411         Ok(unsafe { IrqRequest::new(self.as_ref(), irq as u32) })
412     }
413 
414     /// Returns an [`IrqRequest`] for the IRQ with the given name, if any.
415     pub fn irq_by_name(&self, name: &CStr) -> Result<IrqRequest<'_>> {
416         // SAFETY: `self.as_raw` returns a valid pointer to a `struct platform_device`.
417         let irq = unsafe { bindings::platform_get_irq_byname(self.as_raw(), name.as_char_ptr()) };
418 
419         if irq < 0 {
420             return Err(Error::from_errno(irq));
421         }
422 
423         // SAFETY: `irq` is guaranteed to be a valid IRQ number for `&self`.
424         Ok(unsafe { IrqRequest::new(self.as_ref(), irq as u32) })
425     }
426 
427     /// Returns an [`IrqRequest`] for the IRQ with the given name, but does not
428     /// print an error if the IRQ cannot be obtained.
429     pub fn optional_irq_by_name(&self, name: &CStr) -> Result<IrqRequest<'_>> {
430         // SAFETY: `self.as_raw` returns a valid pointer to a `struct platform_device`.
431         let irq = unsafe {
432             bindings::platform_get_irq_byname_optional(self.as_raw(), name.as_char_ptr())
433         };
434 
435         if irq < 0 {
436             return Err(Error::from_errno(irq));
437         }
438 
439         // SAFETY: `irq` is guaranteed to be a valid IRQ number for `&self`.
440         Ok(unsafe { IrqRequest::new(self.as_ref(), irq as u32) })
441     }
442 
443     define_irq_accessor_by_index!(
444         /// Returns a [`irq::Registration`] for the IRQ at the given index.
445         request_irq_by_index,
446         irq_by_index,
447         Registration,
448         Handler
449     );
450     define_irq_accessor_by_name!(
451         /// Returns a [`irq::Registration`] for the IRQ with the given name.
452         request_irq_by_name,
453         irq_by_name,
454         Registration,
455         Handler
456     );
457     define_irq_accessor_by_index!(
458         /// Does the same as [`Self::request_irq_by_index`], except that it does
459         /// not print an error message if the IRQ cannot be obtained.
460         request_optional_irq_by_index,
461         optional_irq_by_index,
462         Registration,
463         Handler
464     );
465     define_irq_accessor_by_name!(
466         /// Does the same as [`Self::request_irq_by_name`], except that it does
467         /// not print an error message if the IRQ cannot be obtained.
468         request_optional_irq_by_name,
469         optional_irq_by_name,
470         Registration,
471         Handler
472     );
473 
474     define_irq_accessor_by_index!(
475         /// Returns a [`irq::ThreadedRegistration`] for the IRQ at the given index.
476         request_threaded_irq_by_index,
477         irq_by_index,
478         ThreadedRegistration,
479         ThreadedHandler
480     );
481     define_irq_accessor_by_name!(
482         /// Returns a [`irq::ThreadedRegistration`] for the IRQ with the given name.
483         request_threaded_irq_by_name,
484         irq_by_name,
485         ThreadedRegistration,
486         ThreadedHandler
487     );
488     define_irq_accessor_by_index!(
489         /// Does the same as [`Self::request_threaded_irq_by_index`], except
490         /// that it does not print an error message if the IRQ cannot be
491         /// obtained.
492         request_optional_threaded_irq_by_index,
493         optional_irq_by_index,
494         ThreadedRegistration,
495         ThreadedHandler
496     );
497     define_irq_accessor_by_name!(
498         /// Does the same as [`Self::request_threaded_irq_by_name`], except that
499         /// it does not print an error message if the IRQ cannot be obtained.
500         request_optional_threaded_irq_by_name,
501         optional_irq_by_name,
502         ThreadedRegistration,
503         ThreadedHandler
504     );
505 }
506 
507 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
508 // argument.
509 kernel::impl_device_context_deref!(unsafe { Device });
510 kernel::impl_device_context_into_aref!(Device);
511 
512 impl crate::dma::Device for Device<device::Core> {}
513 
514 // SAFETY: Instances of `Device` are always reference-counted.
515 unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
516     fn inc_ref(&self) {
517         // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
518         unsafe { bindings::get_device(self.as_ref().as_raw()) };
519     }
520 
521     unsafe fn dec_ref(obj: NonNull<Self>) {
522         // SAFETY: The safety requirements guarantee that the refcount is non-zero.
523         unsafe { bindings::platform_device_put(obj.cast().as_ptr()) }
524     }
525 }
526 
527 impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
528     fn as_ref(&self) -> &device::Device<Ctx> {
529         // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
530         // `struct platform_device`.
531         let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
532 
533         // SAFETY: `dev` points to a valid `struct device`.
534         unsafe { device::Device::from_raw(dev) }
535     }
536 }
537 
538 impl<Ctx: device::DeviceContext> TryFrom<&device::Device<Ctx>> for &Device<Ctx> {
539     type Error = kernel::error::Error;
540 
541     fn try_from(dev: &device::Device<Ctx>) -> Result<Self, Self::Error> {
542         // SAFETY: By the type invariant of `Device`, `dev.as_raw()` is a valid pointer to a
543         // `struct device`.
544         if !unsafe { bindings::dev_is_platform(dev.as_raw()) } {
545             return Err(EINVAL);
546         }
547 
548         // SAFETY: We've just verified that the bus type of `dev` equals
549         // `bindings::platform_bus_type`, hence `dev` must be embedded in a valid
550         // `struct platform_device` as guaranteed by the corresponding C code.
551         let pdev = unsafe { container_of!(dev.as_raw(), bindings::platform_device, dev) };
552 
553         // SAFETY: `pdev` is a valid pointer to a `struct platform_device`.
554         Ok(unsafe { &*pdev.cast() })
555     }
556 }
557 
558 // SAFETY: A `Device` is always reference-counted and can be released from any thread.
559 unsafe impl Send for Device {}
560 
561 // SAFETY: `Device` can be shared among threads because all methods of `Device`
562 // (i.e. `Device<Normal>) are thread safe.
563 unsafe impl Sync for Device {}
564