1const std = @import("std");
2const uefi = std.os.uefi;
3const Guid = uefi.Guid;
4const Status = uefi.Status;
5const cc = uefi.cc;
6
7pub const SerialIo = extern struct {
8 revision: u64,
9 _reset: *const fn (*SerialIo) callconv(cc) Status,
10 _set_attribute: *const fn (*SerialIo, u64, u32, u32, ParityType, u8, StopBitsType) callconv(cc) Status,
11 _set_control: *const fn (*SerialIo, u32) callconv(cc) Status,
12 _get_control: *const fn (*const SerialIo, *u32) callconv(cc) Status,
13 _write: *const fn (*SerialIo, *usize, *const anyopaque) callconv(cc) Status,
14 _read: *const fn (*SerialIo, *usize, *anyopaque) callconv(cc) Status,
15 mode: *Mode,
16 device_type_guid: ?*Guid,
17
18 pub const ResetError = uefi.UnexpectedError || error{DeviceError};
19 pub const SetAttributeError = uefi.UnexpectedError || error{
20 InvalidParameter,
21 DeviceError,
22 };
23 pub const SetControlError = uefi.UnexpectedError || error{
24 Unsupported,
25 DeviceError,
26 };
27 pub const GetControlError = uefi.UnexpectedError || error{DeviceError};
28 pub const WriteError = uefi.UnexpectedError || error{
29 DeviceError,
30 Timeout,
31 };
32 pub const ReadError = uefi.UnexpectedError || error{
33 DeviceError,
34 Timeout,
35 };
36
37 /// Resets the serial device.
38 pub fn reset(self: *SerialIo) ResetError!void {
39 switch (self._reset(self)) {
40 .success => {},
41 .device_error => return error.DeviceError,
42 else => |status| return uefi.unexpectedStatus(status),
43 }
44 }
45
46 /// Sets the baud rate, receive FIFO depth, transmit/receive time out, parity, data bits, and stop bits on a serial device.
47 pub fn setAttribute(
48 self: *SerialIo,
49 baud_rate: u64,
50 receiver_fifo_depth: u32,
51 timeout: u32,
52 parity: ParityType,
53 data_bits: u8,
54 stop_bits: StopBitsType,
55 ) SetAttributeError!void {
56 switch (self._set_attribute(
57 self,
58 baud_rate,
59 receiver_fifo_depth,
60 timeout,
61 parity,
62 data_bits,
63 stop_bits,
64 )) {
65 .success => {},
66 .invalid_parameter => return error.InvalidParameter,
67 .device_error => return error.DeviceError,
68 else => |status| return uefi.unexpectedStatus(status),
69 }
70 }
71
72 /// Sets the control bits on a serial device.
73 pub fn setControl(self: *SerialIo, control: u32) SetControlError!void {
74 switch (self._set_control(self, control)) {
75 .success => {},
76 .unsupported => return error.Unsupported,
77 .device_error => return error.DeviceError,
78 else => |status| return uefi.unexpectedStatus(status),
79 }
80 }
81
82 /// Retrieves the status of the control bits on a serial device.
83 pub fn getControl(self: *SerialIo) GetControlError!u32 {
84 var control: u32 = undefined;
85 switch (self._get_control(self, &control)) {
86 .success => return control,
87 .device_error => return error.DeviceError,
88 else => |status| return uefi.unexpectedStatus(status),
89 }
90 }
91
92 /// Writes data to a serial device.
93 pub fn write(self: *SerialIo, buffer: []const u8) WriteError!usize {
94 var len: usize = buffer.len;
95 switch (self._write(self, &len, buffer.ptr)) {
96 .success => return len,
97 .device_error => return error.DeviceError,
98 .timeout => return error.Timeout,
99 else => |status| return uefi.unexpectedStatus(status),
100 }
101 }
102
103 /// Reads data from a serial device.
104 pub fn read(self: *SerialIo, buffer: []u8) ReadError!usize {
105 var len: usize = buffer.len;
106 switch (self._read(self, &len, buffer.ptr)) {
107 .success => return len,
108 .device_error => return error.DeviceError,
109 .timeout => return error.Timeout,
110 else => |status| return uefi.unexpectedStatus(status),
111 }
112 }
113
114 pub const guid align(8) = Guid{
115 .time_low = 0xBB25CF6F,
116 .time_mid = 0xF1D4,
117 .time_high_and_version = 0x11D2,
118 .clock_seq_high_and_reserved = 0x9a,
119 .clock_seq_low = 0x0c,
120 .node = [_]u8{ 0x00, 0x90, 0x27, 0x3f, 0xc1, 0xfd },
121 };
122
123 pub const ParityType = enum(u32) {
124 default_parity,
125 no_parity,
126 even_parity,
127 odd_parity,
128 mark_parity,
129 space_parity,
130 };
131
132 pub const StopBitsType = enum(u32) {
133 default_stop_bits,
134 one_stop_bit,
135 one_five_stop_bits,
136 two_stop_bits,
137 };
138
139 pub const Mode = extern struct {
140 control_mask: u32,
141 timeout: u32,
142 baud_rate: u64,
143 receive_fifo_depth: u32,
144 data_bits: u32,
145 parity: u32,
146 stop_bits: u32,
147 };
148};