2 USB Mass Storage Driver that manages USB Mass Storage Device and produces Block I/O Protocol.
4 Copyright (c) 2007 - 2015, 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_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_NOTIFY 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
= UsbBootReadBlocks16 (UsbMass
, Lba
, TotalBlock
, Buffer
);
177 Status
= UsbBootReadBlocks (UsbMass
, (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_NOTIFY 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
= UsbBootWriteBlocks16 (UsbMass
, Lba
, TotalBlock
, Buffer
);
297 Status
= UsbBootWriteBlocks (UsbMass
, (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
);
368 Initilize the USB Mass Storage transport.
370 This function tries to find the matching USB Mass Storage transport
371 protocol for USB device. If found, initializes the matching transport.
373 @param This The USB mass driver's driver binding.
374 @param Controller The device to test.
375 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
376 @param Context The parameter for USB_MASS_DEVICE.Context.
377 @param MaxLun Get the MaxLun if is BOT dev.
379 @retval EFI_SUCCESS The initialization is successful.
380 @retval EFI_UNSUPPORTED No matching transport protocol is found.
381 @retval Others Failed to initialize dev.
385 UsbMassInitTransport (
386 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
387 IN EFI_HANDLE Controller
,
388 OUT USB_MASS_TRANSPORT
**Transport
,
393 EFI_USB_IO_PROTOCOL
*UsbIo
;
394 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
398 Status
= gBS
->OpenProtocol (
400 &gEfiUsbIoProtocolGuid
,
402 This
->DriverBindingHandle
,
404 EFI_OPEN_PROTOCOL_BY_DRIVER
407 if (EFI_ERROR (Status
)) {
411 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
412 if (EFI_ERROR (Status
)) {
416 Status
= EFI_UNSUPPORTED
;
419 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
420 // matching transport protocol.
421 // If not found, return EFI_UNSUPPORTED.
422 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
424 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
425 *Transport
= mUsbMassTransport
[Index
];
427 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
428 Status
= (*Transport
)->Init (UsbIo
, Context
);
433 if (EFI_ERROR (Status
)) {
438 // For BOT device, try to get its max LUN.
439 // If max LUN is 0, then it is a non-lun device.
440 // Otherwise, it is a multi-lun device.
442 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
443 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
449 &gEfiUsbIoProtocolGuid
,
450 This
->DriverBindingHandle
,
457 Initialize data for device that supports multiple LUNSs.
459 @param This The Driver Binding Protocol instance.
460 @param Controller The device to initialize.
461 @param Transport Pointer to USB_MASS_TRANSPORT.
462 @param Context Parameter for USB_MASS_DEVICE.Context.
463 @param DevicePath The remaining device path.
464 @param MaxLun The max LUN number.
466 @retval EFI_SUCCESS At least one LUN is initialized successfully.
467 @retval EFI_NOT_FOUND Fail to initialize any of multiple LUNs.
471 UsbMassInitMultiLun (
472 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
473 IN EFI_HANDLE Controller
,
474 IN USB_MASS_TRANSPORT
*Transport
,
476 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
480 USB_MASS_DEVICE
*UsbMass
;
481 EFI_USB_IO_PROTOCOL
*UsbIo
;
482 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
485 EFI_STATUS ReturnStatus
;
488 ReturnStatus
= EFI_NOT_FOUND
;
490 for (Index
= 0; Index
<= MaxLun
; Index
++) {
492 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
495 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
496 ASSERT (UsbMass
!= NULL
);
498 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
499 UsbMass
->UsbIo
= UsbIo
;
500 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
501 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
502 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
503 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
504 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
505 UsbMass
->OpticalStorage
= FALSE
;
506 UsbMass
->Transport
= Transport
;
507 UsbMass
->Context
= Context
;
508 UsbMass
->Lun
= Index
;
511 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
513 Status
= UsbMassInitMedia (UsbMass
);
514 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
515 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"));
533 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
));
556 FreePool (UsbMass
->DevicePath
);
562 // Open USB I/O Protocol by child to setup a parent-child relationship.
564 Status
= gBS
->OpenProtocol (
566 &gEfiUsbIoProtocolGuid
,
568 This
->DriverBindingHandle
,
570 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
573 if (EFI_ERROR (Status
)) {
574 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
575 gBS
->UninstallMultipleProtocolInterfaces (
576 &UsbMass
->Controller
,
577 &gEfiDevicePathProtocolGuid
,
579 &gEfiBlockIoProtocolGuid
,
581 &gEfiDiskInfoProtocolGuid
,
585 FreePool (UsbMass
->DevicePath
);
589 ReturnStatus
= EFI_SUCCESS
;
590 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
597 Initialize data for device that does not support multiple LUNSs.
599 @param This The Driver Binding Protocol instance.
600 @param Controller The device to initialize.
601 @param Transport Pointer to USB_MASS_TRANSPORT.
602 @param Context Parameter for USB_MASS_DEVICE.Context.
604 @retval EFI_SUCCESS Initialization succeeds.
605 @retval Other Initialization fails.
610 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
611 IN EFI_HANDLE Controller
,
612 IN USB_MASS_TRANSPORT
*Transport
,
616 USB_MASS_DEVICE
*UsbMass
;
617 EFI_USB_IO_PROTOCOL
*UsbIo
;
621 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
622 ASSERT (UsbMass
!= NULL
);
624 Status
= gBS
->OpenProtocol (
626 &gEfiUsbIoProtocolGuid
,
628 This
->DriverBindingHandle
,
630 EFI_OPEN_PROTOCOL_BY_DRIVER
633 if (EFI_ERROR (Status
)) {
634 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
638 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
639 UsbMass
->Controller
= Controller
;
640 UsbMass
->UsbIo
= UsbIo
;
641 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
642 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
643 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
644 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
645 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
646 UsbMass
->OpticalStorage
= FALSE
;
647 UsbMass
->Transport
= Transport
;
648 UsbMass
->Context
= Context
;
651 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
653 Status
= UsbMassInitMedia (UsbMass
);
654 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
655 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
659 InitializeDiskInfo (UsbMass
);
661 Status
= gBS
->InstallMultipleProtocolInterfaces (
663 &gEfiBlockIoProtocolGuid
,
665 &gEfiDiskInfoProtocolGuid
,
669 if (EFI_ERROR (Status
)) {
676 if (UsbMass
!= NULL
) {
682 &gEfiUsbIoProtocolGuid
,
683 This
->DriverBindingHandle
,
692 Check whether the controller is a supported USB mass storage.
694 @param This The USB mass storage driver binding protocol.
695 @param Controller The controller handle to check.
696 @param RemainingDevicePath The remaining device path.
698 @retval EFI_SUCCESS The driver supports this controller.
699 @retval other This device isn't supported.
704 USBMassDriverBindingSupported (
705 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
706 IN EFI_HANDLE Controller
,
707 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
710 EFI_USB_IO_PROTOCOL
*UsbIo
;
711 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
712 USB_MASS_TRANSPORT
*Transport
;
716 Status
= gBS
->OpenProtocol (
718 &gEfiUsbIoProtocolGuid
,
720 This
->DriverBindingHandle
,
722 EFI_OPEN_PROTOCOL_BY_DRIVER
724 if (EFI_ERROR (Status
)) {
729 // Get the interface descriptor to check the USB class and find a transport
732 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
733 if (EFI_ERROR (Status
)) {
737 Status
= EFI_UNSUPPORTED
;
739 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
744 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
745 // matching transport method.
746 // If not found, return EFI_UNSUPPORTED.
747 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
749 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
750 Transport
= mUsbMassTransport
[Index
];
751 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
752 Status
= Transport
->Init (UsbIo
, NULL
);
760 &gEfiUsbIoProtocolGuid
,
761 This
->DriverBindingHandle
,
769 Starts the USB mass storage device with this driver.
771 This function consumes USB I/O Portocol, intializes USB mass storage device,
772 installs Block I/O Protocol, and submits Asynchronous Interrupt
773 Transfer to manage the USB mass storage device.
775 @param This The USB mass storage driver binding protocol.
776 @param Controller The USB mass storage device to start on
777 @param RemainingDevicePath The remaining device path.
779 @retval EFI_SUCCESS This driver supports this device.
780 @retval EFI_UNSUPPORTED This driver does not support this device.
781 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
782 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
783 @retval EFI_ALREADY_STARTED This driver has been started.
788 USBMassDriverBindingStart (
789 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
790 IN EFI_HANDLE Controller
,
791 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
794 USB_MASS_TRANSPORT
*Transport
;
795 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
799 EFI_USB_IO_PROTOCOL
*UsbIo
;
802 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
808 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
810 if (EFI_ERROR (Status
)) {
811 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
816 // Initialize data for device that does not support multiple LUNSs.
818 Status
= UsbMassInitNonLun (This
, Controller
, Transport
, Context
);
819 if (EFI_ERROR (Status
)) {
820 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
824 // Open device path to prepare for appending Device Logic Unit node.
826 Status
= gBS
->OpenProtocol (
828 &gEfiDevicePathProtocolGuid
,
829 (VOID
**) &DevicePath
,
830 This
->DriverBindingHandle
,
832 EFI_OPEN_PROTOCOL_BY_DRIVER
835 if (EFI_ERROR (Status
)) {
836 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
840 Status
= gBS
->OpenProtocol (
842 &gEfiUsbIoProtocolGuid
,
844 This
->DriverBindingHandle
,
846 EFI_OPEN_PROTOCOL_BY_DRIVER
849 if (EFI_ERROR (Status
)) {
850 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status
));
853 &gEfiDevicePathProtocolGuid
,
854 This
->DriverBindingHandle
,
861 // Initialize data for device that supports multiple LUNs.
862 // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.
864 Status
= UsbMassInitMultiLun (This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
865 if (EFI_ERROR (Status
)) {
868 &gEfiDevicePathProtocolGuid
,
869 This
->DriverBindingHandle
,
874 &gEfiUsbIoProtocolGuid
,
875 This
->DriverBindingHandle
,
878 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
882 gBS
->RestoreTPL (OldTpl
);
888 Stop controlling the device.
890 @param This The USB mass storage driver binding
891 @param Controller The device controller controlled by the driver.
892 @param NumberOfChildren The number of children of this device
893 @param ChildHandleBuffer The buffer of children handle.
895 @retval EFI_SUCCESS The driver stopped from controlling the device.
896 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
897 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
898 @retval Others Failed to stop the driver
903 USBMassDriverBindingStop (
904 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
905 IN EFI_HANDLE Controller
,
906 IN UINTN NumberOfChildren
,
907 IN EFI_HANDLE
*ChildHandleBuffer
911 USB_MASS_DEVICE
*UsbMass
;
912 EFI_USB_IO_PROTOCOL
*UsbIo
;
913 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
915 BOOLEAN AllChildrenStopped
;
918 // This is a bus driver stop function since multi-lun is supported.
919 // There are three kinds of device handles that might be passed:
920 // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)
921 // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)
922 // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).
924 if (NumberOfChildren
== 0) {
926 // A handle without any children, might be 1st and 2nd type.
928 Status
= gBS
->OpenProtocol (
930 &gEfiBlockIoProtocolGuid
,
932 This
->DriverBindingHandle
,
934 EFI_OPEN_PROTOCOL_GET_PROTOCOL
937 if (EFI_ERROR(Status
)) {
939 // This is a 2nd type handle(multi-lun root), it needs to close devicepath
940 // and usbio protocol.
944 &gEfiDevicePathProtocolGuid
,
945 This
->DriverBindingHandle
,
950 &gEfiUsbIoProtocolGuid
,
951 This
->DriverBindingHandle
,
954 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
959 // This is a 1st type handle(non-multi-lun), which only needs to uninstall
960 // Block I/O Protocol, close USB I/O Protocol and free mass device.
962 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
965 // Uninstall Block I/O protocol from the device handle,
966 // then call the transport protocol to stop itself.
968 Status
= gBS
->UninstallMultipleProtocolInterfaces (
970 &gEfiBlockIoProtocolGuid
,
972 &gEfiDiskInfoProtocolGuid
,
976 if (EFI_ERROR (Status
)) {
982 &gEfiUsbIoProtocolGuid
,
983 This
->DriverBindingHandle
,
987 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
990 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
995 // This is a 3rd type handle(multi-lun), which needs uninstall
996 // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and
997 // free mass device for all children.
999 AllChildrenStopped
= TRUE
;
1001 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1003 Status
= gBS
->OpenProtocol (
1004 ChildHandleBuffer
[Index
],
1005 &gEfiBlockIoProtocolGuid
,
1007 This
->DriverBindingHandle
,
1009 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1011 if (EFI_ERROR (Status
)) {
1012 AllChildrenStopped
= FALSE
;
1013 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", (UINT32
)Index
));
1017 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
1019 gBS
->CloseProtocol (
1021 &gEfiUsbIoProtocolGuid
,
1022 This
->DriverBindingHandle
,
1023 ChildHandleBuffer
[Index
]
1026 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1027 ChildHandleBuffer
[Index
],
1028 &gEfiDevicePathProtocolGuid
,
1029 UsbMass
->DevicePath
,
1030 &gEfiBlockIoProtocolGuid
,
1032 &gEfiDiskInfoProtocolGuid
,
1037 if (EFI_ERROR (Status
)) {
1039 // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.
1041 AllChildrenStopped
= FALSE
;
1042 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32
)Index
));
1046 &gEfiUsbIoProtocolGuid
,
1048 This
->DriverBindingHandle
,
1049 ChildHandleBuffer
[Index
],
1050 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1054 // Succeed to stop this multi-lun handle, so go on with next child.
1056 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1057 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1063 if (!AllChildrenStopped
) {
1064 return EFI_DEVICE_ERROR
;
1067 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", (UINT32
) NumberOfChildren
));
1072 Entrypoint of USB Mass Storage Driver.
1074 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
1075 Protocol together with Component Name Protocols.
1077 @param ImageHandle The firmware allocated handle for the EFI image.
1078 @param SystemTable A pointer to the EFI System Table.
1080 @retval EFI_SUCCESS The entry point is executed successfully.
1085 USBMassStorageEntryPoint (
1086 IN EFI_HANDLE ImageHandle
,
1087 IN EFI_SYSTEM_TABLE
*SystemTable
1093 // Install driver binding protocol
1095 Status
= EfiLibInstallDriverBindingComponentName2 (
1098 &gUSBMassDriverBinding
,
1100 &gUsbMassStorageComponentName
,
1101 &gUsbMassStorageComponentName2
1103 ASSERT_EFI_ERROR (Status
);