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