Fix PrinterHost demo so that it will only enumerate printers with Bidirectional proto...
[pub/USBasp.git] / Demos / Host / LowLevel / MouseHost / 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 MouseHost_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 mouse interface from the configuration descriptor */
72 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
73 DComp_NextMouseInterface) != DESCRIPTOR_SEARCH_COMP_Found)
74 {
75 /* Descriptor not found, error out */
76 return NoHIDInterfaceFound;
77 }
78
79 /* Get the mouse interface's data endpoint descriptor */
80 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
81 DComp_NextInterfaceMouseDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
82 {
83 /* Descriptor not found, error out */
84 return NoEndpointFound;
85 }
86
87 /* Retrieve the endpoint address from the endpoint descriptor */
88 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
89
90 /* Configure the mouse data pipe */
91 Pipe_ConfigurePipe(MOUSE_DATAPIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
92 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
93
94 /* Valid data found, return success */
95 return SuccessfulConfigRead;
96 }
97
98 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
99 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
100 * descriptor processing if an incompatible descriptor configuration is found.
101 *
102 * This comparator searches for the next Interface descriptor of the correct Mouse HID Class and Protocol values.
103 *
104 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
105 */
106 uint8_t DComp_NextMouseInterface(void* CurrentDescriptor)
107 {
108 /* Determine if the current descriptor is an interface descriptor */
109 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
110 {
111 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
112 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == MOUSE_CLASS) &&
113 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == MOUSE_PROTOCOL))
114 {
115 /* Indicate that the descriptor being searched for has been found */
116 return DESCRIPTOR_SEARCH_Found;
117 }
118 }
119
120 /* Current descriptor does not match what this comparator is looking for */
121 return DESCRIPTOR_SEARCH_NotFound;
122 }
123
124 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
125 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
126 * descriptor processing if an incompatible descriptor configuration is found.
127 *
128 * This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor,
129 * aborting the search if another interface descriptor is found before the required endpoint.
130 *
131 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
132 */
133 uint8_t DComp_NextInterfaceMouseDataEndpoint(void* CurrentDescriptor)
134 {
135 /* Determine the type of the current descriptor */
136 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
137 {
138 /* Check if the current Endpoint descriptor is of type IN */
139 if (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Endpoint_t).EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
140 {
141 /* Indicate that the descriptor being searched for has been found */
142 return DESCRIPTOR_SEARCH_Found;
143 }
144 }
145 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
146 {
147 /* Indicate that the search has failed prematurely and should be aborted */
148 return DESCRIPTOR_SEARCH_Fail;
149 }
150
151 /* Current descriptor does not match what this comparator is looking for */
152 return DESCRIPTOR_SEARCH_NotFound;
153 }