1 | /*
|
---|
2 | * Copyright (c) 2022 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 errno_t hda_get_info_str(ddf_fun_t *, const char **);
|
---|
50 | static unsigned hda_query_cap(ddf_fun_t *, audio_cap_t);
|
---|
51 | static errno_t hda_test_format(ddf_fun_t *, unsigned *, unsigned *,
|
---|
52 | pcm_sample_format_t *);
|
---|
53 | static errno_t hda_get_buffer(ddf_fun_t *, void **, size_t *);
|
---|
54 | static errno_t hda_get_buffer_position(ddf_fun_t *, size_t *);
|
---|
55 | static errno_t hda_set_event_session(ddf_fun_t *, async_sess_t *);
|
---|
56 | static async_sess_t *hda_get_event_session(ddf_fun_t *);
|
---|
57 | static errno_t hda_release_buffer(ddf_fun_t *);
|
---|
58 | static errno_t hda_start_playback(ddf_fun_t *, unsigned, unsigned, unsigned,
|
---|
59 | pcm_sample_format_t);
|
---|
60 | static errno_t hda_stop_playback(ddf_fun_t *, bool);
|
---|
61 | static errno_t hda_start_capture(ddf_fun_t *, unsigned, unsigned, unsigned,
|
---|
62 | pcm_sample_format_t);
|
---|
63 | static errno_t 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 errno_t hda_get_info_str(ddf_fun_t *fun, const char **name)
|
---|
93 | {
|
---|
94 | ddf_msg(LVL_DEBUG, "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 | hda_t *hda = fun_to_hda(fun);
|
---|
103 |
|
---|
104 | ddf_msg(LVL_DEBUG, "hda_query_cap(%d)", cap);
|
---|
105 | switch (cap) {
|
---|
106 | case AUDIO_CAP_PLAYBACK:
|
---|
107 | case AUDIO_CAP_INTERRUPT:
|
---|
108 | /* XXX Only if we have an output converter */
|
---|
109 | return 1;
|
---|
110 | case AUDIO_CAP_CAPTURE:
|
---|
111 | /* Yes if we have an input converter */
|
---|
112 | return hda->ctl->codec->in_aw >= 0;
|
---|
113 | case AUDIO_CAP_BUFFER_POS:
|
---|
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:
|
---|
122 | return -1;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | static errno_t hda_test_format(ddf_fun_t *fun, unsigned *channels,
|
---|
127 | unsigned *rate, pcm_sample_format_t *format)
|
---|
128 | {
|
---|
129 | errno_t rc = EOK;
|
---|
130 |
|
---|
131 | ddf_msg(LVL_DEBUG, "hda_test_format(%u, %u, %d)\n",
|
---|
132 | *channels, *rate, *format);
|
---|
133 |
|
---|
134 | if (*channels != 2) {
|
---|
135 | *channels = 2;
|
---|
136 | rc = ELIMIT;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (*format != PCM_SAMPLE_SINT16_LE) {
|
---|
140 | *format = PCM_SAMPLE_SINT16_LE;
|
---|
141 | rc = ELIMIT;
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (*rate != 44100) {
|
---|
145 | *rate = 44100;
|
---|
146 | rc = ELIMIT;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static errno_t hda_get_buffer(ddf_fun_t *fun, void **buffer, size_t *size)
|
---|
153 | {
|
---|
154 | hda_t *hda = fun_to_hda(fun);
|
---|
155 | errno_t rc;
|
---|
156 |
|
---|
157 | hda_lock(hda);
|
---|
158 |
|
---|
159 | ddf_msg(LVL_DEBUG, "hda_get_buffer(): hda=%p", hda);
|
---|
160 | if (hda->pcm_buffers != NULL) {
|
---|
161 | hda_unlock(hda);
|
---|
162 | return EBUSY;
|
---|
163 | }
|
---|
164 |
|
---|
165 | ddf_msg(LVL_DEBUG, "hda_get_buffer() - allocate stream buffers");
|
---|
166 | rc = hda_stream_buffers_alloc(hda, &hda->pcm_buffers);
|
---|
167 | if (rc != EOK) {
|
---|
168 | assert(rc == ENOMEM);
|
---|
169 | hda_unlock(hda);
|
---|
170 | return ENOMEM;
|
---|
171 | }
|
---|
172 |
|
---|
173 | ddf_msg(LVL_DEBUG, "hda_get_buffer() - fill info");
|
---|
174 | /* XXX This is only one buffer */
|
---|
175 | *buffer = hda->pcm_buffers->buf[0];
|
---|
176 | *size = hda->pcm_buffers->bufsize * hda->pcm_buffers->nbuffers;
|
---|
177 |
|
---|
178 | ddf_msg(LVL_DEBUG, "hda_get_buffer() returing EOK, buffer=%p, size=%zu",
|
---|
179 | *buffer, *size);
|
---|
180 |
|
---|
181 | hda_unlock(hda);
|
---|
182 | return EOK;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static errno_t hda_get_buffer_position(ddf_fun_t *fun, size_t *pos)
|
---|
186 | {
|
---|
187 | ddf_msg(LVL_DEBUG, "hda_get_buffer_position()");
|
---|
188 | return ENOTSUP;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static errno_t hda_set_event_session(ddf_fun_t *fun, async_sess_t *sess)
|
---|
192 | {
|
---|
193 | hda_t *hda = fun_to_hda(fun);
|
---|
194 |
|
---|
195 | ddf_msg(LVL_DEBUG, "hda_set_event_session()");
|
---|
196 | hda_lock(hda);
|
---|
197 | hda->ev_sess = sess;
|
---|
198 | hda_unlock(hda);
|
---|
199 |
|
---|
200 | return EOK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | static async_sess_t *hda_get_event_session(ddf_fun_t *fun)
|
---|
204 | {
|
---|
205 | hda_t *hda = fun_to_hda(fun);
|
---|
206 | async_sess_t *sess;
|
---|
207 |
|
---|
208 | ddf_msg(LVL_DEBUG, "hda_get_event_session()");
|
---|
209 |
|
---|
210 | hda_lock(hda);
|
---|
211 | sess = hda->ev_sess;
|
---|
212 | hda_unlock(hda);
|
---|
213 |
|
---|
214 | return sess;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static errno_t hda_release_buffer(ddf_fun_t *fun)
|
---|
218 | {
|
---|
219 | hda_t *hda = fun_to_hda(fun);
|
---|
220 |
|
---|
221 | hda_lock(hda);
|
---|
222 |
|
---|
223 | ddf_msg(LVL_DEBUG, "hda_release_buffer()");
|
---|
224 | if (hda->pcm_buffers == NULL) {
|
---|
225 | hda_unlock(hda);
|
---|
226 | return EINVAL;
|
---|
227 | }
|
---|
228 |
|
---|
229 | hda_stream_buffers_free(hda->pcm_buffers);
|
---|
230 | hda->pcm_buffers = NULL;
|
---|
231 |
|
---|
232 | hda_unlock(hda);
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static errno_t hda_start_playback(ddf_fun_t *fun, unsigned frames,
|
---|
237 | unsigned channels, unsigned rate, pcm_sample_format_t format)
|
---|
238 | {
|
---|
239 | hda_t *hda = fun_to_hda(fun);
|
---|
240 | errno_t rc;
|
---|
241 |
|
---|
242 | ddf_msg(LVL_DEBUG, "hda_start_playback()");
|
---|
243 | hda_lock(hda);
|
---|
244 |
|
---|
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 |
|
---|
263 | rc = hda_out_converter_setup(hda->ctl->codec, hda->pcm_stream);
|
---|
264 | if (rc != EOK) {
|
---|
265 | hda_stream_destroy(hda->pcm_stream);
|
---|
266 | hda->pcm_stream = NULL;
|
---|
267 | hda_unlock(hda);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | hda->playing = true;
|
---|
272 | hda_stream_start(hda->pcm_stream);
|
---|
273 | hda_unlock(hda);
|
---|
274 | return EOK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | static errno_t hda_stop_playback(ddf_fun_t *fun, bool immediate)
|
---|
278 | {
|
---|
279 | hda_t *hda = fun_to_hda(fun);
|
---|
280 |
|
---|
281 | ddf_msg(LVL_DEBUG, "hda_stop_playback()");
|
---|
282 | hda_lock(hda);
|
---|
283 | hda_stream_stop(hda->pcm_stream);
|
---|
284 | hda_stream_reset(hda->pcm_stream);
|
---|
285 | hda->playing = false;
|
---|
286 | hda_stream_destroy(hda->pcm_stream);
|
---|
287 | hda->pcm_stream = NULL;
|
---|
288 |
|
---|
289 | hda_unlock(hda);
|
---|
290 |
|
---|
291 | hda_pcm_event(hda, PCM_EVENT_PLAYBACK_TERMINATED);
|
---|
292 | return EOK;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static errno_t hda_start_capture(ddf_fun_t *fun, unsigned frames, unsigned channels,
|
---|
296 | unsigned rate, pcm_sample_format_t format)
|
---|
297 | {
|
---|
298 | hda_t *hda = fun_to_hda(fun);
|
---|
299 | errno_t rc;
|
---|
300 |
|
---|
301 | ddf_msg(LVL_DEBUG, "hda_start_capture()");
|
---|
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 |
|
---|
314 | ddf_msg(LVL_DEBUG, "hda_start_capture() - create input stream");
|
---|
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;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static errno_t hda_stop_capture(ddf_fun_t *fun, bool immediate)
|
---|
337 | {
|
---|
338 | hda_t *hda = fun_to_hda(fun);
|
---|
339 |
|
---|
340 | ddf_msg(LVL_DEBUG, "hda_stop_capture()");
|
---|
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;
|
---|
351 | }
|
---|
352 |
|
---|
353 | void hda_pcm_event(hda_t *hda, pcm_event_t event)
|
---|
354 | {
|
---|
355 | async_exch_t *exchange;
|
---|
356 |
|
---|
357 | if (hda->ev_sess == NULL) {
|
---|
358 | if (0)
|
---|
359 | ddf_log_warning("No one listening for event %u", event);
|
---|
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 |
|
---|
368 | /** @}
|
---|
369 | */
|
---|