3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_RNDIS_CLASS_HOST_C
36 #define __INCLUDE_FROM_RNDIS_DRIVER
39 uint8_t RNDIS_Host_ConfigurePipes(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Endpoint_t
* NotificationEndpoint
= NULL
;
46 USB_Descriptor_Interface_t
* RNDISControlInterface
= NULL
;
48 memset(&RNDISInterfaceInfo
->State
, 0x00, sizeof(RNDISInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return RNDIS_ENUMERROR_InvalidConfigDescriptor
;
53 RNDISControlInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
55 while (!(DataINEndpoint
) || !(DataOUTEndpoint
) || !(NotificationEndpoint
))
57 if (!(RNDISControlInterface
) ||
58 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
59 DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
61 if (NotificationEndpoint
)
63 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
64 DCOMP_RNDIS_Host_NextRNDISDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound
;
69 DataINEndpoint
= NULL
;
70 DataOUTEndpoint
= NULL
;
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
75 DCOMP_RNDIS_Host_NextRNDISControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
77 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound
;
80 RNDISControlInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
82 NotificationEndpoint
= NULL
;
88 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
90 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
92 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
93 NotificationEndpoint
= EndpointData
;
95 DataINEndpoint
= EndpointData
;
99 DataOUTEndpoint
= EndpointData
;
103 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
105 if (PipeNum
== RNDISInterfaceInfo
->Config
.DataINPipeNumber
)
107 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
108 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
109 RNDISInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
111 RNDISInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
113 else if (PipeNum
== RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
)
115 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
116 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
117 RNDISInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
119 RNDISInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
121 else if (PipeNum
== RNDISInterfaceInfo
->Config
.NotificationPipeNumber
)
123 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
124 NotificationEndpoint
->EndpointAddress
, NotificationEndpoint
->EndpointSize
,
125 RNDISInterfaceInfo
->Config
.NotificationPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
126 Pipe_SetInterruptPeriod(NotificationEndpoint
->PollingIntervalMS
);
128 RNDISInterfaceInfo
->State
.NotificationPipeSize
= NotificationEndpoint
->EndpointSize
;
132 RNDISInterfaceInfo
->State
.ControlInterfaceNumber
= RNDISControlInterface
->InterfaceNumber
;
133 RNDISInterfaceInfo
->State
.IsActive
= true;
135 return RNDIS_ENUMERROR_NoError
;
138 static uint8_t DCOMP_RNDIS_Host_NextRNDISControlInterface(void* const CurrentDescriptor
)
140 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
142 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
143 USB_Descriptor_Interface_t
);
145 if ((CurrentInterface
->Class
== RNDIS_CONTROL_CLASS
) &&
146 (CurrentInterface
->SubClass
== RNDIS_CONTROL_SUBCLASS
) &&
147 (CurrentInterface
->Protocol
== RNDIS_CONTROL_PROTOCOL
))
149 return DESCRIPTOR_SEARCH_Found
;
153 return DESCRIPTOR_SEARCH_NotFound
;
156 static uint8_t DCOMP_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor
)
158 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
160 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
161 USB_Descriptor_Interface_t
);
163 if ((CurrentInterface
->Class
== RNDIS_DATA_CLASS
) &&
164 (CurrentInterface
->SubClass
== RNDIS_DATA_SUBCLASS
) &&
165 (CurrentInterface
->Protocol
== RNDIS_DATA_PROTOCOL
))
167 return DESCRIPTOR_SEARCH_Found
;
171 return DESCRIPTOR_SEARCH_NotFound
;
174 static uint8_t DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor
)
176 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
178 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
179 USB_Descriptor_Endpoint_t
);
181 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
183 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
184 !(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
186 return DESCRIPTOR_SEARCH_Found
;
189 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
191 return DESCRIPTOR_SEARCH_Fail
;
194 return DESCRIPTOR_SEARCH_NotFound
;
197 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
199 const uint16_t Length
)
201 USB_ControlRequest
= (USB_Request_Header_t
)
203 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
204 .bRequest
= RNDIS_REQ_SendEncapsulatedCommand
,
206 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
210 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
211 return USB_Host_SendControlRequest(Buffer
);
214 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
216 const uint16_t Length
)
218 USB_ControlRequest
= (USB_Request_Header_t
)
220 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
221 .bRequest
= RNDIS_REQ_GetEncapsulatedResponse
,
223 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
227 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
228 return USB_Host_SendControlRequest(Buffer
);
231 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
235 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
236 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
238 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
239 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
240 KeepAliveMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
242 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &KeepAliveMessage
,
243 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
248 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &KeepAliveMessageResponse
,
249 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
254 return HOST_SENDCONTROL_Successful
;
257 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
261 RNDIS_Initialize_Message_t InitMessage
;
262 RNDIS_Initialize_Complete_t InitMessageResponse
;
264 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
265 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
266 InitMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
268 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
269 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
270 InitMessage
.MaxTransferSize
= RNDISInterfaceInfo
->Config
.HostMaxPacketSize
;
272 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &InitMessage
,
273 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
278 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &InitMessageResponse
,
279 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
284 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
285 return RNDIS_COMMAND_FAILED
;
287 RNDISInterfaceInfo
->State
.DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
289 return HOST_SENDCONTROL_Successful
;
292 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
295 const uint16_t Length
)
301 RNDIS_Set_Message_t SetMessage
;
302 uint8_t ContiguousBuffer
[Length
];
305 RNDIS_Set_Complete_t SetMessageResponse
;
307 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
308 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
309 SetMessageData
.SetMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
311 SetMessageData
.SetMessage
.Oid
= Oid
;
312 SetMessageData
.SetMessage
.InformationBufferLength
= Length
;
313 SetMessageData
.SetMessage
.InformationBufferOffset
= (sizeof(RNDIS_Set_Message_t
) - sizeof(RNDIS_Message_Header_t
));
314 SetMessageData
.SetMessage
.DeviceVcHandle
= 0;
316 memcpy(&SetMessageData
.ContiguousBuffer
, Buffer
, Length
);
318 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &SetMessageData
,
319 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
324 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &SetMessageResponse
,
325 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
330 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
331 return RNDIS_COMMAND_FAILED
;
333 return HOST_SENDCONTROL_Successful
;
336 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
339 const uint16_t MaxLength
)
343 RNDIS_Query_Message_t QueryMessage
;
347 RNDIS_Query_Complete_t QueryMessageResponse
;
348 uint8_t ContiguousBuffer
[MaxLength
];
349 } QueryMessageResponseData
;
351 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
352 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
353 QueryMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
355 QueryMessage
.Oid
= Oid
;
356 QueryMessage
.InformationBufferLength
= 0;
357 QueryMessage
.InformationBufferOffset
= 0;
358 QueryMessage
.DeviceVcHandle
= 0;
360 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &QueryMessage
,
361 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
366 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &QueryMessageResponseData
,
367 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
372 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
373 return RNDIS_COMMAND_FAILED
;
375 memcpy(Buffer
, &QueryMessageResponseData
.ContiguousBuffer
, MaxLength
);
377 return HOST_SENDCONTROL_Successful
;
380 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
384 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
387 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
390 PacketWaiting
= Pipe_IsINReceived();
393 return PacketWaiting
;
396 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
398 uint16_t* const PacketLength
)
402 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
403 return PIPE_READYWAIT_DeviceDisconnected
;
405 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
408 if (!(Pipe_IsReadWriteAllowed()))
410 if (Pipe_IsINReceived())
415 return PIPE_RWSTREAM_NoError
;
418 RNDIS_Packet_Message_t DeviceMessage
;
420 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
421 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
426 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
428 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)),
431 Pipe_Read_Stream_LE(Buffer
, *PacketLength
, NO_STREAM_CALLBACK
);
433 if (!(Pipe_BytesInPipe()))
438 return PIPE_RWSTREAM_NoError
;
441 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
443 const uint16_t PacketLength
)
447 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
448 return PIPE_READYWAIT_DeviceDisconnected
;
450 RNDIS_Packet_Message_t DeviceMessage
;
452 memset(&DeviceMessage
, 0, sizeof(RNDIS_Packet_Message_t
));
453 DeviceMessage
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
454 DeviceMessage
.MessageLength
= (sizeof(RNDIS_Packet_Message_t
) + PacketLength
);
455 DeviceMessage
.DataOffset
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
));
456 DeviceMessage
.DataLength
= PacketLength
;
458 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
);
461 if ((ErrorCode
= Pipe_Write_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
462 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
467 Pipe_Write_Stream_LE(Buffer
, PacketLength
, NO_STREAM_CALLBACK
);
472 return PIPE_RWSTREAM_NoError
;