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
{
50 typedef struct OSSVoiceIn
{
66 static void G_GNUC_PRINTF (2, 3) oss_logerr (int err
, const char *fmt
, ...)
71 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
74 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
77 static void G_GNUC_PRINTF (3, 4) oss_logerr2 (
86 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
89 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
92 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
95 static void oss_anal_close (int *fdp
)
99 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
102 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
107 static void oss_helper_poll_out (void *opaque
)
109 AudioState
*s
= opaque
;
110 audio_run(s
, "oss_poll_out");
113 static void oss_helper_poll_in (void *opaque
)
115 AudioState
*s
= opaque
;
116 audio_run(s
, "oss_poll_in");
119 static void oss_poll_out (HWVoiceOut
*hw
)
121 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
123 qemu_set_fd_handler(oss
->fd
, NULL
, oss_helper_poll_out
, hw
->s
);
126 static void oss_poll_in (HWVoiceIn
*hw
)
128 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
130 qemu_set_fd_handler(oss
->fd
, oss_helper_poll_in
, NULL
, hw
->s
);
133 static int aud_to_ossfmt (AudioFormat fmt
, int endianness
)
136 case AUDIO_FORMAT_S8
:
139 case AUDIO_FORMAT_U8
:
142 case AUDIO_FORMAT_S16
:
149 case AUDIO_FORMAT_U16
:
157 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
165 static int oss_to_audfmt (int ossfmt
, AudioFormat
*fmt
, int *endianness
)
170 *fmt
= AUDIO_FORMAT_S8
;
175 *fmt
= AUDIO_FORMAT_U8
;
180 *fmt
= AUDIO_FORMAT_S16
;
185 *fmt
= AUDIO_FORMAT_U16
;
190 *fmt
= AUDIO_FORMAT_S16
;
195 *fmt
= AUDIO_FORMAT_U16
;
199 dolog ("Unrecognized audio format %d\n", ossfmt
);
206 #if defined DEBUG_MISMATCHES || defined DEBUG
207 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
209 dolog ("parameter | requested value | obtained value\n");
210 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
211 dolog ("channels | %10d | %10d\n",
212 req
->nchannels
, obt
->nchannels
);
213 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
214 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
215 dolog ("fragsize | %10d | %10d\n",
216 req
->fragsize
, obt
->fragsize
);
220 #ifdef USE_DSP_POLICY
221 static int oss_get_version (int fd
, int *version
, const char *typ
)
223 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
224 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
226 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
227 * since 7.x, but currently only on the mixer device (or in
228 * the Linuxolator), and in the native version that part of
229 * the code is in fact never reached so the ioctl fails anyway.
230 * Until this is fixed, just check the errno and if its what
231 * FreeBSD's sound drivers return atm assume they are new enough.
233 if (errno
== EINVAL
) {
238 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
245 static int oss_open(int in
, struct oss_params
*req
, audsettings
*as
,
246 struct oss_params
*obt
, int *pfd
, Audiodev
*dev
)
248 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
249 AudiodevOssPerDirectionOptions
*opdo
= in
? oopts
->in
: oopts
->out
;
251 int oflags
= (oopts
->has_exclusive
&& oopts
->exclusive
) ? O_EXCL
: 0;
252 audio_buf_info abinfo
;
253 int fmt
, freq
, nchannels
;
255 const char *dspname
= opdo
->has_dev
? opdo
->dev
: "/dev/dsp";
256 const char *typ
= in
? "ADC" : "DAC";
257 #ifdef USE_DSP_POLICY
258 int policy
= oopts
->has_dsp_policy
? oopts
->dsp_policy
: 5;
261 /* Kludge needed to have working mmap on Linux */
262 oflags
|= (oopts
->has_try_mmap
&& oopts
->try_mmap
) ?
263 O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
265 fd
= open (dspname
, oflags
| O_NONBLOCK
);
267 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
272 nchannels
= req
->nchannels
;
274 req
->nfrags
= opdo
->has_buffer_count
? opdo
->buffer_count
: 4;
275 req
->fragsize
= audio_buffer_bytes(
276 qapi_AudiodevOssPerDirectionOptions_base(opdo
), as
, 23220);
278 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
279 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
283 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
284 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
289 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
290 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
294 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
295 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
299 #ifdef USE_DSP_POLICY
303 if (!oss_get_version (fd
, &version
, typ
)) {
304 trace_oss_version(version
);
306 if (version
>= 0x040000) {
307 int policy2
= policy
;
308 if (ioctl(fd
, SNDCTL_DSP_POLICY
, &policy2
)) {
309 oss_logerr2 (errno
, typ
,
310 "Failed to set timing policy to %d\n",
321 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
322 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
323 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
324 req
->nfrags
, req
->fragsize
);
329 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
330 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
334 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
335 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
336 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
341 obt
->nchannels
= nchannels
;
343 obt
->nfrags
= abinfo
.fragstotal
;
344 obt
->fragsize
= abinfo
.fragsize
;
347 #ifdef DEBUG_MISMATCHES
348 if ((req
->fmt
!= obt
->fmt
) ||
349 (req
->nchannels
!= obt
->nchannels
) ||
350 (req
->freq
!= obt
->freq
) ||
351 (req
->fragsize
!= obt
->fragsize
) ||
352 (req
->nfrags
!= obt
->nfrags
)) {
353 dolog ("Audio parameters mismatch\n");
354 oss_dump_info (req
, obt
);
359 oss_dump_info (req
, obt
);
364 oss_anal_close (&fd
);
368 static size_t oss_get_available_bytes(OSSVoiceOut
*oss
)
371 struct count_info cntinfo
;
372 assert(oss
->mmapped
);
374 err
= ioctl(oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
376 oss_logerr(errno
, "SNDCTL_DSP_GETOPTR failed\n");
380 return audio_ring_dist(cntinfo
.ptr
, oss
->hw
.pos_emul
, oss
->hw
.size_emul
);
383 static void oss_run_buffer_out(HWVoiceOut
*hw
)
385 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
388 audio_generic_run_buffer_out(hw
);
392 static size_t oss_buffer_get_free(HWVoiceOut
*hw
)
394 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
397 return oss_get_available_bytes(oss
);
399 return audio_generic_buffer_get_free(hw
);
403 static void *oss_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
405 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
408 *size
= hw
->size_emul
- hw
->pos_emul
;
409 return hw
->buf_emul
+ hw
->pos_emul
;
411 return audio_generic_get_buffer_out(hw
, size
);
415 static size_t oss_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
417 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
419 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&& size
< hw
->size_emul
);
421 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
424 return audio_generic_put_buffer_out(hw
, buf
, size
);
428 static size_t oss_write(HWVoiceOut
*hw
, void *buf
, size_t len
)
430 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
435 len
= MIN(len
, oss_get_available_bytes(oss
));
439 size_t to_copy
= MIN(len
, hw
->size_emul
- hw
->pos_emul
);
440 memcpy(hw
->buf_emul
+ hw
->pos_emul
, buf
, to_copy
);
442 hw
->pos_emul
= (hw
->pos_emul
+ to_copy
) % hw
->size_emul
;
451 ssize_t bytes_written
;
452 void *pcm
= advance(buf
, pos
);
454 bytes_written
= write(oss
->fd
, pcm
, len
);
455 if (bytes_written
< 0) {
456 if (errno
!= EAGAIN
) {
457 oss_logerr(errno
, "failed to write %zu bytes\n",
463 pos
+= bytes_written
;
464 if (bytes_written
< len
) {
467 len
-= bytes_written
;
472 static void oss_fini_out (HWVoiceOut
*hw
)
475 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
477 ldebug ("oss_fini\n");
478 oss_anal_close (&oss
->fd
);
480 if (oss
->mmapped
&& hw
->buf_emul
) {
481 err
= munmap(hw
->buf_emul
, hw
->size_emul
);
483 oss_logerr(errno
, "Failed to unmap buffer %p, size %zu\n",
484 hw
->buf_emul
, hw
->size_emul
);
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
.bytes_per_frame
) {
529 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
530 obt
.nfrags
* obt
.fragsize
, hw
->info
.bytes_per_frame
);
533 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) / hw
->info
.bytes_per_frame
;
536 if (oopts
->has_try_mmap
&& oopts
->try_mmap
) {
537 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
541 PROT_READ
| PROT_WRITE
,
546 if (hw
->buf_emul
== MAP_FAILED
) {
547 oss_logerr(errno
, "Failed to map %zu bytes of DAC\n",
553 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
554 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"
568 err
= munmap(hw
->buf_emul
, hw
->size_emul
);
570 oss_logerr(errno
, "Failed to unmap buffer %p size %zu\n",
571 hw
->buf_emul
, hw
->size_emul
);
583 static void oss_enable_out(HWVoiceOut
*hw
, bool enable
)
586 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
587 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
590 hw
->poll_mode
= opdo
->try_poll
;
592 ldebug("enabling voice\n");
601 audio_pcm_info_clear_buf(&hw
->info
, hw
->buf_emul
, hw
->samples
);
602 trig
= PCM_ENABLE_OUTPUT
;
603 if (ioctl(oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
605 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n");
610 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
618 ldebug ("disabling voice\n");
620 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
621 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
627 static int oss_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
629 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
630 struct oss_params req
, obt
;
634 AudioFormat effective_fmt
;
635 struct audsettings obt_as
;
636 Audiodev
*dev
= drv_opaque
;
640 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
642 req
.nchannels
= as
->nchannels
;
643 if (oss_open(1, &req
, as
, &obt
, &fd
, dev
)) {
647 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
649 oss_anal_close (&fd
);
653 obt_as
.freq
= obt
.freq
;
654 obt_as
.nchannels
= obt
.nchannels
;
655 obt_as
.fmt
= effective_fmt
;
656 obt_as
.endianness
= endianness
;
658 audio_pcm_init_info (&hw
->info
, &obt_as
);
659 oss
->nfrags
= obt
.nfrags
;
660 oss
->fragsize
= obt
.fragsize
;
662 if (obt
.nfrags
* obt
.fragsize
% hw
->info
.bytes_per_frame
) {
663 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
664 obt
.nfrags
* obt
.fragsize
, hw
->info
.bytes_per_frame
);
667 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) / hw
->info
.bytes_per_frame
;
674 static void oss_fini_in (HWVoiceIn
*hw
)
676 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
678 oss_anal_close (&oss
->fd
);
681 static size_t oss_read(HWVoiceIn
*hw
, void *buf
, size_t len
)
683 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
689 void *dst
= advance(buf
, pos
);
690 nread
= read(oss
->fd
, dst
, len
);
698 oss_logerr(errno
, "Failed to read %zu bytes of audio (to %p)\n",
712 static void oss_enable_in(HWVoiceIn
*hw
, bool enable
)
714 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
715 AudiodevOssPerDirectionOptions
*opdo
= oss
->dev
->u
.oss
.out
;
718 hw
->poll_mode
= opdo
->try_poll
;
725 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
731 static void oss_init_per_direction(AudiodevOssPerDirectionOptions
*opdo
)
733 if (!opdo
->has_try_poll
) {
734 opdo
->try_poll
= true;
735 opdo
->has_try_poll
= true;
739 static void *oss_audio_init(Audiodev
*dev
)
741 AudiodevOssOptions
*oopts
;
742 assert(dev
->driver
== AUDIODEV_DRIVER_OSS
);
745 oss_init_per_direction(oopts
->in
);
746 oss_init_per_direction(oopts
->out
);
748 if (access(oopts
->in
->has_dev
? oopts
->in
->dev
: "/dev/dsp",
750 access(oopts
->out
->has_dev
? oopts
->out
->dev
: "/dev/dsp",
757 static void oss_audio_fini (void *opaque
)
761 static struct audio_pcm_ops oss_pcm_ops
= {
762 .init_out
= oss_init_out
,
763 .fini_out
= oss_fini_out
,
765 .buffer_get_free
= oss_buffer_get_free
,
766 .run_buffer_out
= oss_run_buffer_out
,
767 .get_buffer_out
= oss_get_buffer_out
,
768 .put_buffer_out
= oss_put_buffer_out
,
769 .enable_out
= oss_enable_out
,
771 .init_in
= oss_init_in
,
772 .fini_in
= oss_fini_in
,
774 .run_buffer_in
= audio_generic_run_buffer_in
,
775 .enable_in
= oss_enable_in
778 static struct audio_driver oss_audio_driver
= {
780 .descr
= "OSS http://www.opensound.com",
781 .init
= oss_audio_init
,
782 .fini
= oss_audio_fini
,
783 .pcm_ops
= &oss_pcm_ops
,
785 .max_voices_out
= INT_MAX
,
786 .max_voices_in
= INT_MAX
,
787 .voice_size_out
= sizeof (OSSVoiceOut
),
788 .voice_size_in
= sizeof (OSSVoiceIn
)
791 static void register_audio_oss(void)
793 audio_driver_register(&oss_audio_driver
);
795 type_init(register_audio_oss
);