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     float applied_volume_l;
260     float applied_volume_r;
261 
262     error_log_t *error_log;
263 
264     struct stream_app_type_cfg app_type_cfg;
265 
266     size_t kernel_buffer_size;  // cached value of the alsa buffer size, const after open().
267 
268     // last out_get_presentation_position() cached info.
269     bool         last_fifo_valid;
270     unsigned int last_fifo_frames_remaining;
271     int64_t      last_fifo_time_ns;
272 
273     simple_stats_t fifo_underruns;  // TODO: keep a list of the last N fifo underrun times.
274     simple_stats_t start_latency_ms;
275 };
276 
277 struct stream_in {
278     struct audio_stream_in stream;
279     pthread_mutex_t lock; /* see note below on mutex acquisition order */
280     pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
281     struct pcm_config config;
282     struct pcm *pcm;
283     int standby;
284     int source;
285     int pcm_device_id;
286     audio_devices_t device;
287     audio_channel_mask_t channel_mask;
288     unsigned int sample_rate;
289     audio_usecase_t usecase;
290     bool enable_aec;
291     bool enable_ns;
292     bool enable_ec_port;
293     bool ec_opened;
294     struct listnode aec_list;
295     struct listnode ns_list;
296     int64_t frames_read; /* total frames read, not cleared when entering standby */
297     int64_t frames_muted; /* total frames muted, not cleared when entering standby */
298     int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
299     int     mmap_shared_memory_fd; /* file descriptor associated with MMAP NOIRQ shared memory */
300 
301     audio_io_handle_t capture_handle;
302     audio_input_flags_t flags;
303     bool is_st_session;
304     bool is_st_session_active;
305     bool realtime;
306     int af_period_multiplier;
307     struct audio_device *dev;
308     audio_format_t format;
309     card_status_t card_status;
310     int capture_started;
311     float zoom;
312     audio_microphone_direction_t direction;
313 
314     struct stream_app_type_cfg app_type_cfg;
315 
316     /* Array of supported channel mask configurations.
317        +1 so that the last entry is always 0 */
318     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
319     audio_format_t supported_formats[MAX_SUPPORTED_FORMATS + 1];
320     uint32_t supported_sample_rates[MAX_SUPPORTED_SAMPLE_RATES + 1];
321 
322     error_log_t *error_log;
323 
324     simple_stats_t start_latency_ms;
325 };
326 
327 typedef enum usecase_type_t {
328     PCM_PLAYBACK,
329     PCM_CAPTURE,
330     VOICE_CALL,
331     PCM_HFP_CALL,
332     USECASE_TYPE_MAX
333 } usecase_type_t;
334 
335 union stream_ptr {
336     struct stream_in *in;
337     struct stream_out *out;
338 };
339 
340 struct audio_usecase {
341     struct listnode list;
342     audio_usecase_t id;
343     usecase_type_t  type;
344     audio_devices_t devices;
345     snd_device_t out_snd_device;
346     snd_device_t in_snd_device;
347     union stream_ptr stream;
348 };
349 
350 typedef void* (*adm_init_t)();
351 typedef void (*adm_deinit_t)(void *);
352 typedef void (*adm_register_output_stream_t)(void *, audio_io_handle_t, audio_output_flags_t);
353 typedef void (*adm_register_input_stream_t)(void *, audio_io_handle_t, audio_input_flags_t);
354 typedef void (*adm_deregister_stream_t)(void *, audio_io_handle_t);
355 typedef void (*adm_request_focus_t)(void *, audio_io_handle_t);
356 typedef void (*adm_abandon_focus_t)(void *, audio_io_handle_t);
357 typedef void (*adm_set_config_t)(void *, audio_io_handle_t,
358                                          struct pcm *,
359                                          struct pcm_config *);
360 typedef void (*adm_request_focus_v2_t)(void *, audio_io_handle_t, long);
361 typedef bool (*adm_is_noirq_avail_t)(void *, int, int, int);
362 typedef void (*adm_on_routing_change_t)(void *, audio_io_handle_t);
363 
364 struct audio_device {
365     struct audio_hw_device device;
366 
367     pthread_mutex_t lock; /* see note below on mutex acquisition order */
368     struct mixer *mixer;
369     audio_mode_t mode;
370     struct stream_out *primary_output;
371     struct stream_out *voice_tx_output;
372     struct stream_out *current_call_output;
373     bool bluetooth_nrec;
374     bool screen_off;
375     int *snd_dev_ref_cnt;
376     struct listnode usecase_list;
377     struct audio_route *audio_route;
378     int acdb_settings;
379     struct voice voice;
380     unsigned int cur_hdmi_channels;
381     bool bt_wb_speech_enabled;
382     bool mic_muted;
383     bool enable_voicerx;
384     bool enable_hfp;
385     bool mic_break_enabled;
386     bool use_voice_device_mute;
387 
388     int snd_card;
389     void *platform;
390     void *extspk;
391 
392     card_status_t card_status;
393 
394     void *visualizer_lib;
395     int (*visualizer_start_output)(audio_io_handle_t, int, int, int);
396     int (*visualizer_stop_output)(audio_io_handle_t, int);
397 
398     /* The pcm_params use_case_table is loaded by adev_verify_devices() upon
399      * calling adev_open().
400      *
401      * If an entry is not NULL, it can be used to determine if extended precision
402      * or other capabilities are present for the device corresponding to that usecase.
403      */
404     struct pcm_params *use_case_table[AUDIO_USECASE_MAX];
405     void *offload_effects_lib;
406     int (*offload_effects_start_output)(audio_io_handle_t, int);
407     int (*offload_effects_stop_output)(audio_io_handle_t, int);
408 
409     void *adm_data;
410     void *adm_lib;
411 
412     struct pcm_config haptics_config;
413     struct pcm *haptic_pcm;
414     int    haptic_pcm_device_id;
415     uint8_t *haptic_buffer;
416     size_t haptic_buffer_size;
417 
418     adm_init_t adm_init;
419     adm_deinit_t adm_deinit;
420     adm_register_input_stream_t adm_register_input_stream;
421     adm_register_output_stream_t adm_register_output_stream;
422     adm_deregister_stream_t adm_deregister_stream;
423     adm_request_focus_t adm_request_focus;
424     adm_abandon_focus_t adm_abandon_focus;
425     adm_set_config_t adm_set_config;
426     adm_request_focus_v2_t adm_request_focus_v2;
427     adm_is_noirq_avail_t adm_is_noirq_avail;
428     adm_on_routing_change_t adm_on_routing_change;
429 
430     /* logging */
431     snd_device_t last_logged_snd_device[AUDIO_USECASE_MAX][2]; /* [out, in] */
432     int camera_orientation; /* CAMERA_BACK_LANDSCAPE ... CAMERA_FRONT_PORTRAIT */
433     bool bt_sco_on;
434     bool a2dp_started;
435 };
436 
437 int select_devices(struct audio_device *adev,
438                    audio_usecase_t uc_id);
439 
440 int disable_audio_route(struct audio_device *adev,
441                         struct audio_usecase *usecase);
442 
443 int disable_snd_device(struct audio_device *adev,
444                        snd_device_t snd_device);
445 
446 int enable_snd_device(struct audio_device *adev,
447                       snd_device_t snd_device);
448 
449 int enable_audio_route(struct audio_device *adev,
450                        struct audio_usecase *usecase);
451 
452 struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
453                                             audio_usecase_t uc_id);
454 
455 int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore);
456 
457 #define LITERAL_TO_STRING(x) #x
458 #define CHECK(condition) LOG_ALWAYS_FATAL_IF(!(condition), "%s",\
459             __FILE__ ":" LITERAL_TO_STRING(__LINE__)\
460             " ASSERT_FATAL(" #condition ") failed.")
461 
462 /*
463  * NOTE: when multiple mutexes have to be acquired, always take the
464  * stream_in or stream_out mutex first, followed by the audio_device mutex.
465  */
466 
467 #endif // QCOM_AUDIO_HW_H
468