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

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

sb16: Implement and enable buffer position capability.

  • Property mode set to 100644
File size: 12.2 KB
Line 
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
35#include <as.h>
36#include <bool.h>
37#include <ddi.h>
38#include <devman.h>
39#include <device/hw_res.h>
40#include <libarch/ddi.h>
41#include <libarch/barrier.h>
42#include <str_error.h>
43#include <audio_pcm_iface.h>
44
45#include "ddf_log.h"
46#include "dsp_commands.h"
47#include "dsp.h"
48
49/* Maximum allowed transfer size for ISA DMA transfers is 64kB */
50#define MAX_BUFFER_SIZE (4 * 1024) // use 4kB for now
51
52#ifndef DSP_RETRY_COUNT
53#define DSP_RETRY_COUNT 100
54#endif
55
56#define DSP_RESET_RESPONSE 0xaa
57#define DSP_RATE_LIMIT 45000
58
59#define AUTO_DMA_MODE
60
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}
77
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}
93
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}
102
103static inline void sb_dsp_start_active(sb_dsp_t *dsp, uint8_t command)
104{
105 sb_dsp_write(dsp, command);
106 sb_dsp_write(dsp, dsp->active.mode);
107 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
108 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
109}
110
111static inline void sb_dsp_set_sampling_rate(sb_dsp_t *dsp, unsigned rate)
112{
113 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
114 sb_dsp_write(dsp, rate >> 8);
115 sb_dsp_write(dsp, rate & 0xff);
116 ddf_log_verbose("Sampling rate: %hhx:%hhx.", rate >> 8, rate & 0xff);
117}
118
119static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
120{
121 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
122 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
123
124 const int ret = hw_res_dma_channel_setup(sess,
125 dsp->dma16_channel, pa, size,
126 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
127
128 async_hangup(sess);
129 return ret;
130}
131
132static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
133{
134 assert(dsp);
135 if (size > MAX_BUFFER_SIZE || size == 0 || (size % 2) == 1)
136 size = MAX_BUFFER_SIZE;
137 void *buffer = NULL, *pa = NULL;
138 int ret = dmamem_map_anonymous(size, AS_AREA_WRITE | AS_AREA_READ,
139 0, &pa, &buffer);
140 if (ret != EOK) {
141 ddf_log_error("Failed to allocate DMA buffer.");
142 return ENOMEM;
143 }
144
145 ddf_log_verbose("Setup dma buffer at %p(%p).", buffer, pa, size);
146 assert((uintptr_t)pa < (1 << 25));
147
148 /* Setup 16 bit channel */
149 ret = sb_setup_dma(dsp, (uintptr_t)pa, size);
150 if (ret == EOK) {
151 dsp->buffer.data = buffer;
152 dsp->buffer.size = size;
153 } else {
154 ddf_log_error("Failed to setup DMA16 channel: %s.",
155 str_error(ret));
156 dmamem_unmap_anonymous(buffer);
157 }
158 return ret;
159}
160
161static inline void sb_clear_buffer(sb_dsp_t *dsp)
162{
163 assert(dsp);
164 dmamem_unmap_anonymous(dsp->buffer.data);
165 dsp->buffer.data = NULL;
166 dsp->buffer.size = 0;
167}
168
169static inline size_t sample_count(pcm_sample_format_t format, size_t byte_count)
170{
171 return byte_count / pcm_sample_format_size(format);
172}
173
174int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
175 int dma8, int dma16)
176{
177 assert(dsp);
178 dsp->regs = regs;
179 dsp->dma8_channel = dma8;
180 dsp->dma16_channel = dma16;
181 dsp->event_session = NULL;
182 dsp->event_exchange = NULL;
183 dsp->sb_dev = dev;
184 dsp->status = DSP_STOPPED;
185 dsp->ignore_interrupts = false;
186 sb_dsp_reset(dsp);
187 /* "DSP takes about 100 microseconds to initialize itself" */
188 udelay(100);
189 uint8_t response;
190 const int ret = sb_dsp_read(dsp, &response);
191 if (ret != EOK) {
192 ddf_log_error("Failed to read DSP reset response value.");
193 return ret;
194 }
195 if (response != DSP_RESET_RESPONSE) {
196 ddf_log_error("Invalid DSP reset response: %x.", response);
197 return EIO;
198 }
199
200 /* Get DSP version number */
201 sb_dsp_write(dsp, DSP_VERSION);
202 sb_dsp_read(dsp, &dsp->version.major);
203 sb_dsp_read(dsp, &dsp->version.minor);
204
205 return ret;
206}
207
208void sb_dsp_interrupt(sb_dsp_t *dsp)
209{
210 assert(dsp);
211
212#ifndef AUTO_DMA_MODE
213 if (dsp->status == DSP_PLAYBACK) {
214 sb_dsp_start_active(dsp, SINGLE_DMA_16B_DA);
215 }
216
217 if (dsp->status == DSP_CAPTURE) {
218 sb_dsp_start_active(dsp, SINGLE_DMA_16B_AD);
219 }
220#endif
221
222 if (dsp->ignore_interrupts)
223 return;
224
225 dsp->active.frame_count +=
226 dsp->active.samples / ((dsp->active.mode & DSP_MODE_STEREO) ? 2 : 1);
227
228 if (dsp->event_exchange) {
229 switch (dsp->status) {
230 case DSP_PLAYBACK:
231 async_msg_1(dsp->event_exchange,
232 PCM_EVENT_FRAMES_PLAYED, dsp->active.frame_count);
233 break;
234 case DSP_CAPTURE:
235 async_msg_1(dsp->event_exchange,
236 PCM_EVENT_FRAMES_CAPTURED, dsp->active.frame_count);
237 break;
238 default:
239 case DSP_STOPPED:
240 ddf_log_warning("Interrupt while DSP stopped and "
241 "event exchange present. Terminating exchange");
242 async_exchange_end(dsp->event_exchange);
243 dsp->event_exchange = NULL;
244 }
245 } else {
246 ddf_log_warning("Interrupt with no event consumer.");
247 }
248}
249
250unsigned sb_dsp_query_cap(sb_dsp_t *dsp, audio_cap_t cap)
251{
252 ddf_log_verbose("Querying cap %u", cap);
253 switch(cap) {
254 case AUDIO_CAP_CAPTURE:
255 case AUDIO_CAP_PLAYBACK:
256 case AUDIO_CAP_INTERRUPT:
257 case AUDIO_CAP_BUFFER_POS:
258 return 1;
259 case AUDIO_CAP_MAX_BUFFER:
260 return MAX_BUFFER_SIZE;
261 case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
262 return 1;
263 case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
264 return 16535;
265 default:
266 return 0;
267 }
268}
269
270int sb_dsp_get_buffer_position(sb_dsp_t *dsp, size_t *pos)
271{
272 if (!dsp->buffer.data)
273 return ENOENT;
274
275 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
276 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
277
278 // TODO: Assumes DMA 16
279 const int remain = hw_res_dma_channel_remain(sess, dsp->dma16_channel);
280 async_hangup(sess);
281 if (remain >= 0) {
282 *pos = dsp->buffer.size - remain;
283 return EOK;
284 }
285 return remain;
286}
287
288int sb_dsp_test_format(sb_dsp_t *dsp, unsigned *channels, unsigned *rate,
289 pcm_sample_format_t *format)
290{
291 int ret = EOK;
292 if (*channels == 0 || *channels > 2) {
293 *channels = 2;
294 ret = ELIMIT;
295 }
296 if (*rate > DSP_RATE_LIMIT) {
297 *rate = DSP_RATE_LIMIT;
298 ret = ELIMIT;
299 }
300 //TODO 8bit DMA supports 8bit formats
301 if (*format != PCM_SAMPLE_SINT16_LE && *format != PCM_SAMPLE_UINT16_LE) {
302 *format = pcm_sample_format_is_signed(*format) ?
303 PCM_SAMPLE_SINT16_LE : PCM_SAMPLE_UINT16_LE;
304 ret = ELIMIT;
305 }
306 return ret;
307}
308
309int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size)
310{
311 assert(dsp);
312 assert(size);
313
314 /* buffer is already setup by for someone, refuse to work until
315 * it's released */
316 if (dsp->buffer.data)
317 return EBUSY;
318
319 const int ret = sb_setup_buffer(dsp, *size);
320 if (ret == EOK) {
321 ddf_log_debug("Providing buffer: %p, %zu B.",
322 dsp->buffer.data, dsp->buffer.size);
323
324 if (buffer)
325 *buffer = dsp->buffer.data;
326 if (size)
327 *size = dsp->buffer.size;
328 }
329 return ret;
330}
331
332int sb_dsp_set_event_session(sb_dsp_t *dsp, async_sess_t *session)
333{
334 assert(dsp);
335 assert(session);
336 if (dsp->event_session)
337 return EBUSY;
338 dsp->event_session = session;
339 ddf_log_debug("Set event session.");
340 return EOK;
341}
342
343int sb_dsp_release_buffer(sb_dsp_t *dsp)
344{
345 assert(dsp);
346 sb_clear_buffer(dsp);
347 async_exchange_end(dsp->event_exchange);
348 dsp->event_exchange = NULL;
349 if (dsp->event_session)
350 async_hangup(dsp->event_session);
351 dsp->event_session = NULL;
352 ddf_log_debug("DSP buffer released.");
353 return EOK;
354}
355
356int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned frames,
357 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
358{
359 assert(dsp);
360
361 if (!dsp->buffer.data)
362 return EINVAL;
363
364 /* Check supported parameters */
365 ddf_log_debug("Requested playback: %u frames, %uHz, %s, %u channel(s).",
366 frames, sampling_rate, pcm_sample_format_str(format), channels);
367 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
368 return ENOTSUP;
369
370 /* Client requested regular interrupts */
371 if (frames) {
372 if (!dsp->event_session)
373 return EINVAL;
374 dsp->event_exchange = async_exchange_begin(dsp->event_session);
375 if (!dsp->event_exchange)
376 return ENOMEM;
377 dsp->ignore_interrupts = false;
378 }
379
380 dsp->active.mode = 0
381 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0)
382 | (channels == 2 ? DSP_MODE_STEREO : 0);
383 dsp->active.samples = frames * channels;
384 dsp->active.frame_count = 0;
385
386 sb_dsp_set_sampling_rate(dsp, sampling_rate);
387
388#ifdef AUTO_DMA_MODE
389 sb_dsp_start_active(dsp, AUTO_DMA_16B_DA_FIFO);
390#else
391 sb_dsp_start_active(dsp, SINGLE_DMA_16B_DA);
392#endif
393
394 ddf_log_verbose("Playback started, interrupt every %u samples "
395 "(~1/%u sec)", dsp->active.samples,
396 sampling_rate / (dsp->active.samples * channels));
397
398 dsp->status = DSP_PLAYBACK;
399
400 return EOK;
401}
402
403int sb_dsp_stop_playback(sb_dsp_t *dsp)
404{
405 assert(dsp);
406 sb_dsp_write(dsp, DMA_16B_EXIT);
407 ddf_log_debug("Stopping playback on buffer.");
408 async_msg_0(dsp->event_exchange, PCM_EVENT_PLAYBACK_TERMINATED);
409 async_exchange_end(dsp->event_exchange);
410 dsp->event_exchange = NULL;
411 return EOK;
412}
413
414int sb_dsp_start_capture(sb_dsp_t *dsp, unsigned frames,
415 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
416{
417 assert(dsp);
418 if (!dsp->buffer.data)
419 return EINVAL;
420
421 /* Check supported parameters */
422 ddf_log_debug("Requested capture: %u frames, %uHz, %s, %u channel(s).",
423 frames, sampling_rate, pcm_sample_format_str(format), channels);
424 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
425 return ENOTSUP;
426
427 /* client requested regular interrupts */
428 if (frames) {
429 if (!dsp->event_session)
430 return EINVAL;
431 dsp->event_exchange = async_exchange_begin(dsp->event_session);
432 if (!dsp->event_exchange)
433 return ENOMEM;
434 dsp->ignore_interrupts = false;
435 }
436
437 dsp->active.mode = 0
438 | (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0)
439 | (channels == 2 ? DSP_MODE_STEREO : 0);
440 dsp->active.samples = frames * channels;
441 dsp->active.frame_count = 0;
442
443 sb_dsp_set_sampling_rate(dsp, sampling_rate);
444
445#ifdef AUTO_DMA_MODE
446 sb_dsp_start_active(dsp, AUTO_DMA_16B_AD_FIFO);
447#else
448 sb_dsp_start_active(dsp, SINGLE_DMA_16B_AD);
449#endif
450
451 ddf_log_verbose("Recording started started, interrupt every %u samples "
452 "(~1/%u sec)", dsp->active.samples,
453 sampling_rate / (dsp->active.samples * channels));
454 dsp->status = DSP_CAPTURE;
455
456 return EOK;
457}
458
459int sb_dsp_stop_capture(sb_dsp_t *dsp)
460{
461 assert(dsp);
462 sb_dsp_write(dsp, DMA_16B_EXIT);
463 ddf_log_debug("Stopped capture");
464 async_msg_0(dsp->event_exchange, PCM_EVENT_CAPTURE_TERMINATED);
465 async_exchange_end(dsp->event_exchange);
466 dsp->event_exchange = NULL;
467 return EOK;
468}
469/**
470 * @}
471 */
Note: See TracBrowser for help on using the repository browser.