1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ALSA driver for Echoaudio soundcards. 4 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it> 5 */ 6 7 #include <linux/module.h> 8 9 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>"); 10 MODULE_LICENSE("GPL v2"); 11 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver"); 12 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}"); 13 MODULE_DEVICE_TABLE(pci, snd_echo_ids); 14 15 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 16 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 17 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 18 19 module_param_array(index, int, NULL, 0444); 20 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard."); 21 module_param_array(id, charp, NULL, 0444); 22 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard."); 23 module_param_array(enable, bool, NULL, 0444); 24 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard."); 25 26 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999}; 27 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1); 28 29 30 31 static int get_firmware(const struct firmware **fw_entry, 32 struct echoaudio *chip, const short fw_index) 33 { 34 int err; 35 char name[30]; 36 37 #ifdef CONFIG_PM_SLEEP 38 if (chip->fw_cache[fw_index]) { 39 dev_dbg(chip->card->dev, 40 "firmware requested: %s is cached\n", 41 card_fw[fw_index].data); 42 *fw_entry = chip->fw_cache[fw_index]; 43 return 0; 44 } 45 #endif 46 47 dev_dbg(chip->card->dev, 48 "firmware requested: %s\n", card_fw[fw_index].data); 49 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data); 50 err = request_firmware(fw_entry, name, &chip->pci->dev); 51 if (err < 0) 52 dev_err(chip->card->dev, 53 "get_firmware(): Firmware not available (%d)\n", err); 54 #ifdef CONFIG_PM_SLEEP 55 else 56 chip->fw_cache[fw_index] = *fw_entry; 57 #endif 58 return err; 59 } 60 61 62 63 static void free_firmware(const struct firmware *fw_entry, 64 struct echoaudio *chip) 65 { 66 #ifdef CONFIG_PM_SLEEP 67 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n"); 68 #else 69 release_firmware(fw_entry); 70 #endif 71 } 72 73 74 75 static void free_firmware_cache(struct echoaudio *chip) 76 { 77 #ifdef CONFIG_PM_SLEEP 78 int i; 79 80 for (i = 0; i < 8 ; i++) 81 if (chip->fw_cache[i]) { 82 release_firmware(chip->fw_cache[i]); 83 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i); 84 } 85 86 #endif 87 } 88 89 90 91 /****************************************************************************** 92 PCM interface 93 ******************************************************************************/ 94 95 static void audiopipe_free(struct snd_pcm_runtime *runtime) 96 { 97 struct audiopipe *pipe = runtime->private_data; 98 99 if (pipe->sgpage.area) 100 snd_dma_free_pages(&pipe->sgpage); 101 kfree(pipe); 102 } 103 104 105 106 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params, 107 struct snd_pcm_hw_rule *rule) 108 { 109 struct snd_interval *c = hw_param_interval(params, 110 SNDRV_PCM_HW_PARAM_CHANNELS); 111 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 112 struct snd_mask fmt; 113 114 snd_mask_any(&fmt); 115 116 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32 117 /* >=2 channels cannot be S32_BE */ 118 if (c->min == 2) { 119 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE; 120 return snd_mask_refine(f, &fmt); 121 } 122 #endif 123 /* > 2 channels cannot be U8 and S32_BE */ 124 if (c->min > 2) { 125 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE); 126 return snd_mask_refine(f, &fmt); 127 } 128 /* Mono is ok with any format */ 129 return 0; 130 } 131 132 133 134 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params, 135 struct snd_pcm_hw_rule *rule) 136 { 137 struct snd_interval *c = hw_param_interval(params, 138 SNDRV_PCM_HW_PARAM_CHANNELS); 139 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 140 struct snd_interval ch; 141 142 snd_interval_any(&ch); 143 144 /* S32_BE is mono (and stereo) only */ 145 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) { 146 ch.min = 1; 147 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32 148 ch.max = 2; 149 #else 150 ch.max = 1; 151 #endif 152 ch.integer = 1; 153 return snd_interval_refine(c, &ch); 154 } 155 /* U8 can be only mono or stereo */ 156 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) { 157 ch.min = 1; 158 ch.max = 2; 159 ch.integer = 1; 160 return snd_interval_refine(c, &ch); 161 } 162 /* S16_LE, S24_3LE and S32_LE support any number of channels. */ 163 return 0; 164 } 165 166 167 168 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params, 169 struct snd_pcm_hw_rule *rule) 170 { 171 struct snd_interval *c = hw_param_interval(params, 172 SNDRV_PCM_HW_PARAM_CHANNELS); 173 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 174 struct snd_mask fmt; 175 u64 fmask; 176 snd_mask_any(&fmt); 177 178 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32); 179 180 /* >2 channels must be S16_LE, S24_3LE or S32_LE */ 181 if (c->min > 2) { 182 fmask &= SNDRV_PCM_FMTBIT_S16_LE | 183 SNDRV_PCM_FMTBIT_S24_3LE | 184 SNDRV_PCM_FMTBIT_S32_LE; 185 /* 1 channel must be S32_BE or S32_LE */ 186 } else if (c->max == 1) 187 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE; 188 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32 189 /* 2 channels cannot be S32_BE */ 190 else if (c->min == 2 && c->max == 2) 191 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE; 192 #endif 193 else 194 return 0; 195 196 fmt.bits[0] &= (u32)fmask; 197 fmt.bits[1] &= (u32)(fmask >> 32); 198 return snd_mask_refine(f, &fmt); 199 } 200 201 202 203 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params, 204 struct snd_pcm_hw_rule *rule) 205 { 206 struct snd_interval *c = hw_param_interval(params, 207 SNDRV_PCM_HW_PARAM_CHANNELS); 208 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 209 struct snd_interval ch; 210 u64 fmask; 211 212 snd_interval_any(&ch); 213 ch.integer = 1; 214 fmask = f->bits[0] + ((u64)f->bits[1] << 32); 215 216 /* S32_BE is mono (and stereo) only */ 217 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) { 218 ch.min = 1; 219 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32 220 ch.max = 2; 221 #else 222 ch.max = 1; 223 #endif 224 /* U8 is stereo only */ 225 } else if (fmask == SNDRV_PCM_FMTBIT_U8) 226 ch.min = ch.max = 2; 227 /* S16_LE and S24_3LE must be at least stereo */ 228 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE | 229 SNDRV_PCM_FMTBIT_S24_3LE))) 230 ch.min = 2; 231 else 232 return 0; 233 234 return snd_interval_refine(c, &ch); 235 } 236 237 238 239 /* Since the sample rate is a global setting, do allow the user to change the 240 sample rate only if there is only one pcm device open. */ 241 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params, 242 struct snd_pcm_hw_rule *rule) 243 { 244 struct snd_interval *rate = hw_param_interval(params, 245 SNDRV_PCM_HW_PARAM_RATE); 246 struct echoaudio *chip = rule->private; 247 struct snd_interval fixed; 248 249 if (!chip->can_set_rate) { 250 snd_interval_any(&fixed); 251 fixed.min = fixed.max = chip->sample_rate; 252 return snd_interval_refine(rate, &fixed); 253 } 254 return 0; 255 } 256 257 258 static int pcm_open(struct snd_pcm_substream *substream, 259 signed char max_channels) 260 { 261 struct echoaudio *chip; 262 struct snd_pcm_runtime *runtime; 263 struct audiopipe *pipe; 264 int err, i; 265 266 if (max_channels <= 0) 267 return -EAGAIN; 268 269 chip = snd_pcm_substream_chip(substream); 270 runtime = substream->runtime; 271 272 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL); 273 if (!pipe) 274 return -ENOMEM; 275 pipe->index = -1; /* Not configured yet */ 276 277 /* Set up hw capabilities and contraints */ 278 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware)); 279 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels); 280 pipe->constr.list = channels_list; 281 pipe->constr.mask = 0; 282 for (i = 0; channels_list[i] <= max_channels; i++); 283 pipe->constr.count = i; 284 if (pipe->hw.channels_max > max_channels) 285 pipe->hw.channels_max = max_channels; 286 if (chip->digital_mode == DIGITAL_MODE_ADAT) { 287 pipe->hw.rate_max = 48000; 288 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000; 289 } 290 291 runtime->hw = pipe->hw; 292 runtime->private_data = pipe; 293 runtime->private_free = audiopipe_free; 294 snd_pcm_set_sync(substream); 295 296 /* Only mono and any even number of channels are allowed */ 297 if ((err = snd_pcm_hw_constraint_list(runtime, 0, 298 SNDRV_PCM_HW_PARAM_CHANNELS, 299 &pipe->constr)) < 0) 300 return err; 301 302 /* All periods should have the same size */ 303 if ((err = snd_pcm_hw_constraint_integer(runtime, 304 SNDRV_PCM_HW_PARAM_PERIODS)) < 0) 305 return err; 306 307 /* The hw accesses memory in chunks 32 frames long and they should be 308 32-bytes-aligned. It's not a requirement, but it seems that IRQs are 309 generated with a resolution of 32 frames. Thus we need the following */ 310 if ((err = snd_pcm_hw_constraint_step(runtime, 0, 311 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 312 32)) < 0) 313 return err; 314 if ((err = snd_pcm_hw_constraint_step(runtime, 0, 315 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 316 32)) < 0) 317 return err; 318 319 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 320 SNDRV_PCM_HW_PARAM_RATE, 321 hw_rule_sample_rate, chip, 322 SNDRV_PCM_HW_PARAM_RATE, -1)) < 0) 323 return err; 324 325 /* Finally allocate a page for the scatter-gather list */ 326 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, 327 &chip->pci->dev, 328 PAGE_SIZE, &pipe->sgpage)) < 0) { 329 dev_err(chip->card->dev, "s-g list allocation failed\n"); 330 return err; 331 } 332 333 return 0; 334 } 335 336 337 338 static int pcm_analog_in_open(struct snd_pcm_substream *substream) 339 { 340 struct echoaudio *chip = snd_pcm_substream_chip(substream); 341 int err; 342 343 if ((err = pcm_open(substream, num_analog_busses_in(chip) - 344 substream->number)) < 0) 345 return err; 346 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 347 SNDRV_PCM_HW_PARAM_CHANNELS, 348 hw_rule_capture_channels_by_format, NULL, 349 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0) 350 return err; 351 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 352 SNDRV_PCM_HW_PARAM_FORMAT, 353 hw_rule_capture_format_by_channels, NULL, 354 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0) 355 return err; 356 atomic_inc(&chip->opencount); 357 if (atomic_read(&chip->opencount) > 1 && chip->rate_set) 358 chip->can_set_rate=0; 359 dev_dbg(chip->card->dev, "pcm_analog_in_open cs=%d oc=%d r=%d\n", 360 chip->can_set_rate, atomic_read(&chip->opencount), 361 chip->sample_rate); 362 return 0; 363 } 364 365 366 367 static int pcm_analog_out_open(struct snd_pcm_substream *substream) 368 { 369 struct echoaudio *chip = snd_pcm_substream_chip(substream); 370 int max_channels, err; 371 372 #ifdef ECHOCARD_HAS_VMIXER 373 max_channels = num_pipes_out(chip); 374 #else 375 max_channels = num_analog_busses_out(chip); 376 #endif 377 if ((err = pcm_open(substream, max_channels - substream->number)) < 0) 378 return err; 379 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 380 SNDRV_PCM_HW_PARAM_CHANNELS, 381 hw_rule_playback_channels_by_format, 382 NULL, 383 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0) 384 return err; 385 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 386 SNDRV_PCM_HW_PARAM_FORMAT, 387 hw_rule_playback_format_by_channels, 388 NULL, 389 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0) 390 return err; 391 atomic_inc(&chip->opencount); 392 if (atomic_read(&chip->opencount) > 1 && chip->rate_set) 393 chip->can_set_rate=0; 394 dev_dbg(chip->card->dev, "pcm_analog_out_open cs=%d oc=%d r=%d\n", 395 chip->can_set_rate, atomic_read(&chip->opencount), 396 chip->sample_rate); 397 return 0; 398 } 399 400 401 402 #ifdef ECHOCARD_HAS_DIGITAL_IO 403 404 static int pcm_digital_in_open(struct snd_pcm_substream *substream) 405 { 406 struct echoaudio *chip = snd_pcm_substream_chip(substream); 407 int err, max_channels; 408 409 max_channels = num_digital_busses_in(chip) - substream->number; 410 mutex_lock(&chip->mode_mutex); 411 if (chip->digital_mode == DIGITAL_MODE_ADAT) 412 err = pcm_open(substream, max_channels); 413 else /* If the card has ADAT, subtract the 6 channels 414 * that S/PDIF doesn't have 415 */ 416 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT); 417 418 if (err < 0) 419 goto din_exit; 420 421 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 422 SNDRV_PCM_HW_PARAM_CHANNELS, 423 hw_rule_capture_channels_by_format, NULL, 424 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0) 425 goto din_exit; 426 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 427 SNDRV_PCM_HW_PARAM_FORMAT, 428 hw_rule_capture_format_by_channels, NULL, 429 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0) 430 goto din_exit; 431 432 atomic_inc(&chip->opencount); 433 if (atomic_read(&chip->opencount) > 1 && chip->rate_set) 434 chip->can_set_rate=0; 435 436 din_exit: 437 mutex_unlock(&chip->mode_mutex); 438 return err; 439 } 440 441 442 443 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */ 444 445 static int pcm_digital_out_open(struct snd_pcm_substream *substream) 446 { 447 struct echoaudio *chip = snd_pcm_substream_chip(substream); 448 int err, max_channels; 449 450 max_channels = num_digital_busses_out(chip) - substream->number; 451 mutex_lock(&chip->mode_mutex); 452 if (chip->digital_mode == DIGITAL_MODE_ADAT) 453 err = pcm_open(substream, max_channels); 454 else /* If the card has ADAT, subtract the 6 channels 455 * that S/PDIF doesn't have 456 */ 457 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT); 458 459 if (err < 0) 460 goto dout_exit; 461 462 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 463 SNDRV_PCM_HW_PARAM_CHANNELS, 464 hw_rule_playback_channels_by_format, 465 NULL, SNDRV_PCM_HW_PARAM_FORMAT, 466 -1)) < 0) 467 goto dout_exit; 468 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0, 469 SNDRV_PCM_HW_PARAM_FORMAT, 470 hw_rule_playback_format_by_channels, 471 NULL, SNDRV_PCM_HW_PARAM_CHANNELS, 472 -1)) < 0) 473 goto dout_exit; 474 atomic_inc(&chip->opencount); 475 if (atomic_read(&chip->opencount) > 1 && chip->rate_set) 476 chip->can_set_rate=0; 477 dout_exit: 478 mutex_unlock(&chip->mode_mutex); 479 return err; 480 } 481 482 #endif /* !ECHOCARD_HAS_VMIXER */ 483 484 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 485 486 487 488 static int pcm_close(struct snd_pcm_substream *substream) 489 { 490 struct echoaudio *chip = snd_pcm_substream_chip(substream); 491 int oc; 492 493 /* Nothing to do here. Audio is already off and pipe will be 494 * freed by its callback 495 */ 496 497 atomic_dec(&chip->opencount); 498 oc = atomic_read(&chip->opencount); 499 dev_dbg(chip->card->dev, "pcm_close oc=%d cs=%d rs=%d\n", oc, 500 chip->can_set_rate, chip->rate_set); 501 if (oc < 2) 502 chip->can_set_rate = 1; 503 if (oc == 0) 504 chip->rate_set = 0; 505 dev_dbg(chip->card->dev, "pcm_close2 oc=%d cs=%d rs=%d\n", oc, 506 chip->can_set_rate, chip->rate_set); 507 508 return 0; 509 } 510 511 512 513 /* Channel allocation and scatter-gather list setup */ 514 static int init_engine(struct snd_pcm_substream *substream, 515 struct snd_pcm_hw_params *hw_params, 516 int pipe_index, int interleave) 517 { 518 struct echoaudio *chip; 519 int err, per, rest, page, edge, offs; 520 struct audiopipe *pipe; 521 522 chip = snd_pcm_substream_chip(substream); 523 pipe = (struct audiopipe *) substream->runtime->private_data; 524 525 /* Sets up che hardware. If it's already initialized, reset and 526 * redo with the new parameters 527 */ 528 spin_lock_irq(&chip->lock); 529 if (pipe->index >= 0) { 530 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index); 531 err = free_pipes(chip, pipe); 532 snd_BUG_ON(err); 533 chip->substream[pipe->index] = NULL; 534 } 535 536 err = allocate_pipes(chip, pipe, pipe_index, interleave); 537 if (err < 0) { 538 spin_unlock_irq(&chip->lock); 539 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n", 540 pipe_index, err); 541 return err; 542 } 543 spin_unlock_irq(&chip->lock); 544 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index); 545 546 dev_dbg(chip->card->dev, 547 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n", 548 params_buffer_bytes(hw_params), params_periods(hw_params), 549 params_period_bytes(hw_params)); 550 err = snd_pcm_lib_malloc_pages(substream, 551 params_buffer_bytes(hw_params)); 552 if (err < 0) { 553 dev_err(chip->card->dev, "malloc_pages err=%d\n", err); 554 spin_lock_irq(&chip->lock); 555 free_pipes(chip, pipe); 556 spin_unlock_irq(&chip->lock); 557 pipe->index = -1; 558 return err; 559 } 560 561 sglist_init(chip, pipe); 562 edge = PAGE_SIZE; 563 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params); 564 per++) { 565 rest = params_period_bytes(hw_params); 566 if (offs + rest > params_buffer_bytes(hw_params)) 567 rest = params_buffer_bytes(hw_params) - offs; 568 while (rest) { 569 dma_addr_t addr; 570 addr = snd_pcm_sgbuf_get_addr(substream, offs); 571 if (rest <= edge - offs) { 572 sglist_add_mapping(chip, pipe, addr, rest); 573 sglist_add_irq(chip, pipe); 574 offs += rest; 575 rest = 0; 576 } else { 577 sglist_add_mapping(chip, pipe, addr, 578 edge - offs); 579 rest -= edge - offs; 580 offs = edge; 581 } 582 if (offs == edge) { 583 edge += PAGE_SIZE; 584 page++; 585 } 586 } 587 } 588 589 /* Close the ring buffer */ 590 sglist_wrap(chip, pipe); 591 592 /* This stuff is used by the irq handler, so it must be 593 * initialized before chip->substream 594 */ 595 chip->last_period[pipe_index] = 0; 596 pipe->last_counter = 0; 597 pipe->position = 0; 598 smp_wmb(); 599 chip->substream[pipe_index] = substream; 600 chip->rate_set = 1; 601 spin_lock_irq(&chip->lock); 602 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den); 603 spin_unlock_irq(&chip->lock); 604 return 0; 605 } 606 607 608 609 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream, 610 struct snd_pcm_hw_params *hw_params) 611 { 612 struct echoaudio *chip = snd_pcm_substream_chip(substream); 613 614 return init_engine(substream, hw_params, px_analog_in(chip) + 615 substream->number, params_channels(hw_params)); 616 } 617 618 619 620 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream, 621 struct snd_pcm_hw_params *hw_params) 622 { 623 return init_engine(substream, hw_params, substream->number, 624 params_channels(hw_params)); 625 } 626 627 628 629 #ifdef ECHOCARD_HAS_DIGITAL_IO 630 631 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream, 632 struct snd_pcm_hw_params *hw_params) 633 { 634 struct echoaudio *chip = snd_pcm_substream_chip(substream); 635 636 return init_engine(substream, hw_params, px_digital_in(chip) + 637 substream->number, params_channels(hw_params)); 638 } 639 640 641 642 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */ 643 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream, 644 struct snd_pcm_hw_params *hw_params) 645 { 646 struct echoaudio *chip = snd_pcm_substream_chip(substream); 647 648 return init_engine(substream, hw_params, px_digital_out(chip) + 649 substream->number, params_channels(hw_params)); 650 } 651 #endif /* !ECHOCARD_HAS_VMIXER */ 652 653 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 654 655 656 657 static int pcm_hw_free(struct snd_pcm_substream *substream) 658 { 659 struct echoaudio *chip; 660 struct audiopipe *pipe; 661 662 chip = snd_pcm_substream_chip(substream); 663 pipe = (struct audiopipe *) substream->runtime->private_data; 664 665 spin_lock_irq(&chip->lock); 666 if (pipe->index >= 0) { 667 dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index); 668 free_pipes(chip, pipe); 669 chip->substream[pipe->index] = NULL; 670 pipe->index = -1; 671 } 672 spin_unlock_irq(&chip->lock); 673 674 snd_pcm_lib_free_pages(substream); 675 return 0; 676 } 677 678 679 680 static int pcm_prepare(struct snd_pcm_substream *substream) 681 { 682 struct echoaudio *chip = snd_pcm_substream_chip(substream); 683 struct snd_pcm_runtime *runtime = substream->runtime; 684 struct audioformat format; 685 int pipe_index = ((struct audiopipe *)runtime->private_data)->index; 686 687 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n", 688 runtime->rate, runtime->format, runtime->channels); 689 format.interleave = runtime->channels; 690 format.data_are_bigendian = 0; 691 format.mono_to_stereo = 0; 692 switch (runtime->format) { 693 case SNDRV_PCM_FORMAT_U8: 694 format.bits_per_sample = 8; 695 break; 696 case SNDRV_PCM_FORMAT_S16_LE: 697 format.bits_per_sample = 16; 698 break; 699 case SNDRV_PCM_FORMAT_S24_3LE: 700 format.bits_per_sample = 24; 701 break; 702 case SNDRV_PCM_FORMAT_S32_BE: 703 format.data_are_bigendian = 1; 704 /* fall through */ 705 case SNDRV_PCM_FORMAT_S32_LE: 706 format.bits_per_sample = 32; 707 break; 708 default: 709 dev_err(chip->card->dev, 710 "Prepare error: unsupported format %d\n", 711 runtime->format); 712 return -EINVAL; 713 } 714 715 if (snd_BUG_ON(pipe_index >= px_num(chip))) 716 return -EINVAL; 717 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) 718 return -EINVAL; 719 set_audio_format(chip, pipe_index, &format); 720 return 0; 721 } 722 723 724 725 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd) 726 { 727 struct echoaudio *chip = snd_pcm_substream_chip(substream); 728 struct audiopipe *pipe; 729 int i, err; 730 u32 channelmask = 0; 731 struct snd_pcm_substream *s; 732 733 snd_pcm_group_for_each_entry(s, substream) { 734 for (i = 0; i < DSP_MAXPIPES; i++) { 735 if (s == chip->substream[i]) { 736 channelmask |= 1 << i; 737 snd_pcm_trigger_done(s, substream); 738 } 739 } 740 } 741 742 spin_lock(&chip->lock); 743 switch (cmd) { 744 case SNDRV_PCM_TRIGGER_RESUME: 745 case SNDRV_PCM_TRIGGER_START: 746 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 747 for (i = 0; i < DSP_MAXPIPES; i++) { 748 if (channelmask & (1 << i)) { 749 pipe = chip->substream[i]->runtime->private_data; 750 switch (pipe->state) { 751 case PIPE_STATE_STOPPED: 752 chip->last_period[i] = 0; 753 pipe->last_counter = 0; 754 pipe->position = 0; 755 *pipe->dma_counter = 0; 756 /* fall through */ 757 case PIPE_STATE_PAUSED: 758 pipe->state = PIPE_STATE_STARTED; 759 break; 760 case PIPE_STATE_STARTED: 761 break; 762 } 763 } 764 } 765 err = start_transport(chip, channelmask, 766 chip->pipe_cyclic_mask); 767 break; 768 case SNDRV_PCM_TRIGGER_SUSPEND: 769 case SNDRV_PCM_TRIGGER_STOP: 770 for (i = 0; i < DSP_MAXPIPES; i++) { 771 if (channelmask & (1 << i)) { 772 pipe = chip->substream[i]->runtime->private_data; 773 pipe->state = PIPE_STATE_STOPPED; 774 } 775 } 776 err = stop_transport(chip, channelmask); 777 break; 778 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 779 for (i = 0; i < DSP_MAXPIPES; i++) { 780 if (channelmask & (1 << i)) { 781 pipe = chip->substream[i]->runtime->private_data; 782 pipe->state = PIPE_STATE_PAUSED; 783 } 784 } 785 err = pause_transport(chip, channelmask); 786 break; 787 default: 788 err = -EINVAL; 789 } 790 spin_unlock(&chip->lock); 791 return err; 792 } 793 794 795 796 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream) 797 { 798 struct snd_pcm_runtime *runtime = substream->runtime; 799 struct audiopipe *pipe = runtime->private_data; 800 size_t cnt, bufsize, pos; 801 802 cnt = le32_to_cpu(*pipe->dma_counter); 803 pipe->position += cnt - pipe->last_counter; 804 pipe->last_counter = cnt; 805 bufsize = substream->runtime->buffer_size; 806 pos = bytes_to_frames(substream->runtime, pipe->position); 807 808 while (pos >= bufsize) { 809 pipe->position -= frames_to_bytes(substream->runtime, bufsize); 810 pos -= bufsize; 811 } 812 return pos; 813 } 814 815 816 817 /* pcm *_ops structures */ 818 static const struct snd_pcm_ops analog_playback_ops = { 819 .open = pcm_analog_out_open, 820 .close = pcm_close, 821 .ioctl = snd_pcm_lib_ioctl, 822 .hw_params = pcm_analog_out_hw_params, 823 .hw_free = pcm_hw_free, 824 .prepare = pcm_prepare, 825 .trigger = pcm_trigger, 826 .pointer = pcm_pointer, 827 }; 828 static const struct snd_pcm_ops analog_capture_ops = { 829 .open = pcm_analog_in_open, 830 .close = pcm_close, 831 .ioctl = snd_pcm_lib_ioctl, 832 .hw_params = pcm_analog_in_hw_params, 833 .hw_free = pcm_hw_free, 834 .prepare = pcm_prepare, 835 .trigger = pcm_trigger, 836 .pointer = pcm_pointer, 837 }; 838 #ifdef ECHOCARD_HAS_DIGITAL_IO 839 #ifndef ECHOCARD_HAS_VMIXER 840 static const struct snd_pcm_ops digital_playback_ops = { 841 .open = pcm_digital_out_open, 842 .close = pcm_close, 843 .ioctl = snd_pcm_lib_ioctl, 844 .hw_params = pcm_digital_out_hw_params, 845 .hw_free = pcm_hw_free, 846 .prepare = pcm_prepare, 847 .trigger = pcm_trigger, 848 .pointer = pcm_pointer, 849 }; 850 #endif /* !ECHOCARD_HAS_VMIXER */ 851 static const struct snd_pcm_ops digital_capture_ops = { 852 .open = pcm_digital_in_open, 853 .close = pcm_close, 854 .ioctl = snd_pcm_lib_ioctl, 855 .hw_params = pcm_digital_in_hw_params, 856 .hw_free = pcm_hw_free, 857 .prepare = pcm_prepare, 858 .trigger = pcm_trigger, 859 .pointer = pcm_pointer, 860 }; 861 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 862 863 864 865 /* Preallocate memory only for the first substream because it's the most 866 * used one 867 */ 868 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev) 869 { 870 struct snd_pcm_substream *ss; 871 int stream; 872 873 for (stream = 0; stream < 2; stream++) 874 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) 875 snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG, 876 dev, 877 ss->number ? 0 : 128<<10, 878 256<<10); 879 } 880 881 882 883 /*<--snd_echo_probe() */ 884 static int snd_echo_new_pcm(struct echoaudio *chip) 885 { 886 struct snd_pcm *pcm; 887 int err; 888 889 #ifdef ECHOCARD_HAS_VMIXER 890 /* This card has a Vmixer, that is there is no direct mapping from PCM 891 streams to physical outputs. The user can mix the streams as he wishes 892 via control interface and it's possible to send any stream to any 893 output, thus it makes no sense to keep analog and digital outputs 894 separated */ 895 896 /* PCM#0 Virtual outputs and analog inputs */ 897 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip), 898 num_analog_busses_in(chip), &pcm)) < 0) 899 return err; 900 pcm->private_data = chip; 901 chip->analog_pcm = pcm; 902 strcpy(pcm->name, chip->card->shortname); 903 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops); 904 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops); 905 snd_echo_preallocate_pages(pcm, &chip->pci->dev); 906 907 #ifdef ECHOCARD_HAS_DIGITAL_IO 908 /* PCM#1 Digital inputs, no outputs */ 909 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0, 910 num_digital_busses_in(chip), &pcm)) < 0) 911 return err; 912 pcm->private_data = chip; 913 chip->digital_pcm = pcm; 914 strcpy(pcm->name, chip->card->shortname); 915 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops); 916 snd_echo_preallocate_pages(pcm, &chip->pci->dev); 917 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 918 919 #else /* ECHOCARD_HAS_VMIXER */ 920 921 /* The card can manage substreams formed by analog and digital channels 922 at the same time, but I prefer to keep analog and digital channels 923 separated, because that mixed thing is confusing and useless. So we 924 register two PCM devices: */ 925 926 /* PCM#0 Analog i/o */ 927 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0, 928 num_analog_busses_out(chip), 929 num_analog_busses_in(chip), &pcm)) < 0) 930 return err; 931 pcm->private_data = chip; 932 chip->analog_pcm = pcm; 933 strcpy(pcm->name, chip->card->shortname); 934 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops); 935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops); 936 snd_echo_preallocate_pages(pcm, &chip->pci->dev); 937 938 #ifdef ECHOCARD_HAS_DIGITAL_IO 939 /* PCM#1 Digital i/o */ 940 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 941 num_digital_busses_out(chip), 942 num_digital_busses_in(chip), &pcm)) < 0) 943 return err; 944 pcm->private_data = chip; 945 chip->digital_pcm = pcm; 946 strcpy(pcm->name, chip->card->shortname); 947 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops); 948 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops); 949 snd_echo_preallocate_pages(pcm, &chip->pci->dev); 950 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 951 952 #endif /* ECHOCARD_HAS_VMIXER */ 953 954 return 0; 955 } 956 957 958 959 960 /****************************************************************************** 961 Control interface 962 ******************************************************************************/ 963 964 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN) 965 966 /******************* PCM output volume *******************/ 967 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol, 968 struct snd_ctl_elem_info *uinfo) 969 { 970 struct echoaudio *chip; 971 972 chip = snd_kcontrol_chip(kcontrol); 973 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 974 uinfo->count = num_busses_out(chip); 975 uinfo->value.integer.min = ECHOGAIN_MINOUT; 976 uinfo->value.integer.max = ECHOGAIN_MAXOUT; 977 return 0; 978 } 979 980 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol, 981 struct snd_ctl_elem_value *ucontrol) 982 { 983 struct echoaudio *chip; 984 int c; 985 986 chip = snd_kcontrol_chip(kcontrol); 987 for (c = 0; c < num_busses_out(chip); c++) 988 ucontrol->value.integer.value[c] = chip->output_gain[c]; 989 return 0; 990 } 991 992 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol, 993 struct snd_ctl_elem_value *ucontrol) 994 { 995 struct echoaudio *chip; 996 int c, changed, gain; 997 998 changed = 0; 999 chip = snd_kcontrol_chip(kcontrol); 1000 spin_lock_irq(&chip->lock); 1001 for (c = 0; c < num_busses_out(chip); c++) { 1002 gain = ucontrol->value.integer.value[c]; 1003 /* Ignore out of range values */ 1004 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT) 1005 continue; 1006 if (chip->output_gain[c] != gain) { 1007 set_output_gain(chip, c, gain); 1008 changed = 1; 1009 } 1010 } 1011 if (changed) 1012 update_output_line_level(chip); 1013 spin_unlock_irq(&chip->lock); 1014 return changed; 1015 } 1016 1017 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN 1018 /* On the Mia this one controls the line-out volume */ 1019 static const struct snd_kcontrol_new snd_echo_line_output_gain = { 1020 .name = "Line Playback Volume", 1021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1022 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 1023 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1024 .info = snd_echo_output_gain_info, 1025 .get = snd_echo_output_gain_get, 1026 .put = snd_echo_output_gain_put, 1027 .tlv = {.p = db_scale_output_gain}, 1028 }; 1029 #else 1030 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = { 1031 .name = "PCM Playback Volume", 1032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1033 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1034 .info = snd_echo_output_gain_info, 1035 .get = snd_echo_output_gain_get, 1036 .put = snd_echo_output_gain_put, 1037 .tlv = {.p = db_scale_output_gain}, 1038 }; 1039 #endif 1040 1041 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */ 1042 1043 1044 1045 #ifdef ECHOCARD_HAS_INPUT_GAIN 1046 1047 /******************* Analog input volume *******************/ 1048 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol, 1049 struct snd_ctl_elem_info *uinfo) 1050 { 1051 struct echoaudio *chip; 1052 1053 chip = snd_kcontrol_chip(kcontrol); 1054 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1055 uinfo->count = num_analog_busses_in(chip); 1056 uinfo->value.integer.min = ECHOGAIN_MININP; 1057 uinfo->value.integer.max = ECHOGAIN_MAXINP; 1058 return 0; 1059 } 1060 1061 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol, 1062 struct snd_ctl_elem_value *ucontrol) 1063 { 1064 struct echoaudio *chip; 1065 int c; 1066 1067 chip = snd_kcontrol_chip(kcontrol); 1068 for (c = 0; c < num_analog_busses_in(chip); c++) 1069 ucontrol->value.integer.value[c] = chip->input_gain[c]; 1070 return 0; 1071 } 1072 1073 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol, 1074 struct snd_ctl_elem_value *ucontrol) 1075 { 1076 struct echoaudio *chip; 1077 int c, gain, changed; 1078 1079 changed = 0; 1080 chip = snd_kcontrol_chip(kcontrol); 1081 spin_lock_irq(&chip->lock); 1082 for (c = 0; c < num_analog_busses_in(chip); c++) { 1083 gain = ucontrol->value.integer.value[c]; 1084 /* Ignore out of range values */ 1085 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP) 1086 continue; 1087 if (chip->input_gain[c] != gain) { 1088 set_input_gain(chip, c, gain); 1089 changed = 1; 1090 } 1091 } 1092 if (changed) 1093 update_input_line_level(chip); 1094 spin_unlock_irq(&chip->lock); 1095 return changed; 1096 } 1097 1098 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0); 1099 1100 static const struct snd_kcontrol_new snd_echo_line_input_gain = { 1101 .name = "Line Capture Volume", 1102 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1103 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1104 .info = snd_echo_input_gain_info, 1105 .get = snd_echo_input_gain_get, 1106 .put = snd_echo_input_gain_put, 1107 .tlv = {.p = db_scale_input_gain}, 1108 }; 1109 1110 #endif /* ECHOCARD_HAS_INPUT_GAIN */ 1111 1112 1113 1114 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL 1115 1116 /************ Analog output nominal level (+4dBu / -10dBV) ***************/ 1117 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol, 1118 struct snd_ctl_elem_info *uinfo) 1119 { 1120 struct echoaudio *chip; 1121 1122 chip = snd_kcontrol_chip(kcontrol); 1123 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1124 uinfo->count = num_analog_busses_out(chip); 1125 uinfo->value.integer.min = 0; 1126 uinfo->value.integer.max = 1; 1127 return 0; 1128 } 1129 1130 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol, 1131 struct snd_ctl_elem_value *ucontrol) 1132 { 1133 struct echoaudio *chip; 1134 int c; 1135 1136 chip = snd_kcontrol_chip(kcontrol); 1137 for (c = 0; c < num_analog_busses_out(chip); c++) 1138 ucontrol->value.integer.value[c] = chip->nominal_level[c]; 1139 return 0; 1140 } 1141 1142 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol, 1143 struct snd_ctl_elem_value *ucontrol) 1144 { 1145 struct echoaudio *chip; 1146 int c, changed; 1147 1148 changed = 0; 1149 chip = snd_kcontrol_chip(kcontrol); 1150 spin_lock_irq(&chip->lock); 1151 for (c = 0; c < num_analog_busses_out(chip); c++) { 1152 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) { 1153 set_nominal_level(chip, c, 1154 ucontrol->value.integer.value[c]); 1155 changed = 1; 1156 } 1157 } 1158 if (changed) 1159 update_output_line_level(chip); 1160 spin_unlock_irq(&chip->lock); 1161 return changed; 1162 } 1163 1164 static const struct snd_kcontrol_new snd_echo_output_nominal_level = { 1165 .name = "Line Playback Switch (-10dBV)", 1166 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1167 .info = snd_echo_output_nominal_info, 1168 .get = snd_echo_output_nominal_get, 1169 .put = snd_echo_output_nominal_put, 1170 }; 1171 1172 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */ 1173 1174 1175 1176 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL 1177 1178 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/ 1179 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol, 1180 struct snd_ctl_elem_info *uinfo) 1181 { 1182 struct echoaudio *chip; 1183 1184 chip = snd_kcontrol_chip(kcontrol); 1185 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1186 uinfo->count = num_analog_busses_in(chip); 1187 uinfo->value.integer.min = 0; 1188 uinfo->value.integer.max = 1; 1189 return 0; 1190 } 1191 1192 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol, 1193 struct snd_ctl_elem_value *ucontrol) 1194 { 1195 struct echoaudio *chip; 1196 int c; 1197 1198 chip = snd_kcontrol_chip(kcontrol); 1199 for (c = 0; c < num_analog_busses_in(chip); c++) 1200 ucontrol->value.integer.value[c] = 1201 chip->nominal_level[bx_analog_in(chip) + c]; 1202 return 0; 1203 } 1204 1205 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol, 1206 struct snd_ctl_elem_value *ucontrol) 1207 { 1208 struct echoaudio *chip; 1209 int c, changed; 1210 1211 changed = 0; 1212 chip = snd_kcontrol_chip(kcontrol); 1213 spin_lock_irq(&chip->lock); 1214 for (c = 0; c < num_analog_busses_in(chip); c++) { 1215 if (chip->nominal_level[bx_analog_in(chip) + c] != 1216 ucontrol->value.integer.value[c]) { 1217 set_nominal_level(chip, bx_analog_in(chip) + c, 1218 ucontrol->value.integer.value[c]); 1219 changed = 1; 1220 } 1221 } 1222 if (changed) 1223 update_output_line_level(chip); /* "Output" is not a mistake 1224 * here. 1225 */ 1226 spin_unlock_irq(&chip->lock); 1227 return changed; 1228 } 1229 1230 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = { 1231 .name = "Line Capture Switch (-10dBV)", 1232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1233 .info = snd_echo_input_nominal_info, 1234 .get = snd_echo_input_nominal_get, 1235 .put = snd_echo_input_nominal_put, 1236 }; 1237 1238 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */ 1239 1240 1241 1242 #ifdef ECHOCARD_HAS_MONITOR 1243 1244 /******************* Monitor mixer *******************/ 1245 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol, 1246 struct snd_ctl_elem_info *uinfo) 1247 { 1248 struct echoaudio *chip; 1249 1250 chip = snd_kcontrol_chip(kcontrol); 1251 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1252 uinfo->count = 1; 1253 uinfo->value.integer.min = ECHOGAIN_MINOUT; 1254 uinfo->value.integer.max = ECHOGAIN_MAXOUT; 1255 uinfo->dimen.d[0] = num_busses_out(chip); 1256 uinfo->dimen.d[1] = num_busses_in(chip); 1257 return 0; 1258 } 1259 1260 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol, 1261 struct snd_ctl_elem_value *ucontrol) 1262 { 1263 struct echoaudio *chip = snd_kcontrol_chip(kcontrol); 1264 unsigned int out = ucontrol->id.index / num_busses_in(chip); 1265 unsigned int in = ucontrol->id.index % num_busses_in(chip); 1266 1267 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS) 1268 return -EINVAL; 1269 1270 ucontrol->value.integer.value[0] = chip->monitor_gain[out][in]; 1271 return 0; 1272 } 1273 1274 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol, 1275 struct snd_ctl_elem_value *ucontrol) 1276 { 1277 struct echoaudio *chip; 1278 int changed, gain; 1279 unsigned int out, in; 1280 1281 changed = 0; 1282 chip = snd_kcontrol_chip(kcontrol); 1283 out = ucontrol->id.index / num_busses_in(chip); 1284 in = ucontrol->id.index % num_busses_in(chip); 1285 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS) 1286 return -EINVAL; 1287 gain = ucontrol->value.integer.value[0]; 1288 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT) 1289 return -EINVAL; 1290 if (chip->monitor_gain[out][in] != gain) { 1291 spin_lock_irq(&chip->lock); 1292 set_monitor_gain(chip, out, in, gain); 1293 update_output_line_level(chip); 1294 spin_unlock_irq(&chip->lock); 1295 changed = 1; 1296 } 1297 return changed; 1298 } 1299 1300 static struct snd_kcontrol_new snd_echo_monitor_mixer = { 1301 .name = "Monitor Mixer Volume", 1302 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1303 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1304 .info = snd_echo_mixer_info, 1305 .get = snd_echo_mixer_get, 1306 .put = snd_echo_mixer_put, 1307 .tlv = {.p = db_scale_output_gain}, 1308 }; 1309 1310 #endif /* ECHOCARD_HAS_MONITOR */ 1311 1312 1313 1314 #ifdef ECHOCARD_HAS_VMIXER 1315 1316 /******************* Vmixer *******************/ 1317 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol, 1318 struct snd_ctl_elem_info *uinfo) 1319 { 1320 struct echoaudio *chip; 1321 1322 chip = snd_kcontrol_chip(kcontrol); 1323 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1324 uinfo->count = 1; 1325 uinfo->value.integer.min = ECHOGAIN_MINOUT; 1326 uinfo->value.integer.max = ECHOGAIN_MAXOUT; 1327 uinfo->dimen.d[0] = num_busses_out(chip); 1328 uinfo->dimen.d[1] = num_pipes_out(chip); 1329 return 0; 1330 } 1331 1332 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol, 1333 struct snd_ctl_elem_value *ucontrol) 1334 { 1335 struct echoaudio *chip; 1336 1337 chip = snd_kcontrol_chip(kcontrol); 1338 ucontrol->value.integer.value[0] = 1339 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)] 1340 [ucontrol->id.index % num_pipes_out(chip)]; 1341 return 0; 1342 } 1343 1344 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol, 1345 struct snd_ctl_elem_value *ucontrol) 1346 { 1347 struct echoaudio *chip; 1348 int gain, changed; 1349 short vch, out; 1350 1351 changed = 0; 1352 chip = snd_kcontrol_chip(kcontrol); 1353 out = ucontrol->id.index / num_pipes_out(chip); 1354 vch = ucontrol->id.index % num_pipes_out(chip); 1355 gain = ucontrol->value.integer.value[0]; 1356 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT) 1357 return -EINVAL; 1358 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) { 1359 spin_lock_irq(&chip->lock); 1360 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]); 1361 update_vmixer_level(chip); 1362 spin_unlock_irq(&chip->lock); 1363 changed = 1; 1364 } 1365 return changed; 1366 } 1367 1368 static struct snd_kcontrol_new snd_echo_vmixer = { 1369 .name = "VMixer Volume", 1370 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1371 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1372 .info = snd_echo_vmixer_info, 1373 .get = snd_echo_vmixer_get, 1374 .put = snd_echo_vmixer_put, 1375 .tlv = {.p = db_scale_output_gain}, 1376 }; 1377 1378 #endif /* ECHOCARD_HAS_VMIXER */ 1379 1380 1381 1382 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH 1383 1384 /******************* Digital mode switch *******************/ 1385 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol, 1386 struct snd_ctl_elem_info *uinfo) 1387 { 1388 static const char * const names[4] = { 1389 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical", 1390 "S/PDIF Cdrom" 1391 }; 1392 struct echoaudio *chip; 1393 1394 chip = snd_kcontrol_chip(kcontrol); 1395 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names); 1396 } 1397 1398 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol, 1399 struct snd_ctl_elem_value *ucontrol) 1400 { 1401 struct echoaudio *chip; 1402 int i, mode; 1403 1404 chip = snd_kcontrol_chip(kcontrol); 1405 mode = chip->digital_mode; 1406 for (i = chip->num_digital_modes - 1; i >= 0; i--) 1407 if (mode == chip->digital_mode_list[i]) { 1408 ucontrol->value.enumerated.item[0] = i; 1409 break; 1410 } 1411 return 0; 1412 } 1413 1414 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol, 1415 struct snd_ctl_elem_value *ucontrol) 1416 { 1417 struct echoaudio *chip; 1418 int changed; 1419 unsigned short emode, dmode; 1420 1421 changed = 0; 1422 chip = snd_kcontrol_chip(kcontrol); 1423 1424 emode = ucontrol->value.enumerated.item[0]; 1425 if (emode >= chip->num_digital_modes) 1426 return -EINVAL; 1427 dmode = chip->digital_mode_list[emode]; 1428 1429 if (dmode != chip->digital_mode) { 1430 /* mode_mutex is required to make this operation atomic wrt 1431 pcm_digital_*_open() and set_input_clock() functions. */ 1432 mutex_lock(&chip->mode_mutex); 1433 1434 /* Do not allow the user to change the digital mode when a pcm 1435 device is open because it also changes the number of channels 1436 and the allowed sample rates */ 1437 if (atomic_read(&chip->opencount)) { 1438 changed = -EAGAIN; 1439 } else { 1440 changed = set_digital_mode(chip, dmode); 1441 /* If we had to change the clock source, report it */ 1442 if (changed > 0 && chip->clock_src_ctl) { 1443 snd_ctl_notify(chip->card, 1444 SNDRV_CTL_EVENT_MASK_VALUE, 1445 &chip->clock_src_ctl->id); 1446 dev_dbg(chip->card->dev, 1447 "SDM() =%d\n", changed); 1448 } 1449 if (changed >= 0) 1450 changed = 1; /* No errors */ 1451 } 1452 mutex_unlock(&chip->mode_mutex); 1453 } 1454 return changed; 1455 } 1456 1457 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = { 1458 .name = "Digital mode Switch", 1459 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1460 .info = snd_echo_digital_mode_info, 1461 .get = snd_echo_digital_mode_get, 1462 .put = snd_echo_digital_mode_put, 1463 }; 1464 1465 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */ 1466 1467 1468 1469 #ifdef ECHOCARD_HAS_DIGITAL_IO 1470 1471 /******************* S/PDIF mode switch *******************/ 1472 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol, 1473 struct snd_ctl_elem_info *uinfo) 1474 { 1475 static const char * const names[2] = {"Consumer", "Professional"}; 1476 1477 return snd_ctl_enum_info(uinfo, 1, 2, names); 1478 } 1479 1480 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol, 1481 struct snd_ctl_elem_value *ucontrol) 1482 { 1483 struct echoaudio *chip; 1484 1485 chip = snd_kcontrol_chip(kcontrol); 1486 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif; 1487 return 0; 1488 } 1489 1490 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol, 1491 struct snd_ctl_elem_value *ucontrol) 1492 { 1493 struct echoaudio *chip; 1494 int mode; 1495 1496 chip = snd_kcontrol_chip(kcontrol); 1497 mode = !!ucontrol->value.enumerated.item[0]; 1498 if (mode != chip->professional_spdif) { 1499 spin_lock_irq(&chip->lock); 1500 set_professional_spdif(chip, mode); 1501 spin_unlock_irq(&chip->lock); 1502 return 1; 1503 } 1504 return 0; 1505 } 1506 1507 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = { 1508 .name = "S/PDIF mode Switch", 1509 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1510 .info = snd_echo_spdif_mode_info, 1511 .get = snd_echo_spdif_mode_get, 1512 .put = snd_echo_spdif_mode_put, 1513 }; 1514 1515 #endif /* ECHOCARD_HAS_DIGITAL_IO */ 1516 1517 1518 1519 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK 1520 1521 /******************* Select input clock source *******************/ 1522 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol, 1523 struct snd_ctl_elem_info *uinfo) 1524 { 1525 static const char * const names[8] = { 1526 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync", 1527 "ESync96", "MTC" 1528 }; 1529 struct echoaudio *chip; 1530 1531 chip = snd_kcontrol_chip(kcontrol); 1532 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names); 1533 } 1534 1535 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol, 1536 struct snd_ctl_elem_value *ucontrol) 1537 { 1538 struct echoaudio *chip; 1539 int i, clock; 1540 1541 chip = snd_kcontrol_chip(kcontrol); 1542 clock = chip->input_clock; 1543 1544 for (i = 0; i < chip->num_clock_sources; i++) 1545 if (clock == chip->clock_source_list[i]) 1546 ucontrol->value.enumerated.item[0] = i; 1547 1548 return 0; 1549 } 1550 1551 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol, 1552 struct snd_ctl_elem_value *ucontrol) 1553 { 1554 struct echoaudio *chip; 1555 int changed; 1556 unsigned int eclock, dclock; 1557 1558 changed = 0; 1559 chip = snd_kcontrol_chip(kcontrol); 1560 eclock = ucontrol->value.enumerated.item[0]; 1561 if (eclock >= chip->input_clock_types) 1562 return -EINVAL; 1563 dclock = chip->clock_source_list[eclock]; 1564 if (chip->input_clock != dclock) { 1565 mutex_lock(&chip->mode_mutex); 1566 spin_lock_irq(&chip->lock); 1567 if ((changed = set_input_clock(chip, dclock)) == 0) 1568 changed = 1; /* no errors */ 1569 spin_unlock_irq(&chip->lock); 1570 mutex_unlock(&chip->mode_mutex); 1571 } 1572 1573 if (changed < 0) 1574 dev_dbg(chip->card->dev, 1575 "seticlk val%d err 0x%x\n", dclock, changed); 1576 1577 return changed; 1578 } 1579 1580 static const struct snd_kcontrol_new snd_echo_clock_source_switch = { 1581 .name = "Sample Clock Source", 1582 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1583 .info = snd_echo_clock_source_info, 1584 .get = snd_echo_clock_source_get, 1585 .put = snd_echo_clock_source_put, 1586 }; 1587 1588 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */ 1589 1590 1591 1592 #ifdef ECHOCARD_HAS_PHANTOM_POWER 1593 1594 /******************* Phantom power switch *******************/ 1595 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info 1596 1597 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol, 1598 struct snd_ctl_elem_value *ucontrol) 1599 { 1600 struct echoaudio *chip = snd_kcontrol_chip(kcontrol); 1601 1602 ucontrol->value.integer.value[0] = chip->phantom_power; 1603 return 0; 1604 } 1605 1606 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol, 1607 struct snd_ctl_elem_value *ucontrol) 1608 { 1609 struct echoaudio *chip = snd_kcontrol_chip(kcontrol); 1610 int power, changed = 0; 1611 1612 power = !!ucontrol->value.integer.value[0]; 1613 if (chip->phantom_power != power) { 1614 spin_lock_irq(&chip->lock); 1615 changed = set_phantom_power(chip, power); 1616 spin_unlock_irq(&chip->lock); 1617 if (changed == 0) 1618 changed = 1; /* no errors */ 1619 } 1620 return changed; 1621 } 1622 1623 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = { 1624 .name = "Phantom power Switch", 1625 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1626 .info = snd_echo_phantom_power_info, 1627 .get = snd_echo_phantom_power_get, 1628 .put = snd_echo_phantom_power_put, 1629 }; 1630 1631 #endif /* ECHOCARD_HAS_PHANTOM_POWER */ 1632 1633 1634 1635 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE 1636 1637 /******************* Digital input automute switch *******************/ 1638 #define snd_echo_automute_info snd_ctl_boolean_mono_info 1639 1640 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol, 1641 struct snd_ctl_elem_value *ucontrol) 1642 { 1643 struct echoaudio *chip = snd_kcontrol_chip(kcontrol); 1644 1645 ucontrol->value.integer.value[0] = chip->digital_in_automute; 1646 return 0; 1647 } 1648 1649 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol, 1650 struct snd_ctl_elem_value *ucontrol) 1651 { 1652 struct echoaudio *chip = snd_kcontrol_chip(kcontrol); 1653 int automute, changed = 0; 1654 1655 automute = !!ucontrol->value.integer.value[0]; 1656 if (chip->digital_in_automute != automute) { 1657 spin_lock_irq(&chip->lock); 1658 changed = set_input_auto_mute(chip, automute); 1659 spin_unlock_irq(&chip->lock); 1660 if (changed == 0) 1661 changed = 1; /* no errors */ 1662 } 1663 return changed; 1664 } 1665 1666 static const struct snd_kcontrol_new snd_echo_automute_switch = { 1667 .name = "Digital Capture Switch (automute)", 1668 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1669 .info = snd_echo_automute_info, 1670 .get = snd_echo_automute_get, 1671 .put = snd_echo_automute_put, 1672 }; 1673 1674 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */ 1675 1676 1677 1678 /******************* VU-meters switch *******************/ 1679 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info 1680 1681 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol, 1682 struct snd_ctl_elem_value *ucontrol) 1683 { 1684 struct echoaudio *chip; 1685 1686 chip = snd_kcontrol_chip(kcontrol); 1687 spin_lock_irq(&chip->lock); 1688 set_meters_on(chip, ucontrol->value.integer.value[0]); 1689 spin_unlock_irq(&chip->lock); 1690 return 1; 1691 } 1692 1693 static const struct snd_kcontrol_new snd_echo_vumeters_switch = { 1694 .name = "VU-meters Switch", 1695 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1696 .access = SNDRV_CTL_ELEM_ACCESS_WRITE, 1697 .info = snd_echo_vumeters_switch_info, 1698 .put = snd_echo_vumeters_switch_put, 1699 }; 1700 1701 1702 1703 /***** Read VU-meters (input, output, analog and digital together) *****/ 1704 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol, 1705 struct snd_ctl_elem_info *uinfo) 1706 { 1707 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1708 uinfo->count = 96; 1709 uinfo->value.integer.min = ECHOGAIN_MINOUT; 1710 uinfo->value.integer.max = 0; 1711 #ifdef ECHOCARD_HAS_VMIXER 1712 uinfo->dimen.d[0] = 3; /* Out, In, Virt */ 1713 #else 1714 uinfo->dimen.d[0] = 2; /* Out, In */ 1715 #endif 1716 uinfo->dimen.d[1] = 16; /* 16 channels */ 1717 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */ 1718 return 0; 1719 } 1720 1721 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol, 1722 struct snd_ctl_elem_value *ucontrol) 1723 { 1724 struct echoaudio *chip; 1725 1726 chip = snd_kcontrol_chip(kcontrol); 1727 get_audio_meters(chip, ucontrol->value.integer.value); 1728 return 0; 1729 } 1730 1731 static const struct snd_kcontrol_new snd_echo_vumeters = { 1732 .name = "VU-meters", 1733 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1734 .access = SNDRV_CTL_ELEM_ACCESS_READ | 1735 SNDRV_CTL_ELEM_ACCESS_VOLATILE | 1736 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1737 .info = snd_echo_vumeters_info, 1738 .get = snd_echo_vumeters_get, 1739 .tlv = {.p = db_scale_output_gain}, 1740 }; 1741 1742 1743 1744 /*** Channels info - it exports informations about the number of channels ***/ 1745 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol, 1746 struct snd_ctl_elem_info *uinfo) 1747 { 1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1749 uinfo->count = 6; 1750 uinfo->value.integer.min = 0; 1751 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER; 1752 return 0; 1753 } 1754 1755 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol, 1756 struct snd_ctl_elem_value *ucontrol) 1757 { 1758 struct echoaudio *chip; 1759 int detected, clocks, bit, src; 1760 1761 chip = snd_kcontrol_chip(kcontrol); 1762 ucontrol->value.integer.value[0] = num_busses_in(chip); 1763 ucontrol->value.integer.value[1] = num_analog_busses_in(chip); 1764 ucontrol->value.integer.value[2] = num_busses_out(chip); 1765 ucontrol->value.integer.value[3] = num_analog_busses_out(chip); 1766 ucontrol->value.integer.value[4] = num_pipes_out(chip); 1767 1768 /* Compute the bitmask of the currently valid input clocks */ 1769 detected = detect_input_clocks(chip); 1770 clocks = 0; 1771 src = chip->num_clock_sources - 1; 1772 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--) 1773 if (detected & (1 << bit)) 1774 for (; src >= 0; src--) 1775 if (bit == chip->clock_source_list[src]) { 1776 clocks |= 1 << src; 1777 break; 1778 } 1779 ucontrol->value.integer.value[5] = clocks; 1780 1781 return 0; 1782 } 1783 1784 static const struct snd_kcontrol_new snd_echo_channels_info = { 1785 .name = "Channels info", 1786 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, 1787 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1788 .info = snd_echo_channels_info_info, 1789 .get = snd_echo_channels_info_get, 1790 }; 1791 1792 1793 1794 1795 /****************************************************************************** 1796 IRQ Handler 1797 ******************************************************************************/ 1798 1799 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id) 1800 { 1801 struct echoaudio *chip = dev_id; 1802 struct snd_pcm_substream *substream; 1803 int period, ss, st; 1804 1805 spin_lock(&chip->lock); 1806 st = service_irq(chip); 1807 if (st < 0) { 1808 spin_unlock(&chip->lock); 1809 return IRQ_NONE; 1810 } 1811 /* The hardware doesn't tell us which substream caused the irq, 1812 thus we have to check all running substreams. */ 1813 for (ss = 0; ss < DSP_MAXPIPES; ss++) { 1814 substream = chip->substream[ss]; 1815 if (substream && ((struct audiopipe *)substream->runtime-> 1816 private_data)->state == PIPE_STATE_STARTED) { 1817 period = pcm_pointer(substream) / 1818 substream->runtime->period_size; 1819 if (period != chip->last_period[ss]) { 1820 chip->last_period[ss] = period; 1821 spin_unlock(&chip->lock); 1822 snd_pcm_period_elapsed(substream); 1823 spin_lock(&chip->lock); 1824 } 1825 } 1826 } 1827 spin_unlock(&chip->lock); 1828 1829 #ifdef ECHOCARD_HAS_MIDI 1830 if (st > 0 && chip->midi_in) { 1831 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st); 1832 dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st); 1833 } 1834 #endif 1835 return IRQ_HANDLED; 1836 } 1837 1838 1839 1840 1841 /****************************************************************************** 1842 Module construction / destruction 1843 ******************************************************************************/ 1844 1845 static int snd_echo_free(struct echoaudio *chip) 1846 { 1847 if (chip->comm_page) 1848 rest_in_peace(chip); 1849 1850 if (chip->irq >= 0) 1851 free_irq(chip->irq, chip); 1852 1853 if (chip->comm_page) 1854 snd_dma_free_pages(&chip->commpage_dma_buf); 1855 1856 iounmap(chip->dsp_registers); 1857 release_and_free_resource(chip->iores); 1858 pci_disable_device(chip->pci); 1859 1860 /* release chip data */ 1861 free_firmware_cache(chip); 1862 kfree(chip); 1863 return 0; 1864 } 1865 1866 1867 1868 static int snd_echo_dev_free(struct snd_device *device) 1869 { 1870 struct echoaudio *chip = device->device_data; 1871 1872 return snd_echo_free(chip); 1873 } 1874 1875 1876 1877 /* <--snd_echo_probe() */ 1878 static int snd_echo_create(struct snd_card *card, 1879 struct pci_dev *pci, 1880 struct echoaudio **rchip) 1881 { 1882 struct echoaudio *chip; 1883 int err; 1884 size_t sz; 1885 static struct snd_device_ops ops = { 1886 .dev_free = snd_echo_dev_free, 1887 }; 1888 1889 *rchip = NULL; 1890 1891 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0); 1892 1893 if ((err = pci_enable_device(pci)) < 0) 1894 return err; 1895 pci_set_master(pci); 1896 1897 /* Allocate chip if needed */ 1898 if (!*rchip) { 1899 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1900 if (!chip) { 1901 pci_disable_device(pci); 1902 return -ENOMEM; 1903 } 1904 dev_dbg(card->dev, "chip=%p\n", chip); 1905 spin_lock_init(&chip->lock); 1906 chip->card = card; 1907 chip->pci = pci; 1908 chip->irq = -1; 1909 atomic_set(&chip->opencount, 0); 1910 mutex_init(&chip->mode_mutex); 1911 chip->can_set_rate = 1; 1912 } else { 1913 /* If this was called from the resume function, chip is 1914 * already allocated and it contains current card settings. 1915 */ 1916 chip = *rchip; 1917 } 1918 1919 /* PCI resource allocation */ 1920 chip->dsp_registers_phys = pci_resource_start(pci, 0); 1921 sz = pci_resource_len(pci, 0); 1922 if (sz > PAGE_SIZE) 1923 sz = PAGE_SIZE; /* We map only the required part */ 1924 1925 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz, 1926 ECHOCARD_NAME)) == NULL) { 1927 dev_err(chip->card->dev, "cannot get memory region\n"); 1928 snd_echo_free(chip); 1929 return -EBUSY; 1930 } 1931 chip->dsp_registers = (volatile u32 __iomem *) 1932 ioremap_nocache(chip->dsp_registers_phys, sz); 1933 if (!chip->dsp_registers) { 1934 dev_err(chip->card->dev, "ioremap failed\n"); 1935 snd_echo_free(chip); 1936 return -ENOMEM; 1937 } 1938 1939 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED, 1940 KBUILD_MODNAME, chip)) { 1941 dev_err(chip->card->dev, "cannot grab irq\n"); 1942 snd_echo_free(chip); 1943 return -EBUSY; 1944 } 1945 chip->irq = pci->irq; 1946 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n", 1947 chip->pci, chip->irq, chip->pci->subsystem_device); 1948 1949 /* Create the DSP comm page - this is the area of memory used for most 1950 of the communication with the DSP, which accesses it via bus mastering */ 1951 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 1952 sizeof(struct comm_page), 1953 &chip->commpage_dma_buf) < 0) { 1954 dev_err(chip->card->dev, "cannot allocate the comm page\n"); 1955 snd_echo_free(chip); 1956 return -ENOMEM; 1957 } 1958 chip->comm_page_phys = chip->commpage_dma_buf.addr; 1959 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area; 1960 1961 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device); 1962 if (err >= 0) 1963 err = set_mixer_defaults(chip); 1964 if (err < 0) { 1965 dev_err(card->dev, "init_hw err=%d\n", err); 1966 snd_echo_free(chip); 1967 return err; 1968 } 1969 1970 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 1971 snd_echo_free(chip); 1972 return err; 1973 } 1974 *rchip = chip; 1975 /* Init done ! */ 1976 return 0; 1977 } 1978 1979 1980 1981 /* constructor */ 1982 static int snd_echo_probe(struct pci_dev *pci, 1983 const struct pci_device_id *pci_id) 1984 { 1985 static int dev; 1986 struct snd_card *card; 1987 struct echoaudio *chip; 1988 char *dsp; 1989 int i, err; 1990 1991 if (dev >= SNDRV_CARDS) 1992 return -ENODEV; 1993 if (!enable[dev]) { 1994 dev++; 1995 return -ENOENT; 1996 } 1997 1998 i = 0; 1999 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 2000 0, &card); 2001 if (err < 0) 2002 return err; 2003 2004 chip = NULL; /* Tells snd_echo_create to allocate chip */ 2005 if ((err = snd_echo_create(card, pci, &chip)) < 0) { 2006 snd_card_free(card); 2007 return err; 2008 } 2009 2010 strcpy(card->driver, "Echo_" ECHOCARD_NAME); 2011 strcpy(card->shortname, chip->card_name); 2012 2013 dsp = "56301"; 2014 if (pci_id->device == 0x3410) 2015 dsp = "56361"; 2016 2017 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i", 2018 card->shortname, pci_id->subdevice & 0x000f, dsp, 2019 chip->dsp_registers_phys, chip->irq); 2020 2021 if ((err = snd_echo_new_pcm(chip)) < 0) { 2022 dev_err(chip->card->dev, "new pcm error %d\n", err); 2023 snd_card_free(card); 2024 return err; 2025 } 2026 2027 #ifdef ECHOCARD_HAS_MIDI 2028 if (chip->has_midi) { /* Some Mia's do not have midi */ 2029 if ((err = snd_echo_midi_create(card, chip)) < 0) { 2030 dev_err(chip->card->dev, "new midi error %d\n", err); 2031 snd_card_free(card); 2032 return err; 2033 } 2034 } 2035 #endif 2036 2037 #ifdef ECHOCARD_HAS_VMIXER 2038 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip); 2039 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0) 2040 goto ctl_error; 2041 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN 2042 err = snd_ctl_add(chip->card, 2043 snd_ctl_new1(&snd_echo_line_output_gain, chip)); 2044 if (err < 0) 2045 goto ctl_error; 2046 #endif 2047 #else /* ECHOCARD_HAS_VMIXER */ 2048 err = snd_ctl_add(chip->card, 2049 snd_ctl_new1(&snd_echo_pcm_output_gain, chip)); 2050 if (err < 0) 2051 goto ctl_error; 2052 #endif /* ECHOCARD_HAS_VMIXER */ 2053 2054 #ifdef ECHOCARD_HAS_INPUT_GAIN 2055 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0) 2056 goto ctl_error; 2057 #endif 2058 2059 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL 2060 if (!chip->hasnt_input_nominal_level) 2061 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0) 2062 goto ctl_error; 2063 #endif 2064 2065 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL 2066 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0) 2067 goto ctl_error; 2068 #endif 2069 2070 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0) 2071 goto ctl_error; 2072 2073 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0) 2074 goto ctl_error; 2075 2076 #ifdef ECHOCARD_HAS_MONITOR 2077 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip); 2078 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0) 2079 goto ctl_error; 2080 #endif 2081 2082 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE 2083 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0) 2084 goto ctl_error; 2085 #endif 2086 2087 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0) 2088 goto ctl_error; 2089 2090 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH 2091 /* Creates a list of available digital modes */ 2092 chip->num_digital_modes = 0; 2093 for (i = 0; i < 6; i++) 2094 if (chip->digital_modes & (1 << i)) 2095 chip->digital_mode_list[chip->num_digital_modes++] = i; 2096 2097 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0) 2098 goto ctl_error; 2099 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */ 2100 2101 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK 2102 /* Creates a list of available clock sources */ 2103 chip->num_clock_sources = 0; 2104 for (i = 0; i < 10; i++) 2105 if (chip->input_clock_types & (1 << i)) 2106 chip->clock_source_list[chip->num_clock_sources++] = i; 2107 2108 if (chip->num_clock_sources > 1) { 2109 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip); 2110 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0) 2111 goto ctl_error; 2112 } 2113 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */ 2114 2115 #ifdef ECHOCARD_HAS_DIGITAL_IO 2116 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0) 2117 goto ctl_error; 2118 #endif 2119 2120 #ifdef ECHOCARD_HAS_PHANTOM_POWER 2121 if (chip->has_phantom_power) 2122 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0) 2123 goto ctl_error; 2124 #endif 2125 2126 err = snd_card_register(card); 2127 if (err < 0) 2128 goto ctl_error; 2129 dev_info(card->dev, "Card registered: %s\n", card->longname); 2130 2131 pci_set_drvdata(pci, chip); 2132 dev++; 2133 return 0; 2134 2135 ctl_error: 2136 dev_err(card->dev, "new control error %d\n", err); 2137 snd_card_free(card); 2138 return err; 2139 } 2140 2141 2142 2143 #if defined(CONFIG_PM_SLEEP) 2144 2145 static int snd_echo_suspend(struct device *dev) 2146 { 2147 struct echoaudio *chip = dev_get_drvdata(dev); 2148 2149 #ifdef ECHOCARD_HAS_MIDI 2150 /* This call can sleep */ 2151 if (chip->midi_out) 2152 snd_echo_midi_output_trigger(chip->midi_out, 0); 2153 #endif 2154 spin_lock_irq(&chip->lock); 2155 if (wait_handshake(chip)) { 2156 spin_unlock_irq(&chip->lock); 2157 return -EIO; 2158 } 2159 clear_handshake(chip); 2160 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) { 2161 spin_unlock_irq(&chip->lock); 2162 return -EIO; 2163 } 2164 spin_unlock_irq(&chip->lock); 2165 2166 chip->dsp_code = NULL; 2167 free_irq(chip->irq, chip); 2168 chip->irq = -1; 2169 return 0; 2170 } 2171 2172 2173 2174 static int snd_echo_resume(struct device *dev) 2175 { 2176 struct pci_dev *pci = to_pci_dev(dev); 2177 struct echoaudio *chip = dev_get_drvdata(dev); 2178 struct comm_page *commpage, *commpage_bak; 2179 u32 pipe_alloc_mask; 2180 int err; 2181 2182 commpage = chip->comm_page; 2183 commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL); 2184 if (commpage_bak == NULL) 2185 return -ENOMEM; 2186 2187 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device); 2188 if (err < 0) { 2189 kfree(commpage_bak); 2190 dev_err(dev, "resume init_hw err=%d\n", err); 2191 snd_echo_free(chip); 2192 return err; 2193 } 2194 2195 /* Temporarily set chip->pipe_alloc_mask=0 otherwise 2196 * restore_dsp_settings() fails. 2197 */ 2198 pipe_alloc_mask = chip->pipe_alloc_mask; 2199 chip->pipe_alloc_mask = 0; 2200 err = restore_dsp_rettings(chip); 2201 chip->pipe_alloc_mask = pipe_alloc_mask; 2202 if (err < 0) { 2203 kfree(commpage_bak); 2204 return err; 2205 } 2206 2207 memcpy(&commpage->audio_format, &commpage_bak->audio_format, 2208 sizeof(commpage->audio_format)); 2209 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr, 2210 sizeof(commpage->sglist_addr)); 2211 memcpy(&commpage->midi_output, &commpage_bak->midi_output, 2212 sizeof(commpage->midi_output)); 2213 kfree(commpage_bak); 2214 2215 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED, 2216 KBUILD_MODNAME, chip)) { 2217 dev_err(chip->card->dev, "cannot grab irq\n"); 2218 snd_echo_free(chip); 2219 return -EBUSY; 2220 } 2221 chip->irq = pci->irq; 2222 dev_dbg(dev, "resume irq=%d\n", chip->irq); 2223 2224 #ifdef ECHOCARD_HAS_MIDI 2225 if (chip->midi_input_enabled) 2226 enable_midi_input(chip, true); 2227 if (chip->midi_out) 2228 snd_echo_midi_output_trigger(chip->midi_out, 1); 2229 #endif 2230 2231 return 0; 2232 } 2233 2234 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume); 2235 #define SND_ECHO_PM_OPS &snd_echo_pm 2236 #else 2237 #define SND_ECHO_PM_OPS NULL 2238 #endif /* CONFIG_PM_SLEEP */ 2239 2240 2241 static void snd_echo_remove(struct pci_dev *pci) 2242 { 2243 struct echoaudio *chip; 2244 2245 chip = pci_get_drvdata(pci); 2246 if (chip) 2247 snd_card_free(chip->card); 2248 } 2249 2250 2251 2252 /****************************************************************************** 2253 Everything starts and ends here 2254 ******************************************************************************/ 2255 2256 /* pci_driver definition */ 2257 static struct pci_driver echo_driver = { 2258 .name = KBUILD_MODNAME, 2259 .id_table = snd_echo_ids, 2260 .probe = snd_echo_probe, 2261 .remove = snd_echo_remove, 2262 .driver = { 2263 .pm = SND_ECHO_PM_OPS, 2264 }, 2265 }; 2266 2267 module_pci_driver(echo_driver); 2268