Fixed MassStorage demo not clearing the reset flag when a Mass Storage Reset is issue...
[pub/USBasp.git] / Demos / Host / LowLevel / CDCHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
36 */
37
38 #include "ConfigDescriptor.h"
39
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.
43 *
44 * This routine searches for a CDC interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint.
45 *
46 * \return An error code from the CDCHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t* ConfigDescriptorData;
51 uint16_t ConfigDescriptorSize;
52 uint8_t FoundEndpoints = 0;
53
54 /* Get Configuration Descriptor size from the device */
55 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
56 return ControlError;
57
58 /* Ensure that the Configuration Descriptor isn't too large */
59 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
60 return DescriptorTooLarge;
61
62 /* Allocate enough memory for the entire config descriptor */
63 ConfigDescriptorData = alloca(ConfigDescriptorSize);
64
65 /* Retrieve the entire configuration descriptor into the allocated buffer */
66 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
67
68 /* Validate returned data - ensure first entry is a configuration header descriptor */
69 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
70 return InvalidConfigDataReturned;
71
72 /* Get the CDC control interface from the configuration descriptor */
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
74 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
75 {
76 /* Descriptor not found, error out */
77 return NoCDCInterfaceFound;
78 }
79
80 /* Get the IN and OUT data endpoints for the CDC interface */
81 while (FoundEndpoints != ((1 << CDC_NOTIFICATIONPIPE) | (1 << CDC_DATAPIPE_IN) | (1 << CDC_DATAPIPE_OUT)))
82 {
83 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
85 DComp_NextInterfaceCDCDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */
88 if (FoundEndpoints & (1 << CDC_NOTIFICATIONPIPE))
89 {
90 /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */
91 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
92 DComp_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
93 {
94 /* Descriptor not found, error out */
95 return NoCDCInterfaceFound;
96 }
97 }
98 else
99 {
100 /* Clear the found endpoints mask, since any already processed endpoints aren't in the CDC interface we need */
101 FoundEndpoints = 0;
102
103 /* Disable any already configured pipes from the invalid CDC interfaces */
104 Pipe_SelectPipe(CDC_NOTIFICATIONPIPE);
105 Pipe_DisablePipe();
106 Pipe_SelectPipe(CDC_DATAPIPE_IN);
107 Pipe_DisablePipe();
108 Pipe_SelectPipe(CDC_DATAPIPE_OUT);
109 Pipe_DisablePipe();
110
111 /* Get the next CDC control interface from the configuration descriptor (CDC class has two CDC interfaces) */
112 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
113 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
114 {
115 /* Descriptor not found, error out */
116 return NoCDCInterfaceFound;
117 }
118 }
119
120 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
121 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
122 DComp_NextInterfaceCDCDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
123 {
124 /* Descriptor not found, error out */
125 return NoEndpointFound;
126 }
127 }
128
129 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
130
131 /* Check if the found endpoint is a interrupt or bulk type descriptor */
132 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
133 {
134 /* If the endpoint is a IN type interrupt endpoint */
135 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
136 {
137 /* Configure the notification pipe */
138 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
139 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
140
141 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
142
143 /* Set the flag indicating that the notification pipe has been found */
144 FoundEndpoints |= (1 << CDC_NOTIFICATIONPIPE);
145 }
146 }
147 else
148 {
149 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
150 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
151 {
152 /* Configure the data IN pipe */
153 Pipe_ConfigurePipe(CDC_DATAPIPE_IN, EP_TYPE_BULK, PIPE_TOKEN_IN,
154 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
155
156 /* Set the flag indicating that the data IN pipe has been found */
157 FoundEndpoints |= (1 << CDC_DATAPIPE_IN);
158 }
159 else
160 {
161 /* Configure the data OUT pipe */
162 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,
163 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
164
165 /* Set the flag indicating that the data OUT pipe has been found */
166 FoundEndpoints |= (1 << CDC_DATAPIPE_OUT);
167 }
168 }
169 }
170
171 /* Valid data found, return success */
172 return SuccessfulConfigRead;
173 }
174
175 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
176 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
177 * descriptor processing if an incompatible descriptor configuration is found.
178 *
179 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
180 *
181 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
182 */
183 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
184 {
185 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
186 {
187 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
188 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
189 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
190 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
191 {
192 return DESCRIPTOR_SEARCH_Found;
193 }
194 }
195
196 return DESCRIPTOR_SEARCH_NotFound;
197 }
198
199 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
200 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
201 * descriptor processing if an incompatible descriptor configuration is found.
202 *
203 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
204 *
205 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
206 */
207 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
208 {
209 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
210 {
211 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
212 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
213 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
214 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
215 {
216 return DESCRIPTOR_SEARCH_Found;
217 }
218 }
219
220 return DESCRIPTOR_SEARCH_NotFound;
221 }
222
223 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
224 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
225 * descriptor processing if an incompatible descriptor configuration is found.
226 *
227 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
228 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
229 * using a different comparator to determine if it is another CDC class interface).
230 *
231 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
232 */
233 uint8_t DComp_NextInterfaceCDCDataEndpoint(void* CurrentDescriptor)
234 {
235 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
236 {
237 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
238 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
239
240 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
241 return DESCRIPTOR_SEARCH_Found;
242 }
243 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
244 {
245 return DESCRIPTOR_SEARCH_Fail;
246 }
247
248 return DESCRIPTOR_SEARCH_NotFound;
249 }