summaryrefslogtreecommitdiff
path: root/src/sound_card_control_clv.c
blob: 4ad8460ac25ab680292dce871eccd6a6f13703d5 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * sound-plugin-hal-clovertrail
 *
 * Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <alsa/asoundlib.h>
#include <errno.h>
#include <string.h>

#include <mm_debug.h>
#include <mm_error.h>

const char * card = "hw:3";

typedef struct {
    snd_ctl_elem_info_t *info;
    snd_ctl_elem_id_t *id;
    snd_ctl_elem_value_t *control;
}snd_stack_t;

static int prepare_snd_ctl_by_name(snd_ctl_t *handle, snd_stack_t *stack,
        snd_ctl_elem_type_t *type, unsigned int *count, const char* name)
{
	int err = 0;
	memset(stack->id, 0, snd_ctl_elem_id_sizeof());
	memset(stack->info, 0, snd_ctl_elem_info_sizeof());
	memset(stack->control, 0, snd_ctl_elem_value_sizeof());

	/* Set interface */
	snd_ctl_elem_id_set_interface(stack->id, SND_CTL_ELEM_IFACE_MIXER);

	if (NULL != name)
		snd_ctl_elem_id_set_name(stack->id, name);

	/* init info id */
	snd_ctl_elem_info_set_id(stack->info, stack->id);

	/* get info */
	err = snd_ctl_elem_info(handle, stack->info);
	if (err < 0) {
		debug_error("get info err : %s", snd_strerror(err));
		return -1;
	}

	/* get type */
	*type = snd_ctl_elem_info_get_type(stack->info);

	/* get count */
	*count = snd_ctl_elem_info_get_count(stack->info);

	/* set value id */
	snd_ctl_elem_value_set_id(stack->control, stack->id);
	return 0;
}

static int prepare_snd_ctl(snd_ctl_t *handle, snd_stack_t *stack,
            snd_ctl_elem_type_t *type, unsigned int *count, int numid)
{
    int err = 0;
    memset(stack->id, 0, snd_ctl_elem_id_sizeof());
    memset(stack->info, 0, snd_ctl_elem_info_sizeof());
    memset(stack->control, 0, snd_ctl_elem_value_sizeof());

    /* Set interface */
    snd_ctl_elem_id_set_interface(stack->id, SND_CTL_ELEM_IFACE_MIXER);

    if (numid > 0)
        snd_ctl_elem_id_set_numid(stack->id, numid);

    /* init info id */
    snd_ctl_elem_info_set_id(stack->info, stack->id);

    /* get info */
    err = snd_ctl_elem_info(handle, stack->info);
    if (err < 0) {
        debug_error("get info err : %s", snd_strerror(err));
        return -1;
    }

    /* get type */
    *type = snd_ctl_elem_info_get_type(stack->info);

    /* get count */
    *count = snd_ctl_elem_info_get_count(stack->info);

    /* set value id */
    snd_ctl_elem_value_set_id(stack->control, stack->id);
    return 0;
}

static int sound_card_hw_io(int set_flag, snd_ctl_t *handle, snd_stack_t *stack,
            snd_ctl_elem_type_t *type, unsigned int *count, int value, int *out_val)
{
    unsigned int idx;
    long tmp;
    int err = 0;

    if (!set_flag) {
        /* read element */
        err = snd_ctl_elem_read(handle, stack->control);
        if (err < 0) {
            debug_error("read element err :%s", snd_strerror(err));
            return -1;
        }

        for (idx = 0; idx < *count && idx < 128; idx++) {
            switch (*type)
            {
            case SND_CTL_ELEM_TYPE_BOOLEAN:
                *out_val = snd_ctl_elem_value_get_boolean(stack->control, idx);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER:
                *out_val = snd_ctl_elem_value_get_integer(stack->control, idx);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER64:
                *out_val = snd_ctl_elem_value_get_integer64(stack->control, idx);
                break;
            case SND_CTL_ELEM_TYPE_ENUMERATED:
                *out_val = snd_ctl_elem_value_get_enumerated(stack->control, idx);
                break;
            case SND_CTL_ELEM_TYPE_BYTES:
                *out_val = snd_ctl_elem_value_get_byte(stack->control, idx);
                break;
            default:
                debug_error("Unknown control element type");
                return -1;
            }
            debug_log("Read alsa element, idx=%d out_val=%d\n", idx, *out_val);
        }
    } else {
        /* set value */
        tmp = value;
        for (idx = 0; idx < *count && idx < 128; idx++)
        {
            switch (*type)
            {
            case SND_CTL_ELEM_TYPE_BOOLEAN:
                snd_ctl_elem_value_set_boolean(stack->control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER:
                snd_ctl_elem_value_set_integer(stack->control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER64:
                snd_ctl_elem_value_set_integer64(stack->control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_ENUMERATED:
                snd_ctl_elem_value_set_enumerated(stack->control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_BYTES:
                snd_ctl_elem_value_set_byte(stack->control, idx, tmp);
                break;
            default:
                debug_error("Unknown control element type");
                return -1;
            }

            debug_log("Write alsa element, idx=%u value=%ld\n", idx, tmp);
            /* Write element */
            err = snd_ctl_elem_write(handle, stack->control);
            if (err < 0) {
                debug_error("snd_ctl_elem_write err: %s", snd_strerror(err));
                /* Close sound control */
                return -1;
            }
        }
    }
    return 0;
}

static int sound_card_ctrl_get(int numid, int *out_val)
{
    snd_ctl_t *handle = NULL;
    snd_stack_t stack;
    snd_ctl_elem_type_t type;
    unsigned int count;
    int err = 0;

    snd_ctl_elem_id_alloca(&(stack.id));
    snd_ctl_elem_info_alloca(&(stack.info));
    snd_ctl_elem_value_alloca(&(stack.control));

    err = snd_ctl_open(&handle, card, 0);
    if (err < 0) {
        debug_error("control %s open err: %s", card, snd_strerror(err));
        return -1;
    }

    err = prepare_snd_ctl(handle, &stack, &type, &count, numid);
    if (err < 0) {
        debug_error("prepare hw err:%d", err);
        goto error;
    }

    err = sound_card_hw_io(0, handle, &stack, &type, &count, 0, out_val);
    if (err < 0) {
        debug_error("ctl hw io err:%d", err);
        goto error;
    }
    snd_ctl_close(handle);
    return 0;

error:
    snd_ctl_close(handle);
    return -1;
}

int sound_card_ctrl_set_by_name(const char *name, int value)
{
    snd_ctl_t *handle = NULL;
    snd_stack_t stack;
    snd_ctl_elem_type_t type;
    unsigned int count;
    int err = 0;

    snd_ctl_elem_id_alloca(&(stack.id));
    snd_ctl_elem_info_alloca(&(stack.info));
    snd_ctl_elem_value_alloca(&(stack.control));

    err = snd_ctl_open(&handle, card, 0);
    if (err < 0) {
        debug_error("control %s open err: %s", card, snd_strerror(err));
        return -1;
    }

    err = prepare_snd_ctl_by_name(handle, &stack, &type, &count, name);
    if (err < 0) {
        debug_error("prepare hw err:%d", err);
        goto error;
    }

    /* set value */
    err = sound_card_hw_io(1, handle, &stack, &type, &count, value, NULL);
    if (err < 0) {
        debug_error("ctl hw io err%d", err);
        goto error;
    }
    snd_ctl_close(handle);
    return 0;

error:
    snd_ctl_close(handle);
    return -1;
}

int sound_card_ctrl_set(int numid, int value)
{
    snd_ctl_t *handle = NULL;
    snd_stack_t stack;
    snd_ctl_elem_type_t type;
    unsigned int count;
    int err = 0;

    snd_ctl_elem_id_alloca(&(stack.id));
    snd_ctl_elem_info_alloca(&(stack.info));
    snd_ctl_elem_value_alloca(&(stack.control));

    err = snd_ctl_open(&handle, card, 0);
    if (err < 0) {
        debug_error("control %s open err: %s", card, snd_strerror(err));
        return -1;
    }

    err = prepare_snd_ctl(handle, &stack, &type, &count, numid);
    if (err < 0) {
        debug_error("prepare hw err:%d", err);
        goto error;
    }

    /* set value */
    err = sound_card_hw_io(1, handle, &stack, &type, &count, value, NULL);
    if (err < 0) {
        debug_error("ctl hw io err%d", err);
        goto error;
    }
    snd_ctl_close(handle);
    return 0;

error:
    snd_ctl_close(handle);
    return -1;
}

int set_dual_out(void )
{
    int err = 0;
    int headset_route;

    debug_fenter();

    /* set dual out */
    /*SPK Amp Switch: off*/
    sound_card_ctrl_set(81, 0);
    /*SPKLO Amp Switch: on*/
    sound_card_ctrl_set(79, 1);
    /*EAR Amp Switch: off*/
    sound_card_ctrl_set(80, 0);
    /*HP Amp Switch: off*/
    sound_card_ctrl_set(83, 1);
    debug_log("CLV: set dual out successfully");

    debug_fleave();
    return 0;
}