2 * Copyright (c) 1999 Seigo Tanimura
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <dev/sound/chip.h>
28 #include <dev/sound/pcm/sound.h>
29 #include <dev/sound/isa/sb.h>
31 #include <isa/isavar.h>
33 SND_DECLARE_FILE("$FreeBSD$");
43 driver_intr_t *intr[INTR_MAX];
44 void *intr_arg[INTR_MAX];
45 struct sbc_softc *parent;
48 /* Here is the parameter structure per a device. */
50 device_t dev; /* device */
51 device_t child_pcm, child_midi1, child_midi2;
53 int io_rid[IO_MAX]; /* io port rids */
54 struct resource *io[IO_MAX]; /* io port resources */
55 int io_alloced[IO_MAX]; /* io port alloc flag */
57 int irq_rid[IRQ_MAX]; /* irq rids */
58 struct resource *irq[IRQ_MAX]; /* irq resources */
59 int irq_alloced[IRQ_MAX]; /* irq alloc flag */
61 int drq_rid[DRQ_MAX]; /* drq rids */
62 struct resource *drq[DRQ_MAX]; /* drq resources */
63 int drq_alloced[DRQ_MAX]; /* drq alloc flag */
65 struct sbc_ihl ihl[IRQ_MAX];
74 static int sbc_probe(device_t dev);
75 static int sbc_attach(device_t dev);
76 static void sbc_intr(void *p);
78 static struct resource *sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
79 u_long start, u_long end, u_long count, u_int flags);
80 static int sbc_release_resource(device_t bus, device_t child, int type, int rid,
82 static int sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
84 #if __FreeBSD_version >= 700031
85 driver_filter_t *filter,
88 void *arg, void **cookiep);
89 static int sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
92 static int alloc_resource(struct sbc_softc *scp);
93 static int release_resource(struct sbc_softc *scp);
95 static devclass_t sbc_devclass;
97 static int io_range[3] = {0x10, 0x2, 0x4};
99 #ifdef PC98 /* I/O address table for PC98 */
100 static bus_addr_t pcm_iat[] = {
101 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700,
102 0x800, 0x900, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00
104 static bus_addr_t midi_iat[] = {
107 static bus_addr_t opl_iat[] = {
108 0x000, 0x100, 0x200, 0x300
110 static bus_addr_t *sb_iat[] = { pcm_iat, midi_iat, opl_iat };
113 static int sb_rd(struct resource *io, int reg);
114 static void sb_wr(struct resource *io, int reg, u_int8_t val);
115 static int sb_dspready(struct resource *io);
116 static int sb_cmd(struct resource *io, u_char val);
117 static u_int sb_get_byte(struct resource *io);
118 static void sb_setmixer(struct resource *io, u_int port, u_int value);
121 sbc_lockinit(struct sbc_softc *scp)
123 scp->lock = snd_mtxcreate(device_get_nameunit(scp->dev),
128 sbc_lockdestroy(struct sbc_softc *scp)
130 snd_mtxfree(scp->lock);
134 sbc_lock(struct sbc_softc *scp)
136 snd_mtxlock(scp->lock);
140 sbc_lockassert(struct sbc_softc *scp)
142 snd_mtxassert(scp->lock);
146 sbc_unlock(struct sbc_softc *scp)
148 snd_mtxunlock(scp->lock);
152 sb_rd(struct resource *io, int reg)
154 return bus_space_read_1(rman_get_bustag(io),
155 rman_get_bushandle(io),
160 sb_wr(struct resource *io, int reg, u_int8_t val)
162 bus_space_write_1(rman_get_bustag(io),
163 rman_get_bushandle(io),
168 sb_dspready(struct resource *io)
170 return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0);
174 sb_dspwr(struct resource *io, u_char val)
178 for (i = 0; i < 1000; i++) {
179 if (sb_dspready(io)) {
180 sb_wr(io, SBDSP_CMD, val);
183 if (i > 10) DELAY((i > 100)? 1000 : 10);
185 printf("sb_dspwr(0x%02x) timed out.\n", val);
190 sb_cmd(struct resource *io, u_char val)
192 return sb_dspwr(io, val);
196 sb_setmixer(struct resource *io, u_int port, u_int value)
201 sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
203 sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff));
209 sb_get_byte(struct resource *io)
213 for (i = 1000; i > 0; i--) {
214 if (sb_rd(io, DSP_DATA_AVAIL) & 0x80)
215 return sb_rd(io, DSP_READ);
223 sb_reset_dsp(struct resource *io)
225 sb_wr(io, SBDSP_RST, 3);
227 sb_wr(io, SBDSP_RST, 0);
228 return (sb_get_byte(io) == 0xAA)? 0 : ENXIO;
232 sb_identify_board(struct resource *io)
234 int ver, essver, rev;
236 sb_cmd(io, DSP_CMD_GETVER); /* Get version */
237 ver = (sb_get_byte(io) << 8) | sb_get_byte(io);
238 if (ver < 0x100 || ver > 0x4ff) return 0;
240 /* Try to detect ESS chips. */
241 sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */
242 essver = (sb_get_byte(io) << 8) | sb_get_byte(io);
243 rev = essver & 0x000f;
245 if (essver == 0x4880) ver |= 0x1000;
246 else if (essver == 0x6880) ver = 0x0500 | rev;
251 static struct isa_pnp_id sbc_ids[] = {
252 {0x01008c0e, "Creative ViBRA16C"}, /* CTL0001 */
253 {0x31008c0e, "Creative SB16/SB32"}, /* CTL0031 */
254 {0x41008c0e, "Creative SB16/SB32"}, /* CTL0041 */
255 {0x42008c0e, "Creative SB AWE64"}, /* CTL0042 */
256 {0x43008c0e, "Creative ViBRA16X"}, /* CTL0043 */
257 {0x44008c0e, "Creative SB AWE64 Gold"}, /* CTL0044 */
258 {0x45008c0e, "Creative SB AWE64"}, /* CTL0045 */
259 {0x46008c0e, "Creative SB AWE64"}, /* CTL0046 */
261 {0x01000000, "Avance Logic ALS100+"}, /* @@@0001 - ViBRA16X clone */
262 {0x01100000, "Avance Asound 110"}, /* @@@1001 */
263 {0x01200000, "Avance Logic ALS120"}, /* @@@2001 - ViBRA16X clone */
265 {0x81167316, "ESS ES1681"}, /* ESS1681 */
266 {0x02017316, "ESS ES1688"}, /* ESS1688 */
267 {0x68097316, "ESS ES1688"}, /* ESS1688 */
268 {0x68187316, "ESS ES1868"}, /* ESS1868 */
269 {0x03007316, "ESS ES1869"}, /* ESS1869 */
270 {0x69187316, "ESS ES1869"}, /* ESS1869 */
271 {0xabb0110e, "ESS ES1869 (Compaq OEM)"}, /* CPQb0ab */
272 {0xacb0110e, "ESS ES1869 (Compaq OEM)"}, /* CPQb0ac */
273 {0x78187316, "ESS ES1878"}, /* ESS1878 */
274 {0x79187316, "ESS ES1879"}, /* ESS1879 */
275 {0x88187316, "ESS ES1888"}, /* ESS1888 */
276 {0x07017316, "ESS ES1888 (DEC OEM)"}, /* ESS0107 */
277 {0x06017316, "ESS ES1888 (Dell OEM)"}, /* ESS0106 */
282 sbc_probe(device_t dev)
287 lid = isa_get_logicalid(dev);
288 vid = isa_get_vendorid(dev);
290 if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */
293 return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids);
299 io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
300 pcm_iat, 16, RF_ACTIVE);
302 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
303 0, ~0, 16, RF_ACTIVE);
307 isa_load_resourcev(io, pcm_iat, 16);
309 if (sb_reset_dsp(io)) goto bad2;
310 ver = sb_identify_board(io);
311 if (ver == 0) goto bad2;
312 switch ((ver & 0x00000f00) >> 8) {
314 device_set_desc(dev, "SoundBlaster 1.0 (not supported)");
319 s = "SoundBlaster 2.0";
323 s = (ver & 0x0000f000)? "ESS 488" : "SoundBlaster Pro";
327 s = "SoundBlaster 16";
331 s = (ver & 0x00000008)? "ESS 688" : "ESS 1688";
334 if (s) device_set_desc(dev, s);
335 bad2: bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
336 bad: return s? 0 : ENXIO;
341 sbc_attach(device_t dev)
344 struct sbc_softc *scp;
345 struct sndcard_func *func;
346 u_int32_t logical_id = isa_get_logicalid(dev);
347 int flags = device_get_flags(dev);
348 int f, dh, dl, x, irq, i;
350 if (!logical_id && (flags & DV_F_DUAL_DMA)) {
351 bus_set_resource(dev, SYS_RES_DRQ, 1,
352 flags & DV_F_DRQ_MASK, 1);
355 scp = device_get_softc(dev);
356 bzero(scp, sizeof(*scp));
359 err = "alloc_resource";
360 if (alloc_resource(scp)) goto bad;
362 err = "sb_reset_dsp";
363 if (sb_reset_dsp(scp->io[0])) goto bad;
364 err = "sb_identify_board";
365 scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff;
366 if (scp->bd_ver == 0) goto bad;
368 if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499;
369 switch ((scp->bd_ver & 0x0f00) >> 8) {
370 case 1: /* old sound blaster has nothing... */
375 if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335;
380 scp->bd_ver = 0x0301;
382 f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345;
386 f |= BD_F_SB16 | BD_F_MIX_CT1745;
387 if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1;
388 if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl;
389 if (!logical_id && (dh < dl)) {
392 scp->drq[0] = scp->drq[1];
394 dl = rman_get_start(scp->drq[0]);
395 dh = rman_get_start(scp->drq[1]);
397 /* soft irq/dma configuration */
399 irq = rman_get_start(scp->irq[0]);
401 /* SB16 in PC98 use different IRQ table */
403 else if (irq == 5) x = 8;
404 else if (irq == 10) x = 2;
405 else if (irq == 12) x = 4;
407 err = "bad irq (3/5/10/12 valid)";
410 else sb_setmixer(scp->io[0], IRQ_NR, x);
411 /* SB16 in PC98 use different dma setting */
412 sb_setmixer(scp->io[0], DMA_NR, dh == 0 ? 1 : 2);
415 else if (irq == 7) x = 4;
416 else if (irq == 9) x = 1;
417 else if (irq == 10) x = 8;
419 err = "bad irq (5/7/9/10 valid)";
422 else sb_setmixer(scp->io[0], IRQ_NR, x);
423 sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl));
426 device_printf(dev, "setting card to irq %d, drq %d", irq, dl);
427 if (dl != dh) printf(", %d", dh);
433 switch (logical_id) {
434 case 0x43008c0e: /* CTL0043 */
440 scp->bd_ver |= f << 16;
443 for (i = 0; i < IRQ_MAX; i++) {
444 scp->ihl[i].parent = scp;
445 if (snd_setup_intr(dev, scp->irq[i], 0, sbc_intr, &scp->ihl[i], &scp->ih[i]))
450 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
451 if (func == NULL) goto bad;
452 func->func = SCF_PCM;
453 scp->child_pcm = device_add_child(dev, "pcm", -1);
454 device_set_ivars(scp->child_pcm, func);
457 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
458 if (func == NULL) goto bad;
459 func->func = SCF_MIDI;
460 scp->child_midi1 = device_add_child(dev, "midi", -1);
461 device_set_ivars(scp->child_midi1, func);
463 /* OPL FM Synthesizer */
464 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
465 if (func == NULL) goto bad;
466 func->func = SCF_SYNTH;
467 scp->child_midi2 = device_add_child(dev, "midi", -1);
468 device_set_ivars(scp->child_midi2, func);
470 /* probe/attach kids */
471 bus_generic_attach(dev);
475 bad: if (err) device_printf(dev, "%s\n", err);
476 release_resource(scp);
481 sbc_detach(device_t dev)
483 struct sbc_softc *scp = device_get_softc(dev);
486 device_delete_child(dev, scp->child_midi2);
487 device_delete_child(dev, scp->child_midi1);
488 device_delete_child(dev, scp->child_pcm);
489 release_resource(scp);
490 sbc_lockdestroy(scp);
491 return bus_generic_detach(dev);
497 struct sbc_ihl *ihl = p;
500 /* sbc_lock(ihl->parent); */
502 while (i < INTR_MAX) {
503 if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]);
506 /* sbc_unlock(ihl->parent); */
510 sbc_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
511 #if __FreeBSD_version >= 700031
512 driver_filter_t *filter,
515 void *arg, void **cookiep)
517 struct sbc_softc *scp = device_get_softc(dev);
518 struct sbc_ihl *ihl = NULL;
521 #if __FreeBSD_version >= 700031
522 if (filter != NULL) {
523 printf("sbc.c: we cannot use a filter here\n");
529 while (i < IRQ_MAX) {
530 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
534 if (ihl == NULL) ret = EINVAL;
536 while ((ret == 0) && (i < INTR_MAX)) {
537 if (ihl->intr[i] == NULL) {
539 ihl->intr_arg[i] = arg;
540 *cookiep = &ihl->intr[i];
545 return (ret > 0)? EINVAL : 0;
549 sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
552 struct sbc_softc *scp = device_get_softc(dev);
553 struct sbc_ihl *ihl = NULL;
558 while (i < IRQ_MAX) {
559 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
563 if (ihl == NULL) ret = EINVAL;
565 while ((ret == 0) && (i < INTR_MAX)) {
566 if (cookie == &ihl->intr[i]) {
568 ihl->intr_arg[i] = NULL;
573 return (ret > 0)? EINVAL : 0;
576 static struct resource *
577 sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
578 u_long start, u_long end, u_long count, u_int flags)
580 struct sbc_softc *scp;
581 int *alloced, rid_max, alloced_max;
582 struct resource **res;
587 scp = device_get_softc(bus);
590 alloced = scp->io_alloced;
594 for (i = 0; i < IO_MAX; i++)
595 rid_max += io_range[i];
597 rid_max = IO_MAX - 1;
602 alloced = scp->drq_alloced;
604 rid_max = DRQ_MAX - 1;
608 alloced = scp->irq_alloced;
610 rid_max = IRQ_MAX - 1;
611 alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */
617 if (*rid > rid_max || alloced[*rid] == alloced_max)
625 sbc_release_resource(device_t bus, device_t child, int type, int rid,
628 struct sbc_softc *scp;
629 int *alloced, rid_max;
631 scp = device_get_softc(bus);
634 alloced = scp->io_alloced;
635 rid_max = IO_MAX - 1;
638 alloced = scp->drq_alloced;
639 rid_max = DRQ_MAX - 1;
642 alloced = scp->irq_alloced;
643 rid_max = IRQ_MAX - 1;
649 if (rid > rid_max || alloced[rid] == 0)
657 sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
659 struct sbc_softc *scp = device_get_softc(bus);
660 struct sndcard_func *func = device_get_ivars(dev);
664 *result = func->func;
668 *result = scp->bd_ver;
679 sbc_write_ivar(device_t bus, device_t dev,
680 int index, uintptr_t value)
693 alloc_resource(struct sbc_softc *scp)
697 for (i = 0 ; i < IO_MAX ; i++) {
698 if (scp->io[i] == NULL) {
700 scp->io_rid[i] = i > 0 ?
701 scp->io_rid[i - 1] + io_range[i - 1] : 0;
702 scp->io[i] = isa_alloc_resourcev(scp->dev,
708 if (scp->io[i] != NULL)
709 isa_load_resourcev(scp->io[i], sb_iat[i],
713 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
714 0, ~0, io_range[i], RF_ACTIVE);
716 if (i == 0 && scp->io[i] == NULL)
718 scp->io_alloced[i] = 0;
721 for (i = 0 ; i < DRQ_MAX ; i++) {
722 if (scp->drq[i] == NULL) {
724 scp->drq[i] = bus_alloc_resource_any(scp->dev,
728 if (i == 0 && scp->drq[i] == NULL)
730 scp->drq_alloced[i] = 0;
733 for (i = 0 ; i < IRQ_MAX ; i++) {
734 if (scp->irq[i] == NULL) {
736 scp->irq[i] = bus_alloc_resource_any(scp->dev,
740 if (i == 0 && scp->irq[i] == NULL)
742 scp->irq_alloced[i] = 0;
749 release_resource(struct sbc_softc *scp)
753 for (i = 0 ; i < IO_MAX ; i++) {
754 if (scp->io[i] != NULL) {
755 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
759 for (i = 0 ; i < DRQ_MAX ; i++) {
760 if (scp->drq[i] != NULL) {
761 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
765 for (i = 0 ; i < IRQ_MAX ; i++) {
766 if (scp->irq[i] != NULL) {
767 if (scp->ih[i] != NULL)
768 bus_teardown_intr(scp->dev, scp->irq[i], scp->ih[i]);
770 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]);
777 static device_method_t sbc_methods[] = {
778 /* Device interface */
779 DEVMETHOD(device_probe, sbc_probe),
780 DEVMETHOD(device_attach, sbc_attach),
781 DEVMETHOD(device_detach, sbc_detach),
782 DEVMETHOD(device_shutdown, bus_generic_shutdown),
783 DEVMETHOD(device_suspend, bus_generic_suspend),
784 DEVMETHOD(device_resume, bus_generic_resume),
787 DEVMETHOD(bus_read_ivar, sbc_read_ivar),
788 DEVMETHOD(bus_write_ivar, sbc_write_ivar),
789 DEVMETHOD(bus_print_child, bus_generic_print_child),
790 DEVMETHOD(bus_alloc_resource, sbc_alloc_resource),
791 DEVMETHOD(bus_release_resource, sbc_release_resource),
792 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
793 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
794 DEVMETHOD(bus_setup_intr, sbc_setup_intr),
795 DEVMETHOD(bus_teardown_intr, sbc_teardown_intr),
800 static driver_t sbc_driver = {
803 sizeof(struct sbc_softc),
806 /* sbc can be attached to an isa bus. */
807 DRIVER_MODULE(snd_sbc, isa, sbc_driver, sbc_devclass, 0, 0);
808 DRIVER_MODULE(snd_sbc, acpi, sbc_driver, sbc_devclass, 0, 0);
809 MODULE_DEPEND(snd_sbc, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
810 MODULE_VERSION(snd_sbc, 1);