Update SeaBIOS to include PCI based option rom loading
[qemu/aliguori-queue.git] / audio / alsaaudio.c
blob7698d1010bde2f732ab820a2ec16df5b7179bb12
1 /*
2 * QEMU ALSA audio driver
4 * Copyright (c) 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 <alsa/asoundlib.h>
25 #include "qemu-common.h"
26 #include "qemu-char.h"
27 #include "audio.h"
29 #if QEMU_GNUC_PREREQ(4, 3)
30 #pragma GCC diagnostic ignored "-Waddress"
31 #endif
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
36 struct pollhlp {
37 snd_pcm_t *handle;
38 struct pollfd *pfds;
39 int count;
40 int mask;
43 typedef struct ALSAVoiceOut {
44 HWVoiceOut hw;
45 int wpos;
46 int pending;
47 void *pcm_buf;
48 snd_pcm_t *handle;
49 struct pollhlp pollhlp;
50 } ALSAVoiceOut;
52 typedef struct ALSAVoiceIn {
53 HWVoiceIn hw;
54 snd_pcm_t *handle;
55 void *pcm_buf;
56 struct pollhlp pollhlp;
57 } ALSAVoiceIn;
59 static struct {
60 int size_in_usec_in;
61 int size_in_usec_out;
62 const char *pcm_name_in;
63 const char *pcm_name_out;
64 unsigned int buffer_size_in;
65 unsigned int period_size_in;
66 unsigned int buffer_size_out;
67 unsigned int period_size_out;
68 unsigned int threshold;
70 int buffer_size_in_overridden;
71 int period_size_in_overridden;
73 int buffer_size_out_overridden;
74 int period_size_out_overridden;
75 int verbose;
76 } conf = {
77 .buffer_size_out = 4096,
78 .period_size_out = 1024,
79 .pcm_name_out = "default",
80 .pcm_name_in = "default",
83 struct alsa_params_req {
84 int freq;
85 snd_pcm_format_t fmt;
86 int nchannels;
87 int size_in_usec;
88 int override_mask;
89 unsigned int buffer_size;
90 unsigned int period_size;
93 struct alsa_params_obt {
94 int freq;
95 audfmt_e fmt;
96 int endianness;
97 int nchannels;
98 snd_pcm_uframes_t samples;
101 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
103 va_list ap;
105 va_start (ap, fmt);
106 AUD_vlog (AUDIO_CAP, fmt, ap);
107 va_end (ap);
109 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
112 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
113 int err,
114 const char *typ,
115 const char *fmt,
119 va_list ap;
121 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
123 va_start (ap, fmt);
124 AUD_vlog (AUDIO_CAP, fmt, ap);
125 va_end (ap);
127 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
130 static void alsa_fini_poll (struct pollhlp *hlp)
132 int i;
133 struct pollfd *pfds = hlp->pfds;
135 if (pfds) {
136 for (i = 0; i < hlp->count; ++i) {
137 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
139 qemu_free (pfds);
141 hlp->pfds = NULL;
142 hlp->count = 0;
143 hlp->handle = NULL;
146 static void alsa_anal_close1 (snd_pcm_t **handlep)
148 int err = snd_pcm_close (*handlep);
149 if (err) {
150 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
152 *handlep = NULL;
155 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
157 alsa_fini_poll (hlp);
158 alsa_anal_close1 (handlep);
161 static int alsa_recover (snd_pcm_t *handle)
163 int err = snd_pcm_prepare (handle);
164 if (err < 0) {
165 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
166 return -1;
168 return 0;
171 static int alsa_resume (snd_pcm_t *handle)
173 int err = snd_pcm_resume (handle);
174 if (err < 0) {
175 alsa_logerr (err, "Failed to resume handle %p\n", handle);
176 return -1;
178 return 0;
181 static void alsa_poll_handler (void *opaque)
183 int err, count;
184 snd_pcm_state_t state;
185 struct pollhlp *hlp = opaque;
186 unsigned short revents;
188 count = poll (hlp->pfds, hlp->count, 0);
189 if (count < 0) {
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
191 return;
194 if (!count) {
195 return;
198 /* XXX: ALSA example uses initial count, not the one returned by
199 poll, correct? */
200 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
201 hlp->count, &revents);
202 if (err < 0) {
203 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
204 return;
207 if (!(revents & hlp->mask)) {
208 if (conf.verbose) {
209 dolog ("revents = %d\n", revents);
211 return;
214 state = snd_pcm_state (hlp->handle);
215 switch (state) {
216 case SND_PCM_STATE_XRUN:
217 alsa_recover (hlp->handle);
218 break;
220 case SND_PCM_STATE_SUSPENDED:
221 alsa_resume (hlp->handle);
222 break;
224 case SND_PCM_STATE_PREPARED:
225 audio_run ("alsa run (prepared)");
226 break;
228 case SND_PCM_STATE_RUNNING:
229 audio_run ("alsa run (running)");
230 break;
232 default:
233 dolog ("Unexpected state %d\n", state);
237 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
239 int i, count, err;
240 struct pollfd *pfds;
242 count = snd_pcm_poll_descriptors_count (handle);
243 if (count <= 0) {
244 dolog ("Could not initialize poll mode\n"
245 "Invalid number of poll descriptors %d\n", count);
246 return -1;
249 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
250 if (!pfds) {
251 dolog ("Could not initialize poll mode\n");
252 return -1;
255 err = snd_pcm_poll_descriptors (handle, pfds, count);
256 if (err < 0) {
257 alsa_logerr (err, "Could not initialize poll mode\n"
258 "Could not obtain poll descriptors\n");
259 qemu_free (pfds);
260 return -1;
263 for (i = 0; i < count; ++i) {
264 if (pfds[i].events & POLLIN) {
265 err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
266 NULL, hlp);
268 if (pfds[i].events & POLLOUT) {
269 if (conf.verbose) {
270 dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
272 err = qemu_set_fd_handler (pfds[i].fd, NULL,
273 alsa_poll_handler, hlp);
275 if (conf.verbose) {
276 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
277 pfds[i].events, i, pfds[i].fd, err);
280 if (err) {
281 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
282 pfds[i].events, i, pfds[i].fd, err);
284 while (i--) {
285 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
287 qemu_free (pfds);
288 return -1;
291 hlp->pfds = pfds;
292 hlp->count = count;
293 hlp->handle = handle;
294 hlp->mask = mask;
295 return 0;
298 static int alsa_poll_out (HWVoiceOut *hw)
300 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
302 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
305 static int alsa_poll_in (HWVoiceIn *hw)
307 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
309 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
312 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
314 return audio_pcm_sw_write (sw, buf, len);
317 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt)
319 switch (fmt) {
320 case AUD_FMT_S8:
321 return SND_PCM_FORMAT_S8;
323 case AUD_FMT_U8:
324 return SND_PCM_FORMAT_U8;
326 case AUD_FMT_S16:
327 return SND_PCM_FORMAT_S16_LE;
329 case AUD_FMT_U16:
330 return SND_PCM_FORMAT_U16_LE;
332 case AUD_FMT_S32:
333 return SND_PCM_FORMAT_S32_LE;
335 case AUD_FMT_U32:
336 return SND_PCM_FORMAT_U32_LE;
338 default:
339 dolog ("Internal logic error: Bad audio format %d\n", fmt);
340 #ifdef DEBUG_AUDIO
341 abort ();
342 #endif
343 return SND_PCM_FORMAT_U8;
347 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
348 int *endianness)
350 switch (alsafmt) {
351 case SND_PCM_FORMAT_S8:
352 *endianness = 0;
353 *fmt = AUD_FMT_S8;
354 break;
356 case SND_PCM_FORMAT_U8:
357 *endianness = 0;
358 *fmt = AUD_FMT_U8;
359 break;
361 case SND_PCM_FORMAT_S16_LE:
362 *endianness = 0;
363 *fmt = AUD_FMT_S16;
364 break;
366 case SND_PCM_FORMAT_U16_LE:
367 *endianness = 0;
368 *fmt = AUD_FMT_U16;
369 break;
371 case SND_PCM_FORMAT_S16_BE:
372 *endianness = 1;
373 *fmt = AUD_FMT_S16;
374 break;
376 case SND_PCM_FORMAT_U16_BE:
377 *endianness = 1;
378 *fmt = AUD_FMT_U16;
379 break;
381 case SND_PCM_FORMAT_S32_LE:
382 *endianness = 0;
383 *fmt = AUD_FMT_S32;
384 break;
386 case SND_PCM_FORMAT_U32_LE:
387 *endianness = 0;
388 *fmt = AUD_FMT_U32;
389 break;
391 case SND_PCM_FORMAT_S32_BE:
392 *endianness = 1;
393 *fmt = AUD_FMT_S32;
394 break;
396 case SND_PCM_FORMAT_U32_BE:
397 *endianness = 1;
398 *fmt = AUD_FMT_U32;
399 break;
401 default:
402 dolog ("Unrecognized audio format %d\n", alsafmt);
403 return -1;
406 return 0;
409 static void alsa_dump_info (struct alsa_params_req *req,
410 struct alsa_params_obt *obt)
412 dolog ("parameter | requested value | obtained value\n");
413 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
414 dolog ("channels | %10d | %10d\n",
415 req->nchannels, obt->nchannels);
416 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
417 dolog ("============================================\n");
418 dolog ("requested: buffer size %d period size %d\n",
419 req->buffer_size, req->period_size);
420 dolog ("obtained: samples %ld\n", obt->samples);
423 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
425 int err;
426 snd_pcm_sw_params_t *sw_params;
428 snd_pcm_sw_params_alloca (&sw_params);
430 err = snd_pcm_sw_params_current (handle, sw_params);
431 if (err < 0) {
432 dolog ("Could not fully initialize DAC\n");
433 alsa_logerr (err, "Failed to get current software parameters\n");
434 return;
437 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
438 if (err < 0) {
439 dolog ("Could not fully initialize DAC\n");
440 alsa_logerr (err, "Failed to set software threshold to %ld\n",
441 threshold);
442 return;
445 err = snd_pcm_sw_params (handle, sw_params);
446 if (err < 0) {
447 dolog ("Could not fully initialize DAC\n");
448 alsa_logerr (err, "Failed to set software parameters\n");
449 return;
453 static int alsa_open (int in, struct alsa_params_req *req,
454 struct alsa_params_obt *obt, snd_pcm_t **handlep)
456 snd_pcm_t *handle;
457 snd_pcm_hw_params_t *hw_params;
458 int err;
459 int size_in_usec;
460 unsigned int freq, nchannels;
461 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
462 snd_pcm_uframes_t obt_buffer_size;
463 const char *typ = in ? "ADC" : "DAC";
464 snd_pcm_format_t obtfmt;
466 freq = req->freq;
467 nchannels = req->nchannels;
468 size_in_usec = req->size_in_usec;
470 snd_pcm_hw_params_alloca (&hw_params);
472 err = snd_pcm_open (
473 &handle,
474 pcm_name,
475 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
476 SND_PCM_NONBLOCK
478 if (err < 0) {
479 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
480 return -1;
483 err = snd_pcm_hw_params_any (handle, hw_params);
484 if (err < 0) {
485 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
486 goto err;
489 err = snd_pcm_hw_params_set_access (
490 handle,
491 hw_params,
492 SND_PCM_ACCESS_RW_INTERLEAVED
494 if (err < 0) {
495 alsa_logerr2 (err, typ, "Failed to set access type\n");
496 goto err;
499 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
500 if (err < 0 && conf.verbose) {
501 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
504 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
505 if (err < 0) {
506 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
507 goto err;
510 err = snd_pcm_hw_params_set_channels_near (
511 handle,
512 hw_params,
513 &nchannels
515 if (err < 0) {
516 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
517 req->nchannels);
518 goto err;
521 if (nchannels != 1 && nchannels != 2) {
522 alsa_logerr2 (err, typ,
523 "Can not handle obtained number of channels %d\n",
524 nchannels);
525 goto err;
528 if (req->buffer_size) {
529 unsigned long obt;
531 if (size_in_usec) {
532 int dir = 0;
533 unsigned int btime = req->buffer_size;
535 err = snd_pcm_hw_params_set_buffer_time_near (
536 handle,
537 hw_params,
538 &btime,
539 &dir
541 obt = btime;
543 else {
544 snd_pcm_uframes_t bsize = req->buffer_size;
546 err = snd_pcm_hw_params_set_buffer_size_near (
547 handle,
548 hw_params,
549 &bsize
551 obt = bsize;
553 if (err < 0) {
554 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
555 size_in_usec ? "time" : "size", req->buffer_size);
556 goto err;
559 if ((req->override_mask & 2) && (obt - req->buffer_size))
560 dolog ("Requested buffer %s %u was rejected, using %lu\n",
561 size_in_usec ? "time" : "size", req->buffer_size, obt);
564 if (req->period_size) {
565 unsigned long obt;
567 if (size_in_usec) {
568 int dir = 0;
569 unsigned int ptime = req->period_size;
571 err = snd_pcm_hw_params_set_period_time_near (
572 handle,
573 hw_params,
574 &ptime,
575 &dir
577 obt = ptime;
579 else {
580 int dir = 0;
581 snd_pcm_uframes_t psize = req->period_size;
583 err = snd_pcm_hw_params_set_period_size_near (
584 handle,
585 hw_params,
586 &psize,
587 &dir
589 obt = psize;
592 if (err < 0) {
593 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
594 size_in_usec ? "time" : "size", req->period_size);
595 goto err;
598 if (((req->override_mask & 1) && (obt - req->period_size)))
599 dolog ("Requested period %s %u was rejected, using %lu\n",
600 size_in_usec ? "time" : "size", req->period_size, obt);
603 err = snd_pcm_hw_params (handle, hw_params);
604 if (err < 0) {
605 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
606 goto err;
609 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
610 if (err < 0) {
611 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
612 goto err;
615 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
616 if (err < 0) {
617 alsa_logerr2 (err, typ, "Failed to get format\n");
618 goto err;
621 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
622 dolog ("Invalid format was returned %d\n", obtfmt);
623 goto err;
626 err = snd_pcm_prepare (handle);
627 if (err < 0) {
628 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
629 goto err;
632 if (!in && conf.threshold) {
633 snd_pcm_uframes_t threshold;
634 int bytes_per_sec;
636 bytes_per_sec = freq << (nchannels == 2);
638 switch (obt->fmt) {
639 case AUD_FMT_S8:
640 case AUD_FMT_U8:
641 break;
643 case AUD_FMT_S16:
644 case AUD_FMT_U16:
645 bytes_per_sec <<= 1;
646 break;
648 case AUD_FMT_S32:
649 case AUD_FMT_U32:
650 bytes_per_sec <<= 2;
651 break;
654 threshold = (conf.threshold * bytes_per_sec) / 1000;
655 alsa_set_threshold (handle, threshold);
658 obt->nchannels = nchannels;
659 obt->freq = freq;
660 obt->samples = obt_buffer_size;
662 *handlep = handle;
664 if (conf.verbose &&
665 (obt->fmt != req->fmt ||
666 obt->nchannels != req->nchannels ||
667 obt->freq != req->freq)) {
668 dolog ("Audio paramters for %s\n", typ);
669 alsa_dump_info (req, obt);
672 #ifdef DEBUG
673 alsa_dump_info (req, obt);
674 #endif
675 return 0;
677 err:
678 alsa_anal_close1 (&handle);
679 return -1;
682 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
684 snd_pcm_sframes_t avail;
686 avail = snd_pcm_avail_update (handle);
687 if (avail < 0) {
688 if (avail == -EPIPE) {
689 if (!alsa_recover (handle)) {
690 avail = snd_pcm_avail_update (handle);
694 if (avail < 0) {
695 alsa_logerr (avail,
696 "Could not obtain number of available frames\n");
697 return -1;
701 return avail;
704 static void alsa_write_pending (ALSAVoiceOut *alsa)
706 HWVoiceOut *hw = &alsa->hw;
708 while (alsa->pending) {
709 int left_till_end_samples = hw->samples - alsa->wpos;
710 int len = audio_MIN (alsa->pending, left_till_end_samples);
711 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
713 while (len) {
714 snd_pcm_sframes_t written;
716 written = snd_pcm_writei (alsa->handle, src, len);
718 if (written <= 0) {
719 switch (written) {
720 case 0:
721 if (conf.verbose) {
722 dolog ("Failed to write %d frames (wrote zero)\n", len);
724 return;
726 case -EPIPE:
727 if (alsa_recover (alsa->handle)) {
728 alsa_logerr (written, "Failed to write %d frames\n",
729 len);
730 return;
732 if (conf.verbose) {
733 dolog ("Recovering from playback xrun\n");
735 continue;
737 case -ESTRPIPE:
738 /* stream is suspended and waiting for an
739 application recovery */
740 if (alsa_resume (alsa->handle)) {
741 alsa_logerr (written, "Failed to write %d frames\n",
742 len);
743 return;
745 if (conf.verbose) {
746 dolog ("Resuming suspended output stream\n");
748 continue;
750 case -EAGAIN:
751 return;
753 default:
754 alsa_logerr (written, "Failed to write %d frames from %p\n",
755 len, src);
756 return;
760 alsa->wpos = (alsa->wpos + written) % hw->samples;
761 alsa->pending -= written;
762 len -= written;
767 static int alsa_run_out (HWVoiceOut *hw, int live)
769 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
770 int decr;
771 snd_pcm_sframes_t avail;
773 avail = alsa_get_avail (alsa->handle);
774 if (avail < 0) {
775 dolog ("Could not get number of available playback frames\n");
776 return 0;
779 decr = audio_MIN (live, avail);
780 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
781 alsa->pending += decr;
782 alsa_write_pending (alsa);
783 return decr;
786 static void alsa_fini_out (HWVoiceOut *hw)
788 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
790 ldebug ("alsa_fini\n");
791 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
793 if (alsa->pcm_buf) {
794 qemu_free (alsa->pcm_buf);
795 alsa->pcm_buf = NULL;
799 static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
801 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
802 struct alsa_params_req req;
803 struct alsa_params_obt obt;
804 snd_pcm_t *handle;
805 struct audsettings obt_as;
807 req.fmt = aud_to_alsafmt (as->fmt);
808 req.freq = as->freq;
809 req.nchannels = as->nchannels;
810 req.period_size = conf.period_size_out;
811 req.buffer_size = conf.buffer_size_out;
812 req.size_in_usec = conf.size_in_usec_out;
813 req.override_mask =
814 (conf.period_size_out_overridden ? 1 : 0) |
815 (conf.buffer_size_out_overridden ? 2 : 0);
817 if (alsa_open (0, &req, &obt, &handle)) {
818 return -1;
821 obt_as.freq = obt.freq;
822 obt_as.nchannels = obt.nchannels;
823 obt_as.fmt = obt.fmt;
824 obt_as.endianness = obt.endianness;
826 audio_pcm_init_info (&hw->info, &obt_as);
827 hw->samples = obt.samples;
829 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
830 if (!alsa->pcm_buf) {
831 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
832 hw->samples, 1 << hw->info.shift);
833 alsa_anal_close1 (&handle);
834 return -1;
837 alsa->handle = handle;
838 return 0;
841 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int pause)
843 int err;
845 if (pause) {
846 err = snd_pcm_drop (handle);
847 if (err < 0) {
848 alsa_logerr (err, "Could not stop %s\n", typ);
849 return -1;
852 else {
853 err = snd_pcm_prepare (handle);
854 if (err < 0) {
855 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
856 return -1;
860 return 0;
863 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
865 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
867 switch (cmd) {
868 case VOICE_ENABLE:
870 va_list ap;
871 int poll_mode;
873 va_start (ap, cmd);
874 poll_mode = va_arg (ap, int);
875 va_end (ap);
877 ldebug ("enabling voice\n");
878 if (poll_mode && alsa_poll_out (hw)) {
879 poll_mode = 0;
881 hw->poll_mode = poll_mode;
882 return alsa_voice_ctl (alsa->handle, "playback", 0);
885 case VOICE_DISABLE:
886 ldebug ("disabling voice\n");
887 return alsa_voice_ctl (alsa->handle, "playback", 1);
890 return -1;
893 static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
895 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
896 struct alsa_params_req req;
897 struct alsa_params_obt obt;
898 snd_pcm_t *handle;
899 struct audsettings obt_as;
901 req.fmt = aud_to_alsafmt (as->fmt);
902 req.freq = as->freq;
903 req.nchannels = as->nchannels;
904 req.period_size = conf.period_size_in;
905 req.buffer_size = conf.buffer_size_in;
906 req.size_in_usec = conf.size_in_usec_in;
907 req.override_mask =
908 (conf.period_size_in_overridden ? 1 : 0) |
909 (conf.buffer_size_in_overridden ? 2 : 0);
911 if (alsa_open (1, &req, &obt, &handle)) {
912 return -1;
915 obt_as.freq = obt.freq;
916 obt_as.nchannels = obt.nchannels;
917 obt_as.fmt = obt.fmt;
918 obt_as.endianness = obt.endianness;
920 audio_pcm_init_info (&hw->info, &obt_as);
921 hw->samples = obt.samples;
923 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
924 if (!alsa->pcm_buf) {
925 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
926 hw->samples, 1 << hw->info.shift);
927 alsa_anal_close1 (&handle);
928 return -1;
931 alsa->handle = handle;
932 return 0;
935 static void alsa_fini_in (HWVoiceIn *hw)
937 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
939 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
941 if (alsa->pcm_buf) {
942 qemu_free (alsa->pcm_buf);
943 alsa->pcm_buf = NULL;
947 static int alsa_run_in (HWVoiceIn *hw)
949 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
950 int hwshift = hw->info.shift;
951 int i;
952 int live = audio_pcm_hw_get_live_in (hw);
953 int dead = hw->samples - live;
954 int decr;
955 struct {
956 int add;
957 int len;
958 } bufs[2] = {
959 { .add = hw->wpos, .len = 0 },
960 { .add = 0, .len = 0 }
962 snd_pcm_sframes_t avail;
963 snd_pcm_uframes_t read_samples = 0;
965 if (!dead) {
966 return 0;
969 avail = alsa_get_avail (alsa->handle);
970 if (avail < 0) {
971 dolog ("Could not get number of captured frames\n");
972 return 0;
975 if (!avail) {
976 snd_pcm_state_t state;
978 state = snd_pcm_state (alsa->handle);
979 switch (state) {
980 case SND_PCM_STATE_PREPARED:
981 avail = hw->samples;
982 break;
983 case SND_PCM_STATE_SUSPENDED:
984 /* stream is suspended and waiting for an application recovery */
985 if (alsa_resume (alsa->handle)) {
986 dolog ("Failed to resume suspended input stream\n");
987 return 0;
989 if (conf.verbose) {
990 dolog ("Resuming suspended input stream\n");
992 break;
993 default:
994 if (conf.verbose) {
995 dolog ("No frames available and ALSA state is %d\n", state);
997 return 0;
1001 decr = audio_MIN (dead, avail);
1002 if (!decr) {
1003 return 0;
1006 if (hw->wpos + decr > hw->samples) {
1007 bufs[0].len = (hw->samples - hw->wpos);
1008 bufs[1].len = (decr - (hw->samples - hw->wpos));
1010 else {
1011 bufs[0].len = decr;
1014 for (i = 0; i < 2; ++i) {
1015 void *src;
1016 struct st_sample *dst;
1017 snd_pcm_sframes_t nread;
1018 snd_pcm_uframes_t len;
1020 len = bufs[i].len;
1022 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1023 dst = hw->conv_buf + bufs[i].add;
1025 while (len) {
1026 nread = snd_pcm_readi (alsa->handle, src, len);
1028 if (nread <= 0) {
1029 switch (nread) {
1030 case 0:
1031 if (conf.verbose) {
1032 dolog ("Failed to read %ld frames (read zero)\n", len);
1034 goto exit;
1036 case -EPIPE:
1037 if (alsa_recover (alsa->handle)) {
1038 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1039 goto exit;
1041 if (conf.verbose) {
1042 dolog ("Recovering from capture xrun\n");
1044 continue;
1046 case -EAGAIN:
1047 goto exit;
1049 default:
1050 alsa_logerr (
1051 nread,
1052 "Failed to read %ld frames from %p\n",
1053 len,
1056 goto exit;
1060 hw->conv (dst, src, nread, &nominal_volume);
1062 src = advance (src, nread << hwshift);
1063 dst += nread;
1065 read_samples += nread;
1066 len -= nread;
1070 exit:
1071 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1072 return read_samples;
1075 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1077 return audio_pcm_sw_read (sw, buf, size);
1080 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1082 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1084 switch (cmd) {
1085 case VOICE_ENABLE:
1087 va_list ap;
1088 int poll_mode;
1090 va_start (ap, cmd);
1091 poll_mode = va_arg (ap, int);
1092 va_end (ap);
1094 ldebug ("enabling voice\n");
1095 if (poll_mode && alsa_poll_in (hw)) {
1096 poll_mode = 0;
1098 hw->poll_mode = poll_mode;
1100 return alsa_voice_ctl (alsa->handle, "capture", 0);
1103 case VOICE_DISABLE:
1104 ldebug ("disabling voice\n");
1105 if (hw->poll_mode) {
1106 hw->poll_mode = 0;
1107 alsa_fini_poll (&alsa->pollhlp);
1109 return alsa_voice_ctl (alsa->handle, "capture", 1);
1112 return -1;
1115 static void *alsa_audio_init (void)
1117 return &conf;
1120 static void alsa_audio_fini (void *opaque)
1122 (void) opaque;
1125 static struct audio_option alsa_options[] = {
1127 .name = "DAC_SIZE_IN_USEC",
1128 .tag = AUD_OPT_BOOL,
1129 .valp = &conf.size_in_usec_out,
1130 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1133 .name = "DAC_PERIOD_SIZE",
1134 .tag = AUD_OPT_INT,
1135 .valp = &conf.period_size_out,
1136 .descr = "DAC period size (0 to go with system default)",
1137 .overriddenp = &conf.period_size_out_overridden
1140 .name = "DAC_BUFFER_SIZE",
1141 .tag = AUD_OPT_INT,
1142 .valp = &conf.buffer_size_out,
1143 .descr = "DAC buffer size (0 to go with system default)",
1144 .overriddenp = &conf.buffer_size_out_overridden
1147 .name = "ADC_SIZE_IN_USEC",
1148 .tag = AUD_OPT_BOOL,
1149 .valp = &conf.size_in_usec_in,
1150 .descr =
1151 "ADC period/buffer size in microseconds (otherwise in frames)"
1154 .name = "ADC_PERIOD_SIZE",
1155 .tag = AUD_OPT_INT,
1156 .valp = &conf.period_size_in,
1157 .descr = "ADC period size (0 to go with system default)",
1158 .overriddenp = &conf.period_size_in_overridden
1161 .name = "ADC_BUFFER_SIZE",
1162 .tag = AUD_OPT_INT,
1163 .valp = &conf.buffer_size_in,
1164 .descr = "ADC buffer size (0 to go with system default)",
1165 .overriddenp = &conf.buffer_size_in_overridden
1168 .name = "THRESHOLD",
1169 .tag = AUD_OPT_INT,
1170 .valp = &conf.threshold,
1171 .descr = "(undocumented)"
1174 .name = "DAC_DEV",
1175 .tag = AUD_OPT_STR,
1176 .valp = &conf.pcm_name_out,
1177 .descr = "DAC device name (for instance dmix)"
1180 .name = "ADC_DEV",
1181 .tag = AUD_OPT_STR,
1182 .valp = &conf.pcm_name_in,
1183 .descr = "ADC device name"
1186 .name = "VERBOSE",
1187 .tag = AUD_OPT_BOOL,
1188 .valp = &conf.verbose,
1189 .descr = "Behave in a more verbose way"
1191 { /* End of list */ }
1194 static struct audio_pcm_ops alsa_pcm_ops = {
1195 .init_out = alsa_init_out,
1196 .fini_out = alsa_fini_out,
1197 .run_out = alsa_run_out,
1198 .write = alsa_write,
1199 .ctl_out = alsa_ctl_out,
1201 .init_in = alsa_init_in,
1202 .fini_in = alsa_fini_in,
1203 .run_in = alsa_run_in,
1204 .read = alsa_read,
1205 .ctl_in = alsa_ctl_in,
1208 struct audio_driver alsa_audio_driver = {
1209 .name = "alsa",
1210 .descr = "ALSA http://www.alsa-project.org",
1211 .options = alsa_options,
1212 .init = alsa_audio_init,
1213 .fini = alsa_audio_fini,
1214 .pcm_ops = &alsa_pcm_ops,
1215 .can_be_default = 1,
1216 .max_voices_out = INT_MAX,
1217 .max_voices_in = INT_MAX,
1218 .voice_size_out = sizeof (ALSAVoiceOut),
1219 .voice_size_in = sizeof (ALSAVoiceIn)