EmbeddedPkg: Fix mispellings
[mirror_edk2.git] / EmbeddedPkg / SerialDxe / SerialIo.c
1 /** @file
2 Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system
3 running GDB. One console for error information and another console for user input/output.
4
5 Basic packet format is $packet-data#checksum. So every command has 4 bytes of overhead: $,
6 #, 0, 0. The 0 and 0 are the ascii characters for the checksum.
7
8
9 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
10
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include <PiDxe.h>
22 #include <Library/UefiLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/SerialPortLib.h>
26 #include <Library/PcdLib.h>
27
28 #include <Protocol/SerialIo.h>
29
30 /**
31 Reset the serial device.
32
33 @param This Protocol instance pointer.
34
35 @retval EFI_SUCCESS The device was reset.
36 @retval EFI_DEVICE_ERROR The serial device could not be reset.
37
38 **/
39 EFI_STATUS
40 EFIAPI
41 SerialReset (
42 IN EFI_SERIAL_IO_PROTOCOL *This
43 )
44 {
45 SerialPortInitialize ();
46 return EFI_SUCCESS;
47 }
48
49
50 /**
51 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
52 data buts, and stop bits on a serial device.
53
54 @param This Protocol instance pointer.
55 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
56 device's default interface speed.
57 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
58 serial interface. A ReceiveFifoDepth value of 0 will use
59 the device's default FIFO depth.
60 @param Timeout The requested time out for a single character in microseconds.
61 This timeout applies to both the transmit and receive side of the
62 interface. A Timeout value of 0 will use the device's default time
63 out value.
64 @param Parity The type of parity to use on this serial device. A Parity value of
65 DefaultParity will use the device's default parity value.
66 @param DataBits The number of data bits to use on the serial device. A DataBits
67 value of 0 will use the device's default data bit setting.
68 @param StopBits The number of stop bits to use on this serial device. A StopBits
69 value of DefaultStopBits will use the device's default number of
70 stop bits.
71
72 @retval EFI_SUCCESS The device was reset.
73 @retval EFI_DEVICE_ERROR The serial device could not be reset.
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 SerialSetAttributes (
79 IN EFI_SERIAL_IO_PROTOCOL *This,
80 IN UINT64 BaudRate,
81 IN UINT32 ReceiveFifoDepth,
82 IN UINT32 Timeout,
83 IN EFI_PARITY_TYPE Parity,
84 IN UINT8 DataBits,
85 IN EFI_STOP_BITS_TYPE StopBits
86 )
87 {
88 return EFI_UNSUPPORTED;
89 }
90
91
92 /**
93 Set the control bits on a serial device
94
95 @param This Protocol instance pointer.
96 @param Control Set the bits of Control that are settable.
97
98 @retval EFI_SUCCESS The new control bits were set on the serial device.
99 @retval EFI_UNSUPPORTED The serial device does not support this operation.
100 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
101
102 **/
103 EFI_STATUS
104 EFIAPI
105 SerialSetControl (
106 IN EFI_SERIAL_IO_PROTOCOL *This,
107 IN UINT32 Control
108 )
109 {
110 return EFI_UNSUPPORTED;
111 }
112
113
114 /**
115 Retrieves the status of the control bits on a serial device
116
117 @param This Protocol instance pointer.
118 @param Control A pointer to return the current Control signals from the serial device.
119
120 @retval EFI_SUCCESS The control bits were read from the serial device.
121 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
122
123 **/
124 EFI_STATUS
125 EFIAPI
126 SerialGetControl (
127 IN EFI_SERIAL_IO_PROTOCOL *This,
128 OUT UINT32 *Control
129 )
130 {
131 if (SerialPortPoll ()) {
132 // If a character is pending don't set EFI_SERIAL_INPUT_BUFFER_EMPTY
133 *Control = EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
134 } else {
135 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY | EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
136 }
137 return EFI_SUCCESS;
138 }
139
140
141 /**
142 Writes data to a serial device.
143
144 @param This Protocol instance pointer.
145 @param BufferSize On input, the size of the Buffer. On output, the amount of
146 data actually written.
147 @param Buffer The buffer of data to write
148
149 @retval EFI_SUCCESS The data was written.
150 @retval EFI_DEVICE_ERROR The device reported an error.
151 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
152
153 **/
154 EFI_STATUS
155 EFIAPI
156 SerialWrite (
157 IN EFI_SERIAL_IO_PROTOCOL *This,
158 IN OUT UINTN *BufferSize,
159 IN VOID *Buffer
160 )
161 {
162 UINTN Count;
163
164 Count = SerialPortWrite (Buffer, *BufferSize);
165 *BufferSize = Count;
166 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
167 }
168
169 /**
170 Writes data to a serial device.
171
172 @param This Protocol instance pointer.
173 @param BufferSize On input, the size of the Buffer. On output, the amount of
174 data returned in Buffer.
175 @param Buffer The buffer to return the data into.
176
177 @retval EFI_SUCCESS The data was read.
178 @retval EFI_DEVICE_ERROR The device reported an error.
179 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
180
181 **/
182
183 EFI_STATUS
184 EFIAPI
185 SerialRead (
186 IN EFI_SERIAL_IO_PROTOCOL *This,
187 IN OUT UINTN *BufferSize,
188 OUT VOID *Buffer
189 )
190 {
191 UINTN Count = 0;
192
193 if (SerialPortPoll()) {
194 Count = SerialPortRead (Buffer, *BufferSize);
195 *BufferSize = Count;
196 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
197 }
198
199 // No data to return
200 *BufferSize = 0;
201 return EFI_SUCCESS;
202 }
203
204
205 EFI_HANDLE gHandle = NULL;
206
207 //
208 // Template used to initialize the GDB Serial IO protocols
209 //
210 EFI_SERIAL_IO_MODE gSerialIoMode = {
211 0, // ControlMask
212 0, // Timeout
213 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
214 1, // ReceiveFifoDepth
215 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
216 FixedPcdGet8 (PcdUartDefaultParity), // Parity
217 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
218 };
219
220
221 EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {
222 SERIAL_IO_INTERFACE_REVISION,
223 SerialReset,
224 SerialSetAttributes,
225 SerialSetControl,
226 SerialGetControl,
227 SerialWrite,
228 SerialRead,
229 &gSerialIoMode
230 };
231
232 typedef struct {
233 VENDOR_DEVICE_PATH Guid;
234 UART_DEVICE_PATH Uart;
235 EFI_DEVICE_PATH_PROTOCOL End;
236 } SIMPLE_TEXT_OUT_DEVICE_PATH;
237
238 SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath = {
239 {
240 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, sizeof (VENDOR_DEVICE_PATH), 0},
241 EFI_CALLER_ID_GUID // Use the drivers GUID
242 },
243 {
244 { MESSAGING_DEVICE_PATH, MSG_UART_DP, sizeof (UART_DEVICE_PATH), 0},
245 0, // Reserved
246 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
247 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
248 FixedPcdGet8 (PcdUartDefaultParity), // Parity (N)
249 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
250 },
251 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof (EFI_DEVICE_PATH_PROTOCOL), 0}
252 };
253
254
255 /**
256 Initialize the state information for the Serial Io Protocol
257
258 @param ImageHandle of the loaded driver
259 @param SystemTable Pointer to the System Table
260
261 @retval EFI_SUCCESS Protocol registered
262 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
263 @retval EFI_DEVICE_ERROR Hardware problems
264
265 **/
266 EFI_STATUS
267 EFIAPI
268 SerialDxeInitialize (
269 IN EFI_HANDLE ImageHandle,
270 IN EFI_SYSTEM_TABLE *SystemTable
271 )
272 {
273 EFI_STATUS Status;
274
275
276 // Make a new handle with Serial IO protocol and its device path on it.
277 Status = gBS->InstallMultipleProtocolInterfaces (
278 &gHandle,
279 &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,
280 &gEfiDevicePathProtocolGuid, &mDevicePath,
281 NULL
282 );
283 ASSERT_EFI_ERROR (Status);
284
285 return Status;
286 }
287