Reverted Endpoint/Pipe non-sequential configuration hack, placed restriction on the...
[pub/USBasp.git] / Demos / Host / LowLevel / PrinterHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 #include "ConfigDescriptor.h"
32
33 /** Interface number for the bidirectional Printer interface found within the device. */
34 uint8_t PrinterInterfaceNumber;
35
36 /** Interface Alternate Setting number for the bidirectional Printer interface found within the device. */
37 uint8_t PrinterAltSetting;
38
39 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
40 * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
41 * with compatible devices.
42 *
43 * This routine searches for a bidirectional Printer interface descriptor containing bulk IN and OUT data endpoints.
44 *
45 * \return An error code from the \ref PrinterHost_GetConfigDescriptorDataCodes_t enum.
46 */
47 uint8_t ProcessConfigurationDescriptor(void)
48 {
49 uint8_t ConfigDescriptorData[512];
50 void* CurrConfigLocation = ConfigDescriptorData;
51 uint16_t CurrConfigBytesRem;
52
53 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
54 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
55
56 /* Retrieve the entire configuration descriptor into the allocated buffer */
57 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
58 {
59 case HOST_GETCONFIG_Successful:
60 break;
61 case HOST_GETCONFIG_InvalidData:
62 return InvalidConfigDataReturned;
63 case HOST_GETCONFIG_BuffOverflow:
64 return DescriptorTooLarge;
65 default:
66 return ControlError;
67 }
68
69 /* Get the first Printer interface from the configuration descriptor */
70 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
71 DComp_NextBidirectionalPrinterInterface) != DESCRIPTOR_SEARCH_COMP_Found)
72 {
73 /* Descriptor not found, error out */
74 return NoCompatibleInterfaceFound;
75 }
76
77 /* Save Printer interface details for later use */
78 PrinterInterfaceNumber = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).InterfaceNumber;
79 PrinterAltSetting = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).AlternateSetting;
80
81 while (!(DataINEndpoint) || !(DataOUTEndpoint))
82 {
83 /* Get the next Printer interface's data endpoint descriptor */
84 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
85 DComp_NextPrinterInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
86 {
87 /* Clear any found endpoints */
88 DataINEndpoint = NULL;
89 DataOUTEndpoint = NULL;
90
91 /* Get the next Printer interface from the configuration descriptor */
92 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
93 DComp_NextBidirectionalPrinterInterface) != DESCRIPTOR_SEARCH_COMP_Found)
94 {
95 /* Descriptor not found, error out */
96 return NoCompatibleInterfaceFound;
97 }
98
99 /* Save Printer interface details for later use */
100 PrinterInterfaceNumber = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).InterfaceNumber;
101 PrinterAltSetting = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).AlternateSetting;
102 }
103
104 /* Retrieve the endpoint address from the endpoint descriptor */
105 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
106
107 /* If the endpoint is a IN type endpoint */
108 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
109 DataINEndpoint = EndpointData;
110 else
111 DataOUTEndpoint = EndpointData;
112 }
113
114 /* Configure the Printer data IN pipe */
115 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
116 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_SINGLE);
117
118 /* Configure the Printer data OUT pipe */
119 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
120 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, PIPE_BANK_SINGLE);
121
122 /* Valid data found, return success */
123 return SuccessfulConfigRead;
124 }
125
126 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
127 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
128 * descriptor processing if an incompatible descriptor configuration is found.
129 *
130 * This comparator searches for the next Bidirectional Printer Interface descriptor of the current Printer interface,
131 * aborting the search if the end of the descriptors is found.
132 *
133 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
134 */
135 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor)
136 {
137 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
138 {
139 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
140 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
141 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
142 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PRINTER_PROTOCOL))
143 {
144 return DESCRIPTOR_SEARCH_Found;
145 }
146 }
147
148 return DESCRIPTOR_SEARCH_NotFound;
149 }
150
151 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
152 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
153 * descriptor processing if an incompatible descriptor configuration is found.
154 *
155 * This comparator searches for the next Bulk Endpoint descriptor of the current Printer interface, aborting the
156 * search if another interface descriptor is found before the next endpoint.
157 *
158 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
159 */
160 uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor)
161 {
162 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
163 {
164 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
165 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
166
167 /* Check the endpoint type, break out if correct BULK type endpoint found */
168 if (EndpointType == EP_TYPE_BULK)
169 return DESCRIPTOR_SEARCH_Found;
170 }
171 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
172 {
173 return DESCRIPTOR_SEARCH_Fail;
174 }
175
176 return DESCRIPTOR_SEARCH_NotFound;
177 }