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