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