kernel - More sound kmalloc adjustments.
[dragonfly.git] / sys / dev / sound / isa / ad1816.c
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * Copyright Luigi Rizzo, 1997,1998
4  * Copyright by Hannu Savolainen 1994, 1995
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/isa/ad1816.c,v 1.37.2.2 2006/04/04 17:23:24 ariff Exp $
29  * $DragonFly: src/sys/dev/sound/isa/ad1816.c,v 1.8 2007/06/16 20:07:18 dillon Exp $
30  */
31
32 #include <dev/sound/pcm/sound.h>
33 #include <dev/sound/isa/ad1816.h>
34
35 #include <bus/isa/isavar.h>
36
37 #include "mixer_if.h"
38
39 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/ad1816.c,v 1.8 2007/06/16 20:07:18 dillon Exp $");
40
41 struct ad1816_info;
42
43 struct ad1816_chinfo {
44         struct ad1816_info *parent;
45         struct pcm_channel *channel;
46         struct snd_dbuf *buffer;
47         int dir, blksz;
48 };
49
50 struct ad1816_info {
51         struct resource *io_base;       /* primary I/O address for the board */
52         int io_rid;
53         struct resource *irq;
54         int irq_rid;
55         struct resource *drq1;          /* play */
56         int drq1_rid;
57         struct resource *drq2;          /* rec */
58         int drq2_rid;
59         void *ih;
60         bus_dma_tag_t parent_dmat;
61         sndlock_t       lock;
62
63         unsigned int bufsize;
64         struct ad1816_chinfo pch, rch;
65 };
66
67 static u_int32_t ad1816_fmt[] = {
68         AFMT_U8,
69         AFMT_STEREO | AFMT_U8,
70         AFMT_S16_LE,
71         AFMT_STEREO | AFMT_S16_LE,
72         AFMT_MU_LAW,
73         AFMT_STEREO | AFMT_MU_LAW,
74         AFMT_A_LAW,
75         AFMT_STEREO | AFMT_A_LAW,
76         0
77 };
78
79 static struct pcmchan_caps ad1816_caps = {4000, 55200, ad1816_fmt, 0};
80
81 #define AD1816_MUTE 31          /* value for mute */
82
83 static void
84 ad1816_lock(struct ad1816_info *ad1816)
85 {
86         snd_mtxlock(ad1816->lock);
87 }
88
89 static void
90 ad1816_unlock(struct ad1816_info *ad1816)
91 {
92         snd_mtxunlock(ad1816->lock);
93 }
94
95 static int
96 port_rd(struct resource *port, int off)
97 {
98         if (port)
99                 return bus_space_read_1(rman_get_bustag(port),
100                                         rman_get_bushandle(port),
101                                         off);
102         else
103                 return -1;
104 }
105
106 static void
107 port_wr(struct resource *port, int off, u_int8_t data)
108 {
109         if (port)
110                 bus_space_write_1(rman_get_bustag(port),
111                                   rman_get_bushandle(port),
112                                   off, data);
113 }
114
115 static int
116 io_rd(struct ad1816_info *ad1816, int reg)
117 {
118         return port_rd(ad1816->io_base, reg);
119 }
120
121 static void
122 io_wr(struct ad1816_info *ad1816, int reg, u_int8_t data)
123 {
124         port_wr(ad1816->io_base, reg, data);
125 }
126
127 static void
128 ad1816_intr(void *arg)
129 {
130         struct ad1816_info *ad1816 = (struct ad1816_info *)arg;
131         unsigned char   c, served = 0;
132
133         ad1816_lock(ad1816);
134         /* get interupt status */
135         c = io_rd(ad1816, AD1816_INT);
136
137         /* check for stray interupts */
138         if (c & ~(AD1816_INTRCI | AD1816_INTRPI)) {
139                 kprintf("pcm: stray int (%x)\n", c);
140                 c &= AD1816_INTRCI | AD1816_INTRPI;
141         }
142         /* check for capture interupt */
143         if (sndbuf_runsz(ad1816->rch.buffer) && (c & AD1816_INTRCI)) {
144                 ad1816_unlock(ad1816);
145                 chn_intr(ad1816->rch.channel);
146                 ad1816_lock(ad1816);
147                 served |= AD1816_INTRCI;                /* cp served */
148         }
149         /* check for playback interupt */
150         if (sndbuf_runsz(ad1816->pch.buffer) && (c & AD1816_INTRPI)) {
151                 ad1816_unlock(ad1816);
152                 chn_intr(ad1816->pch.channel);
153                 ad1816_lock(ad1816);
154                 served |= AD1816_INTRPI;                /* pb served */
155         }
156         if (served == 0) {
157                 /* this probably means this is not a (working) ad1816 chip, */
158                 /* or an error in dma handling                              */
159                 kprintf("pcm: int without reason (%x)\n", c);
160                 c = 0;
161         } else c &= ~served;
162         io_wr(ad1816, AD1816_INT, c);
163         c = io_rd(ad1816, AD1816_INT);
164         if (c != 0) kprintf("pcm: int clear failed (%x)\n", c);
165         ad1816_unlock(ad1816);
166 }
167
168 static int
169 ad1816_wait_init(struct ad1816_info *ad1816, int x)
170 {
171         int             n = 0;  /* to shut up the compiler... */
172
173         for (; x--;)
174                 if ((n = (io_rd(ad1816, AD1816_ALE) & AD1816_BUSY)) == 0) DELAY(10);
175                 else return n;
176         kprintf("ad1816_wait_init failed 0x%02x.\n", n);
177         return -1;
178 }
179
180 static unsigned short
181 ad1816_read(struct ad1816_info *ad1816, unsigned int reg)
182 {
183         u_short         x = 0;
184
185         if (ad1816_wait_init(ad1816, 100) == -1) return 0;
186         io_wr(ad1816, AD1816_ALE, 0);
187         io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
188         if (ad1816_wait_init(ad1816, 100) == -1) return 0;
189         x = (io_rd(ad1816, AD1816_HIGH) << 8) | io_rd(ad1816, AD1816_LOW);
190         return x;
191 }
192
193 static void
194 ad1816_write(struct ad1816_info *ad1816, unsigned int reg, unsigned short data)
195 {
196         if (ad1816_wait_init(ad1816, 100) == -1) return;
197         io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
198         io_wr(ad1816, AD1816_LOW,  (data & 0x000000ff));
199         io_wr(ad1816, AD1816_HIGH, (data & 0x0000ff00) >> 8);
200 }
201
202 /* -------------------------------------------------------------------- */
203
204 static int
205 ad1816mix_init(struct snd_mixer *m)
206 {
207         mix_setdevs(m, AD1816_MIXER_DEVICES);
208         mix_setrecdevs(m, AD1816_REC_DEVICES);
209         return 0;
210 }
211
212 static int
213 ad1816mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
214 {
215         struct ad1816_info *ad1816 = mix_getdevinfo(m);
216         u_short reg = 0;
217
218         /* Scale volumes */
219         left = AD1816_MUTE - (AD1816_MUTE * left) / 100;
220         right = AD1816_MUTE - (AD1816_MUTE * right) / 100;
221
222         reg = (left << 8) | right;
223
224         /* do channel selective muting if volume is zero */
225         if (left == AD1816_MUTE)        reg |= 0x8000;
226         if (right == AD1816_MUTE)       reg |= 0x0080;
227
228         ad1816_lock(ad1816);
229         switch (dev) {
230         case SOUND_MIXER_VOLUME:        /* Register 14 master volume */
231                 ad1816_write(ad1816, 14, reg);
232                 break;
233
234         case SOUND_MIXER_CD:    /* Register 15 cd */
235         case SOUND_MIXER_LINE1:
236                 ad1816_write(ad1816, 15, reg);
237                 break;
238
239         case SOUND_MIXER_SYNTH: /* Register 16 synth */
240                 ad1816_write(ad1816, 16, reg);
241                 break;
242
243         case SOUND_MIXER_PCM:   /* Register 4 pcm */
244                 ad1816_write(ad1816, 4, reg);
245                 break;
246
247         case SOUND_MIXER_LINE:
248         case SOUND_MIXER_LINE3: /* Register 18 line in */
249                 ad1816_write(ad1816, 18, reg);
250                 break;
251
252         case SOUND_MIXER_MIC:   /* Register 19 mic volume */
253                 ad1816_write(ad1816, 19, reg & ~0xff);  /* mic is mono */
254                 break;
255
256         case SOUND_MIXER_IGAIN:
257                 /* and now to something completely different ... */
258                 ad1816_write(ad1816, 20, ((ad1816_read(ad1816, 20) & ~0x0f0f)
259                 | (((AD1816_MUTE - left) / 2) << 8) /* four bits of adc gain */
260                 | ((AD1816_MUTE - right) / 2)));
261                 break;
262
263         default:
264                 kprintf("ad1816_mixer_set(): unknown device.\n");
265                 break;
266         }
267         ad1816_unlock(ad1816);
268
269         left = ((AD1816_MUTE - left) * 100) / AD1816_MUTE;
270         right = ((AD1816_MUTE - right) * 100) / AD1816_MUTE;
271
272         return left | (right << 8);
273 }
274
275 static int
276 ad1816mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
277 {
278         struct ad1816_info *ad1816 = mix_getdevinfo(m);
279         int dev;
280
281         switch (src) {
282         case SOUND_MASK_LINE:
283         case SOUND_MASK_LINE3:
284                 dev = 0x00;
285                 break;
286
287         case SOUND_MASK_CD:
288         case SOUND_MASK_LINE1:
289                 dev = 0x20;
290                 break;
291
292         case SOUND_MASK_MIC:
293         default:
294                 dev = 0x50;
295                 src = SOUND_MASK_MIC;
296         }
297
298         dev |= dev << 8;
299         ad1816_lock(ad1816);
300         ad1816_write(ad1816, 20, (ad1816_read(ad1816, 20) & ~0x7070) | dev);
301         ad1816_unlock(ad1816);
302         return src;
303 }
304
305 static kobj_method_t ad1816mixer_methods[] = {
306         KOBJMETHOD(mixer_init,          ad1816mix_init),
307         KOBJMETHOD(mixer_set,           ad1816mix_set),
308         KOBJMETHOD(mixer_setrecsrc,     ad1816mix_setrecsrc),
309         { 0, 0 }
310 };
311 MIXER_DECLARE(ad1816mixer);
312
313 /* -------------------------------------------------------------------- */
314 /* channel interface */
315 static void *
316 ad1816chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
317 {
318         struct ad1816_info *ad1816 = devinfo;
319         struct ad1816_chinfo *ch = (dir == PCMDIR_PLAY)? &ad1816->pch : &ad1816->rch;
320
321         ch->parent = ad1816;
322         ch->channel = c;
323         ch->buffer = b;
324         if (sndbuf_alloc(ch->buffer, ad1816->parent_dmat, ad1816->bufsize) != 0)
325                 return NULL;
326         return ch;
327 }
328
329 static int
330 ad1816chan_setdir(kobj_t obj, void *data, int dir)
331 {
332         struct ad1816_chinfo *ch = data;
333         struct ad1816_info *ad1816 = ch->parent;
334
335         sndbuf_dmasetup(ch->buffer, (dir == PCMDIR_PLAY)? ad1816->drq1 : ad1816->drq2);
336         ch->dir = dir;
337         return 0;
338 }
339
340 static int
341 ad1816chan_setformat(kobj_t obj, void *data, u_int32_t format)
342 {
343         struct ad1816_chinfo *ch = data;
344         struct ad1816_info *ad1816 = ch->parent;
345         int fmt = AD1816_U8, reg;
346
347         ad1816_lock(ad1816);
348         if (ch->dir == PCMDIR_PLAY) {
349                 reg = AD1816_PLAY;
350                 ad1816_write(ad1816, 8, 0x0000);        /* reset base and current counter */
351                 ad1816_write(ad1816, 9, 0x0000);        /* for playback and capture */
352         } else {
353                 reg = AD1816_CAPT;
354                 ad1816_write(ad1816, 10, 0x0000);
355                 ad1816_write(ad1816, 11, 0x0000);
356         }
357         switch (format & ~AFMT_STEREO) {
358         case AFMT_A_LAW:
359                 fmt = AD1816_ALAW;
360                 break;
361
362         case AFMT_MU_LAW:
363                 fmt = AD1816_MULAW;
364                 break;
365
366         case AFMT_S16_LE:
367                 fmt = AD1816_S16LE;
368                 break;
369
370         case AFMT_S16_BE:
371                 fmt = AD1816_S16BE;
372                 break;
373
374         case AFMT_U8:
375                 fmt = AD1816_U8;
376                 break;
377         }
378         if (format & AFMT_STEREO) fmt |= AD1816_STEREO;
379         io_wr(ad1816, reg, fmt);
380         ad1816_unlock(ad1816);
381 #if 0
382         return format;
383 #else
384         return 0;
385 #endif
386 }
387
388 static int
389 ad1816chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
390 {
391         struct ad1816_chinfo *ch = data;
392         struct ad1816_info *ad1816 = ch->parent;
393
394         RANGE(speed, 4000, 55200);
395         ad1816_lock(ad1816);
396         ad1816_write(ad1816, (ch->dir == PCMDIR_PLAY)? 2 : 3, speed);
397         ad1816_unlock(ad1816);
398         return speed;
399 }
400
401 static int
402 ad1816chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
403 {
404         struct ad1816_chinfo *ch = data;
405
406         ch->blksz = blocksize;
407         return ch->blksz;
408 }
409
410 static int
411 ad1816chan_trigger(kobj_t obj, void *data, int go)
412 {
413         struct ad1816_chinfo *ch = data;
414         struct ad1816_info *ad1816 = ch->parent;
415         int wr, reg;
416
417         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
418                 return 0;
419
420         sndbuf_dma(ch->buffer, go);
421         wr = (ch->dir == PCMDIR_PLAY);
422         reg = wr? AD1816_PLAY : AD1816_CAPT;
423         ad1816_lock(ad1816);
424         switch (go) {
425         case PCMTRIG_START:
426                 /* start only if not already running */
427                 if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) {
428                         int cnt = ((ch->blksz) >> 2) - 1;
429                         ad1816_write(ad1816, wr? 8 : 10, cnt); /* count */
430                         ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
431                         ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) |
432                                      (wr? 0x8000 : 0x4000)); /* enable int */
433                         /* enable playback */
434                         io_wr(ad1816, reg, io_rd(ad1816, reg) | AD1816_ENABLE);
435                         if (!(io_rd(ad1816, reg) & AD1816_ENABLE))
436                                 kprintf("ad1816: failed to start %s DMA!\n",
437                                        wr? "play" : "rec");
438                 }
439                 break;
440
441         case PCMTRIG_STOP:
442         case PCMTRIG_ABORT:             /* XXX check this... */
443                 /* we don't test here if it is running... */
444                 if (wr) {
445                         ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) &
446                                      ~(wr? 0x8000 : 0x4000));
447                         /* disable int */
448                         io_wr(ad1816, reg, io_rd(ad1816, reg) & ~AD1816_ENABLE);
449                         /* disable playback */
450                         if (io_rd(ad1816, reg) & AD1816_ENABLE)
451                                 kprintf("ad1816: failed to stop %s DMA!\n",
452                                        wr? "play" : "rec");
453                         ad1816_write(ad1816, wr? 8 : 10, 0); /* reset base cnt */
454                         ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
455                 }
456                 break;
457         }
458         ad1816_unlock(ad1816);
459         return 0;
460 }
461
462 static int
463 ad1816chan_getptr(kobj_t obj, void *data)
464 {
465         struct ad1816_chinfo *ch = data;
466         return sndbuf_dmaptr(ch->buffer);
467 }
468
469 static struct pcmchan_caps *
470 ad1816chan_getcaps(kobj_t obj, void *data)
471 {
472         return &ad1816_caps;
473 }
474
475 static kobj_method_t ad1816chan_methods[] = {
476         KOBJMETHOD(channel_init,                ad1816chan_init),
477         KOBJMETHOD(channel_setdir,              ad1816chan_setdir),
478         KOBJMETHOD(channel_setformat,           ad1816chan_setformat),
479         KOBJMETHOD(channel_setspeed,            ad1816chan_setspeed),
480         KOBJMETHOD(channel_setblocksize,        ad1816chan_setblocksize),
481         KOBJMETHOD(channel_trigger,             ad1816chan_trigger),
482         KOBJMETHOD(channel_getptr,              ad1816chan_getptr),
483         KOBJMETHOD(channel_getcaps,             ad1816chan_getcaps),
484         { 0, 0 }
485 };
486 CHANNEL_DECLARE(ad1816chan);
487
488 /* -------------------------------------------------------------------- */
489
490 static void
491 ad1816_release_resources(struct ad1816_info *ad1816, device_t dev)
492 {
493         if (ad1816->irq) {
494                 if (ad1816->ih)
495                         bus_teardown_intr(dev, ad1816->irq, ad1816->ih);
496                 bus_release_resource(dev, SYS_RES_IRQ, ad1816->irq_rid, ad1816->irq);
497                 ad1816->irq = 0;
498         }
499         if (ad1816->drq1) {
500                 isa_dma_release(rman_get_start(ad1816->drq1));
501                 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq1_rid, ad1816->drq1);
502                 ad1816->drq1 = 0;
503         }
504         if (ad1816->drq2) {
505                 isa_dma_release(rman_get_start(ad1816->drq2));
506                 bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq2_rid, ad1816->drq2);
507                 ad1816->drq2 = 0;
508         }
509         if (ad1816->io_base) {
510                 bus_release_resource(dev, SYS_RES_IOPORT, ad1816->io_rid, ad1816->io_base);
511                 ad1816->io_base = 0;
512         }
513         if (ad1816->parent_dmat) {
514                 bus_dma_tag_destroy(ad1816->parent_dmat);
515                 ad1816->parent_dmat = 0;
516         }
517         if (ad1816->lock)
518                 snd_mtxfree(ad1816->lock);
519
520         kfree(ad1816, M_DEVBUF);
521 }
522
523 static int
524 ad1816_alloc_resources(struct ad1816_info *ad1816, device_t dev)
525 {
526         int ok = 1, pdma, rdma;
527
528         if (!ad1816->io_base)
529                 ad1816->io_base = bus_alloc_resource_any(dev, 
530                         SYS_RES_IOPORT, &ad1816->io_rid, RF_ACTIVE);
531         if (!ad1816->irq)
532                 ad1816->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
533                         &ad1816->irq_rid, RF_ACTIVE);
534         if (!ad1816->drq1)
535                 ad1816->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
536                         &ad1816->drq1_rid, RF_ACTIVE);
537         if (!ad1816->drq2)
538                 ad1816->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ, 
539                         &ad1816->drq2_rid, RF_ACTIVE);
540
541         if (!ad1816->io_base || !ad1816->drq1 || !ad1816->irq) ok = 0;
542
543         if (ok) {
544                 pdma = rman_get_start(ad1816->drq1);
545                 isa_dma_acquire(pdma);
546                 isa_dmainit(pdma, ad1816->bufsize);
547                 if (ad1816->drq2) {
548                         rdma = rman_get_start(ad1816->drq2);
549                         isa_dma_acquire(rdma);
550                         isa_dmainit(rdma, ad1816->bufsize);
551                 } else
552                         rdma = pdma;
553                 if (pdma == rdma)
554                         pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
555         }
556
557         return ok;
558 }
559
560 static int
561 ad1816_init(struct ad1816_info *ad1816, device_t dev)
562 {
563         ad1816_write(ad1816, 1, 0x2);   /* disable interrupts */
564         ad1816_write(ad1816, 32, 0x90F0);       /* SoundSys Mode, split fmt */
565
566         ad1816_write(ad1816, 5, 0x8080);        /* FM volume mute */
567         ad1816_write(ad1816, 6, 0x8080);        /* I2S1 volume mute */
568         ad1816_write(ad1816, 7, 0x8080);        /* I2S0 volume mute */
569         ad1816_write(ad1816, 17, 0x8888);       /* VID Volume mute */
570         ad1816_write(ad1816, 20, 0x5050);       /* recsrc mic, agc off */
571         /* adc gain is set to 0 */
572
573         return 0;
574 }
575
576 static int
577 ad1816_probe(device_t dev)
578 {
579         char *s = NULL;
580         u_int32_t logical_id = isa_get_logicalid(dev);
581
582         switch (logical_id) {
583         case 0x80719304: /* ADS7180 */
584                 s = "AD1816";
585                 break;
586         case 0x50719304: /* ADS7150 */
587                 s = "AD1815";
588                 break;
589         }
590
591         if (s) {
592                 device_set_desc(dev, s);
593                 return BUS_PROBE_DEFAULT;
594         }
595         return ENXIO;
596 }
597
598 static int
599 ad1816_attach(device_t dev)
600 {
601         struct ad1816_info *ad1816;
602         char status[SND_STATUSLEN], status2[SND_STATUSLEN];
603
604         ad1816 = kmalloc(sizeof *ad1816, M_DEVBUF, M_WAITOK | M_ZERO);
605         ad1816->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
606         ad1816->io_rid = 2;
607         ad1816->irq_rid = 0;
608         ad1816->drq1_rid = 0;
609         ad1816->drq2_rid = 1;
610         ad1816->bufsize = pcm_getbuffersize(dev, 4096, DSP_BUFFSIZE, 65536);
611
612         if (!ad1816_alloc_resources(ad1816, dev)) goto no;
613         ad1816_init(ad1816, dev);
614         if (mixer_init(dev, &ad1816mixer_class, ad1816)) goto no;
615
616         snd_setup_intr(dev, ad1816->irq, 0, ad1816_intr, ad1816, &ad1816->ih);
617         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
618                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
619                         /*highaddr*/BUS_SPACE_MAXADDR,
620                         /*filter*/NULL, /*filterarg*/NULL,
621                         /*maxsize*/ad1816->bufsize, /*nsegments*/1,
622                         /*maxsegz*/0x3ffff,
623                         /*flags*/0,
624                         &ad1816->parent_dmat) != 0) {
625                 device_printf(dev, "unable to create dma tag\n");
626                 goto no;
627         }
628         if (ad1816->drq2)
629                 ksnprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(ad1816->drq2));
630         else
631                 status2[0] = '\0';
632
633         ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s",
634                 rman_get_start(ad1816->io_base),
635                 rman_get_start(ad1816->irq),
636                 rman_get_start(ad1816->drq1),
637                 status2,
638                 ad1816->bufsize,
639                 PCM_KLDSTRING(snd_ad1816));
640
641         if (pcm_register(dev, ad1816, 1, 1)) goto no;
642         pcm_addchan(dev, PCMDIR_REC, &ad1816chan_class, ad1816);
643         pcm_addchan(dev, PCMDIR_PLAY, &ad1816chan_class, ad1816);
644         pcm_setstatus(dev, status);
645
646         return 0;
647 no:
648         ad1816_release_resources(ad1816, dev);
649
650         return ENXIO;
651
652 }
653
654 static int
655 ad1816_detach(device_t dev)
656 {
657         int r;
658         struct ad1816_info *ad1816;
659
660         r = pcm_unregister(dev);
661         if (r)
662                 return r;
663
664         ad1816 = pcm_getdevinfo(dev);
665         ad1816_release_resources(ad1816, dev);
666         return 0;
667 }
668
669 static device_method_t ad1816_methods[] = {
670         /* Device interface */
671         DEVMETHOD(device_probe,         ad1816_probe),
672         DEVMETHOD(device_attach,        ad1816_attach),
673         DEVMETHOD(device_detach,        ad1816_detach),
674
675         { 0, 0 }
676 };
677
678 static driver_t ad1816_driver = {
679         "pcm",
680         ad1816_methods,
681         PCM_SOFTC_SIZE,
682 };
683
684 DRIVER_MODULE(snd_ad1816, isa, ad1816_driver, pcm_devclass, 0, 0);
685 DRIVER_MODULE(snd_ad1816, acpi, ad1816_driver, pcm_devclass, 0, 0);
686 MODULE_DEPEND(snd_ad1816, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
687 MODULE_VERSION(snd_ad1816, 1);
688
689