Add FatFS library to the Webserver project, extend the HTTP server so that it now...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / HID.c
index c37dc5d..a5c03f7 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -33,7 +33,7 @@
 \r
 #include "HID.h"\r
 \r
 \r
 #include "HID.h"\r
 \r
-void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo)\r
+void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
        if (!(Endpoint_IsSETUPReceived()))\r
          return;\r
 {\r
        if (!(Endpoint_IsSETUPReceived()))\r
          return;\r
@@ -48,15 +48,17 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInf
                        {\r
                                Endpoint_ClearSETUP();  \r
 \r
                        {\r
                                Endpoint_ClearSETUP();  \r
 \r
-                               uint8_t  ReportINData[HIDInterfaceInfo->Config.ReportINBufferSize];\r
-                               uint16_t ReportINSize;\r
-                               uint8_t  ReportID = (USB_ControlRequest.wValue & 0xFF);\r
+                               uint16_t ReportINSize = 0;\r
+                               uint8_t  ReportID     = (USB_ControlRequest.wValue & 0xFF);\r
+                               uint8_t  ReportType   = (USB_ControlRequest.wValue >> 8) - 1;\r
 \r
 \r
-                               memset(ReportINData, 0, sizeof(ReportINData));\r
+                               memset(HIDInterfaceInfo->Config.PrevReportINBuffer, 0, HIDInterfaceInfo->Config.PrevReportINBufferSize);\r
                                \r
                                \r
-                               ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);\r
+                               CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType,\r
+                                                                   HIDInterfaceInfo->Config.PrevReportINBuffer, &ReportINSize);\r
 \r
 \r
-                               Endpoint_Write_Control_Stream_LE(ReportINData, ReportINSize);\r
+                               Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);\r
+                               Endpoint_Write_Control_Stream_LE(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize);\r
                                Endpoint_ClearOUT();\r
                        }\r
                \r
                                Endpoint_ClearOUT();\r
                        }\r
                \r
@@ -85,8 +87,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInf
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);\r
                                Endpoint_ClearIN();\r
 \r
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);\r
                                Endpoint_ClearIN();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -95,10 +96,9 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInf
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
-                               HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);\r
+                               HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);\r
                                \r
                                \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -106,11 +106,10 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInf
                        if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                                Endpoint_ClearSETUP();\r
                        if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                                Endpoint_ClearSETUP();\r
-                               \r
-                               HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue >> 8) << 2);\r
-                               \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                                       \r
+                               HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
+                                       \r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -122,20 +121,22 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* HIDInterfaceInf
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);\r
                                Endpoint_ClearIN();\r
 \r
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);\r
                                Endpoint_ClearIN();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
        }\r
 }\r
 \r
                        }\r
 \r
                        break;\r
        }\r
 }\r
 \r
-bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo)\r
+bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
 {\r
+       memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));\r
        HIDInterfaceInfo->State.UsingReportProtocol = true;\r
        HIDInterfaceInfo->State.UsingReportProtocol = true;\r
+       HIDInterfaceInfo->State.IdleCount = 500;\r
 \r
        if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT,\r
 \r
        if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT,\r
-                                                                        ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, ENDPOINT_BANK_SINGLE)))\r
+                                                                        ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize,\r
+                                                                        HIDInterfaceInfo->Config.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))\r
        {\r
                return false;\r
        }\r
        {\r
                return false;\r
        }\r
@@ -143,36 +144,45 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo)
        return true;\r
 }\r
                \r
        return true;\r
 }\r
                \r
-void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo)\r
+void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
        \r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
        \r
-       if (Endpoint_IsReadWriteAllowed() &&\r
-           !(HIDInterfaceInfo->State.IdleCount && HIDInterfaceInfo->State.IdleMSRemaining))\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
        {\r
-               if (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining))\r
-                 HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;\r
-\r
-               uint8_t  ReportINData[HIDInterfaceInfo->Config.ReportINBufferSize];\r
-               uint16_t ReportINSize;\r
-               uint8_t  ReportID = 0;\r
+               uint8_t  ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];\r
+               uint8_t  ReportID     = 0;\r
+               uint16_t ReportINSize = 0;\r
 \r
                memset(ReportINData, 0, sizeof(ReportINData));\r
 \r
 \r
                memset(ReportINData, 0, sizeof(ReportINData));\r
 \r
-               ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);\r
+               bool ForceSend         = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, REPORT_ITEM_TYPE_In,\r
+                                                                            ReportINData, &ReportINSize);\r
+               bool StatesChanged     = false;\r
+               bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));\r
+               \r
+               if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)\r
+               {\r
+                       StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);\r
+                       memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, ReportINSize);\r
+               }\r
 \r
 \r
-               if (ReportINSize)\r
+               if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))\r
                {\r
                {\r
+                       HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;\r
+\r
+                       Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
+\r
                        if (ReportID)\r
                        if (ReportID)\r
-                         Endpoint_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);\r
+                         Endpoint_Write_Byte(ReportID);\r
 \r
                        Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);\r
 \r
                        Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);\r
+                       \r
+                       Endpoint_ClearIN();\r
                }\r
                }\r
-               \r
-               Endpoint_ClearIN();\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r