2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Routines for control of GF1 chip (PCM things)
5 * InterWave chips supports interleaved DMA, but this feature isn't used in
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
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"
38 #define SNDRV_GF1_PCM_RATE 48000
40 #define SNDRV_GF1_PCM_PFLG_NONE 0
41 #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0)
42 #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0)
44 struct gus_pcm_private
{
45 struct snd_gus_card
* gus
;
46 struct snd_pcm_substream
*substream
;
49 struct snd_gus_voice
*pvoices
[2];
52 unsigned char voice_ctrl
, ramp_ctrl
;
55 unsigned int block_size
;
56 unsigned int dma_size
;
57 wait_queue_head_t sleep
;
62 static int snd_gf1_pcm_use_dma
= 1;
64 static void snd_gf1_pcm_block_change_ack(struct snd_gus_card
* gus
, void *private_data
)
66 struct gus_pcm_private
*pcmp
= private_data
;
69 atomic_dec(&pcmp
->dma_count
);
70 wake_up(&pcmp
->sleep
);
74 static int snd_gf1_pcm_block_change(struct snd_pcm_substream
*substream
,
79 struct snd_gf1_dma_block block
;
80 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
81 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
86 snd_printk(KERN_DEBUG "block change - offset = 0x%x, count = 0x%x\n",
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
;
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
);
106 static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream
*substream
)
108 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
109 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
110 struct snd_gus_card
* gus
= pcmp
->gus
;
112 unsigned char voice_ctrl
, ramp_ctrl
;
114 unsigned int curr
, begin
, end
;
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;
142 snd_printk(KERN_DEBUG "init: curr=0x%x, begin=0x%x, end=0x%x, "
143 "ctrl=0x%x, ramp=0x%x, rate=0x%x\n",
144 curr, begin, end, voice_ctrl, ramp_ctrl, rate);
146 pan
= runtime
->channels
== 2 ? (!voice
? 1 : 14) : 8;
147 vol
= !voice
? gus
->gf1
.pcm_volume_level_left
: gus
->gf1
.pcm_volume_level_right
;
148 spin_lock_irqsave(&gus
->reg_lock
, flags
);
149 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
150 snd_gf1_write8(gus
, SNDRV_GF1_VB_PAN
, pan
);
151 snd_gf1_write16(gus
, SNDRV_GF1_VW_FREQUENCY
, rate
);
152 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_START
, begin
<< 4, voice_ctrl
& 4);
153 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_END
, end
<< 4, voice_ctrl
& 4);
154 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_CURRENT
, curr
<< 4, voice_ctrl
& 4);
155 snd_gf1_write16(gus
, SNDRV_GF1_VW_VOLUME
, SNDRV_GF1_MIN_VOLUME
<< 4);
156 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_RATE
, 0x2f);
157 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_START
, SNDRV_GF1_MIN_OFFSET
);
158 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_END
, vol
>> 8);
159 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
160 if (!gus
->gf1
.enh_mode
) {
162 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
164 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
166 spin_lock_irqsave(&gus
->reg_lock
, flags
);
167 for (voice
= 0; voice
< pcmp
->voices
; voice
++) {
168 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
169 if (gus
->gf1
.enh_mode
)
170 snd_gf1_write8(gus
, SNDRV_GF1_VB_MODE
, 0x00); /* deactivate voice */
171 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
175 if (!gus
->gf1
.enh_mode
) {
177 for (voice
= 0; voice
< pcmp
->voices
; voice
++) {
178 snd_gf1_select_voice(gus
, pcmp
->pvoices
[voice
]->number
);
179 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
180 voice_ctrl
&= ~0x20; /* disable IRQ for next voice */
183 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
186 static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card
* gus
,
187 struct snd_gus_voice
*pvoice
)
189 struct gus_pcm_private
* pcmp
;
190 struct snd_pcm_runtime
*runtime
;
191 unsigned char voice_ctrl
, ramp_ctrl
;
193 unsigned int end
, step
;
195 if (!pvoice
->private_data
) {
196 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
197 snd_gf1_smart_stop_voice(gus
, pvoice
->number
);
200 pcmp
= pvoice
->private_data
;
202 snd_printd("snd_gf1_pcm: unknown wave irq?\n");
203 snd_gf1_smart_stop_voice(gus
, pvoice
->number
);
207 runtime
= pcmp
->substream
->runtime
;
209 spin_lock(&gus
->reg_lock
);
210 snd_gf1_select_voice(gus
, pvoice
->number
);
211 voice_ctrl
= snd_gf1_read8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
) & ~0x8b;
212 ramp_ctrl
= (snd_gf1_read8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
) & ~0xa4) | 0x03;
214 snd_gf1_select_voice(gus
, pvoice
->number
);
215 printk(KERN_DEBUG
"position = 0x%x\n",
216 (snd_gf1_read_addr(gus
, SNDRV_GF1_VA_CURRENT
, voice_ctrl
& 4) >> 4));
217 snd_gf1_select_voice(gus
, pcmp
->pvoices
[1]->number
);
218 printk(KERN_DEBUG
"position = 0x%x\n",
219 (snd_gf1_read_addr(gus
, SNDRV_GF1_VA_CURRENT
, voice_ctrl
& 4) >> 4));
220 snd_gf1_select_voice(gus
, pvoice
->number
);
223 pcmp
->bpos
%= pcmp
->blocks
;
224 if (pcmp
->bpos
+ 1 >= pcmp
->blocks
) { /* last block? */
225 voice_ctrl
|= 0x08; /* enable loop */
227 ramp_ctrl
|= 0x04; /* enable rollover */
229 end
= pcmp
->memory
+ (((pcmp
->bpos
+ 1) * pcmp
->block_size
) / runtime
->channels
);
230 end
-= voice_ctrl
& 4 ? 2 : 1;
231 step
= pcmp
->dma_size
/ runtime
->channels
;
233 if (!pcmp
->final_volume
) {
237 for (idx
= 0; idx
< pcmp
->voices
; idx
++, end
+= step
) {
238 snd_gf1_select_voice(gus
, pcmp
->pvoices
[idx
]->number
);
239 snd_gf1_write_addr(gus
, SNDRV_GF1_VA_END
, end
<< 4, voice_ctrl
& 4);
240 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
241 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
244 if (!gus
->gf1
.enh_mode
) {
247 for (idx
= 0; idx
< pcmp
->voices
; idx
++) {
248 snd_gf1_select_voice(gus
, pcmp
->pvoices
[idx
]->number
);
249 snd_gf1_write8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
, voice_ctrl
);
250 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, ramp_ctrl
);
254 spin_unlock(&gus
->reg_lock
);
256 snd_pcm_period_elapsed(pcmp
->substream
);
258 if ((runtime
->flags
& SNDRV_PCM_FLG_MMAP
) &&
259 *runtime
->state
== SNDRV_PCM_STATE_RUNNING
) {
260 end
= pcmp
->bpos
* pcmp
->block_size
;
261 if (runtime
->channels
> 1) {
262 snd_gf1_pcm_block_change(pcmp
->substream
, end
, pcmp
->memory
+ (end
/ 2), pcmp
->block_size
/ 2);
263 snd_gf1_pcm_block_change(pcmp
->substream
, end
+ (pcmp
->block_size
/ 2), pcmp
->memory
+ (pcmp
->dma_size
/ 2) + (end
/ 2), pcmp
->block_size
/ 2);
265 snd_gf1_pcm_block_change(pcmp
->substream
, end
, pcmp
->memory
+ end
, pcmp
->block_size
);
271 static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card
* gus
,
272 struct snd_gus_voice
* pvoice
)
276 struct gus_pcm_private
*pcmp
= pvoice
->private_data
;
278 /* stop ramp, but leave rollover bit untouched */
279 spin_lock(&gus
->reg_lock
);
280 snd_gf1_select_voice(gus
, pvoice
->number
);
281 snd_gf1_ctrl_stop(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
);
282 spin_unlock(&gus
->reg_lock
);
286 if (!(pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
))
288 /* load real volume - better precision */
289 cvoice
= pcmp
->pvoices
[0] == pvoice
? 0 : 1;
290 if (pcmp
->substream
== NULL
)
292 vol
= !cvoice
? gus
->gf1
.pcm_volume_level_left
: gus
->gf1
.pcm_volume_level_right
;
293 spin_lock(&gus
->reg_lock
);
294 snd_gf1_select_voice(gus
, pvoice
->number
);
295 snd_gf1_write16(gus
, SNDRV_GF1_VW_VOLUME
, vol
);
296 pcmp
->final_volume
= 1;
297 spin_unlock(&gus
->reg_lock
);
300 static void snd_gf1_pcm_volume_change(struct snd_gus_card
* gus
)
304 static int snd_gf1_pcm_poke_block(struct snd_gus_card
*gus
, unsigned char *buf
,
305 unsigned int pos
, unsigned int count
,
313 "poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n",
314 (int)buf, pos, count, gus->gf1.port);
318 if (len
> 512) /* limit, to allow IRQ */
321 if (gus
->interwave
) {
322 spin_lock_irqsave(&gus
->reg_lock
, flags
);
323 snd_gf1_write8(gus
, SNDRV_GF1_GB_MEMORY_CONTROL
, 0x01 | (invert
? 0x08 : 0x00));
324 snd_gf1_dram_addr(gus
, pos
);
326 outb(SNDRV_GF1_GW_DRAM_IO16
, GUSP(gus
, GF1REGSEL
));
327 outsw(GUSP(gus
, GF1DATALOW
), buf
, len
>> 1);
329 outsb(GUSP(gus
, DRAM
), buf
, len
);
331 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
335 invert
= invert
? 0x80 : 0x00;
339 snd_gf1_poke(gus
, pos
++, *buf
++);
340 snd_gf1_poke(gus
, pos
++, *buf
++ ^ invert
);
344 snd_gf1_poke(gus
, pos
++, *buf
++ ^ invert
);
347 if (count
> 0 && !in_interrupt()) {
348 schedule_timeout_interruptible(1);
349 if (signal_pending(current
))
356 static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream
*substream
,
358 snd_pcm_uframes_t pos
,
360 snd_pcm_uframes_t count
)
362 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
363 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
364 unsigned int bpos
, len
;
366 bpos
= samples_to_bytes(runtime
, pos
) + (voice
* (pcmp
->dma_size
/ 2));
367 len
= samples_to_bytes(runtime
, count
);
368 if (snd_BUG_ON(bpos
> pcmp
->dma_size
))
370 if (snd_BUG_ON(bpos
+ len
> pcmp
->dma_size
))
372 if (copy_from_user(runtime
->dma_area
+ bpos
, src
, len
))
374 if (snd_gf1_pcm_use_dma
&& len
> 32) {
375 return snd_gf1_pcm_block_change(substream
, bpos
, pcmp
->memory
+ bpos
, len
);
377 struct snd_gus_card
*gus
= pcmp
->gus
;
378 int err
, w16
, invert
;
380 w16
= (snd_pcm_format_width(runtime
->format
) == 16);
381 invert
= snd_pcm_format_unsigned(runtime
->format
);
382 if ((err
= snd_gf1_pcm_poke_block(gus
, runtime
->dma_area
+ bpos
, pcmp
->memory
+ bpos
, len
, w16
, invert
)) < 0)
388 static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream
*substream
,
390 snd_pcm_uframes_t pos
,
391 snd_pcm_uframes_t count
)
393 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
394 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
395 unsigned int bpos
, len
;
397 bpos
= samples_to_bytes(runtime
, pos
) + (voice
* (pcmp
->dma_size
/ 2));
398 len
= samples_to_bytes(runtime
, count
);
399 if (snd_BUG_ON(bpos
> pcmp
->dma_size
))
401 if (snd_BUG_ON(bpos
+ len
> pcmp
->dma_size
))
403 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
+ bpos
, count
);
404 if (snd_gf1_pcm_use_dma
&& len
> 32) {
405 return snd_gf1_pcm_block_change(substream
, bpos
, pcmp
->memory
+ bpos
, len
);
407 struct snd_gus_card
*gus
= pcmp
->gus
;
408 int err
, w16
, invert
;
410 w16
= (snd_pcm_format_width(runtime
->format
) == 16);
411 invert
= snd_pcm_format_unsigned(runtime
->format
);
412 if ((err
= snd_gf1_pcm_poke_block(gus
, runtime
->dma_area
+ bpos
, pcmp
->memory
+ bpos
, len
, w16
, invert
)) < 0)
418 static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream
*substream
,
419 struct snd_pcm_hw_params
*hw_params
)
421 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
422 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
423 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
426 if ((err
= snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
))) < 0)
428 if (err
> 0) { /* change */
429 struct snd_gf1_mem_block
*block
;
430 if (pcmp
->memory
> 0) {
431 snd_gf1_mem_free(&gus
->gf1
.mem_alloc
, pcmp
->memory
);
434 if ((block
= snd_gf1_mem_alloc(&gus
->gf1
.mem_alloc
,
435 SNDRV_GF1_MEM_OWNER_DRIVER
,
437 runtime
->dma_bytes
, 1, 32,
440 pcmp
->memory
= block
->ptr
;
442 pcmp
->voices
= params_channels(hw_params
);
443 if (pcmp
->pvoices
[0] == NULL
) {
444 if ((pcmp
->pvoices
[0] = snd_gf1_alloc_voice(pcmp
->gus
, SNDRV_GF1_VOICE_TYPE_PCM
, 0, 0)) == NULL
)
446 pcmp
->pvoices
[0]->handler_wave
= snd_gf1_pcm_interrupt_wave
;
447 pcmp
->pvoices
[0]->handler_volume
= snd_gf1_pcm_interrupt_volume
;
448 pcmp
->pvoices
[0]->volume_change
= snd_gf1_pcm_volume_change
;
449 pcmp
->pvoices
[0]->private_data
= pcmp
;
451 if (pcmp
->voices
> 1 && pcmp
->pvoices
[1] == NULL
) {
452 if ((pcmp
->pvoices
[1] = snd_gf1_alloc_voice(pcmp
->gus
, SNDRV_GF1_VOICE_TYPE_PCM
, 0, 0)) == NULL
)
454 pcmp
->pvoices
[1]->handler_wave
= snd_gf1_pcm_interrupt_wave
;
455 pcmp
->pvoices
[1]->handler_volume
= snd_gf1_pcm_interrupt_volume
;
456 pcmp
->pvoices
[1]->volume_change
= snd_gf1_pcm_volume_change
;
457 pcmp
->pvoices
[1]->private_data
= pcmp
;
458 } else if (pcmp
->voices
== 1) {
459 if (pcmp
->pvoices
[1]) {
460 snd_gf1_free_voice(pcmp
->gus
, pcmp
->pvoices
[1]);
461 pcmp
->pvoices
[1] = NULL
;
467 static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream
*substream
)
469 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
470 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
472 snd_pcm_lib_free_pages(substream
);
473 if (pcmp
->pvoices
[0]) {
474 snd_gf1_free_voice(pcmp
->gus
, pcmp
->pvoices
[0]);
475 pcmp
->pvoices
[0] = NULL
;
477 if (pcmp
->pvoices
[1]) {
478 snd_gf1_free_voice(pcmp
->gus
, pcmp
->pvoices
[1]);
479 pcmp
->pvoices
[1] = NULL
;
481 if (pcmp
->memory
> 0) {
482 snd_gf1_mem_free(&pcmp
->gus
->gf1
.mem_alloc
, pcmp
->memory
);
488 static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream
*substream
)
490 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
491 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
494 pcmp
->dma_size
= snd_pcm_lib_buffer_bytes(substream
);
495 pcmp
->block_size
= snd_pcm_lib_period_bytes(substream
);
496 pcmp
->blocks
= pcmp
->dma_size
/ pcmp
->block_size
;
500 static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream
*substream
,
503 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
504 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
505 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
508 if (cmd
== SNDRV_PCM_TRIGGER_START
) {
509 snd_gf1_pcm_trigger_up(substream
);
510 } else if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
511 spin_lock(&pcmp
->lock
);
512 pcmp
->flags
&= ~SNDRV_GF1_PCM_PFLG_ACTIVE
;
513 spin_unlock(&pcmp
->lock
);
514 voice
= pcmp
->pvoices
[0]->number
;
515 snd_gf1_stop_voices(gus
, voice
, voice
);
516 if (pcmp
->pvoices
[1]) {
517 voice
= pcmp
->pvoices
[1]->number
;
518 snd_gf1_stop_voices(gus
, voice
, voice
);
526 static snd_pcm_uframes_t
snd_gf1_pcm_playback_pointer(struct snd_pcm_substream
*substream
)
528 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
529 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
530 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
532 unsigned char voice_ctrl
;
535 spin_lock(&gus
->reg_lock
);
536 if (pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
) {
537 snd_gf1_select_voice(gus
, pcmp
->pvoices
[0]->number
);
538 voice_ctrl
= snd_gf1_read8(gus
, SNDRV_GF1_VB_ADDRESS_CONTROL
);
539 pos
= (snd_gf1_read_addr(gus
, SNDRV_GF1_VA_CURRENT
, voice_ctrl
& 4) >> 4) - pcmp
->memory
;
540 if (substream
->runtime
->channels
> 1)
542 pos
= bytes_to_frames(runtime
, pos
);
544 spin_unlock(&gus
->reg_lock
);
548 static struct snd_ratnum clock
= {
555 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks
= {
560 static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream
*substream
,
561 struct snd_pcm_hw_params
*hw_params
)
563 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
565 gus
->c_dma_size
= params_buffer_bytes(hw_params
);
566 gus
->c_period_size
= params_period_bytes(hw_params
);
568 gus
->gf1
.pcm_rcntrl_reg
= 0x21; /* IRQ at end, enable & start */
569 if (params_channels(hw_params
) > 1)
570 gus
->gf1
.pcm_rcntrl_reg
|= 2;
571 if (gus
->gf1
.dma2
> 3)
572 gus
->gf1
.pcm_rcntrl_reg
|= 4;
573 if (snd_pcm_format_unsigned(params_format(hw_params
)))
574 gus
->gf1
.pcm_rcntrl_reg
|= 0x80;
575 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
578 static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream
*substream
)
580 return snd_pcm_lib_free_pages(substream
);
583 static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream
*substream
)
585 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
586 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
588 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_RECORD_RATE
, runtime
->rate_den
- 2);
589 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
, 0); /* disable sampling */
590 snd_gf1_i_look8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
); /* Sampling Control Register */
591 snd_dma_program(gus
->gf1
.dma2
, runtime
->dma_addr
, gus
->c_period_size
, DMA_MODE_READ
);
595 static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream
*substream
,
598 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
601 if (cmd
== SNDRV_PCM_TRIGGER_START
) {
602 val
= gus
->gf1
.pcm_rcntrl_reg
;
603 } else if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
609 spin_lock(&gus
->reg_lock
);
610 snd_gf1_write8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
, val
);
611 snd_gf1_look8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
);
612 spin_unlock(&gus
->reg_lock
);
616 static snd_pcm_uframes_t
snd_gf1_pcm_capture_pointer(struct snd_pcm_substream
*substream
)
618 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
619 int pos
= snd_dma_pointer(gus
->gf1
.dma2
, gus
->c_period_size
);
620 pos
= bytes_to_frames(substream
->runtime
, (gus
->c_pos
+ pos
) % gus
->c_dma_size
);
624 static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card
* gus
)
626 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
, 0); /* disable sampling */
627 snd_gf1_i_look8(gus
, SNDRV_GF1_GB_REC_DMA_CONTROL
); /* Sampling Control Register */
628 if (gus
->pcm_cap_substream
!= NULL
) {
629 snd_gf1_pcm_capture_prepare(gus
->pcm_cap_substream
);
630 snd_gf1_pcm_capture_trigger(gus
->pcm_cap_substream
, SNDRV_PCM_TRIGGER_START
);
631 gus
->c_pos
+= gus
->c_period_size
;
632 snd_pcm_period_elapsed(gus
->pcm_cap_substream
);
636 static struct snd_pcm_hardware snd_gf1_pcm_playback
=
638 .info
= SNDRV_PCM_INFO_NONINTERLEAVED
,
639 .formats
= (SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U8
|
640 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U16_LE
),
641 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
646 .buffer_bytes_max
= (128*1024),
647 .period_bytes_min
= 64,
648 .period_bytes_max
= (128*1024),
654 static struct snd_pcm_hardware snd_gf1_pcm_capture
=
656 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
657 SNDRV_PCM_INFO_MMAP_VALID
),
658 .formats
= SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U8
,
659 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_44100
,
664 .buffer_bytes_max
= (128*1024),
665 .period_bytes_min
= 64,
666 .period_bytes_max
= (128*1024),
672 static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime
*runtime
)
674 kfree(runtime
->private_data
);
677 static int snd_gf1_pcm_playback_open(struct snd_pcm_substream
*substream
)
679 struct gus_pcm_private
*pcmp
;
680 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
681 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
684 pcmp
= kzalloc(sizeof(*pcmp
), GFP_KERNEL
);
688 spin_lock_init(&pcmp
->lock
);
689 init_waitqueue_head(&pcmp
->sleep
);
690 atomic_set(&pcmp
->dma_count
, 0);
692 runtime
->private_data
= pcmp
;
693 runtime
->private_free
= snd_gf1_pcm_playback_free
;
696 printk(KERN_DEBUG
"playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n",
697 (long) pcm
->playback
.buffer
, (long) gus
->gf1
.pcm_buffer
);
699 if ((err
= snd_gf1_dma_init(gus
)) < 0)
701 pcmp
->flags
= SNDRV_GF1_PCM_PFLG_NONE
;
702 pcmp
->substream
= substream
;
703 runtime
->hw
= snd_gf1_pcm_playback
;
704 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma1
, &runtime
->hw
.buffer_bytes_max
);
705 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma1
, &runtime
->hw
.period_bytes_max
);
706 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64);
710 static int snd_gf1_pcm_playback_close(struct snd_pcm_substream
*substream
)
712 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
713 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
714 struct gus_pcm_private
*pcmp
= runtime
->private_data
;
716 if (!wait_event_timeout(pcmp
->sleep
, (atomic_read(&pcmp
->dma_count
) <= 0), 2*HZ
))
717 snd_printk(KERN_ERR
"gf1 pcm - serious DMA problem\n");
719 snd_gf1_dma_done(gus
);
723 static int snd_gf1_pcm_capture_open(struct snd_pcm_substream
*substream
)
725 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
726 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
728 gus
->gf1
.interrupt_handler_dma_read
= snd_gf1_pcm_interrupt_dma_read
;
729 gus
->pcm_cap_substream
= substream
;
730 substream
->runtime
->hw
= snd_gf1_pcm_capture
;
731 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma2
, &runtime
->hw
.buffer_bytes_max
);
732 snd_pcm_limit_isa_dma_size(gus
->gf1
.dma2
, &runtime
->hw
.period_bytes_max
);
733 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
734 &hw_constraints_clocks
);
738 static int snd_gf1_pcm_capture_close(struct snd_pcm_substream
*substream
)
740 struct snd_gus_card
*gus
= snd_pcm_substream_chip(substream
);
742 gus
->pcm_cap_substream
= NULL
;
743 snd_gf1_set_default_handlers(gus
, SNDRV_GF1_HANDLER_DMA_READ
);
747 static int snd_gf1_pcm_volume_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
749 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
751 uinfo
->value
.integer
.min
= 0;
752 uinfo
->value
.integer
.max
= 127;
756 static int snd_gf1_pcm_volume_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
758 struct snd_gus_card
*gus
= snd_kcontrol_chip(kcontrol
);
761 spin_lock_irqsave(&gus
->pcm_volume_level_lock
, flags
);
762 ucontrol
->value
.integer
.value
[0] = gus
->gf1
.pcm_volume_level_left1
;
763 ucontrol
->value
.integer
.value
[1] = gus
->gf1
.pcm_volume_level_right1
;
764 spin_unlock_irqrestore(&gus
->pcm_volume_level_lock
, flags
);
768 static int snd_gf1_pcm_volume_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
770 struct snd_gus_card
*gus
= snd_kcontrol_chip(kcontrol
);
774 unsigned short val1
, val2
, vol
;
775 struct gus_pcm_private
*pcmp
;
776 struct snd_gus_voice
*pvoice
;
778 val1
= ucontrol
->value
.integer
.value
[0] & 127;
779 val2
= ucontrol
->value
.integer
.value
[1] & 127;
780 spin_lock_irqsave(&gus
->pcm_volume_level_lock
, flags
);
781 change
= val1
!= gus
->gf1
.pcm_volume_level_left1
||
782 val2
!= gus
->gf1
.pcm_volume_level_right1
;
783 gus
->gf1
.pcm_volume_level_left1
= val1
;
784 gus
->gf1
.pcm_volume_level_right1
= val2
;
785 gus
->gf1
.pcm_volume_level_left
= snd_gf1_lvol_to_gvol_raw(val1
<< 9) << 4;
786 gus
->gf1
.pcm_volume_level_right
= snd_gf1_lvol_to_gvol_raw(val2
<< 9) << 4;
787 spin_unlock_irqrestore(&gus
->pcm_volume_level_lock
, flags
);
789 spin_lock_irqsave(&gus
->voice_alloc
, flags
);
790 for (idx
= 0; idx
< 32; idx
++) {
791 pvoice
= &gus
->gf1
.voices
[idx
];
794 pcmp
= pvoice
->private_data
;
795 if (!(pcmp
->flags
& SNDRV_GF1_PCM_PFLG_ACTIVE
))
797 /* load real volume - better precision */
798 spin_lock(&gus
->reg_lock
);
799 snd_gf1_select_voice(gus
, pvoice
->number
);
800 snd_gf1_ctrl_stop(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
);
801 vol
= pvoice
== pcmp
->pvoices
[0] ? gus
->gf1
.pcm_volume_level_left
: gus
->gf1
.pcm_volume_level_right
;
802 snd_gf1_write16(gus
, SNDRV_GF1_VW_VOLUME
, vol
);
803 pcmp
->final_volume
= 1;
804 spin_unlock(&gus
->reg_lock
);
806 spin_unlock_irqrestore(&gus
->voice_alloc
, flags
);
810 static struct snd_kcontrol_new snd_gf1_pcm_volume_control
=
812 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
813 .name
= "PCM Playback Volume",
814 .info
= snd_gf1_pcm_volume_info
,
815 .get
= snd_gf1_pcm_volume_get
,
816 .put
= snd_gf1_pcm_volume_put
819 static struct snd_kcontrol_new snd_gf1_pcm_volume_control1
=
821 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
822 .name
= "GPCM Playback Volume",
823 .info
= snd_gf1_pcm_volume_info
,
824 .get
= snd_gf1_pcm_volume_get
,
825 .put
= snd_gf1_pcm_volume_put
828 static struct snd_pcm_ops snd_gf1_pcm_playback_ops
= {
829 .open
= snd_gf1_pcm_playback_open
,
830 .close
= snd_gf1_pcm_playback_close
,
831 .ioctl
= snd_pcm_lib_ioctl
,
832 .hw_params
= snd_gf1_pcm_playback_hw_params
,
833 .hw_free
= snd_gf1_pcm_playback_hw_free
,
834 .prepare
= snd_gf1_pcm_playback_prepare
,
835 .trigger
= snd_gf1_pcm_playback_trigger
,
836 .pointer
= snd_gf1_pcm_playback_pointer
,
837 .copy
= snd_gf1_pcm_playback_copy
,
838 .silence
= snd_gf1_pcm_playback_silence
,
841 static struct snd_pcm_ops snd_gf1_pcm_capture_ops
= {
842 .open
= snd_gf1_pcm_capture_open
,
843 .close
= snd_gf1_pcm_capture_close
,
844 .ioctl
= snd_pcm_lib_ioctl
,
845 .hw_params
= snd_gf1_pcm_capture_hw_params
,
846 .hw_free
= snd_gf1_pcm_capture_hw_free
,
847 .prepare
= snd_gf1_pcm_capture_prepare
,
848 .trigger
= snd_gf1_pcm_capture_trigger
,
849 .pointer
= snd_gf1_pcm_capture_pointer
,
852 int snd_gf1_pcm_new(struct snd_gus_card
*gus
, int pcm_dev
, int control_index
)
854 struct snd_card
*card
;
855 struct snd_kcontrol
*kctl
;
857 struct snd_pcm_substream
*substream
;
861 capture
= !gus
->interwave
&& !gus
->ess_flag
&& !gus
->ace_flag
? 1 : 0;
862 err
= snd_pcm_new(card
,
863 gus
->interwave
? "AMD InterWave" : "GF1",
865 gus
->gf1
.pcm_channels
/ 2,
870 pcm
->private_data
= gus
;
872 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_gf1_pcm_playback_ops
);
874 for (substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
; substream
; substream
= substream
->next
)
875 snd_pcm_lib_preallocate_pages(substream
, SNDRV_DMA_TYPE_DEV
,
877 64*1024, gus
->gf1
.dma1
> 3 ? 128*1024 : 64*1024);
880 pcm
->dev_subclass
= SNDRV_PCM_SUBCLASS_GENERIC_MIX
;
882 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_gf1_pcm_capture_ops
);
883 if (gus
->gf1
.dma2
== gus
->gf1
.dma1
)
884 pcm
->info_flags
|= SNDRV_PCM_INFO_HALF_DUPLEX
;
885 snd_pcm_lib_preallocate_pages(pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
,
886 SNDRV_DMA_TYPE_DEV
, snd_dma_isa_data(),
887 64*1024, gus
->gf1
.dma2
> 3 ? 128*1024 : 64*1024);
889 strcpy(pcm
->name
, pcm
->id
);
890 if (gus
->interwave
) {
891 sprintf(pcm
->name
+ strlen(pcm
->name
), " rev %c", gus
->revision
+ 'A');
893 strcat(pcm
->name
, " (synth)");
897 kctl
= snd_ctl_new1(&snd_gf1_pcm_volume_control1
, gus
);
899 kctl
= snd_ctl_new1(&snd_gf1_pcm_volume_control
, gus
);
900 if ((err
= snd_ctl_add(card
, kctl
)) < 0)
902 kctl
->id
.index
= control_index
;