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