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