Ignore:
Timestamp:
Mar 24, 2008, 2:43:42 AM (17 years ago)
Author:
Paul Smedley
Message:

Update source to ALSA 1.0.16 level

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GPL/branches/uniaud-2.0/alsa-kernel/pci/intel8x0.c

    r275 r305  
    22 *   ALSA driver for Intel ICH (i8x0) chipsets
    33 *
    4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
     4 *      Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
    55 *
    66 *
     
    2525
    2626 *
    27  */
    28 
    29 #include <sound/driver.h>
     27 */     
     28
    3029#include <asm/io.h>
    3130#include <linux/delay.h>
     
    3433#include <linux/pci.h>
    3534#include <linux/slab.h>
     35#include <linux/moduleparam.h>
    3636#include <sound/core.h>
    3737#include <sound/pcm.h>
    3838#include <sound/ac97_codec.h>
    3939#include <sound/info.h>
    40 #include <sound/mpu401.h>
    41 #define SNDRV_GET_ID
    4240#include <sound/initval.h>
    43 
    44 #define I810_DEBUG
    45 
    46 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
     41/* for 440MX workaround */
     42#include <asm/pgtable.h>
     43#ifndef TARGET_OS2
     44#include <asm/cacheflush.h>
     45#endif
     46
     47MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
    4748MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7012; Ali 5455");
    4849MODULE_LICENSE("GPL");
    49 MODULE_CLASSES("{sound}");
    50 MODULE_DEVICES("{{Intel,82801AA-ICH},"
    51                "{Intel,82901AB-ICH0},"
    52                "{Intel,82801BA-ICH2},"
    53                "{Intel,82801CA-ICH3},"
    54                "{Intel,82801DB-ICH4},"
    55                "{Intel,ICH5},"
    56                "{Intel,ICH6},"
    57                "{Intel,ICH7},"
    58                "{Intel,6300ESB},"
    59                "{Intel,ESB2},"
    60                "{Intel,MX440},"
    61                "{SiS,SI7012},"
    62                "{NVidia,nForce Audio},"
    63                "{NVidia,nForce2 Audio},"
    64                "{AMD,AMD768},"
    65                "{AMD,AMD8111},"
    66                "{ALI,M5455}}");
    67 
    68 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
    69 //#define SUPPORT_JOYSTICK 1
    70 #endif
    71 #define SUPPORT_MIDI 1
    72 
    73 extern int midi_port;
    74 
    75 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
    76 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
    77 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
    78 #ifdef TARGET_OS2
    79 static int ac97_clock[SNDRV_CARDS] = {REPEAT_SNDRV(0)};
    80 static char *ac97_quirk[SNDRV_CARDS];
    81 #else
    82 static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
    83 #endif
    84 #ifdef SUPPORT_JOYSTICK
    85 static int joystick[SNDRV_CARDS];
    86 #endif
    87 #ifdef SUPPORT_MIDI
    88 static int mpu_port[SNDRV_CARDS];// = {REPEAT_SNDRV(0x330)};;
    89 /* disabled */
    90 #endif
    91 
     50MODULE_SUPPORTED_DEVICE("{{Intel,82801AA-ICH},"
     51                "{Intel,82901AB-ICH0},"
     52                "{Intel,82801BA-ICH2},"
     53                "{Intel,82801CA-ICH3},"
     54                "{Intel,82801DB-ICH4},"
     55                "{Intel,ICH5},"
     56                "{Intel,ICH6},"
     57                "{Intel,ICH7},"
     58                "{Intel,6300ESB},"
     59                "{Intel,ESB2},"
     60                "{Intel,MX440},"
     61                "{SiS,SI7012},"
     62                "{NVidia,nForce Audio},"
     63                "{NVidia,nForce2 Audio},"
     64                "{AMD,AMD768},"
     65                "{AMD,AMD8111},"
     66                "{ALI,M5455}}");
     67
     68static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
     69static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
     70static int ac97_clock;
     71static char *ac97_quirk;
    9272static int buggy_semaphore;
    9373static int buggy_irq = -1; /* auto-check */
    94 
    95 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
     74static int xbox;
     75static int spdif_aclink = -1;
     76
     77module_param(index, int, 0444);
    9678MODULE_PARM_DESC(index, "Index value for Intel i8x0 soundcard.");
    97 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
    98 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
     79module_param(id, charp, 0444);
    9980MODULE_PARM_DESC(id, "ID string for Intel i8x0 soundcard.");
    100 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
    101 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
    102 MODULE_PARM_DESC(enable, "Enable Intel i8x0 soundcard.");
    103 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
    104 MODULE_PARM(ac97_clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
     81module_param(ac97_clock, int, 0444);
    10582MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
    106 MODULE_PARM_SYNTAX(ac97_clock, SNDRV_ENABLED ",default:0");
    107 MODULE_PARM(ac97_quirk, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
     83module_param(ac97_quirk, charp, 0444);
    10884MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
    109 MODULE_PARM_SYNTAX(ac97_quirk, SNDRV_ENABLED ",allows:{{-1,3}},dialog:list,default:-1");
    110 #ifdef SUPPORT_JOYSTICK
    111 MODULE_PARM(joystick, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
    112 MODULE_PARM_DESC(joystick, "Enable joystick for Intel i8x0 soundcard.");
    113 MODULE_PARM_SYNTAX(joystick, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
    114 #endif
    115 #ifdef SUPPORT_MIDI
    116 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
    117 MODULE_PARM_DESC(mpu_port, "MPU401 port # for Intel i8x0 driver.");
    118 MODULE_PARM_SYNTAX(mpu_port, SNDRV_ENABLED ",allows:{{0},{0x330},{0x300}},dialog:list");
    119 #endif
     85module_param(buggy_semaphore, bool, 0444);
     86MODULE_PARM_DESC(buggy_semaphore, "Enable workaround for hardwares with problematic codec semaphores.");
     87module_param(buggy_irq, bool, 0444);
     88MODULE_PARM_DESC(buggy_irq, "Enable workaround for buggy interrupts on some motherboards.");
     89module_param(xbox, bool, 0444);
     90MODULE_PARM_DESC(xbox, "Set to 1 for Xbox, if you have problems with the AC'97 codec detection.");
     91module_param(spdif_aclink, int, 0444);
     92MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
     93
     94/* just for backward compatibility */
     95static int enable;
     96module_param(enable, bool, 0444);
     97static int joystick;
     98module_param(joystick, int, 0444);
    12099
    121100/*
    122101 *  Direct registers
    123102 */
    124 
    125 #ifndef PCI_DEVICE_ID_INTEL_82801
    126 #define PCI_DEVICE_ID_INTEL_82801       0x2415
    127 #endif
    128 #ifndef PCI_DEVICE_ID_INTEL_82901
    129 #define PCI_DEVICE_ID_INTEL_82901       0x2425
    130 #endif
    131 #ifndef PCI_DEVICE_ID_INTEL_82801BA
    132 #define PCI_DEVICE_ID_INTEL_82801BA     0x2445
    133 #endif
    134 #ifndef PCI_DEVICE_ID_INTEL_440MX
    135 #define PCI_DEVICE_ID_INTEL_440MX       0x7195
    136 #endif
    137 #ifndef PCI_DEVICE_ID_INTEL_ICH3
    138 #define PCI_DEVICE_ID_INTEL_ICH3        0x2485
    139 #endif
    140 #ifndef PCI_DEVICE_ID_INTEL_ICH4
    141 #define PCI_DEVICE_ID_INTEL_ICH4        0x24c5
    142 #endif
    143 #ifndef PCI_DEVICE_ID_INTEL_ICH5
    144 #define PCI_DEVICE_ID_INTEL_ICH5        0x24d5
    145 #endif
    146 #ifndef PCI_DEVICE_ID_INTEL_ESB_5
    147 #define PCI_DEVICE_ID_INTEL_ESB_5       0x25a6
    148 #endif
    149 #ifndef PCI_DEVICE_ID_INTEL_ICH6_18
    150 #define PCI_DEVICE_ID_INTEL_ICH6_18     0x266e
    151 #endif
    152 #ifndef PCI_DEVICE_ID_INTEL_ICH7_20
    153 #define PCI_DEVICE_ID_INTEL_ICH7_20     0x27de
    154 #endif
    155 #ifndef PCI_DEVICE_ID_INTEL_ESB2_14
    156 #define PCI_DEVICE_ID_INTEL_ESB2_14     0x2698
    157 #endif
    158 #ifndef PCI_DEVICE_ID_SI_7012
    159 #define PCI_DEVICE_ID_SI_7012           0x7012
    160 #endif
    161 #ifndef PCI_DEVICE_ID_NVIDIA_CK804_AUDIO
    162 #define PCI_DEVICE_ID_NVIDIA_CK804_AUDIO 0x0059
    163 #endif
    164 #ifndef PCI_DEVICE_ID_NVIDIA_MCP_AUDIO
    165 #define PCI_DEVICE_ID_NVIDIA_MCP_AUDIO  0x01b1
    166 #endif
    167 #ifndef PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO
    168 #define PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO 0x006a
    169 #endif
    170 #ifndef PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO
    171 #define PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO 0x00da
    172 #endif
    173 #ifndef PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO
    174 #define PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO 0x00ea
    175 #endif
    176 #ifndef PCI_DEVICE_ID_NVIDIA_CK8_AUDIO
    177 #define PCI_DEVICE_ID_NVIDIA_CK8_AUDIO  0x008a
    178 #endif
    179 
    180 enum { DEVICE_INTEL, DEVICE_INTEL_ICH4, DEVICE_INTEL_ICH5, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
     103enum { DEVICE_INTEL, DEVICE_INTEL_ICH4, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
    181104
    182105#define ICHREG(x) ICH_REG_##x
    183106
    184107#define DEFINE_REGSET(name,base) \
    185     enum { \
    186     ICH_REG_##name##_BDBAR      = base + 0x0,   /* dword - buffer descriptor list base address */ \
    187     ICH_REG_##name##_CIV        = base + 0x04,  /* byte - current index value */ \
    188     ICH_REG_##name##_LVI        = base + 0x05,  /* byte - last valid index */ \
    189     ICH_REG_##name##_SR = base + 0x06,  /* byte - status register */ \
    190     ICH_REG_##name##_PICB       = base + 0x08,  /* word - position in current buffer */ \
    191     ICH_REG_##name##_PIV        = base + 0x0a,  /* byte - prefetched index value */ \
    192     ICH_REG_##name##_CR = base + 0x0b,  /* byte - control register */ \
    193     };
     108enum { \
     109        ICH_REG_##name##_BDBAR  = base + 0x0,   /* dword - buffer descriptor list base address */ \
     110        ICH_REG_##name##_CIV    = base + 0x04,  /* byte - current index value */ \
     111        ICH_REG_##name##_LVI    = base + 0x05,  /* byte - last valid index */ \
     112        ICH_REG_##name##_SR     = base + 0x06,  /* byte - status register */ \
     113        ICH_REG_##name##_PICB   = base + 0x08,  /* word - position in current buffer */ \
     114        ICH_REG_##name##_PIV    = base + 0x0a,  /* byte - prefetched index value */ \
     115        ICH_REG_##name##_CR     = base + 0x0b,  /* byte - control register */ \
     116};
    194117
    195118/* busmaster blocks */
     
    229152/* global block */
    230153#define ICH_REG_GLOB_CNT                0x2c    /* dword - global control */
    231 #define   ICH_PCM_SPDIF_MASK    0xc0000000      /* s/pdif pcm slot mask (ICH4) */
    232 #define   ICH_PCM_SPDIF_NONE    0x00000000      /* reserved - undefined */
    233 #define   ICH_PCM_SPDIF_78      0x40000000      /* s/pdif pcm on slots 7&8 */
    234 #define   ICH_PCM_SPDIF_69      0x80000000      /* s/pdif pcm on slots 6&9 */
    235 #define   ICH_PCM_SPDIF_1011    0xc0000000      /* s/pdif pcm on slots 10&11 */
     154#define   ICH_PCM_SPDIF_MASK    0xc0000000      /* s/pdif pcm slot mask (ICH4) */
     155#define   ICH_PCM_SPDIF_NONE    0x00000000      /* reserved - undefined */
     156#define   ICH_PCM_SPDIF_78      0x40000000      /* s/pdif pcm on slots 7&8 */
     157#define   ICH_PCM_SPDIF_69      0x80000000      /* s/pdif pcm on slots 6&9 */
     158#define   ICH_PCM_SPDIF_1011    0xc0000000      /* s/pdif pcm on slots 10&11 */
    236159#define   ICH_PCM_20BIT         0x00400000      /* 20-bit samples (ICH4) */
    237160#define   ICH_PCM_246_MASK      0x00300000      /* 6 channels (not all chips) */
     
    250173#define   ICH_AC97COLD          0x00000002      /* AC'97 cold reset */
    251174#define   ICH_GIE               0x00000001      /* GPI interrupt enable */
    252 #define   ICH_REG_GLOB_STA      0x30    /* dword - global status */
     175#define ICH_REG_GLOB_STA                0x30    /* dword - global status */
    253176#define   ICH_TRI               0x20000000      /* ICH4: tertiary (AC_SDIN2) resume interrupt */
    254177#define   ICH_TCR               0x10000000      /* ICH4: tertiary (AC_SDIN2) codec ready */
     
    258181#define   ICH_M2INT             0x01000000      /* ICH4: Mic2-In interrupt */
    259182#define   ICH_SAMPLE_CAP        0x00c00000      /* ICH4: sample capability bits (RO) */
    260 #define   ICH_SAMPLE_16_20      0x00400000      /* ICH4: 16- and 20-bit samples */
     183#define   ICH_SAMPLE_16_20      0x00400000      /* ICH4: 16- and 20-bit samples */
    261184#define   ICH_MULTICHAN_CAP     0x00300000      /* ICH4: multi-channel capability bits (RO) */
    262 #define   ICH_SIS_TRI           0x00080000      /* SIS: tertiary resume irq */
    263 #define   ICH_SIS_TCR           0x00040000      /* SIS: tertiary codec ready */
     185#define   ICH_SIS_TRI           0x00080000      /* SIS: tertiary resume irq */
     186#define   ICH_SIS_TCR           0x00040000      /* SIS: tertiary codec ready */
    264187#define   ICH_MD3               0x00020000      /* modem power down semaphore */
    265188#define   ICH_AD3               0x00010000      /* audio power down semaphore */
     
    279202#define   ICH_MIINT             0x00000002      /* modem capture interrupt */
    280203#define   ICH_GSCI              0x00000001      /* GPI status change interrupt */
    281 #define   ICH_REG_ACC_SEMA      0x34    /* byte - codec write semaphore */
     204#define ICH_REG_ACC_SEMA                0x34    /* byte - codec write semaphore */
    282205#define   ICH_CAS               0x01            /* codec access semaphore */
    283206#define ICH_REG_SDM             0x80
     
    289212#define   ICH_LDI_MASK          0x00000003      /* last codec read data input */
    290213
    291 #define   ICH_MAX_FRAGS         32              /* max hw frags */
     214#define ICH_MAX_FRAGS           32              /* max hw frags */
    292215
    293216
     
    310233
    311234enum {
    312     ICH_REG_ALI_SCR = 0x00,             /* System Control Register */
    313     ICH_REG_ALI_SSR = 0x04,             /* System Status Register  */
    314     ICH_REG_ALI_DMACR = 0x08,   /* DMA Control Register    */
    315     ICH_REG_ALI_FIFOCR1 = 0x0c, /* FIFO Control Register 1  */
    316     ICH_REG_ALI_INTERFACECR = 0x10,     /* Interface Control Register */
    317     ICH_REG_ALI_INTERRUPTCR = 0x14,     /* Interrupt control Register */
    318     ICH_REG_ALI_INTERRUPTSR = 0x18,     /* Interrupt  Status Register */
    319     ICH_REG_ALI_FIFOCR2 = 0x1c, /* FIFO Control Register 2   */
    320     ICH_REG_ALI_CPR = 0x20,             /* Command Port Register     */
    321     ICH_REG_ALI_CPR_ADDR = 0x22,        /* ac97 addr write */
    322     ICH_REG_ALI_SPR = 0x24,             /* Status Port Register      */
    323     ICH_REG_ALI_SPR_ADDR = 0x26,        /* ac97 addr read */
    324     ICH_REG_ALI_FIFOCR3 = 0x2c, /* FIFO Control Register 3  */
    325     ICH_REG_ALI_TTSR = 0x30,    /* Transmit Tag Slot Register */
    326     ICH_REG_ALI_RTSR = 0x34,    /* Receive Tag Slot  Register */
    327     ICH_REG_ALI_CSPSR = 0x38,   /* Command/Status Port Status Register */
    328     ICH_REG_ALI_CAS = 0x3c,             /* Codec Write Semaphore Register */
    329     ICH_REG_ALI_HWVOL = 0xf0,   /* hardware volume control/status */
    330     ICH_REG_ALI_I2SCR = 0xf4,   /* I2S control/status */
    331     ICH_REG_ALI_SPDIFCSR = 0xf8,        /* spdif channel status register  */
    332     ICH_REG_ALI_SPDIFICS = 0xfc,        /* spdif interface control/status  */
     235        ICH_REG_ALI_SCR = 0x00,         /* System Control Register */
     236        ICH_REG_ALI_SSR = 0x04,         /* System Status Register  */
     237        ICH_REG_ALI_DMACR = 0x08,       /* DMA Control Register    */
     238        ICH_REG_ALI_FIFOCR1 = 0x0c,     /* FIFO Control Register 1  */
     239        ICH_REG_ALI_INTERFACECR = 0x10, /* Interface Control Register */
     240        ICH_REG_ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
     241        ICH_REG_ALI_INTERRUPTSR = 0x18, /* Interrupt  Status Register */
     242        ICH_REG_ALI_FIFOCR2 = 0x1c,     /* FIFO Control Register 2   */
     243        ICH_REG_ALI_CPR = 0x20,         /* Command Port Register     */
     244        ICH_REG_ALI_CPR_ADDR = 0x22,    /* ac97 addr write */
     245        ICH_REG_ALI_SPR = 0x24,         /* Status Port Register      */
     246        ICH_REG_ALI_SPR_ADDR = 0x26,    /* ac97 addr read */
     247        ICH_REG_ALI_FIFOCR3 = 0x2c,     /* FIFO Control Register 3  */
     248        ICH_REG_ALI_TTSR = 0x30,        /* Transmit Tag Slot Register */
     249        ICH_REG_ALI_RTSR = 0x34,        /* Receive Tag Slot  Register */
     250        ICH_REG_ALI_CSPSR = 0x38,       /* Command/Status Port Status Register */
     251        ICH_REG_ALI_CAS = 0x3c,         /* Codec Write Semaphore Register */
     252        ICH_REG_ALI_HWVOL = 0xf0,       /* hardware volume control/status */
     253        ICH_REG_ALI_I2SCR = 0xf4,       /* I2S control/status */
     254        ICH_REG_ALI_SPDIFCSR = 0xf8,    /* spdif channel status register  */
     255        ICH_REG_ALI_SPDIFICS = 0xfc,    /* spdif interface control/status  */
    333256};
    334257
     
    341264
    342265/* interrupts for the whole chip by interrupt status register finish */
    343 
     266 
    344267#define ALI_INT_MICIN2          (1<<26)
    345268#define ALI_INT_PCMIN2          (1<<25)
     
    356279#define ALI_INT_SPRAIS          (1<<5)  /* status port available */
    357280#define ALI_INT_GPIO            (1<<1)
    358 #define ALI_INT_MASK            (ALI_INT_SPDIFOUT|ALI_INT_CODECSPDIFOUT|ALI_INT_MICIN|ALI_INT_PCMOUT|ALI_INT_PCMIN)
     281#define ALI_INT_MASK            (ALI_INT_SPDIFOUT|ALI_INT_CODECSPDIFOUT|\
     282                                 ALI_INT_MICIN|ALI_INT_PCMOUT|ALI_INT_PCMIN)
    359283
    360284#define ICH_ALI_SC_RESET        (1<<31) /* master reset */
     
    384308
    385309/*
    386  *
     310 * 
    387311 */
    388312
    389 enum { ICHD_PCMIN, ICHD_PCMOUT, ICHD_MIC, ICHD_MIC2, ICHD_PCM2IN, ICHD_SPBAR, ICHD_LAST = ICHD_SPBAR };
    390 enum { NVD_PCMIN, NVD_PCMOUT, NVD_MIC, NVD_SPBAR, NVD_LAST = NVD_SPBAR };
    391 enum { ALID_PCMIN, ALID_PCMOUT, ALID_MIC, ALID_AC97SPDIFOUT, ALID_SPDIFIN, ALID_SPDIFOUT, ALID_LAST = ALID_SPDIFOUT };
    392 
    393 #define get_ichdev(substream) (struct ichdev *)(substream->runtime->private_data)
    394 
    395 typedef struct ichdev {
    396     unsigned int ichd;                  /* ich device number */
    397     unsigned long reg_offset;           /* offset to bmaddr */
    398     u32 *bdbar;                         /* CPU address (32bit) */
    399     unsigned int bdbar_addr;            /* PCI bus address (32bit) */
    400     snd_pcm_substream_t *substream;
    401     unsigned int physbuf;                       /* physical address (32bit) */
    402     unsigned int size;
    403     unsigned int fragsize;
    404     unsigned int fragsize1;
    405     unsigned int position;
    406     unsigned int pos_shift;
    407     int frags;
    408     int lvi;
    409     int lvi_frag;
    410     int civ;
    411     int ack;
    412     int ack_reload;
    413     unsigned int ack_bit;
    414     unsigned int roff_sr;
    415     unsigned int roff_picb;
    416     unsigned int int_sta_mask;          /* interrupt status mask */
    417     unsigned int ali_slot;                      /* ALI DMA slot */
    418     struct ac97_pcm *pcm;
    419     int pcm_open_flag;
    420     unsigned int page_attr_changed: 1;
    421     unsigned int suspended: 1;
     313enum {
     314        ICHD_PCMIN,
     315        ICHD_PCMOUT,
     316        ICHD_MIC,
     317        ICHD_MIC2,
     318        ICHD_PCM2IN,
     319        ICHD_SPBAR,
     320        ICHD_LAST = ICHD_SPBAR
     321};
     322enum {
     323        NVD_PCMIN,
     324        NVD_PCMOUT,
     325        NVD_MIC,
     326        NVD_SPBAR,
     327        NVD_LAST = NVD_SPBAR
     328};
     329enum {
     330        ALID_PCMIN,
     331        ALID_PCMOUT,
     332        ALID_MIC,
     333        ALID_AC97SPDIFOUT,
     334        ALID_SPDIFIN,
     335        ALID_SPDIFOUT,
     336        ALID_LAST = ALID_SPDIFOUT
     337};
     338
     339#define get_ichdev(substream) (substream->runtime->private_data)
     340
     341struct ichdev {
     342        unsigned int ichd;                      /* ich device number */
     343        unsigned long reg_offset;               /* offset to bmaddr */
     344        u32 *bdbar;                             /* CPU address (32bit) */
     345        unsigned int bdbar_addr;                /* PCI bus address (32bit) */
     346        struct snd_pcm_substream *substream;
     347        unsigned int physbuf;                   /* physical address (32bit) */
     348        unsigned int size;
     349        unsigned int fragsize;
     350        unsigned int fragsize1;
     351        unsigned int position;
     352        unsigned int pos_shift;
     353        int frags;
     354        int lvi;
     355        int lvi_frag;
     356        int civ;
     357        int ack;
     358        int ack_reload;
     359        unsigned int ack_bit;
     360        unsigned int roff_sr;
     361        unsigned int roff_picb;
     362        unsigned int int_sta_mask;              /* interrupt status mask */
     363        unsigned int ali_slot;                  /* ALI DMA slot */
     364        struct ac97_pcm *pcm;
     365        int pcm_open_flag;
     366        unsigned int page_attr_changed: 1;
     367        unsigned int suspended: 1;
    422368};
    423369
    424370struct intel8x0 {
    425     unsigned int device_type;
    426 
    427     int irq;
    428 
    429     unsigned int mmio;
    430     unsigned long addr;
    431     unsigned long remap_addr;
    432     unsigned int bm_mmio;
    433     unsigned long bmaddr;
    434     unsigned long remap_bmaddr;
    435 
    436     struct pci_dev *pci;
    437     snd_card_t *card;
    438 
    439     int pcm_devs;
    440     snd_pcm_t *pcm[6];
    441     struct ichdev ichd[6];
    442 
    443     int multi4: 1,
    444     multi6: 1,
    445     dra: 1,
    446     smp20bit: 1;
    447     int in_ac97_init: 1,
    448     in_sdin_init: 1;
    449     unsigned in_measurement: 1; /* during ac97 clock measurement */
    450     unsigned fix_nocache: 1;    /* workaround for 440MX */
    451     unsigned buggy_irq: 1;              /* workaround for buggy mobos */
    452     unsigned xbox: 1;           /* workaround for Xbox AC'97 detection */
    453     unsigned buggy_semaphore: 1;        /* workaround for buggy codec semaphore */
    454 
    455     int spdif_idx;  /* SPDIF BAR index; *_SPBAR or -1 if use PCMOUT */
    456     unsigned int sdm_saved; /* SDM reg value */
    457 
    458     ac97_bus_t *ac97_bus;
    459     ac97_t *ac97[3];
    460     unsigned int ac97_sdin[3];
    461     unsigned int max_codecs, ncodecs;
    462     unsigned int *codec_bit;
    463     unsigned int codec_isr_bits;
    464     unsigned int codec_ready_bits;
    465 
    466     snd_rawmidi_t *rmidi;
    467 
    468     spinlock_t reg_lock;
    469 
    470     u32 bdbars_count;
    471     struct snd_dma_buffer bdbars;
    472     u32 int_sta_reg;            /* interrupt status register */
    473     u32 int_sta_mask;           /* interrupt status mask */
     371        unsigned int device_type;
     372
     373        int irq;
     374
     375        void __iomem *addr;
     376        void __iomem *bmaddr;
     377
     378        struct pci_dev *pci;
     379        struct snd_card *card;
     380
     381        int pcm_devs;
     382        struct snd_pcm *pcm[6];
     383        struct ichdev ichd[6];
     384
     385        unsigned multi4: 1,
     386                 multi6: 1,
     387                 dra: 1,
     388                 smp20bit: 1;
     389        unsigned in_ac97_init: 1,
     390                 in_sdin_init: 1;
     391        unsigned in_measurement: 1;     /* during ac97 clock measurement */
     392        unsigned fix_nocache: 1;        /* workaround for 440MX */
     393        unsigned buggy_irq: 1;          /* workaround for buggy mobos */
     394        unsigned xbox: 1;               /* workaround for Xbox AC'97 detection */
     395        unsigned buggy_semaphore: 1;    /* workaround for buggy codec semaphore */
     396
     397        int spdif_idx;  /* SPDIF BAR index; *_SPBAR or -1 if use PCMOUT */
     398        unsigned int sdm_saved; /* SDM reg value */
     399
     400        struct snd_ac97_bus *ac97_bus;
     401        struct snd_ac97 *ac97[3];
     402        unsigned int ac97_sdin[3];
     403        unsigned int max_codecs, ncodecs;
     404        unsigned int *codec_bit;
     405        unsigned int codec_isr_bits;
     406        unsigned int codec_ready_bits;
     407
     408        spinlock_t reg_lock;
     409       
     410        u32 bdbars_count;
     411        struct snd_dma_buffer bdbars;
     412        u32 int_sta_reg;                /* interrupt status register */
     413        u32 int_sta_mask;               /* interrupt status mask */
    474414};
    475415
    476416static struct pci_device_id snd_intel8x0_ids[] = {
    477     { 0x8086, 0x2415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* 82801AA */
    478     { 0x8086, 0x2425, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* 82901AB */
    479     { 0x8086, 0x2445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* 82801BA */
    480     { 0x8086, 0x2485, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* ICH3 */
    481     { 0x8086, 0x24c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH4 */
    482     { 0x8086, 0x24d5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH5 }, /* ICH5 */
    483     { 0x8086, 0x25a6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH5 }, /* ESB */
    484     { 0x8086, 0x266e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH5 }, /* ICH6 */
    485     { 0x8086, 0x27de, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH5 }, /* ICH7 */
    486     { 0x8086, 0x2698, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB2 */
    487     { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* 440MX */
    488     { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS },       /* SI7012 */
    489     { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },    /* NFORCE */
    490     { 0x10de, 0x003a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* MCP04 */
    491     { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },    /* NFORCE2 */
    492     { 0x10de, 0x0059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK804 */
    493     { 0x10de, 0x008a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8 */
    494     { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },    /* NFORCE3 */
    495     { 0x10de, 0x00ea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8S */
    496     { 0x10de, 0x026b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* MCP51 */
    497     { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* AMD8111 */
    498     { 0x1022, 0x7445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL },     /* AMD768 */
    499     { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI },   /* Ali5455 */
    500     { 0 }
     417        { 0x8086, 0x2415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
     418        { 0x8086, 0x2425, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
     419        { 0x8086, 0x2445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
     420        { 0x8086, 0x2485, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
     421        { 0x8086, 0x24c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH4 */
     422        { 0x8086, 0x24d5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH5 */
     423        { 0x8086, 0x25a6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB */
     424        { 0x8086, 0x266e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH6 */
     425        { 0x8086, 0x27de, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH7 */
     426        { 0x8086, 0x2698, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB2 */
     427        { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
     428        { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS },   /* SI7012 */
     429        { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE */
     430        { 0x10de, 0x003a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* MCP04 */
     431        { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE2 */
     432        { 0x10de, 0x0059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK804 */
     433        { 0x10de, 0x008a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8 */
     434        { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE3 */
     435        { 0x10de, 0x00ea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8S */
     436        { 0x10de, 0x026b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* MCP51 */
     437        { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
     438        { 0x1022, 0x7445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
     439        { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI },   /* Ali5455 */
     440        { 0, }
    501441};
    502442
    503443MODULE_DEVICE_TABLE(pci, snd_intel8x0_ids);
    504 
    505444
    506445/*
     
    508447 */
    509448
    510 static u8 igetbyte(struct intel8x0 *chip, u32 offset)
    511 {
    512     if (chip->bm_mmio)
    513         return readb(chip->remap_bmaddr + offset);
    514     else
    515         return inb(chip->bmaddr + offset);
    516 }
    517 
    518 static u16 igetword(struct intel8x0 *chip, u32 offset)
    519 {
    520     if (chip->bm_mmio)
    521         return readw(chip->remap_bmaddr + offset);
    522     else
    523         return inw(chip->bmaddr + offset);
    524 }
    525 
    526 static u32 igetdword(struct intel8x0 *chip, u32 offset)
    527 {
    528     if (chip->bm_mmio)
    529         return readl(chip->remap_bmaddr + offset);
    530     else
    531         return inl(chip->bmaddr + offset);
    532 }
    533 
    534 static void iputbyte(struct intel8x0 *chip, u32 offset, u8 val)
    535 {
    536     if (chip->bm_mmio)
    537         writeb(val, chip->remap_bmaddr + offset);
    538     else
    539         outb(val, chip->bmaddr + offset);
    540 }
    541 
    542 static void iputword(struct intel8x0 *chip, u32 offset, u16 val)
    543 {
    544     if (chip->bm_mmio)
    545         writew(val, chip->remap_bmaddr + offset);
    546     else
    547         outw(val, chip->bmaddr + offset);
    548 }
    549 
    550 static void iputdword(struct intel8x0 *chip, u32 offset, u32 val)
    551 {
    552     if (chip->bm_mmio)
    553         writel(val, chip->remap_bmaddr + offset);
    554     else
    555         outl(val, chip->bmaddr + offset);
     449static inline u8 igetbyte(struct intel8x0 *chip, u32 offset)
     450{
     451        return ioread8(chip->bmaddr + offset);
     452}
     453
     454static inline u16 igetword(struct intel8x0 *chip, u32 offset)
     455{
     456        return ioread16(chip->bmaddr + offset);
     457}
     458
     459static inline u32 igetdword(struct intel8x0 *chip, u32 offset)
     460{
     461        return ioread32(chip->bmaddr + offset);
     462}
     463
     464static inline void iputbyte(struct intel8x0 *chip, u32 offset, u8 val)
     465{
     466        iowrite8(val, chip->bmaddr + offset);
     467}
     468
     469static inline void iputword(struct intel8x0 *chip, u32 offset, u16 val)
     470{
     471        iowrite16(val, chip->bmaddr + offset);
     472}
     473
     474static inline void iputdword(struct intel8x0 *chip, u32 offset, u32 val)
     475{
     476        iowrite32(val, chip->bmaddr + offset);
    556477}
    557478
     
    560481 */
    561482
    562 static u16 iagetword(struct intel8x0 *chip, u32 offset)
    563 {
    564     if (chip->mmio)
    565         return readw(chip->remap_addr + offset);
    566     else
    567         return inw(chip->addr + offset);
    568 }
    569 
    570 static void iaputword(struct intel8x0 *chip, u32 offset, u16 val)
    571 {
    572     if (chip->mmio)
    573         writew(val, chip->remap_addr + offset);
    574     else
    575         outw(val, chip->addr + offset);
     483static inline u16 iagetword(struct intel8x0 *chip, u32 offset)
     484{
     485        return ioread16(chip->addr + offset);
     486}
     487
     488static inline void iaputword(struct intel8x0 *chip, u32 offset, u16 val)
     489{
     490        iowrite16(val, chip->addr + offset);
    576491}
    577492
     
    586501static int snd_intel8x0_codec_semaphore(struct intel8x0 *chip, unsigned int codec)
    587502{
    588     int time;
    589 
    590     if (codec > 2)
    591         return -EIO;
    592     if (chip->in_sdin_init) {
    593         /* we don't know the ready bit assignment at the moment */
    594         /* so we check any */
    595         codec = chip->codec_isr_bits;
    596     } else {
    597         codec = chip->codec_bit[chip->ac97_sdin[codec]];
    598     }
    599 
    600     /* codec ready ? */
    601     if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
    602         return -EIO;
    603 
    604     /* Anyone holding a semaphore for 1 msec should be shot... */
    605     time = 100;
    606     do {
    607         if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
    608             return 0;
    609         udelay(10);
    610     } while (time--);
    611 
    612     /* access to some forbidden (non existant) ac97 registers will not
    613      * reset the semaphore. So even if you don't get the semaphore, still
    614      * continue the access. We don't need the semaphore anyway. */
    615     snd_printk("codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
    616                igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
    617     iagetword(chip, 0); /* clear semaphore flag */
    618     /* I don't care about the semaphore */
    619     return -EBUSY;
    620 }
    621 
    622 static void snd_intel8x0_codec_write(ac97_t *ac97,
    623                                      unsigned short reg,
    624                                      unsigned short val)
    625 {
    626     struct intel8x0 *chip = ac97->private_data;
    627 
    628     if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
    629         if (! chip->in_ac97_init)
    630             snd_printk("codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
    631     }
    632     iaputword(chip, reg + ac97->num * 0x80, val);
    633 }
    634 
    635 static unsigned short snd_intel8x0_codec_read(ac97_t *ac97,
    636                                               unsigned short reg)
    637 {
    638     struct intel8x0 *chip = ac97->private_data;
    639     unsigned short res;
    640     unsigned int tmp;
    641 
    642     if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
    643         if (! chip->in_ac97_init)
    644             snd_printk("codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
    645         res = 0xffff;
    646     } else {
    647         res = iagetword(chip, reg + ac97->num * 0x80);
    648         if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
    649             /* reset RCS and preserve other R/WC bits */
    650             iputdword(chip, ICHREG(GLOB_STA), tmp & ~(chip->codec_ready_bits | ICH_GSCI));
    651             if (! chip->in_ac97_init)
    652                 snd_printk("codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
    653             res = 0xffff;
    654         }
    655     }
    656     return res;
     503        int time;
     504       
     505        if (codec > 2)
     506                return -EIO;
     507        if (chip->in_sdin_init) {
     508                /* we don't know the ready bit assignment at the moment */
     509                /* so we check any */
     510                codec = chip->codec_isr_bits;
     511        } else {
     512                codec = chip->codec_bit[chip->ac97_sdin[codec]];
     513        }
     514
     515        /* codec ready ? */
     516        if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
     517                return -EIO;
     518
     519        if (chip->buggy_semaphore)
     520                return 0; /* just ignore ... */
     521
     522        /* Anyone holding a semaphore for 1 msec should be shot... */
     523        time = 100;
     524        do {
     525                if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
     526                        return 0;
     527                udelay(10);
     528        } while (time--);
     529
     530        /* access to some forbidden (non existant) ac97 registers will not
     531         * reset the semaphore. So even if you don't get the semaphore, still
     532         * continue the access. We don't need the semaphore anyway. */
     533        snd_printk(KERN_ERR "codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
     534                        igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
     535        iagetword(chip, 0);     /* clear semaphore flag */
     536        /* I don't care about the semaphore */
     537        return -EBUSY;
     538}
     539 
     540static void snd_intel8x0_codec_write(struct snd_ac97 *ac97,
     541                                     unsigned short reg,
     542                                     unsigned short val)
     543{
     544        struct intel8x0 *chip = ac97->private_data;
     545       
     546        if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
     547                if (! chip->in_ac97_init)
     548                        snd_printk(KERN_ERR "codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
     549        }
     550        iaputword(chip, reg + ac97->num * 0x80, val);
     551}
     552
     553static unsigned short snd_intel8x0_codec_read(struct snd_ac97 *ac97,
     554                                              unsigned short reg)
     555{
     556        struct intel8x0 *chip = ac97->private_data;
     557        unsigned short res;
     558        unsigned int tmp;
     559
     560        if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
     561                if (! chip->in_ac97_init)
     562                        snd_printk(KERN_ERR "codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
     563                res = 0xffff;
     564        } else {
     565                res = iagetword(chip, reg + ac97->num * 0x80);
     566                if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
     567                        /* reset RCS and preserve other R/WC bits */
     568                        iputdword(chip, ICHREG(GLOB_STA), tmp &
     569                                  ~(chip->codec_ready_bits | ICH_GSCI));
     570                        if (! chip->in_ac97_init)
     571                                snd_printk(KERN_ERR "codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
     572                        res = 0xffff;
     573                }
     574        }
     575        return res;
    657576}
    658577
    659578static void __devinit snd_intel8x0_codec_read_test(struct intel8x0 *chip,
    660                                                    unsigned int codec)
    661 {
    662     unsigned int tmp;
    663 
    664     if (snd_intel8x0_codec_semaphore(chip, codec) >= 0) {
    665         iagetword(chip, codec * 0x80);
    666         if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
    667             /* reset RCS and preserve other R/WC bits */
    668             iputdword(chip, ICHREG(GLOB_STA), tmp & ~(chip->codec_ready_bits | ICH_GSCI));
    669         }
    670     }
     579                                                   unsigned int codec)
     580{
     581        unsigned int tmp;
     582
     583        if (snd_intel8x0_codec_semaphore(chip, codec) >= 0) {
     584                iagetword(chip, codec * 0x80);
     585                if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
     586                        /* reset RCS and preserve other R/WC bits */
     587                        iputdword(chip, ICHREG(GLOB_STA), tmp &
     588                                  ~(chip->codec_ready_bits | ICH_GSCI));
     589                }
     590        }
    671591}
    672592
     
    676596static int snd_intel8x0_ali_codec_ready(struct intel8x0 *chip, int mask)
    677597{
    678     int count = 0;
    679     for (count = 0; count < 0x7f; count++) {
    680         int val = igetbyte(chip, ICHREG(ALI_CSPSR));
    681         if (val & mask)
    682             return 0;
    683     }
    684     if (! chip->in_ac97_init)
    685         snd_printd(KERN_WARNING "intel8x0: AC97 codec ready timeout.\n");
    686     return -EBUSY;
     598        int count = 0;
     599        for (count = 0; count < 0x7f; count++) {
     600                int val = igetbyte(chip, ICHREG(ALI_CSPSR));
     601                if (val & mask)
     602                        return 0;
     603        }
     604        if (! chip->in_ac97_init)
     605                snd_printd(KERN_WARNING "intel8x0: AC97 codec ready timeout.\n");
     606        return -EBUSY;
    687607}
    688608
    689609static int snd_intel8x0_ali_codec_semaphore(struct intel8x0 *chip)
    690610{
    691     int time = 100;
    692     if (chip->buggy_semaphore)
    693         return 0; /* just ignore ... */
    694     while (time-- && (igetdword(chip, ICHREG(ALI_CAS)) & ALI_CAS_SEM_BUSY))
    695         udelay(1);
    696     if (! time && ! chip->in_ac97_init)
    697         snd_printk(KERN_WARNING "ali_codec_semaphore timeout\n");
    698     return snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_CODEC_READY);
    699 }
    700 
    701 static unsigned short snd_intel8x0_ali_codec_read(ac97_t *ac97, unsigned short reg)
    702 {
    703     struct intel8x0 *chip = ac97->private_data;
    704     unsigned short data = 0xffff;
    705 
    706     if (snd_intel8x0_ali_codec_semaphore(chip))
    707         goto __err;
    708     reg |= ALI_CPR_ADDR_READ;
    709     if (ac97->num)
    710         reg |= ALI_CPR_ADDR_SECONDARY;
    711     iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
    712     if (snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_READ_OK))
    713         goto __err;
    714     data = igetword(chip, ICHREG(ALI_SPR));
    715 __err:
    716     return data;
    717 }
    718 
    719 static void snd_intel8x0_ali_codec_write(ac97_t *ac97, unsigned short reg, unsigned short val)
    720 {
    721     struct intel8x0 *chip = ac97->private_data;
    722 
    723     if (snd_intel8x0_ali_codec_semaphore(chip))
    724         return;
    725     iputword(chip, ICHREG(ALI_CPR), val);
    726     if (ac97->num)
    727         reg |= ALI_CPR_ADDR_SECONDARY;
    728     iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
    729     snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_WRITE_OK);
     611        int time = 100;
     612        if (chip->buggy_semaphore)
     613                return 0; /* just ignore ... */
     614        while (time-- && (igetdword(chip, ICHREG(ALI_CAS)) & ALI_CAS_SEM_BUSY))
     615                udelay(1);
     616        if (! time && ! chip->in_ac97_init)
     617                snd_printk(KERN_WARNING "ali_codec_semaphore timeout\n");
     618        return snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_CODEC_READY);
     619}
     620
     621static unsigned short snd_intel8x0_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
     622{
     623        struct intel8x0 *chip = ac97->private_data;
     624        unsigned short data = 0xffff;
     625
     626        if (snd_intel8x0_ali_codec_semaphore(chip))
     627                goto __err;
     628        reg |= ALI_CPR_ADDR_READ;
     629        if (ac97->num)
     630                reg |= ALI_CPR_ADDR_SECONDARY;
     631        iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
     632        if (snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_READ_OK))
     633                goto __err;
     634        data = igetword(chip, ICHREG(ALI_SPR));
     635 __err:
     636        return data;
     637}
     638
     639static void snd_intel8x0_ali_codec_write(struct snd_ac97 *ac97, unsigned short reg,
     640                                         unsigned short val)
     641{
     642        struct intel8x0 *chip = ac97->private_data;
     643
     644        if (snd_intel8x0_ali_codec_semaphore(chip))
     645                return;
     646        iputword(chip, ICHREG(ALI_CPR), val);
     647        if (ac97->num)
     648                reg |= ALI_CPR_ADDR_SECONDARY;
     649        iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
     650        snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_WRITE_OK);
    730651}
    731652
     
    734655 * DMA I/O
    735656 */
    736 static void snd_intel8x0_setup_periods(struct intel8x0 *chip, struct ichdev *ichdev)
    737 {
    738     int idx;
    739     u32 *bdbar = ichdev->bdbar;
    740     unsigned long port = ichdev->reg_offset;
    741 
    742     iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
    743     if (ichdev->size == ichdev->fragsize) {
    744         ichdev->ack_reload = ichdev->ack = 2;
    745         ichdev->fragsize1 = ichdev->fragsize >> 1;
    746         for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
    747             bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
    748             bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
    749                                          ichdev->fragsize1 >> ichdev->pos_shift);
    750             bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
    751             bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
    752                                          ichdev->fragsize1 >> ichdev->pos_shift);
    753         }
    754         ichdev->frags = 2;
    755     } else {
    756         ichdev->ack_reload = ichdev->ack = 1;
    757         ichdev->fragsize1 = ichdev->fragsize;
    758         for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
    759             bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
    760             bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
    761                                          ichdev->fragsize >> ichdev->pos_shift);
    762             // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
    763         }
    764         ichdev->frags = ichdev->size / ichdev->fragsize;
    765     }
    766     iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
    767     ichdev->civ = 0;
    768     iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
    769     ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
    770     ichdev->position = 0;
     657static void snd_intel8x0_setup_periods(struct intel8x0 *chip, struct ichdev *ichdev)
     658{
     659        int idx;
     660        u32 *bdbar = ichdev->bdbar;
     661        unsigned long port = ichdev->reg_offset;
     662
     663        iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
     664        if (ichdev->size == ichdev->fragsize) {
     665                ichdev->ack_reload = ichdev->ack = 2;
     666                ichdev->fragsize1 = ichdev->fragsize >> 1;
     667                for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
     668                        bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
     669                        bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
     670                                                     ichdev->fragsize1 >> ichdev->pos_shift);
     671                        bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
     672                        bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
     673                                                     ichdev->fragsize1 >> ichdev->pos_shift);
     674                }
     675                ichdev->frags = 2;
     676        } else {
     677                ichdev->ack_reload = ichdev->ack = 1;
     678                ichdev->fragsize1 = ichdev->fragsize;
     679                for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
     680                        bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf +
     681                                                     (((idx >> 1) * ichdev->fragsize) %
     682                                                      ichdev->size));
     683                        bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
     684                                                     ichdev->fragsize >> ichdev->pos_shift);
    771685#if 0
    772     printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
    773            ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
     686                        printk("bdbar[%i] = 0x%x [0x%x]\n",
     687                               idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
    774688#endif
    775     /* clear interrupts */
    776     iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
    777 }
     689                }
     690                ichdev->frags = ichdev->size / ichdev->fragsize;
     691        }
     692        iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
     693        ichdev->civ = 0;
     694        iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
     695        ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
     696        ichdev->position = 0;
     697#if 0
     698        printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
     699                        ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
     700#endif
     701        /* clear interrupts */
     702        iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
     703}
     704
     705#ifdef __i386__
     706/*
     707 * Intel 82443MX running a 100MHz processor system bus has a hardware bug,
     708 * which aborts PCI busmaster for audio transfer.  A workaround is to set
     709 * the pages as non-cached.  For details, see the errata in
     710 *      http://www.intel.com/design/chipsets/specupdt/245051.htm
     711 */
     712static void fill_nocache(void *buf, int size, int nocache)
     713{
     714        size = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
     715        change_page_attr(virt_to_page(buf), size, nocache ? PAGE_KERNEL_NOCACHE : PAGE_KERNEL);
     716        global_flush_tlb();
     717}
     718#else
     719#define fill_nocache(buf,size,nocache)
     720#endif
    778721
    779722/*
     
    783726static inline void snd_intel8x0_update(struct intel8x0 *chip, struct ichdev *ichdev)
    784727{
    785     unsigned long port = ichdev->reg_offset;
    786     int status, civ, i, step;
    787     int ack = 0;
    788 
    789     spin_lock(&chip->reg_lock);
    790     status = igetbyte(chip, port + ichdev->roff_sr);
    791     civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
    792     if (!(status & ICH_BCIS)) {
    793         step = 0;
    794     } else if (civ == ichdev->civ) {
    795         // snd_printd("civ same %d\n", civ);
    796         step = 1;
    797         ichdev->civ++;
    798         ichdev->civ &= ICH_REG_LVI_MASK;
    799     } else {
    800         step = civ - ichdev->civ;
    801         if (step < 0)
    802             step += ICH_REG_LVI_MASK + 1;
    803         // if (step != 1)
    804         //      snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
    805         ichdev->civ = civ;
    806     }
    807 
    808     ichdev->position += step * ichdev->fragsize1;
    809     ichdev->position %= ichdev->size;
    810     ichdev->lvi += step;
    811     ichdev->lvi &= ICH_REG_LVI_MASK;
    812     iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
    813     for (i = 0; i < step; i++) {
    814         ichdev->lvi_frag++;
    815         ichdev->lvi_frag %= ichdev->frags;
    816         ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
    817         // printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n", ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2], ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port), inl(port + 4), inb(port + ICH_REG_OFF_CR));
    818         if (--ichdev->ack == 0) {
    819             ichdev->ack = ichdev->ack_reload;
    820             ack = 1;
    821         }
    822     }
    823     spin_unlock(&chip->reg_lock);
    824     if (ack && ichdev->substream) {
    825         snd_pcm_period_elapsed(ichdev->substream);
    826     }
    827     iputbyte(chip, port + ichdev->roff_sr,
    828              status & (ICH_FIFOE | ICH_BCIS | ICH_LVBCI));
    829 }
    830 
    831 static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
    832 {
    833     struct intel8x0 *chip = dev_id;
    834     struct ichdev *ichdev;
    835     unsigned int status;
    836     unsigned int i;
    837 #ifdef TARGET_OS2
    838     int fOurIrq = FALSE;
     728        unsigned long port = ichdev->reg_offset;
     729        unsigned long flags;
     730        int status, civ, i, step;
     731        int ack = 0;
     732
     733        spin_lock_irqsave(&chip->reg_lock, flags);
     734        status = igetbyte(chip, port + ichdev->roff_sr);
     735        civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
     736        if (!(status & ICH_BCIS)) {
     737                step = 0;
     738        } else if (civ == ichdev->civ) {
     739                // snd_printd("civ same %d\n", civ);
     740                step = 1;
     741                ichdev->civ++;
     742                ichdev->civ &= ICH_REG_LVI_MASK;
     743        } else {
     744                step = civ - ichdev->civ;
     745                if (step < 0)
     746                        step += ICH_REG_LVI_MASK + 1;
     747                // if (step != 1)
     748                //      snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
     749                ichdev->civ = civ;
     750        }
     751
     752        ichdev->position += step * ichdev->fragsize1;
     753        if (! chip->in_measurement)
     754                ichdev->position %= ichdev->size;
     755        ichdev->lvi += step;
     756        ichdev->lvi &= ICH_REG_LVI_MASK;
     757        iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
     758        for (i = 0; i < step; i++) {
     759                ichdev->lvi_frag++;
     760                ichdev->lvi_frag %= ichdev->frags;
     761                ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
     762#if 0
     763        printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n",
     764               ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2],
     765               ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port),
     766               inl(port + 4), inb(port + ICH_REG_OFF_CR));
    839767#endif
    840 
    841     status = igetdword(chip, chip->int_sta_reg);
    842     if (status == 0xffffffff)       /* we are not yet resumed */
    843         return IRQ_NONE;
    844 
    845     if ((status & chip->int_sta_mask) == 0) {
    846         static int err_count = 10;
    847         if (status) {
    848             /* ack */
    849             iputdword(chip, chip->int_sta_reg, status);
    850             /* FIXME: on some ICH5 board shows the same
    851              *        problem.  So we return IRQ_HANDLED
    852              *        in any cases.
    853              * (or, maybe add a new module param to control this?)
    854              */
    855 #if 0
    856             if (chip->device_type != DEVICE_NFORCE)
    857                 status ^= igetdword(chip, chip->int_sta_reg);
    858 #endif
    859         }
    860         return IRQ_NONE/*RETVAL(status)*/;
    861     }
    862 #ifdef TARGET_OS2
    863     fOurIrq = TRUE;
    864 #endif
    865 
    866     for (i = 0; i < chip->bdbars_count; i++) {
    867         ichdev = &chip->ichd[i];
    868         if (status & ichdev->int_sta_mask)
    869             snd_intel8x0_update(chip, ichdev);
    870     }
    871 
    872     /* ack them */
    873     iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
    874 #ifdef TARGET_OS2
    875     if (fOurIrq) {
    876         //eoi_irq(irq);
    877     }
    878 #endif //TARGET_OS2
    879 
    880     return IRQ_HANDLED;
     768                if (--ichdev->ack == 0) {
     769                        ichdev->ack = ichdev->ack_reload;
     770                        ack = 1;
     771                }
     772        }
     773        spin_unlock_irqrestore(&chip->reg_lock, flags);
     774        if (ack && ichdev->substream) {
     775                snd_pcm_period_elapsed(ichdev->substream);
     776        }
     777        iputbyte(chip, port + ichdev->roff_sr,
     778                 status & (ICH_FIFOE | ICH_BCIS | ICH_LVBCI));
     779}
     780
     781static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id)
     782{
     783        struct intel8x0 *chip = dev_id;
     784        struct ichdev *ichdev;
     785        unsigned int status;
     786        unsigned int i;
     787
     788        status = igetdword(chip, chip->int_sta_reg);
     789        if (status == 0xffffffff)       /* we are not yet resumed */
     790                return IRQ_NONE;
     791
     792        if ((status & chip->int_sta_mask) == 0) {
     793                if (status) {
     794                        /* ack */
     795                        iputdword(chip, chip->int_sta_reg, status);
     796                        if (! chip->buggy_irq)
     797                                status = 0;
     798                }
     799                return IRQ_RETVAL(status);
     800        }
     801
     802        for (i = 0; i < chip->bdbars_count; i++) {
     803                ichdev = &chip->ichd[i];
     804                if (status & ichdev->int_sta_mask)
     805                        snd_intel8x0_update(chip, ichdev);
     806        }
     807
     808        /* ack them */
     809        iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
     810       
     811        return IRQ_HANDLED;
    881812}
    882813
     
    885816 */
    886817
    887 static int snd_intel8x0_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
    888 {
    889     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    890     struct ichdev *ichdev = get_ichdev(substream);
    891     unsigned char val = 0;
    892     unsigned long port = ichdev->reg_offset;
    893 
    894     switch (cmd) {
    895     case SNDRV_PCM_TRIGGER_RESUME:
    896         ichdev->suspended = 0;
    897         /* fallthru */
    898     case SNDRV_PCM_TRIGGER_START:
    899         val = ICH_IOCE | ICH_STARTBM;
    900         break;
    901     case SNDRV_PCM_TRIGGER_SUSPEND:
    902         ichdev->suspended = 1;
    903         /* fallthru */
    904     case SNDRV_PCM_TRIGGER_STOP:
    905         val = 0;
    906         break;
    907     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
    908         val = ICH_IOCE;
    909         break;
    910     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
    911         val = ICH_IOCE | ICH_STARTBM;
    912         break;
    913     default:
    914         return -EINVAL;
    915     }
    916     iputbyte(chip, port + ICH_REG_OFF_CR, val);
    917     if (cmd == SNDRV_PCM_TRIGGER_STOP) {
    918         /* wait until DMA stopped */
    919         while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
    920         /* reset whole DMA things */
    921         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
    922     }
    923     return 0;
    924 }
    925 
    926 static int snd_intel8x0_ali_trigger(snd_pcm_substream_t *substream, int cmd)
    927 {
    928     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    929     struct ichdev *ichdev = get_ichdev(substream);
    930     unsigned long port = ichdev->reg_offset;
    931     static int fiforeg[] = { ICHREG(ALI_FIFOCR1), ICHREG(ALI_FIFOCR2), ICHREG(ALI_FIFOCR3) };
    932     unsigned int val, fifo;
    933 
    934     val = igetdword(chip, ICHREG(ALI_DMACR));
    935     switch (cmd) {
    936     case SNDRV_PCM_TRIGGER_RESUME:
    937         ichdev->suspended = 0;
    938         /* fallthru */
    939     case SNDRV_PCM_TRIGGER_START:
    940     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
    941         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
    942             /* clear FIFO for synchronization of channels */
    943             fifo = igetdword(chip, fiforeg[ichdev->ali_slot / 4]);
    944             fifo &= ~(0xff << (ichdev->ali_slot % 4));
    945             fifo |= 0x83 << (ichdev->ali_slot % 4);
    946             iputdword(chip, fiforeg[ichdev->ali_slot / 4], fifo);
    947         }
    948         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
    949         val &= ~(1 << (ichdev->ali_slot + 16)); /* clear PAUSE flag */
    950         iputdword(chip, ICHREG(ALI_DMACR), val | (1 << ichdev->ali_slot)); /* start DMA */
    951         break;
    952     case SNDRV_PCM_TRIGGER_SUSPEND:
    953         ichdev->suspended = 1;
    954         /* fallthru */
    955     case SNDRV_PCM_TRIGGER_STOP:
    956     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
    957         iputdword(chip, ICHREG(ALI_DMACR), val | (1 << (ichdev->ali_slot + 16))); /* pause */
    958         iputbyte(chip, port + ICH_REG_OFF_CR, 0);
    959         while (igetbyte(chip, port + ICH_REG_OFF_CR))
    960             ;
    961         if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
    962             break;
    963         /* reset whole DMA things */
    964         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
    965         /* clear interrupts */
    966         iputbyte(chip, port + ICH_REG_OFF_SR, igetbyte(chip, port + ICH_REG_OFF_SR) | 0x1e);
    967         iputdword(chip, ICHREG(ALI_INTERRUPTSR),
    968                   igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ichdev->int_sta_mask);
    969         break;
    970     default:
    971         return -EINVAL;
    972     }
    973     return 0;
    974 }
    975 
    976 static int snd_intel8x0_hw_params(snd_pcm_substream_t * substream,
    977                                   snd_pcm_hw_params_t * hw_params)
    978 {
    979     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    980     struct ichdev *ichdev = get_ichdev(substream);
    981     int dbl = params_rate(hw_params) > 48000;
    982     int err;
    983 
    984     err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
    985     if (err < 0)
    986         return err;
    987     if (ichdev->pcm_open_flag) {
    988         snd_ac97_pcm_close(ichdev->pcm);
    989         ichdev->pcm_open_flag = 0;
    990     }
    991     err = snd_ac97_pcm_open(ichdev->pcm, params_rate(hw_params),
    992                             params_channels(hw_params),
    993                             ichdev->pcm->r[dbl].slots);
    994     if (err >= 0) {
    995         ichdev->pcm_open_flag = 1;
    996         /* Force SPDIF setting */
    997         if (ichdev->ichd == ICHD_PCMOUT && chip->spdif_idx < 0)
    998             snd_ac97_set_rate(ichdev->pcm->r[0].codec[0], AC97_SPDIF, params_rate(hw_params));
    999     }
    1000     return err;
    1001 }
    1002 
    1003 static int snd_intel8x0_hw_free(snd_pcm_substream_t * substream)
    1004 {
    1005     struct ichdev *ichdev = get_ichdev(substream);
    1006 
    1007     if (ichdev->pcm_open_flag) {
    1008         snd_ac97_pcm_close(ichdev->pcm);
    1009         ichdev->pcm_open_flag = 0;
    1010     }
    1011     return snd_pcm_lib_free_pages(substream);
     818static int snd_intel8x0_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
     819{
     820        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     821        struct ichdev *ichdev = get_ichdev(substream);
     822        unsigned char val = 0;
     823        unsigned long port = ichdev->reg_offset;
     824
     825        switch (cmd) {
     826        case SNDRV_PCM_TRIGGER_RESUME:
     827                ichdev->suspended = 0;
     828                /* fallthru */
     829        case SNDRV_PCM_TRIGGER_START:
     830                val = ICH_IOCE | ICH_STARTBM;
     831                break;
     832        case SNDRV_PCM_TRIGGER_SUSPEND:
     833                ichdev->suspended = 1;
     834                /* fallthru */
     835        case SNDRV_PCM_TRIGGER_STOP:
     836                val = 0;
     837                break;
     838        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
     839                val = ICH_IOCE;
     840                break;
     841        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
     842                val = ICH_IOCE | ICH_STARTBM;
     843                break;
     844        default:
     845                return -EINVAL;
     846        }
     847        iputbyte(chip, port + ICH_REG_OFF_CR, val);
     848        if (cmd == SNDRV_PCM_TRIGGER_STOP) {
     849                /* wait until DMA stopped */
     850                while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
     851                /* reset whole DMA things */
     852                iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
     853        }
     854        return 0;
     855}
     856
     857static int snd_intel8x0_ali_trigger(struct snd_pcm_substream *substream, int cmd)
     858{
     859        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     860        struct ichdev *ichdev = get_ichdev(substream);
     861        unsigned long port = ichdev->reg_offset;
     862        static int fiforeg[] = {
     863                ICHREG(ALI_FIFOCR1), ICHREG(ALI_FIFOCR2), ICHREG(ALI_FIFOCR3)
     864        };
     865        unsigned int val, fifo;
     866
     867        val = igetdword(chip, ICHREG(ALI_DMACR));
     868        switch (cmd) {
     869        case SNDRV_PCM_TRIGGER_RESUME:
     870                ichdev->suspended = 0;
     871                /* fallthru */
     872        case SNDRV_PCM_TRIGGER_START:
     873        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
     874                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
     875                        /* clear FIFO for synchronization of channels */
     876                        fifo = igetdword(chip, fiforeg[ichdev->ali_slot / 4]);
     877                        fifo &= ~(0xff << (ichdev->ali_slot % 4)); 
     878                        fifo |= 0x83 << (ichdev->ali_slot % 4);
     879                        iputdword(chip, fiforeg[ichdev->ali_slot / 4], fifo);
     880                }
     881                iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
     882                val &= ~(1 << (ichdev->ali_slot + 16)); /* clear PAUSE flag */
     883                /* start DMA */
     884                iputdword(chip, ICHREG(ALI_DMACR), val | (1 << ichdev->ali_slot));
     885                break;
     886        case SNDRV_PCM_TRIGGER_SUSPEND:
     887                ichdev->suspended = 1;
     888                /* fallthru */
     889        case SNDRV_PCM_TRIGGER_STOP:
     890        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
     891                /* pause */
     892                iputdword(chip, ICHREG(ALI_DMACR), val | (1 << (ichdev->ali_slot + 16)));
     893                iputbyte(chip, port + ICH_REG_OFF_CR, 0);
     894                while (igetbyte(chip, port + ICH_REG_OFF_CR))
     895                        ;
     896                if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
     897                        break;
     898                /* reset whole DMA things */
     899                iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
     900                /* clear interrupts */
     901                iputbyte(chip, port + ICH_REG_OFF_SR,
     902                         igetbyte(chip, port + ICH_REG_OFF_SR) | 0x1e);
     903                iputdword(chip, ICHREG(ALI_INTERRUPTSR),
     904                          igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ichdev->int_sta_mask);
     905                break;
     906        default:
     907                return -EINVAL;
     908        }
     909        return 0;
     910}
     911
     912static int snd_intel8x0_hw_params(struct snd_pcm_substream *substream,
     913                                  struct snd_pcm_hw_params *hw_params)
     914{
     915        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     916        struct ichdev *ichdev = get_ichdev(substream);
     917        struct snd_pcm_runtime *runtime = substream->runtime;
     918        int dbl = params_rate(hw_params) > 48000;
     919        int err;
     920
     921        if (chip->fix_nocache && ichdev->page_attr_changed) {
     922                fill_nocache(runtime->dma_area, runtime->dma_bytes, 0); /* clear */
     923                ichdev->page_attr_changed = 0;
     924        }
     925        err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
     926        if (err < 0)
     927                return err;
     928        if (chip->fix_nocache) {
     929                if (runtime->dma_area && ! ichdev->page_attr_changed) {
     930                        fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
     931                        ichdev->page_attr_changed = 1;
     932                }
     933        }
     934        if (ichdev->pcm_open_flag) {
     935                snd_ac97_pcm_close(ichdev->pcm);
     936                ichdev->pcm_open_flag = 0;
     937        }
     938        err = snd_ac97_pcm_open(ichdev->pcm, params_rate(hw_params),
     939                                params_channels(hw_params),
     940                                ichdev->pcm->r[dbl].slots);
     941        if (err >= 0) {
     942                ichdev->pcm_open_flag = 1;
     943                /* Force SPDIF setting */
     944                if (ichdev->ichd == ICHD_PCMOUT && chip->spdif_idx < 0)
     945                        snd_ac97_set_rate(ichdev->pcm->r[0].codec[0], AC97_SPDIF,
     946                                          params_rate(hw_params));
     947        }
     948        return err;
     949}
     950
     951static int snd_intel8x0_hw_free(struct snd_pcm_substream *substream)
     952{
     953        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     954        struct ichdev *ichdev = get_ichdev(substream);
     955
     956        if (ichdev->pcm_open_flag) {
     957                snd_ac97_pcm_close(ichdev->pcm);
     958                ichdev->pcm_open_flag = 0;
     959        }
     960        if (chip->fix_nocache && ichdev->page_attr_changed) {
     961                fill_nocache(substream->runtime->dma_area, substream->runtime->dma_bytes, 0);
     962                ichdev->page_attr_changed = 0;
     963        }
     964        return snd_pcm_lib_free_pages(substream);
    1012965}
    1013966
    1014967static void snd_intel8x0_setup_pcm_out(struct intel8x0 *chip,
    1015                                        snd_pcm_runtime_t *runtime)
    1016 {
    1017     unsigned int cnt;
    1018     int dbl = runtime->rate > 48000;
    1019 
    1020     spin_lock_irq(&chip->reg_lock);
    1021     switch (chip->device_type) {
    1022     case DEVICE_ALI:
    1023         cnt = igetdword(chip, ICHREG(ALI_SCR));
    1024         cnt &= ~ICH_ALI_SC_PCM_246_MASK;
    1025         if (runtime->channels == 4 || dbl)
    1026             cnt |= ICH_ALI_SC_PCM_4;
    1027         else if (runtime->channels == 6)
    1028             cnt |= ICH_ALI_SC_PCM_6;
    1029         iputdword(chip, ICHREG(ALI_SCR), cnt);
    1030         break;
    1031     case DEVICE_SIS:
    1032         cnt = igetdword(chip, ICHREG(GLOB_CNT));
    1033         cnt &= ~ICH_SIS_PCM_246_MASK;
    1034         if (runtime->channels == 4 || dbl)
    1035             cnt |= ICH_SIS_PCM_4;
    1036         else if (runtime->channels == 6)
    1037             cnt |= ICH_SIS_PCM_6;
    1038         iputdword(chip, ICHREG(GLOB_CNT), cnt);
    1039         break;
    1040     default:
    1041         cnt = igetdword(chip, ICHREG(GLOB_CNT));
    1042         cnt &= ~(ICH_PCM_246_MASK | ICH_PCM_20BIT);
    1043         if (runtime->channels == 4 || dbl)
    1044             cnt |= ICH_PCM_4;
    1045         else if (runtime->channels == 6)
    1046             cnt |= ICH_PCM_6;
    1047         if (chip->device_type == DEVICE_NFORCE) {
    1048             /* reset to 2ch once to keep the 6 channel data in alignment,
    1049              * to start from Front Left always
    1050              */
    1051             if (cnt & ICH_PCM_246_MASK) {
    1052                 iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_PCM_246_MASK);
    1053                 spin_unlock_irq(&chip->reg_lock);
    1054                 msleep(50); /* grrr... */
    1055                 spin_lock_irq(&chip->reg_lock);
    1056             }
    1057         } else if (chip->device_type == DEVICE_INTEL_ICH4 ||
    1058                    chip->device_type == DEVICE_INTEL_ICH5) {
    1059             if (runtime->sample_bits > 16)
    1060                 cnt |= ICH_PCM_20BIT;
    1061         }
    1062         iputdword(chip, ICHREG(GLOB_CNT), cnt);
    1063         break;
    1064     }
    1065     spin_unlock_irq(&chip->reg_lock);
    1066 }
    1067 
    1068 static int snd_intel8x0_pcm_prepare(snd_pcm_substream_t * substream)
    1069 {
    1070     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1071     snd_pcm_runtime_t *runtime = substream->runtime;
    1072     struct ichdev *ichdev = get_ichdev(substream);
    1073 
    1074     ichdev->physbuf = runtime->dma_addr;
    1075     ichdev->size = snd_pcm_lib_buffer_bytes(substream);
    1076     ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
    1077     if (ichdev->ichd == ICHD_PCMOUT) {
    1078         snd_intel8x0_setup_pcm_out(chip, runtime);
    1079         if (chip->device_type == DEVICE_INTEL_ICH4 ||
    1080             chip->device_type == DEVICE_INTEL_ICH5)
    1081             ichdev->pos_shift = (runtime->sample_bits > 16) ? 2 : 1;
    1082     }
    1083     snd_intel8x0_setup_periods(chip, ichdev);
    1084     return 0;
    1085 }
    1086 
    1087 static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(snd_pcm_substream_t * substream)
    1088 {
    1089     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1090     struct ichdev *ichdev = get_ichdev(substream);
    1091     size_t ptr1, ptr;
    1092     int civ, timeout = 100;
    1093     unsigned int position;
    1094 
    1095     spin_lock(&chip->reg_lock);
    1096     do {
    1097         civ = igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV);
    1098         ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb);
    1099         position = ichdev->position;
    1100         if (ptr1 == 0) {
    1101             udelay(10);
    1102             continue;
    1103         }
    1104         if (civ == igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV) &&
    1105             ptr1 == igetword(chip, ichdev->reg_offset + ichdev->roff_picb))
    1106             break;
    1107     } while (timeout--);
    1108     ptr1 <<= ichdev->pos_shift;
    1109     ptr = ichdev->fragsize1 - ptr1;
    1110     ptr += position;
    1111     spin_unlock(&chip->reg_lock);
    1112     if (ptr >= ichdev->size)
    1113         return 0;
    1114     return bytes_to_frames(substream->runtime, ptr);
    1115 }
    1116 
    1117 static snd_pcm_hardware_t snd_intel8x0_stream =
     968                                       struct snd_pcm_runtime *runtime)
     969{
     970        unsigned int cnt;
     971        int dbl = runtime->rate > 48000;
     972
     973        spin_lock_irq(&chip->reg_lock);
     974        switch (chip->device_type) {
     975        case DEVICE_ALI:
     976                cnt = igetdword(chip, ICHREG(ALI_SCR));
     977                cnt &= ~ICH_ALI_SC_PCM_246_MASK;
     978                if (runtime->channels == 4 || dbl)
     979                        cnt |= ICH_ALI_SC_PCM_4;
     980                else if (runtime->channels == 6)
     981                        cnt |= ICH_ALI_SC_PCM_6;
     982                iputdword(chip, ICHREG(ALI_SCR), cnt);
     983                break;
     984        case DEVICE_SIS:
     985                cnt = igetdword(chip, ICHREG(GLOB_CNT));
     986                cnt &= ~ICH_SIS_PCM_246_MASK;
     987                if (runtime->channels == 4 || dbl)
     988                        cnt |= ICH_SIS_PCM_4;
     989                else if (runtime->channels == 6)
     990                        cnt |= ICH_SIS_PCM_6;
     991                iputdword(chip, ICHREG(GLOB_CNT), cnt);
     992                break;
     993        default:
     994                cnt = igetdword(chip, ICHREG(GLOB_CNT));
     995                cnt &= ~(ICH_PCM_246_MASK | ICH_PCM_20BIT);
     996                if (runtime->channels == 4 || dbl)
     997                        cnt |= ICH_PCM_4;
     998                else if (runtime->channels == 6)
     999                        cnt |= ICH_PCM_6;
     1000                if (chip->device_type == DEVICE_NFORCE) {
     1001                        /* reset to 2ch once to keep the 6 channel data in alignment,
     1002                         * to start from Front Left always
     1003                         */
     1004                        if (cnt & ICH_PCM_246_MASK) {
     1005                                iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_PCM_246_MASK);
     1006                                spin_unlock_irq(&chip->reg_lock);
     1007                                msleep(50); /* grrr... */
     1008                                spin_lock_irq(&chip->reg_lock);
     1009                        }
     1010                } else if (chip->device_type == DEVICE_INTEL_ICH4) {
     1011                        if (runtime->sample_bits > 16)
     1012                                cnt |= ICH_PCM_20BIT;
     1013                }
     1014                iputdword(chip, ICHREG(GLOB_CNT), cnt);
     1015                break;
     1016        }
     1017        spin_unlock_irq(&chip->reg_lock);
     1018}
     1019
     1020static int snd_intel8x0_pcm_prepare(struct snd_pcm_substream *substream)
     1021{
     1022        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1023        struct snd_pcm_runtime *runtime = substream->runtime;
     1024        struct ichdev *ichdev = get_ichdev(substream);
     1025
     1026        ichdev->physbuf = runtime->dma_addr;
     1027        ichdev->size = snd_pcm_lib_buffer_bytes(substream);
     1028        ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
     1029        if (ichdev->ichd == ICHD_PCMOUT) {
     1030                snd_intel8x0_setup_pcm_out(chip, runtime);
     1031                if (chip->device_type == DEVICE_INTEL_ICH4)
     1032                        ichdev->pos_shift = (runtime->sample_bits > 16) ? 2 : 1;
     1033        }
     1034        snd_intel8x0_setup_periods(chip, ichdev);
     1035        return 0;
     1036}
     1037
     1038static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(struct snd_pcm_substream *substream)
     1039{
     1040        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1041        struct ichdev *ichdev = get_ichdev(substream);
     1042        size_t ptr1, ptr;
     1043        int civ, timeout = 100;
     1044        unsigned int position;
     1045
     1046        spin_lock(&chip->reg_lock);
     1047        do {
     1048                civ = igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV);
     1049                ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb);
     1050                position = ichdev->position;
     1051                if (ptr1 == 0) {
     1052                        udelay(10);
     1053                        continue;
     1054                }
     1055                if (civ == igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV) &&
     1056                    ptr1 == igetword(chip, ichdev->reg_offset + ichdev->roff_picb))
     1057                        break;
     1058        } while (timeout--);
     1059        ptr1 <<= ichdev->pos_shift;
     1060        ptr = ichdev->fragsize1 - ptr1;
     1061        ptr += position;
     1062        spin_unlock(&chip->reg_lock);
     1063        if (ptr >= ichdev->size)
     1064                return 0;
     1065        return bytes_to_frames(substream->runtime, ptr);
     1066}
     1067
     1068static struct snd_pcm_hardware snd_intel8x0_stream =
    11181069{
    11191070        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
     
    11371088
    11381089static unsigned int channels4[] = {
    1139     2, 4,
    1140 };
    1141 
    1142 static snd_pcm_hw_constraint_list_t hw_constraints_channels4 = {
     1090        2, 4,
     1091};
     1092
     1093static struct snd_pcm_hw_constraint_list hw_constraints_channels4 = {
    11431094        .count = ARRAY_SIZE(channels4),
    11441095        .list = channels4,
     
    11471098
    11481099static unsigned int channels6[] = {
    1149     2, 4, 6,
    1150 };
    1151 
    1152 static snd_pcm_hw_constraint_list_t hw_constraints_channels6 = {
     1100        2, 4, 6,
     1101};
     1102
     1103static struct snd_pcm_hw_constraint_list hw_constraints_channels6 = {
    11531104        .count = ARRAY_SIZE(channels6),
    11541105        .list = channels6,
     
    11561107};
    11571108
    1158 static int snd_intel8x0_pcm_open(snd_pcm_substream_t * substream, struct ichdev *ichdev)
    1159 {
    1160     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1161     snd_pcm_runtime_t *runtime = substream->runtime;
    1162     int err;
    1163 
    1164     ichdev->substream = substream;
    1165     runtime->hw = snd_intel8x0_stream;
    1166     runtime->hw.rates = ichdev->pcm->rates;
    1167     snd_pcm_limit_hw_rates(runtime);
    1168     if (chip->device_type == DEVICE_SIS) {
    1169         // vladest workaround
    1170         if (!runtime->hw.rates)
    1171             runtime->hw.rates = SNDRV_PCM_RATE_48000;
    1172         runtime->hw.buffer_bytes_max = 64*1024;
    1173         runtime->hw.period_bytes_max = 64*1024;
    1174     }
    1175 #if 1 /* vladest */
    1176     if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
    1177         return err;
     1109static int snd_intel8x0_pcm_open(struct snd_pcm_substream *substream, struct ichdev *ichdev)
     1110{
     1111        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1112        struct snd_pcm_runtime *runtime = substream->runtime;
     1113        int err;
     1114
     1115        ichdev->substream = substream;
     1116        runtime->hw = snd_intel8x0_stream;
     1117        runtime->hw.rates = ichdev->pcm->rates;
     1118        snd_pcm_limit_hw_rates(runtime);
     1119        if (chip->device_type == DEVICE_SIS) {
     1120                runtime->hw.buffer_bytes_max = 64*1024;
     1121                runtime->hw.period_bytes_max = 64*1024;
     1122        }
     1123        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
     1124                return err;
     1125        runtime->private_data = ichdev;
     1126        return 0;
     1127}
     1128
     1129static int snd_intel8x0_playback_open(struct snd_pcm_substream *substream)
     1130{
     1131        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1132        struct snd_pcm_runtime *runtime = substream->runtime;
     1133        int err;
     1134
     1135        err = snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMOUT]);
     1136        if (err < 0)
     1137                return err;
     1138
     1139        if (chip->multi6) {
     1140                runtime->hw.channels_max = 6;
     1141                snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
     1142                                           &hw_constraints_channels6);
     1143        } else if (chip->multi4) {
     1144                runtime->hw.channels_max = 4;
     1145                snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
     1146                                           &hw_constraints_channels4);
     1147        }
     1148        if (chip->dra) {
     1149                snd_ac97_pcm_double_rate_rules(runtime);
     1150        }
     1151        if (chip->smp20bit) {
     1152                runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
     1153                snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
     1154        }
     1155        return 0;
     1156}
     1157
     1158static int snd_intel8x0_playback_close(struct snd_pcm_substream *substream)
     1159{
     1160        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1161
     1162        chip->ichd[ICHD_PCMOUT].substream = NULL;
     1163        return 0;
     1164}
     1165
     1166static int snd_intel8x0_capture_open(struct snd_pcm_substream *substream)
     1167{
     1168        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1169
     1170        return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMIN]);
     1171}
     1172
     1173static int snd_intel8x0_capture_close(struct snd_pcm_substream *substream)
     1174{
     1175        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1176
     1177        chip->ichd[ICHD_PCMIN].substream = NULL;
     1178        return 0;
     1179}
     1180
     1181static int snd_intel8x0_mic_open(struct snd_pcm_substream *substream)
     1182{
     1183        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1184
     1185        return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC]);
     1186}
     1187
     1188static int snd_intel8x0_mic_close(struct snd_pcm_substream *substream)
     1189{
     1190        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1191
     1192        chip->ichd[ICHD_MIC].substream = NULL;
     1193        return 0;
     1194}
     1195
     1196static int snd_intel8x0_mic2_open(struct snd_pcm_substream *substream)
     1197{
     1198        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1199
     1200        return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC2]);
     1201}
     1202
     1203static int snd_intel8x0_mic2_close(struct snd_pcm_substream *substream)
     1204{
     1205        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1206
     1207        chip->ichd[ICHD_MIC2].substream = NULL;
     1208        return 0;
     1209}
     1210
     1211static int snd_intel8x0_capture2_open(struct snd_pcm_substream *substream)
     1212{
     1213        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1214
     1215        return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCM2IN]);
     1216}
     1217
     1218static int snd_intel8x0_capture2_close(struct snd_pcm_substream *substream)
     1219{
     1220        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1221
     1222        chip->ichd[ICHD_PCM2IN].substream = NULL;
     1223        return 0;
     1224}
     1225
     1226static int snd_intel8x0_spdif_open(struct snd_pcm_substream *substream)
     1227{
     1228        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1229        int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
     1230
     1231        return snd_intel8x0_pcm_open(substream, &chip->ichd[idx]);
     1232}
     1233
     1234static int snd_intel8x0_spdif_close(struct snd_pcm_substream *substream)
     1235{
     1236        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1237        int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
     1238
     1239        chip->ichd[idx].substream = NULL;
     1240        return 0;
     1241}
     1242
     1243static int snd_intel8x0_ali_ac97spdifout_open(struct snd_pcm_substream *substream)
     1244{
     1245        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1246        unsigned int val;
     1247
     1248        spin_lock_irq(&chip->reg_lock);
     1249        val = igetdword(chip, ICHREG(ALI_INTERFACECR));
     1250        val |= ICH_ALI_IF_AC97SP;
     1251        iputdword(chip, ICHREG(ALI_INTERFACECR), val);
     1252        /* also needs to set ALI_SC_CODEC_SPDF correctly */
     1253        spin_unlock_irq(&chip->reg_lock);
     1254
     1255        return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_AC97SPDIFOUT]);
     1256}
     1257
     1258static int snd_intel8x0_ali_ac97spdifout_close(struct snd_pcm_substream *substream)
     1259{
     1260        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1261        unsigned int val;
     1262
     1263        chip->ichd[ALID_AC97SPDIFOUT].substream = NULL;
     1264        spin_lock_irq(&chip->reg_lock);
     1265        val = igetdword(chip, ICHREG(ALI_INTERFACECR));
     1266        val &= ~ICH_ALI_IF_AC97SP;
     1267        iputdword(chip, ICHREG(ALI_INTERFACECR), val);
     1268        spin_unlock_irq(&chip->reg_lock);
     1269
     1270        return 0;
     1271}
     1272
     1273#if 0 // NYI
     1274static int snd_intel8x0_ali_spdifin_open(struct snd_pcm_substream *substream)
     1275{
     1276        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1277
     1278        return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFIN]);
     1279}
     1280
     1281static int snd_intel8x0_ali_spdifin_close(struct snd_pcm_substream *substream)
     1282{
     1283        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1284
     1285        chip->ichd[ALID_SPDIFIN].substream = NULL;
     1286        return 0;
     1287}
     1288
     1289static int snd_intel8x0_ali_spdifout_open(struct snd_pcm_substream *substream)
     1290{
     1291        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1292
     1293        return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFOUT]);
     1294}
     1295
     1296static int snd_intel8x0_ali_spdifout_close(struct snd_pcm_substream *substream)
     1297{
     1298        struct intel8x0 *chip = snd_pcm_substream_chip(substream);
     1299
     1300        chip->ichd[ALID_SPDIFOUT].substream = NULL;
     1301        return 0;
     1302}
    11781303#endif
    1179     runtime->private_data = ichdev;
    1180     return 0;
    1181 }
    1182 
    1183 static int snd_intel8x0_playback_open(snd_pcm_substream_t * substream)
    1184 {
    1185     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1186     snd_pcm_runtime_t *runtime = substream->runtime;
    1187     int err;
    1188 
    1189     err = snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMOUT]);
    1190     if (err < 0)
    1191     {
    1192         printk("snd_intel8x0_pcm_open open error: %i\n", err);
    1193         return err;
    1194     }
    1195     if (chip->multi6) {
    1196         runtime->hw.channels_max = 6;
    1197         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels6);
    1198     } else if (chip->multi4) {
    1199         runtime->hw.channels_max = 4;
    1200         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels4);
    1201     }
    1202     if (chip->dra) {
    1203         snd_ac97_pcm_double_rate_rules(runtime);
    1204     }
    1205     if (chip->smp20bit)
    1206         runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
    1207     return 0;
    1208 }
    1209 
    1210 static int snd_intel8x0_playback_close(snd_pcm_substream_t * substream)
    1211 {
    1212     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1213 
    1214     chip->ichd[ICHD_PCMOUT].substream = NULL;
    1215     return 0;
    1216 }
    1217 
    1218 static int snd_intel8x0_capture_open(snd_pcm_substream_t * substream)
    1219 {
    1220     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1221 
    1222     return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMIN]);
    1223 }
    1224 
    1225 static int snd_intel8x0_capture_close(snd_pcm_substream_t * substream)
    1226 {
    1227     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1228 
    1229     chip->ichd[ICHD_PCMIN].substream = NULL;
    1230     return 0;
    1231 }
    1232 
    1233 static int snd_intel8x0_mic_open(snd_pcm_substream_t * substream)
    1234 {
    1235     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1236 
    1237     return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC]);
    1238 }
    1239 
    1240 static int snd_intel8x0_mic_close(snd_pcm_substream_t * substream)
    1241 {
    1242     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1243 
    1244     chip->ichd[ICHD_MIC].substream = NULL;
    1245     return 0;
    1246 }
    1247 
    1248 static int snd_intel8x0_mic2_open(snd_pcm_substream_t * substream)
    1249 {
    1250     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1251 
    1252     return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC2]);
    1253 }
    1254 
    1255 static int snd_intel8x0_mic2_close(snd_pcm_substream_t * substream)
    1256 {
    1257     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1258 
    1259     chip->ichd[ICHD_MIC2].substream = NULL;
    1260     return 0;
    1261 }
    1262 
    1263 static int snd_intel8x0_capture2_open(snd_pcm_substream_t * substream)
    1264 {
    1265     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1266 
    1267     return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCM2IN]);
    1268 }
    1269 
    1270 static int snd_intel8x0_capture2_close(snd_pcm_substream_t * substream)
    1271 {
    1272     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1273 
    1274     chip->ichd[ICHD_PCM2IN].substream = NULL;
    1275     return 0;
    1276 }
    1277 
    1278 static int snd_intel8x0_spdif_open(snd_pcm_substream_t * substream)
    1279 {
    1280     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1281     int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
    1282 
    1283     return snd_intel8x0_pcm_open(substream, &chip->ichd[idx]);
    1284 }
    1285 
    1286 static int snd_intel8x0_spdif_close(snd_pcm_substream_t * substream)
    1287 {
    1288     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1289     int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
    1290 
    1291     chip->ichd[idx].substream = NULL;
    1292     return 0;
    1293 }
    1294 
    1295 static int snd_intel8x0_ali_ac97spdifout_open(snd_pcm_substream_t * substream)
    1296 {
    1297     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1298     unsigned int val;
    1299 
    1300     spin_lock_irq(&chip->reg_lock);
    1301     val = igetdword(chip, ICHREG(ALI_INTERFACECR));
    1302     val |= ICH_ALI_IF_AC97SP;
    1303     iputdword(chip, ICHREG(ALI_INTERFACECR), val);
    1304     /* also needs to set ALI_SC_CODEC_SPDF correctly */
    1305     spin_unlock_irq(&chip->reg_lock);
    1306 
    1307     return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_AC97SPDIFOUT]);
    1308 }
    1309 
    1310 static int snd_intel8x0_ali_ac97spdifout_close(snd_pcm_substream_t * substream)
    1311 {
    1312     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1313     unsigned int val;
    1314 
    1315     chip->ichd[ALID_AC97SPDIFOUT].substream = NULL;
    1316     spin_lock_irq(&chip->reg_lock);
    1317     val = igetdword(chip, ICHREG(ALI_INTERFACECR));
    1318     val &= ~ICH_ALI_IF_AC97SP;
    1319     iputdword(chip, ICHREG(ALI_INTERFACECR), val);
    1320     spin_unlock_irq(&chip->reg_lock);
    1321 
    1322     return 0;
    1323 }
    1324 
    1325 static int snd_intel8x0_ali_spdifin_open(snd_pcm_substream_t * substream)
    1326 {
    1327     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1328 
    1329     return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFIN]);
    1330 }
    1331 
    1332 static int snd_intel8x0_ali_spdifin_close(snd_pcm_substream_t * substream)
    1333 {
    1334     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1335 
    1336     chip->ichd[ALID_SPDIFIN].substream = NULL;
    1337     return 0;
    1338 }
    1339 
    1340 #if 0 // NYI
    1341 static int snd_intel8x0_ali_spdifout_open(snd_pcm_substream_t * substream)
    1342 {
    1343     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1344 
    1345     return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFOUT]);
    1346 }
    1347 
    1348 static int snd_intel8x0_ali_spdifout_close(snd_pcm_substream_t * substream)
    1349 {
    1350     struct intel8x0 *chip = snd_pcm_substream_chip(substream);
    1351 
    1352     chip->ichd[ALID_SPDIFOUT].substream = NULL;
    1353     return 0;
    1354 }
    1355 #endif
    1356 
    1357 static snd_pcm_ops_t snd_intel8x0_playback_ops = {
     1304
     1305static struct snd_pcm_ops snd_intel8x0_playback_ops = {
    13581306        .open =         snd_intel8x0_playback_open,
    13591307        .close =        snd_intel8x0_playback_close,
     
    13661314};
    13671315
    1368 static snd_pcm_ops_t snd_intel8x0_capture_ops = {
     1316static struct snd_pcm_ops snd_intel8x0_capture_ops = {
    13691317        .open =         snd_intel8x0_capture_open,
    13701318        .close =        snd_intel8x0_capture_close,
     
    13771325};
    13781326
    1379 static snd_pcm_ops_t snd_intel8x0_capture_mic_ops = {
     1327static struct snd_pcm_ops snd_intel8x0_capture_mic_ops = {
    13801328        .open =         snd_intel8x0_mic_open,
    13811329        .close =        snd_intel8x0_mic_close,
     
    13881336};
    13891337
    1390 static snd_pcm_ops_t snd_intel8x0_capture_mic2_ops = {
     1338static struct snd_pcm_ops snd_intel8x0_capture_mic2_ops = {
    13911339        .open =         snd_intel8x0_mic2_open,
    13921340        .close =        snd_intel8x0_mic2_close,
     
    13991347};
    14001348
    1401 static snd_pcm_ops_t snd_intel8x0_capture2_ops = {
     1349static struct snd_pcm_ops snd_intel8x0_capture2_ops = {
    14021350        .open =         snd_intel8x0_capture2_open,
    14031351        .close =        snd_intel8x0_capture2_close,
     
    14101358};
    14111359
    1412 static snd_pcm_ops_t snd_intel8x0_spdif_ops = {
     1360static struct snd_pcm_ops snd_intel8x0_spdif_ops = {
    14131361        .open =         snd_intel8x0_spdif_open,
    14141362        .close =        snd_intel8x0_spdif_close,
     
    14211369};
    14221370
    1423 static snd_pcm_ops_t snd_intel8x0_ali_playback_ops = {
     1371static struct snd_pcm_ops snd_intel8x0_ali_playback_ops = {
    14241372        .open =         snd_intel8x0_playback_open,
    14251373        .close =        snd_intel8x0_playback_close,
     
    14321380};
    14331381
    1434 static snd_pcm_ops_t snd_intel8x0_ali_capture_ops = {
     1382static struct snd_pcm_ops snd_intel8x0_ali_capture_ops = {
    14351383        .open =         snd_intel8x0_capture_open,
    14361384        .close =        snd_intel8x0_capture_close,
     
    14431391};
    14441392
    1445 static snd_pcm_ops_t snd_intel8x0_ali_capture_mic_ops = {
     1393static struct snd_pcm_ops snd_intel8x0_ali_capture_mic_ops = {
    14461394        .open =         snd_intel8x0_mic_open,
    14471395        .close =        snd_intel8x0_mic_close,
     
    14541402};
    14551403
    1456 static snd_pcm_ops_t snd_intel8x0_ali_ac97spdifout_ops = {
     1404static struct snd_pcm_ops snd_intel8x0_ali_ac97spdifout_ops = {
    14571405        .open =         snd_intel8x0_ali_ac97spdifout_open,
    14581406        .close =        snd_intel8x0_ali_ac97spdifout_close,
     
    14651413};
    14661414
    1467 static snd_pcm_ops_t snd_intel8x0_ali_spdifin_ops = {
     1415#if 0 // NYI
     1416static struct snd_pcm_ops snd_intel8x0_ali_spdifin_ops = {
    14681417        .open =         snd_intel8x0_ali_spdifin_open,
    14691418        .close =        snd_intel8x0_ali_spdifin_close,
     
    14761425};
    14771426
    1478 #if 0 // NYI
    1479 static snd_pcm_ops_t snd_intel8x0_ali_spdifout_ops = {
     1427static struct snd_pcm_ops snd_intel8x0_ali_spdifout_ops = {
    14801428        .open =         snd_intel8x0_ali_spdifout_open,
    14811429        .close =        snd_intel8x0_ali_spdifout_close,
     
    14891437#endif // NYI
    14901438
    1491 
    14921439struct ich_pcm_table {
    1493     char *suffix;
    1494     snd_pcm_ops_t *playback_ops;
    1495     snd_pcm_ops_t *capture_ops;
    1496     size_t prealloc_size;
    1497     size_t prealloc_max_size;
    1498     int ac97_idx;
    1499 };
    1500 
    1501 static int __devinit snd_intel8x0_pcm1(struct intel8x0 *chip, int device, struct ich_pcm_table *rec)
    1502 {
    1503         snd_pcm_t *pcm;
     1440        char *suffix;
     1441        struct snd_pcm_ops *playback_ops;
     1442        struct snd_pcm_ops *capture_ops;
     1443        size_t prealloc_size;
     1444        size_t prealloc_max_size;
     1445        int ac97_idx;
     1446};
     1447
     1448static int __devinit snd_intel8x0_pcm1(struct intel8x0 *chip, int device,
     1449                                       struct ich_pcm_table *rec)
     1450{
     1451        struct snd_pcm *pcm;
    15041452        int err;
    15051453        char name[32];
     
    15281476        chip->pcm[device] = pcm;
    15291477
    1530         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
     1478        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
     1479                                              snd_dma_pci_data(chip->pci),
    15311480                                              rec->prealloc_size, rec->prealloc_max_size);
    15321481
     
    16111560                .suffix = "IEC958",
    16121561                .playback_ops = &snd_intel8x0_ali_ac97spdifout_ops,
    1613                 .capture_ops = &snd_intel8x0_ali_spdifin_ops,
     1562                /* .capture_ops = &snd_intel8x0_ali_spdifin_ops, */
    16141563                .prealloc_size = 64 * 1024,
    16151564                .prealloc_max_size = 128 * 1024,
     
    16261575};
    16271576
    1628 
    16291577static int __devinit snd_intel8x0_pcm(struct intel8x0 *chip)
    16301578{
    1631     int i, tblsize, device, err;
    1632     struct ich_pcm_table *tbl, *rec;
    1633 
    1634     switch (chip->device_type) {
    1635     case DEVICE_INTEL_ICH4:
    1636     case DEVICE_INTEL_ICH5:
    1637         tbl = intel_pcms;
    1638         tblsize = ARRAY_SIZE(intel_pcms);
    1639         break;
    1640     case DEVICE_NFORCE:
    1641         tbl = nforce_pcms;
    1642         tblsize = ARRAY_SIZE(nforce_pcms);
    1643         break;
    1644     case DEVICE_ALI:
    1645         tbl = ali_pcms;
    1646         tblsize = ARRAY_SIZE(ali_pcms);
    1647         break;
    1648     default:
    1649         tbl = intel_pcms;
    1650         tblsize = 2;
    1651         break;
    1652     }
    1653 
    1654     device = 0;
    1655     for (i = 0; i < tblsize; i++) {
    1656         rec = tbl + i;
    1657         if (i > 0 && rec->ac97_idx) {
    1658             /* activate PCM only when associated AC'97 codec */
    1659             if (! chip->ichd[rec->ac97_idx].pcm)
    1660                 continue;
    1661         }
    1662         err = snd_intel8x0_pcm1(chip, device, rec);
    1663         if (err < 0)
    1664             return err;
    1665         device++;
    1666     }
    1667 
    1668     chip->pcm_devs = device;
    1669     return 0;
    1670 }
    1671 
     1579        int i, tblsize, device, err;
     1580        struct ich_pcm_table *tbl, *rec;
     1581
     1582        switch (chip->device_type) {
     1583        case DEVICE_INTEL_ICH4:
     1584                tbl = intel_pcms;
     1585                tblsize = ARRAY_SIZE(intel_pcms);
     1586                if (spdif_aclink)
     1587                        tblsize--;
     1588                break;
     1589        case DEVICE_NFORCE:
     1590                tbl = nforce_pcms;
     1591                tblsize = ARRAY_SIZE(nforce_pcms);
     1592                if (spdif_aclink)
     1593                        tblsize--;
     1594                break;
     1595        case DEVICE_ALI:
     1596                tbl = ali_pcms;
     1597                tblsize = ARRAY_SIZE(ali_pcms);
     1598                break;
     1599        default:
     1600                tbl = intel_pcms;
     1601                tblsize = 2;
     1602                break;
     1603        }
     1604
     1605        device = 0;
     1606        for (i = 0; i < tblsize; i++) {
     1607                rec = tbl + i;
     1608                if (i > 0 && rec->ac97_idx) {
     1609                        /* activate PCM only when associated AC'97 codec */
     1610                        if (! chip->ichd[rec->ac97_idx].pcm)
     1611                                continue;
     1612                }
     1613                err = snd_intel8x0_pcm1(chip, device, rec);
     1614                if (err < 0)
     1615                        return err;
     1616                device++;
     1617        }
     1618
     1619        chip->pcm_devs = device;
     1620        return 0;
     1621}
     1622       
    16721623
    16731624/*
     
    16751626 */
    16761627
    1677 static void snd_intel8x0_mixer_free_ac97_bus(ac97_bus_t *bus)
    1678 {
    1679     struct intel8x0 *chip = bus->private_data;
    1680     chip->ac97_bus = NULL;
    1681 }
    1682 
    1683 static void snd_intel8x0_mixer_free_ac97(ac97_t *ac97)
    1684 {
    1685     struct intel8x0 *chip = ac97->private_data;
    1686     chip->ac97[ac97->num] = NULL;
     1628static void snd_intel8x0_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
     1629{
     1630        struct intel8x0 *chip = bus->private_data;
     1631        chip->ac97_bus = NULL;
     1632}
     1633
     1634static void snd_intel8x0_mixer_free_ac97(struct snd_ac97 *ac97)
     1635{
     1636        struct intel8x0 *chip = ac97->private_data;
     1637        chip->ac97[ac97->num] = NULL;
    16871638}
    16881639
    16891640static struct ac97_pcm ac97_pcm_defs[] __devinitdata = {
    1690     /* front PCM */
    1691     {
    1692         .exclusive = 1,
    1693         .r =
    1694         {
    1695             {
    1696                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
    1697                     (1 << AC97_SLOT_PCM_RIGHT) |
    1698                     (1 << AC97_SLOT_PCM_CENTER) |
    1699                     (1 << AC97_SLOT_PCM_SLEFT) |
    1700                     (1 << AC97_SLOT_PCM_SRIGHT) |
    1701                     (1 << AC97_SLOT_LFE)
    1702             },
    1703             {
    1704                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
    1705                     (1 << AC97_SLOT_PCM_RIGHT) |
    1706                     (1 << AC97_SLOT_PCM_LEFT_0) |
    1707                     (1 << AC97_SLOT_PCM_RIGHT_0)
    1708             }
    1709         }
    1710     },
    1711     /* PCM IN #1 */
    1712     {
    1713         .stream = 1,
    1714         .exclusive = 1,
    1715         .r = {  {
    1716             .slots = (1 << AC97_SLOT_PCM_LEFT) |
    1717                 (1 << AC97_SLOT_PCM_RIGHT)
    1718         }
    1719         }
    1720     },
    1721     /* MIC IN #1 */
    1722     {
    1723         .stream = 1,
    1724         .exclusive = 1,
    1725         .r = {  {
    1726             .slots = (1 << AC97_SLOT_MIC)
    1727         }
    1728         }
    1729     },
    1730     /* S/PDIF PCM */
    1731     {
    1732         .exclusive = 1,
    1733         .spdif = 1,
    1734         .r = {  {
    1735             .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
    1736                 (1 << AC97_SLOT_SPDIF_RIGHT2)
    1737         }
    1738         }
    1739     },
    1740     /* PCM IN #2 */
    1741     {
    1742         .stream = 1,
    1743         .exclusive = 1,
    1744         .r = {  {
    1745             .slots = (1 << AC97_SLOT_PCM_LEFT) |
    1746                 (1 << AC97_SLOT_PCM_RIGHT)
    1747         }
    1748         }
    1749     },
    1750     /* MIC IN #2 */
    1751     {
    1752         .stream = 1,
    1753         .exclusive = 1,
    1754         .r = {  {
    1755             .slots = (1 << AC97_SLOT_MIC)
    1756         }
    1757         }
    1758     },
     1641        /* front PCM */
     1642        {
     1643                .exclusive = 1,
     1644                .r =
     1645                 {     
     1646                        {
     1647                                .slots = (1 << AC97_SLOT_PCM_LEFT) |
     1648                                        (1 << AC97_SLOT_PCM_RIGHT) |
     1649                                        (1 << AC97_SLOT_PCM_CENTER) |
     1650                                        (1 << AC97_SLOT_PCM_SLEFT) |
     1651                                        (1 << AC97_SLOT_PCM_SRIGHT) |
     1652                                        (1 << AC97_SLOT_LFE)
     1653                        },
     1654                        {
     1655                                .slots = (1 << AC97_SLOT_PCM_LEFT) |
     1656                                        (1 << AC97_SLOT_PCM_RIGHT) |
     1657                                        (1 << AC97_SLOT_PCM_LEFT_0) |
     1658                                        (1 << AC97_SLOT_PCM_RIGHT_0)
     1659                        }
     1660                }
     1661        },
     1662        /* PCM IN #1 */
     1663        {
     1664                .stream = 1,
     1665                .exclusive = 1,
     1666                .r = {  {
     1667                                .slots = (1 << AC97_SLOT_PCM_LEFT) |
     1668                                        (1 << AC97_SLOT_PCM_RIGHT)
     1669                        }
     1670                }
     1671        },
     1672        /* MIC IN #1 */
     1673        {
     1674                .stream = 1,
     1675                .exclusive = 1,
     1676                .r = {  {
     1677                                .slots = (1 << AC97_SLOT_MIC)
     1678                        }
     1679                }
     1680        },
     1681        /* S/PDIF PCM */
     1682        {
     1683                .exclusive = 1,
     1684                .spdif = 1,
     1685                .r = {  {
     1686                                .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
     1687                                        (1 << AC97_SLOT_SPDIF_RIGHT2)
     1688                        }
     1689                }
     1690        },
     1691        /* PCM IN #2 */
     1692        {
     1693                .stream = 1,
     1694                .exclusive = 1,
     1695                .r = {  {
     1696                                .slots = (1 << AC97_SLOT_PCM_LEFT) |
     1697                                        (1 << AC97_SLOT_PCM_RIGHT)
     1698                        }
     1699                }
     1700        },
     1701        /* MIC IN #2 */
     1702        {
     1703                .stream = 1,
     1704                .exclusive = 1,
     1705                .r = {  {
     1706                                .slots = (1 << AC97_SLOT_MIC)
     1707                        }
     1708                }
     1709        },
    17591710};
    17601711
     
    17841735                .type = AC97_TUNE_ALC_JACK
    17851736        },
    1786         {
    1787             .subvendor = 0x1014,
    1788             .subdevice = 0x0267,
    1789             .name = "IBM NetVista A30p",    /* AD1981B */
    1790             .type = AC97_TUNE_HP_ONLY
    1791         },
    1792         {
    1793             .subvendor = 0x1025,
    1794             .subdevice = 0x0083,
    1795             .name = "Acer Aspire 3003LCi",
    1796             .type = AC97_TUNE_HP_ONLY
    1797         },
    1798         {
     1737        {
     1738                .subvendor = 0x1014,
     1739                .subdevice = 0x0267,
     1740                .name = "IBM NetVista A30p",    /* AD1981B */
     1741                .type = AC97_TUNE_HP_ONLY
     1742        },
     1743        {
     1744                .subvendor = 0x1025,
     1745                .subdevice = 0x0082,
     1746                .name = "Acer Travelmate 2310",
     1747                .type = AC97_TUNE_HP_ONLY
     1748        },
     1749        {
     1750                .subvendor = 0x1025,
     1751                .subdevice = 0x0083,
     1752                .name = "Acer Aspire 3003LCi",
     1753                .type = AC97_TUNE_HP_ONLY
     1754        },
     1755        {
    17991756                .subvendor = 0x1028,
    18001757                .subdevice = 0x00d8,
     
    18331790        },
    18341791        {
    1835             .subvendor = 0x1028,
    1836             .subdevice = 0x0151,
    1837             .name = "Dell Optiplex GX270",  /* AD1981B */
    1838             .type = AC97_TUNE_HP_ONLY
    1839         },
    1840         {
    1841             .subvendor = 0x1028,
    1842             .subdevice = 0x014e,
    1843             .name = "Dell D800", /* STAC9750/51 */
    1844             .type = AC97_TUNE_HP_ONLY
     1792                .subvendor = 0x1028,
     1793                .subdevice = 0x0151,
     1794                .name = "Dell Optiplex GX270",  /* AD1981B */
     1795                .type = AC97_TUNE_HP_ONLY
     1796        },
     1797        {
     1798                .subvendor = 0x1028,
     1799                .subdevice = 0x014e,
     1800                .name = "Dell D800", /* STAC9750/51 */
     1801                .type = AC97_TUNE_HP_ONLY
    18451802        },
    18461803        {
     
    18491806                .name = "Dell Unknown", /* STAC9750/51 */
    18501807                .type = AC97_TUNE_HP_ONLY
    1851         },
    1852         {
    1853             .subvendor = 0x1028,
    1854             .subdevice = 0x0191,
    1855             .name = "Dell Inspiron 8600",
    1856             .type = AC97_TUNE_HP_ONLY
    1857         },
     1808        },
     1809        {
     1810                .subvendor = 0x1028,
     1811                .subdevice = 0x0186,
     1812                .name = "Dell Latitude D810", /* cf. Malone #41015 */
     1813                .type = AC97_TUNE_HP_MUTE_LED
     1814        },
     1815        {
     1816                .subvendor = 0x1028,
     1817                .subdevice = 0x0188,
     1818                .name = "Dell Inspiron 6000",
     1819                .type = AC97_TUNE_HP_MUTE_LED /* cf. Malone #41015 */
     1820        },
     1821        {
     1822                .subvendor = 0x1028,
     1823                .subdevice = 0x0191,
     1824                .name = "Dell Inspiron 8600",
     1825                .type = AC97_TUNE_HP_ONLY
     1826        },
    18581827        {
    18591828                .subvendor = 0x103c,
     
    18721841                .subdevice = 0x088c,
    18731842                .name = "HP nc8000",
    1874                 .type = AC97_TUNE_MUTE_LED
     1843                .type = AC97_TUNE_HP_MUTE_LED
    18751844        },
    18761845        {
     
    18811850        },
    18821851        {
    1883             .subvendor = 0x103c,
    1884             .subdevice = 0x0934,
    1885             .name = "HP nx8220",
    1886             .type = AC97_TUNE_MUTE_LED
    1887         },
    1888         {
    1889             .subvendor = 0x103c,
     1852                .subvendor = 0x103c,
     1853                .subdevice = 0x0934,
     1854                .name = "HP nx8220",
     1855                .type = AC97_TUNE_MUTE_LED
     1856        },
     1857        {
     1858                .subvendor = 0x103c,
    18901859                .subdevice = 0x129d,
    18911860                .name = "HP xw8000",
     
    18931862        },
    18941863        {
    1895             .subvendor = 0x103c,
    1896             .subdevice = 0x0938,
    1897             .name = "HP nc4200",
    1898             .type = AC97_TUNE_HP_MUTE_LED
    1899         },
    1900         {
    1901             .subvendor = 0x103c,
    1902             .subdevice = 0x099c,
    1903             .name = "HP nc6120",
    1904             .type = AC97_TUNE_HP_MUTE_LED
    1905         },
    1906         {
    1907             .subvendor = 0x103c,
    1908             .subdevice = 0x0944,
    1909             .name = "HP nx6110/nc6120",
    1910             .type = AC97_TUNE_HP_MUTE_LED
    1911         },
    1912         {
    1913             .subvendor = 0x103c,
    1914             .subdevice = 0x0934,
    1915             .name = "HP nc8220",
    1916             .type = AC97_TUNE_HP_MUTE_LED
    1917         },
    1918         {
    1919             .subvendor = 0x103c,
     1864                .subvendor = 0x103c,
     1865                .subdevice = 0x0938,
     1866                .name = "HP nc4200",
     1867                .type = AC97_TUNE_HP_MUTE_LED
     1868        },
     1869        {
     1870                .subvendor = 0x103c,
     1871                .subdevice = 0x099c,
     1872                .name = "HP nx6110/nc6120",
     1873                .type = AC97_TUNE_HP_MUTE_LED
     1874        },
     1875        {
     1876                .subvendor = 0x103c,
     1877                .subdevice = 0x0944,
     1878                .name = "HP nc6220",
     1879                .type = AC97_TUNE_HP_MUTE_LED
     1880        },
     1881        {
     1882                .subvendor = 0x103c,
     1883                .subdevice = 0x0934,
     1884                .name = "HP nc8220",
     1885                .type = AC97_TUNE_HP_MUTE_LED
     1886        },
     1887        {
     1888                .subvendor = 0x103c,
    19201889                .subdevice = 0x12f1,
    19211890                .name = "HP xw8200",    /* AD1981B*/
     
    19641933                .type = AC97_TUNE_HP_ONLY
    19651934        },
    1966         {
    1967             .subvendor = 0x10cf,
    1968             .subdevice = 0x12ec,
    1969             .name = "Fujitsu-Siemens 4010",
    1970             .type = AC97_TUNE_HP_ONLY
    1971         },
    1972         {
    1973             .subvendor = 0x10cf,
    1974             .subdevice = 0x12f2,
    1975             .name = "Fujitsu-Siemens Celsius H320",
    1976             .type = AC97_TUNE_SWAP_HP
    1977         },
    1978         {
     1935        {
     1936                .subvendor = 0x10cf,
     1937                .subdevice = 0x127e,
     1938                .name = "Fujitsu Lifebook C1211D",
     1939                .type = AC97_TUNE_HP_ONLY
     1940        },
     1941        {
     1942                .subvendor = 0x10cf,
     1943                .subdevice = 0x12ec,
     1944                .name = "Fujitsu-Siemens 4010",
     1945                .type = AC97_TUNE_HP_ONLY
     1946        },
     1947        {
     1948                .subvendor = 0x10cf,
     1949                .subdevice = 0x12f2,
     1950                .name = "Fujitsu-Siemens Celsius H320",
     1951                .type = AC97_TUNE_SWAP_HP
     1952        },
     1953        {
    19791954                .subvendor = 0x10f1,
    19801955                .subdevice = 0x2665,
     
    19881963                .type = AC97_TUNE_HP_ONLY
    19891964        },
    1990         {
    1991             .subvendor = 0x10f1,
    1992             .subdevice = 0x2895,
    1993             .name = "Tyan Thunder K8WE",
    1994             .type = AC97_TUNE_HP_ONLY
     1965        {
     1966                .subvendor = 0x10f1,
     1967                .subdevice = 0x2895,
     1968                .name = "Tyan Thunder K8WE",
     1969                .type = AC97_TUNE_HP_ONLY
     1970        },
     1971        {
     1972                .subvendor = 0x10f7,
     1973                .subdevice = 0x834c,
     1974                .name = "Panasonic CF-R4",
     1975                .type = AC97_TUNE_HP_ONLY,
    19951976        },
    19961977        {
     
    20171998                .name = "Fujitsu-Siemens D1522",        /* AD1981 */
    20181999                .type = AC97_TUNE_HP_ONLY
    2019         },
    2020         {
    2021             .subvendor = 0x8086,
    2022             .subdevice = 0x0104,
    2023             .name = "Intel D845GEBV2",              /* AD1981B */
    2024             .type = AC97_TUNE_HP_ONLY
    2025         },
     2000        },
    20262001        {
    20272002                .subvendor = 0x8086,
     
    20822057};
    20832058
    2084 static int __devinit snd_intel8x0_mixer(struct intel8x0 *chip, int ac97_clock, char *quirk_override)
    2085 {
    2086     ac97_bus_t *pbus;
    2087     ac97_template_t ac97;
    2088     int err;
    2089     unsigned int i, codecs;
    2090     unsigned int glob_sta = 0;
    2091     ac97_bus_ops_t *ops;
    2092     static struct snd_ac97_bus_ops standard_bus_ops = {
    2093         .write = snd_intel8x0_codec_write,
    2094         .read = snd_intel8x0_codec_read,
    2095     };
    2096     static struct snd_ac97_bus_ops ali_bus_ops = {
    2097         .write = snd_intel8x0_ali_codec_write,
    2098         .read = snd_intel8x0_ali_codec_read,
    2099     };
    2100 
    2101     chip->spdif_idx = -1; /* use PCMOUT (or disabled) */
    2102     switch (chip->device_type) {
    2103     case DEVICE_NFORCE:
    2104         chip->spdif_idx = NVD_SPBAR;
    2105         break;
    2106     case DEVICE_ALI:
    2107         chip->spdif_idx = ALID_AC97SPDIFOUT;
    2108         break;
    2109     case DEVICE_INTEL_ICH4:
    2110     case DEVICE_INTEL_ICH5:
    2111         chip->spdif_idx = ICHD_SPBAR;
    2112         break;
    2113     };
    2114 
    2115     chip->in_ac97_init = 1;
    2116 
    2117     memset(&ac97, 0, sizeof(ac97));
    2118     ac97.private_data = chip;
    2119     ac97.private_free = snd_intel8x0_mixer_free_ac97;
    2120     ac97.scaps = AC97_SCAP_SKIP_MODEM;
    2121     if (chip->device_type != DEVICE_ALI) {
    2122         glob_sta = igetdword(chip, ICHREG(GLOB_STA));
    2123         ops = &standard_bus_ops;
    2124         chip->in_sdin_init = 1;
    2125         codecs = 0;
    2126         for (i = 0; i < chip->max_codecs; i++) {
    2127             if (! (glob_sta & chip->codec_bit[i]))
    2128                 continue;
    2129             if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2130                 chip->device_type == DEVICE_INTEL_ICH5) {
    2131                 snd_intel8x0_codec_read_test(chip, codecs);
    2132                 chip->ac97_sdin[codecs] =
    2133                     igetbyte(chip, ICHREG(SDM)) & ICH_LDI_MASK;
    2134                 snd_assert(chip->ac97_sdin[codecs] < 3,
    2135                            chip->ac97_sdin[codecs] = 0);
    2136             } else
    2137                 chip->ac97_sdin[codecs] = i;
    2138             codecs++;
    2139             chip->in_sdin_init = 1;
    2140         }
    2141         chip->in_sdin_init = 0;
    2142         if (! codecs)
    2143             codecs = 1;
    2144     } else {
    2145         ops = &ali_bus_ops;
    2146         codecs = 1;
    2147         /* detect the secondary codec */
    2148         for (i = 0; i < 100; i++) {
    2149             unsigned int reg = igetdword(chip, ICHREG(ALI_RTSR));
    2150             if (reg & 0x40) {
    2151                 codecs = 2;
    2152                 break;
    2153             }
    2154             iputdword(chip, ICHREG(ALI_RTSR), reg | 0x40);
    2155             udelay(1);
    2156         }
    2157     }
    2158     if ((err = snd_ac97_bus(chip->card, 0, ops, chip, &pbus)) < 0)
    2159         goto __err;
    2160     pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
    2161     if (ac97_clock >= 8000 && ac97_clock <= 48000)
    2162         pbus->clock = ac97_clock;
    2163     /* FIXME: my test board doesn't work well with VRA... */
    2164     if (chip->device_type == DEVICE_ALI)
    2165         pbus->no_vra = 1;
    2166     else
    2167         pbus->dra = 1;
    2168     chip->ac97_bus = pbus;
    2169     chip->ncodecs = codecs;
    2170 
    2171     ac97.pci = chip->pci;
    2172     for (i = 0; i < codecs; i++) {
    2173         ac97.num = i;
    2174         if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
    2175             if (err != -EACCES)
    2176                 snd_printk(KERN_ERR "Unable to initialize codec #%d\n", i);
    2177             if (i == 0)
    2178                 goto __err;
    2179             continue;
    2180         }
    2181     }
    2182     /* tune up the primary codec */
    2183     snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
    2184     /* enable separate SDINs for ICH4 */
    2185     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2186         chip->device_type == DEVICE_INTEL_ICH5)
    2187         pbus->isdin = 1;
    2188     /* find the available PCM streams */
    2189     i = ARRAY_SIZE(ac97_pcm_defs);
    2190     if (chip->device_type != DEVICE_INTEL_ICH4 &&
    2191         chip->device_type != DEVICE_INTEL_ICH5)
    2192         i -= 2;         /* do not allocate PCM2IN and MIC2 */
    2193     if (chip->spdif_idx < 0)
    2194         i--;            /* do not allocate S/PDIF */
    2195     err = snd_ac97_pcm_assign(pbus, i, ac97_pcm_defs);
    2196     if (err < 0)
    2197         goto __err;
    2198     chip->ichd[ICHD_PCMOUT].pcm = &pbus->pcms[0];
    2199     chip->ichd[ICHD_PCMIN].pcm = &pbus->pcms[1];
    2200     chip->ichd[ICHD_MIC].pcm = &pbus->pcms[2];
    2201     if (chip->spdif_idx >= 0)
    2202         chip->ichd[chip->spdif_idx].pcm = &pbus->pcms[3];
    2203     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2204         chip->device_type == DEVICE_INTEL_ICH5) {
    2205         chip->ichd[ICHD_PCM2IN].pcm = &pbus->pcms[4];
    2206         chip->ichd[ICHD_MIC2].pcm = &pbus->pcms[5];
    2207     }
    2208     /* enable separate SDINs for ICH4 */
    2209     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2210         chip->device_type == DEVICE_INTEL_ICH5) {
    2211         struct ac97_pcm *pcm = chip->ichd[ICHD_PCM2IN].pcm;
    2212         u8 tmp = igetbyte(chip, ICHREG(SDM));
    2213         tmp &= ~(ICH_DI2L_MASK|ICH_DI1L_MASK);
    2214         if (pcm) {
    2215             tmp |= ICH_SE;      /* steer enable for multiple SDINs */
    2216             tmp |= chip->ac97_sdin[0] << ICH_DI1L_SHIFT;
    2217             for (i = 1; i < 4; i++) {
    2218                 if (pcm->r[0].codec[i]) {
    2219                     tmp |= chip->ac97_sdin[pcm->r[0].codec[1]->num] << ICH_DI2L_SHIFT;
    2220                     break;
    2221                 }
    2222             }
    2223         } else {
    2224             tmp &= ~ICH_SE; /* steer disable */
    2225         }
    2226         iputbyte(chip, ICHREG(SDM), tmp);
    2227     }
    2228     if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
    2229         chip->multi4 = 1;
    2230         if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE))
    2231             chip->multi6 = 1;
    2232     }
    2233     if (pbus->pcms[0].r[1].rslots[0]) {
    2234         chip->dra = 1;
    2235     }
    2236     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2237         chip->device_type == DEVICE_INTEL_ICH5) {
    2238         if ((igetdword(chip, ICHREG(GLOB_STA)) & ICH_SAMPLE_CAP) == ICH_SAMPLE_16_20)
    2239             chip->smp20bit = 1;
    2240     }
    2241     if (chip->device_type == DEVICE_NFORCE) {
    2242         /* 48kHz only */
    2243         chip->ichd[chip->spdif_idx].pcm->rates = SNDRV_PCM_RATE_48000;
    2244     }
    2245 
    2246     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2247         chip->device_type == DEVICE_INTEL_ICH5) {
    2248         /* use slot 10/11 for SPDIF */
    2249         u32 val;
    2250         val = igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK;
    2251         val |= ICH_PCM_SPDIF_1011;
    2252         iputdword(chip, ICHREG(GLOB_CNT), val);
    2253         snd_ac97_update_bits(chip->ac97[0], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
    2254     }
    2255     chip->in_ac97_init = 0;
    2256     return 0;
    2257 
    2258     __err:
    2259         /* clear the cold-reset bit for the next chance */
    2260         if (chip->device_type != DEVICE_ALI)
    2261             iputdword(chip, ICHREG(GLOB_CNT), igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
    2262         return err;
     2059static int __devinit snd_intel8x0_mixer(struct intel8x0 *chip, int ac97_clock,
     2060                                        const char *quirk_override)
     2061{
     2062        struct snd_ac97_bus *pbus;
     2063        struct snd_ac97_template ac97;
     2064        int err;
     2065        unsigned int i, codecs;
     2066        unsigned int glob_sta = 0;
     2067        struct snd_ac97_bus_ops *ops;
     2068        static struct snd_ac97_bus_ops standard_bus_ops = {
     2069                .write = snd_intel8x0_codec_write,
     2070                .read = snd_intel8x0_codec_read,
     2071        };
     2072        static struct snd_ac97_bus_ops ali_bus_ops = {
     2073                .write = snd_intel8x0_ali_codec_write,
     2074                .read = snd_intel8x0_ali_codec_read,
     2075        };
     2076
     2077        chip->spdif_idx = -1; /* use PCMOUT (or disabled) */
     2078        if (!spdif_aclink) {
     2079                switch (chip->device_type) {
     2080                case DEVICE_NFORCE:
     2081                        chip->spdif_idx = NVD_SPBAR;
     2082                        break;
     2083                case DEVICE_ALI:
     2084                        chip->spdif_idx = ALID_AC97SPDIFOUT;
     2085                        break;
     2086                case DEVICE_INTEL_ICH4:
     2087                        chip->spdif_idx = ICHD_SPBAR;
     2088                        break;
     2089                };
     2090        }
     2091
     2092        chip->in_ac97_init = 1;
     2093       
     2094        memset(&ac97, 0, sizeof(ac97));
     2095        ac97.private_data = chip;
     2096        ac97.private_free = snd_intel8x0_mixer_free_ac97;
     2097        ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
     2098        if (chip->xbox)
     2099                ac97.scaps |= AC97_SCAP_DETECT_BY_VENDOR;
     2100        if (chip->device_type != DEVICE_ALI) {
     2101                glob_sta = igetdword(chip, ICHREG(GLOB_STA));
     2102                ops = &standard_bus_ops;
     2103                chip->in_sdin_init = 1;
     2104                codecs = 0;
     2105                for (i = 0; i < chip->max_codecs; i++) {
     2106                        if (! (glob_sta & chip->codec_bit[i]))
     2107                                continue;
     2108                        if (chip->device_type == DEVICE_INTEL_ICH4) {
     2109                                snd_intel8x0_codec_read_test(chip, codecs);
     2110                                chip->ac97_sdin[codecs] =
     2111                                        igetbyte(chip, ICHREG(SDM)) & ICH_LDI_MASK;
     2112                                snd_assert(chip->ac97_sdin[codecs] < 3,
     2113                                           chip->ac97_sdin[codecs] = 0);
     2114                        } else
     2115                                chip->ac97_sdin[codecs] = i;
     2116                        codecs++;
     2117                }
     2118                chip->in_sdin_init = 0;
     2119                if (! codecs)
     2120                        codecs = 1;
     2121        } else {
     2122                ops = &ali_bus_ops;
     2123                codecs = 1;
     2124                /* detect the secondary codec */
     2125                for (i = 0; i < 100; i++) {
     2126                        unsigned int reg = igetdword(chip, ICHREG(ALI_RTSR));
     2127                        if (reg & 0x40) {
     2128                                codecs = 2;
     2129                                break;
     2130                        }
     2131                        iputdword(chip, ICHREG(ALI_RTSR), reg | 0x40);
     2132                        udelay(1);
     2133                }
     2134        }
     2135        if ((err = snd_ac97_bus(chip->card, 0, ops, chip, &pbus)) < 0)
     2136                goto __err;
     2137        pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
     2138        if (ac97_clock >= 8000 && ac97_clock <= 48000)
     2139                pbus->clock = ac97_clock;
     2140        /* FIXME: my test board doesn't work well with VRA... */
     2141        if (chip->device_type == DEVICE_ALI)
     2142                pbus->no_vra = 1;
     2143        else
     2144                pbus->dra = 1;
     2145        chip->ac97_bus = pbus;
     2146        chip->ncodecs = codecs;
     2147
     2148        ac97.pci = chip->pci;
     2149        for (i = 0; i < codecs; i++) {
     2150                ac97.num = i;
     2151                if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
     2152                        if (err != -EACCES)
     2153                                snd_printk(KERN_ERR "Unable to initialize codec #%d\n", i);
     2154                        if (i == 0)
     2155                                goto __err;
     2156                }
     2157        }
     2158        /* tune up the primary codec */
     2159        snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
     2160        /* enable separate SDINs for ICH4 */
     2161        if (chip->device_type == DEVICE_INTEL_ICH4)
     2162                pbus->isdin = 1;
     2163        /* find the available PCM streams */
     2164        i = ARRAY_SIZE(ac97_pcm_defs);
     2165        if (chip->device_type != DEVICE_INTEL_ICH4)
     2166                i -= 2;         /* do not allocate PCM2IN and MIC2 */
     2167        if (chip->spdif_idx < 0)
     2168                i--;            /* do not allocate S/PDIF */
     2169        err = snd_ac97_pcm_assign(pbus, i, ac97_pcm_defs);
     2170        if (err < 0)
     2171                goto __err;
     2172        chip->ichd[ICHD_PCMOUT].pcm = &pbus->pcms[0];
     2173        chip->ichd[ICHD_PCMIN].pcm = &pbus->pcms[1];
     2174        chip->ichd[ICHD_MIC].pcm = &pbus->pcms[2];
     2175        if (chip->spdif_idx >= 0)
     2176                chip->ichd[chip->spdif_idx].pcm = &pbus->pcms[3];
     2177        if (chip->device_type == DEVICE_INTEL_ICH4) {
     2178                chip->ichd[ICHD_PCM2IN].pcm = &pbus->pcms[4];
     2179                chip->ichd[ICHD_MIC2].pcm = &pbus->pcms[5];
     2180        }
     2181        /* enable separate SDINs for ICH4 */
     2182        if (chip->device_type == DEVICE_INTEL_ICH4) {
     2183                struct ac97_pcm *pcm = chip->ichd[ICHD_PCM2IN].pcm;
     2184                u8 tmp = igetbyte(chip, ICHREG(SDM));
     2185                tmp &= ~(ICH_DI2L_MASK|ICH_DI1L_MASK);
     2186                if (pcm) {
     2187                        tmp |= ICH_SE;  /* steer enable for multiple SDINs */
     2188                        tmp |= chip->ac97_sdin[0] << ICH_DI1L_SHIFT;
     2189                        for (i = 1; i < 4; i++) {
     2190                                if (pcm->r[0].codec[i]) {
     2191                                        tmp |= chip->ac97_sdin[pcm->r[0].codec[1]->num] << ICH_DI2L_SHIFT;
     2192                                        break;
     2193                                }
     2194                        }
     2195                } else {
     2196                        tmp &= ~ICH_SE; /* steer disable */
     2197                }
     2198                iputbyte(chip, ICHREG(SDM), tmp);
     2199        }
     2200        if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
     2201                chip->multi4 = 1;
     2202                if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE))
     2203                        chip->multi6 = 1;
     2204        }
     2205        if (pbus->pcms[0].r[1].rslots[0]) {
     2206                chip->dra = 1;
     2207        }
     2208        if (chip->device_type == DEVICE_INTEL_ICH4) {
     2209                if ((igetdword(chip, ICHREG(GLOB_STA)) & ICH_SAMPLE_CAP) == ICH_SAMPLE_16_20)
     2210                        chip->smp20bit = 1;
     2211        }
     2212        if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
     2213                /* 48kHz only */
     2214                chip->ichd[chip->spdif_idx].pcm->rates = SNDRV_PCM_RATE_48000;
     2215        }
     2216        if (chip->device_type == DEVICE_INTEL_ICH4 && !spdif_aclink) {
     2217                /* use slot 10/11 for SPDIF */
     2218                u32 val;
     2219                val = igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK;
     2220                val |= ICH_PCM_SPDIF_1011;
     2221                iputdword(chip, ICHREG(GLOB_CNT), val);
     2222                snd_ac97_update_bits(chip->ac97[0], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
     2223        }
     2224        chip->in_ac97_init = 0;
     2225        return 0;
     2226
     2227 __err:
     2228        /* clear the cold-reset bit for the next chance */
     2229        if (chip->device_type != DEVICE_ALI)
     2230                iputdword(chip, ICHREG(GLOB_CNT),
     2231                          igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
     2232        return err;
    22632233}
    22642234
     
    22702240static void do_ali_reset(struct intel8x0 *chip)
    22712241{
    2272     iputdword(chip, ICHREG(ALI_SCR), ICH_ALI_SC_RESET);
    2273     iputdword(chip, ICHREG(ALI_FIFOCR1), 0x83838383);
    2274     iputdword(chip, ICHREG(ALI_FIFOCR2), 0x83838383);
    2275     iputdword(chip, ICHREG(ALI_FIFOCR3), 0x83838383);
    2276     iputdword(chip, ICHREG(ALI_INTERFACECR),
    2277               ICH_ALI_IF_PI|ICH_ALI_IF_PO);
    2278     iputdword(chip, ICHREG(ALI_INTERRUPTCR), 0x00000000);
    2279     iputdword(chip, ICHREG(ALI_INTERRUPTSR), 0x00000000);
    2280 }
    2281 
    2282 #define do_delay(chip) do {\
    2283     set_current_state(TASK_UNINTERRUPTIBLE);\
    2284     schedule_timeout(1);\
    2285     } while (0)
     2242        iputdword(chip, ICHREG(ALI_SCR), ICH_ALI_SC_RESET);
     2243        iputdword(chip, ICHREG(ALI_FIFOCR1), 0x83838383);
     2244        iputdword(chip, ICHREG(ALI_FIFOCR2), 0x83838383);
     2245        iputdword(chip, ICHREG(ALI_FIFOCR3), 0x83838383);
     2246        iputdword(chip, ICHREG(ALI_INTERFACECR),
     2247                  ICH_ALI_IF_PI|ICH_ALI_IF_PO);
     2248        iputdword(chip, ICHREG(ALI_INTERRUPTCR), 0x00000000);
     2249        iputdword(chip, ICHREG(ALI_INTERRUPTSR), 0x00000000);
     2250}
    22862251
    22872252static int snd_intel8x0_ich_chip_init(struct intel8x0 *chip, int probing)
    22882253{
    2289     unsigned long end_time;
    2290     unsigned int cnt, status, nstatus,i;
    2291 
    2292 #ifdef DEBUG
    2293     dprintf(("ICH chip init begins"));
     2254        unsigned long end_time;
     2255        unsigned int cnt, status, nstatus;
     2256       
     2257        /* put logic to right state */
     2258        /* first clear status bits */
     2259        status = ICH_RCS | ICH_MCINT | ICH_POINT | ICH_PIINT;
     2260        if (chip->device_type == DEVICE_NFORCE)
     2261                status |= ICH_NVSPINT;
     2262        cnt = igetdword(chip, ICHREG(GLOB_STA));
     2263        iputdword(chip, ICHREG(GLOB_STA), cnt & status);
     2264
     2265        /* ACLink on, 2 channels */
     2266        cnt = igetdword(chip, ICHREG(GLOB_CNT));
     2267        cnt &= ~(ICH_ACLINK | ICH_PCM_246_MASK);
     2268#ifdef CONFIG_SND_AC97_POWER_SAVE
     2269        /* do cold reset - the full ac97 powerdown may leave the controller
     2270         * in a warm state but actually it cannot communicate with the codec.
     2271         */
     2272        iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_AC97COLD);
     2273        cnt = igetdword(chip, ICHREG(GLOB_CNT));
     2274        udelay(10);
     2275        iputdword(chip, ICHREG(GLOB_CNT), cnt | ICH_AC97COLD);
     2276        msleep(1);
     2277#else
     2278        /* finish cold or do warm reset */
     2279        cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
     2280        iputdword(chip, ICHREG(GLOB_CNT), cnt);
     2281        end_time = (jiffies + (HZ / 4)) + 1;
     2282        do {
     2283                if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
     2284                        goto __ok;
     2285                schedule_timeout_uninterruptible(1);
     2286        } while (time_after_eq(end_time, jiffies));
     2287        snd_printk(KERN_ERR "AC'97 warm reset still in progress? [0x%x]\n",
     2288                   igetdword(chip, ICHREG(GLOB_CNT)));
     2289        return -EIO;
     2290
     2291      __ok:
    22942292#endif
    2295     /* put logic to right state */
    2296     /* first clear status bits */
    2297     status = ICH_RCS | ICH_MCINT | ICH_POINT | ICH_PIINT;
    2298     if (chip->device_type == DEVICE_NFORCE)
    2299         status |= ICH_NVSPINT;
    2300     cnt = igetdword(chip, ICHREG(GLOB_STA));
    2301     iputdword(chip, ICHREG(GLOB_STA), cnt & status);
    2302     /* ACLink on, 2 channels */
    2303     cnt = igetdword(chip, ICHREG(GLOB_CNT));
    2304     //    cnt &= ~ICH_ACLINK;
    2305     cnt &= ~(ICH_ACLINK | ICH_PCM_246_MASK);
    2306 #if 0
    2307     //def CONFIG_SND_AC97_POWER_SAVE
    2308     /* do cold reset - the full ac97 powerdown may leave the controller
    2309      * in a warm state but actually it cannot communicate with the codec.
    2310      */
    2311     iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_AC97COLD);
    2312     cnt = igetdword(chip, ICHREG(GLOB_CNT));
    2313     udelay(10);
    2314     iputdword(chip, ICHREG(GLOB_CNT), cnt | ICH_AC97COLD);
    2315     msleep(1);
    2316 #else
    2317     cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
    2318     /* finish cold or do warm reset */
    2319 #ifdef DEBUG
    2320     dprintf(("ICH chip init try to wake up ACLink with %x",cnt));
    2321 #endif
    2322     iputdword(chip, ICHREG(GLOB_CNT), cnt);
    2323     mdelay(500); //vladest 06.10.2003 15:08
    2324     end_time = (jiffies + (HZ / 4)) + 1;
    2325     i = 0;
    2326     do {
    2327         cnt=igetdword(chip, ICHREG(GLOB_CNT));
    2328         if ((cnt & ICH_AC97WARM) == 0)
    2329             goto __ok;
    2330 #ifdef DEBUG
    2331         dprintf(("ICH chip init ACLink ON. try no %d got %x",i,cnt));
    2332 #endif
    2333         do_delay(chip);
    2334         i++;
    2335     } while (i<100); //(time_after_eq(end_time, jiffies));
    2336     snd_printk("AC'97 warm reset still in progress? [0x%x]\n", igetdword(chip, ICHREG(GLOB_CNT)));
    2337     return -EIO;
    2338 __ok:
    2339 #endif /* CONFIG_SND_AC97_POWER_SAVE */
    2340 #ifdef DEBUG
    2341     dprintf(("ICH chip init ACLink ON"));
    2342 #endif
    2343 
    2344 //    mdelay(50);
    2345 
    2346     if (probing) {
    2347         /* wait for any codec ready status.
    2348          * Once it becomes ready it should remain ready
    2349          * as long as we do not disable the ac97 link.
    2350          */
    2351         end_time = jiffies + HZ;
    2352         do {
    2353             status = igetdword(chip, ICHREG(GLOB_STA)) &
    2354                 chip->codec_isr_bits;
    2355             if (status)
    2356                 break;
    2357             mdelay(1);
    2358             //do_delay(chip);
    2359         } while (time_after_eq(end_time, jiffies));
    2360 
    2361         if (! status) {
    2362             /* no codec is found */
    2363             snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n", igetdword(chip, ICHREG(GLOB_STA)));
    2364             return -EIO;
     2293        if (probing) {
     2294                /* wait for any codec ready status.
     2295                 * Once it becomes ready it should remain ready
     2296                 * as long as we do not disable the ac97 link.
     2297                 */
     2298                end_time = jiffies + HZ;
     2299                do {
     2300                        status = igetdword(chip, ICHREG(GLOB_STA)) &
     2301                                chip->codec_isr_bits;
     2302                        if (status)
     2303                                break;
     2304                        schedule_timeout_uninterruptible(1);
     2305                } while (time_after_eq(end_time, jiffies));
     2306                if (! status) {
     2307                        /* no codec is found */
     2308                        snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n",
     2309                                   igetdword(chip, ICHREG(GLOB_STA)));
     2310                        return -EIO;
     2311                }
     2312
     2313                /* wait for other codecs ready status. */
     2314                end_time = jiffies + HZ / 4;
     2315                while (status != chip->codec_isr_bits &&
     2316                       time_after_eq(end_time, jiffies)) {
     2317                        schedule_timeout_uninterruptible(1);
     2318                        status |= igetdword(chip, ICHREG(GLOB_STA)) &
     2319                                chip->codec_isr_bits;
     2320                }
     2321
     2322        } else {
     2323                /* resume phase */
     2324                int i;
     2325                status = 0;
     2326                for (i = 0; i < chip->ncodecs; i++)
     2327                        if (chip->ac97[i])
     2328                                status |= chip->codec_bit[chip->ac97_sdin[i]];
     2329                /* wait until all the probed codecs are ready */
     2330                end_time = jiffies + HZ;
     2331                do {
     2332                        nstatus = igetdword(chip, ICHREG(GLOB_STA)) &
     2333                                chip->codec_isr_bits;
     2334                        if (status == nstatus)
     2335                                break;
     2336                        schedule_timeout_uninterruptible(1);
     2337                } while (time_after_eq(end_time, jiffies));
     2338        }
     2339
     2340        if (chip->device_type == DEVICE_SIS) {
     2341                /* unmute the output on SIS7012 */
     2342                iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
     2343        }
     2344        if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
     2345                /* enable SPDIF interrupt */
     2346                unsigned int val;
     2347                pci_read_config_dword(chip->pci, 0x4c, &val);
     2348                val |= 0x1000000;
     2349                pci_write_config_dword(chip->pci, 0x4c, val);
     2350        }
     2351        return 0;
     2352}
     2353
     2354static int snd_intel8x0_ali_chip_init(struct intel8x0 *chip, int probing)
     2355{
     2356        u32 reg;
     2357        int i = 0;
     2358
     2359        reg = igetdword(chip, ICHREG(ALI_SCR));
     2360        if ((reg & 2) == 0)     /* Cold required */
     2361                reg |= 2;
     2362        else
     2363                reg |= 1;       /* Warm */
     2364        reg &= ~0x80000000;     /* ACLink on */
     2365        iputdword(chip, ICHREG(ALI_SCR), reg);
     2366
     2367        for (i = 0; i < HZ / 2; i++) {
     2368                if (! (igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ALI_INT_GPIO))
     2369                        goto __ok;
     2370                schedule_timeout_uninterruptible(1);
     2371        }
     2372        snd_printk(KERN_ERR "AC'97 reset failed.\n");
     2373        if (probing)
     2374                return -EIO;
     2375
     2376 __ok:
     2377        for (i = 0; i < HZ / 2; i++) {
     2378                reg = igetdword(chip, ICHREG(ALI_RTSR));
     2379                if (reg & 0x80) /* primary codec */
     2380                        break;
     2381                iputdword(chip, ICHREG(ALI_RTSR), reg | 0x80);
     2382                schedule_timeout_uninterruptible(1);
     2383        }
     2384
     2385        do_ali_reset(chip);
     2386        return 0;
     2387}
     2388
     2389static int snd_intel8x0_chip_init(struct intel8x0 *chip, int probing)
     2390{
     2391        unsigned int i, timeout;
     2392        int err;
     2393       
     2394        if (chip->device_type != DEVICE_ALI) {
     2395                if ((err = snd_intel8x0_ich_chip_init(chip, probing)) < 0)
     2396                        return err;
     2397                iagetword(chip, 0);     /* clear semaphore flag */
     2398        } else {
     2399                if ((err = snd_intel8x0_ali_chip_init(chip, probing)) < 0)
     2400                        return err;
     2401        }
     2402
     2403        /* disable interrupts */
     2404        for (i = 0; i < chip->bdbars_count; i++)
     2405                iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
     2406        /* reset channels */
     2407        for (i = 0; i < chip->bdbars_count; i++)
     2408                iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
     2409        for (i = 0; i < chip->bdbars_count; i++) {
     2410                timeout = 100000;
     2411                while (--timeout != 0) {
     2412                        if ((igetbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset) & ICH_RESETREGS) == 0)
     2413                                break;
     2414                }
     2415                if (timeout == 0)
     2416                        printk(KERN_ERR "intel8x0: reset of registers failed?\n");
    23652417        }
    2366 #ifdef DEBUG
    2367         dprintf(("ICH chip init codec ready"));
    2368 #endif
    2369 //        mdelay(50);
    2370 
    2371         /* wait for other codecs ready status. */
    2372         end_time = jiffies + HZ / 4;
    2373         while (status != chip->codec_isr_bits &&
    2374                time_after_eq(end_time, jiffies)) {
    2375             do_delay(chip);
    2376             status |= igetdword(chip, ICHREG(GLOB_STA)) &
    2377                 chip->codec_isr_bits;
    2378         }
    2379     } else {
    2380         /* resume phase */
    2381         int i;
    2382         status = 0;
    2383         for (i = 0; i < chip->ncodecs; i++)
    2384             if (chip->ac97[i])
    2385                 status |= chip->codec_bit[chip->ac97_sdin[i]];
    2386         /* wait until all the probed codecs are ready */
    2387         end_time = jiffies + HZ;
    2388         do {
    2389             nstatus = igetdword(chip, ICHREG(GLOB_STA)) & chip->codec_isr_bits;;
    2390             if (status == nstatus)
    2391                 break;
    2392             do_delay(chip);
    2393         } while (time_after_eq(end_time, jiffies));
    2394     }
    2395 
    2396     if (chip->device_type == DEVICE_SIS) {
    2397         /* unmute the output on SIS7012 */
    2398         iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
    2399     }
    2400     if (chip->device_type == DEVICE_NFORCE) {
    2401         /* enable SPDIF interrupt */
    2402         unsigned int val;
    2403         pci_read_config_dword(chip->pci, 0x4c, &val);
    2404         val |= 0x1000000;
    2405         pci_write_config_dword(chip->pci, 0x4c, val);
    2406     }
    2407 #ifdef DEBUG
    2408     dprintf(("ICH chip init finished"));
    2409 #endif
    2410 
    2411     return 0;
    2412 }
    2413 
    2414 static int snd_intel8x0_ali_chip_init(struct intel8x0 *chip, int probing)
    2415 {
    2416     u32 reg;
    2417     int i = 0;
    2418 
    2419     reg = igetdword(chip, ICHREG(ALI_SCR));
    2420     if ((reg & 2) == 0) /* Cold required */
    2421         reg |= 2;
    2422     else
    2423         reg |= 1;       /* Warm */
    2424     reg &= ~0x80000000; /* ACLink on */
    2425     iputdword(chip, ICHREG(ALI_SCR), reg);
    2426 
    2427     for (i = 0; i < HZ / 2; i++) {
    2428         if (! (igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ALI_INT_GPIO))
    2429             goto __ok;
    2430         do_delay(chip);
    2431     }
    2432     snd_printk(KERN_ERR "AC'97 reset failed.\n");
    2433     if (probing)
    2434         return -EIO;
    2435 
    2436     __ok:
    2437         for (i = 0; i < HZ / 2; i++) {
    2438             reg = igetdword(chip, ICHREG(ALI_RTSR));
    2439             if (reg & 0x80) /* primary codec */
    2440                 break;
    2441             iputdword(chip, ICHREG(ALI_RTSR), reg | 0x80);
    2442             do_delay(chip);
    2443         }
    2444 
    2445         do_ali_reset(chip);
    2446         return 0;
    2447 }
    2448 
    2449 static int snd_intel8x0_chip_init(struct intel8x0 *chip, int probing)
    2450 {
    2451     unsigned int i, timeout;
    2452     int err;
    2453 
    2454     if (chip->device_type != DEVICE_ALI) {
    2455         if ((err = snd_intel8x0_ich_chip_init(chip, probing)) < 0)
    2456             return err;
    2457         iagetword(chip, 0);     /* clear semaphore flag */
    2458     } else {
    2459         if ((err = snd_intel8x0_ali_chip_init(chip, probing)) < 0)
    2460             return err;
    2461     }
    2462 
    2463     /* disable interrupts */
    2464     for (i = 0; i < chip->bdbars_count; i++)
    2465         iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
    2466     /* reset channels */
    2467     for (i = 0; i < chip->bdbars_count; i++)
    2468         iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
    2469     for (i = 0; i < chip->bdbars_count; i++) {
    2470         timeout = 100000;
    2471         while (--timeout != 0) {
    2472             if ((igetbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset) & ICH_RESETREGS) == 0)
    2473                 break;
    2474         }
    2475         if (timeout == 0)
    2476             printk(KERN_ERR "intel8x0: reset of registers failed?\n");
    2477     }
    2478     /* initialize Buffer Descriptor Lists */
    2479     for (i = 0; i < chip->bdbars_count; i++)
    2480         iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
    2481     return 0;
     2418        /* initialize Buffer Descriptor Lists */
     2419        for (i = 0; i < chip->bdbars_count; i++)
     2420                iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset,
     2421                          chip->ichd[i].bdbar_addr);
     2422        return 0;
    24822423}
    24832424
    24842425static int snd_intel8x0_free(struct intel8x0 *chip)
    24852426{
    2486     unsigned int i;
    2487 
    2488     if (chip->irq < 0)
    2489         goto __hw_end;
    2490     /* disable interrupts */
    2491     for (i = 0; i < chip->bdbars_count; i++)
    2492         iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
    2493     /* reset channels */
    2494     for (i = 0; i < chip->bdbars_count; i++)
    2495         iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
    2496     if (chip->device_type == DEVICE_NFORCE) {
    2497         /* stop the spdif interrupt */
    2498         unsigned int val;
    2499         pci_read_config_dword(chip->pci, 0x4c, &val);
    2500         val &= ~0x1000000;
    2501         pci_write_config_dword(chip->pci, 0x4c, val);
    2502     }
    2503     /* --- */
    2504     synchronize_irq(chip->irq);
    2505 __hw_end:
    2506     if (chip->irq >= 0)
    2507         free_irq(chip->irq, (void *)chip);
    2508     if (chip->bdbars.area)
    2509         snd_dma_free_pages(&chip->bdbars);
    2510     if (chip->remap_addr)
    2511         iounmap((void *) chip->remap_addr);
    2512     if (chip->remap_bmaddr)
    2513         iounmap((void *) chip->remap_bmaddr);
    2514     pci_release_regions(chip->pci);
    2515     kfree(chip);
    2516     return 0;
     2427        unsigned int i;
     2428
     2429        if (chip->irq < 0)
     2430                goto __hw_end;
     2431        /* disable interrupts */
     2432        for (i = 0; i < chip->bdbars_count; i++)
     2433                iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
     2434        /* reset channels */
     2435        for (i = 0; i < chip->bdbars_count; i++)
     2436                iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
     2437        if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
     2438                /* stop the spdif interrupt */
     2439                unsigned int val;
     2440                pci_read_config_dword(chip->pci, 0x4c, &val);
     2441                val &= ~0x1000000;
     2442                pci_write_config_dword(chip->pci, 0x4c, val);
     2443        }
     2444        /* --- */
     2445        synchronize_irq(chip->irq);
     2446      __hw_end:
     2447        if (chip->irq >= 0)
     2448                free_irq(chip->irq, chip);
     2449        if (chip->bdbars.area) {
     2450                if (chip->fix_nocache)
     2451                        fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 0);
     2452                snd_dma_free_pages(&chip->bdbars);
     2453        }
     2454        if (chip->addr)
     2455                pci_iounmap(chip->pci, chip->addr);
     2456        if (chip->bmaddr)
     2457                pci_iounmap(chip->pci, chip->bmaddr);
     2458        pci_release_regions(chip->pci);
     2459        pci_disable_device(chip->pci);
     2460        kfree(chip);
     2461        return 0;
    25172462}
    25182463
     
    25232468static int intel8x0_suspend(struct pci_dev *pci, pm_message_t state)
    25242469{
    2525     struct snd_card *card = pci_get_drvdata(pci);
    2526     struct intel8x0 *chip = card->private_data;
    2527     int i;
    2528 
    2529     snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
    2530 
    2531     for (i = 0; i < chip->pcm_devs; i++)
    2532         snd_pcm_suspend_all(chip->pcm[i]);
    2533 
    2534     for (i = 0; i < chip->ncodecs; i++)
    2535         if (chip->ac97[i])      //Rudi: check, if codec present !!!
    2536             snd_ac97_suspend(chip->ac97[i]);
    2537     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2538         chip->device_type == DEVICE_INTEL_ICH5)
    2539         chip->sdm_saved = igetbyte(chip, ICHREG(SDM));
    2540 
    2541     if (chip->irq >= 0)
    2542         free_irq(chip->irq, (void *)chip);
    2543     pci_disable_device(pci);
    2544     pci_save_state(pci);
    2545     return 0;
     2470        struct snd_card *card = pci_get_drvdata(pci);
     2471        struct intel8x0 *chip = card->private_data;
     2472        int i;
     2473
     2474        snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
     2475        for (i = 0; i < chip->pcm_devs; i++)
     2476                snd_pcm_suspend_all(chip->pcm[i]);
     2477        /* clear nocache */
     2478        if (chip->fix_nocache) {
     2479                for (i = 0; i < chip->bdbars_count; i++) {
     2480                        struct ichdev *ichdev = &chip->ichd[i];
     2481                        if (ichdev->substream && ichdev->page_attr_changed) {
     2482                                struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
     2483                                if (runtime->dma_area)
     2484                                        fill_nocache(runtime->dma_area, runtime->dma_bytes, 0);
     2485                        }
     2486                }
     2487        }
     2488        for (i = 0; i < chip->ncodecs; i++)
     2489                snd_ac97_suspend(chip->ac97[i]);
     2490        if (chip->device_type == DEVICE_INTEL_ICH4)
     2491                chip->sdm_saved = igetbyte(chip, ICHREG(SDM));
     2492
     2493        if (chip->irq >= 0) {
     2494                synchronize_irq(chip->irq);
     2495                free_irq(chip->irq, chip);
     2496                chip->irq = -1;
     2497        }
     2498        pci_disable_device(pci);
     2499        pci_save_state(pci);
     2500        /* The call below may disable built-in speaker on some laptops
     2501         * after S2RAM.  So, don't touch it.
     2502         */
     2503        /* pci_set_power_state(pci, pci_choose_state(pci, state)); */
     2504        return 0;
    25462505}
    25472506
    25482507static int intel8x0_resume(struct pci_dev *pci)
    25492508{
    2550     struct snd_card *card = pci_get_drvdata(pci);
    2551     struct intel8x0 *chip = card->private_data;
    2552     int i;
    2553 
    2554     pci_restore_state(pci);
    2555     pci_enable_device(pci);
    2556     pci_set_master(pci);
    2557     request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ,
    2558                 card->shortname, (void *)chip);
    2559     chip->irq = pci->irq;
    2560     synchronize_irq(chip->irq);
    2561     snd_intel8x0_chip_init(chip, 0);
    2562     /* re-initialize mixer stuff */
    2563     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2564         chip->device_type == DEVICE_INTEL_ICH5) {
    2565         /* enable separate SDINs for ICH4 */
    2566         iputbyte(chip, ICHREG(SDM), chip->sdm_saved);
    2567         /* use slot 10/11 for SPDIF */
    2568         iputdword(chip, ICHREG(GLOB_CNT),
    2569                   (igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK) |
    2570                   ICH_PCM_SPDIF_1011);
    2571     }
    2572 
    2573     for (i = 0; i < chip->ncodecs; i++)
    2574         if (chip->ac97[i])              //Rudi: check, if codec present !!!
    2575             snd_ac97_resume(chip->ac97[i]);
    2576     /* resume status */
    2577     for (i = 0; i < chip->bdbars_count; i++) {
    2578         struct ichdev *ichdev = &chip->ichd[i];
    2579         unsigned long port = ichdev->reg_offset;
    2580         if (! ichdev->substream || ! ichdev->suspended)
    2581             continue;
    2582         if (ichdev->ichd == ICHD_PCMOUT)
    2583             snd_intel8x0_setup_pcm_out(chip, ichdev->substream->runtime);
    2584         iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
    2585         iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
    2586         iputbyte(chip, port + ICH_REG_OFF_CIV, ichdev->civ);
    2587         iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
    2588     }
    2589     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
    2590     return 0;
    2591 }
    2592 
     2509        struct snd_card *card = pci_get_drvdata(pci);
     2510        struct intel8x0 *chip = card->private_data;
     2511        int i;
     2512
     2513        pci_set_power_state(pci, PCI_D0);
     2514        pci_restore_state(pci);
     2515        if (pci_enable_device(pci) < 0) {
     2516                printk(KERN_ERR "intel8x0: pci_enable_device failed, "
     2517                       "disabling device\n");
     2518                snd_card_disconnect(card);
     2519                return -EIO;
     2520        }
     2521        pci_set_master(pci);
     2522        snd_intel8x0_chip_init(chip, 0);
     2523        if (request_irq(pci->irq, snd_intel8x0_interrupt,
     2524                        IRQF_SHARED, card->shortname, chip)) {
     2525                printk(KERN_ERR "intel8x0: unable to grab IRQ %d, "
     2526                       "disabling device\n", pci->irq);
     2527                snd_card_disconnect(card);
     2528                return -EIO;
     2529        }
     2530        chip->irq = pci->irq;
     2531        synchronize_irq(chip->irq);
     2532
     2533        /* re-initialize mixer stuff */
     2534        if (chip->device_type == DEVICE_INTEL_ICH4 && !spdif_aclink) {
     2535                /* enable separate SDINs for ICH4 */
     2536                iputbyte(chip, ICHREG(SDM), chip->sdm_saved);
     2537                /* use slot 10/11 for SPDIF */
     2538                iputdword(chip, ICHREG(GLOB_CNT),
     2539                          (igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK) |
     2540                          ICH_PCM_SPDIF_1011);
     2541        }
     2542
     2543        /* refill nocache */
     2544        if (chip->fix_nocache)
     2545                fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
     2546
     2547        for (i = 0; i < chip->ncodecs; i++)
     2548                snd_ac97_resume(chip->ac97[i]);
     2549
     2550        /* refill nocache */
     2551        if (chip->fix_nocache) {
     2552                for (i = 0; i < chip->bdbars_count; i++) {
     2553                        struct ichdev *ichdev = &chip->ichd[i];
     2554                        if (ichdev->substream && ichdev->page_attr_changed) {
     2555                                struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
     2556                                if (runtime->dma_area)
     2557                                        fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
     2558                        }
     2559                }
     2560        }
     2561
     2562        /* resume status */
     2563        for (i = 0; i < chip->bdbars_count; i++) {
     2564                struct ichdev *ichdev = &chip->ichd[i];
     2565                unsigned long port = ichdev->reg_offset;
     2566                if (! ichdev->substream || ! ichdev->suspended)
     2567                        continue;
     2568                if (ichdev->ichd == ICHD_PCMOUT)
     2569                        snd_intel8x0_setup_pcm_out(chip, ichdev->substream->runtime);
     2570                iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
     2571                iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
     2572                iputbyte(chip, port + ICH_REG_OFF_CIV, ichdev->civ);
     2573                iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
     2574        }
     2575
     2576        snd_power_change_state(card, SNDRV_CTL_POWER_D0);
     2577        return 0;
     2578}
    25932579#endif /* CONFIG_PM */
    25942580
     
    25972583static void __devinit intel8x0_measure_ac97_clock(struct intel8x0 *chip)
    25982584{
    2599     snd_pcm_substream_t *subs;
    2600     struct ichdev *ichdev;
    2601     unsigned long port;
    2602     unsigned long pos, t;
    2603     struct timeval start_time, stop_time;
    2604 
    2605     if (chip->ac97_bus->clock != 48000)
    2606         return; /* specified in module option */
    2607 
    2608     subs = chip->pcm[0]->streams[0].substream;
    2609     if (! subs || subs->dma_buffer.bytes < INTEL8X0_TESTBUF_SIZE) {
    2610         snd_printk("no playback buffer allocated - aborting measure ac97 clock\n");
    2611         return;
    2612     }
    2613     ichdev = &chip->ichd[ICHD_PCMOUT];
    2614     ichdev->physbuf = subs->dma_buffer.addr;
    2615     ichdev->size = chip->ichd[ICHD_PCMOUT].fragsize = INTEL8X0_TESTBUF_SIZE;
    2616     ichdev->substream = NULL; /* don't process interrupts */
    2617 
    2618     /* set rate */
    2619     if (snd_ac97_set_rate(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 48000) < 0) {
    2620         snd_printk(KERN_ERR "cannot set ac97 rate: clock = %d\n", chip->ac97_bus->clock);
    2621         return;
    2622     }
    2623     snd_intel8x0_setup_periods(chip, ichdev);
    2624     port = ichdev->reg_offset;
    2625     spin_lock_irq(&chip->reg_lock);
    2626     /* trigger */
    2627     if (chip->device_type != DEVICE_ALI)
    2628         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE | ICH_STARTBM);
    2629     else {
    2630         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
    2631         iputdword(chip, ICHREG(ALI_DMACR), 1 << ichdev->ali_slot);
    2632     }
    2633     do_gettimeofday(&start_time);
    2634     spin_unlock_irq(&chip->reg_lock);
    2635     mdelay(50);
    2636 
    2637     spin_lock_irq(&chip->reg_lock);
    2638     /* check the position */
    2639     pos = ichdev->fragsize1;
    2640     pos -= igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << ichdev->pos_shift;
    2641     pos += ichdev->position;
    2642     do_gettimeofday(&stop_time);
    2643     /* stop */
    2644     if (chip->device_type == DEVICE_ALI) {
    2645         iputdword(chip, ICHREG(ALI_DMACR), 1 << (ichdev->ali_slot + 16));
    2646         iputbyte(chip, port + ICH_REG_OFF_CR, 0);
    2647         while (igetbyte(chip, port + ICH_REG_OFF_CR))
    2648             ;
    2649     } else {
    2650         iputbyte(chip, port + ICH_REG_OFF_CR, 0);
    2651         while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH))
    2652             ;
    2653     }
    2654     iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
    2655     spin_unlock_irq(&chip->reg_lock);
    2656 
    2657     t = stop_time.tv_sec - start_time.tv_sec;
    2658     t *= 1000000;
    2659     t += stop_time.tv_usec - start_time.tv_usec;
    2660     printk(KERN_INFO "%s: measured %lu usecs\n", __FUNCTION__, t);
    2661     if (t == 0) {
    2662         snd_printk(KERN_ERR "?? calculation error..\n");
    2663         return;
    2664     }
    2665     pos = (pos / 4) * 1000;
    2666     pos = (pos / t) * 1000 + ((pos % t) * 1000) / t;
    2667     if (pos < 40000 || pos >= 60000)
    2668         /* abnormal value. hw problem? */
    2669         printk(KERN_INFO "intel8x0: measured clock %ld rejected\n", pos);
    2670     else if (pos < 47500 || pos > 48500)
    2671         /* not 48000Hz, tuning the clock.. */
    2672         chip->ac97_bus->clock = (chip->ac97_bus->clock * 48000) / pos;
    2673     printk(KERN_INFO "intel8x0: clocking to %d\n", chip->ac97_bus->clock);
    2674     snd_ac97_update_power(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 0);
     2585        struct snd_pcm_substream *subs;
     2586        struct ichdev *ichdev;
     2587        unsigned long port;
     2588        unsigned long pos, t;
     2589        struct timeval start_time, stop_time;
     2590
     2591        if (chip->ac97_bus->clock != 48000)
     2592                return; /* specified in module option */
     2593
     2594        subs = chip->pcm[0]->streams[0].substream;
     2595        if (! subs || subs->dma_buffer.bytes < INTEL8X0_TESTBUF_SIZE) {
     2596                snd_printk(KERN_WARNING "no playback buffer allocated - aborting measure ac97 clock\n");
     2597                return;
     2598        }
     2599        ichdev = &chip->ichd[ICHD_PCMOUT];
     2600        ichdev->physbuf = subs->dma_buffer.addr;
     2601        ichdev->size = chip->ichd[ICHD_PCMOUT].fragsize = INTEL8X0_TESTBUF_SIZE;
     2602        ichdev->substream = NULL; /* don't process interrupts */
     2603
     2604        /* set rate */
     2605        if (snd_ac97_set_rate(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 48000) < 0) {
     2606                snd_printk(KERN_ERR "cannot set ac97 rate: clock = %d\n", chip->ac97_bus->clock);
     2607                return;
     2608        }
     2609        snd_intel8x0_setup_periods(chip, ichdev);
     2610        port = ichdev->reg_offset;
     2611        spin_lock_irq(&chip->reg_lock);
     2612        chip->in_measurement = 1;
     2613        /* trigger */
     2614        if (chip->device_type != DEVICE_ALI)
     2615                iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE | ICH_STARTBM);
     2616        else {
     2617                iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
     2618                iputdword(chip, ICHREG(ALI_DMACR), 1 << ichdev->ali_slot);
     2619        }
     2620        do_gettimeofday(&start_time);
     2621        spin_unlock_irq(&chip->reg_lock);
     2622        msleep(50);
     2623        spin_lock_irq(&chip->reg_lock);
     2624        /* check the position */
     2625        pos = ichdev->fragsize1;
     2626        pos -= igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << ichdev->pos_shift;
     2627        pos += ichdev->position;
     2628        chip->in_measurement = 0;
     2629        do_gettimeofday(&stop_time);
     2630        /* stop */
     2631        if (chip->device_type == DEVICE_ALI) {
     2632                iputdword(chip, ICHREG(ALI_DMACR), 1 << (ichdev->ali_slot + 16));
     2633                iputbyte(chip, port + ICH_REG_OFF_CR, 0);
     2634                while (igetbyte(chip, port + ICH_REG_OFF_CR))
     2635                        ;
     2636        } else {
     2637                iputbyte(chip, port + ICH_REG_OFF_CR, 0);
     2638                while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH))
     2639                        ;
     2640        }
     2641        iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
     2642        spin_unlock_irq(&chip->reg_lock);
     2643
     2644        t = stop_time.tv_sec - start_time.tv_sec;
     2645        t *= 1000000;
     2646        t += stop_time.tv_usec - start_time.tv_usec;
     2647        printk(KERN_INFO "%s: measured %lu usecs\n", __FUNCTION__, t);
     2648        if (t == 0) {
     2649                snd_printk(KERN_ERR "?? calculation error..\n");
     2650                return;
     2651        }
     2652        pos = (pos / 4) * 1000;
     2653        pos = (pos / t) * 1000 + ((pos % t) * 1000) / t;
     2654        if (pos < 40000 || pos >= 60000)
     2655                /* abnormal value. hw problem? */
     2656                printk(KERN_INFO "intel8x0: measured clock %ld rejected\n", pos);
     2657        else if (pos < 47500 || pos > 48500)
     2658                /* not 48000Hz, tuning the clock.. */
     2659                chip->ac97_bus->clock = (chip->ac97_bus->clock * 48000) / pos;
     2660        printk(KERN_INFO "intel8x0: clocking to %d\n", chip->ac97_bus->clock);
     2661        snd_ac97_update_power(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 0);
    26752662}
    26762663
    26772664#ifdef CONFIG_PROC_FS
    2678 static void snd_intel8x0_proc_read(snd_info_entry_t * entry,
    2679                                    snd_info_buffer_t * buffer)
    2680 {
    2681     struct intel8x0 *chip = entry->private_data;
    2682     unsigned int tmp;
    2683 
    2684     snd_iprintf(buffer, "Intel8x0\n\n");
    2685     if (chip->device_type == DEVICE_ALI)
    2686         return;
    2687     tmp = igetdword(chip, ICHREG(GLOB_STA));
    2688     snd_iprintf(buffer, "Global control        : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
    2689     snd_iprintf(buffer, "Global status         : 0x%08x\n", tmp);
    2690     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2691         chip->device_type == DEVICE_INTEL_ICH5)
    2692         snd_iprintf(buffer, "SDM                   : 0x%08x\n", igetdword(chip, ICHREG(SDM)));
    2693     snd_iprintf(buffer, "AC'97 codecs ready    :");
    2694     if (tmp & chip->codec_isr_bits) {
    2695         int i;
    2696         static const char *codecs[3] = {
    2697             "primary", "secondary", "tertiary"
    2698         };
    2699         for (i = 0; i < chip->max_codecs; i++)
    2700             if (tmp & chip->codec_bit[i])
    2701                 snd_iprintf(buffer, " %s", codecs[i]);
    2702     } else
    2703         snd_iprintf(buffer, " none");
    2704     snd_iprintf(buffer, "\n");
    2705     if (chip->device_type == DEVICE_INTEL_ICH4 ||
    2706                      chip->device_type == DEVICE_SIS)
    2707         snd_iprintf(buffer, "AC'97 codecs SDIN     : %i %i %i\n",
    2708                     chip->ac97_sdin[0],
    2709                     chip->ac97_sdin[1],
    2710                     chip->ac97_sdin[2]);
     2665static void snd_intel8x0_proc_read(struct snd_info_entry * entry,
     2666                                   struct snd_info_buffer *buffer)
     2667{
     2668        struct intel8x0 *chip = entry->private_data;
     2669        unsigned int tmp;
     2670
     2671        snd_iprintf(buffer, "Intel8x0\n\n");
     2672        if (chip->device_type == DEVICE_ALI)
     2673                return;
     2674        tmp = igetdword(chip, ICHREG(GLOB_STA));
     2675        snd_iprintf(buffer, "Global control        : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
     2676        snd_iprintf(buffer, "Global status         : 0x%08x\n", tmp);
     2677        if (chip->device_type == DEVICE_INTEL_ICH4)
     2678                snd_iprintf(buffer, "SDM                   : 0x%08x\n", igetdword(chip, ICHREG(SDM)));
     2679        snd_iprintf(buffer, "AC'97 codecs ready    :");
     2680        if (tmp & chip->codec_isr_bits) {
     2681                int i;
     2682                static const char *codecs[3] = {
     2683                        "primary", "secondary", "tertiary"
     2684                };
     2685                for (i = 0; i < chip->max_codecs; i++)
     2686                        if (tmp & chip->codec_bit[i])
     2687                                snd_iprintf(buffer, " %s", codecs[i]);
     2688        } else
     2689                snd_iprintf(buffer, " none");
     2690        snd_iprintf(buffer, "\n");
     2691        if (chip->device_type == DEVICE_INTEL_ICH4 ||
     2692            chip->device_type == DEVICE_SIS)
     2693                snd_iprintf(buffer, "AC'97 codecs SDIN     : %i %i %i\n",
     2694                        chip->ac97_sdin[0],
     2695                        chip->ac97_sdin[1],
     2696                        chip->ac97_sdin[2]);
    27112697}
    27122698
    27132699static void __devinit snd_intel8x0_proc_init(struct intel8x0 * chip)
    27142700{
    2715     snd_info_entry_t *entry;
    2716 
    2717     if (! snd_card_proc_new(chip->card, "intel8x0", &entry))
    2718         snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0_proc_read);
     2701        struct snd_info_entry *entry;
     2702
     2703        if (! snd_card_proc_new(chip->card, "intel8x0", &entry))
     2704                snd_info_set_text_ops(entry, chip, snd_intel8x0_proc_read);
    27192705}
    27202706#else
     
    27222708#endif
    27232709
    2724 static int snd_intel8x0_dev_free(snd_device_t *device)
    2725 {
    2726     struct intel8x0 *chip = device->device_data;
    2727     return snd_intel8x0_free(chip);
     2710static int snd_intel8x0_dev_free(struct snd_device *device)
     2711{
     2712        struct intel8x0 *chip = device->device_data;
     2713        return snd_intel8x0_free(chip);
    27282714}
    27292715
    27302716struct ich_reg_info {
    2731     unsigned int int_sta_mask;
    2732     unsigned int offset;
     2717        unsigned int int_sta_mask;
     2718        unsigned int offset;
    27332719};
    27342720
    27352721static unsigned int ich_codec_bits[3] = {
    2736     ICH_PCR, ICH_SCR, ICH_TCR
     2722        ICH_PCR, ICH_SCR, ICH_TCR
    27372723};
    27382724static unsigned int sis_codec_bits[3] = {
    2739     ICH_PCR, ICH_SCR, ICH_SIS_TCR
    2740 };
    2741 
    2742 static int __devinit snd_intel8x0_create(snd_card_t * card,
    2743                                          struct pci_dev *pci,
    2744                                          unsigned long device_type,
    2745                                          struct intel8x0 ** r_intel8x0)
    2746 {
    2747     struct intel8x0 *chip;
    2748     int err;
    2749     unsigned int i,pci_dword;
    2750     unsigned int int_sta_masks;
    2751     unsigned short pci_word;
    2752     unsigned char pci_byte;
    2753 
    2754     struct ichdev *ichdev;
    2755 #ifdef TARGET_OS2
    2756     static snd_device_ops_t ops = {
    2757         snd_intel8x0_dev_free,0,0,0
    2758     };
    2759 #else
    2760     static snd_device_ops_t ops = {
    2761         .dev_free =     snd_intel8x0_dev_free,
    2762     };
     2725        ICH_PCR, ICH_SCR, ICH_SIS_TCR
     2726};
     2727
     2728static int __devinit snd_intel8x0_create(struct snd_card *card,
     2729                                         struct pci_dev *pci,
     2730                                         unsigned long device_type,
     2731                                         struct intel8x0 ** r_intel8x0)
     2732{
     2733        struct intel8x0 *chip;
     2734        int err;
     2735        unsigned int i;
     2736        unsigned int int_sta_masks;
     2737        struct ichdev *ichdev;
     2738        static struct snd_device_ops ops = {
     2739                .dev_free =     snd_intel8x0_dev_free,
     2740        };
     2741
     2742        static unsigned int bdbars[] = {
     2743                3, /* DEVICE_INTEL */
     2744                6, /* DEVICE_INTEL_ICH4 */
     2745                3, /* DEVICE_SIS */
     2746                6, /* DEVICE_ALI */
     2747                4, /* DEVICE_NFORCE */
     2748        };
     2749        static struct ich_reg_info intel_regs[6] = {
     2750                { ICH_PIINT, 0 },
     2751                { ICH_POINT, 0x10 },
     2752                { ICH_MCINT, 0x20 },
     2753                { ICH_M2INT, 0x40 },
     2754                { ICH_P2INT, 0x50 },
     2755                { ICH_SPINT, 0x60 },
     2756        };
     2757        static struct ich_reg_info nforce_regs[4] = {
     2758                { ICH_PIINT, 0 },
     2759                { ICH_POINT, 0x10 },
     2760                { ICH_MCINT, 0x20 },
     2761                { ICH_NVSPINT, 0x70 },
     2762        };
     2763        static struct ich_reg_info ali_regs[6] = {
     2764                { ALI_INT_PCMIN, 0x40 },
     2765                { ALI_INT_PCMOUT, 0x50 },
     2766                { ALI_INT_MICIN, 0x60 },
     2767                { ALI_INT_CODECSPDIFOUT, 0x70 },
     2768                { ALI_INT_SPDIFIN, 0xa0 },
     2769                { ALI_INT_SPDIFOUT, 0xb0 },
     2770        };
     2771        struct ich_reg_info *tbl;
     2772
     2773        *r_intel8x0 = NULL;
     2774
     2775        if ((err = pci_enable_device(pci)) < 0)
     2776                return err;
     2777
     2778        chip = kzalloc(sizeof(*chip), GFP_KERNEL);
     2779        if (chip == NULL) {
     2780                pci_disable_device(pci);
     2781                return -ENOMEM;
     2782        }
     2783        spin_lock_init(&chip->reg_lock);
     2784        chip->device_type = device_type;
     2785        chip->card = card;
     2786        chip->pci = pci;
     2787        chip->irq = -1;
     2788
     2789        /* module parameters */
     2790        chip->buggy_irq = buggy_irq;
     2791        chip->buggy_semaphore = buggy_semaphore;
     2792        if (xbox)
     2793                chip->xbox = 1;
     2794
     2795        if (pci->vendor == PCI_VENDOR_ID_INTEL &&
     2796            pci->device == PCI_DEVICE_ID_INTEL_440MX)
     2797                chip->fix_nocache = 1; /* enable workaround */
     2798
     2799        if ((err = pci_request_regions(pci, card->shortname)) < 0) {
     2800                kfree(chip);
     2801                pci_disable_device(pci);
     2802                return err;
     2803        }
     2804
     2805        if (device_type == DEVICE_ALI) {
     2806                /* ALI5455 has no ac97 region */
     2807                chip->bmaddr = pci_iomap(pci, 0, 0);
     2808                goto port_inited;
     2809        }
     2810
     2811        if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) /* ICH4 and Nforce */
     2812                chip->addr = pci_iomap(pci, 2, 0);
     2813        else
     2814                chip->addr = pci_iomap(pci, 0, 0);
     2815        if (!chip->addr) {
     2816                snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
     2817                snd_intel8x0_free(chip);
     2818                return -EIO;
     2819        }
     2820        if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) /* ICH4 */
     2821                chip->bmaddr = pci_iomap(pci, 3, 0);
     2822        else
     2823                chip->bmaddr = pci_iomap(pci, 1, 0);
     2824        if (!chip->bmaddr) {
     2825                snd_printk(KERN_ERR "Controller space ioremap problem\n");
     2826                snd_intel8x0_free(chip);
     2827                return -EIO;
     2828        }
     2829
     2830 port_inited:
     2831        chip->bdbars_count = bdbars[device_type];
     2832
     2833        /* initialize offsets */
     2834        switch (device_type) {
     2835        case DEVICE_NFORCE:
     2836                tbl = nforce_regs;
     2837                break;
     2838        case DEVICE_ALI:
     2839                tbl = ali_regs;
     2840                break;
     2841        default:
     2842                tbl = intel_regs;
     2843                break;
     2844        }
     2845        for (i = 0; i < chip->bdbars_count; i++) {
     2846                ichdev = &chip->ichd[i];
     2847                ichdev->ichd = i;
     2848                ichdev->reg_offset = tbl[i].offset;
     2849                ichdev->int_sta_mask = tbl[i].int_sta_mask;
     2850                if (device_type == DEVICE_SIS) {
     2851                        /* SiS 7012 swaps the registers */
     2852                        ichdev->roff_sr = ICH_REG_OFF_PICB;
     2853                        ichdev->roff_picb = ICH_REG_OFF_SR;
     2854                } else {
     2855                        ichdev->roff_sr = ICH_REG_OFF_SR;
     2856                        ichdev->roff_picb = ICH_REG_OFF_PICB;
     2857                }
     2858                if (device_type == DEVICE_ALI)
     2859                        ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
     2860                /* SIS7012 handles the pcm data in bytes, others are in samples */
     2861                ichdev->pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
     2862        }
     2863
     2864        /* allocate buffer descriptor lists */
     2865        /* the start of each lists must be aligned to 8 bytes */
     2866        if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
     2867                                chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
     2868                                &chip->bdbars) < 0) {
     2869                snd_intel8x0_free(chip);
     2870                snd_printk(KERN_ERR "intel8x0: cannot allocate buffer descriptors\n");
     2871                return -ENOMEM;
     2872        }
     2873        /* tables must be aligned to 8 bytes here, but the kernel pages
     2874           are much bigger, so we don't care (on i386) */
     2875        /* workaround for 440MX */
     2876        if (chip->fix_nocache)
     2877                fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
     2878        int_sta_masks = 0;
     2879        for (i = 0; i < chip->bdbars_count; i++) {
     2880                ichdev = &chip->ichd[i];
     2881                ichdev->bdbar = ((u32 *)chip->bdbars.area) +
     2882                        (i * ICH_MAX_FRAGS * 2);
     2883                ichdev->bdbar_addr = chip->bdbars.addr +
     2884                        (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
     2885                int_sta_masks |= ichdev->int_sta_mask;
     2886        }
     2887        chip->int_sta_reg = device_type == DEVICE_ALI ?
     2888                ICH_REG_ALI_INTERRUPTSR : ICH_REG_GLOB_STA;
     2889        chip->int_sta_mask = int_sta_masks;
     2890
     2891        pci_set_master(pci);
     2892
     2893        switch(chip->device_type) {
     2894        case DEVICE_INTEL_ICH4:
     2895                /* ICH4 can have three codecs */
     2896                chip->max_codecs = 3;
     2897                chip->codec_bit = ich_codec_bits;
     2898                chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_TRI;
     2899                break;
     2900        case DEVICE_SIS:
     2901                /* recent SIS7012 can have three codecs */
     2902                chip->max_codecs = 3;
     2903                chip->codec_bit = sis_codec_bits;
     2904                chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_SIS_TRI;
     2905                break;
     2906        default:
     2907                /* others up to two codecs */
     2908                chip->max_codecs = 2;
     2909                chip->codec_bit = ich_codec_bits;
     2910                chip->codec_ready_bits = ICH_PRI | ICH_SRI;
     2911                break;
     2912        }
     2913        for (i = 0; i < chip->max_codecs; i++)
     2914                chip->codec_isr_bits |= chip->codec_bit[i];
     2915
     2916        if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
     2917                snd_intel8x0_free(chip);
     2918                return err;
     2919        }
     2920
     2921        /* request irq after initializaing int_sta_mask, etc */
     2922        if (request_irq(pci->irq, snd_intel8x0_interrupt,
     2923                        IRQF_SHARED, card->shortname, chip)) {
     2924                snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
     2925                snd_intel8x0_free(chip);
     2926                return -EBUSY;
     2927        }
     2928        chip->irq = pci->irq;
     2929
     2930        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
     2931                snd_intel8x0_free(chip);
     2932                return err;
     2933        }
     2934
     2935        snd_card_set_dev(card, &pci->dev);
     2936
     2937        *r_intel8x0 = chip;
     2938        return 0;
     2939}
     2940
     2941static struct shortname_table {
     2942        unsigned int id;
     2943        const char *s;
     2944} shortnames[] __devinitdata = {
     2945        { PCI_DEVICE_ID_INTEL_82801AA_5, "Intel 82801AA-ICH" },
     2946        { PCI_DEVICE_ID_INTEL_82801AB_5, "Intel 82901AB-ICH0" },
     2947        { PCI_DEVICE_ID_INTEL_82801BA_4, "Intel 82801BA-ICH2" },
     2948        { PCI_DEVICE_ID_INTEL_440MX, "Intel 440MX" },
     2949        { PCI_DEVICE_ID_INTEL_82801CA_5, "Intel 82801CA-ICH3" },
     2950        { PCI_DEVICE_ID_INTEL_82801DB_5, "Intel 82801DB-ICH4" },
     2951        { PCI_DEVICE_ID_INTEL_82801EB_5, "Intel ICH5" },
     2952        { PCI_DEVICE_ID_INTEL_ESB_5, "Intel 6300ESB" },
     2953        { PCI_DEVICE_ID_INTEL_ICH6_18, "Intel ICH6" },
     2954        { PCI_DEVICE_ID_INTEL_ICH7_20, "Intel ICH7" },
     2955        { PCI_DEVICE_ID_INTEL_ESB2_14, "Intel ESB2" },
     2956        { PCI_DEVICE_ID_SI_7012, "SiS SI7012" },
     2957        { PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO, "NVidia nForce" },
     2958        { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
     2959        { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
     2960        { PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO, "NVidia CK8S" },
     2961        { PCI_DEVICE_ID_NVIDIA_CK804_AUDIO, "NVidia CK804" },
     2962        { PCI_DEVICE_ID_NVIDIA_CK8_AUDIO, "NVidia CK8" },
     2963        { 0x003a, "NVidia MCP04" },
     2964        { 0x746d, "AMD AMD8111" },
     2965        { 0x7445, "AMD AMD768" },
     2966        { 0x5455, "ALi M5455" },
     2967        { 0, NULL },
     2968};
     2969
     2970static struct snd_pci_quirk spdif_aclink_defaults[] __devinitdata = {
     2971        SND_PCI_QUIRK(0x147b, 0x1c1a, "ASUS KN8", 1),
     2972        {0} /* end */
     2973};
     2974
     2975/* look up white/black list for SPDIF over ac-link */
     2976static int __devinit check_default_spdif_aclink(struct pci_dev *pci)
     2977{
     2978#ifndef TARGET_OS2
     2979        const struct snd_pci_quirk *w;
     2980
     2981        w = snd_pci_quirk_lookup(pci, spdif_aclink_defaults);
     2982        if (w) {
     2983                if (w->value)
     2984                        snd_printdd(KERN_INFO "intel8x0: Using SPDIF over "
     2985                                    "AC-Link for %s\n", w->name);
     2986                else
     2987                        snd_printdd(KERN_INFO "intel8x0: Using integrated "
     2988                                    "SPDIF DMA for %s\n", w->name);
     2989                return w->value;
     2990        }
    27632991#endif
    2764     static unsigned int bdbars[] = {
    2765         3, /* DEVICE_INTEL */
    2766         6, /* DEVICE_INTEL_ICH4 */
    2767         6, /* DEVICE_INTEL_ICH5 */
    2768         3, /* DEVICE_SIS */
    2769         6, /* DEVICE_ALI */
    2770         4, /* DEVICE_NFORCE */
    2771     };
    2772     static struct ich_reg_info intel_regs[6] = {
    2773         { ICH_PIINT, 0 },
    2774         { ICH_POINT, 0x10 },
    2775         { ICH_MCINT, 0x20 },
    2776         { ICH_M2INT, 0x40 },
    2777         { ICH_P2INT, 0x50 },
    2778         { ICH_SPINT, 0x60 },
    2779     };
    2780     static struct ich_reg_info nforce_regs[4] = {
    2781         { ICH_PIINT, 0 },
    2782         { ICH_POINT, 0x10 },
    2783         { ICH_MCINT, 0x20 },
    2784         { ICH_NVSPINT, 0x70 },
    2785     };
    2786     static struct ich_reg_info ali_regs[6] = {
    2787         { ALI_INT_PCMIN, 0x40 },
    2788         { ALI_INT_PCMOUT, 0x50 },
    2789         { ALI_INT_MICIN, 0x60 },
    2790         { ALI_INT_CODECSPDIFOUT, 0x70 },
    2791         { ALI_INT_SPDIFIN, 0xa0 },
    2792         { ALI_INT_SPDIFOUT, 0xb0 },
    2793     };
    2794     struct ich_reg_info *tbl;
    2795 
    2796     *r_intel8x0 = NULL;
    2797 
    2798     if ((err = pci_enable_device(pci)) < 0)
    2799         return err;
    2800     pci_set_master(pci);
    2801 
    2802 //    pci_write_config_byte(pci, 0x40, 0xff); // added by vladest
    2803 
    2804     chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
    2805     if (chip == NULL)
    2806         return -ENOMEM;
    2807     spin_lock_init(&chip->reg_lock);
    2808     chip->device_type = device_type;
    2809     chip->card = card;
    2810     chip->pci = pci;
    2811     chip->irq = -1;
    2812     if ((err = pci_request_regions(pci, card->shortname)) < 0) {
    2813         kfree(chip);
    2814         return err;
    2815     }
    2816     if (device_type == DEVICE_ALI) {
    2817         /* ALI5455 has no ac97 region */
    2818         chip->bmaddr = pci_resource_start(pci, 0);
    2819         goto port_inited;
    2820     }
    2821     /*
    2822      ¥áâì €¢  ­î ­á .  €® ᬮâà¥âì. ¥à¢®¥: pci_cfg_space 54h,
    2823      €¢  ¬« €èšå ¡šâ  㪠§ë¢ îâ á®áâ®ï­š¥ 皯 . ¥á«š â ¬ 11, â®
    2824      ªà®¬¥ ª®­äš£á¯¥©á  ¡®«ìè¥ ­š å७  ­¥ à ¡®â ¥â.
    2825      ’〠 ­ €® § ¯šá âì 00.
    2826      ‚â®à®¥: ॣšáâà PCICMD, íâ® 16-¡šâ­ë© ॣšáâà ¯® ᬥ饭šî 04h
    2827      ¢ cfg_space. šâ 1 ¢ëáâ ¢šâì ¢ 1
    2828      */
    2829 #if 1
    2830     if (chip->device_type == DEVICE_INTEL_ICH5 ||
    2831         chip->device_type == DEVICE_INTEL_ICH4)
    2832     {
    2833         pci_read_config_dword(pci, 0x40, &pci_dword);
    2834         printk("acpi_base %x\n", pci_dword);
    2835         pci_read_config_byte(pci, 0x44, &pci_byte);
    2836         printk("acpi_cntl %i\n", pci_byte);
    2837 //        pci_write_config_byte(pci, 0x44, pci_byte & ~(0x10));
    2838         pci_read_config_word(pci, 0x54, &pci_word);
    2839 //        pci_write_config_word(pci, 0x54, pci_word & ~(0x3));
    2840 
    2841         pci_read_config_word(pci, PCI_COMMAND, &pci_word);
    2842 //        pci_write_config_word(pci, PCI_COMMAND, pci_word | (PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
    2843 
    2844         pci_read_config_byte(pci, 0x55, &pci_byte);
    2845         printk("pci config at 0x55 %i\n", pci_byte);
    2846 
    2847         pci_write_config_byte(pci, 0x55, 0x01);
    2848     }
    2849 #endif
    2850     // make sure that pci allow mmio operations
    2851 
    2852     if (pci_resource_flags(pci, /*2*/0) & IORESOURCE_MEM) {     /* ICH4 and Nforce */
    2853         chip->mmio = 1;
    2854         chip->addr = pci_resource_start(pci, /*2*/0);
    2855         chip->remap_addr = (unsigned long)
    2856             ioremap_nocache(chip->addr, pci_resource_len(pci, /*2*/0));
    2857         if (chip->remap_addr == 0) {
    2858             snd_printk("AC'97 space ioremap problem\n");
    2859             snd_intel8x0_free(chip);
    2860             return -EIO;
    2861         }
    2862         printk("ioremap %x len %i remap addr: %x\n",
    2863                chip->addr, pci_resource_len(pci, /*2*/0), chip->remap_addr);
    2864     }
    2865     else if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) {     /* ICH4 and Nforce */
    2866         chip->mmio = 1;
    2867         chip->addr = pci_resource_start(pci, 2);
    2868         chip->remap_addr = (unsigned long)
    2869             ioremap_nocache(chip->addr, pci_resource_len(pci, 2));
    2870         if (chip->remap_addr == 0) {
    2871             snd_printk("AC'97 space ioremap problem\n");
    2872             snd_intel8x0_free(chip);
    2873             return -EIO;
    2874         }
    2875         printk("ioremap %x len %i remap addr: %x\n",
    2876                chip->addr, pci_resource_len(pci, 2), chip->remap_addr);
    2877     } else {
    2878         chip->addr = pci_resource_start(pci, 0);
    2879     }
    2880 
    2881     if (pci_resource_flags(pci, /*3*/1) & IORESOURCE_MEM) {     /* ICH4 */
    2882         chip->bm_mmio = 1;
    2883         chip->bmaddr = pci_resource_start(pci, /*3*/1);
    2884         chip->remap_bmaddr = (unsigned long)
    2885             ioremap_nocache(chip->bmaddr, pci_resource_len(pci, /*3*/1));
    2886         if (chip->remap_bmaddr == 0) {
    2887             snd_printk("Controller space ioremap problem\n");
    2888             snd_intel8x0_free(chip);
    2889             return -EIO;
    2890         }
    2891         printk("ioremap bm %x len %i remap addr: %x\n",
    2892                chip->bmaddr, pci_resource_len(pci, /*3*/1), chip->remap_bmaddr);
    2893     }
    2894     else if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) {     /* ICH4 */
    2895         chip->bm_mmio = 1;
    2896         chip->bmaddr = pci_resource_start(pci, 3);
    2897         chip->remap_bmaddr = (unsigned long)
    2898             ioremap_nocache(chip->bmaddr, pci_resource_len(pci, 3));
    2899         if (chip->remap_bmaddr == 0) {
    2900             snd_printk("Controller space ioremap problem\n");
    2901             snd_intel8x0_free(chip);
    2902             return -EIO;
    2903         }
    2904         printk("ioremap bm %x len %i remap addr: %x\n",
    2905                chip->bmaddr, pci_resource_len(pci, 3), chip->remap_bmaddr);
    2906     } else {
    2907         chip->bmaddr = pci_resource_start(pci, 1);
    2908     }
    2909 
    2910 port_inited:
    2911     chip->bdbars_count = bdbars[device_type];
    2912 
    2913     /* initialize offsets */
    2914     switch (device_type) {
    2915     case DEVICE_NFORCE:
    2916         tbl = nforce_regs;
    2917         break;
    2918     case DEVICE_ALI:
    2919         tbl = ali_regs;
    2920         break;
    2921     default:
    2922         tbl = intel_regs;
    2923         break;
    2924     }
    2925     for (i = 0; i < chip->bdbars_count; i++) {
    2926         ichdev = &chip->ichd[i];
    2927         ichdev->ichd = i;
    2928         ichdev->reg_offset = tbl[i].offset;
    2929         ichdev->int_sta_mask = tbl[i].int_sta_mask;
    2930         if (device_type == DEVICE_SIS) {
    2931             /* SiS 7012 swaps the registers */
    2932             ichdev->roff_sr = ICH_REG_OFF_PICB;
    2933             ichdev->roff_picb = ICH_REG_OFF_SR;
    2934         } else {
    2935             ichdev->roff_sr = ICH_REG_OFF_SR;
    2936             ichdev->roff_picb = ICH_REG_OFF_PICB;
    2937         }
    2938         if (device_type == DEVICE_ALI)
    2939             ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
    2940         /* SIS7012 handles the pcm data in bytes, others are in samples */
    2941         ichdev->pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
    2942     }
    2943 
    2944     /* allocate buffer descriptor lists */
    2945     /* the start of each lists must be aligned to 8 bytes */
    2946     if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
    2947                             chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
    2948                             &chip->bdbars) < 0) {
    2949         snd_intel8x0_free(chip);
    2950         snd_printk(KERN_ERR "intel8x0: cannot allocate buffer descriptors\n");
    2951         return -ENOMEM;
    2952     }
    2953     /* tables must be aligned to 8 bytes here, but the kernel pages
    2954      are much bigger, so we don't care (on i386) */
    2955 
    2956     int_sta_masks = 0;
    2957     for (i = 0; i < chip->bdbars_count; i++) {
    2958         ichdev = &chip->ichd[i];
    2959         ichdev->bdbar = ((u32 *)chip->bdbars.area) +
    2960             (i * ICH_MAX_FRAGS * 2);
    2961         ichdev->bdbar_addr = chip->bdbars.addr +
    2962             (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
    2963         int_sta_masks |= ichdev->int_sta_mask;
    2964     }
    2965     chip->int_sta_reg = device_type == DEVICE_ALI ?
    2966         ICH_REG_ALI_INTERRUPTSR : ICH_REG_GLOB_STA;
    2967     chip->int_sta_mask = int_sta_masks;
    2968 
    2969     /* request irq after initializaing int_sta_mask, etc */
    2970     if (request_irq(pci->irq, snd_intel8x0_interrupt,
    2971                     SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
    2972         snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
    2973         snd_intel8x0_free(chip);
    2974         return -EBUSY;
    2975     }
    2976     chip->irq = pci->irq;
    2977     pci_set_master(pci);
    2978     synchronize_irq(chip->irq);
    2979 
    2980     switch(chip->device_type) {
    2981     case DEVICE_INTEL_ICH4:
    2982     case DEVICE_INTEL_ICH5:
    2983         /* ICH4/5 can have three codecs */
    2984         chip->max_codecs = 3;
    2985         chip->codec_bit = ich_codec_bits;
    2986         chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_TRI;
    2987         break;
    2988     case DEVICE_SIS:
    2989         /* recent SIS7012 can have three codecs */
    2990         chip->max_codecs = 3;
    2991         chip->codec_bit = sis_codec_bits;
    2992         chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_SIS_TRI;
    2993         break;
    2994     default:
    2995         /* others up to two codecs */
    2996         chip->max_codecs = 2;
    2997         chip->codec_bit = ich_codec_bits;
    2998         chip->codec_ready_bits = ICH_PRI | ICH_SRI;
    2999         break;
    3000     }
    3001     for (i = 0; i < chip->max_codecs; i++)
    3002         chip->codec_isr_bits |= chip->codec_bit[i];
    3003 
    3004     if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
    3005         snd_intel8x0_free(chip);
    3006         return err;
    3007     }
    3008 #ifdef DEBUG
    3009     dprintf(("snd_intel8x0_create. cp1"));
    3010 #endif
    3011     if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
    3012         snd_intel8x0_free(chip);
    3013         return err;
    3014     }
    3015 
    3016     *r_intel8x0 = chip;
    3017 #ifdef DEBUG
    3018     dprintf(("snd_intel8x0_create. cp2"));
    3019 #endif
    3020     return 0;
    3021 }
    3022 
    3023 static struct shortname_table {
    3024     unsigned int id;
    3025     const char *s;
    3026 } shortnames[] __devinitdata = {
    3027     { PCI_DEVICE_ID_INTEL_82801, "Intel 82801AA-ICH" },
    3028     { PCI_DEVICE_ID_INTEL_82901, "Intel 82901AB-ICH0" },
    3029     { PCI_DEVICE_ID_INTEL_82801BA, "Intel 82801BA-ICH2" },
    3030     { PCI_DEVICE_ID_INTEL_440MX, "Intel 440MX" },
    3031     { PCI_DEVICE_ID_INTEL_ICH3, "Intel 82801CA-ICH3" },
    3032     { PCI_DEVICE_ID_INTEL_ICH4, "Intel 82801DB-ICH4" },
    3033     { PCI_DEVICE_ID_INTEL_ICH5, "Intel ICH5" },
    3034     { PCI_DEVICE_ID_INTEL_ESB_5, "Intel ICH 6300ESB" },
    3035     { PCI_DEVICE_ID_INTEL_ICH6_18, "Intel ICH6" },
    3036     { PCI_DEVICE_ID_INTEL_ICH7_20, "Intel ICH7" },
    3037     { PCI_DEVICE_ID_INTEL_ESB2_14, "Intel ESB2" },
    3038     { PCI_DEVICE_ID_SI_7012, "SiS SI7012" },
    3039     { PCI_DEVICE_ID_NVIDIA_MCP_AUDIO, "NVidia nForce" },
    3040     { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
    3041     { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
    3042     { PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO, "NVidia CK8S" },
    3043     { PCI_DEVICE_ID_NVIDIA_CK804_AUDIO, "NVidia CK804" },
    3044     { PCI_DEVICE_ID_NVIDIA_CK8_AUDIO, "NVidia CK8" },
    3045     { 0x003a, "NVidia MCP04" },
    3046     { 0x746d, "AMD AMD8111" },
    3047     { 0x7445, "AMD AMD768" },
    3048     { 0x5455, "ALi M5455" },
    3049     { 0, 0 },
    3050 };
     2992        return 0;
     2993}
    30512994
    30522995static int __devinit snd_intel8x0_probe(struct pci_dev *pci,
    3053                                         const struct pci_device_id *pci_id)
    3054 {
    3055     static int dev;
    3056     snd_card_t *card;
    3057     struct intel8x0 *chip;
    3058     int err;
    3059     struct shortname_table *name;
    3060 
    3061     if (dev >= SNDRV_CARDS)
    3062         return -ENODEV;
    3063     if (!enable[dev]) {
    3064         dev++;
    3065         return -ENOENT;
    3066     }
    3067 
    3068     card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
    3069     if (card == NULL)
    3070         return -ENOMEM;
    3071     switch (pci_id->driver_data) {
    3072     case DEVICE_NFORCE:
    3073         strcpy(card->driver, "NFORCE");
    3074         break;
    3075     case DEVICE_INTEL_ICH4:
    3076         strcpy(card->driver, "ICH4");
    3077         break;
    3078     case DEVICE_INTEL_ICH5:
    3079         strcpy(card->driver, "ICH5");
    3080         break;
    3081     default:
    3082         strcpy(card->driver, "ICH");
    3083         break;
    3084     }
    3085 
    3086     strcpy(card->shortname, "Intel ICH");
    3087     for (name = shortnames; name->id; name++) {
    3088         if (pci->device == name->id) {
    3089             strcpy(card->shortname, name->s);
    3090             break;
    3091         }
    3092     }
    3093     //  card->dev = &pci->dev;
    3094     if (buggy_irq < 0) {
    3095         /* some Nforce[2] and ICH boards have problems with IRQ handling.
    3096          * Needs to return IRQ_HANDLED for unknown irqs.
    3097          */
    3098         if (pci_id->driver_data == DEVICE_NFORCE)
    3099             buggy_irq = 1;
    3100         else
    3101             buggy_irq = 0;
    3102     }
    3103 
    3104     if ((err = snd_intel8x0_create(card, pci, pci_id->driver_data, &chip)) < 0) {
    3105         snd_card_free(card);
    3106         printk(KERN_ERR "ICH: create error. err = %x\n",err);
    3107         return err;
    3108     }
    3109 
    3110     card->private_data = chip;
    3111 
    3112     if ((err = snd_intel8x0_mixer(chip, ac97_clock[dev], ac97_quirk[dev])) < 0){
    3113         snd_card_free(card);
    3114         //            printk(KERN_ERR "ICH: mixer error. err = %x\n",err);
    3115         return err;
    3116     }
    3117 #ifdef DEBUG
    3118     dprintf(("snd_intel8x0_mixer passed"));
    3119 #endif
    3120     if ((err = snd_intel8x0_pcm(chip)) < 0) {
    3121         snd_card_free(card);
    3122         printk(KERN_ERR "ICH: pcm error. err = %x\n",err);
    3123         return err;
    3124     }
    3125 
    3126 #ifdef DEBUG
    3127     dprintf(("snd_intel8x0_pcm passed"));
    3128 #endif
    3129     if (mpu_port[dev] == 0x300 || mpu_port[dev] == 0x330) {
    3130         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_INTEL8X0,
    3131                                        mpu_port[dev], 0,
    3132                                        -1, 0, &chip->rmidi)) < 0) {
    3133             printk(KERN_ERR "intel8x0: no UART401 device at 0x%x, skipping.\n", mpu_port[dev]);
    3134             mpu_port[dev] = 0;
    3135         }
    3136     } else
    3137         mpu_port[dev] = 0;
    3138 
    3139     snd_intel8x0_proc_init(chip);
    3140 #ifdef DEBUG
    3141     dprintf(("snd_intel8x0_proc_init passed"));
    3142 #endif
    3143 
    3144     sprintf(card->longname, "%s at 0x%lx, irq %i",
    3145             card->shortname, chip->addr, chip->irq);
    3146 
    3147     if (! ac97_clock[dev])
    3148         intel8x0_measure_ac97_clock(chip);
    3149 #ifdef DEBUG
    3150     dprintf(("intel8x0_measure_ac97_clock passed"));
    3151 #endif
    3152 
    3153     if ((err = snd_card_register(card)) < 0) {
    3154         snd_card_free(card);
    3155         printk(KERN_ERR "ICH: card register error. err = %x\n",err);
    3156         return err;
    3157     }
    3158 #ifdef DEBUG
    3159     dprintf(("snd_card_register passed"));
    3160 #endif
    3161     pci_set_drvdata(pci, card);
    3162     dev++;
    3163     return 0;
     2996                                        const struct pci_device_id *pci_id)
     2997{
     2998        struct snd_card *card;
     2999        struct intel8x0 *chip;
     3000        int err;
     3001        struct shortname_table *name;
     3002
     3003        card = snd_card_new(index, id, THIS_MODULE, 0);
     3004        if (card == NULL)
     3005                return -ENOMEM;
     3006
     3007        if (spdif_aclink < 0)
     3008                spdif_aclink = check_default_spdif_aclink(pci);
     3009
     3010        strcpy(card->driver, "ICH");
     3011        if (!spdif_aclink) {
     3012                switch (pci_id->driver_data) {
     3013                case DEVICE_NFORCE:
     3014                        strcpy(card->driver, "NFORCE");
     3015                        break;
     3016                case DEVICE_INTEL_ICH4:
     3017                        strcpy(card->driver, "ICH4");
     3018                }
     3019        }
     3020
     3021        strcpy(card->shortname, "Intel ICH");
     3022        for (name = shortnames; name->id; name++) {
     3023                if (pci->device == name->id) {
     3024                        strcpy(card->shortname, name->s);
     3025                        break;
     3026                }
     3027        }
     3028
     3029        if (buggy_irq < 0) {
     3030                /* some Nforce[2] and ICH boards have problems with IRQ handling.
     3031                 * Needs to return IRQ_HANDLED for unknown irqs.
     3032                 */
     3033                if (pci_id->driver_data == DEVICE_NFORCE)
     3034                        buggy_irq = 1;
     3035                else
     3036                        buggy_irq = 0;
     3037        }
     3038
     3039        if ((err = snd_intel8x0_create(card, pci, pci_id->driver_data,
     3040                                       &chip)) < 0) {
     3041                snd_card_free(card);
     3042                return err;
     3043        }
     3044        card->private_data = chip;
     3045
     3046        if ((err = snd_intel8x0_mixer(chip, ac97_clock, ac97_quirk)) < 0) {
     3047                snd_card_free(card);
     3048                return err;
     3049        }
     3050        if ((err = snd_intel8x0_pcm(chip)) < 0) {
     3051                snd_card_free(card);
     3052                return err;
     3053        }
     3054       
     3055        snd_intel8x0_proc_init(chip);
     3056
     3057        snprintf(card->longname, sizeof(card->longname),
     3058                 "%s with %s at irq %i", card->shortname,
     3059                 snd_ac97_get_short_name(chip->ac97[0]), chip->irq);
     3060
     3061        if (! ac97_clock)
     3062                intel8x0_measure_ac97_clock(chip);
     3063
     3064        if ((err = snd_card_register(card)) < 0) {
     3065                snd_card_free(card);
     3066                return err;
     3067        }
     3068        pci_set_drvdata(pci, card);
     3069        return 0;
    31643070}
    31653071
    31663072static void __devexit snd_intel8x0_remove(struct pci_dev *pci)
    31673073{
    3168     snd_card_free(pci_get_drvdata(pci));
    3169     pci_set_drvdata(pci, NULL);
     3074        snd_card_free(pci_get_drvdata(pci));
     3075        pci_set_drvdata(pci, NULL);
    31703076}
    31713077
     
    31743080        .id_table = snd_intel8x0_ids,
    31753081        .probe = snd_intel8x0_probe,
    3176         .remove = snd_intel8x0_remove,
     3082        .remove = __devexit_p(snd_intel8x0_remove),
    31773083#ifdef CONFIG_PM
    3178         .suspend = intel8x0_suspend,
    3179         .resume = intel8x0_resume,
     3084        .suspend = intel8x0_suspend,
     3085        .resume = intel8x0_resume,
    31803086#endif
    31813087};
    31823088
    3183 #if defined(SUPPORT_JOYSTICK) || defined(SUPPORT_MIDI)
    3184 /*
    3185  * initialize joystick/midi addresses
    3186  */
    3187 #ifdef SUPPORT_JOYSTICK
    3188 /* there is only one available device, so we keep it here */
    3189 static struct pci_dev *ich_gameport_pci;
    3190 static struct gameport ich_gameport = { .io = 0x200 };
    3191 #endif
    3192 
    3193 
    3194 static int __devinit snd_intel8x0_joystick_probe(struct pci_dev *pci,
    3195                                                  const struct pci_device_id *id)
    3196 {
    3197     u16 val;
    3198     static int dev;
    3199     if (dev >= SNDRV_CARDS)
    3200         return -ENODEV;
    3201     if (!enable[dev]) {
    3202         dev++;
    3203         return -ENOENT;
    3204     }
    3205 
    3206     pci_read_config_word(pci, 0xe6, &val);
    3207 #ifdef SUPPORT_JOYSTICK
    3208     if (joystick[dev]) {
    3209         if (! request_region(ich_gameport.io, 8, "ICH gameport")) {
    3210             printk(KERN_WARNING "intel8x0: cannot grab gameport 0x%x\n",  ich_gameport.io);
    3211             joystick[dev] = 0;
    3212         } else {
    3213             ich_gameport_pci = pci;
    3214             gameport_register_port(&ich_gameport);
    3215             val |= 0x100;
    3216         }
    3217     }
    3218 #endif
    3219 #ifdef SUPPORT_MIDI
    3220     if (mpu_port[dev] > 0) {
    3221         if (mpu_port[dev] == 0x300 || mpu_port[dev] == 0x330) {
    3222             u8 b;
    3223             val |= 0x20;
    3224             pci_read_config_byte(pci, 0xe2, &b);
    3225             if (mpu_port[dev] == 0x300)
    3226                 b |= 0x08;
    3227             else
    3228                 b &= ~0x08;
    3229             pci_write_config_byte(pci, 0xe2, b);
    3230         }
    3231     }
    3232 #endif
    3233     pci_write_config_word(pci, 0xe6, val);
    3234     return 0;
    3235 }
    3236 
    3237 #if 0 // fixme to be gone?
    3238 static void __devexit snd_intel8x0_joystick_remove(struct pci_dev *pci)
    3239 {
    3240     u16 val;
    3241 #ifdef SUPPORT_JOYSTICK
    3242     if (ich_gameport_pci == pci) {
    3243         gameport_unregister_port(&ich_gameport);
    3244         release_region(ich_gameport.io, 8);
    3245         ich_gameport_pci = NULL;
    3246     }
    3247 #endif
    3248     /* disable joystick and MIDI */
    3249     pci_read_config_word(pci, 0xe6, &val);
    3250     val &= ~0x120;
    3251     pci_write_config_word(pci, 0xe6, val);
    3252 }
    3253 #endif // fixme to be gone
    3254 
    3255 static struct pci_device_id snd_intel8x0_joystick_ids[] = {
    3256     { 0x8086, 0x2410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },        /* 82801AA */
    3257     { 0x8086, 0x2420, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },        /* 82901AB */
    3258     { 0x8086, 0x2440, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ICH2 */
    3259     { 0x8086, 0x244c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* ICH2M */
    3260     { 0x8086, 0x248c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },        /* ICH3 */
    3261     // { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },     /* 440MX */
    3262     // { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },     /* SI7012 */
    3263     //Rudi: this is not a joystick !!!!
    3264     //  { 0x10de, 0x01b2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* NFORCE */
    3265     //  { 0x10de, 0x006b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* NFORCE2 */
    3266     { 0x10de, 0x00db, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },        /* NFORCE3 */
    3267     { 0, }
    3268 };
    3269 
    3270 static struct pci_driver joystick_driver = {
    3271     0, 0, 0,
    3272     /*  name:     */ "Intel ICH Joystick",
    3273     /*  id_table: */ snd_intel8x0_joystick_ids,
    3274     /*  probe:    */ snd_intel8x0_joystick_probe,
    3275     0,0,0
    3276 };
    3277 static int have_joystick;
    3278 #endif
    32793089
    32803090static int __init alsa_card_intel8x0_init(void)
    32813091{
    3282     int err = 0;
    3283 
    3284     //    printk(KERN_ERR "alsa_card_intel8x0_init\n");
    3285 #ifdef TARGET_OS2
    3286     if (midi_port > 0) mpu_port[0] = midi_port;
    3287 #endif
    3288 
    3289     if ((err = pci_module_init(&driver)) < 0) {
    3290 #ifdef MODULE
    3291         //     printk(KERN_ERR "Intel ICH soundcard not found or device busy. err = %x\n",err);
    3292 #endif
    3293         return err;
    3294     }
    3295 #if defined(SUPPORT_JOYSTICK) || defined(SUPPORT_MIDI)
    3296     if (pci_module_init(&joystick_driver) < 0) {
    3297         snd_printdd(KERN_INFO "no joystick found\n");
    3298         have_joystick = 0;
    3299     } else {
    3300         snd_printdd(KERN_INFO "joystick(s) found\n");
    3301         have_joystick = 1;
    3302     }
    3303 #endif
    3304     return 0;
     3092        return pci_register_driver(&driver);
    33053093}
    33063094
    33073095static void __exit alsa_card_intel8x0_exit(void)
    33083096{
    3309     pci_unregister_driver(&driver);
    3310 #if defined(SUPPORT_JOYSTICK) || defined(SUPPORT_MIDI)
    3311     if (have_joystick)
    3312         pci_unregister_driver(&joystick_driver);
    3313 #endif
     3097        pci_unregister_driver(&driver);
    33143098}
    33153099
    33163100module_init(alsa_card_intel8x0_init)
    33173101module_exit(alsa_card_intel8x0_exit)
    3318 
    3319 #ifndef MODULE
    3320 
    3321 /* format is: snd-intel8x0=enable,index,id,ac97_clock,mpu_port,joystick */
    3322 
    3323 static int __init alsa_card_intel8x0_setup(char *str)
    3324 {
    3325     static unsigned __initdata nr_dev = 0;
    3326 
    3327     if (nr_dev >= SNDRV_CARDS)
    3328         return 0;
    3329     (void)(get_option(&str,&enable[nr_dev]) == 2 &&
    3330            get_option(&str,&index[nr_dev]) == 2 &&
    3331            get_id(&str,&id[nr_dev]) == 2 &&
    3332            get_option(&str,&ac97_clock[nr_dev]) == 2 &&
    3333            get_option(&str,&ac97_quirk[nr_dev]) == 2
    3334 #ifdef SUPPORT_MIDI
    3335            && get_option(&str,&mpu_port[nr_dev]) == 2
    3336 #endif
    3337 #ifdef SUPPORT_JOYSTICK
    3338            && get_option(&str,&joystick[nr_dev]) == 2
    3339 #endif
    3340           );
    3341     nr_dev++;
    3342     return 1;
    3343 }
    3344 
    3345 __setup("snd-intel8x0=", alsa_card_intel8x0_setup);
    3346 
    3347 #endif /* ifndef MODULE */
Note: See TracChangeset for help on using the changeset viewer.