Blame view

sound/pci/atiixp_modem.c 36 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  /*
   *   ALSA driver for ATI IXP 150/200/250 AC97 modem controllers
   *
   *	Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
   *
   *   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
   *
   */
  
  #include <sound/driver.h>
  #include <asm/io.h>
  #include <linux/delay.h>
  #include <linux/interrupt.h>
  #include <linux/init.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
  #include <linux/moduleparam.h>
62932df8f   Ingo Molnar   [ALSA] semaphore ...
30
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
35
36
37
38
39
40
41
  #include <sound/core.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  #include <sound/info.h>
  #include <sound/ac97_codec.h>
  #include <sound/initval.h>
  
  MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  MODULE_DESCRIPTION("ATI IXP MC97 controller");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250}}");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
42
43
44
  static int index = -2; /* Exclude the first card */
  static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
  static int ac97_clock = 48000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45

b7fe46220   Clemens Ladisch   [ALSA] highlander...
46
  module_param(index, int, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
48
  module_param(id, charp, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
b7fe46220   Clemens Ladisch   [ALSA] highlander...
50
  module_param(ac97_clock, int, 0444);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
2b3e584b9   Takashi Iwai   [ALSA] Add dummy ...
52
53
  /* just for backward compatibility */
  static int enable;
698444f31   Takashi Iwai   [ALSA] Fix the ty...
54
  module_param(enable, bool, 0444);
2b3e584b9   Takashi Iwai   [ALSA] Add dummy ...
55

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  /*
   */
  
  #define ATI_REG_ISR			0x00	/* interrupt source */
  #define  ATI_REG_ISR_MODEM_IN_XRUN	(1U<<0)
  #define  ATI_REG_ISR_MODEM_IN_STATUS	(1U<<1)
  #define  ATI_REG_ISR_MODEM_OUT1_XRUN	(1U<<2)
  #define  ATI_REG_ISR_MODEM_OUT1_STATUS	(1U<<3)
  #define  ATI_REG_ISR_MODEM_OUT2_XRUN	(1U<<4)
  #define  ATI_REG_ISR_MODEM_OUT2_STATUS	(1U<<5)
  #define  ATI_REG_ISR_MODEM_OUT3_XRUN	(1U<<6)
  #define  ATI_REG_ISR_MODEM_OUT3_STATUS	(1U<<7)
  #define  ATI_REG_ISR_PHYS_INTR		(1U<<8)
  #define  ATI_REG_ISR_PHYS_MISMATCH	(1U<<9)
  #define  ATI_REG_ISR_CODEC0_NOT_READY	(1U<<10)
  #define  ATI_REG_ISR_CODEC1_NOT_READY	(1U<<11)
  #define  ATI_REG_ISR_CODEC2_NOT_READY	(1U<<12)
  #define  ATI_REG_ISR_NEW_FRAME		(1U<<13)
  #define  ATI_REG_ISR_MODEM_GPIO_DATA	(1U<<14)
  
  #define ATI_REG_IER			0x04	/* interrupt enable */
  #define  ATI_REG_IER_MODEM_IN_XRUN_EN	(1U<<0)
  #define  ATI_REG_IER_MODEM_STATUS_EN	(1U<<1)
  #define  ATI_REG_IER_MODEM_OUT1_XRUN_EN	(1U<<2)
  #define  ATI_REG_IER_MODEM_OUT2_XRUN_EN	(1U<<4)
  #define  ATI_REG_IER_MODEM_OUT3_XRUN_EN	(1U<<6)
  #define  ATI_REG_IER_PHYS_INTR_EN	(1U<<8)
  #define  ATI_REG_IER_PHYS_MISMATCH_EN	(1U<<9)
  #define  ATI_REG_IER_CODEC0_INTR_EN	(1U<<10)
  #define  ATI_REG_IER_CODEC1_INTR_EN	(1U<<11)
  #define  ATI_REG_IER_CODEC2_INTR_EN	(1U<<12)
  #define  ATI_REG_IER_NEW_FRAME_EN	(1U<<13)	/* (RO */
  #define  ATI_REG_IER_MODEM_GPIO_DATA_EN	(1U<<14)	/* (WO) modem is running */
  #define  ATI_REG_IER_MODEM_SET_BUS_BUSY	(1U<<15)
  
  #define ATI_REG_CMD			0x08	/* command */
  #define  ATI_REG_CMD_POWERDOWN	(1U<<0)
  #define  ATI_REG_CMD_MODEM_RECEIVE_EN	(1U<<1)	/* modem only */
  #define  ATI_REG_CMD_MODEM_SEND1_EN	(1U<<2)	/* modem only */
  #define  ATI_REG_CMD_MODEM_SEND2_EN	(1U<<3)	/* modem only */
  #define  ATI_REG_CMD_MODEM_SEND3_EN	(1U<<4)	/* modem only */
  #define  ATI_REG_CMD_MODEM_STATUS_MEM	(1U<<5)	/* modem only */
  #define  ATI_REG_CMD_MODEM_IN_DMA_EN	(1U<<8)	/* modem only */
  #define  ATI_REG_CMD_MODEM_OUT_DMA1_EN	(1U<<9)	/* modem only */
  #define  ATI_REG_CMD_MODEM_OUT_DMA2_EN	(1U<<10)	/* modem only */
  #define  ATI_REG_CMD_MODEM_OUT_DMA3_EN	(1U<<11)	/* modem only */
  #define  ATI_REG_CMD_AUDIO_PRESENT	(1U<<20)
  #define  ATI_REG_CMD_MODEM_GPIO_THRU_DMA	(1U<<22)	/* modem only */
  #define  ATI_REG_CMD_LOOPBACK_EN	(1U<<23)
  #define  ATI_REG_CMD_PACKED_DIS		(1U<<24)
  #define  ATI_REG_CMD_BURST_EN		(1U<<25)
  #define  ATI_REG_CMD_PANIC_EN		(1U<<26)
  #define  ATI_REG_CMD_MODEM_PRESENT	(1U<<27)
  #define  ATI_REG_CMD_ACLINK_ACTIVE	(1U<<28)
  #define  ATI_REG_CMD_AC_SOFT_RESET	(1U<<29)
  #define  ATI_REG_CMD_AC_SYNC		(1U<<30)
  #define  ATI_REG_CMD_AC_RESET		(1U<<31)
  
  #define ATI_REG_PHYS_OUT_ADDR		0x0c
  #define  ATI_REG_PHYS_OUT_CODEC_MASK	(3U<<0)
  #define  ATI_REG_PHYS_OUT_RW		(1U<<2)
  #define  ATI_REG_PHYS_OUT_ADDR_EN	(1U<<8)
  #define  ATI_REG_PHYS_OUT_ADDR_SHIFT	9
  #define  ATI_REG_PHYS_OUT_DATA_SHIFT	16
  
  #define ATI_REG_PHYS_IN_ADDR		0x10
  #define  ATI_REG_PHYS_IN_READ_FLAG	(1U<<8)
  #define  ATI_REG_PHYS_IN_ADDR_SHIFT	9
  #define  ATI_REG_PHYS_IN_DATA_SHIFT	16
  
  #define ATI_REG_SLOTREQ			0x14
  
  #define ATI_REG_COUNTER			0x18
  #define  ATI_REG_COUNTER_SLOT		(3U<<0)	/* slot # */
  #define  ATI_REG_COUNTER_BITCLOCK	(31U<<8)
  
  #define ATI_REG_IN_FIFO_THRESHOLD	0x1c
  
  #define ATI_REG_MODEM_IN_DMA_LINKPTR	0x20
  #define ATI_REG_MODEM_IN_DMA_DT_START	0x24	/* RO */
  #define ATI_REG_MODEM_IN_DMA_DT_NEXT	0x28	/* RO */
  #define ATI_REG_MODEM_IN_DMA_DT_CUR	0x2c	/* RO */
  #define ATI_REG_MODEM_IN_DMA_DT_SIZE	0x30
  #define ATI_REG_MODEM_OUT_FIFO		0x34	/* output threshold */
  #define  ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK	(0xf<<16)
  #define  ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT	16
  #define ATI_REG_MODEM_OUT_DMA1_LINKPTR	0x38
  #define ATI_REG_MODEM_OUT_DMA2_LINKPTR	0x3c
  #define ATI_REG_MODEM_OUT_DMA3_LINKPTR	0x40
  #define ATI_REG_MODEM_OUT_DMA1_DT_START	0x44
  #define ATI_REG_MODEM_OUT_DMA1_DT_NEXT	0x48
  #define ATI_REG_MODEM_OUT_DMA1_DT_CUR	0x4c
  #define ATI_REG_MODEM_OUT_DMA2_DT_START	0x50
  #define ATI_REG_MODEM_OUT_DMA2_DT_NEXT	0x54
  #define ATI_REG_MODEM_OUT_DMA2_DT_CUR	0x58
  #define ATI_REG_MODEM_OUT_DMA3_DT_START	0x5c
  #define ATI_REG_MODEM_OUT_DMA3_DT_NEXT	0x60
  #define ATI_REG_MODEM_OUT_DMA3_DT_CUR	0x64
  #define ATI_REG_MODEM_OUT_DMA12_DT_SIZE	0x68
  #define ATI_REG_MODEM_OUT_DMA3_DT_SIZE	0x6c
  #define ATI_REG_MODEM_OUT_FIFO_USED     0x70
  #define ATI_REG_MODEM_OUT_GPIO		0x74
  #define  ATI_REG_MODEM_OUT_GPIO_EN	   1
  #define  ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT 5
  #define ATI_REG_MODEM_IN_GPIO		0x78
  
  #define ATI_REG_MODEM_MIRROR		0x7c
  #define ATI_REG_AUDIO_MIRROR		0x80
  
  #define ATI_REG_MODEM_FIFO_FLUSH	0x88
  #define  ATI_REG_MODEM_FIFO_OUT1_FLUSH	(1U<<0)
  #define  ATI_REG_MODEM_FIFO_OUT2_FLUSH	(1U<<1)
  #define  ATI_REG_MODEM_FIFO_OUT3_FLUSH	(1U<<2)
  #define  ATI_REG_MODEM_FIFO_IN_FLUSH	(1U<<3)
  
  /* LINKPTR */
  #define  ATI_REG_LINKPTR_EN		(1U<<0)
  
  #define ATI_MAX_DESCRIPTORS	256	/* max number of descriptor packets */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
176
  struct atiixp_modem;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
  
  /*
   * DMA packate descriptor
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
181
  struct atiixp_dma_desc {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
  	u32 addr;	/* DMA buffer address */
  	u16 status;	/* status bits */
  	u16 size;	/* size of the packet in dwords */
  	u32 next;	/* address of the next packet descriptor */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
186
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  
  /*
   * stream enum
   */
  enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, NUM_ATI_DMAS }; /* DMAs */
  enum { ATI_PCM_OUT, ATI_PCM_IN, NUM_ATI_PCMS }; /* AC97 pcm slots */
  enum { ATI_PCMDEV_ANALOG, NUM_ATI_PCMDEVS }; /* pcm devices */
  
  #define NUM_ATI_CODECS	3
  
  
  /*
   * constants and callbacks for each DMA type
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
201
  struct atiixp_dma_ops {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
  	int type;			/* ATI_DMA_XXX */
  	unsigned int llp_offset;	/* LINKPTR offset */
  	unsigned int dt_cur;		/* DT_CUR offset */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
205
206
207
208
209
210
  	/* called from open callback */
  	void (*enable_dma)(struct atiixp_modem *chip, int on);
  	/* called from trigger (START/STOP) */
  	void (*enable_transfer)(struct atiixp_modem *chip, int on);
   	/* called from trigger (STOP only) */
  	void (*flush_dma)(struct atiixp_modem *chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
214
215
  };
  
  /*
   * DMA stream
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
216
217
  struct atiixp_dma {
  	const struct atiixp_dma_ops *ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  	struct snd_dma_buffer desc_buf;
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
219
  	struct snd_pcm_substream *substream;	/* assigned PCM substream */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
227
228
229
230
  	unsigned int buf_addr, buf_bytes;	/* DMA buffer address, bytes */
  	unsigned int period_bytes, periods;
  	int opened;
  	int running;
  	int pcm_open_flag;
  	int ac97_pcm_type;	/* index # of ac97_pcm to access, -1 = not used */
  };
  
  /*
   * ATI IXP chip
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
231
232
  struct atiixp_modem {
  	struct snd_card *card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
237
238
239
  	struct pci_dev *pci;
  
  	struct resource *res;		/* memory i/o */
  	unsigned long addr;
  	void __iomem *remap_addr;
  	int irq;
  	
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
240
241
  	struct snd_ac97_bus *ac97_bus;
  	struct snd_ac97 *ac97[NUM_ATI_CODECS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
  
  	spinlock_t reg_lock;
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
244
  	struct atiixp_dma dmas[NUM_ATI_DMAS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  	struct ac97_pcm *pcms[NUM_ATI_PCMS];
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
246
  	struct snd_pcm *pcmdevs[NUM_ATI_PCMDEVS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
249
250
251
252
  
  	int max_channels;		/* max. channels for PCM out */
  
  	unsigned int codec_not_ready_bits;	/* for codec detection */
  
  	int spdif_over_aclink;		/* passed from the module option */
62932df8f   Ingo Molnar   [ALSA] semaphore ...
253
  	struct mutex open_mutex;	/* playback open mutex */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
  };
  
  
  /*
   */
f40b68903   Takashi Iwai   [ALSA] Fix sectio...
259
  static struct pci_device_id snd_atiixp_ids[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  	{ 0x1002, 0x434d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
548e7823b   Harald Welte   [ALSA] Add new pc...
261
  	{ 0x1002, 0x4378, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB400 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  	{ 0, }
  };
  
  MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
  
  
  /*
   * lowlevel functions
   */
  
  /*
   * update the bits of the given register.
   * return 1 if the bits changed.
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
276
277
  static int snd_atiixp_update_bits(struct atiixp_modem *chip, unsigned int reg,
  				  unsigned int mask, unsigned int value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  {
  	void __iomem *addr = chip->remap_addr + reg;
  	unsigned int data, old_data;
  	old_data = data = readl(addr);
  	data &= ~mask;
  	data |= value;
  	if (old_data == data)
  		return 0;
  	writel(data, addr);
  	return 1;
  }
  
  /*
   * macros for easy use
   */
  #define atiixp_write(chip,reg,value) \
  	writel(value, chip->remap_addr + ATI_REG_##reg)
  #define atiixp_read(chip,reg) \
  	readl(chip->remap_addr + ATI_REG_##reg)
  #define atiixp_update(chip,reg,mask,val) \
  	snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
305
306
  /*
   * handling DMA packets
   *
   * we allocate a linear buffer for the DMA, and split it to  each packet.
   * in a future version, a scatter-gather buffer should be implemented.
   */
  
  #define ATI_DESC_LIST_SIZE \
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
307
  	PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(struct atiixp_dma_desc))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
311
312
313
314
315
316
317
  
  /*
   * build packets ring for the given buffer size.
   *
   * IXP handles the buffer descriptors, which are connected as a linked
   * list.  although we can change the list dynamically, in this version,
   * a static RING of buffer descriptors is used.
   *
   * the ring is built in this function, and is set up to the hardware. 
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
318
319
320
321
322
  static int atiixp_build_dma_packets(struct atiixp_modem *chip,
  				    struct atiixp_dma *dma,
  				    struct snd_pcm_substream *substream,
  				    unsigned int periods,
  				    unsigned int period_bytes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  {
  	unsigned int i;
  	u32 addr, desc_addr;
  	unsigned long flags;
  
  	if (periods > ATI_MAX_DESCRIPTORS)
  		return -ENOMEM;
  
  	if (dma->desc_buf.area == NULL) {
  		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  					ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  			return -ENOMEM;
  		dma->period_bytes = dma->periods = 0; /* clear */
  	}
  
  	if (dma->periods == periods && dma->period_bytes == period_bytes)
  		return 0;
  
  	/* reset DMA before changing the descriptor table */
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	writel(0, chip->remap_addr + dma->ops->llp_offset);
  	dma->ops->enable_dma(chip, 0);
  	dma->ops->enable_dma(chip, 1);
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
  
  	/* fill the entries */
  	addr = (u32)substream->runtime->dma_addr;
  	desc_addr = (u32)dma->desc_buf.addr;
  	for (i = 0; i < periods; i++) {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
352
353
  		struct atiixp_dma_desc *desc;
  		desc = &((struct atiixp_dma_desc *)dma->desc_buf.area)[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
  		desc->addr = cpu_to_le32(addr);
  		desc->status = 0;
  		desc->size = period_bytes >> 2; /* in dwords */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
357
  		desc_addr += sizeof(struct atiixp_dma_desc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  		if (i == periods - 1)
  			desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  		else
  			desc->next = cpu_to_le32(desc_addr);
  		addr += period_bytes;
  	}
  
  	writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  	       chip->remap_addr + dma->ops->llp_offset);
  
  	dma->period_bytes = period_bytes;
  	dma->periods = periods;
  
  	return 0;
  }
  
  /*
   * remove the ring buffer and release it if assigned
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
377
378
379
  static void atiixp_clear_dma_packets(struct atiixp_modem *chip,
  				     struct atiixp_dma *dma,
  				     struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
386
387
388
389
390
  {
  	if (dma->desc_buf.area) {
  		writel(0, chip->remap_addr + dma->ops->llp_offset);
  		snd_dma_free_pages(&dma->desc_buf);
  		dma->desc_buf.area = NULL;
  	}
  }
  
  /*
   * AC97 interface
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
391
  static int snd_atiixp_acquire_codec(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
393
394
395
396
  {
  	int timeout = 1000;
  
  	while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  		if (! timeout--) {
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
397
398
  			snd_printk(KERN_WARNING "atiixp-modem: codec acquire timeout
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
400
401
402
403
404
  			return -EBUSY;
  		}
  		udelay(1);
  	}
  	return 0;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
405
406
407
  static unsigned short snd_atiixp_codec_read(struct atiixp_modem *chip,
  					    unsigned short codec,
  					    unsigned short reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
  {
  	unsigned int data;
  	int timeout;
  
  	if (snd_atiixp_acquire_codec(chip) < 0)
  		return 0xffff;
  	data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  		ATI_REG_PHYS_OUT_ADDR_EN |
  		ATI_REG_PHYS_OUT_RW |
  		codec;
  	atiixp_write(chip, PHYS_OUT_ADDR, data);
  	if (snd_atiixp_acquire_codec(chip) < 0)
  		return 0xffff;
  	timeout = 1000;
  	do {
  		data = atiixp_read(chip, PHYS_IN_ADDR);
  		if (data & ATI_REG_PHYS_IN_READ_FLAG)
  			return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
  		udelay(1);
  	} while (--timeout);
  	/* time out may happen during reset */
  	if (reg < 0x7c)
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
430
431
  		snd_printk(KERN_WARNING "atiixp-modem: codec read timeout (reg %x)
  ", reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
  	return 0xffff;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
434
435
436
  static void snd_atiixp_codec_write(struct atiixp_modem *chip,
  				   unsigned short codec,
  				   unsigned short reg, unsigned short val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
439
440
441
442
443
444
445
446
  {
  	unsigned int data;
      
  	if (snd_atiixp_acquire_codec(chip) < 0)
  		return;
  	data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  		((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  		ATI_REG_PHYS_OUT_ADDR_EN | codec;
  	atiixp_write(chip, PHYS_OUT_ADDR, data);
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
447
448
  static unsigned short snd_atiixp_ac97_read(struct snd_ac97 *ac97,
  					   unsigned short reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
450
  	struct atiixp_modem *chip = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
  	return snd_atiixp_codec_read(chip, ac97->num, reg);
      
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
454
455
  static void snd_atiixp_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  				  unsigned short val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
457
  	struct atiixp_modem *chip = ac97->private_data;
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
458
459
460
461
462
  	if (reg == AC97_GPIO_STATUS) {
  		atiixp_write(chip, MODEM_OUT_GPIO,
  			(val << ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT) | ATI_REG_MODEM_OUT_GPIO_EN);
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
464
465
466
467
468
  	snd_atiixp_codec_write(chip, ac97->num, reg, val);
  }
  
  /*
   * reset AC link
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
469
  static int snd_atiixp_aclink_reset(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  {
  	int timeout;
  
  	/* reset powerdoewn */
  	if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  		udelay(10);
  
  	/* perform a software reset */
  	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  	atiixp_read(chip, CMD);
  	udelay(10);
  	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
      
  	timeout = 10;
  	while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  		/* do a hard reset */
  		atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  			      ATI_REG_CMD_AC_SYNC);
  		atiixp_read(chip, CMD);
bfdcbace6   Takashi Iwai   [ALSA] Remove sup...
489
  		msleep(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
  		atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  		if (--timeout) {
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
492
493
  			snd_printk(KERN_ERR "atiixp-modem: codec reset timeout
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
495
496
497
498
499
500
501
502
503
504
505
  			break;
  		}
  	}
  
  	/* deassert RESET and assert SYNC to make sure */
  	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  		      ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  
  	return 0;
  }
  
  #ifdef CONFIG_PM
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
506
  static int snd_atiixp_aclink_down(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  {
  	// if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  	//	return -EBUSY;
  	atiixp_update(chip, CMD,
  		     ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  		     ATI_REG_CMD_POWERDOWN);
  	return 0;
  }
  #endif
  
  /*
   * auto-detection of codecs
   *
   * the IXP chip can generate interrupts for the non-existing codecs.
   * NEW_FRAME interrupt is used to make sure that the interrupt is generated
   * even if all three codecs are connected.
   */
  
  #define ALL_CODEC_NOT_READY \
  	    (ATI_REG_ISR_CODEC0_NOT_READY |\
  	     ATI_REG_ISR_CODEC1_NOT_READY |\
  	     ATI_REG_ISR_CODEC2_NOT_READY)
  #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
530
  static int snd_atiixp_codec_detect(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
534
535
536
  {
  	int timeout;
  
  	chip->codec_not_ready_bits = 0;
  	atiixp_write(chip, IER, CODEC_CHECK_BITS);
  	/* wait for the interrupts */
bfdcbace6   Takashi Iwai   [ALSA] Remove sup...
537
  	timeout = 50;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
  	while (timeout-- > 0) {
bfdcbace6   Takashi Iwai   [ALSA] Remove sup...
539
  		msleep(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
541
542
543
544
545
  		if (chip->codec_not_ready_bits)
  			break;
  	}
  	atiixp_write(chip, IER, 0); /* disable irqs */
  
  	if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
546
547
  		snd_printk(KERN_ERR "atiixp-modem: no codec detected!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
551
552
553
554
555
556
  		return -ENXIO;
  	}
  	return 0;
  }
  
  
  /*
   * enable DMA and irqs
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
557
  static int snd_atiixp_chip_start(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  {
  	unsigned int reg;
  
  	/* set up spdif, enable burst mode */
  	reg = atiixp_read(chip, CMD);
  	reg |= ATI_REG_CMD_BURST_EN;
  	if(!(reg & ATI_REG_CMD_MODEM_PRESENT))
  		reg |= ATI_REG_CMD_MODEM_PRESENT;
  	atiixp_write(chip, CMD, reg);
  
  	/* clear all interrupt source */
  	atiixp_write(chip, ISR, 0xffffffff);
  	/* enable irqs */
  	atiixp_write(chip, IER,
  		     ATI_REG_IER_MODEM_STATUS_EN |
  		     ATI_REG_IER_MODEM_IN_XRUN_EN |
  		     ATI_REG_IER_MODEM_OUT1_XRUN_EN);
  	return 0;
  }
  
  
  /*
   * disable DMA and IRQs
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
582
  static int snd_atiixp_chip_stop(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
  {
  	/* clear interrupt source */
  	atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  	/* disable irqs */
  	atiixp_write(chip, IER, 0);
  	return 0;
  }
  
  
  /*
   * PCM section
   */
  
  /*
   * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
   * position.  when SG-buffer is implemented, the offset must be calculated
   * correctly...
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
601
  static snd_pcm_uframes_t snd_atiixp_pcm_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
603
604
605
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct atiixp_dma *dma = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  	unsigned int curptr;
  	int timeout = 1000;
  
  	while (timeout--) {
  		curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  		if (curptr < dma->buf_addr)
  			continue;
  		curptr -= dma->buf_addr;
  		if (curptr >= dma->buf_bytes)
  			continue;
  		return bytes_to_frames(runtime, curptr);
  	}
  	snd_printd("atiixp-modem: invalid DMA pointer read 0x%x (buf=%x)
  ",
  		   readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  	return 0;
  }
  
  /*
   * XRUN detected, and stop the PCM substream
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
627
628
  static void snd_atiixp_xrun_dma(struct atiixp_modem *chip,
  				struct atiixp_dma *dma)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
631
  {
  	if (! dma->substream || ! dma->running)
  		return;
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
632
633
  	snd_printdd("atiixp-modem: XRUN detected (DMA %d)
  ", dma->ops->type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
638
639
  	snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  }
  
  /*
   * the period ack.  update the substream.
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
640
641
  static void snd_atiixp_update_dma(struct atiixp_modem *chip,
  				  struct atiixp_dma *dma)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
644
645
646
647
648
649
  {
  	if (! dma->substream || ! dma->running)
  		return;
  	snd_pcm_period_elapsed(dma->substream);
  }
  
  /* set BUS_BUSY interrupt bit if any DMA is running */
  /* call with spinlock held */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
650
  static void snd_atiixp_check_bus_busy(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
652
653
654
655
656
657
658
659
660
661
662
663
  {
  	unsigned int bus_busy;
  	if (atiixp_read(chip, CMD) & (ATI_REG_CMD_MODEM_SEND1_EN |
  				      ATI_REG_CMD_MODEM_RECEIVE_EN))
  		bus_busy = ATI_REG_IER_MODEM_SET_BUS_BUSY;
  	else
  		bus_busy = 0;
  	atiixp_update(chip, IER, ATI_REG_IER_MODEM_SET_BUS_BUSY, bus_busy);
  }
  
  /* common trigger callback
   * calling the lowlevel callbacks in it
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
664
  static int snd_atiixp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
666
667
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  	struct atiixp_dma *dma = substream->runtime->private_data;
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
668
  	int err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
670
  
  	snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
  	spin_lock(&chip->reg_lock);
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
672
673
  	switch(cmd) {
  	case SNDRV_PCM_TRIGGER_START:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
675
  		dma->ops->enable_transfer(chip, 1);
  		dma->running = 1;
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
676
677
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
  		dma->ops->enable_transfer(chip, 0);
  		dma->running = 0;
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
680
681
682
683
  		break;
  	default:
  		err = -EINVAL;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
  	}
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
685
  	if (! err) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
687
688
689
690
  	snd_atiixp_check_bus_busy(chip);
  	if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  		dma->ops->flush_dma(chip);
  		snd_atiixp_check_bus_busy(chip);
  	}
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
691
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
  	spin_unlock(&chip->reg_lock);
83a5b72ad   Sasha Khapyorsky   [ALSA] PCI modem ...
693
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
695
696
697
698
699
700
701
702
703
  }
  
  
  /*
   * lowlevel callbacks for each DMA type
   *
   * every callback is supposed to be called in chip->reg_lock spinlock
   */
  
  /* flush FIFO of analog OUT DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
704
  static void atiixp_out_flush_dma(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705
706
707
708
709
  {
  	atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_OUT1_FLUSH);
  }
  
  /* enable/disable analog OUT DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
710
  static void atiixp_out_enable_dma(struct atiixp_modem *chip, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  {
  	unsigned int data;
  	data = atiixp_read(chip, CMD);
  	if (on) {
  		if (data & ATI_REG_CMD_MODEM_OUT_DMA1_EN)
  			return;
  		atiixp_out_flush_dma(chip);
  		data |= ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  	} else
  		data &= ~ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  	atiixp_write(chip, CMD, data);
  }
  
  /* start/stop transfer over OUT DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
725
  static void atiixp_out_enable_transfer(struct atiixp_modem *chip, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
730
731
  {
  	atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_SEND1_EN,
  		      on ? ATI_REG_CMD_MODEM_SEND1_EN : 0);
  }
  
  /* enable/disable analog IN DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
732
  static void atiixp_in_enable_dma(struct atiixp_modem *chip, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733
734
735
736
737
738
  {
  	atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_IN_DMA_EN,
  		      on ? ATI_REG_CMD_MODEM_IN_DMA_EN : 0);
  }
  
  /* start/stop analog IN DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
739
  static void atiixp_in_enable_transfer(struct atiixp_modem *chip, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
742
743
744
745
746
747
748
749
750
751
  {
  	if (on) {
  		unsigned int data = atiixp_read(chip, CMD);
  		if (! (data & ATI_REG_CMD_MODEM_RECEIVE_EN)) {
  			data |= ATI_REG_CMD_MODEM_RECEIVE_EN;
  			atiixp_write(chip, CMD, data);
  		}
  	} else
  		atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_RECEIVE_EN, 0);
  }
  
  /* flush FIFO of analog IN DMA */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
752
  static void atiixp_in_flush_dma(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
  {
  	atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_IN_FLUSH);
  }
  
  /* set up slots and formats for analog OUT */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
758
  static int snd_atiixp_playback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
760
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
767
768
769
770
771
772
773
  	unsigned int data;
  
  	spin_lock_irq(&chip->reg_lock);
  	/* set output threshold */
  	data = atiixp_read(chip, MODEM_OUT_FIFO);
  	data &= ~ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK;
  	data |= 0x04 << ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT;
  	atiixp_write(chip, MODEM_OUT_FIFO, data);
  	spin_unlock_irq(&chip->reg_lock);
  	return 0;
  }
  
  /* set up slots and formats for analog IN */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
774
  static int snd_atiixp_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
776
777
778
779
780
781
  {
  	return 0;
  }
  
  /*
   * hw_params - allocate the buffer and set up buffer descriptors
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
782
783
  static int snd_atiixp_pcm_hw_params(struct snd_pcm_substream *substream,
  				   struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
785
786
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  	struct atiixp_dma *dma = substream->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
  	int err;
  	int i;
  
  	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  	if (err < 0)
  		return err;
  	dma->buf_addr = substream->runtime->dma_addr;
  	dma->buf_bytes = params_buffer_bytes(hw_params);
  
  	err = atiixp_build_dma_packets(chip, dma, substream,
  				       params_periods(hw_params),
  				       params_period_bytes(hw_params));
  	if (err < 0)
  		return err;
  
  	/* set up modem rate */
  	for (i = 0; i < NUM_ATI_CODECS; i++) {
  		if (! chip->ac97[i])
  			continue;
  		snd_ac97_write(chip->ac97[i], AC97_LINE1_RATE, params_rate(hw_params));
  		snd_ac97_write(chip->ac97[i], AC97_LINE1_LEVEL, 0);
  	}
  
  	return err;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
812
  static int snd_atiixp_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
814
815
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  	struct atiixp_dma *dma = substream->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
817
818
819
820
821
822
823
824
825
  
  	atiixp_clear_dma_packets(chip, dma, substream);
  	snd_pcm_lib_free_pages(substream);
  	return 0;
  }
  
  
  /*
   * pcm hardware definition, identical for all DMA types
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
826
  static struct snd_pcm_hardware snd_atiixp_pcm_hw =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
831
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID),
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
832
833
834
  	.rates =		(SNDRV_PCM_RATE_8000 |
  				 SNDRV_PCM_RATE_16000 |
  				 SNDRV_PCM_RATE_KNOT),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
836
837
838
839
840
841
842
843
844
  	.rate_min =		8000,
  	.rate_max =		16000,
  	.channels_min =		2,
  	.channels_max =		2,
  	.buffer_bytes_max =	256 * 1024,
  	.period_bytes_min =	32,
  	.period_bytes_max =	128 * 1024,
  	.periods_min =		2,
  	.periods_max =		ATI_MAX_DESCRIPTORS,
  };
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
845
846
  static int snd_atiixp_pcm_open(struct snd_pcm_substream *substream,
  			       struct atiixp_dma *dma, int pcm_type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
848
849
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
  	int err;
  	static unsigned int rates[] = { 8000,  9600, 12000, 16000 };
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
852
  	static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853
854
855
856
857
858
859
860
861
862
863
864
  		.count = ARRAY_SIZE(rates),
  		.list = rates,
  		.mask = 0,
  	};
  
  	snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  
  	if (dma->opened)
  		return -EBUSY;
  	dma->substream = substream;
  	runtime->hw = snd_atiixp_pcm_hw;
  	dma->ac97_pcm_type = pcm_type;
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
865
866
867
  	if ((err = snd_pcm_hw_constraint_list(runtime, 0,
  					      SNDRV_PCM_HW_PARAM_RATE,
  					      &hw_constraints_rates)) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
  		return err;
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
869
870
  	if ((err = snd_pcm_hw_constraint_integer(runtime,
  						 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
872
873
874
875
876
877
878
879
880
881
  		return err;
  	runtime->private_data = dma;
  
  	/* enable DMA bits */
  	spin_lock_irq(&chip->reg_lock);
  	dma->ops->enable_dma(chip, 1);
  	spin_unlock_irq(&chip->reg_lock);
  	dma->opened = 1;
  
  	return 0;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
882
883
  static int snd_atiixp_pcm_close(struct snd_pcm_substream *substream,
  				struct atiixp_dma *dma)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
885
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
887
888
889
890
891
892
893
894
895
896
897
  	/* disable DMA bits */
  	snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  	spin_lock_irq(&chip->reg_lock);
  	dma->ops->enable_dma(chip, 0);
  	spin_unlock_irq(&chip->reg_lock);
  	dma->substream = NULL;
  	dma->opened = 0;
  	return 0;
  }
  
  /*
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
898
  static int snd_atiixp_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
900
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
  	int err;
62932df8f   Ingo Molnar   [ALSA] semaphore ...
902
  	mutex_lock(&chip->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
  	err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
62932df8f   Ingo Molnar   [ALSA] semaphore ...
904
  	mutex_unlock(&chip->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
906
907
908
  	if (err < 0)
  		return err;
  	return 0;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
909
  static int snd_atiixp_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
911
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
  	int err;
62932df8f   Ingo Molnar   [ALSA] semaphore ...
913
  	mutex_lock(&chip->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
  	err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
62932df8f   Ingo Molnar   [ALSA] semaphore ...
915
  	mutex_unlock(&chip->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916
917
  	return err;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
918
  static int snd_atiixp_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
920
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921
922
  	return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
923
  static int snd_atiixp_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
925
  	struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
926
927
928
929
930
  	return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  }
  
  
  /* AC97 playback */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
931
  static struct snd_pcm_ops snd_atiixp_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932
933
934
935
936
937
938
939
940
941
942
  	.open =		snd_atiixp_playback_open,
  	.close =	snd_atiixp_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_atiixp_pcm_hw_params,
  	.hw_free =	snd_atiixp_pcm_hw_free,
  	.prepare =	snd_atiixp_playback_prepare,
  	.trigger =	snd_atiixp_pcm_trigger,
  	.pointer =	snd_atiixp_pcm_pointer,
  };
  
  /* AC97 capture */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
943
  static struct snd_pcm_ops snd_atiixp_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
945
946
947
948
949
950
951
952
  	.open =		snd_atiixp_capture_open,
  	.close =	snd_atiixp_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_atiixp_pcm_hw_params,
  	.hw_free =	snd_atiixp_pcm_hw_free,
  	.prepare =	snd_atiixp_capture_prepare,
  	.trigger =	snd_atiixp_pcm_trigger,
  	.pointer =	snd_atiixp_pcm_pointer,
  };
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
953
  static struct atiixp_dma_ops snd_atiixp_playback_dma_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
955
956
957
958
959
960
961
  	.type = ATI_DMA_PLAYBACK,
  	.llp_offset = ATI_REG_MODEM_OUT_DMA1_LINKPTR,
  	.dt_cur = ATI_REG_MODEM_OUT_DMA1_DT_CUR,
  	.enable_dma = atiixp_out_enable_dma,
  	.enable_transfer = atiixp_out_enable_transfer,
  	.flush_dma = atiixp_out_flush_dma,
  };
  	
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
962
  static struct atiixp_dma_ops snd_atiixp_capture_dma_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
963
964
965
966
967
968
969
  	.type = ATI_DMA_CAPTURE,
  	.llp_offset = ATI_REG_MODEM_IN_DMA_LINKPTR,
  	.dt_cur = ATI_REG_MODEM_IN_DMA_DT_CUR,
  	.enable_dma = atiixp_in_enable_dma,
  	.enable_transfer = atiixp_in_enable_transfer,
  	.flush_dma = atiixp_in_flush_dma,
  };
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
970
  static int __devinit snd_atiixp_pcm_new(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
972
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
973
974
975
976
977
978
979
980
981
982
983
984
  	int err;
  
  	/* initialize constants */
  	chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  	chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  
  	/* PCM #0: analog I/O */
  	err = snd_pcm_new(chip->card, "ATI IXP MC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  	if (err < 0)
  		return err;
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
6632d198c   Sasha Khapyorsky   [ALSA] dev_class=...
985
  	pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
987
988
989
990
  	pcm->private_data = chip;
  	strcpy(pcm->name, "ATI IXP MC97");
  	chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
991
992
  					      snd_dma_pci_data(chip->pci),
  					      64*1024, 128*1024);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
998
999
1000
1001
  
  	return 0;
  }
  
  
  
  /*
   * interrupt handler
   */
7d12e780e   David Howells   IRQ: Maintain reg...
1002
  static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1004
  	struct atiixp_modem *chip = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
  	unsigned int status;
  
  	status = atiixp_read(chip, ISR);
  
  	if (! status)
  		return IRQ_NONE;
  
  	/* process audio DMA */
  	if (status & ATI_REG_ISR_MODEM_OUT1_XRUN)
  		snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_PLAYBACK]);
  	else if (status & ATI_REG_ISR_MODEM_OUT1_STATUS)
  		snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  	if (status & ATI_REG_ISR_MODEM_IN_XRUN)
  		snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_CAPTURE]);
  	else if (status & ATI_REG_ISR_MODEM_IN_STATUS)
  		snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  
  	/* for codec detection */
  	if (status & CODEC_CHECK_BITS) {
  		unsigned int detected;
  		detected = status & CODEC_CHECK_BITS;
  		spin_lock(&chip->reg_lock);
  		chip->codec_not_ready_bits |= detected;
  		atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  		spin_unlock(&chip->reg_lock);
  	}
  
  	/* ack */
  	atiixp_write(chip, ISR, status);
  
  	return IRQ_HANDLED;
  }
  
  
  /*
   * ac97 mixer section
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1042
  static int __devinit snd_atiixp_mixer_new(struct atiixp_modem *chip, int clock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1043
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1044
1045
  	struct snd_ac97_bus *pbus;
  	struct snd_ac97_template ac97;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
  	int i, err;
  	int codec_count;
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1048
  	static struct snd_ac97_bus_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  		.write = snd_atiixp_ac97_write,
  		.read = snd_atiixp_ac97_read,
  	};
  	static unsigned int codec_skip[NUM_ATI_CODECS] = {
  		ATI_REG_ISR_CODEC0_NOT_READY,
  		ATI_REG_ISR_CODEC1_NOT_READY,
  		ATI_REG_ISR_CODEC2_NOT_READY,
  	};
  
  	if (snd_atiixp_codec_detect(chip) < 0)
  		return -ENXIO;
  
  	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  		return err;
  	pbus->clock = clock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  	chip->ac97_bus = pbus;
  
  	codec_count = 0;
  	for (i = 0; i < NUM_ATI_CODECS; i++) {
  		if (chip->codec_not_ready_bits & codec_skip[i])
  			continue;
  		memset(&ac97, 0, sizeof(ac97));
  		ac97.private_data = chip;
  		ac97.pci = chip->pci;
  		ac97.num = i;
f1a63a38d   Takashi Iwai   [ALSA] ac97 - Sup...
1074
  		ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
1076
  		if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  			chip->ac97[i] = NULL; /* to be sure */
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
1077
1078
  			snd_printdd("atiixp-modem: codec %d not available for modem
  ", i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
1080
1081
1082
1083
1084
  			continue;
  		}
  		codec_count++;
  	}
  
  	if (! codec_count) {
98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
1085
1086
  		snd_printk(KERN_ERR "atiixp-modem: no codec available
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
  		return -ENODEV;
  	}
  
  	/* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
  
  	return 0;
  }
  
  
  #ifdef CONFIG_PM
  /*
   * power management
   */
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1100
  static int snd_atiixp_suspend(struct pci_dev *pci, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1101
  {
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1102
1103
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct atiixp_modem *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
  	int i;
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1105
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
  	for (i = 0; i < NUM_ATI_PCMDEVS; i++)
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1107
  		snd_pcm_suspend_all(chip->pcmdevs[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1108
  	for (i = 0; i < NUM_ATI_CODECS; i++)
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1109
  		snd_ac97_suspend(chip->ac97[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1110
1111
  	snd_atiixp_aclink_down(chip);
  	snd_atiixp_chip_stop(chip);
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1112
1113
  	pci_disable_device(pci);
  	pci_save_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1114
  	pci_set_power_state(pci, pci_choose_state(pci, state));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
  	return 0;
  }
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1117
  static int snd_atiixp_resume(struct pci_dev *pci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1118
  {
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1119
1120
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct atiixp_modem *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1121
  	int i;
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1122
  	pci_set_power_state(pci, PCI_D0);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1123
1124
1125
1126
1127
1128
1129
1130
  	pci_restore_state(pci);
  	if (pci_enable_device(pci) < 0) {
  		printk(KERN_ERR "atiixp-modem: pci_enable_device failed, "
  		       "disabling device
  ");
  		snd_card_disconnect(card);
  		return -EIO;
  	}
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1131
  	pci_set_master(pci);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
1133
1134
1135
1136
  
  	snd_atiixp_aclink_reset(chip);
  	snd_atiixp_chip_start(chip);
  
  	for (i = 0; i < NUM_ATI_CODECS; i++)
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1137
  		snd_ac97_resume(chip->ac97[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138

92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1139
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
1141
1142
  	return 0;
  }
  #endif /* CONFIG_PM */
adf1b3d25   Takashi Iwai   [ALSA] Optimize f...
1143
  #ifdef CONFIG_PROC_FS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
1146
  /*
   * proc interface for register dump
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1147
1148
  static void snd_atiixp_proc_read(struct snd_info_entry *entry,
  				 struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1150
  	struct atiixp_modem *chip = entry->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1151
1152
1153
1154
1155
1156
  	int i;
  
  	for (i = 0; i < 256; i += 4)
  		snd_iprintf(buffer, "%02x: %08x
  ", i, readl(chip->remap_addr + i));
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1157
  static void __devinit snd_atiixp_proc_init(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1158
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1159
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1160

98b4f5929   Chuck Ebbert   [PATCH] atiixp_mo...
1161
  	if (! snd_card_proc_new(chip->card, "atiixp-modem", &entry))
bf850204a   Takashi Iwai   [ALSA] Remove unn...
1162
  		snd_info_set_text_ops(entry, chip, snd_atiixp_proc_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1163
  }
adf1b3d25   Takashi Iwai   [ALSA] Optimize f...
1164
1165
1166
  #else
  #define snd_atiixp_proc_init(chip)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1167
1168
1169
1170
1171
  
  
  /*
   * destructor
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1172
  static int snd_atiixp_free(struct atiixp_modem *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1173
1174
1175
1176
1177
1178
1179
  {
  	if (chip->irq < 0)
  		goto __hw_end;
  	snd_atiixp_chip_stop(chip);
  	synchronize_irq(chip->irq);
        __hw_end:
  	if (chip->irq >= 0)
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1180
  		free_irq(chip->irq, chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181
1182
1183
1184
1185
1186
1187
  	if (chip->remap_addr)
  		iounmap(chip->remap_addr);
  	pci_release_regions(chip->pci);
  	pci_disable_device(chip->pci);
  	kfree(chip);
  	return 0;
  }
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1188
  static int snd_atiixp_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1190
  	struct atiixp_modem *chip = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1191
1192
1193
1194
1195
1196
  	return snd_atiixp_free(chip);
  }
  
  /*
   * constructor for chip instance
   */
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1197
1198
1199
  static int __devinit snd_atiixp_create(struct snd_card *card,
  				       struct pci_dev *pci,
  				       struct atiixp_modem **r_chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1200
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1201
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1202
1203
  		.dev_free =	snd_atiixp_dev_free,
  	};
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1204
  	struct atiixp_modem *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1205
1206
1207
1208
  	int err;
  
  	if ((err = pci_enable_device(pci)) < 0)
  		return err;
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
1209
  	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1210
1211
1212
1213
1214
1215
  	if (chip == NULL) {
  		pci_disable_device(pci);
  		return -ENOMEM;
  	}
  
  	spin_lock_init(&chip->reg_lock);
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1216
  	mutex_init(&chip->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
  	chip->card = card;
  	chip->pci = pci;
  	chip->irq = -1;
  	if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  		kfree(chip);
  		pci_disable_device(pci);
  		return err;
  	}
  	chip->addr = pci_resource_start(pci, 0);
  	chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  	if (chip->remap_addr == NULL) {
  		snd_printk(KERN_ERR "AC'97 space ioremap problem
  ");
  		snd_atiixp_free(chip);
  		return -EIO;
  	}
437a5a460   Takashi Iwai   [ALSA] Remove IRQ...
1233
  	if (request_irq(pci->irq, snd_atiixp_interrupt, IRQF_SHARED,
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1234
  			card->shortname, chip)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
  		snd_printk(KERN_ERR "unable to grab IRQ %d
  ", pci->irq);
  		snd_atiixp_free(chip);
  		return -EBUSY;
  	}
  	chip->irq = pci->irq;
  	pci_set_master(pci);
  	synchronize_irq(chip->irq);
  
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  		snd_atiixp_free(chip);
  		return err;
  	}
  
  	snd_card_set_dev(card, &pci->dev);
  
  	*r_chip = chip;
  	return 0;
  }
  
  
  static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  				      const struct pci_device_id *pci_id)
  {
74ee4ff1e   Takashi Iwai   [ALSA] Remove xxx...
1259
1260
  	struct snd_card *card;
  	struct atiixp_modem *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1261
1262
  	unsigned char revision;
  	int err;
b7fe46220   Clemens Ladisch   [ALSA] highlander...
1263
  	card = snd_card_new(index, id, THIS_MODULE, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
1266
1267
1268
1269
1270
1271
1272
  	if (card == NULL)
  		return -ENOMEM;
  
  	pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  
  	strcpy(card->driver, "ATIIXP-MODEM");
  	strcpy(card->shortname, "ATI IXP Modem");
  	if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  		goto __error;
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1273
  	card->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1274
1275
1276
  
  	if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  		goto __error;
b7fe46220   Clemens Ladisch   [ALSA] highlander...
1277
  	if ((err = snd_atiixp_mixer_new(chip, ac97_clock)) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  		goto __error;
  
  	if ((err = snd_atiixp_pcm_new(chip)) < 0)
  		goto __error;
  	
  	snd_atiixp_proc_init(chip);
  
  	snd_atiixp_chip_start(chip);
  
  	sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  		card->shortname, revision, chip->addr, chip->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1289
1290
1291
1292
  	if ((err = snd_card_register(card)) < 0)
  		goto __error;
  
  	pci_set_drvdata(pci, card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
  	return 0;
  
   __error:
  	snd_card_free(card);
  	return err;
  }
  
  static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  {
  	snd_card_free(pci_get_drvdata(pci));
  	pci_set_drvdata(pci, NULL);
  }
  
  static struct pci_driver driver = {
  	.name = "ATI IXP MC97 controller",
  	.id_table = snd_atiixp_ids,
  	.probe = snd_atiixp_probe,
  	.remove = __devexit_p(snd_atiixp_remove),
92304cc7e   Takashi Iwai   [ALSA] atiixp - F...
1311
1312
1313
1314
  #ifdef CONFIG_PM
  	.suspend = snd_atiixp_suspend,
  	.resume = snd_atiixp_resume,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
1316
1317
1318
1319
  };
  
  
  static int __init alsa_card_atiixp_init(void)
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
1320
  	return pci_register_driver(&driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321
1322
1323
1324
1325
1326
1327
1328
1329
  }
  
  static void __exit alsa_card_atiixp_exit(void)
  {
  	pci_unregister_driver(&driver);
  }
  
  module_init(alsa_card_atiixp_init)
  module_exit(alsa_card_atiixp_exit)