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"
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "qemu-common.h"
29 #include "qemu/main-loop.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 OSSConf
{
45 const char *devpath_out
;
46 const char *devpath_in
;
51 typedef struct OSSVoiceOut
{
63 typedef struct OSSVoiceIn
{
80 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
85 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
88 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
91 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
100 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
103 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
106 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
109 static void oss_anal_close (int *fdp
)
113 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
116 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
121 static void oss_helper_poll_out (void *opaque
)
124 audio_run ("oss_poll_out");
127 static void oss_helper_poll_in (void *opaque
)
130 audio_run ("oss_poll_in");
133 static void oss_poll_out (HWVoiceOut
*hw
)
135 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
137 qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
140 static void oss_poll_in (HWVoiceIn
*hw
)
142 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
144 qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
147 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
149 return audio_pcm_sw_write (sw
, buf
, len
);
152 static int aud_to_ossfmt (audfmt_e fmt
, int endianness
)
178 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
186 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
220 dolog ("Unrecognized audio format %d\n", ossfmt
);
227 #if defined DEBUG_MISMATCHES || defined DEBUG
228 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
230 dolog ("parameter | requested value | obtained value\n");
231 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
232 dolog ("channels | %10d | %10d\n",
233 req
->nchannels
, obt
->nchannels
);
234 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
235 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
236 dolog ("fragsize | %10d | %10d\n",
237 req
->fragsize
, obt
->fragsize
);
241 #ifdef USE_DSP_POLICY
242 static int oss_get_version (int fd
, int *version
, const char *typ
)
244 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
245 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
247 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
248 * since 7.x, but currently only on the mixer device (or in
249 * the Linuxolator), and in the native version that part of
250 * the code is in fact never reached so the ioctl fails anyway.
251 * Until this is fixed, just check the errno and if its what
252 * FreeBSD's sound drivers return atm assume they are new enough.
254 if (errno
== EINVAL
) {
259 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
266 static int oss_open (int in
, struct oss_params
*req
,
267 struct oss_params
*obt
, int *pfd
, OSSConf
* conf
)
270 int oflags
= conf
->exclusive
? O_EXCL
: 0;
271 audio_buf_info abinfo
;
272 int fmt
, freq
, nchannels
;
274 const char *dspname
= in
? conf
->devpath_in
: conf
->devpath_out
;
275 const char *typ
= in
? "ADC" : "DAC";
277 /* Kludge needed to have working mmap on Linux */
278 oflags
|= conf
->try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
280 fd
= open (dspname
, oflags
| O_NONBLOCK
);
282 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
287 nchannels
= req
->nchannels
;
290 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
291 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
295 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
296 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
301 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
302 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
306 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
307 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
311 #ifdef USE_DSP_POLICY
312 if (conf
->policy
>= 0) {
315 if (!oss_get_version (fd
, &version
, typ
)) {
316 trace_oss_version(version
);
318 if (version
>= 0x040000) {
319 int policy
= conf
->policy
;
320 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
321 oss_logerr2 (errno
, typ
,
322 "Failed to set timing policy to %d\n",
333 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
334 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
335 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
336 req
->nfrags
, req
->fragsize
);
341 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
342 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
346 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
347 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
348 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
353 obt
->nchannels
= nchannels
;
355 obt
->nfrags
= abinfo
.fragstotal
;
356 obt
->fragsize
= abinfo
.fragsize
;
359 #ifdef DEBUG_MISMATCHES
360 if ((req
->fmt
!= obt
->fmt
) ||
361 (req
->nchannels
!= obt
->nchannels
) ||
362 (req
->freq
!= obt
->freq
) ||
363 (req
->fragsize
!= obt
->fragsize
) ||
364 (req
->nfrags
!= obt
->nfrags
)) {
365 dolog ("Audio parameters mismatch\n");
366 oss_dump_info (req
, obt
);
371 oss_dump_info (req
, obt
);
376 oss_anal_close (&fd
);
380 static void oss_write_pending (OSSVoiceOut
*oss
)
382 HWVoiceOut
*hw
= &oss
->hw
;
388 while (oss
->pending
) {
390 ssize_t bytes_written
;
391 int samples_till_end
= hw
->samples
- oss
->wpos
;
392 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
393 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
394 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
396 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
397 if (bytes_written
< 0) {
398 if (errno
!= EAGAIN
) {
399 oss_logerr (errno
, "failed to write %d bytes\n",
405 if (bytes_written
& hw
->info
.align
) {
406 dolog ("misaligned write asked for %d, but got %zd\n",
407 bytes_to_write
, bytes_written
);
411 samples_written
= bytes_written
>> hw
->info
.shift
;
412 oss
->pending
-= samples_written
;
413 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
414 if (bytes_written
- bytes_to_write
) {
420 static int oss_run_out (HWVoiceOut
*hw
, int live
)
422 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
424 struct audio_buf_info abinfo
;
425 struct count_info cntinfo
;
428 bufsize
= hw
->samples
<< hw
->info
.shift
;
433 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
435 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
439 pos
= hw
->rpos
<< hw
->info
.shift
;
440 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
441 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
444 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
446 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
450 if (abinfo
.bytes
> bufsize
) {
451 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
452 abinfo
.bytes
= bufsize
;
455 if (abinfo
.bytes
< 0) {
456 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
460 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
466 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
467 oss
->pending
+= decr
;
468 oss_write_pending (oss
);
473 static void oss_fini_out (HWVoiceOut
*hw
)
476 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
478 ldebug ("oss_fini\n");
479 oss_anal_close (&oss
->fd
);
483 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
485 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
486 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
490 g_free (oss
->pcm_buf
);
496 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
499 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
500 struct oss_params req
, obt
;
504 audfmt_e effective_fmt
;
505 struct audsettings obt_as
;
506 OSSConf
*conf
= drv_opaque
;
510 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
512 req
.nchannels
= as
->nchannels
;
513 req
.fragsize
= conf
->fragsize
;
514 req
.nfrags
= conf
->nfrags
;
516 if (oss_open (0, &req
, &obt
, &fd
, conf
)) {
520 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
522 oss_anal_close (&fd
);
526 obt_as
.freq
= obt
.freq
;
527 obt_as
.nchannels
= obt
.nchannels
;
528 obt_as
.fmt
= effective_fmt
;
529 obt_as
.endianness
= endianness
;
531 audio_pcm_init_info (&hw
->info
, &obt_as
);
532 oss
->nfrags
= obt
.nfrags
;
533 oss
->fragsize
= obt
.fragsize
;
535 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
536 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
537 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
540 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
543 if (conf
->try_mmap
) {
544 oss
->pcm_buf
= mmap (
546 hw
->samples
<< hw
->info
.shift
,
547 PROT_READ
| PROT_WRITE
,
552 if (oss
->pcm_buf
== MAP_FAILED
) {
553 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
554 hw
->samples
<< hw
->info
.shift
);
559 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
560 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
563 trig
= PCM_ENABLE_OUTPUT
;
564 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
567 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
576 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
578 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
579 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
586 oss
->pcm_buf
= audio_calloc (
593 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
597 oss_anal_close (&fd
);
607 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
610 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
619 poll_mode
= va_arg (ap
, int);
622 ldebug ("enabling voice\n");
627 hw
->poll_mode
= poll_mode
;
633 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
634 trig
= PCM_ENABLE_OUTPUT
;
635 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
638 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
647 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
655 ldebug ("disabling voice\n");
657 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
658 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
666 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
668 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
669 struct oss_params req
, obt
;
673 audfmt_e effective_fmt
;
674 struct audsettings obt_as
;
675 OSSConf
*conf
= drv_opaque
;
679 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
681 req
.nchannels
= as
->nchannels
;
682 req
.fragsize
= conf
->fragsize
;
683 req
.nfrags
= conf
->nfrags
;
684 if (oss_open (1, &req
, &obt
, &fd
, conf
)) {
688 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
690 oss_anal_close (&fd
);
694 obt_as
.freq
= obt
.freq
;
695 obt_as
.nchannels
= obt
.nchannels
;
696 obt_as
.fmt
= effective_fmt
;
697 obt_as
.endianness
= endianness
;
699 audio_pcm_init_info (&hw
->info
, &obt_as
);
700 oss
->nfrags
= obt
.nfrags
;
701 oss
->fragsize
= obt
.fragsize
;
703 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
704 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
705 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
708 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
709 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
711 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
712 hw
->samples
, 1 << hw
->info
.shift
);
713 oss_anal_close (&fd
);
722 static void oss_fini_in (HWVoiceIn
*hw
)
724 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
726 oss_anal_close (&oss
->fd
);
728 g_free(oss
->pcm_buf
);
732 static int oss_run_in (HWVoiceIn
*hw
)
734 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
735 int hwshift
= hw
->info
.shift
;
737 int live
= audio_pcm_hw_get_live_in (hw
);
738 int dead
= hw
->samples
- live
;
739 size_t read_samples
= 0;
744 { .add
= hw
->wpos
, .len
= 0 },
745 { .add
= 0, .len
= 0 }
752 if (hw
->wpos
+ dead
> hw
->samples
) {
753 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
754 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
757 bufs
[0].len
= dead
<< hwshift
;
760 for (i
= 0; i
< 2; ++i
) {
764 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
765 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
768 if (nread
& hw
->info
.align
) {
769 dolog ("warning: Misaligned read %zd (requested %d), "
770 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
773 read_samples
+= nread
>> hwshift
;
774 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
777 if (bufs
[i
].len
- nread
) {
786 "Failed to read %d bytes of audio (to %p)\n",
797 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
801 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
803 return audio_pcm_sw_read (sw
, buf
, size
);
806 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
808 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
817 poll_mode
= va_arg (ap
, int);
824 hw
->poll_mode
= poll_mode
;
831 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
838 static OSSConf glob_conf
= {
842 .devpath_out
= "/dev/dsp",
843 .devpath_in
= "/dev/dsp",
848 static void *oss_audio_init (void)
850 OSSConf
*conf
= g_malloc(sizeof(OSSConf
));
853 if (access(conf
->devpath_in
, R_OK
| W_OK
) < 0 ||
854 access(conf
->devpath_out
, R_OK
| W_OK
) < 0) {
861 static void oss_audio_fini (void *opaque
)
866 static struct audio_option oss_options
[] = {
870 .valp
= &glob_conf
.fragsize
,
871 .descr
= "Fragment size in bytes"
876 .valp
= &glob_conf
.nfrags
,
877 .descr
= "Number of fragments"
882 .valp
= &glob_conf
.try_mmap
,
883 .descr
= "Try using memory mapped access"
888 .valp
= &glob_conf
.devpath_out
,
889 .descr
= "Path to DAC device"
894 .valp
= &glob_conf
.devpath_in
,
895 .descr
= "Path to ADC device"
900 .valp
= &glob_conf
.exclusive
,
901 .descr
= "Open device in exclusive mode (vmix wont work)"
903 #ifdef USE_DSP_POLICY
907 .valp
= &glob_conf
.policy
,
908 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
911 { /* End of list */ }
914 static struct audio_pcm_ops oss_pcm_ops
= {
915 .init_out
= oss_init_out
,
916 .fini_out
= oss_fini_out
,
917 .run_out
= oss_run_out
,
919 .ctl_out
= oss_ctl_out
,
921 .init_in
= oss_init_in
,
922 .fini_in
= oss_fini_in
,
923 .run_in
= oss_run_in
,
928 struct audio_driver oss_audio_driver
= {
930 .descr
= "OSS http://www.opensound.com",
931 .options
= oss_options
,
932 .init
= oss_audio_init
,
933 .fini
= oss_audio_fini
,
934 .pcm_ops
= &oss_pcm_ops
,
936 .max_voices_out
= INT_MAX
,
937 .max_voices_in
= INT_MAX
,
938 .voice_size_out
= sizeof (OSSVoiceOut
),
939 .voice_size_in
= sizeof (OSSVoiceIn
)