bulkusb.c
29.6 KB
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
1001
/*++
Copyright (c) 1997-1998 Microsoft Corporation
Module Name:
BulkUsb.c
Abstract:
Bulk USB device driver for Intel 82930 USB test board
Main module
Environment:
kernel mode only
Notes:
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Copyright (c) 1997-1998 Microsoft Corporation. All Rights Reserved.
Revision History:
11/17/97: created
--*/
#include "wdm.h"
#include "stdarg.h"
#include "stdio.h"
#include "usbdi.h"
#include "usbdlib.h"
#include "Blk82930.h"
#include "bbrdb_guid.h"
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This entry point is called directly by the I/O system.
Arguments:
DriverObject - pointer to the driver object
RegistryPath - pointer to a unicode string representing the path
to driver-specific key in the registry
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PDEVICE_OBJECT deviceObject = NULL;
BOOLEAN fRes;
#if DBG
// should be done before any debug output is done.
// read our debug verbosity level from the registry
BulkUsb_GetRegistryDword( BULKUSB_REGISTRY_PARAMETERS_PATH, //absolute registry path
L"DebugLevel", // REG_DWORD ValueName
&gDebugLevel ); // Value receiver
#endif
DbgPrint("Hello world");
BULKUSB_KdPrint( DBGLVL_MINIMUM ,("Entering DriverEntry(), RegistryPath=\n %ws\n", RegistryPath->Buffer ));
//
// Create dispatch points for create, close, unload
DriverObject->MajorFunction[IRP_MJ_CREATE] = BulkUsb_Create;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = BulkUsb_Close;
DriverObject->DriverUnload = BulkUsb_Unload;
// User mode DeviceIoControl() calls will be routed here
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = BulkUsb_ProcessIOCTL;
// User mode ReadFile()/WriteFile() calls will be routed here
DriverObject->MajorFunction[IRP_MJ_WRITE] = BulkUsb_Write;
DriverObject->MajorFunction[IRP_MJ_READ] = BulkUsb_Read;
// routines for handling system PNP and power management requests
DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = BulkUsb_ProcessSysControlIrp;
DriverObject->MajorFunction[IRP_MJ_PNP] = BulkUsb_ProcessPnPIrp;
DriverObject->MajorFunction[IRP_MJ_POWER] = BulkUsb_ProcessPowerIrp;
// The Functional Device Object (FDO) will not be created for PNP devices until
// this routine is called upon device plug-in.
DriverObject->DriverExtension->AddDevice = BulkUsb_PnPAddDevice;
BULKUSB_KdPrint( DBGLVL_DEFAULT,("exiting DriverEntry (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS
BulkUsb_ProcessSysControlIrp(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
/*++
Routine Description:
Main dispatch table routine for IRP_MJ_SYSTEM_CONTROL
We basically just pass these down to the PDO
Arguments:
DeviceObject - pointer to FDO device object
Irp - pointer to an I/O Request Packet
Return Value:
Status returned from lower driver
--*/
{
PIO_STACK_LOCATION irpStack;
PDEVICE_EXTENSION deviceExtension;
NTSTATUS ntStatus = STATUS_SUCCESS;
NTSTATUS waitStatus;
PDEVICE_OBJECT stackDeviceObject;
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
//
// Get a pointer to the current location in the Irp. This is where
// the function codes and parameters are located.
//
irpStack = IoGetCurrentIrpStackLocation (Irp);
//
// Get a pointer to the device extension
//
deviceExtension = DeviceObject->DeviceExtension;
stackDeviceObject = deviceExtension->TopOfStackDeviceObject;
BULKUSB_KdPrint( DBGLVL_HIGH, ( "enter BulkUsb_ProcessSysControlIrp()\n") );
BulkUsb_IncrementIoCount(DeviceObject);
BULKUSB_ASSERT( IRP_MJ_SYSTEM_CONTROL == irpStack->MajorFunction );
IoCopyCurrentIrpStackLocationToNext(Irp);
ntStatus = IoCallDriver(stackDeviceObject,
Irp);
BulkUsb_DecrementIoCount(DeviceObject);
BULKUSB_KdPrint( DBGLVL_HIGH,("BulkUsb_ProcessSysControlIrp() Exit BulkUsb_ProcessSysControlIrp %x\n", ntStatus));
return ntStatus;
}
VOID
BulkUsb_Unload(
IN PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the allocated resources, etc.
Arguments:
DriverObject - pointer to a driver object
Return Value:
--*/
{
BULKUSB_KdPrint( DBGLVL_HIGH,("enter BulkUsb_Unload\n"));
//
// Free any global resources allocated
// in DriverEntry.
// We have few or none because for a PNP device, almost all
// allocation is done in PnpAddDevice() and all freeing
// while handling IRP_MN_REMOVE_DEVICE:
//
BULKUSB_ASSERT( gExAllocCount == 0 );
BULKUSB_KdPrint( DBGLVL_DEFAULT,("exit BulkUsb_Unload\n"));
}
NTSTATUS
BulkUsb_SymbolicLink(
IN PDEVICE_OBJECT DeviceObject,
IN OUT PUNICODE_STRING deviceLinkUnicodeString
)
/*++
Routine Description:
This routine is called to create and initialize
a GUID-based symbolic link to our device to be used to open/create
instances of us from user mode.
Called from BulkUsb_CreateDeviceObject() to create the link.
Arguments:
DeviceObject - pointer to our Physical Device Object ( PDO )
deviceLinkUnicodeString - Points to a unicode string structure allocated by the caller.
If this routine is successful, it initializes the unicode string and allocates
the string buffer containing the kernel-mode path to the symbolic link for this
device interface.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/{
NTSTATUS ntStatus = STATUS_SUCCESS;
// Create the symbolic link
// IoRegisterDeviceInterface registers device functionality (a device interface)
// that a driver will enable for use by applications or other system components.
ntStatus = IoRegisterDeviceInterface(
DeviceObject,
(LPGUID)&GUID_CLASS_BBRDB,
NULL,
deviceLinkUnicodeString);
BULKUSB_KdPrintCond( DBGLVL_MEDIUM, (!(NT_SUCCESS(ntStatus))),
("FAILED to IoRegisterDeviceInterface()\n"));
if (NT_SUCCESS(ntStatus)) {
// IoSetDeviceInterfaceState enables or disables a previously
// registered device interface. Applications and other system components
// can open only interfaces that are enabled.
ntStatus = IoSetDeviceInterfaceState(deviceLinkUnicodeString, TRUE);
BULKUSB_KdPrintCond( DBGLVL_MEDIUM,
(!(NT_SUCCESS(ntStatus))),
("FAILED to IoSetDeviceInterfaceState()\n"));
BULKUSB_KdPrintCond( DBGLVL_MEDIUM,
((NT_SUCCESS(ntStatus))),
("SUCCEEDED IoSetDeviceInterfaceState()\n"));
}
return ntStatus;
}
NTSTATUS
BulkUsb_CreateDeviceObject(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject,
IN PDEVICE_OBJECT *DeviceObject
)
/*++
Routine Description:
Creates a Functional DeviceObject
Arguments:
DriverObject - pointer to the driver object for device
DeviceObject - pointer to DeviceObject pointer to return
created device object.
Instance - instance of the device create.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus;
UNICODE_STRING deviceLinkUnicodeString;
PDEVICE_EXTENSION deviceExtension;
USHORT i;
BULKUSB_KdPrint( DBGLVL_DEFAULT,("enter BulkUsb_CreateDeviceObject() \n"));
ntStatus = BulkUsb_SymbolicLink( PhysicalDeviceObject, &deviceLinkUnicodeString );
BULKUSB_KdPrintCond( DBGLVL_DEFAULT,
(NT_SUCCESS(ntStatus)),
("BulkUsb_CreateDeviceObject() SUCCESS Create GUID_CLASS_BulkUsb_BULK-based Device name\n %ws\n Length decimal %d, MaximumLength decimal %d\n",
deviceLinkUnicodeString.Buffer,
deviceLinkUnicodeString.Length,
deviceLinkUnicodeString.MaximumLength));
BULKUSB_KdPrintCond( DBGLVL_DEFAULT,
(!(NT_SUCCESS(ntStatus))),
("BulkUsb_CreateDeviceObject() FAILED to Create GUID_CLASS_BulkUsb_BULK-based Device name\n"));
if (NT_SUCCESS(ntStatus)) {
ntStatus = IoCreateDevice (DriverObject,
sizeof (DEVICE_EXTENSION),
NULL,
FILE_DEVICE_UNKNOWN,
FILE_AUTOGENERATED_DEVICE_NAME,
FALSE,
DeviceObject);
if (NT_SUCCESS(ntStatus)) {
deviceExtension = (PDEVICE_EXTENSION) ((*DeviceObject)->DeviceExtension);
}
BULKUSB_KdPrintCond( DBGLVL_DEFAULT,
(!(NT_SUCCESS(ntStatus))),
("BulkUsb_CreateDeviceObject() IoCreateDevice() FAILED\n"));
if (!NT_SUCCESS(ntStatus)) {
return ntStatus;
}
//default maximum transfer size per staged io request
deviceExtension->MaximumTransferSize = BULKUSB_MAX_TRANSFER_SIZE ;
#if DBG
// may be overridden in registry
BulkUsb_GetRegistryDword( BULKUSB_REGISTRY_PARAMETERS_PATH,
L"MaximumTransferSize",
&(deviceExtension->MaximumTransferSize) );
#endif
// Name buffer for our named Functional device object link
// The name is generated based on the driver's class GUID
RtlCopyMemory(deviceExtension->DeviceLinkNameBuffer,
deviceLinkUnicodeString.Buffer,
deviceLinkUnicodeString.Length);
// this event is triggered when there is no pending io of any kind and device is removed
KeInitializeEvent(&deviceExtension->RemoveEvent, NotificationEvent, FALSE);
// this event is triggered when self-requested power irps complete
KeInitializeEvent(&deviceExtension->SelfRequestedPowerIrpEvent, NotificationEvent, FALSE);
// this event is triggered when when BulkUsb_AsyncReadWrite_Complete() finishes or cancels last staged io irp
KeInitializeEvent(&deviceExtension->StagingDoneEvent, NotificationEvent, FALSE);
// this event is triggered when there is no pending io (pending io count == 1 )
KeInitializeEvent(&deviceExtension->NoPendingIoEvent, NotificationEvent, FALSE);
// spinlock used to protect inc/dec iocount logic
KeInitializeSpinLock (&deviceExtension->IoCountSpinLock);
// spinlock used to protect test of deviceExtension->BaseIrp in
// BulkUsb_StagedReadWrite()
KeInitializeSpinLock(&deviceExtension->FastCompleteSpinlock);
//free buffer from unicode string we used to init interface
RtlFreeUnicodeString( &deviceLinkUnicodeString );
}
return ntStatus;
}
NTSTATUS
BulkUsb_CallUSBD(
IN PDEVICE_OBJECT DeviceObject,
IN PURB Urb
)
/*++
Routine Description:
Passes a URB to the USBD class driver
The client device driver passes USB request block (URB) structures
to the class driver as a parameter in an IRP with Irp->MajorFunction
set to IRP_MJ_INTERNAL_DEVICE_CONTROL and the next IRP stack location
Parameters.DeviceIoControl.IoControlCode field set to
IOCTL_INTERNAL_USB_SUBMIT_URB.
Arguments:
DeviceObject - pointer to the physical device object (PDO)
Urb - pointer to an already-formatted Urb request block
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus, status = STATUS_SUCCESS;
PDEVICE_EXTENSION deviceExtension;
PIRP irp;
KEVENT event;
IO_STATUS_BLOCK ioStatus;
PIO_STACK_LOCATION nextStack;
BULKUSB_KdPrint( DBGLVL_MAXIMUM,("enter BulkUsb_CallUSBD\n"));
deviceExtension = DeviceObject->DeviceExtension;
//
// issue a synchronous request
//
KeInitializeEvent(&event, NotificationEvent, FALSE);
irp = IoBuildDeviceIoControlRequest(
IOCTL_INTERNAL_USB_SUBMIT_URB,
deviceExtension->TopOfStackDeviceObject, //Points to the next-lower driver's device object
NULL, // optional input bufer; none needed here
0, // input buffer len if used
NULL, // optional output bufer; none needed here
0, // output buffer len if used
TRUE, // If InternalDeviceControl is TRUE the target driver's Dispatch
// outine for IRP_MJ_INTERNAL_DEVICE_CONTROL or IRP_MJ_SCSI
// is called; otherwise, the Dispatch routine for
// IRP_MJ_DEVICE_CONTROL is called.
&event, // event to be signalled on completion
&ioStatus); // Specifies an I/O status block to be set when the request is completed the lower driver.
//
// As an alternative, we could call KeDelayExecutionThread, wait for some
// period of time, and try again....but we keep it simple for right now
//
if (!irp) {
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Call the class driver to perform the operation. If the returned status
// is PENDING, wait for the request to complete.
//
nextStack = IoGetNextIrpStackLocation(irp);
BULKUSB_ASSERT(nextStack != NULL);
//
// pass the URB to the USB driver stack
//
nextStack->Parameters.Others.Argument1 = Urb;
ntStatus = IoCallDriver(deviceExtension->TopOfStackDeviceObject, irp);
BULKUSB_KdPrint( DBGLVL_MAXIMUM,("BulkUsb_CallUSBD() return from IoCallDriver USBD %x\n", ntStatus));
if (ntStatus == STATUS_PENDING) {
status = KeWaitForSingleObject(
&event,
Suspended,
KernelMode,
FALSE,
NULL);
} else {
ioStatus.Status = ntStatus;
}
BULKUSB_KdPrint( DBGLVL_MAXIMUM,("BulkUsb_CallUSBD() URB status = %x status = %x irp status %x\n",
Urb->UrbHeader.Status, status, ioStatus.Status));
//
// USBD maps the error code for us
//
ntStatus = ioStatus.Status;
BULKUSB_KdPrintCond( DBGLVL_MAXIMUM, !NT_SUCCESS( ntStatus ), ("exit BulkUsb_CallUSBD FAILED (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS
BulkUsb_ConfigureDevice(
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
Initializes a given instance of the device on the USB and
selects and saves the configuration.
Arguments:
DeviceObject - pointer to the physical device object for this instance of the 82930
device.
Return Value:
NT status code
--*/
{
PDEVICE_EXTENSION deviceExtension;
NTSTATUS ntStatus;
PURB urb;
ULONG siz;
BULKUSB_KdPrint( DBGLVL_HIGH,("enter BulkUsb_ConfigureDevice\n"));
deviceExtension = DeviceObject->DeviceExtension;
BULKUSB_ASSERT( deviceExtension->UsbConfigurationDescriptor == NULL );
urb = BULKUSB_ExAllocatePool(NonPagedPool,
sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
if ( !urb )
return STATUS_INSUFFICIENT_RESOURCES;
// When USB_CONFIGURATION_DESCRIPTOR_TYPE is specified for DescriptorType
// in a call to UsbBuildGetDescriptorRequest(),
// all interface, endpoint, class-specific, and vendor-specific descriptors
// for the configuration also are retrieved.
// The caller must allocate a buffer large enough to hold all of this
// information or the data is truncated without error.
// Therefore the 'siz' set below is just a 'good guess', and we may have to retry
siz = sizeof(USB_CONFIGURATION_DESCRIPTOR) + 512;
// We will break out of this 'retry loop' when UsbBuildGetDescriptorRequest()
// has a big enough deviceExtension->UsbConfigurationDescriptor buffer not to truncate
while( 1 ) {
deviceExtension->UsbConfigurationDescriptor = BULKUSB_ExAllocatePool(NonPagedPool, siz);
if ( !deviceExtension->UsbConfigurationDescriptor ) {
BULKUSB_ExFreePool(urb);
return STATUS_INSUFFICIENT_RESOURCES;
}
UsbBuildGetDescriptorRequest(urb,
(USHORT) sizeof (struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_CONFIGURATION_DESCRIPTOR_TYPE,
0,
0,
deviceExtension->UsbConfigurationDescriptor,
NULL,
siz,
NULL);
ntStatus = BulkUsb_CallUSBD(DeviceObject, urb);
BULKUSB_KdPrint( DBGLVL_HIGH,("BulkUsb_CallUSBD() Configuration Descriptor = %x, len %x\n",
deviceExtension->UsbConfigurationDescriptor,
urb->UrbControlDescriptorRequest.TransferBufferLength));
//
// if we got some data see if it was enough.
// NOTE: we may get an error in URB because of buffer overrun
if (urb->UrbControlDescriptorRequest.TransferBufferLength>0 &&
deviceExtension->UsbConfigurationDescriptor->wTotalLength > siz) {
siz = deviceExtension->UsbConfigurationDescriptor->wTotalLength;
BULKUSB_ExFreePool(deviceExtension->UsbConfigurationDescriptor);
deviceExtension->UsbConfigurationDescriptor = NULL;
} else {
break; // we got it on the first try
}
} // end, while (retry loop )
BULKUSB_ExFreePool(urb);
BULKUSB_ASSERT( deviceExtension->UsbConfigurationDescriptor );
//
// We have the configuration descriptor for the configuration we want.
// Now we issue the select configuration command to get
// the pipes associated with this configuration.
//
ntStatus = BulkUsb_SelectInterface(DeviceObject,
deviceExtension->UsbConfigurationDescriptor);
BULKUSB_KdPrint( DBGLVL_HIGH,("exit BulkUsb_ConfigureDevice (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS
BulkUsb_SelectInterface(
IN PDEVICE_OBJECT DeviceObject,
IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
)
/*++
Routine Description:
Initializes an 82930 with (possibly) multiple interfaces;
This minidriver only supports one interface (with multiple endpoints).
Arguments:
DeviceObject - pointer to the device object for this instance of the 82930
device.
ConfigurationDescriptor - pointer to the USB configuration
descriptor containing the interface and endpoint
descriptors.
Return Value:
NT status code
--*/
{
PDEVICE_EXTENSION deviceExtension;
NTSTATUS ntStatus;
PURB urb = NULL;
ULONG i;
PUSB_INTERFACE_DESCRIPTOR interfaceDescriptor = NULL;
PUSBD_INTERFACE_INFORMATION Interface = NULL;
USHORT siz;
PUCHAR pInf;
BULKUSB_KdPrint( DBGLVL_MEDIUM,("enter BulkUsb_SelectInterface\n"));
deviceExtension = DeviceObject->DeviceExtension;
BULKUSB_KdPrint( DBGLVL_HIGH,("BulkUsb_SelectInterface() called with NULL Interface\n"));
//
// BulkUsb driver only supports one interface, we must parse
// the configuration descriptor for the interface
// and remember the pipes.
//
urb = USBD_CreateConfigurationRequest(ConfigurationDescriptor, &siz);
if (urb) {
//
// USBD_ParseConfigurationDescriptorEx searches a given configuration
// descriptor and returns a pointer to an interface that matches the
// given search criteria. We only support one interface on this device
//
interfaceDescriptor =
USBD_ParseConfigurationDescriptorEx(ConfigurationDescriptor,
ConfigurationDescriptor, //search from start of config descriptro
-1, // interface number not a criteria; we only support one interface
-1, // not interested in alternate setting here either
-1, // interface class not a criteria
-1, // interface subclass not a criteria
-1 // interface protocol not a criteria
);
if ( !interfaceDescriptor ) {
BULKUSB_KdPrint( DBGLVL_HIGH,("BulkUsb_SelectInterface() ParseConfigurationDescriptorEx() failed\n returning STATUS_INSUFFICIENT_RESOURCES\n"));
BULKUSB_ExFreePool(urb);
return STATUS_INSUFFICIENT_RESOURCES;
}
Interface = &urb->UrbSelectConfiguration.Interface;
// allocate space for an array of pipe information structs;
// in this basic sample, just used to track if opened/closed
deviceExtension->PipeInfo = BULKUSB_ExAllocatePool(
NonPagedPool,
Interface->NumberOfPipes * sizeof ( BULKUSB_PIPEINFO ) );
if ( !deviceExtension->PipeInfo ) {
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlZeroMemory(deviceExtension->PipeInfo,
Interface->NumberOfPipes * sizeof ( BULKUSB_PIPEINFO ) );
pInf = (PUCHAR ) deviceExtension->PipeInfo;
for (i=0; i< Interface->NumberOfPipes; i++) {
//
// Perform any pipe initialization here;
// We set the max transfer size and any Pipe flags we use; USBD sets the rest
// of the Interface struct members
//
Interface->Pipes[i].MaximumTransferSize = deviceExtension->MaximumTransferSize;
( (PBULKUSB_PIPEINFO) pInf)->fPipeOpened = FALSE;
pInf += sizeof ( BULKUSB_PIPEINFO );
}
UsbBuildSelectConfigurationRequest(urb,
(USHORT) siz,
ConfigurationDescriptor);
ntStatus = BulkUsb_CallUSBD(DeviceObject, urb);
deviceExtension->UsbConfigurationHandle =
urb->UrbSelectConfiguration.ConfigurationHandle;
} else {
BULKUSB_KdPrint( DBGLVL_HIGH,("BulkUsb_SelectInterface() USBD_CreateConfigurationRequest() failed\n returning STATUS_INSUFFICIENT_RESOURCES\n"));
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
if (NT_SUCCESS(ntStatus)) {
//
// Save the configuration handle for this device
//
deviceExtension->UsbConfigurationHandle =
urb->UrbSelectConfiguration.ConfigurationHandle;
deviceExtension->UsbInterface = BULKUSB_ExAllocatePool(NonPagedPool,
Interface->Length);
if (deviceExtension->UsbInterface) {
ULONG j;
//
// save a copy of the interface information returned
//
RtlCopyMemory(deviceExtension->UsbInterface, Interface, Interface->Length);
//
// Dump the interface to the debugger
//
BULKUSB_KdPrint( DBGLVL_MEDIUM,("---------\n"));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("NumberOfPipes 0x%x\n", deviceExtension->UsbInterface->NumberOfPipes));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Length 0x%x\n", deviceExtension->UsbInterface->Length));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Alt Setting 0x%x\n", deviceExtension->UsbInterface->AlternateSetting));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Interface Number 0x%x\n", deviceExtension->UsbInterface->InterfaceNumber));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Class, subclass, protocol 0x%x 0x%x 0x%x\n",
deviceExtension->UsbInterface->Class,
deviceExtension->UsbInterface->SubClass,
deviceExtension->UsbInterface->Protocol));
// Dump the pipe info
for (j=0; j<Interface->NumberOfPipes; j++) {
PUSBD_PIPE_INFORMATION pipeInformation;
pipeInformation = &deviceExtension->UsbInterface->Pipes[j];
BULKUSB_KdPrint( DBGLVL_MEDIUM,("---------\n"));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("PipeType 0x%x\n", pipeInformation->PipeType));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("EndpointAddress 0x%x\n", pipeInformation->EndpointAddress));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("MaxPacketSize 0x%x\n", pipeInformation->MaximumPacketSize));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Interval 0x%x\n", pipeInformation->Interval));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("Handle 0x%x\n", pipeInformation->PipeHandle));
BULKUSB_KdPrint( DBGLVL_MEDIUM,("MaximumTransferSize 0x%x\n", pipeInformation->MaximumTransferSize));
}
BULKUSB_KdPrint( DBGLVL_MEDIUM,("---------\n"));
}
}
if (urb) {
// don't call the BULKUSB_ExFreePool since the buffer was
// alloced by USBD_CreateConfigurationRequest, not BULKUSB_ExAllocatePool()
ExFreePool(urb);
}
BULKUSB_KdPrint( DBGLVL_HIGH,("exit BulkUsb_SelectInterface (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS
BulkUsb_ResetPipe(
IN PDEVICE_OBJECT DeviceObject,
IN PUSBD_PIPE_INFORMATION PipeInfo
)
/*++
Routine Description:
Reset a given USB pipe.
NOTES:
This will reset the host to Data0 and should also reset the device to Data0
Arguments:
Ptrs to our FDO and a USBD_PIPE_INFORMATION struct
Return Value:
NT status code
--*/
{
NTSTATUS ntStatus;
PURB urb;
PDEVICE_EXTENSION deviceExtension;
deviceExtension = DeviceObject->DeviceExtension;
BULKUSB_KdPrint( DBGLVL_DEFAULT,("BulkUsb_ResetPipe() Reset Pipe %x\n", PipeInfo));
urb = BULKUSB_ExAllocatePool(NonPagedPool,
sizeof(struct _URB_PIPE_REQUEST));
if (urb) {
urb->UrbHeader.Length = (USHORT) sizeof (struct _URB_PIPE_REQUEST);
urb->UrbHeader.Function = URB_FUNCTION_RESET_PIPE;
urb->UrbPipeRequest.PipeHandle =
PipeInfo->PipeHandle;
ntStatus = BulkUsb_CallUSBD(DeviceObject, urb);
BULKUSB_ExFreePool(urb);
} else {
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
if (!(NT_SUCCESS(ntStatus))) {
#if DBG
if ( gpDbg )
gpDbg->PipeErrorCount++;
#endif
BULKUSB_KdPrint( DBGLVL_DEFAULT,("BulkUsb_ResetPipe() FAILED, ntStatus =0x%x\n", ntStatus ));
}
else {
#if DBG
if ( gpDbg )
gpDbg->ResetPipeCount++;
#endif
BULKUSB_KdPrint( DBGLVL_DEFAULT,("BulkUsb_ResetPipe() SUCCESS, ntStatus =0x%x\n", ntStatus ));
}
return ntStatus;
}
LONG
BulkUsb_DecrementIoCount(
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
We keep a pending IO count ( extension->PendingIoCount ) in the device extension.
The first increment of this count is done on adding the device.
Subsequently, the count is incremented for each new IRP received and
decremented when each IRP is completed or passed on.
Transition to 'one' therefore indicates no IO is pending and signals
deviceExtension->NoPendingIoEvent. This is needed for processing
IRP_MN_QUERY_REMOVE_DEVICE
Transition to 'zero' signals an event ( deviceExtension->RemoveEvent )
to enable device removal. This is used in processing for IRP_MN_REMOVE_DEVICE
Arguments:
DeviceObject -- ptr to our FDO
Return Value:
deviceExtension->PendingIoCount
--*/
{
PDEVICE_EXTENSION deviceExtension;
LONG ioCount;
KIRQL oldIrql;
deviceExtension = DeviceObject->DeviceExtension;
KeAcquireSpinLock (&deviceExtension->IoCountSpinLock, &oldIrql);
ioCount = InterlockedDecrement(&deviceExtension->PendingIoCount);
#if DBG
InterlockedDecrement(&gpDbg->PendingIoCount);
#endif
BULKUSB_TrapCond( DBGLVL_HIGH,( 0 > ioCount ) );
if (ioCount==1) {
// trigger no pending io
KeSetEvent(&deviceExtension->NoPendingIoEvent,
1,
FALSE);
}
if (ioCount==0) {
// trigger remove-device event
KeSetEvent(&deviceExtension->RemoveEvent,
1,
FALSE);
}
KeReleaseSpinLock (&deviceExtension->IoCountSpinLock, oldIrql);
BULKUSB_KdPrint( DBGLVL_HIGH,("Exit BulkUsb_DecrementIoCount() Pending io count = %x\n", ioCount));
return ioCount;
}
VOID
BulkUsb_IncrementIoCount(
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
We keep a pending IO count ( extension->PendingIoCount ) in the device extension.
The first increment of this count is done on adding the device.
Subsequently, the count is incremented for each new IRP received and
decremented when each IRP is completed or passed on.
Arguments:
DeviceObject -- ptr to our FDO
Return Value:
None
--*/
{
PDEVICE_EXTENSION deviceExtension;
KIRQL oldIrql;
deviceExtension = DeviceObject->DeviceExtension;
BULKUSB_KdPrint( DBGLVL_MAXIMUM,("Enter BulkUsb_IncrementIoCount() Pending io count = %x\n", deviceExtension->PendingIoCount));
KeAcquireSpinLock (&deviceExtension->IoCountSpinLock, &oldIrql);
InterlockedIncrement(&deviceExtension->PendingIoCount);
#if DBG
InterlockedIncrement(&gpDbg->PendingIoCount);
#endif
KeReleaseSpinLock (&deviceExtension->IoCountSpinLock, oldIrql);
BULKUSB_KdPrint( DBGLVL_HIGH,("Exit BulkUsb_IncrementIoCount() Pending io count = %x\n", deviceExtension->PendingIoCount));
}