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, 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.
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 MSD interface descriptor containing bulk IN and OUT data endpoints.
46 * \return An error code from the MassStorageHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t* ConfigDescriptorData
;
51 uint16_t ConfigDescriptorSize
;
52 uint8_t FoundEndpoints
= 0;
54 /* Get Configuration Descriptor size from the device */
55 if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
58 /* Ensure that the Configuration Descriptor isn't too large */
59 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
60 return DescriptorTooLarge
;
62 /* Allocate enough memory for the entire config descriptor */
63 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
65 /* Retrieve the entire configuration descriptor into the allocated buffer */
66 USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, ConfigDescriptorData
);
68 /* Validate returned data - ensure first entry is a configuration header descriptor */
69 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
70 return InvalidConfigDataReturned
;
72 /* Get the mass storage interface from the configuration descriptor */
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
74 DComp_NextMassStorageInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
76 /* Descriptor not found, error out */
77 return NoInterfaceFound
;
80 /* Get the IN and OUT data endpoints for the mass storage interface */
81 while (FoundEndpoints
!= ((1 << MASS_STORE_DATA_IN_PIPE
) | (1 << MASS_STORE_DATA_OUT_PIPE
)))
83 /* Fetch the next bulk endpoint from the current mass storage interface */
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
85 DComp_NextInterfaceBulkDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
87 /* Descriptor not found, error out */
88 return NoEndpointFound
;
91 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
93 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
94 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
96 /* Configure the data IN pipe */
97 Pipe_ConfigurePipe(MASS_STORE_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
98 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
101 Pipe_SetInfiniteINRequests();
103 /* Set the flag indicating that the data IN pipe has been found */
104 FoundEndpoints
|= (1 << MASS_STORE_DATA_IN_PIPE
);
108 /* Configure the data OUT pipe */
109 Pipe_ConfigurePipe(MASS_STORE_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
110 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
113 /* Set the flag indicating that the data OUT pipe has been found */
114 FoundEndpoints
|= (1 << MASS_STORE_DATA_OUT_PIPE
);
118 /* Valid data found, return success */
119 return SuccessfulConfigRead
;
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.
126 * This comparator searches for the next Interface descriptor of the correct Mass Storage Class, Subclass and Protocol values.
128 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
130 uint8_t DComp_NextMassStorageInterface(void* CurrentDescriptor
)
132 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
134 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
135 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== MASS_STORE_CLASS
) &&
136 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== MASS_STORE_SUBCLASS
) &&
137 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== MASS_STORE_PROTOCOL
))
139 return DESCRIPTOR_SEARCH_Found
;
143 return DESCRIPTOR_SEARCH_NotFound
;
146 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
147 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
148 * descriptor processing if an incompatible descriptor configuration is found.
150 * This comparator searches for the next Bulk Endpoint descriptor of the correct MSD interface, aborting the search if
151 * another interface descriptor is found before the next endpoint.
153 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
155 uint8_t DComp_NextInterfaceBulkDataEndpoint(void* CurrentDescriptor
)
157 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
159 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
160 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
162 /* Check the endpoint type, break out if correct BULK type endpoint found */
163 if (EndpointType
== EP_TYPE_BULK
)
164 return DESCRIPTOR_SEARCH_Found
;
166 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
168 return DESCRIPTOR_SEARCH_Fail
;
171 return DESCRIPTOR_SEARCH_NotFound
;