Skip to main content

libc/new/apple/xnu/sys/
ioccom.rs

1//! Header: `sys/ioccom.h`
2//!
3//! <https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/ioccom.h>
4
5use crate::prelude::*;
6
7const IOCPARM_MASK: c_ulong = 0x1fff;
8
9const IOCPARM_MAX: c_ulong = IOCPARM_MASK + 1;
10
11// These are u32 in source but would probably be more practical as c_ulong if we ever need
12// to make them public.
13pub(crate) const IOC_VOID: u32 = 0x20000000;
14pub(crate) const IOC_OUT: u32 = 0x40000000;
15pub(crate) const IOC_IN: u32 = 0x80000000;
16pub(crate) const IOC_INOUT: u32 = IOC_IN | IOC_OUT;
17pub(crate) const IOC_DIRMASK: u32 = 0xe0000000;
18
19// Only pub(crate) for the above reason.
20pub(crate) const fn _IOC(inout: u32, group: c_ulong, num: c_ulong, len: c_ulong) -> c_ulong {
21    debug_assert!(inout <= IOC_DIRMASK);
22    debug_assert!(group <= 0xff);
23    debug_assert!(num <= 0xff);
24    debug_assert!(len <= IOCPARM_MAX);
25
26    // Sanity check the cast
27    assert!(size_of::<u32>() <= size_of::<c_ulong>());
28
29    (inout as c_ulong) | ((len & IOCPARM_MASK) << 16) | (group << 8) | num
30}
31
32pub const fn _IO(g: c_ulong, n: c_ulong) -> c_ulong {
33    _IOC(IOC_VOID, g, n, 0)
34}
35
36/// Build an ioctl number for an read-only ioctl.
37pub const fn _IOR<T>(g: c_ulong, n: c_ulong) -> c_ulong {
38    _IOC(IOC_OUT, g, n, mem::size_of::<T>() as c_ulong)
39}
40
41/// Build an ioctl number for an write-only ioctl.
42pub const fn _IOW<T>(g: c_ulong, n: c_ulong) -> c_ulong {
43    _IOC(IOC_IN, g, n, mem::size_of::<T>() as c_ulong)
44}
45
46/// Build an ioctl number for a read-write ioctl.
47pub const fn _IOWR<T>(g: c_ulong, n: c_ulong) -> c_ulong {
48    _IOC(IOC_INOUT, g, n, mem::size_of::<T>() as c_ulong)
49}