xref: /linux/sound/pci/echoaudio/echoaudio.c (revision 4be5e8648b0c287aefc6ac3f3a0b12c696054f43)
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 const 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 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1234 	uinfo->count = 1;
1235 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1236 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1237 	return 0;
1238 }
1239 
1240 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1241 			      struct snd_ctl_elem_value *ucontrol)
1242 {
1243 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1244 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1245 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1246 
1247 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1248 		return -EINVAL;
1249 
1250 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1251 	return 0;
1252 }
1253 
1254 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1255 			      struct snd_ctl_elem_value *ucontrol)
1256 {
1257 	struct echoaudio *chip;
1258 	int changed,  gain;
1259 	unsigned int out, in;
1260 
1261 	changed = 0;
1262 	chip = snd_kcontrol_chip(kcontrol);
1263 	out = ucontrol->id.index / num_busses_in(chip);
1264 	in = ucontrol->id.index % num_busses_in(chip);
1265 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1266 		return -EINVAL;
1267 	gain = ucontrol->value.integer.value[0];
1268 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1269 		return -EINVAL;
1270 	if (chip->monitor_gain[out][in] != gain) {
1271 		spin_lock_irq(&chip->lock);
1272 		set_monitor_gain(chip, out, in, gain);
1273 		update_output_line_level(chip);
1274 		spin_unlock_irq(&chip->lock);
1275 		changed = 1;
1276 	}
1277 	return changed;
1278 }
1279 
1280 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1281 	.name = "Monitor Mixer Volume",
1282 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1283 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1284 	.info = snd_echo_mixer_info,
1285 	.get = snd_echo_mixer_get,
1286 	.put = snd_echo_mixer_put,
1287 	.tlv = {.p = db_scale_output_gain},
1288 };
1289 
1290 #endif /* ECHOCARD_HAS_MONITOR */
1291 
1292 
1293 
1294 #ifdef ECHOCARD_HAS_VMIXER
1295 
1296 /******************* Vmixer *******************/
1297 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1298 				struct snd_ctl_elem_info *uinfo)
1299 {
1300 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1301 	uinfo->count = 1;
1302 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1303 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1304 	return 0;
1305 }
1306 
1307 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1308 			       struct snd_ctl_elem_value *ucontrol)
1309 {
1310 	struct echoaudio *chip;
1311 
1312 	chip = snd_kcontrol_chip(kcontrol);
1313 	ucontrol->value.integer.value[0] =
1314 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1315 			[ucontrol->id.index % num_pipes_out(chip)];
1316 	return 0;
1317 }
1318 
1319 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1320 			       struct snd_ctl_elem_value *ucontrol)
1321 {
1322 	struct echoaudio *chip;
1323 	int gain, changed;
1324 	short vch, out;
1325 
1326 	changed = 0;
1327 	chip = snd_kcontrol_chip(kcontrol);
1328 	out = ucontrol->id.index / num_pipes_out(chip);
1329 	vch = ucontrol->id.index % num_pipes_out(chip);
1330 	gain = ucontrol->value.integer.value[0];
1331 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1332 		return -EINVAL;
1333 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1334 		spin_lock_irq(&chip->lock);
1335 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1336 		update_vmixer_level(chip);
1337 		spin_unlock_irq(&chip->lock);
1338 		changed = 1;
1339 	}
1340 	return changed;
1341 }
1342 
1343 static struct snd_kcontrol_new snd_echo_vmixer = {
1344 	.name = "VMixer Volume",
1345 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1346 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1347 	.info = snd_echo_vmixer_info,
1348 	.get = snd_echo_vmixer_get,
1349 	.put = snd_echo_vmixer_put,
1350 	.tlv = {.p = db_scale_output_gain},
1351 };
1352 
1353 #endif /* ECHOCARD_HAS_VMIXER */
1354 
1355 
1356 
1357 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1358 
1359 /******************* Digital mode switch *******************/
1360 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1361 				      struct snd_ctl_elem_info *uinfo)
1362 {
1363 	static const char * const names[4] = {
1364 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1365 		"S/PDIF Cdrom"
1366 	};
1367 	struct echoaudio *chip;
1368 
1369 	chip = snd_kcontrol_chip(kcontrol);
1370 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1371 }
1372 
1373 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1374 				     struct snd_ctl_elem_value *ucontrol)
1375 {
1376 	struct echoaudio *chip;
1377 	int i, mode;
1378 
1379 	chip = snd_kcontrol_chip(kcontrol);
1380 	mode = chip->digital_mode;
1381 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1382 		if (mode == chip->digital_mode_list[i]) {
1383 			ucontrol->value.enumerated.item[0] = i;
1384 			break;
1385 		}
1386 	return 0;
1387 }
1388 
1389 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1390 				     struct snd_ctl_elem_value *ucontrol)
1391 {
1392 	struct echoaudio *chip;
1393 	int changed;
1394 	unsigned short emode, dmode;
1395 
1396 	changed = 0;
1397 	chip = snd_kcontrol_chip(kcontrol);
1398 
1399 	emode = ucontrol->value.enumerated.item[0];
1400 	if (emode >= chip->num_digital_modes)
1401 		return -EINVAL;
1402 	dmode = chip->digital_mode_list[emode];
1403 
1404 	if (dmode != chip->digital_mode) {
1405 		/* mode_mutex is required to make this operation atomic wrt
1406 		pcm_digital_*_open() and set_input_clock() functions. */
1407 		mutex_lock(&chip->mode_mutex);
1408 
1409 		/* Do not allow the user to change the digital mode when a pcm
1410 		device is open because it also changes the number of channels
1411 		and the allowed sample rates */
1412 		if (atomic_read(&chip->opencount)) {
1413 			changed = -EAGAIN;
1414 		} else {
1415 			changed = set_digital_mode(chip, dmode);
1416 			/* If we had to change the clock source, report it */
1417 			if (changed > 0 && chip->clock_src_ctl) {
1418 				snd_ctl_notify(chip->card,
1419 					       SNDRV_CTL_EVENT_MASK_VALUE,
1420 					       &chip->clock_src_ctl->id);
1421 				dev_dbg(chip->card->dev,
1422 					"SDM() =%d\n", changed);
1423 			}
1424 			if (changed >= 0)
1425 				changed = 1;	/* No errors */
1426 		}
1427 		mutex_unlock(&chip->mode_mutex);
1428 	}
1429 	return changed;
1430 }
1431 
1432 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1433 	.name = "Digital mode Switch",
1434 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1435 	.info = snd_echo_digital_mode_info,
1436 	.get = snd_echo_digital_mode_get,
1437 	.put = snd_echo_digital_mode_put,
1438 };
1439 
1440 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1441 
1442 
1443 
1444 #ifdef ECHOCARD_HAS_DIGITAL_IO
1445 
1446 /******************* S/PDIF mode switch *******************/
1447 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1448 				    struct snd_ctl_elem_info *uinfo)
1449 {
1450 	static const char * const names[2] = {"Consumer", "Professional"};
1451 
1452 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1453 }
1454 
1455 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1456 				   struct snd_ctl_elem_value *ucontrol)
1457 {
1458 	struct echoaudio *chip;
1459 
1460 	chip = snd_kcontrol_chip(kcontrol);
1461 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1462 	return 0;
1463 }
1464 
1465 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1466 				   struct snd_ctl_elem_value *ucontrol)
1467 {
1468 	struct echoaudio *chip;
1469 	int mode;
1470 
1471 	chip = snd_kcontrol_chip(kcontrol);
1472 	mode = !!ucontrol->value.enumerated.item[0];
1473 	if (mode != chip->professional_spdif) {
1474 		spin_lock_irq(&chip->lock);
1475 		set_professional_spdif(chip, mode);
1476 		spin_unlock_irq(&chip->lock);
1477 		return 1;
1478 	}
1479 	return 0;
1480 }
1481 
1482 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1483 	.name = "S/PDIF mode Switch",
1484 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1485 	.info = snd_echo_spdif_mode_info,
1486 	.get = snd_echo_spdif_mode_get,
1487 	.put = snd_echo_spdif_mode_put,
1488 };
1489 
1490 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1491 
1492 
1493 
1494 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1495 
1496 /******************* Select input clock source *******************/
1497 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1498 				      struct snd_ctl_elem_info *uinfo)
1499 {
1500 	static const char * const names[8] = {
1501 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1502 		"ESync96", "MTC"
1503 	};
1504 	struct echoaudio *chip;
1505 
1506 	chip = snd_kcontrol_chip(kcontrol);
1507 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1508 }
1509 
1510 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1511 				     struct snd_ctl_elem_value *ucontrol)
1512 {
1513 	struct echoaudio *chip;
1514 	int i, clock;
1515 
1516 	chip = snd_kcontrol_chip(kcontrol);
1517 	clock = chip->input_clock;
1518 
1519 	for (i = 0; i < chip->num_clock_sources; i++)
1520 		if (clock == chip->clock_source_list[i])
1521 			ucontrol->value.enumerated.item[0] = i;
1522 
1523 	return 0;
1524 }
1525 
1526 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1527 				     struct snd_ctl_elem_value *ucontrol)
1528 {
1529 	struct echoaudio *chip;
1530 	int changed;
1531 	unsigned int eclock, dclock;
1532 
1533 	changed = 0;
1534 	chip = snd_kcontrol_chip(kcontrol);
1535 	eclock = ucontrol->value.enumerated.item[0];
1536 	if (eclock >= chip->input_clock_types)
1537 		return -EINVAL;
1538 	dclock = chip->clock_source_list[eclock];
1539 	if (chip->input_clock != dclock) {
1540 		mutex_lock(&chip->mode_mutex);
1541 		spin_lock_irq(&chip->lock);
1542 		if ((changed = set_input_clock(chip, dclock)) == 0)
1543 			changed = 1;	/* no errors */
1544 		spin_unlock_irq(&chip->lock);
1545 		mutex_unlock(&chip->mode_mutex);
1546 	}
1547 
1548 	if (changed < 0)
1549 		dev_dbg(chip->card->dev,
1550 			"seticlk val%d err 0x%x\n", dclock, changed);
1551 
1552 	return changed;
1553 }
1554 
1555 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1556 	.name = "Sample Clock Source",
1557 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1558 	.info = snd_echo_clock_source_info,
1559 	.get = snd_echo_clock_source_get,
1560 	.put = snd_echo_clock_source_put,
1561 };
1562 
1563 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1564 
1565 
1566 
1567 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1568 
1569 /******************* Phantom power switch *******************/
1570 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1571 
1572 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1573 				      struct snd_ctl_elem_value *ucontrol)
1574 {
1575 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1576 
1577 	ucontrol->value.integer.value[0] = chip->phantom_power;
1578 	return 0;
1579 }
1580 
1581 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1582 				      struct snd_ctl_elem_value *ucontrol)
1583 {
1584 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1585 	int power, changed = 0;
1586 
1587 	power = !!ucontrol->value.integer.value[0];
1588 	if (chip->phantom_power != power) {
1589 		spin_lock_irq(&chip->lock);
1590 		changed = set_phantom_power(chip, power);
1591 		spin_unlock_irq(&chip->lock);
1592 		if (changed == 0)
1593 			changed = 1;	/* no errors */
1594 	}
1595 	return changed;
1596 }
1597 
1598 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1599 	.name = "Phantom power Switch",
1600 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1601 	.info = snd_echo_phantom_power_info,
1602 	.get = snd_echo_phantom_power_get,
1603 	.put = snd_echo_phantom_power_put,
1604 };
1605 
1606 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1607 
1608 
1609 
1610 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1611 
1612 /******************* Digital input automute switch *******************/
1613 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1614 
1615 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1616 				 struct snd_ctl_elem_value *ucontrol)
1617 {
1618 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1619 
1620 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1621 	return 0;
1622 }
1623 
1624 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1625 				 struct snd_ctl_elem_value *ucontrol)
1626 {
1627 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1628 	int automute, changed = 0;
1629 
1630 	automute = !!ucontrol->value.integer.value[0];
1631 	if (chip->digital_in_automute != automute) {
1632 		spin_lock_irq(&chip->lock);
1633 		changed = set_input_auto_mute(chip, automute);
1634 		spin_unlock_irq(&chip->lock);
1635 		if (changed == 0)
1636 			changed = 1;	/* no errors */
1637 	}
1638 	return changed;
1639 }
1640 
1641 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1642 	.name = "Digital Capture Switch (automute)",
1643 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1644 	.info = snd_echo_automute_info,
1645 	.get = snd_echo_automute_get,
1646 	.put = snd_echo_automute_put,
1647 };
1648 
1649 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1650 
1651 
1652 
1653 /******************* VU-meters switch *******************/
1654 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1655 
1656 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1657 					struct snd_ctl_elem_value *ucontrol)
1658 {
1659 	struct echoaudio *chip;
1660 
1661 	chip = snd_kcontrol_chip(kcontrol);
1662 	spin_lock_irq(&chip->lock);
1663 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1664 	spin_unlock_irq(&chip->lock);
1665 	return 1;
1666 }
1667 
1668 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1669 	.name = "VU-meters Switch",
1670 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1671 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1672 	.info = snd_echo_vumeters_switch_info,
1673 	.put = snd_echo_vumeters_switch_put,
1674 };
1675 
1676 
1677 
1678 /***** Read VU-meters (input, output, analog and digital together) *****/
1679 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1680 				  struct snd_ctl_elem_info *uinfo)
1681 {
1682 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1683 	uinfo->count = 96;
1684 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1685 	uinfo->value.integer.max = 0;
1686 	return 0;
1687 }
1688 
1689 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1690 				 struct snd_ctl_elem_value *ucontrol)
1691 {
1692 	struct echoaudio *chip;
1693 
1694 	chip = snd_kcontrol_chip(kcontrol);
1695 	get_audio_meters(chip, ucontrol->value.integer.value);
1696 	return 0;
1697 }
1698 
1699 static const struct snd_kcontrol_new snd_echo_vumeters = {
1700 	.name = "VU-meters",
1701 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1702 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1703 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1704 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1705 	.info = snd_echo_vumeters_info,
1706 	.get = snd_echo_vumeters_get,
1707 	.tlv = {.p = db_scale_output_gain},
1708 };
1709 
1710 
1711 
1712 /*** Channels info - it exports informations about the number of channels ***/
1713 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1714 				       struct snd_ctl_elem_info *uinfo)
1715 {
1716 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1717 	uinfo->count = 6;
1718 	uinfo->value.integer.min = 0;
1719 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1720 	return 0;
1721 }
1722 
1723 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1724 				      struct snd_ctl_elem_value *ucontrol)
1725 {
1726 	struct echoaudio *chip;
1727 	int detected, clocks, bit, src;
1728 
1729 	chip = snd_kcontrol_chip(kcontrol);
1730 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1731 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1732 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1733 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1734 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1735 
1736 	/* Compute the bitmask of the currently valid input clocks */
1737 	detected = detect_input_clocks(chip);
1738 	clocks = 0;
1739 	src = chip->num_clock_sources - 1;
1740 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1741 		if (detected & (1 << bit))
1742 			for (; src >= 0; src--)
1743 				if (bit == chip->clock_source_list[src]) {
1744 					clocks |= 1 << src;
1745 					break;
1746 				}
1747 	ucontrol->value.integer.value[5] = clocks;
1748 
1749 	return 0;
1750 }
1751 
1752 static const struct snd_kcontrol_new snd_echo_channels_info = {
1753 	.name = "Channels info",
1754 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1755 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1756 	.info = snd_echo_channels_info_info,
1757 	.get = snd_echo_channels_info_get,
1758 };
1759 
1760 
1761 
1762 
1763 /******************************************************************************
1764 	IRQ Handler
1765 ******************************************************************************/
1766 
1767 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1768 {
1769 	struct echoaudio *chip = dev_id;
1770 	struct snd_pcm_substream *substream;
1771 	int period, ss, st;
1772 
1773 	spin_lock(&chip->lock);
1774 	st = service_irq(chip);
1775 	if (st < 0) {
1776 		spin_unlock(&chip->lock);
1777 		return IRQ_NONE;
1778 	}
1779 	/* The hardware doesn't tell us which substream caused the irq,
1780 	thus we have to check all running substreams. */
1781 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1782 		substream = chip->substream[ss];
1783 		if (substream && ((struct audiopipe *)substream->runtime->
1784 				private_data)->state == PIPE_STATE_STARTED) {
1785 			period = pcm_pointer(substream) /
1786 				substream->runtime->period_size;
1787 			if (period != chip->last_period[ss]) {
1788 				chip->last_period[ss] = period;
1789 				spin_unlock(&chip->lock);
1790 				snd_pcm_period_elapsed(substream);
1791 				spin_lock(&chip->lock);
1792 			}
1793 		}
1794 	}
1795 	spin_unlock(&chip->lock);
1796 
1797 #ifdef ECHOCARD_HAS_MIDI
1798 	if (st > 0 && chip->midi_in) {
1799 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1800 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1801 	}
1802 #endif
1803 	return IRQ_HANDLED;
1804 }
1805 
1806 
1807 
1808 
1809 /******************************************************************************
1810 	Module construction / destruction
1811 ******************************************************************************/
1812 
1813 static int snd_echo_free(struct echoaudio *chip)
1814 {
1815 	if (chip->comm_page)
1816 		rest_in_peace(chip);
1817 
1818 	if (chip->irq >= 0)
1819 		free_irq(chip->irq, chip);
1820 
1821 	if (chip->comm_page)
1822 		snd_dma_free_pages(&chip->commpage_dma_buf);
1823 
1824 	iounmap(chip->dsp_registers);
1825 	release_and_free_resource(chip->iores);
1826 	pci_disable_device(chip->pci);
1827 
1828 	/* release chip data */
1829 	free_firmware_cache(chip);
1830 	kfree(chip);
1831 	return 0;
1832 }
1833 
1834 
1835 
1836 static int snd_echo_dev_free(struct snd_device *device)
1837 {
1838 	struct echoaudio *chip = device->device_data;
1839 
1840 	return snd_echo_free(chip);
1841 }
1842 
1843 
1844 
1845 /* <--snd_echo_probe() */
1846 static int snd_echo_create(struct snd_card *card,
1847 			   struct pci_dev *pci,
1848 			   struct echoaudio **rchip)
1849 {
1850 	struct echoaudio *chip;
1851 	int err;
1852 	size_t sz;
1853 	static const struct snd_device_ops ops = {
1854 		.dev_free = snd_echo_dev_free,
1855 	};
1856 
1857 	*rchip = NULL;
1858 
1859 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1860 
1861 	if ((err = pci_enable_device(pci)) < 0)
1862 		return err;
1863 	pci_set_master(pci);
1864 
1865 	/* Allocate chip if needed */
1866 	if (!*rchip) {
1867 		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1868 		if (!chip) {
1869 			pci_disable_device(pci);
1870 			return -ENOMEM;
1871 		}
1872 		dev_dbg(card->dev, "chip=%p\n", chip);
1873 		spin_lock_init(&chip->lock);
1874 		chip->card = card;
1875 		chip->pci = pci;
1876 		chip->irq = -1;
1877 		atomic_set(&chip->opencount, 0);
1878 		mutex_init(&chip->mode_mutex);
1879 		chip->can_set_rate = 1;
1880 	} else {
1881 		/* If this was called from the resume function, chip is
1882 		 * already allocated and it contains current card settings.
1883 		 */
1884 		chip = *rchip;
1885 	}
1886 
1887 	/* PCI resource allocation */
1888 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1889 	sz = pci_resource_len(pci, 0);
1890 	if (sz > PAGE_SIZE)
1891 		sz = PAGE_SIZE;		/* We map only the required part */
1892 
1893 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1894 					      ECHOCARD_NAME)) == NULL) {
1895 		dev_err(chip->card->dev, "cannot get memory region\n");
1896 		snd_echo_free(chip);
1897 		return -EBUSY;
1898 	}
1899 	chip->dsp_registers = (volatile u32 __iomem *)
1900 		ioremap(chip->dsp_registers_phys, sz);
1901 	if (!chip->dsp_registers) {
1902 		dev_err(chip->card->dev, "ioremap failed\n");
1903 		snd_echo_free(chip);
1904 		return -ENOMEM;
1905 	}
1906 
1907 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1908 			KBUILD_MODNAME, chip)) {
1909 		dev_err(chip->card->dev, "cannot grab irq\n");
1910 		snd_echo_free(chip);
1911 		return -EBUSY;
1912 	}
1913 	chip->irq = pci->irq;
1914 	card->sync_irq = chip->irq;
1915 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1916 		chip->pci, chip->irq, chip->pci->subsystem_device);
1917 
1918 	/* Create the DSP comm page - this is the area of memory used for most
1919 	of the communication with the DSP, which accesses it via bus mastering */
1920 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
1921 				sizeof(struct comm_page),
1922 				&chip->commpage_dma_buf) < 0) {
1923 		dev_err(chip->card->dev, "cannot allocate the comm page\n");
1924 		snd_echo_free(chip);
1925 		return -ENOMEM;
1926 	}
1927 	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1928 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1929 
1930 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1931 	if (err >= 0)
1932 		err = set_mixer_defaults(chip);
1933 	if (err < 0) {
1934 		dev_err(card->dev, "init_hw err=%d\n", err);
1935 		snd_echo_free(chip);
1936 		return err;
1937 	}
1938 
1939 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1940 		snd_echo_free(chip);
1941 		return err;
1942 	}
1943 	*rchip = chip;
1944 	/* Init done ! */
1945 	return 0;
1946 }
1947 
1948 
1949 
1950 /* constructor */
1951 static int snd_echo_probe(struct pci_dev *pci,
1952 			  const struct pci_device_id *pci_id)
1953 {
1954 	static int dev;
1955 	struct snd_card *card;
1956 	struct echoaudio *chip;
1957 	char *dsp;
1958 	int i, err;
1959 
1960 	if (dev >= SNDRV_CARDS)
1961 		return -ENODEV;
1962 	if (!enable[dev]) {
1963 		dev++;
1964 		return -ENOENT;
1965 	}
1966 
1967 	i = 0;
1968 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1969 			   0, &card);
1970 	if (err < 0)
1971 		return err;
1972 
1973 	chip = NULL;	/* Tells snd_echo_create to allocate chip */
1974 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
1975 		snd_card_free(card);
1976 		return err;
1977 	}
1978 
1979 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
1980 	strcpy(card->shortname, chip->card_name);
1981 
1982 	dsp = "56301";
1983 	if (pci_id->device == 0x3410)
1984 		dsp = "56361";
1985 
1986 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
1987 		card->shortname, pci_id->subdevice & 0x000f, dsp,
1988 		chip->dsp_registers_phys, chip->irq);
1989 
1990 	if ((err = snd_echo_new_pcm(chip)) < 0) {
1991 		dev_err(chip->card->dev, "new pcm error %d\n", err);
1992 		snd_card_free(card);
1993 		return err;
1994 	}
1995 
1996 #ifdef ECHOCARD_HAS_MIDI
1997 	if (chip->has_midi) {	/* Some Mia's do not have midi */
1998 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
1999 			dev_err(chip->card->dev, "new midi error %d\n", err);
2000 			snd_card_free(card);
2001 			return err;
2002 		}
2003 	}
2004 #endif
2005 
2006 #ifdef ECHOCARD_HAS_VMIXER
2007 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2008 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2009 		goto ctl_error;
2010 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2011 	err = snd_ctl_add(chip->card,
2012 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2013 	if (err < 0)
2014 		goto ctl_error;
2015 #endif
2016 #else /* ECHOCARD_HAS_VMIXER */
2017 	err = snd_ctl_add(chip->card,
2018 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2019 	if (err < 0)
2020 		goto ctl_error;
2021 #endif /* ECHOCARD_HAS_VMIXER */
2022 
2023 #ifdef ECHOCARD_HAS_INPUT_GAIN
2024 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2025 		goto ctl_error;
2026 #endif
2027 
2028 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2029 	if (!chip->hasnt_input_nominal_level)
2030 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2031 			goto ctl_error;
2032 #endif
2033 
2034 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2035 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2036 		goto ctl_error;
2037 #endif
2038 
2039 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2040 		goto ctl_error;
2041 
2042 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2043 		goto ctl_error;
2044 
2045 #ifdef ECHOCARD_HAS_MONITOR
2046 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2047 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2048 		goto ctl_error;
2049 #endif
2050 
2051 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2052 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2053 		goto ctl_error;
2054 #endif
2055 
2056 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2057 		goto ctl_error;
2058 
2059 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2060 	/* Creates a list of available digital modes */
2061 	chip->num_digital_modes = 0;
2062 	for (i = 0; i < 6; i++)
2063 		if (chip->digital_modes & (1 << i))
2064 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2065 
2066 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2067 		goto ctl_error;
2068 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2069 
2070 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2071 	/* Creates a list of available clock sources */
2072 	chip->num_clock_sources = 0;
2073 	for (i = 0; i < 10; i++)
2074 		if (chip->input_clock_types & (1 << i))
2075 			chip->clock_source_list[chip->num_clock_sources++] = i;
2076 
2077 	if (chip->num_clock_sources > 1) {
2078 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2079 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2080 			goto ctl_error;
2081 	}
2082 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2083 
2084 #ifdef ECHOCARD_HAS_DIGITAL_IO
2085 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2086 		goto ctl_error;
2087 #endif
2088 
2089 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2090 	if (chip->has_phantom_power)
2091 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2092 			goto ctl_error;
2093 #endif
2094 
2095 	err = snd_card_register(card);
2096 	if (err < 0)
2097 		goto ctl_error;
2098 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2099 
2100 	pci_set_drvdata(pci, chip);
2101 	dev++;
2102 	return 0;
2103 
2104 ctl_error:
2105 	dev_err(card->dev, "new control error %d\n", err);
2106 	snd_card_free(card);
2107 	return err;
2108 }
2109 
2110 
2111 
2112 #if defined(CONFIG_PM_SLEEP)
2113 
2114 static int snd_echo_suspend(struct device *dev)
2115 {
2116 	struct echoaudio *chip = dev_get_drvdata(dev);
2117 
2118 #ifdef ECHOCARD_HAS_MIDI
2119 	/* This call can sleep */
2120 	if (chip->midi_out)
2121 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2122 #endif
2123 	spin_lock_irq(&chip->lock);
2124 	if (wait_handshake(chip)) {
2125 		spin_unlock_irq(&chip->lock);
2126 		return -EIO;
2127 	}
2128 	clear_handshake(chip);
2129 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2130 		spin_unlock_irq(&chip->lock);
2131 		return -EIO;
2132 	}
2133 	spin_unlock_irq(&chip->lock);
2134 
2135 	chip->dsp_code = NULL;
2136 	free_irq(chip->irq, chip);
2137 	chip->irq = -1;
2138 	chip->card->sync_irq = -1;
2139 	return 0;
2140 }
2141 
2142 
2143 
2144 static int snd_echo_resume(struct device *dev)
2145 {
2146 	struct pci_dev *pci = to_pci_dev(dev);
2147 	struct echoaudio *chip = dev_get_drvdata(dev);
2148 	struct comm_page *commpage, *commpage_bak;
2149 	u32 pipe_alloc_mask;
2150 	int err;
2151 
2152 	commpage = chip->comm_page;
2153 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2154 	if (commpage_bak == NULL)
2155 		return -ENOMEM;
2156 
2157 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2158 	if (err < 0) {
2159 		kfree(commpage_bak);
2160 		dev_err(dev, "resume init_hw err=%d\n", err);
2161 		snd_echo_free(chip);
2162 		return err;
2163 	}
2164 
2165 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2166 	 * restore_dsp_settings() fails.
2167 	 */
2168 	pipe_alloc_mask = chip->pipe_alloc_mask;
2169 	chip->pipe_alloc_mask = 0;
2170 	err = restore_dsp_rettings(chip);
2171 	chip->pipe_alloc_mask = pipe_alloc_mask;
2172 	if (err < 0) {
2173 		kfree(commpage_bak);
2174 		return err;
2175 	}
2176 
2177 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2178 		sizeof(commpage->audio_format));
2179 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2180 		sizeof(commpage->sglist_addr));
2181 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2182 		sizeof(commpage->midi_output));
2183 	kfree(commpage_bak);
2184 
2185 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2186 			KBUILD_MODNAME, chip)) {
2187 		dev_err(chip->card->dev, "cannot grab irq\n");
2188 		snd_echo_free(chip);
2189 		return -EBUSY;
2190 	}
2191 	chip->irq = pci->irq;
2192 	chip->card->sync_irq = chip->irq;
2193 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2194 
2195 #ifdef ECHOCARD_HAS_MIDI
2196 	if (chip->midi_input_enabled)
2197 		enable_midi_input(chip, true);
2198 	if (chip->midi_out)
2199 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2200 #endif
2201 
2202 	return 0;
2203 }
2204 
2205 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2206 #define SND_ECHO_PM_OPS	&snd_echo_pm
2207 #else
2208 #define SND_ECHO_PM_OPS	NULL
2209 #endif /* CONFIG_PM_SLEEP */
2210 
2211 
2212 static void snd_echo_remove(struct pci_dev *pci)
2213 {
2214 	struct echoaudio *chip;
2215 
2216 	chip = pci_get_drvdata(pci);
2217 	if (chip)
2218 		snd_card_free(chip->card);
2219 }
2220 
2221 
2222 
2223 /******************************************************************************
2224 	Everything starts and ends here
2225 ******************************************************************************/
2226 
2227 /* pci_driver definition */
2228 static struct pci_driver echo_driver = {
2229 	.name = KBUILD_MODNAME,
2230 	.id_table = snd_echo_ids,
2231 	.probe = snd_echo_probe,
2232 	.remove = snd_echo_remove,
2233 	.driver = {
2234 		.pm = SND_ECHO_PM_OPS,
2235 	},
2236 };
2237 
2238 module_pci_driver(echo_driver);
2239