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