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