blob: a38ca17f6ba9146917f0a99ec1149b11f92cb046 [file] [log] [blame]
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +05301/* Copyright (c) 2013-2014, 2016-2017 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"
42#include "sound_trigger_prop_intf.h"
43
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053044#ifdef DYNAMIC_LOG_ENABLED
45#include <log_xml_parser.h>
46#define LOG_MASK HAL_MOD_FILE_SND_TRIGGER
47#include <log_utils.h>
48#endif
49
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070050#define XSTR(x) STR(x)
51#define STR(x) #x
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053052#define MAX_LIBRARY_PATH 100
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070053
54struct sound_trigger_info {
55 struct sound_trigger_session_info st_ses;
56 bool lab_stopped;
57 struct listnode list;
58};
59
60struct sound_trigger_audio_device {
61 void *lib_handle;
62 struct audio_device *adev;
63 sound_trigger_hw_call_back_t st_callback;
64 struct listnode st_ses_list;
65 pthread_mutex_t lock;
66};
67
68static struct sound_trigger_audio_device *st_dev;
69
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053070#if LINUX_ENABLED
71static void get_library_path(char *lib_path)
72{
73 snprintf(lib_path, MAX_LIBRARY_PATH,
74 "/usr/lib/sound_trigger.primary.default.so");
75}
76#else
77static void get_library_path(char *lib_path)
78{
79 snprintf(lib_path, MAX_LIBRARY_PATH,
David Ng06ccd872017-03-15 11:39:33 -070080 "/vendor/lib/hw/sound_trigger.primary.%s.so",
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053081 XSTR(SOUND_TRIGGER_PLATFORM_NAME));
82}
83#endif
84
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070085static struct sound_trigger_info *
86get_sound_trigger_info(int capture_handle)
87{
88 struct sound_trigger_info *st_ses_info = NULL;
89 struct listnode *node;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -070090 ALOGV("%s: list empty %d capture_handle %d", __func__,
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070091 list_empty(&st_dev->st_ses_list), capture_handle);
92 list_for_each(node, &st_dev->st_ses_list) {
93 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
94 if (st_ses_info->st_ses.capture_handle == capture_handle)
95 return st_ses_info;
96 }
97 return NULL;
98}
99
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530100static void stdev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
101{
102 if (!parms)
103 return;
104
105 audio_extn_sound_trigger_set_parameters(NULL, parms);
106 return;
107}
108
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700109int audio_hw_call_back(sound_trigger_event_type_t event,
110 sound_trigger_event_info_t* config)
111{
112 int status = 0;
113 struct sound_trigger_info *st_ses_info;
114
115 if (!st_dev)
116 return -EINVAL;
117
118 pthread_mutex_lock(&st_dev->lock);
119 switch (event) {
120 case ST_EVENT_SESSION_REGISTER:
121 if (!config) {
122 ALOGE("%s: NULL config", __func__);
123 status = -EINVAL;
124 break;
125 }
126 st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
127 if (!st_ses_info) {
128 ALOGE("%s: st_ses_info alloc failed", __func__);
129 status = -ENOMEM;
130 break;
131 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700132 memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (struct sound_trigger_session_info));
133 ALOGV("%s: add capture_handle %d st session opaque ptr %p", __func__,
134 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700135 list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
136 break;
137
138 case ST_EVENT_SESSION_DEREGISTER:
139 if (!config) {
140 ALOGE("%s: NULL config", __func__);
141 status = -EINVAL;
142 break;
143 }
144 st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
145 if (!st_ses_info) {
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700146 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 -0700147 status = -EINVAL;
148 break;
149 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700150 ALOGV("%s: remove capture_handle %d st session opaque ptr %p", __func__,
151 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700152 list_remove(&st_ses_info->list);
153 free(st_ses_info);
154 break;
155 default:
156 ALOGW("%s: Unknown event %d", __func__, event);
157 break;
158 }
159 pthread_mutex_unlock(&st_dev->lock);
160 return status;
161}
162
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700163int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
164 size_t bytes)
165{
166 int ret = -1;
167 struct sound_trigger_info *st_info = NULL;
168 audio_event_info_t event;
169
170 if (!st_dev)
171 return ret;
172
173 if (!in->is_st_session_active) {
174 ALOGE(" %s: Sound trigger is not active", __func__);
175 goto exit;
176 }
177 if(in->standby)
178 in->standby = false;
179
180 pthread_mutex_lock(&st_dev->lock);
181 st_info = get_sound_trigger_info(in->capture_handle);
182 pthread_mutex_unlock(&st_dev->lock);
183 if (st_info) {
184 event.u.aud_info.ses_info = &st_info->st_ses;
185 event.u.aud_info.buf = buffer;
186 event.u.aud_info.num_bytes = bytes;
187 ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
188 }
189
190exit:
191 if (ret) {
192 if (-ENETRESET == ret)
193 in->is_st_session_active = false;
194 memset(buffer, 0, bytes);
195 ALOGV("%s: read failed status %d - sleep", __func__, ret);
196 usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
197 in->config.rate));
198 }
199 return ret;
200}
201
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700202void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
203{
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700204 struct sound_trigger_info *st_ses_info = NULL;
205 audio_event_info_t event;
206
Mingming Yinfd7607b2016-01-22 12:48:44 -0800207 if (!st_dev || !in || !in->is_st_session_active)
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700208 return;
209
210 pthread_mutex_lock(&st_dev->lock);
211 st_ses_info = get_sound_trigger_info(in->capture_handle);
Bharath Ramachandramurthy5baa6a52014-10-21 11:18:49 -0700212 pthread_mutex_unlock(&st_dev->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700213 if (st_ses_info) {
214 event.u.ses_info = st_ses_info->st_ses;
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700215 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 -0700216 st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
Mingming Yinfd7607b2016-01-22 12:48:44 -0800217 in->is_st_session_active = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700218 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700219}
220void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
221{
222 struct sound_trigger_info *st_ses_info = NULL;
223 struct listnode *node;
224
225 if (!st_dev || !in)
226 return;
227
228 pthread_mutex_lock(&st_dev->lock);
229 in->is_st_session = false;
230 ALOGV("%s: list %d capture_handle %d", __func__,
231 list_empty(&st_dev->st_ses_list), in->capture_handle);
232 list_for_each(node, &st_dev->st_ses_list) {
233 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
234 if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700235 in->config = st_ses_info->st_ses.config;
236 in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
237 in->is_st_session = true;
Bharath Ramachandramurthy837535b2015-02-05 14:27:59 -0800238 in->is_st_session_active = true;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700239 ALOGD("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
240 break;
241 }
242 }
243 pthread_mutex_unlock(&st_dev->lock);
244}
245
246void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
247 st_event_type_t event)
248{
249 bool raise_event = false;
250 int device_type = -1;
251
252 if (!st_dev)
253 return;
254
255 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
256 snd_device < SND_DEVICE_OUT_END)
257 device_type = PCM_PLAYBACK;
258 else if (snd_device >= SND_DEVICE_IN_BEGIN &&
259 snd_device < SND_DEVICE_IN_END)
260 device_type = PCM_CAPTURE;
261 else {
262 ALOGE("%s: invalid device 0x%x, for event %d",
263 __func__, snd_device, event);
264 return;
265 }
266
267 raise_event = platform_sound_trigger_device_needs_event(snd_device);
268 ALOGI("%s: device 0x%x of type %d for Event %d, with Raise=%d",
269 __func__, snd_device, device_type, event, raise_event);
270 if (raise_event && (device_type == PCM_CAPTURE)) {
271 switch(event) {
272 case ST_EVENT_SND_DEVICE_FREE:
273 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
274 break;
275 case ST_EVENT_SND_DEVICE_BUSY:
276 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
277 break;
278 default:
279 ALOGW("%s:invalid event %d for device 0x%x",
280 __func__, event, snd_device);
281 }
282 }/*Events for output device, if required can be placed here in else*/
283}
284
285void audio_extn_sound_trigger_update_stream_status(struct audio_usecase *uc_info,
286 st_event_type_t event)
287{
288 bool raise_event = false;
289 audio_usecase_t uc_id;
290 int usecase_type = -1;
291
292 if (!st_dev) {
293 return;
294 }
295
296 if (uc_info == NULL) {
297 ALOGE("%s: usecase is NULL!!!", __func__);
298 return;
299 }
300 uc_id = uc_info->id;
301 usecase_type = uc_info->type;
302
303 raise_event = platform_sound_trigger_usecase_needs_event(uc_id);
304 ALOGD("%s: uc_id %d of type %d for Event %d, with Raise=%d",
305 __func__, uc_id, usecase_type, event, raise_event);
306 if (raise_event && (usecase_type == PCM_PLAYBACK)) {
307 switch(event) {
308 case ST_EVENT_STREAM_FREE:
309 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE, NULL);
310 break;
311 case ST_EVENT_STREAM_BUSY:
312 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE, NULL);
313 break;
314 default:
315 ALOGW("%s:invalid event %d, for usecase %d",
316 __func__, event, uc_id);
317 }
318 }/*Events for capture usecase, if required can be placed here in else*/
319}
320
321void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
322 struct str_parms *params)
323{
324 audio_event_info_t event;
325 char value[32];
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700326 int ret, val;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700327
328 if(!st_dev || !params) {
329 ALOGE("%s: str_params NULL", __func__);
330 return;
331 }
332
333 ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
334 sizeof(value));
335 if (ret > 0) {
336 if (strstr(value, "OFFLINE")) {
337 event.u.status = SND_CARD_STATUS_OFFLINE;
338 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
339 }
340 else if (strstr(value, "ONLINE")) {
341 event.u.status = SND_CARD_STATUS_ONLINE;
342 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
343 }
344 else
345 ALOGE("%s: unknown snd_card_status", __func__);
346 }
347
348 ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
349 if (ret > 0) {
350 if (strstr(value, "OFFLINE")) {
351 event.u.status = CPE_STATUS_OFFLINE;
352 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
353 }
354 else if (strstr(value, "ONLINE")) {
355 event.u.status = CPE_STATUS_ONLINE;
356 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
357 }
358 else
359 ALOGE("%s: unknown CPE status", __func__);
360 }
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700361
362 ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
363 if (ret >= 0) {
364 event.u.value = val;
365 st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
366 }
Quinn male73dd1fd2016-12-02 10:47:11 -0800367
368 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_CONNECT, &val);
369 if ((ret >= 0) && audio_is_input_device(val)) {
370 event.u.value = val;
371 st_dev->st_callback(AUDIO_EVENT_DEVICE_CONNECT, &event);
372 }
373
374 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_DISCONNECT, &val);
375 if ((ret >= 0) && audio_is_input_device(val)) {
376 event.u.value = val;
377 st_dev->st_callback(AUDIO_EVENT_DEVICE_DISCONNECT, &event);
378 }
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530379
380 ret = str_parms_get_str(params, "SVA_EXEC_MODE", value, sizeof(value));
381 if (ret >= 0) {
382 strlcpy(event.u.str_value, value, sizeof(event.u.str_value));
383 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE, &event);
384 }
385}
386
387void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev __unused,
388 struct str_parms *query, struct str_parms *reply)
389{
390 audio_event_info_t event;
391 int ret;
392 char value[32];
393
394 ret = str_parms_get_str(query, "SVA_EXEC_MODE_STATUS", value,
395 sizeof(value));
396 if (ret >= 0) {
397 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE_STATUS, &event);
398 str_parms_add_int(reply, "SVA_EXEC_MODE_STATUS", event.u.value);
399 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700400}
401
402int audio_extn_sound_trigger_init(struct audio_device *adev)
403{
404 int status = 0;
405 char sound_trigger_lib[100];
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700406
407 ALOGI("%s: Enter", __func__);
408
409 st_dev = (struct sound_trigger_audio_device*)
410 calloc(1, sizeof(struct sound_trigger_audio_device));
411 if (!st_dev) {
412 ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
413 return -ENOMEM;
414 }
415
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530416 get_library_path(sound_trigger_lib);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700417 st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
418
419 if (st_dev->lib_handle == NULL) {
420 ALOGE("%s: DLOPEN failed for %s. error = %s", __func__, sound_trigger_lib,
421 dlerror());
422 status = -EINVAL;
423 goto cleanup;
424 }
425 ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
426
427 st_dev->st_callback = (sound_trigger_hw_call_back_t)
428 dlsym(st_dev->lib_handle, "sound_trigger_hw_call_back");
429
430 if (st_dev->st_callback == NULL) {
431 ALOGE("%s: ERROR. dlsym Error:%s sound_trigger_hw_call_back", __func__,
432 dlerror());
433 goto cleanup;
434 }
435
436 st_dev->adev = adev;
437 list_init(&st_dev->st_ses_list);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530438 audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700439
440 return 0;
441
442cleanup:
443 if (st_dev->lib_handle)
444 dlclose(st_dev->lib_handle);
445 free(st_dev);
446 st_dev = NULL;
447 return status;
448
449}
450
451void audio_extn_sound_trigger_deinit(struct audio_device *adev)
452{
453 ALOGI("%s: Enter", __func__);
454 if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530455 audio_extn_snd_mon_unregister_listener(st_dev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700456 dlclose(st_dev->lib_handle);
457 free(st_dev);
458 st_dev = NULL;
459 }
460}