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