xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision c0264468)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  ALSA driver for Echoaudio soundcards.
4  *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5  */
6 
7 #include <linux/module.h>
8 
9 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
10 MODULE_LICENSE("GPL v2");
11 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
12 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
13 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
14 
15 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
16 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
17 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
18 
19 module_param_array(index, int, NULL, 0444);
20 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
21 module_param_array(id, charp, NULL, 0444);
22 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
23 module_param_array(enable, bool, NULL, 0444);
24 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
25 
26 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
27 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
28 
29 
30 
31 static int get_firmware(const struct firmware **fw_entry,
32 			struct echoaudio *chip, const short fw_index)
33 {
34 	int err;
35 	char name[30];
36 
37 #ifdef CONFIG_PM_SLEEP
38 	if (chip->fw_cache[fw_index]) {
39 		dev_dbg(chip->card->dev,
40 			"firmware requested: %s is cached\n",
41 			card_fw[fw_index].data);
42 		*fw_entry = chip->fw_cache[fw_index];
43 		return 0;
44 	}
45 #endif
46 
47 	dev_dbg(chip->card->dev,
48 		"firmware requested: %s\n", card_fw[fw_index].data);
49 	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
50 	err = request_firmware(fw_entry, name, &chip->pci->dev);
51 	if (err < 0)
52 		dev_err(chip->card->dev,
53 			"get_firmware(): Firmware not available (%d)\n", err);
54 #ifdef CONFIG_PM_SLEEP
55 	else
56 		chip->fw_cache[fw_index] = *fw_entry;
57 #endif
58 	return err;
59 }
60 
61 
62 
63 static void free_firmware(const struct firmware *fw_entry,
64 			  struct echoaudio *chip)
65 {
66 #ifdef CONFIG_PM_SLEEP
67 	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
68 #else
69 	release_firmware(fw_entry);
70 #endif
71 }
72 
73 
74 
75 static void free_firmware_cache(struct echoaudio *chip)
76 {
77 #ifdef CONFIG_PM_SLEEP
78 	int i;
79 
80 	for (i = 0; i < 8 ; i++)
81 		if (chip->fw_cache[i]) {
82 			release_firmware(chip->fw_cache[i]);
83 			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
84 		}
85 
86 #endif
87 }
88 
89 
90 
91 /******************************************************************************
92 	PCM interface
93 ******************************************************************************/
94 
95 static void audiopipe_free(struct snd_pcm_runtime *runtime)
96 {
97 	struct audiopipe *pipe = runtime->private_data;
98 
99 	if (pipe->sgpage.area)
100 		snd_dma_free_pages(&pipe->sgpage);
101 	kfree(pipe);
102 }
103 
104 
105 
106 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
107 					      struct snd_pcm_hw_rule *rule)
108 {
109 	struct snd_interval *c = hw_param_interval(params,
110 						   SNDRV_PCM_HW_PARAM_CHANNELS);
111 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
112 	struct snd_mask fmt;
113 
114 	snd_mask_any(&fmt);
115 
116 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
117 	/* >=2 channels cannot be S32_BE */
118 	if (c->min == 2) {
119 		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
120 		return snd_mask_refine(f, &fmt);
121 	}
122 #endif
123 	/* > 2 channels cannot be U8 and S32_BE */
124 	if (c->min > 2) {
125 		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
126 		return snd_mask_refine(f, &fmt);
127 	}
128 	/* Mono is ok with any format */
129 	return 0;
130 }
131 
132 
133 
134 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
135 					      struct snd_pcm_hw_rule *rule)
136 {
137 	struct snd_interval *c = hw_param_interval(params,
138 						   SNDRV_PCM_HW_PARAM_CHANNELS);
139 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
140 	struct snd_interval ch;
141 
142 	snd_interval_any(&ch);
143 
144 	/* S32_BE is mono (and stereo) only */
145 	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
146 		ch.min = 1;
147 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
148 		ch.max = 2;
149 #else
150 		ch.max = 1;
151 #endif
152 		ch.integer = 1;
153 		return snd_interval_refine(c, &ch);
154 	}
155 	/* U8 can be only mono or stereo */
156 	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
157 		ch.min = 1;
158 		ch.max = 2;
159 		ch.integer = 1;
160 		return snd_interval_refine(c, &ch);
161 	}
162 	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
163 	return 0;
164 }
165 
166 
167 
168 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
169 					       struct snd_pcm_hw_rule *rule)
170 {
171 	struct snd_interval *c = hw_param_interval(params,
172 						   SNDRV_PCM_HW_PARAM_CHANNELS);
173 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
174 	struct snd_mask fmt;
175 	u64 fmask;
176 	snd_mask_any(&fmt);
177 
178 	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
179 
180 	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
181 	if (c->min > 2) {
182 		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
183 			 SNDRV_PCM_FMTBIT_S24_3LE |
184 			 SNDRV_PCM_FMTBIT_S32_LE;
185 	/* 1 channel must be S32_BE or S32_LE */
186 	} else if (c->max == 1)
187 		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
188 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
189 	/* 2 channels cannot be S32_BE */
190 	else if (c->min == 2 && c->max == 2)
191 		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
192 #endif
193 	else
194 		return 0;
195 
196 	fmt.bits[0] &= (u32)fmask;
197 	fmt.bits[1] &= (u32)(fmask >> 32);
198 	return snd_mask_refine(f, &fmt);
199 }
200 
201 
202 
203 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
204 					       struct snd_pcm_hw_rule *rule)
205 {
206 	struct snd_interval *c = hw_param_interval(params,
207 						   SNDRV_PCM_HW_PARAM_CHANNELS);
208 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
209 	struct snd_interval ch;
210 	u64 fmask;
211 
212 	snd_interval_any(&ch);
213 	ch.integer = 1;
214 	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
215 
216 	/* S32_BE is mono (and stereo) only */
217 	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
218 		ch.min = 1;
219 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
220 		ch.max = 2;
221 #else
222 		ch.max = 1;
223 #endif
224 	/* U8 is stereo only */
225 	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
226 		ch.min = ch.max = 2;
227 	/* S16_LE and S24_3LE must be at least stereo */
228 	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
229 			       SNDRV_PCM_FMTBIT_S24_3LE)))
230 		ch.min = 2;
231 	else
232 		return 0;
233 
234 	return snd_interval_refine(c, &ch);
235 }
236 
237 
238 
239 /* Since the sample rate is a global setting, do allow the user to change the
240 sample rate only if there is only one pcm device open. */
241 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
242 			       struct snd_pcm_hw_rule *rule)
243 {
244 	struct snd_interval *rate = hw_param_interval(params,
245 						      SNDRV_PCM_HW_PARAM_RATE);
246 	struct echoaudio *chip = rule->private;
247 	struct snd_interval fixed;
248 
249 	if (!chip->can_set_rate) {
250 		snd_interval_any(&fixed);
251 		fixed.min = fixed.max = chip->sample_rate;
252 		return snd_interval_refine(rate, &fixed);
253 	}
254 	return 0;
255 }
256 
257 
258 static int pcm_open(struct snd_pcm_substream *substream,
259 		    signed char max_channels)
260 {
261 	struct echoaudio *chip;
262 	struct snd_pcm_runtime *runtime;
263 	struct audiopipe *pipe;
264 	int err, i;
265 
266 	if (max_channels <= 0)
267 		return -EAGAIN;
268 
269 	chip = snd_pcm_substream_chip(substream);
270 	runtime = substream->runtime;
271 
272 	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
273 	if (!pipe)
274 		return -ENOMEM;
275 	pipe->index = -1;		/* Not configured yet */
276 
277 	/* Set up hw capabilities and contraints */
278 	memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
279 	dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
280 	pipe->constr.list = channels_list;
281 	pipe->constr.mask = 0;
282 	for (i = 0; channels_list[i] <= max_channels; i++);
283 	pipe->constr.count = i;
284 	if (pipe->hw.channels_max > max_channels)
285 		pipe->hw.channels_max = max_channels;
286 	if (chip->digital_mode == DIGITAL_MODE_ADAT) {
287 		pipe->hw.rate_max = 48000;
288 		pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
289 	}
290 
291 	runtime->hw = pipe->hw;
292 	runtime->private_data = pipe;
293 	runtime->private_free = audiopipe_free;
294 	snd_pcm_set_sync(substream);
295 
296 	/* Only mono and any even number of channels are allowed */
297 	if ((err = snd_pcm_hw_constraint_list(runtime, 0,
298 					      SNDRV_PCM_HW_PARAM_CHANNELS,
299 					      &pipe->constr)) < 0)
300 		return err;
301 
302 	/* All periods should have the same size */
303 	if ((err = snd_pcm_hw_constraint_integer(runtime,
304 						 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
305 		return err;
306 
307 	/* The hw accesses memory in chunks 32 frames long and they should be
308 	32-bytes-aligned. It's not a requirement, but it seems that IRQs are
309 	generated with a resolution of 32 frames. Thus we need the following */
310 	if ((err = snd_pcm_hw_constraint_step(runtime, 0,
311 					      SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
312 					      32)) < 0)
313 		return err;
314 	if ((err = snd_pcm_hw_constraint_step(runtime, 0,
315 					      SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
316 					      32)) < 0)
317 		return err;
318 
319 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
320 				       SNDRV_PCM_HW_PARAM_RATE,
321 					hw_rule_sample_rate, chip,
322 				       SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
323 		return err;
324 
325 	/* Finally allocate a page for the scatter-gather list */
326 	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
327 				       &chip->pci->dev,
328 				       PAGE_SIZE, &pipe->sgpage)) < 0) {
329 		dev_err(chip->card->dev, "s-g list allocation failed\n");
330 		return err;
331 	}
332 
333 	return 0;
334 }
335 
336 
337 
338 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
339 {
340 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
341 	int err;
342 
343 	if ((err = pcm_open(substream, num_analog_busses_in(chip) -
344 			    substream->number)) < 0)
345 		return err;
346 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
347 				       SNDRV_PCM_HW_PARAM_CHANNELS,
348 				       hw_rule_capture_channels_by_format, NULL,
349 				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
350 		return err;
351 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
352 				       SNDRV_PCM_HW_PARAM_FORMAT,
353 				       hw_rule_capture_format_by_channels, NULL,
354 				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
355 		return err;
356 	atomic_inc(&chip->opencount);
357 	if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
358 		chip->can_set_rate=0;
359 	dev_dbg(chip->card->dev, "pcm_analog_in_open  cs=%d  oc=%d  r=%d\n",
360 		chip->can_set_rate, atomic_read(&chip->opencount),
361 		chip->sample_rate);
362 	return 0;
363 }
364 
365 
366 
367 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
368 {
369 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
370 	int max_channels, err;
371 
372 #ifdef ECHOCARD_HAS_VMIXER
373 	max_channels = num_pipes_out(chip);
374 #else
375 	max_channels = num_analog_busses_out(chip);
376 #endif
377 	if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
378 		return err;
379 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
380 				       SNDRV_PCM_HW_PARAM_CHANNELS,
381 				       hw_rule_playback_channels_by_format,
382 				       NULL,
383 				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
384 		return err;
385 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
386 				       SNDRV_PCM_HW_PARAM_FORMAT,
387 				       hw_rule_playback_format_by_channels,
388 				       NULL,
389 				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
390 		return err;
391 	atomic_inc(&chip->opencount);
392 	if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
393 		chip->can_set_rate=0;
394 	dev_dbg(chip->card->dev, "pcm_analog_out_open  cs=%d  oc=%d  r=%d\n",
395 		chip->can_set_rate, atomic_read(&chip->opencount),
396 		chip->sample_rate);
397 	return 0;
398 }
399 
400 
401 
402 #ifdef ECHOCARD_HAS_DIGITAL_IO
403 
404 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
405 {
406 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
407 	int err, max_channels;
408 
409 	max_channels = num_digital_busses_in(chip) - substream->number;
410 	mutex_lock(&chip->mode_mutex);
411 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
412 		err = pcm_open(substream, max_channels);
413 	else	/* If the card has ADAT, subtract the 6 channels
414 		 * that S/PDIF doesn't have
415 		 */
416 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
417 
418 	if (err < 0)
419 		goto din_exit;
420 
421 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
422 				       SNDRV_PCM_HW_PARAM_CHANNELS,
423 				       hw_rule_capture_channels_by_format, NULL,
424 				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
425 		goto din_exit;
426 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
427 				       SNDRV_PCM_HW_PARAM_FORMAT,
428 				       hw_rule_capture_format_by_channels, NULL,
429 				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
430 		goto din_exit;
431 
432 	atomic_inc(&chip->opencount);
433 	if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
434 		chip->can_set_rate=0;
435 
436 din_exit:
437 	mutex_unlock(&chip->mode_mutex);
438 	return err;
439 }
440 
441 
442 
443 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
444 
445 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
446 {
447 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
448 	int err, max_channels;
449 
450 	max_channels = num_digital_busses_out(chip) - substream->number;
451 	mutex_lock(&chip->mode_mutex);
452 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
453 		err = pcm_open(substream, max_channels);
454 	else	/* If the card has ADAT, subtract the 6 channels
455 		 * that S/PDIF doesn't have
456 		 */
457 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
458 
459 	if (err < 0)
460 		goto dout_exit;
461 
462 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
463 				       SNDRV_PCM_HW_PARAM_CHANNELS,
464 				       hw_rule_playback_channels_by_format,
465 				       NULL, SNDRV_PCM_HW_PARAM_FORMAT,
466 				       -1)) < 0)
467 		goto dout_exit;
468 	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
469 				       SNDRV_PCM_HW_PARAM_FORMAT,
470 				       hw_rule_playback_format_by_channels,
471 				       NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
472 				       -1)) < 0)
473 		goto dout_exit;
474 	atomic_inc(&chip->opencount);
475 	if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
476 		chip->can_set_rate=0;
477 dout_exit:
478 	mutex_unlock(&chip->mode_mutex);
479 	return err;
480 }
481 
482 #endif /* !ECHOCARD_HAS_VMIXER */
483 
484 #endif /* ECHOCARD_HAS_DIGITAL_IO */
485 
486 
487 
488 static int pcm_close(struct snd_pcm_substream *substream)
489 {
490 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
491 	int oc;
492 
493 	/* Nothing to do here. Audio is already off and pipe will be
494 	 * freed by its callback
495 	 */
496 
497 	atomic_dec(&chip->opencount);
498 	oc = atomic_read(&chip->opencount);
499 	dev_dbg(chip->card->dev, "pcm_close  oc=%d  cs=%d  rs=%d\n", oc,
500 		chip->can_set_rate, chip->rate_set);
501 	if (oc < 2)
502 		chip->can_set_rate = 1;
503 	if (oc == 0)
504 		chip->rate_set = 0;
505 	dev_dbg(chip->card->dev, "pcm_close2 oc=%d  cs=%d  rs=%d\n", oc,
506 		chip->can_set_rate, chip->rate_set);
507 
508 	return 0;
509 }
510 
511 
512 
513 /* Channel allocation and scatter-gather list setup */
514 static int init_engine(struct snd_pcm_substream *substream,
515 		       struct snd_pcm_hw_params *hw_params,
516 		       int pipe_index, int interleave)
517 {
518 	struct echoaudio *chip;
519 	int err, per, rest, page, edge, offs;
520 	struct audiopipe *pipe;
521 
522 	chip = snd_pcm_substream_chip(substream);
523 	pipe = (struct audiopipe *) substream->runtime->private_data;
524 
525 	/* Sets up che hardware. If it's already initialized, reset and
526 	 * redo with the new parameters
527 	 */
528 	spin_lock_irq(&chip->lock);
529 	if (pipe->index >= 0) {
530 		dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
531 		err = free_pipes(chip, pipe);
532 		snd_BUG_ON(err);
533 		chip->substream[pipe->index] = NULL;
534 	}
535 
536 	err = allocate_pipes(chip, pipe, pipe_index, interleave);
537 	if (err < 0) {
538 		spin_unlock_irq(&chip->lock);
539 		dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
540 			pipe_index, err);
541 		return err;
542 	}
543 	spin_unlock_irq(&chip->lock);
544 	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
545 
546 	dev_dbg(chip->card->dev,
547 		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
548 		params_buffer_bytes(hw_params), params_periods(hw_params),
549 		params_period_bytes(hw_params));
550 
551 	sglist_init(chip, pipe);
552 	edge = PAGE_SIZE;
553 	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
554 	     per++) {
555 		rest = params_period_bytes(hw_params);
556 		if (offs + rest > params_buffer_bytes(hw_params))
557 			rest = params_buffer_bytes(hw_params) - offs;
558 		while (rest) {
559 			dma_addr_t addr;
560 			addr = snd_pcm_sgbuf_get_addr(substream, offs);
561 			if (rest <= edge - offs) {
562 				sglist_add_mapping(chip, pipe, addr, rest);
563 				sglist_add_irq(chip, pipe);
564 				offs += rest;
565 				rest = 0;
566 			} else {
567 				sglist_add_mapping(chip, pipe, addr,
568 						   edge - offs);
569 				rest -= edge - offs;
570 				offs = edge;
571 			}
572 			if (offs == edge) {
573 				edge += PAGE_SIZE;
574 				page++;
575 			}
576 		}
577 	}
578 
579 	/* Close the ring buffer */
580 	sglist_wrap(chip, pipe);
581 
582 	/* This stuff is used by the irq handler, so it must be
583 	 * initialized before chip->substream
584 	 */
585 	chip->last_period[pipe_index] = 0;
586 	pipe->last_counter = 0;
587 	pipe->position = 0;
588 	smp_wmb();
589 	chip->substream[pipe_index] = substream;
590 	chip->rate_set = 1;
591 	spin_lock_irq(&chip->lock);
592 	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
593 	spin_unlock_irq(&chip->lock);
594 	return 0;
595 }
596 
597 
598 
599 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
600 				   struct snd_pcm_hw_params *hw_params)
601 {
602 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
603 
604 	return init_engine(substream, hw_params, px_analog_in(chip) +
605 			substream->number, params_channels(hw_params));
606 }
607 
608 
609 
610 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
611 				    struct snd_pcm_hw_params *hw_params)
612 {
613 	return init_engine(substream, hw_params, substream->number,
614 			   params_channels(hw_params));
615 }
616 
617 
618 
619 #ifdef ECHOCARD_HAS_DIGITAL_IO
620 
621 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
622 				    struct snd_pcm_hw_params *hw_params)
623 {
624 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
625 
626 	return init_engine(substream, hw_params, px_digital_in(chip) +
627 			substream->number, params_channels(hw_params));
628 }
629 
630 
631 
632 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
633 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
634 				     struct snd_pcm_hw_params *hw_params)
635 {
636 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
637 
638 	return init_engine(substream, hw_params, px_digital_out(chip) +
639 			substream->number, params_channels(hw_params));
640 }
641 #endif /* !ECHOCARD_HAS_VMIXER */
642 
643 #endif /* ECHOCARD_HAS_DIGITAL_IO */
644 
645 
646 
647 static int pcm_hw_free(struct snd_pcm_substream *substream)
648 {
649 	struct echoaudio *chip;
650 	struct audiopipe *pipe;
651 
652 	chip = snd_pcm_substream_chip(substream);
653 	pipe = (struct audiopipe *) substream->runtime->private_data;
654 
655 	spin_lock_irq(&chip->lock);
656 	if (pipe->index >= 0) {
657 		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
658 		free_pipes(chip, pipe);
659 		chip->substream[pipe->index] = NULL;
660 		pipe->index = -1;
661 	}
662 	spin_unlock_irq(&chip->lock);
663 
664 	return 0;
665 }
666 
667 
668 
669 static int pcm_prepare(struct snd_pcm_substream *substream)
670 {
671 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
672 	struct snd_pcm_runtime *runtime = substream->runtime;
673 	struct audioformat format;
674 	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
675 
676 	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
677 		runtime->rate, runtime->format, runtime->channels);
678 	format.interleave = runtime->channels;
679 	format.data_are_bigendian = 0;
680 	format.mono_to_stereo = 0;
681 	switch (runtime->format) {
682 	case SNDRV_PCM_FORMAT_U8:
683 		format.bits_per_sample = 8;
684 		break;
685 	case SNDRV_PCM_FORMAT_S16_LE:
686 		format.bits_per_sample = 16;
687 		break;
688 	case SNDRV_PCM_FORMAT_S24_3LE:
689 		format.bits_per_sample = 24;
690 		break;
691 	case SNDRV_PCM_FORMAT_S32_BE:
692 		format.data_are_bigendian = 1;
693 		/* fall through */
694 	case SNDRV_PCM_FORMAT_S32_LE:
695 		format.bits_per_sample = 32;
696 		break;
697 	default:
698 		dev_err(chip->card->dev,
699 			"Prepare error: unsupported format %d\n",
700 			runtime->format);
701 		return -EINVAL;
702 	}
703 
704 	if (snd_BUG_ON(pipe_index >= px_num(chip)))
705 		return -EINVAL;
706 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
707 		return -EINVAL;
708 	set_audio_format(chip, pipe_index, &format);
709 	return 0;
710 }
711 
712 
713 
714 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
715 {
716 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
717 	struct audiopipe *pipe;
718 	int i, err;
719 	u32 channelmask = 0;
720 	struct snd_pcm_substream *s;
721 
722 	snd_pcm_group_for_each_entry(s, substream) {
723 		for (i = 0; i < DSP_MAXPIPES; i++) {
724 			if (s == chip->substream[i]) {
725 				channelmask |= 1 << i;
726 				snd_pcm_trigger_done(s, substream);
727 			}
728 		}
729 	}
730 
731 	spin_lock(&chip->lock);
732 	switch (cmd) {
733 	case SNDRV_PCM_TRIGGER_RESUME:
734 	case SNDRV_PCM_TRIGGER_START:
735 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
736 		for (i = 0; i < DSP_MAXPIPES; i++) {
737 			if (channelmask & (1 << i)) {
738 				pipe = chip->substream[i]->runtime->private_data;
739 				switch (pipe->state) {
740 				case PIPE_STATE_STOPPED:
741 					chip->last_period[i] = 0;
742 					pipe->last_counter = 0;
743 					pipe->position = 0;
744 					*pipe->dma_counter = 0;
745 					/* fall through */
746 				case PIPE_STATE_PAUSED:
747 					pipe->state = PIPE_STATE_STARTED;
748 					break;
749 				case PIPE_STATE_STARTED:
750 					break;
751 				}
752 			}
753 		}
754 		err = start_transport(chip, channelmask,
755 				      chip->pipe_cyclic_mask);
756 		break;
757 	case SNDRV_PCM_TRIGGER_SUSPEND:
758 	case SNDRV_PCM_TRIGGER_STOP:
759 		for (i = 0; i < DSP_MAXPIPES; i++) {
760 			if (channelmask & (1 << i)) {
761 				pipe = chip->substream[i]->runtime->private_data;
762 				pipe->state = PIPE_STATE_STOPPED;
763 			}
764 		}
765 		err = stop_transport(chip, channelmask);
766 		break;
767 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
768 		for (i = 0; i < DSP_MAXPIPES; i++) {
769 			if (channelmask & (1 << i)) {
770 				pipe = chip->substream[i]->runtime->private_data;
771 				pipe->state = PIPE_STATE_PAUSED;
772 			}
773 		}
774 		err = pause_transport(chip, channelmask);
775 		break;
776 	default:
777 		err = -EINVAL;
778 	}
779 	spin_unlock(&chip->lock);
780 	return err;
781 }
782 
783 
784 
785 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
786 {
787 	struct snd_pcm_runtime *runtime = substream->runtime;
788 	struct audiopipe *pipe = runtime->private_data;
789 	size_t cnt, bufsize, pos;
790 
791 	cnt = le32_to_cpu(*pipe->dma_counter);
792 	pipe->position += cnt - pipe->last_counter;
793 	pipe->last_counter = cnt;
794 	bufsize = substream->runtime->buffer_size;
795 	pos = bytes_to_frames(substream->runtime, pipe->position);
796 
797 	while (pos >= bufsize) {
798 		pipe->position -= frames_to_bytes(substream->runtime, bufsize);
799 		pos -= bufsize;
800 	}
801 	return pos;
802 }
803 
804 
805 
806 /* pcm *_ops structures */
807 static const struct snd_pcm_ops analog_playback_ops = {
808 	.open = pcm_analog_out_open,
809 	.close = pcm_close,
810 	.ioctl = snd_pcm_lib_ioctl,
811 	.hw_params = pcm_analog_out_hw_params,
812 	.hw_free = pcm_hw_free,
813 	.prepare = pcm_prepare,
814 	.trigger = pcm_trigger,
815 	.pointer = pcm_pointer,
816 };
817 static const struct snd_pcm_ops analog_capture_ops = {
818 	.open = pcm_analog_in_open,
819 	.close = pcm_close,
820 	.ioctl = snd_pcm_lib_ioctl,
821 	.hw_params = pcm_analog_in_hw_params,
822 	.hw_free = pcm_hw_free,
823 	.prepare = pcm_prepare,
824 	.trigger = pcm_trigger,
825 	.pointer = pcm_pointer,
826 };
827 #ifdef ECHOCARD_HAS_DIGITAL_IO
828 #ifndef ECHOCARD_HAS_VMIXER
829 static const struct snd_pcm_ops digital_playback_ops = {
830 	.open = pcm_digital_out_open,
831 	.close = pcm_close,
832 	.ioctl = snd_pcm_lib_ioctl,
833 	.hw_params = pcm_digital_out_hw_params,
834 	.hw_free = pcm_hw_free,
835 	.prepare = pcm_prepare,
836 	.trigger = pcm_trigger,
837 	.pointer = pcm_pointer,
838 };
839 #endif /* !ECHOCARD_HAS_VMIXER */
840 static const struct snd_pcm_ops digital_capture_ops = {
841 	.open = pcm_digital_in_open,
842 	.close = pcm_close,
843 	.ioctl = snd_pcm_lib_ioctl,
844 	.hw_params = pcm_digital_in_hw_params,
845 	.hw_free = pcm_hw_free,
846 	.prepare = pcm_prepare,
847 	.trigger = pcm_trigger,
848 	.pointer = pcm_pointer,
849 };
850 #endif /* ECHOCARD_HAS_DIGITAL_IO */
851 
852 
853 
854 /* Preallocate memory only for the first substream because it's the most
855  * used one
856  */
857 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
858 {
859 	struct snd_pcm_substream *ss;
860 	int stream;
861 
862 	for (stream = 0; stream < 2; stream++)
863 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
864 			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
865 						   dev,
866 						   ss->number ? 0 : 128<<10,
867 						   256<<10);
868 }
869 
870 
871 
872 /*<--snd_echo_probe() */
873 static int snd_echo_new_pcm(struct echoaudio *chip)
874 {
875 	struct snd_pcm *pcm;
876 	int err;
877 
878 #ifdef ECHOCARD_HAS_VMIXER
879 	/* This card has a Vmixer, that is there is no direct mapping from PCM
880 	streams to physical outputs. The user can mix the streams as he wishes
881 	via control interface and it's possible to send any stream to any
882 	output, thus it makes no sense to keep analog and digital outputs
883 	separated */
884 
885 	/* PCM#0 Virtual outputs and analog inputs */
886 	if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
887 				num_analog_busses_in(chip), &pcm)) < 0)
888 		return err;
889 	pcm->private_data = chip;
890 	chip->analog_pcm = pcm;
891 	strcpy(pcm->name, chip->card->shortname);
892 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
893 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
894 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
895 
896 #ifdef ECHOCARD_HAS_DIGITAL_IO
897 	/* PCM#1 Digital inputs, no outputs */
898 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
899 			       num_digital_busses_in(chip), &pcm)) < 0)
900 		return err;
901 	pcm->private_data = chip;
902 	chip->digital_pcm = pcm;
903 	strcpy(pcm->name, chip->card->shortname);
904 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
905 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
906 #endif /* ECHOCARD_HAS_DIGITAL_IO */
907 
908 #else /* ECHOCARD_HAS_VMIXER */
909 
910 	/* The card can manage substreams formed by analog and digital channels
911 	at the same time, but I prefer to keep analog and digital channels
912 	separated, because that mixed thing is confusing and useless. So we
913 	register two PCM devices: */
914 
915 	/* PCM#0 Analog i/o */
916 	if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
917 			       num_analog_busses_out(chip),
918 			       num_analog_busses_in(chip), &pcm)) < 0)
919 		return err;
920 	pcm->private_data = chip;
921 	chip->analog_pcm = pcm;
922 	strcpy(pcm->name, chip->card->shortname);
923 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
924 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
925 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
926 
927 #ifdef ECHOCARD_HAS_DIGITAL_IO
928 	/* PCM#1 Digital i/o */
929 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
930 			       num_digital_busses_out(chip),
931 			       num_digital_busses_in(chip), &pcm)) < 0)
932 		return err;
933 	pcm->private_data = chip;
934 	chip->digital_pcm = pcm;
935 	strcpy(pcm->name, chip->card->shortname);
936 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
937 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
938 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
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 const struct snd_kcontrol_new snd_echo_line_output_gain = {
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 const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
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 const struct snd_kcontrol_new snd_echo_line_input_gain = {
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 const struct snd_kcontrol_new snd_echo_output_nominal_level = {
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 const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
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 = snd_kcontrol_chip(kcontrol);
1253 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1254 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1255 
1256 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1257 		return -EINVAL;
1258 
1259 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1260 	return 0;
1261 }
1262 
1263 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1264 			      struct snd_ctl_elem_value *ucontrol)
1265 {
1266 	struct echoaudio *chip;
1267 	int changed,  gain;
1268 	unsigned int out, in;
1269 
1270 	changed = 0;
1271 	chip = snd_kcontrol_chip(kcontrol);
1272 	out = ucontrol->id.index / num_busses_in(chip);
1273 	in = ucontrol->id.index % num_busses_in(chip);
1274 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1275 		return -EINVAL;
1276 	gain = ucontrol->value.integer.value[0];
1277 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1278 		return -EINVAL;
1279 	if (chip->monitor_gain[out][in] != gain) {
1280 		spin_lock_irq(&chip->lock);
1281 		set_monitor_gain(chip, out, in, gain);
1282 		update_output_line_level(chip);
1283 		spin_unlock_irq(&chip->lock);
1284 		changed = 1;
1285 	}
1286 	return changed;
1287 }
1288 
1289 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1290 	.name = "Monitor Mixer Volume",
1291 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1292 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1293 	.info = snd_echo_mixer_info,
1294 	.get = snd_echo_mixer_get,
1295 	.put = snd_echo_mixer_put,
1296 	.tlv = {.p = db_scale_output_gain},
1297 };
1298 
1299 #endif /* ECHOCARD_HAS_MONITOR */
1300 
1301 
1302 
1303 #ifdef ECHOCARD_HAS_VMIXER
1304 
1305 /******************* Vmixer *******************/
1306 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1307 				struct snd_ctl_elem_info *uinfo)
1308 {
1309 	struct echoaudio *chip;
1310 
1311 	chip = snd_kcontrol_chip(kcontrol);
1312 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1313 	uinfo->count = 1;
1314 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1315 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1316 	uinfo->dimen.d[0] = num_busses_out(chip);
1317 	uinfo->dimen.d[1] = num_pipes_out(chip);
1318 	return 0;
1319 }
1320 
1321 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1322 			       struct snd_ctl_elem_value *ucontrol)
1323 {
1324 	struct echoaudio *chip;
1325 
1326 	chip = snd_kcontrol_chip(kcontrol);
1327 	ucontrol->value.integer.value[0] =
1328 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1329 			[ucontrol->id.index % num_pipes_out(chip)];
1330 	return 0;
1331 }
1332 
1333 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1334 			       struct snd_ctl_elem_value *ucontrol)
1335 {
1336 	struct echoaudio *chip;
1337 	int gain, changed;
1338 	short vch, out;
1339 
1340 	changed = 0;
1341 	chip = snd_kcontrol_chip(kcontrol);
1342 	out = ucontrol->id.index / num_pipes_out(chip);
1343 	vch = ucontrol->id.index % num_pipes_out(chip);
1344 	gain = ucontrol->value.integer.value[0];
1345 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1346 		return -EINVAL;
1347 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1348 		spin_lock_irq(&chip->lock);
1349 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1350 		update_vmixer_level(chip);
1351 		spin_unlock_irq(&chip->lock);
1352 		changed = 1;
1353 	}
1354 	return changed;
1355 }
1356 
1357 static struct snd_kcontrol_new snd_echo_vmixer = {
1358 	.name = "VMixer Volume",
1359 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1360 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1361 	.info = snd_echo_vmixer_info,
1362 	.get = snd_echo_vmixer_get,
1363 	.put = snd_echo_vmixer_put,
1364 	.tlv = {.p = db_scale_output_gain},
1365 };
1366 
1367 #endif /* ECHOCARD_HAS_VMIXER */
1368 
1369 
1370 
1371 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1372 
1373 /******************* Digital mode switch *******************/
1374 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1375 				      struct snd_ctl_elem_info *uinfo)
1376 {
1377 	static const char * const names[4] = {
1378 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1379 		"S/PDIF Cdrom"
1380 	};
1381 	struct echoaudio *chip;
1382 
1383 	chip = snd_kcontrol_chip(kcontrol);
1384 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1385 }
1386 
1387 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1388 				     struct snd_ctl_elem_value *ucontrol)
1389 {
1390 	struct echoaudio *chip;
1391 	int i, mode;
1392 
1393 	chip = snd_kcontrol_chip(kcontrol);
1394 	mode = chip->digital_mode;
1395 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1396 		if (mode == chip->digital_mode_list[i]) {
1397 			ucontrol->value.enumerated.item[0] = i;
1398 			break;
1399 		}
1400 	return 0;
1401 }
1402 
1403 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1404 				     struct snd_ctl_elem_value *ucontrol)
1405 {
1406 	struct echoaudio *chip;
1407 	int changed;
1408 	unsigned short emode, dmode;
1409 
1410 	changed = 0;
1411 	chip = snd_kcontrol_chip(kcontrol);
1412 
1413 	emode = ucontrol->value.enumerated.item[0];
1414 	if (emode >= chip->num_digital_modes)
1415 		return -EINVAL;
1416 	dmode = chip->digital_mode_list[emode];
1417 
1418 	if (dmode != chip->digital_mode) {
1419 		/* mode_mutex is required to make this operation atomic wrt
1420 		pcm_digital_*_open() and set_input_clock() functions. */
1421 		mutex_lock(&chip->mode_mutex);
1422 
1423 		/* Do not allow the user to change the digital mode when a pcm
1424 		device is open because it also changes the number of channels
1425 		and the allowed sample rates */
1426 		if (atomic_read(&chip->opencount)) {
1427 			changed = -EAGAIN;
1428 		} else {
1429 			changed = set_digital_mode(chip, dmode);
1430 			/* If we had to change the clock source, report it */
1431 			if (changed > 0 && chip->clock_src_ctl) {
1432 				snd_ctl_notify(chip->card,
1433 					       SNDRV_CTL_EVENT_MASK_VALUE,
1434 					       &chip->clock_src_ctl->id);
1435 				dev_dbg(chip->card->dev,
1436 					"SDM() =%d\n", changed);
1437 			}
1438 			if (changed >= 0)
1439 				changed = 1;	/* No errors */
1440 		}
1441 		mutex_unlock(&chip->mode_mutex);
1442 	}
1443 	return changed;
1444 }
1445 
1446 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1447 	.name = "Digital mode Switch",
1448 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1449 	.info = snd_echo_digital_mode_info,
1450 	.get = snd_echo_digital_mode_get,
1451 	.put = snd_echo_digital_mode_put,
1452 };
1453 
1454 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1455 
1456 
1457 
1458 #ifdef ECHOCARD_HAS_DIGITAL_IO
1459 
1460 /******************* S/PDIF mode switch *******************/
1461 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1462 				    struct snd_ctl_elem_info *uinfo)
1463 {
1464 	static const char * const names[2] = {"Consumer", "Professional"};
1465 
1466 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1467 }
1468 
1469 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1470 				   struct snd_ctl_elem_value *ucontrol)
1471 {
1472 	struct echoaudio *chip;
1473 
1474 	chip = snd_kcontrol_chip(kcontrol);
1475 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1476 	return 0;
1477 }
1478 
1479 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1480 				   struct snd_ctl_elem_value *ucontrol)
1481 {
1482 	struct echoaudio *chip;
1483 	int mode;
1484 
1485 	chip = snd_kcontrol_chip(kcontrol);
1486 	mode = !!ucontrol->value.enumerated.item[0];
1487 	if (mode != chip->professional_spdif) {
1488 		spin_lock_irq(&chip->lock);
1489 		set_professional_spdif(chip, mode);
1490 		spin_unlock_irq(&chip->lock);
1491 		return 1;
1492 	}
1493 	return 0;
1494 }
1495 
1496 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1497 	.name = "S/PDIF mode Switch",
1498 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1499 	.info = snd_echo_spdif_mode_info,
1500 	.get = snd_echo_spdif_mode_get,
1501 	.put = snd_echo_spdif_mode_put,
1502 };
1503 
1504 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1505 
1506 
1507 
1508 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1509 
1510 /******************* Select input clock source *******************/
1511 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1512 				      struct snd_ctl_elem_info *uinfo)
1513 {
1514 	static const char * const names[8] = {
1515 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1516 		"ESync96", "MTC"
1517 	};
1518 	struct echoaudio *chip;
1519 
1520 	chip = snd_kcontrol_chip(kcontrol);
1521 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1522 }
1523 
1524 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1525 				     struct snd_ctl_elem_value *ucontrol)
1526 {
1527 	struct echoaudio *chip;
1528 	int i, clock;
1529 
1530 	chip = snd_kcontrol_chip(kcontrol);
1531 	clock = chip->input_clock;
1532 
1533 	for (i = 0; i < chip->num_clock_sources; i++)
1534 		if (clock == chip->clock_source_list[i])
1535 			ucontrol->value.enumerated.item[0] = i;
1536 
1537 	return 0;
1538 }
1539 
1540 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1541 				     struct snd_ctl_elem_value *ucontrol)
1542 {
1543 	struct echoaudio *chip;
1544 	int changed;
1545 	unsigned int eclock, dclock;
1546 
1547 	changed = 0;
1548 	chip = snd_kcontrol_chip(kcontrol);
1549 	eclock = ucontrol->value.enumerated.item[0];
1550 	if (eclock >= chip->input_clock_types)
1551 		return -EINVAL;
1552 	dclock = chip->clock_source_list[eclock];
1553 	if (chip->input_clock != dclock) {
1554 		mutex_lock(&chip->mode_mutex);
1555 		spin_lock_irq(&chip->lock);
1556 		if ((changed = set_input_clock(chip, dclock)) == 0)
1557 			changed = 1;	/* no errors */
1558 		spin_unlock_irq(&chip->lock);
1559 		mutex_unlock(&chip->mode_mutex);
1560 	}
1561 
1562 	if (changed < 0)
1563 		dev_dbg(chip->card->dev,
1564 			"seticlk val%d err 0x%x\n", dclock, changed);
1565 
1566 	return changed;
1567 }
1568 
1569 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1570 	.name = "Sample Clock Source",
1571 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1572 	.info = snd_echo_clock_source_info,
1573 	.get = snd_echo_clock_source_get,
1574 	.put = snd_echo_clock_source_put,
1575 };
1576 
1577 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1578 
1579 
1580 
1581 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1582 
1583 /******************* Phantom power switch *******************/
1584 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1585 
1586 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1587 				      struct snd_ctl_elem_value *ucontrol)
1588 {
1589 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1590 
1591 	ucontrol->value.integer.value[0] = chip->phantom_power;
1592 	return 0;
1593 }
1594 
1595 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1596 				      struct snd_ctl_elem_value *ucontrol)
1597 {
1598 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1599 	int power, changed = 0;
1600 
1601 	power = !!ucontrol->value.integer.value[0];
1602 	if (chip->phantom_power != power) {
1603 		spin_lock_irq(&chip->lock);
1604 		changed = set_phantom_power(chip, power);
1605 		spin_unlock_irq(&chip->lock);
1606 		if (changed == 0)
1607 			changed = 1;	/* no errors */
1608 	}
1609 	return changed;
1610 }
1611 
1612 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1613 	.name = "Phantom power Switch",
1614 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1615 	.info = snd_echo_phantom_power_info,
1616 	.get = snd_echo_phantom_power_get,
1617 	.put = snd_echo_phantom_power_put,
1618 };
1619 
1620 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1621 
1622 
1623 
1624 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1625 
1626 /******************* Digital input automute switch *******************/
1627 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1628 
1629 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1630 				 struct snd_ctl_elem_value *ucontrol)
1631 {
1632 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1633 
1634 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1635 	return 0;
1636 }
1637 
1638 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1639 				 struct snd_ctl_elem_value *ucontrol)
1640 {
1641 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1642 	int automute, changed = 0;
1643 
1644 	automute = !!ucontrol->value.integer.value[0];
1645 	if (chip->digital_in_automute != automute) {
1646 		spin_lock_irq(&chip->lock);
1647 		changed = set_input_auto_mute(chip, automute);
1648 		spin_unlock_irq(&chip->lock);
1649 		if (changed == 0)
1650 			changed = 1;	/* no errors */
1651 	}
1652 	return changed;
1653 }
1654 
1655 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1656 	.name = "Digital Capture Switch (automute)",
1657 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1658 	.info = snd_echo_automute_info,
1659 	.get = snd_echo_automute_get,
1660 	.put = snd_echo_automute_put,
1661 };
1662 
1663 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1664 
1665 
1666 
1667 /******************* VU-meters switch *******************/
1668 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1669 
1670 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1671 					struct snd_ctl_elem_value *ucontrol)
1672 {
1673 	struct echoaudio *chip;
1674 
1675 	chip = snd_kcontrol_chip(kcontrol);
1676 	spin_lock_irq(&chip->lock);
1677 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1678 	spin_unlock_irq(&chip->lock);
1679 	return 1;
1680 }
1681 
1682 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1683 	.name = "VU-meters Switch",
1684 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1685 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1686 	.info = snd_echo_vumeters_switch_info,
1687 	.put = snd_echo_vumeters_switch_put,
1688 };
1689 
1690 
1691 
1692 /***** Read VU-meters (input, output, analog and digital together) *****/
1693 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1694 				  struct snd_ctl_elem_info *uinfo)
1695 {
1696 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1697 	uinfo->count = 96;
1698 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1699 	uinfo->value.integer.max = 0;
1700 #ifdef ECHOCARD_HAS_VMIXER
1701 	uinfo->dimen.d[0] = 3;	/* Out, In, Virt */
1702 #else
1703 	uinfo->dimen.d[0] = 2;	/* Out, In */
1704 #endif
1705 	uinfo->dimen.d[1] = 16;	/* 16 channels */
1706 	uinfo->dimen.d[2] = 2;	/* 0=level, 1=peak */
1707 	return 0;
1708 }
1709 
1710 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1711 				 struct snd_ctl_elem_value *ucontrol)
1712 {
1713 	struct echoaudio *chip;
1714 
1715 	chip = snd_kcontrol_chip(kcontrol);
1716 	get_audio_meters(chip, ucontrol->value.integer.value);
1717 	return 0;
1718 }
1719 
1720 static const struct snd_kcontrol_new snd_echo_vumeters = {
1721 	.name = "VU-meters",
1722 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1723 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1724 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1725 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1726 	.info = snd_echo_vumeters_info,
1727 	.get = snd_echo_vumeters_get,
1728 	.tlv = {.p = db_scale_output_gain},
1729 };
1730 
1731 
1732 
1733 /*** Channels info - it exports informations about the number of channels ***/
1734 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1735 				       struct snd_ctl_elem_info *uinfo)
1736 {
1737 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1738 	uinfo->count = 6;
1739 	uinfo->value.integer.min = 0;
1740 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1741 	return 0;
1742 }
1743 
1744 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1745 				      struct snd_ctl_elem_value *ucontrol)
1746 {
1747 	struct echoaudio *chip;
1748 	int detected, clocks, bit, src;
1749 
1750 	chip = snd_kcontrol_chip(kcontrol);
1751 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1752 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1753 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1754 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1755 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1756 
1757 	/* Compute the bitmask of the currently valid input clocks */
1758 	detected = detect_input_clocks(chip);
1759 	clocks = 0;
1760 	src = chip->num_clock_sources - 1;
1761 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1762 		if (detected & (1 << bit))
1763 			for (; src >= 0; src--)
1764 				if (bit == chip->clock_source_list[src]) {
1765 					clocks |= 1 << src;
1766 					break;
1767 				}
1768 	ucontrol->value.integer.value[5] = clocks;
1769 
1770 	return 0;
1771 }
1772 
1773 static const struct snd_kcontrol_new snd_echo_channels_info = {
1774 	.name = "Channels info",
1775 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1776 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1777 	.info = snd_echo_channels_info_info,
1778 	.get = snd_echo_channels_info_get,
1779 };
1780 
1781 
1782 
1783 
1784 /******************************************************************************
1785 	IRQ Handler
1786 ******************************************************************************/
1787 
1788 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1789 {
1790 	struct echoaudio *chip = dev_id;
1791 	struct snd_pcm_substream *substream;
1792 	int period, ss, st;
1793 
1794 	spin_lock(&chip->lock);
1795 	st = service_irq(chip);
1796 	if (st < 0) {
1797 		spin_unlock(&chip->lock);
1798 		return IRQ_NONE;
1799 	}
1800 	/* The hardware doesn't tell us which substream caused the irq,
1801 	thus we have to check all running substreams. */
1802 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1803 		substream = chip->substream[ss];
1804 		if (substream && ((struct audiopipe *)substream->runtime->
1805 				private_data)->state == PIPE_STATE_STARTED) {
1806 			period = pcm_pointer(substream) /
1807 				substream->runtime->period_size;
1808 			if (period != chip->last_period[ss]) {
1809 				chip->last_period[ss] = period;
1810 				spin_unlock(&chip->lock);
1811 				snd_pcm_period_elapsed(substream);
1812 				spin_lock(&chip->lock);
1813 			}
1814 		}
1815 	}
1816 	spin_unlock(&chip->lock);
1817 
1818 #ifdef ECHOCARD_HAS_MIDI
1819 	if (st > 0 && chip->midi_in) {
1820 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1821 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1822 	}
1823 #endif
1824 	return IRQ_HANDLED;
1825 }
1826 
1827 
1828 
1829 
1830 /******************************************************************************
1831 	Module construction / destruction
1832 ******************************************************************************/
1833 
1834 static int snd_echo_free(struct echoaudio *chip)
1835 {
1836 	if (chip->comm_page)
1837 		rest_in_peace(chip);
1838 
1839 	if (chip->irq >= 0)
1840 		free_irq(chip->irq, chip);
1841 
1842 	if (chip->comm_page)
1843 		snd_dma_free_pages(&chip->commpage_dma_buf);
1844 
1845 	iounmap(chip->dsp_registers);
1846 	release_and_free_resource(chip->iores);
1847 	pci_disable_device(chip->pci);
1848 
1849 	/* release chip data */
1850 	free_firmware_cache(chip);
1851 	kfree(chip);
1852 	return 0;
1853 }
1854 
1855 
1856 
1857 static int snd_echo_dev_free(struct snd_device *device)
1858 {
1859 	struct echoaudio *chip = device->device_data;
1860 
1861 	return snd_echo_free(chip);
1862 }
1863 
1864 
1865 
1866 /* <--snd_echo_probe() */
1867 static int snd_echo_create(struct snd_card *card,
1868 			   struct pci_dev *pci,
1869 			   struct echoaudio **rchip)
1870 {
1871 	struct echoaudio *chip;
1872 	int err;
1873 	size_t sz;
1874 	static struct snd_device_ops ops = {
1875 		.dev_free = snd_echo_dev_free,
1876 	};
1877 
1878 	*rchip = NULL;
1879 
1880 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1881 
1882 	if ((err = pci_enable_device(pci)) < 0)
1883 		return err;
1884 	pci_set_master(pci);
1885 
1886 	/* Allocate chip if needed */
1887 	if (!*rchip) {
1888 		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1889 		if (!chip) {
1890 			pci_disable_device(pci);
1891 			return -ENOMEM;
1892 		}
1893 		dev_dbg(card->dev, "chip=%p\n", chip);
1894 		spin_lock_init(&chip->lock);
1895 		chip->card = card;
1896 		chip->pci = pci;
1897 		chip->irq = -1;
1898 		atomic_set(&chip->opencount, 0);
1899 		mutex_init(&chip->mode_mutex);
1900 		chip->can_set_rate = 1;
1901 	} else {
1902 		/* If this was called from the resume function, chip is
1903 		 * already allocated and it contains current card settings.
1904 		 */
1905 		chip = *rchip;
1906 	}
1907 
1908 	/* PCI resource allocation */
1909 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1910 	sz = pci_resource_len(pci, 0);
1911 	if (sz > PAGE_SIZE)
1912 		sz = PAGE_SIZE;		/* We map only the required part */
1913 
1914 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1915 					      ECHOCARD_NAME)) == NULL) {
1916 		dev_err(chip->card->dev, "cannot get memory region\n");
1917 		snd_echo_free(chip);
1918 		return -EBUSY;
1919 	}
1920 	chip->dsp_registers = (volatile u32 __iomem *)
1921 		ioremap_nocache(chip->dsp_registers_phys, sz);
1922 	if (!chip->dsp_registers) {
1923 		dev_err(chip->card->dev, "ioremap failed\n");
1924 		snd_echo_free(chip);
1925 		return -ENOMEM;
1926 	}
1927 
1928 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1929 			KBUILD_MODNAME, chip)) {
1930 		dev_err(chip->card->dev, "cannot grab irq\n");
1931 		snd_echo_free(chip);
1932 		return -EBUSY;
1933 	}
1934 	chip->irq = pci->irq;
1935 	dev_dbg(card->dev, "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, &chip->pci->dev,
1941 				sizeof(struct comm_page),
1942 				&chip->commpage_dma_buf) < 0) {
1943 		dev_err(chip->card->dev, "cannot allocate the comm page\n");
1944 		snd_echo_free(chip);
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 >= 0)
1952 		err = set_mixer_defaults(chip);
1953 	if (err < 0) {
1954 		dev_err(card->dev, "init_hw err=%d\n", err);
1955 		snd_echo_free(chip);
1956 		return err;
1957 	}
1958 
1959 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1960 		snd_echo_free(chip);
1961 		return err;
1962 	}
1963 	*rchip = chip;
1964 	/* Init done ! */
1965 	return 0;
1966 }
1967 
1968 
1969 
1970 /* constructor */
1971 static int snd_echo_probe(struct pci_dev *pci,
1972 			  const struct pci_device_id *pci_id)
1973 {
1974 	static int dev;
1975 	struct snd_card *card;
1976 	struct echoaudio *chip;
1977 	char *dsp;
1978 	int i, err;
1979 
1980 	if (dev >= SNDRV_CARDS)
1981 		return -ENODEV;
1982 	if (!enable[dev]) {
1983 		dev++;
1984 		return -ENOENT;
1985 	}
1986 
1987 	i = 0;
1988 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1989 			   0, &card);
1990 	if (err < 0)
1991 		return err;
1992 
1993 	chip = NULL;	/* Tells snd_echo_create to allocate chip */
1994 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
1995 		snd_card_free(card);
1996 		return err;
1997 	}
1998 
1999 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2000 	strcpy(card->shortname, chip->card_name);
2001 
2002 	dsp = "56301";
2003 	if (pci_id->device == 0x3410)
2004 		dsp = "56361";
2005 
2006 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2007 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2008 		chip->dsp_registers_phys, chip->irq);
2009 
2010 	if ((err = snd_echo_new_pcm(chip)) < 0) {
2011 		dev_err(chip->card->dev, "new pcm error %d\n", err);
2012 		snd_card_free(card);
2013 		return err;
2014 	}
2015 
2016 #ifdef ECHOCARD_HAS_MIDI
2017 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2018 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
2019 			dev_err(chip->card->dev, "new midi error %d\n", err);
2020 			snd_card_free(card);
2021 			return err;
2022 		}
2023 	}
2024 #endif
2025 
2026 #ifdef ECHOCARD_HAS_VMIXER
2027 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2028 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2029 		goto ctl_error;
2030 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2031 	err = snd_ctl_add(chip->card,
2032 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2033 	if (err < 0)
2034 		goto ctl_error;
2035 #endif
2036 #else /* ECHOCARD_HAS_VMIXER */
2037 	err = snd_ctl_add(chip->card,
2038 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2039 	if (err < 0)
2040 		goto ctl_error;
2041 #endif /* ECHOCARD_HAS_VMIXER */
2042 
2043 #ifdef ECHOCARD_HAS_INPUT_GAIN
2044 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2045 		goto ctl_error;
2046 #endif
2047 
2048 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2049 	if (!chip->hasnt_input_nominal_level)
2050 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2051 			goto ctl_error;
2052 #endif
2053 
2054 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2055 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2056 		goto ctl_error;
2057 #endif
2058 
2059 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2060 		goto ctl_error;
2061 
2062 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2063 		goto ctl_error;
2064 
2065 #ifdef ECHOCARD_HAS_MONITOR
2066 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2067 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2068 		goto ctl_error;
2069 #endif
2070 
2071 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2072 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2073 		goto ctl_error;
2074 #endif
2075 
2076 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2077 		goto ctl_error;
2078 
2079 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2080 	/* Creates a list of available digital modes */
2081 	chip->num_digital_modes = 0;
2082 	for (i = 0; i < 6; i++)
2083 		if (chip->digital_modes & (1 << i))
2084 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2085 
2086 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2087 		goto ctl_error;
2088 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2089 
2090 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2091 	/* Creates a list of available clock sources */
2092 	chip->num_clock_sources = 0;
2093 	for (i = 0; i < 10; i++)
2094 		if (chip->input_clock_types & (1 << i))
2095 			chip->clock_source_list[chip->num_clock_sources++] = i;
2096 
2097 	if (chip->num_clock_sources > 1) {
2098 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2099 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2100 			goto ctl_error;
2101 	}
2102 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2103 
2104 #ifdef ECHOCARD_HAS_DIGITAL_IO
2105 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2106 		goto ctl_error;
2107 #endif
2108 
2109 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2110 	if (chip->has_phantom_power)
2111 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2112 			goto ctl_error;
2113 #endif
2114 
2115 	err = snd_card_register(card);
2116 	if (err < 0)
2117 		goto ctl_error;
2118 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2119 
2120 	pci_set_drvdata(pci, chip);
2121 	dev++;
2122 	return 0;
2123 
2124 ctl_error:
2125 	dev_err(card->dev, "new control error %d\n", err);
2126 	snd_card_free(card);
2127 	return err;
2128 }
2129 
2130 
2131 
2132 #if defined(CONFIG_PM_SLEEP)
2133 
2134 static int snd_echo_suspend(struct device *dev)
2135 {
2136 	struct echoaudio *chip = dev_get_drvdata(dev);
2137 
2138 #ifdef ECHOCARD_HAS_MIDI
2139 	/* This call can sleep */
2140 	if (chip->midi_out)
2141 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2142 #endif
2143 	spin_lock_irq(&chip->lock);
2144 	if (wait_handshake(chip)) {
2145 		spin_unlock_irq(&chip->lock);
2146 		return -EIO;
2147 	}
2148 	clear_handshake(chip);
2149 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2150 		spin_unlock_irq(&chip->lock);
2151 		return -EIO;
2152 	}
2153 	spin_unlock_irq(&chip->lock);
2154 
2155 	chip->dsp_code = NULL;
2156 	free_irq(chip->irq, chip);
2157 	chip->irq = -1;
2158 	return 0;
2159 }
2160 
2161 
2162 
2163 static int snd_echo_resume(struct device *dev)
2164 {
2165 	struct pci_dev *pci = to_pci_dev(dev);
2166 	struct echoaudio *chip = dev_get_drvdata(dev);
2167 	struct comm_page *commpage, *commpage_bak;
2168 	u32 pipe_alloc_mask;
2169 	int err;
2170 
2171 	commpage = chip->comm_page;
2172 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2173 	if (commpage_bak == NULL)
2174 		return -ENOMEM;
2175 
2176 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2177 	if (err < 0) {
2178 		kfree(commpage_bak);
2179 		dev_err(dev, "resume init_hw err=%d\n", err);
2180 		snd_echo_free(chip);
2181 		return err;
2182 	}
2183 
2184 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2185 	 * restore_dsp_settings() fails.
2186 	 */
2187 	pipe_alloc_mask = chip->pipe_alloc_mask;
2188 	chip->pipe_alloc_mask = 0;
2189 	err = restore_dsp_rettings(chip);
2190 	chip->pipe_alloc_mask = pipe_alloc_mask;
2191 	if (err < 0) {
2192 		kfree(commpage_bak);
2193 		return err;
2194 	}
2195 
2196 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2197 		sizeof(commpage->audio_format));
2198 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2199 		sizeof(commpage->sglist_addr));
2200 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2201 		sizeof(commpage->midi_output));
2202 	kfree(commpage_bak);
2203 
2204 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2205 			KBUILD_MODNAME, chip)) {
2206 		dev_err(chip->card->dev, "cannot grab irq\n");
2207 		snd_echo_free(chip);
2208 		return -EBUSY;
2209 	}
2210 	chip->irq = pci->irq;
2211 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2212 
2213 #ifdef ECHOCARD_HAS_MIDI
2214 	if (chip->midi_input_enabled)
2215 		enable_midi_input(chip, true);
2216 	if (chip->midi_out)
2217 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2218 #endif
2219 
2220 	return 0;
2221 }
2222 
2223 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2224 #define SND_ECHO_PM_OPS	&snd_echo_pm
2225 #else
2226 #define SND_ECHO_PM_OPS	NULL
2227 #endif /* CONFIG_PM_SLEEP */
2228 
2229 
2230 static void snd_echo_remove(struct pci_dev *pci)
2231 {
2232 	struct echoaudio *chip;
2233 
2234 	chip = pci_get_drvdata(pci);
2235 	if (chip)
2236 		snd_card_free(chip->card);
2237 }
2238 
2239 
2240 
2241 /******************************************************************************
2242 	Everything starts and ends here
2243 ******************************************************************************/
2244 
2245 /* pci_driver definition */
2246 static struct pci_driver echo_driver = {
2247 	.name = KBUILD_MODNAME,
2248 	.id_table = snd_echo_ids,
2249 	.probe = snd_echo_probe,
2250 	.remove = snd_echo_remove,
2251 	.driver = {
2252 		.pm = SND_ECHO_PM_OPS,
2253 	},
2254 };
2255 
2256 module_pci_driver(echo_driver);
2257