UsbMassBot.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
Implementation of the USB mass storage Bulk-Only Transport protocol,
according to USB Mass Storage Class Bulk-Only Transport, Revision 1.0.
Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "UsbMass.h"
//
// Definition of USB BOT Transport Protocol
//
};
/**
Initializes USB BOT protocol.
This function initializes the USB mass storage class BOT protocol.
It will save its context which is a USB_BOT_PROTOCOL structure
in the Context if Context isn't NULL.
@param UsbIo The USB I/O Protocol instance
@param Context The buffer to save the context to
@retval EFI_SUCCESS The device is successfully initialized.
@retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
@retval Other The USB BOT initialization fails.
**/
)
{
//
// Allocate the BOT context for USB_BOT_PROTOCOL and two endpoint descriptors.
//
//
// Get the interface descriptor and validate that it
// is a USB Mass Storage BOT interface.
//
goto ON_ERROR;
}
goto ON_ERROR;
}
//
// Locate and save the first bulk-in and bulk-out endpoint
//
continue;
}
}
}
}
//
// If bulk-in or bulk-out endpoint is not found, report error.
//
goto ON_ERROR;
}
//
// The USB BOT protocol uses CBWTag to match the CBW and CSW.
//
} else {
}
return EFI_SUCCESS;
return Status;
}
/**
Send the command to the device using Bulk-Out endpoint.
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.
@param UsbBot The USB BOT device
@param Cmd The command to transfer to device
@param CmdLen The length of the command
@param DataDir The direction of the data
@param TransLen The expected length of the data
@param Lun The number of logic unit
@retval EFI_SUCCESS The command is sent to the device.
@retval EFI_NOT_READY The device return NAK to the transfer
@retval Others Failed to send the command to device
**/
)
{
//
// Fill in the Command Block Wrapper.
//
Result = 0;
DataLen = sizeof (USB_BOT_CBW);
//
// Use USB I/O Protocol to send the Command Block Wrapper to the device.
//
&Cbw,
&DataLen,
);
//
// 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.
//
}
}
return Status;
}
/**
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.
@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
@retval EFI_SUCCESS No data to transfer
@retval EFI_NOT_READY The device return NAK to the transfer
@retval Others Failed to transfer data
**/
)
{
//
// If no data to transfer, just return EFI_SUCCESS.
//
return EFI_SUCCESS;
}
//
// Select the endpoint then issue the transfer
//
if (DataDir == EfiUsbDataIn) {
} else {
}
Result = 0;
Data,
);
} else {
}
if(Status == EFI_TIMEOUT){
}
}
return Status;
}
/**
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.
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.
@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.
**/
)
{
//
// Attemp to the read Command Status Wrapper from bulk in endpoint
//
Result = 0;
Len = sizeof (USB_BOT_CSW);
&Csw,
&Len,
);
}
continue;
}
//
// CSW is invalid, so perform reset recovery
//
//
// Respond phase error also needs reset recovery
//
} else {
break;
}
}
//
//The tag is increased even if there is an error.
//
return Status;
}
/**
Call the USB Mass Storage Class BOT protocol to issue
@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
@param Lun The number of logic unit
@param Timeout The time to wait command
@param CmdStatus The result of high level command execution
@retval EFI_SUCCESS The command is executed successfully.
@retval Other Failed to excute command
**/
)
{
//
// Send the command to the device. Return immediately if device
// rejects the command.
//
return Status;
}
//
// Transfer the data. Don't return immediately even data transfer
// failed. The host should attempt to receive the CSW no matter
// whether it succeeds or fails.
//
//
// Get the status, if that succeeds, interpret the result
//
return Status;
}
if (Result == 0) {
}
return EFI_SUCCESS;
}
/**
Reset the USB mass storage device by BOT protocol.
@param Context The context of the BOT protocol, that is,
USB_BOT_PROTOCOL.
@param ExtendedVerification If FALSE, just issue Bulk-Only Mass Storage Reset request.
If TRUE, additionally reset parent hub port.
@retval EFI_SUCCESS The device is reset.
@retval Others Failed to reset the device..
**/
)
{
if (ExtendedVerification) {
//
// If we need to do strictly reset, reset its parent hub port
//
return EFI_DEVICE_ERROR;
}
}
//
// 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.
//
&Request,
NULL,
0,
);
return EFI_DEVICE_ERROR;
}
//
// The device shall NAK the host's request until the reset is
// complete. We can use this to sync the device and host. For
// now just stall 100ms to wait for the device.
//
//
// Clear the Bulk-In and Bulk-Out stall condition.
//
return Status;
}
/**
Get the max LUN (Logical Unit Number) of USB mass storage device.
@param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL
@param MaxLun Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and
LUN1 in all.)
@retval EFI_SUCCESS Max LUN is got successfully.
@retval Others Fail to execute this request.
**/
)
{
//
// Issue a class specific Bulk-Only Mass Storage get max lun reqest.
// according to section 3.2 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
//
&Request,
1,
);
return Status;
}
/**
Clean up the resource used by this BOT protocol.
@param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL.
@retval EFI_SUCCESS The resource is cleaned up.
**/
)
{
return EFI_SUCCESS;
}