]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SerialDxe/SerialIo.c
MdeModulePkg: Convert all .uni files to utf-8
[mirror_edk2.git] / MdeModulePkg / Universal / SerialDxe / SerialIo.c
CommitLineData
bf6b810f
SZ
1/** @file\r
2 Serial driver that layers on top of a Serial Port Library instance.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/SerialPortLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
23#include <Protocol/SerialIo.h>\r
24#include <Protocol/DevicePath.h>\r
25\r
26typedef struct {\r
27 VENDOR_DEVICE_PATH Guid;\r
28 UART_DEVICE_PATH Uart;\r
29 EFI_DEVICE_PATH_PROTOCOL End;\r
30} SERIAL_DEVICE_PATH;\r
31\r
32/**\r
33 Reset the serial device.\r
34\r
35 @param This Protocol instance pointer.\r
36\r
37 @retval EFI_SUCCESS The device was reset.\r
38 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
39\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43SerialReset (\r
44 IN EFI_SERIAL_IO_PROTOCOL *This\r
45 );\r
46\r
47/**\r
48 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
49 data bits, and stop bits on a serial device.\r
50\r
51 @param This Protocol instance pointer.\r
52 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
53 device's default interface speed.\r
54 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
55 serial interface. A ReceiveFifoDepth value of 0 will use\r
56 the device's default FIFO depth.\r
57 @param Timeout The requested time out for a single character in microseconds.\r
58 This timeout applies to both the transmit and receive side of the\r
59 interface. A Timeout value of 0 will use the device's default time\r
60 out value.\r
61 @param Parity The type of parity to use on this serial device. A Parity value of\r
62 DefaultParity will use the device's default parity value.\r
63 @param DataBits The number of data bits to use on the serial device. A DataBits\r
64 value of 0 will use the device's default data bit setting.\r
65 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
66 value of DefaultStopBits will use the device's default number of\r
67 stop bits.\r
68\r
69 @retval EFI_SUCCESS The device was reset.\r
70 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
71\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75SerialSetAttributes (\r
76 IN EFI_SERIAL_IO_PROTOCOL *This,\r
77 IN UINT64 BaudRate,\r
78 IN UINT32 ReceiveFifoDepth,\r
79 IN UINT32 Timeout,\r
80 IN EFI_PARITY_TYPE Parity,\r
81 IN UINT8 DataBits,\r
82 IN EFI_STOP_BITS_TYPE StopBits\r
83 );\r
84\r
85/**\r
86 Set the control bits on a serial device\r
87\r
88 @param This Protocol instance pointer.\r
89 @param Control Set the bits of Control that are settable.\r
90\r
91 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
92 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
93 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
94\r
95**/\r
96EFI_STATUS\r
97EFIAPI\r
98SerialSetControl (\r
99 IN EFI_SERIAL_IO_PROTOCOL *This,\r
100 IN UINT32 Control\r
101 );\r
102\r
103/**\r
104 Retrieves the status of the control bits on a serial device\r
105\r
106 @param This Protocol instance pointer.\r
107 @param Control A pointer to return the current Control signals from the serial device.\r
108\r
109 @retval EFI_SUCCESS The control bits were read from the serial device.\r
110 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
111\r
112**/\r
113EFI_STATUS\r
114EFIAPI\r
115SerialGetControl (\r
116 IN EFI_SERIAL_IO_PROTOCOL *This,\r
117 OUT UINT32 *Control\r
118 );\r
119\r
120/**\r
121 Writes data to a serial device.\r
122\r
123 @param This Protocol instance pointer.\r
124 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
125 data actually written.\r
126 @param Buffer The buffer of data to write\r
127\r
128 @retval EFI_SUCCESS The data was written.\r
129 @retval EFI_DEVICE_ERROR The device reported an error.\r
130 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
131\r
132**/\r
133EFI_STATUS\r
134EFIAPI\r
135SerialWrite (\r
136 IN EFI_SERIAL_IO_PROTOCOL *This,\r
137 IN OUT UINTN *BufferSize,\r
138 IN VOID *Buffer\r
139 );\r
140\r
141/**\r
142 Reads data from 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 returned in Buffer.\r
147 @param Buffer The buffer to return the data into.\r
148\r
149 @retval EFI_SUCCESS The data was read.\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
156SerialRead (\r
157 IN EFI_SERIAL_IO_PROTOCOL *This,\r
158 IN OUT UINTN *BufferSize,\r
159 OUT VOID *Buffer\r
160 );\r
161\r
162EFI_HANDLE mSerialHandle = NULL;\r
163\r
164SERIAL_DEVICE_PATH mSerialDevicePath = {\r
165 {\r
166 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
167 EFI_CALLER_ID_GUID // Use the driver's GUID\r
168 },\r
169 {\r
170 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
171 0, // Reserved\r
172 0, // BaudRate\r
173 0, // DataBits\r
174 0, // Parity\r
175 0 // StopBits\r
176 },\r
177 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
178};\r
179\r
180//\r
181// Template used to initialize the Serial IO protocols.\r
182//\r
183EFI_SERIAL_IO_MODE mSerialIoMode = {\r
184 0, // ControlMask\r
185 0, // Timeout\r
186 0, // BaudRate\r
187 1, // ReceiveFifoDepth\r
188 0, // DataBits\r
189 0, // Parity\r
190 0 // StopBits\r
191};\r
192\r
193EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate = {\r
194 SERIAL_IO_INTERFACE_REVISION,\r
195 SerialReset,\r
196 SerialSetAttributes,\r
197 SerialSetControl,\r
198 SerialGetControl,\r
199 SerialWrite,\r
200 SerialRead,\r
201 &mSerialIoMode\r
202};\r
203\r
204/**\r
205 Reset the serial device.\r
206\r
207 @param This Protocol instance pointer.\r
208\r
209 @retval EFI_SUCCESS The device was reset.\r
210 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
211\r
212**/\r
213EFI_STATUS\r
214EFIAPI\r
215SerialReset (\r
216 IN EFI_SERIAL_IO_PROTOCOL *This\r
217 )\r
218{\r
219 EFI_STATUS Status;\r
220 EFI_TPL Tpl;\r
221\r
222 Status = SerialPortInitialize ();\r
223 if (EFI_ERROR (Status)) {\r
224 return Status;\r
225 }\r
226\r
227 //\r
228 // Set the Serial I/O mode and update the device path\r
229 //\r
230\r
231 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
232\r
233 //\r
234 // Set the Serial I/O mode\r
235 //\r
236 This->Mode->ReceiveFifoDepth = 1;\r
237 This->Mode->Timeout = 0;\r
238 This->Mode->BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
239 This->Mode->DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
240 This->Mode->Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
241 This->Mode->StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
242\r
243 //\r
244 // Check if the device path has actually changed\r
245 //\r
246 if (mSerialDevicePath.Uart.BaudRate == This->Mode->BaudRate &&\r
247 mSerialDevicePath.Uart.DataBits == (UINT8) This->Mode->DataBits &&\r
248 mSerialDevicePath.Uart.Parity == (UINT8) This->Mode->Parity &&\r
249 mSerialDevicePath.Uart.StopBits == (UINT8) This->Mode->StopBits\r
250 ) {\r
251 gBS->RestoreTPL (Tpl);\r
252 return EFI_SUCCESS;\r
253 }\r
254\r
255 //\r
256 // Update the device path\r
257 //\r
258 mSerialDevicePath.Uart.BaudRate = This->Mode->BaudRate;\r
259 mSerialDevicePath.Uart.DataBits = (UINT8) This->Mode->DataBits;\r
260 mSerialDevicePath.Uart.Parity = (UINT8) This->Mode->Parity;\r
261 mSerialDevicePath.Uart.StopBits = (UINT8) This->Mode->StopBits;\r
262\r
263 Status = gBS->ReinstallProtocolInterface (\r
264 mSerialHandle,\r
265 &gEfiDevicePathProtocolGuid,\r
266 &mSerialDevicePath,\r
267 &mSerialDevicePath\r
268 );\r
269\r
270 gBS->RestoreTPL (Tpl);\r
271\r
272 return Status;\r
273}\r
274\r
275/**\r
276 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
277 data bits, and stop bits on a serial device.\r
278\r
279 @param This Protocol instance pointer.\r
280 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
281 device's default interface speed.\r
282 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
283 serial interface. A ReceiveFifoDepth value of 0 will use\r
284 the device's default FIFO depth.\r
285 @param Timeout The requested time out for a single character in microseconds.\r
286 This timeout applies to both the transmit and receive side of the\r
287 interface. A Timeout value of 0 will use the device's default time\r
288 out value.\r
289 @param Parity The type of parity to use on this serial device. A Parity value of\r
290 DefaultParity will use the device's default parity value.\r
291 @param DataBits The number of data bits to use on the serial device. A DataBits\r
292 value of 0 will use the device's default data bit setting.\r
293 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
294 value of DefaultStopBits will use the device's default number of\r
295 stop bits.\r
296\r
297 @retval EFI_SUCCESS The device was reset.\r
298 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
299\r
300**/\r
301EFI_STATUS\r
302EFIAPI\r
303SerialSetAttributes (\r
304 IN EFI_SERIAL_IO_PROTOCOL *This,\r
305 IN UINT64 BaudRate,\r
306 IN UINT32 ReceiveFifoDepth,\r
307 IN UINT32 Timeout,\r
308 IN EFI_PARITY_TYPE Parity,\r
309 IN UINT8 DataBits,\r
310 IN EFI_STOP_BITS_TYPE StopBits\r
311 )\r
312{\r
313 EFI_STATUS Status;\r
314 EFI_TPL Tpl;\r
315\r
316 Status = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
317 if (EFI_ERROR (Status)) {\r
318 return Status;\r
319 }\r
320\r
321 //\r
322 // Set the Serial I/O mode and update the device path\r
323 //\r
324\r
325 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
326\r
327 //\r
328 // Set the Serial I/O mode\r
329 //\r
330 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
331 This->Mode->Timeout = Timeout;\r
332 This->Mode->BaudRate = BaudRate;\r
333 This->Mode->DataBits = (UINT32) DataBits;\r
334 This->Mode->Parity = (UINT32) Parity;\r
335 This->Mode->StopBits = (UINT32) StopBits;\r
336\r
337 //\r
338 // Check if the device path has actually changed\r
339 //\r
340 if (mSerialDevicePath.Uart.BaudRate == BaudRate &&\r
341 mSerialDevicePath.Uart.DataBits == DataBits &&\r
342 mSerialDevicePath.Uart.Parity == (UINT8) Parity &&\r
343 mSerialDevicePath.Uart.StopBits == (UINT8) StopBits\r
344 ) {\r
345 gBS->RestoreTPL (Tpl);\r
346 return EFI_SUCCESS;\r
347 }\r
348\r
349 //\r
350 // Update the device path\r
351 //\r
352 mSerialDevicePath.Uart.BaudRate = BaudRate;\r
353 mSerialDevicePath.Uart.DataBits = DataBits;\r
354 mSerialDevicePath.Uart.Parity = (UINT8) Parity;\r
355 mSerialDevicePath.Uart.StopBits = (UINT8) StopBits;\r
356\r
357 Status = gBS->ReinstallProtocolInterface (\r
358 mSerialHandle,\r
359 &gEfiDevicePathProtocolGuid,\r
360 &mSerialDevicePath,\r
361 &mSerialDevicePath\r
362 );\r
363\r
364 gBS->RestoreTPL (Tpl);\r
365\r
366 return Status;\r
367}\r
368\r
369/**\r
370 Set the control bits on a serial device\r
371\r
372 @param This Protocol instance pointer.\r
373 @param Control Set the bits of Control that are settable.\r
374\r
375 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
376 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
377 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
378\r
379**/\r
380EFI_STATUS\r
381EFIAPI\r
382SerialSetControl (\r
383 IN EFI_SERIAL_IO_PROTOCOL *This,\r
384 IN UINT32 Control\r
385 )\r
386{\r
387 return SerialPortSetControl (Control);\r
388}\r
389\r
390/**\r
391 Retrieves the status of the control bits on a serial device\r
392\r
393 @param This Protocol instance pointer.\r
394 @param Control A pointer to return the current Control signals from the serial device.\r
395\r
396 @retval EFI_SUCCESS The control bits were read from the serial device.\r
397 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
398\r
399**/\r
400EFI_STATUS\r
401EFIAPI\r
402SerialGetControl (\r
403 IN EFI_SERIAL_IO_PROTOCOL *This,\r
404 OUT UINT32 *Control\r
405 )\r
406{\r
407 return SerialPortGetControl (Control);\r
408}\r
409\r
410/**\r
411 Writes data to a serial device.\r
412\r
413 @param This Protocol instance pointer.\r
414 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
415 data actually written.\r
416 @param Buffer The buffer of data to write\r
417\r
418 @retval EFI_SUCCESS The data was written.\r
419 @retval EFI_DEVICE_ERROR The device reported an error.\r
420 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
421\r
422**/\r
423EFI_STATUS\r
424EFIAPI\r
425SerialWrite (\r
426 IN EFI_SERIAL_IO_PROTOCOL *This,\r
427 IN OUT UINTN *BufferSize,\r
428 IN VOID *Buffer\r
429 )\r
430{\r
431 UINTN Count;\r
432\r
433 Count = SerialPortWrite (Buffer, *BufferSize);\r
434\r
435 if (Count != *BufferSize) {\r
436 *BufferSize = Count;\r
437 return EFI_TIMEOUT;\r
438 }\r
439\r
440 return EFI_SUCCESS;\r
441}\r
442\r
443/**\r
444 Reads data from a serial device.\r
445\r
446 @param This Protocol instance pointer.\r
447 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
448 data returned in Buffer.\r
449 @param Buffer The buffer to return the data into.\r
450\r
451 @retval EFI_SUCCESS The data was read.\r
452 @retval EFI_DEVICE_ERROR The device reported an error.\r
453 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
454\r
455**/\r
456EFI_STATUS\r
457EFIAPI\r
458SerialRead (\r
459 IN EFI_SERIAL_IO_PROTOCOL *This,\r
460 IN OUT UINTN *BufferSize,\r
461 OUT VOID *Buffer\r
462 )\r
463{\r
464 UINTN Count;\r
465\r
466 Count = 0;\r
467\r
468 if (SerialPortPoll ()) {\r
469 Count = SerialPortRead (Buffer, *BufferSize);\r
470 }\r
471\r
472 if (Count != *BufferSize) {\r
473 *BufferSize = Count;\r
474 return EFI_TIMEOUT;\r
475 }\r
476\r
477 return EFI_SUCCESS;\r
478}\r
479\r
480/**\r
481 Initialization for the Serial Io Protocol.\r
482\r
483 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
484 @param[in] SystemTable A pointer to the EFI System Table.\r
485\r
486 @retval EFI_SUCCESS The entry point is executed successfully.\r
487 @retval other Some error occurs when executing this entry point.\r
488\r
489**/\r
490EFI_STATUS\r
491EFIAPI\r
492SerialDxeInitialize (\r
493 IN EFI_HANDLE ImageHandle,\r
494 IN EFI_SYSTEM_TABLE *SystemTable\r
495 )\r
496{\r
497 EFI_STATUS Status;\r
498\r
499 Status = SerialPortInitialize ();\r
500 if (EFI_ERROR (Status)) {\r
501 return Status;\r
502 }\r
503\r
504 mSerialIoMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
505 mSerialIoMode.DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
506 mSerialIoMode.Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
507 mSerialIoMode.StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
508 mSerialDevicePath.Uart.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
509 mSerialDevicePath.Uart.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
510 mSerialDevicePath.Uart.Parity = PcdGet8 (PcdUartDefaultParity);\r
511 mSerialDevicePath.Uart.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
512\r
513 //\r
514 // Make a new handle with Serial IO protocol and its device path on it.\r
515 //\r
516 Status = gBS->InstallMultipleProtocolInterfaces (\r
517 &mSerialHandle,\r
518 &gEfiSerialIoProtocolGuid, &mSerialIoTemplate,\r
519 &gEfiDevicePathProtocolGuid, &mSerialDevicePath,\r
520 NULL\r
521 );\r
522 ASSERT_EFI_ERROR (Status);\r
523\r
524 return Status;\r
525}\r
526\r