VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevIchAc97.cpp@ 1965

Last change on this file since 1965 was 1551, checked in by vboxsync, 18 years ago

Mac OS X audio support (only playback).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.6 KB
Line 
1/** @file
2 *
3 * VBox ICH AC97 Audio Controller
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_DEV_AUDIO
26#include <VBox/pdm.h>
27#include <VBox/err.h>
28
29#include <VBox/log.h>
30#include <iprt/assert.h>
31#include <iprt/uuid.h>
32#include <iprt/string.h>
33#include <VBox/mm.h>
34
35#include "Builtins.h"
36
37extern "C" {
38#include "audio.h"
39}
40
41#undef LOG_VOICES
42#ifndef VBOX
43//#define USE_MIXER
44#else
45#define USE_MIXER
46#endif
47
48#define AC97_SSM_VERSION 1
49
50enum {
51 AC97_Reset = 0x00,
52 AC97_Master_Volume_Mute = 0x02,
53 AC97_Headphone_Volume_Mute = 0x04,
54 AC97_Master_Volume_Mono_Mute = 0x06,
55 AC97_Master_Tone_RL = 0x08,
56 AC97_PC_BEEP_Volume_Mute = 0x0A,
57 AC97_Phone_Volume_Mute = 0x0C,
58 AC97_Mic_Volume_Mute = 0x0E,
59 AC97_Line_In_Volume_Mute = 0x10,
60 AC97_CD_Volume_Mute = 0x12,
61 AC97_Video_Volume_Mute = 0x14,
62 AC97_Aux_Volume_Mute = 0x16,
63 AC97_PCM_Out_Volume_Mute = 0x18,
64 AC97_Record_Select = 0x1A,
65 AC97_Record_Gain_Mute = 0x1C,
66 AC97_Record_Gain_Mic_Mute = 0x1E,
67 AC97_General_Purpose = 0x20,
68 AC97_3D_Control = 0x22,
69 AC97_AC_97_RESERVED = 0x24,
70 AC97_Powerdown_Ctrl_Stat = 0x26,
71 AC97_Extended_Audio_ID = 0x28,
72 AC97_Extended_Audio_Ctrl_Stat = 0x2A,
73 AC97_PCM_Front_DAC_Rate = 0x2C,
74 AC97_PCM_Surround_DAC_Rate = 0x2E,
75 AC97_PCM_LFE_DAC_Rate = 0x30,
76 AC97_PCM_LR_ADC_Rate = 0x32,
77 AC97_MIC_ADC_Rate = 0x34,
78 AC97_6Ch_Vol_C_LFE_Mute = 0x36,
79 AC97_6Ch_Vol_L_R_Surround_Mute = 0x38,
80 AC97_Vendor_Reserved = 0x58,
81 AC97_Vendor_ID1 = 0x7c,
82 AC97_Vendor_ID2 = 0x7e
83};
84
85#ifndef VBOX
86#define SOFT_VOLUME
87#else
88#undef SOFT_VOLUME
89#endif
90#define SR_FIFOE BIT(4) /* rwc, fifo error */
91#define SR_BCIS BIT(3) /* rwc, buffer completion interrupt status */
92#define SR_LVBCI BIT(2) /* rwc, last valid buffer completion interrupt */
93#define SR_CELV BIT(1) /* ro, current equals last valid */
94#define SR_DCH BIT(0) /* ro, controller halted */
95#define SR_VALID_MASK (BIT(5) - 1)
96#define SR_WCLEAR_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
97#define SR_RO_MASK (SR_DCH | SR_CELV)
98#define SR_INT_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
99
100#define CR_IOCE BIT(4) /* rw */
101#define CR_FEIE BIT(3) /* rw */
102#define CR_LVBIE BIT(2) /* rw */
103#define CR_RR BIT(1) /* rw */
104#define CR_RPBM BIT(0) /* rw */
105#define CR_VALID_MASK (BIT(5) - 1)
106#define CR_DONT_CLEAR_MASK (CR_IOCE | CR_FEIE | CR_LVBIE)
107
108#define GC_WR 4 /* rw */
109#define GC_CR 2 /* rw */
110#define GC_VALID_MASK (BIT(6) - 1)
111
112#define GS_MD3 BIT(17) /* rw */
113#define GS_AD3 BIT(16) /* rw */
114#define GS_RCS BIT(15) /* rwc */
115#define GS_B3S12 BIT(14) /* ro */
116#define GS_B2S12 BIT(13) /* ro */
117#define GS_B1S12 BIT(12) /* ro */
118#define GS_S1R1 BIT(11) /* rwc */
119#define GS_S0R1 BIT(10) /* rwc */
120#define GS_S1CR BIT(9) /* ro */
121#define GS_S0CR BIT(8) /* ro */
122#define GS_MINT BIT(7) /* ro */
123#define GS_POINT BIT(6) /* ro */
124#define GS_PIINT BIT(5) /* ro */
125#define GS_RSRVD (BIT(4)|BIT(3))
126#define GS_MOINT BIT(2) /* ro */
127#define GS_MIINT BIT(1) /* ro */
128#define GS_GSCI BIT(0) /* rwc */
129#define GS_RO_MASK (GS_B3S12| \
130 GS_B2S12| \
131 GS_B1S12| \
132 GS_S1CR| \
133 GS_S0CR| \
134 GS_MINT| \
135 GS_POINT| \
136 GS_PIINT| \
137 GS_RSRVD| \
138 GS_MOINT| \
139 GS_MIINT)
140#define GS_VALID_MASK (BIT(18) - 1)
141#define GS_WCLEAR_MASK (GS_RCS|GS_S1R1|GS_S0R1|GS_GSCI)
142
143/** Buffer Descriptor */
144#define BD_IOC BIT(31) /* Interrupt on Completion */
145#define BD_BUP BIT(30) /* Buffer Underrun Policy */
146
147#define EACS_VRA 1
148#define EACS_VRM 8
149
150#define VOL_MASK 0x1f
151#define MUTE_SHIFT 15
152
153#define REC_MASK 7
154enum
155{
156 REC_MIC = 0,
157 REC_CD,
158 REC_VIDEO,
159 REC_AUX,
160 REC_LINE_IN,
161 REC_STEREO_MIX,
162 REC_MONO_MIX,
163 REC_PHONE
164};
165
166typedef struct BD
167{
168 uint32_t addr;
169 uint32_t ctl_len;
170} BD;
171
172typedef struct AC97BusMasterRegs
173{
174 uint32_t bdbar; /* rw 0, buffer descriptor list base address register */
175 uint8_t civ; /* ro 0, current index value */
176 uint8_t lvi; /* rw 0, last valid index */
177 uint16_t sr; /* rw 1, status register */
178 uint16_t picb; /* ro 0, position in current buffer */
179 uint8_t piv; /* ro 0, prefetched index value */
180 uint8_t cr; /* rw 0, control register */
181 int bd_valid; /* initialized? */
182 BD bd;
183} AC97BusMasterRegs;
184
185typedef struct AC97LinkState
186{
187 QEMUSoundCard card;
188 /** Global Control (Bus Master Control Register) */
189 uint32_t glob_cnt;
190 /** Global Status (Bus Master Control Register) */
191 uint32_t glob_sta;
192 /** Codec Access Semaphore Register (Bus Master Control Register) */
193 uint32_t cas;
194 uint32_t last_samp;
195 /** Bus Master Control Registers for PCM in, PCM out, and Mic in */
196 AC97BusMasterRegs bm_regs[3];
197 uint8_t mixer_data[256];
198 /** PCM in */
199 SWVoiceIn *voice_pi;
200 /** PCM out */
201 SWVoiceOut *voice_po;
202 /** Mic in */
203 SWVoiceIn *voice_mc;
204 uint8_t silence[128];
205 int bup_flag;
206 /** Pointer to the device instance. */
207 PPDMDEVINS pDevIns;
208 /** Pointer to the connector of the attached audio driver. */
209 PPDMIAUDIOCONNECTOR pDrv;
210 /** Pointer to the attached audio driver. */
211 PPDMIBASE pDrvBase;
212 /** The base interface. */
213 PDMIBASE IBase;
214 /** Base port of the I/O space region. */
215 RTIOPORT IOPortBase[2];
216} AC97LinkState;
217
218#define ICHAC97STATE_2_DEVINS(pAC97) ((pAC97)->pDevIns)
219#define PCIDEV_2_ICHAC97STATE(pPciDev) ((PCIAC97LinkState *)(pPciDev))
220
221enum
222{
223 BUP_SET = BIT(0),
224 BUP_LAST = BIT(1)
225};
226
227typedef struct PCIAC97LinkState
228{
229 PCIDevice dev;
230 AC97LinkState ac97;
231} PCIAC97LinkState;
232
233#define MKREGS(prefix, start) \
234enum { \
235 prefix ## _BDBAR = start, \
236 prefix ## _CIV = start + 4, \
237 prefix ## _LVI = start + 5, \
238 prefix ## _SR = start + 6, \
239 prefix ## _PICB = start + 8, \
240 prefix ## _PIV = start + 10, \
241 prefix ## _CR = start + 11 \
242}
243
244enum
245{
246 PI_INDEX = 0, /* PCM in */
247 PO_INDEX, /* PCM out */
248 MC_INDEX, /* Mic in */
249 LAST_INDEX
250};
251
252MKREGS (PI, PI_INDEX * 16);
253MKREGS (PO, PO_INDEX * 16);
254MKREGS (MC, MC_INDEX * 16);
255
256enum
257{
258 GLOB_CNT = 0x2c,
259 GLOB_STA = 0x30,
260 CAS = 0x34
261};
262
263#define GET_BM(index) (((index) >> 4) & 3)
264
265static void po_callback (void *opaque, int free);
266static void pi_callback (void *opaque, int avail);
267static void mc_callback (void *opaque, int avail);
268
269static void warm_reset (AC97LinkState *s)
270{
271 (void) s;
272}
273
274static void cold_reset (AC97LinkState * s)
275{
276 (void) s;
277}
278
279/** Fetch Buffer Descriptor at _CIV */
280static void fetch_bd (AC97LinkState *s, AC97BusMasterRegs *r)
281{
282 PPDMDEVINS pDevIns = ICHAC97STATE_2_DEVINS(s);
283 uint8_t b[8];
284
285 PDMDevHlpPhysRead (pDevIns, r->bdbar + r->civ * 8, b, sizeof(b));
286 r->bd_valid = 1;
287#if !defined(__X86__) && !defined(__AMD64__)
288#error Please adapt the code (audio buffers are little endian)!
289#else
290 r->bd.addr = (*(uint32_t *) &b[0]) & ~3;
291 r->bd.ctl_len = (*(uint32_t *) &b[4]);
292#endif
293 r->picb = r->bd.ctl_len & 0xffff;
294 Log (("ac97: bd %2d addr=%#x ctl=%#06x len=%#x(%d bytes)\n",
295 r->civ, r->bd.addr, r->bd.ctl_len >> 16,
296 r->bd.ctl_len & 0xffff, (r->bd.ctl_len & 0xffff) << 1));
297}
298
299/**
300 * Update the BM status register
301 */
302static void update_sr (AC97LinkState *s, AC97BusMasterRegs *r, uint32_t new_sr)
303{
304 PPDMDEVINS pDevIns = ICHAC97STATE_2_DEVINS(s);
305 int event = 0;
306 int level = 0;
307 uint32_t new_mask = new_sr & SR_INT_MASK;
308 uint32_t old_mask = r->sr & SR_INT_MASK;
309 uint32_t masks[] = {GS_PIINT, GS_POINT, GS_MINT};
310
311 if (new_mask ^ old_mask)
312 {
313 /** @todo is IRQ deasserted when only one of status bits is cleared? */
314 if (!new_mask)
315 {
316 event = 1;
317 level = 0;
318 }
319 else if ((new_mask & SR_LVBCI) && (r->cr & CR_LVBIE))
320 {
321 event = 1;
322 level = 1;
323 }
324 else if ((new_mask & SR_BCIS) && (r->cr & CR_IOCE))
325 {
326 event = 1;
327 level = 1;
328 }
329 }
330
331 r->sr = new_sr;
332
333 Log (("ac97: IOC%d LVB%d sr=%#x event=%d level=%d\n",
334 r->sr & SR_BCIS, r->sr & SR_LVBCI, r->sr, event, level));
335
336 if (event)
337 {
338 s->glob_sta |= masks[r - s->bm_regs];
339 Log (("ac97: set irq level=%d\n", !!level));
340 PDMDevHlpPCISetIrq (pDevIns, 0, !!level);
341 }
342}
343
344static void voice_set_active (AC97LinkState *s, int bm_index, int on)
345{
346 switch (bm_index)
347 {
348 case PI_INDEX: AUD_set_active_in (s->voice_pi, on); break;
349 case PO_INDEX: AUD_set_active_out(s->voice_po, on); break;
350 case MC_INDEX: AUD_set_active_in (s->voice_mc, on); break;
351 default: AssertFailed ();
352 break;
353 }
354}
355
356static void reset_bm_regs (AC97LinkState *s, AC97BusMasterRegs *r)
357{
358 Log (("ac97: reset_bm_regs\n"));
359 r->bdbar = 0;
360 r->civ = 0;
361 r->lvi = 0;
362 /** @todo do we need to do that? */
363 update_sr (s, r, SR_DCH);
364 r->picb = 0;
365 r->piv = 0;
366 r->cr = r->cr & CR_DONT_CLEAR_MASK;
367 r->bd_valid = 0;
368
369 voice_set_active (s, r - s->bm_regs, 0);
370 memset (s->silence, 0, sizeof (s->silence));
371}
372
373static void mixer_store (AC97LinkState *s, uint32_t i, uint16_t v)
374{
375 if (i + 2 > sizeof (s->mixer_data))
376 {
377 Log (("ac97: mixer_store: index %d out of bounds %d\n",
378 i, sizeof (s->mixer_data)));
379 return;
380 }
381
382 s->mixer_data[i + 0] = v & 0xff;
383 s->mixer_data[i + 1] = v >> 8;
384}
385
386static uint16_t mixer_load (AC97LinkState *s, uint32_t i)
387{
388 uint16_t val;
389
390 if (i + 2 > sizeof (s->mixer_data))
391 {
392 Log (("ac97: mixer_store: index %d out of bounds %d\n",
393 i, sizeof (s->mixer_data)));
394 val = 0xffff;
395 }
396 else
397 val = s->mixer_data[i + 0] | (s->mixer_data[i + 1] << 8);
398
399 return val;
400}
401
402static void open_voice (AC97LinkState *s, int index, int freq)
403{
404 audsettings_t as;
405
406 if (freq)
407 {
408 as.freq = freq;
409 as.nchannels = 2;
410 as.fmt = AUD_FMT_S16;
411 as.endianness = 0;
412
413 switch (index)
414 {
415 case PI_INDEX: /* PCM in */
416 s->voice_pi = AUD_open_in (&s->card, s->voice_pi, "ac97.pi",
417 s, pi_callback, &as);
418#ifdef LOG_VOICES
419 LogRel(("AC97: open PI freq=%d (%s)\n", freq, s->voice_pi ? "ok" : "FAIL"));
420#endif
421 break;
422
423 case PO_INDEX: /* PCM out */
424 s->voice_po = AUD_open_out (&s->card, s->voice_po, "ac97.po",
425 s, po_callback, &as);
426#ifdef LOG_VOICES
427 LogRel(("AC97: open PO freq=%d (%s)\n", freq, s->voice_po ? "ok" : "FAIL"));
428#endif
429 break;
430
431 case MC_INDEX: /* Mic in */
432 s->voice_mc = AUD_open_in (&s->card, s->voice_mc, "ac97.mc",
433 s, mc_callback, &as);
434#ifdef LOG_VOICES
435 LogRel(("AC97: open MC freq=%d (%s)\n", freq, s->voice_mc ? "ok" : "FAIL"));
436#endif
437 break;
438 }
439 }
440 else
441 {
442 switch (index)
443 {
444 case PI_INDEX:
445 AUD_close_in (&s->card, s->voice_pi);
446#ifdef LOG_VOICES
447 LogRel(("AC97: Closing PCM IN\n"));
448#endif
449 s->voice_pi = NULL;
450 break;
451
452 case PO_INDEX:
453 AUD_close_out (&s->card, s->voice_po);
454#ifdef LOG_VOICES
455 LogRel(("AC97: Closing PCM OUT\n"));
456#endif
457 s->voice_po = NULL;
458 break;
459
460 case MC_INDEX:
461 AUD_close_in (&s->card, s->voice_mc);
462#ifdef LOG_VOICES
463 LogRel(("AC97: Closing MIC IN\n"));
464#endif
465 s->voice_mc = NULL;
466 break;
467 }
468 }
469}
470
471static void reset_voices (AC97LinkState *s, uint8_t active[LAST_INDEX])
472{
473 uint16_t freq;
474
475 freq = mixer_load (s, AC97_PCM_LR_ADC_Rate);
476 open_voice (s, PI_INDEX, freq);
477 AUD_set_active_in (s->voice_pi, active[PI_INDEX]);
478
479 freq = mixer_load (s, AC97_PCM_Front_DAC_Rate);
480 open_voice (s, PO_INDEX, freq);
481 AUD_set_active_out (s->voice_po, active[PO_INDEX]);
482
483 freq = mixer_load (s, AC97_MIC_ADC_Rate);
484 open_voice (s, MC_INDEX, freq);
485 AUD_set_active_in (s->voice_mc, active[MC_INDEX]);
486}
487
488#ifdef USE_MIXER
489static void set_volume (AC97LinkState *s, int index,
490 audmixerctl_t mt, uint32_t val)
491{
492 int mute = (val >> MUTE_SHIFT) & 1;
493 uint8_t rvol = VOL_MASK - (val & VOL_MASK);
494 uint8_t lvol = VOL_MASK - ((val >> 8) & VOL_MASK);
495 rvol = 255 * rvol / VOL_MASK;
496 lvol = 255 * lvol / VOL_MASK;
497
498#ifdef SOFT_VOLUME
499 if (index == AC97_Master_Volume_Mute)
500 AUD_set_volume_out (s->voice_po, mute, lvol, rvol);
501 else
502 AUD_set_volume (mt, &mute, &lvol, &rvol);
503#else
504 AUD_set_volume (mt, &mute, &lvol, &rvol);
505#endif
506
507 rvol = VOL_MASK - ((VOL_MASK * rvol) / 255);
508 lvol = VOL_MASK - ((VOL_MASK * lvol) / 255);
509
510#ifdef VBOX
511 /*
512 * From AC'97 SoundMax Codec AD1981A: "Because AC '97 defines 6-bit volume registers, to
513 * maintain compatibility whenever the D5 or D13 bits are set to `1,' their respective
514 * lower five volume bits are automatically set to `1' by the Codec logic. On readback,
515 * all lower 5 bits will read ones whenever these bits are set to `1.'"
516 *
517 * Linux ALSA depends on this behavior.
518 */
519 if (val & BIT(5))
520 val |= BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0);
521 if (val & BIT(13))
522 val |= BIT(12) | BIT(11) | BIT(10) | BIT(9) | BIT(8);
523#endif
524
525 mixer_store (s, index, val);
526}
527
528static audrecsource_t ac97_to_aud_record_source (uint8_t i)
529{
530 switch (i)
531 {
532 case REC_MIC: return AUD_REC_MIC;
533 case REC_CD: return AUD_REC_CD;
534 case REC_VIDEO: return AUD_REC_VIDEO;
535 case REC_AUX: return AUD_REC_AUX;
536 case REC_LINE_IN: return AUD_REC_LINE_IN;
537 case REC_PHONE: return AUD_REC_PHONE;
538 default: Log (("ac97: Unknown record source %d, using MIC\n", i));
539 return AUD_REC_MIC;
540 }
541}
542
543static uint8_t aud_to_ac97_record_source (audrecsource_t rs)
544{
545 switch (rs)
546 {
547 case AUD_REC_MIC: return REC_MIC;
548 case AUD_REC_CD: return REC_CD;
549 case AUD_REC_VIDEO: return REC_VIDEO;
550 case AUD_REC_AUX: return REC_AUX;
551 case AUD_REC_LINE_IN: return REC_LINE_IN;
552 case AUD_REC_PHONE: return REC_PHONE;
553 default: Log (("ac97: Unknown audio recording source %d using MIC\n", rs));
554 return REC_MIC;
555 }
556}
557
558static void record_select (AC97LinkState *s, uint32_t val)
559{
560 uint8_t rs = val & REC_MASK;
561 uint8_t ls = (val >> 8) & REC_MASK;
562 audrecsource_t ars = ac97_to_aud_record_source (rs);
563 audrecsource_t als = ac97_to_aud_record_source (ls);
564 AUD_set_record_source (&als, &ars);
565 rs = aud_to_ac97_record_source (ars);
566 ls = aud_to_ac97_record_source (als);
567 mixer_store (s, AC97_Record_Select, rs | (ls << 8));
568}
569#endif
570
571static void mixer_reset (AC97LinkState *s)
572{
573 uint8_t active[LAST_INDEX];
574
575 Log (("ac97: mixer_reset\n"));
576 memset (s->mixer_data, 0, sizeof (s->mixer_data));
577 memset (active, 0, sizeof (active));
578 mixer_store (s, AC97_Reset , 0x0000); /* 6940 */
579 mixer_store (s, AC97_Master_Volume_Mono_Mute , 0x8000);
580 mixer_store (s, AC97_PC_BEEP_Volume_Mute , 0x0000);
581
582 mixer_store (s, AC97_Phone_Volume_Mute , 0x8008);
583 mixer_store (s, AC97_Mic_Volume_Mute , 0x8008);
584 mixer_store (s, AC97_CD_Volume_Mute , 0x8808);
585 mixer_store (s, AC97_Aux_Volume_Mute , 0x8808);
586 mixer_store (s, AC97_Record_Gain_Mic_Mute , 0x8000);
587 mixer_store (s, AC97_General_Purpose , 0x0000);
588 mixer_store (s, AC97_3D_Control , 0x0000);
589 mixer_store (s, AC97_Powerdown_Ctrl_Stat , 0x000f);
590
591 /*
592 * Sigmatel 9700 (STAC9700)
593 */
594 mixer_store (s, AC97_Vendor_ID1 , 0x8384);
595 mixer_store (s, AC97_Vendor_ID2 , 0x7600); /* 7608 */
596
597 mixer_store (s, AC97_Extended_Audio_ID , 0x0809);
598 mixer_store (s, AC97_Extended_Audio_Ctrl_Stat, 0x0009);
599 mixer_store (s, AC97_PCM_Front_DAC_Rate , 0xbb80);
600 mixer_store (s, AC97_PCM_Surround_DAC_Rate , 0xbb80);
601 mixer_store (s, AC97_PCM_LFE_DAC_Rate , 0xbb80);
602 mixer_store (s, AC97_PCM_LR_ADC_Rate , 0xbb80);
603 mixer_store (s, AC97_MIC_ADC_Rate , 0xbb80);
604
605#ifdef USE_MIXER
606 record_select (s, 0);
607 set_volume (s, AC97_Master_Volume_Mute, AUD_MIXER_VOLUME, 0x8000);
608 set_volume (s, AC97_PCM_Out_Volume_Mute, AUD_MIXER_PCM, 0x8808);
609 set_volume (s, AC97_Line_In_Volume_Mute, AUD_MIXER_LINE_IN, 0x8808);
610#else
611 mixer_store (s, AC97_Record_Select, 0);
612 mixer_store (s, AC97_Master_Volume_Mute, 0x8000);
613 mixer_store (s, AC97_PCM_Out_Volume_Mute, 0x8808);
614 mixer_store (s, AC97_Line_In_Volume_Mute, 0x8808);
615#endif
616
617 reset_voices (s, active);
618}
619
620static int write_audio (AC97LinkState *s, AC97BusMasterRegs *r,
621 int max, int *stop)
622{
623 PPDMDEVINS pDevIns = ICHAC97STATE_2_DEVINS(s);
624 uint8_t tmpbuf[4096];
625 uint32_t addr = r->bd.addr;
626 uint32_t temp = r->picb << 1;
627 uint32_t written = 0;
628 int to_copy = 0;
629
630 temp = audio_MIN (temp, (uint32_t) max);
631 if (!temp)
632 {
633 *stop = 1;
634 return 0;
635 }
636
637 while (temp)
638 {
639 int copied;
640 to_copy = audio_MIN (temp, sizeof (tmpbuf));
641 PDMDevHlpPhysRead (pDevIns, addr, tmpbuf, to_copy);
642 copied = AUD_write (s->voice_po, tmpbuf, to_copy);
643 Log (("ac97: write_audio max=%x to_copy=%x copied=%x\n",
644 max, to_copy, copied));
645 if (!copied)
646 {
647 *stop = 1;
648 break;
649 }
650 temp -= copied;
651 addr += copied;
652 written += copied;
653 }
654
655 if (!temp)
656 {
657 if (to_copy < 4)
658 {
659 Log (("ac97: whoops\n"));
660 s->last_samp = 0;
661 }
662 else
663 s->last_samp = *(uint32_t *) &tmpbuf[to_copy - 4];
664 }
665
666 r->bd.addr = addr;
667 return written;
668}
669
670static void write_bup (AC97LinkState *s, int elapsed)
671{
672 int written = 0;
673
674 Log (("ac97: write_bup\n"));
675 if (!(s->bup_flag & BUP_SET))
676 {
677 if (s->bup_flag & BUP_LAST)
678 {
679 unsigned int i;
680 uint32_t *p = (uint32_t*)s->silence;
681 for (i = 0; i < sizeof (s->silence) / 4; i++)
682 *p++ = s->last_samp;
683 }
684 else
685 memset (s->silence, 0, sizeof (s->silence));
686
687 s->bup_flag |= BUP_SET;
688 }
689
690 while (elapsed)
691 {
692 unsigned int temp = audio_MIN ((unsigned int)elapsed, sizeof (s->silence));
693 while (temp)
694 {
695 int copied = AUD_write (s->voice_po, s->silence, temp);
696 if (!copied)
697 return;
698 temp -= copied;
699 elapsed -= copied;
700 written += copied;
701 }
702 }
703}
704
705static int read_audio (AC97LinkState *s, AC97BusMasterRegs *r,
706 int max, int *stop)
707{
708 PPDMDEVINS pDevIns = ICHAC97STATE_2_DEVINS(s);
709 uint8_t tmpbuf[4096];
710 uint32_t addr = r->bd.addr;
711 uint32_t temp = r->picb << 1;
712 uint32_t nread = 0;
713 int to_copy = 0;
714 SWVoiceIn *voice = (r - s->bm_regs) == MC_INDEX ? s->voice_mc : s->voice_pi;
715
716 temp = audio_MIN (temp, (uint32_t) max);
717 if (!temp)
718 {
719 *stop = 1;
720 return 0;
721 }
722
723 while (temp)
724 {
725 int acquired;
726 to_copy = audio_MIN (temp, sizeof (tmpbuf));
727 acquired = AUD_read (voice, tmpbuf, to_copy);
728 if (!acquired)
729 {
730 *stop = 1;
731 break;
732 }
733 PDMDevHlpPhysWrite (pDevIns, addr, tmpbuf, acquired);
734 temp -= acquired;
735 addr += acquired;
736 nread += acquired;
737 }
738
739 r->bd.addr = addr;
740 return nread;
741}
742
743static void transfer_audio (AC97LinkState *s, int index, int elapsed)
744{
745 AC97BusMasterRegs *r = &s->bm_regs[index];
746 int written = 0, stop = 0;
747
748 if (r->sr & SR_DCH)
749 {
750 if (r->cr & CR_RPBM)
751 {
752 switch (index)
753 {
754 case PO_INDEX:
755 write_bup (s, elapsed);
756 break;
757 }
758 }
759 return;
760 }
761
762 while ((elapsed >> 1) && !stop)
763 {
764 int temp;
765
766 if (!r->bd_valid)
767 {
768 Log (("ac97: invalid bd\n"));
769 fetch_bd (s, r);
770 }
771
772 if (!r->picb)
773 {
774 Log (("ac97: fresh bd %d is empty %#x %#x\n",
775 r->civ, r->bd.addr, r->bd.ctl_len));
776 if (r->civ == r->lvi)
777 {
778 r->sr |= SR_DCH; /* CELV? */
779 s->bup_flag = 0;
780 break;
781 }
782 r->sr &= ~SR_CELV;
783 r->civ = r->piv;
784 r->piv = (r->piv + 1) % 32;
785 fetch_bd (s, r);
786 return;
787 }
788
789 switch (index)
790 {
791 case PO_INDEX:
792 temp = write_audio (s, r, elapsed, &stop);
793 written += temp;
794 elapsed -= temp;
795 r->picb -= (temp >> 1);
796 break;
797
798 case PI_INDEX:
799 case MC_INDEX:
800 temp = read_audio (s, r, elapsed, &stop);
801 elapsed -= temp;
802 r->picb -= (temp >> 1);
803 break;
804 }
805
806 Log(("r->picb = %d\n", r->picb));
807
808 if (!r->picb)
809 {
810 uint32_t new_sr = r->sr & ~SR_CELV;
811
812 if (r->bd.ctl_len & BD_IOC)
813 new_sr |= SR_BCIS;
814
815 if (r->civ == r->lvi)
816 {
817 Log (("ac97: Underrun civ (%d) == lvi (%d)\n", r->civ, r->lvi));
818 new_sr |= SR_LVBCI | SR_DCH | SR_CELV;
819 stop = 1;
820 s->bup_flag = (r->bd.ctl_len & BD_BUP) ? BUP_LAST : 0;
821 }
822 else
823 {
824 r->civ = r->piv;
825 r->piv = (r->piv + 1) % 32;
826 fetch_bd (s, r);
827 }
828 update_sr (s, r, new_sr);
829 }
830 }
831}
832
833static void pi_callback (void *opaque, int avail)
834{
835 transfer_audio ((AC97LinkState*)opaque, PI_INDEX, avail);
836}
837
838static void mc_callback (void *opaque, int avail)
839{
840 transfer_audio ((AC97LinkState*)opaque, MC_INDEX, avail);
841}
842
843static void po_callback (void *opaque, int free)
844{
845 transfer_audio ((AC97LinkState*)opaque, PO_INDEX, free);
846}
847
848/**
849 * Port I/O Handler for IN operations.
850 *
851 * @returns VBox status code.
852 *
853 * @param pDevIns The device instance.
854 * @param pvUser User argument.
855 * @param uPort Port number used for the IN operation.
856 * @param pu32 Where to store the result.
857 * @param cb Number of bytes read.
858 */
859static DECLCALLBACK(int) ichac97IOPortNABMRead (PPDMDEVINS pDevIns, void *pvUser,
860 RTIOPORT Port, uint32_t *pu32, unsigned cb)
861{
862 PCIAC97LinkState *d = (PCIAC97LinkState*)pvUser;
863 AC97LinkState *s = &d->ac97;
864
865 switch (cb)
866 {
867 case 1:
868 {
869 AC97BusMasterRegs *r = NULL;
870 uint32_t index = Port - d->ac97.IOPortBase[1];
871 *pu32 = ~0U;
872
873 switch (index)
874 {
875 case CAS:
876 /* Codec Access Semaphore Register */
877 Log (("ac97: CAS %d\n", s->cas));
878 *pu32 = s->cas;
879 s->cas = 1;
880 break;
881 case PI_CIV:
882 case PO_CIV:
883 case MC_CIV:
884 /* Current Index Value Register */
885 r = &s->bm_regs[GET_BM (index)];
886 *pu32 = r->civ;
887 Log (("ac97: CIV[%d] -> %#x\n", GET_BM (index), *pu32));
888 break;
889 case PI_LVI:
890 case PO_LVI:
891 case MC_LVI:
892 /* Last Valid Index Register */
893 r = &s->bm_regs[GET_BM (index)];
894 *pu32 = r->lvi;
895 Log (("ac97: LVI[%d] -> %#x\n", GET_BM (index), *pu32));
896 break;
897 case PI_PIV:
898 case PO_PIV:
899 case MC_PIV:
900 /* Prefetched Index Value Register */
901 r = &s->bm_regs[GET_BM (index)];
902 *pu32 = r->piv;
903 Log (("ac97: PIV[%d] -> %#x\n", GET_BM (index), *pu32));
904 break;
905 case PI_CR:
906 case PO_CR:
907 case MC_CR:
908 /* Control Register */
909 r = &s->bm_regs[GET_BM (index)];
910 *pu32 = r->cr;
911 Log (("ac97: CR[%d] -> %#x\n", GET_BM (index), *pu32));
912 break;
913 case PI_SR:
914 case PO_SR:
915 case MC_SR:
916 /* Status Register (lower part) */
917 r = &s->bm_regs[GET_BM (index)];
918 *pu32 = r->sr & 0xff;
919 Log (("ac97: SRb[%d] -> %#x\n", GET_BM (index), *pu32));
920 break;
921 default:
922 Log (("ac97: U nabm readb %#x -> %#x\n", Port, *pu32));
923 break;
924 }
925 break;
926 }
927
928 case 2:
929 {
930 AC97BusMasterRegs *r = NULL;
931 uint32_t index = Port - d->ac97.IOPortBase[1];
932 *pu32 = ~0U;
933
934 switch (index)
935 {
936 case PI_SR:
937 case PO_SR:
938 case MC_SR:
939 /* Status Register */
940 r = &s->bm_regs[GET_BM (index)];
941 *pu32 = r->sr;
942 Log (("ac97: SR[%d] -> %#x\n", GET_BM (index), *pu32));
943 break;
944 case PI_PICB:
945 case PO_PICB:
946 case MC_PICB:
947 /* Position in Current Buffer Register */
948 r = &s->bm_regs[GET_BM (index)];
949 *pu32 = r->picb;
950 Log (("ac97: PICB[%d] -> %#x\n", GET_BM (index), *pu32));
951 break;
952 default:
953 Log (("ac97: U nabm readw %#x -> %#x\n", Port, *pu32));
954 break;
955 }
956 break;
957 }
958
959 case 4:
960 {
961 AC97BusMasterRegs *r = NULL;
962 uint32_t index = Port - d->ac97.IOPortBase[1];
963 *pu32 = ~0U;
964
965 switch (index)
966 {
967 case PI_BDBAR:
968 case PO_BDBAR:
969 case MC_BDBAR:
970 /* Buffer Descriptor Base Address Register */
971 r = &s->bm_regs[GET_BM (index)];
972 *pu32 = r->bdbar;
973 Log (("ac97: BMADDR[%d] -> %#x\n", GET_BM (index), *pu32));
974 break;
975 case PI_CIV:
976 case PO_CIV:
977 case MC_CIV:
978 /* 32-bit access: Current Index Value Register +
979 * Last Valid Index Register +
980 * Status Register */
981 r = &s->bm_regs[GET_BM (index)];
982 *pu32 = r->civ | (r->lvi << 8) | (r->sr << 16);
983 Log (("ac97: CIV LVI SR[%d] -> %#x, %#x, %#x\n", GET_BM (index),
984 r->civ, r->lvi, r->sr));
985 break;
986 case PI_PICB:
987 case PO_PICB:
988 case MC_PICB:
989 /* 32-bit access: Position in Current Buffer Register +
990 * Prefetched Index Value Register +
991 * Control Register */
992 r = &s->bm_regs[GET_BM (index)];
993 *pu32 = r->picb | (r->piv << 16) | (r->cr << 24);
994 Log (("ac97: PICB PIV CR[%d] -> %#x %#x %#x %#x\n", GET_BM (index),
995 *pu32, r->picb, r->piv, r->cr));
996 break;
997 case GLOB_CNT:
998 /* Global Control */
999 *pu32 = s->glob_cnt;
1000 Log (("ac97: glob_cnt -> %#x\n", *pu32));
1001 break;
1002 case GLOB_STA:
1003 /* Global Status */
1004 *pu32 = s->glob_sta | GS_S0CR;
1005 Log (("ac97: glob_sta -> %#x\n", *pu32));
1006 break;
1007 default:
1008 Log (("ac97: U nabm readl %#x -> %#x\n", Port, *pu32));
1009 break;
1010 }
1011 break;
1012 }
1013
1014 default:
1015 return VERR_IOM_IOPORT_UNUSED;
1016 }
1017 return VINF_SUCCESS;
1018}
1019
1020/**
1021 * Port I/O Handler for OUT operations.
1022 *
1023 * @returns VBox status code.
1024 *
1025 * @param pDevIns The device instance.
1026 * @param pvUser User argument.
1027 * @param uPort Port number used for the IN operation.
1028 * @param u32 The value to output.
1029 * @param cb The value size in bytes.
1030 */
1031static DECLCALLBACK(int) ichac97IOPortNABMWrite (PPDMDEVINS pDevIns, void *pvUser,
1032 RTIOPORT Port, uint32_t u32, unsigned cb)
1033{
1034 PCIAC97LinkState *d = (PCIAC97LinkState*)pvUser;
1035 AC97LinkState *s = &d->ac97;
1036
1037 switch (cb)
1038 {
1039 case 1:
1040 {
1041 AC97BusMasterRegs *r = NULL;
1042 uint32_t index = Port - d->ac97.IOPortBase[1];
1043 switch (index)
1044 {
1045 case PI_LVI:
1046 case PO_LVI:
1047 case MC_LVI:
1048 /* Last Valid Index */
1049 r = &s->bm_regs[GET_BM (index)];
1050 if ((r->cr & CR_RPBM) && (r->sr & SR_DCH)) {
1051 r->sr &= ~(SR_DCH | SR_CELV);
1052 r->civ = r->piv;
1053 r->piv = (r->piv + 1) % 32;
1054 fetch_bd (s, r);
1055 }
1056 r->lvi = u32 % 32;
1057 Log (("ac97: LVI[%d] <- %#x\n", GET_BM (index), u32));
1058 break;
1059 case PI_CR:
1060 case PO_CR:
1061 case MC_CR:
1062 /* Control Register */
1063 r = &s->bm_regs[GET_BM (index)];
1064 if (u32 & CR_RR)
1065 reset_bm_regs (s, r);
1066 else
1067 {
1068 r->cr = u32 & CR_VALID_MASK;
1069 if (!(r->cr & CR_RPBM))
1070 {
1071 voice_set_active (s, r - s->bm_regs, 0);
1072 r->sr |= SR_DCH;
1073 }
1074 else
1075 {
1076 r->civ = r->piv;
1077 r->piv = (r->piv + 1) % 32;
1078 fetch_bd (s, r);
1079 r->sr &= ~SR_DCH;
1080 voice_set_active (s, r - s->bm_regs, 1);
1081 }
1082 }
1083 Log (("ac97: CR[%d] <- %#x (cr %#x)\n", GET_BM (index), u32, r->cr));
1084 break;
1085 case PI_SR:
1086 case PO_SR:
1087 case MC_SR:
1088 /* Status Register */
1089 r = &s->bm_regs[GET_BM (index)];
1090 r->sr |= u32 & ~(SR_RO_MASK | SR_WCLEAR_MASK);
1091 update_sr (s, r, r->sr & ~(u32 & SR_WCLEAR_MASK));
1092 Log (("ac97: SR[%d] <- %#x (sr %#x)\n", GET_BM (index), u32, r->sr));
1093 break;
1094 default:
1095 Log (("ac97: U nabm writeb %#x <- %#x\n", Port, u32));
1096 break;
1097 }
1098 break;
1099 }
1100
1101 case 2:
1102 {
1103 AC97BusMasterRegs *r = NULL;
1104 uint32_t index = Port - d->ac97.IOPortBase[1];
1105 switch (index)
1106 {
1107 case PI_SR:
1108 case PO_SR:
1109 case MC_SR:
1110 /* Status Register */
1111 r = &s->bm_regs[GET_BM (index)];
1112 r->sr |= u32 & ~(SR_RO_MASK | SR_WCLEAR_MASK);
1113 update_sr (s, r, r->sr & ~(u32 & SR_WCLEAR_MASK));
1114 Log (("ac97: SR[%d] <- %#x (sr %#x)\n", GET_BM (index), u32, r->sr));
1115 break;
1116 default:
1117 Log (("ac97: U nabm writew %#x <- %#x\n", Port, u32));
1118 break;
1119 }
1120 break;
1121 }
1122
1123 case 4:
1124 {
1125 AC97BusMasterRegs *r = NULL;
1126 uint32_t index = Port - d->ac97.IOPortBase[1];
1127 switch (index)
1128 {
1129 case PI_BDBAR:
1130 case PO_BDBAR:
1131 case MC_BDBAR:
1132 /* Buffer Descriptor list Base Address Register */
1133 r = &s->bm_regs[GET_BM (index)];
1134 r->bdbar = u32 & ~3;
1135 Log (("ac97: BDBAR[%d] <- %#x (bdbar %#x)\n",
1136 GET_BM (index), u32, r->bdbar));
1137 break;
1138 case GLOB_CNT:
1139 /* Global Control */
1140 if (u32 & GC_WR)
1141 warm_reset (s);
1142 if (u32 & GC_CR)
1143 cold_reset (s);
1144 if (!(u32 & (GC_WR | GC_CR)))
1145 s->glob_cnt = u32 & GC_VALID_MASK;
1146 Log (("ac97: glob_cnt <- %#x (glob_cnt %#x)\n", u32, s->glob_cnt));
1147 break;
1148 case GLOB_STA:
1149 /* Global Status */
1150 s->glob_sta &= ~(u32 & GS_WCLEAR_MASK);
1151 s->glob_sta |= (u32 & ~(GS_WCLEAR_MASK | GS_RO_MASK)) & GS_VALID_MASK;
1152 Log (("ac97: glob_sta <- %#x (glob_sta %#x)\n", u32, s->glob_sta));
1153 break;
1154 default:
1155 Log (("ac97: U nabm writel %#x <- %#x\n", Port, u32));
1156 break;
1157 }
1158 break;
1159 }
1160
1161 default:
1162 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1163 break;
1164 }
1165 return VINF_SUCCESS;
1166}
1167
1168/**
1169 * Port I/O Handler for IN operations.
1170 *
1171 * @returns VBox status code.
1172 *
1173 * @param pDevIns The device instance.
1174 * @param pvUser User argument.
1175 * @param uPort Port number used for the IN operation.
1176 * @param pu32 Where to store the result.
1177 * @param cb Number of bytes read.
1178 */
1179static DECLCALLBACK(int) ichac97IOPortNAMRead (PPDMDEVINS pDevIns, void *pvUser,
1180 RTIOPORT Port, uint32_t *pu32, unsigned cb)
1181{
1182 PCIAC97LinkState *d = (PCIAC97LinkState*)pvUser;
1183 AC97LinkState *s = &d->ac97;
1184
1185 switch (cb)
1186 {
1187 case 1:
1188 {
1189 Log (("ac97: U nam readb %#x\n", Port));
1190 s->cas = 0;
1191 *pu32 = ~0U;
1192 break;
1193 }
1194
1195 case 2:
1196 {
1197 uint32_t index = Port - d->ac97.IOPortBase[0];
1198 *pu32 = ~0U;
1199 s->cas = 0;
1200 switch (index)
1201 {
1202 default:
1203 *pu32 = mixer_load (s, index);
1204 Log (("ac97: nam readw %#x -> %#x\n", Port, *pu32));
1205 break;
1206 }
1207 break;
1208 }
1209
1210 case 4:
1211 {
1212 Log (("ac97: U nam readl %#x\n", Port));
1213 s->cas = 0;
1214 *pu32 = ~0U;
1215 break;
1216 }
1217
1218 default:
1219 return VERR_IOM_IOPORT_UNUSED;
1220 }
1221 return VINF_SUCCESS;
1222}
1223
1224/**
1225 * Port I/O Handler for OUT operations.
1226 *
1227 * @returns VBox status code.
1228 *
1229 * @param pDevIns The device instance.
1230 * @param pvUser User argument.
1231 * @param uPort Port number used for the IN operation.
1232 * @param u32 The value to output.
1233 * @param cb The value size in bytes.
1234 */
1235static DECLCALLBACK(int) ichac97IOPortNAMWrite (PPDMDEVINS pDevIns, void *pvUser,
1236 RTIOPORT Port, uint32_t u32, unsigned cb)
1237{
1238 PCIAC97LinkState *d = (PCIAC97LinkState*)pvUser;
1239 AC97LinkState *s = &d->ac97;
1240
1241 switch (cb)
1242 {
1243 case 1:
1244 {
1245 Log (("ac97: U nam writeb %#x <- %#x\n", Port, u32));
1246 s->cas = 0;
1247 break;
1248 }
1249
1250 case 2:
1251 {
1252 uint32_t index = Port - d->ac97.IOPortBase[0];
1253 s->cas = 0;
1254 switch (index)
1255 {
1256 case AC97_Reset:
1257 mixer_reset (s);
1258 break;
1259 case AC97_Powerdown_Ctrl_Stat:
1260 u32 &= ~0xf;
1261 u32 |= mixer_load (s, index) & 0xf;
1262 mixer_store (s, index, u32);
1263 break;
1264#ifdef USE_MIXER
1265 case AC97_Master_Volume_Mute:
1266 set_volume (s, index, AUD_MIXER_VOLUME, u32);
1267 break;
1268 case AC97_PCM_Out_Volume_Mute:
1269 set_volume (s, index, AUD_MIXER_PCM, u32);
1270 break;
1271 case AC97_Line_In_Volume_Mute:
1272 set_volume (s, index, AUD_MIXER_LINE_IN, u32);
1273 break;
1274 case AC97_Record_Select:
1275 record_select (s, u32);
1276 break;
1277#else
1278 case AC97_Master_Volume_Mute:
1279 case AC97_PCM_Out_Volume_Mute:
1280 case AC97_Line_In_Volume_Mute:
1281 case AC97_Record_Select:
1282 mixer_store (s, index, u32);
1283 break;
1284#endif
1285 case AC97_Vendor_ID1:
1286 case AC97_Vendor_ID2:
1287 Log (("ac97: Attempt to write vendor ID to %#x\n", u32));
1288 break;
1289 case AC97_Extended_Audio_ID:
1290 Log (("ac97: Attempt to write extended audio ID to %#x\n", u32));
1291 break;
1292 case AC97_Extended_Audio_Ctrl_Stat:
1293 if (!(u32 & EACS_VRA))
1294 {
1295 mixer_store (s, AC97_PCM_Front_DAC_Rate, 0xbb80);
1296 mixer_store (s, AC97_PCM_LR_ADC_Rate, 0xbb80);
1297 open_voice (s, PI_INDEX, 48000);
1298 open_voice (s, PO_INDEX, 48000);
1299 }
1300 if (!(u32 & EACS_VRM))
1301 {
1302 mixer_store (s, AC97_MIC_ADC_Rate, 0xbb80);
1303 open_voice (s, MC_INDEX, 48000);
1304 }
1305 Log (("ac97: Setting extended audio control to %#x\n", u32));
1306 mixer_store (s, AC97_Extended_Audio_Ctrl_Stat, u32);
1307 break;
1308 case AC97_PCM_Front_DAC_Rate:
1309 if (mixer_load (s, AC97_Extended_Audio_Ctrl_Stat) & EACS_VRA)
1310 {
1311 mixer_store (s, index, u32);
1312 Log(("ac97: Set front DAC rate to %d\n", u32));
1313 open_voice (s, PO_INDEX, u32);
1314 }
1315 else
1316 {
1317 Log (("ac97: Attempt to set front DAC rate to %d, "
1318 "but VRA is not set\n",
1319 u32));
1320 }
1321 break;
1322 case AC97_MIC_ADC_Rate:
1323 if (mixer_load (s, AC97_Extended_Audio_Ctrl_Stat) & EACS_VRM)
1324 {
1325 mixer_store (s, index, u32);
1326 Log (("ac97: Set MIC ADC rate to %d\n", u32));
1327 open_voice (s, MC_INDEX, u32);
1328 }
1329 else
1330 {
1331 Log (("ac97: Attempt to set MIC ADC rate to %d, "
1332 "but VRM is not set\n",
1333 u32));
1334 }
1335 break;
1336 case AC97_PCM_LR_ADC_Rate:
1337 if (mixer_load (s, AC97_Extended_Audio_Ctrl_Stat) & EACS_VRA)
1338 {
1339 mixer_store (s, index, u32);
1340 Log (("ac97: Set front LR ADC rate to %d\n", u32));
1341 open_voice (s, PI_INDEX, u32);
1342 }
1343 else
1344 {
1345 Log (("ac97: Attempt to set LR ADC rate to %d, but VRA is not set\n",
1346 u32));
1347 }
1348 break;
1349 default:
1350 Log (("ac97: U nam writew %#x <- %#x\n", Port, u32));
1351 mixer_store (s, index, u32);
1352 break;
1353 }
1354 break;
1355 }
1356
1357 case 4:
1358 {
1359 Log (("ac97: U nam writel %#x <- %#x\n", Port, u32));
1360 s->cas = 0;
1361 break;
1362 }
1363
1364 default:
1365 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1366 break;
1367 }
1368 return VINF_SUCCESS;
1369}
1370
1371/**
1372 * Callback function for mapping a PCI I/O region.
1373 *
1374 * @return VBox status code.
1375 * @param pPciDev Pointer to PCI device.
1376 * Use pPciDev->pDevIns to get the device instance.
1377 * @param iRegion The region number.
1378 * @param GCPhysAddress Physical address of the region.
1379 * If iType is PCI_ADDRESS_SPACE_IO, this is an
1380 * I/O port, else it's a physical address.
1381 * This address is *NOT* relative
1382 * to pci_mem_base like earlier!
1383 * @param enmType One of the PCI_ADDRESS_SPACE_* values.
1384 */
1385static DECLCALLBACK(int) ichac97IOPortMap (PPCIDEVICE pPciDev, int iRegion,
1386 RTGCPHYS GCPhysAddress, uint32_t cb,
1387 PCIADDRESSSPACE enmType)
1388{
1389 int rc;
1390 PPDMDEVINS pDevIns = pPciDev->pDevIns;
1391 RTIOPORT Port = (RTIOPORT)GCPhysAddress;
1392 PCIAC97LinkState *pData = PCIDEV_2_ICHAC97STATE(pPciDev);
1393
1394 Assert(enmType == PCI_ADDRESS_SPACE_IO);
1395 Assert(cb >= 0x20);
1396
1397 if (iRegion == 0)
1398 rc = PDMDevHlpIOPortRegister (pDevIns, Port, 256, pData,
1399 ichac97IOPortNAMWrite, ichac97IOPortNAMRead,
1400 NULL, NULL, "ICHAC97 NAM");
1401 else
1402 rc = PDMDevHlpIOPortRegister (pDevIns, Port, 64, pData,
1403 ichac97IOPortNABMWrite, ichac97IOPortNABMRead,
1404 NULL, NULL, "ICHAC97 NABM");
1405 if (VBOX_FAILURE(rc))
1406 return rc;
1407
1408 pData->ac97.IOPortBase[iRegion] = Port;
1409 return VINF_SUCCESS;
1410}
1411
1412/**
1413 * Saves a state of the AC'97 device.
1414 *
1415 * @returns VBox status code.
1416 * @param pDevIns The device instance.
1417 * @param pSSMHandle The handle to save the state to.
1418 */
1419static DECLCALLBACK(int) ichac97SaveExec (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1420{
1421 PCIAC97LinkState *pData = PDMINS2DATA(pDevIns, PCIAC97LinkState *);
1422 size_t i;
1423 uint8_t active[LAST_INDEX];
1424 AC97LinkState *s = &pData->ac97;
1425
1426 SSMR3PutU32 (pSSMHandle, s->glob_cnt);
1427 SSMR3PutU32 (pSSMHandle, s->glob_sta);
1428 SSMR3PutU32 (pSSMHandle, s->cas);
1429
1430 for (i = 0; i < sizeof (s->bm_regs) / sizeof (s->bm_regs[0]); ++i)
1431 {
1432 AC97BusMasterRegs *r = &s->bm_regs[i];
1433 SSMR3PutU32 (pSSMHandle, r->bdbar);
1434 SSMR3PutU8 (pSSMHandle, r->civ);
1435 SSMR3PutU8 (pSSMHandle, r->lvi);
1436 SSMR3PutU16 (pSSMHandle, r->sr);
1437 SSMR3PutU16 (pSSMHandle, r->picb);
1438 SSMR3PutU8 (pSSMHandle, r->piv);
1439 SSMR3PutU8 (pSSMHandle, r->cr);
1440 SSMR3PutS32 (pSSMHandle, r->bd_valid);
1441 SSMR3PutU32 (pSSMHandle, r->bd.addr);
1442 SSMR3PutU32 (pSSMHandle, r->bd.ctl_len);
1443 }
1444 SSMR3PutMem (pSSMHandle, s->mixer_data, sizeof (s->mixer_data));
1445
1446 active[PI_INDEX] = AUD_is_active_in (s->voice_pi) ? 1 : 0;
1447 active[PO_INDEX] = AUD_is_active_out (s->voice_po) ? 1 : 0;
1448 active[MC_INDEX] = AUD_is_active_in (s->voice_mc) ? 1 : 0;
1449 SSMR3PutMem (pSSMHandle, active, sizeof (active));
1450
1451 return VINF_SUCCESS;
1452}
1453
1454/**
1455 * Loads a saved AC'97 device state.
1456 *
1457 * @returns VBox status code.
1458 * @param pDevIns The device instance.
1459 * @param pSSMHandle The handle to the saved state.
1460 * @param u32Version The data unit version number.
1461 */
1462static DECLCALLBACK(int) ichac97LoadExec (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle,
1463 uint32_t u32Version)
1464{
1465 PCIAC97LinkState *pData = PDMINS2DATA(pDevIns, PCIAC97LinkState *);
1466 size_t i;
1467 uint8_t active[LAST_INDEX];
1468 AC97LinkState *s = &pData->ac97;
1469
1470 if (u32Version != AC97_SSM_VERSION)
1471 {
1472 AssertFailed();
1473 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1474 }
1475
1476 SSMR3GetU32 (pSSMHandle, &s->glob_cnt);
1477 SSMR3GetU32 (pSSMHandle, &s->glob_sta);
1478 SSMR3GetU32 (pSSMHandle, &s->cas);
1479
1480 for (i = 0; i < sizeof (s->bm_regs) / sizeof (s->bm_regs[0]); ++i)
1481 {
1482 AC97BusMasterRegs *r = &s->bm_regs[i];
1483 SSMR3GetU32 (pSSMHandle, &r->bdbar);
1484 SSMR3GetU8 (pSSMHandle, &r->civ);
1485 SSMR3GetU8 (pSSMHandle, &r->lvi);
1486 SSMR3GetU16 (pSSMHandle, &r->sr);
1487 SSMR3GetU16 (pSSMHandle, &r->picb);
1488 SSMR3GetU8 (pSSMHandle, &r->piv);
1489 SSMR3GetU8 (pSSMHandle, &r->cr);
1490 SSMR3GetS32 (pSSMHandle, &r->bd_valid);
1491 SSMR3GetU32 (pSSMHandle, &r->bd.addr);
1492 SSMR3GetU32 (pSSMHandle, &r->bd.ctl_len);
1493 }
1494 SSMR3GetMem (pSSMHandle, s->mixer_data, sizeof (s->mixer_data));
1495 SSMR3GetMem (pSSMHandle, active, sizeof (active));
1496
1497#ifdef USE_MIXER
1498 record_select (s, mixer_load (s, AC97_Record_Select));
1499#define V_(a, b) set_volume (s, a, b, mixer_load (s, a))
1500 V_ (AC97_Master_Volume_Mute, AUD_MIXER_VOLUME);
1501 V_ (AC97_PCM_Out_Volume_Mute, AUD_MIXER_PCM);
1502 V_ (AC97_Line_In_Volume_Mute, AUD_MIXER_LINE_IN);
1503#undef V_
1504#endif
1505 reset_voices (s, active);
1506
1507 s->bup_flag = 0;
1508 s->last_samp = 0;
1509
1510 return VINF_SUCCESS;
1511}
1512
1513/**
1514 * Reset notification.
1515 *
1516 * @returns VBox status.
1517 * @param pDevIns The device instance data.
1518 *
1519 * @remark The original sources didn't install a reset handler, but it seems to
1520 * make sense to me so we'll do it.
1521 */
1522static DECLCALLBACK(void) ac97Reset(PPDMDEVINS pDevIns)
1523{
1524 PCIAC97LinkState *pData = PDMINS2DATA(pDevIns, PCIAC97LinkState *);
1525
1526 /*
1527 * Reset the device state (will need pDrv later).
1528 */
1529 reset_bm_regs (&pData->ac97, &pData->ac97.bm_regs[0]);
1530 reset_bm_regs (&pData->ac97, &pData->ac97.bm_regs[1]);
1531 reset_bm_regs (&pData->ac97, &pData->ac97.bm_regs[2]);
1532
1533 /*
1534 * Reset the mixer too. The Windows XP driver seems to rely on
1535 * this. At least it wants to read the vendor id before it resets
1536 * the codec manually.
1537 */
1538 mixer_reset(&pData->ac97);
1539}
1540
1541/**
1542 * Queries an interface to the driver.
1543 *
1544 * @returns Pointer to interface.
1545 * @returns NULL if the interface was not supported by the driver.
1546 * @param pInterface Pointer to this interface structure.
1547 * @param enmInterface The requested interface identification.
1548 * @thread Any thread.
1549 */
1550static DECLCALLBACK(void *) ichac97QueryInterface (struct PDMIBASE *pInterface,
1551 PDMINTERFACE enmInterface)
1552{
1553 PCIAC97LinkState *pData =
1554 (PCIAC97LinkState *)((uintptr_t)pInterface
1555 - RT_OFFSETOF(PCIAC97LinkState, ac97.IBase));
1556 Assert(&pData->ac97.IBase == pInterface);
1557 switch (enmInterface)
1558 {
1559 case PDMINTERFACE_BASE:
1560 return &pData->ac97.IBase;
1561 default:
1562 return NULL;
1563 }
1564}
1565
1566/**
1567 * Construct a device instance for a VM.
1568 *
1569 * @returns VBox status.
1570 * @param pDevIns The device instance data.
1571 * If the registration structure is needed,
1572 * pDevIns->pDevReg points to it.
1573 * @param iInstance Instance number. Use this to figure out which
1574 * registers and such to use.
1575 * The device number is also found in pDevIns->iInstance,
1576 * but since it's likely to be freqently used PDM passes
1577 * it as parameter.
1578 * @param pCfgHandle Configuration node handle for the device.
1579 * Use this to obtain the configuration
1580 * of the device instance. It's also found in
1581 * pDevIns->pCfgHandle, but like iInstance it's expected
1582 * to be used a bit in this function.
1583 */
1584static DECLCALLBACK(int) ichac97Construct (PPDMDEVINS pDevIns, int iInstance,
1585 PCFGMNODE pCfgHandle)
1586{
1587 PCIAC97LinkState *pData = PDMINS2DATA(pDevIns, PCIAC97LinkState *);
1588 int rc;
1589
1590 Assert(iInstance == 0);
1591
1592 /*
1593 * Initialize data (most of it anyway).
1594 */
1595 pData->ac97.pDevIns = pDevIns;
1596 /* IBase */
1597 pData->ac97.IBase.pfnQueryInterface = ichac97QueryInterface;
1598
1599 /* PCI Device */
1600 pData->dev.config[0x00] = 0x86; /* vid vendor id intel ro */
1601 pData->dev.config[0x01] = 0x80; /* intel */
1602
1603 pData->dev.config[0x02] = 0x15; /* did device id 82801 ro */
1604 pData->dev.config[0x03] = 0x24; /* 82801aa */
1605
1606 pData->dev.config[0x04] = 0x00; /* pcicmd pci command rw, ro */
1607 pData->dev.config[0x05] = 0x00;
1608
1609 pData->dev.config[0x06] = 0x80; /* pcists pci status rwc, ro */
1610 pData->dev.config[0x07] = 0x02;
1611
1612 pData->dev.config[0x08] = 0x01; /* rid revision ro */
1613 pData->dev.config[0x09] = 0x00; /* pi programming interface ro */
1614 pData->dev.config[0x0a] = 0x01; /* scc sub class code ro */
1615 pData->dev.config[0x0b] = 0x04; /* bcc base class code ro */
1616 pData->dev.config[0x0e] = 0x00; /* headtyp header type ro */
1617
1618 pData->dev.config[0x10] = 0x01; /* nambar native audio mixer base
1619 * address rw */
1620 pData->dev.config[0x11] = 0x00;
1621 pData->dev.config[0x12] = 0x00;
1622 pData->dev.config[0x13] = 0x00;
1623
1624 pData->dev.config[0x14] = 0x01; /* nabmbar native audio bus mastering
1625 * base address rw */
1626 pData->dev.config[0x15] = 0x00;
1627 pData->dev.config[0x16] = 0x00;
1628 pData->dev.config[0x17] = 0x00;
1629
1630 pData->dev.config[0x2c] = 0x86; /* svid subsystem vendor id rwo */
1631 pData->dev.config[0x2d] = 0x80;
1632
1633 pData->dev.config[0x2e] = 0x00; /* sid subsystem id rwo */
1634 pData->dev.config[0x2f] = 0x00;
1635
1636 pData->dev.config[0x3c] = 0x00; /* intr_ln interrupt line rw */
1637 pData->dev.config[0x3d] = 0x01; /* intr_pn interrupt pin ro */
1638
1639 /*
1640 * Register the PCI device, it's I/O regions, the timer and the
1641 * saved state item.
1642 */
1643 rc = PDMDevHlpPCIRegister (pDevIns, &pData->dev);
1644 if (VBOX_FAILURE(rc))
1645 return rc;
1646
1647 rc = PDMDevHlpPCIIORegionRegister (pDevIns, 0, 256, PCI_ADDRESS_SPACE_IO,
1648 ichac97IOPortMap);
1649 if (VBOX_FAILURE(rc))
1650 return rc;
1651
1652 rc = PDMDevHlpPCIIORegionRegister (pDevIns, 1, 64, PCI_ADDRESS_SPACE_IO,
1653 ichac97IOPortMap);
1654 if (VBOX_FAILURE(rc))
1655 return rc;
1656
1657 rc = PDMDevHlpSSMRegister (pDevIns, pDevIns->pDevReg->szDeviceName,
1658 iInstance, AC97_SSM_VERSION, sizeof(*pData),
1659 NULL, ichac97SaveExec, NULL,
1660 NULL, ichac97LoadExec, NULL);
1661 if (VBOX_FAILURE(rc))
1662 return rc;
1663
1664 /*
1665 * Attach driver.
1666 */
1667 rc = PDMDevHlpDriverAttach (pDevIns, 0, &pData->ac97.IBase,
1668 &pData->ac97.pDrvBase, "Audio Driver Port");
1669 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1670 Log (("ac97: No attached driver!\n"));
1671 else if (VBOX_FAILURE(rc))
1672 {
1673 AssertMsgFailed(("Failed to attach AC97 LUN #0! rc=%Vrc\n", rc));
1674 return rc;
1675 }
1676
1677 AUD_register_card ("ICH0", &pData->ac97.card);
1678
1679 ac97Reset(pDevIns);
1680
1681 /**
1682 * @todo We should probably display a message box. And perhaps we should
1683 * select the noaudio driver instead.
1684 */
1685#ifndef __DARWIN__ /* coreaudio doesn't supply these. */
1686 if (!pData->ac97.voice_pi)
1687 LogRel(("AC97: WARNING: Unable to open PCM IN!\n"));
1688 if (!pData->ac97.voice_mc)
1689 LogRel(("AC97: WARNING: Unable to open PCM MC!\n"));
1690#endif
1691 if (!pData->ac97.voice_po)
1692 LogRel(("AC97: WARNING: Unable to open PCM OUT!\n"));
1693
1694#ifdef __DARWIN__ /* coreaudio doesn't supply all, only bitch if we don't get anything. */
1695 if (!pData->ac97.voice_pi && !pData->ac97.voice_po && !pData->ac97.voice_mc)
1696#else
1697 if (!pData->ac97.voice_pi || !pData->ac97.voice_po || !pData->ac97.voice_mc)
1698#endif
1699 VMSetRuntimeError(PDMDevHlpGetVM(pDevIns), false,
1700 "HostAudioNotResponding",
1701 N_("Some audio devices could not be opened. Guest applications "
1702 "generating audio output or depending on audio input may hang. "
1703 "Make sure your host audio device is working properly. Check "
1704 "the logfile for error messages of the audio subsystem."));
1705
1706 return VINF_SUCCESS;
1707}
1708
1709/**
1710 * The device registration structure.
1711 */
1712const PDMDEVREG g_DeviceICHAC97 =
1713{
1714 /* u32Version */
1715 PDM_DEVREG_VERSION,
1716 /* szDeviceName */
1717 "ichac97",
1718 /* szGCMod */
1719 "",
1720 /* szR0Mod */
1721 "",
1722 /* pszDescription */
1723 "ICH AC'97 Audio Controller",
1724 /* fFlags */
1725 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT,
1726 /* fClass */
1727 PDM_DEVREG_CLASS_AUDIO,
1728 /* cMaxInstances */
1729 1,
1730 /* cbInstance */
1731 sizeof(PCIAC97LinkState),
1732 /* pfnConstruct */
1733 ichac97Construct,
1734 /* pfnDestruct */
1735 NULL,
1736 /* pfnRelocate */
1737 NULL,
1738 /* pfnIOCtl */
1739 NULL,
1740 /* pfnPowerOn */
1741 NULL,
1742 /* pfnReset */
1743 ac97Reset,
1744 /* pfnSuspend */
1745 NULL,
1746 /* pfnResume */
1747 NULL,
1748 /* pfnAttach */
1749 NULL,
1750 /* pfnDetach */
1751 NULL,
1752 /* pfnQueryInterface. */
1753 NULL
1754};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette