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 and HID descriptor.
46 * \return An error code from the KeyboardHostWithParser_GetConfigDescriptorDataCodes_t enum.
48 uint8_t ProcessConfigurationDescriptor(void)
50 uint8_t* ConfigDescriptorData
;
51 uint16_t ConfigDescriptorSize
;
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 keyboard interface from the configuration descriptor */
73 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextKeyboardInterface
)))
75 /* Descriptor not found, error out */
76 return NoHIDInterfaceFound
;
79 /* Get the keyboard interface's HID descriptor */
80 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextHID
)))
82 /* Descriptor not found, error out */
83 return NoHIDDescriptorFound
;
86 /* Save the HID report size for later use */
87 HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_HID_t
).HIDReportLength
;
89 /* Get the keyboard interface's data endpoint descriptor */
90 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
91 NextInterfaceKeyboardDataEndpoint
)))
93 /* Descriptor not found, error out */
94 return NoEndpointFound
;
97 /* Retrieve the endpoint address from the endpoint descriptor */
98 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
100 /* Configure the keyboard data pipe */
101 Pipe_ConfigurePipe(KEYBOARD_DATAPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
102 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
104 Pipe_SetInfiniteINRequests();
106 /* Valid data found, return success */
107 return SuccessfulConfigRead
;
110 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
111 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
112 * descriptor processing if an incompatible descriptor configuration is found.
114 * This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values.
116 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
118 DESCRIPTOR_COMPARATOR(NextKeyboardInterface
)
120 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
122 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
123 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== KEYBOARD_CLASS
) &&
124 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== KEYBOARD_PROTOCOL
))
126 return Descriptor_Search_Found
;
130 return Descriptor_Search_NotFound
;
133 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
134 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
135 * descriptor processing if an incompatible descriptor configuration is found.
137 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
138 * aborting the search if another interface descriptor is found before the required endpoint.
140 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
142 DESCRIPTOR_COMPARATOR(NextInterfaceKeyboardDataEndpoint
)
144 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
146 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
).EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
147 return Descriptor_Search_Found
;
149 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
151 return Descriptor_Search_Fail
;
154 return Descriptor_Search_NotFound
;
157 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
158 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
159 * descriptor processing if an incompatible descriptor configuration is found.
161 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
163 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
165 DESCRIPTOR_COMPARATOR(NextHID
)
167 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
)
168 return Descriptor_Search_Found
;
170 return Descriptor_Search_NotFound
;