2 Header file for USB Serial Driver's Data Structures.
4 Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.
5 Portions Copyright 2012 Ashley DeSimone
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef _FTDI_USB_SERIAL_DRIVER_H_
11 #define _FTDI_USB_SERIAL_DRIVER_H_
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiLib.h>
18 #include <Library/DevicePathLib.h>
20 #include <Protocol/DevicePath.h>
21 #include <Protocol/UsbIo.h>
22 #include <Protocol/SerialIo.h>
27 #define USB_US_LANG_ID 0x0409
30 // Supported Vendor Ids
32 #define VID_FTDI 0x0403
35 // Supported product ids
37 #define DID_FTDI_FT232 0x6001
42 #define FTDI_COMMAND_RESET_PORT 0
43 #define FTDI_COMMAND_MODEM_CTRL 1
44 #define FTDI_COMMAND_SET_FLOW_CTRL 2
45 #define FTDI_COMMAND_SET_BAUDRATE 3
46 #define FTDI_COMMAND_SET_DATA 4
47 #define FTDI_COMMAND_GET_MODEM_STATUS 5
48 #define FTDI_COMMAND_SET_EVENT_CHAR 6
49 #define FTDI_COMMAND_SET_ERROR_CHAR 7
50 #define FTDI_COMMAND_SET_LATENCY_TIMER 9
51 #define FTDI_COMMAND_GET_LATENCY_TIMER 10
54 // FTDI_PORT_IDENTIFIER
55 // Used in the usb control transfers that issue FTDI commands as the index value.
57 #define FTDI_PORT_IDENTIFIER 0x1 // For FTDI USB serial adapter the port
58 // identifier is always 1.
63 #define RESET_PORT_RESET 0x0 // Purges RX and TX, clears DTR and RTS sets
64 // flow control to none, disables event
65 // trigger, sets the event char to 0x0d and
66 // does nothing to baudrate or data settings
67 #define RESET_PORT_PURGE_RX 0x1
68 #define RESET_PORT_PURGE_TX 0x2
73 #define NO_FLOW_CTRL 0x0
74 #define XON_XOFF_CTRL 0x4
78 // To set baud rate, one must calculate an encoding of the baud rate from
79 // UINT32 to UINT16.See EncodeBaudRateForFtdi() for details
81 #define FTDI_UART_FREQUENCY 3000000
82 #define FTDI_MIN_DIVISOR 0x20
83 #define FTDI_MAX_DIVISOR 0x3FFF8
85 // Special case baudrate values
86 // 300,000 and 200,000 are special cases for calculating the encoded baudrate
88 #define FTDI_SPECIAL_CASE_300_MIN (3000000 * 100) / 103 // minimum adjusted
90 #define FTDI_SPECIAL_CASE_300_MAX (3000000 * 100) / 97 // maximum adjusted
92 #define FTDI_SPECIAL_CASE_200_MIN (2000000 * 100) / 103 // minimum adjusted
94 #define FTDI_SPECIAL_CASE_200_MAX (2000000 * 100) / 97 // maximum adjusted
97 // Min and max frequency values that the FTDI chip can attain
98 //.all generated frequencies must be between these values
100 #define FTDI_MIN_FREQUENCY 46601941 // (3MHz * 1600) / 103 = 46601941
101 #define FTDI_MAX_FREQUENCY 49484536 // (3MHz * 1600) / 97 = 49484536
106 #define SET_DATA_BITS(n) (n)
111 #define SET_PARITY_NONE 0x0
112 #define SET_PARITY_ODD BIT8 // (0x1 << 8)
113 #define SET_PARITY_EVEN BIT9 // (0x2 << 8)
114 #define SET_PARITY_MARK BIT9 | BIT8 // (0x3 << 8)
115 #define SET_PARITY_SPACE BIT10 // (0x4 << 8)
120 #define SET_STOP_BITS_1 0x0
121 #define SET_STOP_BITS_15 BIT11 // (0x1 << 11)
122 #define SET_STOP_BITS_2 BIT12 // (0x2 << 11)
126 // SET_DTR_HIGH = (1 | (1 << 8)), SET_DTR_LOW = (0 | (1 << 8)
127 // SET_RTS_HIGH = (2 | (2 << 8)), SET_RTS_LOW = (0 | (2 << 8)
129 #define SET_DTR_HIGH (BIT8 | BIT0)
130 #define SET_DTR_LOW (BIT8)
131 #define SET_RTS_HIGH (BIT9 | BIT1)
132 #define SET_RTS_LOW (BIT9)
137 #define CTS_MASK BIT4
138 #define DSR_MASK BIT5
141 #define MSR_MASK (CTS_MASK | DSR_MASK | RI_MASK | SD_MASK)
144 // Macro used to check for USB transfer errors
146 #define USB_IS_ERROR(Result, Error) (((Result) & (Error)) != 0)
149 // USB request timeouts
151 #define WDR_TIMEOUT 5000 // default urb timeout in ms
152 #define WDR_SHORT_TIMEOUT 1000 // shorter urb timeout in ms
157 #define FTDI_TIMEOUT 16
162 #define FTDI_MAX_RECEIVE_FIFO_DEPTH 384
165 // FTDI Endpoint Descriptors
167 #define FTDI_ENDPOINT_ADDRESS_IN 0x81 //the endpoint address for the in enpoint generated by the device
168 #define FTDI_ENDPOINT_ADDRESS_OUT 0x02 //the endpoint address for the out endpoint generated by the device
171 // Max buffer size for USB transfers
173 #define SW_FIFO_DEPTH 1024
176 // struct to define a usb device as a vendor and product id pair
184 //struct to describe the control bits of the device
185 //true indicates enabled
186 //false indicates disabled
189 BOOLEAN HardwareFlowControl
;
192 BOOLEAN HardwareLoopBack
;
193 BOOLEAN SoftwareLoopBack
;
197 //struct to describe the status bits of the device
198 //true indicates enabled
199 //false indicated disabled
209 // Structure to describe the last attributes of the Usb Serial device
213 UINT32 ReceiveFifoDepth
;
215 EFI_PARITY_TYPE Parity
;
217 EFI_STOP_BITS_TYPE StopBits
;
218 } PREVIOUS_ATTRIBUTES
;
221 // Structure to describe USB serial device
223 #define USB_SER_DEV_SIGNATURE SIGNATURE_32 ('u', 's', 'b', 's')
227 EFI_HANDLE ControllerHandle
;
228 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
229 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
230 UART_DEVICE_PATH UartDevicePath
;
231 UART_FLOW_CONTROL_DEVICE_PATH FlowControlDevicePath
;
232 EFI_USB_IO_PROTOCOL
*UsbIo
;
233 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
234 EFI_USB_ENDPOINT_DESCRIPTOR InEndpointDescriptor
;
235 EFI_USB_ENDPOINT_DESCRIPTOR OutEndpointDescriptor
;
236 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
237 UINT32 DataBufferHead
;
238 UINT32 DataBufferTail
;
240 EFI_SERIAL_IO_PROTOCOL SerialIo
;
242 EFI_EVENT PollingLoop
;
244 PREVIOUS_ATTRIBUTES LastSettings
;
245 CONTROL_BITS ControlValues
;
246 STATUS_BITS StatusValues
;
247 UINT8 ReadBuffer
[512];
250 #define USB_SER_DEV_FROM_THIS(a) \
251 CR(a, USB_SER_DEV, SerialIo, USB_SER_DEV_SIGNATURE)
256 extern EFI_DRIVER_BINDING_PROTOCOL gUsbSerialDriverBinding
;
257 extern EFI_COMPONENT_NAME_PROTOCOL gUsbSerialComponentName
;
258 extern EFI_COMPONENT_NAME2_PROTOCOL gUsbSerialComponentName2
;
261 // Functions of Driver Binding Protocol
264 Check whether USB Serial driver supports this device.
266 @param This[in] The USB Serial driver binding protocol.
267 @param Controller[in] The controller handle to check.
268 @param RemainingDevicePath[in] The remaining device path.
270 @retval EFI_SUCCESS The driver supports this controller.
271 @retval other This device isn't supported.
276 UsbSerialDriverBindingSupported (
277 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
278 IN EFI_HANDLE Controller
,
279 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
283 Starts the Serial device with this driver.
285 This function produces Serial IO Protocol and initializes the USB
286 Serial device to manage this USB Serial device.
288 @param This[in] The USB Serial driver binding instance.
289 @param Controller[in] Handle of device to bind driver to.
290 @param RemainingDevicePath[in] Optional parameter use to pick a specific
291 child device to start.
293 @retval EFI_SUCCESS The controller is controlled by the USB
295 @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
296 @retval Other This controller cannot be started.
301 UsbSerialDriverBindingStart (
302 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
303 IN EFI_HANDLE Controller
,
304 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
308 Stop the USB Serial device handled by this driver.
310 @param This[in] The USB Serial driver binding protocol.
311 @param Controller[in] The controller to release.
312 @param NumberOfChildren[in] The number of handles in ChildHandleBuffer.
313 @param ChildHandleBuffer[in] The array of child handle.
315 @retval EFI_SUCCESS The device was stopped.
316 @retval EFI_UNSUPPORTED Simple Text In Protocol or Simple Text In Ex
317 Protocol is not installed on Controller.
318 @retval EFI_DEVICE_ERROR The device could not be stopped due to a
320 @retval Others Fail to uninstall protocols attached on the
326 UsbSerialDriverBindingStop (
327 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
328 IN EFI_HANDLE Controller
,
329 IN UINTN NumberOfChildren
,
330 IN EFI_HANDLE
*ChildHandleBuffer
334 // Serial IO Member Functions
338 Writes data to a serial device.
340 @param This[in] Protocol instance pointer.
341 @param BufferSize[in, out] On input, the size of the Buffer. On output,
342 the amount of data actually written.
343 @param Buffer[in] The buffer of data to write
345 @retval EFI_SUCCESS The data was written.
346 @retval EFI_DEVICE_ERROR The device reported an error.
347 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
353 IN EFI_SERIAL_IO_PROTOCOL
*This
,
354 IN OUT UINTN
*BufferSize
,
359 Reads data from a serial device.
361 @param This[in] Protocol instance pointer.
362 @param BufferSize[in, out] On input, the size of the Buffer. On output,
363 the amount of data returned in Buffer.
364 @param Buffer[out] The buffer to return the data into.
366 @retval EFI_SUCCESS The data was read.
367 @retval EFI_DEVICE_ERROR The device reported an error.
368 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
374 IN EFI_SERIAL_IO_PROTOCOL
*This
,
375 IN OUT UINTN
*BufferSize
,
380 Retrieves the status of the control bits on a serial device.
382 @param This[in] Protocol instance pointer.
383 @param Control[out] A pointer to return the current Control signals
384 from the serial device.
386 @retval EFI_SUCCESS The control bits were read from the serial
388 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
394 IN EFI_SERIAL_IO_PROTOCOL
*This
,
399 Set the control bits on a serial device.
401 @param This[in] Protocol instance pointer.
402 @param Control[in] Set the bits of Control that are settable.
404 @retval EFI_SUCCESS The new control bits were set on the serial device.
405 @retval EFI_UNSUPPORTED The serial device does not support this operation.
406 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
412 IN EFI_SERIAL_IO_PROTOCOL
*This
,
417 Calls SetAttributesInternal() to set the baud rate, receive FIFO depth,
418 transmit/receice time out, parity, data buts, and stop bits on a serial device.
420 @param This[in] Protocol instance pointer.
421 @param BaudRate[in] The requested baud rate. A BaudRate value of 0
422 will use the device's default interface speed.
423 @param ReveiveFifoDepth[in] The requested depth of the FIFO on the receive
424 side of the serial interface. A ReceiveFifoDepth
425 value of 0 will use the device's default FIFO
427 @param Timeout[in] The requested time out for a single character in
428 microseconds.This timeout applies to both the
429 transmit and receive side of the interface.A
430 Timeout value of 0 will use the device's default
432 @param Parity[in] The type of parity to use on this serial device.A
433 Parity value of DefaultParity will use the
434 device's default parity value.
435 @param DataBits[in] The number of data bits to use on the serial
436 device. A DataBits value of 0 will use the
437 device's default data bit setting.
438 @param StopBits[in] The number of stop bits to use on this serial
439 device. A StopBits value of DefaultStopBits will
440 use the device's default number of stop bits.
442 @retval EFI_SUCCESS The attributes were set
443 @retval EFI_DEVICE_ERROR The attributes were not able to be
449 IN EFI_SERIAL_IO_PROTOCOL
*This
,
451 IN UINT32 ReceiveFifoDepth
,
453 IN EFI_PARITY_TYPE Parity
,
455 IN EFI_STOP_BITS_TYPE StopBits
459 Reset the serial device.
461 @param This Protocol instance pointer.
463 @retval EFI_SUCCESS The device was reset.
464 @retval EFI_DEVICE_ERROR The serial device could not be reset.
470 IN EFI_SERIAL_IO_PROTOCOL
*This
474 // EFI Component Name Functions
478 Retrieves a Unicode string that is the user readable name of the driver.
480 This function retrieves the user readable name of a driver in the form of a
481 Unicode string. If the driver specified by This has a user readable name in
482 the language specified by Language, then a pointer to the driver name is
483 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
484 by This does not support the language specified by Language,
485 then EFI_UNSUPPORTED is returned.
487 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL
488 or EFI_COMPONENT_NAME_PROTOCOL instance.
489 @param Language[in] A pointer to a Null-terminated ASCII string
490 array indicating the language. This is the
491 language of the driver name that the caller
492 is requesting, and it must match one of the
493 languages specified in SupportedLanguages.
494 The number of languages supported by a
495 driver is up to the driver writer. Language
496 is specified in RFC 4646 or ISO 639-2
497 language code format.
498 @param DriverName[out] A pointer to the Unicode string to return.
499 This Unicode string is the name of the
500 driver specified by This in the language
501 specified by Language.
503 @retval EFI_SUCCESS The Unicode string for the Driver specified
504 by This and the language specified by
505 Language was returned in DriverName.
506 @retval EFI_INVALID_PARAMETER Language is NULL.
507 @retval EFI_INVALID_PARAMETER DriverName is NULL.
508 @retval EFI_UNSUPPORTED The driver specified by This does not
509 support the language specified by Language.
514 UsbSerialComponentNameGetDriverName (
515 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
517 OUT CHAR16
**DriverName
521 Retrieves a Unicode string that is the user readable name of the controller
522 that is being managed by a driver.
524 This function retrieves the user readable name of the controller specified by
525 ControllerHandle and ChildHandle in the form of a Unicode string. If the
526 driver specified by This has a user readable name in the language specified by
527 Language, then a pointer to the controller name is returned in ControllerName,
528 and EFI_SUCCESS is returned. If the driver specified by This is not currently
529 managing the controller specified by ControllerHandle and ChildHandle,
530 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
531 support the language specified by Language, then EFI_UNSUPPORTED is returned.
533 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL
534 or EFI_COMPONENT_NAME_PROTOCOL instance.
535 @param ControllerHandle[in] The handle of a controller that the driver
536 specified by This is managing. This handle
537 specifies the controller whose name is to
539 @param ChildHandle[in] The handle of the child controller to
540 retrieve the name of. This is an optional
541 parameter that may be NULL. It will be NULL
542 for device drivers. It will also be NULL
543 for a bus drivers that wish to retrieve the
544 name of the bus controller. It will not be
545 NULL for a bus driver that wishes to
546 retrieve the name of a child controller.
547 @param Language[in] A pointer to a Null-terminated ASCII string
548 array indicating the language. This is the
549 language of the driver name that the caller
550 is requesting, and it must match one of the
551 languages specified in SupportedLanguages.
552 The number of languages supported by a
553 driver is up to the driver writer. Language
554 is specified in RFC 4646 or ISO 639-2
555 language code format.
556 @param ControllerName[out] A pointer to the Unicode string to return.
557 This Unicode string is the name of the
558 controller specified by ControllerHandle
559 and ChildHandle in the language specified
560 by Language from the point of view of the
561 driver specified by This.
563 @retval EFI_SUCCESS The Unicode string for the user readable
564 name in the language specified by Language
565 for the driver specified by This was
566 returned in DriverName.
567 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
568 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a
570 @retval EFI_INVALID_PARAMETER Language is NULL.
571 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
572 @retval EFI_UNSUPPORTED The driver specified by This is not
573 currently managing the controller specified
574 by ControllerHandle and ChildHandle.
575 @retval EFI_UNSUPPORTED The driver specified by This does not
576 support the language specified by Language.
581 UsbSerialComponentNameGetControllerName (
582 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
583 IN EFI_HANDLE ControllerHandle
,
584 IN EFI_HANDLE ChildHandle OPTIONAL
,
586 OUT CHAR16
**ControllerName