// SPDX-License-Identifier: GPL-2.0 OR MIT12//! DRM File objects.3//!4//! C header: [`include/drm/drm_file.h`](srctree/include/drm/drm_file.h)56use crate::{bindings, drm, error::Result, prelude::*, types::Opaque};7use core::marker::PhantomData;8use core::pin::Pin;910/// Trait that must be implemented by DRM drivers to represent a DRM File (a client instance).11pub trait DriverFile {12/// The parent `Driver` implementation for this `DriverFile`.13type Driver: drm::Driver;1415/// Open a new file (called when a client opens the DRM device).16fn open(device: &drm::Device<Self::Driver>) -> Result<Pin<KBox<Self>>>;17}1819/// An open DRM File.20///21/// # Invariants22///23/// `self.0` is a valid instance of a `struct drm_file`.24#[repr(transparent)]25pub struct File<T: DriverFile>(Opaque<bindings::drm_file>, PhantomData<T>);2627impl<T: DriverFile> File<T> {28#[doc(hidden)]29/// Not intended to be called externally, except via declare_drm_ioctls!()30///31/// # Safety32///33/// `raw_file` must be a valid pointer to an open `struct drm_file`, opened through `T::open`.34pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_file) -> &'a File<T> {35// SAFETY: `raw_file` is valid by the safety requirements of this function.36unsafe { &*ptr.cast() }37}3839pub(super) fn as_raw(&self) -> *mut bindings::drm_file {40self.0.get()41}4243fn driver_priv(&self) -> *mut T {44// SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid.45unsafe { (*self.as_raw()).driver_priv }.cast()46}4748/// Return a pinned reference to the driver file structure.49pub fn inner(&self) -> Pin<&T> {50// SAFETY: By the type invariant the pointer `self.as_raw()` points to a valid and opened51// `struct drm_file`, hence `driver_priv` has been properly initialized by `open_callback`.52unsafe { Pin::new_unchecked(&*(self.driver_priv())) }53}5455/// The open callback of a `struct drm_file`.56pub(crate) extern "C" fn open_callback(57raw_dev: *mut bindings::drm_device,58raw_file: *mut bindings::drm_file,59) -> core::ffi::c_int {60// SAFETY: A callback from `struct drm_driver::open` guarantees that61// - `raw_dev` is valid pointer to a `struct drm_device`,62// - the corresponding `struct drm_device` has been registered.63let drm = unsafe { drm::Device::from_raw(raw_dev) };6465// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`.66let file = unsafe { File::<T>::from_raw(raw_file) };6768let inner = match T::open(drm) {69Err(e) => {70return e.to_errno();71}72Ok(i) => i,73};7475// SAFETY: This pointer is treated as pinned, and the Drop guarantee is upheld in76// `postclose_callback()`.77let driver_priv = KBox::into_raw(unsafe { Pin::into_inner_unchecked(inner) });7879// SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid.80unsafe { (*file.as_raw()).driver_priv = driver_priv.cast() };8182083}8485/// The postclose callback of a `struct drm_file`.86pub(crate) extern "C" fn postclose_callback(87_raw_dev: *mut bindings::drm_device,88raw_file: *mut bindings::drm_file,89) {90// SAFETY: This reference won't escape this function91let file = unsafe { File::<T>::from_raw(raw_file) };9293// SAFETY: `file.driver_priv` has been created in `open_callback` through `KBox::into_raw`.94let _ = unsafe { KBox::from_raw(file.driver_priv()) };95}96}9798impl<T: DriverFile> super::private::Sealed for File<T> {}99100101