3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] 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_DRIVER
36 #define __INCLUDE_FROM_RNDIS_HOST_C
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 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
142 if (Header
->Type
== DTYPE_Interface
)
144 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
146 if ((Interface
->Class
== CDC_CSCP_CDCClass
) &&
147 (Interface
->SubClass
== CDC_CSCP_ACMSubclass
) &&
148 (Interface
->Protocol
== CDC_CSCP_VendorSpecificProtocol
))
150 return DESCRIPTOR_SEARCH_Found
;
154 return DESCRIPTOR_SEARCH_NotFound
;
157 static uint8_t DCOMP_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor
)
159 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
161 if (Header
->Type
== DTYPE_Interface
)
163 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
164 USB_Descriptor_Interface_t
);
166 if ((Interface
->Class
== CDC_CSCP_CDCDataClass
) &&
167 (Interface
->SubClass
== CDC_CSCP_NoDataSubclass
) &&
168 (Interface
->Protocol
== CDC_CSCP_NoDataProtocol
))
170 return DESCRIPTOR_SEARCH_Found
;
174 return DESCRIPTOR_SEARCH_NotFound
;
177 static uint8_t DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor
)
179 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
181 if (Header
->Type
== DTYPE_Endpoint
)
183 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
185 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
187 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
188 !(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
)))
190 return DESCRIPTOR_SEARCH_Found
;
193 else if (Header
->Type
== DTYPE_Interface
)
195 return DESCRIPTOR_SEARCH_Fail
;
198 return DESCRIPTOR_SEARCH_NotFound
;
201 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
203 const uint16_t Length
)
205 USB_ControlRequest
= (USB_Request_Header_t
)
207 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
208 .bRequest
= RNDIS_REQ_SendEncapsulatedCommand
,
210 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
214 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
215 return USB_Host_SendControlRequest(Buffer
);
218 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
220 const uint16_t Length
)
222 USB_ControlRequest
= (USB_Request_Header_t
)
224 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
225 .bRequest
= RNDIS_REQ_GetEncapsulatedResponse
,
227 .wIndex
= RNDISInterfaceInfo
->State
.ControlInterfaceNumber
,
231 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
232 return USB_Host_SendControlRequest(Buffer
);
235 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
239 RNDIS_KeepAlive_Message_t KeepAliveMessage
;
240 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse
;
242 KeepAliveMessage
.MessageType
= REMOTE_NDIS_KEEPALIVE_MSG
;
243 KeepAliveMessage
.MessageLength
= sizeof(RNDIS_KeepAlive_Message_t
);
244 KeepAliveMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
246 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &KeepAliveMessage
,
247 sizeof(RNDIS_KeepAlive_Message_t
))) != HOST_SENDCONTROL_Successful
)
252 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &KeepAliveMessageResponse
,
253 sizeof(RNDIS_KeepAlive_Complete_t
))) != HOST_SENDCONTROL_Successful
)
258 return HOST_SENDCONTROL_Successful
;
261 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
265 RNDIS_Initialize_Message_t InitMessage
;
266 RNDIS_Initialize_Complete_t InitMessageResponse
;
268 InitMessage
.MessageType
= REMOTE_NDIS_INITIALIZE_MSG
;
269 InitMessage
.MessageLength
= sizeof(RNDIS_Initialize_Message_t
);
270 InitMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
272 InitMessage
.MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
273 InitMessage
.MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
274 InitMessage
.MaxTransferSize
= RNDISInterfaceInfo
->Config
.HostMaxPacketSize
;
276 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &InitMessage
,
277 sizeof(RNDIS_Initialize_Message_t
))) != HOST_SENDCONTROL_Successful
)
282 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &InitMessageResponse
,
283 sizeof(RNDIS_Initialize_Complete_t
))) != HOST_SENDCONTROL_Successful
)
288 if (InitMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
289 return RNDIS_COMMAND_FAILED
;
291 RNDISInterfaceInfo
->State
.DeviceMaxPacketSize
= InitMessageResponse
.MaxTransferSize
;
293 return HOST_SENDCONTROL_Successful
;
296 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
299 const uint16_t Length
)
305 RNDIS_Set_Message_t SetMessage
;
306 uint8_t ContiguousBuffer
[Length
];
309 RNDIS_Set_Complete_t SetMessageResponse
;
311 SetMessageData
.SetMessage
.MessageType
= REMOTE_NDIS_SET_MSG
;
312 SetMessageData
.SetMessage
.MessageLength
= sizeof(RNDIS_Set_Message_t
) + Length
;
313 SetMessageData
.SetMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
315 SetMessageData
.SetMessage
.Oid
= Oid
;
316 SetMessageData
.SetMessage
.InformationBufferLength
= Length
;
317 SetMessageData
.SetMessage
.InformationBufferOffset
= (sizeof(RNDIS_Set_Message_t
) - sizeof(RNDIS_Message_Header_t
));
318 SetMessageData
.SetMessage
.DeviceVcHandle
= 0;
320 memcpy(&SetMessageData
.ContiguousBuffer
, Buffer
, Length
);
322 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &SetMessageData
,
323 SetMessageData
.SetMessage
.MessageLength
)) != HOST_SENDCONTROL_Successful
)
328 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &SetMessageResponse
,
329 sizeof(RNDIS_Set_Complete_t
))) != HOST_SENDCONTROL_Successful
)
334 if (SetMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
335 return RNDIS_COMMAND_FAILED
;
337 return HOST_SENDCONTROL_Successful
;
340 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
343 const uint16_t MaxLength
)
347 RNDIS_Query_Message_t QueryMessage
;
351 RNDIS_Query_Complete_t QueryMessageResponse
;
352 uint8_t ContiguousBuffer
[MaxLength
];
353 } QueryMessageResponseData
;
355 QueryMessage
.MessageType
= REMOTE_NDIS_QUERY_MSG
;
356 QueryMessage
.MessageLength
= sizeof(RNDIS_Query_Message_t
);
357 QueryMessage
.RequestId
= RNDISInterfaceInfo
->State
.RequestID
++;
359 QueryMessage
.Oid
= Oid
;
360 QueryMessage
.InformationBufferLength
= 0;
361 QueryMessage
.InformationBufferOffset
= 0;
362 QueryMessage
.DeviceVcHandle
= 0;
364 if ((ErrorCode
= RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo
, &QueryMessage
,
365 sizeof(RNDIS_Query_Message_t
))) != HOST_SENDCONTROL_Successful
)
370 if ((ErrorCode
= RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo
, &QueryMessageResponseData
,
371 sizeof(QueryMessageResponseData
))) != HOST_SENDCONTROL_Successful
)
376 if (QueryMessageResponseData
.QueryMessageResponse
.Status
!= REMOTE_NDIS_STATUS_SUCCESS
)
377 return RNDIS_COMMAND_FAILED
;
379 memcpy(Buffer
, &QueryMessageResponseData
.ContiguousBuffer
, MaxLength
);
381 return HOST_SENDCONTROL_Successful
;
384 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
)
388 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
391 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
394 PacketWaiting
= Pipe_IsINReceived();
397 return PacketWaiting
;
400 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
402 uint16_t* const PacketLength
)
406 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
407 return PIPE_READYWAIT_DeviceDisconnected
;
409 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataINPipeNumber
);
412 if (!(Pipe_IsReadWriteAllowed()))
414 if (Pipe_IsINReceived())
419 return PIPE_RWSTREAM_NoError
;
422 RNDIS_Packet_Message_t DeviceMessage
;
424 if ((ErrorCode
= Pipe_Read_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
425 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
430 *PacketLength
= (uint16_t)DeviceMessage
.DataLength
;
432 Pipe_Discard_Stream(DeviceMessage
.DataOffset
- (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)),
435 Pipe_Read_Stream_LE(Buffer
, *PacketLength
, NO_STREAM_CALLBACK
);
437 if (!(Pipe_BytesInPipe()))
442 return PIPE_RWSTREAM_NoError
;
445 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t
* const RNDISInterfaceInfo
,
447 const uint16_t PacketLength
)
451 if ((USB_HostState
!= HOST_STATE_Configured
) || !(RNDISInterfaceInfo
->State
.IsActive
))
452 return PIPE_READYWAIT_DeviceDisconnected
;
454 RNDIS_Packet_Message_t DeviceMessage
;
456 memset(&DeviceMessage
, 0, sizeof(RNDIS_Packet_Message_t
));
457 DeviceMessage
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
458 DeviceMessage
.MessageLength
= (sizeof(RNDIS_Packet_Message_t
) + PacketLength
);
459 DeviceMessage
.DataOffset
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
));
460 DeviceMessage
.DataLength
= PacketLength
;
462 Pipe_SelectPipe(RNDISInterfaceInfo
->Config
.DataOUTPipeNumber
);
465 if ((ErrorCode
= Pipe_Write_Stream_LE(&DeviceMessage
, sizeof(RNDIS_Packet_Message_t
),
466 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
471 Pipe_Write_Stream_LE(Buffer
, PacketLength
, NO_STREAM_CALLBACK
);
476 return PIPE_RWSTREAM_NoError
;