rust: drm: return ParentDevice from Device AsRef

Change AsRef for drm::Device to return &T::ParentDevice<device::Normal>
instead of &device::Device, and restrict it to the Normal context.
Device<T, Registered> still gets this through Deref coercion.

This provides access to the typed parent bus device rather than the raw
base device.

Reviewed-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Tested-by: Deborah Brouwer <deborah.brouwer@collabora.com>
Link: https://patch.msgid.link/20260628145406.2107056-15-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Danilo Krummrich 2026-06-28 16:53:34 +02:00
parent 478da53e5b
commit 47f600d40b
3 changed files with 11 additions and 5 deletions

View File

@ -450,11 +450,15 @@ unsafe fn dec_ref(obj: NonNull<Self>) {
}
}
impl<T: drm::Driver, C: DeviceContext> AsRef<device::Device> for Device<T, C> {
fn as_ref(&self) -> &device::Device {
impl<T: drm::Driver> AsRef<T::ParentDevice<device::Normal>> for Device<T> {
fn as_ref(&self) -> &T::ParentDevice<device::Normal> {
// SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid,
// which is guaranteed by the type invariant.
unsafe { device::Device::from_raw((*self.as_raw()).dev) }
let dev = unsafe { device::Device::from_raw((*self.as_raw()).dev) };
// SAFETY: The DRM device was constructed in `UnregisteredDevice::new()` with a parent
// device of type `T::ParentDevice`, hence `dev` is contained in a `T::ParentDevice`.
unsafe { device::AsBusDevice::from_device(dev) }
}
}

View File

@ -170,7 +170,8 @@ pub fn new_foreign_owned<'a>(
where
T: 'static,
{
if drm.as_ref().as_raw() != dev.as_raw() {
let parent = drm.as_ref();
if parent.as_ref().as_raw() != dev.as_raw() {
return Err(EINVAL);
}

View File

@ -264,7 +264,8 @@ pub fn sg_table<'a>(
&'a self,
dev: &'a device::Device<Bound>,
) -> Result<&'a scatterlist::SGTable> {
if dev.as_raw() != self.dev().as_ref().as_raw() {
let parent = self.dev().as_ref();
if dev.as_raw() != parent.as_ref().as_raw() {
return Err(EINVAL);
}