]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Host/LowLevel/PrinterHost/ConfigDescriptor.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Host / LowLevel / PrinterHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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_Interface_t* PrinterInterface = NULL;
54 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
55 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
56
57 /* Retrieve the entire configuration descriptor into the allocated buffer */
58 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
59 {
60 case HOST_GETCONFIG_Successful:
61 break;
62 case HOST_GETCONFIG_InvalidData:
63 return InvalidConfigDataReturned;
64 case HOST_GETCONFIG_BuffOverflow:
65 return DescriptorTooLarge;
66 default:
67 return ControlError;
68 }
69
70 while (!(DataINEndpoint) || !(DataOUTEndpoint))
71 {
72 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
73 if (!(PrinterInterface) ||
74 USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
75 DComp_NextPrinterInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
76 {
77 /* Get the next Printer interface from the configuration descriptor */
78 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
79 DComp_NextBidirectionalPrinterInterface) != DESCRIPTOR_SEARCH_COMP_Found)
80 {
81 /* Descriptor not found, error out */
82 return NoCompatibleInterfaceFound;
83 }
84
85 /* Save the interface in case we need to refer back to it later */
86 PrinterInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
87
88 /* Clear any found endpoints */
89 DataINEndpoint = NULL;
90 DataOUTEndpoint = NULL;
91
92 /* Skip the remainder of the loop as we have not found an endpoint yet */
93 continue;
94 }
95
96 /* Retrieve the endpoint address from the endpoint descriptor */
97 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
98
99 /* If the endpoint is a IN type endpoint */
100 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
101 DataINEndpoint = EndpointData;
102 else
103 DataOUTEndpoint = EndpointData;
104 }
105
106 /* Save Printer interface details for later use */
107 PrinterInterfaceNumber = PrinterInterface->InterfaceNumber;
108 PrinterAltSetting = PrinterInterface->AlternateSetting;
109
110 /* Configure the Printer data IN pipe */
111 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
112
113 /* Configure the Printer data OUT pipe */
114 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
115
116 /* Valid data found, return success */
117 return SuccessfulConfigRead;
118 }
119
120 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
121 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
122 * descriptor processing if an incompatible descriptor configuration is found.
123 *
124 * This comparator searches for the next Bidirectional Printer Interface descriptor of the current Printer interface,
125 * aborting the search if the end of the descriptors is found.
126 *
127 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
128 */
129 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor)
130 {
131 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
132
133 if (Header->Type == DTYPE_Interface)
134 {
135 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
136
137 /* Check the descriptor class, subclass and protocol, break out if correct value interface found */
138 if ((Interface->Class == PRNT_CSCP_PrinterClass) &&
139 (Interface->SubClass == PRNT_CSCP_PrinterSubclass) &&
140 (Interface->Protocol == PRNT_CSCP_BidirectionalProtocol))
141 {
142 return DESCRIPTOR_SEARCH_Found;
143 }
144 }
145
146 return DESCRIPTOR_SEARCH_NotFound;
147 }
148
149 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
150 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
151 * descriptor processing if an incompatible descriptor configuration is found.
152 *
153 * This comparator searches for the next Bulk Endpoint descriptor of the current Printer interface, aborting the
154 * search if another interface descriptor is found before the next endpoint.
155 *
156 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
157 */
158 uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor)
159 {
160 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
161
162 if (Header->Type == DTYPE_Endpoint)
163 {
164 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
165
166 /* Check the endpoint type, break out if correct BULK type endpoint found */
167 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK)
168 return DESCRIPTOR_SEARCH_Found;
169 }
170 else if (Header->Type == DTYPE_Interface)
171 {
172 return DESCRIPTOR_SEARCH_Fail;
173 }
174
175 return DESCRIPTOR_SEARCH_NotFound;
176 }
177
Imprint / Impressum