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
))
88 Status
= EFI_UNSUPPORTED
;
93 // Locate and save the bulk-in, bulk-out, and interrupt endpoint
95 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
96 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
97 if (EFI_ERROR (Status
)) {
101 if (USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
103 // Use the first Bulk-In and Bulk-Out endpoints
105 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
106 (UsbCbi
->BulkInEndpoint
== NULL
))
108 UsbCbi
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*)(UsbCbi
+ 1);
109 CopyMem (UsbCbi
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));
112 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
113 (UsbCbi
->BulkOutEndpoint
== NULL
))
115 UsbCbi
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*)(UsbCbi
+ 1) + 1;
116 CopyMem (UsbCbi
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
118 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint
.Attributes
)) {
120 // Use the first interrupt endpoint if it is CBI0
122 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) &&
123 (UsbCbi
->InterruptEndpoint
== NULL
))
125 UsbCbi
->InterruptEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*)(UsbCbi
+ 1) + 2;
126 CopyMem (UsbCbi
->InterruptEndpoint
, &EndPoint
, sizeof (EndPoint
));
131 if ((UsbCbi
->BulkInEndpoint
== NULL
) || (UsbCbi
->BulkOutEndpoint
== NULL
)) {
132 Status
= EFI_UNSUPPORTED
;
136 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) && (UsbCbi
->InterruptEndpoint
== NULL
)) {
137 Status
= EFI_UNSUPPORTED
;
141 if (Context
!= NULL
) {
155 Send the command to the device using class specific control transfer.
157 This function sends command to the device using class specific control transfer.
158 The CBI contains three phases: Command, Data, and Status. This is Command phase.
160 @param UsbCbi The USB CBI protocol
161 @param Cmd The high level command to transfer to device
162 @param CmdLen The length of the command
163 @param Timeout The time to wait the command to finish
165 @retval EFI_SUCCESS The command is sent to the device.
166 @retval Others The command failed to transfer to device
171 IN USB_CBI_PROTOCOL
*UsbCbi
,
177 EFI_USB_DEVICE_REQUEST Request
;
184 // Fill in the device request, CBI use the "Accept Device-Specific
185 // Cmd" (ADSC) class specific request to send commands.
187 Request
.RequestType
= 0x21;
190 Request
.Index
= UsbCbi
->Interface
.InterfaceNumber
;
191 Request
.Length
= CmdLen
;
193 Status
= EFI_SUCCESS
;
194 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
196 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
198 // Use USB I/O Protocol to send the command to the device
203 Status
= UsbCbi
->UsbIo
->UsbControlTransfer (
213 // The device can fail the command by STALL the control endpoint.
214 // It can delay the command by NAK the data or status stage, this
215 // is a "class-specific exemption to the USB specification". Retry
216 // if the command is NAKed.
218 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
229 Transfer data between the device and host.
231 This function transfers data between the device and host.
232 The CBI contains three phases: Command, Data, and Status. This is Data phase.
234 @param UsbCbi The USB CBI device
235 @param DataDir The direction of the data transfer
236 @param Data The buffer to hold the data for input or output.
237 @param TransLen On input, the expected transfer length.
238 On output, the length of data actually transferred.
239 @param Timeout The time to wait for the command to execute
241 @retval EFI_SUCCESS The data transferred successfully.
242 @retval EFI_SUCCESS No data to transfer
243 @retval Others Failed to transfer all the data
248 IN USB_CBI_PROTOCOL
*UsbCbi
,
249 IN EFI_USB_DATA_DIRECTION DataDir
,
251 IN OUT UINTN
*TransLen
,
255 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
264 // If no data to transfer, just return EFI_SUCCESS.
266 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
271 // Select the endpoint then issue the transfer
273 if (DataDir
== EfiUsbDataIn
) {
274 Endpoint
= UsbCbi
->BulkInEndpoint
;
276 Endpoint
= UsbCbi
->BulkOutEndpoint
;
282 Status
= EFI_SUCCESS
;
283 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
286 // Transfer the data with a loop. The length of data transferred once is restricted.
291 if (Remain
> (UINTN
)USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
) {
292 Increment
= USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
;
297 Status
= UsbCbi
->UsbIo
->UsbBulkTransfer (
299 Endpoint
->EndpointAddress
,
305 if (EFI_ERROR (Status
)) {
306 if (TransStatus
== EFI_USB_ERR_NAK
) {
308 // The device can NAK the host if either the data/buffer isn't
309 // available or the command is in-progress.
310 // If data are partially transferred, we just ignore NAK and continue.
311 // If all data have been transferred and status is NAK, then we retry for several times.
312 // If retry exceeds the USB_CBI_MAX_RETRY, then return error status.
314 if (Increment
== 0) {
315 if (++Retry
> USB_CBI_MAX_RETRY
) {
328 // The device can fail the command by STALL the bulk endpoint.
329 // Clear the stall if that is the case.
331 if (TransStatus
== EFI_USB_ERR_STALL
) {
332 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
)) {
409 Execute USB mass storage command through the CBI0/CBI1 transport protocol.
411 @param Context The USB CBI Protocol.
412 @param Cmd The command to transfer to device
413 @param CmdLen The length of the command
414 @param DataDir The direction of data transfer
415 @param Data The buffer to hold the data
416 @param DataLen The length of the buffer
417 @param Lun Should be 0, this field for bot only
418 @param Timeout The time to wait
419 @param CmdStatus The result of the command execution
421 @retval EFI_SUCCESS The command is executed successfully.
422 @retval Other Failed to execute the command
430 IN EFI_USB_DATA_DIRECTION DataDir
,
435 OUT UINT32
*CmdStatus
438 USB_CBI_PROTOCOL
*UsbCbi
;
439 USB_CBI_STATUS Result
;
443 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
444 UsbCbi
= (USB_CBI_PROTOCOL
*)Context
;
447 // Send the command to the device. Return immediately if device
448 // rejects the command.
450 Status
= UsbCbiSendCommand (UsbCbi
, Cmd
, CmdLen
, Timeout
);
451 if (EFI_ERROR (Status
)) {
452 gBS
->Stall (10 * USB_MASS_1_MILLISECOND
);
453 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n", Status
));
458 // Transfer the data. Return this status if no interrupt endpoint
459 // is used to report the transfer status.
461 TransLen
= (UINTN
)DataLen
;
463 Status
= UsbCbiDataTransfer (UsbCbi
, DataDir
, Data
, &TransLen
, Timeout
);
464 if (UsbCbi
->InterruptEndpoint
== NULL
) {
465 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n", Status
));
470 // Get the status. If it succeeds, interpret the result.
472 Status
= UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
473 if (EFI_ERROR (Status
)) {
474 DEBUG ((DEBUG_ERROR
, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n", Status
));
478 if (UsbCbi
->Interface
.InterfaceSubClass
== USB_MASS_STORE_UFI
) {
480 // For UFI device, ASC and ASCQ are returned.
482 // Do not set the USB_MASS_CMD_FAIL for a request sense command
483 // as a bad result type doesn't mean a cmd failure
485 if ((Result
.Type
!= 0) && (*(UINT8
*)Cmd
!= 0x03)) {
486 *CmdStatus
= USB_MASS_CMD_FAIL
;
490 // Check page 27, CBI spec 1.1 for vaious reture status.
492 switch (Result
.Value
& 0x03) {
497 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
502 // Phase Error, response with reset.
503 // No break here to fall through to "Fail".
505 UsbCbiResetDevice (UsbCbi
, FALSE
);
511 *CmdStatus
= USB_MASS_CMD_FAIL
;
516 // Persistent Fail. Need to send REQUEST SENSE.
518 *CmdStatus
= USB_MASS_CMD_PERSISTENT
;
527 Reset the USB mass storage device by CBI protocol.
529 This function resets the USB mass storage device by CBI protocol.
530 The reset is defined as a non-data command. Don't use UsbCbiExecCommand
531 to send the command to device because that may introduce recursive loop.
533 @param Context The USB CBI protocol
534 @param ExtendedVerification The flag controlling the rule of reset.
537 @retval EFI_SUCCESS The device is reset.
538 @retval Others Failed to reset the device.
544 IN BOOLEAN ExtendedVerification
547 UINT8 ResetCmd
[USB_CBI_RESET_CMD_LEN
];
548 USB_CBI_PROTOCOL
*UsbCbi
;
549 USB_CBI_STATUS Result
;
553 UsbCbi
= (USB_CBI_PROTOCOL
*)Context
;
556 // Fill in the reset command.
558 SetMem (ResetCmd
, USB_CBI_RESET_CMD_LEN
, 0xFF);
562 Timeout
= USB_CBI_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
565 // Send the command to the device. Don't use UsbCbiExecCommand here.
567 Status
= UsbCbiSendCommand (UsbCbi
, ResetCmd
, USB_CBI_RESET_CMD_LEN
, Timeout
);
568 if (EFI_ERROR (Status
)) {
569 return EFI_DEVICE_ERROR
;
573 // Just retrieve the status and ignore that. Then stall
574 // 50ms to wait for it to complete.
576 UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
577 gBS
->Stall (USB_CBI_RESET_DEVICE_STALL
);
580 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.
582 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkInEndpoint
->EndpointAddress
);
583 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkOutEndpoint
->EndpointAddress
);
589 Clean up the CBI protocol's resource.
591 @param Context The instance of CBI protocol.
593 @retval EFI_SUCCESS The resource is cleaned up.