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
24 #include "qemu/osdep.h"
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include "qemu-common.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/host-utils.h"
33 #define AUDIO_CAP "oss"
34 #include "audio_int.h"
36 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
37 #define USE_DSP_POLICY
40 typedef struct OSSVoiceOut
{
52 typedef struct OSSVoiceIn
{
69 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
74 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
77 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
80 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
89 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
92 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
95 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
98 static void oss_anal_close (int *fdp
)
102 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
105 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
110 static void oss_helper_poll_out (void *opaque
)
113 audio_run ("oss_poll_out");
116 static void oss_helper_poll_in (void *opaque
)
119 audio_run ("oss_poll_in");
122 static void oss_poll_out (HWVoiceOut
*hw
)
124 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
126 qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
129 static void oss_poll_in (HWVoiceIn
*hw
)
131 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
133 qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
136 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
138 return audio_pcm_sw_write (sw
, buf
, len
);
141 static int aud_to_ossfmt (AudioFormat fmt
, int endianness
)
144 case AUDIO_FORMAT_S8
:
147 case AUDIO_FORMAT_U8
:
150 case AUDIO_FORMAT_S16
:
158 case AUDIO_FORMAT_U16
:
167 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
175 static int oss_to_audfmt (int ossfmt
, AudioFormat
*fmt
, int *endianness
)
180 *fmt
= AUDIO_FORMAT_S8
;
185 *fmt
= AUDIO_FORMAT_U8
;
190 *fmt
= AUDIO_FORMAT_S16
;
195 *fmt
= AUDIO_FORMAT_U16
;
200 *fmt
= AUDIO_FORMAT_S16
;
205 *fmt
= AUDIO_FORMAT_U16
;
209 dolog ("Unrecognized audio format %d\n", ossfmt
);
216 #if defined DEBUG_MISMATCHES || defined DEBUG
217 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
219 dolog ("parameter | requested value | obtained value\n");
220 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
221 dolog ("channels | %10d | %10d\n",
222 req
->nchannels
, obt
->nchannels
);
223 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
224 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
225 dolog ("fragsize | %10d | %10d\n",
226 req
->fragsize
, obt
->fragsize
);
230 #ifdef USE_DSP_POLICY
231 static int oss_get_version (int fd
, int *version
, const char *typ
)
233 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
234 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
236 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
237 * since 7.x, but currently only on the mixer device (or in
238 * the Linuxolator), and in the native version that part of
239 * the code is in fact never reached so the ioctl fails anyway.
240 * Until this is fixed, just check the errno and if its what
241 * FreeBSD's sound drivers return atm assume they are new enough.
243 if (errno
== EINVAL
) {
248 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
255 static int oss_open(int in
, struct oss_params
*req
, audsettings
*as
,
256 struct oss_params
*obt
, int *pfd
, Audiodev
*dev
)
258 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
259 AudiodevOssPerDirectionOptions
*opdo
= in
? oopts
->in
: oopts
->out
;
261 int oflags
= (oopts
->has_exclusive
&& oopts
->exclusive
) ? O_EXCL
: 0;
262 audio_buf_info abinfo
;
263 int fmt
, freq
, nchannels
;
265 const char *dspname
= opdo
->has_dev
? opdo
->dev
: "/dev/dsp";
266 const char *typ
= in
? "ADC" : "DAC";
267 #ifdef USE_DSP_POLICY
268 int policy
= oopts
->has_dsp_policy
? oopts
->dsp_policy
: 5;
271 /* Kludge needed to have working mmap on Linux */
272 oflags
|= (oopts
->has_try_mmap
&& oopts
->try_mmap
) ?
273 O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
275 fd
= open (dspname
, oflags
| O_NONBLOCK
);
277 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
282 nchannels
= req
->nchannels
;
284 req
->nfrags
= opdo
->has_buffer_count
? opdo
->buffer_count
: 4;
285 req
->fragsize
= audio_buffer_bytes(
286 qapi_AudiodevOssPerDirectionOptions_base(opdo
), as
, 23220);
288 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
289 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
293 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
294 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
299 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
300 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
304 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
305 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
309 #ifdef USE_DSP_POLICY
313 if (!oss_get_version (fd
, &version
, typ
)) {
314 trace_oss_version(version
);
316 if (version
>= 0x040000) {
317 int policy2
= policy
;
318 if (ioctl(fd
, SNDCTL_DSP_POLICY
, &policy2
)) {
319 oss_logerr2 (errno
, typ
,
320 "Failed to set timing policy to %d\n",
331 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
332 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
333 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
334 req
->nfrags
, req
->fragsize
);
339 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
340 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
344 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
345 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
346 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
351 obt
->nchannels
= nchannels
;
353 obt
->nfrags
= abinfo
.fragstotal
;
354 obt
->fragsize
= abinfo
.fragsize
;
357 #ifdef DEBUG_MISMATCHES
358 if ((req
->fmt
!= obt
->fmt
) ||
359 (req
->nchannels
!= obt
->nchannels
) ||
360 (req
->freq
!= obt
->freq
) ||
361 (req
->fragsize
!= obt
->fragsize
) ||
362 (req
->nfrags
!= obt
->nfrags
)) {
363 dolog ("Audio parameters mismatch\n");
364 oss_dump_info (req
, obt
);
369 oss_dump_info (req
, obt
);
374 oss_anal_close (&fd
);
378 static void oss_write_pending (OSSVoiceOut
*oss
)
380 HWVoiceOut
*hw
= &oss
->hw
;
386 while (oss
->pending
) {
388 ssize_t bytes_written
;
389 int samples_till_end
= hw
->samples
- oss
->wpos
;
390 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
391 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
392 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
394 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
395 if (bytes_written
< 0) {
396 if (errno
!= EAGAIN
) {
397 oss_logerr (errno
, "failed to write %d bytes\n",
403 if (bytes_written
& hw
->info
.align
) {
404 dolog ("misaligned write asked for %d, but got %zd\n",
405 bytes_to_write
, bytes_written
);
409 samples_written
= bytes_written
>> hw
->info
.shift
;
410 oss
->pending
-= samples_written
;
411 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
412 if (bytes_written
- bytes_to_write
) {
418 static int oss_run_out (HWVoiceOut
*hw
, int live
)
420 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
422 struct audio_buf_info abinfo
;
423 struct count_info cntinfo
;
426 bufsize
= hw
->samples
<< hw
->info
.shift
;
431 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
433 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
437 pos
= hw
->rpos
<< hw
->info
.shift
;
438 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
439 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
442 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
444 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
448 if (abinfo
.bytes
> bufsize
) {
449 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
450 abinfo
.bytes
= bufsize
;
453 if (abinfo
.bytes
< 0) {
454 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
458 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
464 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
465 oss
->pending
+= decr
;
466 oss_write_pending (oss
);
471 static void oss_fini_out (HWVoiceOut
*hw
)
474 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
476 ldebug ("oss_fini\n");
477 oss_anal_close (&oss
->fd
);
481 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
483 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
484 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
488 g_free (oss
->pcm_buf
);
494 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
497 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
498 struct oss_params req
, obt
;
502 AudioFormat effective_fmt
;
503 struct audsettings obt_as
;
504 Audiodev
*dev
= drv_opaque
;
505 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
509 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
511 req
.nchannels
= as
->nchannels
;
513 if (oss_open(0, &req
, as
, &obt
, &fd
, dev
)) {
517 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
519 oss_anal_close (&fd
);
523 obt_as
.freq
= obt
.freq
;
524 obt_as
.nchannels
= obt
.nchannels
;
525 obt_as
.fmt
= effective_fmt
;
526 obt_as
.endianness
= endianness
;
528 audio_pcm_init_info (&hw
->info
, &obt_as
);
529 oss
->nfrags
= obt
.nfrags
;
530 oss
->fragsize
= obt
.fragsize
;
532 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
533 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
534 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
537 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
540 if (oopts
->has_try_mmap
&& oopts
->try_mmap
) {
541 oss
->pcm_buf
= mmap (
543 hw
->samples
<< hw
->info
.shift
,
544 PROT_READ
| PROT_WRITE
,
549 if (oss
->pcm_buf
== MAP_FAILED
) {
550 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
551 hw
->samples
<< hw
->info
.shift
);
556 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
557 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
560 trig
= PCM_ENABLE_OUTPUT
;
561 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
564 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
573 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
575 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
576 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
583 oss
->pcm_buf
= audio_calloc(__func__
,
585 1 << hw
->info
.shift
);
588 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
592 oss_anal_close (&fd
);
602 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
605 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
606 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
611 bool poll_mode
= opdo
->try_poll
;
613 ldebug ("enabling voice\n");
618 hw
->poll_mode
= poll_mode
;
624 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
625 trig
= PCM_ENABLE_OUTPUT
;
626 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
629 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
638 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
646 ldebug ("disabling voice\n");
648 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
649 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
657 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
659 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
660 struct oss_params req
, obt
;
664 AudioFormat effective_fmt
;
665 struct audsettings obt_as
;
666 Audiodev
*dev
= drv_opaque
;
670 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
672 req
.nchannels
= as
->nchannels
;
673 if (oss_open(1, &req
, as
, &obt
, &fd
, dev
)) {
677 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
679 oss_anal_close (&fd
);
683 obt_as
.freq
= obt
.freq
;
684 obt_as
.nchannels
= obt
.nchannels
;
685 obt_as
.fmt
= effective_fmt
;
686 obt_as
.endianness
= endianness
;
688 audio_pcm_init_info (&hw
->info
, &obt_as
);
689 oss
->nfrags
= obt
.nfrags
;
690 oss
->fragsize
= obt
.fragsize
;
692 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
693 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
694 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
697 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
698 oss
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
700 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
701 hw
->samples
, 1 << hw
->info
.shift
);
702 oss_anal_close (&fd
);
711 static void oss_fini_in (HWVoiceIn
*hw
)
713 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
715 oss_anal_close (&oss
->fd
);
717 g_free(oss
->pcm_buf
);
721 static int oss_run_in (HWVoiceIn
*hw
)
723 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
724 int hwshift
= hw
->info
.shift
;
726 int live
= audio_pcm_hw_get_live_in (hw
);
727 int dead
= hw
->samples
- live
;
728 size_t read_samples
= 0;
733 { .add
= hw
->wpos
, .len
= 0 },
734 { .add
= 0, .len
= 0 }
741 if (hw
->wpos
+ dead
> hw
->samples
) {
742 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
743 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
746 bufs
[0].len
= dead
<< hwshift
;
749 for (i
= 0; i
< 2; ++i
) {
753 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
754 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
757 if (nread
& hw
->info
.align
) {
758 dolog ("warning: Misaligned read %zd (requested %d), "
759 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
762 read_samples
+= nread
>> hwshift
;
763 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
766 if (bufs
[i
].len
- nread
) {
775 "Failed to read %d bytes of audio (to %p)\n",
786 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
790 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
792 return audio_pcm_sw_read (sw
, buf
, size
);
795 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
797 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
798 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
803 bool poll_mode
= opdo
->try_poll
;
809 hw
->poll_mode
= poll_mode
;
816 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
823 static void oss_init_per_direction(AudiodevOssPerDirectionOptions
*opdo
)
825 if (!opdo
->has_try_poll
) {
826 opdo
->try_poll
= true;
827 opdo
->has_try_poll
= true;
831 static void *oss_audio_init(Audiodev
*dev
)
833 AudiodevOssOptions
*oopts
;
834 assert(dev
->driver
== AUDIODEV_DRIVER_OSS
);
837 oss_init_per_direction(oopts
->in
);
838 oss_init_per_direction(oopts
->out
);
840 if (access(oopts
->in
->has_dev
? oopts
->in
->dev
: "/dev/dsp",
842 access(oopts
->out
->has_dev
? oopts
->out
->dev
: "/dev/dsp",
849 static void oss_audio_fini (void *opaque
)
853 static struct audio_pcm_ops oss_pcm_ops
= {
854 .init_out
= oss_init_out
,
855 .fini_out
= oss_fini_out
,
856 .run_out
= oss_run_out
,
858 .ctl_out
= oss_ctl_out
,
860 .init_in
= oss_init_in
,
861 .fini_in
= oss_fini_in
,
862 .run_in
= oss_run_in
,
867 static struct audio_driver oss_audio_driver
= {
869 .descr
= "OSS http://www.opensound.com",
870 .init
= oss_audio_init
,
871 .fini
= oss_audio_fini
,
872 .pcm_ops
= &oss_pcm_ops
,
874 .max_voices_out
= INT_MAX
,
875 .max_voices_in
= INT_MAX
,
876 .voice_size_out
= sizeof (OSSVoiceOut
),
877 .voice_size_in
= sizeof (OSSVoiceIn
)
880 static void register_audio_oss(void)
882 audio_driver_register(&oss_audio_driver
);
884 type_init(register_audio_oss
);