Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  ALSA driver for Echoaudio soundcards.
0004  *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
0005  *  Copyright (C) 2020 Mark Hills <mark@xwax.org>
0006  */
0007 
0008 #include <linux/module.h>
0009 
0010 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
0011 MODULE_LICENSE("GPL v2");
0012 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
0013 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
0014 
0015 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0016 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0017 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0018 
0019 module_param_array(index, int, NULL, 0444);
0020 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
0021 module_param_array(id, charp, NULL, 0444);
0022 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
0023 module_param_array(enable, bool, NULL, 0444);
0024 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
0025 
0026 static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
0027 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
0028 
0029 
0030 
0031 static int get_firmware(const struct firmware **fw_entry,
0032             struct echoaudio *chip, const short fw_index)
0033 {
0034     int err;
0035     char name[30];
0036 
0037 #ifdef CONFIG_PM_SLEEP
0038     if (chip->fw_cache[fw_index]) {
0039         dev_dbg(chip->card->dev,
0040             "firmware requested: %s is cached\n",
0041             card_fw[fw_index].data);
0042         *fw_entry = chip->fw_cache[fw_index];
0043         return 0;
0044     }
0045 #endif
0046 
0047     dev_dbg(chip->card->dev,
0048         "firmware requested: %s\n", card_fw[fw_index].data);
0049     snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
0050     err = request_firmware(fw_entry, name, &chip->pci->dev);
0051     if (err < 0)
0052         dev_err(chip->card->dev,
0053             "get_firmware(): Firmware not available (%d)\n", err);
0054 #ifdef CONFIG_PM_SLEEP
0055     else
0056         chip->fw_cache[fw_index] = *fw_entry;
0057 #endif
0058     return err;
0059 }
0060 
0061 
0062 
0063 static void free_firmware(const struct firmware *fw_entry,
0064               struct echoaudio *chip)
0065 {
0066 #ifdef CONFIG_PM_SLEEP
0067     dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
0068 #else
0069     release_firmware(fw_entry);
0070 #endif
0071 }
0072 
0073 
0074 
0075 static void free_firmware_cache(struct echoaudio *chip)
0076 {
0077 #ifdef CONFIG_PM_SLEEP
0078     int i;
0079 
0080     for (i = 0; i < 8 ; i++)
0081         if (chip->fw_cache[i]) {
0082             release_firmware(chip->fw_cache[i]);
0083             dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
0084         }
0085 
0086 #endif
0087 }
0088 
0089 
0090 
0091 /******************************************************************************
0092     PCM interface
0093 ******************************************************************************/
0094 
0095 static void audiopipe_free(struct snd_pcm_runtime *runtime)
0096 {
0097     struct audiopipe *pipe = runtime->private_data;
0098 
0099     if (pipe->sgpage.area)
0100         snd_dma_free_pages(&pipe->sgpage);
0101     kfree(pipe);
0102 }
0103 
0104 
0105 
0106 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
0107                           struct snd_pcm_hw_rule *rule)
0108 {
0109     struct snd_interval *c = hw_param_interval(params,
0110                            SNDRV_PCM_HW_PARAM_CHANNELS);
0111     struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0112     struct snd_mask fmt;
0113 
0114     snd_mask_any(&fmt);
0115 
0116 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
0117     /* >=2 channels cannot be S32_BE */
0118     if (c->min == 2) {
0119         fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
0120         return snd_mask_refine(f, &fmt);
0121     }
0122 #endif
0123     /* > 2 channels cannot be U8 and S32_BE */
0124     if (c->min > 2) {
0125         fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
0126         return snd_mask_refine(f, &fmt);
0127     }
0128     /* Mono is ok with any format */
0129     return 0;
0130 }
0131 
0132 
0133 
0134 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
0135                           struct snd_pcm_hw_rule *rule)
0136 {
0137     struct snd_interval *c = hw_param_interval(params,
0138                            SNDRV_PCM_HW_PARAM_CHANNELS);
0139     struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0140     struct snd_interval ch;
0141 
0142     snd_interval_any(&ch);
0143 
0144     /* S32_BE is mono (and stereo) only */
0145     if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
0146         ch.min = 1;
0147 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
0148         ch.max = 2;
0149 #else
0150         ch.max = 1;
0151 #endif
0152         ch.integer = 1;
0153         return snd_interval_refine(c, &ch);
0154     }
0155     /* U8 can be only mono or stereo */
0156     if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
0157         ch.min = 1;
0158         ch.max = 2;
0159         ch.integer = 1;
0160         return snd_interval_refine(c, &ch);
0161     }
0162     /* S16_LE, S24_3LE and S32_LE support any number of channels. */
0163     return 0;
0164 }
0165 
0166 
0167 
0168 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
0169                            struct snd_pcm_hw_rule *rule)
0170 {
0171     struct snd_interval *c = hw_param_interval(params,
0172                            SNDRV_PCM_HW_PARAM_CHANNELS);
0173     struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0174     struct snd_mask fmt;
0175     u64 fmask;
0176     snd_mask_any(&fmt);
0177 
0178     fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
0179 
0180     /* >2 channels must be S16_LE, S24_3LE or S32_LE */
0181     if (c->min > 2) {
0182         fmask &= SNDRV_PCM_FMTBIT_S16_LE |
0183              SNDRV_PCM_FMTBIT_S24_3LE |
0184              SNDRV_PCM_FMTBIT_S32_LE;
0185     /* 1 channel must be S32_BE or S32_LE */
0186     } else if (c->max == 1)
0187         fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
0188 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
0189     /* 2 channels cannot be S32_BE */
0190     else if (c->min == 2 && c->max == 2)
0191         fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
0192 #endif
0193     else
0194         return 0;
0195 
0196     fmt.bits[0] &= (u32)fmask;
0197     fmt.bits[1] &= (u32)(fmask >> 32);
0198     return snd_mask_refine(f, &fmt);
0199 }
0200 
0201 
0202 
0203 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
0204                            struct snd_pcm_hw_rule *rule)
0205 {
0206     struct snd_interval *c = hw_param_interval(params,
0207                            SNDRV_PCM_HW_PARAM_CHANNELS);
0208     struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0209     struct snd_interval ch;
0210     u64 fmask;
0211 
0212     snd_interval_any(&ch);
0213     ch.integer = 1;
0214     fmask = f->bits[0] + ((u64)f->bits[1] << 32);
0215 
0216     /* S32_BE is mono (and stereo) only */
0217     if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
0218         ch.min = 1;
0219 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
0220         ch.max = 2;
0221 #else
0222         ch.max = 1;
0223 #endif
0224     /* U8 is stereo only */
0225     } else if (fmask == SNDRV_PCM_FMTBIT_U8)
0226         ch.min = ch.max = 2;
0227     /* S16_LE and S24_3LE must be at least stereo */
0228     else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
0229                    SNDRV_PCM_FMTBIT_S24_3LE)))
0230         ch.min = 2;
0231     else
0232         return 0;
0233 
0234     return snd_interval_refine(c, &ch);
0235 }
0236 
0237 
0238 
0239 /* Since the sample rate is a global setting, do allow the user to change the
0240 sample rate only if there is only one pcm device open. */
0241 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
0242                    struct snd_pcm_hw_rule *rule)
0243 {
0244     struct snd_interval *rate = hw_param_interval(params,
0245                               SNDRV_PCM_HW_PARAM_RATE);
0246     struct echoaudio *chip = rule->private;
0247     struct snd_interval fixed;
0248     int err;
0249 
0250     mutex_lock(&chip->mode_mutex);
0251 
0252     if (chip->can_set_rate) {
0253         err = 0;
0254     } else {
0255         snd_interval_any(&fixed);
0256         fixed.min = fixed.max = chip->sample_rate;
0257         err = snd_interval_refine(rate, &fixed);
0258     }
0259 
0260     mutex_unlock(&chip->mode_mutex);
0261     return err;
0262 }
0263 
0264 
0265 static int pcm_open(struct snd_pcm_substream *substream,
0266             signed char max_channels)
0267 {
0268     struct echoaudio *chip;
0269     struct snd_pcm_runtime *runtime;
0270     struct audiopipe *pipe;
0271     int err, i;
0272 
0273     if (max_channels <= 0)
0274         return -EAGAIN;
0275 
0276     chip = snd_pcm_substream_chip(substream);
0277     runtime = substream->runtime;
0278 
0279     pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
0280     if (!pipe)
0281         return -ENOMEM;
0282     pipe->index = -1;       /* Not configured yet */
0283 
0284     /* Set up hw capabilities and contraints */
0285     memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
0286     dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
0287     pipe->constr.list = channels_list;
0288     pipe->constr.mask = 0;
0289     for (i = 0; channels_list[i] <= max_channels; i++);
0290     pipe->constr.count = i;
0291     if (pipe->hw.channels_max > max_channels)
0292         pipe->hw.channels_max = max_channels;
0293     if (chip->digital_mode == DIGITAL_MODE_ADAT) {
0294         pipe->hw.rate_max = 48000;
0295         pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
0296     }
0297 
0298     runtime->hw = pipe->hw;
0299     runtime->private_data = pipe;
0300     runtime->private_free = audiopipe_free;
0301     snd_pcm_set_sync(substream);
0302 
0303     /* Only mono and any even number of channels are allowed */
0304     err = snd_pcm_hw_constraint_list(runtime, 0,
0305                      SNDRV_PCM_HW_PARAM_CHANNELS,
0306                      &pipe->constr);
0307     if (err < 0)
0308         return err;
0309 
0310     /* All periods should have the same size */
0311     err = snd_pcm_hw_constraint_integer(runtime,
0312                         SNDRV_PCM_HW_PARAM_PERIODS);
0313     if (err < 0)
0314         return err;
0315 
0316     /* The hw accesses memory in chunks 32 frames long and they should be
0317     32-bytes-aligned. It's not a requirement, but it seems that IRQs are
0318     generated with a resolution of 32 frames. Thus we need the following */
0319     err = snd_pcm_hw_constraint_step(runtime, 0,
0320                      SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
0321     if (err < 0)
0322         return err;
0323     err = snd_pcm_hw_constraint_step(runtime, 0,
0324                      SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
0325     if (err < 0)
0326         return err;
0327 
0328     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0329                   SNDRV_PCM_HW_PARAM_RATE,
0330                   hw_rule_sample_rate, chip,
0331                   SNDRV_PCM_HW_PARAM_RATE, -1);
0332     if (err < 0)
0333         return err;
0334 
0335     /* Allocate a page for the scatter-gather list */
0336     err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
0337                   &chip->pci->dev,
0338                   PAGE_SIZE, &pipe->sgpage);
0339     if (err < 0) {
0340         dev_err(chip->card->dev, "s-g list allocation failed\n");
0341         return err;
0342     }
0343 
0344     /*
0345      * Sole ownership required to set the rate
0346      */
0347 
0348     dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
0349         chip->opencount, chip->can_set_rate, chip->rate_set);
0350 
0351     chip->opencount++;
0352     if (chip->opencount > 1 && chip->rate_set)
0353         chip->can_set_rate = 0;
0354 
0355     return 0;
0356 }
0357 
0358 
0359 
0360 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
0361 {
0362     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0363     int err;
0364 
0365     err = pcm_open(substream,
0366                num_analog_busses_in(chip) - substream->number);
0367     if (err < 0)
0368         return err;
0369     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0370                   SNDRV_PCM_HW_PARAM_CHANNELS,
0371                   hw_rule_capture_channels_by_format, NULL,
0372                   SNDRV_PCM_HW_PARAM_FORMAT, -1);
0373     if (err < 0)
0374         return err;
0375     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0376                   SNDRV_PCM_HW_PARAM_FORMAT,
0377                   hw_rule_capture_format_by_channels, NULL,
0378                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
0379     if (err < 0)
0380         return err;
0381 
0382     return 0;
0383 }
0384 
0385 
0386 
0387 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
0388 {
0389     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0390     int max_channels, err;
0391 
0392 #ifdef ECHOCARD_HAS_VMIXER
0393     max_channels = num_pipes_out(chip);
0394 #else
0395     max_channels = num_analog_busses_out(chip);
0396 #endif
0397     err = pcm_open(substream, max_channels - substream->number);
0398     if (err < 0)
0399         return err;
0400     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0401                   SNDRV_PCM_HW_PARAM_CHANNELS,
0402                   hw_rule_playback_channels_by_format,
0403                   NULL,
0404                   SNDRV_PCM_HW_PARAM_FORMAT, -1);
0405     if (err < 0)
0406         return err;
0407     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0408                   SNDRV_PCM_HW_PARAM_FORMAT,
0409                   hw_rule_playback_format_by_channels,
0410                   NULL,
0411                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
0412     if (err < 0)
0413         return err;
0414 
0415     return 0;
0416 }
0417 
0418 
0419 
0420 #ifdef ECHOCARD_HAS_DIGITAL_IO
0421 
0422 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
0423 {
0424     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0425     int err, max_channels;
0426 
0427     max_channels = num_digital_busses_in(chip) - substream->number;
0428     mutex_lock(&chip->mode_mutex);
0429     if (chip->digital_mode == DIGITAL_MODE_ADAT)
0430         err = pcm_open(substream, max_channels);
0431     else    /* If the card has ADAT, subtract the 6 channels
0432          * that S/PDIF doesn't have
0433          */
0434         err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
0435 
0436     if (err < 0)
0437         goto din_exit;
0438 
0439     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0440                   SNDRV_PCM_HW_PARAM_CHANNELS,
0441                   hw_rule_capture_channels_by_format, NULL,
0442                   SNDRV_PCM_HW_PARAM_FORMAT, -1);
0443     if (err < 0)
0444         goto din_exit;
0445     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0446                   SNDRV_PCM_HW_PARAM_FORMAT,
0447                   hw_rule_capture_format_by_channels, NULL,
0448                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
0449     if (err < 0)
0450         goto din_exit;
0451 
0452 din_exit:
0453     mutex_unlock(&chip->mode_mutex);
0454     return err;
0455 }
0456 
0457 
0458 
0459 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
0460 
0461 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
0462 {
0463     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0464     int err, max_channels;
0465 
0466     max_channels = num_digital_busses_out(chip) - substream->number;
0467     mutex_lock(&chip->mode_mutex);
0468     if (chip->digital_mode == DIGITAL_MODE_ADAT)
0469         err = pcm_open(substream, max_channels);
0470     else    /* If the card has ADAT, subtract the 6 channels
0471          * that S/PDIF doesn't have
0472          */
0473         err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
0474 
0475     if (err < 0)
0476         goto dout_exit;
0477 
0478     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0479                   SNDRV_PCM_HW_PARAM_CHANNELS,
0480                   hw_rule_playback_channels_by_format,
0481                   NULL, SNDRV_PCM_HW_PARAM_FORMAT,
0482                   -1);
0483     if (err < 0)
0484         goto dout_exit;
0485     err = snd_pcm_hw_rule_add(substream->runtime, 0,
0486                   SNDRV_PCM_HW_PARAM_FORMAT,
0487                   hw_rule_playback_format_by_channels,
0488                   NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
0489                   -1);
0490     if (err < 0)
0491         goto dout_exit;
0492 
0493 dout_exit:
0494     mutex_unlock(&chip->mode_mutex);
0495     return err;
0496 }
0497 
0498 #endif /* !ECHOCARD_HAS_VMIXER */
0499 
0500 #endif /* ECHOCARD_HAS_DIGITAL_IO */
0501 
0502 
0503 
0504 static int pcm_close(struct snd_pcm_substream *substream)
0505 {
0506     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0507 
0508     /* Nothing to do here. Audio is already off and pipe will be
0509      * freed by its callback
0510      */
0511 
0512     mutex_lock(&chip->mode_mutex);
0513 
0514     dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
0515         chip->opencount, chip->can_set_rate, chip->rate_set);
0516 
0517     chip->opencount--;
0518 
0519     switch (chip->opencount) {
0520     case 1:
0521         chip->can_set_rate = 1;
0522         break;
0523 
0524     case 0:
0525         chip->rate_set = 0;
0526         break;
0527     }
0528 
0529     mutex_unlock(&chip->mode_mutex);
0530     return 0;
0531 }
0532 
0533 
0534 
0535 /* Channel allocation and scatter-gather list setup */
0536 static int init_engine(struct snd_pcm_substream *substream,
0537                struct snd_pcm_hw_params *hw_params,
0538                int pipe_index, int interleave)
0539 {
0540     struct echoaudio *chip;
0541     int err, per, rest, page, edge, offs;
0542     struct audiopipe *pipe;
0543 
0544     chip = snd_pcm_substream_chip(substream);
0545     pipe = (struct audiopipe *) substream->runtime->private_data;
0546 
0547     /* Sets up che hardware. If it's already initialized, reset and
0548      * redo with the new parameters
0549      */
0550     spin_lock_irq(&chip->lock);
0551     if (pipe->index >= 0) {
0552         dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
0553         err = free_pipes(chip, pipe);
0554         snd_BUG_ON(err);
0555         chip->substream[pipe->index] = NULL;
0556     }
0557 
0558     err = allocate_pipes(chip, pipe, pipe_index, interleave);
0559     if (err < 0) {
0560         spin_unlock_irq(&chip->lock);
0561         dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
0562             pipe_index, err);
0563         return err;
0564     }
0565     spin_unlock_irq(&chip->lock);
0566     dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
0567 
0568     dev_dbg(chip->card->dev,
0569         "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
0570         params_buffer_bytes(hw_params), params_periods(hw_params),
0571         params_period_bytes(hw_params));
0572 
0573     sglist_init(chip, pipe);
0574     edge = PAGE_SIZE;
0575     for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
0576          per++) {
0577         rest = params_period_bytes(hw_params);
0578         if (offs + rest > params_buffer_bytes(hw_params))
0579             rest = params_buffer_bytes(hw_params) - offs;
0580         while (rest) {
0581             dma_addr_t addr;
0582             addr = snd_pcm_sgbuf_get_addr(substream, offs);
0583             if (rest <= edge - offs) {
0584                 sglist_add_mapping(chip, pipe, addr, rest);
0585                 sglist_add_irq(chip, pipe);
0586                 offs += rest;
0587                 rest = 0;
0588             } else {
0589                 sglist_add_mapping(chip, pipe, addr,
0590                            edge - offs);
0591                 rest -= edge - offs;
0592                 offs = edge;
0593             }
0594             if (offs == edge) {
0595                 edge += PAGE_SIZE;
0596                 page++;
0597             }
0598         }
0599     }
0600 
0601     /* Close the ring buffer */
0602     sglist_wrap(chip, pipe);
0603 
0604     /* This stuff is used by the irq handler, so it must be
0605      * initialized before chip->substream
0606      */
0607     pipe->last_period = 0;
0608     pipe->last_counter = 0;
0609     pipe->position = 0;
0610     smp_wmb();
0611     chip->substream[pipe_index] = substream;
0612     chip->rate_set = 1;
0613     spin_lock_irq(&chip->lock);
0614     set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
0615     spin_unlock_irq(&chip->lock);
0616     return 0;
0617 }
0618 
0619 
0620 
0621 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
0622                    struct snd_pcm_hw_params *hw_params)
0623 {
0624     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0625 
0626     return init_engine(substream, hw_params, px_analog_in(chip) +
0627             substream->number, params_channels(hw_params));
0628 }
0629 
0630 
0631 
0632 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
0633                     struct snd_pcm_hw_params *hw_params)
0634 {
0635     return init_engine(substream, hw_params, substream->number,
0636                params_channels(hw_params));
0637 }
0638 
0639 
0640 
0641 #ifdef ECHOCARD_HAS_DIGITAL_IO
0642 
0643 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
0644                     struct snd_pcm_hw_params *hw_params)
0645 {
0646     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0647 
0648     return init_engine(substream, hw_params, px_digital_in(chip) +
0649             substream->number, params_channels(hw_params));
0650 }
0651 
0652 
0653 
0654 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
0655 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
0656                      struct snd_pcm_hw_params *hw_params)
0657 {
0658     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0659 
0660     return init_engine(substream, hw_params, px_digital_out(chip) +
0661             substream->number, params_channels(hw_params));
0662 }
0663 #endif /* !ECHOCARD_HAS_VMIXER */
0664 
0665 #endif /* ECHOCARD_HAS_DIGITAL_IO */
0666 
0667 
0668 
0669 static int pcm_hw_free(struct snd_pcm_substream *substream)
0670 {
0671     struct echoaudio *chip;
0672     struct audiopipe *pipe;
0673 
0674     chip = snd_pcm_substream_chip(substream);
0675     pipe = (struct audiopipe *) substream->runtime->private_data;
0676 
0677     spin_lock_irq(&chip->lock);
0678     if (pipe->index >= 0) {
0679         dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
0680         free_pipes(chip, pipe);
0681         chip->substream[pipe->index] = NULL;
0682         pipe->index = -1;
0683     }
0684     spin_unlock_irq(&chip->lock);
0685 
0686     return 0;
0687 }
0688 
0689 
0690 
0691 static int pcm_prepare(struct snd_pcm_substream *substream)
0692 {
0693     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0694     struct snd_pcm_runtime *runtime = substream->runtime;
0695     struct audioformat format;
0696     int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
0697 
0698     dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
0699         runtime->rate, runtime->format, runtime->channels);
0700     format.interleave = runtime->channels;
0701     format.data_are_bigendian = 0;
0702     format.mono_to_stereo = 0;
0703     switch (runtime->format) {
0704     case SNDRV_PCM_FORMAT_U8:
0705         format.bits_per_sample = 8;
0706         break;
0707     case SNDRV_PCM_FORMAT_S16_LE:
0708         format.bits_per_sample = 16;
0709         break;
0710     case SNDRV_PCM_FORMAT_S24_3LE:
0711         format.bits_per_sample = 24;
0712         break;
0713     case SNDRV_PCM_FORMAT_S32_BE:
0714         format.data_are_bigendian = 1;
0715         fallthrough;
0716     case SNDRV_PCM_FORMAT_S32_LE:
0717         format.bits_per_sample = 32;
0718         break;
0719     default:
0720         dev_err(chip->card->dev,
0721             "Prepare error: unsupported format %d\n",
0722             runtime->format);
0723         return -EINVAL;
0724     }
0725 
0726     if (snd_BUG_ON(pipe_index >= px_num(chip)))
0727         return -EINVAL;
0728 
0729     /*
0730      * We passed checks we can do independently; now take
0731      * exclusive control
0732      */
0733 
0734     spin_lock_irq(&chip->lock);
0735 
0736     if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
0737         spin_unlock_irq(&chip->lock);
0738         return -EINVAL;
0739     }
0740 
0741     set_audio_format(chip, pipe_index, &format);
0742     spin_unlock_irq(&chip->lock);
0743 
0744     return 0;
0745 }
0746 
0747 
0748 
0749 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0750 {
0751     struct echoaudio *chip = snd_pcm_substream_chip(substream);
0752     struct audiopipe *pipe;
0753     int i, err;
0754     u32 channelmask = 0;
0755     struct snd_pcm_substream *s;
0756 
0757     snd_pcm_group_for_each_entry(s, substream) {
0758         for (i = 0; i < DSP_MAXPIPES; i++) {
0759             if (s == chip->substream[i]) {
0760                 channelmask |= 1 << i;
0761                 snd_pcm_trigger_done(s, substream);
0762             }
0763         }
0764     }
0765 
0766     spin_lock(&chip->lock);
0767     switch (cmd) {
0768     case SNDRV_PCM_TRIGGER_RESUME:
0769     case SNDRV_PCM_TRIGGER_START:
0770     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0771         for (i = 0; i < DSP_MAXPIPES; i++) {
0772             if (channelmask & (1 << i)) {
0773                 pipe = chip->substream[i]->runtime->private_data;
0774                 switch (pipe->state) {
0775                 case PIPE_STATE_STOPPED:
0776                     pipe->last_period = 0;
0777                     pipe->last_counter = 0;
0778                     pipe->position = 0;
0779                     *pipe->dma_counter = 0;
0780                     fallthrough;
0781                 case PIPE_STATE_PAUSED:
0782                     pipe->state = PIPE_STATE_STARTED;
0783                     break;
0784                 case PIPE_STATE_STARTED:
0785                     break;
0786                 }
0787             }
0788         }
0789         err = start_transport(chip, channelmask,
0790                       chip->pipe_cyclic_mask);
0791         break;
0792     case SNDRV_PCM_TRIGGER_SUSPEND:
0793     case SNDRV_PCM_TRIGGER_STOP:
0794         for (i = 0; i < DSP_MAXPIPES; i++) {
0795             if (channelmask & (1 << i)) {
0796                 pipe = chip->substream[i]->runtime->private_data;
0797                 pipe->state = PIPE_STATE_STOPPED;
0798             }
0799         }
0800         err = stop_transport(chip, channelmask);
0801         break;
0802     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0803         for (i = 0; i < DSP_MAXPIPES; i++) {
0804             if (channelmask & (1 << i)) {
0805                 pipe = chip->substream[i]->runtime->private_data;
0806                 pipe->state = PIPE_STATE_PAUSED;
0807             }
0808         }
0809         err = pause_transport(chip, channelmask);
0810         break;
0811     default:
0812         err = -EINVAL;
0813     }
0814     spin_unlock(&chip->lock);
0815     return err;
0816 }
0817 
0818 
0819 
0820 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
0821 {
0822     struct snd_pcm_runtime *runtime = substream->runtime;
0823     struct audiopipe *pipe = runtime->private_data;
0824     u32 counter, step;
0825 
0826     /*
0827      * IRQ handling runs concurrently. Do not share tracking of
0828      * counter with it, which would race or require locking
0829      */
0830 
0831     counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
0832 
0833     step = counter - pipe->last_counter;  /* handles wrapping */
0834     pipe->last_counter = counter;
0835 
0836     /* counter doesn't neccessarily wrap on a multiple of
0837      * buffer_size, so can't derive the position; must
0838      * accumulate */
0839 
0840     pipe->position += step;
0841     pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
0842 
0843     return bytes_to_frames(runtime, pipe->position);
0844 }
0845 
0846 
0847 
0848 /* pcm *_ops structures */
0849 static const struct snd_pcm_ops analog_playback_ops = {
0850     .open = pcm_analog_out_open,
0851     .close = pcm_close,
0852     .hw_params = pcm_analog_out_hw_params,
0853     .hw_free = pcm_hw_free,
0854     .prepare = pcm_prepare,
0855     .trigger = pcm_trigger,
0856     .pointer = pcm_pointer,
0857 };
0858 static const struct snd_pcm_ops analog_capture_ops = {
0859     .open = pcm_analog_in_open,
0860     .close = pcm_close,
0861     .hw_params = pcm_analog_in_hw_params,
0862     .hw_free = pcm_hw_free,
0863     .prepare = pcm_prepare,
0864     .trigger = pcm_trigger,
0865     .pointer = pcm_pointer,
0866 };
0867 #ifdef ECHOCARD_HAS_DIGITAL_IO
0868 #ifndef ECHOCARD_HAS_VMIXER
0869 static const struct snd_pcm_ops digital_playback_ops = {
0870     .open = pcm_digital_out_open,
0871     .close = pcm_close,
0872     .hw_params = pcm_digital_out_hw_params,
0873     .hw_free = pcm_hw_free,
0874     .prepare = pcm_prepare,
0875     .trigger = pcm_trigger,
0876     .pointer = pcm_pointer,
0877 };
0878 #endif /* !ECHOCARD_HAS_VMIXER */
0879 static const struct snd_pcm_ops digital_capture_ops = {
0880     .open = pcm_digital_in_open,
0881     .close = pcm_close,
0882     .hw_params = pcm_digital_in_hw_params,
0883     .hw_free = pcm_hw_free,
0884     .prepare = pcm_prepare,
0885     .trigger = pcm_trigger,
0886     .pointer = pcm_pointer,
0887 };
0888 #endif /* ECHOCARD_HAS_DIGITAL_IO */
0889 
0890 
0891 
0892 /* Preallocate memory only for the first substream because it's the most
0893  * used one
0894  */
0895 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
0896 {
0897     struct snd_pcm_substream *ss;
0898     int stream;
0899 
0900     for (stream = 0; stream < 2; stream++)
0901         for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
0902             snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
0903                            dev,
0904                            ss->number ? 0 : 128<<10,
0905                            256<<10);
0906 }
0907 
0908 
0909 
0910 /*<--snd_echo_probe() */
0911 static int snd_echo_new_pcm(struct echoaudio *chip)
0912 {
0913     struct snd_pcm *pcm;
0914     int err;
0915 
0916 #ifdef ECHOCARD_HAS_VMIXER
0917     /* This card has a Vmixer, that is there is no direct mapping from PCM
0918     streams to physical outputs. The user can mix the streams as he wishes
0919     via control interface and it's possible to send any stream to any
0920     output, thus it makes no sense to keep analog and digital outputs
0921     separated */
0922 
0923     /* PCM#0 Virtual outputs and analog inputs */
0924     err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
0925               num_analog_busses_in(chip), &pcm);
0926     if (err < 0)
0927         return err;
0928     pcm->private_data = chip;
0929     chip->analog_pcm = pcm;
0930     strcpy(pcm->name, chip->card->shortname);
0931     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
0932     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
0933     snd_echo_preallocate_pages(pcm, &chip->pci->dev);
0934 
0935 #ifdef ECHOCARD_HAS_DIGITAL_IO
0936     /* PCM#1 Digital inputs, no outputs */
0937     err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
0938               num_digital_busses_in(chip), &pcm);
0939     if (err < 0)
0940         return err;
0941     pcm->private_data = chip;
0942     chip->digital_pcm = pcm;
0943     strcpy(pcm->name, chip->card->shortname);
0944     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
0945     snd_echo_preallocate_pages(pcm, &chip->pci->dev);
0946 #endif /* ECHOCARD_HAS_DIGITAL_IO */
0947 
0948 #else /* ECHOCARD_HAS_VMIXER */
0949 
0950     /* The card can manage substreams formed by analog and digital channels
0951     at the same time, but I prefer to keep analog and digital channels
0952     separated, because that mixed thing is confusing and useless. So we
0953     register two PCM devices: */
0954 
0955     /* PCM#0 Analog i/o */
0956     err = snd_pcm_new(chip->card, "Analog PCM", 0,
0957               num_analog_busses_out(chip),
0958               num_analog_busses_in(chip), &pcm);
0959     if (err < 0)
0960         return err;
0961     pcm->private_data = chip;
0962     chip->analog_pcm = pcm;
0963     strcpy(pcm->name, chip->card->shortname);
0964     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
0965     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
0966     snd_echo_preallocate_pages(pcm, &chip->pci->dev);
0967 
0968 #ifdef ECHOCARD_HAS_DIGITAL_IO
0969     /* PCM#1 Digital i/o */
0970     err = snd_pcm_new(chip->card, "Digital PCM", 1,
0971               num_digital_busses_out(chip),
0972               num_digital_busses_in(chip), &pcm);
0973     if (err < 0)
0974         return err;
0975     pcm->private_data = chip;
0976     chip->digital_pcm = pcm;
0977     strcpy(pcm->name, chip->card->shortname);
0978     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
0979     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
0980     snd_echo_preallocate_pages(pcm, &chip->pci->dev);
0981 #endif /* ECHOCARD_HAS_DIGITAL_IO */
0982 
0983 #endif /* ECHOCARD_HAS_VMIXER */
0984 
0985     return 0;
0986 }
0987 
0988 
0989 
0990 
0991 /******************************************************************************
0992     Control interface
0993 ******************************************************************************/
0994 
0995 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
0996 
0997 /******************* PCM output volume *******************/
0998 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
0999                      struct snd_ctl_elem_info *uinfo)
1000 {
1001     struct echoaudio *chip;
1002 
1003     chip = snd_kcontrol_chip(kcontrol);
1004     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1005     uinfo->count = num_busses_out(chip);
1006     uinfo->value.integer.min = ECHOGAIN_MINOUT;
1007     uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1008     return 0;
1009 }
1010 
1011 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1012                     struct snd_ctl_elem_value *ucontrol)
1013 {
1014     struct echoaudio *chip;
1015     int c;
1016 
1017     chip = snd_kcontrol_chip(kcontrol);
1018     for (c = 0; c < num_busses_out(chip); c++)
1019         ucontrol->value.integer.value[c] = chip->output_gain[c];
1020     return 0;
1021 }
1022 
1023 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1024                     struct snd_ctl_elem_value *ucontrol)
1025 {
1026     struct echoaudio *chip;
1027     int c, changed, gain;
1028 
1029     changed = 0;
1030     chip = snd_kcontrol_chip(kcontrol);
1031     spin_lock_irq(&chip->lock);
1032     for (c = 0; c < num_busses_out(chip); c++) {
1033         gain = ucontrol->value.integer.value[c];
1034         /* Ignore out of range values */
1035         if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1036             continue;
1037         if (chip->output_gain[c] != gain) {
1038             set_output_gain(chip, c, gain);
1039             changed = 1;
1040         }
1041     }
1042     if (changed)
1043         update_output_line_level(chip);
1044     spin_unlock_irq(&chip->lock);
1045     return changed;
1046 }
1047 
1048 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1049 /* On the Mia this one controls the line-out volume */
1050 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1051     .name = "Line Playback Volume",
1052     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1053     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1054           SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1055     .info = snd_echo_output_gain_info,
1056     .get = snd_echo_output_gain_get,
1057     .put = snd_echo_output_gain_put,
1058     .tlv = {.p = db_scale_output_gain},
1059 };
1060 #else
1061 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1062     .name = "PCM Playback Volume",
1063     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1064     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1065     .info = snd_echo_output_gain_info,
1066     .get = snd_echo_output_gain_get,
1067     .put = snd_echo_output_gain_put,
1068     .tlv = {.p = db_scale_output_gain},
1069 };
1070 #endif
1071 
1072 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1073 
1074 
1075 
1076 #ifdef ECHOCARD_HAS_INPUT_GAIN
1077 
1078 /******************* Analog input volume *******************/
1079 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1080                     struct snd_ctl_elem_info *uinfo)
1081 {
1082     struct echoaudio *chip;
1083 
1084     chip = snd_kcontrol_chip(kcontrol);
1085     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1086     uinfo->count = num_analog_busses_in(chip);
1087     uinfo->value.integer.min = ECHOGAIN_MININP;
1088     uinfo->value.integer.max = ECHOGAIN_MAXINP;
1089     return 0;
1090 }
1091 
1092 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1093                    struct snd_ctl_elem_value *ucontrol)
1094 {
1095     struct echoaudio *chip;
1096     int c;
1097 
1098     chip = snd_kcontrol_chip(kcontrol);
1099     for (c = 0; c < num_analog_busses_in(chip); c++)
1100         ucontrol->value.integer.value[c] = chip->input_gain[c];
1101     return 0;
1102 }
1103 
1104 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1105                    struct snd_ctl_elem_value *ucontrol)
1106 {
1107     struct echoaudio *chip;
1108     int c, gain, changed;
1109 
1110     changed = 0;
1111     chip = snd_kcontrol_chip(kcontrol);
1112     spin_lock_irq(&chip->lock);
1113     for (c = 0; c < num_analog_busses_in(chip); c++) {
1114         gain = ucontrol->value.integer.value[c];
1115         /* Ignore out of range values */
1116         if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1117             continue;
1118         if (chip->input_gain[c] != gain) {
1119             set_input_gain(chip, c, gain);
1120             changed = 1;
1121         }
1122     }
1123     if (changed)
1124         update_input_line_level(chip);
1125     spin_unlock_irq(&chip->lock);
1126     return changed;
1127 }
1128 
1129 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1130 
1131 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1132     .name = "Line Capture Volume",
1133     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1135     .info = snd_echo_input_gain_info,
1136     .get = snd_echo_input_gain_get,
1137     .put = snd_echo_input_gain_put,
1138     .tlv = {.p = db_scale_input_gain},
1139 };
1140 
1141 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1142 
1143 
1144 
1145 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1146 
1147 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1148 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1149                      struct snd_ctl_elem_info *uinfo)
1150 {
1151     struct echoaudio *chip;
1152 
1153     chip = snd_kcontrol_chip(kcontrol);
1154     uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1155     uinfo->count = num_analog_busses_out(chip);
1156     uinfo->value.integer.min = 0;
1157     uinfo->value.integer.max = 1;
1158     return 0;
1159 }
1160 
1161 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1162                        struct snd_ctl_elem_value *ucontrol)
1163 {
1164     struct echoaudio *chip;
1165     int c;
1166 
1167     chip = snd_kcontrol_chip(kcontrol);
1168     for (c = 0; c < num_analog_busses_out(chip); c++)
1169         ucontrol->value.integer.value[c] = chip->nominal_level[c];
1170     return 0;
1171 }
1172 
1173 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1174                        struct snd_ctl_elem_value *ucontrol)
1175 {
1176     struct echoaudio *chip;
1177     int c, changed;
1178 
1179     changed = 0;
1180     chip = snd_kcontrol_chip(kcontrol);
1181     spin_lock_irq(&chip->lock);
1182     for (c = 0; c < num_analog_busses_out(chip); c++) {
1183         if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1184             set_nominal_level(chip, c,
1185                       ucontrol->value.integer.value[c]);
1186             changed = 1;
1187         }
1188     }
1189     if (changed)
1190         update_output_line_level(chip);
1191     spin_unlock_irq(&chip->lock);
1192     return changed;
1193 }
1194 
1195 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1196     .name = "Line Playback Switch (-10dBV)",
1197     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1198     .info = snd_echo_output_nominal_info,
1199     .get = snd_echo_output_nominal_get,
1200     .put = snd_echo_output_nominal_put,
1201 };
1202 
1203 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1204 
1205 
1206 
1207 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1208 
1209 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1210 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1211                        struct snd_ctl_elem_info *uinfo)
1212 {
1213     struct echoaudio *chip;
1214 
1215     chip = snd_kcontrol_chip(kcontrol);
1216     uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1217     uinfo->count = num_analog_busses_in(chip);
1218     uinfo->value.integer.min = 0;
1219     uinfo->value.integer.max = 1;
1220     return 0;
1221 }
1222 
1223 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1224                       struct snd_ctl_elem_value *ucontrol)
1225 {
1226     struct echoaudio *chip;
1227     int c;
1228 
1229     chip = snd_kcontrol_chip(kcontrol);
1230     for (c = 0; c < num_analog_busses_in(chip); c++)
1231         ucontrol->value.integer.value[c] =
1232             chip->nominal_level[bx_analog_in(chip) + c];
1233     return 0;
1234 }
1235 
1236 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1237                       struct snd_ctl_elem_value *ucontrol)
1238 {
1239     struct echoaudio *chip;
1240     int c, changed;
1241 
1242     changed = 0;
1243     chip = snd_kcontrol_chip(kcontrol);
1244     spin_lock_irq(&chip->lock);
1245     for (c = 0; c < num_analog_busses_in(chip); c++) {
1246         if (chip->nominal_level[bx_analog_in(chip) + c] !=
1247             ucontrol->value.integer.value[c]) {
1248             set_nominal_level(chip, bx_analog_in(chip) + c,
1249                       ucontrol->value.integer.value[c]);
1250             changed = 1;
1251         }
1252     }
1253     if (changed)
1254         update_output_line_level(chip); /* "Output" is not a mistake
1255                          * here.
1256                          */
1257     spin_unlock_irq(&chip->lock);
1258     return changed;
1259 }
1260 
1261 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1262     .name = "Line Capture Switch (-10dBV)",
1263     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1264     .info = snd_echo_input_nominal_info,
1265     .get = snd_echo_input_nominal_get,
1266     .put = snd_echo_input_nominal_put,
1267 };
1268 
1269 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1270 
1271 
1272 
1273 #ifdef ECHOCARD_HAS_MONITOR
1274 
1275 /******************* Monitor mixer *******************/
1276 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1277                    struct snd_ctl_elem_info *uinfo)
1278 {
1279     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1280     uinfo->count = 1;
1281     uinfo->value.integer.min = ECHOGAIN_MINOUT;
1282     uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1283     return 0;
1284 }
1285 
1286 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1287                   struct snd_ctl_elem_value *ucontrol)
1288 {
1289     struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1290     unsigned int out = ucontrol->id.index / num_busses_in(chip);
1291     unsigned int in = ucontrol->id.index % num_busses_in(chip);
1292 
1293     if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1294         return -EINVAL;
1295 
1296     ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1297     return 0;
1298 }
1299 
1300 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1301                   struct snd_ctl_elem_value *ucontrol)
1302 {
1303     struct echoaudio *chip;
1304     int changed,  gain;
1305     unsigned int out, in;
1306 
1307     changed = 0;
1308     chip = snd_kcontrol_chip(kcontrol);
1309     out = ucontrol->id.index / num_busses_in(chip);
1310     in = ucontrol->id.index % num_busses_in(chip);
1311     if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1312         return -EINVAL;
1313     gain = ucontrol->value.integer.value[0];
1314     if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1315         return -EINVAL;
1316     if (chip->monitor_gain[out][in] != gain) {
1317         spin_lock_irq(&chip->lock);
1318         set_monitor_gain(chip, out, in, gain);
1319         update_output_line_level(chip);
1320         spin_unlock_irq(&chip->lock);
1321         changed = 1;
1322     }
1323     return changed;
1324 }
1325 
1326 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1327     .name = "Monitor Mixer Volume",
1328     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1329     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1330     .info = snd_echo_mixer_info,
1331     .get = snd_echo_mixer_get,
1332     .put = snd_echo_mixer_put,
1333     .tlv = {.p = db_scale_output_gain},
1334 };
1335 
1336 #endif /* ECHOCARD_HAS_MONITOR */
1337 
1338 
1339 
1340 #ifdef ECHOCARD_HAS_VMIXER
1341 
1342 /******************* Vmixer *******************/
1343 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1344                 struct snd_ctl_elem_info *uinfo)
1345 {
1346     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1347     uinfo->count = 1;
1348     uinfo->value.integer.min = ECHOGAIN_MINOUT;
1349     uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1350     return 0;
1351 }
1352 
1353 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1354                    struct snd_ctl_elem_value *ucontrol)
1355 {
1356     struct echoaudio *chip;
1357 
1358     chip = snd_kcontrol_chip(kcontrol);
1359     ucontrol->value.integer.value[0] =
1360         chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1361             [ucontrol->id.index % num_pipes_out(chip)];
1362     return 0;
1363 }
1364 
1365 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1366                    struct snd_ctl_elem_value *ucontrol)
1367 {
1368     struct echoaudio *chip;
1369     int gain, changed;
1370     short vch, out;
1371 
1372     changed = 0;
1373     chip = snd_kcontrol_chip(kcontrol);
1374     out = ucontrol->id.index / num_pipes_out(chip);
1375     vch = ucontrol->id.index % num_pipes_out(chip);
1376     gain = ucontrol->value.integer.value[0];
1377     if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1378         return -EINVAL;
1379     if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1380         spin_lock_irq(&chip->lock);
1381         set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1382         update_vmixer_level(chip);
1383         spin_unlock_irq(&chip->lock);
1384         changed = 1;
1385     }
1386     return changed;
1387 }
1388 
1389 static struct snd_kcontrol_new snd_echo_vmixer = {
1390     .name = "VMixer Volume",
1391     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1392     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1393     .info = snd_echo_vmixer_info,
1394     .get = snd_echo_vmixer_get,
1395     .put = snd_echo_vmixer_put,
1396     .tlv = {.p = db_scale_output_gain},
1397 };
1398 
1399 #endif /* ECHOCARD_HAS_VMIXER */
1400 
1401 
1402 
1403 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1404 
1405 /******************* Digital mode switch *******************/
1406 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1407                       struct snd_ctl_elem_info *uinfo)
1408 {
1409     static const char * const names[4] = {
1410         "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1411         "S/PDIF Cdrom"
1412     };
1413     struct echoaudio *chip;
1414 
1415     chip = snd_kcontrol_chip(kcontrol);
1416     return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1417 }
1418 
1419 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1420                      struct snd_ctl_elem_value *ucontrol)
1421 {
1422     struct echoaudio *chip;
1423     int i, mode;
1424 
1425     chip = snd_kcontrol_chip(kcontrol);
1426     mode = chip->digital_mode;
1427     for (i = chip->num_digital_modes - 1; i >= 0; i--)
1428         if (mode == chip->digital_mode_list[i]) {
1429             ucontrol->value.enumerated.item[0] = i;
1430             break;
1431         }
1432     return 0;
1433 }
1434 
1435 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1436                      struct snd_ctl_elem_value *ucontrol)
1437 {
1438     struct echoaudio *chip;
1439     int changed;
1440     unsigned short emode, dmode;
1441 
1442     changed = 0;
1443     chip = snd_kcontrol_chip(kcontrol);
1444 
1445     emode = ucontrol->value.enumerated.item[0];
1446     if (emode >= chip->num_digital_modes)
1447         return -EINVAL;
1448     dmode = chip->digital_mode_list[emode];
1449 
1450     if (dmode != chip->digital_mode) {
1451         /* mode_mutex is required to make this operation atomic wrt
1452         pcm_digital_*_open() and set_input_clock() functions. */
1453         mutex_lock(&chip->mode_mutex);
1454 
1455         /* Do not allow the user to change the digital mode when a pcm
1456         device is open because it also changes the number of channels
1457         and the allowed sample rates */
1458         if (chip->opencount) {
1459             changed = -EAGAIN;
1460         } else {
1461             changed = set_digital_mode(chip, dmode);
1462             /* If we had to change the clock source, report it */
1463             if (changed > 0 && chip->clock_src_ctl) {
1464                 snd_ctl_notify(chip->card,
1465                            SNDRV_CTL_EVENT_MASK_VALUE,
1466                            &chip->clock_src_ctl->id);
1467                 dev_dbg(chip->card->dev,
1468                     "SDM() =%d\n", changed);
1469             }
1470             if (changed >= 0)
1471                 changed = 1;    /* No errors */
1472         }
1473         mutex_unlock(&chip->mode_mutex);
1474     }
1475     return changed;
1476 }
1477 
1478 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1479     .name = "Digital mode Switch",
1480     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1481     .info = snd_echo_digital_mode_info,
1482     .get = snd_echo_digital_mode_get,
1483     .put = snd_echo_digital_mode_put,
1484 };
1485 
1486 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1487 
1488 
1489 
1490 #ifdef ECHOCARD_HAS_DIGITAL_IO
1491 
1492 /******************* S/PDIF mode switch *******************/
1493 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1494                     struct snd_ctl_elem_info *uinfo)
1495 {
1496     static const char * const names[2] = {"Consumer", "Professional"};
1497 
1498     return snd_ctl_enum_info(uinfo, 1, 2, names);
1499 }
1500 
1501 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1502                    struct snd_ctl_elem_value *ucontrol)
1503 {
1504     struct echoaudio *chip;
1505 
1506     chip = snd_kcontrol_chip(kcontrol);
1507     ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1508     return 0;
1509 }
1510 
1511 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1512                    struct snd_ctl_elem_value *ucontrol)
1513 {
1514     struct echoaudio *chip;
1515     int mode;
1516 
1517     chip = snd_kcontrol_chip(kcontrol);
1518     mode = !!ucontrol->value.enumerated.item[0];
1519     if (mode != chip->professional_spdif) {
1520         spin_lock_irq(&chip->lock);
1521         set_professional_spdif(chip, mode);
1522         spin_unlock_irq(&chip->lock);
1523         return 1;
1524     }
1525     return 0;
1526 }
1527 
1528 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1529     .name = "S/PDIF mode Switch",
1530     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1531     .info = snd_echo_spdif_mode_info,
1532     .get = snd_echo_spdif_mode_get,
1533     .put = snd_echo_spdif_mode_put,
1534 };
1535 
1536 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1537 
1538 
1539 
1540 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1541 
1542 /******************* Select input clock source *******************/
1543 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1544                       struct snd_ctl_elem_info *uinfo)
1545 {
1546     static const char * const names[8] = {
1547         "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1548         "ESync96", "MTC"
1549     };
1550     struct echoaudio *chip;
1551 
1552     chip = snd_kcontrol_chip(kcontrol);
1553     return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1554 }
1555 
1556 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1557                      struct snd_ctl_elem_value *ucontrol)
1558 {
1559     struct echoaudio *chip;
1560     int i, clock;
1561 
1562     chip = snd_kcontrol_chip(kcontrol);
1563     clock = chip->input_clock;
1564 
1565     for (i = 0; i < chip->num_clock_sources; i++)
1566         if (clock == chip->clock_source_list[i])
1567             ucontrol->value.enumerated.item[0] = i;
1568 
1569     return 0;
1570 }
1571 
1572 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1573                      struct snd_ctl_elem_value *ucontrol)
1574 {
1575     struct echoaudio *chip;
1576     int changed;
1577     unsigned int eclock, dclock;
1578 
1579     changed = 0;
1580     chip = snd_kcontrol_chip(kcontrol);
1581     eclock = ucontrol->value.enumerated.item[0];
1582     if (eclock >= chip->input_clock_types)
1583         return -EINVAL;
1584     dclock = chip->clock_source_list[eclock];
1585     if (chip->input_clock != dclock) {
1586         mutex_lock(&chip->mode_mutex);
1587         spin_lock_irq(&chip->lock);
1588         changed = set_input_clock(chip, dclock);
1589         if (!changed)
1590             changed = 1;    /* no errors */
1591         spin_unlock_irq(&chip->lock);
1592         mutex_unlock(&chip->mode_mutex);
1593     }
1594 
1595     if (changed < 0)
1596         dev_dbg(chip->card->dev,
1597             "seticlk val%d err 0x%x\n", dclock, changed);
1598 
1599     return changed;
1600 }
1601 
1602 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1603     .name = "Sample Clock Source",
1604     .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1605     .info = snd_echo_clock_source_info,
1606     .get = snd_echo_clock_source_get,
1607     .put = snd_echo_clock_source_put,
1608 };
1609 
1610 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1611 
1612 
1613 
1614 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1615 
1616 /******************* Phantom power switch *******************/
1617 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1618 
1619 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1620                       struct snd_ctl_elem_value *ucontrol)
1621 {
1622     struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1623 
1624     ucontrol->value.integer.value[0] = chip->phantom_power;
1625     return 0;
1626 }
1627 
1628 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1629                       struct snd_ctl_elem_value *ucontrol)
1630 {
1631     struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632     int power, changed = 0;
1633 
1634     power = !!ucontrol->value.integer.value[0];
1635     if (chip->phantom_power != power) {
1636         spin_lock_irq(&chip->lock);
1637         changed = set_phantom_power(chip, power);
1638         spin_unlock_irq(&chip->lock);
1639         if (changed == 0)
1640             changed = 1;    /* no errors */
1641     }
1642     return changed;
1643 }
1644 
1645 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1646     .name = "Phantom power Switch",
1647     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1648     .info = snd_echo_phantom_power_info,
1649     .get = snd_echo_phantom_power_get,
1650     .put = snd_echo_phantom_power_put,
1651 };
1652 
1653 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1654 
1655 
1656 
1657 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1658 
1659 /******************* Digital input automute switch *******************/
1660 #define snd_echo_automute_info      snd_ctl_boolean_mono_info
1661 
1662 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1663                  struct snd_ctl_elem_value *ucontrol)
1664 {
1665     struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1666 
1667     ucontrol->value.integer.value[0] = chip->digital_in_automute;
1668     return 0;
1669 }
1670 
1671 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1672                  struct snd_ctl_elem_value *ucontrol)
1673 {
1674     struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1675     int automute, changed = 0;
1676 
1677     automute = !!ucontrol->value.integer.value[0];
1678     if (chip->digital_in_automute != automute) {
1679         spin_lock_irq(&chip->lock);
1680         changed = set_input_auto_mute(chip, automute);
1681         spin_unlock_irq(&chip->lock);
1682         if (changed == 0)
1683             changed = 1;    /* no errors */
1684     }
1685     return changed;
1686 }
1687 
1688 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1689     .name = "Digital Capture Switch (automute)",
1690     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1691     .info = snd_echo_automute_info,
1692     .get = snd_echo_automute_get,
1693     .put = snd_echo_automute_put,
1694 };
1695 
1696 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1697 
1698 
1699 
1700 /******************* VU-meters switch *******************/
1701 #define snd_echo_vumeters_switch_info       snd_ctl_boolean_mono_info
1702 
1703 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1704                     struct snd_ctl_elem_value *ucontrol)
1705 {
1706     struct echoaudio *chip;
1707 
1708     chip = snd_kcontrol_chip(kcontrol);
1709     spin_lock_irq(&chip->lock);
1710     set_meters_on(chip, ucontrol->value.integer.value[0]);
1711     spin_unlock_irq(&chip->lock);
1712     return 1;
1713 }
1714 
1715 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1716     .name = "VU-meters Switch",
1717     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1718     .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1719     .info = snd_echo_vumeters_switch_info,
1720     .put = snd_echo_vumeters_switch_put,
1721 };
1722 
1723 
1724 
1725 /***** Read VU-meters (input, output, analog and digital together) *****/
1726 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1727                   struct snd_ctl_elem_info *uinfo)
1728 {
1729     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1730     uinfo->count = 96;
1731     uinfo->value.integer.min = ECHOGAIN_MINOUT;
1732     uinfo->value.integer.max = 0;
1733     return 0;
1734 }
1735 
1736 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1737                  struct snd_ctl_elem_value *ucontrol)
1738 {
1739     struct echoaudio *chip;
1740 
1741     chip = snd_kcontrol_chip(kcontrol);
1742     get_audio_meters(chip, ucontrol->value.integer.value);
1743     return 0;
1744 }
1745 
1746 static const struct snd_kcontrol_new snd_echo_vumeters = {
1747     .name = "VU-meters",
1748     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749     .access = SNDRV_CTL_ELEM_ACCESS_READ |
1750           SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1751           SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1752     .info = snd_echo_vumeters_info,
1753     .get = snd_echo_vumeters_get,
1754     .tlv = {.p = db_scale_output_gain},
1755 };
1756 
1757 
1758 
1759 /*** Channels info - it exports informations about the number of channels ***/
1760 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1761                        struct snd_ctl_elem_info *uinfo)
1762 {
1763     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1764     uinfo->count = 6;
1765     uinfo->value.integer.min = 0;
1766     uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1767     return 0;
1768 }
1769 
1770 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1771                       struct snd_ctl_elem_value *ucontrol)
1772 {
1773     struct echoaudio *chip;
1774     int detected, clocks, bit, src;
1775 
1776     chip = snd_kcontrol_chip(kcontrol);
1777     ucontrol->value.integer.value[0] = num_busses_in(chip);
1778     ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1779     ucontrol->value.integer.value[2] = num_busses_out(chip);
1780     ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1781     ucontrol->value.integer.value[4] = num_pipes_out(chip);
1782 
1783     /* Compute the bitmask of the currently valid input clocks */
1784     detected = detect_input_clocks(chip);
1785     clocks = 0;
1786     src = chip->num_clock_sources - 1;
1787     for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1788         if (detected & (1 << bit))
1789             for (; src >= 0; src--)
1790                 if (bit == chip->clock_source_list[src]) {
1791                     clocks |= 1 << src;
1792                     break;
1793                 }
1794     ucontrol->value.integer.value[5] = clocks;
1795 
1796     return 0;
1797 }
1798 
1799 static const struct snd_kcontrol_new snd_echo_channels_info = {
1800     .name = "Channels info",
1801     .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1802     .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1803     .info = snd_echo_channels_info_info,
1804     .get = snd_echo_channels_info_get,
1805 };
1806 
1807 
1808 
1809 
1810 /******************************************************************************
1811     IRQ Handling
1812 ******************************************************************************/
1813 /* Check if a period has elapsed since last interrupt
1814  *
1815  * Don't make any updates to state; PCM core handles this with the
1816  * correct locks.
1817  *
1818  * \return true if a period has elapsed, otherwise false
1819  */
1820 static bool period_has_elapsed(struct snd_pcm_substream *substream)
1821 {
1822     struct snd_pcm_runtime *runtime = substream->runtime;
1823     struct audiopipe *pipe = runtime->private_data;
1824     u32 counter, step;
1825     size_t period_bytes;
1826 
1827     if (pipe->state != PIPE_STATE_STARTED)
1828         return false;
1829 
1830     period_bytes = frames_to_bytes(runtime, runtime->period_size);
1831 
1832     counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1833 
1834     step = counter - pipe->last_period;  /* handles wrapping */
1835     step -= step % period_bytes;  /* acknowledge whole periods only */
1836 
1837     if (step == 0)
1838         return false;  /* haven't advanced a whole period yet */
1839 
1840     pipe->last_period += step;  /* used exclusively by us */
1841     return true;
1842 }
1843 
1844 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1845 {
1846     struct echoaudio *chip = dev_id;
1847     int ss, st;
1848 
1849     spin_lock(&chip->lock);
1850     st = service_irq(chip);
1851     if (st < 0) {
1852         spin_unlock(&chip->lock);
1853         return IRQ_NONE;
1854     }
1855     /* The hardware doesn't tell us which substream caused the irq,
1856     thus we have to check all running substreams. */
1857     for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1858         struct snd_pcm_substream *substream;
1859 
1860         substream = chip->substream[ss];
1861         if (substream && period_has_elapsed(substream)) {
1862             spin_unlock(&chip->lock);
1863             snd_pcm_period_elapsed(substream);
1864             spin_lock(&chip->lock);
1865         }
1866     }
1867     spin_unlock(&chip->lock);
1868 
1869 #ifdef ECHOCARD_HAS_MIDI
1870     if (st > 0 && chip->midi_in) {
1871         snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1872         dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1873     }
1874 #endif
1875     return IRQ_HANDLED;
1876 }
1877 
1878 
1879 
1880 
1881 /******************************************************************************
1882     Module construction / destruction
1883 ******************************************************************************/
1884 
1885 static void snd_echo_free(struct snd_card *card)
1886 {
1887     struct echoaudio *chip = card->private_data;
1888 
1889     if (chip->comm_page)
1890         rest_in_peace(chip);
1891 
1892     if (chip->irq >= 0)
1893         free_irq(chip->irq, chip);
1894 
1895     /* release chip data */
1896     free_firmware_cache(chip);
1897 }
1898 
1899 /* <--snd_echo_probe() */
1900 static int snd_echo_create(struct snd_card *card,
1901                struct pci_dev *pci)
1902 {
1903     struct echoaudio *chip = card->private_data;
1904     int err;
1905     size_t sz;
1906 
1907     pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1908 
1909     err = pcim_enable_device(pci);
1910     if (err < 0)
1911         return err;
1912     pci_set_master(pci);
1913 
1914     /* Allocate chip if needed */
1915     spin_lock_init(&chip->lock);
1916     chip->card = card;
1917     chip->pci = pci;
1918     chip->irq = -1;
1919     chip->opencount = 0;
1920     mutex_init(&chip->mode_mutex);
1921     chip->can_set_rate = 1;
1922 
1923     /* PCI resource allocation */
1924     err = pci_request_regions(pci, ECHOCARD_NAME);
1925     if (err < 0)
1926         return err;
1927 
1928     chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929     sz = pci_resource_len(pci, 0);
1930     if (sz > PAGE_SIZE)
1931         sz = PAGE_SIZE;     /* We map only the required part */
1932 
1933     chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
1934     if (!chip->dsp_registers) {
1935         dev_err(chip->card->dev, "ioremap failed\n");
1936         return -ENOMEM;
1937     }
1938 
1939     if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1940             KBUILD_MODNAME, chip)) {
1941         dev_err(chip->card->dev, "cannot grab irq\n");
1942         return -EBUSY;
1943     }
1944     chip->irq = pci->irq;
1945     card->sync_irq = chip->irq;
1946     dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1947         chip->pci, chip->irq, chip->pci->subsystem_device);
1948 
1949     card->private_free = snd_echo_free;
1950 
1951     /* Create the DSP comm page - this is the area of memory used for most
1952     of the communication with the DSP, which accesses it via bus mastering */
1953     chip->commpage_dma_buf =
1954         snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
1955                      sizeof(struct comm_page));
1956     if (!chip->commpage_dma_buf)
1957         return -ENOMEM;
1958     chip->comm_page_phys = chip->commpage_dma_buf->addr;
1959     chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1960 
1961     err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1962     if (err >= 0)
1963         err = set_mixer_defaults(chip);
1964     if (err < 0) {
1965         dev_err(card->dev, "init_hw err=%d\n", err);
1966         return err;
1967     }
1968 
1969     return 0;
1970 }
1971 
1972 /* constructor */
1973 static int __snd_echo_probe(struct pci_dev *pci,
1974                 const struct pci_device_id *pci_id)
1975 {
1976     static int dev;
1977     struct snd_card *card;
1978     struct echoaudio *chip;
1979     char *dsp;
1980     __maybe_unused int i;
1981     int err;
1982 
1983     if (dev >= SNDRV_CARDS)
1984         return -ENODEV;
1985     if (!enable[dev]) {
1986         dev++;
1987         return -ENOENT;
1988     }
1989 
1990     i = 0;
1991     err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1992                 sizeof(*chip), &card);
1993     if (err < 0)
1994         return err;
1995     chip = card->private_data;
1996 
1997     err = snd_echo_create(card, pci);
1998     if (err < 0)
1999         return err;
2000 
2001     strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2002     strcpy(card->shortname, chip->card_name);
2003 
2004     dsp = "56301";
2005     if (pci_id->device == 0x3410)
2006         dsp = "56361";
2007 
2008     sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2009         card->shortname, pci_id->subdevice & 0x000f, dsp,
2010         chip->dsp_registers_phys, chip->irq);
2011 
2012     err = snd_echo_new_pcm(chip);
2013     if (err < 0) {
2014         dev_err(chip->card->dev, "new pcm error %d\n", err);
2015         return err;
2016     }
2017 
2018 #ifdef ECHOCARD_HAS_MIDI
2019     if (chip->has_midi) {   /* Some Mia's do not have midi */
2020         err = snd_echo_midi_create(card, chip);
2021         if (err < 0) {
2022             dev_err(chip->card->dev, "new midi error %d\n", err);
2023             return err;
2024         }
2025     }
2026 #endif
2027 
2028 #ifdef ECHOCARD_HAS_VMIXER
2029     snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2030     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
2031     if (err < 0)
2032         return err;
2033 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2034     err = snd_ctl_add(chip->card,
2035               snd_ctl_new1(&snd_echo_line_output_gain, chip));
2036     if (err < 0)
2037         return err;
2038 #endif
2039 #else /* ECHOCARD_HAS_VMIXER */
2040     err = snd_ctl_add(chip->card,
2041               snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2042     if (err < 0)
2043         return err;
2044 #endif /* ECHOCARD_HAS_VMIXER */
2045 
2046 #ifdef ECHOCARD_HAS_INPUT_GAIN
2047     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2048     if (err < 0)
2049         return err;
2050 #endif
2051 
2052 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2053     if (!chip->hasnt_input_nominal_level) {
2054         err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2055         if (err < 0)
2056             return err;
2057     }
2058 #endif
2059 
2060 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2061     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2062     if (err < 0)
2063         return err;
2064 #endif
2065 
2066     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2067     if (err < 0)
2068         return err;
2069 
2070     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2071     if (err < 0)
2072         return err;
2073 
2074 #ifdef ECHOCARD_HAS_MONITOR
2075     snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2076     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2077     if (err < 0)
2078         return err;
2079 #endif
2080 
2081 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2082     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2083     if (err < 0)
2084         return err;
2085 #endif
2086 
2087     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2088     if (err < 0)
2089         return err;
2090 
2091 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2092     /* Creates a list of available digital modes */
2093     chip->num_digital_modes = 0;
2094     for (i = 0; i < 6; i++)
2095         if (chip->digital_modes & (1 << i))
2096             chip->digital_mode_list[chip->num_digital_modes++] = i;
2097 
2098     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2099     if (err < 0)
2100         return err;
2101 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2102 
2103 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2104     /* Creates a list of available clock sources */
2105     chip->num_clock_sources = 0;
2106     for (i = 0; i < 10; i++)
2107         if (chip->input_clock_types & (1 << i))
2108             chip->clock_source_list[chip->num_clock_sources++] = i;
2109 
2110     if (chip->num_clock_sources > 1) {
2111         chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2112         err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2113         if (err < 0)
2114             return err;
2115     }
2116 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2117 
2118 #ifdef ECHOCARD_HAS_DIGITAL_IO
2119     err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2120     if (err < 0)
2121         return err;
2122 #endif
2123 
2124 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2125     if (chip->has_phantom_power) {
2126         err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2127         if (err < 0)
2128             return err;
2129     }
2130 #endif
2131 
2132     err = snd_card_register(card);
2133     if (err < 0)
2134         return err;
2135     dev_info(card->dev, "Card registered: %s\n", card->longname);
2136 
2137     pci_set_drvdata(pci, chip);
2138     dev++;
2139     return 0;
2140 }
2141 
2142 static int snd_echo_probe(struct pci_dev *pci,
2143               const struct pci_device_id *pci_id)
2144 {
2145     return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2146 }
2147 
2148 
2149 #if defined(CONFIG_PM_SLEEP)
2150 
2151 static int snd_echo_suspend(struct device *dev)
2152 {
2153     struct echoaudio *chip = dev_get_drvdata(dev);
2154 
2155 #ifdef ECHOCARD_HAS_MIDI
2156     /* This call can sleep */
2157     if (chip->midi_out)
2158         snd_echo_midi_output_trigger(chip->midi_out, 0);
2159 #endif
2160     spin_lock_irq(&chip->lock);
2161     if (wait_handshake(chip)) {
2162         spin_unlock_irq(&chip->lock);
2163         return -EIO;
2164     }
2165     clear_handshake(chip);
2166     if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2167         spin_unlock_irq(&chip->lock);
2168         return -EIO;
2169     }
2170     spin_unlock_irq(&chip->lock);
2171 
2172     chip->dsp_code = NULL;
2173     free_irq(chip->irq, chip);
2174     chip->irq = -1;
2175     chip->card->sync_irq = -1;
2176     return 0;
2177 }
2178 
2179 
2180 
2181 static int snd_echo_resume(struct device *dev)
2182 {
2183     struct pci_dev *pci = to_pci_dev(dev);
2184     struct echoaudio *chip = dev_get_drvdata(dev);
2185     struct comm_page *commpage, *commpage_bak;
2186     u32 pipe_alloc_mask;
2187     int err;
2188 
2189     commpage = chip->comm_page;
2190     commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2191     if (commpage_bak == NULL)
2192         return -ENOMEM;
2193 
2194     err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2195     if (err < 0) {
2196         kfree(commpage_bak);
2197         dev_err(dev, "resume init_hw err=%d\n", err);
2198         return err;
2199     }
2200 
2201     /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2202      * restore_dsp_settings() fails.
2203      */
2204     pipe_alloc_mask = chip->pipe_alloc_mask;
2205     chip->pipe_alloc_mask = 0;
2206     err = restore_dsp_rettings(chip);
2207     chip->pipe_alloc_mask = pipe_alloc_mask;
2208     if (err < 0) {
2209         kfree(commpage_bak);
2210         return err;
2211     }
2212 
2213     memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2214         sizeof(commpage->audio_format));
2215     memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2216         sizeof(commpage->sglist_addr));
2217     memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2218         sizeof(commpage->midi_output));
2219     kfree(commpage_bak);
2220 
2221     if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2222             KBUILD_MODNAME, chip)) {
2223         dev_err(chip->card->dev, "cannot grab irq\n");
2224         return -EBUSY;
2225     }
2226     chip->irq = pci->irq;
2227     chip->card->sync_irq = chip->irq;
2228     dev_dbg(dev, "resume irq=%d\n", chip->irq);
2229 
2230 #ifdef ECHOCARD_HAS_MIDI
2231     if (chip->midi_input_enabled)
2232         enable_midi_input(chip, true);
2233     if (chip->midi_out)
2234         snd_echo_midi_output_trigger(chip->midi_out, 1);
2235 #endif
2236 
2237     return 0;
2238 }
2239 
2240 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2241 #define SND_ECHO_PM_OPS &snd_echo_pm
2242 #else
2243 #define SND_ECHO_PM_OPS NULL
2244 #endif /* CONFIG_PM_SLEEP */
2245 
2246 /******************************************************************************
2247     Everything starts and ends here
2248 ******************************************************************************/
2249 
2250 /* pci_driver definition */
2251 static struct pci_driver echo_driver = {
2252     .name = KBUILD_MODNAME,
2253     .id_table = snd_echo_ids,
2254     .probe = snd_echo_probe,
2255     .driver = {
2256         .pm = SND_ECHO_PM_OPS,
2257     },
2258 };
2259 
2260 module_pci_driver(echo_driver);