• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/sound/isa/gus/
1/*
2 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 *  Routines for control of GF1 chip (PCM things)
4 *
5 *  InterWave chips supports interleaved DMA, but this feature isn't used in
6 *  this code.
7 *
8 *  This code emulates autoinit DMA transfer for playback, recording by GF1
9 *  chip doesn't support autoinit DMA.
10 *
11 *
12 *   This program is free software; you can redistribute it and/or modify
13 *   it under the terms of the GNU General Public License as published by
14 *   the Free Software Foundation; either version 2 of the License, or
15 *   (at your option) any later version.
16 *
17 *   This program is distributed in the hope that it will be useful,
18 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 *   GNU General Public License for more details.
21 *
22 *   You should have received a copy of the GNU General Public License
23 *   along with this program; if not, write to the Free Software
24 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25 *
26 */
27
28#include <asm/dma.h>
29#include <linux/slab.h>
30#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/gus.h>
33#include <sound/pcm_params.h>
34#include "gus_tables.h"
35
36/* maximum rate */
37
38#define SNDRV_GF1_PCM_RATE		48000
39
40#define SNDRV_GF1_PCM_PFLG_NONE		0
41#define SNDRV_GF1_PCM_PFLG_ACTIVE	(1<<0)
42#define SNDRV_GF1_PCM_PFLG_NEUTRAL	(2<<0)
43
44struct gus_pcm_private {
45	struct snd_gus_card * gus;
46	struct snd_pcm_substream *substream;
47	spinlock_t lock;
48	unsigned int voices;
49	struct snd_gus_voice *pvoices[2];
50	unsigned int memory;
51	unsigned short flags;
52	unsigned char voice_ctrl, ramp_ctrl;
53	unsigned int bpos;
54	unsigned int blocks;
55	unsigned int block_size;
56	unsigned int dma_size;
57	wait_queue_head_t sleep;
58	atomic_t dma_count;
59	int final_volume;
60};
61
62static int snd_gf1_pcm_use_dma = 1;
63
64static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data)
65{
66	struct gus_pcm_private *pcmp = private_data;
67
68	if (pcmp) {
69		atomic_dec(&pcmp->dma_count);
70		wake_up(&pcmp->sleep);
71	}
72}
73
74static int snd_gf1_pcm_block_change(struct snd_pcm_substream *substream,
75				    unsigned int offset,
76				    unsigned int addr,
77				    unsigned int count)
78{
79	struct snd_gf1_dma_block block;
80	struct snd_pcm_runtime *runtime = substream->runtime;
81	struct gus_pcm_private *pcmp = runtime->private_data;
82
83	count += offset & 31;
84	offset &= ~31;
85	/*
86	snd_printk(KERN_DEBUG "block change - offset = 0x%x, count = 0x%x\n",
87		   offset, count);
88	*/
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
106static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream)
107{
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;
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	spin_lock_irqsave(&pcmp->lock, flags);
120	if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
121		spin_unlock_irqrestore(&pcmp->lock, flags);
122		return;
123	}
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 */
131	ramp_ctrl = 0x24;
132	if (pcmp->blocks == 1) {
133		voice_ctrl |= 0x08;	/* loop enable */
134		ramp_ctrl &= ~0x04;	/* disable rollover */
135	}
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		/*
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);
145		*/
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) {
161			snd_gf1_delay(gus);
162			snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
163		}
164		spin_unlock_irqrestore(&gus->reg_lock, flags);
165	}
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);
172		voice_ctrl &= ~0x20;
173	}
174	voice_ctrl |= 0x20;
175	if (!gus->gf1.enh_mode) {
176		snd_gf1_delay(gus);
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 */
181		}
182	}
183	spin_unlock_irqrestore(&gus->reg_lock, flags);
184}
185
186static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus,
187				       struct snd_gus_voice *pvoice)
188{
189	struct gus_pcm_private * pcmp;
190	struct snd_pcm_runtime *runtime;
191	unsigned char voice_ctrl, ramp_ctrl;
192	unsigned int idx;
193	unsigned int end, step;
194
195	if (!pvoice->private_data) {
196		snd_printd("snd_gf1_pcm: unknown wave irq?\n");
197		snd_gf1_smart_stop_voice(gus, pvoice->number);
198		return;
199	}
200	pcmp = pvoice->private_data;
201	if (pcmp == NULL) {
202		snd_printd("snd_gf1_pcm: unknown wave irq?\n");
203		snd_gf1_smart_stop_voice(gus, pvoice->number);
204		return;
205	}
206	gus = pcmp->gus;
207	runtime = pcmp->substream->runtime;
208
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;
213	pcmp->bpos++;
214	pcmp->bpos %= pcmp->blocks;
215	if (pcmp->bpos + 1 >= pcmp->blocks) {	/* last block? */
216		voice_ctrl |= 0x08;	/* enable loop */
217	} else {
218		ramp_ctrl |= 0x04;	/* enable rollover */
219	}
220	end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels);
221	end -= voice_ctrl & 4 ? 2 : 1;
222	step = pcmp->dma_size / runtime->channels;
223	voice_ctrl |= 0x20;
224	if (!pcmp->final_volume) {
225		ramp_ctrl |= 0x20;
226		ramp_ctrl &= ~0x03;
227	}
228	for (idx = 0; idx < pcmp->voices; idx++, end += step) {
229		snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
230		snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
231		snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
232		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
233		voice_ctrl &= ~0x20;
234	}
235	if (!gus->gf1.enh_mode) {
236		snd_gf1_delay(gus);
237		voice_ctrl |= 0x20;
238		for (idx = 0; idx < pcmp->voices; idx++) {
239			snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
240			snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
241			snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
242			voice_ctrl &= ~0x20;
243		}
244	}
245	spin_unlock(&gus->reg_lock);
246
247	snd_pcm_period_elapsed(pcmp->substream);
248}
249
250static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus,
251					 struct snd_gus_voice * pvoice)
252{
253	unsigned short vol;
254	int cvoice;
255	struct gus_pcm_private *pcmp = pvoice->private_data;
256
257	/* stop ramp, but leave rollover bit untouched */
258	spin_lock(&gus->reg_lock);
259	snd_gf1_select_voice(gus, pvoice->number);
260	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
261	spin_unlock(&gus->reg_lock);
262	if (pcmp == NULL)
263		return;
264	/* are we active? */
265	if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
266		return;
267	/* load real volume - better precision */
268	cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1;
269	if (pcmp->substream == NULL)
270		return;
271	vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
272	spin_lock(&gus->reg_lock);
273	snd_gf1_select_voice(gus, pvoice->number);
274	snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
275	pcmp->final_volume = 1;
276	spin_unlock(&gus->reg_lock);
277}
278
279static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus)
280{
281}
282
283static int snd_gf1_pcm_poke_block(struct snd_gus_card *gus, unsigned char *buf,
284				  unsigned int pos, unsigned int count,
285				  int w16, int invert)
286{
287	unsigned int len;
288	unsigned long flags;
289
290	/*
291	printk(KERN_DEBUG
292	       "poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n",
293	       (int)buf, pos, count, gus->gf1.port);
294	*/
295	while (count > 0) {
296		len = count;
297		if (len > 512)		/* limit, to allow IRQ */
298			len = 512;
299		count -= len;
300		if (gus->interwave) {
301			spin_lock_irqsave(&gus->reg_lock, flags);
302			snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00));
303			snd_gf1_dram_addr(gus, pos);
304			if (w16) {
305				outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL));
306				outsw(GUSP(gus, GF1DATALOW), buf, len >> 1);
307			} else {
308				outsb(GUSP(gus, DRAM), buf, len);
309			}
310			spin_unlock_irqrestore(&gus->reg_lock, flags);
311			buf += 512;
312			pos += 512;
313		} else {
314			invert = invert ? 0x80 : 0x00;
315			if (w16) {
316				len >>= 1;
317				while (len--) {
318					snd_gf1_poke(gus, pos++, *buf++);
319					snd_gf1_poke(gus, pos++, *buf++ ^ invert);
320				}
321			} else {
322				while (len--)
323					snd_gf1_poke(gus, pos++, *buf++ ^ invert);
324			}
325		}
326		if (count > 0 && !in_interrupt()) {
327			schedule_timeout_interruptible(1);
328			if (signal_pending(current))
329				return -EAGAIN;
330		}
331	}
332	return 0;
333}
334
335static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream,
336				     int voice,
337				     snd_pcm_uframes_t pos,
338				     void __user *src,
339				     snd_pcm_uframes_t count)
340{
341	struct snd_pcm_runtime *runtime = substream->runtime;
342	struct gus_pcm_private *pcmp = runtime->private_data;
343	unsigned int bpos, len;
344
345	bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
346	len = samples_to_bytes(runtime, count);
347	if (snd_BUG_ON(bpos > pcmp->dma_size))
348		return -EIO;
349	if (snd_BUG_ON(bpos + len > pcmp->dma_size))
350		return -EIO;
351	if (copy_from_user(runtime->dma_area + bpos, src, len))
352		return -EFAULT;
353	if (snd_gf1_pcm_use_dma && len > 32) {
354		return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
355	} else {
356		struct snd_gus_card *gus = pcmp->gus;
357		int err, w16, invert;
358
359		w16 = (snd_pcm_format_width(runtime->format) == 16);
360		invert = snd_pcm_format_unsigned(runtime->format);
361		if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
362			return err;
363	}
364	return 0;
365}
366
367static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
368					int voice,
369					snd_pcm_uframes_t pos,
370					snd_pcm_uframes_t count)
371{
372	struct snd_pcm_runtime *runtime = substream->runtime;
373	struct gus_pcm_private *pcmp = runtime->private_data;
374	unsigned int bpos, len;
375
376	bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
377	len = samples_to_bytes(runtime, count);
378	if (snd_BUG_ON(bpos > pcmp->dma_size))
379		return -EIO;
380	if (snd_BUG_ON(bpos + len > pcmp->dma_size))
381		return -EIO;
382	snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
383	if (snd_gf1_pcm_use_dma && len > 32) {
384		return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
385	} else {
386		struct snd_gus_card *gus = pcmp->gus;
387		int err, w16, invert;
388
389		w16 = (snd_pcm_format_width(runtime->format) == 16);
390		invert = snd_pcm_format_unsigned(runtime->format);
391		if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
392			return err;
393	}
394	return 0;
395}
396
397static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
398					  struct snd_pcm_hw_params *hw_params)
399{
400	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
401	struct snd_pcm_runtime *runtime = substream->runtime;
402	struct gus_pcm_private *pcmp = runtime->private_data;
403	int err;
404
405	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
406		return err;
407	if (err > 0) {	/* change */
408		struct snd_gf1_mem_block *block;
409		if (pcmp->memory > 0) {
410			snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
411			pcmp->memory = 0;
412		}
413		if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
414		                               SNDRV_GF1_MEM_OWNER_DRIVER,
415					       "GF1 PCM",
416		                               runtime->dma_bytes, 1, 32,
417		                               NULL)) == NULL)
418			return -ENOMEM;
419		pcmp->memory = block->ptr;
420	}
421	pcmp->voices = params_channels(hw_params);
422	if (pcmp->pvoices[0] == NULL) {
423		if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
424			return -ENOMEM;
425		pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
426		pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
427		pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
428		pcmp->pvoices[0]->private_data = pcmp;
429	}
430	if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
431		if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
432			return -ENOMEM;
433		pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
434		pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
435		pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
436		pcmp->pvoices[1]->private_data = pcmp;
437	} else if (pcmp->voices == 1) {
438		if (pcmp->pvoices[1]) {
439			snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
440			pcmp->pvoices[1] = NULL;
441		}
442	}
443	return 0;
444}
445
446static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
447{
448	struct snd_pcm_runtime *runtime = substream->runtime;
449	struct gus_pcm_private *pcmp = runtime->private_data;
450
451	snd_pcm_lib_free_pages(substream);
452	if (pcmp->pvoices[0]) {
453		snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
454		pcmp->pvoices[0] = NULL;
455	}
456	if (pcmp->pvoices[1]) {
457		snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
458		pcmp->pvoices[1] = NULL;
459	}
460	if (pcmp->memory > 0) {
461		snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
462		pcmp->memory = 0;
463	}
464	return 0;
465}
466
467static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
468{
469	struct snd_pcm_runtime *runtime = substream->runtime;
470	struct gus_pcm_private *pcmp = runtime->private_data;
471
472	pcmp->bpos = 0;
473	pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
474	pcmp->block_size = snd_pcm_lib_period_bytes(substream);
475	pcmp->blocks = pcmp->dma_size / pcmp->block_size;
476	return 0;
477}
478
479static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
480					int cmd)
481{
482	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
483	struct snd_pcm_runtime *runtime = substream->runtime;
484	struct gus_pcm_private *pcmp = runtime->private_data;
485	int voice;
486
487	if (cmd == SNDRV_PCM_TRIGGER_START) {
488		snd_gf1_pcm_trigger_up(substream);
489	} else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
490		spin_lock(&pcmp->lock);
491		pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
492		spin_unlock(&pcmp->lock);
493		voice = pcmp->pvoices[0]->number;
494		snd_gf1_stop_voices(gus, voice, voice);
495		if (pcmp->pvoices[1]) {
496			voice = pcmp->pvoices[1]->number;
497			snd_gf1_stop_voices(gus, voice, voice);
498		}
499	} else {
500		return -EINVAL;
501	}
502	return 0;
503}
504
505static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
506{
507	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
508	struct snd_pcm_runtime *runtime = substream->runtime;
509	struct gus_pcm_private *pcmp = runtime->private_data;
510	unsigned int pos;
511	unsigned char voice_ctrl;
512
513	pos = 0;
514	spin_lock(&gus->reg_lock);
515	if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
516		snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
517		voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
518		pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
519		if (substream->runtime->channels > 1)
520			pos <<= 1;
521		pos = bytes_to_frames(runtime, pos);
522	}
523	spin_unlock(&gus->reg_lock);
524	return pos;
525}
526
527static struct snd_ratnum clock = {
528	.num = 9878400/16,
529	.den_min = 2,
530	.den_max = 257,
531	.den_step = 1,
532};
533
534static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks  = {
535	.nrats = 1,
536	.rats = &clock,
537};
538
539static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
540					 struct snd_pcm_hw_params *hw_params)
541{
542	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
543
544	gus->c_dma_size = params_buffer_bytes(hw_params);
545	gus->c_period_size = params_period_bytes(hw_params);
546	gus->c_pos = 0;
547	gus->gf1.pcm_rcntrl_reg = 0x21;		/* IRQ at end, enable & start */
548	if (params_channels(hw_params) > 1)
549		gus->gf1.pcm_rcntrl_reg |= 2;
550	if (gus->gf1.dma2 > 3)
551		gus->gf1.pcm_rcntrl_reg |= 4;
552	if (snd_pcm_format_unsigned(params_format(hw_params)))
553		gus->gf1.pcm_rcntrl_reg |= 0x80;
554	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
555}
556
557static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream *substream)
558{
559	return snd_pcm_lib_free_pages(substream);
560}
561
562static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
563{
564	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
565	struct snd_pcm_runtime *runtime = substream->runtime;
566
567	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
568	snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0);	/* disable sampling */
569	snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);	/* Sampling Control Register */
570	snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
571	return 0;
572}
573
574static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
575				       int cmd)
576{
577	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
578	int val;
579
580	if (cmd == SNDRV_PCM_TRIGGER_START) {
581		val = gus->gf1.pcm_rcntrl_reg;
582	} else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
583		val = 0;
584	} else {
585		return -EINVAL;
586	}
587
588	spin_lock(&gus->reg_lock);
589	snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
590	snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
591	spin_unlock(&gus->reg_lock);
592	return 0;
593}
594
595static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
596{
597	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
598	int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
599	pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
600	return pos;
601}
602
603static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
604{
605	snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0);	/* disable sampling */
606	snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);	/* Sampling Control Register */
607	if (gus->pcm_cap_substream != NULL) {
608		snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream);
609		snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
610		gus->c_pos += gus->c_period_size;
611		snd_pcm_period_elapsed(gus->pcm_cap_substream);
612	}
613}
614
615static struct snd_pcm_hardware snd_gf1_pcm_playback =
616{
617	.info =			SNDRV_PCM_INFO_NONINTERLEAVED,
618	.formats		= (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
619				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
620	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
621	.rate_min =		5510,
622	.rate_max =		48000,
623	.channels_min =		1,
624	.channels_max =		2,
625	.buffer_bytes_max =	(128*1024),
626	.period_bytes_min =	64,
627	.period_bytes_max =	(128*1024),
628	.periods_min =		1,
629	.periods_max =		1024,
630	.fifo_size =		0,
631};
632
633static struct snd_pcm_hardware snd_gf1_pcm_capture =
634{
635	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
636				 SNDRV_PCM_INFO_MMAP_VALID),
637	.formats =		SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
638	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
639	.rate_min =		5510,
640	.rate_max =		44100,
641	.channels_min =		1,
642	.channels_max =		2,
643	.buffer_bytes_max =	(128*1024),
644	.period_bytes_min =	64,
645	.period_bytes_max =	(128*1024),
646	.periods_min =		1,
647	.periods_max =		1024,
648	.fifo_size =		0,
649};
650
651static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
652{
653	kfree(runtime->private_data);
654}
655
656static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
657{
658	struct gus_pcm_private *pcmp;
659	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
660	struct snd_pcm_runtime *runtime = substream->runtime;
661	int err;
662
663	pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL);
664	if (pcmp == NULL)
665		return -ENOMEM;
666	pcmp->gus = gus;
667	spin_lock_init(&pcmp->lock);
668	init_waitqueue_head(&pcmp->sleep);
669	atomic_set(&pcmp->dma_count, 0);
670
671	runtime->private_data = pcmp;
672	runtime->private_free = snd_gf1_pcm_playback_free;
673
674	if ((err = snd_gf1_dma_init(gus)) < 0)
675		return err;
676	pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
677	pcmp->substream = substream;
678	runtime->hw = snd_gf1_pcm_playback;
679	snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
680	snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
681	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
682	return 0;
683}
684
685static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
686{
687	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
688	struct snd_pcm_runtime *runtime = substream->runtime;
689	struct gus_pcm_private *pcmp = runtime->private_data;
690
691	if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
692		snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n");
693
694	snd_gf1_dma_done(gus);
695	return 0;
696}
697
698static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
699{
700	struct snd_pcm_runtime *runtime = substream->runtime;
701	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
702
703	gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
704	gus->pcm_cap_substream = substream;
705	substream->runtime->hw = snd_gf1_pcm_capture;
706	snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
707	snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
708	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
709				      &hw_constraints_clocks);
710	return 0;
711}
712
713static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
714{
715	struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
716
717	gus->pcm_cap_substream = NULL;
718	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
719	return 0;
720}
721
722static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
723{
724	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
725	uinfo->count = 2;
726	uinfo->value.integer.min = 0;
727	uinfo->value.integer.max = 127;
728	return 0;
729}
730
731static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
732{
733	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
734	unsigned long flags;
735
736	spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
737	ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
738	ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
739	spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
740	return 0;
741}
742
743static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
744{
745	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
746	unsigned long flags;
747	int change;
748	unsigned int idx;
749	unsigned short val1, val2, vol;
750	struct gus_pcm_private *pcmp;
751	struct snd_gus_voice *pvoice;
752
753	val1 = ucontrol->value.integer.value[0] & 127;
754	val2 = ucontrol->value.integer.value[1] & 127;
755	spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
756	change = val1 != gus->gf1.pcm_volume_level_left1 ||
757	         val2 != gus->gf1.pcm_volume_level_right1;
758	gus->gf1.pcm_volume_level_left1 = val1;
759	gus->gf1.pcm_volume_level_right1 = val2;
760	gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
761	gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
762	spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
763	/* are we active? */
764	spin_lock_irqsave(&gus->voice_alloc, flags);
765	for (idx = 0; idx < 32; idx++) {
766		pvoice = &gus->gf1.voices[idx];
767		if (!pvoice->pcm)
768			continue;
769		pcmp = pvoice->private_data;
770		if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
771			continue;
772		/* load real volume - better precision */
773		spin_lock(&gus->reg_lock);
774		snd_gf1_select_voice(gus, pvoice->number);
775		snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
776		vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
777		snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
778		pcmp->final_volume = 1;
779		spin_unlock(&gus->reg_lock);
780	}
781	spin_unlock_irqrestore(&gus->voice_alloc, flags);
782	return change;
783}
784
785static struct snd_kcontrol_new snd_gf1_pcm_volume_control =
786{
787	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
788	.name = "PCM Playback Volume",
789	.info = snd_gf1_pcm_volume_info,
790	.get = snd_gf1_pcm_volume_get,
791	.put = snd_gf1_pcm_volume_put
792};
793
794static struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
795{
796	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
797	.name = "GPCM Playback Volume",
798	.info = snd_gf1_pcm_volume_info,
799	.get = snd_gf1_pcm_volume_get,
800	.put = snd_gf1_pcm_volume_put
801};
802
803static struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
804	.open =		snd_gf1_pcm_playback_open,
805	.close =	snd_gf1_pcm_playback_close,
806	.ioctl =	snd_pcm_lib_ioctl,
807	.hw_params =	snd_gf1_pcm_playback_hw_params,
808	.hw_free =	snd_gf1_pcm_playback_hw_free,
809	.prepare =	snd_gf1_pcm_playback_prepare,
810	.trigger =	snd_gf1_pcm_playback_trigger,
811	.pointer =	snd_gf1_pcm_playback_pointer,
812	.copy =		snd_gf1_pcm_playback_copy,
813	.silence =	snd_gf1_pcm_playback_silence,
814};
815
816static struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
817	.open =		snd_gf1_pcm_capture_open,
818	.close =	snd_gf1_pcm_capture_close,
819	.ioctl =	snd_pcm_lib_ioctl,
820	.hw_params =	snd_gf1_pcm_capture_hw_params,
821	.hw_free =	snd_gf1_pcm_capture_hw_free,
822	.prepare =	snd_gf1_pcm_capture_prepare,
823	.trigger =	snd_gf1_pcm_capture_trigger,
824	.pointer =	snd_gf1_pcm_capture_pointer,
825};
826
827int snd_gf1_pcm_new(struct snd_gus_card * gus, int pcm_dev, int control_index, struct snd_pcm ** rpcm)
828{
829	struct snd_card *card;
830	struct snd_kcontrol *kctl;
831	struct snd_pcm *pcm;
832	struct snd_pcm_substream *substream;
833	int capture, err;
834
835	if (rpcm)
836		*rpcm = NULL;
837	card = gus->card;
838	capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
839	err = snd_pcm_new(card,
840			  gus->interwave ? "AMD InterWave" : "GF1",
841			  pcm_dev,
842			  gus->gf1.pcm_channels / 2,
843			  capture,
844			  &pcm);
845	if (err < 0)
846		return err;
847	pcm->private_data = gus;
848	/* playback setup */
849	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
850
851	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
852		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
853					      snd_dma_isa_data(),
854					      64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
855
856	pcm->info_flags = 0;
857	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
858	if (capture) {
859		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
860		if (gus->gf1.dma2 == gus->gf1.dma1)
861			pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
862		snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
863					      SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
864					      64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
865	}
866	strcpy(pcm->name, pcm->id);
867	if (gus->interwave) {
868		sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
869	}
870	strcat(pcm->name, " (synth)");
871	gus->pcm = pcm;
872
873	if (gus->codec_flag)
874		kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
875	else
876		kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
877	if ((err = snd_ctl_add(card, kctl)) < 0)
878		return err;
879	kctl->id.index = control_index;
880
881	if (rpcm)
882		*rpcm = pcm;
883	return 0;
884}
885