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

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

sb16: Use format test function to validate input parameters.

  • Property mode set to 100644
File size: 12.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
[b7c080c]57#define DSP_RATE_LIMIT 45000
[bf38143]58
[5984107]59#define AUTO_DMA_MODE
60
[bf38143]61static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
62{
63 assert(data);
64 assert(dsp);
65 uint8_t status;
66 size_t attempts = DSP_RETRY_COUNT;
67 do {
68 status = pio_read_8(&dsp->regs->dsp_read_status);
69 } while (--attempts && ((status & DSP_READ_READY) == 0));
70
71 if ((status & DSP_READ_READY) == 0)
72 return EIO;
73
74 *data = pio_read_8(&dsp->regs->dsp_data_read);
75 return EOK;
76}
[e941bf8]77
[bf38143]78static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
79{
80 assert(dsp);
81 uint8_t status;
82 size_t attempts = DSP_RETRY_COUNT;
83 do {
84 status = pio_read_8(&dsp->regs->dsp_write);
85 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
86
87 if ((status & DSP_WRITE_BUSY))
88 return EIO;
89
90 pio_write_8(&dsp->regs->dsp_write, data);
91 return EOK;
92}
[e941bf8]93
[bf38143]94static inline void sb_dsp_reset(sb_dsp_t *dsp)
95{
96 assert(dsp);
97 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
98 pio_write_8(&dsp->regs->dsp_reset, 1);
99 udelay(3); /* Keep reset for 3 us */
100 pio_write_8(&dsp->regs->dsp_reset, 0);
101}
[e941bf8]102
[c885a21]103static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
104{
105 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
106 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
107
108 const int ret = hw_res_dma_channel_setup(sess,
109 dsp->dma16_channel, pa, size,
110 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
[c5cbc1b7]111
[c885a21]112 async_hangup(sess);
113 return ret;
114}
[e941bf8]115
[00006e0]116static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
[01aef43]117{
118 assert(dsp);
[9b3d999]119 if (size > MAX_BUFFER_SIZE || size == 0 || (size % 2) == 1)
120 size = MAX_BUFFER_SIZE;
[c5cbc1b7]121 void *buffer = NULL, *pa = NULL;
122 int ret = dmamem_map_anonymous(size, AS_AREA_WRITE | AS_AREA_READ,
123 0, &pa, &buffer);
124 if (ret != EOK) {
[124f9bd]125 ddf_log_error("Failed to allocate DMA buffer.");
[f14e6ea]126 return ENOMEM;
127 }
128
[c5cbc1b7]129 ddf_log_verbose("Setup dma buffer at %p(%p).", buffer, pa, size);
130 assert((uintptr_t)pa < (1 << 25));
[9b3d999]131
[c5cbc1b7]132 /* Setup 16 bit channel */
133 ret = sb_setup_dma(dsp, (uintptr_t)pa, size);
[01aef43]134 if (ret == EOK) {
[dce7e41]135 dsp->buffer.data = buffer;
[00006e0]136 dsp->buffer.size = size;
[01aef43]137 } else {
[124f9bd]138 ddf_log_error("Failed to setup DMA16 channel: %s.",
[01aef43]139 str_error(ret));
[c5cbc1b7]140 dmamem_unmap_anonymous(buffer);
[01aef43]141 }
142 return ret;
143}
[e941bf8]144
[7257eea6]145static inline void sb_clear_buffer(sb_dsp_t *dsp)
146{
[c5cbc1b7]147 assert(dsp);
148 dmamem_unmap_anonymous(dsp->buffer.data);
[dce7e41]149 dsp->buffer.data = NULL;
150 dsp->buffer.size = 0;
[7257eea6]151}
[e941bf8]152
[346643c]153static inline size_t sample_count(pcm_sample_format_t format, size_t byte_count)
[0b4f060]154{
[346643c]155 return byte_count / pcm_sample_format_size(format);
[0b4f060]156}
[e941bf8]157
[c885a21]158int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
159 int dma8, int dma16)
[bf38143]160{
161 assert(dsp);
162 dsp->regs = regs;
[c885a21]163 dsp->dma8_channel = dma8;
164 dsp->dma16_channel = dma16;
[7ca22e5]165 dsp->event_session = NULL;
166 dsp->event_exchange = NULL;
[c885a21]167 dsp->sb_dev = dev;
[1240bb9]168 dsp->status = DSP_STOPPED;
[ff396ea]169 dsp->ignore_interrupts = false;
[bf38143]170 sb_dsp_reset(dsp);
171 /* "DSP takes about 100 microseconds to initialize itself" */
172 udelay(100);
173 uint8_t response;
174 const int ret = sb_dsp_read(dsp, &response);
175 if (ret != EOK) {
[124f9bd]176 ddf_log_error("Failed to read DSP reset response value.");
[bf38143]177 return ret;
178 }
179 if (response != DSP_RESET_RESPONSE) {
[124f9bd]180 ddf_log_error("Invalid DSP reset response: %x.", response);
[bf38143]181 return EIO;
182 }
183
184 /* Get DSP version number */
185 sb_dsp_write(dsp, DSP_VERSION);
186 sb_dsp_read(dsp, &dsp->version.major);
187 sb_dsp_read(dsp, &dsp->version.minor);
[01aef43]188
189 return ret;
[bf38143]190}
[e941bf8]191
[f14e6ea]192void sb_dsp_interrupt(sb_dsp_t *dsp)
193{
[bf38143]194 assert(dsp);
[ff396ea]195
196#ifndef AUTO_DMA_MODE
197 if (dsp->status == DSP_PLAYBACK) {
198 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
199 sb_dsp_write(dsp, dsp->active.mode);
200 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
201 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
202 }
203
204 if (dsp->status == DSP_RECORDING) {
205 sb_dsp_write(dsp, SINGLE_DMA_16B_AD);
206 sb_dsp_write(dsp, dsp->active.mode);
207 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
208 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
209 }
210#endif
211
212 if (dsp->ignore_interrupts)
213 return;
214
[57e8b3b]215 dsp->active.frame_count +=
216 dsp->active.samples / ((dsp->active.mode & DSP_MODE_STEREO) ? 2 : 1);
217
[7ca22e5]218 if (dsp->event_exchange) {
[1240bb9]219 switch (dsp->status) {
220 case DSP_PLAYBACK:
[57e8b3b]221 async_msg_1(dsp->event_exchange,
222 PCM_EVENT_FRAMES_PLAYED, dsp->active.frame_count);
[1240bb9]223 break;
224 case DSP_RECORDING:
[57e8b3b]225 async_msg_1(dsp->event_exchange,
226 PCM_EVENT_FRAMES_RECORDED, dsp->active.frame_count);
[1240bb9]227 break;
228 default:
229 case DSP_STOPPED:
230 ddf_log_warning("Interrupt while DSP stopped and "
231 "event exchange present. Terminating exchange");
232 async_exchange_end(dsp->event_exchange);
233 dsp->event_exchange = NULL;
234 }
[7ca22e5]235 } else {
236 ddf_log_warning("Interrupt with no event consumer.");
237 }
[01aef43]238}
[e941bf8]239
[f0241bda]240unsigned sb_dsp_query_cap(sb_dsp_t *dsp, audio_cap_t cap)
241{
242 switch(cap) {
243 case AUDIO_CAP_RECORD:
244 case AUDIO_CAP_PLAYBACK:
245 case AUDIO_CAP_INTERRUPT:
246 return 1;
247 case AUDIO_CAP_MAX_BUFFER:
248 return MAX_BUFFER_SIZE;
249 case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
250 return 1;
251 case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
252 return 16535;
253 case AUDIO_CAP_BUFFER_POS:
254 default:
255 return 0;
256 }
257}
258
[b7c080c]259int sb_dsp_test_format(sb_dsp_t *dsp, unsigned *channels, unsigned *rate,
260 pcm_sample_format_t *format)
261{
262 int ret = EOK;
263 if (*channels == 0 || *channels > 2) {
264 *channels = 2;
265 ret = ELIMIT;
266 }
267 if (*rate > DSP_RATE_LIMIT) {
268 *rate = DSP_RATE_LIMIT;
269 ret = ELIMIT;
270 }
271 //TODO 8bit DMA supports 8bit formats
272 if (*format != PCM_SAMPLE_SINT16_LE && *format != PCM_SAMPLE_UINT16_LE) {
273 *format = pcm_sample_format_is_signed(*format) ?
274 PCM_SAMPLE_SINT16_LE : PCM_SAMPLE_UINT16_LE;
275 ret = ELIMIT;
276 }
277 return ret;
278}
279
[b497018]280int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size)
[43dec08]281{
282 assert(dsp);
[00006e0]283 assert(size);
284
[7ca22e5]285 /* buffer is already setup by for someone, refuse to work until
286 * it's released */
287 if (dsp->buffer.data)
288 return EBUSY;
289
[00006e0]290 const int ret = sb_setup_buffer(dsp, *size);
[124f9bd]291 if (ret == EOK) {
[b497018]292 ddf_log_debug("Providing buffer: %p, %zu B.",
293 dsp->buffer.data, dsp->buffer.size);
[00006e0]294
[124f9bd]295 if (buffer)
296 *buffer = dsp->buffer.data;
297 if (size)
298 *size = dsp->buffer.size;
299 }
[43dec08]300 return ret;
301}
[7ca22e5]302
[b497018]303int sb_dsp_set_event_session(sb_dsp_t *dsp, async_sess_t *session)
[7ca22e5]304{
305 assert(dsp);
306 assert(session);
307 if (dsp->event_session)
308 return EBUSY;
309 dsp->event_session = session;
[124f9bd]310 ddf_log_debug("Set event session.");
[7ca22e5]311 return EOK;
312}
[e941bf8]313
[b497018]314int sb_dsp_release_buffer(sb_dsp_t *dsp)
[43dec08]315{
316 assert(dsp);
317 sb_clear_buffer(dsp);
[9b3d999]318 async_exchange_end(dsp->event_exchange);
[7ca22e5]319 dsp->event_exchange = NULL;
320 if (dsp->event_session)
321 async_hangup(dsp->event_session);
322 dsp->event_session = NULL;
[124f9bd]323 ddf_log_debug("DSP buffer released.");
[43dec08]324 return EOK;
325}
[e941bf8]326
[57e8b3b]327int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned frames,
[346643c]328 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
[43dec08]329{
330 assert(dsp);
331
[7ca22e5]332 if (!dsp->event_session)
333 return EINVAL;
334
[43dec08]335 /* Check supported parameters */
[57e8b3b]336 ddf_log_debug("Requested playback: %u frames, %uHz, %s, %u channel(s).",
337 frames, sampling_rate, pcm_sample_format_str(format), channels);
[d2765ab3]338 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
[346643c]339 return ENOTSUP;
[43dec08]340
[ff396ea]341 /* Client requested regular interrupts */
342 if (frames) {
343 dsp->event_exchange = async_exchange_begin(dsp->event_session);
344 if (!dsp->event_exchange)
345 return ENOMEM;
346 dsp->ignore_interrupts = false;
347 }
[43dec08]348
[346643c]349 const bool sign = (format == PCM_SAMPLE_SINT16_LE);
350
[43dec08]351 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
352 sb_dsp_write(dsp, sampling_rate >> 8);
353 sb_dsp_write(dsp, sampling_rate & 0xff);
354
[346643c]355 ddf_log_verbose("Sample rate: %hhx:%hhx.",
[43dec08]356 sampling_rate >> 8, sampling_rate & 0xff);
357
358#ifdef AUTO_DMA_MODE
359 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
360#else
[ff396ea]361 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
[43dec08]362#endif
363
[57e8b3b]364 dsp->active.mode = 0
365 | (sign ? DSP_MODE_SIGNED : 0)
366 | (channels == 2 ? DSP_MODE_STEREO : 0);
367 dsp->active.samples = frames * channels;
368 dsp->active.frame_count = 0;
[43dec08]369
[57e8b3b]370 sb_dsp_write(dsp, dsp->active.mode);
[a3ab774]371 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
372 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
[43dec08]373
[9b3d999]374 ddf_log_verbose("Playback started, interrupt every %u samples "
[a3ab774]375 "(~1/%u sec)", dsp->active.samples,
[346643c]376 sampling_rate / (dsp->active.samples * channels));
[a3ab774]377
[1240bb9]378 dsp->status = DSP_PLAYBACK;
[9b3d999]379
[43dec08]380 return EOK;
381}
[e941bf8]382
[b497018]383int sb_dsp_stop_playback(sb_dsp_t *dsp)
[43dec08]384{
385 assert(dsp);
386 sb_dsp_write(dsp, DMA_16B_EXIT);
[b497018]387 ddf_log_debug("Stopping playback on buffer.");
[1240bb9]388 async_msg_0(dsp->event_exchange, PCM_EVENT_PLAYBACK_TERMINATED);
389 async_exchange_end(dsp->event_exchange);
390 dsp->event_exchange = NULL;
[43dec08]391 return EOK;
392}
[e941bf8]393
[57e8b3b]394int sb_dsp_start_record(sb_dsp_t *dsp, unsigned frames,
[346643c]395 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
[43dec08]396{
[a3ab774]397 assert(dsp);
398
399 if (!dsp->event_session)
400 return EINVAL;
401
402 /* Check supported parameters */
[57e8b3b]403 ddf_log_debug("Requested record: %u frames, %uHz, %s, %u channel(s).",
404 frames, sampling_rate, pcm_sample_format_str(format), channels);
[d2765ab3]405 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
[346643c]406 return ENOTSUP;
[a3ab774]407
[ff396ea]408 /* client requested regular interrupts */
409 if (frames) {
410 dsp->event_exchange = async_exchange_begin(dsp->event_session);
411 if (!dsp->event_exchange)
412 return ENOMEM;
413 dsp->ignore_interrupts = false;
414 }
[a3ab774]415
[346643c]416 const bool sign = (format == PCM_SAMPLE_SINT16_LE);
417
[a3ab774]418 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
419 sb_dsp_write(dsp, sampling_rate >> 8);
420 sb_dsp_write(dsp, sampling_rate & 0xff);
421
422 ddf_log_verbose("Sampling rate: %hhx:%hhx.",
423 sampling_rate >> 8, sampling_rate & 0xff);
424
425#ifdef AUTO_DMA_MODE
426 sb_dsp_write(dsp, AUTO_DMA_16B_AD_FIFO);
427#else
[ff396ea]428 sb_dsp_write(dsp, SINGLE_DMA_16B_AD);
[a3ab774]429#endif
430
431 dsp->active.mode = 0 |
[57e8b3b]432 (sign ? DSP_MODE_SIGNED : 0) |
433 (channels == 2 ? DSP_MODE_STEREO : 0);
434 dsp->active.samples = frames * channels;
435 dsp->active.frame_count = 0;
[a3ab774]436
[57e8b3b]437 sb_dsp_write(dsp, dsp->active.mode);
[a3ab774]438 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
439 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
440
441 ddf_log_verbose("Recording started started, interrupt every %u samples "
442 "(~1/%u sec)", dsp->active.samples,
[346643c]443 sampling_rate / (dsp->active.samples * channels));
[1240bb9]444 dsp->status = DSP_RECORDING;
[a3ab774]445
446 return EOK;
[43dec08]447}
[e941bf8]448
[b497018]449int sb_dsp_stop_record(sb_dsp_t *dsp)
[43dec08]450{
[a3ab774]451 assert(dsp);
452 sb_dsp_write(dsp, DMA_16B_EXIT);
[b497018]453 ddf_log_debug("Stopped recording");
[1240bb9]454 async_msg_0(dsp->event_exchange, PCM_EVENT_RECORDING_TERMINATED);
455 async_exchange_end(dsp->event_exchange);
456 dsp->event_exchange = NULL;
[a3ab774]457 return EOK;
[43dec08]458}
[763444e]459/**
460 * @}
461 */
Note: See TracBrowser for help on using the repository browser.