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}
30
31impl ResourceId<ChipsetDeviceHandleKind> for Serial16550DeviceHandle {
32 const ID: &'static str = "serial_16550";
33}
34
35#[derive(Copy, Clone, PartialEq, Eq, Debug)]
37pub enum ComPort {
38 Com1,
40 Com2,
42 Com3,
44 Com4,
46}
47
48impl ComPort {
49 pub const fn io_port(&self) -> u16 {
51 match *self {
52 ComPort::Com1 => 0x3f8,
53 ComPort::Com2 => 0x2f8,
54 ComPort::Com3 => 0x3e8,
55 ComPort::Com4 => 0x2e8,
56 }
57 }
58
59 pub const fn irq(&self) -> u8 {
61 match *self {
62 ComPort::Com1 => 4,
63 ComPort::Com2 => 3,
64 ComPort::Com3 => 4,
65 ComPort::Com4 => 3,
66 }
67 }
68}
69
70impl Serial16550DeviceHandle {
71 pub fn com_port(com_port: ComPort, io: Resource<SerialBackendHandle>) -> Self {
73 Self {
74 base: MmioOrIoPort::IoPort(com_port.io_port()),
75 register_width: 1,
76 irq: com_port.irq().into(),
77 io,
78 wait_for_rts: false,
79 }
80 }
81
82 pub fn com_ports(io: [Resource<SerialBackendHandle>; 4]) -> [Self; 4] {
84 let [com1, com2, com3, com4] = io;
85 [
86 Self::com_port(ComPort::Com1, com1),
87 Self::com_port(ComPort::Com2, com2),
88 Self::com_port(ComPort::Com3, com3),
89 Self::com_port(ComPort::Com4, com4),
90 ]
91 }
92}
93
94#[derive(MeshPayload)]
97pub enum MmioOrIoPort {
98 Mmio(u64),
100 IoPort(u16),
102}