2 * QEMU OSS audio driver
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
30 #define AUDIO_CAP "oss"
31 #include "audio_int.h"
33 typedef struct OSSVoiceOut {
43 typedef struct OSSVoiceIn {
56 const char *devpath_out;
57 const char *devpath_in;
62 .devpath_out = "/dev/dsp",
63 .devpath_in = "/dev/dsp"
74 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
78 AUD_vlog (AUDIO_CAP, fmt, ap);
81 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
85 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
94 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
97 AUD_vlog (AUDIO_CAP, fmt, ap);
100 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
103 static void oss_anal_close (int *fdp)
105 int err = close (*fdp);
107 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
112 static int oss_write (SWVoiceOut *sw, void *buf, int len)
114 return audio_pcm_sw_write (sw, buf, len);
117 static int aud_to_ossfmt (audfmt_e fmt)
133 dolog ("Internal logic error: Bad audio format %d\n", fmt);
141 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
175 dolog ("Unrecognized audio format %d\n", ossfmt);
182 #if defined DEBUG_MISMATCHES || defined DEBUG
183 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
185 dolog ("parameter | requested value | obtained value\n");
186 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
187 dolog ("channels | %10d | %10d\n",
188 req->nchannels, obt->nchannels);
189 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
190 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
191 dolog ("fragsize | %10d | %10d\n",
192 req->fragsize, obt->fragsize);
196 static int oss_open (int in, struct oss_params *req,
197 struct oss_params *obt, int *pfd)
201 audio_buf_info abinfo;
202 int fmt, freq, nchannels;
203 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
204 const char *typ = in ? "ADC" : "DAC";
206 fd = open (dspname, (in ? O_RDONLY : O_WRONLY) | O_NONBLOCK);
208 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
213 nchannels = req->nchannels;
216 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
217 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
221 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
222 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
227 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
228 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
232 if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) {
233 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
237 mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize);
238 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
239 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
240 req->nfrags, req->fragsize);
244 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
245 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
250 obt->nchannels = nchannels;
252 obt->nfrags = abinfo.fragstotal;
253 obt->fragsize = abinfo.fragsize;
256 #ifdef DEBUG_MISMATCHES
257 if ((req->fmt != obt->fmt) ||
258 (req->nchannels != obt->nchannels) ||
259 (req->freq != obt->freq) ||
260 (req->fragsize != obt->fragsize) ||
261 (req->nfrags != obt->nfrags)) {
262 dolog ("Audio parameters mismatch\n");
263 oss_dump_info (req, obt);
268 oss_dump_info (req, obt);
273 oss_anal_close (&fd);
277 static int oss_run_out (HWVoiceOut *hw)
279 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
280 int err, rpos, live, decr;
284 struct audio_buf_info abinfo;
285 struct count_info cntinfo;
288 live = audio_pcm_hw_get_live_out (hw);
293 bufsize = hw->samples << hw->info.shift;
298 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
300 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
304 if (cntinfo.ptr == oss->old_optr) {
305 if (abs (hw->samples - live) < 64) {
306 dolog ("warning: Overrun\n");
311 if (cntinfo.ptr > oss->old_optr) {
312 bytes = cntinfo.ptr - oss->old_optr;
315 bytes = bufsize + cntinfo.ptr - oss->old_optr;
318 decr = audio_MIN (bytes >> hw->info.shift, live);
321 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
323 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
327 if (abinfo.bytes < 0 || abinfo.bytes > bufsize) {
328 ldebug ("warning: Invalid available size, size=%d bufsize=%d\n",
329 abinfo.bytes, bufsize);
333 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
342 int left_till_end_samples = hw->samples - rpos;
343 int convert_samples = audio_MIN (samples, left_till_end_samples);
345 src = hw->mix_buf + rpos;
346 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
348 hw->clip (dst, src, convert_samples);
352 written = write (oss->fd, dst, convert_samples << hw->info.shift);
353 /* XXX: follow errno recommendations ? */
357 "Failed to write %d bytes of audio data from %p\n",
358 convert_samples << hw->info.shift,
364 if (written != convert_samples << hw->info.shift) {
365 int wsamples = written >> hw->info.shift;
366 int wbytes = wsamples << hw->info.shift;
367 if (wbytes != written) {
368 dolog ("warning: Misaligned write %d (requested %d), "
370 wbytes, written, hw->info.align + 1);
372 mixeng_clear (src, wsamples);
374 rpos = (rpos + wsamples) % hw->samples;
379 mixeng_clear (src, convert_samples);
381 rpos = (rpos + convert_samples) % hw->samples;
382 samples -= convert_samples;
385 oss->old_optr = cntinfo.ptr;
392 static void oss_fini_out (HWVoiceOut *hw)
395 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
397 ldebug ("oss_fini\n");
398 oss_anal_close (&oss->fd);
402 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
404 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
405 oss->pcm_buf, hw->samples << hw->info.shift);
409 qemu_free (oss->pcm_buf);
415 static int oss_init_out (HWVoiceOut *hw, audsettings_t *as)
417 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
418 struct oss_params req, obt;
422 audfmt_e effective_fmt;
423 audsettings_t obt_as;
425 req.fmt = aud_to_ossfmt (as->fmt);
427 req.nchannels = as->nchannels;
428 req.fragsize = conf.fragsize;
429 req.nfrags = conf.nfrags;
431 if (oss_open (0, &req, &obt, &fd)) {
435 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
437 oss_anal_close (&fd);
441 obt_as.freq = obt.freq;
442 obt_as.nchannels = obt.nchannels;
443 obt_as.fmt = effective_fmt;
445 audio_pcm_init_info (
448 audio_need_to_swap_endian (endianness)
450 oss->nfrags = obt.nfrags;
451 oss->fragsize = obt.fragsize;
453 if (obt.nfrags * obt.fragsize & hw->info.align) {
454 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
455 obt.nfrags * obt.fragsize, hw->info.align + 1);
458 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
462 oss->pcm_buf = mmap (
464 hw->samples << hw->info.shift,
465 PROT_READ | PROT_WRITE,
470 if (oss->pcm_buf == MAP_FAILED) {
471 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
472 hw->samples << hw->info.shift);
476 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
477 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
480 trig = PCM_ENABLE_OUTPUT;
481 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
484 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
493 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
495 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
496 oss->pcm_buf, hw->samples << hw->info.shift);
503 oss->pcm_buf = audio_calloc (
510 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
514 oss_anal_close (&fd);
523 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
526 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
534 ldebug ("enabling voice\n");
535 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
536 trig = PCM_ENABLE_OUTPUT;
537 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
540 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
547 ldebug ("disabling voice\n");
549 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
550 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
558 static int oss_init_in (HWVoiceIn *hw, audsettings_t *as)
560 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
561 struct oss_params req, obt;
565 audfmt_e effective_fmt;
566 audsettings_t obt_as;
568 req.fmt = aud_to_ossfmt (as->fmt);
570 req.nchannels = as->nchannels;
571 req.fragsize = conf.fragsize;
572 req.nfrags = conf.nfrags;
573 if (oss_open (1, &req, &obt, &fd)) {
577 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
579 oss_anal_close (&fd);
583 obt_as.freq = obt.freq;
584 obt_as.nchannels = obt.nchannels;
585 obt_as.fmt = effective_fmt;
587 audio_pcm_init_info (
590 audio_need_to_swap_endian (endianness)
592 oss->nfrags = obt.nfrags;
593 oss->fragsize = obt.fragsize;
595 if (obt.nfrags * obt.fragsize & hw->info.align) {
596 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
597 obt.nfrags * obt.fragsize, hw->info.align + 1);
600 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
601 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
603 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
604 hw->samples, 1 << hw->info.shift);
605 oss_anal_close (&fd);
613 static void oss_fini_in (HWVoiceIn *hw)
615 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
617 oss_anal_close (&oss->fd);
620 qemu_free (oss->pcm_buf);
625 static int oss_run_in (HWVoiceIn *hw)
627 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
628 int hwshift = hw->info.shift;
630 int live = audio_pcm_hw_get_live_in (hw);
631 int dead = hw->samples - live;
632 size_t read_samples = 0;
645 if (hw->wpos + dead > hw->samples) {
646 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
647 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
650 bufs[0].len = dead << hwshift;
654 for (i = 0; i < 2; ++i) {
658 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
659 nread = read (oss->fd, p, bufs[i].len);
662 if (nread & hw->info.align) {
663 dolog ("warning: Misaligned read %zd (requested %d), "
664 "alignment %d\n", nread, bufs[i].add << hwshift,
667 read_samples += nread >> hwshift;
668 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
672 if (bufs[i].len - nread) {
681 "Failed to read %d bytes of audio (to %p)\n",
692 hw->wpos = (hw->wpos + read_samples) % hw->samples;
696 static int oss_read (SWVoiceIn *sw, void *buf, int size)
698 return audio_pcm_sw_read (sw, buf, size);
701 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
708 static void *oss_audio_init (void)
713 static void oss_audio_fini (void *opaque)
718 static struct audio_option oss_options[] = {
719 {"FRAGSIZE", AUD_OPT_INT, &conf.fragsize,
720 "Fragment size in bytes", NULL, 0},
721 {"NFRAGS", AUD_OPT_INT, &conf.nfrags,
722 "Number of fragments", NULL, 0},
723 {"MMAP", AUD_OPT_BOOL, &conf.try_mmap,
724 "Try using memory mapped access", NULL, 0},
725 {"DAC_DEV", AUD_OPT_STR, &conf.devpath_out,
726 "Path to DAC device", NULL, 0},
727 {"ADC_DEV", AUD_OPT_STR, &conf.devpath_in,
728 "Path to ADC device", NULL, 0},
729 {NULL, 0, NULL, NULL, NULL, 0}
732 static struct audio_pcm_ops oss_pcm_ops = {
746 struct audio_driver oss_audio_driver = {
747 INIT_FIELD (name = ) "oss",
748 INIT_FIELD (descr = ) "OSS http://www.opensound.com",
749 INIT_FIELD (options = ) oss_options,
750 INIT_FIELD (init = ) oss_audio_init,
751 INIT_FIELD (fini = ) oss_audio_fini,
752 INIT_FIELD (pcm_ops = ) &oss_pcm_ops,
753 INIT_FIELD (can_be_default = ) 1,
754 INIT_FIELD (max_voices_out = ) INT_MAX,
755 INIT_FIELD (max_voices_in = ) INT_MAX,
756 INIT_FIELD (voice_size_out = ) sizeof (OSSVoiceOut),
757 INIT_FIELD (voice_size_in = ) sizeof (OSSVoiceIn)