More documentation changes for better module-level documentation rather than file...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / 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 "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_HOST)
34
35 #include "ConfigDescriptor.h"
36
37 uint8_t USB_Host_GetDeviceConfigDescriptor(uint16_t* const ConfigSizePtr, void* BufferPtr)
38 {
39 uint8_t ErrorCode;
40
41 USB_HostRequest = (USB_Host_Request_Header_t)
42 {
43 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
44 bRequest: REQ_GetDescriptor,
45 wValue: (DTYPE_Configuration << 8),
46 wIndex: 0,
47 wLength: sizeof(USB_Descriptor_Configuration_Header_t),
48 };
49
50 Pipe_SelectPipe(PIPE_CONTROLPIPE);
51
52 if (BufferPtr == NULL)
53 {
54 uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];
55
56 ErrorCode = USB_Host_SendControlRequest(ConfigHeader);
57
58 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
59 *ConfigSizePtr = DESCRIPTOR_CAST(ConfigHeader, USB_Descriptor_Configuration_Header_t).TotalConfigurationSize;
60 #else
61 *ConfigSizePtr = DESCRIPTOR_CAST(ConfigHeader, USB_Descriptor_Configuration_Header_t).wTotalLength;
62 #endif
63 }
64 else
65 {
66 USB_HostRequest.wLength = *ConfigSizePtr;
67
68 ErrorCode = USB_Host_SendControlRequest(BufferPtr);
69 }
70
71 return ErrorCode;
72 }
73
74 void USB_Host_GetNextDescriptorOfType(uint16_t* const BytesRem,
75 uint8_t** const CurrConfigLoc,
76 const uint8_t Type)
77 {
78 while (*BytesRem)
79 {
80 USB_Host_GetNextDescriptor(BytesRem, CurrConfigLoc);
81
82 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
83 return;
84 }
85 }
86
87 void USB_Host_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
88 uint8_t** const CurrConfigLoc,
89 const uint8_t Type,
90 const uint8_t BeforeType)
91 {
92 while (*BytesRem)
93 {
94 USB_Host_GetNextDescriptor(BytesRem, CurrConfigLoc);
95
96 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
97 {
98 return;
99 }
100 else if (DESCRIPTOR_TYPE(*CurrConfigLoc) == BeforeType)
101 {
102 *BytesRem = 0;
103 return;
104 }
105 }
106 }
107
108 void USB_Host_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
109 uint8_t** const CurrConfigLoc,
110 const uint8_t Type,
111 const uint8_t AfterType)
112 {
113 USB_Host_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);
114
115 if (*BytesRem)
116 USB_Host_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);
117 }
118
119 uint8_t USB_Host_GetNextDescriptorComp_P(uint16_t* BytesRem, uint8_t** CurrConfigLoc,
120 uint8_t (* const ComparatorRoutine)(void*))
121 {
122 uint8_t ErrorCode;
123
124 while (*BytesRem)
125 {
126 uint8_t* PrevDescLoc = *CurrConfigLoc;
127 uint16_t PrevBytesRem = *BytesRem;
128
129 USB_Host_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 }
145
146 #endif