1 /*
2  * Copyright (C) 2016 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 
17 #define LOG_TAG "audio_hw_hikey"
18 //#define LOG_NDEBUG 0
19 
20 #include <errno.h>
21 #include <malloc.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <sys/time.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 
28 #include <log/log.h>
29 #include <cutils/str_parms.h>
30 #include <cutils/properties.h>
31 
32 #include <hardware/hardware.h>
33 #include <system/audio.h>
34 #include <hardware/audio.h>
35 
36 #include <sound/asound.h>
37 #include <tinyalsa/asoundlib.h>
38 #include <audio_utils/resampler.h>
39 #include <audio_utils/echo_reference.h>
40 #include <hardware/audio_effect.h>
41 #include <hardware/audio_alsaops.h>
42 #include <audio_effects/effect_aec.h>
43 
44 #include <sys/ioctl.h>
45 #include <linux/audio_hifi.h>
46 
47 #define CARD_OUT 0
48 #define PORT_CODEC 0
49 /* Minimum granularity - Arbitrary but small value */
50 #define CODEC_BASE_FRAME_COUNT 32
51 
52 /* number of base blocks in a short period (low latency) */
53 #define PERIOD_MULTIPLIER 32  /* 21 ms */
54 /* number of frames per short period (low latency) */
55 #define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER)
56 /* number of pseudo periods for low latency playback */
57 #define PLAYBACK_PERIOD_COUNT 4
58 #define PLAYBACK_PERIOD_START_THRESHOLD 2
59 #define CODEC_SAMPLING_RATE 48000
60 #define CHANNEL_STEREO 2
61 #define MIN_WRITE_SLEEP_US      5000
62 
63 #ifdef ENABLE_XAF_DSP_DEVICE
64 #include "xaf-utils-test.h"
65 #include "audio/xa_vorbis_dec_api.h"
66 #include "audio/xa-audio-decoder-api.h"
67 #define NUM_COMP_IN_GRAPH   1
68 
69 struct alsa_audio_device;
70 
71 struct xaf_dsp_device {
72     void *p_adev;
73     void *p_decoder;
74     xaf_info_t comp_info;
75     /* ...playback format */
76     xaf_format_t pb_format;
77     xaf_comp_status dec_status;
78     int dec_info[4];
79     void *dec_inbuf[2];
80     int read_length;
81     xf_id_t dec_id;
82     int xaf_started;
83     mem_obj_t* mem_handle;
84     int num_comp;
85     int (*dec_setup)(void *p_comp, struct alsa_audio_device *audio_device);
86     int xafinitdone;
87 };
88 #endif
89 
90 struct stub_stream_in {
91     struct audio_stream_in stream;
92 };
93 
94 struct alsa_audio_device {
95     struct audio_hw_device hw_device;
96 
97     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
98     int devices;
99     struct alsa_stream_in *active_input;
100     struct alsa_stream_out *active_output;
101     bool mic_mute;
102 #ifdef ENABLE_XAF_DSP_DEVICE
103     struct xaf_dsp_device dsp_device;
104     int hifi_dsp_fd;
105 #endif
106 };
107 
108 struct alsa_stream_out {
109     struct audio_stream_out stream;
110 
111     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
112     struct pcm_config config;
113     struct pcm *pcm;
114     bool unavailable;
115     int standby;
116     struct alsa_audio_device *dev;
117     int write_threshold;
118     unsigned int written;
119 };
120 
121 #ifdef ENABLE_XAF_DSP_DEVICE
122 static int pcm_setup(void *p_pcm, struct alsa_audio_device *audio_device)
123 {
124     int param[6];
125 
126     param[0] = XA_CODEC_CONFIG_PARAM_SAMPLE_RATE;
127     param[1] = audio_device->dsp_device.pb_format.sample_rate;
128     param[2] = XA_CODEC_CONFIG_PARAM_CHANNELS;
129     param[3] = audio_device->dsp_device.pb_format.channels;
130     param[4] = XA_CODEC_CONFIG_PARAM_PCM_WIDTH;
131     param[5] = audio_device->dsp_device.pb_format.pcm_width;
132 
133     XF_CHK_API(xaf_comp_set_config(p_pcm, 3, &param[0]));
134 
135     return 0;
136 }
137 
138 void xa_thread_exit_handler(int sig)
139 {
140     /* ...unused arg */
141     (void) sig;
142 
143     pthread_exit(0);
144 }
145 
146 /*xtensa audio device init*/
147 static int xa_device_init(struct alsa_audio_device *audio_device)
148 {
149     /* ...initialize playback format */
150     audio_device->dsp_device.p_adev = NULL;
151     audio_device->dsp_device.pb_format.sample_rate = 48000;
152     audio_device->dsp_device.pb_format.channels    = 2;
153     audio_device->dsp_device.pb_format.pcm_width   = 16;
154     audio_device->dsp_device.xafinitdone = 0;
155     audio_frmwk_buf_size = 0; //unused
156     audio_comp_buf_size  = 0; //unused
157     audio_device->dsp_device.num_comp = NUM_COMP_IN_GRAPH;
158     struct sigaction actions;
159     memset(&actions, 0, sizeof(actions));
160     sigemptyset(&actions.sa_mask);
161     actions.sa_flags = 0;
162     actions.sa_handler = xa_thread_exit_handler;
163     sigaction(SIGUSR1,&actions,NULL);
164     /* ...initialize tracing facility */
165     audio_device->dsp_device.xaf_started =1;
166     audio_device->dsp_device.dec_id    = "audio-decoder/pcm";
167     audio_device->dsp_device.dec_setup = pcm_setup;
168     audio_device->dsp_device.mem_handle = mem_init(); //initialize memory handler
169     XF_CHK_API(xaf_adev_open(&audio_device->dsp_device.p_adev, audio_frmwk_buf_size, audio_comp_buf_size, mem_malloc, mem_free));
170     /* ...create decoder component */
171     XF_CHK_API(xaf_comp_create(audio_device->dsp_device.p_adev, &audio_device->dsp_device.p_decoder, audio_device->dsp_device.dec_id, 1, 1, &audio_device->dsp_device.dec_inbuf[0], XAF_DECODER));
172     XF_CHK_API(audio_device->dsp_device.dec_setup(audio_device->dsp_device.p_decoder,audio_device));
173 
174     /* ...start decoder component */
175     XF_CHK_API(xaf_comp_process(audio_device->dsp_device.p_adev, audio_device->dsp_device.p_decoder, NULL, 0, XAF_START_FLAG));
176     return 0;
177 }
178 
179 static int xa_device_run(struct audio_stream_out *stream, const void *buffer, size_t frame_size, size_t out_frames, size_t bytes)
180 {
181     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
182     struct alsa_audio_device *adev = out->dev;
183     int ret=0;
184     void *p_comp=adev->dsp_device.p_decoder;
185     xaf_comp_status comp_status;
186     memcpy(adev->dsp_device.dec_inbuf[0],buffer,bytes);
187     adev->dsp_device.read_length=bytes;
188 
189     if (adev->dsp_device.xafinitdone == 0) {
190         XF_CHK_API(xaf_comp_process(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG));
191         XF_CHK_API(xaf_comp_get_status(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, &adev->dsp_device.dec_status, &adev->dsp_device.comp_info));
192         ALOGE("PROXY:%s xaf_comp_get_status %d\n",__func__,adev->dsp_device.dec_status);
193         if (adev->dsp_device.dec_status == XAF_INIT_DONE) {
194             adev->dsp_device.xafinitdone = 1;
195             out->written += out_frames;
196             XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_EXEC_FLAG));
197         }
198     } else {
199         XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG));
200         while (1) {
201             XF_CHK_API(xaf_comp_get_status(NULL, p_comp, &comp_status, &adev->dsp_device.comp_info));
202             if (comp_status == XAF_EXEC_DONE) break;
203             if (comp_status == XAF_NEED_INPUT) {
204                  ALOGV("PROXY:%s loop:XAF_NEED_INPUT\n",__func__);
205                  break;
206             }
207             if (comp_status == XAF_OUTPUT_READY) {
208                 void *p_buf = (void *)adev->dsp_device.comp_info.buf;
209                 int size    = adev->dsp_device.comp_info.length;
210                 ret = pcm_mmap_write(out->pcm, p_buf, size);
211                 if (ret == 0) {
212                     out->written += out_frames;
213                 }
214                 XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, (void *)adev->dsp_device.comp_info.buf, adev->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG));
215             }
216         }
217     }
218     return ret;
219 }
220 
221 static int xa_device_close(struct alsa_audio_device *audio_device)
222 {
223     if (audio_device->dsp_device.xaf_started) {
224         xaf_comp_status comp_status;
225         audio_device->dsp_device.xaf_started=0;
226         while (1) {
227             XF_CHK_API(xaf_comp_get_status(NULL, audio_device->dsp_device.p_decoder, &comp_status, &audio_device->dsp_device.comp_info));
228             ALOGV("PROXY:comp_status:%d,audio_device->dsp_device.comp_info.length:%d\n",(int)comp_status,audio_device->dsp_device.comp_info.length);
229             if (comp_status == XAF_EXEC_DONE)
230                 break;
231             if (comp_status == XAF_NEED_INPUT) {
232                 XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, NULL, 0, XAF_INPUT_OVER_FLAG));
233             }
234 
235             if (comp_status == XAF_OUTPUT_READY) {
236                 XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, (void *)audio_device->dsp_device.comp_info.buf, audio_device->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG));
237             }
238         }
239 
240         /* ...exec done, clean-up */
241         XF_CHK_API(xaf_comp_delete(audio_device->dsp_device.p_decoder));
242         XF_CHK_API(xaf_adev_close(audio_device->dsp_device.p_adev, 0 /*unused*/));
243         mem_exit();
244         XF_CHK_API(print_mem_mcps_info(audio_device->dsp_device.mem_handle, audio_device->dsp_device.num_comp));
245     }
246     return 0;
247 }
248 #endif
249 
250 /* must be called with hw device and output stream mutexes locked */
251 static int start_output_stream(struct alsa_stream_out *out)
252 {
253     struct alsa_audio_device *adev = out->dev;
254 
255     if (out->unavailable)
256         return -ENODEV;
257 
258     /* default to low power: will be corrected in out_write if necessary before first write to
259      * tinyalsa.
260      */
261     out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE;
262     out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE;
263     out->config.avail_min = PERIOD_SIZE;
264 
265     out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config);
266 
267     if (!pcm_is_ready(out->pcm)) {
268         ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
269         pcm_close(out->pcm);
270         adev->active_output = NULL;
271         out->unavailable = true;
272         return -ENODEV;
273     }
274 
275     adev->active_output = out;
276     return 0;
277 }
278 
279 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
280 {
281     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
282     return out->config.rate;
283 }
284 
285 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
286 {
287     ALOGV("out_set_sample_rate: %d", 0);
288     return -ENOSYS;
289 }
290 
291 static size_t out_get_buffer_size(const struct audio_stream *stream)
292 {
293     ALOGV("out_get_buffer_size: %d", 4096);
294 
295     /* return the closest majoring multiple of 16 frames, as
296      * audioflinger expects audio buffers to be a multiple of 16 frames */
297     size_t size = PERIOD_SIZE;
298     size = ((size + 15) / 16) * 16;
299     return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
300 }
301 
302 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
303 {
304     ALOGV("out_get_channels");
305     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
306     return audio_channel_out_mask_from_count(out->config.channels);
307 }
308 
309 static audio_format_t out_get_format(const struct audio_stream *stream)
310 {
311     ALOGV("out_get_format");
312     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
313     return audio_format_from_pcm_format(out->config.format);
314 }
315 
316 static int out_set_format(struct audio_stream *stream, audio_format_t format)
317 {
318     ALOGV("out_set_format: %d",format);
319     return -ENOSYS;
320 }
321 
322 static int do_output_standby(struct alsa_stream_out *out)
323 {
324     struct alsa_audio_device *adev = out->dev;
325 
326     if (!out->standby) {
327         pcm_close(out->pcm);
328         out->pcm = NULL;
329         adev->active_output = NULL;
330         out->standby = 1;
331     }
332     return 0;
333 }
334 
335 static int out_standby(struct audio_stream *stream)
336 {
337     ALOGV("out_standby");
338     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
339     int status;
340 
341     pthread_mutex_lock(&out->dev->lock);
342     pthread_mutex_lock(&out->lock);
343 #ifdef ENABLE_XAF_DSP_DEVICE
344     xa_device_close(out->dev);
345 #endif
346     status = do_output_standby(out);
347     pthread_mutex_unlock(&out->lock);
348     pthread_mutex_unlock(&out->dev->lock);
349     return status;
350 }
351 
352 static int out_dump(const struct audio_stream *stream, int fd)
353 {
354     ALOGV("out_dump");
355     return 0;
356 }
357 
358 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
359 {
360     ALOGV("out_set_parameters");
361     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
362     struct alsa_audio_device *adev = out->dev;
363     struct str_parms *parms;
364     char value[32];
365     int val = 0;
366     int ret = -EINVAL;
367 
368     if (kvpairs == NULL || kvpairs[0] == 0) {
369         return 0;
370     }
371 
372     parms = str_parms_create_str(kvpairs);
373 
374     if (str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)) >= 0) {
375         val = atoi(value);
376         pthread_mutex_lock(&adev->lock);
377         pthread_mutex_lock(&out->lock);
378         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
379             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
380             adev->devices |= val;
381         }
382         pthread_mutex_unlock(&out->lock);
383         pthread_mutex_unlock(&adev->lock);
384         ret = 0;
385     }
386 
387     str_parms_destroy(parms);
388     return ret;
389 }
390 
391 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
392 {
393     ALOGV("out_get_parameters");
394     return strdup("");
395 }
396 
397 static uint32_t out_get_latency(const struct audio_stream_out *stream)
398 {
399     ALOGV("out_get_latency");
400     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
401     return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
402 }
403 
404 static int out_set_volume(struct audio_stream_out *stream, float left,
405         float right)
406 {
407     ALOGV("out_set_volume: Left:%f Right:%f", left, right);
408     return 0;
409 }
410 
411 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
412         size_t bytes)
413 {
414     int ret;
415     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
416     struct alsa_audio_device *adev = out->dev;
417     size_t frame_size = audio_stream_out_frame_size(stream);
418     size_t out_frames = bytes / frame_size;
419 
420     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
421      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
422      * mutex
423      */
424     pthread_mutex_lock(&adev->lock);
425     pthread_mutex_lock(&out->lock);
426     if (out->standby) {
427 #ifdef ENABLE_XAF_DSP_DEVICE
428         if (adev->hifi_dsp_fd >= 0) {
429             xa_device_init(adev);
430         }
431 #endif
432         ret = start_output_stream(out);
433         if (ret != 0) {
434             pthread_mutex_unlock(&adev->lock);
435             goto exit;
436         }
437         out->standby = 0;
438     }
439 
440     pthread_mutex_unlock(&adev->lock);
441 
442 #ifdef ENABLE_XAF_DSP_DEVICE
443     /*fallback to original audio processing*/
444     if (adev->dsp_device.p_adev != NULL) {
445         ret = xa_device_run(stream, buffer,frame_size, out_frames, bytes);
446     } else {
447 #endif
448         ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size);
449         if (ret == 0) {
450             out->written += out_frames;
451         }
452 #ifdef ENABLE_XAF_DSP_DEVICE
453     }
454 #endif
455 exit:
456     pthread_mutex_unlock(&out->lock);
457 
458     if (ret != 0) {
459         usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
460                 out_get_sample_rate(&stream->common));
461     }
462 
463     return bytes;
464 }
465 
466 static int out_get_render_position(const struct audio_stream_out *stream,
467         uint32_t *dsp_frames)
468 {
469     *dsp_frames = 0;
470     ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
471     return -EINVAL;
472 }
473 
474 static int out_get_presentation_position(const struct audio_stream_out *stream,
475                                    uint64_t *frames, struct timespec *timestamp)
476 {
477     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
478     int ret = -1;
479 
480         if (out->pcm) {
481             unsigned int avail;
482             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
483                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
484                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
485                 if (signed_frames >= 0) {
486                     *frames = signed_frames;
487                     ret = 0;
488                 }
489             }
490         }
491 
492     return ret;
493 }
494 
495 
496 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
497 {
498     ALOGV("out_add_audio_effect: %p", effect);
499     return 0;
500 }
501 
502 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
503 {
504     ALOGV("out_remove_audio_effect: %p", effect);
505     return 0;
506 }
507 
508 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
509         int64_t *timestamp)
510 {
511     *timestamp = 0;
512     ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
513     return -EINVAL;
514 }
515 
516 /** audio_stream_in implementation **/
517 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
518 {
519     ALOGV("in_get_sample_rate");
520     return 8000;
521 }
522 
523 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
524 {
525     ALOGV("in_set_sample_rate: %d", rate);
526     return -ENOSYS;
527 }
528 
529 static size_t in_get_buffer_size(const struct audio_stream *stream)
530 {
531     ALOGV("in_get_buffer_size: %d", 320);
532     return 320;
533 }
534 
535 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
536 {
537     ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
538     return AUDIO_CHANNEL_IN_MONO;
539 }
540 
541 static audio_format_t in_get_format(const struct audio_stream *stream)
542 {
543     return AUDIO_FORMAT_PCM_16_BIT;
544 }
545 
546 static int in_set_format(struct audio_stream *stream, audio_format_t format)
547 {
548     return -ENOSYS;
549 }
550 
551 static int in_standby(struct audio_stream *stream)
552 {
553     return 0;
554 }
555 
556 static int in_dump(const struct audio_stream *stream, int fd)
557 {
558     return 0;
559 }
560 
561 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
562 {
563     return 0;
564 }
565 
566 static char * in_get_parameters(const struct audio_stream *stream,
567         const char *keys)
568 {
569     return strdup("");
570 }
571 
572 static int in_set_gain(struct audio_stream_in *stream, float gain)
573 {
574     return 0;
575 }
576 
577 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
578         size_t bytes)
579 {
580     ALOGV("in_read: bytes %zu", bytes);
581     /* XXX: fake timing for audio input */
582     usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
583             in_get_sample_rate(&stream->common));
584     memset(buffer, 0, bytes);
585     return bytes;
586 }
587 
588 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
589 {
590     return 0;
591 }
592 
593 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
594 {
595     return 0;
596 }
597 
598 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
599 {
600     return 0;
601 }
602 
603 static int adev_open_output_stream(struct audio_hw_device *dev,
604         audio_io_handle_t handle,
605         audio_devices_t devices,
606         audio_output_flags_t flags,
607         struct audio_config *config,
608         struct audio_stream_out **stream_out,
609         const char *address __unused)
610 {
611     ALOGV("adev_open_output_stream...");
612 
613     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
614     struct alsa_stream_out *out;
615     struct pcm_params *params;
616     int ret = 0;
617 
618     params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
619     if (!params)
620         return -ENOSYS;
621 
622     out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
623     if (!out)
624         return -ENOMEM;
625 
626     out->stream.common.get_sample_rate = out_get_sample_rate;
627     out->stream.common.set_sample_rate = out_set_sample_rate;
628     out->stream.common.get_buffer_size = out_get_buffer_size;
629     out->stream.common.get_channels = out_get_channels;
630     out->stream.common.get_format = out_get_format;
631     out->stream.common.set_format = out_set_format;
632     out->stream.common.standby = out_standby;
633     out->stream.common.dump = out_dump;
634     out->stream.common.set_parameters = out_set_parameters;
635     out->stream.common.get_parameters = out_get_parameters;
636     out->stream.common.add_audio_effect = out_add_audio_effect;
637     out->stream.common.remove_audio_effect = out_remove_audio_effect;
638     out->stream.get_latency = out_get_latency;
639     out->stream.set_volume = out_set_volume;
640     out->stream.write = out_write;
641     out->stream.get_render_position = out_get_render_position;
642     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
643     out->stream.get_presentation_position = out_get_presentation_position;
644 
645     out->config.channels = CHANNEL_STEREO;
646     out->config.rate = CODEC_SAMPLING_RATE;
647     out->config.format = PCM_FORMAT_S16_LE;
648     out->config.period_size = PERIOD_SIZE;
649     out->config.period_count = PLAYBACK_PERIOD_COUNT;
650 
651     if (out->config.rate != config->sample_rate ||
652            audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
653                out->config.format !=  pcm_format_from_audio_format(config->format) ) {
654         config->sample_rate = out->config.rate;
655         config->format = audio_format_from_pcm_format(out->config.format);
656         config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
657         ret = -EINVAL;
658     }
659 
660     ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d",
661                 out->config.channels, out->config.rate, out->config.format);
662 
663     out->dev = ladev;
664     out->standby = 1;
665     out->unavailable = false;
666 
667     config->format = out_get_format(&out->stream.common);
668     config->channel_mask = out_get_channels(&out->stream.common);
669     config->sample_rate = out_get_sample_rate(&out->stream.common);
670 
671     *stream_out = &out->stream;
672 
673     /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
674     ret = 0;
675 
676     return ret;
677 }
678 
679 static void adev_close_output_stream(struct audio_hw_device *dev,
680         struct audio_stream_out *stream)
681 {
682     ALOGV("adev_close_output_stream...");
683     free(stream);
684 }
685 
686 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
687 {
688     ALOGV("adev_set_parameters");
689     return -ENOSYS;
690 }
691 
692 static char * adev_get_parameters(const struct audio_hw_device *dev,
693         const char *keys)
694 {
695     ALOGV("adev_get_parameters");
696     return strdup("");
697 }
698 
699 static int adev_init_check(const struct audio_hw_device *dev)
700 {
701     ALOGV("adev_init_check");
702     return 0;
703 }
704 
705 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
706 {
707     ALOGV("adev_set_voice_volume: %f", volume);
708     return -ENOSYS;
709 }
710 
711 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
712 {
713     ALOGV("adev_set_master_volume: %f", volume);
714     return -ENOSYS;
715 }
716 
717 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
718 {
719     ALOGV("adev_get_master_volume: %f", *volume);
720     return -ENOSYS;
721 }
722 
723 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
724 {
725     ALOGV("adev_set_master_mute: %d", muted);
726     return -ENOSYS;
727 }
728 
729 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
730 {
731     ALOGV("adev_get_master_mute: %d", *muted);
732     return -ENOSYS;
733 }
734 
735 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
736 {
737     ALOGV("adev_set_mode: %d", mode);
738     return 0;
739 }
740 
741 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
742 {
743     ALOGV("adev_set_mic_mute: %d",state);
744     return -ENOSYS;
745 }
746 
747 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
748 {
749     ALOGV("adev_get_mic_mute");
750     return -ENOSYS;
751 }
752 
753 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
754         const struct audio_config *config)
755 {
756     ALOGV("adev_get_input_buffer_size: %d", 320);
757     return 320;
758 }
759 
760 static int adev_open_input_stream(struct audio_hw_device __unused *dev,
761         audio_io_handle_t handle,
762         audio_devices_t devices,
763         struct audio_config *config,
764         struct audio_stream_in **stream_in,
765         audio_input_flags_t flags __unused,
766         const char *address __unused,
767         audio_source_t source __unused)
768 {
769     struct stub_stream_in *in;
770 
771     ALOGV("adev_open_input_stream...");
772 
773     in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
774     if (!in)
775         return -ENOMEM;
776 
777     in->stream.common.get_sample_rate = in_get_sample_rate;
778     in->stream.common.set_sample_rate = in_set_sample_rate;
779     in->stream.common.get_buffer_size = in_get_buffer_size;
780     in->stream.common.get_channels = in_get_channels;
781     in->stream.common.get_format = in_get_format;
782     in->stream.common.set_format = in_set_format;
783     in->stream.common.standby = in_standby;
784     in->stream.common.dump = in_dump;
785     in->stream.common.set_parameters = in_set_parameters;
786     in->stream.common.get_parameters = in_get_parameters;
787     in->stream.common.add_audio_effect = in_add_audio_effect;
788     in->stream.common.remove_audio_effect = in_remove_audio_effect;
789     in->stream.set_gain = in_set_gain;
790     in->stream.read = in_read;
791     in->stream.get_input_frames_lost = in_get_input_frames_lost;
792 
793     *stream_in = &in->stream;
794     return 0;
795 }
796 
797 static void adev_close_input_stream(struct audio_hw_device *dev,
798         struct audio_stream_in *in)
799 {
800     ALOGV("adev_close_input_stream...");
801     return;
802 }
803 
804 static int adev_dump(const audio_hw_device_t *device, int fd)
805 {
806     ALOGV("adev_dump");
807     return 0;
808 }
809 
810 static int adev_close(hw_device_t *device)
811 {
812 #ifdef ENABLE_XAF_DSP_DEVICE
813     struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
814 #endif
815     ALOGV("adev_close");
816 #ifdef ENABLE_XAF_DSP_DEVICE
817     if (adev->hifi_dsp_fd >= 0)
818         close(adev->hifi_dsp_fd);
819 #endif
820     free(device);
821     return 0;
822 }
823 
824 static int adev_open(const hw_module_t* module, const char* name,
825         hw_device_t** device)
826 {
827     struct alsa_audio_device *adev;
828 
829     ALOGV("adev_open: %s", name);
830 
831     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
832         return -EINVAL;
833 
834     adev = calloc(1, sizeof(struct alsa_audio_device));
835     if (!adev)
836         return -ENOMEM;
837 
838     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
839     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
840     adev->hw_device.common.module = (struct hw_module_t *) module;
841     adev->hw_device.common.close = adev_close;
842     adev->hw_device.init_check = adev_init_check;
843     adev->hw_device.set_voice_volume = adev_set_voice_volume;
844     adev->hw_device.set_master_volume = adev_set_master_volume;
845     adev->hw_device.get_master_volume = adev_get_master_volume;
846     adev->hw_device.set_master_mute = adev_set_master_mute;
847     adev->hw_device.get_master_mute = adev_get_master_mute;
848     adev->hw_device.set_mode = adev_set_mode;
849     adev->hw_device.set_mic_mute = adev_set_mic_mute;
850     adev->hw_device.get_mic_mute = adev_get_mic_mute;
851     adev->hw_device.set_parameters = adev_set_parameters;
852     adev->hw_device.get_parameters = adev_get_parameters;
853     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
854     adev->hw_device.open_output_stream = adev_open_output_stream;
855     adev->hw_device.close_output_stream = adev_close_output_stream;
856     adev->hw_device.open_input_stream = adev_open_input_stream;
857     adev->hw_device.close_input_stream = adev_close_input_stream;
858     adev->hw_device.dump = adev_dump;
859 
860     adev->devices = AUDIO_DEVICE_NONE;
861 
862     *device = &adev->hw_device.common;
863 #ifdef ENABLE_XAF_DSP_DEVICE
864     adev->hifi_dsp_fd = open(HIFI_DSP_MISC_DRIVER, O_WRONLY, 0);
865     if (adev->hifi_dsp_fd < 0) {
866         ALOGW("hifi_dsp: Error opening device %d", errno);
867     } else {
868         ALOGI("hifi_dsp: Open device");
869     }
870 #endif
871     return 0;
872 }
873 
874 static struct hw_module_methods_t hal_module_methods = {
875     .open = adev_open,
876 };
877 
878 struct audio_module HAL_MODULE_INFO_SYM = {
879     .common = {
880         .tag = HARDWARE_MODULE_TAG,
881         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
882         .hal_api_version = HARDWARE_HAL_API_VERSION,
883         .id = AUDIO_HARDWARE_MODULE_ID,
884         .name = "Hikey audio HW HAL",
885         .author = "The Android Open Source Project",
886         .methods = &hal_module_methods,
887     },
888 };
889