1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA driver for Echoaudio soundcards.
4 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5 * Copyright (C) 2020 Mark Hills <mark@xwax.org>
8 #include <linux/module.h>
10 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
11 MODULE_LICENSE("GPL v2");
12 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
13 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
15 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
16 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
17 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
19 module_param_array(index, int, NULL, 0444);
20 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
21 module_param_array(id, charp, NULL, 0444);
22 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
23 module_param_array(enable, bool, NULL, 0444);
24 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
26 static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
27 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
31 static int get_firmware(const struct firmware **fw_entry,
32 struct echoaudio *chip, const short fw_index)
37 #ifdef CONFIG_PM_SLEEP
38 if (chip->fw_cache[fw_index]) {
39 dev_dbg(chip->card->dev,
40 "firmware requested: %s is cached\n",
41 card_fw[fw_index].data);
42 *fw_entry = chip->fw_cache[fw_index];
47 dev_dbg(chip->card->dev,
48 "firmware requested: %s\n", card_fw[fw_index].data);
49 snprintf(name, sizeof(name), "/*(DEBLOBBED)*/", card_fw[fw_index].data);
50 err = reject_firmware(fw_entry, name, &chip->pci->dev);
52 dev_err(chip->card->dev,
53 "get_firmware(): Firmware not available (%d)\n", err);
54 #ifdef CONFIG_PM_SLEEP
56 chip->fw_cache[fw_index] = *fw_entry;
63 static void free_firmware(const struct firmware *fw_entry,
64 struct echoaudio *chip)
66 #ifdef CONFIG_PM_SLEEP
67 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
69 release_firmware(fw_entry);
75 static void free_firmware_cache(struct echoaudio *chip)
77 #ifdef CONFIG_PM_SLEEP
80 for (i = 0; i < 8 ; i++)
81 if (chip->fw_cache[i]) {
82 release_firmware(chip->fw_cache[i]);
83 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
91 /******************************************************************************
93 ******************************************************************************/
95 static void audiopipe_free(struct snd_pcm_runtime *runtime)
97 struct audiopipe *pipe = runtime->private_data;
99 if (pipe->sgpage.area)
100 snd_dma_free_pages(&pipe->sgpage);
106 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
107 struct snd_pcm_hw_rule *rule)
109 struct snd_interval *c = hw_param_interval(params,
110 SNDRV_PCM_HW_PARAM_CHANNELS);
111 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
116 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
117 /* >=2 channels cannot be S32_BE */
119 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
120 return snd_mask_refine(f, &fmt);
123 /* > 2 channels cannot be U8 and S32_BE */
125 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
126 return snd_mask_refine(f, &fmt);
128 /* Mono is ok with any format */
134 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
135 struct snd_pcm_hw_rule *rule)
137 struct snd_interval *c = hw_param_interval(params,
138 SNDRV_PCM_HW_PARAM_CHANNELS);
139 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
140 struct snd_interval ch;
142 snd_interval_any(&ch);
144 /* S32_BE is mono (and stereo) only */
145 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
147 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
153 return snd_interval_refine(c, &ch);
155 /* U8 can be only mono or stereo */
156 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
160 return snd_interval_refine(c, &ch);
162 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
168 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
169 struct snd_pcm_hw_rule *rule)
171 struct snd_interval *c = hw_param_interval(params,
172 SNDRV_PCM_HW_PARAM_CHANNELS);
173 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
178 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
180 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
182 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
183 SNDRV_PCM_FMTBIT_S24_3LE |
184 SNDRV_PCM_FMTBIT_S32_LE;
185 /* 1 channel must be S32_BE or S32_LE */
186 } else if (c->max == 1)
187 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
188 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
189 /* 2 channels cannot be S32_BE */
190 else if (c->min == 2 && c->max == 2)
191 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
196 fmt.bits[0] &= (u32)fmask;
197 fmt.bits[1] &= (u32)(fmask >> 32);
198 return snd_mask_refine(f, &fmt);
203 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
204 struct snd_pcm_hw_rule *rule)
206 struct snd_interval *c = hw_param_interval(params,
207 SNDRV_PCM_HW_PARAM_CHANNELS);
208 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
209 struct snd_interval ch;
212 snd_interval_any(&ch);
214 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
216 /* S32_BE is mono (and stereo) only */
217 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
219 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
224 /* U8 is stereo only */
225 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
227 /* S16_LE and S24_3LE must be at least stereo */
228 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
229 SNDRV_PCM_FMTBIT_S24_3LE)))
234 return snd_interval_refine(c, &ch);
239 /* Since the sample rate is a global setting, do allow the user to change the
240 sample rate only if there is only one pcm device open. */
241 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
242 struct snd_pcm_hw_rule *rule)
244 struct snd_interval *rate = hw_param_interval(params,
245 SNDRV_PCM_HW_PARAM_RATE);
246 struct echoaudio *chip = rule->private;
247 struct snd_interval fixed;
250 mutex_lock(&chip->mode_mutex);
252 if (chip->can_set_rate) {
255 snd_interval_any(&fixed);
256 fixed.min = fixed.max = chip->sample_rate;
257 err = snd_interval_refine(rate, &fixed);
260 mutex_unlock(&chip->mode_mutex);
265 static int pcm_open(struct snd_pcm_substream *substream,
266 signed char max_channels)
268 struct echoaudio *chip;
269 struct snd_pcm_runtime *runtime;
270 struct audiopipe *pipe;
273 if (max_channels <= 0)
276 chip = snd_pcm_substream_chip(substream);
277 runtime = substream->runtime;
279 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
282 pipe->index = -1; /* Not configured yet */
284 /* Set up hw capabilities and contraints */
285 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
286 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
287 pipe->constr.list = channels_list;
288 pipe->constr.mask = 0;
289 for (i = 0; channels_list[i] <= max_channels; i++);
290 pipe->constr.count = i;
291 if (pipe->hw.channels_max > max_channels)
292 pipe->hw.channels_max = max_channels;
293 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
294 pipe->hw.rate_max = 48000;
295 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
298 runtime->hw = pipe->hw;
299 runtime->private_data = pipe;
300 runtime->private_free = audiopipe_free;
301 snd_pcm_set_sync(substream);
303 /* Only mono and any even number of channels are allowed */
304 err = snd_pcm_hw_constraint_list(runtime, 0,
305 SNDRV_PCM_HW_PARAM_CHANNELS,
310 /* All periods should have the same size */
311 err = snd_pcm_hw_constraint_integer(runtime,
312 SNDRV_PCM_HW_PARAM_PERIODS);
316 /* The hw accesses memory in chunks 32 frames long and they should be
317 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
318 generated with a resolution of 32 frames. Thus we need the following */
319 err = snd_pcm_hw_constraint_step(runtime, 0,
320 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
323 err = snd_pcm_hw_constraint_step(runtime, 0,
324 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
328 err = snd_pcm_hw_rule_add(substream->runtime, 0,
329 SNDRV_PCM_HW_PARAM_RATE,
330 hw_rule_sample_rate, chip,
331 SNDRV_PCM_HW_PARAM_RATE, -1);
335 /* Allocate a page for the scatter-gather list */
336 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
338 PAGE_SIZE, &pipe->sgpage);
340 dev_err(chip->card->dev, "s-g list allocation failed\n");
345 * Sole ownership required to set the rate
348 dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
349 chip->opencount, chip->can_set_rate, chip->rate_set);
352 if (chip->opencount > 1 && chip->rate_set)
353 chip->can_set_rate = 0;
360 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
362 struct echoaudio *chip = snd_pcm_substream_chip(substream);
365 err = pcm_open(substream,
366 num_analog_busses_in(chip) - substream->number);
369 err = snd_pcm_hw_rule_add(substream->runtime, 0,
370 SNDRV_PCM_HW_PARAM_CHANNELS,
371 hw_rule_capture_channels_by_format, NULL,
372 SNDRV_PCM_HW_PARAM_FORMAT, -1);
375 err = snd_pcm_hw_rule_add(substream->runtime, 0,
376 SNDRV_PCM_HW_PARAM_FORMAT,
377 hw_rule_capture_format_by_channels, NULL,
378 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
387 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
389 struct echoaudio *chip = snd_pcm_substream_chip(substream);
390 int max_channels, err;
392 #ifdef ECHOCARD_HAS_VMIXER
393 max_channels = num_pipes_out(chip);
395 max_channels = num_analog_busses_out(chip);
397 err = pcm_open(substream, max_channels - substream->number);
400 err = snd_pcm_hw_rule_add(substream->runtime, 0,
401 SNDRV_PCM_HW_PARAM_CHANNELS,
402 hw_rule_playback_channels_by_format,
404 SNDRV_PCM_HW_PARAM_FORMAT, -1);
407 err = snd_pcm_hw_rule_add(substream->runtime, 0,
408 SNDRV_PCM_HW_PARAM_FORMAT,
409 hw_rule_playback_format_by_channels,
411 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
420 #ifdef ECHOCARD_HAS_DIGITAL_IO
422 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
424 struct echoaudio *chip = snd_pcm_substream_chip(substream);
425 int err, max_channels;
427 max_channels = num_digital_busses_in(chip) - substream->number;
428 mutex_lock(&chip->mode_mutex);
429 if (chip->digital_mode == DIGITAL_MODE_ADAT)
430 err = pcm_open(substream, max_channels);
431 else /* If the card has ADAT, subtract the 6 channels
432 * that S/PDIF doesn't have
434 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
439 err = snd_pcm_hw_rule_add(substream->runtime, 0,
440 SNDRV_PCM_HW_PARAM_CHANNELS,
441 hw_rule_capture_channels_by_format, NULL,
442 SNDRV_PCM_HW_PARAM_FORMAT, -1);
445 err = snd_pcm_hw_rule_add(substream->runtime, 0,
446 SNDRV_PCM_HW_PARAM_FORMAT,
447 hw_rule_capture_format_by_channels, NULL,
448 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
453 mutex_unlock(&chip->mode_mutex);
459 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
461 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
463 struct echoaudio *chip = snd_pcm_substream_chip(substream);
464 int err, max_channels;
466 max_channels = num_digital_busses_out(chip) - substream->number;
467 mutex_lock(&chip->mode_mutex);
468 if (chip->digital_mode == DIGITAL_MODE_ADAT)
469 err = pcm_open(substream, max_channels);
470 else /* If the card has ADAT, subtract the 6 channels
471 * that S/PDIF doesn't have
473 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
478 err = snd_pcm_hw_rule_add(substream->runtime, 0,
479 SNDRV_PCM_HW_PARAM_CHANNELS,
480 hw_rule_playback_channels_by_format,
481 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
485 err = snd_pcm_hw_rule_add(substream->runtime, 0,
486 SNDRV_PCM_HW_PARAM_FORMAT,
487 hw_rule_playback_format_by_channels,
488 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
494 mutex_unlock(&chip->mode_mutex);
498 #endif /* !ECHOCARD_HAS_VMIXER */
500 #endif /* ECHOCARD_HAS_DIGITAL_IO */
504 static int pcm_close(struct snd_pcm_substream *substream)
506 struct echoaudio *chip = snd_pcm_substream_chip(substream);
508 /* Nothing to do here. Audio is already off and pipe will be
509 * freed by its callback
512 mutex_lock(&chip->mode_mutex);
514 dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
515 chip->opencount, chip->can_set_rate, chip->rate_set);
519 switch (chip->opencount) {
521 chip->can_set_rate = 1;
529 mutex_unlock(&chip->mode_mutex);
535 /* Channel allocation and scatter-gather list setup */
536 static int init_engine(struct snd_pcm_substream *substream,
537 struct snd_pcm_hw_params *hw_params,
538 int pipe_index, int interleave)
540 struct echoaudio *chip;
541 int err, per, rest, page, edge, offs;
542 struct audiopipe *pipe;
544 chip = snd_pcm_substream_chip(substream);
545 pipe = (struct audiopipe *) substream->runtime->private_data;
547 /* Sets up che hardware. If it's already initialized, reset and
548 * redo with the new parameters
550 spin_lock_irq(&chip->lock);
551 if (pipe->index >= 0) {
552 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
553 err = free_pipes(chip, pipe);
555 chip->substream[pipe->index] = NULL;
558 err = allocate_pipes(chip, pipe, pipe_index, interleave);
560 spin_unlock_irq(&chip->lock);
561 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
565 spin_unlock_irq(&chip->lock);
566 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
568 dev_dbg(chip->card->dev,
569 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
570 params_buffer_bytes(hw_params), params_periods(hw_params),
571 params_period_bytes(hw_params));
573 sglist_init(chip, pipe);
575 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
577 rest = params_period_bytes(hw_params);
578 if (offs + rest > params_buffer_bytes(hw_params))
579 rest = params_buffer_bytes(hw_params) - offs;
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);
589 sglist_add_mapping(chip, pipe, addr,
601 /* Close the ring buffer */
602 sglist_wrap(chip, pipe);
604 /* This stuff is used by the irq handler, so it must be
605 * initialized before chip->substream
607 pipe->last_period = 0;
608 pipe->last_counter = 0;
611 chip->substream[pipe_index] = substream;
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);
621 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
622 struct snd_pcm_hw_params *hw_params)
624 struct echoaudio *chip = snd_pcm_substream_chip(substream);
626 return init_engine(substream, hw_params, px_analog_in(chip) +
627 substream->number, params_channels(hw_params));
632 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
633 struct snd_pcm_hw_params *hw_params)
635 return init_engine(substream, hw_params, substream->number,
636 params_channels(hw_params));
641 #ifdef ECHOCARD_HAS_DIGITAL_IO
643 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
644 struct snd_pcm_hw_params *hw_params)
646 struct echoaudio *chip = snd_pcm_substream_chip(substream);
648 return init_engine(substream, hw_params, px_digital_in(chip) +
649 substream->number, params_channels(hw_params));
654 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
655 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_hw_params *hw_params)
658 struct echoaudio *chip = snd_pcm_substream_chip(substream);
660 return init_engine(substream, hw_params, px_digital_out(chip) +
661 substream->number, params_channels(hw_params));
663 #endif /* !ECHOCARD_HAS_VMIXER */
665 #endif /* ECHOCARD_HAS_DIGITAL_IO */
669 static int pcm_hw_free(struct snd_pcm_substream *substream)
671 struct echoaudio *chip;
672 struct audiopipe *pipe;
674 chip = snd_pcm_substream_chip(substream);
675 pipe = (struct audiopipe *) substream->runtime->private_data;
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;
684 spin_unlock_irq(&chip->lock);
691 static int pcm_prepare(struct snd_pcm_substream *substream)
693 struct echoaudio *chip = snd_pcm_substream_chip(substream);
694 struct snd_pcm_runtime *runtime = substream->runtime;
695 struct audioformat format;
696 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
698 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
699 runtime->rate, runtime->format, runtime->channels);
700 format.interleave = runtime->channels;
701 format.data_are_bigendian = 0;
702 format.mono_to_stereo = 0;
703 switch (runtime->format) {
704 case SNDRV_PCM_FORMAT_U8:
705 format.bits_per_sample = 8;
707 case SNDRV_PCM_FORMAT_S16_LE:
708 format.bits_per_sample = 16;
710 case SNDRV_PCM_FORMAT_S24_3LE:
711 format.bits_per_sample = 24;
713 case SNDRV_PCM_FORMAT_S32_BE:
714 format.data_are_bigendian = 1;
716 case SNDRV_PCM_FORMAT_S32_LE:
717 format.bits_per_sample = 32;
720 dev_err(chip->card->dev,
721 "Prepare error: unsupported format %d\n",
726 if (snd_BUG_ON(pipe_index >= px_num(chip)))
730 * We passed checks we can do independently; now take
734 spin_lock_irq(&chip->lock);
736 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
737 spin_unlock_irq(&chip->lock);
741 set_audio_format(chip, pipe_index, &format);
742 spin_unlock_irq(&chip->lock);
749 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
751 struct echoaudio *chip = snd_pcm_substream_chip(substream);
752 struct audiopipe *pipe;
755 struct snd_pcm_substream *s;
757 snd_pcm_group_for_each_entry(s, substream) {
758 for (i = 0; i < DSP_MAXPIPES; i++) {
759 if (s == chip->substream[i]) {
760 channelmask |= 1 << i;
761 snd_pcm_trigger_done(s, substream);
766 spin_lock(&chip->lock);
768 case SNDRV_PCM_TRIGGER_RESUME:
769 case SNDRV_PCM_TRIGGER_START:
770 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
771 for (i = 0; i < DSP_MAXPIPES; i++) {
772 if (channelmask & (1 << i)) {
773 pipe = chip->substream[i]->runtime->private_data;
774 switch (pipe->state) {
775 case PIPE_STATE_STOPPED:
776 pipe->last_period = 0;
777 pipe->last_counter = 0;
779 *pipe->dma_counter = 0;
781 case PIPE_STATE_PAUSED:
782 pipe->state = PIPE_STATE_STARTED;
784 case PIPE_STATE_STARTED:
789 err = start_transport(chip, channelmask,
790 chip->pipe_cyclic_mask);
792 case SNDRV_PCM_TRIGGER_SUSPEND:
793 case SNDRV_PCM_TRIGGER_STOP:
794 for (i = 0; i < DSP_MAXPIPES; i++) {
795 if (channelmask & (1 << i)) {
796 pipe = chip->substream[i]->runtime->private_data;
797 pipe->state = PIPE_STATE_STOPPED;
800 err = stop_transport(chip, channelmask);
802 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
803 for (i = 0; i < DSP_MAXPIPES; i++) {
804 if (channelmask & (1 << i)) {
805 pipe = chip->substream[i]->runtime->private_data;
806 pipe->state = PIPE_STATE_PAUSED;
809 err = pause_transport(chip, channelmask);
814 spin_unlock(&chip->lock);
820 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
822 struct snd_pcm_runtime *runtime = substream->runtime;
823 struct audiopipe *pipe = runtime->private_data;
827 * IRQ handling runs concurrently. Do not share tracking of
828 * counter with it, which would race or require locking
831 counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
833 step = counter - pipe->last_counter; /* handles wrapping */
834 pipe->last_counter = counter;
836 /* counter doesn't neccessarily wrap on a multiple of
837 * buffer_size, so can't derive the position; must
840 pipe->position += step;
841 pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
843 return bytes_to_frames(runtime, pipe->position);
848 /* pcm *_ops structures */
849 static const struct snd_pcm_ops analog_playback_ops = {
850 .open = pcm_analog_out_open,
852 .hw_params = pcm_analog_out_hw_params,
853 .hw_free = pcm_hw_free,
854 .prepare = pcm_prepare,
855 .trigger = pcm_trigger,
856 .pointer = pcm_pointer,
858 static const struct snd_pcm_ops analog_capture_ops = {
859 .open = pcm_analog_in_open,
861 .hw_params = pcm_analog_in_hw_params,
862 .hw_free = pcm_hw_free,
863 .prepare = pcm_prepare,
864 .trigger = pcm_trigger,
865 .pointer = pcm_pointer,
867 #ifdef ECHOCARD_HAS_DIGITAL_IO
868 #ifndef ECHOCARD_HAS_VMIXER
869 static const struct snd_pcm_ops digital_playback_ops = {
870 .open = pcm_digital_out_open,
872 .hw_params = pcm_digital_out_hw_params,
873 .hw_free = pcm_hw_free,
874 .prepare = pcm_prepare,
875 .trigger = pcm_trigger,
876 .pointer = pcm_pointer,
878 #endif /* !ECHOCARD_HAS_VMIXER */
879 static const struct snd_pcm_ops digital_capture_ops = {
880 .open = pcm_digital_in_open,
882 .hw_params = pcm_digital_in_hw_params,
883 .hw_free = pcm_hw_free,
884 .prepare = pcm_prepare,
885 .trigger = pcm_trigger,
886 .pointer = pcm_pointer,
888 #endif /* ECHOCARD_HAS_DIGITAL_IO */
892 /* Preallocate memory only for the first substream because it's the most
895 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
897 struct snd_pcm_substream *ss;
900 for (stream = 0; stream < 2; stream++)
901 for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
902 snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
904 ss->number ? 0 : 128<<10,
910 /*<--snd_echo_probe() */
911 static int snd_echo_new_pcm(struct echoaudio *chip)
916 #ifdef ECHOCARD_HAS_VMIXER
917 /* This card has a Vmixer, that is there is no direct mapping from PCM
918 streams to physical outputs. The user can mix the streams as he wishes
919 via control interface and it's possible to send any stream to any
920 output, thus it makes no sense to keep analog and digital outputs
923 /* PCM#0 Virtual outputs and analog inputs */
924 err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
925 num_analog_busses_in(chip), &pcm);
928 pcm->private_data = chip;
929 chip->analog_pcm = pcm;
930 strcpy(pcm->name, chip->card->shortname);
931 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
933 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
935 #ifdef ECHOCARD_HAS_DIGITAL_IO
936 /* PCM#1 Digital inputs, no outputs */
937 err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
938 num_digital_busses_in(chip), &pcm);
941 pcm->private_data = chip;
942 chip->digital_pcm = pcm;
943 strcpy(pcm->name, chip->card->shortname);
944 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
945 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
946 #endif /* ECHOCARD_HAS_DIGITAL_IO */
948 #else /* ECHOCARD_HAS_VMIXER */
950 /* The card can manage substreams formed by analog and digital channels
951 at the same time, but I prefer to keep analog and digital channels
952 separated, because that mixed thing is confusing and useless. So we
953 register two PCM devices: */
955 /* PCM#0 Analog i/o */
956 err = snd_pcm_new(chip->card, "Analog PCM", 0,
957 num_analog_busses_out(chip),
958 num_analog_busses_in(chip), &pcm);
961 pcm->private_data = chip;
962 chip->analog_pcm = pcm;
963 strcpy(pcm->name, chip->card->shortname);
964 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
965 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
966 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
968 #ifdef ECHOCARD_HAS_DIGITAL_IO
969 /* PCM#1 Digital i/o */
970 err = snd_pcm_new(chip->card, "Digital PCM", 1,
971 num_digital_busses_out(chip),
972 num_digital_busses_in(chip), &pcm);
975 pcm->private_data = chip;
976 chip->digital_pcm = pcm;
977 strcpy(pcm->name, chip->card->shortname);
978 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
979 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
980 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
981 #endif /* ECHOCARD_HAS_DIGITAL_IO */
983 #endif /* ECHOCARD_HAS_VMIXER */
991 /******************************************************************************
993 ******************************************************************************/
995 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
997 /******************* PCM output volume *******************/
998 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
999 struct snd_ctl_elem_info *uinfo)
1001 struct echoaudio *chip;
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;
1011 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1012 struct snd_ctl_elem_value *ucontrol)
1014 struct echoaudio *chip;
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];
1023 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1024 struct snd_ctl_elem_value *ucontrol)
1026 struct echoaudio *chip;
1027 int c, changed, gain;
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)
1037 if (chip->output_gain[c] != gain) {
1038 set_output_gain(chip, c, gain);
1043 update_output_line_level(chip);
1044 spin_unlock_irq(&chip->lock);
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},
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},
1072 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1076 #ifdef ECHOCARD_HAS_INPUT_GAIN
1078 /******************* Analog input volume *******************/
1079 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1080 struct snd_ctl_elem_info *uinfo)
1082 struct echoaudio *chip;
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;
1092 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
1095 struct echoaudio *chip;
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];
1104 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_value *ucontrol)
1107 struct echoaudio *chip;
1108 int c, gain, changed;
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)
1118 if (chip->input_gain[c] != gain) {
1119 set_input_gain(chip, c, gain);
1124 update_input_line_level(chip);
1125 spin_unlock_irq(&chip->lock);
1129 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
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},
1141 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1145 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
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)
1151 struct echoaudio *chip;
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;
1161 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1162 struct snd_ctl_elem_value *ucontrol)
1164 struct echoaudio *chip;
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];
1173 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1174 struct snd_ctl_elem_value *ucontrol)
1176 struct echoaudio *chip;
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]);
1190 update_output_line_level(chip);
1191 spin_unlock_irq(&chip->lock);
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,
1203 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1207 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
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)
1213 struct echoaudio *chip;
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;
1223 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1224 struct snd_ctl_elem_value *ucontrol)
1226 struct echoaudio *chip;
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];
1236 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1237 struct snd_ctl_elem_value *ucontrol)
1239 struct echoaudio *chip;
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]);
1254 update_output_line_level(chip); /* "Output" is not a mistake
1257 spin_unlock_irq(&chip->lock);
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,
1269 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1273 #ifdef ECHOCARD_HAS_MONITOR
1275 /******************* Monitor mixer *******************/
1276 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_info *uinfo)
1279 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1281 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1282 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1286 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1287 struct snd_ctl_elem_value *ucontrol)
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);
1293 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1296 ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1300 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
1303 struct echoaudio *chip;
1305 unsigned int out, in;
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)
1313 gain = ucontrol->value.integer.value[0];
1314 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
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);
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},
1336 #endif /* ECHOCARD_HAS_MONITOR */
1340 #ifdef ECHOCARD_HAS_VMIXER
1342 /******************* Vmixer *******************/
1343 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1344 struct snd_ctl_elem_info *uinfo)
1346 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1348 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1349 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1353 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1354 struct snd_ctl_elem_value *ucontrol)
1356 struct echoaudio *chip;
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)];
1365 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1366 struct snd_ctl_elem_value *ucontrol)
1368 struct echoaudio *chip;
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)
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);
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},
1399 #endif /* ECHOCARD_HAS_VMIXER */
1403 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1405 /******************* Digital mode switch *******************/
1406 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_info *uinfo)
1409 static const char * const names[4] = {
1410 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1413 struct echoaudio *chip;
1415 chip = snd_kcontrol_chip(kcontrol);
1416 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1419 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_value *ucontrol)
1422 struct echoaudio *chip;
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;
1435 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_value *ucontrol)
1438 struct echoaudio *chip;
1440 unsigned short emode, dmode;
1443 chip = snd_kcontrol_chip(kcontrol);
1445 emode = ucontrol->value.enumerated.item[0];
1446 if (emode >= chip->num_digital_modes)
1448 dmode = chip->digital_mode_list[emode];
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);
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) {
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);
1471 changed = 1; /* No errors */
1473 mutex_unlock(&chip->mode_mutex);
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,
1486 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1490 #ifdef ECHOCARD_HAS_DIGITAL_IO
1492 /******************* S/PDIF mode switch *******************/
1493 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_info *uinfo)
1496 static const char * const names[2] = {"Consumer", "Professional"};
1498 return snd_ctl_enum_info(uinfo, 1, 2, names);
1501 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1502 struct snd_ctl_elem_value *ucontrol)
1504 struct echoaudio *chip;
1506 chip = snd_kcontrol_chip(kcontrol);
1507 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1511 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_value *ucontrol)
1514 struct echoaudio *chip;
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);
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,
1536 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1540 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1542 /******************* Select input clock source *******************/
1543 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_info *uinfo)
1546 static const char * const names[8] = {
1547 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1550 struct echoaudio *chip;
1552 chip = snd_kcontrol_chip(kcontrol);
1553 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1556 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1559 struct echoaudio *chip;
1562 chip = snd_kcontrol_chip(kcontrol);
1563 clock = chip->input_clock;
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;
1572 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_value *ucontrol)
1575 struct echoaudio *chip;
1577 unsigned int eclock, dclock;
1580 chip = snd_kcontrol_chip(kcontrol);
1581 eclock = ucontrol->value.enumerated.item[0];
1582 if (eclock >= chip->input_clock_types)
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);
1590 changed = 1; /* no errors */
1591 spin_unlock_irq(&chip->lock);
1592 mutex_unlock(&chip->mode_mutex);
1596 dev_dbg(chip->card->dev,
1597 "seticlk val%d err 0x%x\n", dclock, changed);
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,
1610 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1614 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1616 /******************* Phantom power switch *******************/
1617 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1619 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1620 struct snd_ctl_elem_value *ucontrol)
1622 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1624 ucontrol->value.integer.value[0] = chip->phantom_power;
1628 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_value *ucontrol)
1631 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632 int power, changed = 0;
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);
1640 changed = 1; /* no errors */
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,
1653 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1657 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1659 /******************* Digital input automute switch *******************/
1660 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1662 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol)
1665 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1667 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1671 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol)
1674 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1675 int automute, changed = 0;
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);
1683 changed = 1; /* no errors */
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,
1696 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1700 /******************* VU-meters switch *******************/
1701 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1703 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol)
1706 struct echoaudio *chip;
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);
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,
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)
1729 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1731 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1732 uinfo->value.integer.max = 0;
1736 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
1739 struct echoaudio *chip;
1741 chip = snd_kcontrol_chip(kcontrol);
1742 get_audio_meters(chip, ucontrol->value.integer.value);
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},
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)
1763 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1765 uinfo->value.integer.min = 0;
1766 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1770 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1771 struct snd_ctl_elem_value *ucontrol)
1773 struct echoaudio *chip;
1774 int detected, clocks, bit, src;
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);
1783 /* Compute the bitmask of the currently valid input clocks */
1784 detected = detect_input_clocks(chip);
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]) {
1794 ucontrol->value.integer.value[5] = clocks;
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,
1810 /******************************************************************************
1812 ******************************************************************************/
1813 /* Check if a period has elapsed since last interrupt
1815 * Don't make any updates to state; PCM core handles this with the
1818 * \return true if a period has elapsed, otherwise false
1820 static bool period_has_elapsed(struct snd_pcm_substream *substream)
1822 struct snd_pcm_runtime *runtime = substream->runtime;
1823 struct audiopipe *pipe = runtime->private_data;
1825 size_t period_bytes;
1827 if (pipe->state != PIPE_STATE_STARTED)
1830 period_bytes = frames_to_bytes(runtime, runtime->period_size);
1832 counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
1834 step = counter - pipe->last_period; /* handles wrapping */
1835 step -= step % period_bytes; /* acknowledge whole periods only */
1838 return false; /* haven't advanced a whole period yet */
1840 pipe->last_period += step; /* used exclusively by us */
1844 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1846 struct echoaudio *chip = dev_id;
1849 spin_lock(&chip->lock);
1850 st = service_irq(chip);
1852 spin_unlock(&chip->lock);
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;
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);
1867 spin_unlock(&chip->lock);
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);
1881 /******************************************************************************
1882 Module construction / destruction
1883 ******************************************************************************/
1885 static void snd_echo_free(struct snd_card *card)
1887 struct echoaudio *chip = card->private_data;
1889 if (chip->comm_page)
1890 rest_in_peace(chip);
1893 free_irq(chip->irq, chip);
1895 /* release chip data */
1896 free_firmware_cache(chip);
1899 /* <--snd_echo_probe() */
1900 static int snd_echo_create(struct snd_card *card,
1901 struct pci_dev *pci)
1903 struct echoaudio *chip = card->private_data;
1907 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1909 err = pcim_enable_device(pci);
1912 pci_set_master(pci);
1914 /* Allocate chip if needed */
1915 spin_lock_init(&chip->lock);
1919 chip->opencount = 0;
1920 mutex_init(&chip->mode_mutex);
1921 chip->can_set_rate = 1;
1923 /* PCI resource allocation */
1924 err = pci_request_regions(pci, ECHOCARD_NAME);
1928 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929 sz = pci_resource_len(pci, 0);
1931 sz = PAGE_SIZE; /* We map only the required part */
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");
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");
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);
1949 card->private_free = snd_echo_free;
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)
1958 chip->comm_page_phys = chip->commpage_dma_buf->addr;
1959 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1961 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1963 err = set_mixer_defaults(chip);
1965 dev_err(card->dev, "init_hw err=%d\n", err);
1973 static int __snd_echo_probe(struct pci_dev *pci,
1974 const struct pci_device_id *pci_id)
1977 struct snd_card *card;
1978 struct echoaudio *chip;
1980 __maybe_unused int i;
1983 if (dev >= SNDRV_CARDS)
1991 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1992 sizeof(*chip), &card);
1995 chip = card->private_data;
1997 err = snd_echo_create(card, pci);
2001 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2002 strcpy(card->shortname, chip->card_name);
2005 if (pci_id->device == 0x3410)
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);
2012 err = snd_echo_new_pcm(chip);
2014 dev_err(chip->card->dev, "new pcm error %d\n", err);
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);
2022 dev_err(chip->card->dev, "new midi error %d\n", err);
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));
2033 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2034 err = snd_ctl_add(chip->card,
2035 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2039 #else /* ECHOCARD_HAS_VMIXER */
2040 err = snd_ctl_add(chip->card,
2041 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2044 #endif /* ECHOCARD_HAS_VMIXER */
2046 #ifdef ECHOCARD_HAS_INPUT_GAIN
2047 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
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));
2060 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2061 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2066 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2070 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
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));
2081 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2082 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2087 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
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;
2098 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2101 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
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;
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);
2116 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2118 #ifdef ECHOCARD_HAS_DIGITAL_IO
2119 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
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));
2132 err = snd_card_register(card);
2135 dev_info(card->dev, "Card registered: %s\n", card->longname);
2137 pci_set_drvdata(pci, chip);
2142 static int snd_echo_probe(struct pci_dev *pci,
2143 const struct pci_device_id *pci_id)
2145 return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2149 #if defined(CONFIG_PM_SLEEP)
2151 static int snd_echo_suspend(struct device *dev)
2153 struct echoaudio *chip = dev_get_drvdata(dev);
2155 #ifdef ECHOCARD_HAS_MIDI
2156 /* This call can sleep */
2158 snd_echo_midi_output_trigger(chip->midi_out, 0);
2160 spin_lock_irq(&chip->lock);
2161 if (wait_handshake(chip)) {
2162 spin_unlock_irq(&chip->lock);
2165 clear_handshake(chip);
2166 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2167 spin_unlock_irq(&chip->lock);
2170 spin_unlock_irq(&chip->lock);
2172 chip->dsp_code = NULL;
2173 free_irq(chip->irq, chip);
2175 chip->card->sync_irq = -1;
2181 static int snd_echo_resume(struct device *dev)
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;
2189 commpage = chip->comm_page;
2190 commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2191 if (commpage_bak == NULL)
2194 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2196 kfree(commpage_bak);
2197 dev_err(dev, "resume init_hw err=%d\n", err);
2201 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2202 * restore_dsp_settings() fails.
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;
2209 kfree(commpage_bak);
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);
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");
2226 chip->irq = pci->irq;
2227 chip->card->sync_irq = chip->irq;
2228 dev_dbg(dev, "resume irq=%d\n", chip->irq);
2230 #ifdef ECHOCARD_HAS_MIDI
2231 if (chip->midi_input_enabled)
2232 enable_midi_input(chip, true);
2234 snd_echo_midi_output_trigger(chip->midi_out, 1);
2240 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2241 #define SND_ECHO_PM_OPS &snd_echo_pm
2243 #define SND_ECHO_PM_OPS NULL
2244 #endif /* CONFIG_PM_SLEEP */
2246 /******************************************************************************
2247 Everything starts and ends here
2248 ******************************************************************************/
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,
2256 .pm = SND_ECHO_PM_OPS,
2260 module_pci_driver(echo_driver);