e248b18dabf1bad2dc6176eb7fa19901a48065f0
[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 MaxPacketSize, RNDIS_Initialize_Complete_t* InitMessageResponse)
102 {
103 uint8_t ErrorCode;
104
105 RNDIS_Initialize_Message_t InitMessage;
106
107 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
108 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
109 InitMessage.RequestId = RequestID++;
110 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
111 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
112 InitMessage.MaxTransferSize = sizeof(RNDIS_Packet_Message_t) + MaxPacketSize;
113
114 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
115 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
116 {
117 return ErrorCode;
118 }
119
120 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(InitMessageResponse,
121 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
122 {
123 return ErrorCode;
124 }
125
126 return HOST_SENDCONTROL_Successful;
127 }
128
129 uint8_t RNDIS_SetRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
130 {
131 uint8_t ErrorCode;
132
133 struct
134 {
135 RNDIS_Set_Message_t SetMessage;
136 uint8_t ContigiousBuffer[Length];
137 } SetMessageData;
138
139 RNDIS_Set_Complete_t SetMessageResponse;
140
141 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
142 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
143 SetMessageData.SetMessage.RequestId = RequestID++;
144
145 SetMessageData.SetMessage.Oid = Oid;
146 SetMessageData.SetMessage.InformationBufferLength = Length;
147 SetMessageData.SetMessage.InformationBufferOffset = 0;
148 SetMessageData.SetMessage.DeviceVcHandle = 0;
149
150 memcpy(&SetMessageData.ContigiousBuffer, Buffer, Length);
151
152 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
153 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
154 {
155 return ErrorCode;
156 }
157
158 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
159 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
160 {
161 return ErrorCode;
162 }
163
164 return HOST_SENDCONTROL_Successful;
165 }
166
167 uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
168 {
169 uint8_t ErrorCode;
170
171 RNDIS_Query_Message_t QueryMessage;
172
173 struct
174 {
175 RNDIS_Query_Complete_t QueryMessageResponse;
176 uint8_t ContigiousBuffer[Length];
177 } QueryMessageResponseData;
178
179 QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
180 QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
181 QueryMessage.RequestId = RequestID++;
182
183 QueryMessage.Oid = Oid;
184 QueryMessage.InformationBufferLength = Length;
185 QueryMessage.InformationBufferOffset = 0;
186 QueryMessage.DeviceVcHandle = 0;
187
188 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&QueryMessage,
189 sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
190 {
191 return ErrorCode;
192 }
193
194 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData,
195 sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
196 {
197 return ErrorCode;
198 }
199
200 memcpy(Buffer, &QueryMessageResponseData.ContigiousBuffer, Length);
201
202 return HOST_SENDCONTROL_Successful;
203 }
204
205 uint8_t RNDIS_GetPacketSize(uint16_t* PacketSize)
206 {
207 uint8_t ErrorCode;
208
209 RNDIS_Packet_Message_t DeviceMessage;
210
211 if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t))) != PIPE_RWSTREAM_NoError)
212 {
213 return ErrorCode;
214 }
215
216 *PacketSize = (uint16_t)DeviceMessage.DataLength;
217
218 return PIPE_RWSTREAM_NoError;
219 }