diff --git a/rust/kernel/drm/kms/connector.rs b/rust/kernel/drm/kms/connector.rs index 98ac7fb781d4e4f13be960409aeb7be6c78518a4..f62740d7f6469b58a26e4fb27e62bd3c9b6e9930 100644 --- a/rust/kernel/drm/kms/connector.rs +++ b/rust/kernel/drm/kms/connector.rs @@ -331,6 +331,27 @@ impl<T: DriverConnector> AsRawConnector for Connector<T> { } } +/// Common methods available on any type which implements [`AsRawConnector`]. +/// +/// This is implemented internally by DRM, and provides many of the basic methods for working with +/// connectors. +pub trait RawConnector: AsRawConnector { + /// Return the index of this DRM connector + #[inline] + fn index(&self) -> u32 { + // SAFETY: The index is initialized by the time we expose DRM connector objects to users, + // and is invariant throughout the lifetime of the connector + unsafe { (*self.as_raw()).index } + } + + /// Return the bitmask derived from this DRM connector's index + #[inline] + fn mask(&self) -> u32 { + 1 << self.index() + } +} +impl<T: AsRawConnector> RawConnector for T {} + unsafe extern "C" fn connector_destroy_callback<T: DriverConnector>( connector: *mut bindings::drm_connector, ) { @@ -523,6 +544,19 @@ pub trait FromRawConnectorState: AsRawConnectorState { unsafe fn from_raw_mut<'a>(ptr: *mut bindings::drm_connector_state) -> &'a mut Self; } +/// Common methods available on any type which implements [`AsRawConnectorState`]. +/// +/// This is implemented internally by DRM, and provides many of the basic methods for working with +/// the atomic state of [`Connector`]s. +pub trait RawConnectorState: AsRawConnectorState { + fn connector(&self) -> &Self::Connector { + // SAFETY: This is guaranteed safe by type invariance, and we're guaranteed by DRM that + // `self.state.connector` points to a valid instance of a `Connector<T>` + unsafe { Self::Connector::from_raw((*self.as_raw()).connector) } + } +} +impl<T: AsRawConnectorState> RawConnectorState for T {} + /// The main interface for a [`struct drm_connector_state`]. /// /// This type is the main interface for dealing with the atomic state of DRM connectors. In