a2d4d48dd2a47024c6ac6065ef733d77eb8ee64f
[pub/USBasp.git] / Demos / Host / LowLevel / 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(1, &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(1, &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,
74 DComp_NextStillImageInterface) != DESCRIPTOR_SEARCH_COMP_Found)
75 {
76 /* Descriptor not found, error out */
77 return NoInterfaceFound;
78 }
79
80 /* Get the IN and OUT data and event endpoints for the Still Image interface */
81 while (FoundEndpoints != ((1 << SIMAGE_EVENTS_PIPE) | (1 << SIMAGE_DATA_IN_PIPE) | (1 << SIMAGE_DATA_OUT_PIPE)))
82 {
83 /* Fetch the next endpoint from the current Still Image interface */
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
85 DComp_NextStillImageInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Descriptor not found, error out */
88 return NoEndpointFound;
89 }
90
91 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
92
93 /* Check if the found endpoint is a interrupt or bulk type descriptor */
94 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
95 {
96 /* If the endpoint is a IN type interrupt endpoint */
97 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
98 {
99 /* Configure the events pipe */
100 Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
101 EndpointData->EndpointAddress, EndpointData->EndpointSize,
102 PIPE_BANK_DOUBLE);
103
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 /* Set the flag indicating that the data IN pipe has been found */
121 FoundEndpoints |= (1 << SIMAGE_DATA_IN_PIPE);
122 }
123 else
124 {
125 /* Configure the data OUT pipe */
126 Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
127 EndpointData->EndpointAddress, EndpointData->EndpointSize,
128 PIPE_BANK_DOUBLE);
129
130 /* Set the flag indicating that the data OUT pipe has been found */
131 FoundEndpoints |= (1 << SIMAGE_DATA_OUT_PIPE);
132 }
133 }
134 }
135
136 /* Valid data found, return success */
137 return SuccessfulConfigRead;
138 }
139
140 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
141 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
142 * descriptor processing if an incompatible descriptor configuration is found.
143 *
144 * This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values.
145 *
146 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
147 */
148 uint8_t DComp_NextStillImageInterface(void* CurrentDescriptor)
149 {
150 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
151 {
152 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
153 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == SIMAGE_CLASS) &&
154 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == SIMAGE_SUBCLASS) &&
155 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == SIMAGE_PROTOCOL))
156 {
157 return DESCRIPTOR_SEARCH_Found;
158 }
159 }
160
161 return DESCRIPTOR_SEARCH_NotFound;
162 }
163
164 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
165 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
166 * descriptor processing if an incompatible descriptor configuration is found.
167 *
168 * This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the
169 * search if another interface descriptor is found before the next endpoint.
170 *
171 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
172 */
173 uint8_t DComp_NextStillImageInterfaceDataEndpoint(void* CurrentDescriptor)
174 {
175 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
176 {
177 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
178 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
179
180 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
181 return DESCRIPTOR_SEARCH_Found;
182 }
183 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
184 {
185 return DESCRIPTOR_SEARCH_Fail;
186 }
187
188 return DESCRIPTOR_SEARCH_NotFound;
189 }