Make Low Level host demos use void pointers for the configuration descriptor, to...
[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 \ref CDCHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t ConfigDescriptorData[512];
51 void* CurrConfigLocation = ConfigDescriptorData;
52 uint16_t CurrConfigBytesRem;
53 uint8_t FoundEndpoints = 0;
54
55 /* Retrieve the entire configuration descriptor into the allocated buffer */
56 switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
57 {
58 case HOST_GETCONFIG_Successful:
59 break;
60 case HOST_GETCONFIG_InvalidData:
61 return InvalidConfigDataReturned;
62 case HOST_GETCONFIG_BuffOverflow:
63 return DescriptorTooLarge;
64 default:
65 return ControlError;
66 }
67
68 /* Get the CDC control interface from the configuration descriptor */
69 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
70 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
71 {
72 /* Descriptor not found, error out */
73 return NoCDCInterfaceFound;
74 }
75
76 /* Get the IN and OUT data and IN notification endpoints for the CDC interface */
77 while (FoundEndpoints != ((1 << CDC_NOTIFICATIONPIPE) | (1 << CDC_DATAPIPE_IN) | (1 << CDC_DATAPIPE_OUT)))
78 {
79 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
80 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
81 DComp_NextCDCDataInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
82 {
83 /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */
84 if (FoundEndpoints & (1 << CDC_NOTIFICATIONPIPE))
85 {
86 /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */
87 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
88 DComp_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
89 {
90 /* Descriptor not found, error out */
91 return NoCDCInterfaceFound;
92 }
93 }
94 else
95 {
96 /* Clear the found endpoints mask, since any already processed endpoints aren't in the CDC interface we need */
97 FoundEndpoints = 0;
98
99 /* Disable any already configured pipes from the invalid CDC interfaces */
100 Pipe_SelectPipe(CDC_NOTIFICATIONPIPE);
101 Pipe_DisablePipe();
102 Pipe_SelectPipe(CDC_DATAPIPE_IN);
103 Pipe_DisablePipe();
104 Pipe_SelectPipe(CDC_DATAPIPE_OUT);
105 Pipe_DisablePipe();
106
107 /* Get the next CDC control interface from the configuration descriptor (CDC class has two CDC interfaces) */
108 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
109 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
110 {
111 /* Descriptor not found, error out */
112 return NoCDCInterfaceFound;
113 }
114 }
115
116 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
117 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
118 DComp_NextCDCDataInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
119 {
120 /* Descriptor not found, error out */
121 return NoEndpointFound;
122 }
123 }
124
125 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
126
127 /* Check if the found endpoint is a interrupt or bulk type descriptor */
128 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
129 {
130 /* If the endpoint is a IN type interrupt endpoint */
131 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
132 {
133 /* Configure the notification pipe */
134 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
135 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
136
137 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
138
139 /* Set the flag indicating that the notification pipe has been found */
140 FoundEndpoints |= (1 << CDC_NOTIFICATIONPIPE);
141 }
142 }
143 else
144 {
145 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
146 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
147 {
148 /* Configure the data IN pipe */
149 Pipe_ConfigurePipe(CDC_DATAPIPE_IN, EP_TYPE_BULK, PIPE_TOKEN_IN,
150 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
151
152 /* Set the flag indicating that the data IN pipe has been found */
153 FoundEndpoints |= (1 << CDC_DATAPIPE_IN);
154 }
155 else
156 {
157 /* Configure the data OUT pipe */
158 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,
159 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
160
161 /* Set the flag indicating that the data OUT pipe has been found */
162 FoundEndpoints |= (1 << CDC_DATAPIPE_OUT);
163 }
164 }
165 }
166
167 /* Valid data found, return success */
168 return SuccessfulConfigRead;
169 }
170
171 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
172 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
173 * descriptor processing if an incompatible descriptor configuration is found.
174 *
175 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
176 *
177 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
178 */
179 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
180 {
181 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
182 {
183 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
184 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
185 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
186 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
187 {
188 return DESCRIPTOR_SEARCH_Found;
189 }
190 }
191
192 return DESCRIPTOR_SEARCH_NotFound;
193 }
194
195 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
196 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
197 * descriptor processing if an incompatible descriptor configuration is found.
198 *
199 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
200 *
201 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
202 */
203 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
204 {
205 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
206 {
207 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
208 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
209 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
210 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
211 {
212 return DESCRIPTOR_SEARCH_Found;
213 }
214 }
215
216 return DESCRIPTOR_SEARCH_NotFound;
217 }
218
219 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
220 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
221 * descriptor processing if an incompatible descriptor configuration is found.
222 *
223 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
224 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
225 * using a different comparator to determine if it is another CDC class interface).
226 *
227 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
228 */
229 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
230 {
231 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
232 {
233 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
234 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
235
236 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
237 return DESCRIPTOR_SEARCH_Found;
238 }
239 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
240 {
241 return DESCRIPTOR_SEARCH_Fail;
242 }
243
244 return DESCRIPTOR_SEARCH_NotFound;
245 }