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
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
29 #include <soundcard.h>
31 #include <sys/soundcard.h>
33 #include "qemu-common.h"
34 #include "qemu-char.h"
37 #define AUDIO_CAP "oss"
38 #include "audio_int.h"
40 typedef struct OSSVoiceOut {
50 typedef struct OSSVoiceIn {
63 const char *devpath_out;
64 const char *devpath_in;
70 .devpath_out = "/dev/dsp",
71 .devpath_in = "/dev/dsp",
83 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
88 AUD_vlog (AUDIO_CAP, fmt, ap);
91 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
94 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
103 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
106 AUD_vlog (AUDIO_CAP, fmt, ap);
109 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
112 static void oss_anal_close (int *fdp)
114 int err = close (*fdp);
116 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
118 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
122 static void oss_helper_poll_out (void *opaque)
125 audio_run ("oss_poll_out");
128 static void oss_helper_poll_in (void *opaque)
131 audio_run ("oss_poll_in");
134 static int oss_poll_out (HWVoiceOut *hw)
136 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
138 return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
141 static int oss_poll_in (HWVoiceIn *hw)
143 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
145 return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
148 static int oss_write (SWVoiceOut *sw, void *buf, int len)
150 return audio_pcm_sw_write (sw, buf, len);
153 static int aud_to_ossfmt (audfmt_e fmt)
169 dolog ("Internal logic error: Bad audio format %d\n", fmt);
177 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
211 dolog ("Unrecognized audio format %d\n", ossfmt);
218 #if defined DEBUG_MISMATCHES || defined DEBUG
219 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
221 dolog ("parameter | requested value | obtained value\n");
222 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
223 dolog ("channels | %10d | %10d\n",
224 req->nchannels, obt->nchannels);
225 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
226 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
227 dolog ("fragsize | %10d | %10d\n",
228 req->fragsize, obt->fragsize);
232 static int oss_open (int in, struct oss_params *req,
233 struct oss_params *obt, int *pfd)
238 audio_buf_info abinfo;
239 int fmt, freq, nchannels;
240 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
241 const char *typ = in ? "ADC" : "DAC";
243 /* Kludge needed to have working mmap on Linux */
244 oflags = conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
245 fd = open (dspname, oflags | O_NONBLOCK);
247 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
252 nchannels = req->nchannels;
255 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
256 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
260 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
261 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
266 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
267 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
271 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
272 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
276 mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize);
277 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
278 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
279 req->nfrags, req->fragsize);
283 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
284 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
288 if (!abinfo.fragstotal || !abinfo.fragsize) {
289 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
290 abinfo.fragstotal, abinfo.fragsize, typ);
295 obt->nchannels = nchannels;
297 obt->nfrags = abinfo.fragstotal;
298 obt->fragsize = abinfo.fragsize;
301 #ifdef DEBUG_MISMATCHES
302 if ((req->fmt != obt->fmt) ||
303 (req->nchannels != obt->nchannels) ||
304 (req->freq != obt->freq) ||
305 (req->fragsize != obt->fragsize) ||
306 (req->nfrags != obt->nfrags)) {
307 dolog ("Audio parameters mismatch\n");
308 oss_dump_info (req, obt);
313 oss_dump_info (req, obt);
318 oss_anal_close (&fd);
322 static int oss_run_out (HWVoiceOut *hw)
324 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
325 int err, rpos, live, decr;
328 struct st_sample *src;
329 struct audio_buf_info abinfo;
330 struct count_info cntinfo;
333 live = audio_pcm_hw_get_live_out (hw);
338 bufsize = hw->samples << hw->info.shift;
343 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
345 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
349 if (cntinfo.ptr == oss->old_optr) {
350 if (abs (hw->samples - live) < 64) {
351 dolog ("warning: Overrun\n");
356 if (cntinfo.ptr > oss->old_optr) {
357 bytes = cntinfo.ptr - oss->old_optr;
360 bytes = bufsize + cntinfo.ptr - oss->old_optr;
363 decr = audio_MIN (bytes >> hw->info.shift, live);
366 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
368 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
372 if (abinfo.bytes > bufsize) {
374 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
375 "please report your OS/audio hw to malc@pulsesoft.com\n",
376 abinfo.bytes, bufsize);
378 abinfo.bytes = bufsize;
381 if (abinfo.bytes < 0) {
383 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
384 abinfo.bytes, bufsize);
389 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
398 int left_till_end_samples = hw->samples - rpos;
399 int convert_samples = audio_MIN (samples, left_till_end_samples);
401 src = hw->mix_buf + rpos;
402 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
404 hw->clip (dst, src, convert_samples);
408 written = write (oss->fd, dst, convert_samples << hw->info.shift);
409 /* XXX: follow errno recommendations ? */
413 "Failed to write %d bytes of audio data from %p\n",
414 convert_samples << hw->info.shift,
420 if (written != convert_samples << hw->info.shift) {
421 int wsamples = written >> hw->info.shift;
422 int wbytes = wsamples << hw->info.shift;
423 if (wbytes != written) {
424 dolog ("warning: Misaligned write %d (requested %d), "
426 wbytes, written, hw->info.align + 1);
429 rpos = (rpos + wsamples) % hw->samples;
434 rpos = (rpos + convert_samples) % hw->samples;
435 samples -= convert_samples;
438 oss->old_optr = cntinfo.ptr;
445 static void oss_fini_out (HWVoiceOut *hw)
448 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
450 ldebug ("oss_fini\n");
451 oss_anal_close (&oss->fd);
455 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
457 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
458 oss->pcm_buf, hw->samples << hw->info.shift);
462 qemu_free (oss->pcm_buf);
468 static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
470 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
471 struct oss_params req, obt;
475 audfmt_e effective_fmt;
476 struct audsettings obt_as;
480 req.fmt = aud_to_ossfmt (as->fmt);
482 req.nchannels = as->nchannels;
483 req.fragsize = conf.fragsize;
484 req.nfrags = conf.nfrags;
486 if (oss_open (0, &req, &obt, &fd)) {
490 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
492 oss_anal_close (&fd);
496 obt_as.freq = obt.freq;
497 obt_as.nchannels = obt.nchannels;
498 obt_as.fmt = effective_fmt;
499 obt_as.endianness = endianness;
501 audio_pcm_init_info (&hw->info, &obt_as);
502 oss->nfrags = obt.nfrags;
503 oss->fragsize = obt.fragsize;
505 if (obt.nfrags * obt.fragsize & hw->info.align) {
506 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
507 obt.nfrags * obt.fragsize, hw->info.align + 1);
510 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
514 oss->pcm_buf = mmap (
516 hw->samples << hw->info.shift,
517 PROT_READ | PROT_WRITE,
522 if (oss->pcm_buf == MAP_FAILED) {
523 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
524 hw->samples << hw->info.shift);
528 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
529 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
532 trig = PCM_ENABLE_OUTPUT;
533 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
536 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
545 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
547 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
548 oss->pcm_buf, hw->samples << hw->info.shift);
555 oss->pcm_buf = audio_calloc (
562 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
566 oss_anal_close (&fd);
575 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
580 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
583 poll_mode = va_arg (ap, int);
588 ldebug ("enabling voice\n");
589 if (poll_mode && oss_poll_out (hw)) {
592 hw->poll_mode = poll_mode;
598 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
599 trig = PCM_ENABLE_OUTPUT;
600 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
603 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
611 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
619 ldebug ("disabling voice\n");
621 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
622 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
630 static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
632 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
633 struct oss_params req, obt;
637 audfmt_e effective_fmt;
638 struct audsettings obt_as;
642 req.fmt = aud_to_ossfmt (as->fmt);
644 req.nchannels = as->nchannels;
645 req.fragsize = conf.fragsize;
646 req.nfrags = conf.nfrags;
647 if (oss_open (1, &req, &obt, &fd)) {
651 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
653 oss_anal_close (&fd);
657 obt_as.freq = obt.freq;
658 obt_as.nchannels = obt.nchannels;
659 obt_as.fmt = effective_fmt;
660 obt_as.endianness = endianness;
662 audio_pcm_init_info (&hw->info, &obt_as);
663 oss->nfrags = obt.nfrags;
664 oss->fragsize = obt.fragsize;
666 if (obt.nfrags * obt.fragsize & hw->info.align) {
667 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
668 obt.nfrags * obt.fragsize, hw->info.align + 1);
671 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
672 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
674 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
675 hw->samples, 1 << hw->info.shift);
676 oss_anal_close (&fd);
684 static void oss_fini_in (HWVoiceIn *hw)
686 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
688 oss_anal_close (&oss->fd);
691 qemu_free (oss->pcm_buf);
696 static int oss_run_in (HWVoiceIn *hw)
698 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
699 int hwshift = hw->info.shift;
701 int live = audio_pcm_hw_get_live_in (hw);
702 int dead = hw->samples - live;
703 size_t read_samples = 0;
708 { .add = hw->wpos, .len = 0 },
709 { .add = 0, .len = 0 }
716 if (hw->wpos + dead > hw->samples) {
717 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
718 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
721 bufs[0].len = dead << hwshift;
725 for (i = 0; i < 2; ++i) {
729 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
730 nread = read (oss->fd, p, bufs[i].len);
733 if (nread & hw->info.align) {
734 dolog ("warning: Misaligned read %zd (requested %d), "
735 "alignment %d\n", nread, bufs[i].add << hwshift,
738 read_samples += nread >> hwshift;
739 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
743 if (bufs[i].len - nread) {
752 "Failed to read %d bytes of audio (to %p)\n",
763 hw->wpos = (hw->wpos + read_samples) % hw->samples;
767 static int oss_read (SWVoiceIn *sw, void *buf, int size)
769 return audio_pcm_sw_read (sw, buf, size);
772 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
776 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
779 poll_mode = va_arg (ap, int);
784 if (poll_mode && oss_poll_in (hw)) {
787 hw->poll_mode = poll_mode;
793 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
800 static void *oss_audio_init (void)
805 static void oss_audio_fini (void *opaque)
810 static struct audio_option oss_options[] = {
814 .valp = &conf.fragsize,
815 .descr = "Fragment size in bytes"
820 .valp = &conf.nfrags,
821 .descr = "Number of fragments"
826 .valp = &conf.try_mmap,
827 .descr = "Try using memory mapped access"
832 .valp = &conf.devpath_out,
833 .descr = "Path to DAC device"
838 .valp = &conf.devpath_in,
839 .descr = "Path to ADC device"
845 .descr = "Turn on some debugging messages"
847 { /* End of list */ }
850 static struct audio_pcm_ops oss_pcm_ops = {
851 .init_out = oss_init_out,
852 .fini_out = oss_fini_out,
853 .run_out = oss_run_out,
855 .ctl_out = oss_ctl_out,
857 .init_in = oss_init_in,
858 .fini_in = oss_fini_in,
859 .run_in = oss_run_in,
864 struct audio_driver oss_audio_driver = {
866 .descr = "OSS http://www.opensound.com",
867 .options = oss_options,
868 .init = oss_audio_init,
869 .fini = oss_audio_fini,
870 .pcm_ops = &oss_pcm_ops,
872 .max_voices_out = INT_MAX,
873 .max_voices_in = INT_MAX,
874 .voice_size_out = sizeof (OSSVoiceOut),
875 .voice_size_in = sizeof (OSSVoiceIn)