From 5251bc6e4e312ec5c4f57c43a92ba49c19684c51 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 27 Jul 2009 10:31:07 +0000 Subject: Add new MouseHost Class Driver demo, unfinished, to allow for better testing of the new prototype Host Mode Class Driver framework. Flesh out start of a HID class Host mode class driver. --- LUFA/Drivers/USB/Class/Host/CDC.c | 2 +- LUFA/Drivers/USB/Class/Host/CDC.h | 31 +++++------- LUFA/Drivers/USB/Class/Host/HID.c | 100 ++++++++++++++++++++++++++++++++++++++ LUFA/Drivers/USB/Class/Host/HID.h | 55 +++++++++++++++++++++ 4 files changed, 169 insertions(+), 19 deletions(-) (limited to 'LUFA/Drivers/USB/Class') diff --git a/LUFA/Drivers/USB/Class/Host/CDC.c b/LUFA/Drivers/USB/Class/Host/CDC.c index fda47faf5..d648ec23e 100644 --- a/LUFA/Drivers/USB/Class/Host/CDC.c +++ b/LUFA/Drivers/USB/Class/Host/CDC.c @@ -93,7 +93,7 @@ uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, DComp_CDC_Host_NextInterfaceCDCDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found) { - return CDC_ENUMERROR_NoEndpointFound; + return CDC_ENUMERROR_EndpointsNotFound; } } diff --git a/LUFA/Drivers/USB/Class/Host/CDC.h b/LUFA/Drivers/USB/Class/Host/CDC.h index 9181aca03..e9518fede 100644 --- a/LUFA/Drivers/USB/Class/Host/CDC.h +++ b/LUFA/Drivers/USB/Class/Host/CDC.h @@ -71,8 +71,6 @@ */ struct { - uint8_t ControlInterfaceNumber; /**< Interface number of the CDC control interface within the device */ - uint16_t DataINPipeSize; /**< Size in bytes of the CDC interface's IN data pipe */ uint16_t DataOUTPipeSize; /**< Size in bytes of the CDC interface's OUT data pipe */ uint16_t NotificationPipeSize; /**< Size in bytes of the CDC interface's IN notification endpoint, if used */ @@ -114,11 +112,22 @@ CDC_ENUMERROR_DescriptorTooLarge = 2, /**< The device's Configuration Descriptor is too large to process */ CDC_ENUMERROR_InvalidConfigDataReturned = 3, /**< The device returned an invalid Configuration Descriptor */ CDC_ENUMERROR_NoCDCInterfaceFound = 4, /**< A compatible CDC interface was not found in the device's Configuration Descriptor */ - CDC_ENUMERROR_NoEndpointFound = 5, /**< Compatible CDC endpoints were not found in the device's CDC interface */ + CDC_ENUMERROR_EndpointsNotFound = 5, /**< Compatible CDC endpoints were not found in the device's CDC interface */ } CDCHost_EnumerationFailure_ErrorCodes_t; /* Function Prototypes: */ void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); + uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t MaxConfigBufferSize); + + void EVENT_CDC_Host_ControLineStateChanged(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); + + uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); + uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); + + void CDC_Host_SendString(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, char* Data, uint16_t Length); + void CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint8_t Data); + uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); + uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); /* Private Interface - For use in library only: */ #if !defined(__DOXYGEN__) @@ -139,21 +148,7 @@ static uint8_t DComp_CDC_Host_NextCDCControlInterface(void* CurrentDescriptor); static uint8_t DComp_CDC_Host_NextCDCDataInterface(void* CurrentDescriptor); static uint8_t DComp_CDC_Host_NextInterfaceCDCDataEndpoint(void* CurrentDescriptor); - #endif - - uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t MaxConfigBufferSize); - void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - - void EVENT_CDC_Host_ControLineStateChanged(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - - uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - - void CDC_Host_SendString(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, char* Data, uint16_t Length); - void CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint8_t Data); - uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo); - + #endif #endif /* Disable C linkage for C++ Compilers: */ diff --git a/LUFA/Drivers/USB/Class/Host/HID.c b/LUFA/Drivers/USB/Class/Host/HID.c index 99e44a46d..4f013d56e 100644 --- a/LUFA/Drivers/USB/Class/Host/HID.c +++ b/LUFA/Drivers/USB/Class/Host/HID.c @@ -31,6 +31,106 @@ #include "../../HighLevel/USBMode.h" #if defined(USB_CAN_BE_HOST) +#define INCLUDE_FROM_HID_CLASS_HOST_C #include "HID.h" +uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t MaxConfigBufferSize) +{ + uint8_t* ConfigDescriptorData; + uint16_t ConfigDescriptorSize; + uint8_t FoundEndpoints = 0; + + if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful) + return HID_ENUMERROR_ControlError; + + if (ConfigDescriptorSize > MaxConfigBufferSize) + return HID_ENUMERROR_DescriptorTooLarge; + + ConfigDescriptorData = alloca(ConfigDescriptorSize); + + USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData); + + if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration) + return HID_ENUMERROR_InvalidConfigDataReturned; + + do + { + if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, + DComp_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found) + { + return HID_ENUMERROR_NoHIDInterfaceFound; + } + } while (HIDInterfaceInfo->Config.MatchInterfaceProtocol && + DESCRIPTOR_PCAST(ConfigDescriptorData, + USB_Descriptor_Interface_t)->HIDInterfaceProtocol != HIDInterfaceInfo->Config.Protocol); + + while (FoundEndpoints != ((1 << HID_FOUND_DATAPIPE_IN) | (1 << HID_FOUND_DATAPIPE_OUT))) + { + if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, + DComp_HID_Host_NextInterfaceHIDDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found) + { + if (FoundEndpoints == (1 << HID_FOUND_DATAPIPE_IN)) + break; + + return HID_ENUMERROR_EndpointsNotFound; + } + + USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t); + + if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN) + { + Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN, + EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE); + + FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_IN); + } + else + { + Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT, + EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE); + + FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_OUT); + } + } + + return HID_ENUMERROR_NoError; +} + +static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor) +{ + if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface) + { + USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor, + USB_Descriptor_Interface_t); + + if (CurrentInterface->Class == HID_INTERFACE_CLASS) + return DESCRIPTOR_SEARCH_Found; + } + + return DESCRIPTOR_SEARCH_NotFound; +} + +static uint8_t DComp_HID_Host_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor) +{ + if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint) + { + USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor, + USB_Descriptor_Endpoint_t); + + if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress))) + return DESCRIPTOR_SEARCH_Found; + } + else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface) + { + return DESCRIPTOR_SEARCH_Fail; + } + + return DESCRIPTOR_SEARCH_NotFound; +} + +void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) +{ + +} + #endif diff --git a/LUFA/Drivers/USB/Class/Host/HID.h b/LUFA/Drivers/USB/Class/Host/HID.h index 5e44e0ab1..9ae1f1469 100644 --- a/LUFA/Drivers/USB/Class/Host/HID.h +++ b/LUFA/Drivers/USB/Class/Host/HID.h @@ -54,8 +54,63 @@ #endif /* Public Interface - May be used in end-application: */ + /* Type Defines: */ + /** Class state structure. An instance of this structure should be made within the user application, + * and passed to each of the HID class driver functions as the HIDInterfaceInfo parameter. This + * stores each HID interface's configuration and state information. + */ + typedef struct + { + const struct + { + uint8_t DataINPipeNumber; /**< Pipe number of the HID interface's IN data pipe */ + uint8_t DataOUTPipeNumber; /**< Pipe number of the HID interface's OUT data pipe */ + + bool MatchInterfaceProtocol; + uint8_t HIDInterfaceProtocol; + } Config; /**< Config data for the USB class interface within the device. All elements in this section + * must be set or the interface will fail to enumerate and operate correctly. + */ + struct + { + uint16_t DataINPipeSize; /**< Size in bytes of the HID interface's IN data pipe */ + uint16_t DataOUTPipeSize; /**< Size in bytes of the HID interface's OUT data pipe */ + } State; /**< State data for the USB class interface within the device. All elements in this section + * may be set to initial values, but may also be ignored to default to sane values when + * the interface is enumerated. + */ + } USB_ClassInfo_HID_Host_t; + + /* Enums: */ + enum + { + HID_ENUMERROR_NoError = 0, /**< Configuration Descriptor was processed successfully */ + HID_ENUMERROR_ControlError = 1, /**< A control request to the device failed to complete successfully */ + HID_ENUMERROR_DescriptorTooLarge = 2, /**< The device's Configuration Descriptor is too large to process */ + HID_ENUMERROR_InvalidConfigDataReturned = 3, /**< The device returned an invalid Configuration Descriptor */ + HID_ENUMERROR_NoHIDInterfaceFound = 4, /**< A compatible HID interface was not found in the device's Configuration Descriptor */ + HID_ENUMERROR_EndpointsNotFound = 5, /**< Compatible HID endpoints were not found in the device's CDC interface */ + } CDCHost_EnumerationFailure_ErrorCodes_t; + /* Function Prototypes: */ + void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo); + uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t MaxConfigBufferSize); + /* Private Interface - For use in library only: */ + #if !defined(__DOXYGEN__) + /* Macros: */ + #define HID_INTERFACE_CLASS 0x03 + + #define HID_FOUND_DATAPIPE_IN (1 << 0) + #define HID_FOUND_DATAPIPE_OUT (1 << 1) + + /* Function Prototypes: */ + #if defined(INCLUDE_FROM_HID_CLASS_HOST_C) + static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor); + static uint8_t DComp_HID_Host_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor); + #endif + #endif + /* Disable C linkage for C++ Compilers: */ #if defined(__cplusplus) } -- cgit v1.2.3