2007-07-11 10:47:37 +02:00
|
|
|
/** @file
|
2009-01-12 04:11:00 +01:00
|
|
|
Implementation of the USB mass storage Bulk-Only Transport protocol,
|
|
|
|
according to USB Mass Storage Class Bulk-Only Transport, Revision 1.0.
|
2008-07-23 10:30:25 +02:00
|
|
|
|
2008-06-27 10:09:00 +02:00
|
|
|
Copyright (c) 2007 - 2008, Intel Corporation
|
2007-07-11 10:47:37 +02:00
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
**/
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 08:30:42 +01:00
|
|
|
#include "UsbMassBoot.h"
|
2008-07-23 10:30:25 +02:00
|
|
|
#include "UsbMassBot.h"
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// Definition of USB BOT Transport Protocol
|
|
|
|
//
|
|
|
|
USB_MASS_TRANSPORT mUsbBotTransport = {
|
|
|
|
USB_MASS_STORE_BOT,
|
|
|
|
UsbBotInit,
|
|
|
|
UsbBotExecCommand,
|
|
|
|
UsbBotResetDevice,
|
|
|
|
UsbBotGetMaxLun,
|
|
|
|
UsbBotCleanUp
|
|
|
|
};
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Initializes USB BOT protocol.
|
|
|
|
|
|
|
|
This function initializes the USB mass storage class BOT protocol.
|
2007-07-11 10:47:37 +02:00
|
|
|
It will save its context which is a USB_BOT_PROTOCOL structure
|
|
|
|
in the Context if Context isn't NULL.
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbIo The USB I/O Protocol instance
|
|
|
|
@param Context The buffer to save the context to
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The device is successfully initialized.
|
2007-07-11 10:47:37 +02:00
|
|
|
@retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval Other The USB BOT initialization fails.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotInit (
|
2009-01-12 04:11:00 +01:00
|
|
|
IN EFI_USB_IO_PROTOCOL *UsbIo,
|
2007-07-11 10:47:37 +02:00
|
|
|
OUT VOID **Context OPTIONAL
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_PROTOCOL *UsbBot;
|
|
|
|
EFI_USB_INTERFACE_DESCRIPTOR *Interface;
|
|
|
|
EFI_USB_ENDPOINT_DESCRIPTOR EndPoint;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT8 Index;
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Allocate the BOT context for USB_BOT_PROTOCOL and two endpoint descriptors.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 08:30:42 +01:00
|
|
|
UsbBot = AllocateZeroPool (sizeof (USB_BOT_PROTOCOL) + 2 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));
|
2009-01-12 04:11:00 +01:00
|
|
|
ASSERT (UsbBot != NULL);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
UsbBot->UsbIo = UsbIo;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the interface descriptor and validate that it
|
2009-01-12 04:11:00 +01:00
|
|
|
// is a USB Mass Storage BOT interface.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbBot->Interface);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
goto ON_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
Interface = &UsbBot->Interface;
|
|
|
|
|
|
|
|
if (Interface->InterfaceProtocol != USB_MASS_STORE_BOT) {
|
|
|
|
Status = EFI_UNSUPPORTED;
|
|
|
|
goto ON_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Locate and save the first bulk-in and bulk-out endpoint
|
|
|
|
//
|
|
|
|
for (Index = 0; Index < Interface->NumEndpoints; Index++) {
|
|
|
|
Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &EndPoint);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status) || !USB_IS_BULK_ENDPOINT (EndPoint.Attributes)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (USB_IS_IN_ENDPOINT (EndPoint.EndpointAddress) &&
|
|
|
|
(UsbBot->BulkInEndpoint == NULL)) {
|
|
|
|
|
|
|
|
UsbBot->BulkInEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbBot + 1);
|
2007-08-27 05:33:51 +02:00
|
|
|
CopyMem(UsbBot->BulkInEndpoint, &EndPoint, sizeof (EndPoint));
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&
|
|
|
|
(UsbBot->BulkOutEndpoint == NULL)) {
|
|
|
|
|
|
|
|
UsbBot->BulkOutEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbBot + 1) + 1;
|
2009-01-12 08:30:42 +01:00
|
|
|
CopyMem (UsbBot->BulkOutEndpoint, &EndPoint, sizeof(EndPoint));
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// If bulk-in or bulk-out endpoint is not found, report error.
|
|
|
|
//
|
2007-07-11 10:47:37 +02:00
|
|
|
if ((UsbBot->BulkInEndpoint == NULL) || (UsbBot->BulkOutEndpoint == NULL)) {
|
|
|
|
Status = EFI_UNSUPPORTED;
|
|
|
|
goto ON_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// The USB BOT protocol uses CBWTag to match the CBW and CSW.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
UsbBot->CbwTag = 0x01;
|
|
|
|
|
|
|
|
if (Context != NULL) {
|
|
|
|
*Context = UsbBot;
|
|
|
|
} else {
|
2009-01-13 09:46:31 +01:00
|
|
|
FreePool (UsbBot);
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
|
|
|
ON_ERROR:
|
2009-01-13 09:46:31 +01:00
|
|
|
FreePool (UsbBot);
|
2007-07-11 10:47:37 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-23 10:30:25 +02:00
|
|
|
Send the command to the device using Bulk-Out endpoint.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
This function sends the command to the device using Bulk-Out endpoint.
|
|
|
|
BOT transfer is composed of three phases: Command, Data, and Status.
|
|
|
|
This is the Command phase.
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
@param UsbBot The USB BOT device
|
|
|
|
@param Cmd The command to transfer to device
|
2009-01-12 04:11:00 +01:00
|
|
|
@param CmdLen The length of the command
|
2007-07-11 10:47:37 +02:00
|
|
|
@param DataDir The direction of the data
|
|
|
|
@param TransLen The expected length of the data
|
2008-06-27 10:09:00 +02:00
|
|
|
@param Lun The number of logic unit
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The command is sent to the device.
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_NOT_READY The device return NAK to the transfer
|
2007-07-11 10:47:37 +02:00
|
|
|
@retval Others Failed to send the command to device
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotSendCommand (
|
|
|
|
IN USB_BOT_PROTOCOL *UsbBot,
|
|
|
|
IN UINT8 *Cmd,
|
|
|
|
IN UINT8 CmdLen,
|
|
|
|
IN EFI_USB_DATA_DIRECTION DataDir,
|
2008-06-27 10:09:00 +02:00
|
|
|
IN UINT32 TransLen,
|
|
|
|
IN UINT8 Lun
|
2007-07-11 10:47:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_CBW Cbw;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Result;
|
|
|
|
UINTN DataLen;
|
|
|
|
UINTN Timeout;
|
|
|
|
|
|
|
|
ASSERT ((CmdLen > 0) && (CmdLen <= USB_BOT_MAX_CMDLEN));
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Fill in the Command Block Wrapper.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Cbw.Signature = USB_BOT_CBW_SIGNATURE;
|
|
|
|
Cbw.Tag = UsbBot->CbwTag;
|
|
|
|
Cbw.DataLen = TransLen;
|
2009-01-12 04:11:00 +01:00
|
|
|
Cbw.Flag = (UINT8) ((DataDir == EfiUsbDataIn) ? BIT7 : 0);
|
2008-06-27 10:09:00 +02:00
|
|
|
Cbw.Lun = Lun;
|
2007-07-11 10:47:37 +02:00
|
|
|
Cbw.CmdLen = CmdLen;
|
|
|
|
|
|
|
|
ZeroMem (Cbw.CmdBlock, USB_BOT_MAX_CMDLEN);
|
|
|
|
CopyMem (Cbw.CmdBlock, Cmd, CmdLen);
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
Result = 0;
|
|
|
|
DataLen = sizeof (USB_BOT_CBW);
|
|
|
|
Timeout = USB_BOT_SEND_CBW_TIMEOUT / USB_MASS_1_MILLISECOND;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Use USB I/O Protocol to send the Command Block Wrapper to the device.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Status = UsbBot->UsbIo->UsbBulkTransfer (
|
|
|
|
UsbBot->UsbIo,
|
|
|
|
UsbBot->BulkOutEndpoint->EndpointAddress,
|
|
|
|
&Cbw,
|
|
|
|
&DataLen,
|
|
|
|
Timeout,
|
|
|
|
&Result
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL) && DataDir == EfiUsbDataOut) {
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// Respond to Bulk-Out endpoint stall with a Reset Recovery,
|
|
|
|
// according to section 5.3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
|
|
|
|
//
|
2007-07-11 10:47:37 +02:00
|
|
|
UsbBotResetDevice (UsbBot, FALSE);
|
|
|
|
} else if (USB_IS_ERROR (Result, EFI_USB_ERR_NAK)) {
|
|
|
|
Status = EFI_NOT_READY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Transfer the data between the device and host.
|
|
|
|
|
|
|
|
This function transfers the data between the device and host.
|
|
|
|
BOT transfer is composed of three phases: Command, Data, and Status.
|
|
|
|
This is the Data phase.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@param UsbBot The USB BOT device
|
|
|
|
@param DataDir The direction of the data
|
|
|
|
@param Data The buffer to hold data
|
|
|
|
@param TransLen The expected length of the data
|
|
|
|
@param Timeout The time to wait the command to complete
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The data is transferred
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS No data to transfer
|
|
|
|
@retval EFI_NOT_READY The device return NAK to the transfer
|
2007-07-11 10:47:37 +02:00
|
|
|
@retval Others Failed to transfer data
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotDataTransfer (
|
|
|
|
IN USB_BOT_PROTOCOL *UsbBot,
|
|
|
|
IN EFI_USB_DATA_DIRECTION DataDir,
|
|
|
|
IN OUT UINT8 *Data,
|
|
|
|
IN OUT UINTN *TransLen,
|
|
|
|
IN UINT32 Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Result;
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// If no data to transfer, just return EFI_SUCCESS.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Select the endpoint then issue the transfer
|
|
|
|
//
|
|
|
|
if (DataDir == EfiUsbDataIn) {
|
|
|
|
Endpoint = UsbBot->BulkInEndpoint;
|
|
|
|
} else {
|
|
|
|
Endpoint = UsbBot->BulkOutEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = 0;
|
2007-10-08 08:14:13 +02:00
|
|
|
Timeout = Timeout / USB_MASS_1_MILLISECOND;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBot->UsbIo->UsbBulkTransfer (
|
|
|
|
UsbBot->UsbIo,
|
|
|
|
Endpoint->EndpointAddress,
|
|
|
|
Data,
|
|
|
|
TransLen,
|
|
|
|
Timeout,
|
|
|
|
&Result
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL)) {
|
2009-12-16 01:58:46 +01:00
|
|
|
DEBUG ((EFI_D_INFO, "UsbBotDataTransfer: (%r)\n", Status));
|
|
|
|
DEBUG ((EFI_D_INFO, "UsbBotDataTransfer: DataIn Stall\n"));
|
2007-07-11 10:47:37 +02:00
|
|
|
UsbClearEndpointStall (UsbBot->UsbIo, Endpoint->EndpointAddress);
|
|
|
|
} else if (USB_IS_ERROR (Result, EFI_USB_ERR_NAK)) {
|
|
|
|
Status = EFI_NOT_READY;
|
2009-12-16 01:58:46 +01:00
|
|
|
} else {
|
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBotDataTransfer: (%r)\n", Status));
|
|
|
|
}
|
|
|
|
if(Status == EFI_TIMEOUT){
|
|
|
|
UsbBotResetDevice(UsbBot, FALSE);
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Get the command execution status from device.
|
|
|
|
|
|
|
|
This function gets the command execution status from device.
|
|
|
|
BOT transfer is composed of three phases: Command, Data, and Status.
|
|
|
|
This is the Status phase.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
This function returns the transfer status of the BOT's CSW status,
|
|
|
|
and returns the high level command execution result in Result. So
|
|
|
|
even if EFI_SUCCESS is returned, the command may still have failed.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbBot The USB BOT device.
|
|
|
|
@param TransLen The expected length of the data.
|
|
|
|
@param CmdStatus The result of the command execution.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Command execute result is retrieved and in the Result.
|
|
|
|
@retval Other Error occurred when trying to get status.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotGetStatus (
|
|
|
|
IN USB_BOT_PROTOCOL *UsbBot,
|
|
|
|
IN UINT32 TransLen,
|
|
|
|
OUT UINT8 *CmdStatus
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_CSW Csw;
|
|
|
|
UINTN Len;
|
|
|
|
UINT8 Endpoint;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Result;
|
|
|
|
EFI_USB_IO_PROTOCOL *UsbIo;
|
|
|
|
UINT32 Index;
|
|
|
|
UINTN Timeout;
|
2008-02-13 10:08:24 +01:00
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
*CmdStatus = USB_BOT_COMMAND_ERROR;
|
|
|
|
Status = EFI_DEVICE_ERROR;
|
|
|
|
Endpoint = UsbBot->BulkInEndpoint->EndpointAddress;
|
|
|
|
UsbIo = UsbBot->UsbIo;
|
2007-10-08 08:14:13 +02:00
|
|
|
Timeout = USB_BOT_RECV_CSW_TIMEOUT / USB_MASS_1_MILLISECOND;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2007-10-08 08:14:13 +02:00
|
|
|
for (Index = 0; Index < USB_BOT_RECV_CSW_RETRY; Index++) {
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Attemp to the read Command Status Wrapper from bulk in endpoint
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
ZeroMem (&Csw, sizeof (USB_BOT_CSW));
|
|
|
|
Result = 0;
|
|
|
|
Len = sizeof (USB_BOT_CSW);
|
|
|
|
Status = UsbIo->UsbBulkTransfer (
|
|
|
|
UsbIo,
|
|
|
|
Endpoint,
|
|
|
|
&Csw,
|
|
|
|
&Len,
|
|
|
|
Timeout,
|
|
|
|
&Result
|
|
|
|
);
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
if (USB_IS_ERROR (Result, EFI_USB_ERR_STALL)) {
|
|
|
|
UsbClearEndpointStall (UsbIo, Endpoint);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Csw.Signature != USB_BOT_CSW_SIGNATURE) {
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// CSW is invalid, so perform reset recovery
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Status = UsbBotResetDevice (UsbBot, FALSE);
|
|
|
|
} else if (Csw.CmdStatus == USB_BOT_COMMAND_ERROR) {
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Respond phase error also needs reset recovery
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Status = UsbBotResetDevice (UsbBot, FALSE);
|
|
|
|
} else {
|
|
|
|
*CmdStatus = Csw.CmdStatus;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
//The tag is increased even if there is an error.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
UsbBot->CbwTag++;
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Call the USB Mass Storage Class BOT protocol to issue
|
2008-07-23 10:30:25 +02:00
|
|
|
the command/data/status circle to execute the commands.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@param Context The context of the BOT protocol, that is,
|
|
|
|
USB_BOT_PROTOCOL
|
|
|
|
@param Cmd The high level command
|
|
|
|
@param CmdLen The command length
|
|
|
|
@param DataDir The direction of the data transfer
|
|
|
|
@param Data The buffer to hold data
|
|
|
|
@param DataLen The length of the data
|
2008-06-27 10:09:00 +02:00
|
|
|
@param Lun The number of logic unit
|
2007-07-11 10:47:37 +02:00
|
|
|
@param Timeout The time to wait command
|
|
|
|
@param CmdStatus The result of high level command execution
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The command is executed successfully.
|
2008-07-23 10:30:25 +02:00
|
|
|
@retval Other Failed to excute command
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotExecCommand (
|
|
|
|
IN VOID *Context,
|
|
|
|
IN VOID *Cmd,
|
|
|
|
IN UINT8 CmdLen,
|
|
|
|
IN EFI_USB_DATA_DIRECTION DataDir,
|
|
|
|
IN VOID *Data,
|
|
|
|
IN UINT32 DataLen,
|
2008-06-27 10:09:00 +02:00
|
|
|
IN UINT8 Lun,
|
2007-07-11 10:47:37 +02:00
|
|
|
IN UINT32 Timeout,
|
|
|
|
OUT UINT32 *CmdStatus
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_PROTOCOL *UsbBot;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINTN TransLen;
|
|
|
|
UINT8 Result;
|
|
|
|
|
|
|
|
*CmdStatus = USB_MASS_CMD_FAIL;
|
|
|
|
UsbBot = (USB_BOT_PROTOCOL *) Context;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Send the command to the device. Return immediately if device
|
|
|
|
// rejects the command.
|
|
|
|
//
|
2008-06-27 10:09:00 +02:00
|
|
|
Status = UsbBotSendCommand (UsbBot, Cmd, CmdLen, DataDir, DataLen, Lun);
|
2007-07-11 10:47:37 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBotExecCommand: UsbBotSendCommand (%r)\n", Status));
|
2007-07-11 10:47:37 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Transfer the data. Don't return immediately even data transfer
|
|
|
|
// failed. The host should attempt to receive the CSW no matter
|
2009-01-12 04:11:00 +01:00
|
|
|
// whether it succeeds or fails.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
TransLen = (UINTN) DataLen;
|
|
|
|
UsbBotDataTransfer (UsbBot, DataDir, Data, &TransLen, Timeout);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the status, if that succeeds, interpret the result
|
|
|
|
//
|
|
|
|
Status = UsbBotGetStatus (UsbBot, DataLen, &Result);
|
|
|
|
if (EFI_ERROR (Status)) {
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBotExecCommand: UsbBotGetStatus (%r)\n", Status));
|
2007-07-11 10:47:37 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Result == 0) {
|
|
|
|
*CmdStatus = USB_MASS_CMD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Reset the USB mass storage device by BOT protocol.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@param Context The context of the BOT protocol, that is,
|
2008-07-23 10:30:25 +02:00
|
|
|
USB_BOT_PROTOCOL.
|
2009-01-12 04:11:00 +01:00
|
|
|
@param ExtendedVerification If FALSE, just issue Bulk-Only Mass Storage Reset request.
|
|
|
|
If TRUE, additionally reset parent hub port.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
@retval EFI_SUCCESS The device is reset.
|
|
|
|
@retval Others Failed to reset the device..
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBotResetDevice (
|
|
|
|
IN VOID *Context,
|
2009-01-12 04:11:00 +01:00
|
|
|
IN BOOLEAN ExtendedVerification
|
2007-07-11 10:47:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_PROTOCOL *UsbBot;
|
|
|
|
EFI_USB_DEVICE_REQUEST Request;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Result;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
UsbBot = (USB_BOT_PROTOCOL *) Context;
|
|
|
|
|
|
|
|
if (ExtendedVerification) {
|
|
|
|
//
|
|
|
|
// If we need to do strictly reset, reset its parent hub port
|
|
|
|
//
|
|
|
|
Status = UsbBot->UsbIo->UsbPortReset (UsbBot->UsbIo);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Issue a class specific Bulk-Only Mass Storage Reset request,
|
|
|
|
// according to section 3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
Request.RequestType = 0x21;
|
2007-07-11 10:47:37 +02:00
|
|
|
Request.Request = USB_BOT_RESET_REQUEST;
|
|
|
|
Request.Value = 0;
|
|
|
|
Request.Index = UsbBot->Interface.InterfaceNumber;
|
|
|
|
Request.Length = 0;
|
2007-10-08 08:14:13 +02:00
|
|
|
Timeout = USB_BOT_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBot->UsbIo->UsbControlTransfer (
|
|
|
|
UsbBot->UsbIo,
|
|
|
|
&Request,
|
|
|
|
EfiUsbNoData,
|
|
|
|
Timeout,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&Result
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// The device shall NAK the host's request until the reset is
|
|
|
|
// complete. We can use this to sync the device and host. For
|
2009-01-12 04:11:00 +01:00
|
|
|
// now just stall 100ms to wait for the device.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-10-08 08:14:13 +02:00
|
|
|
gBS->Stall (USB_BOT_RESET_DEVICE_STALL);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Clear the Bulk-In and Bulk-Out stall condition.
|
|
|
|
//
|
|
|
|
UsbClearEndpointStall (UsbBot->UsbIo, UsbBot->BulkInEndpoint->EndpointAddress);
|
|
|
|
UsbClearEndpointStall (UsbBot->UsbIo, UsbBot->BulkOutEndpoint->EndpointAddress);
|
2009-01-12 04:11:00 +01:00
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2008-06-27 10:09:00 +02:00
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Get the max LUN (Logical Unit Number) of USB mass storage device.
|
2008-06-27 10:09:00 +02:00
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
@param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL
|
2009-01-12 04:11:00 +01:00
|
|
|
@param MaxLun Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and
|
|
|
|
LUN1 in all.)
|
2008-06-27 10:09:00 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS Max LUN is got successfully.
|
|
|
|
@retval Others Fail to execute this request.
|
2008-06-27 10:09:00 +02:00
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
**/
|
2008-06-27 10:09:00 +02:00
|
|
|
EFI_STATUS
|
|
|
|
UsbBotGetMaxLun (
|
|
|
|
IN VOID *Context,
|
2009-01-12 04:11:00 +01:00
|
|
|
OUT UINT8 *MaxLun
|
2008-06-27 10:09:00 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOT_PROTOCOL *UsbBot;
|
|
|
|
EFI_USB_DEVICE_REQUEST Request;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 Result;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
ASSERT (Context);
|
|
|
|
|
|
|
|
UsbBot = (USB_BOT_PROTOCOL *) Context;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Issue a class specific Bulk-Only Mass Storage get max lun reqest.
|
2009-01-12 04:11:00 +01:00
|
|
|
// according to section 3.2 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
|
2008-06-27 10:09:00 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
Request.RequestType = 0xA1;
|
2008-06-27 10:09:00 +02:00
|
|
|
Request.Request = USB_BOT_GETLUN_REQUEST;
|
|
|
|
Request.Value = 0;
|
|
|
|
Request.Index = UsbBot->Interface.InterfaceNumber;
|
|
|
|
Request.Length = 1;
|
|
|
|
Timeout = USB_BOT_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;
|
|
|
|
|
|
|
|
Status = UsbBot->UsbIo->UsbControlTransfer (
|
|
|
|
UsbBot->UsbIo,
|
|
|
|
&Request,
|
|
|
|
EfiUsbDataIn,
|
|
|
|
Timeout,
|
2009-01-12 04:11:00 +01:00
|
|
|
(VOID *) MaxLun,
|
2008-06-27 10:09:00 +02:00
|
|
|
1,
|
|
|
|
&Result
|
|
|
|
);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2008-07-23 10:30:25 +02:00
|
|
|
Clean up the resource used by this BOT protocol.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The resource is cleaned up.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
2009-01-12 04:11:00 +01:00
|
|
|
UsbBotCleanUp (
|
2007-07-11 10:47:37 +02:00
|
|
|
IN VOID *Context
|
|
|
|
)
|
|
|
|
{
|
2009-01-13 09:46:31 +01:00
|
|
|
FreePool (Context);
|
2007-07-11 10:47:37 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|