Change Host mode class driver Pipe configuration routines -- better to let the applic...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / CDC.c
index 32cc9e6..4402f33 100644 (file)
 #define  INCLUDE_FROM_CDC_CLASS_HOST_C\r
 #include "CDC.h"\r
 \r
-static uint8_t CDC_Host_ProcessConfigDescriptor(void)\r
+uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t ConfigDescriptorSize,\r
+                                uint8_t* ConfigDescriptorData)\r
 {\r
-       uint8_t* ConfigDescriptorData;\r
-       uint16_t ConfigDescriptorSize;\r
        uint8_t  FoundEndpoints = 0;\r
-       \r
-       if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)\r
-         return ControlError;\r
-       \r
-       if (ConfigDescriptorSize > 512)\r
-         return DescriptorTooLarge;\r
-         \r
-       ConfigDescriptorData = alloca(ConfigDescriptorSize);\r
 \r
-       USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);\r
-       \r
        if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)\r
-         return InvalidConfigDataReturned;\r
+         return CDC_ENUMERROR_InvalidConfigDescriptor;\r
        \r
        if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
                                      DComp_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)\r
        {\r
-               return NoCDCInterfaceFound;\r
+               return CDC_ENUMERROR_NoCDCInterfaceFound;\r
        }\r
 \r
-       while (FoundEndpoints != ((1 << CDC_NOTIFICATIONPIPE) | (1 << CDC_DATAPIPE_IN) | (1 << CDC_DATAPIPE_OUT)))\r
+       while (FoundEndpoints != (CDC_FOUND_DATAPIPE_IN | CDC_FOUND_DATAPIPE_OUT | CDC_FOUND_DATAPIPE_NOTIFICATION))\r
        {\r
                if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
                                              DComp_CDC_Host_NextInterfaceCDCDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)\r
                {\r
-                       /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */\r
-                       if (FoundEndpoints & (1 << CDC_NOTIFICATIONPIPE))\r
+                       if (FoundEndpoints & CDC_FOUND_DATAPIPE_NOTIFICATION)\r
                        {\r
-                               /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */\r
                                if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, \r
                                                              DComp_CDC_Host_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)\r
                                {\r
-                                       /* Descriptor not found, error out */\r
-                                       return NoCDCInterfaceFound;\r
+                                       return CDC_ENUMERROR_NoCDCInterfaceFound;\r
                                }\r
                        }\r
                        else\r
                        {\r
                                FoundEndpoints = 0;\r
 \r
-                               Pipe_SelectPipe(CDC_NOTIFICATIONPIPE);\r
+                               Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);\r
                                Pipe_DisablePipe();\r
-                               Pipe_SelectPipe(CDC_DATAPIPE_IN);\r
+                               Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);\r
                                Pipe_DisablePipe();\r
-                               Pipe_SelectPipe(CDC_DATAPIPE_OUT);\r
+                               Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipeNumber);\r
                                Pipe_DisablePipe();\r
                        \r
                                if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
                                                              DComp_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)\r
                                {\r
-                                       return NoCDCInterfaceFound;\r
+                                       return CDC_ENUMERROR_NoCDCInterfaceFound;\r
                                }\r
                        }\r
 \r
                        if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
                                                      DComp_CDC_Host_NextInterfaceCDCDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)\r
                        {\r
-                               return NoEndpointFound;\r
+                               return CDC_ENUMERROR_EndpointsNotFound;\r
                        }\r
                }\r
                \r
@@ -106,50 +92,49 @@ static uint8_t CDC_Host_ProcessConfigDescriptor(void)
                {\r
                        if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)\r
                        {                                                          \r
-                               Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,\r
+                               Pipe_ConfigurePipe(CDCInterfaceInfo->Config.NotificationPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,\r
                                                                   EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);\r
+                               CDCInterfaceInfo->State.NotificationPipeSize = EndpointData->EndpointSize;\r
 \r
-                               Pipe_SetInfiniteINRequests();\r
                                Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);\r
                                \r
-                               FoundEndpoints |= (1 << CDC_NOTIFICATIONPIPE);\r
+                               FoundEndpoints |= CDC_FOUND_DATAPIPE_NOTIFICATION;\r
                        }\r
                }\r
                else\r
                {\r
                        if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)\r
                        {\r
-                               Pipe_ConfigurePipe(CDC_DATAPIPE_IN, EP_TYPE_BULK, PIPE_TOKEN_IN,\r
+                               Pipe_ConfigurePipe(CDCInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,\r
                                                                   EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);\r
+                               CDCInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;\r
 \r
-                               Pipe_SetInfiniteINRequests();\r
-                               Pipe_Unfreeze();\r
-                               \r
-                               FoundEndpoints |= (1 << CDC_DATAPIPE_IN);\r
+                               FoundEndpoints |= CDC_FOUND_DATAPIPE_IN;\r
                        }\r
                        else\r
                        {\r
-                               Pipe_ConfigurePipe(CDC_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,\r
+                               Pipe_ConfigurePipe(CDCInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,\r
                                                                   EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);\r
+                               CDCInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;\r
                                \r
-                               Pipe_Unfreeze();\r
-                               \r
-                               FoundEndpoints |= (1 << CDC_DATAPIPE_OUT);\r
+                               FoundEndpoints |= CDC_FOUND_DATAPIPE_OUT;\r
                        }\r
                }\r
        }\r
 \r
-       return SuccessfulConfigRead;\r
+       return CDC_ENUMERROR_NoError;\r
 }\r
 \r
 static uint8_t DComp_CDC_Host_NextCDCControlInterface(void* CurrentDescriptor)\r
 {\r
        if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)\r
        {\r
-               /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */\r
-               if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class    == CDC_CONTROL_CLASS)    &&\r
-                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&\r
-                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))\r
+               USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,\r
+                                                                               USB_Descriptor_Interface_t);\r
+       \r
+               if ((CurrentInterface->Class    == CDC_CONTROL_CLASS)    &&\r
+                   (CurrentInterface->SubClass == CDC_CONTROL_SUBCLASS) &&\r
+                       (CurrentInterface->Protocol == CDC_CONTROL_PROTOCOL))\r
                {\r
                        return DESCRIPTOR_SEARCH_Found;\r
                }\r
@@ -162,10 +147,12 @@ static uint8_t DComp_CDC_Host_NextCDCDataInterface(void* CurrentDescriptor)
 {\r
        if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)\r
        {\r
-               /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */\r
-               if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class    == CDC_DATA_CLASS)    &&\r
-                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&\r
-                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))\r
+               USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,\r
+                                                                               USB_Descriptor_Interface_t);\r
+       \r
+               if ((CurrentInterface->Class    == CDC_DATA_CLASS)    &&\r
+                   (CurrentInterface->SubClass == CDC_DATA_SUBCLASS) &&\r
+                       (CurrentInterface->Protocol == CDC_DATA_PROTOCOL))\r
                {\r
                        return DESCRIPTOR_SEARCH_Found;\r
                }\r
@@ -178,11 +165,16 @@ static uint8_t DComp_CDC_Host_NextInterfaceCDCDataEndpoint(void* CurrentDescript
 {\r
        if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)\r
        {\r
-               uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,\r
-                                                       USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);\r
+               USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,\r
+                                                                             USB_Descriptor_Endpoint_t);\r
+       \r
+               uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);\r
        \r
-               if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))\r
-                 return DESCRIPTOR_SEARCH_Found;\r
+               if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&\r
+                   !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))\r
+               {\r
+                       return DESCRIPTOR_SEARCH_Found;\r
+               }\r
        }\r
        else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)\r
        {\r
@@ -192,40 +184,9 @@ static uint8_t DComp_CDC_Host_NextInterfaceCDCDataEndpoint(void* CurrentDescript
        return DESCRIPTOR_SEARCH_NotFound;\r
 }\r
 \r
-void CDC_Host_Task(void)\r
+void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo)\r
 {\r
-       uint8_t ErrorCode;\r
 \r
-       switch (USB_HostState)\r
-       {\r
-               case HOST_STATE_Addressed:\r
-                       USB_ControlRequest = (USB_Request_Header_t)\r
-                               {\r
-                                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
-                                       .bRequest      = REQ_SetConfiguration,\r
-                                       .wValue        = 1,\r
-                                       .wIndex        = 0,\r
-                                       .wLength       = 0,\r
-                               };\r
-\r
-                       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
-\r
-                       if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
-                       {\r
-                               USB_HostState = HOST_STATE_Unattached;\r
-                       }\r
-                               \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
-                       if ((ErrorCode = CDC_Host_ProcessConfigDescriptor()) != SuccessfulConfigRead)\r
-                       {\r
-                               USB_HostState = HOST_STATE_Unattached;\r
-                       }\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-       }\r
 }\r
 \r
 #endif\r