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