1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define LOG_TAG "soundtrigger"
17 /* #define LOG_NDEBUG 0 */
18 #define LOG_NDDEBUG 0
19 
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <dlfcn.h>
24 #include <pthread.h>
25 #include <unistd.h>
26 #include <log/log.h>
27 #include "audio_hw.h"
28 #include "audio_extn.h"
29 #include "platform.h"
30 #include "platform_api.h"
31 
32 /*-------------------- Begin: AHAL-STHAL Interface ---------------------------*/
33 /*
34  * Maintain the proprietary interface between AHAL and STHAL locally to avoid
35  * the compilation dependency of interface header file from STHAL.
36  */
37 
38 #define MAKE_HAL_VERSION(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
39 #define MAJOR_VERSION(ver) (((ver) & 0xff00) >> 8)
40 #define MINOR_VERSION(ver) ((ver) & 0x00ff)
41 
42 /* Proprietary interface version used for compatibility with STHAL */
43 #define STHAL_PROP_API_VERSION_1_0 MAKE_HAL_VERSION(1, 0)
44 #define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_1_0
45 
46 #define ST_EVENT_CONFIG_MAX_STR_VALUE 32
47 
48 typedef enum {
49     ST_EVENT_SESSION_REGISTER,
50     ST_EVENT_SESSION_DEREGISTER
51 } sound_trigger_event_type_t;
52 
53 typedef enum {
54     AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE,
55     AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE,
56     AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE,
57     AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE,
58     AUDIO_EVENT_STOP_LAB,
59     AUDIO_EVENT_SSR,
60     AUDIO_EVENT_NUM_ST_SESSIONS,
61     AUDIO_EVENT_READ_SAMPLES,
62     AUDIO_EVENT_DEVICE_CONNECT,
63     AUDIO_EVENT_DEVICE_DISCONNECT,
64     AUDIO_EVENT_SVA_EXEC_MODE,
65     AUDIO_EVENT_SVA_EXEC_MODE_STATUS,
66     AUDIO_EVENT_CAPTURE_STREAM_INACTIVE,
67     AUDIO_EVENT_CAPTURE_STREAM_ACTIVE,
68 } audio_event_type_t;
69 
70 typedef enum {
71     USECASE_TYPE_PCM_PLAYBACK,
72     USECASE_TYPE_PCM_CAPTURE,
73     USECASE_TYPE_VOICE_CALL,
74     USECASE_TYPE_VOIP_CALL,
75 } audio_stream_usecase_type_t;
76 
77 typedef enum {
78     SND_CARD_STATUS_OFFLINE,
79     SND_CARD_STATUS_ONLINE,
80     CPE_STATUS_OFFLINE,
81     CPE_STATUS_ONLINE
82 } ssr_event_status_t;
83 
84 struct sound_trigger_session_info {
85     void* p_ses; /* opaque pointer to st_session obj */
86     int capture_handle;
87     struct pcm *pcm;
88     struct pcm_config config;
89 };
90 
91 struct audio_read_samples_info {
92     struct sound_trigger_session_info *ses_info;
93     void *buf;
94     size_t num_bytes;
95 };
96 
97 struct audio_hal_usecase {
98     audio_stream_usecase_type_t type;
99 };
100 
101 struct sound_trigger_event_info {
102     struct sound_trigger_session_info st_ses;
103 };
104 
105 struct audio_event_info {
106     union {
107         ssr_event_status_t status;
108         int value;
109         struct sound_trigger_session_info ses_info;
110         struct audio_read_samples_info aud_info;
111         char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
112         struct audio_hal_usecase usecase;
113     }u;
114 };
115 
116 /* STHAL callback which is called by AHAL */
117 typedef int (*sound_trigger_hw_call_back_t)(audio_event_type_t,
118                                   struct audio_event_info*);
119 
120 /*---------------- End: AHAL-STHAL Interface ----------------------------------*/
121 
122 #define XSTR(x) STR(x)
123 #define STR(x) #x
124 
125 #define DLSYM(handle, ptr, symbol, err)                                   \
126 do {                                                                      \
127     ptr = dlsym(handle, #symbol);                                         \
128     if (ptr == NULL) {                                                    \
129         ALOGE("%s: dlsym %s failed . %s", __func__, #symbol, dlerror());  \
130         err = -ENODEV;                                                    \
131     }                                                                     \
132 } while(0)
133 
134 #ifdef __LP64__
135 #define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib64/hw/sound_trigger.primary.%s.so"
136 #else
137 #define SOUND_TRIGGER_LIBRARY_PATH "/vendor/lib/hw/sound_trigger.primary.%s.so"
138 #endif
139 
140 /*
141  * Current proprietary API version used by AHAL. Queried by STHAL
142  * for compatibility check with AHAL
143  */
144 const unsigned int sthal_prop_api_version = STHAL_PROP_API_CURRENT_VERSION;
145 
146 struct sound_trigger_info  {
147     struct sound_trigger_session_info st_ses;
148     bool lab_stopped;
149     struct listnode list;
150 };
151 
152 struct sound_trigger_audio_device {
153     void *lib_handle;
154     struct audio_device *adev;
155     sound_trigger_hw_call_back_t st_callback;
156     struct listnode st_ses_list;
157     pthread_mutex_t lock;
158     unsigned int sthal_prop_api_version;
159 };
160 
161 static struct sound_trigger_audio_device *st_dev;
162 
163 static struct sound_trigger_info *
get_sound_trigger_info(int capture_handle)164 get_sound_trigger_info(int capture_handle)
165 {
166     struct sound_trigger_info  *st_ses_info = NULL;
167     struct listnode *node;
168     ALOGV("%s: list empty %d capture_handle %d", __func__,
169            list_empty(&st_dev->st_ses_list), capture_handle);
170     list_for_each(node, &st_dev->st_ses_list) {
171         st_ses_info = node_to_item(node, struct sound_trigger_info , list);
172         if (st_ses_info->st_ses.capture_handle == capture_handle)
173             return st_ses_info;
174     }
175     return NULL;
176 }
177 
populate_usecase(struct audio_hal_usecase * usecase,struct audio_usecase * uc_info)178 static int populate_usecase(struct audio_hal_usecase *usecase,
179                        struct audio_usecase *uc_info)
180 {
181     int status  = 0;
182 
183     switch (uc_info->type) {
184     case PCM_PLAYBACK:
185         if (uc_info->id == USECASE_AUDIO_PLAYBACK_VOIP)
186             usecase->type = USECASE_TYPE_VOIP_CALL;
187         else
188             usecase->type = USECASE_TYPE_PCM_PLAYBACK;
189         break;
190 
191     case PCM_CAPTURE:
192         if (uc_info->id == USECASE_AUDIO_RECORD_VOIP)
193             usecase->type = USECASE_TYPE_VOIP_CALL;
194         else
195             usecase->type = USECASE_TYPE_PCM_CAPTURE;
196         break;
197 
198     case VOICE_CALL:
199         usecase->type = USECASE_TYPE_VOICE_CALL;
200         break;
201 
202     default:
203         ALOGE("%s: unsupported usecase type %d", __func__, uc_info->type);
204         status = -EINVAL;
205     }
206     return status;
207 }
208 
stdev_snd_mon_cb(void * stream __unused,struct str_parms * parms)209 static void stdev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
210 {
211     if (!parms)
212         return;
213 
214     audio_extn_sound_trigger_set_parameters(NULL, parms);
215     return;
216 }
217 
audio_hw_call_back(sound_trigger_event_type_t event,struct sound_trigger_event_info * config)218 int audio_hw_call_back(sound_trigger_event_type_t event,
219                        struct sound_trigger_event_info* config)
220 {
221     int status = 0;
222     struct sound_trigger_info  *st_ses_info;
223 
224     if (!st_dev)
225        return -EINVAL;
226 
227     pthread_mutex_lock(&st_dev->lock);
228     switch (event) {
229     case ST_EVENT_SESSION_REGISTER:
230         if (!config) {
231             ALOGE("%s: NULL config", __func__);
232             status = -EINVAL;
233             break;
234         }
235         st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
236         if (!st_ses_info) {
237             ALOGE("%s: st_ses_info alloc failed", __func__);
238             status = -ENOMEM;
239             break;
240         }
241         memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (config->st_ses));
242         ALOGV("%s: add capture_handle %d pcm %p", __func__,
243               st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.pcm);
244         list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
245         break;
246 
247     case ST_EVENT_SESSION_DEREGISTER:
248         if (!config) {
249             ALOGE("%s: NULL config", __func__);
250             status = -EINVAL;
251             break;
252         }
253         st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
254         if (!st_ses_info) {
255             ALOGE("%s: pcm %p not in the list!", __func__, config->st_ses.pcm);
256             status = -EINVAL;
257             break;
258         }
259         ALOGV("%s: remove capture_handle %d pcm %p", __func__,
260               st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.pcm);
261         list_remove(&st_ses_info->list);
262         free(st_ses_info);
263         break;
264     default:
265         ALOGW("%s: Unknown event %d", __func__, event);
266         break;
267     }
268     pthread_mutex_unlock(&st_dev->lock);
269     return status;
270 }
271 
audio_extn_sound_trigger_read(struct stream_in * in,void * buffer,size_t bytes)272 int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
273                        size_t bytes)
274 {
275     int ret = -1;
276     struct sound_trigger_info  *st_info = NULL;
277     struct audio_event_info event;
278 
279     if (!st_dev)
280        return ret;
281 
282     if (!in->is_st_session_active) {
283         ALOGE(" %s: Sound trigger is not active", __func__);
284         goto exit;
285     }
286     if (in->standby)
287         in->standby = false;
288 
289     pthread_mutex_lock(&st_dev->lock);
290     st_info = get_sound_trigger_info(in->capture_handle);
291     pthread_mutex_unlock(&st_dev->lock);
292     if (st_info) {
293         event.u.aud_info.ses_info = &st_info->st_ses;
294         event.u.aud_info.buf = buffer;
295         event.u.aud_info.num_bytes = bytes;
296         ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
297     }
298 
299 exit:
300     if (ret) {
301         if (-ENETRESET == ret)
302             in->is_st_session_active = false;
303         memset(buffer, 0, bytes);
304         ALOGV("%s: read failed status %d - sleep", __func__, ret);
305         usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
306                                    in->config.rate));
307     }
308     return ret;
309 }
310 
audio_extn_sound_trigger_stop_lab(struct stream_in * in)311 void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
312 {
313     struct sound_trigger_info  *st_ses_info = NULL;
314     struct audio_event_info event;
315 
316     if (!st_dev || !in || !in->is_st_session_active)
317        return;
318 
319     pthread_mutex_lock(&st_dev->lock);
320     st_ses_info = get_sound_trigger_info(in->capture_handle);
321     pthread_mutex_unlock(&st_dev->lock);
322     if (st_ses_info) {
323         event.u.ses_info = st_ses_info->st_ses;
324         ALOGV("%s: AUDIO_EVENT_STOP_LAB pcm %p", __func__, st_ses_info->st_ses.pcm);
325         st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
326         in->is_st_session_active = false;
327     }
328 }
329 
audio_extn_sound_trigger_check_and_get_session(struct stream_in * in)330 void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
331 {
332     struct sound_trigger_info  *st_ses_info = NULL;
333     struct listnode *node;
334 
335     if (!st_dev || !in)
336        return;
337 
338     pthread_mutex_lock(&st_dev->lock);
339     in->is_st_session = false;
340     ALOGV("%s: list %d capture_handle %d", __func__,
341           list_empty(&st_dev->st_ses_list), in->capture_handle);
342     list_for_each(node, &st_dev->st_ses_list) {
343         st_ses_info = node_to_item(node, struct sound_trigger_info , list);
344         if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
345             in->pcm = st_ses_info->st_ses.pcm;
346             in->config = st_ses_info->st_ses.config;
347             in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
348             in->is_st_session = true;
349             in->is_st_session_active = true;
350             ALOGV("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
351             break;
352         }
353     }
354     pthread_mutex_unlock(&st_dev->lock);
355 }
356 
audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,st_event_type_t event)357 void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
358                                      st_event_type_t event)
359 {
360     int device_type = -1;
361 
362     if (!st_dev)
363        return;
364 
365     if (st_dev->sthal_prop_api_version >= STHAL_PROP_API_VERSION_1_0)
366         return;
367 
368     if (snd_device >= SND_DEVICE_OUT_BEGIN &&
369         snd_device < SND_DEVICE_OUT_END) {
370         device_type = PCM_PLAYBACK;
371     } else if (snd_device >= SND_DEVICE_IN_BEGIN &&
372         snd_device < SND_DEVICE_IN_END) {
373         if (snd_device == SND_DEVICE_IN_CAPTURE_VI_FEEDBACK)
374             return;
375         device_type = PCM_CAPTURE;
376     } else {
377         ALOGE("%s: invalid device 0x%x, for event %d",
378                            __func__, snd_device, event);
379         return;
380     }
381 
382     ALOGV("%s: device 0x%x of type %d for Event %d",
383         __func__, snd_device, device_type, event);
384     if (device_type == PCM_CAPTURE) {
385         switch(event) {
386         case ST_EVENT_SND_DEVICE_FREE:
387             st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
388             break;
389         case ST_EVENT_SND_DEVICE_BUSY:
390             st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
391             break;
392         default:
393             ALOGW("%s:invalid event %d for device 0x%x",
394                                   __func__, event, snd_device);
395         }
396     }/*Events for output device, if required can be placed here in else*/
397 }
398 
audio_extn_sound_trigger_update_stream_status(struct audio_usecase * uc_info,st_event_type_t event)399 void audio_extn_sound_trigger_update_stream_status(struct audio_usecase *uc_info,
400                                      st_event_type_t event)
401 {
402     bool raise_event = false;
403     struct audio_event_info ev_info;
404     audio_event_type_t ev;
405 
406     if (!st_dev)
407        return;
408 
409     if (st_dev->sthal_prop_api_version < STHAL_PROP_API_VERSION_1_0)
410         return;
411 
412     if (uc_info == NULL) {
413         ALOGE("%s: null usecase", __func__);
414         return;
415     }
416 
417     bool valid_type =
418             (uc_info->type == PCM_PLAYBACK &&
419              platform_snd_device_has_speaker(uc_info->out_snd_device)) ||
420             (uc_info->type == PCM_CAPTURE) ||
421             (uc_info->type == VOICE_CALL);
422 
423     if (valid_type && platform_sound_trigger_usecase_needs_event(uc_info->id)) {
424         ALOGV("%s: uc_id %d of type %d for Event %d, with Raise=%d",
425               __func__, uc_info->id, uc_info->type, event, raise_event);
426         if (uc_info->type == PCM_CAPTURE) {
427             ev = (event == ST_EVENT_STREAM_BUSY) ? AUDIO_EVENT_CAPTURE_STREAM_ACTIVE :
428                                                    AUDIO_EVENT_CAPTURE_STREAM_INACTIVE;
429         } else {
430             ev = (event == ST_EVENT_STREAM_BUSY) ? AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE :
431                                                    AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE;
432         }
433         if (!populate_usecase(&ev_info.u.usecase, uc_info)) {
434             ALOGI("%s: send event %d: usecase id %d, type %d",
435                   __func__, ev, uc_info->id, uc_info->type);
436             st_dev->st_callback(ev, &ev_info);
437         }
438     }
439 }
440 
audio_extn_sound_trigger_set_parameters(struct audio_device * adev __unused,struct str_parms * params)441 void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
442                                struct str_parms *params)
443 {
444     struct audio_event_info event;
445     char value[32];
446     int ret, val;
447 
448     if(!st_dev || !params) {
449         ALOGE("%s: str_params NULL", __func__);
450         return;
451     }
452 
453     ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
454                             sizeof(value));
455     if (ret > 0) {
456         if (strstr(value, "OFFLINE")) {
457             event.u.status = SND_CARD_STATUS_OFFLINE;
458             st_dev->st_callback(AUDIO_EVENT_SSR, &event);
459         }
460         else if (strstr(value, "ONLINE")) {
461             event.u.status = SND_CARD_STATUS_ONLINE;
462             st_dev->st_callback(AUDIO_EVENT_SSR, &event);
463         }
464         else
465             ALOGE("%s: unknown snd_card_status", __func__);
466     }
467 
468     ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
469     if (ret > 0) {
470         if (strstr(value, "OFFLINE")) {
471             event.u.status = CPE_STATUS_OFFLINE;
472             st_dev->st_callback(AUDIO_EVENT_SSR, &event);
473         }
474         else if (strstr(value, "ONLINE")) {
475             event.u.status = CPE_STATUS_ONLINE;
476             st_dev->st_callback(AUDIO_EVENT_SSR, &event);
477         }
478         else
479             ALOGE("%s: unknown CPE status", __func__);
480     }
481 
482     ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
483     if (ret >= 0) {
484         event.u.value = val;
485         st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
486     }
487 }
488 
audio_extn_sound_trigger_init(struct audio_device * adev)489 int audio_extn_sound_trigger_init(struct audio_device *adev)
490 {
491     int status = 0;
492     char sound_trigger_lib[100];
493     void *sthal_prop_api_version;
494 
495     ALOGV("%s: Enter", __func__);
496 
497     st_dev = (struct sound_trigger_audio_device*)
498                         calloc(1, sizeof(struct sound_trigger_audio_device));
499     if (!st_dev) {
500         ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
501         return -ENOMEM;
502     }
503 
504     snprintf(sound_trigger_lib, sizeof(sound_trigger_lib),
505              SOUND_TRIGGER_LIBRARY_PATH,
506               XSTR(SOUND_TRIGGER_PLATFORM_NAME));
507 
508     st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
509 
510     if (st_dev->lib_handle == NULL) {
511         ALOGE("%s: error %s", __func__, dlerror());
512         status = -ENODEV;
513         goto cleanup;
514     }
515     ALOGV("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
516 
517     DLSYM(st_dev->lib_handle, st_dev->st_callback, sound_trigger_hw_call_back,
518           status);
519     if (status)
520         goto cleanup;
521 
522     DLSYM(st_dev->lib_handle, sthal_prop_api_version,
523           sthal_prop_api_version, status);
524     if (status) {
525         st_dev->sthal_prop_api_version = 0;
526         status  = 0; /* passthru for backward compability */
527     } else {
528         st_dev->sthal_prop_api_version = *(int*)sthal_prop_api_version;
529         if (MAJOR_VERSION(st_dev->sthal_prop_api_version) !=
530             MAJOR_VERSION(STHAL_PROP_API_CURRENT_VERSION)) {
531             ALOGE("%s: Incompatible API versions ahal:0x%x != sthal:0x%x",
532                   __func__, STHAL_PROP_API_CURRENT_VERSION,
533                   st_dev->sthal_prop_api_version);
534             goto cleanup;
535         }
536         ALOGD("%s: sthal is using proprietary API version 0x%04x", __func__,
537               st_dev->sthal_prop_api_version);
538     }
539 
540     st_dev->adev = adev;
541     list_init(&st_dev->st_ses_list);
542     audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
543 
544     return 0;
545 
546 cleanup:
547     if (st_dev->lib_handle)
548         dlclose(st_dev->lib_handle);
549     free(st_dev);
550     st_dev = NULL;
551     return status;
552 }
553 
audio_extn_sound_trigger_deinit(struct audio_device * adev)554 void audio_extn_sound_trigger_deinit(struct audio_device *adev)
555 {
556     ALOGV("%s: Enter", __func__);
557     if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
558         audio_extn_snd_mon_unregister_listener(st_dev);
559         dlclose(st_dev->lib_handle);
560         free(st_dev);
561         st_dev = NULL;
562     }
563 }
564