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>
28 #include <sys/soundcard.h>
29 #include "qemu-common.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/host-utils.h"
34 #define AUDIO_CAP "oss"
35 #include "audio_int.h"
37 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
38 #define USE_DSP_POLICY
41 typedef struct OSSVoiceOut {
52 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)
121 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
124 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
129 static void oss_helper_poll_out (void *opaque)
132 audio_run ("oss_poll_out");
135 static void oss_helper_poll_in (void *opaque)
138 audio_run ("oss_poll_in");
141 static void oss_poll_out (HWVoiceOut *hw)
143 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
145 qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
148 static void oss_poll_in (HWVoiceIn *hw)
150 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
152 qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
155 static int oss_write (SWVoiceOut *sw, void *buf, int len)
157 return audio_pcm_sw_write (sw, buf, len);
160 static int aud_to_ossfmt (audfmt_e fmt, int endianness)
186 dolog ("Internal logic error: Bad audio format %d\n", fmt);
194 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
228 dolog ("Unrecognized audio format %d\n", ossfmt);
235 #if defined DEBUG_MISMATCHES || defined DEBUG
236 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
238 dolog ("parameter | requested value | obtained value\n");
239 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
240 dolog ("channels | %10d | %10d\n",
241 req->nchannels, obt->nchannels);
242 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
243 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
244 dolog ("fragsize | %10d | %10d\n",
245 req->fragsize, obt->fragsize);
249 #ifdef USE_DSP_POLICY
250 static int oss_get_version (int fd, int *version, const char *typ)
252 if (ioctl (fd, OSS_GETVERSION, &version)) {
253 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
255 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
256 * since 7.x, but currently only on the mixer device (or in
257 * the Linuxolator), and in the native version that part of
258 * the code is in fact never reached so the ioctl fails anyway.
259 * Until this is fixed, just check the errno and if its what
260 * FreeBSD's sound drivers return atm assume they are new enough.
262 if (errno == EINVAL) {
267 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
274 static int oss_open (int in, struct oss_params *req,
275 struct oss_params *obt, int *pfd)
278 int oflags = conf.exclusive ? O_EXCL : 0;
279 audio_buf_info abinfo;
280 int fmt, freq, nchannels;
282 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
283 const char *typ = in ? "ADC" : "DAC";
285 /* Kludge needed to have working mmap on Linux */
286 oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
288 fd = open (dspname, oflags | O_NONBLOCK);
290 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
295 nchannels = req->nchannels;
298 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
299 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
303 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
304 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
309 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
310 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
314 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
315 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
319 #ifdef USE_DSP_POLICY
320 if (conf.policy >= 0) {
323 if (!oss_get_version (fd, &version, typ)) {
325 dolog ("OSS version = %#x\n", version);
328 if (version >= 0x040000) {
329 int policy = conf.policy;
330 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
331 oss_logerr2 (errno, typ,
332 "Failed to set timing policy to %d\n",
343 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
344 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
345 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
346 req->nfrags, req->fragsize);
351 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
352 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
356 if (!abinfo.fragstotal || !abinfo.fragsize) {
357 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
358 abinfo.fragstotal, abinfo.fragsize, typ);
363 obt->nchannels = nchannels;
365 obt->nfrags = abinfo.fragstotal;
366 obt->fragsize = abinfo.fragsize;
369 #ifdef DEBUG_MISMATCHES
370 if ((req->fmt != obt->fmt) ||
371 (req->nchannels != obt->nchannels) ||
372 (req->freq != obt->freq) ||
373 (req->fragsize != obt->fragsize) ||
374 (req->nfrags != obt->nfrags)) {
375 dolog ("Audio parameters mismatch\n");
376 oss_dump_info (req, obt);
381 oss_dump_info (req, obt);
386 oss_anal_close (&fd);
390 static void oss_write_pending (OSSVoiceOut *oss)
392 HWVoiceOut *hw = &oss->hw;
398 while (oss->pending) {
400 ssize_t bytes_written;
401 int samples_till_end = hw->samples - oss->wpos;
402 int samples_to_write = audio_MIN (oss->pending, samples_till_end);
403 int bytes_to_write = samples_to_write << hw->info.shift;
404 void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
406 bytes_written = write (oss->fd, pcm, bytes_to_write);
407 if (bytes_written < 0) {
408 if (errno != EAGAIN) {
409 oss_logerr (errno, "failed to write %d bytes\n",
415 if (bytes_written & hw->info.align) {
416 dolog ("misaligned write asked for %d, but got %zd\n",
417 bytes_to_write, bytes_written);
421 samples_written = bytes_written >> hw->info.shift;
422 oss->pending -= samples_written;
423 oss->wpos = (oss->wpos + samples_written) % hw->samples;
424 if (bytes_written - bytes_to_write) {
430 static int oss_run_out (HWVoiceOut *hw, int live)
432 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
434 struct audio_buf_info abinfo;
435 struct count_info cntinfo;
438 bufsize = hw->samples << hw->info.shift;
443 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
445 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
449 pos = hw->rpos << hw->info.shift;
450 bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
451 decr = audio_MIN (bytes >> hw->info.shift, live);
454 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
456 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
460 if (abinfo.bytes > bufsize) {
462 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
463 "please report your OS/audio hw to av1474@comtv.ru\n",
464 abinfo.bytes, bufsize);
466 abinfo.bytes = bufsize;
469 if (abinfo.bytes < 0) {
471 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
472 abinfo.bytes, bufsize);
477 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
483 decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending);
484 oss->pending += decr;
485 oss_write_pending (oss);
490 static void oss_fini_out (HWVoiceOut *hw)
493 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
495 ldebug ("oss_fini\n");
496 oss_anal_close (&oss->fd);
500 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
502 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
503 oss->pcm_buf, hw->samples << hw->info.shift);
507 g_free (oss->pcm_buf);
513 static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
516 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
517 struct oss_params req, obt;
521 audfmt_e effective_fmt;
522 struct audsettings obt_as;
526 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
528 req.nchannels = as->nchannels;
529 req.fragsize = conf.fragsize;
530 req.nfrags = conf.nfrags;
532 if (oss_open (0, &req, &obt, &fd)) {
536 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
538 oss_anal_close (&fd);
542 obt_as.freq = obt.freq;
543 obt_as.nchannels = obt.nchannels;
544 obt_as.fmt = effective_fmt;
545 obt_as.endianness = endianness;
547 audio_pcm_init_info (&hw->info, &obt_as);
548 oss->nfrags = obt.nfrags;
549 oss->fragsize = obt.fragsize;
551 if (obt.nfrags * obt.fragsize & hw->info.align) {
552 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
553 obt.nfrags * obt.fragsize, hw->info.align + 1);
556 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
560 oss->pcm_buf = mmap (
562 hw->samples << hw->info.shift,
563 PROT_READ | PROT_WRITE,
568 if (oss->pcm_buf == MAP_FAILED) {
569 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
570 hw->samples << hw->info.shift);
575 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
576 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
579 trig = PCM_ENABLE_OUTPUT;
580 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
583 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
592 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
594 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
595 oss->pcm_buf, hw->samples << hw->info.shift);
602 oss->pcm_buf = audio_calloc (
609 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
613 oss_anal_close (&fd);
622 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
625 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
634 poll_mode = va_arg (ap, int);
637 ldebug ("enabling voice\n");
642 hw->poll_mode = poll_mode;
648 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
649 trig = PCM_ENABLE_OUTPUT;
650 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
653 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
662 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
670 ldebug ("disabling voice\n");
672 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
673 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
681 static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
683 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
684 struct oss_params req, obt;
688 audfmt_e effective_fmt;
689 struct audsettings obt_as;
693 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
695 req.nchannels = as->nchannels;
696 req.fragsize = conf.fragsize;
697 req.nfrags = conf.nfrags;
698 if (oss_open (1, &req, &obt, &fd)) {
702 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
704 oss_anal_close (&fd);
708 obt_as.freq = obt.freq;
709 obt_as.nchannels = obt.nchannels;
710 obt_as.fmt = effective_fmt;
711 obt_as.endianness = endianness;
713 audio_pcm_init_info (&hw->info, &obt_as);
714 oss->nfrags = obt.nfrags;
715 oss->fragsize = obt.fragsize;
717 if (obt.nfrags * obt.fragsize & hw->info.align) {
718 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
719 obt.nfrags * obt.fragsize, hw->info.align + 1);
722 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
723 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
725 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
726 hw->samples, 1 << hw->info.shift);
727 oss_anal_close (&fd);
735 static void oss_fini_in (HWVoiceIn *hw)
737 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
739 oss_anal_close (&oss->fd);
741 g_free(oss->pcm_buf);
745 static int oss_run_in (HWVoiceIn *hw)
747 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
748 int hwshift = hw->info.shift;
750 int live = audio_pcm_hw_get_live_in (hw);
751 int dead = hw->samples - live;
752 size_t read_samples = 0;
757 { .add = hw->wpos, .len = 0 },
758 { .add = 0, .len = 0 }
765 if (hw->wpos + dead > hw->samples) {
766 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
767 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
770 bufs[0].len = dead << hwshift;
773 for (i = 0; i < 2; ++i) {
777 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
778 nread = read (oss->fd, p, bufs[i].len);
781 if (nread & hw->info.align) {
782 dolog ("warning: Misaligned read %zd (requested %d), "
783 "alignment %d\n", nread, bufs[i].add << hwshift,
786 read_samples += nread >> hwshift;
787 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift);
790 if (bufs[i].len - nread) {
799 "Failed to read %d bytes of audio (to %p)\n",
810 hw->wpos = (hw->wpos + read_samples) % hw->samples;
814 static int oss_read (SWVoiceIn *sw, void *buf, int size)
816 return audio_pcm_sw_read (sw, buf, size);
819 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
821 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
830 poll_mode = va_arg (ap, int);
837 hw->poll_mode = poll_mode;
844 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
851 static void *oss_audio_init (void)
853 if (access(conf.devpath_in, R_OK | W_OK) < 0 ||
854 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 = &conf.fragsize,
870 .descr = "Fragment size in bytes"
875 .valp = &conf.nfrags,
876 .descr = "Number of fragments"
881 .valp = &conf.try_mmap,
882 .descr = "Try using memory mapped access"
887 .valp = &conf.devpath_out,
888 .descr = "Path to DAC device"
893 .valp = &conf.devpath_in,
894 .descr = "Path to ADC device"
899 .valp = &conf.exclusive,
900 .descr = "Open device in exclusive mode (vmix wont work)"
902 #ifdef USE_DSP_POLICY
906 .valp = &conf.policy,
907 .descr = "Set the timing policy of the device, -1 to use fragment mode",
914 .descr = "Turn on some debugging messages"
916 { /* End of list */ }
919 static struct audio_pcm_ops oss_pcm_ops = {
920 .init_out = oss_init_out,
921 .fini_out = oss_fini_out,
922 .run_out = oss_run_out,
924 .ctl_out = oss_ctl_out,
926 .init_in = oss_init_in,
927 .fini_in = oss_fini_in,
928 .run_in = oss_run_in,
933 struct audio_driver oss_audio_driver = {
935 .descr = "OSS http://www.opensound.com",
936 .options = oss_options,
937 .init = oss_audio_init,
938 .fini = oss_audio_fini,
939 .pcm_ops = &oss_pcm_ops,
941 .max_voices_out = INT_MAX,
942 .max_voices_in = INT_MAX,
943 .voice_size_out = sizeof (OSSVoiceOut),
944 .voice_size_in = sizeof (OSSVoiceIn)