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