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