2 * Copyright (c) by Jaroslav Kysela <perex@suse.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
36 #include <sound/driver.h>
39 #include <linux/init.h>
40 #include <linux/time.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@suse.cz>");
49 MODULE_DESCRIPTION("Routines for control of 16-bit SoundBlaster cards and clones");
50 MODULE_LICENSE("GPL");
54 #ifdef CONFIG_SND_SB16_CSP
55 static void snd_sb16_csp_playback_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
57 if (chip->hardware == SB_HW_16CSP) {
58 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
60 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
61 /* manually loaded codec */
62 if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) &&
63 ((1U << runtime->format) == csp->acc_format)) {
64 /* Supported runtime PCM format for playback */
65 if (csp->ops.csp_use(csp) == 0) {
66 /* If CSP was successfully acquired */
69 } else if ((csp->mode & SNDRV_SB_CSP_MODE_QSOUND) && (csp->q_enabled)) {
70 /* QSound decoder is loaded and enabled */
71 if ((1 << runtime->format) & (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
72 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE)) {
73 /* Only for simple PCM formats */
74 if (csp->ops.csp_use(csp) == 0) {
75 /* If CSP was successfully acquired */
80 } else if (csp->ops.csp_use(csp) == 0) {
81 /* Acquire CSP and try to autoload hardware codec */
82 if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_WRITE)) {
83 /* Unsupported format, release CSP */
84 csp->ops.csp_unuse(csp);
87 /* Try to start CSP */
88 if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_PLAYBACK_16) ?
89 SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
90 (runtime->channels > 1) ?
91 SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
92 /* Failed, release CSP */
93 csp->ops.csp_unuse(csp);
95 /* Success, CSP acquired and running */
96 chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE;
103 static void snd_sb16_csp_capture_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
105 if (chip->hardware == SB_HW_16CSP) {
106 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
108 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
109 /* manually loaded codec */
110 if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) &&
111 ((1U << runtime->format) == csp->acc_format)) {
112 /* Supported runtime PCM format for capture */
113 if (csp->ops.csp_use(csp) == 0) {
114 /* If CSP was successfully acquired */
118 } else if (csp->ops.csp_use(csp) == 0) {
119 /* Acquire CSP and try to autoload hardware codec */
120 if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_READ)) {
121 /* Unsupported format, release CSP */
122 csp->ops.csp_unuse(csp);
125 /* Try to start CSP */
126 if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_CAPTURE_16) ?
127 SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
128 (runtime->channels > 1) ?
129 SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
130 /* Failed, release CSP */
131 csp->ops.csp_unuse(csp);
133 /* Success, CSP acquired and running */
134 chip->open = SNDRV_SB_CSP_MODE_DSP_READ;
141 static void snd_sb16_csp_update(sb_t *chip)
143 if (chip->hardware == SB_HW_16CSP) {
144 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
146 if (csp->qpos_changed) {
147 spin_lock(&chip->reg_lock);
148 csp->ops.csp_qsound_transfer (csp);
149 spin_unlock(&chip->reg_lock);
154 static void snd_sb16_csp_playback_open(sb_t *chip, snd_pcm_runtime_t *runtime)
156 /* CSP decoders (QSound excluded) support only 16bit transfers */
157 if (chip->hardware == SB_HW_16CSP) {
158 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
160 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
161 /* manually loaded codec */
162 if (csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) {
163 runtime->hw.formats |= csp->acc_format;
166 /* autoloaded codecs */
167 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
168 SNDRV_PCM_FMTBIT_IMA_ADPCM;
173 static void snd_sb16_csp_playback_close(sb_t *chip)
175 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_WRITE)) {
176 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
178 if (csp->ops.csp_stop(csp) == 0) {
179 csp->ops.csp_unuse(csp);
185 static void snd_sb16_csp_capture_open(sb_t *chip, snd_pcm_runtime_t *runtime)
187 /* CSP coders support only 16bit transfers */
188 if (chip->hardware == SB_HW_16CSP) {
189 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
191 if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
192 /* manually loaded codec */
193 if (csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) {
194 runtime->hw.formats |= csp->acc_format;
197 /* autoloaded codecs */
198 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
199 SNDRV_PCM_FMTBIT_IMA_ADPCM;
204 static void snd_sb16_csp_capture_close(sb_t *chip)
206 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_READ)) {
207 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
209 if (csp->ops.csp_stop(csp) == 0) {
210 csp->ops.csp_unuse(csp);
216 #define snd_sb16_csp_playback_prepare(chip, runtime) /*nop*/
217 #define snd_sb16_csp_capture_prepare(chip, runtime) /*nop*/
218 #define snd_sb16_csp_update(chip) /*nop*/
219 #define snd_sb16_csp_playback_open(chip, runtime) /*nop*/
220 #define snd_sb16_csp_playback_close(chip) /*nop*/
221 #define snd_sb16_csp_capture_open(chip, runtime) /*nop*/
222 #define snd_sb16_csp_capture_close(chip) /*nop*/
226 static void snd_sb16_setup_rate(sb_t *chip,
232 spin_lock_irqsave(&chip->reg_lock, flags);
233 if (chip->mode & (channel == SNDRV_PCM_STREAM_PLAYBACK ? SB_MODE_PLAYBACK_16 : SB_MODE_CAPTURE_16))
234 snd_sb_ack_16bit(chip);
236 snd_sb_ack_8bit(chip);
237 if (!(chip->mode & SB_RATE_LOCK)) {
238 chip->locked_rate = rate;
239 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_IN);
240 snd_sbdsp_command(chip, rate >> 8);
241 snd_sbdsp_command(chip, rate & 0xff);
242 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
243 snd_sbdsp_command(chip, rate >> 8);
244 snd_sbdsp_command(chip, rate & 0xff);
246 spin_unlock_irqrestore(&chip->reg_lock, flags);
249 static int snd_sb16_hw_params(snd_pcm_substream_t * substream,
250 snd_pcm_hw_params_t * hw_params)
252 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
255 static int snd_sb16_hw_free(snd_pcm_substream_t * substream)
257 snd_pcm_lib_free_pages(substream);
261 static int snd_sb16_playback_prepare(snd_pcm_substream_t * substream)
264 sb_t *chip = snd_pcm_substream_chip(substream);
265 snd_pcm_runtime_t *runtime = substream->runtime;
266 unsigned char format;
267 unsigned int size, count, dma;
269 snd_sb16_csp_playback_prepare(chip, runtime);
270 if (snd_pcm_format_unsigned(runtime->format) > 0) {
271 format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
273 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
276 snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_PLAYBACK);
277 size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream);
278 dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
279 snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
281 count = snd_pcm_lib_period_bytes(substream);
282 spin_lock_irqsave(&chip->reg_lock, flags);
283 if (chip->mode & SB_MODE_PLAYBACK_16) {
286 snd_sbdsp_command(chip, SB_DSP4_OUT16_AI);
287 snd_sbdsp_command(chip, format);
288 snd_sbdsp_command(chip, count & 0xff);
289 snd_sbdsp_command(chip, count >> 8);
290 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
293 snd_sbdsp_command(chip, SB_DSP4_OUT8_AI);
294 snd_sbdsp_command(chip, format);
295 snd_sbdsp_command(chip, count & 0xff);
296 snd_sbdsp_command(chip, count >> 8);
297 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
299 spin_unlock_irqrestore(&chip->reg_lock, flags);
303 static int snd_sb16_playback_trigger(snd_pcm_substream_t * substream,
306 sb_t *chip = snd_pcm_substream_chip(substream);
309 spin_lock(&chip->reg_lock);
311 case SNDRV_PCM_TRIGGER_START:
312 chip->mode |= SB_RATE_LOCK_PLAYBACK;
313 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
315 case SNDRV_PCM_TRIGGER_STOP:
316 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
317 /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
318 if (chip->mode & SB_RATE_LOCK_CAPTURE)
319 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
320 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
325 spin_unlock(&chip->reg_lock);
329 static int snd_sb16_capture_prepare(snd_pcm_substream_t * substream)
332 sb_t *chip = snd_pcm_substream_chip(substream);
333 snd_pcm_runtime_t *runtime = substream->runtime;
334 unsigned char format;
335 unsigned int size, count, dma;
337 snd_sb16_csp_capture_prepare(chip, runtime);
338 if (snd_pcm_format_unsigned(runtime->format) > 0) {
339 format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
341 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
343 snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_CAPTURE);
344 size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
345 dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
346 snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
348 count = snd_pcm_lib_period_bytes(substream);
349 spin_lock_irqsave(&chip->reg_lock, flags);
350 if (chip->mode & SB_MODE_CAPTURE_16) {
353 snd_sbdsp_command(chip, SB_DSP4_IN16_AI);
354 snd_sbdsp_command(chip, format);
355 snd_sbdsp_command(chip, count & 0xff);
356 snd_sbdsp_command(chip, count >> 8);
357 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
360 snd_sbdsp_command(chip, SB_DSP4_IN8_AI);
361 snd_sbdsp_command(chip, format);
362 snd_sbdsp_command(chip, count & 0xff);
363 snd_sbdsp_command(chip, count >> 8);
364 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
366 spin_unlock_irqrestore(&chip->reg_lock, flags);
370 static int snd_sb16_capture_trigger(snd_pcm_substream_t * substream,
373 sb_t *chip = snd_pcm_substream_chip(substream);
376 spin_lock(&chip->reg_lock);
378 case SNDRV_PCM_TRIGGER_START:
379 chip->mode |= SB_RATE_LOCK_CAPTURE;
380 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
382 case SNDRV_PCM_TRIGGER_STOP:
383 snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
384 /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
385 if (chip->mode & SB_RATE_LOCK_PLAYBACK)
386 snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
387 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
392 spin_unlock(&chip->reg_lock);
396 irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
398 sb_t *chip = snd_magic_cast(sb_t, dev_id, return IRQ_NONE);
399 unsigned char status;
402 spin_lock(&chip->mixer_lock);
403 status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
404 spin_unlock(&chip->mixer_lock);
405 if ((status & SB_IRQTYPE_MPUIN) && chip->rmidi_callback)
406 chip->rmidi_callback(irq, chip->rmidi->private_data, regs);
407 if (status & SB_IRQTYPE_8BIT) {
409 if (chip->mode & SB_MODE_PLAYBACK_8) {
410 snd_pcm_period_elapsed(chip->playback_substream);
411 snd_sb16_csp_update(chip);
414 if (chip->mode & SB_MODE_CAPTURE_8) {
415 snd_pcm_period_elapsed(chip->capture_substream);
418 spin_lock(&chip->reg_lock);
420 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
421 snd_sb_ack_8bit(chip);
422 spin_unlock(&chip->reg_lock);
424 if (status & SB_IRQTYPE_16BIT) {
426 if (chip->mode & SB_MODE_PLAYBACK_16) {
427 snd_pcm_period_elapsed(chip->playback_substream);
428 snd_sb16_csp_update(chip);
431 if (chip->mode & SB_MODE_CAPTURE_16) {
432 snd_pcm_period_elapsed(chip->capture_substream);
435 spin_lock(&chip->reg_lock);
437 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
438 snd_sb_ack_16bit(chip);
439 spin_unlock(&chip->reg_lock);
448 static snd_pcm_uframes_t snd_sb16_playback_pointer(snd_pcm_substream_t * substream)
450 sb_t *chip = snd_pcm_substream_chip(substream);
454 dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
455 ptr = snd_dma_pointer(dma, chip->p_dma_size);
456 return bytes_to_frames(substream->runtime, ptr);
459 static snd_pcm_uframes_t snd_sb16_capture_pointer(snd_pcm_substream_t * substream)
461 sb_t *chip = snd_pcm_substream_chip(substream);
465 dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
466 ptr = snd_dma_pointer(dma, chip->c_dma_size);
467 return bytes_to_frames(substream->runtime, ptr);
474 static snd_pcm_hardware_t snd_sb16_playback =
476 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
477 SNDRV_PCM_INFO_MMAP_VALID),
479 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
484 .buffer_bytes_max = (128*1024),
485 .period_bytes_min = 64,
486 .period_bytes_max = (128*1024),
492 static snd_pcm_hardware_t snd_sb16_capture =
494 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
495 SNDRV_PCM_INFO_MMAP_VALID),
497 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
502 .buffer_bytes_max = (128*1024),
503 .period_bytes_min = 64,
504 .period_bytes_max = (128*1024),
514 int snd_sb16_playback_open(snd_pcm_substream_t * substream)
517 sb_t *chip = snd_pcm_substream_chip(substream);
518 snd_pcm_runtime_t *runtime = substream->runtime;
520 spin_lock_irqsave(&chip->open_lock, flags);
521 if (chip->mode & SB_MODE_PLAYBACK) {
522 spin_unlock_irqrestore(&chip->open_lock, flags);
525 runtime->hw = snd_sb16_playback;
527 /* skip if 16 bit DMA was reserved for capture */
528 if (chip->force_mode16 & SB_MODE_CAPTURE_16)
531 if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_CAPTURE_16)) {
532 chip->mode |= SB_MODE_PLAYBACK_16;
533 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
535 if (chip->dma16 <= 3) {
536 runtime->hw.buffer_bytes_max =
537 runtime->hw.period_bytes_max = 64 * 1024;
539 snd_sb16_csp_playback_open(chip, runtime);
545 if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_CAPTURE_8)) {
546 chip->mode |= SB_MODE_PLAYBACK_8;
547 /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
548 if (chip->dma16 < 0) {
549 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
550 chip->mode |= SB_MODE_PLAYBACK_16;
552 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
554 runtime->hw.buffer_bytes_max =
555 runtime->hw.period_bytes_max = 64 * 1024;
558 spin_unlock_irqrestore(&chip->open_lock, flags);
562 if (chip->hardware == SB_HW_ALS100)
563 runtime->hw.rate_max = 48000;
564 if (chip->mode & SB_RATE_LOCK)
565 runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
566 chip->playback_substream = substream;
567 spin_unlock_irqrestore(&chip->open_lock, flags);
571 int snd_sb16_playback_close(snd_pcm_substream_t * substream)
574 sb_t *chip = snd_pcm_substream_chip(substream);
576 snd_sb16_csp_playback_close(chip);
577 spin_lock_irqsave(&chip->open_lock, flags);
578 chip->playback_substream = NULL;
579 chip->mode &= ~SB_MODE_PLAYBACK;
580 spin_unlock_irqrestore(&chip->open_lock, flags);
584 int snd_sb16_capture_open(snd_pcm_substream_t * substream)
587 sb_t *chip = snd_pcm_substream_chip(substream);
588 snd_pcm_runtime_t *runtime = substream->runtime;
590 spin_lock_irqsave(&chip->open_lock, flags);
591 if (chip->mode & SB_MODE_CAPTURE) {
592 spin_unlock_irqrestore(&chip->open_lock, flags);
595 runtime->hw = snd_sb16_capture;
597 /* skip if 16 bit DMA was reserved for playback */
598 if (chip->force_mode16 & SB_MODE_PLAYBACK_16)
601 if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_16)) {
602 chip->mode |= SB_MODE_CAPTURE_16;
603 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
605 if (chip->dma16 <= 3) {
606 runtime->hw.buffer_bytes_max =
607 runtime->hw.period_bytes_max = 64 * 1024;
609 snd_sb16_csp_capture_open(chip, runtime);
615 if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_8)) {
616 chip->mode |= SB_MODE_CAPTURE_8;
617 /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
618 if (chip->dma16 < 0) {
619 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
620 chip->mode |= SB_MODE_CAPTURE_16;
622 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
624 runtime->hw.buffer_bytes_max =
625 runtime->hw.period_bytes_max = 64 * 1024;
628 spin_unlock_irqrestore(&chip->open_lock, flags);
632 if (chip->hardware == SB_HW_ALS100)
633 runtime->hw.rate_max = 48000;
634 if (chip->mode & SB_RATE_LOCK)
635 runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
636 chip->capture_substream = substream;
637 spin_unlock_irqrestore(&chip->open_lock, flags);
641 int snd_sb16_capture_close(snd_pcm_substream_t * substream)
644 sb_t *chip = snd_pcm_substream_chip(substream);
646 snd_sb16_csp_capture_close(chip);
647 spin_lock_irqsave(&chip->open_lock, flags);
648 chip->capture_substream = NULL;
649 chip->mode &= ~SB_MODE_CAPTURE;
650 spin_unlock_irqrestore(&chip->open_lock, flags);
655 * DMA control interface
658 static int snd_sb16_set_dma_mode(sb_t *chip, int what)
660 if (chip->dma8 < 0 || chip->dma16 < 0) {
661 snd_assert(what == 0, return -EINVAL);
665 chip->force_mode16 = 0;
666 } else if (what == 1) {
667 chip->force_mode16 = SB_MODE_PLAYBACK_16;
668 } else if (what == 2) {
669 chip->force_mode16 = SB_MODE_CAPTURE_16;
676 static int snd_sb16_get_dma_mode(sb_t *chip)
678 if (chip->dma8 < 0 || chip->dma16 < 0)
680 switch (chip->force_mode16) {
681 case SB_MODE_PLAYBACK_16:
683 case SB_MODE_CAPTURE_16:
690 static int snd_sb16_dma_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
692 static char *texts[3] = {
693 "Auto", "Playback", "Capture"
696 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
698 uinfo->value.enumerated.items = 3;
699 if (uinfo->value.enumerated.item > 2)
700 uinfo->value.enumerated.item = 2;
701 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
705 static int snd_sb16_dma_control_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
707 sb_t *chip = snd_kcontrol_chip(kcontrol);
710 spin_lock_irqsave(&chip->reg_lock, flags);
711 ucontrol->value.enumerated.item[0] = snd_sb16_get_dma_mode(chip);
712 spin_unlock_irqrestore(&chip->reg_lock, flags);
716 static int snd_sb16_dma_control_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
718 sb_t *chip = snd_kcontrol_chip(kcontrol);
720 unsigned char nval, oval;
723 if ((nval = ucontrol->value.enumerated.item[0]) > 2)
725 spin_lock_irqsave(&chip->reg_lock, flags);
726 oval = snd_sb16_get_dma_mode(chip);
727 change = nval != oval;
728 snd_sb16_set_dma_mode(chip, nval);
729 spin_unlock_irqrestore(&chip->reg_lock, flags);
733 snd_kcontrol_new_t snd_sb16_dma_control = {
734 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
735 .name = "16-bit DMA Allocation",
736 .info = snd_sb16_dma_control_info,
737 .get = snd_sb16_dma_control_get,
738 .put = snd_sb16_dma_control_put
742 * Initialization part
745 int snd_sb16dsp_configure(sb_t * chip)
748 unsigned char irqreg = 0, dmareg = 0, mpureg;
749 unsigned char realirq, realdma, realmpureg;
750 /* note: mpu register should be present only on SB16 Vibra soundcards */
752 // printk("codec->irq=%i, codec->dma8=%i, codec->dma16=%i\n", chip->irq, chip->dma8, chip->dma16);
753 spin_lock_irqsave(&chip->mixer_lock, flags);
754 mpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP) & ~0x06;
755 spin_unlock_irqrestore(&chip->mixer_lock, flags);
759 irqreg |= SB_IRQSETUP_IRQ9;
762 irqreg |= SB_IRQSETUP_IRQ5;
765 irqreg |= SB_IRQSETUP_IRQ7;
768 irqreg |= SB_IRQSETUP_IRQ10;
773 if (chip->dma8 >= 0) {
774 switch (chip->dma8) {
776 dmareg |= SB_DMASETUP_DMA0;
779 dmareg |= SB_DMASETUP_DMA1;
782 dmareg |= SB_DMASETUP_DMA3;
788 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
789 switch (chip->dma16) {
791 dmareg |= SB_DMASETUP_DMA5;
794 dmareg |= SB_DMASETUP_DMA6;
797 dmareg |= SB_DMASETUP_DMA7;
803 switch (chip->mpu_port) {
811 mpureg |= 0x02; /* disable MPU */
813 spin_lock_irqsave(&chip->mixer_lock, flags);
815 snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg);
816 realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP);
818 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg);
819 realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP);
821 snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg);
822 realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP);
824 spin_unlock_irqrestore(&chip->mixer_lock, flags);
825 if ((~realirq) & irqreg || (~realdma) & dmareg) {
826 snd_printk("SB16 [0x%lx]: unable to set DMA & IRQ (PnP device?)\n", chip->port);
827 snd_printk("SB16 [0x%lx]: wanted: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, realirq, realdma, realmpureg);
828 snd_printk("SB16 [0x%lx]: got: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, irqreg, dmareg, mpureg);
834 static snd_pcm_ops_t snd_sb16_playback_ops = {
835 .open = snd_sb16_playback_open,
836 .close = snd_sb16_playback_close,
837 .ioctl = snd_pcm_lib_ioctl,
838 .hw_params = snd_sb16_hw_params,
839 .hw_free = snd_sb16_hw_free,
840 .prepare = snd_sb16_playback_prepare,
841 .trigger = snd_sb16_playback_trigger,
842 .pointer = snd_sb16_playback_pointer,
845 static snd_pcm_ops_t snd_sb16_capture_ops = {
846 .open = snd_sb16_capture_open,
847 .close = snd_sb16_capture_close,
848 .ioctl = snd_pcm_lib_ioctl,
849 .hw_params = snd_sb16_hw_params,
850 .hw_free = snd_sb16_hw_free,
851 .prepare = snd_sb16_capture_prepare,
852 .trigger = snd_sb16_capture_trigger,
853 .pointer = snd_sb16_capture_pointer,
856 static void snd_sb16dsp_pcm_free(snd_pcm_t *pcm)
858 snd_pcm_lib_preallocate_free_for_all(pcm);
861 int snd_sb16dsp_pcm(sb_t * chip, int device, snd_pcm_t ** rpcm)
863 snd_card_t *card = chip->card;
869 if ((err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm)) < 0)
871 sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff);
872 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
873 pcm->private_data = chip;
874 pcm->private_free = snd_sb16dsp_pcm_free;
876 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb16_playback_ops);
877 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb16_capture_ops);
879 if (chip->dma16 >= 0 && chip->dma8 != chip->dma16)
880 snd_ctl_add(card, snd_ctl_new1(&snd_sb16_dma_control, chip));
882 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
884 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
893 const snd_pcm_ops_t *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);
908 static int __init alsa_sb16_init(void)
913 static void __exit alsa_sb16_exit(void)
917 module_init(alsa_sb16_init)
918 module_exit(alsa_sb16_exit)