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
)
180 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
188 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
222 dolog ("Unrecognized audio format %d\n", ossfmt
);
229 #if defined DEBUG_MISMATCHES || defined DEBUG
230 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
232 dolog ("parameter | requested value | obtained value\n");
233 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
234 dolog ("channels | %10d | %10d\n",
235 req
->nchannels
, obt
->nchannels
);
236 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
237 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
238 dolog ("fragsize | %10d | %10d\n",
239 req
->fragsize
, obt
->fragsize
);
243 #ifdef USE_DSP_POLICY
244 static int oss_get_version (int fd
, int *version
, const char *typ
)
246 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
247 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
249 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
250 * since 7.x, but currently only on the mixer device (or in
251 * the Linuxolator), and in the native version that part of
252 * the code is in fact never reached so the ioctl fails anyway.
253 * Until this is fixed, just check the errno and if its what
254 * FreeBSD's sound drivers return atm assume they are new enough.
256 if (errno
== EINVAL
) {
261 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
268 static int oss_open (int in
, struct oss_params
*req
,
269 struct oss_params
*obt
, int *pfd
)
272 int oflags
= conf
.exclusive
? O_EXCL
: 0;
273 audio_buf_info abinfo
;
274 int fmt
, freq
, nchannels
;
276 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
277 const char *typ
= in
? "ADC" : "DAC";
279 /* Kludge needed to have working mmap on Linux */
280 oflags
|= conf
.try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
282 fd
= open (dspname
, oflags
| O_NONBLOCK
);
284 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
289 nchannels
= req
->nchannels
;
292 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
293 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
297 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
298 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
303 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
304 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
308 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
309 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
313 #ifdef USE_DSP_POLICY
314 if (conf
.policy
>= 0) {
317 if (!oss_get_version (fd
, &version
, typ
)) {
319 dolog ("OSS version = %#x\n", version
);
322 if (version
>= 0x040000) {
323 int policy
= conf
.policy
;
324 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
325 oss_logerr2 (errno
, typ
,
326 "Failed to set timing policy to %d\n",
337 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
338 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
339 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
340 req
->nfrags
, req
->fragsize
);
345 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
346 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
350 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
351 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
352 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
357 obt
->nchannels
= nchannels
;
359 obt
->nfrags
= abinfo
.fragstotal
;
360 obt
->fragsize
= abinfo
.fragsize
;
363 #ifdef DEBUG_MISMATCHES
364 if ((req
->fmt
!= obt
->fmt
) ||
365 (req
->nchannels
!= obt
->nchannels
) ||
366 (req
->freq
!= obt
->freq
) ||
367 (req
->fragsize
!= obt
->fragsize
) ||
368 (req
->nfrags
!= obt
->nfrags
)) {
369 dolog ("Audio parameters mismatch\n");
370 oss_dump_info (req
, obt
);
375 oss_dump_info (req
, obt
);
380 oss_anal_close (&fd
);
384 static void oss_write_pending (OSSVoiceOut
*oss
)
386 HWVoiceOut
*hw
= &oss
->hw
;
392 while (oss
->pending
) {
394 ssize_t bytes_written
;
395 int samples_till_end
= hw
->samples
- oss
->wpos
;
396 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
397 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
398 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
400 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
401 if (bytes_written
< 0) {
402 if (errno
!= EAGAIN
) {
403 oss_logerr (errno
, "failed to write %d bytes\n",
409 if (bytes_written
& hw
->info
.align
) {
410 dolog ("misaligned write asked for %d, but got %zd\n",
411 bytes_to_write
, bytes_written
);
415 samples_written
= bytes_written
>> hw
->info
.shift
;
416 oss
->pending
-= samples_written
;
417 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
418 if (bytes_written
- bytes_to_write
) {
424 static int oss_run_out (HWVoiceOut
*hw
, int live
)
426 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
428 struct audio_buf_info abinfo
;
429 struct count_info cntinfo
;
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 qemu_free (oss
->pcm_buf
);
507 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
509 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
510 struct oss_params req
, obt
;
514 audfmt_e effective_fmt
;
515 struct audsettings obt_as
;
519 req
.fmt
= aud_to_ossfmt (as
->fmt
);
521 req
.nchannels
= as
->nchannels
;
522 req
.fragsize
= conf
.fragsize
;
523 req
.nfrags
= conf
.nfrags
;
525 if (oss_open (0, &req
, &obt
, &fd
)) {
529 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
531 oss_anal_close (&fd
);
535 obt_as
.freq
= obt
.freq
;
536 obt_as
.nchannels
= obt
.nchannels
;
537 obt_as
.fmt
= effective_fmt
;
538 obt_as
.endianness
= endianness
;
540 audio_pcm_init_info (&hw
->info
, &obt_as
);
541 oss
->nfrags
= obt
.nfrags
;
542 oss
->fragsize
= obt
.fragsize
;
544 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
545 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
546 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
549 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
553 oss
->pcm_buf
= mmap (
555 hw
->samples
<< hw
->info
.shift
,
556 PROT_READ
| PROT_WRITE
,
561 if (oss
->pcm_buf
== MAP_FAILED
) {
562 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
563 hw
->samples
<< hw
->info
.shift
);
568 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
569 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
572 trig
= PCM_ENABLE_OUTPUT
;
573 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
576 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
585 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
587 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
588 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
595 oss
->pcm_buf
= audio_calloc (
602 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
606 oss_anal_close (&fd
);
615 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
618 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
627 poll_mode
= va_arg (ap
, int);
630 ldebug ("enabling voice\n");
631 if (poll_mode
&& oss_poll_out (hw
)) {
634 hw
->poll_mode
= poll_mode
;
640 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
641 trig
= PCM_ENABLE_OUTPUT
;
642 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
645 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
654 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
662 ldebug ("disabling voice\n");
664 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
665 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
673 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
675 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
676 struct oss_params req
, obt
;
680 audfmt_e effective_fmt
;
681 struct audsettings obt_as
;
685 req
.fmt
= aud_to_ossfmt (as
->fmt
);
687 req
.nchannels
= as
->nchannels
;
688 req
.fragsize
= conf
.fragsize
;
689 req
.nfrags
= conf
.nfrags
;
690 if (oss_open (1, &req
, &obt
, &fd
)) {
694 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
696 oss_anal_close (&fd
);
700 obt_as
.freq
= obt
.freq
;
701 obt_as
.nchannels
= obt
.nchannels
;
702 obt_as
.fmt
= effective_fmt
;
703 obt_as
.endianness
= endianness
;
705 audio_pcm_init_info (&hw
->info
, &obt_as
);
706 oss
->nfrags
= obt
.nfrags
;
707 oss
->fragsize
= obt
.fragsize
;
709 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
710 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
711 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
714 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
715 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
717 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
718 hw
->samples
, 1 << hw
->info
.shift
);
719 oss_anal_close (&fd
);
727 static void oss_fini_in (HWVoiceIn
*hw
)
729 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
731 oss_anal_close (&oss
->fd
);
734 qemu_free (oss
->pcm_buf
);
739 static int oss_run_in (HWVoiceIn
*hw
)
741 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
742 int hwshift
= hw
->info
.shift
;
744 int live
= audio_pcm_hw_get_live_in (hw
);
745 int dead
= hw
->samples
- live
;
746 size_t read_samples
= 0;
751 { .add
= hw
->wpos
, .len
= 0 },
752 { .add
= 0, .len
= 0 }
759 if (hw
->wpos
+ dead
> hw
->samples
) {
760 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
761 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
764 bufs
[0].len
= dead
<< hwshift
;
767 for (i
= 0; i
< 2; ++i
) {
771 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
772 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
775 if (nread
& hw
->info
.align
) {
776 dolog ("warning: Misaligned read %zd (requested %d), "
777 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
780 read_samples
+= nread
>> hwshift
;
781 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
,
785 if (bufs
[i
].len
- nread
) {
794 "Failed to read %d bytes of audio (to %p)\n",
805 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
809 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
811 return audio_pcm_sw_read (sw
, buf
, size
);
814 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
816 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
825 poll_mode
= va_arg (ap
, int);
828 if (poll_mode
&& oss_poll_in (hw
)) {
831 hw
->poll_mode
= poll_mode
;
838 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
845 static void *oss_audio_init (void)
850 static void oss_audio_fini (void *opaque
)
855 static struct audio_option oss_options
[] = {
859 .valp
= &conf
.fragsize
,
860 .descr
= "Fragment size in bytes"
865 .valp
= &conf
.nfrags
,
866 .descr
= "Number of fragments"
871 .valp
= &conf
.try_mmap
,
872 .descr
= "Try using memory mapped access"
877 .valp
= &conf
.devpath_out
,
878 .descr
= "Path to DAC device"
883 .valp
= &conf
.devpath_in
,
884 .descr
= "Path to ADC device"
889 .valp
= &conf
.exclusive
,
890 .descr
= "Open device in exclusive mode (vmix wont work)"
892 #ifdef USE_DSP_POLICY
896 .valp
= &conf
.policy
,
897 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
904 .descr
= "Turn on some debugging messages"
906 { /* End of list */ }
909 static struct audio_pcm_ops oss_pcm_ops
= {
910 .init_out
= oss_init_out
,
911 .fini_out
= oss_fini_out
,
912 .run_out
= oss_run_out
,
914 .ctl_out
= oss_ctl_out
,
916 .init_in
= oss_init_in
,
917 .fini_in
= oss_fini_in
,
918 .run_in
= oss_run_in
,
923 struct audio_driver oss_audio_driver
= {
925 .descr
= "OSS http://www.opensound.com",
926 .options
= oss_options
,
927 .init
= oss_audio_init
,
928 .fini
= oss_audio_fini
,
929 .pcm_ops
= &oss_pcm_ops
,
931 .max_voices_out
= INT_MAX
,
932 .max_voices_in
= INT_MAX
,
933 .voice_size_out
= sizeof (OSSVoiceOut
),
934 .voice_size_in
= sizeof (OSSVoiceIn
)