[KS]
[reactos.git] / reactos / drivers / ksfilter / ks / device.c
index 650115f..432548c 100644 (file)
@@ -16,15 +16,29 @@ IKsDevice_fnQueryInterface(
     REFIID refiid,
     PVOID* Output)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    NTSTATUS Status;
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     if (IsEqualGUIDAligned(refiid, &IID_IUnknown))
     {
-        *Output = &This->lpVtblIKsDevice;
+        *Output = &This->BasicHeader.OuterUnknown;
         _InterlockedIncrement(&This->ref);
         return STATUS_SUCCESS;
     }
 
+    if (This->BasicHeader.ClientAggregate)
+    {
+         /* using client aggregate */
+         Status = This->BasicHeader.ClientAggregate->lpVtbl->QueryInterface(This->BasicHeader.ClientAggregate, refiid, Output);
+
+         if (NT_SUCCESS(Status))
+         {
+             /* client aggregate supports interface */
+             return Status;
+         }
+    }
+
+    DPRINT("IKsDevice_fnQueryInterface no interface\n");
     return STATUS_NOT_SUPPORTED;
 }
 
@@ -33,7 +47,7 @@ NTAPI
 IKsDevice_fnAddRef(
     IN IKsDevice * iface)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     return InterlockedIncrement(&This->ref);
 }
@@ -43,7 +57,7 @@ NTAPI
 IKsDevice_fnRelease(
     IN IKsDevice * iface)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     InterlockedDecrement(&This->ref);
 
@@ -57,7 +71,7 @@ NTAPI
 IKsDevice_fnGetStruct(
     IN IKsDevice * iface)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     return &This->KsDevice;
 }
@@ -66,13 +80,26 @@ NTSTATUS
 NTAPI
 IKsDevice_fnInitializeObjectBag(
     IN IKsDevice * iface,
-    IN struct KSIOBJECTBAG *Bag,
-    IN KMUTANT * Mutant)
+    IN PKSIOBJECT_BAG Bag,
+    IN PRKMUTEX Mutex)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
-    UNIMPLEMENTED
-    return STATUS_NOT_IMPLEMENTED;
+    if (!Mutex)
+    {
+        /* use device mutex */
+        Mutex = &This->BagMutex;
+    }
+
+    /* initialize object bag */
+    Bag->BagMutex = Mutex;
+    Bag->DeviceHeader = (PKSIDEVICE_HEADER)This;
+    InitializeListHead(&Bag->ObjectList);
+
+    /* insert bag into device list */
+    InsertTailList(&This->ObjectBags, &Bag->Entry);
+
+    return STATUS_SUCCESS;
 }
 
 NTSTATUS
@@ -80,7 +107,7 @@ NTAPI
 IKsDevice_fnAcquireDevice(
     IN IKsDevice * iface)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     return KeWaitForSingleObject(&This->DeviceMutex, Executive, KernelMode, FALSE, NULL);
 }
@@ -90,7 +117,7 @@ NTAPI
 IKsDevice_fnReleaseDevice(
     IN IKsDevice * iface)
 {
-    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     return KeReleaseMutex(&This->DeviceMutex, FALSE);
 }
@@ -99,14 +126,17 @@ NTSTATUS
 NTAPI
 IKsDevice_fnGetAdapterObject(
     IN IKsDevice * iface,
-    IN PADAPTER_OBJECT Object,
-    IN PULONG Unknown1,
-    IN PULONG Unknown2)
+    IN PADAPTER_OBJECT Object,
+    IN PULONG MaxMappingsByteCount,
+    IN PULONG MappingTableStride)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
-    UNIMPLEMENTED
-    return STATUS_NOT_IMPLEMENTED;
+    *Object = This->AdapterObject;
+    *MaxMappingsByteCount = This->MaxMappingsByteCount;
+    *MappingTableStride = This->MappingTableStride;
+
+    return STATUS_SUCCESS;
 
 }
 
@@ -117,7 +147,7 @@ IKsDevice_fnAddPowerEntry(
     IN struct KSPOWER_ENTRY * Entry,
     IN IKsPowerNotify* Notify)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     UNIMPLEMENTED
     return STATUS_NOT_IMPLEMENTED;
@@ -129,7 +159,7 @@ IKsDevice_fnRemovePowerEntry(
     IN IKsDevice * iface,
     IN struct KSPOWER_ENTRY * Entry)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     UNIMPLEMENTED
     return STATUS_NOT_IMPLEMENTED;
@@ -145,7 +175,7 @@ IKsDevice_fnPinStateChange(
     IN KSSTATE OldState,
     IN KSSTATE NewState)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     UNIMPLEMENTED
     return STATUS_NOT_IMPLEMENTED;
@@ -156,15 +186,24 @@ NTSTATUS
 NTAPI
 IKsDevice_fnArbitrateAdapterChannel(
     IN IKsDevice * iface,
-    IN ULONG ControlCode,
-    IN IO_ALLOCATION_ACTION Action,
+    IN ULONG NumberOfMapRegisters,
+    IN PDRIVER_CONTROL ExecutionRoutine,
     IN PVOID Context)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
+    NTSTATUS Status;
 
-    UNIMPLEMENTED
-    return STATUS_NOT_IMPLEMENTED;
+    DPRINT("IKsDevice_fnArbitrateAdapterChannel NumberOfMapRegisters %lu ExecutionRoutine %p Context %p Irql %lu\n", NumberOfMapRegisters, ExecutionRoutine, Context, KeGetCurrentIrql());
+
+    /* sanity check */
+    ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
+    ASSERT(This->AdapterObject);
 
+    /* allocate adapter channel */
+    Status = IoAllocateAdapterChannel(This->AdapterObject, This->KsDevice.FunctionalDeviceObject, NumberOfMapRegisters, ExecutionRoutine, Context);
+
+    /* done */
+    return Status;
 }
 
 NTSTATUS
@@ -173,7 +212,7 @@ IKsDevice_fnCheckIoCapability(
     IN IKsDevice * iface,
     IN ULONG Unknown)
 {
-    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, lpVtblIKsDevice);
+    //PKSIDEVICE_HEADER This = (PKSIDEVICE_HEADER)CONTAINING_RECORD(iface, KSIDEVICE_HEADER, BasicHeader.OuterUnknown);
 
     UNIMPLEMENTED
     return STATUS_NOT_IMPLEMENTED;
@@ -207,12 +246,10 @@ IKsDevice_PnpPostStart(
     PPNP_POSTSTART_CONTEXT Ctx = (PPNP_POSTSTART_CONTEXT)Context;
 
     /* call driver pnp post routine */
-    Status = Ctx->DeviceHeader->Descriptor->Dispatch->PostStart(&Ctx->DeviceHeader->KsDevice);
+    Status = Ctx->DeviceHeader->KsDevice.Descriptor->Dispatch->PostStart(&Ctx->DeviceHeader->KsDevice);
 
     if (!NT_SUCCESS(Status))
     {
-        DPRINT1("Driver: PostStart Routine returned %x\n", Status);
-
         /* set state to disabled */
         Ctx->DeviceHeader->TargetState  = KSTARGET_STATE_DISABLED;
     }
@@ -220,6 +257,7 @@ IKsDevice_PnpPostStart(
     {
         /* set state to enabled */
         Ctx->DeviceHeader->TargetState = KSTARGET_STATE_ENABLED;
+        Status = KspSetFilterFactoriesState(Ctx->DeviceHeader, TRUE);
     }
 
     /* free work item */
@@ -227,6 +265,8 @@ IKsDevice_PnpPostStart(
 
     /* free work context */
     FreeItem(Ctx);
+
+    DPRINT("IKsDevice_PnpPostStart: PostStart Routine returned %x\n", Status);
 }
 
 NTSTATUS
@@ -240,6 +280,10 @@ IKsDevice_PnpStartDevice(
     PKSIDEVICE_HEADER DeviceHeader;
     PPNP_POSTSTART_CONTEXT Ctx = NULL;
     NTSTATUS Status;
+    PCM_RESOURCE_LIST TranslatedResourceList;
+    PCM_RESOURCE_LIST UntranslatedResourceList;
+    PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor, UnPartialDescriptor;
+    ULONG Index;
 
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
@@ -248,6 +292,8 @@ IKsDevice_PnpStartDevice(
     /* get device header */
     DeviceHeader = DeviceExtension->DeviceHeader;
 
+    DPRINT("IKsDevice_PnpStartDevice DeviceHeader %p\n", DeviceHeader);
+
     /* first forward irp to lower device object */
     Status = KspForwardIrpSynchronous(DeviceObject, Irp);
 
@@ -260,20 +306,55 @@ IKsDevice_PnpStartDevice(
         return Status;
     }
 
+    TranslatedResourceList = IoStack->Parameters.StartDevice.AllocatedResourcesTranslated;
+    UntranslatedResourceList = IoStack->Parameters.StartDevice.AllocatedResources;
+
+    DPRINT("ResourceDescriptorCount %lu\n", TranslatedResourceList->List[0].PartialResourceList.Count);
+    for (Index = 0; Index < TranslatedResourceList->List[0].PartialResourceList.Count; Index ++ )
+    {
+        PartialDescriptor = &TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
+        UnPartialDescriptor = &UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
+        DPRINT("Descriptor Type %u\n", PartialDescriptor->Type);
+
+        if (PartialDescriptor->Type == CmResourceTypeInterrupt)
+        {
+            DPRINT("CmResourceTypeInterrupt Index %u TRANS   Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\n", Index, PartialDescriptor->u.Interrupt.Affinity, PartialDescriptor->u.Interrupt.Level, PartialDescriptor->u.Interrupt.Vector, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition);
+            DPRINT("CmResourceTypeInterrupt Index %u UNTRANS Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\\n", Index, UnPartialDescriptor->u.Interrupt.Affinity, UnPartialDescriptor->u.Interrupt.Level, UnPartialDescriptor->u.Interrupt.Vector, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition);
+
+        }
+        else if (PartialDescriptor->Type == CmResourceTypePort)
+        {
+            DPRINT("CmResourceTypePort Index %u TRANS    Port Length %u Start %u %u Flags %x Share %x\n", Index, PartialDescriptor->u.Port.Length, PartialDescriptor->u.Port.Start.HighPart, PartialDescriptor->u.Port.Start.LowPart, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition);
+            DPRINT("CmResourceTypePort Index %u UNTRANS  Port Length %u Start %u %u Flags %x Share %x\n", Index, UnPartialDescriptor->u.Port.Length, UnPartialDescriptor->u.Port.Start.HighPart, UnPartialDescriptor->u.Port.Start.LowPart, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition);
+        }
+        else if (PartialDescriptor->Type == CmResourceTypeMemory)
+        {
+            DPRINT("CmResourceTypeMemory Index %u TRANS  Start %x Length %u Flags %x Share %x\n", Index, PartialDescriptor->u.Memory.Start.LowPart, PartialDescriptor->u.Memory.Length, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition);
+            DPRINT("CmResourceTypeMemory Index %u TRANS  Start %x Length %u Flags %x Share %x\n", Index, UnPartialDescriptor->u.Memory.Start.LowPart, UnPartialDescriptor->u.Memory.Length, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition);
+        }
+    }
+
+    ASSERT(DeviceHeader->KsDevice.Descriptor);
+    ASSERT(DeviceHeader->KsDevice.Descriptor->Dispatch);
+    ASSERT(DeviceHeader->KsDevice.Descriptor->Dispatch->Start);
+
+
     /* do we have a device descriptor */
-    if (DeviceHeader->Descriptor)
+    if (DeviceHeader->KsDevice.Descriptor)
     {
         /* does the device want pnp notifications */
-        if (DeviceHeader->Descriptor->Dispatch)
+        if (DeviceHeader->KsDevice.Descriptor->Dispatch)
         {
             /* does the driver care about IRP_MN_START_DEVICE */
-            if (DeviceHeader->Descriptor->Dispatch->Start)
+            if (DeviceHeader->KsDevice.Descriptor->Dispatch->Start)
             {
                 /* call driver start device routine */
-                Status = DeviceHeader->Descriptor->Dispatch->Start(&DeviceHeader->KsDevice, Irp,
-                                                                   IoStack->Parameters.StartDevice.AllocatedResourcesTranslated,
-                                                                   IoStack->Parameters.StartDevice.AllocatedResources);
+                Status = DeviceHeader->KsDevice.Descriptor->Dispatch->Start(&DeviceHeader->KsDevice, Irp,
+                                                                   TranslatedResourceList,
+                                                                   UntranslatedResourceList);
 
+
+                DPRINT("IKsDevice_PnpStartDevice Start %p, Context %p\n", DeviceHeader->KsDevice.Descriptor->Dispatch->Start, DeviceHeader->KsDevice.Context);
                 ASSERT(Status != STATUS_PENDING);
 
                 if (!NT_SUCCESS(Status))
@@ -290,7 +371,7 @@ IKsDevice_PnpStartDevice(
             }
 
             /* does the driver need post start routine */
-            if (DeviceHeader->Descriptor->Dispatch->PostStart)
+            if (DeviceHeader->KsDevice.Descriptor->Dispatch->PostStart)
             {
                 /* allocate pnp post workitem context */
                 Ctx = (PPNP_POSTSTART_CONTEXT)AllocateItem(NonPagedPool, sizeof(PNP_POSTSTART_CONTEXT));
@@ -322,6 +403,7 @@ IKsDevice_PnpStartDevice(
             {
                 /* set state to enabled, IRP_MJ_CREATE request may now succeed */
                 DeviceHeader->TargetState = KSTARGET_STATE_ENABLED;
+                Status = KspSetFilterFactoriesState(DeviceHeader, TRUE);
             }
         }
     }
@@ -338,6 +420,7 @@ IKsDevice_PnpStartDevice(
     }
 
     /* return result */
+    DPRINT1("IKsDevice_PnpStartDevice Status %x PostStartRoutine %p\n", Status, Ctx);
     return Status;
 }
 
@@ -362,10 +445,10 @@ IKsDevice_Pnp(
     DeviceHeader = DeviceExtension->DeviceHeader;
 
     /* do we have a device descriptor */
-    if (DeviceHeader->Descriptor)
+    if (DeviceHeader->KsDevice.Descriptor && DeviceHeader->KsDevice.Descriptor->Dispatch)
     {
         /* does the device want pnp notifications */
-        Dispatch = (PKSDEVICE_DISPATCH)DeviceHeader->Descriptor->Dispatch;
+        Dispatch = (PKSDEVICE_DISPATCH)DeviceHeader->KsDevice.Descriptor->Dispatch;
     }
 
     switch (IoStack->MinorFunction)
@@ -435,7 +518,7 @@ IKsDevice_Pnp(
         }
         case IRP_MN_QUERY_INTERFACE:
         {
-            Status = STATUS_SUCCESS;
+            Status = STATUS_UNSUCCESSFUL;
             /* check for pnp notification support */
             if (Dispatch)
             {
@@ -451,6 +534,7 @@ IKsDevice_Pnp(
             if (NT_SUCCESS(Status))
             {
                 /* driver supports a private interface */
+                DPRINT1("IRP_MN_QUERY_INTERFACE Device supports interface\n");
                 Irp->IoStatus.Status = Status;
                 IoCompleteRequest(Irp, IO_NO_INCREMENT);
                 return Status;
@@ -459,44 +543,39 @@ IKsDevice_Pnp(
             /* pass the irp down the driver stack */
             Status = KspForwardIrpSynchronous(DeviceObject, Irp);
 
-            DPRINT("Next Device: Status %x\n", Status);
-
+            DPRINT1("IRP_MN_QUERY_INTERFACE Next Device: Status %x\n", Status);
             Irp->IoStatus.Status = Status;
             IoCompleteRequest(Irp, IO_NO_INCREMENT);
             return Status;
         }
         case IRP_MN_QUERY_DEVICE_RELATIONS:
         {
-            DPRINT("IRP_MN_QUERY_DEVICE_RELATIONS\n");
-
             /* pass the irp down the driver stack */
             Status = KspForwardIrpSynchronous(DeviceObject, Irp);
 
-            DPRINT("Next Device: Status %x\n", Status);
+            DPRINT("IRP_MN_QUERY_DEVICE_RELATIONS Next Device: Status %x\n", Status);
 
-            Irp->IoStatus.Status = Status;
+            //Irp->IoStatus.Status = Status;
             IoCompleteRequest(Irp, IO_NO_INCREMENT);
             return Status;
         }
         case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
         {
-            DPRINT("IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
             /* pass the irp down the driver stack */
-            Status = KspForwardIrpSynchronous(DeviceObject, Irp);
+            //Status = KspForwardIrpSynchronous(DeviceObject, Irp);
+            Status = Irp->IoStatus.Status;
+            DPRINT("IRP_MN_FILTER_RESOURCE_REQUIREMENTS Next Device: Status %x\n", Status);
 
-            DPRINT("Next Device: Status %x\n", Status);
-
-            Irp->IoStatus.Status = Status;
+            //Irp->IoStatus.Status = Status;
             IoCompleteRequest(Irp, IO_NO_INCREMENT);
             return Status;
         }
        case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
        {
-            DPRINT("IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
             /* pass the irp down the driver stack */
             Status = KspForwardIrpSynchronous(DeviceObject, Irp);
 
-            DPRINT("Next Device: Status %x\n", Status);
+            DPRINT("IRP_MN_QUERY_RESOURCE_REQUIREMENTS Next Device: Status %x\n", Status);
 
             Irp->IoStatus.Status = Status;
             IoCompleteRequest(Irp, IO_NO_INCREMENT);
@@ -504,8 +583,12 @@ IKsDevice_Pnp(
        }
        default:
           DPRINT1("unhandled function %u\n", IoStack->MinorFunction);
+          /* pass the irp down the driver stack */
+          Status = KspForwardIrpSynchronous(DeviceObject, Irp);
+
+          Irp->IoStatus.Status = Status;
           IoCompleteRequest(Irp, IO_NO_INCREMENT);
-          return STATUS_NOT_SUPPORTED;
+          return Status;
     }
 }
 
@@ -532,14 +615,14 @@ IKsDevice_Create(
     IN PDEVICE_OBJECT  DeviceObject,
     IN PIRP Irp)
 {
+    PCREATE_ITEM_ENTRY CreateItemEntry;
     PIO_STACK_LOCATION IoStack;
     PDEVICE_EXTENSION DeviceExtension;
     PKSIDEVICE_HEADER DeviceHeader;
-    ULONG Index;
+    PKSIOBJECT_HEADER ObjectHeader;
     NTSTATUS Status;
-    ULONG Length;
 
-    DPRINT("KS / CREATE\n");
+    DPRINT("IKsDevice_Create\n");
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
     /* get device extension */
@@ -548,67 +631,62 @@ IKsDevice_Create(
     DeviceHeader = DeviceExtension->DeviceHeader;
 
     /* acquire list lock */
-    IKsDevice_fnAcquireDevice((IKsDevice*)&DeviceHeader->lpVtblIKsDevice);
+    IKsDevice_fnAcquireDevice((IKsDevice*)&DeviceHeader->BasicHeader.OuterUnknown);
 
     /* sanity check */
     ASSERT(IoStack->FileObject);
 
-    /* loop all device items */
-    for(Index = 0; Index < DeviceHeader->MaxItems; Index++)
+    /* check if the request is relative */
+    if (IoStack->FileObject->RelatedFileObject != NULL)
     {
-        /* is there a create item */
-        if (DeviceHeader->ItemList[Index].CreateItem == NULL)
-            continue;
-
-        /* check if the create item is initialized */
-        if (!DeviceHeader->ItemList[Index].CreateItem->Create)
-            continue;
-
-        ASSERT(DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer);
-        DPRINT("CreateItem %p Request %S\n", DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer,
-                                              IoStack->FileObject->FileName.Buffer);
-
-        /* get object class length */
-        Length = wcslen(DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer);
-        /* now check if the object class is the same */
-        if (!_wcsnicmp(DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer, &IoStack->FileObject->FileName.Buffer[1], Length) ||
-            (DeviceHeader->ItemList[Index].CreateItem->Flags & KSCREATE_ITEM_WILDCARD))
-        {
-            /* setup create parameters */
-            DeviceHeader->DeviceIndex = Index;
-             /* set object create item */
-            KSCREATE_ITEM_IRP_STORAGE(Irp) = DeviceHeader->ItemList[Index].CreateItem;
+        /* request is to instantiate a pin / node / clock / allocator */
+        ObjectHeader = (PKSIOBJECT_HEADER)IoStack->FileObject->RelatedFileObject->FsContext2;
 
-            /* call create function */
-            Status = DeviceHeader->ItemList[Index].CreateItem->Create(DeviceObject, Irp);
+        /* sanity check */
+        ASSERT(ObjectHeader);
 
-            /* release lock */
-            IKsDevice_fnRelease((IKsDevice*)&DeviceHeader->lpVtblIKsDevice);
+        /* find a matching a create item */
+        Status = FindMatchingCreateItem(&ObjectHeader->ItemList, IoStack->FileObject->FileName.Length, IoStack->FileObject->FileName.Buffer, &CreateItemEntry);
+    }
+    else
+    {
+        /* request to create a filter */
+        Status = FindMatchingCreateItem(&DeviceHeader->ItemList, IoStack->FileObject->FileName.Length, IoStack->FileObject->FileName.Buffer, &CreateItemEntry);
+    }
 
-            /* did we succeed */
-            if (NT_SUCCESS(Status))
-            {
-                /* increment create item reference count */
-                InterlockedIncrement((PLONG)&DeviceHeader->ItemList[Index].ReferenceCount);
-            }
+    if (NT_SUCCESS(Status))
+    {
+        /* set object create item */
+        KSCREATE_ITEM_IRP_STORAGE(Irp) = CreateItemEntry->CreateItem;
 
-            /* return result */
-            return Status;
+        /* call create function */
+        Status = CreateItemEntry->CreateItem->Create(DeviceObject, Irp);
+
+        if (NT_SUCCESS(Status))
+        {
+            /* increment create item reference count */
+            InterlockedIncrement(&CreateItemEntry->ReferenceCount);
         }
     }
 
-    /* release lock */
-    IKsDevice_fnRelease((IKsDevice*)&DeviceHeader->lpVtblIKsDevice);
+    /* acquire list lock */
+    IKsDevice_fnReleaseDevice((IKsDevice*)&DeviceHeader->BasicHeader.OuterUnknown);
+
+    if (Status != STATUS_PENDING)
+    {
+        Irp->IoStatus.Information = 0;
+        /* set return status */
+        Irp->IoStatus.Status = Status;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    }
+
+    return Status;
+
 
-    Irp->IoStatus.Information = 0;
-    /* set return status */
-    Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
-    IoCompleteRequest(Irp, IO_NO_INCREMENT);
-    return STATUS_UNSUCCESSFUL;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
 KSDDKAPI
 NTSTATUS
@@ -619,38 +697,98 @@ KsInitializeDevice(
     IN PDEVICE_OBJECT NextDeviceObject,
     IN const KSDEVICE_DESCRIPTOR* Descriptor OPTIONAL)
 {
+    PDEVICE_EXTENSION DeviceExtension;
     PKSIDEVICE_HEADER Header;
     ULONG Index;
+    PKSIOBJECT_BAG Bag;
     NTSTATUS Status = STATUS_SUCCESS;
 
+    DPRINT("KsInitializeDevice Descriptor %p\n", Descriptor);
+
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)FunctionalDeviceObject->DeviceExtension;
+
     /* first allocate device header */
-    Status = KsAllocateDeviceHeader((KSDEVICE_HEADER*)&Header, 0, NULL);
+    Status = KsAllocateDeviceHeader((KSDEVICE_HEADER*)&DeviceExtension->DeviceHeader, 0, NULL);
+
+    /* point to allocated header */
+    Header = DeviceExtension->DeviceHeader;
+
+    DPRINT("DeviceHeader %p\n", DeviceExtension->DeviceHeader);
+
+    if (Descriptor && Descriptor->Dispatch)
+    {
+        DPRINT("Descriptor Add %p\n", Descriptor->Dispatch->Add);
+        DPRINT("Descriptor Start %p\n", Descriptor->Dispatch->Start);
+        DPRINT("Descriptor PostStart %p\n", Descriptor->Dispatch->PostStart);
+        DPRINT("Descriptor QueryStop %p\n", Descriptor->Dispatch->QueryStop);
+        DPRINT("Descriptor CancelStop %p\n", Descriptor->Dispatch->CancelStop);
+        DPRINT("Descriptor Stop %p\n", Descriptor->Dispatch->Stop);
+        DPRINT("Descriptor QueryRemove %p\n", Descriptor->Dispatch->QueryRemove);
+        DPRINT("Descriptor CancelRemove %p\n", Descriptor->Dispatch->CancelRemove);
+        DPRINT("Descriptor Remove %p\n", Descriptor->Dispatch->Remove);
+        DPRINT("Descriptor QueryCapabilities %p\n", Descriptor->Dispatch->QueryCapabilities);
+        DPRINT("Descriptor SurpriseRemoval %p\n", Descriptor->Dispatch->SurpriseRemoval);
+        DPRINT("Descriptor QueryPower %p\n", Descriptor->Dispatch->QueryPower);
+        DPRINT("Descriptor SetPower %p\n", Descriptor->Dispatch->SetPower);
+        DPRINT("Descriptor QueryInterface %p\n", Descriptor->Dispatch->QueryInterface);
+    }
 
     /* check for success */
     if (!NT_SUCCESS(Status))
     {
-        DPRINT1("Failed to allocate device header with %x\n", Status);
+        DPRINT1("KsInitializeDevice Failed to allocate device header with %x\n", Status);
         return Status;
     }
 
+    /* initialize IKsDevice interface */
+    Header->BasicHeader.OuterUnknown = (PUNKNOWN)&vt_IKsDevice;
+    Header->ref = 1;
+
+    /* allocate object bag */
+    Header->KsDevice.Bag = AllocateItem(NonPagedPool, sizeof(KSIOBJECT_BAG));
+    if (!Header->KsDevice.Bag)
+    {
+        /* no memory */
+        KsFreeDeviceHeader((KSDEVICE_HEADER*)&DeviceExtension->DeviceHeader);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    /* initialize object bag */
+    KeInitializeMutex(&Header->BagMutex, 0);
+    KeInitializeMutex(&Header->DeviceMutex, 0);
+
+    Bag = (PKSIOBJECT_BAG)Header->KsDevice.Bag;
+    Bag->BagMutex = &Header->BagMutex;
+    InitializeListHead(&Header->ObjectBags);
+    InitializeListHead(&Bag->ObjectList);
+    Bag->DeviceHeader = (PVOID)Header;
+
+    /* insert bag into device list */
+    InsertTailList(&Header->ObjectBags, &Bag->Entry);
+
     /* initialize device header */
     Header->KsDevice.FunctionalDeviceObject = FunctionalDeviceObject;
     Header->KsDevice.PhysicalDeviceObject = PhysicalDeviceObject;
     Header->KsDevice.NextDeviceObject = NextDeviceObject;
     Header->KsDevice.Descriptor = Descriptor;
+    Header->KsDevice.SystemPowerState = PowerSystemWorking;
+    Header->KsDevice.DevicePowerState = PowerDeviceD0;
+    Header->KsDevice.Started = FALSE;
+    Header->KsDevice.Context = NULL;
     KsSetDevicePnpAndBaseObject(Header, PhysicalDeviceObject, NextDeviceObject);
-    /* initialize IKsDevice interface */
-    Header->lpVtblIKsDevice = &vt_IKsDevice;
-    Header->ref = 1;
 
-    /* FIXME Power state */
+
 
     if (Descriptor)
     {
         /* create a filter factory for each filter descriptor */
+        DPRINT("KsInitializeDevice FilterDescriptorCount %lu\n", Descriptor->FilterDescriptorsCount);
         for(Index = 0; Index < Descriptor->FilterDescriptorsCount; Index++)
         {
             Status = KspCreateFilterFactory(FunctionalDeviceObject, Descriptor->FilterDescriptors[Index], NULL, NULL, 0, NULL, NULL, NULL);
+
+            DPRINT("KsInitializeDevice Index %lu KspCreateFilterFactory Status %lx\n", Index, Status);
             /* check for success */
             if (!NT_SUCCESS(Status))
             {
@@ -667,6 +805,7 @@ KsInitializeDevice(
             Status = Descriptor->Dispatch->Add(&Header->KsDevice);
 
             DPRINT("Driver: AddHandler Status %x\n", Status);
+            Header->KsDevice.Descriptor = Descriptor;
         }
     }
 
@@ -674,5 +813,97 @@ KsInitializeDevice(
    return Status;
 }
 
+/*
+    @implemented
+*/
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsReferenceSoftwareBusObject(
+    IN KSDEVICE_HEADER  Header)
+{
+     IKsDevice * Device;
+     PKSIDEVICE_HEADER DeviceHeader = (PKSIDEVICE_HEADER)Header;
 
+     /* get device interface */
+     Device = (IKsDevice*)DeviceHeader->BasicHeader.OuterUnknown;
 
+     if (Device)
+     {
+         /* reference device interface */
+         Device->lpVtbl->AddRef(Device);
+     }
+
+    return STATUS_SUCCESS;
+}
+
+/*
+    @implemented
+*/
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsReferenceBusObject(
+    IN  KSDEVICE_HEADER Header)
+{
+     IKsDevice * Device;
+     PKSIDEVICE_HEADER DeviceHeader = (PKSIDEVICE_HEADER)Header;
+
+     /* get device interface */
+     Device = (IKsDevice*)DeviceHeader->BasicHeader.OuterUnknown;
+
+     if (Device)
+     {
+         /* reference device interface */
+         Device->lpVtbl->AddRef(Device);
+     }
+
+    return STATUS_SUCCESS;
+
+}
+
+/*
+    @implemented
+*/
+KSDDKAPI
+VOID
+NTAPI
+KsDereferenceBusObject(
+    IN  KSDEVICE_HEADER Header)
+{
+     IKsDevice * Device;
+     PKSIDEVICE_HEADER DeviceHeader = (PKSIDEVICE_HEADER)Header;
+
+     /* get device interface */
+     Device = (IKsDevice*)DeviceHeader->BasicHeader.OuterUnknown;
+
+     if (Device)
+     {
+         /* release device interface */
+         Device->lpVtbl->Release(Device);
+     }
+}
+
+/*
+    @implemented
+*/
+KSDDKAPI
+VOID
+NTAPI
+KsDereferenceSoftwareBusObject(
+    IN KSDEVICE_HEADER  Header)
+{
+     IKsDevice * Device;
+     PKSIDEVICE_HEADER DeviceHeader = (PKSIDEVICE_HEADER)Header;
+
+     DPRINT1("KsDereferenceSoftwareBusObject DeviceHeader %p\n", Header);
+
+     /* get device interface */
+     Device = (IKsDevice*)DeviceHeader->BasicHeader.OuterUnknown;
+
+     if (Device)
+     {
+         /* release device interface */
+         Device->lpVtbl->Release(Device);
+     }
+}