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 HID interface descriptor containing at least one Interrupt type IN endpoint.
46 * \return An error code from the MouseHost_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t ConfigDescriptorData
[512];
51 uint8_t* CurrConfigLocation
= ConfigDescriptorData
;
52 uint16_t CurrConfigBytesRem
;
54 /* Retrieve the entire configuration descriptor into the allocated buffer */
55 switch (USB_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 /* Get the mouse interface from the configuration descriptor */
68 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
69 DComp_NextMouseInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
71 /* Descriptor not found, error out */
72 return NoHIDInterfaceFound
;
75 /* Get the mouse interface's data endpoint descriptor */
76 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,
77 DComp_NextMouseInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
79 /* Descriptor not found, error out */
80 return NoEndpointFound
;
83 /* Retrieve the endpoint address from the endpoint descriptor */
84 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
86 /* Configure the mouse data pipe */
87 Pipe_ConfigurePipe(MOUSE_DATAPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
88 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
90 /* Valid data found, return success */
91 return SuccessfulConfigRead
;
94 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
95 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
96 * descriptor processing if an incompatible descriptor configuration is found.
98 * This comparator searches for the next Interface descriptor of the correct Mouse HID Class and Protocol values.
100 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
102 uint8_t DComp_NextMouseInterface(void* CurrentDescriptor
)
104 /* Determine if the current descriptor is an interface descriptor */
105 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
107 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
108 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== MOUSE_CLASS
) &&
109 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== MOUSE_PROTOCOL
))
111 /* Indicate that the descriptor being searched for has been found */
112 return DESCRIPTOR_SEARCH_Found
;
116 /* Current descriptor does not match what this comparator is looking for */
117 return DESCRIPTOR_SEARCH_NotFound
;
120 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
121 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
122 * descriptor processing if an incompatible descriptor configuration is found.
124 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
125 * aborting the search if another interface descriptor is found before the required endpoint.
127 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
129 uint8_t DComp_NextMouseInterfaceDataEndpoint(void* CurrentDescriptor
)
131 /* Determine the type of the current descriptor */
132 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
134 /* Check if the current Endpoint descriptor is of type IN */
135 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
).EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
137 /* Indicate that the descriptor being searched for has been found */
138 return DESCRIPTOR_SEARCH_Found
;
141 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
143 /* Indicate that the search has failed prematurely and should be aborted */
144 return DESCRIPTOR_SEARCH_Fail
;
147 /* Current descriptor does not match what this comparator is looking for */
148 return DESCRIPTOR_SEARCH_NotFound
;