3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
38 #include "ConfigDescriptor.h"
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.
44 * This routine searches for a SI interface descriptor containing bulk IN and OUT data endpoints.
46 * \return An error code from the \ref StillImageHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t ConfigDescriptorData
[512];
51 void* CurrConfigLocation
= ConfigDescriptorData
;
52 uint16_t CurrConfigBytesRem
;
53 uint8_t FoundEndpoints
= 0;
55 /* Retrieve the entire configuration descriptor into the allocated buffer */
56 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
)))
58 case HOST_GETCONFIG_Successful
:
60 case HOST_GETCONFIG_InvalidData
:
61 return InvalidConfigDataReturned
;
62 case HOST_GETCONFIG_BuffOverflow
:
63 return DescriptorTooLarge
;
68 /* Get the Still Image interface from the configuration descriptor */
69 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
70 DComp_NextStillImageInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
72 /* Descriptor not found, error out */
73 return NoInterfaceFound
;
76 /* Get the IN and OUT data and event endpoints for the Still Image interface */
77 while (FoundEndpoints
!= ((1 << SIMAGE_EVENTS_PIPE
) | (1 << SIMAGE_DATA_IN_PIPE
) | (1 << SIMAGE_DATA_OUT_PIPE
)))
79 /* Fetch the next endpoint from the current Still Image interface */
80 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
81 DComp_NextStillImageInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
83 /* Descriptor not found, error out */
84 return NoEndpointFound
;
87 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
89 /* Check if the found endpoint is a interrupt or bulk type descriptor */
90 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
92 /* If the endpoint is a IN type interrupt endpoint */
93 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
95 /* Configure the events pipe */
96 Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
97 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
100 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
);
102 /* Set the flag indicating that the events pipe has been found */
103 FoundEndpoints
|= (1 << SIMAGE_EVENTS_PIPE
);
108 /* Check if the endpoint is a bulk IN or bulk OUT endpoint */
109 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
111 /* Configure the data IN pipe */
112 Pipe_ConfigurePipe(SIMAGE_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
113 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
116 /* Set the flag indicating that the data IN pipe has been found */
117 FoundEndpoints
|= (1 << SIMAGE_DATA_IN_PIPE
);
121 /* Configure the data OUT pipe */
122 Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
123 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
126 /* Set the flag indicating that the data OUT pipe has been found */
127 FoundEndpoints
|= (1 << SIMAGE_DATA_OUT_PIPE
);
132 /* Valid data found, return success */
133 return SuccessfulConfigRead
;
136 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
137 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
138 * descriptor processing if an incompatible descriptor configuration is found.
140 * This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values.
142 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
144 uint8_t DComp_NextStillImageInterface(void* CurrentDescriptor
)
146 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
148 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
149 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== SIMAGE_CLASS
) &&
150 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== SIMAGE_SUBCLASS
) &&
151 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== SIMAGE_PROTOCOL
))
153 return DESCRIPTOR_SEARCH_Found
;
157 return DESCRIPTOR_SEARCH_NotFound
;
160 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
161 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
162 * descriptor processing if an incompatible descriptor configuration is found.
164 * This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the
165 * search if another interface descriptor is found before the next endpoint.
167 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
169 uint8_t DComp_NextStillImageInterfaceDataEndpoint(void* CurrentDescriptor
)
171 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
173 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
174 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
176 if ((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
))
177 return DESCRIPTOR_SEARCH_Found
;
179 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
181 return DESCRIPTOR_SEARCH_Fail
;
184 return DESCRIPTOR_SEARCH_NotFound
;