Minor updates to the Benito programmer - remove redundant PORT register manipulations.
[pub/USBasp.git] / Demos / Host / LowLevel / MassStorageHost / Lib / MassStoreCommands.c
index afeace0..d0518df 100644 (file)
@@ -90,6 +90,7 @@ static uint8_t MassStore_SendCommand(void)
        /* Send the data in the OUT pipe to the attached device */\r
        Pipe_ClearOUT();\r
        \r
+       /* Wait until command has been sent */\r
        while(!(Pipe_IsOUTReady()));\r
 \r
        /* Freeze pipe after use */\r
@@ -134,7 +135,7 @@ static uint8_t MassStore_WaitForDataReceived(void)
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the OUT pipe */\r
-                       MassStore_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE);\r
+                       USB_Host_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE);\r
 \r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
@@ -147,13 +148,13 @@ static uint8_t MassStore_WaitForDataReceived(void)
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the IN pipe */\r
-                       MassStore_ClearPipeStall(MASS_STORE_DATA_IN_PIPE);\r
+                       USB_Host_ClearPipeStall(MASS_STORE_DATA_IN_PIPE);\r
 \r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
-               if (!(USB_IsConnected))\r
+               if (USB_HostState == HOST_STATE_Unattached)\r
                  return PIPE_RWSTREAM_DeviceDisconnected;\r
        };\r
        \r
@@ -169,7 +170,7 @@ static uint8_t MassStore_WaitForDataReceived(void)
 /** Sends or receives the transaction's data stage to or from the attached device, reading or\r
  *  writing to the nominated buffer.\r
  *\r
- *  \param  BufferPtr  Pointer to the data buffer to read from or write to\r
+ *  \param[in,out]  BufferPtr  Pointer to the data buffer to read from or write to\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -205,7 +206,11 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr)
                /* Acknowledge the packet */\r
                Pipe_ClearOUT();\r
                \r
-               while (!(Pipe_IsOUTReady()));\r
+               while (!(Pipe_IsOUTReady()))\r
+               {\r
+                       if (USB_HostState == HOST_STATE_Unattached)\r
+                         return PIPE_RWSTREAM_DeviceDisconnected;\r
+               }\r
        }\r
        \r
        /* Freeze used pipe after use */\r
@@ -243,29 +248,6 @@ static uint8_t MassStore_GetReturnedStatus(void)
        return PIPE_RWSTREAM_NoError;\r
 }\r
 \r
-/** Clears the stall condition in the attached device on the nominated endpoint number.\r
- *\r
- *  \param EndpointNum  Endpoint number in the attached device whose stall condition is to be cleared\r
- *\r
- *  \return A value from the USB_Host_SendControlErrorCodes_t enum\r
- */\r
-uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum)\r
-{\r
-       USB_ControlRequest = (USB_Request_Header_t)\r
-               {\r
-                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),\r
-                       .bRequest      = REQ_ClearFeature,\r
-                       .wValue        = FEATURE_ENDPOINT_HALT,\r
-                       .wIndex        = EndpointNum,\r
-                       .wLength       = 0,\r
-               };\r
-       \r
-       /* Select the control pipe for the request transfer */\r
-       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
-\r
-       return USB_Host_SendControlRequest(NULL);\r
-}\r
-\r
 /** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface,\r
  *  readying the device for the next CBW.\r
  *\r
@@ -291,7 +273,7 @@ uint8_t MassStore_MassStorageReset(void)
 /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical\r
  *  Unit in the attached device.\r
  *\r
- *  \param MaxLUNIndex  Pointer to the location that the maximum LUN index value should be stored\r
+ *  \param[out] MaxLUNIndex  Pointer to the location that the maximum LUN index value should be stored\r
  *\r
  *  \return A value from the USB_Host_SendControlErrorCodes_t enum\r
  */\r
@@ -323,11 +305,74 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
        return ErrorCode;\r
 }\r
 \r
+/** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This\r
+ *  gives information on the device's capabilities.\r
+ *\r
+ *  \param[in] LUNIndex    Index of the LUN inside the device the command is being addressed to\r
+ *  \param[out] InquiryPtr  Pointer to the inquiry data structure where the inquiry data from the device is to be stored\r
+ *\r
+ *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
+ */\r
+uint8_t MassStore_Inquiry(const uint8_t LUNIndex, const SCSI_Inquiry_Response_t* const InquiryPtr)\r
+{\r
+       uint8_t ReturnCode = PIPE_RWSTREAM_NoError;\r
+\r
+       /* Create a CBW with a SCSI command to issue INQUIRY command */\r
+       SCSICommandBlock = (CommandBlockWrapper_t)\r
+               {\r
+                       .Header =\r
+                               {\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 6\r
+                               },\r
+                                       \r
+                       .SCSICommandData =\r
+                               {\r
+                                       SCSI_CMD_INQUIRY,\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       sizeof(SCSI_Inquiry_Response_t), // Allocation Length\r
+                                       0x00                    // Unused (control)\r
+                               }\r
+               };\r
+       \r
+       /* Send SCSI command to the attached device */\r
+       MassStore_SendCommand();\r
+\r
+       /* Wait until data received from the device */\r
+       if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ReturnCode;\r
+       }\r
+\r
+       /* Read the returned sense data into the buffer */\r
+       if ((ReturnCode = MassStore_SendReceiveData((uint8_t*)InquiryPtr)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ReturnCode;\r
+       }       \r
+       \r
+       /* Read in the returned CSW from the device */\r
+       if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ReturnCode;\r
+       }\r
+       \r
+       return PIPE_RWSTREAM_NoError;\r
+}\r
+\r
 /** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This\r
  *  gives error codes for the last issued SCSI command to the device.\r
  *\r
- *  \param LUNIndex  Index of the LUN inside the device the command is being addressed to\r
- *  \param SensePtr  Pointer to the sense data structure where the sense data from the device is to be stored\r
+ *  \param[in] LUNIndex   Index of the LUN inside the device the command is being addressed to\r
+ *  \param[out] SensePtr  Pointer to the sense data structure where the sense data from the device is to be stored\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -389,11 +434,11 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_
 /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the\r
  *  storage medium into a buffer.\r
  *\r
- *  \param LUNIndex      Index of the LUN inside the device the command is being addressed to\r
- *  \param BlockAddress  Start block address to read from\r
- *  \param Blocks        Number of blocks to read from the device\r
- *  \param BlockSize     Size in bytes of each block to read\r
- *  \param BufferPtr     Pointer to the buffer where the read data is to be written to\r
+ *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to\r
+ *  \param[in] BlockAddress  Start block address to read from\r
+ *  \param[in] Blocks        Number of blocks to read from the device\r
+ *  \param[in] BlockSize     Size in bytes of each block to read\r
+ *  \param[out] BufferPtr    Pointer to the buffer where the read data is to be written to\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -460,11 +505,11 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd
 /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the\r
  *  storage medium from a buffer.\r
  *\r
- *  \param LUNIndex      Index of the LUN inside the device the command is being addressed to\r
- *  \param BlockAddress  Start block address to write to\r
- *  \param Blocks        Number of blocks to write to in the device\r
- *  \param BlockSize     Size in bytes of each block to write\r
- *  \param BufferPtr     Pointer to the buffer where the write data is to be sourced from\r
+ *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to\r
+ *  \param[in] BlockAddress  Start block address to write to\r
+ *  \param[in] Blocks        Number of blocks to write to in the device\r
+ *  \param[in] BlockSize     Size in bytes of each block to write\r
+ *  \param[in] BufferPtr     Pointer to the buffer where the write data is to be sourced from\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -524,7 +569,7 @@ uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockA
 /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept\r
  *  other commands.\r
  *\r
- *  \param LUNIndex      Index of the LUN inside the device the command is being addressed to\r
+ *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -572,8 +617,8 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex)
 /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the\r
  *  given Logical Unit within the device.\r
  *\r
- *  \param LUNIndex     Index of the LUN inside the device the command is being addressed to\r
- *  \param CapacityPtr  Device capacity structure where the capacity data is to be stored\r
+ *  \param[in] LUNIndex      Index of the LUN inside the device the command is being addressed to\r
+ *  \param[out] CapacityPtr  Device capacity structure where the capacity data is to be stored\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -644,8 +689,8 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca
  *  being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still\r
  *  be issued before the first read or write command is sent.\r
  *\r
- *  \param LUNIndex        Index of the LUN inside the device the command is being addressed to\r
- *  \param PreventRemoval  Whether or not the LUN media should be locked to prevent removal or not\r
+ *  \param[in] LUNIndex        Index of the LUN inside the device the command is being addressed to\r
+ *  \param[in] PreventRemoval  Whether or not the LUN media should be locked to prevent removal or not\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r