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 OSSConf
{
45 const char *devpath_out
;
46 const char *devpath_in
;
52 typedef struct OSSVoiceOut
{
64 typedef struct OSSVoiceIn
{
81 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
86 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
89 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
92 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
101 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
104 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
107 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
110 static void oss_anal_close (int *fdp
)
114 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
117 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
122 static void oss_helper_poll_out (void *opaque
)
125 audio_run ("oss_poll_out");
128 static void oss_helper_poll_in (void *opaque
)
131 audio_run ("oss_poll_in");
134 static void oss_poll_out (HWVoiceOut
*hw
)
136 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
138 qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
141 static void oss_poll_in (HWVoiceIn
*hw
)
143 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
145 qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
148 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
150 return audio_pcm_sw_write (sw
, buf
, len
);
153 static int aud_to_ossfmt (audfmt_e fmt
, int endianness
)
179 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
187 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
221 dolog ("Unrecognized audio format %d\n", ossfmt
);
228 #if defined DEBUG_MISMATCHES || defined DEBUG
229 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
231 dolog ("parameter | requested value | obtained value\n");
232 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
233 dolog ("channels | %10d | %10d\n",
234 req
->nchannels
, obt
->nchannels
);
235 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
236 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
237 dolog ("fragsize | %10d | %10d\n",
238 req
->fragsize
, obt
->fragsize
);
242 #ifdef USE_DSP_POLICY
243 static int oss_get_version (int fd
, int *version
, const char *typ
)
245 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
246 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
248 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
249 * since 7.x, but currently only on the mixer device (or in
250 * the Linuxolator), and in the native version that part of
251 * the code is in fact never reached so the ioctl fails anyway.
252 * Until this is fixed, just check the errno and if its what
253 * FreeBSD's sound drivers return atm assume they are new enough.
255 if (errno
== EINVAL
) {
260 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
267 static int oss_open (int in
, struct oss_params
*req
,
268 struct oss_params
*obt
, int *pfd
, OSSConf
* conf
)
271 int oflags
= conf
->exclusive
? O_EXCL
: 0;
272 audio_buf_info abinfo
;
273 int fmt
, freq
, nchannels
;
275 const char *dspname
= in
? conf
->devpath_in
: conf
->devpath_out
;
276 const char *typ
= in
? "ADC" : "DAC";
278 /* Kludge needed to have working mmap on Linux */
279 oflags
|= conf
->try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
281 fd
= open (dspname
, oflags
| O_NONBLOCK
);
283 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
288 nchannels
= req
->nchannels
;
291 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
292 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
296 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
297 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
302 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
303 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
307 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
308 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
312 #ifdef USE_DSP_POLICY
313 if (conf
->policy
>= 0) {
316 if (!oss_get_version (fd
, &version
, typ
)) {
318 dolog ("OSS version = %#x\n", version
);
321 if (version
>= 0x040000) {
322 int policy
= conf
->policy
;
323 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
324 oss_logerr2 (errno
, typ
,
325 "Failed to set timing policy to %d\n",
336 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
337 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
338 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
339 req
->nfrags
, req
->fragsize
);
344 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
345 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
349 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
350 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
351 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
356 obt
->nchannels
= nchannels
;
358 obt
->nfrags
= abinfo
.fragstotal
;
359 obt
->fragsize
= abinfo
.fragsize
;
362 #ifdef DEBUG_MISMATCHES
363 if ((req
->fmt
!= obt
->fmt
) ||
364 (req
->nchannels
!= obt
->nchannels
) ||
365 (req
->freq
!= obt
->freq
) ||
366 (req
->fragsize
!= obt
->fragsize
) ||
367 (req
->nfrags
!= obt
->nfrags
)) {
368 dolog ("Audio parameters mismatch\n");
369 oss_dump_info (req
, obt
);
374 oss_dump_info (req
, obt
);
379 oss_anal_close (&fd
);
383 static void oss_write_pending (OSSVoiceOut
*oss
)
385 HWVoiceOut
*hw
= &oss
->hw
;
391 while (oss
->pending
) {
393 ssize_t bytes_written
;
394 int samples_till_end
= hw
->samples
- oss
->wpos
;
395 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
396 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
397 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
399 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
400 if (bytes_written
< 0) {
401 if (errno
!= EAGAIN
) {
402 oss_logerr (errno
, "failed to write %d bytes\n",
408 if (bytes_written
& hw
->info
.align
) {
409 dolog ("misaligned write asked for %d, but got %zd\n",
410 bytes_to_write
, bytes_written
);
414 samples_written
= bytes_written
>> hw
->info
.shift
;
415 oss
->pending
-= samples_written
;
416 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
417 if (bytes_written
- bytes_to_write
) {
423 static int oss_run_out (HWVoiceOut
*hw
, int live
)
425 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
427 struct audio_buf_info abinfo
;
428 struct count_info cntinfo
;
430 OSSConf
*conf
= oss
->conf
;
432 bufsize
= hw
->samples
<< hw
->info
.shift
;
437 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
439 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
443 pos
= hw
->rpos
<< hw
->info
.shift
;
444 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
445 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
448 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
450 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
454 if (abinfo
.bytes
> bufsize
) {
456 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
457 "please report your OS/audio hw to av1474@comtv.ru\n",
458 abinfo
.bytes
, bufsize
);
460 abinfo
.bytes
= bufsize
;
463 if (abinfo
.bytes
< 0) {
465 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
466 abinfo
.bytes
, bufsize
);
471 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
477 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
478 oss
->pending
+= decr
;
479 oss_write_pending (oss
);
484 static void oss_fini_out (HWVoiceOut
*hw
)
487 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
489 ldebug ("oss_fini\n");
490 oss_anal_close (&oss
->fd
);
494 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
496 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
497 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
501 g_free (oss
->pcm_buf
);
507 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
510 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
511 struct oss_params req
, obt
;
515 audfmt_e effective_fmt
;
516 struct audsettings obt_as
;
517 OSSConf
*conf
= drv_opaque
;
521 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
523 req
.nchannels
= as
->nchannels
;
524 req
.fragsize
= conf
->fragsize
;
525 req
.nfrags
= conf
->nfrags
;
527 if (oss_open (0, &req
, &obt
, &fd
, conf
)) {
531 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
533 oss_anal_close (&fd
);
537 obt_as
.freq
= obt
.freq
;
538 obt_as
.nchannels
= obt
.nchannels
;
539 obt_as
.fmt
= effective_fmt
;
540 obt_as
.endianness
= endianness
;
542 audio_pcm_init_info (&hw
->info
, &obt_as
);
543 oss
->nfrags
= obt
.nfrags
;
544 oss
->fragsize
= obt
.fragsize
;
546 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
547 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
548 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
551 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
554 if (conf
->try_mmap
) {
555 oss
->pcm_buf
= mmap (
557 hw
->samples
<< hw
->info
.shift
,
558 PROT_READ
| PROT_WRITE
,
563 if (oss
->pcm_buf
== MAP_FAILED
) {
564 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
565 hw
->samples
<< hw
->info
.shift
);
570 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
571 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
574 trig
= PCM_ENABLE_OUTPUT
;
575 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
578 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
587 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
589 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
590 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
597 oss
->pcm_buf
= audio_calloc (
604 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
608 oss_anal_close (&fd
);
618 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
621 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
630 poll_mode
= va_arg (ap
, int);
633 ldebug ("enabling voice\n");
638 hw
->poll_mode
= poll_mode
;
644 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
645 trig
= PCM_ENABLE_OUTPUT
;
646 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
649 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
658 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
666 ldebug ("disabling voice\n");
668 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
669 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
677 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
679 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
680 struct oss_params req
, obt
;
684 audfmt_e effective_fmt
;
685 struct audsettings obt_as
;
686 OSSConf
*conf
= drv_opaque
;
690 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
692 req
.nchannels
= as
->nchannels
;
693 req
.fragsize
= conf
->fragsize
;
694 req
.nfrags
= conf
->nfrags
;
695 if (oss_open (1, &req
, &obt
, &fd
, conf
)) {
699 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
701 oss_anal_close (&fd
);
705 obt_as
.freq
= obt
.freq
;
706 obt_as
.nchannels
= obt
.nchannels
;
707 obt_as
.fmt
= effective_fmt
;
708 obt_as
.endianness
= endianness
;
710 audio_pcm_init_info (&hw
->info
, &obt_as
);
711 oss
->nfrags
= obt
.nfrags
;
712 oss
->fragsize
= obt
.fragsize
;
714 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
715 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
716 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
719 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
720 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
722 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
723 hw
->samples
, 1 << hw
->info
.shift
);
724 oss_anal_close (&fd
);
733 static void oss_fini_in (HWVoiceIn
*hw
)
735 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
737 oss_anal_close (&oss
->fd
);
739 g_free(oss
->pcm_buf
);
743 static int oss_run_in (HWVoiceIn
*hw
)
745 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
746 int hwshift
= hw
->info
.shift
;
748 int live
= audio_pcm_hw_get_live_in (hw
);
749 int dead
= hw
->samples
- live
;
750 size_t read_samples
= 0;
755 { .add
= hw
->wpos
, .len
= 0 },
756 { .add
= 0, .len
= 0 }
763 if (hw
->wpos
+ dead
> hw
->samples
) {
764 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
765 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
768 bufs
[0].len
= dead
<< hwshift
;
771 for (i
= 0; i
< 2; ++i
) {
775 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
776 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
779 if (nread
& hw
->info
.align
) {
780 dolog ("warning: Misaligned read %zd (requested %d), "
781 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
784 read_samples
+= nread
>> hwshift
;
785 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
788 if (bufs
[i
].len
- nread
) {
797 "Failed to read %d bytes of audio (to %p)\n",
808 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
812 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
814 return audio_pcm_sw_read (sw
, buf
, size
);
817 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
819 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
828 poll_mode
= va_arg (ap
, int);
835 hw
->poll_mode
= poll_mode
;
842 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
849 static OSSConf glob_conf
= {
853 .devpath_out
= "/dev/dsp",
854 .devpath_in
= "/dev/dsp",
860 static void *oss_audio_init (void)
862 OSSConf
*conf
= g_malloc(sizeof(OSSConf
));
865 if (access(conf
->devpath_in
, R_OK
| W_OK
) < 0 ||
866 access(conf
->devpath_out
, R_OK
| W_OK
) < 0) {
872 static void oss_audio_fini (void *opaque
)
877 static struct audio_option oss_options
[] = {
881 .valp
= &glob_conf
.fragsize
,
882 .descr
= "Fragment size in bytes"
887 .valp
= &glob_conf
.nfrags
,
888 .descr
= "Number of fragments"
893 .valp
= &glob_conf
.try_mmap
,
894 .descr
= "Try using memory mapped access"
899 .valp
= &glob_conf
.devpath_out
,
900 .descr
= "Path to DAC device"
905 .valp
= &glob_conf
.devpath_in
,
906 .descr
= "Path to ADC device"
911 .valp
= &glob_conf
.exclusive
,
912 .descr
= "Open device in exclusive mode (vmix wont work)"
914 #ifdef USE_DSP_POLICY
918 .valp
= &glob_conf
.policy
,
919 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
925 .valp
= &glob_conf
.debug
,
926 .descr
= "Turn on some debugging messages"
928 { /* End of list */ }
931 static struct audio_pcm_ops oss_pcm_ops
= {
932 .init_out
= oss_init_out
,
933 .fini_out
= oss_fini_out
,
934 .run_out
= oss_run_out
,
936 .ctl_out
= oss_ctl_out
,
938 .init_in
= oss_init_in
,
939 .fini_in
= oss_fini_in
,
940 .run_in
= oss_run_in
,
945 struct audio_driver oss_audio_driver
= {
947 .descr
= "OSS http://www.opensound.com",
948 .options
= oss_options
,
949 .init
= oss_audio_init
,
950 .fini
= oss_audio_fini
,
951 .pcm_ops
= &oss_pcm_ops
,
953 .max_voices_out
= INT_MAX
,
954 .max_voices_in
= INT_MAX
,
955 .voice_size_out
= sizeof (OSSVoiceOut
),
956 .voice_size_in
= sizeof (OSSVoiceIn
)