2 USB Mass Storage Driver that manages USB Mass Storage Device and produces Block I/O Protocol.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #define USB_MASS_TRANSPORT_COUNT 3
19 // Array of USB transport interfaces.
21 USB_MASS_TRANSPORT
*mUsbMassTransport
[USB_MASS_TRANSPORT_COUNT
] = {
27 EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding
= {
28 USBMassDriverBindingSupported
,
29 USBMassDriverBindingStart
,
30 USBMassDriverBindingStop
,
37 Reset the block device.
39 This function implements EFI_BLOCK_IO_PROTOCOL.Reset().
40 It resets the block device hardware.
41 ExtendedVerification is ignored in this implementation.
43 @param This Indicates a pointer to the calling context.
44 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
45 verification operation of the device during reset.
47 @retval EFI_SUCCESS The block device was reset.
48 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.
54 IN EFI_BLOCK_IO_PROTOCOL
*This
,
55 IN BOOLEAN ExtendedVerification
58 USB_MASS_DEVICE
*UsbMass
;
63 // Raise TPL to TPL_CALLBACK to serialize all its operations
64 // to protect shared data structures.
66 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
68 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
69 Status
= UsbMass
->Transport
->Reset (UsbMass
->Context
, ExtendedVerification
);
71 gBS
->RestoreTPL (OldTpl
);
77 Reads the requested number of blocks from the device.
79 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
80 It reads the requested number of blocks from the device.
81 All the blocks are read, or an error is returned.
83 @param This Indicates a pointer to the calling context.
84 @param MediaId The media ID that the read request is for.
85 @param Lba The starting logical block address to read from on the device.
86 @param BufferSize The size of the Buffer in bytes.
87 This must be a multiple of the intrinsic block size of the device.
88 @param Buffer A pointer to the destination buffer for the data. The caller is
89 responsible for either having implicit or explicit ownership of the buffer.
91 @retval EFI_SUCCESS The data was read correctly from the device.
92 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.
93 @retval EFI_NO_MEDIA There is no media in the device.
94 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
95 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.
96 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
97 or the buffer is not on proper alignment.
103 IN EFI_BLOCK_IO_PROTOCOL
*This
,
110 USB_MASS_DEVICE
*UsbMass
;
111 EFI_BLOCK_IO_MEDIA
*Media
;
117 // Raise TPL to TPL_CALLBACK to serialize all its operations
118 // to protect shared data structures.
120 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
121 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
122 Media
= &UsbMass
->BlockIoMedia
;
125 // If it is a removable media, such as CD-Rom or Usb-Floppy,
126 // need to detect the media before each read/write. While some of
127 // Usb-Flash is marked as removable media.
129 if (Media
->RemovableMedia
) {
130 Status
= UsbBootDetectMedia (UsbMass
);
131 if (EFI_ERROR (Status
)) {
136 if (!(Media
->MediaPresent
)) {
137 Status
= EFI_NO_MEDIA
;
141 if (MediaId
!= Media
->MediaId
) {
142 Status
= EFI_MEDIA_CHANGED
;
146 if (BufferSize
== 0) {
147 Status
= EFI_SUCCESS
;
151 if (Buffer
== NULL
) {
152 Status
= EFI_INVALID_PARAMETER
;
157 // BufferSize must be a multiple of the intrinsic block size of the device.
159 if ((BufferSize
% Media
->BlockSize
) != 0) {
160 Status
= EFI_BAD_BUFFER_SIZE
;
164 TotalBlock
= BufferSize
/ Media
->BlockSize
;
167 // Make sure the range to read is valid.
169 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
170 Status
= EFI_INVALID_PARAMETER
;
174 if (UsbMass
->Cdb16Byte
) {
175 Status
= UsbBootReadWriteBlocks16 (UsbMass
, FALSE
, Lba
, TotalBlock
, Buffer
);
177 Status
= UsbBootReadWriteBlocks (UsbMass
, FALSE
, (UINT32
) Lba
, TotalBlock
, Buffer
);
180 if (EFI_ERROR (Status
)) {
181 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
182 UsbMassReset (This
, TRUE
);
186 gBS
->RestoreTPL (OldTpl
);
192 Writes a specified number of blocks to the device.
194 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
195 It writes a specified number of blocks to the device.
196 All blocks are written, or an error is returned.
198 @param This Indicates a pointer to the calling context.
199 @param MediaId The media ID that the write request is for.
200 @param Lba The starting logical block address to be written.
201 @param BufferSize The size of the Buffer in bytes.
202 This must be a multiple of the intrinsic block size of the device.
203 @param Buffer Pointer to the source buffer for the data.
205 @retval EFI_SUCCESS The data were written correctly to the device.
206 @retval EFI_WRITE_PROTECTED The device cannot be written to.
207 @retval EFI_NO_MEDIA There is no media in the device.
208 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
209 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
210 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
211 block size of the device.
212 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
213 or the buffer is not on proper alignment.
219 IN EFI_BLOCK_IO_PROTOCOL
*This
,
226 USB_MASS_DEVICE
*UsbMass
;
227 EFI_BLOCK_IO_MEDIA
*Media
;
233 // Raise TPL to TPL_CALLBACK to serialize all its operations
234 // to protect shared data structures.
236 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
237 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
238 Media
= &UsbMass
->BlockIoMedia
;
241 // If it is a removable media, such as CD-Rom or Usb-Floppy,
242 // need to detect the media before each read/write. Some of
243 // USB Flash is marked as removable media.
245 if (Media
->RemovableMedia
) {
246 Status
= UsbBootDetectMedia (UsbMass
);
247 if (EFI_ERROR (Status
)) {
252 if (!(Media
->MediaPresent
)) {
253 Status
= EFI_NO_MEDIA
;
257 if (MediaId
!= Media
->MediaId
) {
258 Status
= EFI_MEDIA_CHANGED
;
262 if (BufferSize
== 0) {
263 Status
= EFI_SUCCESS
;
267 if (Buffer
== NULL
) {
268 Status
= EFI_INVALID_PARAMETER
;
273 // BufferSize must be a multiple of the intrinsic block size of the device.
275 if ((BufferSize
% Media
->BlockSize
) != 0) {
276 Status
= EFI_BAD_BUFFER_SIZE
;
280 TotalBlock
= BufferSize
/ Media
->BlockSize
;
283 // Make sure the range to write is valid.
285 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
286 Status
= EFI_INVALID_PARAMETER
;
291 // Try to write the data even the device is marked as ReadOnly,
292 // and clear the status should the write succeed.
294 if (UsbMass
->Cdb16Byte
) {
295 Status
= UsbBootReadWriteBlocks16 (UsbMass
, TRUE
, Lba
, TotalBlock
, Buffer
);
297 Status
= UsbBootReadWriteBlocks (UsbMass
, TRUE
, (UINT32
) Lba
, TotalBlock
, Buffer
);
300 if (EFI_ERROR (Status
)) {
301 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
302 UsbMassReset (This
, TRUE
);
306 gBS
->RestoreTPL (OldTpl
);
311 Flushes all modified data to a physical block device.
313 This function implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
314 USB mass storage device doesn't support write cache,
315 so return EFI_SUCCESS directly.
317 @param This Indicates a pointer to the calling context.
319 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
320 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
321 @retval EFI_NO_MEDIA There is no media in the device.
327 IN EFI_BLOCK_IO_PROTOCOL
*This
334 Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
336 @param UsbMass The USB mass storage device
338 @retval EFI_SUCCESS The media parameters are updated successfully.
339 @retval Others Failed to get the media parameters.
344 IN USB_MASS_DEVICE
*UsbMass
347 EFI_BLOCK_IO_MEDIA
*Media
;
350 Media
= &UsbMass
->BlockIoMedia
;
353 // Fields of EFI_BLOCK_IO_MEDIA are defined in UEFI 2.0 spec,
354 // section for Block I/O Protocol.
356 Media
->MediaPresent
= FALSE
;
357 Media
->LogicalPartition
= FALSE
;
358 Media
->ReadOnly
= FALSE
;
359 Media
->WriteCaching
= FALSE
;
363 Status
= UsbBootGetParams (UsbMass
);
364 DEBUG ((DEBUG_INFO
, "UsbMassInitMedia: UsbBootGetParams (%r)\n", Status
));
365 if (Status
== EFI_MEDIA_CHANGED
) {
367 // Some USB storage devices may report MEDIA_CHANGED sense key when hot-plugged.
368 // Treat it as SUCCESS
370 Status
= EFI_SUCCESS
;
376 Initilize the USB Mass Storage transport.
378 This function tries to find the matching USB Mass Storage transport
379 protocol for USB device. If found, initializes the matching transport.
381 @param This The USB mass driver's driver binding.
382 @param Controller The device to test.
383 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
384 @param Context The parameter for USB_MASS_DEVICE.Context.
385 @param MaxLun Get the MaxLun if is BOT dev.
387 @retval EFI_SUCCESS The initialization is successful.
388 @retval EFI_UNSUPPORTED No matching transport protocol is found.
389 @retval Others Failed to initialize dev.
393 UsbMassInitTransport (
394 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
395 IN EFI_HANDLE Controller
,
396 OUT USB_MASS_TRANSPORT
**Transport
,
401 EFI_USB_IO_PROTOCOL
*UsbIo
;
402 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
406 Status
= gBS
->OpenProtocol (
408 &gEfiUsbIoProtocolGuid
,
410 This
->DriverBindingHandle
,
412 EFI_OPEN_PROTOCOL_BY_DRIVER
415 if (EFI_ERROR (Status
)) {
419 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
420 if (EFI_ERROR (Status
)) {
424 Status
= EFI_UNSUPPORTED
;
427 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
428 // matching transport protocol.
429 // If not found, return EFI_UNSUPPORTED.
430 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
432 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
433 *Transport
= mUsbMassTransport
[Index
];
435 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
436 Status
= (*Transport
)->Init (UsbIo
, Context
);
441 if (EFI_ERROR (Status
)) {
446 // For BOT device, try to get its max LUN.
447 // If max LUN is 0, then it is a non-lun device.
448 // Otherwise, it is a multi-lun device.
450 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
451 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
457 &gEfiUsbIoProtocolGuid
,
458 This
->DriverBindingHandle
,
465 Initialize data for device that supports multiple LUNSs.
467 @param This The Driver Binding Protocol instance.
468 @param Controller The device to initialize.
469 @param Transport Pointer to USB_MASS_TRANSPORT.
470 @param Context Parameter for USB_MASS_DEVICE.Context.
471 @param DevicePath The remaining device path.
472 @param MaxLun The max LUN number.
474 @retval EFI_SUCCESS At least one LUN is initialized successfully.
475 @retval EFI_NOT_FOUND Fail to initialize any of multiple LUNs.
479 UsbMassInitMultiLun (
480 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
481 IN EFI_HANDLE Controller
,
482 IN USB_MASS_TRANSPORT
*Transport
,
484 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
488 USB_MASS_DEVICE
*UsbMass
;
489 EFI_USB_IO_PROTOCOL
*UsbIo
;
490 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
493 EFI_STATUS ReturnStatus
;
496 ReturnStatus
= EFI_NOT_FOUND
;
498 for (Index
= 0; Index
<= MaxLun
; Index
++) {
500 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
503 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
504 ASSERT (UsbMass
!= NULL
);
506 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
507 UsbMass
->UsbIo
= UsbIo
;
508 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
509 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
510 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
511 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
512 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
513 UsbMass
->OpticalStorage
= FALSE
;
514 UsbMass
->Transport
= Transport
;
515 UsbMass
->Context
= Context
;
516 UsbMass
->Lun
= Index
;
519 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
521 Status
= UsbMassInitMedia (UsbMass
);
522 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
523 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
529 // Create a device path node for device logic unit, and append it.
531 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
532 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
533 LunNode
.Lun
= UsbMass
->Lun
;
535 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
537 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
539 if (UsbMass
->DevicePath
== NULL
) {
540 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
541 Status
= EFI_OUT_OF_RESOURCES
;
546 InitializeDiskInfo (UsbMass
);
549 // Create a new handle for each LUN, and install Block I/O Protocol and Device Path Protocol.
551 Status
= gBS
->InstallMultipleProtocolInterfaces (
552 &UsbMass
->Controller
,
553 &gEfiDevicePathProtocolGuid
,
555 &gEfiBlockIoProtocolGuid
,
557 &gEfiDiskInfoProtocolGuid
,
562 if (EFI_ERROR (Status
)) {
563 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
564 FreePool (UsbMass
->DevicePath
);
570 // Open USB I/O Protocol by child to setup a parent-child relationship.
572 Status
= gBS
->OpenProtocol (
574 &gEfiUsbIoProtocolGuid
,
576 This
->DriverBindingHandle
,
578 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
581 if (EFI_ERROR (Status
)) {
582 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
583 gBS
->UninstallMultipleProtocolInterfaces (
584 &UsbMass
->Controller
,
585 &gEfiDevicePathProtocolGuid
,
587 &gEfiBlockIoProtocolGuid
,
589 &gEfiDiskInfoProtocolGuid
,
593 FreePool (UsbMass
->DevicePath
);
597 ReturnStatus
= EFI_SUCCESS
;
598 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
605 Initialize data for device that does not support multiple LUNSs.
607 @param This The Driver Binding Protocol instance.
608 @param Controller The device to initialize.
609 @param Transport Pointer to USB_MASS_TRANSPORT.
610 @param Context Parameter for USB_MASS_DEVICE.Context.
612 @retval EFI_SUCCESS Initialization succeeds.
613 @retval Other Initialization fails.
618 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
619 IN EFI_HANDLE Controller
,
620 IN USB_MASS_TRANSPORT
*Transport
,
624 USB_MASS_DEVICE
*UsbMass
;
625 EFI_USB_IO_PROTOCOL
*UsbIo
;
629 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
630 ASSERT (UsbMass
!= NULL
);
632 Status
= gBS
->OpenProtocol (
634 &gEfiUsbIoProtocolGuid
,
636 This
->DriverBindingHandle
,
638 EFI_OPEN_PROTOCOL_BY_DRIVER
641 if (EFI_ERROR (Status
)) {
642 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
646 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
647 UsbMass
->Controller
= Controller
;
648 UsbMass
->UsbIo
= UsbIo
;
649 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
650 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
651 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
652 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
653 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
654 UsbMass
->OpticalStorage
= FALSE
;
655 UsbMass
->Transport
= Transport
;
656 UsbMass
->Context
= Context
;
659 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
661 Status
= UsbMassInitMedia (UsbMass
);
662 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
663 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
667 InitializeDiskInfo (UsbMass
);
669 Status
= gBS
->InstallMultipleProtocolInterfaces (
671 &gEfiBlockIoProtocolGuid
,
673 &gEfiDiskInfoProtocolGuid
,
677 if (EFI_ERROR (Status
)) {
684 if (UsbMass
!= NULL
) {
690 &gEfiUsbIoProtocolGuid
,
691 This
->DriverBindingHandle
,
700 Check whether the controller is a supported USB mass storage.
702 @param This The USB mass storage driver binding protocol.
703 @param Controller The controller handle to check.
704 @param RemainingDevicePath The remaining device path.
706 @retval EFI_SUCCESS The driver supports this controller.
707 @retval other This device isn't supported.
712 USBMassDriverBindingSupported (
713 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
714 IN EFI_HANDLE Controller
,
715 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
718 EFI_USB_IO_PROTOCOL
*UsbIo
;
719 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
720 USB_MASS_TRANSPORT
*Transport
;
724 Status
= gBS
->OpenProtocol (
726 &gEfiUsbIoProtocolGuid
,
728 This
->DriverBindingHandle
,
730 EFI_OPEN_PROTOCOL_BY_DRIVER
732 if (EFI_ERROR (Status
)) {
737 // Get the interface descriptor to check the USB class and find a transport
740 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
741 if (EFI_ERROR (Status
)) {
745 Status
= EFI_UNSUPPORTED
;
747 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
752 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
753 // matching transport method.
754 // If not found, return EFI_UNSUPPORTED.
755 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
757 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
758 Transport
= mUsbMassTransport
[Index
];
759 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
760 Status
= Transport
->Init (UsbIo
, NULL
);
768 &gEfiUsbIoProtocolGuid
,
769 This
->DriverBindingHandle
,
777 Starts the USB mass storage device with this driver.
779 This function consumes USB I/O Portocol, intializes USB mass storage device,
780 installs Block I/O Protocol, and submits Asynchronous Interrupt
781 Transfer to manage the USB mass storage device.
783 @param This The USB mass storage driver binding protocol.
784 @param Controller The USB mass storage device to start on
785 @param RemainingDevicePath The remaining device path.
787 @retval EFI_SUCCESS This driver supports this device.
788 @retval EFI_UNSUPPORTED This driver does not support this device.
789 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
790 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
791 @retval EFI_ALREADY_STARTED This driver has been started.
796 USBMassDriverBindingStart (
797 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
798 IN EFI_HANDLE Controller
,
799 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
802 USB_MASS_TRANSPORT
*Transport
;
803 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
807 EFI_USB_IO_PROTOCOL
*UsbIo
;
810 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
816 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
818 if (EFI_ERROR (Status
)) {
819 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
824 // Initialize data for device that does not support multiple LUNSs.
826 Status
= UsbMassInitNonLun (This
, Controller
, Transport
, Context
);
827 if (EFI_ERROR (Status
)) {
828 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
832 // Open device path to prepare for appending Device Logic Unit node.
834 Status
= gBS
->OpenProtocol (
836 &gEfiDevicePathProtocolGuid
,
837 (VOID
**) &DevicePath
,
838 This
->DriverBindingHandle
,
840 EFI_OPEN_PROTOCOL_BY_DRIVER
843 if (EFI_ERROR (Status
)) {
844 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
848 Status
= gBS
->OpenProtocol (
850 &gEfiUsbIoProtocolGuid
,
852 This
->DriverBindingHandle
,
854 EFI_OPEN_PROTOCOL_BY_DRIVER
857 if (EFI_ERROR (Status
)) {
858 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status
));
861 &gEfiDevicePathProtocolGuid
,
862 This
->DriverBindingHandle
,
869 // Initialize data for device that supports multiple LUNs.
870 // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.
872 Status
= UsbMassInitMultiLun (This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
873 if (EFI_ERROR (Status
)) {
876 &gEfiDevicePathProtocolGuid
,
877 This
->DriverBindingHandle
,
882 &gEfiUsbIoProtocolGuid
,
883 This
->DriverBindingHandle
,
886 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
890 gBS
->RestoreTPL (OldTpl
);
896 Stop controlling the device.
898 @param This The USB mass storage driver binding
899 @param Controller The device controller controlled by the driver.
900 @param NumberOfChildren The number of children of this device
901 @param ChildHandleBuffer The buffer of children handle.
903 @retval EFI_SUCCESS The driver stopped from controlling the device.
904 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
905 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
906 @retval Others Failed to stop the driver
911 USBMassDriverBindingStop (
912 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
913 IN EFI_HANDLE Controller
,
914 IN UINTN NumberOfChildren
,
915 IN EFI_HANDLE
*ChildHandleBuffer
919 USB_MASS_DEVICE
*UsbMass
;
920 EFI_USB_IO_PROTOCOL
*UsbIo
;
921 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
923 BOOLEAN AllChildrenStopped
;
926 // This is a bus driver stop function since multi-lun is supported.
927 // There are three kinds of device handles that might be passed:
928 // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)
929 // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)
930 // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).
932 if (NumberOfChildren
== 0) {
934 // A handle without any children, might be 1st and 2nd type.
936 Status
= gBS
->OpenProtocol (
938 &gEfiBlockIoProtocolGuid
,
940 This
->DriverBindingHandle
,
942 EFI_OPEN_PROTOCOL_GET_PROTOCOL
945 if (EFI_ERROR(Status
)) {
947 // This is a 2nd type handle(multi-lun root), it needs to close devicepath
948 // and usbio protocol.
952 &gEfiDevicePathProtocolGuid
,
953 This
->DriverBindingHandle
,
958 &gEfiUsbIoProtocolGuid
,
959 This
->DriverBindingHandle
,
962 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
967 // This is a 1st type handle(non-multi-lun), which only needs to uninstall
968 // Block I/O Protocol, close USB I/O Protocol and free mass device.
970 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
973 // Uninstall Block I/O protocol from the device handle,
974 // then call the transport protocol to stop itself.
976 Status
= gBS
->UninstallMultipleProtocolInterfaces (
978 &gEfiBlockIoProtocolGuid
,
980 &gEfiDiskInfoProtocolGuid
,
984 if (EFI_ERROR (Status
)) {
990 &gEfiUsbIoProtocolGuid
,
991 This
->DriverBindingHandle
,
995 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
998 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
1003 // This is a 3rd type handle(multi-lun), which needs uninstall
1004 // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and
1005 // free mass device for all children.
1007 AllChildrenStopped
= TRUE
;
1009 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1011 Status
= gBS
->OpenProtocol (
1012 ChildHandleBuffer
[Index
],
1013 &gEfiBlockIoProtocolGuid
,
1015 This
->DriverBindingHandle
,
1017 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1019 if (EFI_ERROR (Status
)) {
1020 AllChildrenStopped
= FALSE
;
1021 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", (UINT32
)Index
));
1025 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
1027 gBS
->CloseProtocol (
1029 &gEfiUsbIoProtocolGuid
,
1030 This
->DriverBindingHandle
,
1031 ChildHandleBuffer
[Index
]
1034 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1035 ChildHandleBuffer
[Index
],
1036 &gEfiDevicePathProtocolGuid
,
1037 UsbMass
->DevicePath
,
1038 &gEfiBlockIoProtocolGuid
,
1040 &gEfiDiskInfoProtocolGuid
,
1045 if (EFI_ERROR (Status
)) {
1047 // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.
1049 AllChildrenStopped
= FALSE
;
1050 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32
)Index
));
1054 &gEfiUsbIoProtocolGuid
,
1056 This
->DriverBindingHandle
,
1057 ChildHandleBuffer
[Index
],
1058 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1062 // Succeed to stop this multi-lun handle, so go on with next child.
1064 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1065 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1071 if (!AllChildrenStopped
) {
1072 return EFI_DEVICE_ERROR
;
1075 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", (UINT32
) NumberOfChildren
));
1080 Entrypoint of USB Mass Storage Driver.
1082 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
1083 Protocol together with Component Name Protocols.
1085 @param ImageHandle The firmware allocated handle for the EFI image.
1086 @param SystemTable A pointer to the EFI System Table.
1088 @retval EFI_SUCCESS The entry point is executed successfully.
1093 USBMassStorageEntryPoint (
1094 IN EFI_HANDLE ImageHandle
,
1095 IN EFI_SYSTEM_TABLE
*SystemTable
1101 // Install driver binding protocol
1103 Status
= EfiLibInstallDriverBindingComponentName2 (
1106 &gUSBMassDriverBinding
,
1108 &gUsbMassStorageComponentName
,
1109 &gUsbMassStorageComponentName2
1111 ASSERT_EFI_ERROR (Status
);