Move the length decrements in the pipe and endpoint stream functions to the point...
[pub/lufa.git] / Demos / Host / MassStorageHost / MassStoreCommands.c
index 700d969..4ebc67a 100644 (file)
@@ -75,7 +75,7 @@ static uint8_t MassStore_SendCommand(void)
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError;\r
 \r
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError;\r
 \r
-       /* Each transmission should have a unique tag value, excluding valued 0 and 0xFFFFFFFF */\r
+       /* Each transmission should have a unique tag value, excluding values 0 and 0xFFFFFFFF */\r
        if (++MassStore_Tag == 0xFFFFFFFF)\r
          MassStore_Tag = 1;\r
 \r
        if (++MassStore_Tag == 0xFFFFFFFF)\r
          MassStore_Tag = 1;\r
 \r
@@ -88,7 +88,7 @@ static uint8_t MassStore_SendCommand(void)
          return ErrorCode;\r
 \r
        /* Send the data in the OUT pipe to the attached device */\r
          return ErrorCode;\r
 \r
        /* Send the data in the OUT pipe to the attached device */\r
-       Pipe_ClearCurrentBank();\r
+       Pipe_ClearOUT();\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
@@ -117,7 +117,7 @@ static uint8_t MassStore_WaitForDataReceived(void)
        Pipe_Unfreeze();\r
 \r
        /* Wait until data received in the IN pipe */\r
        Pipe_Unfreeze();\r
 \r
        /* Wait until data received in the IN pipe */\r
-       while (!(Pipe_ReadWriteAllowed()))\r
+       while (!(Pipe_IsINReceived()))\r
        {\r
                /* Check to see if a new frame has been issued (1ms elapsed) */\r
                if (USB_INT_HasOccurred(USB_INT_HSOFI))\r
        {\r
                /* Check to see if a new frame has been issued (1ms elapsed) */\r
                if (USB_INT_HasOccurred(USB_INT_HSOFI))\r
@@ -183,6 +183,9 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr)
                /* Read in the block data from the pipe */\r
                if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError)\r
                  return ErrorCode;\r
                /* Read in the block data from the pipe */\r
                if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError)\r
                  return ErrorCode;\r
+\r
+               /* Acknowledge the packet */\r
+               Pipe_ClearIN();\r
        }\r
        else\r
        {\r
        }\r
        else\r
        {\r
@@ -193,10 +196,10 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr)
                /* Write the block data to the pipe */\r
                if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError)\r
                  return ErrorCode;\r
                /* Write the block data to the pipe */\r
                if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError)\r
                  return ErrorCode;\r
+\r
+               /* Acknowledge the packet */\r
+               Pipe_ClearOUT();\r
        }\r
        }\r
-       \r
-       /* Acknowledge the packet */\r
-       Pipe_ClearCurrentBank();\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
@@ -215,6 +218,10 @@ static uint8_t MassStore_GetReturnedStatus(void)
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError;\r
 \r
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError;\r
 \r
+       /* If an error in the command ocurred, abort */\r
+       if ((ErrorCode == MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError)\r
+         return ErrorCode;\r
+\r
        /* Select the IN data pipe for data reception */\r
        Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);\r
        Pipe_Unfreeze();\r
        /* Select the IN data pipe for data reception */\r
        Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);\r
        Pipe_Unfreeze();\r
@@ -224,7 +231,7 @@ static uint8_t MassStore_GetReturnedStatus(void)
          return ErrorCode;\r
          \r
        /* Clear the data ready for next reception */\r
          return ErrorCode;\r
          \r
        /* Clear the data ready for next reception */\r
-       Pipe_ClearCurrentBank();\r
+       Pipe_ClearIN();\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
 \r
        /* Some buggy devices require a delay here before the pipe freezing or they will lock up */\r
        USB_Host_WaitMS(1);\r
@@ -243,15 +250,18 @@ static uint8_t MassStore_GetReturnedStatus(void)
  */\r
 uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum)\r
 {\r
  */\r
 uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum)\r
 {\r
-       USB_HostRequest = (USB_Host_Request_Header_t)\r
+       USB_ControlRequest = (USB_Request_Header_t)\r
                {\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
+                       .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
                };\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
        return USB_Host_SendControlRequest(NULL);\r
 }\r
 \r
@@ -262,15 +272,18 @@ uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum)
  */\r
 uint8_t MassStore_MassStorageReset(void)\r
 {\r
  */\r
 uint8_t MassStore_MassStorageReset(void)\r
 {\r
-       USB_HostRequest = (USB_Host_Request_Header_t)\r
+       USB_ControlRequest = (USB_Request_Header_t)\r
                {\r
                {\r
-                       bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),\r
-                       bRequest:      REQ_MassStorageReset,\r
-                       wValue:        0,\r
-                       wIndex:        0,\r
-                       wLength:       0,\r
+                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),\r
+                       .bRequest      = REQ_MassStorageReset,\r
+                       .wValue        = 0,\r
+                       .wIndex        = 0,\r
+                       .wLength       = 0,\r
                };\r
        \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
        return USB_Host_SendControlRequest(NULL);\r
 }\r
 \r
@@ -285,15 +298,18 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
 {\r
        uint8_t ErrorCode;\r
 \r
 {\r
        uint8_t ErrorCode;\r
 \r
-       USB_HostRequest = (USB_Host_Request_Header_t)\r
+       USB_ControlRequest = (USB_Request_Header_t)\r
                {\r
                {\r
-                       bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),\r
-                       bRequest:      REQ_GetMaxLUN,\r
-                       wValue:        0,\r
-                       wIndex:        0,\r
-                       wLength:       1,\r
+                       .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),\r
+                       .bRequest      = REQ_GetMaxLUN,\r
+                       .wValue        = 0,\r
+                       .wIndex        = 0,\r
+                       .wLength       = 1,\r
                };\r
                \r
                };\r
                \r
+       /* Select the control pipe for the request transfer */\r
+       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
+\r
        if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)\r
        {\r
                /* Clear the pipe stall */\r
        if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)\r
        {\r
                /* Clear the pipe stall */\r
@@ -321,17 +337,17 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_
        /* Create a CBW with a SCSI command to issue REQUEST SENSE command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to issue REQUEST SENSE command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: sizeof(SCSI_Request_Sense_Response_t),\r
-                                       Flags:              COMMAND_DIRECTION_DATA_IN,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  6\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 6\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_REQUEST_SENSE,\r
                                        0x00,                   // Reserved\r
                                {\r
                                        SCSI_CMD_REQUEST_SENSE,\r
                                        0x00,                   // Reserved\r
@@ -388,17 +404,17 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd
        /* Create a CBW with a SCSI command to read in the given blocks from the device */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to read in the given blocks from the device */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: ((uint32_t)Blocks * BlockSize),\r
-                                       Flags:              COMMAND_DIRECTION_DATA_IN,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  10\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = ((uint32_t)Blocks * BlockSize),\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 10\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_READ_10,\r
                                        0x00,                   // Unused (control bits, all off)\r
                                {\r
                                        SCSI_CMD_READ_10,\r
                                        0x00,                   // Unused (control bits, all off)\r
@@ -459,17 +475,17 @@ uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockA
        /* Create a CBW with a SCSI command to write the given blocks to the device */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to write the given blocks to the device */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: ((uint32_t)Blocks * BlockSize),\r
-                                       Flags:              COMMAND_DIRECTION_DATA_OUT,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  10\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = ((uint32_t)Blocks * BlockSize),\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_OUT,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 10\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_WRITE_10,\r
                                        0x00,                   // Unused (control bits, all off)\r
                                {\r
                                        SCSI_CMD_WRITE_10,\r
                                        0x00,                   // Unused (control bits, all off)\r
@@ -518,17 +534,17 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex)
        /* Create a CBW with a SCSI command to issue TEST UNIT READY command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to issue TEST UNIT READY command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: 0,\r
-                                       Flags:              COMMAND_DIRECTION_DATA_IN,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  6\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = 0,\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 6\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_TEST_UNIT_READY,\r
                                        0x00,                   // Reserved\r
                                {\r
                                        SCSI_CMD_TEST_UNIT_READY,\r
                                        0x00,                   // Reserved\r
@@ -567,17 +583,17 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca
        /* Create a CBW with a SCSI command to issue READ CAPACITY command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to issue READ CAPACITY command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: 8,\r
-                                       Flags:              COMMAND_DIRECTION_DATA_IN,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  10\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = 8,\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 10\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_READ_CAPACITY_10,\r
                                        0x00,                   // Reserved\r
                                {\r
                                        SCSI_CMD_READ_CAPACITY_10,\r
                                        0x00,                   // Reserved\r
@@ -639,17 +655,17 @@ uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool P
        /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
        /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */\r
        SCSICommandBlock = (CommandBlockWrapper_t)\r
                {\r
-                       Header:\r
+                       .Header =\r
                                {\r
                                {\r
-                                       Signature:          CBW_SIGNATURE,\r
-                                       Tag:                MassStore_Tag,\r
-                                       DataTransferLength: 0,\r
-                                       Flags:              COMMAND_DIRECTION_DATA_OUT,\r
-                                       LUN:                LUNIndex,\r
-                                       SCSICommandLength:  6\r
+                                       .Signature          = CBW_SIGNATURE,\r
+                                       .Tag                = MassStore_Tag,\r
+                                       .DataTransferLength = 0,\r
+                                       .Flags              = COMMAND_DIRECTION_DATA_OUT,\r
+                                       .LUN                = LUNIndex,\r
+                                       .SCSICommandLength  = 6\r
                                },\r
                                        \r
                                },\r
                                        \r
-                       SCSICommandData:\r
+                       .SCSICommandData =\r
                                {\r
                                        SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,\r
                                        0x00,                   // Reserved\r
                                {\r
                                        SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,\r
                                        0x00,                   // Reserved\r