]> git.proxmox.com Git - rustc.git/blob - src/vendor/nix/src/sys/ioctl/platform/linux.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / vendor / nix / src / sys / ioctl / platform / linux.rs
1 pub const NRBITS: u32 = 8;
2 pub const TYPEBITS: u32 = 8;
3
4 #[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
5 mod consts {
6 pub const NONE: u8 = 1;
7 pub const READ: u8 = 2;
8 pub const WRITE: u8 = 4;
9 pub const SIZEBITS: u8 = 13;
10 pub const DIRBITS: u8 = 3;
11 }
12
13 #[cfg(not(any(target_arch = "powerpc",
14 target_arch = "mips",
15 target_arch = "x86",
16 target_arch = "arm",
17 target_arch = "x86_64",
18 target_arch = "aarch64")))]
19 use this_arch_not_supported;
20
21 // "Generic" ioctl protocol
22 #[cfg(any(target_arch = "x86",
23 target_arch = "arm",
24 target_arch = "x86_64",
25 target_arch = "aarch64"))]
26 mod consts {
27 pub const NONE: u8 = 0;
28 pub const READ: u8 = 2;
29 pub const WRITE: u8 = 1;
30 pub const SIZEBITS: u8 = 14;
31 pub const DIRBITS: u8 = 2;
32 }
33
34 pub use self::consts::*;
35
36 pub const NRSHIFT: u32 = 0;
37 pub const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32;
38 pub const SIZESHIFT: u32 = TYPESHIFT + TYPEBITS as u32;
39 pub const DIRSHIFT: u32 = SIZESHIFT + SIZEBITS as u32;
40
41 pub const NRMASK: u32 = (1 << NRBITS) - 1;
42 pub const TYPEMASK: u32 = (1 << TYPEBITS) - 1;
43 pub const SIZEMASK: u32 = (1 << SIZEBITS) - 1;
44 pub const DIRMASK: u32 = (1 << DIRBITS) - 1;
45
46 /// Encode an ioctl command.
47 #[macro_export]
48 macro_rules! ioc {
49 ($dir:expr, $ty:expr, $nr:expr, $sz:expr) => (
50 (($dir as u32) << $crate::sys::ioctl::DIRSHIFT) |
51 (($ty as u32) << $crate::sys::ioctl::TYPESHIFT) |
52 (($nr as u32) << $crate::sys::ioctl::NRSHIFT) |
53 (($sz as u32) << $crate::sys::ioctl::SIZESHIFT))
54 }
55
56 /// Encode an ioctl command that has no associated data.
57 #[macro_export]
58 macro_rules! io {
59 ($ty:expr, $nr:expr) => (ioc!($crate::sys::ioctl::NONE, $ty, $nr, 0))
60 }
61
62 /// Encode an ioctl command that reads.
63 #[macro_export]
64 macro_rules! ior {
65 ($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::READ, $ty, $nr, $sz))
66 }
67
68 /// Encode an ioctl command that writes.
69 #[macro_export]
70 macro_rules! iow {
71 ($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::WRITE, $ty, $nr, $sz))
72 }
73
74 /// Encode an ioctl command that both reads and writes.
75 #[macro_export]
76 macro_rules! iorw {
77 ($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::READ | $crate::sys::ioctl::WRITE, $ty, $nr, $sz))
78 }
79
80 /// Extracts the "direction" (read/write/none) from an encoded ioctl command.
81 #[inline(always)]
82 pub fn ioc_dir(nr: u32) -> u8 {
83 ((nr >> DIRSHIFT) & DIRMASK) as u8
84 }
85
86 /// Extracts the type from an encoded ioctl command.
87 #[inline(always)]
88 pub fn ioc_type(nr: u32) -> u32 {
89 (nr >> TYPESHIFT) & TYPEMASK
90 }
91
92 /// Extracts the ioctl number from an encoded ioctl command.
93 #[inline(always)]
94 pub fn ioc_nr(nr: u32) -> u32 {
95 (nr >> NRSHIFT) & NRMASK
96 }
97
98 /// Extracts the size from an encoded ioctl command.
99 #[inline(always)]
100 pub fn ioc_size(nr: u32) -> u32 {
101 ((nr >> SIZESHIFT) as u32) & SIZEMASK
102 }
103
104 pub const IN: u32 = (WRITE as u32) << DIRSHIFT;
105 pub const OUT: u32 = (READ as u32) << DIRSHIFT;
106 pub const INOUT: u32 = ((READ|WRITE) as u32) << DIRSHIFT;
107 pub const SIZE_MASK: u32 = SIZEMASK << SIZESHIFT;