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
25 #include "qemu/osdep.h"
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "qemu/main-loop.h"
29 #include "qemu/module.h"
30 #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
{
53 typedef struct OSSVoiceIn
{
70 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
75 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
78 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
81 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
90 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
93 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
96 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
99 static void oss_anal_close (int *fdp
)
103 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
106 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
111 static void oss_helper_poll_out (void *opaque
)
114 audio_run ("oss_poll_out");
117 static void oss_helper_poll_in (void *opaque
)
120 audio_run ("oss_poll_in");
123 static void oss_poll_out (HWVoiceOut
*hw
)
125 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
127 qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
130 static void oss_poll_in (HWVoiceIn
*hw
)
132 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
134 qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
137 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
139 return audio_pcm_sw_write (sw
, buf
, len
);
142 static int aud_to_ossfmt (AudioFormat fmt
, int endianness
)
145 case AUDIO_FORMAT_S8
:
148 case AUDIO_FORMAT_U8
:
151 case AUDIO_FORMAT_S16
:
159 case AUDIO_FORMAT_U16
:
168 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
176 static int oss_to_audfmt (int ossfmt
, AudioFormat
*fmt
, int *endianness
)
181 *fmt
= AUDIO_FORMAT_S8
;
186 *fmt
= AUDIO_FORMAT_U8
;
191 *fmt
= AUDIO_FORMAT_S16
;
196 *fmt
= AUDIO_FORMAT_U16
;
201 *fmt
= AUDIO_FORMAT_S16
;
206 *fmt
= AUDIO_FORMAT_U16
;
210 dolog ("Unrecognized audio format %d\n", ossfmt
);
217 #if defined DEBUG_MISMATCHES || defined DEBUG
218 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
220 dolog ("parameter | requested value | obtained value\n");
221 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
222 dolog ("channels | %10d | %10d\n",
223 req
->nchannels
, obt
->nchannels
);
224 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
225 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
226 dolog ("fragsize | %10d | %10d\n",
227 req
->fragsize
, obt
->fragsize
);
231 #ifdef USE_DSP_POLICY
232 static int oss_get_version (int fd
, int *version
, const char *typ
)
234 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
235 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
237 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
238 * since 7.x, but currently only on the mixer device (or in
239 * the Linuxolator), and in the native version that part of
240 * the code is in fact never reached so the ioctl fails anyway.
241 * Until this is fixed, just check the errno and if its what
242 * FreeBSD's sound drivers return atm assume they are new enough.
244 if (errno
== EINVAL
) {
249 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
256 static int oss_open(int in
, struct oss_params
*req
, audsettings
*as
,
257 struct oss_params
*obt
, int *pfd
, Audiodev
*dev
)
259 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
260 AudiodevOssPerDirectionOptions
*opdo
= in
? oopts
->in
: oopts
->out
;
262 int oflags
= (oopts
->has_exclusive
&& oopts
->exclusive
) ? O_EXCL
: 0;
263 audio_buf_info abinfo
;
264 int fmt
, freq
, nchannels
;
266 const char *dspname
= opdo
->has_dev
? opdo
->dev
: "/dev/dsp";
267 const char *typ
= in
? "ADC" : "DAC";
268 #ifdef USE_DSP_POLICY
269 int policy
= oopts
->has_dsp_policy
? oopts
->dsp_policy
: 5;
272 /* Kludge needed to have working mmap on Linux */
273 oflags
|= (oopts
->has_try_mmap
&& oopts
->try_mmap
) ?
274 O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
276 fd
= open (dspname
, oflags
| O_NONBLOCK
);
278 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
283 nchannels
= req
->nchannels
;
285 req
->nfrags
= opdo
->has_buffer_count
? opdo
->buffer_count
: 4;
286 req
->fragsize
= audio_buffer_bytes(
287 qapi_AudiodevOssPerDirectionOptions_base(opdo
), as
, 23220);
289 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
290 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
294 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
295 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
300 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
301 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
305 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
306 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
310 #ifdef USE_DSP_POLICY
314 if (!oss_get_version (fd
, &version
, typ
)) {
315 trace_oss_version(version
);
317 if (version
>= 0x040000) {
318 int policy2
= policy
;
319 if (ioctl(fd
, SNDCTL_DSP_POLICY
, &policy2
)) {
320 oss_logerr2 (errno
, typ
,
321 "Failed to set timing policy to %d\n",
332 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
333 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
334 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
335 req
->nfrags
, req
->fragsize
);
340 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
341 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
345 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
346 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
347 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
352 obt
->nchannels
= nchannels
;
354 obt
->nfrags
= abinfo
.fragstotal
;
355 obt
->fragsize
= abinfo
.fragsize
;
358 #ifdef DEBUG_MISMATCHES
359 if ((req
->fmt
!= obt
->fmt
) ||
360 (req
->nchannels
!= obt
->nchannels
) ||
361 (req
->freq
!= obt
->freq
) ||
362 (req
->fragsize
!= obt
->fragsize
) ||
363 (req
->nfrags
!= obt
->nfrags
)) {
364 dolog ("Audio parameters mismatch\n");
365 oss_dump_info (req
, obt
);
370 oss_dump_info (req
, obt
);
375 oss_anal_close (&fd
);
379 static void oss_write_pending (OSSVoiceOut
*oss
)
381 HWVoiceOut
*hw
= &oss
->hw
;
387 while (oss
->pending
) {
389 ssize_t bytes_written
;
390 int samples_till_end
= hw
->samples
- oss
->wpos
;
391 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
392 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
393 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
395 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
396 if (bytes_written
< 0) {
397 if (errno
!= EAGAIN
) {
398 oss_logerr (errno
, "failed to write %d bytes\n",
404 if (bytes_written
& hw
->info
.align
) {
405 dolog ("misaligned write asked for %d, but got %zd\n",
406 bytes_to_write
, bytes_written
);
410 samples_written
= bytes_written
>> hw
->info
.shift
;
411 oss
->pending
-= samples_written
;
412 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
413 if (bytes_written
- bytes_to_write
) {
419 static int oss_run_out (HWVoiceOut
*hw
, int live
)
421 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
423 struct audio_buf_info abinfo
;
424 struct count_info cntinfo
;
427 bufsize
= hw
->samples
<< hw
->info
.shift
;
432 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
434 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
438 pos
= hw
->rpos
<< hw
->info
.shift
;
439 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
440 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
443 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
445 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
449 if (abinfo
.bytes
> bufsize
) {
450 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
451 abinfo
.bytes
= bufsize
;
454 if (abinfo
.bytes
< 0) {
455 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
459 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
465 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
466 oss
->pending
+= decr
;
467 oss_write_pending (oss
);
472 static void oss_fini_out (HWVoiceOut
*hw
)
475 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
477 ldebug ("oss_fini\n");
478 oss_anal_close (&oss
->fd
);
482 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
484 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
485 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
489 g_free (oss
->pcm_buf
);
495 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
498 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
499 struct oss_params req
, obt
;
503 AudioFormat effective_fmt
;
504 struct audsettings obt_as
;
505 Audiodev
*dev
= drv_opaque
;
506 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
510 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
512 req
.nchannels
= as
->nchannels
;
514 if (oss_open(0, &req
, as
, &obt
, &fd
, dev
)) {
518 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
520 oss_anal_close (&fd
);
524 obt_as
.freq
= obt
.freq
;
525 obt_as
.nchannels
= obt
.nchannels
;
526 obt_as
.fmt
= effective_fmt
;
527 obt_as
.endianness
= endianness
;
529 audio_pcm_init_info (&hw
->info
, &obt_as
);
530 oss
->nfrags
= obt
.nfrags
;
531 oss
->fragsize
= obt
.fragsize
;
533 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
534 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
535 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
538 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
541 if (oopts
->has_try_mmap
&& oopts
->try_mmap
) {
542 oss
->pcm_buf
= mmap (
544 hw
->samples
<< hw
->info
.shift
,
545 PROT_READ
| PROT_WRITE
,
550 if (oss
->pcm_buf
== MAP_FAILED
) {
551 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
552 hw
->samples
<< hw
->info
.shift
);
557 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
558 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
561 trig
= PCM_ENABLE_OUTPUT
;
562 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
565 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
574 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
576 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
577 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
584 oss
->pcm_buf
= audio_calloc(__func__
,
586 1 << hw
->info
.shift
);
589 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
593 oss_anal_close (&fd
);
603 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
606 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
607 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
612 bool poll_mode
= opdo
->try_poll
;
614 ldebug ("enabling voice\n");
619 hw
->poll_mode
= poll_mode
;
625 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
626 trig
= PCM_ENABLE_OUTPUT
;
627 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
630 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
639 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
647 ldebug ("disabling voice\n");
649 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
650 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
658 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
660 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
661 struct oss_params req
, obt
;
665 AudioFormat effective_fmt
;
666 struct audsettings obt_as
;
667 Audiodev
*dev
= drv_opaque
;
671 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
673 req
.nchannels
= as
->nchannels
;
674 if (oss_open(1, &req
, as
, &obt
, &fd
, dev
)) {
678 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
680 oss_anal_close (&fd
);
684 obt_as
.freq
= obt
.freq
;
685 obt_as
.nchannels
= obt
.nchannels
;
686 obt_as
.fmt
= effective_fmt
;
687 obt_as
.endianness
= endianness
;
689 audio_pcm_init_info (&hw
->info
, &obt_as
);
690 oss
->nfrags
= obt
.nfrags
;
691 oss
->fragsize
= obt
.fragsize
;
693 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
694 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
695 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
698 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
699 oss
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
701 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
702 hw
->samples
, 1 << hw
->info
.shift
);
703 oss_anal_close (&fd
);
712 static void oss_fini_in (HWVoiceIn
*hw
)
714 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
716 oss_anal_close (&oss
->fd
);
718 g_free(oss
->pcm_buf
);
722 static int oss_run_in (HWVoiceIn
*hw
)
724 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
725 int hwshift
= hw
->info
.shift
;
727 int live
= audio_pcm_hw_get_live_in (hw
);
728 int dead
= hw
->samples
- live
;
729 size_t read_samples
= 0;
734 { .add
= hw
->wpos
, .len
= 0 },
735 { .add
= 0, .len
= 0 }
742 if (hw
->wpos
+ dead
> hw
->samples
) {
743 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
744 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
747 bufs
[0].len
= dead
<< hwshift
;
750 for (i
= 0; i
< 2; ++i
) {
754 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
755 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
758 if (nread
& hw
->info
.align
) {
759 dolog ("warning: Misaligned read %zd (requested %d), "
760 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
763 read_samples
+= nread
>> hwshift
;
764 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
767 if (bufs
[i
].len
- nread
) {
776 "Failed to read %d bytes of audio (to %p)\n",
787 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
791 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
793 return audio_pcm_sw_read (sw
, buf
, size
);
796 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
798 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
799 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
804 bool poll_mode
= opdo
->try_poll
;
810 hw
->poll_mode
= poll_mode
;
817 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
824 static void oss_init_per_direction(AudiodevOssPerDirectionOptions
*opdo
)
826 if (!opdo
->has_try_poll
) {
827 opdo
->try_poll
= true;
828 opdo
->has_try_poll
= true;
832 static void *oss_audio_init(Audiodev
*dev
)
834 AudiodevOssOptions
*oopts
;
835 assert(dev
->driver
== AUDIODEV_DRIVER_OSS
);
838 oss_init_per_direction(oopts
->in
);
839 oss_init_per_direction(oopts
->out
);
841 if (access(oopts
->in
->has_dev
? oopts
->in
->dev
: "/dev/dsp",
843 access(oopts
->out
->has_dev
? oopts
->out
->dev
: "/dev/dsp",
850 static void oss_audio_fini (void *opaque
)
854 static struct audio_pcm_ops oss_pcm_ops
= {
855 .init_out
= oss_init_out
,
856 .fini_out
= oss_fini_out
,
857 .run_out
= oss_run_out
,
859 .ctl_out
= oss_ctl_out
,
861 .init_in
= oss_init_in
,
862 .fini_in
= oss_fini_in
,
863 .run_in
= oss_run_in
,
868 static struct audio_driver oss_audio_driver
= {
870 .descr
= "OSS http://www.opensound.com",
871 .init
= oss_audio_init
,
872 .fini
= oss_audio_fini
,
873 .pcm_ops
= &oss_pcm_ops
,
875 .max_voices_out
= INT_MAX
,
876 .max_voices_in
= INT_MAX
,
877 .voice_size_out
= sizeof (OSSVoiceOut
),
878 .voice_size_in
= sizeof (OSSVoiceIn
)
881 static void register_audio_oss(void)
883 audio_driver_register(&oss_audio_driver
);
885 type_init(register_audio_oss
);