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