Added new Benito project, a simple modified USB-to-Serial bridge for the programming...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / CDC.c
index 6eca8ae..688143c 100644 (file)
 #define  INCLUDE_FROM_CDC_CLASS_HOST_C\r
 #include "CDC.h"\r
 \r
-static uint8_t CDC_Host_ProcessConfigDescriptor(void)\r
+static uint8_t CDC_Host_ProcessConfigDescriptor(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo)\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
+       if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)\r
          return CDC_ENUMERROR_ControlError;\r
        \r
        if (ConfigDescriptorSize > 512)\r
@@ -48,7 +48,7 @@ static uint8_t CDC_Host_ProcessConfigDescriptor(void)
          \r
        ConfigDescriptorData = alloca(ConfigDescriptorSize);\r
 \r
-       USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);\r
+       USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);\r
        \r
        if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)\r
          return CDC_ENUMERROR_InvalidConfigDataReturned;\r
@@ -59,19 +59,18 @@ static uint8_t CDC_Host_ProcessConfigDescriptor(void)
                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 CDC_ENUMERROR_NoCDCInterfaceFound;\r
                                }\r
                        }\r
@@ -79,11 +78,11 @@ static uint8_t CDC_Host_ProcessConfigDescriptor(void)
                        {\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
@@ -106,35 +105,32 @@ 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
@@ -146,10 +142,12 @@ static uint8_t DComp_CDC_Host_NextCDCControlInterface(void* CurrentDescriptor)
 {\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 +160,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,8 +178,10 @@ 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
@@ -199,20 +201,19 @@ void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo)
        switch (USB_HostState)\r
        {\r
                case HOST_STATE_Addressed:\r
-                       if ((ErrorCode = CDC_Host_ProcessConfigDescriptor()) != SuccessfulConfigRead)\r
+                       if ((ErrorCode = CDC_Host_ProcessConfigDescriptor(CDCInterfaceInfo)) != CDC_ENUMERROR_NoError)\r
                        {\r
-                               USB_HostState = HOST_STATE_Unattached;\r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                        }\r
 \r
                        if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)\r
                        {\r
-                               USB_HostState = HOST_STATE_Unattached;\r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                        }\r
                                \r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                               \r
                        USB_HostState = HOST_STATE_Ready;\r
                        break;\r
        }\r