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

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

libdrv/audio,drv/audio/sb16: Update and implement recording interface.

Untested.

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