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