Copy existing Host mode demos to new ClassDriver and LowLevel subfolders.
[pub/USBasp.git] / Demos / Host / ClassDriver / 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(&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(&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_SetInfiniteINRequests();
142 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
143
144 /* Set the flag indicating that the notification pipe has been found */
145 FoundEndpoints |= (1 << CDC_NOTIFICATIONPIPE);
146 }
147 }
148 else
149 {
150 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
151 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
152 {
153 /* Configure the data IN pipe */
154 Pipe_ConfigurePipe(CDC_DATAPIPE_IN, EP_TYPE_BULK, PIPE_TOKEN_IN,
155 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
156
157 Pipe_SetInfiniteINRequests();
158 Pipe_Unfreeze();
159
160 /* Set the flag indicating that the data IN pipe has been found */
161 FoundEndpoints |= (1 << CDC_DATAPIPE_IN);
162 }
163 else
164 {
165 /* Configure the data OUT pipe */
166 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,
167 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
168
169 Pipe_Unfreeze();
170
171 /* Set the flag indicating that the data OUT pipe has been found */
172 FoundEndpoints |= (1 << CDC_DATAPIPE_OUT);
173 }
174 }
175 }
176
177 /* Valid data found, return success */
178 return SuccessfulConfigRead;
179 }
180
181 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
182 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
183 * descriptor processing if an incompatible descriptor configuration is found.
184 *
185 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
186 *
187 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
188 */
189 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
190 {
191 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
192 {
193 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
194 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
195 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
196 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
197 {
198 return DESCRIPTOR_SEARCH_Found;
199 }
200 }
201
202 return DESCRIPTOR_SEARCH_NotFound;
203 }
204
205 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
206 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
207 * descriptor processing if an incompatible descriptor configuration is found.
208 *
209 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
210 *
211 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
212 */
213 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
214 {
215 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
216 {
217 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
218 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
219 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
220 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
221 {
222 return DESCRIPTOR_SEARCH_Found;
223 }
224 }
225
226 return DESCRIPTOR_SEARCH_NotFound;
227 }
228
229 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
230 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
231 * descriptor processing if an incompatible descriptor configuration is found.
232 *
233 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
234 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
235 * using a different comparator to determine if it is another CDC class interface).
236 *
237 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
238 */
239 uint8_t DComp_NextInterfaceCDCDataEndpoint(void* CurrentDescriptor)
240 {
241 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
242 {
243 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
244 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
245
246 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
247 return DESCRIPTOR_SEARCH_Found;
248 }
249 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
250 {
251 return DESCRIPTOR_SEARCH_Fail;
252 }
253
254 return DESCRIPTOR_SEARCH_NotFound;
255 }