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