2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Routines for control of 16-bit SoundBlaster cards and clones
4 * Note: This is very ugly hardware which uses one 8-bit DMA channel and
5 * second 16-bit DMA channel. Unfortunately 8-bit DMA channel can't
6 * transfer 16-bit samples and 16-bit DMA channels can't transfer
7 * 8-bit samples. This make full duplex more complicated than
8 * can be... People, don't buy these soundcards for full 16-bit
10 * Note: 16-bit wide is assigned to first direction which made request.
11 * With full duplex - playback is preferred with abstract layer.
13 * Note: Some chip revisions have hardware bug. Changing capture
14 * channel from full-duplex 8bit DMA to 16bit DMA will block
15 * 16bit DMA transfers from DSP chip (capture) until 8bit transfer
16 * to DSP chip (playback) starts. This bug can be avoided with
17 * "16bit DMA Allocation" setting set to Playback or Capture.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <linux/init.h>
39 #include <linux/time.h>
40 #include <linux/module.h>
41 #include <sound/core.h>
43 #include <sound/sb16_csp.h>
44 #include <sound/mpu401.h>
45 #include <sound/control.h>
46 #include <sound/info.h>
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
49 MODULE_DESCRIPTION("Routines for control of 16-bit SoundBlaster cards and clones");
50 MODULE_LICENSE("GPL");
52 #define runtime_format_bits(runtime) \
53 ((unsigned int)pcm_format_to_bits((runtime)->format))
55 #ifdef CONFIG_SND_SB16_CSP
56 static void snd_sb16_csp_playback_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
58 if (chip->hardware == SB_HW_16CSP) {
59 struct snd_sb_csp *csp = chip->csp;
61 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
62 /* manually loaded codec */
63 if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) &&
64 (runtime_format_bits(runtime) == csp->acc_format)) {
65 /* Supported runtime PCM format for playback */
66 if (csp->ops.csp_use(csp) == 0) {
67 /* If CSP was successfully acquired */
70 } else if ((csp->mode & SNDRV_SB_CSP_MODE_QSOUND) && (csp->q_enabled)) {
71 /* QSound decoder is loaded and enabled */
72 if (runtime_format_bits(runtime) & (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
73 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE)) {
74 /* Only for simple PCM formats */
75 if (csp->ops.csp_use(csp) == 0) {
76 /* If CSP was successfully acquired */
81 } else if (csp->ops.csp_use(csp) == 0) {
82 /* Acquire CSP and try to autoload hardware codec */
83 if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_WRITE)) {
84 /* Unsupported format, release CSP */
85 csp->ops.csp_unuse(csp);
88 /* Try to start CSP */
89 if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_PLAYBACK_16) ?
90 SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
91 (runtime->channels > 1) ?
92 SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
93 /* Failed, release CSP */
94 csp->ops.csp_unuse(csp);
96 /* Success, CSP acquired and running */
97 chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE;
104 static void snd_sb16_csp_capture_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
106 if (chip->hardware == SB_HW_16CSP) {
107 struct snd_sb_csp *csp = chip->csp;
109 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
110 /* manually loaded codec */
111 if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) &&
112 (runtime_format_bits(runtime) == csp->acc_format)) {
113 /* Supported runtime PCM format for capture */
114 if (csp->ops.csp_use(csp) == 0) {
115 /* If CSP was successfully acquired */
119 } else if (csp->ops.csp_use(csp) == 0) {
120 /* Acquire CSP and try to autoload hardware codec */
121 if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_READ)) {
122 /* Unsupported format, release CSP */
123 csp->ops.csp_unuse(csp);
126 /* Try to start CSP */
127 if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_CAPTURE_16) ?
128 SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
129 (runtime->channels > 1) ?
130 SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
131 /* Failed, release CSP */
132 csp->ops.csp_unuse(csp);
134 /* Success, CSP acquired and running */
135 chip->open = SNDRV_SB_CSP_MODE_DSP_READ;
142 static void snd_sb16_csp_update(struct snd_sb *chip)
144 if (chip->hardware == SB_HW_16CSP) {
145 struct snd_sb_csp *csp = chip->csp;
147 if (csp->qpos_changed) {
148 spin_lock(&chip->reg_lock);
149 csp->ops.csp_qsound_transfer (csp);
150 spin_unlock(&chip->reg_lock);
155 static void snd_sb16_csp_playback_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
157 /* CSP decoders (QSound excluded) support only 16bit transfers */
158 if (chip->hardware == SB_HW_16CSP) {
159 struct snd_sb_csp *csp = chip->csp;
161 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
162 /* manually loaded codec */
163 if (csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) {
164 runtime->hw.formats |= csp->acc_format;
167 /* autoloaded codecs */
168 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
169 SNDRV_PCM_FMTBIT_IMA_ADPCM;
174 static void snd_sb16_csp_playback_close(struct snd_sb *chip)
176 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_WRITE)) {
177 struct snd_sb_csp *csp = chip->csp;
179 if (csp->ops.csp_stop(csp) == 0) {
180 csp->ops.csp_unuse(csp);
186 static void snd_sb16_csp_capture_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
188 /* CSP coders support only 16bit transfers */
189 if (chip->hardware == SB_HW_16CSP) {
190 struct snd_sb_csp *csp = chip->csp;
192 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
193 /* manually loaded codec */
194 if (csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) {
195 runtime->hw.formats |= csp->acc_format;
198 /* autoloaded codecs */
199 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
200 SNDRV_PCM_FMTBIT_IMA_ADPCM;
205 static void snd_sb16_csp_capture_close(struct snd_sb *chip)
207 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_READ)) {
208 struct snd_sb_csp *csp = chip->csp;
210 if (csp->ops.csp_stop(csp) == 0) {
211 csp->ops.csp_unuse(csp);
217 #define snd_sb16_csp_playback_prepare(chip, runtime) /*nop*/
218 #define snd_sb16_csp_capture_prepare(chip, runtime) /*nop*/
219 #define snd_sb16_csp_update(chip) /*nop*/
220 #define snd_sb16_csp_playback_open(chip, runtime) /*nop*/
221 #define snd_sb16_csp_playback_close(chip) /*nop*/
222 #define snd_sb16_csp_capture_open(chip, runtime) /*nop*/
223 #define snd_sb16_csp_capture_close(chip) /*nop*/
227 static void snd_sb16_setup_rate(struct snd_sb *chip,
233 spin_lock_irqsave(&chip->reg_lock, flags);
234 if (chip->mode & (channel == SNDRV_PCM_STREAM_PLAYBACK ? SB_MODE_PLAYBACK_16 : SB_MODE_CAPTURE_16))
235 snd_sb_ack_16bit(chip);
237 snd_sb_ack_8bit(chip);
238 if (!(chip->mode & SB_RATE_LOCK)) {
239 chip->locked_rate = rate;
240 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_IN);
241 snd_sbdsp_command(chip, rate >> 8);
242 snd_sbdsp_command(chip, rate & 0xff);
243 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
244 snd_sbdsp_command(chip, rate >> 8);
245 snd_sbdsp_command(chip, rate & 0xff);
247 spin_unlock_irqrestore(&chip->reg_lock, flags);
250 static int snd_sb16_hw_params(struct snd_pcm_substream *substream,
251 struct snd_pcm_hw_params *hw_params)
253 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
256 static int snd_sb16_hw_free(struct snd_pcm_substream *substream)
258 snd_pcm_lib_free_pages(substream);
262 static int snd_sb16_playback_prepare(struct snd_pcm_substream *substream)
265 struct snd_sb *chip = snd_pcm_substream_chip(substream);
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 unsigned char format;
268 unsigned int size, count, dma;
270 snd_sb16_csp_playback_prepare(chip, runtime);
271 if (snd_pcm_format_unsigned(runtime->format) > 0) {
272 format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
274 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
277 snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_PLAYBACK);
278 size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream);
279 dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
280 snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
282 count = snd_pcm_lib_period_bytes(substream);
283 spin_lock_irqsave(&chip->reg_lock, flags);
284 if (chip->mode & SB_MODE_PLAYBACK_16) {
287 snd_sbdsp_command(chip, SB_DSP4_OUT16_AI);
288 snd_sbdsp_command(chip, format);
289 snd_sbdsp_command(chip, count & 0xff);
290 snd_sbdsp_command(chip, count >> 8);
291 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
294 snd_sbdsp_command(chip, SB_DSP4_OUT8_AI);
295 snd_sbdsp_command(chip, format);
296 snd_sbdsp_command(chip, count & 0xff);
297 snd_sbdsp_command(chip, count >> 8);
298 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
300 spin_unlock_irqrestore(&chip->reg_lock, flags);
304 static int snd_sb16_playback_trigger(struct snd_pcm_substream *substream,
307 struct snd_sb *chip = snd_pcm_substream_chip(substream);
310 spin_lock(&chip->reg_lock);
312 case SNDRV_PCM_TRIGGER_START:
313 case SNDRV_PCM_TRIGGER_RESUME:
314 chip->mode |= SB_RATE_LOCK_PLAYBACK;
315 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
317 case SNDRV_PCM_TRIGGER_STOP:
318 case SNDRV_PCM_TRIGGER_SUSPEND:
319 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
320 /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
321 if (chip->mode & SB_RATE_LOCK_CAPTURE)
322 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
323 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
328 spin_unlock(&chip->reg_lock);
332 static int snd_sb16_capture_prepare(struct snd_pcm_substream *substream)
335 struct snd_sb *chip = snd_pcm_substream_chip(substream);
336 struct snd_pcm_runtime *runtime = substream->runtime;
337 unsigned char format;
338 unsigned int size, count, dma;
340 snd_sb16_csp_capture_prepare(chip, runtime);
341 if (snd_pcm_format_unsigned(runtime->format) > 0) {
342 format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
344 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
346 snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_CAPTURE);
347 size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
348 dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
349 snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
351 count = snd_pcm_lib_period_bytes(substream);
352 spin_lock_irqsave(&chip->reg_lock, flags);
353 if (chip->mode & SB_MODE_CAPTURE_16) {
356 snd_sbdsp_command(chip, SB_DSP4_IN16_AI);
357 snd_sbdsp_command(chip, format);
358 snd_sbdsp_command(chip, count & 0xff);
359 snd_sbdsp_command(chip, count >> 8);
360 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
363 snd_sbdsp_command(chip, SB_DSP4_IN8_AI);
364 snd_sbdsp_command(chip, format);
365 snd_sbdsp_command(chip, count & 0xff);
366 snd_sbdsp_command(chip, count >> 8);
367 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
369 spin_unlock_irqrestore(&chip->reg_lock, flags);
373 static int snd_sb16_capture_trigger(struct snd_pcm_substream *substream,
376 struct snd_sb *chip = snd_pcm_substream_chip(substream);
379 spin_lock(&chip->reg_lock);
381 case SNDRV_PCM_TRIGGER_START:
382 case SNDRV_PCM_TRIGGER_RESUME:
383 chip->mode |= SB_RATE_LOCK_CAPTURE;
384 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
386 case SNDRV_PCM_TRIGGER_STOP:
387 case SNDRV_PCM_TRIGGER_SUSPEND:
388 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
389 /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
390 if (chip->mode & SB_RATE_LOCK_PLAYBACK)
391 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
392 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
397 spin_unlock(&chip->reg_lock);
401 irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id)
403 struct snd_sb *chip = dev_id;
404 unsigned char status;
407 spin_lock(&chip->mixer_lock);
408 status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
409 spin_unlock(&chip->mixer_lock);
410 if ((status & SB_IRQTYPE_MPUIN) && chip->rmidi_callback)
411 chip->rmidi_callback(irq, chip->rmidi->private_data);
412 if (status & SB_IRQTYPE_8BIT) {
414 if (chip->mode & SB_MODE_PLAYBACK_8) {
415 snd_pcm_period_elapsed(chip->playback_substream);
416 snd_sb16_csp_update(chip);
419 if (chip->mode & SB_MODE_CAPTURE_8) {
420 snd_pcm_period_elapsed(chip->capture_substream);
423 spin_lock(&chip->reg_lock);
425 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
426 snd_sb_ack_8bit(chip);
427 spin_unlock(&chip->reg_lock);
429 if (status & SB_IRQTYPE_16BIT) {
431 if (chip->mode & SB_MODE_PLAYBACK_16) {
432 snd_pcm_period_elapsed(chip->playback_substream);
433 snd_sb16_csp_update(chip);
436 if (chip->mode & SB_MODE_CAPTURE_16) {
437 snd_pcm_period_elapsed(chip->capture_substream);
440 spin_lock(&chip->reg_lock);
442 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
443 snd_sb_ack_16bit(chip);
444 spin_unlock(&chip->reg_lock);
453 static snd_pcm_uframes_t snd_sb16_playback_pointer(struct snd_pcm_substream *substream)
455 struct snd_sb *chip = snd_pcm_substream_chip(substream);
459 dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
460 ptr = snd_dma_pointer(dma, chip->p_dma_size);
461 return bytes_to_frames(substream->runtime, ptr);
464 static snd_pcm_uframes_t snd_sb16_capture_pointer(struct snd_pcm_substream *substream)
466 struct snd_sb *chip = snd_pcm_substream_chip(substream);
470 dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
471 ptr = snd_dma_pointer(dma, chip->c_dma_size);
472 return bytes_to_frames(substream->runtime, ptr);
479 static const struct snd_pcm_hardware snd_sb16_playback =
481 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
482 SNDRV_PCM_INFO_MMAP_VALID),
484 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
489 .buffer_bytes_max = (128*1024),
490 .period_bytes_min = 64,
491 .period_bytes_max = (128*1024),
497 static const struct snd_pcm_hardware snd_sb16_capture =
499 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
500 SNDRV_PCM_INFO_MMAP_VALID),
502 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
507 .buffer_bytes_max = (128*1024),
508 .period_bytes_min = 64,
509 .period_bytes_max = (128*1024),
519 static int snd_sb16_playback_open(struct snd_pcm_substream *substream)
522 struct snd_sb *chip = snd_pcm_substream_chip(substream);
523 struct snd_pcm_runtime *runtime = substream->runtime;
525 spin_lock_irqsave(&chip->open_lock, flags);
526 if (chip->mode & SB_MODE_PLAYBACK) {
527 spin_unlock_irqrestore(&chip->open_lock, flags);
530 runtime->hw = snd_sb16_playback;
532 /* skip if 16 bit DMA was reserved for capture */
533 if (chip->force_mode16 & SB_MODE_CAPTURE_16)
536 if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_CAPTURE_16)) {
537 chip->mode |= SB_MODE_PLAYBACK_16;
538 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
540 if (chip->dma16 <= 3) {
541 runtime->hw.buffer_bytes_max =
542 runtime->hw.period_bytes_max = 64 * 1024;
544 snd_sb16_csp_playback_open(chip, runtime);
550 if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_CAPTURE_8)) {
551 chip->mode |= SB_MODE_PLAYBACK_8;
552 /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
553 if (chip->dma16 < 0) {
554 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
555 chip->mode |= SB_MODE_PLAYBACK_16;
557 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
559 runtime->hw.buffer_bytes_max =
560 runtime->hw.period_bytes_max = 64 * 1024;
563 spin_unlock_irqrestore(&chip->open_lock, flags);
567 if (chip->hardware == SB_HW_ALS100)
568 runtime->hw.rate_max = 48000;
569 if (chip->hardware == SB_HW_CS5530) {
570 runtime->hw.buffer_bytes_max = 32 * 1024;
571 runtime->hw.periods_min = 2;
572 runtime->hw.rate_min = 44100;
574 if (chip->mode & SB_RATE_LOCK)
575 runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
576 chip->playback_substream = substream;
577 spin_unlock_irqrestore(&chip->open_lock, flags);
581 static int snd_sb16_playback_close(struct snd_pcm_substream *substream)
584 struct snd_sb *chip = snd_pcm_substream_chip(substream);
586 snd_sb16_csp_playback_close(chip);
587 spin_lock_irqsave(&chip->open_lock, flags);
588 chip->playback_substream = NULL;
589 chip->mode &= ~SB_MODE_PLAYBACK;
590 spin_unlock_irqrestore(&chip->open_lock, flags);
594 static int snd_sb16_capture_open(struct snd_pcm_substream *substream)
597 struct snd_sb *chip = snd_pcm_substream_chip(substream);
598 struct snd_pcm_runtime *runtime = substream->runtime;
600 spin_lock_irqsave(&chip->open_lock, flags);
601 if (chip->mode & SB_MODE_CAPTURE) {
602 spin_unlock_irqrestore(&chip->open_lock, flags);
605 runtime->hw = snd_sb16_capture;
607 /* skip if 16 bit DMA was reserved for playback */
608 if (chip->force_mode16 & SB_MODE_PLAYBACK_16)
611 if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_16)) {
612 chip->mode |= SB_MODE_CAPTURE_16;
613 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
615 if (chip->dma16 <= 3) {
616 runtime->hw.buffer_bytes_max =
617 runtime->hw.period_bytes_max = 64 * 1024;
619 snd_sb16_csp_capture_open(chip, runtime);
625 if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_8)) {
626 chip->mode |= SB_MODE_CAPTURE_8;
627 /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
628 if (chip->dma16 < 0) {
629 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
630 chip->mode |= SB_MODE_CAPTURE_16;
632 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
634 runtime->hw.buffer_bytes_max =
635 runtime->hw.period_bytes_max = 64 * 1024;
638 spin_unlock_irqrestore(&chip->open_lock, flags);
642 if (chip->hardware == SB_HW_ALS100)
643 runtime->hw.rate_max = 48000;
644 if (chip->hardware == SB_HW_CS5530) {
645 runtime->hw.buffer_bytes_max = 32 * 1024;
646 runtime->hw.periods_min = 2;
647 runtime->hw.rate_min = 44100;
649 if (chip->mode & SB_RATE_LOCK)
650 runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
651 chip->capture_substream = substream;
652 spin_unlock_irqrestore(&chip->open_lock, flags);
656 static int snd_sb16_capture_close(struct snd_pcm_substream *substream)
659 struct snd_sb *chip = snd_pcm_substream_chip(substream);
661 snd_sb16_csp_capture_close(chip);
662 spin_lock_irqsave(&chip->open_lock, flags);
663 chip->capture_substream = NULL;
664 chip->mode &= ~SB_MODE_CAPTURE;
665 spin_unlock_irqrestore(&chip->open_lock, flags);
670 * DMA control interface
673 static int snd_sb16_set_dma_mode(struct snd_sb *chip, int what)
675 if (chip->dma8 < 0 || chip->dma16 < 0) {
676 if (snd_BUG_ON(what))
681 chip->force_mode16 = 0;
682 } else if (what == 1) {
683 chip->force_mode16 = SB_MODE_PLAYBACK_16;
684 } else if (what == 2) {
685 chip->force_mode16 = SB_MODE_CAPTURE_16;
692 static int snd_sb16_get_dma_mode(struct snd_sb *chip)
694 if (chip->dma8 < 0 || chip->dma16 < 0)
696 switch (chip->force_mode16) {
697 case SB_MODE_PLAYBACK_16:
699 case SB_MODE_CAPTURE_16:
706 static int snd_sb16_dma_control_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
708 static const char * const texts[3] = {
709 "Auto", "Playback", "Capture"
712 return snd_ctl_enum_info(uinfo, 1, 3, texts);
715 static int snd_sb16_dma_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
717 struct snd_sb *chip = snd_kcontrol_chip(kcontrol);
720 spin_lock_irqsave(&chip->reg_lock, flags);
721 ucontrol->value.enumerated.item[0] = snd_sb16_get_dma_mode(chip);
722 spin_unlock_irqrestore(&chip->reg_lock, flags);
726 static int snd_sb16_dma_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
728 struct snd_sb *chip = snd_kcontrol_chip(kcontrol);
730 unsigned char nval, oval;
733 if ((nval = ucontrol->value.enumerated.item[0]) > 2)
735 spin_lock_irqsave(&chip->reg_lock, flags);
736 oval = snd_sb16_get_dma_mode(chip);
737 change = nval != oval;
738 snd_sb16_set_dma_mode(chip, nval);
739 spin_unlock_irqrestore(&chip->reg_lock, flags);
743 static const struct snd_kcontrol_new snd_sb16_dma_control = {
744 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
745 .name = "16-bit DMA Allocation",
746 .info = snd_sb16_dma_control_info,
747 .get = snd_sb16_dma_control_get,
748 .put = snd_sb16_dma_control_put
752 * Initialization part
755 int snd_sb16dsp_configure(struct snd_sb * chip)
758 unsigned char irqreg = 0, dmareg = 0, mpureg;
759 unsigned char realirq, realdma, realmpureg;
760 /* note: mpu register should be present only on SB16 Vibra soundcards */
762 // printk(KERN_DEBUG "codec->irq=%i, codec->dma8=%i, codec->dma16=%i\n", chip->irq, chip->dma8, chip->dma16);
763 spin_lock_irqsave(&chip->mixer_lock, flags);
764 mpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP) & ~0x06;
765 spin_unlock_irqrestore(&chip->mixer_lock, flags);
769 irqreg |= SB_IRQSETUP_IRQ9;
772 irqreg |= SB_IRQSETUP_IRQ5;
775 irqreg |= SB_IRQSETUP_IRQ7;
778 irqreg |= SB_IRQSETUP_IRQ10;
783 if (chip->dma8 >= 0) {
784 switch (chip->dma8) {
786 dmareg |= SB_DMASETUP_DMA0;
789 dmareg |= SB_DMASETUP_DMA1;
792 dmareg |= SB_DMASETUP_DMA3;
798 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
799 switch (chip->dma16) {
801 dmareg |= SB_DMASETUP_DMA5;
804 dmareg |= SB_DMASETUP_DMA6;
807 dmareg |= SB_DMASETUP_DMA7;
813 switch (chip->mpu_port) {
821 mpureg |= 0x02; /* disable MPU */
823 spin_lock_irqsave(&chip->mixer_lock, flags);
825 snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg);
826 realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP);
828 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg);
829 realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP);
831 snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg);
832 realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP);
834 spin_unlock_irqrestore(&chip->mixer_lock, flags);
835 if ((~realirq) & irqreg || (~realdma) & dmareg) {
836 snd_printk(KERN_ERR "SB16 [0x%lx]: unable to set DMA & IRQ (PnP device?)\n", chip->port);
837 snd_printk(KERN_ERR "SB16 [0x%lx]: wanted: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, realirq, realdma, realmpureg);
838 snd_printk(KERN_ERR "SB16 [0x%lx]: got: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, irqreg, dmareg, mpureg);
844 static const struct snd_pcm_ops snd_sb16_playback_ops = {
845 .open = snd_sb16_playback_open,
846 .close = snd_sb16_playback_close,
847 .ioctl = snd_pcm_lib_ioctl,
848 .hw_params = snd_sb16_hw_params,
849 .hw_free = snd_sb16_hw_free,
850 .prepare = snd_sb16_playback_prepare,
851 .trigger = snd_sb16_playback_trigger,
852 .pointer = snd_sb16_playback_pointer,
855 static const struct snd_pcm_ops snd_sb16_capture_ops = {
856 .open = snd_sb16_capture_open,
857 .close = snd_sb16_capture_close,
858 .ioctl = snd_pcm_lib_ioctl,
859 .hw_params = snd_sb16_hw_params,
860 .hw_free = snd_sb16_hw_free,
861 .prepare = snd_sb16_capture_prepare,
862 .trigger = snd_sb16_capture_trigger,
863 .pointer = snd_sb16_capture_pointer,
866 int snd_sb16dsp_pcm(struct snd_sb *chip, int device)
868 struct snd_card *card = chip->card;
872 if ((err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm)) < 0)
874 sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff);
875 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
876 pcm->private_data = chip;
879 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb16_playback_ops);
880 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb16_capture_ops);
882 if (chip->dma16 >= 0 && chip->dma8 != chip->dma16)
883 snd_ctl_add(card, snd_ctl_new1(&snd_sb16_dma_control, chip));
885 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
887 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
893 const struct snd_pcm_ops *snd_sb16dsp_get_pcm_ops(int direction)
895 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
896 &snd_sb16_playback_ops : &snd_sb16_capture_ops;
899 EXPORT_SYMBOL(snd_sb16dsp_pcm);
900 EXPORT_SYMBOL(snd_sb16dsp_get_pcm_ops);
901 EXPORT_SYMBOL(snd_sb16dsp_configure);
902 EXPORT_SYMBOL(snd_sb16dsp_interrupt);