3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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 uint8_t StreamingInterfaceIndex
= 0;
41 uint8_t StreamingInterfaceAltSetting
= 0;
42 uint8_t StreamingEndpointAddress
= 0;
44 uint8_t ProcessConfigurationDescriptor(void)
46 uint8_t ConfigDescriptorData
[512];
47 void* CurrConfigLocation
= ConfigDescriptorData
;
48 uint16_t CurrConfigBytesRem
;
50 USB_Descriptor_Interface_t
* AudioControlInterface
= NULL
;
51 USB_Descriptor_Interface_t
* AudioStreamingInterface
= NULL
;
52 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
54 /* Retrieve the entire configuration descriptor into the allocated buffer */
55 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
)))
57 case HOST_GETCONFIG_Successful
:
59 case HOST_GETCONFIG_InvalidData
:
60 return InvalidConfigDataReturned
;
61 case HOST_GETCONFIG_BuffOverflow
:
62 return DescriptorTooLarge
;
67 while (!(DataINEndpoint
))
69 if (!(AudioControlInterface
) ||
70 USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
71 DComp_NextAudioInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
73 if (!(AudioControlInterface
))
75 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
76 DComp_NextAudioControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
78 /* Descriptor not found, error out */
79 return NoCompatibleInterfaceFound
;
82 /* Save the interface in case we need to refer back to it later */
83 AudioControlInterface
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
);
86 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
87 DComp_NextAudioStreamInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
89 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
90 DComp_NextAudioControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
92 /* Descriptor not found, error out */
93 return NoCompatibleInterfaceFound
;
96 /* Save the interface in case we need to refer back to it later */
97 AudioControlInterface
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
);
100 /* Save the interface in case we need to refer back to it later */
101 AudioStreamingInterface
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
);
103 /* Skip the remainder of the loop as we have not found an endpoint yet */
107 /* Retrieve the endpoint address from the endpoint descriptor */
108 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
110 /* If the endpoint is a IN type endpoint */
111 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
112 DataINEndpoint
= EndpointData
;
115 StreamingInterfaceIndex
= AudioStreamingInterface
->InterfaceNumber
;
116 StreamingInterfaceAltSetting
= AudioStreamingInterface
->AlternateSetting
;
117 StreamingEndpointAddress
= DataINEndpoint
->EndpointAddress
;
119 /* Configure the HID data IN pipe */
120 Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE
, EP_TYPE_ISOCHRONOUS
, PIPE_TOKEN_IN
,
121 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_DOUBLE
);
123 /* Valid data found, return success */
124 return SuccessfulConfigRead
;
127 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor
)
129 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
131 if (Header
->Type
== DTYPE_Interface
)
133 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
135 if ((Interface
->Class
== AUDIO_CSCP_AudioClass
) &&
136 (Interface
->SubClass
== AUDIO_CSCP_ControlSubclass
) &&
137 (Interface
->Protocol
== AUDIO_CSCP_ControlProtocol
))
139 return DESCRIPTOR_SEARCH_Found
;
143 return DESCRIPTOR_SEARCH_NotFound
;
146 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor
)
148 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
150 if (Header
->Type
== DTYPE_Interface
)
152 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
154 if ((Interface
->Class
== AUDIO_CSCP_AudioClass
) &&
155 (Interface
->SubClass
== AUDIO_CSCP_AudioStreamingSubclass
) &&
156 (Interface
->Protocol
== AUDIO_CSCP_StreamingProtocol
))
158 return DESCRIPTOR_SEARCH_Found
;
162 return DESCRIPTOR_SEARCH_NotFound
;
165 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor
)
167 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
169 if (Header
->Type
== DTYPE_Endpoint
)
171 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
173 if ((Endpoint
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_ISOCHRONOUS
)
174 return DESCRIPTOR_SEARCH_Found
;
176 else if (Header
->Type
== DTYPE_Interface
)
178 return DESCRIPTOR_SEARCH_Fail
;
181 return DESCRIPTOR_SEARCH_NotFound
;