ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / intel8x0m.c
1 /*
2  *   ALSA modem driver for Intel ICH (i8x0) chipsets
3  *
4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
5  *
6  *   This is modified (by Sasha Khapyorsky <sashak@smlink.com>) version
7  *   of ALSA ICH sound driver intel8x0.c .
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */      
25
26 #include <sound/driver.h>
27 #include <asm/io.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/init.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33 #include <linux/gameport.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/info.h>
38 #include <sound/mpu401.h>
39 #define SNDRV_GET_ID
40 #include <sound/initval.h>
41
42 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
43 MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440 modem");
44 MODULE_LICENSE("GPL");
45 MODULE_CLASSES("{sound}");
46 MODULE_DEVICES("{{Intel,82801AA-ICH},"
47                 "{Intel,82901AB-ICH0},"
48                 "{Intel,82801BA-ICH2},"
49                 "{Intel,82801CA-ICH3},"
50                 "{Intel,82801DB-ICH4},"
51                 "{Intel,ICH5},"
52                 "{Intel,MX440}}");
53
54
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
57 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
58 static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
59
60 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
61 MODULE_PARM_DESC(index, "Index value for Intel i8x0 modemcard.");
62 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
63 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
64 MODULE_PARM_DESC(id, "ID string for Intel i8x0 modemcard.");
65 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
66 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
67 MODULE_PARM_DESC(enable, "Enable Intel i8x0 modemcard.");
68 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
69 MODULE_PARM(ac97_clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
70 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
71 MODULE_PARM_SYNTAX(ac97_clock, SNDRV_ENABLED ",default:0");
72
73 /*
74  *  Direct registers
75  */
76
77 #ifndef PCI_DEVICE_ID_INTEL_82801_6
78 #define PCI_DEVICE_ID_INTEL_82801_6     0x2416
79 #endif
80 #ifndef PCI_DEVICE_ID_INTEL_82901_6
81 #define PCI_DEVICE_ID_INTEL_82901_6     0x2426
82 #endif
83 #ifndef PCI_DEVICE_ID_INTEL_82801BA_6
84 #define PCI_DEVICE_ID_INTEL_82801BA_6   0x2446
85 #endif
86 #ifndef PCI_DEVICE_ID_INTEL_440MX_6
87 #define PCI_DEVICE_ID_INTEL_440MX_6     0x7196
88 #endif
89 #ifndef PCI_DEVICE_ID_INTEL_ICH3_6
90 #define PCI_DEVICE_ID_INTEL_ICH3_6      0x2486
91 #endif
92 #ifndef PCI_DEVICE_ID_INTEL_ICH4_6
93 #define PCI_DEVICE_ID_INTEL_ICH4_6      0x24c6
94 #endif
95 #ifndef PCI_DEVICE_ID_INTEL_ICH5_6
96 #define PCI_DEVICE_ID_INTEL_ICH5_6      0x24d6
97 #endif
98 #ifndef PCI_DEVICE_ID_SI_7013
99 #define PCI_DEVICE_ID_SI_7013           0x7013
100 #endif
101 #if 0
102 #ifndef PCI_DEVICE_ID_NVIDIA_MCP_AUDIO
103 #define PCI_DEVICE_ID_NVIDIA_MCP_AUDIO  0x01b1
104 #endif
105 #ifndef PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO
106 #define PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO 0x006a
107 #endif
108 #ifndef PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO
109 #define PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO 0x00da
110 #endif
111 #endif
112
113 enum { DEVICE_INTEL, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
114
115 #define ICHREG(x) ICH_REG_##x
116
117 #define DEFINE_REGSET(name,base) \
118 enum { \
119         ICH_REG_##name##_BDBAR  = base + 0x0,   /* dword - buffer descriptor list base address */ \
120         ICH_REG_##name##_CIV    = base + 0x04,  /* byte - current index value */ \
121         ICH_REG_##name##_LVI    = base + 0x05,  /* byte - last valid index */ \
122         ICH_REG_##name##_SR     = base + 0x06,  /* byte - status register */ \
123         ICH_REG_##name##_PICB   = base + 0x08,  /* word - position in current buffer */ \
124         ICH_REG_##name##_PIV    = base + 0x0a,  /* byte - prefetched index value */ \
125         ICH_REG_##name##_CR     = base + 0x0b,  /* byte - control register */ \
126 };
127
128 /* busmaster blocks */
129 DEFINE_REGSET(OFF, 0);          /* offset */
130
131 /* values for each busmaster block */
132
133 /* LVI */
134 #define ICH_REG_LVI_MASK                0x1f
135
136 /* SR */
137 #define ICH_FIFOE                       0x10    /* FIFO error */
138 #define ICH_BCIS                        0x08    /* buffer completion interrupt status */
139 #define ICH_LVBCI                       0x04    /* last valid buffer completion interrupt */
140 #define ICH_CELV                        0x02    /* current equals last valid */
141 #define ICH_DCH                         0x01    /* DMA controller halted */
142
143 /* PIV */
144 #define ICH_REG_PIV_MASK                0x1f    /* mask */
145
146 /* CR */
147 #define ICH_IOCE                        0x10    /* interrupt on completion enable */
148 #define ICH_FEIE                        0x08    /* fifo error interrupt enable */
149 #define ICH_LVBIE                       0x04    /* last valid buffer interrupt enable */
150 #define ICH_RESETREGS                   0x02    /* reset busmaster registers */
151 #define ICH_STARTBM                     0x01    /* start busmaster operation */
152
153
154 /* global block */
155 #define ICH_REG_GLOB_CNT                0x3c    /* dword - global control */
156 #define   ICH_TRIE              0x00000040      /* tertiary resume interrupt enable */
157 #define   ICH_SRIE              0x00000020      /* secondary resume interrupt enable */
158 #define   ICH_PRIE              0x00000010      /* primary resume interrupt enable */
159 #define   ICH_ACLINK            0x00000008      /* AClink shut off */
160 #define   ICH_AC97WARM          0x00000004      /* AC'97 warm reset */
161 #define   ICH_AC97COLD          0x00000002      /* AC'97 cold reset */
162 #define   ICH_GIE               0x00000001      /* GPI interrupt enable */
163 #define ICH_REG_GLOB_STA                0x40    /* dword - global status */
164 #define   ICH_TRI               0x20000000      /* ICH4: tertiary (AC_SDIN2) resume interrupt */
165 #define   ICH_TCR               0x10000000      /* ICH4: tertiary (AC_SDIN2) codec ready */
166 #define   ICH_BCS               0x08000000      /* ICH4: bit clock stopped */
167 #define   ICH_SPINT             0x04000000      /* ICH4: S/PDIF interrupt */
168 #define   ICH_P2INT             0x02000000      /* ICH4: PCM2-In interrupt */
169 #define   ICH_M2INT             0x01000000      /* ICH4: Mic2-In interrupt */
170 #define   ICH_SAMPLE_CAP        0x00c00000      /* ICH4: sample capability bits (RO) */
171 #define   ICH_MULTICHAN_CAP     0x00300000      /* ICH4: multi-channel capability bits (RO) */
172 #define   ICH_MD3               0x00020000      /* modem power down semaphore */
173 #define   ICH_AD3               0x00010000      /* audio power down semaphore */
174 #define   ICH_RCS               0x00008000      /* read completion status */
175 #define   ICH_BIT3              0x00004000      /* bit 3 slot 12 */
176 #define   ICH_BIT2              0x00002000      /* bit 2 slot 12 */
177 #define   ICH_BIT1              0x00001000      /* bit 1 slot 12 */
178 #define   ICH_SRI               0x00000800      /* secondary (AC_SDIN1) resume interrupt */
179 #define   ICH_PRI               0x00000400      /* primary (AC_SDIN0) resume interrupt */
180 #define   ICH_SCR               0x00000200      /* secondary (AC_SDIN1) codec ready */
181 #define   ICH_PCR               0x00000100      /* primary (AC_SDIN0) codec ready */
182 #define   ICH_MCINT             0x00000080      /* MIC capture interrupt */
183 #define   ICH_POINT             0x00000040      /* playback interrupt */
184 #define   ICH_PIINT             0x00000020      /* capture interrupt */
185 #define   ICH_NVSPINT           0x00000010      /* nforce spdif interrupt */
186 #define   ICH_MOINT             0x00000004      /* modem playback interrupt */
187 #define   ICH_MIINT             0x00000002      /* modem capture interrupt */
188 #define   ICH_GSCI              0x00000001      /* GPI status change interrupt */
189 #define ICH_REG_ACC_SEMA                0x44    /* byte - codec write semaphore */
190 #define   ICH_CAS               0x01            /* codec access semaphore */
191
192 #define ICH_MAX_FRAGS           32              /* max hw frags */
193
194
195 /*
196  *  
197  */
198
199 enum { ICHD_MDMIN, ICHD_MDMOUT, ICHD_MDMLAST = ICHD_MDMOUT };
200 enum { ALID_MDMIN, ALID_MDMOUT, ALID_MDMLAST = ALID_MDMOUT };
201
202 #define get_ichdev(substream) (ichdev_t *)(substream->runtime->private_data)
203
204 typedef struct {
205         unsigned int ichd;                      /* ich device number */
206         unsigned long reg_offset;               /* offset to bmaddr */
207         u32 *bdbar;                             /* CPU address (32bit) */
208         unsigned int bdbar_addr;                /* PCI bus address (32bit) */
209         snd_pcm_substream_t *substream;
210         unsigned int physbuf;                   /* physical address (32bit) */
211         unsigned int size;
212         unsigned int fragsize;
213         unsigned int fragsize1;
214         unsigned int position;
215         int frags;
216         int lvi;
217         int lvi_frag;
218         int civ;
219         int ack;
220         int ack_reload;
221         unsigned int ack_bit;
222         unsigned int roff_sr;
223         unsigned int roff_picb;
224         unsigned int int_sta_mask;              /* interrupt status mask */
225         unsigned int ali_slot;                  /* ALI DMA slot */
226         ac97_t *ac97;
227 } ichdev_t;
228
229 typedef struct _snd_intel8x0m intel8x0_t;
230 #define chip_t intel8x0_t
231
232 struct _snd_intel8x0m {
233         unsigned int device_type;
234         char ac97_name[64];
235         char ctrl_name[64];
236
237         int irq;
238
239         unsigned int mmio;
240         unsigned long addr;
241         unsigned long remap_addr;
242         struct resource *res;
243         unsigned int bm_mmio;
244         unsigned long bmaddr;
245         unsigned long remap_bmaddr;
246         struct resource *res_bm;
247
248         struct pci_dev *pci;
249         snd_card_t *card;
250
251         int pcm_devs;
252         snd_pcm_t *pcm[2];
253         ichdev_t ichd[2];
254
255         int in_ac97_init: 1;
256
257         ac97_bus_t *ac97_bus;
258         ac97_t *ac97;
259
260         spinlock_t reg_lock;
261         spinlock_t ac97_lock;
262         
263         struct snd_dma_device dma_dev;
264         struct snd_dma_buffer bdbars;
265         u32 bdbars_count;
266         u32 int_sta_reg;                /* interrupt status register */
267         u32 int_sta_mask;               /* interrupt status mask */
268         unsigned int pcm_pos_shift;
269         
270 #ifdef CONFIG_PM
271         int in_suspend;
272 #endif
273 };
274
275 static struct pci_device_id snd_intel8x0m_ids[] = {
276         { 0x8086, 0x2416, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
277         { 0x8086, 0x2426, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
278         { 0x8086, 0x2446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
279         { 0x8086, 0x2486, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
280         { 0x8086, 0x24c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH4 */
281         { 0x8086, 0x24d6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH5 */
282         { 0x8086, 0x7196, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
283         { 0x1022, 0x7446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
284 #if 0
285         /* TODO: support needed */
286         { 0x1039, 0x7013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS },   /* SI7013 */
287         { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE */
288         { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE2 */
289         { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE3 */
290         { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
291         { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI },   /* Ali5455 */
292 #endif
293         { 0, }
294 };
295
296 MODULE_DEVICE_TABLE(pci, snd_intel8x0m_ids);
297
298 /*
299  *  Lowlevel I/O - busmaster
300  */
301
302 static u8 igetbyte(intel8x0_t *chip, u32 offset)
303 {
304         if (chip->bm_mmio)
305                 return readb(chip->remap_bmaddr + offset);
306         else
307                 return inb(chip->bmaddr + offset);
308 }
309
310 static u16 igetword(intel8x0_t *chip, u32 offset)
311 {
312         if (chip->bm_mmio)
313                 return readw(chip->remap_bmaddr + offset);
314         else
315                 return inw(chip->bmaddr + offset);
316 }
317
318 static u32 igetdword(intel8x0_t *chip, u32 offset)
319 {
320         if (chip->bm_mmio)
321                 return readl(chip->remap_bmaddr + offset);
322         else
323                 return inl(chip->bmaddr + offset);
324 }
325
326 static void iputbyte(intel8x0_t *chip, u32 offset, u8 val)
327 {
328         if (chip->bm_mmio)
329                 writeb(val, chip->remap_bmaddr + offset);
330         else
331                 outb(val, chip->bmaddr + offset);
332 }
333
334 static void iputdword(intel8x0_t *chip, u32 offset, u32 val)
335 {
336         if (chip->bm_mmio)
337                 writel(val, chip->remap_bmaddr + offset);
338         else
339                 outl(val, chip->bmaddr + offset);
340 }
341
342 /*
343  *  Lowlevel I/O - AC'97 registers
344  */
345
346 static u16 iagetword(intel8x0_t *chip, u32 offset)
347 {
348         if (chip->mmio)
349                 return readw(chip->remap_addr + offset);
350         else
351                 return inw(chip->addr + offset);
352 }
353
354 static void iaputword(intel8x0_t *chip, u32 offset, u16 val)
355 {
356         if (chip->mmio)
357                 writew(val, chip->remap_addr + offset);
358         else
359                 outw(val, chip->addr + offset);
360 }
361
362 /*
363  *  Basic I/O
364  */
365
366 /*
367  * access to AC97 codec via normal i/o (for ICH and SIS7013)
368  */
369
370 /* return the GLOB_STA bit for the corresponding codec */
371 static unsigned int get_ich_codec_bit(intel8x0_t *chip, unsigned int codec)
372 {
373         static unsigned int codec_bit[3] = {
374                 ICH_PCR, ICH_SCR, ICH_TCR
375         };
376         snd_assert(codec < 3, return ICH_PCR);
377         return codec_bit[codec];
378 }
379
380 static int snd_intel8x0m_codec_semaphore(intel8x0_t *chip, unsigned int codec)
381 {
382         int time;
383         
384         if (codec > 1)
385                 return -EIO;
386         codec = get_ich_codec_bit(chip, codec);
387
388         /* codec ready ? */
389         if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
390                 return -EIO;
391
392         /* Anyone holding a semaphore for 1 msec should be shot... */
393         time = 100;
394         do {
395                 if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
396                         return 0;
397                 udelay(10);
398         } while (time--);
399
400         /* access to some forbidden (non existant) ac97 registers will not
401          * reset the semaphore. So even if you don't get the semaphore, still
402          * continue the access. We don't need the semaphore anyway. */
403         snd_printk("codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
404                         igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
405         iagetword(chip, 0);     /* clear semaphore flag */
406         /* I don't care about the semaphore */
407         return -EBUSY;
408 }
409  
410 static void snd_intel8x0_codec_write(ac97_t *ac97,
411                                      unsigned short reg,
412                                      unsigned short val)
413 {
414         intel8x0_t *chip = snd_magic_cast(intel8x0_t, ac97->private_data, return);
415         
416         spin_lock(&chip->ac97_lock);
417         if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
418                 if (! chip->in_ac97_init)
419                         snd_printk("codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
420         }
421         iaputword(chip, reg + ac97->num * 0x80, val);
422         spin_unlock(&chip->ac97_lock);
423 }
424
425 static unsigned short snd_intel8x0_codec_read(ac97_t *ac97,
426                                               unsigned short reg)
427 {
428         intel8x0_t *chip = snd_magic_cast(intel8x0_t, ac97->private_data, return ~0);
429         unsigned short res;
430         unsigned int tmp;
431
432         spin_lock(&chip->ac97_lock);
433         if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
434                 if (! chip->in_ac97_init)
435                         snd_printk("codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
436                 res = 0xffff;
437         } else {
438                 res = iagetword(chip, reg + ac97->num * 0x80);
439                 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
440                         /* reset RCS and preserve other R/WC bits */
441                         iputdword(chip, ICHREG(GLOB_STA), tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
442                         if (! chip->in_ac97_init)
443                                 snd_printk("codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
444                         res = 0xffff;
445                 }
446         }
447         spin_unlock(&chip->ac97_lock);
448         return res;
449 }
450
451
452 /*
453  * DMA I/O
454  */
455 static void snd_intel8x0_setup_periods(intel8x0_t *chip, ichdev_t *ichdev) 
456 {
457         int idx;
458         u32 *bdbar = ichdev->bdbar;
459         unsigned long port = ichdev->reg_offset;
460
461         iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
462         if (ichdev->size == ichdev->fragsize) {
463                 ichdev->ack_reload = ichdev->ack = 2;
464                 ichdev->fragsize1 = ichdev->fragsize >> 1;
465                 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
466                         bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
467                         bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
468                                                      ichdev->fragsize1 >> chip->pcm_pos_shift);
469                         bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
470                         bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
471                                                      ichdev->fragsize1 >> chip->pcm_pos_shift);
472                 }
473                 ichdev->frags = 2;
474         } else {
475                 ichdev->ack_reload = ichdev->ack = 1;
476                 ichdev->fragsize1 = ichdev->fragsize;
477                 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
478                         bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
479                         bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
480                                                      ichdev->fragsize >> chip->pcm_pos_shift);
481                         // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
482                 }
483                 ichdev->frags = ichdev->size / ichdev->fragsize;
484         }
485         iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
486         ichdev->civ = 0;
487         iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
488         ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
489         ichdev->position = 0;
490 #if 0
491         printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
492                         ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
493 #endif
494         /* clear interrupts */
495         iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
496 }
497
498 /*
499  *  Interrupt handler
500  */
501
502 static inline void snd_intel8x0_update(intel8x0_t *chip, ichdev_t *ichdev)
503 {
504         unsigned long port = ichdev->reg_offset;
505         int civ, i, step;
506         int ack = 0;
507
508         civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
509         if (civ == ichdev->civ) {
510                 // snd_printd("civ same %d\n", civ);
511                 step = 1;
512                 ichdev->civ++;
513                 ichdev->civ &= ICH_REG_LVI_MASK;
514         } else {
515                 step = civ - ichdev->civ;
516                 if (step < 0)
517                         step += ICH_REG_LVI_MASK + 1;
518                 // if (step != 1)
519                 //      snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
520                 ichdev->civ = civ;
521         }
522
523         ichdev->position += step * ichdev->fragsize1;
524         ichdev->position %= ichdev->size;
525         ichdev->lvi += step;
526         ichdev->lvi &= ICH_REG_LVI_MASK;
527         iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
528         for (i = 0; i < step; i++) {
529                 ichdev->lvi_frag++;
530                 ichdev->lvi_frag %= ichdev->frags;
531                 ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
532         // 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));
533                 if (--ichdev->ack == 0) {
534                         ichdev->ack = ichdev->ack_reload;
535                         ack = 1;
536                 }
537         }
538         if (ack && ichdev->substream) {
539                 spin_unlock(&chip->reg_lock);
540                 snd_pcm_period_elapsed(ichdev->substream);
541                 spin_lock(&chip->reg_lock);
542         }
543         iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
544 }
545
546 static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
547 {
548         intel8x0_t *chip = snd_magic_cast(intel8x0_t, dev_id, return IRQ_NONE);
549         ichdev_t *ichdev;
550         unsigned int status;
551         unsigned int i;
552
553         spin_lock(&chip->reg_lock);
554         status = igetdword(chip, chip->int_sta_reg);
555         if ((status & chip->int_sta_mask) == 0) {
556                 if (status)
557                         iputdword(chip, chip->int_sta_reg, status);
558                 spin_unlock(&chip->reg_lock);
559                 return IRQ_NONE;
560         }
561
562         for (i = 0; i < chip->bdbars_count; i++) {
563                 ichdev = &chip->ichd[i];
564                 if (status & ichdev->int_sta_mask)
565                         snd_intel8x0_update(chip, ichdev);
566         }
567
568         /* ack them */
569         iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
570         spin_unlock(&chip->reg_lock);
571         
572         return IRQ_HANDLED;
573 }
574
575 /*
576  *  PCM part
577  */
578
579 static int snd_intel8x0_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
580 {
581         intel8x0_t *chip = snd_pcm_substream_chip(substream);
582         ichdev_t *ichdev = get_ichdev(substream);
583         unsigned char val = 0;
584         unsigned long port = ichdev->reg_offset;
585
586         switch (cmd) {
587         case SNDRV_PCM_TRIGGER_START:
588         case SNDRV_PCM_TRIGGER_RESUME:
589                 val = ICH_IOCE | ICH_STARTBM;
590                 break;
591         case SNDRV_PCM_TRIGGER_STOP:
592         case SNDRV_PCM_TRIGGER_SUSPEND:
593                 val = 0;
594                 break;
595         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
596                 val = ICH_IOCE;
597                 break;
598         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
599                 val = ICH_IOCE | ICH_STARTBM;
600                 break;
601         default:
602                 return -EINVAL;
603         }
604         iputbyte(chip, port + ICH_REG_OFF_CR, val);
605         if (cmd == SNDRV_PCM_TRIGGER_STOP) {
606                 /* wait until DMA stopped */
607                 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
608                 /* reset whole DMA things */
609                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
610         }
611         return 0;
612 }
613
614 static int snd_intel8x0_hw_params(snd_pcm_substream_t * substream,
615                                   snd_pcm_hw_params_t * hw_params)
616 {
617         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
618 }
619
620 static int snd_intel8x0_hw_free(snd_pcm_substream_t * substream)
621 {
622         return snd_pcm_lib_free_pages(substream);
623 }
624
625 static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(snd_pcm_substream_t * substream)
626 {
627         intel8x0_t *chip = snd_pcm_substream_chip(substream);
628         ichdev_t *ichdev = get_ichdev(substream);
629         size_t ptr1, ptr;
630
631         ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << chip->pcm_pos_shift;
632         if (ptr1 != 0)
633                 ptr = ichdev->fragsize1 - ptr1;
634         else
635                 ptr = 0;
636         ptr += ichdev->position;
637         if (ptr >= ichdev->size)
638                 return 0;
639         return bytes_to_frames(substream->runtime, ptr);
640 }
641
642 static int snd_intel8x0m_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
643 {
644         ichdev_t *ichdev = get_ichdev(substream);
645         /* hook off/on on start/stop */
646         /* TODO: move it to ac97 controls */
647         switch (cmd) {
648         case SNDRV_PCM_TRIGGER_START:
649                 snd_ac97_update_bits(ichdev->ac97, AC97_GPIO_STATUS,
650                                      AC97_GPIO_LINE1_OH, AC97_GPIO_LINE1_OH);
651                 break;
652         case SNDRV_PCM_TRIGGER_STOP:
653                 snd_ac97_update_bits(ichdev->ac97, AC97_GPIO_STATUS,
654                                      AC97_GPIO_LINE1_OH, ~AC97_GPIO_LINE1_OH);
655                 break;
656         default:
657                 return -EINVAL;
658         }
659         return snd_intel8x0_pcm_trigger(substream,cmd);
660 }
661
662 static int snd_intel8x0m_pcm_prepare(snd_pcm_substream_t * substream)
663 {
664         intel8x0_t *chip = snd_pcm_substream_chip(substream);
665         snd_pcm_runtime_t *runtime = substream->runtime;
666         ichdev_t *ichdev = get_ichdev(substream);
667
668         ichdev->physbuf = runtime->dma_addr;
669         ichdev->size = snd_pcm_lib_buffer_bytes(substream);
670         ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
671         snd_ac97_write(ichdev->ac97, AC97_LINE1_RATE, runtime->rate);
672         snd_ac97_write(ichdev->ac97, AC97_LINE1_LEVEL, 0);
673         snd_intel8x0_setup_periods(chip, ichdev);
674         return 0;
675 }
676
677 static snd_pcm_hardware_t snd_intel8x0m_stream =
678 {
679         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
680                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
681                                  SNDRV_PCM_INFO_MMAP_VALID |
682                                  SNDRV_PCM_INFO_PAUSE |
683                                  SNDRV_PCM_INFO_RESUME),
684         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
685         .rates =                SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
686         .rate_min =             8000,
687         .rate_max =             16000,
688         .channels_min =         1,
689         .channels_max =         1,
690         .buffer_bytes_max =     32 * 1024,
691         .period_bytes_min =     32,
692         .period_bytes_max =     32 * 1024,
693         .periods_min =          1,
694         .periods_max =          1024,
695         .fifo_size =            0,
696 };
697
698
699 static int snd_intel8x0m_pcm_open(snd_pcm_substream_t * substream, ichdev_t *ichdev)
700 {
701         static unsigned int rates[] = { 8000,  9600, 12000, 16000 };
702         static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
703                 .count = ARRAY_SIZE(rates),
704                 .list = rates,
705                 .mask = 0,
706         };
707         snd_pcm_runtime_t *runtime = substream->runtime;
708         int err;
709
710         ichdev->substream = substream;
711         runtime->hw = snd_intel8x0m_stream;
712         err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
713         if ( err < 0 )
714                 return err;
715         runtime->private_data = ichdev;
716         return 0;
717 }
718
719 static int snd_intel8x0m_playback_open(snd_pcm_substream_t * substream)
720 {
721         intel8x0_t *chip = snd_pcm_substream_chip(substream);
722
723         return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMOUT]);
724 }
725
726 static int snd_intel8x0m_playback_close(snd_pcm_substream_t * substream)
727 {
728         intel8x0_t *chip = snd_pcm_substream_chip(substream);
729
730         chip->ichd[ICHD_MDMOUT].substream = NULL;
731         return 0;
732 }
733
734 static int snd_intel8x0m_capture_open(snd_pcm_substream_t * substream)
735 {
736         intel8x0_t *chip = snd_pcm_substream_chip(substream);
737
738         return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMIN]);
739 }
740
741 static int snd_intel8x0m_capture_close(snd_pcm_substream_t * substream)
742 {
743         intel8x0_t *chip = snd_pcm_substream_chip(substream);
744
745         chip->ichd[ICHD_MDMIN].substream = NULL;
746         return 0;
747 }
748
749
750 static snd_pcm_ops_t snd_intel8x0m_playback_ops = {
751         .open =         snd_intel8x0m_playback_open,
752         .close =        snd_intel8x0m_playback_close,
753         .ioctl =        snd_pcm_lib_ioctl,
754         .hw_params =    snd_intel8x0_hw_params,
755         .hw_free =      snd_intel8x0_hw_free,
756         .prepare =      snd_intel8x0m_pcm_prepare,
757         .trigger =      snd_intel8x0m_pcm_trigger,
758         .pointer =      snd_intel8x0_pcm_pointer,
759 };
760
761 static snd_pcm_ops_t snd_intel8x0m_capture_ops = {
762         .open =         snd_intel8x0m_capture_open,
763         .close =        snd_intel8x0m_capture_close,
764         .ioctl =        snd_pcm_lib_ioctl,
765         .hw_params =    snd_intel8x0_hw_params,
766         .hw_free =      snd_intel8x0_hw_free,
767         .prepare =      snd_intel8x0m_pcm_prepare,
768         .trigger =      snd_intel8x0m_pcm_trigger,
769         .pointer =      snd_intel8x0_pcm_pointer,
770 };
771
772
773 struct ich_pcm_table {
774         char *suffix;
775         snd_pcm_ops_t *playback_ops;
776         snd_pcm_ops_t *capture_ops;
777         size_t prealloc_size;
778         size_t prealloc_max_size;
779         int ac97_idx;
780 };
781
782 static int __devinit snd_intel8x0_pcm1(intel8x0_t *chip, int device, struct ich_pcm_table *rec)
783 {
784         snd_pcm_t *pcm;
785         int err;
786         char name[32];
787
788         if (rec->suffix)
789                 sprintf(name, "Intel ICH - %s", rec->suffix);
790         else
791                 strcpy(name, "Intel ICH");
792         err = snd_pcm_new(chip->card, name, device,
793                           rec->playback_ops ? 1 : 0,
794                           rec->capture_ops ? 1 : 0, &pcm);
795         if (err < 0)
796                 return err;
797
798         if (rec->playback_ops)
799                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
800         if (rec->capture_ops)
801                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
802
803         pcm->private_data = chip;
804         pcm->info_flags = 0;
805         if (rec->suffix)
806                 sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
807         else
808                 strcpy(pcm->name, chip->card->shortname);
809         chip->pcm[device] = pcm;
810
811         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
812                                               snd_dma_pci_data(chip->pci),
813                                               rec->prealloc_size,
814                                               rec->prealloc_max_size);
815
816         return 0;
817 }
818
819 static struct ich_pcm_table intel_pcms[] __devinitdata = {
820         {
821                 .suffix = "Modem",
822                 .playback_ops = &snd_intel8x0m_playback_ops,
823                 .capture_ops = &snd_intel8x0m_capture_ops,
824                 .prealloc_size = 4 * 1024,
825                 .prealloc_max_size = 16 * 1024,
826         },
827 };
828
829 static int __devinit snd_intel8x0_pcm(intel8x0_t *chip)
830 {
831         int i, tblsize, device, err;
832         struct ich_pcm_table *tbl, *rec;
833
834 #if 1
835         tbl = intel_pcms;
836         tblsize = 1;
837 #else
838         switch (chip->device_type) {
839         case DEVICE_NFORCE:
840                 tbl = nforce_pcms;
841                 tblsize = ARRAY_SIZE(nforce_pcms);
842                 break;
843         case DEVICE_ALI:
844                 tbl = ali_pcms;
845                 tblsize = ARRAY_SIZE(ali_pcms);
846                 break;
847         default:
848                 tbl = intel_pcms;
849                 tblsize = 2;
850                 break;
851         }
852 #endif
853         device = 0;
854         for (i = 0; i < tblsize; i++) {
855                 rec = tbl + i;
856                 if (i > 0 && rec->ac97_idx) {
857                         /* activate PCM only when associated AC'97 codec */
858                         if (! chip->ichd[rec->ac97_idx].ac97)
859                                 continue;
860                 }
861                 err = snd_intel8x0_pcm1(chip, device, rec);
862                 if (err < 0)
863                         return err;
864                 device++;
865         }
866
867         chip->pcm_devs = device;
868         return 0;
869 }
870         
871
872 /*
873  *  Mixer part
874  */
875
876 static void snd_intel8x0_mixer_free_ac97_bus(ac97_bus_t *bus)
877 {
878         intel8x0_t *chip = snd_magic_cast(intel8x0_t, bus->private_data, return);
879         chip->ac97_bus = NULL;
880 }
881
882 static void snd_intel8x0_mixer_free_ac97(ac97_t *ac97)
883 {
884         intel8x0_t *chip = snd_magic_cast(intel8x0_t, ac97->private_data, return);
885         chip->ac97 = NULL;
886 }
887
888
889 static int __devinit snd_intel8x0_mixer(intel8x0_t *chip, int ac97_clock)
890 {
891         ac97_bus_t bus, *pbus;
892         ac97_t ac97, *x97;
893         int err;
894         unsigned int glob_sta = 0;
895
896         chip->in_ac97_init = 1;
897         memset(&bus, 0, sizeof(bus));
898         bus.private_data = chip;
899         bus.private_free = snd_intel8x0_mixer_free_ac97_bus;
900         if (ac97_clock >= 8000 && ac97_clock <= 48000)
901                 bus.clock = ac97_clock;
902         else
903                 bus.clock = 48000;
904         
905         memset(&ac97, 0, sizeof(ac97));
906         ac97.private_data = chip;
907         ac97.private_free = snd_intel8x0_mixer_free_ac97;
908
909         glob_sta = igetdword(chip, ICHREG(GLOB_STA));
910         bus.write = snd_intel8x0_codec_write;
911         bus.read = snd_intel8x0_codec_read;
912         bus.vra = 1;
913
914         if ((err = snd_ac97_bus(chip->card, &bus, &pbus)) < 0)
915                 goto __err;
916         chip->ac97_bus = pbus;
917         ac97.pci = chip->pci;
918         ac97.num = glob_sta & ICH_SCR ? 1 : 0;
919         if ((err = snd_ac97_mixer(pbus, &ac97, &x97)) < 0) {
920                 snd_printk(KERN_ERR "Unable to initialize codec #%d\n", ac97.num);
921                 if (ac97.num == 0)
922                         goto __err;
923                 return err;
924         }
925         chip->ac97 = x97;
926         if(ac97_is_modem(x97) && !chip->ichd[ICHD_MDMIN].ac97 ) {
927                 chip->ichd[ICHD_MDMIN].ac97 = x97;
928                 chip->ichd[ICHD_MDMOUT].ac97 = x97;
929         }
930
931         chip->in_ac97_init = 0;
932         return 0;
933
934  __err:
935         /* clear the cold-reset bit for the next chance */
936         if (chip->device_type != DEVICE_ALI)
937                 iputdword(chip, ICHREG(GLOB_CNT), igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
938         return err;
939 }
940
941
942 /*
943  *
944  */
945
946 #define do_delay(chip) do {\
947         set_current_state(TASK_UNINTERRUPTIBLE);\
948         schedule_timeout(1);\
949 } while (0)
950
951 static int snd_intel8x0m_ich_chip_init(intel8x0_t *chip, int probing)
952 {
953         unsigned long end_time;
954         unsigned int cnt, status, nstatus;
955         
956         /* put logic to right state */
957         /* first clear status bits */
958         status = ICH_RCS | ICH_MIINT | ICH_MOINT;
959         cnt = igetdword(chip, ICHREG(GLOB_STA));
960         iputdword(chip, ICHREG(GLOB_STA), cnt & status);
961
962         /* ACLink on, 2 channels */
963         cnt = igetdword(chip, ICHREG(GLOB_CNT));
964         cnt &= ~(ICH_ACLINK);
965         /* finish cold or do warm reset */
966         cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
967         iputdword(chip, ICHREG(GLOB_CNT), cnt);
968         end_time = (jiffies + (HZ / 4)) + 1;
969         do {
970                 if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
971                         goto __ok;
972                 do_delay(chip);
973         } while (time_after_eq(end_time, jiffies));
974         snd_printk("AC'97 warm reset still in progress? [0x%x]\n", igetdword(chip, ICHREG(GLOB_CNT)));
975         return -EIO;
976
977       __ok:
978         if (probing) {
979                 /* wait for any codec ready status.
980                  * Once it becomes ready it should remain ready
981                  * as long as we do not disable the ac97 link.
982                  */
983                 end_time = jiffies + HZ;
984                 do {
985                         status = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
986                         if (status)
987                                 break;
988                         do_delay(chip);
989                 } while (time_after_eq(end_time, jiffies));
990                 if (! status) {
991                         /* no codec is found */
992                         snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n", igetdword(chip, ICHREG(GLOB_STA)));
993                         return -EIO;
994                 }
995
996                 /* up to two codecs (modem cannot be tertiary with ICH4) */
997                 nstatus = ICH_PCR | ICH_SCR;
998
999                 /* wait for other codecs ready status. */
1000                 end_time = jiffies + HZ / 4;
1001                 while (status != nstatus && time_after_eq(end_time, jiffies)) {
1002                         do_delay(chip);
1003                         status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
1004                 }
1005
1006         } else {
1007                 /* resume phase */
1008                 status = 0;
1009                 if (chip->ac97)
1010                         status |= get_ich_codec_bit(chip, chip->ac97->num);
1011                 /* wait until all the probed codecs are ready */
1012                 end_time = jiffies + HZ;
1013                 do {
1014                         nstatus = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
1015                         if (status == nstatus)
1016                                 break;
1017                         do_delay(chip);
1018                 } while (time_after_eq(end_time, jiffies));
1019         }
1020
1021         return 0;
1022 }
1023
1024 static int snd_intel8x0_chip_init(intel8x0_t *chip, int probing)
1025 {
1026         unsigned int i;
1027         int err;
1028         
1029         if ((err = snd_intel8x0m_ich_chip_init(chip, probing)) < 0)
1030                 return err;
1031         iagetword(chip, 0);     /* clear semaphore flag */
1032
1033         /* disable interrupts */
1034         for (i = 0; i < chip->bdbars_count; i++)
1035                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
1036         /* reset channels */
1037         for (i = 0; i < chip->bdbars_count; i++)
1038                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
1039         /* initialize Buffer Descriptor Lists */
1040         for (i = 0; i < chip->bdbars_count; i++)
1041                 iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
1042         return 0;
1043 }
1044
1045 static int snd_intel8x0_free(intel8x0_t *chip)
1046 {
1047         unsigned int i;
1048
1049         if (chip->irq < 0)
1050                 goto __hw_end;
1051         /* disable interrupts */
1052         for (i = 0; i < chip->bdbars_count; i++)
1053                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
1054         /* reset channels */
1055         for (i = 0; i < chip->bdbars_count; i++)
1056                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
1057         /* --- */
1058         synchronize_irq(chip->irq);
1059       __hw_end:
1060         if (chip->bdbars.area)
1061                 snd_dma_free_pages(&chip->dma_dev, &chip->bdbars);
1062         if (chip->remap_addr)
1063                 iounmap((void *) chip->remap_addr);
1064         if (chip->remap_bmaddr)
1065                 iounmap((void *) chip->remap_bmaddr);
1066         if (chip->res) {
1067                 release_resource(chip->res);
1068                 kfree_nocheck(chip->res);
1069         }
1070         if (chip->res_bm) {
1071                 release_resource(chip->res_bm);
1072                 kfree_nocheck(chip->res_bm);
1073         }
1074         if (chip->irq >= 0)
1075                 free_irq(chip->irq, (void *)chip);
1076         snd_magic_kfree(chip);
1077         return 0;
1078 }
1079
1080 #ifdef CONFIG_PM
1081 /*
1082  * power management
1083  */
1084 static void intel8x0_suspend(intel8x0_t *chip)
1085 {
1086         snd_card_t *card = chip->card;
1087         int i;
1088
1089         if (chip->in_suspend ||
1090             card->power_state == SNDRV_CTL_POWER_D3hot)
1091                 return;
1092
1093         chip->in_suspend = 1;
1094         for (i = 0; i < chip->pcm_devs; i++)
1095                 snd_pcm_suspend_all(chip->pcm[i]);
1096         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1097 }
1098
1099 static void intel8x0_resume(intel8x0_t *chip)
1100 {
1101         snd_card_t *card = chip->card;
1102
1103         if (! chip->in_suspend ||
1104             card->power_state == SNDRV_CTL_POWER_D0)
1105                 return;
1106
1107         pci_enable_device(chip->pci);
1108         pci_set_master(chip->pci);
1109         snd_intel8x0_chip_init(chip, 0);
1110         if (chip->ac97)
1111                 snd_ac97_resume(chip->ac97);
1112
1113         chip->in_suspend = 0;
1114         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1115 }
1116
1117 static int snd_intel8x0m_suspend(struct pci_dev *dev, u32 state)
1118 {
1119         intel8x0_t *chip = snd_magic_cast(intel8x0_t, pci_get_drvdata(dev), return -ENXIO);
1120         intel8x0_suspend(chip);
1121         return 0;
1122 }
1123 static int snd_intel8x0m_resume(struct pci_dev *dev)
1124 {
1125         intel8x0_t *chip = snd_magic_cast(intel8x0_t, pci_get_drvdata(dev), return -ENXIO);
1126         intel8x0_resume(chip);
1127         return 0;
1128 }
1129
1130 /* callback */
1131 static int snd_intel8x0_set_power_state(snd_card_t *card, unsigned int power_state)
1132 {
1133         intel8x0_t *chip = snd_magic_cast(intel8x0_t, card->power_state_private_data, return -ENXIO);
1134         switch (power_state) {
1135         case SNDRV_CTL_POWER_D0:
1136         case SNDRV_CTL_POWER_D1:
1137         case SNDRV_CTL_POWER_D2:
1138                 intel8x0_resume(chip);
1139                 break;
1140         case SNDRV_CTL_POWER_D3hot:
1141         case SNDRV_CTL_POWER_D3cold:
1142                 intel8x0_suspend(chip);
1143                 break;
1144         default:
1145                 return -EINVAL;
1146         }
1147         return 0;
1148 }
1149
1150 #endif /* CONFIG_PM */
1151
1152 static void snd_intel8x0m_proc_read(snd_info_entry_t * entry,
1153                                    snd_info_buffer_t * buffer)
1154 {
1155         intel8x0_t *chip = snd_magic_cast(intel8x0_t, entry->private_data, return);
1156         unsigned int tmp;
1157
1158         snd_iprintf(buffer, "Intel8x0m\n\n");
1159         if (chip->device_type == DEVICE_ALI)
1160                 return;
1161         tmp = igetdword(chip, ICHREG(GLOB_STA));
1162         snd_iprintf(buffer, "Global control        : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
1163         snd_iprintf(buffer, "Global status         : 0x%08x\n", tmp);
1164         snd_iprintf(buffer, "AC'97 codecs ready    :%s%s%s%s\n",
1165                         tmp & ICH_PCR ? " primary" : "",
1166                         tmp & ICH_SCR ? " secondary" : "",
1167                         tmp & ICH_TCR ? " tertiary" : "",
1168                         (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
1169 }
1170
1171 static void __devinit snd_intel8x0m_proc_init(intel8x0_t * chip)
1172 {
1173         snd_info_entry_t *entry;
1174
1175         if (! snd_card_proc_new(chip->card, "intel8x0m", &entry))
1176                 snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0m_proc_read);
1177 }
1178
1179 static int snd_intel8x0_dev_free(snd_device_t *device)
1180 {
1181         intel8x0_t *chip = snd_magic_cast(intel8x0_t, device->device_data, return -ENXIO);
1182         return snd_intel8x0_free(chip);
1183 }
1184
1185 struct ich_reg_info {
1186         unsigned int int_sta_mask;
1187         unsigned int offset;
1188 };
1189
1190 static int __devinit snd_intel8x0m_create(snd_card_t * card,
1191                                          struct pci_dev *pci,
1192                                          unsigned long device_type,
1193                                          intel8x0_t ** r_intel8x0)
1194 {
1195         intel8x0_t *chip;
1196         int err;
1197         unsigned int i;
1198         unsigned int int_sta_masks;
1199         ichdev_t *ichdev;
1200         static snd_device_ops_t ops = {
1201                 .dev_free =     snd_intel8x0_dev_free,
1202         };
1203         static struct ich_reg_info intel_regs[2] = {
1204                 { ICH_MIINT, 0 },
1205                 { ICH_MOINT, 0x10 },
1206         };
1207         struct ich_reg_info *tbl;
1208
1209         *r_intel8x0 = NULL;
1210
1211         if ((err = pci_enable_device(pci)) < 0)
1212                 return err;
1213
1214         chip = snd_magic_kcalloc(intel8x0_t, 0, GFP_KERNEL);
1215         if (chip == NULL)
1216                 return -ENOMEM;
1217         spin_lock_init(&chip->reg_lock);
1218         spin_lock_init(&chip->ac97_lock);
1219         chip->device_type = device_type;
1220         chip->card = card;
1221         chip->pci = pci;
1222         chip->irq = -1;
1223         snd_intel8x0m_proc_init(chip);
1224         sprintf(chip->ac97_name, "%s - AC'97", card->shortname);
1225         sprintf(chip->ctrl_name, "%s - Controller", card->shortname);
1226         if (device_type == DEVICE_ALI) {
1227                 /* ALI5455 has no ac97 region */
1228                 chip->bmaddr = pci_resource_start(pci, 0);
1229                 if ((chip->res_bm = request_region(chip->bmaddr, 256, chip->ctrl_name)) == NULL) {
1230                         snd_printk("unable to grab ports 0x%lx-0x%lx\n", chip->bmaddr, chip->bmaddr + 256 - 1);
1231                         snd_intel8x0_free(chip);
1232                         return -EBUSY;
1233                 }
1234                 goto port_inited;
1235         }
1236
1237         if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) {      /* ICH4 and Nforce */
1238                 chip->mmio = 1;
1239                 chip->addr = pci_resource_start(pci, 2);
1240                 if ((chip->res = request_mem_region(chip->addr, 512, chip->ac97_name)) == NULL) {
1241                         snd_printk("unable to grab I/O memory 0x%lx-0x%lx\n", chip->addr, chip->addr + 512 - 1);
1242                         snd_intel8x0_free(chip);
1243                         return -EBUSY;
1244                 }
1245                 chip->remap_addr = (unsigned long) ioremap_nocache(chip->addr, 512);
1246                 if (chip->remap_addr == 0) {
1247                         snd_printk("AC'97 space ioremap problem\n");
1248                         snd_intel8x0_free(chip);
1249                         return -EIO;
1250                 }
1251         } else {
1252                 chip->addr = pci_resource_start(pci, 0);
1253                 if ((chip->res = request_region(chip->addr, 256, chip->ac97_name)) == NULL) {
1254                         snd_printk("unable to grab ports 0x%lx-0x%lx\n", chip->addr, chip->addr + 256 - 1);
1255                         snd_intel8x0_free(chip);
1256                         return -EBUSY;
1257                 }
1258         }
1259         if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) {      /* ICH4 */
1260                 chip->bm_mmio = 1;
1261                 chip->bmaddr = pci_resource_start(pci, 3);
1262                 if ((chip->res_bm = request_mem_region(chip->bmaddr, 256, chip->ctrl_name)) == NULL) {
1263                         snd_printk("unable to grab I/O memory 0x%lx-0x%lx\n", chip->bmaddr, chip->bmaddr + 512 - 1);
1264                         snd_intel8x0_free(chip);
1265                         return -EBUSY;
1266                 }
1267                 chip->remap_bmaddr = (unsigned long) ioremap_nocache(chip->bmaddr, 256);
1268                 if (chip->remap_bmaddr == 0) {
1269                         snd_printk("Controller space ioremap problem\n");
1270                         snd_intel8x0_free(chip);
1271                         return -EIO;
1272                 }
1273         } else {
1274                 chip->bmaddr = pci_resource_start(pci, 1);
1275                 if ((chip->res_bm = request_region(chip->bmaddr, 128, chip->ctrl_name)) == NULL) {
1276                         snd_printk("unable to grab ports 0x%lx-0x%lx\n", chip->bmaddr, chip->bmaddr + 128 - 1);
1277                         snd_intel8x0_free(chip);
1278                         return -EBUSY;
1279                 }
1280         }
1281
1282  port_inited:
1283         if (request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
1284                 snd_printk("unable to grab IRQ %d\n", pci->irq);
1285                 snd_intel8x0_free(chip);
1286                 return -EBUSY;
1287         }
1288         chip->irq = pci->irq;
1289         pci_set_master(pci);
1290         synchronize_irq(chip->irq);
1291
1292         /* initialize offsets */
1293         chip->bdbars_count = 2;
1294         tbl = intel_regs;
1295
1296         for (i = 0; i < chip->bdbars_count; i++) {
1297                 ichdev = &chip->ichd[i];
1298                 ichdev->ichd = i;
1299                 ichdev->reg_offset = tbl[i].offset;
1300                 ichdev->int_sta_mask = tbl[i].int_sta_mask;
1301                 if (device_type == DEVICE_SIS) {
1302                         /* SiS 7013 swaps the registers */
1303                         ichdev->roff_sr = ICH_REG_OFF_PICB;
1304                         ichdev->roff_picb = ICH_REG_OFF_SR;
1305                 } else {
1306                         ichdev->roff_sr = ICH_REG_OFF_SR;
1307                         ichdev->roff_picb = ICH_REG_OFF_PICB;
1308                 }
1309                 if (device_type == DEVICE_ALI)
1310                         ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
1311         }
1312         /* SIS7013 handles the pcm data in bytes, others are in words */
1313         chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
1314
1315         /* allocate buffer descriptor lists */
1316         /* the start of each lists must be aligned to 8 bytes */
1317         memset(&chip->dma_dev, 0, sizeof(chip->dma_dev));
1318         chip->dma_dev.type = SNDRV_DMA_TYPE_DEV;
1319         chip->dma_dev.dev = snd_dma_pci_data(pci);
1320         if (snd_dma_alloc_pages(&chip->dma_dev, chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2, &chip->bdbars) < 0) {
1321                 snd_intel8x0_free(chip);
1322                 return -ENOMEM;
1323         }
1324         /* tables must be aligned to 8 bytes here, but the kernel pages
1325            are much bigger, so we don't care (on i386) */
1326         int_sta_masks = 0;
1327         for (i = 0; i < chip->bdbars_count; i++) {
1328                 ichdev = &chip->ichd[i];
1329                 ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
1330                 ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
1331                 int_sta_masks |= ichdev->int_sta_mask;
1332         }
1333         chip->int_sta_reg = ICH_REG_GLOB_STA;
1334         chip->int_sta_mask = int_sta_masks;
1335
1336         if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
1337                 snd_intel8x0_free(chip);
1338                 return err;
1339         }
1340
1341 #ifdef CONFIG_PM
1342         card->set_power_state = snd_intel8x0_set_power_state;
1343         card->power_state_private_data = chip;
1344 #endif
1345
1346         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1347                 snd_intel8x0_free(chip);
1348                 return err;
1349         }
1350
1351         snd_card_set_dev(card, &pci->dev);
1352
1353         *r_intel8x0 = chip;
1354         return 0;
1355 }
1356
1357 static struct shortname_table {
1358         unsigned int id;
1359         const char *s;
1360 } shortnames[] __devinitdata = {
1361         { PCI_DEVICE_ID_INTEL_82801_6, "Intel 82801AA-ICH" },
1362         { PCI_DEVICE_ID_INTEL_82901_6, "Intel 82901AB-ICH0" },
1363         { PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
1364         { PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
1365         { PCI_DEVICE_ID_INTEL_ICH3_6, "Intel 82801CA-ICH3" },
1366         { PCI_DEVICE_ID_INTEL_ICH4_6, "Intel 82801DB-ICH4" },
1367         { PCI_DEVICE_ID_INTEL_ICH5_6, "Intel ICH5" },
1368         { 0x7446, "AMD AMD768" },
1369 #if 0
1370         { PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
1371         { PCI_DEVICE_ID_NVIDIA_MCP_AUDIO, "NVidia nForce" },
1372         { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
1373         { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
1374         { 0x5455, "ALi M5455" },
1375         { 0x746d, "AMD AMD8111" },
1376 #endif
1377         { 0, 0 },
1378 };
1379
1380 static int __devinit snd_intel8x0m_probe(struct pci_dev *pci,
1381                                         const struct pci_device_id *pci_id)
1382 {
1383         static int dev;
1384         snd_card_t *card;
1385         intel8x0_t *chip;
1386         int err;
1387         struct shortname_table *name;
1388
1389         if (dev >= SNDRV_CARDS)
1390                 return -ENODEV;
1391         if (!enable[dev]) {
1392                 dev++;
1393                 return -ENOENT;
1394         }
1395
1396         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1397         if (card == NULL)
1398                 return -ENOMEM;
1399
1400         switch (pci_id->driver_data) {
1401         case DEVICE_NFORCE:
1402                 strcpy(card->driver, "NFORCE");
1403                 break;
1404         default:
1405                 strcpy(card->driver, "ICH");
1406                 break;
1407         }
1408
1409         strcpy(card->shortname, "Intel ICH");
1410         for (name = shortnames; name->id; name++) {
1411                 if (pci->device == name->id) {
1412                         strcpy(card->shortname, name->s);
1413                         break;
1414                 }
1415         }
1416         strcat(card->shortname," Modem");
1417
1418         if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
1419                 snd_card_free(card);
1420                 return err;
1421         }
1422
1423         if ((err = snd_intel8x0_mixer(chip, ac97_clock[dev])) < 0) {
1424                 snd_card_free(card);
1425                 return err;
1426         }
1427         if ((err = snd_intel8x0_pcm(chip)) < 0) {
1428                 snd_card_free(card);
1429                 return err;
1430         }
1431         
1432         sprintf(card->longname, "%s at 0x%lx, irq %i",
1433                 card->shortname, chip->addr, chip->irq);
1434
1435         if ((err = snd_card_register(card)) < 0) {
1436                 snd_card_free(card);
1437                 return err;
1438         }
1439         pci_set_drvdata(pci, chip);
1440         dev++;
1441         return 0;
1442 }
1443
1444 static void __devexit snd_intel8x0m_remove(struct pci_dev *pci)
1445 {
1446         intel8x0_t *chip = snd_magic_cast(intel8x0_t, pci_get_drvdata(pci), return);
1447         if (chip)
1448                 snd_card_free(chip->card);
1449         pci_set_drvdata(pci, NULL);
1450 }
1451
1452 static struct pci_driver driver = {
1453         .name = "Intel ICH Modem",
1454         .id_table = snd_intel8x0m_ids,
1455         .probe = snd_intel8x0m_probe,
1456         .remove = __devexit_p(snd_intel8x0m_remove),
1457 #ifdef CONFIG_PM
1458         .suspend = snd_intel8x0m_suspend,
1459         .resume = snd_intel8x0m_resume,
1460 #endif
1461 };
1462
1463
1464 static int __init alsa_card_intel8x0m_init(void)
1465 {
1466         int err;
1467
1468         if ((err = pci_module_init(&driver)) < 0) {
1469 #ifdef MODULE
1470                 printk(KERN_ERR "Intel ICH modemcard not found or device busy\n");
1471 #endif
1472                 return err;
1473         }
1474
1475         return 0;
1476 }
1477
1478 static void __exit alsa_card_intel8x0m_exit(void)
1479 {
1480         pci_unregister_driver(&driver);
1481 }
1482
1483 module_init(alsa_card_intel8x0m_init)
1484 module_exit(alsa_card_intel8x0m_exit)
1485
1486 #ifndef MODULE
1487
1488 /* format is: snd-intel8x0=enable,index,id,ac97_clock,mpu_port,joystick */
1489
1490 static int __init alsa_card_intel8x0m_setup(char *str)
1491 {
1492         static unsigned __initdata nr_dev = 0;
1493
1494         if (nr_dev >= SNDRV_CARDS)
1495                 return 0;
1496         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
1497                get_option(&str,&index[nr_dev]) == 2 &&
1498                get_id(&str,&id[nr_dev]) == 2 &&
1499                get_option(&str,&ac97_clock[nr_dev]) == 2
1500                );
1501         nr_dev++;
1502         return 1;
1503 }
1504
1505 __setup("snd-intel8x0m=", alsa_card_intel8x0m_setup);
1506
1507 #endif /* ifndef MODULE */