Moved all source to the trunk directory.
[pub/USBasp.git] / Demos / 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_Host_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_Host_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 interface from the configuration descriptor */
73 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, NextCDCInterface))
74 {
75 /* Descriptor not found, error out */
76 return NoCDCInterfaceFound;
77 }
78
79 /* Get the IN and OUT data endpoints for the CDC interface */
80 while (FoundEndpoints != ((1 << CDC_NOTIFICATIONPIPE) | (1 << CDC_DATAPIPE_IN) | (1 << CDC_DATAPIPE_OUT)))
81 {
82 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
83 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
84 NextInterfaceCDCDataEndpoint))
85 {
86 /* Get the next CDC interface from the configuration descriptor (CDC class has two CDC interfaces) */
87 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, NextCDCInterface))
88 {
89 /* Descriptor not found, error out */
90 return NoCDCInterfaceFound;
91 }
92
93 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */
94 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
95 NextInterfaceCDCDataEndpoint))
96 {
97 /* Descriptor not found, error out */
98 return NoEndpointFound;
99 }
100 }
101
102 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
103
104 /* Check if the found endpoint is a interrupt or bulk type descriptor */
105 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
106 {
107 /* If the endpoint is a IN type interrupt endpoint */
108 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
109 {
110 /* Configure the notification pipe */
111 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
112 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
113
114 Pipe_SetInfiniteINRequests();
115 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
116
117 /* Set the flag indicating that the notification pipe has been found */
118 FoundEndpoints |= (1 << CDC_NOTIFICATIONPIPE);
119 }
120 }
121 else
122 {
123 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
124 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
125 {
126 /* Configure the data IN pipe */
127 Pipe_ConfigurePipe(CDC_DATAPIPE_IN, EP_TYPE_BULK, PIPE_TOKEN_IN,
128 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
129
130 Pipe_SetInfiniteINRequests();
131 Pipe_Unfreeze();
132
133 /* Set the flag indicating that the data IN pipe has been found */
134 FoundEndpoints |= (1 << CDC_DATAPIPE_IN);
135 }
136 else
137 {
138 /* Configure the data OUT pipe */
139 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT, EP_TYPE_BULK, PIPE_TOKEN_OUT,
140 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
141
142 Pipe_Unfreeze();
143
144 /* Set the flag indicating that the data OUT pipe has been found */
145 FoundEndpoints |= (1 << CDC_DATAPIPE_OUT);
146 }
147 }
148 }
149
150 /* Valid data found, return success */
151 return SuccessfulConfigRead;
152 }
153
154 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
155 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
156 * descriptor processing if an incompatible descriptor configuration is found.
157 *
158 * This comparator searches for the next Interface descriptor of the correct CDC Class, Subclass and Protocol values.
159 *
160 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
161 */
162 DESCRIPTOR_COMPARATOR(NextCDCInterface)
163 {
164 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
165 {
166 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
167 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_CONTROL_CLASS) &&
168 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_CONTROL_SUBCLASS) &&
169 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_CONTROL_PROTOCOL))
170 {
171 return Descriptor_Search_Found;
172 }
173
174 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
175 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == CDC_DATA_CLASS) &&
176 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == CDC_DATA_SUBCLASS) &&
177 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == CDC_DATA_PROTOCOL))
178 {
179 return Descriptor_Search_Found;
180 }
181 }
182
183 return Descriptor_Search_NotFound;
184 }
185
186 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
187 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
188 * descriptor processing if an incompatible descriptor configuration is found.
189 *
190 * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
191 * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
192 * using a different comparator to determine if it is another CDC class interface).
193 *
194 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
195 */
196 DESCRIPTOR_COMPARATOR(NextInterfaceCDCDataEndpoint)
197 {
198 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
199 {
200 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
201 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
202
203 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
204 return Descriptor_Search_Found;
205 }
206 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
207 {
208 return Descriptor_Search_Fail;
209 }
210
211 return Descriptor_Search_NotFound;
212 }