virtio-net: introduce a new macaddr control
[qemu/ar7.git] / hw / hda-audio.c
blob3190bd1cf840e5b328427fd772b2dd0037720422
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * written by Gerd Hoffmann <kraxel@redhat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw.h"
21 #include "pci/pci.h"
22 #include "intel-hda.h"
23 #include "intel-hda-defs.h"
24 #include "audio/audio.h"
26 /* -------------------------------------------------------------------------- */
28 typedef struct desc_param {
29 uint32_t id;
30 uint32_t val;
31 } desc_param;
33 typedef struct desc_node {
34 uint32_t nid;
35 const char *name;
36 const desc_param *params;
37 uint32_t nparams;
38 uint32_t config;
39 uint32_t pinctl;
40 uint32_t *conn;
41 uint32_t stindex;
42 } desc_node;
44 typedef struct desc_codec {
45 const char *name;
46 uint32_t iid;
47 const desc_node *nodes;
48 uint32_t nnodes;
49 } desc_codec;
51 static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id)
53 int i;
55 for (i = 0; i < node->nparams; i++) {
56 if (node->params[i].id == id) {
57 return &node->params[i];
60 return NULL;
63 static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid)
65 int i;
67 for (i = 0; i < codec->nnodes; i++) {
68 if (codec->nodes[i].nid == nid) {
69 return &codec->nodes[i];
72 return NULL;
75 static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
77 if (format & AC_FMT_TYPE_NON_PCM) {
78 return;
81 as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000;
83 switch ((format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT) {
84 case 1: as->freq *= 2; break;
85 case 2: as->freq *= 3; break;
86 case 3: as->freq *= 4; break;
89 switch ((format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT) {
90 case 1: as->freq /= 2; break;
91 case 2: as->freq /= 3; break;
92 case 3: as->freq /= 4; break;
93 case 4: as->freq /= 5; break;
94 case 5: as->freq /= 6; break;
95 case 6: as->freq /= 7; break;
96 case 7: as->freq /= 8; break;
99 switch (format & AC_FMT_BITS_MASK) {
100 case AC_FMT_BITS_8: as->fmt = AUD_FMT_S8; break;
101 case AC_FMT_BITS_16: as->fmt = AUD_FMT_S16; break;
102 case AC_FMT_BITS_32: as->fmt = AUD_FMT_S32; break;
105 as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1;
108 /* -------------------------------------------------------------------------- */
110 * HDA codec descriptions
113 /* some defines */
115 #define QEMU_HDA_ID_VENDOR 0x1af4
116 #define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
117 0x1fc /* 16 -> 96 kHz */)
118 #define QEMU_HDA_AMP_NONE (0)
119 #define QEMU_HDA_AMP_STEPS 0x4a
121 #ifdef CONFIG_MIXEMU
122 # define QEMU_HDA_ID_OUTPUT ((QEMU_HDA_ID_VENDOR << 16) | 0x12)
123 # define QEMU_HDA_ID_DUPLEX ((QEMU_HDA_ID_VENDOR << 16) | 0x22)
124 # define QEMU_HDA_ID_MICRO ((QEMU_HDA_ID_VENDOR << 16) | 0x32)
125 # define QEMU_HDA_AMP_CAPS \
126 (AC_AMPCAP_MUTE | \
127 (QEMU_HDA_AMP_STEPS << AC_AMPCAP_OFFSET_SHIFT) | \
128 (QEMU_HDA_AMP_STEPS << AC_AMPCAP_NUM_STEPS_SHIFT) | \
129 (3 << AC_AMPCAP_STEP_SIZE_SHIFT))
130 #else
131 # define QEMU_HDA_ID_OUTPUT ((QEMU_HDA_ID_VENDOR << 16) | 0x11)
132 # define QEMU_HDA_ID_DUPLEX ((QEMU_HDA_ID_VENDOR << 16) | 0x21)
133 # define QEMU_HDA_ID_MICRO ((QEMU_HDA_ID_VENDOR << 16) | 0x31)
134 # define QEMU_HDA_AMP_CAPS QEMU_HDA_AMP_NONE
135 #endif
137 /* common: audio output widget */
138 static const desc_param common_params_audio_dac[] = {
140 .id = AC_PAR_AUDIO_WIDGET_CAP,
141 .val = ((AC_WID_AUD_OUT << AC_WCAP_TYPE_SHIFT) |
142 AC_WCAP_FORMAT_OVRD |
143 AC_WCAP_AMP_OVRD |
144 AC_WCAP_OUT_AMP |
145 AC_WCAP_STEREO),
147 .id = AC_PAR_PCM,
148 .val = QEMU_HDA_PCM_FORMATS,
150 .id = AC_PAR_STREAM,
151 .val = AC_SUPFMT_PCM,
153 .id = AC_PAR_AMP_IN_CAP,
154 .val = QEMU_HDA_AMP_NONE,
156 .id = AC_PAR_AMP_OUT_CAP,
157 .val = QEMU_HDA_AMP_CAPS,
161 /* common: audio input widget */
162 static const desc_param common_params_audio_adc[] = {
164 .id = AC_PAR_AUDIO_WIDGET_CAP,
165 .val = ((AC_WID_AUD_IN << AC_WCAP_TYPE_SHIFT) |
166 AC_WCAP_CONN_LIST |
167 AC_WCAP_FORMAT_OVRD |
168 AC_WCAP_AMP_OVRD |
169 AC_WCAP_IN_AMP |
170 AC_WCAP_STEREO),
172 .id = AC_PAR_CONNLIST_LEN,
173 .val = 1,
175 .id = AC_PAR_PCM,
176 .val = QEMU_HDA_PCM_FORMATS,
178 .id = AC_PAR_STREAM,
179 .val = AC_SUPFMT_PCM,
181 .id = AC_PAR_AMP_IN_CAP,
182 .val = QEMU_HDA_AMP_CAPS,
184 .id = AC_PAR_AMP_OUT_CAP,
185 .val = QEMU_HDA_AMP_NONE,
189 /* common: pin widget (line-out) */
190 static const desc_param common_params_audio_lineout[] = {
192 .id = AC_PAR_AUDIO_WIDGET_CAP,
193 .val = ((AC_WID_PIN << AC_WCAP_TYPE_SHIFT) |
194 AC_WCAP_CONN_LIST |
195 AC_WCAP_STEREO),
197 .id = AC_PAR_PIN_CAP,
198 .val = AC_PINCAP_OUT,
200 .id = AC_PAR_CONNLIST_LEN,
201 .val = 1,
203 .id = AC_PAR_AMP_IN_CAP,
204 .val = QEMU_HDA_AMP_NONE,
206 .id = AC_PAR_AMP_OUT_CAP,
207 .val = QEMU_HDA_AMP_NONE,
211 /* common: pin widget (line-in) */
212 static const desc_param common_params_audio_linein[] = {
214 .id = AC_PAR_AUDIO_WIDGET_CAP,
215 .val = ((AC_WID_PIN << AC_WCAP_TYPE_SHIFT) |
216 AC_WCAP_STEREO),
218 .id = AC_PAR_PIN_CAP,
219 .val = AC_PINCAP_IN,
221 .id = AC_PAR_AMP_IN_CAP,
222 .val = QEMU_HDA_AMP_NONE,
224 .id = AC_PAR_AMP_OUT_CAP,
225 .val = QEMU_HDA_AMP_NONE,
229 /* output: root node */
230 static const desc_param output_params_root[] = {
232 .id = AC_PAR_VENDOR_ID,
233 .val = QEMU_HDA_ID_OUTPUT,
235 .id = AC_PAR_SUBSYSTEM_ID,
236 .val = QEMU_HDA_ID_OUTPUT,
238 .id = AC_PAR_REV_ID,
239 .val = 0x00100101,
241 .id = AC_PAR_NODE_COUNT,
242 .val = 0x00010001,
246 /* output: audio function */
247 static const desc_param output_params_audio_func[] = {
249 .id = AC_PAR_FUNCTION_TYPE,
250 .val = AC_GRP_AUDIO_FUNCTION,
252 .id = AC_PAR_SUBSYSTEM_ID,
253 .val = QEMU_HDA_ID_OUTPUT,
255 .id = AC_PAR_NODE_COUNT,
256 .val = 0x00020002,
258 .id = AC_PAR_PCM,
259 .val = QEMU_HDA_PCM_FORMATS,
261 .id = AC_PAR_STREAM,
262 .val = AC_SUPFMT_PCM,
264 .id = AC_PAR_AMP_IN_CAP,
265 .val = QEMU_HDA_AMP_NONE,
267 .id = AC_PAR_AMP_OUT_CAP,
268 .val = QEMU_HDA_AMP_NONE,
270 .id = AC_PAR_GPIO_CAP,
271 .val = 0,
273 .id = AC_PAR_AUDIO_FG_CAP,
274 .val = 0x00000808,
276 .id = AC_PAR_POWER_STATE,
277 .val = 0,
281 /* output: nodes */
282 static const desc_node output_nodes[] = {
284 .nid = AC_NODE_ROOT,
285 .name = "root",
286 .params = output_params_root,
287 .nparams = ARRAY_SIZE(output_params_root),
289 .nid = 1,
290 .name = "func",
291 .params = output_params_audio_func,
292 .nparams = ARRAY_SIZE(output_params_audio_func),
294 .nid = 2,
295 .name = "dac",
296 .params = common_params_audio_dac,
297 .nparams = ARRAY_SIZE(common_params_audio_dac),
298 .stindex = 0,
300 .nid = 3,
301 .name = "out",
302 .params = common_params_audio_lineout,
303 .nparams = ARRAY_SIZE(common_params_audio_lineout),
304 .config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
305 (AC_JACK_LINE_OUT << AC_DEFCFG_DEVICE_SHIFT) |
306 (AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
307 (AC_JACK_COLOR_GREEN << AC_DEFCFG_COLOR_SHIFT) |
308 0x10),
309 .pinctl = AC_PINCTL_OUT_EN,
310 .conn = (uint32_t[]) { 2 },
314 /* output: codec */
315 static const desc_codec output = {
316 .name = "output",
317 .iid = QEMU_HDA_ID_OUTPUT,
318 .nodes = output_nodes,
319 .nnodes = ARRAY_SIZE(output_nodes),
322 /* duplex: root node */
323 static const desc_param duplex_params_root[] = {
325 .id = AC_PAR_VENDOR_ID,
326 .val = QEMU_HDA_ID_DUPLEX,
328 .id = AC_PAR_SUBSYSTEM_ID,
329 .val = QEMU_HDA_ID_DUPLEX,
331 .id = AC_PAR_REV_ID,
332 .val = 0x00100101,
334 .id = AC_PAR_NODE_COUNT,
335 .val = 0x00010001,
339 /* duplex: audio function */
340 static const desc_param duplex_params_audio_func[] = {
342 .id = AC_PAR_FUNCTION_TYPE,
343 .val = AC_GRP_AUDIO_FUNCTION,
345 .id = AC_PAR_SUBSYSTEM_ID,
346 .val = QEMU_HDA_ID_DUPLEX,
348 .id = AC_PAR_NODE_COUNT,
349 .val = 0x00020004,
351 .id = AC_PAR_PCM,
352 .val = QEMU_HDA_PCM_FORMATS,
354 .id = AC_PAR_STREAM,
355 .val = AC_SUPFMT_PCM,
357 .id = AC_PAR_AMP_IN_CAP,
358 .val = QEMU_HDA_AMP_NONE,
360 .id = AC_PAR_AMP_OUT_CAP,
361 .val = QEMU_HDA_AMP_NONE,
363 .id = AC_PAR_GPIO_CAP,
364 .val = 0,
366 .id = AC_PAR_AUDIO_FG_CAP,
367 .val = 0x00000808,
369 .id = AC_PAR_POWER_STATE,
370 .val = 0,
374 /* duplex: nodes */
375 static const desc_node duplex_nodes[] = {
377 .nid = AC_NODE_ROOT,
378 .name = "root",
379 .params = duplex_params_root,
380 .nparams = ARRAY_SIZE(duplex_params_root),
382 .nid = 1,
383 .name = "func",
384 .params = duplex_params_audio_func,
385 .nparams = ARRAY_SIZE(duplex_params_audio_func),
387 .nid = 2,
388 .name = "dac",
389 .params = common_params_audio_dac,
390 .nparams = ARRAY_SIZE(common_params_audio_dac),
391 .stindex = 0,
393 .nid = 3,
394 .name = "out",
395 .params = common_params_audio_lineout,
396 .nparams = ARRAY_SIZE(common_params_audio_lineout),
397 .config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
398 (AC_JACK_LINE_OUT << AC_DEFCFG_DEVICE_SHIFT) |
399 (AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
400 (AC_JACK_COLOR_GREEN << AC_DEFCFG_COLOR_SHIFT) |
401 0x10),
402 .pinctl = AC_PINCTL_OUT_EN,
403 .conn = (uint32_t[]) { 2 },
405 .nid = 4,
406 .name = "adc",
407 .params = common_params_audio_adc,
408 .nparams = ARRAY_SIZE(common_params_audio_adc),
409 .stindex = 1,
410 .conn = (uint32_t[]) { 5 },
412 .nid = 5,
413 .name = "in",
414 .params = common_params_audio_linein,
415 .nparams = ARRAY_SIZE(common_params_audio_linein),
416 .config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
417 (AC_JACK_LINE_IN << AC_DEFCFG_DEVICE_SHIFT) |
418 (AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
419 (AC_JACK_COLOR_RED << AC_DEFCFG_COLOR_SHIFT) |
420 0x20),
421 .pinctl = AC_PINCTL_IN_EN,
425 /* duplex: codec */
426 static const desc_codec duplex = {
427 .name = "duplex",
428 .iid = QEMU_HDA_ID_DUPLEX,
429 .nodes = duplex_nodes,
430 .nnodes = ARRAY_SIZE(duplex_nodes),
433 /* micro: root node */
434 static const desc_param micro_params_root[] = {
436 .id = AC_PAR_VENDOR_ID,
437 .val = QEMU_HDA_ID_MICRO,
439 .id = AC_PAR_SUBSYSTEM_ID,
440 .val = QEMU_HDA_ID_MICRO,
442 .id = AC_PAR_REV_ID,
443 .val = 0x00100101,
445 .id = AC_PAR_NODE_COUNT,
446 .val = 0x00010001,
450 /* micro: audio function */
451 static const desc_param micro_params_audio_func[] = {
453 .id = AC_PAR_FUNCTION_TYPE,
454 .val = AC_GRP_AUDIO_FUNCTION,
456 .id = AC_PAR_SUBSYSTEM_ID,
457 .val = QEMU_HDA_ID_MICRO,
459 .id = AC_PAR_NODE_COUNT,
460 .val = 0x00020004,
462 .id = AC_PAR_PCM,
463 .val = QEMU_HDA_PCM_FORMATS,
465 .id = AC_PAR_STREAM,
466 .val = AC_SUPFMT_PCM,
468 .id = AC_PAR_AMP_IN_CAP,
469 .val = QEMU_HDA_AMP_NONE,
471 .id = AC_PAR_AMP_OUT_CAP,
472 .val = QEMU_HDA_AMP_NONE,
474 .id = AC_PAR_GPIO_CAP,
475 .val = 0,
477 .id = AC_PAR_AUDIO_FG_CAP,
478 .val = 0x00000808,
480 .id = AC_PAR_POWER_STATE,
481 .val = 0,
485 /* micro: nodes */
486 static const desc_node micro_nodes[] = {
488 .nid = AC_NODE_ROOT,
489 .name = "root",
490 .params = micro_params_root,
491 .nparams = ARRAY_SIZE(micro_params_root),
493 .nid = 1,
494 .name = "func",
495 .params = micro_params_audio_func,
496 .nparams = ARRAY_SIZE(micro_params_audio_func),
498 .nid = 2,
499 .name = "dac",
500 .params = common_params_audio_dac,
501 .nparams = ARRAY_SIZE(common_params_audio_dac),
502 .stindex = 0,
504 .nid = 3,
505 .name = "out",
506 .params = common_params_audio_lineout,
507 .nparams = ARRAY_SIZE(common_params_audio_lineout),
508 .config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
509 (AC_JACK_SPEAKER << AC_DEFCFG_DEVICE_SHIFT) |
510 (AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
511 (AC_JACK_COLOR_GREEN << AC_DEFCFG_COLOR_SHIFT) |
512 0x10),
513 .pinctl = AC_PINCTL_OUT_EN,
514 .conn = (uint32_t[]) { 2 },
516 .nid = 4,
517 .name = "adc",
518 .params = common_params_audio_adc,
519 .nparams = ARRAY_SIZE(common_params_audio_adc),
520 .stindex = 1,
521 .conn = (uint32_t[]) { 5 },
523 .nid = 5,
524 .name = "in",
525 .params = common_params_audio_linein,
526 .nparams = ARRAY_SIZE(common_params_audio_linein),
527 .config = ((AC_JACK_PORT_COMPLEX << AC_DEFCFG_PORT_CONN_SHIFT) |
528 (AC_JACK_MIC_IN << AC_DEFCFG_DEVICE_SHIFT) |
529 (AC_JACK_CONN_UNKNOWN << AC_DEFCFG_CONN_TYPE_SHIFT) |
530 (AC_JACK_COLOR_RED << AC_DEFCFG_COLOR_SHIFT) |
531 0x20),
532 .pinctl = AC_PINCTL_IN_EN,
536 /* micro: codec */
537 static const desc_codec micro = {
538 .name = "micro",
539 .iid = QEMU_HDA_ID_MICRO,
540 .nodes = micro_nodes,
541 .nnodes = ARRAY_SIZE(micro_nodes),
544 /* -------------------------------------------------------------------------- */
546 static const char *fmt2name[] = {
547 [ AUD_FMT_U8 ] = "PCM-U8",
548 [ AUD_FMT_S8 ] = "PCM-S8",
549 [ AUD_FMT_U16 ] = "PCM-U16",
550 [ AUD_FMT_S16 ] = "PCM-S16",
551 [ AUD_FMT_U32 ] = "PCM-U32",
552 [ AUD_FMT_S32 ] = "PCM-S32",
555 typedef struct HDAAudioState HDAAudioState;
556 typedef struct HDAAudioStream HDAAudioStream;
558 struct HDAAudioStream {
559 HDAAudioState *state;
560 const desc_node *node;
561 bool output, running;
562 uint32_t stream;
563 uint32_t channel;
564 uint32_t format;
565 uint32_t gain_left, gain_right;
566 bool mute_left, mute_right;
567 struct audsettings as;
568 union {
569 SWVoiceIn *in;
570 SWVoiceOut *out;
571 } voice;
572 uint8_t buf[HDA_BUFFER_SIZE];
573 uint32_t bpos;
576 struct HDAAudioState {
577 HDACodecDevice hda;
578 const char *name;
580 QEMUSoundCard card;
581 const desc_codec *desc;
582 HDAAudioStream st[4];
583 bool running_compat[16];
584 bool running_real[2 * 16];
586 /* properties */
587 uint32_t debug;
590 static void hda_audio_input_cb(void *opaque, int avail)
592 HDAAudioStream *st = opaque;
593 int recv = 0;
594 int len;
595 bool rc;
597 while (avail - recv >= sizeof(st->buf)) {
598 if (st->bpos != sizeof(st->buf)) {
599 len = AUD_read(st->voice.in, st->buf + st->bpos,
600 sizeof(st->buf) - st->bpos);
601 st->bpos += len;
602 recv += len;
603 if (st->bpos != sizeof(st->buf)) {
604 break;
607 rc = hda_codec_xfer(&st->state->hda, st->stream, false,
608 st->buf, sizeof(st->buf));
609 if (!rc) {
610 break;
612 st->bpos = 0;
616 static void hda_audio_output_cb(void *opaque, int avail)
618 HDAAudioStream *st = opaque;
619 int sent = 0;
620 int len;
621 bool rc;
623 while (avail - sent >= sizeof(st->buf)) {
624 if (st->bpos == sizeof(st->buf)) {
625 rc = hda_codec_xfer(&st->state->hda, st->stream, true,
626 st->buf, sizeof(st->buf));
627 if (!rc) {
628 break;
630 st->bpos = 0;
632 len = AUD_write(st->voice.out, st->buf + st->bpos,
633 sizeof(st->buf) - st->bpos);
634 st->bpos += len;
635 sent += len;
636 if (st->bpos != sizeof(st->buf)) {
637 break;
642 static void hda_audio_set_running(HDAAudioStream *st, bool running)
644 if (st->node == NULL) {
645 return;
647 if (st->running == running) {
648 return;
650 st->running = running;
651 dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
652 st->running ? "on" : "off", st->stream);
653 if (st->output) {
654 AUD_set_active_out(st->voice.out, st->running);
655 } else {
656 AUD_set_active_in(st->voice.in, st->running);
660 static void hda_audio_set_amp(HDAAudioStream *st)
662 bool muted;
663 uint32_t left, right;
665 if (st->node == NULL) {
666 return;
669 muted = st->mute_left && st->mute_right;
670 left = st->mute_left ? 0 : st->gain_left;
671 right = st->mute_right ? 0 : st->gain_right;
673 left = left * 255 / QEMU_HDA_AMP_STEPS;
674 right = right * 255 / QEMU_HDA_AMP_STEPS;
676 if (st->output) {
677 AUD_set_volume_out(st->voice.out, muted, left, right);
678 } else {
679 AUD_set_volume_in(st->voice.in, muted, left, right);
683 static void hda_audio_setup(HDAAudioStream *st)
685 if (st->node == NULL) {
686 return;
689 dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
690 st->node->name, st->as.nchannels,
691 fmt2name[st->as.fmt], st->as.freq);
693 if (st->output) {
694 st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
695 st->node->name, st,
696 hda_audio_output_cb, &st->as);
697 } else {
698 st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
699 st->node->name, st,
700 hda_audio_input_cb, &st->as);
704 static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
706 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
707 HDAAudioStream *st;
708 const desc_node *node = NULL;
709 const desc_param *param;
710 uint32_t verb, payload, response, count, shift;
712 if ((data & 0x70000) == 0x70000) {
713 /* 12/8 id/payload */
714 verb = (data >> 8) & 0xfff;
715 payload = data & 0x00ff;
716 } else {
717 /* 4/16 id/payload */
718 verb = (data >> 8) & 0xf00;
719 payload = data & 0xffff;
722 node = hda_codec_find_node(a->desc, nid);
723 if (node == NULL) {
724 goto fail;
726 dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
727 __FUNCTION__, nid, node->name, verb, payload);
729 switch (verb) {
730 /* all nodes */
731 case AC_VERB_PARAMETERS:
732 param = hda_codec_find_param(node, payload);
733 if (param == NULL) {
734 goto fail;
736 hda_codec_response(hda, true, param->val);
737 break;
738 case AC_VERB_GET_SUBSYSTEM_ID:
739 hda_codec_response(hda, true, a->desc->iid);
740 break;
742 /* all functions */
743 case AC_VERB_GET_CONNECT_LIST:
744 param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN);
745 count = param ? param->val : 0;
746 response = 0;
747 shift = 0;
748 while (payload < count && shift < 32) {
749 response |= node->conn[payload] << shift;
750 payload++;
751 shift += 8;
753 hda_codec_response(hda, true, response);
754 break;
756 /* pin widget */
757 case AC_VERB_GET_CONFIG_DEFAULT:
758 hda_codec_response(hda, true, node->config);
759 break;
760 case AC_VERB_GET_PIN_WIDGET_CONTROL:
761 hda_codec_response(hda, true, node->pinctl);
762 break;
763 case AC_VERB_SET_PIN_WIDGET_CONTROL:
764 if (node->pinctl != payload) {
765 dprint(a, 1, "unhandled pin control bit\n");
767 hda_codec_response(hda, true, 0);
768 break;
770 /* audio in/out widget */
771 case AC_VERB_SET_CHANNEL_STREAMID:
772 st = a->st + node->stindex;
773 if (st->node == NULL) {
774 goto fail;
776 hda_audio_set_running(st, false);
777 st->stream = (payload >> 4) & 0x0f;
778 st->channel = payload & 0x0f;
779 dprint(a, 2, "%s: stream %d, channel %d\n",
780 st->node->name, st->stream, st->channel);
781 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
782 hda_codec_response(hda, true, 0);
783 break;
784 case AC_VERB_GET_CONV:
785 st = a->st + node->stindex;
786 if (st->node == NULL) {
787 goto fail;
789 response = st->stream << 4 | st->channel;
790 hda_codec_response(hda, true, response);
791 break;
792 case AC_VERB_SET_STREAM_FORMAT:
793 st = a->st + node->stindex;
794 if (st->node == NULL) {
795 goto fail;
797 st->format = payload;
798 hda_codec_parse_fmt(st->format, &st->as);
799 hda_audio_setup(st);
800 hda_codec_response(hda, true, 0);
801 break;
802 case AC_VERB_GET_STREAM_FORMAT:
803 st = a->st + node->stindex;
804 if (st->node == NULL) {
805 goto fail;
807 hda_codec_response(hda, true, st->format);
808 break;
809 case AC_VERB_GET_AMP_GAIN_MUTE:
810 st = a->st + node->stindex;
811 if (st->node == NULL) {
812 goto fail;
814 if (payload & AC_AMP_GET_LEFT) {
815 response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0);
816 } else {
817 response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0);
819 hda_codec_response(hda, true, response);
820 break;
821 case AC_VERB_SET_AMP_GAIN_MUTE:
822 st = a->st + node->stindex;
823 if (st->node == NULL) {
824 goto fail;
826 dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
827 st->node->name,
828 (payload & AC_AMP_SET_OUTPUT) ? "o" : "-",
829 (payload & AC_AMP_SET_INPUT) ? "i" : "-",
830 (payload & AC_AMP_SET_LEFT) ? "l" : "-",
831 (payload & AC_AMP_SET_RIGHT) ? "r" : "-",
832 (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT,
833 (payload & AC_AMP_GAIN),
834 (payload & AC_AMP_MUTE) ? "muted" : "");
835 if (payload & AC_AMP_SET_LEFT) {
836 st->gain_left = payload & AC_AMP_GAIN;
837 st->mute_left = payload & AC_AMP_MUTE;
839 if (payload & AC_AMP_SET_RIGHT) {
840 st->gain_right = payload & AC_AMP_GAIN;
841 st->mute_right = payload & AC_AMP_MUTE;
843 hda_audio_set_amp(st);
844 hda_codec_response(hda, true, 0);
845 break;
847 /* not supported */
848 case AC_VERB_SET_POWER_STATE:
849 case AC_VERB_GET_POWER_STATE:
850 case AC_VERB_GET_SDI_SELECT:
851 hda_codec_response(hda, true, 0);
852 break;
853 default:
854 goto fail;
856 return;
858 fail:
859 dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
860 __FUNCTION__, nid, node ? node->name : "?", verb, payload);
861 hda_codec_response(hda, true, 0);
864 static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
866 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
867 int s;
869 a->running_compat[stnr] = running;
870 a->running_real[output * 16 + stnr] = running;
871 for (s = 0; s < ARRAY_SIZE(a->st); s++) {
872 if (a->st[s].node == NULL) {
873 continue;
875 if (a->st[s].output != output) {
876 continue;
878 if (a->st[s].stream != stnr) {
879 continue;
881 hda_audio_set_running(&a->st[s], running);
885 static int hda_audio_init(HDACodecDevice *hda, const struct desc_codec *desc)
887 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
888 HDAAudioStream *st;
889 const desc_node *node;
890 const desc_param *param;
891 uint32_t i, type;
893 a->desc = desc;
894 a->name = object_get_typename(OBJECT(a));
895 dprint(a, 1, "%s: cad %d\n", __FUNCTION__, a->hda.cad);
897 AUD_register_card("hda", &a->card);
898 for (i = 0; i < a->desc->nnodes; i++) {
899 node = a->desc->nodes + i;
900 param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP);
901 if (NULL == param)
902 continue;
903 type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
904 switch (type) {
905 case AC_WID_AUD_OUT:
906 case AC_WID_AUD_IN:
907 assert(node->stindex < ARRAY_SIZE(a->st));
908 st = a->st + node->stindex;
909 st->state = a;
910 st->node = node;
911 if (type == AC_WID_AUD_OUT) {
912 /* unmute output by default */
913 st->gain_left = QEMU_HDA_AMP_STEPS;
914 st->gain_right = QEMU_HDA_AMP_STEPS;
915 st->bpos = sizeof(st->buf);
916 st->output = true;
917 } else {
918 st->output = false;
920 st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 |
921 (1 << AC_FMT_CHAN_SHIFT);
922 hda_codec_parse_fmt(st->format, &st->as);
923 hda_audio_setup(st);
924 break;
927 return 0;
930 static int hda_audio_exit(HDACodecDevice *hda)
932 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
933 HDAAudioStream *st;
934 int i;
936 dprint(a, 1, "%s\n", __FUNCTION__);
937 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
938 st = a->st + i;
939 if (st->node == NULL) {
940 continue;
942 if (st->output) {
943 AUD_close_out(&a->card, st->voice.out);
944 } else {
945 AUD_close_in(&a->card, st->voice.in);
948 AUD_remove_card(&a->card);
949 return 0;
952 static int hda_audio_post_load(void *opaque, int version)
954 HDAAudioState *a = opaque;
955 HDAAudioStream *st;
956 int i;
958 dprint(a, 1, "%s\n", __FUNCTION__);
959 if (version == 1) {
960 /* assume running_compat[] is for output streams */
961 for (i = 0; i < ARRAY_SIZE(a->running_compat); i++)
962 a->running_real[16 + i] = a->running_compat[i];
965 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
966 st = a->st + i;
967 if (st->node == NULL)
968 continue;
969 hda_codec_parse_fmt(st->format, &st->as);
970 hda_audio_setup(st);
971 hda_audio_set_amp(st);
972 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
974 return 0;
977 static const VMStateDescription vmstate_hda_audio_stream = {
978 .name = "hda-audio-stream",
979 .version_id = 1,
980 .fields = (VMStateField []) {
981 VMSTATE_UINT32(stream, HDAAudioStream),
982 VMSTATE_UINT32(channel, HDAAudioStream),
983 VMSTATE_UINT32(format, HDAAudioStream),
984 VMSTATE_UINT32(gain_left, HDAAudioStream),
985 VMSTATE_UINT32(gain_right, HDAAudioStream),
986 VMSTATE_BOOL(mute_left, HDAAudioStream),
987 VMSTATE_BOOL(mute_right, HDAAudioStream),
988 VMSTATE_UINT32(bpos, HDAAudioStream),
989 VMSTATE_BUFFER(buf, HDAAudioStream),
990 VMSTATE_END_OF_LIST()
994 static const VMStateDescription vmstate_hda_audio = {
995 .name = "hda-audio",
996 .version_id = 2,
997 .post_load = hda_audio_post_load,
998 .fields = (VMStateField []) {
999 VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0,
1000 vmstate_hda_audio_stream,
1001 HDAAudioStream),
1002 VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
1003 VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
1004 VMSTATE_END_OF_LIST()
1008 static Property hda_audio_properties[] = {
1009 DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0),
1010 DEFINE_PROP_END_OF_LIST(),
1013 static int hda_audio_init_output(HDACodecDevice *hda)
1015 return hda_audio_init(hda, &output);
1018 static int hda_audio_init_duplex(HDACodecDevice *hda)
1020 return hda_audio_init(hda, &duplex);
1023 static int hda_audio_init_micro(HDACodecDevice *hda)
1025 return hda_audio_init(hda, &micro);
1028 static void hda_audio_output_class_init(ObjectClass *klass, void *data)
1030 DeviceClass *dc = DEVICE_CLASS(klass);
1031 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
1033 k->init = hda_audio_init_output;
1034 k->exit = hda_audio_exit;
1035 k->command = hda_audio_command;
1036 k->stream = hda_audio_stream;
1037 dc->desc = "HDA Audio Codec, output-only (line-out)";
1038 dc->vmsd = &vmstate_hda_audio;
1039 dc->props = hda_audio_properties;
1042 static const TypeInfo hda_audio_output_info = {
1043 .name = "hda-output",
1044 .parent = TYPE_HDA_CODEC_DEVICE,
1045 .instance_size = sizeof(HDAAudioState),
1046 .class_init = hda_audio_output_class_init,
1049 static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
1051 DeviceClass *dc = DEVICE_CLASS(klass);
1052 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
1054 k->init = hda_audio_init_duplex;
1055 k->exit = hda_audio_exit;
1056 k->command = hda_audio_command;
1057 k->stream = hda_audio_stream;
1058 dc->desc = "HDA Audio Codec, duplex (line-out, line-in)";
1059 dc->vmsd = &vmstate_hda_audio;
1060 dc->props = hda_audio_properties;
1063 static const TypeInfo hda_audio_duplex_info = {
1064 .name = "hda-duplex",
1065 .parent = TYPE_HDA_CODEC_DEVICE,
1066 .instance_size = sizeof(HDAAudioState),
1067 .class_init = hda_audio_duplex_class_init,
1070 static void hda_audio_micro_class_init(ObjectClass *klass, void *data)
1072 DeviceClass *dc = DEVICE_CLASS(klass);
1073 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
1075 k->init = hda_audio_init_micro;
1076 k->exit = hda_audio_exit;
1077 k->command = hda_audio_command;
1078 k->stream = hda_audio_stream;
1079 dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
1080 dc->vmsd = &vmstate_hda_audio;
1081 dc->props = hda_audio_properties;
1084 static const TypeInfo hda_audio_micro_info = {
1085 .name = "hda-micro",
1086 .parent = TYPE_HDA_CODEC_DEVICE,
1087 .instance_size = sizeof(HDAAudioState),
1088 .class_init = hda_audio_micro_class_init,
1091 static void hda_audio_register_types(void)
1093 type_register_static(&hda_audio_output_info);
1094 type_register_static(&hda_audio_duplex_info);
1095 type_register_static(&hda_audio_micro_info);
1098 type_init(hda_audio_register_types)