1/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *   * Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 *   * Redistributions in binary form must reproduce the above
10 *     copyright notice, this list of conditions and the following
11 *     disclaimer in the documentation and/or other materials provided
12 *     with the distribution.
13 *   * Neither the name of The Linux Foundation nor the names of its
14 *     contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define LOG_TAG "audio_hw_dev_arbi"
31/*#define LOG_NDEBUG 0*/
32#define LOG_NDDEBUG 0
33
34#include <errno.h>
35#include <cutils/log.h>
36#include <fcntl.h>
37#include "audio_hw.h"
38#include "platform.h"
39#include "platform_api.h"
40#include <sys/stat.h>
41#include <stdlib.h>
42#include <dlfcn.h>
43#include <cutils/properties.h>
44#include "audio_extn.h"
45
46#ifdef DEV_ARBI_ENABLED
47
48typedef int (init_fn_t)();
49typedef int (deinit_fn_t)();
50typedef int (acquire_fn_t)(audio_devices_t aud_dev);
51typedef int (release_fn_t)(audio_devices_t aud_dev);
52
53typedef struct {
54    snd_device_t snd_device;
55    audio_devices_t aud_device;
56} snd_aud_dev_mapping_t;
57
58static void* lib_handle = NULL;
59
60static init_fn_t *init_fp = NULL;
61static deinit_fn_t *deinit_fp = NULL;
62static acquire_fn_t *acquire_fp = NULL;
63static release_fn_t *release_fp = NULL;
64
65static int load_dev_arbi_lib()
66{
67    int rc = -EINVAL;
68
69    if (lib_handle != NULL) {
70        ALOGE("%s: library already loaded", __func__);
71        return rc;
72    }
73
74    lib_handle = dlopen("libaudiodevarb.so", RTLD_NOW);
75    if (lib_handle != NULL) {
76        init_fp = (init_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_init");
77        deinit_fp = (deinit_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_deinit");
78        acquire_fp = (acquire_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_acquire");
79        release_fp = (release_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_release");
80
81        if ((init_fp == NULL) ||
82            (deinit_fp == NULL) ||
83            (acquire_fp == NULL) ||
84            (release_fp == NULL)) {
85
86            ALOGE("%s: error loading symbols from library", __func__);
87
88            init_fp = NULL;
89            deinit_fp = NULL;
90            acquire_fp = NULL;
91            release_fp = NULL;
92        } else
93            return 0;
94    }
95
96    return rc;
97}
98
99int audio_extn_dev_arbi_init()
100{
101    int rc = load_dev_arbi_lib();
102    if (!rc)
103        rc = init_fp();
104
105    return rc;
106}
107
108int audio_extn_dev_arbi_deinit()
109{
110    int rc = -EINVAL;
111
112    if(deinit_fp != NULL) {
113        rc = deinit_fp();
114
115        init_fp = NULL;
116        deinit_fp = NULL;
117        acquire_fp = NULL;
118        release_fp = NULL;
119
120        dlclose(lib_handle);
121        lib_handle = NULL;
122    }
123
124    return rc;
125}
126
127static audio_devices_t get_audio_device(snd_device_t snd_device)
128{
129    static snd_aud_dev_mapping_t snd_aud_dev_map[] = {
130        {SND_DEVICE_OUT_HANDSET, AUDIO_DEVICE_OUT_EARPIECE},
131        {SND_DEVICE_OUT_VOICE_HANDSET, AUDIO_DEVICE_OUT_EARPIECE}
132    };
133
134    audio_devices_t aud_device = AUDIO_DEVICE_NONE;
135    uint32_t ind = 0;
136
137    for (ind = 0; ind < ARRAY_SIZE(snd_aud_dev_map); ++ind) {
138        if (snd_device == snd_aud_dev_map[ind].snd_device) {
139            aud_device = snd_aud_dev_map[ind].aud_device;
140            break;
141        }
142    }
143
144    return aud_device;
145}
146
147int audio_extn_dev_arbi_acquire(snd_device_t snd_device)
148{
149    int rc = -EINVAL;
150    audio_devices_t audio_device = get_audio_device(snd_device);
151
152    if ((acquire_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
153        rc = acquire_fp(audio_device);
154
155    return rc;
156}
157
158int audio_extn_dev_arbi_release(snd_device_t snd_device)
159{
160    int rc = -EINVAL;
161    audio_devices_t audio_device = get_audio_device(snd_device);
162
163    if ((release_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
164        rc = release_fp(audio_device);
165
166    return rc;
167}
168
169#endif /*DEV_ARBI_ENABLED*/
170