summaryrefslogtreecommitdiff
path: root/audio/sysvad/EndpointsCommon/AudioModule0.h
blob: c1f0755779b59826afcdd7c3f0efc3139bcf07a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*++

Copyright (c) Microsoft Corporation All Rights Reserved

Module Name:

    AudioModule0.h

Abstract:

    Declaration of Audio Module 0 (system module).

--*/

#ifndef _SYSVAD_AUDIOMODULE0_H_
#define _SYSVAD_AUDIOMODULE0_H_

#include "AudioModuleHelper.h"

#define AUDIOMODULE0_MAJOR  0x1
#define AUDIOMODULE0_MINOR  0X0

static const GUID AudioModule0Id = 
{ 0xe24c8b6f, 0xede7, 0x4255, 0x8b, 0x39, 0x77, 0x97, 0x60, 0x15, 0xd5, 0x93 };

enum AudioModule0_Parameter {
    AudioModule0Parameter1 = 0,
    AudioModule0Parameter2
};

typedef struct _AUDIOMODULE0_CUSTOM_COMMAND {
    ULONG                   Verb;       // get, set and support
    AudioModule0_Parameter  ParameterId;
} AUDIOMODULE0_CUSTOM_COMMAND, *PAUDIOMODULE0_CUSTOM_COMMAND;

enum AudioModule0_Notification_Type {
    AudioModule0ParameterChanged = 0,
};

typedef struct _AUDIOMODULE0_CUSTOM_NOTIFICATION {
    ULONG           Type;
    union {
        struct {
            ULONG   ParameterId;  
        } ParameterChanged;
    };
} AUDIOMODULE0_CUSTOM_NOTIFICATION, *PAUDIOMODULE0_CUSTOM_NOTIFICATION;

typedef struct _AUDIOMODULE0_NOTIFICATION {
    KSAUDIOMODULE_NOTIFICATION          Header;
    AUDIOMODULE0_CUSTOM_NOTIFICATION    CustomNotification;
} AUDIOMODULE0_NOTIFICATION, *PAUDIOMODULE0_NOTIFICATION;

typedef struct _AUDIOMODULE0_CONTEXT {
    ULONG                       Parameter1;
    BYTE                        Parameter2;
    ULONG                       InstanceId;
    AUDIOMODULE0_NOTIFICATION   Notification;  
    PPORTCLSNOTIFICATIONS       PortNotifications;
} AUDIOMODULE0_CONTEXT, *PAUDIOMODULE0_CONTEXT;

static
ULONG AudioModule0_ValidParameter1List[] =
{
    1, 2, 5
};

static
AUDIOMODULE_PARAMETER_INFO  AudioModule0_ParameterInfo[] =
{
    {
        KSPROPERTY_TYPE_GET | KSPROPERTY_TYPE_SET | KSPROPERTY_TYPE_BASICSUPPORT,
        AUDIOMODULE_PARAMETER_FLAG_CHANGE_NOTIFICATION,
        (ULONG)RTL_FIELD_SIZE(AUDIOMODULE0_CONTEXT, Parameter1),
        VT_UI4,
        AudioModule0_ValidParameter1List,
        SIZEOF_ARRAY(AudioModule0_ValidParameter1List)
    },
    {
        KSPROPERTY_TYPE_GET | KSPROPERTY_TYPE_BASICSUPPORT,
        0, 
        (ULONG)RTL_FIELD_SIZE(AUDIOMODULE0_CONTEXT, Parameter2),
        VT_UI1,
        NULL,
        0
    },
};

inline
#pragma code_seg("PAGE")
NTSTATUS
AudioModule0_InitClass(
    _In_  const AUDIOMODULE_DESCRIPTOR * Module,
    _Inout_opt_ PVOID           Context,
    _In_        size_t          Size,
    _In_        KSAUDIOMODULE_NOTIFICATION * NotificationHeader,
    _In_opt_    PPORTCLSNOTIFICATIONS PortNotifications
    )
{
    PAUDIOMODULE0_CONTEXT ctx = (PAUDIOMODULE0_CONTEXT)Context;
    
    UNREFERENCED_PARAMETER(Module);

    PAGED_CODE();

    if (Context == NULL)
    {
        ASSERT(FALSE);  // this should not happen.
        return STATUS_INTERNAL_ERROR;
    }
    
    ASSERT(Size != 0);    
    RtlZeroMemory(Context, Size);

    ctx->Notification.Header = *NotificationHeader;
    ctx->PortNotifications = PortNotifications;
    if (ctx->PortNotifications != NULL)
    {
        ctx->PortNotifications->AddRef();
    }
    
    return STATUS_SUCCESS;
}

inline
#pragma code_seg("PAGE")
NTSTATUS
AudioModule0_InitInstance(
    _In_  const AUDIOMODULE_DESCRIPTOR * Module,
    _In_opt_    PVOID           TemplateContext,
    _Inout_opt_ PVOID           Context,
    _In_        size_t          Size,
    _In_        ULONG           InstanceId
    )
{
    PAUDIOMODULE0_CONTEXT ctx = (PAUDIOMODULE0_CONTEXT)Context;
    
    UNREFERENCED_PARAMETER(Module);

    PAGED_CODE();

    if (Context == NULL)
    {
        ASSERT(FALSE);  // this should not happen.
        return STATUS_INTERNAL_ERROR;
    }
    
    ASSERT(TemplateContext != NULL);
    ASSERT(Size != 0); 
    
    RtlCopyMemory(Context, TemplateContext, Size);
    
    ctx->InstanceId = InstanceId;
    ctx->Notification.Header.ProviderId.InstanceId = InstanceId;
    if (ctx->PortNotifications != NULL)
    {
        ctx->PortNotifications->AddRef();
    }
    
    return STATUS_SUCCESS;
}

inline
#pragma code_seg("PAGE")
VOID
AudioModule0_Cleanup(
    _In_  PVOID           Context
    )
{
    PAUDIOMODULE0_CONTEXT ctx = (PAUDIOMODULE0_CONTEXT)Context;
    
    PAGED_CODE();

    if (Context == NULL)
    {
        ASSERT(FALSE);  // this should not happen.
        return;
    }
    
    SAFE_RELEASE(ctx->PortNotifications);
}

inline
#pragma code_seg("PAGE")
NTSTATUS
AudioModule0_Handler(
    _Inout_opt_                          PVOID   Context,
    _In_reads_bytes_(InBufferCb)         PVOID   InBuffer,
    _In_                                 ULONG   InBufferCb,
    _Out_writes_bytes_opt_(*OutBufferCb) PVOID   OutBuffer,
    _Inout_                              ULONG * OutBufferCb 
    )
{
    NTSTATUS    status = STATUS_SUCCESS;
    BOOL        fNewValue = FALSE;
    PVOID       currentValue;
    PVOID       inBuffer = NULL;
    ULONG       inBufferCb = 0;
        
    PAUDIOMODULE0_CONTEXT ctx = (PAUDIOMODULE0_CONTEXT)Context;
    AUDIOMODULE_PARAMETER_INFO * parameterInfo = NULL;
    AUDIOMODULE0_CUSTOM_COMMAND * command = NULL;
    
    PAGED_CODE();
    
    if (ctx == NULL)
    {
        ASSERT(FALSE);  // this should not happen.
        status = STATUS_INTERNAL_ERROR;
        goto exit;
    }

    //
    // Basic parameter validation (module specific).
    //
    if (InBuffer == NULL || InBufferCb == 0)
    {
        return STATUS_INVALID_PARAMETER;
    }

    if (InBufferCb < sizeof(AUDIOMODULE0_CUSTOM_COMMAND))
    {
        return STATUS_INVALID_PARAMETER;
    }

    command = (AUDIOMODULE0_CUSTOM_COMMAND*)InBuffer;  
    
    //
    // Validate the parameter referenced in the command.
    //
    switch (command->ParameterId)
    {
        case AudioModule0Parameter1:
            currentValue = &ctx->Parameter1;
            parameterInfo = &AudioModule0_ParameterInfo[AudioModule0Parameter1];
            break;

        case AudioModule0Parameter2:
            currentValue = &ctx->Parameter2;
            parameterInfo = &AudioModule0_ParameterInfo[AudioModule0Parameter2];
            break;

        default:
            status = STATUS_INVALID_PARAMETER;
            goto exit;
    }

    //
    // Update input buffer ptr/size.
    //
    inBuffer = (PVOID)((ULONG_PTR)InBuffer + sizeof(AUDIOMODULE0_CUSTOM_COMMAND));
    inBufferCb = InBufferCb - sizeof(AUDIOMODULE0_CUSTOM_COMMAND);

    if (inBufferCb == 0)
    {
        inBuffer = NULL;
    }
    
    status = AudioModule_GenericHandler(
                command->Verb,
                command->ParameterId,
                parameterInfo,
                currentValue,
                inBuffer,
                inBufferCb,
                OutBuffer,
                OutBufferCb,
                &fNewValue);

    if (!NT_SUCCESS(status))
    {
        goto exit;
    }
    
    if (fNewValue && ctx->PortNotifications &&
        (parameterInfo->Flags & AUDIOMODULE_PARAMETER_FLAG_CHANGE_NOTIFICATION))
    {
        // This logic assumes that access to this function is serialized.
        ctx->Notification.CustomNotification.Type = AudioModule0ParameterChanged;
        ctx->Notification.CustomNotification.ParameterChanged.ParameterId = 
            command->ParameterId;

        AudioModule_SendNotification(ctx->PortNotifications,
                                     (PVOID)&ctx->Notification,
                                     (USHORT)sizeof(ctx->Notification));
    }

    // Normalize error code.
    status = STATUS_SUCCESS;
    
exit:
    return status;
}

#endif // _SYSVAD_AUDIOMODULE0_H_