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