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

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

Merge mainline changes.

Conflict resulting from bool.h → stdbool.h move and ddf structs turning opaque.
Fails to boot to shell console.

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