Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu/ar7.git] / audio / ossaudio.c
blob7dbe3332d8e8d554edef07bcfa865a0559dbb68a
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 <stdlib.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/soundcard.h>
29 #include "qemu-common.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/host-utils.h"
32 #include "audio.h"
33 #include "trace.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
40 #endif
42 typedef struct OSSConf {
43 int try_mmap;
44 int nfrags;
45 int fragsize;
46 const char *devpath_out;
47 const char *devpath_in;
48 int exclusive;
49 int policy;
50 } OSSConf;
52 typedef struct OSSVoiceOut {
53 HWVoiceOut hw;
54 void *pcm_buf;
55 int fd;
56 int wpos;
57 int nfrags;
58 int fragsize;
59 int mmapped;
60 int pending;
61 OSSConf *conf;
62 } OSSVoiceOut;
64 typedef struct OSSVoiceIn {
65 HWVoiceIn hw;
66 void *pcm_buf;
67 int fd;
68 int nfrags;
69 int fragsize;
70 OSSConf *conf;
71 } OSSVoiceIn;
73 struct oss_params {
74 int freq;
75 audfmt_e fmt;
76 int nchannels;
77 int nfrags;
78 int fragsize;
81 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
83 va_list ap;
85 va_start (ap, fmt);
86 AUD_vlog (AUDIO_CAP, fmt, ap);
87 va_end (ap);
89 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
92 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
93 int err,
94 const char *typ,
95 const char *fmt,
96 ...
99 va_list ap;
101 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
103 va_start (ap, fmt);
104 AUD_vlog (AUDIO_CAP, fmt, ap);
105 va_end (ap);
107 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
110 static void oss_anal_close (int *fdp)
112 int err;
114 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
115 err = close (*fdp);
116 if (err) {
117 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
119 *fdp = -1;
122 static void oss_helper_poll_out (void *opaque)
124 (void) opaque;
125 audio_run ("oss_poll_out");
128 static void oss_helper_poll_in (void *opaque)
130 (void) opaque;
131 audio_run ("oss_poll_in");
134 static void oss_poll_out (HWVoiceOut *hw)
136 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
138 qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
141 static void oss_poll_in (HWVoiceIn *hw)
143 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
145 qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
148 static int oss_write (SWVoiceOut *sw, void *buf, int len)
150 return audio_pcm_sw_write (sw, buf, len);
153 static int aud_to_ossfmt (audfmt_e fmt, int endianness)
155 switch (fmt) {
156 case AUD_FMT_S8:
157 return AFMT_S8;
159 case AUD_FMT_U8:
160 return AFMT_U8;
162 case AUD_FMT_S16:
163 if (endianness) {
164 return AFMT_S16_BE;
166 else {
167 return AFMT_S16_LE;
170 case AUD_FMT_U16:
171 if (endianness) {
172 return AFMT_U16_BE;
174 else {
175 return AFMT_U16_LE;
178 default:
179 dolog ("Internal logic error: Bad audio format %d\n", fmt);
180 #ifdef DEBUG_AUDIO
181 abort ();
182 #endif
183 return AFMT_U8;
187 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
189 switch (ossfmt) {
190 case AFMT_S8:
191 *endianness = 0;
192 *fmt = AUD_FMT_S8;
193 break;
195 case AFMT_U8:
196 *endianness = 0;
197 *fmt = AUD_FMT_U8;
198 break;
200 case AFMT_S16_LE:
201 *endianness = 0;
202 *fmt = AUD_FMT_S16;
203 break;
205 case AFMT_U16_LE:
206 *endianness = 0;
207 *fmt = AUD_FMT_U16;
208 break;
210 case AFMT_S16_BE:
211 *endianness = 1;
212 *fmt = AUD_FMT_S16;
213 break;
215 case AFMT_U16_BE:
216 *endianness = 1;
217 *fmt = AUD_FMT_U16;
218 break;
220 default:
221 dolog ("Unrecognized audio format %d\n", ossfmt);
222 return -1;
225 return 0;
228 #if defined DEBUG_MISMATCHES || defined DEBUG
229 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
231 dolog ("parameter | requested value | obtained value\n");
232 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
233 dolog ("channels | %10d | %10d\n",
234 req->nchannels, obt->nchannels);
235 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
236 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
237 dolog ("fragsize | %10d | %10d\n",
238 req->fragsize, obt->fragsize);
240 #endif
242 #ifdef USE_DSP_POLICY
243 static int oss_get_version (int fd, int *version, const char *typ)
245 if (ioctl (fd, OSS_GETVERSION, &version)) {
246 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
248 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
249 * since 7.x, but currently only on the mixer device (or in
250 * the Linuxolator), and in the native version that part of
251 * the code is in fact never reached so the ioctl fails anyway.
252 * Until this is fixed, just check the errno and if its what
253 * FreeBSD's sound drivers return atm assume they are new enough.
255 if (errno == EINVAL) {
256 *version = 0x040000;
257 return 0;
259 #endif
260 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
261 return -1;
263 return 0;
265 #endif
267 static int oss_open (int in, struct oss_params *req,
268 struct oss_params *obt, int *pfd, OSSConf* conf)
270 int fd;
271 int oflags = conf->exclusive ? O_EXCL : 0;
272 audio_buf_info abinfo;
273 int fmt, freq, nchannels;
274 int setfragment = 1;
275 const char *dspname = in ? conf->devpath_in : conf->devpath_out;
276 const char *typ = in ? "ADC" : "DAC";
278 /* Kludge needed to have working mmap on Linux */
279 oflags |= conf->try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
281 fd = open (dspname, oflags | O_NONBLOCK);
282 if (-1 == fd) {
283 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
284 return -1;
287 freq = req->freq;
288 nchannels = req->nchannels;
289 fmt = req->fmt;
291 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
292 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
293 goto err;
296 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
297 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
298 req->nchannels);
299 goto err;
302 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
303 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
304 goto err;
307 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
308 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
309 goto err;
312 #ifdef USE_DSP_POLICY
313 if (conf->policy >= 0) {
314 int version;
316 if (!oss_get_version (fd, &version, typ)) {
317 trace_oss_version(version);
319 if (version >= 0x040000) {
320 int policy = conf->policy;
321 if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
322 oss_logerr2 (errno, typ,
323 "Failed to set timing policy to %d\n",
324 conf->policy);
325 goto err;
327 setfragment = 0;
331 #endif
333 if (setfragment) {
334 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
335 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
336 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
337 req->nfrags, req->fragsize);
338 goto err;
342 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
343 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
344 goto err;
347 if (!abinfo.fragstotal || !abinfo.fragsize) {
348 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
349 abinfo.fragstotal, abinfo.fragsize, typ);
350 goto err;
353 obt->fmt = fmt;
354 obt->nchannels = nchannels;
355 obt->freq = freq;
356 obt->nfrags = abinfo.fragstotal;
357 obt->fragsize = abinfo.fragsize;
358 *pfd = fd;
360 #ifdef DEBUG_MISMATCHES
361 if ((req->fmt != obt->fmt) ||
362 (req->nchannels != obt->nchannels) ||
363 (req->freq != obt->freq) ||
364 (req->fragsize != obt->fragsize) ||
365 (req->nfrags != obt->nfrags)) {
366 dolog ("Audio parameters mismatch\n");
367 oss_dump_info (req, obt);
369 #endif
371 #ifdef DEBUG
372 oss_dump_info (req, obt);
373 #endif
374 return 0;
376 err:
377 oss_anal_close (&fd);
378 return -1;
381 static void oss_write_pending (OSSVoiceOut *oss)
383 HWVoiceOut *hw = &oss->hw;
385 if (oss->mmapped) {
386 return;
389 while (oss->pending) {
390 int samples_written;
391 ssize_t bytes_written;
392 int samples_till_end = hw->samples - oss->wpos;
393 int samples_to_write = audio_MIN (oss->pending, samples_till_end);
394 int bytes_to_write = samples_to_write << hw->info.shift;
395 void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
397 bytes_written = write (oss->fd, pcm, bytes_to_write);
398 if (bytes_written < 0) {
399 if (errno != EAGAIN) {
400 oss_logerr (errno, "failed to write %d bytes\n",
401 bytes_to_write);
403 break;
406 if (bytes_written & hw->info.align) {
407 dolog ("misaligned write asked for %d, but got %zd\n",
408 bytes_to_write, bytes_written);
409 return;
412 samples_written = bytes_written >> hw->info.shift;
413 oss->pending -= samples_written;
414 oss->wpos = (oss->wpos + samples_written) % hw->samples;
415 if (bytes_written - bytes_to_write) {
416 break;
421 static int oss_run_out (HWVoiceOut *hw, int live)
423 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
424 int err, decr;
425 struct audio_buf_info abinfo;
426 struct count_info cntinfo;
427 int bufsize;
429 bufsize = hw->samples << hw->info.shift;
431 if (oss->mmapped) {
432 int bytes, pos;
434 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
435 if (err < 0) {
436 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
437 return 0;
440 pos = hw->rpos << hw->info.shift;
441 bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize);
442 decr = audio_MIN (bytes >> hw->info.shift, live);
444 else {
445 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
446 if (err < 0) {
447 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
448 return 0;
451 if (abinfo.bytes > bufsize) {
452 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
453 abinfo.bytes = bufsize;
456 if (abinfo.bytes < 0) {
457 trace_oss_invalid_available_size(abinfo.bytes, bufsize);
458 return 0;
461 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
462 if (!decr) {
463 return 0;
467 decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending);
468 oss->pending += decr;
469 oss_write_pending (oss);
471 return decr;
474 static void oss_fini_out (HWVoiceOut *hw)
476 int err;
477 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
479 ldebug ("oss_fini\n");
480 oss_anal_close (&oss->fd);
482 if (oss->pcm_buf) {
483 if (oss->mmapped) {
484 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
485 if (err) {
486 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
487 oss->pcm_buf, hw->samples << hw->info.shift);
490 else {
491 g_free (oss->pcm_buf);
493 oss->pcm_buf = NULL;
497 static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
498 void *drv_opaque)
500 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
501 struct oss_params req, obt;
502 int endianness;
503 int err;
504 int fd;
505 audfmt_e effective_fmt;
506 struct audsettings obt_as;
507 OSSConf *conf = drv_opaque;
509 oss->fd = -1;
511 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
512 req.freq = as->freq;
513 req.nchannels = as->nchannels;
514 req.fragsize = conf->fragsize;
515 req.nfrags = conf->nfrags;
517 if (oss_open (0, &req, &obt, &fd, conf)) {
518 return -1;
521 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
522 if (err) {
523 oss_anal_close (&fd);
524 return -1;
527 obt_as.freq = obt.freq;
528 obt_as.nchannels = obt.nchannels;
529 obt_as.fmt = effective_fmt;
530 obt_as.endianness = endianness;
532 audio_pcm_init_info (&hw->info, &obt_as);
533 oss->nfrags = obt.nfrags;
534 oss->fragsize = obt.fragsize;
536 if (obt.nfrags * obt.fragsize & hw->info.align) {
537 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
538 obt.nfrags * obt.fragsize, hw->info.align + 1);
541 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
543 oss->mmapped = 0;
544 if (conf->try_mmap) {
545 oss->pcm_buf = mmap (
546 NULL,
547 hw->samples << hw->info.shift,
548 PROT_READ | PROT_WRITE,
549 MAP_SHARED,
553 if (oss->pcm_buf == MAP_FAILED) {
554 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
555 hw->samples << hw->info.shift);
557 else {
558 int err;
559 int trig = 0;
560 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
561 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
563 else {
564 trig = PCM_ENABLE_OUTPUT;
565 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
566 oss_logerr (
567 errno,
568 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
571 else {
572 oss->mmapped = 1;
576 if (!oss->mmapped) {
577 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
578 if (err) {
579 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
580 oss->pcm_buf, hw->samples << hw->info.shift);
586 if (!oss->mmapped) {
587 oss->pcm_buf = audio_calloc (
588 AUDIO_FUNC,
589 hw->samples,
590 1 << hw->info.shift
592 if (!oss->pcm_buf) {
593 dolog (
594 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
595 hw->samples,
596 1 << hw->info.shift
598 oss_anal_close (&fd);
599 return -1;
603 oss->fd = fd;
604 oss->conf = conf;
605 return 0;
608 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
610 int trig;
611 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
613 switch (cmd) {
614 case VOICE_ENABLE:
616 va_list ap;
617 int poll_mode;
619 va_start (ap, cmd);
620 poll_mode = va_arg (ap, int);
621 va_end (ap);
623 ldebug ("enabling voice\n");
624 if (poll_mode) {
625 oss_poll_out (hw);
626 poll_mode = 0;
628 hw->poll_mode = poll_mode;
630 if (!oss->mmapped) {
631 return 0;
634 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
635 trig = PCM_ENABLE_OUTPUT;
636 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
637 oss_logerr (
638 errno,
639 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
641 return -1;
644 break;
646 case VOICE_DISABLE:
647 if (hw->poll_mode) {
648 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
649 hw->poll_mode = 0;
652 if (!oss->mmapped) {
653 return 0;
656 ldebug ("disabling voice\n");
657 trig = 0;
658 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
659 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
660 return -1;
662 break;
664 return 0;
667 static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
669 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
670 struct oss_params req, obt;
671 int endianness;
672 int err;
673 int fd;
674 audfmt_e effective_fmt;
675 struct audsettings obt_as;
676 OSSConf *conf = drv_opaque;
678 oss->fd = -1;
680 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
681 req.freq = as->freq;
682 req.nchannels = as->nchannels;
683 req.fragsize = conf->fragsize;
684 req.nfrags = conf->nfrags;
685 if (oss_open (1, &req, &obt, &fd, conf)) {
686 return -1;
689 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
690 if (err) {
691 oss_anal_close (&fd);
692 return -1;
695 obt_as.freq = obt.freq;
696 obt_as.nchannels = obt.nchannels;
697 obt_as.fmt = effective_fmt;
698 obt_as.endianness = endianness;
700 audio_pcm_init_info (&hw->info, &obt_as);
701 oss->nfrags = obt.nfrags;
702 oss->fragsize = obt.fragsize;
704 if (obt.nfrags * obt.fragsize & hw->info.align) {
705 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
706 obt.nfrags * obt.fragsize, hw->info.align + 1);
709 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
710 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
711 if (!oss->pcm_buf) {
712 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
713 hw->samples, 1 << hw->info.shift);
714 oss_anal_close (&fd);
715 return -1;
718 oss->fd = fd;
719 oss->conf = conf;
720 return 0;
723 static void oss_fini_in (HWVoiceIn *hw)
725 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
727 oss_anal_close (&oss->fd);
729 g_free(oss->pcm_buf);
730 oss->pcm_buf = NULL;
733 static int oss_run_in (HWVoiceIn *hw)
735 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
736 int hwshift = hw->info.shift;
737 int i;
738 int live = audio_pcm_hw_get_live_in (hw);
739 int dead = hw->samples - live;
740 size_t read_samples = 0;
741 struct {
742 int add;
743 int len;
744 } bufs[2] = {
745 { .add = hw->wpos, .len = 0 },
746 { .add = 0, .len = 0 }
749 if (!dead) {
750 return 0;
753 if (hw->wpos + dead > hw->samples) {
754 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
755 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
757 else {
758 bufs[0].len = dead << hwshift;
761 for (i = 0; i < 2; ++i) {
762 ssize_t nread;
764 if (bufs[i].len) {
765 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
766 nread = read (oss->fd, p, bufs[i].len);
768 if (nread > 0) {
769 if (nread & hw->info.align) {
770 dolog ("warning: Misaligned read %zd (requested %d), "
771 "alignment %d\n", nread, bufs[i].add << hwshift,
772 hw->info.align + 1);
774 read_samples += nread >> hwshift;
775 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift);
778 if (bufs[i].len - nread) {
779 if (nread == -1) {
780 switch (errno) {
781 case EINTR:
782 case EAGAIN:
783 break;
784 default:
785 oss_logerr (
786 errno,
787 "Failed to read %d bytes of audio (to %p)\n",
788 bufs[i].len, p
790 break;
793 break;
798 hw->wpos = (hw->wpos + read_samples) % hw->samples;
799 return read_samples;
802 static int oss_read (SWVoiceIn *sw, void *buf, int size)
804 return audio_pcm_sw_read (sw, buf, size);
807 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
809 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
811 switch (cmd) {
812 case VOICE_ENABLE:
814 va_list ap;
815 int poll_mode;
817 va_start (ap, cmd);
818 poll_mode = va_arg (ap, int);
819 va_end (ap);
821 if (poll_mode) {
822 oss_poll_in (hw);
823 poll_mode = 0;
825 hw->poll_mode = poll_mode;
827 break;
829 case VOICE_DISABLE:
830 if (hw->poll_mode) {
831 hw->poll_mode = 0;
832 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
834 break;
836 return 0;
839 static OSSConf glob_conf = {
840 .try_mmap = 0,
841 .nfrags = 4,
842 .fragsize = 4096,
843 .devpath_out = "/dev/dsp",
844 .devpath_in = "/dev/dsp",
845 .exclusive = 0,
846 .policy = 5
849 static void *oss_audio_init (void)
851 OSSConf *conf = g_malloc(sizeof(OSSConf));
852 *conf = glob_conf;
854 if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
855 access(conf->devpath_out, R_OK | W_OK) < 0) {
856 g_free(conf);
857 return NULL;
859 return conf;
862 static void oss_audio_fini (void *opaque)
864 g_free(opaque);
867 static struct audio_option oss_options[] = {
869 .name = "FRAGSIZE",
870 .tag = AUD_OPT_INT,
871 .valp = &glob_conf.fragsize,
872 .descr = "Fragment size in bytes"
875 .name = "NFRAGS",
876 .tag = AUD_OPT_INT,
877 .valp = &glob_conf.nfrags,
878 .descr = "Number of fragments"
881 .name = "MMAP",
882 .tag = AUD_OPT_BOOL,
883 .valp = &glob_conf.try_mmap,
884 .descr = "Try using memory mapped access"
887 .name = "DAC_DEV",
888 .tag = AUD_OPT_STR,
889 .valp = &glob_conf.devpath_out,
890 .descr = "Path to DAC device"
893 .name = "ADC_DEV",
894 .tag = AUD_OPT_STR,
895 .valp = &glob_conf.devpath_in,
896 .descr = "Path to ADC device"
899 .name = "EXCLUSIVE",
900 .tag = AUD_OPT_BOOL,
901 .valp = &glob_conf.exclusive,
902 .descr = "Open device in exclusive mode (vmix wont work)"
904 #ifdef USE_DSP_POLICY
906 .name = "POLICY",
907 .tag = AUD_OPT_INT,
908 .valp = &glob_conf.policy,
909 .descr = "Set the timing policy of the device, -1 to use fragment mode",
911 #endif
912 { /* End of list */ }
915 static struct audio_pcm_ops oss_pcm_ops = {
916 .init_out = oss_init_out,
917 .fini_out = oss_fini_out,
918 .run_out = oss_run_out,
919 .write = oss_write,
920 .ctl_out = oss_ctl_out,
922 .init_in = oss_init_in,
923 .fini_in = oss_fini_in,
924 .run_in = oss_run_in,
925 .read = oss_read,
926 .ctl_in = oss_ctl_in
929 struct audio_driver oss_audio_driver = {
930 .name = "oss",
931 .descr = "OSS http://www.opensound.com",
932 .options = oss_options,
933 .init = oss_audio_init,
934 .fini = oss_audio_fini,
935 .pcm_ops = &oss_pcm_ops,
936 .can_be_default = 1,
937 .max_voices_out = INT_MAX,
938 .max_voices_in = INT_MAX,
939 .voice_size_out = sizeof (OSSVoiceOut),
940 .voice_size_in = sizeof (OSSVoiceIn)