Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[linux-2.6] / sound / isa / gus / gus_pcm.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3  *  Routines for control of GF1 chip (PCM things)
4  *
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.
10  *
11  *
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.
16  *
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.
21  *
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
25  *
26  */
27
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"
35
36 /* maximum rate */
37
38 #define SNDRV_GF1_PCM_RATE              48000
39
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)
43
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;
60 };
61
62 static int snd_gf1_pcm_use_dma = 1;
63
64 static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data)
65 {
66         struct gus_pcm_private *pcmp = private_data;
67
68         if (pcmp) {
69                 atomic_dec(&pcmp->dma_count);
70                 wake_up(&pcmp->sleep);
71         }
72 }
73
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)
78 {
79         struct snd_gf1_dma_block block;
80         struct snd_pcm_runtime *runtime = substream->runtime;
81         struct gus_pcm_private *pcmp = runtime->private_data;
82
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;
101 }
102
103 static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream)
104 {
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;
115
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;
120         }
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 */
132         }
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);
156                 }
157                 spin_unlock_irqrestore(&gus->reg_lock, flags);
158         }
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;
166         }
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 */
174                 }
175         }
176         spin_unlock_irqrestore(&gus->reg_lock, flags);
177 }
178
179 static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus,
180                                        struct snd_gus_voice *pvoice)
181 {
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;
187
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;
192         }
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;
198         }               
199         gus = pcmp->gus;
200         runtime = pcmp->substream->runtime;
201
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 */
219         }
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;
227         }
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;
234         }
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;
243                 }
244         }
245         spin_unlock(&gus->reg_lock);
246
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);
257                 }
258         }
259 #endif
260 }
261
262 static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus,
263                                          struct snd_gus_voice * pvoice)
264 {
265         unsigned short vol;
266         int cvoice;
267         struct gus_pcm_private *pcmp = pvoice->private_data;
268
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);
289 }
290
291 static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus)
292 {
293 }
294
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)
298 {
299         unsigned int len;
300         unsigned long flags;
301
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);
317                         }
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);
328                                 }
329                         } else {
330                                 while (len--)
331                                         snd_gf1_poke(gus, pos++, *buf++ ^ invert);
332                         }
333                 }
334                 if (count > 0 && !in_interrupt()) {
335                         schedule_timeout_interruptible(1);
336                         if (signal_pending(current))
337                                 return -EAGAIN;
338                 }
339         }
340         return 0;
341 }
342
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)
348 {
349         struct snd_pcm_runtime *runtime = substream->runtime;
350         struct gus_pcm_private *pcmp = runtime->private_data;
351         unsigned int bpos, len;
352         
353         bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
354         len = samples_to_bytes(runtime, count);
355         if (snd_BUG_ON(bpos > pcmp->dma_size))
356                 return -EIO;
357         if (snd_BUG_ON(bpos + len > pcmp->dma_size))
358                 return -EIO;
359         if (copy_from_user(runtime->dma_area + bpos, src, len))
360                 return -EFAULT;
361         if (snd_gf1_pcm_use_dma && len > 32) {
362                 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
363         } else {
364                 struct snd_gus_card *gus = pcmp->gus;
365                 int err, w16, invert;
366
367                 w16 = (snd_pcm_format_width(runtime->format) == 16);
368                 invert = snd_pcm_format_unsigned(runtime->format);
369                 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
370                         return err;
371         }
372         return 0;
373 }
374
375 static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
376                                         int voice,
377                                         snd_pcm_uframes_t pos,
378                                         snd_pcm_uframes_t count)
379 {
380         struct snd_pcm_runtime *runtime = substream->runtime;
381         struct gus_pcm_private *pcmp = runtime->private_data;
382         unsigned int bpos, len;
383         
384         bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
385         len = samples_to_bytes(runtime, count);
386         if (snd_BUG_ON(bpos > pcmp->dma_size))
387                 return -EIO;
388         if (snd_BUG_ON(bpos + len > pcmp->dma_size))
389                 return -EIO;
390         snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
391         if (snd_gf1_pcm_use_dma && len > 32) {
392                 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
393         } else {
394                 struct snd_gus_card *gus = pcmp->gus;
395                 int err, w16, invert;
396
397                 w16 = (snd_pcm_format_width(runtime->format) == 16);
398                 invert = snd_pcm_format_unsigned(runtime->format);
399                 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
400                         return err;
401         }
402         return 0;
403 }
404
405 static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
406                                           struct snd_pcm_hw_params *hw_params)
407 {
408         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
409         struct snd_pcm_runtime *runtime = substream->runtime;
410         struct gus_pcm_private *pcmp = runtime->private_data;
411         int err;
412         
413         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
414                 return err;
415         if (err > 0) {  /* change */
416                 struct snd_gf1_mem_block *block;
417                 if (pcmp->memory > 0) {
418                         snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
419                         pcmp->memory = 0;
420                 }
421                 if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
422                                                SNDRV_GF1_MEM_OWNER_DRIVER,
423                                                "GF1 PCM",
424                                                runtime->dma_bytes, 1, 32,
425                                                NULL)) == NULL)
426                         return -ENOMEM;
427                 pcmp->memory = block->ptr;
428         }
429         pcmp->voices = params_channels(hw_params);
430         if (pcmp->pvoices[0] == NULL) {
431                 if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
432                         return -ENOMEM;
433                 pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
434                 pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
435                 pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
436                 pcmp->pvoices[0]->private_data = pcmp;
437         }
438         if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
439                 if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
440                         return -ENOMEM;
441                 pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
442                 pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
443                 pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
444                 pcmp->pvoices[1]->private_data = pcmp;
445         } else if (pcmp->voices == 1) {
446                 if (pcmp->pvoices[1]) {
447                         snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
448                         pcmp->pvoices[1] = NULL;
449                 }
450         }
451         return 0;
452 }
453
454 static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
455 {
456         struct snd_pcm_runtime *runtime = substream->runtime;
457         struct gus_pcm_private *pcmp = runtime->private_data;
458
459         snd_pcm_lib_free_pages(substream);
460         if (pcmp->pvoices[0]) {
461                 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
462                 pcmp->pvoices[0] = NULL;
463         }
464         if (pcmp->pvoices[1]) {
465                 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
466                 pcmp->pvoices[1] = NULL;
467         }
468         if (pcmp->memory > 0) {
469                 snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
470                 pcmp->memory = 0;
471         }
472         return 0;
473 }
474
475 static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
476 {
477         struct snd_pcm_runtime *runtime = substream->runtime;
478         struct gus_pcm_private *pcmp = runtime->private_data;
479
480         pcmp->bpos = 0;
481         pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
482         pcmp->block_size = snd_pcm_lib_period_bytes(substream);
483         pcmp->blocks = pcmp->dma_size / pcmp->block_size;
484         return 0;
485 }
486
487 static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
488                                         int cmd)
489 {
490         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
491         struct snd_pcm_runtime *runtime = substream->runtime;
492         struct gus_pcm_private *pcmp = runtime->private_data;
493         int voice;
494
495         if (cmd == SNDRV_PCM_TRIGGER_START) {
496                 snd_gf1_pcm_trigger_up(substream);
497         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
498                 spin_lock(&pcmp->lock);
499                 pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
500                 spin_unlock(&pcmp->lock);
501                 voice = pcmp->pvoices[0]->number;
502                 snd_gf1_stop_voices(gus, voice, voice);
503                 if (pcmp->pvoices[1]) {
504                         voice = pcmp->pvoices[1]->number;
505                         snd_gf1_stop_voices(gus, voice, voice);
506                 }
507         } else {
508                 return -EINVAL;
509         }
510         return 0;
511 }
512
513 static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
514 {
515         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
516         struct snd_pcm_runtime *runtime = substream->runtime;
517         struct gus_pcm_private *pcmp = runtime->private_data;
518         unsigned int pos;
519         unsigned char voice_ctrl;
520
521         pos = 0;
522         spin_lock(&gus->reg_lock);
523         if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
524                 snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
525                 voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
526                 pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
527                 if (substream->runtime->channels > 1)
528                         pos <<= 1;
529                 pos = bytes_to_frames(runtime, pos);
530         }
531         spin_unlock(&gus->reg_lock);
532         return pos;
533 }
534
535 static struct snd_ratnum clock = {
536         .num = 9878400/16,
537         .den_min = 2,
538         .den_max = 257,
539         .den_step = 1,
540 };
541
542 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks  = {
543         .nrats = 1,
544         .rats = &clock,
545 };
546
547 static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
548                                          struct snd_pcm_hw_params *hw_params)
549 {
550         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
551
552         gus->c_dma_size = params_buffer_bytes(hw_params);
553         gus->c_period_size = params_period_bytes(hw_params);
554         gus->c_pos = 0;
555         gus->gf1.pcm_rcntrl_reg = 0x21;         /* IRQ at end, enable & start */
556         if (params_channels(hw_params) > 1)
557                 gus->gf1.pcm_rcntrl_reg |= 2;
558         if (gus->gf1.dma2 > 3)
559                 gus->gf1.pcm_rcntrl_reg |= 4;
560         if (snd_pcm_format_unsigned(params_format(hw_params)))
561                 gus->gf1.pcm_rcntrl_reg |= 0x80;
562         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
563 }
564
565 static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream *substream)
566 {
567         return snd_pcm_lib_free_pages(substream);
568 }
569
570 static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
571 {
572         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
573         struct snd_pcm_runtime *runtime = substream->runtime;
574
575         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
576         snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
577         snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);     /* Sampling Control Register */
578         snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
579         return 0;
580 }
581
582 static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
583                                        int cmd)
584 {
585         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
586         int val;
587         
588         if (cmd == SNDRV_PCM_TRIGGER_START) {
589                 val = gus->gf1.pcm_rcntrl_reg;
590         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
591                 val = 0;
592         } else {
593                 return -EINVAL;
594         }
595
596         spin_lock(&gus->reg_lock);
597         snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
598         snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
599         spin_unlock(&gus->reg_lock);
600         return 0;
601 }
602
603 static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
604 {
605         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
606         int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
607         pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
608         return pos;
609 }
610
611 static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
612 {
613         snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
614         snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);     /* Sampling Control Register */
615         if (gus->pcm_cap_substream != NULL) {
616                 snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream); 
617                 snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
618                 gus->c_pos += gus->c_period_size;
619                 snd_pcm_period_elapsed(gus->pcm_cap_substream);
620         }
621 }
622
623 static struct snd_pcm_hardware snd_gf1_pcm_playback =
624 {
625         .info =                 SNDRV_PCM_INFO_NONINTERLEAVED,
626         .formats                = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
627                                  SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
628         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
629         .rate_min =             5510,
630         .rate_max =             48000,
631         .channels_min =         1,
632         .channels_max =         2,
633         .buffer_bytes_max =     (128*1024),
634         .period_bytes_min =     64,
635         .period_bytes_max =     (128*1024),
636         .periods_min =          1,
637         .periods_max =          1024,
638         .fifo_size =            0,
639 };
640
641 static struct snd_pcm_hardware snd_gf1_pcm_capture =
642 {
643         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
644                                  SNDRV_PCM_INFO_MMAP_VALID),
645         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
646         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
647         .rate_min =             5510,
648         .rate_max =             44100,
649         .channels_min =         1,
650         .channels_max =         2,
651         .buffer_bytes_max =     (128*1024),
652         .period_bytes_min =     64,
653         .period_bytes_max =     (128*1024),
654         .periods_min =          1,
655         .periods_max =          1024,
656         .fifo_size =            0,
657 };
658
659 static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
660 {
661         kfree(runtime->private_data);
662 }
663
664 static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
665 {
666         struct gus_pcm_private *pcmp;
667         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
668         struct snd_pcm_runtime *runtime = substream->runtime;
669         int err;
670
671         pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL);
672         if (pcmp == NULL)
673                 return -ENOMEM;
674         pcmp->gus = gus;
675         spin_lock_init(&pcmp->lock);
676         init_waitqueue_head(&pcmp->sleep);
677         atomic_set(&pcmp->dma_count, 0);
678
679         runtime->private_data = pcmp;
680         runtime->private_free = snd_gf1_pcm_playback_free;
681
682 #if 0
683         printk("playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n", (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer);
684 #endif
685         if ((err = snd_gf1_dma_init(gus)) < 0)
686                 return err;
687         pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
688         pcmp->substream = substream;
689         runtime->hw = snd_gf1_pcm_playback;
690         snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
691         snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
692         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
693         return 0;
694 }
695
696 static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
697 {
698         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
699         struct snd_pcm_runtime *runtime = substream->runtime;
700         struct gus_pcm_private *pcmp = runtime->private_data;
701         
702         if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
703                 snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n");
704
705         snd_gf1_dma_done(gus);  
706         return 0;
707 }
708
709 static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
710 {
711         struct snd_pcm_runtime *runtime = substream->runtime;
712         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
713
714         gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
715         gus->pcm_cap_substream = substream;
716         substream->runtime->hw = snd_gf1_pcm_capture;
717         snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
718         snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
719         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
720                                       &hw_constraints_clocks);
721         return 0;
722 }
723
724 static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
725 {
726         struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
727
728         gus->pcm_cap_substream = NULL;
729         snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
730         return 0;
731 }
732
733 static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
734 {
735         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
736         uinfo->count = 2;
737         uinfo->value.integer.min = 0;
738         uinfo->value.integer.max = 127;
739         return 0;
740 }
741
742 static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
743 {
744         struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
745         unsigned long flags;
746         
747         spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
748         ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
749         ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
750         spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
751         return 0;
752 }
753
754 static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
755 {
756         struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
757         unsigned long flags;
758         int change;
759         unsigned int idx;
760         unsigned short val1, val2, vol;
761         struct gus_pcm_private *pcmp;
762         struct snd_gus_voice *pvoice;
763         
764         val1 = ucontrol->value.integer.value[0] & 127;
765         val2 = ucontrol->value.integer.value[1] & 127;
766         spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
767         change = val1 != gus->gf1.pcm_volume_level_left1 ||
768                  val2 != gus->gf1.pcm_volume_level_right1;
769         gus->gf1.pcm_volume_level_left1 = val1;
770         gus->gf1.pcm_volume_level_right1 = val2;
771         gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
772         gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
773         spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
774         /* are we active? */
775         spin_lock_irqsave(&gus->voice_alloc, flags);
776         for (idx = 0; idx < 32; idx++) {
777                 pvoice = &gus->gf1.voices[idx];
778                 if (!pvoice->pcm)
779                         continue;
780                 pcmp = pvoice->private_data;
781                 if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
782                         continue;
783                 /* load real volume - better precision */
784                 spin_lock_irqsave(&gus->reg_lock, flags);
785                 snd_gf1_select_voice(gus, pvoice->number);
786                 snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
787                 vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
788                 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
789                 pcmp->final_volume = 1;
790                 spin_unlock_irqrestore(&gus->reg_lock, flags);
791         }
792         spin_unlock_irqrestore(&gus->voice_alloc, flags);
793         return change;
794 }
795
796 static struct snd_kcontrol_new snd_gf1_pcm_volume_control =
797 {
798         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799         .name = "PCM Playback Volume",
800         .info = snd_gf1_pcm_volume_info,
801         .get = snd_gf1_pcm_volume_get,
802         .put = snd_gf1_pcm_volume_put
803 };
804
805 static struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
806 {
807         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808         .name = "GPCM Playback Volume",
809         .info = snd_gf1_pcm_volume_info,
810         .get = snd_gf1_pcm_volume_get,
811         .put = snd_gf1_pcm_volume_put
812 };
813
814 static struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
815         .open =         snd_gf1_pcm_playback_open,
816         .close =        snd_gf1_pcm_playback_close,
817         .ioctl =        snd_pcm_lib_ioctl,
818         .hw_params =    snd_gf1_pcm_playback_hw_params,
819         .hw_free =      snd_gf1_pcm_playback_hw_free,
820         .prepare =      snd_gf1_pcm_playback_prepare,
821         .trigger =      snd_gf1_pcm_playback_trigger,
822         .pointer =      snd_gf1_pcm_playback_pointer,
823         .copy =         snd_gf1_pcm_playback_copy,
824         .silence =      snd_gf1_pcm_playback_silence,
825 };
826
827 static struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
828         .open =         snd_gf1_pcm_capture_open,
829         .close =        snd_gf1_pcm_capture_close,
830         .ioctl =        snd_pcm_lib_ioctl,
831         .hw_params =    snd_gf1_pcm_capture_hw_params,
832         .hw_free =      snd_gf1_pcm_capture_hw_free,
833         .prepare =      snd_gf1_pcm_capture_prepare,
834         .trigger =      snd_gf1_pcm_capture_trigger,
835         .pointer =      snd_gf1_pcm_capture_pointer,
836 };
837
838 int snd_gf1_pcm_new(struct snd_gus_card * gus, int pcm_dev, int control_index, struct snd_pcm ** rpcm)
839 {
840         struct snd_card *card;
841         struct snd_kcontrol *kctl;
842         struct snd_pcm *pcm;
843         struct snd_pcm_substream *substream;
844         int capture, err;
845
846         if (rpcm)
847                 *rpcm = NULL;
848         card = gus->card;
849         capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
850         err = snd_pcm_new(card,
851                           gus->interwave ? "AMD InterWave" : "GF1",
852                           pcm_dev,
853                           gus->gf1.pcm_channels / 2,
854                           capture,
855                           &pcm);
856         if (err < 0)
857                 return err;
858         pcm->private_data = gus;
859         /* playback setup */
860         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
861
862         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
863                 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
864                                               snd_dma_isa_data(),
865                                               64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
866         
867         pcm->info_flags = 0;
868         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
869         if (capture) {
870                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
871                 if (gus->gf1.dma2 == gus->gf1.dma1)
872                         pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
873                 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
874                                               SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
875                                               64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
876         }
877         strcpy(pcm->name, pcm->id);
878         if (gus->interwave) {
879                 sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
880         }
881         strcat(pcm->name, " (synth)");
882         gus->pcm = pcm;
883
884         if (gus->codec_flag)
885                 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
886         else
887                 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
888         if ((err = snd_ctl_add(card, kctl)) < 0)
889                 return err;
890         kctl->id.index = control_index;
891
892         if (rpcm)
893                 *rpcm = pcm;
894         return 0;
895 }
896