summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ioctl-sys/src/platform/mod.rs
blob: d795b4f3c52cc90f10109ffa6b9c0ace4a791c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#[doc(hidden)]
pub const NRBITS: u32 = 8;
#[doc(hidden)]
pub const TYPEBITS: u32 = 8;

#[cfg(any(target_os = "linux", target_os = "android"))]
#[path = "linux.rs"]
mod consts;

#[cfg(target_os = "macos")]
#[path = "macos.rs"]
mod consts;

#[doc(hidden)]
pub use self::consts::*;

#[doc(hidden)]
pub const NRSHIFT: u32 = 0;
#[doc(hidden)]
pub const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32;
#[doc(hidden)]
pub const SIZESHIFT: u32 = TYPESHIFT + TYPEBITS as u32;
#[doc(hidden)]
pub const DIRSHIFT: u32 = SIZESHIFT + SIZEBITS as u32;

#[doc(hidden)]
pub const NRMASK: u32 = (1 << NRBITS) - 1;
#[doc(hidden)]
pub const TYPEMASK: u32 = (1 << TYPEBITS) - 1;
#[doc(hidden)]
pub const SIZEMASK: u32 = (1 << SIZEBITS) - 1;
#[doc(hidden)]
pub const DIRMASK: u32 = (1 << DIRBITS) - 1;

/// Encode an ioctl command.
#[macro_export]
macro_rules! ioc {
    ($dir:expr, $ty:expr, $nr:expr, $sz:expr) => {
        (($dir as u32) << $crate::DIRSHIFT)
            | (($ty as u32) << $crate::TYPESHIFT)
            | (($nr as u32) << $crate::NRSHIFT)
            | (($sz as u32) << $crate::SIZESHIFT)
    };
}

/// Encode an ioctl command that has no associated data.
#[macro_export]
macro_rules! io {
    ($ty:expr, $nr:expr) => {
        $crate::ioc!($crate::NONE, $ty, $nr, 0)
    };
}

/// Encode an ioctl command that reads.
#[macro_export]
macro_rules! ior {
    ($ty:expr, $nr:expr, $sz:expr) => {
        $crate::ioc!($crate::READ, $ty, $nr, $sz)
    };
}

/// Encode an ioctl command that writes.
#[macro_export]
macro_rules! iow {
    ($ty:expr, $nr:expr, $sz:expr) => {
        $crate::ioc!($crate::WRITE, $ty, $nr, $sz)
    };
}

/// Encode an ioctl command that both reads and writes.
#[macro_export]
macro_rules! iorw {
    ($ty:expr, $nr:expr, $sz:expr) => {
        $crate::ioc!($crate::READ | $crate::WRITE, $ty, $nr, $sz)
    };
}

/// Declare a wrapper function around an ioctl.
#[macro_export]
macro_rules! ioctl {
    (bad $name:ident with $nr:expr) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, data: *mut u8) -> ::std::os::raw::c_int {
            $crate::ioctl(fd, $nr as ::std::os::raw::c_ulong, data)
        }
    };
    (bad read $name:ident with $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, data: *mut $ty) -> ::std::os::raw::c_int {
            $crate::ioctl(fd, $nr as ::std::os::raw::c_ulong, data)
        }
    };
    (bad write $name:ident with $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, data: *const $ty) -> ::std::os::raw::c_int {
            $crate::ioctl(fd, $nr as ::std::os::raw::c_ulong, data)
        }
    };
    (none $name:ident with $ioty:expr, $nr:expr) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int) -> ::std::os::raw::c_int {
            $crate::ioctl(fd, $crate::io!($ioty, $nr) as ::std::os::raw::c_ulong)
        }
    };
    (try none $name:ident with $ioty:expr, $nr:expr) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int) -> std::result::Result<(), std::io::Error> {
            $crate::check_res($crate::ioctl(
                fd,
                $crate::io!($ioty, $nr) as ::std::os::raw::c_ulong,
            ))
        }
    };
    (arg $name:ident with $ioty:expr, $nr:expr) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            arg: ::std::os::raw::c_ulong,
        ) -> ::std::os::raw::c_int {
            $crate::ioctl(fd, $crate::io!($ioty, $nr) as ::std::os::raw::c_ulong, arg)
        }
    };
    (read $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, val: *mut $ty) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
    (try read $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *mut $ty,
        ) -> std::result::Result<(), std::io::Error> {
            $crate::check_res($crate::ioctl(
                fd,
                $crate::ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            ))
        }
    };
    (try read0 $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int) -> std::result::Result<$ty, std::io::Error> {
            let mut val: $ty = std::mem::zeroed();
            $crate::check_res($crate::ioctl(
                fd,
                $crate::ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                &mut val as *mut $ty,
            ))
            .map(|_| val)
        }
    };
    (write $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, val: *const $ty) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
    (try write $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *const $ty,
        ) -> std::result::Result<(), std::io::Error> {
            $crate::check_res($crate::ioctl(
                fd,
                $crate::iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            ))
        }
    };
    (readwrite $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(fd: ::std::os::raw::c_int, val: *mut $ty) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
    (try readwrite $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *mut $ty,
        ) -> std::result::Result<(), std::io::Error> {
            $crate::check_res($crate::ioctl(
                fd,
                $crate::iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as ::std::os::raw::c_ulong,
                val,
            ))
        }
    };
    (read buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *mut $ty,
            len: usize,
        ) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::ior!($ioty, $nr, len) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
    (write buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *const $ty,
            len: usize,
        ) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::iow!($ioty, $nr, len) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
    (readwrite buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => {
        pub unsafe fn $name(
            fd: ::std::os::raw::c_int,
            val: *const $ty,
            len: usize,
        ) -> ::std::os::raw::c_int {
            $crate::ioctl(
                fd,
                $crate::iorw!($ioty, $nr, len) as ::std::os::raw::c_ulong,
                val,
            )
        }
    };
}

/// Extracts the "direction" (read/write/none) from an encoded ioctl command.
#[inline(always)]
pub fn ioc_dir(nr: u32) -> u8 {
    ((nr >> DIRSHIFT) & DIRMASK) as u8
}

/// Extracts the type from an encoded ioctl command.
#[inline(always)]
pub fn ioc_type(nr: u32) -> u32 {
    (nr >> TYPESHIFT) & TYPEMASK
}

/// Extracts the ioctl number from an encoded ioctl command.
#[inline(always)]
pub fn ioc_nr(nr: u32) -> u32 {
    (nr >> NRSHIFT) & NRMASK
}

/// Extracts the size from an encoded ioctl command.
#[inline(always)]
pub fn ioc_size(nr: u32) -> u32 {
    ((nr >> SIZESHIFT) as u32) & SIZEMASK
}

#[doc(hidden)]
pub const IN: u32 = (WRITE as u32) << DIRSHIFT;
#[doc(hidden)]
pub const OUT: u32 = (READ as u32) << DIRSHIFT;
#[doc(hidden)]
pub const INOUT: u32 = ((READ | WRITE) as u32) << DIRSHIFT;
#[doc(hidden)]
pub const SIZE_MASK: u32 = SIZEMASK << SIZESHIFT;