3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * RNDOS Device commands, to issue RNDIS commands to the device for
34 * the control and data transfer between the host and RNDIS device.
37 #include "RNDISCommands.h"
39 uint32_t RequestID
= 0;
41 uint8_t RNDIS_SendEncapsulatedCommand(void* Buffer
, uint16_t Length
)
43 USB_ControlRequest
= (USB_Request_Header_t
)
45 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
46 .bRequest
= REQ_SendEncapsulatedCommand
,
52 /* Select the control pipe for the request transfer */
53 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
55 return USB_Host_SendControlRequest(Buffer
);
58 uint8_t RNDIS_GetEncapsulatedResponse(void* Buffer
, uint16_t Length
)
60 USB_ControlRequest
= (USB_Request_Header_t
)
62 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
63 .bRequest
= REQ_GetEncapsulatedResponse
,
69 /* Select the control pipe for the request transfer */
70 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
72 return USB_Host_SendControlRequest(Buffer
);
75 uint8_t RNDIS_KeepAlive(void)
79 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
80 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
82 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
83 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
84 KeepAliveMessage
.RequestId
= RequestID
++;
86 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&KeepAliveMessage
,
87 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
92 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse
,
93 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
98 return HOST_SENDCONTROL_Successful
;
101 uint8_t RNDIS_InitializeDevice(uint16_t HostMaxPacketSize
, uint16_t* DeviceMaxPacketSize
)
105 RNDIS_Initialize_Message_t InitMessage
;
106 RNDIS_Initialize_Complete_t InitMessageResponse
;
108 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
109 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
110 InitMessage
.RequestId
= RequestID
++;
112 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
113 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
114 InitMessage
.MaxTransferSize
= HostMaxPacketSize
;
116 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&InitMessage
,
117 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
122 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&InitMessageResponse
,
123 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
128 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
129 return RNDIS_COMMAND_FAILED
;
131 *DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
133 return HOST_SENDCONTROL_Successful
;
136 uint8_t RNDIS_SetRNDISProperty(uint32_t Oid
, void* Buffer
, uint16_t Length
)
142 RNDIS_Set_Message_t SetMessage
;
143 uint8_t ContigiousBuffer
[Length
];
146 RNDIS_Set_Complete_t SetMessageResponse
;
148 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
149 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
150 SetMessageData
.SetMessage
.RequestId
= RequestID
++;
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;
157 memcpy(&SetMessageData
.ContigiousBuffer
, Buffer
, Length
);
159 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&SetMessageData
,
160 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
165 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&SetMessageResponse
,
166 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
171 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
172 return RNDIS_COMMAND_FAILED
;
174 return HOST_SENDCONTROL_Successful
;
177 uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid
, void* Buffer
, uint16_t MaxLength
)
181 RNDIS_Query_Message_t QueryMessage
;
185 RNDIS_Query_Complete_t QueryMessageResponse
;
186 uint8_t ContigiousBuffer
[MaxLength
];
187 } QueryMessageResponseData
;
189 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
190 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
191 QueryMessage
.RequestId
= RequestID
++;
193 QueryMessage
.Oid
= Oid
;
194 QueryMessage
.InformationBufferLength
= 0;
195 QueryMessage
.InformationBufferOffset
= 0;
196 QueryMessage
.DeviceVcHandle
= 0;
198 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(&QueryMessage
,
199 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
204 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData
,
205 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
210 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
211 return RNDIS_COMMAND_FAILED
;
213 memcpy(Buffer
, &QueryMessageResponseData
.ContigiousBuffer
, MaxLength
);
215 return HOST_SENDCONTROL_Successful
;
218 uint8_t RNDIS_GetPacketLength(uint16_t* PacketLength
)
222 RNDIS_Packet_Message_t DeviceMessage
;
224 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
))) != PIPE_RWSTREAM_NoError
)
229 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
231 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)));
233 return PIPE_RWSTREAM_NoError
;