MassStoreCommands.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. *
  28. * Mass Storage Device commands, to issue MSD commands to the device for
  29. * reading device status, capacity, and other characteristics. This file
  30. * also contains block read and write functions, so that device blocks
  31. * can be read and written. In general, these functions would be chained
  32. * to a FAT library to give file-level access to an attached device's contents.
  33. *
  34. * \note Many Mass Storage devices on the market are non-compliant to the
  35. * specifications and thus can prove difficult to interface with. It
  36. * may be necessary to retry the functions in the module several times
  37. * after they have returned and error to successfully send the command
  38. * to the device. Some devices may also need to have the stream function
  39. * timeout period extended beyond 100ms (some badly designed devices exceeding
  40. * 1.5 seconds occasionally) by defining USB_STREAM_TIMEOUT_MS to a
  41. * larger value in the project makefile and passing it to the compiler
  42. * via the -D switch.
  43. */
  44. #define INCLUDE_FROM_MASSSTORE_COMMANDS_C
  45. #include "MassStoreCommands.h"
  46. /** Current Tag value used in issued CBWs to the device. This is automatically incremented
  47. * each time a command is sent, and is not externally accessible.
  48. */
  49. static uint32_t MassStore_Tag = 1;
  50. /** Routine to send the current CBW to the device, and increment the Tag value as needed.
  51. *
  52. * \param[in] SCSICommandBlock Pointer to a SCSI command block structure to send to the attached device
  53. * \param[in,out] BufferPtr Pointer to a buffer for the data to send or receive to/from the device, or NULL if no data
  54. *
  55. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  56. */
  57. static uint8_t MassStore_SendCommand(MS_CommandBlockWrapper_t* const SCSICommandBlock,
  58. void* BufferPtr)
  59. {
  60. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  61. /* Wrap Tag value when invalid - MS class defines tag values of 0 and 0xFFFFFFFF to be invalid */
  62. if (++MassStore_Tag == 0xFFFFFFFF)
  63. MassStore_Tag = 1;
  64. /* Each transmission should have a unique tag value, increment before use */
  65. SCSICommandBlock->Tag = MassStore_Tag;
  66. /* Select the OUT data pipe for CBW transmission */
  67. Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
  68. Pipe_Unfreeze();
  69. /* Write the CBW command to the OUT pipe */
  70. if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t), NULL)) !=
  71. PIPE_RWSTREAM_NoError)
  72. {
  73. Pipe_Freeze();
  74. return ErrorCode;
  75. }
  76. /* Send the data in the OUT pipe to the attached device */
  77. Pipe_ClearOUT();
  78. /* Wait until command has been sent */
  79. Pipe_WaitUntilReady();
  80. /* Freeze pipe after use */
  81. Pipe_Freeze();
  82. if (BufferPtr != NULL)
  83. {
  84. /* Transfer the requested data (if any) to or from the device */
  85. ErrorCode = MassStore_SendReceiveData(SCSICommandBlock, (void*)BufferPtr);
  86. /* Only fail completely if the transfer fails without a STALL, as a logical STALL can be recovered from */
  87. if ((ErrorCode != PIPE_RWSTREAM_NoError) && (ErrorCode != PIPE_RWSTREAM_PipeStalled))
  88. {
  89. Pipe_Freeze();
  90. return ErrorCode;
  91. }
  92. }
  93. /* Retrieve the returned SCSI status from the device */
  94. MS_CommandStatusWrapper_t SCSIStatusBlock;
  95. return MassStore_GetReturnedStatus(&SCSIStatusBlock);
  96. }
  97. /** Waits until the attached device is ready to accept data following a CBW, checking
  98. * to ensure that the device has not stalled the transaction.
  99. *
  100. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  101. */
  102. static uint8_t MassStore_WaitForDataReceived(void)
  103. {
  104. uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
  105. uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
  106. /* Select the IN data pipe for data reception */
  107. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  108. Pipe_Unfreeze();
  109. /* Wait until data received in the IN pipe */
  110. while (!(Pipe_IsINReceived()))
  111. {
  112. uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
  113. /* Check to see if a new frame has been issued (1ms elapsed) */
  114. if (CurrentFrameNumber != PreviousFrameNumber)
  115. {
  116. /* Save the new frame number and decrement the timeout period */
  117. PreviousFrameNumber = CurrentFrameNumber;
  118. TimeoutMSRem--;
  119. /* Check to see if the timeout period for the command has elapsed */
  120. if (!(TimeoutMSRem))
  121. return PIPE_RWSTREAM_Timeout;
  122. }
  123. Pipe_Freeze();
  124. Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
  125. Pipe_Unfreeze();
  126. /* Check if pipe stalled (command failed by device) */
  127. if (Pipe_IsStalled())
  128. {
  129. /* Clear the stall condition on the OUT pipe */
  130. USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
  131. return PIPE_RWSTREAM_PipeStalled;
  132. }
  133. Pipe_Freeze();
  134. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  135. Pipe_Unfreeze();
  136. /* Check if pipe stalled (command failed by device) */
  137. if (Pipe_IsStalled())
  138. {
  139. /* Clear the stall condition on the IN pipe */
  140. USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
  141. return PIPE_RWSTREAM_PipeStalled;
  142. }
  143. /* Check to see if the device was disconnected, if so exit function */
  144. if (USB_HostState == HOST_STATE_Unattached)
  145. return PIPE_RWSTREAM_DeviceDisconnected;
  146. };
  147. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  148. Pipe_Freeze();
  149. Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
  150. Pipe_Freeze();
  151. return PIPE_RWSTREAM_NoError;
  152. }
  153. /** Sends or receives the transaction's data stage to or from the attached device, reading or
  154. * writing to the nominated buffer.
  155. *
  156. * \param[in] SCSICommandBlock Pointer to a SCSI command block structure being sent to the attached device
  157. * \param[in,out] BufferPtr Pointer to the data buffer to read from or write to
  158. *
  159. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  160. */
  161. static uint8_t MassStore_SendReceiveData(MS_CommandBlockWrapper_t* const SCSICommandBlock,
  162. void* BufferPtr)
  163. {
  164. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  165. uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
  166. /* Check the direction of the SCSI command data stage */
  167. if (SCSICommandBlock->Flags & MS_COMMAND_DIR_DATA_IN)
  168. {
  169. /* Wait until the device has replied with some data */
  170. if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
  171. return ErrorCode;
  172. /* Select the IN data pipe for data reception */
  173. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  174. Pipe_Unfreeze();
  175. /* Read in the block data from the pipe */
  176. if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
  177. return ErrorCode;
  178. /* Acknowledge the packet */
  179. Pipe_ClearIN();
  180. }
  181. else
  182. {
  183. /* Select the OUT data pipe for data transmission */
  184. Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
  185. Pipe_Unfreeze();
  186. /* Write the block data to the pipe */
  187. if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
  188. return ErrorCode;
  189. /* Acknowledge the packet */
  190. Pipe_ClearOUT();
  191. while (!(Pipe_IsOUTReady()))
  192. {
  193. if (USB_HostState == HOST_STATE_Unattached)
  194. return PIPE_RWSTREAM_DeviceDisconnected;
  195. }
  196. }
  197. /* Freeze used pipe after use */
  198. Pipe_Freeze();
  199. return PIPE_RWSTREAM_NoError;
  200. }
  201. /** Routine to receive the current CSW from the device.
  202. *
  203. * \param[out] SCSICommandStatus Pointer to a destination where the returned status data should be stored
  204. *
  205. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  206. */
  207. static uint8_t MassStore_GetReturnedStatus(MS_CommandStatusWrapper_t* const SCSICommandStatus)
  208. {
  209. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  210. /* If an error in the command occurred, abort */
  211. if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
  212. return ErrorCode;
  213. /* Select the IN data pipe for data reception */
  214. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  215. Pipe_Unfreeze();
  216. /* Load in the CSW from the attached device */
  217. if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t), NULL)) !=
  218. PIPE_RWSTREAM_NoError)
  219. {
  220. return ErrorCode;
  221. }
  222. /* Clear the data ready for next reception */
  223. Pipe_ClearIN();
  224. /* Freeze the IN pipe after use */
  225. Pipe_Freeze();
  226. /* Check to see if command failed */
  227. if (SCSICommandStatus->Status != MS_SCSI_COMMAND_Pass)
  228. ErrorCode = MASS_STORE_SCSI_COMMAND_FAILED;
  229. return ErrorCode;
  230. }
  231. /** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface,
  232. * readying the device for the next CBW. The Data endpoints are cleared of any STALL condition once this
  233. * command completes successfully.
  234. *
  235. * \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  236. */
  237. uint8_t MassStore_MassStorageReset(void)
  238. {
  239. uint8_t ErrorCode;
  240. USB_ControlRequest = (USB_Request_Header_t)
  241. {
  242. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  243. .bRequest = MS_REQ_MassStorageReset,
  244. .wValue = 0,
  245. .wIndex = 0,
  246. .wLength = 0,
  247. };
  248. /* Select the control pipe for the request transfer */
  249. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  250. if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  251. return ErrorCode;
  252. /* Select first data pipe to clear STALL condition if one exists */
  253. Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
  254. if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
  255. return ErrorCode;
  256. /* Select second data pipe to clear STALL condition if one exists */
  257. Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
  258. if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
  259. return ErrorCode;
  260. return HOST_SENDCONTROL_Successful;
  261. }
  262. /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical
  263. * Unit in the attached device.
  264. *
  265. * \note Some devices do not support this request, and will STALL it when issued. To get around this,
  266. * on unsupported devices the max LUN index will be reported as zero and no error will be returned
  267. * if the device STALLs the request.
  268. *
  269. * \param[out] MaxLUNIndex Pointer to the location that the maximum LUN index value should be stored
  270. *
  271. * \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  272. */
  273. uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
  274. {
  275. uint8_t ErrorCode;
  276. USB_ControlRequest = (USB_Request_Header_t)
  277. {
  278. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  279. .bRequest = MS_REQ_GetMaxLUN,
  280. .wValue = 0,
  281. .wIndex = 0,
  282. .wLength = 1,
  283. };
  284. /* Select the control pipe for the request transfer */
  285. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  286. if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
  287. {
  288. /* Clear the pipe stall */
  289. Pipe_ClearStall();
  290. /* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */
  291. *MaxLUNIndex = 0;
  292. /* Clear the error, and pretend the request executed correctly if the device STALLed it */
  293. ErrorCode = HOST_SENDCONTROL_Successful;
  294. }
  295. return ErrorCode;
  296. }
  297. /** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This
  298. * gives information on the device's capabilities.
  299. *
  300. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  301. * \param[out] InquiryPtr Pointer to the inquiry data structure where the inquiry data from the device is to be stored
  302. *
  303. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  304. */
  305. uint8_t MassStore_Inquiry(const uint8_t LUNIndex,
  306. SCSI_Inquiry_Response_t* const InquiryPtr)
  307. {
  308. /* Create a CBW with a SCSI command to issue INQUIRY command */
  309. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  310. {
  311. .Signature = MS_CBW_SIGNATURE,
  312. .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
  313. .Flags = MS_COMMAND_DIR_DATA_IN,
  314. .LUN = LUNIndex,
  315. .SCSICommandLength = 6,
  316. .SCSICommandData =
  317. {
  318. SCSI_CMD_INQUIRY,
  319. 0x00, // Reserved
  320. 0x00, // Reserved
  321. 0x00, // Reserved
  322. sizeof(SCSI_Inquiry_Response_t), // Allocation Length
  323. 0x00 // Unused (control)
  324. }
  325. };
  326. /* Send the command and any data to the attached device */
  327. return MassStore_SendCommand(&SCSICommandBlock, InquiryPtr);
  328. }
  329. /** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This
  330. * gives error codes for the last issued SCSI command to the device.
  331. *
  332. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  333. * \param[out] SensePtr Pointer to the sense data structure where the sense data from the device is to be stored
  334. *
  335. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  336. */
  337. uint8_t MassStore_RequestSense(const uint8_t LUNIndex,
  338. SCSI_Request_Sense_Response_t* const SensePtr)
  339. {
  340. /* Create a CBW with a SCSI command to issue REQUEST SENSE command */
  341. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  342. {
  343. .Signature = MS_CBW_SIGNATURE,
  344. .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
  345. .Flags = MS_COMMAND_DIR_DATA_IN,
  346. .LUN = LUNIndex,
  347. .SCSICommandLength = 6,
  348. .SCSICommandData =
  349. {
  350. SCSI_CMD_REQUEST_SENSE,
  351. 0x00, // Reserved
  352. 0x00, // Reserved
  353. 0x00, // Reserved
  354. sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
  355. 0x00 // Unused (control)
  356. }
  357. };
  358. /* Send the command and any data to the attached device */
  359. return MassStore_SendCommand(&SCSICommandBlock, SensePtr);
  360. }
  361. /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the
  362. * storage medium into a buffer.
  363. *
  364. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  365. * \param[in] BlockAddress Start block address to read from
  366. * \param[in] Blocks Number of blocks to read from the device
  367. * \param[in] BlockSize Size in bytes of each block to read
  368. * \param[out] BufferPtr Pointer to the buffer where the read data is to be written to
  369. *
  370. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  371. */
  372. uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex,
  373. const uint32_t BlockAddress,
  374. const uint8_t Blocks,
  375. const uint16_t BlockSize,
  376. void* BufferPtr)
  377. {
  378. /* Create a CBW with a SCSI command to read in the given blocks from the device */
  379. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  380. {
  381. .Signature = MS_CBW_SIGNATURE,
  382. .DataTransferLength = ((uint32_t)Blocks * BlockSize),
  383. .Flags = MS_COMMAND_DIR_DATA_IN,
  384. .LUN = LUNIndex,
  385. .SCSICommandLength = 10,
  386. .SCSICommandData =
  387. {
  388. SCSI_CMD_READ_10,
  389. 0x00, // Unused (control bits, all off)
  390. (BlockAddress >> 24), // MSB of Block Address
  391. (BlockAddress >> 16),
  392. (BlockAddress >> 8),
  393. (BlockAddress & 0xFF), // LSB of Block Address
  394. 0x00, // Reserved
  395. 0x00, // MSB of Total Blocks to Read
  396. Blocks, // LSB of Total Blocks to Read
  397. 0x00 // Unused (control)
  398. }
  399. };
  400. /* Send the command and any data to the attached device */
  401. return MassStore_SendCommand(&SCSICommandBlock, BufferPtr);
  402. }
  403. /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the
  404. * storage medium from a buffer.
  405. *
  406. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  407. * \param[in] BlockAddress Start block address to write to
  408. * \param[in] Blocks Number of blocks to write to in the device
  409. * \param[in] BlockSize Size in bytes of each block to write
  410. * \param[in] BufferPtr Pointer to the buffer where the write data is to be sourced from
  411. *
  412. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  413. */
  414. uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex,
  415. const uint32_t BlockAddress,
  416. const uint8_t Blocks,
  417. const uint16_t BlockSize,
  418. void* BufferPtr)
  419. {
  420. /* Create a CBW with a SCSI command to write the given blocks to the device */
  421. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  422. {
  423. .Signature = MS_CBW_SIGNATURE,
  424. .DataTransferLength = ((uint32_t)Blocks * BlockSize),
  425. .Flags = MS_COMMAND_DIR_DATA_OUT,
  426. .LUN = LUNIndex,
  427. .SCSICommandLength = 10,
  428. .SCSICommandData =
  429. {
  430. SCSI_CMD_WRITE_10,
  431. 0x00, // Unused (control bits, all off)
  432. (BlockAddress >> 24), // MSB of Block Address
  433. (BlockAddress >> 16),
  434. (BlockAddress >> 8),
  435. (BlockAddress & 0xFF), // LSB of Block Address
  436. 0x00, // Unused (reserved)
  437. 0x00, // MSB of Total Blocks to Write
  438. Blocks, // LSB of Total Blocks to Write
  439. 0x00 // Unused (control)
  440. }
  441. };
  442. /* Send the command and any data to the attached device */
  443. return MassStore_SendCommand(&SCSICommandBlock, BufferPtr);
  444. }
  445. /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept
  446. * other commands.
  447. *
  448. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  449. *
  450. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  451. */
  452. uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex)
  453. {
  454. /* Create a CBW with a SCSI command to issue TEST UNIT READY command */
  455. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  456. {
  457. .Signature = MS_CBW_SIGNATURE,
  458. .DataTransferLength = 0,
  459. .Flags = MS_COMMAND_DIR_DATA_IN,
  460. .LUN = LUNIndex,
  461. .SCSICommandLength = 6,
  462. .SCSICommandData =
  463. {
  464. SCSI_CMD_TEST_UNIT_READY,
  465. 0x00, // Reserved
  466. 0x00, // Reserved
  467. 0x00, // Reserved
  468. 0x00, // Reserved
  469. 0x00 // Unused (control)
  470. }
  471. };
  472. /* Send the command and any data to the attached device */
  473. return MassStore_SendCommand(&SCSICommandBlock, NULL);
  474. }
  475. /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the
  476. * given Logical Unit within the device.
  477. *
  478. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  479. * \param[out] CapacityPtr Device capacity structure where the capacity data is to be stored
  480. *
  481. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  482. */
  483. uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex,
  484. SCSI_Capacity_t* const CapacityPtr)
  485. {
  486. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  487. /* Create a CBW with a SCSI command to issue READ CAPACITY command */
  488. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  489. {
  490. .Signature = MS_CBW_SIGNATURE,
  491. .DataTransferLength = sizeof(SCSI_Capacity_t),
  492. .Flags = MS_COMMAND_DIR_DATA_IN,
  493. .LUN = LUNIndex,
  494. .SCSICommandLength = 10,
  495. .SCSICommandData =
  496. {
  497. SCSI_CMD_READ_CAPACITY_10,
  498. 0x00, // Reserved
  499. 0x00, // MSB of Logical block address
  500. 0x00,
  501. 0x00,
  502. 0x00, // LSB of Logical block address
  503. 0x00, // Reserved
  504. 0x00, // Reserved
  505. 0x00, // Partial Medium Indicator
  506. 0x00 // Unused (control)
  507. }
  508. };
  509. /* Send the command and any data to the attached device */
  510. if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, CapacityPtr)) != PIPE_RWSTREAM_NoError)
  511. return ErrorCode;
  512. /* Endian-correct the read data */
  513. CapacityPtr->Blocks = SwapEndian_32(CapacityPtr->Blocks);
  514. CapacityPtr->BlockSize = SwapEndian_32(CapacityPtr->BlockSize);
  515. return ErrorCode;
  516. }
  517. /** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from
  518. * being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still
  519. * be issued before the first read or write command is sent.
  520. *
  521. * \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
  522. * \param[in] PreventRemoval Whether or not the LUN media should be locked to prevent removal or not
  523. *
  524. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  525. */
  526. uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex,
  527. const bool PreventRemoval)
  528. {
  529. /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */
  530. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  531. {
  532. .Signature = MS_CBW_SIGNATURE,
  533. .DataTransferLength = 0,
  534. .Flags = MS_COMMAND_DIR_DATA_OUT,
  535. .LUN = LUNIndex,
  536. .SCSICommandLength = 6,
  537. .SCSICommandData =
  538. {
  539. SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
  540. 0x00, // Reserved
  541. 0x00, // Reserved
  542. PreventRemoval, // Prevent flag
  543. 0x00, // Reserved
  544. 0x00 // Unused (control)
  545. }
  546. };
  547. /* Send the command and any data to the attached device */
  548. return MassStore_SendCommand(&SCSICommandBlock, NULL);
  549. }