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

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

sb16: Use arrays_size macro.

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