* Add this nice filesystem testing tool that I've recently
[dragonfly.git] / sys / dev / sound / pci / via8233.c
1 /*
2  * Copyright (c) 2002 Orion Hodson <orion@freebsd.org>
3  * Portions of this code derived from via82c686.c:
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  * $FreeBSD: src/sys/dev/sound/pci/via8233.c,v 1.2.2.2 2003/02/06 17:35:56 orion Exp $
29  * $DragonFly: src/sys/dev/sound/pci/via8233.c,v 1.3 2003/08/07 21:17:13 dillon Exp $
30  */
31
32 /* Some Credits:
33  *
34  * Grzybowski Rafal, Russell Davies, Mark Handley, Daniel O'Connor for
35  * comments, machine time, testing patches, and patience.  VIA for
36  * providing specs.  ALSA for helpful comments and some register poke
37  * ordering.  
38  */
39
40 #include <dev/sound/pcm/sound.h>
41 #include <dev/sound/pcm/ac97.h>
42
43 #include <bus/pci/pcireg.h>
44 #include <bus/pci/pcivar.h>
45 #include <sys/sysctl.h>
46
47 #include <dev/sound/pci/via8233.h>
48
49 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/via8233.c,v 1.3 2003/08/07 21:17:13 dillon Exp $");
50
51 #define VIA8233_PCI_ID 0x30591106
52
53 #define SEGS_PER_CHAN   2                       /* Segments per channel */
54 #define NCHANS          2                       /* Lines-in,out (mic later) */
55 #define NSEGS           NCHANS * SEGS_PER_CHAN  /* Segments in SGD table */
56
57 #define VIA_DEFAULT_BUFSZ       0x1000
58
59 #undef DEB
60 #define DEB(x) x
61
62 /* we rely on this struct being packed to 64 bits */
63 struct via_dma_op {
64         u_int32_t ptr;
65         u_int32_t flags;
66 #define VIA_DMAOP_EOL         0x80000000
67 #define VIA_DMAOP_FLAG        0x40000000
68 #define VIA_DMAOP_STOP        0x20000000
69 #define VIA_DMAOP_COUNT(x)    ((x)&0x00FFFFFF)
70 };
71
72 struct via_info;
73
74 struct via_chinfo {
75         struct via_info *parent;
76         struct pcm_channel *channel;
77         struct snd_dbuf *buffer;
78         struct via_dma_op *sgd_table;
79         int dir, blksz;
80         int rbase;                      /* base register for channel */
81 };
82
83 struct via_info {
84         bus_space_tag_t st;
85         bus_space_handle_t sh;
86         bus_dma_tag_t parent_dmat;
87         bus_dma_tag_t sgd_dmat;
88         bus_dmamap_t sgd_dmamap;
89
90         struct resource *reg, *irq;
91         int regid, irqid;
92         void *ih;
93         struct ac97_info *codec;
94
95         unsigned int bufsz;
96
97         struct via_chinfo pch, rch;
98         struct via_dma_op *sgd_table;
99         u_int16_t codec_caps;
100 };
101
102 static u_int32_t via_fmt[] = {
103         AFMT_U8,
104         AFMT_STEREO | AFMT_U8,
105         AFMT_S16_LE,
106         AFMT_STEREO | AFMT_S16_LE,
107         0
108 };
109
110 static struct pcmchan_caps via_vracaps = { 4000, 48000, via_fmt, 0 };
111 static struct pcmchan_caps via_caps = { 48000, 48000, via_fmt, 0 };
112
113 static u_int32_t
114 via_rd(struct via_info *via, int regno, int size)
115 {
116         switch (size) {
117         case 1:
118                 return bus_space_read_1(via->st, via->sh, regno);
119         case 2:
120                 return bus_space_read_2(via->st, via->sh, regno);
121         case 4:
122                 return bus_space_read_4(via->st, via->sh, regno);
123         default:
124                 return 0xFFFFFFFF;
125         }
126 }
127
128 static void
129 via_wr(struct via_info *via, int regno, u_int32_t data, int size)
130 {
131
132         switch (size) {
133         case 1:
134                 bus_space_write_1(via->st, via->sh, regno, data);
135                 break;
136         case 2:
137                 bus_space_write_2(via->st, via->sh, regno, data);
138                 break;
139         case 4:
140                 bus_space_write_4(via->st, via->sh, regno, data);
141                 break;
142         }
143 }
144
145 /* -------------------------------------------------------------------- */
146 /* Codec interface */
147
148 static int
149 via_waitready_codec(struct via_info *via)
150 {
151         int i;
152
153         /* poll until codec not busy */
154         for (i = 0; i < 1000; i++) {
155                 if ((via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_BUSY) == 0)
156                         return 0;
157                 DELAY(1);
158         }
159         printf("via: codec busy\n");
160         return 1;
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 < 1000; i++) {
170                 if (via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_CODEC00_VALID)
171                         return 0;
172                 DELAY(1);
173         }
174         printf("via: codec invalid\n");
175         return 1;
176 }
177
178 static int
179 via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
180 {
181         struct via_info *via = addr;
182
183         if (via_waitready_codec(via)) return -1;
184
185         via_wr(via, VIA_AC97_CONTROL, 
186                VIA_AC97_CODEC00_VALID | VIA_AC97_INDEX(reg) |
187                VIA_AC97_DATA(val), 4);
188
189         return 0;
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_AC97_CONTROL, VIA_AC97_CODEC00_VALID | 
201                VIA_AC97_READ | VIA_AC97_INDEX(reg), 4);
202
203         if (via_waitready_codec(via))
204                 return -1;
205
206         if (via_waitvalid_codec(via))
207                 return -1;
208
209         return via_rd(via, VIA_AC97_CONTROL, 2);
210 }
211
212 static kobj_method_t via_ac97_methods[] = {
213         KOBJMETHOD(ac97_read,           via_read_codec),
214         KOBJMETHOD(ac97_write,          via_write_codec),
215         { 0, 0 }
216 };
217 AC97_DECLARE(via_ac97);
218
219 /* -------------------------------------------------------------------- */
220
221 static int
222 via_buildsgdt(struct via_chinfo *ch)
223 {
224         u_int32_t phys_addr, flag;
225         int i, seg_size;
226
227         /*
228          *  Build the scatter/gather DMA (SGD) table.
229          *  There are four slots in the table: two for play, two for record.
230          *  This creates two half-buffers, one of which is playing; the other
231          *  is feeding.
232          */
233         seg_size = sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN;
234                 
235         phys_addr = vtophys(sndbuf_getbuf(ch->buffer));
236
237         for (i = 0; i < SEGS_PER_CHAN; i++) {
238                 flag = (i == SEGS_PER_CHAN - 1) ? VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
239                 ch->sgd_table[i].ptr = phys_addr + (i * seg_size);
240                 ch->sgd_table[i].flags = flag | seg_size;
241         }
242
243         return 0;
244 }
245
246 static int
247 via8233pchan_setformat(kobj_t obj, void *data, u_int32_t format)
248 {
249         struct via_chinfo *ch = data;
250         struct via_info *via = ch->parent;
251
252         u_int32_t s = 0xff000000;
253         u_int8_t  v = (format & AFMT_S16_LE) ? MC_SGD_16BIT : MC_SGD_8BIT;
254
255         if (format & AFMT_STEREO) {
256                 v |= MC_SGD_CHANNELS(2);
257                 s |= SLOT3(1) | SLOT4(2);
258         } else {
259                 v |= MC_SGD_CHANNELS(1);
260                 s |= SLOT3(1) | SLOT4(1);
261         }
262
263         via_wr(via, VIA_MC_SLOT_SELECT, s, 4);
264         via_wr(via, VIA_MC_SGD_FORMAT, v, 1);
265
266         return 0;
267 }
268
269 static int
270 via8233rchan_setformat(kobj_t obj, void *data, u_int32_t format)
271 {
272         struct via_chinfo *ch = data;
273         struct via_info *via = ch->parent;
274         
275         u_int32_t f = WR_FORMAT_STOP_INDEX;
276         if (format & AFMT_STEREO)
277                 f |= WR_FORMAT_STEREO;
278         if (format & AFMT_S16_LE)
279                 f |= WR_FORMAT_16BIT;
280         via_wr(via, VIA_WR0_FORMAT, f, 4);
281
282         return 0;
283 }
284
285 static int
286 via8233pchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
287 {
288         struct via_chinfo *ch = data;
289         struct via_info *via = ch->parent;
290
291         if (via->codec_caps & AC97_EXTCAP_VRA)
292                 return ac97_setrate(via->codec, AC97_REGEXT_FDACRATE, speed); 
293
294         return 48000;
295 }
296
297 static int
298 via8233rchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
299 {
300         struct via_chinfo *ch = data;
301         struct via_info *via = ch->parent;
302
303         u_int32_t spd = 48000;
304         if (via->codec_caps & AC97_EXTCAP_VRA) {
305                 spd = ac97_setrate(via->codec, AC97_REGEXT_LADCRATE, speed);
306         }
307         return spd;
308 }
309
310 static int
311 via8233chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
312 {
313         struct via_chinfo *ch = data;
314
315         sndbuf_resize(ch->buffer, SEGS_PER_CHAN, blocksize);
316         ch->blksz = sndbuf_getblksz(ch->buffer);
317         return ch->blksz;
318 }
319
320 static int
321 via8233chan_getptr(kobj_t obj, void *data)
322 {
323         struct via_chinfo *ch = data;
324         struct via_info *via = ch->parent;
325
326         u_int32_t v = via_rd(via, ch->rbase + VIA_RP_CURRENT_COUNT, 4);
327         u_int32_t index = v >> 24;              /* Last completed buffer */
328         u_int32_t count = v & 0x00ffffff;       /* Bytes remaining */
329         int ptr = (index + 1) * ch->blksz - count;
330         ptr %= SEGS_PER_CHAN * ch->blksz;       /* Wrap to available space */
331
332         return ptr;
333 }
334
335 static struct pcmchan_caps *
336 via8233chan_getcaps(kobj_t obj, void *data)
337 {
338         struct via_chinfo *ch = data;
339         struct via_info *via = ch->parent;
340         if (via->codec_caps & AC97_EXTCAP_VRA) 
341                 return &via_vracaps;
342         return &via_caps;
343 }
344
345 static void
346 via8233chan_reset(struct via_info *via, struct via_chinfo *ch)
347 {
348         via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
349         via_wr(via, ch->rbase + VIA_RP_CONTROL, 0x00, 1);
350         via_wr(via, ch->rbase + VIA_RP_STATUS, 
351                SGD_STATUS_EOL | SGD_STATUS_FLAG, 1);
352 }
353
354 static void*
355 via8233chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
356                  struct pcm_channel *c, int dir)
357 {
358         struct via_info *via = devinfo;
359         struct via_chinfo *ch = (dir == PCMDIR_PLAY)? &via->pch : &via->rch;
360
361         ch->parent = via;
362         ch->channel = c;
363         ch->buffer = b;
364         ch->dir = dir;
365         ch->sgd_table = &via->sgd_table[(dir == PCMDIR_PLAY)? 0 : SEGS_PER_CHAN];
366
367         if (ch->dir == PCMDIR_PLAY) {
368                 ch->rbase = VIA_MC_SGD_STATUS;
369         } else {
370                 ch->rbase = VIA_WR0_SGD_STATUS;
371                 via_wr(via, VIA_WR0_SGD_FORMAT, WR_FIFO_ENABLE, 1);
372         }
373
374         if (sndbuf_alloc(ch->buffer, via->parent_dmat, via->bufsz) == -1)
375                 return NULL;
376
377         via8233chan_reset(via, ch);
378
379         return ch;
380 }
381
382 static int
383 via8233chan_trigger(kobj_t obj, void* data, int go)
384 {
385         struct via_chinfo *ch = data;
386         struct via_info *via = ch->parent;
387         struct via_dma_op *ado = ch->sgd_table;
388
389         switch(go) {
390         case PCMTRIG_START:
391                 via_buildsgdt(ch);
392                 via_wr(via, ch->rbase + VIA_RP_TABLE_PTR, vtophys(ado), 4);
393                 via_wr(via, ch->rbase + VIA_RP_CONTROL,
394                        SGD_CONTROL_START | SGD_CONTROL_AUTOSTART |
395                        SGD_CONTROL_I_EOL | SGD_CONTROL_I_FLAG, 1);
396                 break;
397         case PCMTRIG_STOP:
398         case PCMTRIG_ABORT:
399                 via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
400                 via8233chan_reset(via, ch);
401                 break;
402         }
403         return 0;
404 }
405
406 static kobj_method_t via8233pchan_methods[] = {
407         KOBJMETHOD(channel_init,                via8233chan_init),
408         KOBJMETHOD(channel_setformat,           via8233pchan_setformat),
409         KOBJMETHOD(channel_setspeed,            via8233pchan_setspeed),
410         KOBJMETHOD(channel_setblocksize,        via8233chan_setblocksize),
411         KOBJMETHOD(channel_trigger,             via8233chan_trigger),
412         KOBJMETHOD(channel_getptr,              via8233chan_getptr),
413         KOBJMETHOD(channel_getcaps,             via8233chan_getcaps),
414         { 0, 0 }
415 };
416 CHANNEL_DECLARE(via8233pchan);
417
418 static kobj_method_t via8233rchan_methods[] = {
419         KOBJMETHOD(channel_init,                via8233chan_init),
420         KOBJMETHOD(channel_setformat,           via8233rchan_setformat),
421         KOBJMETHOD(channel_setspeed,            via8233rchan_setspeed),
422         KOBJMETHOD(channel_setblocksize,        via8233chan_setblocksize),
423         KOBJMETHOD(channel_trigger,             via8233chan_trigger),
424         KOBJMETHOD(channel_getptr,              via8233chan_getptr),
425         KOBJMETHOD(channel_getcaps,             via8233chan_getcaps),
426         { 0, 0 }
427 };
428 CHANNEL_DECLARE(via8233rchan);
429
430 /* -------------------------------------------------------------------- */
431
432 static void
433 via_intr(void *p)
434 {
435         struct via_info *via = p;
436         int r = via_rd(via, VIA_MC_SGD_STATUS, 1);
437         if (r & SGD_STATUS_INTR) {
438                 via_wr(via, VIA_MC_SGD_STATUS, SGD_STATUS_INTR, 1);
439                 chn_intr(via->pch.channel);
440         }
441
442         r = via_rd(via, VIA_WR0_SGD_STATUS, 1);
443         if (r & SGD_STATUS_INTR) {
444                 via_wr(via, VIA_WR0_SGD_STATUS, SGD_STATUS_INTR, 1);
445                 chn_intr(via->rch.channel);
446         }
447 }
448
449 /*
450  *  Probe and attach the card
451  */
452 static int
453 via_probe(device_t dev)
454 {
455         switch(pci_get_devid(dev)) {
456         case VIA8233_PCI_ID:
457                 switch(pci_get_revid(dev)) {
458                 case 0x10: 
459                         device_set_desc(dev, "VIA VT8233 (pre)");
460                         return 0;
461                 case 0x20:
462                         device_set_desc(dev, "VIA VT8233C");
463                         return 0;
464                 case 0x30:
465                         device_set_desc(dev, "VIA VT8233");
466                         return 0;
467                 case 0x40:
468                         device_set_desc(dev, "VIA VT8233A");
469                         return 0;
470                 case 0x50:
471                         device_set_desc(dev, "VIA VT8235");
472                         return 0;
473                 default:
474                         device_set_desc(dev, "VIA VT8233X");    /* Unknown */
475                         return 0;
476                 }                       
477         }
478         return ENXIO;
479 }
480
481 static void
482 dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
483 {
484 }
485
486 static int
487 via_chip_init(device_t dev)
488 {
489         int i, s;
490
491         pci_write_config(dev, VIA_PCI_ACLINK_CTRL, 0, 1);       
492         DELAY(100);
493
494         /* assert ACLink reset */
495         pci_write_config(dev, VIA_PCI_ACLINK_CTRL, VIA_PCI_ACLINK_NRST, 1);
496         DELAY(2);
497
498         /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
499         pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
500                          VIA_PCI_ACLINK_NRST | VIA_PCI_ACLINK_SYNC, 1);
501
502         /* ACLink on, deassert ACLink reset, VSR, SGD data out */
503         pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
504                          VIA_PCI_ACLINK_EN | VIA_PCI_ACLINK_NRST
505                          | VIA_PCI_ACLINK_VRATE | VIA_PCI_ACLINK_SGD, 1);
506
507         for (i = 0; i < 100; i++) {
508                 s = pci_read_config(dev, VIA_PCI_ACLINK_STAT, 1);
509                 if (s & VIA_PCI_ACLINK_C00_READY) {
510                         s = pci_read_config(dev, VIA_PCI_ACLINK_CTRL, 1);
511                         return 0;
512                 }
513                 DELAY(10);
514         }
515         device_printf(dev, "primary codec not ready (s = 0x%02x)\n", s);
516         return ENXIO;
517 }
518
519 static int
520 via_attach(device_t dev)
521 {
522         struct via_info *via = 0;
523         char status[SND_STATUSLEN];
524
525         if ((via = malloc(sizeof *via, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
526                 device_printf(dev, "cannot allocate softc\n");
527                 return ENXIO;
528         }
529
530         pci_enable_io(dev, SYS_RES_IOPORT);
531         pci_set_powerstate(dev, PCI_POWERSTATE_D0);
532         pci_enable_busmaster(dev);
533         
534         via->regid = PCIR_MAPS;
535         via->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &via->regid, 0, ~0,
536                                       1, RF_ACTIVE);
537         if (!via->reg) {
538                 device_printf(dev, "cannot allocate bus resource.");
539                 goto bad;
540         }
541         via->st = rman_get_bustag(via->reg);
542         via->sh = rman_get_bushandle(via->reg);
543
544         via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
545
546         via->irqid = 0;
547         via->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &via->irqid, 0, ~0, 1,
548                                       RF_ACTIVE | RF_SHAREABLE);
549         if (!via->irq || 
550             snd_setup_intr(dev, via->irq, 0, via_intr, via, &via->ih)) {
551                 device_printf(dev, "unable to map interrupt\n");
552                 goto bad;
553         }
554
555         /* DMA tag for buffers */
556         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
557                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
558                 /*highaddr*/BUS_SPACE_MAXADDR,
559                 /*filter*/NULL, /*filterarg*/NULL,
560                 /*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
561                 /*flags*/0, &via->parent_dmat) != 0) {
562                 device_printf(dev, "unable to create dma tag\n");
563                 goto bad;
564         }
565
566         /*
567          *  DMA tag for SGD table.  The 686 uses scatter/gather DMA and
568          *  requires a list in memory of work to do.  We need only 16 bytes
569          *  for this list, and it is wasteful to allocate 16K.
570          */
571         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
572                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
573                 /*highaddr*/BUS_SPACE_MAXADDR,
574                 /*filter*/NULL, /*filterarg*/NULL,
575                 /*maxsize*/NSEGS * sizeof(struct via_dma_op),
576                 /*nsegments*/1, /*maxsegz*/0x3ffff,
577                 /*flags*/0, &via->sgd_dmat) != 0) {
578                 device_printf(dev, "unable to create dma tag\n");
579                 goto bad;
580         }
581
582         if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table, 
583                              BUS_DMA_NOWAIT, &via->sgd_dmamap) == -1)
584                 goto bad;
585         if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table, 
586                             NSEGS * sizeof(struct via_dma_op), dma_cb, 0, 0))
587                 goto bad;
588
589         if (via_chip_init(dev))
590                 goto bad;
591
592         via->codec = AC97_CREATE(dev, via, via_ac97);
593         if (!via->codec)
594                 goto bad;
595
596         mixer_init(dev, ac97_getmixerclass(), via->codec);
597
598         via->codec_caps = ac97_getextcaps(via->codec);
599
600         /* Try to set VRA without generating an error, VRM not reqrd yet */
601         if (via->codec_caps & 
602             (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM | AC97_EXTCAP_DRA)) {
603                 u_int16_t ext = ac97_getextmode(via->codec);
604                 ext |= (via->codec_caps & 
605                         (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
606                 ext &= ~AC97_EXTCAP_DRA;
607                 ac97_setextmode(via->codec, ext);
608         }
609
610         snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld", 
611                  rman_get_start(via->reg), rman_get_start(via->irq));
612
613         /* Register */
614         if (pcm_register(dev, via, 1, 1)) goto bad;
615
616         pcm_addchan(dev, PCMDIR_PLAY, &via8233pchan_class, via);
617         pcm_addchan(dev, PCMDIR_REC, &via8233rchan_class, via);
618
619         pcm_setstatus(dev, status);
620
621         return 0;
622 bad:
623         if (via->codec) ac97_destroy(via->codec);
624         if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
625         if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
626         if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
627         if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
628         if (via->sgd_dmamap) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
629         if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
630         if (via) free(via, M_DEVBUF);
631         return ENXIO;
632 }
633
634 static int
635 via_detach(device_t dev)
636 {
637         int r;
638         struct via_info *via = 0;
639
640         r = pcm_unregister(dev);
641         if (r) return r;
642
643         via = pcm_getdevinfo(dev);
644         bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
645         bus_teardown_intr(dev, via->irq, via->ih);
646         bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
647         bus_dma_tag_destroy(via->parent_dmat);
648         bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
649         bus_dma_tag_destroy(via->sgd_dmat);
650         free(via, M_DEVBUF);
651         return 0;
652 }
653
654
655 static device_method_t via_methods[] = {
656         DEVMETHOD(device_probe,         via_probe),
657         DEVMETHOD(device_attach,        via_attach),
658         DEVMETHOD(device_detach,        via_detach),
659         { 0, 0}
660 };
661
662 static driver_t via_driver = {
663         "pcm",
664         via_methods,
665         PCM_SOFTC_SIZE,
666 };
667
668 DRIVER_MODULE(snd_via8233, pci, via_driver, pcm_devclass, 0, 0);
669 MODULE_DEPEND(snd_via8233, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
670 MODULE_VERSION(snd_via8233, 1);