pch_phub: fix error return code in pch_phub_probe()
[linux-2.6/btrfs-unstable.git] / sound / pci / echoaudio / echoaudio.c
blob760cbff532105f3bf56485082a43f0e6177fbad9
1 /*
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)
46 int err;
47 char name[30];
49 #ifdef CONFIG_PM_SLEEP
50 if (chip->fw_cache[fw_index]) {
51 DE_ACT(("firmware requested: %s is cached\n", card_fw[fw_index].data));
52 *fw_entry = chip->fw_cache[fw_index];
53 return 0;
55 #endif
57 DE_ACT(("firmware requested: %s\n", card_fw[fw_index].data));
58 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
59 err = request_firmware(fw_entry, name, pci_device(chip));
60 if (err < 0)
61 snd_printk(KERN_ERR "get_firmware(): Firmware not available (%d)\n", err);
62 #ifdef CONFIG_PM_SLEEP
63 else
64 chip->fw_cache[fw_index] = *fw_entry;
65 #endif
66 return err;
71 static void free_firmware(const struct firmware *fw_entry)
73 #ifdef CONFIG_PM_SLEEP
74 DE_ACT(("firmware not released (kept in cache)\n"));
75 #else
76 release_firmware(fw_entry);
77 DE_ACT(("firmware released\n"));
78 #endif
83 static void free_firmware_cache(struct echoaudio *chip)
85 #ifdef CONFIG_PM_SLEEP
86 int i;
88 for (i = 0; i < 8 ; i++)
89 if (chip->fw_cache[i]) {
90 release_firmware(chip->fw_cache[i]);
91 DE_ACT(("release_firmware(%d)\n", i));
94 DE_ACT(("firmware_cache released\n"));
95 #endif
100 /******************************************************************************
101 PCM interface
102 ******************************************************************************/
104 static void audiopipe_free(struct snd_pcm_runtime *runtime)
106 struct audiopipe *pipe = runtime->private_data;
108 if (pipe->sgpage.area)
109 snd_dma_free_pages(&pipe->sgpage);
110 kfree(pipe);
115 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
116 struct snd_pcm_hw_rule *rule)
118 struct snd_interval *c = hw_param_interval(params,
119 SNDRV_PCM_HW_PARAM_CHANNELS);
120 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
121 struct snd_mask fmt;
123 snd_mask_any(&fmt);
125 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
126 /* >=2 channels cannot be S32_BE */
127 if (c->min == 2) {
128 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
129 return snd_mask_refine(f, &fmt);
131 #endif
132 /* > 2 channels cannot be U8 and S32_BE */
133 if (c->min > 2) {
134 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
135 return snd_mask_refine(f, &fmt);
137 /* Mono is ok with any format */
138 return 0;
143 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
144 struct snd_pcm_hw_rule *rule)
146 struct snd_interval *c = hw_param_interval(params,
147 SNDRV_PCM_HW_PARAM_CHANNELS);
148 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
149 struct snd_interval ch;
151 snd_interval_any(&ch);
153 /* S32_BE is mono (and stereo) only */
154 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
155 ch.min = 1;
156 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
157 ch.max = 2;
158 #else
159 ch.max = 1;
160 #endif
161 ch.integer = 1;
162 return snd_interval_refine(c, &ch);
164 /* U8 can be only mono or stereo */
165 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
166 ch.min = 1;
167 ch.max = 2;
168 ch.integer = 1;
169 return snd_interval_refine(c, &ch);
171 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
172 return 0;
177 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
178 struct snd_pcm_hw_rule *rule)
180 struct snd_interval *c = hw_param_interval(params,
181 SNDRV_PCM_HW_PARAM_CHANNELS);
182 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
183 struct snd_mask fmt;
184 u64 fmask;
185 snd_mask_any(&fmt);
187 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
189 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
190 if (c->min > 2) {
191 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
192 SNDRV_PCM_FMTBIT_S24_3LE |
193 SNDRV_PCM_FMTBIT_S32_LE;
194 /* 1 channel must be S32_BE or S32_LE */
195 } else if (c->max == 1)
196 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
197 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
198 /* 2 channels cannot be S32_BE */
199 else if (c->min == 2 && c->max == 2)
200 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
201 #endif
202 else
203 return 0;
205 fmt.bits[0] &= (u32)fmask;
206 fmt.bits[1] &= (u32)(fmask >> 32);
207 return snd_mask_refine(f, &fmt);
212 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
213 struct snd_pcm_hw_rule *rule)
215 struct snd_interval *c = hw_param_interval(params,
216 SNDRV_PCM_HW_PARAM_CHANNELS);
217 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
218 struct snd_interval ch;
219 u64 fmask;
221 snd_interval_any(&ch);
222 ch.integer = 1;
223 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
225 /* S32_BE is mono (and stereo) only */
226 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
227 ch.min = 1;
228 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
229 ch.max = 2;
230 #else
231 ch.max = 1;
232 #endif
233 /* U8 is stereo only */
234 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
235 ch.min = ch.max = 2;
236 /* S16_LE and S24_3LE must be at least stereo */
237 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
238 SNDRV_PCM_FMTBIT_S24_3LE)))
239 ch.min = 2;
240 else
241 return 0;
243 return snd_interval_refine(c, &ch);
248 /* Since the sample rate is a global setting, do allow the user to change the
249 sample rate only if there is only one pcm device open. */
250 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
251 struct snd_pcm_hw_rule *rule)
253 struct snd_interval *rate = hw_param_interval(params,
254 SNDRV_PCM_HW_PARAM_RATE);
255 struct echoaudio *chip = rule->private;
256 struct snd_interval fixed;
258 if (!chip->can_set_rate) {
259 snd_interval_any(&fixed);
260 fixed.min = fixed.max = chip->sample_rate;
261 return snd_interval_refine(rate, &fixed);
263 return 0;
267 static int pcm_open(struct snd_pcm_substream *substream,
268 signed char max_channels)
270 struct echoaudio *chip;
271 struct snd_pcm_runtime *runtime;
272 struct audiopipe *pipe;
273 int err, i;
275 if (max_channels <= 0)
276 return -EAGAIN;
278 chip = snd_pcm_substream_chip(substream);
279 runtime = substream->runtime;
281 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
282 if (!pipe)
283 return -ENOMEM;
284 pipe->index = -1; /* Not configured yet */
286 /* Set up hw capabilities and contraints */
287 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
288 DE_HWP(("max_channels=%d\n", max_channels));
289 pipe->constr.list = channels_list;
290 pipe->constr.mask = 0;
291 for (i = 0; channels_list[i] <= max_channels; i++);
292 pipe->constr.count = i;
293 if (pipe->hw.channels_max > max_channels)
294 pipe->hw.channels_max = max_channels;
295 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
296 pipe->hw.rate_max = 48000;
297 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
300 runtime->hw = pipe->hw;
301 runtime->private_data = pipe;
302 runtime->private_free = audiopipe_free;
303 snd_pcm_set_sync(substream);
305 /* Only mono and any even number of channels are allowed */
306 if ((err = snd_pcm_hw_constraint_list(runtime, 0,
307 SNDRV_PCM_HW_PARAM_CHANNELS,
308 &pipe->constr)) < 0)
309 return err;
311 /* All periods should have the same size */
312 if ((err = snd_pcm_hw_constraint_integer(runtime,
313 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
314 return err;
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 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
320 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
321 32)) < 0)
322 return err;
323 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
324 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
325 32)) < 0)
326 return err;
328 if ((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)) < 0)
332 return err;
334 /* Finally allocate a page for the scatter-gather list */
335 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
336 snd_dma_pci_data(chip->pci),
337 PAGE_SIZE, &pipe->sgpage)) < 0) {
338 DE_HWP(("s-g list allocation failed\n"));
339 return err;
342 return 0;
347 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
349 struct echoaudio *chip = snd_pcm_substream_chip(substream);
350 int err;
352 DE_ACT(("pcm_analog_in_open\n"));
353 if ((err = pcm_open(substream, num_analog_busses_in(chip) -
354 substream->number)) < 0)
355 return err;
356 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
357 SNDRV_PCM_HW_PARAM_CHANNELS,
358 hw_rule_capture_channels_by_format, NULL,
359 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
360 return err;
361 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
362 SNDRV_PCM_HW_PARAM_FORMAT,
363 hw_rule_capture_format_by_channels, NULL,
364 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
365 return err;
366 atomic_inc(&chip->opencount);
367 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
368 chip->can_set_rate=0;
369 DE_HWP(("pcm_analog_in_open cs=%d oc=%d r=%d\n",
370 chip->can_set_rate, atomic_read(&chip->opencount),
371 chip->sample_rate));
372 return 0;
377 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
379 struct echoaudio *chip = snd_pcm_substream_chip(substream);
380 int max_channels, err;
382 #ifdef ECHOCARD_HAS_VMIXER
383 max_channels = num_pipes_out(chip);
384 #else
385 max_channels = num_analog_busses_out(chip);
386 #endif
387 DE_ACT(("pcm_analog_out_open\n"));
388 if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
389 return err;
390 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
391 SNDRV_PCM_HW_PARAM_CHANNELS,
392 hw_rule_playback_channels_by_format,
393 NULL,
394 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
395 return err;
396 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
397 SNDRV_PCM_HW_PARAM_FORMAT,
398 hw_rule_playback_format_by_channels,
399 NULL,
400 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
401 return err;
402 atomic_inc(&chip->opencount);
403 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
404 chip->can_set_rate=0;
405 DE_HWP(("pcm_analog_out_open cs=%d oc=%d r=%d\n",
406 chip->can_set_rate, atomic_read(&chip->opencount),
407 chip->sample_rate));
408 return 0;
413 #ifdef ECHOCARD_HAS_DIGITAL_IO
415 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
417 struct echoaudio *chip = snd_pcm_substream_chip(substream);
418 int err, max_channels;
420 DE_ACT(("pcm_digital_in_open\n"));
421 max_channels = num_digital_busses_in(chip) - substream->number;
422 mutex_lock(&chip->mode_mutex);
423 if (chip->digital_mode == DIGITAL_MODE_ADAT)
424 err = pcm_open(substream, max_channels);
425 else /* If the card has ADAT, subtract the 6 channels
426 * that S/PDIF doesn't have
428 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
430 if (err < 0)
431 goto din_exit;
433 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
434 SNDRV_PCM_HW_PARAM_CHANNELS,
435 hw_rule_capture_channels_by_format, NULL,
436 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
437 goto din_exit;
438 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
439 SNDRV_PCM_HW_PARAM_FORMAT,
440 hw_rule_capture_format_by_channels, NULL,
441 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
442 goto din_exit;
444 atomic_inc(&chip->opencount);
445 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
446 chip->can_set_rate=0;
448 din_exit:
449 mutex_unlock(&chip->mode_mutex);
450 return err;
455 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
457 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
459 struct echoaudio *chip = snd_pcm_substream_chip(substream);
460 int err, max_channels;
462 DE_ACT(("pcm_digital_out_open\n"));
463 max_channels = num_digital_busses_out(chip) - substream->number;
464 mutex_lock(&chip->mode_mutex);
465 if (chip->digital_mode == DIGITAL_MODE_ADAT)
466 err = pcm_open(substream, max_channels);
467 else /* If the card has ADAT, subtract the 6 channels
468 * that S/PDIF doesn't have
470 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
472 if (err < 0)
473 goto dout_exit;
475 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
476 SNDRV_PCM_HW_PARAM_CHANNELS,
477 hw_rule_playback_channels_by_format,
478 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
479 -1)) < 0)
480 goto dout_exit;
481 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
482 SNDRV_PCM_HW_PARAM_FORMAT,
483 hw_rule_playback_format_by_channels,
484 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
485 -1)) < 0)
486 goto dout_exit;
487 atomic_inc(&chip->opencount);
488 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
489 chip->can_set_rate=0;
490 dout_exit:
491 mutex_unlock(&chip->mode_mutex);
492 return err;
495 #endif /* !ECHOCARD_HAS_VMIXER */
497 #endif /* ECHOCARD_HAS_DIGITAL_IO */
501 static int pcm_close(struct snd_pcm_substream *substream)
503 struct echoaudio *chip = snd_pcm_substream_chip(substream);
504 int oc;
506 /* Nothing to do here. Audio is already off and pipe will be
507 * freed by its callback
509 DE_ACT(("pcm_close\n"));
511 atomic_dec(&chip->opencount);
512 oc = atomic_read(&chip->opencount);
513 DE_ACT(("pcm_close oc=%d cs=%d rs=%d\n", oc,
514 chip->can_set_rate, chip->rate_set));
515 if (oc < 2)
516 chip->can_set_rate = 1;
517 if (oc == 0)
518 chip->rate_set = 0;
519 DE_ACT(("pcm_close2 oc=%d cs=%d rs=%d\n", oc,
520 chip->can_set_rate,chip->rate_set));
522 return 0;
527 /* Channel allocation and scatter-gather list setup */
528 static int init_engine(struct snd_pcm_substream *substream,
529 struct snd_pcm_hw_params *hw_params,
530 int pipe_index, int interleave)
532 struct echoaudio *chip;
533 int err, per, rest, page, edge, offs;
534 struct audiopipe *pipe;
536 chip = snd_pcm_substream_chip(substream);
537 pipe = (struct audiopipe *) substream->runtime->private_data;
539 /* Sets up che hardware. If it's already initialized, reset and
540 * redo with the new parameters
542 spin_lock_irq(&chip->lock);
543 if (pipe->index >= 0) {
544 DE_HWP(("hwp_ie free(%d)\n", pipe->index));
545 err = free_pipes(chip, pipe);
546 snd_BUG_ON(err);
547 chip->substream[pipe->index] = NULL;
550 err = allocate_pipes(chip, pipe, pipe_index, interleave);
551 if (err < 0) {
552 spin_unlock_irq(&chip->lock);
553 DE_ACT((KERN_NOTICE "allocate_pipes(%d) err=%d\n",
554 pipe_index, err));
555 return err;
557 spin_unlock_irq(&chip->lock);
558 DE_ACT((KERN_NOTICE "allocate_pipes()=%d\n", pipe_index));
560 DE_HWP(("pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
561 params_buffer_bytes(hw_params), params_periods(hw_params),
562 params_period_bytes(hw_params)));
563 err = snd_pcm_lib_malloc_pages(substream,
564 params_buffer_bytes(hw_params));
565 if (err < 0) {
566 snd_printk(KERN_ERR "malloc_pages err=%d\n", err);
567 spin_lock_irq(&chip->lock);
568 free_pipes(chip, pipe);
569 spin_unlock_irq(&chip->lock);
570 pipe->index = -1;
571 return err;
574 sglist_init(chip, pipe);
575 edge = PAGE_SIZE;
576 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
577 per++) {
578 rest = params_period_bytes(hw_params);
579 if (offs + rest > params_buffer_bytes(hw_params))
580 rest = params_buffer_bytes(hw_params) - offs;
581 while (rest) {
582 dma_addr_t addr;
583 addr = snd_pcm_sgbuf_get_addr(substream, offs);
584 if (rest <= edge - offs) {
585 sglist_add_mapping(chip, pipe, addr, rest);
586 sglist_add_irq(chip, pipe);
587 offs += rest;
588 rest = 0;
589 } else {
590 sglist_add_mapping(chip, pipe, addr,
591 edge - offs);
592 rest -= edge - offs;
593 offs = edge;
595 if (offs == edge) {
596 edge += PAGE_SIZE;
597 page++;
602 /* Close the ring buffer */
603 sglist_wrap(chip, pipe);
605 /* This stuff is used by the irq handler, so it must be
606 * initialized before chip->substream
608 chip->last_period[pipe_index] = 0;
609 pipe->last_counter = 0;
610 pipe->position = 0;
611 smp_wmb();
612 chip->substream[pipe_index] = substream;
613 chip->rate_set = 1;
614 spin_lock_irq(&chip->lock);
615 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
616 spin_unlock_irq(&chip->lock);
617 DE_HWP(("pcm_hw_params ok\n"));
618 return 0;
623 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
624 struct snd_pcm_hw_params *hw_params)
626 struct echoaudio *chip = snd_pcm_substream_chip(substream);
628 return init_engine(substream, hw_params, px_analog_in(chip) +
629 substream->number, params_channels(hw_params));
634 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
635 struct snd_pcm_hw_params *hw_params)
637 return init_engine(substream, hw_params, substream->number,
638 params_channels(hw_params));
643 #ifdef ECHOCARD_HAS_DIGITAL_IO
645 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
646 struct snd_pcm_hw_params *hw_params)
648 struct echoaudio *chip = snd_pcm_substream_chip(substream);
650 return init_engine(substream, hw_params, px_digital_in(chip) +
651 substream->number, params_channels(hw_params));
656 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
657 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
658 struct snd_pcm_hw_params *hw_params)
660 struct echoaudio *chip = snd_pcm_substream_chip(substream);
662 return init_engine(substream, hw_params, px_digital_out(chip) +
663 substream->number, params_channels(hw_params));
665 #endif /* !ECHOCARD_HAS_VMIXER */
667 #endif /* ECHOCARD_HAS_DIGITAL_IO */
671 static int pcm_hw_free(struct snd_pcm_substream *substream)
673 struct echoaudio *chip;
674 struct audiopipe *pipe;
676 chip = snd_pcm_substream_chip(substream);
677 pipe = (struct audiopipe *) substream->runtime->private_data;
679 spin_lock_irq(&chip->lock);
680 if (pipe->index >= 0) {
681 DE_HWP(("pcm_hw_free(%d)\n", pipe->index));
682 free_pipes(chip, pipe);
683 chip->substream[pipe->index] = NULL;
684 pipe->index = -1;
686 spin_unlock_irq(&chip->lock);
688 DE_HWP(("pcm_hw_freed\n"));
689 snd_pcm_lib_free_pages(substream);
690 return 0;
695 static int pcm_prepare(struct snd_pcm_substream *substream)
697 struct echoaudio *chip = snd_pcm_substream_chip(substream);
698 struct snd_pcm_runtime *runtime = substream->runtime;
699 struct audioformat format;
700 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
702 DE_HWP(("Prepare rate=%d format=%d channels=%d\n",
703 runtime->rate, runtime->format, runtime->channels));
704 format.interleave = runtime->channels;
705 format.data_are_bigendian = 0;
706 format.mono_to_stereo = 0;
707 switch (runtime->format) {
708 case SNDRV_PCM_FORMAT_U8:
709 format.bits_per_sample = 8;
710 break;
711 case SNDRV_PCM_FORMAT_S16_LE:
712 format.bits_per_sample = 16;
713 break;
714 case SNDRV_PCM_FORMAT_S24_3LE:
715 format.bits_per_sample = 24;
716 break;
717 case SNDRV_PCM_FORMAT_S32_BE:
718 format.data_are_bigendian = 1;
719 case SNDRV_PCM_FORMAT_S32_LE:
720 format.bits_per_sample = 32;
721 break;
722 default:
723 DE_HWP(("Prepare error: unsupported format %d\n",
724 runtime->format));
725 return -EINVAL;
728 if (snd_BUG_ON(pipe_index >= px_num(chip)))
729 return -EINVAL;
730 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
731 return -EINVAL;
732 set_audio_format(chip, pipe_index, &format);
733 return 0;
738 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
740 struct echoaudio *chip = snd_pcm_substream_chip(substream);
741 struct snd_pcm_runtime *runtime = substream->runtime;
742 struct audiopipe *pipe = runtime->private_data;
743 int i, err;
744 u32 channelmask = 0;
745 struct snd_pcm_substream *s;
747 snd_pcm_group_for_each_entry(s, substream) {
748 for (i = 0; i < DSP_MAXPIPES; i++) {
749 if (s == chip->substream[i]) {
750 channelmask |= 1 << i;
751 snd_pcm_trigger_done(s, substream);
756 spin_lock(&chip->lock);
757 switch (cmd) {
758 case SNDRV_PCM_TRIGGER_RESUME:
759 DE_ACT(("pcm_trigger resume\n"));
760 case SNDRV_PCM_TRIGGER_START:
761 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
762 DE_ACT(("pcm_trigger start\n"));
763 for (i = 0; i < DSP_MAXPIPES; i++) {
764 if (channelmask & (1 << i)) {
765 pipe = chip->substream[i]->runtime->private_data;
766 switch (pipe->state) {
767 case PIPE_STATE_STOPPED:
768 chip->last_period[i] = 0;
769 pipe->last_counter = 0;
770 pipe->position = 0;
771 *pipe->dma_counter = 0;
772 case PIPE_STATE_PAUSED:
773 pipe->state = PIPE_STATE_STARTED;
774 break;
775 case PIPE_STATE_STARTED:
776 break;
780 err = start_transport(chip, channelmask,
781 chip->pipe_cyclic_mask);
782 break;
783 case SNDRV_PCM_TRIGGER_SUSPEND:
784 DE_ACT(("pcm_trigger suspend\n"));
785 case SNDRV_PCM_TRIGGER_STOP:
786 DE_ACT(("pcm_trigger stop\n"));
787 for (i = 0; i < DSP_MAXPIPES; i++) {
788 if (channelmask & (1 << i)) {
789 pipe = chip->substream[i]->runtime->private_data;
790 pipe->state = PIPE_STATE_STOPPED;
793 err = stop_transport(chip, channelmask);
794 break;
795 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
796 DE_ACT(("pcm_trigger pause\n"));
797 for (i = 0; i < DSP_MAXPIPES; i++) {
798 if (channelmask & (1 << i)) {
799 pipe = chip->substream[i]->runtime->private_data;
800 pipe->state = PIPE_STATE_PAUSED;
803 err = pause_transport(chip, channelmask);
804 break;
805 default:
806 err = -EINVAL;
808 spin_unlock(&chip->lock);
809 return err;
814 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
816 struct snd_pcm_runtime *runtime = substream->runtime;
817 struct audiopipe *pipe = runtime->private_data;
818 size_t cnt, bufsize, pos;
820 cnt = le32_to_cpu(*pipe->dma_counter);
821 pipe->position += cnt - pipe->last_counter;
822 pipe->last_counter = cnt;
823 bufsize = substream->runtime->buffer_size;
824 pos = bytes_to_frames(substream->runtime, pipe->position);
826 while (pos >= bufsize) {
827 pipe->position -= frames_to_bytes(substream->runtime, bufsize);
828 pos -= bufsize;
830 return pos;
835 /* pcm *_ops structures */
836 static struct snd_pcm_ops analog_playback_ops = {
837 .open = pcm_analog_out_open,
838 .close = pcm_close,
839 .ioctl = snd_pcm_lib_ioctl,
840 .hw_params = pcm_analog_out_hw_params,
841 .hw_free = pcm_hw_free,
842 .prepare = pcm_prepare,
843 .trigger = pcm_trigger,
844 .pointer = pcm_pointer,
845 .page = snd_pcm_sgbuf_ops_page,
847 static struct snd_pcm_ops analog_capture_ops = {
848 .open = pcm_analog_in_open,
849 .close = pcm_close,
850 .ioctl = snd_pcm_lib_ioctl,
851 .hw_params = pcm_analog_in_hw_params,
852 .hw_free = pcm_hw_free,
853 .prepare = pcm_prepare,
854 .trigger = pcm_trigger,
855 .pointer = pcm_pointer,
856 .page = snd_pcm_sgbuf_ops_page,
858 #ifdef ECHOCARD_HAS_DIGITAL_IO
859 #ifndef ECHOCARD_HAS_VMIXER
860 static struct snd_pcm_ops digital_playback_ops = {
861 .open = pcm_digital_out_open,
862 .close = pcm_close,
863 .ioctl = snd_pcm_lib_ioctl,
864 .hw_params = pcm_digital_out_hw_params,
865 .hw_free = pcm_hw_free,
866 .prepare = pcm_prepare,
867 .trigger = pcm_trigger,
868 .pointer = pcm_pointer,
869 .page = snd_pcm_sgbuf_ops_page,
871 #endif /* !ECHOCARD_HAS_VMIXER */
872 static struct snd_pcm_ops digital_capture_ops = {
873 .open = pcm_digital_in_open,
874 .close = pcm_close,
875 .ioctl = snd_pcm_lib_ioctl,
876 .hw_params = pcm_digital_in_hw_params,
877 .hw_free = pcm_hw_free,
878 .prepare = pcm_prepare,
879 .trigger = pcm_trigger,
880 .pointer = pcm_pointer,
881 .page = snd_pcm_sgbuf_ops_page,
883 #endif /* ECHOCARD_HAS_DIGITAL_IO */
887 /* Preallocate memory only for the first substream because it's the most
888 * used one
890 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
892 struct snd_pcm_substream *ss;
893 int stream, err;
895 for (stream = 0; stream < 2; stream++)
896 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) {
897 err = snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
898 dev,
899 ss->number ? 0 : 128<<10,
900 256<<10);
901 if (err < 0)
902 return err;
904 return 0;
909 /*<--snd_echo_probe() */
910 static int snd_echo_new_pcm(struct echoaudio *chip)
912 struct snd_pcm *pcm;
913 int err;
915 #ifdef ECHOCARD_HAS_VMIXER
916 /* This card has a Vmixer, that is there is no direct mapping from PCM
917 streams to physical outputs. The user can mix the streams as he wishes
918 via control interface and it's possible to send any stream to any
919 output, thus it makes no sense to keep analog and digital outputs
920 separated */
922 /* PCM#0 Virtual outputs and analog inputs */
923 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
924 num_analog_busses_in(chip), &pcm)) < 0)
925 return err;
926 pcm->private_data = chip;
927 chip->analog_pcm = pcm;
928 strcpy(pcm->name, chip->card->shortname);
929 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
930 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
931 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
932 return err;
933 DE_INIT(("Analog PCM ok\n"));
935 #ifdef ECHOCARD_HAS_DIGITAL_IO
936 /* PCM#1 Digital inputs, no outputs */
937 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
938 num_digital_busses_in(chip), &pcm)) < 0)
939 return err;
940 pcm->private_data = chip;
941 chip->digital_pcm = pcm;
942 strcpy(pcm->name, chip->card->shortname);
943 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
944 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
945 return err;
946 DE_INIT(("Digital PCM ok\n"));
947 #endif /* ECHOCARD_HAS_DIGITAL_IO */
949 #else /* ECHOCARD_HAS_VMIXER */
951 /* The card can manage substreams formed by analog and digital channels
952 at the same time, but I prefer to keep analog and digital channels
953 separated, because that mixed thing is confusing and useless. So we
954 register two PCM devices: */
956 /* PCM#0 Analog i/o */
957 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
958 num_analog_busses_out(chip),
959 num_analog_busses_in(chip), &pcm)) < 0)
960 return err;
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 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
967 return err;
968 DE_INIT(("Analog PCM ok\n"));
970 #ifdef ECHOCARD_HAS_DIGITAL_IO
971 /* PCM#1 Digital i/o */
972 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
973 num_digital_busses_out(chip),
974 num_digital_busses_in(chip), &pcm)) < 0)
975 return err;
976 pcm->private_data = chip;
977 chip->digital_pcm = pcm;
978 strcpy(pcm->name, chip->card->shortname);
979 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
980 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
981 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
982 return err;
983 DE_INIT(("Digital PCM ok\n"));
984 #endif /* ECHOCARD_HAS_DIGITAL_IO */
986 #endif /* ECHOCARD_HAS_VMIXER */
988 return 0;
994 /******************************************************************************
995 Control interface
996 ******************************************************************************/
998 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
1000 /******************* PCM output volume *******************/
1001 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
1002 struct snd_ctl_elem_info *uinfo)
1004 struct echoaudio *chip;
1006 chip = snd_kcontrol_chip(kcontrol);
1007 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1008 uinfo->count = num_busses_out(chip);
1009 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1010 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1011 return 0;
1014 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1015 struct snd_ctl_elem_value *ucontrol)
1017 struct echoaudio *chip;
1018 int c;
1020 chip = snd_kcontrol_chip(kcontrol);
1021 for (c = 0; c < num_busses_out(chip); c++)
1022 ucontrol->value.integer.value[c] = chip->output_gain[c];
1023 return 0;
1026 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1027 struct snd_ctl_elem_value *ucontrol)
1029 struct echoaudio *chip;
1030 int c, changed, gain;
1032 changed = 0;
1033 chip = snd_kcontrol_chip(kcontrol);
1034 spin_lock_irq(&chip->lock);
1035 for (c = 0; c < num_busses_out(chip); c++) {
1036 gain = ucontrol->value.integer.value[c];
1037 /* Ignore out of range values */
1038 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1039 continue;
1040 if (chip->output_gain[c] != gain) {
1041 set_output_gain(chip, c, gain);
1042 changed = 1;
1045 if (changed)
1046 update_output_line_level(chip);
1047 spin_unlock_irq(&chip->lock);
1048 return changed;
1051 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1052 /* On the Mia this one controls the line-out volume */
1053 static struct snd_kcontrol_new snd_echo_line_output_gain = {
1054 .name = "Line Playback Volume",
1055 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1056 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1057 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1058 .info = snd_echo_output_gain_info,
1059 .get = snd_echo_output_gain_get,
1060 .put = snd_echo_output_gain_put,
1061 .tlv = {.p = db_scale_output_gain},
1063 #else
1064 static struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1065 .name = "PCM Playback Volume",
1066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1067 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1068 .info = snd_echo_output_gain_info,
1069 .get = snd_echo_output_gain_get,
1070 .put = snd_echo_output_gain_put,
1071 .tlv = {.p = db_scale_output_gain},
1073 #endif
1075 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1079 #ifdef ECHOCARD_HAS_INPUT_GAIN
1081 /******************* Analog input volume *******************/
1082 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1083 struct snd_ctl_elem_info *uinfo)
1085 struct echoaudio *chip;
1087 chip = snd_kcontrol_chip(kcontrol);
1088 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1089 uinfo->count = num_analog_busses_in(chip);
1090 uinfo->value.integer.min = ECHOGAIN_MININP;
1091 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1092 return 0;
1095 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1098 struct echoaudio *chip;
1099 int c;
1101 chip = snd_kcontrol_chip(kcontrol);
1102 for (c = 0; c < num_analog_busses_in(chip); c++)
1103 ucontrol->value.integer.value[c] = chip->input_gain[c];
1104 return 0;
1107 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1108 struct snd_ctl_elem_value *ucontrol)
1110 struct echoaudio *chip;
1111 int c, gain, changed;
1113 changed = 0;
1114 chip = snd_kcontrol_chip(kcontrol);
1115 spin_lock_irq(&chip->lock);
1116 for (c = 0; c < num_analog_busses_in(chip); c++) {
1117 gain = ucontrol->value.integer.value[c];
1118 /* Ignore out of range values */
1119 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1120 continue;
1121 if (chip->input_gain[c] != gain) {
1122 set_input_gain(chip, c, gain);
1123 changed = 1;
1126 if (changed)
1127 update_input_line_level(chip);
1128 spin_unlock_irq(&chip->lock);
1129 return changed;
1132 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1134 static struct snd_kcontrol_new snd_echo_line_input_gain = {
1135 .name = "Line Capture Volume",
1136 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1137 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1138 .info = snd_echo_input_gain_info,
1139 .get = snd_echo_input_gain_get,
1140 .put = snd_echo_input_gain_put,
1141 .tlv = {.p = db_scale_input_gain},
1144 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1148 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1150 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1151 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1152 struct snd_ctl_elem_info *uinfo)
1154 struct echoaudio *chip;
1156 chip = snd_kcontrol_chip(kcontrol);
1157 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1158 uinfo->count = num_analog_busses_out(chip);
1159 uinfo->value.integer.min = 0;
1160 uinfo->value.integer.max = 1;
1161 return 0;
1164 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1165 struct snd_ctl_elem_value *ucontrol)
1167 struct echoaudio *chip;
1168 int c;
1170 chip = snd_kcontrol_chip(kcontrol);
1171 for (c = 0; c < num_analog_busses_out(chip); c++)
1172 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1173 return 0;
1176 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1177 struct snd_ctl_elem_value *ucontrol)
1179 struct echoaudio *chip;
1180 int c, changed;
1182 changed = 0;
1183 chip = snd_kcontrol_chip(kcontrol);
1184 spin_lock_irq(&chip->lock);
1185 for (c = 0; c < num_analog_busses_out(chip); c++) {
1186 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1187 set_nominal_level(chip, c,
1188 ucontrol->value.integer.value[c]);
1189 changed = 1;
1192 if (changed)
1193 update_output_line_level(chip);
1194 spin_unlock_irq(&chip->lock);
1195 return changed;
1198 static struct snd_kcontrol_new snd_echo_output_nominal_level = {
1199 .name = "Line Playback Switch (-10dBV)",
1200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1201 .info = snd_echo_output_nominal_info,
1202 .get = snd_echo_output_nominal_get,
1203 .put = snd_echo_output_nominal_put,
1206 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1210 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1212 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1213 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1214 struct snd_ctl_elem_info *uinfo)
1216 struct echoaudio *chip;
1218 chip = snd_kcontrol_chip(kcontrol);
1219 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1220 uinfo->count = num_analog_busses_in(chip);
1221 uinfo->value.integer.min = 0;
1222 uinfo->value.integer.max = 1;
1223 return 0;
1226 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1227 struct snd_ctl_elem_value *ucontrol)
1229 struct echoaudio *chip;
1230 int c;
1232 chip = snd_kcontrol_chip(kcontrol);
1233 for (c = 0; c < num_analog_busses_in(chip); c++)
1234 ucontrol->value.integer.value[c] =
1235 chip->nominal_level[bx_analog_in(chip) + c];
1236 return 0;
1239 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1240 struct snd_ctl_elem_value *ucontrol)
1242 struct echoaudio *chip;
1243 int c, changed;
1245 changed = 0;
1246 chip = snd_kcontrol_chip(kcontrol);
1247 spin_lock_irq(&chip->lock);
1248 for (c = 0; c < num_analog_busses_in(chip); c++) {
1249 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1250 ucontrol->value.integer.value[c]) {
1251 set_nominal_level(chip, bx_analog_in(chip) + c,
1252 ucontrol->value.integer.value[c]);
1253 changed = 1;
1256 if (changed)
1257 update_output_line_level(chip); /* "Output" is not a mistake
1258 * here.
1260 spin_unlock_irq(&chip->lock);
1261 return changed;
1264 static struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1265 .name = "Line Capture Switch (-10dBV)",
1266 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1267 .info = snd_echo_input_nominal_info,
1268 .get = snd_echo_input_nominal_get,
1269 .put = snd_echo_input_nominal_put,
1272 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1276 #ifdef ECHOCARD_HAS_MONITOR
1278 /******************* Monitor mixer *******************/
1279 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1280 struct snd_ctl_elem_info *uinfo)
1282 struct echoaudio *chip;
1284 chip = snd_kcontrol_chip(kcontrol);
1285 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1286 uinfo->count = 1;
1287 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1288 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1289 uinfo->dimen.d[0] = num_busses_out(chip);
1290 uinfo->dimen.d[1] = num_busses_in(chip);
1291 return 0;
1294 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1295 struct snd_ctl_elem_value *ucontrol)
1297 struct echoaudio *chip;
1299 chip = snd_kcontrol_chip(kcontrol);
1300 ucontrol->value.integer.value[0] =
1301 chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1302 [ucontrol->id.index % num_busses_in(chip)];
1303 return 0;
1306 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1307 struct snd_ctl_elem_value *ucontrol)
1309 struct echoaudio *chip;
1310 int changed, gain;
1311 short out, in;
1313 changed = 0;
1314 chip = snd_kcontrol_chip(kcontrol);
1315 out = ucontrol->id.index / num_busses_in(chip);
1316 in = ucontrol->id.index % num_busses_in(chip);
1317 gain = ucontrol->value.integer.value[0];
1318 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1319 return -EINVAL;
1320 if (chip->monitor_gain[out][in] != gain) {
1321 spin_lock_irq(&chip->lock);
1322 set_monitor_gain(chip, out, in, gain);
1323 update_output_line_level(chip);
1324 spin_unlock_irq(&chip->lock);
1325 changed = 1;
1327 return changed;
1330 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1331 .name = "Monitor Mixer Volume",
1332 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1333 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1334 .info = snd_echo_mixer_info,
1335 .get = snd_echo_mixer_get,
1336 .put = snd_echo_mixer_put,
1337 .tlv = {.p = db_scale_output_gain},
1340 #endif /* ECHOCARD_HAS_MONITOR */
1344 #ifdef ECHOCARD_HAS_VMIXER
1346 /******************* Vmixer *******************/
1347 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_info *uinfo)
1350 struct echoaudio *chip;
1352 chip = snd_kcontrol_chip(kcontrol);
1353 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1354 uinfo->count = 1;
1355 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1356 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1357 uinfo->dimen.d[0] = num_busses_out(chip);
1358 uinfo->dimen.d[1] = num_pipes_out(chip);
1359 return 0;
1362 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1363 struct snd_ctl_elem_value *ucontrol)
1365 struct echoaudio *chip;
1367 chip = snd_kcontrol_chip(kcontrol);
1368 ucontrol->value.integer.value[0] =
1369 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1370 [ucontrol->id.index % num_pipes_out(chip)];
1371 return 0;
1374 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1375 struct snd_ctl_elem_value *ucontrol)
1377 struct echoaudio *chip;
1378 int gain, changed;
1379 short vch, out;
1381 changed = 0;
1382 chip = snd_kcontrol_chip(kcontrol);
1383 out = ucontrol->id.index / num_pipes_out(chip);
1384 vch = ucontrol->id.index % num_pipes_out(chip);
1385 gain = ucontrol->value.integer.value[0];
1386 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1387 return -EINVAL;
1388 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1389 spin_lock_irq(&chip->lock);
1390 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1391 update_vmixer_level(chip);
1392 spin_unlock_irq(&chip->lock);
1393 changed = 1;
1395 return changed;
1398 static struct snd_kcontrol_new snd_echo_vmixer = {
1399 .name = "VMixer Volume",
1400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1401 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1402 .info = snd_echo_vmixer_info,
1403 .get = snd_echo_vmixer_get,
1404 .put = snd_echo_vmixer_put,
1405 .tlv = {.p = db_scale_output_gain},
1408 #endif /* ECHOCARD_HAS_VMIXER */
1412 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1414 /******************* Digital mode switch *******************/
1415 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1416 struct snd_ctl_elem_info *uinfo)
1418 static char *names[4] = {
1419 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1420 "S/PDIF Cdrom"
1422 struct echoaudio *chip;
1424 chip = snd_kcontrol_chip(kcontrol);
1425 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1426 uinfo->value.enumerated.items = chip->num_digital_modes;
1427 uinfo->count = 1;
1428 if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1429 uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1430 strcpy(uinfo->value.enumerated.name, names[
1431 chip->digital_mode_list[uinfo->value.enumerated.item]]);
1432 return 0;
1435 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_value *ucontrol)
1438 struct echoaudio *chip;
1439 int i, mode;
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;
1446 break;
1448 return 0;
1451 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1454 struct echoaudio *chip;
1455 int changed;
1456 unsigned short emode, dmode;
1458 changed = 0;
1459 chip = snd_kcontrol_chip(kcontrol);
1461 emode = ucontrol->value.enumerated.item[0];
1462 if (emode >= chip->num_digital_modes)
1463 return -EINVAL;
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)) {
1475 changed = -EAGAIN;
1476 } else {
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 DE_ACT(("SDM() =%d\n", changed));
1485 if (changed >= 0)
1486 changed = 1; /* No errors */
1488 mutex_unlock(&chip->mode_mutex);
1490 return changed;
1493 static struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1494 .name = "Digital mode Switch",
1495 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1496 .info = snd_echo_digital_mode_info,
1497 .get = snd_echo_digital_mode_get,
1498 .put = snd_echo_digital_mode_put,
1501 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1505 #ifdef ECHOCARD_HAS_DIGITAL_IO
1507 /******************* S/PDIF mode switch *******************/
1508 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1509 struct snd_ctl_elem_info *uinfo)
1511 static char *names[2] = {"Consumer", "Professional"};
1513 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1514 uinfo->value.enumerated.items = 2;
1515 uinfo->count = 1;
1516 if (uinfo->value.enumerated.item)
1517 uinfo->value.enumerated.item = 1;
1518 strcpy(uinfo->value.enumerated.name,
1519 names[uinfo->value.enumerated.item]);
1520 return 0;
1523 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1524 struct snd_ctl_elem_value *ucontrol)
1526 struct echoaudio *chip;
1528 chip = snd_kcontrol_chip(kcontrol);
1529 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1530 return 0;
1533 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_value *ucontrol)
1536 struct echoaudio *chip;
1537 int mode;
1539 chip = snd_kcontrol_chip(kcontrol);
1540 mode = !!ucontrol->value.enumerated.item[0];
1541 if (mode != chip->professional_spdif) {
1542 spin_lock_irq(&chip->lock);
1543 set_professional_spdif(chip, mode);
1544 spin_unlock_irq(&chip->lock);
1545 return 1;
1547 return 0;
1550 static struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1551 .name = "S/PDIF mode Switch",
1552 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1553 .info = snd_echo_spdif_mode_info,
1554 .get = snd_echo_spdif_mode_get,
1555 .put = snd_echo_spdif_mode_put,
1558 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1562 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1564 /******************* Select input clock source *******************/
1565 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_info *uinfo)
1568 static char *names[8] = {
1569 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1570 "ESync96", "MTC"
1572 struct echoaudio *chip;
1574 chip = snd_kcontrol_chip(kcontrol);
1575 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1576 uinfo->value.enumerated.items = chip->num_clock_sources;
1577 uinfo->count = 1;
1578 if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1579 uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1580 strcpy(uinfo->value.enumerated.name, names[
1581 chip->clock_source_list[uinfo->value.enumerated.item]]);
1582 return 0;
1585 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1586 struct snd_ctl_elem_value *ucontrol)
1588 struct echoaudio *chip;
1589 int i, clock;
1591 chip = snd_kcontrol_chip(kcontrol);
1592 clock = chip->input_clock;
1594 for (i = 0; i < chip->num_clock_sources; i++)
1595 if (clock == chip->clock_source_list[i])
1596 ucontrol->value.enumerated.item[0] = i;
1598 return 0;
1601 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1602 struct snd_ctl_elem_value *ucontrol)
1604 struct echoaudio *chip;
1605 int changed;
1606 unsigned int eclock, dclock;
1608 changed = 0;
1609 chip = snd_kcontrol_chip(kcontrol);
1610 eclock = ucontrol->value.enumerated.item[0];
1611 if (eclock >= chip->input_clock_types)
1612 return -EINVAL;
1613 dclock = chip->clock_source_list[eclock];
1614 if (chip->input_clock != dclock) {
1615 mutex_lock(&chip->mode_mutex);
1616 spin_lock_irq(&chip->lock);
1617 if ((changed = set_input_clock(chip, dclock)) == 0)
1618 changed = 1; /* no errors */
1619 spin_unlock_irq(&chip->lock);
1620 mutex_unlock(&chip->mode_mutex);
1623 if (changed < 0)
1624 DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1626 return changed;
1629 static struct snd_kcontrol_new snd_echo_clock_source_switch = {
1630 .name = "Sample Clock Source",
1631 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1632 .info = snd_echo_clock_source_info,
1633 .get = snd_echo_clock_source_get,
1634 .put = snd_echo_clock_source_put,
1637 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1641 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1643 /******************* Phantom power switch *******************/
1644 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1646 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1649 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1651 ucontrol->value.integer.value[0] = chip->phantom_power;
1652 return 0;
1655 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
1658 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1659 int power, changed = 0;
1661 power = !!ucontrol->value.integer.value[0];
1662 if (chip->phantom_power != power) {
1663 spin_lock_irq(&chip->lock);
1664 changed = set_phantom_power(chip, power);
1665 spin_unlock_irq(&chip->lock);
1666 if (changed == 0)
1667 changed = 1; /* no errors */
1669 return changed;
1672 static struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1673 .name = "Phantom power Switch",
1674 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1675 .info = snd_echo_phantom_power_info,
1676 .get = snd_echo_phantom_power_get,
1677 .put = snd_echo_phantom_power_put,
1680 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1684 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1686 /******************* Digital input automute switch *******************/
1687 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1689 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1692 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1694 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1695 return 0;
1698 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
1701 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1702 int automute, changed = 0;
1704 automute = !!ucontrol->value.integer.value[0];
1705 if (chip->digital_in_automute != automute) {
1706 spin_lock_irq(&chip->lock);
1707 changed = set_input_auto_mute(chip, automute);
1708 spin_unlock_irq(&chip->lock);
1709 if (changed == 0)
1710 changed = 1; /* no errors */
1712 return changed;
1715 static struct snd_kcontrol_new snd_echo_automute_switch = {
1716 .name = "Digital Capture Switch (automute)",
1717 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1718 .info = snd_echo_automute_info,
1719 .get = snd_echo_automute_get,
1720 .put = snd_echo_automute_put,
1723 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1727 /******************* VU-meters switch *******************/
1728 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1730 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1733 struct echoaudio *chip;
1735 chip = snd_kcontrol_chip(kcontrol);
1736 spin_lock_irq(&chip->lock);
1737 set_meters_on(chip, ucontrol->value.integer.value[0]);
1738 spin_unlock_irq(&chip->lock);
1739 return 1;
1742 static struct snd_kcontrol_new snd_echo_vumeters_switch = {
1743 .name = "VU-meters Switch",
1744 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1745 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1746 .info = snd_echo_vumeters_switch_info,
1747 .put = snd_echo_vumeters_switch_put,
1752 /***** Read VU-meters (input, output, analog and digital together) *****/
1753 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_info *uinfo)
1756 struct echoaudio *chip;
1758 chip = snd_kcontrol_chip(kcontrol);
1759 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1760 uinfo->count = 96;
1761 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1762 uinfo->value.integer.max = 0;
1763 #ifdef ECHOCARD_HAS_VMIXER
1764 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1765 #else
1766 uinfo->dimen.d[0] = 2; /* Out, In */
1767 #endif
1768 uinfo->dimen.d[1] = 16; /* 16 channels */
1769 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1770 return 0;
1773 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1774 struct snd_ctl_elem_value *ucontrol)
1776 struct echoaudio *chip;
1778 chip = snd_kcontrol_chip(kcontrol);
1779 get_audio_meters(chip, ucontrol->value.integer.value);
1780 return 0;
1783 static struct snd_kcontrol_new snd_echo_vumeters = {
1784 .name = "VU-meters",
1785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1786 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1787 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1788 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1789 .info = snd_echo_vumeters_info,
1790 .get = snd_echo_vumeters_get,
1791 .tlv = {.p = db_scale_output_gain},
1796 /*** Channels info - it exports informations about the number of channels ***/
1797 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_info *uinfo)
1800 struct echoaudio *chip;
1802 chip = snd_kcontrol_chip(kcontrol);
1803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1804 uinfo->count = 6;
1805 uinfo->value.integer.min = 0;
1806 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1807 return 0;
1810 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_value *ucontrol)
1813 struct echoaudio *chip;
1814 int detected, clocks, bit, src;
1816 chip = snd_kcontrol_chip(kcontrol);
1817 ucontrol->value.integer.value[0] = num_busses_in(chip);
1818 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1819 ucontrol->value.integer.value[2] = num_busses_out(chip);
1820 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1821 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1823 /* Compute the bitmask of the currently valid input clocks */
1824 detected = detect_input_clocks(chip);
1825 clocks = 0;
1826 src = chip->num_clock_sources - 1;
1827 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1828 if (detected & (1 << bit))
1829 for (; src >= 0; src--)
1830 if (bit == chip->clock_source_list[src]) {
1831 clocks |= 1 << src;
1832 break;
1834 ucontrol->value.integer.value[5] = clocks;
1836 return 0;
1839 static struct snd_kcontrol_new snd_echo_channels_info = {
1840 .name = "Channels info",
1841 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1842 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1843 .info = snd_echo_channels_info_info,
1844 .get = snd_echo_channels_info_get,
1850 /******************************************************************************
1851 IRQ Handler
1852 ******************************************************************************/
1854 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1856 struct echoaudio *chip = dev_id;
1857 struct snd_pcm_substream *substream;
1858 int period, ss, st;
1860 spin_lock(&chip->lock);
1861 st = service_irq(chip);
1862 if (st < 0) {
1863 spin_unlock(&chip->lock);
1864 return IRQ_NONE;
1866 /* The hardware doesn't tell us which substream caused the irq,
1867 thus we have to check all running substreams. */
1868 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1869 substream = chip->substream[ss];
1870 if (substream && ((struct audiopipe *)substream->runtime->
1871 private_data)->state == PIPE_STATE_STARTED) {
1872 period = pcm_pointer(substream) /
1873 substream->runtime->period_size;
1874 if (period != chip->last_period[ss]) {
1875 chip->last_period[ss] = period;
1876 spin_unlock(&chip->lock);
1877 snd_pcm_period_elapsed(substream);
1878 spin_lock(&chip->lock);
1882 spin_unlock(&chip->lock);
1884 #ifdef ECHOCARD_HAS_MIDI
1885 if (st > 0 && chip->midi_in) {
1886 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1887 DE_MID(("rawmidi_iread=%d\n", st));
1889 #endif
1890 return IRQ_HANDLED;
1896 /******************************************************************************
1897 Module construction / destruction
1898 ******************************************************************************/
1900 static int snd_echo_free(struct echoaudio *chip)
1902 DE_INIT(("Stop DSP...\n"));
1903 if (chip->comm_page)
1904 rest_in_peace(chip);
1905 DE_INIT(("Stopped.\n"));
1907 if (chip->irq >= 0)
1908 free_irq(chip->irq, chip);
1910 if (chip->comm_page)
1911 snd_dma_free_pages(&chip->commpage_dma_buf);
1913 if (chip->dsp_registers)
1914 iounmap(chip->dsp_registers);
1916 if (chip->iores)
1917 release_and_free_resource(chip->iores);
1919 DE_INIT(("MMIO freed.\n"));
1921 pci_disable_device(chip->pci);
1923 /* release chip data */
1924 free_firmware_cache(chip);
1925 kfree(chip);
1926 DE_INIT(("Chip freed.\n"));
1927 return 0;
1932 static int snd_echo_dev_free(struct snd_device *device)
1934 struct echoaudio *chip = device->device_data;
1936 DE_INIT(("snd_echo_dev_free()...\n"));
1937 return snd_echo_free(chip);
1942 /* <--snd_echo_probe() */
1943 static int snd_echo_create(struct snd_card *card,
1944 struct pci_dev *pci,
1945 struct echoaudio **rchip)
1947 struct echoaudio *chip;
1948 int err;
1949 size_t sz;
1950 static struct snd_device_ops ops = {
1951 .dev_free = snd_echo_dev_free,
1954 *rchip = NULL;
1956 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1958 if ((err = pci_enable_device(pci)) < 0)
1959 return err;
1960 pci_set_master(pci);
1962 /* Allocate chip if needed */
1963 if (!*rchip) {
1964 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1965 if (!chip) {
1966 pci_disable_device(pci);
1967 return -ENOMEM;
1969 DE_INIT(("chip=%p\n", chip));
1970 spin_lock_init(&chip->lock);
1971 chip->card = card;
1972 chip->pci = pci;
1973 chip->irq = -1;
1974 atomic_set(&chip->opencount, 0);
1975 mutex_init(&chip->mode_mutex);
1976 chip->can_set_rate = 1;
1977 } else {
1978 /* If this was called from the resume function, chip is
1979 * already allocated and it contains current card settings.
1981 chip = *rchip;
1984 /* PCI resource allocation */
1985 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1986 sz = pci_resource_len(pci, 0);
1987 if (sz > PAGE_SIZE)
1988 sz = PAGE_SIZE; /* We map only the required part */
1990 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1991 ECHOCARD_NAME)) == NULL) {
1992 snd_echo_free(chip);
1993 snd_printk(KERN_ERR "cannot get memory region\n");
1994 return -EBUSY;
1996 chip->dsp_registers = (volatile u32 __iomem *)
1997 ioremap_nocache(chip->dsp_registers_phys, sz);
1999 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2000 KBUILD_MODNAME, chip)) {
2001 snd_echo_free(chip);
2002 snd_printk(KERN_ERR "cannot grab irq\n");
2003 return -EBUSY;
2005 chip->irq = pci->irq;
2006 DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
2007 chip->pci, chip->irq, chip->pci->subsystem_device));
2009 /* Create the DSP comm page - this is the area of memory used for most
2010 of the communication with the DSP, which accesses it via bus mastering */
2011 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2012 sizeof(struct comm_page),
2013 &chip->commpage_dma_buf) < 0) {
2014 snd_echo_free(chip);
2015 snd_printk(KERN_ERR "cannot allocate the comm page\n");
2016 return -ENOMEM;
2018 chip->comm_page_phys = chip->commpage_dma_buf.addr;
2019 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
2021 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2022 if (err >= 0)
2023 err = set_mixer_defaults(chip);
2024 if (err < 0) {
2025 DE_INIT(("init_hw err=%d\n", err));
2026 snd_echo_free(chip);
2027 return err;
2029 DE_INIT(("Card init OK\n"));
2031 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2032 snd_echo_free(chip);
2033 return err;
2035 *rchip = chip;
2036 /* Init done ! */
2037 return 0;
2042 /* constructor */
2043 static int snd_echo_probe(struct pci_dev *pci,
2044 const struct pci_device_id *pci_id)
2046 static int dev;
2047 struct snd_card *card;
2048 struct echoaudio *chip;
2049 char *dsp;
2050 int i, err;
2052 if (dev >= SNDRV_CARDS)
2053 return -ENODEV;
2054 if (!enable[dev]) {
2055 dev++;
2056 return -ENOENT;
2059 DE_INIT(("Echoaudio driver starting...\n"));
2060 i = 0;
2061 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2062 if (err < 0)
2063 return err;
2065 snd_card_set_dev(card, &pci->dev);
2067 chip = NULL; /* Tells snd_echo_create to allocate chip */
2068 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2069 snd_card_free(card);
2070 return err;
2073 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2074 strcpy(card->shortname, chip->card_name);
2076 dsp = "56301";
2077 if (pci_id->device == 0x3410)
2078 dsp = "56361";
2080 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2081 card->shortname, pci_id->subdevice & 0x000f, dsp,
2082 chip->dsp_registers_phys, chip->irq);
2084 if ((err = snd_echo_new_pcm(chip)) < 0) {
2085 snd_printk(KERN_ERR "new pcm error %d\n", err);
2086 snd_card_free(card);
2087 return err;
2090 #ifdef ECHOCARD_HAS_MIDI
2091 if (chip->has_midi) { /* Some Mia's do not have midi */
2092 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2093 snd_printk(KERN_ERR "new midi error %d\n", err);
2094 snd_card_free(card);
2095 return err;
2098 #endif
2100 #ifdef ECHOCARD_HAS_VMIXER
2101 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2102 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2103 goto ctl_error;
2104 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2105 err = snd_ctl_add(chip->card,
2106 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2107 if (err < 0)
2108 goto ctl_error;
2109 #endif
2110 #else /* ECHOCARD_HAS_VMIXER */
2111 err = snd_ctl_add(chip->card,
2112 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2113 if (err < 0)
2114 goto ctl_error;
2115 #endif /* ECHOCARD_HAS_VMIXER */
2117 #ifdef ECHOCARD_HAS_INPUT_GAIN
2118 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2119 goto ctl_error;
2120 #endif
2122 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2123 if (!chip->hasnt_input_nominal_level)
2124 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2125 goto ctl_error;
2126 #endif
2128 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2129 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2130 goto ctl_error;
2131 #endif
2133 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2134 goto ctl_error;
2136 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2137 goto ctl_error;
2139 #ifdef ECHOCARD_HAS_MONITOR
2140 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2141 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2142 goto ctl_error;
2143 #endif
2145 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2146 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2147 goto ctl_error;
2148 #endif
2150 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2151 goto ctl_error;
2153 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2154 /* Creates a list of available digital modes */
2155 chip->num_digital_modes = 0;
2156 for (i = 0; i < 6; i++)
2157 if (chip->digital_modes & (1 << i))
2158 chip->digital_mode_list[chip->num_digital_modes++] = i;
2160 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2161 goto ctl_error;
2162 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2164 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2165 /* Creates a list of available clock sources */
2166 chip->num_clock_sources = 0;
2167 for (i = 0; i < 10; i++)
2168 if (chip->input_clock_types & (1 << i))
2169 chip->clock_source_list[chip->num_clock_sources++] = i;
2171 if (chip->num_clock_sources > 1) {
2172 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2173 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2174 goto ctl_error;
2176 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2178 #ifdef ECHOCARD_HAS_DIGITAL_IO
2179 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2180 goto ctl_error;
2181 #endif
2183 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2184 if (chip->has_phantom_power)
2185 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2186 goto ctl_error;
2187 #endif
2189 err = snd_card_register(card);
2190 if (err < 0)
2191 goto ctl_error;
2192 snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2194 pci_set_drvdata(pci, chip);
2195 dev++;
2196 return 0;
2198 ctl_error:
2199 snd_printk(KERN_ERR "new control error %d\n", err);
2200 snd_card_free(card);
2201 return err;
2206 #if defined(CONFIG_PM_SLEEP)
2208 static int snd_echo_suspend(struct device *dev)
2210 struct pci_dev *pci = to_pci_dev(dev);
2211 struct echoaudio *chip = dev_get_drvdata(dev);
2213 DE_INIT(("suspend start\n"));
2214 snd_pcm_suspend_all(chip->analog_pcm);
2215 snd_pcm_suspend_all(chip->digital_pcm);
2217 #ifdef ECHOCARD_HAS_MIDI
2218 /* This call can sleep */
2219 if (chip->midi_out)
2220 snd_echo_midi_output_trigger(chip->midi_out, 0);
2221 #endif
2222 spin_lock_irq(&chip->lock);
2223 if (wait_handshake(chip)) {
2224 spin_unlock_irq(&chip->lock);
2225 return -EIO;
2227 clear_handshake(chip);
2228 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2229 spin_unlock_irq(&chip->lock);
2230 return -EIO;
2232 spin_unlock_irq(&chip->lock);
2234 chip->dsp_code = NULL;
2235 free_irq(chip->irq, chip);
2236 chip->irq = -1;
2237 pci_save_state(pci);
2238 pci_disable_device(pci);
2240 DE_INIT(("suspend done\n"));
2241 return 0;
2246 static int snd_echo_resume(struct device *dev)
2248 struct pci_dev *pci = to_pci_dev(dev);
2249 struct echoaudio *chip = dev_get_drvdata(dev);
2250 struct comm_page *commpage, *commpage_bak;
2251 u32 pipe_alloc_mask;
2252 int err;
2254 DE_INIT(("resume start\n"));
2255 pci_restore_state(pci);
2256 commpage_bak = kmalloc(sizeof(struct echoaudio), GFP_KERNEL);
2257 if (commpage_bak == NULL)
2258 return -ENOMEM;
2259 commpage = chip->comm_page;
2260 memcpy(commpage_bak, commpage, sizeof(struct comm_page));
2262 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2263 if (err < 0) {
2264 kfree(commpage_bak);
2265 DE_INIT(("resume init_hw err=%d\n", err));
2266 snd_echo_free(chip);
2267 return err;
2269 DE_INIT(("resume init OK\n"));
2271 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2272 * restore_dsp_settings() fails.
2274 pipe_alloc_mask = chip->pipe_alloc_mask;
2275 chip->pipe_alloc_mask = 0;
2276 err = restore_dsp_rettings(chip);
2277 chip->pipe_alloc_mask = pipe_alloc_mask;
2278 if (err < 0) {
2279 kfree(commpage_bak);
2280 return err;
2282 DE_INIT(("resume restore OK\n"));
2284 memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2285 sizeof(commpage->audio_format));
2286 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2287 sizeof(commpage->sglist_addr));
2288 memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2289 sizeof(commpage->midi_output));
2290 kfree(commpage_bak);
2292 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2293 KBUILD_MODNAME, chip)) {
2294 snd_echo_free(chip);
2295 snd_printk(KERN_ERR "cannot grab irq\n");
2296 return -EBUSY;
2298 chip->irq = pci->irq;
2299 DE_INIT(("resume irq=%d\n", chip->irq));
2301 #ifdef ECHOCARD_HAS_MIDI
2302 if (chip->midi_input_enabled)
2303 enable_midi_input(chip, TRUE);
2304 if (chip->midi_out)
2305 snd_echo_midi_output_trigger(chip->midi_out, 1);
2306 #endif
2308 DE_INIT(("resume done\n"));
2309 return 0;
2312 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2313 #define SND_ECHO_PM_OPS &snd_echo_pm
2314 #else
2315 #define SND_ECHO_PM_OPS NULL
2316 #endif /* CONFIG_PM_SLEEP */
2319 static void snd_echo_remove(struct pci_dev *pci)
2321 struct echoaudio *chip;
2323 chip = pci_get_drvdata(pci);
2324 if (chip)
2325 snd_card_free(chip->card);
2326 pci_set_drvdata(pci, NULL);
2331 /******************************************************************************
2332 Everything starts and ends here
2333 ******************************************************************************/
2335 /* pci_driver definition */
2336 static struct pci_driver echo_driver = {
2337 .name = KBUILD_MODNAME,
2338 .id_table = snd_echo_ids,
2339 .probe = snd_echo_probe,
2340 .remove = snd_echo_remove,
2341 .driver = {
2342 .pm = SND_ECHO_PM_OPS,
2346 module_pci_driver(echo_driver);