1 /** @file
2   Definition of USB Mass Storage Class and its value, USB Mass Transport Protocol,
3   and other common definitions.
4 
5 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution.  The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10 
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef _EFI_USBMASS_H_
17 #define _EFI_USBMASS_H_
18 
19 
20 #include <Uefi.h>
21 #include <IndustryStandard/Scsi.h>
22 #include <Protocol/BlockIo.h>
23 #include <Protocol/UsbIo.h>
24 #include <Protocol/DevicePath.h>
25 #include <Protocol/DiskInfo.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/DevicePathLib.h>
34 
35 typedef struct _USB_MASS_TRANSPORT USB_MASS_TRANSPORT;
36 typedef struct _USB_MASS_DEVICE    USB_MASS_DEVICE;
37 
38 #include "UsbMassBot.h"
39 #include "UsbMassCbi.h"
40 #include "UsbMassBoot.h"
41 #include "UsbMassDiskInfo.h"
42 #include "UsbMassImpl.h"
43 
44 #define USB_IS_IN_ENDPOINT(EndPointAddr)      (((EndPointAddr) & BIT7) == BIT7)
45 #define USB_IS_OUT_ENDPOINT(EndPointAddr)     (((EndPointAddr) & BIT7) == 0)
46 #define USB_IS_BULK_ENDPOINT(Attribute)       (((Attribute) & (BIT0 | BIT1)) == USB_ENDPOINT_BULK)
47 #define USB_IS_INTERRUPT_ENDPOINT(Attribute)  (((Attribute) & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT)
48 #define USB_IS_ERROR(Result, Error)           (((Result) & (Error)) != 0)
49 
50 #define USB_MASS_1_MILLISECOND  1000
51 #define USB_MASS_1_SECOND       (1000 * USB_MASS_1_MILLISECOND)
52 
53 #define USB_MASS_CMD_SUCCESS    0
54 #define USB_MASS_CMD_FAIL       1
55 #define USB_MASS_CMD_PERSISTENT 2
56 
57 /**
58   Initializes USB transport protocol.
59 
60   This function initializes the USB mass storage class transport protocol.
61   It will save its context in the Context if Context isn't NULL.
62 
63   @param  UsbIo                 The USB I/O Protocol instance
64   @param  Context               The buffer to save the context to
65 
66   @retval EFI_SUCCESS           The device is successfully initialized.
67   @retval EFI_UNSUPPORTED       The transport protocol doesn't support the device.
68   @retval Other                 The USB transport initialization fails.
69 
70 **/
71 typedef
72 EFI_STATUS
73 (*USB_MASS_INIT_TRANSPORT) (
74   IN  EFI_USB_IO_PROTOCOL     *Usb,
75   OUT VOID                    **Context    OPTIONAL
76   );
77 
78 /**
79   Execute USB mass storage command through the transport protocol.
80 
81   @param  Context               The USB Transport Protocol.
82   @param  Cmd                   The command to transfer to device
83   @param  CmdLen                The length of the command
84   @param  DataDir               The direction of data transfer
85   @param  Data                  The buffer to hold the data
86   @param  DataLen               The length of the buffer
87   @param  Lun                   Should be 0, this field for bot only
88   @param  Timeout               The time to wait
89   @param  CmdStatus             The result of the command execution
90 
91   @retval EFI_SUCCESS           The command is executed successfully.
92   @retval Other                 Failed to execute the command
93 
94 **/
95 typedef
96 EFI_STATUS
97 (*USB_MASS_EXEC_COMMAND) (
98   IN  VOID                    *Context,
99   IN  VOID                    *Cmd,
100   IN  UINT8                   CmdLen,
101   IN  EFI_USB_DATA_DIRECTION  DataDir,
102   IN  VOID                    *Data,
103   IN  UINT32                  DataLen,
104   IN  UINT8                   Lun,
105   IN  UINT32                  Timeout,
106   OUT UINT32                  *CmdStatus
107   );
108 
109 /**
110   Reset the USB mass storage device by Transport protocol.
111 
112   @param  Context               The USB Transport Protocol
113   @param  ExtendedVerification  The flag controlling the rule of reset.
114                                 Not used here.
115 
116   @retval EFI_SUCCESS           The device is reset.
117   @retval Others                Failed to reset the device.
118 
119 **/
120 typedef
121 EFI_STATUS
122 (*USB_MASS_RESET) (
123   IN  VOID                    *Context,
124   IN  BOOLEAN                 ExtendedVerification
125   );
126 
127 /**
128   Get the max LUN (Logical Unit Number) of USB mass storage device.
129 
130   @param  Context          The context of the transport protocol.
131   @param  MaxLun           Return pointer to the max number of LUN. (e.g. MaxLun=1 means LUN0 and
132                            LUN1 in all.)
133 
134   @retval EFI_SUCCESS      Max LUN is got successfully.
135   @retval Others           Fail to execute this request.
136 
137 **/
138 typedef
139 EFI_STATUS
140 (*USB_MASS_GET_MAX_LUN) (
141   IN  VOID                    *Context,
142   IN  UINT8                   *MaxLun
143   );
144 
145 /**
146   Clean up the transport protocol's resource.
147 
148   @param  Context               The instance of transport protocol.
149 
150   @retval EFI_SUCCESS           The resource is cleaned up.
151 
152 **/
153 typedef
154 EFI_STATUS
155 (*USB_MASS_CLEAN_UP) (
156   IN  VOID                    *Context
157   );
158 
159 ///
160 /// This structure contains information necessary to select the
161 /// proper transport protocol. The mass storage class defines
162 /// two transport protocols. One is the CBI, and the other is BOT.
163 /// CBI is being obseleted. The design is made modular by this
164 /// structure so that the CBI protocol can be easily removed when
165 /// it is no longer necessary.
166 ///
167 struct _USB_MASS_TRANSPORT {
168   UINT8                   Protocol;
169   USB_MASS_INIT_TRANSPORT Init;        ///< Initialize the mass storage transport protocol
170   USB_MASS_EXEC_COMMAND   ExecCommand; ///< Transport command to the device then get result
171   USB_MASS_RESET          Reset;       ///< Reset the device
172   USB_MASS_GET_MAX_LUN    GetMaxLun;   ///< Get max lun, only for bot
173   USB_MASS_CLEAN_UP       CleanUp;     ///< Clean up the resources.
174 };
175 
176 struct _USB_MASS_DEVICE {
177   UINT32                    Signature;
178   EFI_HANDLE                Controller;
179   EFI_USB_IO_PROTOCOL       *UsbIo;
180   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
181   EFI_BLOCK_IO_PROTOCOL     BlockIo;
182   EFI_BLOCK_IO_MEDIA        BlockIoMedia;
183   BOOLEAN                   OpticalStorage;
184   UINT8                     Lun;          ///< Logical Unit Number
185   UINT8                     Pdt;          ///< Peripheral Device Type
186   USB_MASS_TRANSPORT        *Transport;   ///< USB mass storage transport protocol
187   VOID                      *Context;
188   EFI_DISK_INFO_PROTOCOL    DiskInfo;
189   USB_BOOT_INQUIRY_DATA     InquiryData;
190   BOOLEAN                   Cdb16Byte;
191 };
192 
193 #endif
194