xref: /DragonOS/kernel/src/driver/base/device/device_number.rs (revision 02343d0b5b47c07e7f4ec3818940795b1009fae1)
1 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
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 HD_MAJOR: Self = Self::IDE0_MAJOR;
14 
15     pub const INPUT_MAJOR: Self = Self::new(13);
16     /// /dev/fb* framebuffers
17     pub const FB_MAJOR: Self = Self::new(29);
18 
19     pub const fn new(x: u32) -> Self {
20         Major(x)
21     }
22     pub const fn data(&self) -> u32 {
23         self.0
24     }
25 }
26 
27 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
28 pub struct DeviceNumber {
29     data: u32,
30 }
31 
32 impl DeviceNumber {
33     pub const MINOR_BITS: u32 = 20;
34     pub const MINOR_MASK: u32 = 1 << Self::MINOR_BITS - 1;
35 
36     pub const fn new(major: Major, minor: u32) -> Self {
37         Self {
38             data: (major.data() << Self::MINOR_BITS) | minor,
39         }
40     }
41 
42     pub const fn major(&self) -> Major {
43         Major::new(self.data >> Self::MINOR_BITS)
44     }
45 
46     pub const fn minor(&self) -> u32 {
47         self.data & 0xfffff
48     }
49 
50     pub const fn data(&self) -> u32 {
51         self.data
52     }
53 }
54 
55 impl Default for DeviceNumber {
56     fn default() -> Self {
57         Self::new(Major::UNNAMED_MAJOR, 0)
58     }
59 }
60 
61 impl From<u32> for DeviceNumber {
62     fn from(x: u32) -> Self {
63         Self { data: x }
64     }
65 }
66