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 typedef struct OSSVoiceOut
{
52 typedef struct OSSVoiceIn
{
64 const char *devpath_out
;
65 const char *devpath_in
;
73 .devpath_out
= "/dev/dsp",
74 .devpath_in
= "/dev/dsp",
88 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
93 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
96 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
99 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
108 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
111 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
114 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
117 static void oss_anal_close (int *fdp
)
121 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
124 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
129 static void oss_helper_poll_out (void *opaque
)
132 audio_run ("oss_poll_out");
135 static void oss_helper_poll_in (void *opaque
)
138 audio_run ("oss_poll_in");
141 static int oss_poll_out (HWVoiceOut
*hw
)
143 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
145 return qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
148 static int oss_poll_in (HWVoiceIn
*hw
)
150 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
152 return qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
155 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
157 return audio_pcm_sw_write (sw
, buf
, len
);
160 static int aud_to_ossfmt (audfmt_e fmt
)
176 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
184 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
218 dolog ("Unrecognized audio format %d\n", ossfmt
);
225 #if defined DEBUG_MISMATCHES || defined DEBUG
226 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
228 dolog ("parameter | requested value | obtained value\n");
229 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
230 dolog ("channels | %10d | %10d\n",
231 req
->nchannels
, obt
->nchannels
);
232 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
233 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
234 dolog ("fragsize | %10d | %10d\n",
235 req
->fragsize
, obt
->fragsize
);
239 static int oss_open (int in
, struct oss_params
*req
,
240 struct oss_params
*obt
, int *pfd
)
243 #ifdef OSS_GETVERSION
246 int oflags
= conf
.exclusive
? O_EXCL
: 0;
247 audio_buf_info abinfo
;
248 int fmt
, freq
, nchannels
;
249 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
250 const char *typ
= in
? "ADC" : "DAC";
252 /* Kludge needed to have working mmap on Linux */
253 oflags
|= conf
.try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
255 fd
= open (dspname
, oflags
| O_NONBLOCK
);
257 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
262 nchannels
= req
->nchannels
;
265 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
266 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
270 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
271 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
276 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
277 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
281 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
282 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
286 #ifdef OSS_GETVERSION
287 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
288 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
293 dolog ("OSS version = %#x\n", version
);
297 #ifdef SNDCTL_DSP_POLICY
299 #ifdef OSS_GETVERSION
300 && version
>= 0x040000
306 int policy
= conf
.policy
;
307 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
308 oss_logerr2 (errno
, typ
, "Failed to set timing policy to %d\n",
316 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
317 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
318 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
319 req
->nfrags
, req
->fragsize
);
324 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
325 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
329 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
330 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
331 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
336 obt
->nchannels
= nchannels
;
338 obt
->nfrags
= abinfo
.fragstotal
;
339 obt
->fragsize
= abinfo
.fragsize
;
342 #ifdef DEBUG_MISMATCHES
343 if ((req
->fmt
!= obt
->fmt
) ||
344 (req
->nchannels
!= obt
->nchannels
) ||
345 (req
->freq
!= obt
->freq
) ||
346 (req
->fragsize
!= obt
->fragsize
) ||
347 (req
->nfrags
!= obt
->nfrags
)) {
348 dolog ("Audio parameters mismatch\n");
349 oss_dump_info (req
, obt
);
354 oss_dump_info (req
, obt
);
359 oss_anal_close (&fd
);
363 static void oss_write_pending (OSSVoiceOut
*oss
)
365 HWVoiceOut
*hw
= &oss
->hw
;
371 while (oss
->pending
) {
373 ssize_t bytes_written
;
374 int samples_till_end
= hw
->samples
- oss
->wpos
;
375 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
376 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
377 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
379 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
380 if (bytes_written
< 0) {
381 if (errno
!= EAGAIN
) {
382 oss_logerr (errno
, "failed to write %d bytes\n",
388 if (bytes_written
& hw
->info
.align
) {
389 dolog ("misaligned write asked for %d, but got %zd\n",
390 bytes_to_write
, bytes_written
);
394 samples_written
= bytes_written
>> hw
->info
.shift
;
395 oss
->pending
-= samples_written
;
396 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
397 if (bytes_written
- bytes_to_write
) {
403 static int oss_run_out (HWVoiceOut
*hw
, int live
)
405 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
407 struct audio_buf_info abinfo
;
408 struct count_info cntinfo
;
411 bufsize
= hw
->samples
<< hw
->info
.shift
;
416 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
418 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
422 pos
= hw
->rpos
<< hw
->info
.shift
;
423 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
424 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
427 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
429 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
433 if (abinfo
.bytes
> bufsize
) {
435 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
436 "please report your OS/audio hw to av1474@comtv.ru\n",
437 abinfo
.bytes
, bufsize
);
439 abinfo
.bytes
= bufsize
;
442 if (abinfo
.bytes
< 0) {
444 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
445 abinfo
.bytes
, bufsize
);
450 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
456 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
457 oss
->pending
+= decr
;
458 oss_write_pending (oss
);
463 static void oss_fini_out (HWVoiceOut
*hw
)
466 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
468 ldebug ("oss_fini\n");
469 oss_anal_close (&oss
->fd
);
473 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
475 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
476 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
480 qemu_free (oss
->pcm_buf
);
486 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
488 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
489 struct oss_params req
, obt
;
493 audfmt_e effective_fmt
;
494 struct audsettings obt_as
;
498 req
.fmt
= aud_to_ossfmt (as
->fmt
);
500 req
.nchannels
= as
->nchannels
;
501 req
.fragsize
= conf
.fragsize
;
502 req
.nfrags
= conf
.nfrags
;
504 if (oss_open (0, &req
, &obt
, &fd
)) {
508 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
510 oss_anal_close (&fd
);
514 obt_as
.freq
= obt
.freq
;
515 obt_as
.nchannels
= obt
.nchannels
;
516 obt_as
.fmt
= effective_fmt
;
517 obt_as
.endianness
= endianness
;
519 audio_pcm_init_info (&hw
->info
, &obt_as
);
520 oss
->nfrags
= obt
.nfrags
;
521 oss
->fragsize
= obt
.fragsize
;
523 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
524 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
525 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
528 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
532 oss
->pcm_buf
= mmap (
534 hw
->samples
<< hw
->info
.shift
,
535 PROT_READ
| PROT_WRITE
,
540 if (oss
->pcm_buf
== MAP_FAILED
) {
541 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
542 hw
->samples
<< hw
->info
.shift
);
547 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
548 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
551 trig
= PCM_ENABLE_OUTPUT
;
552 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
555 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
564 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
566 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
567 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
574 oss
->pcm_buf
= audio_calloc (
581 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
585 oss_anal_close (&fd
);
594 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
597 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
606 poll_mode
= va_arg (ap
, int);
609 ldebug ("enabling voice\n");
610 if (poll_mode
&& oss_poll_out (hw
)) {
613 hw
->poll_mode
= poll_mode
;
619 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
620 trig
= PCM_ENABLE_OUTPUT
;
621 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
624 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
633 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
641 ldebug ("disabling voice\n");
643 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
644 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
652 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
654 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
655 struct oss_params req
, obt
;
659 audfmt_e effective_fmt
;
660 struct audsettings obt_as
;
664 req
.fmt
= aud_to_ossfmt (as
->fmt
);
666 req
.nchannels
= as
->nchannels
;
667 req
.fragsize
= conf
.fragsize
;
668 req
.nfrags
= conf
.nfrags
;
669 if (oss_open (1, &req
, &obt
, &fd
)) {
673 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
675 oss_anal_close (&fd
);
679 obt_as
.freq
= obt
.freq
;
680 obt_as
.nchannels
= obt
.nchannels
;
681 obt_as
.fmt
= effective_fmt
;
682 obt_as
.endianness
= endianness
;
684 audio_pcm_init_info (&hw
->info
, &obt_as
);
685 oss
->nfrags
= obt
.nfrags
;
686 oss
->fragsize
= obt
.fragsize
;
688 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
689 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
690 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
693 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
694 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
696 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
697 hw
->samples
, 1 << hw
->info
.shift
);
698 oss_anal_close (&fd
);
706 static void oss_fini_in (HWVoiceIn
*hw
)
708 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
710 oss_anal_close (&oss
->fd
);
713 qemu_free (oss
->pcm_buf
);
718 static int oss_run_in (HWVoiceIn
*hw
)
720 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
721 int hwshift
= hw
->info
.shift
;
723 int live
= audio_pcm_hw_get_live_in (hw
);
724 int dead
= hw
->samples
- live
;
725 size_t read_samples
= 0;
730 { .add
= hw
->wpos
, .len
= 0 },
731 { .add
= 0, .len
= 0 }
738 if (hw
->wpos
+ dead
> hw
->samples
) {
739 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
740 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
743 bufs
[0].len
= dead
<< hwshift
;
746 for (i
= 0; i
< 2; ++i
) {
750 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
751 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
754 if (nread
& hw
->info
.align
) {
755 dolog ("warning: Misaligned read %zd (requested %d), "
756 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
759 read_samples
+= nread
>> hwshift
;
760 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
,
764 if (bufs
[i
].len
- nread
) {
773 "Failed to read %d bytes of audio (to %p)\n",
784 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
788 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
790 return audio_pcm_sw_read (sw
, buf
, size
);
793 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
795 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
804 poll_mode
= va_arg (ap
, int);
807 if (poll_mode
&& oss_poll_in (hw
)) {
810 hw
->poll_mode
= poll_mode
;
817 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
824 static void *oss_audio_init (void)
829 static void oss_audio_fini (void *opaque
)
834 static struct audio_option oss_options
[] = {
838 .valp
= &conf
.fragsize
,
839 .descr
= "Fragment size in bytes"
844 .valp
= &conf
.nfrags
,
845 .descr
= "Number of fragments"
850 .valp
= &conf
.try_mmap
,
851 .descr
= "Try using memory mapped access"
856 .valp
= &conf
.devpath_out
,
857 .descr
= "Path to DAC device"
862 .valp
= &conf
.devpath_in
,
863 .descr
= "Path to ADC device"
868 .valp
= &conf
.exclusive
,
869 .descr
= "Open device in exclusive mode (vmix wont work)"
871 #ifdef SNDCTL_DSP_POLICY
875 .valp
= &conf
.policy
,
876 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
883 .descr
= "Turn on some debugging messages"
885 { /* End of list */ }
888 static struct audio_pcm_ops oss_pcm_ops
= {
889 .init_out
= oss_init_out
,
890 .fini_out
= oss_fini_out
,
891 .run_out
= oss_run_out
,
893 .ctl_out
= oss_ctl_out
,
895 .init_in
= oss_init_in
,
896 .fini_in
= oss_fini_in
,
897 .run_in
= oss_run_in
,
902 struct audio_driver oss_audio_driver
= {
904 .descr
= "OSS http://www.opensound.com",
905 .options
= oss_options
,
906 .init
= oss_audio_init
,
907 .fini
= oss_audio_fini
,
908 .pcm_ops
= &oss_pcm_ops
,
910 .max_voices_out
= INT_MAX
,
911 .max_voices_in
= INT_MAX
,
912 .voice_size_out
= sizeof (OSSVoiceOut
),
913 .voice_size_in
= sizeof (OSSVoiceIn
)