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 CDC interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint.
46 * \return An error code from the CDCHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t* ConfigDescriptorData
;
51 uint16_t ConfigDescriptorSize
;
52 uint8_t FoundEndpoints
= 0;
54 /* Get Configuration Descriptor size from the device */
55 if (USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
58 /* Ensure that the Configuration Descriptor isn't too large */
59 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
60 return DescriptorTooLarge
;
62 /* Allocate enough memory for the entire config descriptor */
63 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
65 /* Retrieve the entire configuration descriptor into the allocated buffer */
66 USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, ConfigDescriptorData
);
68 /* Validate returned data - ensure first entry is a configuration header descriptor */
69 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
70 return InvalidConfigDataReturned
;
72 /* Get the CDC control interface from the configuration descriptor */
73 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextCDCControlInterface
))
75 /* Descriptor not found, error out */
76 return NoCDCInterfaceFound
;
79 /* Get the IN and OUT data endpoints for the CDC interface */
80 while (FoundEndpoints
!= ((1 << CDC_NOTIFICATIONPIPE
) | (1 << CDC_DATAPIPE_IN
) | (1 << CDC_DATAPIPE_OUT
)))
82 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
83 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
84 NextInterfaceCDCDataEndpoint
))
86 /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */
87 if (FoundEndpoints
& (1 << CDC_NOTIFICATIONPIPE
))
89 /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */
90 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextCDCDataInterface
))
92 /* Descriptor not found, error out */
93 return NoCDCInterfaceFound
;
98 /* Get the next CDC control interface from the configuration descriptor (CDC class has two CDC interfaces) */
99 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextCDCControlInterface
))
101 /* Descriptor not found, error out */
102 return NoCDCInterfaceFound
;
106 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
107 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
108 NextInterfaceCDCDataEndpoint
))
110 /* Descriptor not found, error out */
111 return NoEndpointFound
;
115 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
117 /* Check if the found endpoint is a interrupt or bulk type descriptor */
118 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
120 /* If the endpoint is a IN type interrupt endpoint */
121 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
123 /* Configure the notification pipe */
124 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
125 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
127 Pipe_SetInfiniteINRequests();
128 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
);
130 /* Set the flag indicating that the notification pipe has been found */
131 FoundEndpoints
|= (1 << CDC_NOTIFICATIONPIPE
);
136 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
137 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
139 /* Configure the data IN pipe */
140 Pipe_ConfigurePipe(CDC_DATAPIPE_IN
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
141 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
143 Pipe_SetInfiniteINRequests();
146 /* Set the flag indicating that the data IN pipe has been found */
147 FoundEndpoints
|= (1 << CDC_DATAPIPE_IN
);
151 /* Configure the data OUT pipe */
152 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
153 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
157 /* Set the flag indicating that the data OUT pipe has been found */
158 FoundEndpoints
|= (1 << CDC_DATAPIPE_OUT
);
163 /* Valid data found, return success */
164 return SuccessfulConfigRead
;
167 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
168 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
169 * descriptor processing if an incompatible descriptor configuration is found.
171 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
173 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
175 DESCRIPTOR_COMPARATOR(NextCDCControlInterface
)
177 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
179 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
180 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== CDC_CONTROL_CLASS
) &&
181 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== CDC_CONTROL_SUBCLASS
) &&
182 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== CDC_CONTROL_PROTOCOL
))
184 return Descriptor_Search_Found
;
188 return Descriptor_Search_NotFound
;
191 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
192 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
193 * descriptor processing if an incompatible descriptor configuration is found.
195 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
197 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
199 DESCRIPTOR_COMPARATOR(NextCDCDataInterface
)
201 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
203 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
204 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== CDC_DATA_CLASS
) &&
205 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== CDC_DATA_SUBCLASS
) &&
206 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== CDC_DATA_PROTOCOL
))
208 return Descriptor_Search_Found
;
212 return Descriptor_Search_NotFound
;
215 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
216 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
217 * descriptor processing if an incompatible descriptor configuration is found.
219 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
220 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
221 * using a different comparator to determine if it is another CDC class interface).
223 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
225 DESCRIPTOR_COMPARATOR(NextInterfaceCDCDataEndpoint
)
227 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
229 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
230 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
232 if ((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
))
233 return Descriptor_Search_Found
;
235 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
237 return Descriptor_Search_Fail
;
240 return Descriptor_Search_NotFound
;