device.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Generic devices that are part of the kernel's driver model.
  3. //!
  4. //! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
  5. use crate::{
  6. bindings,
  7. types::{ARef, Opaque},
  8. };
  9. use core::ptr;
  10. /// A reference-counted device.
  11. ///
  12. /// This structure represents the Rust abstraction for a C `struct device`. This implementation
  13. /// abstracts the usage of an already existing C `struct device` within Rust code that we get
  14. /// passed from the C side.
  15. ///
  16. /// An instance of this abstraction can be obtained temporarily or permanent.
  17. ///
  18. /// A temporary one is bound to the lifetime of the C `struct device` pointer used for creation.
  19. /// A permanent instance is always reference-counted and hence not restricted by any lifetime
  20. /// boundaries.
  21. ///
  22. /// For subsystems it is recommended to create a permanent instance to wrap into a subsystem
  23. /// specific device structure (e.g. `pci::Device`). This is useful for passing it to drivers in
  24. /// `T::probe()`, such that a driver can store the `ARef<Device>` (equivalent to storing a
  25. /// `struct device` pointer in a C driver) for arbitrary purposes, e.g. allocating DMA coherent
  26. /// memory.
  27. ///
  28. /// # Invariants
  29. ///
  30. /// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
  31. ///
  32. /// Instances of this type are always reference-counted, that is, a call to `get_device` ensures
  33. /// that the allocation remains valid at least until the matching call to `put_device`.
  34. ///
  35. /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
  36. /// dropped from any thread.
  37. #[repr(transparent)]
  38. pub struct Device(Opaque<bindings::device>);
  39. impl Device {
  40. /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer.
  41. ///
  42. /// # Safety
  43. ///
  44. /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count,
  45. /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
  46. /// can't drop to zero, for the duration of this function call.
  47. ///
  48. /// It must also be ensured that `bindings::device::release` can be called from any thread.
  49. /// While not officially documented, this should be the case for any `struct device`.
  50. pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
  51. // SAFETY: By the safety requirements ptr is valid
  52. unsafe { Self::as_ref(ptr) }.into()
  53. }
  54. /// Obtain the raw `struct device *`.
  55. pub(crate) fn as_raw(&self) -> *mut bindings::device {
  56. self.0.get()
  57. }
  58. /// Convert a raw C `struct device` pointer to a `&'a Device`.
  59. ///
  60. /// # Safety
  61. ///
  62. /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count,
  63. /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
  64. /// can't drop to zero, for the duration of this function call and the entire duration when the
  65. /// returned reference exists.
  66. pub unsafe fn as_ref<'a>(ptr: *mut bindings::device) -> &'a Self {
  67. // SAFETY: Guaranteed by the safety requirements of the function.
  68. unsafe { &*ptr.cast() }
  69. }
  70. }
  71. // SAFETY: Instances of `Device` are always reference-counted.
  72. unsafe impl crate::types::AlwaysRefCounted for Device {
  73. fn inc_ref(&self) {
  74. // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
  75. unsafe { bindings::get_device(self.as_raw()) };
  76. }
  77. unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
  78. // SAFETY: The safety requirements guarantee that the refcount is non-zero.
  79. unsafe { bindings::put_device(obj.cast().as_ptr()) }
  80. }
  81. }
  82. // SAFETY: As by the type invariant `Device` can be sent to any thread.
  83. unsafe impl Send for Device {}
  84. // SAFETY: `Device` can be shared among threads because all immutable methods are protected by the
  85. // synchronization in `struct device`.
  86. unsafe impl Sync for Device {}