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 
  33  *  Mass Storage Device commands, to issue MSD commands to the device for 
  34  *  reading device status, capacity, and other characteristics. This file 
  35  *  also contains block read and write functions, so that device blocks 
  36  *  can be read and written. In general, these functions would be chained 
  37  *  to a FAT library to give file-level access to an attached device's contents. 
  39  *  \note Many Mass Storage devices on the market are non-compliant to the 
  40  *        specifications and thus can prove difficult to interface with. It 
  41  *        may be necessary to retry the functions in the module several times 
  42  *        after they have returned and error to successfully send the command 
  43  *        to the device. Some devices may also need to have the stream function 
  44  *        timeout period extended beyond 100ms (some badly designed devices exceeding 
  45  *        1.5 seconds occasionally) by defining USB_STREAM_TIMEOUT_MS to a 
  46  *        larger value in the project makefile and passing it to the compiler 
  50 #define  INCLUDE_FROM_MASSSTORE_COMMANDS_C 
  51 #include "MassStoreCommands.h" 
  53 /** Current Tag value used in issued CBWs to the device. This is automatically incremented 
  54  *  each time a command is sent, and is not externally accessible. 
  56 static uint32_t MassStore_Tag 
= 1; 
  59 /** Routine to send the current CBW to the device, and increment the Tag value as needed. 
  61  *  \param[in] SCSICommandBlock  Pointer to a SCSI command block structure to send to the attached device 
  62  *  \param[in,out] BufferPtr     Pointer to a buffer for the data to send or receive to/from the device, or NULL if no data 
  64  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
  66 static uint8_t MassStore_SendCommand(MS_CommandBlockWrapper_t
* const SCSICommandBlock
, 
  69         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
  71         /* Each transmission should have a unique tag value, increment before use */ 
  72         SCSICommandBlock
->Tag 
= ++MassStore_Tag
; 
  74         /* Wrap Tag value when invalid - MS class defines tag values of 0 and 0xFFFFFFFF to be invalid */ 
  75         if (MassStore_Tag 
== 0xFFFFFFFF) 
  78         /* Select the OUT data pipe for CBW transmission */ 
  79         Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
  82         /* Write the CBW command to the OUT pipe */ 
  83         if ((ErrorCode 
= Pipe_Write_Stream_LE(SCSICommandBlock
, sizeof(MS_CommandBlockWrapper_t
), NULL
)) != 
  84                                               PIPE_RWSTREAM_NoError
) 
  89         /* Send the data in the OUT pipe to the attached device */ 
  92         /* Wait until command has been sent */ 
  93         Pipe_WaitUntilReady(); 
  95         /* Freeze pipe after use */ 
  98         /* Send data if any */ 
  99         if ((BufferPtr 
!= NULL
) && 
 100             ((ErrorCode 
= MassStore_SendReceiveData(SCSICommandBlock
, BufferPtr
)) != PIPE_READYWAIT_NoError
)) 
 109 /** Waits until the attached device is ready to accept data following a CBW, checking 
 110  *  to ensure that the device has not stalled the transaction. 
 112  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
 114 static uint8_t MassStore_WaitForDataReceived(void) 
 116         uint16_t TimeoutMSRem        
= COMMAND_DATA_TIMEOUT_MS
; 
 117         uint16_t PreviousFrameNumber 
= USB_Host_GetFrameNumber(); 
 119         /* Select the IN data pipe for data reception */ 
 120         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 123         /* Wait until data received in the IN pipe */ 
 124         while (!(Pipe_IsINReceived())) 
 126                 uint16_t CurrentFrameNumber 
= USB_Host_GetFrameNumber(); 
 128                 /* Check to see if a new frame has been issued (1ms elapsed) */ 
 129                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 131                         /* Save the new frame number and decrement the timeout period */ 
 132                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 135                         /* Check to see if the timeout period for the command has elapsed */ 
 137                           return PIPE_RWSTREAM_Timeout
; 
 141                 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 144                 /* Check if pipe stalled (command failed by device) */ 
 145                 if (Pipe_IsStalled()) 
 147                         /* Clear the stall condition on the OUT pipe */ 
 148                         USB_Host_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE
); 
 150                         return PIPE_RWSTREAM_PipeStalled
; 
 154                 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 157                 /* Check if pipe stalled (command failed by device) */ 
 158                 if (Pipe_IsStalled()) 
 160                         /* Clear the stall condition on the IN pipe */ 
 161                         USB_Host_ClearPipeStall(MASS_STORE_DATA_IN_PIPE
); 
 163                         return PIPE_RWSTREAM_PipeStalled
; 
 166                 /* Check to see if the device was disconnected, if so exit function */ 
 167                 if (USB_HostState 
== HOST_STATE_Unattached
) 
 168                   return PIPE_RWSTREAM_DeviceDisconnected
; 
 171         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 174         Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 177         return PIPE_RWSTREAM_NoError
; 
 180 /** Sends or receives the transaction's data stage to or from the attached device, reading or 
 181  *  writing to the nominated buffer. 
 183  *  \param[in] SCSICommandBlock  Pointer to a SCSI command block structure being sent to the attached device 
 184  *  \param[in,out]  BufferPtr    Pointer to the data buffer to read from or write to 
 186  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
 188 static uint8_t MassStore_SendReceiveData(MS_CommandBlockWrapper_t
* const SCSICommandBlock
, 
 191         uint8_t  ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 192         uint16_t BytesRem  
= SCSICommandBlock
->DataTransferLength
; 
 194         /* Check the direction of the SCSI command data stage */ 
 195         if (SCSICommandBlock
->Flags 
& MS_COMMAND_DIR_DATA_IN
) 
 197                 /* Wait until the device has replied with some data */ 
 198                 if ((ErrorCode 
= MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError
) 
 201                 /* Select the IN data pipe for data reception */ 
 202                 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 205                 /* Read in the block data from the pipe */ 
 206                 if ((ErrorCode 
= Pipe_Read_Stream_LE(BufferPtr
, BytesRem
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 209                 /* Acknowledge the packet */ 
 214                 /* Select the OUT data pipe for data transmission */ 
 215                 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 218                 /* Write the block data to the pipe */ 
 219                 if ((ErrorCode 
= Pipe_Write_Stream_LE(BufferPtr
, BytesRem
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 222                 /* Acknowledge the packet */ 
 225                 while (!(Pipe_IsOUTReady())) 
 227                         if (USB_HostState 
== HOST_STATE_Unattached
) 
 228                           return PIPE_RWSTREAM_DeviceDisconnected
; 
 232         /* Freeze used pipe after use */ 
 235         return PIPE_RWSTREAM_NoError
; 
 238 /** Routine to receive the current CSW from the device. 
 240  *  \param[out] SCSICommandStatus  Pointer to a destination where the returned status data should be stored 
 242  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 244 static uint8_t MassStore_GetReturnedStatus(MS_CommandStatusWrapper_t
* const SCSICommandStatus
) 
 246         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 248         /* If an error in the command occurred, abort */ 
 249         if ((ErrorCode 
= MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError
) 
 252         /* Select the IN data pipe for data reception */ 
 253         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 256         /* Load in the CSW from the attached device */ 
 257         if ((ErrorCode 
= Pipe_Read_Stream_LE(SCSICommandStatus
, sizeof(MS_CommandStatusWrapper_t
), NULL
)) != 
 258                                              PIPE_RWSTREAM_NoError
) 
 263         /* Clear the data ready for next reception */ 
 266         /* Freeze the IN pipe after use */ 
 269         /* Check to see if command failed */ 
 270         if (SCSICommandStatus
->Status 
!= MS_SCSI_COMMAND_Pass
) 
 271           ErrorCode 
= MASS_STORE_SCSI_COMMAND_FAILED
; 
 276 /** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface, 
 277  *  readying the device for the next CBW. 
 279  *  \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 281 uint8_t MassStore_MassStorageReset(void) 
 283         USB_ControlRequest 
= (USB_Request_Header_t
) 
 285                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 286                         .bRequest      
= MS_REQ_MassStorageReset
, 
 292         /* Select the control pipe for the request transfer */ 
 293         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 295         return USB_Host_SendControlRequest(NULL
); 
 298 /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical 
 299  *  Unit in the attached device. 
 301  *  \note Some devices do not support this request, and will STALL it when issued. To get around this, 
 302  *        on unsupported devices the max LUN index will be reported as zero and no error will be returned 
 303  *        if the device STALLs the request. 
 305  *  \param[out] MaxLUNIndex  Pointer to the location that the maximum LUN index value should be stored 
 307  *  \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 309 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex
) 
 311         uint8_t ErrorCode 
= HOST_SENDCONTROL_Successful
; 
 313         USB_ControlRequest 
= (USB_Request_Header_t
) 
 315                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 316                         .bRequest      
= MS_REQ_GetMaxLUN
, 
 322         /* Select the control pipe for the request transfer */ 
 323         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 325         if ((ErrorCode 
= USB_Host_SendControlRequest(MaxLUNIndex
)) == HOST_SENDCONTROL_SetupStalled
) 
 327                 /* Clear the pipe stall */ 
 330                 /* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */ 
 333                 /* Clear the error, and pretend the request executed correctly if the device STALLed it */ 
 334                 ErrorCode 
= HOST_SENDCONTROL_Successful
; 
 340 /** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This 
 341  *  gives information on the device's capabilities. 
 343  *  \param[in] LUNIndex    Index of the LUN inside the device the command is being addressed to 
 344  *  \param[out] InquiryPtr  Pointer to the inquiry data structure where the inquiry data from the device is to be stored 
 346  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 348 uint8_t MassStore_Inquiry(const uint8_t LUNIndex
, 
 349                           SCSI_Inquiry_Response_t
* const InquiryPtr
) 
 351         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 353         /* Create a CBW with a SCSI command to issue INQUIRY command */ 
 354         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 356                         .Signature          
= MS_CBW_SIGNATURE
, 
 357                         .DataTransferLength 
= sizeof(SCSI_Inquiry_Response_t
), 
 358                         .Flags              
= MS_COMMAND_DIR_DATA_IN
, 
 360                         .SCSICommandLength  
= 6, 
 367                                         sizeof(SCSI_Inquiry_Response_t
), // Allocation Length 
 368                                         0x00                    // Unused (control) 
 372         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 374         /* Send the command and any data to the attached device */ 
 375         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, InquiryPtr
)) != PIPE_RWSTREAM_NoError
) 
 381         /* Retrieve status information from the attached device */ 
 382         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 391 /** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This 
 392  *  gives error codes for the last issued SCSI command to the device. 
 394  *  \param[in] LUNIndex   Index of the LUN inside the device the command is being addressed to 
 395  *  \param[out] SensePtr  Pointer to the sense data structure where the sense data from the device is to be stored 
 397  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 399 uint8_t MassStore_RequestSense(const uint8_t LUNIndex
, 
 400                                SCSI_Request_Sense_Response_t
* const SensePtr
) 
 402         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 404         /* Create a CBW with a SCSI command to issue REQUEST SENSE command */ 
 405         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 407                         .Signature          
= MS_CBW_SIGNATURE
, 
 408                         .DataTransferLength 
= sizeof(SCSI_Request_Sense_Response_t
), 
 409                         .Flags              
= MS_COMMAND_DIR_DATA_IN
, 
 411                         .SCSICommandLength  
= 6, 
 414                                         SCSI_CMD_REQUEST_SENSE
, 
 418                                         sizeof(SCSI_Request_Sense_Response_t
), // Allocation Length 
 419                                         0x00                    // Unused (control) 
 423         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 425         /* Send the command and any data to the attached device */ 
 426         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, SensePtr
)) != PIPE_RWSTREAM_NoError
) 
 432         /* Retrieve status information from the attached device */ 
 433         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 442 /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the 
 443  *  storage medium into a buffer. 
 445  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 446  *  \param[in] BlockAddress  Start block address to read from 
 447  *  \param[in] Blocks        Number of blocks to read from the device 
 448  *  \param[in] BlockSize     Size in bytes of each block to read 
 449  *  \param[out] BufferPtr    Pointer to the buffer where the read data is to be written to 
 451  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 453 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex
, 
 454                                   const uint32_t BlockAddress
, 
 455                                   const uint8_t Blocks
, 
 456                                   const uint16_t BlockSize
, 
 459         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 461         /* Create a CBW with a SCSI command to read in the given blocks from the device */ 
 462         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 464                         .Signature          
= MS_CBW_SIGNATURE
, 
 465                         .DataTransferLength 
= ((uint32_t)Blocks 
* BlockSize
), 
 466                         .Flags              
= MS_COMMAND_DIR_DATA_IN
, 
 468                         .SCSICommandLength  
= 10, 
 472                                         0x00,                   // Unused (control bits, all off) 
 473                                         (BlockAddress 
>> 24),   // MSB of Block Address 
 474                                         (BlockAddress 
>> 16), 
 476                                         (BlockAddress 
& 0xFF),  // LSB of Block Address 
 478                                         0x00,                   // MSB of Total Blocks to Read 
 479                                         Blocks
,                 // LSB of Total Blocks to Read 
 480                                         0x00                    // Unused (control) 
 484         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 486         /* Send the command and any data to the attached device */ 
 487         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, BufferPtr
)) != PIPE_RWSTREAM_NoError
) 
 493         /* Retrieve status information from the attached device */ 
 494         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 503 /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the 
 504  *  storage medium from a buffer. 
 506  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 507  *  \param[in] BlockAddress  Start block address to write to 
 508  *  \param[in] Blocks        Number of blocks to write to in the device 
 509  *  \param[in] BlockSize     Size in bytes of each block to write 
 510  *  \param[in] BufferPtr     Pointer to the buffer where the write data is to be sourced from 
 512  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 514 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex
, 
 515                                    const uint32_t BlockAddress
, 
 516                                    const uint8_t Blocks
, 
 517                                    const uint16_t BlockSize
, 
 520         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 522         /* Create a CBW with a SCSI command to write the given blocks to the device */ 
 523         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 525                         .Signature          
= MS_CBW_SIGNATURE
, 
 526                         .DataTransferLength 
= ((uint32_t)Blocks 
* BlockSize
), 
 527                         .Flags              
= MS_COMMAND_DIR_DATA_OUT
, 
 529                         .SCSICommandLength  
= 10, 
 533                                         0x00,                   // Unused (control bits, all off) 
 534                                         (BlockAddress 
>> 24),   // MSB of Block Address 
 535                                         (BlockAddress 
>> 16), 
 537                                         (BlockAddress 
& 0xFF),  // LSB of Block Address 
 538                                         0x00,                   // Unused (reserved) 
 539                                         0x00,                   // MSB of Total Blocks to Write 
 540                                         Blocks
,                 // LSB of Total Blocks to Write 
 541                                         0x00                    // Unused (control) 
 545         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 547         /* Send the command and any data to the attached device */ 
 548         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, BufferPtr
)) != PIPE_RWSTREAM_NoError
) 
 554         /* Retrieve status information from the attached device */ 
 555         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 564 /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept 
 567  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 569  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 571 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex
) 
 573         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 575         /* Create a CBW with a SCSI command to issue TEST UNIT READY command */ 
 576         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 578                         .Signature          
= MS_CBW_SIGNATURE
, 
 579                         .DataTransferLength 
= 0, 
 580                         .Flags              
= MS_COMMAND_DIR_DATA_IN
, 
 582                         .SCSICommandLength  
= 6, 
 585                                         SCSI_CMD_TEST_UNIT_READY
, 
 590                                         0x00                    // Unused (control) 
 594         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 596         /* Send the command and any data to the attached device */ 
 597         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 603         /* Retrieve status information from the attached device */ 
 604         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 613 /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the 
 614  *  given Logical Unit within the device. 
 616  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 617  *  \param[out] CapacityPtr  Device capacity structure where the capacity data is to be stored 
 619  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 621 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex
, 
 622                                SCSI_Capacity_t
* const CapacityPtr
) 
 624         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 626         /* Create a CBW with a SCSI command to issue READ CAPACITY command */ 
 627         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 629                         .Signature          
= MS_CBW_SIGNATURE
, 
 630                         .DataTransferLength 
= sizeof(SCSI_Capacity_t
), 
 631                         .Flags              
= MS_COMMAND_DIR_DATA_IN
, 
 633                         .SCSICommandLength  
= 10, 
 636                                         SCSI_CMD_READ_CAPACITY_10
, 
 638                                         0x00,                   // MSB of Logical block address 
 641                                         0x00,                   // LSB of Logical block address 
 644                                         0x00,                   // Partial Medium Indicator 
 645                                         0x00                    // Unused (control) 
 649         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 651         /* Send the command and any data to the attached device */ 
 652         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, CapacityPtr
)) != PIPE_RWSTREAM_NoError
) 
 658         /* Endian-correct the read data */ 
 659         CapacityPtr
->Blocks    
= SwapEndian_32(CapacityPtr
->Blocks
); 
 660         CapacityPtr
->BlockSize 
= SwapEndian_32(CapacityPtr
->BlockSize
); 
 662         /* Retrieve status information from the attached device */ 
 663         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 672 /** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from 
 673  *  being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still 
 674  *  be issued before the first read or write command is sent. 
 676  *  \param[in] LUNIndex        Index of the LUN inside the device the command is being addressed to 
 677  *  \param[in] PreventRemoval  Whether or not the LUN media should be locked to prevent removal or not 
 679  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 681 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex
, 
 682                                             const bool PreventRemoval
) 
 684         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 686         /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */ 
 687         MS_CommandBlockWrapper_t SCSICommandBlock 
= (MS_CommandBlockWrapper_t
) 
 689                         .Signature          
= MS_CBW_SIGNATURE
, 
 690                         .DataTransferLength 
= 0, 
 691                         .Flags              
= MS_COMMAND_DIR_DATA_OUT
, 
 693                         .SCSICommandLength  
= 6, 
 696                                         SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
, 
 699                                         PreventRemoval
,         // Prevent flag 
 701                                         0x00                    // Unused (control) 
 705         MS_CommandStatusWrapper_t SCSICommandStatus
; 
 707         /* Send the command and any data to the attached device */ 
 708         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 714         /* Retrieve status information from the attached device */ 
 715         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)