e834a6590f5d8a5b7d2bf98d44246e719b55f006
[dragonfly.git] / sys / dev / sound / isa / sbc.c
1 /*-
2  * Copyright (c) 1999 Seigo Tanimura
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/isa/sbc.c,v 1.44.2.1 2005/12/30 19:55:53 netchild Exp $
27  */
28
29 #include <dev/sound/chip.h>
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/isa/sb.h>
32
33 #include <bus/isa/isavar.h>
34
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/sbc.c,v 1.10 2007/06/16 20:07:18 dillon Exp $");
36
37 #define IO_MAX  3
38 #define IRQ_MAX 1
39 #define DRQ_MAX 2
40 #define INTR_MAX        2
41
42 struct sbc_softc;
43
44 struct sbc_ihl {
45         driver_intr_t *intr[INTR_MAX];
46         void *intr_arg[INTR_MAX];
47         struct sbc_softc *parent;
48 };
49
50 /* Here is the parameter structure per a device. */
51 struct sbc_softc {
52         device_t dev; /* device */
53         device_t child_pcm, child_midi1, child_midi2;
54
55         int io_rid[IO_MAX]; /* io port rids */
56         struct resource *io[IO_MAX]; /* io port resources */
57         int io_alloced[IO_MAX]; /* io port alloc flag */
58
59         int irq_rid[IRQ_MAX]; /* irq rids */
60         struct resource *irq[IRQ_MAX]; /* irq resources */
61         int irq_alloced[IRQ_MAX]; /* irq alloc flag */
62
63         int drq_rid[DRQ_MAX]; /* drq rids */
64         struct resource *drq[DRQ_MAX]; /* drq resources */
65         int drq_alloced[DRQ_MAX]; /* drq alloc flag */
66
67         struct sbc_ihl ihl[IRQ_MAX];
68
69         void *ih[IRQ_MAX];
70
71         sndlock_t       lock;
72
73         u_int32_t bd_ver;
74 };
75
76 static int sbc_probe(device_t dev);
77 static int sbc_attach(device_t dev);
78 static void sbc_intr(void *p);
79
80 static struct resource *sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
81                                            u_long start, u_long end, u_long count, u_int flags);
82 static int sbc_release_resource(device_t bus, device_t child, int type, int rid,
83                                 struct resource *r);
84 static int sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
85                int flags, driver_intr_t *intr, void *arg,
86                void **cookiep);
87 static int sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
88                   void *cookie);
89
90 static int alloc_resource(struct sbc_softc *scp);
91 static int release_resource(struct sbc_softc *scp);
92
93 static devclass_t sbc_devclass;
94
95 static int io_range[3] = {0x10, 0x2, 0x4};
96
97 static int sb_rd(struct resource *io, int reg);
98 static void sb_wr(struct resource *io, int reg, u_int8_t val);
99 static int sb_dspready(struct resource *io);
100 static int sb_cmd(struct resource *io, u_char val);
101 static u_int sb_get_byte(struct resource *io);
102 static void sb_setmixer(struct resource *io, u_int port, u_int value);
103
104 static void
105 sbc_lockinit(struct sbc_softc *scp)
106 {
107         scp->lock = snd_mtxcreate(device_get_nameunit(scp->dev), "sound softc");
108 }
109
110 static void
111 sbc_lockdestroy(struct sbc_softc *scp)
112 {
113         snd_mtxfree(scp->lock);
114 }
115
116 void
117 sbc_lock(struct sbc_softc *scp)
118 {
119         snd_mtxlock(scp->lock);
120 }
121
122 void
123 sbc_lockassert(struct sbc_softc *scp)
124 {
125         snd_mtxassert(scp->lock);
126 }
127
128 void
129 sbc_unlock(struct sbc_softc *scp)
130 {
131         snd_mtxunlock(scp->lock);
132 }
133
134 static int
135 sb_rd(struct resource *io, int reg)
136 {
137         return bus_space_read_1(rman_get_bustag(io),
138                                 rman_get_bushandle(io),
139                                 reg);
140 }
141
142 static void
143 sb_wr(struct resource *io, int reg, u_int8_t val)
144 {
145         bus_space_write_1(rman_get_bustag(io),
146                           rman_get_bushandle(io),
147                           reg, val);
148 }
149
150 static int
151 sb_dspready(struct resource *io)
152 {
153         return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0);
154 }
155
156 static int
157 sb_dspwr(struct resource *io, u_char val)
158 {
159         int  i;
160
161         for (i = 0; i < 1000; i++) {
162                 if (sb_dspready(io)) {
163                         sb_wr(io, SBDSP_CMD, val);
164                         return 1;
165                 }
166                 if (i > 10) DELAY((i > 100)? 1000 : 10);
167         }
168         kprintf("sb_dspwr(0x%02x) timed out.\n", val);
169         return 0;
170 }
171
172 static int
173 sb_cmd(struct resource *io, u_char val)
174 {
175         return sb_dspwr(io, val);
176 }
177
178 static void
179 sb_setmixer(struct resource *io, u_int port, u_int value)
180 {
181         crit_enter();
182         sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
183         DELAY(10);
184         sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff));
185         DELAY(10);
186         crit_exit();
187 }
188
189 static u_int
190 sb_get_byte(struct resource *io)
191 {
192         int i;
193
194         for (i = 1000; i > 0; i--) {
195                 if (sb_rd(io, DSP_DATA_AVAIL) & 0x80)
196                         return sb_rd(io, DSP_READ);
197                 else
198                         DELAY(20);
199         }
200         return 0xffff;
201 }
202
203 static int
204 sb_reset_dsp(struct resource *io)
205 {
206         sb_wr(io, SBDSP_RST, 3);
207         DELAY(100);
208         sb_wr(io, SBDSP_RST, 0);
209         return (sb_get_byte(io) == 0xAA)? 0 : ENXIO;
210 }
211
212 static int
213 sb_identify_board(struct resource *io)
214 {
215         int ver, essver, rev;
216
217         sb_cmd(io, DSP_CMD_GETVER);     /* Get version */
218         ver = (sb_get_byte(io) << 8) | sb_get_byte(io);
219         if (ver < 0x100 || ver > 0x4ff) return 0;
220         if (ver == 0x0301) {
221                 /* Try to detect ESS chips. */
222                 sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */
223                 essver = (sb_get_byte(io) << 8) | sb_get_byte(io);
224                 rev = essver & 0x000f;
225                 essver &= 0xfff0;
226                 if (essver == 0x4880) ver |= 0x1000;
227                 else if (essver == 0x6880) ver = 0x0500 | rev;
228         }
229         return ver;
230 }
231
232 static struct isa_pnp_id sbc_ids[] = {
233         {0x01008c0e, "Creative ViBRA16C"},              /* CTL0001 */
234         {0x31008c0e, "Creative SB16/SB32"},             /* CTL0031 */
235         {0x41008c0e, "Creative SB16/SB32"},             /* CTL0041 */
236         {0x42008c0e, "Creative SB AWE64"},              /* CTL0042 */
237         {0x43008c0e, "Creative ViBRA16X"},              /* CTL0043 */
238         {0x44008c0e, "Creative SB AWE64 Gold"},         /* CTL0044 */
239         {0x45008c0e, "Creative SB AWE64"},              /* CTL0045 */
240         {0x46008c0e, "Creative SB AWE64"},              /* CTL0046 */
241
242         {0x01000000, "Avance Logic ALS100+"},           /* @@@0001 - ViBRA16X clone */
243         {0x01100000, "Avance Asound 110"},              /* @@@1001 */
244         {0x01200000, "Avance Logic ALS120"},            /* @@@2001 - ViBRA16X clone */
245
246         {0x81167316, "ESS ES1681"},                     /* ESS1681 */
247         {0x02017316, "ESS ES1688"},                     /* ESS1688 */
248         {0x68097316, "ESS ES1688"},                     /* ESS1688 */
249         {0x68187316, "ESS ES1868"},                     /* ESS1868 */
250         {0x03007316, "ESS ES1869"},                     /* ESS1869 */
251         {0x69187316, "ESS ES1869"},                     /* ESS1869 */
252         {0xabb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ab */
253         {0xacb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ac */
254         {0x78187316, "ESS ES1878"},                     /* ESS1878 */
255         {0x79187316, "ESS ES1879"},                     /* ESS1879 */
256         {0x88187316, "ESS ES1888"},                     /* ESS1888 */
257         {0x07017316, "ESS ES1888 (DEC OEM)"},           /* ESS0107 */
258         {0x06017316, "ESS ES1888 (Dell OEM)"},          /* ESS0106 */
259         {0}
260 };
261
262 static int
263 sbc_probe(device_t dev)
264 {
265         char *s = NULL;
266         u_int32_t lid, vid;
267
268         lid = isa_get_logicalid(dev);
269         vid = isa_get_vendorid(dev);
270         if (lid) {
271                 if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */
272                         return ENXIO;
273                 /* Check pnp ids */
274                 return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids);
275         } else {
276                 int rid = 0, ver;
277                 struct resource *io;
278
279                 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
280                                         0, ~0, 16, RF_ACTIVE);
281                 if (!io) goto bad;
282                 if (sb_reset_dsp(io)) goto bad2;
283                 ver = sb_identify_board(io);
284                 if (ver == 0) goto bad2;
285                 switch ((ver & 0x00000f00) >> 8) {
286                 case 1:
287                         device_set_desc(dev, "SoundBlaster 1.0 (not supported)");
288                         s = NULL;
289                         break;
290
291                 case 2:
292                         s = "SoundBlaster 2.0";
293                         break;
294
295                 case 3:
296                         s = (ver & 0x0000f000)? "ESS 488" : "SoundBlaster Pro";
297                         break;
298
299                 case 4:
300                         s = "SoundBlaster 16";
301                         break;
302
303                 case 5:
304                         s = (ver & 0x00000008)? "ESS 688" : "ESS 1688";
305                         break;
306                 }
307                 if (s) device_set_desc(dev, s);
308 bad2:           bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
309 bad:            return s? 0 : ENXIO;
310         }
311 }
312
313 static int
314 sbc_attach(device_t dev)
315 {
316         char *err = NULL;
317         struct sbc_softc *scp;
318         struct sndcard_func *func;
319         u_int32_t logical_id = isa_get_logicalid(dev);
320         int flags = device_get_flags(dev);
321         int f, dh, dl, x, irq, i;
322
323         if (!logical_id && (flags & DV_F_DUAL_DMA)) {
324                 bus_set_resource(dev, SYS_RES_DRQ, 1,
325                                  flags & DV_F_DRQ_MASK, 1);
326         }
327
328         scp = device_get_softc(dev);
329         bzero(scp, sizeof(*scp));
330         scp->dev = dev;
331         sbc_lockinit(scp);
332         err = "alloc_resource";
333         if (alloc_resource(scp)) goto bad;
334
335         err = "sb_reset_dsp";
336         if (sb_reset_dsp(scp->io[0])) goto bad;
337         err = "sb_identify_board";
338         scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff;
339         if (scp->bd_ver == 0) goto bad;
340         f = 0;
341         if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499;
342         switch ((scp->bd_ver & 0x0f00) >> 8) {
343         case 1: /* old sound blaster has nothing... */
344                 break;
345
346         case 2:
347                 f |= BD_F_DUP_MIDI;
348                 if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335;
349                 break;
350
351         case 5:
352                 f |= BD_F_ESS;
353                 scp->bd_ver = 0x0301;
354         case 3:
355                 f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345;
356                 break;
357
358         case 4:
359                 f |= BD_F_SB16 | BD_F_MIX_CT1745;
360                 if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1;
361                 if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl;
362                 if (!logical_id && (dh < dl)) {
363                         struct resource *r;
364                         r = scp->drq[0];
365                         scp->drq[0] = scp->drq[1];
366                         scp->drq[1] = r;
367                         dl = rman_get_start(scp->drq[0]);
368                         dh = rman_get_start(scp->drq[1]);
369                 }
370                 /* soft irq/dma configuration */
371                 x = -1;
372                 irq = rman_get_start(scp->irq[0]);
373                 if      (irq == 5) x = 2;
374                 else if (irq == 7) x = 4;
375                 else if (irq == 9) x = 1;
376                 else if (irq == 10) x = 8;
377                 if (x == -1) {
378                         err = "bad irq (5/7/9/10 valid)";
379                         goto bad;
380                 }
381                 else sb_setmixer(scp->io[0], IRQ_NR, x);
382                 sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl));
383                 if (bootverbose) {
384                         device_printf(dev, "setting card to irq %d, drq %d", irq, dl);
385                         if (dl != dh) kprintf(", %d", dh);
386                         kprintf("\n");
387                 }
388                 break;
389         }
390
391         switch (logical_id) {
392         case 0x43008c0e:        /* CTL0043 */
393         case 0x01200000:
394         case 0x01000000:
395                 f |= BD_F_SB16X;
396                 break;
397         }
398         scp->bd_ver |= f << 16;
399
400         err = "setup_intr";
401         for (i = 0; i < IRQ_MAX; i++) {
402                 scp->ihl[i].parent = scp;
403                 if (snd_setup_intr(dev, scp->irq[i], 0, sbc_intr, &scp->ihl[i], &scp->ih[i]))
404                         goto bad;
405         }
406
407         /* PCM Audio */
408         func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
409         if (func == NULL) goto bad;
410         func->func = SCF_PCM;
411         scp->child_pcm = device_add_child(dev, "pcm", -1);
412         device_set_ivars(scp->child_pcm, func);
413
414         /* Midi Interface */
415         func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
416         if (func == NULL) goto bad;
417         func->func = SCF_MIDI;
418         scp->child_midi1 = device_add_child(dev, "midi", -1);
419         device_set_ivars(scp->child_midi1, func);
420
421         /* OPL FM Synthesizer */
422         func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
423         if (func == NULL) goto bad;
424         func->func = SCF_SYNTH;
425         scp->child_midi2 = device_add_child(dev, "midi", -1);
426         device_set_ivars(scp->child_midi2, func);
427
428         /* probe/attach kids */
429         bus_generic_attach(dev);
430
431         return (0);
432
433 bad:    if (err) device_printf(dev, "%s\n", err);
434         release_resource(scp);
435         return (ENXIO);
436 }
437
438 static int
439 sbc_detach(device_t dev)
440 {
441         struct sbc_softc *scp = device_get_softc(dev);
442
443         sbc_lock(scp);
444         device_delete_child(dev, scp->child_midi2);
445         device_delete_child(dev, scp->child_midi1);
446         device_delete_child(dev, scp->child_pcm);
447         release_resource(scp);
448         sbc_lockdestroy(scp);
449         return bus_generic_detach(dev);
450 }
451
452 static void
453 sbc_intr(void *p)
454 {
455         struct sbc_ihl *ihl = p;
456         int i;
457
458         /* sbc_lock(ihl->parent); */
459         i = 0;
460         while (i < INTR_MAX) {
461                 if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]);
462                 i++;
463         }
464         /* sbc_unlock(ihl->parent); */
465 }
466
467 static int
468 sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
469                int flags, driver_intr_t *intr, void *arg,
470                void **cookiep)
471 {
472         struct sbc_softc *scp = device_get_softc(dev);
473         struct sbc_ihl *ihl = NULL;
474         int i, ret;
475
476         sbc_lock(scp);
477         i = 0;
478         while (i < IRQ_MAX) {
479                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
480                 i++;
481         }
482         ret = 0;
483         if (ihl == NULL) ret = EINVAL;
484         i = 0;
485         while ((ret == 0) && (i < INTR_MAX)) {
486                 if (ihl->intr[i] == NULL) {
487                         ihl->intr[i] = intr;
488                         ihl->intr_arg[i] = arg;
489                         *cookiep = &ihl->intr[i];
490                         ret = -1;
491                 } else i++;
492         }
493         sbc_unlock(scp);
494         return (ret > 0)? EINVAL : 0;
495 }
496
497 static int
498 sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
499                   void *cookie)
500 {
501         struct sbc_softc *scp = device_get_softc(dev);
502         struct sbc_ihl *ihl = NULL;
503         int i, ret;
504
505         sbc_lock(scp);
506         i = 0;
507         while (i < IRQ_MAX) {
508                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
509                 i++;
510         }
511         ret = 0;
512         if (ihl == NULL) ret = EINVAL;
513         i = 0;
514         while ((ret == 0) && (i < INTR_MAX)) {
515                 if (cookie == &ihl->intr[i]) {
516                         ihl->intr[i] = NULL;
517                         ihl->intr_arg[i] = NULL;
518                         return 0;
519                 } else i++;
520         }
521         sbc_unlock(scp);
522         return (ret > 0)? EINVAL : 0;
523 }
524
525 static struct resource *
526 sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
527                       u_long start, u_long end, u_long count, u_int flags)
528 {
529         struct sbc_softc *scp;
530         int *alloced, rid_max, alloced_max;
531         struct resource **res;
532
533         scp = device_get_softc(bus);
534         switch (type) {
535         case SYS_RES_IOPORT:
536                 alloced = scp->io_alloced;
537                 res = scp->io;
538                 rid_max = IO_MAX - 1;
539                 alloced_max = 1;
540                 break;
541         case SYS_RES_DRQ:
542                 alloced = scp->drq_alloced;
543                 res = scp->drq;
544                 rid_max = DRQ_MAX - 1;
545                 alloced_max = 1;
546                 break;
547         case SYS_RES_IRQ:
548                 alloced = scp->irq_alloced;
549                 res = scp->irq;
550                 rid_max = IRQ_MAX - 1;
551                 alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */
552                 break;
553         default:
554                 return (NULL);
555         }
556
557         if (*rid > rid_max || alloced[*rid] == alloced_max)
558                 return (NULL);
559
560         alloced[*rid]++;
561         return (res[*rid]);
562 }
563
564 static int
565 sbc_release_resource(device_t bus, device_t child, int type, int rid,
566                         struct resource *r)
567 {
568         struct sbc_softc *scp;
569         int *alloced, rid_max;
570
571         scp = device_get_softc(bus);
572         switch (type) {
573         case SYS_RES_IOPORT:
574                 alloced = scp->io_alloced;
575                 rid_max = IO_MAX - 1;
576                 break;
577         case SYS_RES_DRQ:
578                 alloced = scp->drq_alloced;
579                 rid_max = DRQ_MAX - 1;
580                 break;
581         case SYS_RES_IRQ:
582                 alloced = scp->irq_alloced;
583                 rid_max = IRQ_MAX - 1;
584                 break;
585         default:
586                 return (1);
587         }
588
589         if (rid > rid_max || alloced[rid] == 0)
590                 return (1);
591
592         alloced[rid]--;
593         return (0);
594 }
595
596 static int
597 sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
598 {
599         struct sbc_softc *scp = device_get_softc(bus);
600         struct sndcard_func *func = device_get_ivars(dev);
601
602         switch (index) {
603         case 0:
604                 *result = func->func;
605                 break;
606
607         case 1:
608                 *result = scp->bd_ver;
609                 break;
610
611         default:
612                 return ENOENT;
613         }
614
615         return 0;
616 }
617
618 static int
619 sbc_write_ivar(device_t bus, device_t dev,
620                int index, uintptr_t value)
621 {
622         switch (index) {
623         case 0:
624         case 1:
625                 return EINVAL;
626
627         default:
628                 return (ENOENT);
629         }
630 }
631
632 static int
633 alloc_resource(struct sbc_softc *scp)
634 {
635         int i;
636
637         for (i = 0 ; i < IO_MAX ; i++) {
638                 if (scp->io[i] == NULL) {
639                         scp->io_rid[i] = i;
640                         scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
641                                                         0, ~0, io_range[i], RF_ACTIVE);
642                         if (i == 0 && scp->io[i] == NULL)
643                                 return (1);
644                         scp->io_alloced[i] = 0;
645                 }
646         }
647         for (i = 0 ; i < DRQ_MAX ; i++) {
648                 if (scp->drq[i] == NULL) {
649                         scp->drq_rid[i] = i;
650                         scp->drq[i] = bus_alloc_resource_any(scp->dev,
651                                                              SYS_RES_DRQ,
652                                                              &scp->drq_rid[i],
653                                                              RF_ACTIVE);
654                         if (i == 0 && scp->drq[i] == NULL)
655                                 return (1);
656                         scp->drq_alloced[i] = 0;
657                 }
658         }
659         for (i = 0 ; i < IRQ_MAX ; i++) {
660                 if (scp->irq[i] == NULL) {
661                         scp->irq_rid[i] = i;
662                         scp->irq[i] = bus_alloc_resource_any(scp->dev,
663                                                              SYS_RES_IRQ,
664                                                              &scp->irq_rid[i],
665                                                              RF_ACTIVE);
666                         if (i == 0 && scp->irq[i] == NULL)
667                                 return (1);
668                         scp->irq_alloced[i] = 0;
669                 }
670         }
671         return (0);
672 }
673
674 static int
675 release_resource(struct sbc_softc *scp)
676 {
677         int i;
678
679         for (i = 0 ; i < IO_MAX ; i++) {
680                 if (scp->io[i] != NULL) {
681                         bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
682                         scp->io[i] = NULL;
683                 }
684         }
685         for (i = 0 ; i < DRQ_MAX ; i++) {
686                 if (scp->drq[i] != NULL) {
687                         bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
688                         scp->drq[i] = NULL;
689                 }
690         }
691         for (i = 0 ; i < IRQ_MAX ; i++) {
692                 if (scp->irq[i] != NULL) {
693                         if (scp->ih[i] != NULL)
694                                 bus_teardown_intr(scp->dev, scp->irq[i], scp->ih[i]);
695                         scp->ih[i] = NULL;
696                         bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]);
697                         scp->irq[i] = NULL;
698                 }
699         }
700         return (0);
701 }
702
703 static device_method_t sbc_methods[] = {
704         /* Device interface */
705         DEVMETHOD(device_probe,         sbc_probe),
706         DEVMETHOD(device_attach,        sbc_attach),
707         DEVMETHOD(device_detach,        sbc_detach),
708         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
709         DEVMETHOD(device_suspend,       bus_generic_suspend),
710         DEVMETHOD(device_resume,        bus_generic_resume),
711
712         /* Bus interface */
713         DEVMETHOD(bus_read_ivar,        sbc_read_ivar),
714         DEVMETHOD(bus_write_ivar,       sbc_write_ivar),
715         DEVMETHOD(bus_print_child,      bus_generic_print_child),
716         DEVMETHOD(bus_alloc_resource,   sbc_alloc_resource),
717         DEVMETHOD(bus_release_resource, sbc_release_resource),
718         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
719         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
720         DEVMETHOD(bus_setup_intr,       sbc_setup_intr),
721         DEVMETHOD(bus_teardown_intr,    sbc_teardown_intr),
722
723         { 0, 0 }
724 };
725
726 static driver_t sbc_driver = {
727         "sbc",
728         sbc_methods,
729         sizeof(struct sbc_softc),
730 };
731
732 /* sbc can be attached to an isa bus. */
733 DRIVER_MODULE(snd_sbc, isa, sbc_driver, sbc_devclass, 0, 0);
734 DRIVER_MODULE(snd_sbc, acpi, sbc_driver, sbc_devclass, 0, 0);
735 MODULE_DEPEND(snd_sbc, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
736 MODULE_VERSION(snd_sbc, 1);