MassStorageClassHost.h 18 KB

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