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