Reverted Endpoint/Pipe non-sequential configuration hack, placed restriction on the...
[pub/USBasp.git] / Demos / Host / LowLevel / MouseHostWithParser / 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 MouseHostWithParser_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_NextMouseInterface) != 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_CAST(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_NextMouseInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
93 {
94 /* Get the next HID interface from the configuration descriptor */
95 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
96 DComp_NextMouseInterface) != 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_CAST(CurrConfigLocation, USB_Descriptor_HID_t).HIDReportLength;
112 }
113
114 /* Retrieve the endpoint address from the endpoint descriptor */
115 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
116
117 /* If the endpoint is a IN type endpoint */
118 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
119 DataINEndpoint = EndpointData;
120 }
121
122 /* Configure the HID data IN pipe */
123 Pipe_ConfigurePipe(MOUSE_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
124 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
125 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
126
127 /* Valid data found, return success */
128 return SuccessfulConfigRead;
129 }
130
131 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
132 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
133 * descriptor processing if an incompatible descriptor configuration is found.
134 *
135 * This comparator searches for the next Interface descriptor of the correct Mouse HID Class and Protocol values.
136 *
137 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
138 */
139 uint8_t DComp_NextMouseInterface(void* CurrentDescriptor)
140 {
141 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
142 {
143 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
144 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == MOUSE_CLASS) &&
145 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == MOUSE_PROTOCOL))
146 {
147 return DESCRIPTOR_SEARCH_Found;
148 }
149 }
150
151 return DESCRIPTOR_SEARCH_NotFound;
152 }
153
154 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
155 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
156 * descriptor processing if an incompatible descriptor configuration is found.
157 *
158 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
159 * aborting the search if another interface descriptor is found before the required endpoint.
160 *
161 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
162 */
163 uint8_t DComp_NextMouseInterfaceDataEndpoint(void* CurrentDescriptor)
164 {
165 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
166 {
167 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
168 return DESCRIPTOR_SEARCH_Found;
169 }
170 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
171 {
172 return DESCRIPTOR_SEARCH_Fail;
173 }
174
175 return DESCRIPTOR_SEARCH_NotFound;
176 }
177
178 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
179 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
180 * descriptor processing if an incompatible descriptor configuration is found.
181 *
182 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
183 *
184 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
185 */
186 uint8_t DComp_NextHID(void* CurrentDescriptor)
187 {
188 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
189 return DESCRIPTOR_SEARCH_Found;
190 else
191 return DESCRIPTOR_SEARCH_NotFound;
192 }