2 * ALSA driver for Echoaudio soundcards.
3 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <linux/module.h>
21 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
22 MODULE_LICENSE("GPL v2");
23 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
24 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
25 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
27 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
28 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
29 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
31 module_param_array(index, int, NULL, 0444);
32 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
33 module_param_array(id, charp, NULL, 0444);
34 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
35 module_param_array(enable, bool, NULL, 0444);
36 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
38 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
39 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
43 static int get_firmware(const struct firmware **fw_entry,
44 struct echoaudio *chip, const short fw_index)
49 #ifdef CONFIG_PM_SLEEP
50 if (chip->fw_cache[fw_index]) {
51 dev_dbg(chip->card->dev,
52 "firmware requested: %s is cached\n",
53 card_fw[fw_index].data);
54 *fw_entry = chip->fw_cache[fw_index];
59 dev_dbg(chip->card->dev,
60 "firmware requested: %s\n", card_fw[fw_index].data);
61 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
62 err = request_firmware(fw_entry, name, pci_device(chip));
64 dev_err(chip->card->dev,
65 "get_firmware(): Firmware not available (%d)\n", err);
66 #ifdef CONFIG_PM_SLEEP
68 chip->fw_cache[fw_index] = *fw_entry;
75 static void free_firmware(const struct firmware *fw_entry,
76 struct echoaudio *chip)
78 #ifdef CONFIG_PM_SLEEP
79 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
81 release_firmware(fw_entry);
82 dev_dbg(chip->card->dev, "firmware released\n");
88 static void free_firmware_cache(struct echoaudio *chip)
90 #ifdef CONFIG_PM_SLEEP
93 for (i = 0; i < 8 ; i++)
94 if (chip->fw_cache[i]) {
95 release_firmware(chip->fw_cache[i]);
96 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
99 dev_dbg(chip->card->dev, "firmware_cache released\n");
105 /******************************************************************************
107 ******************************************************************************/
109 static void audiopipe_free(struct snd_pcm_runtime *runtime)
111 struct audiopipe *pipe = runtime->private_data;
113 if (pipe->sgpage.area)
114 snd_dma_free_pages(&pipe->sgpage);
120 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
121 struct snd_pcm_hw_rule *rule)
123 struct snd_interval *c = hw_param_interval(params,
124 SNDRV_PCM_HW_PARAM_CHANNELS);
125 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
130 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
131 /* >=2 channels cannot be S32_BE */
133 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
134 return snd_mask_refine(f, &fmt);
137 /* > 2 channels cannot be U8 and S32_BE */
139 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
140 return snd_mask_refine(f, &fmt);
142 /* Mono is ok with any format */
148 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
149 struct snd_pcm_hw_rule *rule)
151 struct snd_interval *c = hw_param_interval(params,
152 SNDRV_PCM_HW_PARAM_CHANNELS);
153 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
154 struct snd_interval ch;
156 snd_interval_any(&ch);
158 /* S32_BE is mono (and stereo) only */
159 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
161 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
167 return snd_interval_refine(c, &ch);
169 /* U8 can be only mono or stereo */
170 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
174 return snd_interval_refine(c, &ch);
176 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
182 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
183 struct snd_pcm_hw_rule *rule)
185 struct snd_interval *c = hw_param_interval(params,
186 SNDRV_PCM_HW_PARAM_CHANNELS);
187 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
192 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
194 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
196 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
197 SNDRV_PCM_FMTBIT_S24_3LE |
198 SNDRV_PCM_FMTBIT_S32_LE;
199 /* 1 channel must be S32_BE or S32_LE */
200 } else if (c->max == 1)
201 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
202 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
203 /* 2 channels cannot be S32_BE */
204 else if (c->min == 2 && c->max == 2)
205 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
210 fmt.bits[0] &= (u32)fmask;
211 fmt.bits[1] &= (u32)(fmask >> 32);
212 return snd_mask_refine(f, &fmt);
217 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
218 struct snd_pcm_hw_rule *rule)
220 struct snd_interval *c = hw_param_interval(params,
221 SNDRV_PCM_HW_PARAM_CHANNELS);
222 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
223 struct snd_interval ch;
226 snd_interval_any(&ch);
228 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
230 /* S32_BE is mono (and stereo) only */
231 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
233 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
238 /* U8 is stereo only */
239 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
241 /* S16_LE and S24_3LE must be at least stereo */
242 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
243 SNDRV_PCM_FMTBIT_S24_3LE)))
248 return snd_interval_refine(c, &ch);
253 /* Since the sample rate is a global setting, do allow the user to change the
254 sample rate only if there is only one pcm device open. */
255 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
256 struct snd_pcm_hw_rule *rule)
258 struct snd_interval *rate = hw_param_interval(params,
259 SNDRV_PCM_HW_PARAM_RATE);
260 struct echoaudio *chip = rule->private;
261 struct snd_interval fixed;
263 if (!chip->can_set_rate) {
264 snd_interval_any(&fixed);
265 fixed.min = fixed.max = chip->sample_rate;
266 return snd_interval_refine(rate, &fixed);
272 static int pcm_open(struct snd_pcm_substream *substream,
273 signed char max_channels)
275 struct echoaudio *chip;
276 struct snd_pcm_runtime *runtime;
277 struct audiopipe *pipe;
280 if (max_channels <= 0)
283 chip = snd_pcm_substream_chip(substream);
284 runtime = substream->runtime;
286 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
289 pipe->index = -1; /* Not configured yet */
291 /* Set up hw capabilities and contraints */
292 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
293 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
294 pipe->constr.list = channels_list;
295 pipe->constr.mask = 0;
296 for (i = 0; channels_list[i] <= max_channels; i++);
297 pipe->constr.count = i;
298 if (pipe->hw.channels_max > max_channels)
299 pipe->hw.channels_max = max_channels;
300 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
301 pipe->hw.rate_max = 48000;
302 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
305 runtime->hw = pipe->hw;
306 runtime->private_data = pipe;
307 runtime->private_free = audiopipe_free;
308 snd_pcm_set_sync(substream);
310 /* Only mono and any even number of channels are allowed */
311 if ((err = snd_pcm_hw_constraint_list(runtime, 0,
312 SNDRV_PCM_HW_PARAM_CHANNELS,
316 /* All periods should have the same size */
317 if ((err = snd_pcm_hw_constraint_integer(runtime,
318 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
321 /* The hw accesses memory in chunks 32 frames long and they should be
322 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
323 generated with a resolution of 32 frames. Thus we need the following */
324 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
325 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
328 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
329 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
333 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
334 SNDRV_PCM_HW_PARAM_RATE,
335 hw_rule_sample_rate, chip,
336 SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
339 /* Finally allocate a page for the scatter-gather list */
340 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
341 snd_dma_pci_data(chip->pci),
342 PAGE_SIZE, &pipe->sgpage)) < 0) {
343 dev_err(chip->card->dev, "s-g list allocation failed\n");
352 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
354 struct echoaudio *chip = snd_pcm_substream_chip(substream);
357 dev_dbg(chip->card->dev, "pcm_analog_in_open\n");
358 if ((err = pcm_open(substream, num_analog_busses_in(chip) -
359 substream->number)) < 0)
361 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
362 SNDRV_PCM_HW_PARAM_CHANNELS,
363 hw_rule_capture_channels_by_format, NULL,
364 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
366 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
367 SNDRV_PCM_HW_PARAM_FORMAT,
368 hw_rule_capture_format_by_channels, NULL,
369 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
371 atomic_inc(&chip->opencount);
372 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
373 chip->can_set_rate=0;
374 dev_dbg(chip->card->dev, "pcm_analog_in_open cs=%d oc=%d r=%d\n",
375 chip->can_set_rate, atomic_read(&chip->opencount),
382 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
384 struct echoaudio *chip = snd_pcm_substream_chip(substream);
385 int max_channels, err;
387 #ifdef ECHOCARD_HAS_VMIXER
388 max_channels = num_pipes_out(chip);
390 max_channels = num_analog_busses_out(chip);
392 dev_dbg(chip->card->dev, "pcm_analog_out_open\n");
393 if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
395 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
396 SNDRV_PCM_HW_PARAM_CHANNELS,
397 hw_rule_playback_channels_by_format,
399 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
401 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
402 SNDRV_PCM_HW_PARAM_FORMAT,
403 hw_rule_playback_format_by_channels,
405 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
407 atomic_inc(&chip->opencount);
408 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
409 chip->can_set_rate=0;
410 dev_dbg(chip->card->dev, "pcm_analog_out_open cs=%d oc=%d r=%d\n",
411 chip->can_set_rate, atomic_read(&chip->opencount),
418 #ifdef ECHOCARD_HAS_DIGITAL_IO
420 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
422 struct echoaudio *chip = snd_pcm_substream_chip(substream);
423 int err, max_channels;
425 dev_dbg(chip->card->dev, "pcm_digital_in_open\n");
426 max_channels = num_digital_busses_in(chip) - substream->number;
427 mutex_lock(&chip->mode_mutex);
428 if (chip->digital_mode == DIGITAL_MODE_ADAT)
429 err = pcm_open(substream, max_channels);
430 else /* If the card has ADAT, subtract the 6 channels
431 * that S/PDIF doesn't have
433 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
438 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
439 SNDRV_PCM_HW_PARAM_CHANNELS,
440 hw_rule_capture_channels_by_format, NULL,
441 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
443 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
444 SNDRV_PCM_HW_PARAM_FORMAT,
445 hw_rule_capture_format_by_channels, NULL,
446 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
449 atomic_inc(&chip->opencount);
450 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
451 chip->can_set_rate=0;
454 mutex_unlock(&chip->mode_mutex);
460 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
462 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
464 struct echoaudio *chip = snd_pcm_substream_chip(substream);
465 int err, max_channels;
467 dev_dbg(chip->card->dev, "pcm_digital_out_open\n");
468 max_channels = num_digital_busses_out(chip) - substream->number;
469 mutex_lock(&chip->mode_mutex);
470 if (chip->digital_mode == DIGITAL_MODE_ADAT)
471 err = pcm_open(substream, max_channels);
472 else /* If the card has ADAT, subtract the 6 channels
473 * that S/PDIF doesn't have
475 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
480 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
481 SNDRV_PCM_HW_PARAM_CHANNELS,
482 hw_rule_playback_channels_by_format,
483 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
486 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
487 SNDRV_PCM_HW_PARAM_FORMAT,
488 hw_rule_playback_format_by_channels,
489 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
492 atomic_inc(&chip->opencount);
493 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
494 chip->can_set_rate=0;
496 mutex_unlock(&chip->mode_mutex);
500 #endif /* !ECHOCARD_HAS_VMIXER */
502 #endif /* ECHOCARD_HAS_DIGITAL_IO */
506 static int pcm_close(struct snd_pcm_substream *substream)
508 struct echoaudio *chip = snd_pcm_substream_chip(substream);
511 /* Nothing to do here. Audio is already off and pipe will be
512 * freed by its callback
514 dev_dbg(chip->card->dev, "pcm_close\n");
516 atomic_dec(&chip->opencount);
517 oc = atomic_read(&chip->opencount);
518 dev_dbg(chip->card->dev, "pcm_close oc=%d cs=%d rs=%d\n", oc,
519 chip->can_set_rate, chip->rate_set);
521 chip->can_set_rate = 1;
524 dev_dbg(chip->card->dev, "pcm_close2 oc=%d cs=%d rs=%d\n", oc,
525 chip->can_set_rate, chip->rate_set);
532 /* Channel allocation and scatter-gather list setup */
533 static int init_engine(struct snd_pcm_substream *substream,
534 struct snd_pcm_hw_params *hw_params,
535 int pipe_index, int interleave)
537 struct echoaudio *chip;
538 int err, per, rest, page, edge, offs;
539 struct audiopipe *pipe;
541 chip = snd_pcm_substream_chip(substream);
542 pipe = (struct audiopipe *) substream->runtime->private_data;
544 /* Sets up che hardware. If it's already initialized, reset and
545 * redo with the new parameters
547 spin_lock_irq(&chip->lock);
548 if (pipe->index >= 0) {
549 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
550 err = free_pipes(chip, pipe);
552 chip->substream[pipe->index] = NULL;
555 err = allocate_pipes(chip, pipe, pipe_index, interleave);
557 spin_unlock_irq(&chip->lock);
558 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
562 spin_unlock_irq(&chip->lock);
563 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
565 dev_dbg(chip->card->dev,
566 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
567 params_buffer_bytes(hw_params), params_periods(hw_params),
568 params_period_bytes(hw_params));
569 err = snd_pcm_lib_malloc_pages(substream,
570 params_buffer_bytes(hw_params));
572 dev_err(chip->card->dev, "malloc_pages err=%d\n", err);
573 spin_lock_irq(&chip->lock);
574 free_pipes(chip, pipe);
575 spin_unlock_irq(&chip->lock);
580 sglist_init(chip, pipe);
582 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
584 rest = params_period_bytes(hw_params);
585 if (offs + rest > params_buffer_bytes(hw_params))
586 rest = params_buffer_bytes(hw_params) - offs;
589 addr = snd_pcm_sgbuf_get_addr(substream, offs);
590 if (rest <= edge - offs) {
591 sglist_add_mapping(chip, pipe, addr, rest);
592 sglist_add_irq(chip, pipe);
596 sglist_add_mapping(chip, pipe, addr,
608 /* Close the ring buffer */
609 sglist_wrap(chip, pipe);
611 /* This stuff is used by the irq handler, so it must be
612 * initialized before chip->substream
614 chip->last_period[pipe_index] = 0;
615 pipe->last_counter = 0;
618 chip->substream[pipe_index] = substream;
620 spin_lock_irq(&chip->lock);
621 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
622 spin_unlock_irq(&chip->lock);
623 dev_dbg(chip->card->dev, "pcm_hw_params ok\n");
629 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
630 struct snd_pcm_hw_params *hw_params)
632 struct echoaudio *chip = snd_pcm_substream_chip(substream);
634 return init_engine(substream, hw_params, px_analog_in(chip) +
635 substream->number, params_channels(hw_params));
640 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
641 struct snd_pcm_hw_params *hw_params)
643 return init_engine(substream, hw_params, substream->number,
644 params_channels(hw_params));
649 #ifdef ECHOCARD_HAS_DIGITAL_IO
651 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
652 struct snd_pcm_hw_params *hw_params)
654 struct echoaudio *chip = snd_pcm_substream_chip(substream);
656 return init_engine(substream, hw_params, px_digital_in(chip) +
657 substream->number, params_channels(hw_params));
662 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
663 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
664 struct snd_pcm_hw_params *hw_params)
666 struct echoaudio *chip = snd_pcm_substream_chip(substream);
668 return init_engine(substream, hw_params, px_digital_out(chip) +
669 substream->number, params_channels(hw_params));
671 #endif /* !ECHOCARD_HAS_VMIXER */
673 #endif /* ECHOCARD_HAS_DIGITAL_IO */
677 static int pcm_hw_free(struct snd_pcm_substream *substream)
679 struct echoaudio *chip;
680 struct audiopipe *pipe;
682 chip = snd_pcm_substream_chip(substream);
683 pipe = (struct audiopipe *) substream->runtime->private_data;
685 spin_lock_irq(&chip->lock);
686 if (pipe->index >= 0) {
687 dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
688 free_pipes(chip, pipe);
689 chip->substream[pipe->index] = NULL;
692 spin_unlock_irq(&chip->lock);
694 dev_dbg(chip->card->dev, "pcm_hw_freed\n");
695 snd_pcm_lib_free_pages(substream);
701 static int pcm_prepare(struct snd_pcm_substream *substream)
703 struct echoaudio *chip = snd_pcm_substream_chip(substream);
704 struct snd_pcm_runtime *runtime = substream->runtime;
705 struct audioformat format;
706 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
708 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
709 runtime->rate, runtime->format, runtime->channels);
710 format.interleave = runtime->channels;
711 format.data_are_bigendian = 0;
712 format.mono_to_stereo = 0;
713 switch (runtime->format) {
714 case SNDRV_PCM_FORMAT_U8:
715 format.bits_per_sample = 8;
717 case SNDRV_PCM_FORMAT_S16_LE:
718 format.bits_per_sample = 16;
720 case SNDRV_PCM_FORMAT_S24_3LE:
721 format.bits_per_sample = 24;
723 case SNDRV_PCM_FORMAT_S32_BE:
724 format.data_are_bigendian = 1;
725 case SNDRV_PCM_FORMAT_S32_LE:
726 format.bits_per_sample = 32;
729 dev_err(chip->card->dev,
730 "Prepare error: unsupported format %d\n",
735 if (snd_BUG_ON(pipe_index >= px_num(chip)))
737 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
739 set_audio_format(chip, pipe_index, &format);
745 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
747 struct echoaudio *chip = snd_pcm_substream_chip(substream);
748 struct snd_pcm_runtime *runtime = substream->runtime;
749 struct audiopipe *pipe = runtime->private_data;
752 struct snd_pcm_substream *s;
754 snd_pcm_group_for_each_entry(s, substream) {
755 for (i = 0; i < DSP_MAXPIPES; i++) {
756 if (s == chip->substream[i]) {
757 channelmask |= 1 << i;
758 snd_pcm_trigger_done(s, substream);
763 spin_lock(&chip->lock);
765 case SNDRV_PCM_TRIGGER_RESUME:
766 dev_dbg(chip->card->dev, "pcm_trigger resume\n");
767 case SNDRV_PCM_TRIGGER_START:
768 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
769 dev_dbg(chip->card->dev, "pcm_trigger start\n");
770 for (i = 0; i < DSP_MAXPIPES; i++) {
771 if (channelmask & (1 << i)) {
772 pipe = chip->substream[i]->runtime->private_data;
773 switch (pipe->state) {
774 case PIPE_STATE_STOPPED:
775 chip->last_period[i] = 0;
776 pipe->last_counter = 0;
778 *pipe->dma_counter = 0;
779 case PIPE_STATE_PAUSED:
780 pipe->state = PIPE_STATE_STARTED;
782 case PIPE_STATE_STARTED:
787 err = start_transport(chip, channelmask,
788 chip->pipe_cyclic_mask);
790 case SNDRV_PCM_TRIGGER_SUSPEND:
791 dev_dbg(chip->card->dev, "pcm_trigger suspend\n");
792 case SNDRV_PCM_TRIGGER_STOP:
793 dev_dbg(chip->card->dev, "pcm_trigger stop\n");
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 dev_dbg(chip->card->dev, "pcm_trigger pause\n");
804 for (i = 0; i < DSP_MAXPIPES; i++) {
805 if (channelmask & (1 << i)) {
806 pipe = chip->substream[i]->runtime->private_data;
807 pipe->state = PIPE_STATE_PAUSED;
810 err = pause_transport(chip, channelmask);
815 spin_unlock(&chip->lock);
821 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
823 struct snd_pcm_runtime *runtime = substream->runtime;
824 struct audiopipe *pipe = runtime->private_data;
825 size_t cnt, bufsize, pos;
827 cnt = le32_to_cpu(*pipe->dma_counter);
828 pipe->position += cnt - pipe->last_counter;
829 pipe->last_counter = cnt;
830 bufsize = substream->runtime->buffer_size;
831 pos = bytes_to_frames(substream->runtime, pipe->position);
833 while (pos >= bufsize) {
834 pipe->position -= frames_to_bytes(substream->runtime, bufsize);
842 /* pcm *_ops structures */
843 static struct snd_pcm_ops analog_playback_ops = {
844 .open = pcm_analog_out_open,
846 .ioctl = snd_pcm_lib_ioctl,
847 .hw_params = pcm_analog_out_hw_params,
848 .hw_free = pcm_hw_free,
849 .prepare = pcm_prepare,
850 .trigger = pcm_trigger,
851 .pointer = pcm_pointer,
852 .page = snd_pcm_sgbuf_ops_page,
854 static struct snd_pcm_ops analog_capture_ops = {
855 .open = pcm_analog_in_open,
857 .ioctl = snd_pcm_lib_ioctl,
858 .hw_params = pcm_analog_in_hw_params,
859 .hw_free = pcm_hw_free,
860 .prepare = pcm_prepare,
861 .trigger = pcm_trigger,
862 .pointer = pcm_pointer,
863 .page = snd_pcm_sgbuf_ops_page,
865 #ifdef ECHOCARD_HAS_DIGITAL_IO
866 #ifndef ECHOCARD_HAS_VMIXER
867 static struct snd_pcm_ops digital_playback_ops = {
868 .open = pcm_digital_out_open,
870 .ioctl = snd_pcm_lib_ioctl,
871 .hw_params = pcm_digital_out_hw_params,
872 .hw_free = pcm_hw_free,
873 .prepare = pcm_prepare,
874 .trigger = pcm_trigger,
875 .pointer = pcm_pointer,
876 .page = snd_pcm_sgbuf_ops_page,
878 #endif /* !ECHOCARD_HAS_VMIXER */
879 static struct snd_pcm_ops digital_capture_ops = {
880 .open = pcm_digital_in_open,
882 .ioctl = snd_pcm_lib_ioctl,
883 .hw_params = pcm_digital_in_hw_params,
884 .hw_free = pcm_hw_free,
885 .prepare = pcm_prepare,
886 .trigger = pcm_trigger,
887 .pointer = pcm_pointer,
888 .page = snd_pcm_sgbuf_ops_page,
890 #endif /* ECHOCARD_HAS_DIGITAL_IO */
894 /* Preallocate memory only for the first substream because it's the most
897 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
899 struct snd_pcm_substream *ss;
902 for (stream = 0; stream < 2; stream++)
903 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) {
904 err = snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
906 ss->number ? 0 : 128<<10,
916 /*<--snd_echo_probe() */
917 static int snd_echo_new_pcm(struct echoaudio *chip)
922 #ifdef ECHOCARD_HAS_VMIXER
923 /* This card has a Vmixer, that is there is no direct mapping from PCM
924 streams to physical outputs. The user can mix the streams as he wishes
925 via control interface and it's possible to send any stream to any
926 output, thus it makes no sense to keep analog and digital outputs
929 /* PCM#0 Virtual outputs and analog inputs */
930 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
931 num_analog_busses_in(chip), &pcm)) < 0)
933 pcm->private_data = chip;
934 chip->analog_pcm = pcm;
935 strcpy(pcm->name, chip->card->shortname);
936 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
937 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
938 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
940 dev_dbg(chip->card->dev, "Analog PCM ok\n");
942 #ifdef ECHOCARD_HAS_DIGITAL_IO
943 /* PCM#1 Digital inputs, no outputs */
944 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
945 num_digital_busses_in(chip), &pcm)) < 0)
947 pcm->private_data = chip;
948 chip->digital_pcm = pcm;
949 strcpy(pcm->name, chip->card->shortname);
950 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
951 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
953 dev_dbg(chip->card->dev, "Digital PCM ok\n");
954 #endif /* ECHOCARD_HAS_DIGITAL_IO */
956 #else /* ECHOCARD_HAS_VMIXER */
958 /* The card can manage substreams formed by analog and digital channels
959 at the same time, but I prefer to keep analog and digital channels
960 separated, because that mixed thing is confusing and useless. So we
961 register two PCM devices: */
963 /* PCM#0 Analog i/o */
964 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
965 num_analog_busses_out(chip),
966 num_analog_busses_in(chip), &pcm)) < 0)
968 pcm->private_data = chip;
969 chip->analog_pcm = pcm;
970 strcpy(pcm->name, chip->card->shortname);
971 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
972 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
973 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
975 dev_dbg(chip->card->dev, "Analog PCM ok\n");
977 #ifdef ECHOCARD_HAS_DIGITAL_IO
978 /* PCM#1 Digital i/o */
979 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
980 num_digital_busses_out(chip),
981 num_digital_busses_in(chip), &pcm)) < 0)
983 pcm->private_data = chip;
984 chip->digital_pcm = pcm;
985 strcpy(pcm->name, chip->card->shortname);
986 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
987 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
988 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
990 dev_dbg(chip->card->dev, "Digital PCM ok\n");
991 #endif /* ECHOCARD_HAS_DIGITAL_IO */
993 #endif /* ECHOCARD_HAS_VMIXER */
1001 /******************************************************************************
1003 ******************************************************************************/
1005 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
1007 /******************* PCM output volume *******************/
1008 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_info *uinfo)
1011 struct echoaudio *chip;
1013 chip = snd_kcontrol_chip(kcontrol);
1014 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1015 uinfo->count = num_busses_out(chip);
1016 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1017 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1021 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1022 struct snd_ctl_elem_value *ucontrol)
1024 struct echoaudio *chip;
1027 chip = snd_kcontrol_chip(kcontrol);
1028 for (c = 0; c < num_busses_out(chip); c++)
1029 ucontrol->value.integer.value[c] = chip->output_gain[c];
1033 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1034 struct snd_ctl_elem_value *ucontrol)
1036 struct echoaudio *chip;
1037 int c, changed, gain;
1040 chip = snd_kcontrol_chip(kcontrol);
1041 spin_lock_irq(&chip->lock);
1042 for (c = 0; c < num_busses_out(chip); c++) {
1043 gain = ucontrol->value.integer.value[c];
1044 /* Ignore out of range values */
1045 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1047 if (chip->output_gain[c] != gain) {
1048 set_output_gain(chip, c, gain);
1053 update_output_line_level(chip);
1054 spin_unlock_irq(&chip->lock);
1058 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1059 /* On the Mia this one controls the line-out volume */
1060 static struct snd_kcontrol_new snd_echo_line_output_gain = {
1061 .name = "Line Playback Volume",
1062 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1063 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1064 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},
1071 static struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1072 .name = "PCM Playback Volume",
1073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1074 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1075 .info = snd_echo_output_gain_info,
1076 .get = snd_echo_output_gain_get,
1077 .put = snd_echo_output_gain_put,
1078 .tlv = {.p = db_scale_output_gain},
1082 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1086 #ifdef ECHOCARD_HAS_INPUT_GAIN
1088 /******************* Analog input volume *******************/
1089 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1090 struct snd_ctl_elem_info *uinfo)
1092 struct echoaudio *chip;
1094 chip = snd_kcontrol_chip(kcontrol);
1095 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1096 uinfo->count = num_analog_busses_in(chip);
1097 uinfo->value.integer.min = ECHOGAIN_MININP;
1098 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1102 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1103 struct snd_ctl_elem_value *ucontrol)
1105 struct echoaudio *chip;
1108 chip = snd_kcontrol_chip(kcontrol);
1109 for (c = 0; c < num_analog_busses_in(chip); c++)
1110 ucontrol->value.integer.value[c] = chip->input_gain[c];
1114 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1115 struct snd_ctl_elem_value *ucontrol)
1117 struct echoaudio *chip;
1118 int c, gain, changed;
1121 chip = snd_kcontrol_chip(kcontrol);
1122 spin_lock_irq(&chip->lock);
1123 for (c = 0; c < num_analog_busses_in(chip); c++) {
1124 gain = ucontrol->value.integer.value[c];
1125 /* Ignore out of range values */
1126 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1128 if (chip->input_gain[c] != gain) {
1129 set_input_gain(chip, c, gain);
1134 update_input_line_level(chip);
1135 spin_unlock_irq(&chip->lock);
1139 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1141 static struct snd_kcontrol_new snd_echo_line_input_gain = {
1142 .name = "Line Capture Volume",
1143 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1144 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1145 .info = snd_echo_input_gain_info,
1146 .get = snd_echo_input_gain_get,
1147 .put = snd_echo_input_gain_put,
1148 .tlv = {.p = db_scale_input_gain},
1151 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1155 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1157 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1158 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1159 struct snd_ctl_elem_info *uinfo)
1161 struct echoaudio *chip;
1163 chip = snd_kcontrol_chip(kcontrol);
1164 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1165 uinfo->count = num_analog_busses_out(chip);
1166 uinfo->value.integer.min = 0;
1167 uinfo->value.integer.max = 1;
1171 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1172 struct snd_ctl_elem_value *ucontrol)
1174 struct echoaudio *chip;
1177 chip = snd_kcontrol_chip(kcontrol);
1178 for (c = 0; c < num_analog_busses_out(chip); c++)
1179 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1183 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1184 struct snd_ctl_elem_value *ucontrol)
1186 struct echoaudio *chip;
1190 chip = snd_kcontrol_chip(kcontrol);
1191 spin_lock_irq(&chip->lock);
1192 for (c = 0; c < num_analog_busses_out(chip); c++) {
1193 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1194 set_nominal_level(chip, c,
1195 ucontrol->value.integer.value[c]);
1200 update_output_line_level(chip);
1201 spin_unlock_irq(&chip->lock);
1205 static struct snd_kcontrol_new snd_echo_output_nominal_level = {
1206 .name = "Line Playback Switch (-10dBV)",
1207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1208 .info = snd_echo_output_nominal_info,
1209 .get = snd_echo_output_nominal_get,
1210 .put = snd_echo_output_nominal_put,
1213 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1217 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1219 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1220 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1221 struct snd_ctl_elem_info *uinfo)
1223 struct echoaudio *chip;
1225 chip = snd_kcontrol_chip(kcontrol);
1226 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1227 uinfo->count = num_analog_busses_in(chip);
1228 uinfo->value.integer.min = 0;
1229 uinfo->value.integer.max = 1;
1233 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1234 struct snd_ctl_elem_value *ucontrol)
1236 struct echoaudio *chip;
1239 chip = snd_kcontrol_chip(kcontrol);
1240 for (c = 0; c < num_analog_busses_in(chip); c++)
1241 ucontrol->value.integer.value[c] =
1242 chip->nominal_level[bx_analog_in(chip) + c];
1246 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1247 struct snd_ctl_elem_value *ucontrol)
1249 struct echoaudio *chip;
1253 chip = snd_kcontrol_chip(kcontrol);
1254 spin_lock_irq(&chip->lock);
1255 for (c = 0; c < num_analog_busses_in(chip); c++) {
1256 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1257 ucontrol->value.integer.value[c]) {
1258 set_nominal_level(chip, bx_analog_in(chip) + c,
1259 ucontrol->value.integer.value[c]);
1264 update_output_line_level(chip); /* "Output" is not a mistake
1267 spin_unlock_irq(&chip->lock);
1271 static struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1272 .name = "Line Capture Switch (-10dBV)",
1273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1274 .info = snd_echo_input_nominal_info,
1275 .get = snd_echo_input_nominal_get,
1276 .put = snd_echo_input_nominal_put,
1279 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1283 #ifdef ECHOCARD_HAS_MONITOR
1285 /******************* Monitor mixer *******************/
1286 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1287 struct snd_ctl_elem_info *uinfo)
1289 struct echoaudio *chip;
1291 chip = snd_kcontrol_chip(kcontrol);
1292 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1294 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1295 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1296 uinfo->dimen.d[0] = num_busses_out(chip);
1297 uinfo->dimen.d[1] = num_busses_in(chip);
1301 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_value *ucontrol)
1304 struct echoaudio *chip;
1306 chip = snd_kcontrol_chip(kcontrol);
1307 ucontrol->value.integer.value[0] =
1308 chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1309 [ucontrol->id.index % num_busses_in(chip)];
1313 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1314 struct snd_ctl_elem_value *ucontrol)
1316 struct echoaudio *chip;
1321 chip = snd_kcontrol_chip(kcontrol);
1322 out = ucontrol->id.index / num_busses_in(chip);
1323 in = ucontrol->id.index % num_busses_in(chip);
1324 gain = ucontrol->value.integer.value[0];
1325 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1327 if (chip->monitor_gain[out][in] != gain) {
1328 spin_lock_irq(&chip->lock);
1329 set_monitor_gain(chip, out, in, gain);
1330 update_output_line_level(chip);
1331 spin_unlock_irq(&chip->lock);
1337 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1338 .name = "Monitor Mixer Volume",
1339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1340 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1341 .info = snd_echo_mixer_info,
1342 .get = snd_echo_mixer_get,
1343 .put = snd_echo_mixer_put,
1344 .tlv = {.p = db_scale_output_gain},
1347 #endif /* ECHOCARD_HAS_MONITOR */
1351 #ifdef ECHOCARD_HAS_VMIXER
1353 /******************* Vmixer *******************/
1354 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_info *uinfo)
1357 struct echoaudio *chip;
1359 chip = snd_kcontrol_chip(kcontrol);
1360 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1362 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1363 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1364 uinfo->dimen.d[0] = num_busses_out(chip);
1365 uinfo->dimen.d[1] = num_pipes_out(chip);
1369 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1370 struct snd_ctl_elem_value *ucontrol)
1372 struct echoaudio *chip;
1374 chip = snd_kcontrol_chip(kcontrol);
1375 ucontrol->value.integer.value[0] =
1376 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1377 [ucontrol->id.index % num_pipes_out(chip)];
1381 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1382 struct snd_ctl_elem_value *ucontrol)
1384 struct echoaudio *chip;
1389 chip = snd_kcontrol_chip(kcontrol);
1390 out = ucontrol->id.index / num_pipes_out(chip);
1391 vch = ucontrol->id.index % num_pipes_out(chip);
1392 gain = ucontrol->value.integer.value[0];
1393 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1395 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1396 spin_lock_irq(&chip->lock);
1397 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1398 update_vmixer_level(chip);
1399 spin_unlock_irq(&chip->lock);
1405 static struct snd_kcontrol_new snd_echo_vmixer = {
1406 .name = "VMixer Volume",
1407 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1408 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1409 .info = snd_echo_vmixer_info,
1410 .get = snd_echo_vmixer_get,
1411 .put = snd_echo_vmixer_put,
1412 .tlv = {.p = db_scale_output_gain},
1415 #endif /* ECHOCARD_HAS_VMIXER */
1419 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1421 /******************* Digital mode switch *******************/
1422 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_info *uinfo)
1425 static const char * const names[4] = {
1426 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1429 struct echoaudio *chip;
1431 chip = snd_kcontrol_chip(kcontrol);
1432 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1435 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_value *ucontrol)
1438 struct echoaudio *chip;
1441 chip = snd_kcontrol_chip(kcontrol);
1442 mode = chip->digital_mode;
1443 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1444 if (mode == chip->digital_mode_list[i]) {
1445 ucontrol->value.enumerated.item[0] = i;
1451 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1454 struct echoaudio *chip;
1456 unsigned short emode, dmode;
1459 chip = snd_kcontrol_chip(kcontrol);
1461 emode = ucontrol->value.enumerated.item[0];
1462 if (emode >= chip->num_digital_modes)
1464 dmode = chip->digital_mode_list[emode];
1466 if (dmode != chip->digital_mode) {
1467 /* mode_mutex is required to make this operation atomic wrt
1468 pcm_digital_*_open() and set_input_clock() functions. */
1469 mutex_lock(&chip->mode_mutex);
1471 /* Do not allow the user to change the digital mode when a pcm
1472 device is open because it also changes the number of channels
1473 and the allowed sample rates */
1474 if (atomic_read(&chip->opencount)) {
1477 changed = set_digital_mode(chip, dmode);
1478 /* If we had to change the clock source, report it */
1479 if (changed > 0 && chip->clock_src_ctl) {
1480 snd_ctl_notify(chip->card,
1481 SNDRV_CTL_EVENT_MASK_VALUE,
1482 &chip->clock_src_ctl->id);
1483 dev_dbg(chip->card->dev,
1484 "SDM() =%d\n", changed);
1487 changed = 1; /* No errors */
1489 mutex_unlock(&chip->mode_mutex);
1494 static struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1495 .name = "Digital mode Switch",
1496 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1497 .info = snd_echo_digital_mode_info,
1498 .get = snd_echo_digital_mode_get,
1499 .put = snd_echo_digital_mode_put,
1502 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1506 #ifdef ECHOCARD_HAS_DIGITAL_IO
1508 /******************* S/PDIF mode switch *******************/
1509 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1510 struct snd_ctl_elem_info *uinfo)
1512 static const char * const names[2] = {"Consumer", "Professional"};
1514 return snd_ctl_enum_info(uinfo, 1, 2, names);
1517 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_value *ucontrol)
1520 struct echoaudio *chip;
1522 chip = snd_kcontrol_chip(kcontrol);
1523 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1527 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1528 struct snd_ctl_elem_value *ucontrol)
1530 struct echoaudio *chip;
1533 chip = snd_kcontrol_chip(kcontrol);
1534 mode = !!ucontrol->value.enumerated.item[0];
1535 if (mode != chip->professional_spdif) {
1536 spin_lock_irq(&chip->lock);
1537 set_professional_spdif(chip, mode);
1538 spin_unlock_irq(&chip->lock);
1544 static struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1545 .name = "S/PDIF mode Switch",
1546 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1547 .info = snd_echo_spdif_mode_info,
1548 .get = snd_echo_spdif_mode_get,
1549 .put = snd_echo_spdif_mode_put,
1552 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1556 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1558 /******************* Select input clock source *******************/
1559 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
1562 static const char * const names[8] = {
1563 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1566 struct echoaudio *chip;
1568 chip = snd_kcontrol_chip(kcontrol);
1569 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1572 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_value *ucontrol)
1575 struct echoaudio *chip;
1578 chip = snd_kcontrol_chip(kcontrol);
1579 clock = chip->input_clock;
1581 for (i = 0; i < chip->num_clock_sources; i++)
1582 if (clock == chip->clock_source_list[i])
1583 ucontrol->value.enumerated.item[0] = i;
1588 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
1591 struct echoaudio *chip;
1593 unsigned int eclock, dclock;
1596 chip = snd_kcontrol_chip(kcontrol);
1597 eclock = ucontrol->value.enumerated.item[0];
1598 if (eclock >= chip->input_clock_types)
1600 dclock = chip->clock_source_list[eclock];
1601 if (chip->input_clock != dclock) {
1602 mutex_lock(&chip->mode_mutex);
1603 spin_lock_irq(&chip->lock);
1604 if ((changed = set_input_clock(chip, dclock)) == 0)
1605 changed = 1; /* no errors */
1606 spin_unlock_irq(&chip->lock);
1607 mutex_unlock(&chip->mode_mutex);
1611 dev_dbg(chip->card->dev,
1612 "seticlk val%d err 0x%x\n", dclock, changed);
1617 static struct snd_kcontrol_new snd_echo_clock_source_switch = {
1618 .name = "Sample Clock Source",
1619 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1620 .info = snd_echo_clock_source_info,
1621 .get = snd_echo_clock_source_get,
1622 .put = snd_echo_clock_source_put,
1625 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1629 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1631 /******************* Phantom power switch *******************/
1632 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1634 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1635 struct snd_ctl_elem_value *ucontrol)
1637 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1639 ucontrol->value.integer.value[0] = chip->phantom_power;
1643 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
1646 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1647 int power, changed = 0;
1649 power = !!ucontrol->value.integer.value[0];
1650 if (chip->phantom_power != power) {
1651 spin_lock_irq(&chip->lock);
1652 changed = set_phantom_power(chip, power);
1653 spin_unlock_irq(&chip->lock);
1655 changed = 1; /* no errors */
1660 static struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1661 .name = "Phantom power Switch",
1662 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1663 .info = snd_echo_phantom_power_info,
1664 .get = snd_echo_phantom_power_get,
1665 .put = snd_echo_phantom_power_put,
1668 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1672 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1674 /******************* Digital input automute switch *******************/
1675 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1677 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
1680 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1682 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1686 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol)
1689 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1690 int automute, changed = 0;
1692 automute = !!ucontrol->value.integer.value[0];
1693 if (chip->digital_in_automute != automute) {
1694 spin_lock_irq(&chip->lock);
1695 changed = set_input_auto_mute(chip, automute);
1696 spin_unlock_irq(&chip->lock);
1698 changed = 1; /* no errors */
1703 static struct snd_kcontrol_new snd_echo_automute_switch = {
1704 .name = "Digital Capture Switch (automute)",
1705 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1706 .info = snd_echo_automute_info,
1707 .get = snd_echo_automute_get,
1708 .put = snd_echo_automute_put,
1711 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1715 /******************* VU-meters switch *******************/
1716 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1718 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1721 struct echoaudio *chip;
1723 chip = snd_kcontrol_chip(kcontrol);
1724 spin_lock_irq(&chip->lock);
1725 set_meters_on(chip, ucontrol->value.integer.value[0]);
1726 spin_unlock_irq(&chip->lock);
1730 static struct snd_kcontrol_new snd_echo_vumeters_switch = {
1731 .name = "VU-meters Switch",
1732 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1733 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1734 .info = snd_echo_vumeters_switch_info,
1735 .put = snd_echo_vumeters_switch_put,
1740 /***** Read VU-meters (input, output, analog and digital together) *****/
1741 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1742 struct snd_ctl_elem_info *uinfo)
1744 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1746 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1747 uinfo->value.integer.max = 0;
1748 #ifdef ECHOCARD_HAS_VMIXER
1749 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1751 uinfo->dimen.d[0] = 2; /* Out, In */
1753 uinfo->dimen.d[1] = 16; /* 16 channels */
1754 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1758 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1759 struct snd_ctl_elem_value *ucontrol)
1761 struct echoaudio *chip;
1763 chip = snd_kcontrol_chip(kcontrol);
1764 get_audio_meters(chip, ucontrol->value.integer.value);
1768 static struct snd_kcontrol_new snd_echo_vumeters = {
1769 .name = "VU-meters",
1770 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1771 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1772 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1773 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1774 .info = snd_echo_vumeters_info,
1775 .get = snd_echo_vumeters_get,
1776 .tlv = {.p = db_scale_output_gain},
1781 /*** Channels info - it exports informations about the number of channels ***/
1782 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1787 uinfo->value.integer.min = 0;
1788 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1792 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1793 struct snd_ctl_elem_value *ucontrol)
1795 struct echoaudio *chip;
1796 int detected, clocks, bit, src;
1798 chip = snd_kcontrol_chip(kcontrol);
1799 ucontrol->value.integer.value[0] = num_busses_in(chip);
1800 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1801 ucontrol->value.integer.value[2] = num_busses_out(chip);
1802 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1803 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1805 /* Compute the bitmask of the currently valid input clocks */
1806 detected = detect_input_clocks(chip);
1808 src = chip->num_clock_sources - 1;
1809 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1810 if (detected & (1 << bit))
1811 for (; src >= 0; src--)
1812 if (bit == chip->clock_source_list[src]) {
1816 ucontrol->value.integer.value[5] = clocks;
1821 static struct snd_kcontrol_new snd_echo_channels_info = {
1822 .name = "Channels info",
1823 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1824 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1825 .info = snd_echo_channels_info_info,
1826 .get = snd_echo_channels_info_get,
1832 /******************************************************************************
1834 ******************************************************************************/
1836 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1838 struct echoaudio *chip = dev_id;
1839 struct snd_pcm_substream *substream;
1842 spin_lock(&chip->lock);
1843 st = service_irq(chip);
1845 spin_unlock(&chip->lock);
1848 /* The hardware doesn't tell us which substream caused the irq,
1849 thus we have to check all running substreams. */
1850 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1851 substream = chip->substream[ss];
1852 if (substream && ((struct audiopipe *)substream->runtime->
1853 private_data)->state == PIPE_STATE_STARTED) {
1854 period = pcm_pointer(substream) /
1855 substream->runtime->period_size;
1856 if (period != chip->last_period[ss]) {
1857 chip->last_period[ss] = period;
1858 spin_unlock(&chip->lock);
1859 snd_pcm_period_elapsed(substream);
1860 spin_lock(&chip->lock);
1864 spin_unlock(&chip->lock);
1866 #ifdef ECHOCARD_HAS_MIDI
1867 if (st > 0 && chip->midi_in) {
1868 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1869 dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1878 /******************************************************************************
1879 Module construction / destruction
1880 ******************************************************************************/
1882 static int snd_echo_free(struct echoaudio *chip)
1884 dev_dbg(chip->card->dev, "Stop DSP...\n");
1885 if (chip->comm_page)
1886 rest_in_peace(chip);
1887 dev_dbg(chip->card->dev, "Stopped.\n");
1890 free_irq(chip->irq, chip);
1892 if (chip->comm_page)
1893 snd_dma_free_pages(&chip->commpage_dma_buf);
1895 if (chip->dsp_registers)
1896 iounmap(chip->dsp_registers);
1899 release_and_free_resource(chip->iores);
1901 dev_dbg(chip->card->dev, "MMIO freed.\n");
1903 pci_disable_device(chip->pci);
1905 /* release chip data */
1906 free_firmware_cache(chip);
1908 dev_dbg(chip->card->dev, "Chip freed.\n");
1914 static int snd_echo_dev_free(struct snd_device *device)
1916 struct echoaudio *chip = device->device_data;
1918 dev_dbg(chip->card->dev, "snd_echo_dev_free()...\n");
1919 return snd_echo_free(chip);
1924 /* <--snd_echo_probe() */
1925 static int snd_echo_create(struct snd_card *card,
1926 struct pci_dev *pci,
1927 struct echoaudio **rchip)
1929 struct echoaudio *chip;
1932 static struct snd_device_ops ops = {
1933 .dev_free = snd_echo_dev_free,
1938 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1940 if ((err = pci_enable_device(pci)) < 0)
1942 pci_set_master(pci);
1944 /* Allocate chip if needed */
1946 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1948 pci_disable_device(pci);
1951 dev_dbg(card->dev, "chip=%p\n", chip);
1952 spin_lock_init(&chip->lock);
1956 atomic_set(&chip->opencount, 0);
1957 mutex_init(&chip->mode_mutex);
1958 chip->can_set_rate = 1;
1960 /* If this was called from the resume function, chip is
1961 * already allocated and it contains current card settings.
1966 /* PCI resource allocation */
1967 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1968 sz = pci_resource_len(pci, 0);
1970 sz = PAGE_SIZE; /* We map only the required part */
1972 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1973 ECHOCARD_NAME)) == NULL) {
1974 dev_err(chip->card->dev, "cannot get memory region\n");
1975 snd_echo_free(chip);
1978 chip->dsp_registers = (volatile u32 __iomem *)
1979 ioremap_nocache(chip->dsp_registers_phys, sz);
1981 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1982 KBUILD_MODNAME, chip)) {
1983 dev_err(chip->card->dev, "cannot grab irq\n");
1984 snd_echo_free(chip);
1987 chip->irq = pci->irq;
1988 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1989 chip->pci, chip->irq, chip->pci->subsystem_device);
1991 /* Create the DSP comm page - this is the area of memory used for most
1992 of the communication with the DSP, which accesses it via bus mastering */
1993 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1994 sizeof(struct comm_page),
1995 &chip->commpage_dma_buf) < 0) {
1996 dev_err(chip->card->dev, "cannot allocate the comm page\n");
1997 snd_echo_free(chip);
2000 chip->comm_page_phys = chip->commpage_dma_buf.addr;
2001 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
2003 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2005 err = set_mixer_defaults(chip);
2007 dev_err(card->dev, "init_hw err=%d\n", err);
2008 snd_echo_free(chip);
2011 dev_dbg(card->dev, "Card init OK\n");
2013 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2014 snd_echo_free(chip);
2025 static int snd_echo_probe(struct pci_dev *pci,
2026 const struct pci_device_id *pci_id)
2029 struct snd_card *card;
2030 struct echoaudio *chip;
2034 if (dev >= SNDRV_CARDS)
2041 dev_dbg(&pci->dev, "Echoaudio driver starting...\n");
2043 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2048 chip = NULL; /* Tells snd_echo_create to allocate chip */
2049 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2050 snd_card_free(card);
2054 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2055 strcpy(card->shortname, chip->card_name);
2058 if (pci_id->device == 0x3410)
2061 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2062 card->shortname, pci_id->subdevice & 0x000f, dsp,
2063 chip->dsp_registers_phys, chip->irq);
2065 if ((err = snd_echo_new_pcm(chip)) < 0) {
2066 dev_err(chip->card->dev, "new pcm error %d\n", err);
2067 snd_card_free(card);
2071 #ifdef ECHOCARD_HAS_MIDI
2072 if (chip->has_midi) { /* Some Mia's do not have midi */
2073 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2074 dev_err(chip->card->dev, "new midi error %d\n", err);
2075 snd_card_free(card);
2081 #ifdef ECHOCARD_HAS_VMIXER
2082 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2083 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2085 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2086 err = snd_ctl_add(chip->card,
2087 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2091 #else /* ECHOCARD_HAS_VMIXER */
2092 err = snd_ctl_add(chip->card,
2093 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2096 #endif /* ECHOCARD_HAS_VMIXER */
2098 #ifdef ECHOCARD_HAS_INPUT_GAIN
2099 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2103 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2104 if (!chip->hasnt_input_nominal_level)
2105 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2109 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2110 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2114 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2117 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2120 #ifdef ECHOCARD_HAS_MONITOR
2121 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2122 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2126 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2127 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2131 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2134 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2135 /* Creates a list of available digital modes */
2136 chip->num_digital_modes = 0;
2137 for (i = 0; i < 6; i++)
2138 if (chip->digital_modes & (1 << i))
2139 chip->digital_mode_list[chip->num_digital_modes++] = i;
2141 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2143 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2145 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2146 /* Creates a list of available clock sources */
2147 chip->num_clock_sources = 0;
2148 for (i = 0; i < 10; i++)
2149 if (chip->input_clock_types & (1 << i))
2150 chip->clock_source_list[chip->num_clock_sources++] = i;
2152 if (chip->num_clock_sources > 1) {
2153 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2154 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2157 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2159 #ifdef ECHOCARD_HAS_DIGITAL_IO
2160 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2164 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2165 if (chip->has_phantom_power)
2166 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2170 err = snd_card_register(card);
2173 dev_info(card->dev, "Card registered: %s\n", card->longname);
2175 pci_set_drvdata(pci, chip);
2180 dev_err(card->dev, "new control error %d\n", err);
2181 snd_card_free(card);
2187 #if defined(CONFIG_PM_SLEEP)
2189 static int snd_echo_suspend(struct device *dev)
2191 struct pci_dev *pci = to_pci_dev(dev);
2192 struct echoaudio *chip = dev_get_drvdata(dev);
2194 dev_dbg(dev, "suspend start\n");
2195 snd_pcm_suspend_all(chip->analog_pcm);
2196 snd_pcm_suspend_all(chip->digital_pcm);
2198 #ifdef ECHOCARD_HAS_MIDI
2199 /* This call can sleep */
2201 snd_echo_midi_output_trigger(chip->midi_out, 0);
2203 spin_lock_irq(&chip->lock);
2204 if (wait_handshake(chip)) {
2205 spin_unlock_irq(&chip->lock);
2208 clear_handshake(chip);
2209 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2210 spin_unlock_irq(&chip->lock);
2213 spin_unlock_irq(&chip->lock);
2215 chip->dsp_code = NULL;
2216 free_irq(chip->irq, chip);
2218 pci_save_state(pci);
2219 pci_disable_device(pci);
2221 dev_dbg(dev, "suspend done\n");
2227 static int snd_echo_resume(struct device *dev)
2229 struct pci_dev *pci = to_pci_dev(dev);
2230 struct echoaudio *chip = dev_get_drvdata(dev);
2231 struct comm_page *commpage, *commpage_bak;
2232 u32 pipe_alloc_mask;
2235 dev_dbg(dev, "resume start\n");
2236 pci_restore_state(pci);
2237 commpage_bak = kmalloc(sizeof(struct echoaudio), GFP_KERNEL);
2238 if (commpage_bak == NULL)
2240 commpage = chip->comm_page;
2241 memcpy(commpage_bak, commpage, sizeof(struct comm_page));
2243 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2245 kfree(commpage_bak);
2246 dev_err(dev, "resume init_hw err=%d\n", err);
2247 snd_echo_free(chip);
2250 dev_dbg(dev, "resume init OK\n");
2252 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2253 * restore_dsp_settings() fails.
2255 pipe_alloc_mask = chip->pipe_alloc_mask;
2256 chip->pipe_alloc_mask = 0;
2257 err = restore_dsp_rettings(chip);
2258 chip->pipe_alloc_mask = pipe_alloc_mask;
2260 kfree(commpage_bak);
2263 dev_dbg(dev, "resume restore OK\n");
2265 memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2266 sizeof(commpage->audio_format));
2267 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2268 sizeof(commpage->sglist_addr));
2269 memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2270 sizeof(commpage->midi_output));
2271 kfree(commpage_bak);
2273 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2274 KBUILD_MODNAME, chip)) {
2275 dev_err(chip->card->dev, "cannot grab irq\n");
2276 snd_echo_free(chip);
2279 chip->irq = pci->irq;
2280 dev_dbg(dev, "resume irq=%d\n", chip->irq);
2282 #ifdef ECHOCARD_HAS_MIDI
2283 if (chip->midi_input_enabled)
2284 enable_midi_input(chip, TRUE);
2286 snd_echo_midi_output_trigger(chip->midi_out, 1);
2289 dev_dbg(dev, "resume done\n");
2293 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2294 #define SND_ECHO_PM_OPS &snd_echo_pm
2296 #define SND_ECHO_PM_OPS NULL
2297 #endif /* CONFIG_PM_SLEEP */
2300 static void snd_echo_remove(struct pci_dev *pci)
2302 struct echoaudio *chip;
2304 chip = pci_get_drvdata(pci);
2306 snd_card_free(chip->card);
2311 /******************************************************************************
2312 Everything starts and ends here
2313 ******************************************************************************/
2315 /* pci_driver definition */
2316 static struct pci_driver echo_driver = {
2317 .name = KBUILD_MODNAME,
2318 .id_table = snd_echo_ids,
2319 .probe = snd_echo_probe,
2320 .remove = snd_echo_remove,
2322 .pm = SND_ECHO_PM_OPS,
2326 module_pci_driver(echo_driver);