kernel/drm/file.rs
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2
3//! DRM File objects.
4//!
5//! C header: [`include/drm/drm_file.h`](srctree/include/drm/drm_file.h)
6
7use crate::{bindings, drm, error::Result, prelude::*, types::Opaque};
8use core::marker::PhantomData;
9use core::pin::Pin;
10use safety_macro::safety;
11/// Trait that must be implemented by DRM drivers to represent a DRM File (a client instance).
12pub trait DriverFile {
13 /// The parent `Driver` implementation for this `DriverFile`.
14 type Driver: drm::Driver;
15
16 /// Open a new file (called when a client opens the DRM device).
17 fn open(device: &drm::Device<Self::Driver>) -> Result<Pin<KBox<Self>>>;
18}
19
20/// An open DRM File.
21///
22/// # Invariants
23///
24/// `self.0` is a valid instance of a `struct drm_file`.
25#[repr(transparent)]
26pub struct File<T: DriverFile>(Opaque<bindings::drm_file>, PhantomData<T>);
27
28impl<T: DriverFile> File<T> {
29 #[doc(hidden)]
30 /// Not intended to be called externally, except via declare_drm_ioctls!()
31 ///
32 /// # Safety
33 ///
34 /// `raw_file` must be a valid pointer to an open `struct drm_file`, opened through `T::open`.
35 #[safety{Typed(ptr, bindings::drm_file), PostToFunc(open)}]
36 pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_file) -> &'a File<T> {
37 // SAFETY: `raw_file` is valid by the safety requirements of this function.
38 unsafe { &*ptr.cast() }
39 }
40
41 pub(super) fn as_raw(&self) -> *mut bindings::drm_file {
42 self.0.get()
43 }
44
45 fn driver_priv(&self) -> *mut T {
46 // SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid.
47 unsafe { (*self.as_raw()).driver_priv }.cast()
48 }
49
50 /// Return a pinned reference to the driver file structure.
51 pub fn inner(&self) -> Pin<&T> {
52 // SAFETY: By the type invariant the pointer `self.as_raw()` points to a valid and opened
53 // `struct drm_file`, hence `driver_priv` has been properly initialized by `open_callback`.
54 unsafe { Pin::new_unchecked(&*(self.driver_priv())) }
55 }
56
57 /// The open callback of a `struct drm_file`.
58 pub(crate) extern "C" fn open_callback(
59 raw_dev: *mut bindings::drm_device,
60 raw_file: *mut bindings::drm_file,
61 ) -> core::ffi::c_int {
62 // SAFETY: A callback from `struct drm_driver::open` guarantees that
63 // - `raw_dev` is valid pointer to a `struct drm_device`,
64 // - the corresponding `struct drm_device` has been registered.
65 let drm = unsafe { drm::Device::from_raw(raw_dev) };
66
67 // SAFETY: `raw_file` is a valid pointer to a `struct drm_file`.
68 let file = unsafe { File::<T>::from_raw(raw_file) };
69
70 let inner = match T::open(drm) {
71 Err(e) => {
72 return e.to_errno();
73 }
74 Ok(i) => i,
75 };
76
77 // SAFETY: This pointer is treated as pinned, and the Drop guarantee is upheld in
78 // `postclose_callback()`.
79 let driver_priv = KBox::into_raw(unsafe { Pin::into_inner_unchecked(inner) });
80
81 // SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid.
82 unsafe { (*file.as_raw()).driver_priv = driver_priv.cast() };
83
84 0
85 }
86
87 /// The postclose callback of a `struct drm_file`.
88 pub(crate) extern "C" fn postclose_callback(
89 _raw_dev: *mut bindings::drm_device,
90 raw_file: *mut bindings::drm_file,
91 ) {
92 // SAFETY: This reference won't escape this function
93 let file = unsafe { File::<T>::from_raw(raw_file) };
94
95 // SAFETY: `file.driver_priv` has been created in `open_callback` through `KBox::into_raw`.
96 let _ = unsafe { KBox::from_raw(file.driver_priv()) };
97 }
98}
99
100impl<T: DriverFile> super::private::Sealed for File<T> {}