3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #include "ConfigDescriptor.h"
33 /** Interface number for the bidirectional Printer interface found within the device. */
34 uint8_t PrinterInterfaceNumber
;
36 /** Interface Alternate Setting number for the bidirectional Printer interface found within the device. */
37 uint8_t PrinterAltSetting
;
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.
43 * This routine searches for a bidirectional Printer interface descriptor containing bulk IN and OUT data endpoints.
45 * \return An error code from the \ref PrinterHost_GetConfigDescriptorDataCodes_t enum.
47 uint8_t ProcessConfigurationDescriptor(void)
49 uint8_t ConfigDescriptorData
[512];
50 uint8_t* CurrConfigLocation
= ConfigDescriptorData
;
51 uint16_t CurrConfigBytesRem
;
52 uint8_t FoundEndpoints
= 0;
54 /* Retrieve the entire configuration descriptor into the allocated buffer */
55 switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
)))
57 case HOST_GETCONFIG_Successful
:
59 case HOST_GETCONFIG_InvalidData
:
60 return InvalidConfigDataReturned
;
61 case HOST_GETCONFIG_BuffOverflow
:
62 return DescriptorTooLarge
;
67 /* Get the printer interface from the configuration descriptor */
68 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, DComp_NextBidirectionalPrinterInterface
))
70 /* Descriptor not found, error out */
71 return NoInterfaceFound
;
74 PrinterInterfaceNumber
= DESCRIPTOR_CAST(CurrConfigLocation
, USB_Descriptor_Interface_t
).InterfaceNumber
;
75 PrinterAltSetting
= DESCRIPTOR_CAST(CurrConfigLocation
, USB_Descriptor_Interface_t
).AlternateSetting
;
77 /* Get the IN and OUT data endpoints for the printer interface */
78 while (FoundEndpoints
!= ((1 << PRINTER_DATA_OUT_PIPE
) | (1 << PRINTER_DATA_IN_PIPE
)))
80 /* Fetch the next bulk endpoint from the current printer interface */
81 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, DComp_NextPrinterInterfaceBulkDataEndpoint
))
83 /* Descriptor not found, error out */
84 return NoEndpointFound
;
87 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
);
89 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
90 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
92 /* Configure the data IN pipe */
93 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
94 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
97 Pipe_SetInfiniteINRequests();
99 /* Set the flag indicating that the data IN pipe has been found */
100 FoundEndpoints
|= (1 << PRINTER_DATA_IN_PIPE
);
104 /* Configure the data OUT pipe */
105 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
106 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
109 /* Set the flag indicating that the data OUT pipe has been found */
110 FoundEndpoints
|= (1 << PRINTER_DATA_OUT_PIPE
);
114 /* Valid data found, return success */
115 return SuccessfulConfigRead
;
118 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
119 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
120 * descriptor processing if an incompatible descriptor configuration is found.
122 * This comparator searches for the next Bidirectional Printer Interface descriptor of the current Printer interface,
123 * aborting the search if the end of the descriptors is found.
125 * \return A value from the \ref DSEARCH_Return_ErrorCodes_t enum
127 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor
)
129 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
131 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
132 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== PRINTER_CLASS
) &&
133 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== PRINTER_SUBCLASS
) &&
134 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== PRINTER_PROTOCOL
))
136 return DESCRIPTOR_SEARCH_Found
;
140 return DESCRIPTOR_SEARCH_NotFound
;
143 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
144 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
145 * descriptor processing if an incompatible descriptor configuration is found.
147 * This comparator searches for the next Bulk Endpoint descriptor of the current Printer interface, aborting the
148 * search if another interface descriptor is found before the next endpoint.
150 * \return A value from the \ref DSEARCH_Return_ErrorCodes_t enum
152 uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor
)
154 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
156 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
157 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
159 /* Check the endpoint type, break out if correct BULK type endpoint found */
160 if (EndpointType
== EP_TYPE_BULK
)
161 return DESCRIPTOR_SEARCH_Found
;
163 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
165 return DESCRIPTOR_SEARCH_Fail
;
168 return DESCRIPTOR_SEARCH_NotFound
;