Merge r51067
[reactos.git] / reactos / drivers / wdm / audio / legacy / wdmaud / control.c
index 9f51bf4..3343405 100644 (file)
@@ -8,17 +8,7 @@
  */
 #include "wdmaud.h"
 
-const GUID KSPROPSETID_Pin                     = {0x8C134960L, 0x51AD, 0x11CF, {0x87, 0x8A, 0x94, 0xF8, 0x01, 0xC1, 0x00, 0x00}};
-const GUID KSPROPSETID_Connection               = {0x1D58C920L, 0xAC9B, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
 const GUID KSPROPSETID_Sysaudio                 = {0xCBE3FAA0L, 0xCC75, 0x11D0, {0xB4, 0x65, 0x00, 0x00, 0x1A, 0x18, 0x18, 0xE6}};
-const GUID KSPROPSETID_General                  = {0x1464EDA5L, 0x6A8F, 0x11D1, {0x9A, 0xA7, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96}};
-const GUID KSINTERFACESETID_Standard            = {0x1A8766A0L, 0x62CE, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
-const GUID KSMEDIUMSETID_Standard               = {0x4747B320L, 0x62CE, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
-const GUID KSDATAFORMAT_TYPE_AUDIO              = {0x73647561L, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
-const GUID KSDATAFORMAT_SUBTYPE_PCM             = {0x00000001L, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
-const GUID KSDATAFORMAT_SPECIFIER_WAVEFORMATEX  = {0x05589f81L, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
-const GUID KSPROPSETID_Topology                 = {0x720D4AC0L, 0x7533, 0x11D0, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
-
 
 NTSTATUS
 WdmAudControlOpen(
@@ -37,6 +27,12 @@ WdmAudControlOpen(
         return WdmAudControlOpenWave(DeviceObject, Irp, DeviceInfo, ClientInfo);
     }
 
+    if (DeviceInfo->DeviceType == MIDI_OUT_DEVICE_TYPE || DeviceInfo->DeviceType == MIDI_IN_DEVICE_TYPE)
+    {
+        return WdmAudControlOpenMidi(DeviceObject, Irp, DeviceInfo, ClientInfo);
+    }
+
+
     return SetIrpIoStatus(Irp, STATUS_NOT_SUPPORTED, sizeof(WDMAUD_DEVICE_INFO));
 }
 
@@ -55,17 +51,26 @@ WdmAudControlDeviceType(
 
     if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
     {
-        Result = DeviceExtension->MixerInfoCount;
+        Result = WdmAudGetMixerDeviceCount();
     }
     else if (DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
     {
-        Result = DeviceExtension->WaveOutDeviceCount;
+        Result = WdmAudGetWaveInDeviceCount();
     }
     else if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE)
     {
-        Result = DeviceExtension->WaveInDeviceCount;
+        Result = WdmAudGetWaveOutDeviceCount();
+    }
+    else if (DeviceInfo->DeviceType == MIDI_IN_DEVICE_TYPE)
+    {
+        Result = WdmAudGetMidiInDeviceCount();
+    }
+    else if (DeviceInfo->DeviceType == MIDI_OUT_DEVICE_TYPE)
+    {
+        Result = WdmAudGetMidiOutDeviceCount();
     }
 
+
     /* store result count */
     DeviceInfo->DeviceCount = Result;
 
@@ -131,6 +136,10 @@ WdmAudCapabilities(
     {
         Status = WdmAudWaveCapabilities(DeviceObject, DeviceInfo, ClientInfo, DeviceExtension);
     }
+    else if (DeviceInfo->DeviceType == MIDI_IN_DEVICE_TYPE || DeviceInfo->DeviceType == MIDI_OUT_DEVICE_TYPE)
+    {
+        Status = WdmAudMidiCapabilities(DeviceObject, DeviceInfo, ClientInfo, DeviceExtension);
+    }
 
     return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
 }
@@ -161,7 +170,7 @@ WdmAudIoctlClose(
             {
                 ObDereferenceObject(ClientInfo->hPins[Index].NotifyEvent);
                 ClientInfo->hPins[Index].NotifyEvent = NULL;
-    }
+            }
         }
     }
 
@@ -221,7 +230,6 @@ WdmAudGetDeviceInterface(
     PWDMAUD_DEVICE_EXTENSION DeviceExtension;
     NTSTATUS Status;
     LPWSTR Device;
-    LPWAVE_INFO WaveInfo;
     ULONG Size, Length;
 
     /* get device extension */
@@ -230,90 +238,38 @@ WdmAudGetDeviceInterface(
     /* get device interface string input length */
     Size = DeviceInfo->u.Interface.DeviceInterfaceStringSize;
 
-    if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE || DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
-    {
-        /* get wave info */
-        Status = GetWaveInfoByIndexAndType(DeviceObject, DeviceInfo->DeviceIndex, DeviceInfo->DeviceType, &WaveInfo);
-
-        /* check for success */
-        if (!NT_SUCCESS(Status))
-        {
-            /* invalid device id */
-            return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
-        }
+   /* get mixer info */
+   Status = WdmAudGetPnpNameByIndexAndType(DeviceInfo->DeviceIndex, DeviceInfo->DeviceType, &Device);
 
-        Status = GetSysAudioDevicePnpName(DeviceObject, WaveInfo->FilterId, &Device);
-        /* check for success */
-        if (!NT_SUCCESS(Status))
-        {
-            /* invalid device id */
-            return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
-        }
+   /* check for success */
+   if (!NT_SUCCESS(Status))
+   {
+        /* invalid device id */
+        return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
+   }
 
-        /* calculate length */
-        Length = (wcslen(Device)+1) * sizeof(WCHAR);
+   /* calculate length */
+   Length = (wcslen(Device)+1) * sizeof(WCHAR);
 
-        if (!Size)
-        {
-            /* store device interface size */
-            DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
-        }
-        else if (Size < Length)
-        {
-            /* buffer too small */
-            DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
-            return SetIrpIoStatus(Irp, STATUS_BUFFER_OVERFLOW, sizeof(WDMAUD_DEVICE_INFO));
-        }
-        else
-        {
-            //FIXME SEH
-            RtlMoveMemory(DeviceInfo->u.Interface.DeviceInterfaceString, Device, Length);
-        }
-
-        ExFreePool(Device);
-        return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
+    if (!Size)
+    {
+        /* store device interface size */
+        DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
     }
-    else if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
+    else if (Size < Length)
     {
-        if (DeviceInfo->DeviceIndex >= DeviceExtension->MixerInfoCount)
-        {
-            /* invalid device id */
-            return SetIrpIoStatus(Irp, STATUS_INVALID_PARAMETER, sizeof(WDMAUD_DEVICE_INFO));
-        }
-
-        Status = GetSysAudioDevicePnpName(DeviceObject, DeviceExtension->MixerInfo[DeviceInfo->DeviceIndex].DeviceIndex, &Device);
-        /* check for success */
-        if (!NT_SUCCESS(Status))
-        {
-            /* invalid device id */
-            return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
-        }
-
-        /* calculate length */
-        Length = (wcslen(Device)+1) * sizeof(WCHAR);
-
-        if (!Size)
-        {
-            /* store device interface size */
-            DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
-        }
-        else if (Size < Length)
-        {
-            /* buffer too small */
-            DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
-            return SetIrpIoStatus(Irp, STATUS_BUFFER_OVERFLOW, sizeof(WDMAUD_DEVICE_INFO));
-        }
-        else
-        {
-            //FIXME SEH
-            RtlMoveMemory(DeviceInfo->u.Interface.DeviceInterfaceString, Device, Length);
-        }
-
-        ExFreePool(Device);
-        return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
+        /* buffer too small */
+        DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
+        return SetIrpIoStatus(Irp, STATUS_BUFFER_OVERFLOW, sizeof(WDMAUD_DEVICE_INFO));
+    }
+    else
+    {
+        //FIXME SEH
+        RtlMoveMemory(DeviceInfo->u.Interface.DeviceInterfaceString, Device, Length);
     }
 
-    return SetIrpIoStatus(Irp, STATUS_INVALID_DEVICE_REQUEST, sizeof(WDMAUD_DEVICE_INFO));
+    FreeItem(Device);
+    return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
 }
 
 NTSTATUS
@@ -428,6 +384,65 @@ WdmAudDeviceControl(
     return SetIrpIoStatus(Irp, STATUS_NOT_IMPLEMENTED, 0);
 }
 
+NTSTATUS
+NTAPI
+IoCompletion (
+    PDEVICE_OBJECT DeviceObject,
+    PIRP Irp,
+    PVOID Ctx)
+{
+    PKSSTREAM_HEADER Header;
+    ULONG Length = 0;
+    PMDL Mdl, NextMdl;
+    PWDMAUD_COMPLETION_CONTEXT Context = (PWDMAUD_COMPLETION_CONTEXT)Ctx;
+
+    /* get stream header */
+    Header = (PKSSTREAM_HEADER)Irp->AssociatedIrp.SystemBuffer;
+
+    /* sanity check */
+    ASSERT(Header);
+
+    /* time to free all allocated mdls */
+    Mdl = Irp->MdlAddress;
+
+    while(Mdl)
+    {
+        /* get next mdl */
+        NextMdl = Mdl->Next;
+
+        /* unlock pages */
+        MmUnlockPages(Mdl);
+
+        /* grab next mdl */
+        Mdl = NextMdl;
+    }
+
+    /* clear mdl list */
+    Irp->MdlAddress = NULL;
+
+   /* 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);
+
+    if (!NT_SUCCESS(Irp->IoStatus.Status))
+    {
+        /* failed */
+        Irp->IoStatus.Information = 0;
+    }
+
+    /* free context */
+    FreeItem(Context);
+
+    return STATUS_SUCCESS;
+}
 
 NTSTATUS
 NTAPI
@@ -442,6 +457,20 @@ WdmAudReadWrite(
     ULONG Length;
     PMDL Mdl;
     BOOLEAN Read = TRUE;
+    PWDMAUD_COMPLETION_CONTEXT Context;
+
+    /* allocate completion context */
+    Context = AllocateItem(NonPagedPool, sizeof(WDMAUD_COMPLETION_CONTEXT));
+
+    if (!Context)
+    {
+        /* not enough memory */
+        Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+
+        /* done */
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
     /* get current irp stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
@@ -458,6 +487,11 @@ WdmAudReadWrite(
     /* store outputbuffer length */
     IoStack->Parameters.DeviceIoControl.OutputBufferLength = Length;
 
+    /* setup context */
+    Context->Length = Length;
+    Context->Function = (IoStack->MajorFunction == IRP_MJ_WRITE ? IOCTL_KS_WRITE_STREAM : IOCTL_KS_READ_STREAM);
+    Context->Mdl = Irp->MdlAddress;
+
     /* store mdl address */
     Mdl = Irp->MdlAddress;
 
@@ -478,12 +512,10 @@ WdmAudReadWrite(
         Status = KsProbeStreamIrp(Irp, KSPROBE_STREAMREAD | KSPROBE_ALLOCATEMDL | KSPROBE_PROBEANDLOCK, Length);
     }
 
-    /* now free the mdl */
-    IoFreeMdl(Mdl);
-
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("KsProbeStreamIrp failed with Status %x Cancel %u\n", Status, Irp->Cancel);
+        Irp->MdlAddress = Mdl;
         return SetIrpIoStatus(Irp, Status, 0);
     }
 
@@ -519,6 +551,9 @@ WdmAudReadWrite(
     IoStack->Parameters.Write.Length = Length;
     IoStack->MajorFunction = IRP_MJ_WRITE;
 
+    IoSetCompletionRoutine(Irp, IoCompletion, (PVOID)Context, TRUE, TRUE, TRUE);
+
+
     /* mark irp as pending */
 //    IoMarkIrpPending(Irp);
     /* call the driver */