3 The implementation of USB mass storage class device driver.
4 The command set supported is "USB Mass Storage Specification
7 Copyright (c) 2007 - 2008, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "UsbMassImpl.h"
21 USB_MASS_TRANSPORT
*mUsbMassTransport
[] = {
29 Reset the block device. ExtendedVerification is ignored for this.
31 @param This The BLOCK IO protocol
32 @param ExtendedVerification Whether to execute extended verfication.
34 @retval EFI_SUCCESS The device is successfully resetted.
35 @retval Others Failed to reset the device.
41 IN EFI_BLOCK_IO_PROTOCOL
*This
,
42 IN BOOLEAN ExtendedVerification
45 USB_MASS_DEVICE
*UsbMass
;
49 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
51 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
52 Status
= UsbMass
->Transport
->Reset (UsbMass
->Context
, ExtendedVerification
);
54 gBS
->RestoreTPL (OldTpl
);
60 Read some blocks of data from the block device.
62 @param This The Block IO protocol
63 @param MediaId The media's ID of the device for current request
64 @param Lba The start block number
65 @param BufferSize The size of buffer to read data in
66 @param Buffer The buffer to read data to
68 @retval EFI_SUCCESS The data is successfully read
69 @retval EFI_NO_MEDIA Media isn't present
70 @retval EFI_MEDIA_CHANGED The device media has been changed, that is,
72 @retval EFI_INVALID_PARAMETER Some parameters are invalid, such as Buffer is
74 @retval EFI_BAD_BUFFER_SIZE The buffer size isn't a multiple of media's block
75 size, or overflow the last block number.
81 IN EFI_BLOCK_IO_PROTOCOL
*This
,
88 USB_MASS_DEVICE
*UsbMass
;
89 EFI_BLOCK_IO_MEDIA
*Media
;
94 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
95 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
96 Media
= &UsbMass
->BlockIoMedia
;
99 // First, validate the parameters
101 if ((Buffer
== NULL
) || (BufferSize
== 0)) {
102 Status
= EFI_INVALID_PARAMETER
;
107 // If it is a removable media, such as CD-Rom or Usb-Floppy,
108 // need to detect the media before each rw. While some of
109 // Usb-Flash is marked as removable media.
112 if (Media
->RemovableMedia
== TRUE
) {
113 Status
= UsbBootDetectMedia (UsbMass
);
114 if (EFI_ERROR (Status
)) {
115 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootDetectMedia (%r)\n", Status
));
121 // Make sure BlockSize and LBA is consistent with BufferSize
123 if ((BufferSize
% Media
->BlockSize
) != 0) {
124 Status
= EFI_BAD_BUFFER_SIZE
;
128 TotalBlock
= BufferSize
/ Media
->BlockSize
;
130 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
131 Status
= EFI_INVALID_PARAMETER
;
135 if (!(Media
->MediaPresent
)) {
136 Status
= EFI_NO_MEDIA
;
140 if (MediaId
!= Media
->MediaId
) {
141 Status
= EFI_MEDIA_CHANGED
;
145 Status
= UsbBootReadBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
146 if (EFI_ERROR (Status
)) {
147 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
148 UsbMassReset (This
, TRUE
);
152 gBS
->RestoreTPL (OldTpl
);
158 Write some blocks of data to the block device.
160 @param This The Block IO protocol
161 @param MediaId The media's ID of the device for current request
162 @param Lba The start block number
163 @param BufferSize The size of buffer to write data to
164 @param Buffer The buffer to write data to
166 @retval EFI_SUCCESS The data is successfully written
167 @retval EFI_NO_MEDIA Media isn't present
168 @retval EFI_MEDIA_CHANGED The device media has been changed, that is,
170 @retval EFI_INVALID_PARAMETER Some parameters are invalid, such as Buffer is
172 @retval EFI_BAD_BUFFER_SIZE The buffer size isn't a multiple of media's block
179 IN EFI_BLOCK_IO_PROTOCOL
*This
,
186 USB_MASS_DEVICE
*UsbMass
;
187 EFI_BLOCK_IO_MEDIA
*Media
;
192 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
193 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
194 Media
= &UsbMass
->BlockIoMedia
;
197 // First, validate the parameters
199 if ((Buffer
== NULL
) || (BufferSize
== 0)) {
200 Status
= EFI_INVALID_PARAMETER
;
205 // If it is a removable media, such as CD-Rom or Usb-Floppy,
206 // need to detect the media before each rw. While some of
207 // Usb-Flash is marked as removable media.
210 if (Media
->RemovableMedia
== TRUE
) {
211 Status
= UsbBootDetectMedia (UsbMass
);
212 if (EFI_ERROR (Status
)) {
213 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootDetectMedia (%r)\n", Status
));
219 // Make sure BlockSize and LBA is consistent with BufferSize
221 if ((BufferSize
% Media
->BlockSize
) != 0) {
222 Status
= EFI_BAD_BUFFER_SIZE
;
226 TotalBlock
= BufferSize
/ Media
->BlockSize
;
228 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
229 Status
= EFI_INVALID_PARAMETER
;
233 if (!(Media
->MediaPresent
)) {
234 Status
= EFI_NO_MEDIA
;
238 if (MediaId
!= Media
->MediaId
) {
239 Status
= EFI_MEDIA_CHANGED
;
244 // Try to write the data even the device is marked as ReadOnly,
245 // and clear the status should the write succeed.
247 Status
= UsbBootWriteBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
248 if (EFI_ERROR (Status
)) {
249 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
250 UsbMassReset (This
, TRUE
);
254 gBS
->RestoreTPL (OldTpl
);
259 Flush the cached writes to disks. USB mass storage device doesn't
260 support write cache, so return EFI_SUCCESS directly.
262 @param This The BLOCK IO protocol
264 @retval EFI_SUCCESS Always returns success
270 IN EFI_BLOCK_IO_PROTOCOL
*This
277 Retrieve the media parameters such as disk gemotric for the
278 device's BLOCK IO protocol.
280 @param UsbMass The USB mass storage device
282 @retval EFI_SUCCESS The media parameters is updated successfully.
283 @retval Others Failed to get the media parameters.
288 IN USB_MASS_DEVICE
*UsbMass
291 EFI_BLOCK_IO_MEDIA
*Media
;
295 Media
= &UsbMass
->BlockIoMedia
;
298 // Initialize the MediaPrsent/ReadOnly and others to the default.
299 // We are not forced to get it right at this time, check UEFI2.0
300 // spec for more information:
302 // MediaPresent: This field shows the media present status as
303 // of the most recent ReadBlocks or WriteBlocks call.
305 // ReadOnly : This field shows the read-only status as of the
306 // recent WriteBlocks call.
308 // but remember to update MediaId/MediaPresent/ReadOnly status
309 // after ReadBlocks and WriteBlocks
311 Media
->MediaPresent
= FALSE
;
312 Media
->LogicalPartition
= FALSE
;
313 Media
->ReadOnly
= FALSE
;
314 Media
->WriteCaching
= FALSE
;
319 // Some device may spend several seconds before it is ready.
320 // Try several times before giving up. Wait 5s at most.
322 Status
= EFI_SUCCESS
;
324 for (Index
= 0; Index
< USB_BOOT_INIT_MEDIA_RETRY
; Index
++) {
326 Status
= UsbBootGetParams (UsbMass
);
327 if ((Status
!= EFI_MEDIA_CHANGED
)
328 && (Status
!= EFI_NOT_READY
)
329 && (Status
!= EFI_TIMEOUT
)) {
333 Status
= UsbBootIsUnitReady (UsbMass
);
334 if (EFI_ERROR (Status
)) {
335 gBS
->Stall (USB_BOOT_RETRY_UNIT_READY_STALL
* (Index
+ 1));
344 Initilize the transport.
346 @param This The USB mass driver's driver binding.
347 @param Controller The device to test.
348 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
349 @param Context The passing parameter.
350 @param MaxLun Get the MaxLun if is BOT dev.
352 @retval EFI_SUCCESS The initilization is successful.
353 @retval Others Failed to initilize dev.
357 UsbMassInitTransport (
358 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
359 IN EFI_HANDLE Controller
,
360 OUT USB_MASS_TRANSPORT
**Transport
,
365 EFI_USB_IO_PROTOCOL
*UsbIo
;
366 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
370 Status
= gBS
->OpenProtocol (
372 &gEfiUsbIoProtocolGuid
,
374 This
->DriverBindingHandle
,
376 EFI_OPEN_PROTOCOL_BY_DRIVER
379 if (EFI_ERROR (Status
)) {
380 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: OpenUsbIoProtocol By Driver (%r)\n", Status
));
384 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
385 if (EFI_ERROR (Status
)) {
386 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: UsbIo->UsbGetInterfaceDescriptor (%r)\n", Status
));
390 Status
= EFI_UNSUPPORTED
;
392 for (Index
= 0; mUsbMassTransport
[Index
] != NULL
; Index
++) {
393 *Transport
= mUsbMassTransport
[Index
];
395 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
396 Status
= (*Transport
)->Init (UsbIo
, Context
);
401 if (EFI_ERROR (Status
)) {
402 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: Transport->Init (%r)\n", Status
));
407 // For bot device, try to get max lun.
408 // If maxlun=0, then non-lun device, else multi-lun device.
410 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
411 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
412 DEBUG ((EFI_D_INFO
, "UsbMassInitTransport: GetMaxLun = %d\n", *MaxLun
));
418 &gEfiUsbIoProtocolGuid
,
419 This
->DriverBindingHandle
,
426 Usb mass storage driver initializes multi lun.
428 @param This The USB mass driver's driver binding.
429 @param Controller The device to test.
430 @param Transport The pointer to USB_MASS_TRANSPORT.
431 @param Context The passing parameter.
432 @param DevicePath The remaining device path
433 @param MaxLun The MaxLun number passed.
435 @retval EFI_SUCCESS Initialization is success.
436 @retval Other Initialization fails.
440 UsbMassInitMultiLun (
441 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
442 IN EFI_HANDLE Controller
,
443 IN USB_MASS_TRANSPORT
*Transport
,
445 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
449 USB_MASS_DEVICE
*UsbMass
;
450 EFI_USB_IO_PROTOCOL
*UsbIo
;
451 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
457 for (Index
= 0; Index
<= MaxLun
; Index
++) {
459 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
462 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
463 if (UsbMass
== NULL
) {
464 Status
= EFI_OUT_OF_RESOURCES
;
468 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
469 UsbMass
->UsbIo
= UsbIo
;
470 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
471 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
472 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
473 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
474 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
475 UsbMass
->OpticalStorage
= FALSE
;
476 UsbMass
->Transport
= Transport
;
477 UsbMass
->Context
= Context
;
478 UsbMass
->Lun
= Index
;
481 // Get the storage's parameters, such as last block number.
482 // then install the BLOCK_IO
484 Status
= UsbMassInitMedia (UsbMass
);
485 if (!EFI_ERROR (Status
)) {
486 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
487 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
488 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
489 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
490 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
493 } else if (Status
!= EFI_NO_MEDIA
){
494 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
499 // Create a device path node of device logic unit, and append it
501 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
502 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
503 LunNode
.Lun
= UsbMass
->Lun
;
505 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
507 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
509 if (UsbMass
->DevicePath
== NULL
) {
510 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
512 Status
= EFI_OUT_OF_RESOURCES
;
517 // Create a UsbMass handle for each lun, and install blockio and devicepath protocols.
519 Status
= gBS
->InstallMultipleProtocolInterfaces (
520 &UsbMass
->Controller
,
521 &gEfiDevicePathProtocolGuid
,
523 &gEfiBlockIoProtocolGuid
,
528 if (EFI_ERROR (Status
)) {
529 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
534 // Open UsbIo protocol by child to setup a parent-child relationship.
536 Status
= gBS
->OpenProtocol (
538 &gEfiUsbIoProtocolGuid
,
540 This
->DriverBindingHandle
,
542 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
545 if (EFI_ERROR (Status
)) {
546 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
547 gBS
->UninstallMultipleProtocolInterfaces (
548 &UsbMass
->Controller
,
549 &gEfiDevicePathProtocolGuid
,
551 &gEfiBlockIoProtocolGuid
,
558 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
564 if (UsbMass
->DevicePath
!= NULL
) {
565 gBS
->FreePool (UsbMass
->DevicePath
);
567 if (UsbMass
!= NULL
) {
568 gBS
->FreePool (UsbMass
);
573 &gEfiUsbIoProtocolGuid
,
574 This
->DriverBindingHandle
,
580 // If only success to initialize one lun, return success, or else return error
590 Initialize No/Unsupported LUN device.
592 @param This The USB mass driver's driver binding.
593 @param Controller The device to test.
594 @param Transport The pointer to USB_MASS_TRANSPORT.
595 @param Context The passing parameter.
597 @retval EFI_SUCCESS Initialization is success.
598 @retval Other Initialization fails.
603 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
604 IN EFI_HANDLE Controller
,
605 IN USB_MASS_TRANSPORT
*Transport
,
609 USB_MASS_DEVICE
*UsbMass
;
610 EFI_USB_IO_PROTOCOL
*UsbIo
;
614 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
615 if (UsbMass
== NULL
) {
616 return EFI_OUT_OF_RESOURCES
;
618 Status
= gBS
->OpenProtocol (
620 &gEfiUsbIoProtocolGuid
,
622 This
->DriverBindingHandle
,
624 EFI_OPEN_PROTOCOL_BY_DRIVER
627 if (EFI_ERROR (Status
)) {
628 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
632 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
633 UsbMass
->Controller
= Controller
;
634 UsbMass
->UsbIo
= UsbIo
;
635 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
636 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
637 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
638 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
639 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
640 UsbMass
->OpticalStorage
= FALSE
;
641 UsbMass
->Transport
= Transport
;
642 UsbMass
->Context
= Context
;
645 // Get the storage's parameters, such as last block number.
646 // then install the BLOCK_IO
648 Status
= UsbMassInitMedia (UsbMass
);
649 if (!EFI_ERROR (Status
)) {
650 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
651 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
652 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
653 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
654 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
657 } else if (Status
!= EFI_NO_MEDIA
){
658 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
662 Status
= gBS
->InstallProtocolInterface (
664 &gEfiBlockIoProtocolGuid
,
665 EFI_NATIVE_INTERFACE
,
668 if (EFI_ERROR (Status
)) {
675 if (UsbMass
!= NULL
) {
676 gBS
->FreePool (UsbMass
);
680 &gEfiUsbIoProtocolGuid
,
681 This
->DriverBindingHandle
,
689 Check whether the controller is a supported USB mass storage.
691 @param This The USB mass driver's driver binding.
692 @param Controller The device to test against.
693 @param RemainingDevicePath The remaining device path
695 @retval EFI_SUCCESS This device is a supported USB mass storage.
696 @retval EFI_UNSUPPORTED The device isn't supported
697 @retval Others Some error happened.
702 USBMassDriverBindingSupported (
703 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
704 IN EFI_HANDLE Controller
,
705 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
708 EFI_USB_IO_PROTOCOL
*UsbIo
;
709 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
710 USB_MASS_TRANSPORT
*Transport
;
715 // Check whether the controlelr support USB_IO
717 Status
= gBS
->OpenProtocol (
719 &gEfiUsbIoProtocolGuid
,
721 This
->DriverBindingHandle
,
723 EFI_OPEN_PROTOCOL_BY_DRIVER
725 if (EFI_ERROR (Status
)) {
730 // Get the interface to check the USB class and find a transport
733 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
734 if (EFI_ERROR (Status
)) {
738 Status
= EFI_UNSUPPORTED
;
740 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
744 for (Index
= 0; mUsbMassTransport
[Index
] != NULL
; Index
++) {
745 Transport
= mUsbMassTransport
[Index
];
746 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
747 Status
= Transport
->Init (UsbIo
, NULL
);
752 DEBUG ((EFI_D_INFO
, "Found a USB mass store device %r\n", Status
));
757 &gEfiUsbIoProtocolGuid
,
758 This
->DriverBindingHandle
,
767 Start the USB mass storage device on the controller. It will
768 install a BLOCK_IO protocol on the device if everything is OK.
770 @param This The USB mass storage driver binding.
771 @param Controller The USB mass storage device to start on
772 @param RemainingDevicePath The remaining device path.
774 @retval EFI_SUCCESS The driver has started on the device.
775 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory
776 @retval Others Failed to start the driver on the device.
781 USBMassDriverBindingStart (
782 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
783 IN EFI_HANDLE Controller
,
784 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
787 USB_MASS_TRANSPORT
*Transport
;
788 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
798 // Get interface and protocols, initialize transport
800 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
802 if (EFI_ERROR (Status
)) {
803 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
808 // Initialize No/Unsupported LUN device
810 Status
= UsbMassInitNonLun(This
, Controller
, Transport
, Context
);
811 if (EFI_ERROR (Status
)) {
812 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
816 // Open device path to perpare append Device Logic Unit node.
818 Status
= gBS
->OpenProtocol (
820 &gEfiDevicePathProtocolGuid
,
821 (VOID
**) &DevicePath
,
822 This
->DriverBindingHandle
,
824 EFI_OPEN_PROTOCOL_BY_DRIVER
827 if (EFI_ERROR (Status
)) {
828 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
833 // Try best to initialize all LUNs, and return success only if one of LUNs successed to initialized.
835 Status
= UsbMassInitMultiLun(This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
836 if (EFI_ERROR (Status
)) {
839 &gEfiDevicePathProtocolGuid
,
840 This
->DriverBindingHandle
,
843 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
851 Stop controlling the device.
853 @param This The USB mass storage driver binding
854 @param Controller The device controller controlled by the driver.
855 @param NumberOfChildren The number of children of this device
856 @param ChildHandleBuffer The buffer of children handle.
858 @retval EFI_SUCCESS The driver stopped from controlling the device.
859 @retval Others Failed to stop the driver
864 USBMassDriverBindingStop (
865 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
866 IN EFI_HANDLE Controller
,
867 IN UINTN NumberOfChildren
,
868 IN EFI_HANDLE
*ChildHandleBuffer
872 USB_MASS_DEVICE
*UsbMass
;
873 EFI_USB_IO_PROTOCOL
*UsbIo
;
874 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
876 BOOLEAN AllChildrenStopped
;
879 // This a bus driver stop function since multi-lun supported. There are three
880 // kinds of device handle might be passed, 1st is a handle with devicepath/
881 // usbio/blockio installed(non-multi-lun), 2nd is a handle with devicepath/
882 // usbio installed(multi-lun root), 3rd is a handle with devicepath/blockio
883 // installed(multi-lun).
885 if (NumberOfChildren
== 0) {
887 // A handle without any children, might be 1st and 2nd type.
889 Status
= gBS
->OpenProtocol (
891 &gEfiBlockIoProtocolGuid
,
893 This
->DriverBindingHandle
,
895 EFI_OPEN_PROTOCOL_GET_PROTOCOL
898 if (EFI_ERROR(Status
)) {
900 // This is a 2nd type handle(multi-lun root), which only needs close
901 // devicepath protocol.
905 &gEfiDevicePathProtocolGuid
,
906 This
->DriverBindingHandle
,
909 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
914 // This is a 1st type handle(non-multi-lun), which only needs uninstall
915 // blockio protocol, close usbio protocol and free mass device.
917 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (BlockIo
);
920 // Uninstall Block I/O protocol from the device handle,
921 // then call the transport protocol to stop itself.
923 Status
= gBS
->UninstallProtocolInterface (
925 &gEfiBlockIoProtocolGuid
,
928 if (EFI_ERROR (Status
)) {
934 &gEfiUsbIoProtocolGuid
,
935 This
->DriverBindingHandle
,
939 UsbMass
->Transport
->Fini (UsbMass
->Context
);
940 gBS
->FreePool (UsbMass
);
942 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
947 // This is a 3rd type handle(multi-lun), which needs uninstall
948 // blockio and devicepath protocol, close usbio protocol and
951 AllChildrenStopped
= TRUE
;
953 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
955 Status
= gBS
->OpenProtocol (
956 ChildHandleBuffer
[Index
],
957 &gEfiBlockIoProtocolGuid
,
959 This
->DriverBindingHandle
,
961 EFI_OPEN_PROTOCOL_GET_PROTOCOL
963 if (EFI_ERROR (Status
)) {
964 AllChildrenStopped
= FALSE
;
965 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", Index
));
969 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (BlockIo
);
973 &gEfiUsbIoProtocolGuid
,
974 This
->DriverBindingHandle
,
975 ChildHandleBuffer
[Index
]
978 Status
= gBS
->UninstallMultipleProtocolInterfaces (
979 ChildHandleBuffer
[Index
],
980 &gEfiDevicePathProtocolGuid
,
982 &gEfiBlockIoProtocolGuid
,
987 if (EFI_ERROR (Status
)) {
989 // Fail to uninstall blockio and devicepath protocol, so re-open usbio by child.
991 AllChildrenStopped
= FALSE
;
992 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", Index
));
996 &gEfiUsbIoProtocolGuid
,
998 This
->DriverBindingHandle
,
999 ChildHandleBuffer
[Index
],
1000 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1004 // Success to stop this multi-lun handle, so go on next child.
1006 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1007 UsbMass
->Transport
->Fini (UsbMass
->Context
);
1009 gBS
->FreePool (UsbMass
);
1013 if (!AllChildrenStopped
) {
1014 return EFI_DEVICE_ERROR
;
1017 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", NumberOfChildren
));
1021 EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding
= {
1022 USBMassDriverBindingSupported
,
1023 USBMassDriverBindingStart
,
1024 USBMassDriverBindingStop
,
1031 The entry point for the driver, which will install the driver binding and
1032 component name protocol.
1034 @param ImageHandle The image handle of this driver.
1035 @param SystemTable The system table.
1037 @retval EFI_SUCCESS The protocols are installed OK.
1038 @retval Others Failed to install protocols.
1043 USBMassStorageEntryPoint (
1044 IN EFI_HANDLE ImageHandle
,
1045 IN EFI_SYSTEM_TABLE
*SystemTable
1051 // Install driver binding protocol
1053 Status
= EfiLibInstallDriverBindingComponentName2 (
1056 &gUSBMassDriverBinding
,
1058 &gUsbMassStorageComponentName
,
1059 &gUsbMassStorageComponentName2