]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/ConfigDescriptors.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / ConfigDescriptors.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all 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 disclaims 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 #define  __INCLUDE_FROM_USB_DRIVER
32 #include "ConfigDescriptors.h"
33
34 #if defined(USB_CAN_BE_HOST)
35 uint8_t USB_Host_GetDeviceConfigDescriptor(const uint8_t ConfigNumber,
36                                            uint16_t* const ConfigSizePtr,
37                                            void* const BufferPtr,
38                                            const uint16_t BufferSize)
39 {
40         uint8_t ErrorCode;
41         uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];
42
43         USB_ControlRequest = (USB_Request_Header_t)
44                 {
45                         .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
46                         .bRequest      = REQ_GetDescriptor,
47                         .wValue        = ((DTYPE_Configuration << 8) | (ConfigNumber - 1)),
48                         .wIndex        = 0,
49                         .wLength       = sizeof(USB_Descriptor_Configuration_Header_t),
50                 };
51
52         Pipe_SelectPipe(PIPE_CONTROLPIPE);
53
54         if ((ErrorCode = USB_Host_SendControlRequest(ConfigHeader)) != HOST_SENDCONTROL_Successful)
55           return ErrorCode;
56
57         *ConfigSizePtr = le16_to_cpu(DESCRIPTOR_PCAST(ConfigHeader, USB_Descriptor_Configuration_Header_t)->TotalConfigurationSize);
58
59         if (*ConfigSizePtr > BufferSize)
60           return HOST_GETCONFIG_BuffOverflow;
61
62         USB_ControlRequest.wLength = *ConfigSizePtr;
63
64         if ((ErrorCode = USB_Host_SendControlRequest(BufferPtr)) != HOST_SENDCONTROL_Successful)
65           return ErrorCode;
66
67         if (DESCRIPTOR_TYPE(BufferPtr) != DTYPE_Configuration)
68           return HOST_GETCONFIG_InvalidData;
69
70         return HOST_GETCONFIG_Successful;
71 }
72 #endif
73
74 void USB_GetNextDescriptorOfType(uint16_t* const BytesRem,
75                                  void** const CurrConfigLoc,
76                                  const uint8_t Type)
77 {
78         while (*BytesRem)
79         {
80                 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
81
82                 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
83                   return;
84         }
85 }
86
87 void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
88                                        void** const CurrConfigLoc,
89                                        const uint8_t Type,
90                                        const uint8_t BeforeType)
91 {
92         while (*BytesRem)
93         {
94                 USB_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_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
109                                       void** const CurrConfigLoc,
110                                       const uint8_t Type,
111                                       const uint8_t AfterType)
112 {
113         USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);
114
115         if (*BytesRem)
116           USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);
117 }
118
119 uint8_t USB_GetNextDescriptorComp(uint16_t* const BytesRem,
120                                   void** const CurrConfigLoc,
121                                   ConfigComparatorPtr_t const ComparatorRoutine)
122 {
123         uint8_t ErrorCode;
124
125         while (*BytesRem)
126         {
127                 uint8_t* PrevDescLoc  = *CurrConfigLoc;
128                 uint16_t PrevBytesRem = *BytesRem;
129
130                 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
131
132                 if ((ErrorCode = ComparatorRoutine(*CurrConfigLoc)) != DESCRIPTOR_SEARCH_NotFound)
133                 {
134                         if (ErrorCode == DESCRIPTOR_SEARCH_Fail)
135                         {
136                                 *CurrConfigLoc = PrevDescLoc;
137                                 *BytesRem      = PrevBytesRem;
138                         }
139
140                         return ErrorCode;
141                 }
142         }
143
144         return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor;
145 }
146