3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  32  *  \brief Host mode driver for the library USB Mass Storage Class driver. 
  34  *  Host mode driver for the library USB Mass Storage Class driver. 
  36  *  \note This file should not be included directly. It is automatically included as needed by the USB module driver 
  37  *        dispatch header located in LUFA/Drivers/USB.h. 
  40 /** \ingroup Group_USBClassMS 
  41  *  @defgroup Group_USBClassMassStorageHost Mass Storage Class Host Mode Driver 
  43  *  \section Sec_Dependencies Module Source Dependencies 
  44  *  The following files must be built with any user project that uses this module: 
  45  *    - LUFA/Drivers/USB/Class/Host/MassStorage.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i> 
  47  *  \section Sec_ModDescription Module Description 
  48  *  Host Mode USB Class driver framework interface, for the Mass Storage USB Class driver. 
  53 #ifndef __MS_CLASS_HOST_H__ 
  54 #define __MS_CLASS_HOST_H__ 
  57                 #include "../../USB.h" 
  58                 #include "../Common/MassStorage.h" 
  60         /* Enable C linkage for C++ Compilers: */ 
  61                 #if defined(__cplusplus) 
  65         /* Preprocessor Checks: */ 
  66                 #if !defined(__INCLUDE_FROM_MS_DRIVER) 
  67                         #error Do not include this file directly. Include LUFA/Drivers/USB.h instead. 
  70                 #if defined(__INCLUDE_FROM_MASSSTORAGE_HOST_C) && defined(NO_STREAM_CALLBACKS) 
  71                         #error The NO_STREAM_CALLBACKS compile time option cannot be used in projects using the library Class drivers. 
  74         /* Public Interface - May be used in end-application: */ 
  76                         /** Error code for some Mass Storage Host functions, indicating a logical (and not hardware) error. */ 
  77                         #define MS_ERROR_LOGICAL_CMD_FAILED              0x80 
  80                         /** \brief Mass Storage Class Host Mode Configuration and State Structure. 
  82                          *  Class state structure. An instance of this structure should be made within the user application, 
  83                          *  and passed to each of the Mass Storage class driver functions as the MSInterfaceInfo parameter. This 
  84                          *  stores each Mass Storage interface's configuration and state information. 
  90                                         uint8_t  DataINPipeNumber
; /**< Pipe number of the Mass Storage interface's IN data pipe. */ 
  91                                         bool     DataINPipeDoubleBank
; /**< Indicates if the Mass Storage interface's IN data pipe should use double banking. */ 
  93                                         uint8_t  DataOUTPipeNumber
; /**< Pipe number of the Mass Storage interface's OUT data pipe. */ 
  94                                         bool     DataOUTPipeDoubleBank
; /**< Indicates if the Mass Storage interface's OUT data pipe should use double banking. */ 
  95                                 } Config
; /**< Config data for the USB class interface within the device. All elements in this section 
  96                                            *   <b>must</b> be set or the interface will fail to enumerate and operate correctly. 
 100                                         bool     IsActive
; /**< Indicates if the current interface instance is connected to an attached device, valid 
 101                                                             *   after \ref MS_Host_ConfigurePipes() is called and the Host state machine is in the 
 104                                         uint8_t  InterfaceNumber
; /**< Interface index of the Mass Storage interface within the attached device. */ 
 106                                         uint16_t DataINPipeSize
; /**< Size in bytes of the Mass Storage interface's IN data pipe. */ 
 107                                         uint16_t DataOUTPipeSize
;  /**< Size in bytes of the Mass Storage interface's OUT data pipe. */ 
 109                                         uint32_t TransactionTag
; /**< Current transaction tag for data synchronizing of packets. */ 
 110                                 } State
; /**< State data for the USB class interface within the device. All elements in this section 
 111                                                   *   <b>may</b> be set to initial values, but may also be ignored to default to sane values when 
 112                                                   *   the interface is enumerated. 
 114                         } USB_ClassInfo_MS_Host_t
; 
 116                         /** \brief SCSI Device LUN Capacity Structure. 
 118                          *  SCSI capacity structure, to hold the total capacity of the device in both the number 
 119                          *  of blocks in the current LUN, and the size of each block. This structure is filled by 
 120                          *  the device when the \ref MS_Host_ReadDeviceCapacity() function is called. 
 124                                 uint32_t Blocks
; /**< Number of blocks in the addressed LUN of the device. */ 
 125                                 uint32_t BlockSize
; /**< Number of bytes in each block in the addressed LUN. */ 
 129                         enum MS_Host_EnumerationFailure_ErrorCodes_t
 
 131                                 MS_ENUMERROR_NoError                    
= 0, /**< Configuration Descriptor was processed successfully. */ 
 132                                 MS_ENUMERROR_InvalidConfigDescriptor    
= 1, /**< The device returned an invalid Configuration Descriptor. */ 
 133                                 MS_ENUMERROR_NoCompatibleInterfaceFound 
= 2, /**< A compatible Mass Storage interface was not found in the device's Configuration Descriptor. */ 
 134                                 MS_ENUMERROR_PipeConfigurationFailed    
= 3, /**< One or more pipes for the specified interface could not be configured correctly. */ 
 137                 /* Function Prototypes: */ 
 138                         /** Host interface configuration routine, to configure a given Mass Storage host interface instance using the 
 139                          *  Configuration Descriptor read from an attached USB device. This function automatically updates the given Mass 
 140                          *  Storage Host instance's state values and configures the pipes required to communicate with the interface if it 
 141                          *  is found within the device. This should be called once after the stack has enumerated the attached device, while 
 142                          *  the host state machine is in the Addressed state. 
 144                          *  \note The pipe index numbers as given in the interface's configuration structure must not overlap with any other 
 145                          *        interface, or pipe bank corruption will occur. Gaps in the allocated pipe numbers or non-sequential indexes 
 146                          *        within a single interface is allowed, but no two interfaces of any type have have interleaved pipe indexes. 
 148                          *  \param[in,out] MSInterfaceInfo       Pointer to a structure containing an MS Class host configuration and state. 
 149                          *  \param[in]     ConfigDescriptorSize  Length of the attached device's Configuration Descriptor. 
 150                          *  \param[in]     ConfigDescriptorData  Pointer to a buffer containing the attached device's Configuration Descriptor. 
 152                          *  \return A value from the \ref MS_Host_EnumerationFailure_ErrorCodes_t enum. 
 154                         uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 155                                                        uint16_t ConfigDescriptorSize
, 
 156                                                        void* ConfigDescriptorData
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3); 
 158                         /** Sends a MASS STORAGE RESET control request to the attached device, resetting the Mass Storage Interface 
 159                          *  and readying it for the next Mass Storage command. 
 161                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 163                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum. 
 165                         uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1); 
 167                         /** Sends a GET MAX LUN control request to the attached device, retrieving the index of the highest LUN (Logical 
 168                          *  UNit, a logical drive) in the device. This value can then be used in the other functions of the Mass Storage 
 169                          *  Host mode Class driver to address a specific LUN within the device. 
 171                          *  \note Some devices do not support this request, and will STALL it when issued. To get around this, 
 172                          *        on unsupported devices the max LUN index will be reported as zero and no error will be returned 
 173                          *        if the device STALLs the request. 
 175                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 176                          *  \param[out]    MaxLUNIndex      Pointer to a location where the highest LUN index value should be stored. 
 178                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum. 
 180                         uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 181                                                   uint8_t* const MaxLUNIndex
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2); 
 183                         /** Retrieves the Mass Storage device's inquiry data for the specified LUN, indicating the device characteristics and 
 186                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 189                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 190                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 191                          *  \param[out]    InquiryData      Location where the read inquiry data should be stored. 
 193                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED. 
 195                         uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 196                                                        const uint8_t LUNIndex
, 
 197                                                        SCSI_Inquiry_Response_t
* const InquiryData
) ATTR_NON_NULL_PTR_ARG(1) 
 198                                                        ATTR_NON_NULL_PTR_ARG(3); 
 200                         /** Sends a TEST UNIT READY command to the device, to determine if it is ready to accept other SCSI commands. 
 202                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 203                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 205                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 207                         uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 208                                                       const uint8_t LUNIndex
) ATTR_NON_NULL_PTR_ARG(1); 
 210                         /** Retrieves the total capacity of the attached USB Mass Storage device, in blocks, and block size. 
 212                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 215                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 216                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 217                          *  \param[out]    DeviceCapacity   Pointer to the location where the capacity information should be stored. 
 219                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 221                         uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 222                                                            const uint8_t LUNIndex
, 
 223                                                            SCSI_Capacity_t
* const DeviceCapacity
) ATTR_NON_NULL_PTR_ARG(1) 
 224                                                            ATTR_NON_NULL_PTR_ARG(3); 
 226                         /** Retrieves the device sense data, indicating the current device state and error codes for the previously 
 229                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 232                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 233                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 234                          *  \param[out]    SenseData        Pointer to the location where the sense information should be stored. 
 236                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 238                         uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 239                                                      const uint8_t LUNIndex
, 
 240                                                      SCSI_Request_Sense_Response_t
* const SenseData
) ATTR_NON_NULL_PTR_ARG(1) 
 241                                                      ATTR_NON_NULL_PTR_ARG(3); 
 243                         /** Issues a PREVENT MEDIUM REMOVAL command, to logically (or, depending on the type of device, physically) lock 
 244                          *  the device from removal so that blocks of data on the medium can be read or altered. 
 246                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 249                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 250                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 251                          *  \param[in]     PreventRemoval   Boolean true if the device should be locked from removal, false otherwise. 
 253                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 255                         uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 256                                                                   const uint8_t LUNIndex
, 
 257                                                                   const bool PreventRemoval
) ATTR_NON_NULL_PTR_ARG(1); 
 259                         /** Reads blocks of data from the attached Mass Storage device's medium. 
 261                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 264                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 265                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 266                          *  \param[in]     BlockAddress     Starting block address within the device to read from. 
 267                          *  \param[in]     Blocks           Total number of blocks to read. 
 268                          *  \param[in]     BlockSize        Size in bytes of each block within the device. 
 269                          *  \param[out]    BlockBuffer      Pointer to where the read data from the device should be stored. 
 271                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 273                         uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 274                                                          const uint8_t LUNIndex
, 
 275                                                          const uint32_t BlockAddress
, 
 276                                                          const uint8_t Blocks
, 
 277                                                          const uint16_t BlockSize
, 
 278                                                          void* BlockBuffer
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6); 
 280                         /** Writes blocks of data to the attached Mass Storage device's medium. 
 282                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the 
 285                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state. 
 286                          *  \param[in]     LUNIndex         LUN index within the device the command is being issued to. 
 287                          *  \param[in]     BlockAddress     Starting block address within the device to write to. 
 288                          *  \param[in]     Blocks           Total number of blocks to read. 
 289                          *  \param[in]     BlockSize        Size in bytes of each block within the device. 
 290                          *  \param[in]     BlockBuffer      Pointer to where the data to write should be sourced from. 
 292                          *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or \ref MS_ERROR_LOGICAL_CMD_FAILED if not ready. 
 294                         uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 295                                                           const uint8_t LUNIndex
, 
 296                                                           const uint32_t BlockAddress
, 
 297                                                           const uint8_t Blocks
, 
 298                                                           const uint16_t BlockSize
, 
 299                                                           const void* BlockBuffer
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6); 
 301                 /* Inline Functions: */ 
 302                         /** General management task for a given Mass Storage host class interface, required for the correct operation of 
 303                          *  the interface. This should be called frequently in the main program loop, before the master USB management task 
 304                          *  \ref USB_USBTask(). 
 306                          *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing an Mass Storage Class host configuration and state. 
 308                         static inline void MS_Host_USBTask(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
); 
 309                         static inline void MS_Host_USBTask(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
) 
 311                                 (void)MSInterfaceInfo
; 
 314         /* Private Interface - For use in library only: */ 
 315         #if !defined(__DOXYGEN__) 
 317                         #define MS_COMMAND_DATA_TIMEOUT_MS        10000 
 319                 /* Function Prototypes: */ 
 320                         #if defined(__INCLUDE_FROM_MASSSTORAGE_HOST_C) 
 321                                 static uint8_t DCOMP_MS_Host_NextMSInterface(void* const CurrentDescriptor
) ATTR_NON_NULL_PTR_ARG(1); 
 322                                 static uint8_t DCOMP_MS_Host_NextMSInterfaceEndpoint(void* const CurrentDescriptor
) ATTR_NON_NULL_PTR_ARG(1); 
 324                                 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 325                                                                    MS_CommandBlockWrapper_t
* const SCSICommandBlock
, 
 326                                                                    const void* const BufferPtr
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2); 
 327                                 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1); 
 328                                 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 329                                                                        MS_CommandBlockWrapper_t
* const SCSICommandBlock
, 
 330                                                                        void* BufferPtr
) ATTR_NON_NULL_PTR_ARG(1)  ATTR_NON_NULL_PTR_ARG(2); 
 331                                 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
, 
 332                                                                          MS_CommandStatusWrapper_t
* const SCSICommandStatus
) 
 333                                                                          ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2); 
 337         /* Disable C linkage for C++ Compilers: */ 
 338                 #if defined(__cplusplus)