Reverted Endpoint/Pipe non-sequential configuration hack, placed restriction on the...
[pub/USBasp.git] / Demos / Host / LowLevel / StillImageHost / 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 SI interface descriptor containing bulk IN and OUT data endpoints.
45 *
46 * \return An error code from the \ref StillImageHost_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 USB_Descriptor_Endpoint_t* EventsEndpoint = NULL;
57
58 /* Retrieve the entire configuration descriptor into the allocated buffer */
59 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
60 {
61 case HOST_GETCONFIG_Successful:
62 break;
63 case HOST_GETCONFIG_InvalidData:
64 return InvalidConfigDataReturned;
65 case HOST_GETCONFIG_BuffOverflow:
66 return DescriptorTooLarge;
67 default:
68 return ControlError;
69 }
70
71 /* Get the first Still Image interface from the configuration descriptor */
72 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
73 DComp_NextStillImageInterface) != DESCRIPTOR_SEARCH_COMP_Found)
74 {
75 /* Descriptor not found, error out */
76 return NoCompatibleInterfaceFound;
77 }
78
79 while (!(DataINEndpoint) || !(DataOUTEndpoint))
80 {
81 /* Get the next Still Image interface's data endpoint descriptor */
82 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
83 DComp_NextStillImageInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
84 {
85 /* Clear any found endpoints */
86 DataINEndpoint = NULL;
87 DataOUTEndpoint = NULL;
88 EventsEndpoint = NULL;
89
90 /* Get the next Still Image interface from the configuration descriptor */
91 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
92 DComp_NextStillImageInterface) != DESCRIPTOR_SEARCH_COMP_Found)
93 {
94 /* Descriptor not found, error out */
95 return NoCompatibleInterfaceFound;
96 }
97 }
98
99 /* Retrieve the endpoint address from the endpoint descriptor */
100 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
101
102 /* If the endpoint is a IN type endpoint */
103 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
104 {
105 /* Check if the found endpoint is a interrupt or bulk type descriptor */
106 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
107 EventsEndpoint = EndpointData;
108 else
109 DataINEndpoint = EndpointData;
110 }
111 else
112 {
113 DataOUTEndpoint = EndpointData;
114 }
115 }
116
117 /* Configure the Still Image data IN pipe */
118 Pipe_ConfigurePipe(SIMAGE_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
119 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
120
121 /* Configure the Still Image data OUT pipe */
122 Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
123 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, PIPE_BANK_SINGLE);
124
125 /* Configure the Still Image events pipe */
126 Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
127 EventsEndpoint->EndpointAddress, EventsEndpoint->EndpointSize, PIPE_BANK_SINGLE);
128 Pipe_SetInterruptPeriod(EventsEndpoint->PollingIntervalMS);
129
130 /* Valid data found, return success */
131 return SuccessfulConfigRead;
132 }
133
134 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
135 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
136 * descriptor processing if an incompatible descriptor configuration is found.
137 *
138 * This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values.
139 *
140 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
141 */
142 uint8_t DComp_NextStillImageInterface(void* CurrentDescriptor)
143 {
144 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
145 {
146 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
147 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == SIMAGE_CLASS) &&
148 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == SIMAGE_SUBCLASS) &&
149 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == SIMAGE_PROTOCOL))
150 {
151 return DESCRIPTOR_SEARCH_Found;
152 }
153 }
154
155 return DESCRIPTOR_SEARCH_NotFound;
156 }
157
158 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
159 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
160 * descriptor processing if an incompatible descriptor configuration is found.
161 *
162 * This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the
163 * search if another interface descriptor is found before the next endpoint.
164 *
165 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
166 */
167 uint8_t DComp_NextStillImageInterfaceDataEndpoint(void* CurrentDescriptor)
168 {
169 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
170 {
171 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
172 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
173
174 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
175 return DESCRIPTOR_SEARCH_Found;
176 }
177 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
178 {
179 return DESCRIPTOR_SEARCH_Fail;
180 }
181
182 return DESCRIPTOR_SEARCH_NotFound;
183 }