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 uint8_t PrinterInterfaceNumber
;
34 uint8_t PrinterAltSetting
;
37 uint8_t ProcessConfigurationDescriptor(void)
39 uint8_t* ConfigDescriptorData
;
40 uint16_t ConfigDescriptorSize
;
42 uint8_t FoundEndpoints
= 0;
44 /* Get Configuration Descriptor size from the device */
45 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
48 /* Ensure that the Configuration Descriptor isn't too large */
49 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
50 return DescriptorTooLarge
;
52 /* Allocate enough memory for the entire config descriptor */
53 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
55 /* Retrieve the entire configuration descriptor into the allocated buffer */
56 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
);
58 /* Validate returned data - ensure first entry is a configuration header descriptor */
59 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
60 return InvalidConfigDataReturned
;
62 /* Get the printer interface from the configuration descriptor */
63 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
64 DComp_NextBidirectionalPrinterInterface
)))
66 /* Descriptor not found, error out */
67 return NoInterfaceFound
;
70 PrinterInterfaceNumber
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
).InterfaceNumber
;
71 PrinterAltSetting
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
).AlternateSetting
;
73 /* Get the IN and OUT data endpoints for the printer interface */
74 while (FoundEndpoints
!= ((1 << PRINTER_DATA_OUT_PIPE
) | (1 << PRINTER_DATA_IN_PIPE
)))
76 /* Fetch the next bulk endpoint from the current printer interface */
77 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
78 DComp_NextInterfaceBulkDataEndpoint
)))
80 /* Descriptor not found, error out */
81 return NoEndpointFound
;
84 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
86 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
87 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
89 /* Configure the data IN pipe */
90 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
91 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
94 Pipe_SetInfiniteINRequests();
96 /* Set the flag indicating that the data IN pipe has been found */
97 FoundEndpoints
|= (1 << PRINTER_DATA_IN_PIPE
);
101 /* Configure the data OUT pipe */
102 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
103 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
106 /* Set the flag indicating that the data OUT pipe has been found */
107 FoundEndpoints
|= (1 << PRINTER_DATA_OUT_PIPE
);
111 /* Valid data found, return success */
112 return SuccessfulConfigRead
;
115 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor
)
117 /* PURPOSE: Find next bidirectional protocol printer class interface descriptor */
119 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
121 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
122 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== PRINTER_CLASS
) &&
123 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== PRINTER_SUBCLASS
) &&
124 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== PRINTER_PROTOCOL
))
126 return DESCRIPTOR_SEARCH_Found
;
130 return DESCRIPTOR_SEARCH_NotFound
;
133 uint8_t DComp_NextInterfaceBulkDataEndpoint(void* CurrentDescriptor
)
135 /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
137 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
139 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
140 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
142 /* Check the endpoint type, break out if correct BULK type endpoint found */
143 if (EndpointType
== EP_TYPE_BULK
)
144 return DESCRIPTOR_SEARCH_Found
;
146 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
148 return DESCRIPTOR_SEARCH_Fail
;
151 return DESCRIPTOR_SEARCH_NotFound
;