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/Statistics.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 * FCC_8) /* support positional and index masks to 8ch */
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 /* Error types for the error log */
61 enum {
62     ERROR_CODE_STANDBY = 1,
63     ERROR_CODE_WRITE,
64     ERROR_CODE_READ,
65 };
66 
67 typedef enum card_status_t {
68     CARD_STATUS_OFFLINE,
69     CARD_STATUS_ONLINE
70 } card_status_t;
71 
72 /* These are the supported use cases by the hardware.
73  * Each usecase is mapped to a specific PCM device.
74  * Refer to pcm_device_table[].
75  */
76 enum {
77     USECASE_INVALID = -1,
78     /* Playback usecases */
79     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
80     USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
81     USECASE_AUDIO_PLAYBACK_HIFI,
82     USECASE_AUDIO_PLAYBACK_OFFLOAD,
83     USECASE_AUDIO_PLAYBACK_TTS,
84     USECASE_AUDIO_PLAYBACK_ULL,
85     USECASE_AUDIO_PLAYBACK_MMAP,
86     USECASE_AUDIO_PLAYBACK_WITH_HAPTICS,
87 
88     /* HFP Use case*/
89     USECASE_AUDIO_HFP_SCO,
90     USECASE_AUDIO_HFP_SCO_WB,
91 
92     /* Capture usecases */
93     USECASE_AUDIO_RECORD,
94     USECASE_AUDIO_RECORD_LOW_LATENCY,
95     USECASE_AUDIO_RECORD_MMAP,
96     USECASE_AUDIO_RECORD_HIFI,
97 
98     /* Voice extension usecases
99      *
100      * Following usecase are specific to voice session names created by
101      * MODEM and APPS on 8992/8994/8084/8974 platforms.
102      */
103     USECASE_VOICE_CALL,  /* Usecase setup for voice session on first subscription for DSDS/DSDA */
104     USECASE_VOICE2_CALL, /* Usecase setup for voice session on second subscription for DSDS/DSDA */
105     USECASE_VOLTE_CALL,  /* Usecase setup for VoLTE session on first subscription */
106     USECASE_QCHAT_CALL,  /* Usecase setup for QCHAT session */
107     USECASE_VOWLAN_CALL, /* Usecase setup for VoWLAN session */
108 
109     /*
110      * Following usecase are specific to voice session names created by
111      * MODEM and APPS on 8996 platforms.
112      */
113 
114     USECASE_VOICEMMODE1_CALL, /* Usecase setup for Voice/VoLTE/VoWLAN sessions on first
115                                * subscription for DSDS/DSDA
116                                */
117     USECASE_VOICEMMODE2_CALL, /* Usecase setup for voice/VoLTE/VoWLAN sessions on second
118                                * subscription for DSDS/DSDA
119                                */
120 
121     USECASE_INCALL_REC_UPLINK,
122     USECASE_INCALL_REC_DOWNLINK,
123     USECASE_INCALL_REC_UPLINK_AND_DOWNLINK,
124 
125     USECASE_AUDIO_SPKR_CALIB_RX,
126     USECASE_AUDIO_SPKR_CALIB_TX,
127 
128     USECASE_AUDIO_PLAYBACK_AFE_PROXY,
129     USECASE_AUDIO_RECORD_AFE_PROXY,
130     USECASE_AUDIO_DSM_FEEDBACK,
131 
132     /* VOIP usecase*/
133     USECASE_AUDIO_PLAYBACK_VOIP,
134     USECASE_AUDIO_RECORD_VOIP,
135 
136     USECASE_INCALL_MUSIC_UPLINK,
137     USECASE_INCALL_MUSIC_UPLINK2,
138 
139     USECASE_AUDIO_A2DP_ABR_FEEDBACK,
140 
141     AUDIO_USECASE_MAX
142 };
143 
144 const char * const use_case_table[AUDIO_USECASE_MAX];
145 
146 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
147 
148 /*
149  * tinyAlsa library interprets period size as number of frames
150  * one frame = channel_count * sizeof (pcm sample)
151  * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
152  * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
153  * We should take care of returning proper size when AudioFlinger queries for
154  * the buffer size of an input/output stream
155  */
156 
157 enum {
158     OFFLOAD_CMD_EXIT,               /* exit compress offload thread loop*/
159     OFFLOAD_CMD_DRAIN,              /* send a full drain request to DSP */
160     OFFLOAD_CMD_PARTIAL_DRAIN,      /* send a partial drain request to DSP */
161     OFFLOAD_CMD_WAIT_FOR_BUFFER,    /* wait for buffer released by DSP */
162     OFFLOAD_CMD_ERROR,              /* offload playback hit some error */
163 };
164 
165 /*
166  * Camera selection indicated via set_parameters "cameraFacing=front|back and
167  * "rotation=0|90|180|270""
168  */
169 enum {
170   CAMERA_FACING_BACK = 0x0,
171   CAMERA_FACING_FRONT = 0x1,
172   CAMERA_FACING_MASK = 0x0F,
173   CAMERA_ROTATION_LANDSCAPE = 0x0,
174   CAMERA_ROTATION_INVERT_LANDSCAPE = 0x10,
175   CAMERA_ROTATION_PORTRAIT = 0x20,
176   CAMERA_ROTATION_MASK = 0xF0,
177   CAMERA_BACK_LANDSCAPE = (CAMERA_FACING_BACK|CAMERA_ROTATION_LANDSCAPE),
178   CAMERA_BACK_INVERT_LANDSCAPE = (CAMERA_FACING_BACK|CAMERA_ROTATION_INVERT_LANDSCAPE),
179   CAMERA_BACK_PORTRAIT = (CAMERA_FACING_BACK|CAMERA_ROTATION_PORTRAIT),
180   CAMERA_FRONT_LANDSCAPE = (CAMERA_FACING_FRONT|CAMERA_ROTATION_LANDSCAPE),
181   CAMERA_FRONT_INVERT_LANDSCAPE = (CAMERA_FACING_FRONT|CAMERA_ROTATION_INVERT_LANDSCAPE),
182   CAMERA_FRONT_PORTRAIT = (CAMERA_FACING_FRONT|CAMERA_ROTATION_PORTRAIT),
183 
184   CAMERA_DEFAULT = CAMERA_BACK_LANDSCAPE,
185 };
186 
187 //FIXME: to be replaced by proper video capture properties API
188 #define AUDIO_PARAMETER_KEY_CAMERA_FACING "cameraFacing"
189 #define AUDIO_PARAMETER_VALUE_FRONT "front"
190 #define AUDIO_PARAMETER_VALUE_BACK "back"
191 
192 enum {
193     OFFLOAD_STATE_IDLE,
194     OFFLOAD_STATE_PLAYING,
195     OFFLOAD_STATE_PAUSED,
196 };
197 
198 struct offload_cmd {
199     struct listnode node;
200     int cmd;
201     int data[];
202 };
203 
204 struct stream_app_type_cfg {
205     int sample_rate;
206     uint32_t bit_width; // unused
207     const char *mode;
208     int app_type;
209     int gain[2];
210 };
211 
212 struct stream_out {
213     struct audio_stream_out stream;
214     pthread_mutex_t lock; /* see note below on mutex acquisition order */
215     pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
216     pthread_mutex_t compr_mute_lock; /* acquire before setting compress volume */
217     pthread_cond_t  cond;
218     struct pcm_config config;
219     struct compr_config compr_config;
220     struct pcm *pcm;
221     struct compress *compr;
222     int standby;
223     int pcm_device_id;
224     unsigned int sample_rate;
225     audio_channel_mask_t channel_mask;
226     audio_format_t format;
227     audio_devices_t devices;
228     audio_output_flags_t flags;
229     audio_usecase_t usecase;
230     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
231     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
232     audio_format_t supported_formats[MAX_SUPPORTED_FORMATS + 1];
233     uint32_t supported_sample_rates[MAX_SUPPORTED_SAMPLE_RATES + 1];
234     bool muted;
235     uint64_t written; /* total frames written, not cleared when entering standby */
236     int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
237     int     mmap_shared_memory_fd; /* file descriptor associated with MMAP NOIRQ shared memory */
238     audio_io_handle_t handle;
239 
240     int non_blocking;
241     int playback_started;
242     int offload_state;
243     pthread_cond_t offload_cond;
244     pthread_t offload_thread;
245     struct listnode offload_cmd_list;
246     bool offload_thread_blocked;
247 
248     stream_callback_t offload_callback;
249     void *offload_cookie;
250     struct compr_gapless_mdata gapless_mdata;
251     int send_new_metadata;
252     bool realtime;
253     int af_period_multiplier;
254     struct audio_device *dev;
255     card_status_t card_status;
256     bool a2dp_compress_mute;
257     float volume_l;
258     float volume_r;
259 
260     error_log_t *error_log;
261 
262     struct stream_app_type_cfg app_type_cfg;
263 
264     size_t kernel_buffer_size;  // cached value of the alsa buffer size, const after open().
265 
266     // last out_get_presentation_position() cached info.
267     bool         last_fifo_valid;
268     unsigned int last_fifo_frames_remaining;
269     int64_t      last_fifo_time_ns;
270 
271     simple_stats_t fifo_underruns;  // TODO: keep a list of the last N fifo underrun times.
272     simple_stats_t start_latency_ms;
273 };
274 
275 struct stream_in {
276     struct audio_stream_in stream;
277     pthread_mutex_t lock; /* see note below on mutex acquisition order */
278     pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
279     struct pcm_config config;
280     struct pcm *pcm;
281     int standby;
282     int source;
283     int pcm_device_id;
284     audio_devices_t device;
285     audio_channel_mask_t channel_mask;
286     unsigned int sample_rate;
287     audio_usecase_t usecase;
288     bool enable_aec;
289     bool enable_ns;
290     bool enable_ec_port;
291     bool ec_opened;
292     struct listnode aec_list;
293     struct listnode ns_list;
294     int64_t frames_read; /* total frames read, not cleared when entering standby */
295     int64_t frames_muted; /* total frames muted, not cleared when entering standby */
296     int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
297     int     mmap_shared_memory_fd; /* file descriptor associated with MMAP NOIRQ shared memory */
298 
299     audio_io_handle_t capture_handle;
300     audio_input_flags_t flags;
301     bool is_st_session;
302     bool is_st_session_active;
303     bool realtime;
304     int af_period_multiplier;
305     struct audio_device *dev;
306     audio_format_t format;
307     card_status_t card_status;
308     int capture_started;
309     float zoom;
310     audio_microphone_direction_t direction;
311 
312     struct stream_app_type_cfg app_type_cfg;
313 
314     /* Array of supported channel mask configurations.
315        +1 so that the last entry is always 0 */
316     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
317     audio_format_t supported_formats[MAX_SUPPORTED_FORMATS + 1];
318     uint32_t supported_sample_rates[MAX_SUPPORTED_SAMPLE_RATES + 1];
319 
320     error_log_t *error_log;
321 
322     simple_stats_t start_latency_ms;
323 };
324 
325 typedef enum usecase_type_t {
326     PCM_PLAYBACK,
327     PCM_CAPTURE,
328     VOICE_CALL,
329     PCM_HFP_CALL,
330     USECASE_TYPE_MAX
331 } usecase_type_t;
332 
333 union stream_ptr {
334     struct stream_in *in;
335     struct stream_out *out;
336 };
337 
338 struct audio_usecase {
339     struct listnode list;
340     audio_usecase_t id;
341     usecase_type_t  type;
342     audio_devices_t devices;
343     snd_device_t out_snd_device;
344     snd_device_t in_snd_device;
345     union stream_ptr stream;
346 };
347 
348 typedef void* (*adm_init_t)();
349 typedef void (*adm_deinit_t)(void *);
350 typedef void (*adm_register_output_stream_t)(void *, audio_io_handle_t, audio_output_flags_t);
351 typedef void (*adm_register_input_stream_t)(void *, audio_io_handle_t, audio_input_flags_t);
352 typedef void (*adm_deregister_stream_t)(void *, audio_io_handle_t);
353 typedef void (*adm_request_focus_t)(void *, audio_io_handle_t);
354 typedef void (*adm_abandon_focus_t)(void *, audio_io_handle_t);
355 typedef void (*adm_set_config_t)(void *, audio_io_handle_t,
356                                          struct pcm *,
357                                          struct pcm_config *);
358 typedef void (*adm_request_focus_v2_t)(void *, audio_io_handle_t, long);
359 typedef bool (*adm_is_noirq_avail_t)(void *, int, int, int);
360 typedef void (*adm_on_routing_change_t)(void *, audio_io_handle_t);
361 
362 struct audio_device {
363     struct audio_hw_device device;
364 
365     pthread_mutex_t lock; /* see note below on mutex acquisition order */
366     struct mixer *mixer;
367     audio_mode_t mode;
368     struct stream_out *primary_output;
369     struct stream_out *voice_tx_output;
370     struct stream_out *current_call_output;
371     bool bluetooth_nrec;
372     bool screen_off;
373     int *snd_dev_ref_cnt;
374     struct listnode usecase_list;
375     struct audio_route *audio_route;
376     int acdb_settings;
377     struct voice voice;
378     unsigned int cur_hdmi_channels;
379     bool bt_wb_speech_enabled;
380     bool mic_muted;
381     bool enable_voicerx;
382     bool enable_hfp;
383     bool mic_break_enabled;
384     bool use_voice_device_mute;
385 
386     int snd_card;
387     void *platform;
388     void *extspk;
389 
390     card_status_t card_status;
391 
392     void *visualizer_lib;
393     int (*visualizer_start_output)(audio_io_handle_t, int, int, int);
394     int (*visualizer_stop_output)(audio_io_handle_t, int);
395 
396     /* The pcm_params use_case_table is loaded by adev_verify_devices() upon
397      * calling adev_open().
398      *
399      * If an entry is not NULL, it can be used to determine if extended precision
400      * or other capabilities are present for the device corresponding to that usecase.
401      */
402     struct pcm_params *use_case_table[AUDIO_USECASE_MAX];
403     void *offload_effects_lib;
404     int (*offload_effects_start_output)(audio_io_handle_t, int);
405     int (*offload_effects_stop_output)(audio_io_handle_t, int);
406 
407     void *adm_data;
408     void *adm_lib;
409 
410     struct pcm_config haptics_config;
411     struct pcm *haptic_pcm;
412     int    haptic_pcm_device_id;
413     uint8_t *haptic_buffer;
414     size_t haptic_buffer_size;
415 
416     adm_init_t adm_init;
417     adm_deinit_t adm_deinit;
418     adm_register_input_stream_t adm_register_input_stream;
419     adm_register_output_stream_t adm_register_output_stream;
420     adm_deregister_stream_t adm_deregister_stream;
421     adm_request_focus_t adm_request_focus;
422     adm_abandon_focus_t adm_abandon_focus;
423     adm_set_config_t adm_set_config;
424     adm_request_focus_v2_t adm_request_focus_v2;
425     adm_is_noirq_avail_t adm_is_noirq_avail;
426     adm_on_routing_change_t adm_on_routing_change;
427 
428     /* logging */
429     snd_device_t last_logged_snd_device[AUDIO_USECASE_MAX][2]; /* [out, in] */
430     int camera_orientation; /* CAMERA_BACK_LANDSCAPE ... CAMERA_FRONT_PORTRAIT */
431     bool bt_sco_on;
432 };
433 
434 int select_devices(struct audio_device *adev,
435                    audio_usecase_t uc_id);
436 
437 int disable_audio_route(struct audio_device *adev,
438                         struct audio_usecase *usecase);
439 
440 int disable_snd_device(struct audio_device *adev,
441                        snd_device_t snd_device);
442 
443 int enable_snd_device(struct audio_device *adev,
444                       snd_device_t snd_device);
445 
446 int enable_audio_route(struct audio_device *adev,
447                        struct audio_usecase *usecase);
448 
449 struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
450                                             audio_usecase_t uc_id);
451 
452 int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore);
453 
454 #define LITERAL_TO_STRING(x) #x
455 #define CHECK(condition) LOG_ALWAYS_FATAL_IF(!(condition), "%s",\
456             __FILE__ ":" LITERAL_TO_STRING(__LINE__)\
457             " ASSERT_FATAL(" #condition ") failed.")
458 
459 /*
460  * NOTE: when multiple mutexes have to be acquired, always take the
461  * stream_in or stream_out mutex first, followed by the audio_device mutex.
462  */
463 
464 #endif // QCOM_AUDIO_HW_H
465