source: mainline/uspace/drv/audio/hdaudio/pcm_iface.c

Last change on this file was 0d59ea7e, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Multiple HD Audio converters cannot pull from a single stream

We select one arbitrary output converter for out PCM output, similar to what
we do for input.

We filter the 'other' VirtualBox output converters based on rates/formats
being zero.

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[93c3163]1/*
[0d59ea7e]2 * Copyright (c) 2022 Jiri Svoboda
[93c3163]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
[99cb9bf]42#include "codec.h"
43#include "hdactl.h"
44#include "hdaudio.h"
[159c722d]45#include "pcm_iface.h"
[99cb9bf]46#include "spec/fmt.h"
47#include "stream.h"
48
[b7fd2a0]49static errno_t hda_get_info_str(ddf_fun_t *, const char **);
[93c3163]50static unsigned hda_query_cap(ddf_fun_t *, audio_cap_t);
[b7fd2a0]51static errno_t hda_test_format(ddf_fun_t *, unsigned *, unsigned *,
[93c3163]52 pcm_sample_format_t *);
[b7fd2a0]53static errno_t hda_get_buffer(ddf_fun_t *, void **, size_t *);
54static errno_t hda_get_buffer_position(ddf_fun_t *, size_t *);
55static errno_t hda_set_event_session(ddf_fun_t *, async_sess_t *);
[93c3163]56static async_sess_t *hda_get_event_session(ddf_fun_t *);
[b7fd2a0]57static errno_t hda_release_buffer(ddf_fun_t *);
58static errno_t hda_start_playback(ddf_fun_t *, unsigned, unsigned, unsigned,
[93c3163]59 pcm_sample_format_t);
[b7fd2a0]60static errno_t hda_stop_playback(ddf_fun_t *, bool);
61static errno_t hda_start_capture(ddf_fun_t *, unsigned, unsigned, unsigned,
[93c3163]62 pcm_sample_format_t);
[b7fd2a0]63static errno_t hda_stop_capture(ddf_fun_t *, bool);
[93c3163]64
65audio_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
[99cb9bf]83enum {
84 max_buffer_size = 65536 /* XXX this is completely arbitrary */
85};
86
87static 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
[b7fd2a0]92static errno_t hda_get_info_str(ddf_fun_t *fun, const char **name)
[93c3163]93{
[cf78637]94 ddf_msg(LVL_DEBUG, "hda_get_info_str()");
[99cb9bf]95 if (name)
96 *name = "High Definition Audio";
97 return EOK;
[93c3163]98}
99
100static unsigned hda_query_cap(ddf_fun_t *fun, audio_cap_t cap)
101{
[0e4c5f0]102 hda_t *hda = fun_to_hda(fun);
103
[cf78637]104 ddf_msg(LVL_DEBUG, "hda_query_cap(%d)", cap);
[99cb9bf]105 switch (cap) {
106 case AUDIO_CAP_PLAYBACK:
107 case AUDIO_CAP_INTERRUPT:
[0e4c5f0]108 /* XXX Only if we have an output converter */
[99cb9bf]109 return 1;
110 case AUDIO_CAP_CAPTURE:
[0e4c5f0]111 /* Yes if we have an input converter */
112 return hda->ctl->codec->in_aw >= 0;
113 case AUDIO_CAP_BUFFER_POS:
[99cb9bf]114 return 0;
115 case AUDIO_CAP_MAX_BUFFER:
116 return max_buffer_size;
117 case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
118 return 128;
119 case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
120 return 16384;
121 default:
[d5c1051]122 return -1;
[99cb9bf]123 }
[93c3163]124}
125
[b7fd2a0]126static errno_t hda_test_format(ddf_fun_t *fun, unsigned *channels,
[93c3163]127 unsigned *rate, pcm_sample_format_t *format)
128{
[b7fd2a0]129 errno_t rc = EOK;
[99cb9bf]130
[cf78637]131 ddf_msg(LVL_DEBUG, "hda_test_format(%u, %u, %d)\n",
[99cb9bf]132 *channels, *rate, *format);
133
[0d59ea7e]134 if (*channels != 2) {
[2f6b916]135 *channels = 2;
[99cb9bf]136 rc = ELIMIT;
137 }
138
139 if (*format != PCM_SAMPLE_SINT16_LE) {
140 *format = PCM_SAMPLE_SINT16_LE;
141 rc = ELIMIT;
142 }
143
[2f6b916]144 if (*rate != 44100) {
145 *rate = 44100;
[99cb9bf]146 rc = ELIMIT;
147 }
148
149 return rc;
[93c3163]150}
151
[b7fd2a0]152static errno_t hda_get_buffer(ddf_fun_t *fun, void **buffer, size_t *size)
[93c3163]153{
[99cb9bf]154 hda_t *hda = fun_to_hda(fun);
[b7fd2a0]155 errno_t rc;
[99cb9bf]156
[57a2208]157 hda_lock(hda);
158
[cf78637]159 ddf_msg(LVL_DEBUG, "hda_get_buffer(): hda=%p", hda);
[0e4c5f0]160 if (hda->pcm_buffers != NULL) {
[57a2208]161 hda_unlock(hda);
[99cb9bf]162 return EBUSY;
[57a2208]163 }
[99cb9bf]164
[cf78637]165 ddf_msg(LVL_DEBUG, "hda_get_buffer() - allocate stream buffers");
[0e4c5f0]166 rc = hda_stream_buffers_alloc(hda, &hda->pcm_buffers);
167 if (rc != EOK) {
168 assert(rc == ENOMEM);
[57a2208]169 hda_unlock(hda);
[0e4c5f0]170 return ENOMEM;
[57a2208]171 }
[99cb9bf]172
[cf78637]173 ddf_msg(LVL_DEBUG, "hda_get_buffer() - fill info");
[99cb9bf]174 /* XXX This is only one buffer */
[0e4c5f0]175 *buffer = hda->pcm_buffers->buf[0];
176 *size = hda->pcm_buffers->bufsize * hda->pcm_buffers->nbuffers;
[99cb9bf]177
[cf78637]178 ddf_msg(LVL_DEBUG, "hda_get_buffer() returing EOK, buffer=%p, size=%zu",
[99cb9bf]179 *buffer, *size);
[57a2208]180
181 hda_unlock(hda);
[99cb9bf]182 return EOK;
[93c3163]183}
184
[b7fd2a0]185static errno_t hda_get_buffer_position(ddf_fun_t *fun, size_t *pos)
[93c3163]186{
[cf78637]187 ddf_msg(LVL_DEBUG, "hda_get_buffer_position()");
[93c3163]188 return ENOTSUP;
189}
190
[b7fd2a0]191static errno_t hda_set_event_session(ddf_fun_t *fun, async_sess_t *sess)
[93c3163]192{
[159c722d]193 hda_t *hda = fun_to_hda(fun);
194
[cf78637]195 ddf_msg(LVL_DEBUG, "hda_set_event_session()");
[57a2208]196 hda_lock(hda);
[159c722d]197 hda->ev_sess = sess;
[57a2208]198 hda_unlock(hda);
199
[159c722d]200 return EOK;
[93c3163]201}
202
203static async_sess_t *hda_get_event_session(ddf_fun_t *fun)
204{
[159c722d]205 hda_t *hda = fun_to_hda(fun);
[57a2208]206 async_sess_t *sess;
[159c722d]207
[cf78637]208 ddf_msg(LVL_DEBUG, "hda_get_event_session()");
[57a2208]209
210 hda_lock(hda);
211 sess = hda->ev_sess;
212 hda_unlock(hda);
213
214 return sess;
[93c3163]215}
216
[b7fd2a0]217static errno_t hda_release_buffer(ddf_fun_t *fun)
[93c3163]218{
[99cb9bf]219 hda_t *hda = fun_to_hda(fun);
220
[57a2208]221 hda_lock(hda);
222
[cf78637]223 ddf_msg(LVL_DEBUG, "hda_release_buffer()");
[0e4c5f0]224 if (hda->pcm_buffers == NULL) {
[57a2208]225 hda_unlock(hda);
[99cb9bf]226 return EINVAL;
[57a2208]227 }
[99cb9bf]228
[0e4c5f0]229 hda_stream_buffers_free(hda->pcm_buffers);
[4a5ae542]230 hda->pcm_buffers = NULL;
[57a2208]231
232 hda_unlock(hda);
[99cb9bf]233 return EOK;
[93c3163]234}
235
[b7fd2a0]236static errno_t hda_start_playback(ddf_fun_t *fun, unsigned frames,
[93c3163]237 unsigned channels, unsigned rate, pcm_sample_format_t format)
238{
[99cb9bf]239 hda_t *hda = fun_to_hda(fun);
[b7fd2a0]240 errno_t rc;
[99cb9bf]241
[cf78637]242 ddf_msg(LVL_DEBUG, "hda_start_playback()");
[57a2208]243 hda_lock(hda);
[99cb9bf]244
[0e4c5f0]245 if (hda->pcm_stream != NULL) {
246 hda_unlock(hda);
247 return EBUSY;
248 }
249
250 /* XXX Choose appropriate parameters */
251 uint32_t fmt;
252 /* 48 kHz, 16-bits, 1 channel */
253 fmt = (fmt_base_44khz << fmt_base) | (fmt_bits_16 << fmt_bits_l) | 1;
254
255 ddf_msg(LVL_NOTE, "hda_start_playback() - create output stream");
256 hda->pcm_stream = hda_stream_create(hda, sdir_output, hda->pcm_buffers,
257 fmt);
258 if (hda->pcm_stream == NULL) {
259 hda_unlock(hda);
260 return EIO;
261 }
262
[3fec817]263 rc = hda_out_converter_setup(hda->ctl->codec, hda->pcm_stream);
[57a2208]264 if (rc != EOK) {
[0e4c5f0]265 hda_stream_destroy(hda->pcm_stream);
266 hda->pcm_stream = NULL;
[57a2208]267 hda_unlock(hda);
[99cb9bf]268 return rc;
[57a2208]269 }
[99cb9bf]270
[57a2208]271 hda->playing = true;
[0e4c5f0]272 hda_stream_start(hda->pcm_stream);
[57a2208]273 hda_unlock(hda);
[99cb9bf]274 return EOK;
[93c3163]275}
276
[b7fd2a0]277static errno_t hda_stop_playback(ddf_fun_t *fun, bool immediate)
[93c3163]278{
[6747b929]279 hda_t *hda = fun_to_hda(fun);
280
[cf78637]281 ddf_msg(LVL_DEBUG, "hda_stop_playback()");
[57a2208]282 hda_lock(hda);
[6747b929]283 hda_stream_stop(hda->pcm_stream);
284 hda_stream_reset(hda->pcm_stream);
[57a2208]285 hda->playing = false;
[0e4c5f0]286 hda_stream_destroy(hda->pcm_stream);
287 hda->pcm_stream = NULL;
288
[57a2208]289 hda_unlock(hda);
[6747b929]290
291 hda_pcm_event(hda, PCM_EVENT_PLAYBACK_TERMINATED);
292 return EOK;
[93c3163]293}
294
[b7fd2a0]295static errno_t hda_start_capture(ddf_fun_t *fun, unsigned frames, unsigned channels,
[93c3163]296 unsigned rate, pcm_sample_format_t format)
297{
[0e4c5f0]298 hda_t *hda = fun_to_hda(fun);
[b7fd2a0]299 errno_t rc;
[0e4c5f0]300
[cf78637]301 ddf_msg(LVL_DEBUG, "hda_start_capture()");
[0e4c5f0]302 hda_lock(hda);
303
304 if (hda->pcm_stream != NULL) {
305 hda_unlock(hda);
306 return EBUSY;
307 }
308
309 /* XXX Choose appropriate parameters */
310 uint32_t fmt;
311 /* 48 kHz, 16-bits, 1 channel */
312 fmt = (fmt_base_44khz << fmt_base) | (fmt_bits_16 << fmt_bits_l) | 1;
313
[cf78637]314 ddf_msg(LVL_DEBUG, "hda_start_capture() - create input stream");
[0e4c5f0]315 hda->pcm_stream = hda_stream_create(hda, sdir_input, hda->pcm_buffers,
316 fmt);
317 if (hda->pcm_stream == NULL) {
318 hda_unlock(hda);
319 return EIO;
320 }
321
322 rc = hda_in_converter_setup(hda->ctl->codec, hda->pcm_stream);
323 if (rc != EOK) {
324 hda_stream_destroy(hda->pcm_stream);
325 hda->pcm_stream = NULL;
326 hda_unlock(hda);
327 return rc;
328 }
329
330 hda->capturing = true;
331 hda_stream_start(hda->pcm_stream);
332 hda_unlock(hda);
333 return EOK;
[93c3163]334}
335
[b7fd2a0]336static errno_t hda_stop_capture(ddf_fun_t *fun, bool immediate)
[93c3163]337{
[0e4c5f0]338 hda_t *hda = fun_to_hda(fun);
339
[cf78637]340 ddf_msg(LVL_DEBUG, "hda_stop_capture()");
[0e4c5f0]341 hda_lock(hda);
342 hda_stream_stop(hda->pcm_stream);
343 hda_stream_reset(hda->pcm_stream);
344 hda->capturing = false;
345 hda_stream_destroy(hda->pcm_stream);
346 hda->pcm_stream = NULL;
347 hda_unlock(hda);
348
349 hda_pcm_event(hda, PCM_EVENT_CAPTURE_TERMINATED);
350 return EOK;
[93c3163]351}
352
[159c722d]353void hda_pcm_event(hda_t *hda, pcm_event_t event)
354{
355 async_exch_t *exchange;
356
357 if (hda->ev_sess == NULL) {
[1433ecda]358 if (0)
359 ddf_log_warning("No one listening for event %u", event);
[159c722d]360 return;
361 }
362
363 exchange = async_exchange_begin(hda->ev_sess);
364 async_msg_1(exchange, event, 0);
365 async_exchange_end(exchange);
366}
367
[93c3163]368/** @}
369 */
Note: See TracBrowser for help on using the repository browser.