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. Signed-off-by: Danilo Krummrich <[email protected]> --- rust/kernel/drm/device.rs | 10 +++++++--- rust/kernel/drm/driver.rs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index c32cc0f0eba0..45bacd7b227f 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -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: By the type invariant of `Device`, the parent device is embedded in + // `T::ParentDevice`. + unsafe { device::AsBusDevice::from_device(dev) } } } diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 3cda8dceb498..ceb2829985c7 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -170,7 +170,7 @@ pub fn new_foreign_owned<'a>( where T: 'static, { - if drm.as_ref().as_raw() != dev.as_raw() { + if drm.as_ref().as_ref().as_raw() != dev.as_raw() { return Err(EINVAL); } -- 2.54.0
