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