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