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 ProcessConfigurationDescriptor(void)
35 uint8_t* ConfigDescriptorData
;
36 uint16_t ConfigDescriptorSize
;
38 uint8_t FoundEndpoints
= 0;
39 uint8_t FoundEndpointMask
;
41 /* Get Configuration Descriptor size from the device */
42 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
)
45 /* Ensure that the Configuration Descriptor isn't too large */
46 if (ConfigDescriptorSize
> MAX_CONFIG_DESCRIPTOR_SIZE
)
47 return DescriptorTooLarge
;
49 /* Allocate enough memory for the entire config descriptor */
50 ConfigDescriptorData
= alloca(ConfigDescriptorSize
);
52 /* Retrieve the entire configuration descriptor into the allocated buffer */
53 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
);
55 /* Validate returned data - ensure first entry is a configuration header descriptor */
56 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
57 return InvalidConfigDataReturned
;
59 /* Get the printer interface from the configuration descriptor */
60 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
61 NextPrinterInterface
)))
63 /* Descriptor not found, error out */
64 return NoInterfaceFound
;
67 /* Get the printer's communication protocol */
68 PrinterProtocol
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
).Protocol
;
70 /* Determine what endpoints to look for from the protocol */
71 switch (PrinterProtocol
)
73 case PROTOCOL_UNIDIRECTIONAL
:
74 FoundEndpointMask
= (1 << PRINTER_DATA_OUT_PIPE
);
76 case PROTOCOL_BIDIRECTIONAL
:
77 case PROTOCOL_IEEE1284
:
78 FoundEndpointMask
= ((1 << PRINTER_DATA_OUT_PIPE
) | (1 << PRINTER_DATA_IN_PIPE
));
81 return NoInterfaceFound
;
84 /* Get the IN and OUT data endpoints for the mass storage interface */
85 while (FoundEndpoints
!= FoundEndpointMask
)
87 /* Fetch the next bulk endpoint from the current printer interface */
88 if ((ErrorCode
= USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
89 NextInterfaceBulkDataEndpoint
)))
91 /* Descriptor not found, error out */
92 return NoEndpointFound
;
95 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
97 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
98 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
100 /* Configure the data IN pipe */
101 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
102 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
105 Pipe_SetInfiniteINRequests();
107 /* Set the flag indicating that the data IN pipe has been found */
108 FoundEndpoints
|= (1 << PRINTER_DATA_IN_PIPE
);
112 /* Configure the data OUT pipe */
113 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
114 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
117 /* Set the flag indicating that the data OUT pipe has been found */
118 FoundEndpoints
|= (1 << PRINTER_DATA_OUT_PIPE
);
122 /* Valid data found, return success */
123 return SuccessfulConfigRead
;
126 uint8_t NextPrinterInterface(void* CurrentDescriptor
)
128 /* PURPOSE: Find next mass storage class interface descriptor */
130 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
132 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
133 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== PRINTER_CLASS
) &&
134 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== PRINTER_SUBCLASS
))
136 return DESCRIPTOR_SEARCH_Found
;
140 return DESCRIPTOR_SEARCH_NotFound
;
143 uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor
)
145 /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
147 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
149 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
150 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
152 /* Check the endpoint type, break out if correct BULK type endpoint found */
153 if (EndpointType
== EP_TYPE_BULK
)
154 return DESCRIPTOR_SEARCH_Found
;
156 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
158 return DESCRIPTOR_SEARCH_Fail
;
161 return DESCRIPTOR_SEARCH_NotFound
;