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 - 2008, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "UsbMassBoot.h"
19 #include "UsbMassCbi.h"
22 // Definition of USB CBI0 Transport Protocol
24 USB_MASS_TRANSPORT mUsbCbi0Transport
= {
34 // Definition of USB CBI1 Transport Protocol
36 USB_MASS_TRANSPORT mUsbCbi1Transport
= {
46 Initializes USB CBI protocol.
48 This function initializes the USB mass storage class CBI protocol.
49 It will save its context which is a USB_CBI_PROTOCOL structure
50 in the Context if Context isn't NULL.
52 @param UsbIo The USB I/O Protocol instance
53 @param Context The buffer to save the context to
55 @retval EFI_SUCCESS The device is successfully initialized.
56 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
57 @retval Other The USB CBI initialization fails.
62 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
63 OUT VOID
**Context OPTIONAL
66 USB_CBI_PROTOCOL
*UsbCbi
;
67 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
68 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
73 // Allocate the CBI context for USB_CBI_PROTOCOL and 3 endpoint descriptors.
75 UsbCbi
= AllocateZeroPool (
76 sizeof (USB_CBI_PROTOCOL
) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
78 ASSERT (UsbCbi
!= NULL
);
80 UsbCbi
->UsbIo
= UsbIo
;
83 // Get the interface descriptor and validate that it
84 // is a USB Mass Storage CBI interface.
86 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbCbi
->Interface
);
87 if (EFI_ERROR (Status
)) {
91 Interface
= &UsbCbi
->Interface
;
92 if ((Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI0
)
93 && (Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI1
)) {
94 Status
= EFI_UNSUPPORTED
;
99 // Locate and save the bulk-in, bulk-out, and interrupt endpoint
101 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
102 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
103 if (EFI_ERROR (Status
)) {
107 if (USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
109 // Use the first Bulk-In and Bulk-Out endpoints
111 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
112 (UsbCbi
->BulkInEndpoint
== NULL
)) {
114 UsbCbi
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1);
115 CopyMem(UsbCbi
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));;
118 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
119 (UsbCbi
->BulkOutEndpoint
== NULL
)) {
121 UsbCbi
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 1;
122 CopyMem(UsbCbi
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
124 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint
.Attributes
)) {
126 // Use the first interrupt endpoint if it is CBI0
128 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) &&
129 (UsbCbi
->InterruptEndpoint
== NULL
)) {
131 UsbCbi
->InterruptEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 2;
132 CopyMem(UsbCbi
->InterruptEndpoint
, &EndPoint
, sizeof (EndPoint
));
137 if ((UsbCbi
->BulkInEndpoint
== NULL
) || (UsbCbi
->BulkOutEndpoint
== NULL
)) {
138 Status
= EFI_UNSUPPORTED
;
141 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) && (UsbCbi
->InterruptEndpoint
== NULL
)) {
142 Status
= EFI_UNSUPPORTED
;
146 if (Context
!= NULL
) {
149 gBS
->FreePool (UsbCbi
);
155 gBS
->FreePool (UsbCbi
);
160 Send the command to the device using class specific control transfer.
162 This function sends command to the device using class specific control transfer.
163 The CBI contains three phases: Command, Data, and Status. This is Command phase.
165 @param UsbCbi The USB CBI protocol
166 @param Cmd The high level command to transfer to device
167 @param CmdLen The length of the command
168 @param Timeout The time to wait the command to finish
170 @retval EFI_SUCCESS The command is sent to the device.
171 @retval Others The command failed to transfer to device
176 IN USB_CBI_PROTOCOL
*UsbCbi
,
182 EFI_USB_DEVICE_REQUEST Request
;
189 // Fill in the device request, CBI use the "Accept Device-Specific
190 // Cmd" (ADSC) class specific request to send commands.
192 Request
.RequestType
= 0x21;
195 Request
.Index
= UsbCbi
->Interface
.InterfaceNumber
;
196 Request
.Length
= CmdLen
;
198 Status
= EFI_SUCCESS
;
199 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
201 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
203 // Use USB I/O Protocol to send the command to the device
208 Status
= UsbCbi
->UsbIo
->UsbControlTransfer (
218 // The device can fail the command by STALL the control endpoint.
219 // It can delay the command by NAK the data or status stage, this
220 // is a "class-specific exemption to the USB specification". Retry
221 // if the command is NAKed.
223 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
235 Transfer data between the device and host.
237 This function transfers data between the device and host.
238 The CBI contains three phases: Command, Data, and Status. This is Data phase.
240 @param UsbCbi The USB CBI device
241 @param DataDir The direction of the data transfer
242 @param Data The buffer to hold the data for input or output.
243 @param TransLen On input, the expected transfer length.
244 On output, the length of data actually transferred.
245 @param Timeout The time to wait for the command to execute
247 @retval EFI_SUCCESS The data transferred successfully.
248 @retval EFI_SUCCESS No data to transfer
249 @retval Others Failed to transfer all the data
254 IN USB_CBI_PROTOCOL
*UsbCbi
,
255 IN EFI_USB_DATA_DIRECTION DataDir
,
257 IN OUT UINTN
*TransLen
,
261 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
270 // If no data to transfer, just return EFI_SUCCESS.
272 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
277 // Select the endpoint then issue the transfer
279 if (DataDir
== EfiUsbDataIn
) {
280 Endpoint
= UsbCbi
->BulkInEndpoint
;
282 Endpoint
= UsbCbi
->BulkOutEndpoint
;
288 Status
= EFI_SUCCESS
;
289 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
292 // Transfer the data with a loop. The length of data transferred once is restricted.
297 if (Remain
> (UINTN
) USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
) {
298 Increment
= USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
;
303 Status
= UsbCbi
->UsbIo
->UsbBulkTransfer (
305 Endpoint
->EndpointAddress
,
311 if (EFI_ERROR (Status
)) {
312 if (TransStatus
== EFI_USB_ERR_NAK
) {
314 // The device can NAK the host if either the data/buffer isn't
315 // aviable or the command is in-progress.
316 // If data are partially transferred, we just ignore NAK and continue.
317 // If all data have been transferred and status is NAK, then we retry for several times.
318 // If retry exceeds the USB_CBI_MAX_RETRY, then return error status.
320 if (Increment
== 0) {
321 if (++Retry
> USB_CBI_MAX_RETRY
) {
334 // The device can fail the command by STALL the bulk endpoint.
335 // Clear the stall if that is the case.
337 if (TransStatus
== EFI_USB_ERR_STALL
) {
338 UsbClearEndpointStall (UsbCbi
->UsbIo
, Endpoint
->EndpointAddress
);
355 Gets the result of high level command execution from interrupt endpoint.
357 This function returns the USB transfer status, and put the high level
358 command execution result in Result.
359 The CBI contains three phases: Command, Data, and Status. This is Status phase.
361 @param UsbCbi The USB CBI protocol
362 @param Timeout The time to wait for the command to execute
363 @param Result The result of the command execution.
365 @retval EFI_SUCCESS The high level command execution result is
367 @retval Others Failed to retrieve the result.
372 IN USB_CBI_PROTOCOL
*UsbCbi
,
374 OUT USB_CBI_STATUS
*Result
383 Endpoint
= UsbCbi
->InterruptEndpoint
->EndpointAddress
;
384 Status
= EFI_SUCCESS
;
385 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
388 // Attemp to the read the result from interrupt endpoint
390 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
392 Len
= sizeof (USB_CBI_STATUS
);
394 Status
= UsbCbi
->UsbIo
->UsbSyncInterruptTransfer (
403 // The CBI can NAK the interrupt endpoint if the command is in-progress.
405 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
417 Execute USB mass storage command through the CBI0/CBI1 transport protocol.
419 @param Context The USB CBI Protocol.
420 @param Cmd The command to transfer to device
421 @param CmdLen The length of the command
422 @param DataDir The direction of data transfer
423 @param Data The buffer to hold the data
424 @param DataLen The length of the buffer
425 @param Lun Should be 0, this field for bot only
426 @param Timeout The time to wait
427 @param CmdStatus The result of the command execution
429 @retval EFI_SUCCESS The command is executed successfully.
430 @retval Other Failed to execute the command
438 IN EFI_USB_DATA_DIRECTION DataDir
,
443 OUT UINT32
*CmdStatus
446 USB_CBI_PROTOCOL
*UsbCbi
;
447 USB_CBI_STATUS Result
;
451 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
452 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
455 // Send the command to the device. Return immediately if device
456 // rejects the command.
458 Status
= UsbCbiSendCommand (UsbCbi
, Cmd
, CmdLen
, Timeout
);
459 if (EFI_ERROR (Status
)) {
460 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n",Status
));
465 // Transfer the data. Return this status if no interrupt endpoint
466 // is used to report the transfer status.
468 TransLen
= (UINTN
) DataLen
;
470 Status
= UsbCbiDataTransfer (UsbCbi
, DataDir
, Data
, &TransLen
, Timeout
);
471 if (UsbCbi
->InterruptEndpoint
== NULL
) {
472 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n",Status
));
477 // Get the status. If it succeeds, interpret the result.
479 Status
= UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
480 if (EFI_ERROR (Status
)) {
481 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n",Status
));
485 if (UsbCbi
->Interface
.InterfaceSubClass
== USB_MASS_STORE_UFI
) {
487 // For UFI device, ASC and ASCQ are returned.
489 if (Result
.Type
!= 0) {
490 *CmdStatus
= USB_MASS_CMD_FAIL
;
495 // Check page 27, CBI spec 1.1 for vaious reture status.
497 switch (Result
.Value
& 0x03) {
502 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
507 // Phase Error, response with reset.
508 // No break here to fall through to "Fail".
510 UsbCbiResetDevice (UsbCbi
, FALSE
);
516 *CmdStatus
= USB_MASS_CMD_FAIL
;
521 // Persistent Fail. Need to send REQUEST SENSE.
523 *CmdStatus
= USB_MASS_CMD_PERSISTENT
;
533 Reset the USB mass storage device by CBI protocol.
535 This function resets the USB mass storage device by CBI protocol.
536 The reset is defined as a non-data command. Don't use UsbCbiExecCommand
537 to send the command to device because that may introduce recursive loop.
539 @param Context The USB CBI protocol
540 @param ExtendedVerification The flag controlling the rule of reset.
543 @retval EFI_SUCCESS The device is reset.
544 @retval Others Failed to reset the device.
550 IN BOOLEAN ExtendedVerification
553 UINT8 ResetCmd
[USB_CBI_RESET_CMD_LEN
];
554 USB_CBI_PROTOCOL
*UsbCbi
;
555 USB_CBI_STATUS Result
;
559 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
562 // Fill in the reset command.
564 SetMem (ResetCmd
, USB_CBI_RESET_CMD_LEN
, 0xFF);
568 Timeout
= USB_CBI_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
571 // Send the command to the device. Don't use UsbCbiExecCommand here.
573 Status
= UsbCbiSendCommand (UsbCbi
, ResetCmd
, USB_CBI_RESET_CMD_LEN
, Timeout
);
574 if (EFI_ERROR (Status
)) {
579 // Just retrieve the status and ignore that. Then stall
580 // 50ms to wait for it to complete.
582 UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
583 gBS
->Stall (USB_CBI_RESET_DEVICE_STALL
);
586 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.
588 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkInEndpoint
->EndpointAddress
);
589 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkOutEndpoint
->EndpointAddress
);
596 Clean up the CBI protocol's resource.
598 @param Context The instance of CBI protocol.
600 @retval EFI_SUCCESS The resource is cleaned up.
608 gBS
->FreePool (Context
);