1 /*
2  * Copyright (C) 2015 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_dsm_feedback"
18 /*#define LOG_NDEBUG 0*/
19 #define LOG_NDDEBUG 0
20 
21 #include <errno.h>
22 #include <math.h>
23 #include <log/log.h>
24 
25 #include "audio_hw.h"
26 #include "platform.h"
27 #include "platform_api.h"
28 #include <stdlib.h>
29 
30 
31 static struct pcm_config pcm_config_dsm = {
32     .channels = 2,
33     .rate = 48000,
34     .period_size = 256,
35     .period_count = 4,
36     .format = PCM_FORMAT_S16_LE,
37     .start_threshold = 0,
38     .stop_threshold = INT_MAX,
39     .avail_min = 0,
40 };
41 
start_dsm_feedback_processing(struct audio_device * adev,int enable)42 int start_dsm_feedback_processing(struct audio_device *adev, int enable)
43 {
44     int ret = 0;
45     int32_t pcm_dev_tx_id = -1;
46     static struct pcm *dsm_pcm_handle = NULL;
47 
48     if (enable) {
49         /*do nothing if already enabled*/
50         if (dsm_pcm_handle)
51             return ret;
52 
53         pcm_dev_tx_id = platform_get_pcm_device_id(USECASE_AUDIO_DSM_FEEDBACK, PCM_CAPTURE);
54         if (pcm_dev_tx_id < 0) {
55             ALOGE("%s: Invalid pcm device for usecase (%d)",
56                   __func__, USECASE_AUDIO_DSM_FEEDBACK);
57             ret = -ENODEV;
58             goto close;
59         }
60 
61         dsm_pcm_handle = pcm_open(adev->snd_card,
62                                  pcm_dev_tx_id,
63                                  PCM_IN, &pcm_config_dsm);
64         if (dsm_pcm_handle && !pcm_is_ready(dsm_pcm_handle)) {
65             ALOGE("%s: %s", __func__, pcm_get_error(dsm_pcm_handle));
66             ret = -EIO;
67             goto close;
68         }
69 
70         if (pcm_start(dsm_pcm_handle) < 0) {
71             ALOGE("%s: pcm start for RX failed", __func__);
72             ret = -EINVAL;
73             goto close;
74         }
75 
76         return ret;
77     }
78 
79 close:
80     /*close pcm if disable or error happend in opening*/
81     if (dsm_pcm_handle) {
82         pcm_close(dsm_pcm_handle);
83         dsm_pcm_handle = NULL;
84     }
85 
86     return ret;
87 }
88 
audio_extn_dsm_feedback_enable(struct audio_device * adev,snd_device_t snd_device,int benable)89 void audio_extn_dsm_feedback_enable(struct audio_device *adev,
90                          snd_device_t snd_device,
91                          int benable)
92 {
93     if ( NULL == adev )
94         return;
95 
96     if( snd_device == SND_DEVICE_OUT_SPEAKER ||
97         snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE ||
98         snd_device == SND_DEVICE_OUT_VOICE_SPEAKER ||
99         snd_device == SND_DEVICE_OUT_SPEAKER_SAFE ||
100         snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES ||
101         snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE ||
102         snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES ||
103         snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE )
104         start_dsm_feedback_processing(adev, benable);
105 }
106