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
;
53 /* Get Configuration Descriptor size from the device */
54 if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
57 /* Ensure that the Configuration Descriptor isn't too large */
58 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
59 return DescriptorTooLarge
;
61 /* Allocate enough memory for the entire config descriptor */
62 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
64 /* Retrieve the entire configuration descriptor into the allocated buffer */
65 USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, ConfigDescriptorData
);
67 /* Validate returned data - ensure first entry is a configuration header descriptor */
68 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
69 return InvalidConfigDataReturned
;
71 /* Get the keyboard interface from the configuration descriptor */
72 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
73 DComp_NextKeyboardInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
75 /* Descriptor not found, error out */
76 return NoHIDInterfaceFound
;
79 /* Get the keyboard interface's HID descriptor */
80 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
81 DComp_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
83 /* Descriptor not found, error out */
84 return NoHIDDescriptorFound
;
87 /* Save the HID report size for later use */
88 HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_HID_t
).HIDReportLength
;
90 /* Get the keyboard interface's data endpoint descriptor */
91 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
92 DComp_NextInterfaceKeyboardDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
94 /* Descriptor not found, error out */
95 return NoEndpointFound
;
98 /* Retrieve the endpoint address from the endpoint descriptor */
99 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
101 /* Configure the keyboard data pipe */
102 Pipe_ConfigurePipe(KEYBOARD_DATAPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
103 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
105 Pipe_SetInfiniteINRequests();
107 /* Valid data found, return success */
108 return SuccessfulConfigRead
;
111 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
112 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
113 * descriptor processing if an incompatible descriptor configuration is found.
115 * This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values.
117 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
119 uint8_t DComp_NextKeyboardInterface(void* CurrentDescriptor
)
121 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
123 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
124 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== KEYBOARD_CLASS
) &&
125 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== KEYBOARD_PROTOCOL
))
127 return DESCRIPTOR_SEARCH_Found
;
131 return DESCRIPTOR_SEARCH_NotFound
;
134 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
135 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
136 * descriptor processing if an incompatible descriptor configuration is found.
138 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
139 * aborting the search if another interface descriptor is found before the required endpoint.
141 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
143 uint8_t DComp_NextInterfaceKeyboardDataEndpoint(void* CurrentDescriptor
)
145 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
147 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
).EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
148 return DESCRIPTOR_SEARCH_Found
;
150 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
152 return DESCRIPTOR_SEARCH_Fail
;
155 return DESCRIPTOR_SEARCH_NotFound
;
158 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
159 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
160 * descriptor processing if an incompatible descriptor configuration is found.
162 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
164 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
166 uint8_t DComp_NextHID(void* CurrentDescriptor
)
168 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
)
169 return DESCRIPTOR_SEARCH_Found
;
171 return DESCRIPTOR_SEARCH_NotFound
;