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