xref: /linux/sound/pci/echoaudio/echoaudio.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
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  *  Copyright (C) 2020 Mark Hills <mark@xwax.org>
6  */
7 
8 #include <linux/module.h>
9 
10 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
11 MODULE_LICENSE("GPL v2");
12 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
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 	if (chip->fw_cache[fw_index]) {
38 		dev_dbg(chip->card->dev,
39 			"firmware requested: %s is cached\n",
40 			card_fw[fw_index].data);
41 		*fw_entry = chip->fw_cache[fw_index];
42 		return 0;
43 	}
44 
45 	dev_dbg(chip->card->dev,
46 		"firmware requested: %s\n", card_fw[fw_index].data);
47 	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
48 	err = request_firmware(fw_entry, name, &chip->pci->dev);
49 	if (err < 0)
50 		dev_err(chip->card->dev,
51 			"get_firmware(): Firmware not available (%d)\n", err);
52 	else
53 		chip->fw_cache[fw_index] = *fw_entry;
54 	return err;
55 }
56 
57 
58 
59 static void free_firmware(const struct firmware *fw_entry,
60 			  struct echoaudio *chip)
61 {
62 	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
63 }
64 
65 
66 
67 static void free_firmware_cache(struct echoaudio *chip)
68 {
69 	int i;
70 
71 	for (i = 0; i < 8 ; i++)
72 		if (chip->fw_cache[i]) {
73 			release_firmware(chip->fw_cache[i]);
74 			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
75 		}
76 }
77 
78 
79 
80 /******************************************************************************
81 	PCM interface
82 ******************************************************************************/
83 
84 static void audiopipe_free(struct snd_pcm_runtime *runtime)
85 {
86 	struct audiopipe *pipe = runtime->private_data;
87 
88 	if (pipe->sgpage.area)
89 		snd_dma_free_pages(&pipe->sgpage);
90 	kfree(pipe);
91 }
92 
93 
94 
95 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
96 					      struct snd_pcm_hw_rule *rule)
97 {
98 	struct snd_interval *c = hw_param_interval(params,
99 						   SNDRV_PCM_HW_PARAM_CHANNELS);
100 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
101 	struct snd_mask fmt;
102 
103 	snd_mask_any(&fmt);
104 
105 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
106 	/* >=2 channels cannot be S32_BE */
107 	if (c->min == 2) {
108 		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
109 		return snd_mask_refine(f, &fmt);
110 	}
111 #endif
112 	/* > 2 channels cannot be U8 and S32_BE */
113 	if (c->min > 2) {
114 		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
115 		return snd_mask_refine(f, &fmt);
116 	}
117 	/* Mono is ok with any format */
118 	return 0;
119 }
120 
121 
122 
123 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
124 					      struct snd_pcm_hw_rule *rule)
125 {
126 	struct snd_interval *c = hw_param_interval(params,
127 						   SNDRV_PCM_HW_PARAM_CHANNELS);
128 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
129 	struct snd_interval ch;
130 
131 	snd_interval_any(&ch);
132 
133 	/* S32_BE is mono (and stereo) only */
134 	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
135 		ch.min = 1;
136 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
137 		ch.max = 2;
138 #else
139 		ch.max = 1;
140 #endif
141 		ch.integer = 1;
142 		return snd_interval_refine(c, &ch);
143 	}
144 	/* U8 can be only mono or stereo */
145 	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
146 		ch.min = 1;
147 		ch.max = 2;
148 		ch.integer = 1;
149 		return snd_interval_refine(c, &ch);
150 	}
151 	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
152 	return 0;
153 }
154 
155 
156 
157 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
158 					       struct snd_pcm_hw_rule *rule)
159 {
160 	struct snd_interval *c = hw_param_interval(params,
161 						   SNDRV_PCM_HW_PARAM_CHANNELS);
162 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
163 	struct snd_mask fmt;
164 	u64 fmask;
165 	snd_mask_any(&fmt);
166 
167 	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
168 
169 	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
170 	if (c->min > 2) {
171 		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
172 			 SNDRV_PCM_FMTBIT_S24_3LE |
173 			 SNDRV_PCM_FMTBIT_S32_LE;
174 	/* 1 channel must be S32_BE or S32_LE */
175 	} else if (c->max == 1)
176 		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
177 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
178 	/* 2 channels cannot be S32_BE */
179 	else if (c->min == 2 && c->max == 2)
180 		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
181 #endif
182 	else
183 		return 0;
184 
185 	fmt.bits[0] &= (u32)fmask;
186 	fmt.bits[1] &= (u32)(fmask >> 32);
187 	return snd_mask_refine(f, &fmt);
188 }
189 
190 
191 
192 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
193 					       struct snd_pcm_hw_rule *rule)
194 {
195 	struct snd_interval *c = hw_param_interval(params,
196 						   SNDRV_PCM_HW_PARAM_CHANNELS);
197 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
198 	struct snd_interval ch;
199 	u64 fmask;
200 
201 	snd_interval_any(&ch);
202 	ch.integer = 1;
203 	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
204 
205 	/* S32_BE is mono (and stereo) only */
206 	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
207 		ch.min = 1;
208 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
209 		ch.max = 2;
210 #else
211 		ch.max = 1;
212 #endif
213 	/* U8 is stereo only */
214 	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
215 		ch.min = ch.max = 2;
216 	/* S16_LE and S24_3LE must be at least stereo */
217 	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
218 			       SNDRV_PCM_FMTBIT_S24_3LE)))
219 		ch.min = 2;
220 	else
221 		return 0;
222 
223 	return snd_interval_refine(c, &ch);
224 }
225 
226 
227 
228 /* Since the sample rate is a global setting, do allow the user to change the
229 sample rate only if there is only one pcm device open. */
230 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
231 			       struct snd_pcm_hw_rule *rule)
232 {
233 	struct snd_interval *rate = hw_param_interval(params,
234 						      SNDRV_PCM_HW_PARAM_RATE);
235 	struct echoaudio *chip = rule->private;
236 	struct snd_interval fixed;
237 	int err;
238 
239 	mutex_lock(&chip->mode_mutex);
240 
241 	if (chip->can_set_rate) {
242 		err = 0;
243 	} else {
244 		snd_interval_any(&fixed);
245 		fixed.min = fixed.max = chip->sample_rate;
246 		err = snd_interval_refine(rate, &fixed);
247 	}
248 
249 	mutex_unlock(&chip->mode_mutex);
250 	return err;
251 }
252 
253 
254 static int pcm_open(struct snd_pcm_substream *substream,
255 		    signed char max_channels)
256 {
257 	struct echoaudio *chip;
258 	struct snd_pcm_runtime *runtime;
259 	struct audiopipe *pipe;
260 	int err, i;
261 
262 	if (max_channels <= 0)
263 		return -EAGAIN;
264 
265 	chip = snd_pcm_substream_chip(substream);
266 	runtime = substream->runtime;
267 
268 	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
269 	if (!pipe)
270 		return -ENOMEM;
271 	pipe->index = -1;		/* Not configured yet */
272 
273 	/* Set up hw capabilities and contraints */
274 	memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
275 	dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
276 	pipe->constr.list = channels_list;
277 	pipe->constr.mask = 0;
278 	for (i = 0; channels_list[i] <= max_channels; i++);
279 	pipe->constr.count = i;
280 	if (pipe->hw.channels_max > max_channels)
281 		pipe->hw.channels_max = max_channels;
282 	if (chip->digital_mode == DIGITAL_MODE_ADAT) {
283 		pipe->hw.rate_max = 48000;
284 		pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
285 	}
286 
287 	runtime->hw = pipe->hw;
288 	runtime->private_data = pipe;
289 	runtime->private_free = audiopipe_free;
290 	snd_pcm_set_sync(substream);
291 
292 	/* Only mono and any even number of channels are allowed */
293 	err = snd_pcm_hw_constraint_list(runtime, 0,
294 					 SNDRV_PCM_HW_PARAM_CHANNELS,
295 					 &pipe->constr);
296 	if (err < 0)
297 		return err;
298 
299 	/* All periods should have the same size */
300 	err = snd_pcm_hw_constraint_integer(runtime,
301 					    SNDRV_PCM_HW_PARAM_PERIODS);
302 	if (err < 0)
303 		return err;
304 
305 	/* The hw accesses memory in chunks 32 frames long and they should be
306 	32-bytes-aligned. It's not a requirement, but it seems that IRQs are
307 	generated with a resolution of 32 frames. Thus we need the following */
308 	err = snd_pcm_hw_constraint_step(runtime, 0,
309 					 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
310 	if (err < 0)
311 		return err;
312 	err = snd_pcm_hw_constraint_step(runtime, 0,
313 					 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
314 	if (err < 0)
315 		return err;
316 
317 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
318 				  SNDRV_PCM_HW_PARAM_RATE,
319 				  hw_rule_sample_rate, chip,
320 				  SNDRV_PCM_HW_PARAM_RATE, -1);
321 	if (err < 0)
322 		return err;
323 
324 	/* Allocate a page for the scatter-gather list */
325 	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
326 				  &chip->pci->dev,
327 				  PAGE_SIZE, &pipe->sgpage);
328 	if (err < 0) {
329 		dev_err(chip->card->dev, "s-g list allocation failed\n");
330 		return err;
331 	}
332 
333 	/*
334 	 * Sole ownership required to set the rate
335 	 */
336 
337 	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
338 		chip->opencount, chip->can_set_rate, chip->rate_set);
339 
340 	chip->opencount++;
341 	if (chip->opencount > 1 && chip->rate_set)
342 		chip->can_set_rate = 0;
343 
344 	return 0;
345 }
346 
347 
348 
349 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
350 {
351 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
352 	int err;
353 
354 	err = pcm_open(substream,
355 		       num_analog_busses_in(chip) - substream->number);
356 	if (err < 0)
357 		return err;
358 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
359 				  SNDRV_PCM_HW_PARAM_CHANNELS,
360 				  hw_rule_capture_channels_by_format, NULL,
361 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
362 	if (err < 0)
363 		return err;
364 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
365 				  SNDRV_PCM_HW_PARAM_FORMAT,
366 				  hw_rule_capture_format_by_channels, NULL,
367 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
368 	if (err < 0)
369 		return err;
370 
371 	return 0;
372 }
373 
374 
375 
376 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
377 {
378 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
379 	int max_channels, err;
380 
381 #ifdef ECHOCARD_HAS_VMIXER
382 	max_channels = num_pipes_out(chip);
383 #else
384 	max_channels = num_analog_busses_out(chip);
385 #endif
386 	err = pcm_open(substream, max_channels - substream->number);
387 	if (err < 0)
388 		return err;
389 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
390 				  SNDRV_PCM_HW_PARAM_CHANNELS,
391 				  hw_rule_playback_channels_by_format,
392 				  NULL,
393 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
394 	if (err < 0)
395 		return err;
396 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
397 				  SNDRV_PCM_HW_PARAM_FORMAT,
398 				  hw_rule_playback_format_by_channels,
399 				  NULL,
400 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
401 	if (err < 0)
402 		return err;
403 
404 	return 0;
405 }
406 
407 
408 
409 #ifdef ECHOCARD_HAS_DIGITAL_IO
410 
411 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
412 {
413 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
414 	int err, max_channels;
415 
416 	max_channels = num_digital_busses_in(chip) - substream->number;
417 	mutex_lock(&chip->mode_mutex);
418 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
419 		err = pcm_open(substream, max_channels);
420 	else	/* If the card has ADAT, subtract the 6 channels
421 		 * that S/PDIF doesn't have
422 		 */
423 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
424 
425 	if (err < 0)
426 		goto din_exit;
427 
428 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
429 				  SNDRV_PCM_HW_PARAM_CHANNELS,
430 				  hw_rule_capture_channels_by_format, NULL,
431 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
432 	if (err < 0)
433 		goto din_exit;
434 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
435 				  SNDRV_PCM_HW_PARAM_FORMAT,
436 				  hw_rule_capture_format_by_channels, NULL,
437 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
438 	if (err < 0)
439 		goto din_exit;
440 
441 din_exit:
442 	mutex_unlock(&chip->mode_mutex);
443 	return err;
444 }
445 
446 
447 
448 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
449 
450 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
451 {
452 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
453 	int err, max_channels;
454 
455 	max_channels = num_digital_busses_out(chip) - substream->number;
456 	mutex_lock(&chip->mode_mutex);
457 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
458 		err = pcm_open(substream, max_channels);
459 	else	/* If the card has ADAT, subtract the 6 channels
460 		 * that S/PDIF doesn't have
461 		 */
462 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
463 
464 	if (err < 0)
465 		goto dout_exit;
466 
467 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
468 				  SNDRV_PCM_HW_PARAM_CHANNELS,
469 				  hw_rule_playback_channels_by_format,
470 				  NULL, SNDRV_PCM_HW_PARAM_FORMAT,
471 				  -1);
472 	if (err < 0)
473 		goto dout_exit;
474 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
475 				  SNDRV_PCM_HW_PARAM_FORMAT,
476 				  hw_rule_playback_format_by_channels,
477 				  NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
478 				  -1);
479 	if (err < 0)
480 		goto dout_exit;
481 
482 dout_exit:
483 	mutex_unlock(&chip->mode_mutex);
484 	return err;
485 }
486 
487 #endif /* !ECHOCARD_HAS_VMIXER */
488 
489 #endif /* ECHOCARD_HAS_DIGITAL_IO */
490 
491 
492 
493 static int pcm_close(struct snd_pcm_substream *substream)
494 {
495 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
496 
497 	/* Nothing to do here. Audio is already off and pipe will be
498 	 * freed by its callback
499 	 */
500 
501 	mutex_lock(&chip->mode_mutex);
502 
503 	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
504 		chip->opencount, chip->can_set_rate, chip->rate_set);
505 
506 	chip->opencount--;
507 
508 	switch (chip->opencount) {
509 	case 1:
510 		chip->can_set_rate = 1;
511 		break;
512 
513 	case 0:
514 		chip->rate_set = 0;
515 		break;
516 	}
517 
518 	mutex_unlock(&chip->mode_mutex);
519 	return 0;
520 }
521 
522 
523 
524 /* Channel allocation and scatter-gather list setup */
525 static int init_engine(struct snd_pcm_substream *substream,
526 		       struct snd_pcm_hw_params *hw_params,
527 		       int pipe_index, int interleave)
528 {
529 	struct echoaudio *chip;
530 	int err, per, rest, page, edge, offs;
531 	struct audiopipe *pipe;
532 
533 	chip = snd_pcm_substream_chip(substream);
534 	pipe = (struct audiopipe *) substream->runtime->private_data;
535 
536 	/* Sets up che hardware. If it's already initialized, reset and
537 	 * redo with the new parameters
538 	 */
539 	spin_lock_irq(&chip->lock);
540 	if (pipe->index >= 0) {
541 		dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
542 		err = free_pipes(chip, pipe);
543 		snd_BUG_ON(err);
544 		chip->substream[pipe->index] = NULL;
545 	}
546 
547 	err = allocate_pipes(chip, pipe, pipe_index, interleave);
548 	if (err < 0) {
549 		spin_unlock_irq(&chip->lock);
550 		dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
551 			pipe_index, err);
552 		return err;
553 	}
554 	spin_unlock_irq(&chip->lock);
555 	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
556 
557 	dev_dbg(chip->card->dev,
558 		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
559 		params_buffer_bytes(hw_params), params_periods(hw_params),
560 		params_period_bytes(hw_params));
561 
562 	sglist_init(chip, pipe);
563 	edge = PAGE_SIZE;
564 	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
565 	     per++) {
566 		rest = params_period_bytes(hw_params);
567 		if (offs + rest > params_buffer_bytes(hw_params))
568 			rest = params_buffer_bytes(hw_params) - offs;
569 		while (rest) {
570 			dma_addr_t addr;
571 			addr = snd_pcm_sgbuf_get_addr(substream, offs);
572 			if (rest <= edge - offs) {
573 				sglist_add_mapping(chip, pipe, addr, rest);
574 				sglist_add_irq(chip, pipe);
575 				offs += rest;
576 				rest = 0;
577 			} else {
578 				sglist_add_mapping(chip, pipe, addr,
579 						   edge - offs);
580 				rest -= edge - offs;
581 				offs = edge;
582 			}
583 			if (offs == edge) {
584 				edge += PAGE_SIZE;
585 				page++;
586 			}
587 		}
588 	}
589 
590 	/* Close the ring buffer */
591 	sglist_wrap(chip, pipe);
592 
593 	/* This stuff is used by the irq handler, so it must be
594 	 * initialized before chip->substream
595 	 */
596 	pipe->last_period = 0;
597 	pipe->last_counter = 0;
598 	pipe->position = 0;
599 	smp_wmb();
600 	chip->substream[pipe_index] = substream;
601 	chip->rate_set = 1;
602 	spin_lock_irq(&chip->lock);
603 	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
604 	spin_unlock_irq(&chip->lock);
605 	return 0;
606 }
607 
608 
609 
610 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
611 				   struct snd_pcm_hw_params *hw_params)
612 {
613 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
614 
615 	return init_engine(substream, hw_params, px_analog_in(chip) +
616 			substream->number, params_channels(hw_params));
617 }
618 
619 
620 
621 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
622 				    struct snd_pcm_hw_params *hw_params)
623 {
624 	return init_engine(substream, hw_params, substream->number,
625 			   params_channels(hw_params));
626 }
627 
628 
629 
630 #ifdef ECHOCARD_HAS_DIGITAL_IO
631 
632 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
633 				    struct snd_pcm_hw_params *hw_params)
634 {
635 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
636 
637 	return init_engine(substream, hw_params, px_digital_in(chip) +
638 			substream->number, params_channels(hw_params));
639 }
640 
641 
642 
643 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
644 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
645 				     struct snd_pcm_hw_params *hw_params)
646 {
647 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
648 
649 	return init_engine(substream, hw_params, px_digital_out(chip) +
650 			substream->number, params_channels(hw_params));
651 }
652 #endif /* !ECHOCARD_HAS_VMIXER */
653 
654 #endif /* ECHOCARD_HAS_DIGITAL_IO */
655 
656 
657 
658 static int pcm_hw_free(struct snd_pcm_substream *substream)
659 {
660 	struct echoaudio *chip;
661 	struct audiopipe *pipe;
662 
663 	chip = snd_pcm_substream_chip(substream);
664 	pipe = (struct audiopipe *) substream->runtime->private_data;
665 
666 	spin_lock_irq(&chip->lock);
667 	if (pipe->index >= 0) {
668 		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
669 		free_pipes(chip, pipe);
670 		chip->substream[pipe->index] = NULL;
671 		pipe->index = -1;
672 	}
673 	spin_unlock_irq(&chip->lock);
674 
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 		fallthrough;
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 
718 	/*
719 	 * We passed checks we can do independently; now take
720 	 * exclusive control
721 	 */
722 
723 	spin_lock_irq(&chip->lock);
724 
725 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
726 		spin_unlock_irq(&chip->lock);
727 		return -EINVAL;
728 	}
729 
730 	set_audio_format(chip, pipe_index, &format);
731 	spin_unlock_irq(&chip->lock);
732 
733 	return 0;
734 }
735 
736 
737 
738 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
739 {
740 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
741 	struct audiopipe *pipe;
742 	int i, err;
743 	u32 channelmask = 0;
744 	struct snd_pcm_substream *s;
745 
746 	snd_pcm_group_for_each_entry(s, substream) {
747 		for (i = 0; i < DSP_MAXPIPES; i++) {
748 			if (s == chip->substream[i]) {
749 				channelmask |= 1 << i;
750 				snd_pcm_trigger_done(s, substream);
751 			}
752 		}
753 	}
754 
755 	spin_lock(&chip->lock);
756 	switch (cmd) {
757 	case SNDRV_PCM_TRIGGER_RESUME:
758 	case SNDRV_PCM_TRIGGER_START:
759 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
760 		for (i = 0; i < DSP_MAXPIPES; i++) {
761 			if (channelmask & (1 << i)) {
762 				pipe = chip->substream[i]->runtime->private_data;
763 				switch (pipe->state) {
764 				case PIPE_STATE_STOPPED:
765 					pipe->last_period = 0;
766 					pipe->last_counter = 0;
767 					pipe->position = 0;
768 					*pipe->dma_counter = 0;
769 					fallthrough;
770 				case PIPE_STATE_PAUSED:
771 					pipe->state = PIPE_STATE_STARTED;
772 					break;
773 				case PIPE_STATE_STARTED:
774 					break;
775 				}
776 			}
777 		}
778 		err = start_transport(chip, channelmask,
779 				      chip->pipe_cyclic_mask);
780 		break;
781 	case SNDRV_PCM_TRIGGER_SUSPEND:
782 	case SNDRV_PCM_TRIGGER_STOP:
783 		for (i = 0; i < DSP_MAXPIPES; i++) {
784 			if (channelmask & (1 << i)) {
785 				pipe = chip->substream[i]->runtime->private_data;
786 				pipe->state = PIPE_STATE_STOPPED;
787 			}
788 		}
789 		err = stop_transport(chip, channelmask);
790 		break;
791 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
792 		for (i = 0; i < DSP_MAXPIPES; i++) {
793 			if (channelmask & (1 << i)) {
794 				pipe = chip->substream[i]->runtime->private_data;
795 				pipe->state = PIPE_STATE_PAUSED;
796 			}
797 		}
798 		err = pause_transport(chip, channelmask);
799 		break;
800 	default:
801 		err = -EINVAL;
802 	}
803 	spin_unlock(&chip->lock);
804 	return err;
805 }
806 
807 
808 
809 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
810 {
811 	struct snd_pcm_runtime *runtime = substream->runtime;
812 	struct audiopipe *pipe = runtime->private_data;
813 	u32 counter, step;
814 
815 	/*
816 	 * IRQ handling runs concurrently. Do not share tracking of
817 	 * counter with it, which would race or require locking
818 	 */
819 
820 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
821 
822 	step = counter - pipe->last_counter;  /* handles wrapping */
823 	pipe->last_counter = counter;
824 
825 	/* counter doesn't neccessarily wrap on a multiple of
826 	 * buffer_size, so can't derive the position; must
827 	 * accumulate */
828 
829 	pipe->position += step;
830 	pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
831 
832 	return bytes_to_frames(runtime, pipe->position);
833 }
834 
835 
836 
837 /* pcm *_ops structures */
838 static const struct snd_pcm_ops analog_playback_ops = {
839 	.open = pcm_analog_out_open,
840 	.close = pcm_close,
841 	.hw_params = pcm_analog_out_hw_params,
842 	.hw_free = pcm_hw_free,
843 	.prepare = pcm_prepare,
844 	.trigger = pcm_trigger,
845 	.pointer = pcm_pointer,
846 };
847 static const struct snd_pcm_ops analog_capture_ops = {
848 	.open = pcm_analog_in_open,
849 	.close = pcm_close,
850 	.hw_params = pcm_analog_in_hw_params,
851 	.hw_free = pcm_hw_free,
852 	.prepare = pcm_prepare,
853 	.trigger = pcm_trigger,
854 	.pointer = pcm_pointer,
855 };
856 #ifdef ECHOCARD_HAS_DIGITAL_IO
857 #ifndef ECHOCARD_HAS_VMIXER
858 static const struct snd_pcm_ops digital_playback_ops = {
859 	.open = pcm_digital_out_open,
860 	.close = pcm_close,
861 	.hw_params = pcm_digital_out_hw_params,
862 	.hw_free = pcm_hw_free,
863 	.prepare = pcm_prepare,
864 	.trigger = pcm_trigger,
865 	.pointer = pcm_pointer,
866 };
867 #endif /* !ECHOCARD_HAS_VMIXER */
868 static const struct snd_pcm_ops digital_capture_ops = {
869 	.open = pcm_digital_in_open,
870 	.close = pcm_close,
871 	.hw_params = pcm_digital_in_hw_params,
872 	.hw_free = pcm_hw_free,
873 	.prepare = pcm_prepare,
874 	.trigger = pcm_trigger,
875 	.pointer = pcm_pointer,
876 };
877 #endif /* ECHOCARD_HAS_DIGITAL_IO */
878 
879 
880 
881 /* Preallocate memory only for the first substream because it's the most
882  * used one
883  */
884 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
885 {
886 	struct snd_pcm_substream *ss;
887 	int stream;
888 
889 	for (stream = 0; stream < 2; stream++)
890 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
891 			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
892 						   dev,
893 						   ss->number ? 0 : 128<<10,
894 						   256<<10);
895 }
896 
897 
898 
899 /*<--snd_echo_probe() */
900 static int snd_echo_new_pcm(struct echoaudio *chip)
901 {
902 	struct snd_pcm *pcm;
903 	int err;
904 
905 #ifdef ECHOCARD_HAS_VMIXER
906 	/* This card has a Vmixer, that is there is no direct mapping from PCM
907 	streams to physical outputs. The user can mix the streams as he wishes
908 	via control interface and it's possible to send any stream to any
909 	output, thus it makes no sense to keep analog and digital outputs
910 	separated */
911 
912 	/* PCM#0 Virtual outputs and analog inputs */
913 	err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
914 			  num_analog_busses_in(chip), &pcm);
915 	if (err < 0)
916 		return err;
917 	pcm->private_data = chip;
918 	chip->analog_pcm = pcm;
919 	strcpy(pcm->name, chip->card->shortname);
920 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
921 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
922 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
923 
924 #ifdef ECHOCARD_HAS_DIGITAL_IO
925 	/* PCM#1 Digital inputs, no outputs */
926 	err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
927 			  num_digital_busses_in(chip), &pcm);
928 	if (err < 0)
929 		return err;
930 	pcm->private_data = chip;
931 	chip->digital_pcm = pcm;
932 	strcpy(pcm->name, chip->card->shortname);
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 #else /* ECHOCARD_HAS_VMIXER */
938 
939 	/* The card can manage substreams formed by analog and digital channels
940 	at the same time, but I prefer to keep analog and digital channels
941 	separated, because that mixed thing is confusing and useless. So we
942 	register two PCM devices: */
943 
944 	/* PCM#0 Analog i/o */
945 	err = snd_pcm_new(chip->card, "Analog PCM", 0,
946 			  num_analog_busses_out(chip),
947 			  num_analog_busses_in(chip), &pcm);
948 	if (err < 0)
949 		return err;
950 	pcm->private_data = chip;
951 	chip->analog_pcm = pcm;
952 	strcpy(pcm->name, chip->card->shortname);
953 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
954 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
955 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
956 
957 #ifdef ECHOCARD_HAS_DIGITAL_IO
958 	/* PCM#1 Digital i/o */
959 	err = snd_pcm_new(chip->card, "Digital PCM", 1,
960 			  num_digital_busses_out(chip),
961 			  num_digital_busses_in(chip), &pcm);
962 	if (err < 0)
963 		return err;
964 	pcm->private_data = chip;
965 	chip->digital_pcm = pcm;
966 	strcpy(pcm->name, chip->card->shortname);
967 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
968 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
969 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
970 #endif /* ECHOCARD_HAS_DIGITAL_IO */
971 
972 #endif /* ECHOCARD_HAS_VMIXER */
973 
974 	return 0;
975 }
976 
977 
978 
979 
980 /******************************************************************************
981 	Control interface
982 ******************************************************************************/
983 
984 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
985 
986 /******************* PCM output volume *******************/
987 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
988 				     struct snd_ctl_elem_info *uinfo)
989 {
990 	struct echoaudio *chip;
991 
992 	chip = snd_kcontrol_chip(kcontrol);
993 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
994 	uinfo->count = num_busses_out(chip);
995 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
996 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
997 	return 0;
998 }
999 
1000 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1001 				    struct snd_ctl_elem_value *ucontrol)
1002 {
1003 	struct echoaudio *chip;
1004 	int c;
1005 
1006 	chip = snd_kcontrol_chip(kcontrol);
1007 	for (c = 0; c < num_busses_out(chip); c++)
1008 		ucontrol->value.integer.value[c] = chip->output_gain[c];
1009 	return 0;
1010 }
1011 
1012 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1013 				    struct snd_ctl_elem_value *ucontrol)
1014 {
1015 	struct echoaudio *chip;
1016 	int c, changed, gain;
1017 
1018 	changed = 0;
1019 	chip = snd_kcontrol_chip(kcontrol);
1020 	spin_lock_irq(&chip->lock);
1021 	for (c = 0; c < num_busses_out(chip); c++) {
1022 		gain = ucontrol->value.integer.value[c];
1023 		/* Ignore out of range values */
1024 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1025 			continue;
1026 		if (chip->output_gain[c] != gain) {
1027 			set_output_gain(chip, c, gain);
1028 			changed = 1;
1029 		}
1030 	}
1031 	if (changed)
1032 		update_output_line_level(chip);
1033 	spin_unlock_irq(&chip->lock);
1034 	return changed;
1035 }
1036 
1037 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1038 /* On the Mia this one controls the line-out volume */
1039 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1040 	.name = "Line Playback Volume",
1041 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1042 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1043 		  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 #else
1050 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1051 	.name = "PCM Playback Volume",
1052 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1053 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1054 	.info = snd_echo_output_gain_info,
1055 	.get = snd_echo_output_gain_get,
1056 	.put = snd_echo_output_gain_put,
1057 	.tlv = {.p = db_scale_output_gain},
1058 };
1059 #endif
1060 
1061 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1062 
1063 
1064 
1065 #ifdef ECHOCARD_HAS_INPUT_GAIN
1066 
1067 /******************* Analog input volume *******************/
1068 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1069 				    struct snd_ctl_elem_info *uinfo)
1070 {
1071 	struct echoaudio *chip;
1072 
1073 	chip = snd_kcontrol_chip(kcontrol);
1074 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1075 	uinfo->count = num_analog_busses_in(chip);
1076 	uinfo->value.integer.min = ECHOGAIN_MININP;
1077 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1078 	return 0;
1079 }
1080 
1081 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1082 				   struct snd_ctl_elem_value *ucontrol)
1083 {
1084 	struct echoaudio *chip;
1085 	int c;
1086 
1087 	chip = snd_kcontrol_chip(kcontrol);
1088 	for (c = 0; c < num_analog_busses_in(chip); c++)
1089 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1090 	return 0;
1091 }
1092 
1093 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1094 				   struct snd_ctl_elem_value *ucontrol)
1095 {
1096 	struct echoaudio *chip;
1097 	int c, gain, changed;
1098 
1099 	changed = 0;
1100 	chip = snd_kcontrol_chip(kcontrol);
1101 	spin_lock_irq(&chip->lock);
1102 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1103 		gain = ucontrol->value.integer.value[c];
1104 		/* Ignore out of range values */
1105 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1106 			continue;
1107 		if (chip->input_gain[c] != gain) {
1108 			set_input_gain(chip, c, gain);
1109 			changed = 1;
1110 		}
1111 	}
1112 	if (changed)
1113 		update_input_line_level(chip);
1114 	spin_unlock_irq(&chip->lock);
1115 	return changed;
1116 }
1117 
1118 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1119 
1120 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1121 	.name = "Line Capture Volume",
1122 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1123 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1124 	.info = snd_echo_input_gain_info,
1125 	.get = snd_echo_input_gain_get,
1126 	.put = snd_echo_input_gain_put,
1127 	.tlv = {.p = db_scale_input_gain},
1128 };
1129 
1130 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1131 
1132 
1133 
1134 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1135 
1136 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1137 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1138 					 struct snd_ctl_elem_info *uinfo)
1139 {
1140 	struct echoaudio *chip;
1141 
1142 	chip = snd_kcontrol_chip(kcontrol);
1143 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1144 	uinfo->count = num_analog_busses_out(chip);
1145 	uinfo->value.integer.min = 0;
1146 	uinfo->value.integer.max = 1;
1147 	return 0;
1148 }
1149 
1150 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1151 				       struct snd_ctl_elem_value *ucontrol)
1152 {
1153 	struct echoaudio *chip;
1154 	int c;
1155 
1156 	chip = snd_kcontrol_chip(kcontrol);
1157 	for (c = 0; c < num_analog_busses_out(chip); c++)
1158 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1159 	return 0;
1160 }
1161 
1162 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1163 				       struct snd_ctl_elem_value *ucontrol)
1164 {
1165 	struct echoaudio *chip;
1166 	int c, changed;
1167 
1168 	changed = 0;
1169 	chip = snd_kcontrol_chip(kcontrol);
1170 	spin_lock_irq(&chip->lock);
1171 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1172 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1173 			set_nominal_level(chip, c,
1174 					  ucontrol->value.integer.value[c]);
1175 			changed = 1;
1176 		}
1177 	}
1178 	if (changed)
1179 		update_output_line_level(chip);
1180 	spin_unlock_irq(&chip->lock);
1181 	return changed;
1182 }
1183 
1184 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1185 	.name = "Line Playback Switch (-10dBV)",
1186 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1187 	.info = snd_echo_output_nominal_info,
1188 	.get = snd_echo_output_nominal_get,
1189 	.put = snd_echo_output_nominal_put,
1190 };
1191 
1192 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1193 
1194 
1195 
1196 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1197 
1198 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1199 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1200 				       struct snd_ctl_elem_info *uinfo)
1201 {
1202 	struct echoaudio *chip;
1203 
1204 	chip = snd_kcontrol_chip(kcontrol);
1205 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1206 	uinfo->count = num_analog_busses_in(chip);
1207 	uinfo->value.integer.min = 0;
1208 	uinfo->value.integer.max = 1;
1209 	return 0;
1210 }
1211 
1212 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1213 				      struct snd_ctl_elem_value *ucontrol)
1214 {
1215 	struct echoaudio *chip;
1216 	int c;
1217 
1218 	chip = snd_kcontrol_chip(kcontrol);
1219 	for (c = 0; c < num_analog_busses_in(chip); c++)
1220 		ucontrol->value.integer.value[c] =
1221 			chip->nominal_level[bx_analog_in(chip) + c];
1222 	return 0;
1223 }
1224 
1225 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1226 				      struct snd_ctl_elem_value *ucontrol)
1227 {
1228 	struct echoaudio *chip;
1229 	int c, changed;
1230 
1231 	changed = 0;
1232 	chip = snd_kcontrol_chip(kcontrol);
1233 	spin_lock_irq(&chip->lock);
1234 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1235 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1236 		    ucontrol->value.integer.value[c]) {
1237 			set_nominal_level(chip, bx_analog_in(chip) + c,
1238 					  ucontrol->value.integer.value[c]);
1239 			changed = 1;
1240 		}
1241 	}
1242 	if (changed)
1243 		update_output_line_level(chip);	/* "Output" is not a mistake
1244 						 * here.
1245 						 */
1246 	spin_unlock_irq(&chip->lock);
1247 	return changed;
1248 }
1249 
1250 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1251 	.name = "Line Capture Switch (-10dBV)",
1252 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1253 	.info = snd_echo_input_nominal_info,
1254 	.get = snd_echo_input_nominal_get,
1255 	.put = snd_echo_input_nominal_put,
1256 };
1257 
1258 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1259 
1260 
1261 
1262 #ifdef ECHOCARD_HAS_MONITOR
1263 
1264 /******************* Monitor mixer *******************/
1265 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1266 			       struct snd_ctl_elem_info *uinfo)
1267 {
1268 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1269 	uinfo->count = 1;
1270 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1271 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1272 	return 0;
1273 }
1274 
1275 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1276 			      struct snd_ctl_elem_value *ucontrol)
1277 {
1278 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1279 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1280 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1281 
1282 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1283 		return -EINVAL;
1284 
1285 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1286 	return 0;
1287 }
1288 
1289 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1290 			      struct snd_ctl_elem_value *ucontrol)
1291 {
1292 	struct echoaudio *chip;
1293 	int changed,  gain;
1294 	unsigned int out, in;
1295 
1296 	changed = 0;
1297 	chip = snd_kcontrol_chip(kcontrol);
1298 	out = ucontrol->id.index / num_busses_in(chip);
1299 	in = ucontrol->id.index % num_busses_in(chip);
1300 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1301 		return -EINVAL;
1302 	gain = ucontrol->value.integer.value[0];
1303 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1304 		return -EINVAL;
1305 	if (chip->monitor_gain[out][in] != gain) {
1306 		spin_lock_irq(&chip->lock);
1307 		set_monitor_gain(chip, out, in, gain);
1308 		update_output_line_level(chip);
1309 		spin_unlock_irq(&chip->lock);
1310 		changed = 1;
1311 	}
1312 	return changed;
1313 }
1314 
1315 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1316 	.name = "Monitor Mixer Volume",
1317 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1318 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1319 	.info = snd_echo_mixer_info,
1320 	.get = snd_echo_mixer_get,
1321 	.put = snd_echo_mixer_put,
1322 	.tlv = {.p = db_scale_output_gain},
1323 };
1324 
1325 #endif /* ECHOCARD_HAS_MONITOR */
1326 
1327 
1328 
1329 #ifdef ECHOCARD_HAS_VMIXER
1330 
1331 /******************* Vmixer *******************/
1332 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1333 				struct snd_ctl_elem_info *uinfo)
1334 {
1335 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1336 	uinfo->count = 1;
1337 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1338 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
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 (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 		changed = set_input_clock(chip, dclock);
1578 		if (!changed)
1579 			changed = 1;	/* no errors */
1580 		spin_unlock_irq(&chip->lock);
1581 		mutex_unlock(&chip->mode_mutex);
1582 	}
1583 
1584 	if (changed < 0)
1585 		dev_dbg(chip->card->dev,
1586 			"seticlk val%d err 0x%x\n", dclock, changed);
1587 
1588 	return changed;
1589 }
1590 
1591 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1592 	.name = "Sample Clock Source",
1593 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1594 	.info = snd_echo_clock_source_info,
1595 	.get = snd_echo_clock_source_get,
1596 	.put = snd_echo_clock_source_put,
1597 };
1598 
1599 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1600 
1601 
1602 
1603 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1604 
1605 /******************* Phantom power switch *******************/
1606 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1607 
1608 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1609 				      struct snd_ctl_elem_value *ucontrol)
1610 {
1611 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1612 
1613 	ucontrol->value.integer.value[0] = chip->phantom_power;
1614 	return 0;
1615 }
1616 
1617 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1618 				      struct snd_ctl_elem_value *ucontrol)
1619 {
1620 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1621 	int power, changed = 0;
1622 
1623 	power = !!ucontrol->value.integer.value[0];
1624 	if (chip->phantom_power != power) {
1625 		spin_lock_irq(&chip->lock);
1626 		changed = set_phantom_power(chip, power);
1627 		spin_unlock_irq(&chip->lock);
1628 		if (changed == 0)
1629 			changed = 1;	/* no errors */
1630 	}
1631 	return changed;
1632 }
1633 
1634 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1635 	.name = "Phantom power Switch",
1636 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1637 	.info = snd_echo_phantom_power_info,
1638 	.get = snd_echo_phantom_power_get,
1639 	.put = snd_echo_phantom_power_put,
1640 };
1641 
1642 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1643 
1644 
1645 
1646 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1647 
1648 /******************* Digital input automute switch *******************/
1649 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1650 
1651 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1652 				 struct snd_ctl_elem_value *ucontrol)
1653 {
1654 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1655 
1656 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1657 	return 0;
1658 }
1659 
1660 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1661 				 struct snd_ctl_elem_value *ucontrol)
1662 {
1663 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1664 	int automute, changed = 0;
1665 
1666 	automute = !!ucontrol->value.integer.value[0];
1667 	if (chip->digital_in_automute != automute) {
1668 		spin_lock_irq(&chip->lock);
1669 		changed = set_input_auto_mute(chip, automute);
1670 		spin_unlock_irq(&chip->lock);
1671 		if (changed == 0)
1672 			changed = 1;	/* no errors */
1673 	}
1674 	return changed;
1675 }
1676 
1677 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1678 	.name = "Digital Capture Switch (automute)",
1679 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1680 	.info = snd_echo_automute_info,
1681 	.get = snd_echo_automute_get,
1682 	.put = snd_echo_automute_put,
1683 };
1684 
1685 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1686 
1687 
1688 
1689 /******************* VU-meters switch *******************/
1690 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1691 
1692 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1693 					struct snd_ctl_elem_value *ucontrol)
1694 {
1695 	struct echoaudio *chip;
1696 
1697 	chip = snd_kcontrol_chip(kcontrol);
1698 	spin_lock_irq(&chip->lock);
1699 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1700 	spin_unlock_irq(&chip->lock);
1701 	return 1;
1702 }
1703 
1704 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1705 	.name = "VU-meters Switch",
1706 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1707 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1708 	.info = snd_echo_vumeters_switch_info,
1709 	.put = snd_echo_vumeters_switch_put,
1710 };
1711 
1712 
1713 
1714 /***** Read VU-meters (input, output, analog and digital together) *****/
1715 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1716 				  struct snd_ctl_elem_info *uinfo)
1717 {
1718 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1719 	uinfo->count = 96;
1720 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1721 	uinfo->value.integer.max = 0;
1722 	return 0;
1723 }
1724 
1725 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1726 				 struct snd_ctl_elem_value *ucontrol)
1727 {
1728 	struct echoaudio *chip;
1729 
1730 	chip = snd_kcontrol_chip(kcontrol);
1731 	get_audio_meters(chip, ucontrol->value.integer.value);
1732 	return 0;
1733 }
1734 
1735 static const struct snd_kcontrol_new snd_echo_vumeters = {
1736 	.name = "VU-meters",
1737 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1738 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1739 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1740 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1741 	.info = snd_echo_vumeters_info,
1742 	.get = snd_echo_vumeters_get,
1743 	.tlv = {.p = db_scale_output_gain},
1744 };
1745 
1746 
1747 
1748 /*** Channels info - it exports informations about the number of channels ***/
1749 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1750 				       struct snd_ctl_elem_info *uinfo)
1751 {
1752 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1753 	uinfo->count = 6;
1754 	uinfo->value.integer.min = 0;
1755 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1756 	return 0;
1757 }
1758 
1759 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1760 				      struct snd_ctl_elem_value *ucontrol)
1761 {
1762 	struct echoaudio *chip;
1763 	int detected, clocks, bit, src;
1764 
1765 	chip = snd_kcontrol_chip(kcontrol);
1766 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1767 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1768 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1769 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1770 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1771 
1772 	/* Compute the bitmask of the currently valid input clocks */
1773 	detected = detect_input_clocks(chip);
1774 	clocks = 0;
1775 	src = chip->num_clock_sources - 1;
1776 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1777 		if (detected & (1 << bit))
1778 			for (; src >= 0; src--)
1779 				if (bit == chip->clock_source_list[src]) {
1780 					clocks |= 1 << src;
1781 					break;
1782 				}
1783 	ucontrol->value.integer.value[5] = clocks;
1784 
1785 	return 0;
1786 }
1787 
1788 static const struct snd_kcontrol_new snd_echo_channels_info = {
1789 	.name = "Channels info",
1790 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1791 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1792 	.info = snd_echo_channels_info_info,
1793 	.get = snd_echo_channels_info_get,
1794 };
1795 
1796 
1797 
1798 
1799 /******************************************************************************
1800 	IRQ Handling
1801 ******************************************************************************/
1802 /* Check if a period has elapsed since last interrupt
1803  *
1804  * Don't make any updates to state; PCM core handles this with the
1805  * correct locks.
1806  *
1807  * \return true if a period has elapsed, otherwise false
1808  */
1809 static bool period_has_elapsed(struct snd_pcm_substream *substream)
1810 {
1811 	struct snd_pcm_runtime *runtime = substream->runtime;
1812 	struct audiopipe *pipe = runtime->private_data;
1813 	u32 counter, step;
1814 	size_t period_bytes;
1815 
1816 	if (pipe->state != PIPE_STATE_STARTED)
1817 		return false;
1818 
1819 	period_bytes = frames_to_bytes(runtime, runtime->period_size);
1820 
1821 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1822 
1823 	step = counter - pipe->last_period;  /* handles wrapping */
1824 	step -= step % period_bytes;  /* acknowledge whole periods only */
1825 
1826 	if (step == 0)
1827 		return false;  /* haven't advanced a whole period yet */
1828 
1829 	pipe->last_period += step;  /* used exclusively by us */
1830 	return true;
1831 }
1832 
1833 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1834 {
1835 	struct echoaudio *chip = dev_id;
1836 	int ss, st;
1837 
1838 	spin_lock(&chip->lock);
1839 	st = service_irq(chip);
1840 	if (st < 0) {
1841 		spin_unlock(&chip->lock);
1842 		return IRQ_NONE;
1843 	}
1844 	/* The hardware doesn't tell us which substream caused the irq,
1845 	thus we have to check all running substreams. */
1846 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1847 		struct snd_pcm_substream *substream;
1848 
1849 		substream = chip->substream[ss];
1850 		if (substream && period_has_elapsed(substream)) {
1851 			spin_unlock(&chip->lock);
1852 			snd_pcm_period_elapsed(substream);
1853 			spin_lock(&chip->lock);
1854 		}
1855 	}
1856 	spin_unlock(&chip->lock);
1857 
1858 #ifdef ECHOCARD_HAS_MIDI
1859 	if (st > 0 && chip->midi_in) {
1860 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1861 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1862 	}
1863 #endif
1864 	return IRQ_HANDLED;
1865 }
1866 
1867 
1868 
1869 
1870 /******************************************************************************
1871 	Module construction / destruction
1872 ******************************************************************************/
1873 
1874 static void snd_echo_free(struct snd_card *card)
1875 {
1876 	struct echoaudio *chip = card->private_data;
1877 
1878 	if (chip->comm_page)
1879 		rest_in_peace(chip);
1880 
1881 	if (chip->irq >= 0)
1882 		free_irq(chip->irq, chip);
1883 
1884 	/* release chip data */
1885 	free_firmware_cache(chip);
1886 }
1887 
1888 /* <--snd_echo_probe() */
1889 static int snd_echo_create(struct snd_card *card,
1890 			   struct pci_dev *pci)
1891 {
1892 	struct echoaudio *chip = card->private_data;
1893 	int err;
1894 	size_t sz;
1895 
1896 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1897 
1898 	err = pcim_enable_device(pci);
1899 	if (err < 0)
1900 		return err;
1901 	pci_set_master(pci);
1902 
1903 	/* Allocate chip if needed */
1904 	spin_lock_init(&chip->lock);
1905 	chip->card = card;
1906 	chip->pci = pci;
1907 	chip->irq = -1;
1908 	chip->opencount = 0;
1909 	mutex_init(&chip->mode_mutex);
1910 	chip->can_set_rate = 1;
1911 
1912 	/* PCI resource allocation */
1913 	err = pci_request_regions(pci, ECHOCARD_NAME);
1914 	if (err < 0)
1915 		return err;
1916 
1917 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1918 	sz = pci_resource_len(pci, 0);
1919 	if (sz > PAGE_SIZE)
1920 		sz = PAGE_SIZE;		/* We map only the required part */
1921 
1922 	chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
1923 	if (!chip->dsp_registers) {
1924 		dev_err(chip->card->dev, "ioremap failed\n");
1925 		return -ENOMEM;
1926 	}
1927 
1928 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1929 			KBUILD_MODNAME, chip)) {
1930 		dev_err(chip->card->dev, "cannot grab irq\n");
1931 		return -EBUSY;
1932 	}
1933 	chip->irq = pci->irq;
1934 	card->sync_irq = chip->irq;
1935 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1936 		chip->pci, chip->irq, chip->pci->subsystem_device);
1937 
1938 	card->private_free = snd_echo_free;
1939 
1940 	/* Create the DSP comm page - this is the area of memory used for most
1941 	of the communication with the DSP, which accesses it via bus mastering */
1942 	chip->commpage_dma_buf =
1943 		snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
1944 				     sizeof(struct comm_page));
1945 	if (!chip->commpage_dma_buf)
1946 		return -ENOMEM;
1947 	chip->comm_page_phys = chip->commpage_dma_buf->addr;
1948 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1949 
1950 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1951 	if (err >= 0)
1952 		err = set_mixer_defaults(chip);
1953 	if (err < 0) {
1954 		dev_err(card->dev, "init_hw err=%d\n", err);
1955 		return err;
1956 	}
1957 
1958 	return 0;
1959 }
1960 
1961 /* constructor */
1962 static int __snd_echo_probe(struct pci_dev *pci,
1963 			    const struct pci_device_id *pci_id)
1964 {
1965 	static int dev;
1966 	struct snd_card *card;
1967 	struct echoaudio *chip;
1968 	char *dsp;
1969 	__maybe_unused int i;
1970 	int err;
1971 
1972 	if (dev >= SNDRV_CARDS)
1973 		return -ENODEV;
1974 	if (!enable[dev]) {
1975 		dev++;
1976 		return -ENOENT;
1977 	}
1978 
1979 	i = 0;
1980 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1981 				sizeof(*chip), &card);
1982 	if (err < 0)
1983 		return err;
1984 	chip = card->private_data;
1985 
1986 	err = snd_echo_create(card, pci);
1987 	if (err < 0)
1988 		return err;
1989 
1990 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
1991 	strcpy(card->shortname, chip->card_name);
1992 
1993 	dsp = "56301";
1994 	if (pci_id->device == 0x3410)
1995 		dsp = "56361";
1996 
1997 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
1998 		card->shortname, pci_id->subdevice & 0x000f, dsp,
1999 		chip->dsp_registers_phys, chip->irq);
2000 
2001 	err = snd_echo_new_pcm(chip);
2002 	if (err < 0) {
2003 		dev_err(chip->card->dev, "new pcm error %d\n", err);
2004 		return err;
2005 	}
2006 
2007 #ifdef ECHOCARD_HAS_MIDI
2008 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2009 		err = snd_echo_midi_create(card, chip);
2010 		if (err < 0) {
2011 			dev_err(chip->card->dev, "new midi error %d\n", err);
2012 			return err;
2013 		}
2014 	}
2015 #endif
2016 
2017 #ifdef ECHOCARD_HAS_VMIXER
2018 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2019 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
2020 	if (err < 0)
2021 		return err;
2022 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2023 	err = snd_ctl_add(chip->card,
2024 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2025 	if (err < 0)
2026 		return err;
2027 #endif
2028 #else /* ECHOCARD_HAS_VMIXER */
2029 	err = snd_ctl_add(chip->card,
2030 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2031 	if (err < 0)
2032 		return err;
2033 #endif /* ECHOCARD_HAS_VMIXER */
2034 
2035 #ifdef ECHOCARD_HAS_INPUT_GAIN
2036 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2037 	if (err < 0)
2038 		return err;
2039 #endif
2040 
2041 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2042 	if (!chip->hasnt_input_nominal_level) {
2043 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2044 		if (err < 0)
2045 			return err;
2046 	}
2047 #endif
2048 
2049 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2050 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2051 	if (err < 0)
2052 		return err;
2053 #endif
2054 
2055 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2056 	if (err < 0)
2057 		return err;
2058 
2059 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2060 	if (err < 0)
2061 		return err;
2062 
2063 #ifdef ECHOCARD_HAS_MONITOR
2064 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2065 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2066 	if (err < 0)
2067 		return err;
2068 #endif
2069 
2070 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2071 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2072 	if (err < 0)
2073 		return err;
2074 #endif
2075 
2076 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2077 	if (err < 0)
2078 		return err;
2079 
2080 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2081 	/* Creates a list of available digital modes */
2082 	chip->num_digital_modes = 0;
2083 	for (i = 0; i < 6; i++)
2084 		if (chip->digital_modes & (1 << i))
2085 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2086 
2087 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2088 	if (err < 0)
2089 		return err;
2090 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2091 
2092 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2093 	/* Creates a list of available clock sources */
2094 	chip->num_clock_sources = 0;
2095 	for (i = 0; i < 10; i++)
2096 		if (chip->input_clock_types & (1 << i))
2097 			chip->clock_source_list[chip->num_clock_sources++] = i;
2098 
2099 	if (chip->num_clock_sources > 1) {
2100 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2101 		err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2102 		if (err < 0)
2103 			return err;
2104 	}
2105 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2106 
2107 #ifdef ECHOCARD_HAS_DIGITAL_IO
2108 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2109 	if (err < 0)
2110 		return err;
2111 #endif
2112 
2113 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2114 	if (chip->has_phantom_power) {
2115 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2116 		if (err < 0)
2117 			return err;
2118 	}
2119 #endif
2120 
2121 	err = snd_card_register(card);
2122 	if (err < 0)
2123 		return err;
2124 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2125 
2126 	pci_set_drvdata(pci, chip);
2127 	dev++;
2128 	return 0;
2129 }
2130 
2131 static int snd_echo_probe(struct pci_dev *pci,
2132 			  const struct pci_device_id *pci_id)
2133 {
2134 	return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2135 }
2136 
2137 
2138 static int snd_echo_suspend(struct device *dev)
2139 {
2140 	struct echoaudio *chip = dev_get_drvdata(dev);
2141 
2142 #ifdef ECHOCARD_HAS_MIDI
2143 	/* This call can sleep */
2144 	if (chip->midi_out)
2145 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2146 #endif
2147 	spin_lock_irq(&chip->lock);
2148 	if (wait_handshake(chip)) {
2149 		spin_unlock_irq(&chip->lock);
2150 		return -EIO;
2151 	}
2152 	clear_handshake(chip);
2153 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2154 		spin_unlock_irq(&chip->lock);
2155 		return -EIO;
2156 	}
2157 	spin_unlock_irq(&chip->lock);
2158 
2159 	chip->dsp_code = NULL;
2160 	free_irq(chip->irq, chip);
2161 	chip->irq = -1;
2162 	chip->card->sync_irq = -1;
2163 	return 0;
2164 }
2165 
2166 
2167 
2168 static int snd_echo_resume(struct device *dev)
2169 {
2170 	struct pci_dev *pci = to_pci_dev(dev);
2171 	struct echoaudio *chip = dev_get_drvdata(dev);
2172 	struct comm_page *commpage, *commpage_bak;
2173 	u32 pipe_alloc_mask;
2174 	int err;
2175 
2176 	commpage = chip->comm_page;
2177 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2178 	if (commpage_bak == NULL)
2179 		return -ENOMEM;
2180 
2181 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2182 	if (err < 0) {
2183 		kfree(commpage_bak);
2184 		dev_err(dev, "resume init_hw err=%d\n", err);
2185 		return err;
2186 	}
2187 
2188 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2189 	 * restore_dsp_settings() fails.
2190 	 */
2191 	pipe_alloc_mask = chip->pipe_alloc_mask;
2192 	chip->pipe_alloc_mask = 0;
2193 	err = restore_dsp_rettings(chip);
2194 	chip->pipe_alloc_mask = pipe_alloc_mask;
2195 	if (err < 0) {
2196 		kfree(commpage_bak);
2197 		return err;
2198 	}
2199 
2200 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2201 		sizeof(commpage->audio_format));
2202 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2203 		sizeof(commpage->sglist_addr));
2204 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2205 		sizeof(commpage->midi_output));
2206 	kfree(commpage_bak);
2207 
2208 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2209 			KBUILD_MODNAME, chip)) {
2210 		dev_err(chip->card->dev, "cannot grab irq\n");
2211 		return -EBUSY;
2212 	}
2213 	chip->irq = pci->irq;
2214 	chip->card->sync_irq = chip->irq;
2215 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2216 
2217 #ifdef ECHOCARD_HAS_MIDI
2218 	if (chip->midi_input_enabled)
2219 		enable_midi_input(chip, true);
2220 	if (chip->midi_out)
2221 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2222 #endif
2223 
2224 	return 0;
2225 }
2226 
2227 static DEFINE_SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2228 
2229 /******************************************************************************
2230 	Everything starts and ends here
2231 ******************************************************************************/
2232 
2233 /* pci_driver definition */
2234 static struct pci_driver echo_driver = {
2235 	.name = KBUILD_MODNAME,
2236 	.id_table = snd_echo_ids,
2237 	.probe = snd_echo_probe,
2238 	.driver = {
2239 		.pm = &snd_echo_pm,
2240 	},
2241 };
2242 
2243 module_pci_driver(echo_driver);
2244