Add structure padding to the appropriate descriptor structures to maintain correct...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / RNDIS.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_RNDIS_DRIVER
37 #define __INCLUDE_FROM_RNDIS_HOST_C
38 #include "RNDIS.h"
39
40 uint8_t RNDIS_Host_ConfigurePipes(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
41 uint16_t ConfigDescriptorSize,
42 void* ConfigDescriptorData)
43 {
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
46 USB_Descriptor_Endpoint_t* NotificationEndpoint = NULL;
47 USB_Descriptor_Interface_t* RNDISControlInterface = NULL;
48
49 memset(&RNDISInterfaceInfo->State, 0x00, sizeof(RNDISInterfaceInfo->State));
50
51 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
52 return RNDIS_ENUMERROR_InvalidConfigDescriptor;
53
54 RNDISControlInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
55
56 while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(NotificationEndpoint))
57 {
58 if (!(RNDISControlInterface) ||
59 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
60 DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
61 {
62 if (NotificationEndpoint)
63 {
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
65 DCOMP_RNDIS_Host_NextRNDISDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
66 {
67 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound;
68 }
69
70 DataINEndpoint = NULL;
71 DataOUTEndpoint = NULL;
72 }
73 else
74 {
75 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
76 DCOMP_RNDIS_Host_NextRNDISControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
77 {
78 return RNDIS_ENUMERROR_NoCompatibleInterfaceFound;
79 }
80
81 RNDISControlInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
82
83 NotificationEndpoint = NULL;
84 }
85
86 continue;
87 }
88
89 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
90
91 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
92 {
93 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
94 NotificationEndpoint = EndpointData;
95 else
96 DataINEndpoint = EndpointData;
97 }
98 else
99 {
100 DataOUTEndpoint = EndpointData;
101 }
102 }
103
104 for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
105 {
106 uint16_t Size;
107 uint8_t Type;
108 uint8_t Token;
109 uint8_t EndpointAddress;
110 uint8_t InterruptPeriod;
111 bool DoubleBanked;
112
113 if (PipeNum == RNDISInterfaceInfo->Config.DataINPipeNumber)
114 {
115 Size = DataINEndpoint->EndpointSize;
116 EndpointAddress = DataINEndpoint->EndpointAddress;
117 Token = PIPE_TOKEN_IN;
118 Type = EP_TYPE_BULK;
119 DoubleBanked = RNDISInterfaceInfo->Config.DataINPipeDoubleBank;
120 InterruptPeriod = 0;
121
122 RNDISInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
123 }
124 else if (PipeNum == RNDISInterfaceInfo->Config.DataOUTPipeNumber)
125 {
126 Size = DataOUTEndpoint->EndpointSize;
127 EndpointAddress = DataOUTEndpoint->EndpointAddress;
128 Token = PIPE_TOKEN_OUT;
129 Type = EP_TYPE_BULK;
130 DoubleBanked = RNDISInterfaceInfo->Config.DataOUTPipeDoubleBank;
131 InterruptPeriod = 0;
132
133 RNDISInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
134 }
135 else if (PipeNum == RNDISInterfaceInfo->Config.NotificationPipeNumber)
136 {
137 Size = NotificationEndpoint->EndpointSize;
138 EndpointAddress = NotificationEndpoint->EndpointAddress;
139 Token = PIPE_TOKEN_IN;
140 Type = EP_TYPE_INTERRUPT;
141 DoubleBanked = RNDISInterfaceInfo->Config.NotificationPipeDoubleBank;
142 InterruptPeriod = NotificationEndpoint->PollingIntervalMS;
143
144 RNDISInterfaceInfo->State.NotificationPipeSize = NotificationEndpoint->EndpointSize;
145 }
146 else
147 {
148 continue;
149 }
150
151 if (!(Pipe_ConfigurePipe(PipeNum, Type, Token, EndpointAddress, Size,
152 DoubleBanked ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE)))
153 {
154 return CDC_ENUMERROR_PipeConfigurationFailed;
155 }
156
157 if (InterruptPeriod)
158 Pipe_SetInterruptPeriod(InterruptPeriod);
159 }
160
161 RNDISInterfaceInfo->State.ControlInterfaceNumber = RNDISControlInterface->InterfaceNumber;
162 RNDISInterfaceInfo->State.IsActive = true;
163
164 return RNDIS_ENUMERROR_NoError;
165 }
166
167 static uint8_t DCOMP_RNDIS_Host_NextRNDISControlInterface(void* const CurrentDescriptor)
168 {
169 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
170
171 if (Header->Type == DTYPE_Interface)
172 {
173 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
174
175 if ((Interface->Class == CDC_CSCP_CDCClass) &&
176 (Interface->SubClass == CDC_CSCP_ACMSubclass) &&
177 (Interface->Protocol == CDC_CSCP_VendorSpecificProtocol))
178 {
179 return DESCRIPTOR_SEARCH_Found;
180 }
181 }
182
183 return DESCRIPTOR_SEARCH_NotFound;
184 }
185
186 static uint8_t DCOMP_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor)
187 {
188 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
189
190 if (Header->Type == DTYPE_Interface)
191 {
192 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor,
193 USB_Descriptor_Interface_t);
194
195 if ((Interface->Class == CDC_CSCP_CDCDataClass) &&
196 (Interface->SubClass == CDC_CSCP_NoDataSubclass) &&
197 (Interface->Protocol == CDC_CSCP_NoDataProtocol))
198 {
199 return DESCRIPTOR_SEARCH_Found;
200 }
201 }
202
203 return DESCRIPTOR_SEARCH_NotFound;
204 }
205
206 static uint8_t DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor)
207 {
208 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
209
210 if (Header->Type == DTYPE_Endpoint)
211 {
212 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
213
214 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
215
216 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
217 !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
218 {
219 return DESCRIPTOR_SEARCH_Found;
220 }
221 }
222 else if (Header->Type == DTYPE_Interface)
223 {
224 return DESCRIPTOR_SEARCH_Fail;
225 }
226
227 return DESCRIPTOR_SEARCH_NotFound;
228 }
229
230 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
231 void* Buffer,
232 const uint16_t Length)
233 {
234 USB_ControlRequest = (USB_Request_Header_t)
235 {
236 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
237 .bRequest = RNDIS_REQ_SendEncapsulatedCommand,
238 .wValue = 0,
239 .wIndex = RNDISInterfaceInfo->State.ControlInterfaceNumber,
240 .wLength = Length,
241 };
242
243 Pipe_SelectPipe(PIPE_CONTROLPIPE);
244 return USB_Host_SendControlRequest(Buffer);
245 }
246
247 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
248 void* Buffer,
249 const uint16_t Length)
250 {
251 USB_ControlRequest = (USB_Request_Header_t)
252 {
253 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
254 .bRequest = RNDIS_REQ_GetEncapsulatedResponse,
255 .wValue = 0,
256 .wIndex = RNDISInterfaceInfo->State.ControlInterfaceNumber,
257 .wLength = Length,
258 };
259
260 Pipe_SelectPipe(PIPE_CONTROLPIPE);
261 return USB_Host_SendControlRequest(Buffer);
262 }
263
264 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
265 {
266 uint8_t ErrorCode;
267
268 RNDIS_KeepAlive_Message_t KeepAliveMessage;
269 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
270
271 KeepAliveMessage.MessageType = REMOTE_NDIS_KEEPALIVE_MSG;
272 KeepAliveMessage.MessageLength = sizeof(RNDIS_KeepAlive_Message_t);
273 KeepAliveMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
274
275 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &KeepAliveMessage,
276 sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
277 {
278 return ErrorCode;
279 }
280
281 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &KeepAliveMessageResponse,
282 sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
283 {
284 return ErrorCode;
285 }
286
287 return HOST_SENDCONTROL_Successful;
288 }
289
290 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
291 {
292 uint8_t ErrorCode;
293
294 RNDIS_Initialize_Message_t InitMessage;
295 RNDIS_Initialize_Complete_t InitMessageResponse;
296
297 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
298 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
299 InitMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
300
301 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
302 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
303 InitMessage.MaxTransferSize = RNDISInterfaceInfo->Config.HostMaxPacketSize;
304
305 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &InitMessage,
306 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
307 {
308 return ErrorCode;
309 }
310
311 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &InitMessageResponse,
312 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
313 {
314 return ErrorCode;
315 }
316
317 if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
318 return RNDIS_COMMAND_FAILED;
319
320 RNDISInterfaceInfo->State.DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
321
322 return HOST_SENDCONTROL_Successful;
323 }
324
325 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
326 const uint32_t Oid,
327 void* Buffer,
328 const uint16_t Length)
329 {
330 uint8_t ErrorCode;
331
332 struct
333 {
334 RNDIS_Set_Message_t SetMessage;
335 uint8_t ContiguousBuffer[Length];
336 } SetMessageData;
337
338 RNDIS_Set_Complete_t SetMessageResponse;
339
340 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
341 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
342 SetMessageData.SetMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
343
344 SetMessageData.SetMessage.Oid = Oid;
345 SetMessageData.SetMessage.InformationBufferLength = Length;
346 SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
347 SetMessageData.SetMessage.DeviceVcHandle = 0;
348
349 memcpy(&SetMessageData.ContiguousBuffer, Buffer, Length);
350
351 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &SetMessageData,
352 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
353 {
354 return ErrorCode;
355 }
356
357 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &SetMessageResponse,
358 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
359 {
360 return ErrorCode;
361 }
362
363 if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
364 return RNDIS_COMMAND_FAILED;
365
366 return HOST_SENDCONTROL_Successful;
367 }
368
369 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
370 const uint32_t Oid,
371 void* Buffer,
372 const uint16_t MaxLength)
373 {
374 uint8_t ErrorCode;
375
376 RNDIS_Query_Message_t QueryMessage;
377
378 struct
379 {
380 RNDIS_Query_Complete_t QueryMessageResponse;
381 uint8_t ContiguousBuffer[MaxLength];
382 } QueryMessageResponseData;
383
384 QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
385 QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
386 QueryMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
387
388 QueryMessage.Oid = Oid;
389 QueryMessage.InformationBufferLength = 0;
390 QueryMessage.InformationBufferOffset = 0;
391 QueryMessage.DeviceVcHandle = 0;
392
393 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &QueryMessage,
394 sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
395 {
396 return ErrorCode;
397 }
398
399 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &QueryMessageResponseData,
400 sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
401 {
402 return ErrorCode;
403 }
404
405 if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
406 return RNDIS_COMMAND_FAILED;
407
408 memcpy(Buffer, &QueryMessageResponseData.ContiguousBuffer, MaxLength);
409
410 return HOST_SENDCONTROL_Successful;
411 }
412
413 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
414 {
415 bool PacketWaiting;
416
417 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
418 return false;
419
420 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataINPipeNumber);
421
422 Pipe_Unfreeze();
423 PacketWaiting = Pipe_IsINReceived();
424 Pipe_Freeze();
425
426 return PacketWaiting;
427 }
428
429 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
430 void* Buffer,
431 uint16_t* const PacketLength)
432 {
433 uint8_t ErrorCode;
434
435 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
436 return PIPE_READYWAIT_DeviceDisconnected;
437
438 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataINPipeNumber);
439 Pipe_Unfreeze();
440
441 if (!(Pipe_IsReadWriteAllowed()))
442 {
443 if (Pipe_IsINReceived())
444 Pipe_ClearIN();
445
446 *PacketLength = 0;
447 Pipe_Freeze();
448 return PIPE_RWSTREAM_NoError;
449 }
450
451 RNDIS_Packet_Message_t DeviceMessage;
452
453 if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
454 NULL)) != PIPE_RWSTREAM_NoError)
455 {
456 return ErrorCode;
457 }
458
459 *PacketLength = (uint16_t)DeviceMessage.DataLength;
460
461 Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
462 NULL);
463
464 Pipe_Read_Stream_LE(Buffer, *PacketLength, NULL);
465
466 if (!(Pipe_BytesInPipe()))
467 Pipe_ClearIN();
468
469 Pipe_Freeze();
470
471 return PIPE_RWSTREAM_NoError;
472 }
473
474 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
475 void* Buffer,
476 const uint16_t PacketLength)
477 {
478 uint8_t ErrorCode;
479
480 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
481 return PIPE_READYWAIT_DeviceDisconnected;
482
483 RNDIS_Packet_Message_t DeviceMessage;
484
485 memset(&DeviceMessage, 0, sizeof(RNDIS_Packet_Message_t));
486 DeviceMessage.MessageType = REMOTE_NDIS_PACKET_MSG;
487 DeviceMessage.MessageLength = (sizeof(RNDIS_Packet_Message_t) + PacketLength);
488 DeviceMessage.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
489 DeviceMessage.DataLength = PacketLength;
490
491 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataOUTPipeNumber);
492 Pipe_Unfreeze();
493
494 if ((ErrorCode = Pipe_Write_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
495 NULL)) != PIPE_RWSTREAM_NoError)
496 {
497 return ErrorCode;
498 }
499
500 Pipe_Write_Stream_LE(Buffer, PacketLength, NULL);
501 Pipe_ClearOUT();
502
503 Pipe_Freeze();
504
505 return PIPE_RWSTREAM_NoError;
506 }
507
508 #endif
509