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;
109 unsigned char format;
110 unsigned char stereo = runtime->channels > 1;
113 rate = runtime->rate;
114 switch (chip->hardware) {
116 if (runtime->format == SNDRV_PCM_FORMAT_S16_LE) {
117 if (chip->mode & SB_MODE_CAPTURE_16)
120 chip->mode |= SB_MODE_PLAYBACK_16;
122 chip->playback_format = SB_DSP_LO_OUTPUT_AUTO;
125 if (runtime->channels > 1) {
126 if (snd_BUG_ON(rate != SB8_RATE(11025) &&
127 rate != SB8_RATE(22050)))
129 chip->playback_format = SB_DSP_HI_OUTPUT_AUTO;
135 chip->playback_format = SB_DSP_HI_OUTPUT_AUTO;
140 chip->playback_format = SB_DSP_LO_OUTPUT_AUTO;
143 chip->playback_format = SB_DSP_OUTPUT;
148 if (chip->mode & SB_MODE_PLAYBACK_16) {
149 format = stereo ? SB_DSP_STEREO_16BIT : SB_DSP_MONO_16BIT;
152 format = stereo ? SB_DSP_STEREO_8BIT : SB_DSP_MONO_8BIT;
153 chip->mode |= SB_MODE_PLAYBACK_8;
156 size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream);
157 count = chip->p_period_size = snd_pcm_lib_period_bytes(substream);
158 spin_lock_irqsave(&chip->reg_lock, flags);
159 snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON);
160 if (chip->hardware == SB_HW_JAZZ16)
161 snd_sbdsp_command(chip, format);
163 /* set playback stereo mode */
164 spin_lock(&chip->mixer_lock);
165 mixreg = snd_sbmixer_read(chip, SB_DSP_STEREO_SW);
166 snd_sbmixer_write(chip, SB_DSP_STEREO_SW, mixreg | 0x02);
167 spin_unlock(&chip->mixer_lock);
169 /* Soundblaster hardware programming reference guide, 3-23 */
170 snd_sbdsp_command(chip, SB_DSP_DMA8_EXIT);
171 runtime->dma_area[0] = 0x80;
172 snd_dma_program(dma, runtime->dma_addr, 1, DMA_MODE_WRITE);
173 /* force interrupt */
174 snd_sbdsp_command(chip, SB_DSP_OUTPUT);
175 snd_sbdsp_command(chip, 0);
176 snd_sbdsp_command(chip, 0);
178 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE);
180 snd_sbdsp_command(chip, 256 - runtime->rate_den / 2);
181 spin_lock(&chip->mixer_lock);
182 /* save output filter status and turn it off */
183 mixreg = snd_sbmixer_read(chip, SB_DSP_PLAYBACK_FILT);
184 snd_sbmixer_write(chip, SB_DSP_PLAYBACK_FILT, mixreg | 0x20);
185 spin_unlock(&chip->mixer_lock);
186 /* just use force_mode16 for temporary storate... */
187 chip->force_mode16 = mixreg;
189 snd_sbdsp_command(chip, 256 - runtime->rate_den);
191 if (chip->playback_format != SB_DSP_OUTPUT) {
192 if (chip->mode & SB_MODE_PLAYBACK_16)
195 snd_sbdsp_command(chip, SB_DSP_BLOCK_SIZE);
196 snd_sbdsp_command(chip, count & 0xff);
197 snd_sbdsp_command(chip, count >> 8);
199 spin_unlock_irqrestore(&chip->reg_lock, flags);
200 snd_dma_program(dma, runtime->dma_addr,
201 size, DMA_MODE_WRITE | DMA_AUTOINIT);
205 static int snd_sb8_playback_trigger(struct snd_pcm_substream *substream,
209 struct snd_sb *chip = snd_pcm_substream_chip(substream);
212 spin_lock_irqsave(&chip->reg_lock, flags);
214 case SNDRV_PCM_TRIGGER_START:
215 snd_sbdsp_command(chip, chip->playback_format);
216 if (chip->playback_format == SB_DSP_OUTPUT) {
217 count = chip->p_period_size - 1;
218 snd_sbdsp_command(chip, count & 0xff);
219 snd_sbdsp_command(chip, count >> 8);
222 case SNDRV_PCM_TRIGGER_STOP:
223 if (chip->playback_format == SB_DSP_HI_OUTPUT_AUTO) {
224 struct snd_pcm_runtime *runtime = substream->runtime;
225 snd_sbdsp_reset(chip);
226 if (runtime->channels > 1) {
227 spin_lock(&chip->mixer_lock);
228 /* restore output filter and set hardware to mono mode */
229 snd_sbmixer_write(chip, SB_DSP_STEREO_SW, chip->force_mode16 & ~0x02);
230 spin_unlock(&chip->mixer_lock);
233 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
235 snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF);
237 spin_unlock_irqrestore(&chip->reg_lock, flags);
241 static int snd_sb8_hw_params(struct snd_pcm_substream *substream,
242 struct snd_pcm_hw_params *hw_params)
244 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
247 static int snd_sb8_hw_free(struct snd_pcm_substream *substream)
249 snd_pcm_lib_free_pages(substream);
253 static int snd_sb8_capture_prepare(struct snd_pcm_substream *substream)
256 struct snd_sb *chip = snd_pcm_substream_chip(substream);
257 struct snd_pcm_runtime *runtime = substream->runtime;
258 unsigned int mixreg, rate, size, count;
259 unsigned char format;
260 unsigned char stereo = runtime->channels > 1;
263 rate = runtime->rate;
264 switch (chip->hardware) {
266 if (runtime->format == SNDRV_PCM_FORMAT_S16_LE) {
267 if (chip->mode & SB_MODE_PLAYBACK_16)
270 chip->mode |= SB_MODE_CAPTURE_16;
272 chip->capture_format = SB_DSP_LO_INPUT_AUTO;
275 if (runtime->channels > 1) {
276 if (snd_BUG_ON(rate != SB8_RATE(11025) &&
277 rate != SB8_RATE(22050)))
279 chip->capture_format = SB_DSP_HI_INPUT_AUTO;
282 chip->capture_format = (rate > 23000) ? SB_DSP_HI_INPUT_AUTO : SB_DSP_LO_INPUT_AUTO;
286 chip->capture_format = SB_DSP_HI_INPUT_AUTO;
291 chip->capture_format = SB_DSP_LO_INPUT_AUTO;
294 chip->capture_format = SB_DSP_INPUT;
299 if (chip->mode & SB_MODE_CAPTURE_16) {
300 format = stereo ? SB_DSP_STEREO_16BIT : SB_DSP_MONO_16BIT;
303 format = stereo ? SB_DSP_STEREO_8BIT : SB_DSP_MONO_8BIT;
304 chip->mode |= SB_MODE_CAPTURE_8;
307 size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
308 count = chip->c_period_size = snd_pcm_lib_period_bytes(substream);
309 spin_lock_irqsave(&chip->reg_lock, flags);
310 snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF);
311 if (chip->hardware == SB_HW_JAZZ16)
312 snd_sbdsp_command(chip, format);
314 snd_sbdsp_command(chip, SB_DSP_STEREO_8BIT);
315 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE);
317 snd_sbdsp_command(chip, 256 - runtime->rate_den / 2);
318 spin_lock(&chip->mixer_lock);
319 /* save input filter status and turn it off */
320 mixreg = snd_sbmixer_read(chip, SB_DSP_CAPTURE_FILT);
321 snd_sbmixer_write(chip, SB_DSP_CAPTURE_FILT, mixreg | 0x20);
322 spin_unlock(&chip->mixer_lock);
323 /* just use force_mode16 for temporary storate... */
324 chip->force_mode16 = mixreg;
326 snd_sbdsp_command(chip, 256 - runtime->rate_den);
328 if (chip->capture_format != SB_DSP_INPUT) {
329 if (chip->mode & SB_MODE_PLAYBACK_16)
332 snd_sbdsp_command(chip, SB_DSP_BLOCK_SIZE);
333 snd_sbdsp_command(chip, count & 0xff);
334 snd_sbdsp_command(chip, count >> 8);
336 spin_unlock_irqrestore(&chip->reg_lock, flags);
337 snd_dma_program(dma, runtime->dma_addr,
338 size, DMA_MODE_READ | DMA_AUTOINIT);
342 static int snd_sb8_capture_trigger(struct snd_pcm_substream *substream,
346 struct snd_sb *chip = snd_pcm_substream_chip(substream);
349 spin_lock_irqsave(&chip->reg_lock, flags);
351 case SNDRV_PCM_TRIGGER_START:
352 snd_sbdsp_command(chip, chip->capture_format);
353 if (chip->capture_format == SB_DSP_INPUT) {
354 count = chip->c_period_size - 1;
355 snd_sbdsp_command(chip, count & 0xff);
356 snd_sbdsp_command(chip, count >> 8);
359 case SNDRV_PCM_TRIGGER_STOP:
360 if (chip->capture_format == SB_DSP_HI_INPUT_AUTO) {
361 struct snd_pcm_runtime *runtime = substream->runtime;
362 snd_sbdsp_reset(chip);
363 if (runtime->channels > 1) {
364 /* restore input filter status */
365 spin_lock(&chip->mixer_lock);
366 snd_sbmixer_write(chip, SB_DSP_CAPTURE_FILT, chip->force_mode16);
367 spin_unlock(&chip->mixer_lock);
368 /* set hardware to mono mode */
369 snd_sbdsp_command(chip, SB_DSP_MONO_8BIT);
372 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
374 snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF);
376 spin_unlock_irqrestore(&chip->reg_lock, flags);
380 irqreturn_t snd_sb8dsp_interrupt(struct snd_sb *chip)
382 struct snd_pcm_substream *substream;
383 struct snd_pcm_runtime *runtime;
385 snd_sb_ack_8bit(chip);
386 switch (chip->mode) {
387 case SB_MODE_PLAYBACK_16: /* ok.. playback is active */
388 if (chip->hardware != SB_HW_JAZZ16)
391 case SB_MODE_PLAYBACK_8:
392 substream = chip->playback_substream;
393 runtime = substream->runtime;
394 if (chip->playback_format == SB_DSP_OUTPUT)
395 snd_sb8_playback_trigger(substream, SNDRV_PCM_TRIGGER_START);
396 snd_pcm_period_elapsed(substream);
398 case SB_MODE_CAPTURE_16:
399 if (chip->hardware != SB_HW_JAZZ16)
402 case SB_MODE_CAPTURE_8:
403 substream = chip->capture_substream;
404 runtime = substream->runtime;
405 if (chip->capture_format == SB_DSP_INPUT)
406 snd_sb8_capture_trigger(substream, SNDRV_PCM_TRIGGER_START);
407 snd_pcm_period_elapsed(substream);
413 static snd_pcm_uframes_t snd_sb8_playback_pointer(struct snd_pcm_substream *substream)
415 struct snd_sb *chip = snd_pcm_substream_chip(substream);
419 if (chip->mode & SB_MODE_PLAYBACK_8)
421 else if (chip->mode & SB_MODE_PLAYBACK_16)
425 ptr = snd_dma_pointer(dma, chip->p_dma_size);
426 return bytes_to_frames(substream->runtime, ptr);
429 static snd_pcm_uframes_t snd_sb8_capture_pointer(struct snd_pcm_substream *substream)
431 struct snd_sb *chip = snd_pcm_substream_chip(substream);
435 if (chip->mode & SB_MODE_CAPTURE_8)
437 else if (chip->mode & SB_MODE_CAPTURE_16)
441 ptr = snd_dma_pointer(dma, chip->c_dma_size);
442 return bytes_to_frames(substream->runtime, ptr);
449 static struct snd_pcm_hardware snd_sb8_playback =
451 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
452 SNDRV_PCM_INFO_MMAP_VALID),
453 .formats = SNDRV_PCM_FMTBIT_U8,
454 .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000 |
455 SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050),
460 .buffer_bytes_max = 65536,
461 .period_bytes_min = 64,
462 .period_bytes_max = 65536,
468 static struct snd_pcm_hardware snd_sb8_capture =
470 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
471 SNDRV_PCM_INFO_MMAP_VALID),
472 .formats = SNDRV_PCM_FMTBIT_U8,
473 .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000 |
474 SNDRV_PCM_RATE_11025),
479 .buffer_bytes_max = 65536,
480 .period_bytes_min = 64,
481 .period_bytes_max = 65536,
491 static int snd_sb8_open(struct snd_pcm_substream *substream)
493 struct snd_sb *chip = snd_pcm_substream_chip(substream);
494 struct snd_pcm_runtime *runtime = substream->runtime;
497 spin_lock_irqsave(&chip->open_lock, flags);
499 spin_unlock_irqrestore(&chip->open_lock, flags);
502 chip->open |= SB_OPEN_PCM;
503 spin_unlock_irqrestore(&chip->open_lock, flags);
504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
505 chip->playback_substream = substream;
506 runtime->hw = snd_sb8_playback;
508 chip->capture_substream = substream;
509 runtime->hw = snd_sb8_capture;
511 switch (chip->hardware) {
513 if (chip->dma16 == 5 || chip->dma16 == 7)
514 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S16_LE;
515 runtime->hw.rates |= SNDRV_PCM_RATE_8000_48000;
516 runtime->hw.rate_min = 4000;
517 runtime->hw.rate_max = 50000;
518 runtime->hw.channels_max = 2;
521 runtime->hw.rate_max = 44100;
522 runtime->hw.channels_max = 2;
523 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
524 snd_sb8_hw_constraint_rate_channels, NULL,
525 SNDRV_PCM_HW_PARAM_CHANNELS,
526 SNDRV_PCM_HW_PARAM_RATE, -1);
527 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
528 snd_sb8_hw_constraint_channels_rate, NULL,
529 SNDRV_PCM_HW_PARAM_RATE, -1);
532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
533 runtime->hw.rate_max = 44100;
535 runtime->hw.rate_max = 15000;
540 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
541 &hw_constraints_clock);
542 if (chip->dma8 > 3 || chip->dma16 >= 0) {
543 snd_pcm_hw_constraint_step(runtime, 0,
544 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2);
545 snd_pcm_hw_constraint_step(runtime, 0,
546 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2);
547 runtime->hw.buffer_bytes_max = 128 * 1024 * 1024;
548 runtime->hw.period_bytes_max = 128 * 1024 * 1024;
553 static int snd_sb8_close(struct snd_pcm_substream *substream)
556 struct snd_sb *chip = snd_pcm_substream_chip(substream);
558 chip->playback_substream = NULL;
559 chip->capture_substream = NULL;
560 spin_lock_irqsave(&chip->open_lock, flags);
561 chip->open &= ~SB_OPEN_PCM;
562 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
563 chip->mode &= ~SB_MODE_PLAYBACK;
565 chip->mode &= ~SB_MODE_CAPTURE;
566 spin_unlock_irqrestore(&chip->open_lock, flags);
571 * Initialization part
574 static struct snd_pcm_ops snd_sb8_playback_ops = {
575 .open = snd_sb8_open,
576 .close = snd_sb8_close,
577 .ioctl = snd_pcm_lib_ioctl,
578 .hw_params = snd_sb8_hw_params,
579 .hw_free = snd_sb8_hw_free,
580 .prepare = snd_sb8_playback_prepare,
581 .trigger = snd_sb8_playback_trigger,
582 .pointer = snd_sb8_playback_pointer,
585 static struct snd_pcm_ops snd_sb8_capture_ops = {
586 .open = snd_sb8_open,
587 .close = snd_sb8_close,
588 .ioctl = snd_pcm_lib_ioctl,
589 .hw_params = snd_sb8_hw_params,
590 .hw_free = snd_sb8_hw_free,
591 .prepare = snd_sb8_capture_prepare,
592 .trigger = snd_sb8_capture_trigger,
593 .pointer = snd_sb8_capture_pointer,
596 int snd_sb8dsp_pcm(struct snd_sb *chip, int device, struct snd_pcm ** rpcm)
598 struct snd_card *card = chip->card;
601 size_t max_prealloc = 64 * 1024;
605 if ((err = snd_pcm_new(card, "SB8 DSP", device, 1, 1, &pcm)) < 0)
607 sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff);
608 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
609 pcm->private_data = chip;
611 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb8_playback_ops);
612 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb8_capture_ops);
614 if (chip->dma8 > 3 || chip->dma16 >= 0)
615 max_prealloc = 128 * 1024;
616 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
618 64*1024, max_prealloc);
625 EXPORT_SYMBOL(snd_sb8dsp_pcm);
626 EXPORT_SYMBOL(snd_sb8dsp_interrupt);
628 EXPORT_SYMBOL(snd_sb8dsp_midi_interrupt);
629 EXPORT_SYMBOL(snd_sb8dsp_midi);
635 static int __init alsa_sb8_init(void)
640 static void __exit alsa_sb8_exit(void)
644 module_init(alsa_sb8_init)
645 module_exit(alsa_sb8_exit)