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 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 // Definition of USB BOT Transport Protocol
15 USB_MASS_TRANSPORT mUsbBotTransport
= {
25 Initializes USB BOT protocol.
27 This function initializes the USB mass storage class BOT protocol.
28 It will save its context which is a USB_BOT_PROTOCOL structure
29 in the Context if Context isn't NULL.
31 @param UsbIo The USB I/O Protocol instance
32 @param Context The buffer to save the context to
34 @retval EFI_SUCCESS The device is successfully initialized.
35 @retval EFI_UNSUPPORTED The transport protocol doesn't support the device.
36 @retval Other The USB BOT initialization fails.
41 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
42 OUT VOID
**Context OPTIONAL
45 USB_BOT_PROTOCOL
*UsbBot
;
46 EFI_USB_INTERFACE_DESCRIPTOR
*Interface
;
47 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint
;
52 // Allocate the BOT context for USB_BOT_PROTOCOL and two endpoint descriptors.
54 UsbBot
= AllocateZeroPool (sizeof (USB_BOT_PROTOCOL
) + 2 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
));
55 ASSERT (UsbBot
!= NULL
);
57 UsbBot
->UsbIo
= UsbIo
;
60 // Get the interface descriptor and validate that it
61 // is a USB Mass Storage BOT interface.
63 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &UsbBot
->Interface
);
65 if (EFI_ERROR (Status
)) {
69 Interface
= &UsbBot
->Interface
;
71 if (Interface
->InterfaceProtocol
!= USB_MASS_STORE_BOT
) {
72 Status
= EFI_UNSUPPORTED
;
77 // Locate and save the first bulk-in and bulk-out endpoint
79 for (Index
= 0; Index
< Interface
->NumEndpoints
; Index
++) {
80 Status
= UsbIo
->UsbGetEndpointDescriptor (UsbIo
, Index
, &EndPoint
);
82 if (EFI_ERROR (Status
) || !USB_IS_BULK_ENDPOINT (EndPoint
.Attributes
)) {
86 if (USB_IS_IN_ENDPOINT (EndPoint
.EndpointAddress
) &&
87 (UsbBot
->BulkInEndpoint
== NULL
))
89 UsbBot
->BulkInEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*)(UsbBot
+ 1);
90 CopyMem (UsbBot
->BulkInEndpoint
, &EndPoint
, sizeof (EndPoint
));
93 if (USB_IS_OUT_ENDPOINT (EndPoint
.EndpointAddress
) &&
94 (UsbBot
->BulkOutEndpoint
== NULL
))
96 UsbBot
->BulkOutEndpoint
= (EFI_USB_ENDPOINT_DESCRIPTOR
*)(UsbBot
+ 1) + 1;
97 CopyMem (UsbBot
->BulkOutEndpoint
, &EndPoint
, sizeof (EndPoint
));
102 // If bulk-in or bulk-out endpoint is not found, report error.
104 if ((UsbBot
->BulkInEndpoint
== NULL
) || (UsbBot
->BulkOutEndpoint
== NULL
)) {
105 Status
= EFI_UNSUPPORTED
;
110 // The USB BOT protocol uses CBWTag to match the CBW and CSW.
112 UsbBot
->CbwTag
= 0x01;
114 if (Context
!= NULL
) {
128 Send the command to the device using Bulk-Out endpoint.
130 This function sends the command to the device using Bulk-Out endpoint.
131 BOT transfer is composed of three phases: Command, Data, and Status.
132 This is the Command phase.
134 @param UsbBot The USB BOT device
135 @param Cmd The command to transfer to device
136 @param CmdLen The length of the command
137 @param DataDir The direction of the data
138 @param TransLen The expected length of the data
139 @param Lun The number of logic unit
141 @retval EFI_SUCCESS The command is sent to the device.
142 @retval EFI_NOT_READY The device return NAK to the transfer
143 @retval Others Failed to send the command to device
148 IN USB_BOT_PROTOCOL
*UsbBot
,
151 IN EFI_USB_DATA_DIRECTION DataDir
,
162 ASSERT ((CmdLen
> 0) && (CmdLen
<= USB_BOT_MAX_CMDLEN
));
165 // Fill in the Command Block Wrapper.
167 Cbw
.Signature
= USB_BOT_CBW_SIGNATURE
;
168 Cbw
.Tag
= UsbBot
->CbwTag
;
169 Cbw
.DataLen
= TransLen
;
170 Cbw
.Flag
= (UINT8
)((DataDir
== EfiUsbDataIn
) ? BIT7
: 0);
174 ZeroMem (Cbw
.CmdBlock
, USB_BOT_MAX_CMDLEN
);
175 CopyMem (Cbw
.CmdBlock
, Cmd
, CmdLen
);
178 DataLen
= sizeof (USB_BOT_CBW
);
179 Timeout
= USB_BOT_SEND_CBW_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
182 // Use USB I/O Protocol to send the Command Block Wrapper to the device.
184 Status
= UsbBot
->UsbIo
->UsbBulkTransfer (
186 UsbBot
->BulkOutEndpoint
->EndpointAddress
,
192 if (EFI_ERROR (Status
)) {
193 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
) && (DataDir
== EfiUsbDataOut
)) {
195 // Respond to Bulk-Out endpoint stall with a Reset Recovery,
196 // according to section 5.3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
198 UsbBotResetDevice (UsbBot
, FALSE
);
199 } else if (USB_IS_ERROR (Result
, EFI_USB_ERR_NAK
)) {
200 Status
= EFI_NOT_READY
;
208 Transfer the data between the device and host.
210 This function transfers the data between the device and host.
211 BOT transfer is composed of three phases: Command, Data, and Status.
212 This is the Data phase.
214 @param UsbBot The USB BOT device
215 @param DataDir The direction of the data
216 @param Data The buffer to hold data
217 @param TransLen The expected length of the data
218 @param Timeout The time to wait the command to complete
220 @retval EFI_SUCCESS The data is transferred
221 @retval EFI_SUCCESS No data to transfer
222 @retval EFI_NOT_READY The device return NAK to the transfer
223 @retval Others Failed to transfer data
228 IN USB_BOT_PROTOCOL
*UsbBot
,
229 IN EFI_USB_DATA_DIRECTION DataDir
,
231 IN OUT UINTN
*TransLen
,
235 EFI_USB_ENDPOINT_DESCRIPTOR
*Endpoint
;
240 // If no data to transfer, just return EFI_SUCCESS.
242 if ((DataDir
== EfiUsbNoData
) || (*TransLen
== 0)) {
247 // Select the endpoint then issue the transfer
249 if (DataDir
== EfiUsbDataIn
) {
250 Endpoint
= UsbBot
->BulkInEndpoint
;
252 Endpoint
= UsbBot
->BulkOutEndpoint
;
256 Timeout
= Timeout
/ USB_MASS_1_MILLISECOND
;
258 Status
= UsbBot
->UsbIo
->UsbBulkTransfer (
260 Endpoint
->EndpointAddress
,
266 if (EFI_ERROR (Status
)) {
267 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
)) {
268 DEBUG ((DEBUG_INFO
, "UsbBotDataTransfer: (%r)\n", Status
));
269 DEBUG ((DEBUG_INFO
, "UsbBotDataTransfer: DataIn Stall\n"));
270 UsbClearEndpointStall (UsbBot
->UsbIo
, Endpoint
->EndpointAddress
);
271 } else if (USB_IS_ERROR (Result
, EFI_USB_ERR_NAK
)) {
272 Status
= EFI_NOT_READY
;
274 DEBUG ((DEBUG_ERROR
, "UsbBotDataTransfer: (%r)\n", Status
));
277 if (Status
== EFI_TIMEOUT
) {
278 UsbBotResetDevice (UsbBot
, FALSE
);
286 Get the command execution status from device.
288 This function gets the command execution status from device.
289 BOT transfer is composed of three phases: Command, Data, and Status.
290 This is the Status phase.
292 This function returns the transfer status of the BOT's CSW status,
293 and returns the high level command execution result in Result. So
294 even if EFI_SUCCESS is returned, the command may still have failed.
296 @param UsbBot The USB BOT device.
297 @param TransLen The expected length of the data.
298 @param CmdStatus The result of the command execution.
300 @retval EFI_SUCCESS Command execute result is retrieved and in the Result.
301 @retval Other Error occurred when trying to get status.
306 IN USB_BOT_PROTOCOL
*UsbBot
,
316 EFI_USB_IO_PROTOCOL
*UsbIo
;
320 *CmdStatus
= USB_BOT_COMMAND_ERROR
;
321 Status
= EFI_DEVICE_ERROR
;
322 Endpoint
= UsbBot
->BulkInEndpoint
->EndpointAddress
;
323 UsbIo
= UsbBot
->UsbIo
;
324 Timeout
= USB_BOT_RECV_CSW_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
326 for (Index
= 0; Index
< USB_BOT_RECV_CSW_RETRY
; Index
++) {
328 // Attempt to the read Command Status Wrapper from bulk in endpoint
330 ZeroMem (&Csw
, sizeof (USB_BOT_CSW
));
332 Len
= sizeof (USB_BOT_CSW
);
333 Status
= UsbIo
->UsbBulkTransfer (
341 if (EFI_ERROR (Status
)) {
342 if (USB_IS_ERROR (Result
, EFI_USB_ERR_STALL
)) {
343 UsbClearEndpointStall (UsbIo
, Endpoint
);
349 if (Csw
.Signature
!= USB_BOT_CSW_SIGNATURE
) {
351 // CSW is invalid, so perform reset recovery
353 Status
= UsbBotResetDevice (UsbBot
, FALSE
);
354 } else if (Csw
.CmdStatus
== USB_BOT_COMMAND_ERROR
) {
356 // Respond phase error also needs reset recovery
358 Status
= UsbBotResetDevice (UsbBot
, FALSE
);
360 *CmdStatus
= Csw
.CmdStatus
;
366 // The tag is increased even if there is an error.
374 Call the USB Mass Storage Class BOT protocol to issue
375 the command/data/status circle to execute the commands.
377 @param Context The context of the BOT protocol, that is,
379 @param Cmd The high level command
380 @param CmdLen The command length
381 @param DataDir The direction of the data transfer
382 @param Data The buffer to hold data
383 @param DataLen The length of the data
384 @param Lun The number of logic unit
385 @param Timeout The time to wait command
386 @param CmdStatus The result of high level command execution
388 @retval EFI_SUCCESS The command is executed successfully.
389 @retval Other Failed to execute command
397 IN EFI_USB_DATA_DIRECTION DataDir
,
402 OUT UINT32
*CmdStatus
405 USB_BOT_PROTOCOL
*UsbBot
;
410 *CmdStatus
= USB_MASS_CMD_FAIL
;
411 UsbBot
= (USB_BOT_PROTOCOL
*)Context
;
414 // Send the command to the device. Return immediately if device
415 // rejects the command.
417 Status
= UsbBotSendCommand (UsbBot
, Cmd
, CmdLen
, DataDir
, DataLen
, Lun
);
418 if (EFI_ERROR (Status
)) {
419 DEBUG ((DEBUG_ERROR
, "UsbBotExecCommand: UsbBotSendCommand (%r)\n", Status
));
424 // Transfer the data. Don't return immediately even data transfer
425 // failed. The host should attempt to receive the CSW no matter
426 // whether it succeeds or fails.
428 TransLen
= (UINTN
)DataLen
;
429 UsbBotDataTransfer (UsbBot
, DataDir
, Data
, &TransLen
, Timeout
);
432 // Get the status, if that succeeds, interpret the result
434 Status
= UsbBotGetStatus (UsbBot
, DataLen
, &Result
);
435 if (EFI_ERROR (Status
)) {
436 DEBUG ((DEBUG_ERROR
, "UsbBotExecCommand: UsbBotGetStatus (%r)\n", Status
));
441 *CmdStatus
= USB_MASS_CMD_SUCCESS
;
448 Reset the USB mass storage device by BOT protocol.
450 @param Context The context of the BOT protocol, that is,
452 @param ExtendedVerification If FALSE, just issue Bulk-Only Mass Storage Reset request.
453 If TRUE, additionally reset parent hub port.
455 @retval EFI_SUCCESS The device is reset.
456 @retval Others Failed to reset the device..
462 IN BOOLEAN ExtendedVerification
465 USB_BOT_PROTOCOL
*UsbBot
;
466 EFI_USB_DEVICE_REQUEST Request
;
471 UsbBot
= (USB_BOT_PROTOCOL
*)Context
;
473 if (ExtendedVerification
) {
475 // If we need to do strictly reset, reset its parent hub port
477 Status
= UsbBot
->UsbIo
->UsbPortReset (UsbBot
->UsbIo
);
478 if (EFI_ERROR (Status
)) {
479 return EFI_DEVICE_ERROR
;
484 // Issue a class specific Bulk-Only Mass Storage Reset request,
485 // according to section 3.1 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
487 Request
.RequestType
= 0x21;
488 Request
.Request
= USB_BOT_RESET_REQUEST
;
490 Request
.Index
= UsbBot
->Interface
.InterfaceNumber
;
492 Timeout
= USB_BOT_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
494 Status
= UsbBot
->UsbIo
->UsbControlTransfer (
504 if (EFI_ERROR (Status
)) {
505 return EFI_DEVICE_ERROR
;
509 // The device shall NAK the host's request until the reset is
510 // complete. We can use this to sync the device and host. For
511 // now just stall 100ms to wait for the device.
513 gBS
->Stall (USB_BOT_RESET_DEVICE_STALL
);
516 // Clear the Bulk-In and Bulk-Out stall condition.
518 UsbClearEndpointStall (UsbBot
->UsbIo
, UsbBot
->BulkInEndpoint
->EndpointAddress
);
519 UsbClearEndpointStall (UsbBot
->UsbIo
, UsbBot
->BulkOutEndpoint
->EndpointAddress
);
525 Get the max LUN (Logical Unit Number) of USB mass storage device.
527 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL
528 @param MaxLun Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and
531 @retval EFI_SUCCESS Max LUN is got successfully.
532 @retval Others Fail to execute this request.
541 USB_BOT_PROTOCOL
*UsbBot
;
542 EFI_USB_DEVICE_REQUEST Request
;
547 if ((Context
== NULL
) || (MaxLun
== NULL
)) {
548 return EFI_INVALID_PARAMETER
;
551 UsbBot
= (USB_BOT_PROTOCOL
*)Context
;
554 // Issue a class specific Bulk-Only Mass Storage get max lun request.
555 // according to section 3.2 of USB Mass Storage Class Bulk-Only Transport Spec, v1.0.
557 Request
.RequestType
= 0xA1;
558 Request
.Request
= USB_BOT_GETLUN_REQUEST
;
560 Request
.Index
= UsbBot
->Interface
.InterfaceNumber
;
562 Timeout
= USB_BOT_RESET_DEVICE_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
564 Status
= UsbBot
->UsbIo
->UsbControlTransfer (
573 if (EFI_ERROR (Status
) || (*MaxLun
> USB_BOT_MAX_LUN
)) {
575 // If the Get LUN request returns an error or the MaxLun is larger than
576 // the maximum LUN value (0x0f) supported by the USB Mass Storage Class
577 // Bulk-Only Transport Spec, then set MaxLun to 0.
579 // This improves compatibility with USB FLASH drives that have a single LUN
580 // and either do not return a max LUN value or return an invalid maximum LUN
590 Clean up the resource used by this BOT protocol.
592 @param Context The context of the BOT protocol, that is, USB_BOT_PROTOCOL.
594 @retval EFI_SUCCESS The resource is cleaned up.