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
24 #include "qemu/osdep.h"
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include "qemu-common.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/host-utils.h"
33 #define AUDIO_CAP "oss"
34 #include "audio_int.h"
36 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
37 #define USE_DSP_POLICY
40 typedef struct OSSConf {
44 const char *devpath_out;
45 const char *devpath_in;
50 typedef struct OSSVoiceOut {
62 typedef struct OSSVoiceIn {
79 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
84 AUD_vlog (AUDIO_CAP, fmt, ap);
87 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
90 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
99 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
102 AUD_vlog (AUDIO_CAP, fmt, ap);
105 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
108 static void oss_anal_close (int *fdp)
112 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
115 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
120 static void oss_helper_poll_out (void *opaque)
123 audio_run ("oss_poll_out");
126 static void oss_helper_poll_in (void *opaque)
129 audio_run ("oss_poll_in");
132 static void oss_poll_out (HWVoiceOut *hw)
134 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
136 qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
139 static void oss_poll_in (HWVoiceIn *hw)
141 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
143 qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
146 static int oss_write (SWVoiceOut *sw, void *buf, int len)
148 return audio_pcm_sw_write (sw, buf, len);
151 static int aud_to_ossfmt (audfmt_e fmt, int endianness)
177 dolog ("Internal logic error: Bad audio format %d\n", fmt);
185 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
219 dolog ("Unrecognized audio format %d\n", ossfmt);
226 #if defined DEBUG_MISMATCHES || defined DEBUG
227 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
229 dolog ("parameter | requested value | obtained value\n");
230 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
231 dolog ("channels | %10d | %10d\n",
232 req->nchannels, obt->nchannels);
233 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
234 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
235 dolog ("fragsize | %10d | %10d\n",
236 req->fragsize, obt->fragsize);
240 #ifdef USE_DSP_POLICY
241 static int oss_get_version (int fd, int *version, const char *typ)
243 if (ioctl (fd, OSS_GETVERSION, &version)) {
244 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
246 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
247 * since 7.x, but currently only on the mixer device (or in
248 * the Linuxolator), and in the native version that part of
249 * the code is in fact never reached so the ioctl fails anyway.
250 * Until this is fixed, just check the errno and if its what
251 * FreeBSD's sound drivers return atm assume they are new enough.
253 if (errno == EINVAL) {
258 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
265 static int oss_open (int in, struct oss_params *req,
266 struct oss_params *obt, int *pfd, OSSConf* conf)
269 int oflags = conf->exclusive ? O_EXCL : 0;
270 audio_buf_info abinfo;
271 int fmt, freq, nchannels;
273 const char *dspname = in ? conf->devpath_in : conf->devpath_out;
274 const char *typ = in ? "ADC" : "DAC";
276 /* Kludge needed to have working mmap on Linux */
277 oflags |= conf->try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
279 fd = open (dspname, oflags | O_NONBLOCK);
281 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
286 nchannels = req->nchannels;
289 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
290 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
294 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
295 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
300 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
301 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
305 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
306 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
310 #ifdef USE_DSP_POLICY
311 if (conf->policy >= 0) {
314 if (!oss_get_version (fd, &version, typ)) {
315 trace_oss_version(version);
317 if (version >= 0x040000) {
318 int policy = conf->policy;
319 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
320 oss_logerr2 (errno, typ,
321 "Failed to set timing policy to %d\n",
332 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
333 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
334 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
335 req->nfrags, req->fragsize);
340 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
341 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
345 if (!abinfo.fragstotal || !abinfo.fragsize) {
346 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
347 abinfo.fragstotal, abinfo.fragsize, typ);
352 obt->nchannels = nchannels;
354 obt->nfrags = abinfo.fragstotal;
355 obt->fragsize = abinfo.fragsize;
358 #ifdef DEBUG_MISMATCHES
359 if ((req->fmt != obt->fmt) ||
360 (req->nchannels != obt->nchannels) ||
361 (req->freq != obt->freq) ||
362 (req->fragsize != obt->fragsize) ||
363 (req->nfrags != obt->nfrags)) {
364 dolog ("Audio parameters mismatch\n");
365 oss_dump_info (req, obt);
370 oss_dump_info (req, obt);
375 oss_anal_close (&fd);
379 static void oss_write_pending (OSSVoiceOut *oss)
381 HWVoiceOut *hw = &oss->hw;
387 while (oss->pending) {
389 ssize_t bytes_written;
390 int samples_till_end = hw->samples - oss->wpos;
391 int samples_to_write = audio_MIN (oss->pending, samples_till_end);
392 int bytes_to_write = samples_to_write << hw->info.shift;
393 void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
395 bytes_written = write (oss->fd, pcm, bytes_to_write);
396 if (bytes_written < 0) {
397 if (errno != EAGAIN) {
398 oss_logerr (errno, "failed to write %d bytes\n",
404 if (bytes_written & hw->info.align) {
405 dolog ("misaligned write asked for %d, but got %zd\n",
406 bytes_to_write, bytes_written);
410 samples_written = bytes_written >> hw->info.shift;
411 oss->pending -= samples_written;
412 oss->wpos = (oss->wpos + samples_written) % hw->samples;
413 if (bytes_written - bytes_to_write) {
419 static int oss_run_out (HWVoiceOut *hw, int live)
421 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
423 struct audio_buf_info abinfo;
424 struct count_info cntinfo;
427 bufsize = hw->samples << hw->info.shift;
432 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
434 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
438 pos = hw->rpos << hw->info.shift;
439 bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
440 decr = audio_MIN (bytes >> hw->info.shift, live);
443 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
445 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
449 if (abinfo.bytes > bufsize) {
450 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
451 abinfo.bytes = bufsize;
454 if (abinfo.bytes < 0) {
455 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
459 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
465 decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending);
466 oss->pending += decr;
467 oss_write_pending (oss);
472 static void oss_fini_out (HWVoiceOut *hw)
475 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
477 ldebug ("oss_fini\n");
478 oss_anal_close (&oss->fd);
482 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
484 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
485 oss->pcm_buf, hw->samples << hw->info.shift);
489 g_free (oss->pcm_buf);
495 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;
505 OSSConf *conf = drv_opaque;
509 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
511 req.nchannels = as->nchannels;
512 req.fragsize = conf->fragsize;
513 req.nfrags = conf->nfrags;
515 if (oss_open (0, &req, &obt, &fd, conf)) {
519 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
521 oss_anal_close (&fd);
525 obt_as.freq = obt.freq;
526 obt_as.nchannels = obt.nchannels;
527 obt_as.fmt = effective_fmt;
528 obt_as.endianness = endianness;
530 audio_pcm_init_info (&hw->info, &obt_as);
531 oss->nfrags = obt.nfrags;
532 oss->fragsize = obt.fragsize;
534 if (obt.nfrags * obt.fragsize & hw->info.align) {
535 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
536 obt.nfrags * obt.fragsize, hw->info.align + 1);
539 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
542 if (conf->try_mmap) {
543 oss->pcm_buf = mmap (
545 hw->samples << hw->info.shift,
546 PROT_READ | PROT_WRITE,
551 if (oss->pcm_buf == MAP_FAILED) {
552 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
553 hw->samples << hw->info.shift);
558 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
559 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
562 trig = PCM_ENABLE_OUTPUT;
563 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
566 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
575 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
577 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
578 oss->pcm_buf, hw->samples << hw->info.shift);
585 oss->pcm_buf = audio_calloc (
592 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
596 oss_anal_close (&fd);
606 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
609 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
618 poll_mode = va_arg (ap, int);
621 ldebug ("enabling voice\n");
626 hw->poll_mode = poll_mode;
632 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
633 trig = PCM_ENABLE_OUTPUT;
634 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
637 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
646 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
654 ldebug ("disabling voice\n");
656 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
657 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
665 static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
667 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
668 struct oss_params req, obt;
672 audfmt_e effective_fmt;
673 struct audsettings obt_as;
674 OSSConf *conf = drv_opaque;
678 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
680 req.nchannels = as->nchannels;
681 req.fragsize = conf->fragsize;
682 req.nfrags = conf->nfrags;
683 if (oss_open (1, &req, &obt, &fd, conf)) {
687 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
689 oss_anal_close (&fd);
693 obt_as.freq = obt.freq;
694 obt_as.nchannels = obt.nchannels;
695 obt_as.fmt = effective_fmt;
696 obt_as.endianness = endianness;
698 audio_pcm_init_info (&hw->info, &obt_as);
699 oss->nfrags = obt.nfrags;
700 oss->fragsize = obt.fragsize;
702 if (obt.nfrags * obt.fragsize & hw->info.align) {
703 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
704 obt.nfrags * obt.fragsize, hw->info.align + 1);
707 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
708 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
710 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
711 hw->samples, 1 << hw->info.shift);
712 oss_anal_close (&fd);
721 static void oss_fini_in (HWVoiceIn *hw)
723 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
725 oss_anal_close (&oss->fd);
727 g_free(oss->pcm_buf);
731 static int oss_run_in (HWVoiceIn *hw)
733 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
734 int hwshift = hw->info.shift;
736 int live = audio_pcm_hw_get_live_in (hw);
737 int dead = hw->samples - live;
738 size_t read_samples = 0;
743 { .add = hw->wpos, .len = 0 },
744 { .add = 0, .len = 0 }
751 if (hw->wpos + dead > hw->samples) {
752 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
753 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
756 bufs[0].len = dead << hwshift;
759 for (i = 0; i < 2; ++i) {
763 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
764 nread = read (oss->fd, p, bufs[i].len);
767 if (nread & hw->info.align) {
768 dolog ("warning: Misaligned read %zd (requested %d), "
769 "alignment %d\n", nread, bufs[i].add << hwshift,
772 read_samples += nread >> hwshift;
773 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift);
776 if (bufs[i].len - nread) {
785 "Failed to read %d bytes of audio (to %p)\n",
796 hw->wpos = (hw->wpos + read_samples) % hw->samples;
800 static int oss_read (SWVoiceIn *sw, void *buf, int size)
802 return audio_pcm_sw_read (sw, buf, size);
805 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
807 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
816 poll_mode = va_arg (ap, int);
823 hw->poll_mode = poll_mode;
830 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
837 static OSSConf glob_conf = {
841 .devpath_out = "/dev/dsp",
842 .devpath_in = "/dev/dsp",
847 static void *oss_audio_init (void)
849 OSSConf *conf = g_malloc(sizeof(OSSConf));
852 if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
853 access(conf->devpath_out, R_OK | W_OK) < 0) {
860 static void oss_audio_fini (void *opaque)
865 static struct audio_option oss_options[] = {
869 .valp = &glob_conf.fragsize,
870 .descr = "Fragment size in bytes"
875 .valp = &glob_conf.nfrags,
876 .descr = "Number of fragments"
881 .valp = &glob_conf.try_mmap,
882 .descr = "Try using memory mapped access"
887 .valp = &glob_conf.devpath_out,
888 .descr = "Path to DAC device"
893 .valp = &glob_conf.devpath_in,
894 .descr = "Path to ADC device"
899 .valp = &glob_conf.exclusive,
900 .descr = "Open device in exclusive mode (vmix won't work)"
902 #ifdef USE_DSP_POLICY
906 .valp = &glob_conf.policy,
907 .descr = "Set the timing policy of the device, -1 to use fragment mode",
910 { /* End of list */ }
913 static struct audio_pcm_ops oss_pcm_ops = {
914 .init_out = oss_init_out,
915 .fini_out = oss_fini_out,
916 .run_out = oss_run_out,
918 .ctl_out = oss_ctl_out,
920 .init_in = oss_init_in,
921 .fini_in = oss_fini_in,
922 .run_in = oss_run_in,
927 struct audio_driver oss_audio_driver = {
929 .descr = "OSS http://www.opensound.com",
930 .options = oss_options,
931 .init = oss_audio_init,
932 .fini = oss_audio_fini,
933 .pcm_ops = &oss_pcm_ops,
935 .max_voices_out = INT_MAX,
936 .max_voices_in = INT_MAX,
937 .voice_size_out = sizeof (OSSVoiceOut),
938 .voice_size_in = sizeof (OSSVoiceIn)