New -cpu options: choose CPU model for emulated target.
[qemu/mini2440.git] / audio / ossaudio.c
blob125e4c8ff79395e2eb875d9bfb135dba5664045c
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 <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "vl.h"
30 #define AUDIO_CAP "oss"
31 #include "audio_int.h"
33 typedef struct OSSVoiceOut {
34 HWVoiceOut hw;
35 void *pcm_buf;
36 int fd;
37 int nfrags;
38 int fragsize;
39 int mmapped;
40 int old_optr;
41 } OSSVoiceOut;
43 typedef struct OSSVoiceIn {
44 HWVoiceIn hw;
45 void *pcm_buf;
46 int fd;
47 int nfrags;
48 int fragsize;
49 int old_optr;
50 } OSSVoiceIn;
52 static struct {
53 int try_mmap;
54 int nfrags;
55 int fragsize;
56 const char *devpath_out;
57 const char *devpath_in;
58 int debug;
59 } conf = {
60 .try_mmap = 0,
61 .nfrags = 4,
62 .fragsize = 4096,
63 .devpath_out = "/dev/dsp",
64 .devpath_in = "/dev/dsp",
65 .debug = 0
68 struct oss_params {
69 int freq;
70 audfmt_e fmt;
71 int nchannels;
72 int nfrags;
73 int fragsize;
76 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
78 va_list ap;
80 va_start (ap, fmt);
81 AUD_vlog (AUDIO_CAP, fmt, ap);
82 va_end (ap);
84 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
87 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
88 int err,
89 const char *typ,
90 const char *fmt,
91 ...
94 va_list ap;
96 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
98 va_start (ap, fmt);
99 AUD_vlog (AUDIO_CAP, fmt, ap);
100 va_end (ap);
102 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
105 static void oss_anal_close (int *fdp)
107 int err = close (*fdp);
108 if (err) {
109 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
111 *fdp = -1;
114 static int oss_write (SWVoiceOut *sw, void *buf, int len)
116 return audio_pcm_sw_write (sw, buf, len);
119 static int aud_to_ossfmt (audfmt_e fmt)
121 switch (fmt) {
122 case AUD_FMT_S8:
123 return AFMT_S8;
125 case AUD_FMT_U8:
126 return AFMT_U8;
128 case AUD_FMT_S16:
129 return AFMT_S16_LE;
131 case AUD_FMT_U16:
132 return AFMT_U16_LE;
134 default:
135 dolog ("Internal logic error: Bad audio format %d\n", fmt);
136 #ifdef DEBUG_AUDIO
137 abort ();
138 #endif
139 return AFMT_U8;
143 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
145 switch (ossfmt) {
146 case AFMT_S8:
147 *endianness =0;
148 *fmt = AUD_FMT_S8;
149 break;
151 case AFMT_U8:
152 *endianness = 0;
153 *fmt = AUD_FMT_U8;
154 break;
156 case AFMT_S16_LE:
157 *endianness = 0;
158 *fmt = AUD_FMT_S16;
159 break;
161 case AFMT_U16_LE:
162 *endianness = 0;
163 *fmt = AUD_FMT_U16;
164 break;
166 case AFMT_S16_BE:
167 *endianness = 1;
168 *fmt = AUD_FMT_S16;
169 break;
171 case AFMT_U16_BE:
172 *endianness = 1;
173 *fmt = AUD_FMT_U16;
174 break;
176 default:
177 dolog ("Unrecognized audio format %d\n", ossfmt);
178 return -1;
181 return 0;
184 #if defined DEBUG_MISMATCHES || defined DEBUG
185 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
187 dolog ("parameter | requested value | obtained value\n");
188 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
189 dolog ("channels | %10d | %10d\n",
190 req->nchannels, obt->nchannels);
191 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
192 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
193 dolog ("fragsize | %10d | %10d\n",
194 req->fragsize, obt->fragsize);
196 #endif
198 static int oss_open (int in, struct oss_params *req,
199 struct oss_params *obt, int *pfd)
201 int fd;
202 int mmmmssss;
203 audio_buf_info abinfo;
204 int fmt, freq, nchannels;
205 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
206 const char *typ = in ? "ADC" : "DAC";
208 fd = open (dspname, (in ? O_RDONLY : O_WRONLY) | O_NONBLOCK);
209 if (-1 == fd) {
210 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
211 return -1;
214 freq = req->freq;
215 nchannels = req->nchannels;
216 fmt = req->fmt;
218 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
219 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
220 goto err;
223 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
224 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
225 req->nchannels);
226 goto err;
229 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
230 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
231 goto err;
234 if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) {
235 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
236 goto err;
239 mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize);
240 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
241 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
242 req->nfrags, req->fragsize);
243 goto err;
246 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
247 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
248 goto err;
251 obt->fmt = fmt;
252 obt->nchannels = nchannels;
253 obt->freq = freq;
254 obt->nfrags = abinfo.fragstotal;
255 obt->fragsize = abinfo.fragsize;
256 *pfd = fd;
258 #ifdef DEBUG_MISMATCHES
259 if ((req->fmt != obt->fmt) ||
260 (req->nchannels != obt->nchannels) ||
261 (req->freq != obt->freq) ||
262 (req->fragsize != obt->fragsize) ||
263 (req->nfrags != obt->nfrags)) {
264 dolog ("Audio parameters mismatch\n");
265 oss_dump_info (req, obt);
267 #endif
269 #ifdef DEBUG
270 oss_dump_info (req, obt);
271 #endif
272 return 0;
274 err:
275 oss_anal_close (&fd);
276 return -1;
279 static int oss_run_out (HWVoiceOut *hw)
281 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
282 int err, rpos, live, decr;
283 int samples;
284 uint8_t *dst;
285 st_sample_t *src;
286 struct audio_buf_info abinfo;
287 struct count_info cntinfo;
288 int bufsize;
290 live = audio_pcm_hw_get_live_out (hw);
291 if (!live) {
292 return 0;
295 bufsize = hw->samples << hw->info.shift;
297 if (oss->mmapped) {
298 int bytes;
300 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
301 if (err < 0) {
302 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
303 return 0;
306 if (cntinfo.ptr == oss->old_optr) {
307 if (abs (hw->samples - live) < 64) {
308 dolog ("warning: Overrun\n");
310 return 0;
313 if (cntinfo.ptr > oss->old_optr) {
314 bytes = cntinfo.ptr - oss->old_optr;
316 else {
317 bytes = bufsize + cntinfo.ptr - oss->old_optr;
320 decr = audio_MIN (bytes >> hw->info.shift, live);
322 else {
323 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
324 if (err < 0) {
325 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
326 return 0;
329 if (abinfo.bytes > bufsize) {
330 if (conf.debug) {
331 dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
332 "please report your OS/audio hw to malc@pulsesoft.com\n",
333 abinfo.bytes, bufsize);
335 abinfo.bytes = bufsize;
338 if (abinfo.bytes < 0) {
339 if (conf.debug) {
340 dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
341 abinfo.bytes, bufsize);
343 return 0;
346 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
347 if (!decr) {
348 return 0;
352 samples = decr;
353 rpos = hw->rpos;
354 while (samples) {
355 int left_till_end_samples = hw->samples - rpos;
356 int convert_samples = audio_MIN (samples, left_till_end_samples);
358 src = hw->mix_buf + rpos;
359 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
361 hw->clip (dst, src, convert_samples);
362 if (!oss->mmapped) {
363 int written;
365 written = write (oss->fd, dst, convert_samples << hw->info.shift);
366 /* XXX: follow errno recommendations ? */
367 if (written == -1) {
368 oss_logerr (
369 errno,
370 "Failed to write %d bytes of audio data from %p\n",
371 convert_samples << hw->info.shift,
374 continue;
377 if (written != convert_samples << hw->info.shift) {
378 int wsamples = written >> hw->info.shift;
379 int wbytes = wsamples << hw->info.shift;
380 if (wbytes != written) {
381 dolog ("warning: Misaligned write %d (requested %d), "
382 "alignment %d\n",
383 wbytes, written, hw->info.align + 1);
385 decr -= wsamples;
386 rpos = (rpos + wsamples) % hw->samples;
387 break;
391 rpos = (rpos + convert_samples) % hw->samples;
392 samples -= convert_samples;
394 if (oss->mmapped) {
395 oss->old_optr = cntinfo.ptr;
398 hw->rpos = rpos;
399 return decr;
402 static void oss_fini_out (HWVoiceOut *hw)
404 int err;
405 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
407 ldebug ("oss_fini\n");
408 oss_anal_close (&oss->fd);
410 if (oss->pcm_buf) {
411 if (oss->mmapped) {
412 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
413 if (err) {
414 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
415 oss->pcm_buf, hw->samples << hw->info.shift);
418 else {
419 qemu_free (oss->pcm_buf);
421 oss->pcm_buf = NULL;
425 static int oss_init_out (HWVoiceOut *hw, audsettings_t *as)
427 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
428 struct oss_params req, obt;
429 int endianness;
430 int err;
431 int fd;
432 audfmt_e effective_fmt;
433 audsettings_t obt_as;
435 oss->fd = -1;
437 req.fmt = aud_to_ossfmt (as->fmt);
438 req.freq = as->freq;
439 req.nchannels = as->nchannels;
440 req.fragsize = conf.fragsize;
441 req.nfrags = conf.nfrags;
443 if (oss_open (0, &req, &obt, &fd)) {
444 return -1;
447 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
448 if (err) {
449 oss_anal_close (&fd);
450 return -1;
453 obt_as.freq = obt.freq;
454 obt_as.nchannels = obt.nchannels;
455 obt_as.fmt = effective_fmt;
456 obt_as.endianness = endianness;
458 audio_pcm_init_info (&hw->info, &obt_as);
459 oss->nfrags = obt.nfrags;
460 oss->fragsize = obt.fragsize;
462 if (obt.nfrags * obt.fragsize & hw->info.align) {
463 dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
464 obt.nfrags * obt.fragsize, hw->info.align + 1);
467 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
469 oss->mmapped = 0;
470 if (conf.try_mmap) {
471 oss->pcm_buf = mmap (
473 hw->samples << hw->info.shift,
474 PROT_READ | PROT_WRITE,
475 MAP_SHARED,
479 if (oss->pcm_buf == MAP_FAILED) {
480 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
481 hw->samples << hw->info.shift);
482 } else {
483 int err;
484 int trig = 0;
485 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
486 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
488 else {
489 trig = PCM_ENABLE_OUTPUT;
490 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
491 oss_logerr (
492 errno,
493 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
496 else {
497 oss->mmapped = 1;
501 if (!oss->mmapped) {
502 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
503 if (err) {
504 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
505 oss->pcm_buf, hw->samples << hw->info.shift);
511 if (!oss->mmapped) {
512 oss->pcm_buf = audio_calloc (
513 AUDIO_FUNC,
514 hw->samples,
515 1 << hw->info.shift
517 if (!oss->pcm_buf) {
518 dolog (
519 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
520 hw->samples,
521 1 << hw->info.shift
523 oss_anal_close (&fd);
524 return -1;
528 oss->fd = fd;
529 return 0;
532 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
534 int trig;
535 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
537 if (!oss->mmapped) {
538 return 0;
541 switch (cmd) {
542 case VOICE_ENABLE:
543 ldebug ("enabling voice\n");
544 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
545 trig = PCM_ENABLE_OUTPUT;
546 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
547 oss_logerr (
548 errno,
549 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
551 return -1;
553 break;
555 case VOICE_DISABLE:
556 ldebug ("disabling voice\n");
557 trig = 0;
558 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
559 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
560 return -1;
562 break;
564 return 0;
567 static int oss_init_in (HWVoiceIn *hw, audsettings_t *as)
569 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
570 struct oss_params req, obt;
571 int endianness;
572 int err;
573 int fd;
574 audfmt_e effective_fmt;
575 audsettings_t obt_as;
577 oss->fd = -1;
579 req.fmt = aud_to_ossfmt (as->fmt);
580 req.freq = as->freq;
581 req.nchannels = as->nchannels;
582 req.fragsize = conf.fragsize;
583 req.nfrags = conf.nfrags;
584 if (oss_open (1, &req, &obt, &fd)) {
585 return -1;
588 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
589 if (err) {
590 oss_anal_close (&fd);
591 return -1;
594 obt_as.freq = obt.freq;
595 obt_as.nchannels = obt.nchannels;
596 obt_as.fmt = effective_fmt;
597 obt_as.endianness = endianness;
599 audio_pcm_init_info (&hw->info, &obt_as);
600 oss->nfrags = obt.nfrags;
601 oss->fragsize = obt.fragsize;
603 if (obt.nfrags * obt.fragsize & hw->info.align) {
604 dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
605 obt.nfrags * obt.fragsize, hw->info.align + 1);
608 hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
609 oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
610 if (!oss->pcm_buf) {
611 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
612 hw->samples, 1 << hw->info.shift);
613 oss_anal_close (&fd);
614 return -1;
617 oss->fd = fd;
618 return 0;
621 static void oss_fini_in (HWVoiceIn *hw)
623 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
625 oss_anal_close (&oss->fd);
627 if (oss->pcm_buf) {
628 qemu_free (oss->pcm_buf);
629 oss->pcm_buf = NULL;
633 static int oss_run_in (HWVoiceIn *hw)
635 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
636 int hwshift = hw->info.shift;
637 int i;
638 int live = audio_pcm_hw_get_live_in (hw);
639 int dead = hw->samples - live;
640 size_t read_samples = 0;
641 struct {
642 int add;
643 int len;
644 } bufs[2] = {
645 { hw->wpos, 0 },
646 { 0, 0 }
649 if (!dead) {
650 return 0;
653 if (hw->wpos + dead > hw->samples) {
654 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
655 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
657 else {
658 bufs[0].len = dead << hwshift;
662 for (i = 0; i < 2; ++i) {
663 ssize_t nread;
665 if (bufs[i].len) {
666 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
667 nread = read (oss->fd, p, bufs[i].len);
669 if (nread > 0) {
670 if (nread & hw->info.align) {
671 dolog ("warning: Misaligned read %zd (requested %d), "
672 "alignment %d\n", nread, bufs[i].add << hwshift,
673 hw->info.align + 1);
675 read_samples += nread >> hwshift;
676 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
677 &nominal_volume);
680 if (bufs[i].len - nread) {
681 if (nread == -1) {
682 switch (errno) {
683 case EINTR:
684 case EAGAIN:
685 break;
686 default:
687 oss_logerr (
688 errno,
689 "Failed to read %d bytes of audio (to %p)\n",
690 bufs[i].len, p
692 break;
695 break;
700 hw->wpos = (hw->wpos + read_samples) % hw->samples;
701 return read_samples;
704 static int oss_read (SWVoiceIn *sw, void *buf, int size)
706 return audio_pcm_sw_read (sw, buf, size);
709 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
711 (void) hw;
712 (void) cmd;
713 return 0;
716 static void *oss_audio_init (void)
718 return &conf;
721 static void oss_audio_fini (void *opaque)
723 (void) opaque;
726 static struct audio_option oss_options[] = {
727 {"FRAGSIZE", AUD_OPT_INT, &conf.fragsize,
728 "Fragment size in bytes", NULL, 0},
729 {"NFRAGS", AUD_OPT_INT, &conf.nfrags,
730 "Number of fragments", NULL, 0},
731 {"MMAP", AUD_OPT_BOOL, &conf.try_mmap,
732 "Try using memory mapped access", NULL, 0},
733 {"DAC_DEV", AUD_OPT_STR, &conf.devpath_out,
734 "Path to DAC device", NULL, 0},
735 {"ADC_DEV", AUD_OPT_STR, &conf.devpath_in,
736 "Path to ADC device", NULL, 0},
737 {"DEBUG", AUD_OPT_BOOL, &conf.debug,
738 "Turn on some debugging messages", NULL, 0},
739 {NULL, 0, NULL, NULL, NULL, 0}
742 static struct audio_pcm_ops oss_pcm_ops = {
743 oss_init_out,
744 oss_fini_out,
745 oss_run_out,
746 oss_write,
747 oss_ctl_out,
749 oss_init_in,
750 oss_fini_in,
751 oss_run_in,
752 oss_read,
753 oss_ctl_in
756 struct audio_driver oss_audio_driver = {
757 INIT_FIELD (name = ) "oss",
758 INIT_FIELD (descr = ) "OSS http://www.opensound.com",
759 INIT_FIELD (options = ) oss_options,
760 INIT_FIELD (init = ) oss_audio_init,
761 INIT_FIELD (fini = ) oss_audio_fini,
762 INIT_FIELD (pcm_ops = ) &oss_pcm_ops,
763 INIT_FIELD (can_be_default = ) 1,
764 INIT_FIELD (max_voices_out = ) INT_MAX,
765 INIT_FIELD (max_voices_in = ) INT_MAX,
766 INIT_FIELD (voice_size_out = ) sizeof (OSSVoiceOut),
767 INIT_FIELD (voice_size_in = ) sizeof (OSSVoiceIn)