xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision b627b4ed)
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 #ifndef ECHOCARD_HAS_VMIXER
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 static struct snd_kcontrol_new snd_echo_pcm_output_gain __devinitdata = {
1007 	.name = "PCM Playback Volume",
1008 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1009 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1010 	.info = snd_echo_output_gain_info,
1011 	.get = snd_echo_output_gain_get,
1012 	.put = snd_echo_output_gain_put,
1013 	.tlv = {.p = db_scale_output_gain},
1014 };
1015 
1016 #endif
1017 
1018 
1019 
1020 #ifdef ECHOCARD_HAS_INPUT_GAIN
1021 
1022 /******************* Analog input volume *******************/
1023 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1024 				    struct snd_ctl_elem_info *uinfo)
1025 {
1026 	struct echoaudio *chip;
1027 
1028 	chip = snd_kcontrol_chip(kcontrol);
1029 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1030 	uinfo->count = num_analog_busses_in(chip);
1031 	uinfo->value.integer.min = ECHOGAIN_MININP;
1032 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1033 	return 0;
1034 }
1035 
1036 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1037 				   struct snd_ctl_elem_value *ucontrol)
1038 {
1039 	struct echoaudio *chip;
1040 	int c;
1041 
1042 	chip = snd_kcontrol_chip(kcontrol);
1043 	for (c = 0; c < num_analog_busses_in(chip); c++)
1044 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1045 	return 0;
1046 }
1047 
1048 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1049 				   struct snd_ctl_elem_value *ucontrol)
1050 {
1051 	struct echoaudio *chip;
1052 	int c, gain, changed;
1053 
1054 	changed = 0;
1055 	chip = snd_kcontrol_chip(kcontrol);
1056 	spin_lock_irq(&chip->lock);
1057 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1058 		gain = ucontrol->value.integer.value[c];
1059 		/* Ignore out of range values */
1060 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1061 			continue;
1062 		if (chip->input_gain[c] != gain) {
1063 			set_input_gain(chip, c, gain);
1064 			changed = 1;
1065 		}
1066 	}
1067 	if (changed)
1068 		update_input_line_level(chip);
1069 	spin_unlock_irq(&chip->lock);
1070 	return changed;
1071 }
1072 
1073 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1074 
1075 static struct snd_kcontrol_new snd_echo_line_input_gain __devinitdata = {
1076 	.name = "Line Capture Volume",
1077 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1078 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1079 	.info = snd_echo_input_gain_info,
1080 	.get = snd_echo_input_gain_get,
1081 	.put = snd_echo_input_gain_put,
1082 	.tlv = {.p = db_scale_input_gain},
1083 };
1084 
1085 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1086 
1087 
1088 
1089 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1090 
1091 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1092 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1093 					 struct snd_ctl_elem_info *uinfo)
1094 {
1095 	struct echoaudio *chip;
1096 
1097 	chip = snd_kcontrol_chip(kcontrol);
1098 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1099 	uinfo->count = num_analog_busses_out(chip);
1100 	uinfo->value.integer.min = 0;
1101 	uinfo->value.integer.max = 1;
1102 	return 0;
1103 }
1104 
1105 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1106 				       struct snd_ctl_elem_value *ucontrol)
1107 {
1108 	struct echoaudio *chip;
1109 	int c;
1110 
1111 	chip = snd_kcontrol_chip(kcontrol);
1112 	for (c = 0; c < num_analog_busses_out(chip); c++)
1113 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1114 	return 0;
1115 }
1116 
1117 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1118 				       struct snd_ctl_elem_value *ucontrol)
1119 {
1120 	struct echoaudio *chip;
1121 	int c, changed;
1122 
1123 	changed = 0;
1124 	chip = snd_kcontrol_chip(kcontrol);
1125 	spin_lock_irq(&chip->lock);
1126 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1127 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1128 			set_nominal_level(chip, c,
1129 					  ucontrol->value.integer.value[c]);
1130 			changed = 1;
1131 		}
1132 	}
1133 	if (changed)
1134 		update_output_line_level(chip);
1135 	spin_unlock_irq(&chip->lock);
1136 	return changed;
1137 }
1138 
1139 static struct snd_kcontrol_new snd_echo_output_nominal_level __devinitdata = {
1140 	.name = "Line Playback Switch (-10dBV)",
1141 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1142 	.info = snd_echo_output_nominal_info,
1143 	.get = snd_echo_output_nominal_get,
1144 	.put = snd_echo_output_nominal_put,
1145 };
1146 
1147 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1148 
1149 
1150 
1151 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1152 
1153 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1154 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1155 				       struct snd_ctl_elem_info *uinfo)
1156 {
1157 	struct echoaudio *chip;
1158 
1159 	chip = snd_kcontrol_chip(kcontrol);
1160 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1161 	uinfo->count = num_analog_busses_in(chip);
1162 	uinfo->value.integer.min = 0;
1163 	uinfo->value.integer.max = 1;
1164 	return 0;
1165 }
1166 
1167 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1168 				      struct snd_ctl_elem_value *ucontrol)
1169 {
1170 	struct echoaudio *chip;
1171 	int c;
1172 
1173 	chip = snd_kcontrol_chip(kcontrol);
1174 	for (c = 0; c < num_analog_busses_in(chip); c++)
1175 		ucontrol->value.integer.value[c] =
1176 			chip->nominal_level[bx_analog_in(chip) + c];
1177 	return 0;
1178 }
1179 
1180 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1181 				      struct snd_ctl_elem_value *ucontrol)
1182 {
1183 	struct echoaudio *chip;
1184 	int c, changed;
1185 
1186 	changed = 0;
1187 	chip = snd_kcontrol_chip(kcontrol);
1188 	spin_lock_irq(&chip->lock);
1189 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1190 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1191 		    ucontrol->value.integer.value[c]) {
1192 			set_nominal_level(chip, bx_analog_in(chip) + c,
1193 					  ucontrol->value.integer.value[c]);
1194 			changed = 1;
1195 		}
1196 	}
1197 	if (changed)
1198 		update_output_line_level(chip);	/* "Output" is not a mistake
1199 						 * here.
1200 						 */
1201 	spin_unlock_irq(&chip->lock);
1202 	return changed;
1203 }
1204 
1205 static struct snd_kcontrol_new snd_echo_intput_nominal_level __devinitdata = {
1206 	.name = "Line Capture Switch (-10dBV)",
1207 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1208 	.info = snd_echo_input_nominal_info,
1209 	.get = snd_echo_input_nominal_get,
1210 	.put = snd_echo_input_nominal_put,
1211 };
1212 
1213 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1214 
1215 
1216 
1217 #ifdef ECHOCARD_HAS_MONITOR
1218 
1219 /******************* Monitor mixer *******************/
1220 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1221 			       struct snd_ctl_elem_info *uinfo)
1222 {
1223 	struct echoaudio *chip;
1224 
1225 	chip = snd_kcontrol_chip(kcontrol);
1226 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1227 	uinfo->count = 1;
1228 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1229 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1230 	uinfo->dimen.d[0] = num_busses_out(chip);
1231 	uinfo->dimen.d[1] = num_busses_in(chip);
1232 	return 0;
1233 }
1234 
1235 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1236 			      struct snd_ctl_elem_value *ucontrol)
1237 {
1238 	struct echoaudio *chip;
1239 
1240 	chip = snd_kcontrol_chip(kcontrol);
1241 	ucontrol->value.integer.value[0] =
1242 		chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1243 			[ucontrol->id.index % num_busses_in(chip)];
1244 	return 0;
1245 }
1246 
1247 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1248 			      struct snd_ctl_elem_value *ucontrol)
1249 {
1250 	struct echoaudio *chip;
1251 	int changed,  gain;
1252 	short out, in;
1253 
1254 	changed = 0;
1255 	chip = snd_kcontrol_chip(kcontrol);
1256 	out = ucontrol->id.index / num_busses_in(chip);
1257 	in = ucontrol->id.index % num_busses_in(chip);
1258 	gain = ucontrol->value.integer.value[0];
1259 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1260 		return -EINVAL;
1261 	if (chip->monitor_gain[out][in] != gain) {
1262 		spin_lock_irq(&chip->lock);
1263 		set_monitor_gain(chip, out, in, gain);
1264 		update_output_line_level(chip);
1265 		spin_unlock_irq(&chip->lock);
1266 		changed = 1;
1267 	}
1268 	return changed;
1269 }
1270 
1271 static struct snd_kcontrol_new snd_echo_monitor_mixer __devinitdata = {
1272 	.name = "Monitor Mixer Volume",
1273 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1274 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1275 	.info = snd_echo_mixer_info,
1276 	.get = snd_echo_mixer_get,
1277 	.put = snd_echo_mixer_put,
1278 	.tlv = {.p = db_scale_output_gain},
1279 };
1280 
1281 #endif /* ECHOCARD_HAS_MONITOR */
1282 
1283 
1284 
1285 #ifdef ECHOCARD_HAS_VMIXER
1286 
1287 /******************* Vmixer *******************/
1288 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1289 				struct snd_ctl_elem_info *uinfo)
1290 {
1291 	struct echoaudio *chip;
1292 
1293 	chip = snd_kcontrol_chip(kcontrol);
1294 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1295 	uinfo->count = 1;
1296 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1297 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1298 	uinfo->dimen.d[0] = num_busses_out(chip);
1299 	uinfo->dimen.d[1] = num_pipes_out(chip);
1300 	return 0;
1301 }
1302 
1303 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1304 			       struct snd_ctl_elem_value *ucontrol)
1305 {
1306 	struct echoaudio *chip;
1307 
1308 	chip = snd_kcontrol_chip(kcontrol);
1309 	ucontrol->value.integer.value[0] =
1310 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1311 			[ucontrol->id.index % num_pipes_out(chip)];
1312 	return 0;
1313 }
1314 
1315 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1316 			       struct snd_ctl_elem_value *ucontrol)
1317 {
1318 	struct echoaudio *chip;
1319 	int gain, changed;
1320 	short vch, out;
1321 
1322 	changed = 0;
1323 	chip = snd_kcontrol_chip(kcontrol);
1324 	out = ucontrol->id.index / num_pipes_out(chip);
1325 	vch = ucontrol->id.index % num_pipes_out(chip);
1326 	gain = ucontrol->value.integer.value[0];
1327 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1328 		return -EINVAL;
1329 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1330 		spin_lock_irq(&chip->lock);
1331 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1332 		update_vmixer_level(chip);
1333 		spin_unlock_irq(&chip->lock);
1334 		changed = 1;
1335 	}
1336 	return changed;
1337 }
1338 
1339 static struct snd_kcontrol_new snd_echo_vmixer __devinitdata = {
1340 	.name = "VMixer Volume",
1341 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1342 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1343 	.info = snd_echo_vmixer_info,
1344 	.get = snd_echo_vmixer_get,
1345 	.put = snd_echo_vmixer_put,
1346 	.tlv = {.p = db_scale_output_gain},
1347 };
1348 
1349 #endif /* ECHOCARD_HAS_VMIXER */
1350 
1351 
1352 
1353 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1354 
1355 /******************* Digital mode switch *******************/
1356 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1357 				      struct snd_ctl_elem_info *uinfo)
1358 {
1359 	static char *names[4] = {
1360 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1361 		"S/PDIF Cdrom"
1362 	};
1363 	struct echoaudio *chip;
1364 
1365 	chip = snd_kcontrol_chip(kcontrol);
1366 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1367 	uinfo->value.enumerated.items = chip->num_digital_modes;
1368 	uinfo->count = 1;
1369 	if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1370 		uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1371 	strcpy(uinfo->value.enumerated.name, names[
1372 			chip->digital_mode_list[uinfo->value.enumerated.item]]);
1373 	return 0;
1374 }
1375 
1376 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1377 				     struct snd_ctl_elem_value *ucontrol)
1378 {
1379 	struct echoaudio *chip;
1380 	int i, mode;
1381 
1382 	chip = snd_kcontrol_chip(kcontrol);
1383 	mode = chip->digital_mode;
1384 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1385 		if (mode == chip->digital_mode_list[i]) {
1386 			ucontrol->value.enumerated.item[0] = i;
1387 			break;
1388 		}
1389 	return 0;
1390 }
1391 
1392 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1393 				     struct snd_ctl_elem_value *ucontrol)
1394 {
1395 	struct echoaudio *chip;
1396 	int changed;
1397 	unsigned short emode, dmode;
1398 
1399 	changed = 0;
1400 	chip = snd_kcontrol_chip(kcontrol);
1401 
1402 	emode = ucontrol->value.enumerated.item[0];
1403 	if (emode >= chip->num_digital_modes)
1404 		return -EINVAL;
1405 	dmode = chip->digital_mode_list[emode];
1406 
1407 	if (dmode != chip->digital_mode) {
1408 		/* mode_mutex is required to make this operation atomic wrt
1409 		pcm_digital_*_open() and set_input_clock() functions. */
1410 		mutex_lock(&chip->mode_mutex);
1411 
1412 		/* Do not allow the user to change the digital mode when a pcm
1413 		device is open because it also changes the number of channels
1414 		and the allowed sample rates */
1415 		if (atomic_read(&chip->opencount)) {
1416 			changed = -EAGAIN;
1417 		} else {
1418 			changed = set_digital_mode(chip, dmode);
1419 			/* If we had to change the clock source, report it */
1420 			if (changed > 0 && chip->clock_src_ctl) {
1421 				snd_ctl_notify(chip->card,
1422 					       SNDRV_CTL_EVENT_MASK_VALUE,
1423 					       &chip->clock_src_ctl->id);
1424 				DE_ACT(("SDM() =%d\n", changed));
1425 			}
1426 			if (changed >= 0)
1427 				changed = 1;	/* No errors */
1428 		}
1429 		mutex_unlock(&chip->mode_mutex);
1430 	}
1431 	return changed;
1432 }
1433 
1434 static struct snd_kcontrol_new snd_echo_digital_mode_switch __devinitdata = {
1435 	.name = "Digital mode Switch",
1436 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1437 	.info = snd_echo_digital_mode_info,
1438 	.get = snd_echo_digital_mode_get,
1439 	.put = snd_echo_digital_mode_put,
1440 };
1441 
1442 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1443 
1444 
1445 
1446 #ifdef ECHOCARD_HAS_DIGITAL_IO
1447 
1448 /******************* S/PDIF mode switch *******************/
1449 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1450 				    struct snd_ctl_elem_info *uinfo)
1451 {
1452 	static char *names[2] = {"Consumer", "Professional"};
1453 
1454 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1455 	uinfo->value.enumerated.items = 2;
1456 	uinfo->count = 1;
1457 	if (uinfo->value.enumerated.item)
1458 		uinfo->value.enumerated.item = 1;
1459 	strcpy(uinfo->value.enumerated.name,
1460 	       names[uinfo->value.enumerated.item]);
1461 	return 0;
1462 }
1463 
1464 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1465 				   struct snd_ctl_elem_value *ucontrol)
1466 {
1467 	struct echoaudio *chip;
1468 
1469 	chip = snd_kcontrol_chip(kcontrol);
1470 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1471 	return 0;
1472 }
1473 
1474 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1475 				   struct snd_ctl_elem_value *ucontrol)
1476 {
1477 	struct echoaudio *chip;
1478 	int mode;
1479 
1480 	chip = snd_kcontrol_chip(kcontrol);
1481 	mode = !!ucontrol->value.enumerated.item[0];
1482 	if (mode != chip->professional_spdif) {
1483 		spin_lock_irq(&chip->lock);
1484 		set_professional_spdif(chip, mode);
1485 		spin_unlock_irq(&chip->lock);
1486 		return 1;
1487 	}
1488 	return 0;
1489 }
1490 
1491 static struct snd_kcontrol_new snd_echo_spdif_mode_switch __devinitdata = {
1492 	.name = "S/PDIF mode Switch",
1493 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1494 	.info = snd_echo_spdif_mode_info,
1495 	.get = snd_echo_spdif_mode_get,
1496 	.put = snd_echo_spdif_mode_put,
1497 };
1498 
1499 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1500 
1501 
1502 
1503 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1504 
1505 /******************* Select input clock source *******************/
1506 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1507 				      struct snd_ctl_elem_info *uinfo)
1508 {
1509 	static char *names[8] = {
1510 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1511 		"ESync96", "MTC"
1512 	};
1513 	struct echoaudio *chip;
1514 
1515 	chip = snd_kcontrol_chip(kcontrol);
1516 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1517 	uinfo->value.enumerated.items = chip->num_clock_sources;
1518 	uinfo->count = 1;
1519 	if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1520 		uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1521 	strcpy(uinfo->value.enumerated.name, names[
1522 			chip->clock_source_list[uinfo->value.enumerated.item]]);
1523 	return 0;
1524 }
1525 
1526 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1527 				     struct snd_ctl_elem_value *ucontrol)
1528 {
1529 	struct echoaudio *chip;
1530 	int i, clock;
1531 
1532 	chip = snd_kcontrol_chip(kcontrol);
1533 	clock = chip->input_clock;
1534 
1535 	for (i = 0; i < chip->num_clock_sources; i++)
1536 		if (clock == chip->clock_source_list[i])
1537 			ucontrol->value.enumerated.item[0] = i;
1538 
1539 	return 0;
1540 }
1541 
1542 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1543 				     struct snd_ctl_elem_value *ucontrol)
1544 {
1545 	struct echoaudio *chip;
1546 	int changed;
1547 	unsigned int eclock, dclock;
1548 
1549 	changed = 0;
1550 	chip = snd_kcontrol_chip(kcontrol);
1551 	eclock = ucontrol->value.enumerated.item[0];
1552 	if (eclock >= chip->input_clock_types)
1553 		return -EINVAL;
1554 	dclock = chip->clock_source_list[eclock];
1555 	if (chip->input_clock != dclock) {
1556 		mutex_lock(&chip->mode_mutex);
1557 		spin_lock_irq(&chip->lock);
1558 		if ((changed = set_input_clock(chip, dclock)) == 0)
1559 			changed = 1;	/* no errors */
1560 		spin_unlock_irq(&chip->lock);
1561 		mutex_unlock(&chip->mode_mutex);
1562 	}
1563 
1564 	if (changed < 0)
1565 		DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1566 
1567 	return changed;
1568 }
1569 
1570 static struct snd_kcontrol_new snd_echo_clock_source_switch __devinitdata = {
1571 	.name = "Sample Clock Source",
1572 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1573 	.info = snd_echo_clock_source_info,
1574 	.get = snd_echo_clock_source_get,
1575 	.put = snd_echo_clock_source_put,
1576 };
1577 
1578 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1579 
1580 
1581 
1582 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1583 
1584 /******************* Phantom power switch *******************/
1585 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1586 
1587 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1588 				      struct snd_ctl_elem_value *ucontrol)
1589 {
1590 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1591 
1592 	ucontrol->value.integer.value[0] = chip->phantom_power;
1593 	return 0;
1594 }
1595 
1596 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1597 				      struct snd_ctl_elem_value *ucontrol)
1598 {
1599 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1600 	int power, changed = 0;
1601 
1602 	power = !!ucontrol->value.integer.value[0];
1603 	if (chip->phantom_power != power) {
1604 		spin_lock_irq(&chip->lock);
1605 		changed = set_phantom_power(chip, power);
1606 		spin_unlock_irq(&chip->lock);
1607 		if (changed == 0)
1608 			changed = 1;	/* no errors */
1609 	}
1610 	return changed;
1611 }
1612 
1613 static struct snd_kcontrol_new snd_echo_phantom_power_switch __devinitdata = {
1614 	.name = "Phantom power Switch",
1615 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1616 	.info = snd_echo_phantom_power_info,
1617 	.get = snd_echo_phantom_power_get,
1618 	.put = snd_echo_phantom_power_put,
1619 };
1620 
1621 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1622 
1623 
1624 
1625 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1626 
1627 /******************* Digital input automute switch *******************/
1628 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1629 
1630 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1631 				 struct snd_ctl_elem_value *ucontrol)
1632 {
1633 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1634 
1635 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1636 	return 0;
1637 }
1638 
1639 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1640 				 struct snd_ctl_elem_value *ucontrol)
1641 {
1642 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1643 	int automute, changed = 0;
1644 
1645 	automute = !!ucontrol->value.integer.value[0];
1646 	if (chip->digital_in_automute != automute) {
1647 		spin_lock_irq(&chip->lock);
1648 		changed = set_input_auto_mute(chip, automute);
1649 		spin_unlock_irq(&chip->lock);
1650 		if (changed == 0)
1651 			changed = 1;	/* no errors */
1652 	}
1653 	return changed;
1654 }
1655 
1656 static struct snd_kcontrol_new snd_echo_automute_switch __devinitdata = {
1657 	.name = "Digital Capture Switch (automute)",
1658 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1659 	.info = snd_echo_automute_info,
1660 	.get = snd_echo_automute_get,
1661 	.put = snd_echo_automute_put,
1662 };
1663 
1664 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1665 
1666 
1667 
1668 /******************* VU-meters switch *******************/
1669 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1670 
1671 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1672 					struct snd_ctl_elem_value *ucontrol)
1673 {
1674 	struct echoaudio *chip;
1675 
1676 	chip = snd_kcontrol_chip(kcontrol);
1677 	spin_lock_irq(&chip->lock);
1678 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1679 	spin_unlock_irq(&chip->lock);
1680 	return 1;
1681 }
1682 
1683 static struct snd_kcontrol_new snd_echo_vumeters_switch __devinitdata = {
1684 	.name = "VU-meters Switch",
1685 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1686 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1687 	.info = snd_echo_vumeters_switch_info,
1688 	.put = snd_echo_vumeters_switch_put,
1689 };
1690 
1691 
1692 
1693 /***** Read VU-meters (input, output, analog and digital together) *****/
1694 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1695 				  struct snd_ctl_elem_info *uinfo)
1696 {
1697 	struct echoaudio *chip;
1698 
1699 	chip = snd_kcontrol_chip(kcontrol);
1700 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1701 	uinfo->count = 96;
1702 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1703 	uinfo->value.integer.max = 0;
1704 #ifdef ECHOCARD_HAS_VMIXER
1705 	uinfo->dimen.d[0] = 3;	/* Out, In, Virt */
1706 #else
1707 	uinfo->dimen.d[0] = 2;	/* Out, In */
1708 #endif
1709 	uinfo->dimen.d[1] = 16;	/* 16 channels */
1710 	uinfo->dimen.d[2] = 2;	/* 0=level, 1=peak */
1711 	return 0;
1712 }
1713 
1714 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1715 				 struct snd_ctl_elem_value *ucontrol)
1716 {
1717 	struct echoaudio *chip;
1718 
1719 	chip = snd_kcontrol_chip(kcontrol);
1720 	get_audio_meters(chip, ucontrol->value.integer.value);
1721 	return 0;
1722 }
1723 
1724 static struct snd_kcontrol_new snd_echo_vumeters __devinitdata = {
1725 	.name = "VU-meters",
1726 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1727 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1728 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1729 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1730 	.info = snd_echo_vumeters_info,
1731 	.get = snd_echo_vumeters_get,
1732 	.tlv = {.p = db_scale_output_gain},
1733 };
1734 
1735 
1736 
1737 /*** Channels info - it exports informations about the number of channels ***/
1738 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1739 				       struct snd_ctl_elem_info *uinfo)
1740 {
1741 	struct echoaudio *chip;
1742 
1743 	chip = snd_kcontrol_chip(kcontrol);
1744 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1745 	uinfo->count = 6;
1746 	uinfo->value.integer.min = 0;
1747 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1748 	return 0;
1749 }
1750 
1751 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1752 				      struct snd_ctl_elem_value *ucontrol)
1753 {
1754 	struct echoaudio *chip;
1755 	int detected, clocks, bit, src;
1756 
1757 	chip = snd_kcontrol_chip(kcontrol);
1758 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1759 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1760 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1761 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1762 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1763 
1764 	/* Compute the bitmask of the currently valid input clocks */
1765 	detected = detect_input_clocks(chip);
1766 	clocks = 0;
1767 	src = chip->num_clock_sources - 1;
1768 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1769 		if (detected & (1 << bit))
1770 			for (; src >= 0; src--)
1771 				if (bit == chip->clock_source_list[src]) {
1772 					clocks |= 1 << src;
1773 					break;
1774 				}
1775 	ucontrol->value.integer.value[5] = clocks;
1776 
1777 	return 0;
1778 }
1779 
1780 static struct snd_kcontrol_new snd_echo_channels_info __devinitdata = {
1781 	.name = "Channels info",
1782 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1783 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1784 	.info = snd_echo_channels_info_info,
1785 	.get = snd_echo_channels_info_get,
1786 };
1787 
1788 
1789 
1790 
1791 /******************************************************************************
1792 	IRQ Handler
1793 ******************************************************************************/
1794 
1795 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1796 {
1797 	struct echoaudio *chip = dev_id;
1798 	struct snd_pcm_substream *substream;
1799 	int period, ss, st;
1800 
1801 	spin_lock(&chip->lock);
1802 	st = service_irq(chip);
1803 	if (st < 0) {
1804 		spin_unlock(&chip->lock);
1805 		return IRQ_NONE;
1806 	}
1807 	/* The hardware doesn't tell us which substream caused the irq,
1808 	thus we have to check all running substreams. */
1809 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1810 		if ((substream = chip->substream[ss])) {
1811 			period = pcm_pointer(substream) /
1812 				substream->runtime->period_size;
1813 			if (period != chip->last_period[ss]) {
1814 				chip->last_period[ss] = period;
1815 				spin_unlock(&chip->lock);
1816 				snd_pcm_period_elapsed(substream);
1817 				spin_lock(&chip->lock);
1818 			}
1819 		}
1820 	}
1821 	spin_unlock(&chip->lock);
1822 
1823 #ifdef ECHOCARD_HAS_MIDI
1824 	if (st > 0 && chip->midi_in) {
1825 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1826 		DE_MID(("rawmidi_iread=%d\n", st));
1827 	}
1828 #endif
1829 	return IRQ_HANDLED;
1830 }
1831 
1832 
1833 
1834 
1835 /******************************************************************************
1836 	Module construction / destruction
1837 ******************************************************************************/
1838 
1839 static int snd_echo_free(struct echoaudio *chip)
1840 {
1841 	DE_INIT(("Stop DSP...\n"));
1842 	if (chip->comm_page)
1843 		rest_in_peace(chip);
1844 	DE_INIT(("Stopped.\n"));
1845 
1846 	if (chip->irq >= 0)
1847 		free_irq(chip->irq, chip);
1848 
1849 	if (chip->comm_page)
1850 		snd_dma_free_pages(&chip->commpage_dma_buf);
1851 
1852 	if (chip->dsp_registers)
1853 		iounmap(chip->dsp_registers);
1854 
1855 	if (chip->iores)
1856 		release_and_free_resource(chip->iores);
1857 
1858 	DE_INIT(("MMIO freed.\n"));
1859 
1860 	pci_disable_device(chip->pci);
1861 
1862 	/* release chip data */
1863 	kfree(chip);
1864 	DE_INIT(("Chip freed.\n"));
1865 	return 0;
1866 }
1867 
1868 
1869 
1870 static int snd_echo_dev_free(struct snd_device *device)
1871 {
1872 	struct echoaudio *chip = device->device_data;
1873 
1874 	DE_INIT(("snd_echo_dev_free()...\n"));
1875 	return snd_echo_free(chip);
1876 }
1877 
1878 
1879 
1880 /* <--snd_echo_probe() */
1881 static __devinit int snd_echo_create(struct snd_card *card,
1882 				     struct pci_dev *pci,
1883 				     struct echoaudio **rchip)
1884 {
1885 	struct echoaudio *chip;
1886 	int err;
1887 	size_t sz;
1888 	static struct snd_device_ops ops = {
1889 		.dev_free = snd_echo_dev_free,
1890 	};
1891 
1892 	*rchip = NULL;
1893 
1894 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1895 
1896 	if ((err = pci_enable_device(pci)) < 0)
1897 		return err;
1898 	pci_set_master(pci);
1899 
1900 	/* allocate a chip-specific data */
1901 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1902 	if (!chip) {
1903 		pci_disable_device(pci);
1904 		return -ENOMEM;
1905 	}
1906 	DE_INIT(("chip=%p\n", chip));
1907 
1908 	spin_lock_init(&chip->lock);
1909 	chip->card = card;
1910 	chip->pci = pci;
1911 	chip->irq = -1;
1912 
1913 	/* PCI resource allocation */
1914 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1915 	sz = pci_resource_len(pci, 0);
1916 	if (sz > PAGE_SIZE)
1917 		sz = PAGE_SIZE;		/* We map only the required part */
1918 
1919 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1920 					      ECHOCARD_NAME)) == NULL) {
1921 		snd_echo_free(chip);
1922 		snd_printk(KERN_ERR "cannot get memory region\n");
1923 		return -EBUSY;
1924 	}
1925 	chip->dsp_registers = (volatile u32 __iomem *)
1926 		ioremap_nocache(chip->dsp_registers_phys, sz);
1927 
1928 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1929 			ECHOCARD_NAME, chip)) {
1930 		snd_echo_free(chip);
1931 		snd_printk(KERN_ERR "cannot grab irq\n");
1932 		return -EBUSY;
1933 	}
1934 	chip->irq = pci->irq;
1935 	DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
1936 		 chip->pci, chip->irq, chip->pci->subsystem_device));
1937 
1938 	/* Create the DSP comm page - this is the area of memory used for most
1939 	of the communication with the DSP, which accesses it via bus mastering */
1940 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1941 				sizeof(struct comm_page),
1942 				&chip->commpage_dma_buf) < 0) {
1943 		snd_echo_free(chip);
1944 		snd_printk(KERN_ERR "cannot allocate the comm page\n");
1945 		return -ENOMEM;
1946 	}
1947 	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1948 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1949 
1950 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1951 	if (err) {
1952 		DE_INIT(("init_hw err=%d\n", err));
1953 		snd_echo_free(chip);
1954 		return err;
1955 	}
1956 	DE_INIT(("Card init OK\n"));
1957 
1958 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1959 		snd_echo_free(chip);
1960 		return err;
1961 	}
1962 	atomic_set(&chip->opencount, 0);
1963 	mutex_init(&chip->mode_mutex);
1964 	chip->can_set_rate = 1;
1965 	*rchip = chip;
1966 	/* Init done ! */
1967 	return 0;
1968 }
1969 
1970 
1971 
1972 /* constructor */
1973 static int __devinit snd_echo_probe(struct pci_dev *pci,
1974 				    const struct pci_device_id *pci_id)
1975 {
1976 	static int dev;
1977 	struct snd_card *card;
1978 	struct echoaudio *chip;
1979 	char *dsp;
1980 	int i, err;
1981 
1982 	if (dev >= SNDRV_CARDS)
1983 		return -ENODEV;
1984 	if (!enable[dev]) {
1985 		dev++;
1986 		return -ENOENT;
1987 	}
1988 
1989 	DE_INIT(("Echoaudio driver starting...\n"));
1990 	i = 0;
1991 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
1992 	if (err < 0)
1993 		return err;
1994 
1995 	snd_card_set_dev(card, &pci->dev);
1996 
1997 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
1998 		snd_card_free(card);
1999 		return err;
2000 	}
2001 
2002 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2003 	strcpy(card->shortname, chip->card_name);
2004 
2005 	dsp = "56301";
2006 	if (pci_id->device == 0x3410)
2007 		dsp = "56361";
2008 
2009 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2010 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2011 		chip->dsp_registers_phys, chip->irq);
2012 
2013 	if ((err = snd_echo_new_pcm(chip)) < 0) {
2014 		snd_printk(KERN_ERR "new pcm error %d\n", err);
2015 		snd_card_free(card);
2016 		return err;
2017 	}
2018 
2019 #ifdef ECHOCARD_HAS_MIDI
2020 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2021 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
2022 			snd_printk(KERN_ERR "new midi error %d\n", err);
2023 			snd_card_free(card);
2024 			return err;
2025 		}
2026 	}
2027 #endif
2028 
2029 #ifdef ECHOCARD_HAS_VMIXER
2030 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2031 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2032 		goto ctl_error;
2033 #else
2034 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_pcm_output_gain, chip))) < 0)
2035 		goto ctl_error;
2036 #endif
2037 
2038 #ifdef ECHOCARD_HAS_INPUT_GAIN
2039 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2040 		goto ctl_error;
2041 #endif
2042 
2043 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2044 	if (!chip->hasnt_input_nominal_level)
2045 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2046 			goto ctl_error;
2047 #endif
2048 
2049 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2050 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2051 		goto ctl_error;
2052 #endif
2053 
2054 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2055 		goto ctl_error;
2056 
2057 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2058 		goto ctl_error;
2059 
2060 #ifdef ECHOCARD_HAS_MONITOR
2061 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2062 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2063 		goto ctl_error;
2064 #endif
2065 
2066 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2067 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2068 		goto ctl_error;
2069 #endif
2070 
2071 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2072 		goto ctl_error;
2073 
2074 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2075 	/* Creates a list of available digital modes */
2076 	chip->num_digital_modes = 0;
2077 	for (i = 0; i < 6; i++)
2078 		if (chip->digital_modes & (1 << i))
2079 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2080 
2081 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2082 		goto ctl_error;
2083 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2084 
2085 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2086 	/* Creates a list of available clock sources */
2087 	chip->num_clock_sources = 0;
2088 	for (i = 0; i < 10; i++)
2089 		if (chip->input_clock_types & (1 << i))
2090 			chip->clock_source_list[chip->num_clock_sources++] = i;
2091 
2092 	if (chip->num_clock_sources > 1) {
2093 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2094 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2095 			goto ctl_error;
2096 	}
2097 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2098 
2099 #ifdef ECHOCARD_HAS_DIGITAL_IO
2100 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2101 		goto ctl_error;
2102 #endif
2103 
2104 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2105 	if (chip->has_phantom_power)
2106 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2107 			goto ctl_error;
2108 #endif
2109 
2110 	if ((err = snd_card_register(card)) < 0) {
2111 		snd_card_free(card);
2112 		goto ctl_error;
2113 	}
2114 	snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2115 
2116 	pci_set_drvdata(pci, chip);
2117 	dev++;
2118 	return 0;
2119 
2120 ctl_error:
2121 	snd_printk(KERN_ERR "new control error %d\n", err);
2122 	snd_card_free(card);
2123 	return err;
2124 }
2125 
2126 
2127 
2128 static void __devexit snd_echo_remove(struct pci_dev *pci)
2129 {
2130 	struct echoaudio *chip;
2131 
2132 	chip = pci_get_drvdata(pci);
2133 	if (chip)
2134 		snd_card_free(chip->card);
2135 	pci_set_drvdata(pci, NULL);
2136 }
2137 
2138 
2139 
2140 /******************************************************************************
2141 	Everything starts and ends here
2142 ******************************************************************************/
2143 
2144 /* pci_driver definition */
2145 static struct pci_driver driver = {
2146 	.name = "Echoaudio " ECHOCARD_NAME,
2147 	.id_table = snd_echo_ids,
2148 	.probe = snd_echo_probe,
2149 	.remove = __devexit_p(snd_echo_remove),
2150 };
2151 
2152 
2153 
2154 /* initialization of the module */
2155 static int __init alsa_card_echo_init(void)
2156 {
2157 	return pci_register_driver(&driver);
2158 }
2159 
2160 
2161 
2162 /* clean up the module */
2163 static void __exit alsa_card_echo_exit(void)
2164 {
2165 	pci_unregister_driver(&driver);
2166 }
2167 
2168 
2169 module_init(alsa_card_echo_init)
2170 module_exit(alsa_card_echo_exit)
2171