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