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