2de7b722b2a8b4f869e2dd8b28ba53e9b023a17d
[pub/USBasp.git] / Demos / Host / LowLevel / GenericHIDHost / 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.
45 *
46 * \return An error code from the \ref 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_Endpoint_t* DataINEndpoint = NULL;
55 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
56
57 /* Retrieve the entire configuration descriptor into the allocated buffer */
58 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
59 {
60 case HOST_GETCONFIG_Successful:
61 break;
62 case HOST_GETCONFIG_InvalidData:
63 return InvalidConfigDataReturned;
64 case HOST_GETCONFIG_BuffOverflow:
65 return DescriptorTooLarge;
66 default:
67 return ControlError;
68 }
69
70 /* Get the first HID interface from the configuration descriptor */
71 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
72 DComp_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
73 {
74 /* Descriptor not found, error out */
75 return NoCompatibleInterfaceFound;
76 }
77
78 while (!(DataINEndpoint) || !(DataOUTEndpoint))
79 {
80 /* Get the next HID interface's data endpoint descriptor */
81 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
82 DComp_NextHIDInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
83 {
84 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
85 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
86 if (DataINEndpoint)
87 break;
88
89 /* Clear any found endpoints */
90 DataOUTEndpoint = NULL;
91
92 /* Get the next HID interface from the configuration descriptor */
93 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
94 DComp_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
95 {
96 /* Descriptor not found, error out */
97 return NoCompatibleInterfaceFound;
98 }
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 else
111 DataOUTEndpoint = EndpointData;
112 }
113
114 /* Configure the HID data IN pipe */
115 Pipe_ConfigurePipe(HID_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
116 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
117 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
118
119 /* Check if the HID interface contained an optional OUT data endpoint */
120 if (DataOUTEndpoint)
121 {
122 /* Configure the HID data OUT pipe */
123 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
124 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, PIPE_BANK_SINGLE);
125 }
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 HID Class value.
136 *
137 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
138 */
139 uint8_t DComp_NextHIDInterface(void* CurrentDescriptor)
140 {
141 /* Determine if the current descriptor is an interface descriptor */
142 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
143 {
144 /* Check the HID descriptor class, break out if correct class/protocol interface found */
145 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == HID_CLASS)
146 {
147 /* Indicate that the descriptor being searched for has been found */
148 return DESCRIPTOR_SEARCH_Found;
149 }
150 }
151
152 /* Current descriptor does not match what this comparator is looking for */
153 return DESCRIPTOR_SEARCH_NotFound;
154 }
155
156 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
157 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
158 * descriptor processing if an incompatible descriptor configuration is found.
159 *
160 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
161 * aborting the search if another interface descriptor is found before the required endpoint.
162 *
163 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
164 */
165 uint8_t DComp_NextHIDInterfaceDataEndpoint(void* CurrentDescriptor)
166 {
167 /* Determine the type of the current descriptor */
168 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
169 {
170 /* Indicate that the descriptor being searched for has been found */
171 return DESCRIPTOR_SEARCH_Found;
172 }
173 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
174 {
175 /* Indicate that the search has failed prematurely and should be aborted */
176 return DESCRIPTOR_SEARCH_Fail;
177 }
178
179 /* Current descriptor does not match what this comparator is looking for */
180 return DESCRIPTOR_SEARCH_NotFound;
181 }