2 USB Mass Storage Driver that manages USB Mass Storage Device and produces Block I/O Protocol.
4 Copyright (c) 2007 - 2011, 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_NOTIFY to serialize all its operations
64 // to protect shared data structures.
66 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
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_NOTIFY to serialize all its operations
118 // to protect shared data structures.
120 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
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 Status
= UsbBootReadBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
175 if (EFI_ERROR (Status
)) {
176 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
177 UsbMassReset (This
, TRUE
);
181 gBS
->RestoreTPL (OldTpl
);
187 Writes a specified number of blocks to the device.
189 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
190 It writes a specified number of blocks to the device.
191 All blocks are written, or an error is returned.
193 @param This Indicates a pointer to the calling context.
194 @param MediaId The media ID that the write request is for.
195 @param Lba The starting logical block address to be written.
196 @param BufferSize The size of the Buffer in bytes.
197 This must be a multiple of the intrinsic block size of the device.
198 @param Buffer Pointer to the source buffer for the data.
200 @retval EFI_SUCCESS The data were written correctly to the device.
201 @retval EFI_WRITE_PROTECTED The device cannot be written to.
202 @retval EFI_NO_MEDIA There is no media in the device.
203 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
204 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
205 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
206 block size of the device.
207 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
208 or the buffer is not on proper alignment.
214 IN EFI_BLOCK_IO_PROTOCOL
*This
,
221 USB_MASS_DEVICE
*UsbMass
;
222 EFI_BLOCK_IO_MEDIA
*Media
;
228 // Raise TPL to TPL_NOTIFY to serialize all its operations
229 // to protect shared data structures.
231 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
232 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
233 Media
= &UsbMass
->BlockIoMedia
;
236 // If it is a removable media, such as CD-Rom or Usb-Floppy,
237 // need to detect the media before each read/write. Some of
238 // USB Flash is marked as removable media.
240 if (Media
->RemovableMedia
) {
241 Status
= UsbBootDetectMedia (UsbMass
);
242 if (EFI_ERROR (Status
)) {
247 if (!(Media
->MediaPresent
)) {
248 Status
= EFI_NO_MEDIA
;
252 if (MediaId
!= Media
->MediaId
) {
253 Status
= EFI_MEDIA_CHANGED
;
257 if (BufferSize
== 0) {
258 Status
= EFI_SUCCESS
;
262 if (Buffer
== NULL
) {
263 Status
= EFI_INVALID_PARAMETER
;
268 // BufferSize must be a multiple of the intrinsic block size of the device.
270 if ((BufferSize
% Media
->BlockSize
) != 0) {
271 Status
= EFI_BAD_BUFFER_SIZE
;
275 TotalBlock
= BufferSize
/ Media
->BlockSize
;
278 // Make sure the range to write is valid.
280 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
281 Status
= EFI_INVALID_PARAMETER
;
286 // Try to write the data even the device is marked as ReadOnly,
287 // and clear the status should the write succeed.
289 Status
= UsbBootWriteBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
290 if (EFI_ERROR (Status
)) {
291 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
292 UsbMassReset (This
, TRUE
);
296 gBS
->RestoreTPL (OldTpl
);
301 Flushes all modified data to a physical block device.
303 This function implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
304 USB mass storage device doesn't support write cache,
305 so return EFI_SUCCESS directly.
307 @param This Indicates a pointer to the calling context.
309 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
310 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
311 @retval EFI_NO_MEDIA There is no media in the device.
317 IN EFI_BLOCK_IO_PROTOCOL
*This
324 Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
326 @param UsbMass The USB mass storage device
328 @retval EFI_SUCCESS The media parameters are updated successfully.
329 @retval Others Failed to get the media parameters.
334 IN USB_MASS_DEVICE
*UsbMass
337 EFI_BLOCK_IO_MEDIA
*Media
;
340 Media
= &UsbMass
->BlockIoMedia
;
343 // Fields of EFI_BLOCK_IO_MEDIA are defined in UEFI 2.0 spec,
344 // section for Block I/O Protocol.
346 Media
->MediaPresent
= FALSE
;
347 Media
->LogicalPartition
= FALSE
;
348 Media
->ReadOnly
= FALSE
;
349 Media
->WriteCaching
= FALSE
;
353 Status
= UsbBootGetParams (UsbMass
);
358 Initilize the USB Mass Storage transport.
360 This function tries to find the matching USB Mass Storage transport
361 protocol for USB device. If found, initializes the matching transport.
363 @param This The USB mass driver's driver binding.
364 @param Controller The device to test.
365 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
366 @param Context The parameter for USB_MASS_DEVICE.Context.
367 @param MaxLun Get the MaxLun if is BOT dev.
369 @retval EFI_SUCCESS The initialization is successful.
370 @retval EFI_UNSUPPORTED No matching transport protocol is found.
371 @retval Others Failed to initialize dev.
375 UsbMassInitTransport (
376 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
377 IN EFI_HANDLE Controller
,
378 OUT USB_MASS_TRANSPORT
**Transport
,
383 EFI_USB_IO_PROTOCOL
*UsbIo
;
384 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
388 Status
= gBS
->OpenProtocol (
390 &gEfiUsbIoProtocolGuid
,
392 This
->DriverBindingHandle
,
394 EFI_OPEN_PROTOCOL_BY_DRIVER
397 if (EFI_ERROR (Status
)) {
401 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
402 if (EFI_ERROR (Status
)) {
406 Status
= EFI_UNSUPPORTED
;
409 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
410 // matching transport protocol.
411 // If not found, return EFI_UNSUPPORTED.
412 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
414 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
415 *Transport
= mUsbMassTransport
[Index
];
417 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
418 Status
= (*Transport
)->Init (UsbIo
, Context
);
423 if (EFI_ERROR (Status
)) {
428 // For BOT device, try to get its max LUN.
429 // If max LUN is 0, then it is a non-lun device.
430 // Otherwise, it is a multi-lun device.
432 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
433 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
439 &gEfiUsbIoProtocolGuid
,
440 This
->DriverBindingHandle
,
447 Initialize data for device that supports multiple LUNSs.
449 @param This The Driver Binding Protocol instance.
450 @param Controller The device to initialize.
451 @param Transport Pointer to USB_MASS_TRANSPORT.
452 @param Context Parameter for USB_MASS_DEVICE.Context.
453 @param DevicePath The remaining device path.
454 @param MaxLun The max LUN number.
456 @retval EFI_SUCCESS At least one LUN is initialized successfully.
457 @retval EFI_OUT_OF_RESOURCES Out of resource while creating device path node.
458 @retval Other Initialization fails.
462 UsbMassInitMultiLun (
463 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
464 IN EFI_HANDLE Controller
,
465 IN USB_MASS_TRANSPORT
*Transport
,
467 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
471 USB_MASS_DEVICE
*UsbMass
;
472 EFI_USB_IO_PROTOCOL
*UsbIo
;
473 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
479 for (Index
= 0; Index
<= MaxLun
; Index
++) {
481 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
484 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
485 ASSERT (UsbMass
!= NULL
);
487 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
488 UsbMass
->UsbIo
= UsbIo
;
489 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
490 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
491 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
492 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
493 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
494 UsbMass
->OpticalStorage
= FALSE
;
495 UsbMass
->Transport
= Transport
;
496 UsbMass
->Context
= Context
;
497 UsbMass
->Lun
= Index
;
500 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
502 Status
= UsbMassInitMedia (UsbMass
);
503 if (!EFI_ERROR (Status
)) {
505 // According to USB Mass Storage Specification for Bootability, only following
506 // 4 Peripheral Device Types are in spec.
508 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
509 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
510 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
511 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
512 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
515 } else if (Status
!= EFI_NO_MEDIA
){
516 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
521 // Create a device path node for device logic unit, and append it.
523 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
524 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
525 LunNode
.Lun
= UsbMass
->Lun
;
527 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
529 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
531 if (UsbMass
->DevicePath
== NULL
) {
532 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
534 Status
= EFI_OUT_OF_RESOURCES
;
538 InitializeDiskInfo (UsbMass
);
541 // Create a new handle for each LUN, and install Block I/O Protocol and Device Path Protocol.
543 Status
= gBS
->InstallMultipleProtocolInterfaces (
544 &UsbMass
->Controller
,
545 &gEfiDevicePathProtocolGuid
,
547 &gEfiBlockIoProtocolGuid
,
549 &gEfiDiskInfoProtocolGuid
,
554 if (EFI_ERROR (Status
)) {
555 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
560 // Open USB I/O Protocol by child to setup a parent-child relationship.
562 Status
= gBS
->OpenProtocol (
564 &gEfiUsbIoProtocolGuid
,
566 This
->DriverBindingHandle
,
568 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
571 if (EFI_ERROR (Status
)) {
572 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
573 gBS
->UninstallMultipleProtocolInterfaces (
574 &UsbMass
->Controller
,
575 &gEfiDevicePathProtocolGuid
,
577 &gEfiBlockIoProtocolGuid
,
579 &gEfiDiskInfoProtocolGuid
,
586 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
592 if (UsbMass
!= NULL
) {
593 if (UsbMass
->DevicePath
!= NULL
) {
594 FreePool (UsbMass
->DevicePath
);
601 &gEfiUsbIoProtocolGuid
,
602 This
->DriverBindingHandle
,
608 // Return EFI_SUCCESS if at least one LUN is initialized successfully.
618 Initialize data for device that does not support multiple LUNSs.
620 @param This The Driver Binding Protocol instance.
621 @param Controller The device to initialize.
622 @param Transport Pointer to USB_MASS_TRANSPORT.
623 @param Context Parameter for USB_MASS_DEVICE.Context.
625 @retval EFI_SUCCESS Initialization succeeds.
626 @retval Other Initialization fails.
631 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
632 IN EFI_HANDLE Controller
,
633 IN USB_MASS_TRANSPORT
*Transport
,
637 USB_MASS_DEVICE
*UsbMass
;
638 EFI_USB_IO_PROTOCOL
*UsbIo
;
642 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
643 ASSERT (UsbMass
!= NULL
);
645 Status
= gBS
->OpenProtocol (
647 &gEfiUsbIoProtocolGuid
,
649 This
->DriverBindingHandle
,
651 EFI_OPEN_PROTOCOL_BY_DRIVER
654 if (EFI_ERROR (Status
)) {
655 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
659 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
660 UsbMass
->Controller
= Controller
;
661 UsbMass
->UsbIo
= UsbIo
;
662 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
663 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
664 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
665 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
666 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
667 UsbMass
->OpticalStorage
= FALSE
;
668 UsbMass
->Transport
= Transport
;
669 UsbMass
->Context
= Context
;
672 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
674 Status
= UsbMassInitMedia (UsbMass
);
675 if (!EFI_ERROR (Status
)) {
677 // According to USB Mass Storage Specification for Bootability, only following
678 // 4 Peripheral Device Types are in spec.
680 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
681 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
682 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
683 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
684 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
687 } else if (Status
!= EFI_NO_MEDIA
){
688 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
692 InitializeDiskInfo (UsbMass
);
694 Status
= gBS
->InstallMultipleProtocolInterfaces (
696 &gEfiBlockIoProtocolGuid
,
698 &gEfiDiskInfoProtocolGuid
,
702 if (EFI_ERROR (Status
)) {
709 if (UsbMass
!= NULL
) {
715 &gEfiUsbIoProtocolGuid
,
716 This
->DriverBindingHandle
,
725 Check whether the controller is a supported USB mass storage.
727 @param This The USB mass storage driver binding protocol.
728 @param Controller The controller handle to check.
729 @param RemainingDevicePath The remaining device path.
731 @retval EFI_SUCCESS The driver supports this controller.
732 @retval other This device isn't supported.
737 USBMassDriverBindingSupported (
738 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
739 IN EFI_HANDLE Controller
,
740 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
743 EFI_USB_IO_PROTOCOL
*UsbIo
;
744 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
745 USB_MASS_TRANSPORT
*Transport
;
749 Status
= gBS
->OpenProtocol (
751 &gEfiUsbIoProtocolGuid
,
753 This
->DriverBindingHandle
,
755 EFI_OPEN_PROTOCOL_BY_DRIVER
757 if (EFI_ERROR (Status
)) {
762 // Get the interface descriptor to check the USB class and find a transport
765 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
766 if (EFI_ERROR (Status
)) {
770 Status
= EFI_UNSUPPORTED
;
772 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
777 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
778 // matching transport method.
779 // If not found, return EFI_UNSUPPORTED.
780 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
782 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
783 Transport
= mUsbMassTransport
[Index
];
784 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
785 Status
= Transport
->Init (UsbIo
, NULL
);
793 &gEfiUsbIoProtocolGuid
,
794 This
->DriverBindingHandle
,
802 Starts the USB mass storage device with this driver.
804 This function consumes USB I/O Portocol, intializes USB mass storage device,
805 installs Block I/O Protocol, and submits Asynchronous Interrupt
806 Transfer to manage the USB mass storage device.
808 @param This The USB mass storage driver binding protocol.
809 @param Controller The USB mass storage device to start on
810 @param RemainingDevicePath The remaining device path.
812 @retval EFI_SUCCESS This driver supports this device.
813 @retval EFI_UNSUPPORTED This driver does not support this device.
814 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
815 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
816 @retval EFI_ALREADY_STARTED This driver has been started.
821 USBMassDriverBindingStart (
822 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
823 IN EFI_HANDLE Controller
,
824 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
827 USB_MASS_TRANSPORT
*Transport
;
828 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
832 EFI_USB_IO_PROTOCOL
*UsbIo
;
835 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
841 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
843 if (EFI_ERROR (Status
)) {
844 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
849 // Initialize data for device that does not support multiple LUNSs.
851 Status
= UsbMassInitNonLun (This
, Controller
, Transport
, Context
);
852 if (EFI_ERROR (Status
)) {
853 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
857 // Open device path to prepare for appending Device Logic Unit node.
859 Status
= gBS
->OpenProtocol (
861 &gEfiDevicePathProtocolGuid
,
862 (VOID
**) &DevicePath
,
863 This
->DriverBindingHandle
,
865 EFI_OPEN_PROTOCOL_BY_DRIVER
868 if (EFI_ERROR (Status
)) {
869 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
873 Status
= gBS
->OpenProtocol (
875 &gEfiUsbIoProtocolGuid
,
877 This
->DriverBindingHandle
,
879 EFI_OPEN_PROTOCOL_BY_DRIVER
882 if (EFI_ERROR (Status
)) {
883 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status
));
886 &gEfiDevicePathProtocolGuid
,
887 This
->DriverBindingHandle
,
894 // Initialize data for device that supports multiple LUNSs.
895 // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.
897 Status
= UsbMassInitMultiLun (This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
898 if (EFI_ERROR (Status
)) {
901 &gEfiDevicePathProtocolGuid
,
902 This
->DriverBindingHandle
,
907 &gEfiUsbIoProtocolGuid
,
908 This
->DriverBindingHandle
,
911 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
915 gBS
->RestoreTPL (OldTpl
);
921 Stop controlling the device.
923 @param This The USB mass storage driver binding
924 @param Controller The device controller controlled by the driver.
925 @param NumberOfChildren The number of children of this device
926 @param ChildHandleBuffer The buffer of children handle.
928 @retval EFI_SUCCESS The driver stopped from controlling the device.
929 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
930 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
931 @retval Others Failed to stop the driver
936 USBMassDriverBindingStop (
937 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
938 IN EFI_HANDLE Controller
,
939 IN UINTN NumberOfChildren
,
940 IN EFI_HANDLE
*ChildHandleBuffer
944 USB_MASS_DEVICE
*UsbMass
;
945 EFI_USB_IO_PROTOCOL
*UsbIo
;
946 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
948 BOOLEAN AllChildrenStopped
;
951 // This is a bus driver stop function since multi-lun is supported.
952 // There are three kinds of device handles that might be passed:
953 // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)
954 // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)
955 // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).
957 if (NumberOfChildren
== 0) {
959 // A handle without any children, might be 1st and 2nd type.
961 Status
= gBS
->OpenProtocol (
963 &gEfiBlockIoProtocolGuid
,
965 This
->DriverBindingHandle
,
967 EFI_OPEN_PROTOCOL_GET_PROTOCOL
970 if (EFI_ERROR(Status
)) {
972 // This is a 2nd type handle(multi-lun root), it needs to close devicepath
973 // and usbio protocol.
977 &gEfiDevicePathProtocolGuid
,
978 This
->DriverBindingHandle
,
983 &gEfiUsbIoProtocolGuid
,
984 This
->DriverBindingHandle
,
987 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
992 // This is a 1st type handle(non-multi-lun), which only needs to uninstall
993 // Block I/O Protocol, close USB I/O Protocol and free mass device.
995 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
998 // Uninstall Block I/O protocol from the device handle,
999 // then call the transport protocol to stop itself.
1001 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1003 &gEfiBlockIoProtocolGuid
,
1005 &gEfiDiskInfoProtocolGuid
,
1009 if (EFI_ERROR (Status
)) {
1013 gBS
->CloseProtocol (
1015 &gEfiUsbIoProtocolGuid
,
1016 This
->DriverBindingHandle
,
1020 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1023 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
1028 // This is a 3rd type handle(multi-lun), which needs uninstall
1029 // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and
1030 // free mass device for all children.
1032 AllChildrenStopped
= TRUE
;
1034 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1036 Status
= gBS
->OpenProtocol (
1037 ChildHandleBuffer
[Index
],
1038 &gEfiBlockIoProtocolGuid
,
1040 This
->DriverBindingHandle
,
1042 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1044 if (EFI_ERROR (Status
)) {
1045 AllChildrenStopped
= FALSE
;
1046 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", (UINT32
)Index
));
1050 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
1052 gBS
->CloseProtocol (
1054 &gEfiUsbIoProtocolGuid
,
1055 This
->DriverBindingHandle
,
1056 ChildHandleBuffer
[Index
]
1059 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1060 ChildHandleBuffer
[Index
],
1061 &gEfiDevicePathProtocolGuid
,
1062 UsbMass
->DevicePath
,
1063 &gEfiBlockIoProtocolGuid
,
1065 &gEfiDiskInfoProtocolGuid
,
1070 if (EFI_ERROR (Status
)) {
1072 // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.
1074 AllChildrenStopped
= FALSE
;
1075 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32
)Index
));
1079 &gEfiUsbIoProtocolGuid
,
1081 This
->DriverBindingHandle
,
1082 ChildHandleBuffer
[Index
],
1083 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1087 // Succeed to stop this multi-lun handle, so go on with next child.
1089 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1090 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1096 if (!AllChildrenStopped
) {
1097 return EFI_DEVICE_ERROR
;
1100 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", (UINT32
) NumberOfChildren
));
1105 Entrypoint of USB Mass Storage Driver.
1107 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
1108 Protocol together with Component Name Protocols.
1110 @param ImageHandle The firmware allocated handle for the EFI image.
1111 @param SystemTable A pointer to the EFI System Table.
1113 @retval EFI_SUCCESS The entry point is executed successfully.
1118 USBMassStorageEntryPoint (
1119 IN EFI_HANDLE ImageHandle
,
1120 IN EFI_SYSTEM_TABLE
*SystemTable
1126 // Install driver binding protocol
1128 Status
= EfiLibInstallDriverBindingComponentName2 (
1131 &gUSBMassDriverBinding
,
1133 &gUsbMassStorageComponentName
,
1134 &gUsbMassStorageComponentName2
1136 ASSERT_EFI_ERROR (Status
);