| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup hdaudio
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file High Definition Audio PCM interface
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <async.h>
|
|---|
| 36 | #include <audio_pcm_iface.h>
|
|---|
| 37 | #include <ddf/log.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <pcm/sample_format.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "codec.h"
|
|---|
| 43 | #include "hdactl.h"
|
|---|
| 44 | #include "hdaudio.h"
|
|---|
| 45 | #include "pcm_iface.h"
|
|---|
| 46 | #include "spec/fmt.h"
|
|---|
| 47 | #include "stream.h"
|
|---|
| 48 |
|
|---|
| 49 | static int hda_get_info_str(ddf_fun_t *, const char **);
|
|---|
| 50 | static unsigned hda_query_cap(ddf_fun_t *, audio_cap_t);
|
|---|
| 51 | static int hda_test_format(ddf_fun_t *, unsigned *, unsigned *,
|
|---|
| 52 | pcm_sample_format_t *);
|
|---|
| 53 | static int hda_get_buffer(ddf_fun_t *, void **, size_t *);
|
|---|
| 54 | static int hda_get_buffer_position(ddf_fun_t *, size_t *);
|
|---|
| 55 | static int hda_set_event_session(ddf_fun_t *, async_sess_t *);
|
|---|
| 56 | static async_sess_t *hda_get_event_session(ddf_fun_t *);
|
|---|
| 57 | static int hda_release_buffer(ddf_fun_t *);
|
|---|
| 58 | static int hda_start_playback(ddf_fun_t *, unsigned, unsigned, unsigned,
|
|---|
| 59 | pcm_sample_format_t);
|
|---|
| 60 | static int hda_stop_playback(ddf_fun_t *, bool);
|
|---|
| 61 | static int hda_start_capture(ddf_fun_t *, unsigned, unsigned, unsigned,
|
|---|
| 62 | pcm_sample_format_t);
|
|---|
| 63 | static int hda_stop_capture(ddf_fun_t *, bool);
|
|---|
| 64 |
|
|---|
| 65 | audio_pcm_iface_t hda_pcm_iface = {
|
|---|
| 66 | .get_info_str = hda_get_info_str,
|
|---|
| 67 | .test_format = hda_test_format,
|
|---|
| 68 | .query_cap = hda_query_cap,
|
|---|
| 69 |
|
|---|
| 70 | .get_buffer = hda_get_buffer,
|
|---|
| 71 | .release_buffer = hda_release_buffer,
|
|---|
| 72 | .set_event_session = hda_set_event_session,
|
|---|
| 73 | .get_event_session = hda_get_event_session,
|
|---|
| 74 | .get_buffer_pos = hda_get_buffer_position,
|
|---|
| 75 |
|
|---|
| 76 | .start_playback = hda_start_playback,
|
|---|
| 77 | .stop_playback = hda_stop_playback,
|
|---|
| 78 |
|
|---|
| 79 | .start_capture = hda_start_capture,
|
|---|
| 80 | .stop_capture = hda_stop_capture
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | enum {
|
|---|
| 84 | max_buffer_size = 65536 /* XXX this is completely arbitrary */
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | static hda_t *fun_to_hda(ddf_fun_t *fun)
|
|---|
| 88 | {
|
|---|
| 89 | return (hda_t *)ddf_dev_data_get(ddf_fun_get_dev(fun));
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | static int hda_get_info_str(ddf_fun_t *fun, const char **name)
|
|---|
| 93 | {
|
|---|
| 94 | ddf_msg(LVL_NOTE, "hda_get_info_str()");
|
|---|
| 95 | if (name)
|
|---|
| 96 | *name = "High Definition Audio";
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static unsigned hda_query_cap(ddf_fun_t *fun, audio_cap_t cap)
|
|---|
| 101 | {
|
|---|
| 102 | ddf_msg(LVL_NOTE, "hda_query_cap(%d)", cap);
|
|---|
| 103 | switch (cap) {
|
|---|
| 104 | case AUDIO_CAP_PLAYBACK:
|
|---|
| 105 | case AUDIO_CAP_INTERRUPT:
|
|---|
| 106 | return 1;
|
|---|
| 107 | case AUDIO_CAP_BUFFER_POS:
|
|---|
| 108 | case AUDIO_CAP_CAPTURE:
|
|---|
| 109 | return 0;
|
|---|
| 110 | case AUDIO_CAP_MAX_BUFFER:
|
|---|
| 111 | return max_buffer_size;
|
|---|
| 112 | case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
|
|---|
| 113 | return 128;
|
|---|
| 114 | case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
|
|---|
| 115 | return 16384;
|
|---|
| 116 | default:
|
|---|
| 117 | return ENOTSUP;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static int hda_test_format(ddf_fun_t *fun, unsigned *channels,
|
|---|
| 122 | unsigned *rate, pcm_sample_format_t *format)
|
|---|
| 123 | {
|
|---|
| 124 | int rc = EOK;
|
|---|
| 125 |
|
|---|
| 126 | ddf_msg(LVL_NOTE, "hda_test_format(%u, %u, %d)\n",
|
|---|
| 127 | *channels, *rate, *format);
|
|---|
| 128 |
|
|---|
| 129 | if (*channels != 1) {
|
|---|
| 130 | *channels = 2;
|
|---|
| 131 | rc = ELIMIT;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (*format != PCM_SAMPLE_SINT16_LE) {
|
|---|
| 135 | *format = PCM_SAMPLE_SINT16_LE;
|
|---|
| 136 | rc = ELIMIT;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | if (*rate != 44100) {
|
|---|
| 140 | *rate = 44100;
|
|---|
| 141 | rc = ELIMIT;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return rc;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | static int hda_get_buffer(ddf_fun_t *fun, void **buffer, size_t *size)
|
|---|
| 148 | {
|
|---|
| 149 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 150 |
|
|---|
| 151 | ddf_msg(LVL_NOTE, "hda_get_buffer(): hda=%p", hda);
|
|---|
| 152 | if (hda->pcm_stream != NULL)
|
|---|
| 153 | return EBUSY;
|
|---|
| 154 |
|
|---|
| 155 | /* XXX Choose appropriate parameters */
|
|---|
| 156 | uint32_t fmt;
|
|---|
| 157 | /* 48 kHz, 16-bits, 1 channel */
|
|---|
| 158 | fmt = (fmt_base_44khz << fmt_base) | (fmt_bits_16 << fmt_bits_l) | 1;
|
|---|
| 159 |
|
|---|
| 160 | ddf_msg(LVL_NOTE, "hda_get_buffer() - create stream");
|
|---|
| 161 | hda->pcm_stream = hda_stream_create(hda, sdir_output, fmt);
|
|---|
| 162 | if (hda->pcm_stream == NULL)
|
|---|
| 163 | return EIO;
|
|---|
| 164 |
|
|---|
| 165 | ddf_msg(LVL_NOTE, "hda_get_buffer() - fill info");
|
|---|
| 166 | /* XXX This is only one buffer */
|
|---|
| 167 | *buffer = hda->pcm_stream->buf[0];
|
|---|
| 168 | *size = hda->pcm_stream->bufsize * hda->pcm_stream->nbuffers;
|
|---|
| 169 |
|
|---|
| 170 | ddf_msg(LVL_NOTE, "hda_get_buffer() retturing EOK, buffer=%p, size=%zu",
|
|---|
| 171 | *buffer, *size);
|
|---|
| 172 | return EOK;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | static int hda_get_buffer_position(ddf_fun_t *fun, size_t *pos)
|
|---|
| 176 | {
|
|---|
| 177 | ddf_msg(LVL_NOTE, "hda_get_buffer_position()");
|
|---|
| 178 | return ENOTSUP;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static int hda_set_event_session(ddf_fun_t *fun, async_sess_t *sess)
|
|---|
| 182 | {
|
|---|
| 183 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 184 |
|
|---|
| 185 | ddf_msg(LVL_NOTE, "hda_set_event_session()");
|
|---|
| 186 | hda->ev_sess = sess;
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static async_sess_t *hda_get_event_session(ddf_fun_t *fun)
|
|---|
| 191 | {
|
|---|
| 192 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 193 |
|
|---|
| 194 | ddf_msg(LVL_NOTE, "hda_get_event_session()");
|
|---|
| 195 | return hda->ev_sess;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static int hda_release_buffer(ddf_fun_t *fun)
|
|---|
| 199 | {
|
|---|
| 200 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 201 |
|
|---|
| 202 | ddf_msg(LVL_NOTE, "hda_release_buffer()");
|
|---|
| 203 | if (hda->pcm_stream == NULL)
|
|---|
| 204 | return EINVAL;
|
|---|
| 205 |
|
|---|
| 206 | hda_stream_destroy(hda->pcm_stream);
|
|---|
| 207 | hda->pcm_stream = NULL;
|
|---|
| 208 | return EOK;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | static int hda_start_playback(ddf_fun_t *fun, unsigned frames,
|
|---|
| 212 | unsigned channels, unsigned rate, pcm_sample_format_t format)
|
|---|
| 213 | {
|
|---|
| 214 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 215 | int rc;
|
|---|
| 216 |
|
|---|
| 217 | ddf_msg(LVL_NOTE, "hda_start_playback()");
|
|---|
| 218 |
|
|---|
| 219 | rc = hda_out_converter_setup(hda->ctl->codec, hda->pcm_stream);
|
|---|
| 220 | if (rc != EOK)
|
|---|
| 221 | return rc;
|
|---|
| 222 |
|
|---|
| 223 | async_usleep(1000*1000);
|
|---|
| 224 | hda_stream_start(hda->pcm_stream);
|
|---|
| 225 | return EOK;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | static int hda_stop_playback(ddf_fun_t *fun, bool immediate)
|
|---|
| 229 | {
|
|---|
| 230 | hda_t *hda = fun_to_hda(fun);
|
|---|
| 231 |
|
|---|
| 232 | ddf_msg(LVL_NOTE, "hda_stop_playback()");
|
|---|
| 233 | hda_stream_stop(hda->pcm_stream);
|
|---|
| 234 | hda_stream_reset(hda->pcm_stream);
|
|---|
| 235 |
|
|---|
| 236 | hda_pcm_event(hda, PCM_EVENT_PLAYBACK_TERMINATED);
|
|---|
| 237 | return EOK;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | static int hda_start_capture(ddf_fun_t *fun, unsigned frames, unsigned channels,
|
|---|
| 241 | unsigned rate, pcm_sample_format_t format)
|
|---|
| 242 | {
|
|---|
| 243 | ddf_msg(LVL_NOTE, "hda_start_capture()");
|
|---|
| 244 | return ENOTSUP;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | static int hda_stop_capture(ddf_fun_t *fun, bool immediate)
|
|---|
| 248 | {
|
|---|
| 249 | ddf_msg(LVL_NOTE, "hda_stop_capture()");
|
|---|
| 250 | return ENOTSUP;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | void hda_pcm_event(hda_t *hda, pcm_event_t event)
|
|---|
| 254 | {
|
|---|
| 255 | async_exch_t *exchange;
|
|---|
| 256 |
|
|---|
| 257 | if (hda->ev_sess == NULL) {
|
|---|
| 258 | if (0) ddf_log_warning("No one listening for event %u", event);
|
|---|
| 259 | return;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | exchange = async_exchange_begin(hda->ev_sess);
|
|---|
| 263 | async_msg_1(exchange, event, 0);
|
|---|
| 264 | async_exchange_end(exchange);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** @}
|
|---|
| 268 | */
|
|---|