Partial commit: change references to Drivers/AT90USBXXX to Drivers/Peripheral.
[pub/USBasp.git] / Demos / Host / GenericHIDHost / 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.
45 *
46 * \return An error code from the GenericHIDHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t* ConfigDescriptorData;
51 uint16_t ConfigDescriptorSize;
52
53 uint8_t FoundEndpoints = 0;
54
55 /* Get Configuration Descriptor size from the device */
56 if (USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
57 return ControlError;
58
59 /* Ensure that the Configuration Descriptor isn't too large */
60 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
61 return DescriptorTooLarge;
62
63 /* Allocate enough memory for the entire config descriptor */
64 ConfigDescriptorData = alloca(ConfigDescriptorSize);
65
66 /* Retrieve the entire configuration descriptor into the allocated buffer */
67 USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);
68
69 /* Validate returned data - ensure first entry is a configuration header descriptor */
70 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
71 return InvalidConfigDataReturned;
72
73 /* Get the HID interface from the configuration descriptor */
74 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, NextHIDInterface))
75 {
76 /* Descriptor not found, error out */
77 return NoHIDInterfaceFound;
78 }
79
80 while (FoundEndpoints != ((1 << HID_DATA_IN_PIPE) | (1 << HID_DATA_OUT_PIPE)))
81 {
82 /* Get the next HID interface's data endpoint descriptor */
83 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
84 NextInterfaceHIDDataEndpoint))
85 {
86 /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
87 * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
88 if (FoundEndpoints == (1 << HID_DATA_IN_PIPE))
89 break;
90
91 /* Descriptor not found, error out */
92 return NoEndpointFound;
93 }
94
95 /* Retrieve the endpoint address from the endpoint descriptor */
96 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
97
98 /* If the endpoint is a IN type endpoint */
99 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
100 {
101 /* Configure the HID data IN pipe */
102 Pipe_ConfigurePipe(HID_DATA_IN_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
103 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
104
105 Pipe_SetInfiniteINRequests();
106
107 #if defined(INTERRUPT_DATA_PIPE)
108 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
109
110 /* Enable the pipe IN interrupt for the data pipe */
111 USB_INT_Enable(PIPE_INT_IN);
112 #endif
113
114 FoundEndpoints |= (1 << HID_DATA_IN_PIPE);
115 }
116 else
117 {
118 /* Configure the HID data OUT pipe */
119 Pipe_ConfigurePipe(HID_DATA_OUT_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
120 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
121
122 FoundEndpoints |= (1 << HID_DATA_OUT_PIPE);
123 }
124 }
125
126 /* Valid data found, return success */
127 return SuccessfulConfigRead;
128 }
129
130 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
131 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
132 * descriptor processing if an incompatible descriptor configuration is found.
133 *
134 * This comparator searches for the next Interface descriptor of the correct HID Class value.
135 *
136 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
137 */
138 DESCRIPTOR_COMPARATOR(NextHIDInterface)
139 {
140 /* Determine if the current descriptor is an interface descriptor */
141 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
142 {
143 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
144 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == HID_CLASS)
145 {
146 /* Indicate that the descriptor being searched for has been found */
147 return Descriptor_Search_Found;
148 }
149 }
150
151 /* Current descriptor does not match what this comparator is looking for */
152 return Descriptor_Search_NotFound;
153 }
154
155 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
156 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
157 * descriptor processing if an incompatible descriptor configuration is found.
158 *
159 * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
160 * aborting the search if another interface descriptor is found before the required endpoint.
161 *
162 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
163 */
164 DESCRIPTOR_COMPARATOR(NextInterfaceHIDDataEndpoint)
165 {
166 /* Determine the type of the current descriptor */
167 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
168 {
169 /* Indicate that the descriptor being searched for has been found */
170 return Descriptor_Search_Found;
171 }
172 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
173 {
174 /* Indicate that the search has failed prematurely and should be aborted */
175 return Descriptor_Search_Fail;
176 }
177
178 /* Current descriptor does not match what this comparator is looking for */
179 return Descriptor_Search_NotFound;
180 }