at91: at91sam9g45 family: identify several chip versions
[linux-2.6/next.git] / sound / pci / echoaudio / echoaudio.c
blob1305f7ca02c3c2726049dc448dec6b7fa36a63cd
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 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
20 MODULE_LICENSE("GPL v2");
21 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
22 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
23 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
27 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
36 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
37 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
39 static int get_firmware(const struct firmware **fw_entry,
40 const struct firmware *frm, struct echoaudio *chip)
42 int err;
43 char name[30];
44 DE_ACT(("firmware requested: %s\n", frm->data));
45 snprintf(name, sizeof(name), "ea/%s", frm->data);
46 if ((err = request_firmware(fw_entry, name, pci_device(chip))) < 0)
47 snd_printk(KERN_ERR "get_firmware(): Firmware not available (%d)\n", err);
48 return err;
51 static void free_firmware(const struct firmware *fw_entry)
53 release_firmware(fw_entry);
54 DE_ACT(("firmware released\n"));
59 /******************************************************************************
60 PCM interface
61 ******************************************************************************/
63 static void audiopipe_free(struct snd_pcm_runtime *runtime)
65 struct audiopipe *pipe = runtime->private_data;
67 if (pipe->sgpage.area)
68 snd_dma_free_pages(&pipe->sgpage);
69 kfree(pipe);
74 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
75 struct snd_pcm_hw_rule *rule)
77 struct snd_interval *c = hw_param_interval(params,
78 SNDRV_PCM_HW_PARAM_CHANNELS);
79 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
80 struct snd_mask fmt;
82 snd_mask_any(&fmt);
84 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
85 /* >=2 channels cannot be S32_BE */
86 if (c->min == 2) {
87 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
88 return snd_mask_refine(f, &fmt);
90 #endif
91 /* > 2 channels cannot be U8 and S32_BE */
92 if (c->min > 2) {
93 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
94 return snd_mask_refine(f, &fmt);
96 /* Mono is ok with any format */
97 return 0;
102 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
103 struct snd_pcm_hw_rule *rule)
105 struct snd_interval *c = hw_param_interval(params,
106 SNDRV_PCM_HW_PARAM_CHANNELS);
107 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
108 struct snd_interval ch;
110 snd_interval_any(&ch);
112 /* S32_BE is mono (and stereo) only */
113 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
114 ch.min = 1;
115 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
116 ch.max = 2;
117 #else
118 ch.max = 1;
119 #endif
120 ch.integer = 1;
121 return snd_interval_refine(c, &ch);
123 /* U8 can be only mono or stereo */
124 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
125 ch.min = 1;
126 ch.max = 2;
127 ch.integer = 1;
128 return snd_interval_refine(c, &ch);
130 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
131 return 0;
136 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
137 struct snd_pcm_hw_rule *rule)
139 struct snd_interval *c = hw_param_interval(params,
140 SNDRV_PCM_HW_PARAM_CHANNELS);
141 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
142 struct snd_mask fmt;
143 u64 fmask;
144 snd_mask_any(&fmt);
146 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
148 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
149 if (c->min > 2) {
150 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
151 SNDRV_PCM_FMTBIT_S24_3LE |
152 SNDRV_PCM_FMTBIT_S32_LE;
153 /* 1 channel must be S32_BE or S32_LE */
154 } else if (c->max == 1)
155 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
156 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
157 /* 2 channels cannot be S32_BE */
158 else if (c->min == 2 && c->max == 2)
159 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
160 #endif
161 else
162 return 0;
164 fmt.bits[0] &= (u32)fmask;
165 fmt.bits[1] &= (u32)(fmask >> 32);
166 return snd_mask_refine(f, &fmt);
171 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
172 struct snd_pcm_hw_rule *rule)
174 struct snd_interval *c = hw_param_interval(params,
175 SNDRV_PCM_HW_PARAM_CHANNELS);
176 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
177 struct snd_interval ch;
178 u64 fmask;
180 snd_interval_any(&ch);
181 ch.integer = 1;
182 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
184 /* S32_BE is mono (and stereo) only */
185 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
186 ch.min = 1;
187 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
188 ch.max = 2;
189 #else
190 ch.max = 1;
191 #endif
192 /* U8 is stereo only */
193 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
194 ch.min = ch.max = 2;
195 /* S16_LE and S24_3LE must be at least stereo */
196 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
197 SNDRV_PCM_FMTBIT_S24_3LE)))
198 ch.min = 2;
199 else
200 return 0;
202 return snd_interval_refine(c, &ch);
207 /* Since the sample rate is a global setting, do allow the user to change the
208 sample rate only if there is only one pcm device open. */
209 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
210 struct snd_pcm_hw_rule *rule)
212 struct snd_interval *rate = hw_param_interval(params,
213 SNDRV_PCM_HW_PARAM_RATE);
214 struct echoaudio *chip = rule->private;
215 struct snd_interval fixed;
217 if (!chip->can_set_rate) {
218 snd_interval_any(&fixed);
219 fixed.min = fixed.max = chip->sample_rate;
220 return snd_interval_refine(rate, &fixed);
222 return 0;
226 static int pcm_open(struct snd_pcm_substream *substream,
227 signed char max_channels)
229 struct echoaudio *chip;
230 struct snd_pcm_runtime *runtime;
231 struct audiopipe *pipe;
232 int err, i;
234 if (max_channels <= 0)
235 return -EAGAIN;
237 chip = snd_pcm_substream_chip(substream);
238 runtime = substream->runtime;
240 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
241 if (!pipe)
242 return -ENOMEM;
243 pipe->index = -1; /* Not configured yet */
245 /* Set up hw capabilities and contraints */
246 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
247 DE_HWP(("max_channels=%d\n", max_channels));
248 pipe->constr.list = channels_list;
249 pipe->constr.mask = 0;
250 for (i = 0; channels_list[i] <= max_channels; i++);
251 pipe->constr.count = i;
252 if (pipe->hw.channels_max > max_channels)
253 pipe->hw.channels_max = max_channels;
254 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
255 pipe->hw.rate_max = 48000;
256 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
259 runtime->hw = pipe->hw;
260 runtime->private_data = pipe;
261 runtime->private_free = audiopipe_free;
262 snd_pcm_set_sync(substream);
264 /* Only mono and any even number of channels are allowed */
265 if ((err = snd_pcm_hw_constraint_list(runtime, 0,
266 SNDRV_PCM_HW_PARAM_CHANNELS,
267 &pipe->constr)) < 0)
268 return err;
270 /* All periods should have the same size */
271 if ((err = snd_pcm_hw_constraint_integer(runtime,
272 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
273 return err;
275 /* The hw accesses memory in chunks 32 frames long and they should be
276 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
277 generated with a resolution of 32 frames. Thus we need the following */
278 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
279 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
280 32)) < 0)
281 return err;
282 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
283 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
284 32)) < 0)
285 return err;
287 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
288 SNDRV_PCM_HW_PARAM_RATE,
289 hw_rule_sample_rate, chip,
290 SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
291 return err;
293 /* Finally allocate a page for the scatter-gather list */
294 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
295 snd_dma_pci_data(chip->pci),
296 PAGE_SIZE, &pipe->sgpage)) < 0) {
297 DE_HWP(("s-g list allocation failed\n"));
298 return err;
301 return 0;
306 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
308 struct echoaudio *chip = snd_pcm_substream_chip(substream);
309 int err;
311 DE_ACT(("pcm_analog_in_open\n"));
312 if ((err = pcm_open(substream, num_analog_busses_in(chip) -
313 substream->number)) < 0)
314 return err;
315 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
316 SNDRV_PCM_HW_PARAM_CHANNELS,
317 hw_rule_capture_channels_by_format, NULL,
318 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
319 return err;
320 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
321 SNDRV_PCM_HW_PARAM_FORMAT,
322 hw_rule_capture_format_by_channels, NULL,
323 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
324 return err;
325 atomic_inc(&chip->opencount);
326 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
327 chip->can_set_rate=0;
328 DE_HWP(("pcm_analog_in_open cs=%d oc=%d r=%d\n",
329 chip->can_set_rate, atomic_read(&chip->opencount),
330 chip->sample_rate));
331 return 0;
336 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
338 struct echoaudio *chip = snd_pcm_substream_chip(substream);
339 int max_channels, err;
341 #ifdef ECHOCARD_HAS_VMIXER
342 max_channels = num_pipes_out(chip);
343 #else
344 max_channels = num_analog_busses_out(chip);
345 #endif
346 DE_ACT(("pcm_analog_out_open\n"));
347 if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
348 return err;
349 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
350 SNDRV_PCM_HW_PARAM_CHANNELS,
351 hw_rule_playback_channels_by_format,
352 NULL,
353 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
354 return err;
355 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
356 SNDRV_PCM_HW_PARAM_FORMAT,
357 hw_rule_playback_format_by_channels,
358 NULL,
359 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
360 return err;
361 atomic_inc(&chip->opencount);
362 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
363 chip->can_set_rate=0;
364 DE_HWP(("pcm_analog_out_open cs=%d oc=%d r=%d\n",
365 chip->can_set_rate, atomic_read(&chip->opencount),
366 chip->sample_rate));
367 return 0;
372 #ifdef ECHOCARD_HAS_DIGITAL_IO
374 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
376 struct echoaudio *chip = snd_pcm_substream_chip(substream);
377 int err, max_channels;
379 DE_ACT(("pcm_digital_in_open\n"));
380 max_channels = num_digital_busses_in(chip) - substream->number;
381 mutex_lock(&chip->mode_mutex);
382 if (chip->digital_mode == DIGITAL_MODE_ADAT)
383 err = pcm_open(substream, max_channels);
384 else /* If the card has ADAT, subtract the 6 channels
385 * that S/PDIF doesn't have
387 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
389 if (err < 0)
390 goto din_exit;
392 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
393 SNDRV_PCM_HW_PARAM_CHANNELS,
394 hw_rule_capture_channels_by_format, NULL,
395 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
396 goto din_exit;
397 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
398 SNDRV_PCM_HW_PARAM_FORMAT,
399 hw_rule_capture_format_by_channels, NULL,
400 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
401 goto din_exit;
403 atomic_inc(&chip->opencount);
404 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
405 chip->can_set_rate=0;
407 din_exit:
408 mutex_unlock(&chip->mode_mutex);
409 return err;
414 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
416 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
418 struct echoaudio *chip = snd_pcm_substream_chip(substream);
419 int err, max_channels;
421 DE_ACT(("pcm_digital_out_open\n"));
422 max_channels = num_digital_busses_out(chip) - substream->number;
423 mutex_lock(&chip->mode_mutex);
424 if (chip->digital_mode == DIGITAL_MODE_ADAT)
425 err = pcm_open(substream, max_channels);
426 else /* If the card has ADAT, subtract the 6 channels
427 * that S/PDIF doesn't have
429 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
431 if (err < 0)
432 goto dout_exit;
434 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
435 SNDRV_PCM_HW_PARAM_CHANNELS,
436 hw_rule_playback_channels_by_format,
437 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
438 -1)) < 0)
439 goto dout_exit;
440 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
441 SNDRV_PCM_HW_PARAM_FORMAT,
442 hw_rule_playback_format_by_channels,
443 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
444 -1)) < 0)
445 goto dout_exit;
446 atomic_inc(&chip->opencount);
447 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
448 chip->can_set_rate=0;
449 dout_exit:
450 mutex_unlock(&chip->mode_mutex);
451 return err;
454 #endif /* !ECHOCARD_HAS_VMIXER */
456 #endif /* ECHOCARD_HAS_DIGITAL_IO */
460 static int pcm_close(struct snd_pcm_substream *substream)
462 struct echoaudio *chip = snd_pcm_substream_chip(substream);
463 int oc;
465 /* Nothing to do here. Audio is already off and pipe will be
466 * freed by its callback
468 DE_ACT(("pcm_close\n"));
470 atomic_dec(&chip->opencount);
471 oc = atomic_read(&chip->opencount);
472 DE_ACT(("pcm_close oc=%d cs=%d rs=%d\n", oc,
473 chip->can_set_rate, chip->rate_set));
474 if (oc < 2)
475 chip->can_set_rate = 1;
476 if (oc == 0)
477 chip->rate_set = 0;
478 DE_ACT(("pcm_close2 oc=%d cs=%d rs=%d\n", oc,
479 chip->can_set_rate,chip->rate_set));
481 return 0;
486 /* Channel allocation and scatter-gather list setup */
487 static int init_engine(struct snd_pcm_substream *substream,
488 struct snd_pcm_hw_params *hw_params,
489 int pipe_index, int interleave)
491 struct echoaudio *chip;
492 int err, per, rest, page, edge, offs;
493 struct audiopipe *pipe;
495 chip = snd_pcm_substream_chip(substream);
496 pipe = (struct audiopipe *) substream->runtime->private_data;
498 /* Sets up che hardware. If it's already initialized, reset and
499 * redo with the new parameters
501 spin_lock_irq(&chip->lock);
502 if (pipe->index >= 0) {
503 DE_HWP(("hwp_ie free(%d)\n", pipe->index));
504 err = free_pipes(chip, pipe);
505 snd_BUG_ON(err);
506 chip->substream[pipe->index] = NULL;
509 err = allocate_pipes(chip, pipe, pipe_index, interleave);
510 if (err < 0) {
511 spin_unlock_irq(&chip->lock);
512 DE_ACT((KERN_NOTICE "allocate_pipes(%d) err=%d\n",
513 pipe_index, err));
514 return err;
516 spin_unlock_irq(&chip->lock);
517 DE_ACT((KERN_NOTICE "allocate_pipes()=%d\n", pipe_index));
519 DE_HWP(("pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
520 params_buffer_bytes(hw_params), params_periods(hw_params),
521 params_period_bytes(hw_params)));
522 err = snd_pcm_lib_malloc_pages(substream,
523 params_buffer_bytes(hw_params));
524 if (err < 0) {
525 snd_printk(KERN_ERR "malloc_pages err=%d\n", err);
526 spin_lock_irq(&chip->lock);
527 free_pipes(chip, pipe);
528 spin_unlock_irq(&chip->lock);
529 pipe->index = -1;
530 return err;
533 sglist_init(chip, pipe);
534 edge = PAGE_SIZE;
535 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
536 per++) {
537 rest = params_period_bytes(hw_params);
538 if (offs + rest > params_buffer_bytes(hw_params))
539 rest = params_buffer_bytes(hw_params) - offs;
540 while (rest) {
541 dma_addr_t addr;
542 addr = snd_pcm_sgbuf_get_addr(substream, offs);
543 if (rest <= edge - offs) {
544 sglist_add_mapping(chip, pipe, addr, rest);
545 sglist_add_irq(chip, pipe);
546 offs += rest;
547 rest = 0;
548 } else {
549 sglist_add_mapping(chip, pipe, addr,
550 edge - offs);
551 rest -= edge - offs;
552 offs = edge;
554 if (offs == edge) {
555 edge += PAGE_SIZE;
556 page++;
561 /* Close the ring buffer */
562 sglist_wrap(chip, pipe);
564 /* This stuff is used by the irq handler, so it must be
565 * initialized before chip->substream
567 chip->last_period[pipe_index] = 0;
568 pipe->last_counter = 0;
569 pipe->position = 0;
570 smp_wmb();
571 chip->substream[pipe_index] = substream;
572 chip->rate_set = 1;
573 spin_lock_irq(&chip->lock);
574 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
575 spin_unlock_irq(&chip->lock);
576 DE_HWP(("pcm_hw_params ok\n"));
577 return 0;
582 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
583 struct snd_pcm_hw_params *hw_params)
585 struct echoaudio *chip = snd_pcm_substream_chip(substream);
587 return init_engine(substream, hw_params, px_analog_in(chip) +
588 substream->number, params_channels(hw_params));
593 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *hw_params)
596 return init_engine(substream, hw_params, substream->number,
597 params_channels(hw_params));
602 #ifdef ECHOCARD_HAS_DIGITAL_IO
604 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
605 struct snd_pcm_hw_params *hw_params)
607 struct echoaudio *chip = snd_pcm_substream_chip(substream);
609 return init_engine(substream, hw_params, px_digital_in(chip) +
610 substream->number, params_channels(hw_params));
615 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
616 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
617 struct snd_pcm_hw_params *hw_params)
619 struct echoaudio *chip = snd_pcm_substream_chip(substream);
621 return init_engine(substream, hw_params, px_digital_out(chip) +
622 substream->number, params_channels(hw_params));
624 #endif /* !ECHOCARD_HAS_VMIXER */
626 #endif /* ECHOCARD_HAS_DIGITAL_IO */
630 static int pcm_hw_free(struct snd_pcm_substream *substream)
632 struct echoaudio *chip;
633 struct audiopipe *pipe;
635 chip = snd_pcm_substream_chip(substream);
636 pipe = (struct audiopipe *) substream->runtime->private_data;
638 spin_lock_irq(&chip->lock);
639 if (pipe->index >= 0) {
640 DE_HWP(("pcm_hw_free(%d)\n", pipe->index));
641 free_pipes(chip, pipe);
642 chip->substream[pipe->index] = NULL;
643 pipe->index = -1;
645 spin_unlock_irq(&chip->lock);
647 DE_HWP(("pcm_hw_freed\n"));
648 snd_pcm_lib_free_pages(substream);
649 return 0;
654 static int pcm_prepare(struct snd_pcm_substream *substream)
656 struct echoaudio *chip = snd_pcm_substream_chip(substream);
657 struct snd_pcm_runtime *runtime = substream->runtime;
658 struct audioformat format;
659 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
661 DE_HWP(("Prepare rate=%d format=%d channels=%d\n",
662 runtime->rate, runtime->format, runtime->channels));
663 format.interleave = runtime->channels;
664 format.data_are_bigendian = 0;
665 format.mono_to_stereo = 0;
666 switch (runtime->format) {
667 case SNDRV_PCM_FORMAT_U8:
668 format.bits_per_sample = 8;
669 break;
670 case SNDRV_PCM_FORMAT_S16_LE:
671 format.bits_per_sample = 16;
672 break;
673 case SNDRV_PCM_FORMAT_S24_3LE:
674 format.bits_per_sample = 24;
675 break;
676 case SNDRV_PCM_FORMAT_S32_BE:
677 format.data_are_bigendian = 1;
678 case SNDRV_PCM_FORMAT_S32_LE:
679 format.bits_per_sample = 32;
680 break;
681 default:
682 DE_HWP(("Prepare error: unsupported format %d\n",
683 runtime->format));
684 return -EINVAL;
687 if (snd_BUG_ON(pipe_index >= px_num(chip)))
688 return -EINVAL;
689 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
690 return -EINVAL;
691 set_audio_format(chip, pipe_index, &format);
692 return 0;
697 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
699 struct echoaudio *chip = snd_pcm_substream_chip(substream);
700 struct snd_pcm_runtime *runtime = substream->runtime;
701 struct audiopipe *pipe = runtime->private_data;
702 int i, err;
703 u32 channelmask = 0;
704 struct snd_pcm_substream *s;
706 snd_pcm_group_for_each_entry(s, substream) {
707 for (i = 0; i < DSP_MAXPIPES; i++) {
708 if (s == chip->substream[i]) {
709 channelmask |= 1 << i;
710 snd_pcm_trigger_done(s, substream);
715 spin_lock(&chip->lock);
716 switch (cmd) {
717 case SNDRV_PCM_TRIGGER_START:
718 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
719 DE_ACT(("pcm_trigger start\n"));
720 for (i = 0; i < DSP_MAXPIPES; i++) {
721 if (channelmask & (1 << i)) {
722 pipe = chip->substream[i]->runtime->private_data;
723 switch (pipe->state) {
724 case PIPE_STATE_STOPPED:
725 chip->last_period[i] = 0;
726 pipe->last_counter = 0;
727 pipe->position = 0;
728 *pipe->dma_counter = 0;
729 case PIPE_STATE_PAUSED:
730 pipe->state = PIPE_STATE_STARTED;
731 break;
732 case PIPE_STATE_STARTED:
733 break;
737 err = start_transport(chip, channelmask,
738 chip->pipe_cyclic_mask);
739 break;
740 case SNDRV_PCM_TRIGGER_STOP:
741 DE_ACT(("pcm_trigger stop\n"));
742 for (i = 0; i < DSP_MAXPIPES; i++) {
743 if (channelmask & (1 << i)) {
744 pipe = chip->substream[i]->runtime->private_data;
745 pipe->state = PIPE_STATE_STOPPED;
748 err = stop_transport(chip, channelmask);
749 break;
750 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
751 DE_ACT(("pcm_trigger pause\n"));
752 for (i = 0; i < DSP_MAXPIPES; i++) {
753 if (channelmask & (1 << i)) {
754 pipe = chip->substream[i]->runtime->private_data;
755 pipe->state = PIPE_STATE_PAUSED;
758 err = pause_transport(chip, channelmask);
759 break;
760 default:
761 err = -EINVAL;
763 spin_unlock(&chip->lock);
764 return err;
769 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
771 struct snd_pcm_runtime *runtime = substream->runtime;
772 struct audiopipe *pipe = runtime->private_data;
773 size_t cnt, bufsize, pos;
775 cnt = le32_to_cpu(*pipe->dma_counter);
776 pipe->position += cnt - pipe->last_counter;
777 pipe->last_counter = cnt;
778 bufsize = substream->runtime->buffer_size;
779 pos = bytes_to_frames(substream->runtime, pipe->position);
781 while (pos >= bufsize) {
782 pipe->position -= frames_to_bytes(substream->runtime, bufsize);
783 pos -= bufsize;
785 return pos;
790 /* pcm *_ops structures */
791 static struct snd_pcm_ops analog_playback_ops = {
792 .open = pcm_analog_out_open,
793 .close = pcm_close,
794 .ioctl = snd_pcm_lib_ioctl,
795 .hw_params = pcm_analog_out_hw_params,
796 .hw_free = pcm_hw_free,
797 .prepare = pcm_prepare,
798 .trigger = pcm_trigger,
799 .pointer = pcm_pointer,
800 .page = snd_pcm_sgbuf_ops_page,
802 static struct snd_pcm_ops analog_capture_ops = {
803 .open = pcm_analog_in_open,
804 .close = pcm_close,
805 .ioctl = snd_pcm_lib_ioctl,
806 .hw_params = pcm_analog_in_hw_params,
807 .hw_free = pcm_hw_free,
808 .prepare = pcm_prepare,
809 .trigger = pcm_trigger,
810 .pointer = pcm_pointer,
811 .page = snd_pcm_sgbuf_ops_page,
813 #ifdef ECHOCARD_HAS_DIGITAL_IO
814 #ifndef ECHOCARD_HAS_VMIXER
815 static struct snd_pcm_ops digital_playback_ops = {
816 .open = pcm_digital_out_open,
817 .close = pcm_close,
818 .ioctl = snd_pcm_lib_ioctl,
819 .hw_params = pcm_digital_out_hw_params,
820 .hw_free = pcm_hw_free,
821 .prepare = pcm_prepare,
822 .trigger = pcm_trigger,
823 .pointer = pcm_pointer,
824 .page = snd_pcm_sgbuf_ops_page,
826 #endif /* !ECHOCARD_HAS_VMIXER */
827 static struct snd_pcm_ops digital_capture_ops = {
828 .open = pcm_digital_in_open,
829 .close = pcm_close,
830 .ioctl = snd_pcm_lib_ioctl,
831 .hw_params = pcm_digital_in_hw_params,
832 .hw_free = pcm_hw_free,
833 .prepare = pcm_prepare,
834 .trigger = pcm_trigger,
835 .pointer = pcm_pointer,
836 .page = snd_pcm_sgbuf_ops_page,
838 #endif /* ECHOCARD_HAS_DIGITAL_IO */
842 /* Preallocate memory only for the first substream because it's the most
843 * used one
845 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
847 struct snd_pcm_substream *ss;
848 int stream, err;
850 for (stream = 0; stream < 2; stream++)
851 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) {
852 err = snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
853 dev,
854 ss->number ? 0 : 128<<10,
855 256<<10);
856 if (err < 0)
857 return err;
859 return 0;
864 /*<--snd_echo_probe() */
865 static int __devinit snd_echo_new_pcm(struct echoaudio *chip)
867 struct snd_pcm *pcm;
868 int err;
870 #ifdef ECHOCARD_HAS_VMIXER
871 /* This card has a Vmixer, that is there is no direct mapping from PCM
872 streams to physical outputs. The user can mix the streams as he wishes
873 via control interface and it's possible to send any stream to any
874 output, thus it makes no sense to keep analog and digital outputs
875 separated */
877 /* PCM#0 Virtual outputs and analog inputs */
878 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
879 num_analog_busses_in(chip), &pcm)) < 0)
880 return err;
881 pcm->private_data = chip;
882 chip->analog_pcm = pcm;
883 strcpy(pcm->name, chip->card->shortname);
884 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
885 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
886 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
887 return err;
888 DE_INIT(("Analog PCM ok\n"));
890 #ifdef ECHOCARD_HAS_DIGITAL_IO
891 /* PCM#1 Digital inputs, no outputs */
892 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
893 num_digital_busses_in(chip), &pcm)) < 0)
894 return err;
895 pcm->private_data = chip;
896 chip->digital_pcm = pcm;
897 strcpy(pcm->name, chip->card->shortname);
898 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
899 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
900 return err;
901 DE_INIT(("Digital PCM ok\n"));
902 #endif /* ECHOCARD_HAS_DIGITAL_IO */
904 #else /* ECHOCARD_HAS_VMIXER */
906 /* The card can manage substreams formed by analog and digital channels
907 at the same time, but I prefer to keep analog and digital channels
908 separated, because that mixed thing is confusing and useless. So we
909 register two PCM devices: */
911 /* PCM#0 Analog i/o */
912 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
913 num_analog_busses_out(chip),
914 num_analog_busses_in(chip), &pcm)) < 0)
915 return err;
916 pcm->private_data = chip;
917 chip->analog_pcm = pcm;
918 strcpy(pcm->name, chip->card->shortname);
919 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
920 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
921 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
922 return err;
923 DE_INIT(("Analog PCM ok\n"));
925 #ifdef ECHOCARD_HAS_DIGITAL_IO
926 /* PCM#1 Digital i/o */
927 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
928 num_digital_busses_out(chip),
929 num_digital_busses_in(chip), &pcm)) < 0)
930 return err;
931 pcm->private_data = chip;
932 chip->digital_pcm = pcm;
933 strcpy(pcm->name, chip->card->shortname);
934 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
936 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
937 return err;
938 DE_INIT(("Digital PCM ok\n"));
939 #endif /* ECHOCARD_HAS_DIGITAL_IO */
941 #endif /* ECHOCARD_HAS_VMIXER */
943 return 0;
949 /******************************************************************************
950 Control interface
951 ******************************************************************************/
953 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
955 /******************* PCM output volume *******************/
956 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
957 struct snd_ctl_elem_info *uinfo)
959 struct echoaudio *chip;
961 chip = snd_kcontrol_chip(kcontrol);
962 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
963 uinfo->count = num_busses_out(chip);
964 uinfo->value.integer.min = ECHOGAIN_MINOUT;
965 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
966 return 0;
969 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_value *ucontrol)
972 struct echoaudio *chip;
973 int c;
975 chip = snd_kcontrol_chip(kcontrol);
976 for (c = 0; c < num_busses_out(chip); c++)
977 ucontrol->value.integer.value[c] = chip->output_gain[c];
978 return 0;
981 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
984 struct echoaudio *chip;
985 int c, changed, gain;
987 changed = 0;
988 chip = snd_kcontrol_chip(kcontrol);
989 spin_lock_irq(&chip->lock);
990 for (c = 0; c < num_busses_out(chip); c++) {
991 gain = ucontrol->value.integer.value[c];
992 /* Ignore out of range values */
993 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
994 continue;
995 if (chip->output_gain[c] != gain) {
996 set_output_gain(chip, c, gain);
997 changed = 1;
1000 if (changed)
1001 update_output_line_level(chip);
1002 spin_unlock_irq(&chip->lock);
1003 return changed;
1006 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1007 /* On the Mia this one controls the line-out volume */
1008 static struct snd_kcontrol_new snd_echo_line_output_gain __devinitdata = {
1009 .name = "Line Playback Volume",
1010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1011 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1012 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1013 .info = snd_echo_output_gain_info,
1014 .get = snd_echo_output_gain_get,
1015 .put = snd_echo_output_gain_put,
1016 .tlv = {.p = db_scale_output_gain},
1018 #else
1019 static struct snd_kcontrol_new snd_echo_pcm_output_gain __devinitdata = {
1020 .name = "PCM Playback Volume",
1021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1022 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1023 .info = snd_echo_output_gain_info,
1024 .get = snd_echo_output_gain_get,
1025 .put = snd_echo_output_gain_put,
1026 .tlv = {.p = db_scale_output_gain},
1028 #endif
1030 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1034 #ifdef ECHOCARD_HAS_INPUT_GAIN
1036 /******************* Analog input volume *******************/
1037 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1038 struct snd_ctl_elem_info *uinfo)
1040 struct echoaudio *chip;
1042 chip = snd_kcontrol_chip(kcontrol);
1043 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1044 uinfo->count = num_analog_busses_in(chip);
1045 uinfo->value.integer.min = ECHOGAIN_MININP;
1046 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1047 return 0;
1050 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_value *ucontrol)
1053 struct echoaudio *chip;
1054 int c;
1056 chip = snd_kcontrol_chip(kcontrol);
1057 for (c = 0; c < num_analog_busses_in(chip); c++)
1058 ucontrol->value.integer.value[c] = chip->input_gain[c];
1059 return 0;
1062 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1063 struct snd_ctl_elem_value *ucontrol)
1065 struct echoaudio *chip;
1066 int c, gain, changed;
1068 changed = 0;
1069 chip = snd_kcontrol_chip(kcontrol);
1070 spin_lock_irq(&chip->lock);
1071 for (c = 0; c < num_analog_busses_in(chip); c++) {
1072 gain = ucontrol->value.integer.value[c];
1073 /* Ignore out of range values */
1074 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1075 continue;
1076 if (chip->input_gain[c] != gain) {
1077 set_input_gain(chip, c, gain);
1078 changed = 1;
1081 if (changed)
1082 update_input_line_level(chip);
1083 spin_unlock_irq(&chip->lock);
1084 return changed;
1087 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1089 static struct snd_kcontrol_new snd_echo_line_input_gain __devinitdata = {
1090 .name = "Line Capture Volume",
1091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1092 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1093 .info = snd_echo_input_gain_info,
1094 .get = snd_echo_input_gain_get,
1095 .put = snd_echo_input_gain_put,
1096 .tlv = {.p = db_scale_input_gain},
1099 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1103 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1105 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1106 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1107 struct snd_ctl_elem_info *uinfo)
1109 struct echoaudio *chip;
1111 chip = snd_kcontrol_chip(kcontrol);
1112 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1113 uinfo->count = num_analog_busses_out(chip);
1114 uinfo->value.integer.min = 0;
1115 uinfo->value.integer.max = 1;
1116 return 0;
1119 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1120 struct snd_ctl_elem_value *ucontrol)
1122 struct echoaudio *chip;
1123 int c;
1125 chip = snd_kcontrol_chip(kcontrol);
1126 for (c = 0; c < num_analog_busses_out(chip); c++)
1127 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1128 return 0;
1131 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1132 struct snd_ctl_elem_value *ucontrol)
1134 struct echoaudio *chip;
1135 int c, changed;
1137 changed = 0;
1138 chip = snd_kcontrol_chip(kcontrol);
1139 spin_lock_irq(&chip->lock);
1140 for (c = 0; c < num_analog_busses_out(chip); c++) {
1141 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1142 set_nominal_level(chip, c,
1143 ucontrol->value.integer.value[c]);
1144 changed = 1;
1147 if (changed)
1148 update_output_line_level(chip);
1149 spin_unlock_irq(&chip->lock);
1150 return changed;
1153 static struct snd_kcontrol_new snd_echo_output_nominal_level __devinitdata = {
1154 .name = "Line Playback Switch (-10dBV)",
1155 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1156 .info = snd_echo_output_nominal_info,
1157 .get = snd_echo_output_nominal_get,
1158 .put = snd_echo_output_nominal_put,
1161 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1165 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1167 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1168 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1169 struct snd_ctl_elem_info *uinfo)
1171 struct echoaudio *chip;
1173 chip = snd_kcontrol_chip(kcontrol);
1174 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1175 uinfo->count = num_analog_busses_in(chip);
1176 uinfo->value.integer.min = 0;
1177 uinfo->value.integer.max = 1;
1178 return 0;
1181 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1182 struct snd_ctl_elem_value *ucontrol)
1184 struct echoaudio *chip;
1185 int c;
1187 chip = snd_kcontrol_chip(kcontrol);
1188 for (c = 0; c < num_analog_busses_in(chip); c++)
1189 ucontrol->value.integer.value[c] =
1190 chip->nominal_level[bx_analog_in(chip) + c];
1191 return 0;
1194 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1195 struct snd_ctl_elem_value *ucontrol)
1197 struct echoaudio *chip;
1198 int c, changed;
1200 changed = 0;
1201 chip = snd_kcontrol_chip(kcontrol);
1202 spin_lock_irq(&chip->lock);
1203 for (c = 0; c < num_analog_busses_in(chip); c++) {
1204 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1205 ucontrol->value.integer.value[c]) {
1206 set_nominal_level(chip, bx_analog_in(chip) + c,
1207 ucontrol->value.integer.value[c]);
1208 changed = 1;
1211 if (changed)
1212 update_output_line_level(chip); /* "Output" is not a mistake
1213 * here.
1215 spin_unlock_irq(&chip->lock);
1216 return changed;
1219 static struct snd_kcontrol_new snd_echo_intput_nominal_level __devinitdata = {
1220 .name = "Line Capture Switch (-10dBV)",
1221 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1222 .info = snd_echo_input_nominal_info,
1223 .get = snd_echo_input_nominal_get,
1224 .put = snd_echo_input_nominal_put,
1227 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1231 #ifdef ECHOCARD_HAS_MONITOR
1233 /******************* Monitor mixer *******************/
1234 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1235 struct snd_ctl_elem_info *uinfo)
1237 struct echoaudio *chip;
1239 chip = snd_kcontrol_chip(kcontrol);
1240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1241 uinfo->count = 1;
1242 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1243 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1244 uinfo->dimen.d[0] = num_busses_out(chip);
1245 uinfo->dimen.d[1] = num_busses_in(chip);
1246 return 0;
1249 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1250 struct snd_ctl_elem_value *ucontrol)
1252 struct echoaudio *chip;
1254 chip = snd_kcontrol_chip(kcontrol);
1255 ucontrol->value.integer.value[0] =
1256 chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1257 [ucontrol->id.index % num_busses_in(chip)];
1258 return 0;
1261 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1262 struct snd_ctl_elem_value *ucontrol)
1264 struct echoaudio *chip;
1265 int changed, gain;
1266 short out, in;
1268 changed = 0;
1269 chip = snd_kcontrol_chip(kcontrol);
1270 out = ucontrol->id.index / num_busses_in(chip);
1271 in = ucontrol->id.index % num_busses_in(chip);
1272 gain = ucontrol->value.integer.value[0];
1273 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1274 return -EINVAL;
1275 if (chip->monitor_gain[out][in] != gain) {
1276 spin_lock_irq(&chip->lock);
1277 set_monitor_gain(chip, out, in, gain);
1278 update_output_line_level(chip);
1279 spin_unlock_irq(&chip->lock);
1280 changed = 1;
1282 return changed;
1285 static struct snd_kcontrol_new snd_echo_monitor_mixer __devinitdata = {
1286 .name = "Monitor Mixer Volume",
1287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1288 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1289 .info = snd_echo_mixer_info,
1290 .get = snd_echo_mixer_get,
1291 .put = snd_echo_mixer_put,
1292 .tlv = {.p = db_scale_output_gain},
1295 #endif /* ECHOCARD_HAS_MONITOR */
1299 #ifdef ECHOCARD_HAS_VMIXER
1301 /******************* Vmixer *******************/
1302 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1303 struct snd_ctl_elem_info *uinfo)
1305 struct echoaudio *chip;
1307 chip = snd_kcontrol_chip(kcontrol);
1308 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1309 uinfo->count = 1;
1310 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1311 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1312 uinfo->dimen.d[0] = num_busses_out(chip);
1313 uinfo->dimen.d[1] = num_pipes_out(chip);
1314 return 0;
1317 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_value *ucontrol)
1320 struct echoaudio *chip;
1322 chip = snd_kcontrol_chip(kcontrol);
1323 ucontrol->value.integer.value[0] =
1324 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1325 [ucontrol->id.index % num_pipes_out(chip)];
1326 return 0;
1329 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1330 struct snd_ctl_elem_value *ucontrol)
1332 struct echoaudio *chip;
1333 int gain, changed;
1334 short vch, out;
1336 changed = 0;
1337 chip = snd_kcontrol_chip(kcontrol);
1338 out = ucontrol->id.index / num_pipes_out(chip);
1339 vch = ucontrol->id.index % num_pipes_out(chip);
1340 gain = ucontrol->value.integer.value[0];
1341 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1342 return -EINVAL;
1343 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1344 spin_lock_irq(&chip->lock);
1345 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1346 update_vmixer_level(chip);
1347 spin_unlock_irq(&chip->lock);
1348 changed = 1;
1350 return changed;
1353 static struct snd_kcontrol_new snd_echo_vmixer __devinitdata = {
1354 .name = "VMixer Volume",
1355 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1356 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1357 .info = snd_echo_vmixer_info,
1358 .get = snd_echo_vmixer_get,
1359 .put = snd_echo_vmixer_put,
1360 .tlv = {.p = db_scale_output_gain},
1363 #endif /* ECHOCARD_HAS_VMIXER */
1367 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1369 /******************* Digital mode switch *******************/
1370 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1371 struct snd_ctl_elem_info *uinfo)
1373 static char *names[4] = {
1374 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1375 "S/PDIF Cdrom"
1377 struct echoaudio *chip;
1379 chip = snd_kcontrol_chip(kcontrol);
1380 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1381 uinfo->value.enumerated.items = chip->num_digital_modes;
1382 uinfo->count = 1;
1383 if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1384 uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1385 strcpy(uinfo->value.enumerated.name, names[
1386 chip->digital_mode_list[uinfo->value.enumerated.item]]);
1387 return 0;
1390 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1391 struct snd_ctl_elem_value *ucontrol)
1393 struct echoaudio *chip;
1394 int i, mode;
1396 chip = snd_kcontrol_chip(kcontrol);
1397 mode = chip->digital_mode;
1398 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1399 if (mode == chip->digital_mode_list[i]) {
1400 ucontrol->value.enumerated.item[0] = i;
1401 break;
1403 return 0;
1406 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_value *ucontrol)
1409 struct echoaudio *chip;
1410 int changed;
1411 unsigned short emode, dmode;
1413 changed = 0;
1414 chip = snd_kcontrol_chip(kcontrol);
1416 emode = ucontrol->value.enumerated.item[0];
1417 if (emode >= chip->num_digital_modes)
1418 return -EINVAL;
1419 dmode = chip->digital_mode_list[emode];
1421 if (dmode != chip->digital_mode) {
1422 /* mode_mutex is required to make this operation atomic wrt
1423 pcm_digital_*_open() and set_input_clock() functions. */
1424 mutex_lock(&chip->mode_mutex);
1426 /* Do not allow the user to change the digital mode when a pcm
1427 device is open because it also changes the number of channels
1428 and the allowed sample rates */
1429 if (atomic_read(&chip->opencount)) {
1430 changed = -EAGAIN;
1431 } else {
1432 changed = set_digital_mode(chip, dmode);
1433 /* If we had to change the clock source, report it */
1434 if (changed > 0 && chip->clock_src_ctl) {
1435 snd_ctl_notify(chip->card,
1436 SNDRV_CTL_EVENT_MASK_VALUE,
1437 &chip->clock_src_ctl->id);
1438 DE_ACT(("SDM() =%d\n", changed));
1440 if (changed >= 0)
1441 changed = 1; /* No errors */
1443 mutex_unlock(&chip->mode_mutex);
1445 return changed;
1448 static struct snd_kcontrol_new snd_echo_digital_mode_switch __devinitdata = {
1449 .name = "Digital mode Switch",
1450 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1451 .info = snd_echo_digital_mode_info,
1452 .get = snd_echo_digital_mode_get,
1453 .put = snd_echo_digital_mode_put,
1456 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1460 #ifdef ECHOCARD_HAS_DIGITAL_IO
1462 /******************* S/PDIF mode switch *******************/
1463 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_info *uinfo)
1466 static char *names[2] = {"Consumer", "Professional"};
1468 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1469 uinfo->value.enumerated.items = 2;
1470 uinfo->count = 1;
1471 if (uinfo->value.enumerated.item)
1472 uinfo->value.enumerated.item = 1;
1473 strcpy(uinfo->value.enumerated.name,
1474 names[uinfo->value.enumerated.item]);
1475 return 0;
1478 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
1481 struct echoaudio *chip;
1483 chip = snd_kcontrol_chip(kcontrol);
1484 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1485 return 0;
1488 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_value *ucontrol)
1491 struct echoaudio *chip;
1492 int mode;
1494 chip = snd_kcontrol_chip(kcontrol);
1495 mode = !!ucontrol->value.enumerated.item[0];
1496 if (mode != chip->professional_spdif) {
1497 spin_lock_irq(&chip->lock);
1498 set_professional_spdif(chip, mode);
1499 spin_unlock_irq(&chip->lock);
1500 return 1;
1502 return 0;
1505 static struct snd_kcontrol_new snd_echo_spdif_mode_switch __devinitdata = {
1506 .name = "S/PDIF mode Switch",
1507 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1508 .info = snd_echo_spdif_mode_info,
1509 .get = snd_echo_spdif_mode_get,
1510 .put = snd_echo_spdif_mode_put,
1513 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1517 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1519 /******************* Select input clock source *******************/
1520 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_info *uinfo)
1523 static char *names[8] = {
1524 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1525 "ESync96", "MTC"
1527 struct echoaudio *chip;
1529 chip = snd_kcontrol_chip(kcontrol);
1530 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1531 uinfo->value.enumerated.items = chip->num_clock_sources;
1532 uinfo->count = 1;
1533 if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1534 uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1535 strcpy(uinfo->value.enumerated.name, names[
1536 chip->clock_source_list[uinfo->value.enumerated.item]]);
1537 return 0;
1540 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1543 struct echoaudio *chip;
1544 int i, clock;
1546 chip = snd_kcontrol_chip(kcontrol);
1547 clock = chip->input_clock;
1549 for (i = 0; i < chip->num_clock_sources; i++)
1550 if (clock == chip->clock_source_list[i])
1551 ucontrol->value.enumerated.item[0] = i;
1553 return 0;
1556 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1559 struct echoaudio *chip;
1560 int changed;
1561 unsigned int eclock, dclock;
1563 changed = 0;
1564 chip = snd_kcontrol_chip(kcontrol);
1565 eclock = ucontrol->value.enumerated.item[0];
1566 if (eclock >= chip->input_clock_types)
1567 return -EINVAL;
1568 dclock = chip->clock_source_list[eclock];
1569 if (chip->input_clock != dclock) {
1570 mutex_lock(&chip->mode_mutex);
1571 spin_lock_irq(&chip->lock);
1572 if ((changed = set_input_clock(chip, dclock)) == 0)
1573 changed = 1; /* no errors */
1574 spin_unlock_irq(&chip->lock);
1575 mutex_unlock(&chip->mode_mutex);
1578 if (changed < 0)
1579 DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1581 return changed;
1584 static struct snd_kcontrol_new snd_echo_clock_source_switch __devinitdata = {
1585 .name = "Sample Clock Source",
1586 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1587 .info = snd_echo_clock_source_info,
1588 .get = snd_echo_clock_source_get,
1589 .put = snd_echo_clock_source_put,
1592 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1596 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1598 /******************* Phantom power switch *******************/
1599 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1601 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1602 struct snd_ctl_elem_value *ucontrol)
1604 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1606 ucontrol->value.integer.value[0] = chip->phantom_power;
1607 return 0;
1610 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1611 struct snd_ctl_elem_value *ucontrol)
1613 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1614 int power, changed = 0;
1616 power = !!ucontrol->value.integer.value[0];
1617 if (chip->phantom_power != power) {
1618 spin_lock_irq(&chip->lock);
1619 changed = set_phantom_power(chip, power);
1620 spin_unlock_irq(&chip->lock);
1621 if (changed == 0)
1622 changed = 1; /* no errors */
1624 return changed;
1627 static struct snd_kcontrol_new snd_echo_phantom_power_switch __devinitdata = {
1628 .name = "Phantom power Switch",
1629 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1630 .info = snd_echo_phantom_power_info,
1631 .get = snd_echo_phantom_power_get,
1632 .put = snd_echo_phantom_power_put,
1635 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1639 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1641 /******************* Digital input automute switch *******************/
1642 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1644 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
1647 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1649 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1650 return 0;
1653 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol)
1656 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1657 int automute, changed = 0;
1659 automute = !!ucontrol->value.integer.value[0];
1660 if (chip->digital_in_automute != automute) {
1661 spin_lock_irq(&chip->lock);
1662 changed = set_input_auto_mute(chip, automute);
1663 spin_unlock_irq(&chip->lock);
1664 if (changed == 0)
1665 changed = 1; /* no errors */
1667 return changed;
1670 static struct snd_kcontrol_new snd_echo_automute_switch __devinitdata = {
1671 .name = "Digital Capture Switch (automute)",
1672 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1673 .info = snd_echo_automute_info,
1674 .get = snd_echo_automute_get,
1675 .put = snd_echo_automute_put,
1678 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1682 /******************* VU-meters switch *******************/
1683 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1685 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1686 struct snd_ctl_elem_value *ucontrol)
1688 struct echoaudio *chip;
1690 chip = snd_kcontrol_chip(kcontrol);
1691 spin_lock_irq(&chip->lock);
1692 set_meters_on(chip, ucontrol->value.integer.value[0]);
1693 spin_unlock_irq(&chip->lock);
1694 return 1;
1697 static struct snd_kcontrol_new snd_echo_vumeters_switch __devinitdata = {
1698 .name = "VU-meters Switch",
1699 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1700 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1701 .info = snd_echo_vumeters_switch_info,
1702 .put = snd_echo_vumeters_switch_put,
1707 /***** Read VU-meters (input, output, analog and digital together) *****/
1708 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_info *uinfo)
1711 struct echoaudio *chip;
1713 chip = snd_kcontrol_chip(kcontrol);
1714 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1715 uinfo->count = 96;
1716 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1717 uinfo->value.integer.max = 0;
1718 #ifdef ECHOCARD_HAS_VMIXER
1719 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1720 #else
1721 uinfo->dimen.d[0] = 2; /* Out, In */
1722 #endif
1723 uinfo->dimen.d[1] = 16; /* 16 channels */
1724 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1725 return 0;
1728 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_value *ucontrol)
1731 struct echoaudio *chip;
1733 chip = snd_kcontrol_chip(kcontrol);
1734 get_audio_meters(chip, ucontrol->value.integer.value);
1735 return 0;
1738 static struct snd_kcontrol_new snd_echo_vumeters __devinitdata = {
1739 .name = "VU-meters",
1740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1741 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1742 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1743 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1744 .info = snd_echo_vumeters_info,
1745 .get = snd_echo_vumeters_get,
1746 .tlv = {.p = db_scale_output_gain},
1751 /*** Channels info - it exports informations about the number of channels ***/
1752 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1753 struct snd_ctl_elem_info *uinfo)
1755 struct echoaudio *chip;
1757 chip = snd_kcontrol_chip(kcontrol);
1758 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1759 uinfo->count = 6;
1760 uinfo->value.integer.min = 0;
1761 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1762 return 0;
1765 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1768 struct echoaudio *chip;
1769 int detected, clocks, bit, src;
1771 chip = snd_kcontrol_chip(kcontrol);
1772 ucontrol->value.integer.value[0] = num_busses_in(chip);
1773 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1774 ucontrol->value.integer.value[2] = num_busses_out(chip);
1775 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1776 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1778 /* Compute the bitmask of the currently valid input clocks */
1779 detected = detect_input_clocks(chip);
1780 clocks = 0;
1781 src = chip->num_clock_sources - 1;
1782 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1783 if (detected & (1 << bit))
1784 for (; src >= 0; src--)
1785 if (bit == chip->clock_source_list[src]) {
1786 clocks |= 1 << src;
1787 break;
1789 ucontrol->value.integer.value[5] = clocks;
1791 return 0;
1794 static struct snd_kcontrol_new snd_echo_channels_info __devinitdata = {
1795 .name = "Channels info",
1796 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1797 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1798 .info = snd_echo_channels_info_info,
1799 .get = snd_echo_channels_info_get,
1805 /******************************************************************************
1806 IRQ Handler
1807 ******************************************************************************/
1809 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1811 struct echoaudio *chip = dev_id;
1812 struct snd_pcm_substream *substream;
1813 int period, ss, st;
1815 spin_lock(&chip->lock);
1816 st = service_irq(chip);
1817 if (st < 0) {
1818 spin_unlock(&chip->lock);
1819 return IRQ_NONE;
1821 /* The hardware doesn't tell us which substream caused the irq,
1822 thus we have to check all running substreams. */
1823 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1824 if ((substream = chip->substream[ss])) {
1825 period = pcm_pointer(substream) /
1826 substream->runtime->period_size;
1827 if (period != chip->last_period[ss]) {
1828 chip->last_period[ss] = period;
1829 spin_unlock(&chip->lock);
1830 snd_pcm_period_elapsed(substream);
1831 spin_lock(&chip->lock);
1835 spin_unlock(&chip->lock);
1837 #ifdef ECHOCARD_HAS_MIDI
1838 if (st > 0 && chip->midi_in) {
1839 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1840 DE_MID(("rawmidi_iread=%d\n", st));
1842 #endif
1843 return IRQ_HANDLED;
1849 /******************************************************************************
1850 Module construction / destruction
1851 ******************************************************************************/
1853 static int snd_echo_free(struct echoaudio *chip)
1855 DE_INIT(("Stop DSP...\n"));
1856 if (chip->comm_page)
1857 rest_in_peace(chip);
1858 DE_INIT(("Stopped.\n"));
1860 if (chip->irq >= 0)
1861 free_irq(chip->irq, chip);
1863 if (chip->comm_page)
1864 snd_dma_free_pages(&chip->commpage_dma_buf);
1866 if (chip->dsp_registers)
1867 iounmap(chip->dsp_registers);
1869 if (chip->iores)
1870 release_and_free_resource(chip->iores);
1872 DE_INIT(("MMIO freed.\n"));
1874 pci_disable_device(chip->pci);
1876 /* release chip data */
1877 kfree(chip);
1878 DE_INIT(("Chip freed.\n"));
1879 return 0;
1884 static int snd_echo_dev_free(struct snd_device *device)
1886 struct echoaudio *chip = device->device_data;
1888 DE_INIT(("snd_echo_dev_free()...\n"));
1889 return snd_echo_free(chip);
1894 /* <--snd_echo_probe() */
1895 static __devinit int snd_echo_create(struct snd_card *card,
1896 struct pci_dev *pci,
1897 struct echoaudio **rchip)
1899 struct echoaudio *chip;
1900 int err;
1901 size_t sz;
1902 static struct snd_device_ops ops = {
1903 .dev_free = snd_echo_dev_free,
1906 *rchip = NULL;
1908 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1910 if ((err = pci_enable_device(pci)) < 0)
1911 return err;
1912 pci_set_master(pci);
1914 /* allocate a chip-specific data */
1915 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1916 if (!chip) {
1917 pci_disable_device(pci);
1918 return -ENOMEM;
1920 DE_INIT(("chip=%p\n", chip));
1922 spin_lock_init(&chip->lock);
1923 chip->card = card;
1924 chip->pci = pci;
1925 chip->irq = -1;
1927 /* PCI resource allocation */
1928 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929 sz = pci_resource_len(pci, 0);
1930 if (sz > PAGE_SIZE)
1931 sz = PAGE_SIZE; /* We map only the required part */
1933 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1934 ECHOCARD_NAME)) == NULL) {
1935 snd_echo_free(chip);
1936 snd_printk(KERN_ERR "cannot get memory region\n");
1937 return -EBUSY;
1939 chip->dsp_registers = (volatile u32 __iomem *)
1940 ioremap_nocache(chip->dsp_registers_phys, sz);
1942 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1943 ECHOCARD_NAME, chip)) {
1944 snd_echo_free(chip);
1945 snd_printk(KERN_ERR "cannot grab irq\n");
1946 return -EBUSY;
1948 chip->irq = pci->irq;
1949 DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
1950 chip->pci, chip->irq, chip->pci->subsystem_device));
1952 /* Create the DSP comm page - this is the area of memory used for most
1953 of the communication with the DSP, which accesses it via bus mastering */
1954 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1955 sizeof(struct comm_page),
1956 &chip->commpage_dma_buf) < 0) {
1957 snd_echo_free(chip);
1958 snd_printk(KERN_ERR "cannot allocate the comm page\n");
1959 return -ENOMEM;
1961 chip->comm_page_phys = chip->commpage_dma_buf.addr;
1962 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1964 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1965 if (err) {
1966 DE_INIT(("init_hw err=%d\n", err));
1967 snd_echo_free(chip);
1968 return err;
1970 DE_INIT(("Card init OK\n"));
1972 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1973 snd_echo_free(chip);
1974 return err;
1976 atomic_set(&chip->opencount, 0);
1977 mutex_init(&chip->mode_mutex);
1978 chip->can_set_rate = 1;
1979 *rchip = chip;
1980 /* Init done ! */
1981 return 0;
1986 /* constructor */
1987 static int __devinit snd_echo_probe(struct pci_dev *pci,
1988 const struct pci_device_id *pci_id)
1990 static int dev;
1991 struct snd_card *card;
1992 struct echoaudio *chip;
1993 char *dsp;
1994 int i, err;
1996 if (dev >= SNDRV_CARDS)
1997 return -ENODEV;
1998 if (!enable[dev]) {
1999 dev++;
2000 return -ENOENT;
2003 DE_INIT(("Echoaudio driver starting...\n"));
2004 i = 0;
2005 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2006 if (err < 0)
2007 return err;
2009 snd_card_set_dev(card, &pci->dev);
2011 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2012 snd_card_free(card);
2013 return err;
2016 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2017 strcpy(card->shortname, chip->card_name);
2019 dsp = "56301";
2020 if (pci_id->device == 0x3410)
2021 dsp = "56361";
2023 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2024 card->shortname, pci_id->subdevice & 0x000f, dsp,
2025 chip->dsp_registers_phys, chip->irq);
2027 if ((err = snd_echo_new_pcm(chip)) < 0) {
2028 snd_printk(KERN_ERR "new pcm error %d\n", err);
2029 snd_card_free(card);
2030 return err;
2033 #ifdef ECHOCARD_HAS_MIDI
2034 if (chip->has_midi) { /* Some Mia's do not have midi */
2035 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2036 snd_printk(KERN_ERR "new midi error %d\n", err);
2037 snd_card_free(card);
2038 return err;
2041 #endif
2043 #ifdef ECHOCARD_HAS_VMIXER
2044 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2045 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2046 goto ctl_error;
2047 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2048 err = snd_ctl_add(chip->card,
2049 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2050 if (err < 0)
2051 goto ctl_error;
2052 #endif
2053 #else /* ECHOCARD_HAS_VMIXER */
2054 err = snd_ctl_add(chip->card,
2055 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2056 if (err < 0)
2057 goto ctl_error;
2058 #endif /* ECHOCARD_HAS_VMIXER */
2060 #ifdef ECHOCARD_HAS_INPUT_GAIN
2061 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2062 goto ctl_error;
2063 #endif
2065 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2066 if (!chip->hasnt_input_nominal_level)
2067 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2068 goto ctl_error;
2069 #endif
2071 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2072 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2073 goto ctl_error;
2074 #endif
2076 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2077 goto ctl_error;
2079 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2080 goto ctl_error;
2082 #ifdef ECHOCARD_HAS_MONITOR
2083 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2084 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2085 goto ctl_error;
2086 #endif
2088 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2089 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2090 goto ctl_error;
2091 #endif
2093 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2094 goto ctl_error;
2096 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2097 /* Creates a list of available digital modes */
2098 chip->num_digital_modes = 0;
2099 for (i = 0; i < 6; i++)
2100 if (chip->digital_modes & (1 << i))
2101 chip->digital_mode_list[chip->num_digital_modes++] = i;
2103 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2104 goto ctl_error;
2105 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2107 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2108 /* Creates a list of available clock sources */
2109 chip->num_clock_sources = 0;
2110 for (i = 0; i < 10; i++)
2111 if (chip->input_clock_types & (1 << i))
2112 chip->clock_source_list[chip->num_clock_sources++] = i;
2114 if (chip->num_clock_sources > 1) {
2115 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2116 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2117 goto ctl_error;
2119 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2121 #ifdef ECHOCARD_HAS_DIGITAL_IO
2122 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2123 goto ctl_error;
2124 #endif
2126 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2127 if (chip->has_phantom_power)
2128 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2129 goto ctl_error;
2130 #endif
2132 if ((err = snd_card_register(card)) < 0) {
2133 snd_card_free(card);
2134 goto ctl_error;
2136 snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2138 pci_set_drvdata(pci, chip);
2139 dev++;
2140 return 0;
2142 ctl_error:
2143 snd_printk(KERN_ERR "new control error %d\n", err);
2144 snd_card_free(card);
2145 return err;
2150 static void __devexit snd_echo_remove(struct pci_dev *pci)
2152 struct echoaudio *chip;
2154 chip = pci_get_drvdata(pci);
2155 if (chip)
2156 snd_card_free(chip->card);
2157 pci_set_drvdata(pci, NULL);
2162 /******************************************************************************
2163 Everything starts and ends here
2164 ******************************************************************************/
2166 /* pci_driver definition */
2167 static struct pci_driver driver = {
2168 .name = "Echoaudio " ECHOCARD_NAME,
2169 .id_table = snd_echo_ids,
2170 .probe = snd_echo_probe,
2171 .remove = __devexit_p(snd_echo_remove),
2176 /* initialization of the module */
2177 static int __init alsa_card_echo_init(void)
2179 return pci_register_driver(&driver);
2184 /* clean up the module */
2185 static void __exit alsa_card_echo_exit(void)
2187 pci_unregister_driver(&driver);
2191 module_init(alsa_card_echo_init)
2192 module_exit(alsa_card_echo_exit)