bf2c4e051a166398d850f0029e718cd6f645f17e
[pub/USBasp.git] / Demos / Host / LowLevel / KeyboardHostWithParser / 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 HID interface descriptor containing at least one Interrupt type IN endpoint and HID descriptor.
45 *
46 * \return An error code from the \ref KeyboardHostWithParser_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
56 /* Retrieve the entire configuration descriptor into the allocated buffer */
57 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
58 {
59 case HOST_GETCONFIG_Successful:
60 break;
61 case HOST_GETCONFIG_InvalidData:
62 return InvalidConfigDataReturned;
63 case HOST_GETCONFIG_BuffOverflow:
64 return DescriptorTooLarge;
65 default:
66 return ControlError;
67 }
68
69 /* Get the first HID interface from the configuration descriptor */
70 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
71 DComp_NextKeyboardInterface) != DESCRIPTOR_SEARCH_COMP_Found)
72 {
73 /* Descriptor not found, error out */
74 return NoCompatibleInterfaceFound;
75 }
76
77 /* Get the HID descriptor from the configuration descriptor */
78 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
79 DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
80 {
81 /* Descriptor not found, error out */
82 return NoCompatibleInterfaceFound;
83 }
84
85 /* Save the HID report size for later use */
86 HIDReportSize = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_HID_t)->HIDReportLength;
87
88 while (!(DataINEndpoint))
89 {
90 /* Get the next HID interface's data endpoint descriptor */
91 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
92 DComp_NextKeyboardInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
93 {
94 /* Get the next HID interface from the configuration descriptor */
95 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
96 DComp_NextKeyboardInterface) != DESCRIPTOR_SEARCH_COMP_Found)
97 {
98 /* Descriptor not found, error out */
99 return NoCompatibleInterfaceFound;
100 }
101
102 /* Get the HID descriptor from the configuration descriptor */
103 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
104 DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
105 {
106 /* Descriptor not found, error out */
107 return NoCompatibleInterfaceFound;
108 }
109
110 /* Save the HID report size for later use */
111 HIDReportSize = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_HID_t)->HIDReportLength;
112
113 /* Skip the remainder of the loop as we have not found an endpoint yet */
114 continue;
115 }
116
117 /* Retrieve the endpoint address from the endpoint descriptor */
118 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
119
120 /* If the endpoint is a IN type endpoint */
121 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
122 DataINEndpoint = EndpointData;
123 }
124
125 /* Configure the HID data IN pipe */
126 Pipe_ConfigurePipe(KEYBOARD_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
127 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
128 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
129
130 /* Valid data found, return success */
131 return SuccessfulConfigRead;
132 }
133
134 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
135 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
136 * descriptor processing if an incompatible descriptor configuration is found.
137 *
138 * This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values.
139 *
140 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
141 */
142 uint8_t DComp_NextKeyboardInterface(void* CurrentDescriptor)
143 {
144 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
145 {
146 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
147 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == KEYBOARD_CLASS) &&
148 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == KEYBOARD_PROTOCOL))
149 {
150 return DESCRIPTOR_SEARCH_Found;
151 }
152 }
153
154 return DESCRIPTOR_SEARCH_NotFound;
155 }
156
157 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
158 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
159 * descriptor processing if an incompatible descriptor configuration is found.
160 *
161 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
162 * aborting the search if another interface descriptor is found before the required endpoint.
163 *
164 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
165 */
166 uint8_t DComp_NextKeyboardInterfaceDataEndpoint(void* CurrentDescriptor)
167 {
168 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
169 {
170 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
171 return DESCRIPTOR_SEARCH_Found;
172 }
173 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
174 {
175 return DESCRIPTOR_SEARCH_Fail;
176 }
177
178 return DESCRIPTOR_SEARCH_NotFound;
179 }
180
181 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
182 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
183 * descriptor processing if an incompatible descriptor configuration is found.
184 *
185 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
186 *
187 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
188 */
189 uint8_t DComp_NextHID(void* CurrentDescriptor)
190 {
191 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
192 return DESCRIPTOR_SEARCH_Found;
193 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
194 return DESCRIPTOR_SEARCH_Fail;
195 else
196 return DESCRIPTOR_SEARCH_NotFound;
197 }