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