]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-git/Demos/Host/LowLevel/RNDISEthernetHost/Lib/RNDISCommands.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[qmk_firmware.git] / protocol / lufa / LUFA-git / Demos / Host / LowLevel / RNDISEthernetHost / Lib / RNDISCommands.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 /** \file
32  *
33  *  RNDIS Device commands, to issue RNDIS commands to the device for
34  *  the control and data transfer between the host and RNDIS device.
35  */
36
37 #include "RNDISCommands.h"
38
39 /** Current RNDIS Request ID, for associating sent commands with received data */
40 uint32_t RequestID = 0;
41
42
43 /** Function to send the given encapsulated RNDIS command to the device.
44  *
45  *  \param[in] Buffer  Source command data buffer to send to the device
46  *  \param[in] Length   Number of bytes to send
47  *
48  *  \return A value from the USB_Host_SendControlErrorCodes_t enum
49  */
50 uint8_t RNDIS_SendEncapsulatedCommand(void* const Buffer,
51                                       const uint16_t Length)
52 {
53         USB_ControlRequest = (USB_Request_Header_t)
54                 {
55                         .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
56                         .bRequest      = RNDIS_REQ_SendEncapsulatedCommand,
57                         .wValue        = 0,
58                         .wIndex        = 0,
59                         .wLength       = Length,
60                 };
61
62         /* Select the control pipe for the request transfer */
63         Pipe_SelectPipe(PIPE_CONTROLPIPE);
64
65         return USB_Host_SendControlRequest(Buffer);
66 }
67
68 /** Function to receive the given encapsulated RNDIS response from the device.
69  *
70  *  \param[out] Buffer  Destination command data buffer to write read data from the device to
71  *  \param[in] Length   Number of bytes to read
72  *
73  *  \return A value from the USB_Host_SendControlErrorCodes_t enum
74  */
75 uint8_t RNDIS_GetEncapsulatedResponse(void* const Buffer,
76                                       const uint16_t Length)
77 {
78         USB_ControlRequest = (USB_Request_Header_t)
79                 {
80                         .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
81                         .bRequest      = RNDIS_REQ_GetEncapsulatedResponse,
82                         .wValue        = 0,
83                         .wIndex        = 0,
84                         .wLength       = Length,
85                 };
86
87         /* Select the control pipe for the request transfer */
88         Pipe_SelectPipe(PIPE_CONTROLPIPE);
89
90         return USB_Host_SendControlRequest(Buffer);
91 }
92
93 /** Sends a RNDIS KEEPALIVE command to the device, to ensure that it does not enter standby mode after periods
94  *  of long inactivity.
95  *
96  *  \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
97  *          logical command failure
98  */
99 uint8_t RNDIS_SendKeepAlive(void)
100 {
101         uint8_t ErrorCode;
102
103         RNDIS_KeepAlive_Message_t  KeepAliveMessage;
104         RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
105
106         KeepAliveMessage.MessageType     = REMOTE_NDIS_KEEPALIVE_MSG;
107         KeepAliveMessage.MessageLength   = sizeof(RNDIS_KeepAlive_Message_t);
108         KeepAliveMessage.RequestId       = RequestID++;
109
110         if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&KeepAliveMessage,
111                                                        sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
112         {
113                 return ErrorCode;
114         }
115
116         if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse,
117                                                        sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
118         {
119                 return ErrorCode;
120         }
121
122         return HOST_SENDCONTROL_Successful;
123 }
124
125 /** Initializes the attached RNDIS device's RNDIS interface.
126  *
127  *  \param[in] HostMaxPacketSize  Size of the packet buffer on the host
128  *  \param[out] DeviceMaxPacketSize   Pointer to where the packet buffer size of the device is to be stored
129  *
130  *  \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
131  *          logical command failure
132  */
133 uint8_t RNDIS_InitializeDevice(const uint16_t HostMaxPacketSize,
134                                uint16_t* const DeviceMaxPacketSize)
135 {
136         uint8_t ErrorCode;
137
138         RNDIS_Initialize_Message_t  InitMessage;
139         RNDIS_Initialize_Complete_t InitMessageResponse;
140
141         InitMessage.MessageType     = REMOTE_NDIS_INITIALIZE_MSG;
142         InitMessage.MessageLength   = sizeof(RNDIS_Initialize_Message_t);
143         InitMessage.RequestId       = RequestID++;
144
145         InitMessage.MajorVersion    = REMOTE_NDIS_VERSION_MAJOR;
146         InitMessage.MinorVersion    = REMOTE_NDIS_VERSION_MINOR;
147         InitMessage.MaxTransferSize = HostMaxPacketSize;
148
149         if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
150                                                        sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
151         {
152                 return ErrorCode;
153         }
154
155         if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&InitMessageResponse,
156                                                        sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
157         {
158                 return ErrorCode;
159         }
160
161         if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
162           return RNDIS_COMMAND_FAILED;
163
164         *DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
165
166         return HOST_SENDCONTROL_Successful;
167 }
168
169 /** Sets a given RNDIS property of an attached RNDIS device.
170  *
171  *  \param[in] Oid  OID number of the parameter to set
172  *  \param[in] Buffer  Pointer to where the property data is to be sourced from
173  *  \param[in] Length  Length in bytes of the property data to sent to the device
174  *
175  *  \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
176  *          logical command failure
177  */
178 uint8_t RNDIS_SetRNDISProperty(const uint32_t Oid,
179                                void* Buffer,
180                                const uint16_t Length)
181 {
182         uint8_t ErrorCode;
183
184         struct
185         {
186                 RNDIS_Set_Message_t SetMessage;
187                 uint8_t             ContiguousBuffer[Length];
188         } SetMessageData;
189
190         RNDIS_Set_Complete_t SetMessageResponse;
191
192         SetMessageData.SetMessage.MessageType    = REMOTE_NDIS_SET_MSG;
193         SetMessageData.SetMessage.MessageLength  = sizeof(RNDIS_Set_Message_t) + Length;
194         SetMessageData.SetMessage.RequestId      = RequestID++;
195
196         SetMessageData.SetMessage.Oid            = Oid;
197         SetMessageData.SetMessage.InformationBufferLength = Length;
198         SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
199         SetMessageData.SetMessage.DeviceVcHandle = 0;
200
201         memcpy(SetMessageData.ContiguousBuffer, Buffer, Length);
202
203         if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
204                                                        SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
205         {
206                 return ErrorCode;
207         }
208
209         if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
210                                                        sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
211         {
212                 return ErrorCode;
213         }
214
215         if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
216           return RNDIS_COMMAND_FAILED;
217
218         return HOST_SENDCONTROL_Successful;
219 }
220
221 /** Gets a given RNDIS property of an attached RNDIS device.
222  *
223  *  \param[in] Oid  OID number of the parameter to get
224  *  \param[in] Buffer  Pointer to where the property data is to be written to
225  *  \param[in] MaxLength  Length in bytes of the destination buffer size
226  *
227  *  \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
228  *          logical command failure
229  */
230 uint8_t RNDIS_QueryRNDISProperty(const uint32_t Oid,
231                                  void* Buffer,
232                                  const uint16_t MaxLength)
233 {
234         uint8_t ErrorCode;
235
236         RNDIS_Query_Message_t QueryMessage;
237
238         struct
239         {
240                 RNDIS_Query_Complete_t QueryMessageResponse;
241                 uint8_t                ContiguousBuffer[MaxLength];
242         } QueryMessageResponseData;
243
244         QueryMessage.MessageType    = REMOTE_NDIS_QUERY_MSG;
245         QueryMessage.MessageLength  = sizeof(RNDIS_Query_Message_t);
246         QueryMessage.RequestId      = RequestID++;
247
248         QueryMessage.Oid            = Oid;
249         QueryMessage.InformationBufferLength = 0;
250         QueryMessage.InformationBufferOffset = 0;
251         QueryMessage.DeviceVcHandle = 0;
252
253         if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&QueryMessage,
254                                                        sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
255         {
256                 return ErrorCode;
257         }
258
259         if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData,
260                                                        sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
261         {
262                 return ErrorCode;
263         }
264
265         if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
266           return RNDIS_COMMAND_FAILED;
267
268         memcpy(Buffer, &QueryMessageResponseData.ContiguousBuffer, MaxLength);
269
270         return HOST_SENDCONTROL_Successful;
271 }
272
273 /** Retrieves the size of a received packet, discarding the remainder of the RNDIS packet header to leave only the
274  *  packet contents for processing by the host.
275  *
276  *  \param[out] PacketLength  Size of the packet currently in the pipe
277  *
278  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
279  */
280 uint8_t RNDIS_GetPacketLength(uint16_t* const PacketLength)
281 {
282         uint8_t ErrorCode;
283
284         Pipe_SelectPipe(RNDIS_DATA_IN_PIPE);
285         Pipe_SetPipeToken(PIPE_TOKEN_IN);
286         Pipe_Unfreeze();
287
288         if (!(Pipe_IsReadWriteAllowed()))
289         {
290                 *PacketLength = 0;
291                 Pipe_Freeze();
292                 return PIPE_RWSTREAM_NoError;
293         }
294
295         RNDIS_Packet_Message_t DeviceMessage;
296
297         if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t), NULL)) != PIPE_RWSTREAM_NoError)
298         {
299                 return ErrorCode;
300         }
301
302         *PacketLength = (uint16_t)DeviceMessage.DataLength;
303
304         Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
305                             NULL);
306
307         Pipe_Freeze();
308
309         return PIPE_RWSTREAM_NoError;
310 }
311