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;
71 .devpath_out = "/dev/dsp",
72 .devpath_in = "/dev/dsp",
84 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
89 AUD_vlog (AUDIO_CAP, fmt, ap);
92 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
95 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
104 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
107 AUD_vlog (AUDIO_CAP, fmt, ap);
110 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
113 static void oss_anal_close (int *fdp)
115 int err = close (*fdp);
117 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
119 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
123 static void oss_helper_poll_out (void *opaque)
126 audio_run ("oss_poll_out");
129 static void oss_helper_poll_in (void *opaque)
132 audio_run ("oss_poll_in");
135 static int oss_poll_out (HWVoiceOut *hw)
137 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
139 return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
142 static int oss_poll_in (HWVoiceIn *hw)
144 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
146 return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
149 static int oss_write (SWVoiceOut *sw, void *buf, int len)
151 return audio_pcm_sw_write (sw, buf, len);
154 static int aud_to_ossfmt (audfmt_e fmt)
170 dolog ("Internal logic error: Bad audio format %d\n", fmt);
178 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
212 dolog ("Unrecognized audio format %d\n", ossfmt);
219 #if defined DEBUG_MISMATCHES || defined DEBUG
220 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
222 dolog ("parameter | requested value | obtained value\n");
223 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
224 dolog ("channels | %10d | %10d\n",
225 req->nchannels, obt->nchannels);
226 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
227 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
228 dolog ("fragsize | %10d | %10d\n",
229 req->fragsize, obt->fragsize);
233 static int oss_open (int in, struct oss_params *req,
234 struct oss_params *obt, int *pfd)
239 audio_buf_info abinfo;
240 int fmt, freq, nchannels;
241 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
242 const char *typ = in ? "ADC" : "DAC";
244 /* Kludge needed to have working mmap on Linux */
245 oflags = conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
246 fd = open (dspname, oflags | O_NONBLOCK);
248 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
253 nchannels = req->nchannels;
256 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
257 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
261 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
262 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
267 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
268 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
272 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
273 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
277 mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
278 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
279 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
280 req->nfrags, req->fragsize);
284 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
285 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
289 if (!abinfo.fragstotal || !abinfo.fragsize) {
290 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
291 abinfo.fragstotal, abinfo.fragsize, typ);
296 obt->nchannels = nchannels;
298 obt->nfrags = abinfo.fragstotal;
299 obt->fragsize = abinfo.fragsize;
302 #ifdef DEBUG_MISMATCHES
303 if ((req->fmt != obt->fmt) ||
304 (req->nchannels != obt->nchannels) ||
305 (req->freq != obt->freq) ||
306 (req->fragsize != obt->fragsize) ||
307 (req->nfrags != obt->nfrags)) {
308 dolog ("Audio parameters mismatch\n");
309 oss_dump_info (req, obt);
314 oss_dump_info (req, obt);
319 oss_anal_close (&fd);
323 static int oss_run_out (HWVoiceOut *hw)
325 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
326 int err, rpos, live, decr;
329 struct st_sample *src;
330 struct audio_buf_info abinfo;
331 struct count_info cntinfo;
334 live = audio_pcm_hw_get_live_out (hw);
339 bufsize = hw->samples << hw->info.shift;
344 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
346 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
350 if (cntinfo.ptr == oss->old_optr) {
351 if (abs (hw->samples - live) < 64) {
352 dolog ("warning: Overrun\n");
357 if (cntinfo.ptr > oss->old_optr) {
358 bytes = cntinfo.ptr - oss->old_optr;
361 bytes = bufsize + cntinfo.ptr - oss->old_optr;
364 decr = audio_MIN (bytes >> hw->info.shift, live);
367 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
369 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
373 if (abinfo.bytes > bufsize) {
375 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
376 "please report your OS/audio hw to malc@pulsesoft.com\n",
377 abinfo.bytes, bufsize);
379 abinfo.bytes = bufsize;
382 if (abinfo.bytes < 0) {
384 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
385 abinfo.bytes, bufsize);
390 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
399 int left_till_end_samples = hw->samples - rpos;
400 int convert_samples = audio_MIN (samples, left_till_end_samples);
402 src = hw->mix_buf + rpos;
403 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
405 hw->clip (dst, src, convert_samples);
409 written = write (oss->fd, dst, convert_samples << hw->info.shift);
410 /* XXX: follow errno recommendations ? */
414 "Failed to write %d bytes of audio data from %p\n",
415 convert_samples << hw->info.shift,
421 if (written != convert_samples << hw->info.shift) {
422 int wsamples = written >> hw->info.shift;
423 int wbytes = wsamples << hw->info.shift;
424 if (wbytes != written) {
425 dolog ("warning: Misaligned write %d (requested %d), "
427 wbytes, written, hw->info.align + 1);
430 rpos = (rpos + wsamples) % hw->samples;
435 rpos = (rpos + convert_samples) % hw->samples;
436 samples -= convert_samples;
439 oss->old_optr = cntinfo.ptr;
446 static void oss_fini_out (HWVoiceOut *hw)
449 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
451 ldebug ("oss_fini\n");
452 oss_anal_close (&oss->fd);
456 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
458 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
459 oss->pcm_buf, hw->samples << hw->info.shift);
463 qemu_free (oss->pcm_buf);
469 static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
471 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
472 struct oss_params req, obt;
476 audfmt_e effective_fmt;
477 struct audsettings obt_as;
481 req.fmt = aud_to_ossfmt (as->fmt);
483 req.nchannels = as->nchannels;
484 req.fragsize = conf.fragsize;
485 req.nfrags = conf.nfrags;
487 if (oss_open (0, &req, &obt, &fd)) {
491 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
493 oss_anal_close (&fd);
497 obt_as.freq = obt.freq;
498 obt_as.nchannels = obt.nchannels;
499 obt_as.fmt = effective_fmt;
500 obt_as.endianness = endianness;
502 audio_pcm_init_info (&hw->info, &obt_as);
503 oss->nfrags = obt.nfrags;
504 oss->fragsize = obt.fragsize;
506 if (obt.nfrags * obt.fragsize & hw->info.align) {
507 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
508 obt.nfrags * obt.fragsize, hw->info.align + 1);
511 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
515 oss->pcm_buf = mmap (
517 hw->samples << hw->info.shift,
518 PROT_READ | PROT_WRITE,
523 if (oss->pcm_buf == MAP_FAILED) {
524 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
525 hw->samples << hw->info.shift);
529 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
530 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
533 trig = PCM_ENABLE_OUTPUT;
534 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
537 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
546 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
548 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
549 oss->pcm_buf, hw->samples << hw->info.shift);
556 oss->pcm_buf = audio_calloc (
563 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
567 oss_anal_close (&fd);
576 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
581 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
584 poll_mode = va_arg (ap, int);
589 ldebug ("enabling voice\n");
590 if (poll_mode && oss_poll_out (hw)) {
593 hw->poll_mode = poll_mode;
599 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
600 trig = PCM_ENABLE_OUTPUT;
601 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
604 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
612 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
620 ldebug ("disabling voice\n");
622 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
623 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
631 static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
633 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
634 struct oss_params req, obt;
638 audfmt_e effective_fmt;
639 struct audsettings obt_as;
643 req.fmt = aud_to_ossfmt (as->fmt);
645 req.nchannels = as->nchannels;
646 req.fragsize = conf.fragsize;
647 req.nfrags = conf.nfrags;
648 if (oss_open (1, &req, &obt, &fd)) {
652 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
654 oss_anal_close (&fd);
658 obt_as.freq = obt.freq;
659 obt_as.nchannels = obt.nchannels;
660 obt_as.fmt = effective_fmt;
661 obt_as.endianness = endianness;
663 audio_pcm_init_info (&hw->info, &obt_as);
664 oss->nfrags = obt.nfrags;
665 oss->fragsize = obt.fragsize;
667 if (obt.nfrags * obt.fragsize & hw->info.align) {
668 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
669 obt.nfrags * obt.fragsize, hw->info.align + 1);
672 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
673 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
675 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
676 hw->samples, 1 << hw->info.shift);
677 oss_anal_close (&fd);
685 static void oss_fini_in (HWVoiceIn *hw)
687 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
689 oss_anal_close (&oss->fd);
692 qemu_free (oss->pcm_buf);
697 static int oss_run_in (HWVoiceIn *hw)
699 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
700 int hwshift = hw->info.shift;
702 int live = audio_pcm_hw_get_live_in (hw);
703 int dead = hw->samples - live;
704 size_t read_samples = 0;
709 { .add = hw->wpos, .len = 0 },
710 { .add = 0, .len = 0 }
717 if (hw->wpos + dead > hw->samples) {
718 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
719 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
722 bufs[0].len = dead << hwshift;
726 for (i = 0; i < 2; ++i) {
730 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
731 nread = read (oss->fd, p, bufs[i].len);
734 if (nread & hw->info.align) {
735 dolog ("warning: Misaligned read %zd (requested %d), "
736 "alignment %d\n", nread, bufs[i].add << hwshift,
739 read_samples += nread >> hwshift;
740 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
744 if (bufs[i].len - nread) {
753 "Failed to read %d bytes of audio (to %p)\n",
764 hw->wpos = (hw->wpos + read_samples) % hw->samples;
768 static int oss_read (SWVoiceIn *sw, void *buf, int size)
770 return audio_pcm_sw_read (sw, buf, size);
773 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
777 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
780 poll_mode = va_arg (ap, int);
785 if (poll_mode && oss_poll_in (hw)) {
788 hw->poll_mode = poll_mode;
794 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
801 static void *oss_audio_init (void)
806 static void oss_audio_fini (void *opaque)
811 static struct audio_option oss_options[] = {
815 .valp = &conf.fragsize,
816 .descr = "Fragment size in bytes"
821 .valp = &conf.nfrags,
822 .descr = "Number of fragments"
827 .valp = &conf.try_mmap,
828 .descr = "Try using memory mapped access"
833 .valp = &conf.devpath_out,
834 .descr = "Path to DAC device"
839 .valp = &conf.devpath_in,
840 .descr = "Path to ADC device"
846 .descr = "Turn on some debugging messages"
848 { /* End of list */ }
851 static struct audio_pcm_ops oss_pcm_ops = {
852 .init_out = oss_init_out,
853 .fini_out = oss_fini_out,
854 .run_out = oss_run_out,
856 .ctl_out = oss_ctl_out,
858 .init_in = oss_init_in,
859 .fini_in = oss_fini_in,
860 .run_in = oss_run_in,
865 struct audio_driver oss_audio_driver = {
867 .descr = "OSS http://www.opensound.com",
868 .options = oss_options,
869 .init = oss_audio_init,
870 .fini = oss_audio_fini,
871 .pcm_ops = &oss_pcm_ops,
873 .max_voices_out = INT_MAX,
874 .max_voices_in = INT_MAX,
875 .voice_size_out = sizeof (OSSVoiceOut),
876 .voice_size_in = sizeof (OSSVoiceIn)