Lines Matching full:device
5 //! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
18 /// The core representation of a device in the kernel's driver model.
20 /// This structure represents the Rust abstraction for a C `struct device`. A [`Device`] can either
21 /// exist as temporary reference (see also [`Device::from_raw`]), which is only valid within a
22 /// certain scope or as [`ARef<Device>`], owning a dedicated reference count.
24 /// # Device Types
26 /// A [`Device`] can represent either a bus device or a class device.
30 /// A bus device is a [`Device`] that is associated with a physical or virtual bus. Examples of
37 /// A class device is a [`Device`] that is associated with a logical category of functionality
42 /// # Device Context
44 /// [`Device`] references are generic over a [`DeviceContext`], which represents the type state of
45 /// a [`Device`].
47 /// As the name indicates, this type state represents the context of the scope the [`Device`]
48 /// reference is valid in. For instance, the [`Bound`] context guarantees that the [`Device`] is
49 /// bound to a driver for the entire duration of the existence of a [`Device<Bound>`] reference.
53 /// Unless selected otherwise [`Device`] defaults to the [`Normal`] [`DeviceContext`], which by
56 /// It is always up to the caller of [`Device::from_raw`] to select the correct [`DeviceContext`]
57 /// type for the corresponding scope the [`Device`] reference is created in.
64 /// This section provides a guideline to implement bus specific devices, such as [`pci::Device`] or
65 /// [`platform::Device`].
67 /// A bus specific device should be defined as follows.
71 /// pub struct Device<Ctx: device::DeviceContext = device::Normal>(
77 /// Since devices are reference counted, [`AlwaysRefCounted`] should be implemented for `Device`
78 /// (i.e. `Device<Normal>`). Note that [`AlwaysRefCounted`] must not be implemented for any other
79 /// [`DeviceContext`], since all other device context types are only valid within a certain scope.
81 /// In order to be able to implement the [`DeviceContext`] dereference hierarchy, bus device
85 /// // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s
87 /// kernel::impl_device_context_deref!(unsafe { Device });
90 /// In order to convert from a any [`Device<Ctx>`] to [`ARef<Device>`], bus devices can implement
94 /// kernel::impl_device_context_into_aref!(Device);
98 /// easily derive a generic [`Device`] reference.
101 /// impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
102 /// fn as_ref(&self) -> &device::Device<Ctx> {
110 /// Class device implementations require less infrastructure and depend slightly more on the
113 /// An example implementation for a class device could look like this.
117 /// pub struct Device<T: class::Driver> {
123 /// This class device uses the sub-classing pattern to embed the driver's private data within the
124 /// allocation of the class device. For this to be possible the class device is generic over the
127 /// Just like any device, class devices are reference counted and should hence implement
128 /// [`AlwaysRefCounted`] for `Device`.
131 /// easily derive a generic [`Device`] reference.
134 /// impl<T: class::Driver> AsRef<device::Device> for Device<T> {
135 /// fn as_ref(&self) -> &device::Device {
141 /// An example for a class device implementation is [`drm::Device`].
145 /// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
150 /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
154 /// [`drm::Device`]: kernel::drm::Device
156 /// [`pci::Device`]: kernel::pci::Device
157 /// [`platform::Device`]: kernel::platform::Device
159 pub struct Device<Ctx: DeviceContext = Normal>(Opaque<bindings::device>, PhantomData<Ctx>); struct
161 impl Device { impl
162 /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer.
167 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
170 /// It must also be ensured that `bindings::device::release` can be called from any thread.
171 /// While not officially documented, this should be the case for any `struct device`.
172 pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> { in get_device()
177 /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
181 /// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)
182 /// only lives as long as it can be guaranteed that the [`Device`] is actually bound.
183 pub unsafe fn as_bound(&self) -> &Device<Bound> { in as_bound()
187 // returned reference only lives as long as the device is actually bound. in as_bound()
192 // - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`. in as_bound()
197 impl Device<CoreInternal> { impl
200 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in set_drvdata()
204 /// Take ownership of the private data stored in this [`Device`].
208 /// - Must only be called once after a preceding call to [`Device::set_drvdata`].
210 /// [`Device::set_drvdata`].
212 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in drvdata_obtain()
223 /// Borrow the driver's private data bound to this [`Device`].
227 /// - Must only be called after a preceding call to [`Device::set_drvdata`] and before
228 /// [`Device::drvdata_obtain`].
230 /// [`Device::set_drvdata`].
232 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. in drvdata_borrow()
244 impl<Ctx: DeviceContext> Device<Ctx> { impl
245 /// Obtain the raw `struct device *`.
246 pub(crate) fn as_raw(&self) -> *mut bindings::device { in as_raw() argument
250 /// Returns a reference to the parent device, if any.
255 // - The parent device is only ever set at device creation. in parent()
262 // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`. in parent()
263 // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a in parent()
269 /// Convert a raw C `struct device` pointer to a `&'a Device`.
274 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to
277 pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self { in from_raw()
282 /// Prints an emergency-level message (level 0) prefixed with device information.
292 /// Prints an alert-level message (level 1) prefixed with device information.
302 /// Prints a critical-level message (level 2) prefixed with device information.
312 /// Prints an error-level message (level 3) prefixed with device information.
322 /// Prints a warning-level message (level 4) prefixed with device information.
332 /// Prints a notice-level message (level 5) prefixed with device information.
342 /// Prints an info-level message (level 6) prefixed with device information.
352 /// Prints a debug-level message (level 7) prefixed with device information.
386 /// Obtain the [`FwNode`](property::FwNode) corresponding to this [`Device`].
402 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
404 kernel::impl_device_context_deref!(unsafe { Device });
405 kernel::impl_device_context_into_aref!(Device);
407 // SAFETY: Instances of `Device` are always reference-counted.
408 unsafe impl crate::types::AlwaysRefCounted for Device { implementation
420 // SAFETY: As by the type invariant `Device` can be sent to any thread.
421 unsafe impl Send for Device {} implementation
423 // SAFETY: `Device` can be shared among threads because all immutable methods are protected by the
424 // synchronization in `struct device`.
425 unsafe impl Sync for Device {} implementation
427 /// Marker trait for the context or scope of a bus specific device.
430 /// [`Device`].
432 /// The specific device context types are: [`CoreInternal`], [`Core`], [`Bound`] and [`Normal`].
436 /// [`Device<Core>`] can dereference to a [`Device<Bound>`].
445 /// Note that the guarantee for a [`Device`] reference to have a certain [`DeviceContext`] comes
446 /// from the specific scope the [`Device`] reference is valid in.
451 /// The [`Normal`] context is the default [`DeviceContext`] of any [`Device`].
453 /// The normal context does not indicate any specific context. Any `Device<Ctx>` is also a valid
454 /// [`Device<Normal>`]. It is the only [`DeviceContext`] for which it is valid to implement
460 /// The [`Core`] context is the context of a bus specific device when it appears as argument of
463 /// The core context indicates that the [`Device<Core>`] reference's scope is limited to the bus
464 /// callback it appears in. It is intended to be used for synchronization purposes. Bus device
465 /// implementations can implement methods for [`Device<Core>`], such that they can only be called
476 /// This context mainly exists to share generic [`Device`] infrastructure that should only be called
480 /// The [`Bound`] context is the [`DeviceContext`] of a bus specific device when it is guaranteed to
483 /// The bound context indicates that for the entire duration of the lifetime of a [`Device<Bound>`]
484 /// reference, the [`Device`] is guaranteed to be bound to a driver.
486 /// Some APIs, such as [`dma::CoherentAllocation`] or [`Devres`] rely on the [`Device`] to be bound,
487 /// which can be proven with the [`Bound`] device context.
489 /// Any abstraction that can guarantee a scope where the corresponding bus device is bound, should
490 /// provide a [`Device<Bound>`] reference to its users for this scope. This allows users to benefit
491 /// from optimizations for accessing device resources, see also [`Devres::access`].
514 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the
515 /// generic argument of `$device`.
519 (unsafe { $device:ident, $src:ty => $dst:ty }) => {
520 impl ::core::ops::Deref for $device<$src> {
521 type Target = $device<$dst>;
526 // CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the
538 /// specific) device.
542 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the
543 /// generic argument of `$device`.
546 (unsafe { $device:ident }) => {
550 $device,
551 $crate::device::CoreInternal => $crate::device::Core
557 $device,
558 $crate::device::Core => $crate::device::Bound
564 $device,
565 $crate::device::Bound => $crate::device::Normal
573 ($src:ty, $device:tt) => {
574 impl ::core::convert::From<&$device<$src>> for $crate::types::ARef<$device> {
575 fn from(dev: &$device<$src>) -> Self {
582 /// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an
583 /// `ARef<Device>`.
586 ($device:tt) => {
587 ::kernel::__impl_device_context_into_aref!($crate::device::CoreInternal, $device);
588 ::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);
589 ::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device);
603 /// Prints an emergency-level message (level 0) prefixed with device information.
618 /// # use kernel::device::Device;
620 /// fn example(dev: &Device) {
629 /// Prints an alert-level message (level 1) prefixed with device information.
644 /// # use kernel::device::Device;
646 /// fn example(dev: &Device) {
655 /// Prints a critical-level message (level 2) prefixed with device information.
670 /// # use kernel::device::Device;
672 /// fn example(dev: &Device) {
681 /// Prints an error-level message (level 3) prefixed with device information.
696 /// # use kernel::device::Device;
698 /// fn example(dev: &Device) {
707 /// Prints a warning-level message (level 4) prefixed with device information.
722 /// # use kernel::device::Device;
724 /// fn example(dev: &Device) {
733 /// Prints a notice-level message (level 5) prefixed with device information.
748 /// # use kernel::device::Device;
750 /// fn example(dev: &Device) {
759 /// Prints an info-level message (level 6) prefixed with device information.
774 /// # use kernel::device::Device;
776 /// fn example(dev: &Device) {
785 /// Prints a debug-level message (level 7) prefixed with device information.
800 /// # use kernel::device::Device;
802 /// fn example(dev: &Device) {