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