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 | hda_lock(hda);
|
---|
152 |
|
---|
153 | ddf_msg(LVL_NOTE, "hda_get_buffer(): hda=%p", hda);
|
---|
154 | if (hda->pcm_stream != NULL) {
|
---|
155 | hda_unlock(hda);
|
---|
156 | return EBUSY;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* XXX Choose appropriate parameters */
|
---|
160 | uint32_t fmt;
|
---|
161 | /* 48 kHz, 16-bits, 1 channel */
|
---|
162 | fmt = (fmt_base_44khz << fmt_base) | (fmt_bits_16 << fmt_bits_l) | 1;
|
---|
163 |
|
---|
164 | ddf_msg(LVL_NOTE, "hda_get_buffer() - create stream");
|
---|
165 | hda->pcm_stream = hda_stream_create(hda, sdir_output, fmt);
|
---|
166 | if (hda->pcm_stream == NULL) {
|
---|
167 | hda_unlock(hda);
|
---|
168 | return EIO;
|
---|
169 | }
|
---|
170 |
|
---|
171 | ddf_msg(LVL_NOTE, "hda_get_buffer() - fill info");
|
---|
172 | /* XXX This is only one buffer */
|
---|
173 | *buffer = hda->pcm_stream->buf[0];
|
---|
174 | *size = hda->pcm_stream->bufsize * hda->pcm_stream->nbuffers;
|
---|
175 |
|
---|
176 | ddf_msg(LVL_NOTE, "hda_get_buffer() retturing EOK, buffer=%p, size=%zu",
|
---|
177 | *buffer, *size);
|
---|
178 |
|
---|
179 | hda_unlock(hda);
|
---|
180 | return EOK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | static int hda_get_buffer_position(ddf_fun_t *fun, size_t *pos)
|
---|
184 | {
|
---|
185 | ddf_msg(LVL_NOTE, "hda_get_buffer_position()");
|
---|
186 | return ENOTSUP;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static int hda_set_event_session(ddf_fun_t *fun, async_sess_t *sess)
|
---|
190 | {
|
---|
191 | hda_t *hda = fun_to_hda(fun);
|
---|
192 |
|
---|
193 | ddf_msg(LVL_NOTE, "hda_set_event_session()");
|
---|
194 | hda_lock(hda);
|
---|
195 | hda->ev_sess = sess;
|
---|
196 | hda_unlock(hda);
|
---|
197 |
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static async_sess_t *hda_get_event_session(ddf_fun_t *fun)
|
---|
202 | {
|
---|
203 | hda_t *hda = fun_to_hda(fun);
|
---|
204 | async_sess_t *sess;
|
---|
205 |
|
---|
206 | ddf_msg(LVL_NOTE, "hda_get_event_session()");
|
---|
207 |
|
---|
208 | hda_lock(hda);
|
---|
209 | sess = hda->ev_sess;
|
---|
210 | hda_unlock(hda);
|
---|
211 |
|
---|
212 | return sess;
|
---|
213 | }
|
---|
214 |
|
---|
215 | static int hda_release_buffer(ddf_fun_t *fun)
|
---|
216 | {
|
---|
217 | hda_t *hda = fun_to_hda(fun);
|
---|
218 |
|
---|
219 | hda_lock(hda);
|
---|
220 |
|
---|
221 | ddf_msg(LVL_NOTE, "hda_release_buffer()");
|
---|
222 | if (hda->pcm_stream == NULL) {
|
---|
223 | hda_unlock(hda);
|
---|
224 | return EINVAL;
|
---|
225 | }
|
---|
226 |
|
---|
227 | hda_stream_destroy(hda->pcm_stream);
|
---|
228 | hda->pcm_stream = NULL;
|
---|
229 |
|
---|
230 | hda_unlock(hda);
|
---|
231 | return EOK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | static int hda_start_playback(ddf_fun_t *fun, unsigned frames,
|
---|
235 | unsigned channels, unsigned rate, pcm_sample_format_t format)
|
---|
236 | {
|
---|
237 | hda_t *hda = fun_to_hda(fun);
|
---|
238 | int rc;
|
---|
239 |
|
---|
240 | ddf_msg(LVL_NOTE, "hda_start_playback()");
|
---|
241 | hda_lock(hda);
|
---|
242 |
|
---|
243 | rc = hda_out_converter_setup(hda->ctl->codec, hda->pcm_stream);
|
---|
244 | if (rc != EOK) {
|
---|
245 | hda_unlock(hda);
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 |
|
---|
249 | hda_stream_start(hda->pcm_stream);
|
---|
250 | hda->playing = true;
|
---|
251 | hda_unlock(hda);
|
---|
252 | return EOK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | static int hda_stop_playback(ddf_fun_t *fun, bool immediate)
|
---|
256 | {
|
---|
257 | hda_t *hda = fun_to_hda(fun);
|
---|
258 |
|
---|
259 | ddf_msg(LVL_NOTE, "hda_stop_playback()");
|
---|
260 | hda_lock(hda);
|
---|
261 | hda_stream_stop(hda->pcm_stream);
|
---|
262 | hda_stream_reset(hda->pcm_stream);
|
---|
263 | hda->playing = false;
|
---|
264 | hda_unlock(hda);
|
---|
265 |
|
---|
266 | hda_pcm_event(hda, PCM_EVENT_PLAYBACK_TERMINATED);
|
---|
267 | return EOK;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static int hda_start_capture(ddf_fun_t *fun, unsigned frames, unsigned channels,
|
---|
271 | unsigned rate, pcm_sample_format_t format)
|
---|
272 | {
|
---|
273 | ddf_msg(LVL_NOTE, "hda_start_capture()");
|
---|
274 | return ENOTSUP;
|
---|
275 | }
|
---|
276 |
|
---|
277 | static int hda_stop_capture(ddf_fun_t *fun, bool immediate)
|
---|
278 | {
|
---|
279 | ddf_msg(LVL_NOTE, "hda_stop_capture()");
|
---|
280 | return ENOTSUP;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void hda_pcm_event(hda_t *hda, pcm_event_t event)
|
---|
284 | {
|
---|
285 | async_exch_t *exchange;
|
---|
286 |
|
---|
287 | if (hda->ev_sess == NULL) {
|
---|
288 | if (0) ddf_log_warning("No one listening for event %u", event);
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | exchange = async_exchange_begin(hda->ev_sess);
|
---|
293 | async_msg_1(exchange, event, 0);
|
---|
294 | async_exchange_end(exchange);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** @}
|
---|
298 | */
|
---|