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