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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
34 #define INCLUDE_FROM_RNDIS_CLASS_HOST_C
37 uint8_t RNDIS_Host_ConfigurePipes(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
, uint16_t ConfigDescriptorSize
,
38 void* ConfigDescriptorData
)
40 uint8_t FoundEndpoints
= 0;
42 memset(&RNDISInterfaceInfo
->State
, 0x00, sizeof(RNDISInterfaceInfo
->State
));
44 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
45 return RNDIS_ENUMERROR_InvalidConfigDescriptor
;
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
48 DComp_RNDIS_Host_NextRNDISControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
50 return RNDIS_ENUMERROR_NoRNDISInterfaceFound
;
53 RNDISInterfaceInfo
->State
.ControlInterfaceNumber
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
).InterfaceNumber
;
55 while (FoundEndpoints
!= (RNDIS_FOUND_NOTIFICATION_IN
| RNDIS_FOUND_DATAPIPE_IN
| RNDIS_FOUND_DATAPIPE_OUT
))
57 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
58 DComp_RNDIS_Host_NextRNDISInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
60 if (FoundEndpoints
& RNDIS_FOUND_NOTIFICATION_IN
)
62 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
63 DComp_RNDIS_Host_NextRNDISDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
65 return RNDIS_ENUMERROR_NoRNDISInterfaceFound
;
72 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
74 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
);
76 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.NotificationPipeNumber
);
79 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
80 DComp_RNDIS_Host_NextRNDISControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
82 return RNDIS_ENUMERROR_NoRNDISInterfaceFound
;
86 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
87 DComp_RNDIS_Host_NextRNDISInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
89 return RNDIS_ENUMERROR_EndpointsNotFound
;
93 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
95 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
97 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
99 Pipe_ConfigurePipe(RNDISInterfaceInfo
->Config
.NotificationPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
100 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
101 RNDISInterfaceInfo
->Config
.NotificationPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
102 RNDISInterfaceInfo
->State
.NotificationPipeSize
= EndpointData
->EndpointSize
;
104 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
);
106 FoundEndpoints
|= RNDIS_FOUND_NOTIFICATION_IN
;
111 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
113 if (Pipe_IsEndpointBound(EndpointData
->EndpointAddress
))
115 RNDISInterfaceInfo
->State
.BidirectionalDataEndpoints
= true;
119 Pipe_ConfigurePipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
120 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
121 RNDISInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
122 RNDISInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
124 FoundEndpoints
|= RNDIS_FOUND_DATAPIPE_IN
;
128 if (Pipe_IsEndpointBound(EndpointData
->EndpointAddress
))
130 RNDISInterfaceInfo
->State
.BidirectionalDataEndpoints
= true;
134 Pipe_ConfigurePipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
135 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
136 RNDISInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
139 RNDISInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
141 FoundEndpoints
|= RNDIS_FOUND_DATAPIPE_OUT
;
146 RNDISInterfaceInfo
->State
.IsActive
= true;
147 return RNDIS_ENUMERROR_NoError
;
150 static uint8_t DComp_RNDIS_Host_NextRNDISControlInterface(void* const CurrentDescriptor
)
152 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
154 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
155 USB_Descriptor_Interface_t
);
157 if ((CurrentInterface
->Class
== RNDIS_CONTROL_CLASS
) &&
158 (CurrentInterface
->SubClass
== RNDIS_CONTROL_SUBCLASS
) &&
159 (CurrentInterface
->Protocol
== RNDIS_CONTROL_PROTOCOL
))
161 return DESCRIPTOR_SEARCH_Found
;
165 return DESCRIPTOR_SEARCH_NotFound
;
168 static uint8_t DComp_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor
)
170 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
172 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
173 USB_Descriptor_Interface_t
);
175 if ((CurrentInterface
->Class
== RNDIS_DATA_CLASS
) &&
176 (CurrentInterface
->SubClass
== RNDIS_DATA_SUBCLASS
) &&
177 (CurrentInterface
->Protocol
== RNDIS_DATA_PROTOCOL
))
179 return DESCRIPTOR_SEARCH_Found
;
183 return DESCRIPTOR_SEARCH_NotFound
;
186 static uint8_t DComp_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor
)
188 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
190 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
191 USB_Descriptor_Endpoint_t
);
193 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
195 if ((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
))
197 return DESCRIPTOR_SEARCH_Found
;
200 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
202 return DESCRIPTOR_SEARCH_Fail
;
205 return DESCRIPTOR_SEARCH_NotFound
;
208 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
209 void* Buffer
, uint16_t Length
)
211 USB_ControlRequest
= (USB_Request_Header_t
)
213 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
214 .bRequest
= REQ_SendEncapsulatedCommand
,
216 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
220 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
221 return USB_Host_SendControlRequest(Buffer
);
224 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
225 void* Buffer
, uint16_t Length
)
227 USB_ControlRequest
= (USB_Request_Header_t
)
229 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
230 .bRequest
= REQ_GetEncapsulatedResponse
,
232 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
236 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
237 return USB_Host_SendControlRequest(Buffer
);
240 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
244 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
245 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
247 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
248 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
249 KeepAliveMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
251 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &KeepAliveMessage
,
252 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
257 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &KeepAliveMessageResponse
,
258 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
263 return HOST_SENDCONTROL_Successful
;
266 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
270 RNDIS_Initialize_Message_t InitMessage
;
271 RNDIS_Initialize_Complete_t InitMessageResponse
;
273 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
274 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
275 InitMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
277 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
278 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
279 InitMessage
.MaxTransferSize
= RNDISInterfaceInfo
->Config
.HostMaxPacketSize
;
281 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &InitMessage
,
282 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
287 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &InitMessageResponse
,
288 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
293 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
294 return RNDIS_COMMAND_FAILED
;
296 RNDISInterfaceInfo
->State
.DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
298 return HOST_SENDCONTROL_Successful
;
301 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
, uint32_t Oid
, void* Buffer
, uint16_t Length
)
307 RNDIS_Set_Message_t SetMessage
;
308 uint8_t ContigiousBuffer
[Length
];
311 RNDIS_Set_Complete_t SetMessageResponse
;
313 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
314 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
315 SetMessageData
.SetMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
317 SetMessageData
.SetMessage
.Oid
= Oid
;
318 SetMessageData
.SetMessage
.InformationBufferLength
= Length
;
319 SetMessageData
.SetMessage
.InformationBufferOffset
= (sizeof(RNDIS_Set_Message_t
) - sizeof(RNDIS_Message_Header_t
));
320 SetMessageData
.SetMessage
.DeviceVcHandle
= 0;
322 memcpy(&SetMessageData
.ContigiousBuffer
, Buffer
, Length
);
324 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &SetMessageData
,
325 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
330 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &SetMessageResponse
,
331 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
336 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
337 return RNDIS_COMMAND_FAILED
;
339 return HOST_SENDCONTROL_Successful
;
342 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
, uint32_t Oid
, void* Buffer
, uint16_t MaxLength
)
346 RNDIS_Query_Message_t QueryMessage
;
350 RNDIS_Query_Complete_t QueryMessageResponse
;
351 uint8_t ContigiousBuffer
[MaxLength
];
352 } QueryMessageResponseData
;
354 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
355 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
356 QueryMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
358 QueryMessage
.Oid
= Oid
;
359 QueryMessage
.InformationBufferLength
= 0;
360 QueryMessage
.InformationBufferOffset
= 0;
361 QueryMessage
.DeviceVcHandle
= 0;
363 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &QueryMessage
,
364 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
369 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &QueryMessageResponseData
,
370 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
375 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
376 return RNDIS_COMMAND_FAILED
;
378 memcpy(Buffer
, &QueryMessageResponseData
.ContigiousBuffer
, MaxLength
);
380 return HOST_SENDCONTROL_Successful
;
383 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
387 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
390 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
391 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
395 PacketWaiting
= (Pipe_IsINReceived() && Pipe_BytesInPipe());
399 return PacketWaiting
;
402 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
, void* Buffer
, uint16_t* PacketLength
)
406 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
407 return PIPE_READYWAIT_DeviceDisconnected
;
409 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
410 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
413 if (!(Pipe_IsReadWriteAllowed()))
415 if (Pipe_IsINReceived())
420 return PIPE_RWSTREAM_NoError
;
423 RNDIS_Packet_Message_t DeviceMessage
;
425 if (Pipe_BytesInPipe() < sizeof(RNDIS_Packet_Message_t
))
427 printf("*SIZE YARG: %d*\r\n", Pipe_BytesInPipe());
430 return RNDIS_COMMAND_FAILED
;
433 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
434 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
439 if (DeviceMessage
.MessageType
!= REMOTE_NDIS_PACKET_MSG
)
441 printf("****YARG****\r\n");
444 return RNDIS_COMMAND_FAILED
;
447 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
449 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)),
452 Pipe_Read_Stream_LE(Buffer
, *PacketLength
, NO_STREAM_CALLBACK
);
454 if (!(Pipe_BytesInPipe()))
459 return PIPE_RWSTREAM_NoError
;
462 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
, void* Buffer
, uint16_t PacketLength
)
466 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
467 return PIPE_READYWAIT_DeviceDisconnected
;
469 if (RNDISInterfaceInfo
->State
.BidirectionalDataEndpoints
)
471 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
472 Pipe_SetPipeToken(PIPE_TOKEN_OUT
);
476 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
);
479 RNDIS_Packet_Message_t DeviceMessage
;
481 memset(&DeviceMessage
, 0, sizeof(RNDIS_Packet_Message_t
));
482 DeviceMessage
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
483 DeviceMessage
.MessageLength
= (sizeof(RNDIS_Packet_Message_t
) + PacketLength
);
484 DeviceMessage
.DataOffset
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
));
485 DeviceMessage
.DataLength
= PacketLength
;
489 if ((ErrorCode
= Pipe_Write_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
490 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
492 if (RNDISInterfaceInfo
->State
.BidirectionalDataEndpoints
)
493 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
498 Pipe_Write_Stream_LE(Buffer
, PacketLength
, NO_STREAM_CALLBACK
);
503 if (RNDISInterfaceInfo
->State
.BidirectionalDataEndpoints
)
504 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
506 return PIPE_RWSTREAM_NoError
;