xref: /DragonOS/kernel/src/driver/base/device/device_number.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
2 pub struct Major(u32);
3 
4 impl Major {
5     // 常量定义参考:
6     //
7     // https://code.dragonos.org.cn/xref/linux-6.1.9/include/uapi/linux/major.h
8 
9     /// 未命名的主设备
10     pub const UNNAMED_MAJOR: Self = Self::new(0);
11 
12     pub const IDE0_MAJOR: Self = Self::new(3);
13     pub const TTY_MAJOR: Self = Self::new(4);
14     pub const TTYAUX_MAJOR: Self = Self::new(5);
15     pub const HD_MAJOR: Self = Self::IDE0_MAJOR;
16 
17     pub const INPUT_MAJOR: Self = Self::new(13);
18     /// /dev/fb* framebuffers
19     pub const FB_MAJOR: Self = Self::new(29);
20 
21     /// Pty
22     pub const UNIX98_PTY_MASTER_MAJOR: Self = Self::new(128);
23     pub const UNIX98_PTY_MAJOR_COUNT: Self = Self::new(8);
24     pub const UNIX98_PTY_SLAVE_MAJOR: Self =
25         Self::new(Self::UNIX98_PTY_MASTER_MAJOR.0 + Self::UNIX98_PTY_MAJOR_COUNT.0);
26 
27     pub const fn new(x: u32) -> Self {
28         Major(x)
29     }
30     pub const fn data(&self) -> u32 {
31         self.0
32     }
33 }
34 
35 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36 pub struct DeviceNumber {
37     data: u32,
38 }
39 
40 impl DeviceNumber {
41     pub const MINOR_BITS: u32 = 20;
42     pub const MINOR_MASK: u32 = (1 << Self::MINOR_BITS) - 1;
43 
44     pub const fn new(major: Major, minor: u32) -> Self {
45         Self {
46             data: (major.data() << Self::MINOR_BITS) | minor,
47         }
48     }
49 
50     pub const fn major(&self) -> Major {
51         Major::new(self.data >> Self::MINOR_BITS)
52     }
53 
54     pub const fn minor(&self) -> u32 {
55         self.data & 0xfffff
56     }
57 
58     pub const fn data(&self) -> u32 {
59         self.data
60     }
61 }
62 
63 impl Default for DeviceNumber {
64     fn default() -> Self {
65         Self::new(Major::UNNAMED_MAJOR, 0)
66     }
67 }
68 
69 impl From<u32> for DeviceNumber {
70     fn from(x: u32) -> Self {
71         Self { data: x }
72     }
73 }
74