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"
36 #define AUDIO_CAP "oss"
37 #include "audio_int.h"
39 typedef struct OSSVoiceOut
{
49 typedef struct OSSVoiceIn
{
62 const char *devpath_out
;
63 const char *devpath_in
;
69 .devpath_out
= "/dev/dsp",
70 .devpath_in
= "/dev/dsp",
82 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
87 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
90 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
93 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
102 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
105 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
108 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
111 static void oss_anal_close (int *fdp
)
113 int err
= close (*fdp
);
115 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
120 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
122 return audio_pcm_sw_write (sw
, buf
, len
);
125 static int aud_to_ossfmt (audfmt_e fmt
)
141 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
149 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
183 dolog ("Unrecognized audio format %d\n", ossfmt
);
190 #if defined DEBUG_MISMATCHES || defined DEBUG
191 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
193 dolog ("parameter | requested value | obtained value\n");
194 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
195 dolog ("channels | %10d | %10d\n",
196 req
->nchannels
, obt
->nchannels
);
197 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
198 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
199 dolog ("fragsize | %10d | %10d\n",
200 req
->fragsize
, obt
->fragsize
);
204 static int oss_open (int in
, struct oss_params
*req
,
205 struct oss_params
*obt
, int *pfd
)
209 audio_buf_info abinfo
;
210 int fmt
, freq
, nchannels
;
211 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
212 const char *typ
= in
? "ADC" : "DAC";
214 fd
= open (dspname
, (in
? O_RDONLY
: O_WRONLY
) | O_NONBLOCK
);
216 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
221 nchannels
= req
->nchannels
;
224 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
225 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
229 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
230 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
235 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
236 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
240 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
241 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
245 mmmmssss
= (req
->nfrags
<< 16) | lsbindex (req
->fragsize
);
246 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
247 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
248 req
->nfrags
, req
->fragsize
);
252 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
253 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
257 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
258 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
259 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
264 obt
->nchannels
= nchannels
;
266 obt
->nfrags
= abinfo
.fragstotal
;
267 obt
->fragsize
= abinfo
.fragsize
;
270 #ifdef DEBUG_MISMATCHES
271 if ((req
->fmt
!= obt
->fmt
) ||
272 (req
->nchannels
!= obt
->nchannels
) ||
273 (req
->freq
!= obt
->freq
) ||
274 (req
->fragsize
!= obt
->fragsize
) ||
275 (req
->nfrags
!= obt
->nfrags
)) {
276 dolog ("Audio parameters mismatch\n");
277 oss_dump_info (req
, obt
);
282 oss_dump_info (req
, obt
);
287 oss_anal_close (&fd
);
291 static int oss_run_out (HWVoiceOut
*hw
)
293 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
294 int err
, rpos
, live
, decr
;
297 struct st_sample
*src
;
298 struct audio_buf_info abinfo
;
299 struct count_info cntinfo
;
302 live
= audio_pcm_hw_get_live_out (hw
);
307 bufsize
= hw
->samples
<< hw
->info
.shift
;
312 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
314 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
318 if (cntinfo
.ptr
== oss
->old_optr
) {
319 if (abs (hw
->samples
- live
) < 64) {
320 dolog ("warning: Overrun\n");
325 if (cntinfo
.ptr
> oss
->old_optr
) {
326 bytes
= cntinfo
.ptr
- oss
->old_optr
;
329 bytes
= bufsize
+ cntinfo
.ptr
- oss
->old_optr
;
332 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
335 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
337 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
341 if (abinfo
.bytes
> bufsize
) {
343 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
344 "please report your OS/audio hw to malc@pulsesoft.com\n",
345 abinfo
.bytes
, bufsize
);
347 abinfo
.bytes
= bufsize
;
350 if (abinfo
.bytes
< 0) {
352 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
353 abinfo
.bytes
, bufsize
);
358 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
367 int left_till_end_samples
= hw
->samples
- rpos
;
368 int convert_samples
= audio_MIN (samples
, left_till_end_samples
);
370 src
= hw
->mix_buf
+ rpos
;
371 dst
= advance (oss
->pcm_buf
, rpos
<< hw
->info
.shift
);
373 hw
->clip (dst
, src
, convert_samples
);
377 written
= write (oss
->fd
, dst
, convert_samples
<< hw
->info
.shift
);
378 /* XXX: follow errno recommendations ? */
382 "Failed to write %d bytes of audio data from %p\n",
383 convert_samples
<< hw
->info
.shift
,
389 if (written
!= convert_samples
<< hw
->info
.shift
) {
390 int wsamples
= written
>> hw
->info
.shift
;
391 int wbytes
= wsamples
<< hw
->info
.shift
;
392 if (wbytes
!= written
) {
393 dolog ("warning: Misaligned write %d (requested %d), "
395 wbytes
, written
, hw
->info
.align
+ 1);
398 rpos
= (rpos
+ wsamples
) % hw
->samples
;
403 rpos
= (rpos
+ convert_samples
) % hw
->samples
;
404 samples
-= convert_samples
;
407 oss
->old_optr
= cntinfo
.ptr
;
414 static void oss_fini_out (HWVoiceOut
*hw
)
417 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
419 ldebug ("oss_fini\n");
420 oss_anal_close (&oss
->fd
);
424 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
426 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
427 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
431 qemu_free (oss
->pcm_buf
);
437 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
439 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
440 struct oss_params req
, obt
;
444 audfmt_e effective_fmt
;
445 struct audsettings obt_as
;
449 req
.fmt
= aud_to_ossfmt (as
->fmt
);
451 req
.nchannels
= as
->nchannels
;
452 req
.fragsize
= conf
.fragsize
;
453 req
.nfrags
= conf
.nfrags
;
455 if (oss_open (0, &req
, &obt
, &fd
)) {
459 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
461 oss_anal_close (&fd
);
465 obt_as
.freq
= obt
.freq
;
466 obt_as
.nchannels
= obt
.nchannels
;
467 obt_as
.fmt
= effective_fmt
;
468 obt_as
.endianness
= endianness
;
470 audio_pcm_init_info (&hw
->info
, &obt_as
);
471 oss
->nfrags
= obt
.nfrags
;
472 oss
->fragsize
= obt
.fragsize
;
474 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
475 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
476 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
479 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
483 oss
->pcm_buf
= mmap (
485 hw
->samples
<< hw
->info
.shift
,
486 PROT_READ
| PROT_WRITE
,
491 if (oss
->pcm_buf
== MAP_FAILED
) {
492 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
493 hw
->samples
<< hw
->info
.shift
);
497 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
498 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
501 trig
= PCM_ENABLE_OUTPUT
;
502 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
505 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
514 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
516 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
517 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
524 oss
->pcm_buf
= audio_calloc (
531 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
535 oss_anal_close (&fd
);
544 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
547 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
555 ldebug ("enabling voice\n");
556 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
557 trig
= PCM_ENABLE_OUTPUT
;
558 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
561 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
568 ldebug ("disabling voice\n");
570 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
571 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
579 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
581 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
582 struct oss_params req
, obt
;
586 audfmt_e effective_fmt
;
587 struct audsettings obt_as
;
591 req
.fmt
= aud_to_ossfmt (as
->fmt
);
593 req
.nchannels
= as
->nchannels
;
594 req
.fragsize
= conf
.fragsize
;
595 req
.nfrags
= conf
.nfrags
;
596 if (oss_open (1, &req
, &obt
, &fd
)) {
600 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
602 oss_anal_close (&fd
);
606 obt_as
.freq
= obt
.freq
;
607 obt_as
.nchannels
= obt
.nchannels
;
608 obt_as
.fmt
= effective_fmt
;
609 obt_as
.endianness
= endianness
;
611 audio_pcm_init_info (&hw
->info
, &obt_as
);
612 oss
->nfrags
= obt
.nfrags
;
613 oss
->fragsize
= obt
.fragsize
;
615 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
616 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
617 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
620 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
621 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
623 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
624 hw
->samples
, 1 << hw
->info
.shift
);
625 oss_anal_close (&fd
);
633 static void oss_fini_in (HWVoiceIn
*hw
)
635 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
637 oss_anal_close (&oss
->fd
);
640 qemu_free (oss
->pcm_buf
);
645 static int oss_run_in (HWVoiceIn
*hw
)
647 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
648 int hwshift
= hw
->info
.shift
;
650 int live
= audio_pcm_hw_get_live_in (hw
);
651 int dead
= hw
->samples
- live
;
652 size_t read_samples
= 0;
657 { .add
= hw
->wpos
, .len
= 0 },
658 { .add
= 0, .len
= 0 }
665 if (hw
->wpos
+ dead
> hw
->samples
) {
666 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
667 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
670 bufs
[0].len
= dead
<< hwshift
;
674 for (i
= 0; i
< 2; ++i
) {
678 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
679 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
682 if (nread
& hw
->info
.align
) {
683 dolog ("warning: Misaligned read %zd (requested %d), "
684 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
687 read_samples
+= nread
>> hwshift
;
688 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
,
692 if (bufs
[i
].len
- nread
) {
701 "Failed to read %d bytes of audio (to %p)\n",
712 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
716 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
718 return audio_pcm_sw_read (sw
, buf
, size
);
721 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
728 static void *oss_audio_init (void)
733 static void oss_audio_fini (void *opaque
)
738 static struct audio_option oss_options
[] = {
742 .valp
= &conf
.fragsize
,
743 .descr
= "Fragment size in bytes"
748 .valp
= &conf
.nfrags
,
749 .descr
= "Number of fragments"
754 .valp
= &conf
.try_mmap
,
755 .descr
= "Try using memory mapped access"
760 .valp
= &conf
.devpath_out
,
761 .descr
= "Path to DAC device"
766 .valp
= &conf
.devpath_in
,
767 .descr
= "Path to ADC device"
773 .descr
= "Turn on some debugging messages"
775 { /* End of list */ }
778 static struct audio_pcm_ops oss_pcm_ops
= {
779 .init_out
= oss_init_out
,
780 .fini_out
= oss_fini_out
,
781 .run_out
= oss_run_out
,
783 .ctl_out
= oss_ctl_out
,
785 .init_in
= oss_init_in
,
786 .fini_in
= oss_fini_in
,
787 .run_in
= oss_run_in
,
792 struct audio_driver oss_audio_driver
= {
794 .descr
= "OSS http://www.opensound.com",
795 .options
= oss_options
,
796 .init
= oss_audio_init
,
797 .fini
= oss_audio_fini
,
798 .pcm_ops
= &oss_pcm_ops
,
800 .max_voices_out
= INT_MAX
,
801 .max_voices_in
= INT_MAX
,
802 .voice_size_out
= sizeof (OSSVoiceOut
),
803 .voice_size_in
= sizeof (OSSVoiceIn
)