]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Host/MassStorageClassHost.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Host / MassStorageClassHost.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief Host mode driver for the library USB Mass Storage Class driver.\r
33  *\r
34  *  Host mode driver for the library USB Mass Storage Class driver.\r
35  *\r
36  *  \note This file should not be included directly. It is automatically included as needed by the USB module driver\r
37  *        dispatch header located in LUFA/Drivers/USB.h.\r
38  */\r
39 \r
40 /** \ingroup Group_USBClassMS\r
41  *  \defgroup Group_USBClassMassStorageHost Mass Storage Class Host Mode Driver\r
42  *\r
43  *  \section Sec_Dependencies Module Source Dependencies\r
44  *  The following files must be built with any user project that uses this module:\r
45  *    - LUFA/Drivers/USB/Class/Host/MassStorageClassHost.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>\r
46  *\r
47  *  \section Sec_ModDescription Module Description\r
48  *  Host Mode USB Class driver framework interface, for the Mass Storage USB Class driver.\r
49  *\r
50  *  @{\r
51  */\r
52 \r
53 #ifndef __MS_CLASS_HOST_H__\r
54 #define __MS_CLASS_HOST_H__\r
55 \r
56         /* Includes: */\r
57                 #include "../../USB.h"\r
58                 #include "../Common/MassStorageClassCommon.h"\r
59 \r
60         /* Enable C linkage for C++ Compilers: */\r
61                 #if defined(__cplusplus)\r
62                         extern "C" {\r
63                 #endif\r
64 \r
65         /* Preprocessor Checks: */\r
66                 #if !defined(__INCLUDE_FROM_MS_DRIVER)\r
67                         #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.\r
68                 #endif\r
69 \r
70         /* Public Interface - May be used in end-application: */\r
71                 /* Macros: */\r
72                         /** Error code for some Mass Storage Host functions, indicating a logical (and not hardware) error. */\r
73                         #define MS_ERROR_LOGICAL_CMD_FAILED              0x80\r
74 \r
75                 /* Type Defines: */\r
76                         /** \brief Mass Storage Class Host Mode Configuration and State Structure.\r
77                          *\r
78                          *  Class state structure. An instance of this structure should be made within the user application,\r
79                          *  and passed to each of the Mass Storage class driver functions as the \c MSInterfaceInfo parameter. This\r
80                          *  stores each Mass Storage interface's configuration and state information.\r
81                          */\r
82                         typedef struct\r
83                         {\r
84                                 struct\r
85                                 {\r
86                                         USB_Pipe_Table_t DataINPipe; /**< Data IN Pipe configuration table. */\r
87                                         USB_Pipe_Table_t DataOUTPipe; /**< Data OUT Pipe configuration table. */\r
88                                 } Config; /**< Config data for the USB class interface within the device. All elements in this section\r
89                                            *   <b>must</b> be set or the interface will fail to enumerate and operate correctly.\r
90                                            */\r
91                                 struct\r
92                                 {\r
93                                         bool     IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid\r
94                                                             *   after \ref MS_Host_ConfigurePipes() is called and the Host state machine is in the\r
95                                                             *   Configured state.\r
96                                                             */\r
97                                         uint8_t  InterfaceNumber; /**< Interface index of the Mass Storage interface within the attached device. */\r
98 \r
99                                         uint32_t TransactionTag; /**< Current transaction tag for data synchronizing of packets. */\r
100                                 } State; /**< State data for the USB class interface within the device. All elements in this section\r
101                                                   *   <b>may</b> be set to initial values, but may also be ignored to default to sane values when\r
102                                                   *   the interface is enumerated.\r
103                                                   */\r
104                         } USB_ClassInfo_MS_Host_t;\r
105 \r
106                         /** \brief SCSI Device LUN Capacity Structure.\r
107                          *\r
108                          *  SCSI capacity structure, to hold the total capacity of the device in both the number\r
109                          *  of blocks in the current LUN, and the size of each block. This structure is filled by\r
110                          *  the device when the \ref MS_Host_ReadDeviceCapacity() function is called.\r
111                          */\r
112                         typedef struct\r
113                         {\r
114                                 uint32_t Blocks; /**< Number of blocks in the addressed LUN of the device. */\r
115                                 uint32_t BlockSize; /**< Number of bytes in each block in the addressed LUN. */\r
116                         } SCSI_Capacity_t;\r
117 \r
118                 /* Enums: */\r
119                         /** Enum for the possible error codes returned by the \ref MS_Host_ConfigurePipes() function. */\r
120                         enum MS_Host_EnumerationFailure_ErrorCodes_t\r
121                         {\r
122                                 MS_ENUMERROR_NoError                    = 0, /**< Configuration Descriptor was processed successfully. */\r
123                                 MS_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor. */\r
124                                 MS_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible Mass Storage interface was not found in the device's Configuration Descriptor. */\r
125                                 MS_ENUMERROR_PipeConfigurationFailed    = 3, /**< One or more pipes for the specified interface could not be configured correctly. */\r
126                         };\r
127 \r
128                 /* Function Prototypes: */\r
129                         /** Host interface configuration routine, to configure a given Mass Storage host interface instance using the\r
130                          *  Configuration Descriptor read from an attached USB device. This function automatically updates the given Mass\r
131                          *  Storage Host instance's state values and configures the pipes required to communicate with the interface if it\r
132                          *  is found within the device. This should be called once after the stack has enumerated the attached device, while\r
133                          *  the host state machine is in the Addressed state.\r
134                          *\r
135                          *  \param[in,out] MSInterfaceInfo         Pointer to a structure containing an MS Class host configuration and state.\r
136                          *  \param[in]     ConfigDescriptorSize    Length of the attached device's Configuration Descriptor.\r
137                          *  \param[in]     DeviceConfigDescriptor  Pointer to a buffer containing the attached device's Configuration Descriptor.\r
138                          *\r
139                          *  \return A value from the \ref MS_Host_EnumerationFailure_ErrorCodes_t enum.\r
140                          */\r
141                         uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
142                                                        uint16_t ConfigDescriptorSize,\r
143                                                        void* DeviceConfigDescriptor) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);\r
144 \r
145                         /** Sends a MASS STORAGE RESET control request to the attached device, resetting the Mass Storage Interface\r
146                          *  and readying it for the next Mass Storage command. This should be called after a failed SCSI request to\r
147                          *  ensure the attached Mass Storage device is ready to receive the next command.\r
148                          *\r
149                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
150                          *\r
151                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.\r
152                          */\r
153                         uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);\r
154 \r
155                         /** Sends a GET MAX LUN control request to the attached device, retrieving the index of the highest LUN (Logical\r
156                          *  UNit, a logical drive) in the device. This value can then be used in the other functions of the Mass Storage\r
157                          *  Host mode Class driver to address a specific LUN within the device.\r
158                          *\r
159                          *  \note Some devices do not support this request, and will STALL it when issued. To get around this,\r
160                          *        on unsupported devices the max LUN index will be reported as zero and no error will be returned\r
161                          *        if the device STALLs the request.\r
162                          *\r
163                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
164                          *  \param[out]    MaxLUNIndex      Pointer to a location where the highest LUN index value should be stored.\r
165                          *\r
166                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.\r
167                          */\r
168                         uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
169                                                   uint8_t* const MaxLUNIndex) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);\r
170 \r
171                         /** Retrieves the Mass Storage device's inquiry data for the specified LUN, indicating the device characteristics and\r
172                          *  properties.\r
173                          *\r
174                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
175                          *       call will fail.\r
176                          *\r
177                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
178                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
179                          *  \param[out]    InquiryData      Location where the read inquiry data should be stored.\r
180                          *\r
181                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED.\r
182                          */\r
183                         uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
184                                                        const uint8_t LUNIndex,\r
185                                                        SCSI_Inquiry_Response_t* const InquiryData) ATTR_NON_NULL_PTR_ARG(1)\r
186                                                        ATTR_NON_NULL_PTR_ARG(3);\r
187 \r
188                         /** Sends a TEST UNIT READY command to the device, to determine if it is ready to accept other SCSI commands.\r
189                          *\r
190                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
191                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
192                          *\r
193                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
194                          */\r
195                         uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
196                                                       const uint8_t LUNIndex) ATTR_NON_NULL_PTR_ARG(1);\r
197 \r
198                         /** Retrieves the total capacity of the attached USB Mass Storage device, in blocks, and block size.\r
199                          *\r
200                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
201                          *       call will fail.\r
202                          *\r
203                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
204                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
205                          *  \param[out]    DeviceCapacity   Pointer to the location where the capacity information should be stored.\r
206                          *\r
207                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
208                          */\r
209                         uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
210                                                            const uint8_t LUNIndex,\r
211                                                            SCSI_Capacity_t* const DeviceCapacity) ATTR_NON_NULL_PTR_ARG(1)\r
212                                                            ATTR_NON_NULL_PTR_ARG(3);\r
213 \r
214                         /** Retrieves the device sense data, indicating the current device state and error codes for the previously\r
215                          *  issued command.\r
216                          *\r
217                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
218                          *       call will fail.\r
219                          *\r
220                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
221                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
222                          *  \param[out]    SenseData        Pointer to the location where the sense information should be stored.\r
223                          *\r
224                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
225                          */\r
226                         uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
227                                                      const uint8_t LUNIndex,\r
228                                                      SCSI_Request_Sense_Response_t* const SenseData) ATTR_NON_NULL_PTR_ARG(1)\r
229                                                      ATTR_NON_NULL_PTR_ARG(3);\r
230 \r
231                         /** Issues a PREVENT MEDIUM REMOVAL command, to logically (or, depending on the type of device, physically) lock\r
232                          *  the device from removal so that blocks of data on the medium can be read or altered.\r
233                          *\r
234                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
235                          *       call will fail.\r
236                          *\r
237                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
238                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
239                          *  \param[in]     PreventRemoval   Boolean \c true if the device should be locked from removal, \c false otherwise.\r
240                          *\r
241                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
242                          */\r
243                         uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
244                                                                   const uint8_t LUNIndex,\r
245                                                                   const bool PreventRemoval) ATTR_NON_NULL_PTR_ARG(1);\r
246 \r
247                         /** Reads blocks of data from the attached Mass Storage device's medium.\r
248                          *\r
249                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
250                          *       call will fail.\r
251                          *\r
252                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
253                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
254                          *  \param[in]     BlockAddress     Starting block address within the device to read from.\r
255                          *  \param[in]     Blocks           Total number of blocks to read.\r
256                          *  \param[in]     BlockSize        Size in bytes of each block within the device.\r
257                          *  \param[out]    BlockBuffer      Pointer to where the read data from the device should be stored.\r
258                          *\r
259                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
260                          */\r
261                         uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
262                                                          const uint8_t LUNIndex,\r
263                                                          const uint32_t BlockAddress,\r
264                                                          const uint8_t Blocks,\r
265                                                          const uint16_t BlockSize,\r
266                                                          void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6);\r
267 \r
268                         /** Writes blocks of data to the attached Mass Storage device's medium.\r
269                          *\r
270                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the\r
271                          *       call will fail.\r
272                          *\r
273                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state.\r
274                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to.\r
275                          *  \param[in]     BlockAddress     Starting block address within the device to write to.\r
276                          *  \param[in]     Blocks           Total number of blocks to read.\r
277                          *  \param[in]     BlockSize        Size in bytes of each block within the device.\r
278                          *  \param[in]     BlockBuffer      Pointer to where the data to write should be sourced from.\r
279                          *\r
280                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready.\r
281                          */\r
282                         uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
283                                                           const uint8_t LUNIndex,\r
284                                                           const uint32_t BlockAddress,\r
285                                                           const uint8_t Blocks,\r
286                                                           const uint16_t BlockSize,\r
287                                                           const void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6);\r
288 \r
289                 /* Inline Functions: */\r
290                         /** General management task for a given Mass Storage host class interface, required for the correct operation of\r
291                          *  the interface. This should be called frequently in the main program loop, before the master USB management task\r
292                          *  \ref USB_USBTask().\r
293                          *\r
294                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing an Mass Storage Class host configuration and state.\r
295                          */\r
296                         static inline void MS_Host_USBTask(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;\r
297                         static inline void MS_Host_USBTask(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)\r
298                         {\r
299                                 (void)MSInterfaceInfo;\r
300                         }\r
301 \r
302         /* Private Interface - For use in library only: */\r
303         #if !defined(__DOXYGEN__)\r
304                 /* Macros: */\r
305                         #define MS_COMMAND_DATA_TIMEOUT_MS        10000\r
306 \r
307                 /* Function Prototypes: */\r
308                         #if defined(__INCLUDE_FROM_MASSSTORAGE_HOST_C)\r
309                                 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
310                                                                    MS_CommandBlockWrapper_t* const SCSICommandBlock,\r
311                                                                    const void* const BufferPtr) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);\r
312                                 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);\r
313                                 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
314                                                                        MS_CommandBlockWrapper_t* const SCSICommandBlock,\r
315                                                                        void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);\r
316                                 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,\r
317                                                                          MS_CommandStatusWrapper_t* const SCSICommandStatus)\r
318                                                                          ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);\r
319 \r
320                                 static uint8_t DCOMP_MS_Host_NextMSInterface(void* const CurrentDescriptor)\r
321                                                                              ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
322                                 static uint8_t DCOMP_MS_Host_NextMSInterfaceEndpoint(void* const CurrentDescriptor)\r
323                                                                                      ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
324                         #endif\r
325         #endif\r
326 \r
327         /* Disable C linkage for C++ Compilers: */\r
328                 #if defined(__cplusplus)\r
329                         }\r
330                 #endif\r
331 \r
332 #endif\r
333 \r
334 /** @} */\r
335 \r