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 "host-utils.h"
35 #include "qemu-char.h"
38 #define AUDIO_CAP "oss"
39 #include "audio_int.h"
41 typedef struct OSSVoiceOut {
51 typedef struct OSSVoiceIn {
64 const char *devpath_out;
65 const char *devpath_in;
73 .devpath_out = "/dev/dsp",
74 .devpath_in = "/dev/dsp",
88 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
93 AUD_vlog (AUDIO_CAP, fmt, ap);
96 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
99 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
108 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
111 AUD_vlog (AUDIO_CAP, fmt, ap);
114 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
117 static void oss_anal_close (int *fdp)
119 int err = close (*fdp);
121 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
123 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
127 static void oss_helper_poll_out (void *opaque)
130 audio_run ("oss_poll_out");
133 static void oss_helper_poll_in (void *opaque)
136 audio_run ("oss_poll_in");
139 static int oss_poll_out (HWVoiceOut *hw)
141 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
143 return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
146 static int oss_poll_in (HWVoiceIn *hw)
148 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
150 return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
153 static int oss_write (SWVoiceOut *sw, void *buf, int len)
155 return audio_pcm_sw_write (sw, buf, len);
158 static int aud_to_ossfmt (audfmt_e fmt)
174 dolog ("Internal logic error: Bad audio format %d\n", fmt);
182 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
216 dolog ("Unrecognized audio format %d\n", ossfmt);
223 #if defined DEBUG_MISMATCHES || defined DEBUG
224 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
226 dolog ("parameter | requested value | obtained value\n");
227 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
228 dolog ("channels | %10d | %10d\n",
229 req->nchannels, obt->nchannels);
230 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
231 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
232 dolog ("fragsize | %10d | %10d\n",
233 req->fragsize, obt->fragsize);
237 static int oss_open (int in, struct oss_params *req,
238 struct oss_params *obt, int *pfd)
242 int oflags = conf.exclusive ? O_EXCL : 0;
243 audio_buf_info abinfo;
244 int fmt, freq, nchannels;
245 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
246 const char *typ = in ? "ADC" : "DAC";
248 /* Kludge needed to have working mmap on Linux */
249 oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
251 fd = open (dspname, oflags | O_NONBLOCK);
253 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
258 nchannels = req->nchannels;
261 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
262 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
266 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
267 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
272 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
273 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
277 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
278 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
282 if (ioctl (fd, OSS_GETVERSION, &version)) {
283 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
288 dolog ("OSS version = %#x\n", version);
291 #ifdef SNDCTL_DSP_POLICY
292 if (conf.policy >= 0 && version >= 0x040000) {
293 int policy = conf.policy;
294 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
295 oss_logerr2 (errno, typ, "Failed to set timing policy to %d\n",
303 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
304 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
305 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
306 req->nfrags, req->fragsize);
311 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
312 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
316 if (!abinfo.fragstotal || !abinfo.fragsize) {
317 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
318 abinfo.fragstotal, abinfo.fragsize, typ);
323 obt->nchannels = nchannels;
325 obt->nfrags = abinfo.fragstotal;
326 obt->fragsize = abinfo.fragsize;
329 #ifdef DEBUG_MISMATCHES
330 if ((req->fmt != obt->fmt) ||
331 (req->nchannels != obt->nchannels) ||
332 (req->freq != obt->freq) ||
333 (req->fragsize != obt->fragsize) ||
334 (req->nfrags != obt->nfrags)) {
335 dolog ("Audio parameters mismatch\n");
336 oss_dump_info (req, obt);
341 oss_dump_info (req, obt);
346 oss_anal_close (&fd);
350 static int oss_run_out (HWVoiceOut *hw)
352 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
353 int err, rpos, live, decr;
356 struct st_sample *src;
357 struct audio_buf_info abinfo;
358 struct count_info cntinfo;
361 live = audio_pcm_hw_get_live_out (hw);
366 bufsize = hw->samples << hw->info.shift;
371 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
373 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
377 if (cntinfo.ptr == oss->old_optr) {
378 if (abs (hw->samples - live) < 64) {
379 dolog ("warning: Overrun\n");
384 if (cntinfo.ptr > oss->old_optr) {
385 bytes = cntinfo.ptr - oss->old_optr;
388 bytes = bufsize + cntinfo.ptr - oss->old_optr;
391 decr = audio_MIN (bytes >> hw->info.shift, live);
394 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
396 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
400 if (abinfo.bytes > bufsize) {
402 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
404 abinfo.bytes, bufsize);
406 abinfo.bytes = bufsize;
409 if (abinfo.bytes < 0) {
411 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
412 abinfo.bytes, bufsize);
417 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
426 int left_till_end_samples = hw->samples - rpos;
427 int convert_samples = audio_MIN (samples, left_till_end_samples);
429 src = hw->mix_buf + rpos;
430 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
432 hw->clip (dst, src, convert_samples);
436 written = write (oss->fd, dst, convert_samples << hw->info.shift);
437 /* XXX: follow errno recommendations ? */
441 "Failed to write %d bytes of audio data from %p\n",
442 convert_samples << hw->info.shift,
448 if (written != convert_samples << hw->info.shift) {
449 int wsamples = written >> hw->info.shift;
450 int wbytes = wsamples << hw->info.shift;
451 if (wbytes != written) {
452 dolog ("warning: Misaligned write %d (requested %d), "
454 wbytes, written, hw->info.align + 1);
457 rpos = (rpos + wsamples) % hw->samples;
462 rpos = (rpos + convert_samples) % hw->samples;
463 samples -= convert_samples;
466 oss->old_optr = cntinfo.ptr;
473 static void oss_fini_out (HWVoiceOut *hw)
476 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
478 ldebug ("oss_fini\n");
479 oss_anal_close (&oss->fd);
483 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
485 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
486 oss->pcm_buf, hw->samples << hw->info.shift);
490 qemu_free (oss->pcm_buf);
496 static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
498 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
499 struct oss_params req, obt;
503 audfmt_e effective_fmt;
504 struct audsettings obt_as;
508 req.fmt = aud_to_ossfmt (as->fmt);
510 req.nchannels = as->nchannels;
511 req.fragsize = conf.fragsize;
512 req.nfrags = conf.nfrags;
514 if (oss_open (0, &req, &obt, &fd)) {
518 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
520 oss_anal_close (&fd);
524 obt_as.freq = obt.freq;
525 obt_as.nchannels = obt.nchannels;
526 obt_as.fmt = effective_fmt;
527 obt_as.endianness = endianness;
529 audio_pcm_init_info (&hw->info, &obt_as);
530 oss->nfrags = obt.nfrags;
531 oss->fragsize = obt.fragsize;
533 if (obt.nfrags * obt.fragsize & hw->info.align) {
534 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
535 obt.nfrags * obt.fragsize, hw->info.align + 1);
538 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
542 oss->pcm_buf = mmap (
544 hw->samples << hw->info.shift,
545 PROT_READ | PROT_WRITE,
550 if (oss->pcm_buf == MAP_FAILED) {
551 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
552 hw->samples << hw->info.shift);
556 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
557 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
560 trig = PCM_ENABLE_OUTPUT;
561 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
564 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
573 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
575 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
576 oss->pcm_buf, hw->samples << hw->info.shift);
583 oss->pcm_buf = audio_calloc (
590 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
594 oss_anal_close (&fd);
603 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
608 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
611 poll_mode = va_arg (ap, int);
616 ldebug ("enabling voice\n");
617 if (poll_mode && oss_poll_out (hw)) {
620 hw->poll_mode = poll_mode;
626 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
627 trig = PCM_ENABLE_OUTPUT;
628 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
631 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
639 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
647 ldebug ("disabling voice\n");
649 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
650 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
658 static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
660 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
661 struct oss_params req, obt;
665 audfmt_e effective_fmt;
666 struct audsettings obt_as;
670 req.fmt = aud_to_ossfmt (as->fmt);
672 req.nchannels = as->nchannels;
673 req.fragsize = conf.fragsize;
674 req.nfrags = conf.nfrags;
675 if (oss_open (1, &req, &obt, &fd)) {
679 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
681 oss_anal_close (&fd);
685 obt_as.freq = obt.freq;
686 obt_as.nchannels = obt.nchannels;
687 obt_as.fmt = effective_fmt;
688 obt_as.endianness = endianness;
690 audio_pcm_init_info (&hw->info, &obt_as);
691 oss->nfrags = obt.nfrags;
692 oss->fragsize = obt.fragsize;
694 if (obt.nfrags * obt.fragsize & hw->info.align) {
695 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
696 obt.nfrags * obt.fragsize, hw->info.align + 1);
699 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
700 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
702 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
703 hw->samples, 1 << hw->info.shift);
704 oss_anal_close (&fd);
712 static void oss_fini_in (HWVoiceIn *hw)
714 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
716 oss_anal_close (&oss->fd);
719 qemu_free (oss->pcm_buf);
724 static int oss_run_in (HWVoiceIn *hw)
726 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
727 int hwshift = hw->info.shift;
729 int live = audio_pcm_hw_get_live_in (hw);
730 int dead = hw->samples - live;
731 size_t read_samples = 0;
736 { .add = hw->wpos, .len = 0 },
737 { .add = 0, .len = 0 }
744 if (hw->wpos + dead > hw->samples) {
745 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
746 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
749 bufs[0].len = dead << hwshift;
753 for (i = 0; i < 2; ++i) {
757 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
758 nread = read (oss->fd, p, bufs[i].len);
761 if (nread & hw->info.align) {
762 dolog ("warning: Misaligned read %zd (requested %d), "
763 "alignment %d\n", nread, bufs[i].add << hwshift,
766 read_samples += nread >> hwshift;
767 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
771 if (bufs[i].len - nread) {
780 "Failed to read %d bytes of audio (to %p)\n",
791 hw->wpos = (hw->wpos + read_samples) % hw->samples;
795 static int oss_read (SWVoiceIn *sw, void *buf, int size)
797 return audio_pcm_sw_read (sw, buf, size);
800 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
804 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
807 poll_mode = va_arg (ap, int);
812 if (poll_mode && oss_poll_in (hw)) {
815 hw->poll_mode = poll_mode;
821 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
828 static void *oss_audio_init (void)
833 static void oss_audio_fini (void *opaque)
838 static struct audio_option oss_options[] = {
842 .valp = &conf.fragsize,
843 .descr = "Fragment size in bytes"
848 .valp = &conf.nfrags,
849 .descr = "Number of fragments"
854 .valp = &conf.try_mmap,
855 .descr = "Try using memory mapped access"
860 .valp = &conf.devpath_out,
861 .descr = "Path to DAC device"
866 .valp = &conf.devpath_in,
867 .descr = "Path to ADC device"
872 .valp = &conf.exclusive,
873 .descr = "Open device in exclusive mode (vmix wont work)"
875 #ifdef SNDCTL_DSP_POLICY
879 .valp = &conf.policy,
880 .descr = "Set the timing policy of the device, -1 to use fragment mode",
887 .descr = "Turn on some debugging messages"
889 { /* End of list */ }
892 static struct audio_pcm_ops oss_pcm_ops = {
893 .init_out = oss_init_out,
894 .fini_out = oss_fini_out,
895 .run_out = oss_run_out,
897 .ctl_out = oss_ctl_out,
899 .init_in = oss_init_in,
900 .fini_in = oss_fini_in,
901 .run_in = oss_run_in,
906 struct audio_driver oss_audio_driver = {
908 .descr = "OSS http://www.opensound.com",
909 .options = oss_options,
910 .init = oss_audio_init,
911 .fini = oss_audio_fini,
912 .pcm_ops = &oss_pcm_ops,
914 .max_voices_out = INT_MAX,
915 .max_voices_in = INT_MAX,
916 .voice_size_out = sizeof (OSSVoiceOut),
917 .voice_size_in = sizeof (OSSVoiceIn)