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