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 #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
42 #define USE_DSP_POLICY
45 typedef struct OSSVoiceOut
{
56 typedef struct OSSVoiceIn
{
68 const char *devpath_out
;
69 const char *devpath_in
;
77 .devpath_out
= "/dev/dsp",
78 .devpath_in
= "/dev/dsp",
92 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err
, const char *fmt
, ...)
97 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
100 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
103 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
112 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
115 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
118 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
121 static void oss_anal_close (int *fdp
)
125 qemu_set_fd_handler (*fdp
, NULL
, NULL
, NULL
);
128 oss_logerr (errno
, "Failed to close file(fd=%d)\n", *fdp
);
133 static void oss_helper_poll_out (void *opaque
)
136 audio_run ("oss_poll_out");
139 static void oss_helper_poll_in (void *opaque
)
142 audio_run ("oss_poll_in");
145 static int oss_poll_out (HWVoiceOut
*hw
)
147 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
149 return qemu_set_fd_handler (oss
->fd
, NULL
, oss_helper_poll_out
, NULL
);
152 static int oss_poll_in (HWVoiceIn
*hw
)
154 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
156 return qemu_set_fd_handler (oss
->fd
, oss_helper_poll_in
, NULL
, NULL
);
159 static int oss_write (SWVoiceOut
*sw
, void *buf
, int len
)
161 return audio_pcm_sw_write (sw
, buf
, len
);
164 static int aud_to_ossfmt (audfmt_e fmt
)
180 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
188 static int oss_to_audfmt (int ossfmt
, audfmt_e
*fmt
, int *endianness
)
222 dolog ("Unrecognized audio format %d\n", ossfmt
);
229 #if defined DEBUG_MISMATCHES || defined DEBUG
230 static void oss_dump_info (struct oss_params
*req
, struct oss_params
*obt
)
232 dolog ("parameter | requested value | obtained value\n");
233 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
234 dolog ("channels | %10d | %10d\n",
235 req
->nchannels
, obt
->nchannels
);
236 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
237 dolog ("nfrags | %10d | %10d\n", req
->nfrags
, obt
->nfrags
);
238 dolog ("fragsize | %10d | %10d\n",
239 req
->fragsize
, obt
->fragsize
);
243 static int oss_open (int in
, struct oss_params
*req
,
244 struct oss_params
*obt
, int *pfd
)
247 int oflags
= conf
.exclusive
? O_EXCL
: 0;
248 audio_buf_info abinfo
;
249 int fmt
, freq
, nchannels
;
251 const char *dspname
= in
? conf
.devpath_in
: conf
.devpath_out
;
252 const char *typ
= in
? "ADC" : "DAC";
254 /* Kludge needed to have working mmap on Linux */
255 oflags
|= conf
.try_mmap
? O_RDWR
: (in
? O_RDONLY
: O_WRONLY
);
257 fd
= open (dspname
, oflags
| O_NONBLOCK
);
259 oss_logerr2 (errno
, typ
, "Failed to open `%s'\n", dspname
);
264 nchannels
= req
->nchannels
;
267 if (ioctl (fd
, SNDCTL_DSP_SAMPLESIZE
, &fmt
)) {
268 oss_logerr2 (errno
, typ
, "Failed to set sample size %d\n", req
->fmt
);
272 if (ioctl (fd
, SNDCTL_DSP_CHANNELS
, &nchannels
)) {
273 oss_logerr2 (errno
, typ
, "Failed to set number of channels %d\n",
278 if (ioctl (fd
, SNDCTL_DSP_SPEED
, &freq
)) {
279 oss_logerr2 (errno
, typ
, "Failed to set frequency %d\n", req
->freq
);
283 if (ioctl (fd
, SNDCTL_DSP_NONBLOCK
, NULL
)) {
284 oss_logerr2 (errno
, typ
, "Failed to set non-blocking mode\n");
288 #ifdef USE_DSP_POLICY
289 if (conf
.policy
>= 0) {
292 if (ioctl (fd
, OSS_GETVERSION
, &version
)) {
293 oss_logerr2 (errno
, typ
, "Failed to get OSS version\n");
297 dolog ("OSS version = %#x\n", version
);
300 if (version
>= 0x040000) {
301 int policy
= conf
.policy
;
302 if (ioctl (fd
, SNDCTL_DSP_POLICY
, &policy
)) {
303 oss_logerr2 (errno
, typ
,
304 "Failed to set timing policy to %d\n",
315 int mmmmssss
= (req
->nfrags
<< 16) | ctz32 (req
->fragsize
);
316 if (ioctl (fd
, SNDCTL_DSP_SETFRAGMENT
, &mmmmssss
)) {
317 oss_logerr2 (errno
, typ
, "Failed to set buffer length (%d, %d)\n",
318 req
->nfrags
, req
->fragsize
);
323 if (ioctl (fd
, in
? SNDCTL_DSP_GETISPACE
: SNDCTL_DSP_GETOSPACE
, &abinfo
)) {
324 oss_logerr2 (errno
, typ
, "Failed to get buffer length\n");
328 if (!abinfo
.fragstotal
|| !abinfo
.fragsize
) {
329 AUD_log (AUDIO_CAP
, "Returned bogus buffer information(%d, %d) for %s\n",
330 abinfo
.fragstotal
, abinfo
.fragsize
, typ
);
335 obt
->nchannels
= nchannels
;
337 obt
->nfrags
= abinfo
.fragstotal
;
338 obt
->fragsize
= abinfo
.fragsize
;
341 #ifdef DEBUG_MISMATCHES
342 if ((req
->fmt
!= obt
->fmt
) ||
343 (req
->nchannels
!= obt
->nchannels
) ||
344 (req
->freq
!= obt
->freq
) ||
345 (req
->fragsize
!= obt
->fragsize
) ||
346 (req
->nfrags
!= obt
->nfrags
)) {
347 dolog ("Audio parameters mismatch\n");
348 oss_dump_info (req
, obt
);
353 oss_dump_info (req
, obt
);
358 oss_anal_close (&fd
);
362 static void oss_write_pending (OSSVoiceOut
*oss
)
364 HWVoiceOut
*hw
= &oss
->hw
;
370 while (oss
->pending
) {
372 ssize_t bytes_written
;
373 int samples_till_end
= hw
->samples
- oss
->wpos
;
374 int samples_to_write
= audio_MIN (oss
->pending
, samples_till_end
);
375 int bytes_to_write
= samples_to_write
<< hw
->info
.shift
;
376 void *pcm
= advance (oss
->pcm_buf
, oss
->wpos
<< hw
->info
.shift
);
378 bytes_written
= write (oss
->fd
, pcm
, bytes_to_write
);
379 if (bytes_written
< 0) {
380 if (errno
!= EAGAIN
) {
381 oss_logerr (errno
, "failed to write %d bytes\n",
387 if (bytes_written
& hw
->info
.align
) {
388 dolog ("misaligned write asked for %d, but got %zd\n",
389 bytes_to_write
, bytes_written
);
393 samples_written
= bytes_written
>> hw
->info
.shift
;
394 oss
->pending
-= samples_written
;
395 oss
->wpos
= (oss
->wpos
+ samples_written
) % hw
->samples
;
396 if (bytes_written
- bytes_to_write
) {
402 static int oss_run_out (HWVoiceOut
*hw
, int live
)
404 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
406 struct audio_buf_info abinfo
;
407 struct count_info cntinfo
;
410 bufsize
= hw
->samples
<< hw
->info
.shift
;
415 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOPTR
, &cntinfo
);
417 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
421 pos
= hw
->rpos
<< hw
->info
.shift
;
422 bytes
= audio_ring_dist (cntinfo
.ptr
, pos
, bufsize
);
423 decr
= audio_MIN (bytes
>> hw
->info
.shift
, live
);
426 err
= ioctl (oss
->fd
, SNDCTL_DSP_GETOSPACE
, &abinfo
);
428 oss_logerr (errno
, "SNDCTL_DSP_GETOPTR failed\n");
432 if (abinfo
.bytes
> bufsize
) {
434 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
435 "please report your OS/audio hw to av1474@comtv.ru\n",
436 abinfo
.bytes
, bufsize
);
438 abinfo
.bytes
= bufsize
;
441 if (abinfo
.bytes
< 0) {
443 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
444 abinfo
.bytes
, bufsize
);
449 decr
= audio_MIN (abinfo
.bytes
>> hw
->info
.shift
, live
);
455 decr
= audio_pcm_hw_clip_out (hw
, oss
->pcm_buf
, decr
, oss
->pending
);
456 oss
->pending
+= decr
;
457 oss_write_pending (oss
);
462 static void oss_fini_out (HWVoiceOut
*hw
)
465 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
467 ldebug ("oss_fini\n");
468 oss_anal_close (&oss
->fd
);
472 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
474 oss_logerr (errno
, "Failed to unmap buffer %p, size %d\n",
475 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
479 qemu_free (oss
->pcm_buf
);
485 static int oss_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
487 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
488 struct oss_params req
, obt
;
492 audfmt_e effective_fmt
;
493 struct audsettings obt_as
;
497 req
.fmt
= aud_to_ossfmt (as
->fmt
);
499 req
.nchannels
= as
->nchannels
;
500 req
.fragsize
= conf
.fragsize
;
501 req
.nfrags
= conf
.nfrags
;
503 if (oss_open (0, &req
, &obt
, &fd
)) {
507 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
509 oss_anal_close (&fd
);
513 obt_as
.freq
= obt
.freq
;
514 obt_as
.nchannels
= obt
.nchannels
;
515 obt_as
.fmt
= effective_fmt
;
516 obt_as
.endianness
= endianness
;
518 audio_pcm_init_info (&hw
->info
, &obt_as
);
519 oss
->nfrags
= obt
.nfrags
;
520 oss
->fragsize
= obt
.fragsize
;
522 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
523 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
524 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
527 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
531 oss
->pcm_buf
= mmap (
533 hw
->samples
<< hw
->info
.shift
,
534 PROT_READ
| PROT_WRITE
,
539 if (oss
->pcm_buf
== MAP_FAILED
) {
540 oss_logerr (errno
, "Failed to map %d bytes of DAC\n",
541 hw
->samples
<< hw
->info
.shift
);
546 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
547 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
550 trig
= PCM_ENABLE_OUTPUT
;
551 if (ioctl (fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
554 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
563 err
= munmap (oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
565 oss_logerr (errno
, "Failed to unmap buffer %p size %d\n",
566 oss
->pcm_buf
, hw
->samples
<< hw
->info
.shift
);
573 oss
->pcm_buf
= audio_calloc (
580 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
584 oss_anal_close (&fd
);
593 static int oss_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
596 OSSVoiceOut
*oss
= (OSSVoiceOut
*) hw
;
605 poll_mode
= va_arg (ap
, int);
608 ldebug ("enabling voice\n");
609 if (poll_mode
&& oss_poll_out (hw
)) {
612 hw
->poll_mode
= poll_mode
;
618 audio_pcm_info_clear_buf (&hw
->info
, oss
->pcm_buf
, hw
->samples
);
619 trig
= PCM_ENABLE_OUTPUT
;
620 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
623 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
632 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
640 ldebug ("disabling voice\n");
642 if (ioctl (oss
->fd
, SNDCTL_DSP_SETTRIGGER
, &trig
) < 0) {
643 oss_logerr (errno
, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
651 static int oss_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
653 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
654 struct oss_params req
, obt
;
658 audfmt_e effective_fmt
;
659 struct audsettings obt_as
;
663 req
.fmt
= aud_to_ossfmt (as
->fmt
);
665 req
.nchannels
= as
->nchannels
;
666 req
.fragsize
= conf
.fragsize
;
667 req
.nfrags
= conf
.nfrags
;
668 if (oss_open (1, &req
, &obt
, &fd
)) {
672 err
= oss_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
674 oss_anal_close (&fd
);
678 obt_as
.freq
= obt
.freq
;
679 obt_as
.nchannels
= obt
.nchannels
;
680 obt_as
.fmt
= effective_fmt
;
681 obt_as
.endianness
= endianness
;
683 audio_pcm_init_info (&hw
->info
, &obt_as
);
684 oss
->nfrags
= obt
.nfrags
;
685 oss
->fragsize
= obt
.fragsize
;
687 if (obt
.nfrags
* obt
.fragsize
& hw
->info
.align
) {
688 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
689 obt
.nfrags
* obt
.fragsize
, hw
->info
.align
+ 1);
692 hw
->samples
= (obt
.nfrags
* obt
.fragsize
) >> hw
->info
.shift
;
693 oss
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
695 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
696 hw
->samples
, 1 << hw
->info
.shift
);
697 oss_anal_close (&fd
);
705 static void oss_fini_in (HWVoiceIn
*hw
)
707 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
709 oss_anal_close (&oss
->fd
);
712 qemu_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
,
763 if (bufs
[i
].len
- nread
) {
772 "Failed to read %d bytes of audio (to %p)\n",
783 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
787 static int oss_read (SWVoiceIn
*sw
, void *buf
, int size
)
789 return audio_pcm_sw_read (sw
, buf
, size
);
792 static int oss_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
794 OSSVoiceIn
*oss
= (OSSVoiceIn
*) hw
;
803 poll_mode
= va_arg (ap
, int);
806 if (poll_mode
&& oss_poll_in (hw
)) {
809 hw
->poll_mode
= poll_mode
;
816 qemu_set_fd_handler (oss
->fd
, NULL
, NULL
, NULL
);
823 static void *oss_audio_init (void)
828 static void oss_audio_fini (void *opaque
)
833 static struct audio_option oss_options
[] = {
837 .valp
= &conf
.fragsize
,
838 .descr
= "Fragment size in bytes"
843 .valp
= &conf
.nfrags
,
844 .descr
= "Number of fragments"
849 .valp
= &conf
.try_mmap
,
850 .descr
= "Try using memory mapped access"
855 .valp
= &conf
.devpath_out
,
856 .descr
= "Path to DAC device"
861 .valp
= &conf
.devpath_in
,
862 .descr
= "Path to ADC device"
867 .valp
= &conf
.exclusive
,
868 .descr
= "Open device in exclusive mode (vmix wont work)"
870 #ifdef USE_DSP_POLICY
874 .valp
= &conf
.policy
,
875 .descr
= "Set the timing policy of the device, -1 to use fragment mode",
882 .descr
= "Turn on some debugging messages"
884 { /* End of list */ }
887 static struct audio_pcm_ops oss_pcm_ops
= {
888 .init_out
= oss_init_out
,
889 .fini_out
= oss_fini_out
,
890 .run_out
= oss_run_out
,
892 .ctl_out
= oss_ctl_out
,
894 .init_in
= oss_init_in
,
895 .fini_in
= oss_fini_in
,
896 .run_in
= oss_run_in
,
901 struct audio_driver oss_audio_driver
= {
903 .descr
= "OSS http://www.opensound.com",
904 .options
= oss_options
,
905 .init
= oss_audio_init
,
906 .fini
= oss_audio_fini
,
907 .pcm_ops
= &oss_pcm_ops
,
909 .max_voices_out
= INT_MAX
,
910 .max_voices_in
= INT_MAX
,
911 .voice_size_out
= sizeof (OSSVoiceOut
),
912 .voice_size_in
= sizeof (OSSVoiceIn
)