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 GenericHIDHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t* ConfigDescriptorData
;
51 uint16_t ConfigDescriptorSize
;
53 uint8_t FoundEndpoints
= 0;
55 /* Get Configuration Descriptor size from the device */
56 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
59 /* Ensure that the Configuration Descriptor isn't too large */
60 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
61 return DescriptorTooLarge
;
63 /* Allocate enough memory for the entire config descriptor */
64 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
66 /* Retrieve the entire configuration descriptor into the allocated buffer */
67 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
);
69 /* Validate returned data - ensure first entry is a configuration header descriptor */
70 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
71 return InvalidConfigDataReturned
;
73 /* Get the HID interface from the configuration descriptor */
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
75 DComp_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
77 /* Descriptor not found, error out */
78 return NoHIDInterfaceFound
;
81 while (FoundEndpoints
!= ((1 << HID_DATA_IN_PIPE
) | (1 << HID_DATA_OUT_PIPE
)))
83 /* Get the next HID interface's data endpoint descriptor */
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
85 DComp_NextInterfaceHIDDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
87 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
88 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
89 if (FoundEndpoints
== (1 << HID_DATA_IN_PIPE
))
92 /* Descriptor not found, error out */
93 return NoEndpointFound
;
96 /* Retrieve the endpoint address from the endpoint descriptor */
97 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
99 /* If the endpoint is a IN type endpoint */
100 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
102 /* Configure the HID data IN pipe */
103 Pipe_ConfigurePipe(HID_DATA_IN_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
104 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
106 FoundEndpoints
|= (1 << HID_DATA_IN_PIPE
);
110 /* Configure the HID data OUT pipe */
111 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
112 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
114 FoundEndpoints
|= (1 << HID_DATA_OUT_PIPE
);
118 /* Valid data found, return success */
119 return SuccessfulConfigRead
;
122 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
123 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
124 * descriptor processing if an incompatible descriptor configuration is found.
126 * This comparator searches for the next Interface descriptor of the correct HID Class value.
128 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
130 uint8_t DComp_NextHIDInterface(void* CurrentDescriptor
)
132 /* Determine if the current descriptor is an interface descriptor */
133 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
135 /* Check the HID descriptor class, break out if correct class/protocol interface found */
136 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== HID_CLASS
)
138 /* Indicate that the descriptor being searched for has been found */
139 return DESCRIPTOR_SEARCH_Found
;
143 /* Current descriptor does not match what this comparator is looking for */
144 return DESCRIPTOR_SEARCH_NotFound
;
147 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
148 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
149 * descriptor processing if an incompatible descriptor configuration is found.
151 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
152 * aborting the search if another interface descriptor is found before the required endpoint.
154 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
156 uint8_t DComp_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor
)
158 /* Determine the type of the current descriptor */
159 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
161 /* Indicate that the descriptor being searched for has been found */
162 return DESCRIPTOR_SEARCH_Found
;
164 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
166 /* Indicate that the search has failed prematurely and should be aborted */
167 return DESCRIPTOR_SEARCH_Fail
;
170 /* Current descriptor does not match what this comparator is looking for */
171 return DESCRIPTOR_SEARCH_NotFound
;