Lines Matching full:device
5 //! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
19 /// The core representation of a device in the kernel's driver model.
21 /// This structure represents the Rust abstraction for a C `struct device`. A [`Device`] can either
22 /// exist as temporary reference (see also [`Device::from_raw`]), which is only valid within a
23 /// certain scope or as [`ARef<Device>`], owning a dedicated reference count.
25 /// # Device Types
27 /// A [`Device`] can represent either a bus device or a class device.
31 /// A bus device is a [`Device`] that is associated with a physical or virtual bus. Examples of
38 /// A class device is a [`Device`] that is associated with a logical category of functionality
43 /// # Device Context
45 /// [`Device`] references are generic over a [`DeviceContext`], which represents the type state of
46 /// a [`Device`].
48 /// As the name indicates, this type state represents the context of the scope the [`Device`]
49 /// reference is valid in. For instance, the [`Bound`] context guarantees that the [`Device`] is
50 /// bound to a driver for the entire duration of the existence of a [`Device<Bound>`] reference.
54 /// Unless selected otherwise [`Device`] defaults to the [`Normal`] [`DeviceContext`], which by
57 /// It is always up to the caller of [`Device::from_raw`] to select the correct [`DeviceContext`]
58 /// type for the corresponding scope the [`Device`] reference is created in.
65 /// This section provides a guideline to implement bus specific devices, such as [`pci::Device`] or
66 /// [`platform::Device`].
68 /// A bus specific device should be defined as follows.
72 /// pub struct Device<Ctx: device::DeviceContext = device::Normal>(
78 /// Since devices are reference counted, [`AlwaysRefCounted`] should be implemented for `Device`
79 /// (i.e. `Device<Normal>`). Note that [`AlwaysRefCounted`] must not be implemented for any other
80 /// [`DeviceContext`], since all other device context types are only valid within a certain scope.
82 /// In order to be able to implement the [`DeviceContext`] dereference hierarchy, bus device
86 /// // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s
88 /// kernel::impl_device_context_deref!(unsafe { Device });
91 /// In order to convert from a any [`Device<Ctx>`] to [`ARef<Device>`], bus devices can implement
95 /// kernel::impl_device_context_into_aref!(Device);
99 /// easily derive a generic [`Device`] reference.
102 /// impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
103 /// fn as_ref(&self) -> &device::Device<Ctx> {
111 /// Class device implementations require less infrastructure and depend slightly more on the
114 /// An example implementation for a class device could look like this.
118 /// pub struct Device<T: class::Driver> {
124 /// This class device uses the sub-classing pattern to embed the driver's private data within the
125 /// allocation of the class device. For this to be possible the class device is generic over the
128 /// Just like any device, class devices are reference counted and should hence implement
129 /// [`AlwaysRefCounted`] for `Device`.
132 /// easily derive a generic [`Device`] reference.
135 /// impl<T: class::Driver> AsRef<device::Device> for Device<T> {
136 /// fn as_ref(&self) -> &device::Device {
142 /// An example for a class device implementation is
143 #[cfg_attr(CONFIG_DRM = "y", doc = "[`drm::Device`](kernel::drm::Device).")]
144 #[cfg_attr(not(CONFIG_DRM = "y"), doc = "`drm::Device`.")]
148 /// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
153 /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
158 /// [`pci::Device`]: kernel::pci::Device
159 /// [`platform::Device`]: kernel::platform::Device
161 pub struct Device<Ctx: DeviceContext = Normal>(Opaque<bindings::device>, PhantomData<Ctx>); struct
163 impl Device { implementation
164 /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer.
169 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
172 /// It must also be ensured that `bindings::device::release` can be called from any thread.
173 /// While not officially documented, this should be the case for any `struct device`.
174 pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> { in get_device()
179 /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
183 /// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)
184 /// only lives as long as it can be guaranteed that the [`Device`] is actually bound.
185 pub unsafe fn as_bound(&self) -> &Device<Bound> { in as_bound()
189 // returned reference only lives as long as the device is actually bound. in as_bound()
194 // - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`. in as_bound()
199 impl Device<CoreInternal> { implementation
202 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in set_drvdata()
206 /// Take ownership of the private data stored in this [`Device`].
210 /// - Must only be called once after a preceding call to [`Device::set_drvdata`].
212 /// [`Device::set_drvdata`].
214 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in drvdata_obtain()
225 /// Borrow the driver's private data bound to this [`Device`].
229 /// - Must only be called after a preceding call to [`Device::set_drvdata`] and before
230 /// [`Device::drvdata_obtain`].
232 /// [`Device::set_drvdata`].
234 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in drvdata_borrow()
246 impl<Ctx: DeviceContext> Device<Ctx> { impl
247 /// Obtain the raw `struct device *`.
248 pub(crate) fn as_raw(&self) -> *mut bindings::device { in as_raw() argument
252 /// Returns a reference to the parent device, if any.
254 pub(crate) fn parent(&self) -> Option<&Device> { in parent() argument
257 // - The parent device is only ever set at device creation. in parent()
264 // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`. in parent()
265 // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a in parent()
267 Some(unsafe { Device::from_raw(parent) }) in parent()
271 /// Convert a raw C `struct device` pointer to a `&'a Device`.
276 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
279 pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self { in from_raw()
284 /// Prints an emergency-level message (level 0) prefixed with device information.
294 /// Prints an alert-level message (level 1) prefixed with device information.
304 /// Prints a critical-level message (level 2) prefixed with device information.
314 /// Prints an error-level message (level 3) prefixed with device information.
324 /// Prints a warning-level message (level 4) prefixed with device information.
334 /// Prints a notice-level message (level 5) prefixed with device information.
344 /// Prints an info-level message (level 6) prefixed with device information.
354 /// Prints a debug-level message (level 7) prefixed with device information.
388 /// Obtain the [`FwNode`](property::FwNode) corresponding to this [`Device`].
404 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
406 kernel::impl_device_context_deref!(unsafe { Device });
407 kernel::impl_device_context_into_aref!(Device);
409 // SAFETY: Instances of `Device` are always reference-counted.
410 unsafe impl crate::sync::aref::AlwaysRefCounted for Device { implementation
422 // SAFETY: As by the type invariant `Device` can be sent to any thread.
423 unsafe impl Send for Device {} implementation
425 // SAFETY: `Device` can be shared among threads because all immutable methods are protected by the
426 // synchronization in `struct device`.
427 unsafe impl Sync for Device {} implementation
429 /// Marker trait for the context or scope of a bus specific device.
432 /// [`Device`].
434 /// The specific device context types are: [`CoreInternal`], [`Core`], [`Bound`] and [`Normal`].
438 /// [`Device<Core>`] can dereference to a [`Device<Bound>`].
447 /// Note that the guarantee for a [`Device`] reference to have a certain [`DeviceContext`] comes
448 /// from the specific scope the [`Device`] reference is valid in.
453 /// The [`Normal`] context is the default [`DeviceContext`] of any [`Device`].
455 /// The normal context does not indicate any specific context. Any `Device<Ctx>` is also a valid
456 /// [`Device<Normal>`]. It is the only [`DeviceContext`] for which it is valid to implement
462 /// The [`Core`] context is the context of a bus specific device when it appears as argument of
465 /// The core context indicates that the [`Device<Core>`] reference's scope is limited to the bus
466 /// callback it appears in. It is intended to be used for synchronization purposes. Bus device
467 /// implementations can implement methods for [`Device<Core>`], such that they can only be called
478 /// This context mainly exists to share generic [`Device`] infrastructure that should only be called
482 /// The [`Bound`] context is the [`DeviceContext`] of a bus specific device when it is guaranteed to
485 /// The bound context indicates that for the entire duration of the lifetime of a [`Device<Bound>`]
486 /// reference, the [`Device`] is guaranteed to be bound to a driver.
488 /// Some APIs, such as [`dma::CoherentAllocation`] or [`Devres`] rely on the [`Device`] to be bound,
489 /// which can be proven with the [`Bound`] device context.
491 /// Any abstraction that can guarantee a scope where the corresponding bus device is bound, should
492 /// provide a [`Device<Bound>`] reference to its users for this scope. This allows users to benefit
493 /// from optimizations for accessing device resources, see also [`Devres::access`].
516 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the
517 /// generic argument of `$device`.
521 (unsafe { $device:ident, $src:ty => $dst:ty }) => {
522 impl ::core::ops::Deref for $device<$src> {
523 type Target = $device<$dst>;
528 // CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the
540 /// specific) device.
544 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the
545 /// generic argument of `$device`.
548 (unsafe { $device:ident }) => {
552 $device,
553 $crate::device::CoreInternal => $crate::device::Core
559 $device,
560 $crate::device::Core => $crate::device::Bound
566 $device,
567 $crate::device::Bound => $crate::device::Normal
575 ($src:ty, $device:tt) => {
576 impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
577 fn from(dev: &$device<$src>) -> Self {
584 /// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an
585 /// `ARef<Device>`.
588 ($device:tt) => {
589 ::kernel::__impl_device_context_into_aref!($crate::device::CoreInternal, $device);
590 ::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);
591 ::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device);
605 /// Prints an emergency-level message (level 0) prefixed with device information.
620 /// # use kernel::device::Device;
622 /// fn example(dev: &Device) {
631 /// Prints an alert-level message (level 1) prefixed with device information.
646 /// # use kernel::device::Device;
648 /// fn example(dev: &Device) {
657 /// Prints a critical-level message (level 2) prefixed with device information.
672 /// # use kernel::device::Device;
674 /// fn example(dev: &Device) {
683 /// Prints an error-level message (level 3) prefixed with device information.
698 /// # use kernel::device::Device;
700 /// fn example(dev: &Device) {
709 /// Prints a warning-level message (level 4) prefixed with device information.
724 /// # use kernel::device::Device;
726 /// fn example(dev: &Device) {
735 /// Prints a notice-level message (level 5) prefixed with device information.
750 /// # use kernel::device::Device;
752 /// fn example(dev: &Device) {
761 /// Prints an info-level message (level 6) prefixed with device information.
776 /// # use kernel::device::Device;
778 /// fn example(dev: &Device) {
787 /// Prints a debug-level message (level 7) prefixed with device information.
802 /// # use kernel::device::Device;
804 /// fn example(dev: &Device) {