2 Implementation of the USB mass storage Bulk-Only Transport protocol,
3 according to USB Mass Storage Class Bulk-Only Transport, Revision 1.0.
5 Copyright (c) 2007 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "UsbMassBot.h"
20 // Definition of USB BOT Transport Protocol
22 USB_MASS_TRANSPORT mUsbBotTransport
= {
32 Initializes USB BOT protocol.
34 This function initializes the USB mass storage class BOT protocol.
35 It will save its context which is a USB_BOT_PROTOCOL structure
36 in the Context if Context isn't NULL.
38 @param UsbIo The USB I/O Protocol instance
39 @param Context The buffer to save the context to
41 @retval EFI_SUCCESS The device is successfully initialized.
42 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
43 @retval Other The USB BOT initialization fails.
48 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
49 OUT VOID
**Context OPTIONAL
52 USB_BOT_PROTOCOL
*UsbBot
;
53 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
54 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
59 // Allocate the BOT context for USB_BOT_PROTOCOL and two endpoint descriptors.
61 UsbBot
= AllocateZeroPool (
62 sizeof (USB_BOT_PROTOCOL
) + 2 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
64 ASSERT (UsbBot
!= NULL
);
66 UsbBot
->UsbIo
= UsbIo
;
69 // Get the interface descriptor and validate that it
70 // is a USB Mass Storage BOT interface.
72 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbBot
->Interface
);
74 if (EFI_ERROR (Status
)) {
78 Interface
= &UsbBot
->Interface
;
80 if (Interface
->InterfaceProtocol
!= USB_MASS_STORE_BOT
) {
81 Status
= EFI_UNSUPPORTED
;
86 // Locate and save the first bulk-in and bulk-out endpoint
88 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
89 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
91 if (EFI_ERROR (Status
) || !USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
95 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
96 (UsbBot
->BulkInEndpoint
== NULL
)) {
98 UsbBot
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbBot
+ 1);
99 CopyMem(UsbBot
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));
102 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
103 (UsbBot
->BulkOutEndpoint
== NULL
)) {
105 UsbBot
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*) (UsbBot
+ 1) + 1;
106 CopyMem(UsbBot
->BulkOutEndpoint
, &EndPoint
, sizeof(EndPoint
));
111 // If bulk-in or bulk-out endpoint is not found, report error.
113 if ((UsbBot
->BulkInEndpoint
== NULL
) || (UsbBot
->BulkOutEndpoint
== NULL
)) {
114 Status
= EFI_UNSUPPORTED
;
119 // The USB BOT protocol uses CBWTag to match the CBW and CSW.
121 UsbBot
->CbwTag
= 0x01;
123 if (Context
!= NULL
) {
126 gBS
->FreePool (UsbBot
);
132 gBS
->FreePool (UsbBot
);
137 Send the command to the device using Bulk-Out endpoint.
139 This function sends the command to the device using Bulk-Out endpoint.
140 BOT transfer is composed of three phases: Command, Data, and Status.
141 This is the Command phase.
143 @param UsbBot The USB BOT device
144 @param Cmd The command to transfer to device
145 @param CmdLen The length of the command
146 @param DataDir The direction of the data
147 @param TransLen The expected length of the data
148 @param Lun The number of logic unit
150 @retval EFI_SUCCESS The command is sent to the device.
151 @retval EFI_NOT_READY The device return NAK to the transfer
152 @retval Others Failed to send the command to device
157 IN USB_BOT_PROTOCOL
*UsbBot
,
160 IN EFI_USB_DATA_DIRECTION DataDir
,
171 ASSERT ((CmdLen
> 0) && (CmdLen
<= USB_BOT_MAX_CMDLEN
));
174 // Fill in the Command Block Wrapper.
176 Cbw
.Signature
= USB_BOT_CBW_SIGNATURE
;
177 Cbw
.Tag
= UsbBot
->CbwTag
;
178 Cbw
.DataLen
= TransLen
;
179 Cbw
.Flag
= (UINT8
) ((DataDir
== EfiUsbDataIn
) ? BIT7
: 0);
183 ZeroMem (Cbw
.CmdBlock
, USB_BOT_MAX_CMDLEN
);
184 CopyMem (Cbw
.CmdBlock
, Cmd
, CmdLen
);
187 DataLen
= sizeof (USB_BOT_CBW
);
188 Timeout
= USB_BOT_SEND_CBW_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
191 // Use USB I/O Protocol to send the Command Block Wrapper to the device.
193 Status
= UsbBot
->UsbIo
->UsbBulkTransfer (
195 UsbBot
->BulkOutEndpoint
->EndpointAddress
,
201 if (EFI_ERROR (Status
)) {
202 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
) && DataDir
== EfiUsbDataOut
) {
204 // Respond to Bulk-Out endpoint stall with a Reset Recovery,
205 // according to section 5.3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
207 UsbBotResetDevice (UsbBot
, FALSE
);
208 } else if (USB_IS_ERROR (Result
, EFI_USB_ERR_NAK
)) {
209 Status
= EFI_NOT_READY
;
218 Transfer the data between the device and host.
220 This function transfers the data between the device and host.
221 BOT transfer is composed of three phases: Command, Data, and Status.
222 This is the Data phase.
224 @param UsbBot The USB BOT device
225 @param DataDir The direction of the data
226 @param Data The buffer to hold data
227 @param TransLen The expected length of the data
228 @param Timeout The time to wait the command to complete
230 @retval EFI_SUCCESS The data is transferred
231 @retval EFI_SUCCESS No data to transfer
232 @retval EFI_NOT_READY The device return NAK to the transfer
233 @retval Others Failed to transfer data
238 IN USB_BOT_PROTOCOL
*UsbBot
,
239 IN EFI_USB_DATA_DIRECTION DataDir
,
241 IN OUT UINTN
*TransLen
,
245 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
250 // If no data to transfer, just return EFI_SUCCESS.
252 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
257 // Select the endpoint then issue the transfer
259 if (DataDir
== EfiUsbDataIn
) {
260 Endpoint
= UsbBot
->BulkInEndpoint
;
262 Endpoint
= UsbBot
->BulkOutEndpoint
;
266 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
268 Status
= UsbBot
->UsbIo
->UsbBulkTransfer (
270 Endpoint
->EndpointAddress
,
276 if (EFI_ERROR (Status
)) {
277 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
)) {
278 UsbClearEndpointStall (UsbBot
->UsbIo
, Endpoint
->EndpointAddress
);
279 } else if (USB_IS_ERROR (Result
, EFI_USB_ERR_NAK
)) {
280 Status
= EFI_NOT_READY
;
289 Get the command execution status from device.
291 This function gets the command execution status from device.
292 BOT transfer is composed of three phases: Command, Data, and Status.
293 This is the Status phase.
295 This function returns the transfer status of the BOT's CSW status,
296 and returns the high level command execution result in Result. So
297 even if EFI_SUCCESS is returned, the command may still have failed.
299 @param UsbBot The USB BOT device.
300 @param TransLen The expected length of the data.
301 @param CmdStatus The result of the command execution.
303 @retval EFI_SUCCESS Command execute result is retrieved and in the Result.
304 @retval Other Error occurred when trying to get status.
309 IN USB_BOT_PROTOCOL
*UsbBot
,
319 EFI_USB_IO_PROTOCOL
*UsbIo
;
323 *CmdStatus
= USB_BOT_COMMAND_ERROR
;
324 Status
= EFI_DEVICE_ERROR
;
325 Endpoint
= UsbBot
->BulkInEndpoint
->EndpointAddress
;
326 UsbIo
= UsbBot
->UsbIo
;
327 Timeout
= USB_BOT_RECV_CSW_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
329 for (Index
= 0; Index
< USB_BOT_RECV_CSW_RETRY
; Index
++) {
331 // Attemp to the read Command Status Wrapper from bulk in endpoint
333 ZeroMem (&Csw
, sizeof (USB_BOT_CSW
));
335 Len
= sizeof (USB_BOT_CSW
);
336 Status
= UsbIo
->UsbBulkTransfer (
344 if (EFI_ERROR(Status
)) {
345 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
)) {
346 UsbClearEndpointStall (UsbIo
, Endpoint
);
351 if (Csw
.Signature
!= USB_BOT_CSW_SIGNATURE
) {
353 // CSW is invalid, so perform reset recovery
355 Status
= UsbBotResetDevice (UsbBot
, FALSE
);
356 } else if (Csw
.CmdStatus
== USB_BOT_COMMAND_ERROR
) {
358 // Respond phase error also needs reset recovery
360 Status
= UsbBotResetDevice (UsbBot
, FALSE
);
362 *CmdStatus
= Csw
.CmdStatus
;
367 //The tag is increased even if there is an error.
376 Call the USB Mass Storage Class BOT protocol to issue
377 the command/data/status circle to execute the commands.
379 @param Context The context of the BOT protocol, that is,
381 @param Cmd The high level command
382 @param CmdLen The command length
383 @param DataDir The direction of the data transfer
384 @param Data The buffer to hold data
385 @param DataLen The length of the data
386 @param Lun The number of logic unit
387 @param Timeout The time to wait command
388 @param CmdStatus The result of high level command execution
390 @retval EFI_SUCCESS The command is executed successfully.
391 @retval Other Failed to excute command
399 IN EFI_USB_DATA_DIRECTION DataDir
,
404 OUT UINT32
*CmdStatus
407 USB_BOT_PROTOCOL
*UsbBot
;
412 *CmdStatus
= USB_MASS_CMD_FAIL
;
413 UsbBot
= (USB_BOT_PROTOCOL
*) Context
;
416 // Send the command to the device. Return immediately if device
417 // rejects the command.
419 Status
= UsbBotSendCommand (UsbBot
, Cmd
, CmdLen
, DataDir
, DataLen
, Lun
);
420 if (EFI_ERROR (Status
)) {
421 DEBUG ((EFI_D_ERROR
, "UsbBotExecCommand: UsbBotSendCommand (%r)\n", Status
));
426 // Transfer the data. Don't return immediately even data transfer
427 // failed. The host should attempt to receive the CSW no matter
428 // whether it succeeds or fails.
430 TransLen
= (UINTN
) DataLen
;
431 UsbBotDataTransfer (UsbBot
, DataDir
, Data
, &TransLen
, Timeout
);
434 // Get the status, if that succeeds, interpret the result
436 Status
= UsbBotGetStatus (UsbBot
, DataLen
, &Result
);
437 if (EFI_ERROR (Status
)) {
438 DEBUG ((EFI_D_ERROR
, "UsbBotExecCommand: UsbBotGetStatus (%r)\n", Status
));
443 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
451 Reset the USB mass storage device by BOT protocol.
453 @param Context The context of the BOT protocol, that is,
455 @param ExtendedVerification If FALSE, just issue Bulk-Only Mass Storage Reset request.
456 If TRUE, additionally reset parent hub port.
458 @retval EFI_SUCCESS The device is reset.
459 @retval Others Failed to reset the device..
465 IN BOOLEAN ExtendedVerification
468 USB_BOT_PROTOCOL
*UsbBot
;
469 EFI_USB_DEVICE_REQUEST Request
;
474 UsbBot
= (USB_BOT_PROTOCOL
*) Context
;
476 if (ExtendedVerification
) {
478 // If we need to do strictly reset, reset its parent hub port
480 Status
= UsbBot
->UsbIo
->UsbPortReset (UsbBot
->UsbIo
);
481 if (EFI_ERROR (Status
)) {
487 // Issue a class specific Bulk-Only Mass Storage Reset request,
488 // according to section 3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
490 Request
.RequestType
= 0x21;
491 Request
.Request
= USB_BOT_RESET_REQUEST
;
493 Request
.Index
= UsbBot
->Interface
.InterfaceNumber
;
495 Timeout
= USB_BOT_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
497 Status
= UsbBot
->UsbIo
->UsbControlTransfer (
507 if (EFI_ERROR (Status
)) {
512 // The device shall NAK the host's request until the reset is
513 // complete. We can use this to sync the device and host. For
514 // now just stall 100ms to wait for the device.
516 gBS
->Stall (USB_BOT_RESET_DEVICE_STALL
);
519 // Clear the Bulk-In and Bulk-Out stall condition.
521 UsbClearEndpointStall (UsbBot
->UsbIo
, UsbBot
->BulkInEndpoint
->EndpointAddress
);
522 UsbClearEndpointStall (UsbBot
->UsbIo
, UsbBot
->BulkOutEndpoint
->EndpointAddress
);
529 Get the max LUN (Logical Unit Number) of USB mass storage device.
531 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL
532 @param MaxLun Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and
535 @retval EFI_SUCCESS Max LUN is got successfully.
536 @retval Others Fail to execute this request.
545 USB_BOT_PROTOCOL
*UsbBot
;
546 EFI_USB_DEVICE_REQUEST Request
;
553 UsbBot
= (USB_BOT_PROTOCOL
*) Context
;
556 // Issue a class specific Bulk-Only Mass Storage get max lun reqest.
557 // according to section 3.2 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
559 Request
.RequestType
= 0xA1;
560 Request
.Request
= USB_BOT_GETLUN_REQUEST
;
562 Request
.Index
= UsbBot
->Interface
.InterfaceNumber
;
564 Timeout
= USB_BOT_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
566 Status
= UsbBot
->UsbIo
->UsbControlTransfer (
580 Clean up the resource used by this BOT protocol.
582 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL.
584 @retval EFI_SUCCESS The resource is cleaned up.
592 gBS
->FreePool (Context
);