Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 1 | /* |
| 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 Park | 6431fe6 | 2017-06-29 15:15:58 +0900 | [diff] [blame] | 25 | #include <unistd.h> |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 26 | #include <cutils/str_parms.h> |
Haynes Mathew George | e6e2d44 | 2018-02-22 18:51:56 -0800 | [diff] [blame] | 27 | #include <log/log.h> |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 28 | #include <cutils/misc.h> |
| 29 | |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 30 | #include "acdb.h" |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 31 | #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 mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 38 | static 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 Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 43 | { |
| 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 mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 48 | int snd_device_be_idx = -1; |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 49 | |
| 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 mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 68 | |
| 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 Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 76 | mixer_ctl_set_array(ctl, app_type_cfg, len); |
| 77 | |
| 78 | exit: |
| 79 | return rc; |
| 80 | } |
| 81 | |
| 82 | void 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 mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 105 | static 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 George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 119 | static int audio_extn_utils_send_app_type_cfg_hfp(struct audio_device *adev, |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 120 | 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 | |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 144 | acdb_dev_id = platform_get_snd_device_acdb_id(snd_device); |
| 145 | if (acdb_dev_id < 0) { |
| 146 | ALOGE("%s: Couldn't get the acdb dev id", __func__); |
| 147 | rc = -EINVAL; |
| 148 | goto exit_send_app_type_cfg; |
| 149 | } |
| 150 | |
| 151 | if (usecase->type == PCM_HFP_CALL) { |
| 152 | |
| 153 | /* config HFP session:1 playback path */ |
| 154 | rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type); |
| 155 | if (rc < 0) |
| 156 | goto exit_send_app_type_cfg; |
| 157 | |
| 158 | sample_rate= CODEC_BACKEND_DEFAULT_SAMPLE_RATE; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 159 | rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, |
| 160 | acdb_dev_id, sample_rate, |
| 161 | PCM_PLAYBACK, |
| 162 | SND_DEVICE_NONE); // use legacy behavior |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 163 | if (rc < 0) |
| 164 | goto exit_send_app_type_cfg; |
| 165 | /* config HFP session:1 capture path */ |
| 166 | rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type); |
| 167 | |
| 168 | if (rc == 0) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 169 | rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, |
| 170 | acdb_dev_id, sample_rate, |
| 171 | PCM_CAPTURE, |
| 172 | SND_DEVICE_NONE); |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 173 | if (rc < 0) |
| 174 | goto exit_send_app_type_cfg; |
| 175 | } |
| 176 | /* config HFP session:2 capture path */ |
| 177 | pcm_device_id = HFP_ASM_RX_TX; |
| 178 | snd_device = usecase->in_snd_device; |
| 179 | acdb_dev_id = platform_get_snd_device_acdb_id(snd_device); |
| 180 | if (acdb_dev_id <= 0) { |
| 181 | ALOGE("%s: Couldn't get the acdb dev id", __func__); |
| 182 | rc = -EINVAL; |
| 183 | goto exit_send_app_type_cfg; |
| 184 | } |
| 185 | rc = platform_get_default_app_type_v2(adev->platform, PCM_CAPTURE, &app_type); |
| 186 | if (rc == 0) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 187 | rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, |
| 188 | acdb_dev_id, sample_rate, PCM_CAPTURE, |
| 189 | SND_DEVICE_NONE); |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 190 | if (rc < 0) |
| 191 | goto exit_send_app_type_cfg; |
| 192 | } |
| 193 | |
| 194 | /* config HFP session:2 playback path */ |
| 195 | rc = platform_get_default_app_type_v2(adev->platform, PCM_PLAYBACK, &app_type); |
| 196 | if (rc == 0) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 197 | rc = set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, |
| 198 | acdb_dev_id, sample_rate, |
| 199 | PCM_PLAYBACK, SND_DEVICE_NONE); |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 200 | if (rc < 0) |
| 201 | goto exit_send_app_type_cfg; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | rc = 0; |
| 206 | exit_send_app_type_cfg: |
| 207 | return rc; |
| 208 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 209 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 210 | |
| 211 | static int derive_capture_app_type_cfg(struct audio_device *adev, |
| 212 | struct audio_usecase *usecase, |
| 213 | int *app_type, |
| 214 | int *sample_rate) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 215 | { |
Mark Urbanus | 2e25997 | 2017-06-15 16:50:53 -0700 | [diff] [blame] | 216 | if (usecase->stream.in == NULL) { |
| 217 | return -1; |
| 218 | } |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 219 | struct stream_in *in = usecase->stream.in; |
| 220 | struct stream_app_type_cfg *app_type_cfg = &in->app_type_cfg; |
| 221 | |
| 222 | *sample_rate = DEFAULT_INPUT_SAMPLING_RATE; |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 223 | if (audio_is_usb_in_device(in->device)) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 224 | platform_check_and_update_copp_sample_rate(adev->platform, |
| 225 | usecase->in_snd_device, |
| 226 | in->sample_rate, |
| 227 | sample_rate); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 228 | } |
| 229 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 230 | app_type_cfg->mode = flags_to_mode(1 /*capture*/, in->flags); |
| 231 | ALOGV("%s mode %s", __func__, app_type_cfg->mode); |
| 232 | if (in->format == AUDIO_FORMAT_PCM_16_BIT) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 233 | platform_get_app_type_v2(adev->platform, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 234 | PCM_CAPTURE, |
| 235 | app_type_cfg->mode, |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 236 | 16, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 237 | app_type_cfg->sample_rate, |
| 238 | app_type); |
| 239 | } else if (in->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 240 | in->format == AUDIO_FORMAT_PCM_8_24_BIT) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 241 | platform_get_app_type_v2(adev->platform, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 242 | PCM_CAPTURE, |
| 243 | app_type_cfg->mode, |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 244 | 24, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 245 | app_type_cfg->sample_rate, |
| 246 | app_type); |
| 247 | } else if (in->format == AUDIO_FORMAT_PCM_32_BIT) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 248 | platform_get_app_type_v2(adev->platform, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 249 | PCM_CAPTURE, |
| 250 | app_type_cfg->mode, |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 251 | 32, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 252 | app_type_cfg->sample_rate, |
| 253 | app_type); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 254 | } else { |
| 255 | ALOGE("%s bad format\n", __func__); |
| 256 | return -1; |
| 257 | } |
| 258 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 259 | app_type_cfg->app_type = *app_type; |
| 260 | app_type_cfg->sample_rate = *sample_rate; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 264 | static int derive_playback_app_type_cfg(struct audio_device *adev, |
| 265 | struct audio_usecase *usecase, |
| 266 | int *app_type, |
| 267 | int *sample_rate) |
| 268 | { |
Mark Urbanus | 2e25997 | 2017-06-15 16:50:53 -0700 | [diff] [blame] | 269 | if (usecase->stream.out == NULL) { |
| 270 | return -1; |
| 271 | } |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 272 | struct stream_out *out = usecase->stream.out; |
| 273 | struct stream_app_type_cfg *app_type_cfg = &out->app_type_cfg; |
| 274 | |
| 275 | *sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 276 | |
| 277 | // add speaker prot changes if needed |
| 278 | // and use that to check for device |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 279 | if (audio_is_usb_out_device(out->devices)) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 280 | platform_check_and_update_copp_sample_rate(adev->platform, |
| 281 | usecase->out_snd_device, |
| 282 | out->sample_rate, |
| 283 | sample_rate); |
| 284 | } |
| 285 | |
| 286 | app_type_cfg->mode = flags_to_mode(0 /*playback*/, out->flags); |
| 287 | if (!audio_is_linear_pcm(out->format)) { |
Haynes Mathew George | df0f886 | 2017-06-28 13:36:40 -0700 | [diff] [blame] | 288 | platform_get_app_type_v2(adev->platform, |
| 289 | PCM_PLAYBACK, |
| 290 | app_type_cfg->mode, |
| 291 | 24, |
| 292 | *sample_rate, |
| 293 | app_type); |
| 294 | ALOGV("Non pcm got app type %d", *app_type); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 295 | } else if (out->format == AUDIO_FORMAT_PCM_16_BIT) { |
| 296 | platform_get_app_type_v2(adev->platform, |
| 297 | PCM_PLAYBACK, |
| 298 | app_type_cfg->mode, |
| 299 | 16, |
| 300 | *sample_rate, |
| 301 | app_type); |
| 302 | } else if (out->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 303 | out->format == AUDIO_FORMAT_PCM_8_24_BIT) { |
| 304 | platform_get_app_type_v2(adev->platform, |
| 305 | PCM_PLAYBACK, |
| 306 | app_type_cfg->mode, |
| 307 | 24, |
| 308 | *sample_rate, |
| 309 | app_type); |
| 310 | } else if (out->format == AUDIO_FORMAT_PCM_32_BIT) { |
| 311 | platform_get_app_type_v2(adev->platform, |
| 312 | PCM_PLAYBACK, |
| 313 | app_type_cfg->mode, |
| 314 | 32, |
| 315 | *sample_rate, |
| 316 | app_type); |
| 317 | } else { |
| 318 | ALOGE("%s bad format\n", __func__); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | app_type_cfg->app_type = *app_type; |
| 323 | app_type_cfg->sample_rate = *sample_rate; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int derive_acdb_dev_id(struct audio_device *adev __unused, |
| 328 | struct audio_usecase *usecase) |
| 329 | { |
| 330 | struct stream_out *out; |
| 331 | struct stream_in *in; |
| 332 | |
| 333 | if (usecase->type == PCM_PLAYBACK) { |
Aniket Kumar Lata | 9d6679a | 2018-04-11 18:13:23 -0700 | [diff] [blame] | 334 | return platform_get_snd_device_acdb_id(usecase->out_snd_device); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 335 | } else if(usecase->type == PCM_CAPTURE) { |
| 336 | return platform_get_snd_device_acdb_id(usecase->in_snd_device); |
| 337 | } |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | int audio_extn_utils_send_app_type_cfg(struct audio_device *adev, |
| 342 | struct audio_usecase *usecase) |
| 343 | { |
| 344 | int len = 0; |
| 345 | int sample_rate; |
| 346 | int app_type; |
| 347 | int acdb_dev_id; |
juyuchen | 220b32e | 2018-05-31 16:14:26 +0800 | [diff] [blame^] | 348 | int new_snd_device[2] = {0}; |
| 349 | int i = 0, num_devices = 1; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 350 | size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0}; |
| 351 | char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0}; |
| 352 | int pcm_device_id; |
| 353 | struct mixer_ctl *ctl; |
| 354 | int ret; |
| 355 | |
| 356 | if (usecase->type == PCM_HFP_CALL) { |
| 357 | return audio_extn_utils_send_app_type_cfg_hfp(adev, usecase); |
| 358 | } |
| 359 | |
| 360 | if (!platform_supports_app_type_cfg()) |
| 361 | return -1; |
| 362 | |
| 363 | if (usecase->type == PCM_PLAYBACK) { |
| 364 | ret = derive_playback_app_type_cfg(adev, |
| 365 | usecase, |
| 366 | &app_type, |
| 367 | &sample_rate); |
| 368 | } else if (usecase->type == PCM_CAPTURE) { |
| 369 | ret = derive_capture_app_type_cfg(adev, |
| 370 | usecase, |
| 371 | &app_type, |
| 372 | &sample_rate); |
| 373 | } else { |
| 374 | ALOGE("%s: Invalid uc type : 0x%x", __func__, usecase->type); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | if (ret < 0) { |
| 379 | ALOGE("%s: Failed to derive app_type for uc type : 0x%x", __func__, |
| 380 | usecase->type); |
| 381 | return -1; |
| 382 | } |
| 383 | |
juyuchen | 220b32e | 2018-05-31 16:14:26 +0800 | [diff] [blame^] | 384 | if (usecase->type == PCM_PLAYBACK) { |
| 385 | if (platform_can_split_snd_device(usecase->out_snd_device, |
| 386 | &num_devices, new_snd_device) < 0) |
| 387 | new_snd_device[0] = usecase->out_snd_device; |
| 388 | |
| 389 | } else if (usecase->type == PCM_CAPTURE) |
| 390 | new_snd_device[0] = usecase->in_snd_device; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 391 | |
| 392 | pcm_device_id = platform_get_pcm_device_id(usecase->id, usecase->type); |
juyuchen | 220b32e | 2018-05-31 16:14:26 +0800 | [diff] [blame^] | 393 | |
| 394 | for (i = 0; i < num_devices; i++) { |
| 395 | acdb_dev_id = platform_get_snd_device_acdb_id(new_snd_device[i]); |
| 396 | |
| 397 | if (acdb_dev_id < 0) { |
| 398 | ALOGE("%s: Could not find acdb id for device(%d)", |
| 399 | __func__, new_snd_device[i]); |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | ALOGV("%s: sending app type for snd_device(%d) acdb_id(%d) i %d", |
| 403 | __func__, new_snd_device[i], acdb_dev_id, i); |
| 404 | |
| 405 | set_stream_app_type_mixer_ctrl(adev, pcm_device_id, app_type, acdb_dev_id, |
| 406 | sample_rate, |
| 407 | usecase->type, |
| 408 | new_snd_device[i]); |
| 409 | } |
| 410 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 414 | int audio_extn_utils_send_app_type_gain(struct audio_device *adev, |
| 415 | int app_type, |
| 416 | int *gain) |
| 417 | { |
| 418 | int gain_cfg[4]; |
| 419 | const char *mixer_ctl_name = "App Type Gain"; |
| 420 | struct mixer_ctl *ctl; |
| 421 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 422 | if (!ctl) { |
| 423 | ALOGE("%s: Could not get volume ctl mixer %s", __func__, |
| 424 | mixer_ctl_name); |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | gain_cfg[0] = 0; |
| 428 | gain_cfg[1] = app_type; |
| 429 | gain_cfg[2] = gain[0]; |
| 430 | gain_cfg[3] = gain[1]; |
| 431 | ALOGV("%s app_type %d l(%d) r(%d)", __func__, app_type, gain[0], gain[1]); |
| 432 | return mixer_ctl_set_array(ctl, gain_cfg, |
| 433 | sizeof(gain_cfg)/sizeof(gain_cfg[0])); |
| 434 | } |
| 435 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 436 | // this assumes correct app_type and sample_rate fields |
| 437 | // have been set for the stream using audio_extn_utils_send_app_type_cfg |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 438 | void audio_extn_utils_send_audio_calibration(struct audio_device *adev, |
| 439 | struct audio_usecase *usecase) |
| 440 | { |
| 441 | int type = usecase->type; |
| 442 | int app_type = 0; |
| 443 | |
| 444 | if (type == PCM_PLAYBACK && usecase->stream.out != NULL) { |
| 445 | struct stream_out *out = usecase->stream.out; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 446 | ALOGV("%s send cal for app_type %d, rate %d", __func__, |
| 447 | out->app_type_cfg.app_type, |
| 448 | out->app_type_cfg.sample_rate); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 449 | platform_send_audio_calibration_v2(adev->platform, usecase, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 450 | out->app_type_cfg.app_type, |
| 451 | out->app_type_cfg.sample_rate); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 452 | } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) { |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 453 | struct stream_in *in = usecase->stream.in; |
| 454 | ALOGV("%s send cal for capture app_type %d, rate %d", __func__, |
| 455 | in->app_type_cfg.app_type, |
| 456 | in->app_type_cfg.sample_rate); |
| 457 | platform_send_audio_calibration_v2(adev->platform, usecase, |
| 458 | in->app_type_cfg.app_type, |
| 459 | in->app_type_cfg.sample_rate); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 460 | } else { |
| 461 | /* when app type is default. the sample rate is not used to send cal */ |
| 462 | platform_get_default_app_type_v2(adev->platform, type, &app_type); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 463 | platform_send_audio_calibration_v2(adev->platform, usecase, app_type, |
| 464 | 48000); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 467 | |
| 468 | #define MAX_SND_CARD 8 |
| 469 | #define RETRY_US 500000 |
| 470 | #define RETRY_NUMBER 10 |
| 471 | |
| 472 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 473 | |
| 474 | static const char *kConfigLocationList[] = |
| 475 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 476 | static const int kConfigLocationListSize = |
| 477 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 478 | |
| 479 | bool audio_extn_utils_resolve_config_file(char file_name[MIXER_PATH_MAX_LENGTH]) |
| 480 | { |
| 481 | char full_config_path[MIXER_PATH_MAX_LENGTH]; |
| 482 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 483 | snprintf(full_config_path, |
| 484 | MIXER_PATH_MAX_LENGTH, |
| 485 | "%s/%s", |
| 486 | kConfigLocationList[i], |
| 487 | file_name); |
| 488 | if (F_OK == access(full_config_path, 0)) { |
| 489 | strcpy(file_name, full_config_path); |
| 490 | return true; |
| 491 | } |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | /* platform_info_file should be size 'MIXER_PATH_MAX_LENGTH' */ |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 497 | int audio_extn_utils_get_platform_info(const char* snd_card_name, char* platform_info_file) |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 498 | { |
| 499 | if (NULL == snd_card_name) { |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 500 | return -1; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | struct snd_card_split *snd_split_handle = NULL; |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 504 | int ret = 0; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 505 | audio_extn_set_snd_card_split(snd_card_name); |
| 506 | snd_split_handle = audio_extn_get_snd_card_split(); |
| 507 | |
| 508 | snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s_%s.xml", |
| 509 | PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card, |
| 510 | snd_split_handle->form_factor); |
| 511 | |
| 512 | if (!audio_extn_utils_resolve_config_file(platform_info_file)) { |
| 513 | memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH); |
| 514 | snprintf(platform_info_file, MIXER_PATH_MAX_LENGTH, "%s_%s.xml", |
| 515 | PLATFORM_INFO_XML_BASE_STRING, snd_split_handle->snd_card); |
| 516 | |
| 517 | if (!audio_extn_utils_resolve_config_file(platform_info_file)) { |
| 518 | memset(platform_info_file, 0, MIXER_PATH_MAX_LENGTH); |
| 519 | strlcpy(platform_info_file, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH); |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 520 | ret = audio_extn_utils_resolve_config_file(platform_info_file) ? 0 : -1; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 523 | |
| 524 | return ret; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int audio_extn_utils_get_snd_card_num() |
| 528 | { |
| 529 | |
| 530 | void *hw_info = NULL; |
| 531 | struct mixer *mixer = NULL; |
| 532 | int retry_num = 0; |
| 533 | int snd_card_num = 0; |
| 534 | const char* snd_card_name = NULL; |
| 535 | char platform_info_file[MIXER_PATH_MAX_LENGTH]= {0}; |
| 536 | |
| 537 | struct acdb_platform_data *my_data = calloc(1, sizeof(struct acdb_platform_data)); |
| 538 | |
| 539 | bool card_verifed[MAX_SND_CARD] = {0}; |
| 540 | const int retry_limit = property_get_int32("audio.snd_card.open.retries", RETRY_NUMBER); |
| 541 | |
| 542 | for (;;) { |
| 543 | if (snd_card_num >= MAX_SND_CARD) { |
| 544 | if (retry_num++ >= retry_limit) { |
| 545 | ALOGE("%s: Unable to find correct sound card, aborting.", __func__); |
| 546 | snd_card_num = -1; |
| 547 | goto done; |
| 548 | } |
| 549 | |
| 550 | snd_card_num = 0; |
| 551 | usleep(RETRY_US); |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | if (card_verifed[snd_card_num]) { |
| 556 | ++snd_card_num; |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | mixer = mixer_open(snd_card_num); |
| 561 | |
| 562 | if (!mixer) { |
| 563 | ALOGE("%s: Unable to open the mixer card: %d", __func__, |
| 564 | snd_card_num); |
| 565 | ++snd_card_num; |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | card_verifed[snd_card_num] = true; |
| 570 | |
| 571 | snd_card_name = mixer_get_name(mixer); |
| 572 | hw_info = hw_info_init(snd_card_name); |
| 573 | |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 574 | if (audio_extn_utils_get_platform_info(snd_card_name, platform_info_file) < 0) { |
| 575 | ALOGE("Failed to find platform_info_file"); |
| 576 | goto cleanup; |
| 577 | } |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 578 | |
| 579 | /* Initialize snd card name specific ids and/or backends*/ |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 580 | if (snd_card_info_init(platform_info_file, my_data, |
| 581 | &acdb_set_parameters) < 0) { |
| 582 | ALOGE("Failed to find platform_info_file"); |
| 583 | goto cleanup; |
| 584 | } |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 585 | |
| 586 | /* validate the sound card name |
| 587 | * my_data->snd_card_name can contain |
| 588 | * <a> complete sound card name, i.e. <device>-<codec>-<form_factor>-snd-card |
| 589 | * example: msm8994-tomtom-mtp-snd-card |
| 590 | * <b> or sub string of the card name, i.e. <device>-<codec> |
| 591 | * example: msm8994-tomtom |
| 592 | * snd_card_name is truncated to 32 charaters as per mixer_get_name() implementation |
| 593 | * so use min of my_data->snd_card_name and snd_card_name length for comparison |
| 594 | */ |
| 595 | |
| 596 | if (my_data->snd_card_name != NULL && |
| 597 | strncmp(snd_card_name, my_data->snd_card_name, |
| 598 | min(strlen(snd_card_name), strlen(my_data->snd_card_name))) != 0) { |
| 599 | ALOGI("%s: found valid sound card %s, but not primary sound card %s", |
| 600 | __func__, snd_card_name, my_data->snd_card_name); |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 601 | goto cleanup; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 604 | ALOGI("%s: found sound card %s, primary sound card expected is %s", |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 605 | __func__, snd_card_name, my_data->snd_card_name); |
| 606 | break; |
Haynes Mathew George | 66ff30c | 2017-06-01 20:24:42 -0700 | [diff] [blame] | 607 | cleanup: |
| 608 | ++snd_card_num; |
| 609 | mixer_close(mixer); |
| 610 | mixer = NULL; |
| 611 | hw_info_deinit(hw_info); |
| 612 | hw_info = NULL; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | done: |
| 616 | mixer_close(mixer); |
| 617 | hw_info_deinit(hw_info); |
| 618 | |
| 619 | if (my_data) |
| 620 | free(my_data); |
| 621 | |
| 622 | return snd_card_num; |
| 623 | } |