Cleanups to the MassStorage Device demos, and the MassStorage Device Class driver.
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 15 Nov 2009 12:50:23 +0000 (12:50 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 15 Nov 2009 12:50:23 +0000 (12:50 +0000)
Demos/Device/ClassDriver/MassStorage/Lib/SCSI.c
Demos/Device/LowLevel/MassStorage/MassStorage.c
Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c
Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.h
LUFA/Drivers/USB/Class/Device/MassStorage.c
LUFA/Drivers/USB/Class/Device/MassStorage.h

index 468a04a..2e5adba 100644 (file)
@@ -239,8 +239,6 @@ static void SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_Device_t* MSInterfaceI
        \r
        /* Succeed the command and update the bytes transferred counter */\r
        MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;\r
-       \r
-       return;\r
 }\r
 \r
 /** Command processing for an issued SCSI READ (10) or WRITE (10) command. This command reads in the block start address\r
@@ -255,15 +253,11 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo
        uint32_t BlockAddress;\r
        uint16_t TotalBlocks;\r
        \r
-       /* Load in the 32-bit block address (SCSI uses big-endian, so have to do it byte-by-byte) */\r
-       ((uint8_t*)&BlockAddress)[3] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[2];\r
-       ((uint8_t*)&BlockAddress)[2] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[3];\r
-       ((uint8_t*)&BlockAddress)[1] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];\r
-       ((uint8_t*)&BlockAddress)[0] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[5];\r
+       /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */\r
+       BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);\r
 \r
-       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to do it byte-by-byte) */\r
-       ((uint8_t*)&TotalBlocks)[1]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[7];\r
-       ((uint8_t*)&TotalBlocks)[0]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[8];\r
+       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */\r
+       TotalBlocks  = SwapEndian_16(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);\r
        \r
        /* Check if the block address is outside the maximum allowable value for the LUN */\r
        if (BlockAddress >= LUN_MEDIA_BLOCKS)\r
@@ -289,6 +283,4 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo
 \r
        /* Update the bytes transferred counter and succeed the command */\r
        MSInterfaceInfo->State.CommandBlock.DataTransferLength -= ((uint32_t)TotalBlocks * VIRTUAL_MEMORY_BLOCK_SIZE);\r
-       \r
-       return;\r
 }\r
index c41b577..f9ec4a8 100644 (file)
@@ -141,7 +141,7 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
                                Endpoint_ClearSETUP();\r
 \r
                                /* Indicate that the current transfer should be aborted */\r
-                               IsMassStoreReset = true;                        \r
+                               IsMassStoreReset = true;\r
 \r
                                Endpoint_ClearStatusStage();\r
                        }\r
@@ -204,19 +204,6 @@ void MassStorage_Task(void)
 \r
                        /* Return command status block to the host */\r
                        ReturnCommandStatus();\r
-                       \r
-                       /* Check if a Mass Storage Reset occurred */\r
-                       if (IsMassStoreReset)\r
-                       {\r
-                               /* Reset the data endpoint banks */\r
-                               Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
-                               Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
-                               \r
-                               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
-                               Endpoint_ClearStall();\r
-                               Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
-                               Endpoint_ClearStall();\r
-                       }\r
 \r
                        /* Indicate ready */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
@@ -228,8 +215,23 @@ void MassStorage_Task(void)
                }\r
        }\r
 \r
-       /* Clear the abort transfer flag */\r
-       IsMassStoreReset = false;\r
+       /* Check if a Mass Storage Reset occurred */\r
+       if (IsMassStoreReset)\r
+       {\r
+               /* Reset the data endpoint banks */\r
+               Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
+               Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
+               \r
+               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
+               Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
+               Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
+               Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
+\r
+               /* Clear the abort transfer flag */\r
+               IsMassStoreReset = false;\r
+       }\r
 }\r
 \r
 /** Function to read in a command block from the host, via the bulk data OUT endpoint. This function reads in the next command block\r
index 3c82eee..921f3a9 100644 (file)
@@ -165,12 +165,12 @@ void ReadNextReport(void)
  *\r
  *  \param[in] ReportOUTData  Buffer containing the report to send to the device\r
  *  \param[in] ReportIndex  Index of the report in the device (zero if the device does not use multiple reports)\r
- *  \param[in] ReportType  Type of report to send, either HID_REPORTTYPE_OUTPUT or HID_REPORTTYPE_FEATURE\r
+ *  \param[in] ReportType  Type of report to send, either REPORT_ITEM_TYPE_Out or REPORT_ITEM_TYPE_Feature\r
  *  \param[in] ReportLength  Length of the report to send\r
  */\r
 void WriteNextReport(uint8_t* ReportOUTData, uint8_t ReportIndex, uint8_t ReportType, uint16_t ReportLength)\r
 {\r
-       /* Select and unfreeze HID data OUT pipe */\r
+       /* Select the HID data OUT pipe */\r
        Pipe_SelectPipe(HID_DATA_OUT_PIPE);\r
        \r
        /* Not all HID devices have an OUT endpoint (some require OUT reports to be sent over the\r
index 4a64b57..3395e32 100644 (file)
 \r
                /** HID Class specific request to send a HID report to the device. */\r
                #define REQ_SetReport                    0x09\r
-               \r
-               /** HID Report type specifier, for output reports to a device */\r
-               #define HID_REPORTTYPE_OUTPUT            0x02\r
-               \r
-               /** HID Report type specifier, for feature reports to a device */\r
-               #define HID_REPORTTYPE_FEATURE           0x03\r
 \r
                /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */\r
                #define LEDMASK_USB_NOTREADY      LEDS_LED1\r
index 43dc3f8..f061ec8 100644 (file)
@@ -34,7 +34,7 @@
 #define  INCLUDE_FROM_MS_CLASS_DEVICE_C\r
 #include "MassStorage.h"\r
 \r
-static USB_ClassInfo_MS_Device_t* CallbackMSInterfaceInfo;\r
+static volatile bool* CallbackIsResetSource;\r
 \r
 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
@@ -130,8 +130,10 @@ void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
                \r
                Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
                Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
                Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);\r
                Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
 \r
                MSInterfaceInfo->State.IsMassStoreReset = false;\r
        }\r
@@ -141,11 +143,14 @@ static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInte
 {\r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
 \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,\r
-                               (sizeof(MS_CommandBlockWrapper_t) - 16),\r
-                               StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-\r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,\r
+                                   (sizeof(MS_CommandBlockWrapper_t) - 16),\r
+                                   StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return false;\r
+       }\r
+       \r
        if ((MSInterfaceInfo->State.CommandBlock.Signature         != MS_CBW_SIGNATURE)                  ||\r
            (MSInterfaceInfo->State.CommandBlock.LUN               >= MSInterfaceInfo->Config.TotalLUNs) ||\r
                (MSInterfaceInfo->State.CommandBlock.Flags              & 0x1F)                              ||\r
@@ -159,14 +164,17 @@ static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInte
                return false;\r
        }\r
 \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,\r
-                               MSInterfaceInfo->State.CommandBlock.SCSICommandLength,\r
-                               StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-                                                       \r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,\r
+                                   MSInterfaceInfo->State.CommandBlock.SCSICommandLength,\r
+                                   StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return false;\r
+       }\r
+\r
        Endpoint_ClearOUT();\r
        \r
-       return !(MSInterfaceInfo->State.IsMassStoreReset);\r
+       return true;\r
 }\r
 \r
 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
@@ -175,7 +183,9 @@ static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInt
 \r
        while (Endpoint_IsStalled())\r
        {\r
+               #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
                USB_USBTask();\r
+               #endif\r
 \r
                if (MSInterfaceInfo->State.IsMassStoreReset)\r
                  return;\r
@@ -185,27 +195,31 @@ static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInt
 \r
        while (Endpoint_IsStalled())\r
        {\r
+               #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
                USB_USBTask();\r
-\r
+               #endif\r
+               \r
                if (MSInterfaceInfo->State.IsMassStoreReset)\r
                  return;\r
        }\r
        \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),\r
-                                StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-       \r
-       Endpoint_ClearIN();\r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),\r
+                                    StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return;\r
+       }\r
 \r
-       if (MSInterfaceInfo->State.IsMassStoreReset)\r
-         return;\r
+       Endpoint_ClearIN();\r
 }\r
 \r
 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)\r
 {\r
-       MS_Device_USBTask(CallbackMSInterfaceInfo);\r
+       #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
+       USB_USBTask();\r
+       #endif\r
 \r
-       if (CallbackMSInterfaceInfo->State.IsMassStoreReset)\r
+       if (*CallbackIsResetSource)\r
          return STREAMCALLBACK_Abort;\r
        else\r
          return STREAMCALLBACK_Continue;\r
index a05003b..5c3d21b 100644 (file)
@@ -87,9 +87,9 @@
                                        MS_CommandStatusWrapper_t CommandStatus; /**< Mass Storage class command status structure, set elements to indicate\r
                                                                                                                          *   the issued command's success or failure to the host\r
                                                                                                                          */\r
-                                       bool IsMassStoreReset; /**< Flag indicating that the host has requested that the Mass Storage interface be reset\r
-                                                                                       *   and that all current Mass Storage operations should immediately abort\r
-                                                                                       */\r
+                                       volatile bool IsMassStoreReset; /**< Flag indicating that the host has requested that the Mass Storage interface be reset\r
+                                                                                                *   and that all current Mass Storage operations should immediately abort\r
+                                                                                                */\r
                                } State; /**< State data for the USB class interface within the device. All elements in this section\r
                                          *   are reset to their defaults when the interface is enumerated.\r
                                          */\r