]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - sound/isa/sb/sb16_main.c
Merge tag 'pwm/for-5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[mirror_ubuntu-focal-kernel.git] / sound / isa / sb / sb16_main.c
1 /*
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
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 <linux/io.h>
37 #include <asm/dma.h>
38 #include <linux/init.h>
39 #include <linux/time.h>
40 #include <linux/module.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@perex.cz>");
49 MODULE_DESCRIPTION("Routines for control of 16-bit SoundBlaster cards and clones");
50 MODULE_LICENSE("GPL");
51
52 #define runtime_format_bits(runtime) \
53 ((unsigned int)pcm_format_to_bits((runtime)->format))
54
55 #ifdef CONFIG_SND_SB16_CSP
56 static void snd_sb16_csp_playback_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
57 {
58 if (chip->hardware == SB_HW_16CSP) {
59 struct snd_sb_csp *csp = chip->csp;
60
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 */
68 goto __start_CSP;
69 }
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 */
77 goto __start_CSP;
78 }
79 }
80 }
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);
86 } else {
87 __start_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);
95 } else {
96 /* Success, CSP acquired and running */
97 chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE;
98 }
99 }
100 }
101 }
102 }
103
104 static void snd_sb16_csp_capture_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
105 {
106 if (chip->hardware == SB_HW_16CSP) {
107 struct snd_sb_csp *csp = chip->csp;
108
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 */
116 goto __start_CSP;
117 }
118 }
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);
124 } else {
125 __start_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);
133 } else {
134 /* Success, CSP acquired and running */
135 chip->open = SNDRV_SB_CSP_MODE_DSP_READ;
136 }
137 }
138 }
139 }
140 }
141
142 static void snd_sb16_csp_update(struct snd_sb *chip)
143 {
144 if (chip->hardware == SB_HW_16CSP) {
145 struct snd_sb_csp *csp = chip->csp;
146
147 if (csp->qpos_changed) {
148 spin_lock(&chip->reg_lock);
149 csp->ops.csp_qsound_transfer (csp);
150 spin_unlock(&chip->reg_lock);
151 }
152 }
153 }
154
155 static void snd_sb16_csp_playback_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
156 {
157 /* CSP decoders (QSound excluded) support only 16bit transfers */
158 if (chip->hardware == SB_HW_16CSP) {
159 struct snd_sb_csp *csp = chip->csp;
160
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;
165 }
166 } else {
167 /* autoloaded codecs */
168 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
169 SNDRV_PCM_FMTBIT_IMA_ADPCM;
170 }
171 }
172 }
173
174 static void snd_sb16_csp_playback_close(struct snd_sb *chip)
175 {
176 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_WRITE)) {
177 struct snd_sb_csp *csp = chip->csp;
178
179 if (csp->ops.csp_stop(csp) == 0) {
180 csp->ops.csp_unuse(csp);
181 chip->open = 0;
182 }
183 }
184 }
185
186 static void snd_sb16_csp_capture_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime)
187 {
188 /* CSP coders support only 16bit transfers */
189 if (chip->hardware == SB_HW_16CSP) {
190 struct snd_sb_csp *csp = chip->csp;
191
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;
196 }
197 } else {
198 /* autoloaded codecs */
199 runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
200 SNDRV_PCM_FMTBIT_IMA_ADPCM;
201 }
202 }
203 }
204
205 static void snd_sb16_csp_capture_close(struct snd_sb *chip)
206 {
207 if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_READ)) {
208 struct snd_sb_csp *csp = chip->csp;
209
210 if (csp->ops.csp_stop(csp) == 0) {
211 csp->ops.csp_unuse(csp);
212 chip->open = 0;
213 }
214 }
215 }
216 #else
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*/
224 #endif
225
226
227 static void snd_sb16_setup_rate(struct snd_sb *chip,
228 unsigned short rate,
229 int channel)
230 {
231 unsigned long flags;
232
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);
236 else
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);
246 }
247 spin_unlock_irqrestore(&chip->reg_lock, flags);
248 }
249
250 static int snd_sb16_hw_params(struct snd_pcm_substream *substream,
251 struct snd_pcm_hw_params *hw_params)
252 {
253 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
254 }
255
256 static int snd_sb16_hw_free(struct snd_pcm_substream *substream)
257 {
258 snd_pcm_lib_free_pages(substream);
259 return 0;
260 }
261
262 static int snd_sb16_playback_prepare(struct snd_pcm_substream *substream)
263 {
264 unsigned long flags;
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;
269
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;
273 } else {
274 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
275 }
276
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);
281
282 count = snd_pcm_lib_period_bytes(substream);
283 spin_lock_irqsave(&chip->reg_lock, flags);
284 if (chip->mode & SB_MODE_PLAYBACK_16) {
285 count >>= 1;
286 count--;
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);
292 } else {
293 count--;
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);
299 }
300 spin_unlock_irqrestore(&chip->reg_lock, flags);
301 return 0;
302 }
303
304 static int snd_sb16_playback_trigger(struct snd_pcm_substream *substream,
305 int cmd)
306 {
307 struct snd_sb *chip = snd_pcm_substream_chip(substream);
308 int result = 0;
309
310 spin_lock(&chip->reg_lock);
311 switch (cmd) {
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);
316 break;
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;
324 break;
325 default:
326 result = -EINVAL;
327 }
328 spin_unlock(&chip->reg_lock);
329 return result;
330 }
331
332 static int snd_sb16_capture_prepare(struct snd_pcm_substream *substream)
333 {
334 unsigned long flags;
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;
339
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;
343 } else {
344 format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
345 }
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);
350
351 count = snd_pcm_lib_period_bytes(substream);
352 spin_lock_irqsave(&chip->reg_lock, flags);
353 if (chip->mode & SB_MODE_CAPTURE_16) {
354 count >>= 1;
355 count--;
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);
361 } else {
362 count--;
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);
368 }
369 spin_unlock_irqrestore(&chip->reg_lock, flags);
370 return 0;
371 }
372
373 static int snd_sb16_capture_trigger(struct snd_pcm_substream *substream,
374 int cmd)
375 {
376 struct snd_sb *chip = snd_pcm_substream_chip(substream);
377 int result = 0;
378
379 spin_lock(&chip->reg_lock);
380 switch (cmd) {
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);
385 break;
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;
393 break;
394 default:
395 result = -EINVAL;
396 }
397 spin_unlock(&chip->reg_lock);
398 return result;
399 }
400
401 irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id)
402 {
403 struct snd_sb *chip = dev_id;
404 unsigned char status;
405 int ok;
406
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) {
413 ok = 0;
414 if (chip->mode & SB_MODE_PLAYBACK_8) {
415 snd_pcm_period_elapsed(chip->playback_substream);
416 snd_sb16_csp_update(chip);
417 ok++;
418 }
419 if (chip->mode & SB_MODE_CAPTURE_8) {
420 snd_pcm_period_elapsed(chip->capture_substream);
421 ok++;
422 }
423 spin_lock(&chip->reg_lock);
424 if (!ok)
425 snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
426 snd_sb_ack_8bit(chip);
427 spin_unlock(&chip->reg_lock);
428 }
429 if (status & SB_IRQTYPE_16BIT) {
430 ok = 0;
431 if (chip->mode & SB_MODE_PLAYBACK_16) {
432 snd_pcm_period_elapsed(chip->playback_substream);
433 snd_sb16_csp_update(chip);
434 ok++;
435 }
436 if (chip->mode & SB_MODE_CAPTURE_16) {
437 snd_pcm_period_elapsed(chip->capture_substream);
438 ok++;
439 }
440 spin_lock(&chip->reg_lock);
441 if (!ok)
442 snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
443 snd_sb_ack_16bit(chip);
444 spin_unlock(&chip->reg_lock);
445 }
446 return IRQ_HANDLED;
447 }
448
449 /*
450
451 */
452
453 static snd_pcm_uframes_t snd_sb16_playback_pointer(struct snd_pcm_substream *substream)
454 {
455 struct snd_sb *chip = snd_pcm_substream_chip(substream);
456 unsigned int dma;
457 size_t ptr;
458
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);
462 }
463
464 static snd_pcm_uframes_t snd_sb16_capture_pointer(struct snd_pcm_substream *substream)
465 {
466 struct snd_sb *chip = snd_pcm_substream_chip(substream);
467 unsigned int dma;
468 size_t ptr;
469
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);
473 }
474
475 /*
476
477 */
478
479 static const struct snd_pcm_hardware snd_sb16_playback =
480 {
481 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
482 SNDRV_PCM_INFO_MMAP_VALID),
483 .formats = 0,
484 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
485 .rate_min = 4000,
486 .rate_max = 44100,
487 .channels_min = 1,
488 .channels_max = 2,
489 .buffer_bytes_max = (128*1024),
490 .period_bytes_min = 64,
491 .period_bytes_max = (128*1024),
492 .periods_min = 1,
493 .periods_max = 1024,
494 .fifo_size = 0,
495 };
496
497 static const struct snd_pcm_hardware snd_sb16_capture =
498 {
499 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
500 SNDRV_PCM_INFO_MMAP_VALID),
501 .formats = 0,
502 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
503 .rate_min = 4000,
504 .rate_max = 44100,
505 .channels_min = 1,
506 .channels_max = 2,
507 .buffer_bytes_max = (128*1024),
508 .period_bytes_min = 64,
509 .period_bytes_max = (128*1024),
510 .periods_min = 1,
511 .periods_max = 1024,
512 .fifo_size = 0,
513 };
514
515 /*
516 * open/close
517 */
518
519 static int snd_sb16_playback_open(struct snd_pcm_substream *substream)
520 {
521 unsigned long flags;
522 struct snd_sb *chip = snd_pcm_substream_chip(substream);
523 struct snd_pcm_runtime *runtime = substream->runtime;
524
525 spin_lock_irqsave(&chip->open_lock, flags);
526 if (chip->mode & SB_MODE_PLAYBACK) {
527 spin_unlock_irqrestore(&chip->open_lock, flags);
528 return -EAGAIN;
529 }
530 runtime->hw = snd_sb16_playback;
531
532 /* skip if 16 bit DMA was reserved for capture */
533 if (chip->force_mode16 & SB_MODE_CAPTURE_16)
534 goto __skip_16bit;
535
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;
539 /* Vibra16X hack */
540 if (chip->dma16 <= 3) {
541 runtime->hw.buffer_bytes_max =
542 runtime->hw.period_bytes_max = 64 * 1024;
543 } else {
544 snd_sb16_csp_playback_open(chip, runtime);
545 }
546 goto __open_ok;
547 }
548
549 __skip_16bit:
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;
556 } else {
557 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
558 }
559 runtime->hw.buffer_bytes_max =
560 runtime->hw.period_bytes_max = 64 * 1024;
561 goto __open_ok;
562 }
563 spin_unlock_irqrestore(&chip->open_lock, flags);
564 return -EAGAIN;
565
566 __open_ok:
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;
573 }
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);
578 return 0;
579 }
580
581 static int snd_sb16_playback_close(struct snd_pcm_substream *substream)
582 {
583 unsigned long flags;
584 struct snd_sb *chip = snd_pcm_substream_chip(substream);
585
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);
591 return 0;
592 }
593
594 static int snd_sb16_capture_open(struct snd_pcm_substream *substream)
595 {
596 unsigned long flags;
597 struct snd_sb *chip = snd_pcm_substream_chip(substream);
598 struct snd_pcm_runtime *runtime = substream->runtime;
599
600 spin_lock_irqsave(&chip->open_lock, flags);
601 if (chip->mode & SB_MODE_CAPTURE) {
602 spin_unlock_irqrestore(&chip->open_lock, flags);
603 return -EAGAIN;
604 }
605 runtime->hw = snd_sb16_capture;
606
607 /* skip if 16 bit DMA was reserved for playback */
608 if (chip->force_mode16 & SB_MODE_PLAYBACK_16)
609 goto __skip_16bit;
610
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;
614 /* Vibra16X hack */
615 if (chip->dma16 <= 3) {
616 runtime->hw.buffer_bytes_max =
617 runtime->hw.period_bytes_max = 64 * 1024;
618 } else {
619 snd_sb16_csp_capture_open(chip, runtime);
620 }
621 goto __open_ok;
622 }
623
624 __skip_16bit:
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;
631 } else {
632 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
633 }
634 runtime->hw.buffer_bytes_max =
635 runtime->hw.period_bytes_max = 64 * 1024;
636 goto __open_ok;
637 }
638 spin_unlock_irqrestore(&chip->open_lock, flags);
639 return -EAGAIN;
640
641 __open_ok:
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;
648 }
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);
653 return 0;
654 }
655
656 static int snd_sb16_capture_close(struct snd_pcm_substream *substream)
657 {
658 unsigned long flags;
659 struct snd_sb *chip = snd_pcm_substream_chip(substream);
660
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);
666 return 0;
667 }
668
669 /*
670 * DMA control interface
671 */
672
673 static int snd_sb16_set_dma_mode(struct snd_sb *chip, int what)
674 {
675 if (chip->dma8 < 0 || chip->dma16 < 0) {
676 if (snd_BUG_ON(what))
677 return -EINVAL;
678 return 0;
679 }
680 if (what == 0) {
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;
686 } else {
687 return -EINVAL;
688 }
689 return 0;
690 }
691
692 static int snd_sb16_get_dma_mode(struct snd_sb *chip)
693 {
694 if (chip->dma8 < 0 || chip->dma16 < 0)
695 return 0;
696 switch (chip->force_mode16) {
697 case SB_MODE_PLAYBACK_16:
698 return 1;
699 case SB_MODE_CAPTURE_16:
700 return 2;
701 default:
702 return 0;
703 }
704 }
705
706 static int snd_sb16_dma_control_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
707 {
708 static const char * const texts[3] = {
709 "Auto", "Playback", "Capture"
710 };
711
712 return snd_ctl_enum_info(uinfo, 1, 3, texts);
713 }
714
715 static int snd_sb16_dma_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
716 {
717 struct snd_sb *chip = snd_kcontrol_chip(kcontrol);
718 unsigned long flags;
719
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);
723 return 0;
724 }
725
726 static int snd_sb16_dma_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
727 {
728 struct snd_sb *chip = snd_kcontrol_chip(kcontrol);
729 unsigned long flags;
730 unsigned char nval, oval;
731 int change;
732
733 if ((nval = ucontrol->value.enumerated.item[0]) > 2)
734 return -EINVAL;
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);
740 return change;
741 }
742
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
749 };
750
751 /*
752 * Initialization part
753 */
754
755 int snd_sb16dsp_configure(struct snd_sb * chip)
756 {
757 unsigned long flags;
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 */
761
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);
766 switch (chip->irq) {
767 case 2:
768 case 9:
769 irqreg |= SB_IRQSETUP_IRQ9;
770 break;
771 case 5:
772 irqreg |= SB_IRQSETUP_IRQ5;
773 break;
774 case 7:
775 irqreg |= SB_IRQSETUP_IRQ7;
776 break;
777 case 10:
778 irqreg |= SB_IRQSETUP_IRQ10;
779 break;
780 default:
781 return -EINVAL;
782 }
783 if (chip->dma8 >= 0) {
784 switch (chip->dma8) {
785 case 0:
786 dmareg |= SB_DMASETUP_DMA0;
787 break;
788 case 1:
789 dmareg |= SB_DMASETUP_DMA1;
790 break;
791 case 3:
792 dmareg |= SB_DMASETUP_DMA3;
793 break;
794 default:
795 return -EINVAL;
796 }
797 }
798 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
799 switch (chip->dma16) {
800 case 5:
801 dmareg |= SB_DMASETUP_DMA5;
802 break;
803 case 6:
804 dmareg |= SB_DMASETUP_DMA6;
805 break;
806 case 7:
807 dmareg |= SB_DMASETUP_DMA7;
808 break;
809 default:
810 return -EINVAL;
811 }
812 }
813 switch (chip->mpu_port) {
814 case 0x300:
815 mpureg |= 0x04;
816 break;
817 case 0x330:
818 mpureg |= 0x00;
819 break;
820 default:
821 mpureg |= 0x02; /* disable MPU */
822 }
823 spin_lock_irqsave(&chip->mixer_lock, flags);
824
825 snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg);
826 realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP);
827
828 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg);
829 realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP);
830
831 snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg);
832 realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP);
833
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);
839 return -ENODEV;
840 }
841 return 0;
842 }
843
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,
853 };
854
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,
864 };
865
866 int snd_sb16dsp_pcm(struct snd_sb *chip, int device)
867 {
868 struct snd_card *card = chip->card;
869 struct snd_pcm *pcm;
870 int err;
871
872 if ((err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm)) < 0)
873 return err;
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;
877 chip->pcm = pcm;
878
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);
881
882 if (chip->dma16 >= 0 && chip->dma8 != chip->dma16) {
883 err = snd_ctl_add(card, snd_ctl_new1(
884 &snd_sb16_dma_control, chip));
885 if (err)
886 return err;
887 } else {
888 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
889 }
890
891 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
892 card->dev,
893 64*1024, 128*1024);
894 return 0;
895 }
896
897 const struct snd_pcm_ops *snd_sb16dsp_get_pcm_ops(int direction)
898 {
899 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
900 &snd_sb16_playback_ops : &snd_sb16_capture_ops;
901 }
902
903 EXPORT_SYMBOL(snd_sb16dsp_pcm);
904 EXPORT_SYMBOL(snd_sb16dsp_get_pcm_ops);
905 EXPORT_SYMBOL(snd_sb16dsp_configure);
906 EXPORT_SYMBOL(snd_sb16dsp_interrupt);