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