Pipe_ConfigurePipe() now automatically defaults IN pipes to accepting infinite IN...
[pub/USBasp.git] / Demos / Host / LowLevel / GenericHIDHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 GenericHIDHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t* ConfigDescriptorData;
51 uint16_t ConfigDescriptorSize;
52
53 uint8_t FoundEndpoints = 0;
54
55 /* Get Configuration Descriptor size from the device */
56 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
57 return ControlError;
58
59 /* Ensure that the Configuration Descriptor isn't too large */
60 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
61 return DescriptorTooLarge;
62
63 /* Allocate enough memory for the entire config descriptor */
64 ConfigDescriptorData = alloca(ConfigDescriptorSize);
65
66 /* Retrieve the entire configuration descriptor into the allocated buffer */
67 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
68
69 /* Validate returned data - ensure first entry is a configuration header descriptor */
70 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
71 return InvalidConfigDataReturned;
72
73 /* Get the HID interface from the configuration descriptor */
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
75 DComp_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
76 {
77 /* Descriptor not found, error out */
78 return NoHIDInterfaceFound;
79 }
80
81 while (FoundEndpoints != ((1 << HID_DATA_IN_PIPE) | (1 << HID_DATA_OUT_PIPE)))
82 {
83 /* Get the next HID interface's data endpoint descriptor */
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
85 DComp_NextInterfaceHIDDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
88 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
89 if (FoundEndpoints == (1 << HID_DATA_IN_PIPE))
90 break;
91
92 /* Descriptor not found, error out */
93 return NoEndpointFound;
94 }
95
96 /* Retrieve the endpoint address from the endpoint descriptor */
97 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
98
99 /* If the endpoint is a IN type endpoint */
100 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
101 {
102 /* Configure the HID data IN pipe */
103 Pipe_ConfigurePipe(HID_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
104 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
105
106 FoundEndpoints |= (1 << HID_DATA_IN_PIPE);
107 }
108 else
109 {
110 /* Configure the HID data OUT pipe */
111 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
112 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
113
114 FoundEndpoints |= (1 << HID_DATA_OUT_PIPE);
115 }
116 }
117
118 /* Valid data found, return success */
119 return SuccessfulConfigRead;
120 }
121
122 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
123 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
124 * descriptor processing if an incompatible descriptor configuration is found.
125 *
126 * This comparator searches for the next Interface descriptor of the correct HID Class value.
127 *
128 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
129 */
130 uint8_t DComp_NextHIDInterface(void* CurrentDescriptor)
131 {
132 /* Determine if the current descriptor is an interface descriptor */
133 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
134 {
135 /* Check the HID descriptor class, break out if correct class/protocol interface found */
136 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == HID_CLASS)
137 {
138 /* Indicate that the descriptor being searched for has been found */
139 return DESCRIPTOR_SEARCH_Found;
140 }
141 }
142
143 /* Current descriptor does not match what this comparator is looking for */
144 return DESCRIPTOR_SEARCH_NotFound;
145 }
146
147 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
148 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
149 * descriptor processing if an incompatible descriptor configuration is found.
150 *
151 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
152 * aborting the search if another interface descriptor is found before the required endpoint.
153 *
154 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
155 */
156 uint8_t DComp_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor)
157 {
158 /* Determine the type of the current descriptor */
159 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
160 {
161 /* Indicate that the descriptor being searched for has been found */
162 return DESCRIPTOR_SEARCH_Found;
163 }
164 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
165 {
166 /* Indicate that the search has failed prematurely and should be aborted */
167 return DESCRIPTOR_SEARCH_Fail;
168 }
169
170 /* Current descriptor does not match what this comparator is looking for */
171 return DESCRIPTOR_SEARCH_NotFound;
172 }