]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/ConfigDescriptors.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / ConfigDescriptors.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #define  __INCLUDE_FROM_USB_DRIVER\r
32 #include "ConfigDescriptors.h"\r
33 \r
34 #if defined(USB_CAN_BE_HOST)\r
35 uint8_t USB_Host_GetDeviceConfigDescriptor(const uint8_t ConfigNumber,\r
36                                            uint16_t* const ConfigSizePtr,\r
37                                            void* const BufferPtr,\r
38                                            const uint16_t BufferSize)\r
39 {\r
40         uint8_t ErrorCode;\r
41         uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];\r
42 \r
43         USB_ControlRequest = (USB_Request_Header_t)\r
44                 {\r
45                         .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),\r
46                         .bRequest      = REQ_GetDescriptor,\r
47                         .wValue        = ((DTYPE_Configuration << 8) | (ConfigNumber - 1)),\r
48                         .wIndex        = 0,\r
49                         .wLength       = sizeof(USB_Descriptor_Configuration_Header_t),\r
50                 };\r
51 \r
52         Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
53 \r
54         if ((ErrorCode = USB_Host_SendControlRequest(ConfigHeader)) != HOST_SENDCONTROL_Successful)\r
55           return ErrorCode;\r
56 \r
57         *ConfigSizePtr = le16_to_cpu(DESCRIPTOR_PCAST(ConfigHeader, USB_Descriptor_Configuration_Header_t)->TotalConfigurationSize);\r
58 \r
59         if (*ConfigSizePtr > BufferSize)\r
60           return HOST_GETCONFIG_BuffOverflow;\r
61 \r
62         USB_ControlRequest.wLength = *ConfigSizePtr;\r
63 \r
64         if ((ErrorCode = USB_Host_SendControlRequest(BufferPtr)) != HOST_SENDCONTROL_Successful)\r
65           return ErrorCode;\r
66 \r
67         if (DESCRIPTOR_TYPE(BufferPtr) != DTYPE_Configuration)\r
68           return HOST_GETCONFIG_InvalidData;\r
69 \r
70         return HOST_GETCONFIG_Successful;\r
71 }\r
72 #endif\r
73 \r
74 void USB_GetNextDescriptorOfType(uint16_t* const BytesRem,\r
75                                  void** const CurrConfigLoc,\r
76                                  const uint8_t Type)\r
77 {\r
78         while (*BytesRem)\r
79         {\r
80                 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);\r
81 \r
82                 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)\r
83                   return;\r
84         }\r
85 }\r
86 \r
87 void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,\r
88                                        void** const CurrConfigLoc,\r
89                                        const uint8_t Type,\r
90                                        const uint8_t BeforeType)\r
91 {\r
92         while (*BytesRem)\r
93         {\r
94                 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);\r
95 \r
96                 if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)\r
97                 {\r
98                         return;\r
99                 }\r
100                 else if (DESCRIPTOR_TYPE(*CurrConfigLoc) == BeforeType)\r
101                 {\r
102                         *BytesRem = 0;\r
103                         return;\r
104                 }\r
105         }\r
106 }\r
107 \r
108 void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,\r
109                                       void** const CurrConfigLoc,\r
110                                       const uint8_t Type,\r
111                                       const uint8_t AfterType)\r
112 {\r
113         USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);\r
114 \r
115         if (*BytesRem)\r
116           USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);\r
117 }\r
118 \r
119 uint8_t USB_GetNextDescriptorComp(uint16_t* const BytesRem,\r
120                                   void** const CurrConfigLoc,\r
121                                   ConfigComparatorPtr_t const ComparatorRoutine)\r
122 {\r
123         uint8_t ErrorCode;\r
124 \r
125         while (*BytesRem)\r
126         {\r
127                 uint8_t* PrevDescLoc  = *CurrConfigLoc;\r
128                 uint16_t PrevBytesRem = *BytesRem;\r
129 \r
130                 USB_GetNextDescriptor(BytesRem, CurrConfigLoc);\r
131 \r
132                 if ((ErrorCode = ComparatorRoutine(*CurrConfigLoc)) != DESCRIPTOR_SEARCH_NotFound)\r
133                 {\r
134                         if (ErrorCode == DESCRIPTOR_SEARCH_Fail)\r
135                         {\r
136                                 *CurrConfigLoc = PrevDescLoc;\r
137                                 *BytesRem      = PrevBytesRem;\r
138                         }\r
139 \r
140                         return ErrorCode;\r
141                 }\r
142         }\r
143 \r
144         return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor;\r
145 }\r
146 \r