Add new MIDIHost LowLevel demo application.
[pub/USBasp.git] / Demos / Host / LowLevel / 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[512];
40 uint8_t* CurrConfigLocation = ConfigDescriptorData;
41 uint16_t CurrConfigBytesRem;
42 uint8_t FoundEndpoints = 0;
43
44 /* Retrieve the entire configuration descriptor into the allocated buffer */
45 switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
46 {
47 case HOST_GETCONFIG_Successful:
48 break;
49 case HOST_GETCONFIG_InvalidData:
50 return InvalidConfigDataReturned;
51 case HOST_GETCONFIG_BuffOverflow:
52 return DescriptorTooLarge;
53 default:
54 return ControlError;
55 }
56
57 /* Get the printer interface from the configuration descriptor */
58 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, DComp_NextBidirectionalPrinterInterface))
59 {
60 /* Descriptor not found, error out */
61 return NoInterfaceFound;
62 }
63
64 PrinterInterfaceNumber = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).InterfaceNumber;
65 PrinterAltSetting = DESCRIPTOR_CAST(CurrConfigLocation, USB_Descriptor_Interface_t).AlternateSetting;
66
67 /* Get the IN and OUT data endpoints for the printer interface */
68 while (FoundEndpoints != ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE)))
69 {
70 /* Fetch the next bulk endpoint from the current printer interface */
71 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, DComp_NextPrinterInterfaceBulkDataEndpoint))
72 {
73 /* Descriptor not found, error out */
74 return NoEndpointFound;
75 }
76
77 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
78
79 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
80 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
81 {
82 /* Configure the data IN pipe */
83 Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
84 EndpointData->EndpointAddress, EndpointData->EndpointSize,
85 PIPE_BANK_SINGLE);
86
87 Pipe_SetInfiniteINRequests();
88
89 /* Set the flag indicating that the data IN pipe has been found */
90 FoundEndpoints |= (1 << PRINTER_DATA_IN_PIPE);
91 }
92 else
93 {
94 /* Configure the data OUT pipe */
95 Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
96 EndpointData->EndpointAddress, EndpointData->EndpointSize,
97 PIPE_BANK_SINGLE);
98
99 /* Set the flag indicating that the data OUT pipe has been found */
100 FoundEndpoints |= (1 << PRINTER_DATA_OUT_PIPE);
101 }
102 }
103
104 /* Valid data found, return success */
105 return SuccessfulConfigRead;
106 }
107
108 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor)
109 {
110 /* PURPOSE: Find next bidirectional protocol printer class interface descriptor */
111
112 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
113 {
114 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
115 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
116 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
117 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PRINTER_PROTOCOL))
118 {
119 return DESCRIPTOR_SEARCH_Found;
120 }
121 }
122
123 return DESCRIPTOR_SEARCH_NotFound;
124 }
125
126 uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor)
127 {
128 /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
129
130 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
131 {
132 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
133 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
134
135 /* Check the endpoint type, break out if correct BULK type endpoint found */
136 if (EndpointType == EP_TYPE_BULK)
137 return DESCRIPTOR_SEARCH_Found;
138 }
139 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
140 {
141 return DESCRIPTOR_SEARCH_Fail;
142 }
143
144 return DESCRIPTOR_SEARCH_NotFound;
145 }