Update copyright year to 2010.
[pub/USBasp.git] / Demos / Host / LowLevel / RNDISEthernetHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 RNDIS interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint.
45 *
46 * \return An error code from the \ref RNDISHost_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_Host_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 NoRNDISInterfaceFound;
74 }
75
76 /* Get the IN and OUT data and IN notification endpoints for the RNDIS interface */
77 while (FoundEndpoints != ((1 << RNDIS_NOTIFICATIONPIPE) | (1 << RNDIS_DATAPIPE_IN) | (1 << RNDIS_DATAPIPE_OUT)))
78 {
79 /* Fetch the next bulk or interrupt endpoint from the current RNDIS 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 << RNDIS_NOTIFICATIONPIPE))
85 {
86 /* Get the next CDC data interface from the configuration descriptor (RNDIS 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 NoRNDISInterfaceFound;
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 RNDIS interfaces */
100 Pipe_SelectPipe(RNDIS_NOTIFICATIONPIPE);
101 Pipe_DisablePipe();
102 Pipe_SelectPipe(RNDIS_DATAPIPE_IN);
103 Pipe_DisablePipe();
104 Pipe_SelectPipe(RNDIS_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 NoRNDISInterfaceFound;
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(RNDIS_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 << RNDIS_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 /* Kill the configured OUT pipe if the data endpoints are bidirectional */
149 if (Pipe_IsEndpointBound(EndpointData->EndpointAddress))
150 Pipe_DisablePipe();
151
152 /* Configure the data IN pipe */
153 Pipe_ConfigurePipe(RNDIS_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 << RNDIS_DATAPIPE_IN);
158 }
159 else
160 {
161 /* Only configure the OUT data pipe if the data endpoints haev not shown to be bidirectional */
162 if (!(Pipe_IsEndpointBound(EndpointData->EndpointAddress)))
163 {
164 /* Configure the data OUT pipe */
165 Pipe_ConfigurePipe(RNDIS_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,
166 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
167 }
168
169 /* Set the flag indicating that the data OUT pipe has been found */
170 FoundEndpoints |= (1 << RNDIS_DATAPIPE_OUT);
171 }
172 }
173 }
174
175 /* Valid data found, return success */
176 return SuccessfulConfigRead;
177 }
178
179 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
180 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
181 * descriptor processing if an incompatible descriptor configuration is found.
182 *
183 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
184 *
185 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
186 */
187 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
188 {
189 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
190 {
191 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
192 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
193 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
194 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
195 {
196 return DESCRIPTOR_SEARCH_Found;
197 }
198 }
199
200 return DESCRIPTOR_SEARCH_NotFound;
201 }
202
203 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
204 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
205 * descriptor processing if an incompatible descriptor configuration is found.
206 *
207 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
208 *
209 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
210 */
211 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
212 {
213 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
214 {
215 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
216 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
217 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
218 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
219 {
220 return DESCRIPTOR_SEARCH_Found;
221 }
222 }
223
224 return DESCRIPTOR_SEARCH_NotFound;
225 }
226
227 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
228 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
229 * descriptor processing if an incompatible descriptor configuration is found.
230 *
231 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
232 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
233 * using a different comparator to determine if it is another CDC class interface).
234 *
235 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
236 */
237 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
238 {
239 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
240 {
241 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
242 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
243
244 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
245 return DESCRIPTOR_SEARCH_Found;
246 }
247 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
248 {
249 return DESCRIPTOR_SEARCH_Fail;
250 }
251
252 return DESCRIPTOR_SEARCH_NotFound;
253 }