152da9a59SGnoCiYeH #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] 2c566df45SLoGin pub struct Major(u32); 3c566df45SLoGin 4c566df45SLoGin impl Major { 5c566df45SLoGin // 常量定义参考: 6c566df45SLoGin // 7c566df45SLoGin // https://code.dragonos.org.cn/xref/linux-6.1.9/include/uapi/linux/major.h 8c566df45SLoGin 9c566df45SLoGin /// 未命名的主设备 10c566df45SLoGin pub const UNNAMED_MAJOR: Self = Self::new(0); 1102343d0bSLoGin 1202343d0bSLoGin pub const IDE0_MAJOR: Self = Self::new(3); 1352da9a59SGnoCiYeH pub const TTY_MAJOR: Self = Self::new(4); 1452da9a59SGnoCiYeH pub const TTYAUX_MAJOR: Self = Self::new(5); 1502343d0bSLoGin pub const HD_MAJOR: Self = Self::IDE0_MAJOR; 1602343d0bSLoGin 1702343d0bSLoGin pub const INPUT_MAJOR: Self = Self::new(13); 18c566df45SLoGin /// /dev/fb* framebuffers 19c566df45SLoGin pub const FB_MAJOR: Self = Self::new(29); 20c566df45SLoGin 21*dfe53cf0SGnoCiYeH /// Pty 22*dfe53cf0SGnoCiYeH pub const UNIX98_PTY_MASTER_MAJOR: Self = Self::new(128); 23*dfe53cf0SGnoCiYeH pub const UNIX98_PTY_MAJOR_COUNT: Self = Self::new(8); 24*dfe53cf0SGnoCiYeH pub const UNIX98_PTY_SLAVE_MAJOR: Self = 25*dfe53cf0SGnoCiYeH Self::new(Self::UNIX98_PTY_MASTER_MAJOR.0 + Self::UNIX98_PTY_MAJOR_COUNT.0); 26*dfe53cf0SGnoCiYeH new(x: u32) -> Self27c566df45SLoGin pub const fn new(x: u32) -> Self { 28c566df45SLoGin Major(x) 29c566df45SLoGin } data(&self) -> u3230c566df45SLoGin pub const fn data(&self) -> u32 { 31c566df45SLoGin self.0 32c566df45SLoGin } 33c566df45SLoGin } 34c566df45SLoGin 35c566df45SLoGin #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] 36c566df45SLoGin pub struct DeviceNumber { 37c566df45SLoGin data: u32, 38c566df45SLoGin } 39c566df45SLoGin 40c566df45SLoGin impl DeviceNumber { 41c566df45SLoGin pub const MINOR_BITS: u32 = 20; 42b5b571e0SLoGin pub const MINOR_MASK: u32 = (1 << Self::MINOR_BITS) - 1; 43c566df45SLoGin new(major: Major, minor: u32) -> Self44c566df45SLoGin pub const fn new(major: Major, minor: u32) -> Self { 45c566df45SLoGin Self { 46c566df45SLoGin data: (major.data() << Self::MINOR_BITS) | minor, 47c566df45SLoGin } 48c566df45SLoGin } 49c566df45SLoGin major(&self) -> Major50c566df45SLoGin pub const fn major(&self) -> Major { 51c566df45SLoGin Major::new(self.data >> Self::MINOR_BITS) 52c566df45SLoGin } 53c566df45SLoGin minor(&self) -> u3254c566df45SLoGin pub const fn minor(&self) -> u32 { 55c566df45SLoGin self.data & 0xfffff 56c566df45SLoGin } 57c566df45SLoGin data(&self) -> u3258c566df45SLoGin pub const fn data(&self) -> u32 { 59c566df45SLoGin self.data 60c566df45SLoGin } 61c566df45SLoGin } 62c566df45SLoGin 63c566df45SLoGin impl Default for DeviceNumber { default() -> Self64c566df45SLoGin fn default() -> Self { 65c566df45SLoGin Self::new(Major::UNNAMED_MAJOR, 0) 66c566df45SLoGin } 67c566df45SLoGin } 68c566df45SLoGin 69c566df45SLoGin impl From<u32> for DeviceNumber { from(x: u32) -> Self70c566df45SLoGin fn from(x: u32) -> Self { 71c566df45SLoGin Self { data: x } 72c566df45SLoGin } 73c566df45SLoGin } 74