The USB_Host_SendControlRequest() function no longer automatically selects the Contro...
[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 "ConfigDescriptor.h"
32
33 uint8_t USB_Host_GetDeviceConfigDescriptor(uint16_t* const ConfigSizePtr, void* BufferPtr)
34 {
35 uint8_t ErrorCode;
36
37 USB_HostRequest = (USB_Host_Request_Header_t)
38 {
39 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
40 bRequest: REQ_GetDescriptor,
41 wValue: (DTYPE_Configuration << 8),
42 wIndex: 0,
43 wLength: sizeof(USB_Descriptor_Configuration_Header_t),
44 };
45
46 Pipe_SelectPipe(PIPE_CONTROLPIPE);
47
48 if (BufferPtr == NULL)
49 {
50 uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];
51
52 ErrorCode = USB_Host_SendControlRequest(ConfigHeader);
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 else
61 {
62 USB_HostRequest.wLength = *ConfigSizePtr;
63
64 ErrorCode = USB_Host_SendControlRequest(BufferPtr);
65 }
66
67 return ErrorCode;
68 }
69
70 void USB_Host_GetNextDescriptorOfType(uint16_t* const BytesRem,
71 uint8_t** const CurrConfigLoc,
72 const uint8_t Type)
73 {
74 while (*BytesRem)
75 {
76 USB_Host_GetNextDescriptor(BytesRem, CurrConfigLoc);
77
78 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
79 return;
80 }
81 }
82
83 void USB_Host_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
84 uint8_t** const CurrConfigLoc,
85 const uint8_t Type,
86 const uint8_t BeforeType)
87 {
88 while (*BytesRem)
89 {
90 USB_Host_GetNextDescriptor(BytesRem, CurrConfigLoc);
91
92 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
93 {
94 return;
95 }
96 else if (DESCRIPTOR_TYPE(*CurrConfigLoc) == BeforeType)
97 {
98 *BytesRem = 0;
99 return;
100 }
101 }
102 }
103
104 void USB_Host_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
105 uint8_t** const CurrConfigLoc,
106 const uint8_t Type,
107 const uint8_t AfterType)
108 {
109 USB_Host_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);
110
111 if (*BytesRem)
112 USB_Host_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);
113 }
114
115 uint8_t USB_Host_GetNextDescriptorComp_P(uint16_t* BytesRem, uint8_t** CurrConfigLoc,
116 uint8_t (* const ComparatorRoutine)(void*))
117 {
118 uint8_t ErrorCode;
119
120 while (*BytesRem)
121 {
122 uint8_t* PrevDescLoc = *CurrConfigLoc;
123 uint16_t PrevBytesRem = *BytesRem;
124
125 USB_Host_GetNextDescriptor(BytesRem, CurrConfigLoc);
126
127 if ((ErrorCode = ComparatorRoutine(*CurrConfigLoc)) != Descriptor_Search_NotFound)
128 {
129 if (ErrorCode == Descriptor_Search_Fail)
130 {
131 *CurrConfigLoc = PrevDescLoc;
132 *BytesRem = PrevBytesRem;
133 }
134
135 return ErrorCode;
136 }
137 }
138
139 return Descriptor_Search_Comp_EndOfDescriptor;
140 }