Removed unused INCLUDE_FROM_BOARD_DRIVER internal define from the board driver dispat...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / 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 #if defined(USB_CAN_BE_HOST)
34 uint8_t USB_GetDeviceConfigDescriptor(uint8_t ConfigNumber, uint16_t* const ConfigSizePtr,
35 void* BufferPtr, uint16_t BufferSize)
36 {
37 uint8_t ErrorCode;
38 uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];
39
40 USB_ControlRequest = (USB_Request_Header_t)
41 {
42 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
43 .bRequest = REQ_GetDescriptor,
44 .wValue = ((DTYPE_Configuration << 8) | (ConfigNumber - 1)),
45 .wIndex = 0,
46 .wLength = sizeof(USB_Descriptor_Configuration_Header_t),
47 };
48
49 Pipe_SelectPipe(PIPE_CONTROLPIPE);
50
51 if ((ErrorCode = USB_Host_SendControlRequest(ConfigHeader)) != HOST_SENDCONTROL_Successful)
52 return ErrorCode;
53
54 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
55 *ConfigSizePtr = DESCRIPTOR_CAST(ConfigHeader, USB_Descriptor_Configuration_Header_t).TotalConfigurationSize;
56 #else
57 *ConfigSizePtr = DESCRIPTOR_CAST(ConfigHeader, USB_Descriptor_Configuration_Header_t).wTotalLength;
58 #endif
59
60 if (*ConfigSizePtr > BufferSize)
61 return HOST_GETCONFIG_BuffOverflow;
62
63 USB_ControlRequest.wLength = *ConfigSizePtr;
64
65 if ((ErrorCode = USB_Host_SendControlRequest(BufferPtr)) != HOST_SENDCONTROL_Successful)
66 return ErrorCode;
67
68 if (DESCRIPTOR_TYPE(BufferPtr) != DTYPE_Configuration)
69 return HOST_GETCONFIG_InvalidData;
70
71 return HOST_GETCONFIG_Successful;
72 }
73 #endif
74
75 void USB_GetNextDescriptorOfType(uint16_t* const BytesRem,
76 uint8_t** const CurrConfigLoc,
77 const uint8_t Type)
78 {
79 while (*BytesRem)
80 {
81 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
82
83 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
84 return;
85 }
86 }
87
88 void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
89 uint8_t** const CurrConfigLoc,
90 const uint8_t Type,
91 const uint8_t BeforeType)
92 {
93 while (*BytesRem)
94 {
95 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
96
97 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
98 {
99 return;
100 }
101 else if (DESCRIPTOR_TYPE(*CurrConfigLoc) == BeforeType)
102 {
103 *BytesRem = 0;
104 return;
105 }
106 }
107 }
108
109 void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
110 uint8_t** const CurrConfigLoc,
111 const uint8_t Type,
112 const uint8_t AfterType)
113 {
114 USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);
115
116 if (*BytesRem)
117 USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);
118 }
119
120 uint8_t USB_GetNextDescriptorComp(uint16_t* BytesRem, uint8_t** CurrConfigLoc, ConfigComparatorPtr_t ComparatorRoutine)
121 {
122 uint8_t ErrorCode;
123
124 while (*BytesRem)
125 {
126 uint8_t* PrevDescLoc = *CurrConfigLoc;
127 uint16_t PrevBytesRem = *BytesRem;
128
129 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
130
131 if ((ErrorCode = ComparatorRoutine(*CurrConfigLoc)) != DESCRIPTOR_SEARCH_NotFound)
132 {
133 if (ErrorCode == DESCRIPTOR_SEARCH_Fail)
134 {
135 *CurrConfigLoc = PrevDescLoc;
136 *BytesRem = PrevBytesRem;
137 }
138
139 return ErrorCode;
140 }
141 }
142
143 return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor;
144 }