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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD
8 License which accompanies this distribution.  The full text of the license may
9 be found at http://opensource.org/licenses/bsd-license.php
10 
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef _FTDI_USB_SERIAL_DRIVER_H_
17 #define _FTDI_USB_SERIAL_DRIVER_H_
18 
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiLib.h>
24 #include <Library/DevicePathLib.h>
25 
26 #include <Protocol/DevicePath.h>
27 #include <Protocol/UsbIo.h>
28 #include <Protocol/SerialIo.h>
29 
30 //
31 // US English LangID
32 //
33 #define USB_US_LANG_ID  0x0409
34 
35 //
36 // Supported Vendor Ids
37 //
38 #define VID_FTDI    0x0403
39 
40 //
41 // Supported product ids
42 //
43 #define DID_FTDI_FT232    0x6001
44 
45 //
46 // FTDI Commands
47 //
48 #define FTDI_COMMAND_RESET_PORT          0
49 #define FTDI_COMMAND_MODEM_CTRL          1
50 #define FTDI_COMMAND_SET_FLOW_CTRL       2
51 #define FTDI_COMMAND_SET_BAUDRATE        3
52 #define FTDI_COMMAND_SET_DATA            4
53 #define FTDI_COMMAND_GET_MODEM_STATUS    5
54 #define FTDI_COMMAND_SET_EVENT_CHAR      6
55 #define FTDI_COMMAND_SET_ERROR_CHAR      7
56 #define FTDI_COMMAND_SET_LATENCY_TIMER   9
57 #define FTDI_COMMAND_GET_LATENCY_TIMER   10
58 
59 //
60 // FTDI_PORT_IDENTIFIER
61 // Used in the usb control transfers that issue FTDI commands as the index value.
62 //
63 #define FTDI_PORT_IDENTIFIER    0x1 // For FTDI USB serial adapter the port
64                                     // identifier is always 1.
65 
66 //
67 // RESET_PORT
68 //
69 #define RESET_PORT_RESET        0x0 // Purges RX and TX, clears DTR and RTS sets
70                                     // flow control to none, disables event
71                                     // trigger, sets the event char to 0x0d and
72                                     // does nothing to baudrate or data settings
73 #define RESET_PORT_PURGE_RX     0x1
74 #define RESET_PORT_PURGE_TX     0x2
75 
76 //
77 // SET_FLOW_CONTROL
78 //
79 #define NO_FLOW_CTRL                     0x0
80 #define XON_XOFF_CTRL                    0x4
81 
82 //
83 // SET_BAUD_RATE
84 // To set baud rate, one must calculate an encoding of the baud rate from
85 // UINT32 to UINT16.See EncodeBaudRateForFtdi() for details
86 //
87 #define FTDI_UART_FREQUENCY              3000000
88 #define FTDI_MIN_DIVISOR                 0x20
89 #define FTDI_MAX_DIVISOR                 0x3FFF8
90 //
91 // Special case baudrate values
92 // 300,000 and 200,000 are special cases for calculating the encoded baudrate
93 //
94 #define FTDI_SPECIAL_CASE_300_MIN        (3000000 * 100) / 103 // minimum adjusted
95                                                                // value for 300,000
96 #define FTDI_SPECIAL_CASE_300_MAX        (3000000 * 100) / 97  // maximum adjusted
97                                                                // value for 300,000
98 #define FTDI_SPECIAL_CASE_200_MIN        (2000000 * 100) / 103 // minimum adjusted
99                                                                // value for 200,000
100 #define FTDI_SPECIAL_CASE_200_MAX        (2000000 * 100) / 97  // maximum adjusted
101                                                                // value for 200,000
102 //
103 // Min and max frequency values that the FTDI chip can attain
104 //.all generated frequencies must be between these values
105 //
106 #define FTDI_MIN_FREQUENCY              46601941 // (3MHz * 1600) / 103 = 46601941
107 #define FTDI_MAX_FREQUENCY              49484536 // (3MHz * 1600) / 97 = 49484536
108 
109 //
110 // SET_DATA_BITS
111 //
112 #define SET_DATA_BITS(n)                 (n)
113 
114 //
115 // SET_PARITY
116 //
117 #define SET_PARITY_NONE                   0x0
118 #define SET_PARITY_ODD                    BIT8 // (0x1 << 8)
119 #define SET_PARITY_EVEN                   BIT9 // (0x2 << 8)
120 #define SET_PARITY_MARK                   BIT9 | BIT8 // (0x3 << 8)
121 #define SET_PARITY_SPACE                  BIT10 // (0x4 << 8)
122 
123 //
124 // SET_STOP_BITS
125 //
126 #define SET_STOP_BITS_1                   0x0
127 #define SET_STOP_BITS_15                  BIT11 // (0x1 << 11)
128 #define SET_STOP_BITS_2                   BIT12 // (0x2 << 11)
129 
130 //
131 // SET_MODEM_CTRL
132 // SET_DTR_HIGH = (1 | (1 << 8)), SET_DTR_LOW = (0 | (1 << 8)
133 // SET_RTS_HIGH = (2 | (2 << 8)), SET_RTS_LOW = (0 | (2 << 8)
134 //
135 #define SET_DTR_HIGH                     (BIT8 | BIT0)
136 #define SET_DTR_LOW                      (BIT8)
137 #define SET_RTS_HIGH                     (BIT9 | BIT1)
138 #define SET_RTS_LOW                      (BIT9)
139 
140 //
141 // MODEM_STATUS
142 //
143 #define CTS_MASK                         BIT4
144 #define DSR_MASK                         BIT5
145 #define RI_MASK                          BIT6
146 #define SD_MASK                          BIT7
147 #define MSR_MASK                         (CTS_MASK | DSR_MASK | RI_MASK | SD_MASK)
148 
149 //
150 // Macro used to check for USB transfer errors
151 //
152 #define USB_IS_ERROR(Result, Error)           (((Result) & (Error)) != 0)
153 
154 //
155 // USB request timeouts
156 //
157 #define WDR_TIMEOUT        5000  // default urb timeout in ms
158 #define WDR_SHORT_TIMEOUT  1000  // shorter urb timeout in ms
159 
160 //
161 // FTDI timeout
162 //
163 #define FTDI_TIMEOUT       16
164 
165 //
166 // FTDI FIFO depth
167 //
168 #define FTDI_MAX_RECEIVE_FIFO_DEPTH  384
169 
170 //
171 // FTDI Endpoint Descriptors
172 //
173 #define FTDI_ENDPOINT_ADDRESS_IN   0x81 //the endpoint address for the in enpoint generated by the device
174 #define FTDI_ENDPOINT_ADDRESS_OUT  0x02 //the endpoint address for the out endpoint generated by the device
175 
176 //
177 // Max buffer size for USB transfers
178 //
179 #define SW_FIFO_DEPTH 1024
180 
181 //
182 // struct to define a usb device as a vendor and product id pair
183 //
184 typedef struct {
185   UINTN     VendorId;
186   UINTN     DeviceId;
187 } USB_DEVICE;
188 
189 //
190 //struct to describe the control bits of the device
191 //true indicates enabled
192 //false indicates disabled
193 //
194 typedef struct {
195   BOOLEAN    HardwareFlowControl;
196   BOOLEAN    DtrState;
197   BOOLEAN    RtsState;
198   BOOLEAN    HardwareLoopBack;
199   BOOLEAN    SoftwareLoopBack;
200 } CONTROL_BITS;
201 
202 //
203 //struct to describe the status bits of the device
204 //true indicates enabled
205 //false indicated disabled
206 //
207 typedef struct {
208   BOOLEAN    CtsState;
209   BOOLEAN    DsrState;
210   BOOLEAN    RiState;
211   BOOLEAN    SdState;
212 } STATUS_BITS;
213 
214 //
215 // Structure to describe the last attributes of the Usb Serial device
216 //
217 typedef struct {
218   UINT64              BaudRate;
219   UINT32              ReceiveFifoDepth;
220   UINT32              Timeout;
221   EFI_PARITY_TYPE     Parity;
222   UINT8               DataBits;
223   EFI_STOP_BITS_TYPE  StopBits;
224 } PREVIOUS_ATTRIBUTES;
225 
226 //
227 // Structure to describe USB serial device
228 //
229 #define USB_SER_DEV_SIGNATURE  SIGNATURE_32 ('u', 's', 'b', 's')
230 
231 typedef struct {
232   UINTN                         Signature;
233   EFI_HANDLE                    ControllerHandle;
234   EFI_DEVICE_PATH_PROTOCOL      *DevicePath;
235   EFI_DEVICE_PATH_PROTOCOL      *ParentDevicePath;
236   UART_DEVICE_PATH              UartDevicePath;
237   UART_FLOW_CONTROL_DEVICE_PATH FlowControlDevicePath;
238   EFI_USB_IO_PROTOCOL           *UsbIo;
239   EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;
240   EFI_USB_ENDPOINT_DESCRIPTOR   InEndpointDescriptor;
241   EFI_USB_ENDPOINT_DESCRIPTOR   OutEndpointDescriptor;
242   EFI_UNICODE_STRING_TABLE      *ControllerNameTable;
243   UINT32                        DataBufferHead;
244   UINT32                        DataBufferTail;
245   UINT8                         *DataBuffer;
246   EFI_SERIAL_IO_PROTOCOL        SerialIo;
247   BOOLEAN                       Shutdown;
248   EFI_EVENT                     PollingLoop;
249   UINT32                        ControlBits;
250   PREVIOUS_ATTRIBUTES           LastSettings;
251   CONTROL_BITS                  ControlValues;
252   STATUS_BITS                   StatusValues;
253   UINT8                         ReadBuffer[512];
254 } USB_SER_DEV;
255 
256 #define USB_SER_DEV_FROM_THIS(a) \
257   CR(a, USB_SER_DEV, SerialIo, USB_SER_DEV_SIGNATURE)
258 
259 //
260 // Global Variables
261 //
262 extern EFI_DRIVER_BINDING_PROTOCOL   gUsbSerialDriverBinding;
263 extern EFI_COMPONENT_NAME_PROTOCOL   gUsbSerialComponentName;
264 extern EFI_COMPONENT_NAME2_PROTOCOL  gUsbSerialComponentName2;
265 
266 //
267 // Functions of Driver Binding Protocol
268 //
269 /**
270   Check whether USB Serial driver supports this device.
271 
272   @param  This[in]                   The USB Serial driver binding protocol.
273   @param  Controller[in]             The controller handle to check.
274   @param  RemainingDevicePath[in]    The remaining device path.
275 
276   @retval EFI_SUCCESS                The driver supports this controller.
277   @retval other                      This device isn't supported.
278 
279 **/
280 EFI_STATUS
281 EFIAPI
282 UsbSerialDriverBindingSupported (
283   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
284   IN EFI_HANDLE                   Controller,
285   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
286   );
287 
288 /**
289   Starts the Serial device with this driver.
290 
291   This function produces Serial IO Protocol and initializes the USB
292   Serial device to manage this USB Serial device.
293 
294   @param  This[in]                   The USB Serial driver binding instance.
295   @param  Controller[in]             Handle of device to bind driver to.
296   @param  RemainingDevicePath[in]    Optional parameter use to pick a specific
297                                      child device to start.
298 
299   @retval EFI_SUCCESS                The controller is controlled by the USB
300                                      Serial driver.
301   @retval EFI_UNSUPPORTED            No interrupt endpoint can be found.
302   @retval Other                      This controller cannot be started.
303 
304 **/
305 EFI_STATUS
306 EFIAPI
307 UsbSerialDriverBindingStart (
308   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
309   IN EFI_HANDLE                   Controller,
310   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
311   );
312 
313 /**
314   Stop the USB Serial device handled by this driver.
315 
316   @param  This[in]                   The USB Serial driver binding protocol.
317   @param  Controller[in]             The controller to release.
318   @param  NumberOfChildren[in]       The number of handles in ChildHandleBuffer.
319   @param  ChildHandleBuffer[in]      The array of child handle.
320 
321   @retval EFI_SUCCESS                The device was stopped.
322   @retval EFI_UNSUPPORTED            Simple Text In Protocol or Simple Text In Ex
323                                      Protocol is not installed on Controller.
324   @retval EFI_DEVICE_ERROR           The device could not be stopped due to a
325                                      device error.
326   @retval Others                     Fail to uninstall protocols attached on the
327                                      device.
328 
329 **/
330 EFI_STATUS
331 EFIAPI
332 UsbSerialDriverBindingStop (
333   IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
334   IN  EFI_HANDLE                   Controller,
335   IN  UINTN                        NumberOfChildren,
336   IN  EFI_HANDLE                   *ChildHandleBuffer
337   );
338 
339 //
340 // Serial IO Member Functions
341 //
342 
343 /**
344   Writes data to a serial device.
345 
346   @param  This[in]                   Protocol instance pointer.
347   @param  BufferSize[in, out]        On input, the size of the Buffer. On output,
348                                      the amount of data actually written.
349   @param  Buffer[in]                 The buffer of data to write
350 
351   @retval EFI_SUCCESS                The data was written.
352   @retval EFI_DEVICE_ERROR           The device reported an error.
353   @retval EFI_TIMEOUT                The data write was stopped due to a timeout.
354 
355 **/
356 EFI_STATUS
357 EFIAPI
358 WriteSerialIo (
359   IN EFI_SERIAL_IO_PROTOCOL  *This,
360   IN OUT UINTN               *BufferSize,
361   IN VOID                    *Buffer
362   );
363 
364 /**
365   Reads data from a serial device.
366 
367   @param  This[in]                   Protocol instance pointer.
368   @param  BufferSize[in, out]        On input, the size of the Buffer. On output,
369                                      the amount of data returned in Buffer.
370   @param  Buffer[out]                The buffer to return the data into.
371 
372   @retval EFI_SUCCESS                The data was read.
373   @retval EFI_DEVICE_ERROR           The device reported an error.
374   @retval EFI_TIMEOUT                The data write was stopped due to a timeout.
375 
376 **/
377 EFI_STATUS
378 EFIAPI
379 ReadSerialIo (
380   IN EFI_SERIAL_IO_PROTOCOL  *This,
381   IN OUT UINTN               *BufferSize,
382   OUT VOID                   *Buffer
383   );
384 
385 /**
386   Retrieves the status of the control bits on a serial device.
387 
388   @param  This[in]               Protocol instance pointer.
389   @param  Control[out]           A pointer to return the current Control signals
390                                  from the serial device.
391 
392   @retval EFI_SUCCESS            The control bits were read from the serial
393                                  device.
394   @retval EFI_DEVICE_ERROR       The serial device is not functioning correctly.
395 
396 **/
397 EFI_STATUS
398 EFIAPI
399 GetControlBits (
400   IN EFI_SERIAL_IO_PROTOCOL  *This,
401   OUT UINT32                 *Control
402   );
403 
404 /**
405   Set the control bits on a serial device.
406 
407   @param  This[in]             Protocol instance pointer.
408   @param  Control[in]          Set the bits of Control that are settable.
409 
410   @retval EFI_SUCCESS          The new control bits were set on the serial device.
411   @retval EFI_UNSUPPORTED      The serial device does not support this operation.
412   @retval EFI_DEVICE_ERROR     The serial device is not functioning correctly.
413 
414 **/
415 EFI_STATUS
416 EFIAPI
417 SetControlBits (
418   IN EFI_SERIAL_IO_PROTOCOL  *This,
419   IN UINT32                  Control
420   );
421 
422 /**
423   Calls SetAttributesInternal() to set the baud rate, receive FIFO depth,
424   transmit/receice time out, parity, data buts, and stop bits on a serial device.
425 
426   @param  This[in]             Protocol instance pointer.
427   @param  BaudRate[in]         The requested baud rate. A BaudRate value of 0
428                                will use the device's default interface speed.
429   @param  ReveiveFifoDepth[in] The requested depth of the FIFO on the receive
430                                side of the serial interface. A ReceiveFifoDepth
431                                value of 0 will use the device's default FIFO
432                                depth.
433   @param  Timeout[in]          The requested time out for a single character in
434                                microseconds.This timeout applies to both the
435                                transmit and receive side of the interface.A
436                                Timeout value of 0 will use the device's default
437                                time out value.
438   @param  Parity[in]           The type of parity to use on this serial device.A
439                                Parity value of DefaultParity will use the
440                                device's default parity value.
441   @param  DataBits[in]         The number of data bits to use on the serial
442                                device. A DataBits value of 0 will use the
443                                device's default data bit setting.
444   @param  StopBits[in]         The number of stop bits to use on this serial
445                                device. A StopBits value of DefaultStopBits will
446                                use the device's default number of stop bits.
447 
448   @retval EFI_SUCCESS          The attributes were set
449   @retval EFI_DEVICE_ERROR     The attributes were not able to be
450 
451 **/
452 EFI_STATUS
453 EFIAPI
454 SetAttributes (
455   IN EFI_SERIAL_IO_PROTOCOL  *This,
456   IN UINT64                  BaudRate,
457   IN UINT32                  ReceiveFifoDepth,
458   IN UINT32                  Timeout,
459   IN EFI_PARITY_TYPE         Parity,
460   IN UINT8                   DataBits,
461   IN EFI_STOP_BITS_TYPE      StopBits
462   );
463 
464 /**
465   Reset the serial device.
466 
467   @param  This              Protocol instance pointer.
468 
469   @retval EFI_SUCCESS       The device was reset.
470   @retval EFI_DEVICE_ERROR  The serial device could not be reset.
471 
472 **/
473 EFI_STATUS
474 EFIAPI
475 SerialReset (
476   IN EFI_SERIAL_IO_PROTOCOL  *This
477   );
478 
479 //
480 // EFI Component Name Functions
481 //
482 
483 /**
484   Retrieves a Unicode string that is the user readable name of the driver.
485 
486   This function retrieves the user readable name of a driver in the form of a
487   Unicode string. If the driver specified by This has a user readable name in
488   the language specified by Language, then a pointer to the driver name is
489   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
490   by This does not support the language specified by Language,
491   then EFI_UNSUPPORTED is returned.
492 
493   @param  This[in]                   A pointer to the EFI_COMPONENT_NAME2_PROTOCOL
494                                      or EFI_COMPONENT_NAME_PROTOCOL instance.
495   @param  Language[in]               A pointer to a Null-terminated ASCII string
496                                      array indicating the language. This is the
497                                      language of the driver name that the caller
498                                      is requesting, and it must match one of the
499                                      languages specified in SupportedLanguages.
500                                      The number of languages supported by a
501                                      driver is up to the driver writer. Language
502                                      is specified in RFC 4646 or ISO 639-2
503                                      language code format.
504   @param  DriverName[out]            A pointer to the Unicode string to return.
505                                      This Unicode string is the name of the
506                                      driver specified by This in the language
507                                      specified by Language.
508 
509   @retval EFI_SUCCESS                The Unicode string for the Driver specified
510                                      by This and the language specified by
511                                      Language was returned in DriverName.
512   @retval EFI_INVALID_PARAMETER      Language is NULL.
513   @retval EFI_INVALID_PARAMETER      DriverName is NULL.
514   @retval EFI_UNSUPPORTED            The driver specified by This does not
515                                      support the language specified by Language.
516 
517 **/
518 EFI_STATUS
519 EFIAPI
520 UsbSerialComponentNameGetDriverName (
521   IN  EFI_COMPONENT_NAME2_PROTOCOL  *This,
522   IN  CHAR8                         *Language,
523   OUT CHAR16                        **DriverName
524   );
525 
526 /**
527   Retrieves a Unicode string that is the user readable name of the controller
528   that is being managed by a driver.
529 
530   This function retrieves the user readable name of the controller specified by
531   ControllerHandle and ChildHandle in the form of a Unicode string. If the
532   driver specified by This has a user readable name in the language specified by
533   Language, then a pointer to the controller name is returned in ControllerName,
534   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
535   managing the controller specified by ControllerHandle and ChildHandle,
536   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
537   support the language specified by Language, then EFI_UNSUPPORTED is returned.
538 
539   @param  This[in]                   A pointer to the EFI_COMPONENT_NAME2_PROTOCOL
540                                      or EFI_COMPONENT_NAME_PROTOCOL instance.
541   @param  ControllerHandle[in]       The handle of a controller that the driver
542                                      specified by This is managing.  This handle
543                                      specifies the controller whose name is to
544                                      be returned.
545   @param  ChildHandle[in]            The handle of the child controller to
546                                      retrieve the name of. This is an optional
547                                      parameter that may be NULL. It will be NULL
548                                      for device drivers. It will also be NULL
549                                      for a bus drivers that wish to retrieve the
550                                      name of the bus controller. It will not be
551                                      NULL for a bus driver that wishes to
552                                      retrieve the name of a child controller.
553   @param  Language[in]               A pointer to a Null-terminated ASCII string
554                                      array indicating the language.  This is the
555                                      language of the driver name that the caller
556                                      is requesting, and it must match one of the
557                                      languages specified in SupportedLanguages.
558                                      The number of languages supported by a
559                                      driver is up to the driver writer. Language
560                                      is specified in RFC 4646 or ISO 639-2
561                                      language code format.
562   @param  ControllerName[out]        A pointer to the Unicode string to return.
563                                      This Unicode string is the name of the
564                                      controller specified by ControllerHandle
565                                      and ChildHandle in the language specified
566                                      by Language from the point of view of the
567                                      driver specified by This.
568 
569   @retval EFI_SUCCESS                The Unicode string for the user readable
570                                      name in the language specified by Language
571                                      for the driver specified by This was
572                                      returned in DriverName.
573   @retval EFI_INVALID_PARAMETER      ControllerHandle is not a valid EFI_HANDLE.
574   @retval EFI_INVALID_PARAMETER      ChildHandle is not NULL and it is not a
575                                      valid EFI_HANDLE.
576   @retval EFI_INVALID_PARAMETER      Language is NULL.
577   @retval EFI_INVALID_PARAMETER      ControllerName is NULL.
578   @retval EFI_UNSUPPORTED            The driver specified by This is not
579                                      currently managing the controller specified
580                                      by ControllerHandle and ChildHandle.
581   @retval EFI_UNSUPPORTED            The driver specified by This does not
582                                      support the language specified by Language.
583 
584 **/
585 EFI_STATUS
586 EFIAPI
587 UsbSerialComponentNameGetControllerName (
588   IN  EFI_COMPONENT_NAME2_PROTOCOL  *This,
589   IN  EFI_HANDLE                    ControllerHandle,
590   IN  EFI_HANDLE                    ChildHandle      OPTIONAL,
591   IN  CHAR8                         *Language,
592   OUT CHAR16                        **ControllerName
593   );
594 
595 #endif
596