Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / isa / gus / gus_pcm.c
blob99731dc9732564fa1223becbadff7f2e0b9cb782
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Routines for control of GF1 chip (PCM things)
5 * InterWave chips supports interleaved DMA, but this feature isn't used in
6 * this code.
7 *
8 * This code emulates autoinit DMA transfer for playback, recording by GF1
9 * chip doesn't support autoinit DMA.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <asm/dma.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/gus.h>
33 #include <sound/pcm_params.h>
34 #include "gus_tables.h"
36 /* maximum rate */
38 #define SNDRV_GF1_PCM_RATE 48000
40 #define SNDRV_GF1_PCM_PFLG_NONE 0
41 #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0)
42 #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0)
44 struct gus_pcm_private {
45 struct snd_gus_card * gus;
46 struct snd_pcm_substream *substream;
47 spinlock_t lock;
48 unsigned int voices;
49 struct snd_gus_voice *pvoices[2];
50 unsigned int memory;
51 unsigned short flags;
52 unsigned char voice_ctrl, ramp_ctrl;
53 unsigned int bpos;
54 unsigned int blocks;
55 unsigned int block_size;
56 unsigned int dma_size;
57 wait_queue_head_t sleep;
58 atomic_t dma_count;
59 int final_volume;
62 static int snd_gf1_pcm_use_dma = 1;
64 static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data)
66 struct gus_pcm_private *pcmp = private_data;
68 if (pcmp) {
69 atomic_dec(&pcmp->dma_count);
70 wake_up(&pcmp->sleep);
74 static int snd_gf1_pcm_block_change(struct snd_pcm_substream *substream,
75 unsigned int offset,
76 unsigned int addr,
77 unsigned int count)
79 struct snd_gf1_dma_block block;
80 struct snd_pcm_runtime *runtime = substream->runtime;
81 struct gus_pcm_private *pcmp = runtime->private_data;
83 count += offset & 31;
84 offset &= ~31;
85 // snd_printk("block change - offset = 0x%x, count = 0x%x\n", offset, count);
86 memset(&block, 0, sizeof(block));
87 block.cmd = SNDRV_GF1_DMA_IRQ;
88 if (snd_pcm_format_unsigned(runtime->format))
89 block.cmd |= SNDRV_GF1_DMA_UNSIGNED;
90 if (snd_pcm_format_width(runtime->format) == 16)
91 block.cmd |= SNDRV_GF1_DMA_16BIT;
92 block.addr = addr & ~31;
93 block.buffer = runtime->dma_area + offset;
94 block.buf_addr = runtime->dma_addr + offset;
95 block.count = count;
96 block.private_data = pcmp;
97 block.ack = snd_gf1_pcm_block_change_ack;
98 if (!snd_gf1_dma_transfer_block(pcmp->gus, &block, 0, 0))
99 atomic_inc(&pcmp->dma_count);
100 return 0;
103 static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream)
105 struct snd_pcm_runtime *runtime = substream->runtime;
106 struct gus_pcm_private *pcmp = runtime->private_data;
107 struct snd_gus_card * gus = pcmp->gus;
108 unsigned long flags;
109 unsigned char voice_ctrl, ramp_ctrl;
110 unsigned short rate;
111 unsigned int curr, begin, end;
112 unsigned short vol;
113 unsigned char pan;
114 unsigned int voice;
116 spin_lock_irqsave(&pcmp->lock, flags);
117 if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
118 spin_unlock_irqrestore(&pcmp->lock, flags);
119 return;
121 pcmp->flags |= SNDRV_GF1_PCM_PFLG_ACTIVE;
122 pcmp->final_volume = 0;
123 spin_unlock_irqrestore(&pcmp->lock, flags);
124 rate = snd_gf1_translate_freq(gus, runtime->rate << 4);
125 /* enable WAVE IRQ */
126 voice_ctrl = snd_pcm_format_width(runtime->format) == 16 ? 0x24 : 0x20;
127 /* enable RAMP IRQ + rollover */
128 ramp_ctrl = 0x24;
129 if (pcmp->blocks == 1) {
130 voice_ctrl |= 0x08; /* loop enable */
131 ramp_ctrl &= ~0x04; /* disable rollover */
133 for (voice = 0; voice < pcmp->voices; voice++) {
134 begin = pcmp->memory + voice * (pcmp->dma_size / runtime->channels);
135 curr = begin + (pcmp->bpos * pcmp->block_size) / runtime->channels;
136 end = curr + (pcmp->block_size / runtime->channels);
137 end -= snd_pcm_format_width(runtime->format) == 16 ? 2 : 1;
138 // snd_printk("init: curr=0x%x, begin=0x%x, end=0x%x, ctrl=0x%x, ramp=0x%x, rate=0x%x\n", curr, begin, end, voice_ctrl, ramp_ctrl, rate);
139 pan = runtime->channels == 2 ? (!voice ? 1 : 14) : 8;
140 vol = !voice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
141 spin_lock_irqsave(&gus->reg_lock, flags);
142 snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
143 snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, pan);
144 snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, rate);
145 snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, begin << 4, voice_ctrl & 4);
146 snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
147 snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, curr << 4, voice_ctrl & 4);
148 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, SNDRV_GF1_MIN_VOLUME << 4);
149 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0x2f);
150 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);
151 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, vol >> 8);
152 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
153 if (!gus->gf1.enh_mode) {
154 snd_gf1_delay(gus);
155 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
157 spin_unlock_irqrestore(&gus->reg_lock, flags);
159 spin_lock_irqsave(&gus->reg_lock, flags);
160 for (voice = 0; voice < pcmp->voices; voice++) {
161 snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
162 if (gus->gf1.enh_mode)
163 snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, 0x00); /* deactivate voice */
164 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
165 voice_ctrl &= ~0x20;
167 voice_ctrl |= 0x20;
168 if (!gus->gf1.enh_mode) {
169 snd_gf1_delay(gus);
170 for (voice = 0; voice < pcmp->voices; voice++) {
171 snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
172 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
173 voice_ctrl &= ~0x20; /* disable IRQ for next voice */
176 spin_unlock_irqrestore(&gus->reg_lock, flags);
179 static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus,
180 struct snd_gus_voice *pvoice)
182 struct gus_pcm_private * pcmp;
183 struct snd_pcm_runtime *runtime;
184 unsigned char voice_ctrl, ramp_ctrl;
185 unsigned int idx;
186 unsigned int end, step;
188 if (!pvoice->private_data) {
189 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
190 snd_gf1_smart_stop_voice(gus, pvoice->number);
191 return;
193 pcmp = pvoice->private_data;
194 if (pcmp == NULL) {
195 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
196 snd_gf1_smart_stop_voice(gus, pvoice->number);
197 return;
199 gus = pcmp->gus;
200 runtime = pcmp->substream->runtime;
202 spin_lock(&gus->reg_lock);
203 snd_gf1_select_voice(gus, pvoice->number);
204 voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & ~0x8b;
205 ramp_ctrl = (snd_gf1_read8(gus, SNDRV_GF1_VB_VOLUME_CONTROL) & ~0xa4) | 0x03;
206 #if 0
207 snd_gf1_select_voice(gus, pvoice->number);
208 printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
209 snd_gf1_select_voice(gus, pcmp->pvoices[1]->number);
210 printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
211 snd_gf1_select_voice(gus, pvoice->number);
212 #endif
213 pcmp->bpos++;
214 pcmp->bpos %= pcmp->blocks;
215 if (pcmp->bpos + 1 >= pcmp->blocks) { /* last block? */
216 voice_ctrl |= 0x08; /* enable loop */
217 } else {
218 ramp_ctrl |= 0x04; /* enable rollover */
220 end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels);
221 end -= voice_ctrl & 4 ? 2 : 1;
222 step = pcmp->dma_size / runtime->channels;
223 voice_ctrl |= 0x20;
224 if (!pcmp->final_volume) {
225 ramp_ctrl |= 0x20;
226 ramp_ctrl &= ~0x03;
228 for (idx = 0; idx < pcmp->voices; idx++, end += step) {
229 snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
230 snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
231 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
232 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
233 voice_ctrl &= ~0x20;
235 if (!gus->gf1.enh_mode) {
236 snd_gf1_delay(gus);
237 voice_ctrl |= 0x20;
238 for (idx = 0; idx < pcmp->voices; idx++) {
239 snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
240 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
241 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
242 voice_ctrl &= ~0x20;
245 spin_unlock(&gus->reg_lock);
247 snd_pcm_period_elapsed(pcmp->substream);
248 #if 0
249 if ((runtime->flags & SNDRV_PCM_FLG_MMAP) &&
250 *runtime->state == SNDRV_PCM_STATE_RUNNING) {
251 end = pcmp->bpos * pcmp->block_size;
252 if (runtime->channels > 1) {
253 snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + (end / 2), pcmp->block_size / 2);
254 snd_gf1_pcm_block_change(pcmp->substream, end + (pcmp->block_size / 2), pcmp->memory + (pcmp->dma_size / 2) + (end / 2), pcmp->block_size / 2);
255 } else {
256 snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + end, pcmp->block_size);
259 #endif
262 static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus,
263 struct snd_gus_voice * pvoice)
265 unsigned short vol;
266 int cvoice;
267 struct gus_pcm_private *pcmp = pvoice->private_data;
269 /* stop ramp, but leave rollover bit untouched */
270 spin_lock(&gus->reg_lock);
271 snd_gf1_select_voice(gus, pvoice->number);
272 snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
273 spin_unlock(&gus->reg_lock);
274 if (pcmp == NULL)
275 return;
276 /* are we active? */
277 if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
278 return;
279 /* load real volume - better precision */
280 cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1;
281 if (pcmp->substream == NULL)
282 return;
283 vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
284 spin_lock(&gus->reg_lock);
285 snd_gf1_select_voice(gus, pvoice->number);
286 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
287 pcmp->final_volume = 1;
288 spin_unlock(&gus->reg_lock);
291 static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus)
295 static int snd_gf1_pcm_poke_block(struct snd_gus_card *gus, unsigned char *buf,
296 unsigned int pos, unsigned int count,
297 int w16, int invert)
299 unsigned int len;
300 unsigned long flags;
302 // printk("poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n", (int)buf, pos, count, gus->gf1.port);
303 while (count > 0) {
304 len = count;
305 if (len > 512) /* limit, to allow IRQ */
306 len = 512;
307 count -= len;
308 if (gus->interwave) {
309 spin_lock_irqsave(&gus->reg_lock, flags);
310 snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00));
311 snd_gf1_dram_addr(gus, pos);
312 if (w16) {
313 outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL));
314 outsw(GUSP(gus, GF1DATALOW), buf, len >> 1);
315 } else {
316 outsb(GUSP(gus, DRAM), buf, len);
318 spin_unlock_irqrestore(&gus->reg_lock, flags);
319 buf += 512;
320 pos += 512;
321 } else {
322 invert = invert ? 0x80 : 0x00;
323 if (w16) {
324 len >>= 1;
325 while (len--) {
326 snd_gf1_poke(gus, pos++, *buf++);
327 snd_gf1_poke(gus, pos++, *buf++ ^ invert);
329 } else {
330 while (len--)
331 snd_gf1_poke(gus, pos++, *buf++ ^ invert);
334 if (count > 0 && !in_interrupt()) {
335 schedule_timeout_interruptible(1);
336 if (signal_pending(current))
337 return -EAGAIN;
340 return 0;
343 static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream,
344 int voice,
345 snd_pcm_uframes_t pos,
346 void __user *src,
347 snd_pcm_uframes_t count)
349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct gus_pcm_private *pcmp = runtime->private_data;
351 unsigned int bpos, len;
353 bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
354 len = samples_to_bytes(runtime, count);
355 snd_assert(bpos <= pcmp->dma_size, return -EIO);
356 snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
357 if (copy_from_user(runtime->dma_area + bpos, src, len))
358 return -EFAULT;
359 if (snd_gf1_pcm_use_dma && len > 32) {
360 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
361 } else {
362 struct snd_gus_card *gus = pcmp->gus;
363 int err, w16, invert;
365 w16 = (snd_pcm_format_width(runtime->format) == 16);
366 invert = snd_pcm_format_unsigned(runtime->format);
367 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
368 return err;
370 return 0;
373 static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
374 int voice,
375 snd_pcm_uframes_t pos,
376 snd_pcm_uframes_t count)
378 struct snd_pcm_runtime *runtime = substream->runtime;
379 struct gus_pcm_private *pcmp = runtime->private_data;
380 unsigned int bpos, len;
382 bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
383 len = samples_to_bytes(runtime, count);
384 snd_assert(bpos <= pcmp->dma_size, return -EIO);
385 snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
386 snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
387 if (snd_gf1_pcm_use_dma && len > 32) {
388 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
389 } else {
390 struct snd_gus_card *gus = pcmp->gus;
391 int err, w16, invert;
393 w16 = (snd_pcm_format_width(runtime->format) == 16);
394 invert = snd_pcm_format_unsigned(runtime->format);
395 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
396 return err;
398 return 0;
401 static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
402 struct snd_pcm_hw_params *hw_params)
404 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
405 struct snd_pcm_runtime *runtime = substream->runtime;
406 struct gus_pcm_private *pcmp = runtime->private_data;
407 int err;
409 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
410 return err;
411 if (err > 0) { /* change */
412 struct snd_gf1_mem_block *block;
413 if (pcmp->memory > 0) {
414 snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
415 pcmp->memory = 0;
417 if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
418 SNDRV_GF1_MEM_OWNER_DRIVER,
419 "GF1 PCM",
420 runtime->dma_bytes, 1, 32,
421 NULL)) == NULL)
422 return -ENOMEM;
423 pcmp->memory = block->ptr;
425 pcmp->voices = params_channels(hw_params);
426 if (pcmp->pvoices[0] == NULL) {
427 if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
428 return -ENOMEM;
429 pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
430 pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
431 pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
432 pcmp->pvoices[0]->private_data = pcmp;
434 if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
435 if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
436 return -ENOMEM;
437 pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
438 pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
439 pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
440 pcmp->pvoices[1]->private_data = pcmp;
441 } else if (pcmp->voices == 1) {
442 if (pcmp->pvoices[1]) {
443 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
444 pcmp->pvoices[1] = NULL;
447 return 0;
450 static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
452 struct snd_pcm_runtime *runtime = substream->runtime;
453 struct gus_pcm_private *pcmp = runtime->private_data;
455 snd_pcm_lib_free_pages(substream);
456 if (pcmp->pvoices[0]) {
457 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
458 pcmp->pvoices[0] = NULL;
460 if (pcmp->pvoices[1]) {
461 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
462 pcmp->pvoices[1] = NULL;
464 if (pcmp->memory > 0) {
465 snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
466 pcmp->memory = 0;
468 return 0;
471 static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
473 struct snd_pcm_runtime *runtime = substream->runtime;
474 struct gus_pcm_private *pcmp = runtime->private_data;
476 pcmp->bpos = 0;
477 pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
478 pcmp->block_size = snd_pcm_lib_period_bytes(substream);
479 pcmp->blocks = pcmp->dma_size / pcmp->block_size;
480 return 0;
483 static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
484 int cmd)
486 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
487 struct snd_pcm_runtime *runtime = substream->runtime;
488 struct gus_pcm_private *pcmp = runtime->private_data;
489 int voice;
491 if (cmd == SNDRV_PCM_TRIGGER_START) {
492 snd_gf1_pcm_trigger_up(substream);
493 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
494 spin_lock(&pcmp->lock);
495 pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
496 spin_unlock(&pcmp->lock);
497 voice = pcmp->pvoices[0]->number;
498 snd_gf1_stop_voices(gus, voice, voice);
499 if (pcmp->pvoices[1]) {
500 voice = pcmp->pvoices[1]->number;
501 snd_gf1_stop_voices(gus, voice, voice);
503 } else {
504 return -EINVAL;
506 return 0;
509 static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
511 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
512 struct snd_pcm_runtime *runtime = substream->runtime;
513 struct gus_pcm_private *pcmp = runtime->private_data;
514 unsigned int pos;
515 unsigned char voice_ctrl;
517 pos = 0;
518 spin_lock(&gus->reg_lock);
519 if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
520 snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
521 voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
522 pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
523 if (substream->runtime->channels > 1)
524 pos <<= 1;
525 pos = bytes_to_frames(runtime, pos);
527 spin_unlock(&gus->reg_lock);
528 return pos;
531 static struct snd_ratnum clock = {
532 .num = 9878400/16,
533 .den_min = 2,
534 .den_max = 257,
535 .den_step = 1,
538 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
539 .nrats = 1,
540 .rats = &clock,
543 static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
544 struct snd_pcm_hw_params *hw_params)
546 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
548 gus->c_dma_size = params_buffer_bytes(hw_params);
549 gus->c_period_size = params_period_bytes(hw_params);
550 gus->c_pos = 0;
551 gus->gf1.pcm_rcntrl_reg = 0x21; /* IRQ at end, enable & start */
552 if (params_channels(hw_params) > 1)
553 gus->gf1.pcm_rcntrl_reg |= 2;
554 if (gus->gf1.dma2 > 3)
555 gus->gf1.pcm_rcntrl_reg |= 4;
556 if (snd_pcm_format_unsigned(params_format(hw_params)))
557 gus->gf1.pcm_rcntrl_reg |= 0x80;
558 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
561 static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream *substream)
563 return snd_pcm_lib_free_pages(substream);
566 static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
568 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
569 struct snd_pcm_runtime *runtime = substream->runtime;
571 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
572 snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
573 snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
574 snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
575 return 0;
578 static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
579 int cmd)
581 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
582 int val;
584 if (cmd == SNDRV_PCM_TRIGGER_START) {
585 val = gus->gf1.pcm_rcntrl_reg;
586 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
587 val = 0;
588 } else {
589 return -EINVAL;
592 spin_lock(&gus->reg_lock);
593 snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
594 snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
595 spin_unlock(&gus->reg_lock);
596 return 0;
599 static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
601 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
602 int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
603 pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
604 return pos;
607 static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
609 snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
610 snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
611 if (gus->pcm_cap_substream != NULL) {
612 snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream);
613 snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
614 gus->c_pos += gus->c_period_size;
615 snd_pcm_period_elapsed(gus->pcm_cap_substream);
619 static struct snd_pcm_hardware snd_gf1_pcm_playback =
621 .info = SNDRV_PCM_INFO_NONINTERLEAVED,
622 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
623 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
624 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
625 .rate_min = 5510,
626 .rate_max = 48000,
627 .channels_min = 1,
628 .channels_max = 2,
629 .buffer_bytes_max = (128*1024),
630 .period_bytes_min = 64,
631 .period_bytes_max = (128*1024),
632 .periods_min = 1,
633 .periods_max = 1024,
634 .fifo_size = 0,
637 static struct snd_pcm_hardware snd_gf1_pcm_capture =
639 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
640 SNDRV_PCM_INFO_MMAP_VALID),
641 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
642 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
643 .rate_min = 5510,
644 .rate_max = 44100,
645 .channels_min = 1,
646 .channels_max = 2,
647 .buffer_bytes_max = (128*1024),
648 .period_bytes_min = 64,
649 .period_bytes_max = (128*1024),
650 .periods_min = 1,
651 .periods_max = 1024,
652 .fifo_size = 0,
655 static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
657 kfree(runtime->private_data);
660 static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
662 struct gus_pcm_private *pcmp;
663 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 int err;
667 pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL);
668 if (pcmp == NULL)
669 return -ENOMEM;
670 pcmp->gus = gus;
671 spin_lock_init(&pcmp->lock);
672 init_waitqueue_head(&pcmp->sleep);
673 atomic_set(&pcmp->dma_count, 0);
675 runtime->private_data = pcmp;
676 runtime->private_free = snd_gf1_pcm_playback_free;
678 #if 0
679 printk("playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n", (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer);
680 #endif
681 if ((err = snd_gf1_dma_init(gus)) < 0)
682 return err;
683 pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
684 pcmp->substream = substream;
685 runtime->hw = snd_gf1_pcm_playback;
686 snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
687 snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
688 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
689 return 0;
692 static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
694 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 struct gus_pcm_private *pcmp = runtime->private_data;
698 if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
699 snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n");
701 snd_gf1_dma_done(gus);
702 return 0;
705 static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
707 struct snd_pcm_runtime *runtime = substream->runtime;
708 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
710 gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
711 gus->pcm_cap_substream = substream;
712 substream->runtime->hw = snd_gf1_pcm_capture;
713 snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
714 snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
715 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
716 &hw_constraints_clocks);
717 return 0;
720 static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
722 struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
724 gus->pcm_cap_substream = NULL;
725 snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
726 return 0;
729 static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
731 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
732 uinfo->count = 2;
733 uinfo->value.integer.min = 0;
734 uinfo->value.integer.max = 127;
735 return 0;
738 static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
740 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
741 unsigned long flags;
743 spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
744 ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
745 ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
746 spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
747 return 0;
750 static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
752 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
753 unsigned long flags;
754 int change;
755 unsigned int idx;
756 unsigned short val1, val2, vol;
757 struct gus_pcm_private *pcmp;
758 struct snd_gus_voice *pvoice;
760 val1 = ucontrol->value.integer.value[0] & 127;
761 val2 = ucontrol->value.integer.value[1] & 127;
762 spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
763 change = val1 != gus->gf1.pcm_volume_level_left1 ||
764 val2 != gus->gf1.pcm_volume_level_right1;
765 gus->gf1.pcm_volume_level_left1 = val1;
766 gus->gf1.pcm_volume_level_right1 = val2;
767 gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
768 gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
769 spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
770 /* are we active? */
771 spin_lock_irqsave(&gus->voice_alloc, flags);
772 for (idx = 0; idx < 32; idx++) {
773 pvoice = &gus->gf1.voices[idx];
774 if (!pvoice->pcm)
775 continue;
776 pcmp = pvoice->private_data;
777 if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
778 continue;
779 /* load real volume - better precision */
780 spin_lock_irqsave(&gus->reg_lock, flags);
781 snd_gf1_select_voice(gus, pvoice->number);
782 snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
783 vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
784 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
785 pcmp->final_volume = 1;
786 spin_unlock_irqrestore(&gus->reg_lock, flags);
788 spin_unlock_irqrestore(&gus->voice_alloc, flags);
789 return change;
792 static struct snd_kcontrol_new snd_gf1_pcm_volume_control =
794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
795 .name = "PCM Playback Volume",
796 .info = snd_gf1_pcm_volume_info,
797 .get = snd_gf1_pcm_volume_get,
798 .put = snd_gf1_pcm_volume_put
801 static struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
804 .name = "GPCM Playback Volume",
805 .info = snd_gf1_pcm_volume_info,
806 .get = snd_gf1_pcm_volume_get,
807 .put = snd_gf1_pcm_volume_put
810 static struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
811 .open = snd_gf1_pcm_playback_open,
812 .close = snd_gf1_pcm_playback_close,
813 .ioctl = snd_pcm_lib_ioctl,
814 .hw_params = snd_gf1_pcm_playback_hw_params,
815 .hw_free = snd_gf1_pcm_playback_hw_free,
816 .prepare = snd_gf1_pcm_playback_prepare,
817 .trigger = snd_gf1_pcm_playback_trigger,
818 .pointer = snd_gf1_pcm_playback_pointer,
819 .copy = snd_gf1_pcm_playback_copy,
820 .silence = snd_gf1_pcm_playback_silence,
823 static struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
824 .open = snd_gf1_pcm_capture_open,
825 .close = snd_gf1_pcm_capture_close,
826 .ioctl = snd_pcm_lib_ioctl,
827 .hw_params = snd_gf1_pcm_capture_hw_params,
828 .hw_free = snd_gf1_pcm_capture_hw_free,
829 .prepare = snd_gf1_pcm_capture_prepare,
830 .trigger = snd_gf1_pcm_capture_trigger,
831 .pointer = snd_gf1_pcm_capture_pointer,
834 int snd_gf1_pcm_new(struct snd_gus_card * gus, int pcm_dev, int control_index, struct snd_pcm ** rpcm)
836 struct snd_card *card;
837 struct snd_kcontrol *kctl;
838 struct snd_pcm *pcm;
839 struct snd_pcm_substream *substream;
840 int capture, err;
842 if (rpcm)
843 *rpcm = NULL;
844 card = gus->card;
845 capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
846 err = snd_pcm_new(card,
847 gus->interwave ? "AMD InterWave" : "GF1",
848 pcm_dev,
849 gus->gf1.pcm_channels / 2,
850 capture,
851 &pcm);
852 if (err < 0)
853 return err;
854 pcm->private_data = gus;
855 /* playback setup */
856 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
858 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
859 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
860 snd_dma_isa_data(),
861 64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
863 pcm->info_flags = 0;
864 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
865 if (capture) {
866 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
867 if (gus->gf1.dma2 == gus->gf1.dma1)
868 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
869 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
870 SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
871 64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
873 strcpy(pcm->name, pcm->id);
874 if (gus->interwave) {
875 sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
877 strcat(pcm->name, " (synth)");
878 gus->pcm = pcm;
880 if (gus->codec_flag)
881 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
882 else
883 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
884 if ((err = snd_ctl_add(card, kctl)) < 0)
885 return err;
886 kctl->id.index = control_index;
888 if (rpcm)
889 *rpcm = pcm;
890 return 0;