]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Host/LowLevel/VirtualSerialHost/ConfigDescriptor.c
Merge commit '28203e909e83b1ac6becb45a3eadae23b190df32' into master-core-pull
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Host / LowLevel / VirtualSerialHost / ConfigDescriptor.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 disclaims 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_DIR_MASK) == ENDPOINT_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, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
135
136         /* Configure the CDC data OUT pipe */
137         Pipe_ConfigurePipe(CDC_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
138
139         /* Configure the CDC notification pipe */
140         Pipe_ConfigurePipe(CDC_NOTIFICATION_PIPE, EP_TYPE_INTERRUPT, NotificationEndpoint->EndpointAddress, NotificationEndpoint->EndpointSize, 1);
141         Pipe_SetInterruptPeriod(NotificationEndpoint->PollingIntervalMS);
142
143         /* Valid data found, return success */
144         return SuccessfulConfigRead;
145 }
146
147 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
148  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
149  *  descriptor processing if an incompatible descriptor configuration is found.
150  *
151  *  This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
152  *
153  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
154  */
155 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
156 {
157         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
158
159         if (Header->Type == DTYPE_Interface)
160         {
161                 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
162
163                 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
164                 if ((Interface->Class    == CDC_CSCP_CDCClass)    &&
165                     (Interface->SubClass == CDC_CSCP_ACMSubclass) &&
166                     (Interface->Protocol == CDC_CSCP_ATCommandProtocol))
167                 {
168                         return DESCRIPTOR_SEARCH_Found;
169                 }
170         }
171
172         return DESCRIPTOR_SEARCH_NotFound;
173 }
174
175 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
176  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
177  *  descriptor processing if an incompatible descriptor configuration is found.
178  *
179  *  This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
180  *
181  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
182  */
183 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
184 {
185         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
186
187         if (Header->Type == DTYPE_Interface)
188         {
189                 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
190
191                 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
192                 if ((Interface->Class    == CDC_CSCP_CDCDataClass)   &&
193                     (Interface->SubClass == CDC_CSCP_NoDataSubclass) &&
194                     (Interface->Protocol == CDC_CSCP_NoDataProtocol))
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 bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
208  *  aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
209  *  using a different comparator to determine if it is another CDC class interface).
210  *
211  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
212  */
213 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
214 {
215         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
216
217         if (Header->Type == DTYPE_Endpoint)
218         {
219                 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
220
221                 if (((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK) ||
222                     ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT))
223                 {
224                         return DESCRIPTOR_SEARCH_Found;
225                 }
226         }
227         else if (Header->Type == DTYPE_Interface)
228         {
229                 return DESCRIPTOR_SEARCH_Fail;
230         }
231
232         return DESCRIPTOR_SEARCH_NotFound;
233 }
234