Cleanups to the MassStorage Device demos, and the MassStorage Device Class driver.
[pub/USBasp.git] / Demos / Host / Incomplete / 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 * RNDOS 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 uint32_t RequestID = 0;
40
41 uint8_t RNDIS_SendEncapsulatedCommand(void* Buffer, uint16_t Length)
42 {
43 USB_ControlRequest = (USB_Request_Header_t)
44 {
45 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
46 .bRequest = REQ_SendEncapsulatedCommand,
47 .wValue = 0,
48 .wIndex = 0,
49 .wLength = Length,
50 };
51
52 /* Select the control pipe for the request transfer */
53 Pipe_SelectPipe(PIPE_CONTROLPIPE);
54
55 return USB_Host_SendControlRequest(Buffer);
56 }
57
58 uint8_t RNDIS_GetEncapsulatedResponse(void* Buffer, uint16_t Length)
59 {
60 USB_ControlRequest = (USB_Request_Header_t)
61 {
62 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
63 .bRequest = REQ_GetEncapsulatedResponse,
64 .wValue = 0,
65 .wIndex = 0,
66 .wLength = Length,
67 };
68
69 /* Select the control pipe for the request transfer */
70 Pipe_SelectPipe(PIPE_CONTROLPIPE);
71
72 return USB_Host_SendControlRequest(Buffer);
73 }
74
75 uint8_t RNDIS_KeepAlive(void)
76 {
77 uint8_t ErrorCode;
78
79 RNDIS_KeepAlive_Message_t KeepAliveMessage;
80 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
81
82 KeepAliveMessage.MessageType = REMOTE_NDIS_KEEPALIVE_MSG;
83 KeepAliveMessage.MessageLength = sizeof(RNDIS_KeepAlive_Message_t);
84 KeepAliveMessage.RequestId = RequestID++;
85
86 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&KeepAliveMessage,
87 sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
88 {
89 return ErrorCode;
90 }
91
92 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse,
93 sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
94 {
95 return ErrorCode;
96 }
97
98 return HOST_SENDCONTROL_Successful;
99 }
100
101 uint8_t RNDIS_InitializeDevice(uint16_t HostMaxPacketSize, uint16_t* DeviceMaxPacketSize)
102 {
103 uint8_t ErrorCode;
104
105 RNDIS_Initialize_Message_t InitMessage;
106 RNDIS_Initialize_Complete_t InitMessageResponse;
107
108 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
109 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
110 InitMessage.RequestId = RequestID++;
111
112 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
113 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
114 InitMessage.MaxTransferSize = HostMaxPacketSize;
115
116 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
117 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
118 {
119 return ErrorCode;
120 }
121
122 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&InitMessageResponse,
123 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
124 {
125 return ErrorCode;
126 }
127
128 if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
129 return RNDIS_COMMAND_FAILED;
130
131 *DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
132
133 return HOST_SENDCONTROL_Successful;
134 }
135
136 uint8_t RNDIS_SetRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
137 {
138 uint8_t ErrorCode;
139
140 struct
141 {
142 RNDIS_Set_Message_t SetMessage;
143 uint8_t ContigiousBuffer[Length];
144 } SetMessageData;
145
146 RNDIS_Set_Complete_t SetMessageResponse;
147
148 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
149 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
150 SetMessageData.SetMessage.RequestId = RequestID++;
151
152 SetMessageData.SetMessage.Oid = Oid;
153 SetMessageData.SetMessage.InformationBufferLength = Length;
154 SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
155 SetMessageData.SetMessage.DeviceVcHandle = 0;
156
157 memcpy(&SetMessageData.ContigiousBuffer, Buffer, Length);
158
159 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
160 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
161 {
162 return ErrorCode;
163 }
164
165 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
166 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
167 {
168 return ErrorCode;
169 }
170
171 if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
172 return RNDIS_COMMAND_FAILED;
173
174 return HOST_SENDCONTROL_Successful;
175 }
176
177 uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t MaxLength)
178 {
179 uint8_t ErrorCode;
180
181 RNDIS_Query_Message_t QueryMessage;
182
183 struct
184 {
185 RNDIS_Query_Complete_t QueryMessageResponse;
186 uint8_t ContigiousBuffer[MaxLength];
187 } QueryMessageResponseData;
188
189 QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
190 QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
191 QueryMessage.RequestId = RequestID++;
192
193 QueryMessage.Oid = Oid;
194 QueryMessage.InformationBufferLength = 0;
195 QueryMessage.InformationBufferOffset = 0;
196 QueryMessage.DeviceVcHandle = 0;
197
198 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&QueryMessage,
199 sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
200 {
201 return ErrorCode;
202 }
203
204 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData,
205 sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
206 {
207 return ErrorCode;
208 }
209
210 if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
211 return RNDIS_COMMAND_FAILED;
212
213 memcpy(Buffer, &QueryMessageResponseData.ContigiousBuffer, MaxLength);
214
215 return HOST_SENDCONTROL_Successful;
216 }
217
218 uint8_t RNDIS_GetPacketLength(uint16_t* PacketLength)
219 {
220 uint8_t ErrorCode;
221
222 RNDIS_Packet_Message_t DeviceMessage;
223
224 if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t))) != PIPE_RWSTREAM_NoError)
225 {
226 return ErrorCode;
227 }
228
229 *PacketLength = (uint16_t)DeviceMessage.DataLength;
230
231 Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)));
232
233 return PIPE_RWSTREAM_NoError;
234 }