d4eaff94a1abc11e8cb3f8943318b6a155e08d7d
[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 PrinterInterfaceNumber;
34 uint8_t PrinterAltSetting;
35
36
37 uint8_t ProcessConfigurationDescriptor(void)
38 {
39 uint8_t* ConfigDescriptorData;
40 uint16_t ConfigDescriptorSize;
41 uint8_t ErrorCode;
42 uint8_t FoundEndpoints = 0;
43
44 /* Get Configuration Descriptor size from the device */
45 if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
46 return ControlError;
47
48 /* Ensure that the Configuration Descriptor isn't too large */
49 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
50 return DescriptorTooLarge;
51
52 /* Allocate enough memory for the entire config descriptor */
53 ConfigDescriptorData = alloca(ConfigDescriptorSize);
54
55 /* Retrieve the entire configuration descriptor into the allocated buffer */
56 USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
57
58 /* Validate returned data - ensure first entry is a configuration header descriptor */
59 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
60 return InvalidConfigDataReturned;
61
62 /* Get the printer interface from the configuration descriptor */
63 if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
64 DComp_NextBidirectionalPrinterInterface)))
65 {
66 /* Descriptor not found, error out */
67 return NoInterfaceFound;
68 }
69
70 PrinterInterfaceNumber = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).InterfaceNumber;
71 PrinterAltSetting = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).AlternateSetting;
72
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)))
75 {
76 /* Fetch the next bulk endpoint from the current printer interface */
77 if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
78 DComp_NextInterfaceBulkDataEndpoint)))
79 {
80 /* Descriptor not found, error out */
81 return NoEndpointFound;
82 }
83
84 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
85
86 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
87 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
88 {
89 /* Configure the data IN pipe */
90 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
91 EndpointData->EndpointAddress, EndpointData->EndpointSize,
92 PIPE_BANK_SINGLE);
93
94 Pipe_SetInfiniteINRequests();
95
96 /* Set the flag indicating that the data IN pipe has been found */
97 FoundEndpoints |= (1 << PRINTER_DATA_IN_PIPE);
98 }
99 else
100 {
101 /* Configure the data OUT pipe */
102 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
103 EndpointData->EndpointAddress, EndpointData->EndpointSize,
104 PIPE_BANK_SINGLE);
105
106 /* Set the flag indicating that the data OUT pipe has been found */
107 FoundEndpoints |= (1 << PRINTER_DATA_OUT_PIPE);
108 }
109 }
110
111 /* Valid data found, return success */
112 return SuccessfulConfigRead;
113 }
114
115 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor)
116 {
117 /* PURPOSE: Find next bidirectional protocol printer class interface descriptor */
118
119 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
120 {
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))
125 {
126 return DESCRIPTOR_SEARCH_Found;
127 }
128 }
129
130 return DESCRIPTOR_SEARCH_NotFound;
131 }
132
133 uint8_t DComp_NextInterfaceBulkDataEndpoint(void* CurrentDescriptor)
134 {
135 /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
136
137 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
138 {
139 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
140 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
141
142 /* Check the endpoint type, break out if correct BULK type endpoint found */
143 if (EndpointType == EP_TYPE_BULK)
144 return DESCRIPTOR_SEARCH_Found;
145 }
146 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
147 {
148 return DESCRIPTOR_SEARCH_Fail;
149 }
150
151 return DESCRIPTOR_SEARCH_NotFound;
152 }