3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations
34 * needed to communication with an attached USB device. Descriptors are special computer-readable structures
35 * which the host requests upon device enumeration, to determine the device's capabilities and functions.
38 #include "ConfigDescriptor.h"
40 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
41 * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
42 * with compatible devices.
44 * This routine searches for a HID interface descriptor containing at least one Interrupt type IN endpoint.
46 * \return An error code from the \ref GenericHIDHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t ConfigDescriptorData
[512];
51 uint8_t* CurrConfigLocation
= ConfigDescriptorData
;
52 uint16_t CurrConfigBytesRem
;
53 uint8_t FoundEndpoints
= 0;
55 /* Retrieve the entire configuration descriptor into the allocated buffer */
56 switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
)))
58 case HOST_GETCONFIG_Successful
:
60 case HOST_GETCONFIG_InvalidData
:
61 return InvalidConfigDataReturned
;
62 case HOST_GETCONFIG_BuffOverflow
:
63 return DescriptorTooLarge
;
68 /* Get the HID interface from the configuration descriptor */
69 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
70 DComp_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
72 /* Descriptor not found, error out */
73 return NoHIDInterfaceFound
;
76 while (FoundEndpoints
!= ((1 << HID_DATA_IN_PIPE
) | (1 << HID_DATA_OUT_PIPE
)))
78 /* Get the next HID interface's data endpoint descriptor */
79 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
80 DComp_NextHIDInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
82 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
83 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
84 if (FoundEndpoints
== (1 << HID_DATA_IN_PIPE
))
87 /* Descriptor not found, error out */
88 return NoEndpointFound
;
91 /* Retrieve the endpoint address from the endpoint descriptor */
92 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
94 /* If the endpoint is a IN type endpoint */
95 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
97 /* Configure the HID data IN pipe */
98 Pipe_ConfigurePipe(HID_DATA_IN_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
99 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
101 FoundEndpoints
|= (1 << HID_DATA_IN_PIPE
);
105 /* Configure the HID data OUT pipe */
106 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
107 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
109 FoundEndpoints
|= (1 << HID_DATA_OUT_PIPE
);
113 /* Valid data found, return success */
114 return SuccessfulConfigRead
;
117 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
118 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
119 * descriptor processing if an incompatible descriptor configuration is found.
121 * This comparator searches for the next Interface descriptor of the correct HID Class value.
123 * \return A value from the \ref DSEARCH_Return_ErrorCodes_t enum
125 uint8_t DComp_NextHIDInterface(void* CurrentDescriptor
)
127 /* Determine if the current descriptor is an interface descriptor */
128 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
130 /* Check the HID descriptor class, break out if correct class/protocol interface found */
131 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== HID_CLASS
)
133 /* Indicate that the descriptor being searched for has been found */
134 return DESCRIPTOR_SEARCH_Found
;
138 /* Current descriptor does not match what this comparator is looking for */
139 return DESCRIPTOR_SEARCH_NotFound
;
142 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
143 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
144 * descriptor processing if an incompatible descriptor configuration is found.
146 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
147 * aborting the search if another interface descriptor is found before the required endpoint.
149 * \return A value from the \ref DSEARCH_Return_ErrorCodes_t enum
151 uint8_t DComp_NextHIDInterfaceDataEndpoint(void* CurrentDescriptor
)
153 /* Determine the type of the current descriptor */
154 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
156 /* Indicate that the descriptor being searched for has been found */
157 return DESCRIPTOR_SEARCH_Found
;
159 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
161 /* Indicate that the search has failed prematurely and should be aborted */
162 return DESCRIPTOR_SEARCH_Fail
;
165 /* Current descriptor does not match what this comparator is looking for */
166 return DESCRIPTOR_SEARCH_NotFound
;