1/*
2 *   ALSA modem driver for VIA VT82xx (South Bridge)
3 *
4 *   VT82C686A/B/C, VT8233A/C, VT8235
5 *
6 *	Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
7 *	                   Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8 *                    2002 Takashi Iwai <tiwai@suse.de>
9 *
10 *   This program is free software; you can redistribute it and/or modify
11 *   it under the terms of the GNU General Public License as published by
12 *   the Free Software Foundation; either version 2 of the License, or
13 *   (at your option) any later version.
14 *
15 *   This program is distributed in the hope that it will be useful,
16 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *   GNU General Public License for more details.
19 *
20 *   You should have received a copy of the GNU General Public License
21 *   along with this program; if not, write to the Free Software
22 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23 *
24 */
25
26/*
27 * Changes:
28 *
29 * Sep. 2,  2004  Sasha Khapyorsky <sashak@alsa-project.org>
30 *      Modified from original audio driver 'via82xx.c' to support AC97
31 *      modems.
32 */
33
34#include <sound/driver.h>
35#include <asm/io.h>
36#include <linux/delay.h>
37#include <linux/interrupt.h>
38#include <linux/init.h>
39#include <linux/pci.h>
40#include <linux/slab.h>
41#include <linux/moduleparam.h>
42#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/info.h>
46#include <sound/ac97_codec.h>
47#include <sound/initval.h>
48
49
50MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
51MODULE_DESCRIPTION("VIA VT82xx modem");
52MODULE_LICENSE("GPL");
53MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C modem,pci}}");
54
55static int index = -2; /* Exclude the first card */
56static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
57static int ac97_clock = 48000;
58
59module_param(index, int, 0444);
60MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
61module_param(id, charp, 0444);
62MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
63module_param(ac97_clock, int, 0444);
64MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
65
66/* just for backward compatibility */
67static int enable;
68module_param(enable, bool, 0444);
69
70
71/*
72 *  Direct registers
73 */
74
75#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
76#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
77
78/* common offsets */
79#define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
80#define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
81#define   VIA_REG_STAT_PAUSED		0x40	/* RO */
82#define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
83#define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
84#define   VIA_REG_STAT_EOL		0x02	/* RWC */
85#define   VIA_REG_STAT_FLAG		0x01	/* RWC */
86#define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
87#define   VIA_REG_CTRL_START		0x80	/* WO */
88#define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
89#define   VIA_REG_CTRL_AUTOSTART	0x20
90#define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
91#define   VIA_REG_CTRL_INT_STOP		0x04
92#define   VIA_REG_CTRL_INT_EOL		0x02
93#define   VIA_REG_CTRL_INT_FLAG		0x01
94#define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
95#define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
96#define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
97#define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
98#define   VIA_REG_TYPE_16BIT		0x20	/* RW */
99#define   VIA_REG_TYPE_STEREO		0x10	/* RW */
100#define   VIA_REG_TYPE_INT_LLINE	0x00
101#define   VIA_REG_TYPE_INT_LSAMPLE	0x04
102#define   VIA_REG_TYPE_INT_LESSONE	0x08
103#define   VIA_REG_TYPE_INT_MASK		0x0c
104#define   VIA_REG_TYPE_INT_EOL		0x02
105#define   VIA_REG_TYPE_INT_FLAG		0x01
106#define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
107#define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
108#define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
109#define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
110#define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
111
112#define DEFINE_VIA_REGSET(name,val) \
113enum {\
114	VIA_REG_##name##_STATUS		= (val),\
115	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
116	VIA_REG_##name##_TYPE		= (val) + 0x02,\
117	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
118	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
119	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
120	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
121}
122
123/* modem block */
124DEFINE_VIA_REGSET(MO, 0x40);
125DEFINE_VIA_REGSET(MI, 0x50);
126
127/* AC'97 */
128#define VIA_REG_AC97			0x80	/* dword */
129#define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
130#define   VIA_REG_AC97_CODEC_ID_SHIFT	30
131#define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
132#define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
133#define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
134#define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
135#define   VIA_REG_AC97_BUSY		(1<<24)
136#define   VIA_REG_AC97_READ		(1<<23)
137#define   VIA_REG_AC97_CMD_SHIFT	16
138#define   VIA_REG_AC97_CMD_MASK		0x7e
139#define   VIA_REG_AC97_DATA_SHIFT	0
140#define   VIA_REG_AC97_DATA_MASK	0xffff
141
142#define VIA_REG_SGD_SHADOW		0x84	/* dword */
143#define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
144#define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
145#define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
146#define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
147#define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
148#define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
149#define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
150#define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
151#define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
152#define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
153#define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
154#define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
155#define   VIA_REG_SGD_STAT_MR_FLAG      (1<<16)
156#define   VIA_REG_SGD_STAT_MW_FLAG      (1<<17)
157#define   VIA_REG_SGD_STAT_MR_EOL       (1<<20)
158#define   VIA_REG_SGD_STAT_MW_EOL       (1<<21)
159#define   VIA_REG_SGD_STAT_MR_STOP      (1<<24)
160#define   VIA_REG_SGD_STAT_MW_STOP      (1<<25)
161#define   VIA_REG_SGD_STAT_MR_ACTIVE    (1<<28)
162#define   VIA_REG_SGD_STAT_MW_ACTIVE    (1<<29)
163
164#define VIA_REG_GPI_STATUS		0x88
165#define VIA_REG_GPI_INTR		0x8c
166
167#define VIA_TBL_BIT_FLAG	0x40000000
168#define VIA_TBL_BIT_EOL		0x80000000
169
170/* pci space */
171#define VIA_ACLINK_STAT		0x40
172#define  VIA_ACLINK_C11_READY	0x20
173#define  VIA_ACLINK_C10_READY	0x10
174#define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
175#define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
176#define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
177#define VIA_ACLINK_CTRL		0x41
178#define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
179#define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
180#define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
181#define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
182#define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
183#define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
184#define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
185#define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
186#define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
187				 VIA_ACLINK_CTRL_RESET|\
188				 VIA_ACLINK_CTRL_PCM)
189#define VIA_FUNC_ENABLE		0x42
190#define  VIA_FUNC_MIDI_PNP	0x80
191#define  VIA_FUNC_MIDI_IRQMASK	0x40
192#define  VIA_FUNC_RX2C_WRITE	0x20
193#define  VIA_FUNC_SB_FIFO_EMPTY	0x10
194#define  VIA_FUNC_ENABLE_GAME	0x08
195#define  VIA_FUNC_ENABLE_FM	0x04
196#define  VIA_FUNC_ENABLE_MIDI	0x02
197#define  VIA_FUNC_ENABLE_SB	0x01
198#define VIA_PNP_CONTROL		0x43
199#define VIA_MC97_CTRL		0x44
200#define  VIA_MC97_CTRL_ENABLE   0x80
201#define  VIA_MC97_CTRL_SECONDARY 0x40
202#define  VIA_MC97_CTRL_INIT     (VIA_MC97_CTRL_ENABLE|\
203                                 VIA_MC97_CTRL_SECONDARY)
204
205
206/*
207 * pcm stream
208 */
209
210struct snd_via_sg_table {
211	unsigned int offset;
212	unsigned int size;
213} ;
214
215#define VIA_TABLE_SIZE	255
216
217struct viadev {
218	unsigned int reg_offset;
219	unsigned long port;
220	int direction;	/* playback = 0, capture = 1 */
221        struct snd_pcm_substream *substream;
222	int running;
223	unsigned int tbl_entries; /* # descriptors */
224	struct snd_dma_buffer table;
225	struct snd_via_sg_table *idx_table;
226	/* for recovery from the unexpected pointer */
227	unsigned int lastpos;
228	unsigned int bufsize;
229	unsigned int bufsize2;
230};
231
232enum { TYPE_CARD_VIA82XX_MODEM = 1 };
233
234#define VIA_MAX_MODEM_DEVS	2
235
236struct via82xx_modem {
237	int irq;
238
239	unsigned long port;
240
241	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
242
243	struct pci_dev *pci;
244	struct snd_card *card;
245
246	unsigned int num_devs;
247	unsigned int playback_devno, capture_devno;
248	struct viadev devs[VIA_MAX_MODEM_DEVS];
249
250	struct snd_pcm *pcms[2];
251
252	struct snd_ac97_bus *ac97_bus;
253	struct snd_ac97 *ac97;
254	unsigned int ac97_clock;
255	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
256
257	spinlock_t reg_lock;
258	struct snd_info_entry *proc_entry;
259};
260
261static struct pci_device_id snd_via82xx_modem_ids[] = {
262	{ 0x1106, 0x3068, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA82XX_MODEM, },
263	{ 0, }
264};
265
266MODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
267
268/*
269 */
270
271/*
272 * allocate and initialize the descriptor buffers
273 * periods = number of periods
274 * fragsize = period size in bytes
275 */
276static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
277			   struct pci_dev *pci,
278			   unsigned int periods, unsigned int fragsize)
279{
280	unsigned int i, idx, ofs, rest;
281	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
282	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
283
284	if (dev->table.area == NULL) {
285		/* the start of each lists must be aligned to 8 bytes,
286		 * but the kernel pages are much bigger, so we don't care
287		 */
288		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
289					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
290					&dev->table) < 0)
291			return -ENOMEM;
292	}
293	if (! dev->idx_table) {
294		dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
295		if (! dev->idx_table)
296			return -ENOMEM;
297	}
298
299	/* fill the entries */
300	idx = 0;
301	ofs = 0;
302	for (i = 0; i < periods; i++) {
303		rest = fragsize;
304		/* fill descriptors for a period.
305		 * a period can be split to several descriptors if it's
306		 * over page boundary.
307		 */
308		do {
309			unsigned int r;
310			unsigned int flag;
311
312			if (idx >= VIA_TABLE_SIZE) {
313				snd_printk(KERN_ERR "via82xx: too much table size!\n");
314				return -EINVAL;
315			}
316			((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
317			r = PAGE_SIZE - (ofs % PAGE_SIZE);
318			if (rest < r)
319				r = rest;
320			rest -= r;
321			if (! rest) {
322				if (i == periods - 1)
323					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
324				else
325					flag = VIA_TBL_BIT_FLAG; /* period boundary */
326			} else
327				flag = 0; /* period continues to the next */
328			// printk("via: tbl %d: at %d  size %d (rest %d)\n", idx, ofs, r, rest);
329			((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
330			dev->idx_table[idx].offset = ofs;
331			dev->idx_table[idx].size = r;
332			ofs += r;
333			idx++;
334		} while (rest > 0);
335	}
336	dev->tbl_entries = idx;
337	dev->bufsize = periods * fragsize;
338	dev->bufsize2 = dev->bufsize / 2;
339	return 0;
340}
341
342
343static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
344			   struct pci_dev *pci)
345{
346	if (dev->table.area) {
347		snd_dma_free_pages(&dev->table);
348		dev->table.area = NULL;
349	}
350	kfree(dev->idx_table);
351	dev->idx_table = NULL;
352	return 0;
353}
354
355/*
356 *  Basic I/O
357 */
358
359static inline unsigned int snd_via82xx_codec_xread(struct via82xx_modem *chip)
360{
361	return inl(VIAREG(chip, AC97));
362}
363
364static inline void snd_via82xx_codec_xwrite(struct via82xx_modem *chip, unsigned int val)
365{
366	outl(val, VIAREG(chip, AC97));
367}
368
369static int snd_via82xx_codec_ready(struct via82xx_modem *chip, int secondary)
370{
371	unsigned int timeout = 1000;	/* 1ms */
372	unsigned int val;
373
374	while (timeout-- > 0) {
375		udelay(1);
376		if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
377			return val & 0xffff;
378	}
379	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
380		   secondary, snd_via82xx_codec_xread(chip));
381	return -EIO;
382}
383
384static int snd_via82xx_codec_valid(struct via82xx_modem *chip, int secondary)
385{
386	unsigned int timeout = 1000;	/* 1ms */
387	unsigned int val, val1;
388	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
389					 VIA_REG_AC97_SECONDARY_VALID;
390
391	while (timeout-- > 0) {
392		val = snd_via82xx_codec_xread(chip);
393		val1 = val & (VIA_REG_AC97_BUSY | stat);
394		if (val1 == stat)
395			return val & 0xffff;
396		udelay(1);
397	}
398	return -EIO;
399}
400
401static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
402{
403	struct via82xx_modem *chip = ac97->private_data;
404	int err;
405	err = snd_via82xx_codec_ready(chip, ac97->num);
406	/* here we need to wait fairly for long time.. */
407	msleep(500);
408}
409
410static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
411				    unsigned short reg,
412				    unsigned short val)
413{
414	struct via82xx_modem *chip = ac97->private_data;
415	unsigned int xval;
416	if(reg == AC97_GPIO_STATUS) {
417		outl(val, VIAREG(chip, GPI_STATUS));
418		return;
419	}
420	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
421	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
422	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
423	xval |= val << VIA_REG_AC97_DATA_SHIFT;
424	snd_via82xx_codec_xwrite(chip, xval);
425	snd_via82xx_codec_ready(chip, ac97->num);
426}
427
428static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
429{
430	struct via82xx_modem *chip = ac97->private_data;
431	unsigned int xval, val = 0xffff;
432	int again = 0;
433
434	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
435	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
436	xval |= VIA_REG_AC97_READ;
437	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
438      	while (1) {
439      		if (again++ > 3) {
440			snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
441				   ac97->num, snd_via82xx_codec_xread(chip));
442		      	return 0xffff;
443		}
444		snd_via82xx_codec_xwrite(chip, xval);
445		udelay (20);
446		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
447			udelay(25);
448			val = snd_via82xx_codec_xread(chip);
449			break;
450		}
451	}
452	return val & 0xffff;
453}
454
455static void snd_via82xx_channel_reset(struct via82xx_modem *chip, struct viadev *viadev)
456{
457	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
458	     VIADEV_REG(viadev, OFFSET_CONTROL));
459	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
460	udelay(50);
461	/* disable interrupts */
462	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
463	/* clear interrupts */
464	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
465	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
466	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
467	viadev->lastpos = 0;
468}
469
470
471/*
472 *  Interrupt handler
473 */
474
475static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id)
476{
477	struct via82xx_modem *chip = dev_id;
478	unsigned int status;
479	unsigned int i;
480
481	status = inl(VIAREG(chip, SGD_SHADOW));
482	if (! (status & chip->intr_mask)) {
483		return IRQ_NONE;
484	}
485// _skip_sgd:
486
487	/* check status for each stream */
488	spin_lock(&chip->reg_lock);
489	for (i = 0; i < chip->num_devs; i++) {
490		struct viadev *viadev = &chip->devs[i];
491		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
492		c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
493		if (! c_status)
494			continue;
495		if (viadev->substream && viadev->running) {
496			spin_unlock(&chip->reg_lock);
497			snd_pcm_period_elapsed(viadev->substream);
498			spin_lock(&chip->reg_lock);
499		}
500		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
501	}
502	spin_unlock(&chip->reg_lock);
503	return IRQ_HANDLED;
504}
505
506/*
507 *  PCM callbacks
508 */
509
510/*
511 * trigger callback
512 */
513static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
514{
515	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
516	struct viadev *viadev = substream->runtime->private_data;
517	unsigned char val = 0;
518
519	switch (cmd) {
520	case SNDRV_PCM_TRIGGER_START:
521	case SNDRV_PCM_TRIGGER_SUSPEND:
522		val |= VIA_REG_CTRL_START;
523		viadev->running = 1;
524		break;
525	case SNDRV_PCM_TRIGGER_STOP:
526		val = VIA_REG_CTRL_TERMINATE;
527		viadev->running = 0;
528		break;
529	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
530		val |= VIA_REG_CTRL_PAUSE;
531		viadev->running = 0;
532		break;
533	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
534		viadev->running = 1;
535		break;
536	default:
537		return -EINVAL;
538	}
539	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
540	if (cmd == SNDRV_PCM_TRIGGER_STOP)
541		snd_via82xx_channel_reset(chip, viadev);
542	return 0;
543}
544
545/*
546 * pointer callbacks
547 */
548
549/*
550 * calculate the linear position at the given sg-buffer index and the rest count
551 */
552
553#define check_invalid_pos(viadev,pos) \
554	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
555				     viadev->lastpos < viadev->bufsize2))
556
557static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
558					   unsigned int count)
559{
560	unsigned int size, res;
561
562	size = viadev->idx_table[idx].size;
563	res = viadev->idx_table[idx].offset + size - count;
564
565	/* check the validity of the calculated position */
566	if (size < count) {
567		snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
568			   (int)size, (int)count);
569		res = viadev->lastpos;
570	} else if (check_invalid_pos(viadev, res)) {
571#ifdef POINTER_DEBUG
572		printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
573		       "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
574		       "count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos,
575		       viadev->bufsize2, viadev->idx_table[idx].offset,
576		       viadev->idx_table[idx].size, count);
577#endif
578		if (count && size < count) {
579			snd_printd(KERN_ERR "invalid via82xx_cur_ptr, "
580				   "using last valid pointer\n");
581			res = viadev->lastpos;
582		} else {
583			if (! count)
584				/* bogus count 0 on the DMA boundary? */
585				res = viadev->idx_table[idx].offset;
586			else
587				/* count register returns full size
588				 * when end of buffer is reached
589				 */
590				res = viadev->idx_table[idx].offset + size;
591			if (check_invalid_pos(viadev, res)) {
592				snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
593					   "using last valid pointer\n");
594				res = viadev->lastpos;
595			}
596		}
597	}
598	viadev->lastpos = res; /* remember the last position */
599	if (res >= viadev->bufsize)
600		res -= viadev->bufsize;
601	return res;
602}
603
604/*
605 * get the current pointer on via686
606 */
607static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
608{
609	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
610	struct viadev *viadev = substream->runtime->private_data;
611	unsigned int idx, ptr, count, res;
612
613	snd_assert(viadev->tbl_entries, return 0);
614	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
615		return 0;
616
617	spin_lock(&chip->reg_lock);
618	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
619	/* The via686a does not have the current index register,
620	 * so we need to calculate the index from CURR_PTR.
621	 */
622	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
623	if (ptr <= (unsigned int)viadev->table.addr)
624		idx = 0;
625	else /* CURR_PTR holds the address + 8 */
626		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) %
627			viadev->tbl_entries;
628	res = calc_linear_pos(viadev, idx, count);
629	spin_unlock(&chip->reg_lock);
630
631	return bytes_to_frames(substream->runtime, res);
632}
633
634/*
635 * hw_params callback:
636 * allocate the buffer and build up the buffer description table
637 */
638static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
639				 struct snd_pcm_hw_params *hw_params)
640{
641	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
642	struct viadev *viadev = substream->runtime->private_data;
643	int err;
644
645	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
646	if (err < 0)
647		return err;
648	err = build_via_table(viadev, substream, chip->pci,
649			      params_periods(hw_params),
650			      params_period_bytes(hw_params));
651	if (err < 0)
652		return err;
653
654	snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
655	snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
656
657	return 0;
658}
659
660/*
661 * hw_free callback:
662 * clean up the buffer description table and release the buffer
663 */
664static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
665{
666	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
667	struct viadev *viadev = substream->runtime->private_data;
668
669	clean_via_table(viadev, substream, chip->pci);
670	snd_pcm_lib_free_pages(substream);
671	return 0;
672}
673
674
675/*
676 * set up the table pointer
677 */
678static void snd_via82xx_set_table_ptr(struct via82xx_modem *chip, struct viadev *viadev)
679{
680	snd_via82xx_codec_ready(chip, chip->ac97_secondary);
681	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
682	udelay(20);
683	snd_via82xx_codec_ready(chip, chip->ac97_secondary);
684}
685
686/*
687 * prepare callback for playback and capture
688 */
689static int snd_via82xx_pcm_prepare(struct snd_pcm_substream *substream)
690{
691	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
692	struct viadev *viadev = substream->runtime->private_data;
693
694	snd_via82xx_channel_reset(chip, viadev);
695	/* this must be set after channel_reset */
696	snd_via82xx_set_table_ptr(chip, viadev);
697	outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
698	     VIADEV_REG(viadev, OFFSET_TYPE));
699	return 0;
700}
701
702/*
703 * pcm hardware definition, identical for both playback and capture
704 */
705static struct snd_pcm_hardware snd_via82xx_hw =
706{
707	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
708				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
709				 SNDRV_PCM_INFO_MMAP_VALID |
710				 /* SNDRV_PCM_INFO_RESUME | */
711				 SNDRV_PCM_INFO_PAUSE),
712	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
713	.rates =		SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
714	.rate_min =		8000,
715	.rate_max =		16000,
716	.channels_min =		1,
717	.channels_max =		1,
718	.buffer_bytes_max =	128 * 1024,
719	.period_bytes_min =	32,
720	.period_bytes_max =	128 * 1024,
721	.periods_min =		2,
722	.periods_max =		VIA_TABLE_SIZE / 2,
723	.fifo_size =		0,
724};
725
726
727/*
728 * open callback skeleton
729 */
730static int snd_via82xx_modem_pcm_open(struct via82xx_modem *chip, struct viadev *viadev,
731				      struct snd_pcm_substream *substream)
732{
733	struct snd_pcm_runtime *runtime = substream->runtime;
734	int err;
735        static unsigned int rates[] = { 8000,  9600, 12000, 16000 };
736        static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
737                .count = ARRAY_SIZE(rates),
738                .list = rates,
739                .mask = 0,
740        };
741
742	runtime->hw = snd_via82xx_hw;
743
744        if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
745					      &hw_constraints_rates)) < 0)
746                return err;
747
748	/* we may remove following constaint when we modify table entries
749	   in interrupt */
750	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
751		return err;
752
753	runtime->private_data = viadev;
754	viadev->substream = substream;
755
756	return 0;
757}
758
759
760/*
761 * open callback for playback
762 */
763static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
764{
765	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
766	struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
767
768	return snd_via82xx_modem_pcm_open(chip, viadev, substream);
769}
770
771/*
772 * open callback for capture
773 */
774static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
775{
776	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
777	struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
778
779	return snd_via82xx_modem_pcm_open(chip, viadev, substream);
780}
781
782/*
783 * close callback
784 */
785static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
786{
787	struct viadev *viadev = substream->runtime->private_data;
788
789	viadev->substream = NULL;
790	return 0;
791}
792
793
794/* via686 playback callbacks */
795static struct snd_pcm_ops snd_via686_playback_ops = {
796	.open =		snd_via82xx_playback_open,
797	.close =	snd_via82xx_pcm_close,
798	.ioctl =	snd_pcm_lib_ioctl,
799	.hw_params =	snd_via82xx_hw_params,
800	.hw_free =	snd_via82xx_hw_free,
801	.prepare =	snd_via82xx_pcm_prepare,
802	.trigger =	snd_via82xx_pcm_trigger,
803	.pointer =	snd_via686_pcm_pointer,
804	.page =		snd_pcm_sgbuf_ops_page,
805};
806
807/* via686 capture callbacks */
808static struct snd_pcm_ops snd_via686_capture_ops = {
809	.open =		snd_via82xx_capture_open,
810	.close =	snd_via82xx_pcm_close,
811	.ioctl =	snd_pcm_lib_ioctl,
812	.hw_params =	snd_via82xx_hw_params,
813	.hw_free =	snd_via82xx_hw_free,
814	.prepare =	snd_via82xx_pcm_prepare,
815	.trigger =	snd_via82xx_pcm_trigger,
816	.pointer =	snd_via686_pcm_pointer,
817	.page =		snd_pcm_sgbuf_ops_page,
818};
819
820
821static void init_viadev(struct via82xx_modem *chip, int idx, unsigned int reg_offset,
822			int direction)
823{
824	chip->devs[idx].reg_offset = reg_offset;
825	chip->devs[idx].direction = direction;
826	chip->devs[idx].port = chip->port + reg_offset;
827}
828
829/*
830 * create a pcm instance for via686a/b
831 */
832static int __devinit snd_via686_pcm_new(struct via82xx_modem *chip)
833{
834	struct snd_pcm *pcm;
835	int err;
836
837	chip->playback_devno = 0;
838	chip->capture_devno = 1;
839	chip->num_devs = 2;
840	chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
841
842	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
843	if (err < 0)
844		return err;
845	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
846	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
847	pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
848	pcm->private_data = chip;
849	strcpy(pcm->name, chip->card->shortname);
850	chip->pcms[0] = pcm;
851	init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
852	init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
853
854	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
855							 snd_dma_pci_data(chip->pci),
856							 64*1024, 128*1024)) < 0)
857		return err;
858
859	return 0;
860}
861
862
863/*
864 *  Mixer part
865 */
866
867
868static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
869{
870	struct via82xx_modem *chip = bus->private_data;
871	chip->ac97_bus = NULL;
872}
873
874static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
875{
876	struct via82xx_modem *chip = ac97->private_data;
877	chip->ac97 = NULL;
878}
879
880
881static int __devinit snd_via82xx_mixer_new(struct via82xx_modem *chip)
882{
883	struct snd_ac97_template ac97;
884	int err;
885	static struct snd_ac97_bus_ops ops = {
886		.write = snd_via82xx_codec_write,
887		.read = snd_via82xx_codec_read,
888		.wait = snd_via82xx_codec_wait,
889	};
890
891	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
892		return err;
893	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
894	chip->ac97_bus->clock = chip->ac97_clock;
895
896	memset(&ac97, 0, sizeof(ac97));
897	ac97.private_data = chip;
898	ac97.private_free = snd_via82xx_mixer_free_ac97;
899	ac97.pci = chip->pci;
900	ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
901	ac97.num = chip->ac97_secondary;
902
903	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
904		return err;
905
906	return 0;
907}
908
909
910/*
911 * proc interface
912 */
913static void snd_via82xx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
914{
915	struct via82xx_modem *chip = entry->private_data;
916	int i;
917
918	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
919	for (i = 0; i < 0xa0; i += 4) {
920		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
921	}
922}
923
924static void __devinit snd_via82xx_proc_init(struct via82xx_modem *chip)
925{
926	struct snd_info_entry *entry;
927
928	if (! snd_card_proc_new(chip->card, "via82xx", &entry))
929		snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
930}
931
932/*
933 *
934 */
935
936static int snd_via82xx_chip_init(struct via82xx_modem *chip)
937{
938	unsigned int val;
939	unsigned long end_time;
940	unsigned char pval;
941
942	pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
943	if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
944		pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
945		udelay(100);
946	}
947
948	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
949	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
950		/* deassert ACLink reset, force SYNC */
951		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
952				      VIA_ACLINK_CTRL_ENABLE |
953				      VIA_ACLINK_CTRL_RESET |
954				      VIA_ACLINK_CTRL_SYNC);
955		udelay(100);
956		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
957		udelay(100);
958		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
959		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
960		udelay(100);
961	}
962
963	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
964	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
965		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
966		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
967		udelay(100);
968	}
969
970	/* wait until codec ready */
971	end_time = jiffies + msecs_to_jiffies(750);
972	do {
973		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
974		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
975			break;
976		schedule_timeout_uninterruptible(1);
977	} while (time_before(jiffies, end_time));
978
979	if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
980		snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
981
982	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
983				 VIA_REG_AC97_SECONDARY_VALID |
984				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
985	end_time = jiffies + msecs_to_jiffies(750);
986	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
987				 VIA_REG_AC97_SECONDARY_VALID |
988				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
989	do {
990		if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
991			chip->ac97_secondary = 1;
992			goto __ac97_ok2;
993		}
994		schedule_timeout_interruptible(1);
995	} while (time_before(jiffies, end_time));
996	/* This is ok, the most of motherboards have only one codec */
997
998      __ac97_ok2:
999
1000	/* route FM trap to IRQ, disable FM trap */
1001	// pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1002	/* disable all GPI interrupts */
1003	outl(0, VIAREG(chip, GPI_INTR));
1004
1005	return 0;
1006}
1007
1008#ifdef CONFIG_PM
1009/*
1010 * power management
1011 */
1012static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
1013{
1014	struct snd_card *card = pci_get_drvdata(pci);
1015	struct via82xx_modem *chip = card->private_data;
1016	int i;
1017
1018	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1019	for (i = 0; i < 2; i++)
1020		snd_pcm_suspend_all(chip->pcms[i]);
1021	for (i = 0; i < chip->num_devs; i++)
1022		snd_via82xx_channel_reset(chip, &chip->devs[i]);
1023	synchronize_irq(chip->irq);
1024	snd_ac97_suspend(chip->ac97);
1025
1026	pci_disable_device(pci);
1027	pci_save_state(pci);
1028	pci_set_power_state(pci, pci_choose_state(pci, state));
1029	return 0;
1030}
1031
1032static int snd_via82xx_resume(struct pci_dev *pci)
1033{
1034	struct snd_card *card = pci_get_drvdata(pci);
1035	struct via82xx_modem *chip = card->private_data;
1036	int i;
1037
1038	pci_set_power_state(pci, PCI_D0);
1039	pci_restore_state(pci);
1040	if (pci_enable_device(pci) < 0) {
1041		printk(KERN_ERR "via82xx-modem: pci_enable_device failed, "
1042		       "disabling device\n");
1043		snd_card_disconnect(card);
1044		return -EIO;
1045	}
1046	pci_set_master(pci);
1047
1048	snd_via82xx_chip_init(chip);
1049
1050	snd_ac97_resume(chip->ac97);
1051
1052	for (i = 0; i < chip->num_devs; i++)
1053		snd_via82xx_channel_reset(chip, &chip->devs[i]);
1054
1055	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1056	return 0;
1057}
1058#endif /* CONFIG_PM */
1059
1060static int snd_via82xx_free(struct via82xx_modem *chip)
1061{
1062	unsigned int i;
1063
1064	if (chip->irq < 0)
1065		goto __end_hw;
1066	/* disable interrupts */
1067	for (i = 0; i < chip->num_devs; i++)
1068		snd_via82xx_channel_reset(chip, &chip->devs[i]);
1069	synchronize_irq(chip->irq);
1070      __end_hw:
1071	if (chip->irq >= 0)
1072		free_irq(chip->irq, chip);
1073	pci_release_regions(chip->pci);
1074	pci_disable_device(chip->pci);
1075	kfree(chip);
1076	return 0;
1077}
1078
1079static int snd_via82xx_dev_free(struct snd_device *device)
1080{
1081	struct via82xx_modem *chip = device->device_data;
1082	return snd_via82xx_free(chip);
1083}
1084
1085static int __devinit snd_via82xx_create(struct snd_card *card,
1086					struct pci_dev *pci,
1087					int chip_type,
1088					int revision,
1089					unsigned int ac97_clock,
1090					struct via82xx_modem ** r_via)
1091{
1092	struct via82xx_modem *chip;
1093	int err;
1094        static struct snd_device_ops ops = {
1095		.dev_free =	snd_via82xx_dev_free,
1096        };
1097
1098	if ((err = pci_enable_device(pci)) < 0)
1099		return err;
1100
1101	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
1102		pci_disable_device(pci);
1103		return -ENOMEM;
1104	}
1105
1106	spin_lock_init(&chip->reg_lock);
1107	chip->card = card;
1108	chip->pci = pci;
1109	chip->irq = -1;
1110
1111	if ((err = pci_request_regions(pci, card->driver)) < 0) {
1112		kfree(chip);
1113		pci_disable_device(pci);
1114		return err;
1115	}
1116	chip->port = pci_resource_start(pci, 0);
1117	if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
1118			card->driver, chip)) {
1119		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1120		snd_via82xx_free(chip);
1121		return -EBUSY;
1122	}
1123	chip->irq = pci->irq;
1124	if (ac97_clock >= 8000 && ac97_clock <= 48000)
1125		chip->ac97_clock = ac97_clock;
1126	synchronize_irq(chip->irq);
1127
1128	if ((err = snd_via82xx_chip_init(chip)) < 0) {
1129		snd_via82xx_free(chip);
1130		return err;
1131	}
1132
1133	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1134		snd_via82xx_free(chip);
1135		return err;
1136	}
1137
1138	/* The 8233 ac97 controller does not implement the master bit
1139	 * in the pci command register. IMHO this is a violation of the PCI spec.
1140	 * We call pci_set_master here because it does not hurt. */
1141	pci_set_master(pci);
1142
1143	snd_card_set_dev(card, &pci->dev);
1144
1145	*r_via = chip;
1146	return 0;
1147}
1148
1149
1150static int __devinit snd_via82xx_probe(struct pci_dev *pci,
1151				       const struct pci_device_id *pci_id)
1152{
1153	struct snd_card *card;
1154	struct via82xx_modem *chip;
1155	unsigned char revision;
1156	int chip_type = 0, card_type;
1157	unsigned int i;
1158	int err;
1159
1160	card = snd_card_new(index, id, THIS_MODULE, 0);
1161	if (card == NULL)
1162		return -ENOMEM;
1163
1164	card_type = pci_id->driver_data;
1165	pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
1166	switch (card_type) {
1167	case TYPE_CARD_VIA82XX_MODEM:
1168		strcpy(card->driver, "VIA82XX-MODEM");
1169		sprintf(card->shortname, "VIA 82XX modem");
1170		break;
1171	default:
1172		snd_printk(KERN_ERR "invalid card type %d\n", card_type);
1173		err = -EINVAL;
1174		goto __error;
1175	}
1176
1177	if ((err = snd_via82xx_create(card, pci, chip_type, revision,
1178				      ac97_clock, &chip)) < 0)
1179		goto __error;
1180	card->private_data = chip;
1181	if ((err = snd_via82xx_mixer_new(chip)) < 0)
1182		goto __error;
1183
1184	if ((err = snd_via686_pcm_new(chip)) < 0 )
1185		goto __error;
1186
1187	/* disable interrupts */
1188	for (i = 0; i < chip->num_devs; i++)
1189		snd_via82xx_channel_reset(chip, &chip->devs[i]);
1190
1191	sprintf(card->longname, "%s at 0x%lx, irq %d",
1192		card->shortname, chip->port, chip->irq);
1193
1194	snd_via82xx_proc_init(chip);
1195
1196	if ((err = snd_card_register(card)) < 0) {
1197		snd_card_free(card);
1198		return err;
1199	}
1200	pci_set_drvdata(pci, card);
1201	return 0;
1202
1203 __error:
1204	snd_card_free(card);
1205	return err;
1206}
1207
1208static void __devexit snd_via82xx_remove(struct pci_dev *pci)
1209{
1210	snd_card_free(pci_get_drvdata(pci));
1211	pci_set_drvdata(pci, NULL);
1212}
1213
1214static struct pci_driver driver = {
1215	.name = "VIA 82xx Modem",
1216	.id_table = snd_via82xx_modem_ids,
1217	.probe = snd_via82xx_probe,
1218	.remove = __devexit_p(snd_via82xx_remove),
1219#ifdef CONFIG_PM
1220	.suspend = snd_via82xx_suspend,
1221	.resume = snd_via82xx_resume,
1222#endif
1223};
1224
1225static int __init alsa_card_via82xx_init(void)
1226{
1227	return pci_register_driver(&driver);
1228}
1229
1230static void __exit alsa_card_via82xx_exit(void)
1231{
1232	pci_unregister_driver(&driver);
1233}
1234
1235module_init(alsa_card_via82xx_init)
1236module_exit(alsa_card_via82xx_exit)
1237