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
30 * $FreeBSD: src/sys/dev/sound/pci/vibes.c,v 1.19.2.1 2006/01/24 18:54:22 joel Exp $
31 * $DragonFly: src/sys/dev/sound/pci/vibes.c,v 1.10 2007/01/04 21:47:02 corecode Exp $
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pci/vibes.h>
37 #include <bus/pci/pcireg.h>
38 #include <bus/pci/pcivar.h>
42 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/vibes.c,v 1.10 2007/01/04 21:47:02 corecode Exp $");
44 /* ------------------------------------------------------------------------- */
47 #define SV_PCI_ID 0xca005333
48 #define SV_DEFAULT_BUFSZ 16384
49 #define SV_MIN_BLKSZ 128
50 #define SV_INTR_PER_BUFFER 2
53 #define DEB(x) /* (x) */
56 /* ------------------------------------------------------------------------- */
62 struct sc_info
*parent
;
63 struct pcm_channel
*channel
;
64 struct snd_dbuf
*buffer
;
67 int dma_active
, dma_was_active
;
73 /* DMA buffer allocator */
74 bus_dma_tag_t parent_dmat
;
76 /* Enhanced register resources */
77 struct resource
*enh_reg
;
78 bus_space_tag_t enh_st
;
79 bus_space_handle_t enh_sh
;
83 /* DMA configuration */
84 struct resource
*dmaa_reg
, *dmac_reg
;
85 bus_space_tag_t dmaa_st
, dmac_st
;
86 bus_space_handle_t dmaa_sh
, dmac_sh
;
87 int dmaa_type
, dmac_type
;
88 int dmaa_rid
, dmac_rid
;
90 /* Interrupt resources */
95 /* User configurable buffer size */
98 struct sc_chinfo rch
, pch
;
102 static u_int32_t sc_fmt
[] = {
104 AFMT_U8
| AFMT_STEREO
,
106 AFMT_S16_LE
| AFMT_STEREO
,
110 static struct pcmchan_caps sc_caps
= {8000, 48000, sc_fmt
, 0};
112 /* ------------------------------------------------------------------------- */
113 /* Register Manipulations */
115 #define sv_direct_set(x, y, z) _sv_direct_set(x, y, z, __LINE__)
118 sv_direct_get(struct sc_info
*sc
, u_int8_t reg
)
120 return bus_space_read_1(sc
->enh_st
, sc
->enh_sh
, reg
);
124 _sv_direct_set(struct sc_info
*sc
, u_int8_t reg
, u_int8_t val
, int line
)
127 bus_space_write_1(sc
->enh_st
, sc
->enh_sh
, reg
, val
);
129 n
= sv_direct_get(sc
, reg
);
131 device_printf(sc
->dev
, "sv_direct_set register 0x%02x %d != %d from line %d\n", reg
, n
, val
, line
);
136 sv_indirect_get(struct sc_info
*sc
, u_int8_t reg
)
138 if (reg
== SV_REG_FORMAT
|| reg
== SV_REG_ANALOG_PWR
)
139 reg
|= SV_CM_INDEX_MCE
;
141 bus_space_write_1(sc
->enh_st
, sc
->enh_sh
, SV_CM_INDEX
, reg
);
142 return bus_space_read_1(sc
->enh_st
, sc
->enh_sh
, SV_CM_DATA
);
145 #define sv_indirect_set(x, y, z) _sv_indirect_set(x, y, z, __LINE__)
148 _sv_indirect_set(struct sc_info
*sc
, u_int8_t reg
, u_int8_t val
, int line
)
150 if (reg
== SV_REG_FORMAT
|| reg
== SV_REG_ANALOG_PWR
)
151 reg
|= SV_CM_INDEX_MCE
;
153 bus_space_write_1(sc
->enh_st
, sc
->enh_sh
, SV_CM_INDEX
, reg
);
154 bus_space_write_1(sc
->enh_st
, sc
->enh_sh
, SV_CM_DATA
, val
);
156 reg
&= ~SV_CM_INDEX_MCE
;
157 if (reg
!= SV_REG_ADC_PLLM
) {
159 n
= sv_indirect_get(sc
, reg
);
161 device_printf(sc
->dev
, "sv_indirect_set register 0x%02x %d != %d line %d\n", reg
, n
, val
, line
);
167 sv_dma_set_config(bus_space_tag_t st
, bus_space_handle_t sh
,
168 u_int32_t base
, u_int32_t count
, u_int8_t mode
)
170 bus_space_write_4(st
, sh
, SV_DMA_ADDR
, base
);
171 bus_space_write_4(st
, sh
, SV_DMA_COUNT
, count
& 0xffffff);
172 bus_space_write_1(st
, sh
, SV_DMA_MODE
, mode
);
174 DEB(kprintf("base 0x%08x count %5d mode 0x%02x\n",
179 sv_dma_get_count(bus_space_tag_t st
, bus_space_handle_t sh
)
181 return bus_space_read_4(st
, sh
, SV_DMA_COUNT
) & 0xffffff;
184 /* ------------------------------------------------------------------------- */
185 /* Play / Record Common Interface */
188 svchan_init(kobj_t obj
, void *devinfo
, struct snd_dbuf
*b
, struct pcm_channel
*c
, int dir
)
190 struct sc_info
*sc
= devinfo
;
191 struct sc_chinfo
*ch
;
192 ch
= (dir
== PCMDIR_PLAY
) ? &sc
->pch
: &sc
->rch
;
198 if (sndbuf_alloc(b
, sc
->parent_dmat
, sc
->bufsz
) != 0) {
199 DEB(kprintf("svchan_init failed\n"));
204 ch
->spd
= DSP_DEFAULT_SPEED
;
205 ch
->dma_active
= ch
->dma_was_active
= 0;
210 static struct pcmchan_caps
*
211 svchan_getcaps(kobj_t obj
, void *data
)
217 svchan_setblocksize(kobj_t obj
, void *data
, u_int32_t blocksize
)
219 struct sc_chinfo
*ch
= data
;
220 struct sc_info
*sc
= ch
->parent
;
222 /* user has requested interrupts every blocksize bytes */
223 RANGE(blocksize
, SV_MIN_BLKSZ
, sc
->bufsz
/ SV_INTR_PER_BUFFER
);
224 sndbuf_resize(ch
->buffer
, SV_INTR_PER_BUFFER
, blocksize
);
225 DEB(kprintf("svchan_setblocksize: %d\n", blocksize
));
230 svchan_setformat(kobj_t obj
, void *data
, u_int32_t format
)
232 struct sc_chinfo
*ch
= data
;
233 /* NB Just note format here as setting format register
234 * generates noise if dma channel is inactive. */
235 ch
->fmt
= (format
& AFMT_STEREO
) ? SV_AFMT_STEREO
: SV_AFMT_MONO
;
236 ch
->fmt
|= (format
& AFMT_16BIT
) ? SV_AFMT_S16
: SV_AFMT_U8
;
241 svchan_setspeed(kobj_t obj
, void *data
, u_int32_t speed
)
243 struct sc_chinfo
*ch
= data
;
244 RANGE(speed
, 8000, 48000);
249 /* ------------------------------------------------------------------------- */
250 /* Recording interface */
253 sv_set_recspeed(struct sc_info
*sc
, u_int32_t speed
)
255 u_int32_t f_out
, f_actual
;
256 u_int32_t rs
, re
, r
, best_r
= 0, r2
, t
, n
, best_n
= 0;
257 int32_t m
, best_m
= 0, ms
, me
, err
, min_err
;
259 /* This algorithm is a variant described in sonicvibes.pdf
260 * appendix A. This search is marginally more extensive and
261 * results in (nominally) better sample rate matching. */
263 f_out
= SV_F_SCALE
* speed
;
264 min_err
= 0x7fffffff;
266 /* Find bounds of r to examine, rs <= r <= re */
267 t
= 80000000 / f_out
;
268 for (rs
= 1; (1 << rs
) < t
; rs
++);
270 t
= 150000000 / f_out
;
271 for (re
= 1; (2 << re
) < t
; re
++);
274 /* Search over r, n, m */
275 for (r
= rs
; r
<= re
; r
++) {
277 for (n
= 3; n
< 34; n
++) {
278 m
= f_out
* n
/ (SV_F_REF
/ r2
);
279 ms
= (m
> 3) ? (m
- 1) : 3;
280 me
= (m
< 129) ? (m
+ 1) : 129;
281 for (m
= ms
; m
<= me
; m
++) {
282 f_actual
= m
* SV_F_REF
/ (n
* r2
);
283 if (f_actual
> f_out
) {
284 err
= f_actual
- f_out
;
286 err
= f_out
- f_actual
;
299 sv_indirect_set(sc
, SV_REG_ADC_PLLM
, best_m
);
300 sv_indirect_set(sc
, SV_REG_ADC_PLLN
,
301 SV_ADC_PLLN(best_n
) | SV_ADC_PLLR(best_r
));
302 DEB(kprintf("svrchan_setspeed: %d -> PLLM 0x%02x PLLNR 0x%08x\n",
304 sv_indirect_get(sc
, SV_REG_ADC_PLLM
),
305 sv_indirect_get(sc
, SV_REG_ADC_PLLN
)));
310 svrchan_trigger(kobj_t obj
, void *data
, int go
)
312 struct sc_chinfo
*ch
= data
;
313 struct sc_info
*sc
= ch
->parent
;
314 u_int32_t count
, enable
;
320 sv_set_recspeed(sc
, ch
->spd
);
323 v
= sv_indirect_get(sc
, SV_REG_FORMAT
) & ~SV_AFMT_DMAC_MSK
;
324 v
|= SV_AFMT_DMAC(ch
->fmt
);
325 sv_indirect_set(sc
, SV_REG_FORMAT
, v
);
328 count
= sndbuf_getsize(ch
->buffer
) / 2; /* DMAC uses words */
329 sv_dma_set_config(sc
->dmac_st
, sc
->dmac_sh
,
330 sndbuf_getbufaddr(ch
->buffer
),
332 SV_DMA_MODE_AUTO
| SV_DMA_MODE_RD
);
333 count
= count
/ SV_INTR_PER_BUFFER
- 1;
334 sv_indirect_set(sc
, SV_REG_DMAC_COUNT_HI
, count
>> 8);
335 sv_indirect_set(sc
, SV_REG_DMAC_COUNT_LO
, count
& 0xff);
338 enable
= sv_indirect_get(sc
, SV_REG_ENABLE
) | SV_RECORD_ENABLE
;
339 sv_indirect_set(sc
, SV_REG_ENABLE
, enable
);
343 enable
= sv_indirect_get(sc
, SV_REG_ENABLE
) & ~SV_RECORD_ENABLE
;
344 sv_indirect_set(sc
, SV_REG_ENABLE
, enable
);
353 svrchan_getptr(kobj_t obj
, void *data
)
355 struct sc_chinfo
*ch
= data
;
356 struct sc_info
*sc
= ch
->parent
;
357 u_int32_t sz
, remain
;
359 sz
= sndbuf_getsize(ch
->buffer
);
360 /* DMAC uses words */
361 remain
= (sv_dma_get_count(sc
->dmac_st
, sc
->dmac_sh
) + 1) * 2;
365 static kobj_method_t svrchan_methods
[] = {
366 KOBJMETHOD(channel_init
, svchan_init
),
367 KOBJMETHOD(channel_setformat
, svchan_setformat
),
368 KOBJMETHOD(channel_setspeed
, svchan_setspeed
),
369 KOBJMETHOD(channel_setblocksize
, svchan_setblocksize
),
370 KOBJMETHOD(channel_trigger
, svrchan_trigger
),
371 KOBJMETHOD(channel_getptr
, svrchan_getptr
),
372 KOBJMETHOD(channel_getcaps
, svchan_getcaps
),
375 CHANNEL_DECLARE(svrchan
);
377 /* ------------------------------------------------------------------------- */
378 /* Playback interface */
381 svpchan_trigger(kobj_t obj
, void *data
, int go
)
383 struct sc_chinfo
*ch
= data
;
384 struct sc_info
*sc
= ch
->parent
;
385 u_int32_t count
, enable
, speed
;
391 speed
= (ch
->spd
* 65536) / 48000;
394 sv_indirect_set(sc
, SV_REG_PCM_SAMPLING_HI
, speed
>> 8);
395 sv_indirect_set(sc
, SV_REG_PCM_SAMPLING_LO
, speed
& 0xff);
398 v
= sv_indirect_get(sc
, SV_REG_FORMAT
) & ~SV_AFMT_DMAA_MSK
;
399 v
|= SV_AFMT_DMAA(ch
->fmt
);
400 sv_indirect_set(sc
, SV_REG_FORMAT
, v
);
403 count
= sndbuf_getsize(ch
->buffer
);
404 sv_dma_set_config(sc
->dmaa_st
, sc
->dmaa_sh
,
405 sndbuf_getbufaddr(ch
->buffer
),
407 SV_DMA_MODE_AUTO
| SV_DMA_MODE_WR
);
408 count
= count
/ SV_INTR_PER_BUFFER
- 1;
409 sv_indirect_set(sc
, SV_REG_DMAA_COUNT_HI
, count
>> 8);
410 sv_indirect_set(sc
, SV_REG_DMAA_COUNT_LO
, count
& 0xff);
413 enable
= sv_indirect_get(sc
, SV_REG_ENABLE
);
414 enable
= (enable
| SV_PLAY_ENABLE
) & ~SV_PLAYBACK_PAUSE
;
415 sv_indirect_set(sc
, SV_REG_ENABLE
, enable
);
419 enable
= sv_indirect_get(sc
, SV_REG_ENABLE
) & ~SV_PLAY_ENABLE
;
420 sv_indirect_set(sc
, SV_REG_ENABLE
, enable
);
429 svpchan_getptr(kobj_t obj
, void *data
)
431 struct sc_chinfo
*ch
= data
;
432 struct sc_info
*sc
= ch
->parent
;
433 u_int32_t sz
, remain
;
435 sz
= sndbuf_getsize(ch
->buffer
);
436 /* DMAA uses bytes */
437 remain
= sv_dma_get_count(sc
->dmaa_st
, sc
->dmaa_sh
) + 1;
438 return (sz
- remain
);
441 static kobj_method_t svpchan_methods
[] = {
442 KOBJMETHOD(channel_init
, svchan_init
),
443 KOBJMETHOD(channel_setformat
, svchan_setformat
),
444 KOBJMETHOD(channel_setspeed
, svchan_setspeed
),
445 KOBJMETHOD(channel_setblocksize
, svchan_setblocksize
),
446 KOBJMETHOD(channel_trigger
, svpchan_trigger
),
447 KOBJMETHOD(channel_getptr
, svpchan_getptr
),
448 KOBJMETHOD(channel_getcaps
, svchan_getcaps
),
451 CHANNEL_DECLARE(svpchan
);
453 /* ------------------------------------------------------------------------- */
456 struct sv_mix_props
{
457 u_int8_t reg
; /* Register */
458 u_int8_t stereo
:1; /* Supports 2 channels */
459 u_int8_t mute
:1; /* Supports muting */
460 u_int8_t neg
:1; /* Negative gain */
461 u_int8_t max
; /* Max gain */
462 u_int8_t iselect
; /* Input selector */
463 } static const mt
[SOUND_MIXER_NRDEVICES
] = {
464 [SOUND_MIXER_LINE1
] = {SV_REG_AUX1
, 1, 1, 1, SV_DEFAULT_MAX
, SV_INPUT_AUX1
},
465 [SOUND_MIXER_CD
] = {SV_REG_CD
, 1, 1, 1, SV_DEFAULT_MAX
, SV_INPUT_CD
},
466 [SOUND_MIXER_LINE
] = {SV_REG_LINE
, 1, 1, 1, SV_DEFAULT_MAX
, SV_INPUT_LINE
},
467 [SOUND_MIXER_MIC
] = {SV_REG_MIC
, 0, 1, 1, SV_MIC_MAX
, SV_INPUT_MIC
},
468 [SOUND_MIXER_SYNTH
] = {SV_REG_SYNTH
, 0, 1, 1, SV_DEFAULT_MAX
, 0},
469 [SOUND_MIXER_LINE2
] = {SV_REG_AUX2
, 1, 1, 1, SV_DEFAULT_MAX
, SV_INPUT_AUX2
},
470 [SOUND_MIXER_VOLUME
] = {SV_REG_MIX
, 1, 1, 1, SV_DEFAULT_MAX
, 0},
471 [SOUND_MIXER_PCM
] = {SV_REG_PCM
, 1, 1, 1, SV_PCM_MAX
, 0},
472 [SOUND_MIXER_RECLEV
] = {SV_REG_ADC_INPUT
, 1, 0, 0, SV_ADC_MAX
, 0},
476 sv_channel_gain(struct sc_info
*sc
, u_int32_t dev
, u_int32_t gain
, u_int32_t channel
)
481 g
= mt
[dev
].max
* gain
/ 100;
484 v
= sv_indirect_get(sc
, mt
[dev
].reg
+ channel
) & ~mt
[dev
].max
;
494 sv_indirect_set(sc
, mt
[dev
].reg
+ channel
, v
);
498 sv_gain(struct sc_info
*sc
, u_int32_t dev
, u_int32_t left
, u_int32_t right
)
500 sv_channel_gain(sc
, dev
, left
, 0);
502 sv_channel_gain(sc
, dev
, right
, 1);
507 sv_mix_mute_all(struct sc_info
*sc
)
510 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
511 if (mt
[i
].reg
) sv_gain(sc
, i
, 0, 0);
516 sv_mix_init(struct snd_mixer
*m
)
520 for(i
= v
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
521 if (mt
[i
].max
) v
|= (1 << i
);
525 for(i
= v
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
526 if (mt
[i
].iselect
) v
|= (1 << i
);
528 mix_setrecdevs(m
, v
);
534 sv_mix_set(struct snd_mixer
*m
, u_int32_t dev
, u_int32_t left
, u_int32_t right
)
536 struct sc_info
*sc
= mix_getdevinfo(m
);
537 return sv_gain(sc
, dev
, left
, right
);
541 sv_mix_setrecsrc(struct snd_mixer
*m
, u_int32_t mask
)
543 struct sc_info
*sc
= mix_getdevinfo(m
);
546 v
= sv_indirect_get(sc
, SV_REG_ADC_INPUT
) & SV_INPUT_GAIN_MASK
;
547 for(i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
548 if ((1 << i
) & mask
) {
552 DEB(kprintf("sv_mix_setrecsrc: mask 0x%08x adc_input 0x%02x\n", mask
, v
));
553 sv_indirect_set(sc
, SV_REG_ADC_INPUT
, v
);
557 static kobj_method_t sv_mixer_methods
[] = {
558 KOBJMETHOD(mixer_init
, sv_mix_init
),
559 KOBJMETHOD(mixer_set
, sv_mix_set
),
560 KOBJMETHOD(mixer_setrecsrc
, sv_mix_setrecsrc
),
563 MIXER_DECLARE(sv_mixer
);
565 /* ------------------------------------------------------------------------- */
566 /* Power management and reset */
569 sv_power(struct sc_info
*sc
, int state
)
576 v
= sv_indirect_get(sc
, SV_REG_ANALOG_PWR
) &~ SV_ANALOG_OFF
;
577 v
|= SV_ANALOG_OFF_SRS
| SV_ANALOG_OFF_SPLL
;
578 sv_indirect_set(sc
, SV_REG_ANALOG_PWR
, v
);
579 v
= sv_indirect_get(sc
, SV_REG_DIGITAL_PWR
) &~ SV_DIGITAL_OFF
;
580 v
|= SV_DIGITAL_OFF_SYN
| SV_DIGITAL_OFF_MU
| SV_DIGITAL_OFF_GP
;
581 sv_indirect_set(sc
, SV_REG_DIGITAL_PWR
, v
);
585 v
= sv_indirect_get(sc
, SV_REG_ANALOG_PWR
) | SV_ANALOG_OFF
;
586 sv_indirect_set(sc
, SV_REG_ANALOG_PWR
, v
);
587 v
= sv_indirect_get(sc
, SV_REG_DIGITAL_PWR
) | SV_DIGITAL_OFF
;
588 sv_indirect_set(sc
, SV_REG_DIGITAL_PWR
, SV_DIGITAL_OFF
);
591 DEB(kprintf("Power state %d\n", state
));
595 sv_init(struct sc_info
*sc
)
600 v
= sv_direct_get(sc
, SV_CM_CONTROL
) & ~SV_CM_CONTROL_ENHANCED
;
601 v
|= SV_CM_CONTROL_RESET
;
602 sv_direct_set(sc
, SV_CM_CONTROL
, v
);
605 v
= sv_direct_get(sc
, SV_CM_CONTROL
) & ~SV_CM_CONTROL_RESET
;
606 sv_direct_set(sc
, SV_CM_CONTROL
, v
);
609 /* Set in enhanced mode */
610 v
= sv_direct_get(sc
, SV_CM_CONTROL
);
611 v
|= SV_CM_CONTROL_ENHANCED
;
612 sv_direct_set(sc
, SV_CM_CONTROL
, v
);
614 /* Enable interrupts (UDM and MIDM are superfluous) */
615 v
= sv_direct_get(sc
, SV_CM_IMR
);
616 v
&= ~(SV_CM_IMR_AMSK
| SV_CM_IMR_CMSK
| SV_CM_IMR_SMSK
);
617 sv_direct_set(sc
, SV_CM_IMR
, v
);
619 /* Select ADC PLL for ADC clock */
620 v
= sv_indirect_get(sc
, SV_REG_CLOCK_SOURCE
) & ~SV_CLOCK_ALTERNATE
;
621 sv_indirect_set(sc
, SV_REG_CLOCK_SOURCE
, v
);
623 /* Disable loopback - binds ADC and DAC rates */
624 v
= sv_indirect_get(sc
, SV_REG_LOOPBACK
) & ~SV_LOOPBACK_ENABLE
;
625 sv_indirect_set(sc
, SV_REG_LOOPBACK
, v
);
628 v
= sv_indirect_get(sc
, SV_REG_SRS_SPACE
) | SV_SRS_DISABLED
;
629 sv_indirect_set(sc
, SV_REG_SRS_SPACE
, v
);
632 sc
->rev
= sv_indirect_get(sc
, SV_REG_REVISION
);
638 sv_suspend(device_t dev
)
640 struct sc_info
*sc
= pcm_getdevinfo(dev
);
642 sc
->rch
.dma_was_active
= sc
->rch
.dma_active
;
643 svrchan_trigger(NULL
, &sc
->rch
, PCMTRIG_ABORT
);
645 sc
->pch
.dma_was_active
= sc
->pch
.dma_active
;
646 svrchan_trigger(NULL
, &sc
->pch
, PCMTRIG_ABORT
);
655 sv_resume(device_t dev
)
657 struct sc_info
*sc
= pcm_getdevinfo(dev
);
661 if (sv_init(sc
) == -1) {
662 device_printf(dev
, "unable to reinitialize the card\n");
666 if (mixer_reinit(dev
) == -1) {
667 device_printf(dev
, "unable to reinitialize the mixer\n");
671 if (sc
->rch
.dma_was_active
) {
672 svrchan_trigger(0, &sc
->rch
, PCMTRIG_START
);
675 if (sc
->pch
.dma_was_active
) {
676 svpchan_trigger(0, &sc
->pch
, PCMTRIG_START
);
682 /* ------------------------------------------------------------------------- */
683 /* Resource related */
688 struct sc_info
*sc
= data
;
691 status
= sv_direct_get(sc
, SV_CM_STATUS
);
692 if (status
& SV_CM_STATUS_AINT
)
693 chn_intr(sc
->pch
.channel
);
695 if (status
& SV_CM_STATUS_CINT
)
696 chn_intr(sc
->rch
.channel
);
698 status
&= ~(SV_CM_STATUS_AINT
|SV_CM_STATUS_CINT
);
699 DEB(if (status
) kprintf("intr 0x%02x ?\n", status
));
705 sv_probe(device_t dev
)
707 switch(pci_get_devid(dev
)) {
709 device_set_desc(dev
, "S3 Sonicvibes");
710 return BUS_PROBE_DEFAULT
;
717 sv_attach(device_t dev
) {
720 char status
[SND_STATUSLEN
];
721 u_long midi_start
, games_start
, count
, sdmaa
, sdmac
, ml
, mu
;
723 sc
= kmalloc(sizeof(struct sc_info
), M_DEVBUF
, M_NOWAIT
| M_ZERO
);
725 device_printf(dev
, "cannot allocate softc");
730 data
= pci_read_config(dev
, PCIR_COMMAND
, 2);
731 data
|= (PCIM_CMD_PORTEN
|PCIM_CMD_BUSMASTEREN
);
732 pci_write_config(dev
, PCIR_COMMAND
, data
, 2);
733 data
= pci_read_config(dev
, PCIR_COMMAND
, 2);
735 #if __FreeBSD_version > 500000
736 if (pci_get_powerstate(dev
) != PCI_POWERSTATE_D0
) {
737 device_printf(dev
, "chip is in D%d power mode "
738 "-- setting to D0\n", pci_get_powerstate(dev
));
739 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
742 sc
->enh_rid
= SV_PCI_ENHANCED
;
743 sc
->enh_type
= SYS_RES_IOPORT
;
744 sc
->enh_reg
= bus_alloc_resource(dev
, sc
->enh_type
,
746 SV_PCI_ENHANCED_SIZE
, RF_ACTIVE
);
747 if (sc
->enh_reg
== NULL
) {
748 device_printf(dev
, "sv_attach: cannot allocate enh\n");
751 sc
->enh_st
= rman_get_bustag(sc
->enh_reg
);
752 sc
->enh_sh
= rman_get_bushandle(sc
->enh_reg
);
754 data
= pci_read_config(dev
, SV_PCI_DMAA
, 4);
755 DEB(kprintf("sv_attach: initial dmaa 0x%08x\n", data
));
756 data
= pci_read_config(dev
, SV_PCI_DMAC
, 4);
757 DEB(kprintf("sv_attach: initial dmac 0x%08x\n", data
));
759 /* Initialize DMA_A and DMA_C */
760 pci_write_config(dev
, SV_PCI_DMAA
, SV_PCI_DMA_EXTENDED
, 4);
761 pci_write_config(dev
, SV_PCI_DMAC
, 0, 4);
763 /* Register IRQ handler */
765 sc
->irq
= bus_alloc_resource(dev
, SYS_RES_IRQ
, &sc
->irqid
,
766 0, ~0, 1, RF_ACTIVE
| RF_SHAREABLE
);
768 bus_setup_intr(dev
, sc
->irq
, INTR_TYPE_AV
, sv_intr
, sc
, &sc
->ih
, NULL
)) {
769 device_printf(dev
, "sv_attach: Unable to map interrupt\n");
773 sc
->bufsz
= pcm_getbuffersize(dev
, 4096, SV_DEFAULT_BUFSZ
, 65536);
774 if (bus_dma_tag_create(/*parent*/NULL
, /*alignment*/2, /*boundary*/0,
775 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT
,
776 /*highaddr*/BUS_SPACE_MAXADDR
,
777 /*filter*/NULL
, /*filterarg*/NULL
,
778 /*maxsize*/sc
->bufsz
, /*nsegments*/1,
779 /*maxsegz*/0x3ffff, /*flags*/0,
780 &sc
->parent_dmat
) != 0) {
781 device_printf(dev
, "sv_attach: Unable to create dma tag\n");
785 /* Power up and initialize */
790 if (mixer_init(dev
, &sv_mixer_class
, sc
) != 0) {
791 device_printf(dev
, "sv_attach: Mixer failed to initialize\n");
795 /* XXX This is a hack, and it's ugly. Okay, the deal is this
796 * card has two more io regions that available for automatic
797 * configuration by the pci code. These need to be allocated
798 * to used as control registers for the DMA engines.
799 * Unfortunately FBSD has no bus_space_foo() functions so we
800 * have to grab port space in region of existing resources. Go
801 * for space between midi and game ports.
803 bus_get_resource(dev
, SYS_RES_IOPORT
, SV_PCI_MIDI
, &midi_start
, &count
);
804 bus_get_resource(dev
, SYS_RES_IOPORT
, SV_PCI_GAMES
, &games_start
, &count
);
806 if (games_start
< midi_start
) {
813 /* Check assumptions about space availability and
814 alignment. How driver loaded can determine whether
815 games_start > midi_start or vice versa */
816 if ((mu
- ml
>= 0x800) ||
817 ((mu
- ml
) % 0x200)) {
818 device_printf(dev
, "sv_attach: resource assumptions not met "
819 "(midi 0x%08lx, games 0x%08lx)\n",
820 midi_start
, games_start
);
825 sdmac
= sdmaa
+ 0x40;
827 /* Add resources to list of pci resources for this device - from here on
828 * they look like normal pci resources. */
829 bus_set_resource(dev
, SYS_RES_IOPORT
, SV_PCI_DMAA
, sdmaa
, SV_PCI_DMAA_SIZE
);
830 bus_set_resource(dev
, SYS_RES_IOPORT
, SV_PCI_DMAC
, sdmac
, SV_PCI_DMAC_SIZE
);
832 /* Cache resource short-cuts for dma_a */
833 sc
->dmaa_rid
= SV_PCI_DMAA
;
834 sc
->dmaa_type
= SYS_RES_IOPORT
;
835 sc
->dmaa_reg
= bus_alloc_resource(dev
, sc
->dmaa_type
,
836 &sc
->dmaa_rid
, 0, ~0,
837 SV_PCI_ENHANCED_SIZE
, RF_ACTIVE
);
838 if (sc
->dmaa_reg
== NULL
) {
839 device_printf(dev
, "sv_attach: cannot allocate dmaa\n");
842 sc
->dmaa_st
= rman_get_bustag(sc
->dmaa_reg
);
843 sc
->dmaa_sh
= rman_get_bushandle(sc
->dmaa_reg
);
845 /* Poke port into dma_a configuration, nb bit flags to enable dma */
846 data
= pci_read_config(dev
, SV_PCI_DMAA
, 4) | SV_PCI_DMA_ENABLE
| SV_PCI_DMA_EXTENDED
;
847 data
= ((u_int32_t
)sdmaa
& 0xfffffff0) | (data
& 0x0f);
848 pci_write_config(dev
, SV_PCI_DMAA
, data
, 4);
849 DEB(kprintf("dmaa: 0x%x 0x%x\n", data
, pci_read_config(dev
, SV_PCI_DMAA
, 4)));
851 /* Cache resource short-cuts for dma_c */
852 sc
->dmac_rid
= SV_PCI_DMAC
;
853 sc
->dmac_type
= SYS_RES_IOPORT
;
854 sc
->dmac_reg
= bus_alloc_resource(dev
, sc
->dmac_type
,
855 &sc
->dmac_rid
, 0, ~0,
856 SV_PCI_ENHANCED_SIZE
, RF_ACTIVE
);
857 if (sc
->dmac_reg
== NULL
) {
858 device_printf(dev
, "sv_attach: cannot allocate dmac\n");
861 sc
->dmac_st
= rman_get_bustag(sc
->dmac_reg
);
862 sc
->dmac_sh
= rman_get_bushandle(sc
->dmac_reg
);
864 /* Poke port into dma_c configuration, nb bit flags to enable dma */
865 data
= pci_read_config(dev
, SV_PCI_DMAC
, 4) | SV_PCI_DMA_ENABLE
| SV_PCI_DMA_EXTENDED
;
866 data
= ((u_int32_t
)sdmac
& 0xfffffff0) | (data
& 0x0f);
867 pci_write_config(dev
, SV_PCI_DMAC
, data
, 4);
868 DEB(kprintf("dmac: 0x%x 0x%x\n", data
, pci_read_config(dev
, SV_PCI_DMAC
, 4)));
871 kprintf("Sonicvibes: revision %d.\n", sc
->rev
);
873 if (pcm_register(dev
, sc
, 1, 1)) {
874 device_printf(dev
, "sv_attach: pcm_register fail\n");
878 pcm_addchan(dev
, PCMDIR_PLAY
, &svpchan_class
, sc
);
879 pcm_addchan(dev
, PCMDIR_REC
, &svrchan_class
, sc
);
881 ksnprintf(status
, SND_STATUSLEN
, "at io 0x%lx irq %ld %s",
882 rman_get_start(sc
->enh_reg
), rman_get_start(sc
->irq
),PCM_KLDSTRING(snd_vibes
));
883 pcm_setstatus(dev
, status
);
885 DEB(kprintf("sv_attach: succeeded\n"));
891 bus_dma_tag_destroy(sc
->parent_dmat
);
893 bus_teardown_intr(dev
, sc
->irq
, sc
->ih
);
895 bus_release_resource(dev
, SYS_RES_IRQ
, sc
->irqid
, sc
->irq
);
897 bus_release_resource(dev
, sc
->enh_type
, sc
->enh_rid
, sc
->enh_reg
);
899 bus_release_resource(dev
, sc
->dmaa_type
, sc
->dmaa_rid
, sc
->dmaa_reg
);
901 bus_release_resource(dev
, sc
->dmac_type
, sc
->dmac_rid
, sc
->dmac_reg
);
906 sv_detach(device_t dev
) {
910 r
= pcm_unregister(dev
);
913 sc
= pcm_getdevinfo(dev
);
917 bus_dma_tag_destroy(sc
->parent_dmat
);
918 bus_teardown_intr(dev
, sc
->irq
, sc
->ih
);
919 bus_release_resource(dev
, SYS_RES_IRQ
, sc
->irqid
, sc
->irq
);
920 bus_release_resource(dev
, sc
->enh_type
, sc
->enh_rid
, sc
->enh_reg
);
921 bus_release_resource(dev
, sc
->dmaa_type
, sc
->dmaa_rid
, sc
->dmaa_reg
);
922 bus_release_resource(dev
, sc
->dmac_type
, sc
->dmac_rid
, sc
->dmac_reg
);
929 static device_method_t sc_methods
[] = {
930 DEVMETHOD(device_probe
, sv_probe
),
931 DEVMETHOD(device_attach
, sv_attach
),
932 DEVMETHOD(device_detach
, sv_detach
),
933 DEVMETHOD(device_resume
, sv_resume
),
934 DEVMETHOD(device_suspend
, sv_suspend
),
938 static driver_t sonicvibes_driver
= {
944 DRIVER_MODULE(snd_vibes
, pci
, sonicvibes_driver
, pcm_devclass
, 0, 0);
945 MODULE_DEPEND(snd_vibes
, sound
, SOUND_MINVER
, SOUND_PREFVER
, SOUND_MAXVER
);
946 MODULE_VERSION(snd_vibes
, 1);