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