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"
20 USB_MASS_TRANSPORT
*mUsbMassTransport
[] = {
28 Reset the block device. ExtendedVerification is ignored for this.
30 @param This The BLOCK IO protocol
31 @param ExtendedVerification Whether to execute extended verfication.
33 @retval EFI_SUCCESS The device is successfully resetted.
34 @retval Others Failed to reset the device.
40 IN EFI_BLOCK_IO_PROTOCOL
*This
,
41 IN BOOLEAN ExtendedVerification
44 USB_MASS_DEVICE
*UsbMass
;
48 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
50 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
51 Status
= UsbMass
->Transport
->Reset (UsbMass
->Context
, ExtendedVerification
);
53 gBS
->RestoreTPL (OldTpl
);
59 Read some blocks of data from the block device.
61 @param This The Block IO protocol
62 @param MediaId The media's ID of the device for current request
63 @param Lba The start block number
64 @param BufferSize The size of buffer to read data in
65 @param Buffer The buffer to read data to
67 @retval EFI_SUCCESS The data is successfully read
68 @retval EFI_NO_MEDIA Media isn't present
69 @retval EFI_MEDIA_CHANGED The device media has been changed, that is,
71 @retval EFI_INVALID_PARAMETER Some parameters are invalid, such as Buffer is
73 @retval EFI_BAD_BUFFER_SIZE The buffer size isn't a multiple of media's block
74 size, or overflow the last block number.
80 IN EFI_BLOCK_IO_PROTOCOL
*This
,
87 USB_MASS_DEVICE
*UsbMass
;
88 EFI_BLOCK_IO_MEDIA
*Media
;
93 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
94 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
95 Media
= &UsbMass
->BlockIoMedia
;
98 // First, validate the parameters
100 if ((Buffer
== NULL
) || (BufferSize
== 0)) {
101 Status
= EFI_INVALID_PARAMETER
;
106 // If it is a removable media, such as CD-Rom or Usb-Floppy,
107 // need to detect the media before each rw. While some of
108 // Usb-Flash is marked as removable media.
111 if (Media
->RemovableMedia
== TRUE
) {
112 Status
= UsbBootDetectMedia (UsbMass
);
113 if (EFI_ERROR (Status
)) {
114 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootDetectMedia (%r)\n", Status
));
120 // Make sure BlockSize and LBA is consistent with BufferSize
122 if ((BufferSize
% Media
->BlockSize
) != 0) {
123 Status
= EFI_BAD_BUFFER_SIZE
;
127 TotalBlock
= BufferSize
/ Media
->BlockSize
;
129 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
130 Status
= EFI_INVALID_PARAMETER
;
134 if (!(Media
->MediaPresent
)) {
135 Status
= EFI_NO_MEDIA
;
139 if (MediaId
!= Media
->MediaId
) {
140 Status
= EFI_MEDIA_CHANGED
;
144 Status
= UsbBootReadBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
145 if (EFI_ERROR (Status
)) {
146 DEBUG ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
147 UsbMassReset (This
, TRUE
);
151 gBS
->RestoreTPL (OldTpl
);
157 Write some blocks of data to the block device.
159 @param This The Block IO protocol
160 @param MediaId The media's ID of the device for current request
161 @param Lba The start block number
162 @param BufferSize The size of buffer to write data to
163 @param Buffer The buffer to write data to
165 @retval EFI_SUCCESS The data is successfully written
166 @retval EFI_NO_MEDIA Media isn't present
167 @retval EFI_MEDIA_CHANGED The device media has been changed, that is,
169 @retval EFI_INVALID_PARAMETER Some parameters are invalid, such as Buffer is
171 @retval EFI_BAD_BUFFER_SIZE The buffer size isn't a multiple of media's block
178 IN EFI_BLOCK_IO_PROTOCOL
*This
,
185 USB_MASS_DEVICE
*UsbMass
;
186 EFI_BLOCK_IO_MEDIA
*Media
;
191 OldTpl
= gBS
->RaiseTPL (USB_MASS_TPL
);
192 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (This
);
193 Media
= &UsbMass
->BlockIoMedia
;
196 // First, validate the parameters
198 if ((Buffer
== NULL
) || (BufferSize
== 0)) {
199 Status
= EFI_INVALID_PARAMETER
;
204 // If it is a removable media, such as CD-Rom or Usb-Floppy,
205 // need to detect the media before each rw. While some of
206 // Usb-Flash is marked as removable media.
209 if (Media
->RemovableMedia
== TRUE
) {
210 Status
= UsbBootDetectMedia (UsbMass
);
211 if (EFI_ERROR (Status
)) {
212 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootDetectMedia (%r)\n", Status
));
218 // Make sure BlockSize and LBA is consistent with BufferSize
220 if ((BufferSize
% Media
->BlockSize
) != 0) {
221 Status
= EFI_BAD_BUFFER_SIZE
;
225 TotalBlock
= BufferSize
/ Media
->BlockSize
;
227 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
228 Status
= EFI_INVALID_PARAMETER
;
232 if (!(Media
->MediaPresent
)) {
233 Status
= EFI_NO_MEDIA
;
237 if (MediaId
!= Media
->MediaId
) {
238 Status
= EFI_MEDIA_CHANGED
;
243 // Try to write the data even the device is marked as ReadOnly,
244 // and clear the status should the write succeed.
246 Status
= UsbBootWriteBlocks (UsbMass
, (UINT32
) Lba
, TotalBlock
, Buffer
);
247 if (EFI_ERROR (Status
)) {
248 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
249 UsbMassReset (This
, TRUE
);
253 gBS
->RestoreTPL (OldTpl
);
258 Flush the cached writes to disks. USB mass storage device doesn't
259 support write cache, so return EFI_SUCCESS directly.
261 @param This The BLOCK IO protocol
263 @retval EFI_SUCCESS Always returns success
269 IN EFI_BLOCK_IO_PROTOCOL
*This
276 Retrieve the media parameters such as disk gemotric for the
277 device's BLOCK IO protocol.
279 @param UsbMass The USB mass storage device
281 @retval EFI_SUCCESS The media parameters is updated successfully.
282 @retval Others Failed to get the media parameters.
287 IN USB_MASS_DEVICE
*UsbMass
290 EFI_BLOCK_IO_MEDIA
*Media
;
294 Media
= &UsbMass
->BlockIoMedia
;
297 // Initialize the MediaPrsent/ReadOnly and others to the default.
298 // We are not forced to get it right at this time, check UEFI2.0
299 // spec for more information:
301 // MediaPresent: This field shows the media present status as
302 // of the most recent ReadBlocks or WriteBlocks call.
304 // ReadOnly : This field shows the read-only status as of the
305 // recent WriteBlocks call.
307 // but remember to update MediaId/MediaPresent/ReadOnly status
308 // after ReadBlocks and WriteBlocks
310 Media
->MediaPresent
= FALSE
;
311 Media
->LogicalPartition
= FALSE
;
312 Media
->ReadOnly
= FALSE
;
313 Media
->WriteCaching
= FALSE
;
318 // Some device may spend several seconds before it is ready.
319 // Try several times before giving up. Wait 5s at most.
321 Status
= EFI_SUCCESS
;
323 for (Index
= 0; Index
< USB_BOOT_INIT_MEDIA_RETRY
; Index
++) {
325 Status
= UsbBootGetParams (UsbMass
);
326 if ((Status
!= EFI_MEDIA_CHANGED
)
327 && (Status
!= EFI_NOT_READY
)
328 && (Status
!= EFI_TIMEOUT
)) {
332 Status
= UsbBootIsUnitReady (UsbMass
);
333 if (EFI_ERROR (Status
)) {
334 gBS
->Stall (USB_BOOT_RETRY_UNIT_READY_STALL
* (Index
+ 1));
343 Initilize the transport.
345 @param This The USB mass driver's driver binding.
346 @param Controller The device to test.
347 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
348 @param Context The passing parameter.
349 @param MaxLun Get the MaxLun if is BOT dev.
351 @retval EFI_SUCCESS The initilization is successful.
352 @retval Others Failed to initilize dev.
356 UsbMassInitTransport (
357 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
358 IN EFI_HANDLE Controller
,
359 OUT USB_MASS_TRANSPORT
**Transport
,
364 EFI_USB_IO_PROTOCOL
*UsbIo
;
365 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
369 Status
= gBS
->OpenProtocol (
371 &gEfiUsbIoProtocolGuid
,
373 This
->DriverBindingHandle
,
375 EFI_OPEN_PROTOCOL_BY_DRIVER
378 if (EFI_ERROR (Status
)) {
379 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: OpenUsbIoProtocol By Driver (%r)\n", Status
));
383 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
384 if (EFI_ERROR (Status
)) {
385 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: UsbIo->UsbGetInterfaceDescriptor (%r)\n", Status
));
389 Status
= EFI_UNSUPPORTED
;
391 for (Index
= 0; mUsbMassTransport
[Index
] != NULL
; Index
++) {
392 *Transport
= mUsbMassTransport
[Index
];
394 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
395 Status
= (*Transport
)->Init (UsbIo
, Context
);
400 if (EFI_ERROR (Status
)) {
401 DEBUG ((EFI_D_ERROR
, "UsbMassInitTransport: Transport->Init (%r)\n", Status
));
406 // For bot device, try to get max lun.
407 // If maxlun=0, then non-lun device, else multi-lun device.
409 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
410 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
411 DEBUG ((EFI_D_INFO
, "UsbMassInitTransport: GetMaxLun = %d\n", *MaxLun
));
417 &gEfiUsbIoProtocolGuid
,
418 This
->DriverBindingHandle
,
425 Usb mass storage driver initializes multi lun.
427 @param This The USB mass driver's driver binding.
428 @param Controller The device to test.
429 @param Transport The pointer to USB_MASS_TRANSPORT.
430 @param Context The passing parameter.
431 @param DevicePath The remaining device path
432 @param MaxLun The MaxLun number passed.
434 @retval EFI_SUCCESS Initialization is success.
435 @retval Other Initialization fails.
439 UsbMassInitMultiLun (
440 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
441 IN EFI_HANDLE Controller
,
442 IN USB_MASS_TRANSPORT
*Transport
,
444 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
448 USB_MASS_DEVICE
*UsbMass
;
449 EFI_USB_IO_PROTOCOL
*UsbIo
;
450 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
456 for (Index
= 0; Index
<= MaxLun
; Index
++) {
458 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
461 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
462 if (UsbMass
== NULL
) {
463 Status
= EFI_OUT_OF_RESOURCES
;
467 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
468 UsbMass
->UsbIo
= UsbIo
;
469 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
470 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
471 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
472 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
473 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
474 UsbMass
->OpticalStorage
= FALSE
;
475 UsbMass
->Transport
= Transport
;
476 UsbMass
->Context
= Context
;
477 UsbMass
->Lun
= Index
;
480 // Get the storage's parameters, such as last block number.
481 // then install the BLOCK_IO
483 Status
= UsbMassInitMedia (UsbMass
);
484 if (!EFI_ERROR (Status
)) {
485 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
486 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
487 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
488 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
489 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
492 } else if (Status
!= EFI_NO_MEDIA
){
493 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
498 // Create a device path node of device logic unit, and append it
500 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
501 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
502 LunNode
.Lun
= UsbMass
->Lun
;
504 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
506 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
508 if (UsbMass
->DevicePath
== NULL
) {
509 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
511 Status
= EFI_OUT_OF_RESOURCES
;
516 // Create a UsbMass handle for each lun, and install blockio and devicepath protocols.
518 Status
= gBS
->InstallMultipleProtocolInterfaces (
519 &UsbMass
->Controller
,
520 &gEfiDevicePathProtocolGuid
,
522 &gEfiBlockIoProtocolGuid
,
527 if (EFI_ERROR (Status
)) {
528 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
533 // Open UsbIo protocol by child to setup a parent-child relationship.
535 Status
= gBS
->OpenProtocol (
537 &gEfiUsbIoProtocolGuid
,
539 This
->DriverBindingHandle
,
541 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
544 if (EFI_ERROR (Status
)) {
545 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
546 gBS
->UninstallMultipleProtocolInterfaces (
547 &UsbMass
->Controller
,
548 &gEfiDevicePathProtocolGuid
,
550 &gEfiBlockIoProtocolGuid
,
557 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
563 if (UsbMass
->DevicePath
!= NULL
) {
564 gBS
->FreePool (UsbMass
->DevicePath
);
566 if (UsbMass
!= NULL
) {
567 gBS
->FreePool (UsbMass
);
572 &gEfiUsbIoProtocolGuid
,
573 This
->DriverBindingHandle
,
579 // If only success to initialize one lun, return success, or else return error
589 Initialize No/Unsupported LUN device.
591 @param This The USB mass driver's driver binding.
592 @param Controller The device to test.
593 @param Transport The pointer to USB_MASS_TRANSPORT.
594 @param Context The passing parameter.
596 @retval EFI_SUCCESS Initialization is success.
597 @retval Other Initialization fails.
602 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
603 IN EFI_HANDLE Controller
,
604 IN USB_MASS_TRANSPORT
*Transport
,
608 USB_MASS_DEVICE
*UsbMass
;
609 EFI_USB_IO_PROTOCOL
*UsbIo
;
613 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
614 if (UsbMass
== NULL
) {
615 return EFI_OUT_OF_RESOURCES
;
617 Status
= gBS
->OpenProtocol (
619 &gEfiUsbIoProtocolGuid
,
621 This
->DriverBindingHandle
,
623 EFI_OPEN_PROTOCOL_BY_DRIVER
626 if (EFI_ERROR (Status
)) {
627 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
631 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
632 UsbMass
->Controller
= Controller
;
633 UsbMass
->UsbIo
= UsbIo
;
634 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
635 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
636 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
637 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
638 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
639 UsbMass
->OpticalStorage
= FALSE
;
640 UsbMass
->Transport
= Transport
;
641 UsbMass
->Context
= Context
;
644 // Get the storage's parameters, such as last block number.
645 // then install the BLOCK_IO
647 Status
= UsbMassInitMedia (UsbMass
);
648 if (!EFI_ERROR (Status
)) {
649 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
650 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
651 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
652 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
653 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
656 } else if (Status
!= EFI_NO_MEDIA
){
657 DEBUG ((EFI_D_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
661 Status
= gBS
->InstallProtocolInterface (
663 &gEfiBlockIoProtocolGuid
,
664 EFI_NATIVE_INTERFACE
,
667 if (EFI_ERROR (Status
)) {
674 if (UsbMass
!= NULL
) {
675 gBS
->FreePool (UsbMass
);
679 &gEfiUsbIoProtocolGuid
,
680 This
->DriverBindingHandle
,
688 Check whether the controller is a supported USB mass storage.
690 @param This The USB mass driver's driver binding.
691 @param Controller The device to test against.
692 @param RemainingDevicePath The remaining device path
694 @retval EFI_SUCCESS This device is a supported USB mass storage.
695 @retval EFI_UNSUPPORTED The device isn't supported
696 @retval Others Some error happened.
701 USBMassDriverBindingSupported (
702 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
703 IN EFI_HANDLE Controller
,
704 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
707 EFI_USB_IO_PROTOCOL
*UsbIo
;
708 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
709 USB_MASS_TRANSPORT
*Transport
;
714 // Check whether the controlelr support USB_IO
716 Status
= gBS
->OpenProtocol (
718 &gEfiUsbIoProtocolGuid
,
720 This
->DriverBindingHandle
,
722 EFI_OPEN_PROTOCOL_BY_DRIVER
724 if (EFI_ERROR (Status
)) {
729 // Get the interface 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
) {
743 for (Index
= 0; mUsbMassTransport
[Index
] != NULL
; Index
++) {
744 Transport
= mUsbMassTransport
[Index
];
745 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
746 Status
= Transport
->Init (UsbIo
, NULL
);
751 DEBUG ((EFI_D_INFO
, "Found a USB mass store device %r\n", Status
));
756 &gEfiUsbIoProtocolGuid
,
757 This
->DriverBindingHandle
,
766 Start the USB mass storage device on the controller. It will
767 install a BLOCK_IO protocol on the device if everything is OK.
769 @param This The USB mass storage driver binding.
770 @param Controller The USB mass storage device to start on
771 @param RemainingDevicePath The remaining device path.
773 @retval EFI_SUCCESS The driver has started on the device.
774 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory
775 @retval Others Failed to start the driver on the device.
780 USBMassDriverBindingStart (
781 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
782 IN EFI_HANDLE Controller
,
783 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
786 USB_MASS_TRANSPORT
*Transport
;
787 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
797 // Get interface and protocols, initialize transport
799 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
801 if (EFI_ERROR (Status
)) {
802 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
807 // Initialize No/Unsupported LUN device
809 Status
= UsbMassInitNonLun(This
, Controller
, Transport
, Context
);
810 if (EFI_ERROR (Status
)) {
811 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
815 // Open device path to perpare append Device Logic Unit node.
817 Status
= gBS
->OpenProtocol (
819 &gEfiDevicePathProtocolGuid
,
820 (VOID
**) &DevicePath
,
821 This
->DriverBindingHandle
,
823 EFI_OPEN_PROTOCOL_BY_DRIVER
826 if (EFI_ERROR (Status
)) {
827 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
832 // Try best to initialize all LUNs, and return success only if one of LUNs successed to initialized.
834 Status
= UsbMassInitMultiLun(This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
835 if (EFI_ERROR (Status
)) {
838 &gEfiDevicePathProtocolGuid
,
839 This
->DriverBindingHandle
,
842 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
850 Stop controlling the device.
852 @param This The USB mass storage driver binding
853 @param Controller The device controller controlled by the driver.
854 @param NumberOfChildren The number of children of this device
855 @param ChildHandleBuffer The buffer of children handle.
857 @retval EFI_SUCCESS The driver stopped from controlling the device.
858 @retval Others Failed to stop the driver
863 USBMassDriverBindingStop (
864 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
865 IN EFI_HANDLE Controller
,
866 IN UINTN NumberOfChildren
,
867 IN EFI_HANDLE
*ChildHandleBuffer
871 USB_MASS_DEVICE
*UsbMass
;
872 EFI_USB_IO_PROTOCOL
*UsbIo
;
873 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
875 BOOLEAN AllChildrenStopped
;
878 // This a bus driver stop function since multi-lun supported. There are three
879 // kinds of device handle might be passed, 1st is a handle with devicepath/
880 // usbio/blockio installed(non-multi-lun), 2nd is a handle with devicepath/
881 // usbio installed(multi-lun root), 3rd is a handle with devicepath/blockio
882 // installed(multi-lun).
884 if (NumberOfChildren
== 0) {
886 // A handle without any children, might be 1st and 2nd type.
888 Status
= gBS
->OpenProtocol (
890 &gEfiBlockIoProtocolGuid
,
892 This
->DriverBindingHandle
,
894 EFI_OPEN_PROTOCOL_GET_PROTOCOL
897 if (EFI_ERROR(Status
)) {
899 // This is a 2nd type handle(multi-lun root), which only needs close
900 // devicepath protocol.
904 &gEfiDevicePathProtocolGuid
,
905 This
->DriverBindingHandle
,
908 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
913 // This is a 1st type handle(non-multi-lun), which only needs uninstall
914 // blockio protocol, close usbio protocol and free mass device.
916 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (BlockIo
);
919 // Uninstall Block I/O protocol from the device handle,
920 // then call the transport protocol to stop itself.
922 Status
= gBS
->UninstallProtocolInterface (
924 &gEfiBlockIoProtocolGuid
,
927 if (EFI_ERROR (Status
)) {
933 &gEfiUsbIoProtocolGuid
,
934 This
->DriverBindingHandle
,
938 UsbMass
->Transport
->Fini (UsbMass
->Context
);
939 gBS
->FreePool (UsbMass
);
941 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
946 // This is a 3rd type handle(multi-lun), which needs uninstall
947 // blockio and devicepath protocol, close usbio protocol and
950 AllChildrenStopped
= TRUE
;
952 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
954 Status
= gBS
->OpenProtocol (
955 ChildHandleBuffer
[Index
],
956 &gEfiBlockIoProtocolGuid
,
958 This
->DriverBindingHandle
,
960 EFI_OPEN_PROTOCOL_GET_PROTOCOL
962 if (EFI_ERROR (Status
)) {
963 AllChildrenStopped
= FALSE
;
964 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", Index
));
968 UsbMass
= USB_MASS_DEVICE_FROM_BLOCKIO (BlockIo
);
972 &gEfiUsbIoProtocolGuid
,
973 This
->DriverBindingHandle
,
974 ChildHandleBuffer
[Index
]
977 Status
= gBS
->UninstallMultipleProtocolInterfaces (
978 ChildHandleBuffer
[Index
],
979 &gEfiDevicePathProtocolGuid
,
981 &gEfiBlockIoProtocolGuid
,
986 if (EFI_ERROR (Status
)) {
988 // Fail to uninstall blockio and devicepath protocol, so re-open usbio by child.
990 AllChildrenStopped
= FALSE
;
991 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", Index
));
995 &gEfiUsbIoProtocolGuid
,
997 This
->DriverBindingHandle
,
998 ChildHandleBuffer
[Index
],
999 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1003 // Success to stop this multi-lun handle, so go on next child.
1005 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1006 UsbMass
->Transport
->Fini (UsbMass
->Context
);
1008 gBS
->FreePool (UsbMass
);
1012 if (!AllChildrenStopped
) {
1013 return EFI_DEVICE_ERROR
;
1016 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", NumberOfChildren
));
1020 EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding
= {
1021 USBMassDriverBindingSupported
,
1022 USBMassDriverBindingStart
,
1023 USBMassDriverBindingStop
,
1030 The entry point for the driver, which will install the driver binding and
1031 component name protocol.
1033 @param ImageHandle The image handle of this driver.
1034 @param SystemTable The system table.
1036 @retval EFI_SUCCESS The protocols are installed OK.
1037 @retval Others Failed to install protocols.
1042 USBMassStorageEntryPoint (
1043 IN EFI_HANDLE ImageHandle
,
1044 IN EFI_SYSTEM_TABLE
*SystemTable
1050 // Install driver binding protocol
1052 Status
= EfiLibInstallDriverBindingComponentName2 (
1055 &gUSBMassDriverBinding
,
1057 &gUsbMassStorageComponentName
,
1058 &gUsbMassStorageComponentName2