[KS]
[reactos.git] / reactos / drivers / ksfilter / ks / driver.c
index b02c4aa..c0195a4 100644 (file)
@@ -1,12 +1,18 @@
-#include <ntddk.h>
-#include <debug.h>
-#include <ks.h>
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Kernel Streaming
+ * FILE:            drivers/ksfilter/ks/driver.c
+ * PURPOSE:         KS Driver functions
+ * PROGRAMMER:      Johannes Anderwald
+ */
+
+#include "priv.h"
 
 #include "ksfunc.h"
 
 
 /*
-    @unimplemented
+    @implemented
 */
 KSDDKAPI
 PKSDEVICE
@@ -14,23 +20,40 @@ NTAPI
 KsGetDeviceForDeviceObject(
     IN PDEVICE_OBJECT FunctionalDeviceObject)
 {
+    PDEVICE_EXTENSION DeviceExtension;
 
-    return NULL;
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)FunctionalDeviceObject->DeviceExtension;
+
+    return &DeviceExtension->DeviceHeader->KsDevice;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
 KSDDKAPI
-NTSTATUS
+PKSDEVICE
 NTAPI
-KsInitializeDevice(
-    IN PDEVICE_OBJECT FunctionalDeviceObject,
-    IN PDEVICE_OBJECT PhysicalDeviceObject,
-    IN PDEVICE_OBJECT NextDeviceObject,
-    IN const KSDEVICE_DESCRIPTOR* Descriptor OPTIONAL)
+KsGetDevice(
+    IN PVOID Object)
 {
-   return STATUS_UNSUCCESSFUL;
+    PKSBASIC_HEADER BasicHeader = (PKSBASIC_HEADER)((ULONG_PTR)Object - sizeof(KSBASIC_HEADER));
+
+    DPRINT("KsGetDevice Type %lu KsDevice %p\n", BasicHeader->Type, BasicHeader->KsDevice);
+
+    ASSERT(BasicHeader->Type == KsObjectTypeFilterFactory || BasicHeader->Type == KsObjectTypeFilter || BasicHeader->Type == KsObjectTypePin);
+    ASSERT(BasicHeader->KsDevice);
+    ASSERT(BasicHeader->KsDevice->Descriptor);
+    ASSERT(BasicHeader->KsDevice->Bag);
+    ASSERT(BasicHeader->KsDevice->Context);
+    ASSERT(BasicHeader->KsDevice->FunctionalDeviceObject);
+    ASSERT(BasicHeader->KsDevice->PhysicalDeviceObject);
+    ASSERT(BasicHeader->KsDevice->NextDeviceObject);
+    ASSERT(BasicHeader->KsDevice->Started);
+    ASSERT(BasicHeader->KsDevice->SystemPowerState == PowerSystemWorking);
+    ASSERT(BasicHeader->KsDevice->DevicePowerState == PowerDeviceD0);
+
+    return BasicHeader->KsDevice;
 }
 
 /*
@@ -49,9 +72,10 @@ KsCreateDevice(
     NTSTATUS Status = STATUS_DEVICE_REMOVED;
     PDEVICE_OBJECT FunctionalDeviceObject= NULL;
     PDEVICE_OBJECT OldHighestDeviceObject;
-
     if (!ExtensionSize)
-        ExtensionSize = sizeof(PVOID);
+        ExtensionSize = sizeof(KSDEVICE_HEADER);
+
+    DPRINT("KsCreateDevice Descriptor %p ExtensionSize %lu\n", Descriptor, ExtensionSize);
 
     Status = IoCreateDevice(DriverObject, ExtensionSize, NULL, FILE_DEVICE_KS, FILE_DEVICE_SECURE_OPEN, FALSE, &FunctionalDeviceObject);
     if (!NT_SUCCESS(Status))
@@ -62,7 +86,12 @@ KsCreateDevice(
     {
         Status = KsInitializeDevice(FunctionalDeviceObject, PhysicalDeviceObject, OldHighestDeviceObject, Descriptor);
     }
+    else
+    {
+        Status = STATUS_DEVICE_REMOVED;
+    }
 
+    /* check if all succeeded */
     if (!NT_SUCCESS(Status))
     {
         if (OldHighestDeviceObject)
@@ -72,9 +101,20 @@ KsCreateDevice(
         return Status;
     }
 
+    /* set device flags */
+    FunctionalDeviceObject->Flags |= DO_DIRECT_IO | DO_POWER_PAGABLE;
+    FunctionalDeviceObject->Flags &= ~ DO_DEVICE_INITIALIZING;
+
     if (Device)
     {
+        /* get PKSDEVICE struct */
         *Device = KsGetDeviceForDeviceObject(FunctionalDeviceObject);
+
+        if (ExtensionSize > sizeof(KSDEVICE_HEADER))
+        {
+            /* caller needs a device extension */
+            (*Device)->Context = (PVOID)((ULONG_PTR)FunctionalDeviceObject->DeviceExtension + sizeof(KSDEVICE_HEADER));
+        }
     }
 
     return Status;
@@ -90,13 +130,17 @@ KsAddDevice(
     IN  PDRIVER_OBJECT DriverObject,
     IN  PDEVICE_OBJECT PhysicalDeviceObject)
 {
-    PKSDEVICE_DESCRIPTOR *DriverObjectExtension;
-    PKSDEVICE_DESCRIPTOR Descriptor = NULL;
+    PKS_DRIVER_EXTENSION DriverObjectExtension;
+    const KSDEVICE_DESCRIPTOR *Descriptor = NULL;
+
+    /* get stored driver object extension */
+
+    DriverObjectExtension = IoGetDriverObjectExtension(DriverObject, (PVOID)KsInitializeDriver);
 
-    DriverObjectExtension = IoGetDriverObjectExtension(DriverObject, (PVOID)KsAddDevice);
     if (DriverObjectExtension)
     {
-        Descriptor = *DriverObjectExtension;
+        /* get the stored descriptor see KsInitializeDriver */
+        Descriptor = DriverObjectExtension->Descriptor;
     }
 
     return KsCreateDevice(DriverObject, PhysicalDeviceObject, Descriptor, 0, NULL);
@@ -114,27 +158,40 @@ KsInitializeDriver(
     IN const KSDEVICE_DESCRIPTOR  *Descriptor OPTIONAL
 )
 {
-    PKSDEVICE_DESCRIPTOR *DriverObjectExtension;
-    NTSTATUS Status;
+    PKS_DRIVER_EXTENSION DriverObjectExtension;
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    DPRINT("KsInitializeDriver\n");
 
     if (Descriptor)
     {
-        Status = IoAllocateDriverObjectExtension(DriverObject, (PVOID)KsAddDevice, sizeof(PKSDEVICE_DESCRIPTOR), (PVOID*)&DriverObjectExtension);
+        Status = IoAllocateDriverObjectExtension(DriverObject, (PVOID)KsInitializeDriver, sizeof(KS_DRIVER_EXTENSION), (PVOID*)&DriverObjectExtension);
         if (NT_SUCCESS(Status))
         {
-            *DriverObjectExtension = (KSDEVICE_DESCRIPTOR*)Descriptor;
+            DriverObjectExtension->Descriptor = Descriptor;
         }
     }
+
+    /* sanity check */
+    ASSERT(Status == STATUS_SUCCESS);
+
+    if (!NT_SUCCESS(Status))
+        return Status;
+
     /* Setting our IRP handlers */
-    //DriverObject->MajorFunction[IRP_MJ_CREATE] = KspDispatch;
-    //DriverObject->MajorFunction[IRP_MJ_PNP] = KspDispatch;
-    //DriverObject->MajorFunction[IRP_MJ_POWER] = KspDispatch;
+    DriverObject->MajorFunction[IRP_MJ_CREATE] = IKsDevice_Create;
+    DriverObject->MajorFunction[IRP_MJ_PNP] = IKsDevice_Pnp;
+    DriverObject->MajorFunction[IRP_MJ_POWER] = IKsDevice_Power;
+    DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = KsDefaultForwardIrp;
+
+    /* The driver unload routine */
+    DriverObject->DriverUnload = KsNullDriverUnload;
 
     /* The driver-supplied AddDevice */
     DriverObject->DriverExtension->AddDevice = KsAddDevice;
 
     /* KS handles these */
-    DPRINT1("Setting KS function handlers\n");
+    DPRINT1("KsInitializeDriver Setting KS function handlers\n");
     KsSetMajorFunctionHandler(DriverObject, IRP_MJ_CLOSE);
     KsSetMajorFunctionHandler(DriverObject, IRP_MJ_DEVICE_CONTROL);