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