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 {
50 typedef struct OSSVoiceIn {
62 const char *devpath_out;
63 const char *devpath_in;
71 .devpath_out = "/dev/dsp",
72 .devpath_in = "/dev/dsp",
86 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
91 AUD_vlog (AUDIO_CAP, fmt, ap);
94 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
97 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
106 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
109 AUD_vlog (AUDIO_CAP, fmt, ap);
112 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
115 static void oss_anal_close (int *fdp)
117 int err = close (*fdp);
119 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
121 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
125 static void oss_helper_poll_out (void *opaque)
128 audio_run ("oss_poll_out");
131 static void oss_helper_poll_in (void *opaque)
134 audio_run ("oss_poll_in");
137 static int oss_poll_out (HWVoiceOut *hw)
139 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
141 return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
144 static int oss_poll_in (HWVoiceIn *hw)
146 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
148 return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
151 static int oss_write (SWVoiceOut *sw, void *buf, int len)
153 return audio_pcm_sw_write (sw, buf, len);
156 static int aud_to_ossfmt (audfmt_e fmt)
172 dolog ("Internal logic error: Bad audio format %d\n", fmt);
180 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
214 dolog ("Unrecognized audio format %d\n", ossfmt);
221 #if defined DEBUG_MISMATCHES || defined DEBUG
222 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
224 dolog ("parameter | requested value | obtained value\n");
225 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
226 dolog ("channels | %10d | %10d\n",
227 req->nchannels, obt->nchannels);
228 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
229 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
230 dolog ("fragsize | %10d | %10d\n",
231 req->fragsize, obt->fragsize);
235 static int oss_open (int in, struct oss_params *req,
236 struct oss_params *obt, int *pfd)
240 int oflags = conf.exclusive ? O_EXCL : 0;
241 audio_buf_info abinfo;
242 int fmt, freq, nchannels;
243 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
244 const char *typ = in ? "ADC" : "DAC";
246 /* Kludge needed to have working mmap on Linux */
247 oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
249 fd = open (dspname, oflags | O_NONBLOCK);
251 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
256 nchannels = req->nchannels;
259 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
260 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
264 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
265 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
270 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
271 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
275 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
276 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
280 if (ioctl (fd, OSS_GETVERSION, &version)) {
281 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
286 dolog ("OSS version = %#x\n", version);
289 #ifdef SNDCTL_DSP_POLICY
290 if (conf.policy >= 0 && version >= 0x040000) {
291 int policy = conf.policy;
292 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
293 oss_logerr2 (errno, typ, "Failed to set timing policy to %d\n",
301 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
302 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
303 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
304 req->nfrags, req->fragsize);
309 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
310 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
314 if (!abinfo.fragstotal || !abinfo.fragsize) {
315 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
316 abinfo.fragstotal, abinfo.fragsize, typ);
321 obt->nchannels = nchannels;
323 obt->nfrags = abinfo.fragstotal;
324 obt->fragsize = abinfo.fragsize;
327 #ifdef DEBUG_MISMATCHES
328 if ((req->fmt != obt->fmt) ||
329 (req->nchannels != obt->nchannels) ||
330 (req->freq != obt->freq) ||
331 (req->fragsize != obt->fragsize) ||
332 (req->nfrags != obt->nfrags)) {
333 dolog ("Audio parameters mismatch\n");
334 oss_dump_info (req, obt);
339 oss_dump_info (req, obt);
344 oss_anal_close (&fd);
348 static int oss_run_out (HWVoiceOut *hw)
350 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
351 int err, rpos, live, decr;
354 struct st_sample *src;
355 struct audio_buf_info abinfo;
356 struct count_info cntinfo;
359 live = audio_pcm_hw_get_live_out (hw);
364 bufsize = hw->samples << hw->info.shift;
369 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
371 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
375 pos = hw->rpos << hw->info.shift;
376 bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
377 decr = audio_MIN (bytes >> hw->info.shift, live);
380 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
382 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
386 if (abinfo.bytes > bufsize) {
388 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
389 "please report your OS/audio hw to malc@pulsesoft.com\n",
390 abinfo.bytes, bufsize);
392 abinfo.bytes = bufsize;
395 if (abinfo.bytes < 0) {
397 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
398 abinfo.bytes, bufsize);
403 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
412 int left_till_end_samples = hw->samples - rpos;
413 int convert_samples = audio_MIN (samples, left_till_end_samples);
415 src = hw->mix_buf + rpos;
416 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
418 hw->clip (dst, src, convert_samples);
422 written = write (oss->fd, dst, convert_samples << hw->info.shift);
423 /* XXX: follow errno recommendations ? */
427 "Failed to write %d bytes of audio data from %p\n",
428 convert_samples << hw->info.shift,
434 if (written != convert_samples << hw->info.shift) {
435 int wsamples = written >> hw->info.shift;
436 int wbytes = wsamples << hw->info.shift;
437 if (wbytes != written) {
438 dolog ("warning: Misaligned write %d (requested %d), "
440 wbytes, written, hw->info.align + 1);
443 rpos = (rpos + wsamples) % hw->samples;
448 rpos = (rpos + convert_samples) % hw->samples;
449 samples -= convert_samples;
456 static void oss_fini_out (HWVoiceOut *hw)
459 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
461 ldebug ("oss_fini\n");
462 oss_anal_close (&oss->fd);
466 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
468 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
469 oss->pcm_buf, hw->samples << hw->info.shift);
473 qemu_free (oss->pcm_buf);
479 static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
481 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
482 struct oss_params req, obt;
486 audfmt_e effective_fmt;
487 struct audsettings obt_as;
491 req.fmt = aud_to_ossfmt (as->fmt);
493 req.nchannels = as->nchannels;
494 req.fragsize = conf.fragsize;
495 req.nfrags = conf.nfrags;
497 if (oss_open (0, &req, &obt, &fd)) {
501 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
503 oss_anal_close (&fd);
507 obt_as.freq = obt.freq;
508 obt_as.nchannels = obt.nchannels;
509 obt_as.fmt = effective_fmt;
510 obt_as.endianness = endianness;
512 audio_pcm_init_info (&hw->info, &obt_as);
513 oss->nfrags = obt.nfrags;
514 oss->fragsize = obt.fragsize;
516 if (obt.nfrags * obt.fragsize & hw->info.align) {
517 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
518 obt.nfrags * obt.fragsize, hw->info.align + 1);
521 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
525 oss->pcm_buf = mmap (
527 hw->samples << hw->info.shift,
528 PROT_READ | PROT_WRITE,
533 if (oss->pcm_buf == MAP_FAILED) {
534 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
535 hw->samples << hw->info.shift);
540 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
541 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
544 trig = PCM_ENABLE_OUTPUT;
545 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
548 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
557 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
559 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
560 oss->pcm_buf, hw->samples << hw->info.shift);
567 oss->pcm_buf = audio_calloc (
574 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
578 oss_anal_close (&fd);
587 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
592 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
595 poll_mode = va_arg (ap, int);
600 ldebug ("enabling voice\n");
601 if (poll_mode && oss_poll_out (hw)) {
604 hw->poll_mode = poll_mode;
610 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
611 trig = PCM_ENABLE_OUTPUT;
612 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
615 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
623 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
631 ldebug ("disabling voice\n");
633 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
634 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
642 static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
644 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
645 struct oss_params req, obt;
649 audfmt_e effective_fmt;
650 struct audsettings obt_as;
654 req.fmt = aud_to_ossfmt (as->fmt);
656 req.nchannels = as->nchannels;
657 req.fragsize = conf.fragsize;
658 req.nfrags = conf.nfrags;
659 if (oss_open (1, &req, &obt, &fd)) {
663 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
665 oss_anal_close (&fd);
669 obt_as.freq = obt.freq;
670 obt_as.nchannels = obt.nchannels;
671 obt_as.fmt = effective_fmt;
672 obt_as.endianness = endianness;
674 audio_pcm_init_info (&hw->info, &obt_as);
675 oss->nfrags = obt.nfrags;
676 oss->fragsize = obt.fragsize;
678 if (obt.nfrags * obt.fragsize & hw->info.align) {
679 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
680 obt.nfrags * obt.fragsize, hw->info.align + 1);
683 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
684 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
686 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
687 hw->samples, 1 << hw->info.shift);
688 oss_anal_close (&fd);
696 static void oss_fini_in (HWVoiceIn *hw)
698 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
700 oss_anal_close (&oss->fd);
703 qemu_free (oss->pcm_buf);
708 static int oss_run_in (HWVoiceIn *hw)
710 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
711 int hwshift = hw->info.shift;
713 int live = audio_pcm_hw_get_live_in (hw);
714 int dead = hw->samples - live;
715 size_t read_samples = 0;
720 { .add = hw->wpos, .len = 0 },
721 { .add = 0, .len = 0 }
728 if (hw->wpos + dead > hw->samples) {
729 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
730 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
733 bufs[0].len = dead << hwshift;
736 for (i = 0; i < 2; ++i) {
740 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
741 nread = read (oss->fd, p, bufs[i].len);
744 if (nread & hw->info.align) {
745 dolog ("warning: Misaligned read %zd (requested %d), "
746 "alignment %d\n", nread, bufs[i].add << hwshift,
749 read_samples += nread >> hwshift;
750 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
754 if (bufs[i].len - nread) {
763 "Failed to read %d bytes of audio (to %p)\n",
774 hw->wpos = (hw->wpos + read_samples) % hw->samples;
778 static int oss_read (SWVoiceIn *sw, void *buf, int size)
780 return audio_pcm_sw_read (sw, buf, size);
783 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
787 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
790 poll_mode = va_arg (ap, int);
795 if (poll_mode && oss_poll_in (hw)) {
798 hw->poll_mode = poll_mode;
804 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
811 static void *oss_audio_init (void)
816 static void oss_audio_fini (void *opaque)
821 static struct audio_option oss_options[] = {
825 .valp = &conf.fragsize,
826 .descr = "Fragment size in bytes"
831 .valp = &conf.nfrags,
832 .descr = "Number of fragments"
837 .valp = &conf.try_mmap,
838 .descr = "Try using memory mapped access"
843 .valp = &conf.devpath_out,
844 .descr = "Path to DAC device"
849 .valp = &conf.devpath_in,
850 .descr = "Path to ADC device"
855 .valp = &conf.exclusive,
856 .descr = "Open device in exclusive mode (vmix wont work)"
858 #ifdef SNDCTL_DSP_POLICY
862 .valp = &conf.policy,
863 .descr = "Set the timing policy of the device, -1 to use fragment mode",
870 .descr = "Turn on some debugging messages"
872 { /* End of list */ }
875 static struct audio_pcm_ops oss_pcm_ops = {
876 .init_out = oss_init_out,
877 .fini_out = oss_fini_out,
878 .run_out = oss_run_out,
880 .ctl_out = oss_ctl_out,
882 .init_in = oss_init_in,
883 .fini_in = oss_fini_in,
884 .run_in = oss_run_in,
889 struct audio_driver oss_audio_driver = {
891 .descr = "OSS http://www.opensound.com",
892 .options = oss_options,
893 .init = oss_audio_init,
894 .fini = oss_audio_fini,
895 .pcm_ops = &oss_pcm_ops,
897 .max_voices_out = INT_MAX,
898 .max_voices_in = INT_MAX,
899 .voice_size_out = sizeof (OSSVoiceOut),
900 .voice_size_in = sizeof (OSSVoiceIn)