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
31 #define INCLUDE_FROM_RNDIS_CLASS_C
34 static const uint32_t PROGMEM AdapterSupportedOIDList
[] =
36 OID_GEN_SUPPORTED_LIST
,
37 OID_GEN_PHYSICAL_MEDIUM
,
38 OID_GEN_HARDWARE_STATUS
,
39 OID_GEN_MEDIA_SUPPORTED
,
41 OID_GEN_MAXIMUM_FRAME_SIZE
,
42 OID_GEN_MAXIMUM_TOTAL_SIZE
,
44 OID_GEN_TRANSMIT_BLOCK_SIZE
,
45 OID_GEN_RECEIVE_BLOCK_SIZE
,
47 OID_GEN_VENDOR_DESCRIPTION
,
48 OID_GEN_CURRENT_PACKET_FILTER
,
49 OID_GEN_MAXIMUM_TOTAL_SIZE
,
50 OID_GEN_MEDIA_CONNECT_STATUS
,
55 OID_GEN_RCV_NO_BUFFER
,
56 OID_802_3_PERMANENT_ADDRESS
,
57 OID_802_3_CURRENT_ADDRESS
,
58 OID_802_3_MULTICAST_LIST
,
59 OID_802_3_MAXIMUM_LIST_SIZE
,
60 OID_802_3_RCV_ERROR_ALIGNMENT
,
61 OID_802_3_XMIT_ONE_COLLISION
,
62 OID_802_3_XMIT_MORE_COLLISIONS
,
65 void USB_RNDIS_ProcessControlPacket(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
)
67 if (!(Endpoint_IsSETUPReceived()))
70 if (USB_ControlRequest
.wIndex
!= RNDISInterfaceInfo
->ControlInterfaceNumber
)
73 switch (USB_ControlRequest
.bRequest
)
75 case REQ_SendEncapsulatedCommand
:
76 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
78 Endpoint_ClearSETUP();
80 Endpoint_Read_Control_Stream_LE(RNDISInterfaceInfo
->RNDISMessageBuffer
, USB_ControlRequest
.wLength
);
83 USB_RNDIS_ProcessRNDISControlMessage(RNDISInterfaceInfo
);
87 case REQ_GetEncapsulatedResponse
:
88 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
90 Endpoint_ClearSETUP();
92 RNDIS_Message_Header_t
* MessageHeader
= (RNDIS_Message_Header_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
94 if (!(MessageHeader
->MessageLength
))
96 RNDISInterfaceInfo
->RNDISMessageBuffer
[0] = 0;
97 MessageHeader
->MessageLength
= 1;
100 Endpoint_Write_Control_Stream_LE(RNDISInterfaceInfo
->RNDISMessageBuffer
, MessageHeader
->MessageLength
);
103 MessageHeader
->MessageLength
= 0;
110 bool USB_RNDIS_ConfigureEndpoints(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
)
112 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo
->DataINEndpointNumber
, EP_TYPE_BULK
,
113 ENDPOINT_DIR_IN
, RNDISInterfaceInfo
->DataINEndpointSize
,
114 ENDPOINT_BANK_SINGLE
)))
119 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo
->DataOUTEndpointNumber
, EP_TYPE_BULK
,
120 ENDPOINT_DIR_OUT
, RNDISInterfaceInfo
->DataOUTEndpointSize
,
121 ENDPOINT_BANK_SINGLE
)))
126 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo
->NotificationEndpointNumber
, EP_TYPE_INTERRUPT
,
127 ENDPOINT_DIR_IN
, RNDISInterfaceInfo
->NotificationEndpointSize
,
128 ENDPOINT_BANK_SINGLE
)))
136 void USB_RNDIS_USBTask(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
)
138 if (!(USB_IsConnected
))
141 RNDIS_Message_Header_t
* MessageHeader
= (RNDIS_Message_Header_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
143 Endpoint_SelectEndpoint(RNDISInterfaceInfo
->NotificationEndpointNumber
);
145 if (Endpoint_IsINReady() && RNDISInterfaceInfo
->ResponseReady
)
147 USB_Request_Header_t Notification
= (USB_Request_Header_t
)
149 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
150 .bRequest
= NOTIF_ResponseAvailable
,
156 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
), NO_STREAM_CALLBACK
);
160 RNDISInterfaceInfo
->ResponseReady
= false;
163 if ((RNDISInterfaceInfo
->CurrRNDISState
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
))
165 RNDIS_PACKET_MSG_t RNDISPacketHeader
;
167 Endpoint_SelectEndpoint(RNDISInterfaceInfo
->DataOUTEndpointNumber
);
169 if (Endpoint_IsOUTReceived() && !(RNDISInterfaceInfo
->FrameIN
.FrameInBuffer
))
171 Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
), NO_STREAM_CALLBACK
);
173 if (RNDISPacketHeader
.DataLength
> ETHERNET_FRAME_SIZE_MAX
)
175 Endpoint_StallTransaction();
179 Endpoint_Read_Stream_LE(RNDISInterfaceInfo
->FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
, NO_STREAM_CALLBACK
);
183 RNDISInterfaceInfo
->FrameIN
.FrameLength
= RNDISPacketHeader
.DataLength
;
185 RNDISInterfaceInfo
->FrameIN
.FrameInBuffer
= true;
188 Endpoint_SelectEndpoint(RNDISInterfaceInfo
->DataINEndpointNumber
);
190 if (Endpoint_IsINReady() && RNDISInterfaceInfo
->FrameOUT
.FrameInBuffer
)
192 memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_PACKET_MSG_t
));
194 RNDISPacketHeader
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
195 RNDISPacketHeader
.MessageLength
= (sizeof(RNDIS_PACKET_MSG_t
) + RNDISInterfaceInfo
->FrameOUT
.FrameLength
);
196 RNDISPacketHeader
.DataOffset
= (sizeof(RNDIS_PACKET_MSG_t
) - sizeof(RNDIS_Message_Header_t
));
197 RNDISPacketHeader
.DataLength
= RNDISInterfaceInfo
->FrameOUT
.FrameLength
;
199 Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
), NO_STREAM_CALLBACK
);
200 Endpoint_Write_Stream_LE(RNDISInterfaceInfo
->FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
, NO_STREAM_CALLBACK
);
203 RNDISInterfaceInfo
->FrameOUT
.FrameInBuffer
= false;
208 void USB_RNDIS_ProcessRNDISControlMessage(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
)
210 /* Note: Only a single buffer is used for both the received message and its response to save SRAM. Because of
211 this, response bytes should be filled in order so that they do not clobber unread data in the buffer. */
213 RNDIS_Message_Header_t
* MessageHeader
= (RNDIS_Message_Header_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
215 switch (MessageHeader
->MessageType
)
217 case REMOTE_NDIS_INITIALIZE_MSG
:
218 RNDISInterfaceInfo
->ResponseReady
= true;
220 RNDIS_INITIALIZE_MSG_t
* INITIALIZE_Message
= (RNDIS_INITIALIZE_MSG_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
221 RNDIS_INITIALIZE_CMPLT_t
* INITIALIZE_Response
= (RNDIS_INITIALIZE_CMPLT_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
223 INITIALIZE_Response
->MessageType
= REMOTE_NDIS_INITIALIZE_CMPLT
;
224 INITIALIZE_Response
->MessageLength
= sizeof(RNDIS_INITIALIZE_CMPLT_t
);
225 INITIALIZE_Response
->RequestId
= INITIALIZE_Message
->RequestId
;
226 INITIALIZE_Response
->Status
= REMOTE_NDIS_STATUS_SUCCESS
;
228 INITIALIZE_Response
->MajorVersion
= REMOTE_NDIS_VERSION_MAJOR
;
229 INITIALIZE_Response
->MinorVersion
= REMOTE_NDIS_VERSION_MINOR
;
230 INITIALIZE_Response
->DeviceFlags
= REMOTE_NDIS_DF_CONNECTIONLESS
;
231 INITIALIZE_Response
->Medium
= REMOTE_NDIS_MEDIUM_802_3
;
232 INITIALIZE_Response
->MaxPacketsPerTransfer
= 1;
233 INITIALIZE_Response
->MaxTransferSize
= (sizeof(RNDIS_PACKET_MSG_t
) + ETHERNET_FRAME_SIZE_MAX
);
234 INITIALIZE_Response
->PacketAlignmentFactor
= 0;
235 INITIALIZE_Response
->AFListOffset
= 0;
236 INITIALIZE_Response
->AFListSize
= 0;
238 RNDISInterfaceInfo
->CurrRNDISState
= RNDIS_Initialized
;
241 case REMOTE_NDIS_HALT_MSG
:
242 RNDISInterfaceInfo
->ResponseReady
= false;
243 MessageHeader
->MessageLength
= 0;
245 RNDISInterfaceInfo
->CurrRNDISState
= RNDIS_Uninitialized
;
248 case REMOTE_NDIS_QUERY_MSG
:
249 RNDISInterfaceInfo
->ResponseReady
= true;
251 RNDIS_QUERY_MSG_t
* QUERY_Message
= (RNDIS_QUERY_MSG_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
252 RNDIS_QUERY_CMPLT_t
* QUERY_Response
= (RNDIS_QUERY_CMPLT_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
253 uint32_t Query_Oid
= QUERY_Message
->Oid
;
255 void* QueryData
= &RNDISInterfaceInfo
->RNDISMessageBuffer
[sizeof(RNDIS_Message_Header_t
) +
256 QUERY_Message
->InformationBufferOffset
];
257 void* ResponseData
= &RNDISInterfaceInfo
->RNDISMessageBuffer
[sizeof(RNDIS_QUERY_CMPLT_t
)];
258 uint16_t ResponseSize
;
260 QUERY_Response
->MessageType
= REMOTE_NDIS_QUERY_CMPLT
;
261 QUERY_Response
->MessageLength
= sizeof(RNDIS_QUERY_CMPLT_t
);
263 if (USB_RNDIS_ProcessNDISQuery(RNDISInterfaceInfo
, Query_Oid
, QueryData
, QUERY_Message
->InformationBufferLength
,
264 ResponseData
, &ResponseSize
))
266 QUERY_Response
->Status
= REMOTE_NDIS_STATUS_SUCCESS
;
267 QUERY_Response
->MessageLength
+= ResponseSize
;
269 QUERY_Response
->InformationBufferLength
= ResponseSize
;
270 QUERY_Response
->InformationBufferOffset
= (sizeof(RNDIS_QUERY_CMPLT_t
) - sizeof(RNDIS_Message_Header_t
));
274 QUERY_Response
->Status
= REMOTE_NDIS_STATUS_NOT_SUPPORTED
;
276 QUERY_Response
->InformationBufferLength
= 0;
277 QUERY_Response
->InformationBufferOffset
= 0;
281 case REMOTE_NDIS_SET_MSG
:
282 RNDISInterfaceInfo
->ResponseReady
= true;
284 RNDIS_SET_MSG_t
* SET_Message
= (RNDIS_SET_MSG_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
285 RNDIS_SET_CMPLT_t
* SET_Response
= (RNDIS_SET_CMPLT_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
286 uint32_t SET_Oid
= SET_Message
->Oid
;
288 SET_Response
->MessageType
= REMOTE_NDIS_SET_CMPLT
;
289 SET_Response
->MessageLength
= sizeof(RNDIS_SET_CMPLT_t
);
290 SET_Response
->RequestId
= SET_Message
->RequestId
;
292 void* SetData
= &RNDISInterfaceInfo
->RNDISMessageBuffer
[sizeof(RNDIS_Message_Header_t
) +
293 SET_Message
->InformationBufferOffset
];
295 if (USB_RNDIS_ProcessNDISSet(RNDISInterfaceInfo
, SET_Oid
, SetData
, SET_Message
->InformationBufferLength
))
296 SET_Response
->Status
= REMOTE_NDIS_STATUS_SUCCESS
;
298 SET_Response
->Status
= REMOTE_NDIS_STATUS_NOT_SUPPORTED
;
301 case REMOTE_NDIS_RESET_MSG
:
302 RNDISInterfaceInfo
->ResponseReady
= true;
304 RNDIS_RESET_CMPLT_t
* RESET_Response
= (RNDIS_RESET_CMPLT_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
306 RESET_Response
->MessageType
= REMOTE_NDIS_RESET_CMPLT
;
307 RESET_Response
->MessageLength
= sizeof(RNDIS_RESET_CMPLT_t
);
308 RESET_Response
->Status
= REMOTE_NDIS_STATUS_SUCCESS
;
309 RESET_Response
->AddressingReset
= 0;
312 case REMOTE_NDIS_KEEPALIVE_MSG
:
313 RNDISInterfaceInfo
->ResponseReady
= true;
315 RNDIS_KEEPALIVE_MSG_t
* KEEPALIVE_Message
= (RNDIS_KEEPALIVE_MSG_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
316 RNDIS_KEEPALIVE_CMPLT_t
* KEEPALIVE_Response
= (RNDIS_KEEPALIVE_CMPLT_t
*)&RNDISInterfaceInfo
->RNDISMessageBuffer
;
318 KEEPALIVE_Response
->MessageType
= REMOTE_NDIS_KEEPALIVE_CMPLT
;
319 KEEPALIVE_Response
->MessageLength
= sizeof(RNDIS_KEEPALIVE_CMPLT_t
);
320 KEEPALIVE_Response
->RequestId
= KEEPALIVE_Message
->RequestId
;
321 KEEPALIVE_Response
->Status
= REMOTE_NDIS_STATUS_SUCCESS
;
327 static bool USB_RNDIS_ProcessNDISQuery(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
,
328 uint32_t OId
, void* QueryData
, uint16_t QuerySize
,
329 void* ResponseData
, uint16_t* ResponseSize
)
333 case OID_GEN_SUPPORTED_LIST
:
334 *ResponseSize
= sizeof(AdapterSupportedOIDList
);
336 memcpy_P(ResponseData
, AdapterSupportedOIDList
, sizeof(AdapterSupportedOIDList
));
339 case OID_GEN_PHYSICAL_MEDIUM
:
340 *ResponseSize
= sizeof(uint32_t);
342 /* Indicate that the device is a true ethernet link */
343 *((uint32_t*)ResponseData
) = 0;
346 case OID_GEN_HARDWARE_STATUS
:
347 *ResponseSize
= sizeof(uint32_t);
349 *((uint32_t*)ResponseData
) = NdisHardwareStatusReady
;
352 case OID_GEN_MEDIA_SUPPORTED
:
353 case OID_GEN_MEDIA_IN_USE
:
354 *ResponseSize
= sizeof(uint32_t);
356 *((uint32_t*)ResponseData
) = REMOTE_NDIS_MEDIUM_802_3
;
359 case OID_GEN_VENDOR_ID
:
360 *ResponseSize
= sizeof(uint32_t);
362 /* Vendor ID 0x0xFFFFFF is reserved for vendors who have not purchased a NDIS VID */
363 *((uint32_t*)ResponseData
) = 0x00FFFFFF;
366 case OID_GEN_MAXIMUM_FRAME_SIZE
:
367 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
368 case OID_GEN_RECEIVE_BLOCK_SIZE
:
369 *ResponseSize
= sizeof(uint32_t);
371 *((uint32_t*)ResponseData
) = ETHERNET_FRAME_SIZE_MAX
;
374 case OID_GEN_VENDOR_DESCRIPTION
:
375 *ResponseSize
= (strlen(RNDISInterfaceInfo
->AdapterVendorDescription
) + 1);
377 memcpy(ResponseData
, RNDISInterfaceInfo
->AdapterVendorDescription
, *ResponseSize
);
380 case OID_GEN_MEDIA_CONNECT_STATUS
:
381 *ResponseSize
= sizeof(uint32_t);
383 *((uint32_t*)ResponseData
) = REMOTE_NDIS_MEDIA_STATE_CONNECTED
;
386 case OID_GEN_LINK_SPEED
:
387 *ResponseSize
= sizeof(uint32_t);
389 /* Indicate 10Mb/s link speed */
390 *((uint32_t*)ResponseData
) = 100000;
393 case OID_802_3_PERMANENT_ADDRESS
:
394 case OID_802_3_CURRENT_ADDRESS
:
395 *ResponseSize
= sizeof(MAC_Address_t
);
397 memcpy(ResponseData
, &RNDISInterfaceInfo
->AdapterMACAddress
, sizeof(MAC_Address_t
));
400 case OID_802_3_MAXIMUM_LIST_SIZE
:
401 *ResponseSize
= sizeof(uint32_t);
403 /* Indicate only one multicast address supported */
404 *((uint32_t*)ResponseData
) = 1;
407 case OID_GEN_CURRENT_PACKET_FILTER
:
408 *ResponseSize
= sizeof(uint32_t);
410 *((uint32_t*)ResponseData
) = RNDISInterfaceInfo
->CurrPacketFilter
;
413 case OID_GEN_XMIT_OK
:
415 case OID_GEN_XMIT_ERROR
:
416 case OID_GEN_RCV_ERROR
:
417 case OID_GEN_RCV_NO_BUFFER
:
418 case OID_802_3_RCV_ERROR_ALIGNMENT
:
419 case OID_802_3_XMIT_ONE_COLLISION
:
420 case OID_802_3_XMIT_MORE_COLLISIONS
:
421 *ResponseSize
= sizeof(uint32_t);
423 /* Unused statistic OIDs - always return 0 for each */
424 *((uint32_t*)ResponseData
) = 0;
427 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
428 *ResponseSize
= sizeof(uint32_t);
430 /* Indicate maximum overall buffer (Ethernet frame and RNDIS header) the adapter can handle */
431 *((uint32_t*)ResponseData
) = (RNDIS_MESSAGE_BUFFER_SIZE
+ ETHERNET_FRAME_SIZE_MAX
);
439 static bool USB_RNDIS_ProcessNDISSet(USB_ClassInfo_RNDIS_t
* RNDISInterfaceInfo
, uint32_t OId
, void* SetData
, uint16_t SetSize
)
443 case OID_GEN_CURRENT_PACKET_FILTER
:
444 RNDISInterfaceInfo
->CurrPacketFilter
= *((uint32_t*)SetData
);
445 RNDISInterfaceInfo
->CurrRNDISState
= ((RNDISInterfaceInfo
->CurrPacketFilter
) ?
446 RNDIS_Data_Initialized
: RNDIS_Data_Initialized
);
449 case OID_802_3_MULTICAST_LIST
:
450 /* Do nothing - throw away the value from the host as it is unused */