09fac7ab5c20bf979726555ea858f3f8ea4c1fc6
[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.fourwalledcubicle.com
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_CLASS_HOST_C
36 #define __INCLUDE_FROM_RNDIS_DRIVER
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 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
141 {
142 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
143 USB_Descriptor_Interface_t);
144
145 if ((CurrentInterface->Class == RNDIS_CONTROL_CLASS) &&
146 (CurrentInterface->SubClass == RNDIS_CONTROL_SUBCLASS) &&
147 (CurrentInterface->Protocol == RNDIS_CONTROL_PROTOCOL))
148 {
149 return DESCRIPTOR_SEARCH_Found;
150 }
151 }
152
153 return DESCRIPTOR_SEARCH_NotFound;
154 }
155
156 static uint8_t DCOMP_RNDIS_Host_NextRNDISDataInterface(void* const CurrentDescriptor)
157 {
158 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
159 {
160 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
161 USB_Descriptor_Interface_t);
162
163 if ((CurrentInterface->Class == RNDIS_DATA_CLASS) &&
164 (CurrentInterface->SubClass == RNDIS_DATA_SUBCLASS) &&
165 (CurrentInterface->Protocol == RNDIS_DATA_PROTOCOL))
166 {
167 return DESCRIPTOR_SEARCH_Found;
168 }
169 }
170
171 return DESCRIPTOR_SEARCH_NotFound;
172 }
173
174 static uint8_t DCOMP_RNDIS_Host_NextRNDISInterfaceEndpoint(void* const CurrentDescriptor)
175 {
176 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
177 {
178 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
179 USB_Descriptor_Endpoint_t);
180
181 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
182
183 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
184 !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
185 {
186 return DESCRIPTOR_SEARCH_Found;
187 }
188 }
189 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
190 {
191 return DESCRIPTOR_SEARCH_Fail;
192 }
193
194 return DESCRIPTOR_SEARCH_NotFound;
195 }
196
197 static uint8_t RNDIS_SendEncapsulatedCommand(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
198 void* Buffer,
199 const uint16_t Length)
200 {
201 USB_ControlRequest = (USB_Request_Header_t)
202 {
203 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
204 .bRequest = RNDIS_REQ_SendEncapsulatedCommand,
205 .wValue = 0,
206 .wIndex = RNDISInterfaceInfo->State.ControlInterfaceNumber,
207 .wLength = Length,
208 };
209
210 Pipe_SelectPipe(PIPE_CONTROLPIPE);
211 return USB_Host_SendControlRequest(Buffer);
212 }
213
214 static uint8_t RNDIS_GetEncapsulatedResponse(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
215 void* Buffer,
216 const uint16_t Length)
217 {
218 USB_ControlRequest = (USB_Request_Header_t)
219 {
220 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
221 .bRequest = RNDIS_REQ_GetEncapsulatedResponse,
222 .wValue = 0,
223 .wIndex = RNDISInterfaceInfo->State.ControlInterfaceNumber,
224 .wLength = Length,
225 };
226
227 Pipe_SelectPipe(PIPE_CONTROLPIPE);
228 return USB_Host_SendControlRequest(Buffer);
229 }
230
231 uint8_t RNDIS_Host_SendKeepAlive(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
232 {
233 uint8_t ErrorCode;
234
235 RNDIS_KeepAlive_Message_t KeepAliveMessage;
236 RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
237
238 KeepAliveMessage.MessageType = REMOTE_NDIS_KEEPALIVE_MSG;
239 KeepAliveMessage.MessageLength = sizeof(RNDIS_KeepAlive_Message_t);
240 KeepAliveMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
241
242 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &KeepAliveMessage,
243 sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
244 {
245 return ErrorCode;
246 }
247
248 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &KeepAliveMessageResponse,
249 sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
250 {
251 return ErrorCode;
252 }
253
254 return HOST_SENDCONTROL_Successful;
255 }
256
257 uint8_t RNDIS_Host_InitializeDevice(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
258 {
259 uint8_t ErrorCode;
260
261 RNDIS_Initialize_Message_t InitMessage;
262 RNDIS_Initialize_Complete_t InitMessageResponse;
263
264 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
265 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
266 InitMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
267
268 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
269 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
270 InitMessage.MaxTransferSize = RNDISInterfaceInfo->Config.HostMaxPacketSize;
271
272 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &InitMessage,
273 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
274 {
275 return ErrorCode;
276 }
277
278 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &InitMessageResponse,
279 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
280 {
281 return ErrorCode;
282 }
283
284 if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
285 return RNDIS_COMMAND_FAILED;
286
287 RNDISInterfaceInfo->State.DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
288
289 return HOST_SENDCONTROL_Successful;
290 }
291
292 uint8_t RNDIS_Host_SetRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
293 const uint32_t Oid,
294 void* Buffer,
295 const uint16_t Length)
296 {
297 uint8_t ErrorCode;
298
299 struct
300 {
301 RNDIS_Set_Message_t SetMessage;
302 uint8_t ContiguousBuffer[Length];
303 } SetMessageData;
304
305 RNDIS_Set_Complete_t SetMessageResponse;
306
307 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
308 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
309 SetMessageData.SetMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
310
311 SetMessageData.SetMessage.Oid = Oid;
312 SetMessageData.SetMessage.InformationBufferLength = Length;
313 SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
314 SetMessageData.SetMessage.DeviceVcHandle = 0;
315
316 memcpy(&SetMessageData.ContiguousBuffer, Buffer, Length);
317
318 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &SetMessageData,
319 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
320 {
321 return ErrorCode;
322 }
323
324 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &SetMessageResponse,
325 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
326 {
327 return ErrorCode;
328 }
329
330 if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
331 return RNDIS_COMMAND_FAILED;
332
333 return HOST_SENDCONTROL_Successful;
334 }
335
336 uint8_t RNDIS_Host_QueryRNDISProperty(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
337 const uint32_t Oid,
338 void* Buffer,
339 const uint16_t MaxLength)
340 {
341 uint8_t ErrorCode;
342
343 RNDIS_Query_Message_t QueryMessage;
344
345 struct
346 {
347 RNDIS_Query_Complete_t QueryMessageResponse;
348 uint8_t ContiguousBuffer[MaxLength];
349 } QueryMessageResponseData;
350
351 QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
352 QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
353 QueryMessage.RequestId = RNDISInterfaceInfo->State.RequestID++;
354
355 QueryMessage.Oid = Oid;
356 QueryMessage.InformationBufferLength = 0;
357 QueryMessage.InformationBufferOffset = 0;
358 QueryMessage.DeviceVcHandle = 0;
359
360 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(RNDISInterfaceInfo, &QueryMessage,
361 sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
362 {
363 return ErrorCode;
364 }
365
366 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(RNDISInterfaceInfo, &QueryMessageResponseData,
367 sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
368 {
369 return ErrorCode;
370 }
371
372 if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
373 return RNDIS_COMMAND_FAILED;
374
375 memcpy(Buffer, &QueryMessageResponseData.ContiguousBuffer, MaxLength);
376
377 return HOST_SENDCONTROL_Successful;
378 }
379
380 bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo)
381 {
382 bool PacketWaiting;
383
384 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
385 return false;
386
387 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataINPipeNumber);
388
389 Pipe_Unfreeze();
390 PacketWaiting = Pipe_IsINReceived();
391 Pipe_Freeze();
392
393 return PacketWaiting;
394 }
395
396 uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
397 void* Buffer,
398 uint16_t* const PacketLength)
399 {
400 uint8_t ErrorCode;
401
402 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
403 return PIPE_READYWAIT_DeviceDisconnected;
404
405 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataINPipeNumber);
406 Pipe_Unfreeze();
407
408 if (!(Pipe_IsReadWriteAllowed()))
409 {
410 if (Pipe_IsINReceived())
411 Pipe_ClearIN();
412
413 *PacketLength = 0;
414 Pipe_Freeze();
415 return PIPE_RWSTREAM_NoError;
416 }
417
418 RNDIS_Packet_Message_t DeviceMessage;
419
420 if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
421 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
422 {
423 return ErrorCode;
424 }
425
426 *PacketLength = (uint16_t)DeviceMessage.DataLength;
427
428 Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
429 NO_STREAM_CALLBACK);
430
431 Pipe_Read_Stream_LE(Buffer, *PacketLength, NO_STREAM_CALLBACK);
432
433 if (!(Pipe_BytesInPipe()))
434 Pipe_ClearIN();
435
436 Pipe_Freeze();
437
438 return PIPE_RWSTREAM_NoError;
439 }
440
441 uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceInfo,
442 void* Buffer,
443 const uint16_t PacketLength)
444 {
445 uint8_t ErrorCode;
446
447 if ((USB_HostState != HOST_STATE_Configured) || !(RNDISInterfaceInfo->State.IsActive))
448 return PIPE_READYWAIT_DeviceDisconnected;
449
450 RNDIS_Packet_Message_t DeviceMessage;
451
452 memset(&DeviceMessage, 0, sizeof(RNDIS_Packet_Message_t));
453 DeviceMessage.MessageType = REMOTE_NDIS_PACKET_MSG;
454 DeviceMessage.MessageLength = (sizeof(RNDIS_Packet_Message_t) + PacketLength);
455 DeviceMessage.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
456 DeviceMessage.DataLength = PacketLength;
457
458 Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataOUTPipeNumber);
459 Pipe_Unfreeze();
460
461 if ((ErrorCode = Pipe_Write_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
462 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
463 {
464 return ErrorCode;
465 }
466
467 Pipe_Write_Stream_LE(Buffer, PacketLength, NO_STREAM_CALLBACK);
468 Pipe_ClearOUT();
469
470 Pipe_Freeze();
471
472 return PIPE_RWSTREAM_NoError;
473 }
474
475 #endif