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
#if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
42
#define USE_DSP_POLICY
45
typedef struct OSSVoiceOut {
56
typedef struct OSSVoiceIn {
68
const char *devpath_out;
69
const char *devpath_in;
77
.devpath_out = "/dev/dsp",
78
.devpath_in = "/dev/dsp",
92
static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
97
AUD_vlog (AUDIO_CAP, fmt, ap);
100
AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
103
static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
112
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
115
AUD_vlog (AUDIO_CAP, fmt, ap);
118
AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
121
static void oss_anal_close (int *fdp)
125
qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
128
oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
133
static void oss_helper_poll_out (void *opaque)
136
audio_run ("oss_poll_out");
139
static void oss_helper_poll_in (void *opaque)
142
audio_run ("oss_poll_in");
145
static int oss_poll_out (HWVoiceOut *hw)
147
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
149
return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
152
static int oss_poll_in (HWVoiceIn *hw)
154
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
156
return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
159
static int oss_write (SWVoiceOut *sw, void *buf, int len)
161
return audio_pcm_sw_write (sw, buf, len);
164
static int aud_to_ossfmt (audfmt_e fmt, int endianness)
190
dolog ("Internal logic error: Bad audio format %d\n", fmt);
198
static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
232
dolog ("Unrecognized audio format %d\n", ossfmt);
239
#if defined DEBUG_MISMATCHES || defined DEBUG
240
static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
242
dolog ("parameter | requested value | obtained value\n");
243
dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
244
dolog ("channels | %10d | %10d\n",
245
req->nchannels, obt->nchannels);
246
dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
247
dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
248
dolog ("fragsize | %10d | %10d\n",
249
req->fragsize, obt->fragsize);
253
#ifdef USE_DSP_POLICY
254
static int oss_get_version (int fd, int *version, const char *typ)
256
if (ioctl (fd, OSS_GETVERSION, &version)) {
257
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
259
* Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
260
* since 7.x, but currently only on the mixer device (or in
261
* the Linuxolator), and in the native version that part of
262
* the code is in fact never reached so the ioctl fails anyway.
263
* Until this is fixed, just check the errno and if its what
264
* FreeBSD's sound drivers return atm assume they are new enough.
266
if (errno == EINVAL) {
271
oss_logerr2 (errno, typ, "Failed to get OSS version\n");
278
static int oss_open (int in, struct oss_params *req,
279
struct oss_params *obt, int *pfd)
282
int oflags = conf.exclusive ? O_EXCL : 0;
283
audio_buf_info abinfo;
284
int fmt, freq, nchannels;
286
const char *dspname = in ? conf.devpath_in : conf.devpath_out;
287
const char *typ = in ? "ADC" : "DAC";
289
/* Kludge needed to have working mmap on Linux */
290
oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
292
fd = open (dspname, oflags | O_NONBLOCK);
294
oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
299
nchannels = req->nchannels;
302
if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
303
oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
307
if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
308
oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
313
if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
314
oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
318
if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
319
oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
323
#ifdef USE_DSP_POLICY
324
if (conf.policy >= 0) {
327
if (!oss_get_version (fd, &version, typ)) {
329
dolog ("OSS version = %#x\n", version);
332
if (version >= 0x040000) {
333
int policy = conf.policy;
334
if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
335
oss_logerr2 (errno, typ,
336
"Failed to set timing policy to %d\n",
347
int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
348
if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
349
oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
350
req->nfrags, req->fragsize);
355
if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
356
oss_logerr2 (errno, typ, "Failed to get buffer length\n");
360
if (!abinfo.fragstotal || !abinfo.fragsize) {
361
AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
362
abinfo.fragstotal, abinfo.fragsize, typ);
367
obt->nchannels = nchannels;
369
obt->nfrags = abinfo.fragstotal;
370
obt->fragsize = abinfo.fragsize;
373
#ifdef DEBUG_MISMATCHES
374
if ((req->fmt != obt->fmt) ||
375
(req->nchannels != obt->nchannels) ||
376
(req->freq != obt->freq) ||
377
(req->fragsize != obt->fragsize) ||
378
(req->nfrags != obt->nfrags)) {
379
dolog ("Audio parameters mismatch\n");
380
oss_dump_info (req, obt);
385
oss_dump_info (req, obt);
390
oss_anal_close (&fd);
394
static void oss_write_pending (OSSVoiceOut *oss)
396
HWVoiceOut *hw = &oss->hw;
402
while (oss->pending) {
404
ssize_t bytes_written;
405
int samples_till_end = hw->samples - oss->wpos;
406
int samples_to_write = audio_MIN (oss->pending, samples_till_end);
407
int bytes_to_write = samples_to_write << hw->info.shift;
408
void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
410
bytes_written = write (oss->fd, pcm, bytes_to_write);
411
if (bytes_written < 0) {
412
if (errno != EAGAIN) {
413
oss_logerr (errno, "failed to write %d bytes\n",
419
if (bytes_written & hw->info.align) {
420
dolog ("misaligned write asked for %d, but got %zd\n",
421
bytes_to_write, bytes_written);
425
samples_written = bytes_written >> hw->info.shift;
426
oss->pending -= samples_written;
427
oss->wpos = (oss->wpos + samples_written) % hw->samples;
428
if (bytes_written - bytes_to_write) {
434
static int oss_run_out (HWVoiceOut *hw, int live)
436
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
438
struct audio_buf_info abinfo;
439
struct count_info cntinfo;
442
bufsize = hw->samples << hw->info.shift;
447
err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
449
oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
453
pos = hw->rpos << hw->info.shift;
454
bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
455
decr = audio_MIN (bytes >> hw->info.shift, live);
458
err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
460
oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
464
if (abinfo.bytes > bufsize) {
466
dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
467
"please report your OS/audio hw to av1474@comtv.ru\n",
468
abinfo.bytes, bufsize);
470
abinfo.bytes = bufsize;
473
if (abinfo.bytes < 0) {
475
dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
476
abinfo.bytes, bufsize);
481
decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
487
decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending);
488
oss->pending += decr;
489
oss_write_pending (oss);
494
static void oss_fini_out (HWVoiceOut *hw)
497
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
499
ldebug ("oss_fini\n");
500
oss_anal_close (&oss->fd);
504
err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
506
oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
507
oss->pcm_buf, hw->samples << hw->info.shift);
511
g_free (oss->pcm_buf);
517
static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
519
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
520
struct oss_params req, obt;
524
audfmt_e effective_fmt;
525
struct audsettings obt_as;
529
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
531
req.nchannels = as->nchannels;
532
req.fragsize = conf.fragsize;
533
req.nfrags = conf.nfrags;
535
if (oss_open (0, &req, &obt, &fd)) {
539
err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
541
oss_anal_close (&fd);
545
obt_as.freq = obt.freq;
546
obt_as.nchannels = obt.nchannels;
547
obt_as.fmt = effective_fmt;
548
obt_as.endianness = endianness;
550
audio_pcm_init_info (&hw->info, &obt_as);
551
oss->nfrags = obt.nfrags;
552
oss->fragsize = obt.fragsize;
554
if (obt.nfrags * obt.fragsize & hw->info.align) {
555
dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
556
obt.nfrags * obt.fragsize, hw->info.align + 1);
559
hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
563
oss->pcm_buf = mmap (
565
hw->samples << hw->info.shift,
566
PROT_READ | PROT_WRITE,
571
if (oss->pcm_buf == MAP_FAILED) {
572
oss_logerr (errno, "Failed to map %d bytes of DAC\n",
573
hw->samples << hw->info.shift);
578
if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
579
oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
582
trig = PCM_ENABLE_OUTPUT;
583
if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
586
"SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
595
err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
597
oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
598
oss->pcm_buf, hw->samples << hw->info.shift);
605
oss->pcm_buf = audio_calloc (
612
"Could not allocate DAC buffer (%d samples, each %d bytes)\n",
616
oss_anal_close (&fd);
625
static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
628
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
637
poll_mode = va_arg (ap, int);
640
ldebug ("enabling voice\n");
641
if (poll_mode && oss_poll_out (hw)) {
644
hw->poll_mode = poll_mode;
650
audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
651
trig = PCM_ENABLE_OUTPUT;
652
if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
655
"SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
664
qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
672
ldebug ("disabling voice\n");
674
if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
675
oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
683
static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
685
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
686
struct oss_params req, obt;
690
audfmt_e effective_fmt;
691
struct audsettings obt_as;
695
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
697
req.nchannels = as->nchannels;
698
req.fragsize = conf.fragsize;
699
req.nfrags = conf.nfrags;
700
if (oss_open (1, &req, &obt, &fd)) {
704
err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
706
oss_anal_close (&fd);
710
obt_as.freq = obt.freq;
711
obt_as.nchannels = obt.nchannels;
712
obt_as.fmt = effective_fmt;
713
obt_as.endianness = endianness;
715
audio_pcm_init_info (&hw->info, &obt_as);
716
oss->nfrags = obt.nfrags;
717
oss->fragsize = obt.fragsize;
719
if (obt.nfrags * obt.fragsize & hw->info.align) {
720
dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
721
obt.nfrags * obt.fragsize, hw->info.align + 1);
724
hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
725
oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
727
dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
728
hw->samples, 1 << hw->info.shift);
729
oss_anal_close (&fd);
737
static void oss_fini_in (HWVoiceIn *hw)
739
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
741
oss_anal_close (&oss->fd);
744
g_free (oss->pcm_buf);
749
static int oss_run_in (HWVoiceIn *hw)
751
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
752
int hwshift = hw->info.shift;
754
int live = audio_pcm_hw_get_live_in (hw);
755
int dead = hw->samples - live;
756
size_t read_samples = 0;
761
{ .add = hw->wpos, .len = 0 },
762
{ .add = 0, .len = 0 }
769
if (hw->wpos + dead > hw->samples) {
770
bufs[0].len = (hw->samples - hw->wpos) << hwshift;
771
bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
774
bufs[0].len = dead << hwshift;
777
for (i = 0; i < 2; ++i) {
781
void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
782
nread = read (oss->fd, p, bufs[i].len);
785
if (nread & hw->info.align) {
786
dolog ("warning: Misaligned read %zd (requested %d), "
787
"alignment %d\n", nread, bufs[i].add << hwshift,
790
read_samples += nread >> hwshift;
791
hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift);
794
if (bufs[i].len - nread) {
803
"Failed to read %d bytes of audio (to %p)\n",
814
hw->wpos = (hw->wpos + read_samples) % hw->samples;
818
static int oss_read (SWVoiceIn *sw, void *buf, int size)
820
return audio_pcm_sw_read (sw, buf, size);
823
static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
825
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
834
poll_mode = va_arg (ap, int);
837
if (poll_mode && oss_poll_in (hw)) {
840
hw->poll_mode = poll_mode;
847
qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
854
static void *oss_audio_init (void)
859
static void oss_audio_fini (void *opaque)
864
static struct audio_option oss_options[] = {
868
.valp = &conf.fragsize,
869
.descr = "Fragment size in bytes"
874
.valp = &conf.nfrags,
875
.descr = "Number of fragments"
880
.valp = &conf.try_mmap,
881
.descr = "Try using memory mapped access"
886
.valp = &conf.devpath_out,
887
.descr = "Path to DAC device"
892
.valp = &conf.devpath_in,
893
.descr = "Path to ADC device"
898
.valp = &conf.exclusive,
899
.descr = "Open device in exclusive mode (vmix wont work)"
901
#ifdef USE_DSP_POLICY
905
.valp = &conf.policy,
906
.descr = "Set the timing policy of the device, -1 to use fragment mode",
913
.descr = "Turn on some debugging messages"
915
{ /* End of list */ }
918
static struct audio_pcm_ops oss_pcm_ops = {
919
.init_out = oss_init_out,
920
.fini_out = oss_fini_out,
921
.run_out = oss_run_out,
923
.ctl_out = oss_ctl_out,
925
.init_in = oss_init_in,
926
.fini_in = oss_fini_in,
927
.run_in = oss_run_in,
932
struct audio_driver oss_audio_driver = {
934
.descr = "OSS http://www.opensound.com",
935
.options = oss_options,
936
.init = oss_audio_init,
937
.fini = oss_audio_fini,
938
.pcm_ops = &oss_pcm_ops,
940
.max_voices_out = INT_MAX,
941
.max_voices_in = INT_MAX,
942
.voice_size_out = sizeof (OSSVoiceOut),
943
.voice_size_in = sizeof (OSSVoiceIn)