]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SerialDxe/SerialIo.c
MdeModulePkg/SerialDxe: Fix return valued in SerialSetAttributes
[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
13d378fc
JG
69 @retval EFI_SUCCESS The device was reset.\r
70 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.\r
71 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
bf6b810f
SZ
72\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76SerialSetAttributes (\r
77 IN EFI_SERIAL_IO_PROTOCOL *This,\r
78 IN UINT64 BaudRate,\r
79 IN UINT32 ReceiveFifoDepth,\r
80 IN UINT32 Timeout,\r
81 IN EFI_PARITY_TYPE Parity,\r
82 IN UINT8 DataBits,\r
83 IN EFI_STOP_BITS_TYPE StopBits\r
84 );\r
85\r
86/**\r
87 Set the control bits on a serial device\r
88\r
89 @param This Protocol instance pointer.\r
90 @param Control Set the bits of Control that are settable.\r
91\r
92 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
93 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
94 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
95\r
96**/\r
97EFI_STATUS\r
98EFIAPI\r
99SerialSetControl (\r
100 IN EFI_SERIAL_IO_PROTOCOL *This,\r
101 IN UINT32 Control\r
102 );\r
103\r
104/**\r
105 Retrieves the status of the control bits on a serial device\r
106\r
107 @param This Protocol instance pointer.\r
108 @param Control A pointer to return the current Control signals from the serial device.\r
109\r
110 @retval EFI_SUCCESS The control bits were read from the serial device.\r
111 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
112\r
113**/\r
114EFI_STATUS\r
115EFIAPI\r
116SerialGetControl (\r
117 IN EFI_SERIAL_IO_PROTOCOL *This,\r
118 OUT UINT32 *Control\r
119 );\r
120\r
121/**\r
122 Writes data to a serial device.\r
123\r
124 @param This Protocol instance pointer.\r
125 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
126 data actually written.\r
127 @param Buffer The buffer of data to write\r
128\r
129 @retval EFI_SUCCESS The data was written.\r
130 @retval EFI_DEVICE_ERROR The device reported an error.\r
131 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
132\r
133**/\r
134EFI_STATUS\r
135EFIAPI\r
136SerialWrite (\r
137 IN EFI_SERIAL_IO_PROTOCOL *This,\r
138 IN OUT UINTN *BufferSize,\r
139 IN VOID *Buffer\r
140 );\r
141\r
142/**\r
143 Reads data from a serial device.\r
144\r
145 @param This Protocol instance pointer.\r
146 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
147 data returned in Buffer.\r
148 @param Buffer The buffer to return the data into.\r
149\r
150 @retval EFI_SUCCESS The data was read.\r
151 @retval EFI_DEVICE_ERROR The device reported an error.\r
152 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
153\r
154**/\r
155EFI_STATUS\r
156EFIAPI\r
157SerialRead (\r
158 IN EFI_SERIAL_IO_PROTOCOL *This,\r
159 IN OUT UINTN *BufferSize,\r
160 OUT VOID *Buffer\r
161 );\r
162\r
163EFI_HANDLE mSerialHandle = NULL;\r
164\r
165SERIAL_DEVICE_PATH mSerialDevicePath = {\r
166 {\r
167 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
168 EFI_CALLER_ID_GUID // Use the driver's GUID\r
169 },\r
170 {\r
171 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
172 0, // Reserved\r
173 0, // BaudRate\r
174 0, // DataBits\r
175 0, // Parity\r
176 0 // StopBits\r
177 },\r
178 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
179};\r
180\r
181//\r
182// Template used to initialize the Serial IO protocols.\r
183//\r
184EFI_SERIAL_IO_MODE mSerialIoMode = {\r
c0beed6d
LE
185 //\r
186 // value field set in SerialDxeInitialize()?\r
187 //--------- ------------------- -----------------------------\r
188 0, // ControlMask\r
ea012619 189 1000 * 1000, // Timeout\r
c0beed6d
LE
190 0, // BaudRate yes\r
191 1, // ReceiveFifoDepth\r
192 0, // DataBits yes\r
193 0, // Parity yes\r
194 0 // StopBits yes\r
bf6b810f
SZ
195};\r
196\r
197EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate = {\r
198 SERIAL_IO_INTERFACE_REVISION,\r
199 SerialReset,\r
200 SerialSetAttributes,\r
201 SerialSetControl,\r
202 SerialGetControl,\r
203 SerialWrite,\r
204 SerialRead,\r
205 &mSerialIoMode\r
206};\r
207\r
208/**\r
209 Reset the serial device.\r
210\r
211 @param This Protocol instance pointer.\r
212\r
213 @retval EFI_SUCCESS The device was reset.\r
214 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
215\r
216**/\r
217EFI_STATUS\r
218EFIAPI\r
219SerialReset (\r
220 IN EFI_SERIAL_IO_PROTOCOL *This\r
221 )\r
222{\r
223 EFI_STATUS Status;\r
bf6b810f
SZ
224\r
225 Status = SerialPortInitialize ();\r
226 if (EFI_ERROR (Status)) {\r
227 return Status;\r
228 }\r
229\r
230 //\r
91cc526b 231 // Go set the current attributes\r
bf6b810f 232 //\r
91cc526b
PB
233 Status = This->SetAttributes (\r
234 This,\r
235 This->Mode->BaudRate,\r
236 This->Mode->ReceiveFifoDepth,\r
237 This->Mode->Timeout,\r
238 (EFI_PARITY_TYPE) This->Mode->Parity,\r
239 (UINT8) This->Mode->DataBits,\r
240 (EFI_STOP_BITS_TYPE) This->Mode->StopBits\r
241 );\r
bf6b810f
SZ
242\r
243 return Status;\r
244}\r
245\r
246/**\r
247 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
248 data bits, and stop bits on a serial device.\r
249\r
250 @param This Protocol instance pointer.\r
251 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
252 device's default interface speed.\r
253 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
254 serial interface. A ReceiveFifoDepth value of 0 will use\r
255 the device's default FIFO depth.\r
256 @param Timeout The requested time out for a single character in microseconds.\r
257 This timeout applies to both the transmit and receive side of the\r
258 interface. A Timeout value of 0 will use the device's default time\r
259 out value.\r
260 @param Parity The type of parity to use on this serial device. A Parity value of\r
261 DefaultParity will use the device's default parity value.\r
262 @param DataBits The number of data bits to use on the serial device. A DataBits\r
263 value of 0 will use the device's default data bit setting.\r
264 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
265 value of DefaultStopBits will use the device's default number of\r
266 stop bits.\r
267\r
13d378fc
JG
268 @retval EFI_SUCCESS The device was reset.\r
269 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.\r
270 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
bf6b810f
SZ
271\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275SerialSetAttributes (\r
276 IN EFI_SERIAL_IO_PROTOCOL *This,\r
277 IN UINT64 BaudRate,\r
278 IN UINT32 ReceiveFifoDepth,\r
279 IN UINT32 Timeout,\r
280 IN EFI_PARITY_TYPE Parity,\r
281 IN UINT8 DataBits,\r
282 IN EFI_STOP_BITS_TYPE StopBits\r
283 )\r
284{\r
a7fd8452
SZ
285 EFI_STATUS Status;\r
286 EFI_TPL Tpl;\r
287 UINT64 OriginalBaudRate;\r
288 UINT32 OriginalReceiveFifoDepth;\r
289 UINT32 OriginalTimeout;\r
290 EFI_PARITY_TYPE OriginalParity;\r
291 UINT8 OriginalDataBits;\r
292 EFI_STOP_BITS_TYPE OriginalStopBits;\r
bf6b810f 293\r
a7fd8452
SZ
294 //\r
295 // Preserve the original input values in case\r
296 // SerialPortSetAttributes() updates the input/output\r
297 // parameters even on error.\r
298 //\r
299 OriginalBaudRate = BaudRate;\r
300 OriginalReceiveFifoDepth = ReceiveFifoDepth;\r
301 OriginalTimeout = Timeout;\r
302 OriginalParity = Parity;\r
303 OriginalDataBits = DataBits;\r
304 OriginalStopBits = StopBits;\r
bf6b810f
SZ
305 Status = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
306 if (EFI_ERROR (Status)) {\r
a7fd8452
SZ
307 //\r
308 // If it is just to set Timeout value and unsupported is returned,\r
309 // do not return error.\r
310 //\r
311 if ((Status == EFI_UNSUPPORTED) &&\r
312 (This->Mode->Timeout != OriginalTimeout) &&\r
313 (This->Mode->ReceiveFifoDepth == OriginalReceiveFifoDepth) &&\r
314 (This->Mode->BaudRate == OriginalBaudRate) &&\r
315 (This->Mode->DataBits == (UINT32) OriginalDataBits) &&\r
316 (This->Mode->Parity == (UINT32) OriginalParity) &&\r
317 (This->Mode->StopBits == (UINT32) OriginalStopBits)) {\r
318 //\r
319 // Restore to the original input values.\r
320 //\r
321 BaudRate = OriginalBaudRate;\r
322 ReceiveFifoDepth = OriginalReceiveFifoDepth;\r
323 Timeout = OriginalTimeout;\r
324 Parity = OriginalParity;\r
325 DataBits = OriginalDataBits;\r
326 StopBits = OriginalStopBits;\r
327 Status = EFI_SUCCESS;\r
13d378fc
JG
328 } else if (Status == EFI_INVALID_PARAMETER || Status == EFI_UNSUPPORTED) {\r
329 return EFI_INVALID_PARAMETER;\r
a7fd8452 330 } else {\r
13d378fc 331 return EFI_DEVICE_ERROR;\r
a7fd8452 332 }\r
bf6b810f
SZ
333 }\r
334\r
335 //\r
336 // Set the Serial I/O mode and update the device path\r
337 //\r
338\r
339 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
340\r
341 //\r
342 // Set the Serial I/O mode\r
343 //\r
344 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
345 This->Mode->Timeout = Timeout;\r
346 This->Mode->BaudRate = BaudRate;\r
347 This->Mode->DataBits = (UINT32) DataBits;\r
348 This->Mode->Parity = (UINT32) Parity;\r
349 This->Mode->StopBits = (UINT32) StopBits;\r
350\r
351 //\r
352 // Check if the device path has actually changed\r
353 //\r
354 if (mSerialDevicePath.Uart.BaudRate == BaudRate &&\r
355 mSerialDevicePath.Uart.DataBits == DataBits &&\r
356 mSerialDevicePath.Uart.Parity == (UINT8) Parity &&\r
357 mSerialDevicePath.Uart.StopBits == (UINT8) StopBits\r
358 ) {\r
359 gBS->RestoreTPL (Tpl);\r
360 return EFI_SUCCESS;\r
361 }\r
362\r
363 //\r
364 // Update the device path\r
365 //\r
366 mSerialDevicePath.Uart.BaudRate = BaudRate;\r
367 mSerialDevicePath.Uart.DataBits = DataBits;\r
368 mSerialDevicePath.Uart.Parity = (UINT8) Parity;\r
369 mSerialDevicePath.Uart.StopBits = (UINT8) StopBits;\r
370\r
371 Status = gBS->ReinstallProtocolInterface (\r
372 mSerialHandle,\r
373 &gEfiDevicePathProtocolGuid,\r
374 &mSerialDevicePath,\r
375 &mSerialDevicePath\r
376 );\r
377\r
378 gBS->RestoreTPL (Tpl);\r
379\r
380 return Status;\r
381}\r
382\r
383/**\r
384 Set the control bits on a serial device\r
385\r
386 @param This Protocol instance pointer.\r
387 @param Control Set the bits of Control that are settable.\r
388\r
389 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
390 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
391 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
392\r
393**/\r
394EFI_STATUS\r
395EFIAPI\r
396SerialSetControl (\r
397 IN EFI_SERIAL_IO_PROTOCOL *This,\r
398 IN UINT32 Control\r
399 )\r
400{\r
401 return SerialPortSetControl (Control);\r
402}\r
403\r
404/**\r
405 Retrieves the status of the control bits on a serial device\r
406\r
407 @param This Protocol instance pointer.\r
408 @param Control A pointer to return the current Control signals from the serial device.\r
409\r
410 @retval EFI_SUCCESS The control bits were read from the serial device.\r
411 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
412\r
413**/\r
414EFI_STATUS\r
415EFIAPI\r
416SerialGetControl (\r
417 IN EFI_SERIAL_IO_PROTOCOL *This,\r
418 OUT UINT32 *Control\r
419 )\r
420{\r
421 return SerialPortGetControl (Control);\r
422}\r
423\r
424/**\r
425 Writes data to a serial device.\r
426\r
427 @param This Protocol instance pointer.\r
428 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
429 data actually written.\r
430 @param Buffer The buffer of data to write\r
431\r
432 @retval EFI_SUCCESS The data was written.\r
433 @retval EFI_DEVICE_ERROR The device reported an error.\r
434 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
435\r
436**/\r
437EFI_STATUS\r
438EFIAPI\r
439SerialWrite (\r
440 IN EFI_SERIAL_IO_PROTOCOL *This,\r
441 IN OUT UINTN *BufferSize,\r
442 IN VOID *Buffer\r
443 )\r
444{\r
445 UINTN Count;\r
446\r
447 Count = SerialPortWrite (Buffer, *BufferSize);\r
448\r
449 if (Count != *BufferSize) {\r
450 *BufferSize = Count;\r
451 return EFI_TIMEOUT;\r
452 }\r
453\r
454 return EFI_SUCCESS;\r
455}\r
456\r
457/**\r
458 Reads data from a serial device.\r
459\r
460 @param This Protocol instance pointer.\r
461 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
462 data returned in Buffer.\r
463 @param Buffer The buffer to return the data into.\r
464\r
465 @retval EFI_SUCCESS The data was read.\r
466 @retval EFI_DEVICE_ERROR The device reported an error.\r
467 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
468\r
469**/\r
470EFI_STATUS\r
471EFIAPI\r
472SerialRead (\r
473 IN EFI_SERIAL_IO_PROTOCOL *This,\r
474 IN OUT UINTN *BufferSize,\r
475 OUT VOID *Buffer\r
476 )\r
477{\r
478 UINTN Count;\r
4cf3f37c 479 UINTN TimeOut;\r
bf6b810f
SZ
480\r
481 Count = 0;\r
482\r
4cf3f37c
SZ
483 while (Count < *BufferSize) {\r
484 TimeOut = 0;\r
485 while (TimeOut < mSerialIoMode.Timeout) {\r
486 if (SerialPortPoll ()) {\r
487 break;\r
488 }\r
489 gBS->Stall (10);\r
490 TimeOut += 10;\r
491 }\r
492 if (TimeOut >= mSerialIoMode.Timeout) {\r
493 break;\r
494 }\r
495 SerialPortRead (Buffer, 1);\r
496 Count++;\r
497 Buffer = (VOID *) ((UINT8 *) Buffer + 1);\r
bf6b810f
SZ
498 }\r
499\r
500 if (Count != *BufferSize) {\r
501 *BufferSize = Count;\r
502 return EFI_TIMEOUT;\r
503 }\r
504\r
505 return EFI_SUCCESS;\r
506}\r
507\r
508/**\r
509 Initialization for the Serial Io Protocol.\r
510\r
511 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
512 @param[in] SystemTable A pointer to the EFI System Table.\r
513\r
514 @retval EFI_SUCCESS The entry point is executed successfully.\r
515 @retval other Some error occurs when executing this entry point.\r
516\r
517**/\r
518EFI_STATUS\r
519EFIAPI\r
520SerialDxeInitialize (\r
521 IN EFI_HANDLE ImageHandle,\r
522 IN EFI_SYSTEM_TABLE *SystemTable\r
523 )\r
524{\r
525 EFI_STATUS Status;\r
526\r
bf6b810f
SZ
527 mSerialIoMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
528 mSerialIoMode.DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
529 mSerialIoMode.Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
530 mSerialIoMode.StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
f423d760 531 mSerialIoMode.ReceiveFifoDepth = PcdGet16 (PcdUartDefaultReceiveFifoDepth);\r
bf6b810f
SZ
532 mSerialDevicePath.Uart.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
533 mSerialDevicePath.Uart.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
534 mSerialDevicePath.Uart.Parity = PcdGet8 (PcdUartDefaultParity);\r
535 mSerialDevicePath.Uart.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
536\r
91cc526b
PB
537 //\r
538 // Issue a reset to initialize the Serial Port\r
539 //\r
540 Status = mSerialIoTemplate.Reset (&mSerialIoTemplate);\r
541 if (EFI_ERROR (Status)) {\r
542 return Status;\r
543 }\r
544\r
bf6b810f
SZ
545 //\r
546 // Make a new handle with Serial IO protocol and its device path on it.\r
547 //\r
548 Status = gBS->InstallMultipleProtocolInterfaces (\r
549 &mSerialHandle,\r
550 &gEfiSerialIoProtocolGuid, &mSerialIoTemplate,\r
551 &gEfiDevicePathProtocolGuid, &mSerialDevicePath,\r
552 NULL\r
553 );\r
554 ASSERT_EFI_ERROR (Status);\r
555\r
556 return Status;\r
557}\r
558\r