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