Blame view

sound/pci/intel8x0m.c 37.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   *   ALSA modem driver for Intel ICH (i8x0) chipsets
   *
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
4
   *	Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
   *
f01cc521a   Sasha Khapyorsky   [ALSA] Sasha Khap...
6
   *   This is modified (by Sasha Khapyorsky <sashak@alsa-project.org>) version
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   *   of ALSA ICH sound driver intel8x0.c .
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */      
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
29
30
  #include <asm/io.h>
  #include <linux/delay.h>
  #include <linux/interrupt.h>
  #include <linux/init.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
65a772172   Paul Gortmaker   sound: fix driver...
31
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
  #include <sound/core.h>
  #include <sound/pcm.h>
  #include <sound/ac97_codec.h>
  #include <sound/info.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  #include <sound/initval.h>
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
37
  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
38
39
  MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; "
  		   "SiS 7013; NVidia MCP/2/2S/3 modems");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{Intel,82801AA-ICH},"
  		"{Intel,82901AB-ICH0},"
  		"{Intel,82801BA-ICH2},"
  		"{Intel,82801CA-ICH3},"
  		"{Intel,82801DB-ICH4},"
  		"{Intel,ICH5},"
  		"{Intel,ICH6},"
  		"{Intel,ICH7},"
  	        "{Intel,MX440},"
  		"{SiS,7013},"
  		"{NVidia,NForce Modem},"
  		"{NVidia,NForce2 Modem},"
  		"{NVidia,NForce2s Modem},"
  		"{NVidia,NForce3 Modem},"
  		"{AMD,AMD768}}");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
56
57
  static int index = -2; /* Exclude the first card */
  static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
6581f4e74   Takashi Iwai   [ALSA] Remove zer...
58
  static int ac97_clock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

b7fe46220   Clemens Ladisch   [ALSA] highlander...
60
  module_param(index, int, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
  MODULE_PARM_DESC(index, "Index value for Intel i8x0 modemcard.");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
62
  module_param(id, charp, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  MODULE_PARM_DESC(id, "ID string for Intel i8x0 modemcard.");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
64
  module_param(ac97_clock, int, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
  MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
2b3e584b9   Takashi Iwai   [ALSA] Add dummy ...
66
  /* just for backward compatibility */
a67ff6a54   Rusty Russell   ALSA: module_para...
67
  static bool enable;
698444f31   Takashi Iwai   [ALSA] Fix the ty...
68
  module_param(enable, bool, 0444);
2b3e584b9   Takashi Iwai   [ALSA] Add dummy ...
69

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
  /*
   *  Direct registers
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  enum { DEVICE_INTEL, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
  
  #define ICHREG(x) ICH_REG_##x
  
  #define DEFINE_REGSET(name,base) \
  enum { \
  	ICH_REG_##name##_BDBAR	= base + 0x0,	/* dword - buffer descriptor list base address */ \
  	ICH_REG_##name##_CIV	= base + 0x04,	/* byte - current index value */ \
  	ICH_REG_##name##_LVI	= base + 0x05,	/* byte - last valid index */ \
  	ICH_REG_##name##_SR	= base + 0x06,	/* byte - status register */ \
  	ICH_REG_##name##_PICB	= base + 0x08,	/* word - position in current buffer */ \
  	ICH_REG_##name##_PIV	= base + 0x0a,	/* byte - prefetched index value */ \
  	ICH_REG_##name##_CR	= base + 0x0b,	/* byte - control register */ \
  };
  
  /* busmaster blocks */
  DEFINE_REGSET(OFF, 0);		/* offset */
  
  /* values for each busmaster block */
  
  /* LVI */
  #define ICH_REG_LVI_MASK		0x1f
  
  /* SR */
  #define ICH_FIFOE			0x10	/* FIFO error */
  #define ICH_BCIS			0x08	/* buffer completion interrupt status */
  #define ICH_LVBCI			0x04	/* last valid buffer completion interrupt */
  #define ICH_CELV			0x02	/* current equals last valid */
  #define ICH_DCH				0x01	/* DMA controller halted */
  
  /* PIV */
  #define ICH_REG_PIV_MASK		0x1f	/* mask */
  
  /* CR */
  #define ICH_IOCE			0x10	/* interrupt on completion enable */
  #define ICH_FEIE			0x08	/* fifo error interrupt enable */
  #define ICH_LVBIE			0x04	/* last valid buffer interrupt enable */
  #define ICH_RESETREGS			0x02	/* reset busmaster registers */
  #define ICH_STARTBM			0x01	/* start busmaster operation */
  
  
  /* global block */
  #define ICH_REG_GLOB_CNT		0x3c	/* dword - global control */
  #define   ICH_TRIE		0x00000040	/* tertiary resume interrupt enable */
  #define   ICH_SRIE		0x00000020	/* secondary resume interrupt enable */
  #define   ICH_PRIE		0x00000010	/* primary resume interrupt enable */
  #define   ICH_ACLINK		0x00000008	/* AClink shut off */
  #define   ICH_AC97WARM		0x00000004	/* AC'97 warm reset */
  #define   ICH_AC97COLD		0x00000002	/* AC'97 cold reset */
  #define   ICH_GIE		0x00000001	/* GPI interrupt enable */
  #define ICH_REG_GLOB_STA		0x40	/* dword - global status */
  #define   ICH_TRI		0x20000000	/* ICH4: tertiary (AC_SDIN2) resume interrupt */
  #define   ICH_TCR		0x10000000	/* ICH4: tertiary (AC_SDIN2) codec ready */
  #define   ICH_BCS		0x08000000	/* ICH4: bit clock stopped */
  #define   ICH_SPINT		0x04000000	/* ICH4: S/PDIF interrupt */
  #define   ICH_P2INT		0x02000000	/* ICH4: PCM2-In interrupt */
  #define   ICH_M2INT		0x01000000	/* ICH4: Mic2-In interrupt */
  #define   ICH_SAMPLE_CAP	0x00c00000	/* ICH4: sample capability bits (RO) */
  #define   ICH_MULTICHAN_CAP	0x00300000	/* ICH4: multi-channel capability bits (RO) */
  #define   ICH_MD3		0x00020000	/* modem power down semaphore */
  #define   ICH_AD3		0x00010000	/* audio power down semaphore */
  #define   ICH_RCS		0x00008000	/* read completion status */
  #define   ICH_BIT3		0x00004000	/* bit 3 slot 12 */
  #define   ICH_BIT2		0x00002000	/* bit 2 slot 12 */
  #define   ICH_BIT1		0x00001000	/* bit 1 slot 12 */
  #define   ICH_SRI		0x00000800	/* secondary (AC_SDIN1) resume interrupt */
  #define   ICH_PRI		0x00000400	/* primary (AC_SDIN0) resume interrupt */
  #define   ICH_SCR		0x00000200	/* secondary (AC_SDIN1) codec ready */
  #define   ICH_PCR		0x00000100	/* primary (AC_SDIN0) codec ready */
  #define   ICH_MCINT		0x00000080	/* MIC capture interrupt */
  #define   ICH_POINT		0x00000040	/* playback interrupt */
  #define   ICH_PIINT		0x00000020	/* capture interrupt */
  #define   ICH_NVSPINT		0x00000010	/* nforce spdif interrupt */
  #define   ICH_MOINT		0x00000004	/* modem playback interrupt */
  #define   ICH_MIINT		0x00000002	/* modem capture interrupt */
  #define   ICH_GSCI		0x00000001	/* GPI status change interrupt */
  #define ICH_REG_ACC_SEMA		0x44	/* byte - codec write semaphore */
  #define   ICH_CAS		0x01		/* codec access semaphore */
  
  #define ICH_MAX_FRAGS		32		/* max hw frags */
  
  
  /*
   *  
   */
  
  enum { ICHD_MDMIN, ICHD_MDMOUT, ICHD_MDMLAST = ICHD_MDMOUT };
  enum { ALID_MDMIN, ALID_MDMOUT, ALID_MDMLAST = ALID_MDMOUT };
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
161
  #define get_ichdev(substream) (substream->runtime->private_data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162

6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
163
  struct ichdev {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
167
  	unsigned int ichd;			/* ich device number */
  	unsigned long reg_offset;		/* offset to bmaddr */
  	u32 *bdbar;				/* CPU address (32bit) */
  	unsigned int bdbar_addr;		/* PCI bus address (32bit) */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
168
  	struct snd_pcm_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  	unsigned int physbuf;			/* physical address (32bit) */
          unsigned int size;
          unsigned int fragsize;
          unsigned int fragsize1;
          unsigned int position;
          int frags;
          int lvi;
          int lvi_frag;
  	int civ;
  	int ack;
  	int ack_reload;
  	unsigned int ack_bit;
  	unsigned int roff_sr;
  	unsigned int roff_picb;
  	unsigned int int_sta_mask;		/* interrupt status mask */
  	unsigned int ali_slot;			/* ALI DMA slot */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
185
186
  	struct snd_ac97 *ac97;
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187

6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
188
  struct intel8x0m {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
  	unsigned int device_type;
  
  	int irq;
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
192
193
  	void __iomem *addr;
  	void __iomem *bmaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
  
  	struct pci_dev *pci;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
196
  	struct snd_card *card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
198
  
  	int pcm_devs;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
199
200
  	struct snd_pcm *pcm[2];
  	struct ichdev ichd[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
  
  	unsigned int in_ac97_init: 1;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
203
204
  	struct snd_ac97_bus *ac97_bus;
  	struct snd_ac97 *ac97;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
208
209
210
211
212
213
  
  	spinlock_t reg_lock;
  	
  	struct snd_dma_buffer bdbars;
  	u32 bdbars_count;
  	u32 int_sta_reg;		/* interrupt status register */
  	u32 int_sta_mask;		/* interrupt status mask */
  	unsigned int pcm_pos_shift;
  };
cebe41d4b   Alexey Dobriyan   sound: use DEFINE...
214
  static DEFINE_PCI_DEVICE_TABLE(snd_intel8x0m_ids) = {
28d27aae9   Joe Perches   sound: Use PCI_VD...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  	{ PCI_VDEVICE(INTEL, 0x2416), DEVICE_INTEL },	/* 82801AA */
  	{ PCI_VDEVICE(INTEL, 0x2426), DEVICE_INTEL },	/* 82901AB */
  	{ PCI_VDEVICE(INTEL, 0x2446), DEVICE_INTEL },	/* 82801BA */
  	{ PCI_VDEVICE(INTEL, 0x2486), DEVICE_INTEL },	/* ICH3 */
  	{ PCI_VDEVICE(INTEL, 0x24c6), DEVICE_INTEL }, /* ICH4 */
  	{ PCI_VDEVICE(INTEL, 0x24d6), DEVICE_INTEL }, /* ICH5 */
  	{ PCI_VDEVICE(INTEL, 0x266d), DEVICE_INTEL },	/* ICH6 */
  	{ PCI_VDEVICE(INTEL, 0x27dd), DEVICE_INTEL },	/* ICH7 */
  	{ PCI_VDEVICE(INTEL, 0x7196), DEVICE_INTEL },	/* 440MX */
  	{ PCI_VDEVICE(AMD, 0x7446), DEVICE_INTEL },	/* AMD768 */
  	{ PCI_VDEVICE(SI, 0x7013), DEVICE_SIS },	/* SI7013 */
  	{ PCI_VDEVICE(NVIDIA, 0x01c1), DEVICE_NFORCE }, /* NFORCE */
  	{ PCI_VDEVICE(NVIDIA, 0x0069), DEVICE_NFORCE }, /* NFORCE2 */
  	{ PCI_VDEVICE(NVIDIA, 0x0089), DEVICE_NFORCE }, /* NFORCE2s */
  	{ PCI_VDEVICE(NVIDIA, 0x00d9), DEVICE_NFORCE }, /* NFORCE3 */
df1fe1328   Dmitry Eremin-Solenikov   ALSA: intel8x0m: ...
230
  	{ PCI_VDEVICE(AMD, 0x746e), DEVICE_INTEL },	/* AMD8111 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
  #if 0
28d27aae9   Joe Perches   sound: Use PCI_VD...
232
  	{ PCI_VDEVICE(AL, 0x5455), DEVICE_ALI },   /* Ali5455 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
  #endif
  	{ 0, }
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
  
  MODULE_DEVICE_TABLE(pci, snd_intel8x0m_ids);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
  /*
   *  Lowlevel I/O - busmaster
   */
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
241
  static inline u8 igetbyte(struct intel8x0m *chip, u32 offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
243
  	return ioread8(chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
245
  static inline u16 igetword(struct intel8x0m *chip, u32 offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
247
  	return ioread16(chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
249
  static inline u32 igetdword(struct intel8x0m *chip, u32 offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
251
  	return ioread32(chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
253
  static inline void iputbyte(struct intel8x0m *chip, u32 offset, u8 val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
255
  	iowrite8(val, chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
257
  static inline void iputword(struct intel8x0m *chip, u32 offset, u16 val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
259
  	iowrite16(val, chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
261
  static inline void iputdword(struct intel8x0m *chip, u32 offset, u32 val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
263
  	iowrite32(val, chip->bmaddr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
  }
  
  /*
   *  Lowlevel I/O - AC'97 registers
   */
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
269
  static inline u16 iagetword(struct intel8x0m *chip, u32 offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
271
  	return ioread16(chip->addr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
  }
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
273
  static inline void iaputword(struct intel8x0m *chip, u32 offset, u16 val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
  {
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
275
  	iowrite16(val, chip->addr + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
277
278
279
280
281
282
283
284
285
286
  }
  
  /*
   *  Basic I/O
   */
  
  /*
   * access to AC97 codec via normal i/o (for ICH and SIS7013)
   */
  
  /* return the GLOB_STA bit for the corresponding codec */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
287
  static unsigned int get_ich_codec_bit(struct intel8x0m *chip, unsigned int codec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
291
  {
  	static unsigned int codec_bit[3] = {
  		ICH_PCR, ICH_SCR, ICH_TCR
  	};
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
292
293
  	if (snd_BUG_ON(codec >= 3))
  		return ICH_PCR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
  	return codec_bit[codec];
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
296
  static int snd_intel8x0m_codec_semaphore(struct intel8x0m *chip, unsigned int codec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  {
  	int time;
  	
  	if (codec > 1)
  		return -EIO;
  	codec = get_ich_codec_bit(chip, codec);
  
  	/* codec ready ? */
  	if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
  		return -EIO;
  
  	/* Anyone holding a semaphore for 1 msec should be shot... */
  	time = 100;
        	do {
        		if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
        			return 0;
  		udelay(10);
  	} while (time--);
25985edce   Lucas De Marchi   Fix common misspe...
315
  	/* access to some forbidden (non existent) ac97 registers will not
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
317
  	 * reset the semaphore. So even if you don't get the semaphore, still
  	 * continue the access. We don't need the semaphore anyway. */
99b359ba1   Takashi Iwai   [ALSA] Add missin...
318
319
  	snd_printk(KERN_ERR "codec_semaphore: semaphore is not ready [0x%x][0x%x]
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
  			igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
  	iagetword(chip, 0);	/* clear semaphore flag */
  	/* I don't care about the semaphore */
  	return -EBUSY;
  }
   
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
326
327
328
  static void snd_intel8x0m_codec_write(struct snd_ac97 *ac97,
  				      unsigned short reg,
  				      unsigned short val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
330
  	struct intel8x0m *chip = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
  	
  	if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  		if (! chip->in_ac97_init)
99b359ba1   Takashi Iwai   [ALSA] Add missin...
334
335
  			snd_printk(KERN_ERR "codec_write %d: semaphore is not ready for register 0x%x
  ", ac97->num, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
  	}
  	iaputword(chip, reg + ac97->num * 0x80, val);
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
339
340
  static unsigned short snd_intel8x0m_codec_read(struct snd_ac97 *ac97,
  					       unsigned short reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
342
  	struct intel8x0m *chip = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
347
  	unsigned short res;
  	unsigned int tmp;
  
  	if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  		if (! chip->in_ac97_init)
99b359ba1   Takashi Iwai   [ALSA] Add missin...
348
349
  			snd_printk(KERN_ERR "codec_read %d: semaphore is not ready for register 0x%x
  ", ac97->num, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
351
352
353
354
  		res = 0xffff;
  	} else {
  		res = iagetword(chip, reg + ac97->num * 0x80);
  		if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
  			/* reset RCS and preserve other R/WC bits */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
355
356
  			iputdword(chip, ICHREG(GLOB_STA),
  				  tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
  			if (! chip->in_ac97_init)
99b359ba1   Takashi Iwai   [ALSA] Add missin...
358
359
  				snd_printk(KERN_ERR "codec_read %d: read timeout for register 0x%x
  ", ac97->num, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
361
362
  			res = 0xffff;
  		}
  	}
2c56c47f6   Sasha Khapyorsky   [ALSA] Fix 'semap...
363
364
  	if (reg == AC97_GPIO_STATUS)
  		iagetword(chip, 0); /* clear semaphore */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
  	return res;
  }
  
  
  /*
   * DMA I/O
   */
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
372
  static void snd_intel8x0m_setup_periods(struct intel8x0m *chip, struct ichdev *ichdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  {
  	int idx;
  	u32 *bdbar = ichdev->bdbar;
  	unsigned long port = ichdev->reg_offset;
  
  	iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
  	if (ichdev->size == ichdev->fragsize) {
  		ichdev->ack_reload = ichdev->ack = 2;
  		ichdev->fragsize1 = ichdev->fragsize >> 1;
  		for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
  			bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
  			bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  						     ichdev->fragsize1 >> chip->pcm_pos_shift);
  			bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
  			bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  						     ichdev->fragsize1 >> chip->pcm_pos_shift);
  		}
  		ichdev->frags = 2;
  	} else {
  		ichdev->ack_reload = ichdev->ack = 1;
  		ichdev->fragsize1 = ichdev->fragsize;
  		for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
  			bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
  			bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  						     ichdev->fragsize >> chip->pcm_pos_shift);
14ab08610   Takashi Iwai   ALSA: intel8x0 - ...
398
399
400
401
402
  			/*
  			printk(KERN_DEBUG "bdbar[%i] = 0x%x [0x%x]
  ",
  			       idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
  			*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
404
405
406
407
408
409
410
411
  		}
  		ichdev->frags = ichdev->size / ichdev->fragsize;
  	}
  	iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
  	ichdev->civ = 0;
  	iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
  	ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
  	ichdev->position = 0;
  #if 0
14ab08610   Takashi Iwai   ALSA: intel8x0 - ...
412
413
414
415
416
  	printk(KERN_DEBUG "lvi_frag = %i, frags = %i, period_size = 0x%x, "
  	       "period_size1 = 0x%x
  ",
  	       ichdev->lvi_frag, ichdev->frags, ichdev->fragsize,
  	       ichdev->fragsize1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
419
420
421
422
423
424
  #endif
  	/* clear interrupts */
  	iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  }
  
  /*
   *  Interrupt handler
   */
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
425
  static inline void snd_intel8x0m_update(struct intel8x0m *chip, struct ichdev *ichdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  {
  	unsigned long port = ichdev->reg_offset;
  	int civ, i, step;
  	int ack = 0;
  
  	civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
  	if (civ == ichdev->civ) {
  		// snd_printd("civ same %d
  ", civ);
  		step = 1;
  		ichdev->civ++;
  		ichdev->civ &= ICH_REG_LVI_MASK;
  	} else {
  		step = civ - ichdev->civ;
  		if (step < 0)
  			step += ICH_REG_LVI_MASK + 1;
  		// if (step != 1)
  		//	snd_printd("step = %d, %d -> %d
  ", step, ichdev->civ, civ);
  		ichdev->civ = civ;
  	}
  
  	ichdev->position += step * ichdev->fragsize1;
  	ichdev->position %= ichdev->size;
  	ichdev->lvi += step;
  	ichdev->lvi &= ICH_REG_LVI_MASK;
  	iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
  	for (i = 0; i < step; i++) {
  		ichdev->lvi_frag++;
  		ichdev->lvi_frag %= ichdev->frags;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
456
457
458
459
  		ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf +
  							     ichdev->lvi_frag *
  							     ichdev->fragsize1);
  #if 0
14ab08610   Takashi Iwai   ALSA: intel8x0 - ...
460
461
462
  		printk(KERN_DEBUG "new: bdbar[%i] = 0x%x [0x%x], "
  		       "prefetch = %i, all = 0x%x, 0x%x
  ",
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
463
464
465
466
  		       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));
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
474
475
476
477
478
  		if (--ichdev->ack == 0) {
  			ichdev->ack = ichdev->ack_reload;
  			ack = 1;
  		}
  	}
  	if (ack && ichdev->substream) {
  		spin_unlock(&chip->reg_lock);
  		snd_pcm_period_elapsed(ichdev->substream);
  		spin_lock(&chip->reg_lock);
  	}
  	iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
479
  static irqreturn_t snd_intel8x0m_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
481
482
  	struct intel8x0m *chip = dev_id;
  	struct ichdev *ichdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  	unsigned int status;
  	unsigned int i;
  
  	spin_lock(&chip->reg_lock);
  	status = igetdword(chip, chip->int_sta_reg);
  	if (status == 0xffffffff) { /* we are not yet resumed */
  		spin_unlock(&chip->reg_lock);
  		return IRQ_NONE;
  	}
  	if ((status & chip->int_sta_mask) == 0) {
  		if (status)
  			iputdword(chip, chip->int_sta_reg, status);
  		spin_unlock(&chip->reg_lock);
  		return IRQ_NONE;
  	}
  
  	for (i = 0; i < chip->bdbars_count; i++) {
  		ichdev = &chip->ichd[i];
  		if (status & ichdev->int_sta_mask)
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
502
  			snd_intel8x0m_update(chip, ichdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
504
505
506
507
508
509
510
511
512
513
514
  	}
  
  	/* ack them */
  	iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
  	spin_unlock(&chip->reg_lock);
  	
  	return IRQ_HANDLED;
  }
  
  /*
   *  PCM part
   */
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
515
  static int snd_intel8x0m_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
517
518
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  	struct ichdev *ichdev = get_ichdev(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  	unsigned char val = 0;
  	unsigned long port = ichdev->reg_offset;
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  	case SNDRV_PCM_TRIGGER_RESUME:
  		val = ICH_IOCE | ICH_STARTBM;
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  		val = 0;
  		break;
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  		val = ICH_IOCE;
  		break;
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  		val = ICH_IOCE | ICH_STARTBM;
  		break;
  	default:
  		return -EINVAL;
  	}
  	iputbyte(chip, port + ICH_REG_OFF_CR, val);
  	if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  		/* wait until DMA stopped */
  		while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
  		/* reset whole DMA things */
  		iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
  	}
  	return 0;
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
549
  static int snd_intel8x0m_hw_params(struct snd_pcm_substream *substream,
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
550
  				  struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
  {
  	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
554
  static int snd_intel8x0m_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
557
  {
  	return snd_pcm_lib_free_pages(substream);
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
558
  static snd_pcm_uframes_t snd_intel8x0m_pcm_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
560
561
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  	struct ichdev *ichdev = get_ichdev(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
564
565
566
567
568
569
570
571
572
573
  	size_t ptr1, ptr;
  
  	ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << chip->pcm_pos_shift;
  	if (ptr1 != 0)
  		ptr = ichdev->fragsize1 - ptr1;
  	else
  		ptr = 0;
  	ptr += ichdev->position;
  	if (ptr >= ichdev->size)
  		return 0;
  	return bytes_to_frames(substream->runtime, ptr);
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
574
  static int snd_intel8x0m_pcm_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
575
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
576
577
578
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct ichdev *ichdev = get_ichdev(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
581
582
583
584
  
  	ichdev->physbuf = runtime->dma_addr;
  	ichdev->size = snd_pcm_lib_buffer_bytes(substream);
  	ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
  	snd_ac97_write(ichdev->ac97, AC97_LINE1_RATE, runtime->rate);
  	snd_ac97_write(ichdev->ac97, AC97_LINE1_LEVEL, 0);
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
585
  	snd_intel8x0m_setup_periods(chip, ichdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
587
  	return 0;
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
588
  static struct snd_pcm_hardware snd_intel8x0m_stream =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID |
  				 SNDRV_PCM_INFO_PAUSE |
  				 SNDRV_PCM_INFO_RESUME),
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  	.rate_min =		8000,
  	.rate_max =		16000,
  	.channels_min =		1,
  	.channels_max =		1,
  	.buffer_bytes_max =	64 * 1024,
  	.period_bytes_min =	32,
  	.period_bytes_max =	64 * 1024,
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
608
  static int snd_intel8x0m_pcm_open(struct snd_pcm_substream *substream, struct ichdev *ichdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
610
  {
  	static unsigned int rates[] = { 8000,  9600, 12000, 16000 };
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
611
  	static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
  		.count = ARRAY_SIZE(rates),
  		.list = rates,
  		.mask = 0,
  	};
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
616
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
620
  	int err;
  
  	ichdev->substream = substream;
  	runtime->hw = snd_intel8x0m_stream;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
621
622
  	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  					 &hw_constraints_rates);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
624
625
626
627
  	if ( err < 0 )
  		return err;
  	runtime->private_data = ichdev;
  	return 0;
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
628
  static int snd_intel8x0m_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
630
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
632
633
  
  	return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMOUT]);
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
634
  static int snd_intel8x0m_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
636
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637
638
639
640
  
  	chip->ichd[ICHD_MDMOUT].substream = NULL;
  	return 0;
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
641
  static int snd_intel8x0m_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
643
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
645
646
  
  	return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMIN]);
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
647
  static int snd_intel8x0m_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
649
  	struct intel8x0m *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
651
652
653
  
  	chip->ichd[ICHD_MDMIN].substream = NULL;
  	return 0;
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
654
  static struct snd_pcm_ops snd_intel8x0m_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
656
657
  	.open =		snd_intel8x0m_playback_open,
  	.close =	snd_intel8x0m_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
658
659
  	.hw_params =	snd_intel8x0m_hw_params,
  	.hw_free =	snd_intel8x0m_hw_free,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  	.prepare =	snd_intel8x0m_pcm_prepare,
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
661
662
  	.trigger =	snd_intel8x0m_pcm_trigger,
  	.pointer =	snd_intel8x0m_pcm_pointer,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
  };
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
664
  static struct snd_pcm_ops snd_intel8x0m_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
  	.open =		snd_intel8x0m_capture_open,
  	.close =	snd_intel8x0m_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
668
669
  	.hw_params =	snd_intel8x0m_hw_params,
  	.hw_free =	snd_intel8x0m_hw_free,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
  	.prepare =	snd_intel8x0m_pcm_prepare,
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
671
672
  	.trigger =	snd_intel8x0m_pcm_trigger,
  	.pointer =	snd_intel8x0m_pcm_pointer,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
  };
  
  
  struct ich_pcm_table {
  	char *suffix;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
678
679
  	struct snd_pcm_ops *playback_ops;
  	struct snd_pcm_ops *capture_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
681
682
683
  	size_t prealloc_size;
  	size_t prealloc_max_size;
  	int ac97_idx;
  };
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
684
  static int __devinit snd_intel8x0m_pcm1(struct intel8x0m *chip, int device,
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
685
  				       struct ich_pcm_table *rec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
687
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
  	int err;
  	char name[32];
  
  	if (rec->suffix)
  		sprintf(name, "Intel ICH - %s", rec->suffix);
  	else
  		strcpy(name, "Intel ICH");
  	err = snd_pcm_new(chip->card, name, device,
  			  rec->playback_ops ? 1 : 0,
  			  rec->capture_ops ? 1 : 0, &pcm);
  	if (err < 0)
  		return err;
  
  	if (rec->playback_ops)
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
  	if (rec->capture_ops)
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
  
  	pcm->private_data = chip;
  	pcm->info_flags = 0;
6632d198c   Sasha Khapyorsky   [ALSA] dev_class=...
708
  	pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
  	if (rec->suffix)
  		sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
  	else
  		strcpy(pcm->name, chip->card->shortname);
  	chip->pcm[device] = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_pci_data(chip->pci),
  					      rec->prealloc_size,
  					      rec->prealloc_max_size);
  
  	return 0;
  }
  
  static struct ich_pcm_table intel_pcms[] __devinitdata = {
  	{
  		.suffix = "Modem",
  		.playback_ops = &snd_intel8x0m_playback_ops,
  		.capture_ops = &snd_intel8x0m_capture_ops,
  		.prealloc_size = 32 * 1024,
  		.prealloc_max_size = 64 * 1024,
  	},
  };
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
732
  static int __devinit snd_intel8x0m_pcm(struct intel8x0m *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
  {
  	int i, tblsize, device, err;
  	struct ich_pcm_table *tbl, *rec;
  
  #if 1
  	tbl = intel_pcms;
  	tblsize = 1;
  #else
  	switch (chip->device_type) {
  	case DEVICE_NFORCE:
  		tbl = nforce_pcms;
  		tblsize = ARRAY_SIZE(nforce_pcms);
  		break;
  	case DEVICE_ALI:
  		tbl = ali_pcms;
  		tblsize = ARRAY_SIZE(ali_pcms);
  		break;
  	default:
  		tbl = intel_pcms;
  		tblsize = 2;
  		break;
  	}
  #endif
  	device = 0;
  	for (i = 0; i < tblsize; i++) {
  		rec = tbl + i;
  		if (i > 0 && rec->ac97_idx) {
  			/* activate PCM only when associated AC'97 codec */
  			if (! chip->ichd[rec->ac97_idx].ac97)
  				continue;
  		}
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
764
  		err = snd_intel8x0m_pcm1(chip, device, rec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
766
767
768
769
770
771
772
773
774
775
776
777
  		if (err < 0)
  			return err;
  		device++;
  	}
  
  	chip->pcm_devs = device;
  	return 0;
  }
  	
  
  /*
   *  Mixer part
   */
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
778
  static void snd_intel8x0m_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
779
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
780
  	struct intel8x0m *chip = bus->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
  	chip->ac97_bus = NULL;
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
783
  static void snd_intel8x0m_mixer_free_ac97(struct snd_ac97 *ac97)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
785
  	struct intel8x0m *chip = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
787
  	chip->ac97 = NULL;
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
788
  static int __devinit snd_intel8x0m_mixer(struct intel8x0m *chip, int ac97_clock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
790
791
792
  	struct snd_ac97_bus *pbus;
  	struct snd_ac97_template ac97;
  	struct snd_ac97 *x97;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
  	int err;
  	unsigned int glob_sta = 0;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
795
  	static struct snd_ac97_bus_ops ops = {
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
796
797
  		.write = snd_intel8x0m_codec_write,
  		.read = snd_intel8x0m_codec_read,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
799
800
801
802
803
  	};
  
  	chip->in_ac97_init = 1;
  	
  	memset(&ac97, 0, sizeof(ac97));
  	ac97.private_data = chip;
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
804
  	ac97.private_free = snd_intel8x0m_mixer_free_ac97;
f1a63a38d   Takashi Iwai   [ALSA] ac97 - Sup...
805
  	ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
808
809
810
  
  	glob_sta = igetdword(chip, ICHREG(GLOB_STA));
  
  	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  		goto __err;
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
811
  	pbus->private_free = snd_intel8x0m_mixer_free_ac97_bus;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
  	if (ac97_clock >= 8000 && ac97_clock <= 48000)
  		pbus->clock = ac97_clock;
  	chip->ac97_bus = pbus;
  
  	ac97.pci = chip->pci;
  	ac97.num = glob_sta & ICH_SCR ? 1 : 0;
  	if ((err = snd_ac97_mixer(pbus, &ac97, &x97)) < 0) {
  		snd_printk(KERN_ERR "Unable to initialize codec #%d
  ", ac97.num);
  		if (ac97.num == 0)
  			goto __err;
  		return err;
  	}
  	chip->ac97 = x97;
  	if(ac97_is_modem(x97) && !chip->ichd[ICHD_MDMIN].ac97) {
  		chip->ichd[ICHD_MDMIN].ac97 = x97;
  		chip->ichd[ICHD_MDMOUT].ac97 = x97;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830
831
832
833
834
835
836
  
  	chip->in_ac97_init = 0;
  	return 0;
  
   __err:
  	/* clear the cold-reset bit for the next chance */
  	if (chip->device_type != DEVICE_ALI)
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
837
838
  		iputdword(chip, ICHREG(GLOB_CNT),
  			  igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
840
841
842
843
844
845
  	return err;
  }
  
  
  /*
   *
   */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
846
  static int snd_intel8x0m_ich_chip_init(struct intel8x0m *chip, int probing)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  {
  	unsigned long end_time;
  	unsigned int cnt, status, nstatus;
  	
  	/* put logic to right state */
  	/* first clear status bits */
  	status = ICH_RCS | ICH_MIINT | ICH_MOINT;
  	cnt = igetdword(chip, ICHREG(GLOB_STA));
  	iputdword(chip, ICHREG(GLOB_STA), cnt & status);
  
  	/* ACLink on, 2 channels */
  	cnt = igetdword(chip, ICHREG(GLOB_CNT));
  	cnt &= ~(ICH_ACLINK);
  	/* finish cold or do warm reset */
  	cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
  	iputdword(chip, ICHREG(GLOB_CNT), cnt);
5cd2ad81f   Paul Bolle   ALSA: intel8x0m: ...
863
864
  	usleep_range(500, 1000); /* give warm reset some time */
  	end_time = jiffies + HZ / 4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
866
867
  	do {
  		if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
  			goto __ok;
954bea355   Takashi Iwai   [ALSA] intel8x0 -...
868
  		schedule_timeout_uninterruptible(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
  	} while (time_after_eq(end_time, jiffies));
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
870
871
872
  	snd_printk(KERN_ERR "AC'97 warm reset still in progress? [0x%x]
  ",
  		   igetdword(chip, ICHREG(GLOB_CNT)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
874
875
876
877
878
879
880
881
882
  	return -EIO;
  
        __ok:
  	if (probing) {
  		/* wait for any codec ready status.
  		 * Once it becomes ready it should remain ready
  		 * as long as we do not disable the ac97 link.
  		 */
  		end_time = jiffies + HZ;
  		do {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
883
884
  			status = igetdword(chip, ICHREG(GLOB_STA)) &
  				(ICH_PCR | ICH_SCR | ICH_TCR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
886
  			if (status)
  				break;
954bea355   Takashi Iwai   [ALSA] intel8x0 -...
887
  			schedule_timeout_uninterruptible(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
  		} while (time_after_eq(end_time, jiffies));
  		if (! status) {
  			/* no codec is found */
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
891
892
893
  			snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]
  ",
  				   igetdword(chip, ICHREG(GLOB_STA)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
894
895
896
897
898
899
900
901
902
  			return -EIO;
  		}
  
  		/* up to two codecs (modem cannot be tertiary with ICH4) */
  		nstatus = ICH_PCR | ICH_SCR;
  
  		/* wait for other codecs ready status. */
  		end_time = jiffies + HZ / 4;
  		while (status != nstatus && time_after_eq(end_time, jiffies)) {
954bea355   Takashi Iwai   [ALSA] intel8x0 -...
903
  			schedule_timeout_uninterruptible(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
905
906
907
908
909
910
911
912
913
914
  			status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
  		}
  
  	} else {
  		/* resume phase */
  		status = 0;
  		if (chip->ac97)
  			status |= get_ich_codec_bit(chip, chip->ac97->num);
  		/* wait until all the probed codecs are ready */
  		end_time = jiffies + HZ;
  		do {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
915
916
  			nstatus = igetdword(chip, ICHREG(GLOB_STA)) &
  				(ICH_PCR | ICH_SCR | ICH_TCR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
918
  			if (status == nstatus)
  				break;
954bea355   Takashi Iwai   [ALSA] intel8x0 -...
919
  			schedule_timeout_uninterruptible(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
922
923
924
925
926
927
928
929
  		} while (time_after_eq(end_time, jiffies));
  	}
  
  	if (chip->device_type == DEVICE_SIS) {
  		/* unmute the output on SIS7012 */
  		iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
  	}
  
        	return 0;
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
930
  static int snd_intel8x0m_chip_init(struct intel8x0m *chip, int probing)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  {
  	unsigned int i;
  	int err;
  	
  	if ((err = snd_intel8x0m_ich_chip_init(chip, probing)) < 0)
  		return err;
  	iagetword(chip, 0);	/* clear semaphore flag */
  
  	/* disable interrupts */
  	for (i = 0; i < chip->bdbars_count; i++)
  		iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  	/* reset channels */
  	for (i = 0; i < chip->bdbars_count; i++)
  		iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  	/* initialize Buffer Descriptor Lists */
  	for (i = 0; i < chip->bdbars_count; i++)
  		iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
  	return 0;
  }
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
950
  static int snd_intel8x0m_free(struct intel8x0m *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
951
952
953
954
955
956
957
958
959
960
961
  {
  	unsigned int i;
  
  	if (chip->irq < 0)
  		goto __hw_end;
  	/* disable interrupts */
  	for (i = 0; i < chip->bdbars_count; i++)
  		iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  	/* reset channels */
  	for (i = 0; i < chip->bdbars_count; i++)
  		iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
ebf029da3   Takashi Iwai   [ALSA] Fix possib...
962
   __hw_end:
f000fd809   Jeff Garzik   [ALSA] Fix synchr...
963
  	if (chip->irq >= 0)
ebf029da3   Takashi Iwai   [ALSA] Fix possib...
964
  		free_irq(chip->irq, chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965
966
  	if (chip->bdbars.area)
  		snd_dma_free_pages(&chip->bdbars);
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
967
968
969
970
  	if (chip->addr)
  		pci_iounmap(chip->pci, chip->addr);
  	if (chip->bmaddr)
  		pci_iounmap(chip->pci, chip->bmaddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
973
974
975
976
977
978
979
980
  	pci_release_regions(chip->pci);
  	pci_disable_device(chip->pci);
  	kfree(chip);
  	return 0;
  }
  
  #ifdef CONFIG_PM
  /*
   * power management
   */
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
981
  static int intel8x0m_suspend(struct pci_dev *pci, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
  {
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
983
984
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct intel8x0m *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985
  	int i;
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
986
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
  	for (i = 0; i < chip->pcm_devs; i++)
  		snd_pcm_suspend_all(chip->pcm[i]);
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
989
  	snd_ac97_suspend(chip->ac97);
30b35399c   Takashi Iwai   [ALSA] Various fi...
990
  	if (chip->irq >= 0) {
f0063c448   Takashi Iwai   [ALSA] intel8x0m ...
991
  		free_irq(chip->irq, chip);
30b35399c   Takashi Iwai   [ALSA] Various fi...
992
993
  		chip->irq = -1;
  	}
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
994
995
  	pci_disable_device(pci);
  	pci_save_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
996
  	pci_set_power_state(pci, pci_choose_state(pci, state));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
997
998
  	return 0;
  }
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
999
  static int intel8x0m_resume(struct pci_dev *pci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1000
  {
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1001
1002
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct intel8x0m *chip = card->private_data;
30b35399c   Takashi Iwai   [ALSA] Various fi...
1003
  	pci_set_power_state(pci, PCI_D0);
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1004
  	pci_restore_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1005
1006
1007
1008
1009
1010
1011
  	if (pci_enable_device(pci) < 0) {
  		printk(KERN_ERR "intel8x0m: pci_enable_device failed, "
  		       "disabling device
  ");
  		snd_card_disconnect(card);
  		return -EIO;
  	}
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1012
  	pci_set_master(pci);
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1013
  	if (request_irq(pci->irq, snd_intel8x0m_interrupt,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
1014
  			IRQF_SHARED, KBUILD_MODNAME, chip)) {
30b35399c   Takashi Iwai   [ALSA] Various fi...
1015
1016
1017
1018
1019
1020
  		printk(KERN_ERR "intel8x0m: unable to grab IRQ %d, "
  		       "disabling device
  ", pci->irq);
  		snd_card_disconnect(card);
  		return -EIO;
  	}
f0063c448   Takashi Iwai   [ALSA] intel8x0m ...
1021
  	chip->irq = pci->irq;
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1022
  	snd_intel8x0m_chip_init(chip, 0);
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1023
  	snd_ac97_resume(chip->ac97);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024

5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1025
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1026
1027
1028
  	return 0;
  }
  #endif /* CONFIG_PM */
adf1b3d25   Takashi Iwai   [ALSA] Optimize f...
1029
  #ifdef CONFIG_PROC_FS
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1030
1031
  static void snd_intel8x0m_proc_read(struct snd_info_entry * entry,
  				   struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1033
  	struct intel8x0m *chip = entry->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
1035
1036
1037
1038
1039
1040
1041
  	unsigned int tmp;
  
  	snd_iprintf(buffer, "Intel8x0m
  
  ");
  	if (chip->device_type == DEVICE_ALI)
  		return;
  	tmp = igetdword(chip, ICHREG(GLOB_STA));
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1042
1043
1044
  	snd_iprintf(buffer, "Global control        : 0x%08x
  ",
  		    igetdword(chip, ICHREG(GLOB_CNT)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
1046
1047
1048
1049
1050
1051
1052
1053
  	snd_iprintf(buffer, "Global status         : 0x%08x
  ", tmp);
  	snd_iprintf(buffer, "AC'97 codecs ready    :%s%s%s%s
  ",
  			tmp & ICH_PCR ? " primary" : "",
  			tmp & ICH_SCR ? " secondary" : "",
  			tmp & ICH_TCR ? " tertiary" : "",
  			(tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
  }
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1054
  static void __devinit snd_intel8x0m_proc_init(struct intel8x0m * chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1056
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1057
1058
  
  	if (! snd_card_proc_new(chip->card, "intel8x0m", &entry))
bf850204a   Takashi Iwai   [ALSA] Remove unn...
1059
  		snd_info_set_text_ops(entry, chip, snd_intel8x0m_proc_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1060
  }
adf1b3d25   Takashi Iwai   [ALSA] Optimize f...
1061
1062
1063
  #else /* !CONFIG_PROC_FS */
  #define snd_intel8x0m_proc_init(chip)
  #endif /* CONFIG_PROC_FS */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1064

a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1065
  static int snd_intel8x0m_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1067
  	struct intel8x0m *chip = device->device_data;
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1068
  	return snd_intel8x0m_free(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1069
1070
1071
1072
1073
1074
  }
  
  struct ich_reg_info {
  	unsigned int int_sta_mask;
  	unsigned int offset;
  };
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1075
  static int __devinit snd_intel8x0m_create(struct snd_card *card,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
1077
  					 struct pci_dev *pci,
  					 unsigned long device_type,
966a7f0dc   Paul Bolle   ALSA: intel8x0m: ...
1078
  					 struct intel8x0m **r_intel8x0m)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1080
  	struct intel8x0m *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1081
1082
1083
  	int err;
  	unsigned int i;
  	unsigned int int_sta_masks;
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1084
1085
  	struct ichdev *ichdev;
  	static struct snd_device_ops ops = {
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1086
  		.dev_free =	snd_intel8x0m_dev_free,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087
1088
1089
1090
1091
1092
  	};
  	static struct ich_reg_info intel_regs[2] = {
  		{ ICH_MIINT, 0 },
  		{ ICH_MOINT, 0x10 },
  	};
  	struct ich_reg_info *tbl;
966a7f0dc   Paul Bolle   ALSA: intel8x0m: ...
1093
  	*r_intel8x0m = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094
1095
1096
  
  	if ((err = pci_enable_device(pci)) < 0)
  		return err;
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
1097
  	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  	if (chip == NULL) {
  		pci_disable_device(pci);
  		return -ENOMEM;
  	}
  	spin_lock_init(&chip->reg_lock);
  	chip->device_type = device_type;
  	chip->card = card;
  	chip->pci = pci;
  	chip->irq = -1;
  
  	if ((err = pci_request_regions(pci, card->shortname)) < 0) {
  		kfree(chip);
  		pci_disable_device(pci);
  		return err;
  	}
  
  	if (device_type == DEVICE_ALI) {
  		/* ALI5455 has no ac97 region */
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1116
  		chip->bmaddr = pci_iomap(pci, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
1118
  		goto port_inited;
  	}
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1119
1120
1121
1122
1123
1124
1125
  	if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) /* ICH4 and Nforce */
  		chip->addr = pci_iomap(pci, 2, 0);
  	else
  		chip->addr = pci_iomap(pci, 0, 0);
  	if (!chip->addr) {
  		snd_printk(KERN_ERR "AC'97 space ioremap problem
  ");
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1126
  		snd_intel8x0m_free(chip);
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1127
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1128
  	}
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1129
1130
1131
1132
1133
1134
1135
  	if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) /* ICH4 */
  		chip->bmaddr = pci_iomap(pci, 3, 0);
  	else
  		chip->bmaddr = pci_iomap(pci, 1, 0);
  	if (!chip->bmaddr) {
  		snd_printk(KERN_ERR "Controller space ioremap problem
  ");
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1136
  		snd_intel8x0m_free(chip);
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1137
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
1139
1140
  	}
  
   port_inited:
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1141
  	if (request_irq(pci->irq, snd_intel8x0m_interrupt, IRQF_SHARED,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
1142
  			KBUILD_MODNAME, chip)) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1143
1144
  		snd_printk(KERN_ERR "unable to grab IRQ %d
  ", pci->irq);
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1145
  		snd_intel8x0m_free(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
  		return -EBUSY;
  	}
  	chip->irq = pci->irq;
  	pci_set_master(pci);
  	synchronize_irq(chip->irq);
  
  	/* initialize offsets */
  	chip->bdbars_count = 2;
  	tbl = intel_regs;
  
  	for (i = 0; i < chip->bdbars_count; i++) {
  		ichdev = &chip->ichd[i];
  		ichdev->ichd = i;
  		ichdev->reg_offset = tbl[i].offset;
  		ichdev->int_sta_mask = tbl[i].int_sta_mask;
  		if (device_type == DEVICE_SIS) {
  			/* SiS 7013 swaps the registers */
  			ichdev->roff_sr = ICH_REG_OFF_PICB;
  			ichdev->roff_picb = ICH_REG_OFF_SR;
  		} else {
  			ichdev->roff_sr = ICH_REG_OFF_SR;
  			ichdev->roff_picb = ICH_REG_OFF_PICB;
  		}
  		if (device_type == DEVICE_ALI)
  			ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
  	}
  	/* SIS7013 handles the pcm data in bytes, others are in words */
  	chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
  
  	/* allocate buffer descriptor lists */
  	/* the start of each lists must be aligned to 8 bytes */
  	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  				chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
  				&chip->bdbars) < 0) {
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1180
  		snd_intel8x0m_free(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
  		return -ENOMEM;
  	}
  	/* tables must be aligned to 8 bytes here, but the kernel pages
  	   are much bigger, so we don't care (on i386) */
  	int_sta_masks = 0;
  	for (i = 0; i < chip->bdbars_count; i++) {
  		ichdev = &chip->ichd[i];
  		ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
  		ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
  		int_sta_masks |= ichdev->int_sta_mask;
  	}
  	chip->int_sta_reg = ICH_REG_GLOB_STA;
  	chip->int_sta_mask = int_sta_masks;
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1194
1195
  	if ((err = snd_intel8x0m_chip_init(chip, 1)) < 0) {
  		snd_intel8x0m_free(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
1197
  		return err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1198
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1199
  		snd_intel8x0m_free(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1200
1201
1202
1203
  		return err;
  	}
  
  	snd_card_set_dev(card, &pci->dev);
966a7f0dc   Paul Bolle   ALSA: intel8x0m: ...
1204
  	*r_intel8x0m = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1205
1206
1207
1208
1209
1210
1211
  	return 0;
  }
  
  static struct shortname_table {
  	unsigned int id;
  	const char *s;
  } shortnames[] __devinitdata = {
8cdfd2519   Takashi Iwai   [ALSA] Remove sup...
1212
1213
  	{ PCI_DEVICE_ID_INTEL_82801AA_6, "Intel 82801AA-ICH" },
  	{ PCI_DEVICE_ID_INTEL_82801AB_6, "Intel 82901AB-ICH0" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1214
1215
  	{ PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
  	{ PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
8cdfd2519   Takashi Iwai   [ALSA] Remove sup...
1216
1217
1218
1219
1220
  	{ PCI_DEVICE_ID_INTEL_82801CA_6, "Intel 82801CA-ICH3" },
  	{ PCI_DEVICE_ID_INTEL_82801DB_6, "Intel 82801DB-ICH4" },
  	{ PCI_DEVICE_ID_INTEL_82801EB_6, "Intel ICH5" },
  	{ PCI_DEVICE_ID_INTEL_ICH6_17, "Intel ICH6" },
  	{ PCI_DEVICE_ID_INTEL_ICH7_19, "Intel ICH7" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1221
1222
  	{ 0x7446, "AMD AMD768" },
  	{ PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
8cdfd2519   Takashi Iwai   [ALSA] Remove sup...
1223
  	{ PCI_DEVICE_ID_NVIDIA_MCP1_MODEM, "NVidia nForce" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1224
1225
1226
  	{ PCI_DEVICE_ID_NVIDIA_MCP2_MODEM, "NVidia nForce2" },
  	{ PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM, "NVidia nForce2s" },
  	{ PCI_DEVICE_ID_NVIDIA_MCP3_MODEM, "NVidia nForce3" },
df1fe1328   Dmitry Eremin-Solenikov   ALSA: intel8x0m: ...
1227
  	{ 0x746e, "AMD AMD8111" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1228
1229
  #if 0
  	{ 0x5455, "ALi M5455" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
1231
1232
1233
1234
1235
1236
  #endif
  	{ 0 },
  };
  
  static int __devinit snd_intel8x0m_probe(struct pci_dev *pci,
  					const struct pci_device_id *pci_id)
  {
6b75a9d8b   Takashi Iwai   [ALSA] Remove xxx...
1237
1238
  	struct snd_card *card;
  	struct intel8x0m *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1239
1240
  	int err;
  	struct shortname_table *name;
e58de7baf   Takashi Iwai   ALSA: Convert to ...
1241
1242
1243
  	err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  	if (err < 0)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
  
  	strcpy(card->driver, "ICH-MODEM");
  	strcpy(card->shortname, "Intel ICH");
  	for (name = shortnames; name->id; name++) {
  		if (pci->device == name->id) {
  			strcpy(card->shortname, name->s);
  			break;
  		}
  	}
  	strcat(card->shortname," Modem");
  
  	if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1259
  	card->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1260

a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1261
  	if ((err = snd_intel8x0m_mixer(chip, ac97_clock)) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1262
1263
1264
  		snd_card_free(card);
  		return err;
  	}
a6e8509f2   Paul Bolle   ALSA: intel8x0m: ...
1265
  	if ((err = snd_intel8x0m_pcm(chip)) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
1267
1268
1269
1270
  		snd_card_free(card);
  		return err;
  	}
  	
  	snd_intel8x0m_proc_init(chip);
3388c37e0   Takashi Iwai   [ALSA] intel8x0 -...
1271
1272
  	sprintf(card->longname, "%s at irq %i",
  		card->shortname, chip->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
1275
1276
1277
1278
  
  	if ((err = snd_card_register(card)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  	pci_set_drvdata(pci, card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  	return 0;
  }
  
  static void __devexit snd_intel8x0m_remove(struct pci_dev *pci)
  {
  	snd_card_free(pci_get_drvdata(pci));
  	pci_set_drvdata(pci, NULL);
  }
  
  static struct pci_driver driver = {
3733e424c   Takashi Iwai   ALSA: Use KBUILD_...
1289
  	.name = KBUILD_MODNAME,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
1291
1292
  	.id_table = snd_intel8x0m_ids,
  	.probe = snd_intel8x0m_probe,
  	.remove = __devexit_p(snd_intel8x0m_remove),
5809c6c41   Takashi Iwai   [ALSA] intel8x0 -...
1293
1294
1295
1296
  #ifdef CONFIG_PM
  	.suspend = intel8x0m_suspend,
  	.resume = intel8x0m_resume,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1297
1298
1299
1300
1301
  };
  
  
  static int __init alsa_card_intel8x0m_init(void)
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
1302
  	return pci_register_driver(&driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1303
1304
1305
1306
1307
1308
1309
1310
1311
  }
  
  static void __exit alsa_card_intel8x0m_exit(void)
  {
  	pci_unregister_driver(&driver);
  }
  
  module_init(alsa_card_intel8x0m_init)
  module_exit(alsa_card_intel8x0m_exit)