powerpc/64: Copy as much as possible in __copy_tofrom_user
[linux/fpc-iii.git] / sound / pci / echoaudio / echoaudio.c
blob358ef7dcf4105f2a20666d4da87362ba0af2e0db
1 /*
2 * ALSA driver for Echoaudio soundcards.
3 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <linux/module.h>
21 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
22 MODULE_LICENSE("GPL v2");
23 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
24 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
25 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
27 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
28 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
29 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
31 module_param_array(index, int, NULL, 0444);
32 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
33 module_param_array(id, charp, NULL, 0444);
34 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
35 module_param_array(enable, bool, NULL, 0444);
36 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
38 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
39 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
43 static int get_firmware(const struct firmware **fw_entry,
44 struct echoaudio *chip, const short fw_index)
46 int err;
47 char name[30];
49 #ifdef CONFIG_PM_SLEEP
50 if (chip->fw_cache[fw_index]) {
51 dev_dbg(chip->card->dev,
52 "firmware requested: %s is cached\n",
53 card_fw[fw_index].data);
54 *fw_entry = chip->fw_cache[fw_index];
55 return 0;
57 #endif
59 dev_dbg(chip->card->dev,
60 "firmware requested: %s\n", card_fw[fw_index].data);
61 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
62 err = request_firmware(fw_entry, name, &chip->pci->dev);
63 if (err < 0)
64 dev_err(chip->card->dev,
65 "get_firmware(): Firmware not available (%d)\n", err);
66 #ifdef CONFIG_PM_SLEEP
67 else
68 chip->fw_cache[fw_index] = *fw_entry;
69 #endif
70 return err;
75 static void free_firmware(const struct firmware *fw_entry,
76 struct echoaudio *chip)
78 #ifdef CONFIG_PM_SLEEP
79 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
80 #else
81 release_firmware(fw_entry);
82 #endif
87 static void free_firmware_cache(struct echoaudio *chip)
89 #ifdef CONFIG_PM_SLEEP
90 int i;
92 for (i = 0; i < 8 ; i++)
93 if (chip->fw_cache[i]) {
94 release_firmware(chip->fw_cache[i]);
95 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
98 #endif
103 /******************************************************************************
104 PCM interface
105 ******************************************************************************/
107 static void audiopipe_free(struct snd_pcm_runtime *runtime)
109 struct audiopipe *pipe = runtime->private_data;
111 if (pipe->sgpage.area)
112 snd_dma_free_pages(&pipe->sgpage);
113 kfree(pipe);
118 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
119 struct snd_pcm_hw_rule *rule)
121 struct snd_interval *c = hw_param_interval(params,
122 SNDRV_PCM_HW_PARAM_CHANNELS);
123 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
124 struct snd_mask fmt;
126 snd_mask_any(&fmt);
128 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
129 /* >=2 channels cannot be S32_BE */
130 if (c->min == 2) {
131 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
132 return snd_mask_refine(f, &fmt);
134 #endif
135 /* > 2 channels cannot be U8 and S32_BE */
136 if (c->min > 2) {
137 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
138 return snd_mask_refine(f, &fmt);
140 /* Mono is ok with any format */
141 return 0;
146 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
147 struct snd_pcm_hw_rule *rule)
149 struct snd_interval *c = hw_param_interval(params,
150 SNDRV_PCM_HW_PARAM_CHANNELS);
151 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
152 struct snd_interval ch;
154 snd_interval_any(&ch);
156 /* S32_BE is mono (and stereo) only */
157 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
158 ch.min = 1;
159 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
160 ch.max = 2;
161 #else
162 ch.max = 1;
163 #endif
164 ch.integer = 1;
165 return snd_interval_refine(c, &ch);
167 /* U8 can be only mono or stereo */
168 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
169 ch.min = 1;
170 ch.max = 2;
171 ch.integer = 1;
172 return snd_interval_refine(c, &ch);
174 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
175 return 0;
180 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
181 struct snd_pcm_hw_rule *rule)
183 struct snd_interval *c = hw_param_interval(params,
184 SNDRV_PCM_HW_PARAM_CHANNELS);
185 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
186 struct snd_mask fmt;
187 u64 fmask;
188 snd_mask_any(&fmt);
190 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
192 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
193 if (c->min > 2) {
194 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
195 SNDRV_PCM_FMTBIT_S24_3LE |
196 SNDRV_PCM_FMTBIT_S32_LE;
197 /* 1 channel must be S32_BE or S32_LE */
198 } else if (c->max == 1)
199 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
200 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
201 /* 2 channels cannot be S32_BE */
202 else if (c->min == 2 && c->max == 2)
203 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
204 #endif
205 else
206 return 0;
208 fmt.bits[0] &= (u32)fmask;
209 fmt.bits[1] &= (u32)(fmask >> 32);
210 return snd_mask_refine(f, &fmt);
215 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
216 struct snd_pcm_hw_rule *rule)
218 struct snd_interval *c = hw_param_interval(params,
219 SNDRV_PCM_HW_PARAM_CHANNELS);
220 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
221 struct snd_interval ch;
222 u64 fmask;
224 snd_interval_any(&ch);
225 ch.integer = 1;
226 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
228 /* S32_BE is mono (and stereo) only */
229 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
230 ch.min = 1;
231 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
232 ch.max = 2;
233 #else
234 ch.max = 1;
235 #endif
236 /* U8 is stereo only */
237 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
238 ch.min = ch.max = 2;
239 /* S16_LE and S24_3LE must be at least stereo */
240 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
241 SNDRV_PCM_FMTBIT_S24_3LE)))
242 ch.min = 2;
243 else
244 return 0;
246 return snd_interval_refine(c, &ch);
251 /* Since the sample rate is a global setting, do allow the user to change the
252 sample rate only if there is only one pcm device open. */
253 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
254 struct snd_pcm_hw_rule *rule)
256 struct snd_interval *rate = hw_param_interval(params,
257 SNDRV_PCM_HW_PARAM_RATE);
258 struct echoaudio *chip = rule->private;
259 struct snd_interval fixed;
261 if (!chip->can_set_rate) {
262 snd_interval_any(&fixed);
263 fixed.min = fixed.max = chip->sample_rate;
264 return snd_interval_refine(rate, &fixed);
266 return 0;
270 static int pcm_open(struct snd_pcm_substream *substream,
271 signed char max_channels)
273 struct echoaudio *chip;
274 struct snd_pcm_runtime *runtime;
275 struct audiopipe *pipe;
276 int err, i;
278 if (max_channels <= 0)
279 return -EAGAIN;
281 chip = snd_pcm_substream_chip(substream);
282 runtime = substream->runtime;
284 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
285 if (!pipe)
286 return -ENOMEM;
287 pipe->index = -1; /* Not configured yet */
289 /* Set up hw capabilities and contraints */
290 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
291 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
292 pipe->constr.list = channels_list;
293 pipe->constr.mask = 0;
294 for (i = 0; channels_list[i] <= max_channels; i++);
295 pipe->constr.count = i;
296 if (pipe->hw.channels_max > max_channels)
297 pipe->hw.channels_max = max_channels;
298 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
299 pipe->hw.rate_max = 48000;
300 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
303 runtime->hw = pipe->hw;
304 runtime->private_data = pipe;
305 runtime->private_free = audiopipe_free;
306 snd_pcm_set_sync(substream);
308 /* Only mono and any even number of channels are allowed */
309 if ((err = snd_pcm_hw_constraint_list(runtime, 0,
310 SNDRV_PCM_HW_PARAM_CHANNELS,
311 &pipe->constr)) < 0)
312 return err;
314 /* All periods should have the same size */
315 if ((err = snd_pcm_hw_constraint_integer(runtime,
316 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
317 return err;
319 /* The hw accesses memory in chunks 32 frames long and they should be
320 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
321 generated with a resolution of 32 frames. Thus we need the following */
322 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
323 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
324 32)) < 0)
325 return err;
326 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
327 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
328 32)) < 0)
329 return err;
331 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
332 SNDRV_PCM_HW_PARAM_RATE,
333 hw_rule_sample_rate, chip,
334 SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
335 return err;
337 /* Finally allocate a page for the scatter-gather list */
338 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
339 snd_dma_pci_data(chip->pci),
340 PAGE_SIZE, &pipe->sgpage)) < 0) {
341 dev_err(chip->card->dev, "s-g list allocation failed\n");
342 return err;
345 return 0;
350 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
352 struct echoaudio *chip = snd_pcm_substream_chip(substream);
353 int err;
355 if ((err = pcm_open(substream, num_analog_busses_in(chip) -
356 substream->number)) < 0)
357 return err;
358 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
359 SNDRV_PCM_HW_PARAM_CHANNELS,
360 hw_rule_capture_channels_by_format, NULL,
361 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
362 return err;
363 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
364 SNDRV_PCM_HW_PARAM_FORMAT,
365 hw_rule_capture_format_by_channels, NULL,
366 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
367 return err;
368 atomic_inc(&chip->opencount);
369 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
370 chip->can_set_rate=0;
371 dev_dbg(chip->card->dev, "pcm_analog_in_open cs=%d oc=%d r=%d\n",
372 chip->can_set_rate, atomic_read(&chip->opencount),
373 chip->sample_rate);
374 return 0;
379 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
381 struct echoaudio *chip = snd_pcm_substream_chip(substream);
382 int max_channels, err;
384 #ifdef ECHOCARD_HAS_VMIXER
385 max_channels = num_pipes_out(chip);
386 #else
387 max_channels = num_analog_busses_out(chip);
388 #endif
389 if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
390 return err;
391 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
392 SNDRV_PCM_HW_PARAM_CHANNELS,
393 hw_rule_playback_channels_by_format,
394 NULL,
395 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
396 return err;
397 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
398 SNDRV_PCM_HW_PARAM_FORMAT,
399 hw_rule_playback_format_by_channels,
400 NULL,
401 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
402 return err;
403 atomic_inc(&chip->opencount);
404 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
405 chip->can_set_rate=0;
406 dev_dbg(chip->card->dev, "pcm_analog_out_open cs=%d oc=%d r=%d\n",
407 chip->can_set_rate, atomic_read(&chip->opencount),
408 chip->sample_rate);
409 return 0;
414 #ifdef ECHOCARD_HAS_DIGITAL_IO
416 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
418 struct echoaudio *chip = snd_pcm_substream_chip(substream);
419 int err, max_channels;
421 max_channels = num_digital_busses_in(chip) - substream->number;
422 mutex_lock(&chip->mode_mutex);
423 if (chip->digital_mode == DIGITAL_MODE_ADAT)
424 err = pcm_open(substream, max_channels);
425 else /* If the card has ADAT, subtract the 6 channels
426 * that S/PDIF doesn't have
428 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
430 if (err < 0)
431 goto din_exit;
433 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
434 SNDRV_PCM_HW_PARAM_CHANNELS,
435 hw_rule_capture_channels_by_format, NULL,
436 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
437 goto din_exit;
438 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
439 SNDRV_PCM_HW_PARAM_FORMAT,
440 hw_rule_capture_format_by_channels, NULL,
441 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
442 goto din_exit;
444 atomic_inc(&chip->opencount);
445 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
446 chip->can_set_rate=0;
448 din_exit:
449 mutex_unlock(&chip->mode_mutex);
450 return err;
455 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
457 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
459 struct echoaudio *chip = snd_pcm_substream_chip(substream);
460 int err, max_channels;
462 max_channels = num_digital_busses_out(chip) - substream->number;
463 mutex_lock(&chip->mode_mutex);
464 if (chip->digital_mode == DIGITAL_MODE_ADAT)
465 err = pcm_open(substream, max_channels);
466 else /* If the card has ADAT, subtract the 6 channels
467 * that S/PDIF doesn't have
469 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
471 if (err < 0)
472 goto dout_exit;
474 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
475 SNDRV_PCM_HW_PARAM_CHANNELS,
476 hw_rule_playback_channels_by_format,
477 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
478 -1)) < 0)
479 goto dout_exit;
480 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
481 SNDRV_PCM_HW_PARAM_FORMAT,
482 hw_rule_playback_format_by_channels,
483 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
484 -1)) < 0)
485 goto dout_exit;
486 atomic_inc(&chip->opencount);
487 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
488 chip->can_set_rate=0;
489 dout_exit:
490 mutex_unlock(&chip->mode_mutex);
491 return err;
494 #endif /* !ECHOCARD_HAS_VMIXER */
496 #endif /* ECHOCARD_HAS_DIGITAL_IO */
500 static int pcm_close(struct snd_pcm_substream *substream)
502 struct echoaudio *chip = snd_pcm_substream_chip(substream);
503 int oc;
505 /* Nothing to do here. Audio is already off and pipe will be
506 * freed by its callback
509 atomic_dec(&chip->opencount);
510 oc = atomic_read(&chip->opencount);
511 dev_dbg(chip->card->dev, "pcm_close oc=%d cs=%d rs=%d\n", oc,
512 chip->can_set_rate, chip->rate_set);
513 if (oc < 2)
514 chip->can_set_rate = 1;
515 if (oc == 0)
516 chip->rate_set = 0;
517 dev_dbg(chip->card->dev, "pcm_close2 oc=%d cs=%d rs=%d\n", oc,
518 chip->can_set_rate, chip->rate_set);
520 return 0;
525 /* Channel allocation and scatter-gather list setup */
526 static int init_engine(struct snd_pcm_substream *substream,
527 struct snd_pcm_hw_params *hw_params,
528 int pipe_index, int interleave)
530 struct echoaudio *chip;
531 int err, per, rest, page, edge, offs;
532 struct audiopipe *pipe;
534 chip = snd_pcm_substream_chip(substream);
535 pipe = (struct audiopipe *) substream->runtime->private_data;
537 /* Sets up che hardware. If it's already initialized, reset and
538 * redo with the new parameters
540 spin_lock_irq(&chip->lock);
541 if (pipe->index >= 0) {
542 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
543 err = free_pipes(chip, pipe);
544 snd_BUG_ON(err);
545 chip->substream[pipe->index] = NULL;
548 err = allocate_pipes(chip, pipe, pipe_index, interleave);
549 if (err < 0) {
550 spin_unlock_irq(&chip->lock);
551 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
552 pipe_index, err);
553 return err;
555 spin_unlock_irq(&chip->lock);
556 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
558 dev_dbg(chip->card->dev,
559 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
560 params_buffer_bytes(hw_params), params_periods(hw_params),
561 params_period_bytes(hw_params));
562 err = snd_pcm_lib_malloc_pages(substream,
563 params_buffer_bytes(hw_params));
564 if (err < 0) {
565 dev_err(chip->card->dev, "malloc_pages err=%d\n", err);
566 spin_lock_irq(&chip->lock);
567 free_pipes(chip, pipe);
568 spin_unlock_irq(&chip->lock);
569 pipe->index = -1;
570 return err;
573 sglist_init(chip, pipe);
574 edge = PAGE_SIZE;
575 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
576 per++) {
577 rest = params_period_bytes(hw_params);
578 if (offs + rest > params_buffer_bytes(hw_params))
579 rest = params_buffer_bytes(hw_params) - offs;
580 while (rest) {
581 dma_addr_t addr;
582 addr = snd_pcm_sgbuf_get_addr(substream, offs);
583 if (rest <= edge - offs) {
584 sglist_add_mapping(chip, pipe, addr, rest);
585 sglist_add_irq(chip, pipe);
586 offs += rest;
587 rest = 0;
588 } else {
589 sglist_add_mapping(chip, pipe, addr,
590 edge - offs);
591 rest -= edge - offs;
592 offs = edge;
594 if (offs == edge) {
595 edge += PAGE_SIZE;
596 page++;
601 /* Close the ring buffer */
602 sglist_wrap(chip, pipe);
604 /* This stuff is used by the irq handler, so it must be
605 * initialized before chip->substream
607 chip->last_period[pipe_index] = 0;
608 pipe->last_counter = 0;
609 pipe->position = 0;
610 smp_wmb();
611 chip->substream[pipe_index] = substream;
612 chip->rate_set = 1;
613 spin_lock_irq(&chip->lock);
614 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
615 spin_unlock_irq(&chip->lock);
616 return 0;
621 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
622 struct snd_pcm_hw_params *hw_params)
624 struct echoaudio *chip = snd_pcm_substream_chip(substream);
626 return init_engine(substream, hw_params, px_analog_in(chip) +
627 substream->number, params_channels(hw_params));
632 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
633 struct snd_pcm_hw_params *hw_params)
635 return init_engine(substream, hw_params, substream->number,
636 params_channels(hw_params));
641 #ifdef ECHOCARD_HAS_DIGITAL_IO
643 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
644 struct snd_pcm_hw_params *hw_params)
646 struct echoaudio *chip = snd_pcm_substream_chip(substream);
648 return init_engine(substream, hw_params, px_digital_in(chip) +
649 substream->number, params_channels(hw_params));
654 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
655 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_hw_params *hw_params)
658 struct echoaudio *chip = snd_pcm_substream_chip(substream);
660 return init_engine(substream, hw_params, px_digital_out(chip) +
661 substream->number, params_channels(hw_params));
663 #endif /* !ECHOCARD_HAS_VMIXER */
665 #endif /* ECHOCARD_HAS_DIGITAL_IO */
669 static int pcm_hw_free(struct snd_pcm_substream *substream)
671 struct echoaudio *chip;
672 struct audiopipe *pipe;
674 chip = snd_pcm_substream_chip(substream);
675 pipe = (struct audiopipe *) substream->runtime->private_data;
677 spin_lock_irq(&chip->lock);
678 if (pipe->index >= 0) {
679 dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
680 free_pipes(chip, pipe);
681 chip->substream[pipe->index] = NULL;
682 pipe->index = -1;
684 spin_unlock_irq(&chip->lock);
686 snd_pcm_lib_free_pages(substream);
687 return 0;
692 static int pcm_prepare(struct snd_pcm_substream *substream)
694 struct echoaudio *chip = snd_pcm_substream_chip(substream);
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 struct audioformat format;
697 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
699 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
700 runtime->rate, runtime->format, runtime->channels);
701 format.interleave = runtime->channels;
702 format.data_are_bigendian = 0;
703 format.mono_to_stereo = 0;
704 switch (runtime->format) {
705 case SNDRV_PCM_FORMAT_U8:
706 format.bits_per_sample = 8;
707 break;
708 case SNDRV_PCM_FORMAT_S16_LE:
709 format.bits_per_sample = 16;
710 break;
711 case SNDRV_PCM_FORMAT_S24_3LE:
712 format.bits_per_sample = 24;
713 break;
714 case SNDRV_PCM_FORMAT_S32_BE:
715 format.data_are_bigendian = 1;
716 case SNDRV_PCM_FORMAT_S32_LE:
717 format.bits_per_sample = 32;
718 break;
719 default:
720 dev_err(chip->card->dev,
721 "Prepare error: unsupported format %d\n",
722 runtime->format);
723 return -EINVAL;
726 if (snd_BUG_ON(pipe_index >= px_num(chip)))
727 return -EINVAL;
728 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
729 return -EINVAL;
730 set_audio_format(chip, pipe_index, &format);
731 return 0;
736 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
738 struct echoaudio *chip = snd_pcm_substream_chip(substream);
739 struct audiopipe *pipe;
740 int i, err;
741 u32 channelmask = 0;
742 struct snd_pcm_substream *s;
744 snd_pcm_group_for_each_entry(s, substream) {
745 for (i = 0; i < DSP_MAXPIPES; i++) {
746 if (s == chip->substream[i]) {
747 channelmask |= 1 << i;
748 snd_pcm_trigger_done(s, substream);
753 spin_lock(&chip->lock);
754 switch (cmd) {
755 case SNDRV_PCM_TRIGGER_RESUME:
756 case SNDRV_PCM_TRIGGER_START:
757 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
758 for (i = 0; i < DSP_MAXPIPES; i++) {
759 if (channelmask & (1 << i)) {
760 pipe = chip->substream[i]->runtime->private_data;
761 switch (pipe->state) {
762 case PIPE_STATE_STOPPED:
763 chip->last_period[i] = 0;
764 pipe->last_counter = 0;
765 pipe->position = 0;
766 *pipe->dma_counter = 0;
767 case PIPE_STATE_PAUSED:
768 pipe->state = PIPE_STATE_STARTED;
769 break;
770 case PIPE_STATE_STARTED:
771 break;
775 err = start_transport(chip, channelmask,
776 chip->pipe_cyclic_mask);
777 break;
778 case SNDRV_PCM_TRIGGER_SUSPEND:
779 case SNDRV_PCM_TRIGGER_STOP:
780 for (i = 0; i < DSP_MAXPIPES; i++) {
781 if (channelmask & (1 << i)) {
782 pipe = chip->substream[i]->runtime->private_data;
783 pipe->state = PIPE_STATE_STOPPED;
786 err = stop_transport(chip, channelmask);
787 break;
788 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
789 for (i = 0; i < DSP_MAXPIPES; i++) {
790 if (channelmask & (1 << i)) {
791 pipe = chip->substream[i]->runtime->private_data;
792 pipe->state = PIPE_STATE_PAUSED;
795 err = pause_transport(chip, channelmask);
796 break;
797 default:
798 err = -EINVAL;
800 spin_unlock(&chip->lock);
801 return err;
806 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
808 struct snd_pcm_runtime *runtime = substream->runtime;
809 struct audiopipe *pipe = runtime->private_data;
810 size_t cnt, bufsize, pos;
812 cnt = le32_to_cpu(*pipe->dma_counter);
813 pipe->position += cnt - pipe->last_counter;
814 pipe->last_counter = cnt;
815 bufsize = substream->runtime->buffer_size;
816 pos = bytes_to_frames(substream->runtime, pipe->position);
818 while (pos >= bufsize) {
819 pipe->position -= frames_to_bytes(substream->runtime, bufsize);
820 pos -= bufsize;
822 return pos;
827 /* pcm *_ops structures */
828 static const struct snd_pcm_ops analog_playback_ops = {
829 .open = pcm_analog_out_open,
830 .close = pcm_close,
831 .ioctl = snd_pcm_lib_ioctl,
832 .hw_params = pcm_analog_out_hw_params,
833 .hw_free = pcm_hw_free,
834 .prepare = pcm_prepare,
835 .trigger = pcm_trigger,
836 .pointer = pcm_pointer,
837 .page = snd_pcm_sgbuf_ops_page,
839 static const struct snd_pcm_ops analog_capture_ops = {
840 .open = pcm_analog_in_open,
841 .close = pcm_close,
842 .ioctl = snd_pcm_lib_ioctl,
843 .hw_params = pcm_analog_in_hw_params,
844 .hw_free = pcm_hw_free,
845 .prepare = pcm_prepare,
846 .trigger = pcm_trigger,
847 .pointer = pcm_pointer,
848 .page = snd_pcm_sgbuf_ops_page,
850 #ifdef ECHOCARD_HAS_DIGITAL_IO
851 #ifndef ECHOCARD_HAS_VMIXER
852 static const struct snd_pcm_ops digital_playback_ops = {
853 .open = pcm_digital_out_open,
854 .close = pcm_close,
855 .ioctl = snd_pcm_lib_ioctl,
856 .hw_params = pcm_digital_out_hw_params,
857 .hw_free = pcm_hw_free,
858 .prepare = pcm_prepare,
859 .trigger = pcm_trigger,
860 .pointer = pcm_pointer,
861 .page = snd_pcm_sgbuf_ops_page,
863 #endif /* !ECHOCARD_HAS_VMIXER */
864 static const struct snd_pcm_ops digital_capture_ops = {
865 .open = pcm_digital_in_open,
866 .close = pcm_close,
867 .ioctl = snd_pcm_lib_ioctl,
868 .hw_params = pcm_digital_in_hw_params,
869 .hw_free = pcm_hw_free,
870 .prepare = pcm_prepare,
871 .trigger = pcm_trigger,
872 .pointer = pcm_pointer,
873 .page = snd_pcm_sgbuf_ops_page,
875 #endif /* ECHOCARD_HAS_DIGITAL_IO */
879 /* Preallocate memory only for the first substream because it's the most
880 * used one
882 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
884 struct snd_pcm_substream *ss;
885 int stream, err;
887 for (stream = 0; stream < 2; stream++)
888 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) {
889 err = snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
890 dev,
891 ss->number ? 0 : 128<<10,
892 256<<10);
893 if (err < 0)
894 return err;
896 return 0;
901 /*<--snd_echo_probe() */
902 static int snd_echo_new_pcm(struct echoaudio *chip)
904 struct snd_pcm *pcm;
905 int err;
907 #ifdef ECHOCARD_HAS_VMIXER
908 /* This card has a Vmixer, that is there is no direct mapping from PCM
909 streams to physical outputs. The user can mix the streams as he wishes
910 via control interface and it's possible to send any stream to any
911 output, thus it makes no sense to keep analog and digital outputs
912 separated */
914 /* PCM#0 Virtual outputs and analog inputs */
915 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
916 num_analog_busses_in(chip), &pcm)) < 0)
917 return err;
918 pcm->private_data = chip;
919 chip->analog_pcm = pcm;
920 strcpy(pcm->name, chip->card->shortname);
921 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
922 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
923 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
924 return err;
926 #ifdef ECHOCARD_HAS_DIGITAL_IO
927 /* PCM#1 Digital inputs, no outputs */
928 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
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_CAPTURE, &digital_capture_ops);
935 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
936 return err;
937 #endif /* ECHOCARD_HAS_DIGITAL_IO */
939 #else /* ECHOCARD_HAS_VMIXER */
941 /* The card can manage substreams formed by analog and digital channels
942 at the same time, but I prefer to keep analog and digital channels
943 separated, because that mixed thing is confusing and useless. So we
944 register two PCM devices: */
946 /* PCM#0 Analog i/o */
947 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
948 num_analog_busses_out(chip),
949 num_analog_busses_in(chip), &pcm)) < 0)
950 return err;
951 pcm->private_data = chip;
952 chip->analog_pcm = pcm;
953 strcpy(pcm->name, chip->card->shortname);
954 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
955 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
956 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
957 return err;
959 #ifdef ECHOCARD_HAS_DIGITAL_IO
960 /* PCM#1 Digital i/o */
961 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
962 num_digital_busses_out(chip),
963 num_digital_busses_in(chip), &pcm)) < 0)
964 return err;
965 pcm->private_data = chip;
966 chip->digital_pcm = pcm;
967 strcpy(pcm->name, chip->card->shortname);
968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
969 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
970 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
971 return err;
972 #endif /* ECHOCARD_HAS_DIGITAL_IO */
974 #endif /* ECHOCARD_HAS_VMIXER */
976 return 0;
982 /******************************************************************************
983 Control interface
984 ******************************************************************************/
986 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
988 /******************* PCM output volume *******************/
989 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
990 struct snd_ctl_elem_info *uinfo)
992 struct echoaudio *chip;
994 chip = snd_kcontrol_chip(kcontrol);
995 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
996 uinfo->count = num_busses_out(chip);
997 uinfo->value.integer.min = ECHOGAIN_MINOUT;
998 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
999 return 0;
1002 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_value *ucontrol)
1005 struct echoaudio *chip;
1006 int c;
1008 chip = snd_kcontrol_chip(kcontrol);
1009 for (c = 0; c < num_busses_out(chip); c++)
1010 ucontrol->value.integer.value[c] = chip->output_gain[c];
1011 return 0;
1014 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1015 struct snd_ctl_elem_value *ucontrol)
1017 struct echoaudio *chip;
1018 int c, changed, gain;
1020 changed = 0;
1021 chip = snd_kcontrol_chip(kcontrol);
1022 spin_lock_irq(&chip->lock);
1023 for (c = 0; c < num_busses_out(chip); c++) {
1024 gain = ucontrol->value.integer.value[c];
1025 /* Ignore out of range values */
1026 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1027 continue;
1028 if (chip->output_gain[c] != gain) {
1029 set_output_gain(chip, c, gain);
1030 changed = 1;
1033 if (changed)
1034 update_output_line_level(chip);
1035 spin_unlock_irq(&chip->lock);
1036 return changed;
1039 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1040 /* On the Mia this one controls the line-out volume */
1041 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1042 .name = "Line Playback Volume",
1043 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1044 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1045 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1046 .info = snd_echo_output_gain_info,
1047 .get = snd_echo_output_gain_get,
1048 .put = snd_echo_output_gain_put,
1049 .tlv = {.p = db_scale_output_gain},
1051 #else
1052 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1053 .name = "PCM Playback Volume",
1054 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1055 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1056 .info = snd_echo_output_gain_info,
1057 .get = snd_echo_output_gain_get,
1058 .put = snd_echo_output_gain_put,
1059 .tlv = {.p = db_scale_output_gain},
1061 #endif
1063 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1067 #ifdef ECHOCARD_HAS_INPUT_GAIN
1069 /******************* Analog input volume *******************/
1070 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1071 struct snd_ctl_elem_info *uinfo)
1073 struct echoaudio *chip;
1075 chip = snd_kcontrol_chip(kcontrol);
1076 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1077 uinfo->count = num_analog_busses_in(chip);
1078 uinfo->value.integer.min = ECHOGAIN_MININP;
1079 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1080 return 0;
1083 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1086 struct echoaudio *chip;
1087 int c;
1089 chip = snd_kcontrol_chip(kcontrol);
1090 for (c = 0; c < num_analog_busses_in(chip); c++)
1091 ucontrol->value.integer.value[c] = chip->input_gain[c];
1092 return 0;
1095 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1098 struct echoaudio *chip;
1099 int c, gain, changed;
1101 changed = 0;
1102 chip = snd_kcontrol_chip(kcontrol);
1103 spin_lock_irq(&chip->lock);
1104 for (c = 0; c < num_analog_busses_in(chip); c++) {
1105 gain = ucontrol->value.integer.value[c];
1106 /* Ignore out of range values */
1107 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1108 continue;
1109 if (chip->input_gain[c] != gain) {
1110 set_input_gain(chip, c, gain);
1111 changed = 1;
1114 if (changed)
1115 update_input_line_level(chip);
1116 spin_unlock_irq(&chip->lock);
1117 return changed;
1120 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1122 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1123 .name = "Line Capture Volume",
1124 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1125 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1126 .info = snd_echo_input_gain_info,
1127 .get = snd_echo_input_gain_get,
1128 .put = snd_echo_input_gain_put,
1129 .tlv = {.p = db_scale_input_gain},
1132 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1136 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1138 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1139 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1140 struct snd_ctl_elem_info *uinfo)
1142 struct echoaudio *chip;
1144 chip = snd_kcontrol_chip(kcontrol);
1145 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1146 uinfo->count = num_analog_busses_out(chip);
1147 uinfo->value.integer.min = 0;
1148 uinfo->value.integer.max = 1;
1149 return 0;
1152 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1153 struct snd_ctl_elem_value *ucontrol)
1155 struct echoaudio *chip;
1156 int c;
1158 chip = snd_kcontrol_chip(kcontrol);
1159 for (c = 0; c < num_analog_busses_out(chip); c++)
1160 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1161 return 0;
1164 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1165 struct snd_ctl_elem_value *ucontrol)
1167 struct echoaudio *chip;
1168 int c, changed;
1170 changed = 0;
1171 chip = snd_kcontrol_chip(kcontrol);
1172 spin_lock_irq(&chip->lock);
1173 for (c = 0; c < num_analog_busses_out(chip); c++) {
1174 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1175 set_nominal_level(chip, c,
1176 ucontrol->value.integer.value[c]);
1177 changed = 1;
1180 if (changed)
1181 update_output_line_level(chip);
1182 spin_unlock_irq(&chip->lock);
1183 return changed;
1186 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1187 .name = "Line Playback Switch (-10dBV)",
1188 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1189 .info = snd_echo_output_nominal_info,
1190 .get = snd_echo_output_nominal_get,
1191 .put = snd_echo_output_nominal_put,
1194 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1198 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1200 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1201 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1202 struct snd_ctl_elem_info *uinfo)
1204 struct echoaudio *chip;
1206 chip = snd_kcontrol_chip(kcontrol);
1207 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1208 uinfo->count = num_analog_busses_in(chip);
1209 uinfo->value.integer.min = 0;
1210 uinfo->value.integer.max = 1;
1211 return 0;
1214 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1215 struct snd_ctl_elem_value *ucontrol)
1217 struct echoaudio *chip;
1218 int c;
1220 chip = snd_kcontrol_chip(kcontrol);
1221 for (c = 0; c < num_analog_busses_in(chip); c++)
1222 ucontrol->value.integer.value[c] =
1223 chip->nominal_level[bx_analog_in(chip) + c];
1224 return 0;
1227 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1228 struct snd_ctl_elem_value *ucontrol)
1230 struct echoaudio *chip;
1231 int c, changed;
1233 changed = 0;
1234 chip = snd_kcontrol_chip(kcontrol);
1235 spin_lock_irq(&chip->lock);
1236 for (c = 0; c < num_analog_busses_in(chip); c++) {
1237 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1238 ucontrol->value.integer.value[c]) {
1239 set_nominal_level(chip, bx_analog_in(chip) + c,
1240 ucontrol->value.integer.value[c]);
1241 changed = 1;
1244 if (changed)
1245 update_output_line_level(chip); /* "Output" is not a mistake
1246 * here.
1248 spin_unlock_irq(&chip->lock);
1249 return changed;
1252 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1253 .name = "Line Capture Switch (-10dBV)",
1254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1255 .info = snd_echo_input_nominal_info,
1256 .get = snd_echo_input_nominal_get,
1257 .put = snd_echo_input_nominal_put,
1260 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1264 #ifdef ECHOCARD_HAS_MONITOR
1266 /******************* Monitor mixer *******************/
1267 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1268 struct snd_ctl_elem_info *uinfo)
1270 struct echoaudio *chip;
1272 chip = snd_kcontrol_chip(kcontrol);
1273 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1274 uinfo->count = 1;
1275 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1276 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1277 uinfo->dimen.d[0] = num_busses_out(chip);
1278 uinfo->dimen.d[1] = num_busses_in(chip);
1279 return 0;
1282 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1283 struct snd_ctl_elem_value *ucontrol)
1285 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1286 unsigned int out = ucontrol->id.index / num_busses_in(chip);
1287 unsigned int in = ucontrol->id.index % num_busses_in(chip);
1289 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1290 return -EINVAL;
1292 ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1293 return 0;
1296 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1297 struct snd_ctl_elem_value *ucontrol)
1299 struct echoaudio *chip;
1300 int changed, gain;
1301 unsigned int out, in;
1303 changed = 0;
1304 chip = snd_kcontrol_chip(kcontrol);
1305 out = ucontrol->id.index / num_busses_in(chip);
1306 in = ucontrol->id.index % num_busses_in(chip);
1307 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1308 return -EINVAL;
1309 gain = ucontrol->value.integer.value[0];
1310 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1311 return -EINVAL;
1312 if (chip->monitor_gain[out][in] != gain) {
1313 spin_lock_irq(&chip->lock);
1314 set_monitor_gain(chip, out, in, gain);
1315 update_output_line_level(chip);
1316 spin_unlock_irq(&chip->lock);
1317 changed = 1;
1319 return changed;
1322 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1323 .name = "Monitor Mixer Volume",
1324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1325 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1326 .info = snd_echo_mixer_info,
1327 .get = snd_echo_mixer_get,
1328 .put = snd_echo_mixer_put,
1329 .tlv = {.p = db_scale_output_gain},
1332 #endif /* ECHOCARD_HAS_MONITOR */
1336 #ifdef ECHOCARD_HAS_VMIXER
1338 /******************* Vmixer *******************/
1339 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_info *uinfo)
1342 struct echoaudio *chip;
1344 chip = snd_kcontrol_chip(kcontrol);
1345 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1346 uinfo->count = 1;
1347 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1348 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1349 uinfo->dimen.d[0] = num_busses_out(chip);
1350 uinfo->dimen.d[1] = num_pipes_out(chip);
1351 return 0;
1354 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
1357 struct echoaudio *chip;
1359 chip = snd_kcontrol_chip(kcontrol);
1360 ucontrol->value.integer.value[0] =
1361 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1362 [ucontrol->id.index % num_pipes_out(chip)];
1363 return 0;
1366 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1367 struct snd_ctl_elem_value *ucontrol)
1369 struct echoaudio *chip;
1370 int gain, changed;
1371 short vch, out;
1373 changed = 0;
1374 chip = snd_kcontrol_chip(kcontrol);
1375 out = ucontrol->id.index / num_pipes_out(chip);
1376 vch = ucontrol->id.index % num_pipes_out(chip);
1377 gain = ucontrol->value.integer.value[0];
1378 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1379 return -EINVAL;
1380 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1381 spin_lock_irq(&chip->lock);
1382 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1383 update_vmixer_level(chip);
1384 spin_unlock_irq(&chip->lock);
1385 changed = 1;
1387 return changed;
1390 static struct snd_kcontrol_new snd_echo_vmixer = {
1391 .name = "VMixer Volume",
1392 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1393 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1394 .info = snd_echo_vmixer_info,
1395 .get = snd_echo_vmixer_get,
1396 .put = snd_echo_vmixer_put,
1397 .tlv = {.p = db_scale_output_gain},
1400 #endif /* ECHOCARD_HAS_VMIXER */
1404 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1406 /******************* Digital mode switch *******************/
1407 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_info *uinfo)
1410 static const char * const names[4] = {
1411 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1412 "S/PDIF Cdrom"
1414 struct echoaudio *chip;
1416 chip = snd_kcontrol_chip(kcontrol);
1417 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1420 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_value *ucontrol)
1423 struct echoaudio *chip;
1424 int i, mode;
1426 chip = snd_kcontrol_chip(kcontrol);
1427 mode = chip->digital_mode;
1428 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1429 if (mode == chip->digital_mode_list[i]) {
1430 ucontrol->value.enumerated.item[0] = i;
1431 break;
1433 return 0;
1436 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
1439 struct echoaudio *chip;
1440 int changed;
1441 unsigned short emode, dmode;
1443 changed = 0;
1444 chip = snd_kcontrol_chip(kcontrol);
1446 emode = ucontrol->value.enumerated.item[0];
1447 if (emode >= chip->num_digital_modes)
1448 return -EINVAL;
1449 dmode = chip->digital_mode_list[emode];
1451 if (dmode != chip->digital_mode) {
1452 /* mode_mutex is required to make this operation atomic wrt
1453 pcm_digital_*_open() and set_input_clock() functions. */
1454 mutex_lock(&chip->mode_mutex);
1456 /* Do not allow the user to change the digital mode when a pcm
1457 device is open because it also changes the number of channels
1458 and the allowed sample rates */
1459 if (atomic_read(&chip->opencount)) {
1460 changed = -EAGAIN;
1461 } else {
1462 changed = set_digital_mode(chip, dmode);
1463 /* If we had to change the clock source, report it */
1464 if (changed > 0 && chip->clock_src_ctl) {
1465 snd_ctl_notify(chip->card,
1466 SNDRV_CTL_EVENT_MASK_VALUE,
1467 &chip->clock_src_ctl->id);
1468 dev_dbg(chip->card->dev,
1469 "SDM() =%d\n", changed);
1471 if (changed >= 0)
1472 changed = 1; /* No errors */
1474 mutex_unlock(&chip->mode_mutex);
1476 return changed;
1479 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1480 .name = "Digital mode Switch",
1481 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1482 .info = snd_echo_digital_mode_info,
1483 .get = snd_echo_digital_mode_get,
1484 .put = snd_echo_digital_mode_put,
1487 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1491 #ifdef ECHOCARD_HAS_DIGITAL_IO
1493 /******************* S/PDIF mode switch *******************/
1494 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1497 static const char * const names[2] = {"Consumer", "Professional"};
1499 return snd_ctl_enum_info(uinfo, 1, 2, names);
1502 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_value *ucontrol)
1505 struct echoaudio *chip;
1507 chip = snd_kcontrol_chip(kcontrol);
1508 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1509 return 0;
1512 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1515 struct echoaudio *chip;
1516 int mode;
1518 chip = snd_kcontrol_chip(kcontrol);
1519 mode = !!ucontrol->value.enumerated.item[0];
1520 if (mode != chip->professional_spdif) {
1521 spin_lock_irq(&chip->lock);
1522 set_professional_spdif(chip, mode);
1523 spin_unlock_irq(&chip->lock);
1524 return 1;
1526 return 0;
1529 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1530 .name = "S/PDIF mode Switch",
1531 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1532 .info = snd_echo_spdif_mode_info,
1533 .get = snd_echo_spdif_mode_get,
1534 .put = snd_echo_spdif_mode_put,
1537 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1541 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1543 /******************* Select input clock source *******************/
1544 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_info *uinfo)
1547 static const char * const names[8] = {
1548 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1549 "ESync96", "MTC"
1551 struct echoaudio *chip;
1553 chip = snd_kcontrol_chip(kcontrol);
1554 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1557 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1558 struct snd_ctl_elem_value *ucontrol)
1560 struct echoaudio *chip;
1561 int i, clock;
1563 chip = snd_kcontrol_chip(kcontrol);
1564 clock = chip->input_clock;
1566 for (i = 0; i < chip->num_clock_sources; i++)
1567 if (clock == chip->clock_source_list[i])
1568 ucontrol->value.enumerated.item[0] = i;
1570 return 0;
1573 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1574 struct snd_ctl_elem_value *ucontrol)
1576 struct echoaudio *chip;
1577 int changed;
1578 unsigned int eclock, dclock;
1580 changed = 0;
1581 chip = snd_kcontrol_chip(kcontrol);
1582 eclock = ucontrol->value.enumerated.item[0];
1583 if (eclock >= chip->input_clock_types)
1584 return -EINVAL;
1585 dclock = chip->clock_source_list[eclock];
1586 if (chip->input_clock != dclock) {
1587 mutex_lock(&chip->mode_mutex);
1588 spin_lock_irq(&chip->lock);
1589 if ((changed = set_input_clock(chip, dclock)) == 0)
1590 changed = 1; /* no errors */
1591 spin_unlock_irq(&chip->lock);
1592 mutex_unlock(&chip->mode_mutex);
1595 if (changed < 0)
1596 dev_dbg(chip->card->dev,
1597 "seticlk val%d err 0x%x\n", dclock, changed);
1599 return changed;
1602 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1603 .name = "Sample Clock Source",
1604 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1605 .info = snd_echo_clock_source_info,
1606 .get = snd_echo_clock_source_get,
1607 .put = snd_echo_clock_source_put,
1610 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1614 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1616 /******************* Phantom power switch *******************/
1617 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1619 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1620 struct snd_ctl_elem_value *ucontrol)
1622 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1624 ucontrol->value.integer.value[0] = chip->phantom_power;
1625 return 0;
1628 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_value *ucontrol)
1631 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632 int power, changed = 0;
1634 power = !!ucontrol->value.integer.value[0];
1635 if (chip->phantom_power != power) {
1636 spin_lock_irq(&chip->lock);
1637 changed = set_phantom_power(chip, power);
1638 spin_unlock_irq(&chip->lock);
1639 if (changed == 0)
1640 changed = 1; /* no errors */
1642 return changed;
1645 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1646 .name = "Phantom power Switch",
1647 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1648 .info = snd_echo_phantom_power_info,
1649 .get = snd_echo_phantom_power_get,
1650 .put = snd_echo_phantom_power_put,
1653 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1657 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1659 /******************* Digital input automute switch *******************/
1660 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1662 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol)
1665 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1667 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1668 return 0;
1671 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol)
1674 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1675 int automute, changed = 0;
1677 automute = !!ucontrol->value.integer.value[0];
1678 if (chip->digital_in_automute != automute) {
1679 spin_lock_irq(&chip->lock);
1680 changed = set_input_auto_mute(chip, automute);
1681 spin_unlock_irq(&chip->lock);
1682 if (changed == 0)
1683 changed = 1; /* no errors */
1685 return changed;
1688 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1689 .name = "Digital Capture Switch (automute)",
1690 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1691 .info = snd_echo_automute_info,
1692 .get = snd_echo_automute_get,
1693 .put = snd_echo_automute_put,
1696 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1700 /******************* VU-meters switch *******************/
1701 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1703 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol)
1706 struct echoaudio *chip;
1708 chip = snd_kcontrol_chip(kcontrol);
1709 spin_lock_irq(&chip->lock);
1710 set_meters_on(chip, ucontrol->value.integer.value[0]);
1711 spin_unlock_irq(&chip->lock);
1712 return 1;
1715 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1716 .name = "VU-meters Switch",
1717 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1718 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1719 .info = snd_echo_vumeters_switch_info,
1720 .put = snd_echo_vumeters_switch_put,
1725 /***** Read VU-meters (input, output, analog and digital together) *****/
1726 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_info *uinfo)
1729 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1730 uinfo->count = 96;
1731 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1732 uinfo->value.integer.max = 0;
1733 #ifdef ECHOCARD_HAS_VMIXER
1734 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1735 #else
1736 uinfo->dimen.d[0] = 2; /* Out, In */
1737 #endif
1738 uinfo->dimen.d[1] = 16; /* 16 channels */
1739 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1740 return 0;
1743 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
1746 struct echoaudio *chip;
1748 chip = snd_kcontrol_chip(kcontrol);
1749 get_audio_meters(chip, ucontrol->value.integer.value);
1750 return 0;
1753 static const struct snd_kcontrol_new snd_echo_vumeters = {
1754 .name = "VU-meters",
1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1756 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1757 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1758 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1759 .info = snd_echo_vumeters_info,
1760 .get = snd_echo_vumeters_get,
1761 .tlv = {.p = db_scale_output_gain},
1766 /*** Channels info - it exports informations about the number of channels ***/
1767 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1768 struct snd_ctl_elem_info *uinfo)
1770 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1771 uinfo->count = 6;
1772 uinfo->value.integer.min = 0;
1773 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1774 return 0;
1777 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
1780 struct echoaudio *chip;
1781 int detected, clocks, bit, src;
1783 chip = snd_kcontrol_chip(kcontrol);
1784 ucontrol->value.integer.value[0] = num_busses_in(chip);
1785 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1786 ucontrol->value.integer.value[2] = num_busses_out(chip);
1787 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1788 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1790 /* Compute the bitmask of the currently valid input clocks */
1791 detected = detect_input_clocks(chip);
1792 clocks = 0;
1793 src = chip->num_clock_sources - 1;
1794 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1795 if (detected & (1 << bit))
1796 for (; src >= 0; src--)
1797 if (bit == chip->clock_source_list[src]) {
1798 clocks |= 1 << src;
1799 break;
1801 ucontrol->value.integer.value[5] = clocks;
1803 return 0;
1806 static const struct snd_kcontrol_new snd_echo_channels_info = {
1807 .name = "Channels info",
1808 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1809 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1810 .info = snd_echo_channels_info_info,
1811 .get = snd_echo_channels_info_get,
1817 /******************************************************************************
1818 IRQ Handler
1819 ******************************************************************************/
1821 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1823 struct echoaudio *chip = dev_id;
1824 struct snd_pcm_substream *substream;
1825 int period, ss, st;
1827 spin_lock(&chip->lock);
1828 st = service_irq(chip);
1829 if (st < 0) {
1830 spin_unlock(&chip->lock);
1831 return IRQ_NONE;
1833 /* The hardware doesn't tell us which substream caused the irq,
1834 thus we have to check all running substreams. */
1835 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1836 substream = chip->substream[ss];
1837 if (substream && ((struct audiopipe *)substream->runtime->
1838 private_data)->state == PIPE_STATE_STARTED) {
1839 period = pcm_pointer(substream) /
1840 substream->runtime->period_size;
1841 if (period != chip->last_period[ss]) {
1842 chip->last_period[ss] = period;
1843 spin_unlock(&chip->lock);
1844 snd_pcm_period_elapsed(substream);
1845 spin_lock(&chip->lock);
1849 spin_unlock(&chip->lock);
1851 #ifdef ECHOCARD_HAS_MIDI
1852 if (st > 0 && chip->midi_in) {
1853 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1854 dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1856 #endif
1857 return IRQ_HANDLED;
1863 /******************************************************************************
1864 Module construction / destruction
1865 ******************************************************************************/
1867 static int snd_echo_free(struct echoaudio *chip)
1869 if (chip->comm_page)
1870 rest_in_peace(chip);
1872 if (chip->irq >= 0)
1873 free_irq(chip->irq, chip);
1875 if (chip->comm_page)
1876 snd_dma_free_pages(&chip->commpage_dma_buf);
1878 iounmap(chip->dsp_registers);
1879 release_and_free_resource(chip->iores);
1880 pci_disable_device(chip->pci);
1882 /* release chip data */
1883 free_firmware_cache(chip);
1884 kfree(chip);
1885 return 0;
1890 static int snd_echo_dev_free(struct snd_device *device)
1892 struct echoaudio *chip = device->device_data;
1894 return snd_echo_free(chip);
1899 /* <--snd_echo_probe() */
1900 static int snd_echo_create(struct snd_card *card,
1901 struct pci_dev *pci,
1902 struct echoaudio **rchip)
1904 struct echoaudio *chip;
1905 int err;
1906 size_t sz;
1907 static struct snd_device_ops ops = {
1908 .dev_free = snd_echo_dev_free,
1911 *rchip = NULL;
1913 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1915 if ((err = pci_enable_device(pci)) < 0)
1916 return err;
1917 pci_set_master(pci);
1919 /* Allocate chip if needed */
1920 if (!*rchip) {
1921 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1922 if (!chip) {
1923 pci_disable_device(pci);
1924 return -ENOMEM;
1926 dev_dbg(card->dev, "chip=%p\n", chip);
1927 spin_lock_init(&chip->lock);
1928 chip->card = card;
1929 chip->pci = pci;
1930 chip->irq = -1;
1931 atomic_set(&chip->opencount, 0);
1932 mutex_init(&chip->mode_mutex);
1933 chip->can_set_rate = 1;
1934 } else {
1935 /* If this was called from the resume function, chip is
1936 * already allocated and it contains current card settings.
1938 chip = *rchip;
1941 /* PCI resource allocation */
1942 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1943 sz = pci_resource_len(pci, 0);
1944 if (sz > PAGE_SIZE)
1945 sz = PAGE_SIZE; /* We map only the required part */
1947 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1948 ECHOCARD_NAME)) == NULL) {
1949 dev_err(chip->card->dev, "cannot get memory region\n");
1950 snd_echo_free(chip);
1951 return -EBUSY;
1953 chip->dsp_registers = (volatile u32 __iomem *)
1954 ioremap_nocache(chip->dsp_registers_phys, sz);
1956 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1957 KBUILD_MODNAME, chip)) {
1958 dev_err(chip->card->dev, "cannot grab irq\n");
1959 snd_echo_free(chip);
1960 return -EBUSY;
1962 chip->irq = pci->irq;
1963 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1964 chip->pci, chip->irq, chip->pci->subsystem_device);
1966 /* Create the DSP comm page - this is the area of memory used for most
1967 of the communication with the DSP, which accesses it via bus mastering */
1968 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1969 sizeof(struct comm_page),
1970 &chip->commpage_dma_buf) < 0) {
1971 dev_err(chip->card->dev, "cannot allocate the comm page\n");
1972 snd_echo_free(chip);
1973 return -ENOMEM;
1975 chip->comm_page_phys = chip->commpage_dma_buf.addr;
1976 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1978 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1979 if (err >= 0)
1980 err = set_mixer_defaults(chip);
1981 if (err < 0) {
1982 dev_err(card->dev, "init_hw err=%d\n", err);
1983 snd_echo_free(chip);
1984 return err;
1987 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1988 snd_echo_free(chip);
1989 return err;
1991 *rchip = chip;
1992 /* Init done ! */
1993 return 0;
1998 /* constructor */
1999 static int snd_echo_probe(struct pci_dev *pci,
2000 const struct pci_device_id *pci_id)
2002 static int dev;
2003 struct snd_card *card;
2004 struct echoaudio *chip;
2005 char *dsp;
2006 int i, err;
2008 if (dev >= SNDRV_CARDS)
2009 return -ENODEV;
2010 if (!enable[dev]) {
2011 dev++;
2012 return -ENOENT;
2015 i = 0;
2016 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2017 0, &card);
2018 if (err < 0)
2019 return err;
2021 chip = NULL; /* Tells snd_echo_create to allocate chip */
2022 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2023 snd_card_free(card);
2024 return err;
2027 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2028 strcpy(card->shortname, chip->card_name);
2030 dsp = "56301";
2031 if (pci_id->device == 0x3410)
2032 dsp = "56361";
2034 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2035 card->shortname, pci_id->subdevice & 0x000f, dsp,
2036 chip->dsp_registers_phys, chip->irq);
2038 if ((err = snd_echo_new_pcm(chip)) < 0) {
2039 dev_err(chip->card->dev, "new pcm error %d\n", err);
2040 snd_card_free(card);
2041 return err;
2044 #ifdef ECHOCARD_HAS_MIDI
2045 if (chip->has_midi) { /* Some Mia's do not have midi */
2046 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2047 dev_err(chip->card->dev, "new midi error %d\n", err);
2048 snd_card_free(card);
2049 return err;
2052 #endif
2054 #ifdef ECHOCARD_HAS_VMIXER
2055 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2056 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2057 goto ctl_error;
2058 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2059 err = snd_ctl_add(chip->card,
2060 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2061 if (err < 0)
2062 goto ctl_error;
2063 #endif
2064 #else /* ECHOCARD_HAS_VMIXER */
2065 err = snd_ctl_add(chip->card,
2066 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2067 if (err < 0)
2068 goto ctl_error;
2069 #endif /* ECHOCARD_HAS_VMIXER */
2071 #ifdef ECHOCARD_HAS_INPUT_GAIN
2072 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2073 goto ctl_error;
2074 #endif
2076 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2077 if (!chip->hasnt_input_nominal_level)
2078 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2079 goto ctl_error;
2080 #endif
2082 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2083 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2084 goto ctl_error;
2085 #endif
2087 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2088 goto ctl_error;
2090 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2091 goto ctl_error;
2093 #ifdef ECHOCARD_HAS_MONITOR
2094 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2095 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2096 goto ctl_error;
2097 #endif
2099 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2100 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2101 goto ctl_error;
2102 #endif
2104 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2105 goto ctl_error;
2107 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2108 /* Creates a list of available digital modes */
2109 chip->num_digital_modes = 0;
2110 for (i = 0; i < 6; i++)
2111 if (chip->digital_modes & (1 << i))
2112 chip->digital_mode_list[chip->num_digital_modes++] = i;
2114 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2115 goto ctl_error;
2116 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2118 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2119 /* Creates a list of available clock sources */
2120 chip->num_clock_sources = 0;
2121 for (i = 0; i < 10; i++)
2122 if (chip->input_clock_types & (1 << i))
2123 chip->clock_source_list[chip->num_clock_sources++] = i;
2125 if (chip->num_clock_sources > 1) {
2126 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2127 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2128 goto ctl_error;
2130 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2132 #ifdef ECHOCARD_HAS_DIGITAL_IO
2133 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2134 goto ctl_error;
2135 #endif
2137 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2138 if (chip->has_phantom_power)
2139 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2140 goto ctl_error;
2141 #endif
2143 err = snd_card_register(card);
2144 if (err < 0)
2145 goto ctl_error;
2146 dev_info(card->dev, "Card registered: %s\n", card->longname);
2148 pci_set_drvdata(pci, chip);
2149 dev++;
2150 return 0;
2152 ctl_error:
2153 dev_err(card->dev, "new control error %d\n", err);
2154 snd_card_free(card);
2155 return err;
2160 #if defined(CONFIG_PM_SLEEP)
2162 static int snd_echo_suspend(struct device *dev)
2164 struct echoaudio *chip = dev_get_drvdata(dev);
2166 snd_pcm_suspend_all(chip->analog_pcm);
2167 snd_pcm_suspend_all(chip->digital_pcm);
2169 #ifdef ECHOCARD_HAS_MIDI
2170 /* This call can sleep */
2171 if (chip->midi_out)
2172 snd_echo_midi_output_trigger(chip->midi_out, 0);
2173 #endif
2174 spin_lock_irq(&chip->lock);
2175 if (wait_handshake(chip)) {
2176 spin_unlock_irq(&chip->lock);
2177 return -EIO;
2179 clear_handshake(chip);
2180 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2181 spin_unlock_irq(&chip->lock);
2182 return -EIO;
2184 spin_unlock_irq(&chip->lock);
2186 chip->dsp_code = NULL;
2187 free_irq(chip->irq, chip);
2188 chip->irq = -1;
2189 return 0;
2194 static int snd_echo_resume(struct device *dev)
2196 struct pci_dev *pci = to_pci_dev(dev);
2197 struct echoaudio *chip = dev_get_drvdata(dev);
2198 struct comm_page *commpage, *commpage_bak;
2199 u32 pipe_alloc_mask;
2200 int err;
2202 commpage_bak = kmalloc(sizeof(*commpage), GFP_KERNEL);
2203 if (commpage_bak == NULL)
2204 return -ENOMEM;
2205 commpage = chip->comm_page;
2206 memcpy(commpage_bak, commpage, sizeof(*commpage));
2208 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2209 if (err < 0) {
2210 kfree(commpage_bak);
2211 dev_err(dev, "resume init_hw err=%d\n", err);
2212 snd_echo_free(chip);
2213 return err;
2216 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2217 * restore_dsp_settings() fails.
2219 pipe_alloc_mask = chip->pipe_alloc_mask;
2220 chip->pipe_alloc_mask = 0;
2221 err = restore_dsp_rettings(chip);
2222 chip->pipe_alloc_mask = pipe_alloc_mask;
2223 if (err < 0) {
2224 kfree(commpage_bak);
2225 return err;
2228 memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2229 sizeof(commpage->audio_format));
2230 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2231 sizeof(commpage->sglist_addr));
2232 memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2233 sizeof(commpage->midi_output));
2234 kfree(commpage_bak);
2236 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2237 KBUILD_MODNAME, chip)) {
2238 dev_err(chip->card->dev, "cannot grab irq\n");
2239 snd_echo_free(chip);
2240 return -EBUSY;
2242 chip->irq = pci->irq;
2243 dev_dbg(dev, "resume irq=%d\n", chip->irq);
2245 #ifdef ECHOCARD_HAS_MIDI
2246 if (chip->midi_input_enabled)
2247 enable_midi_input(chip, true);
2248 if (chip->midi_out)
2249 snd_echo_midi_output_trigger(chip->midi_out, 1);
2250 #endif
2252 return 0;
2255 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2256 #define SND_ECHO_PM_OPS &snd_echo_pm
2257 #else
2258 #define SND_ECHO_PM_OPS NULL
2259 #endif /* CONFIG_PM_SLEEP */
2262 static void snd_echo_remove(struct pci_dev *pci)
2264 struct echoaudio *chip;
2266 chip = pci_get_drvdata(pci);
2267 if (chip)
2268 snd_card_free(chip->card);
2273 /******************************************************************************
2274 Everything starts and ends here
2275 ******************************************************************************/
2277 /* pci_driver definition */
2278 static struct pci_driver echo_driver = {
2279 .name = KBUILD_MODNAME,
2280 .id_table = snd_echo_ids,
2281 .probe = snd_echo_probe,
2282 .remove = snd_echo_remove,
2283 .driver = {
2284 .pm = SND_ECHO_PM_OPS,
2288 module_pci_driver(echo_driver);