1 /*
2  * Copyright (C) 2013-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 #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 <audio_utils/ErrorLog.h>
29 #include <audio_utils/PowerLog.h>
30 #include "voice.h"
31 
32 // dlopen() does not go through default library path search if there is a "/" in the library name.
33 #ifdef __LP64__
34 #define VISUALIZER_LIBRARY_PATH "/vendor/lib64/soundfx/libqcomvisualizer.so"
35 #define OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH "/vendor/lib64/soundfx/libqcompostprocbundle.so"
36 #else
37 #define VISUALIZER_LIBRARY_PATH "/vendor/lib/soundfx/libqcomvisualizer.so"
38 #define OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH "/vendor/lib/soundfx/libqcompostprocbundle.so"
39 #endif
40 #define ADM_LIBRARY_PATH "libadm.so"
41 
42 /* Flags used to initialize acdb_settings variable that goes to ACDB library */
43 #define DMIC_FLAG       0x00000002
44 #define TTY_MODE_OFF    0x00000010
45 #define TTY_MODE_FULL   0x00000020
46 #define TTY_MODE_VCO    0x00000040
47 #define TTY_MODE_HCO    0x00000080
48 #define TTY_MODE_CLEAR  0xFFFFFF0F
49 
50 #define ACDB_DEV_TYPE_OUT 1
51 #define ACDB_DEV_TYPE_IN 2
52 
53 #define MAX_SUPPORTED_CHANNEL_MASKS 2
54 #define MAX_SUPPORTED_FORMATS 15
55 #define MAX_SUPPORTED_SAMPLE_RATES 7
56 #define DEFAULT_HDMI_OUT_CHANNELS   2
57 
58 #define ERROR_LOG_ENTRIES 16
59 
60 #define POWER_LOG_LINES 40
61 #define POWER_LOG_SAMPLING_INTERVAL_MS 50
62 #define POWER_LOG_ENTRIES (1 /* minutes */ * 60 /* seconds */ * 1000 /* msec */ \
63                            / POWER_LOG_SAMPLING_INTERVAL_MS)
64 
65 /* Error types for the error log */
66 enum {
67     ERROR_CODE_STANDBY = 1,
68     ERROR_CODE_WRITE,
69 };
70 
71 typedef enum card_status_t {
72     CARD_STATUS_OFFLINE,
73     CARD_STATUS_ONLINE
74 } card_status_t;
75 
76 /* These are the supported use cases by the hardware.
77  * Each usecase is mapped to a specific PCM device.
78  * Refer to pcm_device_table[].
79  */
80 enum {
81     USECASE_INVALID = -1,
82     /* Playback usecases */
83     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
84     USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
85     USECASE_AUDIO_PLAYBACK_MULTI_CH,
86     USECASE_AUDIO_PLAYBACK_OFFLOAD,
87     USECASE_AUDIO_PLAYBACK_TTS,
88     USECASE_AUDIO_PLAYBACK_ULL,
89     USECASE_AUDIO_PLAYBACK_MMAP,
90 
91     /* HFP Use case*/
92     USECASE_AUDIO_HFP_SCO,
93     USECASE_AUDIO_HFP_SCO_WB,
94 
95     /* Capture usecases */
96     USECASE_AUDIO_RECORD,
97     USECASE_AUDIO_RECORD_LOW_LATENCY,
98     USECASE_AUDIO_RECORD_MMAP,
99 
100     /* Voice extension usecases
101      *
102      * Following usecase are specific to voice session names created by
103      * MODEM and APPS on 8992/8994/8084/8974 platforms.
104      */
105     USECASE_VOICE_CALL,  /* Usecase setup for voice session on first subscription for DSDS/DSDA */
106     USECASE_VOICE2_CALL, /* Usecase setup for voice session on second subscription for DSDS/DSDA */
107     USECASE_VOLTE_CALL,  /* Usecase setup for VoLTE session on first subscription */
108     USECASE_QCHAT_CALL,  /* Usecase setup for QCHAT session */
109     USECASE_VOWLAN_CALL, /* Usecase setup for VoWLAN session */
110 
111     /*
112      * Following usecase are specific to voice session names created by
113      * MODEM and APPS on 8996 platforms.
114      */
115 
116     USECASE_VOICEMMODE1_CALL, /* Usecase setup for Voice/VoLTE/VoWLAN sessions on first
117                                * subscription for DSDS/DSDA
118                                */
119     USECASE_VOICEMMODE2_CALL, /* Usecase setup for voice/VoLTE/VoWLAN sessions on second
120                                * subscription for DSDS/DSDA
121                                */
122 
123     USECASE_INCALL_REC_UPLINK,
124     USECASE_INCALL_REC_DOWNLINK,
125     USECASE_INCALL_REC_UPLINK_AND_DOWNLINK,
126 
127     USECASE_AUDIO_SPKR_CALIB_RX,
128     USECASE_AUDIO_SPKR_CALIB_TX,
129 
130     USECASE_AUDIO_PLAYBACK_AFE_PROXY,
131     USECASE_AUDIO_RECORD_AFE_PROXY,
132     USECASE_AUDIO_DSM_FEEDBACK,
133 
134     AUDIO_USECASE_MAX
135 };
136 
137 const char * const use_case_table[AUDIO_USECASE_MAX];
138 
139 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
140 
141 /*
142  * tinyAlsa library interprets period size as number of frames
143  * one frame = channel_count * sizeof (pcm sample)
144  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
145  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
146  * We should take care of returning proper size when AudioFlinger queries for
147  * the buffer size of an input/output stream
148  */
149 
150 enum {
151     OFFLOAD_CMD_EXIT,               /* exit compress offload thread loop*/
152     OFFLOAD_CMD_DRAIN,              /* send a full drain request to DSP */
153     OFFLOAD_CMD_PARTIAL_DRAIN,      /* send a partial drain request to DSP */
154     OFFLOAD_CMD_WAIT_FOR_BUFFER,    /* wait for buffer released by DSP */
155     OFFLOAD_CMD_ERROR,              /* offload playback hit some error */
156 };
157 
158 enum {
159     OFFLOAD_STATE_IDLE,
160     OFFLOAD_STATE_PLAYING,
161     OFFLOAD_STATE_PAUSED,
162 };
163 
164 struct offload_cmd {
165     struct listnode node;
166     int cmd;
167     int data[];
168 };
169 
170 struct stream_out {
171     struct audio_stream_out stream;
172     pthread_mutex_t lock; /* see note below on mutex acquisition order */
173     pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
174     pthread_cond_t  cond;
175     struct pcm_config config;
176     struct compr_config compr_config;
177     struct pcm *pcm;
178     struct compress *compr;
179     int standby;
180     int pcm_device_id;
181     unsigned int sample_rate;
182     audio_channel_mask_t channel_mask;
183     audio_format_t format;
184     audio_devices_t devices;
185     audio_output_flags_t flags;
186     audio_usecase_t usecase;
187     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
188     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
189     bool muted;
190     uint64_t written; /* total frames written, not cleared when entering standby */
191     audio_io_handle_t handle;
192 
193     int non_blocking;
194     int playback_started;
195     int offload_state;
196     pthread_cond_t offload_cond;
197     pthread_t offload_thread;
198     struct listnode offload_cmd_list;
199     bool offload_thread_blocked;
200 
201     stream_callback_t offload_callback;
202     void *offload_cookie;
203     struct compr_gapless_mdata gapless_mdata;
204     int send_new_metadata;
205     bool realtime;
206     int af_period_multiplier;
207     struct audio_device *dev;
208     card_status_t card_status;
209 
210     error_log_t *error_log;
211     power_log_t *power_log;
212 };
213 
214 struct stream_in {
215     struct audio_stream_in stream;
216     pthread_mutex_t lock; /* see note below on mutex acquisition order */
217     pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
218     struct pcm_config config;
219     struct pcm *pcm;
220     int standby;
221     int source;
222     int pcm_device_id;
223     audio_devices_t device;
224     audio_channel_mask_t channel_mask;
225     audio_usecase_t usecase;
226     bool enable_aec;
227     bool enable_ns;
228     int64_t frames_read; /* total frames read, not cleared when entering standby */
229 
230     audio_io_handle_t capture_handle;
231     audio_input_flags_t flags;
232     bool is_st_session;
233     bool is_st_session_active;
234     bool realtime;
235     int af_period_multiplier;
236     struct audio_device *dev;
237     audio_format_t format;
238     card_status_t card_status;
239     int capture_started;
240 };
241 
242 typedef enum usecase_type_t {
243     PCM_PLAYBACK,
244     PCM_CAPTURE,
245     VOICE_CALL,
246     PCM_HFP_CALL
247 } usecase_type_t;
248 
249 union stream_ptr {
250     struct stream_in *in;
251     struct stream_out *out;
252 };
253 
254 struct audio_usecase {
255     struct listnode list;
256     audio_usecase_t id;
257     usecase_type_t  type;
258     audio_devices_t devices;
259     snd_device_t out_snd_device;
260     snd_device_t in_snd_device;
261     union stream_ptr stream;
262 };
263 
264 typedef void* (*adm_init_t)();
265 typedef void (*adm_deinit_t)(void *);
266 typedef void (*adm_register_output_stream_t)(void *, audio_io_handle_t, audio_output_flags_t);
267 typedef void (*adm_register_input_stream_t)(void *, audio_io_handle_t, audio_input_flags_t);
268 typedef void (*adm_deregister_stream_t)(void *, audio_io_handle_t);
269 typedef void (*adm_request_focus_t)(void *, audio_io_handle_t);
270 typedef void (*adm_abandon_focus_t)(void *, audio_io_handle_t);
271 typedef void (*adm_set_config_t)(void *, audio_io_handle_t,
272                                          struct pcm *,
273                                          struct pcm_config *);
274 typedef void (*adm_request_focus_v2_t)(void *, audio_io_handle_t, long);
275 typedef bool (*adm_is_noirq_avail_t)(void *, int, int, int);
276 typedef void (*adm_on_routing_change_t)(void *, audio_io_handle_t);
277 
278 struct audio_device {
279     struct audio_hw_device device;
280     pthread_mutex_t lock; /* see note below on mutex acquisition order */
281     struct mixer *mixer;
282     audio_mode_t mode;
283     struct stream_in *active_input;
284     struct stream_out *primary_output;
285     struct stream_out *voice_tx_output;
286     struct stream_out *current_call_output;
287     bool bluetooth_nrec;
288     bool screen_off;
289     int *snd_dev_ref_cnt;
290     struct listnode usecase_list;
291     struct audio_route *audio_route;
292     int acdb_settings;
293     struct voice voice;
294     unsigned int cur_hdmi_channels;
295     bool bt_wb_speech_enabled;
296     bool mic_muted;
297     bool enable_voicerx;
298     bool enable_hfp;
299 
300     int snd_card;
301     void *platform;
302     void *extspk;
303 
304     card_status_t card_status;
305 
306     void *visualizer_lib;
307     int (*visualizer_start_output)(audio_io_handle_t, int);
308     int (*visualizer_stop_output)(audio_io_handle_t, int);
309 
310     /* The pcm_params use_case_table is loaded by adev_verify_devices() upon
311      * calling adev_open().
312      *
313      * If an entry is not NULL, it can be used to determine if extended precision
314      * or other capabilities are present for the device corresponding to that usecase.
315      */
316     struct pcm_params *use_case_table[AUDIO_USECASE_MAX];
317     void *offload_effects_lib;
318     int (*offload_effects_start_output)(audio_io_handle_t, int);
319     int (*offload_effects_stop_output)(audio_io_handle_t, int);
320 
321     void *adm_data;
322     void *adm_lib;
323     adm_init_t adm_init;
324     adm_deinit_t adm_deinit;
325     adm_register_input_stream_t adm_register_input_stream;
326     adm_register_output_stream_t adm_register_output_stream;
327     adm_deregister_stream_t adm_deregister_stream;
328     adm_request_focus_t adm_request_focus;
329     adm_abandon_focus_t adm_abandon_focus;
330     adm_set_config_t adm_set_config;
331     adm_request_focus_v2_t adm_request_focus_v2;
332     adm_is_noirq_avail_t adm_is_noirq_avail;
333     adm_on_routing_change_t adm_on_routing_change;
334 
335     /* logging */
336     snd_device_t last_logged_snd_device[AUDIO_USECASE_MAX][2]; /* [out, in] */
337 };
338 
339 int select_devices(struct audio_device *adev,
340                    audio_usecase_t uc_id);
341 
342 int disable_audio_route(struct audio_device *adev,
343                         struct audio_usecase *usecase);
344 
345 int disable_snd_device(struct audio_device *adev,
346                        snd_device_t snd_device);
347 
348 int enable_snd_device(struct audio_device *adev,
349                       snd_device_t snd_device);
350 
351 int enable_audio_route(struct audio_device *adev,
352                        struct audio_usecase *usecase);
353 
354 struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
355                                             audio_usecase_t uc_id);
356 
357 #define LITERAL_TO_STRING(x) #x
358 #define CHECK(condition) LOG_ALWAYS_FATAL_IF(!(condition), "%s",\
359             __FILE__ ":" LITERAL_TO_STRING(__LINE__)\
360             " ASSERT_FATAL(" #condition ") failed.")
361 
362 /*
363  * NOTE: when multiple mutexes have to be acquired, always take the
364  * stream_in or stream_out mutex first, followed by the audio_device mutex.
365  */
366 
367 #endif // QCOM_AUDIO_HW_H
368