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