]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/MissileLauncher/ConfigDescriptor.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / MissileLauncher / 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 HID interface descriptor containing at least one Interrupt type IN endpoint.
45 *
46 * \return An error code from the GenericHIDHost_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* HIDInterface = NULL;
55 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
56 USB_Descriptor_Endpoint_t* DataOUTEndpoint = 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 while (!(DataINEndpoint) || !(DataOUTEndpoint))
72 {
73 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
74 if (!(HIDInterface) ||
75 USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
76 DComp_NextHIDInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
77 {
78 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
79 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
80 if (DataINEndpoint)
81 break;
82
83 /* Get the next HID interface from the configuration descriptor */
84 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
85 DComp_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Descriptor not found, error out */
88 return NoCompatibleInterfaceFound;
89 }
90
91 /* Save the interface in case we need to refer back to it later */
92 HIDInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
93
94 /* Clear any found endpoints */
95 DataOUTEndpoint = NULL;
96
97 /* Skip the remainder of the loop as we have not found an endpoint yet */
98 continue;
99 }
100
101 /* Retrieve the endpoint address from the endpoint descriptor */
102 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
103
104 /* If the endpoint is a IN type endpoint */
105 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
106 DataINEndpoint = EndpointData;
107 else
108 DataOUTEndpoint = EndpointData;
109 }
110
111 /* Configure the HID data IN pipe */
112 Pipe_ConfigurePipe(HID_DATA_IN_PIPE, EP_TYPE_INTERRUPT, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
113 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
114
115 /* Check if the HID interface contained an optional OUT data endpoint */
116 if (DataOUTEndpoint)
117 {
118 /* Configure the HID data OUT pipe */
119 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE, EP_TYPE_INTERRUPT, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
120 }
121
122 /* Valid data found, return success */
123 return SuccessfulConfigRead;
124 }
125
126 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
127 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
128 * descriptor processing if an incompatible descriptor configuration is found.
129 *
130 * This comparator searches for the next Interface descriptor of the correct HID Class value.
131 *
132 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
133 */
134 uint8_t DComp_NextHIDInterface(void* CurrentDescriptor)
135 {
136 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
137
138 /* Determine if the current descriptor is an interface descriptor */
139 if (Header->Type == DTYPE_Interface)
140 {
141 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
142
143 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
144 if (Interface->Class == HID_CLASS)
145 {
146 /* Indicate that the descriptor being searched for has been found */
147 return DESCRIPTOR_SEARCH_Found;
148 }
149 }
150
151 /* Current descriptor does not match what this comparator is looking for */
152 return DESCRIPTOR_SEARCH_NotFound;
153 }
154
155 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
156 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
157 * descriptor processing if an incompatible descriptor configuration is found.
158 *
159 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
160 * aborting the search if another interface descriptor is found before the required endpoint.
161 *
162 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
163 */
164 uint8_t DComp_NextHIDInterfaceDataEndpoint(void* CurrentDescriptor)
165 {
166 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
167
168 /* Determine the type of the current descriptor */
169 if (Header->Type == DTYPE_Endpoint)
170 {
171 /* Indicate that the descriptor being searched for has been found */
172 return DESCRIPTOR_SEARCH_Found;
173 }
174 else if (Header->Type == DTYPE_Interface)
175 {
176 /* Indicate that the search has failed prematurely and should be aborted */
177 return DESCRIPTOR_SEARCH_Fail;
178 }
179 else
180 {
181 /* Current descriptor does not match what this comparator is looking for */
182 return DESCRIPTOR_SEARCH_NotFound;
183 }
184 }
185
Imprint / Impressum