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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d1582b50 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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