blob: 0fa30d0047f4fa7f890f2b6011022ddb359397cd [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>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053038#include <unistd.h>
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070039#include "audio_hw.h"
40#include "audio_extn.h"
41#include "platform.h"
42#include "platform_api.h"
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +053043
44/*-------------------- Begin: AHAL-STHAL Interface ---------------------------*/
45/*
46 * Maintain the proprietary interface between AHAL and STHAL locally to avoid
47 * the compilation dependency of interface header file from STHAL.
48 */
49
50#define MAKE_HAL_VERSION(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
51#define MAJOR_VERSION(ver) (((ver) & 0xff00) >> 8)
52#define MINOR_VERSION(ver) ((ver) & 0x00ff)
53
54/* Proprietary interface version used for compatibility with STHAL */
55#define STHAL_PROP_API_VERSION_1_0 MAKE_HAL_VERSION(1, 0)
56#define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_1_0
57
58#define ST_EVENT_CONFIG_MAX_STR_VALUE 32
59#define ST_DEVICE_HANDSET_MIC 1
60
61typedef enum {
62 ST_EVENT_SESSION_REGISTER,
63 ST_EVENT_SESSION_DEREGISTER,
64 ST_EVENT_START_KEEP_ALIVE,
65 ST_EVENT_STOP_KEEP_ALIVE,
66} sound_trigger_event_type_t;
67
68typedef enum {
69 AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE,
70 AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE,
71 AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE,
72 AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE,
73 AUDIO_EVENT_STOP_LAB,
74 AUDIO_EVENT_SSR,
75 AUDIO_EVENT_NUM_ST_SESSIONS,
76 AUDIO_EVENT_READ_SAMPLES,
77 AUDIO_EVENT_DEVICE_CONNECT,
78 AUDIO_EVENT_DEVICE_DISCONNECT,
79 AUDIO_EVENT_SVA_EXEC_MODE,
80 AUDIO_EVENT_SVA_EXEC_MODE_STATUS,
81 AUDIO_EVENT_CAPTURE_STREAM_INACTIVE,
82 AUDIO_EVENT_CAPTURE_STREAM_ACTIVE,
83} audio_event_type_t;
84
85typedef enum {
86 USECASE_TYPE_PCM_PLAYBACK,
87 USECASE_TYPE_PCM_CAPTURE,
88 USECASE_TYPE_VOICE_CALL,
89 USECASE_TYPE_VOIP_CALL,
90} audio_stream_usecase_type_t;
91
92typedef enum {
93 SND_CARD_STATUS_OFFLINE,
94 SND_CARD_STATUS_ONLINE,
95 CPE_STATUS_OFFLINE,
96 CPE_STATUS_ONLINE
97} ssr_event_status_t;
98
99struct sound_trigger_session_info {
100 void* p_ses; /* opaque pointer to st_session obj */
101 int capture_handle;
102 struct pcm *pcm;
103 struct pcm_config config;
104};
105
106struct audio_read_samples_info {
107 struct sound_trigger_session_info *ses_info;
108 void *buf;
109 size_t num_bytes;
110};
111
112struct audio_hal_usecase {
113 audio_stream_usecase_type_t type;
114};
115
116struct sound_trigger_event_info {
117 struct sound_trigger_session_info st_ses;
118};
119typedef struct sound_trigger_event_info sound_trigger_event_info_t;
120
121struct sound_trigger_device_info {
122 int device;
123};
124
125struct audio_event_info {
126 union {
127 ssr_event_status_t status;
128 int value;
129 struct sound_trigger_session_info ses_info;
130 struct audio_read_samples_info aud_info;
131 char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
132 struct audio_hal_usecase usecase;
133 } u;
134 struct sound_trigger_device_info device_info;
135};
136typedef struct audio_event_info audio_event_info_t;
137/* STHAL callback which is called by AHAL */
138typedef int (*sound_trigger_hw_call_back_t)(audio_event_type_t,
139 struct audio_event_info*);
140
141/*---------------- End: AHAL-STHAL Interface ----------------------------------*/
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700142
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +0530143#ifdef DYNAMIC_LOG_ENABLED
144#include <log_xml_parser.h>
145#define LOG_MASK HAL_MOD_FILE_SND_TRIGGER
146#include <log_utils.h>
147#endif
148
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700149#define XSTR(x) STR(x)
150#define STR(x) #x
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530151#define MAX_LIBRARY_PATH 100
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700152
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530153#define DLSYM(handle, ptr, symbol, err) \
154do {\
Venkatesh Mangalappalibcc684c2018-04-06 14:58:44 -0700155 ptr = dlsym(handle, #symbol); \
156 if (ptr == NULL) {\
157 ALOGW("%s: %s not found. %s", __func__, #symbol, dlerror());\
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530158 err = -ENODEV;\
159 }\
160} while(0)
161
162#ifdef __LP64__
163#define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib64/hw/sound_trigger.primary.%s.so"
164#else
165#define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib/hw/sound_trigger.primary.%s.so"
166#endif
167
168/*
169 * Current proprietary API version used by AHAL. Queried by STHAL
170 * for compatibility check with AHAL
171 */
172const unsigned int sthal_prop_api_version = STHAL_PROP_API_CURRENT_VERSION;
173
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700174struct sound_trigger_info {
175 struct sound_trigger_session_info st_ses;
176 bool lab_stopped;
177 struct listnode list;
178};
179
180struct sound_trigger_audio_device {
181 void *lib_handle;
182 struct audio_device *adev;
183 sound_trigger_hw_call_back_t st_callback;
184 struct listnode st_ses_list;
185 pthread_mutex_t lock;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530186 unsigned int sthal_prop_api_version;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700187};
188
189static struct sound_trigger_audio_device *st_dev;
190
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530191#if LINUX_ENABLED
192static void get_library_path(char *lib_path)
193{
194 snprintf(lib_path, MAX_LIBRARY_PATH,
Manish Dewangan6a36d002017-10-09 14:11:00 +0530195 "sound_trigger.primary.default.so");
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530196}
197#else
198static void get_library_path(char *lib_path)
199{
200 snprintf(lib_path, MAX_LIBRARY_PATH,
David Ng06ccd872017-03-15 11:39:33 -0700201 "/vendor/lib/hw/sound_trigger.primary.%s.so",
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530202 XSTR(SOUND_TRIGGER_PLATFORM_NAME));
203}
204#endif
205
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700206static struct sound_trigger_info *
207get_sound_trigger_info(int capture_handle)
208{
209 struct sound_trigger_info *st_ses_info = NULL;
210 struct listnode *node;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700211 ALOGV("%s: list empty %d capture_handle %d", __func__,
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700212 list_empty(&st_dev->st_ses_list), capture_handle);
213 list_for_each(node, &st_dev->st_ses_list) {
214 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
215 if (st_ses_info->st_ses.capture_handle == capture_handle)
216 return st_ses_info;
217 }
218 return NULL;
219}
220
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530221static int populate_usecase(struct audio_hal_usecase *usecase,
222 struct audio_usecase *uc_info)
223{
224 int status = 0;
225
226 switch(uc_info->type) {
227 case PCM_PLAYBACK:
228 if (uc_info->id == USECASE_AUDIO_PLAYBACK_VOIP)
229 usecase->type = USECASE_TYPE_VOIP_CALL;
230 else
231 usecase->type = USECASE_TYPE_PCM_PLAYBACK;
232 break;
233
234 case PCM_CAPTURE:
235 if (uc_info->id == USECASE_AUDIO_RECORD_VOIP)
236 usecase->type = USECASE_TYPE_VOIP_CALL;
237 else
238 usecase->type = USECASE_TYPE_PCM_CAPTURE;
239 break;
240
241 case VOICE_CALL:
242 usecase->type = USECASE_TYPE_VOICE_CALL;
243 break;
244
245 default:
246 ALOGE("%s: unsupported usecase type %d", __func__, uc_info->type);
247 status = -EINVAL;
248 }
249 return status;
250}
251
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530252static void stdev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
253{
254 if (!parms)
255 return;
256
257 audio_extn_sound_trigger_set_parameters(NULL, parms);
258 return;
259}
260
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700261int audio_hw_call_back(sound_trigger_event_type_t event,
262 sound_trigger_event_info_t* config)
263{
264 int status = 0;
265 struct sound_trigger_info *st_ses_info;
266
267 if (!st_dev)
268 return -EINVAL;
269
270 pthread_mutex_lock(&st_dev->lock);
271 switch (event) {
272 case ST_EVENT_SESSION_REGISTER:
273 if (!config) {
274 ALOGE("%s: NULL config", __func__);
275 status = -EINVAL;
276 break;
277 }
278 st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
279 if (!st_ses_info) {
280 ALOGE("%s: st_ses_info alloc failed", __func__);
281 status = -ENOMEM;
282 break;
283 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700284 memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (struct sound_trigger_session_info));
285 ALOGV("%s: add capture_handle %d st session opaque ptr %p", __func__,
286 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700287 list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
288 break;
289
Surendar Karka59c51072017-12-13 11:25:57 +0530290 case ST_EVENT_START_KEEP_ALIVE:
291 pthread_mutex_unlock(&st_dev->lock);
292 pthread_mutex_lock(&st_dev->adev->lock);
293 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
294 pthread_mutex_unlock(&st_dev->adev->lock);
295 goto done;
296
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700297 case ST_EVENT_SESSION_DEREGISTER:
298 if (!config) {
299 ALOGE("%s: NULL config", __func__);
300 status = -EINVAL;
301 break;
302 }
303 st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
304 if (!st_ses_info) {
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700305 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 -0700306 status = -EINVAL;
307 break;
308 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700309 ALOGV("%s: remove capture_handle %d st session opaque ptr %p", __func__,
310 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700311 list_remove(&st_ses_info->list);
312 free(st_ses_info);
313 break;
Surendar Karka59c51072017-12-13 11:25:57 +0530314
315 case ST_EVENT_STOP_KEEP_ALIVE:
316 pthread_mutex_unlock(&st_dev->lock);
317 pthread_mutex_lock(&st_dev->adev->lock);
318 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
319 pthread_mutex_unlock(&st_dev->adev->lock);
320 goto done;
321
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700322 default:
323 ALOGW("%s: Unknown event %d", __func__, event);
324 break;
325 }
326 pthread_mutex_unlock(&st_dev->lock);
Surendar Karka59c51072017-12-13 11:25:57 +0530327done:
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700328 return status;
329}
330
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700331int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
332 size_t bytes)
333{
334 int ret = -1;
335 struct sound_trigger_info *st_info = NULL;
336 audio_event_info_t event;
337
338 if (!st_dev)
339 return ret;
340
341 if (!in->is_st_session_active) {
342 ALOGE(" %s: Sound trigger is not active", __func__);
343 goto exit;
344 }
345 if(in->standby)
346 in->standby = false;
347
348 pthread_mutex_lock(&st_dev->lock);
349 st_info = get_sound_trigger_info(in->capture_handle);
350 pthread_mutex_unlock(&st_dev->lock);
351 if (st_info) {
352 event.u.aud_info.ses_info = &st_info->st_ses;
353 event.u.aud_info.buf = buffer;
354 event.u.aud_info.num_bytes = bytes;
355 ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
356 }
357
358exit:
359 if (ret) {
360 if (-ENETRESET == ret)
361 in->is_st_session_active = false;
362 memset(buffer, 0, bytes);
363 ALOGV("%s: read failed status %d - sleep", __func__, ret);
364 usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
365 in->config.rate));
366 }
367 return ret;
368}
369
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700370void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
371{
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700372 struct sound_trigger_info *st_ses_info = NULL;
373 audio_event_info_t event;
374
Mingming Yinfd7607b2016-01-22 12:48:44 -0800375 if (!st_dev || !in || !in->is_st_session_active)
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700376 return;
377
378 pthread_mutex_lock(&st_dev->lock);
379 st_ses_info = get_sound_trigger_info(in->capture_handle);
Bharath Ramachandramurthy5baa6a52014-10-21 11:18:49 -0700380 pthread_mutex_unlock(&st_dev->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700381 if (st_ses_info) {
382 event.u.ses_info = st_ses_info->st_ses;
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700383 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 -0700384 st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
Mingming Yinfd7607b2016-01-22 12:48:44 -0800385 in->is_st_session_active = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700386 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700387}
388void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
389{
390 struct sound_trigger_info *st_ses_info = NULL;
391 struct listnode *node;
392
393 if (!st_dev || !in)
394 return;
395
396 pthread_mutex_lock(&st_dev->lock);
397 in->is_st_session = false;
398 ALOGV("%s: list %d capture_handle %d", __func__,
399 list_empty(&st_dev->st_ses_list), in->capture_handle);
400 list_for_each(node, &st_dev->st_ses_list) {
401 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
402 if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700403 in->config = st_ses_info->st_ses.config;
404 in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
405 in->is_st_session = true;
Bharath Ramachandramurthy837535b2015-02-05 14:27:59 -0800406 in->is_st_session_active = true;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700407 ALOGD("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
408 break;
409 }
410 }
411 pthread_mutex_unlock(&st_dev->lock);
412}
413
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530414bool is_same_as_st_device(snd_device_t snd_device)
415{
416 if (snd_device == SND_DEVICE_IN_HANDSET_MIC_AEC ||
417 snd_device == SND_DEVICE_IN_HANDSET_MIC ||
418 snd_device == SND_DEVICE_IN_HANDSET_MIC_AEC_NS ||
419 snd_device == SND_DEVICE_IN_SPEAKER_MIC ||
420 snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC ||
421 snd_device == SND_DEVICE_IN_SPEAKER_MIC_AEC ||
422 snd_device == SND_DEVICE_IN_SPEAKER_MIC_AEC_NS ||
423 snd_device == SND_DEVICE_IN_SPEAKER_MIC_NS) {
424 ALOGD("audio HAL using same device %d as ST", snd_device);
425 return true;
426 }
427 return false;
428}
429
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700430void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
431 st_event_type_t event)
432{
433 bool raise_event = false;
434 int device_type = -1;
435
436 if (!st_dev)
437 return;
438
439 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
440 snd_device < SND_DEVICE_OUT_END)
441 device_type = PCM_PLAYBACK;
442 else if (snd_device >= SND_DEVICE_IN_BEGIN &&
443 snd_device < SND_DEVICE_IN_END)
444 device_type = PCM_CAPTURE;
445 else {
446 ALOGE("%s: invalid device 0x%x, for event %d",
447 __func__, snd_device, event);
448 return;
449 }
450
451 raise_event = platform_sound_trigger_device_needs_event(snd_device);
452 ALOGI("%s: device 0x%x of type %d for Event %d, with Raise=%d",
453 __func__, snd_device, device_type, event, raise_event);
454 if (raise_event && (device_type == PCM_CAPTURE)) {
455 switch(event) {
456 case ST_EVENT_SND_DEVICE_FREE:
457 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
458 break;
459 case ST_EVENT_SND_DEVICE_BUSY:
460 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
461 break;
462 default:
463 ALOGW("%s:invalid event %d for device 0x%x",
464 __func__, event, snd_device);
465 }
466 }/*Events for output device, if required can be placed here in else*/
467}
468
469void audio_extn_sound_trigger_update_stream_status(struct audio_usecase *uc_info,
470 st_event_type_t event)
471{
472 bool raise_event = false;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530473 struct audio_event_info ev_info;
474 audio_event_type_t ev;
475 /*Initialize to invalid device*/
476 ev_info.device_info.device = -1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700477
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530478 if (!st_dev)
479 return;
480
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700481 if (uc_info == NULL) {
482 ALOGE("%s: usecase is NULL!!!", __func__);
483 return;
484 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700485
Dhanalakshmi Siddani24e9ad52018-06-15 14:47:20 +0530486 if ((st_dev->sthal_prop_api_version < STHAL_PROP_API_VERSION_1_0) &&
487 (uc_info->type != PCM_PLAYBACK))
488 return;
489
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530490 if ((uc_info->in_snd_device >= SND_DEVICE_IN_BEGIN &&
491 uc_info->in_snd_device < SND_DEVICE_IN_END)) {
492 if (is_same_as_st_device(uc_info->in_snd_device))
493 ev_info.device_info.device = ST_DEVICE_HANDSET_MIC;
494 } else {
495 ALOGE("%s: invalid input device 0x%x, for event %d",
496 __func__, uc_info->in_snd_device, event);
497 }
498 raise_event = platform_sound_trigger_usecase_needs_event(uc_info->id);
499 ALOGD("%s: uc_info->id %d of type %d for Event %d, with Raise=%d",
500 __func__, uc_info->id, uc_info->type, event, raise_event);
501 if (raise_event) {
502 if (uc_info->type == PCM_PLAYBACK) {
503 switch(event) {
504 case ST_EVENT_STREAM_FREE:
505 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE, NULL);
506 break;
507 case ST_EVENT_STREAM_BUSY:
508 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE, NULL);
509 break;
510 default:
511 ALOGW("%s:invalid event %d, for usecase %d",
512 __func__, event, uc_info->id);
513 }
Revathi Uddaraju9f99ddd2018-05-03 12:01:38 +0530514 } else if ((uc_info->type == PCM_CAPTURE) || (uc_info->type == VOICE_CALL)) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530515 if (event == ST_EVENT_STREAM_BUSY)
516 ev = AUDIO_EVENT_CAPTURE_STREAM_ACTIVE;
517 else
518 ev = AUDIO_EVENT_CAPTURE_STREAM_INACTIVE;
519 if (!populate_usecase(&ev_info.u.usecase, uc_info)) {
520 ALOGD("%s: send event %d: usecase id %d, type %d",
521 __func__, ev, uc_info->id, uc_info->type);
522 st_dev->st_callback(ev, &ev_info);
523 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700524 }
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530525 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700526}
527
528void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
529 struct str_parms *params)
530{
531 audio_event_info_t event;
532 char value[32];
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700533 int ret, val;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700534
535 if(!st_dev || !params) {
536 ALOGE("%s: str_params NULL", __func__);
537 return;
538 }
539
540 ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
541 sizeof(value));
542 if (ret > 0) {
543 if (strstr(value, "OFFLINE")) {
544 event.u.status = SND_CARD_STATUS_OFFLINE;
545 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
546 }
547 else if (strstr(value, "ONLINE")) {
548 event.u.status = SND_CARD_STATUS_ONLINE;
549 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
550 }
551 else
552 ALOGE("%s: unknown snd_card_status", __func__);
553 }
554
555 ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
556 if (ret > 0) {
557 if (strstr(value, "OFFLINE")) {
558 event.u.status = CPE_STATUS_OFFLINE;
559 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
560 }
561 else if (strstr(value, "ONLINE")) {
562 event.u.status = CPE_STATUS_ONLINE;
563 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
564 }
565 else
566 ALOGE("%s: unknown CPE status", __func__);
567 }
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700568
569 ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
570 if (ret >= 0) {
571 event.u.value = val;
572 st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
573 }
Quinn male73dd1fd2016-12-02 10:47:11 -0800574
575 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_CONNECT, &val);
576 if ((ret >= 0) && audio_is_input_device(val)) {
577 event.u.value = val;
578 st_dev->st_callback(AUDIO_EVENT_DEVICE_CONNECT, &event);
579 }
580
581 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_DISCONNECT, &val);
582 if ((ret >= 0) && audio_is_input_device(val)) {
583 event.u.value = val;
584 st_dev->st_callback(AUDIO_EVENT_DEVICE_DISCONNECT, &event);
585 }
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530586
587 ret = str_parms_get_str(params, "SVA_EXEC_MODE", value, sizeof(value));
588 if (ret >= 0) {
589 strlcpy(event.u.str_value, value, sizeof(event.u.str_value));
590 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE, &event);
591 }
592}
593
594void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev __unused,
595 struct str_parms *query, struct str_parms *reply)
596{
597 audio_event_info_t event;
598 int ret;
599 char value[32];
600
601 ret = str_parms_get_str(query, "SVA_EXEC_MODE_STATUS", value,
602 sizeof(value));
603 if (ret >= 0) {
604 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE_STATUS, &event);
605 str_parms_add_int(reply, "SVA_EXEC_MODE_STATUS", event.u.value);
606 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700607}
608
609int audio_extn_sound_trigger_init(struct audio_device *adev)
610{
611 int status = 0;
612 char sound_trigger_lib[100];
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530613 void *sthal_prop_api_version;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700614
615 ALOGI("%s: Enter", __func__);
616
617 st_dev = (struct sound_trigger_audio_device*)
618 calloc(1, sizeof(struct sound_trigger_audio_device));
619 if (!st_dev) {
620 ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
621 return -ENOMEM;
622 }
623
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530624 get_library_path(sound_trigger_lib);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700625 st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
626
627 if (st_dev->lib_handle == NULL) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530628 ALOGE("%s: error %s", __func__, dlerror());
629 status = -ENODEV;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700630 goto cleanup;
631 }
632 ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
633
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530634 DLSYM(st_dev->lib_handle, st_dev->st_callback, sound_trigger_hw_call_back,
635 status);
636 if (status)
637 goto cleanup;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700638
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530639 DLSYM(st_dev->lib_handle, sthal_prop_api_version,
640 sthal_prop_api_version, status);
641 if (status) {
642 st_dev->sthal_prop_api_version = 0;
643 status = 0; /* passthru for backward compability */
644 } else {
645 st_dev->sthal_prop_api_version = *(int*)sthal_prop_api_version;
646 if (MAJOR_VERSION(st_dev->sthal_prop_api_version) !=
647 MAJOR_VERSION(STHAL_PROP_API_CURRENT_VERSION)) {
648 ALOGE("%s: Incompatible API versions ahal:0x%x != sthal:0x%x",
649 __func__, STHAL_PROP_API_CURRENT_VERSION,
650 st_dev->sthal_prop_api_version);
651 goto cleanup;
652 }
653 ALOGD("%s: sthal is using proprietary API version 0x%04x", __func__,
654 st_dev->sthal_prop_api_version);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700655 }
656
657 st_dev->adev = adev;
658 list_init(&st_dev->st_ses_list);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530659 audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700660
661 return 0;
662
663cleanup:
664 if (st_dev->lib_handle)
665 dlclose(st_dev->lib_handle);
666 free(st_dev);
667 st_dev = NULL;
668 return status;
669
670}
671
672void audio_extn_sound_trigger_deinit(struct audio_device *adev)
673{
674 ALOGI("%s: Enter", __func__);
675 if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530676 audio_extn_snd_mon_unregister_listener(st_dev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700677 dlclose(st_dev->lib_handle);
678 free(st_dev);
679 st_dev = NULL;
680 }
681}