Fix low level host mode demos not correctly fetching the next endpoint when an invali...
[pub/USBasp.git] / Demos / Host / LowLevel / JoystickHostWithParser / 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 JoystickHostWithParser_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_NextJoystickInterface) != DESCRIPTOR_SEARCH_COMP_Found)
72 {
73 /* Descriptor not found, error out */
74 return NoCompatibleInterfaceFound;
75 }
76
77 while (!(DataINEndpoint))
78 {
79 /* Get the next HID interface's data endpoint descriptor */
80 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
81 DComp_NextJoystickInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
82 {
83 /* Get the next HID interface from the configuration descriptor */
84 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
85 DComp_NextJoystickInterface) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Descriptor not found, error out */
88 return NoCompatibleInterfaceFound;
89 }
90
91 /* Skip the remainder of the loop as we have not found an endpoint yet */
92 continue;
93 }
94
95 /* Retrieve the endpoint address from the endpoint descriptor */
96 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
97
98 /* If the endpoint is a IN type endpoint */
99 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
100 DataINEndpoint = EndpointData;
101 }
102
103 /* Configure the HID data IN pipe */
104 Pipe_ConfigurePipe(JOYSTICK_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
105 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
106 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
107
108 /* Valid data found, return success */
109 return SuccessfulConfigRead;
110 }
111
112 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
113 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
114 * descriptor processing if an incompatible descriptor configuration is found.
115 *
116 * This comparator searches for the next Interface descriptor of the correct Joystick HID Class and Protocol values.
117 *
118 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
119 */
120 uint8_t DComp_NextJoystickInterface(void* CurrentDescriptor)
121 {
122 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
123 {
124 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
125 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == JOYSTICK_CLASS) &&
126 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == JOYSTICK_PROTOCOL))
127 {
128 return DESCRIPTOR_SEARCH_Found;
129 }
130 }
131
132 return DESCRIPTOR_SEARCH_NotFound;
133 }
134
135 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
136 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
137 * descriptor processing if an incompatible descriptor configuration is found.
138 *
139 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
140 * aborting the search if another interface descriptor is found before the required endpoint.
141 *
142 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
143 */
144 uint8_t DComp_NextJoystickInterfaceDataEndpoint(void* CurrentDescriptor)
145 {
146 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
147 {
148 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
149 return DESCRIPTOR_SEARCH_Found;
150 }
151 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
152 {
153 return DESCRIPTOR_SEARCH_Fail;
154 }
155
156 return DESCRIPTOR_SEARCH_NotFound;
157 }
158
159 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
160 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
161 * descriptor processing if an incompatible descriptor configuration is found.
162 *
163 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
164 *
165 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
166 */
167 uint8_t DComp_NextHID(void* CurrentDescriptor)
168 {
169 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
170 return DESCRIPTOR_SEARCH_Found;
171 else
172 return DESCRIPTOR_SEARCH_NotFound;
173 }