- Make ks and portcls compile under msvc
[reactos.git] / reactos / drivers / ksfilter / ks / irp.c
index 336f8e7..2603701 100644 (file)
 /*
-    ReactOS Kernel Streaming
-    IRP Helpers
-*/
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Kernel Streaming
+ * FILE:            drivers/ksfilter/ks/factory.c
+ * PURPOSE:         KS Allocator functions
+ * PROGRAMMER:      Johannes Anderwald
+ */
+
 
 #include "priv.h"
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsAcquireResetValue(
-    IN  PIRP Irp,
-    OUT KSRESET* ResetValue)
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsReferenceBusObject(
+    IN  KSDEVICE_HEADER Header)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
 /*
-    @implemented
+    @unimplemented
 */
 KSDDKAPI
 VOID
 NTAPI
-KsAddIrpToCancelableQueue(
-    IN  OUT PLIST_ENTRY QueueHead,
-    IN  PKSPIN_LOCK SpinLock,
-    IN  PIRP Irp,
-    IN  KSLIST_ENTRY_LOCATION ListLocation,
-    IN  PDRIVER_CANCEL DriverCancel OPTIONAL)
+KsDereferenceBusObject(
+    IN  KSDEVICE_HEADER Header)
 {
-    PQUEUE_ENTRY Entry;
-
-    if (!QueueHead || !SpinLock || !Irp)
-        return;
-
-    Entry = ExAllocatePool(NonPagedPool, sizeof(QUEUE_ENTRY));
-    if (!Entry)
-        return;
-
-    ///FIXME
-    // setup cancel routine
-    //
-
-    Entry->Irp = Irp;
-
-    if (ListLocation == KsListEntryTail)
-        ExInterlockedInsertTailList(QueueHead, &Entry->Entry, SpinLock);
-    else
-        ExInterlockedInsertHeadList(QueueHead, &Entry->Entry, SpinLock);
-
+    UNIMPLEMENTED;
 }
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsAddObjectCreateItemToObjectHeader(
-    IN  KSOBJECT_HEADER Header,
-    IN  PDRIVER_DISPATCH Create,
-    IN  PVOID Context,
-    IN  PWCHAR ObjectClass,
-    IN  PSECURITY_DESCRIPTOR SecurityDescriptor)
+KSDDKAPI
+VOID
+NTAPI
+KsDereferenceSoftwareBusObject(
+    IN KSDEVICE_HEADER  Header)
 {
     UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
 }
 
-
 /*
     @implemented
 */
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsAddObjectCreateItemToDeviceHeader(
-    IN  KSDEVICE_HEADER DevHeader,
-    IN  PDRIVER_DISPATCH Create,
-    IN  PVOID Context,
-    IN  PWCHAR ObjectClass,
-    IN  PSECURITY_DESCRIPTOR SecurityDescriptor)
+KsDispatchQuerySecurity(
+    IN PDEVICE_OBJECT DeviceObject,
+    IN PIRP Irp)
 {
-    PKSIDEVICE_HEADER Header;
-    ULONG FreeIndex, Index;
-
-    Header = (PKSIDEVICE_HEADER)DevHeader;
-
-    DPRINT1("KsAddObjectCreateItemToDeviceHeader entered\n");
-
-     /* check if a device header has been provided */
-    if (!DevHeader)
-        return STATUS_INVALID_PARAMETER_1;
+    PKSOBJECT_CREATE_ITEM CreateItem;
+    PIO_STACK_LOCATION IoStack;
+    NTSTATUS Status;
+    ULONG Length;
 
-    /* check if a create item has been provided */
-    if (!Create)
-        return STATUS_INVALID_PARAMETER_2;
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    /* check if a object class has been provided */
-    if (!ObjectClass)
-        return STATUS_INVALID_PARAMETER_4;
+    /* get create item */
+    CreateItem = KSCREATE_ITEM_IRP_STORAGE(Irp);
 
-    FreeIndex = (ULONG)-1;
-    /* now scan the list and check for a free item */
-    for(Index = 0; Index < Header->MaxItems; Index++)
+    if (!CreateItem || !CreateItem->SecurityDescriptor)
     {
-        ASSERT(Header->ItemList[Index].CreateItem);
+        /* no create item */
+        Irp->IoStatus.Status = STATUS_NO_SECURITY_ON_OBJECT;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+        return STATUS_NO_SECURITY_ON_OBJECT;
+    }
 
-        if (Header->ItemList[Index].CreateItem->Create == NULL)
-        {
-            FreeIndex = Index;
-            break;
-        }
 
-        if (!wcsicmp(ObjectClass, Header->ItemList[Index].CreateItem->ObjectClass.Buffer))
-        {
-            /* the same object class already exists */
-            return STATUS_OBJECT_NAME_COLLISION;
-        }
-    }
-    /* found a free index */
-    if (FreeIndex == (ULONG)-1)
-    {
-        /* no empty space found */
-        return STATUS_ALLOTTED_SPACE_EXCEEDED;
-    }
+    /* get input length */
+    Length = IoStack->Parameters.QuerySecurity.Length;
 
-    /* initialize create item */
-    Header->ItemList[FreeIndex].CreateItem->Create = Create;
-    Header->ItemList[FreeIndex].CreateItem->Context = Context;
-    RtlInitUnicodeString(&Header->ItemList[FreeIndex].CreateItem->ObjectClass, ObjectClass);
-    Header->ItemList[FreeIndex].CreateItem->SecurityDescriptor = SecurityDescriptor;
+    /* clone the security descriptor */
+    Status = SeQuerySecurityDescriptorInfo(&IoStack->Parameters.QuerySecurity.SecurityInformation, (PSECURITY_DESCRIPTOR)Irp->UserBuffer, &Length, &CreateItem->SecurityDescriptor);
 
+    DPRINT("SeQuerySecurityDescriptorInfo Status %x\n", Status);
+    /* store result */
+    Irp->IoStatus.Status = Status;
+    Irp->IoStatus.Information = Length;
 
-    return STATUS_SUCCESS;
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    return Status;
 }
 
 /*
@@ -141,327 +97,534 @@ KsAddObjectCreateItemToDeviceHeader(
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsAllocateDeviceHeader(
-    OUT KSDEVICE_HEADER* OutHeader,
-    IN  ULONG ItemsCount,
-    IN  PKSOBJECT_CREATE_ITEM ItemsList OPTIONAL)
+KsDispatchSetSecurity(
+    IN PDEVICE_OBJECT DeviceObject,
+    IN PIRP Irp)
 {
-    ULONG Index = 0;
-    PKSIDEVICE_HEADER Header;
+    PKSOBJECT_CREATE_ITEM CreateItem;
+    PIO_STACK_LOCATION IoStack;
+    PGENERIC_MAPPING Mapping;
+    PSECURITY_DESCRIPTOR Descriptor;
+    NTSTATUS Status;
 
-    if (!OutHeader)
-        return STATUS_INVALID_PARAMETER;
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    /* allocate a device header */
-    Header = ExAllocatePoolWithTag(PagedPool, sizeof(KSIDEVICE_HEADER), TAG_DEVICE_HEADER);
+    /* get create item */
+    CreateItem = KSCREATE_ITEM_IRP_STORAGE(Irp);
 
-    /* check for success */
-    if (!Header)
-        return STATUS_INSUFFICIENT_RESOURCES;
+    if (!CreateItem || !CreateItem->SecurityDescriptor)
+    {
+        /* no create item */
+        Irp->IoStatus.Status = STATUS_NO_SECURITY_ON_OBJECT;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+        return STATUS_NO_SECURITY_ON_OBJECT;
+    }
 
-    /* clear all memory */
-    RtlZeroMemory(Header, sizeof(KSIDEVICE_HEADER));
+    /* backup old descriptor */
+    Descriptor = CreateItem->SecurityDescriptor;
 
-    /* initialize spin lock */
-    KeInitializeSpinLock(&Header->ItemListLock);
+    /* get generic mapping */
+    Mapping = IoGetFileObjectGenericMapping();
 
-    /* are there any create items provided */
-    if (ItemsCount && ItemsList)
+    /* change security descriptor */
+    Status = SeSetSecurityDescriptorInfo(NULL, /*FIXME */
+                                         &IoStack->Parameters.SetSecurity.SecurityInformation,
+                                         IoStack->Parameters.SetSecurity.SecurityDescriptor,
+                                         &CreateItem->SecurityDescriptor,
+                                         NonPagedPool,
+                                         Mapping);
+
+    if (NT_SUCCESS(Status))
     {
-        /* allocate space for device item list */
-        Header->ItemList = ExAllocatePoolWithTag(NonPagedPool, sizeof(DEVICE_ITEM) * ItemsCount, TAG_DEVICE_HEADER);
-        if (!Header->ItemList)
-        {
-            ExFreePoolWithTag(Header, TAG_DEVICE_HEADER);
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        RtlZeroMemory(Header->ItemList, sizeof(DEVICE_ITEM) * ItemsCount);
+        /* free old descriptor */
+        ExFreePool(Descriptor);
 
-        for(Index = 0; Index < ItemsCount; Index++)
-        {
-            /* store provided create items */
-            Header->ItemList[Index].CreateItem = &ItemsList[Index];
-        }
-        Header->MaxItems = ItemsCount;
+       /* mark create item as changed */
+       CreateItem->Flags |= KSCREATE_ITEM_SECURITYCHANGED;
     }
 
     /* store result */
-    *OutHeader = Header;
+    Irp->IoStatus.Status = Status;
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 
-    return STATUS_SUCCESS;
+    return Status;
 }
 
 /*
     @unimplemented
-
-    http://www.osronline.com/DDKx/stream/ksfunc_3sc3.htm
 */
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsAllocateObjectCreateItem(
-    IN  KSDEVICE_HEADER DevHeader,
-    IN  PKSOBJECT_CREATE_ITEM CreateItem,
-    IN  BOOLEAN AllocateEntry,
-    IN  PFNKSITEMFREECALLBACK ItemFreeCallback OPTIONAL)
+KsDispatchSpecificProperty(
+    IN  PIRP Irp,
+    IN  PFNKSHANDLER Handler)
 {
-    PKSIDEVICE_HEADER Header;
-
-    Header = (PKSIDEVICE_HEADER)DevHeader;
-
-    if (!DevHeader)
-        return STATUS_INVALID_PARAMETER_1;
-
-    if (!CreateItem)
-        return STATUS_INVALID_PARAMETER_2;
-
-    //FIXME
-    //handle ItemFreeCallback
-    //
-    if (AllocateEntry && ItemFreeCallback)
-        DPRINT1("Ignoring ItemFreeCallback\n");
-
-    return KsAddObjectCreateItemToDeviceHeader(DevHeader, CreateItem->Create, CreateItem->Context, CreateItem->ObjectClass.Buffer, CreateItem->SecurityDescriptor);
+    UNIMPLEMENTED;
+    return STATUS_UNSUCCESSFUL;
 }
 
-
 /*
-    @implemented
+    @unimplemented
 */
 KSDDKAPI
-VOID
+NTSTATUS
 NTAPI
-KsFreeDeviceHeader(
-    IN  KSDEVICE_HEADER DevHeader)
-{
-    PKSIDEVICE_HEADER Header;
-
-    Header = (PKSIDEVICE_HEADER)DevHeader;
-
-    if (!DevHeader)
-        return;
-
-    ExFreePoolWithTag(Header->ItemList, TAG_DEVICE_HEADER);
-    ExFreePoolWithTag(Header, TAG_DEVICE_HEADER);
-}
-
-/*
-    @unimplemented
-*/
-KSDDKAPI NTSTATUS NTAPI
-KsAllocateExtraData(
+KsDispatchSpecificMethod(
     IN  PIRP Irp,
-    IN  ULONG ExtraSize,
-    OUT PVOID* ExtraBuffer)
+    IN  PFNKSHANDLER Handler)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
-/*
-    @unimplemented
-
-    Initialize the required file context header.
-    Allocates KSOBJECT_HEADER structure.
-    Irp is an IRP_MJ_CREATE structure.
-    Driver must allocate KSDISPATCH_TABLE and initialize it first.
 
-    http://www.osronline.com/DDKx/stream/ksfunc_0u2b.htm
+/*
+    @implemented
 */
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsAllocateObjectHeader(
-    OUT KSOBJECT_HEADER *Header,
-    IN  ULONG ItemsCount,
-    IN  PKSOBJECT_CREATE_ITEM ItemsList OPTIONAL,
-    IN  PIRP Irp,
-    IN  KSDISPATCH_TABLE* Table)
+KsReadFile(
+    IN  PFILE_OBJECT FileObject,
+    IN  PKEVENT Event OPTIONAL,
+    IN  PVOID PortContext OPTIONAL,
+    OUT PIO_STATUS_BLOCK IoStatusBlock,
+    OUT PVOID Buffer,
+    IN  ULONG Length,
+    IN  ULONG Key OPTIONAL,
+    IN  KPROCESSOR_MODE RequestorMode)
 {
-    PIO_STACK_LOCATION IoStack;
-    PDEVICE_EXTENSION DeviceExtension;
-    PKSIDEVICE_HEADER DeviceHeader;
-    PKSIOBJECT_HEADER ObjectHeader;
+    PDEVICE_OBJECT DeviceObject;
+    PIRP Irp;
+    NTSTATUS Status;
+    BOOLEAN Result;
+    KEVENT LocalEvent;
+
+    if (Event)
+    {
+        /* make sure event is reset */
+        KeClearEvent(Event);
+    }
 
-    if (!Header)
-        return STATUS_INVALID_PARAMETER_1;
+    if (RequestorMode == UserMode)
+    {
+        /* probe the user buffer */
+        _SEH2_TRY
+        {
+            ProbeForWrite(Buffer, Length, sizeof(UCHAR));
+            Status = STATUS_SUCCESS;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Exception, get the error code */
+            Status = _SEH2_GetExceptionCode();
+        }
+        _SEH2_END;
 
-    if (!Irp)
-        return STATUS_INVALID_PARAMETER_4;
+         if (!NT_SUCCESS(Status))
+         {
+             DPRINT1("Invalid user buffer provided\n");
+             return Status;
+         }
+    }
 
-    if (!Table)
-        return STATUS_INVALID_PARAMETER_5;
+    /* get corresponding device object */
+    DeviceObject = IoGetRelatedDeviceObject(FileObject);
 
-    /* get current stack location */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
-    /* get device extension */
-    DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
-    /* get device header */
-    DeviceHeader = DeviceExtension->DeviceHeader;
+    /* fast-io read is only available for kernel mode clients */
+    if (RequestorMode == KernelMode && ExGetPreviousMode() == KernelMode &&
+        DeviceObject->DriverObject->FastIoDispatch->FastIoRead)
+    {
+        /* call fast io write */
+        Result = DeviceObject->DriverObject->FastIoDispatch->FastIoRead(FileObject, &FileObject->CurrentByteOffset, Length, TRUE, Key, Buffer, IoStatusBlock, DeviceObject);
 
-    /* sanity check */
-    ASSERT(IoStack->FileObject);
-    /* check for an file object */
+        if (Result && NT_SUCCESS(IoStatusBlock->Status))
+        {
+            /* request was handeled and succeeded */
+            return STATUS_SUCCESS;
+        }
+    }
 
-    /* allocate the object header */
-    ObjectHeader = ExAllocatePoolWithTag(NonPagedPool, sizeof(KSIOBJECT_HEADER), TAG_DEVICE_HEADER);
-    if (!ObjectHeader)
+    /* do the slow way */
+    if (!Event)
+    {
+        /* initialize temp event */
+        KeInitializeEvent(&LocalEvent, NotificationEvent, FALSE);
+        Event = &LocalEvent;
+    }
+
+    /* build the irp packet */
+    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ, DeviceObject, Buffer, Length, &FileObject->CurrentByteOffset, Event, IoStatusBlock);
+    if (!Irp)
+    {
+        /* not enough resources */
         return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
-    /* initialize object header */
-    RtlZeroMemory(ObjectHeader, sizeof(KSIOBJECT_HEADER));
+    /* send the packet */
+    Status = IoCallDriver(DeviceObject, Irp);
 
-    /* do we have a name */
-    if (IoStack->FileObject->FileName.Buffer)
+    if (Status == STATUS_PENDING)
     {
-        /* copy object class */
-        ObjectHeader->ObjectClass.MaximumLength = IoStack->FileObject->FileName.MaximumLength;
-        ObjectHeader->ObjectClass.Buffer = ExAllocatePoolWithTag(NonPagedPool, ObjectHeader->ObjectClass.MaximumLength, TAG_DEVICE_HEADER);
-        if (!ObjectHeader->ObjectClass.Buffer)
+        /* operation is pending, is sync file object */
+        if (FileObject->Flags & FO_SYNCHRONOUS_IO)
         {
-            ExFreePoolWithTag(ObjectHeader, TAG_DEVICE_HEADER);
-            return STATUS_INSUFFICIENT_RESOURCES;
+            /* it is so wait */
+            KeWaitForSingleObject(Event, Executive, RequestorMode, FALSE, NULL);
+            Status = IoStatusBlock->Status;
         }
-        RtlCopyUnicodeString(&ObjectHeader->ObjectClass, &IoStack->FileObject->FileName);
     }
+    /* return result */
+    return Status;
+}
+
+/*
+    @implemented
+*/
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsWriteFile(
+    IN  PFILE_OBJECT FileObject,
+    IN  PKEVENT Event OPTIONAL,
+    IN  PVOID PortContext OPTIONAL,
+    OUT PIO_STATUS_BLOCK IoStatusBlock,
+    IN  PVOID Buffer,
+    IN  ULONG Length,
+    IN  ULONG Key OPTIONAL,
+    IN  KPROCESSOR_MODE RequestorMode)
+{
+    PDEVICE_OBJECT DeviceObject;
+    PIRP Irp;
+    NTSTATUS Status;
+    BOOLEAN Result;
+    KEVENT LocalEvent;
 
-    /* copy dispatch table */
-    RtlCopyMemory(&ObjectHeader->DispatchTable, Table, sizeof(KSDISPATCH_TABLE));
-    /* store create items */
-    if (ItemsCount && ItemsList)
+    if (Event)
     {
-        ObjectHeader->ItemCount = ItemsCount;
-        ObjectHeader->CreateItem = ItemsList;
+        /* make sure event is reset */
+        KeClearEvent(Event);
     }
 
-    /* was the request for a pin/clock/node */
-    if (IoStack->FileObject->FileName.Buffer)
+    if (RequestorMode == UserMode)
     {
-        /* store the object in the file object */
-        ASSERT(IoStack->FileObject->FsContext == NULL);
-        IoStack->FileObject->FsContext = ObjectHeader;
+        /* probe the user buffer */
+        _SEH2_TRY
+        {
+            ProbeForRead(Buffer, Length, sizeof(UCHAR));
+            Status = STATUS_SUCCESS;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            /* Exception, get the error code */
+            Status = _SEH2_GetExceptionCode();
+        }
+        _SEH2_END;
+
+         if (!NT_SUCCESS(Status))
+         {
+             DPRINT1("Invalid user buffer provided\n");
+             return Status;
+         }
     }
-    else
+
+    /* get corresponding device object */
+    DeviceObject = IoGetRelatedDeviceObject(FileObject);
+
+    /* fast-io write is only available for kernel mode clients */
+    if (RequestorMode == KernelMode && ExGetPreviousMode() == KernelMode &&
+        DeviceObject->DriverObject->FastIoDispatch->FastIoWrite)
     {
-        /* the object header is for a audio filter */
-        ASSERT(DeviceHeader->DeviceIndex < DeviceHeader->MaxItems);
-        IoStack->FileObject->FsContext = ObjectHeader;
-    }
+        /* call fast io write */
+        Result = DeviceObject->DriverObject->FastIoDispatch->FastIoWrite(FileObject, &FileObject->CurrentByteOffset, Length, TRUE, Key, Buffer, IoStatusBlock, DeviceObject);
 
-    /* store result */
-    *Header = ObjectHeader;
+        if (Result && NT_SUCCESS(IoStatusBlock->Status))
+        {
+            /* request was handeled and succeeded */
+            return STATUS_SUCCESS;
+        }
+    }
 
+    /* do the slow way */
+    if (!Event)
+    {
+        /* initialize temp event */
+        KeInitializeEvent(&LocalEvent, NotificationEvent, FALSE);
+        Event = &LocalEvent;
+    }
 
-    DPRINT("KsAllocateObjectHeader ObjectClass %S FileObject %p, ObjectHeader %p\n", ObjectHeader->ObjectClass.Buffer, IoStack->FileObject, ObjectHeader);
+    /* build the irp packet */
+    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE, DeviceObject, Buffer, Length, &FileObject->CurrentByteOffset, Event, IoStatusBlock);
+    if (!Irp)
+    {
+        /* not enough resources */
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
-    return STATUS_SUCCESS;
+    /* send the packet */
+    Status = IoCallDriver(DeviceObject, Irp);
 
+    if (Status == STATUS_PENDING)
+    {
+        /* operation is pending, is sync file object */
+        if (FileObject->Flags & FO_SYNCHRONOUS_IO)
+        {
+            /* it is so wait */
+            KeWaitForSingleObject(Event, Executive, RequestorMode, FALSE, NULL);
+            Status = IoStatusBlock->Status;
+        }
+    }
+    /* return result */
+    return Status;
 }
 
 /*
     @unimplemented
 */
 KSDDKAPI
-VOID
+NTSTATUS
 NTAPI
-KsFreeObjectHeader(
-    IN  PVOID Header)
+KsQueryInformationFile(
+    IN  PFILE_OBJECT FileObject,
+    OUT PVOID FileInformation,
+    IN  ULONG Length,
+    IN  FILE_INFORMATION_CLASS FileInformationClass)
 {
+    PDEVICE_OBJECT DeviceObject;
+    PFAST_IO_DISPATCH FastIoDispatch;
+    IO_STATUS_BLOCK IoStatus;
 
+    /* get related file object */
+    DeviceObject = IoGetRelatedDeviceObject(FileObject);
 
-}
+    /* get fast i/o table */
+    FastIoDispatch = DeviceObject->DriverObject->FastIoDispatch;
 
-/*
-    @unimplemented
-*/
-KSDDKAPI VOID NTAPI
-KsCancelIo(
-    IN  OUT PLIST_ENTRY QueueHead,
-    IN  PKSPIN_LOCK SpinLock)
-{
-    UNIMPLEMENTED;
-}
+    /* is there a fast table */
+    if (FastIoDispatch)
+    {
+        /* check the class */
+        if (FileInformationClass == FileBasicInformation)
+        {
+            /* use FastIoQueryBasicInfo routine */
+            if (FastIoDispatch->FastIoQueryBasicInfo)
+            {
+                return FastIoDispatch->FastIoQueryBasicInfo(FileObject, TRUE, (PFILE_BASIC_INFORMATION)FileInformation, &IoStatus, DeviceObject);
+            }
+        }
+        else if (FileInformationClass == FileStandardInformation)
+        {
+            /* use FastIoQueryBasicInfo routine */
+            if (FastIoDispatch->FastIoQueryBasicInfo)
+            {
+                return FastIoDispatch->FastIoQueryStandardInfo(FileObject, TRUE, (PFILE_STANDARD_INFORMATION)FileInformation, &IoStatus, DeviceObject);
+            }
+        }
+    }
 
-/*
-    @unimplemented
-*/
-KSDDKAPI VOID NTAPI
-KsCancelRoutine(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    UNIMPLEMENTED;
-}
+    /* Implement Me */
 
-/*
-    @unimplemented
-*/
-KSDDKAPI NTSTATUS NTAPI
-KsDefaultDeviceIoCompletion(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI BOOLEAN NTAPI
-KsDispatchFastIoDeviceControlFailure(
-    IN  PFILE_OBJECT FileObject,
-    IN  BOOLEAN Wait,
-    IN  PVOID InputBuffer  OPTIONAL,
-    IN  ULONG InputBufferLength,
-    OUT PVOID OutputBuffer  OPTIONAL,
-    IN  ULONG OutputBufferLength,
-    IN  ULONG IoControlCode,
-    OUT PIO_STATUS_BLOCK IoStatus,
-    IN  PDEVICE_OBJECT DeviceObject)   /* always return false */
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsSetInformationFile(
+    IN  PFILE_OBJECT FileObject,
+    IN  PVOID FileInformation,
+    IN  ULONG Length,
+    IN  FILE_INFORMATION_CLASS FileInformationClass)
 {
-    return FALSE;
+    PIO_STACK_LOCATION IoStack;
+    PDEVICE_OBJECT DeviceObject;
+    PIRP Irp;
+    PVOID Buffer;
+    KEVENT Event;
+    LARGE_INTEGER Offset;
+    IO_STATUS_BLOCK IoStatus;
+    NTSTATUS Status;
+
+    /* get related device object */
+    DeviceObject = IoGetRelatedDeviceObject(FileObject);
+
+    /* copy file information */
+    Buffer = AllocateItem(NonPagedPool, Length);
+    if (!Buffer)
+        return STATUS_INSUFFICIENT_RESOURCES;
+
+    _SEH2_TRY
+    {
+        ProbeForRead(Buffer, Length, sizeof(UCHAR));
+        RtlMoveMemory(Buffer, FileInformation, Length);
+        Status = STATUS_SUCCESS;
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        /* Exception, get the error code */
+        Status = _SEH2_GetExceptionCode();
+    }
+    _SEH2_END;
+
+    if (!NT_SUCCESS(Status))
+    {
+        /* invalid user buffer */
+        FreeItem(Buffer);
+        return Status;
+    }
+
+    /* initialize the event */
+    KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
+
+    /* zero offset */
+    Offset.QuadPart = 0LL;
+
+    /* build the irp */
+    Irp = IoBuildSynchronousFsdRequest(IRP_MJ_SET_INFORMATION, DeviceObject, NULL, 0, &Offset, &Event, &IoStatus);
+
+    if (!Irp)
+    {
+        /* failed to allocate irp */
+        FreeItem(Buffer);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    /* get next stack location */
+    IoStack = IoGetNextIrpStackLocation(Irp);
+
+    /* set irp parameters */
+    IoStack->Parameters.SetFile.FileInformationClass = FileInformationClass;
+    IoStack->Parameters.SetFile.Length = Length;
+    IoStack->Parameters.SetFile.FileObject = FileObject;
+    Irp->AssociatedIrp.SystemBuffer = Buffer;
+    Irp->UserBuffer = FileInformation;
+
+    /* dispatch the irp */
+    Status = IoCallDriver(DeviceObject, Irp);
+
+    if (Status == STATUS_PENDING)
+    {
+        /* wait untill the operation has completed */
+        KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
+        /* is a sync file object */
+        if (FileObject->Flags & FO_SYNCHRONOUS_IO)
+            Status = FileObject->FinalStatus;
+        else
+            Status = IoStatus.Status;
+    }
+    /* done */
+    return Status;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI BOOLEAN NTAPI
-KsDispatchFastReadFailure(
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsStreamIo(
     IN  PFILE_OBJECT FileObject,
-    IN  PLARGE_INTEGER FileOffset,
+    IN  PKEVENT Event OPTIONAL,
+    IN  PVOID PortContext OPTIONAL,
+    IN  PIO_COMPLETION_ROUTINE CompletionRoutine OPTIONAL,
+    IN  PVOID CompletionContext OPTIONAL,
+    IN  KSCOMPLETION_INVOCATION CompletionInvocationFlags OPTIONAL,
+    OUT PIO_STATUS_BLOCK IoStatusBlock,
+    IN  OUT PVOID StreamHeaders,
     IN  ULONG Length,
-    IN  BOOLEAN Wait,
-    IN  ULONG LockKey,
-    OUT PVOID Buffer,
-    OUT PIO_STATUS_BLOCK IoStatus,
-    IN  PDEVICE_OBJECT DeviceObject)   /* always return false */
+    IN  ULONG Flags,
+    IN  KPROCESSOR_MODE RequestorMode)
 {
-    return FALSE;
-}
+    PIRP Irp;
+    PIO_STACK_LOCATION IoStack;
+    PDEVICE_OBJECT DeviceObject;
+    ULONG Code;
+    NTSTATUS Status;
+    LARGE_INTEGER Offset;
+    PKSIOBJECT_HEADER ObjectHeader;
 
-/*
-    Used in dispatch table entries that aren't handled and need to return
-    STATUS_INVALID_DEVICE_REQUEST.
-*/
-KSDDKAPI NTSTATUS NTAPI
-KsDispatchInvalidDeviceRequest(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
-    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 
-    return STATUS_INVALID_DEVICE_REQUEST;
+    if (Flags == KSSTREAM_READ)
+        Code = IRP_MJ_READ;
+    else if (Flags == KSSTREAM_WRITE)
+        Code = IRP_MJ_WRITE;
+    else
+        return STATUS_INVALID_PARAMETER;
+
+    DeviceObject = IoGetRelatedDeviceObject(FileObject);
+    if (!DeviceObject)
+        return STATUS_INVALID_PARAMETER;
+
+    if (Event)
+    {
+        KeResetEvent(Event);
+    }
+
+    //ASSERT(DeviceObject->DeviceType == FILE_DEVICE_KS);
+    ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext;
+    ASSERT(ObjectHeader);
+    if (Code == IRP_MJ_READ)
+    {
+        if (ObjectHeader->DispatchTable.FastRead)
+        {
+            if (ObjectHeader->DispatchTable.FastRead(FileObject, NULL, Length, FALSE, 0, StreamHeaders, IoStatusBlock, DeviceObject))
+            {
+                return STATUS_SUCCESS;
+            }
+        }
+    }
+    else
+    {
+        if (ObjectHeader->DispatchTable.FastWrite)
+        {
+            if (ObjectHeader->DispatchTable.FastWrite(FileObject, NULL, Length, FALSE, 0, StreamHeaders, IoStatusBlock, DeviceObject))
+            {
+                return STATUS_SUCCESS;
+            }
+        }
+    }
+
+    Offset.QuadPart = 0LL;
+    Irp = IoBuildSynchronousFsdRequest(Code, DeviceObject, (PVOID)StreamHeaders, Length, &Offset, Event, IoStatusBlock);
+    if (!Irp)
+    {
+        return STATUS_UNSUCCESSFUL;
+    }
+
+
+    if (CompletionRoutine)
+    {
+        IoSetCompletionRoutine(Irp,
+                               CompletionRoutine,
+                               CompletionContext,
+                               (CompletionInvocationFlags & KsInvokeOnSuccess),
+                               (CompletionInvocationFlags & KsInvokeOnError),
+                               (CompletionInvocationFlags & KsInvokeOnCancel));
+    }
+
+    IoStack = IoGetNextIrpStackLocation(Irp);
+    IoStack->FileObject = FileObject;
+
+    Status = IoCallDriver(DeviceObject, Irp);
+    return Status;
 }
 
+
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsDispatchSpecificMethod(
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsProbeStreamIrp(
     IN  PIRP Irp,
-    IN  PFNKSHANDLER Handler)
+    IN  ULONG ProbeFlags,
+    IN  ULONG HeaderSize)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
@@ -470,246 +633,591 @@ KsDispatchSpecificMethod(
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsDispatchSpecificProperty(
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsAllocateExtraData(
     IN  PIRP Irp,
-    IN  PFNKSHANDLER Handler)
+    IN  ULONG ExtraSize,
+    OUT PVOID* ExtraBuffer)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
 /*
-    @unimplemented
+    @implemented
+*/
+KSDDKAPI
+VOID
+NTAPI
+KsNullDriverUnload(
+    IN  PDRIVER_OBJECT DriverObject)
+{
+}
+
+/*
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsForwardAndCatchIrp(
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsDispatchInvalidDeviceRequest(
     IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp,
-    IN  PFILE_OBJECT FileObject,
-    IN  KSSTACK_USE StackUse)
+    IN  PIRP Irp)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
+
+    return STATUS_INVALID_DEVICE_REQUEST;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsForwardIrp(
-    IN  PIRP Irp,
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsDefaultDeviceIoCompletion(
+    IN  PDEVICE_OBJECT DeviceObject,
+    IN  PIRP Irp)
+{
+    PIO_STACK_LOCATION IoStack;
+    NTSTATUS Status;
+
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
+
+    if (IoStack->Parameters.DeviceIoControl.IoControlCode != IOCTL_KS_PROPERTY && 
+        IoStack->Parameters.DeviceIoControl.IoControlCode != IOCTL_KS_METHOD &&
+        IoStack->Parameters.DeviceIoControl.IoControlCode != IOCTL_KS_PROPERTY)
+    {
+        if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_KS_RESET_STATE)
+        {
+            /* fake success */
+            Status = STATUS_SUCCESS;
+        }
+        else
+        {
+            /* request unsupported */
+            Status = STATUS_INVALID_DEVICE_REQUEST;
+        }
+    }
+    else
+    {
+        /* property / method / event not found */
+        Status = STATUS_PROPSET_NOT_FOUND;
+    }
+
+    /* complete request */
+    Irp->IoStatus.Status = Status;
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
+
+
+    return Status;
+}
+
+/*
+    @implemented
+*/
+KSDDKAPI
+BOOLEAN
+NTAPI
+KsDispatchFastIoDeviceControlFailure(
     IN  PFILE_OBJECT FileObject,
-    IN  BOOLEAN ReuseStackLocation)
+    IN  BOOLEAN Wait,
+    IN  PVOID InputBuffer  OPTIONAL,
+    IN  ULONG InputBufferLength,
+    OUT PVOID OutputBuffer  OPTIONAL,
+    IN  ULONG OutputBufferLength,
+    IN  ULONG IoControlCode,
+    OUT PIO_STATUS_BLOCK IoStatus,
+    IN  PDEVICE_OBJECT DeviceObject)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    return FALSE;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsGetChildCreateParameter(
-    IN  PIRP Irp,
-    OUT PVOID* CreateParameter)
+KSDDKAPI
+BOOLEAN
+NTAPI
+KsDispatchFastReadFailure(
+    IN  PFILE_OBJECT FileObject,
+    IN  PLARGE_INTEGER FileOffset,
+    IN  ULONG Length,
+    IN  BOOLEAN Wait,
+    IN  ULONG LockKey,
+    OUT PVOID Buffer,
+    OUT PIO_STATUS_BLOCK IoStatus,
+    IN  PDEVICE_OBJECT DeviceObject)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    return FALSE;
 }
 
+
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsMoveIrpsOnCancelableQueue(
-    IN  OUT PLIST_ENTRY SourceList,
-    IN  PKSPIN_LOCK SourceLock,
-    IN  OUT PLIST_ENTRY DestinationList,
-    IN  PKSPIN_LOCK DestinationLock OPTIONAL,
-    IN  KSLIST_ENTRY_LOCATION ListLocation,
-    IN  PFNKSIRPLISTCALLBACK ListCallback,
-    IN  PVOID Context)
+KSDDKAPI
+VOID
+NTAPI
+KsCancelIo(
+    IN  OUT PLIST_ENTRY QueueHead,
+    IN  PKSPIN_LOCK SpinLock)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    PDRIVER_CANCEL OldDriverCancel;
+    PIO_STACK_LOCATION IoStack;
+    PLIST_ENTRY Entry;
+    PIRP Irp;
+    KIRQL OldLevel;
+
+    /* acquire spinlock */
+    KeAcquireSpinLock(SpinLock, &OldLevel);
+    /* point to first entry */
+    Entry = QueueHead->Flink;
+    /* loop all items */
+    while(Entry != QueueHead)
+    {
+        /* get irp offset */
+        Irp = (PIRP)CONTAINING_RECORD(Entry, IRP, Tail.Overlay.ListEntry);
+
+        /* set cancelled bit */
+        Irp->Cancel = TRUE;
+
+        /* now set the cancel routine */
+        OldDriverCancel = IoSetCancelRoutine(Irp, NULL);
+        if (OldDriverCancel)
+        {
+            /* this irp hasnt been yet used, so free to cancel */
+            KeReleaseSpinLock(SpinLock, OldLevel);
+
+            /* get current irp stack */
+            IoStack = IoGetCurrentIrpStackLocation(Irp);
+
+            /* acquire cancel spinlock */
+            IoAcquireCancelSpinLock(&Irp->CancelIrql);
+
+            /* call provided cancel routine */
+            OldDriverCancel(IoStack->DeviceObject, Irp);
+
+            /* re-acquire spinlock */
+            KeAcquireSpinLock(SpinLock, &OldLevel);
+        }
+        /* move on to next entry */
+        Entry = Entry->Flink;
+    }
+
+    /* the irp has already been canceled */
+    KeReleaseSpinLock(SpinLock, OldLevel);
+
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsProbeStreamIrp(
+KSDDKAPI
+VOID
+NTAPI
+KsReleaseIrpOnCancelableQueue(
     IN  PIRP Irp,
-    IN  ULONG ProbeFlags,
-    IN  ULONG HeaderSize)
+    IN  PDRIVER_CANCEL DriverCancel OPTIONAL)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    PKSPIN_LOCK SpinLock;
+    PDRIVER_CANCEL OldDriverCancel;
+    PIO_STACK_LOCATION IoStack;
+    KIRQL OldLevel;
+
+    /* check for required parameters */
+    if (!Irp)
+        return;
+
+    if (!DriverCancel)
+    {
+        /* default to KsCancelRoutine */
+        DriverCancel = KsCancelRoutine;
+    }
+
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
+
+    /* get internal queue lock */
+    SpinLock = KSQUEUE_SPINLOCK_IRP_STORAGE(Irp);
+
+    /* acquire spinlock */
+    KeAcquireSpinLock(SpinLock, &OldLevel);
+
+    /* now set the cancel routine */
+    OldDriverCancel = IoSetCancelRoutine(Irp, DriverCancel);
+
+    if (Irp->Cancel && OldDriverCancel == NULL)
+    {
+        /* the irp has already been canceled */
+        KeReleaseSpinLock(SpinLock, OldLevel);
+
+        /* cancel routine requires that cancel spinlock is held */
+        IoAcquireCancelSpinLock(&Irp->CancelIrql);
+
+        /* cancel irp */
+        DriverCancel(IoStack->DeviceObject, Irp);
+    }
+    else
+    {
+        /* done */
+        KeReleaseSpinLock(SpinLock, OldLevel);
+    }
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsQueryInformationFile(
-    IN  PFILE_OBJECT FileObject,
-    OUT PVOID FileInformation,
-    IN  ULONG Length,
-    IN  FILE_INFORMATION_CLASS FileInformationClass)
+KSDDKAPI
+PIRP
+NTAPI
+KsRemoveIrpFromCancelableQueue(
+    IN  OUT PLIST_ENTRY QueueHead,
+    IN  PKSPIN_LOCK SpinLock,
+    IN  KSLIST_ENTRY_LOCATION ListLocation,
+    IN  KSIRP_REMOVAL_OPERATION RemovalOperation)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
-}
+    PIRP Irp;
+    PLIST_ENTRY CurEntry;
+    KIRQL OldIrql;
+
+    /* check parameters */
+    if (!QueueHead || !SpinLock)
+        return NULL;
+
+    /* check if parameter ListLocation is valid */
+    if (ListLocation != KsListEntryTail && ListLocation != KsListEntryHead)
+        return NULL;
+
+    /* acquire list lock */
+    KeAcquireSpinLock(SpinLock, &OldIrql);
+
+    /* point to queue head */
+    CurEntry = QueueHead;
+
+    do
+    {
+        /* reset irp to null */
+        Irp = NULL;
+
+        /* iterate to next entry */
+        if (ListLocation == KsListEntryHead)
+            CurEntry = CurEntry->Flink;
+        else
+            CurEntry = CurEntry->Blink;
+
+        /* is the end of list reached */
+        if (CurEntry == QueueHead)
+        {
+            /* reached end of list */
+            break;
+        }
+
+        /* get irp offset */
+        Irp = (PIRP)CONTAINING_RECORD(Irp, IRP, Tail.Overlay.ListEntry);
+
+        if (Irp->Cancel)
+        {
+            /* irp has been canceled */
+            break;
+        }
+
+        if (Irp->CancelRoutine)
+        {
+            /* remove cancel routine */
+            Irp->CancelRoutine = NULL;
+
+            if (RemovalOperation == KsAcquireAndRemove || RemovalOperation == KsAcquireAndRemoveOnlySingleItem)
+            {
+                /* remove irp from list */
+                RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
+            }
+
+            if (RemovalOperation == KsAcquireAndRemoveOnlySingleItem || RemovalOperation == KsAcquireOnlySingleItem)
+                break;
+        }
+
+    }while(TRUE);
+
+    /* release lock */
+    KeReleaseSpinLock(SpinLock, OldIrql);
+
+    if (!Irp || Irp->CancelRoutine == NULL)
+    {
+        /* either an irp has been acquired or nothing found */
+        return Irp;
+    }
 
-/*
-    @unimplemented
-*/
-KSDDKAPI ACCESS_MASK NTAPI
-KsQueryObjectAccessMask(
-    IN KSOBJECT_HEADER Header)
-{
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
-}
+    /* time to remove the canceled irp */
+    IoAcquireCancelSpinLock(&OldIrql);
+    /* acquire list lock */
+    KeAcquireSpinLockAtDpcLevel(SpinLock);
 
-/*
-    @unimplemented
-*/
-KSDDKAPI PKSOBJECT_CREATE_ITEM NTAPI
-KsQueryObjectCreateItem(
-    IN KSOBJECT_HEADER Header)
-{
-    UNIMPLEMENTED;
-/*    return STATUS_UNSUCCESSFUL; */
+    if (RemovalOperation == KsAcquireAndRemove || RemovalOperation == KsAcquireAndRemoveOnlySingleItem)
+    {
+        /* remove it */
+        RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
+    }
+
+    /* release list lock */
+    KeReleaseSpinLockFromDpcLevel(SpinLock);
+
+    /* release cancel spinlock */
+    IoReleaseCancelSpinLock(OldIrql);
+    /* no non canceled irp has been found */
     return NULL;
 }
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsReadFile(
-    IN  PFILE_OBJECT FileObject,
-    IN  PKEVENT Event OPTIONAL,
-    IN  PVOID PortContext OPTIONAL,
-    OUT PIO_STATUS_BLOCK IoStatusBlock,
-    OUT PVOID Buffer,
-    IN  ULONG Length,
-    IN  ULONG Key OPTIONAL,
-    IN  KPROCESSOR_MODE RequestorMode)
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsMoveIrpsOnCancelableQueue(
+    IN  OUT PLIST_ENTRY SourceList,
+    IN  PKSPIN_LOCK SourceLock,
+    IN  OUT PLIST_ENTRY DestinationList,
+    IN  PKSPIN_LOCK DestinationLock OPTIONAL,
+    IN  KSLIST_ENTRY_LOCATION ListLocation,
+    IN  PFNKSIRPLISTCALLBACK ListCallback,
+    IN  PVOID Context)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI VOID NTAPI
-KsReleaseIrpOnCancelableQueue(
-    IN  PIRP Irp,
-    IN  PDRIVER_CANCEL DriverCancel OPTIONAL)
+KSDDKAPI
+VOID
+NTAPI
+KsRemoveSpecificIrpFromCancelableQueue(
+    IN  PIRP Irp)
 {
-    UNIMPLEMENTED;
+    PKSPIN_LOCK SpinLock;
+    KIRQL OldLevel;
+
+    /* get internal queue lock */
+    SpinLock = KSQUEUE_SPINLOCK_IRP_STORAGE(Irp);
+
+    /* acquire spinlock */
+    KeAcquireSpinLock(SpinLock, &OldLevel);
+
+    /* remove the irp from the list */
+    RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
+
+    /* release spinlock */
+    KeReleaseSpinLock(SpinLock, OldLevel);
 }
 
+
 /*
     @implemented
 */
 KSDDKAPI
-PIRP
+VOID
 NTAPI
-KsRemoveIrpFromCancelableQueue(
+KsAddIrpToCancelableQueue(
     IN  OUT PLIST_ENTRY QueueHead,
     IN  PKSPIN_LOCK SpinLock,
+    IN  PIRP Irp,
     IN  KSLIST_ENTRY_LOCATION ListLocation,
-    IN  KSIRP_REMOVAL_OPERATION RemovalOperation)
+    IN  PDRIVER_CANCEL DriverCancel OPTIONAL)
 {
-    PQUEUE_ENTRY Entry = NULL;
-    PIRP Irp;
-    KIRQL OldIrql;
+    PDRIVER_CANCEL OldDriverCancel;
+    PIO_STACK_LOCATION IoStack;
+    KIRQL OldLevel;
 
-    if (!QueueHead || !SpinLock)
-        return NULL;
+    /* check for required parameters */
+    if (!QueueHead || !SpinLock || !Irp)
+        return;
 
-    if (ListLocation != KsListEntryTail && ListLocation != KsListEntryHead)
-        return NULL;
+    if (!DriverCancel)
+    {
+        /* default to KsCancelRoutine */
+        DriverCancel = KsCancelRoutine;
+    }
 
-    if (RemovalOperation != KsAcquireOnly && RemovalOperation != KsAcquireAndRemove)
-        return NULL;
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    KeAcquireSpinLock(SpinLock, &OldIrql);
+    /* acquire spinlock */
+    KeAcquireSpinLock(SpinLock, &OldLevel);
 
-    if (!IsListEmpty(QueueHead))
+    if (ListLocation == KsListEntryTail)
     {
-        if (RemovalOperation == KsAcquireOnly)
-        {
-            if (ListLocation == KsListEntryHead)
-                Entry = (PQUEUE_ENTRY)QueueHead->Flink;
-            else
-                Entry = (PQUEUE_ENTRY)QueueHead->Blink;
-        }
-        else if (RemovalOperation == KsAcquireAndRemove)
-        {
-            if (ListLocation == KsListEntryTail)
-                Entry = (PQUEUE_ENTRY)RemoveTailList(QueueHead);
-            else
-                Entry = (PQUEUE_ENTRY)RemoveHeadList(QueueHead);
-        }
+        /* insert irp to tail of list */
+        InsertTailList(QueueHead, &Irp->Tail.Overlay.ListEntry);
+    }
+    else
+    {
+        /* insert irp to head of list */
+        InsertHeadList(QueueHead, &Irp->Tail.Overlay.ListEntry);
     }
-    KeReleaseSpinLock(SpinLock, OldIrql);
 
-    if (!Entry)
-        return NULL;
+    /* store internal queue lock */
+    KSQUEUE_SPINLOCK_IRP_STORAGE(Irp) = SpinLock;
+
+    /* now set the cancel routine */
+    OldDriverCancel = IoSetCancelRoutine(Irp, DriverCancel);
 
-    Irp = Entry->Irp;
+    if (Irp->Cancel && OldDriverCancel == NULL)
+    {
+        /* the irp has already been canceled */
+        KeReleaseSpinLock(SpinLock, OldLevel);
 
-    if (RemovalOperation == KsAcquireAndRemove)
-        ExFreePool(Entry);
+        /* cancel routine requires that cancel spinlock is held */
+        IoAcquireCancelSpinLock(&Irp->CancelIrql);
 
-    return Irp;
+        /* cancel irp */
+        DriverCancel(IoStack->DeviceObject, Irp);
+    }
+    else
+    {
+        /* done */
+        KeReleaseSpinLock(SpinLock, OldLevel);
+    }
 }
 
 /*
-    @unimplemented
+    @implemented
 */
-KSDDKAPI VOID NTAPI
-KsRemoveSpecificIrpFromCancelableQueue(
+KSDDKAPI
+VOID
+NTAPI
+KsCancelRoutine(
+    IN  PDEVICE_OBJECT DeviceObject,
     IN  PIRP Irp)
 {
-    UNIMPLEMENTED;
+    PKSPIN_LOCK SpinLock;
+    KIRQL OldLevel;
+
+    /* get internal queue lock */
+    SpinLock = KSQUEUE_SPINLOCK_IRP_STORAGE(Irp);
+
+    /* acquire spinlock */
+    KeAcquireSpinLock(SpinLock, &OldLevel);
+
+    /* sanity check */
+    ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
+
+    /* release cancel spinlock */
+    IoReleaseCancelSpinLock(DISPATCH_LEVEL);
+
+    /* remove the irp from the list */
+    RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
+
+    /* release spinlock */
+    KeReleaseSpinLock(SpinLock, OldLevel);
+
+    /* has the irp already been canceled */
+    if (Irp->IoStatus.Status != STATUS_CANCELLED)
+    {
+        /* let's complete it */
+        Irp->IoStatus.Status = STATUS_CANCELLED;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    }
 }
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsSetInformationFile(
-    IN  PFILE_OBJECT FileObject,
-    IN  PVOID FileInformation,
-    IN  ULONG Length,
-    IN  FILE_INFORMATION_CLASS FileInformationClass)
+KSDDKAPI
+NTSTATUS
+NTAPI
+KsGetChildCreateParameter(
+    IN  PIRP Irp,
+    OUT PVOID* CreateParameter)
 {
     UNIMPLEMENTED;
     return STATUS_UNSUCCESSFUL;
 }
 
+NTSTATUS
+FindMatchingCreateItem(
+    PLIST_ENTRY ListHead,
+    ULONG BufferSize,
+    LPWSTR Buffer,
+    OUT PCREATE_ITEM_ENTRY *OutCreateItem)
+{
+    PLIST_ENTRY Entry;
+    PCREATE_ITEM_ENTRY CreateItemEntry;
+
+    /* remove '\' slash */
+    Buffer++;
+    BufferSize -= sizeof(WCHAR);
 
+    /* point to first entry */
+    Entry = ListHead->Flink;
+
+    /* loop all device items */
+    while(Entry != ListHead)
+    {
+        /* get create item entry */
+        CreateItemEntry = (PCREATE_ITEM_ENTRY)CONTAINING_RECORD(Entry, CREATE_ITEM_ENTRY, Entry);
+
+        ASSERT(CreateItemEntry->CreateItem);
+
+        if(CreateItemEntry->CreateItem->Flags & KSCREATE_ITEM_WILDCARD)
+        {
+            /* create item is default */
+            *OutCreateItem = CreateItemEntry;
+            return STATUS_SUCCESS;
+        }
+
+        if (!CreateItemEntry->CreateItem->Create)
+        {
+            /* skip free create item */
+            Entry = Entry->Flink;
+            continue;
+        }
+
+        ASSERT(CreateItemEntry->CreateItem->ObjectClass.Buffer);
+
+        DPRINT1("CreateItem %S Length %u Request %S %u\n", CreateItemEntry->CreateItem->ObjectClass.Buffer,
+                                                           CreateItemEntry->CreateItem->ObjectClass.Length,
+                                                           Buffer,
+                                                           BufferSize);
+
+        if (CreateItemEntry->CreateItem->ObjectClass.Length > BufferSize)
+        {
+            /* create item doesnt match in length */
+            Entry = Entry->Flink;
+            continue;
+        }
+
+         /* now check if the object class is the same */
+        if (RtlCompareMemory(CreateItemEntry->CreateItem->ObjectClass.Buffer, Buffer, CreateItemEntry->CreateItem->ObjectClass.Length) == CreateItemEntry->CreateItem->ObjectClass.Length)
+        {
+            /* found matching create item */
+            *OutCreateItem = CreateItemEntry;
+            return STATUS_SUCCESS;
+        }
+        /* iterate to next */
+        Entry = Entry->Flink;
+    }
+
+    return STATUS_NOT_FOUND;
+}
 
-NTAPI
 NTSTATUS
-KsCreate(
+NTAPI
+KspCreate(
     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;
-    KIRQL OldLevel;
-    ULONG Length;
 
     DPRINT("KS / CREATE\n");
     /* get current stack location */
@@ -719,34 +1227,36 @@ KsCreate(
     /* get device header */
     DeviceHeader = DeviceExtension->DeviceHeader;
 
-    /* acquire list lock */
-    KeAcquireSpinLock(&DeviceHeader->ItemListLock, &OldLevel);
-
-    /* sanity check */
-    ASSERT(IoStack->FileObject);
-
-    if (IoStack->FileObject->FileName.Buffer == NULL && DeviceHeader->MaxItems == 1)
+    if (IoStack->FileObject->FileName.Buffer == NULL && DeviceHeader->ItemListCount == 1)
     {
         /* hack for bug 4566 */
-        if (!DeviceHeader->ItemList[0].CreateItem || !DeviceHeader->ItemList[0].CreateItem->Create)
+        ASSERT(!IsListEmpty(&DeviceHeader->ItemList));
+        /* get create item entry */
+        CreateItemEntry = (PCREATE_ITEM_ENTRY)CONTAINING_RECORD(DeviceHeader->ItemList.Flink, CREATE_ITEM_ENTRY, Entry);
+
+        ASSERT(CreateItemEntry->CreateItem);
+
+        if (!CreateItemEntry->CreateItem->Create)
         {
             /* no valid create item */
             Irp->IoStatus.Information = 0;
             Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
             IoCompleteRequest(Irp, IO_NO_INCREMENT);
-            /* release lock */
-            KeReleaseSpinLock(&DeviceHeader->ItemListLock, OldLevel);
             /* return status */
             return STATUS_UNSUCCESSFUL;
         }
 
         /* set object create item */
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = DeviceHeader->ItemList[0].CreateItem;
+        KSCREATE_ITEM_IRP_STORAGE(Irp) = CreateItemEntry->CreateItem;
 
         /* call create function */
-        Status = DeviceHeader->ItemList[0].CreateItem->Create(DeviceObject, Irp);
-        /* release lock */
-        KeReleaseSpinLock(&DeviceHeader->ItemListLock, OldLevel);
+        Status = CreateItemEntry->CreateItem->Create(DeviceObject, Irp);
+
+        if (NT_SUCCESS(Status))
+        {
+            /* increment create item reference count */
+            InterlockedIncrement(&CreateItemEntry->ReferenceCount);
+        }
         /* return result */
         return Status;
     }
@@ -756,8 +1266,6 @@ KsCreate(
     if (IoStack->FileObject->FileName.Buffer == NULL)
     {
         DPRINT("Using reference string hack\n");
-        /* release lock */
-        KeReleaseSpinLock(&DeviceHeader->ItemListLock, OldLevel);
         Irp->IoStatus.Information = 0;
         /* set return status */
         Irp->IoStatus.Status = STATUS_SUCCESS;
@@ -765,366 +1273,241 @@ KsCreate(
         return STATUS_SUCCESS;
     }
 
-    /* loop all device items */
-    for(Index = 0; Index < DeviceHeader->MaxItems; Index++)
-    {
-        /* 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;
-
-            /* call create function */
-            Status = DeviceHeader->ItemList[Index].CreateItem->Create(DeviceObject, Irp);
-
-            /* release lock */
-            KeReleaseSpinLock(&DeviceHeader->ItemListLock, OldLevel);
-
-            /* return result */
-            return Status;
-        }
-    }
-
-    /* release lock */
-    KeReleaseSpinLock(&DeviceHeader->ItemListLock, OldLevel);
-
-    Irp->IoStatus.Information = 0;
-    /* set return status */
-    Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
-    IoCompleteRequest(Irp, IO_NO_INCREMENT);
-    return STATUS_UNSUCCESSFUL;
-}
-
-static NTAPI
-NTSTATUS
-KsClose(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    PIO_STACK_LOCATION IoStack;
-    PKSIOBJECT_HEADER ObjectHeader;
-    PDEVICE_EXTENSION DeviceExtension;
-    PKSIDEVICE_HEADER DeviceHeader;
-
-    /* get current stack location */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
-    /* get device extension */
-    DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
-    /* get device header */
-    DeviceHeader = DeviceExtension->DeviceHeader;
-
 
-    DPRINT("KS / CLOSE\n");
-
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
+    if (IoStack->FileObject->RelatedFileObject != NULL)
     {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+        /* request is to instantiate a pin / node / clock / allocator */
+        ObjectHeader = (PKSIOBJECT_HEADER)IoStack->FileObject->RelatedFileObject->FsContext;
+
+        /* sanity check */
+        ASSERT(ObjectHeader);
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.Close(DeviceObject, Irp);
+        /* find a matching a create item */
+        Status = FindMatchingCreateItem(&ObjectHeader->ItemList, IoStack->FileObject->FileName.Length, IoStack->FileObject->FileName.Buffer, &CreateItemEntry);
     }
     else
     {
-#if 0
-        DPRINT1("Expected Object Header FileObject %p FsContext %p\n", IoStack->FileObject, IoStack->FileObject->FsContext);
-        KeBugCheckEx(0, 0, 0, 0, 0);
-#else
-        DPRINT("Using reference string hack\n");
-        Irp->IoStatus.Status = STATUS_SUCCESS;
-        Irp->IoStatus.Information = 0;
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
-        return STATUS_SUCCESS;
-#endif
-        return STATUS_SUCCESS;
+        /* request to create a filter */
+        Status = FindMatchingCreateItem(&DeviceHeader->ItemList, IoStack->FileObject->FileName.Length, IoStack->FileObject->FileName.Buffer, &CreateItemEntry);
     }
-}
-
-static NTAPI
-NTSTATUS
-KsDeviceControl(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    PIO_STACK_LOCATION IoStack;
-    PKSIOBJECT_HEADER ObjectHeader;
-    PKSIDEVICE_HEADER DeviceHeader;
-    PDEVICE_EXTENSION DeviceExtension;
-    ULONG Length, Index;
-    LPWSTR Buffer;
-
-    /* get current stack location */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    /* hack for bug 4566 */
-    if (IoStack->MajorFunction == IRP_MJ_DEVICE_CONTROL && IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_KS_OBJECT_CLASS)
+    if (NT_SUCCESS(Status))
     {
-        /* get device extension */
-        DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
-        /* get device header */
-        DeviceHeader = DeviceExtension->DeviceHeader;
-
-        /* retrieve all available reference strings registered */
-        Length = 0;
-
-        for(Index = 0; Index < DeviceHeader->MaxItems; Index++)
-        {
-            if (!DeviceHeader->ItemList[Index].CreateItem || !DeviceHeader->ItemList[Index].CreateItem->Create || !DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer)
-                continue;
-
-            Length += wcslen(DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer) + 1;
-        }
-
-        /* add extra zero */
-        Length += 1;
-
-        /* allocate the buffer */
-        Buffer = ExAllocatePool(NonPagedPool, Length * sizeof(WCHAR));
-        if (!Buffer)
-        {
-            Irp->IoStatus.Information = 0;
-            Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
-            IoCompleteRequest(Irp, IO_NO_INCREMENT);
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
+        /* set object create item */
+        KSCREATE_ITEM_IRP_STORAGE(Irp) = CreateItemEntry->CreateItem;
 
-        *((LPWSTR*)Irp->UserBuffer) = Buffer;
-        Irp->IoStatus.Status = STATUS_SUCCESS;
-        Irp->IoStatus.Information = sizeof(LPWSTR);
+        /* call create function */
+        Status = CreateItemEntry->CreateItem->Create(DeviceObject, Irp);
 
-        for(Index = 0; Index < DeviceHeader->MaxItems; Index++)
+        if (NT_SUCCESS(Status))
         {
-            if (!DeviceHeader->ItemList[Index].CreateItem || !DeviceHeader->ItemList[Index].CreateItem->Create || !DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer)
-                continue;
-
-            wcscpy(Buffer, DeviceHeader->ItemList[Index].CreateItem->ObjectClass.Buffer);
-            Buffer += wcslen(Buffer) + 1;
+            /* increment create item reference count */
+            InterlockedIncrement(&CreateItemEntry->ReferenceCount);
         }
-        *Buffer = L'\0';
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
-        return STATUS_SUCCESS;
+        return Status;
     }
 
-    DPRINT("KS / DeviceControl\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
-    {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
-
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
 
-        return ObjectHeader->DispatchTable.DeviceIoControl(DeviceObject, Irp);
-    }
-    else
-    {
-        DPRINT1("Expected Object Header\n");
-        KeBugCheckEx(0, 0, 0, 0, 0);
-        return STATUS_SUCCESS;
-    }
+    Irp->IoStatus.Information = 0;
+    /* set return status */
+    Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    return STATUS_UNSUCCESSFUL;
 }
 
-static NTAPI
 NTSTATUS
-KsRead(
+RosDeviceInterfaceReferenceStringHack(
     IN  PDEVICE_OBJECT DeviceObject,
     IN  PIRP Irp)
 {
     PIO_STACK_LOCATION IoStack;
-    PKSIOBJECT_HEADER ObjectHeader;
+    PKSIDEVICE_HEADER DeviceHeader;
+    PDEVICE_EXTENSION DeviceExtension;
+    PCREATE_ITEM_ENTRY CreateItemEntry;
+    PLIST_ENTRY Entry;
+    LPWSTR Buffer;
+    ULONG Length;
 
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    DPRINT("KS / Read\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
+    /* get device header */
+    DeviceHeader = DeviceExtension->DeviceHeader;
+
+    /* retrieve all available reference strings registered */
+    Length = 0;
+    Entry = DeviceHeader->ItemList.Flink;
+    while(Entry != &DeviceHeader->ItemList)
     {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+        CreateItemEntry = (PCREATE_ITEM_ENTRY)CONTAINING_RECORD(Entry, CREATE_ITEM_ENTRY, Entry);
+
+        ASSERT(CreateItemEntry->CreateItem);
+        if (CreateItemEntry->CreateItem->Create && CreateItemEntry->CreateItem->ObjectClass.Buffer)
+            Length += wcslen(CreateItemEntry->CreateItem->ObjectClass.Buffer) + 1;
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.Read(DeviceObject, Irp);
+        Entry = Entry->Flink;
     }
-    else
+
+    /* add extra zero */
+    Length += 1;
+
+    /* allocate the buffer */
+    Buffer = ExAllocatePool(NonPagedPool, Length * sizeof(WCHAR));
+    if (!Buffer)
     {
-        DPRINT1("Expected Object Header\n");
-        KeBugCheckEx(0, 0, 0, 0, 0);
-        return STATUS_SUCCESS;
+        Irp->IoStatus.Information = 0;
+        Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+        return STATUS_INSUFFICIENT_RESOURCES;
     }
-}
 
-static NTAPI
-NTSTATUS
-KsWrite(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    PIO_STACK_LOCATION IoStack;
-    PKSIOBJECT_HEADER ObjectHeader;
 
-    /* get current stack location */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
+    *((LPWSTR*)Irp->UserBuffer) = Buffer;
+    Irp->IoStatus.Status = STATUS_SUCCESS;
+    Irp->IoStatus.Information = sizeof(LPWSTR);
 
-    DPRINT("KS / Write\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
+    Entry = DeviceHeader->ItemList.Flink;
+    while(Entry != &DeviceHeader->ItemList)
     {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+        CreateItemEntry = (PCREATE_ITEM_ENTRY)CONTAINING_RECORD(Entry, CREATE_ITEM_ENTRY, Entry);
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.Write(DeviceObject, Irp);
-    }
-    else
-    {
-        DPRINT1("Expected Object Header %p\n", IoStack->FileObject);
-        KeBugCheckEx(0, 0, 0, 0, 0);
-        return STATUS_SUCCESS;
+        ASSERT(CreateItemEntry->CreateItem);
+        if (CreateItemEntry->CreateItem->Create && CreateItemEntry->CreateItem->ObjectClass.Buffer)
+        {
+            wcscpy(Buffer, CreateItemEntry->CreateItem->ObjectClass.Buffer);
+            Buffer += wcslen(Buffer) + 1;
+        }
+        Entry = Entry->Flink;
     }
-}
 
-static NTAPI
-NTSTATUS
-KsFlushBuffers(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
-{
-    PIO_STACK_LOCATION IoStack;
-    PKSIOBJECT_HEADER ObjectHeader;
 
-    /* get current stack location */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    DPRINT("KS / FlushBuffers\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
-    {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+    *Buffer = L'\0';
+    IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    return STATUS_SUCCESS;
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.Flush(DeviceObject, Irp);
-    }
-    else
-    {
-        DPRINT1("Expected Object Header\n");
-        KeBugCheckEx(0, 0, 0, 0, 0);
-        return STATUS_SUCCESS;
-    }
 }
 
-static NTAPI
 NTSTATUS
-KsQuerySecurity(
+NTAPI
+KspDeviceControl(
     IN  PDEVICE_OBJECT DeviceObject,
     IN  PIRP Irp)
 {
     PIO_STACK_LOCATION IoStack;
     PKSIOBJECT_HEADER ObjectHeader;
+    PKSIDEVICE_HEADER DeviceHeader;
+    PDEVICE_EXTENSION DeviceExtension;
+
+
 
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    DPRINT("KS / QuerySecurity\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
-    {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
+    /* get device header */
+    DeviceHeader = DeviceExtension->DeviceHeader;
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.QuerySecurity(DeviceObject, Irp);
-    }
-    else
+    if (IoStack->MajorFunction == IRP_MJ_DEVICE_CONTROL && IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_KS_OBJECT_CLASS)
     {
-        DPRINT1("Expected Object Header\n");
-        KeBugCheckEx(0, 0, 0, 0, 0);
-        return STATUS_SUCCESS;
+        /* hack for bug 4566 */
+        return RosDeviceInterfaceReferenceStringHack(DeviceObject, Irp);
     }
+
+    ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+
+    ASSERT(ObjectHeader);
+    //KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
+
+    return ObjectHeader->DispatchTable.DeviceIoControl(DeviceObject, Irp);
 }
 
-static NTAPI
 NTSTATUS
-KsSetSecurity(
+NTAPI
+KspDispatchIrp(
     IN  PDEVICE_OBJECT DeviceObject,
     IN  PIRP Irp)
 {
     PIO_STACK_LOCATION IoStack;
+    PDEVICE_EXTENSION DeviceExtension;
     PKSIOBJECT_HEADER ObjectHeader;
+    PKSIDEVICE_HEADER DeviceHeader;
+    PDRIVER_DISPATCH Dispatch;
+    NTSTATUS Status;
 
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    DPRINT("KS / SetSecurity\n");
-    if (IoStack->FileObject && IoStack->FileObject->FsContext)
-    {
-        ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    /* get device header */
+    DeviceHeader = DeviceExtension->DeviceHeader;
 
-        KSCREATE_ITEM_IRP_STORAGE(Irp) = ObjectHeader->CreateItem;
-        return ObjectHeader->DispatchTable.SetSecurity(DeviceObject, Irp);
-    }
-    else
+    /* get object header */
+    ObjectHeader = (PKSIOBJECT_HEADER) IoStack->FileObject->FsContext;
+
+    if (!ObjectHeader)
     {
-        DPRINT1("Expected Object Header\n");
-        KeBugCheckEx(0, 0, 0, 0, 0);
+        /* hack for bug 4566 */
+        Irp->IoStatus.Status = STATUS_SUCCESS;
+        Irp->IoStatus.Information = 0;
+        /* complete and forget */
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
         return STATUS_SUCCESS;
     }
-}
-
-/*
-    @implemented
-*/
-KSDDKAPI NTSTATUS NTAPI
-KsSetMajorFunctionHandler(
-    IN  PDRIVER_OBJECT DriverObject,
-    IN  ULONG MajorFunction)
-{
-    /*
-        Sets a DriverObject's major function handler to point to an internal
-        function we implement.
 
-        TODO: Deal with KSDISPATCH_FASTIO
-    */
+    /* sanity check */
+    ASSERT(ObjectHeader);
+    /* store create item */
+    //KSCREATE_ITEM_IRP_STORAGE(Irp) = (PKSOBJECT_CREATE_ITEM)0x12345678; //ObjectHeader->CreateItem;
 
-    switch ( MajorFunction )
+    /* retrieve matching dispatch function */
+    switch(IoStack->MajorFunction)
     {
-        case IRP_MJ_CREATE:
-            DriverObject->MajorFunction[MajorFunction] = KsCreate;
-            break;
         case IRP_MJ_CLOSE:
-            DriverObject->MajorFunction[MajorFunction] = KsClose;
+            Dispatch = ObjectHeader->DispatchTable.Close;
             break;
         case IRP_MJ_DEVICE_CONTROL:
-            DriverObject->MajorFunction[MajorFunction] = KsDeviceControl;
+            Dispatch = ObjectHeader->DispatchTable.DeviceIoControl;
             break;
         case IRP_MJ_READ:
-            DriverObject->MajorFunction[MajorFunction] = KsRead;
+            Dispatch = ObjectHeader->DispatchTable.Read;
             break;
         case IRP_MJ_WRITE:
-            DriverObject->MajorFunction[MajorFunction] = KsWrite;
+            Dispatch = ObjectHeader->DispatchTable.Write;
             break;
         case IRP_MJ_FLUSH_BUFFERS :
-            DriverObject->MajorFunction[MajorFunction] = KsFlushBuffers;
+            Dispatch = ObjectHeader->DispatchTable.Flush;
             break;
         case IRP_MJ_QUERY_SECURITY:
-            DriverObject->MajorFunction[MajorFunction] = KsQuerySecurity;
+            Dispatch = ObjectHeader->DispatchTable.QuerySecurity;
             break;
         case IRP_MJ_SET_SECURITY:
-            DriverObject->MajorFunction[MajorFunction] = KsSetSecurity;
+            Dispatch = ObjectHeader->DispatchTable.SetSecurity;
             break;
-
+        case IRP_MJ_PNP:
+            Dispatch = KsDefaultDispatchPnp;
         default:
-            return STATUS_INVALID_PARAMETER;    /* is this right? */
-    };
+            Dispatch = NULL;
+    }
 
-    return STATUS_SUCCESS;
+    /* is the request supported */
+    if (Dispatch)
+    {
+        /* now call the dispatch function */
+        Status = Dispatch(DeviceObject, Irp);
+    }
+    else
+    {
+        /* not supported request */
+        Status = KsDispatchInvalidDeviceRequest(DeviceObject, Irp);
+    }
+
+    /* done */
+    return Status;
 }
 
 /*
@@ -1133,52 +1516,33 @@ KsSetMajorFunctionHandler(
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsDispatchIrp(
-    IN  PDEVICE_OBJECT DeviceObject,
-    IN  PIRP Irp)
+KsSetMajorFunctionHandler(
+    IN  PDRIVER_OBJECT DriverObject,
+    IN  ULONG MajorFunction)
 {
-    PIO_STACK_LOCATION IoStack;
-
-    /* Calls a dispatch routine corresponding to the function code of the IRP */
-    /*
-        First we need to get the dispatch table. An opaque header is pointed to by
-        FsContext. The first element points to this table. This table is the key
-        to dispatching the IRP correctly.
-    */
-
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
-    DPRINT("KsDispatchIrp %x\n", IoStack->MajorFunction);
-
-    switch (IoStack->MajorFunction)
+    switch ( MajorFunction )
     {
         case IRP_MJ_CREATE:
-            return KsCreate(DeviceObject, Irp);
-        case IRP_MJ_CLOSE:
-            return KsClose(DeviceObject, Irp);
+            DriverObject->MajorFunction[MajorFunction] = KspCreate;
+            break;
             break;
         case IRP_MJ_DEVICE_CONTROL:
-            return KsDeviceControl(DeviceObject, Irp);
+            DriverObject->MajorFunction[MajorFunction] = KspDeviceControl;
             break;
+        case IRP_MJ_CLOSE:
         case IRP_MJ_READ:
-            return KsRead(DeviceObject, Irp);
-            break;
         case IRP_MJ_WRITE:
-            return KsWrite(DeviceObject, Irp);
-            break;
-        case IRP_MJ_FLUSH_BUFFERS:
-            return KsFlushBuffers(DeviceObject, Irp);
-            break;
+        case IRP_MJ_FLUSH_BUFFERS :
         case IRP_MJ_QUERY_SECURITY:
-            return KsQuerySecurity(DeviceObject, Irp);
-            break;
         case IRP_MJ_SET_SECURITY:
-            return KsSetSecurity(DeviceObject, Irp);
+            DriverObject->MajorFunction[MajorFunction] = KspDispatchIrp;
             break;
         default:
-            return STATUS_INVALID_PARAMETER;    /* is this right? */
+            return STATUS_INVALID_PARAMETER;
     };
-}
 
+    return STATUS_SUCCESS;
+}
 
 /*
     @implemented
@@ -1186,123 +1550,124 @@ KsDispatchIrp(
 KSDDKAPI
 NTSTATUS
 NTAPI
-KsStreamIo(
-    IN  PFILE_OBJECT FileObject,
-    IN  PKEVENT Event OPTIONAL,
-    IN  PVOID PortContext OPTIONAL,
-    IN  PIO_COMPLETION_ROUTINE CompletionRoutine OPTIONAL,
-    IN  PVOID CompletionContext OPTIONAL,
-    IN  KSCOMPLETION_INVOCATION CompletionInvocationFlags OPTIONAL,
-    OUT PIO_STATUS_BLOCK IoStatusBlock,
-    IN  OUT PVOID StreamHeaders,
-    IN  ULONG Length,
-    IN  ULONG Flags,
-    IN  KPROCESSOR_MODE RequestorMode)
+KsDispatchIrp(
+    IN  PDEVICE_OBJECT DeviceObject,
+    IN  PIRP Irp)
 {
-    PIRP Irp;
     PIO_STACK_LOCATION IoStack;
-    PDEVICE_OBJECT DeviceObject;
-    ULONG Code;
-    NTSTATUS Status;
-    LARGE_INTEGER Offset;
-    PKSIOBJECT_HEADER ObjectHeader;
+    PKSIDEVICE_HEADER DeviceHeader;
+    PDEVICE_EXTENSION DeviceExtension;
 
+    /* get device extension */
+    DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
+    /* get device header */
+    DeviceHeader = DeviceExtension->DeviceHeader;
 
-    if (Flags == KSSTREAM_READ)
-        Code = IRP_MJ_READ;
-    else if (Flags == KSSTREAM_WRITE)
-        Code = IRP_MJ_WRITE;
-    else
-        return STATUS_INVALID_PARAMETER;
 
-    DeviceObject = IoGetRelatedDeviceObject(FileObject);
-    if (!DeviceObject)
-        return STATUS_INVALID_PARAMETER;
+    /* get current irp stack */
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
 
-    if (Event)
+    if (IoStack->MajorFunction <= IRP_MJ_DEVICE_CONTROL)
     {
-        KeResetEvent(Event);
+        if (IoStack->MajorFunction == IRP_MJ_CREATE)
+        {
+            /* check internal type */
+            if (DeviceHeader->lpVtblIKsDevice) /* FIXME improve check */
+            {
+                /* AVStream client */
+                return IKsDevice_Create(DeviceObject, Irp);
+            }
+            else
+            {
+                /* external client (portcls) */
+                return KspCreate(DeviceObject, Irp);
+            }
+        }
+
+        if (IoStack->MajorFunction == IRP_MJ_DEVICE_CONTROL)
+        {
+            /* handle device requests */
+            return KspDeviceControl(DeviceObject, Irp);
+        }
+
+        switch (IoStack->MajorFunction)
+        {
+            case IRP_MJ_CLOSE:
+            case IRP_MJ_READ:
+            case IRP_MJ_WRITE:
+            case IRP_MJ_FLUSH_BUFFERS:
+            case IRP_MJ_QUERY_SECURITY:
+            case IRP_MJ_SET_SECURITY:
+            case IRP_MJ_PNP:
+                return KspDispatchIrp(DeviceObject, Irp);
+            default:
+                return KsDispatchInvalidDeviceRequest(DeviceObject, Irp);
+        }
     }
 
-    //ASSERT(DeviceObject->DeviceType == FILE_DEVICE_KS);
-    ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext;
-    ASSERT(ObjectHeader);
-    if (Code == IRP_MJ_READ)
+    /* dispatch power */
+    if (IoStack->MajorFunction == IRP_MJ_POWER)
     {
-        if (ObjectHeader->DispatchTable.FastRead)
+        /* check internal type */
+        if (DeviceHeader->lpVtblIKsDevice) /* FIXME improve check */
         {
-            if (ObjectHeader->DispatchTable.FastRead(FileObject, NULL, Length, FALSE, 0, StreamHeaders, IoStatusBlock, DeviceObject))
-            {
-                return STATUS_SUCCESS;
-            }
+            /* AVStream client */
+            return IKsDevice_Power(DeviceObject, Irp);
+        }
+        else
+        {
+            /* external client (portcls) */
+            return KsDefaultDispatchPower(DeviceObject, Irp);
         }
     }
-    else
+    else if (IoStack->MajorFunction == IRP_MJ_PNP) /* dispatch pnp */
     {
-        if (ObjectHeader->DispatchTable.FastWrite)
+        /* check internal type */
+        if (DeviceHeader->lpVtblIKsDevice) /* FIXME improve check */
         {
-            if (ObjectHeader->DispatchTable.FastWrite(FileObject, NULL, Length, FALSE, 0, StreamHeaders, IoStatusBlock, DeviceObject))
-            {
-                return STATUS_SUCCESS;
-            }
+            /* AVStream client */
+            return IKsDevice_Pnp(DeviceObject, Irp);
+        }
+        else
+        {
+            /* external client (portcls) */
+            return KsDefaultDispatchPnp(DeviceObject, Irp);
         }
     }
-
-    Offset.QuadPart = 0LL;
-    Irp = IoBuildSynchronousFsdRequest(Code, DeviceObject, (PVOID)StreamHeaders, Length, &Offset, Event, IoStatusBlock);
-    if (!Irp)
+    else if (IoStack->MajorFunction == IRP_MJ_SYSTEM_CONTROL)
     {
-        return STATUS_UNSUCCESSFUL;
+        /* forward irp */
+        return KsDefaultForwardIrp(DeviceObject, Irp);
     }
-
-
-    if (CompletionRoutine)
+    else
     {
-        IoSetCompletionRoutine(Irp,
-                               CompletionRoutine,
-                               CompletionContext,
-                               (CompletionInvocationFlags & KsInvokeOnSuccess),
-                               (CompletionInvocationFlags & KsInvokeOnError),
-                               (CompletionInvocationFlags & KsInvokeOnCancel));
+        /* not supported */
+        return KsDispatchInvalidDeviceRequest(DeviceObject, Irp);
     }
-
-    IoStack = IoGetNextIrpStackLocation(Irp);
-    IoStack->FileObject = FileObject;
-
-    Status = IoCallDriver(DeviceObject, Irp);
-    return Status;
 }
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsWriteFile(
-    IN  PFILE_OBJECT FileObject,
-    IN  PKEVENT Event OPTIONAL,
-    IN  PVOID PortContext OPTIONAL,
-    OUT PIO_STATUS_BLOCK IoStatusBlock,
-    IN  PVOID Buffer,
-    IN  ULONG Length,
-    IN  ULONG Key OPTIONAL,
-    IN  KPROCESSOR_MODE RequestorMode)
+KSDDKAPI
+ULONG
+NTAPI
+KsGetNodeIdFromIrp(
+    IN PIRP Irp)
 {
-    UNIMPLEMENTED;
-    return STATUS_UNSUCCESSFUL;
+    UNIMPLEMENTED
+    return (ULONG)-1;
 }
 
 /*
     @unimplemented
 */
-KSDDKAPI NTSTATUS NTAPI
-KsDefaultForwardIrp(
-    IN PDEVICE_OBJECT DeviceObject,
+KSDDKAPI
+PKSPIN
+NTAPI
+KsGetPinFromIrp(
     IN PIRP Irp)
 {
-    UNIMPLEMENTED;
-    Irp->IoStatus.Information = 0;
-    Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
-    IoCompleteRequest(Irp, IO_NO_INCREMENT);
-    return STATUS_UNSUCCESSFUL;
+    UNIMPLEMENTED
+    return NULL;
 }
-