Seperated out parts of the PrinterHost incomplete demo into a seperate Lib subdirectory.
[pub/USBasp.git] / Demos / Host / Incomplete / PrinterHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 uint8_t ProcessConfigurationDescriptor(void)
34 {
35 uint8_t* ConfigDescriptorData;
36 uint16_t ConfigDescriptorSize;
37 uint8_t ErrorCode;
38 uint8_t FoundEndpoints = 0;
39 uint8_t FoundEndpointMask;
40
41 /* Get Configuration Descriptor size from the device */
42 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
43 return ControlError;
44
45 /* Ensure that the Configuration Descriptor isn't too large */
46 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
47 return DescriptorTooLarge;
48
49 /* Allocate enough memory for the entire config descriptor */
50 ConfigDescriptorData = alloca(ConfigDescriptorSize);
51
52 /* Retrieve the entire configuration descriptor into the allocated buffer */
53 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
54
55 /* Validate returned data - ensure first entry is a configuration header descriptor */
56 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
57 return InvalidConfigDataReturned;
58
59 /* Get the printer interface from the configuration descriptor */
60 if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
61 NextPrinterInterface)))
62 {
63 /* Descriptor not found, error out */
64 return NoInterfaceFound;
65 }
66
67 /* Get the printer's communication protocol */
68 PrinterProtocol = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).Protocol;
69
70 /* Determine what endpoints to look for from the protocol */
71 switch (PrinterProtocol)
72 {
73 case PROTOCOL_UNIDIRECTIONAL:
74 FoundEndpointMask = (1 << PRINTER_DATA_OUT_PIPE);
75 break;
76 case PROTOCOL_BIDIRECTIONAL:
77 case PROTOCOL_IEEE1284:
78 FoundEndpointMask = ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE));
79 break;
80 default:
81 return NoInterfaceFound;
82 }
83
84 /* Get the IN and OUT data endpoints for the mass storage interface */
85 while (FoundEndpoints != FoundEndpointMask)
86 {
87 /* Fetch the next bulk endpoint from the current printer interface */
88 if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
89 NextInterfaceBulkDataEndpoint)))
90 {
91 /* Descriptor not found, error out */
92 return NoEndpointFound;
93 }
94
95 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
96
97 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
98 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
99 {
100 /* Configure the data IN pipe */
101 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
102 EndpointData->EndpointAddress, EndpointData->EndpointSize,
103 PIPE_BANK_SINGLE);
104
105 Pipe_SetInfiniteINRequests();
106
107 /* Set the flag indicating that the data IN pipe has been found */
108 FoundEndpoints |= (1 << PRINTER_DATA_IN_PIPE);
109 }
110 else
111 {
112 /* Configure the data OUT pipe */
113 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
114 EndpointData->EndpointAddress, EndpointData->EndpointSize,
115 PIPE_BANK_SINGLE);
116
117 /* Set the flag indicating that the data OUT pipe has been found */
118 FoundEndpoints |= (1 << PRINTER_DATA_OUT_PIPE);
119 }
120 }
121
122 /* Valid data found, return success */
123 return SuccessfulConfigRead;
124 }
125
126 uint8_t NextPrinterInterface(void* CurrentDescriptor)
127 {
128 /* PURPOSE: Find next mass storage class interface descriptor */
129
130 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
131 {
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))
135 {
136 return DESCRIPTOR_SEARCH_Found;
137 }
138 }
139
140 return DESCRIPTOR_SEARCH_NotFound;
141 }
142
143 uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor)
144 {
145 /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
146
147 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
148 {
149 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
150 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
151
152 /* Check the endpoint type, break out if correct BULK type endpoint found */
153 if (EndpointType == EP_TYPE_BULK)
154 return DESCRIPTOR_SEARCH_Found;
155 }
156 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
157 {
158 return DESCRIPTOR_SEARCH_Fail;
159 }
160
161 return DESCRIPTOR_SEARCH_NotFound;
162 }