[WDMAUD_KERNEL]
[reactos.git] / reactos / drivers / wdm / audio / legacy / wdmaud / control.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel Streaming
4 * FILE: drivers/wdm/audio/legacy/wdmaud/deviface.c
5 * PURPOSE: System Audio graph builder
6 * PROGRAMMER: Andrew Greenwood
7 * Johannes Anderwald
8 */
9 #include "wdmaud.h"
10
11 const GUID KSPROPSETID_Pin = {0x8C134960L, 0x51AD, 0x11CF, {0x87, 0x8A, 0x94, 0xF8, 0x01, 0xC1, 0x00, 0x00}};
12 const GUID KSPROPSETID_Connection = {0x1D58C920L, 0xAC9B, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
13 const GUID KSPROPSETID_Sysaudio = {0xCBE3FAA0L, 0xCC75, 0x11D0, {0xB4, 0x65, 0x00, 0x00, 0x1A, 0x18, 0x18, 0xE6}};
14 const GUID KSPROPSETID_General = {0x1464EDA5L, 0x6A8F, 0x11D1, {0x9A, 0xA7, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96}};
15 const GUID KSINTERFACESETID_Standard = {0x1A8766A0L, 0x62CE, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
16 const GUID KSMEDIUMSETID_Standard = {0x4747B320L, 0x62CE, 0x11CF, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
17 const GUID KSDATAFORMAT_TYPE_AUDIO = {0x73647561L, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
18 const GUID KSDATAFORMAT_SUBTYPE_PCM = {0x00000001L, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
19 const GUID KSDATAFORMAT_SPECIFIER_WAVEFORMATEX = {0x05589f81L, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
20 const GUID KSPROPSETID_Topology = {0x720D4AC0L, 0x7533, 0x11D0, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
21
22
23 NTSTATUS
24 WdmAudControlOpen(
25 IN PDEVICE_OBJECT DeviceObject,
26 IN PIRP Irp,
27 IN PWDMAUD_DEVICE_INFO DeviceInfo,
28 IN PWDMAUD_CLIENT ClientInfo)
29 {
30 if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
31 {
32 return WdmAudControlOpenMixer(DeviceObject, Irp, DeviceInfo, ClientInfo);
33 }
34
35 if (DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE || DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE)
36 {
37 return WdmAudControlOpenWave(DeviceObject, Irp, DeviceInfo, ClientInfo);
38 }
39
40 return SetIrpIoStatus(Irp, STATUS_NOT_SUPPORTED, sizeof(WDMAUD_DEVICE_INFO));
41 }
42
43 NTSTATUS
44 WdmAudControlDeviceType(
45 IN PDEVICE_OBJECT DeviceObject,
46 IN PIRP Irp,
47 IN PWDMAUD_DEVICE_INFO DeviceInfo,
48 IN PWDMAUD_CLIENT ClientInfo)
49 {
50 ULONG Result = 0;
51 NTSTATUS Status = STATUS_SUCCESS;
52 PWDMAUD_DEVICE_EXTENSION DeviceExtension;
53
54 DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
55
56 if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
57 {
58 Result = DeviceExtension->MixerInfoCount;
59 }
60 else if (DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
61 {
62 Result = DeviceExtension->WaveOutDeviceCount;
63 }
64 else if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE)
65 {
66 Result = DeviceExtension->WaveInDeviceCount;
67 }
68
69 /* store result count */
70 DeviceInfo->DeviceCount = Result;
71
72 DPRINT("WdmAudControlDeviceType Status %x Devices %u\n", Status, DeviceInfo->DeviceCount);
73 return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
74 }
75
76 NTSTATUS
77 WdmAudControlDeviceState(
78 IN PDEVICE_OBJECT DeviceObject,
79 IN PIRP Irp,
80 IN PWDMAUD_DEVICE_INFO DeviceInfo,
81 IN PWDMAUD_CLIENT ClientInfo)
82 {
83 KSPROPERTY Property;
84 KSSTATE State;
85 NTSTATUS Status;
86 ULONG BytesReturned;
87 PFILE_OBJECT FileObject;
88
89 //DPRINT1("WdmAudControlDeviceState\n");
90
91 Status = ObReferenceObjectByHandle(DeviceInfo->hDevice, GENERIC_READ | GENERIC_WRITE, IoFileObjectType, KernelMode, (PVOID*)&FileObject, NULL);
92 if (!NT_SUCCESS(Status))
93 {
94 DPRINT1("Error: invalid device handle provided %p Type %x\n", DeviceInfo->hDevice, DeviceInfo->DeviceType);
95 return SetIrpIoStatus(Irp, STATUS_UNSUCCESSFUL, 0);
96 }
97
98 Property.Set = KSPROPSETID_Connection;
99 Property.Id = KSPROPERTY_CONNECTION_STATE;
100 Property.Flags = KSPROPERTY_TYPE_SET;
101
102 State = DeviceInfo->u.State;
103
104 Status = KsSynchronousIoControlDevice(FileObject, KernelMode, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSPROPERTY), (PVOID)&State, sizeof(KSSTATE), &BytesReturned);
105
106 ObDereferenceObject(FileObject);
107
108 //DPRINT1("WdmAudControlDeviceState Status %x\n", Status);
109 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
110 }
111
112 NTSTATUS
113 WdmAudCapabilities(
114 IN PDEVICE_OBJECT DeviceObject,
115 IN PIRP Irp,
116 IN PWDMAUD_DEVICE_INFO DeviceInfo,
117 IN PWDMAUD_CLIENT ClientInfo)
118 {
119 PWDMAUD_DEVICE_EXTENSION DeviceExtension;
120 NTSTATUS Status = STATUS_UNSUCCESSFUL;
121
122 DPRINT("WdmAudCapabilities entered\n");
123
124 DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
125
126 if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
127 {
128 Status = WdmAudMixerCapabilities(DeviceObject, DeviceInfo, ClientInfo, DeviceExtension);
129 }
130 else if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE || DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
131 {
132 Status = WdmAudWaveCapabilities(DeviceObject, DeviceInfo, ClientInfo, DeviceExtension);
133 }
134
135 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
136 }
137
138 NTSTATUS
139 NTAPI
140 WdmAudIoctlClose(
141 IN PDEVICE_OBJECT DeviceObject,
142 IN PIRP Irp,
143 IN PWDMAUD_DEVICE_INFO DeviceInfo,
144 IN PWDMAUD_CLIENT ClientInfo)
145 {
146 ULONG Index;
147
148 for(Index = 0; Index < ClientInfo->NumPins; Index++)
149 {
150 if (ClientInfo->hPins[Index].Handle == DeviceInfo->hDevice && ClientInfo->hPins[Index].Type != MIXER_DEVICE_TYPE)
151 {
152 DPRINT1("Closing device %p\n", DeviceInfo->hDevice);
153 ZwClose(DeviceInfo->hDevice);
154 ClientInfo->hPins[Index].Handle = NULL;
155 SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
156 return STATUS_SUCCESS;
157 }
158 }
159
160 SetIrpIoStatus(Irp, STATUS_INVALID_PARAMETER, sizeof(WDMAUD_DEVICE_INFO));
161 return STATUS_INVALID_PARAMETER;
162 }
163
164 NTSTATUS
165 NTAPI
166 WdmAudFrameSize(
167 IN PDEVICE_OBJECT DeviceObject,
168 IN PIRP Irp,
169 IN PWDMAUD_DEVICE_INFO DeviceInfo,
170 IN PWDMAUD_CLIENT ClientInfo)
171 {
172 PFILE_OBJECT FileObject;
173 KSPROPERTY Property;
174 ULONG BytesReturned;
175 KSALLOCATOR_FRAMING Framing;
176 NTSTATUS Status;
177
178 /* Get sysaudio pin file object */
179 Status = ObReferenceObjectByHandle(DeviceInfo->hDevice, GENERIC_WRITE, IoFileObjectType, KernelMode, (PVOID*)&FileObject, NULL);
180 if (!NT_SUCCESS(Status))
181 {
182 DPRINT1("Invalid buffer handle %p\n", DeviceInfo->hDevice);
183 return SetIrpIoStatus(Irp, Status, 0);
184 }
185
186 /* Setup get framing request */
187 Property.Id = KSPROPERTY_CONNECTION_ALLOCATORFRAMING;
188 Property.Flags = KSPROPERTY_TYPE_GET;
189 Property.Set = KSPROPSETID_Connection;
190
191 Status = KsSynchronousIoControlDevice(FileObject, KernelMode, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSPROPERTY), (PVOID)&Framing, sizeof(KSALLOCATOR_FRAMING), &BytesReturned);
192 /* Did we succeed */
193 if (NT_SUCCESS(Status))
194 {
195 /* Store framesize */
196 DeviceInfo->u.FrameSize = Framing.FrameSize;
197 }
198
199 /* Release file object */
200 ObDereferenceObject(FileObject);
201
202 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
203
204 }
205
206 NTSTATUS
207 NTAPI
208 WdmAudGetDeviceInterface(
209 IN PDEVICE_OBJECT DeviceObject,
210 IN PIRP Irp,
211 IN PWDMAUD_DEVICE_INFO DeviceInfo)
212 {
213 PWDMAUD_DEVICE_EXTENSION DeviceExtension;
214 NTSTATUS Status;
215 LPWSTR Device;
216 LPWAVE_INFO WaveInfo;
217 ULONG Size, Length;
218
219 /* get device extension */
220 DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
221
222 /* get device interface string input length */
223 Size = DeviceInfo->u.Interface.DeviceInterfaceStringSize;
224
225 if (DeviceInfo->DeviceType == WAVE_IN_DEVICE_TYPE || DeviceInfo->DeviceType == WAVE_OUT_DEVICE_TYPE)
226 {
227 /* get wave info */
228 Status = GetWaveInfoByIndexAndType(DeviceObject, DeviceInfo->DeviceIndex, DeviceInfo->DeviceType, &WaveInfo);
229
230 /* check for success */
231 if (!NT_SUCCESS(Status))
232 {
233 /* invalid device id */
234 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
235 }
236
237 Status = GetSysAudioDevicePnpName(DeviceObject, WaveInfo->FilterId, &Device);
238 /* check for success */
239 if (!NT_SUCCESS(Status))
240 {
241 /* invalid device id */
242 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
243 }
244
245 /* calculate length */
246 Length = (wcslen(Device)+1) * sizeof(WCHAR);
247
248 if (!Size)
249 {
250 /* store device interface size */
251 DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
252 }
253 else if (Size < Length)
254 {
255 /* buffer too small */
256 DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
257 return SetIrpIoStatus(Irp, STATUS_BUFFER_OVERFLOW, sizeof(WDMAUD_DEVICE_INFO));
258 }
259 else
260 {
261 //FIXME SEH
262 RtlMoveMemory(DeviceInfo->u.Interface.DeviceInterfaceString, Device, Length);
263 }
264
265 ExFreePool(Device);
266 return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
267 }
268 else if (DeviceInfo->DeviceType == MIXER_DEVICE_TYPE)
269 {
270 if (DeviceInfo->DeviceIndex >= DeviceExtension->MixerInfoCount)
271 {
272 /* invalid device id */
273 return SetIrpIoStatus(Irp, STATUS_INVALID_PARAMETER, sizeof(WDMAUD_DEVICE_INFO));
274 }
275
276 Status = GetSysAudioDevicePnpName(DeviceObject, DeviceExtension->MixerInfo[DeviceInfo->DeviceIndex].DeviceIndex, &Device);
277 /* check for success */
278 if (!NT_SUCCESS(Status))
279 {
280 /* invalid device id */
281 return SetIrpIoStatus(Irp, Status, sizeof(WDMAUD_DEVICE_INFO));
282 }
283
284 /* calculate length */
285 Length = (wcslen(Device)+1) * sizeof(WCHAR);
286
287 if (!Size)
288 {
289 /* store device interface size */
290 DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
291 }
292 else if (Size < Length)
293 {
294 /* buffer too small */
295 DeviceInfo->u.Interface.DeviceInterfaceStringSize = Length;
296 return SetIrpIoStatus(Irp, STATUS_BUFFER_OVERFLOW, sizeof(WDMAUD_DEVICE_INFO));
297 }
298 else
299 {
300 //FIXME SEH
301 RtlMoveMemory(DeviceInfo->u.Interface.DeviceInterfaceString, Device, Length);
302 }
303
304 ExFreePool(Device);
305 return SetIrpIoStatus(Irp, STATUS_SUCCESS, sizeof(WDMAUD_DEVICE_INFO));
306 }
307
308 return SetIrpIoStatus(Irp, STATUS_INVALID_DEVICE_REQUEST, sizeof(WDMAUD_DEVICE_INFO));
309 }
310
311 NTSTATUS
312 NTAPI
313 WdmAudDeviceControl(
314 IN PDEVICE_OBJECT DeviceObject,
315 IN PIRP Irp)
316 {
317 PIO_STACK_LOCATION IoStack;
318 PWDMAUD_DEVICE_INFO DeviceInfo;
319 PWDMAUD_CLIENT ClientInfo;
320
321 IoStack = IoGetCurrentIrpStackLocation(Irp);
322
323 DPRINT("WdmAudDeviceControl entered\n");
324
325 if (IoStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(WDMAUD_DEVICE_INFO))
326 {
327 /* invalid parameter */
328 DPRINT1("Input buffer too small size %u expected %u\n", IoStack->Parameters.DeviceIoControl.InputBufferLength, sizeof(WDMAUD_DEVICE_INFO));
329 return SetIrpIoStatus(Irp, STATUS_INVALID_PARAMETER, 0);
330 }
331
332 DeviceInfo = (PWDMAUD_DEVICE_INFO)Irp->AssociatedIrp.SystemBuffer;
333
334 if (DeviceInfo->DeviceType < MIN_SOUND_DEVICE_TYPE || DeviceInfo->DeviceType > MAX_SOUND_DEVICE_TYPE)
335 {
336 /* invalid parameter */
337 DPRINT1("Error: device type not set\n");
338 return SetIrpIoStatus(Irp, STATUS_INVALID_PARAMETER, 0);
339 }
340
341 if (!IoStack->FileObject)
342 {
343 /* file object parameter */
344 DPRINT1("Error: file object is not attached\n");
345 return SetIrpIoStatus(Irp, STATUS_UNSUCCESSFUL, 0);
346 }
347 ClientInfo = (PWDMAUD_CLIENT)IoStack->FileObject->FsContext;
348
349 DPRINT("WdmAudDeviceControl entered\n");
350
351 switch(IoStack->Parameters.DeviceIoControl.IoControlCode)
352 {
353 case IOCTL_OPEN_WDMAUD:
354 return WdmAudControlOpen(DeviceObject, Irp, DeviceInfo, ClientInfo);
355 case IOCTL_GETNUMDEVS_TYPE:
356 return WdmAudControlDeviceType(DeviceObject, Irp, DeviceInfo, ClientInfo);
357 case IOCTL_SETDEVICE_STATE:
358 return WdmAudControlDeviceState(DeviceObject, Irp, DeviceInfo, ClientInfo);
359 case IOCTL_GETCAPABILITIES:
360 return WdmAudCapabilities(DeviceObject, Irp, DeviceInfo, ClientInfo);
361 case IOCTL_CLOSE_WDMAUD:
362 return WdmAudIoctlClose(DeviceObject, Irp, DeviceInfo, ClientInfo);
363 case IOCTL_GETFRAMESIZE:
364 return WdmAudFrameSize(DeviceObject, Irp, DeviceInfo, ClientInfo);
365 case IOCTL_GETLINEINFO:
366 return WdmAudGetLineInfo(DeviceObject, Irp, DeviceInfo, ClientInfo);
367 case IOCTL_GETLINECONTROLS:
368 return WdmAudGetLineControls(DeviceObject, Irp, DeviceInfo, ClientInfo);
369 case IOCTL_SETCONTROLDETAILS:
370 return WdmAudSetControlDetails(DeviceObject, Irp, DeviceInfo, ClientInfo);
371 case IOCTL_GETCONTROLDETAILS:
372 return WdmAudGetControlDetails(DeviceObject, Irp, DeviceInfo, ClientInfo);
373 case IOCTL_QUERYDEVICEINTERFACESTRING:
374 return WdmAudGetDeviceInterface(DeviceObject, Irp, DeviceInfo);
375 case IOCTL_GET_MIXER_EVENT:
376 return WdmAudGetMixerEvent(DeviceObject, Irp, DeviceInfo, ClientInfo);
377 case IOCTL_GETPOS:
378 case IOCTL_GETDEVID:
379 case IOCTL_GETVOLUME:
380 case IOCTL_SETVOLUME:
381
382 DPRINT1("Unhandeled %x\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
383 break;
384 }
385
386 return SetIrpIoStatus(Irp, STATUS_NOT_IMPLEMENTED, 0);
387 }
388
389
390 NTSTATUS
391 NTAPI
392 WdmAudReadWrite(
393 IN PDEVICE_OBJECT DeviceObject,
394 IN PIRP Irp)
395 {
396 NTSTATUS Status;
397 PWDMAUD_DEVICE_INFO DeviceInfo;
398 PFILE_OBJECT FileObject;
399 PIO_STACK_LOCATION IoStack;
400 ULONG Length;
401 PMDL Mdl;
402 BOOLEAN Read = TRUE;
403
404 /* get current irp stack location */
405 IoStack = IoGetCurrentIrpStackLocation(Irp);
406
407 /* store the input buffer in UserBuffer - as KsProbeStreamIrp operates on IRP_MJ_DEVICE_CONTROL */
408 Irp->UserBuffer = MmGetMdlVirtualAddress(Irp->MdlAddress);
409
410 /* sanity check */
411 ASSERT(Irp->UserBuffer);
412
413 /* get the length of the request length */
414 Length = IoStack->Parameters.Write.Length;
415
416 /* store outputbuffer length */
417 IoStack->Parameters.DeviceIoControl.OutputBufferLength = Length;
418
419 /* store mdl address */
420 Mdl = Irp->MdlAddress;
421
422 /* remove mdladdress as KsProbeStreamIrp will interprete it as an already probed audio buffer */
423 Irp->MdlAddress = NULL;
424
425 /* check for success */
426
427 if (IoStack->MajorFunction == IRP_MJ_WRITE)
428 {
429 /* probe the write stream irp */
430 Read = FALSE;
431 Status = KsProbeStreamIrp(Irp, KSPROBE_STREAMWRITE | KSPROBE_ALLOCATEMDL | KSPROBE_PROBEANDLOCK, Length);
432 }
433 else
434 {
435 /* probe the read stream irp */
436 Status = KsProbeStreamIrp(Irp, KSPROBE_STREAMREAD | KSPROBE_ALLOCATEMDL | KSPROBE_PROBEANDLOCK, Length);
437 }
438
439 /* now free the mdl */
440 IoFreeMdl(Mdl);
441
442 if (!NT_SUCCESS(Status))
443 {
444 DPRINT1("KsProbeStreamIrp failed with Status %x Cancel %u\n", Status, Irp->Cancel);
445 return SetIrpIoStatus(Irp, Status, 0);
446 }
447
448 /* get device info */
449 DeviceInfo = (PWDMAUD_DEVICE_INFO)Irp->AssociatedIrp.SystemBuffer;
450 ASSERT(DeviceInfo);
451
452 /* now get sysaudio file object */
453 Status = ObReferenceObjectByHandle(DeviceInfo->hDevice, GENERIC_WRITE, IoFileObjectType, KernelMode, (PVOID*)&FileObject, NULL);
454 if (!NT_SUCCESS(Status))
455 {
456 DPRINT1("Invalid pin handle %p\n", DeviceInfo->hDevice);
457 return SetIrpIoStatus(Irp, Status, 0);
458 }
459
460 /* skip current irp stack location */
461 IoSkipCurrentIrpStackLocation(Irp);
462
463 /* get next stack location */
464 IoStack = IoGetNextIrpStackLocation(Irp);
465
466 if (Read)
467 {
468 IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_KS_READ_STREAM;
469 }
470 else
471 {
472 IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_KS_WRITE_STREAM;
473 }
474
475 /* attach file object */
476 IoStack->FileObject = FileObject;
477 IoStack->Parameters.Write.Length = Length;
478 IoStack->MajorFunction = IRP_MJ_WRITE;
479
480 /* mark irp as pending */
481 // IoMarkIrpPending(Irp);
482 /* call the driver */
483 Status = IoCallDriver(IoGetRelatedDeviceObject(FileObject), Irp);
484
485 /* dereference file object */
486 ObDereferenceObject(FileObject);
487
488 return Status;
489 }