//! Implements typical patterns for `ioctl` usage. use super::{Ioctl, IoctlOutput, Opcode, RawOpcode}; use crate::backend::c; use crate::io::Result; use core::marker::PhantomData; use core::ptr::addr_of_mut; use core::{fmt, mem}; /// Implements an `ioctl` with no real arguments. pub struct NoArg { /// The opcode. _opcode: PhantomData, } impl fmt::Debug for NoArg { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("NoArg").field(&Opcode::OPCODE).finish() } } impl NoArg { /// Create a new no-argument `ioctl` object. /// /// # Safety /// /// - `Opcode` must provide a valid opcode. #[inline] pub unsafe fn new() -> Self { Self { _opcode: PhantomData, } } } unsafe impl Ioctl for NoArg { type Output = (); const IS_MUTATING: bool = false; const OPCODE: self::Opcode = Opcode::OPCODE; fn as_ptr(&mut self) -> *mut c::c_void { core::ptr::null_mut() } unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c::c_void) -> Result { Ok(()) } } /// Implements the traditional “getter” pattern for `ioctl`s. /// /// Some `ioctl`s just read data into the userspace. As this is a popular /// pattern this structure implements it. pub struct Getter { /// The output data. output: mem::MaybeUninit, /// The opcode. _opcode: PhantomData, } impl fmt::Debug for Getter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Getter").field(&Opcode::OPCODE).finish() } } impl Getter { /// Create a new getter-style `ioctl` object. /// /// # Safety /// /// - `Opcode` must provide a valid opcode. /// - For this opcode, `Output` must be the type that the kernel expects to /// write into. #[inline] pub unsafe fn new() -> Self { Self { output: mem::MaybeUninit::uninit(), _opcode: PhantomData, } } } unsafe impl Ioctl for Getter { type Output = Output; const IS_MUTATING: bool = true; const OPCODE: self::Opcode = Opcode::OPCODE; fn as_ptr(&mut self) -> *mut c::c_void { self.output.as_mut_ptr().cast() } unsafe fn output_from_ptr(_: IoctlOutput, ptr: *mut c::c_void) -> Result { Ok(ptr.cast::().read()) } } /// Implements the pattern for `ioctl`s where a pointer argument is given to /// the `ioctl`. /// /// The opcode must be read-only. pub struct Setter { /// The input data. input: Input, /// The opcode. _opcode: PhantomData, } impl fmt::Debug for Setter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Setter") .field(&Opcode::OPCODE) .field(&self.input) .finish() } } impl Setter { /// Create a new pointer setter-style `ioctl` object. /// /// # Safety /// /// - `Opcode` must provide a valid opcode. /// - For this opcode, `Input` must be the type that the kernel expects to /// get. #[inline] pub unsafe fn new(input: Input) -> Self { Self { input, _opcode: PhantomData, } } } unsafe impl Ioctl for Setter { type Output = (); const IS_MUTATING: bool = false; const OPCODE: self::Opcode = Opcode::OPCODE; fn as_ptr(&mut self) -> *mut c::c_void { addr_of_mut!(self.input).cast::() } unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c::c_void) -> Result { Ok(()) } } /// Trait for something that provides an `ioctl` opcode at compile time. pub trait CompileTimeOpcode { /// The opcode. const OPCODE: Opcode; } /// Provides a bad opcode at compile time. pub struct BadOpcode; impl CompileTimeOpcode for BadOpcode { const OPCODE: Opcode = Opcode::old(OPCODE); } /// Provides a read code at compile time. /// /// This corresponds to the C macro `_IOR(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct ReadOpcode(Data); #[cfg(any(linux_kernel, bsd))] impl CompileTimeOpcode for ReadOpcode { const OPCODE: Opcode = Opcode::read::(GROUP, NUM); } /// Provides a write code at compile time. /// /// This corresponds to the C macro `_IOW(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct WriteOpcode(Data); #[cfg(any(linux_kernel, bsd))] impl CompileTimeOpcode for WriteOpcode { const OPCODE: Opcode = Opcode::write::(GROUP, NUM); } /// Provides a read/write code at compile time. /// /// This corresponds to the C macro `_IOWR(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct ReadWriteOpcode(Data); #[cfg(any(linux_kernel, bsd))] impl CompileTimeOpcode for ReadWriteOpcode { const OPCODE: Opcode = Opcode::read_write::(GROUP, NUM); } /// Provides a `None` code at compile time. /// /// This corresponds to the C macro `_IO(GROUP, NUM)` when `Data` is zero /// sized. #[cfg(any(linux_kernel, bsd))] pub struct NoneOpcode(Data); #[cfg(any(linux_kernel, bsd))] impl CompileTimeOpcode for NoneOpcode { const OPCODE: Opcode = Opcode::none::(GROUP, NUM); }