making the kernel build with up to date compilers again, see https://bbs.archlinux...
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / sound / pci / echoaudio / echoaudio.c
blob8dbc5c4ba421a03dd128050dbd271616efdee3ae
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 /******************* PCM output volume *******************/
954 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_info *uinfo)
957 struct echoaudio *chip;
959 chip = snd_kcontrol_chip(kcontrol);
960 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
961 uinfo->count = num_busses_out(chip);
962 uinfo->value.integer.min = ECHOGAIN_MINOUT;
963 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
964 return 0;
967 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
968 struct snd_ctl_elem_value *ucontrol)
970 struct echoaudio *chip;
971 int c;
973 chip = snd_kcontrol_chip(kcontrol);
974 for (c = 0; c < num_busses_out(chip); c++)
975 ucontrol->value.integer.value[c] = chip->output_gain[c];
976 return 0;
979 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
980 struct snd_ctl_elem_value *ucontrol)
982 struct echoaudio *chip;
983 int c, changed, gain;
985 changed = 0;
986 chip = snd_kcontrol_chip(kcontrol);
987 spin_lock_irq(&chip->lock);
988 for (c = 0; c < num_busses_out(chip); c++) {
989 gain = ucontrol->value.integer.value[c];
990 /* Ignore out of range values */
991 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
992 continue;
993 if (chip->output_gain[c] != gain) {
994 set_output_gain(chip, c, gain);
995 changed = 1;
998 if (changed)
999 update_output_line_level(chip);
1000 spin_unlock_irq(&chip->lock);
1001 return changed;
1004 #ifdef ECHOCARD_HAS_VMIXER
1005 /* On Vmixer cards this one controls the line-out volume */
1006 static struct snd_kcontrol_new snd_echo_line_output_gain __devinitdata = {
1007 .name = "Line Playback Volume",
1008 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1009 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1010 .info = snd_echo_output_gain_info,
1011 .get = snd_echo_output_gain_get,
1012 .put = snd_echo_output_gain_put,
1013 .tlv = {.p = db_scale_output_gain},
1015 #else
1016 static struct snd_kcontrol_new snd_echo_pcm_output_gain __devinitdata = {
1017 .name = "PCM Playback Volume",
1018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1020 .info = snd_echo_output_gain_info,
1021 .get = snd_echo_output_gain_get,
1022 .put = snd_echo_output_gain_put,
1023 .tlv = {.p = db_scale_output_gain},
1025 #endif
1029 #ifdef ECHOCARD_HAS_INPUT_GAIN
1031 /******************* Analog input volume *******************/
1032 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1033 struct snd_ctl_elem_info *uinfo)
1035 struct echoaudio *chip;
1037 chip = snd_kcontrol_chip(kcontrol);
1038 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1039 uinfo->count = num_analog_busses_in(chip);
1040 uinfo->value.integer.min = ECHOGAIN_MININP;
1041 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1042 return 0;
1045 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1046 struct snd_ctl_elem_value *ucontrol)
1048 struct echoaudio *chip;
1049 int c;
1051 chip = snd_kcontrol_chip(kcontrol);
1052 for (c = 0; c < num_analog_busses_in(chip); c++)
1053 ucontrol->value.integer.value[c] = chip->input_gain[c];
1054 return 0;
1057 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
1060 struct echoaudio *chip;
1061 int c, gain, changed;
1063 changed = 0;
1064 chip = snd_kcontrol_chip(kcontrol);
1065 spin_lock_irq(&chip->lock);
1066 for (c = 0; c < num_analog_busses_in(chip); c++) {
1067 gain = ucontrol->value.integer.value[c];
1068 /* Ignore out of range values */
1069 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1070 continue;
1071 if (chip->input_gain[c] != gain) {
1072 set_input_gain(chip, c, gain);
1073 changed = 1;
1076 if (changed)
1077 update_input_line_level(chip);
1078 spin_unlock_irq(&chip->lock);
1079 return changed;
1082 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1084 static struct snd_kcontrol_new snd_echo_line_input_gain __devinitdata = {
1085 .name = "Line Capture Volume",
1086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1087 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1088 .info = snd_echo_input_gain_info,
1089 .get = snd_echo_input_gain_get,
1090 .put = snd_echo_input_gain_put,
1091 .tlv = {.p = db_scale_input_gain},
1094 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1098 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1100 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1101 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_info *uinfo)
1104 struct echoaudio *chip;
1106 chip = snd_kcontrol_chip(kcontrol);
1107 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1108 uinfo->count = num_analog_busses_out(chip);
1109 uinfo->value.integer.min = 0;
1110 uinfo->value.integer.max = 1;
1111 return 0;
1114 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1115 struct snd_ctl_elem_value *ucontrol)
1117 struct echoaudio *chip;
1118 int c;
1120 chip = snd_kcontrol_chip(kcontrol);
1121 for (c = 0; c < num_analog_busses_out(chip); c++)
1122 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1123 return 0;
1126 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1129 struct echoaudio *chip;
1130 int c, changed;
1132 changed = 0;
1133 chip = snd_kcontrol_chip(kcontrol);
1134 spin_lock_irq(&chip->lock);
1135 for (c = 0; c < num_analog_busses_out(chip); c++) {
1136 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1137 set_nominal_level(chip, c,
1138 ucontrol->value.integer.value[c]);
1139 changed = 1;
1142 if (changed)
1143 update_output_line_level(chip);
1144 spin_unlock_irq(&chip->lock);
1145 return changed;
1148 static struct snd_kcontrol_new snd_echo_output_nominal_level __devinitdata = {
1149 .name = "Line Playback Switch (-10dBV)",
1150 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1151 .info = snd_echo_output_nominal_info,
1152 .get = snd_echo_output_nominal_get,
1153 .put = snd_echo_output_nominal_put,
1156 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1160 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1162 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1163 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1164 struct snd_ctl_elem_info *uinfo)
1166 struct echoaudio *chip;
1168 chip = snd_kcontrol_chip(kcontrol);
1169 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1170 uinfo->count = num_analog_busses_in(chip);
1171 uinfo->value.integer.min = 0;
1172 uinfo->value.integer.max = 1;
1173 return 0;
1176 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1177 struct snd_ctl_elem_value *ucontrol)
1179 struct echoaudio *chip;
1180 int c;
1182 chip = snd_kcontrol_chip(kcontrol);
1183 for (c = 0; c < num_analog_busses_in(chip); c++)
1184 ucontrol->value.integer.value[c] =
1185 chip->nominal_level[bx_analog_in(chip) + c];
1186 return 0;
1189 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1190 struct snd_ctl_elem_value *ucontrol)
1192 struct echoaudio *chip;
1193 int c, changed;
1195 changed = 0;
1196 chip = snd_kcontrol_chip(kcontrol);
1197 spin_lock_irq(&chip->lock);
1198 for (c = 0; c < num_analog_busses_in(chip); c++) {
1199 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1200 ucontrol->value.integer.value[c]) {
1201 set_nominal_level(chip, bx_analog_in(chip) + c,
1202 ucontrol->value.integer.value[c]);
1203 changed = 1;
1206 if (changed)
1207 update_output_line_level(chip); /* "Output" is not a mistake
1208 * here.
1210 spin_unlock_irq(&chip->lock);
1211 return changed;
1214 static struct snd_kcontrol_new snd_echo_intput_nominal_level __devinitdata = {
1215 .name = "Line Capture Switch (-10dBV)",
1216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1217 .info = snd_echo_input_nominal_info,
1218 .get = snd_echo_input_nominal_get,
1219 .put = snd_echo_input_nominal_put,
1222 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1226 #ifdef ECHOCARD_HAS_MONITOR
1228 /******************* Monitor mixer *******************/
1229 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_info *uinfo)
1232 struct echoaudio *chip;
1234 chip = snd_kcontrol_chip(kcontrol);
1235 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1236 uinfo->count = 1;
1237 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1238 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1239 uinfo->dimen.d[0] = num_busses_out(chip);
1240 uinfo->dimen.d[1] = num_busses_in(chip);
1241 return 0;
1244 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1245 struct snd_ctl_elem_value *ucontrol)
1247 struct echoaudio *chip;
1249 chip = snd_kcontrol_chip(kcontrol);
1250 ucontrol->value.integer.value[0] =
1251 chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1252 [ucontrol->id.index % num_busses_in(chip)];
1253 return 0;
1256 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1257 struct snd_ctl_elem_value *ucontrol)
1259 struct echoaudio *chip;
1260 int changed, gain;
1261 short out, in;
1263 changed = 0;
1264 chip = snd_kcontrol_chip(kcontrol);
1265 out = ucontrol->id.index / num_busses_in(chip);
1266 in = ucontrol->id.index % num_busses_in(chip);
1267 gain = ucontrol->value.integer.value[0];
1268 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1269 return -EINVAL;
1270 if (chip->monitor_gain[out][in] != gain) {
1271 spin_lock_irq(&chip->lock);
1272 set_monitor_gain(chip, out, in, gain);
1273 update_output_line_level(chip);
1274 spin_unlock_irq(&chip->lock);
1275 changed = 1;
1277 return changed;
1280 static struct snd_kcontrol_new snd_echo_monitor_mixer __devinitdata = {
1281 .name = "Monitor Mixer Volume",
1282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1283 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1284 .info = snd_echo_mixer_info,
1285 .get = snd_echo_mixer_get,
1286 .put = snd_echo_mixer_put,
1287 .tlv = {.p = db_scale_output_gain},
1290 #endif /* ECHOCARD_HAS_MONITOR */
1294 #ifdef ECHOCARD_HAS_VMIXER
1296 /******************* Vmixer *******************/
1297 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_info *uinfo)
1300 struct echoaudio *chip;
1302 chip = snd_kcontrol_chip(kcontrol);
1303 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1304 uinfo->count = 1;
1305 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1306 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1307 uinfo->dimen.d[0] = num_busses_out(chip);
1308 uinfo->dimen.d[1] = num_pipes_out(chip);
1309 return 0;
1312 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1313 struct snd_ctl_elem_value *ucontrol)
1315 struct echoaudio *chip;
1317 chip = snd_kcontrol_chip(kcontrol);
1318 ucontrol->value.integer.value[0] =
1319 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1320 [ucontrol->id.index % num_pipes_out(chip)];
1321 return 0;
1324 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1325 struct snd_ctl_elem_value *ucontrol)
1327 struct echoaudio *chip;
1328 int gain, changed;
1329 short vch, out;
1331 changed = 0;
1332 chip = snd_kcontrol_chip(kcontrol);
1333 out = ucontrol->id.index / num_pipes_out(chip);
1334 vch = ucontrol->id.index % num_pipes_out(chip);
1335 gain = ucontrol->value.integer.value[0];
1336 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1337 return -EINVAL;
1338 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1339 spin_lock_irq(&chip->lock);
1340 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1341 update_vmixer_level(chip);
1342 spin_unlock_irq(&chip->lock);
1343 changed = 1;
1345 return changed;
1348 static struct snd_kcontrol_new snd_echo_vmixer __devinitdata = {
1349 .name = "VMixer Volume",
1350 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1351 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1352 .info = snd_echo_vmixer_info,
1353 .get = snd_echo_vmixer_get,
1354 .put = snd_echo_vmixer_put,
1355 .tlv = {.p = db_scale_output_gain},
1358 #endif /* ECHOCARD_HAS_VMIXER */
1362 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1364 /******************* Digital mode switch *******************/
1365 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1366 struct snd_ctl_elem_info *uinfo)
1368 static char *names[4] = {
1369 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1370 "S/PDIF Cdrom"
1372 struct echoaudio *chip;
1374 chip = snd_kcontrol_chip(kcontrol);
1375 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1376 uinfo->value.enumerated.items = chip->num_digital_modes;
1377 uinfo->count = 1;
1378 if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1379 uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1380 strcpy(uinfo->value.enumerated.name, names[
1381 chip->digital_mode_list[uinfo->value.enumerated.item]]);
1382 return 0;
1385 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1386 struct snd_ctl_elem_value *ucontrol)
1388 struct echoaudio *chip;
1389 int i, mode;
1391 chip = snd_kcontrol_chip(kcontrol);
1392 mode = chip->digital_mode;
1393 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1394 if (mode == chip->digital_mode_list[i]) {
1395 ucontrol->value.enumerated.item[0] = i;
1396 break;
1398 return 0;
1401 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1402 struct snd_ctl_elem_value *ucontrol)
1404 struct echoaudio *chip;
1405 int changed;
1406 unsigned short emode, dmode;
1408 changed = 0;
1409 chip = snd_kcontrol_chip(kcontrol);
1411 emode = ucontrol->value.enumerated.item[0];
1412 if (emode >= chip->num_digital_modes)
1413 return -EINVAL;
1414 dmode = chip->digital_mode_list[emode];
1416 if (dmode != chip->digital_mode) {
1417 /* mode_mutex is required to make this operation atomic wrt
1418 pcm_digital_*_open() and set_input_clock() functions. */
1419 mutex_lock(&chip->mode_mutex);
1421 /* Do not allow the user to change the digital mode when a pcm
1422 device is open because it also changes the number of channels
1423 and the allowed sample rates */
1424 if (atomic_read(&chip->opencount)) {
1425 changed = -EAGAIN;
1426 } else {
1427 changed = set_digital_mode(chip, dmode);
1428 /* If we had to change the clock source, report it */
1429 if (changed > 0 && chip->clock_src_ctl) {
1430 snd_ctl_notify(chip->card,
1431 SNDRV_CTL_EVENT_MASK_VALUE,
1432 &chip->clock_src_ctl->id);
1433 DE_ACT(("SDM() =%d\n", changed));
1435 if (changed >= 0)
1436 changed = 1; /* No errors */
1438 mutex_unlock(&chip->mode_mutex);
1440 return changed;
1443 static struct snd_kcontrol_new snd_echo_digital_mode_switch __devinitdata = {
1444 .name = "Digital mode Switch",
1445 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1446 .info = snd_echo_digital_mode_info,
1447 .get = snd_echo_digital_mode_get,
1448 .put = snd_echo_digital_mode_put,
1451 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1455 #ifdef ECHOCARD_HAS_DIGITAL_IO
1457 /******************* S/PDIF mode switch *******************/
1458 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_info *uinfo)
1461 static char *names[2] = {"Consumer", "Professional"};
1463 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1464 uinfo->value.enumerated.items = 2;
1465 uinfo->count = 1;
1466 if (uinfo->value.enumerated.item)
1467 uinfo->value.enumerated.item = 1;
1468 strcpy(uinfo->value.enumerated.name,
1469 names[uinfo->value.enumerated.item]);
1470 return 0;
1473 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_value *ucontrol)
1476 struct echoaudio *chip;
1478 chip = snd_kcontrol_chip(kcontrol);
1479 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1480 return 0;
1483 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1484 struct snd_ctl_elem_value *ucontrol)
1486 struct echoaudio *chip;
1487 int mode;
1489 chip = snd_kcontrol_chip(kcontrol);
1490 mode = !!ucontrol->value.enumerated.item[0];
1491 if (mode != chip->professional_spdif) {
1492 spin_lock_irq(&chip->lock);
1493 set_professional_spdif(chip, mode);
1494 spin_unlock_irq(&chip->lock);
1495 return 1;
1497 return 0;
1500 static struct snd_kcontrol_new snd_echo_spdif_mode_switch __devinitdata = {
1501 .name = "S/PDIF mode Switch",
1502 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1503 .info = snd_echo_spdif_mode_info,
1504 .get = snd_echo_spdif_mode_get,
1505 .put = snd_echo_spdif_mode_put,
1508 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1512 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1514 /******************* Select input clock source *******************/
1515 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_info *uinfo)
1518 static char *names[8] = {
1519 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1520 "ESync96", "MTC"
1522 struct echoaudio *chip;
1524 chip = snd_kcontrol_chip(kcontrol);
1525 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1526 uinfo->value.enumerated.items = chip->num_clock_sources;
1527 uinfo->count = 1;
1528 if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1529 uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1530 strcpy(uinfo->value.enumerated.name, names[
1531 chip->clock_source_list[uinfo->value.enumerated.item]]);
1532 return 0;
1535 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_value *ucontrol)
1538 struct echoaudio *chip;
1539 int i, clock;
1541 chip = snd_kcontrol_chip(kcontrol);
1542 clock = chip->input_clock;
1544 for (i = 0; i < chip->num_clock_sources; i++)
1545 if (clock == chip->clock_source_list[i])
1546 ucontrol->value.enumerated.item[0] = i;
1548 return 0;
1551 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_value *ucontrol)
1554 struct echoaudio *chip;
1555 int changed;
1556 unsigned int eclock, dclock;
1558 changed = 0;
1559 chip = snd_kcontrol_chip(kcontrol);
1560 eclock = ucontrol->value.enumerated.item[0];
1561 if (eclock >= chip->input_clock_types)
1562 return -EINVAL;
1563 dclock = chip->clock_source_list[eclock];
1564 if (chip->input_clock != dclock) {
1565 mutex_lock(&chip->mode_mutex);
1566 spin_lock_irq(&chip->lock);
1567 if ((changed = set_input_clock(chip, dclock)) == 0)
1568 changed = 1; /* no errors */
1569 spin_unlock_irq(&chip->lock);
1570 mutex_unlock(&chip->mode_mutex);
1573 if (changed < 0)
1574 DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1576 return changed;
1579 static struct snd_kcontrol_new snd_echo_clock_source_switch __devinitdata = {
1580 .name = "Sample Clock Source",
1581 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1582 .info = snd_echo_clock_source_info,
1583 .get = snd_echo_clock_source_get,
1584 .put = snd_echo_clock_source_put,
1587 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1591 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1593 /******************* Phantom power switch *******************/
1594 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1596 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1597 struct snd_ctl_elem_value *ucontrol)
1599 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1601 ucontrol->value.integer.value[0] = chip->phantom_power;
1602 return 0;
1605 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1606 struct snd_ctl_elem_value *ucontrol)
1608 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1609 int power, changed = 0;
1611 power = !!ucontrol->value.integer.value[0];
1612 if (chip->phantom_power != power) {
1613 spin_lock_irq(&chip->lock);
1614 changed = set_phantom_power(chip, power);
1615 spin_unlock_irq(&chip->lock);
1616 if (changed == 0)
1617 changed = 1; /* no errors */
1619 return changed;
1622 static struct snd_kcontrol_new snd_echo_phantom_power_switch __devinitdata = {
1623 .name = "Phantom power Switch",
1624 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1625 .info = snd_echo_phantom_power_info,
1626 .get = snd_echo_phantom_power_get,
1627 .put = snd_echo_phantom_power_put,
1630 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1634 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1636 /******************* Digital input automute switch *******************/
1637 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1639 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1642 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1644 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1645 return 0;
1648 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_value *ucontrol)
1651 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1652 int automute, changed = 0;
1654 automute = !!ucontrol->value.integer.value[0];
1655 if (chip->digital_in_automute != automute) {
1656 spin_lock_irq(&chip->lock);
1657 changed = set_input_auto_mute(chip, automute);
1658 spin_unlock_irq(&chip->lock);
1659 if (changed == 0)
1660 changed = 1; /* no errors */
1662 return changed;
1665 static struct snd_kcontrol_new snd_echo_automute_switch __devinitdata = {
1666 .name = "Digital Capture Switch (automute)",
1667 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1668 .info = snd_echo_automute_info,
1669 .get = snd_echo_automute_get,
1670 .put = snd_echo_automute_put,
1673 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1677 /******************* VU-meters switch *******************/
1678 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1680 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol)
1683 struct echoaudio *chip;
1685 chip = snd_kcontrol_chip(kcontrol);
1686 spin_lock_irq(&chip->lock);
1687 set_meters_on(chip, ucontrol->value.integer.value[0]);
1688 spin_unlock_irq(&chip->lock);
1689 return 1;
1692 static struct snd_kcontrol_new snd_echo_vumeters_switch __devinitdata = {
1693 .name = "VU-meters Switch",
1694 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1695 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1696 .info = snd_echo_vumeters_switch_info,
1697 .put = snd_echo_vumeters_switch_put,
1702 /***** Read VU-meters (input, output, analog and digital together) *****/
1703 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_info *uinfo)
1706 struct echoaudio *chip;
1708 chip = snd_kcontrol_chip(kcontrol);
1709 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1710 uinfo->count = 96;
1711 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1712 uinfo->value.integer.max = 0;
1713 #ifdef ECHOCARD_HAS_VMIXER
1714 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1715 #else
1716 uinfo->dimen.d[0] = 2; /* Out, In */
1717 #endif
1718 uinfo->dimen.d[1] = 16; /* 16 channels */
1719 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1720 return 0;
1723 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_value *ucontrol)
1726 struct echoaudio *chip;
1728 chip = snd_kcontrol_chip(kcontrol);
1729 get_audio_meters(chip, ucontrol->value.integer.value);
1730 return 0;
1733 static struct snd_kcontrol_new snd_echo_vumeters __devinitdata = {
1734 .name = "VU-meters",
1735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1737 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1738 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1739 .info = snd_echo_vumeters_info,
1740 .get = snd_echo_vumeters_get,
1741 .tlv = {.p = db_scale_output_gain},
1746 /*** Channels info - it exports informations about the number of channels ***/
1747 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_info *uinfo)
1750 struct echoaudio *chip;
1752 chip = snd_kcontrol_chip(kcontrol);
1753 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1754 uinfo->count = 6;
1755 uinfo->value.integer.min = 0;
1756 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1757 return 0;
1760 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1761 struct snd_ctl_elem_value *ucontrol)
1763 struct echoaudio *chip;
1764 int detected, clocks, bit, src;
1766 chip = snd_kcontrol_chip(kcontrol);
1767 ucontrol->value.integer.value[0] = num_busses_in(chip);
1768 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1769 ucontrol->value.integer.value[2] = num_busses_out(chip);
1770 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1771 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1773 /* Compute the bitmask of the currently valid input clocks */
1774 detected = detect_input_clocks(chip);
1775 clocks = 0;
1776 src = chip->num_clock_sources - 1;
1777 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1778 if (detected & (1 << bit))
1779 for (; src >= 0; src--)
1780 if (bit == chip->clock_source_list[src]) {
1781 clocks |= 1 << src;
1782 break;
1784 ucontrol->value.integer.value[5] = clocks;
1786 return 0;
1789 static struct snd_kcontrol_new snd_echo_channels_info __devinitdata = {
1790 .name = "Channels info",
1791 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1792 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1793 .info = snd_echo_channels_info_info,
1794 .get = snd_echo_channels_info_get,
1800 /******************************************************************************
1801 IRQ Handler
1802 ******************************************************************************/
1804 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1806 struct echoaudio *chip = dev_id;
1807 struct snd_pcm_substream *substream;
1808 int period, ss, st;
1810 spin_lock(&chip->lock);
1811 st = service_irq(chip);
1812 if (st < 0) {
1813 spin_unlock(&chip->lock);
1814 return IRQ_NONE;
1816 /* The hardware doesn't tell us which substream caused the irq,
1817 thus we have to check all running substreams. */
1818 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1819 if ((substream = chip->substream[ss])) {
1820 period = pcm_pointer(substream) /
1821 substream->runtime->period_size;
1822 if (period != chip->last_period[ss]) {
1823 chip->last_period[ss] = period;
1824 spin_unlock(&chip->lock);
1825 snd_pcm_period_elapsed(substream);
1826 spin_lock(&chip->lock);
1830 spin_unlock(&chip->lock);
1832 #ifdef ECHOCARD_HAS_MIDI
1833 if (st > 0 && chip->midi_in) {
1834 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1835 DE_MID(("rawmidi_iread=%d\n", st));
1837 #endif
1838 return IRQ_HANDLED;
1844 /******************************************************************************
1845 Module construction / destruction
1846 ******************************************************************************/
1848 static int snd_echo_free(struct echoaudio *chip)
1850 DE_INIT(("Stop DSP...\n"));
1851 if (chip->comm_page)
1852 rest_in_peace(chip);
1853 DE_INIT(("Stopped.\n"));
1855 if (chip->irq >= 0)
1856 free_irq(chip->irq, chip);
1858 if (chip->comm_page)
1859 snd_dma_free_pages(&chip->commpage_dma_buf);
1861 if (chip->dsp_registers)
1862 iounmap(chip->dsp_registers);
1864 if (chip->iores)
1865 release_and_free_resource(chip->iores);
1867 DE_INIT(("MMIO freed.\n"));
1869 pci_disable_device(chip->pci);
1871 /* release chip data */
1872 kfree(chip);
1873 DE_INIT(("Chip freed.\n"));
1874 return 0;
1879 static int snd_echo_dev_free(struct snd_device *device)
1881 struct echoaudio *chip = device->device_data;
1883 DE_INIT(("snd_echo_dev_free()...\n"));
1884 return snd_echo_free(chip);
1889 /* <--snd_echo_probe() */
1890 static __devinit int snd_echo_create(struct snd_card *card,
1891 struct pci_dev *pci,
1892 struct echoaudio **rchip)
1894 struct echoaudio *chip;
1895 int err;
1896 size_t sz;
1897 static struct snd_device_ops ops = {
1898 .dev_free = snd_echo_dev_free,
1901 *rchip = NULL;
1903 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1905 if ((err = pci_enable_device(pci)) < 0)
1906 return err;
1907 pci_set_master(pci);
1909 /* allocate a chip-specific data */
1910 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1911 if (!chip) {
1912 pci_disable_device(pci);
1913 return -ENOMEM;
1915 DE_INIT(("chip=%p\n", chip));
1917 spin_lock_init(&chip->lock);
1918 chip->card = card;
1919 chip->pci = pci;
1920 chip->irq = -1;
1922 /* PCI resource allocation */
1923 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1924 sz = pci_resource_len(pci, 0);
1925 if (sz > PAGE_SIZE)
1926 sz = PAGE_SIZE; /* We map only the required part */
1928 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1929 ECHOCARD_NAME)) == NULL) {
1930 snd_echo_free(chip);
1931 snd_printk(KERN_ERR "cannot get memory region\n");
1932 return -EBUSY;
1934 chip->dsp_registers = (volatile u32 __iomem *)
1935 ioremap_nocache(chip->dsp_registers_phys, sz);
1937 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1938 ECHOCARD_NAME, chip)) {
1939 snd_echo_free(chip);
1940 snd_printk(KERN_ERR "cannot grab irq\n");
1941 return -EBUSY;
1943 chip->irq = pci->irq;
1944 DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
1945 chip->pci, chip->irq, chip->pci->subsystem_device));
1947 /* Create the DSP comm page - this is the area of memory used for most
1948 of the communication with the DSP, which accesses it via bus mastering */
1949 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1950 sizeof(struct comm_page),
1951 &chip->commpage_dma_buf) < 0) {
1952 snd_echo_free(chip);
1953 snd_printk(KERN_ERR "cannot allocate the comm page\n");
1954 return -ENOMEM;
1956 chip->comm_page_phys = chip->commpage_dma_buf.addr;
1957 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1959 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1960 if (err) {
1961 DE_INIT(("init_hw err=%d\n", err));
1962 snd_echo_free(chip);
1963 return err;
1965 DE_INIT(("Card init OK\n"));
1967 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1968 snd_echo_free(chip);
1969 return err;
1971 atomic_set(&chip->opencount, 0);
1972 mutex_init(&chip->mode_mutex);
1973 chip->can_set_rate = 1;
1974 *rchip = chip;
1975 /* Init done ! */
1976 return 0;
1981 /* constructor */
1982 static int __devinit snd_echo_probe(struct pci_dev *pci,
1983 const struct pci_device_id *pci_id)
1985 static int dev;
1986 struct snd_card *card;
1987 struct echoaudio *chip;
1988 char *dsp;
1989 int i, err;
1991 if (dev >= SNDRV_CARDS)
1992 return -ENODEV;
1993 if (!enable[dev]) {
1994 dev++;
1995 return -ENOENT;
1998 DE_INIT(("Echoaudio driver starting...\n"));
1999 i = 0;
2000 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2001 if (card == NULL)
2002 return -ENOMEM;
2004 snd_card_set_dev(card, &pci->dev);
2006 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2007 snd_card_free(card);
2008 return err;
2011 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2012 strcpy(card->shortname, chip->card_name);
2014 dsp = "56301";
2015 if (pci_id->device == 0x3410)
2016 dsp = "56361";
2018 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2019 card->shortname, pci_id->subdevice & 0x000f, dsp,
2020 chip->dsp_registers_phys, chip->irq);
2022 if ((err = snd_echo_new_pcm(chip)) < 0) {
2023 snd_printk(KERN_ERR "new pcm error %d\n", err);
2024 snd_card_free(card);
2025 return err;
2028 #ifdef ECHOCARD_HAS_MIDI
2029 if (chip->has_midi) { /* Some Mia's do not have midi */
2030 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2031 snd_printk(KERN_ERR "new midi error %d\n", err);
2032 snd_card_free(card);
2033 return err;
2036 #endif
2038 #ifdef ECHOCARD_HAS_VMIXER
2039 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2040 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_output_gain, chip))) < 0)
2041 goto ctl_error;
2042 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2043 goto ctl_error;
2044 #else
2045 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_pcm_output_gain, chip))) < 0)
2046 goto ctl_error;
2047 #endif
2049 #ifdef ECHOCARD_HAS_INPUT_GAIN
2050 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2051 goto ctl_error;
2052 #endif
2054 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2055 if (!chip->hasnt_input_nominal_level)
2056 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2057 goto ctl_error;
2058 #endif
2060 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2061 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2062 goto ctl_error;
2063 #endif
2065 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2066 goto ctl_error;
2068 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2069 goto ctl_error;
2071 #ifdef ECHOCARD_HAS_MONITOR
2072 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2073 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2074 goto ctl_error;
2075 #endif
2077 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2078 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2079 goto ctl_error;
2080 #endif
2082 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2083 goto ctl_error;
2085 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2086 /* Creates a list of available digital modes */
2087 chip->num_digital_modes = 0;
2088 for (i = 0; i < 6; i++)
2089 if (chip->digital_modes & (1 << i))
2090 chip->digital_mode_list[chip->num_digital_modes++] = i;
2092 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2093 goto ctl_error;
2094 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2096 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2097 /* Creates a list of available clock sources */
2098 chip->num_clock_sources = 0;
2099 for (i = 0; i < 10; i++)
2100 if (chip->input_clock_types & (1 << i))
2101 chip->clock_source_list[chip->num_clock_sources++] = i;
2103 if (chip->num_clock_sources > 1) {
2104 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2105 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2106 goto ctl_error;
2108 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2110 #ifdef ECHOCARD_HAS_DIGITAL_IO
2111 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2112 goto ctl_error;
2113 #endif
2115 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2116 if (chip->has_phantom_power)
2117 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2118 goto ctl_error;
2119 #endif
2121 if ((err = snd_card_register(card)) < 0) {
2122 snd_card_free(card);
2123 goto ctl_error;
2125 snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2127 pci_set_drvdata(pci, chip);
2128 dev++;
2129 return 0;
2131 ctl_error:
2132 snd_printk(KERN_ERR "new control error %d\n", err);
2133 snd_card_free(card);
2134 return err;
2139 static void __devexit snd_echo_remove(struct pci_dev *pci)
2141 struct echoaudio *chip;
2143 chip = pci_get_drvdata(pci);
2144 if (chip)
2145 snd_card_free(chip->card);
2146 pci_set_drvdata(pci, NULL);
2151 /******************************************************************************
2152 Everything starts and ends here
2153 ******************************************************************************/
2155 /* pci_driver definition */
2156 static struct pci_driver driver = {
2157 .name = "Echoaudio " ECHOCARD_NAME,
2158 .id_table = snd_echo_ids,
2159 .probe = snd_echo_probe,
2160 .remove = __devexit_p(snd_echo_remove),
2165 /* initialization of the module */
2166 static int __init alsa_card_echo_init(void)
2168 return pci_register_driver(&driver);
2173 /* clean up the module */
2174 static void __exit alsa_card_echo_exit(void)
2176 pci_unregister_driver(&driver);
2180 module_init(alsa_card_echo_init)
2181 module_exit(alsa_card_echo_exit)