Removed specialized Endpoint_ClearControl* and Pipe_ClearControl* macros in favour...
[pub/USBasp.git] / Demos / Host / StillImageHost / 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 SI interface descriptor containing bulk IN and OUT data endpoints.
45 *
46 * \return An error code from the StillImageHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t* ConfigDescriptorData;
51 uint16_t ConfigDescriptorSize;
52 uint8_t FoundEndpoints = 0;
53
54 /* Get Configuration Descriptor size from the device */
55 if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
56 return ControlError;
57
58 /* Ensure that the Configuration Descriptor isn't too large */
59 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
60 return DescriptorTooLarge;
61
62 /* Allocate enough memory for the entire config descriptor */
63 ConfigDescriptorData = alloca(ConfigDescriptorSize);
64
65 /* Retrieve the entire configuration descriptor into the allocated buffer */
66 USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);
67
68 /* Validate returned data - ensure first entry is a configuration header descriptor */
69 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
70 return InvalidConfigDataReturned;
71
72 /* Get the Still Image interface from the configuration descriptor */
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, NextStillImageInterface))
74 {
75 /* Descriptor not found, error out */
76 return NoInterfaceFound;
77 }
78
79 /* Get the IN and OUT data and event endpoints for the Still Image interface */
80 while (FoundEndpoints != ((1 << SIMAGE_EVENTS_PIPE) | (1 << SIMAGE_DATA_IN_PIPE) | (1 << SIMAGE_DATA_OUT_PIPE)))
81 {
82 /* Fetch the next endpoint from the current Still Image interface */
83 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
84 NextSImageInterfaceDataEndpoint))
85 {
86 /* Descriptor not found, error out */
87 return NoEndpointFound;
88 }
89
90 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
91
92 /* Check if the found endpoint is a interrupt or bulk type descriptor */
93 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
94 {
95 /* If the endpoint is a IN type interrupt endpoint */
96 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
97 {
98 /* Configure the events pipe */
99 Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
100 EndpointData->EndpointAddress, EndpointData->EndpointSize,
101 PIPE_BANK_DOUBLE);
102
103 Pipe_SetInfiniteINRequests();
104 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
105
106 /* Set the flag indicating that the events pipe has been found */
107 FoundEndpoints |= (1 << SIMAGE_EVENTS_PIPE);
108 }
109 }
110 else
111 {
112 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
113 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
114 {
115 /* Configure the data IN pipe */
116 Pipe_ConfigurePipe(SIMAGE_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
117 EndpointData->EndpointAddress, EndpointData->EndpointSize,
118 PIPE_BANK_DOUBLE);
119
120 Pipe_SetInfiniteINRequests();
121
122 /* Set the flag indicating that the data IN pipe has been found */
123 FoundEndpoints |= (1 << SIMAGE_DATA_IN_PIPE);
124 }
125 else
126 {
127 /* Configure the data OUT pipe */
128 Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
129 EndpointData->EndpointAddress, EndpointData->EndpointSize,
130 PIPE_BANK_DOUBLE);
131
132 /* Set the flag indicating that the data OUT pipe has been found */
133 FoundEndpoints |= (1 << SIMAGE_DATA_OUT_PIPE);
134 }
135 }
136 }
137
138 /* Valid data found, return success */
139 return SuccessfulConfigRead;
140 }
141
142 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
143 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
144 * descriptor processing if an incompatible descriptor configuration is found.
145 *
146 * This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values.
147 *
148 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
149 */
150 DESCRIPTOR_COMPARATOR(NextStillImageInterface)
151 {
152 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
153 {
154 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
155 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == SIMAGE_CLASS) &&
156 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == SIMAGE_SUBCLASS) &&
157 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == SIMAGE_PROTOCOL))
158 {
159 return DESCRIPTOR_SEARCH_Found;
160 }
161 }
162
163 return DESCRIPTOR_SEARCH_NotFound;
164 }
165
166 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
167 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
168 * descriptor processing if an incompatible descriptor configuration is found.
169 *
170 * This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the
171 * search if another interface descriptor is found before the next endpoint.
172 *
173 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
174 */
175 DESCRIPTOR_COMPARATOR(NextSImageInterfaceDataEndpoint)
176 {
177 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
178 {
179 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
180 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
181
182 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
183 return DESCRIPTOR_SEARCH_Found;
184 }
185 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
186 {
187 return DESCRIPTOR_SEARCH_Fail;
188 }
189
190 return DESCRIPTOR_SEARCH_NotFound;
191 }