1 /* 2 * Copyright (C) 2010 Red Hat, Inc. 3 * 4 * written by Gerd Hoffmann <kraxel@redhat.com> 5 * 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. 10 * 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. 15 * 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/>. 18 */ 19 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" 28 #include "trace.h" 29 #include "qom/object.h" 30 31 /* -------------------------------------------------------------------------- */ 32 33 typedef struct desc_param { 34 uint32_t id; 35 uint32_t val; 36 } desc_param; 37 38 typedef struct desc_node { 39 uint32_t nid; 40 const char *name; 41 const desc_param *params; 42 uint32_t nparams; 43 uint32_t config; 44 uint32_t pinctl; 45 uint32_t *conn; 46 uint32_t stindex; 47 } desc_node; 48 49 typedef struct desc_codec { 50 const char *name; 51 uint32_t iid; 52 const desc_node *nodes; 53 uint32_t nnodes; 54 } desc_codec; 55 56 static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id) 57 { 58 int i; 59 60 for (i = 0; i < node->nparams; i++) { 61 if (node->params[i].id == id) { 62 return &node->params[i]; 63 } 64 } 65 return NULL; 66 } 67 68 static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid) 69 { 70 int i; 71 72 for (i = 0; i < codec->nnodes; i++) { 73 if (codec->nodes[i].nid == nid) { 74 return &codec->nodes[i]; 75 } 76 } 77 return NULL; 78 } 79 80 static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as) 81 { 82 if (format & AC_FMT_TYPE_NON_PCM) { 83 return; 84 } 85 86 as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000; 87 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; 92 } 93 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; 102 } 103 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; 108 } 109 110 as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1; 111 } 112 113 /* -------------------------------------------------------------------------- */ 114 /* 115 * HDA codec descriptions 116 */ 117 118 /* some defines */ 119 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 125 126 #define PARAM mixemu 127 #define HDA_MIXER 128 #include "hda-codec-common.h" 129 130 #define PARAM nomixemu 131 #include "hda-codec-common.h" 132 133 #define HDA_TIMER_TICKS (SCALE_MS) 134 #define B_SIZE sizeof(st->buf) 135 #define B_MASK (sizeof(st->buf) - 1) 136 137 /* -------------------------------------------------------------------------- */ 138 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", 146 }; 147 148 typedef struct HDAAudioState HDAAudioState; 149 typedef struct HDAAudioStream HDAAudioStream; 150 151 struct HDAAudioStream { 152 HDAAudioState *state; 153 const desc_node *node; 154 bool output, running; 155 uint32_t stream; 156 uint32_t channel; 157 uint32_t format; 158 uint32_t gain_left, gain_right; 159 bool mute_left, mute_right; 160 struct audsettings as; 161 union { 162 SWVoiceIn *in; 163 SWVoiceOut *out; 164 } voice; 165 uint8_t compat_buf[HDA_BUFFER_SIZE]; 166 uint32_t compat_bpos; 167 uint8_t buf[8192]; /* size must be power of two */ 168 int64_t rpos; 169 int64_t wpos; 170 QEMUTimer *buft; 171 int64_t buft_start; 172 }; 173 174 #define TYPE_HDA_AUDIO "hda-audio" 175 DECLARE_INSTANCE_CHECKER(HDAAudioState, HDA_AUDIO, 176 TYPE_HDA_AUDIO) 177 178 struct HDAAudioState { 179 HDACodecDevice hda; 180 const char *name; 181 182 QEMUSoundCard card; 183 const desc_codec *desc; 184 HDAAudioStream st[4]; 185 bool running_compat[16]; 186 bool running_real[2 * 16]; 187 188 /* properties */ 189 uint32_t debug; 190 bool mixer; 191 bool use_timer; 192 }; 193 194 static inline int64_t hda_bytes_per_second(HDAAudioStream *st) 195 { 196 return 2LL * st->as.nchannels * st->as.freq; 197 } 198 199 static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t target_pos) 200 { 201 int64_t limit = B_SIZE / 8; 202 int64_t corr = 0; 203 204 if (target_pos > limit) { 205 corr = HDA_TIMER_TICKS; 206 } 207 if (target_pos < -limit) { 208 corr = -HDA_TIMER_TICKS; 209 } 210 if (target_pos < -(2 * limit)) { 211 corr = -(4 * HDA_TIMER_TICKS); 212 } 213 if (corr == 0) { 214 return; 215 } 216 217 trace_hda_audio_adjust(st->node->name, target_pos); 218 st->buft_start += corr; 219 } 220 221 static void hda_audio_input_timer(void *opaque) 222 { 223 HDAAudioStream *st = opaque; 224 225 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 226 227 int64_t buft_start = st->buft_start; 228 int64_t wpos = st->wpos; 229 int64_t rpos = st->rpos; 230 231 int64_t wanted_rpos = hda_bytes_per_second(st) * (now - buft_start) 232 / NANOSECONDS_PER_SECOND; 233 wanted_rpos &= -4; /* IMPORTANT! clip to frames */ 234 235 if (wanted_rpos <= rpos) { 236 /* we already transmitted the data */ 237 goto out_timer; 238 } 239 240 int64_t to_transfer = MIN(wpos - rpos, wanted_rpos - rpos); 241 while (to_transfer) { 242 uint32_t start = (rpos & B_MASK); 243 uint32_t chunk = MIN(B_SIZE - start, to_transfer); 244 int rc = hda_codec_xfer( 245 &st->state->hda, st->stream, false, st->buf + start, chunk); 246 if (!rc) { 247 break; 248 } 249 rpos += chunk; 250 to_transfer -= chunk; 251 st->rpos += chunk; 252 } 253 254 out_timer: 255 256 if (st->running) { 257 timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); 258 } 259 } 260 261 static void hda_audio_input_cb(void *opaque, int avail) 262 { 263 HDAAudioStream *st = opaque; 264 265 int64_t wpos = st->wpos; 266 int64_t rpos = st->rpos; 267 268 int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), avail); 269 270 while (to_transfer) { 271 uint32_t start = (uint32_t) (wpos & B_MASK); 272 uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer); 273 uint32_t read = AUD_read(st->voice.in, st->buf + start, chunk); 274 wpos += read; 275 to_transfer -= read; 276 st->wpos += read; 277 if (chunk != read) { 278 break; 279 } 280 } 281 282 hda_timer_sync_adjust(st, -((wpos - rpos) - (B_SIZE >> 1))); 283 } 284 285 static void hda_audio_output_timer(void *opaque) 286 { 287 HDAAudioStream *st = opaque; 288 289 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 290 291 int64_t buft_start = st->buft_start; 292 int64_t wpos = st->wpos; 293 int64_t rpos = st->rpos; 294 295 int64_t wanted_wpos = hda_bytes_per_second(st) * (now - buft_start) 296 / NANOSECONDS_PER_SECOND; 297 wanted_wpos &= -4; /* IMPORTANT! clip to frames */ 298 299 if (wanted_wpos <= wpos) { 300 /* we already received the data */ 301 goto out_timer; 302 } 303 304 int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), wanted_wpos - wpos); 305 while (to_transfer) { 306 uint32_t start = (wpos & B_MASK); 307 uint32_t chunk = MIN(B_SIZE - start, to_transfer); 308 int rc = hda_codec_xfer( 309 &st->state->hda, st->stream, true, st->buf + start, chunk); 310 if (!rc) { 311 break; 312 } 313 wpos += chunk; 314 to_transfer -= chunk; 315 st->wpos += chunk; 316 } 317 318 out_timer: 319 320 if (st->running) { 321 timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); 322 } 323 } 324 325 static void hda_audio_output_cb(void *opaque, int avail) 326 { 327 HDAAudioStream *st = opaque; 328 329 int64_t wpos = st->wpos; 330 int64_t rpos = st->rpos; 331 332 int64_t to_transfer = MIN(wpos - rpos, avail); 333 334 if (wpos - rpos == B_SIZE) { 335 /* drop buffer, reset timer adjust */ 336 st->rpos = 0; 337 st->wpos = 0; 338 st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 339 trace_hda_audio_overrun(st->node->name); 340 return; 341 } 342 343 while (to_transfer) { 344 uint32_t start = (uint32_t) (rpos & B_MASK); 345 uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer); 346 uint32_t written = AUD_write(st->voice.out, st->buf + start, chunk); 347 rpos += written; 348 to_transfer -= written; 349 st->rpos += written; 350 if (chunk != written) { 351 break; 352 } 353 } 354 355 hda_timer_sync_adjust(st, (wpos - rpos) - (B_SIZE >> 1)); 356 } 357 358 static void hda_audio_compat_input_cb(void *opaque, int avail) 359 { 360 HDAAudioStream *st = opaque; 361 int recv = 0; 362 int len; 363 bool rc; 364 365 while (avail - recv >= sizeof(st->compat_buf)) { 366 if (st->compat_bpos != sizeof(st->compat_buf)) { 367 len = AUD_read(st->voice.in, st->compat_buf + st->compat_bpos, 368 sizeof(st->compat_buf) - st->compat_bpos); 369 st->compat_bpos += len; 370 recv += len; 371 if (st->compat_bpos != sizeof(st->compat_buf)) { 372 break; 373 } 374 } 375 rc = hda_codec_xfer(&st->state->hda, st->stream, false, 376 st->compat_buf, sizeof(st->compat_buf)); 377 if (!rc) { 378 break; 379 } 380 st->compat_bpos = 0; 381 } 382 } 383 384 static void hda_audio_compat_output_cb(void *opaque, int avail) 385 { 386 HDAAudioStream *st = opaque; 387 int sent = 0; 388 int len; 389 bool rc; 390 391 while (avail - sent >= sizeof(st->compat_buf)) { 392 if (st->compat_bpos == sizeof(st->compat_buf)) { 393 rc = hda_codec_xfer(&st->state->hda, st->stream, true, 394 st->compat_buf, sizeof(st->compat_buf)); 395 if (!rc) { 396 break; 397 } 398 st->compat_bpos = 0; 399 } 400 len = AUD_write(st->voice.out, st->compat_buf + st->compat_bpos, 401 sizeof(st->compat_buf) - st->compat_bpos); 402 st->compat_bpos += len; 403 sent += len; 404 if (st->compat_bpos != sizeof(st->compat_buf)) { 405 break; 406 } 407 } 408 } 409 410 static void hda_audio_set_running(HDAAudioStream *st, bool running) 411 { 412 if (st->node == NULL) { 413 return; 414 } 415 if (st->running == running) { 416 return; 417 } 418 st->running = running; 419 trace_hda_audio_running(st->node->name, st->stream, st->running); 420 if (st->state->use_timer) { 421 if (running) { 422 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 423 st->rpos = 0; 424 st->wpos = 0; 425 st->buft_start = now; 426 timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); 427 } else { 428 timer_del(st->buft); 429 } 430 } 431 if (st->output) { 432 AUD_set_active_out(st->voice.out, st->running); 433 } else { 434 AUD_set_active_in(st->voice.in, st->running); 435 } 436 } 437 438 static void hda_audio_set_amp(HDAAudioStream *st) 439 { 440 bool muted; 441 uint32_t left, right; 442 443 if (st->node == NULL) { 444 return; 445 } 446 447 muted = st->mute_left && st->mute_right; 448 left = st->mute_left ? 0 : st->gain_left; 449 right = st->mute_right ? 0 : st->gain_right; 450 451 left = left * 255 / QEMU_HDA_AMP_STEPS; 452 right = right * 255 / QEMU_HDA_AMP_STEPS; 453 454 if (!st->state->mixer) { 455 return; 456 } 457 if (st->output) { 458 AUD_set_volume_out(st->voice.out, muted, left, right); 459 } else { 460 AUD_set_volume_in(st->voice.in, muted, left, right); 461 } 462 } 463 464 static void hda_audio_setup(HDAAudioStream *st) 465 { 466 bool use_timer = st->state->use_timer; 467 audio_callback_fn cb; 468 469 if (st->node == NULL) { 470 return; 471 } 472 473 trace_hda_audio_format(st->node->name, st->as.nchannels, 474 fmt2name[st->as.fmt], st->as.freq); 475 476 if (st->output) { 477 if (use_timer) { 478 cb = hda_audio_output_cb; 479 st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL, 480 hda_audio_output_timer, st); 481 } else { 482 cb = hda_audio_compat_output_cb; 483 } 484 st->voice.out = AUD_open_out(&st->state->card, st->voice.out, 485 st->node->name, st, cb, &st->as); 486 } else { 487 if (use_timer) { 488 cb = hda_audio_input_cb; 489 st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL, 490 hda_audio_input_timer, st); 491 } else { 492 cb = hda_audio_compat_input_cb; 493 } 494 st->voice.in = AUD_open_in(&st->state->card, st->voice.in, 495 st->node->name, st, cb, &st->as); 496 } 497 } 498 499 static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data) 500 { 501 HDAAudioState *a = HDA_AUDIO(hda); 502 HDAAudioStream *st; 503 const desc_node *node = NULL; 504 const desc_param *param; 505 uint32_t verb, payload, response, count, shift; 506 507 if ((data & 0x70000) == 0x70000) { 508 /* 12/8 id/payload */ 509 verb = (data >> 8) & 0xfff; 510 payload = data & 0x00ff; 511 } else { 512 /* 4/16 id/payload */ 513 verb = (data >> 8) & 0xf00; 514 payload = data & 0xffff; 515 } 516 517 node = hda_codec_find_node(a->desc, nid); 518 if (node == NULL) { 519 goto fail; 520 } 521 dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n", 522 __func__, nid, node->name, verb, payload); 523 524 switch (verb) { 525 /* all nodes */ 526 case AC_VERB_PARAMETERS: 527 param = hda_codec_find_param(node, payload); 528 if (param == NULL) { 529 goto fail; 530 } 531 hda_codec_response(hda, true, param->val); 532 break; 533 case AC_VERB_GET_SUBSYSTEM_ID: 534 hda_codec_response(hda, true, a->desc->iid); 535 break; 536 537 /* all functions */ 538 case AC_VERB_GET_CONNECT_LIST: 539 param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN); 540 count = param ? param->val : 0; 541 response = 0; 542 shift = 0; 543 while (payload < count && shift < 32) { 544 response |= node->conn[payload] << shift; 545 payload++; 546 shift += 8; 547 } 548 hda_codec_response(hda, true, response); 549 break; 550 551 /* pin widget */ 552 case AC_VERB_GET_CONFIG_DEFAULT: 553 hda_codec_response(hda, true, node->config); 554 break; 555 case AC_VERB_GET_PIN_WIDGET_CONTROL: 556 hda_codec_response(hda, true, node->pinctl); 557 break; 558 case AC_VERB_SET_PIN_WIDGET_CONTROL: 559 if (node->pinctl != payload) { 560 dprint(a, 1, "unhandled pin control bit\n"); 561 } 562 hda_codec_response(hda, true, 0); 563 break; 564 565 /* audio in/out widget */ 566 case AC_VERB_SET_CHANNEL_STREAMID: 567 st = a->st + node->stindex; 568 if (st->node == NULL) { 569 goto fail; 570 } 571 hda_audio_set_running(st, false); 572 st->stream = (payload >> 4) & 0x0f; 573 st->channel = payload & 0x0f; 574 dprint(a, 2, "%s: stream %d, channel %d\n", 575 st->node->name, st->stream, st->channel); 576 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]); 577 hda_codec_response(hda, true, 0); 578 break; 579 case AC_VERB_GET_CONV: 580 st = a->st + node->stindex; 581 if (st->node == NULL) { 582 goto fail; 583 } 584 response = st->stream << 4 | st->channel; 585 hda_codec_response(hda, true, response); 586 break; 587 case AC_VERB_SET_STREAM_FORMAT: 588 st = a->st + node->stindex; 589 if (st->node == NULL) { 590 goto fail; 591 } 592 st->format = payload; 593 hda_codec_parse_fmt(st->format, &st->as); 594 hda_audio_setup(st); 595 hda_codec_response(hda, true, 0); 596 break; 597 case AC_VERB_GET_STREAM_FORMAT: 598 st = a->st + node->stindex; 599 if (st->node == NULL) { 600 goto fail; 601 } 602 hda_codec_response(hda, true, st->format); 603 break; 604 case AC_VERB_GET_AMP_GAIN_MUTE: 605 st = a->st + node->stindex; 606 if (st->node == NULL) { 607 goto fail; 608 } 609 if (payload & AC_AMP_GET_LEFT) { 610 response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0); 611 } else { 612 response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0); 613 } 614 hda_codec_response(hda, true, response); 615 break; 616 case AC_VERB_SET_AMP_GAIN_MUTE: 617 st = a->st + node->stindex; 618 if (st->node == NULL) { 619 goto fail; 620 } 621 dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n", 622 st->node->name, 623 (payload & AC_AMP_SET_OUTPUT) ? "o" : "-", 624 (payload & AC_AMP_SET_INPUT) ? "i" : "-", 625 (payload & AC_AMP_SET_LEFT) ? "l" : "-", 626 (payload & AC_AMP_SET_RIGHT) ? "r" : "-", 627 (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT, 628 (payload & AC_AMP_GAIN), 629 (payload & AC_AMP_MUTE) ? "muted" : ""); 630 if (payload & AC_AMP_SET_LEFT) { 631 st->gain_left = payload & AC_AMP_GAIN; 632 st->mute_left = payload & AC_AMP_MUTE; 633 } 634 if (payload & AC_AMP_SET_RIGHT) { 635 st->gain_right = payload & AC_AMP_GAIN; 636 st->mute_right = payload & AC_AMP_MUTE; 637 } 638 hda_audio_set_amp(st); 639 hda_codec_response(hda, true, 0); 640 break; 641 642 /* not supported */ 643 case AC_VERB_SET_POWER_STATE: 644 case AC_VERB_GET_POWER_STATE: 645 case AC_VERB_GET_SDI_SELECT: 646 hda_codec_response(hda, true, 0); 647 break; 648 default: 649 goto fail; 650 } 651 return; 652 653 fail: 654 dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n", 655 __func__, nid, node ? node->name : "?", verb, payload); 656 hda_codec_response(hda, true, 0); 657 } 658 659 static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output) 660 { 661 HDAAudioState *a = HDA_AUDIO(hda); 662 int s; 663 664 a->running_compat[stnr] = running; 665 a->running_real[output * 16 + stnr] = running; 666 for (s = 0; s < ARRAY_SIZE(a->st); s++) { 667 if (a->st[s].node == NULL) { 668 continue; 669 } 670 if (a->st[s].output != output) { 671 continue; 672 } 673 if (a->st[s].stream != stnr) { 674 continue; 675 } 676 hda_audio_set_running(&a->st[s], running); 677 } 678 } 679 680 static int hda_audio_init(HDACodecDevice *hda, const struct desc_codec *desc) 681 { 682 HDAAudioState *a = HDA_AUDIO(hda); 683 HDAAudioStream *st; 684 const desc_node *node; 685 const desc_param *param; 686 uint32_t i, type; 687 688 a->desc = desc; 689 a->name = object_get_typename(OBJECT(a)); 690 dprint(a, 1, "%s: cad %d\n", __func__, a->hda.cad); 691 692 AUD_register_card("hda", &a->card); 693 for (i = 0; i < a->desc->nnodes; i++) { 694 node = a->desc->nodes + i; 695 param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP); 696 if (param == NULL) { 697 continue; 698 } 699 type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; 700 switch (type) { 701 case AC_WID_AUD_OUT: 702 case AC_WID_AUD_IN: 703 assert(node->stindex < ARRAY_SIZE(a->st)); 704 st = a->st + node->stindex; 705 st->state = a; 706 st->node = node; 707 if (type == AC_WID_AUD_OUT) { 708 /* unmute output by default */ 709 st->gain_left = QEMU_HDA_AMP_STEPS; 710 st->gain_right = QEMU_HDA_AMP_STEPS; 711 st->compat_bpos = sizeof(st->compat_buf); 712 st->output = true; 713 } else { 714 st->output = false; 715 } 716 st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 | 717 (1 << AC_FMT_CHAN_SHIFT); 718 hda_codec_parse_fmt(st->format, &st->as); 719 hda_audio_setup(st); 720 break; 721 } 722 } 723 return 0; 724 } 725 726 static void hda_audio_exit(HDACodecDevice *hda) 727 { 728 HDAAudioState *a = HDA_AUDIO(hda); 729 HDAAudioStream *st; 730 int i; 731 732 dprint(a, 1, "%s\n", __func__); 733 for (i = 0; i < ARRAY_SIZE(a->st); i++) { 734 st = a->st + i; 735 if (st->node == NULL) { 736 continue; 737 } 738 if (a->use_timer) { 739 timer_del(st->buft); 740 } 741 if (st->output) { 742 AUD_close_out(&a->card, st->voice.out); 743 } else { 744 AUD_close_in(&a->card, st->voice.in); 745 } 746 } 747 AUD_remove_card(&a->card); 748 } 749 750 static int hda_audio_post_load(void *opaque, int version) 751 { 752 HDAAudioState *a = opaque; 753 HDAAudioStream *st; 754 int i; 755 756 dprint(a, 1, "%s\n", __func__); 757 if (version == 1) { 758 /* assume running_compat[] is for output streams */ 759 for (i = 0; i < ARRAY_SIZE(a->running_compat); i++) 760 a->running_real[16 + i] = a->running_compat[i]; 761 } 762 763 for (i = 0; i < ARRAY_SIZE(a->st); i++) { 764 st = a->st + i; 765 if (st->node == NULL) 766 continue; 767 hda_codec_parse_fmt(st->format, &st->as); 768 hda_audio_setup(st); 769 hda_audio_set_amp(st); 770 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]); 771 } 772 return 0; 773 } 774 775 static void hda_audio_reset(DeviceState *dev) 776 { 777 HDAAudioState *a = HDA_AUDIO(dev); 778 HDAAudioStream *st; 779 int i; 780 781 dprint(a, 1, "%s\n", __func__); 782 for (i = 0; i < ARRAY_SIZE(a->st); i++) { 783 st = a->st + i; 784 if (st->node != NULL) { 785 hda_audio_set_running(st, false); 786 } 787 } 788 } 789 790 static bool vmstate_hda_audio_stream_buf_needed(void *opaque) 791 { 792 HDAAudioStream *st = opaque; 793 return st->state && st->state->use_timer; 794 } 795 796 static const VMStateDescription vmstate_hda_audio_stream_buf = { 797 .name = "hda-audio-stream/buffer", 798 .version_id = 1, 799 .needed = vmstate_hda_audio_stream_buf_needed, 800 .fields = (VMStateField[]) { 801 VMSTATE_BUFFER(buf, HDAAudioStream), 802 VMSTATE_INT64(rpos, HDAAudioStream), 803 VMSTATE_INT64(wpos, HDAAudioStream), 804 VMSTATE_TIMER_PTR(buft, HDAAudioStream), 805 VMSTATE_INT64(buft_start, HDAAudioStream), 806 VMSTATE_END_OF_LIST() 807 } 808 }; 809 810 static const VMStateDescription vmstate_hda_audio_stream = { 811 .name = "hda-audio-stream", 812 .version_id = 1, 813 .fields = (VMStateField[]) { 814 VMSTATE_UINT32(stream, HDAAudioStream), 815 VMSTATE_UINT32(channel, HDAAudioStream), 816 VMSTATE_UINT32(format, HDAAudioStream), 817 VMSTATE_UINT32(gain_left, HDAAudioStream), 818 VMSTATE_UINT32(gain_right, HDAAudioStream), 819 VMSTATE_BOOL(mute_left, HDAAudioStream), 820 VMSTATE_BOOL(mute_right, HDAAudioStream), 821 VMSTATE_UINT32(compat_bpos, HDAAudioStream), 822 VMSTATE_BUFFER(compat_buf, HDAAudioStream), 823 VMSTATE_END_OF_LIST() 824 }, 825 .subsections = (const VMStateDescription * []) { 826 &vmstate_hda_audio_stream_buf, 827 NULL 828 } 829 }; 830 831 static const VMStateDescription vmstate_hda_audio = { 832 .name = "hda-audio", 833 .version_id = 2, 834 .post_load = hda_audio_post_load, 835 .fields = (VMStateField[]) { 836 VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0, 837 vmstate_hda_audio_stream, 838 HDAAudioStream), 839 VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16), 840 VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2), 841 VMSTATE_END_OF_LIST() 842 } 843 }; 844 845 static Property hda_audio_properties[] = { 846 DEFINE_AUDIO_PROPERTIES(HDAAudioState, card), 847 DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0), 848 DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true), 849 DEFINE_PROP_BOOL("use-timer", HDAAudioState, use_timer, true), 850 DEFINE_PROP_END_OF_LIST(), 851 }; 852 853 static int hda_audio_init_output(HDACodecDevice *hda) 854 { 855 HDAAudioState *a = HDA_AUDIO(hda); 856 857 if (!a->mixer) { 858 return hda_audio_init(hda, &output_nomixemu); 859 } else { 860 return hda_audio_init(hda, &output_mixemu); 861 } 862 } 863 864 static int hda_audio_init_duplex(HDACodecDevice *hda) 865 { 866 HDAAudioState *a = HDA_AUDIO(hda); 867 868 if (!a->mixer) { 869 return hda_audio_init(hda, &duplex_nomixemu); 870 } else { 871 return hda_audio_init(hda, &duplex_mixemu); 872 } 873 } 874 875 static int hda_audio_init_micro(HDACodecDevice *hda) 876 { 877 HDAAudioState *a = HDA_AUDIO(hda); 878 879 if (!a->mixer) { 880 return hda_audio_init(hda, µ_nomixemu); 881 } else { 882 return hda_audio_init(hda, µ_mixemu); 883 } 884 } 885 886 static void hda_audio_base_class_init(ObjectClass *klass, void *data) 887 { 888 DeviceClass *dc = DEVICE_CLASS(klass); 889 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass); 890 891 k->exit = hda_audio_exit; 892 k->command = hda_audio_command; 893 k->stream = hda_audio_stream; 894 set_bit(DEVICE_CATEGORY_SOUND, dc->categories); 895 dc->reset = hda_audio_reset; 896 dc->vmsd = &vmstate_hda_audio; 897 device_class_set_props(dc, hda_audio_properties); 898 } 899 900 static const TypeInfo hda_audio_info = { 901 .name = TYPE_HDA_AUDIO, 902 .parent = TYPE_HDA_CODEC_DEVICE, 903 .instance_size = sizeof(HDAAudioState), 904 .class_init = hda_audio_base_class_init, 905 .abstract = true, 906 }; 907 908 static void hda_audio_output_class_init(ObjectClass *klass, void *data) 909 { 910 DeviceClass *dc = DEVICE_CLASS(klass); 911 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass); 912 913 k->init = hda_audio_init_output; 914 dc->desc = "HDA Audio Codec, output-only (line-out)"; 915 } 916 917 static const TypeInfo hda_audio_output_info = { 918 .name = "hda-output", 919 .parent = TYPE_HDA_AUDIO, 920 .class_init = hda_audio_output_class_init, 921 }; 922 923 static void hda_audio_duplex_class_init(ObjectClass *klass, void *data) 924 { 925 DeviceClass *dc = DEVICE_CLASS(klass); 926 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass); 927 928 k->init = hda_audio_init_duplex; 929 dc->desc = "HDA Audio Codec, duplex (line-out, line-in)"; 930 } 931 932 static const TypeInfo hda_audio_duplex_info = { 933 .name = "hda-duplex", 934 .parent = TYPE_HDA_AUDIO, 935 .class_init = hda_audio_duplex_class_init, 936 }; 937 938 static void hda_audio_micro_class_init(ObjectClass *klass, void *data) 939 { 940 DeviceClass *dc = DEVICE_CLASS(klass); 941 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass); 942 943 k->init = hda_audio_init_micro; 944 dc->desc = "HDA Audio Codec, duplex (speaker, microphone)"; 945 } 946 947 static const TypeInfo hda_audio_micro_info = { 948 .name = "hda-micro", 949 .parent = TYPE_HDA_AUDIO, 950 .class_init = hda_audio_micro_class_init, 951 }; 952 953 static void hda_audio_register_types(void) 954 { 955 type_register_static(&hda_audio_info); 956 type_register_static(&hda_audio_output_info); 957 type_register_static(&hda_audio_duplex_info); 958 type_register_static(&hda_audio_micro_info); 959 } 960 961 type_init(hda_audio_register_types) 962