]> git.proxmox.com Git - mirror_edk2.git/blob - OptionRomPkg/Bus/Usb/FtdiUsbSerialDxe/FtdiUsbSerialDriver.h
OptionRomPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / OptionRomPkg / Bus / Usb / FtdiUsbSerialDxe / FtdiUsbSerialDriver.h
1 /** @file
2 Header file for USB Serial Driver's Data Structures.
3
4 Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.
5 Portions Copyright 2012 Ashley DeSimone
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #ifndef _FTDI_USB_SERIAL_DRIVER_H_
11 #define _FTDI_USB_SERIAL_DRIVER_H_
12
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>
19
20 #include <Protocol/DevicePath.h>
21 #include <Protocol/UsbIo.h>
22 #include <Protocol/SerialIo.h>
23
24 //
25 // US English LangID
26 //
27 #define USB_US_LANG_ID 0x0409
28
29 //
30 // Supported Vendor Ids
31 //
32 #define VID_FTDI 0x0403
33
34 //
35 // Supported product ids
36 //
37 #define DID_FTDI_FT232 0x6001
38
39 //
40 // FTDI Commands
41 //
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
52
53 //
54 // FTDI_PORT_IDENTIFIER
55 // Used in the usb control transfers that issue FTDI commands as the index value.
56 //
57 #define FTDI_PORT_IDENTIFIER 0x1 // For FTDI USB serial adapter the port
58 // identifier is always 1.
59
60 //
61 // RESET_PORT
62 //
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
69
70 //
71 // SET_FLOW_CONTROL
72 //
73 #define NO_FLOW_CTRL 0x0
74 #define XON_XOFF_CTRL 0x4
75
76 //
77 // SET_BAUD_RATE
78 // To set baud rate, one must calculate an encoding of the baud rate from
79 // UINT32 to UINT16.See EncodeBaudRateForFtdi() for details
80 //
81 #define FTDI_UART_FREQUENCY 3000000
82 #define FTDI_MIN_DIVISOR 0x20
83 #define FTDI_MAX_DIVISOR 0x3FFF8
84 //
85 // Special case baudrate values
86 // 300,000 and 200,000 are special cases for calculating the encoded baudrate
87 //
88 #define FTDI_SPECIAL_CASE_300_MIN (3000000 * 100) / 103 // minimum adjusted
89 // value for 300,000
90 #define FTDI_SPECIAL_CASE_300_MAX (3000000 * 100) / 97 // maximum adjusted
91 // value for 300,000
92 #define FTDI_SPECIAL_CASE_200_MIN (2000000 * 100) / 103 // minimum adjusted
93 // value for 200,000
94 #define FTDI_SPECIAL_CASE_200_MAX (2000000 * 100) / 97 // maximum adjusted
95 // value for 200,000
96 //
97 // Min and max frequency values that the FTDI chip can attain
98 //.all generated frequencies must be between these values
99 //
100 #define FTDI_MIN_FREQUENCY 46601941 // (3MHz * 1600) / 103 = 46601941
101 #define FTDI_MAX_FREQUENCY 49484536 // (3MHz * 1600) / 97 = 49484536
102
103 //
104 // SET_DATA_BITS
105 //
106 #define SET_DATA_BITS(n) (n)
107
108 //
109 // SET_PARITY
110 //
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)
116
117 //
118 // SET_STOP_BITS
119 //
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)
123
124 //
125 // SET_MODEM_CTRL
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)
128 //
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)
133
134 //
135 // MODEM_STATUS
136 //
137 #define CTS_MASK BIT4
138 #define DSR_MASK BIT5
139 #define RI_MASK BIT6
140 #define SD_MASK BIT7
141 #define MSR_MASK (CTS_MASK | DSR_MASK | RI_MASK | SD_MASK)
142
143 //
144 // Macro used to check for USB transfer errors
145 //
146 #define USB_IS_ERROR(Result, Error) (((Result) & (Error)) != 0)
147
148 //
149 // USB request timeouts
150 //
151 #define WDR_TIMEOUT 5000 // default urb timeout in ms
152 #define WDR_SHORT_TIMEOUT 1000 // shorter urb timeout in ms
153
154 //
155 // FTDI timeout
156 //
157 #define FTDI_TIMEOUT 16
158
159 //
160 // FTDI FIFO depth
161 //
162 #define FTDI_MAX_RECEIVE_FIFO_DEPTH 384
163
164 //
165 // FTDI Endpoint Descriptors
166 //
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
169
170 //
171 // Max buffer size for USB transfers
172 //
173 #define SW_FIFO_DEPTH 1024
174
175 //
176 // struct to define a usb device as a vendor and product id pair
177 //
178 typedef struct {
179 UINTN VendorId;
180 UINTN DeviceId;
181 } USB_DEVICE;
182
183 //
184 //struct to describe the control bits of the device
185 //true indicates enabled
186 //false indicates disabled
187 //
188 typedef struct {
189 BOOLEAN HardwareFlowControl;
190 BOOLEAN DtrState;
191 BOOLEAN RtsState;
192 BOOLEAN HardwareLoopBack;
193 BOOLEAN SoftwareLoopBack;
194 } CONTROL_BITS;
195
196 //
197 //struct to describe the status bits of the device
198 //true indicates enabled
199 //false indicated disabled
200 //
201 typedef struct {
202 BOOLEAN CtsState;
203 BOOLEAN DsrState;
204 BOOLEAN RiState;
205 BOOLEAN SdState;
206 } STATUS_BITS;
207
208 //
209 // Structure to describe the last attributes of the Usb Serial device
210 //
211 typedef struct {
212 UINT64 BaudRate;
213 UINT32 ReceiveFifoDepth;
214 UINT32 Timeout;
215 EFI_PARITY_TYPE Parity;
216 UINT8 DataBits;
217 EFI_STOP_BITS_TYPE StopBits;
218 } PREVIOUS_ATTRIBUTES;
219
220 //
221 // Structure to describe USB serial device
222 //
223 #define USB_SER_DEV_SIGNATURE SIGNATURE_32 ('u', 's', 'b', 's')
224
225 typedef struct {
226 UINTN Signature;
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;
239 UINT8 *DataBuffer;
240 EFI_SERIAL_IO_PROTOCOL SerialIo;
241 BOOLEAN Shutdown;
242 EFI_EVENT PollingLoop;
243 UINT32 ControlBits;
244 PREVIOUS_ATTRIBUTES LastSettings;
245 CONTROL_BITS ControlValues;
246 STATUS_BITS StatusValues;
247 UINT8 ReadBuffer[512];
248 } USB_SER_DEV;
249
250 #define USB_SER_DEV_FROM_THIS(a) \
251 CR(a, USB_SER_DEV, SerialIo, USB_SER_DEV_SIGNATURE)
252
253 //
254 // Global Variables
255 //
256 extern EFI_DRIVER_BINDING_PROTOCOL gUsbSerialDriverBinding;
257 extern EFI_COMPONENT_NAME_PROTOCOL gUsbSerialComponentName;
258 extern EFI_COMPONENT_NAME2_PROTOCOL gUsbSerialComponentName2;
259
260 //
261 // Functions of Driver Binding Protocol
262 //
263 /**
264 Check whether USB Serial driver supports this device.
265
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.
269
270 @retval EFI_SUCCESS The driver supports this controller.
271 @retval other This device isn't supported.
272
273 **/
274 EFI_STATUS
275 EFIAPI
276 UsbSerialDriverBindingSupported (
277 IN EFI_DRIVER_BINDING_PROTOCOL *This,
278 IN EFI_HANDLE Controller,
279 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
280 );
281
282 /**
283 Starts the Serial device with this driver.
284
285 This function produces Serial IO Protocol and initializes the USB
286 Serial device to manage this USB Serial device.
287
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.
292
293 @retval EFI_SUCCESS The controller is controlled by the USB
294 Serial driver.
295 @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
296 @retval Other This controller cannot be started.
297
298 **/
299 EFI_STATUS
300 EFIAPI
301 UsbSerialDriverBindingStart (
302 IN EFI_DRIVER_BINDING_PROTOCOL *This,
303 IN EFI_HANDLE Controller,
304 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
305 );
306
307 /**
308 Stop the USB Serial device handled by this driver.
309
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.
314
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
319 device error.
320 @retval Others Fail to uninstall protocols attached on the
321 device.
322
323 **/
324 EFI_STATUS
325 EFIAPI
326 UsbSerialDriverBindingStop (
327 IN EFI_DRIVER_BINDING_PROTOCOL *This,
328 IN EFI_HANDLE Controller,
329 IN UINTN NumberOfChildren,
330 IN EFI_HANDLE *ChildHandleBuffer
331 );
332
333 //
334 // Serial IO Member Functions
335 //
336
337 /**
338 Writes data to a serial device.
339
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
344
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.
348
349 **/
350 EFI_STATUS
351 EFIAPI
352 WriteSerialIo (
353 IN EFI_SERIAL_IO_PROTOCOL *This,
354 IN OUT UINTN *BufferSize,
355 IN VOID *Buffer
356 );
357
358 /**
359 Reads data from a serial device.
360
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.
365
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.
369
370 **/
371 EFI_STATUS
372 EFIAPI
373 ReadSerialIo (
374 IN EFI_SERIAL_IO_PROTOCOL *This,
375 IN OUT UINTN *BufferSize,
376 OUT VOID *Buffer
377 );
378
379 /**
380 Retrieves the status of the control bits on a serial device.
381
382 @param This[in] Protocol instance pointer.
383 @param Control[out] A pointer to return the current Control signals
384 from the serial device.
385
386 @retval EFI_SUCCESS The control bits were read from the serial
387 device.
388 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
389
390 **/
391 EFI_STATUS
392 EFIAPI
393 GetControlBits (
394 IN EFI_SERIAL_IO_PROTOCOL *This,
395 OUT UINT32 *Control
396 );
397
398 /**
399 Set the control bits on a serial device.
400
401 @param This[in] Protocol instance pointer.
402 @param Control[in] Set the bits of Control that are settable.
403
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.
407
408 **/
409 EFI_STATUS
410 EFIAPI
411 SetControlBits (
412 IN EFI_SERIAL_IO_PROTOCOL *This,
413 IN UINT32 Control
414 );
415
416 /**
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.
419
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
426 depth.
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
431 time out value.
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.
441
442 @retval EFI_SUCCESS The attributes were set
443 @retval EFI_DEVICE_ERROR The attributes were not able to be
444
445 **/
446 EFI_STATUS
447 EFIAPI
448 SetAttributes (
449 IN EFI_SERIAL_IO_PROTOCOL *This,
450 IN UINT64 BaudRate,
451 IN UINT32 ReceiveFifoDepth,
452 IN UINT32 Timeout,
453 IN EFI_PARITY_TYPE Parity,
454 IN UINT8 DataBits,
455 IN EFI_STOP_BITS_TYPE StopBits
456 );
457
458 /**
459 Reset the serial device.
460
461 @param This Protocol instance pointer.
462
463 @retval EFI_SUCCESS The device was reset.
464 @retval EFI_DEVICE_ERROR The serial device could not be reset.
465
466 **/
467 EFI_STATUS
468 EFIAPI
469 SerialReset (
470 IN EFI_SERIAL_IO_PROTOCOL *This
471 );
472
473 //
474 // EFI Component Name Functions
475 //
476
477 /**
478 Retrieves a Unicode string that is the user readable name of the driver.
479
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.
486
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.
502
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.
510
511 **/
512 EFI_STATUS
513 EFIAPI
514 UsbSerialComponentNameGetDriverName (
515 IN EFI_COMPONENT_NAME2_PROTOCOL *This,
516 IN CHAR8 *Language,
517 OUT CHAR16 **DriverName
518 );
519
520 /**
521 Retrieves a Unicode string that is the user readable name of the controller
522 that is being managed by a driver.
523
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.
532
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
538 be returned.
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.
562
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
569 valid EFI_HANDLE.
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.
577
578 **/
579 EFI_STATUS
580 EFIAPI
581 UsbSerialComponentNameGetControllerName (
582 IN EFI_COMPONENT_NAME2_PROTOCOL *This,
583 IN EFI_HANDLE ControllerHandle,
584 IN EFI_HANDLE ChildHandle OPTIONAL,
585 IN CHAR8 *Language,
586 OUT CHAR16 **ControllerName
587 );
588
589 #endif