blob: 424f177a4ebc04e43d4126a4413998ee0a9cc913 [file] [log] [blame]
Yamit Mehtae3b99562016-09-16 22:44:00 +05301/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_utils"
18//#define LOG_NDEBUG 0
19
20#include <errno.h>
21#include <cutils/properties.h>
22#include <cutils/config_utils.h>
23#include <stdlib.h>
24#include <dlfcn.h>
Jiyong Park6431fe62017-06-29 15:15:58 +090025#include <unistd.h>
Yamit Mehtae3b99562016-09-16 22:44:00 +053026#include <cutils/str_parms.h>
27#include <cutils/log.h>
28#include <cutils/misc.h>
29
vivek mehta0fb11312017-05-15 19:35:32 -070030#include "acdb.h"
Yamit Mehtae3b99562016-09-16 22:44:00 +053031#include "audio_hw.h"
32#include "platform.h"
33#include "platform_api.h"
34#include "audio_extn.h"
35
36#define MAX_LENGTH_MIXER_CONTROL_IN_INT 128
37
vivek mehtaa68fea62017-06-08 19:04:02 -070038static int set_stream_app_type_mixer_ctrl(struct audio_device *adev,
39 int pcm_device_id, int app_type,
40 int acdb_dev_id, int sample_rate,
41 int stream_type,
42 snd_device_t snd_device)
Yamit Mehtae3b99562016-09-16 22:44:00 +053043{
44
45 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
46 struct mixer_ctl *ctl;
47 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc = 0;
vivek mehtaa68fea62017-06-08 19:04:02 -070048 int snd_device_be_idx = -1;
Yamit Mehtae3b99562016-09-16 22:44:00 +053049
50 if (stream_type == PCM_PLAYBACK) {
51 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
52 "Audio Stream %d App Type Cfg", pcm_device_id);
53 } else if (stream_type == PCM_CAPTURE) {
54 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
55 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
56 }
57
58 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
59 if (!ctl) {
60 ALOGE("%s: Could not get ctl for mixer cmd - %s",
61 __func__, mixer_ctl_name);
62 rc = -EINVAL;
63 goto exit;
64 }
65 app_type_cfg[len++] = app_type;
66 app_type_cfg[len++] = acdb_dev_id;
67 app_type_cfg[len++] = sample_rate;
vivek mehtaa68fea62017-06-08 19:04:02 -070068
69 snd_device_be_idx = platform_get_snd_device_backend_index(snd_device);
70 if (snd_device_be_idx > 0)
71 app_type_cfg[len++] = snd_device_be_idx;
72 ALOGV("%s: stream type %d app_type %d, acdb_dev_id %d "
73 "sample rate %d, snd_device_be_idx %d",
74 __func__, stream_type, app_type, acdb_dev_id, sample_rate,
75 snd_device_be_idx);
Yamit Mehtae3b99562016-09-16 22:44:00 +053076 mixer_ctl_set_array(ctl, app_type_cfg, len);
77
78exit:
79 return rc;
80}
81
82void audio_extn_utils_send_default_app_type_cfg(void *platform, struct mixer *mixer)
83{
84 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
85 int length = 0, app_type = 0,rc = 0;
86 struct mixer_ctl *ctl = NULL;
87 const char *mixer_ctl_name = "App Type Config";
88
89 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
90 if (!ctl) {
91 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
92 return;
93 }
94 rc = platform_get_default_app_type_v2(platform, PCM_PLAYBACK, &app_type);
95 if (rc == 0) {
96 app_type_cfg[length++] = 1;
97 app_type_cfg[length++] = app_type;
98 app_type_cfg[length++] = 48000;
99 app_type_cfg[length++] = 16;
100 mixer_ctl_set_array(ctl, app_type_cfg, length);
101 }
102 return;
103}
104
vivek mehtaa68fea62017-06-08 19:04:02 -0700105static const char *flags_to_mode(int dir, uint32_t flags)
106{
107 if (dir == 0) {
108 if (flags & AUDIO_OUTPUT_FLAG_VOIP_RX) {
109 return "voip";
110 }
111 } else if (dir == 1) {
112 if (flags & AUDIO_INPUT_FLAG_VOIP_TX) {
113 return "voip";
114 }
115 }
116 return "default";
117}
118
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800119static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev,
Yamit Mehtae3b99562016-09-16 22:44:00 +0530120 struct audio_usecase *usecase)
121{
122 struct mixer_ctl *ctl;
123 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
124 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
125 int app_type = 0, rc = 0;
126
127 ALOGV("%s", __func__);
128
129 if (usecase->type != PCM_HFP_CALL) {
130 ALOGV("%s: not a playback or HFP path, no need to cfg app type", __func__);
131 rc = 0;
132 goto exit_send_app_type_cfg;
133 }
134 if ((usecase->id != USECASE_AUDIO_HFP_SCO) &&
135 (usecase->id != USECASE_AUDIO_HFP_SCO_WB)) {
136 ALOGV("%s: a playback path where app type cfg is not required", __func__);
137 rc = 0;
138 goto exit_send_app_type_cfg;
139 }
140
141 snd_device = usecase->out_snd_device;
142 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
143
144 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
145 audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
146 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
147 if (acdb_dev_id < 0) {
148 ALOGE("%s: Couldn't get the acdb dev id", __func__);
149 rc = -EINVAL;
150 goto exit_send_app_type_cfg;
151 }
152
153 if (usecase->type == PCM_HFP_CALL) {
154
155 /* config HFP session:1 playback path */
156 rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type);
157 if (rc < 0)
158 goto exit_send_app_type_cfg;
159
160 sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
vivek mehtaa68fea62017-06-08 19:04:02 -0700161 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
162 acdb_dev_id, sample_rate,
163 PCM_PLAYBACK,
164 SND_DEVICE_NONE); // use legacy behavior
Yamit Mehtae3b99562016-09-16 22:44:00 +0530165 if (rc < 0)
166 goto exit_send_app_type_cfg;
167 /* config HFP session:1 capture path */
168 rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
169
170 if (rc == 0) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700171 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
172 acdb_dev_id, sample_rate,
173 PCM_CAPTURE,
174 SND_DEVICE_NONE);
Yamit Mehtae3b99562016-09-16 22:44:00 +0530175 if (rc < 0)
176 goto exit_send_app_type_cfg;
177 }
178 /* config HFP session:2 capture path */
179 pcm_device_id = HFP_ASM_RX_TX;
180 snd_device = usecase->in_snd_device;
181 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
182 if (acdb_dev_id <= 0) {
183 ALOGE("%s: Couldn't get the acdb dev id", __func__);
184 rc = -EINVAL;
185 goto exit_send_app_type_cfg;
186 }
187 rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type);
188 if (rc == 0) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700189 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
190 acdb_dev_id, sample_rate, PCM_CAPTURE,
191 SND_DEVICE_NONE);
Yamit Mehtae3b99562016-09-16 22:44:00 +0530192 if (rc < 0)
193 goto exit_send_app_type_cfg;
194 }
195
196 /* config HFP session:2 playback path */
197 rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type);
198 if (rc == 0) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700199 rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type,
200 acdb_dev_id, sample_rate,
201 PCM_PLAYBACK, SND_DEVICE_NONE);
Yamit Mehtae3b99562016-09-16 22:44:00 +0530202 if (rc < 0)
203 goto exit_send_app_type_cfg;
204 }
205 }
206
207 rc = 0;
208exit_send_app_type_cfg:
209 return rc;
210}
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800211
vivek mehtaa68fea62017-06-08 19:04:02 -0700212
213static int derive_capture_app_type_cfg(struct audio_device *adev,
214 struct audio_usecase *usecase,
215 int *app_type,
216 int *sample_rate)
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800217{
Mark Urbanus2e259972017-06-15 16:50:53 -0700218 if (usecase->stream.in == NULL) {
219 return -1;
220 }
vivek mehtaa68fea62017-06-08 19:04:02 -0700221 struct stream_in *in = usecase->stream.in;
222 struct stream_app_type_cfg *app_type_cfg = &in->app_type_cfg;
223
224 *sample_rate = DEFAULT_INPUT_SAMPLING_RATE;
vivek mehta4a824772017-06-08 19:05:49 -0700225 if (audio_is_usb_in_device(in->device)) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700226 platform_check_and_update_copp_sample_rate(adev->platform,
227 usecase->in_snd_device,
228 in->sample_rate,
229 sample_rate);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800230 }
231
vivek mehtaa68fea62017-06-08 19:04:02 -0700232 app_type_cfg->mode = flags_to_mode(1 /*capture*/, in->flags);
233 ALOGV("%s mode %s", __func__, app_type_cfg->mode);
234 if (in->format == AUDIO_FORMAT_PCM_16_BIT) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800235 platform_get_app_type_v2(adev->platform,
vivek mehtaa68fea62017-06-08 19:04:02 -0700236 PCM_CAPTURE,
237 app_type_cfg->mode,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800238 16,
vivek mehtaa68fea62017-06-08 19:04:02 -0700239 app_type_cfg->sample_rate,
240 app_type);
241 } else if (in->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
242 in->format == AUDIO_FORMAT_PCM_8_24_BIT) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800243 platform_get_app_type_v2(adev->platform,
vivek mehtaa68fea62017-06-08 19:04:02 -0700244 PCM_CAPTURE,
245 app_type_cfg->mode,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800246 24,
vivek mehtaa68fea62017-06-08 19:04:02 -0700247 app_type_cfg->sample_rate,
248 app_type);
249 } else if (in->format == AUDIO_FORMAT_PCM_32_BIT) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800250 platform_get_app_type_v2(adev->platform,
vivek mehtaa68fea62017-06-08 19:04:02 -0700251 PCM_CAPTURE,
252 app_type_cfg->mode,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800253 32,
vivek mehtaa68fea62017-06-08 19:04:02 -0700254 app_type_cfg->sample_rate,
255 app_type);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800256 } else {
257 ALOGE("%s bad format\n", __func__);
258 return -1;
259 }
260
vivek mehtaa68fea62017-06-08 19:04:02 -0700261 app_type_cfg->app_type = *app_type;
262 app_type_cfg->sample_rate = *sample_rate;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800263 return 0;
264}
265
vivek mehtaa68fea62017-06-08 19:04:02 -0700266static int derive_playback_app_type_cfg(struct audio_device *adev,
267 struct audio_usecase *usecase,
268 int *app_type,
269 int *sample_rate)
270{
Mark Urbanus2e259972017-06-15 16:50:53 -0700271 if (usecase->stream.out == NULL) {
272 return -1;
273 }
vivek mehtaa68fea62017-06-08 19:04:02 -0700274 struct stream_out *out = usecase->stream.out;
275 struct stream_app_type_cfg *app_type_cfg = &out->app_type_cfg;
276
277 *sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
278
279 // add speaker prot changes if needed
280 // and use that to check for device
vivek mehta4a824772017-06-08 19:05:49 -0700281 if (audio_is_usb_out_device(out->devices)) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700282 platform_check_and_update_copp_sample_rate(adev->platform,
283 usecase->out_snd_device,
284 out->sample_rate,
285 sample_rate);
286 }
287
288 app_type_cfg->mode = flags_to_mode(0 /*playback*/, out->flags);
289 if (!audio_is_linear_pcm(out->format)) {
290 platform_get_default_app_type_v2(adev->platform,
291 PCM_PLAYBACK,
292 app_type);
293 } else if (out->format == AUDIO_FORMAT_PCM_16_BIT) {
294 platform_get_app_type_v2(adev->platform,
295 PCM_PLAYBACK,
296 app_type_cfg->mode,
297 16,
298 *sample_rate,
299 app_type);
300 } else if (out->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
301 out->format == AUDIO_FORMAT_PCM_8_24_BIT) {
302 platform_get_app_type_v2(adev->platform,
303 PCM_PLAYBACK,
304 app_type_cfg->mode,
305 24,
306 *sample_rate,
307 app_type);
308 } else if (out->format == AUDIO_FORMAT_PCM_32_BIT) {
309 platform_get_app_type_v2(adev->platform,
310 PCM_PLAYBACK,
311 app_type_cfg->mode,
312 32,
313 *sample_rate,
314 app_type);
315 } else {
316 ALOGE("%s bad format\n", __func__);
317 return -1;
318 }
319
320 app_type_cfg->app_type = *app_type;
321 app_type_cfg->sample_rate = *sample_rate;
322 return 0;
323}
324
325static int derive_acdb_dev_id(struct audio_device *adev __unused,
326 struct audio_usecase *usecase)
327{
328 struct stream_out *out;
329 struct stream_in *in;
330
331 if (usecase->type == PCM_PLAYBACK) {
332 return platform_get_snd_device_acdb_id(usecase->out_snd_device);
333 } else if(usecase->type == PCM_CAPTURE) {
334 return platform_get_snd_device_acdb_id(usecase->in_snd_device);
335 }
336 return -1;
337}
338
339int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
340 struct audio_usecase *usecase)
341{
342 int len = 0;
343 int sample_rate;
344 int app_type;
345 int acdb_dev_id;
346 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
347 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
348 int pcm_device_id;
349 struct mixer_ctl *ctl;
350 int ret;
351
352 if (usecase->type == PCM_HFP_CALL) {
353 return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase);
354 }
355
356 if (!platform_supports_app_type_cfg())
357 return -1;
358
359 if (usecase->type == PCM_PLAYBACK) {
360 ret = derive_playback_app_type_cfg(adev,
361 usecase,
362 &app_type,
363 &sample_rate);
364 } else if (usecase->type == PCM_CAPTURE) {
365 ret = derive_capture_app_type_cfg(adev,
366 usecase,
367 &app_type,
368 &sample_rate);
369 } else {
370 ALOGE("%s: Invalid uc type : 0x%x", __func__, usecase->type);
371 return -1;
372 }
373
374 if (ret < 0) {
375 ALOGE("%s: Failed to derive app_type for uc type : 0x%x", __func__,
376 usecase->type);
377 return -1;
378 }
379
380 acdb_dev_id = derive_acdb_dev_id(adev, usecase);
381 if (acdb_dev_id <= 0) {
382 ALOGE("%s: Couldn't get the acdb dev id", __func__);
383 return -1;
384 }
385
386 pcm_device_id = platform_get_pcm_device_id(usecase->id, usecase->type);
387 set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, acdb_dev_id,
388 sample_rate,
389 usecase->type,
390 usecase->type == PCM_PLAYBACK ? usecase->out_snd_device :
391 usecase->in_snd_device);
392 return 0;
393}
394
vivek mehta4a824772017-06-08 19:05:49 -0700395int audio_extn_utils_send_app_type_gain(struct audio_device *adev,
396 int app_type,
397 int *gain)
398{
399 int gain_cfg[4];
400 const char *mixer_ctl_name = "App Type Gain";
401 struct mixer_ctl *ctl;
402 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
403 if (!ctl) {
404 ALOGE("%s: Could not get volume ctl mixer %s", __func__,
405 mixer_ctl_name);
406 return -EINVAL;
407 }
408 gain_cfg[0] = 0;
409 gain_cfg[1] = app_type;
410 gain_cfg[2] = gain[0];
411 gain_cfg[3] = gain[1];
412 ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]);
413 return mixer_ctl_set_array(ctl, gain_cfg,
414 sizeof(gain_cfg)/sizeof(gain_cfg[0]));
415}
416
vivek mehtaa68fea62017-06-08 19:04:02 -0700417// this assumes correct app_type and sample_rate fields
418// have been set for the stream using audio_extn_utils_send_app_type_cfg
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800419void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
420 struct audio_usecase *usecase)
421{
422 int type = usecase->type;
423 int app_type = 0;
424
425 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
426 struct stream_out *out = usecase->stream.out;
vivek mehtaa68fea62017-06-08 19:04:02 -0700427 ALOGV("%s send cal for app_type %d, rate %d", __func__,
428 out->app_type_cfg.app_type,
429 out->app_type_cfg.sample_rate);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800430 platform_send_audio_calibration_v2(adev->platform, usecase,
vivek mehtaa68fea62017-06-08 19:04:02 -0700431 out->app_type_cfg.app_type,
432 out->app_type_cfg.sample_rate);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800433 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
vivek mehtaa68fea62017-06-08 19:04:02 -0700434 struct stream_in *in = usecase->stream.in;
435 ALOGV("%s send cal for capture app_type %d, rate %d", __func__,
436 in->app_type_cfg.app_type,
437 in->app_type_cfg.sample_rate);
438 platform_send_audio_calibration_v2(adev->platform, usecase,
439 in->app_type_cfg.app_type,
440 in->app_type_cfg.sample_rate);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800441 } else {
442 /* when app type is default. the sample rate is not used to send cal */
443 platform_get_default_app_type_v2(adev->platform, type, &app_type);
vivek mehtaa68fea62017-06-08 19:04:02 -0700444 platform_send_audio_calibration_v2(adev->platform, usecase, app_type,
445 48000);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800446 }
447}
vivek mehta0fb11312017-05-15 19:35:32 -0700448
449#define MAX_SND_CARD 8
450#define RETRY_US 500000
451#define RETRY_NUMBER 10
452
453#define min(a, b) ((a) < (b) ? (a) : (b))
454
455static const char *kConfigLocationList[] =
456 {"/odm/etc", "/vendor/etc", "/system/etc"};
457static const int kConfigLocationListSize =
458 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
459
460bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH])
461{
462 char full_config_path[MIXER_PATH_MAX_LENGTH];
463 for (int i = 0; i < kConfigLocationListSize; i++) {
464 snprintf(full_config_path,
465 MIXER_PATH_MAX_LENGTH,
466 "%s/%s",
467 kConfigLocationList[i],
468 file_name);
469 if (F_OK == access(full_config_path, 0)) {
470 strcpy(file_name, full_config_path);
471 return true;
472 }
473 }
474 return false;
475}
476
477/* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700478int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file)
vivek mehta0fb11312017-05-15 19:35:32 -0700479{
480 if (NULL == snd_card_name) {
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700481 return -1;
vivek mehta0fb11312017-05-15 19:35:32 -0700482 }
483
484 struct snd_card_split *snd_split_handle = NULL;
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700485 int ret = 0;
vivek mehta0fb11312017-05-15 19:35:32 -0700486 audio_extn_set_snd_card_split(snd_card_name);
487 snd_split_handle = audio_extn_get_snd_card_split();
488
489 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml",
490 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card,
491 snd_split_handle->form_factor);
492
493 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
494 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
495 snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml",
496 PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card);
497
498 if (!audio_extn_utils_resolve_config_file(platform_info_file)) {
499 memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH);
500 strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700501 ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1;
vivek mehta0fb11312017-05-15 19:35:32 -0700502 }
503 }
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700504
505 return ret;
vivek mehta0fb11312017-05-15 19:35:32 -0700506}
507
508int audio_extn_utils_get_snd_card_num()
509{
510
511 void *hw_info = NULL;
512 struct mixer *mixer = NULL;
513 int retry_num = 0;
514 int snd_card_num = 0;
515 const char* snd_card_name = NULL;
516 char platform_info_file[MIXER_PATH_MAX_LENGTH]= {0};
517
518 struct acdb_platform_data *my_data = calloc(1, sizeof(struct acdb_platform_data));
519
520 bool card_verifed[MAX_SND_CARD] = {0};
521 const int retry_limit = property_get_int32("audio.snd_card.open.retries", RETRY_NUMBER);
522
523 for (;;) {
524 if (snd_card_num >= MAX_SND_CARD) {
525 if (retry_num++ >= retry_limit) {
526 ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
527 snd_card_num = -1;
528 goto done;
529 }
530
531 snd_card_num = 0;
532 usleep(RETRY_US);
533 continue;
534 }
535
536 if (card_verifed[snd_card_num]) {
537 ++snd_card_num;
538 continue;
539 }
540
541 mixer = mixer_open(snd_card_num);
542
543 if (!mixer) {
544 ALOGE("%s: Unable to open the mixer card: %d", __func__,
545 snd_card_num);
546 ++snd_card_num;
547 continue;
548 }
549
550 card_verifed[snd_card_num] = true;
551
552 snd_card_name = mixer_get_name(mixer);
553 hw_info = hw_info_init(snd_card_name);
554
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700555 if (audio_extn_utils_get_platform_info(snd_card_name, platform_info_file) < 0) {
556 ALOGE("Failed to find platform_info_file");
557 goto cleanup;
558 }
vivek mehta0fb11312017-05-15 19:35:32 -0700559
560 /* Initialize snd card name specific ids and/or backends*/
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700561 if (snd_card_info_init(platform_info_file, my_data,
562 &acdb_set_parameters) < 0) {
563 ALOGE("Failed to find platform_info_file");
564 goto cleanup;
565 }
vivek mehta0fb11312017-05-15 19:35:32 -0700566
567 /* validate the sound card name
568 * my_data->snd_card_name can contain
569 * <a> complete sound card name, i.e. <device>-<codec>-<form_factor>-snd-card
570 * example: msm8994-tomtom-mtp-snd-card
571 * <b> or sub string of the card name, i.e. <device>-<codec>
572 * example: msm8994-tomtom
573 * snd_card_name is truncated to 32 charaters as per mixer_get_name() implementation
574 * so use min of my_data->snd_card_name and snd_card_name length for comparison
575 */
576
577 if (my_data->snd_card_name != NULL &&
578 strncmp(snd_card_name, my_data->snd_card_name,
579 min(strlen(snd_card_name), strlen(my_data->snd_card_name))) != 0) {
580 ALOGI("%s: found valid sound card %s, but not primary sound card %s",
581 __func__, snd_card_name, my_data->snd_card_name);
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700582 goto cleanup;
vivek mehta0fb11312017-05-15 19:35:32 -0700583 }
584
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700585 ALOGI("%s: found sound card %s, primary sound card expected is %s",
vivek mehta0fb11312017-05-15 19:35:32 -0700586 __func__, snd_card_name, my_data->snd_card_name);
587 break;
Haynes Mathew George66ff30c2017-06-01 20:24:42 -0700588 cleanup:
589 ++snd_card_num;
590 mixer_close(mixer);
591 mixer = NULL;
592 hw_info_deinit(hw_info);
593 hw_info = NULL;
vivek mehta0fb11312017-05-15 19:35:32 -0700594 }
595
596done:
597 mixer_close(mixer);
598 hw_info_deinit(hw_info);
599
600 if (my_data)
601 free(my_data);
602
603 return snd_card_num;
604}