forked from flowswitch/cypress-ezusb-x64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezusbsys.c
4906 lines (3975 loc) · 152 KB
/
ezusbsys.c
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
//////////////////////////////////////////////////////////////////////
//
// File: ezusbsys.c
// $Archive: /USB/Drivers/ezusbdrv/ezusbsys.c $
//
// Purpose:
// General purpose USB device driver
//
// Environment:
// kernel mode
//
// $Author: Mdn $
//
// $History: ezusbsys.c $
//
// ***************** Version 6 *****************
// User: Mdn Date: 9/17/01 Time: 10:54a
// Updated in $/USB/Drivers/ezusbdrv
// Fixed memory leak bug in ISO streaming code. Was setting the
// PendingTransfers variable to the wrong value and not properly freeing
// after stopping the ISO stream.
//
// ***************** Version 5 *****************
// User: Mdn Date: 10/06/00 Time: 10:08a
// Updated in $/USB/Drivers/ezusbdrv
// Added IOCTL_EZUSB_SET_FEATURE
//
// ***************** Version 4 *****************
// User: Mdn Date: 8/17/00 Time: 9:46a
// Updated in $/USB/Drivers/ezusbdrv
// added a generic get descriptor function.
//
// ***************** Version 3 *****************
// User: Mdn Date: 8/04/00 Time: 11:41a
// Updated in $/USB/Drivers/ezmon
// added support for monitor download to FX2
//
// ***************** Version 2 *****************
// User: Mdn Date: 7/21/00 Time: 4:27p
// Updated in $/USB/Drivers/ezusbdrv
// Added a Started flag to the device extension. This flag gets set after
// the device has successfully Started. It is used to prevent a potential
// race condition where an application could get a handle (with
// CreateFile()) before the device is completely enumerated.
//
// ***************** Version 1 *****************
// User: Tpm Date: 6/09/00 Time: 6:32p
// Created in $/USB/Drivers/ezusbdrv
//
// ***************** Version 42 *****************
// User: Mdn Date: 5/05/00 Time: 10:24a
// Updated in $/EzUsb/Drivers/ezusbdrv
// comments
//
// ***************** Version 41 *****************
// User: Mdn Date: 4/18/00 Time: 2:45p
// Updated in $/EzUsb/Drivers/ezusbdrv
// fixed memory leak where I wasn't freeing an URB allocated for
// vendor/class requests.
//
// ***************** Version 40 *****************
// User: Mdn Date: 1/21/00 Time: 10:03a
// Updated in $/EzUsb/Drivers/ezusbdrv
// modified device removal code to send an abort message to all pipes
// before proceding with device removal. This solves the deadlock problem
// that can occur when a transfer is pending and the device receives a
// remove message. Before this change, the driver would block the removal
// indefinitely waiting for all pending transfers to complete.
//
// ***************** Version 39 *****************
// User: Markm Date: 12/17/99 Time: 11:17a
// Updated in $/EzUsb/Drivers/ezusbdrv
// Removed interface select workaround for an obscure OSR2.1 bug since the
// driver no longer supports Win95. Changed interface select so that it
// selects the first interface/alternate setting in the config descriptor.
// Before, it was hardcoded to select interface 0, AS 0. This should
// allow the driver to handle composite devices more easily, although I
// haven't tested it.
//
// ***************** Version 38 *****************
// User: Markm Date: 10/06/99 Time: 1:18p
// Updated in $/EzUsb/Drivers/ezusbdrv
// Made changes to Power IRP handling to fix the blue screen on shutdown
// that was occuring under Win2K. Specifically, started using PoXXX()
// calls for handling power IRPs. See code comments in
// Ezusb_DispatchPower(). The PoXXX() calls are not supported by Win95, so
// as of this revision, the driver will no longer work with Windows 95.
//
// ***************** Version 37 *****************
// User: Markm Date: 5/18/99 Time: 3:37p
// Updated in $/EzUsb/Drivers/ezusbdrv
//
// ***************** Version 36 *****************
// User: Markm Date: 4/29/99 Time: 9:48a
// Updated in $/EzUsb/Drivers/ezusbdrv
// Fixed IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST so that it correctly returns
// the number of bytes transferred.
//
// ***************** Version 35 *****************
// User: Markm Date: 4/16/99 Time: 3:39p
// Updated in $/EzUsb/Drivers/ezusbdrv
// more minor changes to get rid of compiler warnings.
//
// ***************** Version 34 *****************
// User: Markm Date: 4/12/99 Time: 10:26a
// Updated in $/EzUsb/Drivers/ezusbdrv
// minor changes to get rid of compiler warnings.
//
// ***************** Version 33 *****************
// User: Markm Date: 3/25/99 Time: 4:16p
// Updated in $/EzUsb/Drivers/ezusbdrv
// Fixed a bug in the surprise removal code I just added. I was returning
// from the PnP dispatch function without unlocking the device object.
//
// ***************** Version 32 *****************
// User: Markm Date: 3/25/99 Time: 2:03p
// Updated in $/EzUsb/Drivers/ezusbdrv
// Added code to allow unplugs (surprise removal) under NT5 without
// notifying the user.
//
// ***************** Version 31 *****************
// User: Markm Date: 2/23/99 Time: 9:50a
// Updated in $/EzUsb/Drivers/ezusbdrv
// Driver now supports ISO IN streaming with a path to user mode.
//
// ***************** Version 30 *****************
// User: Markm Date: 2/10/99 Time: 3:31p
// Updated in $/EzUsb/Drivers/ezusbdrv
// removed lots of unused code. Added ring buffer support functions.
//
// ***************** Version 29 *****************
// User: Markm Date: 2/01/99 Time: 11:57a
// Updated in $/EzUsb/Drivers/ezusbdrv
// Added preliminary support for ISO streaming.
//
// ***************** Version 28 *****************
// User: Markm Date: 6/12/98 Time: 4:23p
// Updated in $/EzUsb/Drivers/ezusbdrv
// modified pipe reset and abort code so that the driver will work
// correctly under Windows 95 OSR2.1. For Win98, MS changed the size of
// the URB_PIPE_REQUEST structure. They added an extra ulong to it. So,
// a driver compiled with the 98 DDk and run under 95 will send the wrong
// URB size. The solution is to check the USBDI version whenever I do a
// pipe reset or abort. If the USBDI version is pre-win98, I subtract 4
// from the size of the urb.
//
// ***************** Version 27 *****************
// User: Markm Date: 4/10/98 Time: 2:52p
// Updated in $/EZUSB/ezmon
// Modified Intel Hex download code to stop the 8051 before downloading to
// internal RAM.
//
// ***************** Version 26 *****************
// User: Markm Date: 4/09/98 Time: 4:39p
// Updated in $/EZUSB/ezusb driver
// Was not returning status information for ISO transfers. Now it does!
//
// ***************** Version 25 *****************
// User: Markm Date: 4/09/98 Time: 4:39p
// Updated in $/EZUSB/ezmon
// changes for monitor version of driver
//
// ***************** Version 24 *****************
// User: Markm Date: 4/09/98 Time: 3:09p
// Updated in $/EZUSB/ezusb driver
// nothing much
//
// ***************** Version 23 *****************
// User: Markm Date: 4/09/98 Time: 3:00p
// Updated in $/EZUSB/ezusb driver
// Added function to download Intel Hex File records to EZ-USB. For now,
// this function is only used by the conditional build of the driver that
// automatically downloads the Keil monitor.
//
// ***************** Version 22 *****************
// User: Markm Date: 4/07/98 Time: 1:52p
// Updated in $/EZUSB/ezusb driver
// Added IOCTL_EZUSB_GET_DRIVER_VERSION
//
// ***************** Version 21 *****************
// User: Markm Date: 4/06/98 Time: 4:26p
// Updated in $/EZUSB/ezusb driver
// Modified ISO transfer code.
// * Transfer descriptors for the ISO transfer are now sent up to the
// caller along with the actual data, so the caller can get the status of
// the transfer on a packet-by-packet basis.
// * Disabled default values. Caller must specify all fields in the ISO
// control structure.
// * Corrected bug where the Stream and Transfer objects weren't being
// freed.
//
// Added some code to measure the latency of a bulk transfer.
//
// ***************** Version 20 *****************
// User: Markm Date: 3/19/98 Time: 10:13a
// Updated in $/EZUSB/ezusb driver
// Added IOCTL_EZUSB_ANCHOR_DOWNLOAD to support A0 loads to a specific
// memory offset.
//
// ***************** Version 19 *****************
// User: Markm Date: 2/26/98 Time: 4:04p
// Updated in $/EZUSB/ezusb driver
// Added functions to perform anchor downloads and 8051 reset.
// Added conditionally compiled code to support a special version of the
// driver that will automatically download the Keil 8051 monitor after
// device attachment.
//
// ***************** Version 18 *****************
// User: Markm Date: 2/25/98 Time: 2:09p
// Updated in $/EZUSB/ezusb driver
// changes for adding version resource to the driver
//
// ***************** Version 17 *****************
// User: Markm Date: 2/11/98 Time: 9:51a
// Updated in $/EZUSB/ezusb driver
// 1. Added code to handle IRP_MN_CLOSE.
// 2. Now maintain a count of open file handles in the device extension.
// 3. Added workaround in Ezusb_SelectInterfaces() to avoid system hangs
// during device removal under OSR2.1. See comments there.
//
// ***************** Version 16 *****************
// User: Markm Date: 2/02/98 Time: 3:35p
// Updated in $/EZUSB/ezusb driver
//
// ***************** Version 15 *****************
// User: Markm Date: 1/27/98 Time: 11:36a
// Updated in $/EZUSB/ezusb driver
// Modified ISO transfer code so that the number of transfer buffers and
// the number of ISO frames per buffer can be specified by the caller.
//
// ***************** Version 14 *****************
// User: Markm Date: 1/26/98 Time: 10:11a
// Updated in $/EZUSB/ezusb driver
// 1) modified device removal code that was crashing OSR2.1. MS sample
// code says that during device removal, the remove IRP should be passed
// down the stack, and then the deviceobject should be deleted. This
// causes a bugcheck under OSR2.1. Reversing the order of these
// operations fixes the problem.
// 2) hardcoded the initial alternate setting to 2. It was set to 1, but
// this was also causing system crashes under OSR 2.1 during device
// removal. Still under investigation.
//
// ***************** Version 13 *****************
// User: Markm Date: 1/22/98 Time: 11:50a
// Updated in $/EZUSB/ezusb driver
// Fixed a bug in the device removal handler. It wasn't deleteting the
// symbolic link for the deviceobject after removal.
// Removed lots of unused code.
// Rewrote ISO loopback test code.
// Wrote new ISO read/write functions.
//
// ***************** Version 12 *****************
// User: Markm Date: 1/18/98 Time: 3:20p
// Updated in $/EZUSB/ezusb driver
// renamed variables. DeviceExtension becomes pdx and DeviceObject
// becomes fdo.
// rewrote handlers for device removal. Cleaned up power management
// dispatch. Added code to maintain a usage count for the device.
//
// ***************** Version 11 *****************
// User: Markm Date: 1/15/98 Time: 4:36p
// Updated in $/EZUSB/ezusb driver
// Added
// IOCTL_EZUSB_GET_CURRENT_FRAME_NUMBER
// IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST
// IOCTL_EZUSB_GET_LAST_ERROR
// preliminary code for handling device removal gracefully.
//
// ***************** Version 10 *****************
// User: Markm Date: 1/14/98 Time: 10:29a
// Updated in $/EZUSB/ezusb driver
// Cleanup.
// New functions for handling bulk transfers.
//
// ***************** Version 9 *****************
// User: Markm Date: 1/08/98 Time: 5:14p
// Updated in $/EZUSB/ezusb driver
// major changes to PnP IRP handling. lots of reformatting.
//
// ***************** Version 8 *****************
// User: Markm Date: 1/02/98 Time: 1:41p
// Updated in $/EZUSB/ezusb driver
// Added support for setting the interface, preliminary code for naming
// pipes, get string descriptor
//
// ***************** Version 7 *****************
// User: Markm Date: 11/18/97 Time: 4:40p
// Updated in $/EZUSB/ezusb driver
// Added abort pipe IOCTL
// Added function to dump a buffer to the debuger
// Added experimental file I/O code (from Brad Carpenter)
//
// ***************** Version 6 *****************
// User: Markm Date: 11/17/97 Time: 9:37a
// Updated in $/EZUSB/ezusb driver
// fixed bug where I set the pipesize
//
// ***************** Version 5 *****************
// User: Markm Date: 11/14/97 Time: 4:53p
// Updated in $/EZUSB/ezusb driver
// started using USBD_CreateConfigurationRequestEx
//
// ***************** Version 4 *****************
// User: Markm Date: 11/14/97 Time: 4:31p
// Updated in $/EZUSB/ezusb driver
// added code to experiment wth different methods of switiching
// interfaces.
//
// ***************** Version 3 *****************
// User: Markm Date: 11/07/97 Time: 1:23p
// Updated in $/EZUSB/ezusb driver
// added sourcesafe keywords
//
// Copyright (c) 1997 Anchor Chips, Inc. May not be reproduced without
// permission. See the license agreement for more details.
//
//////////////////////////////////////////////////////////////////////
// #define DRIVER
//
// Include files needed for WDM driver support
//
#include <wdm.h>
#include "stdarg.h"
#include "stdio.h"
//
// Include files needed for USB support
//
#include "usbdi.h"
#include "usbdlib.h"
//
// Include file for the Ezusb Device
//
#include "ezusbsys.h"
//
// incude file containing driver version
//
#include "version.h"
#ifdef DOWNLOAD_KEIL_MONITOR
extern INTEL_HEX_RECORD mon_ext_sio1_ezusb[];
extern INTEL_HEX_RECORD mon_ext_sio1_fx2[];
extern INTEL_HEX_RECORD loader[];
#endif // ifdef DOWNLOAD_KEIL_MONITOR
void
DumpBuffer(PVOID pvBuffer, ULONG length);
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This is where the driver is called when the driver is being loaded
by the I/O system. 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;
Ezusb_KdPrint (("entering (Ezusb) DriverEntry (Build: %s/%s\n",__DATE__,__TIME__));
//
// Create dispatch points for the various events handled by this
// driver. For example, device I/O control calls (e.g., when a Win32
// application calls the DeviceIoControl function) will be dispatched to
// routine specified below in the IRP_MJ_DEVICE_CONTROL case.
//
DriverObject->MajorFunction[IRP_MJ_CREATE] = Ezusb_Create;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = Ezusb_Close;
DriverObject->DriverUnload = Ezusb_Unload;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Ezusb_ProcessIOCTL;
DriverObject->MajorFunction[IRP_MJ_PNP] = Ezusb_DispatchPnp;
DriverObject->MajorFunction[IRP_MJ_POWER] = Ezusb_DispatchPower;
DriverObject->DriverExtension->AddDevice = Ezusb_PnPAddDevice;
Ezusb_KdPrint (("exiting (Ezusb) DriverEntry (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS
Ezusb_DefaultPnpHandler(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
{
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoSkipCurrentIrpStackLocation(Irp);
return IoCallDriver(pdx->StackDeviceObject, Irp);
}
///////////////////////////////////////////////////////////////////////////////
// @func Handle completion of a request by a lower-level driver
// @parm Functional device object
// @parm I/O request which has completed
// @parm Context argument supplied to IoSetCompletionRoutine, namely address of
// KEVENT object on which ForwardAndWait is waiting
// @comm This is the completion routine used for requests forwarded by ForwardAndWait. It
// sets the event object and thereby awakens ForwardAndWait.
// @comm Note that it's *not* necessary for this particular completion routine to test
// the PendingReturned flag in the IRP and then call IoMarkIrpPending. You do that in many
// completion routines because the dispatch routine can't know soon enough that the
// lower layer has returned STATUS_PENDING. In our case, we're never going to pass a
// STATUS_PENDING back up the driver chain, so we don't need to worry about this.
NTSTATUS
OnRequestComplete(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp,
IN PKEVENT pev
)
/*++
Routine Description:
Handle completion of a request by a lower-level driver
Arguments:
DriverObject - Functional device object
Irp - I/O request which has completed
pev - Context argument supplied to IoSetCompletionRoutine, namely address of
KEVENT object on which ForwardAndWait is waiting
Return Value:
STATUS_MORE_PROCESSING_REQUIRED
--*/
{
KeSetEvent(pev, 0, FALSE);
return STATUS_MORE_PROCESSING_REQUIRED;
}
NTSTATUS
ForwardAndWait(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
/*++
Routine Description:
Forward request to lower level and await completion
The only purpose of this routine in this particular driver is to pass down
IRP_MN_START_DEVICE requests and wait for the PDO to handle them.
The processor must be at PASSIVE IRQL because this function initializes
and waits for non-zero time on a kernel event object.
Arguments:
fdo - pointer to a device object
Irp - pointer to an I/O Request Packet
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
KEVENT event;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
NTSTATUS ntStatus;
ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
//
// Initialize a kernel event object to use in waiting for the lower-level
// driver to finish processing the object.
//
KeInitializeEvent(&event, NotificationEvent, FALSE);
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) OnRequestComplete,
(PVOID) &event, TRUE, TRUE, TRUE);
ntStatus = IoCallDriver(pdx->StackDeviceObject, Irp);
if (ntStatus == STATUS_PENDING)
{
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
ntStatus = Irp->IoStatus.Status;
}
return ntStatus;
}
NTSTATUS
CompleteRequest(
IN PIRP Irp,
IN NTSTATUS status,
IN ULONG_PTR info
)
/*++
Routine Description:
Mark I/O request complete
Arguments:
Irp - I/O request in question
status - Standard status code
info Additional information related to status code
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = info;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS
Ezusb_DispatchPnp(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
/*++
Routine Description:
Process Plug and Play IRPs sent to this device.
Arguments:
fdo - pointer to a device object
Irp - pointer to an I/O Request Packet
Return Value:
NTSTATUS
--*/
{
PIO_STACK_LOCATION irpStack;
PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
ULONG fcn;
NTSTATUS ntStatus;
Ezusb_KdPrint (("Enter Ezusb_DispatchPnp\n"));
if (!LockDevice(fdo))
return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);
//
// Get a pointer to the current location in the Irp. This is where
// the function codes and parameters are located.
//
irpStack = IoGetCurrentIrpStackLocation (Irp);
ASSERT(irpStack->MajorFunction == IRP_MJ_PNP);
fcn = irpStack->MinorFunction;
switch (fcn)
{
case IRP_MN_START_DEVICE:
Ezusb_KdPrint (("IRP_MN_START_DEVICE\n"));
ntStatus = Ezusb_HandleStartDevice(fdo,Irp);
if (ntStatus == STATUS_SUCCESS)
{
pdx->Started = TRUE;
}
break; //IRP_MN_START_DEVICE
case IRP_MN_STOP_DEVICE:
Ezusb_KdPrint (("IRP_MN_STOP_DEVICE\n"));
//
// first pass the request down the stack
//
Ezusb_DefaultPnpHandler(fdo,Irp);
ntStatus = Ezusb_StopDevice(fdo);
break; //IRP_MN_STOP_DEVICE
case IRP_MN_REMOVE_DEVICE:
Ezusb_KdPrint (("IRP_MN_REMOVE_DEVICE\n"))
ntStatus = Ezusb_HandleRemoveDevice(fdo,Irp);
break; //IRP_MN_REMOVE_DEVICE
case IRP_MN_QUERY_CAPABILITIES:
{
//
// This code swiped from Walter Oney. Please buy his book!!
//
PDEVICE_CAPABILITIES pdc = irpStack->Parameters.DeviceCapabilities.Capabilities;
Ezusb_KdPrint (("IRP_MN_QUERY_CAPABILITIES\n"))
// Check to be sure we know how to handle this version of the capabilities structure
if (pdc->Version < 1)
{
ntStatus = Ezusb_DefaultPnpHandler(fdo, Irp);
break;
}
ntStatus = ForwardAndWait(fdo, Irp);
if (NT_SUCCESS(ntStatus))
{ // IRP succeeded
pdc = irpStack->Parameters.DeviceCapabilities.Capabilities;
// setting this field prevents NT5 from notifying the user when the
// device is removed.
pdc->SurpriseRemovalOK = TRUE;
} // IRP succeeded
ntStatus = CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
}
break; //IRP_MN_QUERY_CAPABILITIES
//
// All other PNP IRP's are just passed down the stack by the default handler
//
default:
Ezusb_KdPrint (("Passing down unhandled PnP IOCTL 0x%x\n", fcn));
ntStatus = Ezusb_DefaultPnpHandler(fdo, Irp);
} // switch MinorFunction
if (fcn != IRP_MN_REMOVE_DEVICE)
UnlockDevice(fdo);
Ezusb_KdPrint (("Exit Ezusb_DispatchPnp %x\n", ntStatus));
return ntStatus;
}//Ezusb_Dispatch
NTSTATUS
Ezusb_DispatchPower(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
/*++
Routine Description:
Process the IRPs sent to this device.
Arguments:
fdo - pointer to a device object
Irp - pointer to an I/O Request Packet
Return Value:
NTSTATUS
--*/
{
PIO_STACK_LOCATION irpStack, nextStack;
PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
NTSTATUS ntStatus;
Ezusb_KdPrint (("Enter Ezusb_DispatchPower\n"));
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);
Ezusb_KdPrint (("IRP_MJ_POWER MIN=0x%x Type=0x%x State=0x%x\n",irpStack->MinorFunction,
irpStack->Parameters.Power.Type,
irpStack->Parameters.Power.State.DeviceState));
switch (irpStack->MinorFunction)
{
case IRP_MN_SET_POWER:
switch (irpStack->Parameters.Power.Type)
{
case SystemPowerState:
break; //SystemPowerState
case DevicePowerState:
switch (irpStack->Parameters.Power.State.DeviceState)
{
case PowerDeviceD3:
Ezusb_KdPrint (("IRP_MN_SET_D3\n"));
break;
case PowerDeviceD2:
Ezusb_KdPrint (("IRP_MN_SET_D2\n"));
break;
case PowerDeviceD1:
Ezusb_KdPrint (("IRP_MN_SET_D1\n"));
break;
case PowerDeviceD0:
Ezusb_KdPrint (("IRP_MN_SET_D0\n"));
break;
} // switch on Power.State.DeviceState
break; //DevicePowerState
}// switch on Power.Type
break; //IRP_MN_SET_POWER
case IRP_MN_QUERY_POWER:
// Look at what type of power query this is
switch (irpStack->Parameters.Power.Type)
{
case SystemPowerState:
break; //SystemPowerState
case DevicePowerState:
switch (irpStack->Parameters.Power.State.DeviceState)
{
case PowerDeviceD2:
Ezusb_KdPrint (("IRP_MN_QUERY_D2\n"));
break;
case PowerDeviceD1:
Ezusb_KdPrint (("IRP_MN_QUERY_D1\n"));
break;
case PowerDeviceD3:
Ezusb_KdPrint (("IRP_MN_QUERY_D3\n"));
break;
} //switch on Power.State.DeviceState
break; //DevicePowerState
}//switch on Power.Type
break; //IRP_MN_QUERY_POWER
default:
// A PnP Minor Function was not handled
Ezusb_KdPrint (("Power IOCTL not handled\n"));
} /* switch MinorFunction*/
nextStack = IoGetNextIrpStackLocation(Irp);
ASSERT(nextStack != NULL);
RtlCopyMemory(nextStack, irpStack, sizeof(IO_STACK_LOCATION));
//
// All PNP_POWER messages get passed to the StackDeviceObject that
// we were given in PnPAddDevice.
//
// This stack device object is managed by the USB software subsystem,
// and so this IRP must be propagated to the owning device driver for
// that stack device object, so that driver in turn can perform any
// device state management (e.g., remove its device object, etc.).
//
Ezusb_KdPrint (("Passing Power Irp down\n"));
//
// Notes on passing power IRPs down: Using IoCallDriver() to pass
// down power IRPs worked until Windows 2000. Using this method
// with Win2K causes a blue screen at system shutdown. Because of this,
// I am modifying the driver to use the more correct PoXXX() functions
// to handle power IRPs. Unfortunately, the PoXXX() calls weren't
// added until the kernel until after the release of Windows 95 OSR2.
// So, a driver using these calls will not load on a Windows 95 system.
// If you need to use this driver under Windows 95. then you must
// #define WIN95.
//
#ifdef WIN95
ntStatus = IoCallDriver(pdx->StackDeviceObject, Irp);
#else
PoStartNextPowerIrp(Irp);
ntStatus = PoCallDriver(pdx->StackDeviceObject,Irp);
#endif
//
// If lower layer driver marked the Irp as pending then reflect that by
// calling IoMarkIrpPending.
//
if (ntStatus == STATUS_PENDING)
{
IoMarkIrpPending(Irp);
Ezusb_KdPrint (("Power Irp came back with STATUS_PENDING (%x)\n", ntStatus));
}
else
{
Ezusb_KdPrint (("Power Irp came back, status = %x\n", ntStatus));
} // if ntStatus is PENDING
goto Ezusb_Dispatch_Done;
Ezusb_Dispatch_Done:
Ezusb_KdPrint (("Exit Ezusb_DispatchPower %x\n", ntStatus));
return ntStatus;
}//Ezusb_DispatchPower
VOID
Ezusb_Unload(
IN PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the allocated resources, etc.
TODO: This is a placeholder for driver writer to add code on unload
Arguments:
DriverObject - pointer to a driver object
Return Value:
None
--*/
{
Ezusb_KdPrint (("enter Ezusb_Unload\n"));
/*
// TODO: Free any global resources allocated in DriverEntry
*/
Ezusb_KdPrint (("exit Ezusb_Unload\n"));
}
NTSTATUS
Ezusb_HandleRemoveDevice(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
ULONG i;
// set the removing flag to prevent any new I/O's
pdx->removing = TRUE;
// brute force - send an abort pipe message to all pipes to cancel any
// pending transfers. this should solve the problem of the driver blocking
// on a REMOVE message because there is a pending transfer.
for (i = 0; i < pdx->Interface->NumberOfPipes; i++)
{
Ezusb_AbortPipe(fdo,(USBD_PIPE_HANDLE) pdx->Interface->Pipes[i].PipeHandle);
}
UnlockDevice(fdo); // once for LockDevice at start of dispatch
UnlockDevice(fdo); // once for initialization during AddDevice
KeWaitForSingleObject(&pdx->evRemove, Executive, KernelMode, FALSE, NULL);
Ezusb_RemoveDevice(fdo);
ntStatus = Ezusb_DefaultPnpHandler(fdo, Irp);
return ntStatus; // lower-level completed IoStatus already
}
NTSTATUS
Ezusb_HandleStartDevice(
IN PDEVICE_OBJECT fdo,
IN PIRP Irp
)
{
NTSTATUS ntStatus;
//
// First let all lower-level drivers handle this request.
//
ntStatus = ForwardAndWait(fdo, Irp);
if (!NT_SUCCESS(ntStatus))
return CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
//
// now do whatever we need to do to start the device
//
ntStatus = Ezusb_StartDevice(fdo);
return CompleteRequest(Irp, ntStatus, 0);
}
NTSTATUS
Ezusb_StartDevice(
IN PDEVICE_OBJECT fdo
)
/*++
Routine Description:
Initializes a given instance of the Ezusb Device on the USB.
Arguments:
fdo - pointer to the device object for this instance of a
Ezusb Device
Return Value:
NT status code
--*/
{
PDEVICE_EXTENSION pdx;
NTSTATUS ntStatus;
PUSB_DEVICE_DESCRIPTOR deviceDescriptor = NULL;
PURB urb;
ULONG siz;
Ezusb_KdPrint (("enter Ezusb_StartDevice\n"));
pdx = fdo->DeviceExtension;
pdx->NeedCleanup = TRUE;
/*
// Get some memory from then non paged pool (fixed, locked system memory)
// for use by the USB Request Block (urb) for the specific USB Request we
// will be performing below (a USB device request).
*/
urb = ExAllocatePool( NonPagedPool,
sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
if (urb) {
siz = sizeof(USB_DEVICE_DESCRIPTOR);
// Get some non paged memory for the device descriptor contents
deviceDescriptor = ExAllocatePool(NonPagedPool,
siz);
if (deviceDescriptor) {
// Use a macro in the standard USB header files to build the URB
UsbBuildGetDescriptorRequest(urb,
(USHORT) sizeof (struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
deviceDescriptor,
NULL,
siz,
NULL);
// Get the device descriptor
ntStatus = Ezusb_CallUSBD(fdo, urb);
// Dump out the descriptor info to the debugger
if (NT_SUCCESS(ntStatus)) {
Ezusb_KdPrint (("Device Descriptor = %x, len %x\n",
deviceDescriptor,
urb->UrbControlDescriptorRequest.TransferBufferLength));
Ezusb_KdPrint (("Ezusb Device Descriptor:\n"));
Ezusb_KdPrint (("-------------------------\n"));
Ezusb_KdPrint (("bLength %d\n", deviceDescriptor->bLength));
Ezusb_KdPrint (("bDescriptorType 0x%x\n", deviceDescriptor->bDescriptorType));
Ezusb_KdPrint (("bcdUSB 0x%x\n", deviceDescriptor->bcdUSB));
Ezusb_KdPrint (("bDeviceClass 0x%x\n", deviceDescriptor->bDeviceClass));
Ezusb_KdPrint (("bDeviceSubClass 0x%x\n", deviceDescriptor->bDeviceSubClass));
Ezusb_KdPrint (("bDeviceProtocol 0x%x\n", deviceDescriptor->bDeviceProtocol));
Ezusb_KdPrint (("bMaxPacketSize0 0x%x\n", deviceDescriptor->bMaxPacketSize0));
Ezusb_KdPrint (("idVendor 0x%x\n", deviceDescriptor->idVendor));
Ezusb_KdPrint (("idProduct 0x%x\n", deviceDescriptor->idProduct));
Ezusb_KdPrint (("bcdDevice 0x%x\n", deviceDescriptor->bcdDevice));
Ezusb_KdPrint (("iManufacturer 0x%x\n", deviceDescriptor->iManufacturer));
Ezusb_KdPrint (("iProduct 0x%x\n", deviceDescriptor->iProduct));
Ezusb_KdPrint (("iSerialNumber 0x%x\n", deviceDescriptor->iSerialNumber));
Ezusb_KdPrint (("bNumConfigurations 0x%x\n", deviceDescriptor->bNumConfigurations));
}
} else {
ntStatus = STATUS_NO_MEMORY;
}
if (NT_SUCCESS(ntStatus)) {
/*
// Put a ptr to the device descriptor in the device extension for easy
// access (like a "cached" copy). We will free this memory when the
// device is removed. See the "Ezusb_RemoveDevice" code.
*/
pdx->DeviceDescriptor = deviceDescriptor;
pdx->Stopped = FALSE;
} else if (deviceDescriptor) {
/*
// If the bus transaction failed, then free up the memory created to hold
// the device descriptor, since the device is probably non-functional
*/