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

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

sb16: Implement capabilities query.

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