3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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(CommandBlockWrapper_t
* SCSICommandBlock
, void* BufferPtr
) 
  68         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
  70         /* Each transmission should have a unique tag value, increment before use */ 
  71         SCSICommandBlock
->Tag 
= ++MassStore_Tag
; 
  73         /* Wrap Tag value when invalid - MS class defines tag values of 0 and 0xFFFFFFFF to be invalid */ 
  74         if (MassStore_Tag 
== 0xFFFFFFFF) 
  77         /* Select the OUT data pipe for CBW transmission */ 
  78         Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
  81         /* Write the CBW command to the OUT pipe */ 
  82         if ((ErrorCode 
= Pipe_Write_Stream_LE(SCSICommandBlock
, sizeof(CommandBlockWrapper_t
))) != PIPE_RWSTREAM_NoError
) 
  85         /* Send the data in the OUT pipe to the attached device */ 
  88         /* Wait until command has been sent */ 
  89         Pipe_WaitUntilReady(); 
  91         /* Freeze pipe after use */ 
  94         /* Send data if any */ 
  95         if ((BufferPtr 
!= NULL
) && 
  96             ((ErrorCode 
= MassStore_SendReceiveData(SCSICommandBlock
, BufferPtr
)) != PIPE_READYWAIT_NoError
)) 
 105 /** Waits until the attached device is ready to accept data following a CBW, checking 
 106  *  to ensure that the device has not stalled the transaction. 
 108  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
 110 static uint8_t MassStore_WaitForDataReceived(void) 
 112         uint16_t TimeoutMSRem 
= COMMAND_DATA_TIMEOUT_MS
; 
 114         /* Select the IN data pipe for data reception */ 
 115         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 118         /* Wait until data received in the IN pipe */ 
 119         while (!(Pipe_IsINReceived())) 
 121                 /* Check to see if a new frame has been issued (1ms elapsed) */ 
 122                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 124                         /* Clear the flag and decrement the timeout period counter */ 
 125                         USB_INT_Clear(USB_INT_HSOFI
); 
 128                         /* Check to see if the timeout period for the command has elapsed */ 
 130                           return PIPE_RWSTREAM_Timeout
; 
 134                 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 137                 /* Check if pipe stalled (command failed by device) */ 
 138                 if (Pipe_IsStalled()) 
 140                         /* Clear the stall condition on the OUT pipe */ 
 141                         USB_Host_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE
); 
 143                         return PIPE_RWSTREAM_PipeStalled
; 
 147                 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 150                 /* Check if pipe stalled (command failed by device) */ 
 151                 if (Pipe_IsStalled()) 
 153                         /* Clear the stall condition on the IN pipe */ 
 154                         USB_Host_ClearPipeStall(MASS_STORE_DATA_IN_PIPE
); 
 156                         return PIPE_RWSTREAM_PipeStalled
; 
 159                 /* Check to see if the device was disconnected, if so exit function */ 
 160                 if (USB_HostState 
== HOST_STATE_Unattached
) 
 161                   return PIPE_RWSTREAM_DeviceDisconnected
; 
 164         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 167         Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 170         return PIPE_RWSTREAM_NoError
; 
 173 /** Sends or receives the transaction's data stage to or from the attached device, reading or 
 174  *  writing to the nominated buffer. 
 176  *  \param[in] SCSICommandBlock  Pointer to a SCSI command block structure being sent to the attached device 
 177  *  \param[in,out]  BufferPtr    Pointer to the data buffer to read from or write to 
 179  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum 
 181 static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t
* SCSICommandBlock
, void* BufferPtr
) 
 183         uint8_t  ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 184         uint16_t BytesRem  
= SCSICommandBlock
->DataTransferLength
; 
 186         /* Check the direction of the SCSI command data stage */ 
 187         if (SCSICommandBlock
->Flags 
& COMMAND_DIRECTION_DATA_IN
) 
 189                 /* Wait until the device has replied with some data */ 
 190                 if ((ErrorCode 
= MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError
) 
 193                 /* Select the IN data pipe for data reception */ 
 194                 Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 197                 /* Read in the block data from the pipe */ 
 198                 if ((ErrorCode 
= Pipe_Read_Stream_LE(BufferPtr
, BytesRem
)) != PIPE_RWSTREAM_NoError
) 
 201                 /* Acknowledge the packet */ 
 206                 /* Select the OUT data pipe for data transmission */ 
 207                 Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE
); 
 210                 /* Write the block data to the pipe */ 
 211                 if ((ErrorCode 
= Pipe_Write_Stream_LE(BufferPtr
, BytesRem
)) != PIPE_RWSTREAM_NoError
) 
 214                 /* Acknowledge the packet */ 
 217                 while (!(Pipe_IsOUTReady())) 
 219                         if (USB_HostState 
== HOST_STATE_Unattached
) 
 220                           return PIPE_RWSTREAM_DeviceDisconnected
; 
 224         /* Freeze used pipe after use */ 
 227         return PIPE_RWSTREAM_NoError
; 
 230 /** Routine to receive the current CSW from the device. 
 232  *  \param[out] SCSICommandStatus  Pointer to a destination where the returned status data should be stored 
 234  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 236 static uint8_t MassStore_GetReturnedStatus(CommandStatusWrapper_t
* SCSICommandStatus
) 
 238         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 240         /* If an error in the command occurred, abort */ 
 241         if ((ErrorCode 
= MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError
) 
 244         /* Select the IN data pipe for data reception */ 
 245         Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE
); 
 248         /* Load in the CSW from the attached device */ 
 249         if ((ErrorCode 
= Pipe_Read_Stream_LE(SCSICommandStatus
, sizeof(CommandStatusWrapper_t
))) != PIPE_RWSTREAM_NoError
) 
 252         /* Clear the data ready for next reception */ 
 255         /* Freeze the IN pipe after use */ 
 258         /* Check to see if command failed */ 
 259         if (SCSICommandStatus
->Status 
!= Command_Pass
) 
 260           ErrorCode 
= MASS_STORE_SCSI_COMMAND_FAILED
; 
 265 /** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface, 
 266  *  readying the device for the next CBW. 
 268  *  \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 270 uint8_t MassStore_MassStorageReset(void) 
 272         USB_ControlRequest 
= (USB_Request_Header_t
) 
 274                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 275                         .bRequest      
= REQ_MassStorageReset
, 
 281         /* Select the control pipe for the request transfer */ 
 282         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 284         return USB_Host_SendControlRequest(NULL
); 
 287 /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical 
 288  *  Unit in the attached device. 
 290  *  \param[out] MaxLUNIndex  Pointer to the location that the maximum LUN index value should be stored 
 292  *  \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 294 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex
) 
 298         USB_ControlRequest 
= (USB_Request_Header_t
) 
 300                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 301                         .bRequest      
= REQ_GetMaxLUN
, 
 307         /* Select the control pipe for the request transfer */ 
 308         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 310         if ((ErrorCode 
= USB_Host_SendControlRequest(MaxLUNIndex
)) == HOST_SENDCONTROL_SetupStalled
) 
 312                 /* Clear the pipe stall */ 
 315                 /* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */ 
 322 /** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This 
 323  *  gives information on the device's capabilities. 
 325  *  \param[in] LUNIndex    Index of the LUN inside the device the command is being addressed to 
 326  *  \param[out] InquiryPtr  Pointer to the inquiry data structure where the inquiry data from the device is to be stored 
 328  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 330 uint8_t MassStore_Inquiry(const uint8_t LUNIndex
, SCSI_Inquiry_Response_t
* const InquiryPtr
) 
 332         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 334         /* Create a CBW with a SCSI command to issue INQUIRY command */ 
 335         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 337                         .Signature          
= CBW_SIGNATURE
, 
 338                         .DataTransferLength 
= sizeof(SCSI_Inquiry_Response_t
), 
 339                         .Flags              
= COMMAND_DIRECTION_DATA_IN
, 
 341                         .SCSICommandLength  
= 6, 
 348                                         sizeof(SCSI_Inquiry_Response_t
), // Allocation Length 
 349                                         0x00                    // Unused (control) 
 353         CommandStatusWrapper_t SCSICommandStatus
; 
 355         /* Send the command and any data to the attached device */ 
 356         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, InquiryPtr
)) != PIPE_RWSTREAM_NoError
) 
 362         /* Retrieve status information from the attached device */ 
 363         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 372 /** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This 
 373  *  gives error codes for the last issued SCSI command to the device. 
 375  *  \param[in] LUNIndex   Index of the LUN inside the device the command is being addressed to 
 376  *  \param[out] SensePtr  Pointer to the sense data structure where the sense data from the device is to be stored 
 378  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 380 uint8_t MassStore_RequestSense(const uint8_t LUNIndex
, SCSI_Request_Sense_Response_t
* const SensePtr
) 
 382         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 384         /* Create a CBW with a SCSI command to issue REQUEST SENSE command */ 
 385         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 387                         .Signature          
= CBW_SIGNATURE
, 
 388                         .DataTransferLength 
= sizeof(SCSI_Request_Sense_Response_t
), 
 389                         .Flags              
= COMMAND_DIRECTION_DATA_IN
, 
 391                         .SCSICommandLength  
= 6, 
 394                                         SCSI_CMD_REQUEST_SENSE
, 
 398                                         sizeof(SCSI_Request_Sense_Response_t
), // Allocation Length 
 399                                         0x00                    // Unused (control) 
 403         CommandStatusWrapper_t SCSICommandStatus
; 
 405         /* Send the command and any data to the attached device */ 
 406         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, SensePtr
)) != PIPE_RWSTREAM_NoError
) 
 412         /* Retrieve status information from the attached device */ 
 413         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 422 /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the 
 423  *  storage medium into a buffer. 
 425  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 426  *  \param[in] BlockAddress  Start block address to read from 
 427  *  \param[in] Blocks        Number of blocks to read from the device 
 428  *  \param[in] BlockSize     Size in bytes of each block to read 
 429  *  \param[out] BufferPtr    Pointer to the buffer where the read data is to be written to 
 431  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 433 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex
, const uint32_t BlockAddress
, 
 434                                   const uint8_t Blocks
, const uint16_t BlockSize
, void* BufferPtr
) 
 436         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 438         /* Create a CBW with a SCSI command to read in the given blocks from the device */ 
 439         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 441                         .Signature          
= CBW_SIGNATURE
, 
 442                         .DataTransferLength 
= ((uint32_t)Blocks 
* BlockSize
), 
 443                         .Flags              
= COMMAND_DIRECTION_DATA_IN
, 
 445                         .SCSICommandLength  
= 10, 
 449                                         0x00,                   // Unused (control bits, all off) 
 450                                         (BlockAddress 
>> 24),   // MSB of Block Address 
 451                                         (BlockAddress 
>> 16), 
 453                                         (BlockAddress 
& 0xFF),  // LSB of Block Address 
 454                                         0x00,                   // Unused (reserved) 
 455                                         0x00,                   // MSB of Total Blocks to Read 
 456                                         Blocks
,                 // LSB of Total Blocks to Read 
 457                                         0x00                    // Unused (control) 
 461         CommandStatusWrapper_t SCSICommandStatus
; 
 463         /* Send the command and any data to the attached device */ 
 464         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, BufferPtr
)) != PIPE_RWSTREAM_NoError
) 
 470         /* Retrieve status information from the attached device */ 
 471         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 480 /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the 
 481  *  storage medium from a buffer. 
 483  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 484  *  \param[in] BlockAddress  Start block address to write to 
 485  *  \param[in] Blocks        Number of blocks to write to in the device 
 486  *  \param[in] BlockSize     Size in bytes of each block to write 
 487  *  \param[in] BufferPtr     Pointer to the buffer where the write data is to be sourced from 
 489  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 491 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex
, const uint32_t BlockAddress
, 
 492                                    const uint8_t Blocks
, const uint16_t BlockSize
, void* BufferPtr
) 
 494         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 496         /* Create a CBW with a SCSI command to write the given blocks to the device */ 
 497         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 499                         .Signature          
= CBW_SIGNATURE
, 
 500                         .DataTransferLength 
= ((uint32_t)Blocks 
* BlockSize
), 
 501                         .Flags              
= COMMAND_DIRECTION_DATA_OUT
, 
 503                         .SCSICommandLength  
= 10, 
 507                                         0x00,                   // Unused (control bits, all off) 
 508                                         (BlockAddress 
>> 24),   // MSB of Block Address 
 509                                         (BlockAddress 
>> 16), 
 511                                         (BlockAddress 
& 0xFF),  // LSB of Block Address 
 512                                         0x00,                   // Unused (reserved) 
 513                                         0x00,                   // MSB of Total Blocks to Write 
 514                                         Blocks
,                 // LSB of Total Blocks to Write 
 515                                         0x00                    // Unused (control) 
 519         CommandStatusWrapper_t SCSICommandStatus
; 
 521         /* Send the command and any data to the attached device */ 
 522         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, BufferPtr
)) != PIPE_RWSTREAM_NoError
) 
 528         /* Retrieve status information from the attached device */ 
 529         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 538 /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept 
 541  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 543  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 545 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex
) 
 547         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
;       
 549         /* Create a CBW with a SCSI command to issue TEST UNIT READY command */ 
 550         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 552                         .Signature          
= CBW_SIGNATURE
, 
 553                         .DataTransferLength 
= 0, 
 554                         .Flags              
= COMMAND_DIRECTION_DATA_IN
, 
 556                         .SCSICommandLength  
= 6, 
 559                                         SCSI_CMD_TEST_UNIT_READY
, 
 564                                         0x00                    // Unused (control) 
 568         CommandStatusWrapper_t SCSICommandStatus
; 
 570         /* Send the command and any data to the attached device */ 
 571         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 577         /* Retrieve status information from the attached device */ 
 578         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 587 /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the 
 588  *  given Logical Unit within the device. 
 590  *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to 
 591  *  \param[out] CapacityPtr  Device capacity structure where the capacity data is to be stored 
 593  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 595 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex
, SCSI_Capacity_t
* const CapacityPtr
) 
 597         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 599         /* Create a CBW with a SCSI command to issue READ CAPACITY command */ 
 600         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 602                         .Signature          
= CBW_SIGNATURE
, 
 603                         .DataTransferLength 
= sizeof(SCSI_Capacity_t
), 
 604                         .Flags              
= COMMAND_DIRECTION_DATA_IN
, 
 606                         .SCSICommandLength  
= 10, 
 609                                         SCSI_CMD_READ_CAPACITY_10
, 
 611                                         0x00,                   // MSB of Logical block address 
 614                                         0x00,                   // LSB of Logical block address 
 617                                         0x00,                   // Partial Medium Indicator 
 618                                         0x00                    // Unused (control) 
 622         CommandStatusWrapper_t SCSICommandStatus
; 
 624         /* Send the command and any data to the attached device */ 
 625         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, CapacityPtr
)) != PIPE_RWSTREAM_NoError
) 
 631         /* Endian-correct the read data */ 
 632         CapacityPtr
->Blocks    
= SwapEndian_32(CapacityPtr
->Blocks
); 
 633         CapacityPtr
->BlockSize 
= SwapEndian_32(CapacityPtr
->BlockSize
); 
 635         /* Retrieve status information from the attached device */ 
 636         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
) 
 645 /** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from 
 646  *  being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still 
 647  *  be issued before the first read or write command is sent. 
 649  *  \param[in] LUNIndex        Index of the LUN inside the device the command is being addressed to 
 650  *  \param[in] PreventRemoval  Whether or not the LUN media should be locked to prevent removal or not 
 652  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails 
 654 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex
, const bool PreventRemoval
) 
 656         uint8_t ErrorCode 
= PIPE_RWSTREAM_NoError
; 
 658         /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */ 
 659         CommandBlockWrapper_t SCSICommandBlock 
= (CommandBlockWrapper_t
) 
 661                         .Signature          
= CBW_SIGNATURE
, 
 662                         .DataTransferLength 
= 0, 
 663                         .Flags              
= COMMAND_DIRECTION_DATA_OUT
, 
 665                         .SCSICommandLength  
= 6, 
 668                                         SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
, 
 671                                         PreventRemoval
,         // Prevent flag 
 673                                         0x00                    // Unused (control) 
 677         CommandStatusWrapper_t SCSICommandStatus
; 
 679         /* Send the command and any data to the attached device */ 
 680         if ((ErrorCode 
= MassStore_SendCommand(&SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
) 
 686         /* Retrieve status information from the attached device */ 
 687         if ((ErrorCode 
= MassStore_GetReturnedStatus(&SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)