ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / sb / sb16_main.c
1 /*
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
9  *        duplex!!!
10  *  Note: 16-bit wide is assigned to first direction which made request.
11  *        With full duplex - playback is preferred with abstract layer.
12  *
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.
18  *
19  *
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.
24  *
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.
29  *
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
33  *
34  */
35
36 #include <sound/driver.h>
37 #include <asm/io.h>
38 #include <asm/dma.h>
39 #include <linux/init.h>
40 #include <linux/time.h>
41 #include <sound/core.h>
42 #include <sound/sb.h>
43 #include <sound/sb16_csp.h>
44 #include <sound/mpu401.h>
45 #include <sound/control.h>
46 #include <sound/info.h>
47
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");
51
52 #define chip_t sb_t
53
54 #ifdef CONFIG_SND_SB16_CSP
55 static void snd_sb16_csp_playback_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
56 {
57         if (chip->hardware == SB_HW_16CSP) {
58                 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
59
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 */
67                                         goto __start_CSP;
68                                 }
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 */
76                                                 goto __start_CSP;
77                                         }
78                                 }
79                         }
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);
85                         } else {
86                       __start_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);
94                                 } else {
95                                         /* Success, CSP acquired and running */
96                                         chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE;
97                                 }
98                         }
99                 }
100         }
101 }
102
103 static void snd_sb16_csp_capture_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
104 {
105         if (chip->hardware == SB_HW_16CSP) {
106                 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
107
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 */
115                                         goto __start_CSP;
116                                 }
117                         }
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);
123                         } else {
124                       __start_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);
132                                 } else {
133                                         /* Success, CSP acquired and running */
134                                         chip->open = SNDRV_SB_CSP_MODE_DSP_READ;
135                                 }
136                         }
137                 }
138         }
139 }
140
141 static void snd_sb16_csp_update(sb_t *chip)
142 {
143         if (chip->hardware == SB_HW_16CSP) {
144                 snd_sb_csp_t *csp = snd_magic_cast(snd_sb_csp_t, chip->csp, return);
145
146                 if (csp->qpos_changed) {
147                         spin_lock(&chip->reg_lock);
148                         csp->ops.csp_qsound_transfer (csp);
149                         spin_unlock(&chip->reg_lock);
150                 }
151         }
152 }
153
154 static void snd_sb16_csp_playback_open(sb_t *chip, snd_pcm_runtime_t *runtime)
155 {
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);
159
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;
164                         }
165                 } else {
166                         /* autoloaded codecs */
167                         runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
168                                                SNDRV_PCM_FMTBIT_IMA_ADPCM;
169                 }
170         }
171 }
172
173 static void snd_sb16_csp_playback_close(sb_t *chip)
174 {
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);
177
178                 if (csp->ops.csp_stop(csp) == 0) {
179                         csp->ops.csp_unuse(csp);
180                         chip->open = 0;
181                 }
182         }
183 }
184
185 static void snd_sb16_csp_capture_open(sb_t *chip, snd_pcm_runtime_t *runtime)
186 {
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);
190
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;
195                         }
196                 } else {
197                         /* autoloaded codecs */
198                         runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
199                                                SNDRV_PCM_FMTBIT_IMA_ADPCM;
200                 }
201         }
202 }
203
204 static void snd_sb16_csp_capture_close(sb_t *chip)
205 {
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);
208
209                 if (csp->ops.csp_stop(csp) == 0) {
210                         csp->ops.csp_unuse(csp);
211                         chip->open = 0;
212                 }
213         }
214 }
215 #else
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*/
223 #endif
224
225
226 static void snd_sb16_setup_rate(sb_t *chip,
227                                 unsigned short rate,
228                                 int channel)
229 {
230         unsigned long flags;
231
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);
235         else
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);
245         }
246         spin_unlock_irqrestore(&chip->reg_lock, flags);
247 }
248
249 static int snd_sb16_hw_params(snd_pcm_substream_t * substream,
250                               snd_pcm_hw_params_t * hw_params)
251 {
252         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
253 }
254
255 static int snd_sb16_hw_free(snd_pcm_substream_t * substream)
256 {
257         snd_pcm_lib_free_pages(substream);
258         return 0;
259 }
260
261 static int snd_sb16_playback_prepare(snd_pcm_substream_t * substream)
262 {
263         unsigned long flags;
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;
268
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;
272         } else {
273                 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
274         }
275
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);
280
281         count = snd_pcm_lib_period_bytes(substream);
282         spin_lock_irqsave(&chip->reg_lock, flags);
283         if (chip->mode & SB_MODE_PLAYBACK_16) {
284                 count >>= 1;
285                 count--;
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);
291         } else {
292                 count--;
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);
298         }
299         spin_unlock_irqrestore(&chip->reg_lock, flags);
300         return 0;
301 }
302
303 static int snd_sb16_playback_trigger(snd_pcm_substream_t * substream,
304                                      int cmd)
305 {
306         sb_t *chip = snd_pcm_substream_chip(substream);
307         int result = 0;
308
309         spin_lock(&chip->reg_lock);
310         switch (cmd) {
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);
314                 break;
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;
321                 break;
322         default:
323                 result = -EINVAL;
324         }
325         spin_unlock(&chip->reg_lock);
326         return result;
327 }
328
329 static int snd_sb16_capture_prepare(snd_pcm_substream_t * substream)
330 {
331         unsigned long flags;
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;
336
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;
340         } else {
341                 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
342         }
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);
347
348         count = snd_pcm_lib_period_bytes(substream);
349         spin_lock_irqsave(&chip->reg_lock, flags);
350         if (chip->mode & SB_MODE_CAPTURE_16) {
351                 count >>= 1;
352                 count--;
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);
358         } else {
359                 count--;
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);
365         }
366         spin_unlock_irqrestore(&chip->reg_lock, flags);
367         return 0;
368 }
369
370 static int snd_sb16_capture_trigger(snd_pcm_substream_t * substream,
371                                     int cmd)
372 {
373         sb_t *chip = snd_pcm_substream_chip(substream);
374         int result = 0;
375
376         spin_lock(&chip->reg_lock);
377         switch (cmd) {
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);
381                 break;
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;
388                 break;
389         default:
390                 result = -EINVAL;
391         }
392         spin_unlock(&chip->reg_lock);
393         return result;
394 }
395
396 irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
397 {
398         sb_t *chip = snd_magic_cast(sb_t, dev_id, return IRQ_NONE);
399         unsigned char status;
400         int ok;
401
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) {
408                 ok = 0;
409                 if (chip->mode & SB_MODE_PLAYBACK_8) {
410                         snd_pcm_period_elapsed(chip->playback_substream);
411                         snd_sb16_csp_update(chip);
412                         ok++;
413                 }
414                 if (chip->mode & SB_MODE_CAPTURE_8) {
415                         snd_pcm_period_elapsed(chip->capture_substream);
416                         ok++;
417                 }
418                 spin_lock(&chip->reg_lock);
419                 if (!ok)
420                         snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
421                 snd_sb_ack_8bit(chip);
422                 spin_unlock(&chip->reg_lock);
423         }
424         if (status & SB_IRQTYPE_16BIT) {
425                 ok = 0;
426                 if (chip->mode & SB_MODE_PLAYBACK_16) {
427                         snd_pcm_period_elapsed(chip->playback_substream);
428                         snd_sb16_csp_update(chip);
429                         ok++;
430                 }
431                 if (chip->mode & SB_MODE_CAPTURE_16) {
432                         snd_pcm_period_elapsed(chip->capture_substream);
433                         ok++;
434                 }
435                 spin_lock(&chip->reg_lock);
436                 if (!ok)
437                         snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
438                 snd_sb_ack_16bit(chip);
439                 spin_unlock(&chip->reg_lock);
440         }
441         return IRQ_HANDLED;
442 }
443
444 /*
445
446  */
447
448 static snd_pcm_uframes_t snd_sb16_playback_pointer(snd_pcm_substream_t * substream)
449 {
450         sb_t *chip = snd_pcm_substream_chip(substream);
451         unsigned int dma;
452         size_t ptr;
453
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);
457 }
458
459 static snd_pcm_uframes_t snd_sb16_capture_pointer(snd_pcm_substream_t * substream)
460 {
461         sb_t *chip = snd_pcm_substream_chip(substream);
462         unsigned int dma;
463         size_t ptr;
464
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);
468 }
469
470 /*
471
472  */
473
474 static snd_pcm_hardware_t snd_sb16_playback =
475 {
476         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
477                                  SNDRV_PCM_INFO_MMAP_VALID),
478         .formats =              0,
479         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
480         .rate_min =             4000,
481         .rate_max =             44100,
482         .channels_min =         1,
483         .channels_max =         2,
484         .buffer_bytes_max =     (128*1024),
485         .period_bytes_min =     64,
486         .period_bytes_max =     (128*1024),
487         .periods_min =          1,
488         .periods_max =          1024,
489         .fifo_size =            0,
490 };
491
492 static snd_pcm_hardware_t snd_sb16_capture =
493 {
494         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
495                                  SNDRV_PCM_INFO_MMAP_VALID),
496         .formats =              0,
497         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
498         .rate_min =             4000,
499         .rate_max =             44100,
500         .channels_min =         1,
501         .channels_max =         2,
502         .buffer_bytes_max =     (128*1024),
503         .period_bytes_min =     64,
504         .period_bytes_max =     (128*1024),
505         .periods_min =          1,
506         .periods_max =          1024,
507         .fifo_size =            0,
508 };
509
510 /*
511  *  open/close
512  */
513
514 int snd_sb16_playback_open(snd_pcm_substream_t * substream)
515 {
516         unsigned long flags;
517         sb_t *chip = snd_pcm_substream_chip(substream);
518         snd_pcm_runtime_t *runtime = substream->runtime;
519
520         spin_lock_irqsave(&chip->open_lock, flags);
521         if (chip->mode & SB_MODE_PLAYBACK) {
522                 spin_unlock_irqrestore(&chip->open_lock, flags);
523                 return -EAGAIN;
524         }
525         runtime->hw = snd_sb16_playback;
526
527         /* skip if 16 bit DMA was reserved for capture */
528         if (chip->force_mode16 & SB_MODE_CAPTURE_16)
529                 goto __skip_16bit;
530
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;
534                 /* Vibra16X hack */
535                 if (chip->dma16 <= 3) {
536                         runtime->hw.buffer_bytes_max =
537                         runtime->hw.period_bytes_max = 64 * 1024;
538                 } else {
539                         snd_sb16_csp_playback_open(chip, runtime);
540                 }
541                 goto __open_ok;
542         }
543
544       __skip_16bit:
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;
551                 } else {
552                         runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
553                 }
554                 runtime->hw.buffer_bytes_max =
555                 runtime->hw.period_bytes_max = 64 * 1024;
556                 goto __open_ok;
557         }
558         spin_unlock_irqrestore(&chip->open_lock, flags);
559         return -EAGAIN;
560
561       __open_ok:
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);
568         return 0;
569 }
570
571 int snd_sb16_playback_close(snd_pcm_substream_t * substream)
572 {
573         unsigned long flags;
574         sb_t *chip = snd_pcm_substream_chip(substream);
575
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);
581         return 0;
582 }
583
584 int snd_sb16_capture_open(snd_pcm_substream_t * substream)
585 {
586         unsigned long flags;
587         sb_t *chip = snd_pcm_substream_chip(substream);
588         snd_pcm_runtime_t *runtime = substream->runtime;
589
590         spin_lock_irqsave(&chip->open_lock, flags);
591         if (chip->mode & SB_MODE_CAPTURE) {
592                 spin_unlock_irqrestore(&chip->open_lock, flags);
593                 return -EAGAIN;
594         }
595         runtime->hw = snd_sb16_capture;
596
597         /* skip if 16 bit DMA was reserved for playback */
598         if (chip->force_mode16 & SB_MODE_PLAYBACK_16)
599                 goto __skip_16bit;
600
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;
604                 /* Vibra16X hack */
605                 if (chip->dma16 <= 3) {
606                         runtime->hw.buffer_bytes_max =
607                         runtime->hw.period_bytes_max = 64 * 1024;
608                 } else {
609                         snd_sb16_csp_capture_open(chip, runtime);
610                 }
611                 goto __open_ok;
612         }
613
614       __skip_16bit:
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;
621                 } else {
622                         runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
623                 }
624                 runtime->hw.buffer_bytes_max =
625                 runtime->hw.period_bytes_max = 64 * 1024;
626                 goto __open_ok;
627         }
628         spin_unlock_irqrestore(&chip->open_lock, flags);
629         return -EAGAIN;
630
631       __open_ok:
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);
638         return 0;
639 }
640
641 int snd_sb16_capture_close(snd_pcm_substream_t * substream)
642 {
643         unsigned long flags;
644         sb_t *chip = snd_pcm_substream_chip(substream);
645
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);
651         return 0;
652 }
653
654 /*
655  *  DMA control interface
656  */
657
658 static int snd_sb16_set_dma_mode(sb_t *chip, int what)
659 {
660         if (chip->dma8 < 0 || chip->dma16 < 0) {
661                 snd_assert(what == 0, return -EINVAL);
662                 return 0;
663         }
664         if (what == 0) {
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;
670         } else {
671                 return -EINVAL;
672         }
673         return 0;
674 }
675
676 static int snd_sb16_get_dma_mode(sb_t *chip)
677 {
678         if (chip->dma8 < 0 || chip->dma16 < 0)
679                 return 0;
680         switch (chip->force_mode16) {
681         case SB_MODE_PLAYBACK_16:
682                 return 1;
683         case SB_MODE_CAPTURE_16:
684                 return 2;
685         default:
686                 return 0;
687         }
688 }
689
690 static int snd_sb16_dma_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
691 {
692         static char *texts[3] = {
693                 "Auto", "Playback", "Capture"
694         };
695
696         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
697         uinfo->count = 1;
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]);
702         return 0;
703 }
704
705 static int snd_sb16_dma_control_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
706 {
707         sb_t *chip = snd_kcontrol_chip(kcontrol);
708         unsigned long flags;
709         
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);
713         return 0;
714 }
715
716 static int snd_sb16_dma_control_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
717 {
718         sb_t *chip = snd_kcontrol_chip(kcontrol);
719         unsigned long flags;
720         unsigned char nval, oval;
721         int change;
722         
723         if ((nval = ucontrol->value.enumerated.item[0]) > 2)
724                 return -EINVAL;
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);
730         return change;
731 }
732
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
739 };
740
741 /*
742  *  Initialization part
743  */
744  
745 int snd_sb16dsp_configure(sb_t * chip)
746 {
747         unsigned long flags;
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 */
751
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);
756         switch (chip->irq) {
757         case 2:
758         case 9:
759                 irqreg |= SB_IRQSETUP_IRQ9;
760                 break;
761         case 5:
762                 irqreg |= SB_IRQSETUP_IRQ5;
763                 break;
764         case 7:
765                 irqreg |= SB_IRQSETUP_IRQ7;
766                 break;
767         case 10:
768                 irqreg |= SB_IRQSETUP_IRQ10;
769                 break;
770         default:
771                 return -EINVAL;
772         }
773         if (chip->dma8 >= 0) {
774                 switch (chip->dma8) {
775                 case 0:
776                         dmareg |= SB_DMASETUP_DMA0;
777                         break;
778                 case 1:
779                         dmareg |= SB_DMASETUP_DMA1;
780                         break;
781                 case 3:
782                         dmareg |= SB_DMASETUP_DMA3;
783                         break;
784                 default:
785                         return -EINVAL;
786                 }
787         }
788         if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
789                 switch (chip->dma16) {
790                 case 5:
791                         dmareg |= SB_DMASETUP_DMA5;
792                         break;
793                 case 6:
794                         dmareg |= SB_DMASETUP_DMA6;
795                         break;
796                 case 7:
797                         dmareg |= SB_DMASETUP_DMA7;
798                         break;
799                 default:
800                         return -EINVAL;
801                 }
802         }
803         switch (chip->mpu_port) {
804         case 0x300:
805                 mpureg |= 0x04;
806                 break;
807         case 0x330:
808                 mpureg |= 0x00;
809                 break;
810         default:
811                 mpureg |= 0x02; /* disable MPU */
812         }
813         spin_lock_irqsave(&chip->mixer_lock, flags);
814
815         snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg);
816         realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP);
817
818         snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg);
819         realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP);
820
821         snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg);
822         realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP);
823
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);
829                 return -ENODEV;
830         }
831         return 0;
832 }
833
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,
843 };
844
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,
854 };
855
856 static void snd_sb16dsp_pcm_free(snd_pcm_t *pcm)
857 {
858         snd_pcm_lib_preallocate_free_for_all(pcm);
859 }
860
861 int snd_sb16dsp_pcm(sb_t * chip, int device, snd_pcm_t ** rpcm)
862 {
863         snd_card_t *card = chip->card;
864         snd_pcm_t *pcm;
865         int err;
866
867         if (rpcm)
868                 *rpcm = NULL;
869         if ((err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm)) < 0)
870                 return err;
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;
875
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);
878
879         if (chip->dma16 >= 0 && chip->dma8 != chip->dma16)
880                 snd_ctl_add(card, snd_ctl_new1(&snd_sb16_dma_control, chip));
881         else
882                 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
883
884         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
885                                               snd_dma_isa_data(),
886                                               64*1024, 128*1024);
887
888         if (rpcm)
889                 *rpcm = pcm;
890         return 0;
891 }
892
893 const snd_pcm_ops_t *snd_sb16dsp_get_pcm_ops(int direction)
894 {
895         return direction == SNDRV_PCM_STREAM_PLAYBACK ?
896                 &snd_sb16_playback_ops : &snd_sb16_capture_ops;
897 }
898
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);
903
904 /*
905  *  INIT part
906  */
907
908 static int __init alsa_sb16_init(void)
909 {
910         return 0;
911 }
912
913 static void __exit alsa_sb16_exit(void)
914 {
915 }
916
917 module_init(alsa_sb16_init)
918 module_exit(alsa_sb16_exit)