xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision 4bb1e4e7)
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 	.hw_params = pcm_analog_out_hw_params,
811 	.hw_free = pcm_hw_free,
812 	.prepare = pcm_prepare,
813 	.trigger = pcm_trigger,
814 	.pointer = pcm_pointer,
815 };
816 static const struct snd_pcm_ops analog_capture_ops = {
817 	.open = pcm_analog_in_open,
818 	.close = pcm_close,
819 	.hw_params = pcm_analog_in_hw_params,
820 	.hw_free = pcm_hw_free,
821 	.prepare = pcm_prepare,
822 	.trigger = pcm_trigger,
823 	.pointer = pcm_pointer,
824 };
825 #ifdef ECHOCARD_HAS_DIGITAL_IO
826 #ifndef ECHOCARD_HAS_VMIXER
827 static const struct snd_pcm_ops digital_playback_ops = {
828 	.open = pcm_digital_out_open,
829 	.close = pcm_close,
830 	.hw_params = pcm_digital_out_hw_params,
831 	.hw_free = pcm_hw_free,
832 	.prepare = pcm_prepare,
833 	.trigger = pcm_trigger,
834 	.pointer = pcm_pointer,
835 };
836 #endif /* !ECHOCARD_HAS_VMIXER */
837 static const struct snd_pcm_ops digital_capture_ops = {
838 	.open = pcm_digital_in_open,
839 	.close = pcm_close,
840 	.hw_params = pcm_digital_in_hw_params,
841 	.hw_free = pcm_hw_free,
842 	.prepare = pcm_prepare,
843 	.trigger = pcm_trigger,
844 	.pointer = pcm_pointer,
845 };
846 #endif /* ECHOCARD_HAS_DIGITAL_IO */
847 
848 
849 
850 /* Preallocate memory only for the first substream because it's the most
851  * used one
852  */
853 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
854 {
855 	struct snd_pcm_substream *ss;
856 	int stream;
857 
858 	for (stream = 0; stream < 2; stream++)
859 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
860 			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
861 						   dev,
862 						   ss->number ? 0 : 128<<10,
863 						   256<<10);
864 }
865 
866 
867 
868 /*<--snd_echo_probe() */
869 static int snd_echo_new_pcm(struct echoaudio *chip)
870 {
871 	struct snd_pcm *pcm;
872 	int err;
873 
874 #ifdef ECHOCARD_HAS_VMIXER
875 	/* This card has a Vmixer, that is there is no direct mapping from PCM
876 	streams to physical outputs. The user can mix the streams as he wishes
877 	via control interface and it's possible to send any stream to any
878 	output, thus it makes no sense to keep analog and digital outputs
879 	separated */
880 
881 	/* PCM#0 Virtual outputs and analog inputs */
882 	if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
883 				num_analog_busses_in(chip), &pcm)) < 0)
884 		return err;
885 	pcm->private_data = chip;
886 	chip->analog_pcm = pcm;
887 	strcpy(pcm->name, chip->card->shortname);
888 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
889 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
890 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
891 
892 #ifdef ECHOCARD_HAS_DIGITAL_IO
893 	/* PCM#1 Digital inputs, no outputs */
894 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
895 			       num_digital_busses_in(chip), &pcm)) < 0)
896 		return err;
897 	pcm->private_data = chip;
898 	chip->digital_pcm = pcm;
899 	strcpy(pcm->name, chip->card->shortname);
900 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
901 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
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 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
922 
923 #ifdef ECHOCARD_HAS_DIGITAL_IO
924 	/* PCM#1 Digital i/o */
925 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
926 			       num_digital_busses_out(chip),
927 			       num_digital_busses_in(chip), &pcm)) < 0)
928 		return err;
929 	pcm->private_data = chip;
930 	chip->digital_pcm = pcm;
931 	strcpy(pcm->name, chip->card->shortname);
932 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
933 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
934 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
935 #endif /* ECHOCARD_HAS_DIGITAL_IO */
936 
937 #endif /* ECHOCARD_HAS_VMIXER */
938 
939 	return 0;
940 }
941 
942 
943 
944 
945 /******************************************************************************
946 	Control interface
947 ******************************************************************************/
948 
949 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
950 
951 /******************* PCM output volume *******************/
952 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
953 				     struct snd_ctl_elem_info *uinfo)
954 {
955 	struct echoaudio *chip;
956 
957 	chip = snd_kcontrol_chip(kcontrol);
958 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
959 	uinfo->count = num_busses_out(chip);
960 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
961 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
962 	return 0;
963 }
964 
965 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
966 				    struct snd_ctl_elem_value *ucontrol)
967 {
968 	struct echoaudio *chip;
969 	int c;
970 
971 	chip = snd_kcontrol_chip(kcontrol);
972 	for (c = 0; c < num_busses_out(chip); c++)
973 		ucontrol->value.integer.value[c] = chip->output_gain[c];
974 	return 0;
975 }
976 
977 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
978 				    struct snd_ctl_elem_value *ucontrol)
979 {
980 	struct echoaudio *chip;
981 	int c, changed, gain;
982 
983 	changed = 0;
984 	chip = snd_kcontrol_chip(kcontrol);
985 	spin_lock_irq(&chip->lock);
986 	for (c = 0; c < num_busses_out(chip); c++) {
987 		gain = ucontrol->value.integer.value[c];
988 		/* Ignore out of range values */
989 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
990 			continue;
991 		if (chip->output_gain[c] != gain) {
992 			set_output_gain(chip, c, gain);
993 			changed = 1;
994 		}
995 	}
996 	if (changed)
997 		update_output_line_level(chip);
998 	spin_unlock_irq(&chip->lock);
999 	return changed;
1000 }
1001 
1002 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1003 /* On the Mia this one controls the line-out volume */
1004 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1005 	.name = "Line Playback Volume",
1006 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1007 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1008 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1009 	.info = snd_echo_output_gain_info,
1010 	.get = snd_echo_output_gain_get,
1011 	.put = snd_echo_output_gain_put,
1012 	.tlv = {.p = db_scale_output_gain},
1013 };
1014 #else
1015 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1016 	.name = "PCM Playback Volume",
1017 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1018 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1019 	.info = snd_echo_output_gain_info,
1020 	.get = snd_echo_output_gain_get,
1021 	.put = snd_echo_output_gain_put,
1022 	.tlv = {.p = db_scale_output_gain},
1023 };
1024 #endif
1025 
1026 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1027 
1028 
1029 
1030 #ifdef ECHOCARD_HAS_INPUT_GAIN
1031 
1032 /******************* Analog input volume *******************/
1033 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1034 				    struct snd_ctl_elem_info *uinfo)
1035 {
1036 	struct echoaudio *chip;
1037 
1038 	chip = snd_kcontrol_chip(kcontrol);
1039 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1040 	uinfo->count = num_analog_busses_in(chip);
1041 	uinfo->value.integer.min = ECHOGAIN_MININP;
1042 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1043 	return 0;
1044 }
1045 
1046 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1047 				   struct snd_ctl_elem_value *ucontrol)
1048 {
1049 	struct echoaudio *chip;
1050 	int c;
1051 
1052 	chip = snd_kcontrol_chip(kcontrol);
1053 	for (c = 0; c < num_analog_busses_in(chip); c++)
1054 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1055 	return 0;
1056 }
1057 
1058 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1059 				   struct snd_ctl_elem_value *ucontrol)
1060 {
1061 	struct echoaudio *chip;
1062 	int c, gain, changed;
1063 
1064 	changed = 0;
1065 	chip = snd_kcontrol_chip(kcontrol);
1066 	spin_lock_irq(&chip->lock);
1067 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1068 		gain = ucontrol->value.integer.value[c];
1069 		/* Ignore out of range values */
1070 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1071 			continue;
1072 		if (chip->input_gain[c] != gain) {
1073 			set_input_gain(chip, c, gain);
1074 			changed = 1;
1075 		}
1076 	}
1077 	if (changed)
1078 		update_input_line_level(chip);
1079 	spin_unlock_irq(&chip->lock);
1080 	return changed;
1081 }
1082 
1083 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1084 
1085 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1086 	.name = "Line Capture Volume",
1087 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1088 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1089 	.info = snd_echo_input_gain_info,
1090 	.get = snd_echo_input_gain_get,
1091 	.put = snd_echo_input_gain_put,
1092 	.tlv = {.p = db_scale_input_gain},
1093 };
1094 
1095 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1096 
1097 
1098 
1099 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1100 
1101 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1102 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1103 					 struct snd_ctl_elem_info *uinfo)
1104 {
1105 	struct echoaudio *chip;
1106 
1107 	chip = snd_kcontrol_chip(kcontrol);
1108 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1109 	uinfo->count = num_analog_busses_out(chip);
1110 	uinfo->value.integer.min = 0;
1111 	uinfo->value.integer.max = 1;
1112 	return 0;
1113 }
1114 
1115 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1116 				       struct snd_ctl_elem_value *ucontrol)
1117 {
1118 	struct echoaudio *chip;
1119 	int c;
1120 
1121 	chip = snd_kcontrol_chip(kcontrol);
1122 	for (c = 0; c < num_analog_busses_out(chip); c++)
1123 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1124 	return 0;
1125 }
1126 
1127 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1128 				       struct snd_ctl_elem_value *ucontrol)
1129 {
1130 	struct echoaudio *chip;
1131 	int c, changed;
1132 
1133 	changed = 0;
1134 	chip = snd_kcontrol_chip(kcontrol);
1135 	spin_lock_irq(&chip->lock);
1136 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1137 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1138 			set_nominal_level(chip, c,
1139 					  ucontrol->value.integer.value[c]);
1140 			changed = 1;
1141 		}
1142 	}
1143 	if (changed)
1144 		update_output_line_level(chip);
1145 	spin_unlock_irq(&chip->lock);
1146 	return changed;
1147 }
1148 
1149 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1150 	.name = "Line Playback Switch (-10dBV)",
1151 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1152 	.info = snd_echo_output_nominal_info,
1153 	.get = snd_echo_output_nominal_get,
1154 	.put = snd_echo_output_nominal_put,
1155 };
1156 
1157 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1158 
1159 
1160 
1161 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1162 
1163 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1164 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1165 				       struct snd_ctl_elem_info *uinfo)
1166 {
1167 	struct echoaudio *chip;
1168 
1169 	chip = snd_kcontrol_chip(kcontrol);
1170 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1171 	uinfo->count = num_analog_busses_in(chip);
1172 	uinfo->value.integer.min = 0;
1173 	uinfo->value.integer.max = 1;
1174 	return 0;
1175 }
1176 
1177 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1178 				      struct snd_ctl_elem_value *ucontrol)
1179 {
1180 	struct echoaudio *chip;
1181 	int c;
1182 
1183 	chip = snd_kcontrol_chip(kcontrol);
1184 	for (c = 0; c < num_analog_busses_in(chip); c++)
1185 		ucontrol->value.integer.value[c] =
1186 			chip->nominal_level[bx_analog_in(chip) + c];
1187 	return 0;
1188 }
1189 
1190 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1191 				      struct snd_ctl_elem_value *ucontrol)
1192 {
1193 	struct echoaudio *chip;
1194 	int c, changed;
1195 
1196 	changed = 0;
1197 	chip = snd_kcontrol_chip(kcontrol);
1198 	spin_lock_irq(&chip->lock);
1199 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1200 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1201 		    ucontrol->value.integer.value[c]) {
1202 			set_nominal_level(chip, bx_analog_in(chip) + c,
1203 					  ucontrol->value.integer.value[c]);
1204 			changed = 1;
1205 		}
1206 	}
1207 	if (changed)
1208 		update_output_line_level(chip);	/* "Output" is not a mistake
1209 						 * here.
1210 						 */
1211 	spin_unlock_irq(&chip->lock);
1212 	return changed;
1213 }
1214 
1215 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1216 	.name = "Line Capture Switch (-10dBV)",
1217 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1218 	.info = snd_echo_input_nominal_info,
1219 	.get = snd_echo_input_nominal_get,
1220 	.put = snd_echo_input_nominal_put,
1221 };
1222 
1223 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1224 
1225 
1226 
1227 #ifdef ECHOCARD_HAS_MONITOR
1228 
1229 /******************* Monitor mixer *******************/
1230 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1231 			       struct snd_ctl_elem_info *uinfo)
1232 {
1233 	struct echoaudio *chip;
1234 
1235 	chip = snd_kcontrol_chip(kcontrol);
1236 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1237 	uinfo->count = 1;
1238 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1239 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1240 	uinfo->dimen.d[0] = num_busses_out(chip);
1241 	uinfo->dimen.d[1] = num_busses_in(chip);
1242 	return 0;
1243 }
1244 
1245 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1246 			      struct snd_ctl_elem_value *ucontrol)
1247 {
1248 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1249 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1250 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1251 
1252 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1253 		return -EINVAL;
1254 
1255 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1256 	return 0;
1257 }
1258 
1259 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1260 			      struct snd_ctl_elem_value *ucontrol)
1261 {
1262 	struct echoaudio *chip;
1263 	int changed,  gain;
1264 	unsigned int out, in;
1265 
1266 	changed = 0;
1267 	chip = snd_kcontrol_chip(kcontrol);
1268 	out = ucontrol->id.index / num_busses_in(chip);
1269 	in = ucontrol->id.index % num_busses_in(chip);
1270 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1271 		return -EINVAL;
1272 	gain = ucontrol->value.integer.value[0];
1273 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1274 		return -EINVAL;
1275 	if (chip->monitor_gain[out][in] != gain) {
1276 		spin_lock_irq(&chip->lock);
1277 		set_monitor_gain(chip, out, in, gain);
1278 		update_output_line_level(chip);
1279 		spin_unlock_irq(&chip->lock);
1280 		changed = 1;
1281 	}
1282 	return changed;
1283 }
1284 
1285 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1286 	.name = "Monitor Mixer Volume",
1287 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1288 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1289 	.info = snd_echo_mixer_info,
1290 	.get = snd_echo_mixer_get,
1291 	.put = snd_echo_mixer_put,
1292 	.tlv = {.p = db_scale_output_gain},
1293 };
1294 
1295 #endif /* ECHOCARD_HAS_MONITOR */
1296 
1297 
1298 
1299 #ifdef ECHOCARD_HAS_VMIXER
1300 
1301 /******************* Vmixer *******************/
1302 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1303 				struct snd_ctl_elem_info *uinfo)
1304 {
1305 	struct echoaudio *chip;
1306 
1307 	chip = snd_kcontrol_chip(kcontrol);
1308 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1309 	uinfo->count = 1;
1310 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1311 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1312 	uinfo->dimen.d[0] = num_busses_out(chip);
1313 	uinfo->dimen.d[1] = num_pipes_out(chip);
1314 	return 0;
1315 }
1316 
1317 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1318 			       struct snd_ctl_elem_value *ucontrol)
1319 {
1320 	struct echoaudio *chip;
1321 
1322 	chip = snd_kcontrol_chip(kcontrol);
1323 	ucontrol->value.integer.value[0] =
1324 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1325 			[ucontrol->id.index % num_pipes_out(chip)];
1326 	return 0;
1327 }
1328 
1329 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1330 			       struct snd_ctl_elem_value *ucontrol)
1331 {
1332 	struct echoaudio *chip;
1333 	int gain, changed;
1334 	short vch, out;
1335 
1336 	changed = 0;
1337 	chip = snd_kcontrol_chip(kcontrol);
1338 	out = ucontrol->id.index / num_pipes_out(chip);
1339 	vch = ucontrol->id.index % num_pipes_out(chip);
1340 	gain = ucontrol->value.integer.value[0];
1341 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1342 		return -EINVAL;
1343 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1344 		spin_lock_irq(&chip->lock);
1345 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1346 		update_vmixer_level(chip);
1347 		spin_unlock_irq(&chip->lock);
1348 		changed = 1;
1349 	}
1350 	return changed;
1351 }
1352 
1353 static struct snd_kcontrol_new snd_echo_vmixer = {
1354 	.name = "VMixer Volume",
1355 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1356 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1357 	.info = snd_echo_vmixer_info,
1358 	.get = snd_echo_vmixer_get,
1359 	.put = snd_echo_vmixer_put,
1360 	.tlv = {.p = db_scale_output_gain},
1361 };
1362 
1363 #endif /* ECHOCARD_HAS_VMIXER */
1364 
1365 
1366 
1367 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1368 
1369 /******************* Digital mode switch *******************/
1370 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1371 				      struct snd_ctl_elem_info *uinfo)
1372 {
1373 	static const char * const names[4] = {
1374 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1375 		"S/PDIF Cdrom"
1376 	};
1377 	struct echoaudio *chip;
1378 
1379 	chip = snd_kcontrol_chip(kcontrol);
1380 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1381 }
1382 
1383 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1384 				     struct snd_ctl_elem_value *ucontrol)
1385 {
1386 	struct echoaudio *chip;
1387 	int i, mode;
1388 
1389 	chip = snd_kcontrol_chip(kcontrol);
1390 	mode = chip->digital_mode;
1391 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1392 		if (mode == chip->digital_mode_list[i]) {
1393 			ucontrol->value.enumerated.item[0] = i;
1394 			break;
1395 		}
1396 	return 0;
1397 }
1398 
1399 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1400 				     struct snd_ctl_elem_value *ucontrol)
1401 {
1402 	struct echoaudio *chip;
1403 	int changed;
1404 	unsigned short emode, dmode;
1405 
1406 	changed = 0;
1407 	chip = snd_kcontrol_chip(kcontrol);
1408 
1409 	emode = ucontrol->value.enumerated.item[0];
1410 	if (emode >= chip->num_digital_modes)
1411 		return -EINVAL;
1412 	dmode = chip->digital_mode_list[emode];
1413 
1414 	if (dmode != chip->digital_mode) {
1415 		/* mode_mutex is required to make this operation atomic wrt
1416 		pcm_digital_*_open() and set_input_clock() functions. */
1417 		mutex_lock(&chip->mode_mutex);
1418 
1419 		/* Do not allow the user to change the digital mode when a pcm
1420 		device is open because it also changes the number of channels
1421 		and the allowed sample rates */
1422 		if (atomic_read(&chip->opencount)) {
1423 			changed = -EAGAIN;
1424 		} else {
1425 			changed = set_digital_mode(chip, dmode);
1426 			/* If we had to change the clock source, report it */
1427 			if (changed > 0 && chip->clock_src_ctl) {
1428 				snd_ctl_notify(chip->card,
1429 					       SNDRV_CTL_EVENT_MASK_VALUE,
1430 					       &chip->clock_src_ctl->id);
1431 				dev_dbg(chip->card->dev,
1432 					"SDM() =%d\n", changed);
1433 			}
1434 			if (changed >= 0)
1435 				changed = 1;	/* No errors */
1436 		}
1437 		mutex_unlock(&chip->mode_mutex);
1438 	}
1439 	return changed;
1440 }
1441 
1442 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1443 	.name = "Digital mode Switch",
1444 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1445 	.info = snd_echo_digital_mode_info,
1446 	.get = snd_echo_digital_mode_get,
1447 	.put = snd_echo_digital_mode_put,
1448 };
1449 
1450 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1451 
1452 
1453 
1454 #ifdef ECHOCARD_HAS_DIGITAL_IO
1455 
1456 /******************* S/PDIF mode switch *******************/
1457 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1458 				    struct snd_ctl_elem_info *uinfo)
1459 {
1460 	static const char * const names[2] = {"Consumer", "Professional"};
1461 
1462 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1463 }
1464 
1465 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1466 				   struct snd_ctl_elem_value *ucontrol)
1467 {
1468 	struct echoaudio *chip;
1469 
1470 	chip = snd_kcontrol_chip(kcontrol);
1471 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1472 	return 0;
1473 }
1474 
1475 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1476 				   struct snd_ctl_elem_value *ucontrol)
1477 {
1478 	struct echoaudio *chip;
1479 	int mode;
1480 
1481 	chip = snd_kcontrol_chip(kcontrol);
1482 	mode = !!ucontrol->value.enumerated.item[0];
1483 	if (mode != chip->professional_spdif) {
1484 		spin_lock_irq(&chip->lock);
1485 		set_professional_spdif(chip, mode);
1486 		spin_unlock_irq(&chip->lock);
1487 		return 1;
1488 	}
1489 	return 0;
1490 }
1491 
1492 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1493 	.name = "S/PDIF mode Switch",
1494 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1495 	.info = snd_echo_spdif_mode_info,
1496 	.get = snd_echo_spdif_mode_get,
1497 	.put = snd_echo_spdif_mode_put,
1498 };
1499 
1500 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1501 
1502 
1503 
1504 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1505 
1506 /******************* Select input clock source *******************/
1507 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1508 				      struct snd_ctl_elem_info *uinfo)
1509 {
1510 	static const char * const names[8] = {
1511 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1512 		"ESync96", "MTC"
1513 	};
1514 	struct echoaudio *chip;
1515 
1516 	chip = snd_kcontrol_chip(kcontrol);
1517 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1518 }
1519 
1520 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1521 				     struct snd_ctl_elem_value *ucontrol)
1522 {
1523 	struct echoaudio *chip;
1524 	int i, clock;
1525 
1526 	chip = snd_kcontrol_chip(kcontrol);
1527 	clock = chip->input_clock;
1528 
1529 	for (i = 0; i < chip->num_clock_sources; i++)
1530 		if (clock == chip->clock_source_list[i])
1531 			ucontrol->value.enumerated.item[0] = i;
1532 
1533 	return 0;
1534 }
1535 
1536 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1537 				     struct snd_ctl_elem_value *ucontrol)
1538 {
1539 	struct echoaudio *chip;
1540 	int changed;
1541 	unsigned int eclock, dclock;
1542 
1543 	changed = 0;
1544 	chip = snd_kcontrol_chip(kcontrol);
1545 	eclock = ucontrol->value.enumerated.item[0];
1546 	if (eclock >= chip->input_clock_types)
1547 		return -EINVAL;
1548 	dclock = chip->clock_source_list[eclock];
1549 	if (chip->input_clock != dclock) {
1550 		mutex_lock(&chip->mode_mutex);
1551 		spin_lock_irq(&chip->lock);
1552 		if ((changed = set_input_clock(chip, dclock)) == 0)
1553 			changed = 1;	/* no errors */
1554 		spin_unlock_irq(&chip->lock);
1555 		mutex_unlock(&chip->mode_mutex);
1556 	}
1557 
1558 	if (changed < 0)
1559 		dev_dbg(chip->card->dev,
1560 			"seticlk val%d err 0x%x\n", dclock, changed);
1561 
1562 	return changed;
1563 }
1564 
1565 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1566 	.name = "Sample Clock Source",
1567 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1568 	.info = snd_echo_clock_source_info,
1569 	.get = snd_echo_clock_source_get,
1570 	.put = snd_echo_clock_source_put,
1571 };
1572 
1573 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1574 
1575 
1576 
1577 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1578 
1579 /******************* Phantom power switch *******************/
1580 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1581 
1582 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1583 				      struct snd_ctl_elem_value *ucontrol)
1584 {
1585 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1586 
1587 	ucontrol->value.integer.value[0] = chip->phantom_power;
1588 	return 0;
1589 }
1590 
1591 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1592 				      struct snd_ctl_elem_value *ucontrol)
1593 {
1594 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1595 	int power, changed = 0;
1596 
1597 	power = !!ucontrol->value.integer.value[0];
1598 	if (chip->phantom_power != power) {
1599 		spin_lock_irq(&chip->lock);
1600 		changed = set_phantom_power(chip, power);
1601 		spin_unlock_irq(&chip->lock);
1602 		if (changed == 0)
1603 			changed = 1;	/* no errors */
1604 	}
1605 	return changed;
1606 }
1607 
1608 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1609 	.name = "Phantom power Switch",
1610 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1611 	.info = snd_echo_phantom_power_info,
1612 	.get = snd_echo_phantom_power_get,
1613 	.put = snd_echo_phantom_power_put,
1614 };
1615 
1616 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1617 
1618 
1619 
1620 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1621 
1622 /******************* Digital input automute switch *******************/
1623 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1624 
1625 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1626 				 struct snd_ctl_elem_value *ucontrol)
1627 {
1628 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1629 
1630 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1631 	return 0;
1632 }
1633 
1634 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1635 				 struct snd_ctl_elem_value *ucontrol)
1636 {
1637 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1638 	int automute, changed = 0;
1639 
1640 	automute = !!ucontrol->value.integer.value[0];
1641 	if (chip->digital_in_automute != automute) {
1642 		spin_lock_irq(&chip->lock);
1643 		changed = set_input_auto_mute(chip, automute);
1644 		spin_unlock_irq(&chip->lock);
1645 		if (changed == 0)
1646 			changed = 1;	/* no errors */
1647 	}
1648 	return changed;
1649 }
1650 
1651 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1652 	.name = "Digital Capture Switch (automute)",
1653 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1654 	.info = snd_echo_automute_info,
1655 	.get = snd_echo_automute_get,
1656 	.put = snd_echo_automute_put,
1657 };
1658 
1659 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1660 
1661 
1662 
1663 /******************* VU-meters switch *******************/
1664 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1665 
1666 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1667 					struct snd_ctl_elem_value *ucontrol)
1668 {
1669 	struct echoaudio *chip;
1670 
1671 	chip = snd_kcontrol_chip(kcontrol);
1672 	spin_lock_irq(&chip->lock);
1673 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1674 	spin_unlock_irq(&chip->lock);
1675 	return 1;
1676 }
1677 
1678 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1679 	.name = "VU-meters Switch",
1680 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1681 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1682 	.info = snd_echo_vumeters_switch_info,
1683 	.put = snd_echo_vumeters_switch_put,
1684 };
1685 
1686 
1687 
1688 /***** Read VU-meters (input, output, analog and digital together) *****/
1689 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1690 				  struct snd_ctl_elem_info *uinfo)
1691 {
1692 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1693 	uinfo->count = 96;
1694 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1695 	uinfo->value.integer.max = 0;
1696 #ifdef ECHOCARD_HAS_VMIXER
1697 	uinfo->dimen.d[0] = 3;	/* Out, In, Virt */
1698 #else
1699 	uinfo->dimen.d[0] = 2;	/* Out, In */
1700 #endif
1701 	uinfo->dimen.d[1] = 16;	/* 16 channels */
1702 	uinfo->dimen.d[2] = 2;	/* 0=level, 1=peak */
1703 	return 0;
1704 }
1705 
1706 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1707 				 struct snd_ctl_elem_value *ucontrol)
1708 {
1709 	struct echoaudio *chip;
1710 
1711 	chip = snd_kcontrol_chip(kcontrol);
1712 	get_audio_meters(chip, ucontrol->value.integer.value);
1713 	return 0;
1714 }
1715 
1716 static const struct snd_kcontrol_new snd_echo_vumeters = {
1717 	.name = "VU-meters",
1718 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1719 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1720 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1721 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1722 	.info = snd_echo_vumeters_info,
1723 	.get = snd_echo_vumeters_get,
1724 	.tlv = {.p = db_scale_output_gain},
1725 };
1726 
1727 
1728 
1729 /*** Channels info - it exports informations about the number of channels ***/
1730 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1731 				       struct snd_ctl_elem_info *uinfo)
1732 {
1733 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1734 	uinfo->count = 6;
1735 	uinfo->value.integer.min = 0;
1736 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1737 	return 0;
1738 }
1739 
1740 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1741 				      struct snd_ctl_elem_value *ucontrol)
1742 {
1743 	struct echoaudio *chip;
1744 	int detected, clocks, bit, src;
1745 
1746 	chip = snd_kcontrol_chip(kcontrol);
1747 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1748 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1749 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1750 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1751 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1752 
1753 	/* Compute the bitmask of the currently valid input clocks */
1754 	detected = detect_input_clocks(chip);
1755 	clocks = 0;
1756 	src = chip->num_clock_sources - 1;
1757 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1758 		if (detected & (1 << bit))
1759 			for (; src >= 0; src--)
1760 				if (bit == chip->clock_source_list[src]) {
1761 					clocks |= 1 << src;
1762 					break;
1763 				}
1764 	ucontrol->value.integer.value[5] = clocks;
1765 
1766 	return 0;
1767 }
1768 
1769 static const struct snd_kcontrol_new snd_echo_channels_info = {
1770 	.name = "Channels info",
1771 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1772 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1773 	.info = snd_echo_channels_info_info,
1774 	.get = snd_echo_channels_info_get,
1775 };
1776 
1777 
1778 
1779 
1780 /******************************************************************************
1781 	IRQ Handler
1782 ******************************************************************************/
1783 
1784 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1785 {
1786 	struct echoaudio *chip = dev_id;
1787 	struct snd_pcm_substream *substream;
1788 	int period, ss, st;
1789 
1790 	spin_lock(&chip->lock);
1791 	st = service_irq(chip);
1792 	if (st < 0) {
1793 		spin_unlock(&chip->lock);
1794 		return IRQ_NONE;
1795 	}
1796 	/* The hardware doesn't tell us which substream caused the irq,
1797 	thus we have to check all running substreams. */
1798 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1799 		substream = chip->substream[ss];
1800 		if (substream && ((struct audiopipe *)substream->runtime->
1801 				private_data)->state == PIPE_STATE_STARTED) {
1802 			period = pcm_pointer(substream) /
1803 				substream->runtime->period_size;
1804 			if (period != chip->last_period[ss]) {
1805 				chip->last_period[ss] = period;
1806 				spin_unlock(&chip->lock);
1807 				snd_pcm_period_elapsed(substream);
1808 				spin_lock(&chip->lock);
1809 			}
1810 		}
1811 	}
1812 	spin_unlock(&chip->lock);
1813 
1814 #ifdef ECHOCARD_HAS_MIDI
1815 	if (st > 0 && chip->midi_in) {
1816 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1817 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1818 	}
1819 #endif
1820 	return IRQ_HANDLED;
1821 }
1822 
1823 
1824 
1825 
1826 /******************************************************************************
1827 	Module construction / destruction
1828 ******************************************************************************/
1829 
1830 static int snd_echo_free(struct echoaudio *chip)
1831 {
1832 	if (chip->comm_page)
1833 		rest_in_peace(chip);
1834 
1835 	if (chip->irq >= 0)
1836 		free_irq(chip->irq, chip);
1837 
1838 	if (chip->comm_page)
1839 		snd_dma_free_pages(&chip->commpage_dma_buf);
1840 
1841 	iounmap(chip->dsp_registers);
1842 	release_and_free_resource(chip->iores);
1843 	pci_disable_device(chip->pci);
1844 
1845 	/* release chip data */
1846 	free_firmware_cache(chip);
1847 	kfree(chip);
1848 	return 0;
1849 }
1850 
1851 
1852 
1853 static int snd_echo_dev_free(struct snd_device *device)
1854 {
1855 	struct echoaudio *chip = device->device_data;
1856 
1857 	return snd_echo_free(chip);
1858 }
1859 
1860 
1861 
1862 /* <--snd_echo_probe() */
1863 static int snd_echo_create(struct snd_card *card,
1864 			   struct pci_dev *pci,
1865 			   struct echoaudio **rchip)
1866 {
1867 	struct echoaudio *chip;
1868 	int err;
1869 	size_t sz;
1870 	static struct snd_device_ops ops = {
1871 		.dev_free = snd_echo_dev_free,
1872 	};
1873 
1874 	*rchip = NULL;
1875 
1876 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1877 
1878 	if ((err = pci_enable_device(pci)) < 0)
1879 		return err;
1880 	pci_set_master(pci);
1881 
1882 	/* Allocate chip if needed */
1883 	if (!*rchip) {
1884 		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1885 		if (!chip) {
1886 			pci_disable_device(pci);
1887 			return -ENOMEM;
1888 		}
1889 		dev_dbg(card->dev, "chip=%p\n", chip);
1890 		spin_lock_init(&chip->lock);
1891 		chip->card = card;
1892 		chip->pci = pci;
1893 		chip->irq = -1;
1894 		atomic_set(&chip->opencount, 0);
1895 		mutex_init(&chip->mode_mutex);
1896 		chip->can_set_rate = 1;
1897 	} else {
1898 		/* If this was called from the resume function, chip is
1899 		 * already allocated and it contains current card settings.
1900 		 */
1901 		chip = *rchip;
1902 	}
1903 
1904 	/* PCI resource allocation */
1905 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1906 	sz = pci_resource_len(pci, 0);
1907 	if (sz > PAGE_SIZE)
1908 		sz = PAGE_SIZE;		/* We map only the required part */
1909 
1910 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1911 					      ECHOCARD_NAME)) == NULL) {
1912 		dev_err(chip->card->dev, "cannot get memory region\n");
1913 		snd_echo_free(chip);
1914 		return -EBUSY;
1915 	}
1916 	chip->dsp_registers = (volatile u32 __iomem *)
1917 		ioremap_nocache(chip->dsp_registers_phys, sz);
1918 	if (!chip->dsp_registers) {
1919 		dev_err(chip->card->dev, "ioremap failed\n");
1920 		snd_echo_free(chip);
1921 		return -ENOMEM;
1922 	}
1923 
1924 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1925 			KBUILD_MODNAME, chip)) {
1926 		dev_err(chip->card->dev, "cannot grab irq\n");
1927 		snd_echo_free(chip);
1928 		return -EBUSY;
1929 	}
1930 	chip->irq = pci->irq;
1931 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1932 		chip->pci, chip->irq, chip->pci->subsystem_device);
1933 
1934 	/* Create the DSP comm page - this is the area of memory used for most
1935 	of the communication with the DSP, which accesses it via bus mastering */
1936 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
1937 				sizeof(struct comm_page),
1938 				&chip->commpage_dma_buf) < 0) {
1939 		dev_err(chip->card->dev, "cannot allocate the comm page\n");
1940 		snd_echo_free(chip);
1941 		return -ENOMEM;
1942 	}
1943 	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1944 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1945 
1946 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1947 	if (err >= 0)
1948 		err = set_mixer_defaults(chip);
1949 	if (err < 0) {
1950 		dev_err(card->dev, "init_hw err=%d\n", err);
1951 		snd_echo_free(chip);
1952 		return err;
1953 	}
1954 
1955 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1956 		snd_echo_free(chip);
1957 		return err;
1958 	}
1959 	*rchip = chip;
1960 	/* Init done ! */
1961 	return 0;
1962 }
1963 
1964 
1965 
1966 /* constructor */
1967 static int snd_echo_probe(struct pci_dev *pci,
1968 			  const struct pci_device_id *pci_id)
1969 {
1970 	static int dev;
1971 	struct snd_card *card;
1972 	struct echoaudio *chip;
1973 	char *dsp;
1974 	int i, err;
1975 
1976 	if (dev >= SNDRV_CARDS)
1977 		return -ENODEV;
1978 	if (!enable[dev]) {
1979 		dev++;
1980 		return -ENOENT;
1981 	}
1982 
1983 	i = 0;
1984 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1985 			   0, &card);
1986 	if (err < 0)
1987 		return err;
1988 
1989 	chip = NULL;	/* Tells snd_echo_create to allocate chip */
1990 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
1991 		snd_card_free(card);
1992 		return err;
1993 	}
1994 
1995 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
1996 	strcpy(card->shortname, chip->card_name);
1997 
1998 	dsp = "56301";
1999 	if (pci_id->device == 0x3410)
2000 		dsp = "56361";
2001 
2002 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2003 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2004 		chip->dsp_registers_phys, chip->irq);
2005 
2006 	if ((err = snd_echo_new_pcm(chip)) < 0) {
2007 		dev_err(chip->card->dev, "new pcm error %d\n", err);
2008 		snd_card_free(card);
2009 		return err;
2010 	}
2011 
2012 #ifdef ECHOCARD_HAS_MIDI
2013 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2014 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
2015 			dev_err(chip->card->dev, "new midi error %d\n", err);
2016 			snd_card_free(card);
2017 			return err;
2018 		}
2019 	}
2020 #endif
2021 
2022 #ifdef ECHOCARD_HAS_VMIXER
2023 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2024 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2025 		goto ctl_error;
2026 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2027 	err = snd_ctl_add(chip->card,
2028 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2029 	if (err < 0)
2030 		goto ctl_error;
2031 #endif
2032 #else /* ECHOCARD_HAS_VMIXER */
2033 	err = snd_ctl_add(chip->card,
2034 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2035 	if (err < 0)
2036 		goto ctl_error;
2037 #endif /* ECHOCARD_HAS_VMIXER */
2038 
2039 #ifdef ECHOCARD_HAS_INPUT_GAIN
2040 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2041 		goto ctl_error;
2042 #endif
2043 
2044 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2045 	if (!chip->hasnt_input_nominal_level)
2046 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2047 			goto ctl_error;
2048 #endif
2049 
2050 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2051 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2052 		goto ctl_error;
2053 #endif
2054 
2055 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2056 		goto ctl_error;
2057 
2058 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2059 		goto ctl_error;
2060 
2061 #ifdef ECHOCARD_HAS_MONITOR
2062 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2063 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2064 		goto ctl_error;
2065 #endif
2066 
2067 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2068 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2069 		goto ctl_error;
2070 #endif
2071 
2072 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2073 		goto ctl_error;
2074 
2075 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2076 	/* Creates a list of available digital modes */
2077 	chip->num_digital_modes = 0;
2078 	for (i = 0; i < 6; i++)
2079 		if (chip->digital_modes & (1 << i))
2080 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2081 
2082 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2083 		goto ctl_error;
2084 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2085 
2086 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2087 	/* Creates a list of available clock sources */
2088 	chip->num_clock_sources = 0;
2089 	for (i = 0; i < 10; i++)
2090 		if (chip->input_clock_types & (1 << i))
2091 			chip->clock_source_list[chip->num_clock_sources++] = i;
2092 
2093 	if (chip->num_clock_sources > 1) {
2094 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2095 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2096 			goto ctl_error;
2097 	}
2098 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2099 
2100 #ifdef ECHOCARD_HAS_DIGITAL_IO
2101 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2102 		goto ctl_error;
2103 #endif
2104 
2105 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2106 	if (chip->has_phantom_power)
2107 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2108 			goto ctl_error;
2109 #endif
2110 
2111 	err = snd_card_register(card);
2112 	if (err < 0)
2113 		goto ctl_error;
2114 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2115 
2116 	pci_set_drvdata(pci, chip);
2117 	dev++;
2118 	return 0;
2119 
2120 ctl_error:
2121 	dev_err(card->dev, "new control error %d\n", err);
2122 	snd_card_free(card);
2123 	return err;
2124 }
2125 
2126 
2127 
2128 #if defined(CONFIG_PM_SLEEP)
2129 
2130 static int snd_echo_suspend(struct device *dev)
2131 {
2132 	struct echoaudio *chip = dev_get_drvdata(dev);
2133 
2134 #ifdef ECHOCARD_HAS_MIDI
2135 	/* This call can sleep */
2136 	if (chip->midi_out)
2137 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2138 #endif
2139 	spin_lock_irq(&chip->lock);
2140 	if (wait_handshake(chip)) {
2141 		spin_unlock_irq(&chip->lock);
2142 		return -EIO;
2143 	}
2144 	clear_handshake(chip);
2145 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2146 		spin_unlock_irq(&chip->lock);
2147 		return -EIO;
2148 	}
2149 	spin_unlock_irq(&chip->lock);
2150 
2151 	chip->dsp_code = NULL;
2152 	free_irq(chip->irq, chip);
2153 	chip->irq = -1;
2154 	return 0;
2155 }
2156 
2157 
2158 
2159 static int snd_echo_resume(struct device *dev)
2160 {
2161 	struct pci_dev *pci = to_pci_dev(dev);
2162 	struct echoaudio *chip = dev_get_drvdata(dev);
2163 	struct comm_page *commpage, *commpage_bak;
2164 	u32 pipe_alloc_mask;
2165 	int err;
2166 
2167 	commpage = chip->comm_page;
2168 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2169 	if (commpage_bak == NULL)
2170 		return -ENOMEM;
2171 
2172 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2173 	if (err < 0) {
2174 		kfree(commpage_bak);
2175 		dev_err(dev, "resume init_hw err=%d\n", err);
2176 		snd_echo_free(chip);
2177 		return err;
2178 	}
2179 
2180 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2181 	 * restore_dsp_settings() fails.
2182 	 */
2183 	pipe_alloc_mask = chip->pipe_alloc_mask;
2184 	chip->pipe_alloc_mask = 0;
2185 	err = restore_dsp_rettings(chip);
2186 	chip->pipe_alloc_mask = pipe_alloc_mask;
2187 	if (err < 0) {
2188 		kfree(commpage_bak);
2189 		return err;
2190 	}
2191 
2192 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2193 		sizeof(commpage->audio_format));
2194 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2195 		sizeof(commpage->sglist_addr));
2196 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2197 		sizeof(commpage->midi_output));
2198 	kfree(commpage_bak);
2199 
2200 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2201 			KBUILD_MODNAME, chip)) {
2202 		dev_err(chip->card->dev, "cannot grab irq\n");
2203 		snd_echo_free(chip);
2204 		return -EBUSY;
2205 	}
2206 	chip->irq = pci->irq;
2207 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2208 
2209 #ifdef ECHOCARD_HAS_MIDI
2210 	if (chip->midi_input_enabled)
2211 		enable_midi_input(chip, true);
2212 	if (chip->midi_out)
2213 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2214 #endif
2215 
2216 	return 0;
2217 }
2218 
2219 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2220 #define SND_ECHO_PM_OPS	&snd_echo_pm
2221 #else
2222 #define SND_ECHO_PM_OPS	NULL
2223 #endif /* CONFIG_PM_SLEEP */
2224 
2225 
2226 static void snd_echo_remove(struct pci_dev *pci)
2227 {
2228 	struct echoaudio *chip;
2229 
2230 	chip = pci_get_drvdata(pci);
2231 	if (chip)
2232 		snd_card_free(chip->card);
2233 }
2234 
2235 
2236 
2237 /******************************************************************************
2238 	Everything starts and ends here
2239 ******************************************************************************/
2240 
2241 /* pci_driver definition */
2242 static struct pci_driver echo_driver = {
2243 	.name = KBUILD_MODNAME,
2244 	.id_table = snd_echo_ids,
2245 	.probe = snd_echo_probe,
2246 	.remove = snd_echo_remove,
2247 	.driver = {
2248 		.pm = SND_ECHO_PM_OPS,
2249 	},
2250 };
2251 
2252 module_pci_driver(echo_driver);
2253