Minor documentation page updates.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / RNDIS.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #define INCLUDE_FROM_RNDIS_CLASS_C
32 #include "RNDIS.h"
33
34 static const uint32_t PROGMEM AdapterSupportedOIDList[] =
35 {
36 OID_GEN_SUPPORTED_LIST,
37 OID_GEN_PHYSICAL_MEDIUM,
38 OID_GEN_HARDWARE_STATUS,
39 OID_GEN_MEDIA_SUPPORTED,
40 OID_GEN_MEDIA_IN_USE,
41 OID_GEN_MAXIMUM_FRAME_SIZE,
42 OID_GEN_MAXIMUM_TOTAL_SIZE,
43 OID_GEN_LINK_SPEED,
44 OID_GEN_TRANSMIT_BLOCK_SIZE,
45 OID_GEN_RECEIVE_BLOCK_SIZE,
46 OID_GEN_VENDOR_ID,
47 OID_GEN_VENDOR_DESCRIPTION,
48 OID_GEN_CURRENT_PACKET_FILTER,
49 OID_GEN_MAXIMUM_TOTAL_SIZE,
50 OID_GEN_MEDIA_CONNECT_STATUS,
51 OID_GEN_XMIT_OK,
52 OID_GEN_RCV_OK,
53 OID_GEN_XMIT_ERROR,
54 OID_GEN_RCV_ERROR,
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,
63 };
64
65 void USB_RNDIS_ProcessControlPacket(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo)
66 {
67 if (!(Endpoint_IsSETUPReceived()))
68 return;
69
70 if (USB_ControlRequest.wIndex != RNDISInterfaceInfo->ControlInterfaceNumber)
71 return;
72
73 switch (USB_ControlRequest.bRequest)
74 {
75 case REQ_SendEncapsulatedCommand:
76 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
77 {
78 Endpoint_ClearSETUP();
79
80 Endpoint_Read_Control_Stream_LE(RNDISInterfaceInfo->RNDISMessageBuffer, USB_ControlRequest.wLength);
81 Endpoint_ClearIN();
82
83 USB_RNDIS_ProcessRNDISControlMessage(RNDISInterfaceInfo);
84 }
85
86 break;
87 case REQ_GetEncapsulatedResponse:
88 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
89 {
90 Endpoint_ClearSETUP();
91
92 RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->RNDISMessageBuffer;
93
94 if (!(MessageHeader->MessageLength))
95 {
96 RNDISInterfaceInfo->RNDISMessageBuffer[0] = 0;
97 MessageHeader->MessageLength = 1;
98 }
99
100 Endpoint_Write_Control_Stream_LE(RNDISInterfaceInfo->RNDISMessageBuffer, MessageHeader->MessageLength);
101 Endpoint_ClearOUT();
102
103 MessageHeader->MessageLength = 0;
104 }
105
106 break;
107 }
108 }
109
110 bool USB_RNDIS_ConfigureEndpoints(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo)
111 {
112 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK,
113 ENDPOINT_DIR_IN, RNDISInterfaceInfo->DataINEndpointSize,
114 ENDPOINT_BANK_SINGLE)))
115 {
116 return false;
117 }
118
119 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK,
120 ENDPOINT_DIR_OUT, RNDISInterfaceInfo->DataOUTEndpointSize,
121 ENDPOINT_BANK_SINGLE)))
122 {
123 return false;
124 }
125
126 if (!(Endpoint_ConfigureEndpoint(RNDISInterfaceInfo->NotificationEndpointNumber, EP_TYPE_INTERRUPT,
127 ENDPOINT_DIR_IN, RNDISInterfaceInfo->NotificationEndpointSize,
128 ENDPOINT_BANK_SINGLE)))
129 {
130 return false;
131 }
132
133 return true;
134 }
135
136 void USB_RNDIS_USBTask(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo)
137 {
138 if (!(USB_IsConnected))
139 return;
140
141 RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->RNDISMessageBuffer;
142
143 Endpoint_SelectEndpoint(RNDISInterfaceInfo->NotificationEndpointNumber);
144
145 if (Endpoint_IsINReady() && RNDISInterfaceInfo->ResponseReady)
146 {
147 USB_Request_Header_t Notification = (USB_Request_Header_t)
148 {
149 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
150 .bRequest = NOTIF_ResponseAvailable,
151 .wValue = 0,
152 .wIndex = 0,
153 .wLength = 0,
154 };
155
156 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NO_STREAM_CALLBACK);
157
158 Endpoint_ClearIN();
159
160 RNDISInterfaceInfo->ResponseReady = false;
161 }
162
163 if ((RNDISInterfaceInfo->CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
164 {
165 RNDIS_PACKET_MSG_t RNDISPacketHeader;
166
167 Endpoint_SelectEndpoint(RNDISInterfaceInfo->DataOUTEndpointNumber);
168
169 if (Endpoint_IsOUTReceived() && !(RNDISInterfaceInfo->FrameIN.FrameInBuffer))
170 {
171 Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_PACKET_MSG_t), NO_STREAM_CALLBACK);
172
173 if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
174 {
175 Endpoint_StallTransaction();
176 return;
177 }
178
179 Endpoint_Read_Stream_LE(RNDISInterfaceInfo->FrameIN.FrameData, RNDISPacketHeader.DataLength, NO_STREAM_CALLBACK);
180
181 Endpoint_ClearOUT();
182
183 RNDISInterfaceInfo->FrameIN.FrameLength = RNDISPacketHeader.DataLength;
184
185 RNDISInterfaceInfo->FrameIN.FrameInBuffer = true;
186 }
187
188 Endpoint_SelectEndpoint(RNDISInterfaceInfo->DataINEndpointNumber);
189
190 if (Endpoint_IsINReady() && RNDISInterfaceInfo->FrameOUT.FrameInBuffer)
191 {
192 memset(&RNDISPacketHeader, 0, sizeof(RNDIS_PACKET_MSG_t));
193
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;
198
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);
201 Endpoint_ClearIN();
202
203 RNDISInterfaceInfo->FrameOUT.FrameInBuffer = false;
204 }
205 }
206 }
207
208 void USB_RNDIS_ProcessRNDISControlMessage(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo)
209 {
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. */
212
213 RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->RNDISMessageBuffer;
214
215 switch (MessageHeader->MessageType)
216 {
217 case REMOTE_NDIS_INITIALIZE_MSG:
218 RNDISInterfaceInfo->ResponseReady = true;
219
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;
222
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;
227
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;
237
238 RNDISInterfaceInfo->CurrRNDISState = RNDIS_Initialized;
239
240 break;
241 case REMOTE_NDIS_HALT_MSG:
242 RNDISInterfaceInfo->ResponseReady = false;
243 MessageHeader->MessageLength = 0;
244
245 RNDISInterfaceInfo->CurrRNDISState = RNDIS_Uninitialized;
246
247 break;
248 case REMOTE_NDIS_QUERY_MSG:
249 RNDISInterfaceInfo->ResponseReady = true;
250
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;
254
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;
259
260 QUERY_Response->MessageType = REMOTE_NDIS_QUERY_CMPLT;
261 QUERY_Response->MessageLength = sizeof(RNDIS_QUERY_CMPLT_t);
262
263 if (USB_RNDIS_ProcessNDISQuery(RNDISInterfaceInfo, Query_Oid, QueryData, QUERY_Message->InformationBufferLength,
264 ResponseData, &ResponseSize))
265 {
266 QUERY_Response->Status = REMOTE_NDIS_STATUS_SUCCESS;
267 QUERY_Response->MessageLength += ResponseSize;
268
269 QUERY_Response->InformationBufferLength = ResponseSize;
270 QUERY_Response->InformationBufferOffset = (sizeof(RNDIS_QUERY_CMPLT_t) - sizeof(RNDIS_Message_Header_t));
271 }
272 else
273 {
274 QUERY_Response->Status = REMOTE_NDIS_STATUS_NOT_SUPPORTED;
275
276 QUERY_Response->InformationBufferLength = 0;
277 QUERY_Response->InformationBufferOffset = 0;
278 }
279
280 break;
281 case REMOTE_NDIS_SET_MSG:
282 RNDISInterfaceInfo->ResponseReady = true;
283
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;
287
288 SET_Response->MessageType = REMOTE_NDIS_SET_CMPLT;
289 SET_Response->MessageLength = sizeof(RNDIS_SET_CMPLT_t);
290 SET_Response->RequestId = SET_Message->RequestId;
291
292 void* SetData = &RNDISInterfaceInfo->RNDISMessageBuffer[sizeof(RNDIS_Message_Header_t) +
293 SET_Message->InformationBufferOffset];
294
295 if (USB_RNDIS_ProcessNDISSet(RNDISInterfaceInfo, SET_Oid, SetData, SET_Message->InformationBufferLength))
296 SET_Response->Status = REMOTE_NDIS_STATUS_SUCCESS;
297 else
298 SET_Response->Status = REMOTE_NDIS_STATUS_NOT_SUPPORTED;
299
300 break;
301 case REMOTE_NDIS_RESET_MSG:
302 RNDISInterfaceInfo->ResponseReady = true;
303
304 RNDIS_RESET_CMPLT_t* RESET_Response = (RNDIS_RESET_CMPLT_t*)&RNDISInterfaceInfo->RNDISMessageBuffer;
305
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;
310
311 break;
312 case REMOTE_NDIS_KEEPALIVE_MSG:
313 RNDISInterfaceInfo->ResponseReady = true;
314
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;
317
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;
322
323 break;
324 }
325 }
326
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)
330 {
331 switch (OId)
332 {
333 case OID_GEN_SUPPORTED_LIST:
334 *ResponseSize = sizeof(AdapterSupportedOIDList);
335
336 memcpy_P(ResponseData, AdapterSupportedOIDList, sizeof(AdapterSupportedOIDList));
337
338 return true;
339 case OID_GEN_PHYSICAL_MEDIUM:
340 *ResponseSize = sizeof(uint32_t);
341
342 /* Indicate that the device is a true ethernet link */
343 *((uint32_t*)ResponseData) = 0;
344
345 return true;
346 case OID_GEN_HARDWARE_STATUS:
347 *ResponseSize = sizeof(uint32_t);
348
349 *((uint32_t*)ResponseData) = NdisHardwareStatusReady;
350
351 return true;
352 case OID_GEN_MEDIA_SUPPORTED:
353 case OID_GEN_MEDIA_IN_USE:
354 *ResponseSize = sizeof(uint32_t);
355
356 *((uint32_t*)ResponseData) = REMOTE_NDIS_MEDIUM_802_3;
357
358 return true;
359 case OID_GEN_VENDOR_ID:
360 *ResponseSize = sizeof(uint32_t);
361
362 /* Vendor ID 0x0xFFFFFF is reserved for vendors who have not purchased a NDIS VID */
363 *((uint32_t*)ResponseData) = 0x00FFFFFF;
364
365 return true;
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);
370
371 *((uint32_t*)ResponseData) = ETHERNET_FRAME_SIZE_MAX;
372
373 return true;
374 case OID_GEN_VENDOR_DESCRIPTION:
375 *ResponseSize = (strlen(RNDISInterfaceInfo->AdapterVendorDescription) + 1);
376
377 memcpy(ResponseData, RNDISInterfaceInfo->AdapterVendorDescription, *ResponseSize);
378
379 return true;
380 case OID_GEN_MEDIA_CONNECT_STATUS:
381 *ResponseSize = sizeof(uint32_t);
382
383 *((uint32_t*)ResponseData) = REMOTE_NDIS_MEDIA_STATE_CONNECTED;
384
385 return true;
386 case OID_GEN_LINK_SPEED:
387 *ResponseSize = sizeof(uint32_t);
388
389 /* Indicate 10Mb/s link speed */
390 *((uint32_t*)ResponseData) = 100000;
391
392 return true;
393 case OID_802_3_PERMANENT_ADDRESS:
394 case OID_802_3_CURRENT_ADDRESS:
395 *ResponseSize = sizeof(MAC_Address_t);
396
397 memcpy(ResponseData, &RNDISInterfaceInfo->AdapterMACAddress, sizeof(MAC_Address_t));
398
399 return true;
400 case OID_802_3_MAXIMUM_LIST_SIZE:
401 *ResponseSize = sizeof(uint32_t);
402
403 /* Indicate only one multicast address supported */
404 *((uint32_t*)ResponseData) = 1;
405
406 return true;
407 case OID_GEN_CURRENT_PACKET_FILTER:
408 *ResponseSize = sizeof(uint32_t);
409
410 *((uint32_t*)ResponseData) = RNDISInterfaceInfo->CurrPacketFilter;
411
412 return true;
413 case OID_GEN_XMIT_OK:
414 case OID_GEN_RCV_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);
422
423 /* Unused statistic OIDs - always return 0 for each */
424 *((uint32_t*)ResponseData) = 0;
425
426 return true;
427 case OID_GEN_MAXIMUM_TOTAL_SIZE:
428 *ResponseSize = sizeof(uint32_t);
429
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);
432
433 return true;
434 default:
435 return false;
436 }
437 }
438
439 static bool USB_RNDIS_ProcessNDISSet(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo, uint32_t OId, void* SetData, uint16_t SetSize)
440 {
441 switch (OId)
442 {
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);
447
448 return true;
449 case OID_802_3_MULTICAST_LIST:
450 /* Do nothing - throw away the value from the host as it is unused */
451
452 return true;
453 default:
454 return false;
455 }
456 }