blob: 1c981031c96f5168dc90ee2183bf6ea58afb7a61 [file] [log] [blame]
Surendar Karka59c51072017-12-13 11:25:57 +05301/* Copyright (c) 2013-2014, 2016-2018 The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29#define LOG_TAG "soundtrigger"
30/* #define LOG_NDEBUG 0 */
31#define LOG_NDDEBUG 0
32
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -070033#include <errno.h>
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070034#include <stdbool.h>
35#include <stdlib.h>
36#include <dlfcn.h>
37#include <cutils/log.h>
38#include "audio_hw.h"
39#include "audio_extn.h"
40#include "platform.h"
41#include "platform_api.h"
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +053042
43/*-------------------- Begin: AHAL-STHAL Interface ---------------------------*/
44/*
45 * Maintain the proprietary interface between AHAL and STHAL locally to avoid
46 * the compilation dependency of interface header file from STHAL.
47 */
48
49#define MAKE_HAL_VERSION(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
50#define MAJOR_VERSION(ver) (((ver) & 0xff00) >> 8)
51#define MINOR_VERSION(ver) ((ver) & 0x00ff)
52
53/* Proprietary interface version used for compatibility with STHAL */
54#define STHAL_PROP_API_VERSION_1_0 MAKE_HAL_VERSION(1, 0)
55#define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_1_0
56
57#define ST_EVENT_CONFIG_MAX_STR_VALUE 32
58#define ST_DEVICE_HANDSET_MIC 1
59
60typedef enum {
61 ST_EVENT_SESSION_REGISTER,
62 ST_EVENT_SESSION_DEREGISTER,
63 ST_EVENT_START_KEEP_ALIVE,
64 ST_EVENT_STOP_KEEP_ALIVE,
65} sound_trigger_event_type_t;
66
67typedef enum {
68 AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE,
69 AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE,
70 AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE,
71 AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE,
72 AUDIO_EVENT_STOP_LAB,
73 AUDIO_EVENT_SSR,
74 AUDIO_EVENT_NUM_ST_SESSIONS,
75 AUDIO_EVENT_READ_SAMPLES,
76 AUDIO_EVENT_DEVICE_CONNECT,
77 AUDIO_EVENT_DEVICE_DISCONNECT,
78 AUDIO_EVENT_SVA_EXEC_MODE,
79 AUDIO_EVENT_SVA_EXEC_MODE_STATUS,
80 AUDIO_EVENT_CAPTURE_STREAM_INACTIVE,
81 AUDIO_EVENT_CAPTURE_STREAM_ACTIVE,
Zhou Song4623d7e2018-07-11 17:18:07 +080082 AUDIO_EVENT_BATTERY_STATUS_CHANGED,
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +053083 AUDIO_EVENT_GET_PARAM
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +053084} audio_event_type_t;
85
86typedef enum {
87 USECASE_TYPE_PCM_PLAYBACK,
88 USECASE_TYPE_PCM_CAPTURE,
89 USECASE_TYPE_VOICE_CALL,
90 USECASE_TYPE_VOIP_CALL,
91} audio_stream_usecase_type_t;
92
93typedef enum {
94 SND_CARD_STATUS_OFFLINE,
95 SND_CARD_STATUS_ONLINE,
96 CPE_STATUS_OFFLINE,
97 CPE_STATUS_ONLINE
98} ssr_event_status_t;
99
100struct sound_trigger_session_info {
101 void* p_ses; /* opaque pointer to st_session obj */
102 int capture_handle;
103 struct pcm *pcm;
104 struct pcm_config config;
105};
106
107struct audio_read_samples_info {
108 struct sound_trigger_session_info *ses_info;
109 void *buf;
110 size_t num_bytes;
111};
112
113struct audio_hal_usecase {
114 audio_stream_usecase_type_t type;
115};
116
117struct sound_trigger_event_info {
118 struct sound_trigger_session_info st_ses;
119};
120typedef struct sound_trigger_event_info sound_trigger_event_info_t;
121
122struct sound_trigger_device_info {
123 int device;
124};
125
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530126struct sound_trigger_get_param_data {
127 char *param;
128 int sm_handle;
129 struct str_parms *reply;
130};
131
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530132struct audio_event_info {
133 union {
134 ssr_event_status_t status;
135 int value;
136 struct sound_trigger_session_info ses_info;
137 struct audio_read_samples_info aud_info;
138 char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
139 struct audio_hal_usecase usecase;
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530140 struct sound_trigger_get_param_data st_get_param_data;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530141 } u;
142 struct sound_trigger_device_info device_info;
143};
144typedef struct audio_event_info audio_event_info_t;
145/* STHAL callback which is called by AHAL */
146typedef int (*sound_trigger_hw_call_back_t)(audio_event_type_t,
147 struct audio_event_info*);
148
149/*---------------- End: AHAL-STHAL Interface ----------------------------------*/
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700150
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +0530151#ifdef DYNAMIC_LOG_ENABLED
152#include <log_xml_parser.h>
153#define LOG_MASK HAL_MOD_FILE_SND_TRIGGER
154#include <log_utils.h>
155#endif
156
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700157#define XSTR(x) STR(x)
158#define STR(x) #x
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530159#define MAX_LIBRARY_PATH 100
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700160
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530161#define DLSYM(handle, ptr, symbol, err) \
162do {\
163 const char* error; \
164 *(void**)&ptr = dlsym(handle, #symbol); \
165 if ((error = dlerror())) {\
166 ALOGE("%s: ERROR. %s", __func__, error);\
167 err = -ENODEV;\
168 }\
169} while(0)
170
171#ifdef __LP64__
172#define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib64/hw/sound_trigger.primary.%s.so"
173#else
174#define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib/hw/sound_trigger.primary.%s.so"
175#endif
176
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530177#define SVA_PARAM_DIRECTION_OF_ARRIVAL "st_direction_of_arrival"
178#define SVA_PARAM_CHANNEL_INDEX "st_channel_index"
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530179/*
180 * Current proprietary API version used by AHAL. Queried by STHAL
181 * for compatibility check with AHAL
182 */
183const unsigned int sthal_prop_api_version = STHAL_PROP_API_CURRENT_VERSION;
184
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700185struct sound_trigger_info {
186 struct sound_trigger_session_info st_ses;
187 bool lab_stopped;
188 struct listnode list;
189};
190
191struct sound_trigger_audio_device {
192 void *lib_handle;
193 struct audio_device *adev;
194 sound_trigger_hw_call_back_t st_callback;
195 struct listnode st_ses_list;
196 pthread_mutex_t lock;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530197 unsigned int sthal_prop_api_version;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700198};
199
200static struct sound_trigger_audio_device *st_dev;
201
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530202#if LINUX_ENABLED
203static void get_library_path(char *lib_path)
204{
205 snprintf(lib_path, MAX_LIBRARY_PATH,
Manish Dewangan6a36d002017-10-09 14:11:00 +0530206 "sound_trigger.primary.default.so");
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530207}
208#else
209static void get_library_path(char *lib_path)
210{
211 snprintf(lib_path, MAX_LIBRARY_PATH,
David Ng06ccd872017-03-15 11:39:33 -0700212 "/vendor/lib/hw/sound_trigger.primary.%s.so",
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530213 XSTR(SOUND_TRIGGER_PLATFORM_NAME));
214}
215#endif
216
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700217static struct sound_trigger_info *
218get_sound_trigger_info(int capture_handle)
219{
220 struct sound_trigger_info *st_ses_info = NULL;
221 struct listnode *node;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700222 ALOGV("%s: list empty %d capture_handle %d", __func__,
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700223 list_empty(&st_dev->st_ses_list), capture_handle);
224 list_for_each(node, &st_dev->st_ses_list) {
225 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
226 if (st_ses_info->st_ses.capture_handle == capture_handle)
227 return st_ses_info;
228 }
229 return NULL;
230}
231
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530232static int populate_usecase(struct audio_hal_usecase *usecase,
233 struct audio_usecase *uc_info)
234{
235 int status = 0;
236
237 switch(uc_info->type) {
238 case PCM_PLAYBACK:
239 if (uc_info->id == USECASE_AUDIO_PLAYBACK_VOIP)
240 usecase->type = USECASE_TYPE_VOIP_CALL;
241 else
242 usecase->type = USECASE_TYPE_PCM_PLAYBACK;
243 break;
244
245 case PCM_CAPTURE:
246 if (uc_info->id == USECASE_AUDIO_RECORD_VOIP)
247 usecase->type = USECASE_TYPE_VOIP_CALL;
248 else
249 usecase->type = USECASE_TYPE_PCM_CAPTURE;
250 break;
251
252 case VOICE_CALL:
253 usecase->type = USECASE_TYPE_VOICE_CALL;
254 break;
255
256 default:
257 ALOGE("%s: unsupported usecase type %d", __func__, uc_info->type);
258 status = -EINVAL;
259 }
260 return status;
261}
262
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530263static void stdev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
264{
265 if (!parms)
266 return;
267
268 audio_extn_sound_trigger_set_parameters(NULL, parms);
269 return;
270}
271
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700272int audio_hw_call_back(sound_trigger_event_type_t event,
273 sound_trigger_event_info_t* config)
274{
275 int status = 0;
276 struct sound_trigger_info *st_ses_info;
277
278 if (!st_dev)
279 return -EINVAL;
280
281 pthread_mutex_lock(&st_dev->lock);
282 switch (event) {
283 case ST_EVENT_SESSION_REGISTER:
284 if (!config) {
285 ALOGE("%s: NULL config", __func__);
286 status = -EINVAL;
287 break;
288 }
289 st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
290 if (!st_ses_info) {
291 ALOGE("%s: st_ses_info alloc failed", __func__);
292 status = -ENOMEM;
293 break;
294 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700295 memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (struct sound_trigger_session_info));
296 ALOGV("%s: add capture_handle %d st session opaque ptr %p", __func__,
297 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700298 list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
299 break;
300
Surendar Karka59c51072017-12-13 11:25:57 +0530301 case ST_EVENT_START_KEEP_ALIVE:
302 pthread_mutex_unlock(&st_dev->lock);
303 pthread_mutex_lock(&st_dev->adev->lock);
304 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
305 pthread_mutex_unlock(&st_dev->adev->lock);
306 goto done;
307
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700308 case ST_EVENT_SESSION_DEREGISTER:
309 if (!config) {
310 ALOGE("%s: NULL config", __func__);
311 status = -EINVAL;
312 break;
313 }
314 st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
315 if (!st_ses_info) {
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700316 ALOGE("%s: st session opaque ptr %p not in the list!", __func__, config->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700317 status = -EINVAL;
318 break;
319 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700320 ALOGV("%s: remove capture_handle %d st session opaque ptr %p", __func__,
321 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700322 list_remove(&st_ses_info->list);
323 free(st_ses_info);
324 break;
Surendar Karka59c51072017-12-13 11:25:57 +0530325
326 case ST_EVENT_STOP_KEEP_ALIVE:
327 pthread_mutex_unlock(&st_dev->lock);
328 pthread_mutex_lock(&st_dev->adev->lock);
329 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
330 pthread_mutex_unlock(&st_dev->adev->lock);
331 goto done;
332
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700333 default:
334 ALOGW("%s: Unknown event %d", __func__, event);
335 break;
336 }
337 pthread_mutex_unlock(&st_dev->lock);
Surendar Karka59c51072017-12-13 11:25:57 +0530338done:
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700339 return status;
340}
341
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700342int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
343 size_t bytes)
344{
345 int ret = -1;
346 struct sound_trigger_info *st_info = NULL;
347 audio_event_info_t event;
348
349 if (!st_dev)
350 return ret;
351
352 if (!in->is_st_session_active) {
353 ALOGE(" %s: Sound trigger is not active", __func__);
354 goto exit;
355 }
356 if(in->standby)
357 in->standby = false;
358
359 pthread_mutex_lock(&st_dev->lock);
360 st_info = get_sound_trigger_info(in->capture_handle);
361 pthread_mutex_unlock(&st_dev->lock);
362 if (st_info) {
363 event.u.aud_info.ses_info = &st_info->st_ses;
364 event.u.aud_info.buf = buffer;
365 event.u.aud_info.num_bytes = bytes;
366 ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
367 }
368
369exit:
370 if (ret) {
371 if (-ENETRESET == ret)
372 in->is_st_session_active = false;
373 memset(buffer, 0, bytes);
374 ALOGV("%s: read failed status %d - sleep", __func__, ret);
375 usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
376 in->config.rate));
377 }
378 return ret;
379}
380
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700381void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
382{
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700383 struct sound_trigger_info *st_ses_info = NULL;
384 audio_event_info_t event;
385
Mingming Yinfd7607b2016-01-22 12:48:44 -0800386 if (!st_dev || !in || !in->is_st_session_active)
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700387 return;
388
389 pthread_mutex_lock(&st_dev->lock);
390 st_ses_info = get_sound_trigger_info(in->capture_handle);
Bharath Ramachandramurthy5baa6a52014-10-21 11:18:49 -0700391 pthread_mutex_unlock(&st_dev->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700392 if (st_ses_info) {
393 event.u.ses_info = st_ses_info->st_ses;
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700394 ALOGV("%s: AUDIO_EVENT_STOP_LAB st sess %p", __func__, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700395 st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
Mingming Yinfd7607b2016-01-22 12:48:44 -0800396 in->is_st_session_active = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700397 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700398}
399void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
400{
401 struct sound_trigger_info *st_ses_info = NULL;
402 struct listnode *node;
403
404 if (!st_dev || !in)
405 return;
406
407 pthread_mutex_lock(&st_dev->lock);
408 in->is_st_session = false;
409 ALOGV("%s: list %d capture_handle %d", __func__,
410 list_empty(&st_dev->st_ses_list), in->capture_handle);
411 list_for_each(node, &st_dev->st_ses_list) {
412 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
413 if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700414 in->config = st_ses_info->st_ses.config;
415 in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
416 in->is_st_session = true;
Bharath Ramachandramurthy837535b2015-02-05 14:27:59 -0800417 in->is_st_session_active = true;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700418 ALOGD("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
419 break;
420 }
421 }
422 pthread_mutex_unlock(&st_dev->lock);
423}
424
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530425bool is_same_as_st_device(snd_device_t snd_device)
426{
427 if (snd_device == SND_DEVICE_IN_HANDSET_MIC_AEC ||
428 snd_device == SND_DEVICE_IN_HANDSET_MIC ||
429 snd_device == SND_DEVICE_IN_HANDSET_MIC_AEC_NS ||
430 snd_device == SND_DEVICE_IN_SPEAKER_MIC ||
431 snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC ||
432 snd_device == SND_DEVICE_IN_SPEAKER_MIC_AEC ||
433 snd_device == SND_DEVICE_IN_SPEAKER_MIC_AEC_NS ||
434 snd_device == SND_DEVICE_IN_SPEAKER_MIC_NS) {
435 ALOGD("audio HAL using same device %d as ST", snd_device);
436 return true;
437 }
438 return false;
439}
440
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700441void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
442 st_event_type_t event)
443{
444 bool raise_event = false;
445 int device_type = -1;
446
447 if (!st_dev)
448 return;
449
450 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
451 snd_device < SND_DEVICE_OUT_END)
452 device_type = PCM_PLAYBACK;
453 else if (snd_device >= SND_DEVICE_IN_BEGIN &&
454 snd_device < SND_DEVICE_IN_END)
455 device_type = PCM_CAPTURE;
456 else {
457 ALOGE("%s: invalid device 0x%x, for event %d",
458 __func__, snd_device, event);
459 return;
460 }
461
462 raise_event = platform_sound_trigger_device_needs_event(snd_device);
463 ALOGI("%s: device 0x%x of type %d for Event %d, with Raise=%d",
464 __func__, snd_device, device_type, event, raise_event);
465 if (raise_event && (device_type == PCM_CAPTURE)) {
466 switch(event) {
467 case ST_EVENT_SND_DEVICE_FREE:
468 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
469 break;
470 case ST_EVENT_SND_DEVICE_BUSY:
471 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
472 break;
473 default:
474 ALOGW("%s:invalid event %d for device 0x%x",
475 __func__, event, snd_device);
476 }
477 }/*Events for output device, if required can be placed here in else*/
478}
479
480void audio_extn_sound_trigger_update_stream_status(struct audio_usecase *uc_info,
481 st_event_type_t event)
482{
483 bool raise_event = false;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530484 struct audio_event_info ev_info;
485 audio_event_type_t ev;
486 /*Initialize to invalid device*/
487 ev_info.device_info.device = -1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700488
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530489 if (!st_dev)
490 return;
491
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700492 if (uc_info == NULL) {
493 ALOGE("%s: usecase is NULL!!!", __func__);
494 return;
495 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700496
Dhanalakshmi Siddaniabf4a642018-06-15 14:47:20 +0530497 if ((st_dev->sthal_prop_api_version < STHAL_PROP_API_VERSION_1_0) &&
498 (uc_info->type != PCM_PLAYBACK))
499 return;
500
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530501 if ((uc_info->in_snd_device >= SND_DEVICE_IN_BEGIN &&
502 uc_info->in_snd_device < SND_DEVICE_IN_END)) {
503 if (is_same_as_st_device(uc_info->in_snd_device))
504 ev_info.device_info.device = ST_DEVICE_HANDSET_MIC;
505 } else {
506 ALOGE("%s: invalid input device 0x%x, for event %d",
507 __func__, uc_info->in_snd_device, event);
508 }
509 raise_event = platform_sound_trigger_usecase_needs_event(uc_info->id);
510 ALOGD("%s: uc_info->id %d of type %d for Event %d, with Raise=%d",
511 __func__, uc_info->id, uc_info->type, event, raise_event);
512 if (raise_event) {
513 if (uc_info->type == PCM_PLAYBACK) {
514 switch(event) {
515 case ST_EVENT_STREAM_FREE:
516 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE, NULL);
517 break;
518 case ST_EVENT_STREAM_BUSY:
519 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE, NULL);
520 break;
521 default:
522 ALOGW("%s:invalid event %d, for usecase %d",
523 __func__, event, uc_info->id);
524 }
Revathi Uddarajub46af6c2018-05-03 12:01:38 +0530525 } else if ((uc_info->type == PCM_CAPTURE) || (uc_info->type == VOICE_CALL)) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530526 if (event == ST_EVENT_STREAM_BUSY)
527 ev = AUDIO_EVENT_CAPTURE_STREAM_ACTIVE;
528 else
529 ev = AUDIO_EVENT_CAPTURE_STREAM_INACTIVE;
530 if (!populate_usecase(&ev_info.u.usecase, uc_info)) {
531 ALOGD("%s: send event %d: usecase id %d, type %d",
532 __func__, ev, uc_info->id, uc_info->type);
533 st_dev->st_callback(ev, &ev_info);
534 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700535 }
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530536 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700537}
538
539void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
540 struct str_parms *params)
541{
542 audio_event_info_t event;
543 char value[32];
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700544 int ret, val;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700545
546 if(!st_dev || !params) {
547 ALOGE("%s: str_params NULL", __func__);
548 return;
549 }
550
551 ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
552 sizeof(value));
553 if (ret > 0) {
554 if (strstr(value, "OFFLINE")) {
555 event.u.status = SND_CARD_STATUS_OFFLINE;
556 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
557 }
558 else if (strstr(value, "ONLINE")) {
559 event.u.status = SND_CARD_STATUS_ONLINE;
560 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
561 }
562 else
563 ALOGE("%s: unknown snd_card_status", __func__);
564 }
565
566 ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
567 if (ret > 0) {
568 if (strstr(value, "OFFLINE")) {
569 event.u.status = CPE_STATUS_OFFLINE;
570 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
571 }
572 else if (strstr(value, "ONLINE")) {
573 event.u.status = CPE_STATUS_ONLINE;
574 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
575 }
576 else
577 ALOGE("%s: unknown CPE status", __func__);
578 }
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700579
580 ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
581 if (ret >= 0) {
582 event.u.value = val;
583 st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
584 }
Quinn male73dd1fd2016-12-02 10:47:11 -0800585
586 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_CONNECT, &val);
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +0530587 if ((ret >= 0) && (audio_is_input_device(val) ||
588 (val == AUDIO_DEVICE_OUT_LINE))) {
Quinn male73dd1fd2016-12-02 10:47:11 -0800589 event.u.value = val;
590 st_dev->st_callback(AUDIO_EVENT_DEVICE_CONNECT, &event);
591 }
592
593 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_DISCONNECT, &val);
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +0530594 if ((ret >= 0) && (audio_is_input_device(val) ||
595 (val == AUDIO_DEVICE_OUT_LINE))) {
Quinn male73dd1fd2016-12-02 10:47:11 -0800596 event.u.value = val;
597 st_dev->st_callback(AUDIO_EVENT_DEVICE_DISCONNECT, &event);
598 }
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530599
600 ret = str_parms_get_str(params, "SVA_EXEC_MODE", value, sizeof(value));
601 if (ret >= 0) {
602 strlcpy(event.u.str_value, value, sizeof(event.u.str_value));
603 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE, &event);
604 }
605}
606
607void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev __unused,
608 struct str_parms *query, struct str_parms *reply)
609{
610 audio_event_info_t event;
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530611 int ret, val;
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530612 char value[32];
613
614 ret = str_parms_get_str(query, "SVA_EXEC_MODE_STATUS", value,
615 sizeof(value));
616 if (ret >= 0) {
617 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE_STATUS, &event);
618 str_parms_add_int(reply, "SVA_EXEC_MODE_STATUS", event.u.value);
619 }
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530620
621 ret = str_parms_get_int(query, SVA_PARAM_DIRECTION_OF_ARRIVAL, &val);
622 if (ret >= 0) {
623 event.u.st_get_param_data.sm_handle = val;
624 event.u.st_get_param_data.param = SVA_PARAM_DIRECTION_OF_ARRIVAL;
625 event.u.st_get_param_data.reply = reply;
626 st_dev->st_callback(AUDIO_EVENT_GET_PARAM, &event);
627 }
628
629 ret = str_parms_get_int(query, SVA_PARAM_CHANNEL_INDEX, &val);
630 if (ret >= 0) {
631 event.u.st_get_param_data.sm_handle = val;
632 event.u.st_get_param_data.param = SVA_PARAM_CHANNEL_INDEX;
633 event.u.st_get_param_data.reply = reply;
634 st_dev->st_callback(AUDIO_EVENT_GET_PARAM, &event);
635 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700636}
637
638int audio_extn_sound_trigger_init(struct audio_device *adev)
639{
640 int status = 0;
641 char sound_trigger_lib[100];
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530642 void *sthal_prop_api_version;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700643
644 ALOGI("%s: Enter", __func__);
645
646 st_dev = (struct sound_trigger_audio_device*)
647 calloc(1, sizeof(struct sound_trigger_audio_device));
648 if (!st_dev) {
649 ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
650 return -ENOMEM;
651 }
652
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530653 get_library_path(sound_trigger_lib);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700654 st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
655
656 if (st_dev->lib_handle == NULL) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530657 ALOGE("%s: error %s", __func__, dlerror());
658 status = -ENODEV;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700659 goto cleanup;
660 }
661 ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
662
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530663 dlerror();
664 DLSYM(st_dev->lib_handle, st_dev->st_callback, sound_trigger_hw_call_back,
665 status);
666 if (status)
667 goto cleanup;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700668
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530669 DLSYM(st_dev->lib_handle, sthal_prop_api_version,
670 sthal_prop_api_version, status);
671 if (status) {
672 st_dev->sthal_prop_api_version = 0;
673 status = 0; /* passthru for backward compability */
674 } else {
675 st_dev->sthal_prop_api_version = *(int*)sthal_prop_api_version;
676 if (MAJOR_VERSION(st_dev->sthal_prop_api_version) !=
677 MAJOR_VERSION(STHAL_PROP_API_CURRENT_VERSION)) {
678 ALOGE("%s: Incompatible API versions ahal:0x%x != sthal:0x%x",
679 __func__, STHAL_PROP_API_CURRENT_VERSION,
680 st_dev->sthal_prop_api_version);
681 goto cleanup;
682 }
683 ALOGD("%s: sthal is using proprietary API version 0x%04x", __func__,
684 st_dev->sthal_prop_api_version);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700685 }
686
687 st_dev->adev = adev;
688 list_init(&st_dev->st_ses_list);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530689 audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700690
691 return 0;
692
693cleanup:
694 if (st_dev->lib_handle)
695 dlclose(st_dev->lib_handle);
696 free(st_dev);
697 st_dev = NULL;
698 return status;
699
700}
701
702void audio_extn_sound_trigger_deinit(struct audio_device *adev)
703{
704 ALOGI("%s: Enter", __func__);
705 if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530706 audio_extn_snd_mon_unregister_listener(st_dev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700707 dlclose(st_dev->lib_handle);
708 free(st_dev);
709 st_dev = NULL;
710 }
711}