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 - 2011, Intel Corporation. All rights reserved.<BR>
8 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.
21 // Definition of USB CBI0 Transport Protocol
23 USB_MASS_TRANSPORT mUsbCbi0Transport
= {
33 // Definition of USB CBI1 Transport Protocol
35 USB_MASS_TRANSPORT mUsbCbi1Transport
= {
45 Initializes USB CBI protocol.
47 This function initializes the USB mass storage class CBI protocol.
48 It will save its context which is a USB_CBI_PROTOCOL structure
49 in the Context if Context isn't NULL.
51 @param UsbIo The USB I/O Protocol instance
52 @param Context The buffer to save the context to
54 @retval EFI_SUCCESS The device is successfully initialized.
55 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
56 @retval Other The USB CBI initialization fails.
61 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
62 OUT VOID
**Context OPTIONAL
65 USB_CBI_PROTOCOL
*UsbCbi
;
66 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
67 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
72 // Allocate the CBI context for USB_CBI_PROTOCOL and 3 endpoint descriptors.
74 UsbCbi
= AllocateZeroPool (
75 sizeof (USB_CBI_PROTOCOL
) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
77 ASSERT (UsbCbi
!= NULL
);
79 UsbCbi
->UsbIo
= UsbIo
;
82 // Get the interface descriptor and validate that it
83 // is a USB Mass Storage CBI interface.
85 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbCbi
->Interface
);
86 if (EFI_ERROR (Status
)) {
90 Interface
= &UsbCbi
->Interface
;
91 if ((Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI0
)
92 && (Interface
->InterfaceProtocol
!= USB_MASS_STORE_CBI1
)) {
93 Status
= EFI_UNSUPPORTED
;
98 // Locate and save the bulk-in, bulk-out, and interrupt endpoint
100 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
101 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
102 if (EFI_ERROR (Status
)) {
106 if (USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
108 // Use the first Bulk-In and Bulk-Out endpoints
110 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
111 (UsbCbi
->BulkInEndpoint
== NULL
)) {
113 UsbCbi
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1);
114 CopyMem(UsbCbi
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));;
117 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
118 (UsbCbi
->BulkOutEndpoint
== NULL
)) {
120 UsbCbi
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 1;
121 CopyMem(UsbCbi
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
123 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint
.Attributes
)) {
125 // Use the first interrupt endpoint if it is CBI0
127 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) &&
128 (UsbCbi
->InterruptEndpoint
== NULL
)) {
130 UsbCbi
->InterruptEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbCbi
+ 1) + 2;
131 CopyMem(UsbCbi
->InterruptEndpoint
, &EndPoint
, sizeof (EndPoint
));
136 if ((UsbCbi
->BulkInEndpoint
== NULL
) || (UsbCbi
->BulkOutEndpoint
== NULL
)) {
137 Status
= EFI_UNSUPPORTED
;
140 if ((Interface
->InterfaceProtocol
== USB_MASS_STORE_CBI0
) && (UsbCbi
->InterruptEndpoint
== NULL
)) {
141 Status
= EFI_UNSUPPORTED
;
145 if (Context
!= NULL
) {
159 Send the command to the device using class specific control transfer.
161 This function sends command to the device using class specific control transfer.
162 The CBI contains three phases: Command, Data, and Status. This is Command phase.
164 @param UsbCbi The USB CBI protocol
165 @param Cmd The high level command to transfer to device
166 @param CmdLen The length of the command
167 @param Timeout The time to wait the command to finish
169 @retval EFI_SUCCESS The command is sent to the device.
170 @retval Others The command failed to transfer to device
175 IN USB_CBI_PROTOCOL
*UsbCbi
,
181 EFI_USB_DEVICE_REQUEST Request
;
188 // Fill in the device request, CBI use the "Accept Device-Specific
189 // Cmd" (ADSC) class specific request to send commands.
191 Request
.RequestType
= 0x21;
194 Request
.Index
= UsbCbi
->Interface
.InterfaceNumber
;
195 Request
.Length
= CmdLen
;
197 Status
= EFI_SUCCESS
;
198 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
200 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
202 // Use USB I/O Protocol to send the command to the device
207 Status
= UsbCbi
->UsbIo
->UsbControlTransfer (
217 // The device can fail the command by STALL the control endpoint.
218 // It can delay the command by NAK the data or status stage, this
219 // is a "class-specific exemption to the USB specification". Retry
220 // if the command is NAKed.
222 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
234 Transfer data between the device and host.
236 This function transfers data between the device and host.
237 The CBI contains three phases: Command, Data, and Status. This is Data phase.
239 @param UsbCbi The USB CBI device
240 @param DataDir The direction of the data transfer
241 @param Data The buffer to hold the data for input or output.
242 @param TransLen On input, the expected transfer length.
243 On output, the length of data actually transferred.
244 @param Timeout The time to wait for the command to execute
246 @retval EFI_SUCCESS The data transferred successfully.
247 @retval EFI_SUCCESS No data to transfer
248 @retval Others Failed to transfer all the data
253 IN USB_CBI_PROTOCOL
*UsbCbi
,
254 IN EFI_USB_DATA_DIRECTION DataDir
,
256 IN OUT UINTN
*TransLen
,
260 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
269 // If no data to transfer, just return EFI_SUCCESS.
271 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
276 // Select the endpoint then issue the transfer
278 if (DataDir
== EfiUsbDataIn
) {
279 Endpoint
= UsbCbi
->BulkInEndpoint
;
281 Endpoint
= UsbCbi
->BulkOutEndpoint
;
287 Status
= EFI_SUCCESS
;
288 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
291 // Transfer the data with a loop. The length of data transferred once is restricted.
296 if (Remain
> (UINTN
) USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
) {
297 Increment
= USB_CBI_MAX_PACKET_NUM
* Endpoint
->MaxPacketSize
;
302 Status
= UsbCbi
->UsbIo
->UsbBulkTransfer (
304 Endpoint
->EndpointAddress
,
310 if (EFI_ERROR (Status
)) {
311 if (TransStatus
== EFI_USB_ERR_NAK
) {
313 // The device can NAK the host if either the data/buffer isn't
314 // aviable or the command is in-progress.
315 // If data are partially transferred, we just ignore NAK and continue.
316 // If all data have been transferred and status is NAK, then we retry for several times.
317 // If retry exceeds the USB_CBI_MAX_RETRY, then return error status.
319 if (Increment
== 0) {
320 if (++Retry
> USB_CBI_MAX_RETRY
) {
333 // The device can fail the command by STALL the bulk endpoint.
334 // Clear the stall if that is the case.
336 if (TransStatus
== EFI_USB_ERR_STALL
) {
337 UsbClearEndpointStall (UsbCbi
->UsbIo
, Endpoint
->EndpointAddress
);
354 Gets the result of high level command execution from interrupt endpoint.
356 This function returns the USB transfer status, and put the high level
357 command execution result in Result.
358 The CBI contains three phases: Command, Data, and Status. This is Status phase.
360 @param UsbCbi The USB CBI protocol
361 @param Timeout The time to wait for the command to execute
362 @param Result The result of the command execution.
364 @retval EFI_SUCCESS The high level command execution result is
366 @retval Others Failed to retrieve the result.
371 IN USB_CBI_PROTOCOL
*UsbCbi
,
373 OUT USB_CBI_STATUS
*Result
382 Endpoint
= UsbCbi
->InterruptEndpoint
->EndpointAddress
;
383 Status
= EFI_SUCCESS
;
384 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
387 // Attemp to the read the result from interrupt endpoint
389 for (Retry
= 0; Retry
< USB_CBI_MAX_RETRY
; Retry
++) {
391 Len
= sizeof (USB_CBI_STATUS
);
393 Status
= UsbCbi
->UsbIo
->UsbSyncInterruptTransfer (
402 // The CBI can NAK the interrupt endpoint if the command is in-progress.
404 if (EFI_ERROR (Status
) && (TransStatus
== EFI_USB_ERR_NAK
)) {
416 Execute USB mass storage command through the CBI0/CBI1 transport protocol.
418 @param Context The USB CBI Protocol.
419 @param Cmd The command to transfer to device
420 @param CmdLen The length of the command
421 @param DataDir The direction of data transfer
422 @param Data The buffer to hold the data
423 @param DataLen The length of the buffer
424 @param Lun Should be 0, this field for bot only
425 @param Timeout The time to wait
426 @param CmdStatus The result of the command execution
428 @retval EFI_SUCCESS The command is executed successfully.
429 @retval Other Failed to execute the command
437 IN EFI_USB_DATA_DIRECTION DataDir
,
442 OUT UINT32
*CmdStatus
445 USB_CBI_PROTOCOL
*UsbCbi
;
446 USB_CBI_STATUS Result
;
450 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
451 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
454 // Send the command to the device. Return immediately if device
455 // rejects the command.
457 Status
= UsbCbiSendCommand (UsbCbi
, Cmd
, CmdLen
, Timeout
);
458 if (EFI_ERROR (Status
)) {
459 gBS
->Stall(10 * USB_MASS_1_MILLISECOND
);
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 // Do not set the USB_MASS_CMD_FAIL for a request sense command
490 // as a bad result type doesn't mean a cmd failure
492 if (Result
.Type
!= 0 && *(UINT8
*)Cmd
!= 0x03) {
493 *CmdStatus
= USB_MASS_CMD_FAIL
;
497 // Check page 27, CBI spec 1.1 for vaious reture status.
499 switch (Result
.Value
& 0x03) {
504 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
509 // Phase Error, response with reset.
510 // No break here to fall through to "Fail".
512 UsbCbiResetDevice (UsbCbi
, FALSE
);
518 *CmdStatus
= USB_MASS_CMD_FAIL
;
523 // Persistent Fail. Need to send REQUEST SENSE.
525 *CmdStatus
= USB_MASS_CMD_PERSISTENT
;
535 Reset the USB mass storage device by CBI protocol.
537 This function resets the USB mass storage device by CBI protocol.
538 The reset is defined as a non-data command. Don't use UsbCbiExecCommand
539 to send the command to device because that may introduce recursive loop.
541 @param Context The USB CBI protocol
542 @param ExtendedVerification The flag controlling the rule of reset.
545 @retval EFI_SUCCESS The device is reset.
546 @retval Others Failed to reset the device.
552 IN BOOLEAN ExtendedVerification
555 UINT8 ResetCmd
[USB_CBI_RESET_CMD_LEN
];
556 USB_CBI_PROTOCOL
*UsbCbi
;
557 USB_CBI_STATUS Result
;
561 UsbCbi
= (USB_CBI_PROTOCOL
*) Context
;
564 // Fill in the reset command.
566 SetMem (ResetCmd
, USB_CBI_RESET_CMD_LEN
, 0xFF);
570 Timeout
= USB_CBI_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
573 // Send the command to the device. Don't use UsbCbiExecCommand here.
575 Status
= UsbCbiSendCommand (UsbCbi
, ResetCmd
, USB_CBI_RESET_CMD_LEN
, Timeout
);
576 if (EFI_ERROR (Status
)) {
577 return EFI_DEVICE_ERROR
;
581 // Just retrieve the status and ignore that. Then stall
582 // 50ms to wait for it to complete.
584 UsbCbiGetStatus (UsbCbi
, Timeout
, &Result
);
585 gBS
->Stall (USB_CBI_RESET_DEVICE_STALL
);
588 // Clear the Bulk-In and Bulk-Out stall condition and init data toggle.
590 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkInEndpoint
->EndpointAddress
);
591 UsbClearEndpointStall (UsbCbi
->UsbIo
, UsbCbi
->BulkOutEndpoint
->EndpointAddress
);
598 Clean up the CBI protocol's resource.
600 @param Context The instance of CBI protocol.
602 @retval EFI_SUCCESS The resource is cleaned up.