2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Copyright (c) 1997,1998 Luigi Rizzo
7 * Derived from files in the Voxware 3.5 distribution,
8 * Copyright by Hannu Savolainen 1994, under the same copyright
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
38 #include <dev/sound/pcm/sound.h>
40 #include <dev/sound/isa/sb.h>
41 #include <dev/sound/chip.h>
43 #include <isa/isavar.h>
47 SND_DECLARE_FILE("$FreeBSD$");
49 #define SB_DEFAULT_BUFSZ 4096
51 static u_int32_t sb_fmt[] = {
52 SND_FORMAT(AFMT_U8, 1, 0),
55 static struct pcmchan_caps sb200_playcaps = {4000, 23000, sb_fmt, 0};
56 static struct pcmchan_caps sb200_reccaps = {4000, 13000, sb_fmt, 0};
57 static struct pcmchan_caps sb201_playcaps = {4000, 44100, sb_fmt, 0};
58 static struct pcmchan_caps sb201_reccaps = {4000, 15000, sb_fmt, 0};
60 static u_int32_t sbpro_fmt[] = {
61 SND_FORMAT(AFMT_U8, 1, 0),
62 SND_FORMAT(AFMT_U8, 2, 0),
65 static struct pcmchan_caps sbpro_playcaps = {4000, 44100, sbpro_fmt, 0};
66 static struct pcmchan_caps sbpro_reccaps = {4000, 44100, sbpro_fmt, 0};
71 struct sb_info *parent;
72 struct pcm_channel *channel;
73 struct snd_dbuf *buffer;
75 u_int32_t fmt, spd, blksz;
80 struct resource *io_base; /* I/O address for the board */
84 bus_dma_tag_t parent_dmat;
88 u_long bd_flags; /* board-specific flags */
89 struct sb_chinfo pch, rch;
92 static int sb_rd(struct sb_info *sb, int reg);
93 static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
94 static int sb_dspready(struct sb_info *sb);
95 static int sb_cmd(struct sb_info *sb, u_char val);
96 static int sb_cmd1(struct sb_info *sb, u_char cmd, int val);
97 static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
98 static u_int sb_get_byte(struct sb_info *sb);
99 static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
100 static int sb_getmixer(struct sb_info *sb, u_int port);
101 static int sb_reset_dsp(struct sb_info *sb);
103 static void sb_intr(void *arg);
104 static int sb_speed(struct sb_chinfo *ch);
105 static int sb_start(struct sb_chinfo *ch);
106 static int sb_stop(struct sb_chinfo *ch);
109 * Common code for the midi and pcm functions
111 * sb_cmd write a single byte to the CMD port.
112 * sb_cmd1 write a CMD + 1 byte arg
113 * sb_cmd2 write a CMD + 2 byte arg
114 * sb_get_byte returns a single byte from the DSP data port
118 sb_lock(struct sb_info *sb) {
120 sbc_lock(device_get_softc(sb->parent_dev));
124 sb_unlock(struct sb_info *sb) {
126 sbc_unlock(device_get_softc(sb->parent_dev));
130 port_rd(struct resource *port, int off)
132 return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off);
136 port_wr(struct resource *port, int off, u_int8_t data)
138 bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data);
142 sb_rd(struct sb_info *sb, int reg)
144 return port_rd(sb->io_base, reg);
148 sb_wr(struct sb_info *sb, int reg, u_int8_t val)
150 port_wr(sb->io_base, reg, val);
154 sb_dspready(struct sb_info *sb)
156 return ((sb_rd(sb, SBDSP_STATUS) & 0x80) == 0);
160 sb_dspwr(struct sb_info *sb, u_char val)
164 for (i = 0; i < 1000; i++) {
165 if (sb_dspready(sb)) {
166 sb_wr(sb, SBDSP_CMD, val);
169 if (i > 10) DELAY((i > 100)? 1000 : 10);
171 printf("sb_dspwr(0x%02x) timed out.\n", val);
176 sb_cmd(struct sb_info *sb, u_char val)
179 printf("sb_cmd: %x\n", val);
181 return sb_dspwr(sb, val);
185 sb_cmd1(struct sb_info *sb, u_char cmd, int val)
188 printf("sb_cmd1: %x, %x\n", cmd, val);
190 if (sb_dspwr(sb, cmd)) {
191 return sb_dspwr(sb, val & 0xff);
196 sb_cmd2(struct sb_info *sb, u_char cmd, int val)
199 printf("sb_cmd2: %x, %x\n", cmd, val);
201 if (sb_dspwr(sb, cmd)) {
202 return sb_dspwr(sb, val & 0xff) &&
203 sb_dspwr(sb, (val >> 8) & 0xff);
208 * in the SB, there is a set of indirect "mixer" registers with
209 * address at offset 4, data at offset 5
211 * we don't need to interlock these, the mixer lock will suffice.
214 sb_setmixer(struct sb_info *sb, u_int port, u_int value)
216 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
218 sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
223 sb_getmixer(struct sb_info *sb, u_int port)
227 sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
229 val = sb_rd(sb, SB_MIX_DATA);
236 sb_get_byte(struct sb_info *sb)
240 for (i = 1000; i > 0; i--) {
241 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
242 return sb_rd(sb, DSP_READ);
250 sb_reset_dsp(struct sb_info *sb)
252 sb_wr(sb, SBDSP_RST, 3);
254 sb_wr(sb, SBDSP_RST, 0);
255 if (sb_get_byte(sb) != 0xAA) {
256 DEB(printf("sb_reset_dsp 0x%lx failed\n",
257 rman_get_start(sb->io_base)));
258 return ENXIO; /* Sorry */
264 sb_release_resources(struct sb_info *sb, device_t dev)
268 bus_teardown_intr(dev, sb->irq, sb->ih);
269 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
273 isa_dma_release(rman_get_start(sb->drq));
274 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq);
278 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
281 if (sb->parent_dmat) {
282 bus_dma_tag_destroy(sb->parent_dmat);
289 sb_alloc_resources(struct sb_info *sb, device_t dev)
295 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
299 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
303 sb->drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
306 if (sb->io_base && sb->drq && sb->irq) {
307 isa_dma_acquire(rman_get_start(sb->drq));
308 isa_dmainit(rman_get_start(sb->drq), sb->bufsize);
314 /************************************************************/
317 sbpromix_init(struct snd_mixer *m)
319 struct sb_info *sb = mix_getdevinfo(m);
321 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
322 SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME);
324 mix_setrecdevs(m, SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD);
326 sb_setmixer(sb, 0, 1); /* reset mixer */
332 sbpromix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
334 struct sb_info *sb = mix_getdevinfo(m);
340 case SOUND_MIXER_PCM:
344 case SOUND_MIXER_MIC:
349 case SOUND_MIXER_VOLUME:
353 case SOUND_MIXER_SYNTH:
361 case SOUND_MIXER_LINE:
369 left = (left * max) / 100;
370 right = (dev == SOUND_MIXER_MIC)? left : ((right * max) / 100);
372 val = (dev == SOUND_MIXER_MIC)? (left << 1) : (left << 5 | right << 1);
373 sb_setmixer(sb, reg, val);
375 left = (left * 100) / max;
376 right = (right * 100) / max;
378 return left | (right << 8);
382 sbpromix_setrecsrc(struct snd_mixer *m, u_int32_t src)
384 struct sb_info *sb = mix_getdevinfo(m);
387 if (src == SOUND_MASK_LINE)
389 else if (src == SOUND_MASK_CD)
391 else { /* default: mic */
392 src = SOUND_MASK_MIC;
395 sb_setmixer(sb, RECORD_SRC, recdev | (sb_getmixer(sb, RECORD_SRC) & ~0x07));
400 static kobj_method_t sbpromix_mixer_methods[] = {
401 KOBJMETHOD(mixer_init, sbpromix_init),
402 KOBJMETHOD(mixer_set, sbpromix_set),
403 KOBJMETHOD(mixer_setrecsrc, sbpromix_setrecsrc),
406 MIXER_DECLARE(sbpromix_mixer);
408 /************************************************************/
411 sbmix_init(struct snd_mixer *m)
413 struct sb_info *sb = mix_getdevinfo(m);
415 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_CD | SOUND_MASK_VOLUME);
417 mix_setrecdevs(m, 0);
419 sb_setmixer(sb, 0, 1); /* reset mixer */
425 sbmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
427 struct sb_info *sb = mix_getdevinfo(m);
432 case SOUND_MIXER_VOLUME:
436 case SOUND_MIXER_SYNTH:
444 case SOUND_MIXER_PCM:
453 left = (left * max) / 100;
455 sb_setmixer(sb, reg, left << 1);
457 left = (left * 100) / max;
459 return left | (left << 8);
463 sbmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
468 static kobj_method_t sbmix_mixer_methods[] = {
469 KOBJMETHOD(mixer_init, sbmix_init),
470 KOBJMETHOD(mixer_set, sbmix_set),
471 KOBJMETHOD(mixer_setrecsrc, sbmix_setrecsrc),
474 MIXER_DECLARE(sbmix_mixer);
476 /************************************************************/
481 struct sb_info *sb = (struct sb_info *)arg;
484 if (sndbuf_runsz(sb->pch.buffer) > 0) {
486 chn_intr(sb->pch.channel);
490 if (sndbuf_runsz(sb->rch.buffer) > 0) {
492 chn_intr(sb->rch.channel);
496 sb_rd(sb, DSP_DATA_AVAIL); /* int ack */
501 sb_speed(struct sb_chinfo *ch)
503 struct sb_info *sb = ch->parent;
504 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
505 int stereo = (AFMT_CHANNEL(ch->fmt) > 1)? 1 : 0;
506 int speed, tmp, thresh, max;
509 if (sb->bd_id >= 0x300) {
510 thresh = stereo? 11025 : 23000;
511 max = stereo? 22050 : 44100;
512 } else if (sb->bd_id > 0x200) {
513 thresh = play? 23000 : 13000;
514 max = play? 44100 : 15000;
517 max = play? 23000 : 13000;
525 sb->bd_flags &= ~BD_F_HISPEED;
527 sb->bd_flags |= BD_F_HISPEED;
529 tmp = 65536 - (256000000 / (speed << stereo));
532 sb_cmd1(sb, 0x40, tconst); /* set time constant */
534 speed = (256000000 / (65536 - tmp)) >> stereo;
542 sb_start(struct sb_chinfo *ch)
544 struct sb_info *sb = ch->parent;
545 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
546 int stereo = (AFMT_CHANNEL(ch->fmt) > 1)? 1 : 0;
554 sb_cmd(sb, DSP_CMD_SPKON);
556 if (sb->bd_flags & BD_F_HISPEED)
557 i = play? 0x90 : 0x98;
559 i = play? 0x1c : 0x2c;
561 sb_setmixer(sb, 0x0e, stereo? 2 : 0);
562 sb_cmd2(sb, 0x48, l);
565 sb->bd_flags |= BD_F_DMARUN;
571 sb_stop(struct sb_chinfo *ch)
573 struct sb_info *sb = ch->parent;
574 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
577 if (sb->bd_flags & BD_F_HISPEED)
582 * NOTE: DSP_CMD_DMAEXIT_8 does not work with old
585 sb_cmd(sb, DSP_CMD_DMAEXIT_8);
591 sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
593 sb->bd_flags &= ~BD_F_DMARUN;
597 /* channel interface */
599 sbchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
601 struct sb_info *sb = devinfo;
602 struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
608 if (sndbuf_alloc(ch->buffer, sb->parent_dmat, 0, sb->bufsize) != 0)
610 sndbuf_dmasetup(ch->buffer, sb->drq);
615 sbchan_setformat(kobj_t obj, void *data, u_int32_t format)
617 struct sb_chinfo *ch = data;
624 sbchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
626 struct sb_chinfo *ch = data;
633 sbchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
635 struct sb_chinfo *ch = data;
637 ch->blksz = blocksize;
642 sbchan_trigger(kobj_t obj, void *data, int go)
644 struct sb_chinfo *ch = data;
646 if (!PCMTRIG_COMMON(go))
649 sndbuf_dma(ch->buffer, go);
650 if (go == PCMTRIG_START)
658 sbchan_getptr(kobj_t obj, void *data)
660 struct sb_chinfo *ch = data;
662 return sndbuf_dmaptr(ch->buffer);
665 static struct pcmchan_caps *
666 sbchan_getcaps(kobj_t obj, void *data)
668 struct sb_chinfo *ch = data;
669 int p = (ch->dir == PCMDIR_PLAY)? 1 : 0;
671 if (ch->parent->bd_id == 0x200)
672 return p? &sb200_playcaps : &sb200_reccaps;
673 if (ch->parent->bd_id < 0x300)
674 return p? &sb201_playcaps : &sb201_reccaps;
675 return p? &sbpro_playcaps : &sbpro_reccaps;
678 static kobj_method_t sbchan_methods[] = {
679 KOBJMETHOD(channel_init, sbchan_init),
680 KOBJMETHOD(channel_setformat, sbchan_setformat),
681 KOBJMETHOD(channel_setspeed, sbchan_setspeed),
682 KOBJMETHOD(channel_setblocksize, sbchan_setblocksize),
683 KOBJMETHOD(channel_trigger, sbchan_trigger),
684 KOBJMETHOD(channel_getptr, sbchan_getptr),
685 KOBJMETHOD(channel_getcaps, sbchan_getcaps),
688 CHANNEL_DECLARE(sbchan);
690 /************************************************************/
693 sb_probe(device_t dev)
696 uintptr_t func, ver, r, f;
698 /* The parent device has already been probed. */
699 r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
703 r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
704 f = (ver & 0xffff0000) >> 16;
706 if ((f & BD_F_ESS) || (ver >= 0x400))
709 snprintf(buf, sizeof buf, "SB DSP %d.%02d", (int) ver >> 8, (int) ver & 0xff);
711 device_set_desc_copy(dev, buf);
717 sb_attach(device_t dev)
720 char status[SND_STATUSLEN];
723 sb = malloc(sizeof(*sb), M_DEVBUF, M_WAITOK | M_ZERO);
724 sb->parent_dev = device_get_parent(dev);
725 BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
726 sb->bd_id = ver & 0x0000ffff;
727 sb->bd_flags = (ver & 0xffff0000) >> 16;
728 sb->bufsize = pcm_getbuffersize(dev, 4096, SB_DEFAULT_BUFSZ, 65536);
730 if (sb_alloc_resources(sb, dev))
732 if (sb_reset_dsp(sb))
734 if (mixer_init(dev, (sb->bd_id < 0x300)? &sbmix_mixer_class : &sbpromix_mixer_class, sb))
736 if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih))
739 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
741 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
743 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
744 /*highaddr*/BUS_SPACE_MAXADDR,
745 /*filter*/NULL, /*filterarg*/NULL,
746 /*maxsize*/sb->bufsize, /*nsegments*/1,
747 /*maxsegz*/0x3ffff, /*flags*/0,
748 /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
749 &sb->parent_dmat) != 0) {
750 device_printf(dev, "unable to create dma tag\n");
754 snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd drq %jd bufsz %u %s",
755 rman_get_start(sb->io_base), rman_get_start(sb->irq),
756 rman_get_start(sb->drq), sb->bufsize, PCM_KLDSTRING(snd_sb8));
758 if (pcm_register(dev, sb, 1, 1))
760 pcm_addchan(dev, PCMDIR_REC, &sbchan_class, sb);
761 pcm_addchan(dev, PCMDIR_PLAY, &sbchan_class, sb);
763 pcm_setstatus(dev, status);
768 sb_release_resources(sb, dev);
773 sb_detach(device_t dev)
778 r = pcm_unregister(dev);
782 sb = pcm_getdevinfo(dev);
783 sb_release_resources(sb, dev);
787 static device_method_t sb_methods[] = {
788 /* Device interface */
789 DEVMETHOD(device_probe, sb_probe),
790 DEVMETHOD(device_attach, sb_attach),
791 DEVMETHOD(device_detach, sb_detach),
796 static driver_t sb_driver = {
802 DRIVER_MODULE(snd_sb8, sbc, sb_driver, pcm_devclass, 0, 0);
803 MODULE_DEPEND(snd_sb8, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
804 MODULE_DEPEND(snd_sb8, snd_sbc, 1, 1, 1);
805 MODULE_VERSION(snd_sb8, 1);