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