serial_16550_resources/
lib.rs1#![forbid(unsafe_code)]
7
8use mesh::MeshPayload;
9use vm_resource::Resource;
10use vm_resource::ResourceId;
11use vm_resource::kind::ChipsetDeviceHandleKind;
12use vm_resource::kind::SerialBackendHandle;
13
14#[derive(MeshPayload)]
16pub struct Serial16550DeviceHandle {
17 pub base: MmioOrIoPort,
19 pub register_width: u8,
21 pub irq: u32,
23 pub io: Resource<SerialBackendHandle>,
25 pub wait_for_rts: bool,
29 pub debugger_mode: bool,
33}
34
35impl ResourceId<ChipsetDeviceHandleKind> for Serial16550DeviceHandle {
36 const ID: &'static str = "serial_16550";
37}
38
39#[derive(Copy, Clone, PartialEq, Eq, Debug)]
41pub enum ComPort {
42 Com1,
44 Com2,
46 Com3,
48 Com4,
50}
51
52impl ComPort {
53 pub const fn io_port(&self) -> u16 {
55 match *self {
56 ComPort::Com1 => 0x3f8,
57 ComPort::Com2 => 0x2f8,
58 ComPort::Com3 => 0x3e8,
59 ComPort::Com4 => 0x2e8,
60 }
61 }
62
63 pub const fn irq(&self) -> u8 {
65 match *self {
66 ComPort::Com1 => 4,
67 ComPort::Com2 => 3,
68 ComPort::Com3 => 4,
69 ComPort::Com4 => 3,
70 }
71 }
72}
73
74impl Serial16550DeviceHandle {
75 pub fn com_port(com_port: ComPort, io: Resource<SerialBackendHandle>) -> Self {
77 Self {
78 base: MmioOrIoPort::IoPort(com_port.io_port()),
79 register_width: 1,
80 irq: com_port.irq().into(),
81 io,
82 wait_for_rts: false,
83 debugger_mode: false,
84 }
85 }
86
87 pub fn com_ports(io: [Resource<SerialBackendHandle>; 4]) -> [Self; 4] {
89 let [com1, com2, com3, com4] = io;
90 [
91 Self::com_port(ComPort::Com1, com1),
92 Self::com_port(ComPort::Com2, com2),
93 Self::com_port(ComPort::Com3, com3),
94 Self::com_port(ComPort::Com4, com4),
95 ]
96 }
97}
98
99#[derive(MeshPayload)]
102pub enum MmioOrIoPort {
103 Mmio(u64),
105 IoPort(u16),
107}