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