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

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

Use named audio events.

audio/sb16: dsp uses more detailed status (not just playing/not-playing).
app/drec: Fix parameter order in.

  • Property mode set to 100644
File size: 11.4 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 <bool.h>
36#include <devman.h>
37#include <device/hw_res.h>
38#include <libarch/ddi.h>
39#include <libarch/barrier.h>
40#include <str_error.h>
41#include <audio_pcm_iface.h>
42
43#include "dma.h"
44#include "ddf_log.h"
45#include "dsp_commands.h"
46#include "dsp.h"
47
48#define BUFFER_ID 1
49#define MAX_BUFFER_SIZE (PAGE_SIZE)
50
51#ifndef DSP_RETRY_COUNT
52#define DSP_RETRY_COUNT 100
53#endif
54
55#define DSP_RESET_RESPONSE 0xaa
56
57#define AUTO_DMA_MODE
58
59static inline int sb_dsp_read(sb_dsp_t *dsp, uint8_t *data)
60{
61 assert(data);
62 assert(dsp);
63 uint8_t status;
64 size_t attempts = DSP_RETRY_COUNT;
65 do {
66 status = pio_read_8(&dsp->regs->dsp_read_status);
67 } while (--attempts && ((status & DSP_READ_READY) == 0));
68
69 if ((status & DSP_READ_READY) == 0)
70 return EIO;
71
72 *data = pio_read_8(&dsp->regs->dsp_data_read);
73 return EOK;
74}
75
76static inline int sb_dsp_write(sb_dsp_t *dsp, uint8_t data)
77{
78 assert(dsp);
79 uint8_t status;
80 size_t attempts = DSP_RETRY_COUNT;
81 do {
82 status = pio_read_8(&dsp->regs->dsp_write);
83 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
84
85 if ((status & DSP_WRITE_BUSY))
86 return EIO;
87
88 pio_write_8(&dsp->regs->dsp_write, data);
89 return EOK;
90}
91
92static inline void sb_dsp_reset(sb_dsp_t *dsp)
93{
94 assert(dsp);
95 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
96 pio_write_8(&dsp->regs->dsp_reset, 1);
97 udelay(3); /* Keep reset for 3 us */
98 pio_write_8(&dsp->regs->dsp_reset, 0);
99}
100
101static inline int sb_setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
102{
103 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
104 dsp->sb_dev->handle, IPC_FLAG_BLOCKING);
105 if (!sess)
106 return ENOMEM;
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 async_hangup(sess);
112 return ret;
113}
114
115static inline int sb_setup_buffer(sb_dsp_t *dsp, size_t size)
116{
117 assert(dsp);
118 if (size > MAX_BUFFER_SIZE || size == 0 || (size % 2) == 1)
119 size = MAX_BUFFER_SIZE;
120 uint8_t *buffer = dma_create_buffer24(size);
121 if (buffer == NULL) {
122 ddf_log_error("Failed to allocate DMA buffer.");
123 return ENOMEM;
124 }
125
126 const uintptr_t pa = addr_to_phys(buffer);
127 assert(pa < (1 << 25));
128
129 /* Set 16 bit channel */
130 const int ret = sb_setup_dma(dsp, pa, size);
131 if (ret == EOK) {
132 dsp->buffer.data = buffer;
133 dsp->buffer.size = size;
134 bzero(dsp->buffer.data, dsp->buffer.size);
135 } else {
136 ddf_log_error("Failed to setup DMA16 channel: %s.",
137 str_error(ret));
138 dma_destroy_buffer(buffer);
139 }
140 return ret;
141}
142
143static inline void sb_clear_buffer(sb_dsp_t *dsp)
144{
145 dma_destroy_buffer(dsp->buffer.data);
146 dsp->buffer.data = NULL;
147 dsp->buffer.size = 0;
148}
149
150static inline size_t sample_count(pcm_sample_format_t format, size_t byte_count)
151{
152 return byte_count / pcm_sample_format_size(format);
153}
154
155int sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
156 int dma8, int dma16)
157{
158 assert(dsp);
159 dsp->regs = regs;
160 dsp->dma8_channel = dma8;
161 dsp->dma16_channel = dma16;
162 dsp->event_session = NULL;
163 dsp->event_exchange = NULL;
164 dsp->sb_dev = dev;
165 dsp->status = DSP_STOPPED;
166 sb_dsp_reset(dsp);
167 /* "DSP takes about 100 microseconds to initialize itself" */
168 udelay(100);
169 uint8_t response;
170 const int ret = sb_dsp_read(dsp, &response);
171 if (ret != EOK) {
172 ddf_log_error("Failed to read DSP reset response value.");
173 return ret;
174 }
175 if (response != DSP_RESET_RESPONSE) {
176 ddf_log_error("Invalid DSP reset response: %x.", response);
177 return EIO;
178 }
179
180 /* Get DSP version number */
181 sb_dsp_write(dsp, DSP_VERSION);
182 sb_dsp_read(dsp, &dsp->version.major);
183 sb_dsp_read(dsp, &dsp->version.minor);
184
185 return ret;
186}
187
188void sb_dsp_interrupt(sb_dsp_t *dsp)
189{
190 assert(dsp);
191 if (dsp->event_exchange) {
192 switch (dsp->status) {
193 case DSP_PLAYBACK:
194 async_msg_0(dsp->event_exchange,
195 PCM_EVENT_PLAYBACK_DONE);
196 break;
197 case DSP_RECORDING:
198 async_msg_0(dsp->event_exchange,
199 PCM_EVENT_RECORDING_DONE);
200 break;
201 default:
202 case DSP_STOPPED:
203 ddf_log_warning("Interrupt while DSP stopped and "
204 "event exchange present. Terminating exchange");
205 async_exchange_end(dsp->event_exchange);
206 dsp->event_exchange = NULL;
207 }
208 } else {
209 ddf_log_warning("Interrupt with no event consumer.");
210 }
211#ifndef AUTO_DMA_MODE
212 if (dsp->active.playing)
213 sb_dsp_write(dsp, SINGLE_DMA_16B_DA);
214 else
215 sb_dsp_write(dsp, SINGLE_DMA_16B_AD);
216
217 sb_dsp_write(dsp, dsp->active.mode);
218 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
219 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
220#endif
221}
222
223int sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size, unsigned *id)
224{
225 assert(dsp);
226 assert(size);
227
228 /* buffer is already setup by for someone, refuse to work until
229 * it's released */
230 if (dsp->buffer.data)
231 return EBUSY;
232
233 const int ret = sb_setup_buffer(dsp, *size);
234 if (ret == EOK) {
235 ddf_log_debug("Providing buffer(%u): %p, %zu B.",
236 BUFFER_ID, dsp->buffer.data, dsp->buffer.size);
237
238 if (buffer)
239 *buffer = dsp->buffer.data;
240 if (size)
241 *size = dsp->buffer.size;
242 if (id)
243 *id = BUFFER_ID;
244 }
245 return ret;
246}
247
248int sb_dsp_set_event_session(sb_dsp_t *dsp, unsigned id, async_sess_t *session)
249{
250 assert(dsp);
251 assert(session);
252 if (id != BUFFER_ID)
253 return ENOENT;
254 if (dsp->event_session)
255 return EBUSY;
256 dsp->event_session = session;
257 ddf_log_debug("Set event session.");
258 return EOK;
259}
260
261int sb_dsp_release_buffer(sb_dsp_t *dsp, unsigned id)
262{
263 assert(dsp);
264 if (id != BUFFER_ID)
265 return ENOENT;
266 sb_clear_buffer(dsp);
267 async_exchange_end(dsp->event_exchange);
268 dsp->event_exchange = NULL;
269 if (dsp->event_session)
270 async_hangup(dsp->event_session);
271 dsp->event_session = NULL;
272 ddf_log_debug("DSP buffer released.");
273 return EOK;
274}
275
276int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned parts,
277 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
278{
279 assert(dsp);
280
281 if (!dsp->event_session)
282 return EINVAL;
283
284 /* Play block size must be even number (we use DMA 16)*/
285 if (dsp->buffer.size % (parts * 2))
286 return EINVAL;
287
288 const unsigned play_block_size = dsp->buffer.size / parts;
289
290 /* Check supported parameters */
291 ddf_log_debug("Requested playback on buffer \"%u\" (%u parts): %uHz, "
292 "%s, %u channel(s).", id, parts, sampling_rate,
293 pcm_sample_format_str(format), channels);
294 if (id != BUFFER_ID)
295 return ENOENT;
296 if (channels != 1 && channels != 2)
297 return ENOTSUP;
298 if (sampling_rate > 44100)
299 return ENOTSUP;
300 // FIXME We only support 16 bit playback
301 if (format != PCM_SAMPLE_UINT16_LE && format != PCM_SAMPLE_SINT16_LE)
302 return ENOTSUP;
303
304 dsp->event_exchange = async_exchange_begin(dsp->event_session);
305 if (!dsp->event_exchange)
306 return ENOMEM;
307
308 const bool sign = (format == PCM_SAMPLE_SINT16_LE);
309
310 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
311 sb_dsp_write(dsp, sampling_rate >> 8);
312 sb_dsp_write(dsp, sampling_rate & 0xff);
313
314 ddf_log_verbose("Sample rate: %hhx:%hhx.",
315 sampling_rate >> 8, sampling_rate & 0xff);
316
317#ifdef AUTO_DMA_MODE
318 sb_dsp_write(dsp, AUTO_DMA_16B_DA_FIFO);
319#else
320 sb_dsp_write(dsp, SINGLE_DMA_16B_DA_FIFO);
321#endif
322
323 dsp->active.mode = 0 |
324 (sign ? DSP_MODE_SIGNED : 0) | (channels == 2 ? DSP_MODE_STEREO : 0);
325 sb_dsp_write(dsp, dsp->active.mode);
326
327 dsp->active.samples = sample_count(format, play_block_size);
328 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
329 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
330
331 ddf_log_verbose("Playback started, interrupt every %u samples "
332 "(~1/%u sec)", dsp->active.samples,
333 sampling_rate / (dsp->active.samples * channels));
334
335 dsp->status = DSP_PLAYBACK;
336
337 return EOK;
338}
339
340int sb_dsp_stop_playback(sb_dsp_t *dsp, unsigned id)
341{
342 assert(dsp);
343 if (id != BUFFER_ID)
344 return ENOENT;
345 sb_dsp_write(dsp, DMA_16B_EXIT);
346 ddf_log_debug("Stopping playback on buffer %u.", id);
347 async_msg_0(dsp->event_exchange, PCM_EVENT_PLAYBACK_TERMINATED);
348 async_exchange_end(dsp->event_exchange);
349 dsp->event_exchange = NULL;
350 return EOK;
351}
352
353int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned parts,
354 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
355{
356 assert(dsp);
357
358 if (!dsp->event_session)
359 return EINVAL;
360
361 /* Play block size must be even number (we use DMA 16)*/
362 if (dsp->buffer.size % (parts * 2))
363 return EINVAL;
364
365 const unsigned play_block_size = dsp->buffer.size / parts;
366
367 /* Check supported parameters */
368 ddf_log_debug("Requested recording on buffer \"%u\" (%u parts): %uHz, "
369 "%s, %u channel(s).", id, parts, sampling_rate,
370 pcm_sample_format_str(format), channels);
371 if (id != BUFFER_ID)
372 return ENOENT;
373 if (channels != 1 && channels != 2)
374 return ENOTSUP;
375 if (sampling_rate > 44100)
376 return ENOTSUP;
377 // FIXME We only support 16 bit recording
378 if (format != PCM_SAMPLE_UINT16_LE && format != PCM_SAMPLE_SINT16_LE)
379 return ENOTSUP;
380
381 dsp->event_exchange = async_exchange_begin(dsp->event_session);
382 if (!dsp->event_exchange)
383 return ENOMEM;
384
385 const bool sign = (format == PCM_SAMPLE_SINT16_LE);
386
387 sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
388 sb_dsp_write(dsp, sampling_rate >> 8);
389 sb_dsp_write(dsp, sampling_rate & 0xff);
390
391 ddf_log_verbose("Sampling rate: %hhx:%hhx.",
392 sampling_rate >> 8, sampling_rate & 0xff);
393
394#ifdef AUTO_DMA_MODE
395 sb_dsp_write(dsp, AUTO_DMA_16B_AD_FIFO);
396#else
397 sb_dsp_write(dsp, SINGLE_DMA_16B_AD_FIFO);
398#endif
399
400 dsp->active.mode = 0 |
401 (sign ? DSP_MODE_SIGNED : 0) | (channels == 2 ? DSP_MODE_STEREO : 0);
402 sb_dsp_write(dsp, dsp->active.mode);
403
404 dsp->active.samples = sample_count(format, play_block_size);
405 sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
406 sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
407
408 ddf_log_verbose("Recording started started, interrupt every %u samples "
409 "(~1/%u sec)", dsp->active.samples,
410 sampling_rate / (dsp->active.samples * channels));
411 dsp->status = DSP_RECORDING;
412
413 return EOK;
414}
415
416int sb_dsp_stop_record(sb_dsp_t *dsp, unsigned id)
417{
418 assert(dsp);
419 if (id != BUFFER_ID)
420 return ENOENT;
421 sb_dsp_write(dsp, DMA_16B_EXIT);
422 ddf_log_debug("Stopping playback on buffer %u.", id);
423 async_msg_0(dsp->event_exchange, PCM_EVENT_RECORDING_TERMINATED);
424 async_exchange_end(dsp->event_exchange);
425 dsp->event_exchange = NULL;
426 return EOK;
427}
428/**
429 * @}
430 */
Note: See TracBrowser for help on using the repository browser.