Skip to content

Commit ea76e08

Browse files
asahilinaojeda
authored andcommitted
rust: ioctl: Add ioctl number manipulation functions
Add simple 1:1 wrappers of the C ioctl number manipulation functions. Since these are macros we cannot bindgen them directly, and since they should be usable in const context we cannot use helper wrappers, so we'll have to reimplement them in Rust. Thankfully, the C headers do declare defines for the relevant bitfield positions, so we don't need to duplicate that. Signed-off-by: Asahi Lina <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Moved the `#![allow(non_snake_case)]` to the usual place. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 4e17466 commit ea76e08

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

rust/kernel/ioctl.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! ioctl() number definitions
4+
//!
5+
//! C header: [`include/asm-generic/ioctl.h`](../../../../include/asm-generic/ioctl.h)
6+
7+
#![allow(non_snake_case)]
8+
9+
use crate::build_assert;
10+
11+
/// Build an ioctl number, analogous to the C macro of the same name.
12+
#[inline(always)]
13+
const fn _IOC(dir: u32, ty: u32, nr: u32, size: usize) -> u32 {
14+
build_assert!(dir <= uapi::_IOC_DIRMASK);
15+
build_assert!(ty <= uapi::_IOC_TYPEMASK);
16+
build_assert!(nr <= uapi::_IOC_NRMASK);
17+
build_assert!(size <= (uapi::_IOC_SIZEMASK as usize));
18+
19+
(dir << uapi::_IOC_DIRSHIFT)
20+
| (ty << uapi::_IOC_TYPESHIFT)
21+
| (nr << uapi::_IOC_NRSHIFT)
22+
| ((size as u32) << uapi::_IOC_SIZESHIFT)
23+
}
24+
25+
/// Build an ioctl number for an argumentless ioctl.
26+
#[inline(always)]
27+
pub const fn _IO(ty: u32, nr: u32) -> u32 {
28+
_IOC(uapi::_IOC_NONE, ty, nr, 0)
29+
}
30+
31+
/// Build an ioctl number for an read-only ioctl.
32+
#[inline(always)]
33+
pub const fn _IOR<T>(ty: u32, nr: u32) -> u32 {
34+
_IOC(uapi::_IOC_READ, ty, nr, core::mem::size_of::<T>())
35+
}
36+
37+
/// Build an ioctl number for an write-only ioctl.
38+
#[inline(always)]
39+
pub const fn _IOW<T>(ty: u32, nr: u32) -> u32 {
40+
_IOC(uapi::_IOC_WRITE, ty, nr, core::mem::size_of::<T>())
41+
}
42+
43+
/// Build an ioctl number for a read-write ioctl.
44+
#[inline(always)]
45+
pub const fn _IOWR<T>(ty: u32, nr: u32) -> u32 {
46+
_IOC(
47+
uapi::_IOC_READ | uapi::_IOC_WRITE,
48+
ty,
49+
nr,
50+
core::mem::size_of::<T>(),
51+
)
52+
}
53+
54+
/// Get the ioctl direction from an ioctl number.
55+
pub const fn _IOC_DIR(nr: u32) -> u32 {
56+
(nr >> uapi::_IOC_DIRSHIFT) & uapi::_IOC_DIRMASK
57+
}
58+
59+
/// Get the ioctl type from an ioctl number.
60+
pub const fn _IOC_TYPE(nr: u32) -> u32 {
61+
(nr >> uapi::_IOC_TYPESHIFT) & uapi::_IOC_TYPEMASK
62+
}
63+
64+
/// Get the ioctl number from an ioctl number.
65+
pub const fn _IOC_NR(nr: u32) -> u32 {
66+
(nr >> uapi::_IOC_NRSHIFT) & uapi::_IOC_NRMASK
67+
}
68+
69+
/// Get the ioctl size from an ioctl number.
70+
pub const fn _IOC_SIZE(nr: u32) -> usize {
71+
((nr >> uapi::_IOC_SIZESHIFT) & uapi::_IOC_SIZEMASK) as usize
72+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod allocator;
3737
mod build_assert;
3838
pub mod error;
3939
pub mod init;
40+
pub mod ioctl;
4041
pub mod prelude;
4142
pub mod print;
4243
mod static_assert;

0 commit comments

Comments
 (0)