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