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"
22 #include "hw/pci/pci.h"
23 #include "intel-hda.h"
24 #include "intel-hda-defs.h"
25 #include "audio/audio.h"
27 /* -------------------------------------------------------------------------- */
29 typedef struct desc_param
{
34 typedef struct desc_node
{
37 const desc_param
*params
;
45 typedef struct desc_codec
{
48 const desc_node
*nodes
;
52 static const desc_param
* hda_codec_find_param(const desc_node
*node
, uint32_t id
)
56 for (i
= 0; i
< node
->nparams
; i
++) {
57 if (node
->params
[i
].id
== id
) {
58 return &node
->params
[i
];
64 static const desc_node
* hda_codec_find_node(const desc_codec
*codec
, uint32_t nid
)
68 for (i
= 0; i
< codec
->nnodes
; i
++) {
69 if (codec
->nodes
[i
].nid
== nid
) {
70 return &codec
->nodes
[i
];
76 static void hda_codec_parse_fmt(uint32_t format
, struct audsettings
*as
)
78 if (format
& AC_FMT_TYPE_NON_PCM
) {
82 as
->freq
= (format
& AC_FMT_BASE_44K
) ? 44100 : 48000;
84 switch ((format
& AC_FMT_MULT_MASK
) >> AC_FMT_MULT_SHIFT
) {
85 case 1: as
->freq
*= 2; break;
86 case 2: as
->freq
*= 3; break;
87 case 3: as
->freq
*= 4; break;
90 switch ((format
& AC_FMT_DIV_MASK
) >> AC_FMT_DIV_SHIFT
) {
91 case 1: as
->freq
/= 2; break;
92 case 2: as
->freq
/= 3; break;
93 case 3: as
->freq
/= 4; break;
94 case 4: as
->freq
/= 5; break;
95 case 5: as
->freq
/= 6; break;
96 case 6: as
->freq
/= 7; break;
97 case 7: as
->freq
/= 8; break;
100 switch (format
& AC_FMT_BITS_MASK
) {
101 case AC_FMT_BITS_8
: as
->fmt
= AUD_FMT_S8
; break;
102 case AC_FMT_BITS_16
: as
->fmt
= AUD_FMT_S16
; break;
103 case AC_FMT_BITS_32
: as
->fmt
= AUD_FMT_S32
; break;
106 as
->nchannels
= ((format
& AC_FMT_CHAN_MASK
) >> AC_FMT_CHAN_SHIFT
) + 1;
109 /* -------------------------------------------------------------------------- */
111 * HDA codec descriptions
116 #define QEMU_HDA_ID_VENDOR 0x1af4
117 #define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
118 0x1fc /* 16 -> 96 kHz */)
119 #define QEMU_HDA_AMP_NONE (0)
120 #define QEMU_HDA_AMP_STEPS 0x4a
124 #include "hda-codec-common.h"
126 #define PARAM nomixemu
127 #include "hda-codec-common.h"
129 /* -------------------------------------------------------------------------- */
131 static const char *fmt2name
[] = {
132 [ AUD_FMT_U8
] = "PCM-U8",
133 [ AUD_FMT_S8
] = "PCM-S8",
134 [ AUD_FMT_U16
] = "PCM-U16",
135 [ AUD_FMT_S16
] = "PCM-S16",
136 [ AUD_FMT_U32
] = "PCM-U32",
137 [ AUD_FMT_S32
] = "PCM-S32",
140 typedef struct HDAAudioState HDAAudioState
;
141 typedef struct HDAAudioStream HDAAudioStream
;
143 struct HDAAudioStream
{
144 HDAAudioState
*state
;
145 const desc_node
*node
;
146 bool output
, running
;
150 uint32_t gain_left
, gain_right
;
151 bool mute_left
, mute_right
;
152 struct audsettings as
;
157 uint8_t buf
[HDA_BUFFER_SIZE
];
161 #define TYPE_HDA_AUDIO "hda-audio"
162 #define HDA_AUDIO(obj) OBJECT_CHECK(HDAAudioState, (obj), TYPE_HDA_AUDIO)
164 struct HDAAudioState
{
169 const desc_codec
*desc
;
170 HDAAudioStream st
[4];
171 bool running_compat
[16];
172 bool running_real
[2 * 16];
179 static void hda_audio_input_cb(void *opaque
, int avail
)
181 HDAAudioStream
*st
= opaque
;
186 while (avail
- recv
>= sizeof(st
->buf
)) {
187 if (st
->bpos
!= sizeof(st
->buf
)) {
188 len
= AUD_read(st
->voice
.in
, st
->buf
+ st
->bpos
,
189 sizeof(st
->buf
) - st
->bpos
);
192 if (st
->bpos
!= sizeof(st
->buf
)) {
196 rc
= hda_codec_xfer(&st
->state
->hda
, st
->stream
, false,
197 st
->buf
, sizeof(st
->buf
));
205 static void hda_audio_output_cb(void *opaque
, int avail
)
207 HDAAudioStream
*st
= opaque
;
212 while (avail
- sent
>= sizeof(st
->buf
)) {
213 if (st
->bpos
== sizeof(st
->buf
)) {
214 rc
= hda_codec_xfer(&st
->state
->hda
, st
->stream
, true,
215 st
->buf
, sizeof(st
->buf
));
221 len
= AUD_write(st
->voice
.out
, st
->buf
+ st
->bpos
,
222 sizeof(st
->buf
) - st
->bpos
);
225 if (st
->bpos
!= sizeof(st
->buf
)) {
231 static void hda_audio_set_running(HDAAudioStream
*st
, bool running
)
233 if (st
->node
== NULL
) {
236 if (st
->running
== running
) {
239 st
->running
= running
;
240 dprint(st
->state
, 1, "%s: %s (stream %d)\n", st
->node
->name
,
241 st
->running
? "on" : "off", st
->stream
);
243 AUD_set_active_out(st
->voice
.out
, st
->running
);
245 AUD_set_active_in(st
->voice
.in
, st
->running
);
249 static void hda_audio_set_amp(HDAAudioStream
*st
)
252 uint32_t left
, right
;
254 if (st
->node
== NULL
) {
258 muted
= st
->mute_left
&& st
->mute_right
;
259 left
= st
->mute_left
? 0 : st
->gain_left
;
260 right
= st
->mute_right
? 0 : st
->gain_right
;
262 left
= left
* 255 / QEMU_HDA_AMP_STEPS
;
263 right
= right
* 255 / QEMU_HDA_AMP_STEPS
;
265 if (!st
->state
->mixer
) {
269 AUD_set_volume_out(st
->voice
.out
, muted
, left
, right
);
271 AUD_set_volume_in(st
->voice
.in
, muted
, left
, right
);
275 static void hda_audio_setup(HDAAudioStream
*st
)
277 if (st
->node
== NULL
) {
281 dprint(st
->state
, 1, "%s: format: %d x %s @ %d Hz\n",
282 st
->node
->name
, st
->as
.nchannels
,
283 fmt2name
[st
->as
.fmt
], st
->as
.freq
);
286 st
->voice
.out
= AUD_open_out(&st
->state
->card
, st
->voice
.out
,
288 hda_audio_output_cb
, &st
->as
);
290 st
->voice
.in
= AUD_open_in(&st
->state
->card
, st
->voice
.in
,
292 hda_audio_input_cb
, &st
->as
);
296 static void hda_audio_command(HDACodecDevice
*hda
, uint32_t nid
, uint32_t data
)
298 HDAAudioState
*a
= HDA_AUDIO(hda
);
300 const desc_node
*node
= NULL
;
301 const desc_param
*param
;
302 uint32_t verb
, payload
, response
, count
, shift
;
304 if ((data
& 0x70000) == 0x70000) {
305 /* 12/8 id/payload */
306 verb
= (data
>> 8) & 0xfff;
307 payload
= data
& 0x00ff;
309 /* 4/16 id/payload */
310 verb
= (data
>> 8) & 0xf00;
311 payload
= data
& 0xffff;
314 node
= hda_codec_find_node(a
->desc
, nid
);
318 dprint(a
, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
319 __FUNCTION__
, nid
, node
->name
, verb
, payload
);
323 case AC_VERB_PARAMETERS
:
324 param
= hda_codec_find_param(node
, payload
);
328 hda_codec_response(hda
, true, param
->val
);
330 case AC_VERB_GET_SUBSYSTEM_ID
:
331 hda_codec_response(hda
, true, a
->desc
->iid
);
335 case AC_VERB_GET_CONNECT_LIST
:
336 param
= hda_codec_find_param(node
, AC_PAR_CONNLIST_LEN
);
337 count
= param
? param
->val
: 0;
340 while (payload
< count
&& shift
< 32) {
341 response
|= node
->conn
[payload
] << shift
;
345 hda_codec_response(hda
, true, response
);
349 case AC_VERB_GET_CONFIG_DEFAULT
:
350 hda_codec_response(hda
, true, node
->config
);
352 case AC_VERB_GET_PIN_WIDGET_CONTROL
:
353 hda_codec_response(hda
, true, node
->pinctl
);
355 case AC_VERB_SET_PIN_WIDGET_CONTROL
:
356 if (node
->pinctl
!= payload
) {
357 dprint(a
, 1, "unhandled pin control bit\n");
359 hda_codec_response(hda
, true, 0);
362 /* audio in/out widget */
363 case AC_VERB_SET_CHANNEL_STREAMID
:
364 st
= a
->st
+ node
->stindex
;
365 if (st
->node
== NULL
) {
368 hda_audio_set_running(st
, false);
369 st
->stream
= (payload
>> 4) & 0x0f;
370 st
->channel
= payload
& 0x0f;
371 dprint(a
, 2, "%s: stream %d, channel %d\n",
372 st
->node
->name
, st
->stream
, st
->channel
);
373 hda_audio_set_running(st
, a
->running_real
[st
->output
* 16 + st
->stream
]);
374 hda_codec_response(hda
, true, 0);
376 case AC_VERB_GET_CONV
:
377 st
= a
->st
+ node
->stindex
;
378 if (st
->node
== NULL
) {
381 response
= st
->stream
<< 4 | st
->channel
;
382 hda_codec_response(hda
, true, response
);
384 case AC_VERB_SET_STREAM_FORMAT
:
385 st
= a
->st
+ node
->stindex
;
386 if (st
->node
== NULL
) {
389 st
->format
= payload
;
390 hda_codec_parse_fmt(st
->format
, &st
->as
);
392 hda_codec_response(hda
, true, 0);
394 case AC_VERB_GET_STREAM_FORMAT
:
395 st
= a
->st
+ node
->stindex
;
396 if (st
->node
== NULL
) {
399 hda_codec_response(hda
, true, st
->format
);
401 case AC_VERB_GET_AMP_GAIN_MUTE
:
402 st
= a
->st
+ node
->stindex
;
403 if (st
->node
== NULL
) {
406 if (payload
& AC_AMP_GET_LEFT
) {
407 response
= st
->gain_left
| (st
->mute_left
? AC_AMP_MUTE
: 0);
409 response
= st
->gain_right
| (st
->mute_right
? AC_AMP_MUTE
: 0);
411 hda_codec_response(hda
, true, response
);
413 case AC_VERB_SET_AMP_GAIN_MUTE
:
414 st
= a
->st
+ node
->stindex
;
415 if (st
->node
== NULL
) {
418 dprint(a
, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
420 (payload
& AC_AMP_SET_OUTPUT
) ? "o" : "-",
421 (payload
& AC_AMP_SET_INPUT
) ? "i" : "-",
422 (payload
& AC_AMP_SET_LEFT
) ? "l" : "-",
423 (payload
& AC_AMP_SET_RIGHT
) ? "r" : "-",
424 (payload
& AC_AMP_SET_INDEX
) >> AC_AMP_SET_INDEX_SHIFT
,
425 (payload
& AC_AMP_GAIN
),
426 (payload
& AC_AMP_MUTE
) ? "muted" : "");
427 if (payload
& AC_AMP_SET_LEFT
) {
428 st
->gain_left
= payload
& AC_AMP_GAIN
;
429 st
->mute_left
= payload
& AC_AMP_MUTE
;
431 if (payload
& AC_AMP_SET_RIGHT
) {
432 st
->gain_right
= payload
& AC_AMP_GAIN
;
433 st
->mute_right
= payload
& AC_AMP_MUTE
;
435 hda_audio_set_amp(st
);
436 hda_codec_response(hda
, true, 0);
440 case AC_VERB_SET_POWER_STATE
:
441 case AC_VERB_GET_POWER_STATE
:
442 case AC_VERB_GET_SDI_SELECT
:
443 hda_codec_response(hda
, true, 0);
451 dprint(a
, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
452 __FUNCTION__
, nid
, node
? node
->name
: "?", verb
, payload
);
453 hda_codec_response(hda
, true, 0);
456 static void hda_audio_stream(HDACodecDevice
*hda
, uint32_t stnr
, bool running
, bool output
)
458 HDAAudioState
*a
= HDA_AUDIO(hda
);
461 a
->running_compat
[stnr
] = running
;
462 a
->running_real
[output
* 16 + stnr
] = running
;
463 for (s
= 0; s
< ARRAY_SIZE(a
->st
); s
++) {
464 if (a
->st
[s
].node
== NULL
) {
467 if (a
->st
[s
].output
!= output
) {
470 if (a
->st
[s
].stream
!= stnr
) {
473 hda_audio_set_running(&a
->st
[s
], running
);
477 static int hda_audio_init(HDACodecDevice
*hda
, const struct desc_codec
*desc
)
479 HDAAudioState
*a
= HDA_AUDIO(hda
);
481 const desc_node
*node
;
482 const desc_param
*param
;
486 a
->name
= object_get_typename(OBJECT(a
));
487 dprint(a
, 1, "%s: cad %d\n", __FUNCTION__
, a
->hda
.cad
);
489 AUD_register_card("hda", &a
->card
);
490 for (i
= 0; i
< a
->desc
->nnodes
; i
++) {
491 node
= a
->desc
->nodes
+ i
;
492 param
= hda_codec_find_param(node
, AC_PAR_AUDIO_WIDGET_CAP
);
496 type
= (param
->val
& AC_WCAP_TYPE
) >> AC_WCAP_TYPE_SHIFT
;
500 assert(node
->stindex
< ARRAY_SIZE(a
->st
));
501 st
= a
->st
+ node
->stindex
;
504 if (type
== AC_WID_AUD_OUT
) {
505 /* unmute output by default */
506 st
->gain_left
= QEMU_HDA_AMP_STEPS
;
507 st
->gain_right
= QEMU_HDA_AMP_STEPS
;
508 st
->bpos
= sizeof(st
->buf
);
513 st
->format
= AC_FMT_TYPE_PCM
| AC_FMT_BITS_16
|
514 (1 << AC_FMT_CHAN_SHIFT
);
515 hda_codec_parse_fmt(st
->format
, &st
->as
);
523 static int hda_audio_exit(HDACodecDevice
*hda
)
525 HDAAudioState
*a
= HDA_AUDIO(hda
);
529 dprint(a
, 1, "%s\n", __FUNCTION__
);
530 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
532 if (st
->node
== NULL
) {
536 AUD_close_out(&a
->card
, st
->voice
.out
);
538 AUD_close_in(&a
->card
, st
->voice
.in
);
541 AUD_remove_card(&a
->card
);
545 static int hda_audio_post_load(void *opaque
, int version
)
547 HDAAudioState
*a
= opaque
;
551 dprint(a
, 1, "%s\n", __FUNCTION__
);
553 /* assume running_compat[] is for output streams */
554 for (i
= 0; i
< ARRAY_SIZE(a
->running_compat
); i
++)
555 a
->running_real
[16 + i
] = a
->running_compat
[i
];
558 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
560 if (st
->node
== NULL
)
562 hda_codec_parse_fmt(st
->format
, &st
->as
);
564 hda_audio_set_amp(st
);
565 hda_audio_set_running(st
, a
->running_real
[st
->output
* 16 + st
->stream
]);
570 static void hda_audio_reset(DeviceState
*dev
)
572 HDAAudioState
*a
= HDA_AUDIO(dev
);
576 dprint(a
, 1, "%s\n", __func__
);
577 for (i
= 0; i
< ARRAY_SIZE(a
->st
); i
++) {
579 if (st
->node
!= NULL
) {
580 hda_audio_set_running(st
, false);
585 static const VMStateDescription vmstate_hda_audio_stream
= {
586 .name
= "hda-audio-stream",
588 .fields
= (VMStateField
[]) {
589 VMSTATE_UINT32(stream
, HDAAudioStream
),
590 VMSTATE_UINT32(channel
, HDAAudioStream
),
591 VMSTATE_UINT32(format
, HDAAudioStream
),
592 VMSTATE_UINT32(gain_left
, HDAAudioStream
),
593 VMSTATE_UINT32(gain_right
, HDAAudioStream
),
594 VMSTATE_BOOL(mute_left
, HDAAudioStream
),
595 VMSTATE_BOOL(mute_right
, HDAAudioStream
),
596 VMSTATE_UINT32(bpos
, HDAAudioStream
),
597 VMSTATE_BUFFER(buf
, HDAAudioStream
),
598 VMSTATE_END_OF_LIST()
602 static const VMStateDescription vmstate_hda_audio
= {
605 .post_load
= hda_audio_post_load
,
606 .fields
= (VMStateField
[]) {
607 VMSTATE_STRUCT_ARRAY(st
, HDAAudioState
, 4, 0,
608 vmstate_hda_audio_stream
,
610 VMSTATE_BOOL_ARRAY(running_compat
, HDAAudioState
, 16),
611 VMSTATE_BOOL_ARRAY_V(running_real
, HDAAudioState
, 2 * 16, 2),
612 VMSTATE_END_OF_LIST()
616 static Property hda_audio_properties
[] = {
617 DEFINE_PROP_UINT32("debug", HDAAudioState
, debug
, 0),
618 DEFINE_PROP_BOOL("mixer", HDAAudioState
, mixer
, true),
619 DEFINE_PROP_END_OF_LIST(),
622 static int hda_audio_init_output(HDACodecDevice
*hda
)
624 HDAAudioState
*a
= HDA_AUDIO(hda
);
627 return hda_audio_init(hda
, &output_nomixemu
);
629 return hda_audio_init(hda
, &output_mixemu
);
633 static int hda_audio_init_duplex(HDACodecDevice
*hda
)
635 HDAAudioState
*a
= HDA_AUDIO(hda
);
638 return hda_audio_init(hda
, &duplex_nomixemu
);
640 return hda_audio_init(hda
, &duplex_mixemu
);
644 static int hda_audio_init_micro(HDACodecDevice
*hda
)
646 HDAAudioState
*a
= HDA_AUDIO(hda
);
649 return hda_audio_init(hda
, µ_nomixemu
);
651 return hda_audio_init(hda
, µ_mixemu
);
655 static void hda_audio_base_class_init(ObjectClass
*klass
, void *data
)
657 DeviceClass
*dc
= DEVICE_CLASS(klass
);
658 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
660 k
->exit
= hda_audio_exit
;
661 k
->command
= hda_audio_command
;
662 k
->stream
= hda_audio_stream
;
663 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
664 dc
->reset
= hda_audio_reset
;
665 dc
->vmsd
= &vmstate_hda_audio
;
666 dc
->props
= hda_audio_properties
;
669 static const TypeInfo hda_audio_info
= {
670 .name
= TYPE_HDA_AUDIO
,
671 .parent
= TYPE_HDA_CODEC_DEVICE
,
672 .class_init
= hda_audio_base_class_init
,
676 static void hda_audio_output_class_init(ObjectClass
*klass
, void *data
)
678 DeviceClass
*dc
= DEVICE_CLASS(klass
);
679 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
681 k
->init
= hda_audio_init_output
;
682 dc
->desc
= "HDA Audio Codec, output-only (line-out)";
685 static const TypeInfo hda_audio_output_info
= {
686 .name
= "hda-output",
687 .parent
= TYPE_HDA_AUDIO
,
688 .instance_size
= sizeof(HDAAudioState
),
689 .class_init
= hda_audio_output_class_init
,
692 static void hda_audio_duplex_class_init(ObjectClass
*klass
, void *data
)
694 DeviceClass
*dc
= DEVICE_CLASS(klass
);
695 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
697 k
->init
= hda_audio_init_duplex
;
698 dc
->desc
= "HDA Audio Codec, duplex (line-out, line-in)";
701 static const TypeInfo hda_audio_duplex_info
= {
702 .name
= "hda-duplex",
703 .parent
= TYPE_HDA_AUDIO
,
704 .instance_size
= sizeof(HDAAudioState
),
705 .class_init
= hda_audio_duplex_class_init
,
708 static void hda_audio_micro_class_init(ObjectClass
*klass
, void *data
)
710 DeviceClass
*dc
= DEVICE_CLASS(klass
);
711 HDACodecDeviceClass
*k
= HDA_CODEC_DEVICE_CLASS(klass
);
713 k
->init
= hda_audio_init_micro
;
714 dc
->desc
= "HDA Audio Codec, duplex (speaker, microphone)";
717 static const TypeInfo hda_audio_micro_info
= {
719 .parent
= TYPE_HDA_AUDIO
,
720 .instance_size
= sizeof(HDAAudioState
),
721 .class_init
= hda_audio_micro_class_init
,
724 static void hda_audio_register_types(void)
726 type_register_static(&hda_audio_info
);
727 type_register_static(&hda_audio_output_info
);
728 type_register_static(&hda_audio_duplex_info
);
729 type_register_static(&hda_audio_micro_info
);
732 type_init(hda_audio_register_types
)