Fixed swapped paremeters in the HID state memory copy call while processing a HID...
authorDean Camera <dean@fourwalledcubicle.com>
Tue, 11 Aug 2009 08:36:25 +0000 (08:36 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Tue, 11 Aug 2009 08:36:25 +0000 (08:36 +0000)
Fixed memory corruption HID report parser when too many COLLECTION or PUSH items were processed.

Make RNDIS device class driver include the CDC device class driver header, so that it can inherrit the required CDC functional descriptor macro.

Make HID host class driver include the HID report parser.

Demos/Device/ClassDriver/Mouse/Descriptors.h
Demos/Device/ClassDriver/RNDISEthernet/Descriptors.h
Demos/Host/ClassDriver/CDCHost/CDCHost.h
Demos/Host/ClassDriver/MouseHost/MouseHost.c
Demos/Host/makefile
LUFA/Drivers/USB/Class/Common/RNDIS.h
LUFA/Drivers/USB/Class/Host/HID.c
LUFA/Drivers/USB/Class/Host/HID.h
LUFA/Drivers/USB/Class/Host/HIDParser.c
LUFA/ManPages/ChangeLog.txt
LUFA/ManPages/LUFAPoweredProjects.txt

index 0c34342..8b8158b 100644 (file)
 #define _DESCRIPTORS_H_\r
 \r
        /* Includes: */\r
 #define _DESCRIPTORS_H_\r
 \r
        /* Includes: */\r
-               #include <avr/pgmspace.h>\r
-\r
                #include <LUFA/Drivers/USB/USB.h>\r
                #include <LUFA/Drivers/USB/Class/HID.h>\r
 \r
                #include <LUFA/Drivers/USB/USB.h>\r
                #include <LUFA/Drivers/USB/Class/HID.h>\r
 \r
+               #include <avr/pgmspace.h>\r
+\r
        /* Type Defines: */\r
                /** Type define for the device configuration descriptor structure. This must be defined in the\r
                 *  application code, as the configuration descriptor contains several sub-descriptors which\r
        /* Type Defines: */\r
                /** Type define for the device configuration descriptor structure. This must be defined in the\r
                 *  application code, as the configuration descriptor contains several sub-descriptors which\r
index 28b801f..961a4f4 100644 (file)
 \r
        /* Includes: */\r
                #include <LUFA/Drivers/USB/USB.h>\r
 \r
        /* Includes: */\r
                #include <LUFA/Drivers/USB/USB.h>\r
-\r
+               #include <LUFA/Drivers/USB/Class/RNDIS.h>\r
+               \r
                #include <avr/pgmspace.h>\r
 \r
        /* Macros: */\r
                #include <avr/pgmspace.h>\r
 \r
        /* Macros: */\r
-               /** Macro to define a CDC class-specific functional descriptor. CDC functional descriptors have a\r
-                *  uniform structure but variable sized data payloads, thus cannot be represented accurately by\r
-                *  a single typedef struct. A macro is used instead so that functional descriptors can be created\r
-                *  easily by specifying the size of the payload. This allows sizeof() to work correctly.\r
-                *\r
-                *  \param[in] DataSize  Size in bytes of the CDC functional descriptor's data payload\r
-                */             \r
-               #define CDC_FUNCTIONAL_DESCRIPTOR(DataSize)        \\r
-                    struct                                        \\r
-                    {                                             \\r
-                         USB_Descriptor_Header_t Header;          \\r
-                             uint8_t                 SubType;         \\r
-                         uint8_t                 Data[DataSize];  \\r
-                    }\r
-\r
                /** Endpoint number of the CDC device-to-host notification IN endpoint. */\r
                #define CDC_NOTIFICATION_EPNUM         3\r
 \r
                /** Endpoint number of the CDC device-to-host notification IN endpoint. */\r
                #define CDC_NOTIFICATION_EPNUM         3\r
 \r
index 87a0bbc..ab31a57 100644 (file)
@@ -47,8 +47,7 @@
                #include <LUFA/Drivers/Misc/TerminalCodes.h>\r
                #include <LUFA/Drivers/Peripheral/SerialStream.h>\r
                #include <LUFA/Drivers/Board/LEDs.h>\r
                #include <LUFA/Drivers/Misc/TerminalCodes.h>\r
                #include <LUFA/Drivers/Peripheral/SerialStream.h>\r
                #include <LUFA/Drivers/Board/LEDs.h>\r
-               #include <LUFA/Drivers/USB/USB.h>\r
-               #include <LUFA/Drivers/USB/Class/Host/CDC.h>\r
+               #include <LUFA/Drivers/USB/USB.h>\\r
                \r
        /* Macros: */\r
                /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */\r
                \r
        /* Macros: */\r
                /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */\r
index 65fb489..cf2997b 100644 (file)
@@ -105,7 +105,7 @@ int main(void)
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                        case HOST_STATE_Configured:\r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                        case HOST_STATE_Configured:\r
-                               if (HID_Host_ReportReceived(&Mouse_HID_Interface))\r
+                               if (HID_Host_IsReportReceived(&Mouse_HID_Interface))\r
                                {\r
                                        \r
                                }\r
                                {\r
                                        \r
                                }\r
index 2531413..937c8fa 100644 (file)
@@ -14,5 +14,5 @@
 # code.
 
 %:
 # code.
 
 %:
-#      make -C ClassDriver/ $@        <TODO: Re-enable after Host class drivers complete>
+       make -C ClassDriver/ $@
        make -C LowLevel/ $@
        make -C LowLevel/ $@
index 5c875b7..88ee04f 100644 (file)
@@ -37,6 +37,7 @@
 \r
        /* Includes: */\r
                #include "../../USB.h"\r
 \r
        /* Includes: */\r
                #include "../../USB.h"\r
+               #include "CDC.h"\r
                \r
                #include <string.h>\r
        \r
                \r
                #include <string.h>\r
        \r
index 3a4faff..a42f8af 100644 (file)
@@ -127,11 +127,21 @@ void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
 \r
 }\r
 \r
 \r
 }\r
 \r
-void HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)\r
+bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)\r
 {\r
 {\r
+       bool ReportReceived;\r
+\r
+       if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.Active))\r
+         return false;\r
+\r
        Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);\r
        Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);\r
+       Pipe_Unfreeze();\r
+       \r
+       ReportReceived = Pipe_IsReadWriteAllowed();\r
+       \r
+       Pipe_Freeze();\r
 \r
 \r
-       return Pipe_IsReadWriteAllowed();\r
+       return ReportReceived;\r
 }\r
 \r
 #endif\r
 }\r
 \r
 #endif\r
index 0386881..e67103a 100644 (file)
@@ -47,6 +47,7 @@
        /* Includes: */\r
                #include "../../USB.h"\r
                #include "../Common/HID.h"\r
        /* Includes: */\r
                #include "../../USB.h"\r
                #include "../Common/HID.h"\r
+               #include "HIDParser.h"\r
                \r
        /* Enable C linkage for C++ Compilers: */\r
                #if defined(__cplusplus)\r
                \r
        /* Enable C linkage for C++ Compilers: */\r
                #if defined(__cplusplus)\r
                                HID_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor */\r
                                HID_ENUMERROR_NoHIDInterfaceFound        = 2, /**< A compatible HID interface was not found in the device's Configuration Descriptor */\r
                                HID_ENUMERROR_EndpointsNotFound          = 3, /**< Compatible HID endpoints were not found in the device's CDC interface */\r
                                HID_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor */\r
                                HID_ENUMERROR_NoHIDInterfaceFound        = 2, /**< A compatible HID interface was not found in the device's Configuration Descriptor */\r
                                HID_ENUMERROR_EndpointsNotFound          = 3, /**< Compatible HID endpoints were not found in the device's CDC interface */\r
-                       } CDCHost_EnumerationFailure_ErrorCodes_t;\r
+                       } HIDHost_EnumerationFailure_ErrorCodes_t;\r
        \r
                /* Function Prototypes: */\r
                        void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo);\r
                        uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorLength,\r
                                                        uint8_t* DeviceConfigDescriptor);\r
 \r
        \r
                /* Function Prototypes: */\r
                        void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo);\r
                        uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorLength,\r
                                                        uint8_t* DeviceConfigDescriptor);\r
 \r
-                       void HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo);\r
+                       bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo);\r
                \r
        /* Private Interface - For use in library only: */\r
        #if !defined(__DOXYGEN__)\r
                \r
        /* Private Interface - For use in library only: */\r
        #if !defined(__DOXYGEN__)\r
index cee602a..7ec003f 100644 (file)
 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID_ReportInfo_t* const ParserData)\r
 {\r
        HID_StateTable_t  StateTable[HID_STATETABLE_STACK_DEPTH];\r
 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID_ReportInfo_t* const ParserData)\r
 {\r
        HID_StateTable_t  StateTable[HID_STATETABLE_STACK_DEPTH];\r
-       HID_StateTable_t* CurrStateTable               = &StateTable[0];\r
+       HID_StateTable_t* CurrStateTable          = &StateTable[0];\r
        uint16_t          UsageStack[HID_USAGE_STACK_DEPTH];\r
        uint16_t          UsageStack[HID_USAGE_STACK_DEPTH];\r
-       uint8_t           UsageStackSize               = 0;\r
-       uint16_t          BitOffsetIn                  = 0;\r
-       uint16_t          BitOffsetOut                 = 0;\r
+       uint8_t           UsageStackSize          = 0;\r
+       uint16_t          BitOffsetIn             = 0;\r
+       uint16_t          BitOffsetOut            = 0;\r
 #if defined(HID_ENABLE_FEATURE_PROCESSING)\r
 #if defined(HID_ENABLE_FEATURE_PROCESSING)\r
-       uint16_t          BitOffsetFeature             = 0;\r
+       uint16_t          BitOffsetFeature        = 0;\r
 #endif\r
 #endif\r
-       HID_CollectionPath_t* CurrCollectionPath       = NULL;\r
+       HID_CollectionPath_t* CurrCollectionPath  = NULL;\r
 \r
 \r
-       memset((void*)ParserData, 0x00, sizeof(HID_ReportInfo_t));\r
-       memset((void*)StateTable, 0x00, sizeof(StateTable));\r
+       memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));\r
+       memset(StateTable, 0x00, sizeof(StateTable));\r
 \r
        while (ReportSize)\r
        {\r
 \r
        while (ReportSize)\r
        {\r
+               uint8_t  HIDReportItem  = *(ReportData++);\r
                uint32_t ReportItemData = 0;\r
                \r
                uint32_t ReportItemData = 0;\r
                \r
-               switch (*ReportData & DATA_SIZE_MASK)\r
+               ReportSize--;\r
+               \r
+               switch (HIDReportItem & DATA_SIZE_MASK)\r
                {\r
                        case DATA_SIZE_4:\r
                {\r
                        case DATA_SIZE_4:\r
-                               ReportItemData = *((uint32_t*)(ReportData + 1));\r
+                               ReportItemData  = *((uint32_t*)ReportData);\r
+                               ReportSize     -= 4;\r
+                               ReportData     += 4;\r
                                break;\r
                        case DATA_SIZE_2:\r
                                break;\r
                        case DATA_SIZE_2:\r
-                               ReportItemData = *((uint16_t*)(ReportData + 1));\r
+                               ReportItemData  = *((uint16_t*)ReportData);\r
+                               ReportSize     -= 2;\r
+                               ReportData     += 2;\r
                                break;\r
                        case DATA_SIZE_1:\r
                                break;\r
                        case DATA_SIZE_1:\r
-                               ReportItemData = *((uint8_t*)(ReportData + 1));\r
+                               ReportItemData  = *((uint8_t*)ReportData);\r
+                               ReportSize     -= 1;\r
+                               ReportData     += 1;\r
                                break;\r
                }\r
 \r
                                break;\r
                }\r
 \r
-               switch (*ReportData & (TYPE_MASK | TAG_MASK))\r
+               switch (HIDReportItem & (TYPE_MASK | TAG_MASK))\r
                {\r
                        case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):\r
                {\r
                        case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):\r
-                               if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH])\r
+                               if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])\r
                                  return HID_PARSE_HIDStackOverflow;\r
        \r
                                  return HID_PARSE_HIDStackOverflow;\r
        \r
-                               memcpy((CurrStateTable - 1),\r
-                                      CurrStateTable,\r
+                               memcpy(CurrStateTable,\r
+                                      (CurrStateTable + 1),\r
                                       sizeof(HID_ReportItem_t));\r
 \r
                                CurrStateTable++;\r
                                       sizeof(HID_ReportItem_t));\r
 \r
                                CurrStateTable++;\r
@@ -113,7 +122,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                break;\r
                        case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):\r
                                CurrStateTable->ReportID                    = ReportItemData;\r
                                break;\r
                        case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):\r
                                CurrStateTable->ReportID                    = ReportItemData;\r
-                               BitOffsetIn = 0;\r
+                               BitOffsetIn  = 0;\r
                                BitOffsetOut = 0;\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGE):\r
                                BitOffsetOut = 0;\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGE):\r
@@ -141,7 +150,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
 \r
                                        while (CurrCollectionPath->Parent != NULL);\r
                                        {\r
 \r
                                        while (CurrCollectionPath->Parent != NULL);\r
                                        {\r
-                                               if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS])\r
+                                               if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])\r
                                                  return HID_PARSE_InsufficientCollectionPaths;\r
                                        \r
                                                CurrCollectionPath++;\r
                                                  return HID_PARSE_InsufficientCollectionPaths;\r
                                        \r
                                                CurrCollectionPath++;\r
@@ -209,7 +218,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                                CurrReportItem->Attributes.Usage.Usage = 0;\r
                                        }\r
                                                                                        \r
                                                CurrReportItem->Attributes.Usage.Usage = 0;\r
                                        }\r
                                                                                        \r
-                                       switch (*ReportData & TAG_MASK)\r
+                                       switch (HIDReportItem & TAG_MASK)\r
                                        {\r
                                                case TAG_MAIN_INPUT:\r
                                                        CurrReportItem->ItemType  = REPORT_ITEM_TYPE_In;\r
                                        {\r
                                                case TAG_MAIN_INPUT:\r
                                                        CurrReportItem->ItemType  = REPORT_ITEM_TYPE_In;\r
@@ -236,11 +245,11 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
 #endif\r
                                        }\r
                                        \r
 #endif\r
                                        }\r
                                        \r
-#if !defined(HID_INCLUDE_CONSTANT_DATA_ITEMS)\r
+#if defined(HID_INCLUDE_CONSTANT_DATA_ITEMS)\r
+                                       ParserData->TotalReportItems++;\r
+#else\r
                                        if (!(ReportItemData & IOF_CONSTANT))\r
                                          ParserData->TotalReportItems++;\r
                                        if (!(ReportItemData & IOF_CONSTANT))\r
                                          ParserData->TotalReportItems++;\r
-#else\r
-                                       ParserData->TotalReportItems++;\r
 #endif\r
                                }\r
                                \r
 #endif\r
                                }\r
                                \r
@@ -249,32 +258,12 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                break;\r
                }\r
          \r
                                break;\r
                }\r
          \r
-               if ((*ReportData & TYPE_MASK) == TYPE_MAIN)\r
+               if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)\r
                {\r
                        CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;\r
                        CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;\r
                        UsageStackSize = 0;\r
                }\r
                {\r
                        CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;\r
                        CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;\r
                        UsageStackSize = 0;\r
                }\r
-               \r
-               switch (*ReportData & DATA_SIZE_MASK)\r
-               {\r
-                       case DATA_SIZE_4:\r
-                               ReportSize -= 5;\r
-                               ReportData += 5;\r
-                               break;\r
-                       case DATA_SIZE_2:\r
-                               ReportSize -= 3;\r
-                               ReportData += 3;\r
-                               break;\r
-                       case DATA_SIZE_1:\r
-                               ReportSize -= 2;\r
-                               ReportData += 2;\r
-                               break;\r
-                       case DATA_SIZE_0:\r
-                               ReportSize -= 1;\r
-                               ReportData += 1;\r
-                               break;\r
-               }\r
        }\r
        \r
        return HID_PARSE_Successful;\r
        }\r
        \r
        return HID_PARSE_Successful;\r
index ed39769..a28c0d6 100644 (file)
@@ -15,6 +15,8 @@
   *  <b>Fixed:</b>\r
   *  - Fixed possible lockup in the CDC device class driver, when the host sends data that is a multiple of the\r
   *    endpoint's bank\r
   *  <b>Fixed:</b>\r
   *  - Fixed possible lockup in the CDC device class driver, when the host sends data that is a multiple of the\r
   *    endpoint's bank\r
+  *  - Fixed swapped paremeters in the HID state memory copy call while processing a HID PUSH item in the HID report parser\r
+  *  - Fixed memory corruption HID report parser when too many COLLECTION or PUSH items were processed\r
   *\r
   *\r
   *  \section Sec_ChangeLog090810 Version 090810\r
   *\r
   *\r
   *  \section Sec_ChangeLog090810 Version 090810\r
index 9717491..dad5c9b 100644 (file)
@@ -32,6 +32,7 @@
  *\r
  *  - Bicycle POV: http://www.code.google.com/p/bicycleledpov/\r
  *  - CAMTRIG, a remote Camera Trigger device: http://code.astraw.com/projects/motmot/camtrig\r
  *\r
  *  - Bicycle POV: http://www.code.google.com/p/bicycleledpov/\r
  *  - CAMTRIG, a remote Camera Trigger device: http://code.astraw.com/projects/motmot/camtrig\r
+ *  - "Fingerlicking Wingdinger" (WARNING: Bad Language if no Javascript), a MIDI controller - http://noisybox.net/electronics/wingdinger/\r
  *  - Opendous-JTAG, an open source JTAG device: http://code.google.com/p/opendous-jtag/\r
  *  - Openkubus, an open source hardware-based authentication dongle: http://code.google.com/p/openkubus/\r
  *  - Orbee, a USB connected RGB Orb for notifications: http://www.franksworkshop.com.au/Electronics/Orbee/Orbee.htm\r
  *  - Opendous-JTAG, an open source JTAG device: http://code.google.com/p/opendous-jtag/\r
  *  - Openkubus, an open source hardware-based authentication dongle: http://code.google.com/p/openkubus/\r
  *  - Orbee, a USB connected RGB Orb for notifications: http://www.franksworkshop.com.au/Electronics/Orbee/Orbee.htm\r