-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Device.cpp
891 lines (716 loc) · 17.7 KB
/
Device.cpp
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
/**************************************************************************
A/V Stream Camera Sample
Copyright (c) 2001, Microsoft Corporation.
File:
device.cpp
Abstract:
This file contains the implementation of CCaptureDevice.
CCaptureDevice is the base device class object. It handles basic PnP
requests and constructs the filter factories.
History:
created 3/9/2001
**************************************************************************/
#include "Common.h"
/**************************************************************************
PAGEABLE CODE
**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
#ifndef DEVPKEY_Device_PanelId
DEFINE_DEVPROPKEY(DEVPKEY_Device_PanelId, 0x8dbc9c86, 0x97a9, 0x4bff, 0x9b, 0xc6, 0xbf, 0xe9, 0x5d, 0x3e, 0x6d, 0xad, 2); // DEVPROP_TYPE_STRING
#endif
CCaptureDevice::
CCaptureDevice (
_In_ PKSDEVICE Device
)
: m_Device (Device)
, m_FilterDescriptorCount(0)
, m_Sensor(nullptr)
, m_Context(nullptr)
{
PAGED_CODE();
}
CCaptureDevice::
~CCaptureDevice()
{
PAGED_CODE();
if( m_Sensor )
{
for( size_t i=0; i<m_FilterDescriptorCount; i++ )
{
SAFE_DELETE( m_Sensor[i] );
}
SAFE_DELETE_ARRAY( m_Sensor );
}
}
NTSTATUS
CCaptureDevice::
Initialize()
{
PAGED_CODE();
m_Sensor = new (NonPagedPoolNx, 'iveD') CSensor *[m_FilterDescriptorCount];
if( !m_Sensor )
{
return STATUS_INSUFFICIENT_RESOURCES;
}
return STATUS_SUCCESS;
}
ULONG
CCaptureDevice::
GetFilterIndex(PKSFILTER Filter)
{
PAGED_CODE();
ULONG i;
for( i=0; i<m_FilterDescriptorCount; i++ )
{
if( Filter->Descriptor->ReferenceGuid &&
IsEqualGUID(*(m_Context[i].Descriptor->ReferenceGuid), *Filter->Descriptor->ReferenceGuid))
{
break;
}
}
// This is a complete failure. We really need to bail.
NT_ASSERT(i<m_FilterDescriptorCount);
return i;
}
NTSTATUS
CCaptureDevice::
IrpSynchronousCompletion(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID pKevent
)
{
PAGED_CODE();
if (Irp->PendingReturned)
{
NT_ASSERT(pKevent);
KeSetEvent((PRKEVENT)pKevent, 0, FALSE);
}
return STATUS_MORE_PROCESSING_REQUIRED;
}
_Must_inspect_result_
NTSTATUS
CCaptureDevice::
QueryForInterface(
_In_ PDEVICE_OBJECT TopOfStack,
_In_ const GUID* InterfaceType,
_Out_ PINTERFACE Interface,
_In_ USHORT Size,
_In_ USHORT Version,
_In_opt_ PVOID InterfaceSpecificData
)
{
PAGED_CODE();
PIRP pIrp;
NTSTATUS status;
if (TopOfStack == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
pIrp = IoAllocateIrp(TopOfStack->StackSize, FALSE);
if (pIrp != NULL)
{
PIO_STACK_LOCATION stack;
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
IoSetCompletionRoutine(pIrp,
IrpSynchronousCompletion,
&event,
TRUE,
TRUE,
TRUE);
pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
stack = IoGetNextIrpStackLocation(pIrp);
stack->MajorFunction = IRP_MJ_PNP;
stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
stack->Parameters.QueryInterface.Interface = Interface;
stack->Parameters.QueryInterface.InterfaceSpecificData = InterfaceSpecificData;
stack->Parameters.QueryInterface.Size = Size;
stack->Parameters.QueryInterface.Version = Version;
stack->Parameters.QueryInterface.InterfaceType = InterfaceType;
status = IoCallDriver(TopOfStack, pIrp);
if (status == STATUS_PENDING)
{
KeWaitForSingleObject(
&event,
Executive,
KernelMode,
FALSE, // Not alertable
NULL
);
status = pIrp->IoStatus.Status;
}
IoFreeIrp(pIrp);
}
else {
status = STATUS_INSUFFICIENT_RESOURCES;
}
return status;
}
NTSTATUS
CCaptureDevice::
Prepare()
{
PAGED_CODE();
NTSTATUS Status = Initialize();
if( !NT_SUCCESS(Status) )
{
return Status;
}
PKSDEVICE Device = m_Device;
//
// Add the item to the object bag if we were successful.
// Whenever the device goes away, the bag is cleaned up and
// we will be freed.
//
// For backwards compatibility with DirectX 8.0, we must grab
// the device mutex before doing this. For Windows XP, this is
// not required, but it is still safe.
//
LockDevice Lock(Device);
Status = KsAddItemToObjectBag (
Device -> Bag,
reinterpret_cast <PVOID> (this),
reinterpret_cast <PFNKSFREE> (CCaptureDevice::Cleanup)
);
if( NT_SUCCESS (Status) )
{
Device->Context=this;
}
return Status;
}
NTSTATUS
CCaptureDevice::
DispatchCreate (
_In_ PKSDEVICE Device
)
/*++
Routine Description:
Create the capture device. This is the creation dispatch for the
capture device.
Arguments:
Device -
The AVStream device being created.
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
DBG_ENTER("()");
CCaptureDevice *CapDevice = new (NonPagedPoolNx, 'eviD') CCaptureDevice (Device);
if( !CapDevice)
{
//
// Return failure if we couldn't create the device.
//
return STATUS_INSUFFICIENT_RESOURCES;
}
NTSTATUS Status = CapDevice->Prepare();
if (!NT_SUCCESS (Status))
{
// Init failure.
delete CapDevice;
}
DBG_LEAVE("()");
return Status;
}
//
// DispatchStart():
//
// This is the Pnp Start dispatch for the capture device. It simply
// bridges to PnpStart() in the context of the CCaptureDevice.
//
NTSTATUS
CCaptureDevice::
DispatchStart (
_In_ PKSDEVICE Device,
_In_ PIRP Irp,
_In_ PCM_RESOURCE_LIST TranslatedResourceList,
_In_ PCM_RESOURCE_LIST UntranslatedResourceList
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->
PnpStart (
TranslatedResourceList,
UntranslatedResourceList
);
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
//
// DispatchStop():
//
// This is the Pnp stop dispatch for the capture device. It simply
// bridges to PnpStop() in the context of the CCaptureDevice.
//
void
CCaptureDevice::
DispatchStop (
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
Recast(Device)->PnpStop(Irp);
DBG_LEAVE("()");
}
NTSTATUS
CCaptureDevice::
DispatchPostStart(
_In_ PKSDEVICE Device
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->PnpPostStart();
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
NTSTATUS
CCaptureDevice::
DispatchQueryStop(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->PnpQueryStop(Irp);
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
void
CCaptureDevice::
DispatchCancelStop(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
Recast(Device)->PnpCancelStop(Irp);
DBG_LEAVE("()");
}
NTSTATUS
CCaptureDevice::
DispatchQueryRemove(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->PnpQueryRemove(Irp);
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
void
CCaptureDevice::
DispatchCancelRemove(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
Recast(Device)->PnpCancelRemove(Irp);
DBG_LEAVE("()");
}
void
CCaptureDevice::
DispatchRemove(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
Recast(Device)->PnpRemove(Irp);
DBG_LEAVE("()");
}
void
CCaptureDevice::
DispatchSurpriseRemoval(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
Recast(Device)->PnpSurpriseRemoval(Irp);
DBG_LEAVE("()");
}
NTSTATUS
CCaptureDevice::
DispatchQueryCapabilities(
_In_ PKSDEVICE Device,
_In_ PIRP Irp,
_Inout_ PDEVICE_CAPABILITIES Capabilities
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->
PnpQueryCapabilities( Irp, Capabilities );
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
// Make Power callbacks non-pageable.
#ifdef ALLOC_PRAGMA
#pragma code_seg()
#endif // ALLOC_PRAGMA
NTSTATUS
CCaptureDevice::
DispatchQueryPower(
_In_ PKSDEVICE Device,
_In_ PIRP Irp,
_In_ DEVICE_POWER_STATE DeviceTo,
_In_ DEVICE_POWER_STATE DeviceFrom,
_In_ SYSTEM_POWER_STATE SystemTo,
_In_ SYSTEM_POWER_STATE SystemFrom,
_In_ POWER_ACTION Action
)
{
// If this device supports the the DO_POWER_INRUSH flag, this function
// can be called at DISPATCH.
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->
PnpQueryPower(
Irp,
DeviceTo,
DeviceFrom,
SystemTo,
SystemFrom,
Action );
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
void
CCaptureDevice::
DispatchSetPower(
_In_ PKSDEVICE Device,
_In_ PIRP Irp,
_In_ DEVICE_POWER_STATE To,
_In_ DEVICE_POWER_STATE From
)
{
// If this device supports the the DO_POWER_INRUSH flag, this function
// can be called at DISPATCH.
DBG_ENTER("()");
Recast(Device)->
PnpSetPower( Irp, To, From );
DBG_LEAVE("()");
}
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
NTSTATUS
CCaptureDevice::
DispatchQueryInterface(
_In_ PKSDEVICE Device,
_In_ PIRP Irp
)
{
PAGED_CODE();
DBG_ENTER("()");
NTSTATUS Status =
Recast(Device)->
PnpQueryInterface(Irp);
DBG_LEAVE("()=0x%08X", Status);
return Status;
}
/*************************************************/
NTSTATUS
CCaptureDevice::
PnpStart (
_In_ PCM_RESOURCE_LIST TranslatedResourceList,
_In_ PCM_RESOURCE_LIST UntranslatedResourceList
)
/*++
Routine Description:
Called at Pnp start. We start up our hardware simulation.
Arguments:
TranslatedResourceList -
The translated resource list from Pnp
UntranslatedResourceList -
The untranslated resource list from Pnp
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
//
// Normally, we'd do things here like parsing the resource lists and
// connecting our interrupt. Since this is a simulation, there isn't
// much to parse. The parsing and connection should be the same as
// any WDM driver. The sections that will differ are illustrated below
// in setting up a simulated DMA.
//
NTSTATUS Status = STATUS_SUCCESS;
//
// By PnP, it's possible to receive multiple starts without an intervening
// stop (to reevaluate resources, for example). Thus, we only perform
// creations of the simulation on the initial start and ignore any
// subsequent start. Hardware drivers with resources should evaluate
// resources and make changes on 2nd start.
//
// Walk the list of descriptors and enumerate the h/w sims described.
//
if (!m_Device -> Started)
{
// Create the Filter for the device
for( size_t i=0; i<m_FilterDescriptorCount; i++ )
{
PKSFILTERFACTORY FilterFactory=nullptr;
IFFAILED_EXIT(
KsCreateFilterFactory( m_Device->FunctionalDeviceObject,
m_Context[i].Descriptor,
m_Context[i].Name,
NULL,
KSCREATE_ITEM_FREEONSTOP,
NULL,
NULL,
&FilterFactory )
);
// Set primary camera to its interface, this allows device to be queried by GUID
if( FilterFactory )
{
PKSDEVICE_PROFILE_INFO pDeviceProfiles = nullptr;
UINT32 uiProfileCount = 0;
PUNICODE_STRING SymbolicLinkName = KsFilterFactoryGetSymbolicLink(FilterFactory);
ACPI_PLD_V2_BUFFER pld = {0};
pld.Revision = 2;
pld.Panel = m_Context[i].AcpiPosition;
pld.Rotation = m_Context[i].AcpiRotation;
pld.CabinetNumber = 0;
WCHAR PanelId[MAX_PATH] = { 0 };
size_t cchDest = MAX_PATH;
IFFAILED_EXIT(
RtlStringCchPrintfW(PanelId, cchDest, L"{00000000-0000-0000-ffff-ffffffffffff}\\%04X\\%u", pld.CabinetNumber, PnpAcpiPanelSideMap[pld.Panel]));
IFFAILED_EXIT(
IoSetDeviceInterfacePropertyData(SymbolicLinkName,
&DEVPKEY_Device_PhysicalDeviceLocation,
LOCALE_NEUTRAL,
PLUGPLAY_PROPERTY_PERSISTENT,
DEVPROP_TYPE_BINARY,
sizeof(pld),
(PVOID)&pld)
);
// Also set Panel ID manually, because PnP does not do this for the interface currently
IoSetDeviceInterfacePropertyData(SymbolicLinkName,
&DEVPKEY_Device_PanelId,
LOCALE_NEUTRAL,
PLUGPLAY_PROPERTY_PERSISTENT,
DEVPROP_TYPE_STRING,
(ULONG)(wcslen(PanelId)+1)*sizeof(WCHAR),
(PVOID)PanelId);
}
// On a real device this object would be constructed whenever the sensor hardware is ready...
m_Sensor[i] = CreateSensor( m_Context[i].Descriptor );
if( !m_Sensor[i] )
{
Status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
IFFAILED_EXIT(m_Sensor[i]->Initialize());
m_Sensor[i]->SetMountingOrientation( m_Context[i].AcpiRotation );
}
}
done:
if( !NT_SUCCESS(Status) )
{
// Free any sensor objects we created during this failed attempt.
for( size_t i=0; i<m_FilterDescriptorCount; i++ )
{
SAFE_DELETE( m_Sensor[i] );
}
}
return Status;
}
CSensor *
CCaptureDevice::
CreateSensor(
_In_ const KSFILTER_DESCRIPTOR *Descriptors
)
{
PAGED_CODE();
return new (NonPagedPoolNx, 'sneS') CSensorSimulation( this, Descriptors );
}
PDEVICE_OBJECT
CCaptureDevice::
GetDeviceObject()
/*++
Routine Description:
Return the pointer to the FDO for our device.
Arguments:
[none]
Return Value:
PDEVICE_OBJECT
--*/
{
PAGED_CODE();
return m_Device->FunctionalDeviceObject;
}
/*************************************************/
void
CCaptureDevice::
PnpStop (
_In_ PIRP Irp
)
/*++
Routine Description:
This is the pnp stop dispatch for the capture device. It releases any
adapter object previously allocated by IoGetDmaAdapter during Pnp Start.
Arguments:
None
Return Value:
None
--*/
{
UNREFERENCED_PARAMETER(Irp);
PAGED_CODE();
if (m_DmaAdapterObject)
{
//
// Return the DMA adapter back to the system.
//
m_DmaAdapterObject -> DmaOperations ->
PutDmaAdapter (m_DmaAdapterObject);
m_DmaAdapterObject = NULL;
}
}
NTSTATUS
CCaptureDevice::
PnpPostStart()
{
PAGED_CODE();
return STATUS_SUCCESS;
}
NTSTATUS
CCaptureDevice::
PnpQueryStop(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
return STATUS_SUCCESS;
}
void
CCaptureDevice::
PnpCancelStop(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
}
NTSTATUS
CCaptureDevice::
PnpQueryRemove(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
return STATUS_SUCCESS;
}
void
CCaptureDevice::
PnpCancelRemove(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
}
void
CCaptureDevice::
PnpRemove(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
}
void
CCaptureDevice::
PnpSurpriseRemoval(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
}
NTSTATUS
CCaptureDevice::
PnpQueryCapabilities(
_In_ PIRP Irp,
_Inout_ PDEVICE_CAPABILITIES Capabilities
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
UNREFERENCED_PARAMETER(Capabilities);
return STATUS_SUCCESS;
}
// Make Power callbacks non-pageable.
#ifdef ALLOC_PRAGMA
#pragma code_seg()
#endif // ALLOC_PRAGMA
NTSTATUS
CCaptureDevice::
PnpQueryPower(
_In_ PIRP Irp,
_In_ DEVICE_POWER_STATE DeviceTo,
_In_ DEVICE_POWER_STATE DeviceFrom,
_In_ SYSTEM_POWER_STATE SystemTo,
_In_ SYSTEM_POWER_STATE SystemFrom,
_In_ POWER_ACTION Action
)
{
UNREFERENCED_PARAMETER(Irp);
UNREFERENCED_PARAMETER(DeviceTo);
UNREFERENCED_PARAMETER(DeviceFrom);
UNREFERENCED_PARAMETER(SystemTo);
UNREFERENCED_PARAMETER(SystemFrom);
UNREFERENCED_PARAMETER(Action);
return STATUS_SUCCESS;
}
void
CCaptureDevice::
PnpSetPower(
_In_ PIRP Irp,
_In_ DEVICE_POWER_STATE To,
_In_ DEVICE_POWER_STATE From
)
{
UNREFERENCED_PARAMETER(Irp);
UNREFERENCED_PARAMETER(To);
UNREFERENCED_PARAMETER(From);
}
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
NTSTATUS
CCaptureDevice::
PnpQueryInterface(
_In_ PIRP Irp
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Irp);
return STATUS_SUCCESS;
}
/*************************************************/