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