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