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/>.
21 #include "hw/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
{
33 typedef struct desc_node
{
36 const desc_param
*params
;
44 typedef struct desc_codec
{
47 const desc_node
*nodes
;
51 static const desc_param
* hda_codec_find_param(const desc_node
*node
, uint32_t id
)
55 for (i
= 0; i
< node
->nparams
; i
++) {
56 if (node
->params
[i
].id
== id
) {
57 return &node
->params
[i
];
63 static const desc_node
* hda_codec_find_node(const desc_codec
*codec
, uint32_t nid
)
67 for (i
= 0; i
< codec
->nnodes
; i
++) {
68 if (codec
->nodes
[i
].nid
== nid
) {
69 return &codec
->nodes
[i
];
75 static void hda_codec_parse_fmt(uint32_t format
, struct audsettings
*as
)
77 if (format
& AC_FMT_TYPE_NON_PCM
) {
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
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
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 \
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))
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
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
|
148 .val
= QEMU_HDA_PCM_FORMATS
,
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
) |
167 AC_WCAP_FORMAT_OVRD
|
172 .id
= AC_PAR_CONNLIST_LEN
,
176 .val
= QEMU_HDA_PCM_FORMATS
,
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
) |
197 .id
= AC_PAR_PIN_CAP
,
198 .val
= AC_PINCAP_OUT
,
200 .id
= AC_PAR_CONNLIST_LEN
,
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
) |
218 .id
= AC_PAR_PIN_CAP
,
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
,
241 .id
= AC_PAR_NODE_COUNT
,
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
,
259 .val
= QEMU_HDA_PCM_FORMATS
,
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
,
273 .id
= AC_PAR_AUDIO_FG_CAP
,
276 .id
= AC_PAR_POWER_STATE
,
282 static const desc_node output_nodes
[] = {
286 .params
= output_params_root
,
287 .nparams
= ARRAY_SIZE(output_params_root
),
291 .params
= output_params_audio_func
,
292 .nparams
= ARRAY_SIZE(output_params_audio_func
),
296 .params
= common_params_audio_dac
,
297 .nparams
= ARRAY_SIZE(common_params_audio_dac
),
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
) |
309 .pinctl
= AC_PINCTL_OUT_EN
,
310 .conn
= (uint32_t[]) { 2 },
315 static const desc_codec 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
,
334 .id
= AC_PAR_NODE_COUNT
,
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
,
352 .val
= QEMU_HDA_PCM_FORMATS
,
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
,
366 .id
= AC_PAR_AUDIO_FG_CAP
,
369 .id
= AC_PAR_POWER_STATE
,
375 static const desc_node duplex_nodes
[] = {
379 .params
= duplex_params_root
,
380 .nparams
= ARRAY_SIZE(duplex_params_root
),
384 .params
= duplex_params_audio_func
,
385 .nparams
= ARRAY_SIZE(duplex_params_audio_func
),
389 .params
= common_params_audio_dac
,
390 .nparams
= ARRAY_SIZE(common_params_audio_dac
),
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
) |
402 .pinctl
= AC_PINCTL_OUT_EN
,
403 .conn
= (uint32_t[]) { 2 },
407 .params
= common_params_audio_adc
,
408 .nparams
= ARRAY_SIZE(common_params_audio_adc
),
410 .conn
= (uint32_t[]) { 5 },
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
) |
421 .pinctl
= AC_PINCTL_IN_EN
,
426 static const desc_codec 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
,
445 .id
= AC_PAR_NODE_COUNT
,
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
,
463 .val
= QEMU_HDA_PCM_FORMATS
,
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
,
477 .id
= AC_PAR_AUDIO_FG_CAP
,
480 .id
= AC_PAR_POWER_STATE
,
486 static const desc_node micro_nodes
[] = {
490 .params
= micro_params_root
,
491 .nparams
= ARRAY_SIZE(micro_params_root
),
495 .params
= micro_params_audio_func
,
496 .nparams
= ARRAY_SIZE(micro_params_audio_func
),
500 .params
= common_params_audio_dac
,
501 .nparams
= ARRAY_SIZE(common_params_audio_dac
),
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
) |
513 .pinctl
= AC_PINCTL_OUT_EN
,
514 .conn
= (uint32_t[]) { 2 },
518 .params
= common_params_audio_adc
,
519 .nparams
= ARRAY_SIZE(common_params_audio_adc
),
521 .conn
= (uint32_t[]) { 5 },
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
) |
532 .pinctl
= AC_PINCTL_IN_EN
,
537 static const desc_codec 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
;
565 uint32_t gain_left
, gain_right
;
566 bool mute_left
, mute_right
;
567 struct audsettings as
;
572 uint8_t buf
[HDA_BUFFER_SIZE
];
576 struct HDAAudioState
{
581 const desc_codec
*desc
;
582 HDAAudioStream st
[4];
583 bool running_compat
[16];
584 bool running_real
[2 * 16];
590 static void hda_audio_input_cb(void *opaque
, int avail
)
592 HDAAudioStream
*st
= opaque
;
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
);
603 if (st
->bpos
!= sizeof(st
->buf
)) {
607 rc
= hda_codec_xfer(&st
->state
->hda
, st
->stream
, false,
608 st
->buf
, sizeof(st
->buf
));
616 static void hda_audio_output_cb(void *opaque
, int avail
)
618 HDAAudioStream
*st
= opaque
;
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
));
632 len
= AUD_write(st
->voice
.out
, st
->buf
+ st
->bpos
,
633 sizeof(st
->buf
) - st
->bpos
);
636 if (st
->bpos
!= sizeof(st
->buf
)) {
642 static void hda_audio_set_running(HDAAudioStream
*st
, bool running
)
644 if (st
->node
== NULL
) {
647 if (st
->running
== running
) {
650 st
->running
= running
;
651 dprint(st
->state
, 1, "%s: %s (stream %d)\n", st
->node
->name
,
652 st
->running
? "on" : "off", st
->stream
);
654 AUD_set_active_out(st
->voice
.out
, st
->running
);
656 AUD_set_active_in(st
->voice
.in
, st
->running
);
660 static void hda_audio_set_amp(HDAAudioStream
*st
)
663 uint32_t left
, right
;
665 if (st
->node
== NULL
) {
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
;
677 AUD_set_volume_out(st
->voice
.out
, muted
, left
, right
);
679 AUD_set_volume_in(st
->voice
.in
, muted
, left
, right
);
683 static void hda_audio_setup(HDAAudioStream
*st
)
685 if (st
->node
== NULL
) {
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
);
694 st
->voice
.out
= AUD_open_out(&st
->state
->card
, st
->voice
.out
,
696 hda_audio_output_cb
, &st
->as
);
698 st
->voice
.in
= AUD_open_in(&st
->state
->card
, st
->voice
.in
,
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
);
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;
717 /* 4/16 id/payload */
718 verb
= (data
>> 8) & 0xf00;
719 payload
= data
& 0xffff;
722 node
= hda_codec_find_node(a
->desc
, nid
);
726 dprint(a
, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
727 __FUNCTION__
, nid
, node
->name
, verb
, payload
);
731 case AC_VERB_PARAMETERS
:
732 param
= hda_codec_find_param(node
, payload
);
736 hda_codec_response(hda
, true, param
->val
);
738 case AC_VERB_GET_SUBSYSTEM_ID
:
739 hda_codec_response(hda
, true, a
->desc
->iid
);
743 case AC_VERB_GET_CONNECT_LIST
:
744 param
= hda_codec_find_param(node
, AC_PAR_CONNLIST_LEN
);
745 count
= param
? param
->val
: 0;
748 while (payload
< count
&& shift
< 32) {
749 response
|= node
->conn
[payload
] << shift
;
753 hda_codec_response(hda
, true, response
);
757 case AC_VERB_GET_CONFIG_DEFAULT
:
758 hda_codec_response(hda
, true, node
->config
);
760 case AC_VERB_GET_PIN_WIDGET_CONTROL
:
761 hda_codec_response(hda
, true, node
->pinctl
);
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);
770 /* audio in/out widget */
771 case AC_VERB_SET_CHANNEL_STREAMID
:
772 st
= a
->st
+ node
->stindex
;
773 if (st
->node
== NULL
) {
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);
784 case AC_VERB_GET_CONV
:
785 st
= a
->st
+ node
->stindex
;
786 if (st
->node
== NULL
) {
789 response
= st
->stream
<< 4 | st
->channel
;
790 hda_codec_response(hda
, true, response
);
792 case AC_VERB_SET_STREAM_FORMAT
:
793 st
= a
->st
+ node
->stindex
;
794 if (st
->node
== NULL
) {
797 st
->format
= payload
;
798 hda_codec_parse_fmt(st
->format
, &st
->as
);
800 hda_codec_response(hda
, true, 0);
802 case AC_VERB_GET_STREAM_FORMAT
:
803 st
= a
->st
+ node
->stindex
;
804 if (st
->node
== NULL
) {
807 hda_codec_response(hda
, true, st
->format
);
809 case AC_VERB_GET_AMP_GAIN_MUTE
:
810 st
= a
->st
+ node
->stindex
;
811 if (st
->node
== NULL
) {
814 if (payload
& AC_AMP_GET_LEFT
) {
815 response
= st
->gain_left
| (st
->mute_left
? AC_AMP_MUTE
: 0);
817 response
= st
->gain_right
| (st
->mute_right
? AC_AMP_MUTE
: 0);
819 hda_codec_response(hda
, true, response
);
821 case AC_VERB_SET_AMP_GAIN_MUTE
:
822 st
= a
->st
+ node
->stindex
;
823 if (st
->node
== NULL
) {
826 dprint(a
, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
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);
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);
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
);
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
) {
875 if (a
->st
[s
].output
!= output
) {
878 if (a
->st
[s
].stream
!= stnr
) {
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
);
889 const desc_node
*node
;
890 const desc_param
*param
;
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
);
903 type
= (param
->val
& AC_WCAP_TYPE
) >> AC_WCAP_TYPE_SHIFT
;
907 assert(node
->stindex
< ARRAY_SIZE(a
->st
));
908 st
= a
->st
+ node
->stindex
;
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
);
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
);
930 static int hda_audio_exit(HDACodecDevice
*hda
)
932 HDAAudioState
*a
= DO_UPCAST(HDAAudioState
, hda
, hda
);
936 dprint(a
, 1, "%s\n", __FUNCTION__
);
937 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
939 if (st
->node
== NULL
) {
943 AUD_close_out(&a
->card
, st
->voice
.out
);
945 AUD_close_in(&a
->card
, st
->voice
.in
);
948 AUD_remove_card(&a
->card
);
952 static int hda_audio_post_load(void *opaque
, int version
)
954 HDAAudioState
*a
= opaque
;
958 dprint(a
, 1, "%s\n", __FUNCTION__
);
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
++) {
967 if (st
->node
== NULL
)
969 hda_codec_parse_fmt(st
->format
, &st
->as
);
971 hda_audio_set_amp(st
);
972 hda_audio_set_running(st
, a
->running_real
[st
->output
* 16 + st
->stream
]);
977 static const VMStateDescription vmstate_hda_audio_stream
= {
978 .name
= "hda-audio-stream",
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
= {
997 .post_load
= hda_audio_post_load
,
998 .fields
= (VMStateField
[]) {
999 VMSTATE_STRUCT_ARRAY(st
, HDAAudioState
, 4, 0,
1000 vmstate_hda_audio_stream
,
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
, µ
);
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
)