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
25 #include "qemu/osdep.h"
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "qemu/main-loop.h"
29 #include "qemu/module.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 OSSVoiceOut
{
53 typedef struct OSSVoiceIn
{
70 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
75 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
78 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
81 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
90 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
93 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
96 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
99 static void oss_anal_close (int *fdp
)
103 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
106 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
111 static void oss_helper_poll_out (void *opaque
)
113 AudioState
*s
= opaque
;
114 audio_run(s
, "oss_poll_out");
117 static void oss_helper_poll_in (void *opaque
)
119 AudioState
*s
= opaque
;
120 audio_run(s
, "oss_poll_in");
123 static void oss_poll_out (HWVoiceOut
*hw
)
125 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
127 qemu_set_fd_handler(oss
->fd
, NULL
, oss_helper_poll_out
, hw
->s
);
130 static void oss_poll_in (HWVoiceIn
*hw
)
132 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
134 qemu_set_fd_handler(oss
->fd
, oss_helper_poll_in
, NULL
, hw
->s
);
137 static int aud_to_ossfmt (AudioFormat fmt
, int endianness
)
140 case AUDIO_FORMAT_S8
:
143 case AUDIO_FORMAT_U8
:
146 case AUDIO_FORMAT_S16
:
154 case AUDIO_FORMAT_U16
:
163 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
171 static int oss_to_audfmt (int ossfmt
, AudioFormat
*fmt
, int *endianness
)
176 *fmt
= AUDIO_FORMAT_S8
;
181 *fmt
= AUDIO_FORMAT_U8
;
186 *fmt
= AUDIO_FORMAT_S16
;
191 *fmt
= AUDIO_FORMAT_U16
;
196 *fmt
= AUDIO_FORMAT_S16
;
201 *fmt
= AUDIO_FORMAT_U16
;
205 dolog ("Unrecognized audio format %d\n", ossfmt
);
212 #if defined DEBUG_MISMATCHES || defined DEBUG
213 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
215 dolog ("parameter | requested value | obtained value\n");
216 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
217 dolog ("channels | %10d | %10d\n",
218 req
->nchannels
, obt
->nchannels
);
219 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
220 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
221 dolog ("fragsize | %10d | %10d\n",
222 req
->fragsize
, obt
->fragsize
);
226 #ifdef USE_DSP_POLICY
227 static int oss_get_version (int fd
, int *version
, const char *typ
)
229 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
230 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
232 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
233 * since 7.x, but currently only on the mixer device (or in
234 * the Linuxolator), and in the native version that part of
235 * the code is in fact never reached so the ioctl fails anyway.
236 * Until this is fixed, just check the errno and if its what
237 * FreeBSD's sound drivers return atm assume they are new enough.
239 if (errno
== EINVAL
) {
244 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
251 static int oss_open(int in
, struct oss_params
*req
, audsettings
*as
,
252 struct oss_params
*obt
, int *pfd
, Audiodev
*dev
)
254 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
255 AudiodevOssPerDirectionOptions
*opdo
= in
? oopts
->in
: oopts
->out
;
257 int oflags
= (oopts
->has_exclusive
&& oopts
->exclusive
) ? O_EXCL
: 0;
258 audio_buf_info abinfo
;
259 int fmt
, freq
, nchannels
;
261 const char *dspname
= opdo
->has_dev
? opdo
->dev
: "/dev/dsp";
262 const char *typ
= in
? "ADC" : "DAC";
263 #ifdef USE_DSP_POLICY
264 int policy
= oopts
->has_dsp_policy
? oopts
->dsp_policy
: 5;
267 /* Kludge needed to have working mmap on Linux */
268 oflags
|= (oopts
->has_try_mmap
&& oopts
->try_mmap
) ?
269 O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
271 fd
= open (dspname
, oflags
| O_NONBLOCK
);
273 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
278 nchannels
= req
->nchannels
;
280 req
->nfrags
= opdo
->has_buffer_count
? opdo
->buffer_count
: 4;
281 req
->fragsize
= audio_buffer_bytes(
282 qapi_AudiodevOssPerDirectionOptions_base(opdo
), as
, 23220);
284 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
285 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
289 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
290 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
295 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
296 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
300 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
301 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
305 #ifdef USE_DSP_POLICY
309 if (!oss_get_version (fd
, &version
, typ
)) {
310 trace_oss_version(version
);
312 if (version
>= 0x040000) {
313 int policy2
= policy
;
314 if (ioctl(fd
, SNDCTL_DSP_POLICY
, &policy2
)) {
315 oss_logerr2 (errno
, typ
,
316 "Failed to set timing policy to %d\n",
327 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
328 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
329 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
330 req
->nfrags
, req
->fragsize
);
335 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
336 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
340 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
341 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
342 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
347 obt
->nchannels
= nchannels
;
349 obt
->nfrags
= abinfo
.fragstotal
;
350 obt
->fragsize
= abinfo
.fragsize
;
353 #ifdef DEBUG_MISMATCHES
354 if ((req
->fmt
!= obt
->fmt
) ||
355 (req
->nchannels
!= obt
->nchannels
) ||
356 (req
->freq
!= obt
->freq
) ||
357 (req
->fragsize
!= obt
->fragsize
) ||
358 (req
->nfrags
!= obt
->nfrags
)) {
359 dolog ("Audio parameters mismatch\n");
360 oss_dump_info (req
, obt
);
365 oss_dump_info (req
, obt
);
370 oss_anal_close (&fd
);
374 static void oss_write_pending (OSSVoiceOut
*oss
)
376 HWVoiceOut
*hw
= &oss
->hw
;
382 while (oss
->pending
) {
384 ssize_t bytes_written
;
385 int samples_till_end
= hw
->samples
- oss
->wpos
;
386 int samples_to_write
= MIN (oss
->pending
, samples_till_end
);
387 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
388 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
390 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
391 if (bytes_written
< 0) {
392 if (errno
!= EAGAIN
) {
393 oss_logerr (errno
, "failed to write %d bytes\n",
399 if (bytes_written
& hw
->info
.align
) {
400 dolog ("misaligned write asked for %d, but got %zd\n",
401 bytes_to_write
, bytes_written
);
405 samples_written
= bytes_written
>> hw
->info
.shift
;
406 oss
->pending
-= samples_written
;
407 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
408 if (bytes_written
- bytes_to_write
) {
414 static int oss_run_out (HWVoiceOut
*hw
, int live
)
416 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
418 struct audio_buf_info abinfo
;
419 struct count_info cntinfo
;
422 bufsize
= hw
->samples
<< hw
->info
.shift
;
427 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
429 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
433 pos
= hw
->rpos
<< hw
->info
.shift
;
434 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
435 decr
= MIN (bytes
>> hw
->info
.shift
, live
);
438 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
440 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
444 if (abinfo
.bytes
> bufsize
) {
445 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
446 abinfo
.bytes
= bufsize
;
449 if (abinfo
.bytes
< 0) {
450 trace_oss_invalid_available_size(abinfo
.bytes
, bufsize
);
454 decr
= MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
460 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
461 oss
->pending
+= decr
;
462 oss_write_pending (oss
);
467 static void oss_fini_out (HWVoiceOut
*hw
)
470 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
472 ldebug ("oss_fini\n");
473 oss_anal_close (&oss
->fd
);
477 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
479 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
480 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
484 g_free (oss
->pcm_buf
);
490 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
493 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
494 struct oss_params req
, obt
;
498 AudioFormat effective_fmt
;
499 struct audsettings obt_as
;
500 Audiodev
*dev
= drv_opaque
;
501 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
505 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
507 req
.nchannels
= as
->nchannels
;
509 if (oss_open(0, &req
, as
, &obt
, &fd
, dev
)) {
513 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
515 oss_anal_close (&fd
);
519 obt_as
.freq
= obt
.freq
;
520 obt_as
.nchannels
= obt
.nchannels
;
521 obt_as
.fmt
= effective_fmt
;
522 obt_as
.endianness
= endianness
;
524 audio_pcm_init_info (&hw
->info
, &obt_as
);
525 oss
->nfrags
= obt
.nfrags
;
526 oss
->fragsize
= obt
.fragsize
;
528 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
529 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
530 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
533 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
536 if (oopts
->has_try_mmap
&& oopts
->try_mmap
) {
537 oss
->pcm_buf
= mmap (
539 hw
->samples
<< hw
->info
.shift
,
540 PROT_READ
| PROT_WRITE
,
545 if (oss
->pcm_buf
== MAP_FAILED
) {
546 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
547 hw
->samples
<< hw
->info
.shift
);
552 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
553 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
556 trig
= PCM_ENABLE_OUTPUT
;
557 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
560 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
569 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
571 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
572 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
579 oss
->pcm_buf
= audio_calloc(__func__
,
581 1 << hw
->info
.shift
);
584 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
588 oss_anal_close (&fd
);
598 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
601 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
602 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
607 bool poll_mode
= opdo
->try_poll
;
609 ldebug ("enabling voice\n");
614 hw
->poll_mode
= poll_mode
;
620 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
621 trig
= PCM_ENABLE_OUTPUT
;
622 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
625 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
634 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
642 ldebug ("disabling voice\n");
644 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
645 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
653 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
655 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
656 struct oss_params req
, obt
;
660 AudioFormat effective_fmt
;
661 struct audsettings obt_as
;
662 Audiodev
*dev
= drv_opaque
;
666 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
668 req
.nchannels
= as
->nchannels
;
669 if (oss_open(1, &req
, as
, &obt
, &fd
, dev
)) {
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(__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
);
707 static void oss_fini_in (HWVoiceIn
*hw
)
709 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
711 oss_anal_close (&oss
->fd
);
713 g_free(oss
->pcm_buf
);
717 static int oss_run_in (HWVoiceIn
*hw
)
719 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
720 int hwshift
= hw
->info
.shift
;
722 int live
= audio_pcm_hw_get_live_in (hw
);
723 int dead
= hw
->samples
- live
;
724 size_t read_samples
= 0;
729 { .add
= hw
->wpos
, .len
= 0 },
730 { .add
= 0, .len
= 0 }
737 if (hw
->wpos
+ dead
> hw
->samples
) {
738 bufs
[0].len
= (hw
->samples
- hw
->wpos
) << hwshift
;
739 bufs
[1].len
= (dead
- (hw
->samples
- hw
->wpos
)) << hwshift
;
742 bufs
[0].len
= dead
<< hwshift
;
745 for (i
= 0; i
< 2; ++i
) {
749 void *p
= advance (oss
->pcm_buf
, bufs
[i
].add
<< hwshift
);
750 nread
= read (oss
->fd
, p
, bufs
[i
].len
);
753 if (nread
& hw
->info
.align
) {
754 dolog ("warning: Misaligned read %zd (requested %d), "
755 "alignment %d\n", nread
, bufs
[i
].add
<< hwshift
,
758 read_samples
+= nread
>> hwshift
;
759 hw
->conv (hw
->conv_buf
+ bufs
[i
].add
, p
, nread
>> hwshift
);
762 if (bufs
[i
].len
- nread
) {
771 "Failed to read %d bytes of audio (to %p)\n",
782 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
786 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
788 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
789 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
794 bool poll_mode
= opdo
->try_poll
;
800 hw
->poll_mode
= poll_mode
;
807 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
814 static void oss_init_per_direction(AudiodevOssPerDirectionOptions
*opdo
)
816 if (!opdo
->has_try_poll
) {
817 opdo
->try_poll
= true;
818 opdo
->has_try_poll
= true;
822 static void *oss_audio_init(Audiodev
*dev
)
824 AudiodevOssOptions
*oopts
;
825 assert(dev
->driver
== AUDIODEV_DRIVER_OSS
);
828 oss_init_per_direction(oopts
->in
);
829 oss_init_per_direction(oopts
->out
);
831 if (access(oopts
->in
->has_dev
? oopts
->in
->dev
: "/dev/dsp",
833 access(oopts
->out
->has_dev
? oopts
->out
->dev
: "/dev/dsp",
840 static void oss_audio_fini (void *opaque
)
844 static struct audio_pcm_ops oss_pcm_ops
= {
845 .init_out
= oss_init_out
,
846 .fini_out
= oss_fini_out
,
847 .run_out
= oss_run_out
,
848 .ctl_out
= oss_ctl_out
,
850 .init_in
= oss_init_in
,
851 .fini_in
= oss_fini_in
,
852 .run_in
= oss_run_in
,
856 static struct audio_driver oss_audio_driver
= {
858 .descr
= "OSS http://www.opensound.com",
859 .init
= oss_audio_init
,
860 .fini
= oss_audio_fini
,
861 .pcm_ops
= &oss_pcm_ops
,
863 .max_voices_out
= INT_MAX
,
864 .max_voices_in
= INT_MAX
,
865 .voice_size_out
= sizeof (OSSVoiceOut
),
866 .voice_size_in
= sizeof (OSSVoiceIn
)
869 static void register_audio_oss(void)
871 audio_driver_register(&oss_audio_driver
);
873 type_init(register_audio_oss
);