1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
|
//
// Copyright (C) Microsoft. All rights reserved.
//
#include "precomp.h"
typedef struct _USB_WRITE_REQ_CONTEXT
{
WDFMEMORY UrbMemory;
PURB Urb;
PMDL Mdl;
WDFMEMORY LookasideBuffer;
PBUS_OBJECT BusObject;
MBB_BUS_SEND_DATA_COMPLETION_CALLBACK Callback;
MBB_REQUEST_HANDLE RequestHandle;
} USB_WRITE_REQ_CONTEXT, *PUSB_WRITE_REQ_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(USB_WRITE_REQ_CONTEXT, GetWriteRequestContext)
EVT_WDF_USB_READER_COMPLETION_ROUTINE BulkInReadComplete;
EVT_WDF_USB_READERS_FAILED BulkInReadError;
NTSTATUS
GetWriteRequests(WDFUSBDEVICE UsbDevice, WDFREQUEST* ReturnedWriteRequest);
VOID FreeWriteRequest(WDFUSBDEVICE UsbDevice, WDFREQUEST WriteRequest);
NTSTATUS
MbbBusSelectDataAltSetting(__in MBB_BUS_HANDLE BusHandle, __in UCHAR AltSetting)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
WDF_USB_INTERFACE_SELECT_SETTING_PARAMS SettingParams;
WDFUSBINTERFACE UsbInterface = NULL;
WDF_USB_PIPE_INFORMATION pipeInfo;
WDFUSBPIPE pipe;
UCHAR ConfiguredPipes = 0;
UCHAR index = 0;
NTSTATUS Status;
NTSTATUS TempStatus;
WDF_USB_CONTINUOUS_READER_CONFIG ReaderConfig;
usbDeviceContext = GetUsbDeviceContext(BusObject->WdfUsbDevice);
WdfWaitLockAcquire(usbDeviceContext->PipeStateLock, NULL);
UsbInterface = WdfUsbTargetDeviceGetInterface(BusObject->WdfUsbDevice, usbDeviceContext->WdfDataInterfaceIndex);
WDF_USB_INTERFACE_SELECT_SETTING_PARAMS_INIT_SETTING(&SettingParams, AltSetting == 0 ? 0 : usbDeviceContext->UsbDataInterfaceSetting);
Status = WdfUsbInterfaceSelectSetting(UsbInterface, NULL, &SettingParams);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
ConfiguredPipes = WdfUsbInterfaceGetNumConfiguredPipes(UsbInterface);
if (((AltSetting == ALT_DATA_SETTING_0) && (ConfiguredPipes != ALT_DATA_SETTING_0_PIPES)) ||
((AltSetting == ALT_DATA_SETTING_1) && (ConfiguredPipes != ALT_DATA_SETTING_1_PIPES)))
{
Status = STATUS_UNSUCCESSFUL;
goto Cleanup;
}
if (AltSetting == ALT_DATA_SETTING_1)
{
for (index = 0; index < ConfiguredPipes; index++)
{
WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo);
pipe = WdfUsbInterfaceGetConfiguredPipe(
UsbInterface,
index, // PipeIndex,
&pipeInfo);
if (WdfUsbPipeTypeBulk == pipeInfo.PipeType)
{
if (WdfUsbTargetPipeIsInEndpoint(pipe))
{
usbDeviceContext->BulkInputPipeConfigured = TRUE;
usbDeviceContext->BulkInputPipe = pipe;
usbDeviceContext->BulkInputPipeMaxPacket = pipeInfo.MaximumPacketSize;
}
else
{
usbDeviceContext->BulkOutputPipeConfigured = TRUE;
usbDeviceContext->BulkOutputPipe = pipe;
usbDeviceContext->BulkOutputPipeMaxPacket = pipeInfo.MaximumPacketSize;
}
}
else
{
Status = STATUS_UNSUCCESSFUL;
goto Cleanup;
}
}
if (!(usbDeviceContext->BulkInputPipeConfigured && usbDeviceContext->BulkOutputPipeConfigured))
{
Status = STATUS_UNSUCCESSFUL;
goto Cleanup;
}
//
// configure the continous reader on the bulk pipe now, since this can only be done once on a given pipe
// unless it is unselected
//
WDF_USB_CONTINUOUS_READER_CONFIG_INIT(&ReaderConfig, BulkInReadComplete, BusObject, BusObject->MaxBulkInTransfer);
if (BusObject->UsbCapDeviceInfo.DeviceInfoHeader.DeviceType == USB_CAP_DEVICE_TYPE_UDE_MBIM)
{
ReaderConfig.NumPendingReads = PENDING_BULK_IN_READS_FOR_UDE_MBIM;
}
else
{
ReaderConfig.NumPendingReads = PENDING_BULK_IN_READS;
}
ReaderConfig.HeaderLength = BusObject->BulkInHeaderSize;
ReaderConfig.EvtUsbTargetPipeReadersFailed = BulkInReadError;
Status = WdfUsbTargetPipeConfigContinuousReader(usbDeviceContext->BulkInputPipe, &ReaderConfig);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
//
// call this now that the pipe is configured
//
PreAllocateWriteRequests(BusObject->WdfUsbDevice);
}
else
{
//
// selecting the setting causes the frame work to delete the pipe
//
WDFREQUEST WriteRequest = NULL;
usbDeviceContext->BulkInputPipeConfigured = FALSE;
usbDeviceContext->BulkInputPipe = NULL;
usbDeviceContext->BulkOutputPipeConfigured = FALSE;
usbDeviceContext->BulkOutputPipe = NULL;
WdfSpinLockAcquire(usbDeviceContext->WriteCollectionLock);
WriteRequest = (WDFREQUEST)WdfCollectionGetLastItem(usbDeviceContext->WriteRequestCollection);
//
// delete the write request because they are associated with a pipe object
//
while (WriteRequest != NULL)
{
WdfCollectionRemove(usbDeviceContext->WriteRequestCollection, WriteRequest);
WdfObjectDelete(WriteRequest);
WriteRequest = (WDFREQUEST)WdfCollectionGetLastItem(usbDeviceContext->WriteRequestCollection);
}
WdfSpinLockRelease(usbDeviceContext->WriteCollectionLock);
}
Cleanup:
if (!NT_SUCCESS(Status))
{
//
// failed to configure the pipes, set back to alt setting 0
//
WDF_USB_INTERFACE_SELECT_SETTING_PARAMS_INIT_SETTING(&SettingParams, 0);
TempStatus = WdfUsbInterfaceSelectSetting(UsbInterface, NULL, &SettingParams);
usbDeviceContext->BulkInputPipeConfigured = FALSE;
usbDeviceContext->BulkInputPipe = NULL;
usbDeviceContext->BulkOutputPipeConfigured = FALSE;
usbDeviceContext->BulkOutputPipe = NULL;
}
WdfWaitLockRelease(usbDeviceContext->PipeStateLock);
return Status;
}
NTSTATUS
MbbUsbDeviceStartDataPipes(__in PUSB_DEVICE_CONTEXT usbDeviceContext)
{
NTSTATUS Status;
WdfWaitLockAcquire(usbDeviceContext->PipeStateLock, NULL);
if (!(usbDeviceContext->BulkInputPipeConfigured && usbDeviceContext->BulkOutputPipeConfigured))
{
Status = STATUS_UNSUCCESSFUL;
goto Cleanup;
}
Status = WdfIoTargetStart(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkInputPipe));
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
usbDeviceContext->BulkInputPipeStarted = TRUE;
Status = WdfIoTargetStart(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe));
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
usbDeviceContext->BulkOutputPipeStarted = TRUE;
Cleanup:
if (!NT_SUCCESS(Status))
{
//
// failed to configure the pipes, set back to alt setting 0
//
if (usbDeviceContext->BulkInputPipeStarted)
{
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkInputPipe), WdfIoTargetCancelSentIo);
}
if (usbDeviceContext->BulkOutputPipeStarted)
{
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe), WdfIoTargetCancelSentIo);
}
usbDeviceContext->BulkInputPipeStarted = FALSE;
usbDeviceContext->BulkOutputPipeStarted = FALSE;
}
WdfWaitLockRelease(usbDeviceContext->PipeStateLock);
return Status;
}
NTSTATUS
MbbBusStartDataPipes(__in MBB_BUS_HANDLE BusHandle)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
return MbbUsbDeviceStartDataPipes(GetUsbDeviceContext(BusObject->WdfUsbDevice));
}
VOID MbbUsbDeviceStopDataPipes(__in PUSB_DEVICE_CONTEXT usbDeviceContext)
{
WDFUSBINTERFACE UsbInterface = NULL;
WdfWaitLockAcquire(usbDeviceContext->PipeStateLock, NULL);
if (usbDeviceContext->BulkInputPipeConfigured)
{
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkInputPipe), WdfIoTargetCancelSentIo);
}
if (usbDeviceContext->BulkOutputPipeConfigured)
{
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe), WdfIoTargetCancelSentIo);
}
usbDeviceContext->BulkInputPipeStarted = FALSE;
usbDeviceContext->BulkOutputPipeStarted = FALSE;
WdfWaitLockRelease(usbDeviceContext->PipeStateLock);
return;
}
VOID MbbBusStopDataPipes(__in MBB_BUS_HANDLE BusHandle)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
PUSB_DEVICE_CONTEXT usbDeviceContext = GetUsbDeviceContext(BusObject->WdfUsbDevice);
WdfWorkItemFlush(usbDeviceContext->BulkPipeResetWorkitem);
MbbUsbDeviceStopDataPipes(GetUsbDeviceContext(BusObject->WdfUsbDevice));
}
NTSTATUS
MbbUsbDeviceResetBulkPipe(__in PUSB_DEVICE_CONTEXT usbDeviceContext, __in BOOLEAN Out)
{
WDFUSBPIPE Pipe = NULL;
WDF_REQUEST_SEND_OPTIONS SendOptions;
NTSTATUS StatusToReturn = STATUS_SUCCESS;
NTSTATUS Status;
WdfWaitLockAcquire(usbDeviceContext->PipeStateLock, NULL);
if (Out ? usbDeviceContext->BulkOutputPipeConfigured : usbDeviceContext->BulkInputPipeConfigured)
{
Pipe = Out ? usbDeviceContext->BulkOutputPipe : usbDeviceContext->BulkInputPipe;
WDF_REQUEST_SEND_OPTIONS_INIT(&SendOptions, WDF_REQUEST_SEND_OPTION_IGNORE_TARGET_STATE);
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&SendOptions, WDF_REL_TIMEOUT_IN_SEC(30));
Status = WdfUsbTargetPipeAbortSynchronously(Pipe, NULL, &SendOptions);
if (!NT_SUCCESS(Status))
{
if (StatusToReturn == STATUS_SUCCESS)
{
StatusToReturn = Status;
}
}
WDF_REQUEST_SEND_OPTIONS_INIT(&SendOptions, WDF_REQUEST_SEND_OPTION_IGNORE_TARGET_STATE);
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&SendOptions, WDF_REL_TIMEOUT_IN_SEC(30));
Status = WdfUsbTargetPipeResetSynchronously(Pipe, NULL, &SendOptions);
if (!NT_SUCCESS(Status))
{
if (StatusToReturn == STATUS_SUCCESS)
{
StatusToReturn = Status;
}
}
}
WdfWaitLockRelease(usbDeviceContext->PipeStateLock);
return StatusToReturn;
}
NTSTATUS
MbbBusResetBulkPipe(__in MBB_BUS_HANDLE BusHandle, __in BOOLEAN Out)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
return MbbUsbDeviceResetBulkPipe(GetUsbDeviceContext(BusObject->WdfUsbDevice), Out);
}
EVT_WDF_REQUEST_COMPLETION_ROUTINE WriteCompletionRoutine;
void WriteCompletionRoutine(__in WDFREQUEST Request, __in WDFIOTARGET Target, __in PWDF_REQUEST_COMPLETION_PARAMS CompletionParams, __in WDFCONTEXT Context)
/*++
Routine Description
Request - Handle to the WDFREQUEST which was used to send data to the USB target.
Target - Handle to the Iotarget to which teh Request was sent. COnceptually this
is the BULK USB __out pipe(on of Data or beacon)
CompletionParams - In case of USB this contains the USB status and amount of bytes transferred
Context - This is the COntext we set in WdfRequestSend
Arguments:
Return Value:
--*/
{
PUSB_WRITE_REQ_CONTEXT WriteContext;
NTSTATUS Status;
UNREFERENCED_PARAMETER(Target);
WriteContext = (PUSB_WRITE_REQ_CONTEXT)Context;
Status = CompletionParams->IoStatus.Status;
//
// For usb devices, we should look at the Usb.Completion param.
//
if (WriteContext->Callback != NULL)
{
(*WriteContext->Callback)(WriteContext->BusObject->ProtocolHandle, WriteContext->RequestHandle, Status, WriteContext->Mdl);
}
if (WriteContext->LookasideBuffer != NULL)
{
WdfObjectDelete(WriteContext->LookasideBuffer);
}
if (WriteContext->UrbMemory != NULL)
{
WdfObjectDelete(WriteContext->UrbMemory);
}
FreeWriteRequest(WriteContext->BusObject->WdfUsbDevice, Request);
}
NTSTATUS
MbbBusWriteData(__in MBB_BUS_HANDLE BusHandle, __in MBB_REQUEST_HANDLE RequestHandle, __in PMDL Mdl, __in MBB_BUS_SEND_DATA_COMPLETION_CALLBACK Callback)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
PMDL TempMdl = NULL;
ULONGLONG TotalTransferLength = 0;
NTSTATUS Status;
WDFMEMORY urbMemory = NULL;
WDF_OBJECT_ATTRIBUTES objectAttribs;
PURB urbBuffer = NULL;
WDF_REQUEST_SEND_OPTIONS SendOptions;
WDFREQUEST WriteRequest = NULL;
USBD_PIPE_HANDLE usbdPipeHandle = NULL;
PUSB_WRITE_REQ_CONTEXT writeContext = NULL;
BOOLEAN SentToDevice = FALSE;
WDFMEMORY BufferMemoryObject = NULL;
WDFREQUEST WriteRequestZLP = NULL;
PUSB_WRITE_REQ_CONTEXT writeContextZLP = NULL;
NTSTATUS ZlpStatus;
usbDeviceContext = GetUsbDeviceContext(BusObject->WdfUsbDevice);
if (!ExAcquireRundownProtection(&usbDeviceContext->BulkPipeResetRundown))
{
return STATUS_NDIS_ADAPTER_NOT_READY;
}
//
// make sure the length is not too big
//
TempMdl = Mdl;
while (TempMdl != NULL)
{
TotalTransferLength += MmGetMdlByteCount(TempMdl);
TempMdl = TempMdl->Next;
}
if (TotalTransferLength > BusObject->NtbParam.dwNtbOutMaxSize)
{
//
// too big
//
Status = STATUS_UNSUCCESSFUL;
goto Cleanup;
}
if ((TotalTransferLength < BusObject->NtbParam.dwNtbOutMaxSize) && (TotalTransferLength % usbDeviceContext->BulkOutputPipeMaxPacket == 0))
{
//
// The transfer length is less than the max out transfer size, and transfer length is a multiple of MaxPacket size.
// Need to send a ZLP to the device stack to terminated the transfer.
Status = GetWriteRequests(BusObject->WdfUsbDevice, &WriteRequestZLP);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
writeContextZLP = GetWriteRequestContext(WriteRequestZLP);
writeContextZLP->BusObject = BusObject;
writeContextZLP->Callback = NULL;
}
Status = GetWriteRequests(BusObject->WdfUsbDevice, &WriteRequest);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
writeContext = GetWriteRequestContext(WriteRequest);
if (usbDeviceContext->LookasideList == NULL)
{
//
// allocate the URB, request is the parent
//
WDF_OBJECT_ATTRIBUTES_INIT(&objectAttribs);
objectAttribs.ParentObject = WriteRequest;
Status = WdfUsbTargetDeviceCreateUrb(BusObject->WdfUsbDevice, &objectAttribs, &urbMemory, &urbBuffer);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
//
// get the USBD pipe handle, to build the URB
//
usbdPipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(usbDeviceContext->BulkOutputPipe);
//
// NOTE : call UsbBuildInterruptOrBulkTransferRequest otherwise
// WdfUsbTargetPipeFormatRequestForUrb will assert
// with *** Assertion failed: Urb->UrbHeader.Length >= sizeof(_URB_HEADER)
//
UsbBuildInterruptOrBulkTransferRequest(
urbBuffer,
sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER),
usbdPipeHandle,
NULL,
Mdl,
(ULONG)TotalTransferLength,
USBD_TRANSFER_DIRECTION_OUT | USBD_SHORT_TRANSFER_OK,
NULL);
//
// By calling WdfUsbTargetPipeFormatRequestForUrb the frameworks allocate a lot of resources
// like the underlying IRP for the request and hence it is better to do it at initilization
// to prevent an avoidable failure later.
//
Status = WdfUsbTargetPipeFormatRequestForUrb(usbDeviceContext->BulkOutputPipe, WriteRequest, urbMemory, NULL);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
writeContext->UrbMemory = urbMemory;
writeContext->Urb = (PURB)urbBuffer;
}
else
{
//
// usb stack does not support chained mdl's, double buffer the transfer
//
PUCHAR DestBuffer = NULL;
PUCHAR SourceAddress = NULL;
size_t BufferSize = 0;
ULONG SourceLength = 0;
WDFMEMORY_OFFSET MemoryOffset;
Status = WdfMemoryCreateFromLookaside(usbDeviceContext->LookasideList, &BufferMemoryObject);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
DestBuffer = (PUCHAR)WdfMemoryGetBuffer(BufferMemoryObject, &BufferSize);
TempMdl = Mdl;
TotalTransferLength = 0;
while (TempMdl != NULL)
{
SourceLength = MmGetMdlByteCount(TempMdl);
SourceAddress = (PUCHAR)MmGetSystemAddressForMdlSafe(TempMdl, NormalPagePriority | MdlMappingNoExecute);
if (SourceAddress == NULL)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Cleanup;
}
RtlCopyMemory(DestBuffer, SourceAddress, SourceLength);
DestBuffer += SourceLength;
TotalTransferLength += SourceLength;
ASSERT(TotalTransferLength <= BufferSize);
TempMdl = TempMdl->Next;
}
MemoryOffset.BufferOffset = 0;
MemoryOffset.BufferLength = (ULONG)TotalTransferLength;
Status = WdfUsbTargetPipeFormatRequestForWrite(usbDeviceContext->BulkOutputPipe, WriteRequest, BufferMemoryObject, &MemoryOffset);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
writeContext->LookasideBuffer = BufferMemoryObject;
BufferMemoryObject = NULL;
}
//
// set REQUEST_CONTEXT parameters.
//
writeContext->Mdl = Mdl;
writeContext->BusObject = BusObject;
writeContext->Callback = Callback;
writeContext->RequestHandle = RequestHandle;
WdfRequestSetCompletionRoutine(WriteRequest, WriteCompletionRoutine, writeContext);
WDF_REQUEST_SEND_OPTIONS_INIT(&SendOptions, 0);
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&SendOptions, WDF_REL_TIMEOUT_IN_SEC(30));
SentToDevice = WdfRequestSend(WriteRequest, WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe), &SendOptions);
Status = STATUS_PENDING;
if (!SentToDevice)
{
Status = WdfRequestGetStatus(WriteRequest);
}
else
{
WriteRequest = NULL;
}
if (NT_SUCCESS(Status))
{
//
// actual data write worked, see if we need to send the zlp
//
if (WriteRequestZLP != NULL)
{
Status = WdfUsbTargetPipeFormatRequestForWrite(usbDeviceContext->BulkOutputPipe, WriteRequestZLP, NULL, NULL);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
WdfRequestSetCompletionRoutine(WriteRequestZLP, WriteCompletionRoutine, writeContextZLP);
WDF_REQUEST_SEND_OPTIONS_INIT(&SendOptions, 0);
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&SendOptions, WDF_REL_TIMEOUT_IN_SEC(30));
SentToDevice = WdfRequestSend(WriteRequestZLP, WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe), &SendOptions);
ZlpStatus = STATUS_PENDING;
if (!SentToDevice)
{
ZlpStatus = WdfRequestGetStatus(WriteRequestZLP);
}
else
{
WriteRequestZLP = NULL;
}
}
}
Cleanup:
ExReleaseRundownProtection(&usbDeviceContext->BulkPipeResetRundown);
if (WriteRequest != NULL)
{
if (NULL != writeContext)
{
if (NULL != writeContext->LookasideBuffer)
{
WdfObjectDelete(writeContext->LookasideBuffer);
}
if (NULL != writeContext->UrbMemory)
{
WdfObjectDelete(writeContext->UrbMemory);
}
}
FreeWriteRequest(BusObject->WdfUsbDevice, WriteRequest);
WriteRequest = NULL;
}
if (WriteRequestZLP != NULL)
{
Status = ZlpStatus;
FreeWriteRequest(BusObject->WdfUsbDevice, WriteRequestZLP);
WriteRequestZLP = NULL;
}
return Status;
}
VOID BulkInReadComplete(__in WDFUSBPIPE Pipe, __in WDFMEMORY Memory, __in size_t NumBytesTransferred, __in WDFCONTEXT Context)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)Context;
PUCHAR Buffer = NULL;
PMDL Mdl = NULL;
PUCHAR DataBuffer = NULL;
if (NumBytesTransferred > 0)
{
//
// actaully got some data
//
Buffer = (PUCHAR)WdfMemoryGetBuffer(Memory, NULL);
//
// the header at the front is where we will put the MDL
//
Mdl = (PMDL)Buffer;
//
// the actual payload starts after the header/mdl
//
DataBuffer = Buffer + BusObject->BulkInHeaderSize;
ASSERT(((ULONG_PTR)DataBuffer & 0x7) == 0);
//
// bytes transfered does not include the header
//
MmInitializeMdl(Mdl, DataBuffer, NumBytesTransferred);
MmBuildMdlForNonPagedPool(Mdl);
ASSERT(BusObject->BulkInHeaderSize >= MmSizeOfMdl(Mdl, NumBytesTransferred));
WdfObjectReference(Memory);
(*BusObject->ReceiveDataCallback)(BusObject->ProtocolHandle, Memory, Mdl);
}
return;
}
BOOLEAN
BulkInReadError(__in WDFUSBPIPE Pipe, __in NTSTATUS Status, __in USBD_STATUS UsbdStatus)
{
return TRUE;
}
VOID MbbBusReturnReceiveBuffer(__in MBB_BUS_HANDLE BusHandle, __in MBB_RECEIVE_CONTEXT ReceiveContext, __in PMDL Mdl)
{
WDFMEMORY Memory = (WDFMEMORY)ReceiveContext;
WdfObjectDereference(Memory);
return;
}
NTSTATUS
CreateWriteRequest(WDFUSBDEVICE UsbDevice, WDFREQUEST* ReturnedWriteRequest)
{
WDFREQUEST WriteRequest = NULL;
PUSB_WRITE_REQ_CONTEXT writeContext = NULL;
WDF_OBJECT_ATTRIBUTES objectAttribs;
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
NTSTATUS Status = STATUS_INVALID_PARAMETER;
*ReturnedWriteRequest = NULL;
usbDeviceContext = GetUsbDeviceContext(UsbDevice);
if (usbDeviceContext->BulkOutputPipe == NULL)
{
goto Cleanup;
}
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&objectAttribs, USB_WRITE_REQ_CONTEXT);
objectAttribs.ParentObject = UsbDevice;
//
// create the request
//
Status = WdfRequestCreate(&objectAttribs, WdfUsbTargetPipeGetIoTarget(usbDeviceContext->BulkOutputPipe), &WriteRequest);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
writeContext = GetWriteRequestContext(WriteRequest);
RtlZeroMemory(writeContext, sizeof(*writeContext));
//
// Preallocate the request timer to prevent the request from failing while trying to send it.
//
Status = WdfRequestAllocateTimer(WriteRequest);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
Status = WdfUsbTargetPipeFormatRequestForWrite(usbDeviceContext->BulkOutputPipe, WriteRequest, NULL, NULL);
if (!NT_SUCCESS(Status))
{
goto Cleanup;
}
*ReturnedWriteRequest = WriteRequest;
WriteRequest = NULL;
Cleanup:
if (WriteRequest != NULL)
{
WdfObjectDelete(WriteRequest);
WriteRequest = NULL;
}
return Status;
}
NTSTATUS
PreAllocateWriteRequests(WDFUSBDEVICE UsbDevice)
{
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
WDFREQUEST WriteRequest = NULL;
NTSTATUS Status;
ULONG i = 0;
usbDeviceContext = GetUsbDeviceContext(UsbDevice);
for (i = 0; i < MAX_PREALLOCATED_WRITE_REQUESTS; i++)
{
Status = CreateWriteRequest(UsbDevice, &WriteRequest);
if (!NT_SUCCESS(Status))
{
break;
}
Status = WdfCollectionAdd(usbDeviceContext->WriteRequestCollection, WriteRequest);
if (!NT_SUCCESS(Status))
{
WdfObjectDelete(WriteRequest);
WriteRequest = NULL;
break;
}
}
return Status;
}
NTSTATUS
GetWriteRequests(WDFUSBDEVICE UsbDevice, WDFREQUEST* ReturnedWriteRequest)
{
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
NTSTATUS Status = STATUS_SUCCESS;
WDF_REQUEST_REUSE_PARAMS ReuseParams;
PUSB_WRITE_REQ_CONTEXT writeContext = NULL;
usbDeviceContext = GetUsbDeviceContext(UsbDevice);
*ReturnedWriteRequest = NULL;
if (usbDeviceContext->BulkOutputPipe == NULL)
{
return STATUS_UNSUCCESSFUL;
}
WdfSpinLockAcquire(usbDeviceContext->WriteCollectionLock);
*ReturnedWriteRequest = (WDFREQUEST)WdfCollectionGetLastItem(usbDeviceContext->WriteRequestCollection);
if (*ReturnedWriteRequest == NULL)
{
//
// the collection is empty, try creating a new one
//
WdfSpinLockRelease(usbDeviceContext->WriteCollectionLock);
Status = CreateWriteRequest(UsbDevice, ReturnedWriteRequest);
}
else
{
WdfCollectionRemove(usbDeviceContext->WriteRequestCollection, *ReturnedWriteRequest);
WdfSpinLockRelease(usbDeviceContext->WriteCollectionLock);
WDF_REQUEST_REUSE_PARAMS_INIT(&ReuseParams, 0, STATUS_SUCCESS);
WdfRequestReuse(*ReturnedWriteRequest, &ReuseParams);
}
if (NT_SUCCESS(Status))
{
writeContext = GetWriteRequestContext(*ReturnedWriteRequest);
RtlZeroMemory(writeContext, sizeof(*writeContext));
}
return Status;
}
VOID FreeWriteRequest(WDFUSBDEVICE UsbDevice, WDFREQUEST WriteRequest)
{
PUSB_DEVICE_CONTEXT usbDeviceContext = NULL;
NTSTATUS Status = STATUS_SUCCESS;
ULONG RequestCount = 0;
usbDeviceContext = GetUsbDeviceContext(UsbDevice);
WdfSpinLockAcquire(usbDeviceContext->WriteCollectionLock);
RequestCount = WdfCollectionGetCount(usbDeviceContext->WriteRequestCollection);
if (RequestCount < MAX_PREALLOCATED_WRITE_REQUESTS * 2)
{
//
// put it back in the collection
//
Status = WdfCollectionAdd(usbDeviceContext->WriteRequestCollection, WriteRequest);
if (NT_SUCCESS(Status))
{
WriteRequest = NULL;
}
}
WdfSpinLockRelease(usbDeviceContext->WriteCollectionLock);
if (WriteRequest != NULL)
{
WdfObjectDelete(WriteRequest);
WriteRequest = NULL;
}
return;
}
VOID MbbBusResetDataPipes(_In_ MBB_BUS_HANDLE BusHandle)
{
PBUS_OBJECT BusObject = (PBUS_OBJECT)BusHandle;
PUSB_DEVICE_CONTEXT usbDeviceContext = GetUsbDeviceContext(BusObject->WdfUsbDevice);
if (!InterlockedCompareExchange(&usbDeviceContext->BulkPipeResetFlag, TRUE, FALSE))
{
WdfWorkItemEnqueue(usbDeviceContext->BulkPipeResetWorkitem);
}
else
{
}
}
VOID MbbUsbDeviceCyclePort(_In_ PUSB_DEVICE_CONTEXT usbDeviceContext)
{
NTSTATUS Status = WdfUsbTargetDeviceCyclePortSynchronously(usbDeviceContext->BusObject->WdfUsbDevice);
if (!NT_SUCCESS(Status))
{
}
}
void ResetDataPipeWorkItem(_In_ WDFWORKITEM WorkItem)
{
PUSB_DEVICE_CONTEXT usbDeviceContext = GetUsbDeviceContext(WdfWorkItemGetParentObject(WorkItem));
ExWaitForRundownProtectionRelease(&usbDeviceContext->BulkPipeResetRundown);
ExRundownCompleted(&usbDeviceContext->BulkPipeResetRundown);
MbbUsbDeviceStopDataPipes(usbDeviceContext);
MbbUsbDeviceResetBulkPipe(usbDeviceContext, TRUE);
MbbUsbDeviceCyclePort(usbDeviceContext);
MbbUsbDeviceStartDataPipes(usbDeviceContext);
ExReInitializeRundownProtection(&usbDeviceContext->BulkPipeResetRundown);
InterlockedExchange(&usbDeviceContext->BulkPipeResetFlag, FALSE);
}
|