• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 #include <cutils/log.h>
28 #include <cutils/str_parms.h>
29 #include <cutils/properties.h>
30 
31 #include <hardware/hardware.h>
32 #include <system/audio.h>
33 #include <hardware/audio.h>
34 
35 #include <sound/asound.h>
36 #include <tinyalsa/asoundlib.h>
37 #include <audio_utils/resampler.h>
38 #include <audio_utils/echo_reference.h>
39 #include <hardware/audio_effect.h>
40 #include <hardware/audio_alsaops.h>
41 #include <audio_effects/effect_aec.h>
42 
43 #include <sys/ioctl.h>
44 #include <linux/audio_hifi.h>
45 
46 #define CARD_OUT 0
47 #define PORT_CODEC 0
48 /* Minimum granularity - Arbitrary but small value */
49 #define CODEC_BASE_FRAME_COUNT 32
50 
51 /* number of base blocks in a short period (low latency) */
52 #define PERIOD_MULTIPLIER 32  /* 21 ms */
53 /* number of frames per short period (low latency) */
54 #define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER)
55 /* number of pseudo periods for low latency playback */
56 #define PLAYBACK_PERIOD_COUNT 4
57 #define PLAYBACK_PERIOD_START_THRESHOLD 2
58 #define CODEC_SAMPLING_RATE 48000
59 #define CHANNEL_STEREO 2
60 #define MIN_WRITE_SLEEP_US      5000
61 
62 struct stub_stream_in {
63     struct audio_stream_in stream;
64 };
65 
66 struct alsa_audio_device {
67     struct audio_hw_device hw_device;
68 
69     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
70     int devices;
71     struct alsa_stream_in *active_input;
72     struct alsa_stream_out *active_output;
73     bool mic_mute;
74     int hifi_dsp_fd;
75 };
76 
77 struct alsa_stream_out {
78     struct audio_stream_out stream;
79 
80     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
81     struct pcm_config config;
82     struct pcm *pcm;
83     bool unavailable;
84     int standby;
85     struct alsa_audio_device *dev;
86     int write_threshold;
87     unsigned int written;
88 };
89 
90 
91 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct alsa_stream_out * out)92 static int start_output_stream(struct alsa_stream_out *out)
93 {
94     struct alsa_audio_device *adev = out->dev;
95 
96     if (out->unavailable)
97         return -ENODEV;
98 
99     /* default to low power: will be corrected in out_write if necessary before first write to
100      * tinyalsa.
101      */
102     out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE;
103     out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE;
104     out->config.avail_min = PERIOD_SIZE;
105 
106     out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config);
107 
108     if (!pcm_is_ready(out->pcm)) {
109         ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
110         pcm_close(out->pcm);
111         adev->active_output = NULL;
112         out->unavailable = true;
113         return -ENODEV;
114     }
115 
116     adev->active_output = out;
117     return 0;
118 }
119 
out_get_sample_rate(const struct audio_stream * stream)120 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
121 {
122     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
123     return out->config.rate;
124 }
125 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)126 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
127 {
128     ALOGV("out_set_sample_rate: %d", 0);
129     return -ENOSYS;
130 }
131 
out_get_buffer_size(const struct audio_stream * stream)132 static size_t out_get_buffer_size(const struct audio_stream *stream)
133 {
134     ALOGV("out_get_buffer_size: %d", 4096);
135 
136     /* return the closest majoring multiple of 16 frames, as
137      * audioflinger expects audio buffers to be a multiple of 16 frames */
138     size_t size = PERIOD_SIZE;
139     size = ((size + 15) / 16) * 16;
140     return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
141 }
142 
out_get_channels(const struct audio_stream * stream)143 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
144 {
145     ALOGV("out_get_channels");
146     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
147     return audio_channel_out_mask_from_count(out->config.channels);
148 }
149 
out_get_format(const struct audio_stream * stream)150 static audio_format_t out_get_format(const struct audio_stream *stream)
151 {
152     ALOGV("out_get_format");
153     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
154     return audio_format_from_pcm_format(out->config.format);
155 }
156 
out_set_format(struct audio_stream * stream,audio_format_t format)157 static int out_set_format(struct audio_stream *stream, audio_format_t format)
158 {
159     ALOGV("out_set_format: %d",format);
160     return -ENOSYS;
161 }
162 
do_output_standby(struct alsa_stream_out * out)163 static int do_output_standby(struct alsa_stream_out *out)
164 {
165     struct alsa_audio_device *adev = out->dev;
166 
167     if (!out->standby) {
168         pcm_close(out->pcm);
169         out->pcm = NULL;
170         adev->active_output = NULL;
171         out->standby = 1;
172     }
173     return 0;
174 }
175 
out_standby(struct audio_stream * stream)176 static int out_standby(struct audio_stream *stream)
177 {
178     ALOGV("out_standby");
179     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
180     int status;
181 
182     pthread_mutex_lock(&out->dev->lock);
183     pthread_mutex_lock(&out->lock);
184     status = do_output_standby(out);
185     pthread_mutex_unlock(&out->lock);
186     pthread_mutex_unlock(&out->dev->lock);
187     return status;
188 }
189 
out_dump(const struct audio_stream * stream,int fd)190 static int out_dump(const struct audio_stream *stream, int fd)
191 {
192     ALOGV("out_dump");
193     return 0;
194 }
195 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)196 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
197 {
198     ALOGV("out_set_parameters");
199     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
200     struct alsa_audio_device *adev = out->dev;
201     struct str_parms *parms;
202     char value[32];
203     int ret, val = 0;
204 
205     parms = str_parms_create_str(kvpairs);
206 
207     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
208     if (ret >= 0) {
209         val = atoi(value);
210         pthread_mutex_lock(&adev->lock);
211         pthread_mutex_lock(&out->lock);
212         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
213             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
214             adev->devices |= val;
215         }
216         pthread_mutex_unlock(&out->lock);
217         pthread_mutex_unlock(&adev->lock);
218     }
219 
220     str_parms_destroy(parms);
221     return ret;
222 }
223 
out_get_parameters(const struct audio_stream * stream,const char * keys)224 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
225 {
226     ALOGV("out_get_parameters");
227     return strdup("");
228 }
229 
out_get_latency(const struct audio_stream_out * stream)230 static uint32_t out_get_latency(const struct audio_stream_out *stream)
231 {
232     ALOGV("out_get_latency");
233     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
234     return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
235 }
236 
out_set_volume(struct audio_stream_out * stream,float left,float right)237 static int out_set_volume(struct audio_stream_out *stream, float left,
238         float right)
239 {
240     ALOGV("out_set_volume: Left:%f Right:%f", left, right);
241     return 0;
242 }
243 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)244 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
245         size_t bytes)
246 {
247     int ret;
248     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
249     struct alsa_audio_device *adev = out->dev;
250     size_t frame_size = audio_stream_out_frame_size(stream);
251     size_t out_frames = bytes / frame_size;
252     struct misc_io_pcm_buf_param pcmbuf;
253 
254     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
255      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
256      * mutex
257      */
258     pthread_mutex_lock(&adev->lock);
259     pthread_mutex_lock(&out->lock);
260     if (out->standby) {
261         ret = start_output_stream(out);
262         if (ret != 0) {
263             pthread_mutex_unlock(&adev->lock);
264             goto exit;
265         }
266         out->standby = 0;
267     }
268 
269     pthread_mutex_unlock(&adev->lock);
270 
271     if (adev->hifi_dsp_fd >= 0) {
272         pcmbuf.buf = (uint64_t)buffer;
273         pcmbuf.buf_size = bytes;
274         ret = ioctl(adev->hifi_dsp_fd, HIFI_MISC_IOCTL_PCM_GAIN, &pcmbuf);
275         if (ret) {
276             ALOGV("hifi_dsp: Error buffer processing: %d", errno);
277         }
278     }
279 
280     ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size);
281     if (ret == 0) {
282         out->written += out_frames;
283     }
284 exit:
285     pthread_mutex_unlock(&out->lock);
286 
287     if (ret != 0) {
288         usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
289                 out_get_sample_rate(&stream->common));
290     }
291 
292     return bytes;
293 }
294 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)295 static int out_get_render_position(const struct audio_stream_out *stream,
296         uint32_t *dsp_frames)
297 {
298     *dsp_frames = 0;
299     ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
300     return -EINVAL;
301 }
302 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)303 static int out_get_presentation_position(const struct audio_stream_out *stream,
304                                    uint64_t *frames, struct timespec *timestamp)
305 {
306     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
307     int ret = -1;
308 
309         if (out->pcm) {
310             unsigned int avail;
311             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
312                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
313                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
314                 if (signed_frames >= 0) {
315                     *frames = signed_frames;
316                     ret = 0;
317                 }
318             }
319         }
320 
321     return ret;
322 }
323 
324 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)325 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
326 {
327     ALOGV("out_add_audio_effect: %p", effect);
328     return 0;
329 }
330 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)331 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
332 {
333     ALOGV("out_remove_audio_effect: %p", effect);
334     return 0;
335 }
336 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)337 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
338         int64_t *timestamp)
339 {
340     *timestamp = 0;
341     ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
342     return -EINVAL;
343 }
344 
345 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)346 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
347 {
348     ALOGV("in_get_sample_rate");
349     return 8000;
350 }
351 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)352 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
353 {
354     ALOGV("in_set_sample_rate: %d", rate);
355     return -ENOSYS;
356 }
357 
in_get_buffer_size(const struct audio_stream * stream)358 static size_t in_get_buffer_size(const struct audio_stream *stream)
359 {
360     ALOGV("in_get_buffer_size: %d", 320);
361     return 320;
362 }
363 
in_get_channels(const struct audio_stream * stream)364 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
365 {
366     ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
367     return AUDIO_CHANNEL_IN_MONO;
368 }
369 
in_get_format(const struct audio_stream * stream)370 static audio_format_t in_get_format(const struct audio_stream *stream)
371 {
372     return AUDIO_FORMAT_PCM_16_BIT;
373 }
374 
in_set_format(struct audio_stream * stream,audio_format_t format)375 static int in_set_format(struct audio_stream *stream, audio_format_t format)
376 {
377     return -ENOSYS;
378 }
379 
in_standby(struct audio_stream * stream)380 static int in_standby(struct audio_stream *stream)
381 {
382     return 0;
383 }
384 
in_dump(const struct audio_stream * stream,int fd)385 static int in_dump(const struct audio_stream *stream, int fd)
386 {
387     return 0;
388 }
389 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)390 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
391 {
392     return 0;
393 }
394 
in_get_parameters(const struct audio_stream * stream,const char * keys)395 static char * in_get_parameters(const struct audio_stream *stream,
396         const char *keys)
397 {
398     return strdup("");
399 }
400 
in_set_gain(struct audio_stream_in * stream,float gain)401 static int in_set_gain(struct audio_stream_in *stream, float gain)
402 {
403     return 0;
404 }
405 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)406 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
407         size_t bytes)
408 {
409     ALOGV("in_read: bytes %zu", bytes);
410     /* XXX: fake timing for audio input */
411     usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
412             in_get_sample_rate(&stream->common));
413     memset(buffer, 0, bytes);
414     return bytes;
415 }
416 
in_get_input_frames_lost(struct audio_stream_in * stream)417 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
418 {
419     return 0;
420 }
421 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)422 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
423 {
424     return 0;
425 }
426 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)427 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
428 {
429     return 0;
430 }
431 
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)432 static int adev_open_output_stream(struct audio_hw_device *dev,
433         audio_io_handle_t handle,
434         audio_devices_t devices,
435         audio_output_flags_t flags,
436         struct audio_config *config,
437         struct audio_stream_out **stream_out,
438         const char *address __unused)
439 {
440     ALOGV("adev_open_output_stream...");
441 
442     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
443     struct alsa_stream_out *out;
444     struct pcm_params *params;
445     int ret = 0;
446 
447     params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
448     if (!params)
449         return -ENOSYS;
450 
451     out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
452     if (!out)
453         return -ENOMEM;
454 
455     out->stream.common.get_sample_rate = out_get_sample_rate;
456     out->stream.common.set_sample_rate = out_set_sample_rate;
457     out->stream.common.get_buffer_size = out_get_buffer_size;
458     out->stream.common.get_channels = out_get_channels;
459     out->stream.common.get_format = out_get_format;
460     out->stream.common.set_format = out_set_format;
461     out->stream.common.standby = out_standby;
462     out->stream.common.dump = out_dump;
463     out->stream.common.set_parameters = out_set_parameters;
464     out->stream.common.get_parameters = out_get_parameters;
465     out->stream.common.add_audio_effect = out_add_audio_effect;
466     out->stream.common.remove_audio_effect = out_remove_audio_effect;
467     out->stream.get_latency = out_get_latency;
468     out->stream.set_volume = out_set_volume;
469     out->stream.write = out_write;
470     out->stream.get_render_position = out_get_render_position;
471     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
472     out->stream.get_presentation_position = out_get_presentation_position;
473 
474     out->config.channels = CHANNEL_STEREO;
475     out->config.rate = CODEC_SAMPLING_RATE;
476     out->config.format = PCM_FORMAT_S16_LE;
477     out->config.period_size = PERIOD_SIZE;
478     out->config.period_count = PLAYBACK_PERIOD_COUNT;
479 
480     if (out->config.rate != config->sample_rate ||
481            audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
482                out->config.format !=  pcm_format_from_audio_format(config->format) ) {
483         config->sample_rate = out->config.rate;
484         config->format = audio_format_from_pcm_format(out->config.format);
485         config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
486         ret = -EINVAL;
487     }
488 
489     ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d",
490                 out->config.channels, out->config.rate, out->config.format);
491 
492     out->dev = ladev;
493     out->standby = 1;
494     out->unavailable = false;
495 
496     config->format = out_get_format(&out->stream.common);
497     config->channel_mask = out_get_channels(&out->stream.common);
498     config->sample_rate = out_get_sample_rate(&out->stream.common);
499 
500     *stream_out = &out->stream;
501 
502     /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
503     ret = 0;
504 
505     return ret;
506 }
507 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)508 static void adev_close_output_stream(struct audio_hw_device *dev,
509         struct audio_stream_out *stream)
510 {
511     ALOGV("adev_close_output_stream...");
512     free(stream);
513 }
514 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)515 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
516 {
517     ALOGV("adev_set_parameters");
518     return -ENOSYS;
519 }
520 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)521 static char * adev_get_parameters(const struct audio_hw_device *dev,
522         const char *keys)
523 {
524     ALOGV("adev_get_parameters");
525     return strdup("");
526 }
527 
adev_init_check(const struct audio_hw_device * dev)528 static int adev_init_check(const struct audio_hw_device *dev)
529 {
530     ALOGV("adev_init_check");
531     return 0;
532 }
533 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)534 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
535 {
536     ALOGV("adev_set_voice_volume: %f", volume);
537     return -ENOSYS;
538 }
539 
adev_set_master_volume(struct audio_hw_device * dev,float volume)540 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
541 {
542     ALOGV("adev_set_master_volume: %f", volume);
543     return -ENOSYS;
544 }
545 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)546 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
547 {
548     ALOGV("adev_get_master_volume: %f", *volume);
549     return -ENOSYS;
550 }
551 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)552 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
553 {
554     ALOGV("adev_set_master_mute: %d", muted);
555     return -ENOSYS;
556 }
557 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)558 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
559 {
560     ALOGV("adev_get_master_mute: %d", *muted);
561     return -ENOSYS;
562 }
563 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)564 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
565 {
566     ALOGV("adev_set_mode: %d", mode);
567     return 0;
568 }
569 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)570 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
571 {
572     ALOGV("adev_set_mic_mute: %d",state);
573     return -ENOSYS;
574 }
575 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)576 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
577 {
578     ALOGV("adev_get_mic_mute");
579     return -ENOSYS;
580 }
581 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)582 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
583         const struct audio_config *config)
584 {
585     ALOGV("adev_get_input_buffer_size: %d", 320);
586     return 320;
587 }
588 
adev_open_input_stream(struct audio_hw_device __unused * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address __unused,audio_source_t source __unused)589 static int adev_open_input_stream(struct audio_hw_device __unused *dev,
590         audio_io_handle_t handle,
591         audio_devices_t devices,
592         struct audio_config *config,
593         struct audio_stream_in **stream_in,
594         audio_input_flags_t flags __unused,
595         const char *address __unused,
596         audio_source_t source __unused)
597 {
598     struct stub_stream_in *in;
599 
600     ALOGV("adev_open_input_stream...");
601 
602     in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
603     if (!in)
604         return -ENOMEM;
605 
606     in->stream.common.get_sample_rate = in_get_sample_rate;
607     in->stream.common.set_sample_rate = in_set_sample_rate;
608     in->stream.common.get_buffer_size = in_get_buffer_size;
609     in->stream.common.get_channels = in_get_channels;
610     in->stream.common.get_format = in_get_format;
611     in->stream.common.set_format = in_set_format;
612     in->stream.common.standby = in_standby;
613     in->stream.common.dump = in_dump;
614     in->stream.common.set_parameters = in_set_parameters;
615     in->stream.common.get_parameters = in_get_parameters;
616     in->stream.common.add_audio_effect = in_add_audio_effect;
617     in->stream.common.remove_audio_effect = in_remove_audio_effect;
618     in->stream.set_gain = in_set_gain;
619     in->stream.read = in_read;
620     in->stream.get_input_frames_lost = in_get_input_frames_lost;
621 
622     *stream_in = &in->stream;
623     return 0;
624 }
625 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * in)626 static void adev_close_input_stream(struct audio_hw_device *dev,
627         struct audio_stream_in *in)
628 {
629     ALOGV("adev_close_input_stream...");
630     return;
631 }
632 
adev_dump(const audio_hw_device_t * device,int fd)633 static int adev_dump(const audio_hw_device_t *device, int fd)
634 {
635     ALOGV("adev_dump");
636     return 0;
637 }
638 
adev_close(hw_device_t * device)639 static int adev_close(hw_device_t *device)
640 {
641     struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
642 
643     ALOGV("adev_close");
644     if (adev->hifi_dsp_fd >= 0)
645         close(adev->hifi_dsp_fd);
646     free(device);
647     return 0;
648 }
649 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)650 static int adev_open(const hw_module_t* module, const char* name,
651         hw_device_t** device)
652 {
653     struct alsa_audio_device *adev;
654 
655     ALOGV("adev_open: %s", name);
656 
657     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
658         return -EINVAL;
659 
660     adev = calloc(1, sizeof(struct alsa_audio_device));
661     if (!adev)
662         return -ENOMEM;
663 
664     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
665     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
666     adev->hw_device.common.module = (struct hw_module_t *) module;
667     adev->hw_device.common.close = adev_close;
668     adev->hw_device.init_check = adev_init_check;
669     adev->hw_device.set_voice_volume = adev_set_voice_volume;
670     adev->hw_device.set_master_volume = adev_set_master_volume;
671     adev->hw_device.get_master_volume = adev_get_master_volume;
672     adev->hw_device.set_master_mute = adev_set_master_mute;
673     adev->hw_device.get_master_mute = adev_get_master_mute;
674     adev->hw_device.set_mode = adev_set_mode;
675     adev->hw_device.set_mic_mute = adev_set_mic_mute;
676     adev->hw_device.get_mic_mute = adev_get_mic_mute;
677     adev->hw_device.set_parameters = adev_set_parameters;
678     adev->hw_device.get_parameters = adev_get_parameters;
679     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
680     adev->hw_device.open_output_stream = adev_open_output_stream;
681     adev->hw_device.close_output_stream = adev_close_output_stream;
682     adev->hw_device.open_input_stream = adev_open_input_stream;
683     adev->hw_device.close_input_stream = adev_close_input_stream;
684     adev->hw_device.dump = adev_dump;
685 
686     adev->devices = AUDIO_DEVICE_NONE;
687 
688     *device = &adev->hw_device.common;
689 
690     adev->hifi_dsp_fd = open(HIFI_DSP_MISC_DRIVER, O_WRONLY, 0);
691     if (adev->hifi_dsp_fd < 0) {
692         ALOGW("hifi_dsp: Error opening device %d", errno);
693     } else {
694         ALOGI("hifi_dsp: Open device");
695     }
696     return 0;
697 }
698 
699 static struct hw_module_methods_t hal_module_methods = {
700     .open = adev_open,
701 };
702 
703 struct audio_module HAL_MODULE_INFO_SYM = {
704     .common = {
705         .tag = HARDWARE_MODULE_TAG,
706         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
707         .hal_api_version = HARDWARE_HAL_API_VERSION,
708         .id = AUDIO_HARDWARE_MODULE_ID,
709         .name = "Hikey audio HW HAL",
710         .author = "The Android Open Source Project",
711         .methods = &hal_module_methods,
712     },
713 };
714