xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision e8e0929d)
1 /*
2  *  ALSA driver for Echoaudio soundcards.
3  *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
4  *
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.
8  *
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.
13  *
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.
17  */
18 
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);
24 
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;
28 
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.");
35 
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);
38 
39 static int get_firmware(const struct firmware **fw_entry,
40 			const struct firmware *frm, struct echoaudio *chip)
41 {
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;
49 }
50 
51 static void free_firmware(const struct firmware *fw_entry)
52 {
53 	release_firmware(fw_entry);
54 	DE_ACT(("firmware released\n"));
55 }
56 
57 
58 
59 /******************************************************************************
60 	PCM interface
61 ******************************************************************************/
62 
63 static void audiopipe_free(struct snd_pcm_runtime *runtime)
64 {
65 	struct audiopipe *pipe = runtime->private_data;
66 
67 	if (pipe->sgpage.area)
68 		snd_dma_free_pages(&pipe->sgpage);
69 	kfree(pipe);
70 }
71 
72 
73 
74 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
75 					      struct snd_pcm_hw_rule *rule)
76 {
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;
81 
82 	snd_mask_any(&fmt);
83 
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);
89 	}
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);
95 	}
96 	/* Mono is ok with any format */
97 	return 0;
98 }
99 
100 
101 
102 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
103 					      struct snd_pcm_hw_rule *rule)
104 {
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;
109 
110 	snd_interval_any(&ch);
111 
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);
122 	}
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);
129 	}
130 	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
131 	return 0;
132 }
133 
134 
135 
136 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
137 					       struct snd_pcm_hw_rule *rule)
138 {
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);
145 
146 	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
147 
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;
163 
164 	fmt.bits[0] &= (u32)fmask;
165 	fmt.bits[1] &= (u32)(fmask >> 32);
166 	return snd_mask_refine(f, &fmt);
167 }
168 
169 
170 
171 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
172 					       struct snd_pcm_hw_rule *rule)
173 {
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;
179 
180 	snd_interval_any(&ch);
181 	ch.integer = 1;
182 	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
183 
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;
201 
202 	return snd_interval_refine(c, &ch);
203 }
204 
205 
206 
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)
211 {
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;
216 
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);
221 	}
222 	return 0;
223 }
224 
225 
226 static int pcm_open(struct snd_pcm_substream *substream,
227 		    signed char max_channels)
228 {
229 	struct echoaudio *chip;
230 	struct snd_pcm_runtime *runtime;
231 	struct audiopipe *pipe;
232 	int err, i;
233 
234 	if (max_channels <= 0)
235 		return -EAGAIN;
236 
237 	chip = snd_pcm_substream_chip(substream);
238 	runtime = substream->runtime;
239 
240 	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
241 	if (!pipe)
242 		return -ENOMEM;
243 	pipe->index = -1;		/* Not configured yet */
244 
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;
257 	}
258 
259 	runtime->hw = pipe->hw;
260 	runtime->private_data = pipe;
261 	runtime->private_free = audiopipe_free;
262 	snd_pcm_set_sync(substream);
263 
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;
269 
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;
274 
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;
286 
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;
292 
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;
299 	}
300 
301 	return 0;
302 }
303 
304 
305 
306 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
307 {
308 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
309 	int err;
310 
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;
332 }
333 
334 
335 
336 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
337 {
338 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
339 	int max_channels, err;
340 
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;
368 }
369 
370 
371 
372 #ifdef ECHOCARD_HAS_DIGITAL_IO
373 
374 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
375 {
376 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
377 	int err, max_channels;
378 
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
386 		 */
387 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
388 
389 	if (err < 0)
390 		goto din_exit;
391 
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;
402 
403 	atomic_inc(&chip->opencount);
404 	if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
405 		chip->can_set_rate=0;
406 
407 din_exit:
408 	mutex_unlock(&chip->mode_mutex);
409 	return err;
410 }
411 
412 
413 
414 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
415 
416 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
417 {
418 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
419 	int err, max_channels;
420 
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
428 		 */
429 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
430 
431 	if (err < 0)
432 		goto dout_exit;
433 
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;
452 }
453 
454 #endif /* !ECHOCARD_HAS_VMIXER */
455 
456 #endif /* ECHOCARD_HAS_DIGITAL_IO */
457 
458 
459 
460 static int pcm_close(struct snd_pcm_substream *substream)
461 {
462 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
463 	int oc;
464 
465 	/* Nothing to do here. Audio is already off and pipe will be
466 	 * freed by its callback
467 	 */
468 	DE_ACT(("pcm_close\n"));
469 
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));
480 
481 	return 0;
482 }
483 
484 
485 
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)
490 {
491 	struct echoaudio *chip;
492 	int err, per, rest, page, edge, offs;
493 	struct audiopipe *pipe;
494 
495 	chip = snd_pcm_substream_chip(substream);
496 	pipe = (struct audiopipe *) substream->runtime->private_data;
497 
498 	/* Sets up che hardware. If it's already initialized, reset and
499 	 * redo with the new parameters
500 	 */
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;
507 	}
508 
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;
515 	}
516 	spin_unlock_irq(&chip->lock);
517 	DE_ACT((KERN_NOTICE "allocate_pipes()=%d\n", pipe_index));
518 
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;
531 	}
532 
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;
553 			}
554 			if (offs == edge) {
555 				edge += PAGE_SIZE;
556 				page++;
557 			}
558 		}
559 	}
560 
561 	/* Close the ring buffer */
562 	sglist_wrap(chip, pipe);
563 
564 	/* This stuff is used by the irq handler, so it must be
565 	 * initialized before chip->substream
566 	 */
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;
578 }
579 
580 
581 
582 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
583 				   struct snd_pcm_hw_params *hw_params)
584 {
585 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
586 
587 	return init_engine(substream, hw_params, px_analog_in(chip) +
588 			substream->number, params_channels(hw_params));
589 }
590 
591 
592 
593 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
594 				    struct snd_pcm_hw_params *hw_params)
595 {
596 	return init_engine(substream, hw_params, substream->number,
597 			   params_channels(hw_params));
598 }
599 
600 
601 
602 #ifdef ECHOCARD_HAS_DIGITAL_IO
603 
604 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
605 				    struct snd_pcm_hw_params *hw_params)
606 {
607 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
608 
609 	return init_engine(substream, hw_params, px_digital_in(chip) +
610 			substream->number, params_channels(hw_params));
611 }
612 
613 
614 
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)
618 {
619 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
620 
621 	return init_engine(substream, hw_params, px_digital_out(chip) +
622 			substream->number, params_channels(hw_params));
623 }
624 #endif /* !ECHOCARD_HAS_VMIXER */
625 
626 #endif /* ECHOCARD_HAS_DIGITAL_IO */
627 
628 
629 
630 static int pcm_hw_free(struct snd_pcm_substream *substream)
631 {
632 	struct echoaudio *chip;
633 	struct audiopipe *pipe;
634 
635 	chip = snd_pcm_substream_chip(substream);
636 	pipe = (struct audiopipe *) substream->runtime->private_data;
637 
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;
644 	}
645 	spin_unlock_irq(&chip->lock);
646 
647 	DE_HWP(("pcm_hw_freed\n"));
648 	snd_pcm_lib_free_pages(substream);
649 	return 0;
650 }
651 
652 
653 
654 static int pcm_prepare(struct snd_pcm_substream *substream)
655 {
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;
660 
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;
685 	}
686 
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;
693 }
694 
695 
696 
697 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
698 {
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;
705 
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);
711 			}
712 		}
713 	}
714 
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;
734 				}
735 			}
736 		}
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;
746 			}
747 		}
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;
756 			}
757 		}
758 		err = pause_transport(chip, channelmask);
759 		break;
760 	default:
761 		err = -EINVAL;
762 	}
763 	spin_unlock(&chip->lock);
764 	return err;
765 }
766 
767 
768 
769 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
770 {
771 	struct snd_pcm_runtime *runtime = substream->runtime;
772 	struct audiopipe *pipe = runtime->private_data;
773 	size_t cnt, bufsize, pos;
774 
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);
780 
781 	while (pos >= bufsize) {
782 		pipe->position -= frames_to_bytes(substream->runtime, bufsize);
783 		pos -= bufsize;
784 	}
785 	return pos;
786 }
787 
788 
789 
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,
801 };
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,
812 };
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,
825 };
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,
837 };
838 #endif /* ECHOCARD_HAS_DIGITAL_IO */
839 
840 
841 
842 /* Preallocate memory only for the first substream because it's the most
843  * used one
844  */
845 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
846 {
847 	struct snd_pcm_substream *ss;
848 	int stream, err;
849 
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;
858 		}
859 	return 0;
860 }
861 
862 
863 
864 /*<--snd_echo_probe() */
865 static int __devinit snd_echo_new_pcm(struct echoaudio *chip)
866 {
867 	struct snd_pcm *pcm;
868 	int err;
869 
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 */
876 
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"));
889 
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 */
903 
904 #else /* ECHOCARD_HAS_VMIXER */
905 
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: */
910 
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"));
924 
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 */
940 
941 #endif /* ECHOCARD_HAS_VMIXER */
942 
943 	return 0;
944 }
945 
946 
947 
948 
949 /******************************************************************************
950 	Control interface
951 ******************************************************************************/
952 
953 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
954 
955 /******************* PCM output volume *******************/
956 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
957 				     struct snd_ctl_elem_info *uinfo)
958 {
959 	struct echoaudio *chip;
960 
961 	chip = snd_kcontrol_chip(kcontrol);
962 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
963 	uinfo->count = num_busses_out(chip);
964 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
965 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
966 	return 0;
967 }
968 
969 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
970 				    struct snd_ctl_elem_value *ucontrol)
971 {
972 	struct echoaudio *chip;
973 	int c;
974 
975 	chip = snd_kcontrol_chip(kcontrol);
976 	for (c = 0; c < num_busses_out(chip); c++)
977 		ucontrol->value.integer.value[c] = chip->output_gain[c];
978 	return 0;
979 }
980 
981 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
982 				    struct snd_ctl_elem_value *ucontrol)
983 {
984 	struct echoaudio *chip;
985 	int c, changed, gain;
986 
987 	changed = 0;
988 	chip = snd_kcontrol_chip(kcontrol);
989 	spin_lock_irq(&chip->lock);
990 	for (c = 0; c < num_busses_out(chip); c++) {
991 		gain = ucontrol->value.integer.value[c];
992 		/* Ignore out of range values */
993 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
994 			continue;
995 		if (chip->output_gain[c] != gain) {
996 			set_output_gain(chip, c, gain);
997 			changed = 1;
998 		}
999 	}
1000 	if (changed)
1001 		update_output_line_level(chip);
1002 	spin_unlock_irq(&chip->lock);
1003 	return changed;
1004 }
1005 
1006 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1007 /* On the Mia this one controls the line-out volume */
1008 static struct snd_kcontrol_new snd_echo_line_output_gain __devinitdata = {
1009 	.name = "Line Playback Volume",
1010 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1011 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1012 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1013 	.info = snd_echo_output_gain_info,
1014 	.get = snd_echo_output_gain_get,
1015 	.put = snd_echo_output_gain_put,
1016 	.tlv = {.p = db_scale_output_gain},
1017 };
1018 #else
1019 static struct snd_kcontrol_new snd_echo_pcm_output_gain __devinitdata = {
1020 	.name = "PCM Playback Volume",
1021 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1022 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1023 	.info = snd_echo_output_gain_info,
1024 	.get = snd_echo_output_gain_get,
1025 	.put = snd_echo_output_gain_put,
1026 	.tlv = {.p = db_scale_output_gain},
1027 };
1028 #endif
1029 
1030 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1031 
1032 
1033 
1034 #ifdef ECHOCARD_HAS_INPUT_GAIN
1035 
1036 /******************* Analog input volume *******************/
1037 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1038 				    struct snd_ctl_elem_info *uinfo)
1039 {
1040 	struct echoaudio *chip;
1041 
1042 	chip = snd_kcontrol_chip(kcontrol);
1043 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1044 	uinfo->count = num_analog_busses_in(chip);
1045 	uinfo->value.integer.min = ECHOGAIN_MININP;
1046 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1047 	return 0;
1048 }
1049 
1050 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1051 				   struct snd_ctl_elem_value *ucontrol)
1052 {
1053 	struct echoaudio *chip;
1054 	int c;
1055 
1056 	chip = snd_kcontrol_chip(kcontrol);
1057 	for (c = 0; c < num_analog_busses_in(chip); c++)
1058 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1059 	return 0;
1060 }
1061 
1062 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1063 				   struct snd_ctl_elem_value *ucontrol)
1064 {
1065 	struct echoaudio *chip;
1066 	int c, gain, changed;
1067 
1068 	changed = 0;
1069 	chip = snd_kcontrol_chip(kcontrol);
1070 	spin_lock_irq(&chip->lock);
1071 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1072 		gain = ucontrol->value.integer.value[c];
1073 		/* Ignore out of range values */
1074 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1075 			continue;
1076 		if (chip->input_gain[c] != gain) {
1077 			set_input_gain(chip, c, gain);
1078 			changed = 1;
1079 		}
1080 	}
1081 	if (changed)
1082 		update_input_line_level(chip);
1083 	spin_unlock_irq(&chip->lock);
1084 	return changed;
1085 }
1086 
1087 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1088 
1089 static struct snd_kcontrol_new snd_echo_line_input_gain __devinitdata = {
1090 	.name = "Line Capture Volume",
1091 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1092 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1093 	.info = snd_echo_input_gain_info,
1094 	.get = snd_echo_input_gain_get,
1095 	.put = snd_echo_input_gain_put,
1096 	.tlv = {.p = db_scale_input_gain},
1097 };
1098 
1099 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1100 
1101 
1102 
1103 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1104 
1105 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1106 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1107 					 struct snd_ctl_elem_info *uinfo)
1108 {
1109 	struct echoaudio *chip;
1110 
1111 	chip = snd_kcontrol_chip(kcontrol);
1112 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1113 	uinfo->count = num_analog_busses_out(chip);
1114 	uinfo->value.integer.min = 0;
1115 	uinfo->value.integer.max = 1;
1116 	return 0;
1117 }
1118 
1119 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1120 				       struct snd_ctl_elem_value *ucontrol)
1121 {
1122 	struct echoaudio *chip;
1123 	int c;
1124 
1125 	chip = snd_kcontrol_chip(kcontrol);
1126 	for (c = 0; c < num_analog_busses_out(chip); c++)
1127 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1128 	return 0;
1129 }
1130 
1131 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1132 				       struct snd_ctl_elem_value *ucontrol)
1133 {
1134 	struct echoaudio *chip;
1135 	int c, changed;
1136 
1137 	changed = 0;
1138 	chip = snd_kcontrol_chip(kcontrol);
1139 	spin_lock_irq(&chip->lock);
1140 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1141 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1142 			set_nominal_level(chip, c,
1143 					  ucontrol->value.integer.value[c]);
1144 			changed = 1;
1145 		}
1146 	}
1147 	if (changed)
1148 		update_output_line_level(chip);
1149 	spin_unlock_irq(&chip->lock);
1150 	return changed;
1151 }
1152 
1153 static struct snd_kcontrol_new snd_echo_output_nominal_level __devinitdata = {
1154 	.name = "Line Playback Switch (-10dBV)",
1155 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1156 	.info = snd_echo_output_nominal_info,
1157 	.get = snd_echo_output_nominal_get,
1158 	.put = snd_echo_output_nominal_put,
1159 };
1160 
1161 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1162 
1163 
1164 
1165 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1166 
1167 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1168 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1169 				       struct snd_ctl_elem_info *uinfo)
1170 {
1171 	struct echoaudio *chip;
1172 
1173 	chip = snd_kcontrol_chip(kcontrol);
1174 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1175 	uinfo->count = num_analog_busses_in(chip);
1176 	uinfo->value.integer.min = 0;
1177 	uinfo->value.integer.max = 1;
1178 	return 0;
1179 }
1180 
1181 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1182 				      struct snd_ctl_elem_value *ucontrol)
1183 {
1184 	struct echoaudio *chip;
1185 	int c;
1186 
1187 	chip = snd_kcontrol_chip(kcontrol);
1188 	for (c = 0; c < num_analog_busses_in(chip); c++)
1189 		ucontrol->value.integer.value[c] =
1190 			chip->nominal_level[bx_analog_in(chip) + c];
1191 	return 0;
1192 }
1193 
1194 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1195 				      struct snd_ctl_elem_value *ucontrol)
1196 {
1197 	struct echoaudio *chip;
1198 	int c, changed;
1199 
1200 	changed = 0;
1201 	chip = snd_kcontrol_chip(kcontrol);
1202 	spin_lock_irq(&chip->lock);
1203 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1204 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1205 		    ucontrol->value.integer.value[c]) {
1206 			set_nominal_level(chip, bx_analog_in(chip) + c,
1207 					  ucontrol->value.integer.value[c]);
1208 			changed = 1;
1209 		}
1210 	}
1211 	if (changed)
1212 		update_output_line_level(chip);	/* "Output" is not a mistake
1213 						 * here.
1214 						 */
1215 	spin_unlock_irq(&chip->lock);
1216 	return changed;
1217 }
1218 
1219 static struct snd_kcontrol_new snd_echo_intput_nominal_level __devinitdata = {
1220 	.name = "Line Capture Switch (-10dBV)",
1221 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1222 	.info = snd_echo_input_nominal_info,
1223 	.get = snd_echo_input_nominal_get,
1224 	.put = snd_echo_input_nominal_put,
1225 };
1226 
1227 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1228 
1229 
1230 
1231 #ifdef ECHOCARD_HAS_MONITOR
1232 
1233 /******************* Monitor mixer *******************/
1234 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1235 			       struct snd_ctl_elem_info *uinfo)
1236 {
1237 	struct echoaudio *chip;
1238 
1239 	chip = snd_kcontrol_chip(kcontrol);
1240 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1241 	uinfo->count = 1;
1242 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1243 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1244 	uinfo->dimen.d[0] = num_busses_out(chip);
1245 	uinfo->dimen.d[1] = num_busses_in(chip);
1246 	return 0;
1247 }
1248 
1249 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1250 			      struct snd_ctl_elem_value *ucontrol)
1251 {
1252 	struct echoaudio *chip;
1253 
1254 	chip = snd_kcontrol_chip(kcontrol);
1255 	ucontrol->value.integer.value[0] =
1256 		chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1257 			[ucontrol->id.index % num_busses_in(chip)];
1258 	return 0;
1259 }
1260 
1261 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1262 			      struct snd_ctl_elem_value *ucontrol)
1263 {
1264 	struct echoaudio *chip;
1265 	int changed,  gain;
1266 	short out, in;
1267 
1268 	changed = 0;
1269 	chip = snd_kcontrol_chip(kcontrol);
1270 	out = ucontrol->id.index / num_busses_in(chip);
1271 	in = ucontrol->id.index % num_busses_in(chip);
1272 	gain = ucontrol->value.integer.value[0];
1273 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1274 		return -EINVAL;
1275 	if (chip->monitor_gain[out][in] != gain) {
1276 		spin_lock_irq(&chip->lock);
1277 		set_monitor_gain(chip, out, in, gain);
1278 		update_output_line_level(chip);
1279 		spin_unlock_irq(&chip->lock);
1280 		changed = 1;
1281 	}
1282 	return changed;
1283 }
1284 
1285 static struct snd_kcontrol_new snd_echo_monitor_mixer __devinitdata = {
1286 	.name = "Monitor Mixer Volume",
1287 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1288 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1289 	.info = snd_echo_mixer_info,
1290 	.get = snd_echo_mixer_get,
1291 	.put = snd_echo_mixer_put,
1292 	.tlv = {.p = db_scale_output_gain},
1293 };
1294 
1295 #endif /* ECHOCARD_HAS_MONITOR */
1296 
1297 
1298 
1299 #ifdef ECHOCARD_HAS_VMIXER
1300 
1301 /******************* Vmixer *******************/
1302 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1303 				struct snd_ctl_elem_info *uinfo)
1304 {
1305 	struct echoaudio *chip;
1306 
1307 	chip = snd_kcontrol_chip(kcontrol);
1308 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1309 	uinfo->count = 1;
1310 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1311 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1312 	uinfo->dimen.d[0] = num_busses_out(chip);
1313 	uinfo->dimen.d[1] = num_pipes_out(chip);
1314 	return 0;
1315 }
1316 
1317 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1318 			       struct snd_ctl_elem_value *ucontrol)
1319 {
1320 	struct echoaudio *chip;
1321 
1322 	chip = snd_kcontrol_chip(kcontrol);
1323 	ucontrol->value.integer.value[0] =
1324 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1325 			[ucontrol->id.index % num_pipes_out(chip)];
1326 	return 0;
1327 }
1328 
1329 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1330 			       struct snd_ctl_elem_value *ucontrol)
1331 {
1332 	struct echoaudio *chip;
1333 	int gain, changed;
1334 	short vch, out;
1335 
1336 	changed = 0;
1337 	chip = snd_kcontrol_chip(kcontrol);
1338 	out = ucontrol->id.index / num_pipes_out(chip);
1339 	vch = ucontrol->id.index % num_pipes_out(chip);
1340 	gain = ucontrol->value.integer.value[0];
1341 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1342 		return -EINVAL;
1343 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1344 		spin_lock_irq(&chip->lock);
1345 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1346 		update_vmixer_level(chip);
1347 		spin_unlock_irq(&chip->lock);
1348 		changed = 1;
1349 	}
1350 	return changed;
1351 }
1352 
1353 static struct snd_kcontrol_new snd_echo_vmixer __devinitdata = {
1354 	.name = "VMixer Volume",
1355 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1356 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1357 	.info = snd_echo_vmixer_info,
1358 	.get = snd_echo_vmixer_get,
1359 	.put = snd_echo_vmixer_put,
1360 	.tlv = {.p = db_scale_output_gain},
1361 };
1362 
1363 #endif /* ECHOCARD_HAS_VMIXER */
1364 
1365 
1366 
1367 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1368 
1369 /******************* Digital mode switch *******************/
1370 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1371 				      struct snd_ctl_elem_info *uinfo)
1372 {
1373 	static char *names[4] = {
1374 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1375 		"S/PDIF Cdrom"
1376 	};
1377 	struct echoaudio *chip;
1378 
1379 	chip = snd_kcontrol_chip(kcontrol);
1380 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1381 	uinfo->value.enumerated.items = chip->num_digital_modes;
1382 	uinfo->count = 1;
1383 	if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1384 		uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1385 	strcpy(uinfo->value.enumerated.name, names[
1386 			chip->digital_mode_list[uinfo->value.enumerated.item]]);
1387 	return 0;
1388 }
1389 
1390 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1391 				     struct snd_ctl_elem_value *ucontrol)
1392 {
1393 	struct echoaudio *chip;
1394 	int i, mode;
1395 
1396 	chip = snd_kcontrol_chip(kcontrol);
1397 	mode = chip->digital_mode;
1398 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1399 		if (mode == chip->digital_mode_list[i]) {
1400 			ucontrol->value.enumerated.item[0] = i;
1401 			break;
1402 		}
1403 	return 0;
1404 }
1405 
1406 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1407 				     struct snd_ctl_elem_value *ucontrol)
1408 {
1409 	struct echoaudio *chip;
1410 	int changed;
1411 	unsigned short emode, dmode;
1412 
1413 	changed = 0;
1414 	chip = snd_kcontrol_chip(kcontrol);
1415 
1416 	emode = ucontrol->value.enumerated.item[0];
1417 	if (emode >= chip->num_digital_modes)
1418 		return -EINVAL;
1419 	dmode = chip->digital_mode_list[emode];
1420 
1421 	if (dmode != chip->digital_mode) {
1422 		/* mode_mutex is required to make this operation atomic wrt
1423 		pcm_digital_*_open() and set_input_clock() functions. */
1424 		mutex_lock(&chip->mode_mutex);
1425 
1426 		/* Do not allow the user to change the digital mode when a pcm
1427 		device is open because it also changes the number of channels
1428 		and the allowed sample rates */
1429 		if (atomic_read(&chip->opencount)) {
1430 			changed = -EAGAIN;
1431 		} else {
1432 			changed = set_digital_mode(chip, dmode);
1433 			/* If we had to change the clock source, report it */
1434 			if (changed > 0 && chip->clock_src_ctl) {
1435 				snd_ctl_notify(chip->card,
1436 					       SNDRV_CTL_EVENT_MASK_VALUE,
1437 					       &chip->clock_src_ctl->id);
1438 				DE_ACT(("SDM() =%d\n", changed));
1439 			}
1440 			if (changed >= 0)
1441 				changed = 1;	/* No errors */
1442 		}
1443 		mutex_unlock(&chip->mode_mutex);
1444 	}
1445 	return changed;
1446 }
1447 
1448 static struct snd_kcontrol_new snd_echo_digital_mode_switch __devinitdata = {
1449 	.name = "Digital mode Switch",
1450 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1451 	.info = snd_echo_digital_mode_info,
1452 	.get = snd_echo_digital_mode_get,
1453 	.put = snd_echo_digital_mode_put,
1454 };
1455 
1456 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1457 
1458 
1459 
1460 #ifdef ECHOCARD_HAS_DIGITAL_IO
1461 
1462 /******************* S/PDIF mode switch *******************/
1463 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1464 				    struct snd_ctl_elem_info *uinfo)
1465 {
1466 	static char *names[2] = {"Consumer", "Professional"};
1467 
1468 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1469 	uinfo->value.enumerated.items = 2;
1470 	uinfo->count = 1;
1471 	if (uinfo->value.enumerated.item)
1472 		uinfo->value.enumerated.item = 1;
1473 	strcpy(uinfo->value.enumerated.name,
1474 	       names[uinfo->value.enumerated.item]);
1475 	return 0;
1476 }
1477 
1478 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1479 				   struct snd_ctl_elem_value *ucontrol)
1480 {
1481 	struct echoaudio *chip;
1482 
1483 	chip = snd_kcontrol_chip(kcontrol);
1484 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1485 	return 0;
1486 }
1487 
1488 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1489 				   struct snd_ctl_elem_value *ucontrol)
1490 {
1491 	struct echoaudio *chip;
1492 	int mode;
1493 
1494 	chip = snd_kcontrol_chip(kcontrol);
1495 	mode = !!ucontrol->value.enumerated.item[0];
1496 	if (mode != chip->professional_spdif) {
1497 		spin_lock_irq(&chip->lock);
1498 		set_professional_spdif(chip, mode);
1499 		spin_unlock_irq(&chip->lock);
1500 		return 1;
1501 	}
1502 	return 0;
1503 }
1504 
1505 static struct snd_kcontrol_new snd_echo_spdif_mode_switch __devinitdata = {
1506 	.name = "S/PDIF mode Switch",
1507 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1508 	.info = snd_echo_spdif_mode_info,
1509 	.get = snd_echo_spdif_mode_get,
1510 	.put = snd_echo_spdif_mode_put,
1511 };
1512 
1513 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1514 
1515 
1516 
1517 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1518 
1519 /******************* Select input clock source *******************/
1520 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1521 				      struct snd_ctl_elem_info *uinfo)
1522 {
1523 	static char *names[8] = {
1524 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1525 		"ESync96", "MTC"
1526 	};
1527 	struct echoaudio *chip;
1528 
1529 	chip = snd_kcontrol_chip(kcontrol);
1530 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1531 	uinfo->value.enumerated.items = chip->num_clock_sources;
1532 	uinfo->count = 1;
1533 	if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1534 		uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1535 	strcpy(uinfo->value.enumerated.name, names[
1536 			chip->clock_source_list[uinfo->value.enumerated.item]]);
1537 	return 0;
1538 }
1539 
1540 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1541 				     struct snd_ctl_elem_value *ucontrol)
1542 {
1543 	struct echoaudio *chip;
1544 	int i, clock;
1545 
1546 	chip = snd_kcontrol_chip(kcontrol);
1547 	clock = chip->input_clock;
1548 
1549 	for (i = 0; i < chip->num_clock_sources; i++)
1550 		if (clock == chip->clock_source_list[i])
1551 			ucontrol->value.enumerated.item[0] = i;
1552 
1553 	return 0;
1554 }
1555 
1556 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1557 				     struct snd_ctl_elem_value *ucontrol)
1558 {
1559 	struct echoaudio *chip;
1560 	int changed;
1561 	unsigned int eclock, dclock;
1562 
1563 	changed = 0;
1564 	chip = snd_kcontrol_chip(kcontrol);
1565 	eclock = ucontrol->value.enumerated.item[0];
1566 	if (eclock >= chip->input_clock_types)
1567 		return -EINVAL;
1568 	dclock = chip->clock_source_list[eclock];
1569 	if (chip->input_clock != dclock) {
1570 		mutex_lock(&chip->mode_mutex);
1571 		spin_lock_irq(&chip->lock);
1572 		if ((changed = set_input_clock(chip, dclock)) == 0)
1573 			changed = 1;	/* no errors */
1574 		spin_unlock_irq(&chip->lock);
1575 		mutex_unlock(&chip->mode_mutex);
1576 	}
1577 
1578 	if (changed < 0)
1579 		DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1580 
1581 	return changed;
1582 }
1583 
1584 static struct snd_kcontrol_new snd_echo_clock_source_switch __devinitdata = {
1585 	.name = "Sample Clock Source",
1586 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1587 	.info = snd_echo_clock_source_info,
1588 	.get = snd_echo_clock_source_get,
1589 	.put = snd_echo_clock_source_put,
1590 };
1591 
1592 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1593 
1594 
1595 
1596 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1597 
1598 /******************* Phantom power switch *******************/
1599 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1600 
1601 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1602 				      struct snd_ctl_elem_value *ucontrol)
1603 {
1604 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1605 
1606 	ucontrol->value.integer.value[0] = chip->phantom_power;
1607 	return 0;
1608 }
1609 
1610 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1611 				      struct snd_ctl_elem_value *ucontrol)
1612 {
1613 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1614 	int power, changed = 0;
1615 
1616 	power = !!ucontrol->value.integer.value[0];
1617 	if (chip->phantom_power != power) {
1618 		spin_lock_irq(&chip->lock);
1619 		changed = set_phantom_power(chip, power);
1620 		spin_unlock_irq(&chip->lock);
1621 		if (changed == 0)
1622 			changed = 1;	/* no errors */
1623 	}
1624 	return changed;
1625 }
1626 
1627 static struct snd_kcontrol_new snd_echo_phantom_power_switch __devinitdata = {
1628 	.name = "Phantom power Switch",
1629 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1630 	.info = snd_echo_phantom_power_info,
1631 	.get = snd_echo_phantom_power_get,
1632 	.put = snd_echo_phantom_power_put,
1633 };
1634 
1635 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1636 
1637 
1638 
1639 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1640 
1641 /******************* Digital input automute switch *******************/
1642 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1643 
1644 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1645 				 struct snd_ctl_elem_value *ucontrol)
1646 {
1647 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1648 
1649 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1650 	return 0;
1651 }
1652 
1653 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1654 				 struct snd_ctl_elem_value *ucontrol)
1655 {
1656 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1657 	int automute, changed = 0;
1658 
1659 	automute = !!ucontrol->value.integer.value[0];
1660 	if (chip->digital_in_automute != automute) {
1661 		spin_lock_irq(&chip->lock);
1662 		changed = set_input_auto_mute(chip, automute);
1663 		spin_unlock_irq(&chip->lock);
1664 		if (changed == 0)
1665 			changed = 1;	/* no errors */
1666 	}
1667 	return changed;
1668 }
1669 
1670 static struct snd_kcontrol_new snd_echo_automute_switch __devinitdata = {
1671 	.name = "Digital Capture Switch (automute)",
1672 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1673 	.info = snd_echo_automute_info,
1674 	.get = snd_echo_automute_get,
1675 	.put = snd_echo_automute_put,
1676 };
1677 
1678 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1679 
1680 
1681 
1682 /******************* VU-meters switch *******************/
1683 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1684 
1685 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1686 					struct snd_ctl_elem_value *ucontrol)
1687 {
1688 	struct echoaudio *chip;
1689 
1690 	chip = snd_kcontrol_chip(kcontrol);
1691 	spin_lock_irq(&chip->lock);
1692 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1693 	spin_unlock_irq(&chip->lock);
1694 	return 1;
1695 }
1696 
1697 static struct snd_kcontrol_new snd_echo_vumeters_switch __devinitdata = {
1698 	.name = "VU-meters Switch",
1699 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1700 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1701 	.info = snd_echo_vumeters_switch_info,
1702 	.put = snd_echo_vumeters_switch_put,
1703 };
1704 
1705 
1706 
1707 /***** Read VU-meters (input, output, analog and digital together) *****/
1708 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1709 				  struct snd_ctl_elem_info *uinfo)
1710 {
1711 	struct echoaudio *chip;
1712 
1713 	chip = snd_kcontrol_chip(kcontrol);
1714 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1715 	uinfo->count = 96;
1716 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1717 	uinfo->value.integer.max = 0;
1718 #ifdef ECHOCARD_HAS_VMIXER
1719 	uinfo->dimen.d[0] = 3;	/* Out, In, Virt */
1720 #else
1721 	uinfo->dimen.d[0] = 2;	/* Out, In */
1722 #endif
1723 	uinfo->dimen.d[1] = 16;	/* 16 channels */
1724 	uinfo->dimen.d[2] = 2;	/* 0=level, 1=peak */
1725 	return 0;
1726 }
1727 
1728 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1729 				 struct snd_ctl_elem_value *ucontrol)
1730 {
1731 	struct echoaudio *chip;
1732 
1733 	chip = snd_kcontrol_chip(kcontrol);
1734 	get_audio_meters(chip, ucontrol->value.integer.value);
1735 	return 0;
1736 }
1737 
1738 static struct snd_kcontrol_new snd_echo_vumeters __devinitdata = {
1739 	.name = "VU-meters",
1740 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1741 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1742 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1743 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1744 	.info = snd_echo_vumeters_info,
1745 	.get = snd_echo_vumeters_get,
1746 	.tlv = {.p = db_scale_output_gain},
1747 };
1748 
1749 
1750 
1751 /*** Channels info - it exports informations about the number of channels ***/
1752 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1753 				       struct snd_ctl_elem_info *uinfo)
1754 {
1755 	struct echoaudio *chip;
1756 
1757 	chip = snd_kcontrol_chip(kcontrol);
1758 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1759 	uinfo->count = 6;
1760 	uinfo->value.integer.min = 0;
1761 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1762 	return 0;
1763 }
1764 
1765 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1766 				      struct snd_ctl_elem_value *ucontrol)
1767 {
1768 	struct echoaudio *chip;
1769 	int detected, clocks, bit, src;
1770 
1771 	chip = snd_kcontrol_chip(kcontrol);
1772 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1773 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1774 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1775 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1776 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1777 
1778 	/* Compute the bitmask of the currently valid input clocks */
1779 	detected = detect_input_clocks(chip);
1780 	clocks = 0;
1781 	src = chip->num_clock_sources - 1;
1782 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1783 		if (detected & (1 << bit))
1784 			for (; src >= 0; src--)
1785 				if (bit == chip->clock_source_list[src]) {
1786 					clocks |= 1 << src;
1787 					break;
1788 				}
1789 	ucontrol->value.integer.value[5] = clocks;
1790 
1791 	return 0;
1792 }
1793 
1794 static struct snd_kcontrol_new snd_echo_channels_info __devinitdata = {
1795 	.name = "Channels info",
1796 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1797 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1798 	.info = snd_echo_channels_info_info,
1799 	.get = snd_echo_channels_info_get,
1800 };
1801 
1802 
1803 
1804 
1805 /******************************************************************************
1806 	IRQ Handler
1807 ******************************************************************************/
1808 
1809 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1810 {
1811 	struct echoaudio *chip = dev_id;
1812 	struct snd_pcm_substream *substream;
1813 	int period, ss, st;
1814 
1815 	spin_lock(&chip->lock);
1816 	st = service_irq(chip);
1817 	if (st < 0) {
1818 		spin_unlock(&chip->lock);
1819 		return IRQ_NONE;
1820 	}
1821 	/* The hardware doesn't tell us which substream caused the irq,
1822 	thus we have to check all running substreams. */
1823 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1824 		if ((substream = chip->substream[ss])) {
1825 			period = pcm_pointer(substream) /
1826 				substream->runtime->period_size;
1827 			if (period != chip->last_period[ss]) {
1828 				chip->last_period[ss] = period;
1829 				spin_unlock(&chip->lock);
1830 				snd_pcm_period_elapsed(substream);
1831 				spin_lock(&chip->lock);
1832 			}
1833 		}
1834 	}
1835 	spin_unlock(&chip->lock);
1836 
1837 #ifdef ECHOCARD_HAS_MIDI
1838 	if (st > 0 && chip->midi_in) {
1839 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1840 		DE_MID(("rawmidi_iread=%d\n", st));
1841 	}
1842 #endif
1843 	return IRQ_HANDLED;
1844 }
1845 
1846 
1847 
1848 
1849 /******************************************************************************
1850 	Module construction / destruction
1851 ******************************************************************************/
1852 
1853 static int snd_echo_free(struct echoaudio *chip)
1854 {
1855 	DE_INIT(("Stop DSP...\n"));
1856 	if (chip->comm_page)
1857 		rest_in_peace(chip);
1858 	DE_INIT(("Stopped.\n"));
1859 
1860 	if (chip->irq >= 0)
1861 		free_irq(chip->irq, chip);
1862 
1863 	if (chip->comm_page)
1864 		snd_dma_free_pages(&chip->commpage_dma_buf);
1865 
1866 	if (chip->dsp_registers)
1867 		iounmap(chip->dsp_registers);
1868 
1869 	if (chip->iores)
1870 		release_and_free_resource(chip->iores);
1871 
1872 	DE_INIT(("MMIO freed.\n"));
1873 
1874 	pci_disable_device(chip->pci);
1875 
1876 	/* release chip data */
1877 	kfree(chip);
1878 	DE_INIT(("Chip freed.\n"));
1879 	return 0;
1880 }
1881 
1882 
1883 
1884 static int snd_echo_dev_free(struct snd_device *device)
1885 {
1886 	struct echoaudio *chip = device->device_data;
1887 
1888 	DE_INIT(("snd_echo_dev_free()...\n"));
1889 	return snd_echo_free(chip);
1890 }
1891 
1892 
1893 
1894 /* <--snd_echo_probe() */
1895 static __devinit int snd_echo_create(struct snd_card *card,
1896 				     struct pci_dev *pci,
1897 				     struct echoaudio **rchip)
1898 {
1899 	struct echoaudio *chip;
1900 	int err;
1901 	size_t sz;
1902 	static struct snd_device_ops ops = {
1903 		.dev_free = snd_echo_dev_free,
1904 	};
1905 
1906 	*rchip = NULL;
1907 
1908 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1909 
1910 	if ((err = pci_enable_device(pci)) < 0)
1911 		return err;
1912 	pci_set_master(pci);
1913 
1914 	/* allocate a chip-specific data */
1915 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1916 	if (!chip) {
1917 		pci_disable_device(pci);
1918 		return -ENOMEM;
1919 	}
1920 	DE_INIT(("chip=%p\n", chip));
1921 
1922 	spin_lock_init(&chip->lock);
1923 	chip->card = card;
1924 	chip->pci = pci;
1925 	chip->irq = -1;
1926 
1927 	/* PCI resource allocation */
1928 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929 	sz = pci_resource_len(pci, 0);
1930 	if (sz > PAGE_SIZE)
1931 		sz = PAGE_SIZE;		/* We map only the required part */
1932 
1933 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1934 					      ECHOCARD_NAME)) == NULL) {
1935 		snd_echo_free(chip);
1936 		snd_printk(KERN_ERR "cannot get memory region\n");
1937 		return -EBUSY;
1938 	}
1939 	chip->dsp_registers = (volatile u32 __iomem *)
1940 		ioremap_nocache(chip->dsp_registers_phys, sz);
1941 
1942 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1943 			ECHOCARD_NAME, chip)) {
1944 		snd_echo_free(chip);
1945 		snd_printk(KERN_ERR "cannot grab irq\n");
1946 		return -EBUSY;
1947 	}
1948 	chip->irq = pci->irq;
1949 	DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
1950 		 chip->pci, chip->irq, chip->pci->subsystem_device));
1951 
1952 	/* Create the DSP comm page - this is the area of memory used for most
1953 	of the communication with the DSP, which accesses it via bus mastering */
1954 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1955 				sizeof(struct comm_page),
1956 				&chip->commpage_dma_buf) < 0) {
1957 		snd_echo_free(chip);
1958 		snd_printk(KERN_ERR "cannot allocate the comm page\n");
1959 		return -ENOMEM;
1960 	}
1961 	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1962 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1963 
1964 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1965 	if (err) {
1966 		DE_INIT(("init_hw err=%d\n", err));
1967 		snd_echo_free(chip);
1968 		return err;
1969 	}
1970 	DE_INIT(("Card init OK\n"));
1971 
1972 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1973 		snd_echo_free(chip);
1974 		return err;
1975 	}
1976 	atomic_set(&chip->opencount, 0);
1977 	mutex_init(&chip->mode_mutex);
1978 	chip->can_set_rate = 1;
1979 	*rchip = chip;
1980 	/* Init done ! */
1981 	return 0;
1982 }
1983 
1984 
1985 
1986 /* constructor */
1987 static int __devinit snd_echo_probe(struct pci_dev *pci,
1988 				    const struct pci_device_id *pci_id)
1989 {
1990 	static int dev;
1991 	struct snd_card *card;
1992 	struct echoaudio *chip;
1993 	char *dsp;
1994 	int i, err;
1995 
1996 	if (dev >= SNDRV_CARDS)
1997 		return -ENODEV;
1998 	if (!enable[dev]) {
1999 		dev++;
2000 		return -ENOENT;
2001 	}
2002 
2003 	DE_INIT(("Echoaudio driver starting...\n"));
2004 	i = 0;
2005 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2006 	if (err < 0)
2007 		return err;
2008 
2009 	snd_card_set_dev(card, &pci->dev);
2010 
2011 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2012 		snd_card_free(card);
2013 		return err;
2014 	}
2015 
2016 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2017 	strcpy(card->shortname, chip->card_name);
2018 
2019 	dsp = "56301";
2020 	if (pci_id->device == 0x3410)
2021 		dsp = "56361";
2022 
2023 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2024 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2025 		chip->dsp_registers_phys, chip->irq);
2026 
2027 	if ((err = snd_echo_new_pcm(chip)) < 0) {
2028 		snd_printk(KERN_ERR "new pcm error %d\n", err);
2029 		snd_card_free(card);
2030 		return err;
2031 	}
2032 
2033 #ifdef ECHOCARD_HAS_MIDI
2034 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2035 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
2036 			snd_printk(KERN_ERR "new midi error %d\n", err);
2037 			snd_card_free(card);
2038 			return err;
2039 		}
2040 	}
2041 #endif
2042 
2043 #ifdef ECHOCARD_HAS_VMIXER
2044 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2045 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2046 		goto ctl_error;
2047 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2048 	err = snd_ctl_add(chip->card,
2049 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2050 	if (err < 0)
2051 		goto ctl_error;
2052 #endif
2053 #else /* ECHOCARD_HAS_VMIXER */
2054 	err = snd_ctl_add(chip->card,
2055 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2056 	if (err < 0)
2057 		goto ctl_error;
2058 #endif /* ECHOCARD_HAS_VMIXER */
2059 
2060 #ifdef ECHOCARD_HAS_INPUT_GAIN
2061 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2062 		goto ctl_error;
2063 #endif
2064 
2065 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2066 	if (!chip->hasnt_input_nominal_level)
2067 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2068 			goto ctl_error;
2069 #endif
2070 
2071 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2072 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2073 		goto ctl_error;
2074 #endif
2075 
2076 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2077 		goto ctl_error;
2078 
2079 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2080 		goto ctl_error;
2081 
2082 #ifdef ECHOCARD_HAS_MONITOR
2083 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2084 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2085 		goto ctl_error;
2086 #endif
2087 
2088 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2089 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2090 		goto ctl_error;
2091 #endif
2092 
2093 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2094 		goto ctl_error;
2095 
2096 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2097 	/* Creates a list of available digital modes */
2098 	chip->num_digital_modes = 0;
2099 	for (i = 0; i < 6; i++)
2100 		if (chip->digital_modes & (1 << i))
2101 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2102 
2103 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2104 		goto ctl_error;
2105 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2106 
2107 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2108 	/* Creates a list of available clock sources */
2109 	chip->num_clock_sources = 0;
2110 	for (i = 0; i < 10; i++)
2111 		if (chip->input_clock_types & (1 << i))
2112 			chip->clock_source_list[chip->num_clock_sources++] = i;
2113 
2114 	if (chip->num_clock_sources > 1) {
2115 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2116 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2117 			goto ctl_error;
2118 	}
2119 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2120 
2121 #ifdef ECHOCARD_HAS_DIGITAL_IO
2122 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2123 		goto ctl_error;
2124 #endif
2125 
2126 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2127 	if (chip->has_phantom_power)
2128 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2129 			goto ctl_error;
2130 #endif
2131 
2132 	if ((err = snd_card_register(card)) < 0) {
2133 		snd_card_free(card);
2134 		goto ctl_error;
2135 	}
2136 	snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2137 
2138 	pci_set_drvdata(pci, chip);
2139 	dev++;
2140 	return 0;
2141 
2142 ctl_error:
2143 	snd_printk(KERN_ERR "new control error %d\n", err);
2144 	snd_card_free(card);
2145 	return err;
2146 }
2147 
2148 
2149 
2150 static void __devexit snd_echo_remove(struct pci_dev *pci)
2151 {
2152 	struct echoaudio *chip;
2153 
2154 	chip = pci_get_drvdata(pci);
2155 	if (chip)
2156 		snd_card_free(chip->card);
2157 	pci_set_drvdata(pci, NULL);
2158 }
2159 
2160 
2161 
2162 /******************************************************************************
2163 	Everything starts and ends here
2164 ******************************************************************************/
2165 
2166 /* pci_driver definition */
2167 static struct pci_driver driver = {
2168 	.name = "Echoaudio " ECHOCARD_NAME,
2169 	.id_table = snd_echo_ids,
2170 	.probe = snd_echo_probe,
2171 	.remove = __devexit_p(snd_echo_remove),
2172 };
2173 
2174 
2175 
2176 /* initialization of the module */
2177 static int __init alsa_card_echo_init(void)
2178 {
2179 	return pci_register_driver(&driver);
2180 }
2181 
2182 
2183 
2184 /* clean up the module */
2185 static void __exit alsa_card_echo_exit(void)
2186 {
2187 	pci_unregister_driver(&driver);
2188 }
2189 
2190 
2191 module_init(alsa_card_echo_init)
2192 module_exit(alsa_card_echo_exit)
2193