2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Uros Bizjak <uros@kss-loka.si>
5 * Routines for control of 8-bit SoundBlaster cards and clones
6 * Please note: I don't have access to old SB8 soundcards.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Thu Apr 29 20:36:17 BST 1999 George David Morrison <gdm@gedamo.demon.co.uk>
26 * DSP can't respond to commands whilst in "high speed" mode. Caused
27 * glitching during playback. Fixed.
29 * Wed Jul 12 22:02:55 CEST 2000 Uros Bizjak <uros@kss-loka.si>
30 * Cleaned up and rewrote lowlevel routines.
35 #include <linux/init.h>
36 #include <linux/time.h>
37 #include <sound/core.h>
40 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Uros Bizjak <uros@kss-loka.si>");
41 MODULE_DESCRIPTION("Routines for control of 8-bit SoundBlaster cards and clones");
42 MODULE_LICENSE("GPL");
44 #define SB8_CLOCK 1000000
45 #define SB8_DEN(v) ((SB8_CLOCK + (v) / 2) / (v))
46 #define SB8_RATE(v) (SB8_CLOCK / SB8_DEN(v))
48 static struct snd_ratnum clock
= {
55 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clock
= {
60 static struct snd_ratnum stereo_clocks
[] = {
63 .den_min
= SB8_DEN(22050),
64 .den_max
= SB8_DEN(22050),
69 .den_min
= SB8_DEN(11025),
70 .den_max
= SB8_DEN(11025),
75 static int snd_sb8_hw_constraint_rate_channels(struct snd_pcm_hw_params
*params
,
76 struct snd_pcm_hw_rule
*rule
)
78 struct snd_interval
*c
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
80 unsigned int num
= 0, den
= 0;
81 int err
= snd_interval_ratnum(hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
),
82 2, stereo_clocks
, &num
, &den
);
83 if (err
>= 0 && den
) {
84 params
->rate_num
= num
;
85 params
->rate_den
= den
;
92 static int snd_sb8_hw_constraint_channels_rate(struct snd_pcm_hw_params
*params
,
93 struct snd_pcm_hw_rule
*rule
)
95 struct snd_interval
*r
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
96 if (r
->min
> SB8_RATE(22050) || r
->max
<= SB8_RATE(11025)) {
97 struct snd_interval t
= { .min
= 1, .max
= 1 };
98 return snd_interval_refine(hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
), &t
);
103 static int snd_sb8_playback_prepare(struct snd_pcm_substream
*substream
)
106 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
107 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
108 unsigned int mixreg
, rate
, size
, count
;
110 rate
= runtime
->rate
;
111 switch (chip
->hardware
) {
113 if (runtime
->channels
> 1) {
114 snd_assert(rate
== SB8_RATE(11025) || rate
== SB8_RATE(22050), return -EINVAL
);
115 chip
->playback_format
= SB_DSP_HI_OUTPUT_AUTO
;
121 chip
->playback_format
= SB_DSP_HI_OUTPUT_AUTO
;
126 chip
->playback_format
= SB_DSP_LO_OUTPUT_AUTO
;
129 chip
->playback_format
= SB_DSP_OUTPUT
;
134 size
= chip
->p_dma_size
= snd_pcm_lib_buffer_bytes(substream
);
135 count
= chip
->p_period_size
= snd_pcm_lib_period_bytes(substream
);
136 spin_lock_irqsave(&chip
->reg_lock
, flags
);
137 snd_sbdsp_command(chip
, SB_DSP_SPEAKER_ON
);
138 if (runtime
->channels
> 1) {
139 /* set playback stereo mode */
140 spin_lock(&chip
->mixer_lock
);
141 mixreg
= snd_sbmixer_read(chip
, SB_DSP_STEREO_SW
);
142 snd_sbmixer_write(chip
, SB_DSP_STEREO_SW
, mixreg
| 0x02);
143 spin_unlock(&chip
->mixer_lock
);
145 /* Soundblaster hardware programming reference guide, 3-23 */
146 snd_sbdsp_command(chip
, SB_DSP_DMA8_EXIT
);
147 runtime
->dma_area
[0] = 0x80;
148 snd_dma_program(chip
->dma8
, runtime
->dma_addr
, 1, DMA_MODE_WRITE
);
149 /* force interrupt */
150 chip
->mode
= SB_MODE_HALT
;
151 snd_sbdsp_command(chip
, SB_DSP_OUTPUT
);
152 snd_sbdsp_command(chip
, 0);
153 snd_sbdsp_command(chip
, 0);
155 snd_sbdsp_command(chip
, SB_DSP_SAMPLE_RATE
);
156 if (runtime
->channels
> 1) {
157 snd_sbdsp_command(chip
, 256 - runtime
->rate_den
/ 2);
158 spin_lock(&chip
->mixer_lock
);
159 /* save output filter status and turn it off */
160 mixreg
= snd_sbmixer_read(chip
, SB_DSP_PLAYBACK_FILT
);
161 snd_sbmixer_write(chip
, SB_DSP_PLAYBACK_FILT
, mixreg
| 0x20);
162 spin_unlock(&chip
->mixer_lock
);
163 /* just use force_mode16 for temporary storate... */
164 chip
->force_mode16
= mixreg
;
166 snd_sbdsp_command(chip
, 256 - runtime
->rate_den
);
168 if (chip
->playback_format
!= SB_DSP_OUTPUT
) {
170 snd_sbdsp_command(chip
, SB_DSP_BLOCK_SIZE
);
171 snd_sbdsp_command(chip
, count
& 0xff);
172 snd_sbdsp_command(chip
, count
>> 8);
174 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
175 snd_dma_program(chip
->dma8
, runtime
->dma_addr
,
176 size
, DMA_MODE_WRITE
| DMA_AUTOINIT
);
180 static int snd_sb8_playback_trigger(struct snd_pcm_substream
*substream
,
184 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
187 spin_lock_irqsave(&chip
->reg_lock
, flags
);
189 case SNDRV_PCM_TRIGGER_START
:
190 snd_sbdsp_command(chip
, chip
->playback_format
);
191 if (chip
->playback_format
== SB_DSP_OUTPUT
) {
192 count
= chip
->p_period_size
- 1;
193 snd_sbdsp_command(chip
, count
& 0xff);
194 snd_sbdsp_command(chip
, count
>> 8);
197 case SNDRV_PCM_TRIGGER_STOP
:
198 if (chip
->playback_format
== SB_DSP_HI_OUTPUT_AUTO
) {
199 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
200 snd_sbdsp_reset(chip
);
201 if (runtime
->channels
> 1) {
202 spin_lock(&chip
->mixer_lock
);
203 /* restore output filter and set hardware to mono mode */
204 snd_sbmixer_write(chip
, SB_DSP_STEREO_SW
, chip
->force_mode16
& ~0x02);
205 spin_unlock(&chip
->mixer_lock
);
208 snd_sbdsp_command(chip
, SB_DSP_DMA8_OFF
);
210 snd_sbdsp_command(chip
, SB_DSP_SPEAKER_OFF
);
212 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
213 chip
->mode
= (cmd
== SNDRV_PCM_TRIGGER_START
) ? SB_MODE_PLAYBACK_8
: SB_MODE_HALT
;
217 static int snd_sb8_hw_params(struct snd_pcm_substream
*substream
,
218 struct snd_pcm_hw_params
*hw_params
)
220 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
223 static int snd_sb8_hw_free(struct snd_pcm_substream
*substream
)
225 snd_pcm_lib_free_pages(substream
);
229 static int snd_sb8_capture_prepare(struct snd_pcm_substream
*substream
)
232 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
233 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
234 unsigned int mixreg
, rate
, size
, count
;
236 rate
= runtime
->rate
;
237 switch (chip
->hardware
) {
239 if (runtime
->channels
> 1) {
240 snd_assert(rate
== SB8_RATE(11025) || rate
== SB8_RATE(22050), return -EINVAL
);
241 chip
->capture_format
= SB_DSP_HI_INPUT_AUTO
;
244 chip
->capture_format
= (rate
> 23000) ? SB_DSP_HI_INPUT_AUTO
: SB_DSP_LO_INPUT_AUTO
;
248 chip
->capture_format
= SB_DSP_HI_INPUT_AUTO
;
253 chip
->capture_format
= SB_DSP_LO_INPUT_AUTO
;
256 chip
->capture_format
= SB_DSP_INPUT
;
261 size
= chip
->c_dma_size
= snd_pcm_lib_buffer_bytes(substream
);
262 count
= chip
->c_period_size
= snd_pcm_lib_period_bytes(substream
);
263 spin_lock_irqsave(&chip
->reg_lock
, flags
);
264 snd_sbdsp_command(chip
, SB_DSP_SPEAKER_OFF
);
265 if (runtime
->channels
> 1)
266 snd_sbdsp_command(chip
, SB_DSP_STEREO_8BIT
);
267 snd_sbdsp_command(chip
, SB_DSP_SAMPLE_RATE
);
268 if (runtime
->channels
> 1) {
269 snd_sbdsp_command(chip
, 256 - runtime
->rate_den
/ 2);
270 spin_lock(&chip
->mixer_lock
);
271 /* save input filter status and turn it off */
272 mixreg
= snd_sbmixer_read(chip
, SB_DSP_CAPTURE_FILT
);
273 snd_sbmixer_write(chip
, SB_DSP_CAPTURE_FILT
, mixreg
| 0x20);
274 spin_unlock(&chip
->mixer_lock
);
275 /* just use force_mode16 for temporary storate... */
276 chip
->force_mode16
= mixreg
;
278 snd_sbdsp_command(chip
, 256 - runtime
->rate_den
);
280 if (chip
->capture_format
!= SB_DSP_INPUT
) {
282 snd_sbdsp_command(chip
, SB_DSP_BLOCK_SIZE
);
283 snd_sbdsp_command(chip
, count
& 0xff);
284 snd_sbdsp_command(chip
, count
>> 8);
286 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
287 snd_dma_program(chip
->dma8
, runtime
->dma_addr
,
288 size
, DMA_MODE_READ
| DMA_AUTOINIT
);
292 static int snd_sb8_capture_trigger(struct snd_pcm_substream
*substream
,
296 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
299 spin_lock_irqsave(&chip
->reg_lock
, flags
);
301 case SNDRV_PCM_TRIGGER_START
:
302 snd_sbdsp_command(chip
, chip
->capture_format
);
303 if (chip
->capture_format
== SB_DSP_INPUT
) {
304 count
= chip
->c_period_size
- 1;
305 snd_sbdsp_command(chip
, count
& 0xff);
306 snd_sbdsp_command(chip
, count
>> 8);
309 case SNDRV_PCM_TRIGGER_STOP
:
310 if (chip
->capture_format
== SB_DSP_HI_INPUT_AUTO
) {
311 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
312 snd_sbdsp_reset(chip
);
313 if (runtime
->channels
> 1) {
314 /* restore input filter status */
315 spin_lock(&chip
->mixer_lock
);
316 snd_sbmixer_write(chip
, SB_DSP_CAPTURE_FILT
, chip
->force_mode16
);
317 spin_unlock(&chip
->mixer_lock
);
318 /* set hardware to mono mode */
319 snd_sbdsp_command(chip
, SB_DSP_MONO_8BIT
);
322 snd_sbdsp_command(chip
, SB_DSP_DMA8_OFF
);
324 snd_sbdsp_command(chip
, SB_DSP_SPEAKER_OFF
);
326 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
327 chip
->mode
= (cmd
== SNDRV_PCM_TRIGGER_START
) ? SB_MODE_CAPTURE_8
: SB_MODE_HALT
;
331 irqreturn_t
snd_sb8dsp_interrupt(struct snd_sb
*chip
)
333 struct snd_pcm_substream
*substream
;
334 struct snd_pcm_runtime
*runtime
;
336 snd_sb_ack_8bit(chip
);
337 switch (chip
->mode
) {
338 case SB_MODE_PLAYBACK_8
: /* ok.. playback is active */
339 substream
= chip
->playback_substream
;
340 runtime
= substream
->runtime
;
341 if (chip
->playback_format
== SB_DSP_OUTPUT
)
342 snd_sb8_playback_trigger(substream
, SNDRV_PCM_TRIGGER_START
);
343 snd_pcm_period_elapsed(substream
);
345 case SB_MODE_CAPTURE_8
:
346 substream
= chip
->capture_substream
;
347 runtime
= substream
->runtime
;
348 if (chip
->capture_format
== SB_DSP_INPUT
)
349 snd_sb8_capture_trigger(substream
, SNDRV_PCM_TRIGGER_START
);
350 snd_pcm_period_elapsed(substream
);
356 static snd_pcm_uframes_t
snd_sb8_playback_pointer(struct snd_pcm_substream
*substream
)
358 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
361 if (chip
->mode
!= SB_MODE_PLAYBACK_8
)
363 ptr
= snd_dma_pointer(chip
->dma8
, chip
->p_dma_size
);
364 return bytes_to_frames(substream
->runtime
, ptr
);
367 static snd_pcm_uframes_t
snd_sb8_capture_pointer(struct snd_pcm_substream
*substream
)
369 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
372 if (chip
->mode
!= SB_MODE_CAPTURE_8
)
374 ptr
= snd_dma_pointer(chip
->dma8
, chip
->c_dma_size
);
375 return bytes_to_frames(substream
->runtime
, ptr
);
382 static struct snd_pcm_hardware snd_sb8_playback
=
384 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
385 SNDRV_PCM_INFO_MMAP_VALID
),
386 .formats
= SNDRV_PCM_FMTBIT_U8
,
387 .rates
= (SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000
|
388 SNDRV_PCM_RATE_11025
| SNDRV_PCM_RATE_22050
),
393 .buffer_bytes_max
= 65536,
394 .period_bytes_min
= 64,
395 .period_bytes_max
= 65536,
401 static struct snd_pcm_hardware snd_sb8_capture
=
403 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
404 SNDRV_PCM_INFO_MMAP_VALID
),
405 .formats
= SNDRV_PCM_FMTBIT_U8
,
406 .rates
= (SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000
|
407 SNDRV_PCM_RATE_11025
),
412 .buffer_bytes_max
= 65536,
413 .period_bytes_min
= 64,
414 .period_bytes_max
= 65536,
424 static int snd_sb8_open(struct snd_pcm_substream
*substream
)
426 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
427 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
430 spin_lock_irqsave(&chip
->open_lock
, flags
);
432 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
435 chip
->open
|= SB_OPEN_PCM
;
436 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
437 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
438 chip
->playback_substream
= substream
;
439 runtime
->hw
= snd_sb8_playback
;
441 chip
->capture_substream
= substream
;
442 runtime
->hw
= snd_sb8_capture
;
444 switch (chip
->hardware
) {
446 runtime
->hw
.rate_max
= 44100;
447 runtime
->hw
.channels_max
= 2;
448 snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
449 snd_sb8_hw_constraint_rate_channels
, NULL
,
450 SNDRV_PCM_HW_PARAM_CHANNELS
,
451 SNDRV_PCM_HW_PARAM_RATE
, -1);
452 snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
453 snd_sb8_hw_constraint_channels_rate
, NULL
,
454 SNDRV_PCM_HW_PARAM_RATE
, -1);
457 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
458 runtime
->hw
.rate_max
= 44100;
460 runtime
->hw
.rate_max
= 15000;
465 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
466 &hw_constraints_clock
);
470 static int snd_sb8_close(struct snd_pcm_substream
*substream
)
473 struct snd_sb
*chip
= snd_pcm_substream_chip(substream
);
475 chip
->playback_substream
= NULL
;
476 chip
->capture_substream
= NULL
;
477 spin_lock_irqsave(&chip
->open_lock
, flags
);
478 chip
->open
&= ~SB_OPEN_PCM
;
479 spin_unlock_irqrestore(&chip
->open_lock
, flags
);
484 * Initialization part
487 static struct snd_pcm_ops snd_sb8_playback_ops
= {
488 .open
= snd_sb8_open
,
489 .close
= snd_sb8_close
,
490 .ioctl
= snd_pcm_lib_ioctl
,
491 .hw_params
= snd_sb8_hw_params
,
492 .hw_free
= snd_sb8_hw_free
,
493 .prepare
= snd_sb8_playback_prepare
,
494 .trigger
= snd_sb8_playback_trigger
,
495 .pointer
= snd_sb8_playback_pointer
,
498 static struct snd_pcm_ops snd_sb8_capture_ops
= {
499 .open
= snd_sb8_open
,
500 .close
= snd_sb8_close
,
501 .ioctl
= snd_pcm_lib_ioctl
,
502 .hw_params
= snd_sb8_hw_params
,
503 .hw_free
= snd_sb8_hw_free
,
504 .prepare
= snd_sb8_capture_prepare
,
505 .trigger
= snd_sb8_capture_trigger
,
506 .pointer
= snd_sb8_capture_pointer
,
509 int snd_sb8dsp_pcm(struct snd_sb
*chip
, int device
, struct snd_pcm
** rpcm
)
511 struct snd_card
*card
= chip
->card
;
517 if ((err
= snd_pcm_new(card
, "SB8 DSP", device
, 1, 1, &pcm
)) < 0)
519 sprintf(pcm
->name
, "DSP v%i.%i", chip
->version
>> 8, chip
->version
& 0xff);
520 pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
521 pcm
->private_data
= chip
;
523 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_sb8_playback_ops
);
524 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_sb8_capture_ops
);
526 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
535 EXPORT_SYMBOL(snd_sb8dsp_pcm
);
536 EXPORT_SYMBOL(snd_sb8dsp_interrupt
);
538 EXPORT_SYMBOL(snd_sb8dsp_midi_interrupt
);
539 EXPORT_SYMBOL(snd_sb8dsp_midi
);
545 static int __init
alsa_sb8_init(void)
550 static void __exit
alsa_sb8_exit(void)
554 module_init(alsa_sb8_init
)
555 module_exit(alsa_sb8_exit
)