2 * Copyright (c) 2001 Orion Hodson <O.Hodson@cs.ucl.ac.uk>
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
26 * This card has the annoying habit of "clicking" when attached and
27 * detached, haven't been able to remedy this with any combination of
31 #include <dev/sound/pcm/sound.h>
32 #include <dev/sound/pci/vibes.h>
34 #include <dev/pci/pcireg.h>
35 #include <dev/pci/pcivar.h>
39 SND_DECLARE_FILE("$FreeBSD$");
41 /* ------------------------------------------------------------------------- */
44 #define SV_PCI_ID 0xca005333
45 #define SV_DEFAULT_BUFSZ 16384
46 #define SV_MIN_BLKSZ 128
47 #define SV_INTR_PER_BUFFER 2
50 #define DEB(x) /* (x) */
53 /* ------------------------------------------------------------------------- */
59 struct sc_info *parent;
60 struct pcm_channel *channel;
61 struct snd_dbuf *buffer;
64 int dma_active, dma_was_active;
70 /* DMA buffer allocator */
71 bus_dma_tag_t parent_dmat;
73 /* Enhanced register resources */
74 struct resource *enh_reg;
75 bus_space_tag_t enh_st;
76 bus_space_handle_t enh_sh;
80 /* DMA configuration */
81 struct resource *dmaa_reg, *dmac_reg;
82 bus_space_tag_t dmaa_st, dmac_st;
83 bus_space_handle_t dmaa_sh, dmac_sh;
84 int dmaa_type, dmac_type;
85 int dmaa_rid, dmac_rid;
87 /* Interrupt resources */
92 /* User configurable buffer size */
95 struct sc_chinfo rch, pch;
99 static u_int32_t sc_fmt[] = {
101 AFMT_U8 | AFMT_STEREO,
103 AFMT_S16_LE | AFMT_STEREO,
107 static struct pcmchan_caps sc_caps = {8000, 48000, sc_fmt, 0};
109 /* ------------------------------------------------------------------------- */
110 /* Register Manipulations */
112 #define sv_direct_set(x, y, z) _sv_direct_set(x, y, z, __LINE__)
115 sv_direct_get(struct sc_info *sc, u_int8_t reg)
117 return bus_space_read_1(sc->enh_st, sc->enh_sh, reg);
121 _sv_direct_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line)
124 bus_space_write_1(sc->enh_st, sc->enh_sh, reg, val);
126 n = sv_direct_get(sc, reg);
128 device_printf(sc->dev, "sv_direct_set register 0x%02x %d != %d from line %d\n", reg, n, val, line);
133 sv_indirect_get(struct sc_info *sc, u_int8_t reg)
135 if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR)
136 reg |= SV_CM_INDEX_MCE;
138 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg);
139 return bus_space_read_1(sc->enh_st, sc->enh_sh, SV_CM_DATA);
142 #define sv_indirect_set(x, y, z) _sv_indirect_set(x, y, z, __LINE__)
145 _sv_indirect_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line)
147 if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR)
148 reg |= SV_CM_INDEX_MCE;
150 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg);
151 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_DATA, val);
153 reg &= ~SV_CM_INDEX_MCE;
154 if (reg != SV_REG_ADC_PLLM) {
156 n = sv_indirect_get(sc, reg);
158 device_printf(sc->dev, "sv_indirect_set register 0x%02x %d != %d line %d\n", reg, n, val, line);
164 sv_dma_set_config(bus_space_tag_t st, bus_space_handle_t sh,
165 u_int32_t base, u_int32_t count, u_int8_t mode)
167 bus_space_write_4(st, sh, SV_DMA_ADDR, base);
168 bus_space_write_4(st, sh, SV_DMA_COUNT, count & 0xffffff);
169 bus_space_write_1(st, sh, SV_DMA_MODE, mode);
171 DEB(printf("base 0x%08x count %5d mode 0x%02x\n",
176 sv_dma_get_count(bus_space_tag_t st, bus_space_handle_t sh)
178 return bus_space_read_4(st, sh, SV_DMA_COUNT) & 0xffffff;
181 /* ------------------------------------------------------------------------- */
182 /* Play / Record Common Interface */
185 svchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
187 struct sc_info *sc = devinfo;
188 struct sc_chinfo *ch;
189 ch = (dir == PCMDIR_PLAY) ? &sc->pch : &sc->rch;
195 if (sndbuf_alloc(b, sc->parent_dmat, 0, sc->bufsz) != 0) {
196 DEB(printf("svchan_init failed\n"));
201 ch->spd = DSP_DEFAULT_SPEED;
202 ch->dma_active = ch->dma_was_active = 0;
207 static struct pcmchan_caps *
208 svchan_getcaps(kobj_t obj, void *data)
214 svchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
216 struct sc_chinfo *ch = data;
217 struct sc_info *sc = ch->parent;
219 /* user has requested interrupts every blocksize bytes */
220 RANGE(blocksize, SV_MIN_BLKSZ, sc->bufsz / SV_INTR_PER_BUFFER);
221 sndbuf_resize(ch->buffer, SV_INTR_PER_BUFFER, blocksize);
222 DEB(printf("svchan_setblocksize: %d\n", blocksize));
227 svchan_setformat(kobj_t obj, void *data, u_int32_t format)
229 struct sc_chinfo *ch = data;
230 /* NB Just note format here as setting format register
231 * generates noise if dma channel is inactive. */
232 ch->fmt = (format & AFMT_STEREO) ? SV_AFMT_STEREO : SV_AFMT_MONO;
233 ch->fmt |= (format & AFMT_16BIT) ? SV_AFMT_S16 : SV_AFMT_U8;
238 svchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
240 struct sc_chinfo *ch = data;
241 RANGE(speed, 8000, 48000);
246 /* ------------------------------------------------------------------------- */
247 /* Recording interface */
250 sv_set_recspeed(struct sc_info *sc, u_int32_t speed)
252 u_int32_t f_out, f_actual;
253 u_int32_t rs, re, r, best_r = 0, r2, t, n, best_n = 0;
254 int32_t m, best_m = 0, ms, me, err, min_err;
256 /* This algorithm is a variant described in sonicvibes.pdf
257 * appendix A. This search is marginally more extensive and
258 * results in (nominally) better sample rate matching. */
260 f_out = SV_F_SCALE * speed;
261 min_err = 0x7fffffff;
263 /* Find bounds of r to examine, rs <= r <= re */
264 t = 80000000 / f_out;
265 for (rs = 1; (1 << rs) < t; rs++);
267 t = 150000000 / f_out;
268 for (re = 1; (2 << re) < t; re++);
271 /* Search over r, n, m */
272 for (r = rs; r <= re; r++) {
274 for (n = 3; n < 34; n++) {
275 m = f_out * n / (SV_F_REF / r2);
276 ms = (m > 3) ? (m - 1) : 3;
277 me = (m < 129) ? (m + 1) : 129;
278 for (m = ms; m <= me; m++) {
279 f_actual = m * SV_F_REF / (n * r2);
280 if (f_actual > f_out) {
281 err = f_actual - f_out;
283 err = f_out - f_actual;
296 sv_indirect_set(sc, SV_REG_ADC_PLLM, best_m);
297 sv_indirect_set(sc, SV_REG_ADC_PLLN,
298 SV_ADC_PLLN(best_n) | SV_ADC_PLLR(best_r));
299 DEB(printf("svrchan_setspeed: %d -> PLLM 0x%02x PLLNR 0x%08x\n",
301 sv_indirect_get(sc, SV_REG_ADC_PLLM),
302 sv_indirect_get(sc, SV_REG_ADC_PLLN)));
307 svrchan_trigger(kobj_t obj, void *data, int go)
309 struct sc_chinfo *ch = data;
310 struct sc_info *sc = ch->parent;
311 u_int32_t count, enable;
317 sv_set_recspeed(sc, ch->spd);
320 v = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAC_MSK;
321 v |= SV_AFMT_DMAC(ch->fmt);
322 sv_indirect_set(sc, SV_REG_FORMAT, v);
325 count = sndbuf_getsize(ch->buffer) / 2; /* DMAC uses words */
326 sv_dma_set_config(sc->dmac_st, sc->dmac_sh,
327 sndbuf_getbufaddr(ch->buffer),
329 SV_DMA_MODE_AUTO | SV_DMA_MODE_RD);
330 count = count / SV_INTR_PER_BUFFER - 1;
331 sv_indirect_set(sc, SV_REG_DMAC_COUNT_HI, count >> 8);
332 sv_indirect_set(sc, SV_REG_DMAC_COUNT_LO, count & 0xff);
335 enable = sv_indirect_get(sc, SV_REG_ENABLE) | SV_RECORD_ENABLE;
336 sv_indirect_set(sc, SV_REG_ENABLE, enable);
341 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_RECORD_ENABLE;
342 sv_indirect_set(sc, SV_REG_ENABLE, enable);
351 svrchan_getptr(kobj_t obj, void *data)
353 struct sc_chinfo *ch = data;
354 struct sc_info *sc = ch->parent;
355 u_int32_t sz, remain;
357 sz = sndbuf_getsize(ch->buffer);
358 /* DMAC uses words */
359 remain = (sv_dma_get_count(sc->dmac_st, sc->dmac_sh) + 1) * 2;
363 static kobj_method_t svrchan_methods[] = {
364 KOBJMETHOD(channel_init, svchan_init),
365 KOBJMETHOD(channel_setformat, svchan_setformat),
366 KOBJMETHOD(channel_setspeed, svchan_setspeed),
367 KOBJMETHOD(channel_setblocksize, svchan_setblocksize),
368 KOBJMETHOD(channel_trigger, svrchan_trigger),
369 KOBJMETHOD(channel_getptr, svrchan_getptr),
370 KOBJMETHOD(channel_getcaps, svchan_getcaps),
373 CHANNEL_DECLARE(svrchan);
375 /* ------------------------------------------------------------------------- */
376 /* Playback interface */
379 svpchan_trigger(kobj_t obj, void *data, int go)
381 struct sc_chinfo *ch = data;
382 struct sc_info *sc = ch->parent;
383 u_int32_t count, enable, speed;
389 speed = (ch->spd * 65536) / 48000;
392 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_HI, speed >> 8);
393 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_LO, speed & 0xff);
396 v = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAA_MSK;
397 v |= SV_AFMT_DMAA(ch->fmt);
398 sv_indirect_set(sc, SV_REG_FORMAT, v);
401 count = sndbuf_getsize(ch->buffer);
402 sv_dma_set_config(sc->dmaa_st, sc->dmaa_sh,
403 sndbuf_getbufaddr(ch->buffer),
405 SV_DMA_MODE_AUTO | SV_DMA_MODE_WR);
406 count = count / SV_INTR_PER_BUFFER - 1;
407 sv_indirect_set(sc, SV_REG_DMAA_COUNT_HI, count >> 8);
408 sv_indirect_set(sc, SV_REG_DMAA_COUNT_LO, count & 0xff);
411 enable = sv_indirect_get(sc, SV_REG_ENABLE);
412 enable = (enable | SV_PLAY_ENABLE) & ~SV_PLAYBACK_PAUSE;
413 sv_indirect_set(sc, SV_REG_ENABLE, enable);
418 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_PLAY_ENABLE;
419 sv_indirect_set(sc, SV_REG_ENABLE, enable);
428 svpchan_getptr(kobj_t obj, void *data)
430 struct sc_chinfo *ch = data;
431 struct sc_info *sc = ch->parent;
432 u_int32_t sz, remain;
434 sz = sndbuf_getsize(ch->buffer);
435 /* DMAA uses bytes */
436 remain = sv_dma_get_count(sc->dmaa_st, sc->dmaa_sh) + 1;
437 return (sz - remain);
440 static kobj_method_t svpchan_methods[] = {
441 KOBJMETHOD(channel_init, svchan_init),
442 KOBJMETHOD(channel_setformat, svchan_setformat),
443 KOBJMETHOD(channel_setspeed, svchan_setspeed),
444 KOBJMETHOD(channel_setblocksize, svchan_setblocksize),
445 KOBJMETHOD(channel_trigger, svpchan_trigger),
446 KOBJMETHOD(channel_getptr, svpchan_getptr),
447 KOBJMETHOD(channel_getcaps, svchan_getcaps),
450 CHANNEL_DECLARE(svpchan);
452 /* ------------------------------------------------------------------------- */
455 struct sv_mix_props {
456 u_int8_t reg; /* Register */
457 u_int8_t stereo:1; /* Supports 2 channels */
458 u_int8_t mute:1; /* Supports muting */
459 u_int8_t neg:1; /* Negative gain */
460 u_int8_t max; /* Max gain */
461 u_int8_t iselect; /* Input selector */
462 } static const mt [SOUND_MIXER_NRDEVICES] = {
463 [SOUND_MIXER_LINE1] = {SV_REG_AUX1, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX1},
464 [SOUND_MIXER_CD] = {SV_REG_CD, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_CD},
465 [SOUND_MIXER_LINE] = {SV_REG_LINE, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_LINE},
466 [SOUND_MIXER_MIC] = {SV_REG_MIC, 0, 1, 1, SV_MIC_MAX, SV_INPUT_MIC},
467 [SOUND_MIXER_SYNTH] = {SV_REG_SYNTH, 0, 1, 1, SV_DEFAULT_MAX, 0},
468 [SOUND_MIXER_LINE2] = {SV_REG_AUX2, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX2},
469 [SOUND_MIXER_VOLUME] = {SV_REG_MIX, 1, 1, 1, SV_DEFAULT_MAX, 0},
470 [SOUND_MIXER_PCM] = {SV_REG_PCM, 1, 1, 1, SV_PCM_MAX, 0},
471 [SOUND_MIXER_RECLEV] = {SV_REG_ADC_INPUT, 1, 0, 0, SV_ADC_MAX, 0},
475 sv_channel_gain(struct sc_info *sc, u_int32_t dev, u_int32_t gain, u_int32_t channel)
480 g = mt[dev].max * gain / 100;
483 v = sv_indirect_get(sc, mt[dev].reg + channel) & ~mt[dev].max;
493 sv_indirect_set(sc, mt[dev].reg + channel, v);
497 sv_gain(struct sc_info *sc, u_int32_t dev, u_int32_t left, u_int32_t right)
499 sv_channel_gain(sc, dev, left, 0);
501 sv_channel_gain(sc, dev, right, 1);
506 sv_mix_mute_all(struct sc_info *sc)
509 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
510 if (mt[i].reg) sv_gain(sc, i, 0, 0);
515 sv_mix_init(struct snd_mixer *m)
519 for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
520 if (mt[i].max) v |= (1 << i);
524 for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
525 if (mt[i].iselect) v |= (1 << i);
527 mix_setrecdevs(m, v);
533 sv_mix_set(struct snd_mixer *m, u_int32_t dev, u_int32_t left, u_int32_t right)
535 struct sc_info *sc = mix_getdevinfo(m);
536 return sv_gain(sc, dev, left, right);
540 sv_mix_setrecsrc(struct snd_mixer *m, u_int32_t mask)
542 struct sc_info *sc = mix_getdevinfo(m);
545 v = sv_indirect_get(sc, SV_REG_ADC_INPUT) & SV_INPUT_GAIN_MASK;
546 for(i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
547 if ((1 << i) & mask) {
551 DEB(printf("sv_mix_setrecsrc: mask 0x%08x adc_input 0x%02x\n", mask, v));
552 sv_indirect_set(sc, SV_REG_ADC_INPUT, v);
556 static kobj_method_t sv_mixer_methods[] = {
557 KOBJMETHOD(mixer_init, sv_mix_init),
558 KOBJMETHOD(mixer_set, sv_mix_set),
559 KOBJMETHOD(mixer_setrecsrc, sv_mix_setrecsrc),
562 MIXER_DECLARE(sv_mixer);
564 /* ------------------------------------------------------------------------- */
565 /* Power management and reset */
568 sv_power(struct sc_info *sc, int state)
575 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) &~ SV_ANALOG_OFF;
576 v |= SV_ANALOG_OFF_SRS | SV_ANALOG_OFF_SPLL;
577 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v);
578 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) &~ SV_DIGITAL_OFF;
579 v |= SV_DIGITAL_OFF_SYN | SV_DIGITAL_OFF_MU | SV_DIGITAL_OFF_GP;
580 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, v);
584 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) | SV_ANALOG_OFF;
585 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v);
586 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) | SV_DIGITAL_OFF;
587 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, SV_DIGITAL_OFF);
590 DEB(printf("Power state %d\n", state));
594 sv_init(struct sc_info *sc)
599 v = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_ENHANCED;
600 v |= SV_CM_CONTROL_RESET;
601 sv_direct_set(sc, SV_CM_CONTROL, v);
604 v = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_RESET;
605 sv_direct_set(sc, SV_CM_CONTROL, v);
608 /* Set in enhanced mode */
609 v = sv_direct_get(sc, SV_CM_CONTROL);
610 v |= SV_CM_CONTROL_ENHANCED;
611 sv_direct_set(sc, SV_CM_CONTROL, v);
613 /* Enable interrupts (UDM and MIDM are superfluous) */
614 v = sv_direct_get(sc, SV_CM_IMR);
615 v &= ~(SV_CM_IMR_AMSK | SV_CM_IMR_CMSK | SV_CM_IMR_SMSK);
616 sv_direct_set(sc, SV_CM_IMR, v);
618 /* Select ADC PLL for ADC clock */
619 v = sv_indirect_get(sc, SV_REG_CLOCK_SOURCE) & ~SV_CLOCK_ALTERNATE;
620 sv_indirect_set(sc, SV_REG_CLOCK_SOURCE, v);
622 /* Disable loopback - binds ADC and DAC rates */
623 v = sv_indirect_get(sc, SV_REG_LOOPBACK) & ~SV_LOOPBACK_ENABLE;
624 sv_indirect_set(sc, SV_REG_LOOPBACK, v);
627 v = sv_indirect_get(sc, SV_REG_SRS_SPACE) | SV_SRS_DISABLED;
628 sv_indirect_set(sc, SV_REG_SRS_SPACE, v);
631 sc->rev = sv_indirect_get(sc, SV_REG_REVISION);
637 sv_suspend(device_t dev)
639 struct sc_info *sc = pcm_getdevinfo(dev);
641 sc->rch.dma_was_active = sc->rch.dma_active;
642 svrchan_trigger(NULL, &sc->rch, PCMTRIG_ABORT);
644 sc->pch.dma_was_active = sc->pch.dma_active;
645 svrchan_trigger(NULL, &sc->pch, PCMTRIG_ABORT);
654 sv_resume(device_t dev)
656 struct sc_info *sc = pcm_getdevinfo(dev);
660 if (sv_init(sc) == -1) {
661 device_printf(dev, "unable to reinitialize the card\n");
665 if (mixer_reinit(dev) == -1) {
666 device_printf(dev, "unable to reinitialize the mixer\n");
670 if (sc->rch.dma_was_active) {
671 svrchan_trigger(0, &sc->rch, PCMTRIG_START);
674 if (sc->pch.dma_was_active) {
675 svpchan_trigger(0, &sc->pch, PCMTRIG_START);
681 /* ------------------------------------------------------------------------- */
682 /* Resource related */
687 struct sc_info *sc = data;
690 status = sv_direct_get(sc, SV_CM_STATUS);
691 if (status & SV_CM_STATUS_AINT)
692 chn_intr(sc->pch.channel);
694 if (status & SV_CM_STATUS_CINT)
695 chn_intr(sc->rch.channel);
697 status &= ~(SV_CM_STATUS_AINT|SV_CM_STATUS_CINT);
698 DEB(if (status) printf("intr 0x%02x ?\n", status));
704 sv_probe(device_t dev)
706 switch(pci_get_devid(dev)) {
708 device_set_desc(dev, "S3 Sonicvibes");
709 return BUS_PROBE_DEFAULT;
716 sv_attach(device_t dev) {
719 char status[SND_STATUSLEN];
720 u_long midi_start, games_start, count, sdmaa, sdmac, ml, mu;
722 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
725 data = pci_read_config(dev, PCIR_COMMAND, 2);
726 data |= (PCIM_CMD_PORTEN|PCIM_CMD_BUSMASTEREN);
727 pci_write_config(dev, PCIR_COMMAND, data, 2);
728 data = pci_read_config(dev, PCIR_COMMAND, 2);
730 #if __FreeBSD_version > 500000
731 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
732 device_printf(dev, "chip is in D%d power mode "
733 "-- setting to D0\n", pci_get_powerstate(dev));
734 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
737 sc->enh_rid = SV_PCI_ENHANCED;
738 sc->enh_type = SYS_RES_IOPORT;
739 sc->enh_reg = bus_alloc_resource(dev, sc->enh_type,
741 SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
742 if (sc->enh_reg == NULL) {
743 device_printf(dev, "sv_attach: cannot allocate enh\n");
746 sc->enh_st = rman_get_bustag(sc->enh_reg);
747 sc->enh_sh = rman_get_bushandle(sc->enh_reg);
749 data = pci_read_config(dev, SV_PCI_DMAA, 4);
750 DEB(printf("sv_attach: initial dmaa 0x%08x\n", data));
751 data = pci_read_config(dev, SV_PCI_DMAC, 4);
752 DEB(printf("sv_attach: initial dmac 0x%08x\n", data));
754 /* Initialize DMA_A and DMA_C */
755 pci_write_config(dev, SV_PCI_DMAA, SV_PCI_DMA_EXTENDED, 4);
756 pci_write_config(dev, SV_PCI_DMAC, 0, 4);
758 /* Register IRQ handler */
760 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid,
761 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
763 snd_setup_intr(dev, sc->irq, 0, sv_intr, sc, &sc->ih)) {
764 device_printf(dev, "sv_attach: Unable to map interrupt\n");
768 sc->bufsz = pcm_getbuffersize(dev, 4096, SV_DEFAULT_BUFSZ, 65536);
769 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
771 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
772 /*highaddr*/BUS_SPACE_MAXADDR,
773 /*filter*/NULL, /*filterarg*/NULL,
774 /*maxsize*/sc->bufsz, /*nsegments*/1,
775 /*maxsegz*/0x3ffff, /*flags*/0,
776 /*lockfunc*/busdma_lock_mutex,
777 /*lockarg*/&Giant, &sc->parent_dmat) != 0) {
778 device_printf(dev, "sv_attach: Unable to create dma tag\n");
782 /* Power up and initialize */
787 if (mixer_init(dev, &sv_mixer_class, sc) != 0) {
788 device_printf(dev, "sv_attach: Mixer failed to initialize\n");
792 /* XXX This is a hack, and it's ugly. Okay, the deal is this
793 * card has two more io regions that available for automatic
794 * configuration by the pci code. These need to be allocated
795 * to used as control registers for the DMA engines.
796 * Unfortunately FBSD has no bus_space_foo() functions so we
797 * have to grab port space in region of existing resources. Go
798 * for space between midi and game ports.
800 bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_MIDI, &midi_start, &count);
801 bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_GAMES, &games_start, &count);
803 if (games_start < midi_start) {
810 /* Check assumptions about space availability and
811 alignment. How driver loaded can determine whether
812 games_start > midi_start or vice versa */
813 if ((mu - ml >= 0x800) ||
814 ((mu - ml) % 0x200)) {
815 device_printf(dev, "sv_attach: resource assumptions not met "
816 "(midi 0x%08lx, games 0x%08lx)\n",
817 midi_start, games_start);
822 sdmac = sdmaa + 0x40;
824 /* Add resources to list of pci resources for this device - from here on
825 * they look like normal pci resources. */
826 bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAA, sdmaa, SV_PCI_DMAA_SIZE);
827 bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAC, sdmac, SV_PCI_DMAC_SIZE);
829 /* Cache resource short-cuts for dma_a */
830 sc->dmaa_rid = SV_PCI_DMAA;
831 sc->dmaa_type = SYS_RES_IOPORT;
832 sc->dmaa_reg = bus_alloc_resource(dev, sc->dmaa_type,
833 &sc->dmaa_rid, 0, ~0,
834 SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
835 if (sc->dmaa_reg == NULL) {
836 device_printf(dev, "sv_attach: cannot allocate dmaa\n");
839 sc->dmaa_st = rman_get_bustag(sc->dmaa_reg);
840 sc->dmaa_sh = rman_get_bushandle(sc->dmaa_reg);
842 /* Poke port into dma_a configuration, nb bit flags to enable dma */
843 data = pci_read_config(dev, SV_PCI_DMAA, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED;
844 data = ((u_int32_t)sdmaa & 0xfffffff0) | (data & 0x0f);
845 pci_write_config(dev, SV_PCI_DMAA, data, 4);
846 DEB(printf("dmaa: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAA, 4)));
848 /* Cache resource short-cuts for dma_c */
849 sc->dmac_rid = SV_PCI_DMAC;
850 sc->dmac_type = SYS_RES_IOPORT;
851 sc->dmac_reg = bus_alloc_resource(dev, sc->dmac_type,
852 &sc->dmac_rid, 0, ~0,
853 SV_PCI_ENHANCED_SIZE, RF_ACTIVE);
854 if (sc->dmac_reg == NULL) {
855 device_printf(dev, "sv_attach: cannot allocate dmac\n");
858 sc->dmac_st = rman_get_bustag(sc->dmac_reg);
859 sc->dmac_sh = rman_get_bushandle(sc->dmac_reg);
861 /* Poke port into dma_c configuration, nb bit flags to enable dma */
862 data = pci_read_config(dev, SV_PCI_DMAC, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED;
863 data = ((u_int32_t)sdmac & 0xfffffff0) | (data & 0x0f);
864 pci_write_config(dev, SV_PCI_DMAC, data, 4);
865 DEB(printf("dmac: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAC, 4)));
868 printf("Sonicvibes: revision %d.\n", sc->rev);
870 if (pcm_register(dev, sc, 1, 1)) {
871 device_printf(dev, "sv_attach: pcm_register fail\n");
875 pcm_addchan(dev, PCMDIR_PLAY, &svpchan_class, sc);
876 pcm_addchan(dev, PCMDIR_REC, &svrchan_class, sc);
878 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s",
879 rman_get_start(sc->enh_reg), rman_get_start(sc->irq),PCM_KLDSTRING(snd_vibes));
880 pcm_setstatus(dev, status);
882 DEB(printf("sv_attach: succeeded\n"));
888 bus_dma_tag_destroy(sc->parent_dmat);
890 bus_teardown_intr(dev, sc->irq, sc->ih);
892 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
894 bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg);
896 bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg);
898 bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg);
903 sv_detach(device_t dev) {
907 r = pcm_unregister(dev);
910 sc = pcm_getdevinfo(dev);
914 bus_dma_tag_destroy(sc->parent_dmat);
915 bus_teardown_intr(dev, sc->irq, sc->ih);
916 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
917 bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg);
918 bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg);
919 bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg);
926 static device_method_t sc_methods[] = {
927 DEVMETHOD(device_probe, sv_probe),
928 DEVMETHOD(device_attach, sv_attach),
929 DEVMETHOD(device_detach, sv_detach),
930 DEVMETHOD(device_resume, sv_resume),
931 DEVMETHOD(device_suspend, sv_suspend),
935 static driver_t sonicvibes_driver = {
941 DRIVER_MODULE(snd_vibes, pci, sonicvibes_driver, pcm_devclass, 0, 0);
942 MODULE_DEPEND(snd_vibes, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
943 MODULE_VERSION(snd_vibes, 1);