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