xref: /freebsd/sys/dev/sound/pci/via82c686.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 static __inline void
123 via_wr(struct via_info *via, int regno, u_int32_t data, int size)
124 {
125 
126 	switch (size) {
127 	case 1:
128 		bus_space_write_1(via->st, via->sh, regno, data);
129 		break;
130 	case 2:
131 		bus_space_write_2(via->st, via->sh, regno, data);
132 		break;
133 	case 4:
134 		bus_space_write_4(via->st, via->sh, regno, data);
135 		break;
136 	}
137 }
138 
139 /* -------------------------------------------------------------------- */
140 /* Codec interface */
141 
142 static int
143 via_waitready_codec(struct via_info *via)
144 {
145 	int i;
146 
147 	/* poll until codec not busy */
148 	for (i = 0; (i < TIMEOUT) &&
149 	    (via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_BUSY); i++)
150 		DELAY(1);
151 	if (i >= TIMEOUT) {
152 		printf("via: codec busy\n");
153 		return 1;
154 	}
155 
156 	return 0;
157 }
158 
159 static int
160 via_waitvalid_codec(struct via_info *via)
161 {
162 	int i;
163 
164 	/* poll until codec valid */
165 	for (i = 0; (i < TIMEOUT) &&
166 	    !(via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_PRIVALID); i++)
167 		    DELAY(1);
168 	if (i >= TIMEOUT) {
169 		printf("via: codec invalid\n");
170 		return 1;
171 	}
172 
173 	return 0;
174 }
175 
176 static int
177 via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
178 {
179 	struct via_info *via = addr;
180 
181 	if (via_waitready_codec(via)) return -1;
182 
183 	via_wr(via, VIA_CODEC_CTL, VIA_CODEC_PRIVALID | VIA_CODEC_INDEX(reg) | val, 4);
184 
185 	return 0;
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 	KOBJMETHOD_END
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 (AFMT_CHANNEL(format) > 1)
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 u_int32_t
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 u_int32_t
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 	bus_addr_t sgd_addr = ch->sgd_addr;
343 
344 	if (!PCMTRIG_COMMON(go))
345 		return 0;
346 
347 	DEB(printf("ado located at va=%p pa=%x\n", ch->sgd_table, sgd_addr));
348 
349 	snd_mtxlock(via->lock);
350 	if (go == PCMTRIG_START) {
351 		via_buildsgdt(ch);
352 		via_wr(via, ch->base, sgd_addr, 4);
353 		via_wr(via, ch->ctrl, VIA_RPCTRL_START, 1);
354 	} else
355 		via_wr(via, ch->ctrl, VIA_RPCTRL_TERMINATE, 1);
356 	snd_mtxunlock(via->lock);
357 
358 	DEB(printf("viachan_trigger: go=%d\n", go));
359 	return 0;
360 }
361 
362 static u_int32_t
363 viachan_getptr(kobj_t obj, void *data)
364 {
365 	struct via_chinfo *ch = data;
366 	struct via_info *via = ch->parent;
367 	bus_addr_t sgd_addr = ch->sgd_addr;
368 	u_int32_t ptr, base, base1, len, seg;
369 
370 	snd_mtxlock(via->lock);
371 	base1 = via_rd(via, ch->base, 4);
372 	len = via_rd(via, ch->count, 4);
373 	base = via_rd(via, ch->base, 4);
374 	if (base != base1) 	/* Avoid race hazard */
375 		len = via_rd(via, ch->count, 4);
376 	snd_mtxunlock(via->lock);
377 
378 	DEB(printf("viachan_getptr: len / base = %x / %x\n", len, base));
379 
380 	/* Base points to SGD segment to do, one past current */
381 
382 	/* Determine how many segments have been done */
383 	seg = (base - sgd_addr) / sizeof(struct via_dma_op);
384 	if (seg == 0)
385 		seg = SEGS_PER_CHAN;
386 
387 	/* Now work out offset: seg less count */
388 	ptr = (seg * sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN) - len;
389 	if (ch->dir == PCMDIR_REC) {
390 		/* DMA appears to operate on memory 'lines' of 32 bytes	*/
391 		/* so don't return any part line - it isn't in RAM yet	*/
392 		ptr = ptr & ~0x1f;
393 	}
394 
395 	DEB(printf("return ptr=%u\n", ptr));
396 	return ptr;
397 }
398 
399 static struct pcmchan_caps *
400 viachan_getcaps(kobj_t obj, void *data)
401 {
402 	struct via_chinfo *ch = data;
403 	struct via_info *via = ch->parent;
404 
405 	return (via->codec_caps & AC97_EXTCAP_VRA)? &via_vracaps : &via_caps;
406 }
407 
408 static kobj_method_t viachan_methods[] = {
409     	KOBJMETHOD(channel_init,		viachan_init),
410     	KOBJMETHOD(channel_setformat,		viachan_setformat),
411     	KOBJMETHOD(channel_setspeed,		viachan_setspeed),
412     	KOBJMETHOD(channel_setblocksize,	viachan_setblocksize),
413     	KOBJMETHOD(channel_trigger,		viachan_trigger),
414     	KOBJMETHOD(channel_getptr,		viachan_getptr),
415     	KOBJMETHOD(channel_getcaps,		viachan_getcaps),
416 	KOBJMETHOD_END
417 };
418 CHANNEL_DECLARE(viachan);
419 
420 /* -------------------------------------------------------------------- */
421 
422 static void
423 via_intr(void *p)
424 {
425 	struct via_info *via = p;
426 
427 	/* DEB(printf("viachan_intr\n")); */
428 	/* Read channel */
429 	snd_mtxlock(via->lock);
430 	if (via_rd(via, VIA_PLAY_STAT, 1) & VIA_RPSTAT_INTR) {
431 		via_wr(via, VIA_PLAY_STAT, VIA_RPSTAT_INTR, 1);
432 		snd_mtxunlock(via->lock);
433 		chn_intr(via->pch.channel);
434 		snd_mtxlock(via->lock);
435 	}
436 
437 	/* Write channel */
438 	if (via_rd(via, VIA_RECORD_STAT, 1) & VIA_RPSTAT_INTR) {
439 		via_wr(via, VIA_RECORD_STAT, VIA_RPSTAT_INTR, 1);
440 		snd_mtxunlock(via->lock);
441 		chn_intr(via->rch.channel);
442 		return;
443 	}
444 	snd_mtxunlock(via->lock);
445 }
446 
447 /*
448  *  Probe and attach the card
449  */
450 static int
451 via_probe(device_t dev)
452 {
453 	if (pci_get_devid(dev) == VIA_PCI_ID) {
454 		device_set_desc(dev, "VIA VT82C686A");
455 		return BUS_PROBE_DEFAULT;
456 	}
457 	return ENXIO;
458 }
459 
460 static void
461 dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
462 {
463 	struct via_info *via = (struct via_info *)p;
464 	via->sgd_addr = bds->ds_addr;
465 }
466 
467 static int
468 via_attach(device_t dev)
469 {
470 	struct via_info *via = NULL;
471 	char status[SND_STATUSLEN];
472 	u_int32_t data, cnt;
473 
474 	via = malloc(sizeof(*via), M_DEVBUF, M_WAITOK | M_ZERO);
475 	via->lock = snd_mtxcreate(device_get_nameunit(dev),
476 	    "snd_via82c686 softc");
477 
478 	pci_enable_busmaster(dev);
479 
480 	/* Wake up and reset AC97 if necessary */
481 	data = pci_read_config(dev, VIA_AC97STATUS, 1);
482 
483 	if ((data & VIA_AC97STATUS_RDY) == 0) {
484 		/* Cold reset per ac97r2.3 spec (page 95) */
485 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Assert low */
486 		DELAY(100);									/* Wait T_rst_low */
487 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN | VIA_ACLINK_NRST, 1);	/* Assert high */
488 		DELAY(5);									/* Wait T_rst2clk */
489 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Assert low */
490 	} else {
491 		/* Warm reset */
492 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Force no sync */
493 		DELAY(100);
494 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN | VIA_ACLINK_SYNC, 1);	/* Sync */
495 		DELAY(5);									/* Wait T_sync_high */
496 		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Force no sync */
497 		DELAY(5);									/* Wait T_sync2clk */
498 	}
499 
500 	/* Power everything up */
501 	pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_DESIRED, 1);
502 
503 	/* Wait for codec to become ready (largest reported delay here 310ms) */
504 	for (cnt = 0; cnt < 2000; cnt++) {
505 		data = pci_read_config(dev, VIA_AC97STATUS, 1);
506 		if (data & VIA_AC97STATUS_RDY)
507 			break;
508 		DELAY(5000);
509 	}
510 
511 	via->regid = PCIR_BAR(0);
512 	via->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
513 		&via->regid, RF_ACTIVE);
514 	if (!via->reg) {
515 		device_printf(dev, "cannot allocate bus resource.");
516 		goto bad;
517 	}
518 	via->st = rman_get_bustag(via->reg);
519 	via->sh = rman_get_bushandle(via->reg);
520 
521 	via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
522 
523 	via->irqid = 0;
524 	via->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &via->irqid,
525 		RF_ACTIVE | RF_SHAREABLE);
526 	if (!via->irq || snd_setup_intr(dev, via->irq, INTR_MPSAFE, via_intr, via, &via->ih)) {
527 		device_printf(dev, "unable to map interrupt\n");
528 		goto bad;
529 	}
530 
531 	via_wr(via, VIA_PLAY_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
532 	via_wr(via, VIA_RECORD_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
533 
534 	via->codec = AC97_CREATE(dev, via, via_ac97);
535 	if (!via->codec)
536 		goto bad;
537 
538 	if (mixer_init(dev, ac97_getmixerclass(), via->codec))
539 		goto bad;
540 
541 	via->codec_caps = ac97_getextcaps(via->codec);
542 	ac97_setextmode(via->codec,
543 			via->codec_caps & (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
544 
545 	/* DMA tag for buffers */
546 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
547 		/*boundary*/0,
548 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
549 		/*highaddr*/BUS_SPACE_MAXADDR,
550 		/*filter*/NULL, /*filterarg*/NULL,
551 		/*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
552 		/*flags*/0, /*lockfunc*/NULL,
553 		/*lockarg*/NULL, &via->parent_dmat) != 0) {
554 		device_printf(dev, "unable to create dma tag\n");
555 		goto bad;
556 	}
557 
558 	/*
559 	 *  DMA tag for SGD table.  The 686 uses scatter/gather DMA and
560 	 *  requires a list in memory of work to do.  We need only 16 bytes
561 	 *  for this list, and it is wasteful to allocate 16K.
562 	 */
563 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
564 		/*boundary*/0,
565 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
566 		/*highaddr*/BUS_SPACE_MAXADDR,
567 		/*filter*/NULL, /*filterarg*/NULL,
568 		/*maxsize*/NSEGS * sizeof(struct via_dma_op),
569 		/*nsegments*/1, /*maxsegz*/0x3ffff,
570 		/*flags*/0, /*lockfunc*/NULL,
571 		/*lockarg*/NULL, &via->sgd_dmat) != 0) {
572 		device_printf(dev, "unable to create dma tag\n");
573 		goto bad;
574 	}
575 
576 	if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table,
577 	    BUS_DMA_NOWAIT, &via->sgd_dmamap) != 0)
578 		goto bad;
579 	if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table,
580 	    NSEGS * sizeof(struct via_dma_op), dma_cb, via, 0) != 0)
581 		goto bad;
582 
583 	snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
584 		 rman_get_start(via->reg), rman_get_start(via->irq),
585 		 PCM_KLDSTRING(snd_via82c686));
586 
587 	/* Register */
588 	if (pcm_register(dev, via, 1, 1)) goto bad;
589 	pcm_addchan(dev, PCMDIR_PLAY, &viachan_class, via);
590 	pcm_addchan(dev, PCMDIR_REC, &viachan_class, via);
591 	pcm_setstatus(dev, status);
592 	return 0;
593 bad:
594 	if (via->codec) ac97_destroy(via->codec);
595 	if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
596 	if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
597 	if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
598 	if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
599 	if (via->sgd_addr) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
600 	if (via->sgd_table) bus_dmamem_free(via->sgd_dmat, via->sgd_table, via->sgd_dmamap);
601 	if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
602 	if (via->lock) snd_mtxfree(via->lock);
603 	if (via) free(via, M_DEVBUF);
604 	return ENXIO;
605 }
606 
607 static int
608 via_detach(device_t dev)
609 {
610 	int r;
611 	struct via_info *via = NULL;
612 
613 	r = pcm_unregister(dev);
614 	if (r)
615 		return r;
616 
617 	via = pcm_getdevinfo(dev);
618 	bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
619 	bus_teardown_intr(dev, via->irq, via->ih);
620 	bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
621 	bus_dma_tag_destroy(via->parent_dmat);
622 	bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
623 	bus_dmamem_free(via->sgd_dmat, via->sgd_table, via->sgd_dmamap);
624 	bus_dma_tag_destroy(via->sgd_dmat);
625 	snd_mtxfree(via->lock);
626 	free(via, M_DEVBUF);
627 	return 0;
628 }
629 
630 static device_method_t via_methods[] = {
631 	DEVMETHOD(device_probe,		via_probe),
632 	DEVMETHOD(device_attach,	via_attach),
633 	DEVMETHOD(device_detach,	via_detach),
634 	{ 0, 0}
635 };
636 
637 static driver_t via_driver = {
638 	"pcm",
639 	via_methods,
640 	PCM_SOFTC_SIZE,
641 };
642 
643 DRIVER_MODULE(snd_via82c686, pci, via_driver, 0, 0);
644 MODULE_DEPEND(snd_via82c686, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
645 MODULE_VERSION(snd_via82c686, 1);
646