Fix low level host mode demos not correctly fetching the next endpoint when an invali...
[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
54 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
55 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
56 USB_Descriptor_Endpoint_t* NotificationEndpoint = NULL;
57
58 /* Retrieve the entire configuration descriptor into the allocated buffer */
59 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
60 {
61 case HOST_GETCONFIG_Successful:
62 break;
63 case HOST_GETCONFIG_InvalidData:
64 return InvalidConfigDataReturned;
65 case HOST_GETCONFIG_BuffOverflow:
66 return DescriptorTooLarge;
67 default:
68 return ControlError;
69 }
70
71 /* Get the first RNDIS control interface from the configuration descriptor */
72 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
73 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
74 {
75 /* Descriptor not found, error out */
76 return NoCompatibleInterfaceFound;
77 }
78
79 while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(NotificationEndpoint))
80 {
81 /* Get the next RNDIS interface's endpoint descriptor */
82 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
83 DComp_NextCDCDataInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
84 {
85 /* Check if we have already found the control interface's notification endpoint or not */
86 if (NotificationEndpoint)
87 {
88 /* Get the next RNDIS data interface from the configuration descriptor */
89 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
90 DComp_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
91 {
92 /* Descriptor not found, error out */
93 return NoCompatibleInterfaceFound;
94 }
95
96 /* Clear any found endpoints */
97 DataINEndpoint = NULL;
98 DataOUTEndpoint = NULL;
99 }
100 else
101 {
102 /* Get the next RNDIS control interface from the configuration descriptor */
103 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
104 DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
105 {
106 /* Descriptor not found, error out */
107 return NoCompatibleInterfaceFound;
108 }
109
110 /* Clear any found endpoints */
111 NotificationEndpoint = NULL;
112 }
113
114 /* Skip the remainder of the loop as we have not found an endpoint yet */
115 continue;
116 }
117
118 /* Retrieve the endpoint address from the endpoint descriptor */
119 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
120
121 /* If the endpoint is a IN type endpoint */
122 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
123 {
124 /* Check if the found endpoint is a interrupt or bulk type descriptor */
125 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
126 NotificationEndpoint = EndpointData;
127 else
128 DataINEndpoint = EndpointData;
129 }
130 else
131 {
132 DataOUTEndpoint = EndpointData;
133 }
134 }
135
136 /* Configure the RNDIS data IN pipe */
137 Pipe_ConfigurePipe(RNDIS_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
138 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
139
140 /* Configure the RNDIS data OUT pipe */
141 Pipe_ConfigurePipe(RNDIS_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
142 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, PIPE_BANK_SINGLE);
143
144 /* Configure the RNDIS notification pipe */
145 Pipe_ConfigurePipe(RNDIS_NOTIFICATION_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
146 NotificationEndpoint->EndpointAddress, NotificationEndpoint->EndpointSize, PIPE_BANK_SINGLE);
147 Pipe_SetInterruptPeriod(NotificationEndpoint->PollingIntervalMS);
148
149 /* Valid data found, return success */
150 return SuccessfulConfigRead;
151 }
152
153 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
154 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
155 * descriptor processing if an incompatible descriptor configuration is found.
156 *
157 * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
158 *
159 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
160 */
161 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
162 {
163 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
164 {
165 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
166 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
167 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
168 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
169 {
170 return DESCRIPTOR_SEARCH_Found;
171 }
172 }
173
174 return DESCRIPTOR_SEARCH_NotFound;
175 }
176
177 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
178 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
179 * descriptor processing if an incompatible descriptor configuration is found.
180 *
181 * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
182 *
183 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
184 */
185 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
186 {
187 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
188 {
189 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
190 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
191 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
192 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
193 {
194 return DESCRIPTOR_SEARCH_Found;
195 }
196 }
197
198 return DESCRIPTOR_SEARCH_NotFound;
199 }
200
201 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
202 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
203 * descriptor processing if an incompatible descriptor configuration is found.
204 *
205 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
206 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
207 * using a different comparator to determine if it is another CDC class interface).
208 *
209 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
210 */
211 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
212 {
213 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
214 {
215 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
216 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
217
218 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
219 return DESCRIPTOR_SEARCH_Found;
220 }
221 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
222 {
223 return DESCRIPTOR_SEARCH_Fail;
224 }
225
226 return DESCRIPTOR_SEARCH_NotFound;
227 }