3 Implementation of the USB mass storage Control/Bulk/Interrupt transport.
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.
19 #include "UsbMassCbi.h"
22 Call the Usb mass storage class transport protocol to
23 reset the device. The reset is defined as a Non-Data
24 command. Don't use UsbCbiExecCommand to send the command
25 to device because that may introduce recursive loop.
27 @param Context The USB CBI device protocol
28 @param ExtendedVerification The flag controlling the rule of reset
30 @retval EFI_SUCCESS the device is reset
31 @retval Others Failed to reset the device
37 IN BOOLEAN ExtendedVerification
42 Initialize the USB mass storage class CBI transport protocol.
43 If Context isn't NULL, it will save its context in it.
45 @param UsbIo The USB IO to use
46 @param Context The variable to save context in
48 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory
49 @retval EFI_UNSUPPORTED The device isn't supported
50 @retval EFI_SUCCESS The CBI protocol is initialized.
51 @retval Other The Usb cbi init failed.
56 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
57 OUT VOID
**Context OPTIONAL
60 USB_CBI_PROTOCOL
*UsbCbi
;
61 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
62 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
67 // Allocate the CBI context
69 UsbCbi
= AllocateZeroPool (
70 sizeof (USB_CBI_PROTOCOL
) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
74 return EFI_OUT_OF_RESOURCES
;
77 UsbCbi
->UsbIo
= UsbIo
;
80 // Get the interface descriptor and validate that it is a USB mass
81 // storage class CBI interface.
83 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbCbi
->Interface
);
84 if (EFI_ERROR (Status
)) {
88 Interface
= &UsbCbi
->Interface
;
89 if ((Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI0
)
90 && (Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI1
)) {
91 Status
= EFI_UNSUPPORTED
;
96 // Locate and save the bulk-in, bulk-out, and interrupt endpoint
98 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
99 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
100 if (EFI_ERROR (Status
)) {
104 if (USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
106 // Use the first Bulk-In and Bulk-Out endpoints
108 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
109 (UsbCbi
->BulkInEndpoint
== NULL
)) {
111 UsbCbi
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1);
112 CopyMem(UsbCbi
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));;
115 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
116 (UsbCbi
->BulkOutEndpoint
== NULL
)) {
118 UsbCbi
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 1;
119 CopyMem(UsbCbi
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
122 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint
.Attributes
)) {
124 // Use the first interrupt endpoint if it is CBI0
126 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) &&
127 (UsbCbi
->InterruptEndpoint
== NULL
)) {
129 UsbCbi
->InterruptEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 2;
130 CopyMem(UsbCbi
->InterruptEndpoint
, &EndPoint
, sizeof (EndPoint
));
135 if ((UsbCbi
->BulkInEndpoint
== NULL
)
136 || (UsbCbi
->BulkOutEndpoint
== NULL
)
137 || ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
)
138 && (UsbCbi
->InterruptEndpoint
== NULL
))) {
139 Status
= EFI_UNSUPPORTED
;
143 if (Context
!= NULL
) {
146 gBS
->FreePool (UsbCbi
);
151 gBS
->FreePool (UsbCbi
);
157 Send the command to the device using class specific control transfer.
159 @param UsbCbi The USB CBI protocol
160 @param Cmd The high level command to transfer to device
161 @param CmdLen The length of the command
162 @param Timeout The time to wait the command to finish
164 @retval EFI_SUCCESS The command is transferred to device
165 @retval Others The command failed to transfer to device
170 IN USB_CBI_PROTOCOL
*UsbCbi
,
176 EFI_USB_DEVICE_REQUEST Request
;
183 // Fill in the device request, CBI use the "Accept Device-Specific
184 // Cmd" (ADSC) class specific request to send commands
186 Request
.RequestType
= 0x21;
189 Request
.Index
= UsbCbi
->Interface
.InterfaceNumber
;
190 Request
.Length
= CmdLen
;
192 Status
= EFI_SUCCESS
;
193 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
195 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
197 // Use the UsbIo to send the command to the device
202 Status
= UsbCbi
->UsbIo
->UsbControlTransfer (
212 // The device can fail the command by STALL the control endpoint.
213 // It can delay the command by NAK the data or status stage, this
214 // is a "class-specific exemption to the USB specification". Retry
215 // if the command is NAKed.
217 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
229 Transfer data between the device and host. The CBI contains three phase,
230 command, data, and status. This is data phase.
232 @param UsbCbi The USB CBI device
233 @param DataDir The direction of the data transfer
234 @param Data The buffer to hold the data
235 @param TransLen The expected transfer length
236 @param Timeout The time to wait the command to execute
238 @retval EFI_SUCCESS The data transfer succeeded
239 @retval Others Failed to transfer all the data
244 IN USB_CBI_PROTOCOL
*UsbCbi
,
245 IN EFI_USB_DATA_DIRECTION DataDir
,
247 IN OUT UINTN
*TransLen
,
251 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
260 // It's OK if no data to transfer
262 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
267 // Select the endpoint then issue the transfer
269 if (DataDir
== EfiUsbDataIn
) {
270 Endpoint
= UsbCbi
->BulkInEndpoint
;
272 Endpoint
= UsbCbi
->BulkOutEndpoint
;
278 Status
= EFI_SUCCESS
;
279 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
282 // Transfer the data, if the device returns NAK, retry it.
287 if (Remain
> (UINTN
) USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
) {
288 Increment
= USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
;
293 Status
= UsbCbi
->UsbIo
->UsbBulkTransfer (
295 Endpoint
->EndpointAddress
,
301 if (EFI_ERROR (Status
)) {
302 if (TransStatus
== EFI_USB_ERR_NAK
) {
304 // The device can NAK the host if either the data/buffer isn't
305 // aviable or the command is in-progress. The data can be partly
306 // transferred. The transfer is aborted if several succssive data
307 // transfer commands are NAKed.
309 if (Increment
== 0) {
310 if (++Retry
> USB_CBI_MAX_RETRY
) {
324 // The device can fail the command by STALL the bulk endpoint.
325 // Clear the stall if that is the case.
327 if (TransStatus
== EFI_USB_ERR_STALL
) {
328 UsbClearEndpointStall (UsbCbi
->UsbIo
, Endpoint
->EndpointAddress
);
345 Get the result of high level command execution from interrupt
346 endpoint. This function returns the USB transfer status, and
347 put the high level command execution result in Result.
349 @param UsbCbi The USB CBI protocol
350 @param Timeout The time to wait the command to execute
351 @param Result GC_TODO: add argument description
353 @retval EFI_SUCCESS The high level command execution result is
355 @retval Others Failed to retrieve the result.
360 IN USB_CBI_PROTOCOL
*UsbCbi
,
362 OUT USB_CBI_STATUS
*Result
371 Endpoint
= UsbCbi
->InterruptEndpoint
->EndpointAddress
;
372 Status
= EFI_SUCCESS
;
373 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
376 // Attemp to the read the result from interrupt endpoint
378 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
380 Len
= sizeof (USB_CBI_STATUS
);
382 Status
= UsbCbi
->UsbIo
->UsbSyncInterruptTransfer (
391 // The CBI can NAK the interrupt endpoint if the command is in-progress.
393 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
405 Execute USB mass storage command through the CBI0/CBI1 transport protocol.
407 @param Context The USB CBI device
408 @param Cmd The command to transfer to device
409 @param CmdLen The length of the command
410 @param DataDir The direction of data transfer
411 @param Data The buffer to hold the data
412 @param DataLen The length of the buffer
413 @param Lun Should be 0, this field for bot only
414 @param Timeout The time to wait
415 @param CmdStatus The result of the command execution
417 @retval EFI_SUCCESS The command is executed OK and result in CmdStatus.
418 @retval Other Failed to execute the command
426 IN EFI_USB_DATA_DIRECTION DataDir
,
431 OUT UINT32
*CmdStatus
434 USB_CBI_PROTOCOL
*UsbCbi
;
435 USB_CBI_STATUS Result
;
439 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
440 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
443 // Send the command to the device. Return immediately if device
444 // rejects the command.
446 Status
= UsbCbiSendCommand (UsbCbi
, Cmd
, CmdLen
, Timeout
);
447 if (EFI_ERROR (Status
)) {
448 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n",Status
));
453 // Transfer the data, return this status if no interrupt endpoint
454 // is used to report the transfer status.
456 TransLen
= (UINTN
) DataLen
;
458 Status
= UsbCbiDataTransfer (UsbCbi
, DataDir
, Data
, &TransLen
, Timeout
);
459 if (UsbCbi
->InterruptEndpoint
== NULL
) {
460 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n",Status
));
465 // Get the status, if that succeeds, interpret the result
467 Status
= UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
468 if (EFI_ERROR (Status
)) {
469 DEBUG ((EFI_D_ERROR
, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n",Status
));
470 return EFI_DEVICE_ERROR
;
473 if (UsbCbi
->Interface
.InterfaceSubClass
== USB_MASS_STORE_UFI
) {
475 // For UFI device, ASC and ASCQ are returned.
477 if (Result
.Type
!= 0) {
478 *CmdStatus
= USB_MASS_CMD_FAIL
;
483 // Check page 27, CBI spec 1.1 for vaious reture status.
485 switch (Result
.Value
& 0x03) {
490 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
495 // Phase Error, response with reset. Fall through to Fail.
497 UsbCbiResetDevice (UsbCbi
, FALSE
);
503 *CmdStatus
= USB_MASS_CMD_FAIL
;
508 // Persistent Fail, need to send REQUEST SENSE.
510 *CmdStatus
= USB_MASS_CMD_PERSISTENT
;
520 Call the Usb mass storage class transport protocol to
521 reset the device. The reset is defined as a Non-Data
522 command. Don't use UsbCbiExecCommand to send the command
523 to device because that may introduce recursive loop.
525 @param Context The USB CBI device protocol
526 @param ExtendedVerification The flag controlling the rule of reset
528 @retval EFI_SUCCESS the device is reset
529 @retval Others Failed to reset the device
535 IN BOOLEAN ExtendedVerification
538 UINT8 ResetCmd
[USB_CBI_RESET_CMD_LEN
];
539 USB_CBI_PROTOCOL
*UsbCbi
;
540 USB_CBI_STATUS Result
;
544 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
547 // Fill in the reset command.
549 SetMem (ResetCmd
, USB_CBI_RESET_CMD_LEN
, 0xFF);
553 Timeout
= USB_CBI_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
556 // Send the command to the device. Don't use UsbCbiExecCommand here.
558 Status
= UsbCbiSendCommand (UsbCbi
, ResetCmd
, USB_CBI_RESET_CMD_LEN
, Timeout
);
559 if (EFI_ERROR (Status
)) {
564 // Just retrieve the status and ignore that. Then stall
565 // 50ms to wait it complete
567 UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
568 gBS
->Stall (USB_CBI_RESET_DEVICE_STALL
);
571 // Clear the Bulk-In and Bulk-Out stall condition and
574 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkInEndpoint
->EndpointAddress
);
575 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkOutEndpoint
->EndpointAddress
);
581 Clean up the CBI protocol's resource.
583 @param Context The instance of CBI protocol.
585 @retval EFI_SUCCESS The resource is cleaned up.
593 gBS
->FreePool (Context
);
598 mUsbCbi0Transport
= {
608 mUsbCbi1Transport
= {