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"
35 #define AUDIO_CAP "oss"
36 #include "audio_int.h"
38 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
39 #define USE_DSP_POLICY
42 typedef struct OSSConf
{
46 const char *devpath_out
;
47 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
)) {
317 trace_oss_version(version
);
319 if (version
>= 0x040000) {
320 int policy
= conf
->policy
;
321 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
322 oss_logerr2 (errno
, typ
,
323 "Failed to set timing policy to %d\n",
334 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
335 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
336 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
337 req
->nfrags
, req
->fragsize
);
342 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
343 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
347 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
348 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
349 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
354 obt
->nchannels
= nchannels
;
356 obt
->nfrags
= abinfo
.fragstotal
;
357 obt
->fragsize
= abinfo
.fragsize
;
360 #ifdef DEBUG_MISMATCHES
361 if ((req
->fmt
!= obt
->fmt
) ||
362 (req
->nchannels
!= obt
->nchannels
) ||
363 (req
->freq
!= obt
->freq
) ||
364 (req
->fragsize
!= obt
->fragsize
) ||
365 (req
->nfrags
!= obt
->nfrags
)) {
366 dolog ("Audio parameters mismatch\n");
367 oss_dump_info (req
, obt
);
372 oss_dump_info (req
, obt
);
377 oss_anal_close (&fd
);
381 static void oss_write_pending (OSSVoiceOut
*oss
)
383 HWVoiceOut
*hw
= &oss
->hw
;
389 while (oss
->pending
) {
391 ssize_t bytes_written
;
392 int samples_till_end
= hw
->samples
- oss
->wpos
;
393 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
394 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
395 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
397 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
398 if (bytes_written
< 0) {
399 if (errno
!= EAGAIN
) {
400 oss_logerr (errno
, "failed to write %d bytes\n",
406 if (bytes_written
& hw
->info
.align
) {
407 dolog ("misaligned write asked for %d, but got %zd\n",
408 bytes_to_write
, bytes_written
);
412 samples_written
= bytes_written
>> hw
->info
.shift
;
413 oss
->pending
-= samples_written
;
414 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
415 if (bytes_written
- bytes_to_write
) {
421 static int oss_run_out (HWVoiceOut
*hw
, int live
)
423 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
425 struct audio_buf_info abinfo
;
426 struct count_info cntinfo
;
429 bufsize
= hw
->samples
<< hw
->info
.shift
;
434 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
436 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
440 pos
= hw
->rpos
<< hw
->info
.shift
;
441 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
442 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
445 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
447 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
451 if (abinfo
.bytes
> bufsize
) {
452 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
453 abinfo
.bytes
= bufsize
;
456 if (abinfo
.bytes
< 0) {
457 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
461 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
467 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
468 oss
->pending
+= decr
;
469 oss_write_pending (oss
);
474 static void oss_fini_out (HWVoiceOut
*hw
)
477 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
479 ldebug ("oss_fini\n");
480 oss_anal_close (&oss
->fd
);
484 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
486 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
487 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
491 g_free (oss
->pcm_buf
);
497 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
500 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
501 struct oss_params req
, obt
;
505 audfmt_e effective_fmt
;
506 struct audsettings obt_as
;
507 OSSConf
*conf
= drv_opaque
;
511 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
513 req
.nchannels
= as
->nchannels
;
514 req
.fragsize
= conf
->fragsize
;
515 req
.nfrags
= conf
->nfrags
;
517 if (oss_open (0, &req
, &obt
, &fd
, conf
)) {
521 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
523 oss_anal_close (&fd
);
527 obt_as
.freq
= obt
.freq
;
528 obt_as
.nchannels
= obt
.nchannels
;
529 obt_as
.fmt
= effective_fmt
;
530 obt_as
.endianness
= endianness
;
532 audio_pcm_init_info (&hw
->info
, &obt_as
);
533 oss
->nfrags
= obt
.nfrags
;
534 oss
->fragsize
= obt
.fragsize
;
536 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
537 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
538 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
541 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
544 if (conf
->try_mmap
) {
545 oss
->pcm_buf
= mmap (
547 hw
->samples
<< hw
->info
.shift
,
548 PROT_READ
| PROT_WRITE
,
553 if (oss
->pcm_buf
== MAP_FAILED
) {
554 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
555 hw
->samples
<< hw
->info
.shift
);
560 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
561 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
564 trig
= PCM_ENABLE_OUTPUT
;
565 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
568 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
577 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
579 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
580 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
587 oss
->pcm_buf
= audio_calloc (
594 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
598 oss_anal_close (&fd
);
608 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
611 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
620 poll_mode
= va_arg (ap
, int);
623 ldebug ("enabling voice\n");
628 hw
->poll_mode
= poll_mode
;
634 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
635 trig
= PCM_ENABLE_OUTPUT
;
636 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
639 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
648 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
656 ldebug ("disabling voice\n");
658 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
659 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
667 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
669 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
670 struct oss_params req
, obt
;
674 audfmt_e effective_fmt
;
675 struct audsettings obt_as
;
676 OSSConf
*conf
= drv_opaque
;
680 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
682 req
.nchannels
= as
->nchannels
;
683 req
.fragsize
= conf
->fragsize
;
684 req
.nfrags
= conf
->nfrags
;
685 if (oss_open (1, &req
, &obt
, &fd
, conf
)) {
689 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
691 oss_anal_close (&fd
);
695 obt_as
.freq
= obt
.freq
;
696 obt_as
.nchannels
= obt
.nchannels
;
697 obt_as
.fmt
= effective_fmt
;
698 obt_as
.endianness
= endianness
;
700 audio_pcm_init_info (&hw
->info
, &obt_as
);
701 oss
->nfrags
= obt
.nfrags
;
702 oss
->fragsize
= obt
.fragsize
;
704 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
705 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
706 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
709 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
710 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
712 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
713 hw
->samples
, 1 << hw
->info
.shift
);
714 oss_anal_close (&fd
);
723 static void oss_fini_in (HWVoiceIn
*hw
)
725 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
727 oss_anal_close (&oss
->fd
);
729 g_free(oss
->pcm_buf
);
733 static int oss_run_in (HWVoiceIn
*hw
)
735 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
736 int hwshift
= hw
->info
.shift
;
738 int live
= audio_pcm_hw_get_live_in (hw
);
739 int dead
= hw
->samples
- live
;
740 size_t read_samples
= 0;
745 { .add
= hw
->wpos
, .len
= 0 },
746 { .add
= 0, .len
= 0 }
753 if (hw
->wpos
+ dead
> hw
->samples
) {
754 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
755 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
758 bufs
[0].len
= dead
<< hwshift
;
761 for (i
= 0; i
< 2; ++i
) {
765 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
766 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
769 if (nread
& hw
->info
.align
) {
770 dolog ("warning: Misaligned read %zd (requested %d), "
771 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
774 read_samples
+= nread
>> hwshift
;
775 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
778 if (bufs
[i
].len
- nread
) {
787 "Failed to read %d bytes of audio (to %p)\n",
798 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
802 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
804 return audio_pcm_sw_read (sw
, buf
, size
);
807 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
809 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
818 poll_mode
= va_arg (ap
, int);
825 hw
->poll_mode
= poll_mode
;
832 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
839 static OSSConf glob_conf
= {
843 .devpath_out
= "/dev/dsp",
844 .devpath_in
= "/dev/dsp",
849 static void *oss_audio_init (void)
851 OSSConf
*conf
= g_malloc(sizeof(OSSConf
));
854 if (access(conf
->devpath_in
, R_OK
| W_OK
) < 0 ||
855 access(conf
->devpath_out
, R_OK
| W_OK
) < 0) {
862 static void oss_audio_fini (void *opaque
)
867 static struct audio_option oss_options
[] = {
871 .valp
= &glob_conf
.fragsize
,
872 .descr
= "Fragment size in bytes"
877 .valp
= &glob_conf
.nfrags
,
878 .descr
= "Number of fragments"
883 .valp
= &glob_conf
.try_mmap
,
884 .descr
= "Try using memory mapped access"
889 .valp
= &glob_conf
.devpath_out
,
890 .descr
= "Path to DAC device"
895 .valp
= &glob_conf
.devpath_in
,
896 .descr
= "Path to ADC device"
901 .valp
= &glob_conf
.exclusive
,
902 .descr
= "Open device in exclusive mode (vmix wont work)"
904 #ifdef USE_DSP_POLICY
908 .valp
= &glob_conf
.policy
,
909 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
912 { /* End of list */ }
915 static struct audio_pcm_ops oss_pcm_ops
= {
916 .init_out
= oss_init_out
,
917 .fini_out
= oss_fini_out
,
918 .run_out
= oss_run_out
,
920 .ctl_out
= oss_ctl_out
,
922 .init_in
= oss_init_in
,
923 .fini_in
= oss_fini_in
,
924 .run_in
= oss_run_in
,
929 struct audio_driver oss_audio_driver
= {
931 .descr
= "OSS http://www.opensound.com",
932 .options
= oss_options
,
933 .init
= oss_audio_init
,
934 .fini
= oss_audio_fini
,
935 .pcm_ops
= &oss_pcm_ops
,
937 .max_voices_out
= INT_MAX
,
938 .max_voices_in
= INT_MAX
,
939 .voice_size_out
= sizeof (OSSVoiceOut
),
940 .voice_size_in
= sizeof (OSSVoiceIn
)