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