patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / gus / gus_pcm.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
29 #include <asm/dma.h>
30 #include <linux/slab.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/gus.h>
34 #include <sound/pcm_params.h>
35 #include "gus_tables.h"
36
37 #define chip_t snd_gus_card_t
38
39 /* maximum rate */
40
41 #define SNDRV_GF1_PCM_RATE              48000
42
43 #define SNDRV_GF1_PCM_PFLG_NONE         0
44 #define SNDRV_GF1_PCM_PFLG_ACTIVE       (1<<0)
45 #define SNDRV_GF1_PCM_PFLG_NEUTRAL      (2<<0)
46
47 typedef struct {
48         snd_gus_card_t * gus;
49         snd_pcm_substream_t * substream;
50         spinlock_t lock;
51         unsigned int voices;
52         snd_gus_voice_t *pvoices[2];
53         unsigned int memory;
54         unsigned short flags;
55         unsigned char voice_ctrl, ramp_ctrl;
56         unsigned int bpos;
57         unsigned int blocks;
58         unsigned int block_size;
59         unsigned int dma_size;
60         wait_queue_head_t sleep;
61         atomic_t dma_count;
62         int final_volume;
63 } gus_pcm_private_t;
64
65 static int snd_gf1_pcm_use_dma = 1;
66
67 static void snd_gf1_pcm_block_change_ack(snd_gus_card_t * gus, void *private_data)
68 {
69         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, private_data, return);
70
71         if (pcmp) {
72                 atomic_dec(&pcmp->dma_count);
73                 wake_up(&pcmp->sleep);
74         }
75 }
76
77 static int snd_gf1_pcm_block_change(snd_pcm_substream_t * substream,
78                                     unsigned int offset,
79                                     unsigned int addr,
80                                     unsigned int count)
81 {
82         snd_gf1_dma_block_t block;
83         snd_pcm_runtime_t *runtime = substream->runtime;
84         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
85
86         count += offset & 31;
87         offset &= ~31;
88         // snd_printk("block change - offset = 0x%x, count = 0x%x\n", offset, count);
89         memset(&block, 0, sizeof(block));
90         block.cmd = SNDRV_GF1_DMA_IRQ;
91         if (snd_pcm_format_unsigned(runtime->format))
92                 block.cmd |= SNDRV_GF1_DMA_UNSIGNED;
93         if (snd_pcm_format_width(runtime->format) == 16)
94                 block.cmd |= SNDRV_GF1_DMA_16BIT;
95         block.addr = addr & ~31;
96         block.buffer = runtime->dma_area + offset;
97         block.buf_addr = runtime->dma_addr + offset;
98         block.count = count;
99         block.private_data = pcmp;
100         block.ack = snd_gf1_pcm_block_change_ack;
101         if (!snd_gf1_dma_transfer_block(pcmp->gus, &block, 0, 0))
102                 atomic_inc(&pcmp->dma_count);
103         return 0;
104 }
105
106 static void snd_gf1_pcm_trigger_up(snd_pcm_substream_t * substream)
107 {
108         snd_pcm_runtime_t *runtime = substream->runtime;
109         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return);
110         snd_gus_card_t * gus = pcmp->gus;
111         unsigned long flags;
112         unsigned char voice_ctrl, ramp_ctrl;
113         unsigned short rate;
114         unsigned int curr, begin, end;
115         unsigned short vol;
116         unsigned char pan;
117         unsigned int voice;
118
119         if (substream == NULL)
120                 return;
121         spin_lock_irqsave(&pcmp->lock, flags);
122         if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
123                 spin_unlock_irqrestore(&pcmp->lock, flags);
124                 return;
125         }
126         pcmp->flags |= SNDRV_GF1_PCM_PFLG_ACTIVE;
127         pcmp->final_volume = 0;
128         spin_unlock_irqrestore(&pcmp->lock, flags);
129         rate = snd_gf1_translate_freq(gus, runtime->rate << 4);
130         /* enable WAVE IRQ */
131         voice_ctrl = snd_pcm_format_width(runtime->format) == 16 ? 0x24 : 0x20;
132         /* enable RAMP IRQ + rollover */
133         ramp_ctrl = 0x24;
134         if (pcmp->blocks == 1) {
135                 voice_ctrl |= 0x08;     /* loop enable */
136                 ramp_ctrl &= ~0x04;     /* disable rollover */
137         }
138         for (voice = 0; voice < pcmp->voices; voice++) {
139                 begin = pcmp->memory + voice * (pcmp->dma_size / runtime->channels);
140                 curr = begin + (pcmp->bpos * pcmp->block_size) / runtime->channels;
141                 end = curr + (pcmp->block_size / runtime->channels);
142                 end -= snd_pcm_format_width(runtime->format) == 16 ? 2 : 1;
143                 // 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);
144                 pan = runtime->channels == 2 ? (!voice ? 1 : 14) : 8;
145                 vol = !voice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
146                 spin_lock_irqsave(&gus->reg_lock, flags);
147                 snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
148                 snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, pan);
149                 snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, rate);
150                 snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, begin << 4, voice_ctrl & 4);
151                 snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
152                 snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, curr << 4, voice_ctrl & 4);
153                 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, SNDRV_GF1_MIN_VOLUME << 4);
154                 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0x2f);
155                 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);
156                 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, vol >> 8);
157                 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
158                 if (!gus->gf1.enh_mode) {
159                         snd_gf1_delay(gus);
160                         snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
161                 }
162                 spin_unlock_irqrestore(&gus->reg_lock, flags);
163         }
164         spin_lock_irqsave(&gus->reg_lock, flags);
165         for (voice = 0; voice < pcmp->voices; voice++) {
166                 snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
167                 if (gus->gf1.enh_mode)
168                         snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, 0x00);   /* deactivate voice */
169                 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
170                 voice_ctrl &= ~0x20;
171         }
172         voice_ctrl |= 0x20;
173         if (!gus->gf1.enh_mode) {
174                 snd_gf1_delay(gus);
175                 for (voice = 0; voice < pcmp->voices; voice++) {
176                         snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
177                         snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
178                         voice_ctrl &= ~0x20;    /* disable IRQ for next voice */
179                 }
180         }
181         spin_unlock_irqrestore(&gus->reg_lock, flags);
182 }
183
184 static void snd_gf1_pcm_interrupt_wave(snd_gus_card_t * gus, snd_gus_voice_t *pvoice)
185 {
186         gus_pcm_private_t * pcmp;
187         snd_pcm_runtime_t * runtime;
188         unsigned char voice_ctrl, ramp_ctrl;
189         unsigned int idx;
190         unsigned int end, step;
191
192         if (!pvoice->private_data) {
193                 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
194                 snd_gf1_smart_stop_voice(gus, pvoice->number);
195                 return;
196         }
197         pcmp = snd_magic_cast(gus_pcm_private_t, pvoice->private_data, return);
198         if (pcmp == NULL) {
199                 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
200                 snd_gf1_smart_stop_voice(gus, pvoice->number);
201                 return;
202         }               
203         gus = pcmp->gus;
204         runtime = pcmp->substream->runtime;
205
206         spin_lock(&gus->reg_lock);
207         snd_gf1_select_voice(gus, pvoice->number);
208         voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & ~0x8b;
209         ramp_ctrl = (snd_gf1_read8(gus, SNDRV_GF1_VB_VOLUME_CONTROL) & ~0xa4) | 0x03;
210 #if 0
211         snd_gf1_select_voice(gus, pvoice->number);
212         printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
213         snd_gf1_select_voice(gus, pcmp->pvoices[1]->number);
214         printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
215         snd_gf1_select_voice(gus, pvoice->number);
216 #endif
217         pcmp->bpos++;
218         pcmp->bpos %= pcmp->blocks;
219         if (pcmp->bpos + 1 >= pcmp->blocks) {   /* last block? */
220                 voice_ctrl |= 0x08;     /* enable loop */
221         } else {
222                 ramp_ctrl |= 0x04;      /* enable rollover */
223         }
224         end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels);
225         end -= voice_ctrl & 4 ? 2 : 1;
226         step = pcmp->dma_size / runtime->channels;
227         voice_ctrl |= 0x20;
228         if (!pcmp->final_volume) {
229                 ramp_ctrl |= 0x20;
230                 ramp_ctrl &= ~0x03;
231         }
232         for (idx = 0; idx < pcmp->voices; idx++, end += step) {
233                 snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
234                 snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
235                 snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
236                 snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
237                 voice_ctrl &= ~0x20;
238         }
239         if (!gus->gf1.enh_mode) {
240                 snd_gf1_delay(gus);
241                 voice_ctrl |= 0x20;
242                 for (idx = 0; idx < pcmp->voices; idx++) {
243                         snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
244                         snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
245                         snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
246                         voice_ctrl &= ~0x20;
247                 }
248         }
249         spin_unlock(&gus->reg_lock);
250
251         snd_pcm_period_elapsed(pcmp->substream);
252 #if 0
253         if ((runtime->flags & SNDRV_PCM_FLG_MMAP) &&
254             *runtime->state == SNDRV_PCM_STATE_RUNNING) {
255                 end = pcmp->bpos * pcmp->block_size;
256                 if (runtime->channels > 1) {
257                         snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + (end / 2), pcmp->block_size / 2);
258                         snd_gf1_pcm_block_change(pcmp->substream, end + (pcmp->block_size / 2), pcmp->memory + (pcmp->dma_size / 2) + (end / 2), pcmp->block_size / 2);
259                 } else {
260                         snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + end, pcmp->block_size);
261                 }
262         }
263 #endif
264 }
265
266 static void snd_gf1_pcm_interrupt_volume(snd_gus_card_t * gus, snd_gus_voice_t * pvoice)
267 {
268         unsigned short vol;
269         int cvoice;
270         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, pvoice->private_data, return);
271
272         /* stop ramp, but leave rollover bit untouched */
273         spin_lock(&gus->reg_lock);
274         snd_gf1_select_voice(gus, pvoice->number);
275         snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
276         spin_unlock(&gus->reg_lock);
277         if (pcmp == NULL)
278                 return;
279         /* are we active? */
280         if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
281                 return;
282         /* load real volume - better precision */
283         cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1;
284         if (pcmp->substream == NULL)
285                 return;
286         vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
287         spin_lock(&gus->reg_lock);
288         snd_gf1_select_voice(gus, pvoice->number);
289         snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
290         pcmp->final_volume = 1;
291         spin_unlock(&gus->reg_lock);
292 }
293
294 static void snd_gf1_pcm_volume_change(snd_gus_card_t * gus)
295 {
296 }
297
298 static int snd_gf1_pcm_poke_block(snd_gus_card_t *gus, unsigned char *buf,
299                                   unsigned int pos, unsigned int count,
300                                   int w16, int invert)
301 {
302         unsigned int len;
303         unsigned long flags;
304
305         // printk("poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n", (int)buf, pos, count, gus->gf1.port);
306         while (count > 0) {
307                 len = count;
308                 if (len > 512)          /* limit, to allow IRQ */
309                         len = 512;
310                 count -= len;
311                 if (gus->interwave) {
312                         spin_lock_irqsave(&gus->reg_lock, flags);
313                         snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00));
314                         snd_gf1_dram_addr(gus, pos);
315                         if (w16) {
316                                 outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL));
317                                 outsw(GUSP(gus, GF1DATALOW), buf, len >> 1);
318                         } else {
319                                 outsb(GUSP(gus, DRAM), buf, len);
320                         }
321                         spin_unlock_irqrestore(&gus->reg_lock, flags);
322                         buf += 512;
323                         pos += 512;
324                 } else {
325                         invert = invert ? 0x80 : 0x00;
326                         if (w16) {
327                                 len >>= 1;
328                                 while (len--) {
329                                         snd_gf1_poke(gus, pos++, *buf++);
330                                         snd_gf1_poke(gus, pos++, *buf++ ^ invert);
331                                 }
332                         } else {
333                                 while (len--)
334                                         snd_gf1_poke(gus, pos++, *buf++ ^ invert);
335                         }
336                 }
337                 if (count > 0 && !in_interrupt()) {
338                         schedule_timeout(1);
339                         if (signal_pending(current))
340                                 return -EAGAIN;
341                 }
342         }
343         return 0;
344 }
345
346 static int snd_gf1_pcm_playback_copy(snd_pcm_substream_t *substream,
347                                      int voice,
348                                      snd_pcm_uframes_t pos,
349                                      void __user *src,
350                                      snd_pcm_uframes_t count)
351 {
352         snd_pcm_runtime_t *runtime = substream->runtime;
353         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
354         unsigned int bpos, len;
355         
356         bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
357         len = samples_to_bytes(runtime, count);
358         snd_assert(bpos <= pcmp->dma_size, return -EIO);
359         snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
360         if (copy_from_user(runtime->dma_area + bpos, src, len))
361                 return -EFAULT;
362         if (snd_gf1_pcm_use_dma && len > 32) {
363                 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
364         } else {
365                 snd_gus_card_t *gus = pcmp->gus;
366                 int err, w16, invert;
367
368                 w16 = (snd_pcm_format_width(runtime->format) == 16);
369                 invert = snd_pcm_format_unsigned(runtime->format);
370                 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
371                         return err;
372         }
373         return 0;
374 }
375
376 static int snd_gf1_pcm_playback_silence(snd_pcm_substream_t *substream,
377                                         int voice,
378                                         snd_pcm_uframes_t pos,
379                                         snd_pcm_uframes_t count)
380 {
381         snd_pcm_runtime_t *runtime = substream->runtime;
382         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
383         unsigned int bpos, len;
384         
385         bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
386         len = samples_to_bytes(runtime, count);
387         snd_assert(bpos <= pcmp->dma_size, return -EIO);
388         snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
389         snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
390         if (snd_gf1_pcm_use_dma && len > 32) {
391                 return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
392         } else {
393                 snd_gus_card_t *gus = pcmp->gus;
394                 int err, w16, invert;
395
396                 w16 = (snd_pcm_format_width(runtime->format) == 16);
397                 invert = snd_pcm_format_unsigned(runtime->format);
398                 if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
399                         return err;
400         }
401         return 0;
402 }
403
404 static int snd_gf1_pcm_playback_hw_params(snd_pcm_substream_t * substream,
405                                           snd_pcm_hw_params_t * hw_params)
406 {
407         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
408         snd_pcm_runtime_t *runtime = substream->runtime;
409         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
410         int err;
411         
412         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
413                 return err;
414         if (err > 0) {  /* change */
415                 snd_gf1_mem_block_t *block;
416                 if (pcmp->memory > 0) {
417                         snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
418                         pcmp->memory = 0;
419                 }
420                 if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
421                                                SNDRV_GF1_MEM_OWNER_DRIVER,
422                                                "GF1 PCM",
423                                                runtime->dma_bytes, 1, 32,
424                                                NULL)) == NULL)
425                         return -ENOMEM;
426                 pcmp->memory = block->ptr;
427         }
428         pcmp->voices = params_channels(hw_params);
429         if (pcmp->pvoices[0] == NULL) {
430                 if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
431                         return -ENOMEM;
432                 pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
433                 pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
434                 pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
435                 pcmp->pvoices[0]->private_data = pcmp;
436         }
437         if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
438                 if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
439                         return -ENOMEM;
440                 pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
441                 pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
442                 pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
443                 pcmp->pvoices[1]->private_data = pcmp;
444         } else if (pcmp->voices == 1) {
445                 if (pcmp->pvoices[1]) {
446                         snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
447                         pcmp->pvoices[1] = NULL;
448                 }
449         }
450         return 0;
451 }
452
453 static int snd_gf1_pcm_playback_hw_free(snd_pcm_substream_t * substream)
454 {
455         snd_pcm_runtime_t *runtime = substream->runtime;
456         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
457
458         snd_pcm_lib_free_pages(substream);
459         if (pcmp->pvoices[0]) {
460                 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
461                 pcmp->pvoices[0] = NULL;
462         }
463         if (pcmp->pvoices[1]) {
464                 snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
465                 pcmp->pvoices[1] = NULL;
466         }
467         if (pcmp->memory > 0) {
468                 snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
469                 pcmp->memory = 0;
470         }
471         return 0;
472 }
473
474 static int snd_gf1_pcm_playback_prepare(snd_pcm_substream_t * substream)
475 {
476         snd_pcm_runtime_t *runtime = substream->runtime;
477         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
478
479         pcmp->bpos = 0;
480         pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
481         pcmp->block_size = snd_pcm_lib_period_bytes(substream);
482         pcmp->blocks = pcmp->dma_size / pcmp->block_size;
483         return 0;
484 }
485
486 static int snd_gf1_pcm_playback_trigger(snd_pcm_substream_t * substream,
487                                         int cmd)
488 {
489         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
490         snd_pcm_runtime_t *runtime = substream->runtime;
491         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
492         int voice;
493
494         if (cmd == SNDRV_PCM_TRIGGER_START) {
495                 snd_gf1_pcm_trigger_up(substream);
496         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
497                 spin_lock(&pcmp->lock);
498                 pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
499                 spin_unlock(&pcmp->lock);
500                 voice = pcmp->pvoices[0]->number;
501                 snd_gf1_stop_voices(gus, voice, voice);
502                 if (pcmp->pvoices[1]) {
503                         voice = pcmp->pvoices[1]->number;
504                         snd_gf1_stop_voices(gus, voice, voice);
505                 }
506         } else {
507                 return -EINVAL;
508         }
509         return 0;
510 }
511
512 static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(snd_pcm_substream_t * substream)
513 {
514         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
515         snd_pcm_runtime_t *runtime = substream->runtime;
516         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
517         unsigned int pos;
518         unsigned char voice_ctrl;
519
520         pos = 0;
521         spin_lock(&gus->reg_lock);
522         if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
523                 snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
524                 voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
525                 pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
526                 if (substream->runtime->channels > 1)
527                         pos <<= 1;
528                 pos = bytes_to_frames(runtime, pos);
529         }
530         spin_unlock(&gus->reg_lock);
531         return pos;
532 }
533
534 static ratnum_t clock = {
535         .num = 9878400/16,
536         .den_min = 2,
537         .den_max = 257,
538         .den_step = 1,
539 };
540
541 static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks  = {
542         .nrats = 1,
543         .rats = &clock,
544 };
545
546 static int snd_gf1_pcm_capture_hw_params(snd_pcm_substream_t * substream,
547                                          snd_pcm_hw_params_t * hw_params)
548 {
549         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
550
551         gus->c_dma_size = params_buffer_bytes(hw_params);
552         gus->c_period_size = params_period_bytes(hw_params);
553         gus->c_pos = 0;
554         gus->gf1.pcm_rcntrl_reg = 0x21;         /* IRQ at end, enable & start */
555         if (params_channels(hw_params) > 1)
556                 gus->gf1.pcm_rcntrl_reg |= 2;
557         if (gus->gf1.dma2 > 3)
558                 gus->gf1.pcm_rcntrl_reg |= 4;
559         if (snd_pcm_format_unsigned(params_format(hw_params)))
560                 gus->gf1.pcm_rcntrl_reg |= 0x80;
561         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
562 }
563
564 static int snd_gf1_pcm_capture_hw_free(snd_pcm_substream_t * substream)
565 {
566         return snd_pcm_lib_free_pages(substream);
567 }
568
569 static int snd_gf1_pcm_capture_prepare(snd_pcm_substream_t * substream)
570 {
571         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
572         snd_pcm_runtime_t *runtime = substream->runtime;
573
574         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
575         snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
576         snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);     /* Sampling Control Register */
577         snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
578         return 0;
579 }
580
581 static int snd_gf1_pcm_capture_trigger(snd_pcm_substream_t * substream,
582                                        int cmd)
583 {
584         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
585         int val;
586         
587         if (cmd == SNDRV_PCM_TRIGGER_START) {
588                 val = gus->gf1.pcm_rcntrl_reg;
589         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
590                 val = 0;
591         } else {
592                 return -EINVAL;
593         }
594
595         spin_lock(&gus->reg_lock);
596         snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
597         snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
598         spin_unlock(&gus->reg_lock);
599         return 0;
600 }
601
602 static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(snd_pcm_substream_t * substream)
603 {
604         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
605         int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
606         pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
607         return pos;
608 }
609
610 static void snd_gf1_pcm_interrupt_dma_read(snd_gus_card_t * gus)
611 {
612         snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
613         snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);     /* Sampling Control Register */
614         if (gus->pcm_cap_substream != NULL) {
615                 snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream); 
616                 snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
617                 gus->c_pos += gus->c_period_size;
618                 snd_pcm_period_elapsed(gus->pcm_cap_substream);
619         }
620 }
621
622 static snd_pcm_hardware_t snd_gf1_pcm_playback =
623 {
624         .info =                 SNDRV_PCM_INFO_NONINTERLEAVED,
625         .formats                = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
626                                  SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
627         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
628         .rate_min =             5510,
629         .rate_max =             48000,
630         .channels_min =         1,
631         .channels_max =         2,
632         .buffer_bytes_max =     (128*1024),
633         .period_bytes_min =     64,
634         .period_bytes_max =     (128*1024),
635         .periods_min =          1,
636         .periods_max =          1024,
637         .fifo_size =            0,
638 };
639
640 static snd_pcm_hardware_t snd_gf1_pcm_capture =
641 {
642         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
643                                  SNDRV_PCM_INFO_MMAP_VALID),
644         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
645         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
646         .rate_min =             5510,
647         .rate_max =             44100,
648         .channels_min =         1,
649         .channels_max =         2,
650         .buffer_bytes_max =     (128*1024),
651         .period_bytes_min =     64,
652         .period_bytes_max =     (128*1024),
653         .periods_min =          1,
654         .periods_max =          1024,
655         .fifo_size =            0,
656 };
657
658 static void snd_gf1_pcm_playback_free(snd_pcm_runtime_t *runtime)
659 {
660         gus_pcm_private_t * pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return);
661         snd_magic_kfree(pcmp);
662 }
663
664 static int snd_gf1_pcm_playback_open(snd_pcm_substream_t *substream)
665 {
666         gus_pcm_private_t *pcmp;
667         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
668         snd_pcm_runtime_t *runtime = substream->runtime;
669         int err;
670
671         pcmp = snd_magic_kcalloc(gus_pcm_private_t, 0, 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(snd_pcm_substream_t * substream)
697 {
698         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
699         snd_pcm_runtime_t *runtime = substream->runtime;
700         gus_pcm_private_t *pcmp = snd_magic_cast(gus_pcm_private_t, runtime->private_data, return -ENXIO);
701         unsigned long jiffies_old;
702
703         jiffies_old = jiffies;
704         while (atomic_read(&pcmp->dma_count) > 0) {
705                 interruptible_sleep_on_timeout(&pcmp->sleep, 1);
706                 if ((signed long)(jiffies - jiffies_old) > 2*HZ) {
707                         snd_printk("gf1 pcm - serious DMA problem\n");
708                         break;
709                 }
710         }
711         snd_gf1_dma_done(gus);  
712         return 0;
713 }
714
715 static int snd_gf1_pcm_capture_open(snd_pcm_substream_t * substream)
716 {
717         snd_pcm_runtime_t *runtime = substream->runtime;
718         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
719
720         gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
721         gus->pcm_cap_substream = substream;
722         substream->runtime->hw = snd_gf1_pcm_capture;
723         snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
724         snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
725         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
726                                       &hw_constraints_clocks);
727         return 0;
728 }
729
730 static int snd_gf1_pcm_capture_close(snd_pcm_substream_t * substream)
731 {
732         snd_gus_card_t *gus = snd_pcm_substream_chip(substream);
733
734         gus->pcm_cap_substream = NULL;
735         snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
736         return 0;
737 }
738
739 static void snd_gf1_pcm_free(snd_pcm_t *pcm)
740 {
741         snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, pcm->private_data, return);
742         gus->pcm = NULL;
743         snd_pcm_lib_preallocate_free_for_all(pcm);
744 }
745
746 static int snd_gf1_pcm_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
747 {
748         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
749         uinfo->count = 2;
750         uinfo->value.integer.min = 0;
751         uinfo->value.integer.max = 127;
752         return 0;
753 }
754
755 static int snd_gf1_pcm_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
756 {
757         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
758         unsigned long flags;
759         
760         spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
761         ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
762         ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
763         spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
764         return 0;
765 }
766
767 static int snd_gf1_pcm_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
768 {
769         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
770         unsigned long flags;
771         int change;
772         unsigned int idx;
773         unsigned short val1, val2, vol;
774         gus_pcm_private_t *pcmp;
775         snd_gus_voice_t *pvoice;
776         
777         val1 = ucontrol->value.integer.value[0] & 127;
778         val2 = ucontrol->value.integer.value[1] & 127;
779         spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
780         change = val1 != gus->gf1.pcm_volume_level_left1 ||
781                  val2 != gus->gf1.pcm_volume_level_right1;
782         gus->gf1.pcm_volume_level_left1 = val1;
783         gus->gf1.pcm_volume_level_right1 = val2;
784         gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
785         gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
786         spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
787         /* are we active? */
788         spin_lock_irqsave(&gus->voice_alloc, flags);
789         for (idx = 0; idx < 32; idx++) {
790                 pvoice = &gus->gf1.voices[idx];
791                 if (!pvoice->pcm)
792                         continue;
793                 pcmp = snd_magic_cast(gus_pcm_private_t, pvoice->private_data, return -ENXIO);
794                 if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
795                         continue;
796                 /* load real volume - better precision */
797                 spin_lock_irqsave(&gus->reg_lock, flags);
798                 snd_gf1_select_voice(gus, pvoice->number);
799                 snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
800                 vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
801                 snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
802                 pcmp->final_volume = 1;
803                 spin_unlock_irqrestore(&gus->reg_lock, flags);
804         }
805         spin_unlock_irqrestore(&gus->voice_alloc, flags);
806         return change;
807 }
808
809 static snd_kcontrol_new_t snd_gf1_pcm_volume_control =
810 {
811         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
812         .name = "PCM Playback Volume",
813         .info = snd_gf1_pcm_volume_info,
814         .get = snd_gf1_pcm_volume_get,
815         .put = snd_gf1_pcm_volume_put
816 };
817
818 static snd_kcontrol_new_t snd_gf1_pcm_volume_control1 =
819 {
820         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
821         .name = "GPCM Playback Volume",
822         .info = snd_gf1_pcm_volume_info,
823         .get = snd_gf1_pcm_volume_get,
824         .put = snd_gf1_pcm_volume_put
825 };
826
827 static snd_pcm_ops_t snd_gf1_pcm_playback_ops = {
828         .open =         snd_gf1_pcm_playback_open,
829         .close =        snd_gf1_pcm_playback_close,
830         .ioctl =        snd_pcm_lib_ioctl,
831         .hw_params =    snd_gf1_pcm_playback_hw_params,
832         .hw_free =      snd_gf1_pcm_playback_hw_free,
833         .prepare =      snd_gf1_pcm_playback_prepare,
834         .trigger =      snd_gf1_pcm_playback_trigger,
835         .pointer =      snd_gf1_pcm_playback_pointer,
836         .copy =         snd_gf1_pcm_playback_copy,
837         .silence =      snd_gf1_pcm_playback_silence,
838 };
839
840 static snd_pcm_ops_t snd_gf1_pcm_capture_ops = {
841         .open =         snd_gf1_pcm_capture_open,
842         .close =        snd_gf1_pcm_capture_close,
843         .ioctl =        snd_pcm_lib_ioctl,
844         .hw_params =    snd_gf1_pcm_capture_hw_params,
845         .hw_free =      snd_gf1_pcm_capture_hw_free,
846         .prepare =      snd_gf1_pcm_capture_prepare,
847         .trigger =      snd_gf1_pcm_capture_trigger,
848         .pointer =      snd_gf1_pcm_capture_pointer,
849 };
850
851 int snd_gf1_pcm_new(snd_gus_card_t * gus, int pcm_dev, int control_index, snd_pcm_t ** rpcm)
852 {
853         snd_card_t *card;
854         snd_kcontrol_t *kctl;
855         snd_pcm_t *pcm;
856         snd_pcm_substream_t *substream;
857         int capture, err;
858
859         if (rpcm)
860                 *rpcm = NULL;
861         card = gus->card;
862         capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
863         err = snd_pcm_new(card,
864                           gus->interwave ? "AMD InterWave" : "GF1",
865                           pcm_dev,
866                           gus->gf1.pcm_channels / 2,
867                           capture,
868                           &pcm);
869         if (err < 0)
870                 return err;
871         pcm->private_data = gus;
872         pcm->private_free = snd_gf1_pcm_free;
873         /* playback setup */
874         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
875
876         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
877                 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
878                                               snd_dma_isa_data(),
879                                               64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
880         
881         pcm->info_flags = 0;
882         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
883         if (capture) {
884                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
885                 if (gus->gf1.dma2 == gus->gf1.dma1)
886                         pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
887                 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
888                                               SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
889                                               64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
890         }
891         strcpy(pcm->name, pcm->id);
892         if (gus->interwave) {
893                 sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
894         }
895         strcat(pcm->name, " (synth)");
896         gus->pcm = pcm;
897
898         if (gus->codec_flag)
899                 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
900         else
901                 kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
902         if ((err = snd_ctl_add(card, kctl)) < 0)
903                 return err;
904         kctl->id.index = control_index;
905
906         if (rpcm)
907                 *rpcm = pcm;
908         return 0;
909 }
910