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
{
50 typedef struct OSSVoiceIn
{
62 const char *devpath_out
;
63 const char *devpath_in
;
71 .devpath_out
= "/dev/dsp",
72 .devpath_in
= "/dev/dsp",
86 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
91 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
94 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
97 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
106 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
109 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
112 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
115 static void oss_anal_close (int *fdp
)
119 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
122 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
127 static void oss_helper_poll_out (void *opaque
)
130 audio_run ("oss_poll_out");
133 static void oss_helper_poll_in (void *opaque
)
136 audio_run ("oss_poll_in");
139 static int oss_poll_out (HWVoiceOut
*hw
)
141 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
143 return qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
146 static int oss_poll_in (HWVoiceIn
*hw
)
148 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
150 return qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
153 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
155 return audio_pcm_sw_write (sw
, buf
, len
);
158 static int aud_to_ossfmt (audfmt_e fmt
)
174 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
182 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
216 dolog ("Unrecognized audio format %d\n", ossfmt
);
223 #if defined DEBUG_MISMATCHES || defined DEBUG
224 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
226 dolog ("parameter | requested value | obtained value\n");
227 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
228 dolog ("channels | %10d | %10d\n",
229 req
->nchannels
, obt
->nchannels
);
230 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
231 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
232 dolog ("fragsize | %10d | %10d\n",
233 req
->fragsize
, obt
->fragsize
);
237 static int oss_open (int in
, struct oss_params
*req
,
238 struct oss_params
*obt
, int *pfd
)
242 int oflags
= conf
.exclusive
? O_EXCL
: 0;
243 audio_buf_info abinfo
;
244 int fmt
, freq
, nchannels
;
245 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
246 const char *typ
= in
? "ADC" : "DAC";
248 /* Kludge needed to have working mmap on Linux */
249 oflags
|= conf
.try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
251 fd
= open (dspname
, oflags
| O_NONBLOCK
);
253 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
258 nchannels
= req
->nchannels
;
261 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
262 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
266 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
267 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
272 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
273 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
277 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
278 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
282 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
283 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
288 dolog ("OSS version = %#x\n", version
);
291 #ifdef SNDCTL_DSP_POLICY
292 if (conf
.policy
>= 0 && version
>= 0x040000) {
293 int policy
= conf
.policy
;
294 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
295 oss_logerr2 (errno
, typ
, "Failed to set timing policy to %d\n",
303 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
304 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
305 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
306 req
->nfrags
, req
->fragsize
);
311 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
312 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
316 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
317 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
318 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
323 obt
->nchannels
= nchannels
;
325 obt
->nfrags
= abinfo
.fragstotal
;
326 obt
->fragsize
= abinfo
.fragsize
;
329 #ifdef DEBUG_MISMATCHES
330 if ((req
->fmt
!= obt
->fmt
) ||
331 (req
->nchannels
!= obt
->nchannels
) ||
332 (req
->freq
!= obt
->freq
) ||
333 (req
->fragsize
!= obt
->fragsize
) ||
334 (req
->nfrags
!= obt
->nfrags
)) {
335 dolog ("Audio parameters mismatch\n");
336 oss_dump_info (req
, obt
);
341 oss_dump_info (req
, obt
);
346 oss_anal_close (&fd
);
350 static int oss_run_out (HWVoiceOut
*hw
)
352 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
353 int err
, rpos
, live
, decr
;
356 struct st_sample
*src
;
357 struct audio_buf_info abinfo
;
358 struct count_info cntinfo
;
361 live
= audio_pcm_hw_get_live_out (hw
);
366 bufsize
= hw
->samples
<< hw
->info
.shift
;
371 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
373 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
377 pos
= hw
->rpos
<< hw
->info
.shift
;
378 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
379 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
382 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
384 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
388 if (abinfo
.bytes
> bufsize
) {
390 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
391 "please report your OS/audio hw to malc@pulsesoft.com\n",
392 abinfo
.bytes
, bufsize
);
394 abinfo
.bytes
= bufsize
;
397 if (abinfo
.bytes
< 0) {
399 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
400 abinfo
.bytes
, bufsize
);
405 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
414 int left_till_end_samples
= hw
->samples
- rpos
;
415 int convert_samples
= audio_MIN (samples
, left_till_end_samples
);
417 src
= hw
->mix_buf
+ rpos
;
418 dst
= advance (oss
->pcm_buf
, rpos
<< hw
->info
.shift
);
420 hw
->clip (dst
, src
, convert_samples
);
424 written
= write (oss
->fd
, dst
, convert_samples
<< hw
->info
.shift
);
425 /* XXX: follow errno recommendations ? */
429 "Failed to write %d bytes of audio data from %p\n",
430 convert_samples
<< hw
->info
.shift
,
436 if (written
!= convert_samples
<< hw
->info
.shift
) {
437 int wsamples
= written
>> hw
->info
.shift
;
438 int wbytes
= wsamples
<< hw
->info
.shift
;
439 if (wbytes
!= written
) {
440 dolog ("warning: Misaligned write %d (requested %d), "
442 wbytes
, written
, hw
->info
.align
+ 1);
445 rpos
= (rpos
+ wsamples
) % hw
->samples
;
450 rpos
= (rpos
+ convert_samples
) % hw
->samples
;
451 samples
-= convert_samples
;
458 static void oss_fini_out (HWVoiceOut
*hw
)
461 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
463 ldebug ("oss_fini\n");
464 oss_anal_close (&oss
->fd
);
468 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
470 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
471 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
475 qemu_free (oss
->pcm_buf
);
481 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
483 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
484 struct oss_params req
, obt
;
488 audfmt_e effective_fmt
;
489 struct audsettings obt_as
;
493 req
.fmt
= aud_to_ossfmt (as
->fmt
);
495 req
.nchannels
= as
->nchannels
;
496 req
.fragsize
= conf
.fragsize
;
497 req
.nfrags
= conf
.nfrags
;
499 if (oss_open (0, &req
, &obt
, &fd
)) {
503 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
505 oss_anal_close (&fd
);
509 obt_as
.freq
= obt
.freq
;
510 obt_as
.nchannels
= obt
.nchannels
;
511 obt_as
.fmt
= effective_fmt
;
512 obt_as
.endianness
= endianness
;
514 audio_pcm_init_info (&hw
->info
, &obt_as
);
515 oss
->nfrags
= obt
.nfrags
;
516 oss
->fragsize
= obt
.fragsize
;
518 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
519 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
520 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
523 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
527 oss
->pcm_buf
= mmap (
529 hw
->samples
<< hw
->info
.shift
,
530 PROT_READ
| PROT_WRITE
,
535 if (oss
->pcm_buf
== MAP_FAILED
) {
536 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
537 hw
->samples
<< hw
->info
.shift
);
542 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
543 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
546 trig
= PCM_ENABLE_OUTPUT
;
547 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
550 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
559 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
561 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
562 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
569 oss
->pcm_buf
= audio_calloc (
576 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
580 oss_anal_close (&fd
);
589 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
594 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
597 poll_mode
= va_arg (ap
, int);
602 ldebug ("enabling voice\n");
603 if (poll_mode
&& oss_poll_out (hw
)) {
606 hw
->poll_mode
= poll_mode
;
612 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
613 trig
= PCM_ENABLE_OUTPUT
;
614 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
617 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
625 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
633 ldebug ("disabling voice\n");
635 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
636 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
644 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
646 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
647 struct oss_params req
, obt
;
651 audfmt_e effective_fmt
;
652 struct audsettings obt_as
;
656 req
.fmt
= aud_to_ossfmt (as
->fmt
);
658 req
.nchannels
= as
->nchannels
;
659 req
.fragsize
= conf
.fragsize
;
660 req
.nfrags
= conf
.nfrags
;
661 if (oss_open (1, &req
, &obt
, &fd
)) {
665 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
667 oss_anal_close (&fd
);
671 obt_as
.freq
= obt
.freq
;
672 obt_as
.nchannels
= obt
.nchannels
;
673 obt_as
.fmt
= effective_fmt
;
674 obt_as
.endianness
= endianness
;
676 audio_pcm_init_info (&hw
->info
, &obt_as
);
677 oss
->nfrags
= obt
.nfrags
;
678 oss
->fragsize
= obt
.fragsize
;
680 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
681 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
682 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
685 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
686 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
688 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
689 hw
->samples
, 1 << hw
->info
.shift
);
690 oss_anal_close (&fd
);
698 static void oss_fini_in (HWVoiceIn
*hw
)
700 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
702 oss_anal_close (&oss
->fd
);
705 qemu_free (oss
->pcm_buf
);
710 static int oss_run_in (HWVoiceIn
*hw
)
712 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
713 int hwshift
= hw
->info
.shift
;
715 int live
= audio_pcm_hw_get_live_in (hw
);
716 int dead
= hw
->samples
- live
;
717 size_t read_samples
= 0;
722 { .add
= hw
->wpos
, .len
= 0 },
723 { .add
= 0, .len
= 0 }
730 if (hw
->wpos
+ dead
> hw
->samples
) {
731 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
732 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
735 bufs
[0].len
= dead
<< hwshift
;
738 for (i
= 0; i
< 2; ++i
) {
742 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
743 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
746 if (nread
& hw
->info
.align
) {
747 dolog ("warning: Misaligned read %zd (requested %d), "
748 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
751 read_samples
+= nread
>> hwshift
;
752 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
,
756 if (bufs
[i
].len
- nread
) {
765 "Failed to read %d bytes of audio (to %p)\n",
776 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
780 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
782 return audio_pcm_sw_read (sw
, buf
, size
);
785 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
789 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
792 poll_mode
= va_arg (ap
, int);
797 if (poll_mode
&& oss_poll_in (hw
)) {
800 hw
->poll_mode
= poll_mode
;
806 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
813 static void *oss_audio_init (void)
818 static void oss_audio_fini (void *opaque
)
823 static struct audio_option oss_options
[] = {
827 .valp
= &conf
.fragsize
,
828 .descr
= "Fragment size in bytes"
833 .valp
= &conf
.nfrags
,
834 .descr
= "Number of fragments"
839 .valp
= &conf
.try_mmap
,
840 .descr
= "Try using memory mapped access"
845 .valp
= &conf
.devpath_out
,
846 .descr
= "Path to DAC device"
851 .valp
= &conf
.devpath_in
,
852 .descr
= "Path to ADC device"
857 .valp
= &conf
.exclusive
,
858 .descr
= "Open device in exclusive mode (vmix wont work)"
860 #ifdef SNDCTL_DSP_POLICY
864 .valp
= &conf
.policy
,
865 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
872 .descr
= "Turn on some debugging messages"
874 { /* End of list */ }
877 static struct audio_pcm_ops oss_pcm_ops
= {
878 .init_out
= oss_init_out
,
879 .fini_out
= oss_fini_out
,
880 .run_out
= oss_run_out
,
882 .ctl_out
= oss_ctl_out
,
884 .init_in
= oss_init_in
,
885 .fini_in
= oss_fini_in
,
886 .run_in
= oss_run_in
,
891 struct audio_driver oss_audio_driver
= {
893 .descr
= "OSS http://www.opensound.com",
894 .options
= oss_options
,
895 .init
= oss_audio_init
,
896 .fini
= oss_audio_fini
,
897 .pcm_ops
= &oss_pcm_ops
,
899 .max_voices_out
= INT_MAX
,
900 .max_voices_in
= INT_MAX
,
901 .voice_size_out
= sizeof (OSSVoiceOut
),
902 .voice_size_in
= sizeof (OSSVoiceIn
)