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

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

sb16: Implement test_format call.

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