xref: /linux/sound/pci/echoaudio/echoaudio.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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 #include <linux/string.h>
10 
11 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
12 MODULE_LICENSE("GPL v2");
13 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
14 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
15 
16 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
17 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
18 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
19 
20 module_param_array(index, int, NULL, 0444);
21 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
22 module_param_array(id, charp, NULL, 0444);
23 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
24 module_param_array(enable, bool, NULL, 0444);
25 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
26 
27 static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
28 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
29 
30 
31 
32 static int get_firmware(const struct firmware **fw_entry,
33 			struct echoaudio *chip, const short fw_index)
34 {
35 	int err;
36 	char name[30];
37 
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 
46 	dev_dbg(chip->card->dev,
47 		"firmware requested: %s\n", card_fw[fw_index].data);
48 	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
49 	err = request_firmware(fw_entry, name, &chip->pci->dev);
50 	if (err < 0)
51 		dev_err(chip->card->dev,
52 			"get_firmware(): Firmware not available (%d)\n", err);
53 	else
54 		chip->fw_cache[fw_index] = *fw_entry;
55 	return err;
56 }
57 
58 
59 
60 static void free_firmware(const struct firmware *fw_entry,
61 			  struct echoaudio *chip)
62 {
63 	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
64 }
65 
66 
67 
68 static void free_firmware_cache(struct echoaudio *chip)
69 {
70 	int i;
71 
72 	for (i = 0; i < 8 ; i++)
73 		if (chip->fw_cache[i]) {
74 			release_firmware(chip->fw_cache[i]);
75 			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
76 		}
77 }
78 
79 
80 
81 /******************************************************************************
82 	PCM interface
83 ******************************************************************************/
84 
85 static void audiopipe_free(struct snd_pcm_runtime *runtime)
86 {
87 	struct audiopipe *pipe = runtime->private_data;
88 
89 	if (pipe->sgpage.area)
90 		snd_dma_free_pages(&pipe->sgpage);
91 	kfree(pipe);
92 }
93 
94 
95 
96 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
97 					      struct snd_pcm_hw_rule *rule)
98 {
99 	struct snd_interval *c = hw_param_interval(params,
100 						   SNDRV_PCM_HW_PARAM_CHANNELS);
101 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
102 	struct snd_mask fmt;
103 
104 	snd_mask_any(&fmt);
105 
106 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
107 	/* >=2 channels cannot be S32_BE */
108 	if (c->min == 2) {
109 		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
110 		return snd_mask_refine(f, &fmt);
111 	}
112 #endif
113 	/* > 2 channels cannot be U8 and S32_BE */
114 	if (c->min > 2) {
115 		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
116 		return snd_mask_refine(f, &fmt);
117 	}
118 	/* Mono is ok with any format */
119 	return 0;
120 }
121 
122 
123 
124 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
125 					      struct snd_pcm_hw_rule *rule)
126 {
127 	struct snd_interval *c = hw_param_interval(params,
128 						   SNDRV_PCM_HW_PARAM_CHANNELS);
129 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
130 	struct snd_interval ch;
131 
132 	snd_interval_any(&ch);
133 
134 	/* S32_BE is mono (and stereo) only */
135 	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
136 		ch.min = 1;
137 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
138 		ch.max = 2;
139 #else
140 		ch.max = 1;
141 #endif
142 		ch.integer = 1;
143 		return snd_interval_refine(c, &ch);
144 	}
145 	/* U8 can be only mono or stereo */
146 	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
147 		ch.min = 1;
148 		ch.max = 2;
149 		ch.integer = 1;
150 		return snd_interval_refine(c, &ch);
151 	}
152 	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
153 	return 0;
154 }
155 
156 
157 
158 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
159 					       struct snd_pcm_hw_rule *rule)
160 {
161 	struct snd_interval *c = hw_param_interval(params,
162 						   SNDRV_PCM_HW_PARAM_CHANNELS);
163 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
164 	struct snd_mask fmt;
165 	u64 fmask;
166 	snd_mask_any(&fmt);
167 
168 	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
169 
170 	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
171 	if (c->min > 2) {
172 		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
173 			 SNDRV_PCM_FMTBIT_S24_3LE |
174 			 SNDRV_PCM_FMTBIT_S32_LE;
175 	/* 1 channel must be S32_BE or S32_LE */
176 	} else if (c->max == 1)
177 		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
178 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
179 	/* 2 channels cannot be S32_BE */
180 	else if (c->min == 2 && c->max == 2)
181 		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
182 #endif
183 	else
184 		return 0;
185 
186 	fmt.bits[0] &= (u32)fmask;
187 	fmt.bits[1] &= (u32)(fmask >> 32);
188 	return snd_mask_refine(f, &fmt);
189 }
190 
191 
192 
193 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
194 					       struct snd_pcm_hw_rule *rule)
195 {
196 	struct snd_interval *c = hw_param_interval(params,
197 						   SNDRV_PCM_HW_PARAM_CHANNELS);
198 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
199 	struct snd_interval ch;
200 	u64 fmask;
201 
202 	snd_interval_any(&ch);
203 	ch.integer = 1;
204 	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
205 
206 	/* S32_BE is mono (and stereo) only */
207 	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
208 		ch.min = 1;
209 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
210 		ch.max = 2;
211 #else
212 		ch.max = 1;
213 #endif
214 	/* U8 is stereo only */
215 	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
216 		ch.min = ch.max = 2;
217 	/* S16_LE and S24_3LE must be at least stereo */
218 	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
219 			       SNDRV_PCM_FMTBIT_S24_3LE)))
220 		ch.min = 2;
221 	else
222 		return 0;
223 
224 	return snd_interval_refine(c, &ch);
225 }
226 
227 
228 
229 /* Since the sample rate is a global setting, do allow the user to change the
230 sample rate only if there is only one pcm device open. */
231 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
232 			       struct snd_pcm_hw_rule *rule)
233 {
234 	struct snd_interval *rate = hw_param_interval(params,
235 						      SNDRV_PCM_HW_PARAM_RATE);
236 	struct echoaudio *chip = rule->private;
237 	struct snd_interval fixed;
238 	int err;
239 
240 	guard(mutex)(&chip->mode_mutex);
241 
242 	if (chip->can_set_rate) {
243 		err = 0;
244 	} else {
245 		snd_interval_any(&fixed);
246 		fixed.min = fixed.max = chip->sample_rate;
247 		err = snd_interval_refine(rate, &fixed);
248 	}
249 
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 	guard(mutex)(&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 		return err;
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 		return err;
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 		return err;
440 
441 	return 0;
442 }
443 
444 
445 
446 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
447 
448 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
449 {
450 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
451 	int err, max_channels;
452 
453 	max_channels = num_digital_busses_out(chip) - substream->number;
454 	guard(mutex)(&chip->mode_mutex);
455 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
456 		err = pcm_open(substream, max_channels);
457 	else	/* If the card has ADAT, subtract the 6 channels
458 		 * that S/PDIF doesn't have
459 		 */
460 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
461 
462 	if (err < 0)
463 		return err;
464 
465 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
466 				  SNDRV_PCM_HW_PARAM_CHANNELS,
467 				  hw_rule_playback_channels_by_format,
468 				  NULL, SNDRV_PCM_HW_PARAM_FORMAT,
469 				  -1);
470 	if (err < 0)
471 		return err;
472 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
473 				  SNDRV_PCM_HW_PARAM_FORMAT,
474 				  hw_rule_playback_format_by_channels,
475 				  NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
476 				  -1);
477 	if (err < 0)
478 		return err;
479 
480 	return 0;
481 }
482 
483 #endif /* !ECHOCARD_HAS_VMIXER */
484 
485 #endif /* ECHOCARD_HAS_DIGITAL_IO */
486 
487 
488 
489 static int pcm_close(struct snd_pcm_substream *substream)
490 {
491 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
492 
493 	/* Nothing to do here. Audio is already off and pipe will be
494 	 * freed by its callback
495 	 */
496 
497 	guard(mutex)(&chip->mode_mutex);
498 
499 	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
500 		chip->opencount, chip->can_set_rate, chip->rate_set);
501 
502 	chip->opencount--;
503 
504 	switch (chip->opencount) {
505 	case 1:
506 		chip->can_set_rate = 1;
507 		break;
508 
509 	case 0:
510 		chip->rate_set = 0;
511 		break;
512 	}
513 
514 	return 0;
515 }
516 
517 
518 
519 /* Channel allocation and scatter-gather list setup */
520 static int init_engine(struct snd_pcm_substream *substream,
521 		       struct snd_pcm_hw_params *hw_params,
522 		       int pipe_index, int interleave)
523 {
524 	struct echoaudio *chip;
525 	int err, per, rest, page, edge, offs;
526 	struct audiopipe *pipe;
527 
528 	chip = snd_pcm_substream_chip(substream);
529 	pipe = (struct audiopipe *) substream->runtime->private_data;
530 
531 	/* Sets up che hardware. If it's already initialized, reset and
532 	 * redo with the new parameters
533 	 */
534 	scoped_guard(spinlock_irq, &chip->lock) {
535 		if (pipe->index >= 0) {
536 			dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
537 			err = free_pipes(chip, pipe);
538 			snd_BUG_ON(err);
539 			chip->substream[pipe->index] = NULL;
540 		}
541 
542 		err = allocate_pipes(chip, pipe, pipe_index, interleave);
543 		if (err < 0) {
544 			dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
545 				pipe_index, err);
546 			return err;
547 		}
548 	}
549 	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
550 
551 	dev_dbg(chip->card->dev,
552 		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
553 		params_buffer_bytes(hw_params), params_periods(hw_params),
554 		params_period_bytes(hw_params));
555 
556 	sglist_init(chip, pipe);
557 	edge = PAGE_SIZE;
558 	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
559 	     per++) {
560 		rest = params_period_bytes(hw_params);
561 		if (offs + rest > params_buffer_bytes(hw_params))
562 			rest = params_buffer_bytes(hw_params) - offs;
563 		while (rest) {
564 			dma_addr_t addr;
565 			addr = snd_pcm_sgbuf_get_addr(substream, offs);
566 			if (rest <= edge - offs) {
567 				sglist_add_mapping(chip, pipe, addr, rest);
568 				sglist_add_irq(chip, pipe);
569 				offs += rest;
570 				rest = 0;
571 			} else {
572 				sglist_add_mapping(chip, pipe, addr,
573 						   edge - offs);
574 				rest -= edge - offs;
575 				offs = edge;
576 			}
577 			if (offs == edge) {
578 				edge += PAGE_SIZE;
579 				page++;
580 			}
581 		}
582 	}
583 
584 	/* Close the ring buffer */
585 	sglist_wrap(chip, pipe);
586 
587 	/* This stuff is used by the irq handler, so it must be
588 	 * initialized before chip->substream
589 	 */
590 	pipe->last_period = 0;
591 	pipe->last_counter = 0;
592 	pipe->position = 0;
593 	smp_wmb();
594 	chip->substream[pipe_index] = substream;
595 	chip->rate_set = 1;
596 	guard(spinlock_irq)(&chip->lock);
597 	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
598 	return 0;
599 }
600 
601 
602 
603 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
604 				   struct snd_pcm_hw_params *hw_params)
605 {
606 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
607 
608 	return init_engine(substream, hw_params, px_analog_in(chip) +
609 			substream->number, params_channels(hw_params));
610 }
611 
612 
613 
614 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
615 				    struct snd_pcm_hw_params *hw_params)
616 {
617 	return init_engine(substream, hw_params, substream->number,
618 			   params_channels(hw_params));
619 }
620 
621 
622 
623 #ifdef ECHOCARD_HAS_DIGITAL_IO
624 
625 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
626 				    struct snd_pcm_hw_params *hw_params)
627 {
628 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
629 
630 	return init_engine(substream, hw_params, px_digital_in(chip) +
631 			substream->number, params_channels(hw_params));
632 }
633 
634 
635 
636 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
637 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
638 				     struct snd_pcm_hw_params *hw_params)
639 {
640 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
641 
642 	return init_engine(substream, hw_params, px_digital_out(chip) +
643 			substream->number, params_channels(hw_params));
644 }
645 #endif /* !ECHOCARD_HAS_VMIXER */
646 
647 #endif /* ECHOCARD_HAS_DIGITAL_IO */
648 
649 
650 
651 static int pcm_hw_free(struct snd_pcm_substream *substream)
652 {
653 	struct echoaudio *chip;
654 	struct audiopipe *pipe;
655 
656 	chip = snd_pcm_substream_chip(substream);
657 	pipe = (struct audiopipe *) substream->runtime->private_data;
658 
659 	guard(spinlock_irq)(&chip->lock);
660 	if (pipe->index >= 0) {
661 		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
662 		free_pipes(chip, pipe);
663 		chip->substream[pipe->index] = NULL;
664 		pipe->index = -1;
665 	}
666 
667 	return 0;
668 }
669 
670 
671 
672 static int pcm_prepare(struct snd_pcm_substream *substream)
673 {
674 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
675 	struct snd_pcm_runtime *runtime = substream->runtime;
676 	struct audioformat format;
677 	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
678 
679 	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
680 		runtime->rate, runtime->format, runtime->channels);
681 	format.interleave = runtime->channels;
682 	format.data_are_bigendian = 0;
683 	format.mono_to_stereo = 0;
684 	switch (runtime->format) {
685 	case SNDRV_PCM_FORMAT_U8:
686 		format.bits_per_sample = 8;
687 		break;
688 	case SNDRV_PCM_FORMAT_S16_LE:
689 		format.bits_per_sample = 16;
690 		break;
691 	case SNDRV_PCM_FORMAT_S24_3LE:
692 		format.bits_per_sample = 24;
693 		break;
694 	case SNDRV_PCM_FORMAT_S32_BE:
695 		format.data_are_bigendian = 1;
696 		fallthrough;
697 	case SNDRV_PCM_FORMAT_S32_LE:
698 		format.bits_per_sample = 32;
699 		break;
700 	default:
701 		dev_err(chip->card->dev,
702 			"Prepare error: unsupported format %d\n",
703 			runtime->format);
704 		return -EINVAL;
705 	}
706 
707 	if (snd_BUG_ON(pipe_index >= px_num(chip)))
708 		return -EINVAL;
709 
710 	/*
711 	 * We passed checks we can do independently; now take
712 	 * exclusive control
713 	 */
714 
715 	guard(spinlock_irq)(&chip->lock);
716 
717 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
718 		return -EINVAL;
719 
720 	set_audio_format(chip, pipe_index, &format);
721 
722 	return 0;
723 }
724 
725 
726 
727 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
728 {
729 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
730 	struct audiopipe *pipe;
731 	int i, err;
732 	u32 channelmask = 0;
733 	struct snd_pcm_substream *s;
734 
735 	snd_pcm_group_for_each_entry(s, substream) {
736 		for (i = 0; i < DSP_MAXPIPES; i++) {
737 			if (s == chip->substream[i]) {
738 				channelmask |= 1 << i;
739 				snd_pcm_trigger_done(s, substream);
740 			}
741 		}
742 	}
743 
744 	guard(spinlock)(&chip->lock);
745 	switch (cmd) {
746 	case SNDRV_PCM_TRIGGER_RESUME:
747 	case SNDRV_PCM_TRIGGER_START:
748 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
749 		for (i = 0; i < DSP_MAXPIPES; i++) {
750 			if (channelmask & (1 << i)) {
751 				pipe = chip->substream[i]->runtime->private_data;
752 				switch (pipe->state) {
753 				case PIPE_STATE_STOPPED:
754 					pipe->last_period = 0;
755 					pipe->last_counter = 0;
756 					pipe->position = 0;
757 					*pipe->dma_counter = 0;
758 					fallthrough;
759 				case PIPE_STATE_PAUSED:
760 					pipe->state = PIPE_STATE_STARTED;
761 					break;
762 				case PIPE_STATE_STARTED:
763 					break;
764 				}
765 			}
766 		}
767 		err = start_transport(chip, channelmask,
768 				      chip->pipe_cyclic_mask);
769 		break;
770 	case SNDRV_PCM_TRIGGER_SUSPEND:
771 	case SNDRV_PCM_TRIGGER_STOP:
772 		for (i = 0; i < DSP_MAXPIPES; i++) {
773 			if (channelmask & (1 << i)) {
774 				pipe = chip->substream[i]->runtime->private_data;
775 				pipe->state = PIPE_STATE_STOPPED;
776 			}
777 		}
778 		err = stop_transport(chip, channelmask);
779 		break;
780 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
781 		for (i = 0; i < DSP_MAXPIPES; i++) {
782 			if (channelmask & (1 << i)) {
783 				pipe = chip->substream[i]->runtime->private_data;
784 				pipe->state = PIPE_STATE_PAUSED;
785 			}
786 		}
787 		err = pause_transport(chip, channelmask);
788 		break;
789 	default:
790 		err = -EINVAL;
791 	}
792 	return err;
793 }
794 
795 
796 
797 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
798 {
799 	struct snd_pcm_runtime *runtime = substream->runtime;
800 	struct audiopipe *pipe = runtime->private_data;
801 	u32 counter, step;
802 
803 	/*
804 	 * IRQ handling runs concurrently. Do not share tracking of
805 	 * counter with it, which would race or require locking
806 	 */
807 
808 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
809 
810 	step = counter - pipe->last_counter;  /* handles wrapping */
811 	pipe->last_counter = counter;
812 
813 	/* counter doesn't neccessarily wrap on a multiple of
814 	 * buffer_size, so can't derive the position; must
815 	 * accumulate */
816 
817 	pipe->position += step;
818 	pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
819 
820 	return bytes_to_frames(runtime, pipe->position);
821 }
822 
823 
824 
825 /* pcm *_ops structures */
826 static const struct snd_pcm_ops analog_playback_ops = {
827 	.open = pcm_analog_out_open,
828 	.close = pcm_close,
829 	.hw_params = pcm_analog_out_hw_params,
830 	.hw_free = pcm_hw_free,
831 	.prepare = pcm_prepare,
832 	.trigger = pcm_trigger,
833 	.pointer = pcm_pointer,
834 };
835 static const struct snd_pcm_ops analog_capture_ops = {
836 	.open = pcm_analog_in_open,
837 	.close = pcm_close,
838 	.hw_params = pcm_analog_in_hw_params,
839 	.hw_free = pcm_hw_free,
840 	.prepare = pcm_prepare,
841 	.trigger = pcm_trigger,
842 	.pointer = pcm_pointer,
843 };
844 #ifdef ECHOCARD_HAS_DIGITAL_IO
845 #ifndef ECHOCARD_HAS_VMIXER
846 static const struct snd_pcm_ops digital_playback_ops = {
847 	.open = pcm_digital_out_open,
848 	.close = pcm_close,
849 	.hw_params = pcm_digital_out_hw_params,
850 	.hw_free = pcm_hw_free,
851 	.prepare = pcm_prepare,
852 	.trigger = pcm_trigger,
853 	.pointer = pcm_pointer,
854 };
855 #endif /* !ECHOCARD_HAS_VMIXER */
856 static const struct snd_pcm_ops digital_capture_ops = {
857 	.open = pcm_digital_in_open,
858 	.close = pcm_close,
859 	.hw_params = pcm_digital_in_hw_params,
860 	.hw_free = pcm_hw_free,
861 	.prepare = pcm_prepare,
862 	.trigger = pcm_trigger,
863 	.pointer = pcm_pointer,
864 };
865 #endif /* ECHOCARD_HAS_DIGITAL_IO */
866 
867 
868 
869 /* Preallocate memory only for the first substream because it's the most
870  * used one
871  */
872 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
873 {
874 	struct snd_pcm_substream *ss;
875 	int stream;
876 
877 	for (stream = 0; stream < 2; stream++)
878 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
879 			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
880 						   dev,
881 						   ss->number ? 0 : 128<<10,
882 						   256<<10);
883 }
884 
885 
886 
887 /*<--snd_echo_probe() */
888 static int snd_echo_new_pcm(struct echoaudio *chip)
889 {
890 	struct snd_pcm *pcm;
891 	int err;
892 
893 #ifdef ECHOCARD_HAS_VMIXER
894 	/* This card has a Vmixer, that is there is no direct mapping from PCM
895 	streams to physical outputs. The user can mix the streams as he wishes
896 	via control interface and it's possible to send any stream to any
897 	output, thus it makes no sense to keep analog and digital outputs
898 	separated */
899 
900 	/* PCM#0 Virtual outputs and analog inputs */
901 	err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
902 			  num_analog_busses_in(chip), &pcm);
903 	if (err < 0)
904 		return err;
905 	pcm->private_data = chip;
906 	chip->analog_pcm = pcm;
907 	strscpy(pcm->name, chip->card->shortname);
908 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
909 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
910 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
911 
912 #ifdef ECHOCARD_HAS_DIGITAL_IO
913 	/* PCM#1 Digital inputs, no outputs */
914 	err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
915 			  num_digital_busses_in(chip), &pcm);
916 	if (err < 0)
917 		return err;
918 	pcm->private_data = chip;
919 	chip->digital_pcm = pcm;
920 	strscpy(pcm->name, chip->card->shortname);
921 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
922 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
923 #endif /* ECHOCARD_HAS_DIGITAL_IO */
924 
925 #else /* ECHOCARD_HAS_VMIXER */
926 
927 	/* The card can manage substreams formed by analog and digital channels
928 	at the same time, but I prefer to keep analog and digital channels
929 	separated, because that mixed thing is confusing and useless. So we
930 	register two PCM devices: */
931 
932 	/* PCM#0 Analog i/o */
933 	err = snd_pcm_new(chip->card, "Analog PCM", 0,
934 			  num_analog_busses_out(chip),
935 			  num_analog_busses_in(chip), &pcm);
936 	if (err < 0)
937 		return err;
938 	pcm->private_data = chip;
939 	chip->analog_pcm = pcm;
940 	strscpy(pcm->name, chip->card->shortname);
941 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
942 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
943 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
944 
945 #ifdef ECHOCARD_HAS_DIGITAL_IO
946 	/* PCM#1 Digital i/o */
947 	err = snd_pcm_new(chip->card, "Digital PCM", 1,
948 			  num_digital_busses_out(chip),
949 			  num_digital_busses_in(chip), &pcm);
950 	if (err < 0)
951 		return err;
952 	pcm->private_data = chip;
953 	chip->digital_pcm = pcm;
954 	strscpy(pcm->name, chip->card->shortname);
955 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
956 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
957 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
958 #endif /* ECHOCARD_HAS_DIGITAL_IO */
959 
960 #endif /* ECHOCARD_HAS_VMIXER */
961 
962 	return 0;
963 }
964 
965 
966 
967 
968 /******************************************************************************
969 	Control interface
970 ******************************************************************************/
971 
972 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
973 
974 /******************* PCM output volume *******************/
975 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
976 				     struct snd_ctl_elem_info *uinfo)
977 {
978 	struct echoaudio *chip;
979 
980 	chip = snd_kcontrol_chip(kcontrol);
981 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
982 	uinfo->count = num_busses_out(chip);
983 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
984 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
985 	return 0;
986 }
987 
988 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
989 				    struct snd_ctl_elem_value *ucontrol)
990 {
991 	struct echoaudio *chip;
992 	int c;
993 
994 	chip = snd_kcontrol_chip(kcontrol);
995 	for (c = 0; c < num_busses_out(chip); c++)
996 		ucontrol->value.integer.value[c] = chip->output_gain[c];
997 	return 0;
998 }
999 
1000 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1001 				    struct snd_ctl_elem_value *ucontrol)
1002 {
1003 	struct echoaudio *chip;
1004 	int c, changed, gain;
1005 
1006 	changed = 0;
1007 	chip = snd_kcontrol_chip(kcontrol);
1008 	guard(spinlock_irq)(&chip->lock);
1009 	for (c = 0; c < num_busses_out(chip); c++) {
1010 		gain = ucontrol->value.integer.value[c];
1011 		/* Ignore out of range values */
1012 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1013 			continue;
1014 		if (chip->output_gain[c] != gain) {
1015 			set_output_gain(chip, c, gain);
1016 			changed = 1;
1017 		}
1018 	}
1019 	if (changed)
1020 		update_output_line_level(chip);
1021 	return changed;
1022 }
1023 
1024 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1025 /* On the Mia this one controls the line-out volume */
1026 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1027 	.name = "Line Playback Volume",
1028 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1029 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1030 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1031 	.info = snd_echo_output_gain_info,
1032 	.get = snd_echo_output_gain_get,
1033 	.put = snd_echo_output_gain_put,
1034 	.tlv = {.p = db_scale_output_gain},
1035 };
1036 #else
1037 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1038 	.name = "PCM Playback Volume",
1039 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1040 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1041 	.info = snd_echo_output_gain_info,
1042 	.get = snd_echo_output_gain_get,
1043 	.put = snd_echo_output_gain_put,
1044 	.tlv = {.p = db_scale_output_gain},
1045 };
1046 #endif
1047 
1048 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1049 
1050 
1051 
1052 #ifdef ECHOCARD_HAS_INPUT_GAIN
1053 
1054 /******************* Analog input volume *******************/
1055 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1056 				    struct snd_ctl_elem_info *uinfo)
1057 {
1058 	struct echoaudio *chip;
1059 
1060 	chip = snd_kcontrol_chip(kcontrol);
1061 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1062 	uinfo->count = num_analog_busses_in(chip);
1063 	uinfo->value.integer.min = ECHOGAIN_MININP;
1064 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1065 	return 0;
1066 }
1067 
1068 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1069 				   struct snd_ctl_elem_value *ucontrol)
1070 {
1071 	struct echoaudio *chip;
1072 	int c;
1073 
1074 	chip = snd_kcontrol_chip(kcontrol);
1075 	for (c = 0; c < num_analog_busses_in(chip); c++)
1076 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1077 	return 0;
1078 }
1079 
1080 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1081 				   struct snd_ctl_elem_value *ucontrol)
1082 {
1083 	struct echoaudio *chip;
1084 	int c, gain, changed;
1085 
1086 	changed = 0;
1087 	chip = snd_kcontrol_chip(kcontrol);
1088 	guard(spinlock_irq)(&chip->lock);
1089 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1090 		gain = ucontrol->value.integer.value[c];
1091 		/* Ignore out of range values */
1092 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1093 			continue;
1094 		if (chip->input_gain[c] != gain) {
1095 			set_input_gain(chip, c, gain);
1096 			changed = 1;
1097 		}
1098 	}
1099 	if (changed)
1100 		update_input_line_level(chip);
1101 	return changed;
1102 }
1103 
1104 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1105 
1106 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1107 	.name = "Line Capture Volume",
1108 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1109 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1110 	.info = snd_echo_input_gain_info,
1111 	.get = snd_echo_input_gain_get,
1112 	.put = snd_echo_input_gain_put,
1113 	.tlv = {.p = db_scale_input_gain},
1114 };
1115 
1116 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1117 
1118 
1119 
1120 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1121 
1122 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1123 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1124 					 struct snd_ctl_elem_info *uinfo)
1125 {
1126 	struct echoaudio *chip;
1127 
1128 	chip = snd_kcontrol_chip(kcontrol);
1129 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1130 	uinfo->count = num_analog_busses_out(chip);
1131 	uinfo->value.integer.min = 0;
1132 	uinfo->value.integer.max = 1;
1133 	return 0;
1134 }
1135 
1136 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1137 				       struct snd_ctl_elem_value *ucontrol)
1138 {
1139 	struct echoaudio *chip;
1140 	int c;
1141 
1142 	chip = snd_kcontrol_chip(kcontrol);
1143 	for (c = 0; c < num_analog_busses_out(chip); c++)
1144 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1145 	return 0;
1146 }
1147 
1148 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1149 				       struct snd_ctl_elem_value *ucontrol)
1150 {
1151 	struct echoaudio *chip;
1152 	int c, changed;
1153 
1154 	changed = 0;
1155 	chip = snd_kcontrol_chip(kcontrol);
1156 	guard(spinlock_irq)(&chip->lock);
1157 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1158 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1159 			set_nominal_level(chip, c,
1160 					  ucontrol->value.integer.value[c]);
1161 			changed = 1;
1162 		}
1163 	}
1164 	if (changed)
1165 		update_output_line_level(chip);
1166 	return changed;
1167 }
1168 
1169 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1170 	.name = "Line Playback Switch (-10dBV)",
1171 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1172 	.info = snd_echo_output_nominal_info,
1173 	.get = snd_echo_output_nominal_get,
1174 	.put = snd_echo_output_nominal_put,
1175 };
1176 
1177 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1178 
1179 
1180 
1181 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1182 
1183 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1184 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1185 				       struct snd_ctl_elem_info *uinfo)
1186 {
1187 	struct echoaudio *chip;
1188 
1189 	chip = snd_kcontrol_chip(kcontrol);
1190 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1191 	uinfo->count = num_analog_busses_in(chip);
1192 	uinfo->value.integer.min = 0;
1193 	uinfo->value.integer.max = 1;
1194 	return 0;
1195 }
1196 
1197 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1198 				      struct snd_ctl_elem_value *ucontrol)
1199 {
1200 	struct echoaudio *chip;
1201 	int c;
1202 
1203 	chip = snd_kcontrol_chip(kcontrol);
1204 	for (c = 0; c < num_analog_busses_in(chip); c++)
1205 		ucontrol->value.integer.value[c] =
1206 			chip->nominal_level[bx_analog_in(chip) + c];
1207 	return 0;
1208 }
1209 
1210 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1211 				      struct snd_ctl_elem_value *ucontrol)
1212 {
1213 	struct echoaudio *chip;
1214 	int c, changed;
1215 
1216 	changed = 0;
1217 	chip = snd_kcontrol_chip(kcontrol);
1218 	guard(spinlock_irq)(&chip->lock);
1219 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1220 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1221 		    ucontrol->value.integer.value[c]) {
1222 			set_nominal_level(chip, bx_analog_in(chip) + c,
1223 					  ucontrol->value.integer.value[c]);
1224 			changed = 1;
1225 		}
1226 	}
1227 	if (changed)
1228 		update_output_line_level(chip);	/* "Output" is not a mistake
1229 						 * here.
1230 						 */
1231 	return changed;
1232 }
1233 
1234 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1235 	.name = "Line Capture Switch (-10dBV)",
1236 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1237 	.info = snd_echo_input_nominal_info,
1238 	.get = snd_echo_input_nominal_get,
1239 	.put = snd_echo_input_nominal_put,
1240 };
1241 
1242 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1243 
1244 
1245 
1246 #ifdef ECHOCARD_HAS_MONITOR
1247 
1248 /******************* Monitor mixer *******************/
1249 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1250 			       struct snd_ctl_elem_info *uinfo)
1251 {
1252 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1253 	uinfo->count = 1;
1254 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1255 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1256 	return 0;
1257 }
1258 
1259 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1260 			      struct snd_ctl_elem_value *ucontrol)
1261 {
1262 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1263 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1264 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1265 
1266 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1267 		return -EINVAL;
1268 
1269 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1270 	return 0;
1271 }
1272 
1273 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1274 			      struct snd_ctl_elem_value *ucontrol)
1275 {
1276 	struct echoaudio *chip;
1277 	int changed,  gain;
1278 	unsigned int out, in;
1279 
1280 	changed = 0;
1281 	chip = snd_kcontrol_chip(kcontrol);
1282 	out = ucontrol->id.index / num_busses_in(chip);
1283 	in = ucontrol->id.index % num_busses_in(chip);
1284 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1285 		return -EINVAL;
1286 	gain = ucontrol->value.integer.value[0];
1287 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1288 		return -EINVAL;
1289 	if (chip->monitor_gain[out][in] != gain) {
1290 		guard(spinlock_irq)(&chip->lock);
1291 		set_monitor_gain(chip, out, in, gain);
1292 		update_output_line_level(chip);
1293 		changed = 1;
1294 	}
1295 	return changed;
1296 }
1297 
1298 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1299 	.name = "Monitor Mixer Volume",
1300 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1301 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1302 	.info = snd_echo_mixer_info,
1303 	.get = snd_echo_mixer_get,
1304 	.put = snd_echo_mixer_put,
1305 	.tlv = {.p = db_scale_output_gain},
1306 };
1307 
1308 #endif /* ECHOCARD_HAS_MONITOR */
1309 
1310 
1311 
1312 #ifdef ECHOCARD_HAS_VMIXER
1313 
1314 /******************* Vmixer *******************/
1315 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1316 				struct snd_ctl_elem_info *uinfo)
1317 {
1318 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1319 	uinfo->count = 1;
1320 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1321 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1322 	return 0;
1323 }
1324 
1325 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1326 			       struct snd_ctl_elem_value *ucontrol)
1327 {
1328 	struct echoaudio *chip;
1329 
1330 	chip = snd_kcontrol_chip(kcontrol);
1331 	ucontrol->value.integer.value[0] =
1332 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1333 			[ucontrol->id.index % num_pipes_out(chip)];
1334 	return 0;
1335 }
1336 
1337 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1338 			       struct snd_ctl_elem_value *ucontrol)
1339 {
1340 	struct echoaudio *chip;
1341 	int gain, changed;
1342 	short vch, out;
1343 
1344 	changed = 0;
1345 	chip = snd_kcontrol_chip(kcontrol);
1346 	out = ucontrol->id.index / num_pipes_out(chip);
1347 	vch = ucontrol->id.index % num_pipes_out(chip);
1348 	gain = ucontrol->value.integer.value[0];
1349 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1350 		return -EINVAL;
1351 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1352 		guard(spinlock_irq)(&chip->lock);
1353 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1354 		update_vmixer_level(chip);
1355 		changed = 1;
1356 	}
1357 	return changed;
1358 }
1359 
1360 static struct snd_kcontrol_new snd_echo_vmixer = {
1361 	.name = "VMixer Volume",
1362 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1363 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1364 	.info = snd_echo_vmixer_info,
1365 	.get = snd_echo_vmixer_get,
1366 	.put = snd_echo_vmixer_put,
1367 	.tlv = {.p = db_scale_output_gain},
1368 };
1369 
1370 #endif /* ECHOCARD_HAS_VMIXER */
1371 
1372 
1373 
1374 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1375 
1376 /******************* Digital mode switch *******************/
1377 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1378 				      struct snd_ctl_elem_info *uinfo)
1379 {
1380 	static const char * const names[4] = {
1381 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1382 		"S/PDIF Cdrom"
1383 	};
1384 	struct echoaudio *chip;
1385 
1386 	chip = snd_kcontrol_chip(kcontrol);
1387 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1388 }
1389 
1390 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1391 				     struct snd_ctl_elem_value *ucontrol)
1392 {
1393 	struct echoaudio *chip;
1394 	int i, mode;
1395 
1396 	chip = snd_kcontrol_chip(kcontrol);
1397 	mode = chip->digital_mode;
1398 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1399 		if (mode == chip->digital_mode_list[i]) {
1400 			ucontrol->value.enumerated.item[0] = i;
1401 			break;
1402 		}
1403 	return 0;
1404 }
1405 
1406 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1407 				     struct snd_ctl_elem_value *ucontrol)
1408 {
1409 	struct echoaudio *chip;
1410 	int changed;
1411 	unsigned short emode, dmode;
1412 
1413 	changed = 0;
1414 	chip = snd_kcontrol_chip(kcontrol);
1415 
1416 	emode = ucontrol->value.enumerated.item[0];
1417 	if (emode >= chip->num_digital_modes)
1418 		return -EINVAL;
1419 	dmode = chip->digital_mode_list[emode];
1420 
1421 	if (dmode != chip->digital_mode) {
1422 		/* mode_mutex is required to make this operation atomic wrt
1423 		pcm_digital_*_open() and set_input_clock() functions. */
1424 		guard(mutex)(&chip->mode_mutex);
1425 
1426 		/* Do not allow the user to change the digital mode when a pcm
1427 		device is open because it also changes the number of channels
1428 		and the allowed sample rates */
1429 		if (chip->opencount) {
1430 			changed = -EAGAIN;
1431 		} else {
1432 			changed = set_digital_mode(chip, dmode);
1433 			/* If we had to change the clock source, report it */
1434 			if (changed > 0 && chip->clock_src_ctl) {
1435 				snd_ctl_notify(chip->card,
1436 					       SNDRV_CTL_EVENT_MASK_VALUE,
1437 					       &chip->clock_src_ctl->id);
1438 				dev_dbg(chip->card->dev,
1439 					"SDM() =%d\n", changed);
1440 			}
1441 			if (changed >= 0)
1442 				changed = 1;	/* No errors */
1443 		}
1444 	}
1445 	return changed;
1446 }
1447 
1448 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1449 	.name = "Digital mode Switch",
1450 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1451 	.info = snd_echo_digital_mode_info,
1452 	.get = snd_echo_digital_mode_get,
1453 	.put = snd_echo_digital_mode_put,
1454 };
1455 
1456 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1457 
1458 
1459 
1460 #ifdef ECHOCARD_HAS_DIGITAL_IO
1461 
1462 /******************* S/PDIF mode switch *******************/
1463 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1464 				    struct snd_ctl_elem_info *uinfo)
1465 {
1466 	static const char * const names[2] = {"Consumer", "Professional"};
1467 
1468 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1469 }
1470 
1471 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1472 				   struct snd_ctl_elem_value *ucontrol)
1473 {
1474 	struct echoaudio *chip;
1475 
1476 	chip = snd_kcontrol_chip(kcontrol);
1477 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1478 	return 0;
1479 }
1480 
1481 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1482 				   struct snd_ctl_elem_value *ucontrol)
1483 {
1484 	struct echoaudio *chip;
1485 	int mode;
1486 
1487 	chip = snd_kcontrol_chip(kcontrol);
1488 	mode = !!ucontrol->value.enumerated.item[0];
1489 	if (mode != chip->professional_spdif) {
1490 		guard(spinlock_irq)(&chip->lock);
1491 		set_professional_spdif(chip, mode);
1492 		return 1;
1493 	}
1494 	return 0;
1495 }
1496 
1497 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1498 	.name = "S/PDIF mode Switch",
1499 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1500 	.info = snd_echo_spdif_mode_info,
1501 	.get = snd_echo_spdif_mode_get,
1502 	.put = snd_echo_spdif_mode_put,
1503 };
1504 
1505 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1506 
1507 
1508 
1509 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1510 
1511 /******************* Select input clock source *******************/
1512 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1513 				      struct snd_ctl_elem_info *uinfo)
1514 {
1515 	static const char * const names[8] = {
1516 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1517 		"ESync96", "MTC"
1518 	};
1519 	struct echoaudio *chip;
1520 
1521 	chip = snd_kcontrol_chip(kcontrol);
1522 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1523 }
1524 
1525 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1526 				     struct snd_ctl_elem_value *ucontrol)
1527 {
1528 	struct echoaudio *chip;
1529 	int i, clock;
1530 
1531 	chip = snd_kcontrol_chip(kcontrol);
1532 	clock = chip->input_clock;
1533 
1534 	for (i = 0; i < chip->num_clock_sources; i++)
1535 		if (clock == chip->clock_source_list[i])
1536 			ucontrol->value.enumerated.item[0] = i;
1537 
1538 	return 0;
1539 }
1540 
1541 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1542 				     struct snd_ctl_elem_value *ucontrol)
1543 {
1544 	struct echoaudio *chip;
1545 	int changed;
1546 	unsigned int eclock, dclock;
1547 
1548 	changed = 0;
1549 	chip = snd_kcontrol_chip(kcontrol);
1550 	eclock = ucontrol->value.enumerated.item[0];
1551 	if (eclock >= chip->input_clock_types)
1552 		return -EINVAL;
1553 	dclock = chip->clock_source_list[eclock];
1554 	if (chip->input_clock != dclock) {
1555 		guard(mutex)(&chip->mode_mutex);
1556 		guard(spinlock_irq)(&chip->lock);
1557 		changed = set_input_clock(chip, dclock);
1558 		if (!changed)
1559 			changed = 1;	/* no errors */
1560 	}
1561 
1562 	if (changed < 0)
1563 		dev_dbg(chip->card->dev,
1564 			"seticlk val%d err 0x%x\n", dclock, changed);
1565 
1566 	return changed;
1567 }
1568 
1569 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1570 	.name = "Sample Clock Source",
1571 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1572 	.info = snd_echo_clock_source_info,
1573 	.get = snd_echo_clock_source_get,
1574 	.put = snd_echo_clock_source_put,
1575 };
1576 
1577 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1578 
1579 
1580 
1581 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1582 
1583 /******************* Phantom power switch *******************/
1584 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1585 
1586 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1587 				      struct snd_ctl_elem_value *ucontrol)
1588 {
1589 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1590 
1591 	ucontrol->value.integer.value[0] = chip->phantom_power;
1592 	return 0;
1593 }
1594 
1595 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1596 				      struct snd_ctl_elem_value *ucontrol)
1597 {
1598 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1599 	int power, changed = 0;
1600 
1601 	power = !!ucontrol->value.integer.value[0];
1602 	if (chip->phantom_power != power) {
1603 		guard(spinlock_irq)(&chip->lock);
1604 		changed = set_phantom_power(chip, power);
1605 		if (changed == 0)
1606 			changed = 1;	/* no errors */
1607 	}
1608 	return changed;
1609 }
1610 
1611 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1612 	.name = "Phantom power Switch",
1613 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1614 	.info = snd_echo_phantom_power_info,
1615 	.get = snd_echo_phantom_power_get,
1616 	.put = snd_echo_phantom_power_put,
1617 };
1618 
1619 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1620 
1621 
1622 
1623 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1624 
1625 /******************* Digital input automute switch *******************/
1626 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1627 
1628 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1629 				 struct snd_ctl_elem_value *ucontrol)
1630 {
1631 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632 
1633 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1634 	return 0;
1635 }
1636 
1637 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1638 				 struct snd_ctl_elem_value *ucontrol)
1639 {
1640 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1641 	int automute, changed = 0;
1642 
1643 	automute = !!ucontrol->value.integer.value[0];
1644 	if (chip->digital_in_automute != automute) {
1645 		guard(spinlock_irq)(&chip->lock);
1646 		changed = set_input_auto_mute(chip, automute);
1647 		if (changed == 0)
1648 			changed = 1;	/* no errors */
1649 	}
1650 	return changed;
1651 }
1652 
1653 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1654 	.name = "Digital Capture Switch (automute)",
1655 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1656 	.info = snd_echo_automute_info,
1657 	.get = snd_echo_automute_get,
1658 	.put = snd_echo_automute_put,
1659 };
1660 
1661 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1662 
1663 
1664 
1665 /******************* VU-meters switch *******************/
1666 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1667 
1668 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1669 					struct snd_ctl_elem_value *ucontrol)
1670 {
1671 	struct echoaudio *chip;
1672 
1673 	chip = snd_kcontrol_chip(kcontrol);
1674 	guard(spinlock_irq)(&chip->lock);
1675 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1676 	return 1;
1677 }
1678 
1679 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1680 	.name = "VU-meters Switch",
1681 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1682 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1683 	.info = snd_echo_vumeters_switch_info,
1684 	.put = snd_echo_vumeters_switch_put,
1685 };
1686 
1687 
1688 
1689 /***** Read VU-meters (input, output, analog and digital together) *****/
1690 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1691 				  struct snd_ctl_elem_info *uinfo)
1692 {
1693 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1694 	uinfo->count = 96;
1695 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1696 	uinfo->value.integer.max = 0;
1697 	return 0;
1698 }
1699 
1700 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1701 				 struct snd_ctl_elem_value *ucontrol)
1702 {
1703 	struct echoaudio *chip;
1704 
1705 	chip = snd_kcontrol_chip(kcontrol);
1706 	get_audio_meters(chip, ucontrol->value.integer.value);
1707 	return 0;
1708 }
1709 
1710 static const struct snd_kcontrol_new snd_echo_vumeters = {
1711 	.name = "VU-meters",
1712 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1713 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1714 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1715 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1716 	.info = snd_echo_vumeters_info,
1717 	.get = snd_echo_vumeters_get,
1718 	.tlv = {.p = db_scale_output_gain},
1719 };
1720 
1721 
1722 
1723 /*** Channels info - it exports informations about the number of channels ***/
1724 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1725 				       struct snd_ctl_elem_info *uinfo)
1726 {
1727 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1728 	uinfo->count = 6;
1729 	uinfo->value.integer.min = 0;
1730 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1731 	return 0;
1732 }
1733 
1734 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1735 				      struct snd_ctl_elem_value *ucontrol)
1736 {
1737 	struct echoaudio *chip;
1738 	int detected, clocks, bit, src;
1739 
1740 	chip = snd_kcontrol_chip(kcontrol);
1741 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1742 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1743 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1744 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1745 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1746 
1747 	/* Compute the bitmask of the currently valid input clocks */
1748 	detected = detect_input_clocks(chip);
1749 	clocks = 0;
1750 	src = chip->num_clock_sources - 1;
1751 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1752 		if (detected & (1 << bit))
1753 			for (; src >= 0; src--)
1754 				if (bit == chip->clock_source_list[src]) {
1755 					clocks |= 1 << src;
1756 					break;
1757 				}
1758 	ucontrol->value.integer.value[5] = clocks;
1759 
1760 	return 0;
1761 }
1762 
1763 static const struct snd_kcontrol_new snd_echo_channels_info = {
1764 	.name = "Channels info",
1765 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1766 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1767 	.info = snd_echo_channels_info_info,
1768 	.get = snd_echo_channels_info_get,
1769 };
1770 
1771 
1772 
1773 
1774 /******************************************************************************
1775 	IRQ Handling
1776 ******************************************************************************/
1777 /* Check if a period has elapsed since last interrupt
1778  *
1779  * Don't make any updates to state; PCM core handles this with the
1780  * correct locks.
1781  *
1782  * \return true if a period has elapsed, otherwise false
1783  */
1784 static bool period_has_elapsed(struct snd_pcm_substream *substream)
1785 {
1786 	struct snd_pcm_runtime *runtime = substream->runtime;
1787 	struct audiopipe *pipe = runtime->private_data;
1788 	u32 counter, step;
1789 	size_t period_bytes;
1790 
1791 	if (pipe->state != PIPE_STATE_STARTED)
1792 		return false;
1793 
1794 	period_bytes = frames_to_bytes(runtime, runtime->period_size);
1795 
1796 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1797 
1798 	step = counter - pipe->last_period;  /* handles wrapping */
1799 	step -= step % period_bytes;  /* acknowledge whole periods only */
1800 
1801 	if (step == 0)
1802 		return false;  /* haven't advanced a whole period yet */
1803 
1804 	pipe->last_period += step;  /* used exclusively by us */
1805 	return true;
1806 }
1807 
1808 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1809 {
1810 	struct echoaudio *chip = dev_id;
1811 	int ss, st;
1812 
1813 	spin_lock(&chip->lock);
1814 	st = service_irq(chip);
1815 	if (st < 0) {
1816 		spin_unlock(&chip->lock);
1817 		return IRQ_NONE;
1818 	}
1819 	/* The hardware doesn't tell us which substream caused the irq,
1820 	thus we have to check all running substreams. */
1821 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1822 		struct snd_pcm_substream *substream;
1823 
1824 		substream = chip->substream[ss];
1825 		if (substream && period_has_elapsed(substream)) {
1826 			spin_unlock(&chip->lock);
1827 			snd_pcm_period_elapsed(substream);
1828 			spin_lock(&chip->lock);
1829 		}
1830 	}
1831 	spin_unlock(&chip->lock);
1832 
1833 #ifdef ECHOCARD_HAS_MIDI
1834 	if (st > 0 && chip->midi_in) {
1835 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1836 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1837 	}
1838 #endif
1839 	return IRQ_HANDLED;
1840 }
1841 
1842 
1843 
1844 
1845 /******************************************************************************
1846 	Module construction / destruction
1847 ******************************************************************************/
1848 
1849 static void snd_echo_free(struct snd_card *card)
1850 {
1851 	struct echoaudio *chip = card->private_data;
1852 
1853 	if (chip->comm_page)
1854 		rest_in_peace(chip);
1855 
1856 	if (chip->irq >= 0)
1857 		free_irq(chip->irq, chip);
1858 
1859 	/* release chip data */
1860 	free_firmware_cache(chip);
1861 }
1862 
1863 /* <--snd_echo_probe() */
1864 static int snd_echo_create(struct snd_card *card,
1865 			   struct pci_dev *pci)
1866 {
1867 	struct echoaudio *chip = card->private_data;
1868 	int err;
1869 	size_t sz;
1870 
1871 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1872 
1873 	err = pcim_enable_device(pci);
1874 	if (err < 0)
1875 		return err;
1876 	pci_set_master(pci);
1877 
1878 	/* Allocate chip if needed */
1879 	spin_lock_init(&chip->lock);
1880 	chip->card = card;
1881 	chip->pci = pci;
1882 	chip->irq = -1;
1883 	chip->opencount = 0;
1884 	mutex_init(&chip->mode_mutex);
1885 	chip->can_set_rate = 1;
1886 
1887 	/* PCI resource allocation */
1888 	err = pcim_request_all_regions(pci, ECHOCARD_NAME);
1889 	if (err < 0)
1890 		return err;
1891 
1892 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1893 	sz = pci_resource_len(pci, 0);
1894 	if (sz > PAGE_SIZE)
1895 		sz = PAGE_SIZE;		/* We map only the required part */
1896 
1897 	chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
1898 	if (!chip->dsp_registers) {
1899 		dev_err(chip->card->dev, "ioremap failed\n");
1900 		return -ENOMEM;
1901 	}
1902 
1903 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1904 			KBUILD_MODNAME, chip)) {
1905 		dev_err(chip->card->dev, "cannot grab irq\n");
1906 		return -EBUSY;
1907 	}
1908 	chip->irq = pci->irq;
1909 	card->sync_irq = chip->irq;
1910 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1911 		chip->pci, chip->irq, chip->pci->subsystem_device);
1912 
1913 	card->private_free = snd_echo_free;
1914 
1915 	/* Create the DSP comm page - this is the area of memory used for most
1916 	of the communication with the DSP, which accesses it via bus mastering */
1917 	chip->commpage_dma_buf =
1918 		snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
1919 				     sizeof(struct comm_page));
1920 	if (!chip->commpage_dma_buf)
1921 		return -ENOMEM;
1922 	chip->comm_page_phys = chip->commpage_dma_buf->addr;
1923 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1924 
1925 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1926 	if (err >= 0)
1927 		err = set_mixer_defaults(chip);
1928 	if (err < 0) {
1929 		dev_err(card->dev, "init_hw err=%d\n", err);
1930 		return err;
1931 	}
1932 
1933 	return 0;
1934 }
1935 
1936 /* constructor */
1937 static int __snd_echo_probe(struct pci_dev *pci,
1938 			    const struct pci_device_id *pci_id)
1939 {
1940 	static int dev;
1941 	struct snd_card *card;
1942 	struct echoaudio *chip;
1943 	char *dsp;
1944 	int err;
1945 
1946 	if (dev >= SNDRV_CARDS)
1947 		return -ENODEV;
1948 	if (!enable[dev]) {
1949 		dev++;
1950 		return -ENOENT;
1951 	}
1952 
1953 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1954 				sizeof(*chip), &card);
1955 	if (err < 0)
1956 		return err;
1957 	chip = card->private_data;
1958 
1959 	err = snd_echo_create(card, pci);
1960 	if (err < 0)
1961 		return err;
1962 
1963 	strscpy(card->driver, "Echo_" ECHOCARD_NAME);
1964 	strscpy(card->shortname, chip->card_name);
1965 
1966 	dsp = "56301";
1967 	if (pci_id->device == 0x3410)
1968 		dsp = "56361";
1969 
1970 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
1971 		card->shortname, pci_id->subdevice & 0x000f, dsp,
1972 		chip->dsp_registers_phys, chip->irq);
1973 
1974 	err = snd_echo_new_pcm(chip);
1975 	if (err < 0) {
1976 		dev_err(chip->card->dev, "new pcm error %d\n", err);
1977 		return err;
1978 	}
1979 
1980 #ifdef ECHOCARD_HAS_MIDI
1981 	if (chip->has_midi) {	/* Some Mia's do not have midi */
1982 		err = snd_echo_midi_create(card, chip);
1983 		if (err < 0) {
1984 			dev_err(chip->card->dev, "new midi error %d\n", err);
1985 			return err;
1986 		}
1987 	}
1988 #endif
1989 
1990 #ifdef ECHOCARD_HAS_VMIXER
1991 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
1992 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
1993 	if (err < 0)
1994 		return err;
1995 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1996 	err = snd_ctl_add(chip->card,
1997 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
1998 	if (err < 0)
1999 		return err;
2000 #endif
2001 #else /* ECHOCARD_HAS_VMIXER */
2002 	err = snd_ctl_add(chip->card,
2003 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2004 	if (err < 0)
2005 		return err;
2006 #endif /* ECHOCARD_HAS_VMIXER */
2007 
2008 #ifdef ECHOCARD_HAS_INPUT_GAIN
2009 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2010 	if (err < 0)
2011 		return err;
2012 #endif
2013 
2014 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2015 	if (!chip->hasnt_input_nominal_level) {
2016 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2017 		if (err < 0)
2018 			return err;
2019 	}
2020 #endif
2021 
2022 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2023 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2024 	if (err < 0)
2025 		return err;
2026 #endif
2027 
2028 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2029 	if (err < 0)
2030 		return err;
2031 
2032 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2033 	if (err < 0)
2034 		return err;
2035 
2036 #ifdef ECHOCARD_HAS_MONITOR
2037 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2038 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2039 	if (err < 0)
2040 		return err;
2041 #endif
2042 
2043 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2044 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2045 	if (err < 0)
2046 		return err;
2047 #endif
2048 
2049 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2050 	if (err < 0)
2051 		return err;
2052 
2053 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2054 	/* Creates a list of available digital modes */
2055 	chip->num_digital_modes = 0;
2056 	for (int i = 0; i < 6; i++)
2057 		if (chip->digital_modes & (1 << i))
2058 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2059 
2060 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2061 	if (err < 0)
2062 		return err;
2063 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2064 
2065 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2066 	/* Creates a list of available clock sources */
2067 	chip->num_clock_sources = 0;
2068 	for (int i = 0; i < 10; i++)
2069 		if (chip->input_clock_types & (1 << i))
2070 			chip->clock_source_list[chip->num_clock_sources++] = i;
2071 
2072 	if (chip->num_clock_sources > 1) {
2073 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2074 		err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2075 		if (err < 0)
2076 			return err;
2077 	}
2078 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2079 
2080 #ifdef ECHOCARD_HAS_DIGITAL_IO
2081 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2082 	if (err < 0)
2083 		return err;
2084 #endif
2085 
2086 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2087 	if (chip->has_phantom_power) {
2088 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2089 		if (err < 0)
2090 			return err;
2091 	}
2092 #endif
2093 
2094 	err = snd_card_register(card);
2095 	if (err < 0)
2096 		return err;
2097 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2098 
2099 	pci_set_drvdata(pci, chip);
2100 	dev++;
2101 	return 0;
2102 }
2103 
2104 static int snd_echo_probe(struct pci_dev *pci,
2105 			  const struct pci_device_id *pci_id)
2106 {
2107 	return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2108 }
2109 
2110 
2111 static int snd_echo_suspend(struct device *dev)
2112 {
2113 	struct echoaudio *chip = dev_get_drvdata(dev);
2114 
2115 #ifdef ECHOCARD_HAS_MIDI
2116 	/* This call can sleep */
2117 	if (chip->midi_out)
2118 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2119 #endif
2120 	scoped_guard(spinlock_irq, &chip->lock) {
2121 		if (wait_handshake(chip))
2122 			return -EIO;
2123 		clear_handshake(chip);
2124 		if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0)
2125 			return -EIO;
2126 	}
2127 
2128 	chip->dsp_code = NULL;
2129 	free_irq(chip->irq, chip);
2130 	chip->irq = -1;
2131 	chip->card->sync_irq = -1;
2132 	return 0;
2133 }
2134 
2135 
2136 
2137 static int snd_echo_resume(struct device *dev)
2138 {
2139 	struct pci_dev *pci = to_pci_dev(dev);
2140 	struct echoaudio *chip = dev_get_drvdata(dev);
2141 	struct comm_page *commpage, *commpage_bak;
2142 	u32 pipe_alloc_mask;
2143 	int err;
2144 
2145 	commpage = chip->comm_page;
2146 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2147 	if (commpage_bak == NULL)
2148 		return -ENOMEM;
2149 
2150 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2151 	if (err < 0) {
2152 		kfree(commpage_bak);
2153 		dev_err(dev, "resume init_hw err=%d\n", err);
2154 		return err;
2155 	}
2156 
2157 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2158 	 * restore_dsp_settings() fails.
2159 	 */
2160 	pipe_alloc_mask = chip->pipe_alloc_mask;
2161 	chip->pipe_alloc_mask = 0;
2162 	err = restore_dsp_rettings(chip);
2163 	chip->pipe_alloc_mask = pipe_alloc_mask;
2164 	if (err < 0) {
2165 		kfree(commpage_bak);
2166 		return err;
2167 	}
2168 
2169 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2170 		sizeof(commpage->audio_format));
2171 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2172 		sizeof(commpage->sglist_addr));
2173 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2174 		sizeof(commpage->midi_output));
2175 	kfree(commpage_bak);
2176 
2177 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2178 			KBUILD_MODNAME, chip)) {
2179 		dev_err(chip->card->dev, "cannot grab irq\n");
2180 		return -EBUSY;
2181 	}
2182 	chip->irq = pci->irq;
2183 	chip->card->sync_irq = chip->irq;
2184 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2185 
2186 #ifdef ECHOCARD_HAS_MIDI
2187 	if (chip->midi_input_enabled)
2188 		enable_midi_input(chip, true);
2189 	if (chip->midi_out)
2190 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2191 #endif
2192 
2193 	return 0;
2194 }
2195 
2196 static DEFINE_SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2197 
2198 /******************************************************************************
2199 	Everything starts and ends here
2200 ******************************************************************************/
2201 
2202 /* pci_driver definition */
2203 static struct pci_driver echo_driver = {
2204 	.name = KBUILD_MODNAME,
2205 	.id_table = snd_echo_ids,
2206 	.probe = snd_echo_probe,
2207 	.driver = {
2208 		.pm = &snd_echo_pm,
2209 	},
2210 };
2211 
2212 module_pci_driver(echo_driver);
2213