via82c686.c revision 70346
1/*
2 * Copyright (c) 2000 David Jones <dej@ox.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/sound/pci/via82c686.c 70346 2000-12-25 02:49:28Z cg $
27 */
28
29#include <dev/sound/pcm/sound.h>
30#include <dev/sound/pcm/ac97.h>
31
32#include <pci/pcireg.h>
33#include <pci/pcivar.h>
34#include <sys/sysctl.h>
35
36#include <dev/sound/pci/via82c686.h>
37
38#define VIA_PCI_ID 0x30581106
39#define	NSEGS		16	/* Number of segments in SGD table */
40
41#define SEGS_PER_CHAN	(NSEGS/2)
42
43#define TIMEOUT	50
44#define	VIA_BUFFSIZE	0x4000
45
46#undef DEB
47#define DEB(x)
48
49/* we rely on this struct being packed to 64 bits */
50struct via_dma_op {
51        u_int32_t ptr;
52        u_int32_t flags;
53#define VIA_DMAOP_EOL         0x80000000
54#define VIA_DMAOP_FLAG        0x40000000
55#define VIA_DMAOP_STOP        0x20000000
56#define VIA_DMAOP_COUNT(x)    ((x)&0x00FFFFFF)
57};
58
59struct via_info;
60
61struct via_chinfo {
62	struct via_info *parent;
63	pcm_channel *channel;
64	snd_dbuf *buffer;
65	int dir;
66};
67
68struct via_info {
69	bus_space_tag_t st;
70	bus_space_handle_t sh;
71	bus_dma_tag_t	parent_dmat;
72	bus_dma_tag_t	sgd_dmat;
73
74	struct resource *reg, *irq;
75	int regid, irqid;
76	void *ih;
77
78	struct via_chinfo pch, rch;
79	struct via_dma_op *sgd_table;
80	u_int16_t	codec_caps;
81};
82
83static u_int32_t via_rd(struct via_info *via, int regno, int size);
84static void via_wr(struct via_info *, int regno, u_int32_t data, int size);
85
86static void via_intr(void *);
87bus_dmamap_callback_t dma_cb;
88
89static u_int32_t via_playfmt[] = {
90	AFMT_U8,
91	AFMT_STEREO | AFMT_U8,
92	AFMT_S16_LE,
93	AFMT_STEREO | AFMT_S16_LE,
94	0
95};
96static pcmchan_caps via_playcaps = {4000, 48000, via_playfmt, 0};
97
98static u_int32_t via_recfmt[] = {
99	AFMT_U8,
100	AFMT_STEREO | AFMT_U8,
101	AFMT_S16_LE,
102	AFMT_STEREO | AFMT_S16_LE,
103	0
104};
105static pcmchan_caps via_reccaps = {4000, 48000, via_recfmt, 0};
106
107static u_int32_t
108via_rd(struct via_info *via, int regno, int size)
109{
110
111	switch (size) {
112	case 1:
113		return bus_space_read_1(via->st, via->sh, regno);
114	case 2:
115		return bus_space_read_2(via->st, via->sh, regno);
116	case 4:
117		return bus_space_read_4(via->st, via->sh, regno);
118	default:
119		return 0xFFFFFFFF;
120	}
121}
122
123
124static void
125via_wr(struct via_info *via, int regno, u_int32_t data, int size)
126{
127
128	switch (size) {
129	case 1:
130		bus_space_write_1(via->st, via->sh, regno, data);
131		break;
132	case 2:
133		bus_space_write_2(via->st, via->sh, regno, data);
134		break;
135	case 4:
136		bus_space_write_4(via->st, via->sh, regno, data);
137		break;
138	}
139}
140
141/* -------------------------------------------------------------------- */
142/* Codec interface */
143
144static int
145via_waitready_codec(struct via_info *via)
146{
147	int i;
148
149	/* poll until codec not busy */
150	for (i = 0; (i < TIMEOUT) &&
151	    (via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_BUSY); i++)
152		DELAY(1);
153	if (i >= TIMEOUT) {
154		printf("via: codec busy\n");
155		return 1;
156	}
157
158	return 0;
159}
160
161
162static int
163via_waitvalid_codec(struct via_info *via)
164{
165	int i;
166
167	/* poll until codec valid */
168	for (i = 0; (i < TIMEOUT) &&
169	    !(via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_PRIVALID); i++)
170		    DELAY(1);
171	if (i >= TIMEOUT) {
172		printf("via: codec invalid\n");
173		return 1;
174	}
175
176	return 0;
177}
178
179
180static int
181via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
182{
183	struct via_info *via = addr;
184
185	if (via_waitready_codec(via)) return -1;
186
187	via_wr(via, VIA_CODEC_CTL,
188		VIA_CODEC_PRIVALID | VIA_CODEC_INDEX(reg) | val, 4);
189
190	return 0;
191}
192
193
194static int
195via_read_codec(kobj_t obj, void *addr, int reg)
196{
197	struct via_info *via = addr;
198
199	if (via_waitready_codec(via))
200		return 1;
201
202	via_wr(via, VIA_CODEC_CTL,
203	    VIA_CODEC_PRIVALID | VIA_CODEC_READ | VIA_CODEC_INDEX(reg),4);
204
205	if (via_waitready_codec(via))
206		return 1;
207
208	if (via_waitvalid_codec(via))
209		return 1;
210
211	return via_rd(via, VIA_CODEC_CTL, 2);
212}
213
214static kobj_method_t via_ac97_methods[] = {
215    	KOBJMETHOD(ac97_read,		via_read_codec),
216    	KOBJMETHOD(ac97_write,		via_write_codec),
217	{ 0, 0 }
218};
219AC97_DECLARE(via_ac97);
220
221/* -------------------------------------------------------------------- */
222
223/* channel interface */
224static void *
225viachan_init(kobj_t obj, void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
226{
227	struct via_info *via = devinfo;
228	struct via_chinfo *ch = (dir == PCMDIR_PLAY) ? &via->pch : &via->rch;
229
230	ch->parent = via;
231	ch->channel = c;
232	ch->buffer = b;
233
234	if (sndbuf_alloc(ch->buffer, via->parent_dmat, VIA_BUFFSIZE) == -1) return NULL;
235	return ch;
236}
237
238static int
239viachan_setdir(kobj_t obj, void *data, int dir)
240{
241	struct via_chinfo *ch = data;
242	struct via_info *via = ch->parent;
243	struct via_dma_op *ado;
244	int i, chunk_size;
245	int	phys_addr, flag;
246
247	ch->dir = dir;
248	/*
249	 *  Build the scatter/gather DMA (SGD) table.
250	 *  There are four slots in the table: two for play, two for record.
251	 *  This creates two half-buffers, one of which is playing; the other
252	 *  is feeding.
253	 */
254	ado = via->sgd_table;
255	chunk_size = sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN;
256
257	if (dir == PCMDIR_REC) {
258		ado += SEGS_PER_CHAN;
259	}
260
261	DEB(printf("SGD table located at va %p\n", ado));
262	phys_addr = vtophys(sndbuf_getbuf(ch->buffer));
263	for (i = 0; i < SEGS_PER_CHAN; i++) {
264		ado->ptr = phys_addr;
265		flag = (i == SEGS_PER_CHAN-1) ?
266			VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
267		ado->flags = flag | chunk_size;
268		DEB(printf("ado->ptr/flags = %x/%x\n", phys_addr, flag));
269		phys_addr += chunk_size;
270		ado++;
271	}
272	return 0;
273}
274
275static int
276viachan_setformat(kobj_t obj, void *data, u_int32_t format)
277{
278	struct via_chinfo *ch = data;
279	struct via_info *via = ch->parent;
280	int	mode, mode_set;
281
282	mode_set = 0;
283	if (format & AFMT_STEREO)
284		mode_set |= VIA_RPMODE_STEREO;
285	if (format & AFMT_S16_LE)
286		mode_set |= VIA_RPMODE_16BIT;
287
288	/* Set up for output format */
289	if (ch->dir == PCMDIR_PLAY) {
290		DEB(printf("set play format: %x\n", format));
291		mode = via_rd(via, VIA_PLAY_MODE, 1);
292		mode &= ~(VIA_RPMODE_16BIT | VIA_RPMODE_STEREO);
293		mode |= mode_set;
294		via_wr(via, VIA_PLAY_MODE, mode, 1);
295	}
296	else {
297		DEB(printf("set record format: %x\n", format));
298		mode = via_rd(via, VIA_RECORD_MODE, 1);
299		mode &= ~(VIA_RPMODE_16BIT | VIA_RPMODE_STEREO);
300		mode |= mode_set;
301		via_wr(via, VIA_RECORD_MODE, mode, 1);
302	}
303
304	return 0;
305}
306
307static int
308viachan_setspeed(kobj_t obj, void *data, u_int32_t speed)
309{
310	struct via_chinfo *ch = data;
311	struct via_info *via = ch->parent;
312
313	/*
314	 *  Basic AC'97 defines a 48 kHz sample rate only.  For other rates,
315	 *  upsampling is required.
316	 *
317	 *  The VT82C686A does not perform upsampling, and neither do we.
318	 *  If the codec supports variable-rate audio (i.e. does the upsampling
319	 *  itself), then negotiate the rate with the codec.  Otherwise,
320	 *  return 48 kHz cuz that's all you got.
321	 */
322	if (ch->dir == PCMDIR_PLAY) {
323		DEB(printf("requested play speed: %d\n", speed));
324		if (via->codec_caps & AC97_CODEC_DOES_VRA) {
325			via_write_codec(NULL, via, AC97_REG_EXT_DAC_RATE, speed);
326			speed = via_read_codec(NULL, via, AC97_REG_EXT_DAC_RATE);
327		}
328		else {
329			DEB(printf("VRA not supported!\n"));
330			speed = 48000;
331		}
332		DEB(printf("obtained play speed: %d\n", speed));
333	}
334	else {
335		DEB(printf("requested record speed: %d\n", speed));
336		if (via->codec_caps & AC97_CODEC_DOES_VRA) {
337			via_write_codec(NULL, via, AC97_REG_EXT_ADC_RATE, speed);
338			speed = via_read_codec(NULL, via, AC97_REG_EXT_ADC_RATE);
339		}
340		else {
341			DEB(printf("VRA not supported!\n"));
342			speed = 48000;
343		}
344		DEB(printf("obtained record speed: %d\n", speed));
345	}
346	return speed;
347}
348
349static int
350viachan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
351{
352	struct via_chinfo *ch = data;
353
354	return sndbuf_getsize(ch->buffer) / 2;
355}
356
357static int
358viachan_trigger(kobj_t obj, void *data, int go)
359{
360	struct via_chinfo *ch = data;
361	struct via_info *via = ch->parent;
362	struct via_dma_op *ado;
363
364	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) return 0;
365	if (ch->dir == PCMDIR_PLAY) {
366		if (go == PCMTRIG_START) {
367			ado = &via->sgd_table[0];
368			DEB(printf("ado located at va=%p pa=%x\n", ado, vtophys(ado)));
369			via_wr(via, VIA_PLAY_DMAOPS_BASE, vtophys(ado),4);
370			via_wr(via, VIA_PLAY_CONTROL,
371				VIA_RPCTRL_START, 1);
372		}
373		else {
374			/* Stop DMA */
375			via_wr(via, VIA_PLAY_CONTROL,
376				VIA_RPCTRL_TERMINATE, 1);
377		}
378	} else {
379		if (go == PCMTRIG_START) {
380			ado = &via->sgd_table[SEGS_PER_CHAN];
381			DEB(printf("ado located at va=%p pa=%x\n", ado, vtophys(ado)));
382			via_wr(via, VIA_RECORD_DMAOPS_BASE,
383				vtophys(ado),4);
384			via_wr(via, VIA_RECORD_CONTROL,
385				VIA_RPCTRL_START, 1);
386		}
387		else {
388			/* Stop DMA */
389			via_wr(via, VIA_RECORD_CONTROL,
390				VIA_RPCTRL_TERMINATE, 1);
391		}
392	}
393
394DEB(printf("viachan_trigger: go=%d\n", go));
395	return 0;
396}
397
398static int
399viachan_getptr(kobj_t obj, void *data)
400{
401	struct via_chinfo *ch = data;
402	struct via_info *via = ch->parent;
403	struct via_dma_op *ado;
404	int	ptr, base, len, seg;
405	int base1;
406
407	if (ch->dir == PCMDIR_PLAY) {
408		ado = &via->sgd_table[0];
409		base1 = via_rd(via, VIA_PLAY_DMAOPS_BASE, 4);
410		len = via_rd(via, VIA_PLAY_DMAOPS_COUNT, 4);
411		base = via_rd(via, VIA_PLAY_DMAOPS_BASE, 4);
412		if (base != base1) {	/* Avoid race hazzard	*/
413			len = via_rd(via, VIA_PLAY_DMAOPS_COUNT, 4);
414		}
415		DEB(printf("viachan_getptr: len / base = %x / %x\n", len, base));
416
417		/* Base points to SGD segment to do, one past current */
418
419		/* Determine how many segments have been done */
420		seg = (base - vtophys(ado)) / sizeof(struct via_dma_op);
421		if (seg == 0) seg = SEGS_PER_CHAN;
422
423		/* Now work out offset: seg less count */
424		ptr = seg * sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN - len;
425		DEB(printf("return ptr=%d\n", ptr));
426		return ptr;
427	}
428	else {
429		base1 = via_rd(via, VIA_RECORD_DMAOPS_BASE, 4);
430		ado = &via->sgd_table[SEGS_PER_CHAN];
431		len = via_rd(via, VIA_RECORD_DMAOPS_COUNT, 4);
432		base = via_rd(via, VIA_RECORD_DMAOPS_BASE, 4);
433		if (base != base1) {	/* Avoid race hazzard	*/
434			len = via_rd(via, VIA_RECORD_DMAOPS_COUNT, 4);
435		}
436		DEB(printf("viachan_getptr: len / base = %x / %x\n", len, base));
437
438		/* Base points to next block to do, one past current */
439
440		/* Determine how many segments have been done */
441		seg = (base - vtophys(ado)) / sizeof(struct via_dma_op);
442		if (seg == 0) seg = SEGS_PER_CHAN;
443
444		/* Now work out offset: seg less count */
445		ptr = seg * sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN - len;
446
447		/* DMA appears to operate on memory 'lines' of 32 bytes	*/
448		/* so don't return any part line - it isn't in RAM yet	*/
449		ptr = ptr & ~0x1f;
450		DEB(printf("return ptr=%d\n", ptr));
451		return ptr;
452	}
453	return 0;
454}
455
456static pcmchan_caps *
457viachan_getcaps(kobj_t obj, void *data)
458{
459	struct via_chinfo *ch = data;
460	return (ch->dir == PCMDIR_PLAY) ? &via_playcaps : &via_reccaps;
461}
462
463static kobj_method_t viachan_methods[] = {
464    	KOBJMETHOD(channel_init,		viachan_init),
465    	KOBJMETHOD(channel_setdir,		viachan_setdir),
466    	KOBJMETHOD(channel_setformat,		viachan_setformat),
467    	KOBJMETHOD(channel_setspeed,		viachan_setspeed),
468    	KOBJMETHOD(channel_setblocksize,	viachan_setblocksize),
469    	KOBJMETHOD(channel_trigger,		viachan_trigger),
470    	KOBJMETHOD(channel_getptr,		viachan_getptr),
471    	KOBJMETHOD(channel_getcaps,		viachan_getcaps),
472	{ 0, 0 }
473};
474CHANNEL_DECLARE(viachan);
475
476/* -------------------------------------------------------------------- */
477
478static void
479via_intr(void *p)
480{
481	struct via_info *via = p;
482	int		st;
483
484	/* DEB(printf("viachan_intr\n")); */
485	/* Read channel */
486	st = via_rd(via, VIA_PLAY_STAT, 1);
487	if (st & VIA_RPSTAT_INTR) {
488		via_wr(via, VIA_PLAY_STAT, VIA_RPSTAT_INTR, 1);
489		chn_intr(via->pch.channel);
490	}
491
492	/* Write channel */
493	st = via_rd(via, VIA_RECORD_STAT, 1);
494	if (st & VIA_RPSTAT_INTR) {
495		via_wr(via, VIA_RECORD_STAT, VIA_RPSTAT_INTR, 1);
496		chn_intr(via->rch.channel);
497	}
498}
499
500/*
501 *  Probe and attach the card
502 */
503static int
504via_probe(device_t dev)
505{
506	if (pci_get_devid(dev) == VIA_PCI_ID) {
507	    device_set_desc(dev, "VIA VT82C686A");
508	    return 0;
509	}
510	return ENXIO;
511}
512
513
514void dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
515{
516}
517
518
519static int
520via_attach(device_t dev)
521{
522	struct via_info *via = 0;
523	struct ac97_info *codec = 0;
524	char		status[SND_STATUSLEN];
525
526	u_int32_t	data;
527
528	u_int16_t	v;
529	bus_dmamap_t	sgd_dma_map;
530
531	if ((via = malloc(sizeof *via, M_DEVBUF, M_NOWAIT)) == NULL) {
532		device_printf(dev, "cannot allocate softc\n");
533		return ENXIO;
534	}
535	bzero(via, sizeof *via);
536
537	/* Get resources */
538	data = pci_read_config(dev, PCIR_COMMAND, 2);
539	data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
540	pci_write_config(dev, PCIR_COMMAND, data, 2);
541	data = pci_read_config(dev, PCIR_COMMAND, 2);
542
543	pci_write_config(dev, VIA_PCICONF_MISC,
544		VIA_PCICONF_ACLINKENAB | VIA_PCICONF_ACSGD |
545		VIA_PCICONF_ACNOTRST | VIA_PCICONF_ACVSR, 1);
546
547	via->regid = PCIR_MAPS;
548	via->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &via->regid,
549		0, ~0, 1, RF_ACTIVE);
550	if (!via->reg) {
551		device_printf(dev, "cannot allocate bus resource.");
552		goto bad;
553	}
554	via->st = rman_get_bustag(via->reg);
555	via->sh = rman_get_bushandle(via->reg);
556
557	via->irqid = 0;
558	via->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &via->irqid,
559		0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
560	if (!via->irq
561	    || bus_setup_intr(dev, via->irq, INTR_TYPE_TTY, via_intr, via, &via->ih)){
562		device_printf(dev, "unable to map interrupt\n");
563		goto bad;
564	}
565
566	via_wr(via, VIA_PLAY_MODE,
567		VIA_RPMODE_AUTOSTART |
568		VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
569	via_wr(via, VIA_RECORD_MODE,
570		VIA_RPMODE_AUTOSTART |
571		VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
572
573	codec = AC97_CREATE(dev, via, via_ac97);
574	if (!codec) goto bad;
575
576	mixer_init(dev, ac97_getmixerclass(), codec);
577
578	/*
579	 *  The mixer init resets the codec.  So enabling VRA must be done
580	 *  afterwards.
581	 */
582	v = via_read_codec(NULL, via, AC97_REG_EXT_AUDIO_ID);
583	v &= (AC97_ENAB_VRA | AC97_ENAB_MICVRA);
584	via_write_codec(NULL, via, AC97_REG_EXT_AUDIO_STAT, v);
585	via->codec_caps = v;
586	{
587		v = via_read_codec(NULL, via, AC97_REG_EXT_AUDIO_STAT);
588		DEB(printf("init: codec stat: %d\n", v));
589	}
590
591	if (!(v & AC97_CODEC_DOES_VRA)) {
592		/* no VRA => can do only 48 kbps */
593		via_playcaps.minspeed = 48000;
594		via_reccaps.minspeed = 48000;
595	}
596
597	/* DMA tag for buffers */
598	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
599		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
600		/*highaddr*/BUS_SPACE_MAXADDR,
601		/*filter*/NULL, /*filterarg*/NULL,
602		/*maxsize*/VIA_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff,
603		/*flags*/0, &via->parent_dmat) != 0) {
604		device_printf(dev, "unable to create dma tag\n");
605		goto bad;
606	}
607
608	/*
609	 *  DMA tag for SGD table.  The 686 uses scatter/gather DMA and
610	 *  requires a list in memory of work to do.  We need only 16 bytes
611	 *  for this list, and it is wasteful to allocate 16K.
612	 */
613	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
614		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
615		/*highaddr*/BUS_SPACE_MAXADDR,
616		/*filter*/NULL, /*filterarg*/NULL,
617		/*maxsize*/NSEGS * sizeof(struct via_dma_op),
618		/*nsegments*/1, /*maxsegz*/0x3ffff,
619		/*flags*/0, &via->sgd_dmat) != 0) {
620		device_printf(dev, "unable to create dma tag\n");
621		goto bad;
622	}
623
624	if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table,
625		BUS_DMA_NOWAIT, &sgd_dma_map) == -1) goto bad;
626	if (bus_dmamap_load(via->sgd_dmat, sgd_dma_map, via->sgd_table,
627		NSEGS * sizeof(struct via_dma_op), dma_cb, 0, 0)) goto bad;
628
629	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld",
630		rman_get_start(via->reg), rman_get_start(via->irq));
631
632	/* Register */
633	if (pcm_register(dev, via, 1, 1)) goto bad;
634	pcm_addchan(dev, PCMDIR_PLAY, &viachan_class, via);
635	pcm_addchan(dev, PCMDIR_REC, &viachan_class, via);
636	pcm_setstatus(dev, status);
637	return 0;
638bad:
639	if (codec) ac97_destroy(codec);
640	if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
641	if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
642	if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
643	if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
644	if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
645	if (via) free(via, M_DEVBUF);
646	return ENXIO;
647}
648
649static int
650via_detach(device_t dev)
651{
652	int r;
653	struct via_info *via = 0;
654
655	r = pcm_unregister(dev);
656	if (r)
657		return r;
658
659	via = pcm_getdevinfo(dev);
660	bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
661	bus_teardown_intr(dev, via->irq, via->ih);
662	bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
663	bus_dma_tag_destroy(via->parent_dmat);
664	bus_dma_tag_destroy(via->sgd_dmat);
665	free(via, M_DEVBUF);
666	return 0;
667}
668
669
670static device_method_t via_methods[] = {
671	DEVMETHOD(device_probe,		via_probe),
672	DEVMETHOD(device_attach,	via_attach),
673	DEVMETHOD(device_detach,	via_detach),
674	{ 0, 0}
675};
676
677static driver_t via_driver = {
678	"pcm",
679	via_methods,
680	sizeof(snddev_info),
681};
682
683static devclass_t pcm_devclass;
684
685DRIVER_MODULE(via, pci, via_driver, pcm_devclass, 0, 0);
686MODULE_DEPEND(via, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
687MODULE_VERSION(via, 1);
688
689
690