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 "qemu/osdep.h"
21 #include "hw/pci/pci.h"
22 #include "hw/qdev-properties.h"
23 #include "intel-hda.h"
24 #include "migration/vmstate.h"
25 #include "qemu/module.h"
26 #include "intel-hda-defs.h"
27 #include "audio/audio.h"
29 #include "qom/object.h"
31 /* -------------------------------------------------------------------------- */
33 typedef struct desc_param
{
38 typedef struct desc_node
{
41 const desc_param
*params
;
49 typedef struct desc_codec
{
52 const desc_node
*nodes
;
56 static const desc_param
* hda_codec_find_param(const desc_node
*node
, uint32_t id
)
60 for (i
= 0; i
< node
->nparams
; i
++) {
61 if (node
->params
[i
].id
== id
) {
62 return &node
->params
[i
];
68 static const desc_node
* hda_codec_find_node(const desc_codec
*codec
, uint32_t nid
)
72 for (i
= 0; i
< codec
->nnodes
; i
++) {
73 if (codec
->nodes
[i
].nid
== nid
) {
74 return &codec
->nodes
[i
];
80 static void hda_codec_parse_fmt(uint32_t format
, struct audsettings
*as
)
82 if (format
& AC_FMT_TYPE_NON_PCM
) {
86 as
->freq
= (format
& AC_FMT_BASE_44K
) ? 44100 : 48000;
88 switch ((format
& AC_FMT_MULT_MASK
) >> AC_FMT_MULT_SHIFT
) {
89 case 1: as
->freq
*= 2; break;
90 case 2: as
->freq
*= 3; break;
91 case 3: as
->freq
*= 4; break;
94 switch ((format
& AC_FMT_DIV_MASK
) >> AC_FMT_DIV_SHIFT
) {
95 case 1: as
->freq
/= 2; break;
96 case 2: as
->freq
/= 3; break;
97 case 3: as
->freq
/= 4; break;
98 case 4: as
->freq
/= 5; break;
99 case 5: as
->freq
/= 6; break;
100 case 6: as
->freq
/= 7; break;
101 case 7: as
->freq
/= 8; break;
104 switch (format
& AC_FMT_BITS_MASK
) {
105 case AC_FMT_BITS_8
: as
->fmt
= AUDIO_FORMAT_S8
; break;
106 case AC_FMT_BITS_16
: as
->fmt
= AUDIO_FORMAT_S16
; break;
107 case AC_FMT_BITS_32
: as
->fmt
= AUDIO_FORMAT_S32
; break;
110 as
->nchannels
= ((format
& AC_FMT_CHAN_MASK
) >> AC_FMT_CHAN_SHIFT
) + 1;
113 /* -------------------------------------------------------------------------- */
115 * HDA codec descriptions
120 #define QEMU_HDA_ID_VENDOR 0x1af4
121 #define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
122 0x1fc /* 16 -> 96 kHz */)
123 #define QEMU_HDA_AMP_NONE (0)
124 #define QEMU_HDA_AMP_STEPS 0x4a
128 #include "hda-codec-common.h"
130 #define PARAM nomixemu
131 #include "hda-codec-common.h"
133 #define HDA_TIMER_TICKS (SCALE_MS)
134 #define B_SIZE sizeof(st->buf)
135 #define B_MASK (sizeof(st->buf) - 1)
137 /* -------------------------------------------------------------------------- */
139 static const char *fmt2name
[] = {
140 [ AUDIO_FORMAT_U8
] = "PCM-U8",
141 [ AUDIO_FORMAT_S8
] = "PCM-S8",
142 [ AUDIO_FORMAT_U16
] = "PCM-U16",
143 [ AUDIO_FORMAT_S16
] = "PCM-S16",
144 [ AUDIO_FORMAT_U32
] = "PCM-U32",
145 [ AUDIO_FORMAT_S32
] = "PCM-S32",
148 typedef struct HDAAudioState HDAAudioState
;
149 typedef struct HDAAudioStream HDAAudioStream
;
151 struct HDAAudioStream
{
152 HDAAudioState
*state
;
153 const desc_node
*node
;
154 bool output
, running
;
158 uint32_t gain_left
, gain_right
;
159 bool mute_left
, mute_right
;
160 struct audsettings as
;
165 uint8_t compat_buf
[HDA_BUFFER_SIZE
];
166 uint32_t compat_bpos
;
167 uint8_t buf
[8192]; /* size must be power of two */
174 #define TYPE_HDA_AUDIO "hda-audio"
175 OBJECT_DECLARE_SIMPLE_TYPE(HDAAudioState
, HDA_AUDIO
)
177 struct HDAAudioState
{
182 const desc_codec
*desc
;
183 HDAAudioStream st
[4];
184 bool running_compat
[16];
185 bool running_real
[2 * 16];
193 static inline int64_t hda_bytes_per_second(HDAAudioStream
*st
)
195 return 2LL * st
->as
.nchannels
* st
->as
.freq
;
198 static inline void hda_timer_sync_adjust(HDAAudioStream
*st
, int64_t target_pos
)
200 int64_t limit
= B_SIZE
/ 8;
203 if (target_pos
> limit
) {
204 corr
= HDA_TIMER_TICKS
;
206 if (target_pos
< -limit
) {
207 corr
= -HDA_TIMER_TICKS
;
209 if (target_pos
< -(2 * limit
)) {
210 corr
= -(4 * HDA_TIMER_TICKS
);
216 trace_hda_audio_adjust(st
->node
->name
, target_pos
);
217 st
->buft_start
+= corr
;
220 static void hda_audio_input_timer(void *opaque
)
222 HDAAudioStream
*st
= opaque
;
224 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
226 int64_t buft_start
= st
->buft_start
;
227 int64_t wpos
= st
->wpos
;
228 int64_t rpos
= st
->rpos
;
230 int64_t wanted_rpos
= hda_bytes_per_second(st
) * (now
- buft_start
)
231 / NANOSECONDS_PER_SECOND
;
232 wanted_rpos
&= -4; /* IMPORTANT! clip to frames */
234 if (wanted_rpos
<= rpos
) {
235 /* we already transmitted the data */
239 int64_t to_transfer
= MIN(wpos
- rpos
, wanted_rpos
- rpos
);
240 while (to_transfer
) {
241 uint32_t start
= (rpos
& B_MASK
);
242 uint32_t chunk
= MIN(B_SIZE
- start
, to_transfer
);
243 int rc
= hda_codec_xfer(
244 &st
->state
->hda
, st
->stream
, false, st
->buf
+ start
, chunk
);
249 to_transfer
-= chunk
;
256 timer_mod_anticipate_ns(st
->buft
, now
+ HDA_TIMER_TICKS
);
260 static void hda_audio_input_cb(void *opaque
, int avail
)
262 HDAAudioStream
*st
= opaque
;
264 int64_t wpos
= st
->wpos
;
265 int64_t rpos
= st
->rpos
;
267 int64_t to_transfer
= MIN(B_SIZE
- (wpos
- rpos
), avail
);
269 while (to_transfer
) {
270 uint32_t start
= (uint32_t) (wpos
& B_MASK
);
271 uint32_t chunk
= (uint32_t) MIN(B_SIZE
- start
, to_transfer
);
272 uint32_t read
= AUD_read(st
->voice
.in
, st
->buf
+ start
, chunk
);
281 hda_timer_sync_adjust(st
, -((wpos
- rpos
) - (B_SIZE
>> 1)));
284 static void hda_audio_output_timer(void *opaque
)
286 HDAAudioStream
*st
= opaque
;
288 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
290 int64_t buft_start
= st
->buft_start
;
291 int64_t wpos
= st
->wpos
;
292 int64_t rpos
= st
->rpos
;
294 int64_t wanted_wpos
= hda_bytes_per_second(st
) * (now
- buft_start
)
295 / NANOSECONDS_PER_SECOND
;
296 wanted_wpos
&= -4; /* IMPORTANT! clip to frames */
298 if (wanted_wpos
<= wpos
) {
299 /* we already received the data */
303 int64_t to_transfer
= MIN(B_SIZE
- (wpos
- rpos
), wanted_wpos
- wpos
);
304 while (to_transfer
) {
305 uint32_t start
= (wpos
& B_MASK
);
306 uint32_t chunk
= MIN(B_SIZE
- start
, to_transfer
);
307 int rc
= hda_codec_xfer(
308 &st
->state
->hda
, st
->stream
, true, st
->buf
+ start
, chunk
);
313 to_transfer
-= chunk
;
320 timer_mod_anticipate_ns(st
->buft
, now
+ HDA_TIMER_TICKS
);
324 static void hda_audio_output_cb(void *opaque
, int avail
)
326 HDAAudioStream
*st
= opaque
;
328 int64_t wpos
= st
->wpos
;
329 int64_t rpos
= st
->rpos
;
331 int64_t to_transfer
= MIN(wpos
- rpos
, avail
);
333 if (wpos
- rpos
== B_SIZE
) {
334 /* drop buffer, reset timer adjust */
337 st
->buft_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
338 trace_hda_audio_overrun(st
->node
->name
);
342 while (to_transfer
) {
343 uint32_t start
= (uint32_t) (rpos
& B_MASK
);
344 uint32_t chunk
= (uint32_t) MIN(B_SIZE
- start
, to_transfer
);
345 uint32_t written
= AUD_write(st
->voice
.out
, st
->buf
+ start
, chunk
);
347 to_transfer
-= written
;
349 if (chunk
!= written
) {
354 hda_timer_sync_adjust(st
, (wpos
- rpos
) - (B_SIZE
>> 1));
357 static void hda_audio_compat_input_cb(void *opaque
, int avail
)
359 HDAAudioStream
*st
= opaque
;
364 while (avail
- recv
>= sizeof(st
->compat_buf
)) {
365 if (st
->compat_bpos
!= sizeof(st
->compat_buf
)) {
366 len
= AUD_read(st
->voice
.in
, st
->compat_buf
+ st
->compat_bpos
,
367 sizeof(st
->compat_buf
) - st
->compat_bpos
);
368 st
->compat_bpos
+= len
;
370 if (st
->compat_bpos
!= sizeof(st
->compat_buf
)) {
374 rc
= hda_codec_xfer(&st
->state
->hda
, st
->stream
, false,
375 st
->compat_buf
, sizeof(st
->compat_buf
));
383 static void hda_audio_compat_output_cb(void *opaque
, int avail
)
385 HDAAudioStream
*st
= opaque
;
390 while (avail
- sent
>= sizeof(st
->compat_buf
)) {
391 if (st
->compat_bpos
== sizeof(st
->compat_buf
)) {
392 rc
= hda_codec_xfer(&st
->state
->hda
, st
->stream
, true,
393 st
->compat_buf
, sizeof(st
->compat_buf
));
399 len
= AUD_write(st
->voice
.out
, st
->compat_buf
+ st
->compat_bpos
,
400 sizeof(st
->compat_buf
) - st
->compat_bpos
);
401 st
->compat_bpos
+= len
;
403 if (st
->compat_bpos
!= sizeof(st
->compat_buf
)) {
409 static void hda_audio_set_running(HDAAudioStream
*st
, bool running
)
411 if (st
->node
== NULL
) {
414 if (st
->running
== running
) {
417 st
->running
= running
;
418 trace_hda_audio_running(st
->node
->name
, st
->stream
, st
->running
);
419 if (st
->state
->use_timer
) {
421 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
424 st
->buft_start
= now
;
425 timer_mod_anticipate_ns(st
->buft
, now
+ HDA_TIMER_TICKS
);
431 AUD_set_active_out(st
->voice
.out
, st
->running
);
433 AUD_set_active_in(st
->voice
.in
, st
->running
);
437 static void hda_audio_set_amp(HDAAudioStream
*st
)
440 uint32_t left
, right
;
442 if (st
->node
== NULL
) {
446 muted
= st
->mute_left
&& st
->mute_right
;
447 left
= st
->mute_left
? 0 : st
->gain_left
;
448 right
= st
->mute_right
? 0 : st
->gain_right
;
450 left
= left
* 255 / QEMU_HDA_AMP_STEPS
;
451 right
= right
* 255 / QEMU_HDA_AMP_STEPS
;
453 if (!st
->state
->mixer
) {
457 AUD_set_volume_out(st
->voice
.out
, muted
, left
, right
);
459 AUD_set_volume_in(st
->voice
.in
, muted
, left
, right
);
463 static void hda_audio_setup(HDAAudioStream
*st
)
465 bool use_timer
= st
->state
->use_timer
;
466 audio_callback_fn cb
;
468 if (st
->node
== NULL
) {
472 trace_hda_audio_format(st
->node
->name
, st
->as
.nchannels
,
473 fmt2name
[st
->as
.fmt
], st
->as
.freq
);
477 cb
= hda_audio_output_cb
;
478 st
->buft
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
479 hda_audio_output_timer
, st
);
481 cb
= hda_audio_compat_output_cb
;
483 st
->voice
.out
= AUD_open_out(&st
->state
->card
, st
->voice
.out
,
484 st
->node
->name
, st
, cb
, &st
->as
);
487 cb
= hda_audio_input_cb
;
488 st
->buft
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
489 hda_audio_input_timer
, st
);
491 cb
= hda_audio_compat_input_cb
;
493 st
->voice
.in
= AUD_open_in(&st
->state
->card
, st
->voice
.in
,
494 st
->node
->name
, st
, cb
, &st
->as
);
498 static void hda_audio_command(HDACodecDevice
*hda
, uint32_t nid
, uint32_t data
)
500 HDAAudioState
*a
= HDA_AUDIO(hda
);
502 const desc_node
*node
= NULL
;
503 const desc_param
*param
;
504 uint32_t verb
, payload
, response
, count
, shift
;
506 if ((data
& 0x70000) == 0x70000) {
507 /* 12/8 id/payload */
508 verb
= (data
>> 8) & 0xfff;
509 payload
= data
& 0x00ff;
511 /* 4/16 id/payload */
512 verb
= (data
>> 8) & 0xf00;
513 payload
= data
& 0xffff;
516 node
= hda_codec_find_node(a
->desc
, nid
);
520 dprint(a
, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
521 __func__
, nid
, node
->name
, verb
, payload
);
525 case AC_VERB_PARAMETERS
:
526 param
= hda_codec_find_param(node
, payload
);
530 hda_codec_response(hda
, true, param
->val
);
532 case AC_VERB_GET_SUBSYSTEM_ID
:
533 hda_codec_response(hda
, true, a
->desc
->iid
);
537 case AC_VERB_GET_CONNECT_LIST
:
538 param
= hda_codec_find_param(node
, AC_PAR_CONNLIST_LEN
);
539 count
= param
? param
->val
: 0;
542 while (payload
< count
&& shift
< 32) {
543 response
|= node
->conn
[payload
] << shift
;
547 hda_codec_response(hda
, true, response
);
551 case AC_VERB_GET_CONFIG_DEFAULT
:
552 hda_codec_response(hda
, true, node
->config
);
554 case AC_VERB_GET_PIN_WIDGET_CONTROL
:
555 hda_codec_response(hda
, true, node
->pinctl
);
557 case AC_VERB_SET_PIN_WIDGET_CONTROL
:
558 if (node
->pinctl
!= payload
) {
559 dprint(a
, 1, "unhandled pin control bit\n");
561 hda_codec_response(hda
, true, 0);
564 /* audio in/out widget */
565 case AC_VERB_SET_CHANNEL_STREAMID
:
566 st
= a
->st
+ node
->stindex
;
567 if (st
->node
== NULL
) {
570 hda_audio_set_running(st
, false);
571 st
->stream
= (payload
>> 4) & 0x0f;
572 st
->channel
= payload
& 0x0f;
573 dprint(a
, 2, "%s: stream %d, channel %d\n",
574 st
->node
->name
, st
->stream
, st
->channel
);
575 hda_audio_set_running(st
, a
->running_real
[st
->output
* 16 + st
->stream
]);
576 hda_codec_response(hda
, true, 0);
578 case AC_VERB_GET_CONV
:
579 st
= a
->st
+ node
->stindex
;
580 if (st
->node
== NULL
) {
583 response
= st
->stream
<< 4 | st
->channel
;
584 hda_codec_response(hda
, true, response
);
586 case AC_VERB_SET_STREAM_FORMAT
:
587 st
= a
->st
+ node
->stindex
;
588 if (st
->node
== NULL
) {
591 st
->format
= payload
;
592 hda_codec_parse_fmt(st
->format
, &st
->as
);
594 hda_codec_response(hda
, true, 0);
596 case AC_VERB_GET_STREAM_FORMAT
:
597 st
= a
->st
+ node
->stindex
;
598 if (st
->node
== NULL
) {
601 hda_codec_response(hda
, true, st
->format
);
603 case AC_VERB_GET_AMP_GAIN_MUTE
:
604 st
= a
->st
+ node
->stindex
;
605 if (st
->node
== NULL
) {
608 if (payload
& AC_AMP_GET_LEFT
) {
609 response
= st
->gain_left
| (st
->mute_left
? AC_AMP_MUTE
: 0);
611 response
= st
->gain_right
| (st
->mute_right
? AC_AMP_MUTE
: 0);
613 hda_codec_response(hda
, true, response
);
615 case AC_VERB_SET_AMP_GAIN_MUTE
:
616 st
= a
->st
+ node
->stindex
;
617 if (st
->node
== NULL
) {
620 dprint(a
, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
622 (payload
& AC_AMP_SET_OUTPUT
) ? "o" : "-",
623 (payload
& AC_AMP_SET_INPUT
) ? "i" : "-",
624 (payload
& AC_AMP_SET_LEFT
) ? "l" : "-",
625 (payload
& AC_AMP_SET_RIGHT
) ? "r" : "-",
626 (payload
& AC_AMP_SET_INDEX
) >> AC_AMP_SET_INDEX_SHIFT
,
627 (payload
& AC_AMP_GAIN
),
628 (payload
& AC_AMP_MUTE
) ? "muted" : "");
629 if (payload
& AC_AMP_SET_LEFT
) {
630 st
->gain_left
= payload
& AC_AMP_GAIN
;
631 st
->mute_left
= payload
& AC_AMP_MUTE
;
633 if (payload
& AC_AMP_SET_RIGHT
) {
634 st
->gain_right
= payload
& AC_AMP_GAIN
;
635 st
->mute_right
= payload
& AC_AMP_MUTE
;
637 hda_audio_set_amp(st
);
638 hda_codec_response(hda
, true, 0);
642 case AC_VERB_SET_POWER_STATE
:
643 case AC_VERB_GET_POWER_STATE
:
644 case AC_VERB_GET_SDI_SELECT
:
645 hda_codec_response(hda
, true, 0);
653 dprint(a
, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
654 __func__
, nid
, node
? node
->name
: "?", verb
, payload
);
655 hda_codec_response(hda
, true, 0);
658 static void hda_audio_stream(HDACodecDevice
*hda
, uint32_t stnr
, bool running
, bool output
)
660 HDAAudioState
*a
= HDA_AUDIO(hda
);
663 a
->running_compat
[stnr
] = running
;
664 a
->running_real
[output
* 16 + stnr
] = running
;
665 for (s
= 0; s
< ARRAY_SIZE(a
->st
); s
++) {
666 if (a
->st
[s
].node
== NULL
) {
669 if (a
->st
[s
].output
!= output
) {
672 if (a
->st
[s
].stream
!= stnr
) {
675 hda_audio_set_running(&a
->st
[s
], running
);
679 static int hda_audio_init(HDACodecDevice
*hda
, const struct desc_codec
*desc
)
681 HDAAudioState
*a
= HDA_AUDIO(hda
);
683 const desc_node
*node
;
684 const desc_param
*param
;
688 a
->name
= object_get_typename(OBJECT(a
));
689 dprint(a
, 1, "%s: cad %d\n", __func__
, a
->hda
.cad
);
691 AUD_register_card("hda", &a
->card
);
692 for (i
= 0; i
< a
->desc
->nnodes
; i
++) {
693 node
= a
->desc
->nodes
+ i
;
694 param
= hda_codec_find_param(node
, AC_PAR_AUDIO_WIDGET_CAP
);
698 type
= (param
->val
& AC_WCAP_TYPE
) >> AC_WCAP_TYPE_SHIFT
;
702 assert(node
->stindex
< ARRAY_SIZE(a
->st
));
703 st
= a
->st
+ node
->stindex
;
706 if (type
== AC_WID_AUD_OUT
) {
707 /* unmute output by default */
708 st
->gain_left
= QEMU_HDA_AMP_STEPS
;
709 st
->gain_right
= QEMU_HDA_AMP_STEPS
;
710 st
->compat_bpos
= sizeof(st
->compat_buf
);
715 st
->format
= AC_FMT_TYPE_PCM
| AC_FMT_BITS_16
|
716 (1 << AC_FMT_CHAN_SHIFT
);
717 hda_codec_parse_fmt(st
->format
, &st
->as
);
725 static void hda_audio_exit(HDACodecDevice
*hda
)
727 HDAAudioState
*a
= HDA_AUDIO(hda
);
731 dprint(a
, 1, "%s\n", __func__
);
732 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
734 if (st
->node
== NULL
) {
741 AUD_close_out(&a
->card
, st
->voice
.out
);
743 AUD_close_in(&a
->card
, st
->voice
.in
);
746 AUD_remove_card(&a
->card
);
749 static int hda_audio_post_load(void *opaque
, int version
)
751 HDAAudioState
*a
= opaque
;
755 dprint(a
, 1, "%s\n", __func__
);
757 /* assume running_compat[] is for output streams */
758 for (i
= 0; i
< ARRAY_SIZE(a
->running_compat
); i
++)
759 a
->running_real
[16 + i
] = a
->running_compat
[i
];
762 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
764 if (st
->node
== NULL
)
766 hda_codec_parse_fmt(st
->format
, &st
->as
);
768 hda_audio_set_amp(st
);
769 hda_audio_set_running(st
, a
->running_real
[st
->output
* 16 + st
->stream
]);
774 static void hda_audio_reset(DeviceState
*dev
)
776 HDAAudioState
*a
= HDA_AUDIO(dev
);
780 dprint(a
, 1, "%s\n", __func__
);
781 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
783 if (st
->node
!= NULL
) {
784 hda_audio_set_running(st
, false);
789 static bool vmstate_hda_audio_stream_buf_needed(void *opaque
)
791 HDAAudioStream
*st
= opaque
;
792 return st
->state
&& st
->state
->use_timer
;
795 static const VMStateDescription vmstate_hda_audio_stream_buf
= {
796 .name
= "hda-audio-stream/buffer",
798 .needed
= vmstate_hda_audio_stream_buf_needed
,
799 .fields
= (VMStateField
[]) {
800 VMSTATE_BUFFER(buf
, HDAAudioStream
),
801 VMSTATE_INT64(rpos
, HDAAudioStream
),
802 VMSTATE_INT64(wpos
, HDAAudioStream
),
803 VMSTATE_TIMER_PTR(buft
, HDAAudioStream
),
804 VMSTATE_INT64(buft_start
, HDAAudioStream
),
805 VMSTATE_END_OF_LIST()
809 static const VMStateDescription vmstate_hda_audio_stream
= {
810 .name
= "hda-audio-stream",
812 .fields
= (VMStateField
[]) {
813 VMSTATE_UINT32(stream
, HDAAudioStream
),
814 VMSTATE_UINT32(channel
, HDAAudioStream
),
815 VMSTATE_UINT32(format
, HDAAudioStream
),
816 VMSTATE_UINT32(gain_left
, HDAAudioStream
),
817 VMSTATE_UINT32(gain_right
, HDAAudioStream
),
818 VMSTATE_BOOL(mute_left
, HDAAudioStream
),
819 VMSTATE_BOOL(mute_right
, HDAAudioStream
),
820 VMSTATE_UINT32(compat_bpos
, HDAAudioStream
),
821 VMSTATE_BUFFER(compat_buf
, HDAAudioStream
),
822 VMSTATE_END_OF_LIST()
824 .subsections
= (const VMStateDescription
* []) {
825 &vmstate_hda_audio_stream_buf
,
830 static const VMStateDescription vmstate_hda_audio
= {
833 .post_load
= hda_audio_post_load
,
834 .fields
= (VMStateField
[]) {
835 VMSTATE_STRUCT_ARRAY(st
, HDAAudioState
, 4, 0,
836 vmstate_hda_audio_stream
,
838 VMSTATE_BOOL_ARRAY(running_compat
, HDAAudioState
, 16),
839 VMSTATE_BOOL_ARRAY_V(running_real
, HDAAudioState
, 2 * 16, 2),
840 VMSTATE_END_OF_LIST()
844 static Property hda_audio_properties
[] = {
845 DEFINE_AUDIO_PROPERTIES(HDAAudioState
, card
),
846 DEFINE_PROP_UINT32("debug", HDAAudioState
, debug
, 0),
847 DEFINE_PROP_BOOL("mixer", HDAAudioState
, mixer
, true),
848 DEFINE_PROP_BOOL("use-timer", HDAAudioState
, use_timer
, true),
849 DEFINE_PROP_END_OF_LIST(),
852 static int hda_audio_init_output(HDACodecDevice
*hda
)
854 HDAAudioState
*a
= HDA_AUDIO(hda
);
857 return hda_audio_init(hda
, &output_nomixemu
);
859 return hda_audio_init(hda
, &output_mixemu
);
863 static int hda_audio_init_duplex(HDACodecDevice
*hda
)
865 HDAAudioState
*a
= HDA_AUDIO(hda
);
868 return hda_audio_init(hda
, &duplex_nomixemu
);
870 return hda_audio_init(hda
, &duplex_mixemu
);
874 static int hda_audio_init_micro(HDACodecDevice
*hda
)
876 HDAAudioState
*a
= HDA_AUDIO(hda
);
879 return hda_audio_init(hda
, µ_nomixemu
);
881 return hda_audio_init(hda
, µ_mixemu
);
885 static void hda_audio_base_class_init(ObjectClass
*klass
, void *data
)
887 DeviceClass
*dc
= DEVICE_CLASS(klass
);
888 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
890 k
->exit
= hda_audio_exit
;
891 k
->command
= hda_audio_command
;
892 k
->stream
= hda_audio_stream
;
893 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
894 dc
->reset
= hda_audio_reset
;
895 dc
->vmsd
= &vmstate_hda_audio
;
896 device_class_set_props(dc
, hda_audio_properties
);
899 static const TypeInfo hda_audio_info
= {
900 .name
= TYPE_HDA_AUDIO
,
901 .parent
= TYPE_HDA_CODEC_DEVICE
,
902 .instance_size
= sizeof(HDAAudioState
),
903 .class_init
= hda_audio_base_class_init
,
907 static void hda_audio_output_class_init(ObjectClass
*klass
, void *data
)
909 DeviceClass
*dc
= DEVICE_CLASS(klass
);
910 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
912 k
->init
= hda_audio_init_output
;
913 dc
->desc
= "HDA Audio Codec, output-only (line-out)";
916 static const TypeInfo hda_audio_output_info
= {
917 .name
= "hda-output",
918 .parent
= TYPE_HDA_AUDIO
,
919 .class_init
= hda_audio_output_class_init
,
922 static void hda_audio_duplex_class_init(ObjectClass
*klass
, void *data
)
924 DeviceClass
*dc
= DEVICE_CLASS(klass
);
925 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
927 k
->init
= hda_audio_init_duplex
;
928 dc
->desc
= "HDA Audio Codec, duplex (line-out, line-in)";
931 static const TypeInfo hda_audio_duplex_info
= {
932 .name
= "hda-duplex",
933 .parent
= TYPE_HDA_AUDIO
,
934 .class_init
= hda_audio_duplex_class_init
,
937 static void hda_audio_micro_class_init(ObjectClass
*klass
, void *data
)
939 DeviceClass
*dc
= DEVICE_CLASS(klass
);
940 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
942 k
->init
= hda_audio_init_micro
;
943 dc
->desc
= "HDA Audio Codec, duplex (speaker, microphone)";
946 static const TypeInfo hda_audio_micro_info
= {
948 .parent
= TYPE_HDA_AUDIO
,
949 .class_init
= hda_audio_micro_class_init
,
952 static void hda_audio_register_types(void)
954 type_register_static(&hda_audio_info
);
955 type_register_static(&hda_audio_output_info
);
956 type_register_static(&hda_audio_duplex_info
);
957 type_register_static(&hda_audio_micro_info
);
960 type_init(hda_audio_register_types
)