1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "hw/hw.h"
21#include "hw/pci/pci.h"
22#include "intel-hda.h"
23#include "intel-hda-defs.h"
24#include "audio/audio.h"
25
26
27
28typedef struct desc_param {
29 uint32_t id;
30 uint32_t val;
31} desc_param;
32
33typedef 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;
43
44typedef struct desc_codec {
45 const char *name;
46 uint32_t iid;
47 const desc_node *nodes;
48 uint32_t nnodes;
49} desc_codec;
50
51static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id)
52{
53 int i;
54
55 for (i = 0; i < node->nparams; i++) {
56 if (node->params[i].id == id) {
57 return &node->params[i];
58 }
59 }
60 return NULL;
61}
62
63static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid)
64{
65 int i;
66
67 for (i = 0; i < codec->nnodes; i++) {
68 if (codec->nodes[i].nid == nid) {
69 return &codec->nodes[i];
70 }
71 }
72 return NULL;
73}
74
75static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
76{
77 if (format & AC_FMT_TYPE_NON_PCM) {
78 return;
79 }
80
81 as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000;
82
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;
87 }
88
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;
97 }
98
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;
103 }
104
105 as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1;
106}
107
108
109
110
111
112
113
114
115#define QEMU_HDA_ID_VENDOR 0x1af4
116#define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
117 0x1fc )
118#define QEMU_HDA_AMP_NONE (0)
119#define QEMU_HDA_AMP_STEPS 0x4a
120
121#define PARAM mixemu
122#define HDA_MIXER
123#include "hda-codec-common.h"
124
125#define PARAM nomixemu
126#include "hda-codec-common.h"
127
128
129
130static const char *fmt2name[] = {
131 [ AUD_FMT_U8 ] = "PCM-U8",
132 [ AUD_FMT_S8 ] = "PCM-S8",
133 [ AUD_FMT_U16 ] = "PCM-U16",
134 [ AUD_FMT_S16 ] = "PCM-S16",
135 [ AUD_FMT_U32 ] = "PCM-U32",
136 [ AUD_FMT_S32 ] = "PCM-S32",
137};
138
139typedef struct HDAAudioState HDAAudioState;
140typedef struct HDAAudioStream HDAAudioStream;
141
142struct HDAAudioStream {
143 HDAAudioState *state;
144 const desc_node *node;
145 bool output, running;
146 uint32_t stream;
147 uint32_t channel;
148 uint32_t format;
149 uint32_t gain_left, gain_right;
150 bool mute_left, mute_right;
151 struct audsettings as;
152 union {
153 SWVoiceIn *in;
154 SWVoiceOut *out;
155 } voice;
156 uint8_t buf[HDA_BUFFER_SIZE];
157 uint32_t bpos;
158};
159
160struct HDAAudioState {
161 HDACodecDevice hda;
162 const char *name;
163
164 QEMUSoundCard card;
165 const desc_codec *desc;
166 HDAAudioStream st[4];
167 bool running_compat[16];
168 bool running_real[2 * 16];
169
170
171 uint32_t debug;
172 bool mixer;
173};
174
175static void hda_audio_input_cb(void *opaque, int avail)
176{
177 HDAAudioStream *st = opaque;
178 int recv = 0;
179 int len;
180 bool rc;
181
182 while (avail - recv >= sizeof(st->buf)) {
183 if (st->bpos != sizeof(st->buf)) {
184 len = AUD_read(st->voice.in, st->buf + st->bpos,
185 sizeof(st->buf) - st->bpos);
186 st->bpos += len;
187 recv += len;
188 if (st->bpos != sizeof(st->buf)) {
189 break;
190 }
191 }
192 rc = hda_codec_xfer(&st->state->hda, st->stream, false,
193 st->buf, sizeof(st->buf));
194 if (!rc) {
195 break;
196 }
197 st->bpos = 0;
198 }
199}
200
201static void hda_audio_output_cb(void *opaque, int avail)
202{
203 HDAAudioStream *st = opaque;
204 int sent = 0;
205 int len;
206 bool rc;
207
208 while (avail - sent >= sizeof(st->buf)) {
209 if (st->bpos == sizeof(st->buf)) {
210 rc = hda_codec_xfer(&st->state->hda, st->stream, true,
211 st->buf, sizeof(st->buf));
212 if (!rc) {
213 break;
214 }
215 st->bpos = 0;
216 }
217 len = AUD_write(st->voice.out, st->buf + st->bpos,
218 sizeof(st->buf) - st->bpos);
219 st->bpos += len;
220 sent += len;
221 if (st->bpos != sizeof(st->buf)) {
222 break;
223 }
224 }
225}
226
227static void hda_audio_set_running(HDAAudioStream *st, bool running)
228{
229 if (st->node == NULL) {
230 return;
231 }
232 if (st->running == running) {
233 return;
234 }
235 st->running = running;
236 dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
237 st->running ? "on" : "off", st->stream);
238 if (st->output) {
239 AUD_set_active_out(st->voice.out, st->running);
240 } else {
241 AUD_set_active_in(st->voice.in, st->running);
242 }
243}
244
245static void hda_audio_set_amp(HDAAudioStream *st)
246{
247 bool muted;
248 uint32_t left, right;
249
250 if (st->node == NULL) {
251 return;
252 }
253
254 muted = st->mute_left && st->mute_right;
255 left = st->mute_left ? 0 : st->gain_left;
256 right = st->mute_right ? 0 : st->gain_right;
257
258 left = left * 255 / QEMU_HDA_AMP_STEPS;
259 right = right * 255 / QEMU_HDA_AMP_STEPS;
260
261 if (st->output) {
262 AUD_set_volume_out(st->voice.out, muted, left, right);
263 } else {
264 AUD_set_volume_in(st->voice.in, muted, left, right);
265 }
266}
267
268static void hda_audio_setup(HDAAudioStream *st)
269{
270 if (st->node == NULL) {
271 return;
272 }
273
274 dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
275 st->node->name, st->as.nchannels,
276 fmt2name[st->as.fmt], st->as.freq);
277
278 if (st->output) {
279 st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
280 st->node->name, st,
281 hda_audio_output_cb, &st->as);
282 } else {
283 st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
284 st->node->name, st,
285 hda_audio_input_cb, &st->as);
286 }
287}
288
289static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
290{
291 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
292 HDAAudioStream *st;
293 const desc_node *node = NULL;
294 const desc_param *param;
295 uint32_t verb, payload, response, count, shift;
296
297 if ((data & 0x70000) == 0x70000) {
298
299 verb = (data >> 8) & 0xfff;
300 payload = data & 0x00ff;
301 } else {
302
303 verb = (data >> 8) & 0xf00;
304 payload = data & 0xffff;
305 }
306
307 node = hda_codec_find_node(a->desc, nid);
308 if (node == NULL) {
309 goto fail;
310 }
311 dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
312 __FUNCTION__, nid, node->name, verb, payload);
313
314 switch (verb) {
315
316 case AC_VERB_PARAMETERS:
317 param = hda_codec_find_param(node, payload);
318 if (param == NULL) {
319 goto fail;
320 }
321 hda_codec_response(hda, true, param->val);
322 break;
323 case AC_VERB_GET_SUBSYSTEM_ID:
324 hda_codec_response(hda, true, a->desc->iid);
325 break;
326
327
328 case AC_VERB_GET_CONNECT_LIST:
329 param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN);
330 count = param ? param->val : 0;
331 response = 0;
332 shift = 0;
333 while (payload < count && shift < 32) {
334 response |= node->conn[payload] << shift;
335 payload++;
336 shift += 8;
337 }
338 hda_codec_response(hda, true, response);
339 break;
340
341
342 case AC_VERB_GET_CONFIG_DEFAULT:
343 hda_codec_response(hda, true, node->config);
344 break;
345 case AC_VERB_GET_PIN_WIDGET_CONTROL:
346 hda_codec_response(hda, true, node->pinctl);
347 break;
348 case AC_VERB_SET_PIN_WIDGET_CONTROL:
349 if (node->pinctl != payload) {
350 dprint(a, 1, "unhandled pin control bit\n");
351 }
352 hda_codec_response(hda, true, 0);
353 break;
354
355
356 case AC_VERB_SET_CHANNEL_STREAMID:
357 st = a->st + node->stindex;
358 if (st->node == NULL) {
359 goto fail;
360 }
361 hda_audio_set_running(st, false);
362 st->stream = (payload >> 4) & 0x0f;
363 st->channel = payload & 0x0f;
364 dprint(a, 2, "%s: stream %d, channel %d\n",
365 st->node->name, st->stream, st->channel);
366 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
367 hda_codec_response(hda, true, 0);
368 break;
369 case AC_VERB_GET_CONV:
370 st = a->st + node->stindex;
371 if (st->node == NULL) {
372 goto fail;
373 }
374 response = st->stream << 4 | st->channel;
375 hda_codec_response(hda, true, response);
376 break;
377 case AC_VERB_SET_STREAM_FORMAT:
378 st = a->st + node->stindex;
379 if (st->node == NULL) {
380 goto fail;
381 }
382 st->format = payload;
383 hda_codec_parse_fmt(st->format, &st->as);
384 hda_audio_setup(st);
385 hda_codec_response(hda, true, 0);
386 break;
387 case AC_VERB_GET_STREAM_FORMAT:
388 st = a->st + node->stindex;
389 if (st->node == NULL) {
390 goto fail;
391 }
392 hda_codec_response(hda, true, st->format);
393 break;
394 case AC_VERB_GET_AMP_GAIN_MUTE:
395 st = a->st + node->stindex;
396 if (st->node == NULL) {
397 goto fail;
398 }
399 if (payload & AC_AMP_GET_LEFT) {
400 response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0);
401 } else {
402 response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0);
403 }
404 hda_codec_response(hda, true, response);
405 break;
406 case AC_VERB_SET_AMP_GAIN_MUTE:
407 st = a->st + node->stindex;
408 if (st->node == NULL) {
409 goto fail;
410 }
411 dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
412 st->node->name,
413 (payload & AC_AMP_SET_OUTPUT) ? "o" : "-",
414 (payload & AC_AMP_SET_INPUT) ? "i" : "-",
415 (payload & AC_AMP_SET_LEFT) ? "l" : "-",
416 (payload & AC_AMP_SET_RIGHT) ? "r" : "-",
417 (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT,
418 (payload & AC_AMP_GAIN),
419 (payload & AC_AMP_MUTE) ? "muted" : "");
420 if (payload & AC_AMP_SET_LEFT) {
421 st->gain_left = payload & AC_AMP_GAIN;
422 st->mute_left = payload & AC_AMP_MUTE;
423 }
424 if (payload & AC_AMP_SET_RIGHT) {
425 st->gain_right = payload & AC_AMP_GAIN;
426 st->mute_right = payload & AC_AMP_MUTE;
427 }
428 hda_audio_set_amp(st);
429 hda_codec_response(hda, true, 0);
430 break;
431
432
433 case AC_VERB_SET_POWER_STATE:
434 case AC_VERB_GET_POWER_STATE:
435 case AC_VERB_GET_SDI_SELECT:
436 hda_codec_response(hda, true, 0);
437 break;
438 default:
439 goto fail;
440 }
441 return;
442
443fail:
444 dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
445 __FUNCTION__, nid, node ? node->name : "?", verb, payload);
446 hda_codec_response(hda, true, 0);
447}
448
449static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
450{
451 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
452 int s;
453
454 a->running_compat[stnr] = running;
455 a->running_real[output * 16 + stnr] = running;
456 for (s = 0; s < ARRAY_SIZE(a->st); s++) {
457 if (a->st[s].node == NULL) {
458 continue;
459 }
460 if (a->st[s].output != output) {
461 continue;
462 }
463 if (a->st[s].stream != stnr) {
464 continue;
465 }
466 hda_audio_set_running(&a->st[s], running);
467 }
468}
469
470static int hda_audio_init(HDACodecDevice *hda, const struct desc_codec *desc)
471{
472 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
473 HDAAudioStream *st;
474 const desc_node *node;
475 const desc_param *param;
476 uint32_t i, type;
477
478 a->desc = desc;
479 a->name = object_get_typename(OBJECT(a));
480 dprint(a, 1, "%s: cad %d\n", __FUNCTION__, a->hda.cad);
481
482 AUD_register_card("hda", &a->card);
483 for (i = 0; i < a->desc->nnodes; i++) {
484 node = a->desc->nodes + i;
485 param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP);
486 if (NULL == param)
487 continue;
488 type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
489 switch (type) {
490 case AC_WID_AUD_OUT:
491 case AC_WID_AUD_IN:
492 assert(node->stindex < ARRAY_SIZE(a->st));
493 st = a->st + node->stindex;
494 st->state = a;
495 st->node = node;
496 if (type == AC_WID_AUD_OUT) {
497
498 st->gain_left = QEMU_HDA_AMP_STEPS;
499 st->gain_right = QEMU_HDA_AMP_STEPS;
500 st->bpos = sizeof(st->buf);
501 st->output = true;
502 } else {
503 st->output = false;
504 }
505 st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 |
506 (1 << AC_FMT_CHAN_SHIFT);
507 hda_codec_parse_fmt(st->format, &st->as);
508 hda_audio_setup(st);
509 break;
510 }
511 }
512 return 0;
513}
514
515static int hda_audio_exit(HDACodecDevice *hda)
516{
517 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
518 HDAAudioStream *st;
519 int i;
520
521 dprint(a, 1, "%s\n", __FUNCTION__);
522 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
523 st = a->st + i;
524 if (st->node == NULL) {
525 continue;
526 }
527 if (st->output) {
528 AUD_close_out(&a->card, st->voice.out);
529 } else {
530 AUD_close_in(&a->card, st->voice.in);
531 }
532 }
533 AUD_remove_card(&a->card);
534 return 0;
535}
536
537static int hda_audio_post_load(void *opaque, int version)
538{
539 HDAAudioState *a = opaque;
540 HDAAudioStream *st;
541 int i;
542
543 dprint(a, 1, "%s\n", __FUNCTION__);
544 if (version == 1) {
545
546 for (i = 0; i < ARRAY_SIZE(a->running_compat); i++)
547 a->running_real[16 + i] = a->running_compat[i];
548 }
549
550 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
551 st = a->st + i;
552 if (st->node == NULL)
553 continue;
554 hda_codec_parse_fmt(st->format, &st->as);
555 hda_audio_setup(st);
556 hda_audio_set_amp(st);
557 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
558 }
559 return 0;
560}
561
562static const VMStateDescription vmstate_hda_audio_stream = {
563 .name = "hda-audio-stream",
564 .version_id = 1,
565 .fields = (VMStateField []) {
566 VMSTATE_UINT32(stream, HDAAudioStream),
567 VMSTATE_UINT32(channel, HDAAudioStream),
568 VMSTATE_UINT32(format, HDAAudioStream),
569 VMSTATE_UINT32(gain_left, HDAAudioStream),
570 VMSTATE_UINT32(gain_right, HDAAudioStream),
571 VMSTATE_BOOL(mute_left, HDAAudioStream),
572 VMSTATE_BOOL(mute_right, HDAAudioStream),
573 VMSTATE_UINT32(bpos, HDAAudioStream),
574 VMSTATE_BUFFER(buf, HDAAudioStream),
575 VMSTATE_END_OF_LIST()
576 }
577};
578
579static const VMStateDescription vmstate_hda_audio = {
580 .name = "hda-audio",
581 .version_id = 2,
582 .post_load = hda_audio_post_load,
583 .fields = (VMStateField []) {
584 VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0,
585 vmstate_hda_audio_stream,
586 HDAAudioStream),
587 VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
588 VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
589 VMSTATE_END_OF_LIST()
590 }
591};
592
593static Property hda_audio_properties[] = {
594 DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0),
595 DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true),
596 DEFINE_PROP_END_OF_LIST(),
597};
598
599static int hda_audio_init_output(HDACodecDevice *hda)
600{
601 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
602
603 if (!a->mixer) {
604 return hda_audio_init(hda, &output_nomixemu);
605 } else {
606 return hda_audio_init(hda, &output_mixemu);
607 }
608}
609
610static int hda_audio_init_duplex(HDACodecDevice *hda)
611{
612 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
613
614 if (!a->mixer) {
615 return hda_audio_init(hda, &duplex_nomixemu);
616 } else {
617 return hda_audio_init(hda, &duplex_mixemu);
618 }
619}
620
621static int hda_audio_init_micro(HDACodecDevice *hda)
622{
623 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
624
625 if (!a->mixer) {
626 return hda_audio_init(hda, µ_nomixemu);
627 } else {
628 return hda_audio_init(hda, µ_mixemu);
629 }
630}
631
632static void hda_audio_output_class_init(ObjectClass *klass, void *data)
633{
634 DeviceClass *dc = DEVICE_CLASS(klass);
635 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
636
637 k->init = hda_audio_init_output;
638 k->exit = hda_audio_exit;
639 k->command = hda_audio_command;
640 k->stream = hda_audio_stream;
641 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
642 dc->desc = "HDA Audio Codec, output-only (line-out)";
643 dc->vmsd = &vmstate_hda_audio;
644 dc->props = hda_audio_properties;
645}
646
647static const TypeInfo hda_audio_output_info = {
648 .name = "hda-output",
649 .parent = TYPE_HDA_CODEC_DEVICE,
650 .instance_size = sizeof(HDAAudioState),
651 .class_init = hda_audio_output_class_init,
652};
653
654static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
655{
656 DeviceClass *dc = DEVICE_CLASS(klass);
657 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
658
659 k->init = hda_audio_init_duplex;
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->desc = "HDA Audio Codec, duplex (line-out, line-in)";
665 dc->vmsd = &vmstate_hda_audio;
666 dc->props = hda_audio_properties;
667}
668
669static const TypeInfo hda_audio_duplex_info = {
670 .name = "hda-duplex",
671 .parent = TYPE_HDA_CODEC_DEVICE,
672 .instance_size = sizeof(HDAAudioState),
673 .class_init = hda_audio_duplex_class_init,
674};
675
676static void hda_audio_micro_class_init(ObjectClass *klass, void *data)
677{
678 DeviceClass *dc = DEVICE_CLASS(klass);
679 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
680
681 k->init = hda_audio_init_micro;
682 k->exit = hda_audio_exit;
683 k->command = hda_audio_command;
684 k->stream = hda_audio_stream;
685 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
686 dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
687 dc->vmsd = &vmstate_hda_audio;
688 dc->props = hda_audio_properties;
689}
690
691static const TypeInfo hda_audio_micro_info = {
692 .name = "hda-micro",
693 .parent = TYPE_HDA_CODEC_DEVICE,
694 .instance_size = sizeof(HDAAudioState),
695 .class_init = hda_audio_micro_class_init,
696};
697
698static void hda_audio_register_types(void)
699{
700 type_register_static(&hda_audio_output_info);
701 type_register_static(&hda_audio_duplex_info);
702 type_register_static(&hda_audio_micro_info);
703}
704
705type_init(hda_audio_register_types)
706