// SPDX-License-Identifier: GPL-2.0 OR MIT12//! DRM IOCTL definitions.3//!4//! C header: [`include/drm/drm_ioctl.h`](srctree/include/drm/drm_ioctl.h)56use crate::ioctl;78const BASE: u32 = uapi::DRM_IOCTL_BASE as u32;910/// Construct a DRM ioctl number with no argument.11#[allow(non_snake_case)]12#[inline(always)]13pub const fn IO(nr: u32) -> u32 {14ioctl::_IO(BASE, nr)15}1617/// Construct a DRM ioctl number with a read-only argument.18#[allow(non_snake_case)]19#[inline(always)]20pub const fn IOR<T>(nr: u32) -> u32 {21ioctl::_IOR::<T>(BASE, nr)22}2324/// Construct a DRM ioctl number with a write-only argument.25#[allow(non_snake_case)]26#[inline(always)]27pub const fn IOW<T>(nr: u32) -> u32 {28ioctl::_IOW::<T>(BASE, nr)29}3031/// Construct a DRM ioctl number with a read-write argument.32#[allow(non_snake_case)]33#[inline(always)]34pub const fn IOWR<T>(nr: u32) -> u32 {35ioctl::_IOWR::<T>(BASE, nr)36}3738/// Descriptor type for DRM ioctls. Use the `declare_drm_ioctls!{}` macro to construct them.39pub type DrmIoctlDescriptor = bindings::drm_ioctl_desc;4041/// This is for ioctl which are used for rendering, and require that the file descriptor is either42/// for a render node, or if it’s a legacy/primary node, then it must be authenticated.43pub const AUTH: u32 = bindings::drm_ioctl_flags_DRM_AUTH;4445/// This must be set for any ioctl which can change the modeset or display state. Userspace must46/// call the ioctl through a primary node, while it is the active master.47///48/// Note that read-only modeset ioctl can also be called by unauthenticated clients, or when a49/// master is not the currently active one.50pub const MASTER: u32 = bindings::drm_ioctl_flags_DRM_MASTER;5152/// Anything that could potentially wreak a master file descriptor needs to have this flag set.53///54/// Current that’s only for the SETMASTER and DROPMASTER ioctl, which e.g. logind can call to55/// force a non-behaving master (display compositor) into compliance.56///57/// This is equivalent to callers with the SYSADMIN capability.58pub const ROOT_ONLY: u32 = bindings::drm_ioctl_flags_DRM_ROOT_ONLY;5960/// This is used for all ioctl needed for rendering only, for drivers which support render nodes.61/// This should be all new render drivers, and hence it should be always set for any ioctl with62/// `AUTH` set. Note though that read-only query ioctl might have this set, but have not set63/// DRM_AUTH because they do not require authentication.64pub const RENDER_ALLOW: u32 = bindings::drm_ioctl_flags_DRM_RENDER_ALLOW;6566/// Internal structures used by the `declare_drm_ioctls!{}` macro. Do not use directly.67#[doc(hidden)]68pub mod internal {69pub use bindings::drm_device;70pub use bindings::drm_file;71pub use bindings::drm_ioctl_desc;72}7374/// Declare the DRM ioctls for a driver.75///76/// Each entry in the list should have the form:77///78/// `(ioctl_number, argument_type, flags, user_callback),`79///80/// `argument_type` is the type name within the `bindings` crate.81/// `user_callback` should have the following prototype:82///83/// ```ignore84/// fn foo(device: &kernel::drm::Device<Self>,85/// data: &mut uapi::argument_type,86/// file: &kernel::drm::File<Self::File>,87/// ) -> Result<u32>88/// ```89/// where `Self` is the drm::drv::Driver implementation these ioctls are being declared within.90///91/// # Examples92///93/// ```ignore94/// kernel::declare_drm_ioctls! {95/// (FOO_GET_PARAM, drm_foo_get_param, ioctl::RENDER_ALLOW, my_get_param_handler),96/// }97/// ```98///99#[macro_export]100macro_rules! declare_drm_ioctls {101( $(($cmd:ident, $struct:ident, $flags:expr, $func:expr)),* $(,)? ) => {102const IOCTLS: &'static [$crate::drm::ioctl::DrmIoctlDescriptor] = {103use $crate::uapi::*;104const _:() = {105let i: u32 = $crate::uapi::DRM_COMMAND_BASE;106// Assert that all the IOCTLs are in the right order and there are no gaps,107// and that the size of the specified type is correct.108$(109let cmd: u32 = $crate::macros::concat_idents!(DRM_IOCTL_, $cmd);110::core::assert!(i == $crate::ioctl::_IOC_NR(cmd));111::core::assert!(core::mem::size_of::<$crate::uapi::$struct>() ==112$crate::ioctl::_IOC_SIZE(cmd));113let i: u32 = i + 1;114)*115};116117let ioctls = &[$(118$crate::drm::ioctl::internal::drm_ioctl_desc {119cmd: $crate::macros::concat_idents!(DRM_IOCTL_, $cmd) as u32,120func: {121#[allow(non_snake_case)]122unsafe extern "C" fn $cmd(123raw_dev: *mut $crate::drm::ioctl::internal::drm_device,124raw_data: *mut ::core::ffi::c_void,125raw_file: *mut $crate::drm::ioctl::internal::drm_file,126) -> core::ffi::c_int {127// SAFETY:128// - The DRM core ensures the device lives while callbacks are being129// called.130// - The DRM device must have been registered when we're called through131// an IOCTL.132//133// FIXME: Currently there is nothing enforcing that the types of the134// dev/file match the current driver these ioctls are being declared135// for, and it's not clear how to enforce this within the type system.136let dev = $crate::drm::device::Device::from_raw(raw_dev);137// SAFETY: The ioctl argument has size `_IOC_SIZE(cmd)`, which we138// asserted above matches the size of this type, and all bit patterns of139// UAPI structs must be valid.140// The `ioctl` argument is exclusively owned by the handler141// and guaranteed by the C implementation (`drm_ioctl()`) to remain142// valid for the entire lifetime of the reference taken here.143// There is no concurrent access or aliasing; no other references144// to this object exist during this call.145let data = unsafe { &mut *(raw_data.cast::<$crate::uapi::$struct>()) };146// SAFETY: This is just the DRM file structure147let file = unsafe { $crate::drm::File::from_raw(raw_file) };148149match $func(dev, data, file) {150Err(e) => e.to_errno(),151Ok(i) => i.try_into()152.unwrap_or($crate::error::code::ERANGE.to_errno()),153}154}155Some($cmd)156},157flags: $flags,158name: $crate::c_str!(::core::stringify!($cmd)).as_char_ptr(),159}160),*];161ioctls162};163};164}165166167