vmxnet3: Use common MAC address tracing macros
[qemu.git] / audio / ossaudio.c
bloba0d9cda1ec5cd7f3a2846e7bd4ce2effc949ac18
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include <sys/mman.h>
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "qemu-common.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/host-utils.h"
31 #include "audio.h"
32 #include "trace.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
39 #endif
41 typedef struct OSSConf {
42 int try_mmap;
43 int nfrags;
44 int fragsize;
45 const char *devpath_out;
46 const char *devpath_in;
47 int exclusive;
48 int policy;
49 } OSSConf;
51 typedef struct OSSVoiceOut {
52 HWVoiceOut hw;
53 void *pcm_buf;
54 int fd;
55 int wpos;
56 int nfrags;
57 int fragsize;
58 int mmapped;
59 int pending;
60 OSSConf *conf;
61 } OSSVoiceOut;
63 typedef struct OSSVoiceIn {
64 HWVoiceIn hw;
65 void *pcm_buf;
66 int fd;
67 int nfrags;
68 int fragsize;
69 OSSConf *conf;
70 } OSSVoiceIn;
72 struct oss_params {
73 int freq;
74 audfmt_e fmt;
75 int nchannels;
76 int nfrags;
77 int fragsize;
80 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
82 va_list ap;
84 va_start (ap, fmt);
85 AUD_vlog (AUDIO_CAP, fmt, ap);
86 va_end (ap);
88 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
91 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
92 int err,
93 const char *typ,
94 const char *fmt,
95 ...
98 va_list ap;
100 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
102 va_start (ap, fmt);
103 AUD_vlog (AUDIO_CAP, fmt, ap);
104 va_end (ap);
106 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
109 static void oss_anal_close (int *fdp)
111 int err;
113 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
114 err = close (*fdp);
115 if (err) {
116 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
118 *fdp = -1;
121 static void oss_helper_poll_out (void *opaque)
123 (void) opaque;
124 audio_run ("oss_poll_out");
127 static void oss_helper_poll_in (void *opaque)
129 (void) opaque;
130 audio_run ("oss_poll_in");
133 static void oss_poll_out (HWVoiceOut *hw)
135 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
137 qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
140 static void oss_poll_in (HWVoiceIn *hw)
142 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
144 qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
147 static int oss_write (SWVoiceOut *sw, void *buf, int len)
149 return audio_pcm_sw_write (sw, buf, len);
152 static int aud_to_ossfmt (audfmt_e fmt, int endianness)
154 switch (fmt) {
155 case AUD_FMT_S8:
156 return AFMT_S8;
158 case AUD_FMT_U8:
159 return AFMT_U8;
161 case AUD_FMT_S16:
162 if (endianness) {
163 return AFMT_S16_BE;
165 else {
166 return AFMT_S16_LE;
169 case AUD_FMT_U16:
170 if (endianness) {
171 return AFMT_U16_BE;
173 else {
174 return AFMT_U16_LE;
177 default:
178 dolog ("Internal logic error: Bad audio format %d\n", fmt);
179 #ifdef DEBUG_AUDIO
180 abort ();
181 #endif
182 return AFMT_U8;
186 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
188 switch (ossfmt) {
189 case AFMT_S8:
190 *endianness = 0;
191 *fmt = AUD_FMT_S8;
192 break;
194 case AFMT_U8:
195 *endianness = 0;
196 *fmt = AUD_FMT_U8;
197 break;
199 case AFMT_S16_LE:
200 *endianness = 0;
201 *fmt = AUD_FMT_S16;
202 break;
204 case AFMT_U16_LE:
205 *endianness = 0;
206 *fmt = AUD_FMT_U16;
207 break;
209 case AFMT_S16_BE:
210 *endianness = 1;
211 *fmt = AUD_FMT_S16;
212 break;
214 case AFMT_U16_BE:
215 *endianness = 1;
216 *fmt = AUD_FMT_U16;
217 break;
219 default:
220 dolog ("Unrecognized audio format %d\n", ossfmt);
221 return -1;
224 return 0;
227 #if defined DEBUG_MISMATCHES || defined DEBUG
228 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
230 dolog ("parameter | requested value | obtained value\n");
231 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
232 dolog ("channels | %10d | %10d\n",
233 req->nchannels, obt->nchannels);
234 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
235 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
236 dolog ("fragsize | %10d | %10d\n",
237 req->fragsize, obt->fragsize);
239 #endif
241 #ifdef USE_DSP_POLICY
242 static int oss_get_version (int fd, int *version, const char *typ)
244 if (ioctl (fd, OSS_GETVERSION, &version)) {
245 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
247 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
248 * since 7.x, but currently only on the mixer device (or in
249 * the Linuxolator), and in the native version that part of
250 * the code is in fact never reached so the ioctl fails anyway.
251 * Until this is fixed, just check the errno and if its what
252 * FreeBSD's sound drivers return atm assume they are new enough.
254 if (errno == EINVAL) {
255 *version = 0x040000;
256 return 0;
258 #endif
259 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
260 return -1;
262 return 0;
264 #endif
266 static int oss_open (int in, struct oss_params *req,
267 struct oss_params *obt, int *pfd, OSSConf* conf)
269 int fd;
270 int oflags = conf->exclusive ? O_EXCL : 0;
271 audio_buf_info abinfo;
272 int fmt, freq, nchannels;
273 int setfragment = 1;
274 const char *dspname = in ? conf->devpath_in : conf->devpath_out;
275 const char *typ = in ? "ADC" : "DAC";
277 /* Kludge needed to have working mmap on Linux */
278 oflags |= conf->try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
280 fd = open (dspname, oflags | O_NONBLOCK);
281 if (-1 == fd) {
282 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
283 return -1;
286 freq = req->freq;
287 nchannels = req->nchannels;
288 fmt = req->fmt;
290 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
291 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
292 goto err;
295 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
296 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
297 req->nchannels);
298 goto err;
301 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
302 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
303 goto err;
306 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
307 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
308 goto err;
311 #ifdef USE_DSP_POLICY
312 if (conf->policy >= 0) {
313 int version;
315 if (!oss_get_version (fd, &version, typ)) {
316 trace_oss_version(version);
318 if (version >= 0x040000) {
319 int policy = conf->policy;
320 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
321 oss_logerr2 (errno, typ,
322 "Failed to set timing policy to %d\n",
323 conf->policy);
324 goto err;
326 setfragment = 0;
330 #endif
332 if (setfragment) {
333 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
334 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
335 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
336 req->nfrags, req->fragsize);
337 goto err;
341 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
342 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
343 goto err;
346 if (!abinfo.fragstotal || !abinfo.fragsize) {
347 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
348 abinfo.fragstotal, abinfo.fragsize, typ);
349 goto err;
352 obt->fmt = fmt;
353 obt->nchannels = nchannels;
354 obt->freq = freq;
355 obt->nfrags = abinfo.fragstotal;
356 obt->fragsize = abinfo.fragsize;
357 *pfd = fd;
359 #ifdef DEBUG_MISMATCHES
360 if ((req->fmt != obt->fmt) ||
361 (req->nchannels != obt->nchannels) ||
362 (req->freq != obt->freq) ||
363 (req->fragsize != obt->fragsize) ||
364 (req->nfrags != obt->nfrags)) {
365 dolog ("Audio parameters mismatch\n");
366 oss_dump_info (req, obt);
368 #endif
370 #ifdef DEBUG
371 oss_dump_info (req, obt);
372 #endif
373 return 0;
375 err:
376 oss_anal_close (&fd);
377 return -1;
380 static void oss_write_pending (OSSVoiceOut *oss)
382 HWVoiceOut *hw = &oss->hw;
384 if (oss->mmapped) {
385 return;
388 while (oss->pending) {
389 int samples_written;
390 ssize_t bytes_written;
391 int samples_till_end = hw->samples - oss->wpos;
392 int samples_to_write = audio_MIN (oss->pending, samples_till_end);
393 int bytes_to_write = samples_to_write << hw->info.shift;
394 void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
396 bytes_written = write (oss->fd, pcm, bytes_to_write);
397 if (bytes_written < 0) {
398 if (errno != EAGAIN) {
399 oss_logerr (errno, "failed to write %d bytes\n",
400 bytes_to_write);
402 break;
405 if (bytes_written & hw->info.align) {
406 dolog ("misaligned write asked for %d, but got %zd\n",
407 bytes_to_write, bytes_written);
408 return;
411 samples_written = bytes_written >> hw->info.shift;
412 oss->pending -= samples_written;
413 oss->wpos = (oss->wpos + samples_written) % hw->samples;
414 if (bytes_written - bytes_to_write) {
415 break;
420 static int oss_run_out (HWVoiceOut *hw, int live)
422 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
423 int err, decr;
424 struct audio_buf_info abinfo;
425 struct count_info cntinfo;
426 int bufsize;
428 bufsize = hw->samples << hw->info.shift;
430 if (oss->mmapped) {
431 int bytes, pos;
433 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
434 if (err < 0) {
435 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
436 return 0;
439 pos = hw->rpos << hw->info.shift;
440 bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
441 decr = audio_MIN (bytes >> hw->info.shift, live);
443 else {
444 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
445 if (err < 0) {
446 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
447 return 0;
450 if (abinfo.bytes > bufsize) {
451 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
452 abinfo.bytes = bufsize;
455 if (abinfo.bytes < 0) {
456 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
457 return 0;
460 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
461 if (!decr) {
462 return 0;
466 decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending);
467 oss->pending += decr;
468 oss_write_pending (oss);
470 return decr;
473 static void oss_fini_out (HWVoiceOut *hw)
475 int err;
476 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
478 ldebug ("oss_fini\n");
479 oss_anal_close (&oss->fd);
481 if (oss->pcm_buf) {
482 if (oss->mmapped) {
483 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
484 if (err) {
485 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
486 oss->pcm_buf, hw->samples << hw->info.shift);
489 else {
490 g_free (oss->pcm_buf);
492 oss->pcm_buf = NULL;
496 static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
497 void *drv_opaque)
499 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
500 struct oss_params req, obt;
501 int endianness;
502 int err;
503 int fd;
504 audfmt_e effective_fmt;
505 struct audsettings obt_as;
506 OSSConf *conf = drv_opaque;
508 oss->fd = -1;
510 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
511 req.freq = as->freq;
512 req.nchannels = as->nchannels;
513 req.fragsize = conf->fragsize;
514 req.nfrags = conf->nfrags;
516 if (oss_open (0, &req, &obt, &fd, conf)) {
517 return -1;
520 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
521 if (err) {
522 oss_anal_close (&fd);
523 return -1;
526 obt_as.freq = obt.freq;
527 obt_as.nchannels = obt.nchannels;
528 obt_as.fmt = effective_fmt;
529 obt_as.endianness = endianness;
531 audio_pcm_init_info (&hw->info, &obt_as);
532 oss->nfrags = obt.nfrags;
533 oss->fragsize = obt.fragsize;
535 if (obt.nfrags * obt.fragsize & hw->info.align) {
536 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
537 obt.nfrags * obt.fragsize, hw->info.align + 1);
540 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
542 oss->mmapped = 0;
543 if (conf->try_mmap) {
544 oss->pcm_buf = mmap (
545 NULL,
546 hw->samples << hw->info.shift,
547 PROT_READ | PROT_WRITE,
548 MAP_SHARED,
552 if (oss->pcm_buf == MAP_FAILED) {
553 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
554 hw->samples << hw->info.shift);
556 else {
557 int err;
558 int trig = 0;
559 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
560 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
562 else {
563 trig = PCM_ENABLE_OUTPUT;
564 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
565 oss_logerr (
566 errno,
567 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
570 else {
571 oss->mmapped = 1;
575 if (!oss->mmapped) {
576 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
577 if (err) {
578 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
579 oss->pcm_buf, hw->samples << hw->info.shift);
585 if (!oss->mmapped) {
586 oss->pcm_buf = audio_calloc (
587 AUDIO_FUNC,
588 hw->samples,
589 1 << hw->info.shift
591 if (!oss->pcm_buf) {
592 dolog (
593 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
594 hw->samples,
595 1 << hw->info.shift
597 oss_anal_close (&fd);
598 return -1;
602 oss->fd = fd;
603 oss->conf = conf;
604 return 0;
607 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
609 int trig;
610 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
612 switch (cmd) {
613 case VOICE_ENABLE:
615 va_list ap;
616 int poll_mode;
618 va_start (ap, cmd);
619 poll_mode = va_arg (ap, int);
620 va_end (ap);
622 ldebug ("enabling voice\n");
623 if (poll_mode) {
624 oss_poll_out (hw);
625 poll_mode = 0;
627 hw->poll_mode = poll_mode;
629 if (!oss->mmapped) {
630 return 0;
633 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
634 trig = PCM_ENABLE_OUTPUT;
635 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
636 oss_logerr (
637 errno,
638 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
640 return -1;
643 break;
645 case VOICE_DISABLE:
646 if (hw->poll_mode) {
647 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
648 hw->poll_mode = 0;
651 if (!oss->mmapped) {
652 return 0;
655 ldebug ("disabling voice\n");
656 trig = 0;
657 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
658 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
659 return -1;
661 break;
663 return 0;
666 static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
668 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
669 struct oss_params req, obt;
670 int endianness;
671 int err;
672 int fd;
673 audfmt_e effective_fmt;
674 struct audsettings obt_as;
675 OSSConf *conf = drv_opaque;
677 oss->fd = -1;
679 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
680 req.freq = as->freq;
681 req.nchannels = as->nchannels;
682 req.fragsize = conf->fragsize;
683 req.nfrags = conf->nfrags;
684 if (oss_open (1, &req, &obt, &fd, conf)) {
685 return -1;
688 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
689 if (err) {
690 oss_anal_close (&fd);
691 return -1;
694 obt_as.freq = obt.freq;
695 obt_as.nchannels = obt.nchannels;
696 obt_as.fmt = effective_fmt;
697 obt_as.endianness = endianness;
699 audio_pcm_init_info (&hw->info, &obt_as);
700 oss->nfrags = obt.nfrags;
701 oss->fragsize = obt.fragsize;
703 if (obt.nfrags * obt.fragsize & hw->info.align) {
704 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
705 obt.nfrags * obt.fragsize, hw->info.align + 1);
708 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
709 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
710 if (!oss->pcm_buf) {
711 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
712 hw->samples, 1 << hw->info.shift);
713 oss_anal_close (&fd);
714 return -1;
717 oss->fd = fd;
718 oss->conf = conf;
719 return 0;
722 static void oss_fini_in (HWVoiceIn *hw)
724 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
726 oss_anal_close (&oss->fd);
728 g_free(oss->pcm_buf);
729 oss->pcm_buf = NULL;
732 static int oss_run_in (HWVoiceIn *hw)
734 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
735 int hwshift = hw->info.shift;
736 int i;
737 int live = audio_pcm_hw_get_live_in (hw);
738 int dead = hw->samples - live;
739 size_t read_samples = 0;
740 struct {
741 int add;
742 int len;
743 } bufs[2] = {
744 { .add = hw->wpos, .len = 0 },
745 { .add = 0, .len = 0 }
748 if (!dead) {
749 return 0;
752 if (hw->wpos + dead > hw->samples) {
753 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
754 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
756 else {
757 bufs[0].len = dead << hwshift;
760 for (i = 0; i < 2; ++i) {
761 ssize_t nread;
763 if (bufs[i].len) {
764 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
765 nread = read (oss->fd, p, bufs[i].len);
767 if (nread > 0) {
768 if (nread & hw->info.align) {
769 dolog ("warning: Misaligned read %zd (requested %d), "
770 "alignment %d\n", nread, bufs[i].add << hwshift,
771 hw->info.align + 1);
773 read_samples += nread >> hwshift;
774 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift);
777 if (bufs[i].len - nread) {
778 if (nread == -1) {
779 switch (errno) {
780 case EINTR:
781 case EAGAIN:
782 break;
783 default:
784 oss_logerr (
785 errno,
786 "Failed to read %d bytes of audio (to %p)\n",
787 bufs[i].len, p
789 break;
792 break;
797 hw->wpos = (hw->wpos + read_samples) % hw->samples;
798 return read_samples;
801 static int oss_read (SWVoiceIn *sw, void *buf, int size)
803 return audio_pcm_sw_read (sw, buf, size);
806 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
808 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
810 switch (cmd) {
811 case VOICE_ENABLE:
813 va_list ap;
814 int poll_mode;
816 va_start (ap, cmd);
817 poll_mode = va_arg (ap, int);
818 va_end (ap);
820 if (poll_mode) {
821 oss_poll_in (hw);
822 poll_mode = 0;
824 hw->poll_mode = poll_mode;
826 break;
828 case VOICE_DISABLE:
829 if (hw->poll_mode) {
830 hw->poll_mode = 0;
831 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
833 break;
835 return 0;
838 static OSSConf glob_conf = {
839 .try_mmap = 0,
840 .nfrags = 4,
841 .fragsize = 4096,
842 .devpath_out = "/dev/dsp",
843 .devpath_in = "/dev/dsp",
844 .exclusive = 0,
845 .policy = 5
848 static void *oss_audio_init (void)
850 OSSConf *conf = g_malloc(sizeof(OSSConf));
851 *conf = glob_conf;
853 if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
854 access(conf->devpath_out, R_OK | W_OK) < 0) {
855 g_free(conf);
856 return NULL;
858 return conf;
861 static void oss_audio_fini (void *opaque)
863 g_free(opaque);
866 static struct audio_option oss_options[] = {
868 .name = "FRAGSIZE",
869 .tag = AUD_OPT_INT,
870 .valp = &glob_conf.fragsize,
871 .descr = "Fragment size in bytes"
874 .name = "NFRAGS",
875 .tag = AUD_OPT_INT,
876 .valp = &glob_conf.nfrags,
877 .descr = "Number of fragments"
880 .name = "MMAP",
881 .tag = AUD_OPT_BOOL,
882 .valp = &glob_conf.try_mmap,
883 .descr = "Try using memory mapped access"
886 .name = "DAC_DEV",
887 .tag = AUD_OPT_STR,
888 .valp = &glob_conf.devpath_out,
889 .descr = "Path to DAC device"
892 .name = "ADC_DEV",
893 .tag = AUD_OPT_STR,
894 .valp = &glob_conf.devpath_in,
895 .descr = "Path to ADC device"
898 .name = "EXCLUSIVE",
899 .tag = AUD_OPT_BOOL,
900 .valp = &glob_conf.exclusive,
901 .descr = "Open device in exclusive mode (vmix won't work)"
903 #ifdef USE_DSP_POLICY
905 .name = "POLICY",
906 .tag = AUD_OPT_INT,
907 .valp = &glob_conf.policy,
908 .descr = "Set the timing policy of the device, -1 to use fragment mode",
910 #endif
911 { /* End of list */ }
914 static struct audio_pcm_ops oss_pcm_ops = {
915 .init_out = oss_init_out,
916 .fini_out = oss_fini_out,
917 .run_out = oss_run_out,
918 .write = oss_write,
919 .ctl_out = oss_ctl_out,
921 .init_in = oss_init_in,
922 .fini_in = oss_fini_in,
923 .run_in = oss_run_in,
924 .read = oss_read,
925 .ctl_in = oss_ctl_in
928 struct audio_driver oss_audio_driver = {
929 .name = "oss",
930 .descr = "OSS http://www.opensound.com",
931 .options = oss_options,
932 .init = oss_audio_init,
933 .fini = oss_audio_fini,
934 .pcm_ops = &oss_pcm_ops,
935 .can_be_default = 1,
936 .max_voices_out = INT_MAX,
937 .max_voices_in = INT_MAX,
938 .voice_size_out = sizeof (OSSVoiceOut),
939 .voice_size_in = sizeof (OSSVoiceIn)