Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 #1140

Closed
wants to merge 2 commits into from
Closed

1 #1140

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions rust/kernel/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
//! `ioctl()` number definitions.
//!
//! C header: [`include/asm-generic/ioctl.h`](srctree/include/asm-generic/ioctl.h)

#![allow(non_snake_case)]

use crate::build_assert;

/// Build an ioctl number, analogous to the C macro of the same name.
#[inline(always)]
const fn _IOC(dir: u32, ty: u32, nr: u32, size: usize) -> u32 {
build_assert!(dir <= uapi::_IOC_DIRMASK);
build_assert!(ty <= uapi::_IOC_TYPEMASK);
build_assert!(nr <= uapi::_IOC_NRMASK);
build_assert!(size <= (uapi::_IOC_SIZEMASK as usize));
build_assert!(dir <= uapi::_IOC_DIRMASK, "Invalid direction value");
build_assert!(ty <= uapi::_IOC_TYPEMASK, "Invalid type value");
build_assert!(nr <= uapi::_IOC_NRMASK, "Invalid number value");
build_assert!(size <= (uapi::_IOC_SIZEMASK as usize), "Invalid size value");

(dir << uapi::_IOC_DIRSHIFT)
| (ty << uapi::_IOC_TYPESHIFT)
Expand Down Expand Up @@ -70,3 +69,11 @@ pub const fn _IOC_NR(nr: u32) -> u32 {
pub const fn _IOC_SIZE(nr: u32) -> usize {
((nr >> uapi::_IOC_SIZESHIFT) & uapi::_IOC_SIZEMASK) as usize
}

// Example usage
const MY_DEVICE_TYPE: u32 = 0x1234;
const MY_IOCTL_NUMBER: u32 = 0x01;

// Creating IOCTL numbers
const IOCTL_MY_DEVICE_READ: u32 = _IOR<MyDataType>(MY_DEVICE_TYPE, MY_IOCTL_NUMBER);
const IOCTL_MY_DEVICE_WRITE: u32 = _IOW<MyDataType>(MY_DEVICE_TYPE, MY_IOCTL_NUMBER);