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"
31 #include "qapi/error.h"
35 #define AUDIO_CAP "oss"
36 #include "audio_int.h"
38 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
39 #define USE_DSP_POLICY
42 typedef struct OSSVoiceOut
{
51 typedef struct OSSVoiceIn
{
67 static void G_GNUC_PRINTF (2, 3) oss_logerr (int err
, const char *fmt
, ...)
72 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
75 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
78 static void G_GNUC_PRINTF (3, 4) oss_logerr2 (
87 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
90 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
93 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
96 static void oss_anal_close (int *fdp
)
100 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
103 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
108 static void oss_helper_poll_out (void *opaque
)
110 AudioState
*s
= opaque
;
111 audio_run(s
, "oss_poll_out");
114 static void oss_helper_poll_in (void *opaque
)
116 AudioState
*s
= opaque
;
117 audio_run(s
, "oss_poll_in");
120 static void oss_poll_out (HWVoiceOut
*hw
)
122 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
124 qemu_set_fd_handler(oss
->fd
, NULL
, oss_helper_poll_out
, hw
->s
);
127 static void oss_poll_in (HWVoiceIn
*hw
)
129 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
131 qemu_set_fd_handler(oss
->fd
, oss_helper_poll_in
, NULL
, hw
->s
);
134 static int aud_to_ossfmt (AudioFormat fmt
, int endianness
)
137 case AUDIO_FORMAT_S8
:
140 case AUDIO_FORMAT_U8
:
143 case AUDIO_FORMAT_S16
:
150 case AUDIO_FORMAT_U16
:
158 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
166 static int oss_to_audfmt (int ossfmt
, AudioFormat
*fmt
, int *endianness
)
171 *fmt
= AUDIO_FORMAT_S8
;
176 *fmt
= AUDIO_FORMAT_U8
;
181 *fmt
= AUDIO_FORMAT_S16
;
186 *fmt
= AUDIO_FORMAT_U16
;
191 *fmt
= AUDIO_FORMAT_S16
;
196 *fmt
= AUDIO_FORMAT_U16
;
200 dolog ("Unrecognized audio format %d\n", ossfmt
);
207 #if defined DEBUG_MISMATCHES || defined DEBUG
208 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
210 dolog ("parameter | requested value | obtained value\n");
211 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
212 dolog ("channels | %10d | %10d\n",
213 req
->nchannels
, obt
->nchannels
);
214 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
215 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
216 dolog ("fragsize | %10d | %10d\n",
217 req
->fragsize
, obt
->fragsize
);
221 #ifdef USE_DSP_POLICY
222 static int oss_get_version (int fd
, int *version
, const char *typ
)
224 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
225 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
227 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
228 * since 7.x, but currently only on the mixer device (or in
229 * the Linuxolator), and in the native version that part of
230 * the code is in fact never reached so the ioctl fails anyway.
231 * Until this is fixed, just check the errno and if its what
232 * FreeBSD's sound drivers return atm assume they are new enough.
234 if (errno
== EINVAL
) {
239 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
246 static int oss_open(int in
, struct oss_params
*req
, audsettings
*as
,
247 struct oss_params
*obt
, int *pfd
, Audiodev
*dev
)
249 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
250 AudiodevOssPerDirectionOptions
*opdo
= in
? oopts
->in
: oopts
->out
;
252 int oflags
= (oopts
->has_exclusive
&& oopts
->exclusive
) ? O_EXCL
: 0;
253 audio_buf_info abinfo
;
254 int fmt
, freq
, nchannels
;
256 const char *dspname
= opdo
->dev
?: "/dev/dsp";
257 const char *typ
= in
? "ADC" : "DAC";
258 #ifdef USE_DSP_POLICY
259 int policy
= oopts
->has_dsp_policy
? oopts
->dsp_policy
: 5;
262 /* Kludge needed to have working mmap on Linux */
263 oflags
|= (oopts
->has_try_mmap
&& oopts
->try_mmap
) ?
264 O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
266 fd
= open (dspname
, oflags
| O_NONBLOCK
);
268 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
273 nchannels
= req
->nchannels
;
275 req
->nfrags
= opdo
->has_buffer_count
? opdo
->buffer_count
: 4;
276 req
->fragsize
= audio_buffer_bytes(
277 qapi_AudiodevOssPerDirectionOptions_base(opdo
), as
, 23220);
279 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
280 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
284 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
285 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
290 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
291 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
295 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
296 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
300 #ifdef USE_DSP_POLICY
304 if (!oss_get_version (fd
, &version
, typ
)) {
305 trace_oss_version(version
);
307 if (version
>= 0x040000) {
308 int policy2
= policy
;
309 if (ioctl(fd
, SNDCTL_DSP_POLICY
, &policy2
)) {
310 oss_logerr2 (errno
, typ
,
311 "Failed to set timing policy to %d\n",
322 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
323 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
324 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
325 req
->nfrags
, req
->fragsize
);
330 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
331 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
335 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
336 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
337 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
342 obt
->nchannels
= nchannels
;
344 obt
->nfrags
= abinfo
.fragstotal
;
345 obt
->fragsize
= abinfo
.fragsize
;
348 #ifdef DEBUG_MISMATCHES
349 if ((req
->fmt
!= obt
->fmt
) ||
350 (req
->nchannels
!= obt
->nchannels
) ||
351 (req
->freq
!= obt
->freq
) ||
352 (req
->fragsize
!= obt
->fragsize
) ||
353 (req
->nfrags
!= obt
->nfrags
)) {
354 dolog ("Audio parameters mismatch\n");
355 oss_dump_info (req
, obt
);
360 oss_dump_info (req
, obt
);
365 oss_anal_close (&fd
);
369 static size_t oss_get_available_bytes(OSSVoiceOut
*oss
)
372 struct count_info cntinfo
;
373 assert(oss
->mmapped
);
375 err
= ioctl(oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
377 oss_logerr(errno
, "SNDCTL_DSP_GETOPTR failed\n");
381 return audio_ring_dist(cntinfo
.ptr
, oss
->hw
.pos_emul
, oss
->hw
.size_emul
);
384 static void oss_run_buffer_out(HWVoiceOut
*hw
)
386 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
389 audio_generic_run_buffer_out(hw
);
393 static size_t oss_buffer_get_free(HWVoiceOut
*hw
)
395 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
398 return oss_get_available_bytes(oss
);
400 return audio_generic_buffer_get_free(hw
);
404 static void *oss_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
406 OSSVoiceOut
*oss
= (OSSVoiceOut
*)hw
;
409 *size
= hw
->size_emul
- hw
->pos_emul
;
410 return hw
->buf_emul
+ hw
->pos_emul
;
412 return audio_generic_get_buffer_out(hw
, size
);
416 static size_t oss_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
418 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
420 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&& size
< hw
->size_emul
);
422 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
425 return audio_generic_put_buffer_out(hw
, buf
, size
);
429 static size_t oss_write(HWVoiceOut
*hw
, void *buf
, size_t len
)
431 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
436 len
= MIN(len
, oss_get_available_bytes(oss
));
440 size_t to_copy
= MIN(len
, hw
->size_emul
- hw
->pos_emul
);
441 memcpy(hw
->buf_emul
+ hw
->pos_emul
, buf
, to_copy
);
443 hw
->pos_emul
= (hw
->pos_emul
+ to_copy
) % hw
->size_emul
;
452 ssize_t bytes_written
;
453 void *pcm
= advance(buf
, pos
);
455 bytes_written
= write(oss
->fd
, pcm
, len
);
456 if (bytes_written
< 0) {
457 if (errno
!= EAGAIN
) {
458 oss_logerr(errno
, "failed to write %zu bytes\n",
464 pos
+= bytes_written
;
465 if (bytes_written
< len
) {
468 len
-= bytes_written
;
473 static void oss_fini_out (HWVoiceOut
*hw
)
476 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
478 ldebug ("oss_fini\n");
479 oss_anal_close (&oss
->fd
);
481 if (oss
->mmapped
&& hw
->buf_emul
) {
482 err
= munmap(hw
->buf_emul
, hw
->size_emul
);
484 oss_logerr(errno
, "Failed to unmap buffer %p, size %zu\n",
485 hw
->buf_emul
, hw
->size_emul
);
491 static int oss_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
494 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
495 struct oss_params req
, obt
;
499 AudioFormat effective_fmt
;
500 struct audsettings obt_as
;
501 Audiodev
*dev
= drv_opaque
;
502 AudiodevOssOptions
*oopts
= &dev
->u
.oss
;
506 req
.fmt
= aud_to_ossfmt (as
->fmt
, as
->endianness
);
508 req
.nchannels
= as
->nchannels
;
510 if (oss_open(0, &req
, as
, &obt
, &fd
, dev
)) {
514 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
516 oss_anal_close (&fd
);
520 obt_as
.freq
= obt
.freq
;
521 obt_as
.nchannels
= obt
.nchannels
;
522 obt_as
.fmt
= effective_fmt
;
523 obt_as
.endianness
= endianness
;
525 audio_pcm_init_info (&hw
->info
, &obt_as
);
526 oss
->nfrags
= obt
.nfrags
;
527 oss
->fragsize
= obt
.fragsize
;
529 if (obt
.nfrags
* obt
.fragsize
% hw
->info
.bytes_per_frame
) {
530 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
531 obt
.nfrags
* obt
.fragsize
, hw
->info
.bytes_per_frame
);
534 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) / hw
->info
.bytes_per_frame
;
537 if (oopts
->has_try_mmap
&& oopts
->try_mmap
) {
538 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
542 PROT_READ
| PROT_WRITE
,
547 if (hw
->buf_emul
== MAP_FAILED
) {
548 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
, Error
**errp
)
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
->dev
?: "/dev/dsp", R_OK
| W_OK
) < 0) {
749 error_setg_errno(errp
, errno
, "%s not accessible", oopts
->in
->dev
?: "/dev/dsp");
752 if (access(oopts
->out
->dev
?: "/dev/dsp", R_OK
| W_OK
) < 0) {
753 error_setg_errno(errp
, errno
, "%s not accessible", oopts
->out
->dev
?: "/dev/dsp");
759 static void oss_audio_fini (void *opaque
)
763 static struct audio_pcm_ops oss_pcm_ops
= {
764 .init_out
= oss_init_out
,
765 .fini_out
= oss_fini_out
,
767 .buffer_get_free
= oss_buffer_get_free
,
768 .run_buffer_out
= oss_run_buffer_out
,
769 .get_buffer_out
= oss_get_buffer_out
,
770 .put_buffer_out
= oss_put_buffer_out
,
771 .enable_out
= oss_enable_out
,
773 .init_in
= oss_init_in
,
774 .fini_in
= oss_fini_in
,
776 .run_buffer_in
= audio_generic_run_buffer_in
,
777 .enable_in
= oss_enable_in
780 static struct audio_driver oss_audio_driver
= {
782 .descr
= "OSS http://www.opensound.com",
783 .init
= oss_audio_init
,
784 .fini
= oss_audio_fini
,
785 .pcm_ops
= &oss_pcm_ops
,
786 .max_voices_out
= INT_MAX
,
787 .max_voices_in
= INT_MAX
,
788 .voice_size_out
= sizeof (OSSVoiceOut
),
789 .voice_size_in
= sizeof (OSSVoiceIn
)
792 static void register_audio_oss(void)
794 audio_driver_register(&oss_audio_driver
);
796 type_init(register_audio_oss
);