• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #define LOG_TAG "audio_hw_primary"
18 /*#define LOG_NDEBUG 0*/
19 
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <sys/time.h>
24 #include <stdlib.h>
25 
26 #include <cutils/log.h>
27 #include <cutils/str_parms.h>
28 #include <cutils/properties.h>
29 
30 #include <hardware/hardware.h>
31 #include <system/audio.h>
32 #include <hardware/audio.h>
33 
34 #include <tinyalsa/asoundlib.h>
35 #include <audio_utils/resampler.h>
36 #include <audio_utils/echo_reference.h>
37 #include <hardware/audio_effect.h>
38 #include <audio_effects/effect_aec.h>
39 
40 #include "ril_interface.h"
41 
42 /* Mixer control names */
43 #define MIXER_DL2_LEFT_EQUALIZER            "DL2 Left Equalizer"
44 #define MIXER_DL2_RIGHT_EQUALIZER           "DL2 Right Equalizer"
45 #define MIXER_DL1_MEDIA_PLAYBACK_VOLUME     "DL1 Media Playback Volume"
46 #define MIXER_DL1_VOICE_PLAYBACK_VOLUME     "DL1 Voice Playback Volume"
47 #define MIXER_DL2_MEDIA_PLAYBACK_VOLUME     "DL2 Media Playback Volume"
48 #define MIXER_DL2_VOICE_PLAYBACK_VOLUME     "DL2 Voice Playback Volume"
49 #define MIXER_SDT_DL_VOLUME                 "SDT DL Volume"
50 #define MIXER_SDT_UL_VOLUME                 "SDT UL Volume"
51 
52 #define MIXER_HEADSET_PLAYBACK_VOLUME       "Headset Playback Volume"
53 #define MIXER_HANDSFREE_PLAYBACK_VOLUME     "Handsfree Playback Volume"
54 #define MIXER_EARPHONE_PLAYBACK_VOLUME      "Earphone Playback Volume"
55 #define MIXER_BT_UL_VOLUME                  "BT UL Volume"
56 
57 #define MIXER_DL1_EQUALIZER                 "DL1 Equalizer"
58 #define MIXER_DL1_MIXER_MULTIMEDIA          "DL1 Mixer Multimedia"
59 #define MIXER_DL1_MIXER_VOICE               "DL1 Mixer Voice"
60 #define MIXER_DL2_MIXER_MULTIMEDIA          "DL2 Mixer Multimedia"
61 #define MIXER_DL2_MIXER_VOICE               "DL2 Mixer Voice"
62 #define MIXER_SIDETONE_MIXER_PLAYBACK       "Sidetone Mixer Playback"
63 #define MIXER_SIDETONE_MIXER_CAPTURE        "Sidetone Mixer Capture"
64 #define MIXER_DL2_MONO_MIXER                "DL2 Mono Mixer"
65 #define MIXER_DL1_PDM_SWITCH                "DL1 PDM Switch"
66 #define MIXER_DL1_BT_VX_SWITCH              "DL1 BT_VX Switch"
67 #define MIXER_VOICE_CAPTURE_MIXER_CAPTURE   "Voice Capture Mixer Capture"
68 
69 #define MIXER_HS_LEFT_PLAYBACK              "HS Left Playback"
70 #define MIXER_HS_RIGHT_PLAYBACK             "HS Right Playback"
71 #define MIXER_HF_LEFT_PLAYBACK              "HF Left Playback"
72 #define MIXER_HF_RIGHT_PLAYBACK             "HF Right Playback"
73 #define MIXER_EARPHONE_ENABLE_SWITCH        "Earphone Enable Switch"
74 
75 #define MIXER_ANALOG_LEFT_CAPTURE_ROUTE     "Analog Left Capture Route"
76 #define MIXER_ANALOG_RIGHT_CAPTURE_ROUTE    "Analog Right Capture Route"
77 #define MIXER_CAPTURE_PREAMPLIFIER_VOLUME   "Capture Preamplifier Volume"
78 #define MIXER_CAPTURE_VOLUME                "Capture Volume"
79 #define MIXER_AMIC_UL_VOLUME                "AMIC UL Volume"
80 #define MIXER_AUDUL_VOICE_UL_VOLUME         "AUDUL Voice UL Volume"
81 #define MIXER_MUX_VX0                       "MUX_VX0"
82 #define MIXER_MUX_VX1                       "MUX_VX1"
83 #define MIXER_MUX_UL10                      "MUX_UL10"
84 #define MIXER_MUX_UL11                      "MUX_UL11"
85 
86 /* Mixer control gain and route values */
87 #define MIXER_ABE_GAIN_0DB                  120
88 #define MIXER_PLAYBACK_HS_DAC               "HS DAC"
89 #define MIXER_PLAYBACK_HF_DAC               "HF DAC"
90 #define MIXER_MAIN_MIC                      "Main Mic"
91 #define MIXER_SUB_MIC                       "Sub Mic"
92 #define MIXER_HS_MIC                        "Headset Mic"
93 #define MIXER_AMIC0                         "AMic0"
94 #define MIXER_AMIC1                         "AMic1"
95 #define MIXER_BT_LEFT                       "BT Left"
96 #define MIXER_BT_RIGHT                      "BT Right"
97 #define MIXER_450HZ_HIGH_PASS               "450Hz High-pass"
98 #define MIXER_FLAT_RESPONSE                 "Flat response"
99 #define MIXER_4KHZ_LPF_0DB                  "4Khz LPF   0dB"
100 
101 
102 /* ALSA cards for OMAP4 */
103 #define CARD_OMAP4_ABE 0
104 #define CARD_OMAP4_HDMI 1
105 #define CARD_TUNA_DEFAULT CARD_OMAP4_ABE
106 
107 /* ALSA ports for OMAP4 */
108 #define PORT_MM 0
109 #define PORT_MM2_UL 1
110 #define PORT_VX 2
111 #define PORT_TONES 3
112 #define PORT_VIBRA 4
113 #define PORT_MODEM 5
114 #define PORT_MM_LP 6
115 #define PORT_SPDIF 9
116 #define PORT_HDMI 0
117 
118 /* constraint imposed by ABE: all period sizes must be multiples of 24 */
119 #define ABE_BASE_FRAME_COUNT 24
120 /* number of base blocks in a short period (low latency) */
121 #define SHORT_PERIOD_MULTIPLIER 44  /* 22 ms */
122 /* number of frames per short period (low latency) */
123 #define SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * SHORT_PERIOD_MULTIPLIER)
124 /* number of short periods in a long period (low power) */
125 #define LONG_PERIOD_MULTIPLIER 14  /* 308 ms */
126 /* number of frames per long period (low power) */
127 #define LONG_PERIOD_SIZE (SHORT_PERIOD_SIZE * LONG_PERIOD_MULTIPLIER)
128 /* number of periods for low power playback */
129 #define PLAYBACK_LONG_PERIOD_COUNT 2
130 /* number of pseudo periods for low latency playback */
131 #define PLAYBACK_SHORT_PERIOD_COUNT 4
132 /* number of periods for capture */
133 #define CAPTURE_PERIOD_COUNT 2
134 /* minimum sleep time in out_write() when write threshold is not reached */
135 #define MIN_WRITE_SLEEP_US 5000
136 
137 #define RESAMPLER_BUFFER_FRAMES (SHORT_PERIOD_SIZE * 2)
138 #define RESAMPLER_BUFFER_SIZE (4 * RESAMPLER_BUFFER_FRAMES)
139 
140 #define DEFAULT_OUT_SAMPLING_RATE 44100
141 
142 /* sampling rate when using MM low power port */
143 #define MM_LOW_POWER_SAMPLING_RATE 44100
144 /* sampling rate when using MM full power port */
145 #define MM_FULL_POWER_SAMPLING_RATE 48000
146 /* sampling rate when using VX port for narrow band */
147 #define VX_NB_SAMPLING_RATE 8000
148 /* sampling rate when using VX port for wide band */
149 #define VX_WB_SAMPLING_RATE 16000
150 
151 /* conversions from dB to ABE and codec gains */
152 #define DB_TO_ABE_GAIN(x) ((x) + MIXER_ABE_GAIN_0DB)
153 #define DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(x) (((x) + 6) / 6)
154 #define DB_TO_CAPTURE_VOLUME(x) (((x) - 6) / 6)
155 #define DB_TO_HEADSET_VOLUME(x) (((x) + 30) / 2)
156 #define DB_TO_SPEAKER_VOLUME(x) (((x) + 52) / 2)
157 #define DB_TO_EARPIECE_VOLUME(x) (((x) + 24) / 2)
158 
159 /* conversions from codec and ABE gains to dB */
160 #define DB_FROM_SPEAKER_VOLUME(x) ((x) * 2 - 52)
161 
162 /* use-case specific mic volumes, all in dB */
163 #define CAPTURE_MAIN_MIC_VOLUME 16
164 #define CAPTURE_SUB_MIC_VOLUME 18
165 #define CAPTURE_HEADSET_MIC_VOLUME 12
166 
167 #define VOICE_RECOGNITION_MAIN_MIC_VOLUME 5
168 #define VOICE_RECOGNITION_SUB_MIC_VOLUME 18
169 #define VOICE_RECOGNITION_HEADSET_MIC_VOLUME 14
170 
171 #define CAMCORDER_MAIN_MIC_VOLUME 13
172 #define CAMCORDER_SUB_MIC_VOLUME 10
173 #define CAMCORDER_HEADSET_MIC_VOLUME 12
174 
175 #define VOIP_MAIN_MIC_VOLUME 13
176 #define VOIP_SUB_MIC_VOLUME 20
177 #define VOIP_HEADSET_MIC_VOLUME 12
178 
179 #define VOICE_CALL_MAIN_MIC_VOLUME 0
180 #define VOICE_CALL_SUB_MIC_VOLUME_MAGURO -4
181 #define VOICE_CALL_SUB_MIC_VOLUME_TORO -2
182 #define VOICE_CALL_HEADSET_MIC_VOLUME 8
183 
184 /* use-case specific output volumes */
185 #define NORMAL_SPEAKER_VOLUME_TORO 4
186 #define NORMAL_SPEAKER_VOLUME_MAGURO 2
187 #define NORMAL_HEADSET_VOLUME_TORO -12
188 #define NORMAL_HEADSET_VOLUME_MAGURO -12
189 #define NORMAL_HEADPHONE_VOLUME_TORO -6 /* allow louder output for headphones */
190 #define NORMAL_HEADPHONE_VOLUME_MAGURO -6
191 #define NORMAL_EARPIECE_VOLUME_TORO -2
192 #define NORMAL_EARPIECE_VOLUME_MAGURO -2
193 
194 #define VOICE_CALL_SPEAKER_VOLUME_TORO 9
195 #define VOICE_CALL_SPEAKER_VOLUME_MAGURO 6
196 #define VOICE_CALL_HEADSET_VOLUME_TORO -6
197 #define VOICE_CALL_HEADSET_VOLUME_MAGURO 0
198 #define VOICE_CALL_EARPIECE_VOLUME_TORO 2
199 #define VOICE_CALL_EARPIECE_VOLUME_MAGURO 6
200 
201 #define VOIP_SPEAKER_VOLUME_TORO 9
202 #define VOIP_SPEAKER_VOLUME_MAGURO 7
203 #define VOIP_HEADSET_VOLUME_TORO -6
204 #define VOIP_HEADSET_VOLUME_MAGURO -6
205 #define VOIP_EARPIECE_VOLUME_TORO 6
206 #define VOIP_EARPIECE_VOLUME_MAGURO 6
207 
208 #define HEADPHONE_VOLUME_TTY -2
209 #define RINGTONE_HEADSET_VOLUME_OFFSET -14
210 
211 /* product-specific defines */
212 #define PRODUCT_DEVICE_PROPERTY "ro.product.device"
213 #define PRODUCT_NAME_PROPERTY   "ro.product.name"
214 #define PRODUCT_DEVICE_TORO     "toro"
215 #define PRODUCT_NAME_YAKJU      "yakju"
216 
217 enum tty_modes {
218     TTY_MODE_OFF,
219     TTY_MODE_VCO,
220     TTY_MODE_HCO,
221     TTY_MODE_FULL
222 };
223 
224 struct pcm_config pcm_config_mm = {
225     .channels = 2,
226     .rate = MM_FULL_POWER_SAMPLING_RATE,
227     .period_size = LONG_PERIOD_SIZE,
228     .period_count = PLAYBACK_LONG_PERIOD_COUNT,
229     .format = PCM_FORMAT_S16_LE,
230 };
231 
232 struct pcm_config pcm_config_mm_ul = {
233     .channels = 2,
234     .rate = MM_FULL_POWER_SAMPLING_RATE,
235     .period_size = SHORT_PERIOD_SIZE,
236     .period_count = CAPTURE_PERIOD_COUNT,
237     .format = PCM_FORMAT_S16_LE,
238 };
239 
240 struct pcm_config pcm_config_vx = {
241     .channels = 2,
242     .rate = VX_NB_SAMPLING_RATE,
243     .period_size = 160,
244     .period_count = 2,
245     .format = PCM_FORMAT_S16_LE,
246 };
247 
248 #define MIN(x, y) ((x) > (y) ? (y) : (x))
249 
250 struct route_setting
251 {
252     char *ctl_name;
253     int intval;
254     char *strval;
255 };
256 
257 /* These are values that never change */
258 struct route_setting defaults[] = {
259     /* general */
260     {
261         .ctl_name = MIXER_DL2_LEFT_EQUALIZER,
262         .strval = MIXER_450HZ_HIGH_PASS,
263     },
264     {
265         .ctl_name = MIXER_DL2_RIGHT_EQUALIZER,
266         .strval = MIXER_450HZ_HIGH_PASS,
267     },
268     {
269         .ctl_name = MIXER_DL1_MEDIA_PLAYBACK_VOLUME,
270         .intval = MIXER_ABE_GAIN_0DB,
271     },
272     {
273         .ctl_name = MIXER_DL2_MEDIA_PLAYBACK_VOLUME,
274         .intval = MIXER_ABE_GAIN_0DB,
275     },
276     {
277         .ctl_name = MIXER_DL1_VOICE_PLAYBACK_VOLUME,
278         .intval = MIXER_ABE_GAIN_0DB,
279     },
280     {
281         .ctl_name = MIXER_DL2_VOICE_PLAYBACK_VOLUME,
282         .intval = MIXER_ABE_GAIN_0DB,
283     },
284     {
285         .ctl_name = MIXER_SDT_DL_VOLUME,
286         .intval = MIXER_ABE_GAIN_0DB,
287     },
288     {
289         .ctl_name = MIXER_AUDUL_VOICE_UL_VOLUME,
290         .intval = MIXER_ABE_GAIN_0DB,
291     },
292     {
293         .ctl_name = MIXER_CAPTURE_PREAMPLIFIER_VOLUME,
294         .intval = DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(0),
295     },
296     {
297         .ctl_name = MIXER_CAPTURE_VOLUME,
298         .intval = DB_TO_CAPTURE_VOLUME(30),
299     },
300     {
301         .ctl_name = MIXER_SDT_UL_VOLUME,
302         .intval = MIXER_ABE_GAIN_0DB - 17,
303     },
304     {
305         .ctl_name = MIXER_SIDETONE_MIXER_CAPTURE,
306         .intval = 0,
307     },
308 
309     /* headset */
310     {
311         .ctl_name = MIXER_SIDETONE_MIXER_PLAYBACK,
312         .intval = 1,
313     },
314     {
315         .ctl_name = MIXER_DL1_PDM_SWITCH,
316         .intval = 1,
317     },
318 
319     /* bt */
320     {
321         .ctl_name = MIXER_BT_UL_VOLUME,
322         .intval = MIXER_ABE_GAIN_0DB,
323     },
324     {
325         .ctl_name = NULL,
326     },
327 };
328 
329 struct route_setting hf_output[] = {
330     {
331         .ctl_name = MIXER_HF_LEFT_PLAYBACK,
332         .strval = MIXER_PLAYBACK_HF_DAC,
333     },
334     {
335         .ctl_name = MIXER_HF_RIGHT_PLAYBACK,
336         .strval = MIXER_PLAYBACK_HF_DAC,
337     },
338     {
339         .ctl_name = NULL,
340     },
341 };
342 
343 struct route_setting hs_output[] = {
344     {
345         .ctl_name = MIXER_HS_LEFT_PLAYBACK,
346         .strval = MIXER_PLAYBACK_HS_DAC,
347     },
348     {
349         .ctl_name = MIXER_HS_RIGHT_PLAYBACK,
350         .strval = MIXER_PLAYBACK_HS_DAC,
351     },
352     {
353         .ctl_name = NULL,
354     },
355 };
356 
357 /* MM UL front-end paths */
358 struct route_setting mm_ul2_bt[] = {
359     {
360         .ctl_name = MIXER_MUX_UL10,
361         .strval = MIXER_BT_LEFT,
362     },
363     {
364         .ctl_name = MIXER_MUX_UL11,
365         .strval = MIXER_BT_LEFT,
366     },
367     {
368         .ctl_name = NULL,
369     },
370 };
371 
372 struct route_setting mm_ul2_amic_left[] = {
373     {
374         .ctl_name = MIXER_MUX_UL10,
375         .strval = MIXER_AMIC0,
376     },
377     {
378         .ctl_name = MIXER_MUX_UL11,
379         .strval = MIXER_AMIC0,
380     },
381     {
382         .ctl_name = NULL,
383     },
384 };
385 
386 struct route_setting mm_ul2_amic_right[] = {
387     {
388         .ctl_name = MIXER_MUX_UL10,
389         .strval = MIXER_AMIC1,
390     },
391     {
392         .ctl_name = MIXER_MUX_UL11,
393         .strval = MIXER_AMIC1,
394     },
395     {
396         .ctl_name = NULL,
397     },
398 };
399 
400 /* VX UL front-end paths */
401 struct route_setting vx_ul_amic_left[] = {
402     {
403         .ctl_name = MIXER_MUX_VX0,
404         .strval = MIXER_AMIC0,
405     },
406     {
407         .ctl_name = MIXER_MUX_VX1,
408         .strval = MIXER_AMIC0,
409     },
410     {
411         .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
412         .intval = 1,
413     },
414     {
415         .ctl_name = NULL,
416     },
417 };
418 
419 struct route_setting vx_ul_amic_right[] = {
420     {
421         .ctl_name = MIXER_MUX_VX0,
422         .strval = MIXER_AMIC1,
423     },
424     {
425         .ctl_name = MIXER_MUX_VX1,
426         .strval = MIXER_AMIC1,
427     },
428     {
429         .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
430         .intval = 1,
431     },
432     {
433         .ctl_name = NULL,
434     },
435 };
436 
437 struct route_setting vx_ul_bt[] = {
438     {
439         .ctl_name = MIXER_MUX_VX0,
440         .strval = MIXER_BT_LEFT,
441     },
442     {
443         .ctl_name = MIXER_MUX_VX1,
444         .strval = MIXER_BT_LEFT,
445     },
446     {
447         .ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
448         .intval = 1,
449     },
450     {
451         .ctl_name = NULL,
452     },
453 };
454 
455 struct mixer_ctls
456 {
457     struct mixer_ctl *dl1_eq;
458     struct mixer_ctl *mm_dl2_volume;
459     struct mixer_ctl *vx_dl2_volume;
460     struct mixer_ctl *mm_dl1;
461     struct mixer_ctl *mm_dl2;
462     struct mixer_ctl *vx_dl1;
463     struct mixer_ctl *vx_dl2;
464     struct mixer_ctl *earpiece_enable;
465     struct mixer_ctl *dl2_mono;
466     struct mixer_ctl *dl1_headset;
467     struct mixer_ctl *dl1_bt;
468     struct mixer_ctl *left_capture;
469     struct mixer_ctl *right_capture;
470     struct mixer_ctl *amic_ul_volume;
471     struct mixer_ctl *voice_ul_volume;
472     struct mixer_ctl *sidetone_capture;
473     struct mixer_ctl *headset_volume;
474     struct mixer_ctl *speaker_volume;
475     struct mixer_ctl *earpiece_volume;
476 };
477 
478 struct tuna_audio_device {
479     struct audio_hw_device hw_device;
480 
481     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
482     struct mixer *mixer;
483     struct mixer_ctls mixer_ctls;
484     int mode;
485     int devices;
486     struct pcm *pcm_modem_dl;
487     struct pcm *pcm_modem_ul;
488     int in_call;
489     float voice_volume;
490     struct tuna_stream_in *active_input;
491     struct tuna_stream_out *active_output;
492     bool mic_mute;
493     int tty_mode;
494     struct echo_reference_itfe *echo_reference;
495     bool bluetooth_nrec;
496     bool device_is_toro;
497     int wb_amr;
498     bool low_power;
499 
500     /* RIL */
501     struct ril_handle ril;
502 };
503 
504 struct tuna_stream_out {
505     struct audio_stream_out stream;
506 
507     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
508     struct pcm_config config;
509     struct pcm *pcm;
510     struct resampler_itfe *resampler;
511     char *buffer;
512     int standby;
513     struct echo_reference_itfe *echo_reference;
514     struct tuna_audio_device *dev;
515     int write_threshold;
516     bool low_power;
517 };
518 
519 #define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */
520 
521 struct tuna_stream_in {
522     struct audio_stream_in stream;
523 
524     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
525     struct pcm_config config;
526     struct pcm *pcm;
527     int device;
528     struct resampler_itfe *resampler;
529     struct resampler_buffer_provider buf_provider;
530     int16_t *buffer;
531     size_t frames_in;
532     unsigned int requested_rate;
533     int standby;
534     int source;
535     struct echo_reference_itfe *echo_reference;
536     bool need_echo_reference;
537     effect_handle_t preprocessors[MAX_PREPROCESSORS];
538     int num_preprocessors;
539     int16_t *proc_buf;
540     size_t proc_buf_size;
541     size_t proc_frames_in;
542     int16_t *ref_buf;
543     size_t ref_buf_size;
544     size_t ref_frames_in;
545     int read_status;
546 
547     struct tuna_audio_device *dev;
548 };
549 
550 /**
551  * NOTE: when multiple mutexes have to be acquired, always respect the following order:
552  *        hw device > in stream > out stream
553  */
554 
555 
556 static void select_output_device(struct tuna_audio_device *adev);
557 static void select_input_device(struct tuna_audio_device *adev);
558 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume);
559 static int do_input_standby(struct tuna_stream_in *in);
560 static int do_output_standby(struct tuna_stream_out *out);
561 
562 /* Returns true on devices that are toro, false otherwise */
is_device_toro(void)563 static int is_device_toro(void)
564 {
565     char property[PROPERTY_VALUE_MAX];
566 
567     property_get(PRODUCT_DEVICE_PROPERTY, property, PRODUCT_DEVICE_TORO);
568 
569     /* return true if the property matches the given value */
570     return strcmp(property, PRODUCT_DEVICE_TORO) == 0;
571 }
572 
573 /* The enable flag when 0 makes the assumption that enums are disabled by
574  * "Off" and integers/booleans by 0 */
set_route_by_array(struct mixer * mixer,struct route_setting * route,int enable)575 static int set_route_by_array(struct mixer *mixer, struct route_setting *route,
576                               int enable)
577 {
578     struct mixer_ctl *ctl;
579     unsigned int i, j;
580 
581     /* Go through the route array and set each value */
582     i = 0;
583     while (route[i].ctl_name) {
584         ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name);
585         if (!ctl)
586             return -EINVAL;
587 
588         if (route[i].strval) {
589             if (enable)
590                 mixer_ctl_set_enum_by_string(ctl, route[i].strval);
591             else
592                 mixer_ctl_set_enum_by_string(ctl, "Off");
593         } else {
594             /* This ensures multiple (i.e. stereo) values are set jointly */
595             for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) {
596                 if (enable)
597                     mixer_ctl_set_value(ctl, j, route[i].intval);
598                 else
599                     mixer_ctl_set_value(ctl, j, 0);
600             }
601         }
602         i++;
603     }
604 
605     return 0;
606 }
607 
start_call(struct tuna_audio_device * adev)608 static int start_call(struct tuna_audio_device *adev)
609 {
610     LOGE("Opening modem PCMs");
611 
612     pcm_config_vx.rate = adev->wb_amr ? VX_WB_SAMPLING_RATE : VX_NB_SAMPLING_RATE;
613 
614     /* Open modem PCM channels */
615     if (adev->pcm_modem_dl == NULL) {
616         adev->pcm_modem_dl = pcm_open(0, PORT_MODEM, PCM_OUT, &pcm_config_vx);
617         if (!pcm_is_ready(adev->pcm_modem_dl)) {
618             LOGE("cannot open PCM modem DL stream: %s", pcm_get_error(adev->pcm_modem_dl));
619             goto err_open_dl;
620         }
621     }
622 
623     if (adev->pcm_modem_ul == NULL) {
624         adev->pcm_modem_ul = pcm_open(0, PORT_MODEM, PCM_IN, &pcm_config_vx);
625         if (!pcm_is_ready(adev->pcm_modem_ul)) {
626             LOGE("cannot open PCM modem UL stream: %s", pcm_get_error(adev->pcm_modem_ul));
627             goto err_open_ul;
628         }
629     }
630 
631     pcm_start(adev->pcm_modem_dl);
632     pcm_start(adev->pcm_modem_ul);
633 
634     return 0;
635 
636 err_open_ul:
637     pcm_close(adev->pcm_modem_ul);
638     adev->pcm_modem_ul = NULL;
639 err_open_dl:
640     pcm_close(adev->pcm_modem_dl);
641     adev->pcm_modem_dl = NULL;
642 
643     return -ENOMEM;
644 }
645 
end_call(struct tuna_audio_device * adev)646 static void end_call(struct tuna_audio_device *adev)
647 {
648     LOGE("Closing modem PCMs");
649 
650     pcm_stop(adev->pcm_modem_dl);
651     pcm_stop(adev->pcm_modem_ul);
652     pcm_close(adev->pcm_modem_dl);
653     pcm_close(adev->pcm_modem_ul);
654     adev->pcm_modem_dl = NULL;
655     adev->pcm_modem_ul = NULL;
656 }
657 
set_eq_filter(struct tuna_audio_device * adev)658 static void set_eq_filter(struct tuna_audio_device *adev)
659 {
660     /* DL1_EQ can't be used for bt */
661     int dl1_eq_applicable = adev->devices & (AUDIO_DEVICE_OUT_WIRED_HEADSET |
662                     AUDIO_DEVICE_OUT_WIRED_HEADPHONE | AUDIO_DEVICE_OUT_EARPIECE);
663 
664     /* 4Khz LPF is used only in NB-AMR voicecall */
665     if ((adev->mode == AUDIO_MODE_IN_CALL) && dl1_eq_applicable &&
666             (adev->tty_mode == TTY_MODE_OFF) && !adev->wb_amr)
667         mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_4KHZ_LPF_0DB);
668     else
669         mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_FLAT_RESPONSE);
670 }
671 
audio_set_wb_amr_callback(void * data,int enable)672 void audio_set_wb_amr_callback(void *data, int enable)
673 {
674     struct tuna_audio_device *adev = (struct tuna_audio_device *)data;
675 
676     pthread_mutex_lock(&adev->lock);
677     if (adev->wb_amr != enable) {
678         adev->wb_amr = enable;
679 
680         /* reopen the modem PCMs at the new rate */
681         if (adev->in_call) {
682             end_call(adev);
683             set_eq_filter(adev);
684             start_call(adev);
685         }
686     }
687     pthread_mutex_unlock(&adev->lock);
688 }
689 
set_incall_device(struct tuna_audio_device * adev)690 static void set_incall_device(struct tuna_audio_device *adev)
691 {
692     int device_type;
693 
694     switch(adev->devices & AUDIO_DEVICE_OUT_ALL) {
695         case AUDIO_DEVICE_OUT_EARPIECE:
696             device_type = SOUND_AUDIO_PATH_HANDSET;
697             break;
698         case AUDIO_DEVICE_OUT_SPEAKER:
699         case AUDIO_DEVICE_OUT_AUX_DIGITAL:
700             device_type = SOUND_AUDIO_PATH_SPEAKER;
701             break;
702         case AUDIO_DEVICE_OUT_WIRED_HEADSET:
703             device_type = SOUND_AUDIO_PATH_HEADSET;
704             break;
705         case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
706             device_type = SOUND_AUDIO_PATH_HEADPHONE;
707             break;
708         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
709         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
710         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
711             if (adev->bluetooth_nrec)
712                 device_type = SOUND_AUDIO_PATH_BLUETOOTH;
713             else
714                 device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR;
715             break;
716         default:
717             device_type = SOUND_AUDIO_PATH_HANDSET;
718             break;
719     }
720 
721     /* if output device isn't supported, open modem side to handset by default */
722     ril_set_call_audio_path(&adev->ril, device_type);
723 }
724 
set_input_volumes(struct tuna_audio_device * adev,int main_mic_on,int headset_mic_on,int sub_mic_on)725 static void set_input_volumes(struct tuna_audio_device *adev, int main_mic_on,
726                               int headset_mic_on, int sub_mic_on)
727 {
728     unsigned int channel;
729     int volume = MIXER_ABE_GAIN_0DB;
730 
731     if (adev->mode == AUDIO_MODE_IN_CALL) {
732         int sub_mic_volume = is_device_toro() ? VOICE_CALL_SUB_MIC_VOLUME_TORO :
733 	                                        VOICE_CALL_SUB_MIC_VOLUME_MAGURO;
734         /* special case: don't look at input source for IN_CALL state */
735         volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_CALL_MAIN_MIC_VOLUME :
736                 (headset_mic_on ? VOICE_CALL_HEADSET_MIC_VOLUME :
737                 (sub_mic_on ? sub_mic_volume : 0)));
738     } else if (adev->active_input) {
739         /* determine input volume by use case */
740         switch (adev->active_input->source) {
741         case AUDIO_SOURCE_MIC: /* general capture */
742             volume = DB_TO_ABE_GAIN(main_mic_on ? CAPTURE_MAIN_MIC_VOLUME :
743                     (headset_mic_on ? CAPTURE_HEADSET_MIC_VOLUME :
744                     (sub_mic_on ? CAPTURE_SUB_MIC_VOLUME : 0)));
745             break;
746 
747         case AUDIO_SOURCE_CAMCORDER:
748             volume = DB_TO_ABE_GAIN(main_mic_on ? CAMCORDER_MAIN_MIC_VOLUME :
749                     (headset_mic_on ? CAMCORDER_HEADSET_MIC_VOLUME :
750                     (sub_mic_on ? CAMCORDER_SUB_MIC_VOLUME : 0)));
751             break;
752 
753         case AUDIO_SOURCE_VOICE_RECOGNITION:
754             volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_RECOGNITION_MAIN_MIC_VOLUME :
755                     (headset_mic_on ? VOICE_RECOGNITION_HEADSET_MIC_VOLUME :
756                     (sub_mic_on ? VOICE_RECOGNITION_SUB_MIC_VOLUME : 0)));
757             break;
758 
759         case AUDIO_SOURCE_VOICE_COMMUNICATION: /* VoIP */
760             volume = DB_TO_ABE_GAIN(main_mic_on ? VOIP_MAIN_MIC_VOLUME :
761                     (headset_mic_on ? VOIP_HEADSET_MIC_VOLUME :
762                     (sub_mic_on ? VOIP_SUB_MIC_VOLUME : 0)));
763             break;
764 
765         default:
766             /* nothing to do */
767             break;
768         }
769     }
770 
771     for (channel = 0; channel < 2; channel++)
772         mixer_ctl_set_value(adev->mixer_ctls.amic_ul_volume, channel, volume);
773 }
774 
set_output_volumes(struct tuna_audio_device * adev,bool tty_volume)775 static void set_output_volumes(struct tuna_audio_device *adev, bool tty_volume)
776 {
777     unsigned int channel;
778     int speaker_volume;
779     int headset_volume;
780     int earpiece_volume;
781     bool toro = adev->device_is_toro;
782     int headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
783     int speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER;
784     int speaker_volume_overrange = MIXER_ABE_GAIN_0DB;
785     int speaker_max_db =
786         DB_FROM_SPEAKER_VOLUME(mixer_ctl_get_range_max(adev->mixer_ctls.speaker_volume));
787     struct mixer_ctl *mixer_ctl_overrange = adev->mixer_ctls.mm_dl2_volume;
788 
789     if (adev->mode == AUDIO_MODE_IN_CALL) {
790         /* Voice call */
791         speaker_volume = toro ? VOICE_CALL_SPEAKER_VOLUME_TORO :
792                                 VOICE_CALL_SPEAKER_VOLUME_MAGURO;
793         headset_volume = toro ? VOICE_CALL_HEADSET_VOLUME_TORO :
794                                 VOICE_CALL_HEADSET_VOLUME_MAGURO;
795         earpiece_volume = toro ? VOICE_CALL_EARPIECE_VOLUME_TORO :
796                                  VOICE_CALL_EARPIECE_VOLUME_MAGURO;
797         mixer_ctl_overrange = adev->mixer_ctls.vx_dl2_volume;
798     } else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
799         /* VoIP */
800         speaker_volume = toro ? VOIP_SPEAKER_VOLUME_TORO :
801                                 VOIP_SPEAKER_VOLUME_MAGURO;
802         headset_volume = toro ? VOIP_HEADSET_VOLUME_TORO :
803                                 VOIP_HEADSET_VOLUME_MAGURO;
804         earpiece_volume = toro ? VOIP_EARPIECE_VOLUME_TORO :
805                                  VOIP_EARPIECE_VOLUME_MAGURO;
806     } else {
807         /* Media */
808         speaker_volume = toro ? NORMAL_SPEAKER_VOLUME_TORO :
809                                 NORMAL_SPEAKER_VOLUME_MAGURO;
810         if (headphone_on)
811             headset_volume = toro ? NORMAL_HEADPHONE_VOLUME_TORO :
812                                     NORMAL_HEADPHONE_VOLUME_MAGURO;
813         else
814             headset_volume = toro ? NORMAL_HEADSET_VOLUME_TORO :
815                                     NORMAL_HEADSET_VOLUME_MAGURO;
816         earpiece_volume = toro ? NORMAL_EARPIECE_VOLUME_TORO :
817                                  NORMAL_EARPIECE_VOLUME_MAGURO;
818     }
819     if (tty_volume)
820         headset_volume = HEADPHONE_VOLUME_TTY;
821     else if (adev->mode == AUDIO_MODE_RINGTONE)
822         headset_volume += RINGTONE_HEADSET_VOLUME_OFFSET;
823 
824     /* If we have run out of range in the codec (analog) speaker volume,
825        we have to apply the remainder of the dB increase to the DL2
826        media/voice mixer volume, which is a digital gain */
827     if (speaker_volume > speaker_max_db) {
828         speaker_volume_overrange += (speaker_volume - speaker_max_db);
829         speaker_volume = speaker_max_db;
830     }
831 
832     for (channel = 0; channel < 2; channel++) {
833         mixer_ctl_set_value(adev->mixer_ctls.speaker_volume, channel,
834             DB_TO_SPEAKER_VOLUME(speaker_volume));
835         mixer_ctl_set_value(adev->mixer_ctls.headset_volume, channel,
836             DB_TO_HEADSET_VOLUME(headset_volume));
837     }
838     if (speaker_on)
839         mixer_ctl_set_value(mixer_ctl_overrange, 0, speaker_volume_overrange);
840     else
841         mixer_ctl_set_value(mixer_ctl_overrange, 0, MIXER_ABE_GAIN_0DB);
842     mixer_ctl_set_value(adev->mixer_ctls.earpiece_volume, 0,
843         DB_TO_EARPIECE_VOLUME(earpiece_volume));
844 }
845 
force_all_standby(struct tuna_audio_device * adev)846 static void force_all_standby(struct tuna_audio_device *adev)
847 {
848     struct tuna_stream_in *in;
849     struct tuna_stream_out *out;
850 
851     if (adev->active_output) {
852         out = adev->active_output;
853         pthread_mutex_lock(&out->lock);
854         do_output_standby(out);
855         pthread_mutex_unlock(&out->lock);
856     }
857 
858     if (adev->active_input) {
859         in = adev->active_input;
860         pthread_mutex_lock(&in->lock);
861         do_input_standby(in);
862         pthread_mutex_unlock(&in->lock);
863     }
864 }
865 
select_mode(struct tuna_audio_device * adev)866 static void select_mode(struct tuna_audio_device *adev)
867 {
868     if (adev->mode == AUDIO_MODE_IN_CALL) {
869         LOGE("Entering IN_CALL state, in_call=%d", adev->in_call);
870         if (!adev->in_call) {
871             force_all_standby(adev);
872             /* force earpiece route for in call state if speaker is the
873             only currently selected route. This prevents having to tear
874             down the modem PCMs to change route from speaker to earpiece
875             after the ringtone is played, but doesn't cause a route
876             change if a headset or bt device is already connected. If
877             speaker is not the only thing active, just remove it from
878             the route. We'll assume it'll never be used initally during
879             a call. This works because we're sure that the audio policy
880             manager will update the output device after the audio mode
881             change, even if the device selection did not change. */
882             if ((adev->devices & AUDIO_DEVICE_OUT_ALL) == AUDIO_DEVICE_OUT_SPEAKER)
883                 adev->devices = AUDIO_DEVICE_OUT_EARPIECE |
884                                 AUDIO_DEVICE_IN_BUILTIN_MIC;
885             else
886                 adev->devices &= ~AUDIO_DEVICE_OUT_SPEAKER;
887             select_output_device(adev);
888             start_call(adev);
889             ril_set_call_clock_sync(&adev->ril, SOUND_CLOCK_START);
890             adev_set_voice_volume(&adev->hw_device, adev->voice_volume);
891             adev->in_call = 1;
892         }
893     } else {
894         LOGE("Leaving IN_CALL state, in_call=%d, mode=%d",
895              adev->in_call, adev->mode);
896         if (adev->in_call) {
897             adev->in_call = 0;
898             end_call(adev);
899             force_all_standby(adev);
900             select_output_device(adev);
901             select_input_device(adev);
902         }
903     }
904 }
905 
select_output_device(struct tuna_audio_device * adev)906 static void select_output_device(struct tuna_audio_device *adev)
907 {
908     int headset_on;
909     int headphone_on;
910     int speaker_on;
911     int earpiece_on;
912     int bt_on;
913     int dl1_on;
914     int sidetone_capture_on = 0;
915     bool tty_volume = false;
916     unsigned int channel;
917 
918     /* Mute VX_UL to avoid pop noises in the tx path
919      * during call before switch changes.
920      */
921     if (adev->mode == AUDIO_MODE_IN_CALL) {
922         for (channel = 0; channel < 2; channel++)
923             mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume,
924                                 channel, 0);
925     }
926 
927     headset_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
928     headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
929     speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER;
930     earpiece_on = adev->devices & AUDIO_DEVICE_OUT_EARPIECE;
931     bt_on = adev->devices & AUDIO_DEVICE_OUT_ALL_SCO;
932 
933     /* force rx path according to TTY mode when in call */
934     if (adev->mode == AUDIO_MODE_IN_CALL && !bt_on) {
935         switch(adev->tty_mode) {
936             case TTY_MODE_FULL:
937             case TTY_MODE_VCO:
938                 /* rx path to headphones */
939                 headphone_on = 1;
940                 headset_on = 0;
941                 speaker_on = 0;
942                 earpiece_on = 0;
943                 tty_volume = true;
944                 break;
945             case TTY_MODE_HCO:
946                 /* rx path to device speaker */
947                 headphone_on = 0;
948                 headset_on = 0;
949                 speaker_on = 1;
950                 earpiece_on = 0;
951                 break;
952             case TTY_MODE_OFF:
953             default:
954                 /* force speaker on when in call and HDMI is selected as voice DL audio
955                  * cannot be routed to HDMI by ABE */
956                 if (adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
957                     speaker_on = 1;
958                 break;
959         }
960     }
961 
962     dl1_on = headset_on | headphone_on | earpiece_on | bt_on;
963 
964     /* Select front end */
965     mixer_ctl_set_value(adev->mixer_ctls.mm_dl2, 0, speaker_on);
966     mixer_ctl_set_value(adev->mixer_ctls.vx_dl2, 0,
967                         speaker_on && (adev->mode == AUDIO_MODE_IN_CALL));
968     mixer_ctl_set_value(adev->mixer_ctls.mm_dl1, 0, dl1_on);
969     mixer_ctl_set_value(adev->mixer_ctls.vx_dl1, 0,
970                         dl1_on && (adev->mode == AUDIO_MODE_IN_CALL));
971     /* Select back end */
972     mixer_ctl_set_value(adev->mixer_ctls.dl1_headset, 0,
973                         headset_on | headphone_on | earpiece_on);
974     mixer_ctl_set_value(adev->mixer_ctls.dl1_bt, 0, bt_on);
975     mixer_ctl_set_value(adev->mixer_ctls.dl2_mono, 0,
976                         (adev->mode != AUDIO_MODE_IN_CALL) && speaker_on);
977     mixer_ctl_set_value(adev->mixer_ctls.earpiece_enable, 0, earpiece_on);
978 
979     /* select output stage */
980     set_route_by_array(adev->mixer, hs_output, headset_on | headphone_on);
981     set_route_by_array(adev->mixer, hf_output, speaker_on);
982 
983     set_eq_filter(adev);
984     set_output_volumes(adev, tty_volume);
985 
986     /* Special case: select input path if in a call, otherwise
987        in_set_parameters is used to update the input route
988        todo: use sub mic for handsfree case */
989     if (adev->mode == AUDIO_MODE_IN_CALL) {
990         if (bt_on)
991             set_route_by_array(adev->mixer, vx_ul_bt, bt_on);
992         else {
993             /* force tx path according to TTY mode when in call */
994             switch(adev->tty_mode) {
995                 case TTY_MODE_FULL:
996                 case TTY_MODE_HCO:
997                     /* tx path from headset mic */
998                     headphone_on = 0;
999                     headset_on = 1;
1000                     speaker_on = 0;
1001                     earpiece_on = 0;
1002                     break;
1003                 case TTY_MODE_VCO:
1004                     /* tx path from device sub mic */
1005                     headphone_on = 0;
1006                     headset_on = 0;
1007                     speaker_on = 1;
1008                     earpiece_on = 0;
1009                     break;
1010                 case TTY_MODE_OFF:
1011                 default:
1012                     break;
1013             }
1014 
1015             if (headset_on || headphone_on || earpiece_on)
1016                 set_route_by_array(adev->mixer, vx_ul_amic_left, 1);
1017             else if (speaker_on)
1018                 set_route_by_array(adev->mixer, vx_ul_amic_right, 1);
1019             else
1020                 set_route_by_array(adev->mixer, vx_ul_amic_left, 0);
1021 
1022             mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture,
1023                                         (earpiece_on || headphone_on) ? MIXER_MAIN_MIC :
1024                                         (headset_on ? MIXER_HS_MIC : "Off"));
1025             mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture,
1026                                          speaker_on ? MIXER_SUB_MIC : "Off");
1027 
1028             set_input_volumes(adev, earpiece_on || headphone_on,
1029                               headset_on, speaker_on);
1030 
1031             /* enable sidetone mixer capture if needed */
1032             sidetone_capture_on = earpiece_on && adev->device_is_toro;
1033         }
1034 
1035         set_incall_device(adev);
1036 
1037         /* Unmute VX_UL after the switch */
1038         for (channel = 0; channel < 2; channel++) {
1039             mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume,
1040                                 channel, MIXER_ABE_GAIN_0DB);
1041         }
1042     }
1043 
1044     mixer_ctl_set_value(adev->mixer_ctls.sidetone_capture, 0, sidetone_capture_on);
1045 }
1046 
select_input_device(struct tuna_audio_device * adev)1047 static void select_input_device(struct tuna_audio_device *adev)
1048 {
1049     int headset_on = 0;
1050     int main_mic_on = 0;
1051     int sub_mic_on = 0;
1052     int bt_on = adev->devices & AUDIO_DEVICE_IN_ALL_SCO;
1053 
1054     if (!bt_on) {
1055         if ((adev->mode != AUDIO_MODE_IN_CALL) && (adev->active_input != 0)) {
1056             /* sub mic is used for camcorder or VoIP on speaker phone */
1057             sub_mic_on = (adev->active_input->source == AUDIO_SOURCE_CAMCORDER) ||
1058                          ((adev->devices & AUDIO_DEVICE_OUT_SPEAKER) &&
1059                           (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION));
1060         }
1061         if (!sub_mic_on) {
1062             headset_on = adev->devices & AUDIO_DEVICE_IN_WIRED_HEADSET;
1063             main_mic_on = adev->devices & AUDIO_DEVICE_IN_BUILTIN_MIC;
1064         }
1065     }
1066 
1067    /* TODO: check how capture is possible during voice calls or if
1068     * both use cases are mutually exclusive.
1069     */
1070     if (bt_on)
1071         set_route_by_array(adev->mixer, mm_ul2_bt, 1);
1072     else {
1073         /* Select front end */
1074         if (main_mic_on || headset_on)
1075             set_route_by_array(adev->mixer, mm_ul2_amic_left, 1);
1076         else if (sub_mic_on)
1077             set_route_by_array(adev->mixer, mm_ul2_amic_right, 1);
1078         else
1079             set_route_by_array(adev->mixer, mm_ul2_amic_left, 0);
1080 
1081         /* Select back end */
1082         mixer_ctl_set_enum_by_string(adev->mixer_ctls.right_capture,
1083                                      sub_mic_on ? MIXER_SUB_MIC : "Off");
1084         mixer_ctl_set_enum_by_string(adev->mixer_ctls.left_capture,
1085                                      main_mic_on ? MIXER_MAIN_MIC :
1086                                      (headset_on ? MIXER_HS_MIC : "Off"));
1087     }
1088 
1089     set_input_volumes(adev, main_mic_on, headset_on, sub_mic_on);
1090 }
1091 
1092 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct tuna_stream_out * out)1093 static int start_output_stream(struct tuna_stream_out *out)
1094 {
1095     struct tuna_audio_device *adev = out->dev;
1096     unsigned int card = CARD_TUNA_DEFAULT;
1097     unsigned int port = PORT_MM;
1098 
1099     adev->active_output = out;
1100 
1101     if (adev->mode != AUDIO_MODE_IN_CALL) {
1102         /* FIXME: only works if only one output can be active at a time */
1103         select_output_device(adev);
1104     }
1105     /* S/PDIF takes priority over HDMI audio. In the case of multiple
1106      * devices, this will cause use of S/PDIF or HDMI only */
1107     out->config.rate = MM_FULL_POWER_SAMPLING_RATE;
1108     if (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)
1109         port = PORT_SPDIF;
1110     else if(adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1111         card = CARD_OMAP4_HDMI;
1112         port = PORT_HDMI;
1113         out->config.rate = MM_LOW_POWER_SAMPLING_RATE;
1114     }
1115     /* default to low power: will be corrected in out_write if necessary before first write to
1116      * tinyalsa.
1117      */
1118     out->write_threshold = PLAYBACK_LONG_PERIOD_COUNT * LONG_PERIOD_SIZE;
1119     out->config.start_threshold = SHORT_PERIOD_SIZE * 2;
1120     out->config.avail_min = LONG_PERIOD_SIZE;
1121     out->low_power = 1;
1122 
1123     out->pcm = pcm_open(card, port, PCM_OUT | PCM_MMAP | PCM_NOIRQ, &out->config);
1124 
1125     if (!pcm_is_ready(out->pcm)) {
1126         LOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
1127         pcm_close(out->pcm);
1128         adev->active_output = NULL;
1129         return -ENOMEM;
1130     }
1131 
1132     if (adev->echo_reference != NULL)
1133         out->echo_reference = adev->echo_reference;
1134 
1135     out->resampler->reset(out->resampler);
1136 
1137     return 0;
1138 }
1139 
check_input_parameters(uint32_t sample_rate,int format,int channel_count)1140 static int check_input_parameters(uint32_t sample_rate, int format, int channel_count)
1141 {
1142     if (format != AUDIO_FORMAT_PCM_16_BIT)
1143         return -EINVAL;
1144 
1145     if ((channel_count < 1) || (channel_count > 2))
1146         return -EINVAL;
1147 
1148     switch(sample_rate) {
1149     case 8000:
1150     case 11025:
1151     case 16000:
1152     case 22050:
1153     case 24000:
1154     case 32000:
1155     case 44100:
1156     case 48000:
1157         break;
1158     default:
1159         return -EINVAL;
1160     }
1161 
1162     return 0;
1163 }
1164 
get_input_buffer_size(uint32_t sample_rate,int format,int channel_count)1165 static size_t get_input_buffer_size(uint32_t sample_rate, int format, int channel_count)
1166 {
1167     size_t size;
1168     size_t device_rate;
1169 
1170     if (check_input_parameters(sample_rate, format, channel_count) != 0)
1171         return 0;
1172 
1173     /* take resampling into account and return the closest majoring
1174     multiple of 16 frames, as audioflinger expects audio buffers to
1175     be a multiple of 16 frames */
1176     size = (pcm_config_mm_ul.period_size * sample_rate) / pcm_config_mm_ul.rate;
1177     size = ((size + 15) / 16) * 16;
1178 
1179     return size * channel_count * sizeof(short);
1180 }
1181 
add_echo_reference(struct tuna_stream_out * out,struct echo_reference_itfe * reference)1182 static void add_echo_reference(struct tuna_stream_out *out,
1183                                struct echo_reference_itfe *reference)
1184 {
1185     pthread_mutex_lock(&out->lock);
1186     out->echo_reference = reference;
1187     pthread_mutex_unlock(&out->lock);
1188 }
1189 
remove_echo_reference(struct tuna_stream_out * out,struct echo_reference_itfe * reference)1190 static void remove_echo_reference(struct tuna_stream_out *out,
1191                                   struct echo_reference_itfe *reference)
1192 {
1193     pthread_mutex_lock(&out->lock);
1194     if (out->echo_reference == reference) {
1195         /* stop writing to echo reference */
1196         reference->write(reference, NULL);
1197         out->echo_reference = NULL;
1198     }
1199     pthread_mutex_unlock(&out->lock);
1200 }
1201 
put_echo_reference(struct tuna_audio_device * adev,struct echo_reference_itfe * reference)1202 static void put_echo_reference(struct tuna_audio_device *adev,
1203                           struct echo_reference_itfe *reference)
1204 {
1205     if (adev->echo_reference != NULL &&
1206             reference == adev->echo_reference) {
1207         if (adev->active_output != NULL)
1208             remove_echo_reference(adev->active_output, reference);
1209         release_echo_reference(reference);
1210         adev->echo_reference = NULL;
1211     }
1212 }
1213 
get_echo_reference(struct tuna_audio_device * adev,audio_format_t format,uint32_t channel_count,uint32_t sampling_rate)1214 static struct echo_reference_itfe *get_echo_reference(struct tuna_audio_device *adev,
1215                                                audio_format_t format,
1216                                                uint32_t channel_count,
1217                                                uint32_t sampling_rate)
1218 {
1219     put_echo_reference(adev, adev->echo_reference);
1220     if (adev->active_output != NULL) {
1221         struct audio_stream *stream = &adev->active_output->stream.common;
1222         uint32_t wr_channel_count = popcount(stream->get_channels(stream));
1223         uint32_t wr_sampling_rate = stream->get_sample_rate(stream);
1224 
1225         int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT,
1226                                            channel_count,
1227                                            sampling_rate,
1228                                            AUDIO_FORMAT_PCM_16_BIT,
1229                                            wr_channel_count,
1230                                            wr_sampling_rate,
1231                                            &adev->echo_reference);
1232         if (status == 0)
1233             add_echo_reference(adev->active_output, adev->echo_reference);
1234     }
1235     return adev->echo_reference;
1236 }
1237 
get_playback_delay(struct tuna_stream_out * out,size_t frames,struct echo_reference_buffer * buffer)1238 static int get_playback_delay(struct tuna_stream_out *out,
1239                        size_t frames,
1240                        struct echo_reference_buffer *buffer)
1241 {
1242     size_t kernel_frames;
1243     int status;
1244 
1245     status = pcm_get_htimestamp(out->pcm, &kernel_frames, &buffer->time_stamp);
1246     if (status < 0) {
1247         buffer->time_stamp.tv_sec  = 0;
1248         buffer->time_stamp.tv_nsec = 0;
1249         buffer->delay_ns           = 0;
1250         LOGV("get_playback_delay(): pcm_get_htimestamp error,"
1251                 "setting playbackTimestamp to 0");
1252         return status;
1253     }
1254 
1255     kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames;
1256 
1257     /* adjust render time stamp with delay added by current driver buffer.
1258      * Add the duration of current frame as we want the render time of the last
1259      * sample being written. */
1260     buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/
1261                             MM_FULL_POWER_SAMPLING_RATE);
1262 
1263     return 0;
1264 }
1265 
out_get_sample_rate(const struct audio_stream * stream)1266 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1267 {
1268     return DEFAULT_OUT_SAMPLING_RATE;
1269 }
1270 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)1271 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1272 {
1273     return 0;
1274 }
1275 
out_get_buffer_size(const struct audio_stream * stream)1276 static size_t out_get_buffer_size(const struct audio_stream *stream)
1277 {
1278     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
1279 
1280     /* take resampling into account and return the closest majoring
1281     multiple of 16 frames, as audioflinger expects audio buffers to
1282     be a multiple of 16 frames */
1283     size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / out->config.rate;
1284     size = ((size + 15) / 16) * 16;
1285     return size * audio_stream_frame_size((struct audio_stream *)stream);
1286 }
1287 
out_get_channels(const struct audio_stream * stream)1288 static uint32_t out_get_channels(const struct audio_stream *stream)
1289 {
1290     return AUDIO_CHANNEL_OUT_STEREO;
1291 }
1292 
out_get_format(const struct audio_stream * stream)1293 static int out_get_format(const struct audio_stream *stream)
1294 {
1295     return AUDIO_FORMAT_PCM_16_BIT;
1296 }
1297 
out_set_format(struct audio_stream * stream,int format)1298 static int out_set_format(struct audio_stream *stream, int format)
1299 {
1300     return 0;
1301 }
1302 
1303 /* must be called with hw device and output stream mutexes locked */
do_output_standby(struct tuna_stream_out * out)1304 static int do_output_standby(struct tuna_stream_out *out)
1305 {
1306     struct tuna_audio_device *adev = out->dev;
1307 
1308     if (!out->standby) {
1309         pcm_close(out->pcm);
1310         out->pcm = NULL;
1311 
1312         adev->active_output = 0;
1313 
1314         /* if in call, don't turn off the output stage. This will
1315         be done when the call is ended */
1316         if (adev->mode != AUDIO_MODE_IN_CALL) {
1317             /* FIXME: only works if only one output can be active at a time */
1318             set_route_by_array(adev->mixer, hs_output, 0);
1319             set_route_by_array(adev->mixer, hf_output, 0);
1320         }
1321 
1322         /* stop writing to echo reference */
1323         if (out->echo_reference != NULL) {
1324             out->echo_reference->write(out->echo_reference, NULL);
1325             out->echo_reference = NULL;
1326         }
1327 
1328         out->standby = 1;
1329     }
1330     return 0;
1331 }
1332 
out_standby(struct audio_stream * stream)1333 static int out_standby(struct audio_stream *stream)
1334 {
1335     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
1336     int status;
1337 
1338     pthread_mutex_lock(&out->dev->lock);
1339     pthread_mutex_lock(&out->lock);
1340     status = do_output_standby(out);
1341     pthread_mutex_unlock(&out->lock);
1342     pthread_mutex_unlock(&out->dev->lock);
1343     return status;
1344 }
1345 
out_dump(const struct audio_stream * stream,int fd)1346 static int out_dump(const struct audio_stream *stream, int fd)
1347 {
1348     return 0;
1349 }
1350 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1351 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1352 {
1353     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
1354     struct tuna_audio_device *adev = out->dev;
1355     struct tuna_stream_in *in;
1356     struct str_parms *parms;
1357     char *str;
1358     char value[32];
1359     int ret, val = 0;
1360     bool force_input_standby = false;
1361 
1362     parms = str_parms_create_str(kvpairs);
1363 
1364     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1365     if (ret >= 0) {
1366         val = atoi(value);
1367         pthread_mutex_lock(&adev->lock);
1368         pthread_mutex_lock(&out->lock);
1369         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
1370             if (out == adev->active_output) {
1371                 /* a change in output device may change the microphone selection */
1372                 if (adev->active_input &&
1373                         adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1374                     force_input_standby = true;
1375                 }
1376                 /* force standby if moving to/from HDMI */
1377                 if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^
1378                         (adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) ||
1379                         ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
1380                         (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)))
1381                     do_output_standby(out);
1382             }
1383             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
1384             adev->devices |= val;
1385             select_output_device(adev);
1386         }
1387         pthread_mutex_unlock(&out->lock);
1388         if (force_input_standby) {
1389             in = adev->active_input;
1390             pthread_mutex_lock(&in->lock);
1391             do_input_standby(in);
1392             pthread_mutex_unlock(&in->lock);
1393         }
1394         pthread_mutex_unlock(&adev->lock);
1395     }
1396 
1397     str_parms_destroy(parms);
1398     return ret;
1399 }
1400 
out_get_parameters(const struct audio_stream * stream,const char * keys)1401 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
1402 {
1403     return strdup("");
1404 }
1405 
out_get_latency(const struct audio_stream_out * stream)1406 static uint32_t out_get_latency(const struct audio_stream_out *stream)
1407 {
1408     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
1409 
1410     return (SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT * 1000) / out->config.rate;
1411 }
1412 
out_set_volume(struct audio_stream_out * stream,float left,float right)1413 static int out_set_volume(struct audio_stream_out *stream, float left,
1414                           float right)
1415 {
1416     return -ENOSYS;
1417 }
1418 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)1419 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
1420                          size_t bytes)
1421 {
1422     int ret;
1423     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
1424     struct tuna_audio_device *adev = out->dev;
1425     size_t frame_size = audio_stream_frame_size(&out->stream.common);
1426     size_t in_frames = bytes / frame_size;
1427     size_t out_frames = RESAMPLER_BUFFER_SIZE / frame_size;
1428     bool force_input_standby = false;
1429     struct tuna_stream_in *in;
1430     bool low_power;
1431     int kernel_frames;
1432     void *buf;
1433 
1434     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
1435      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
1436      * mutex
1437      */
1438     pthread_mutex_lock(&adev->lock);
1439     pthread_mutex_lock(&out->lock);
1440     if (out->standby) {
1441         ret = start_output_stream(out);
1442         if (ret != 0) {
1443             pthread_mutex_unlock(&adev->lock);
1444             goto exit;
1445         }
1446         out->standby = 0;
1447         /* a change in output device may change the microphone selection */
1448         if (adev->active_input &&
1449                 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
1450             force_input_standby = true;
1451     }
1452     low_power = adev->low_power && !adev->active_input;
1453     pthread_mutex_unlock(&adev->lock);
1454 
1455     if (low_power != out->low_power) {
1456         if (low_power) {
1457             out->write_threshold = LONG_PERIOD_SIZE * PLAYBACK_LONG_PERIOD_COUNT;
1458             out->config.avail_min = LONG_PERIOD_SIZE;
1459         } else {
1460             out->write_threshold = SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT;
1461             out->config.avail_min = SHORT_PERIOD_SIZE;
1462         }
1463         pcm_set_avail_min(out->pcm, out->config.avail_min);
1464         out->low_power = low_power;
1465     }
1466 
1467     /* only use resampler if required */
1468     if (out->config.rate != DEFAULT_OUT_SAMPLING_RATE) {
1469         out->resampler->resample_from_input(out->resampler,
1470                                             (int16_t *)buffer,
1471                                             &in_frames,
1472                                             (int16_t *)out->buffer,
1473                                             &out_frames);
1474         buf = out->buffer;
1475     } else {
1476         out_frames = in_frames;
1477         buf = (void *)buffer;
1478     }
1479     if (out->echo_reference != NULL) {
1480         struct echo_reference_buffer b;
1481         b.raw = (void *)buffer;
1482         b.frame_count = in_frames;
1483 
1484         get_playback_delay(out, out_frames, &b);
1485         out->echo_reference->write(out->echo_reference, &b);
1486     }
1487 
1488     /* do not allow more than out->write_threshold frames in kernel pcm driver buffer */
1489     do {
1490         struct timespec time_stamp;
1491 
1492         if (pcm_get_htimestamp(out->pcm, (unsigned int *)&kernel_frames, &time_stamp) < 0)
1493             break;
1494         kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames;
1495 
1496         if (kernel_frames > out->write_threshold) {
1497             unsigned long time = (unsigned long)
1498                     (((int64_t)(kernel_frames - out->write_threshold) * 1000000) /
1499                             MM_FULL_POWER_SAMPLING_RATE);
1500             if (time < MIN_WRITE_SLEEP_US)
1501                 time = MIN_WRITE_SLEEP_US;
1502             usleep(time);
1503         }
1504     } while (kernel_frames > out->write_threshold);
1505 
1506     ret = pcm_mmap_write(out->pcm, (void *)buf, out_frames * frame_size);
1507 
1508 exit:
1509     pthread_mutex_unlock(&out->lock);
1510 
1511     if (ret != 0) {
1512         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
1513                out_get_sample_rate(&stream->common));
1514     }
1515 
1516     if (force_input_standby) {
1517         pthread_mutex_lock(&adev->lock);
1518         if (adev->active_input) {
1519             in = adev->active_input;
1520             pthread_mutex_lock(&in->lock);
1521             do_input_standby(in);
1522             pthread_mutex_unlock(&in->lock);
1523         }
1524         pthread_mutex_unlock(&adev->lock);
1525     }
1526 
1527     return bytes;
1528 }
1529 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)1530 static int out_get_render_position(const struct audio_stream_out *stream,
1531                                    uint32_t *dsp_frames)
1532 {
1533     return -EINVAL;
1534 }
1535 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1536 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1537 {
1538     return 0;
1539 }
1540 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1541 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1542 {
1543     return 0;
1544 }
1545 
1546 /** audio_stream_in implementation **/
1547 
1548 /* must be called with hw device and input stream mutexes locked */
start_input_stream(struct tuna_stream_in * in)1549 static int start_input_stream(struct tuna_stream_in *in)
1550 {
1551     int ret = 0;
1552     struct tuna_audio_device *adev = in->dev;
1553 
1554     adev->active_input = in;
1555 
1556     if (adev->mode != AUDIO_MODE_IN_CALL) {
1557         adev->devices &= ~AUDIO_DEVICE_IN_ALL;
1558         adev->devices |= in->device;
1559         select_input_device(adev);
1560     }
1561 
1562     if (in->need_echo_reference && in->echo_reference == NULL)
1563         in->echo_reference = get_echo_reference(adev,
1564                                         AUDIO_FORMAT_PCM_16_BIT,
1565                                         in->config.channels,
1566                                         in->requested_rate);
1567 
1568     /* this assumes routing is done previously */
1569     in->pcm = pcm_open(0, PORT_MM2_UL, PCM_IN, &in->config);
1570     if (!pcm_is_ready(in->pcm)) {
1571         LOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm));
1572         pcm_close(in->pcm);
1573         adev->active_input = NULL;
1574         return -ENOMEM;
1575     }
1576 
1577     /* if no supported sample rate is available, use the resampler */
1578     if (in->resampler) {
1579         in->resampler->reset(in->resampler);
1580         in->frames_in = 0;
1581     }
1582     return 0;
1583 }
1584 
in_get_sample_rate(const struct audio_stream * stream)1585 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1586 {
1587     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
1588 
1589     return in->requested_rate;
1590 }
1591 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1592 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1593 {
1594     return 0;
1595 }
1596 
in_get_buffer_size(const struct audio_stream * stream)1597 static size_t in_get_buffer_size(const struct audio_stream *stream)
1598 {
1599     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
1600 
1601     return get_input_buffer_size(in->requested_rate,
1602                                  AUDIO_FORMAT_PCM_16_BIT,
1603                                  in->config.channels);
1604 }
1605 
in_get_channels(const struct audio_stream * stream)1606 static uint32_t in_get_channels(const struct audio_stream *stream)
1607 {
1608     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
1609 
1610     if (in->config.channels == 1) {
1611         return AUDIO_CHANNEL_IN_MONO;
1612     } else {
1613         return AUDIO_CHANNEL_IN_STEREO;
1614     }
1615 }
1616 
in_get_format(const struct audio_stream * stream)1617 static int in_get_format(const struct audio_stream *stream)
1618 {
1619     return AUDIO_FORMAT_PCM_16_BIT;
1620 }
1621 
in_set_format(struct audio_stream * stream,int format)1622 static int in_set_format(struct audio_stream *stream, int format)
1623 {
1624     return 0;
1625 }
1626 
1627 /* must be called with hw device and input stream mutexes locked */
do_input_standby(struct tuna_stream_in * in)1628 static int do_input_standby(struct tuna_stream_in *in)
1629 {
1630     struct tuna_audio_device *adev = in->dev;
1631 
1632     if (!in->standby) {
1633         pcm_close(in->pcm);
1634         in->pcm = NULL;
1635 
1636         adev->active_input = 0;
1637         if (adev->mode != AUDIO_MODE_IN_CALL) {
1638             adev->devices &= ~AUDIO_DEVICE_IN_ALL;
1639             select_input_device(adev);
1640         }
1641 
1642         if (in->echo_reference != NULL) {
1643             /* stop reading from echo reference */
1644             in->echo_reference->read(in->echo_reference, NULL);
1645             put_echo_reference(adev, in->echo_reference);
1646             in->echo_reference = NULL;
1647         }
1648 
1649         in->standby = 1;
1650     }
1651     return 0;
1652 }
1653 
in_standby(struct audio_stream * stream)1654 static int in_standby(struct audio_stream *stream)
1655 {
1656     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
1657     int status;
1658 
1659     pthread_mutex_lock(&in->dev->lock);
1660     pthread_mutex_lock(&in->lock);
1661     status = do_input_standby(in);
1662     pthread_mutex_unlock(&in->lock);
1663     pthread_mutex_unlock(&in->dev->lock);
1664     return status;
1665 }
1666 
in_dump(const struct audio_stream * stream,int fd)1667 static int in_dump(const struct audio_stream *stream, int fd)
1668 {
1669     return 0;
1670 }
1671 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)1672 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1673 {
1674     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
1675     struct tuna_audio_device *adev = in->dev;
1676     struct str_parms *parms;
1677     char *str;
1678     char value[32];
1679     int ret, val = 0;
1680     bool do_standby = false;
1681 
1682     parms = str_parms_create_str(kvpairs);
1683 
1684     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1685 
1686     pthread_mutex_lock(&adev->lock);
1687     pthread_mutex_lock(&in->lock);
1688     if (ret >= 0) {
1689         val = atoi(value);
1690         /* no audio source uses val == 0 */
1691         if ((in->source != val) && (val != 0)) {
1692             in->source = val;
1693             do_standby = true;
1694         }
1695     }
1696 
1697     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1698     if (ret >= 0) {
1699         val = atoi(value);
1700         if ((in->device != val) && (val != 0)) {
1701             in->device = val;
1702             do_standby = true;
1703         }
1704     }
1705 
1706     if (do_standby)
1707         do_input_standby(in);
1708     pthread_mutex_unlock(&in->lock);
1709     pthread_mutex_unlock(&adev->lock);
1710 
1711     str_parms_destroy(parms);
1712     return ret;
1713 }
1714 
in_get_parameters(const struct audio_stream * stream,const char * keys)1715 static char * in_get_parameters(const struct audio_stream *stream,
1716                                 const char *keys)
1717 {
1718     return strdup("");
1719 }
1720 
in_set_gain(struct audio_stream_in * stream,float gain)1721 static int in_set_gain(struct audio_stream_in *stream, float gain)
1722 {
1723     return 0;
1724 }
1725 
get_capture_delay(struct tuna_stream_in * in,size_t frames,struct echo_reference_buffer * buffer)1726 static void get_capture_delay(struct tuna_stream_in *in,
1727                        size_t frames,
1728                        struct echo_reference_buffer *buffer)
1729 {
1730 
1731     /* read frames available in kernel driver buffer */
1732     size_t kernel_frames;
1733     struct timespec tstamp;
1734     long buf_delay;
1735     long rsmp_delay;
1736     long kernel_delay;
1737     long delay_ns;
1738 
1739     if (pcm_get_htimestamp(in->pcm, &kernel_frames, &tstamp) < 0) {
1740         buffer->time_stamp.tv_sec  = 0;
1741         buffer->time_stamp.tv_nsec = 0;
1742         buffer->delay_ns           = 0;
1743         LOGW("read get_capture_delay(): pcm_htimestamp error");
1744         return;
1745     }
1746 
1747     /* read frames available in audio HAL input buffer
1748      * add number of frames being read as we want the capture time of first sample
1749      * in current buffer */
1750     buf_delay = (long)(((int64_t)(in->frames_in + in->proc_frames_in) * 1000000000)
1751                                     / in->config.rate);
1752     /* add delay introduced by resampler */
1753     rsmp_delay = 0;
1754     if (in->resampler) {
1755         rsmp_delay = in->resampler->delay_ns(in->resampler);
1756     }
1757 
1758     kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate);
1759 
1760     delay_ns = kernel_delay + buf_delay + rsmp_delay;
1761 
1762     buffer->time_stamp = tstamp;
1763     buffer->delay_ns   = delay_ns;
1764     LOGV("get_capture_delay time_stamp = [%ld].[%ld], delay_ns: [%d],"
1765          " kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], kernel_frames:[%d], "
1766          "in->frames_in:[%d], in->proc_frames_in:[%d], frames:[%d]",
1767          buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, buffer->delay_ns,
1768          kernel_delay, buf_delay, rsmp_delay, kernel_frames,
1769          in->frames_in, in->proc_frames_in, frames);
1770 
1771 }
1772 
update_echo_reference(struct tuna_stream_in * in,size_t frames)1773 static int32_t update_echo_reference(struct tuna_stream_in *in, size_t frames)
1774 {
1775     struct echo_reference_buffer b;
1776     b.delay_ns = 0;
1777 
1778     LOGV("update_echo_reference, frames = [%d], in->ref_frames_in = [%d],  "
1779           "b.frame_count = [%d]",
1780          frames, in->ref_frames_in, frames - in->ref_frames_in);
1781     if (in->ref_frames_in < frames) {
1782         if (in->ref_buf_size < frames) {
1783             in->ref_buf_size = frames;
1784             in->ref_buf = (int16_t *)realloc(in->ref_buf,
1785                                              in->ref_buf_size *
1786                                                  in->config.channels * sizeof(int16_t));
1787         }
1788 
1789         b.frame_count = frames - in->ref_frames_in;
1790         b.raw = (void *)(in->ref_buf + in->ref_frames_in * in->config.channels);
1791 
1792         get_capture_delay(in, frames, &b);
1793 
1794         if (in->echo_reference->read(in->echo_reference, &b) == 0)
1795         {
1796             in->ref_frames_in += b.frame_count;
1797             LOGV("update_echo_reference: in->ref_frames_in:[%d], "
1798                     "in->ref_buf_size:[%d], frames:[%d], b.frame_count:[%d]",
1799                  in->ref_frames_in, in->ref_buf_size, frames, b.frame_count);
1800         }
1801     } else
1802         LOGW("update_echo_reference: NOT enough frames to read ref buffer");
1803     return b.delay_ns;
1804 }
1805 
set_preprocessor_param(effect_handle_t handle,effect_param_t * param)1806 static int set_preprocessor_param(effect_handle_t handle,
1807                            effect_param_t *param)
1808 {
1809     uint32_t size = sizeof(int);
1810     uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1811                         param->vsize;
1812 
1813     int status = (*handle)->command(handle,
1814                                    EFFECT_CMD_SET_PARAM,
1815                                    sizeof (effect_param_t) + psize,
1816                                    param,
1817                                    &size,
1818                                    &param->status);
1819     if (status == 0)
1820         status = param->status;
1821 
1822     return status;
1823 }
1824 
set_preprocessor_echo_delay(effect_handle_t handle,int32_t delay_us)1825 static int set_preprocessor_echo_delay(effect_handle_t handle,
1826                                      int32_t delay_us)
1827 {
1828     uint32_t buf[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1829     effect_param_t *param = (effect_param_t *)buf;
1830 
1831     param->psize = sizeof(uint32_t);
1832     param->vsize = sizeof(uint32_t);
1833     *(uint32_t *)param->data = AEC_PARAM_ECHO_DELAY;
1834     *((int32_t *)param->data + 1) = delay_us;
1835 
1836     return set_preprocessor_param(handle, param);
1837 }
1838 
push_echo_reference(struct tuna_stream_in * in,size_t frames)1839 static void push_echo_reference(struct tuna_stream_in *in, size_t frames)
1840 {
1841     /* read frames from echo reference buffer and update echo delay
1842      * in->ref_frames_in is updated with frames available in in->ref_buf */
1843     int32_t delay_us = update_echo_reference(in, frames)/1000;
1844     int i;
1845     audio_buffer_t buf;
1846 
1847     if (in->ref_frames_in < frames)
1848         frames = in->ref_frames_in;
1849 
1850     buf.frameCount = frames;
1851     buf.raw = in->ref_buf;
1852 
1853     for (i = 0; i < in->num_preprocessors; i++) {
1854         if ((*in->preprocessors[i])->process_reverse == NULL)
1855             continue;
1856 
1857         (*in->preprocessors[i])->process_reverse(in->preprocessors[i],
1858                                                &buf,
1859                                                NULL);
1860         set_preprocessor_echo_delay(in->preprocessors[i], delay_us);
1861     }
1862 
1863     in->ref_frames_in -= buf.frameCount;
1864     if (in->ref_frames_in) {
1865         memcpy(in->ref_buf,
1866                in->ref_buf + buf.frameCount * in->config.channels,
1867                in->ref_frames_in * in->config.channels * sizeof(int16_t));
1868     }
1869 }
1870 
get_next_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1871 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
1872                                    struct resampler_buffer* buffer)
1873 {
1874     struct tuna_stream_in *in;
1875 
1876     if (buffer_provider == NULL || buffer == NULL)
1877         return -EINVAL;
1878 
1879     in = (struct tuna_stream_in *)((char *)buffer_provider -
1880                                    offsetof(struct tuna_stream_in, buf_provider));
1881 
1882     if (in->pcm == NULL) {
1883         buffer->raw = NULL;
1884         buffer->frame_count = 0;
1885         in->read_status = -ENODEV;
1886         return -ENODEV;
1887     }
1888 
1889     if (in->frames_in == 0) {
1890         in->read_status = pcm_read(in->pcm,
1891                                    (void*)in->buffer,
1892                                    in->config.period_size *
1893                                        audio_stream_frame_size(&in->stream.common));
1894         if (in->read_status != 0) {
1895             LOGE("get_next_buffer() pcm_read error %d", in->read_status);
1896             buffer->raw = NULL;
1897             buffer->frame_count = 0;
1898             return in->read_status;
1899         }
1900         in->frames_in = in->config.period_size;
1901     }
1902 
1903     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
1904                                 in->frames_in : buffer->frame_count;
1905     buffer->i16 = in->buffer + (in->config.period_size - in->frames_in) *
1906                                                 in->config.channels;
1907 
1908     return in->read_status;
1909 
1910 }
1911 
release_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1912 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
1913                                   struct resampler_buffer* buffer)
1914 {
1915     struct tuna_stream_in *in;
1916 
1917     if (buffer_provider == NULL || buffer == NULL)
1918         return;
1919 
1920     in = (struct tuna_stream_in *)((char *)buffer_provider -
1921                                    offsetof(struct tuna_stream_in, buf_provider));
1922 
1923     in->frames_in -= buffer->frame_count;
1924 }
1925 
1926 /* read_frames() reads frames from kernel driver, down samples to capture rate
1927  * if necessary and output the number of frames requested to the buffer specified */
read_frames(struct tuna_stream_in * in,void * buffer,ssize_t frames)1928 static ssize_t read_frames(struct tuna_stream_in *in, void *buffer, ssize_t frames)
1929 {
1930     ssize_t frames_wr = 0;
1931 
1932     while (frames_wr < frames) {
1933         size_t frames_rd = frames - frames_wr;
1934         if (in->resampler != NULL) {
1935             in->resampler->resample_from_provider(in->resampler,
1936                     (int16_t *)((char *)buffer +
1937                             frames_wr * audio_stream_frame_size(&in->stream.common)),
1938                     &frames_rd);
1939         } else {
1940             struct resampler_buffer buf = {
1941                     { raw : NULL, },
1942                     frame_count : frames_rd,
1943             };
1944             get_next_buffer(&in->buf_provider, &buf);
1945             if (buf.raw != NULL) {
1946                 memcpy((char *)buffer +
1947                            frames_wr * audio_stream_frame_size(&in->stream.common),
1948                         buf.raw,
1949                         buf.frame_count * audio_stream_frame_size(&in->stream.common));
1950                 frames_rd = buf.frame_count;
1951             }
1952             release_buffer(&in->buf_provider, &buf);
1953         }
1954         /* in->read_status is updated by getNextBuffer() also called by
1955          * in->resampler->resample_from_provider() */
1956         if (in->read_status != 0)
1957             return in->read_status;
1958 
1959         frames_wr += frames_rd;
1960     }
1961     return frames_wr;
1962 }
1963 
1964 /* process_frames() reads frames from kernel driver (via read_frames()),
1965  * calls the active audio pre processings and output the number of frames requested
1966  * to the buffer specified */
process_frames(struct tuna_stream_in * in,void * buffer,ssize_t frames)1967 static ssize_t process_frames(struct tuna_stream_in *in, void* buffer, ssize_t frames)
1968 {
1969     ssize_t frames_wr = 0;
1970     audio_buffer_t in_buf;
1971     audio_buffer_t out_buf;
1972     int i;
1973 
1974     while (frames_wr < frames) {
1975         /* first reload enough frames at the end of process input buffer */
1976         if (in->proc_frames_in < (size_t)frames) {
1977             ssize_t frames_rd;
1978 
1979             if (in->proc_buf_size < (size_t)frames) {
1980                 in->proc_buf_size = (size_t)frames;
1981                 in->proc_buf = (int16_t *)realloc(in->proc_buf,
1982                                          in->proc_buf_size *
1983                                              in->config.channels * sizeof(int16_t));
1984                 LOGV("process_frames(): in->proc_buf %p size extended to %d frames",
1985                      in->proc_buf, in->proc_buf_size);
1986             }
1987             frames_rd = read_frames(in,
1988                                     in->proc_buf +
1989                                         in->proc_frames_in * in->config.channels,
1990                                     frames - in->proc_frames_in);
1991             if (frames_rd < 0) {
1992                 frames_wr = frames_rd;
1993                 break;
1994             }
1995             in->proc_frames_in += frames_rd;
1996         }
1997 
1998         if (in->echo_reference != NULL)
1999             push_echo_reference(in, in->proc_frames_in);
2000 
2001          /* in_buf.frameCount and out_buf.frameCount indicate respectively
2002           * the maximum number of frames to be consumed and produced by process() */
2003         in_buf.frameCount = in->proc_frames_in;
2004         in_buf.s16 = in->proc_buf;
2005         out_buf.frameCount = frames - frames_wr;
2006         out_buf.s16 = (int16_t *)buffer + frames_wr * in->config.channels;
2007 
2008         for (i = 0; i < in->num_preprocessors; i++)
2009             (*in->preprocessors[i])->process(in->preprocessors[i],
2010                                                &in_buf,
2011                                                &out_buf);
2012 
2013         /* process() has updated the number of frames consumed and produced in
2014          * in_buf.frameCount and out_buf.frameCount respectively
2015          * move remaining frames to the beginning of in->proc_buf */
2016         in->proc_frames_in -= in_buf.frameCount;
2017         if (in->proc_frames_in) {
2018             memcpy(in->proc_buf,
2019                    in->proc_buf + in_buf.frameCount * in->config.channels,
2020                    in->proc_frames_in * in->config.channels * sizeof(int16_t));
2021         }
2022 
2023         /* if not enough frames were passed to process(), read more and retry. */
2024         if (out_buf.frameCount == 0)
2025             continue;
2026 
2027         frames_wr += out_buf.frameCount;
2028     }
2029     return frames_wr;
2030 }
2031 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)2032 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
2033                        size_t bytes)
2034 {
2035     int ret = 0;
2036     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
2037     struct tuna_audio_device *adev = in->dev;
2038     size_t frames_rq = bytes / audio_stream_frame_size(&stream->common);
2039 
2040     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
2041      * on the input stream mutex - e.g. executing select_mode() while holding the hw device
2042      * mutex
2043      */
2044     pthread_mutex_lock(&adev->lock);
2045     pthread_mutex_lock(&in->lock);
2046     if (in->standby) {
2047         ret = start_input_stream(in);
2048         if (ret == 0)
2049             in->standby = 0;
2050     }
2051     pthread_mutex_unlock(&adev->lock);
2052 
2053     if (ret < 0)
2054         goto exit;
2055 
2056     if (in->num_preprocessors != 0)
2057         ret = process_frames(in, buffer, frames_rq);
2058     else if (in->resampler != NULL)
2059         ret = read_frames(in, buffer, frames_rq);
2060     else
2061         ret = pcm_read(in->pcm, buffer, bytes);
2062 
2063     if (ret > 0)
2064         ret = 0;
2065 
2066     if (ret == 0 && adev->mic_mute)
2067         memset(buffer, 0, bytes);
2068 
2069 exit:
2070     if (ret < 0)
2071         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
2072                in_get_sample_rate(&stream->common));
2073 
2074     pthread_mutex_unlock(&in->lock);
2075     return bytes;
2076 }
2077 
in_get_input_frames_lost(struct audio_stream_in * stream)2078 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2079 {
2080     return 0;
2081 }
2082 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2083 static int in_add_audio_effect(const struct audio_stream *stream,
2084                                effect_handle_t effect)
2085 {
2086     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
2087     int status;
2088     effect_descriptor_t desc;
2089 
2090     pthread_mutex_lock(&in->dev->lock);
2091     pthread_mutex_lock(&in->lock);
2092     if (in->num_preprocessors >= MAX_PREPROCESSORS) {
2093         status = -ENOSYS;
2094         goto exit;
2095     }
2096 
2097     status = (*effect)->get_descriptor(effect, &desc);
2098     if (status != 0)
2099         goto exit;
2100 
2101     in->preprocessors[in->num_preprocessors++] = effect;
2102 
2103     if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
2104         in->need_echo_reference = true;
2105         do_input_standby(in);
2106     }
2107 
2108 exit:
2109 
2110     pthread_mutex_unlock(&in->lock);
2111     pthread_mutex_unlock(&in->dev->lock);
2112     return status;
2113 }
2114 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2115 static int in_remove_audio_effect(const struct audio_stream *stream,
2116                                   effect_handle_t effect)
2117 {
2118     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
2119     int i;
2120     int status = -EINVAL;
2121     bool found = false;
2122     effect_descriptor_t desc;
2123 
2124     pthread_mutex_lock(&in->dev->lock);
2125     pthread_mutex_lock(&in->lock);
2126     if (in->num_preprocessors <= 0) {
2127         status = -ENOSYS;
2128         goto exit;
2129     }
2130 
2131     for (i = 0; i < in->num_preprocessors; i++) {
2132         if (found) {
2133             in->preprocessors[i - 1] = in->preprocessors[i];
2134             continue;
2135         }
2136         if (in->preprocessors[i] == effect) {
2137             in->preprocessors[i] = NULL;
2138             status = 0;
2139             found = true;
2140         }
2141     }
2142 
2143     if (status != 0)
2144         goto exit;
2145 
2146     in->num_preprocessors--;
2147 
2148     status = (*effect)->get_descriptor(effect, &desc);
2149     if (status != 0)
2150         goto exit;
2151     if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
2152         in->need_echo_reference = false;
2153         do_input_standby(in);
2154     }
2155 
2156 exit:
2157 
2158     pthread_mutex_unlock(&in->lock);
2159     pthread_mutex_unlock(&in->dev->lock);
2160     return status;
2161 }
2162 
2163 
adev_open_output_stream(struct audio_hw_device * dev,uint32_t devices,int * format,uint32_t * channels,uint32_t * sample_rate,struct audio_stream_out ** stream_out)2164 static int adev_open_output_stream(struct audio_hw_device *dev,
2165                                    uint32_t devices, int *format,
2166                                    uint32_t *channels, uint32_t *sample_rate,
2167                                    struct audio_stream_out **stream_out)
2168 {
2169     struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev;
2170     struct tuna_stream_out *out;
2171     int ret;
2172 
2173     out = (struct tuna_stream_out *)calloc(1, sizeof(struct tuna_stream_out));
2174     if (!out)
2175         return -ENOMEM;
2176 
2177     ret = create_resampler(DEFAULT_OUT_SAMPLING_RATE,
2178                            MM_FULL_POWER_SAMPLING_RATE,
2179                            2,
2180                            RESAMPLER_QUALITY_DEFAULT,
2181                            NULL,
2182                            &out->resampler);
2183     if (ret != 0)
2184         goto err_open;
2185     out->buffer = malloc(RESAMPLER_BUFFER_SIZE); /* todo: allow for reallocing */
2186 
2187     out->stream.common.get_sample_rate = out_get_sample_rate;
2188     out->stream.common.set_sample_rate = out_set_sample_rate;
2189     out->stream.common.get_buffer_size = out_get_buffer_size;
2190     out->stream.common.get_channels = out_get_channels;
2191     out->stream.common.get_format = out_get_format;
2192     out->stream.common.set_format = out_set_format;
2193     out->stream.common.standby = out_standby;
2194     out->stream.common.dump = out_dump;
2195     out->stream.common.set_parameters = out_set_parameters;
2196     out->stream.common.get_parameters = out_get_parameters;
2197     out->stream.common.add_audio_effect = out_add_audio_effect;
2198     out->stream.common.remove_audio_effect = out_remove_audio_effect;
2199     out->stream.get_latency = out_get_latency;
2200     out->stream.set_volume = out_set_volume;
2201     out->stream.write = out_write;
2202     out->stream.get_render_position = out_get_render_position;
2203 
2204     out->config = pcm_config_mm;
2205 
2206     out->dev = ladev;
2207     out->standby = 1;
2208 
2209     /* FIXME: when we support multiple output devices, we will want to
2210      * do the following:
2211      * adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
2212      * adev->devices |= out->device;
2213      * select_output_device(adev);
2214      * This is because out_set_parameters() with a route is not
2215      * guaranteed to be called after an output stream is opened. */
2216 
2217     *format = out_get_format(&out->stream.common);
2218     *channels = out_get_channels(&out->stream.common);
2219     *sample_rate = out_get_sample_rate(&out->stream.common);
2220 
2221     *stream_out = &out->stream;
2222     return 0;
2223 
2224 err_open:
2225     free(out);
2226     *stream_out = NULL;
2227     return ret;
2228 }
2229 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)2230 static void adev_close_output_stream(struct audio_hw_device *dev,
2231                                      struct audio_stream_out *stream)
2232 {
2233     struct tuna_stream_out *out = (struct tuna_stream_out *)stream;
2234 
2235     out_standby(&stream->common);
2236     if (out->buffer)
2237         free(out->buffer);
2238     if (out->resampler)
2239         release_resampler(out->resampler);
2240     free(stream);
2241 }
2242 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)2243 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2244 {
2245     struct tuna_audio_device *adev = (struct tuna_audio_device *)dev;
2246     struct str_parms *parms;
2247     char *str;
2248     char value[32];
2249     int ret;
2250 
2251     parms = str_parms_create_str(kvpairs);
2252     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2253     if (ret >= 0) {
2254         int tty_mode;
2255 
2256         if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2257             tty_mode = TTY_MODE_OFF;
2258         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2259             tty_mode = TTY_MODE_VCO;
2260         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2261             tty_mode = TTY_MODE_HCO;
2262         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2263             tty_mode = TTY_MODE_FULL;
2264         else
2265             return -EINVAL;
2266 
2267         pthread_mutex_lock(&adev->lock);
2268         if (tty_mode != adev->tty_mode) {
2269             adev->tty_mode = tty_mode;
2270             if (adev->mode == AUDIO_MODE_IN_CALL)
2271                 select_output_device(adev);
2272         }
2273         pthread_mutex_unlock(&adev->lock);
2274     }
2275 
2276     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2277     if (ret >= 0) {
2278         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2279             adev->bluetooth_nrec = true;
2280         else
2281             adev->bluetooth_nrec = false;
2282     }
2283 
2284     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2285     if (ret >= 0) {
2286         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2287             adev->low_power = false;
2288         else
2289             adev->low_power = true;
2290     }
2291 
2292     str_parms_destroy(parms);
2293     return ret;
2294 }
2295 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)2296 static char * adev_get_parameters(const struct audio_hw_device *dev,
2297                                   const char *keys)
2298 {
2299     return strdup("");
2300 }
2301 
adev_init_check(const struct audio_hw_device * dev)2302 static int adev_init_check(const struct audio_hw_device *dev)
2303 {
2304     return 0;
2305 }
2306 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)2307 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2308 {
2309     struct tuna_audio_device *adev = (struct tuna_audio_device *)dev;
2310 
2311     adev->voice_volume = volume;
2312 
2313     if (adev->mode == AUDIO_MODE_IN_CALL)
2314         ril_set_call_volume(&adev->ril, SOUND_TYPE_VOICE, volume);
2315 
2316     return 0;
2317 }
2318 
adev_set_master_volume(struct audio_hw_device * dev,float volume)2319 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2320 {
2321     return -ENOSYS;
2322 }
2323 
adev_set_mode(struct audio_hw_device * dev,int mode)2324 static int adev_set_mode(struct audio_hw_device *dev, int mode)
2325 {
2326     struct tuna_audio_device *adev = (struct tuna_audio_device *)dev;
2327 
2328     pthread_mutex_lock(&adev->lock);
2329     if (adev->mode != mode) {
2330         adev->mode = mode;
2331         select_mode(adev);
2332     }
2333     pthread_mutex_unlock(&adev->lock);
2334 
2335     return 0;
2336 }
2337 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)2338 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2339 {
2340     struct tuna_audio_device *adev = (struct tuna_audio_device *)dev;
2341 
2342     adev->mic_mute = state;
2343 
2344     return 0;
2345 }
2346 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)2347 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2348 {
2349     struct tuna_audio_device *adev = (struct tuna_audio_device *)dev;
2350 
2351     *state = adev->mic_mute;
2352 
2353     return 0;
2354 }
2355 
adev_get_input_buffer_size(const struct audio_hw_device * dev,uint32_t sample_rate,int format,int channel_count)2356 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2357                                          uint32_t sample_rate, int format,
2358                                          int channel_count)
2359 {
2360     size_t size;
2361 
2362     if (check_input_parameters(sample_rate, format, channel_count) != 0)
2363         return 0;
2364 
2365     return get_input_buffer_size(sample_rate, format, channel_count);
2366 }
2367 
adev_open_input_stream(struct audio_hw_device * dev,uint32_t devices,int * format,uint32_t * channel_mask,uint32_t * sample_rate,audio_in_acoustics_t acoustics,struct audio_stream_in ** stream_in)2368 static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
2369                                   int *format, uint32_t *channel_mask,
2370                                   uint32_t *sample_rate,
2371                                   audio_in_acoustics_t acoustics,
2372                                   struct audio_stream_in **stream_in)
2373 {
2374     struct tuna_audio_device *ladev = (struct tuna_audio_device *)dev;
2375     struct tuna_stream_in *in;
2376     int ret;
2377     int channel_count = popcount(*channel_mask);
2378 
2379     if (check_input_parameters(*sample_rate, *format, channel_count) != 0)
2380         return -EINVAL;
2381 
2382     in = (struct tuna_stream_in *)calloc(1, sizeof(struct tuna_stream_in));
2383     if (!in)
2384         return -ENOMEM;
2385 
2386     in->stream.common.get_sample_rate = in_get_sample_rate;
2387     in->stream.common.set_sample_rate = in_set_sample_rate;
2388     in->stream.common.get_buffer_size = in_get_buffer_size;
2389     in->stream.common.get_channels = in_get_channels;
2390     in->stream.common.get_format = in_get_format;
2391     in->stream.common.set_format = in_set_format;
2392     in->stream.common.standby = in_standby;
2393     in->stream.common.dump = in_dump;
2394     in->stream.common.set_parameters = in_set_parameters;
2395     in->stream.common.get_parameters = in_get_parameters;
2396     in->stream.common.add_audio_effect = in_add_audio_effect;
2397     in->stream.common.remove_audio_effect = in_remove_audio_effect;
2398     in->stream.set_gain = in_set_gain;
2399     in->stream.read = in_read;
2400     in->stream.get_input_frames_lost = in_get_input_frames_lost;
2401 
2402     in->requested_rate = *sample_rate;
2403 
2404     memcpy(&in->config, &pcm_config_mm_ul, sizeof(pcm_config_mm_ul));
2405     in->config.channels = channel_count;
2406 
2407     in->buffer = malloc(in->config.period_size *
2408                         audio_stream_frame_size(&in->stream.common));
2409     if (!in->buffer) {
2410         ret = -ENOMEM;
2411         goto err;
2412     }
2413 
2414     if (in->requested_rate != in->config.rate) {
2415         in->buf_provider.get_next_buffer = get_next_buffer;
2416         in->buf_provider.release_buffer = release_buffer;
2417 
2418         ret = create_resampler(in->config.rate,
2419                                in->requested_rate,
2420                                in->config.channels,
2421                                RESAMPLER_QUALITY_DEFAULT,
2422                                &in->buf_provider,
2423                                &in->resampler);
2424         if (ret != 0) {
2425             ret = -EINVAL;
2426             goto err;
2427         }
2428     }
2429 
2430     in->dev = ladev;
2431     in->standby = 1;
2432     in->device = devices;
2433 
2434     *stream_in = &in->stream;
2435     return 0;
2436 
2437 err:
2438     if (in->resampler)
2439         release_resampler(in->resampler);
2440 
2441     free(in);
2442     *stream_in = NULL;
2443     return ret;
2444 }
2445 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)2446 static void adev_close_input_stream(struct audio_hw_device *dev,
2447                                    struct audio_stream_in *stream)
2448 {
2449     struct tuna_stream_in *in = (struct tuna_stream_in *)stream;
2450 
2451     in_standby(&stream->common);
2452 
2453     if (in->resampler) {
2454         free(in->buffer);
2455         release_resampler(in->resampler);
2456     }
2457 
2458     free(stream);
2459     return;
2460 }
2461 
adev_dump(const audio_hw_device_t * device,int fd)2462 static int adev_dump(const audio_hw_device_t *device, int fd)
2463 {
2464     return 0;
2465 }
2466 
adev_close(hw_device_t * device)2467 static int adev_close(hw_device_t *device)
2468 {
2469     struct tuna_audio_device *adev = (struct tuna_audio_device *)device;
2470 
2471     /* RIL */
2472     ril_close(&adev->ril);
2473 
2474     mixer_close(adev->mixer);
2475     free(device);
2476     return 0;
2477 }
2478 
adev_get_supported_devices(const struct audio_hw_device * dev)2479 static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
2480 {
2481     return (/* OUT */
2482             AUDIO_DEVICE_OUT_EARPIECE |
2483             AUDIO_DEVICE_OUT_SPEAKER |
2484             AUDIO_DEVICE_OUT_WIRED_HEADSET |
2485             AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
2486             AUDIO_DEVICE_OUT_AUX_DIGITAL |
2487             AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
2488             AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
2489             AUDIO_DEVICE_OUT_ALL_SCO |
2490             AUDIO_DEVICE_OUT_DEFAULT |
2491             /* IN */
2492             AUDIO_DEVICE_IN_COMMUNICATION |
2493             AUDIO_DEVICE_IN_AMBIENT |
2494             AUDIO_DEVICE_IN_BUILTIN_MIC |
2495             AUDIO_DEVICE_IN_WIRED_HEADSET |
2496             AUDIO_DEVICE_IN_AUX_DIGITAL |
2497             AUDIO_DEVICE_IN_BACK_MIC |
2498             AUDIO_DEVICE_IN_ALL_SCO |
2499             AUDIO_DEVICE_IN_DEFAULT);
2500 }
2501 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)2502 static int adev_open(const hw_module_t* module, const char* name,
2503                      hw_device_t** device)
2504 {
2505     struct tuna_audio_device *adev;
2506     int ret;
2507 
2508     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2509         return -EINVAL;
2510 
2511     adev = calloc(1, sizeof(struct tuna_audio_device));
2512     if (!adev)
2513         return -ENOMEM;
2514 
2515     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
2516     adev->hw_device.common.version = 0;
2517     adev->hw_device.common.module = (struct hw_module_t *) module;
2518     adev->hw_device.common.close = adev_close;
2519 
2520     adev->hw_device.get_supported_devices = adev_get_supported_devices;
2521     adev->hw_device.init_check = adev_init_check;
2522     adev->hw_device.set_voice_volume = adev_set_voice_volume;
2523     adev->hw_device.set_master_volume = adev_set_master_volume;
2524     adev->hw_device.set_mode = adev_set_mode;
2525     adev->hw_device.set_mic_mute = adev_set_mic_mute;
2526     adev->hw_device.get_mic_mute = adev_get_mic_mute;
2527     adev->hw_device.set_parameters = adev_set_parameters;
2528     adev->hw_device.get_parameters = adev_get_parameters;
2529     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2530     adev->hw_device.open_output_stream = adev_open_output_stream;
2531     adev->hw_device.close_output_stream = adev_close_output_stream;
2532     adev->hw_device.open_input_stream = adev_open_input_stream;
2533     adev->hw_device.close_input_stream = adev_close_input_stream;
2534     adev->hw_device.dump = adev_dump;
2535 
2536     adev->mixer = mixer_open(0);
2537     if (!adev->mixer) {
2538         free(adev);
2539         LOGE("Unable to open the mixer, aborting.");
2540         return -EINVAL;
2541     }
2542 
2543     adev->mixer_ctls.dl1_eq = mixer_get_ctl_by_name(adev->mixer,
2544                                            MIXER_DL1_EQUALIZER);
2545     adev->mixer_ctls.mm_dl2_volume = mixer_get_ctl_by_name(adev->mixer,
2546                                            MIXER_DL2_MEDIA_PLAYBACK_VOLUME);
2547     adev->mixer_ctls.vx_dl2_volume = mixer_get_ctl_by_name(adev->mixer,
2548                                            MIXER_DL2_VOICE_PLAYBACK_VOLUME);
2549     adev->mixer_ctls.mm_dl1 = mixer_get_ctl_by_name(adev->mixer,
2550                                            MIXER_DL1_MIXER_MULTIMEDIA);
2551     adev->mixer_ctls.vx_dl1 = mixer_get_ctl_by_name(adev->mixer,
2552                                            MIXER_DL1_MIXER_VOICE);
2553     adev->mixer_ctls.mm_dl2 = mixer_get_ctl_by_name(adev->mixer,
2554                                            MIXER_DL2_MIXER_MULTIMEDIA);
2555     adev->mixer_ctls.vx_dl2 = mixer_get_ctl_by_name(adev->mixer,
2556                                            MIXER_DL2_MIXER_VOICE);
2557     adev->mixer_ctls.dl2_mono = mixer_get_ctl_by_name(adev->mixer,
2558                                            MIXER_DL2_MONO_MIXER);
2559     adev->mixer_ctls.dl1_headset = mixer_get_ctl_by_name(adev->mixer,
2560                                            MIXER_DL1_PDM_SWITCH);
2561     adev->mixer_ctls.dl1_bt = mixer_get_ctl_by_name(adev->mixer,
2562                                            MIXER_DL1_BT_VX_SWITCH);
2563     adev->mixer_ctls.earpiece_enable = mixer_get_ctl_by_name(adev->mixer,
2564                                            MIXER_EARPHONE_ENABLE_SWITCH);
2565     adev->mixer_ctls.left_capture = mixer_get_ctl_by_name(adev->mixer,
2566                                            MIXER_ANALOG_LEFT_CAPTURE_ROUTE);
2567     adev->mixer_ctls.right_capture = mixer_get_ctl_by_name(adev->mixer,
2568                                            MIXER_ANALOG_RIGHT_CAPTURE_ROUTE);
2569     adev->mixer_ctls.amic_ul_volume = mixer_get_ctl_by_name(adev->mixer,
2570                                            MIXER_AMIC_UL_VOLUME);
2571     adev->mixer_ctls.voice_ul_volume = mixer_get_ctl_by_name(adev->mixer,
2572                                            MIXER_AUDUL_VOICE_UL_VOLUME);
2573     adev->mixer_ctls.sidetone_capture = mixer_get_ctl_by_name(adev->mixer,
2574                                            MIXER_SIDETONE_MIXER_CAPTURE);
2575     adev->mixer_ctls.headset_volume = mixer_get_ctl_by_name(adev->mixer,
2576                                            MIXER_HEADSET_PLAYBACK_VOLUME);
2577     adev->mixer_ctls.speaker_volume = mixer_get_ctl_by_name(adev->mixer,
2578                                            MIXER_HANDSFREE_PLAYBACK_VOLUME);
2579     adev->mixer_ctls.earpiece_volume = mixer_get_ctl_by_name(adev->mixer,
2580                                            MIXER_EARPHONE_PLAYBACK_VOLUME);
2581 
2582     if (!adev->mixer_ctls.dl1_eq || !adev->mixer_ctls.vx_dl2_volume ||
2583         !adev->mixer_ctls.mm_dl2_volume || !adev->mixer_ctls.mm_dl1 ||
2584         !adev->mixer_ctls.vx_dl1 || !adev->mixer_ctls.mm_dl2 ||
2585         !adev->mixer_ctls.vx_dl2 || !adev->mixer_ctls.dl2_mono ||
2586         !adev->mixer_ctls.dl1_headset || !adev->mixer_ctls.dl1_bt ||
2587         !adev->mixer_ctls.earpiece_enable || !adev->mixer_ctls.left_capture ||
2588         !adev->mixer_ctls.right_capture || !adev->mixer_ctls.amic_ul_volume ||
2589         !adev->mixer_ctls.voice_ul_volume || !adev->mixer_ctls.sidetone_capture ||
2590         !adev->mixer_ctls.headset_volume || !adev->mixer_ctls.speaker_volume ||
2591         !adev->mixer_ctls.earpiece_volume) {
2592         mixer_close(adev->mixer);
2593         free(adev);
2594         LOGE("Unable to locate all mixer controls, aborting.");
2595         return -EINVAL;
2596     }
2597 
2598     /* Set the default route before the PCM stream is opened */
2599     pthread_mutex_lock(&adev->lock);
2600     set_route_by_array(adev->mixer, defaults, 1);
2601     adev->mode = AUDIO_MODE_NORMAL;
2602     adev->devices = AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_IN_BUILTIN_MIC;
2603     select_output_device(adev);
2604 
2605     adev->pcm_modem_dl = NULL;
2606     adev->pcm_modem_ul = NULL;
2607     adev->voice_volume = 1.0f;
2608     adev->tty_mode = TTY_MODE_OFF;
2609     adev->device_is_toro = is_device_toro();
2610     adev->bluetooth_nrec = true;
2611     adev->wb_amr = 0;
2612 
2613     /* RIL */
2614     ril_open(&adev->ril);
2615     pthread_mutex_unlock(&adev->lock);
2616     /* register callback for wideband AMR setting */
2617     ril_register_set_wb_amr_callback(audio_set_wb_amr_callback, (void *)adev);
2618 
2619     *device = &adev->hw_device.common;
2620 
2621     return 0;
2622 }
2623 
2624 static struct hw_module_methods_t hal_module_methods = {
2625     .open = adev_open,
2626 };
2627 
2628 struct audio_module HAL_MODULE_INFO_SYM = {
2629     .common = {
2630         .tag = HARDWARE_MODULE_TAG,
2631         .version_major = 1,
2632         .version_minor = 0,
2633         .id = AUDIO_HARDWARE_MODULE_ID,
2634         .name = "Tuna audio HW HAL",
2635         .author = "The Android Open Source Project",
2636         .methods = &hal_module_methods,
2637     },
2638 };
2639