blob: 94a8a2be3f983c0cad00648b400691d1b090033b [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
100int audio_hw_call_back(sound_trigger_event_type_t event,
101 sound_trigger_event_info_t* config)
102{
103 int status = 0;
104 struct sound_trigger_info *st_ses_info;
105
106 if (!st_dev)
107 return -EINVAL;
108
109 pthread_mutex_lock(&st_dev->lock);
110 switch (event) {
111 case ST_EVENT_SESSION_REGISTER:
112 if (!config) {
113 ALOGE("%s: NULL config", __func__);
114 status = -EINVAL;
115 break;
116 }
117 st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
118 if (!st_ses_info) {
119 ALOGE("%s: st_ses_info alloc failed", __func__);
120 status = -ENOMEM;
121 break;
122 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700123 memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (struct sound_trigger_session_info));
124 ALOGV("%s: add capture_handle %d st session opaque ptr %p", __func__,
125 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700126 list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
127 break;
128
129 case ST_EVENT_SESSION_DEREGISTER:
130 if (!config) {
131 ALOGE("%s: NULL config", __func__);
132 status = -EINVAL;
133 break;
134 }
135 st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
136 if (!st_ses_info) {
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700137 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 -0700138 status = -EINVAL;
139 break;
140 }
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700141 ALOGV("%s: remove capture_handle %d st session opaque ptr %p", __func__,
142 st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.p_ses);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700143 list_remove(&st_ses_info->list);
144 free(st_ses_info);
145 break;
146 default:
147 ALOGW("%s: Unknown event %d", __func__, event);
148 break;
149 }
150 pthread_mutex_unlock(&st_dev->lock);
151 return status;
152}
153
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -0700154int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
155 size_t bytes)
156{
157 int ret = -1;
158 struct sound_trigger_info *st_info = NULL;
159 audio_event_info_t event;
160
161 if (!st_dev)
162 return ret;
163
164 if (!in->is_st_session_active) {
165 ALOGE(" %s: Sound trigger is not active", __func__);
166 goto exit;
167 }
168 if(in->standby)
169 in->standby = false;
170
171 pthread_mutex_lock(&st_dev->lock);
172 st_info = get_sound_trigger_info(in->capture_handle);
173 pthread_mutex_unlock(&st_dev->lock);
174 if (st_info) {
175 event.u.aud_info.ses_info = &st_info->st_ses;
176 event.u.aud_info.buf = buffer;
177 event.u.aud_info.num_bytes = bytes;
178 ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
179 }
180
181exit:
182 if (ret) {
183 if (-ENETRESET == ret)
184 in->is_st_session_active = false;
185 memset(buffer, 0, bytes);
186 ALOGV("%s: read failed status %d - sleep", __func__, ret);
187 usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
188 in->config.rate));
189 }
190 return ret;
191}
192
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700193void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
194{
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700195 struct sound_trigger_info *st_ses_info = NULL;
196 audio_event_info_t event;
197
Mingming Yinfd7607b2016-01-22 12:48:44 -0800198 if (!st_dev || !in || !in->is_st_session_active)
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700199 return;
200
201 pthread_mutex_lock(&st_dev->lock);
202 st_ses_info = get_sound_trigger_info(in->capture_handle);
Bharath Ramachandramurthy5baa6a52014-10-21 11:18:49 -0700203 pthread_mutex_unlock(&st_dev->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700204 if (st_ses_info) {
205 event.u.ses_info = st_ses_info->st_ses;
Shiv Maliyappanahalli0e283d32016-07-14 23:20:03 -0700206 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 -0700207 st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
Mingming Yinfd7607b2016-01-22 12:48:44 -0800208 in->is_st_session_active = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700209 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700210}
211void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
212{
213 struct sound_trigger_info *st_ses_info = NULL;
214 struct listnode *node;
215
216 if (!st_dev || !in)
217 return;
218
219 pthread_mutex_lock(&st_dev->lock);
220 in->is_st_session = false;
221 ALOGV("%s: list %d capture_handle %d", __func__,
222 list_empty(&st_dev->st_ses_list), in->capture_handle);
223 list_for_each(node, &st_dev->st_ses_list) {
224 st_ses_info = node_to_item(node, struct sound_trigger_info , list);
225 if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700226 in->config = st_ses_info->st_ses.config;
227 in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
228 in->is_st_session = true;
Bharath Ramachandramurthy837535b2015-02-05 14:27:59 -0800229 in->is_st_session_active = true;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700230 ALOGD("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
231 break;
232 }
233 }
234 pthread_mutex_unlock(&st_dev->lock);
235}
236
237void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
238 st_event_type_t event)
239{
240 bool raise_event = false;
241 int device_type = -1;
242
243 if (!st_dev)
244 return;
245
246 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
247 snd_device < SND_DEVICE_OUT_END)
248 device_type = PCM_PLAYBACK;
249 else if (snd_device >= SND_DEVICE_IN_BEGIN &&
250 snd_device < SND_DEVICE_IN_END)
251 device_type = PCM_CAPTURE;
252 else {
253 ALOGE("%s: invalid device 0x%x, for event %d",
254 __func__, snd_device, event);
255 return;
256 }
257
258 raise_event = platform_sound_trigger_device_needs_event(snd_device);
259 ALOGI("%s: device 0x%x of type %d for Event %d, with Raise=%d",
260 __func__, snd_device, device_type, event, raise_event);
261 if (raise_event && (device_type == PCM_CAPTURE)) {
262 switch(event) {
263 case ST_EVENT_SND_DEVICE_FREE:
264 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
265 break;
266 case ST_EVENT_SND_DEVICE_BUSY:
267 st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
268 break;
269 default:
270 ALOGW("%s:invalid event %d for device 0x%x",
271 __func__, event, snd_device);
272 }
273 }/*Events for output device, if required can be placed here in else*/
274}
275
276void audio_extn_sound_trigger_update_stream_status(struct audio_usecase *uc_info,
277 st_event_type_t event)
278{
279 bool raise_event = false;
280 audio_usecase_t uc_id;
281 int usecase_type = -1;
282
283 if (!st_dev) {
284 return;
285 }
286
287 if (uc_info == NULL) {
288 ALOGE("%s: usecase is NULL!!!", __func__);
289 return;
290 }
291 uc_id = uc_info->id;
292 usecase_type = uc_info->type;
293
294 raise_event = platform_sound_trigger_usecase_needs_event(uc_id);
295 ALOGD("%s: uc_id %d of type %d for Event %d, with Raise=%d",
296 __func__, uc_id, usecase_type, event, raise_event);
297 if (raise_event && (usecase_type == PCM_PLAYBACK)) {
298 switch(event) {
299 case ST_EVENT_STREAM_FREE:
300 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE, NULL);
301 break;
302 case ST_EVENT_STREAM_BUSY:
303 st_dev->st_callback(AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE, NULL);
304 break;
305 default:
306 ALOGW("%s:invalid event %d, for usecase %d",
307 __func__, event, uc_id);
308 }
309 }/*Events for capture usecase, if required can be placed here in else*/
310}
311
312void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
313 struct str_parms *params)
314{
315 audio_event_info_t event;
316 char value[32];
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700317 int ret, val;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700318
319 if(!st_dev || !params) {
320 ALOGE("%s: str_params NULL", __func__);
321 return;
322 }
323
324 ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
325 sizeof(value));
326 if (ret > 0) {
327 if (strstr(value, "OFFLINE")) {
328 event.u.status = SND_CARD_STATUS_OFFLINE;
329 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
330 }
331 else if (strstr(value, "ONLINE")) {
332 event.u.status = SND_CARD_STATUS_ONLINE;
333 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
334 }
335 else
336 ALOGE("%s: unknown snd_card_status", __func__);
337 }
338
339 ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
340 if (ret > 0) {
341 if (strstr(value, "OFFLINE")) {
342 event.u.status = CPE_STATUS_OFFLINE;
343 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
344 }
345 else if (strstr(value, "ONLINE")) {
346 event.u.status = CPE_STATUS_ONLINE;
347 st_dev->st_callback(AUDIO_EVENT_SSR, &event);
348 }
349 else
350 ALOGE("%s: unknown CPE status", __func__);
351 }
Bharath Ramachandramurthyc694f8a2014-09-25 16:15:12 -0700352
353 ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
354 if (ret >= 0) {
355 event.u.value = val;
356 st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
357 }
Quinn male73dd1fd2016-12-02 10:47:11 -0800358
359 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_CONNECT, &val);
360 if ((ret >= 0) && audio_is_input_device(val)) {
361 event.u.value = val;
362 st_dev->st_callback(AUDIO_EVENT_DEVICE_CONNECT, &event);
363 }
364
365 ret = str_parms_get_int(params, AUDIO_PARAMETER_DEVICE_DISCONNECT, &val);
366 if ((ret >= 0) && audio_is_input_device(val)) {
367 event.u.value = val;
368 st_dev->st_callback(AUDIO_EVENT_DEVICE_DISCONNECT, &event);
369 }
Chaithanya Krishna Bacharajue3d711e2016-12-08 16:17:32 +0530370
371 ret = str_parms_get_str(params, "SVA_EXEC_MODE", value, sizeof(value));
372 if (ret >= 0) {
373 strlcpy(event.u.str_value, value, sizeof(event.u.str_value));
374 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE, &event);
375 }
376}
377
378void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev __unused,
379 struct str_parms *query, struct str_parms *reply)
380{
381 audio_event_info_t event;
382 int ret;
383 char value[32];
384
385 ret = str_parms_get_str(query, "SVA_EXEC_MODE_STATUS", value,
386 sizeof(value));
387 if (ret >= 0) {
388 st_dev->st_callback(AUDIO_EVENT_SVA_EXEC_MODE_STATUS, &event);
389 str_parms_add_int(reply, "SVA_EXEC_MODE_STATUS", event.u.value);
390 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700391}
392
393int audio_extn_sound_trigger_init(struct audio_device *adev)
394{
395 int status = 0;
396 char sound_trigger_lib[100];
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700397
398 ALOGI("%s: Enter", __func__);
399
400 st_dev = (struct sound_trigger_audio_device*)
401 calloc(1, sizeof(struct sound_trigger_audio_device));
402 if (!st_dev) {
403 ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
404 return -ENOMEM;
405 }
406
Yamit Mehtaa0d653a2016-11-25 20:33:25 +0530407 get_library_path(sound_trigger_lib);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700408 st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
409
410 if (st_dev->lib_handle == NULL) {
411 ALOGE("%s: DLOPEN failed for %s. error = %s", __func__, sound_trigger_lib,
412 dlerror());
413 status = -EINVAL;
414 goto cleanup;
415 }
416 ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
417
418 st_dev->st_callback = (sound_trigger_hw_call_back_t)
419 dlsym(st_dev->lib_handle, "sound_trigger_hw_call_back");
420
421 if (st_dev->st_callback == NULL) {
422 ALOGE("%s: ERROR. dlsym Error:%s sound_trigger_hw_call_back", __func__,
423 dlerror());
424 goto cleanup;
425 }
426
427 st_dev->adev = adev;
428 list_init(&st_dev->st_ses_list);
429
430 return 0;
431
432cleanup:
433 if (st_dev->lib_handle)
434 dlclose(st_dev->lib_handle);
435 free(st_dev);
436 st_dev = NULL;
437 return status;
438
439}
440
441void audio_extn_sound_trigger_deinit(struct audio_device *adev)
442{
443 ALOGI("%s: Enter", __func__);
444 if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
445 dlclose(st_dev->lib_handle);
446 free(st_dev);
447 st_dev = NULL;
448 }
449}