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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 05882233 was 05882233, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Unify various barrier includes into <barrier.h>

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