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