1 /* 2 * Copyright (C) 2013-2014 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 #ifndef QCOM_AUDIO_HW_H 18 #define QCOM_AUDIO_HW_H 19 20 #include <cutils/str_parms.h> 21 #include <cutils/list.h> 22 #include <hardware/audio.h> 23 24 #include <tinyalsa/asoundlib.h> 25 #include <tinycompress/tinycompress.h> 26 27 #include <audio_route/audio_route.h> 28 #include "voice.h" 29 30 #define VISUALIZER_LIBRARY_PATH "/system/lib/soundfx/libqcomvisualizer.so" 31 #define OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH "/system/lib/soundfx/libqcompostprocbundle.so" 32 #define ADM_LIBRARY_PATH "/system/vendor/lib/libadm.so" 33 34 /* Flags used to initialize acdb_settings variable that goes to ACDB library */ 35 #define DMIC_FLAG 0x00000002 36 #define TTY_MODE_OFF 0x00000010 37 #define TTY_MODE_FULL 0x00000020 38 #define TTY_MODE_VCO 0x00000040 39 #define TTY_MODE_HCO 0x00000080 40 #define TTY_MODE_CLEAR 0xFFFFFF0F 41 42 #define ACDB_DEV_TYPE_OUT 1 43 #define ACDB_DEV_TYPE_IN 2 44 45 #define MAX_SUPPORTED_CHANNEL_MASKS 2 46 #define DEFAULT_HDMI_OUT_CHANNELS 2 47 48 typedef int snd_device_t; 49 50 /* These are the supported use cases by the hardware. 51 * Each usecase is mapped to a specific PCM device. 52 * Refer to pcm_device_table[]. 53 */ 54 enum { 55 USECASE_INVALID = -1, 56 /* Playback usecases */ 57 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0, 58 USECASE_AUDIO_PLAYBACK_LOW_LATENCY, 59 USECASE_AUDIO_PLAYBACK_MULTI_CH, 60 USECASE_AUDIO_PLAYBACK_OFFLOAD, 61 USECASE_AUDIO_PLAYBACK_TTS, 62 USECASE_AUDIO_PLAYBACK_ULL, 63 64 /* HFP Use case*/ 65 USECASE_AUDIO_HFP_SCO, 66 USECASE_AUDIO_HFP_SCO_WB, 67 68 /* Capture usecases */ 69 USECASE_AUDIO_RECORD, 70 USECASE_AUDIO_RECORD_LOW_LATENCY, 71 72 USECASE_VOICE_CALL, 73 74 /* Voice extension usecases */ 75 USECASE_VOICE2_CALL, 76 USECASE_VOLTE_CALL, 77 USECASE_QCHAT_CALL, 78 USECASE_VOWLAN_CALL, 79 USECASE_INCALL_REC_UPLINK, 80 USECASE_INCALL_REC_DOWNLINK, 81 USECASE_INCALL_REC_UPLINK_AND_DOWNLINK, 82 83 USECASE_AUDIO_SPKR_CALIB_RX, 84 USECASE_AUDIO_SPKR_CALIB_TX, 85 86 USECASE_AUDIO_PLAYBACK_AFE_PROXY, 87 USECASE_AUDIO_RECORD_AFE_PROXY, 88 USECASE_AUDIO_DSM_FEEDBACK, 89 90 AUDIO_USECASE_MAX 91 }; 92 93 const char * const use_case_table[AUDIO_USECASE_MAX]; 94 95 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 96 97 /* 98 * tinyAlsa library interprets period size as number of frames 99 * one frame = channel_count * sizeof (pcm sample) 100 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes 101 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes 102 * We should take care of returning proper size when AudioFlinger queries for 103 * the buffer size of an input/output stream 104 */ 105 106 enum { 107 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/ 108 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */ 109 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */ 110 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */ 111 }; 112 113 enum { 114 OFFLOAD_STATE_IDLE, 115 OFFLOAD_STATE_PLAYING, 116 OFFLOAD_STATE_PAUSED, 117 }; 118 119 struct offload_cmd { 120 struct listnode node; 121 int cmd; 122 int data[]; 123 }; 124 125 struct stream_out { 126 struct audio_stream_out stream; 127 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 128 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */ 129 pthread_cond_t cond; 130 struct pcm_config config; 131 struct compr_config compr_config; 132 struct pcm *pcm; 133 struct compress *compr; 134 int standby; 135 int pcm_device_id; 136 unsigned int sample_rate; 137 audio_channel_mask_t channel_mask; 138 audio_format_t format; 139 audio_devices_t devices; 140 audio_output_flags_t flags; 141 audio_usecase_t usecase; 142 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */ 143 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1]; 144 bool muted; 145 uint64_t written; /* total frames written, not cleared when entering standby */ 146 audio_io_handle_t handle; 147 148 int non_blocking; 149 int playback_started; 150 int offload_state; 151 pthread_cond_t offload_cond; 152 pthread_t offload_thread; 153 struct listnode offload_cmd_list; 154 bool offload_thread_blocked; 155 156 stream_callback_t offload_callback; 157 void *offload_cookie; 158 struct compr_gapless_mdata gapless_mdata; 159 int send_new_metadata; 160 161 struct audio_device *dev; 162 }; 163 164 struct stream_in { 165 struct audio_stream_in stream; 166 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 167 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */ 168 struct pcm_config config; 169 struct pcm *pcm; 170 int standby; 171 int source; 172 int pcm_device_id; 173 audio_devices_t device; 174 audio_channel_mask_t channel_mask; 175 audio_usecase_t usecase; 176 bool enable_aec; 177 bool enable_ns; 178 179 audio_io_handle_t capture_handle; 180 audio_input_flags_t flags; 181 bool is_st_session; 182 bool is_st_session_active; 183 184 struct audio_device *dev; 185 }; 186 187 typedef enum { 188 PCM_PLAYBACK, 189 PCM_CAPTURE, 190 VOICE_CALL, 191 PCM_HFP_CALL 192 } usecase_type_t; 193 194 union stream_ptr { 195 struct stream_in *in; 196 struct stream_out *out; 197 }; 198 199 struct audio_usecase { 200 struct listnode list; 201 audio_usecase_t id; 202 usecase_type_t type; 203 audio_devices_t devices; 204 snd_device_t out_snd_device; 205 snd_device_t in_snd_device; 206 union stream_ptr stream; 207 }; 208 209 typedef void* (*adm_init_t)(); 210 typedef void (*adm_deinit_t)(void *); 211 typedef void (*adm_register_output_stream_t)(void *, audio_io_handle_t, audio_output_flags_t); 212 typedef void (*adm_register_input_stream_t)(void *, audio_io_handle_t, audio_input_flags_t); 213 typedef void (*adm_deregister_stream_t)(void *, audio_io_handle_t); 214 typedef void (*adm_request_focus_t)(void *, audio_io_handle_t); 215 typedef void (*adm_abandon_focus_t)(void *, audio_io_handle_t); 216 217 struct audio_device { 218 struct audio_hw_device device; 219 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 220 struct mixer *mixer; 221 audio_mode_t mode; 222 struct stream_in *active_input; 223 struct stream_out *primary_output; 224 struct stream_out *voice_tx_output; 225 struct stream_out *current_call_output; 226 bool bluetooth_nrec; 227 bool screen_off; 228 int *snd_dev_ref_cnt; 229 struct listnode usecase_list; 230 struct audio_route *audio_route; 231 int acdb_settings; 232 struct voice voice; 233 unsigned int cur_hdmi_channels; 234 bool bt_wb_speech_enabled; 235 bool mic_muted; 236 bool enable_voicerx; 237 238 int snd_card; 239 void *platform; 240 void *extspk; 241 242 void *visualizer_lib; 243 int (*visualizer_start_output)(audio_io_handle_t, int); 244 int (*visualizer_stop_output)(audio_io_handle_t, int); 245 246 /* The pcm_params use_case_table is loaded by adev_verify_devices() upon 247 * calling adev_open(). 248 * 249 * If an entry is not NULL, it can be used to determine if extended precision 250 * or other capabilities are present for the device corresponding to that usecase. 251 */ 252 struct pcm_params *use_case_table[AUDIO_USECASE_MAX]; 253 void *offload_effects_lib; 254 int (*offload_effects_start_output)(audio_io_handle_t, int); 255 int (*offload_effects_stop_output)(audio_io_handle_t, int); 256 257 void *adm_data; 258 void *adm_lib; 259 adm_init_t adm_init; 260 adm_deinit_t adm_deinit; 261 adm_register_input_stream_t adm_register_input_stream; 262 adm_register_output_stream_t adm_register_output_stream; 263 adm_deregister_stream_t adm_deregister_stream; 264 adm_request_focus_t adm_request_focus; 265 adm_abandon_focus_t adm_abandon_focus; 266 }; 267 268 int select_devices(struct audio_device *adev, 269 audio_usecase_t uc_id); 270 271 int disable_audio_route(struct audio_device *adev, 272 struct audio_usecase *usecase); 273 274 int disable_snd_device(struct audio_device *adev, 275 snd_device_t snd_device); 276 277 int enable_snd_device(struct audio_device *adev, 278 snd_device_t snd_device); 279 280 int enable_audio_route(struct audio_device *adev, 281 struct audio_usecase *usecase); 282 283 struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 284 audio_usecase_t uc_id); 285 286 #define LITERAL_TO_STRING(x) #x 287 #define CHECK(condition) LOG_ALWAYS_FATAL_IF(!(condition), "%s",\ 288 __FILE__ ":" LITERAL_TO_STRING(__LINE__)\ 289 " ASSERT_FATAL(" #condition ") failed.") 290 291 /* 292 * NOTE: when multiple mutexes have to be acquired, always take the 293 * stream_in or stream_out mutex first, followed by the audio_device mutex. 294 */ 295 296 #endif // QCOM_AUDIO_HW_H 297