blob: 95907f369b6386a556410a9e91312b54e7e9711f [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,
Zhou Songc1088ea2018-06-12 00:17:29 +080083 AUDIO_EVENT_BATTERY_STATUS_CHANGED,
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +053084 AUDIO_EVENT_GET_PARAM
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +053085} audio_event_type_t;
86
87typedef enum {
88 USECASE_TYPE_PCM_PLAYBACK,
89 USECASE_TYPE_PCM_CAPTURE,
90 USECASE_TYPE_VOICE_CALL,
91 USECASE_TYPE_VOIP_CALL,
92} audio_stream_usecase_type_t;
93
94typedef enum {
95 SND_CARD_STATUS_OFFLINE,
96 SND_CARD_STATUS_ONLINE,
97 CPE_STATUS_OFFLINE,
98 CPE_STATUS_ONLINE
99} ssr_event_status_t;
100
101struct sound_trigger_session_info {
102 void* p_ses; /* opaque pointer to st_session obj */
103 int capture_handle;
104 struct pcm *pcm;
105 struct pcm_config config;
106};
107
108struct audio_read_samples_info {
109 struct sound_trigger_session_info *ses_info;
110 void *buf;
111 size_t num_bytes;
112};
113
114struct audio_hal_usecase {
115 audio_stream_usecase_type_t type;
116};
117
118struct sound_trigger_event_info {
119 struct sound_trigger_session_info st_ses;
120};
121typedef struct sound_trigger_event_info sound_trigger_event_info_t;
122
123struct sound_trigger_device_info {
124 int device;
125};
126
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530127struct sound_trigger_get_param_data {
128 char *param;
129 int sm_handle;
130 struct str_parms *reply;
131};
132
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530133struct audio_event_info {
134 union {
135 ssr_event_status_t status;
136 int value;
137 struct sound_trigger_session_info ses_info;
138 struct audio_read_samples_info aud_info;
139 char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
140 struct audio_hal_usecase usecase;
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530141 struct sound_trigger_get_param_data st_get_param_data;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530142 } u;
143 struct sound_trigger_device_info device_info;
144};
145typedef struct audio_event_info audio_event_info_t;
146/* STHAL callback which is called by AHAL */
147typedef int (*sound_trigger_hw_call_back_t)(audio_event_type_t,
148 struct audio_event_info*);
149
150/*---------------- End: AHAL-STHAL Interface ----------------------------------*/
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700151
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +0530152#ifdef DYNAMIC_LOG_ENABLED
153#include <log_xml_parser.h>
154#define LOG_MASK HAL_MOD_FILE_SND_TRIGGER
155#include <log_utils.h>
156#endif
157
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700158#define XSTR(x) STR(x)
159#define STR(x) #x
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530160#define MAX_LIBRARY_PATH 100
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700161
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530162#define DLSYM(handle, ptr, symbol, err) \
163do {\
Venkatesh Mangalappalibcc684c2018-04-06 14:58:44 -0700164 ptr = dlsym(handle, #symbol); \
165 if (ptr == NULL) {\
166 ALOGW("%s: %s not found. %s", __func__, #symbol, dlerror());\
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530167 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 Siddani24e9ad52018-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) {
Zhou Songc1088ea2018-06-12 00:17:29 +0800514 if (uc_info->stream.out)
515 ev_info.device_info.device = uc_info->stream.out->devices;
516 else
517 ev_info.device_info.device = AUDIO_DEVICE_OUT_SPEAKER;
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530518 switch(event) {
519 case ST_EVENT_STREAM_FREE:
Zhou Songc1088ea2018-06-12 00:17:29 +0800520 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE, &ev_info);
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530521 break;
522 case ST_EVENT_STREAM_BUSY:
Zhou Songc1088ea2018-06-12 00:17:29 +0800523 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE, &ev_info);
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530524 break;
525 default:
526 ALOGW("%s:invalid event %d, for usecase %d",
527 __func__, event, uc_info->id);
528 }
Revathi Uddaraju9f99ddd2018-05-03 12:01:38 +0530529 } else if ((uc_info->type == PCM_CAPTURE) || (uc_info->type == VOICE_CALL)) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530530 if (event == ST_EVENT_STREAM_BUSY)
531 ev = AUDIO_EVENT_CAPTURE_STREAM_ACTIVE;
532 else
533 ev = AUDIO_EVENT_CAPTURE_STREAM_INACTIVE;
534 if (!populate_usecase(&ev_info.u.usecase, uc_info)) {
535 ALOGD("%s: send event %d: usecase id %d, type %d",
536 __func__, ev, uc_info->id, uc_info->type);
537 st_dev->st_callback(ev, &ev_info);
538 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700539 }
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530540 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700541}
542
Zhou Songc1088ea2018-06-12 00:17:29 +0800543void audio_extn_sound_trigger_update_battery_status(bool charging)
544{
545 struct audio_event_info ev_info;
546
547 if (!st_dev || st_dev->sthal_prop_api_version < STHAL_PROP_API_VERSION_1_0)
548 return;
549
550 ev_info.u.value = charging;
551 st_dev->st_callback(AUDIO_EVENT_BATTERY_STATUS_CHANGED, &ev_info);
552}
553
554
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700555void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
556 struct str_parms *params)
557{
558 audio_event_info_t event;
559 char value[32];
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700560 int ret, val;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700561
562 if(!st_dev || !params) {
563 ALOGE("%s: str_params NULL", __func__);
564 return;
565 }
566
567 ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
568 sizeof(value));
569 if (ret > 0) {
570 if (strstr(value, "OFFLINE")) {
571 event.u.status = SND_CARD_STATUS_OFFLINE;
572 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
573 }
574 else if (strstr(value, "ONLINE")) {
575 event.u.status = SND_CARD_STATUS_ONLINE;
576 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
577 }
578 else
579 ALOGE("%s: unknown snd_card_status", __func__);
580 }
581
582 ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
583 if (ret > 0) {
584 if (strstr(value, "OFFLINE")) {
585 event.u.status = CPE_STATUS_OFFLINE;
586 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
587 }
588 else if (strstr(value, "ONLINE")) {
589 event.u.status = CPE_STATUS_ONLINE;
590 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
591 }
592 else
593 ALOGE("%s: unknown CPE status", __func__);
594 }
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700595
596 ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
597 if (ret >= 0) {
598 event.u.value = val;
599 st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
600 }
Quinn male73dd1fd2016-12-02 10:47:11 -0800601
602 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_CONNECT, &val);
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +0530603 if ((ret >= 0) && (audio_is_input_device(val) ||
604 (val == AUDIO_DEVICE_OUT_LINE))) {
Quinn male73dd1fd2016-12-02 10:47:11 -0800605 event.u.value = val;
606 st_dev->st_callback(AUDIO_EVENT_DEVICE_CONNECT, &event);
607 }
608
609 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_DISCONNECT, &val);
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +0530610 if ((ret >= 0) && (audio_is_input_device(val) ||
611 (val == AUDIO_DEVICE_OUT_LINE))) {
Quinn male73dd1fd2016-12-02 10:47:11 -0800612 event.u.value = val;
613 st_dev->st_callback(AUDIO_EVENT_DEVICE_DISCONNECT, &event);
614 }
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530615
616 ret = str_parms_get_str(params, "SVA_EXEC_MODE", value, sizeof(value));
617 if (ret >= 0) {
618 strlcpy(event.u.str_value, value, sizeof(event.u.str_value));
619 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE, &event);
620 }
621}
622
623void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev __unused,
624 struct str_parms *query, struct str_parms *reply)
625{
626 audio_event_info_t event;
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530627 int ret, val;
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530628 char value[32];
629
630 ret = str_parms_get_str(query, "SVA_EXEC_MODE_STATUS", value,
631 sizeof(value));
632 if (ret >= 0) {
633 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE_STATUS, &event);
634 str_parms_add_int(reply, "SVA_EXEC_MODE_STATUS", event.u.value);
635 }
Dhanalakshmi Siddani57b014e2018-04-23 12:52:02 +0530636
637 ret = str_parms_get_int(query, SVA_PARAM_DIRECTION_OF_ARRIVAL, &val);
638 if (ret >= 0) {
639 event.u.st_get_param_data.sm_handle = val;
640 event.u.st_get_param_data.param = SVA_PARAM_DIRECTION_OF_ARRIVAL;
641 event.u.st_get_param_data.reply = reply;
642 st_dev->st_callback(AUDIO_EVENT_GET_PARAM, &event);
643 }
644
645 ret = str_parms_get_int(query, SVA_PARAM_CHANNEL_INDEX, &val);
646 if (ret >= 0) {
647 event.u.st_get_param_data.sm_handle = val;
648 event.u.st_get_param_data.param = SVA_PARAM_CHANNEL_INDEX;
649 event.u.st_get_param_data.reply = reply;
650 st_dev->st_callback(AUDIO_EVENT_GET_PARAM, &event);
651 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700652}
653
654int audio_extn_sound_trigger_init(struct audio_device *adev)
655{
656 int status = 0;
657 char sound_trigger_lib[100];
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530658 void *sthal_prop_api_version;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700659
660 ALOGI("%s: Enter", __func__);
661
662 st_dev = (struct sound_trigger_audio_device*)
663 calloc(1, sizeof(struct sound_trigger_audio_device));
664 if (!st_dev) {
665 ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
666 return -ENOMEM;
667 }
668
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530669 get_library_path(sound_trigger_lib);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700670 st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
671
672 if (st_dev->lib_handle == NULL) {
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530673 ALOGE("%s: error %s", __func__, dlerror());
674 status = -ENODEV;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700675 goto cleanup;
676 }
677 ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
678
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530679 DLSYM(st_dev->lib_handle, st_dev->st_callback, sound_trigger_hw_call_back,
680 status);
681 if (status)
682 goto cleanup;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700683
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530684 DLSYM(st_dev->lib_handle, sthal_prop_api_version,
685 sthal_prop_api_version, status);
686 if (status) {
687 st_dev->sthal_prop_api_version = 0;
688 status = 0; /* passthru for backward compability */
689 } else {
Jhansi Konathala902fc412018-09-05 13:21:44 +0530690 if (sthal_prop_api_version != NULL) {
691 st_dev->sthal_prop_api_version = *(int*)sthal_prop_api_version;
692 }
Dhanalakshmi Siddanif133cc52018-02-08 14:34:51 +0530693 if (MAJOR_VERSION(st_dev->sthal_prop_api_version) !=
694 MAJOR_VERSION(STHAL_PROP_API_CURRENT_VERSION)) {
695 ALOGE("%s: Incompatible API versions ahal:0x%x != sthal:0x%x",
696 __func__, STHAL_PROP_API_CURRENT_VERSION,
697 st_dev->sthal_prop_api_version);
698 goto cleanup;
699 }
700 ALOGD("%s: sthal is using proprietary API version 0x%04x", __func__,
701 st_dev->sthal_prop_api_version);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700702 }
703
704 st_dev->adev = adev;
705 list_init(&st_dev->st_ses_list);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530706 audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700707
708 return 0;
709
710cleanup:
711 if (st_dev->lib_handle)
712 dlclose(st_dev->lib_handle);
713 free(st_dev);
714 st_dev = NULL;
715 return status;
716
717}
718
719void audio_extn_sound_trigger_deinit(struct audio_device *adev)
720{
721 ALOGI("%s: Enter", __func__);
722 if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530723 audio_extn_snd_mon_unregister_listener(st_dev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700724 dlclose(st_dev->lib_handle);
725 free(st_dev);
726 st_dev = NULL;
727 }
728}