2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of GF1 chip (PCM things)
5 * InterWave chips supports interleaved DMA, but this feature isn't used in
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 <sound/driver.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"
39 #define SNDRV_GF1_PCM_RATE 48000
41 #define SNDRV_GF1_PCM_PFLG_NONE 0
42 #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0)
43 #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0)
45 struct gus_pcm_private
{
46 struct snd_gus_card
* gus
;
47 struct snd_pcm_substream
*substream
;
50 struct snd_gus_voice
*pvoices
[2];
53 unsigned char voice_ctrl
, ramp_ctrl
;
56 unsigned int block_size
;
57 unsigned int dma_size
;
58 wait_queue_head_t sleep
;
63 static int snd_gf1_pcm_use_dma
= 1;
65 static void snd_gf1_pcm_block_change_ack(struct snd_gus_card
* gus
, void *private_data
)
67 struct gus_pcm_private
*pcmp
= private_data
;
70 atomic_dec(&pcmp
->dma_count
);
71 wake_up(&pcmp
->sleep
);
75 static int snd_gf1_pcm_block_change(struct snd_pcm_substream
*substream
,
80 struct snd_gf1_dma_block block
;
81 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
82 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
86 // snd_printk("block change - offset = 0x%x, count = 0x%x\n", offset, count);
87 memset(&block
, 0, sizeof(block
));
88 block
.cmd
= SNDRV_GF1_DMA_IRQ
;
89 if (snd_pcm_format_unsigned(runtime
->format
))
90 block
.cmd
|= SNDRV_GF1_DMA_UNSIGNED
;
91 if (snd_pcm_format_width(runtime
->format
) == 16)
92 block
.cmd
|= SNDRV_GF1_DMA_16BIT
;
93 block
.addr
= addr
& ~31;
94 block
.buffer
= runtime
->dma_area
+ offset
;
95 block
.buf_addr
= runtime
->dma_addr
+ offset
;
97 block
.private_data
= pcmp
;
98 block
.ack
= snd_gf1_pcm_block_change_ack
;
99 if (!snd_gf1_dma_transfer_block(pcmp
->gus
, &block
, 0, 0))
100 atomic_inc(&pcmp
->dma_count
);
104 static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream
*substream
)
106 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
107 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
108 struct snd_gus_card
* gus
= pcmp
->gus
;
110 unsigned char voice_ctrl
, ramp_ctrl
;
112 unsigned int curr
, begin
, end
;
117 if (substream
== NULL
)
119 spin_lock_irqsave(&pcmp
->lock
, flags
);
120 if (pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
) {
121 spin_unlock_irqrestore(&pcmp
->lock
, flags
);
124 pcmp
->flags
|= SNDRV_GF1_PCM_PFLG_ACTIVE
;
125 pcmp
->final_volume
= 0;
126 spin_unlock_irqrestore(&pcmp
->lock
, flags
);
127 rate
= snd_gf1_translate_freq(gus
, runtime
->rate
<< 4);
128 /* enable WAVE IRQ */
129 voice_ctrl
= snd_pcm_format_width(runtime
->format
) == 16 ? 0x24 : 0x20;
130 /* enable RAMP IRQ + rollover */
132 if (pcmp
->blocks
== 1) {
133 voice_ctrl
|= 0x08; /* loop enable */
134 ramp_ctrl
&= ~0x04; /* disable rollover */
136 for (voice
= 0; voice
< pcmp
->voices
; voice
++) {
137 begin
= pcmp
->memory
+ voice
* (pcmp
->dma_size
/ runtime
->channels
);
138 curr
= begin
+ (pcmp
->bpos
* pcmp
->block_size
) / runtime
->channels
;
139 end
= curr
+ (pcmp
->block_size
/ runtime
->channels
);
140 end
-= snd_pcm_format_width(runtime
->format
) == 16 ? 2 : 1;
141 // 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);
142 pan
= runtime
->channels
== 2 ? (!voice
? 1 : 14) : 8;
143 vol
= !voice
? gus
->gf1
.pcm_volume_level_left
: gus
->gf1
.pcm_volume_level_right
;
144 spin_lock_irqsave(&gus
->reg_lock
, flags
);
145 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
146 snd_gf1_write8(gus
, SNDRV_GF1_VB_PAN
, pan
);
147 snd_gf1_write16(gus
, SNDRV_GF1_VW_FREQUENCY
, rate
);
148 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_START
, begin
<< 4, voice_ctrl
& 4);
149 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_END
, end
<< 4, voice_ctrl
& 4);
150 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_CURRENT
, curr
<< 4, voice_ctrl
& 4);
151 snd_gf1_write16(gus
, SNDRV_GF1_VW_VOLUME
, SNDRV_GF1_MIN_VOLUME
<< 4);
152 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_RATE
, 0x2f);
153 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_START
, SNDRV_GF1_MIN_OFFSET
);
154 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_END
, vol
>> 8);
155 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
156 if (!gus
->gf1
.enh_mode
) {
158 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
160 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
162 spin_lock_irqsave(&gus
->reg_lock
, flags
);
163 for (voice
= 0; voice
< pcmp
->voices
; voice
++) {
164 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
165 if (gus
->gf1
.enh_mode
)
166 snd_gf1_write8(gus
, SNDRV_GF1_VB_MODE
, 0x00); /* deactivate voice */
167 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
171 if (!gus
->gf1
.enh_mode
) {
173 for (voice
= 0; voice
< pcmp
->voices
; voice
++) {
174 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
175 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
176 voice_ctrl
&= ~0x20; /* disable IRQ for next voice */
179 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
182 static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card
* gus
,
183 struct snd_gus_voice
*pvoice
)
185 struct gus_pcm_private
* pcmp
;
186 struct snd_pcm_runtime
*runtime
;
187 unsigned char voice_ctrl
, ramp_ctrl
;
189 unsigned int end
, step
;
191 if (!pvoice
->private_data
) {
192 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
193 snd_gf1_smart_stop_voice(gus
, pvoice
->number
);
196 pcmp
= pvoice
->private_data
;
198 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
199 snd_gf1_smart_stop_voice(gus
, pvoice
->number
);
203 runtime
= pcmp
->substream
->runtime
;
205 spin_lock(&gus
->reg_lock
);
206 snd_gf1_select_voice(gus
, pvoice
->number
);
207 voice_ctrl
= snd_gf1_read8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
) & ~0x8b;
208 ramp_ctrl
= (snd_gf1_read8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
) & ~0xa4) | 0x03;
210 snd_gf1_select_voice(gus
, pvoice
->number
);
211 printk("position = 0x%x\n", (snd_gf1_read_addr(gus
, SNDRV_GF1_VA_CURRENT
, voice_ctrl
& 4) >> 4));
212 snd_gf1_select_voice(gus
, pcmp
->pvoices
[1]->number
);
213 printk("position = 0x%x\n", (snd_gf1_read_addr(gus
, SNDRV_GF1_VA_CURRENT
, voice_ctrl
& 4) >> 4));
214 snd_gf1_select_voice(gus
, pvoice
->number
);
217 pcmp
->bpos
%= pcmp
->blocks
;
218 if (pcmp
->bpos
+ 1 >= pcmp
->blocks
) { /* last block? */
219 voice_ctrl
|= 0x08; /* enable loop */
221 ramp_ctrl
|= 0x04; /* enable rollover */
223 end
= pcmp
->memory
+ (((pcmp
->bpos
+ 1) * pcmp
->block_size
) / runtime
->channels
);
224 end
-= voice_ctrl
& 4 ? 2 : 1;
225 step
= pcmp
->dma_size
/ runtime
->channels
;
227 if (!pcmp
->final_volume
) {
231 for (idx
= 0; idx
< pcmp
->voices
; idx
++, end
+= step
) {
232 snd_gf1_select_voice(gus
, pcmp
->pvoices
[idx
]->number
);
233 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_END
, end
<< 4, voice_ctrl
& 4);
234 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
235 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
238 if (!gus
->gf1
.enh_mode
) {
241 for (idx
= 0; idx
< pcmp
->voices
; idx
++) {
242 snd_gf1_select_voice(gus
, pcmp
->pvoices
[idx
]->number
);
243 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
244 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
248 spin_unlock(&gus
->reg_lock
);
250 snd_pcm_period_elapsed(pcmp
->substream
);
252 if ((runtime
->flags
& SNDRV_PCM_FLG_MMAP
) &&
253 *runtime
->state
== SNDRV_PCM_STATE_RUNNING
) {
254 end
= pcmp
->bpos
* pcmp
->block_size
;
255 if (runtime
->channels
> 1) {
256 snd_gf1_pcm_block_change(pcmp
->substream
, end
, pcmp
->memory
+ (end
/ 2), pcmp
->block_size
/ 2);
257 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 snd_gf1_pcm_block_change(pcmp
->substream
, end
, pcmp
->memory
+ end
, pcmp
->block_size
);
265 static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card
* gus
,
266 struct snd_gus_voice
* pvoice
)
270 struct gus_pcm_private
*pcmp
= pvoice
->private_data
;
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
);
280 if (!(pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
))
282 /* load real volume - better precision */
283 cvoice
= pcmp
->pvoices
[0] == pvoice
? 0 : 1;
284 if (pcmp
->substream
== NULL
)
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
);
294 static void snd_gf1_pcm_volume_change(struct snd_gus_card
* gus
)
298 static int snd_gf1_pcm_poke_block(struct snd_gus_card
*gus
, unsigned char *buf
,
299 unsigned int pos
, unsigned int count
,
305 // printk("poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n", (int)buf, pos, count, gus->gf1.port);
308 if (len
> 512) /* limit, to allow IRQ */
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
);
316 outb(SNDRV_GF1_GW_DRAM_IO16
, GUSP(gus
, GF1REGSEL
));
317 outsw(GUSP(gus
, GF1DATALOW
), buf
, len
>> 1);
319 outsb(GUSP(gus
, DRAM
), buf
, len
);
321 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
325 invert
= invert
? 0x80 : 0x00;
329 snd_gf1_poke(gus
, pos
++, *buf
++);
330 snd_gf1_poke(gus
, pos
++, *buf
++ ^ invert
);
334 snd_gf1_poke(gus
, pos
++, *buf
++ ^ invert
);
337 if (count
> 0 && !in_interrupt()) {
338 schedule_timeout_interruptible(1);
339 if (signal_pending(current
))
346 static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream
*substream
,
348 snd_pcm_uframes_t pos
,
350 snd_pcm_uframes_t count
)
352 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
353 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
354 unsigned int bpos
, len
;
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
))
362 if (snd_gf1_pcm_use_dma
&& len
> 32) {
363 return snd_gf1_pcm_block_change(substream
, bpos
, pcmp
->memory
+ bpos
, len
);
365 struct snd_gus_card
*gus
= pcmp
->gus
;
366 int err
, w16
, invert
;
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)
376 static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream
*substream
,
378 snd_pcm_uframes_t pos
,
379 snd_pcm_uframes_t count
)
381 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
382 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
383 unsigned int bpos
, len
;
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
);
393 struct snd_gus_card
*gus
= pcmp
->gus
;
394 int err
, w16
, invert
;
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)
404 static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream
*substream
,
405 struct snd_pcm_hw_params
*hw_params
)
407 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
408 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
409 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
412 if ((err
= snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
))) < 0)
414 if (err
> 0) { /* change */
415 struct snd_gf1_mem_block
*block
;
416 if (pcmp
->memory
> 0) {
417 snd_gf1_mem_free(&gus
->gf1
.mem_alloc
, pcmp
->memory
);
420 if ((block
= snd_gf1_mem_alloc(&gus
->gf1
.mem_alloc
,
421 SNDRV_GF1_MEM_OWNER_DRIVER
,
423 runtime
->dma_bytes
, 1, 32,
426 pcmp
->memory
= block
->ptr
;
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
)
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
;
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
)
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
;
453 static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream
*substream
)
455 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
456 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
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
;
463 if (pcmp
->pvoices
[1]) {
464 snd_gf1_free_voice(pcmp
->gus
, pcmp
->pvoices
[1]);
465 pcmp
->pvoices
[1] = NULL
;
467 if (pcmp
->memory
> 0) {
468 snd_gf1_mem_free(&pcmp
->gus
->gf1
.mem_alloc
, pcmp
->memory
);
474 static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream
*substream
)
476 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
477 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
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
;
486 static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream
*substream
,
489 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
490 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
491 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
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
);
512 static snd_pcm_uframes_t
snd_gf1_pcm_playback_pointer(struct snd_pcm_substream
*substream
)
514 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
515 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
516 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
518 unsigned char voice_ctrl
;
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)
528 pos
= bytes_to_frames(runtime
, pos
);
530 spin_unlock(&gus
->reg_lock
);
534 static struct snd_ratnum clock
= {
541 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks
= {
546 static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream
*substream
,
547 struct snd_pcm_hw_params
*hw_params
)
549 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
551 gus
->c_dma_size
= params_buffer_bytes(hw_params
);
552 gus
->c_period_size
= params_period_bytes(hw_params
);
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
));
564 static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream
*substream
)
566 return snd_pcm_lib_free_pages(substream
);
569 static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream
*substream
)
571 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
572 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
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
);
581 static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream
*substream
,
584 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
587 if (cmd
== SNDRV_PCM_TRIGGER_START
) {
588 val
= gus
->gf1
.pcm_rcntrl_reg
;
589 } else if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
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
);
602 static snd_pcm_uframes_t
snd_gf1_pcm_capture_pointer(struct snd_pcm_substream
*substream
)
604 struct snd_gus_card
*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
);
610 static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card
* gus
)
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
);
622 static struct snd_pcm_hardware snd_gf1_pcm_playback
=
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
,
632 .buffer_bytes_max
= (128*1024),
633 .period_bytes_min
= 64,
634 .period_bytes_max
= (128*1024),
640 static struct snd_pcm_hardware snd_gf1_pcm_capture
=
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
,
650 .buffer_bytes_max
= (128*1024),
651 .period_bytes_min
= 64,
652 .period_bytes_max
= (128*1024),
658 static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime
*runtime
)
660 kfree(runtime
->private_data
);
663 static int snd_gf1_pcm_playback_open(struct snd_pcm_substream
*substream
)
665 struct gus_pcm_private
*pcmp
;
666 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
667 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
670 pcmp
= kzalloc(sizeof(*pcmp
), GFP_KERNEL
);
674 spin_lock_init(&pcmp
->lock
);
675 init_waitqueue_head(&pcmp
->sleep
);
676 atomic_set(&pcmp
->dma_count
, 0);
678 runtime
->private_data
= pcmp
;
679 runtime
->private_free
= snd_gf1_pcm_playback_free
;
682 printk("playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n", (long) pcm
->playback
.buffer
, (long) gus
->gf1
.pcm_buffer
);
684 if ((err
= snd_gf1_dma_init(gus
)) < 0)
686 pcmp
->flags
= SNDRV_GF1_PCM_PFLG_NONE
;
687 pcmp
->substream
= substream
;
688 runtime
->hw
= snd_gf1_pcm_playback
;
689 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma1
, &runtime
->hw
.buffer_bytes_max
);
690 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma1
, &runtime
->hw
.period_bytes_max
);
691 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64);
695 static int snd_gf1_pcm_playback_close(struct snd_pcm_substream
*substream
)
697 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
698 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
699 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
701 if (!wait_event_timeout(pcmp
->sleep
, (atomic_read(&pcmp
->dma_count
) <= 0), 2*HZ
))
702 snd_printk(KERN_ERR
"gf1 pcm - serious DMA problem\n");
704 snd_gf1_dma_done(gus
);
708 static int snd_gf1_pcm_capture_open(struct snd_pcm_substream
*substream
)
710 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
711 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
713 gus
->gf1
.interrupt_handler_dma_read
= snd_gf1_pcm_interrupt_dma_read
;
714 gus
->pcm_cap_substream
= substream
;
715 substream
->runtime
->hw
= snd_gf1_pcm_capture
;
716 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma2
, &runtime
->hw
.buffer_bytes_max
);
717 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma2
, &runtime
->hw
.period_bytes_max
);
718 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
719 &hw_constraints_clocks
);
723 static int snd_gf1_pcm_capture_close(struct snd_pcm_substream
*substream
)
725 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
727 gus
->pcm_cap_substream
= NULL
;
728 snd_gf1_set_default_handlers(gus
, SNDRV_GF1_HANDLER_DMA_READ
);
732 static int snd_gf1_pcm_volume_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
734 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
736 uinfo
->value
.integer
.min
= 0;
737 uinfo
->value
.integer
.max
= 127;
741 static int snd_gf1_pcm_volume_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
743 struct snd_gus_card
*gus
= snd_kcontrol_chip(kcontrol
);
746 spin_lock_irqsave(&gus
->pcm_volume_level_lock
, flags
);
747 ucontrol
->value
.integer
.value
[0] = gus
->gf1
.pcm_volume_level_left1
;
748 ucontrol
->value
.integer
.value
[1] = gus
->gf1
.pcm_volume_level_right1
;
749 spin_unlock_irqrestore(&gus
->pcm_volume_level_lock
, flags
);
753 static int snd_gf1_pcm_volume_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
755 struct snd_gus_card
*gus
= snd_kcontrol_chip(kcontrol
);
759 unsigned short val1
, val2
, vol
;
760 struct gus_pcm_private
*pcmp
;
761 struct snd_gus_voice
*pvoice
;
763 val1
= ucontrol
->value
.integer
.value
[0] & 127;
764 val2
= ucontrol
->value
.integer
.value
[1] & 127;
765 spin_lock_irqsave(&gus
->pcm_volume_level_lock
, flags
);
766 change
= val1
!= gus
->gf1
.pcm_volume_level_left1
||
767 val2
!= gus
->gf1
.pcm_volume_level_right1
;
768 gus
->gf1
.pcm_volume_level_left1
= val1
;
769 gus
->gf1
.pcm_volume_level_right1
= val2
;
770 gus
->gf1
.pcm_volume_level_left
= snd_gf1_lvol_to_gvol_raw(val1
<< 9) << 4;
771 gus
->gf1
.pcm_volume_level_right
= snd_gf1_lvol_to_gvol_raw(val2
<< 9) << 4;
772 spin_unlock_irqrestore(&gus
->pcm_volume_level_lock
, flags
);
774 spin_lock_irqsave(&gus
->voice_alloc
, flags
);
775 for (idx
= 0; idx
< 32; idx
++) {
776 pvoice
= &gus
->gf1
.voices
[idx
];
779 pcmp
= pvoice
->private_data
;
780 if (!(pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
))
782 /* load real volume - better precision */
783 spin_lock_irqsave(&gus
->reg_lock
, flags
);
784 snd_gf1_select_voice(gus
, pvoice
->number
);
785 snd_gf1_ctrl_stop(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
);
786 vol
= pvoice
== pcmp
->pvoices
[0] ? gus
->gf1
.pcm_volume_level_left
: gus
->gf1
.pcm_volume_level_right
;
787 snd_gf1_write16(gus
, SNDRV_GF1_VW_VOLUME
, vol
);
788 pcmp
->final_volume
= 1;
789 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
791 spin_unlock_irqrestore(&gus
->voice_alloc
, flags
);
795 static struct snd_kcontrol_new snd_gf1_pcm_volume_control
=
797 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
798 .name
= "PCM Playback Volume",
799 .info
= snd_gf1_pcm_volume_info
,
800 .get
= snd_gf1_pcm_volume_get
,
801 .put
= snd_gf1_pcm_volume_put
804 static struct snd_kcontrol_new snd_gf1_pcm_volume_control1
=
806 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
807 .name
= "GPCM Playback Volume",
808 .info
= snd_gf1_pcm_volume_info
,
809 .get
= snd_gf1_pcm_volume_get
,
810 .put
= snd_gf1_pcm_volume_put
813 static struct snd_pcm_ops snd_gf1_pcm_playback_ops
= {
814 .open
= snd_gf1_pcm_playback_open
,
815 .close
= snd_gf1_pcm_playback_close
,
816 .ioctl
= snd_pcm_lib_ioctl
,
817 .hw_params
= snd_gf1_pcm_playback_hw_params
,
818 .hw_free
= snd_gf1_pcm_playback_hw_free
,
819 .prepare
= snd_gf1_pcm_playback_prepare
,
820 .trigger
= snd_gf1_pcm_playback_trigger
,
821 .pointer
= snd_gf1_pcm_playback_pointer
,
822 .copy
= snd_gf1_pcm_playback_copy
,
823 .silence
= snd_gf1_pcm_playback_silence
,
826 static struct snd_pcm_ops snd_gf1_pcm_capture_ops
= {
827 .open
= snd_gf1_pcm_capture_open
,
828 .close
= snd_gf1_pcm_capture_close
,
829 .ioctl
= snd_pcm_lib_ioctl
,
830 .hw_params
= snd_gf1_pcm_capture_hw_params
,
831 .hw_free
= snd_gf1_pcm_capture_hw_free
,
832 .prepare
= snd_gf1_pcm_capture_prepare
,
833 .trigger
= snd_gf1_pcm_capture_trigger
,
834 .pointer
= snd_gf1_pcm_capture_pointer
,
837 int snd_gf1_pcm_new(struct snd_gus_card
* gus
, int pcm_dev
, int control_index
, struct snd_pcm
** rpcm
)
839 struct snd_card
*card
;
840 struct snd_kcontrol
*kctl
;
842 struct snd_pcm_substream
*substream
;
848 capture
= !gus
->interwave
&& !gus
->ess_flag
&& !gus
->ace_flag
? 1 : 0;
849 err
= snd_pcm_new(card
,
850 gus
->interwave
? "AMD InterWave" : "GF1",
852 gus
->gf1
.pcm_channels
/ 2,
857 pcm
->private_data
= gus
;
859 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_gf1_pcm_playback_ops
);
861 for (substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
; substream
; substream
= substream
->next
)
862 snd_pcm_lib_preallocate_pages(substream
, SNDRV_DMA_TYPE_DEV
,
864 64*1024, gus
->gf1
.dma1
> 3 ? 128*1024 : 64*1024);
867 pcm
->dev_subclass
= SNDRV_PCM_SUBCLASS_GENERIC_MIX
;
869 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_gf1_pcm_capture_ops
);
870 if (gus
->gf1
.dma2
== gus
->gf1
.dma1
)
871 pcm
->info_flags
|= SNDRV_PCM_INFO_HALF_DUPLEX
;
872 snd_pcm_lib_preallocate_pages(pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
,
873 SNDRV_DMA_TYPE_DEV
, snd_dma_isa_data(),
874 64*1024, gus
->gf1
.dma2
> 3 ? 128*1024 : 64*1024);
876 strcpy(pcm
->name
, pcm
->id
);
877 if (gus
->interwave
) {
878 sprintf(pcm
->name
+ strlen(pcm
->name
), " rev %c", gus
->revision
+ 'A');
880 strcat(pcm
->name
, " (synth)");
884 kctl
= snd_ctl_new1(&snd_gf1_pcm_volume_control1
, gus
);
886 kctl
= snd_ctl_new1(&snd_gf1_pcm_volume_control
, gus
);
887 if ((err
= snd_ctl_add(card
, kctl
)) < 0)
889 kctl
->id
.index
= control_index
;