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
;
47 memset(&RNDISInterfaceInfo
->State
, 0x00, sizeof(RNDISInterfaceInfo
->State
));
49 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
50 return RNDIS_ENUMERROR_InvalidConfigDescriptor
;
52 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
53 DCOMP_RNDIS_Host_NextRNDISControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
55 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound
;
58 RNDISInterfaceInfo
->State
.ControlInterfaceNumber
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
59 USB_Descriptor_Interface_t
)->InterfaceNumber
;
61 while (!(DataINEndpoint
) || !(DataOUTEndpoint
) || !(NotificationEndpoint
))
63 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
64 DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 if (NotificationEndpoint
)
68 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
69 DCOMP_RNDIS_Host_NextRNDISDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
71 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound
;
74 DataINEndpoint
= NULL
;
75 DataOUTEndpoint
= NULL
;
79 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
80 DCOMP_RNDIS_Host_NextRNDISControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
82 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound
;
85 RNDISInterfaceInfo
->State
.ControlInterfaceNumber
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
86 USB_Descriptor_Interface_t
)->InterfaceNumber
;
88 NotificationEndpoint
= NULL
;
94 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
96 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
98 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
99 NotificationEndpoint
= EndpointData
;
101 DataINEndpoint
= EndpointData
;
105 DataOUTEndpoint
= EndpointData
;
109 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
111 if (PipeNum
== RNDISInterfaceInfo
->Config
.DataINPipeNumber
)
113 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
114 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
115 RNDISInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
117 RNDISInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
119 else if (PipeNum
== RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
)
121 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
122 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
123 RNDISInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
125 RNDISInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
127 else if (PipeNum
== RNDISInterfaceInfo
->Config
.NotificationPipeNumber
)
129 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
130 NotificationEndpoint
->EndpointAddress
, NotificationEndpoint
->EndpointSize
,
131 RNDISInterfaceInfo
->Config
.NotificationPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
132 Pipe_SetInterruptPeriod(NotificationEndpoint
->PollingIntervalMS
);
134 RNDISInterfaceInfo
->State
.NotificationPipeSize
= NotificationEndpoint
->EndpointSize
;
138 RNDISInterfaceInfo
->State
.IsActive
= true;
140 return RNDIS_ENUMERROR_NoError
;
143 static uint8_t DCOMP_RNDIS_Host_NextRNDISControlInterface(void* const CurrentDescriptor
)
145 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
147 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
148 USB_Descriptor_Interface_t
);
150 if ((CurrentInterface
->Class
== RNDIS_CONTROL_CLASS
) &&
151 (CurrentInterface
->SubClass
== RNDIS_CONTROL_SUBCLASS
) &&
152 (CurrentInterface
->Protocol
== RNDIS_CONTROL_PROTOCOL
))
154 return DESCRIPTOR_SEARCH_Found
;
158 return DESCRIPTOR_SEARCH_NotFound
;
161 static uint8_t DCOMP_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor
)
163 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
165 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
166 USB_Descriptor_Interface_t
);
168 if ((CurrentInterface
->Class
== RNDIS_DATA_CLASS
) &&
169 (CurrentInterface
->SubClass
== RNDIS_DATA_SUBCLASS
) &&
170 (CurrentInterface
->Protocol
== RNDIS_DATA_PROTOCOL
))
172 return DESCRIPTOR_SEARCH_Found
;
176 return DESCRIPTOR_SEARCH_NotFound
;
179 static uint8_t DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor
)
181 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
183 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
184 USB_Descriptor_Endpoint_t
);
186 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
188 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
189 !(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
191 return DESCRIPTOR_SEARCH_Found
;
194 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
196 return DESCRIPTOR_SEARCH_Fail
;
199 return DESCRIPTOR_SEARCH_NotFound
;
202 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
204 const uint16_t Length
)
206 USB_ControlRequest
= (USB_Request_Header_t
)
208 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
209 .bRequest
= RNDIS_REQ_SendEncapsulatedCommand
,
211 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
215 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
216 return USB_Host_SendControlRequest(Buffer
);
219 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
221 const uint16_t Length
)
223 USB_ControlRequest
= (USB_Request_Header_t
)
225 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
226 .bRequest
= RNDIS_REQ_GetEncapsulatedResponse
,
228 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
232 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
233 return USB_Host_SendControlRequest(Buffer
);
236 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
240 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
241 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
243 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
244 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
245 KeepAliveMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
247 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &KeepAliveMessage
,
248 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
253 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &KeepAliveMessageResponse
,
254 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
259 return HOST_SENDCONTROL_Successful
;
262 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
266 RNDIS_Initialize_Message_t InitMessage
;
267 RNDIS_Initialize_Complete_t InitMessageResponse
;
269 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
270 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
271 InitMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
273 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
274 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
275 InitMessage
.MaxTransferSize
= RNDISInterfaceInfo
->Config
.HostMaxPacketSize
;
277 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &InitMessage
,
278 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
283 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &InitMessageResponse
,
284 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
289 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
290 return RNDIS_COMMAND_FAILED
;
292 RNDISInterfaceInfo
->State
.DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
294 return HOST_SENDCONTROL_Successful
;
297 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
300 const uint16_t Length
)
306 RNDIS_Set_Message_t SetMessage
;
307 uint8_t ContiguousBuffer
[Length
];
310 RNDIS_Set_Complete_t SetMessageResponse
;
312 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
313 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
314 SetMessageData
.SetMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
316 SetMessageData
.SetMessage
.Oid
= Oid
;
317 SetMessageData
.SetMessage
.InformationBufferLength
= Length
;
318 SetMessageData
.SetMessage
.InformationBufferOffset
= (sizeof(RNDIS_Set_Message_t
) - sizeof(RNDIS_Message_Header_t
));
319 SetMessageData
.SetMessage
.DeviceVcHandle
= 0;
321 memcpy(&SetMessageData
.ContiguousBuffer
, Buffer
, Length
);
323 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &SetMessageData
,
324 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
329 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &SetMessageResponse
,
330 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
335 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
336 return RNDIS_COMMAND_FAILED
;
338 return HOST_SENDCONTROL_Successful
;
341 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
344 const uint16_t MaxLength
)
348 RNDIS_Query_Message_t QueryMessage
;
352 RNDIS_Query_Complete_t QueryMessageResponse
;
353 uint8_t ContiguousBuffer
[MaxLength
];
354 } QueryMessageResponseData
;
356 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
357 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
358 QueryMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
360 QueryMessage
.Oid
= Oid
;
361 QueryMessage
.InformationBufferLength
= 0;
362 QueryMessage
.InformationBufferOffset
= 0;
363 QueryMessage
.DeviceVcHandle
= 0;
365 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &QueryMessage
,
366 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
371 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &QueryMessageResponseData
,
372 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
377 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
378 return RNDIS_COMMAND_FAILED
;
380 memcpy(Buffer
, &QueryMessageResponseData
.ContiguousBuffer
, MaxLength
);
382 return HOST_SENDCONTROL_Successful
;
385 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
389 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
392 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
395 PacketWaiting
= Pipe_IsINReceived();
398 return PacketWaiting
;
401 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
403 uint16_t* const PacketLength
)
407 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
408 return PIPE_READYWAIT_DeviceDisconnected
;
410 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
413 if (!(Pipe_IsReadWriteAllowed()))
415 if (Pipe_IsINReceived())
420 return PIPE_RWSTREAM_NoError
;
423 RNDIS_Packet_Message_t DeviceMessage
;
425 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
426 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
431 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
433 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)),
436 Pipe_Read_Stream_LE(Buffer
, *PacketLength
, NO_STREAM_CALLBACK
);
438 if (!(Pipe_BytesInPipe()))
443 return PIPE_RWSTREAM_NoError
;
446 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
448 const uint16_t PacketLength
)
452 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
453 return PIPE_READYWAIT_DeviceDisconnected
;
455 RNDIS_Packet_Message_t DeviceMessage
;
457 memset(&DeviceMessage
, 0, sizeof(RNDIS_Packet_Message_t
));
458 DeviceMessage
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
459 DeviceMessage
.MessageLength
= (sizeof(RNDIS_Packet_Message_t
) + PacketLength
);
460 DeviceMessage
.DataOffset
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
));
461 DeviceMessage
.DataLength
= PacketLength
;
463 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
);
466 if ((ErrorCode
= Pipe_Write_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
467 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
472 Pipe_Write_Stream_LE(Buffer
, PacketLength
, NO_STREAM_CALLBACK
);
477 return PIPE_RWSTREAM_NoError
;