source: mainline/uspace/drv/audio/sb16/dsp.c@ aae11d0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aae11d0 was 6f57933, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

sb16: Fix session cleanup.

  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[763444e]1/*
2 * Copyright (c) 2011 Jan Vesely
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/** @addtogroup drvaudiosb16
29 * @{
30 */
31/** @file
32 * @brief DSP helper functions implementation
33 */
34
[c5cbc1b7]35#include <as.h>
[1240bb9]36#include <bool.h>
[c5cbc1b7]37#include <ddi.h>
[c885a21]38#include <devman.h>
39#include <device/hw_res.h>
[763444e]40#include <libarch/ddi.h>
[0b4f060]41#include <libarch/barrier.h>
[01aef43]42#include <str_error.h>
[1240bb9]43#include <audio_pcm_iface.h>
[0f2e7c1]44
[bf38143]45#include "ddf_log.h"
46#include "dsp_commands.h"
[763444e]47#include "dsp.h"
48
[c5cbc1b7]49/* Maximum allowed transfer size for ISA DMA transfers is 64kB */
50#define MAX_BUFFER_SIZE (4 * 1024) // use 4kB for now
[bf38143]51
52#ifndef DSP_RETRY_COUNT
53#define DSP_RETRY_COUNT 100
54#endif
55
56#define DSP_RESET_RESPONSE 0xaa
[018ab50]57
58/* These are only for SB16 (DSP4.00+) */
59#define DSP_RATE_UPPER_LIMIT 44100
60#define DSP_RATE_LOWER_LIMIT 5000
[bf38143]61
[5984107]62#define AUTO_DMA_MODE
63
[fb6c98f]64static inline const char * dsp_state_to_str(dsp_state_t state)
65{
66 static const char* state_names[] = {
67 [DSP_PLAYBACK_ACTIVE_EVENTS] = "PLAYBACK w/ EVENTS",
68 [DSP_CAPTURE_ACTIVE_EVENTS] = "CAPTURE w/o ACTIVE",
69 [DSP_PLAYBACK_NOEVENTS] = "PLAYBACK w/. EVENTS",
70 [DSP_CAPTURE_NOEVENTS] = "CAPTURE w/o EVENTS",
71 [DSP_STOPPED] = "STOPPED",
72 [DSP_READY] = "READY",
73 [DSP_NO_BUFFER] = "NO BUFFER",
74 };
75 if (state < (sizeof(state_names) / sizeof(state_names[0])))
76 return state_names[state];
77 return "UNKNOWN";
78}
79
80
81static inline void sb_dsp_change_state(sb_dsp_t *dsp, dsp_state_t state)
82{
83 assert(dsp);
84 ddf_log_verbose("Changing state from %s to %s",
85 dsp_state_to_str(dsp->state), dsp_state_to_str(state));
86 dsp->state = state;
87}
88
[bf38143]89static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
90{
91 assert(data);
92 assert(dsp);
93 uint8_t status;
94 size_t attempts = DSP_RETRY_COUNT;
95 do {
96 status = pio_read_8(&dsp->regs->dsp_read_status);
97 } while (--attempts && ((status & DSP_READ_READY) == 0));
98
99 if ((status & DSP_READ_READY) == 0)
100 return EIO;
101
102 *data = pio_read_8(&dsp->regs->dsp_data_read);
103 return EOK;
104}
[e941bf8]105
[bf38143]106static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
107{
108 assert(dsp);
109 uint8_t status;
110 size_t attempts = DSP_RETRY_COUNT;
111 do {
112 status = pio_read_8(&dsp->regs->dsp_write);
113 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
114
115 if ((status & DSP_WRITE_BUSY))
116 return EIO;
117
118 pio_write_8(&dsp->regs->dsp_write, data);
119 return EOK;
120}
[e941bf8]121
[bf38143]122static inline void sb_dsp_reset(sb_dsp_t *dsp)
123{
124 assert(dsp);
125 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
126 pio_write_8(&dsp->regs->dsp_reset, 1);
127 udelay(3); /* Keep reset for 3 us */
128 pio_write_8(&dsp->regs->dsp_reset, 0);
129}
[e941bf8]130
[ad42844]131static inline void sb_dsp_start_active(sb_dsp_t *dsp, uint8_t command)
132{
133 sb_dsp_write(dsp, command);
134 sb_dsp_write(dsp, dsp->active.mode);
135 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
136 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
137}
138
139static inline void sb_dsp_set_sampling_rate(sb_dsp_t *dsp, unsigned rate)
140{
141 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
142 sb_dsp_write(dsp, rate >> 8);
143 sb_dsp_write(dsp, rate & 0xff);
144 ddf_log_verbose("Sampling rate: %hhx:%hhx.", rate >> 8, rate & 0xff);
145}
146
[c885a21]147static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
148{
149 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
150 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
151
152 const int ret = hw_res_dma_channel_setup(sess,
153 dsp->dma16_channel, pa, size,
154 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
[c5cbc1b7]155
[c885a21]156 async_hangup(sess);
157 return ret;
158}
[e941bf8]159
[00006e0]160static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
[01aef43]161{
162 assert(dsp);
[9b3d999]163 if (size > MAX_BUFFER_SIZE || size == 0 || (size % 2) == 1)
164 size = MAX_BUFFER_SIZE;
[c5cbc1b7]165 void *buffer = NULL, *pa = NULL;
166 int ret = dmamem_map_anonymous(size, AS_AREA_WRITE | AS_AREA_READ,
167 0, &pa, &buffer);
168 if (ret != EOK) {
[124f9bd]169 ddf_log_error("Failed to allocate DMA buffer.");
[f14e6ea]170 return ENOMEM;
171 }
172
[c5cbc1b7]173 ddf_log_verbose("Setup dma buffer at %p(%p).", buffer, pa, size);
174 assert((uintptr_t)pa < (1 << 25));
[9b3d999]175
[c5cbc1b7]176 /* Setup 16 bit channel */
177 ret = sb_setup_dma(dsp, (uintptr_t)pa, size);
[01aef43]178 if (ret == EOK) {
[dce7e41]179 dsp->buffer.data = buffer;
[00006e0]180 dsp->buffer.size = size;
[01aef43]181 } else {
[124f9bd]182 ddf_log_error("Failed to setup DMA16 channel: %s.",
[01aef43]183 str_error(ret));
[c5cbc1b7]184 dmamem_unmap_anonymous(buffer);
[01aef43]185 }
186 return ret;
187}
[e941bf8]188
[346643c]189static inline size_t sample_count(pcm_sample_format_t format, size_t byte_count)
[0b4f060]190{
[346643c]191 return byte_count / pcm_sample_format_size(format);
[0b4f060]192}
[e941bf8]193
[c885a21]194int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
195 int dma8, int dma16)
[bf38143]196{
197 assert(dsp);
198 dsp->regs = regs;
[c885a21]199 dsp->dma8_channel = dma8;
200 dsp->dma16_channel = dma16;
[7ca22e5]201 dsp->event_session = NULL;
202 dsp->event_exchange = NULL;
[c885a21]203 dsp->sb_dev = dev;
[fb6c98f]204 dsp->state = DSP_NO_BUFFER;
[bf38143]205 sb_dsp_reset(dsp);
206 /* "DSP takes about 100 microseconds to initialize itself" */
207 udelay(100);
208 uint8_t response;
209 const int ret = sb_dsp_read(dsp, &response);
210 if (ret != EOK) {
[124f9bd]211 ddf_log_error("Failed to read DSP reset response value.");
[bf38143]212 return ret;
213 }
214 if (response != DSP_RESET_RESPONSE) {
[124f9bd]215 ddf_log_error("Invalid DSP reset response: %x.", response);
[bf38143]216 return EIO;
217 }
218
219 /* Get DSP version number */
220 sb_dsp_write(dsp, DSP_VERSION);
221 sb_dsp_read(dsp, &dsp->version.major);
222 sb_dsp_read(dsp, &dsp->version.minor);
[01aef43]223
224 return ret;
[bf38143]225}
[e941bf8]226
[f14e6ea]227void sb_dsp_interrupt(sb_dsp_t *dsp)
228{
[bf38143]229 assert(dsp);
[ff396ea]230
[57e8b3b]231 dsp->active.frame_count +=
232 dsp->active.samples / ((dsp->active.mode & DSP_MODE_STEREO) ? 2 : 1);
233
[fb6c98f]234 switch (dsp->state)
235 {
236 case DSP_PLAYBACK_ACTIVE_EVENTS:
237 async_msg_1(dsp->event_exchange,
238 PCM_EVENT_FRAMES_PLAYED, dsp->active.frame_count);
239 case DSP_PLAYBACK_NOEVENTS:
240#ifndef AUTO_DMA_MODE
241 sb_dsp_start_active(dsp, SINGLE_DMA_16B_DA);
242#endif
243 break;
244 case DSP_CAPTURE_ACTIVE_EVENTS:
245 async_msg_1(dsp->event_exchange,
246 PCM_EVENT_FRAMES_CAPTURED, dsp->active.frame_count);
247 case DSP_CAPTURE_NOEVENTS:
248#ifndef AUTO_DMA_MODE
249 sb_dsp_start_active(dsp, SINGLE_DMA_16B_DA);
250#endif
251 break;
252 default:
253 ddf_log_warning("Interrupt while DSP not active");
[7ca22e5]254 }
[01aef43]255}
[e941bf8]256
[f0241bda]257unsigned sb_dsp_query_cap(sb_dsp_t *dsp, audio_cap_t cap)
258{
[1ba95ba]259 ddf_log_verbose("Querying cap %u", cap);
[f0241bda]260 switch(cap) {
[d86c9736]261 case AUDIO_CAP_CAPTURE:
[f0241bda]262 case AUDIO_CAP_PLAYBACK:
263 case AUDIO_CAP_INTERRUPT:
[1ba95ba]264 case AUDIO_CAP_BUFFER_POS:
[f0241bda]265 return 1;
266 case AUDIO_CAP_MAX_BUFFER:
267 return MAX_BUFFER_SIZE;
268 case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
269 return 1;
270 case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
271 return 16535;
272 default:
273 return 0;
274 }
275}
276
[1ba95ba]277int sb_dsp_get_buffer_position(sb_dsp_t *dsp, size_t *pos)
278{
[fb6c98f]279 if (dsp->state == DSP_NO_BUFFER)
[1ba95ba]280 return ENOENT;
281
[fb6c98f]282 assert(dsp->buffer.data);
[1ba95ba]283 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
284 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
285
286 // TODO: Assumes DMA 16
287 const int remain = hw_res_dma_channel_remain(sess, dsp->dma16_channel);
288 async_hangup(sess);
289 if (remain >= 0) {
290 *pos = dsp->buffer.size - remain;
291 return EOK;
292 }
293 return remain;
294}
295
[b7c080c]296int sb_dsp_test_format(sb_dsp_t *dsp, unsigned *channels, unsigned *rate,
297 pcm_sample_format_t *format)
298{
299 int ret = EOK;
300 if (*channels == 0 || *channels > 2) {
301 *channels = 2;
302 ret = ELIMIT;
303 }
304 //TODO 8bit DMA supports 8bit formats
305 if (*format != PCM_SAMPLE_SINT16_LE && *format != PCM_SAMPLE_UINT16_LE) {
306 *format = pcm_sample_format_is_signed(*format) ?
307 PCM_SAMPLE_SINT16_LE : PCM_SAMPLE_UINT16_LE;
308 ret = ELIMIT;
309 }
[018ab50]310 if (*rate > DSP_RATE_UPPER_LIMIT) {
311 *rate = DSP_RATE_UPPER_LIMIT;
312 ret = ELIMIT;
313 }
314 if (*rate < DSP_RATE_LOWER_LIMIT) {
315 *rate = DSP_RATE_LOWER_LIMIT;
316 ret = ELIMIT;
317 }
[b7c080c]318 return ret;
319}
320
[fb6c98f]321int sb_dsp_set_event_session(sb_dsp_t *dsp, async_sess_t *session)
322{
323 assert(dsp);
[6f57933]324 if (dsp->event_session && session)
[fb6c98f]325 return EBUSY;
326 dsp->event_session = session;
327 ddf_log_debug("Set event session to %p.", session);
328 return EOK;
329}
330
331async_sess_t * sb_dsp_get_event_session(sb_dsp_t *dsp)
332{
333 assert(dsp);
334 ddf_log_debug("Get event session: %p.", dsp->event_session);
335 return dsp->event_session;
336}
337
[b497018]338int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size)
[43dec08]339{
340 assert(dsp);
[00006e0]341 assert(size);
342
[7ca22e5]343 /* buffer is already setup by for someone, refuse to work until
344 * it's released */
[fb6c98f]345 if (dsp->state != DSP_NO_BUFFER)
[7ca22e5]346 return EBUSY;
[fb6c98f]347 assert(dsp->buffer.data == NULL);
[7ca22e5]348
[00006e0]349 const int ret = sb_setup_buffer(dsp, *size);
[124f9bd]350 if (ret == EOK) {
[b497018]351 ddf_log_debug("Providing buffer: %p, %zu B.",
352 dsp->buffer.data, dsp->buffer.size);
[00006e0]353
[124f9bd]354 if (buffer)
355 *buffer = dsp->buffer.data;
356 if (size)
357 *size = dsp->buffer.size;
[fb6c98f]358 sb_dsp_change_state(dsp, DSP_READY);
[124f9bd]359 }
[43dec08]360 return ret;
361}
[7ca22e5]362
[b497018]363int sb_dsp_release_buffer(sb_dsp_t *dsp)
[43dec08]364{
365 assert(dsp);
[fb6c98f]366 if (dsp->state != DSP_READY && dsp->state != DSP_STOPPED)
367 return EINVAL;
368 assert(dsp->buffer.data);
369 dmamem_unmap_anonymous(dsp->buffer.data);
370 dsp->buffer.data = NULL;
371 dsp->buffer.size = 0;
[124f9bd]372 ddf_log_debug("DSP buffer released.");
[fb6c98f]373 sb_dsp_change_state(dsp, DSP_NO_BUFFER);
[43dec08]374 return EOK;
375}
[e941bf8]376
[57e8b3b]377int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned frames,
[346643c]378 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
[43dec08]379{
380 assert(dsp);
381
[d758301]382 if (!dsp->buffer.data)
[7ca22e5]383 return EINVAL;
384
[43dec08]385 /* Check supported parameters */
[57e8b3b]386 ddf_log_debug("Requested playback: %u frames, %uHz, %s, %u channel(s).",
387 frames, sampling_rate, pcm_sample_format_str(format), channels);
[d2765ab3]388 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
[346643c]389 return ENOTSUP;
[43dec08]390
[ff396ea]391 /* Client requested regular interrupts */
392 if (frames) {
[d758301]393 if (!dsp->event_session)
394 return EINVAL;
[ff396ea]395 dsp->event_exchange = async_exchange_begin(dsp->event_session);
396 if (!dsp->event_exchange)
397 return ENOMEM;
398 }
[43dec08]399
[57e8b3b]400 dsp->active.mode = 0
[ad42844]401 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0)
[57e8b3b]402 | (channels == 2 ? DSP_MODE_STEREO : 0);
403 dsp->active.samples = frames * channels;
404 dsp->active.frame_count = 0;
[43dec08]405
[ad42844]406 sb_dsp_set_sampling_rate(dsp, sampling_rate);
407
408#ifdef AUTO_DMA_MODE
409 sb_dsp_start_active(dsp, AUTO_DMA_16B_DA_FIFO);
410#else
411 sb_dsp_start_active(dsp, SINGLE_DMA_16B_DA);
412#endif
[43dec08]413
[9b3d999]414 ddf_log_verbose("Playback started, interrupt every %u samples "
[a3ab774]415 "(~1/%u sec)", dsp->active.samples,
[346643c]416 sampling_rate / (dsp->active.samples * channels));
[a3ab774]417
[fb6c98f]418 sb_dsp_change_state(dsp,
419 frames ? DSP_PLAYBACK_ACTIVE_EVENTS : DSP_PLAYBACK_NOEVENTS);
[9b3d999]420
[43dec08]421 return EOK;
422}
[e941bf8]423
[b497018]424int sb_dsp_stop_playback(sb_dsp_t *dsp)
[43dec08]425{
426 assert(dsp);
427 sb_dsp_write(dsp, DMA_16B_EXIT);
[b497018]428 ddf_log_debug("Stopping playback on buffer.");
[1240bb9]429 async_msg_0(dsp->event_exchange, PCM_EVENT_PLAYBACK_TERMINATED);
430 async_exchange_end(dsp->event_exchange);
431 dsp->event_exchange = NULL;
[fb6c98f]432 sb_dsp_change_state(dsp, DSP_STOPPED);
[43dec08]433 return EOK;
434}
[e941bf8]435
[d86c9736]436int sb_dsp_start_capture(sb_dsp_t *dsp, unsigned frames,
[346643c]437 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
[43dec08]438{
[a3ab774]439 assert(dsp);
[d758301]440 if (!dsp->buffer.data)
[a3ab774]441 return EINVAL;
442
443 /* Check supported parameters */
[d86c9736]444 ddf_log_debug("Requested capture: %u frames, %uHz, %s, %u channel(s).",
[57e8b3b]445 frames, sampling_rate, pcm_sample_format_str(format), channels);
[d2765ab3]446 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
[346643c]447 return ENOTSUP;
[a3ab774]448
[ff396ea]449 /* client requested regular interrupts */
450 if (frames) {
[d758301]451 if (!dsp->event_session)
452 return EINVAL;
[ff396ea]453 dsp->event_exchange = async_exchange_begin(dsp->event_session);
454 if (!dsp->event_exchange)
455 return ENOMEM;
456 }
[a3ab774]457
[ad42844]458 dsp->active.mode = 0
459 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0)
460 | (channels == 2 ? DSP_MODE_STEREO : 0);
461 dsp->active.samples = frames * channels;
462 dsp->active.frame_count = 0;
[a3ab774]463
[ad42844]464 sb_dsp_set_sampling_rate(dsp, sampling_rate);
[a3ab774]465
466#ifdef AUTO_DMA_MODE
[ad42844]467 sb_dsp_start_active(dsp, AUTO_DMA_16B_AD_FIFO);
[a3ab774]468#else
[ad42844]469 sb_dsp_start_active(dsp, SINGLE_DMA_16B_AD);
[a3ab774]470#endif
471
472 ddf_log_verbose("Recording started started, interrupt every %u samples "
473 "(~1/%u sec)", dsp->active.samples,
[346643c]474 sampling_rate / (dsp->active.samples * channels));
[fb6c98f]475 sb_dsp_change_state(dsp,
476 frames ? DSP_CAPTURE_ACTIVE_EVENTS : DSP_CAPTURE_NOEVENTS);
[a3ab774]477 return EOK;
[43dec08]478}
[e941bf8]479
[d86c9736]480int sb_dsp_stop_capture(sb_dsp_t *dsp)
[43dec08]481{
[a3ab774]482 assert(dsp);
483 sb_dsp_write(dsp, DMA_16B_EXIT);
[d86c9736]484 ddf_log_debug("Stopped capture");
485 async_msg_0(dsp->event_exchange, PCM_EVENT_CAPTURE_TERMINATED);
[1240bb9]486 async_exchange_end(dsp->event_exchange);
487 dsp->event_exchange = NULL;
[fb6c98f]488 sb_dsp_change_state(dsp, DSP_STOPPED);
[a3ab774]489 return EOK;
[43dec08]490}
[763444e]491/**
492 * @}
493 */
Note: See TracBrowser for help on using the repository browser.