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 "main-loop.h"
35 #include "host-utils.h"
36 #include "qemu-char.h"
39 #define AUDIO_CAP "oss"
40 #include "audio_int.h"
42 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
43 #define USE_DSP_POLICY
46 typedef struct OSSVoiceOut
{
57 typedef struct OSSVoiceIn
{
69 const char *devpath_out
;
70 const char *devpath_in
;
78 .devpath_out
= "/dev/dsp",
79 .devpath_in
= "/dev/dsp",
93 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
98 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
101 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
104 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
113 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
116 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
119 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
122 static void oss_anal_close (int *fdp
)
126 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
129 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
134 static void oss_helper_poll_out (void *opaque
)
137 audio_run ("oss_poll_out");
140 static void oss_helper_poll_in (void *opaque
)
143 audio_run ("oss_poll_in");
146 static int oss_poll_out (HWVoiceOut
*hw
)
148 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
150 return qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
153 static int oss_poll_in (HWVoiceIn
*hw
)
155 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
157 return qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
160 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
162 return audio_pcm_sw_write (sw
, buf
, len
);
165 static int aud_to_ossfmt (audfmt_e fmt
, int endianness
)
191 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
199 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
233 dolog ("Unrecognized audio format %d\n", ossfmt
);
240 #if defined DEBUG_MISMATCHES || defined DEBUG
241 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
243 dolog ("parameter | requested value | obtained value\n");
244 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
245 dolog ("channels | %10d | %10d\n",
246 req
->nchannels
, obt
->nchannels
);
247 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
248 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
249 dolog ("fragsize | %10d | %10d\n",
250 req
->fragsize
, obt
->fragsize
);
254 #ifdef USE_DSP_POLICY
255 static int oss_get_version (int fd
, int *version
, const char *typ
)
257 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
258 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
260 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
261 * since 7.x, but currently only on the mixer device (or in
262 * the Linuxolator), and in the native version that part of
263 * the code is in fact never reached so the ioctl fails anyway.
264 * Until this is fixed, just check the errno and if its what
265 * FreeBSD's sound drivers return atm assume they are new enough.
267 if (errno
== EINVAL
) {
272 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
279 static int oss_open (int in
, struct oss_params
*req
,
280 struct oss_params
*obt
, int *pfd
)
283 int oflags
= conf
.exclusive
? O_EXCL
: 0;
284 audio_buf_info abinfo
;
285 int fmt
, freq
, nchannels
;
287 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
288 const char *typ
= in
? "ADC" : "DAC";
290 /* Kludge needed to have working mmap on Linux */
291 oflags
|= conf
.try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
293 fd
= open (dspname
, oflags
| O_NONBLOCK
);
295 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
300 nchannels
= req
->nchannels
;
303 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
304 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
308 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
309 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
314 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
315 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
319 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
320 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
324 #ifdef USE_DSP_POLICY
325 if (conf
.policy
>= 0) {
328 if (!oss_get_version (fd
, &version
, typ
)) {
330 dolog ("OSS version = %#x\n", version
);
333 if (version
>= 0x040000) {
334 int policy
= conf
.policy
;
335 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
336 oss_logerr2 (errno
, typ
,
337 "Failed to set timing policy to %d\n",
348 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
349 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
350 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
351 req
->nfrags
, req
->fragsize
);
356 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
357 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
361 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
362 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
363 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
368 obt
->nchannels
= nchannels
;
370 obt
->nfrags
= abinfo
.fragstotal
;
371 obt
->fragsize
= abinfo
.fragsize
;
374 #ifdef DEBUG_MISMATCHES
375 if ((req
->fmt
!= obt
->fmt
) ||
376 (req
->nchannels
!= obt
->nchannels
) ||
377 (req
->freq
!= obt
->freq
) ||
378 (req
->fragsize
!= obt
->fragsize
) ||
379 (req
->nfrags
!= obt
->nfrags
)) {
380 dolog ("Audio parameters mismatch\n");
381 oss_dump_info (req
, obt
);
386 oss_dump_info (req
, obt
);
391 oss_anal_close (&fd
);
395 static void oss_write_pending (OSSVoiceOut
*oss
)
397 HWVoiceOut
*hw
= &oss
->hw
;
403 while (oss
->pending
) {
405 ssize_t bytes_written
;
406 int samples_till_end
= hw
->samples
- oss
->wpos
;
407 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
408 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
409 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
411 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
412 if (bytes_written
< 0) {
413 if (errno
!= EAGAIN
) {
414 oss_logerr (errno
, "failed to write %d bytes\n",
420 if (bytes_written
& hw
->info
.align
) {
421 dolog ("misaligned write asked for %d, but got %zd\n",
422 bytes_to_write
, bytes_written
);
426 samples_written
= bytes_written
>> hw
->info
.shift
;
427 oss
->pending
-= samples_written
;
428 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
429 if (bytes_written
- bytes_to_write
) {
435 static int oss_run_out (HWVoiceOut
*hw
, int live
)
437 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
439 struct audio_buf_info abinfo
;
440 struct count_info cntinfo
;
443 bufsize
= hw
->samples
<< hw
->info
.shift
;
448 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
450 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
454 pos
= hw
->rpos
<< hw
->info
.shift
;
455 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
456 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
459 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
461 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
465 if (abinfo
.bytes
> bufsize
) {
467 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
468 "please report your OS/audio hw to av1474@comtv.ru\n",
469 abinfo
.bytes
, bufsize
);
471 abinfo
.bytes
= bufsize
;
474 if (abinfo
.bytes
< 0) {
476 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
477 abinfo
.bytes
, bufsize
);
482 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
488 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
489 oss
->pending
+= decr
;
490 oss_write_pending (oss
);
495 static void oss_fini_out (HWVoiceOut
*hw
)
498 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
500 ldebug ("oss_fini\n");
501 oss_anal_close (&oss
->fd
);
505 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
507 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
508 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
512 g_free (oss
->pcm_buf
);
518 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
520 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
521 struct oss_params req
, obt
;
525 audfmt_e effective_fmt
;
526 struct audsettings obt_as
;
530 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
532 req
.nchannels
= as
->nchannels
;
533 req
.fragsize
= conf
.fragsize
;
534 req
.nfrags
= conf
.nfrags
;
536 if (oss_open (0, &req
, &obt
, &fd
)) {
540 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
542 oss_anal_close (&fd
);
546 obt_as
.freq
= obt
.freq
;
547 obt_as
.nchannels
= obt
.nchannels
;
548 obt_as
.fmt
= effective_fmt
;
549 obt_as
.endianness
= endianness
;
551 audio_pcm_init_info (&hw
->info
, &obt_as
);
552 oss
->nfrags
= obt
.nfrags
;
553 oss
->fragsize
= obt
.fragsize
;
555 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
556 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
557 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
560 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
564 oss
->pcm_buf
= mmap (
566 hw
->samples
<< hw
->info
.shift
,
567 PROT_READ
| PROT_WRITE
,
572 if (oss
->pcm_buf
== MAP_FAILED
) {
573 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
574 hw
->samples
<< hw
->info
.shift
);
579 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
580 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
583 trig
= PCM_ENABLE_OUTPUT
;
584 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
587 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
596 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
598 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
599 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
606 oss
->pcm_buf
= audio_calloc (
613 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
617 oss_anal_close (&fd
);
626 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
629 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
638 poll_mode
= va_arg (ap
, int);
641 ldebug ("enabling voice\n");
642 if (poll_mode
&& oss_poll_out (hw
)) {
645 hw
->poll_mode
= poll_mode
;
651 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
652 trig
= PCM_ENABLE_OUTPUT
;
653 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
656 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
665 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
673 ldebug ("disabling voice\n");
675 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
676 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
684 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
686 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
687 struct oss_params req
, obt
;
691 audfmt_e effective_fmt
;
692 struct audsettings obt_as
;
696 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
698 req
.nchannels
= as
->nchannels
;
699 req
.fragsize
= conf
.fragsize
;
700 req
.nfrags
= conf
.nfrags
;
701 if (oss_open (1, &req
, &obt
, &fd
)) {
705 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
707 oss_anal_close (&fd
);
711 obt_as
.freq
= obt
.freq
;
712 obt_as
.nchannels
= obt
.nchannels
;
713 obt_as
.fmt
= effective_fmt
;
714 obt_as
.endianness
= endianness
;
716 audio_pcm_init_info (&hw
->info
, &obt_as
);
717 oss
->nfrags
= obt
.nfrags
;
718 oss
->fragsize
= obt
.fragsize
;
720 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
721 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
722 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
725 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
726 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
728 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
729 hw
->samples
, 1 << hw
->info
.shift
);
730 oss_anal_close (&fd
);
738 static void oss_fini_in (HWVoiceIn
*hw
)
740 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
742 oss_anal_close (&oss
->fd
);
745 g_free (oss
->pcm_buf
);
750 static int oss_run_in (HWVoiceIn
*hw
)
752 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
753 int hwshift
= hw
->info
.shift
;
755 int live
= audio_pcm_hw_get_live_in (hw
);
756 int dead
= hw
->samples
- live
;
757 size_t read_samples
= 0;
762 { .add
= hw
->wpos
, .len
= 0 },
763 { .add
= 0, .len
= 0 }
770 if (hw
->wpos
+ dead
> hw
->samples
) {
771 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
772 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
775 bufs
[0].len
= dead
<< hwshift
;
778 for (i
= 0; i
< 2; ++i
) {
782 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
783 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
786 if (nread
& hw
->info
.align
) {
787 dolog ("warning: Misaligned read %zd (requested %d), "
788 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
791 read_samples
+= nread
>> hwshift
;
792 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
795 if (bufs
[i
].len
- nread
) {
804 "Failed to read %d bytes of audio (to %p)\n",
815 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
819 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
821 return audio_pcm_sw_read (sw
, buf
, size
);
824 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
826 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
835 poll_mode
= va_arg (ap
, int);
838 if (poll_mode
&& oss_poll_in (hw
)) {
841 hw
->poll_mode
= poll_mode
;
848 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
855 static void *oss_audio_init (void)
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
)