Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / Demos / Host / LowLevel / KeyboardHostWithParser / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
36 */
37
38 #include "ConfigDescriptor.h"
39
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.
43 *
44 * This routine searches for a HID interface descriptor containing at least one Interrupt type IN endpoint and HID descriptor.
45 *
46 * \return An error code from the KeyboardHostWithParser_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t* ConfigDescriptorData;
51 uint16_t ConfigDescriptorSize;
52
53 /* Get Configuration Descriptor size from the device */
54 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
55 return ControlError;
56
57 /* Ensure that the Configuration Descriptor isn't too large */
58 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
59 return DescriptorTooLarge;
60
61 /* Allocate enough memory for the entire config descriptor */
62 ConfigDescriptorData = alloca(ConfigDescriptorSize);
63
64 /* Retrieve the entire configuration descriptor into the allocated buffer */
65 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
66
67 /* Validate returned data - ensure first entry is a configuration header descriptor */
68 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
69 return InvalidConfigDataReturned;
70
71 /* Get the keyboard interface from the configuration descriptor */
72 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
73 DComp_NextKeyboardInterface) != DESCRIPTOR_SEARCH_COMP_Found)
74 {
75 /* Descriptor not found, error out */
76 return NoHIDInterfaceFound;
77 }
78
79 /* Get the keyboard interface's HID descriptor */
80 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
81 DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
82 {
83 /* Descriptor not found, error out */
84 return NoHIDDescriptorFound;
85 }
86
87 /* Save the HID report size for later use */
88 HIDReportSize = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_HID_t).HIDReportLength;
89
90 /* Get the keyboard interface's data endpoint descriptor */
91 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
92 DComp_NextInterfaceKeyboardDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
93 {
94 /* Descriptor not found, error out */
95 return NoEndpointFound;
96 }
97
98 /* Retrieve the endpoint address from the endpoint descriptor */
99 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
100
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);
104
105 /* Valid data found, return success */
106 return SuccessfulConfigRead;
107 }
108
109 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
110 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
111 * descriptor processing if an incompatible descriptor configuration is found.
112 *
113 * This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values.
114 *
115 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
116 */
117 uint8_t DComp_NextKeyboardInterface(void* CurrentDescriptor)
118 {
119 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
120 {
121 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
122 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == KEYBOARD_CLASS) &&
123 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == KEYBOARD_PROTOCOL))
124 {
125 return DESCRIPTOR_SEARCH_Found;
126 }
127 }
128
129 return DESCRIPTOR_SEARCH_NotFound;
130 }
131
132 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
133 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
134 * descriptor processing if an incompatible descriptor configuration is found.
135 *
136 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
137 * aborting the search if another interface descriptor is found before the required endpoint.
138 *
139 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
140 */
141 uint8_t DComp_NextInterfaceKeyboardDataEndpoint(void* CurrentDescriptor)
142 {
143 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
144 {
145 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
146 return DESCRIPTOR_SEARCH_Found;
147 }
148 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
149 {
150 return DESCRIPTOR_SEARCH_Fail;
151 }
152
153 return DESCRIPTOR_SEARCH_NotFound;
154 }
155
156 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
157 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
158 * descriptor processing if an incompatible descriptor configuration is found.
159 *
160 * This comparator searches for the next HID descriptor within the current HID interface descriptor.
161 *
162 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
163 */
164 uint8_t DComp_NextHID(void* CurrentDescriptor)
165 {
166 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
167 return DESCRIPTOR_SEARCH_Found;
168 else
169 return DESCRIPTOR_SEARCH_NotFound;
170 }