[MMIXER] Cleanup mixer notifications opened by an application when it is closed.
[reactos.git] / reactos / drivers / wdm / audio / legacy / wdmaud / control.c
index 89d85fd..f9ae9a3 100644 (file)
@@ -48,10 +48,6 @@ WdmAudControlDeviceType(
     IN  PWDMAUD_CLIENT ClientInfo)
 {
     ULONG Result = 0;
-    NTSTATUS Status = STATUS_SUCCESS;
-    //PWDMAUD_DEVICE_EXTENSION DeviceExtension;
-
-    //DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
 
     if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
     {
@@ -59,11 +55,11 @@ WdmAudControlDeviceType(
     }
     else if (DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
     {
-        Result = WdmAudGetWaveInDeviceCount();
+        Result = WdmAudGetWaveOutDeviceCount();
     }
     else if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE)
     {
-        Result = WdmAudGetWaveOutDeviceCount();
+        Result = WdmAudGetWaveInDeviceCount();
     }
     else if (DeviceInfo->DeviceType == MIDI_IN_DEVICE_TYPE)
     {
@@ -78,7 +74,7 @@ WdmAudControlDeviceType(
     /* store result count */
     DeviceInfo->DeviceCount = Result;
 
-    DPRINT("WdmAudControlDeviceType Status %x Devices %u\n", Status, DeviceInfo->DeviceCount);
+    DPRINT("WdmAudControlDeviceType Devices %u\n", DeviceInfo->DeviceCount);
     return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
 }
 
@@ -170,11 +166,8 @@ WdmAudIoctlClose(
         }
         else if (ClientInfo->hPins[Index].Handle == DeviceInfo->hDevice && ClientInfo->hPins[Index].Type == MIXER_DEVICE_TYPE)
         {
-            if (ClientInfo->hPins[Index].NotifyEvent)
-            {
-                ObDereferenceObject(ClientInfo->hPins[Index].NotifyEvent);
-                ClientInfo->hPins[Index].NotifyEvent = NULL;
-            }
+            DPRINT1("Closing mixer %p\n", DeviceInfo->hDevice);
+            return WdmAudControlCloseMixer(DeviceObject, Irp, DeviceInfo, ClientInfo, Index);
         }
     }
 
@@ -231,14 +224,10 @@ WdmAudGetDeviceInterface(
     IN  PIRP Irp,
     IN  PWDMAUD_DEVICE_INFO DeviceInfo)
 {
-    //PWDMAUD_DEVICE_EXTENSION DeviceExtension;
     NTSTATUS Status;
     LPWSTR Device;
     ULONG Size, Length;
 
-    /* get device extension */
-    //DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
-
     /* get device interface string input length */
     Size = DeviceInfo->u.Interface.DeviceInterfaceStringSize;
 
@@ -381,7 +370,7 @@ WdmAudDeviceControl(
         case IOCTL_GETVOLUME:
         case IOCTL_SETVOLUME:
 
-           DPRINT1("Unhandeled %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
+           DPRINT1("Unhandled %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
            break;
     }
 
@@ -396,7 +385,6 @@ IoCompletion (
     PVOID Ctx)
 {
     PKSSTREAM_HEADER Header;
-    ULONG Length = 0;
     PMDL Mdl, NextMdl;
     PWDMAUD_COMPLETION_CONTEXT Context = (PWDMAUD_COMPLETION_CONTEXT)Ctx;
 
@@ -420,21 +408,13 @@ IoCompletion (
         /* grab next mdl */
         Mdl = NextMdl;
     }
-
+    //IoFreeMdl(Mdl);
     /* clear mdl list */
-    Irp->MdlAddress = NULL;
+    Irp->MdlAddress = Context->Mdl;
 
-   /* check if mdl is locked */
-    if (Context->Mdl->MdlFlags & MDL_PAGES_LOCKED)
-    {
-        /* unlock pages */
-        MmUnlockPages(Context->Mdl);
-    }
 
-    /* now free the mdl */
-    IoFreeMdl(Context->Mdl);
 
-    DPRINT("IoCompletion Irp %p IoStatus %lx Information %lx Length %lu\n", Irp, Irp->IoStatus.Status, Irp->IoStatus.Information, Length);
+    DPRINT("IoCompletion Irp %p IoStatus %lx Information %lx\n", Irp, Irp->IoStatus.Status, Irp->IoStatus.Information);
 
     if (!NT_SUCCESS(Irp->IoStatus.Status))
     {
@@ -442,6 +422,9 @@ IoCompletion (
         Irp->IoStatus.Information = 0;
     }
 
+    /* dereference file object */
+    ObDereferenceObject(Context->FileObject);
+
     /* free context */
     FreeItem(Context);
 
@@ -499,11 +482,9 @@ WdmAudReadWrite(
     /* store mdl address */
     Mdl = Irp->MdlAddress;
 
-    /* remove mdladdress as KsProbeStreamIrp will interprete it as an already probed audio buffer */
+    /* remove mdladdress as KsProbeStreamIrp will interpret it as an already probed audio buffer */
     Irp->MdlAddress = NULL;
 
-    /* check for success */
-
     if (IoStack->MajorFunction == IRP_MJ_WRITE)
     {
         /* probe the write stream irp */
@@ -520,6 +501,7 @@ WdmAudReadWrite(
     {
         DPRINT1("KsProbeStreamIrp failed with Status %x Cancel %u\n", Status, Irp->Cancel);
         Irp->MdlAddress = Mdl;
+        FreeItem(Context);
         return SetIrpIoStatus(Irp, Status, 0);
     }
 
@@ -532,39 +514,30 @@ WdmAudReadWrite(
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Invalid pin handle %p\n", DeviceInfo->hDevice);
+        Irp->MdlAddress = Mdl;
+        FreeItem(Context);
         return SetIrpIoStatus(Irp, Status, 0);
     }
 
+    /* store file object whose reference is released in the completion callback */
+    Context->FileObject = FileObject;
+
     /* skip current irp stack location */
     IoSkipCurrentIrpStackLocation(Irp);
 
     /* get next stack location */
     IoStack = IoGetNextIrpStackLocation(Irp);
 
-    if (Read)
-    {
-        IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_KS_READ_STREAM;
-    }
-    else
-    {
-        IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_KS_WRITE_STREAM;
-    }
-
-    /* attach file object */
+    /* prepare stack location */
     IoStack->FileObject = FileObject;
     IoStack->Parameters.Write.Length = Length;
     IoStack->MajorFunction = IRP_MJ_WRITE;
-
+    IoStack->Parameters.DeviceIoControl.IoControlCode = (Read ? IOCTL_KS_READ_STREAM : IOCTL_KS_WRITE_STREAM);
     IoSetCompletionRoutine(Irp, IoCompletion, (PVOID)Context, TRUE, TRUE, TRUE);
 
-
     /* mark irp as pending */
 //    IoMarkIrpPending(Irp);
     /* call the driver */
     Status = IoCallDriver(IoGetRelatedDeviceObject(FileObject), Irp);
-
-    /* dereference file object */
-    ObDereferenceObject(FileObject);
-
     return Status;
 }