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