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