ppc/pnv: Loop on the whole hierarchy to populate the DT with the XSCOM nodes
[qemu/rayw.git] / audio / ossaudio.c
blobc43faeeea4aa208c9729cc760dcd129705d5521d
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.
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 "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 OSSVoiceOut {
42 HWVoiceOut hw;
43 int fd;
44 int nfrags;
45 int fragsize;
46 int mmapped;
47 Audiodev *dev;
48 } OSSVoiceOut;
50 typedef struct OSSVoiceIn {
51 HWVoiceIn hw;
52 int fd;
53 int nfrags;
54 int fragsize;
55 Audiodev *dev;
56 } OSSVoiceIn;
58 struct oss_params {
59 int freq;
60 int fmt;
61 int nchannels;
62 int nfrags;
63 int fragsize;
66 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
68 va_list ap;
70 va_start (ap, fmt);
71 AUD_vlog (AUDIO_CAP, fmt, ap);
72 va_end (ap);
74 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
77 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
78 int err,
79 const char *typ,
80 const char *fmt,
81 ...
84 va_list ap;
86 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
88 va_start (ap, fmt);
89 AUD_vlog (AUDIO_CAP, fmt, ap);
90 va_end (ap);
92 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
95 static void oss_anal_close (int *fdp)
97 int err;
99 qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
100 err = close (*fdp);
101 if (err) {
102 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
104 *fdp = -1;
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)
135 switch (fmt) {
136 case AUDIO_FORMAT_S8:
137 return AFMT_S8;
139 case AUDIO_FORMAT_U8:
140 return AFMT_U8;
142 case AUDIO_FORMAT_S16:
143 if (endianness) {
144 return AFMT_S16_BE;
146 else {
147 return AFMT_S16_LE;
150 case AUDIO_FORMAT_U16:
151 if (endianness) {
152 return AFMT_U16_BE;
154 else {
155 return AFMT_U16_LE;
158 default:
159 dolog ("Internal logic error: Bad audio format %d\n", fmt);
160 #ifdef DEBUG_AUDIO
161 abort ();
162 #endif
163 return AFMT_U8;
167 static int oss_to_audfmt (int ossfmt, AudioFormat *fmt, int *endianness)
169 switch (ossfmt) {
170 case AFMT_S8:
171 *endianness = 0;
172 *fmt = AUDIO_FORMAT_S8;
173 break;
175 case AFMT_U8:
176 *endianness = 0;
177 *fmt = AUDIO_FORMAT_U8;
178 break;
180 case AFMT_S16_LE:
181 *endianness = 0;
182 *fmt = AUDIO_FORMAT_S16;
183 break;
185 case AFMT_U16_LE:
186 *endianness = 0;
187 *fmt = AUDIO_FORMAT_U16;
188 break;
190 case AFMT_S16_BE:
191 *endianness = 1;
192 *fmt = AUDIO_FORMAT_S16;
193 break;
195 case AFMT_U16_BE:
196 *endianness = 1;
197 *fmt = AUDIO_FORMAT_U16;
198 break;
200 default:
201 dolog ("Unrecognized audio format %d\n", ossfmt);
202 return -1;
205 return 0;
208 #if defined DEBUG_MISMATCHES || defined DEBUG
209 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
211 dolog ("parameter | requested value | obtained value\n");
212 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
213 dolog ("channels | %10d | %10d\n",
214 req->nchannels, obt->nchannels);
215 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
216 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
217 dolog ("fragsize | %10d | %10d\n",
218 req->fragsize, obt->fragsize);
220 #endif
222 #ifdef USE_DSP_POLICY
223 static int oss_get_version (int fd, int *version, const char *typ)
225 if (ioctl (fd, OSS_GETVERSION, &version)) {
226 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
228 * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
229 * since 7.x, but currently only on the mixer device (or in
230 * the Linuxolator), and in the native version that part of
231 * the code is in fact never reached so the ioctl fails anyway.
232 * Until this is fixed, just check the errno and if its what
233 * FreeBSD's sound drivers return atm assume they are new enough.
235 if (errno == EINVAL) {
236 *version = 0x040000;
237 return 0;
239 #endif
240 oss_logerr2 (errno, typ, "Failed to get OSS version\n");
241 return -1;
243 return 0;
245 #endif
247 static int oss_open(int in, struct oss_params *req, audsettings *as,
248 struct oss_params *obt, int *pfd, Audiodev *dev)
250 AudiodevOssOptions *oopts = &dev->u.oss;
251 AudiodevOssPerDirectionOptions *opdo = in ? oopts->in : oopts->out;
252 int fd;
253 int oflags = (oopts->has_exclusive && oopts->exclusive) ? O_EXCL : 0;
254 audio_buf_info abinfo;
255 int fmt, freq, nchannels;
256 int setfragment = 1;
257 const char *dspname = opdo->has_dev ? opdo->dev : "/dev/dsp";
258 const char *typ = in ? "ADC" : "DAC";
259 #ifdef USE_DSP_POLICY
260 int policy = oopts->has_dsp_policy ? oopts->dsp_policy : 5;
261 #endif
263 /* Kludge needed to have working mmap on Linux */
264 oflags |= (oopts->has_try_mmap && oopts->try_mmap) ?
265 O_RDWR : (in ? O_RDONLY : O_WRONLY);
267 fd = open (dspname, oflags | O_NONBLOCK);
268 if (-1 == fd) {
269 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
270 return -1;
273 freq = req->freq;
274 nchannels = req->nchannels;
275 fmt = req->fmt;
276 req->nfrags = opdo->has_buffer_count ? opdo->buffer_count : 4;
277 req->fragsize = audio_buffer_bytes(
278 qapi_AudiodevOssPerDirectionOptions_base(opdo), as, 23220);
280 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
281 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
282 goto err;
285 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
286 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
287 req->nchannels);
288 goto err;
291 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
292 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
293 goto err;
296 if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
297 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
298 goto err;
301 #ifdef USE_DSP_POLICY
302 if (policy >= 0) {
303 int version;
305 if (!oss_get_version (fd, &version, typ)) {
306 trace_oss_version(version);
308 if (version >= 0x040000) {
309 int policy2 = policy;
310 if (ioctl(fd, SNDCTL_DSP_POLICY, &policy2)) {
311 oss_logerr2 (errno, typ,
312 "Failed to set timing policy to %d\n",
313 policy);
314 goto err;
316 setfragment = 0;
320 #endif
322 if (setfragment) {
323 int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
324 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
325 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
326 req->nfrags, req->fragsize);
327 goto err;
331 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
332 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
333 goto err;
336 if (!abinfo.fragstotal || !abinfo.fragsize) {
337 AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
338 abinfo.fragstotal, abinfo.fragsize, typ);
339 goto err;
342 obt->fmt = fmt;
343 obt->nchannels = nchannels;
344 obt->freq = freq;
345 obt->nfrags = abinfo.fragstotal;
346 obt->fragsize = abinfo.fragsize;
347 *pfd = fd;
349 #ifdef DEBUG_MISMATCHES
350 if ((req->fmt != obt->fmt) ||
351 (req->nchannels != obt->nchannels) ||
352 (req->freq != obt->freq) ||
353 (req->fragsize != obt->fragsize) ||
354 (req->nfrags != obt->nfrags)) {
355 dolog ("Audio parameters mismatch\n");
356 oss_dump_info (req, obt);
358 #endif
360 #ifdef DEBUG
361 oss_dump_info (req, obt);
362 #endif
363 return 0;
365 err:
366 oss_anal_close (&fd);
367 return -1;
370 static size_t oss_get_available_bytes(OSSVoiceOut *oss)
372 int err;
373 struct count_info cntinfo;
374 assert(oss->mmapped);
376 err = ioctl(oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
377 if (err < 0) {
378 oss_logerr(errno, "SNDCTL_DSP_GETOPTR failed\n");
379 return 0;
382 return audio_ring_dist(cntinfo.ptr, oss->hw.pos_emul, oss->hw.size_emul);
385 static void *oss_get_buffer_out(HWVoiceOut *hw, size_t *size)
387 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
388 if (oss->mmapped) {
389 *size = MIN(oss_get_available_bytes(oss), hw->size_emul - hw->pos_emul);
390 return hw->buf_emul + hw->pos_emul;
391 } else {
392 return audio_generic_get_buffer_out(hw, size);
396 static size_t oss_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
398 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
399 if (oss->mmapped) {
400 assert(buf == hw->buf_emul + hw->pos_emul && size < hw->size_emul);
402 hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
403 return size;
404 } else {
405 return audio_generic_put_buffer_out(hw, buf, size);
409 static size_t oss_write(HWVoiceOut *hw, void *buf, size_t len)
411 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
412 size_t pos;
414 if (oss->mmapped) {
415 size_t total_len;
416 len = MIN(len, oss_get_available_bytes(oss));
418 total_len = len;
419 while (len) {
420 size_t to_copy = MIN(len, hw->size_emul - hw->pos_emul);
421 memcpy(hw->buf_emul + hw->pos_emul, buf, to_copy);
423 hw->pos_emul = (hw->pos_emul + to_copy) % hw->pos_emul;
424 buf += to_copy;
425 len -= to_copy;
427 return total_len;
430 pos = 0;
431 while (len) {
432 ssize_t bytes_written;
433 void *pcm = advance(buf, pos);
435 bytes_written = write(oss->fd, pcm, len);
436 if (bytes_written < 0) {
437 if (errno != EAGAIN) {
438 oss_logerr(errno, "failed to write %zu bytes\n",
439 len);
441 return pos;
444 pos += bytes_written;
445 if (bytes_written < len) {
446 break;
448 len -= bytes_written;
450 return pos;
453 static void oss_fini_out (HWVoiceOut *hw)
455 int err;
456 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
458 ldebug ("oss_fini\n");
459 oss_anal_close (&oss->fd);
461 if (oss->mmapped && hw->buf_emul) {
462 err = munmap(hw->buf_emul, hw->size_emul);
463 if (err) {
464 oss_logerr(errno, "Failed to unmap buffer %p, size %zu\n",
465 hw->buf_emul, hw->size_emul);
467 hw->buf_emul = NULL;
471 static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
472 void *drv_opaque)
474 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
475 struct oss_params req, obt;
476 int endianness;
477 int err;
478 int fd;
479 AudioFormat effective_fmt;
480 struct audsettings obt_as;
481 Audiodev *dev = drv_opaque;
482 AudiodevOssOptions *oopts = &dev->u.oss;
484 oss->fd = -1;
486 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
487 req.freq = as->freq;
488 req.nchannels = as->nchannels;
490 if (oss_open(0, &req, as, &obt, &fd, dev)) {
491 return -1;
494 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
495 if (err) {
496 oss_anal_close (&fd);
497 return -1;
500 obt_as.freq = obt.freq;
501 obt_as.nchannels = obt.nchannels;
502 obt_as.fmt = effective_fmt;
503 obt_as.endianness = endianness;
505 audio_pcm_init_info (&hw->info, &obt_as);
506 oss->nfrags = obt.nfrags;
507 oss->fragsize = obt.fragsize;
509 if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) {
510 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
511 obt.nfrags * obt.fragsize, hw->info.bytes_per_frame);
514 hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame;
516 oss->mmapped = 0;
517 if (oopts->has_try_mmap && oopts->try_mmap) {
518 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
519 hw->buf_emul = mmap(
520 NULL,
521 hw->size_emul,
522 PROT_READ | PROT_WRITE,
523 MAP_SHARED,
527 if (hw->buf_emul == MAP_FAILED) {
528 oss_logerr(errno, "Failed to map %zu bytes of DAC\n",
529 hw->size_emul);
530 hw->buf_emul = NULL;
531 } else {
532 int err;
533 int trig = 0;
534 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
535 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
537 else {
538 trig = PCM_ENABLE_OUTPUT;
539 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
540 oss_logerr (
541 errno,
542 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
545 else {
546 oss->mmapped = 1;
550 if (!oss->mmapped) {
551 err = munmap(hw->buf_emul, hw->size_emul);
552 if (err) {
553 oss_logerr(errno, "Failed to unmap buffer %p size %zu\n",
554 hw->buf_emul, hw->size_emul);
556 hw->buf_emul = NULL;
561 oss->fd = fd;
562 oss->dev = dev;
563 return 0;
566 static void oss_enable_out(HWVoiceOut *hw, bool enable)
568 int trig;
569 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
570 AudiodevOssPerDirectionOptions *opdo = oss->dev->u.oss.out;
572 if (enable) {
573 bool poll_mode = opdo->try_poll;
575 ldebug("enabling voice\n");
576 if (poll_mode) {
577 oss_poll_out(hw);
578 poll_mode = 0;
580 hw->poll_mode = poll_mode;
582 if (!oss->mmapped) {
583 return;
586 audio_pcm_info_clear_buf(&hw->info, hw->buf_emul, hw->mix_buf->size);
587 trig = PCM_ENABLE_OUTPUT;
588 if (ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
589 oss_logerr(errno,
590 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n");
591 return;
593 } else {
594 if (hw->poll_mode) {
595 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
596 hw->poll_mode = 0;
599 if (!oss->mmapped) {
600 return;
603 ldebug ("disabling voice\n");
604 trig = 0;
605 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
606 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
607 return;
612 static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
614 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
615 struct oss_params req, obt;
616 int endianness;
617 int err;
618 int fd;
619 AudioFormat effective_fmt;
620 struct audsettings obt_as;
621 Audiodev *dev = drv_opaque;
623 oss->fd = -1;
625 req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
626 req.freq = as->freq;
627 req.nchannels = as->nchannels;
628 if (oss_open(1, &req, as, &obt, &fd, dev)) {
629 return -1;
632 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
633 if (err) {
634 oss_anal_close (&fd);
635 return -1;
638 obt_as.freq = obt.freq;
639 obt_as.nchannels = obt.nchannels;
640 obt_as.fmt = effective_fmt;
641 obt_as.endianness = endianness;
643 audio_pcm_init_info (&hw->info, &obt_as);
644 oss->nfrags = obt.nfrags;
645 oss->fragsize = obt.fragsize;
647 if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) {
648 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
649 obt.nfrags * obt.fragsize, hw->info.bytes_per_frame);
652 hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame;
654 oss->fd = fd;
655 oss->dev = dev;
656 return 0;
659 static void oss_fini_in (HWVoiceIn *hw)
661 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
663 oss_anal_close (&oss->fd);
666 static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len)
668 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
669 size_t pos = 0;
671 while (len) {
672 ssize_t nread;
674 void *dst = advance(buf, pos);
675 nread = read(oss->fd, dst, len);
677 if (nread == -1) {
678 switch (errno) {
679 case EINTR:
680 case EAGAIN:
681 break;
682 default:
683 oss_logerr(errno, "Failed to read %zu bytes of audio (to %p)\n",
684 len, dst);
685 break;
689 pos += nread;
690 len -= nread;
693 return pos;
696 static void oss_enable_in(HWVoiceIn *hw, bool enable)
698 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
699 AudiodevOssPerDirectionOptions *opdo = oss->dev->u.oss.out;
701 if (enable) {
702 bool poll_mode = opdo->try_poll;
704 if (poll_mode) {
705 oss_poll_in(hw);
706 poll_mode = 0;
708 hw->poll_mode = poll_mode;
709 } else {
710 if (hw->poll_mode) {
711 hw->poll_mode = 0;
712 qemu_set_fd_handler (oss->fd, NULL, NULL, NULL);
717 static void oss_init_per_direction(AudiodevOssPerDirectionOptions *opdo)
719 if (!opdo->has_try_poll) {
720 opdo->try_poll = true;
721 opdo->has_try_poll = true;
725 static void *oss_audio_init(Audiodev *dev)
727 AudiodevOssOptions *oopts;
728 assert(dev->driver == AUDIODEV_DRIVER_OSS);
730 oopts = &dev->u.oss;
731 oss_init_per_direction(oopts->in);
732 oss_init_per_direction(oopts->out);
734 if (access(oopts->in->has_dev ? oopts->in->dev : "/dev/dsp",
735 R_OK | W_OK) < 0 ||
736 access(oopts->out->has_dev ? oopts->out->dev : "/dev/dsp",
737 R_OK | W_OK) < 0) {
738 return NULL;
740 return dev;
743 static void oss_audio_fini (void *opaque)
747 static struct audio_pcm_ops oss_pcm_ops = {
748 .init_out = oss_init_out,
749 .fini_out = oss_fini_out,
750 .write = oss_write,
751 .get_buffer_out = oss_get_buffer_out,
752 .put_buffer_out = oss_put_buffer_out,
753 .enable_out = oss_enable_out,
755 .init_in = oss_init_in,
756 .fini_in = oss_fini_in,
757 .read = oss_read,
758 .enable_in = oss_enable_in
761 static struct audio_driver oss_audio_driver = {
762 .name = "oss",
763 .descr = "OSS http://www.opensound.com",
764 .init = oss_audio_init,
765 .fini = oss_audio_fini,
766 .pcm_ops = &oss_pcm_ops,
767 .can_be_default = 1,
768 .max_voices_out = INT_MAX,
769 .max_voices_in = INT_MAX,
770 .voice_size_out = sizeof (OSSVoiceOut),
771 .voice_size_in = sizeof (OSSVoiceIn)
774 static void register_audio_oss(void)
776 audio_driver_register(&oss_audio_driver);
778 type_init(register_audio_oss);