]> git.proxmox.com Git - mirror_qemu.git/blame - hw/audio/hda-codec.c
Use DECLARE_*CHECKER* macros
[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 21#include "hw/pci/pci.h"
a27bd6c7 22#include "hw/qdev-properties.h"
47b43a1f 23#include "intel-hda.h"
d6454270 24#include "migration/vmstate.h"
0b8fa32f 25#include "qemu/module.h"
47b43a1f 26#include "intel-hda-defs.h"
d61a4ce8 27#include "audio/audio.h"
0a373bb3 28#include "trace.h"
db1015e9 29#include "qom/object.h"
d61a4ce8
GH
30
31/* -------------------------------------------------------------------------- */
32
33typedef struct desc_param {
34 uint32_t id;
35 uint32_t val;
36} desc_param;
37
38typedef 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
49typedef struct desc_codec {
50 const char *name;
51 uint32_t iid;
52 const desc_node *nodes;
53 uint32_t nnodes;
54} desc_codec;
55
56static 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
68static 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
80static 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) {
85bc5852
KZ
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;
d61a4ce8
GH
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
d61a4ce8
GH
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
2690e61e
BD
126#define PARAM mixemu
127#define HDA_MIXER
7953793c 128#include "hda-codec-common.h"
2690e61e
BD
129
130#define PARAM nomixemu
131#include "hda-codec-common.h"
132
280c1e1c 133#define HDA_TIMER_TICKS (SCALE_MS)
280c1e1c
GH
134#define B_SIZE sizeof(st->buf)
135#define B_MASK (sizeof(st->buf) - 1)
136
d61a4ce8
GH
137/* -------------------------------------------------------------------------- */
138
139static const char *fmt2name[] = {
85bc5852
KZ
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",
d61a4ce8
GH
146};
147
148typedef struct HDAAudioState HDAAudioState;
149typedef struct HDAAudioStream HDAAudioStream;
150
151struct 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;
280c1e1c
GH
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;
d61a4ce8
GH
172};
173
cd6c8830 174#define TYPE_HDA_AUDIO "hda-audio"
8110fa1d
EH
175DECLARE_INSTANCE_CHECKER(HDAAudioState, HDA_AUDIO,
176 TYPE_HDA_AUDIO)
cd6c8830 177
d61a4ce8
GH
178struct HDAAudioState {
179 HDACodecDevice hda;
180 const char *name;
181
182 QEMUSoundCard card;
183 const desc_codec *desc;
184 HDAAudioStream st[4];
ba43d289
MAL
185 bool running_compat[16];
186 bool running_real[2 * 16];
d61a4ce8
GH
187
188 /* properties */
189 uint32_t debug;
2690e61e 190 bool mixer;
280c1e1c 191 bool use_timer;
d61a4ce8
GH
192};
193
280c1e1c
GH
194static inline int64_t hda_bytes_per_second(HDAAudioStream *st)
195{
cabedbc2 196 return 2LL * st->as.nchannels * st->as.freq;
280c1e1c
GH
197}
198
199static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t target_pos)
200{
8ced0669
GH
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;
280c1e1c 209 }
9d340f67
GH
210 if (target_pos < -(2 * limit)) {
211 corr = -(4 * HDA_TIMER_TICKS);
212 }
8ced0669
GH
213 if (corr == 0) {
214 return;
215 }
216
217 trace_hda_audio_adjust(st->node->name, target_pos);
3b84967c 218 st->buft_start += corr;
280c1e1c
GH
219}
220
221static 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
3b84967c
GH
227 int64_t buft_start = st->buft_start;
228 int64_t wpos = st->wpos;
229 int64_t rpos = st->rpos;
280c1e1c
GH
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
58935915 240 int64_t to_transfer = MIN(wpos - rpos, wanted_rpos - rpos);
280c1e1c
GH
241 while (to_transfer) {
242 uint32_t start = (rpos & B_MASK);
58935915 243 uint32_t chunk = MIN(B_SIZE - start, to_transfer);
280c1e1c
GH
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;
3b84967c 251 st->rpos += chunk;
280c1e1c
GH
252 }
253
254out_timer:
255
256 if (st->running) {
257 timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS);
258 }
259}
260
d61a4ce8 261static void hda_audio_input_cb(void *opaque, int avail)
280c1e1c
GH
262{
263 HDAAudioStream *st = opaque;
264
3b84967c
GH
265 int64_t wpos = st->wpos;
266 int64_t rpos = st->rpos;
280c1e1c 267
58935915 268 int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), avail);
280c1e1c 269
280c1e1c
GH
270 while (to_transfer) {
271 uint32_t start = (uint32_t) (wpos & B_MASK);
58935915 272 uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer);
280c1e1c
GH
273 uint32_t read = AUD_read(st->voice.in, st->buf + start, chunk);
274 wpos += read;
275 to_transfer -= read;
3b84967c 276 st->wpos += read;
280c1e1c
GH
277 if (chunk != read) {
278 break;
279 }
280 }
c435fea7
VR
281
282 hda_timer_sync_adjust(st, -((wpos - rpos) - (B_SIZE >> 1)));
280c1e1c
GH
283}
284
285static 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
3b84967c
GH
291 int64_t buft_start = st->buft_start;
292 int64_t wpos = st->wpos;
293 int64_t rpos = st->rpos;
280c1e1c
GH
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
58935915 304 int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), wanted_wpos - wpos);
280c1e1c
GH
305 while (to_transfer) {
306 uint32_t start = (wpos & B_MASK);
58935915 307 uint32_t chunk = MIN(B_SIZE - start, to_transfer);
280c1e1c
GH
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;
3b84967c 315 st->wpos += chunk;
280c1e1c
GH
316 }
317
318out_timer:
319
320 if (st->running) {
321 timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS);
322 }
323}
324
325static void hda_audio_output_cb(void *opaque, int avail)
326{
327 HDAAudioStream *st = opaque;
328
3b84967c
GH
329 int64_t wpos = st->wpos;
330 int64_t rpos = st->rpos;
280c1e1c 331
58935915 332 int64_t to_transfer = MIN(wpos - rpos, avail);
280c1e1c 333
4501ee16
GH
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
280c1e1c
GH
343 while (to_transfer) {
344 uint32_t start = (uint32_t) (rpos & B_MASK);
58935915 345 uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer);
280c1e1c
GH
346 uint32_t written = AUD_write(st->voice.out, st->buf + start, chunk);
347 rpos += written;
348 to_transfer -= written;
3b84967c 349 st->rpos += written;
280c1e1c
GH
350 if (chunk != written) {
351 break;
352 }
353 }
df016a19
VR
354
355 hda_timer_sync_adjust(st, (wpos - rpos) - (B_SIZE >> 1));
280c1e1c
GH
356}
357
358static void hda_audio_compat_input_cb(void *opaque, int avail)
d61a4ce8
GH
359{
360 HDAAudioStream *st = opaque;
361 int recv = 0;
362 int len;
363 bool rc;
364
280c1e1c
GH
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;
d61a4ce8 370 recv += len;
280c1e1c 371 if (st->compat_bpos != sizeof(st->compat_buf)) {
d61a4ce8
GH
372 break;
373 }
374 }
375 rc = hda_codec_xfer(&st->state->hda, st->stream, false,
280c1e1c 376 st->compat_buf, sizeof(st->compat_buf));
d61a4ce8
GH
377 if (!rc) {
378 break;
379 }
280c1e1c 380 st->compat_bpos = 0;
d61a4ce8
GH
381 }
382}
383
280c1e1c 384static void hda_audio_compat_output_cb(void *opaque, int avail)
d61a4ce8
GH
385{
386 HDAAudioStream *st = opaque;
387 int sent = 0;
388 int len;
389 bool rc;
390
280c1e1c
GH
391 while (avail - sent >= sizeof(st->compat_buf)) {
392 if (st->compat_bpos == sizeof(st->compat_buf)) {
d61a4ce8 393 rc = hda_codec_xfer(&st->state->hda, st->stream, true,
280c1e1c 394 st->compat_buf, sizeof(st->compat_buf));
d61a4ce8
GH
395 if (!rc) {
396 break;
397 }
280c1e1c 398 st->compat_bpos = 0;
d61a4ce8 399 }
280c1e1c
GH
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;
d61a4ce8 403 sent += len;
280c1e1c 404 if (st->compat_bpos != sizeof(st->compat_buf)) {
d61a4ce8
GH
405 break;
406 }
407 }
408}
409
410static 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;
0a373bb3 419 trace_hda_audio_running(st->node->name, st->stream, st->running);
280c1e1c
GH
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 }
d61a4ce8
GH
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
438static 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
4843877e
GH
454 if (!st->state->mixer) {
455 return;
456 }
d61a4ce8 457 if (st->output) {
9fe5497c 458 AUD_set_volume_out(st->voice.out, muted, left, right);
d61a4ce8 459 } else {
9fe5497c 460 AUD_set_volume_in(st->voice.in, muted, left, right);
d61a4ce8
GH
461 }
462}
463
464static void hda_audio_setup(HDAAudioStream *st)
465{
280c1e1c
GH
466 bool use_timer = st->state->use_timer;
467 audio_callback_fn cb;
468
d61a4ce8
GH
469 if (st->node == NULL) {
470 return;
471 }
472
0a373bb3
GH
473 trace_hda_audio_format(st->node->name, st->as.nchannels,
474 fmt2name[st->as.fmt], st->as.freq);
d61a4ce8
GH
475
476 if (st->output) {
280c1e1c
GH
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 }
d61a4ce8 484 st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
280c1e1c 485 st->node->name, st, cb, &st->as);
d61a4ce8 486 } else {
280c1e1c
GH
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 }
d61a4ce8 494 st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
280c1e1c 495 st->node->name, st, cb, &st->as);
d61a4ce8
GH
496 }
497}
498
499static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
500{
cd6c8830 501 HDAAudioState *a = HDA_AUDIO(hda);
d61a4ce8
GH
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",
a89f364a 522 __func__, nid, node->name, verb, payload);
d61a4ce8
GH
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);
ba43d289 576 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
d61a4ce8
GH
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
653fail:
654 dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
a89f364a 655 __func__, nid, node ? node->name : "?", verb, payload);
d61a4ce8
GH
656 hda_codec_response(hda, true, 0);
657}
658
ba43d289 659static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
d61a4ce8 660{
cd6c8830 661 HDAAudioState *a = HDA_AUDIO(hda);
d61a4ce8
GH
662 int s;
663
ba43d289
MAL
664 a->running_compat[stnr] = running;
665 a->running_real[output * 16 + stnr] = running;
d61a4ce8
GH
666 for (s = 0; s < ARRAY_SIZE(a->st); s++) {
667 if (a->st[s].node == NULL) {
668 continue;
669 }
ba43d289
MAL
670 if (a->st[s].output != output) {
671 continue;
672 }
d61a4ce8
GH
673 if (a->st[s].stream != stnr) {
674 continue;
675 }
676 hda_audio_set_running(&a->st[s], running);
677 }
678}
679
680static int hda_audio_init(HDACodecDevice *hda, const struct desc_codec *desc)
681{
cd6c8830 682 HDAAudioState *a = HDA_AUDIO(hda);
d61a4ce8
GH
683 HDAAudioStream *st;
684 const desc_node *node;
685 const desc_param *param;
686 uint32_t i, type;
687
688 a->desc = desc;
f79f2bfc 689 a->name = object_get_typename(OBJECT(a));
a89f364a 690 dprint(a, 1, "%s: cad %d\n", __func__, a->hda.cad);
d61a4ce8
GH
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);
2ab5bf67 696 if (param == NULL) {
d61a4ce8 697 continue;
2ab5bf67 698 }
d61a4ce8
GH
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;
280c1e1c 711 st->compat_bpos = sizeof(st->compat_buf);
d61a4ce8
GH
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
5eaa8e1e 726static void hda_audio_exit(HDACodecDevice *hda)
129dcd2c 727{
cd6c8830 728 HDAAudioState *a = HDA_AUDIO(hda);
129dcd2c
GH
729 HDAAudioStream *st;
730 int i;
731
a89f364a 732 dprint(a, 1, "%s\n", __func__);
129dcd2c
GH
733 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
734 st = a->st + i;
735 if (st->node == NULL) {
736 continue;
737 }
280c1e1c
GH
738 if (a->use_timer) {
739 timer_del(st->buft);
740 }
129dcd2c
GH
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);
129dcd2c
GH
748}
749
d61a4ce8
GH
750static int hda_audio_post_load(void *opaque, int version)
751{
752 HDAAudioState *a = opaque;
753 HDAAudioStream *st;
754 int i;
755
a89f364a 756 dprint(a, 1, "%s\n", __func__);
ba43d289
MAL
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
d61a4ce8
GH
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);
ba43d289 770 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
d61a4ce8
GH
771 }
772 return 0;
773}
774
39e6a38c
GH
775static void hda_audio_reset(DeviceState *dev)
776{
cd6c8830 777 HDAAudioState *a = HDA_AUDIO(dev);
39e6a38c
GH
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
280c1e1c
GH
790static bool vmstate_hda_audio_stream_buf_needed(void *opaque)
791{
792 HDAAudioStream *st = opaque;
67fa1f57 793 return st->state && st->state->use_timer;
280c1e1c
GH
794}
795
796static 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
d61a4ce8
GH
810static const VMStateDescription vmstate_hda_audio_stream = {
811 .name = "hda-audio-stream",
812 .version_id = 1,
d49805ae 813 .fields = (VMStateField[]) {
d61a4ce8
GH
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),
280c1e1c
GH
821 VMSTATE_UINT32(compat_bpos, HDAAudioStream),
822 VMSTATE_BUFFER(compat_buf, HDAAudioStream),
d61a4ce8 823 VMSTATE_END_OF_LIST()
280c1e1c
GH
824 },
825 .subsections = (const VMStateDescription * []) {
826 &vmstate_hda_audio_stream_buf,
827 NULL
d61a4ce8
GH
828 }
829};
830
831static const VMStateDescription vmstate_hda_audio = {
832 .name = "hda-audio",
ba43d289 833 .version_id = 2,
d61a4ce8 834 .post_load = hda_audio_post_load,
d49805ae 835 .fields = (VMStateField[]) {
d61a4ce8
GH
836 VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0,
837 vmstate_hda_audio_stream,
838 HDAAudioStream),
ba43d289
MAL
839 VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
840 VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
d61a4ce8
GH
841 VMSTATE_END_OF_LIST()
842 }
843};
844
845static Property hda_audio_properties[] = {
88e47b9a 846 DEFINE_AUDIO_PROPERTIES(HDAAudioState, card),
2690e61e 847 DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0),
2690e61e 848 DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true),
bc753dc0 849 DEFINE_PROP_BOOL("use-timer", HDAAudioState, use_timer, true),
d61a4ce8
GH
850 DEFINE_PROP_END_OF_LIST(),
851};
852
853static int hda_audio_init_output(HDACodecDevice *hda)
854{
cd6c8830 855 HDAAudioState *a = HDA_AUDIO(hda);
2690e61e
BD
856
857 if (!a->mixer) {
858 return hda_audio_init(hda, &output_nomixemu);
859 } else {
2690e61e 860 return hda_audio_init(hda, &output_mixemu);
2690e61e 861 }
d61a4ce8
GH
862}
863
864static int hda_audio_init_duplex(HDACodecDevice *hda)
865{
cd6c8830 866 HDAAudioState *a = HDA_AUDIO(hda);
2690e61e
BD
867
868 if (!a->mixer) {
869 return hda_audio_init(hda, &duplex_nomixemu);
870 } else {
2690e61e 871 return hda_audio_init(hda, &duplex_mixemu);
2690e61e 872 }
d61a4ce8
GH
873}
874
20110065
GH
875static int hda_audio_init_micro(HDACodecDevice *hda)
876{
cd6c8830 877 HDAAudioState *a = HDA_AUDIO(hda);
2690e61e
BD
878
879 if (!a->mixer) {
880 return hda_audio_init(hda, &micro_nomixemu);
881 } else {
2690e61e 882 return hda_audio_init(hda, &micro_mixemu);
2690e61e 883 }
20110065
GH
884}
885
cd6c8830 886static void hda_audio_base_class_init(ObjectClass *klass, void *data)
dbaa7904 887{
39bffca2 888 DeviceClass *dc = DEVICE_CLASS(klass);
dbaa7904
AL
889 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
890
dbaa7904
AL
891 k->exit = hda_audio_exit;
892 k->command = hda_audio_command;
893 k->stream = hda_audio_stream;
125ee0ed 894 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
39e6a38c 895 dc->reset = hda_audio_reset;
39bffca2 896 dc->vmsd = &vmstate_hda_audio;
4f67d30b 897 device_class_set_props(dc, hda_audio_properties);
dbaa7904
AL
898}
899
cd6c8830
GH
900static const TypeInfo hda_audio_info = {
901 .name = TYPE_HDA_AUDIO,
902 .parent = TYPE_HDA_CODEC_DEVICE,
edf632ee 903 .instance_size = sizeof(HDAAudioState),
cd6c8830
GH
904 .class_init = hda_audio_base_class_init,
905 .abstract = true,
906};
907
908static 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
8c43a6f0 917static const TypeInfo hda_audio_output_info = {
39bffca2 918 .name = "hda-output",
cd6c8830 919 .parent = TYPE_HDA_AUDIO,
39bffca2 920 .class_init = hda_audio_output_class_init,
d61a4ce8
GH
921};
922
dbaa7904
AL
923static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
924{
39bffca2 925 DeviceClass *dc = DEVICE_CLASS(klass);
dbaa7904
AL
926 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
927
928 k->init = hda_audio_init_duplex;
20110065 929 dc->desc = "HDA Audio Codec, duplex (line-out, line-in)";
dbaa7904
AL
930}
931
8c43a6f0 932static const TypeInfo hda_audio_duplex_info = {
39bffca2 933 .name = "hda-duplex",
cd6c8830 934 .parent = TYPE_HDA_AUDIO,
39bffca2 935 .class_init = hda_audio_duplex_class_init,
d61a4ce8
GH
936};
937
20110065
GH
938static 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;
20110065 944 dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
20110065
GH
945}
946
8c43a6f0 947static const TypeInfo hda_audio_micro_info = {
20110065 948 .name = "hda-micro",
cd6c8830 949 .parent = TYPE_HDA_AUDIO,
20110065
GH
950 .class_init = hda_audio_micro_class_init,
951};
952
83f7d43a 953static void hda_audio_register_types(void)
d61a4ce8 954{
cd6c8830 955 type_register_static(&hda_audio_info);
39bffca2
AL
956 type_register_static(&hda_audio_output_info);
957 type_register_static(&hda_audio_duplex_info);
20110065 958 type_register_static(&hda_audio_micro_info);
d61a4ce8 959}
83f7d43a
AF
960
961type_init(hda_audio_register_types)