2 Implementation of the USB mass storage Control/Bulk/Interrupt transport,
3 according to USB Mass Storage Class Control/Bulk/Interrupt (CBI) Transport, Revision 1.1.
4 Notice: it is being obsoleted by the standard body in favor of the BOT
7 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
15 // Definition of USB CBI0 Transport Protocol
17 USB_MASS_TRANSPORT mUsbCbi0Transport
= {
27 // Definition of USB CBI1 Transport Protocol
29 USB_MASS_TRANSPORT mUsbCbi1Transport
= {
39 Initializes USB CBI protocol.
41 This function initializes the USB mass storage class CBI protocol.
42 It will save its context which is a USB_CBI_PROTOCOL structure
43 in the Context if Context isn't NULL.
45 @param UsbIo The USB I/O Protocol instance
46 @param Context The buffer to save the context to
48 @retval EFI_SUCCESS The device is successfully initialized.
49 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
50 @retval Other The USB CBI initialization fails.
55 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
56 OUT VOID
**Context OPTIONAL
59 USB_CBI_PROTOCOL
*UsbCbi
;
60 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
61 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
66 // Allocate the CBI context for USB_CBI_PROTOCOL and 3 endpoint descriptors.
68 UsbCbi
= AllocateZeroPool (
69 sizeof (USB_CBI_PROTOCOL
) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
71 ASSERT (UsbCbi
!= NULL
);
73 UsbCbi
->UsbIo
= UsbIo
;
76 // Get the interface descriptor and validate that it
77 // is a USB Mass Storage CBI interface.
79 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbCbi
->Interface
);
80 if (EFI_ERROR (Status
)) {
84 Interface
= &UsbCbi
->Interface
;
85 if ((Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI0
)
86 && (Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI1
)) {
87 Status
= EFI_UNSUPPORTED
;
92 // Locate and save the bulk-in, bulk-out, and interrupt endpoint
94 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
95 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
96 if (EFI_ERROR (Status
)) {
100 if (USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
102 // Use the first Bulk-In and Bulk-Out endpoints
104 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
105 (UsbCbi
->BulkInEndpoint
== NULL
)) {
107 UsbCbi
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1);
108 CopyMem(UsbCbi
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));;
111 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
112 (UsbCbi
->BulkOutEndpoint
== NULL
)) {
114 UsbCbi
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 1;
115 CopyMem(UsbCbi
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
117 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint
.Attributes
)) {
119 // Use the first interrupt endpoint if it is CBI0
121 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) &&
122 (UsbCbi
->InterruptEndpoint
== NULL
)) {
124 UsbCbi
->InterruptEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 2;
125 CopyMem(UsbCbi
->InterruptEndpoint
, &EndPoint
, sizeof (EndPoint
));
130 if ((UsbCbi
->BulkInEndpoint
== NULL
) || (UsbCbi
->BulkOutEndpoint
== NULL
)) {
131 Status
= EFI_UNSUPPORTED
;
134 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) && (UsbCbi
->InterruptEndpoint
== NULL
)) {
135 Status
= EFI_UNSUPPORTED
;
139 if (Context
!= NULL
) {
153 Send the command to the device using class specific control transfer.
155 This function sends command to the device using class specific control transfer.
156 The CBI contains three phases: Command, Data, and Status. This is Command phase.
158 @param UsbCbi The USB CBI protocol
159 @param Cmd The high level command to transfer to device
160 @param CmdLen The length of the command
161 @param Timeout The time to wait the command to finish
163 @retval EFI_SUCCESS The command is sent to the device.
164 @retval Others The command failed to transfer to device
169 IN USB_CBI_PROTOCOL
*UsbCbi
,
175 EFI_USB_DEVICE_REQUEST Request
;
182 // Fill in the device request, CBI use the "Accept Device-Specific
183 // Cmd" (ADSC) class specific request to send commands.
185 Request
.RequestType
= 0x21;
188 Request
.Index
= UsbCbi
->Interface
.InterfaceNumber
;
189 Request
.Length
= CmdLen
;
191 Status
= EFI_SUCCESS
;
192 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
194 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
196 // Use USB I/O Protocol to send the command to the device
201 Status
= UsbCbi
->UsbIo
->UsbControlTransfer (
211 // The device can fail the command by STALL the control endpoint.
212 // It can delay the command by NAK the data or status stage, this
213 // is a "class-specific exemption to the USB specification". Retry
214 // if the command is NAKed.
216 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
228 Transfer data between the device and host.
230 This function transfers data between the device and host.
231 The CBI contains three phases: Command, Data, and Status. This is Data phase.
233 @param UsbCbi The USB CBI device
234 @param DataDir The direction of the data transfer
235 @param Data The buffer to hold the data for input or output.
236 @param TransLen On input, the expected transfer length.
237 On output, the length of data actually transferred.
238 @param Timeout The time to wait for the command to execute
240 @retval EFI_SUCCESS The data transferred successfully.
241 @retval EFI_SUCCESS No data to transfer
242 @retval Others Failed to transfer all the data
247 IN USB_CBI_PROTOCOL
*UsbCbi
,
248 IN EFI_USB_DATA_DIRECTION DataDir
,
250 IN OUT UINTN
*TransLen
,
254 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
263 // If no data to transfer, just return EFI_SUCCESS.
265 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
270 // Select the endpoint then issue the transfer
272 if (DataDir
== EfiUsbDataIn
) {
273 Endpoint
= UsbCbi
->BulkInEndpoint
;
275 Endpoint
= UsbCbi
->BulkOutEndpoint
;
281 Status
= EFI_SUCCESS
;
282 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
285 // Transfer the data with a loop. The length of data transferred once is restricted.
290 if (Remain
> (UINTN
) USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
) {
291 Increment
= USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
;
296 Status
= UsbCbi
->UsbIo
->UsbBulkTransfer (
298 Endpoint
->EndpointAddress
,
304 if (EFI_ERROR (Status
)) {
305 if (TransStatus
== EFI_USB_ERR_NAK
) {
307 // The device can NAK the host if either the data/buffer isn't
308 // available or the command is in-progress.
309 // If data are partially transferred, we just ignore NAK and continue.
310 // If all data have been transferred and status is NAK, then we retry for several times.
311 // If retry exceeds the USB_CBI_MAX_RETRY, then return error status.
313 if (Increment
== 0) {
314 if (++Retry
> USB_CBI_MAX_RETRY
) {
327 // The device can fail the command by STALL the bulk endpoint.
328 // Clear the stall if that is the case.
330 if (TransStatus
== EFI_USB_ERR_STALL
) {
331 UsbClearEndpointStall (UsbCbi
->UsbIo
, Endpoint
->EndpointAddress
);
348 Gets the result of high level command execution from interrupt endpoint.
350 This function returns the USB transfer status, and put the high level
351 command execution result in Result.
352 The CBI contains three phases: Command, Data, and Status. This is Status phase.
354 @param UsbCbi The USB CBI protocol
355 @param Timeout The time to wait for the command to execute
356 @param Result The result of the command execution.
358 @retval EFI_SUCCESS The high level command execution result is
360 @retval Others Failed to retrieve the result.
365 IN USB_CBI_PROTOCOL
*UsbCbi
,
367 OUT USB_CBI_STATUS
*Result
376 Endpoint
= UsbCbi
->InterruptEndpoint
->EndpointAddress
;
377 Status
= EFI_SUCCESS
;
378 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
381 // Attempt to the read the result from interrupt endpoint
383 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
385 Len
= sizeof (USB_CBI_STATUS
);
387 Status
= UsbCbi
->UsbIo
->UsbSyncInterruptTransfer (
396 // The CBI can NAK the interrupt endpoint if the command is in-progress.
398 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
410 Execute USB mass storage command through the CBI0/CBI1 transport protocol.
412 @param Context The USB CBI Protocol.
413 @param Cmd The command to transfer to device
414 @param CmdLen The length of the command
415 @param DataDir The direction of data transfer
416 @param Data The buffer to hold the data
417 @param DataLen The length of the buffer
418 @param Lun Should be 0, this field for bot only
419 @param Timeout The time to wait
420 @param CmdStatus The result of the command execution
422 @retval EFI_SUCCESS The command is executed successfully.
423 @retval Other Failed to execute the command
431 IN EFI_USB_DATA_DIRECTION DataDir
,
436 OUT UINT32
*CmdStatus
439 USB_CBI_PROTOCOL
*UsbCbi
;
440 USB_CBI_STATUS Result
;
444 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
445 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
448 // Send the command to the device. Return immediately if device
449 // rejects the command.
451 Status
= UsbCbiSendCommand (UsbCbi
, Cmd
, CmdLen
, Timeout
);
452 if (EFI_ERROR (Status
)) {
453 gBS
->Stall(10 * USB_MASS_1_MILLISECOND
);
454 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n",Status
));
459 // Transfer the data. Return this status if no interrupt endpoint
460 // is used to report the transfer status.
462 TransLen
= (UINTN
) DataLen
;
464 Status
= UsbCbiDataTransfer (UsbCbi
, DataDir
, Data
, &TransLen
, Timeout
);
465 if (UsbCbi
->InterruptEndpoint
== NULL
) {
466 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n",Status
));
471 // Get the status. If it succeeds, interpret the result.
473 Status
= UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
474 if (EFI_ERROR (Status
)) {
475 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n",Status
));
479 if (UsbCbi
->Interface
.InterfaceSubClass
== USB_MASS_STORE_UFI
) {
481 // For UFI device, ASC and ASCQ are returned.
483 // Do not set the USB_MASS_CMD_FAIL for a request sense command
484 // as a bad result type doesn't mean a cmd failure
486 if (Result
.Type
!= 0 && *(UINT8
*)Cmd
!= 0x03) {
487 *CmdStatus
= USB_MASS_CMD_FAIL
;
491 // Check page 27, CBI spec 1.1 for vaious reture status.
493 switch (Result
.Value
& 0x03) {
498 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
503 // Phase Error, response with reset.
504 // No break here to fall through to "Fail".
506 UsbCbiResetDevice (UsbCbi
, FALSE
);
512 *CmdStatus
= USB_MASS_CMD_FAIL
;
517 // Persistent Fail. Need to send REQUEST SENSE.
519 *CmdStatus
= USB_MASS_CMD_PERSISTENT
;
529 Reset the USB mass storage device by CBI protocol.
531 This function resets the USB mass storage device by CBI protocol.
532 The reset is defined as a non-data command. Don't use UsbCbiExecCommand
533 to send the command to device because that may introduce recursive loop.
535 @param Context The USB CBI protocol
536 @param ExtendedVerification The flag controlling the rule of reset.
539 @retval EFI_SUCCESS The device is reset.
540 @retval Others Failed to reset the device.
546 IN BOOLEAN ExtendedVerification
549 UINT8 ResetCmd
[USB_CBI_RESET_CMD_LEN
];
550 USB_CBI_PROTOCOL
*UsbCbi
;
551 USB_CBI_STATUS Result
;
555 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
558 // Fill in the reset command.
560 SetMem (ResetCmd
, USB_CBI_RESET_CMD_LEN
, 0xFF);
564 Timeout
= USB_CBI_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
567 // Send the command to the device. Don't use UsbCbiExecCommand here.
569 Status
= UsbCbiSendCommand (UsbCbi
, ResetCmd
, USB_CBI_RESET_CMD_LEN
, Timeout
);
570 if (EFI_ERROR (Status
)) {
571 return EFI_DEVICE_ERROR
;
575 // Just retrieve the status and ignore that. Then stall
576 // 50ms to wait for it to complete.
578 UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
579 gBS
->Stall (USB_CBI_RESET_DEVICE_STALL
);
582 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.
584 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkInEndpoint
->EndpointAddress
);
585 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkOutEndpoint
->EndpointAddress
);
592 Clean up the CBI protocol's resource.
594 @param Context The instance of CBI protocol.
596 @retval EFI_SUCCESS The resource is cleaned up.