1 // SPDX-License-Identifier: GPL-2.0
3 //! `ioctl()` number definitions.
5 //! C header: [`include/asm-generic/ioctl.h`](srctree/include/asm-generic/ioctl.h)
7 #![expect(non_snake_case)]
9 use crate::build_assert;
11 /// Build an ioctl number, analogous to the C macro of the same name.
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));
19 (dir << uapi::_IOC_DIRSHIFT)
20 | (ty << uapi::_IOC_TYPESHIFT)
21 | (nr << uapi::_IOC_NRSHIFT)
22 | ((size as u32) << uapi::_IOC_SIZESHIFT)
25 /// Build an ioctl number for an argumentless ioctl.
27 pub const fn _IO(ty: u32, nr: u32) -> u32 {
28 _IOC(uapi::_IOC_NONE, ty, nr, 0)
31 /// Build an ioctl number for a read-only ioctl.
33 pub const fn _IOR<T>(ty: u32, nr: u32) -> u32 {
34 _IOC(uapi::_IOC_READ, ty, nr, core::mem::size_of::<T>())
37 /// Build an ioctl number for a write-only ioctl.
39 pub const fn _IOW<T>(ty: u32, nr: u32) -> u32 {
40 _IOC(uapi::_IOC_WRITE, ty, nr, core::mem::size_of::<T>())
43 /// Build an ioctl number for a read-write ioctl.
45 pub const fn _IOWR<T>(ty: u32, nr: u32) -> u32 {
47 uapi::_IOC_READ | uapi::_IOC_WRITE,
50 core::mem::size_of::<T>(),
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
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
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
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