Add descriptor class, subclass and protocol constants to the class drivers, modify...
[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_Interface_t* HIDInterface = NULL;
55 USB_HID_Descriptor_HID_t* HIDDescriptor = NULL;
56 USB_Descriptor_Endpoint_t* DataINEndpoint = 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))
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_NextKeyboardInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
77 {
78 /* Get the next HID interface from the configuration descriptor */
79 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
80 DComp_NextKeyboardInterface) != DESCRIPTOR_SEARCH_COMP_Found)
81 {
82 /* Descriptor not found, error out */
83 return NoCompatibleInterfaceFound;
84 }
85
86 /* Save the interface in case we need to refer back to it later */
87 HIDInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
88
89 /* Get the HID descriptor from the configuration descriptor */
90 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
91 DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
92 {
93 /* Descriptor not found, error out */
94 return NoCompatibleInterfaceFound;
95 }
96
97 /* Save the HID descriptor for later use */
98 HIDDescriptor = DESCRIPTOR_PCAST(CurrConfigLocation, USB_HID_Descriptor_HID_t);
99
100 /* Skip the remainder of the loop as we have not found an endpoint yet */
101 continue;
102 }
103
104 /* Retrieve the endpoint address from the endpoint descriptor */
105 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
106
107 /* If the endpoint is a IN type endpoint */
108 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
109 DataINEndpoint = EndpointData;
110 }
111
112 /* Configure the HID data IN pipe */
113 Pipe_ConfigurePipe(KEYBOARD_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
114 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
115 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
116
117 /* Get the HID report size from the HID report descriptor */
118 HIDReportSize = HIDDescriptor->HIDReportLength;
119
120 /* Valid data found, return success */
121 return SuccessfulConfigRead;
122 }
123
124 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
125 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
126 * descriptor processing if an incompatible descriptor configuration is found.
127 *
128 * This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values.
129 *
130 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
131 */
132 uint8_t DComp_NextKeyboardInterface(void* CurrentDescriptor)
133 {
134 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
135 {
136 /* Check the HID descriptor class, break out if correct class interface found */
137 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == HID_CSCP_HIDClass)
138 {
139 return DESCRIPTOR_SEARCH_Found;
140 }
141 }
142
143 return DESCRIPTOR_SEARCH_NotFound;
144 }
145
146 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
147 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
148 * descriptor processing if an incompatible descriptor configuration is found.
149 *
150 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
151 * aborting the search if another interface descriptor is found before the required endpoint.
152 *
153 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
154 */
155 uint8_t DComp_NextKeyboardInterfaceDataEndpoint(void* CurrentDescriptor)
156 {
157 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
158 {
159 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
160 return DESCRIPTOR_SEARCH_Found;
161 }
162 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
163 {
164 return DESCRIPTOR_SEARCH_Fail;
165 }
166
167 return DESCRIPTOR_SEARCH_NotFound;
168 }
169
170 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
171 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
172 * descriptor processing if an incompatible descriptor configuration is found.
173 *
174 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
175 *
176 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
177 */
178 uint8_t DComp_NextHID(void* CurrentDescriptor)
179 {
180 if (DESCRIPTOR_TYPE(CurrentDescriptor) == HID_DTYPE_HID)
181 return DESCRIPTOR_SEARCH_Found;
182 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
183 return DESCRIPTOR_SEARCH_Fail;
184 else
185 return DESCRIPTOR_SEARCH_NotFound;
186 }
187