Merge tag 'compiler-attributes-6.9' of https://github.com/ojeda/linux
[sfrench/cifs-2.6.git] / rust / kernel / ioctl.rs
1 // SPDX-License-Identifier: GPL-2.0
2
3 //! ioctl() number definitions
4 //!
5 //! C header: [`include/asm-generic/ioctl.h`](srctree/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 }