3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 BT interface descriptor containing bulk IN and OUT data endpoints.
46 * \return An error code from the \ref BluetoothHost_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 /* The bluetooth USB transport addendum mandates that the data (not streaming voice) endpoints
69 be in the first interface descriptor (interface 0) */
70 USB_GetNextDescriptorOfType(&CurrConfigBytesRem
, &CurrConfigLocation
, DTYPE_Interface
);
72 /* Ensure that an interface was found, and the end of the descriptor was not reached */
73 if (!(CurrConfigBytesRem
))
74 return NoBTInterfaceFound
;
76 /* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */
77 while (FoundEndpoints
!= ((1 << BLUETOOTH_DATA_IN_PIPE
) | (1 << BLUETOOTH_DATA_OUT_PIPE
) |
78 (1 << BLUETOOTH_EVENTS_PIPE
)))
80 /* Fetch the next endpoint from the current bluetooth interface */
81 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
82 DComp_NextInterfaceBluetoothDataEndpoint
))
84 /* Descriptor not found, error out */
85 return NoEndpointFound
;
88 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
90 /* Check if the endpoint is a bulk or interrupt type endpoint */
91 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
93 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
95 /* Configure the events IN pipe */
96 Pipe_ConfigurePipe(BLUETOOTH_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 notification pipe has been found */
103 FoundEndpoints
|= (1 << BLUETOOTH_EVENTS_PIPE
);
108 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
110 /* Configure the data IN pipe */
111 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
112 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
115 /* Set the flag indicating that the data IN pipe has been found */
116 FoundEndpoints
|= (1 << BLUETOOTH_DATA_IN_PIPE
);
120 /* Configure the data OUT pipe */
121 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
122 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
125 /* Set the flag indicating that the data OUT pipe has been found */
126 FoundEndpoints
|= (1 << BLUETOOTH_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 Endpoint descriptor inside the current interface descriptor, aborting the
141 * search if another interface descriptor is found before the required endpoint.
143 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
145 uint8_t DComp_NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor
)
147 /* Determine the type of the current descriptor */
148 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
150 /* Indicate that the descriptor being searched for has been found */
151 return DESCRIPTOR_SEARCH_Found
;
153 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
155 /* Indicate that the search has failed prematurely and should be aborted */
156 return DESCRIPTOR_SEARCH_Fail
;
159 /* Current descriptor does not match what this comparator is looking for */
160 return DESCRIPTOR_SEARCH_NotFound
;