hw/timer/m48t59: Convert debug printf()s to trace events
[qemu/ar7.git] / audio / alsaaudio.c
blobf37ce1ce85705388c5fd88d760cc0de4f5e98181
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.
25 #include "qemu/osdep.h"
26 #include <alsa/asoundlib.h>
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
29 #include "audio.h"
30 #include "trace.h"
32 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
37 struct pollhlp {
38 snd_pcm_t *handle;
39 struct pollfd *pfds;
40 int count;
41 int mask;
42 AudioState *s;
45 typedef struct ALSAVoiceOut {
46 HWVoiceOut hw;
47 snd_pcm_t *handle;
48 struct pollhlp pollhlp;
49 Audiodev *dev;
50 } ALSAVoiceOut;
52 typedef struct ALSAVoiceIn {
53 HWVoiceIn hw;
54 snd_pcm_t *handle;
55 struct pollhlp pollhlp;
56 Audiodev *dev;
57 } ALSAVoiceIn;
59 struct alsa_params_req {
60 int freq;
61 snd_pcm_format_t fmt;
62 int nchannels;
65 struct alsa_params_obt {
66 int freq;
67 AudioFormat fmt;
68 int endianness;
69 int nchannels;
70 snd_pcm_uframes_t samples;
73 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
75 va_list ap;
77 va_start (ap, fmt);
78 AUD_vlog (AUDIO_CAP, fmt, ap);
79 va_end (ap);
81 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
84 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
85 int err,
86 const char *typ,
87 const char *fmt,
88 ...
91 va_list ap;
93 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
95 va_start (ap, fmt);
96 AUD_vlog (AUDIO_CAP, fmt, ap);
97 va_end (ap);
99 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
102 static void alsa_fini_poll (struct pollhlp *hlp)
104 int i;
105 struct pollfd *pfds = hlp->pfds;
107 if (pfds) {
108 for (i = 0; i < hlp->count; ++i) {
109 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
111 g_free (pfds);
113 hlp->pfds = NULL;
114 hlp->count = 0;
115 hlp->handle = NULL;
118 static void alsa_anal_close1 (snd_pcm_t **handlep)
120 int err = snd_pcm_close (*handlep);
121 if (err) {
122 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
124 *handlep = NULL;
127 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
129 alsa_fini_poll (hlp);
130 alsa_anal_close1 (handlep);
133 static int alsa_recover (snd_pcm_t *handle)
135 int err = snd_pcm_prepare (handle);
136 if (err < 0) {
137 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
138 return -1;
140 return 0;
143 static int alsa_resume (snd_pcm_t *handle)
145 int err = snd_pcm_resume (handle);
146 if (err < 0) {
147 alsa_logerr (err, "Failed to resume handle %p\n", handle);
148 return -1;
150 return 0;
153 static void alsa_poll_handler (void *opaque)
155 int err, count;
156 snd_pcm_state_t state;
157 struct pollhlp *hlp = opaque;
158 unsigned short revents;
160 count = poll (hlp->pfds, hlp->count, 0);
161 if (count < 0) {
162 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
163 return;
166 if (!count) {
167 return;
170 /* XXX: ALSA example uses initial count, not the one returned by
171 poll, correct? */
172 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
173 hlp->count, &revents);
174 if (err < 0) {
175 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
176 return;
179 if (!(revents & hlp->mask)) {
180 trace_alsa_revents(revents);
181 return;
184 state = snd_pcm_state (hlp->handle);
185 switch (state) {
186 case SND_PCM_STATE_SETUP:
187 alsa_recover (hlp->handle);
188 break;
190 case SND_PCM_STATE_XRUN:
191 alsa_recover (hlp->handle);
192 break;
194 case SND_PCM_STATE_SUSPENDED:
195 alsa_resume (hlp->handle);
196 break;
198 case SND_PCM_STATE_PREPARED:
199 audio_run(hlp->s, "alsa run (prepared)");
200 break;
202 case SND_PCM_STATE_RUNNING:
203 audio_run(hlp->s, "alsa run (running)");
204 break;
206 default:
207 dolog ("Unexpected state %d\n", state);
211 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
213 int i, count, err;
214 struct pollfd *pfds;
216 count = snd_pcm_poll_descriptors_count (handle);
217 if (count <= 0) {
218 dolog ("Could not initialize poll mode\n"
219 "Invalid number of poll descriptors %d\n", count);
220 return -1;
223 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
224 if (!pfds) {
225 dolog ("Could not initialize poll mode\n");
226 return -1;
229 err = snd_pcm_poll_descriptors (handle, pfds, count);
230 if (err < 0) {
231 alsa_logerr (err, "Could not initialize poll mode\n"
232 "Could not obtain poll descriptors\n");
233 g_free (pfds);
234 return -1;
237 for (i = 0; i < count; ++i) {
238 if (pfds[i].events & POLLIN) {
239 qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
241 if (pfds[i].events & POLLOUT) {
242 trace_alsa_pollout(i, pfds[i].fd);
243 qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
245 trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
248 hlp->pfds = pfds;
249 hlp->count = count;
250 hlp->handle = handle;
251 hlp->mask = mask;
252 return 0;
255 static int alsa_poll_out (HWVoiceOut *hw)
257 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
259 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
262 static int alsa_poll_in (HWVoiceIn *hw)
264 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
266 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
269 static snd_pcm_format_t aud_to_alsafmt (AudioFormat fmt, int endianness)
271 switch (fmt) {
272 case AUDIO_FORMAT_S8:
273 return SND_PCM_FORMAT_S8;
275 case AUDIO_FORMAT_U8:
276 return SND_PCM_FORMAT_U8;
278 case AUDIO_FORMAT_S16:
279 if (endianness) {
280 return SND_PCM_FORMAT_S16_BE;
282 else {
283 return SND_PCM_FORMAT_S16_LE;
286 case AUDIO_FORMAT_U16:
287 if (endianness) {
288 return SND_PCM_FORMAT_U16_BE;
290 else {
291 return SND_PCM_FORMAT_U16_LE;
294 case AUDIO_FORMAT_S32:
295 if (endianness) {
296 return SND_PCM_FORMAT_S32_BE;
298 else {
299 return SND_PCM_FORMAT_S32_LE;
302 case AUDIO_FORMAT_U32:
303 if (endianness) {
304 return SND_PCM_FORMAT_U32_BE;
306 else {
307 return SND_PCM_FORMAT_U32_LE;
310 default:
311 dolog ("Internal logic error: Bad audio format %d\n", fmt);
312 #ifdef DEBUG_AUDIO
313 abort ();
314 #endif
315 return SND_PCM_FORMAT_U8;
319 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, AudioFormat *fmt,
320 int *endianness)
322 switch (alsafmt) {
323 case SND_PCM_FORMAT_S8:
324 *endianness = 0;
325 *fmt = AUDIO_FORMAT_S8;
326 break;
328 case SND_PCM_FORMAT_U8:
329 *endianness = 0;
330 *fmt = AUDIO_FORMAT_U8;
331 break;
333 case SND_PCM_FORMAT_S16_LE:
334 *endianness = 0;
335 *fmt = AUDIO_FORMAT_S16;
336 break;
338 case SND_PCM_FORMAT_U16_LE:
339 *endianness = 0;
340 *fmt = AUDIO_FORMAT_U16;
341 break;
343 case SND_PCM_FORMAT_S16_BE:
344 *endianness = 1;
345 *fmt = AUDIO_FORMAT_S16;
346 break;
348 case SND_PCM_FORMAT_U16_BE:
349 *endianness = 1;
350 *fmt = AUDIO_FORMAT_U16;
351 break;
353 case SND_PCM_FORMAT_S32_LE:
354 *endianness = 0;
355 *fmt = AUDIO_FORMAT_S32;
356 break;
358 case SND_PCM_FORMAT_U32_LE:
359 *endianness = 0;
360 *fmt = AUDIO_FORMAT_U32;
361 break;
363 case SND_PCM_FORMAT_S32_BE:
364 *endianness = 1;
365 *fmt = AUDIO_FORMAT_S32;
366 break;
368 case SND_PCM_FORMAT_U32_BE:
369 *endianness = 1;
370 *fmt = AUDIO_FORMAT_U32;
371 break;
373 default:
374 dolog ("Unrecognized audio format %d\n", alsafmt);
375 return -1;
378 return 0;
381 static void alsa_dump_info (struct alsa_params_req *req,
382 struct alsa_params_obt *obt,
383 snd_pcm_format_t obtfmt,
384 AudiodevAlsaPerDirectionOptions *apdo)
386 dolog("parameter | requested value | obtained value\n");
387 dolog("format | %10d | %10d\n", req->fmt, obtfmt);
388 dolog("channels | %10d | %10d\n",
389 req->nchannels, obt->nchannels);
390 dolog("frequency | %10d | %10d\n", req->freq, obt->freq);
391 dolog("============================================\n");
392 dolog("requested: buffer len %" PRId32 " period len %" PRId32 "\n",
393 apdo->buffer_length, apdo->period_length);
394 dolog("obtained: samples %ld\n", obt->samples);
397 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
399 int err;
400 snd_pcm_sw_params_t *sw_params;
402 snd_pcm_sw_params_alloca (&sw_params);
404 err = snd_pcm_sw_params_current (handle, sw_params);
405 if (err < 0) {
406 dolog ("Could not fully initialize DAC\n");
407 alsa_logerr (err, "Failed to get current software parameters\n");
408 return;
411 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
412 if (err < 0) {
413 dolog ("Could not fully initialize DAC\n");
414 alsa_logerr (err, "Failed to set software threshold to %ld\n",
415 threshold);
416 return;
419 err = snd_pcm_sw_params (handle, sw_params);
420 if (err < 0) {
421 dolog ("Could not fully initialize DAC\n");
422 alsa_logerr (err, "Failed to set software parameters\n");
423 return;
427 static int alsa_open(bool in, struct alsa_params_req *req,
428 struct alsa_params_obt *obt, snd_pcm_t **handlep,
429 Audiodev *dev)
431 AudiodevAlsaOptions *aopts = &dev->u.alsa;
432 AudiodevAlsaPerDirectionOptions *apdo = in ? aopts->in : aopts->out;
433 snd_pcm_t *handle;
434 snd_pcm_hw_params_t *hw_params;
435 int err;
436 unsigned int freq, nchannels;
437 const char *pcm_name = apdo->has_dev ? apdo->dev : "default";
438 snd_pcm_uframes_t obt_buffer_size;
439 const char *typ = in ? "ADC" : "DAC";
440 snd_pcm_format_t obtfmt;
442 freq = req->freq;
443 nchannels = req->nchannels;
445 snd_pcm_hw_params_alloca (&hw_params);
447 err = snd_pcm_open (
448 &handle,
449 pcm_name,
450 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
451 SND_PCM_NONBLOCK
453 if (err < 0) {
454 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
455 return -1;
458 err = snd_pcm_hw_params_any (handle, hw_params);
459 if (err < 0) {
460 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
461 goto err;
464 err = snd_pcm_hw_params_set_access (
465 handle,
466 hw_params,
467 SND_PCM_ACCESS_RW_INTERLEAVED
469 if (err < 0) {
470 alsa_logerr2 (err, typ, "Failed to set access type\n");
471 goto err;
474 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
475 if (err < 0) {
476 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
479 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
480 if (err < 0) {
481 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
482 goto err;
485 err = snd_pcm_hw_params_set_channels_near (
486 handle,
487 hw_params,
488 &nchannels
490 if (err < 0) {
491 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
492 req->nchannels);
493 goto err;
496 if (apdo->buffer_length) {
497 int dir = 0;
498 unsigned int btime = apdo->buffer_length;
500 err = snd_pcm_hw_params_set_buffer_time_near(
501 handle, hw_params, &btime, &dir);
503 if (err < 0) {
504 alsa_logerr2(err, typ, "Failed to set buffer time to %" PRId32 "\n",
505 apdo->buffer_length);
506 goto err;
509 if (apdo->has_buffer_length && btime != apdo->buffer_length) {
510 dolog("Requested buffer time %" PRId32
511 " was rejected, using %u\n", apdo->buffer_length, btime);
515 if (apdo->period_length) {
516 int dir = 0;
517 unsigned int ptime = apdo->period_length;
519 err = snd_pcm_hw_params_set_period_time_near(handle, hw_params, &ptime,
520 &dir);
522 if (err < 0) {
523 alsa_logerr2(err, typ, "Failed to set period time to %" PRId32 "\n",
524 apdo->period_length);
525 goto err;
528 if (apdo->has_period_length && ptime != apdo->period_length) {
529 dolog("Requested period time %" PRId32 " was rejected, using %d\n",
530 apdo->period_length, ptime);
534 err = snd_pcm_hw_params (handle, hw_params);
535 if (err < 0) {
536 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
537 goto err;
540 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
541 if (err < 0) {
542 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
543 goto err;
546 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
547 if (err < 0) {
548 alsa_logerr2 (err, typ, "Failed to get format\n");
549 goto err;
552 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
553 dolog ("Invalid format was returned %d\n", obtfmt);
554 goto err;
557 err = snd_pcm_prepare (handle);
558 if (err < 0) {
559 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
560 goto err;
563 if (!in && aopts->has_threshold && aopts->threshold) {
564 struct audsettings as = { .freq = freq };
565 alsa_set_threshold(
566 handle,
567 audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo),
568 &as, aopts->threshold));
571 obt->nchannels = nchannels;
572 obt->freq = freq;
573 obt->samples = obt_buffer_size;
575 *handlep = handle;
577 if (obtfmt != req->fmt ||
578 obt->nchannels != req->nchannels ||
579 obt->freq != req->freq) {
580 dolog ("Audio parameters for %s\n", typ);
581 alsa_dump_info(req, obt, obtfmt, apdo);
584 #ifdef DEBUG
585 alsa_dump_info(req, obt, obtfmt, pdo);
586 #endif
587 return 0;
589 err:
590 alsa_anal_close1 (&handle);
591 return -1;
594 static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len)
596 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
597 size_t pos = 0;
598 size_t len_frames = len / hw->info.bytes_per_frame;
600 while (len_frames) {
601 char *src = advance(buf, pos);
602 snd_pcm_sframes_t written;
604 written = snd_pcm_writei(alsa->handle, src, len_frames);
606 if (written <= 0) {
607 switch (written) {
608 case 0:
609 trace_alsa_wrote_zero(len_frames);
610 return pos;
612 case -EPIPE:
613 if (alsa_recover(alsa->handle)) {
614 alsa_logerr(written, "Failed to write %zu frames\n",
615 len_frames);
616 return pos;
618 trace_alsa_xrun_out();
619 continue;
621 case -ESTRPIPE:
623 * stream is suspended and waiting for an application
624 * recovery
626 if (alsa_resume(alsa->handle)) {
627 alsa_logerr(written, "Failed to write %zu frames\n",
628 len_frames);
629 return pos;
631 trace_alsa_resume_out();
632 continue;
634 case -EAGAIN:
635 return pos;
637 default:
638 alsa_logerr(written, "Failed to write %zu frames from %p\n",
639 len, src);
640 return pos;
644 pos += written * hw->info.bytes_per_frame;
645 if (written < len_frames) {
646 break;
648 len_frames -= written;
651 return pos;
654 static void alsa_fini_out (HWVoiceOut *hw)
656 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
658 ldebug ("alsa_fini\n");
659 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
662 static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
663 void *drv_opaque)
665 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
666 struct alsa_params_req req;
667 struct alsa_params_obt obt;
668 snd_pcm_t *handle;
669 struct audsettings obt_as;
670 Audiodev *dev = drv_opaque;
672 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
673 req.freq = as->freq;
674 req.nchannels = as->nchannels;
676 if (alsa_open(0, &req, &obt, &handle, dev)) {
677 return -1;
680 obt_as.freq = obt.freq;
681 obt_as.nchannels = obt.nchannels;
682 obt_as.fmt = obt.fmt;
683 obt_as.endianness = obt.endianness;
685 audio_pcm_init_info (&hw->info, &obt_as);
686 hw->samples = obt.samples;
688 alsa->pollhlp.s = hw->s;
689 alsa->handle = handle;
690 alsa->dev = dev;
691 return 0;
694 #define VOICE_CTL_PAUSE 0
695 #define VOICE_CTL_PREPARE 1
696 #define VOICE_CTL_START 2
698 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
700 int err;
702 if (ctl == VOICE_CTL_PAUSE) {
703 err = snd_pcm_drop (handle);
704 if (err < 0) {
705 alsa_logerr (err, "Could not stop %s\n", typ);
706 return -1;
709 else {
710 err = snd_pcm_prepare (handle);
711 if (err < 0) {
712 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
713 return -1;
715 if (ctl == VOICE_CTL_START) {
716 err = snd_pcm_start(handle);
717 if (err < 0) {
718 alsa_logerr (err, "Could not start handle for %s\n", typ);
719 return -1;
724 return 0;
727 static void alsa_enable_out(HWVoiceOut *hw, bool enable)
729 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
730 AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out;
732 if (enable) {
733 bool poll_mode = apdo->try_poll;
735 ldebug("enabling voice\n");
736 if (poll_mode && alsa_poll_out(hw)) {
737 poll_mode = 0;
739 hw->poll_mode = poll_mode;
740 alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PREPARE);
741 } else {
742 ldebug("disabling voice\n");
743 if (hw->poll_mode) {
744 hw->poll_mode = 0;
745 alsa_fini_poll(&alsa->pollhlp);
747 alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PAUSE);
751 static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
753 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
754 struct alsa_params_req req;
755 struct alsa_params_obt obt;
756 snd_pcm_t *handle;
757 struct audsettings obt_as;
758 Audiodev *dev = drv_opaque;
760 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
761 req.freq = as->freq;
762 req.nchannels = as->nchannels;
764 if (alsa_open(1, &req, &obt, &handle, dev)) {
765 return -1;
768 obt_as.freq = obt.freq;
769 obt_as.nchannels = obt.nchannels;
770 obt_as.fmt = obt.fmt;
771 obt_as.endianness = obt.endianness;
773 audio_pcm_init_info (&hw->info, &obt_as);
774 hw->samples = obt.samples;
776 alsa->pollhlp.s = hw->s;
777 alsa->handle = handle;
778 alsa->dev = dev;
779 return 0;
782 static void alsa_fini_in (HWVoiceIn *hw)
784 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
786 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
789 static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)
791 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
792 size_t pos = 0;
794 while (len) {
795 void *dst = advance(buf, pos);
796 snd_pcm_sframes_t nread;
798 nread = snd_pcm_readi(
799 alsa->handle, dst, len / hw->info.bytes_per_frame);
801 if (nread <= 0) {
802 switch (nread) {
803 case 0:
804 trace_alsa_read_zero(len);
805 return pos;;
807 case -EPIPE:
808 if (alsa_recover(alsa->handle)) {
809 alsa_logerr(nread, "Failed to read %zu frames\n", len);
810 return pos;
812 trace_alsa_xrun_in();
813 continue;
815 case -EAGAIN:
816 return pos;
818 default:
819 alsa_logerr(nread, "Failed to read %zu frames to %p\n",
820 len, dst);
821 return pos;;
825 pos += nread * hw->info.bytes_per_frame;
826 len -= nread * hw->info.bytes_per_frame;
829 return pos;
832 static void alsa_enable_in(HWVoiceIn *hw, bool enable)
834 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
835 AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in;
837 if (enable) {
838 bool poll_mode = apdo->try_poll;
840 ldebug("enabling voice\n");
841 if (poll_mode && alsa_poll_in(hw)) {
842 poll_mode = 0;
844 hw->poll_mode = poll_mode;
846 alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_START);
847 } else {
848 ldebug ("disabling voice\n");
849 if (hw->poll_mode) {
850 hw->poll_mode = 0;
851 alsa_fini_poll(&alsa->pollhlp);
853 alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_PAUSE);
857 static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions *apdo)
859 if (!apdo->has_try_poll) {
860 apdo->try_poll = true;
861 apdo->has_try_poll = true;
865 static void *alsa_audio_init(Audiodev *dev)
867 AudiodevAlsaOptions *aopts;
868 assert(dev->driver == AUDIODEV_DRIVER_ALSA);
870 aopts = &dev->u.alsa;
871 alsa_init_per_direction(aopts->in);
872 alsa_init_per_direction(aopts->out);
875 * need to define them, as otherwise alsa produces no sound
876 * doesn't set has_* so alsa_open can identify it wasn't set by the user
878 if (!dev->u.alsa.out->has_period_length) {
879 /* 1024 frames assuming 44100Hz */
880 dev->u.alsa.out->period_length = 1024 * 1000000 / 44100;
882 if (!dev->u.alsa.out->has_buffer_length) {
883 /* 4096 frames assuming 44100Hz */
884 dev->u.alsa.out->buffer_length = 4096ll * 1000000 / 44100;
888 * OptsVisitor sets unspecified optional fields to zero, but do not depend
889 * on it...
891 if (!dev->u.alsa.in->has_period_length) {
892 dev->u.alsa.in->period_length = 0;
894 if (!dev->u.alsa.in->has_buffer_length) {
895 dev->u.alsa.in->buffer_length = 0;
898 return dev;
901 static void alsa_audio_fini (void *opaque)
905 static struct audio_pcm_ops alsa_pcm_ops = {
906 .init_out = alsa_init_out,
907 .fini_out = alsa_fini_out,
908 .write = alsa_write,
909 .enable_out = alsa_enable_out,
911 .init_in = alsa_init_in,
912 .fini_in = alsa_fini_in,
913 .read = alsa_read,
914 .enable_in = alsa_enable_in,
917 static struct audio_driver alsa_audio_driver = {
918 .name = "alsa",
919 .descr = "ALSA http://www.alsa-project.org",
920 .init = alsa_audio_init,
921 .fini = alsa_audio_fini,
922 .pcm_ops = &alsa_pcm_ops,
923 .can_be_default = 1,
924 .max_voices_out = INT_MAX,
925 .max_voices_in = INT_MAX,
926 .voice_size_out = sizeof (ALSAVoiceOut),
927 .voice_size_in = sizeof (ALSAVoiceIn)
930 static void register_audio_alsa(void)
932 audio_driver_register(&alsa_audio_driver);
934 type_init(register_audio_alsa);