[763444e] | 1 | /*
|
---|
[a64970e1] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[763444e] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 | /** @addtogroup drvaudiosb16
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief DSP helper functions implementation
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[c5cbc1b7] | 36 | #include <as.h>
|
---|
[03362fbd] | 37 | #include <stdbool.h>
|
---|
[d15797d] | 38 | #include <ddf/driver.h>
|
---|
[c5cbc1b7] | 39 | #include <ddi.h>
|
---|
[c885a21] | 40 | #include <device/hw_res.h>
|
---|
[763444e] | 41 | #include <libarch/ddi.h>
|
---|
[05882233] | 42 | #include <barrier.h>
|
---|
[365f259] | 43 | #include <macros.h>
|
---|
[01aef43] | 44 | #include <str_error.h>
|
---|
[1240bb9] | 45 | #include <audio_pcm_iface.h>
|
---|
[0f2e7c1] | 46 |
|
---|
[bf38143] | 47 | #include "ddf_log.h"
|
---|
| 48 | #include "dsp_commands.h"
|
---|
[763444e] | 49 | #include "dsp.h"
|
---|
| 50 |
|
---|
[c5cbc1b7] | 51 | /* Maximum allowed transfer size for ISA DMA transfers is 64kB */
|
---|
[537620a8] | 52 | #define MAX_BUFFER_SIZE (64 * 1024)
|
---|
[bf38143] | 53 |
|
---|
| 54 | #ifndef DSP_RETRY_COUNT
|
---|
| 55 | #define DSP_RETRY_COUNT 100
|
---|
| 56 | #endif
|
---|
| 57 |
|
---|
| 58 | #define DSP_RESET_RESPONSE 0xaa
|
---|
[018ab50] | 59 |
|
---|
| 60 | /* These are only for SB16 (DSP4.00+) */
|
---|
| 61 | #define DSP_RATE_UPPER_LIMIT 44100
|
---|
| 62 | #define DSP_RATE_LOWER_LIMIT 5000
|
---|
[bf38143] | 63 |
|
---|
[5984107] | 64 | #define AUTO_DMA_MODE
|
---|
| 65 |
|
---|
[3bacee1] | 66 | static inline const char *dsp_state_to_str(dsp_state_t state)
|
---|
[fb6c98f] | 67 | {
|
---|
[3bacee1] | 68 | static const char *state_names[] = {
|
---|
[fb6c98f] | 69 | [DSP_PLAYBACK_ACTIVE_EVENTS] = "PLAYBACK w/ EVENTS",
|
---|
[f3fb83a] | 70 | [DSP_CAPTURE_ACTIVE_EVENTS] = "CAPTURE w/ EVENTS",
|
---|
| 71 | [DSP_PLAYBACK_NOEVENTS] = "PLAYBACK w/o EVENTS",
|
---|
[fb6c98f] | 72 | [DSP_CAPTURE_NOEVENTS] = "CAPTURE w/o EVENTS",
|
---|
[03d6e0d] | 73 | [DSP_PLAYBACK_TERMINATE] = "PLAYBACK TERMINATE",
|
---|
| 74 | [DSP_CAPTURE_TERMINATE] = "CAPTURE TERMINATE",
|
---|
[fb6c98f] | 75 | [DSP_READY] = "READY",
|
---|
| 76 | [DSP_NO_BUFFER] = "NO BUFFER",
|
---|
[a64970e1] | 77 | [DSP_QUIESCED] = "QUIESCED"
|
---|
[fb6c98f] | 78 | };
|
---|
[05b59393] | 79 | if ((size_t)state < ARRAY_SIZE(state_names))
|
---|
[fb6c98f] | 80 | return state_names[state];
|
---|
| 81 | return "UNKNOWN";
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[5eed99d] | 84 | static inline void dsp_change_state(sb_dsp_t *dsp, dsp_state_t state)
|
---|
[fb6c98f] | 85 | {
|
---|
| 86 | assert(dsp);
|
---|
[a64970e1] | 87 | if (dsp->state == DSP_QUIESCED)
|
---|
| 88 | return;
|
---|
| 89 |
|
---|
[fb6c98f] | 90 | ddf_log_verbose("Changing state from %s to %s",
|
---|
| 91 | dsp_state_to_str(dsp->state), dsp_state_to_str(state));
|
---|
| 92 | dsp->state = state;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[b7fd2a0] | 95 | static inline errno_t dsp_read(sb_dsp_t *dsp, uint8_t *data)
|
---|
[bf38143] | 96 | {
|
---|
| 97 | assert(data);
|
---|
| 98 | assert(dsp);
|
---|
| 99 | uint8_t status;
|
---|
| 100 | size_t attempts = DSP_RETRY_COUNT;
|
---|
| 101 | do {
|
---|
| 102 | status = pio_read_8(&dsp->regs->dsp_read_status);
|
---|
| 103 | } while (--attempts && ((status & DSP_READ_READY) == 0));
|
---|
| 104 |
|
---|
| 105 | if ((status & DSP_READ_READY) == 0)
|
---|
| 106 | return EIO;
|
---|
| 107 |
|
---|
| 108 | *data = pio_read_8(&dsp->regs->dsp_data_read);
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
[e941bf8] | 111 |
|
---|
[b7fd2a0] | 112 | static inline errno_t dsp_write(sb_dsp_t *dsp, uint8_t data)
|
---|
[bf38143] | 113 | {
|
---|
| 114 | assert(dsp);
|
---|
| 115 | uint8_t status;
|
---|
| 116 | size_t attempts = DSP_RETRY_COUNT;
|
---|
| 117 | do {
|
---|
| 118 | status = pio_read_8(&dsp->regs->dsp_write);
|
---|
| 119 | } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
|
---|
| 120 |
|
---|
| 121 | if ((status & DSP_WRITE_BUSY))
|
---|
| 122 | return EIO;
|
---|
| 123 |
|
---|
| 124 | pio_write_8(&dsp->regs->dsp_write, data);
|
---|
| 125 | return EOK;
|
---|
| 126 | }
|
---|
[e941bf8] | 127 |
|
---|
[5eed99d] | 128 | static inline void dsp_reset(sb_dsp_t *dsp)
|
---|
[bf38143] | 129 | {
|
---|
| 130 | assert(dsp);
|
---|
| 131 | /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
|
---|
| 132 | pio_write_8(&dsp->regs->dsp_reset, 1);
|
---|
| 133 | udelay(3); /* Keep reset for 3 us */
|
---|
| 134 | pio_write_8(&dsp->regs->dsp_reset, 0);
|
---|
[5eed99d] | 135 | /* "DSP takes about 100 microseconds to initialize itself" */
|
---|
| 136 | udelay(100);
|
---|
[bf38143] | 137 | }
|
---|
[e941bf8] | 138 |
|
---|
[5eed99d] | 139 | static inline void dsp_start_current_active(sb_dsp_t *dsp, uint8_t command)
|
---|
[ad42844] | 140 | {
|
---|
[5eed99d] | 141 | dsp_write(dsp, command);
|
---|
| 142 | dsp_write(dsp, dsp->active.mode);
|
---|
| 143 | dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
|
---|
| 144 | dsp_write(dsp, (dsp->active.samples - 1) >> 8);
|
---|
[ad42844] | 145 | }
|
---|
| 146 |
|
---|
[5eed99d] | 147 | static inline void dsp_set_sampling_rate(sb_dsp_t *dsp, unsigned rate)
|
---|
[ad42844] | 148 | {
|
---|
[5eed99d] | 149 | dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
|
---|
[05b59393] | 150 | uint8_t rate_lo = rate & 0xff;
|
---|
| 151 | uint8_t rate_hi = rate >> 8;
|
---|
| 152 | dsp_write(dsp, rate_hi);
|
---|
| 153 | dsp_write(dsp, rate_lo);
|
---|
| 154 | ddf_log_verbose("Sampling rate: %hhx:%hhx.", rate_hi, rate_lo);
|
---|
[ad42844] | 155 | }
|
---|
| 156 |
|
---|
[5eed99d] | 157 | static inline void dsp_report_event(sb_dsp_t *dsp, pcm_event_t event)
|
---|
| 158 | {
|
---|
| 159 | assert(dsp);
|
---|
| 160 | if (!dsp->event_exchange)
|
---|
| 161 | ddf_log_warning("No one listening for event %u", event);
|
---|
| 162 | async_msg_1(dsp->event_exchange, event, dsp->active.frame_count);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[b7fd2a0] | 165 | static inline errno_t setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
|
---|
[c885a21] | 166 | {
|
---|
[d15797d] | 167 | async_sess_t *sess = ddf_dev_parent_sess_get(dsp->sb_dev);
|
---|
[c885a21] | 168 |
|
---|
[53a309e] | 169 | return hw_res_dma_channel_setup(sess,
|
---|
[c885a21] | 170 | dsp->dma16_channel, pa, size,
|
---|
| 171 | DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
|
---|
| 172 | }
|
---|
[e941bf8] | 173 |
|
---|
[b7fd2a0] | 174 | static inline errno_t setup_buffer(sb_dsp_t *dsp, size_t size)
|
---|
[01aef43] | 175 | {
|
---|
| 176 | assert(dsp);
|
---|
[a35b458] | 177 |
|
---|
[8442d10] | 178 | if ((size > MAX_BUFFER_SIZE) || (size == 0) || ((size % 2) == 1))
|
---|
[9b3d999] | 179 | size = MAX_BUFFER_SIZE;
|
---|
[a35b458] | 180 |
|
---|
[8442d10] | 181 | uintptr_t pa = 0;
|
---|
[bf9cb2f] | 182 | void *buffer = AS_AREA_ANY;
|
---|
[a35b458] | 183 |
|
---|
[b7fd2a0] | 184 | errno_t ret = dmamem_map_anonymous(size, DMAMEM_16MiB | 0x0000ffff,
|
---|
[8442d10] | 185 | AS_AREA_WRITE | AS_AREA_READ, 0, &pa, &buffer);
|
---|
[c5cbc1b7] | 186 | if (ret != EOK) {
|
---|
[124f9bd] | 187 | ddf_log_error("Failed to allocate DMA buffer.");
|
---|
[f14e6ea] | 188 | return ENOMEM;
|
---|
| 189 | }
|
---|
[a35b458] | 190 |
|
---|
[8442d10] | 191 | ddf_log_verbose("Setup DMA buffer at %p (%zu) %zu.", buffer, pa, size);
|
---|
| 192 | assert(pa < (1 << 24));
|
---|
[a35b458] | 193 |
|
---|
[c5cbc1b7] | 194 | /* Setup 16 bit channel */
|
---|
[8442d10] | 195 | ret = setup_dma(dsp, pa, size);
|
---|
[01aef43] | 196 | if (ret == EOK) {
|
---|
[dce7e41] | 197 | dsp->buffer.data = buffer;
|
---|
[00006e0] | 198 | dsp->buffer.size = size;
|
---|
[01aef43] | 199 | } else {
|
---|
[124f9bd] | 200 | ddf_log_error("Failed to setup DMA16 channel: %s.",
|
---|
[01aef43] | 201 | str_error(ret));
|
---|
[c5cbc1b7] | 202 | dmamem_unmap_anonymous(buffer);
|
---|
[01aef43] | 203 | }
|
---|
[a35b458] | 204 |
|
---|
[01aef43] | 205 | return ret;
|
---|
| 206 | }
|
---|
[e941bf8] | 207 |
|
---|
[b7fd2a0] | 208 | errno_t sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
|
---|
[c885a21] | 209 | int dma8, int dma16)
|
---|
[bf38143] | 210 | {
|
---|
| 211 | assert(dsp);
|
---|
| 212 | dsp->regs = regs;
|
---|
[c885a21] | 213 | dsp->dma8_channel = dma8;
|
---|
| 214 | dsp->dma16_channel = dma16;
|
---|
[7ca22e5] | 215 | dsp->event_session = NULL;
|
---|
| 216 | dsp->event_exchange = NULL;
|
---|
[c885a21] | 217 | dsp->sb_dev = dev;
|
---|
[fb6c98f] | 218 | dsp->state = DSP_NO_BUFFER;
|
---|
[5eed99d] | 219 | dsp_reset(dsp);
|
---|
[bf38143] | 220 | uint8_t response;
|
---|
[b7fd2a0] | 221 | const errno_t ret = dsp_read(dsp, &response);
|
---|
[bf38143] | 222 | if (ret != EOK) {
|
---|
[124f9bd] | 223 | ddf_log_error("Failed to read DSP reset response value.");
|
---|
[bf38143] | 224 | return ret;
|
---|
| 225 | }
|
---|
| 226 | if (response != DSP_RESET_RESPONSE) {
|
---|
[124f9bd] | 227 | ddf_log_error("Invalid DSP reset response: %x.", response);
|
---|
[bf38143] | 228 | return EIO;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /* Get DSP version number */
|
---|
[5eed99d] | 232 | dsp_write(dsp, DSP_VERSION);
|
---|
| 233 | dsp_read(dsp, &dsp->version.major);
|
---|
| 234 | dsp_read(dsp, &dsp->version.minor);
|
---|
[01aef43] | 235 |
|
---|
| 236 | return ret;
|
---|
[bf38143] | 237 | }
|
---|
[e941bf8] | 238 |
|
---|
[a64970e1] | 239 | void sb_dsp_quiesce(sb_dsp_t *dsp)
|
---|
| 240 | {
|
---|
| 241 | dsp->state = DSP_QUIESCED;
|
---|
| 242 | dsp_reset(dsp);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[f14e6ea] | 245 | void sb_dsp_interrupt(sb_dsp_t *dsp)
|
---|
| 246 | {
|
---|
[bf38143] | 247 | assert(dsp);
|
---|
[ff396ea] | 248 |
|
---|
[57e8b3b] | 249 | dsp->active.frame_count +=
|
---|
| 250 | dsp->active.samples / ((dsp->active.mode & DSP_MODE_STEREO) ? 2 : 1);
|
---|
| 251 |
|
---|
[3bacee1] | 252 | switch (dsp->state) {
|
---|
[fb6c98f] | 253 | case DSP_PLAYBACK_ACTIVE_EVENTS:
|
---|
[0387b92] | 254 | dsp_report_event(dsp, PCM_EVENT_FRAMES_PLAYED);
|
---|
[fb6c98f] | 255 | case DSP_PLAYBACK_NOEVENTS:
|
---|
| 256 | #ifndef AUTO_DMA_MODE
|
---|
[d337f74] | 257 | dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
|
---|
[fb6c98f] | 258 | #endif
|
---|
| 259 | break;
|
---|
| 260 | case DSP_CAPTURE_ACTIVE_EVENTS:
|
---|
[0387b92] | 261 | dsp_report_event(dsp, PCM_EVENT_FRAMES_CAPTURED);
|
---|
[fb6c98f] | 262 | case DSP_CAPTURE_NOEVENTS:
|
---|
| 263 | #ifndef AUTO_DMA_MODE
|
---|
[d337f74] | 264 | dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
|
---|
[fb6c98f] | 265 | #endif
|
---|
| 266 | break;
|
---|
[daed689] | 267 | case DSP_CAPTURE_TERMINATE:
|
---|
[d337f74] | 268 | dsp_change_state(dsp, DSP_READY);
|
---|
[daed689] | 269 | dsp_report_event(dsp, PCM_EVENT_CAPTURE_TERMINATED);
|
---|
| 270 | async_exchange_end(dsp->event_exchange);
|
---|
| 271 | dsp->event_exchange = NULL;
|
---|
| 272 | break;
|
---|
| 273 | case DSP_PLAYBACK_TERMINATE:
|
---|
[d337f74] | 274 | dsp_change_state(dsp, DSP_READY);
|
---|
[daed689] | 275 | dsp_report_event(dsp, PCM_EVENT_PLAYBACK_TERMINATED);
|
---|
| 276 | async_exchange_end(dsp->event_exchange);
|
---|
| 277 | dsp->event_exchange = NULL;
|
---|
| 278 | break;
|
---|
[fb6c98f] | 279 | default:
|
---|
[5b8e08e] | 280 | ddf_log_warning("Interrupt while DSP not active (%s)",
|
---|
| 281 | dsp_state_to_str(dsp->state));
|
---|
[7ca22e5] | 282 | }
|
---|
[01aef43] | 283 | }
|
---|
[e941bf8] | 284 |
|
---|
[f0241bda] | 285 | unsigned sb_dsp_query_cap(sb_dsp_t *dsp, audio_cap_t cap)
|
---|
| 286 | {
|
---|
[37367f7] | 287 | ddf_log_verbose("Querying cap %s", audio_pcm_cap_str(cap));
|
---|
[3bacee1] | 288 | switch (cap) {
|
---|
[d86c9736] | 289 | case AUDIO_CAP_CAPTURE:
|
---|
[f0241bda] | 290 | case AUDIO_CAP_PLAYBACK:
|
---|
| 291 | case AUDIO_CAP_INTERRUPT:
|
---|
[1ba95ba] | 292 | case AUDIO_CAP_BUFFER_POS:
|
---|
[f0241bda] | 293 | return 1;
|
---|
| 294 | case AUDIO_CAP_MAX_BUFFER:
|
---|
| 295 | return MAX_BUFFER_SIZE;
|
---|
| 296 | case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
|
---|
| 297 | return 1;
|
---|
| 298 | case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
|
---|
| 299 | return 16535;
|
---|
| 300 | default:
|
---|
[d5c1051] | 301 | return -1;
|
---|
[f0241bda] | 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[b7fd2a0] | 305 | errno_t sb_dsp_get_buffer_position(sb_dsp_t *dsp, size_t *pos)
|
---|
[1ba95ba] | 306 | {
|
---|
[fb6c98f] | 307 | if (dsp->state == DSP_NO_BUFFER)
|
---|
[1ba95ba] | 308 | return ENOENT;
|
---|
| 309 |
|
---|
[fb6c98f] | 310 | assert(dsp->buffer.data);
|
---|
[d15797d] | 311 | async_sess_t *sess = ddf_dev_parent_sess_get(dsp->sb_dev);
|
---|
[1ba95ba] | 312 |
|
---|
| 313 | // TODO: Assumes DMA 16
|
---|
[c19a5a59] | 314 | size_t remain;
|
---|
[b7fd2a0] | 315 | errno_t rc = hw_res_dma_channel_remain(sess, dsp->dma16_channel, &remain);
|
---|
[c19a5a59] | 316 | if (rc == EOK) {
|
---|
[1ba95ba] | 317 | *pos = dsp->buffer.size - remain;
|
---|
| 318 | }
|
---|
[c19a5a59] | 319 | return rc;
|
---|
[1ba95ba] | 320 | }
|
---|
| 321 |
|
---|
[b7fd2a0] | 322 | errno_t sb_dsp_test_format(sb_dsp_t *dsp, unsigned *channels, unsigned *rate,
|
---|
[3bacee1] | 323 | pcm_sample_format_t *format)
|
---|
[b7c080c] | 324 | {
|
---|
[b7fd2a0] | 325 | errno_t ret = EOK;
|
---|
[b7c080c] | 326 | if (*channels == 0 || *channels > 2) {
|
---|
| 327 | *channels = 2;
|
---|
| 328 | ret = ELIMIT;
|
---|
| 329 | }
|
---|
| 330 | //TODO 8bit DMA supports 8bit formats
|
---|
| 331 | if (*format != PCM_SAMPLE_SINT16_LE && *format != PCM_SAMPLE_UINT16_LE) {
|
---|
| 332 | *format = pcm_sample_format_is_signed(*format) ?
|
---|
| 333 | PCM_SAMPLE_SINT16_LE : PCM_SAMPLE_UINT16_LE;
|
---|
| 334 | ret = ELIMIT;
|
---|
| 335 | }
|
---|
[018ab50] | 336 | if (*rate > DSP_RATE_UPPER_LIMIT) {
|
---|
| 337 | *rate = DSP_RATE_UPPER_LIMIT;
|
---|
| 338 | ret = ELIMIT;
|
---|
| 339 | }
|
---|
| 340 | if (*rate < DSP_RATE_LOWER_LIMIT) {
|
---|
| 341 | *rate = DSP_RATE_LOWER_LIMIT;
|
---|
| 342 | ret = ELIMIT;
|
---|
| 343 | }
|
---|
[b7c080c] | 344 | return ret;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[b7fd2a0] | 347 | errno_t sb_dsp_set_event_session(sb_dsp_t *dsp, async_sess_t *session)
|
---|
[fb6c98f] | 348 | {
|
---|
| 349 | assert(dsp);
|
---|
[6f57933] | 350 | if (dsp->event_session && session)
|
---|
[fb6c98f] | 351 | return EBUSY;
|
---|
| 352 | dsp->event_session = session;
|
---|
| 353 | ddf_log_debug("Set event session to %p.", session);
|
---|
| 354 | return EOK;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[3bacee1] | 357 | async_sess_t *sb_dsp_get_event_session(sb_dsp_t *dsp)
|
---|
[fb6c98f] | 358 | {
|
---|
| 359 | assert(dsp);
|
---|
| 360 | ddf_log_debug("Get event session: %p.", dsp->event_session);
|
---|
| 361 | return dsp->event_session;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[b7fd2a0] | 364 | errno_t sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size)
|
---|
[43dec08] | 365 | {
|
---|
| 366 | assert(dsp);
|
---|
[00006e0] | 367 | assert(size);
|
---|
| 368 |
|
---|
[7c3fb9b] | 369 | /*
|
---|
| 370 | * buffer is already setup by for someone, refuse to work until
|
---|
| 371 | * it's released
|
---|
| 372 | */
|
---|
[fb6c98f] | 373 | if (dsp->state != DSP_NO_BUFFER)
|
---|
[7ca22e5] | 374 | return EBUSY;
|
---|
[fb6c98f] | 375 | assert(dsp->buffer.data == NULL);
|
---|
[7ca22e5] | 376 |
|
---|
[b7fd2a0] | 377 | const errno_t ret = setup_buffer(dsp, *size);
|
---|
[124f9bd] | 378 | if (ret == EOK) {
|
---|
[b497018] | 379 | ddf_log_debug("Providing buffer: %p, %zu B.",
|
---|
| 380 | dsp->buffer.data, dsp->buffer.size);
|
---|
[00006e0] | 381 |
|
---|
[124f9bd] | 382 | if (buffer)
|
---|
| 383 | *buffer = dsp->buffer.data;
|
---|
| 384 | if (size)
|
---|
| 385 | *size = dsp->buffer.size;
|
---|
[5eed99d] | 386 | dsp_change_state(dsp, DSP_READY);
|
---|
[124f9bd] | 387 | }
|
---|
[43dec08] | 388 | return ret;
|
---|
| 389 | }
|
---|
[7ca22e5] | 390 |
|
---|
[b7fd2a0] | 391 | errno_t sb_dsp_release_buffer(sb_dsp_t *dsp)
|
---|
[43dec08] | 392 | {
|
---|
| 393 | assert(dsp);
|
---|
[f2a92b0] | 394 | if (dsp->state != DSP_READY)
|
---|
[fb6c98f] | 395 | return EINVAL;
|
---|
| 396 | assert(dsp->buffer.data);
|
---|
| 397 | dmamem_unmap_anonymous(dsp->buffer.data);
|
---|
| 398 | dsp->buffer.data = NULL;
|
---|
| 399 | dsp->buffer.size = 0;
|
---|
[124f9bd] | 400 | ddf_log_debug("DSP buffer released.");
|
---|
[5eed99d] | 401 | dsp_change_state(dsp, DSP_NO_BUFFER);
|
---|
[43dec08] | 402 | return EOK;
|
---|
| 403 | }
|
---|
[e941bf8] | 404 |
|
---|
[b7fd2a0] | 405 | errno_t sb_dsp_start_playback(sb_dsp_t *dsp, unsigned frames,
|
---|
[346643c] | 406 | unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
|
---|
[43dec08] | 407 | {
|
---|
| 408 | assert(dsp);
|
---|
| 409 |
|
---|
[f2a92b0] | 410 | if (!dsp->buffer.data || dsp->state != DSP_READY)
|
---|
[92b638c] | 411 | return EINVAL;
|
---|
| 412 |
|
---|
[43dec08] | 413 | /* Check supported parameters */
|
---|
[57e8b3b] | 414 | ddf_log_debug("Requested playback: %u frames, %uHz, %s, %u channel(s).",
|
---|
| 415 | frames, sampling_rate, pcm_sample_format_str(format), channels);
|
---|
[d2765ab3] | 416 | if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
|
---|
[346643c] | 417 | return ENOTSUP;
|
---|
[43dec08] | 418 |
|
---|
[f3fb83a] | 419 | /* Client requested regular events */
|
---|
[fda3bd9] | 420 | if (frames && !dsp->event_session)
|
---|
| 421 | return EINVAL;
|
---|
| 422 |
|
---|
| 423 | if (dsp->event_session) {
|
---|
[ff396ea] | 424 | dsp->event_exchange = async_exchange_begin(dsp->event_session);
|
---|
| 425 | if (!dsp->event_exchange)
|
---|
| 426 | return ENOMEM;
|
---|
| 427 | }
|
---|
[43dec08] | 428 |
|
---|
[3bacee1] | 429 | dsp->active.mode = 0 |
|
---|
| 430 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0) |
|
---|
| 431 | (channels == 2 ? DSP_MODE_STEREO : 0);
|
---|
[57e8b3b] | 432 | dsp->active.samples = frames * channels;
|
---|
| 433 | dsp->active.frame_count = 0;
|
---|
[43dec08] | 434 |
|
---|
[5eed99d] | 435 | dsp_set_sampling_rate(dsp, sampling_rate);
|
---|
[ad42844] | 436 |
|
---|
| 437 | #ifdef AUTO_DMA_MODE
|
---|
[5eed99d] | 438 | dsp_start_current_active(dsp, AUTO_DMA_16B_DA_FIFO);
|
---|
[ad42844] | 439 | #else
|
---|
[5eed99d] | 440 | dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
|
---|
[ad42844] | 441 | #endif
|
---|
[43dec08] | 442 |
|
---|
[f3fb83a] | 443 | ddf_log_verbose("Playback started, event every %u samples",
|
---|
| 444 | dsp->active.samples);
|
---|
[a3ab774] | 445 |
|
---|
[5eed99d] | 446 | dsp_change_state(dsp,
|
---|
[fb6c98f] | 447 | frames ? DSP_PLAYBACK_ACTIVE_EVENTS : DSP_PLAYBACK_NOEVENTS);
|
---|
[0387b92] | 448 | if (dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS)
|
---|
| 449 | dsp_report_event(dsp, PCM_EVENT_PLAYBACK_STARTED);
|
---|
[9b3d999] | 450 |
|
---|
[43dec08] | 451 | return EOK;
|
---|
| 452 | }
|
---|
[e941bf8] | 453 |
|
---|
[b7fd2a0] | 454 | errno_t sb_dsp_stop_playback(sb_dsp_t *dsp, bool immediate)
|
---|
[43dec08] | 455 | {
|
---|
| 456 | assert(dsp);
|
---|
[92b638c] | 457 | if ((dsp->state == DSP_PLAYBACK_NOEVENTS ||
|
---|
| 458 | dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS) &&
|
---|
[3bacee1] | 459 | immediate) {
|
---|
[5eed99d] | 460 | dsp_write(dsp, DMA_16B_PAUSE);
|
---|
| 461 | dsp_reset(dsp);
|
---|
[92b638c] | 462 | ddf_log_debug("Stopped playback");
|
---|
[f2a92b0] | 463 | dsp_change_state(dsp, DSP_READY);
|
---|
[f7b36bc] | 464 | if (dsp->event_exchange) {
|
---|
| 465 | dsp_report_event(dsp, PCM_EVENT_PLAYBACK_TERMINATED);
|
---|
| 466 | async_exchange_end(dsp->event_exchange);
|
---|
| 467 | dsp->event_exchange = NULL;
|
---|
| 468 | }
|
---|
[92b638c] | 469 | return EOK;
|
---|
| 470 | }
|
---|
[3bacee1] | 471 | if (dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS) {
|
---|
[92b638c] | 472 | /* Stop after current fragment */
|
---|
| 473 | assert(!immediate);
|
---|
[5eed99d] | 474 | dsp_write(dsp, DMA_16B_EXIT);
|
---|
[92b638c] | 475 | ddf_log_debug("Last playback fragment");
|
---|
[5eed99d] | 476 | dsp_change_state(dsp, DSP_PLAYBACK_TERMINATE);
|
---|
[92b638c] | 477 | return EOK;
|
---|
| 478 | }
|
---|
| 479 | return EINVAL;
|
---|
[43dec08] | 480 | }
|
---|
[e941bf8] | 481 |
|
---|
[b7fd2a0] | 482 | errno_t sb_dsp_start_capture(sb_dsp_t *dsp, unsigned frames,
|
---|
[346643c] | 483 | unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
|
---|
[43dec08] | 484 | {
|
---|
[a3ab774] | 485 | assert(dsp);
|
---|
[f2a92b0] | 486 | if (!dsp->buffer.data || dsp->state != DSP_READY)
|
---|
[92b638c] | 487 | return EINVAL;
|
---|
[a3ab774] | 488 |
|
---|
| 489 | /* Check supported parameters */
|
---|
[d86c9736] | 490 | ddf_log_debug("Requested capture: %u frames, %uHz, %s, %u channel(s).",
|
---|
[57e8b3b] | 491 | frames, sampling_rate, pcm_sample_format_str(format), channels);
|
---|
[d2765ab3] | 492 | if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
|
---|
[346643c] | 493 | return ENOTSUP;
|
---|
[a3ab774] | 494 |
|
---|
[f3fb83a] | 495 | /* Client requested regular events */
|
---|
[fda3bd9] | 496 | if (frames && !dsp->event_session)
|
---|
| 497 | return EINVAL;
|
---|
| 498 |
|
---|
| 499 | if (dsp->event_session) {
|
---|
[ff396ea] | 500 | dsp->event_exchange = async_exchange_begin(dsp->event_session);
|
---|
| 501 | if (!dsp->event_exchange)
|
---|
| 502 | return ENOMEM;
|
---|
| 503 | }
|
---|
[a3ab774] | 504 |
|
---|
[3bacee1] | 505 | dsp->active.mode = 0 |
|
---|
| 506 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0) |
|
---|
| 507 | (channels == 2 ? DSP_MODE_STEREO : 0);
|
---|
[ad42844] | 508 | dsp->active.samples = frames * channels;
|
---|
| 509 | dsp->active.frame_count = 0;
|
---|
[a3ab774] | 510 |
|
---|
[5eed99d] | 511 | dsp_set_sampling_rate(dsp, sampling_rate);
|
---|
[a3ab774] | 512 |
|
---|
| 513 | #ifdef AUTO_DMA_MODE
|
---|
[5eed99d] | 514 | dsp_start_current_active(dsp, AUTO_DMA_16B_AD_FIFO);
|
---|
[a3ab774] | 515 | #else
|
---|
[5eed99d] | 516 | dsp_start_current_active(dsp, SINGLE_DMA_16B_AD);
|
---|
[a3ab774] | 517 | #endif
|
---|
| 518 |
|
---|
[f3fb83a] | 519 | ddf_log_verbose("Capture started started, event every %u samples",
|
---|
| 520 | dsp->active.samples);
|
---|
[5eed99d] | 521 | dsp_change_state(dsp,
|
---|
[fb6c98f] | 522 | frames ? DSP_CAPTURE_ACTIVE_EVENTS : DSP_CAPTURE_NOEVENTS);
|
---|
[0387b92] | 523 | if (dsp->state == DSP_CAPTURE_ACTIVE_EVENTS)
|
---|
| 524 | dsp_report_event(dsp, PCM_EVENT_CAPTURE_STARTED);
|
---|
[a3ab774] | 525 | return EOK;
|
---|
[43dec08] | 526 | }
|
---|
[e941bf8] | 527 |
|
---|
[b7fd2a0] | 528 | errno_t sb_dsp_stop_capture(sb_dsp_t *dsp, bool immediate)
|
---|
[43dec08] | 529 | {
|
---|
[a3ab774] | 530 | assert(dsp);
|
---|
[92b638c] | 531 | if ((dsp->state == DSP_CAPTURE_NOEVENTS ||
|
---|
| 532 | dsp->state == DSP_CAPTURE_ACTIVE_EVENTS) &&
|
---|
[3bacee1] | 533 | immediate) {
|
---|
[5eed99d] | 534 | dsp_write(dsp, DMA_16B_PAUSE);
|
---|
| 535 | dsp_reset(dsp);
|
---|
[92b638c] | 536 | ddf_log_debug("Stopped capture fragment");
|
---|
[f2a92b0] | 537 | dsp_change_state(dsp, DSP_READY);
|
---|
[f7b36bc] | 538 | if (dsp->event_exchange) {
|
---|
| 539 | dsp_report_event(dsp, PCM_EVENT_CAPTURE_TERMINATED);
|
---|
| 540 | async_exchange_end(dsp->event_exchange);
|
---|
| 541 | dsp->event_exchange = NULL;
|
---|
| 542 | }
|
---|
[92b638c] | 543 | return EOK;
|
---|
| 544 | }
|
---|
[3bacee1] | 545 | if (dsp->state == DSP_CAPTURE_ACTIVE_EVENTS) {
|
---|
[92b638c] | 546 | /* Stop after current fragment */
|
---|
| 547 | assert(!immediate);
|
---|
[5eed99d] | 548 | dsp_write(dsp, DMA_16B_EXIT);
|
---|
[92b638c] | 549 | ddf_log_debug("Last capture fragment");
|
---|
[5eed99d] | 550 | dsp_change_state(dsp, DSP_CAPTURE_TERMINATE);
|
---|
[92b638c] | 551 | return EOK;
|
---|
| 552 | }
|
---|
| 553 | return EINVAL;
|
---|
[43dec08] | 554 | }
|
---|
[763444e] | 555 | /**
|
---|
| 556 | * @}
|
---|
| 557 | */
|
---|