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

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

sb16: Fix session cleanup.

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