xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision 16ccca11088c1bdd9311a2c630b453541305c48a)
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 				       snd_dma_pci_data(chip->pci),
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 	err = snd_pcm_lib_malloc_pages(substream,
551 				       params_buffer_bytes(hw_params));
552 	if (err < 0) {
553 		dev_err(chip->card->dev, "malloc_pages err=%d\n", err);
554 		spin_lock_irq(&chip->lock);
555 		free_pipes(chip, pipe);
556 		spin_unlock_irq(&chip->lock);
557 		pipe->index = -1;
558 		return err;
559 	}
560 
561 	sglist_init(chip, pipe);
562 	edge = PAGE_SIZE;
563 	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
564 	     per++) {
565 		rest = params_period_bytes(hw_params);
566 		if (offs + rest > params_buffer_bytes(hw_params))
567 			rest = params_buffer_bytes(hw_params) - offs;
568 		while (rest) {
569 			dma_addr_t addr;
570 			addr = snd_pcm_sgbuf_get_addr(substream, offs);
571 			if (rest <= edge - offs) {
572 				sglist_add_mapping(chip, pipe, addr, rest);
573 				sglist_add_irq(chip, pipe);
574 				offs += rest;
575 				rest = 0;
576 			} else {
577 				sglist_add_mapping(chip, pipe, addr,
578 						   edge - offs);
579 				rest -= edge - offs;
580 				offs = edge;
581 			}
582 			if (offs == edge) {
583 				edge += PAGE_SIZE;
584 				page++;
585 			}
586 		}
587 	}
588 
589 	/* Close the ring buffer */
590 	sglist_wrap(chip, pipe);
591 
592 	/* This stuff is used by the irq handler, so it must be
593 	 * initialized before chip->substream
594 	 */
595 	chip->last_period[pipe_index] = 0;
596 	pipe->last_counter = 0;
597 	pipe->position = 0;
598 	smp_wmb();
599 	chip->substream[pipe_index] = substream;
600 	chip->rate_set = 1;
601 	spin_lock_irq(&chip->lock);
602 	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
603 	spin_unlock_irq(&chip->lock);
604 	return 0;
605 }
606 
607 
608 
609 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
610 				   struct snd_pcm_hw_params *hw_params)
611 {
612 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
613 
614 	return init_engine(substream, hw_params, px_analog_in(chip) +
615 			substream->number, params_channels(hw_params));
616 }
617 
618 
619 
620 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
621 				    struct snd_pcm_hw_params *hw_params)
622 {
623 	return init_engine(substream, hw_params, substream->number,
624 			   params_channels(hw_params));
625 }
626 
627 
628 
629 #ifdef ECHOCARD_HAS_DIGITAL_IO
630 
631 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
632 				    struct snd_pcm_hw_params *hw_params)
633 {
634 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
635 
636 	return init_engine(substream, hw_params, px_digital_in(chip) +
637 			substream->number, params_channels(hw_params));
638 }
639 
640 
641 
642 #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
643 static int pcm_digital_out_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_out(chip) +
649 			substream->number, params_channels(hw_params));
650 }
651 #endif /* !ECHOCARD_HAS_VMIXER */
652 
653 #endif /* ECHOCARD_HAS_DIGITAL_IO */
654 
655 
656 
657 static int pcm_hw_free(struct snd_pcm_substream *substream)
658 {
659 	struct echoaudio *chip;
660 	struct audiopipe *pipe;
661 
662 	chip = snd_pcm_substream_chip(substream);
663 	pipe = (struct audiopipe *) substream->runtime->private_data;
664 
665 	spin_lock_irq(&chip->lock);
666 	if (pipe->index >= 0) {
667 		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
668 		free_pipes(chip, pipe);
669 		chip->substream[pipe->index] = NULL;
670 		pipe->index = -1;
671 	}
672 	spin_unlock_irq(&chip->lock);
673 
674 	snd_pcm_lib_free_pages(substream);
675 	return 0;
676 }
677 
678 
679 
680 static int pcm_prepare(struct snd_pcm_substream *substream)
681 {
682 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
683 	struct snd_pcm_runtime *runtime = substream->runtime;
684 	struct audioformat format;
685 	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
686 
687 	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
688 		runtime->rate, runtime->format, runtime->channels);
689 	format.interleave = runtime->channels;
690 	format.data_are_bigendian = 0;
691 	format.mono_to_stereo = 0;
692 	switch (runtime->format) {
693 	case SNDRV_PCM_FORMAT_U8:
694 		format.bits_per_sample = 8;
695 		break;
696 	case SNDRV_PCM_FORMAT_S16_LE:
697 		format.bits_per_sample = 16;
698 		break;
699 	case SNDRV_PCM_FORMAT_S24_3LE:
700 		format.bits_per_sample = 24;
701 		break;
702 	case SNDRV_PCM_FORMAT_S32_BE:
703 		format.data_are_bigendian = 1;
704 		/* fall through */
705 	case SNDRV_PCM_FORMAT_S32_LE:
706 		format.bits_per_sample = 32;
707 		break;
708 	default:
709 		dev_err(chip->card->dev,
710 			"Prepare error: unsupported format %d\n",
711 			runtime->format);
712 		return -EINVAL;
713 	}
714 
715 	if (snd_BUG_ON(pipe_index >= px_num(chip)))
716 		return -EINVAL;
717 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
718 		return -EINVAL;
719 	set_audio_format(chip, pipe_index, &format);
720 	return 0;
721 }
722 
723 
724 
725 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
726 {
727 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
728 	struct audiopipe *pipe;
729 	int i, err;
730 	u32 channelmask = 0;
731 	struct snd_pcm_substream *s;
732 
733 	snd_pcm_group_for_each_entry(s, substream) {
734 		for (i = 0; i < DSP_MAXPIPES; i++) {
735 			if (s == chip->substream[i]) {
736 				channelmask |= 1 << i;
737 				snd_pcm_trigger_done(s, substream);
738 			}
739 		}
740 	}
741 
742 	spin_lock(&chip->lock);
743 	switch (cmd) {
744 	case SNDRV_PCM_TRIGGER_RESUME:
745 	case SNDRV_PCM_TRIGGER_START:
746 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
747 		for (i = 0; i < DSP_MAXPIPES; i++) {
748 			if (channelmask & (1 << i)) {
749 				pipe = chip->substream[i]->runtime->private_data;
750 				switch (pipe->state) {
751 				case PIPE_STATE_STOPPED:
752 					chip->last_period[i] = 0;
753 					pipe->last_counter = 0;
754 					pipe->position = 0;
755 					*pipe->dma_counter = 0;
756 					/* fall through */
757 				case PIPE_STATE_PAUSED:
758 					pipe->state = PIPE_STATE_STARTED;
759 					break;
760 				case PIPE_STATE_STARTED:
761 					break;
762 				}
763 			}
764 		}
765 		err = start_transport(chip, channelmask,
766 				      chip->pipe_cyclic_mask);
767 		break;
768 	case SNDRV_PCM_TRIGGER_SUSPEND:
769 	case SNDRV_PCM_TRIGGER_STOP:
770 		for (i = 0; i < DSP_MAXPIPES; i++) {
771 			if (channelmask & (1 << i)) {
772 				pipe = chip->substream[i]->runtime->private_data;
773 				pipe->state = PIPE_STATE_STOPPED;
774 			}
775 		}
776 		err = stop_transport(chip, channelmask);
777 		break;
778 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
779 		for (i = 0; i < DSP_MAXPIPES; i++) {
780 			if (channelmask & (1 << i)) {
781 				pipe = chip->substream[i]->runtime->private_data;
782 				pipe->state = PIPE_STATE_PAUSED;
783 			}
784 		}
785 		err = pause_transport(chip, channelmask);
786 		break;
787 	default:
788 		err = -EINVAL;
789 	}
790 	spin_unlock(&chip->lock);
791 	return err;
792 }
793 
794 
795 
796 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
797 {
798 	struct snd_pcm_runtime *runtime = substream->runtime;
799 	struct audiopipe *pipe = runtime->private_data;
800 	size_t cnt, bufsize, pos;
801 
802 	cnt = le32_to_cpu(*pipe->dma_counter);
803 	pipe->position += cnt - pipe->last_counter;
804 	pipe->last_counter = cnt;
805 	bufsize = substream->runtime->buffer_size;
806 	pos = bytes_to_frames(substream->runtime, pipe->position);
807 
808 	while (pos >= bufsize) {
809 		pipe->position -= frames_to_bytes(substream->runtime, bufsize);
810 		pos -= bufsize;
811 	}
812 	return pos;
813 }
814 
815 
816 
817 /* pcm *_ops structures */
818 static const struct snd_pcm_ops analog_playback_ops = {
819 	.open = pcm_analog_out_open,
820 	.close = pcm_close,
821 	.ioctl = snd_pcm_lib_ioctl,
822 	.hw_params = pcm_analog_out_hw_params,
823 	.hw_free = pcm_hw_free,
824 	.prepare = pcm_prepare,
825 	.trigger = pcm_trigger,
826 	.pointer = pcm_pointer,
827 };
828 static const struct snd_pcm_ops analog_capture_ops = {
829 	.open = pcm_analog_in_open,
830 	.close = pcm_close,
831 	.ioctl = snd_pcm_lib_ioctl,
832 	.hw_params = pcm_analog_in_hw_params,
833 	.hw_free = pcm_hw_free,
834 	.prepare = pcm_prepare,
835 	.trigger = pcm_trigger,
836 	.pointer = pcm_pointer,
837 };
838 #ifdef ECHOCARD_HAS_DIGITAL_IO
839 #ifndef ECHOCARD_HAS_VMIXER
840 static const struct snd_pcm_ops digital_playback_ops = {
841 	.open = pcm_digital_out_open,
842 	.close = pcm_close,
843 	.ioctl = snd_pcm_lib_ioctl,
844 	.hw_params = pcm_digital_out_hw_params,
845 	.hw_free = pcm_hw_free,
846 	.prepare = pcm_prepare,
847 	.trigger = pcm_trigger,
848 	.pointer = pcm_pointer,
849 };
850 #endif /* !ECHOCARD_HAS_VMIXER */
851 static const struct snd_pcm_ops digital_capture_ops = {
852 	.open = pcm_digital_in_open,
853 	.close = pcm_close,
854 	.ioctl = snd_pcm_lib_ioctl,
855 	.hw_params = pcm_digital_in_hw_params,
856 	.hw_free = pcm_hw_free,
857 	.prepare = pcm_prepare,
858 	.trigger = pcm_trigger,
859 	.pointer = pcm_pointer,
860 };
861 #endif /* ECHOCARD_HAS_DIGITAL_IO */
862 
863 
864 
865 /* Preallocate memory only for the first substream because it's the most
866  * used one
867  */
868 static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
869 {
870 	struct snd_pcm_substream *ss;
871 	int stream;
872 
873 	for (stream = 0; stream < 2; stream++)
874 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
875 			snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
876 						      dev,
877 						      ss->number ? 0 : 128<<10,
878 						      256<<10);
879 
880 	return 0;
881 }
882 
883 
884 
885 /*<--snd_echo_probe() */
886 static int snd_echo_new_pcm(struct echoaudio *chip)
887 {
888 	struct snd_pcm *pcm;
889 	int err;
890 
891 #ifdef ECHOCARD_HAS_VMIXER
892 	/* This card has a Vmixer, that is there is no direct mapping from PCM
893 	streams to physical outputs. The user can mix the streams as he wishes
894 	via control interface and it's possible to send any stream to any
895 	output, thus it makes no sense to keep analog and digital outputs
896 	separated */
897 
898 	/* PCM#0 Virtual outputs and analog inputs */
899 	if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
900 				num_analog_busses_in(chip), &pcm)) < 0)
901 		return err;
902 	pcm->private_data = chip;
903 	chip->analog_pcm = pcm;
904 	strcpy(pcm->name, chip->card->shortname);
905 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
906 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
907 	if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
908 		return err;
909 
910 #ifdef ECHOCARD_HAS_DIGITAL_IO
911 	/* PCM#1 Digital inputs, no outputs */
912 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
913 			       num_digital_busses_in(chip), &pcm)) < 0)
914 		return err;
915 	pcm->private_data = chip;
916 	chip->digital_pcm = pcm;
917 	strcpy(pcm->name, chip->card->shortname);
918 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
919 	if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
920 		return err;
921 #endif /* ECHOCARD_HAS_DIGITAL_IO */
922 
923 #else /* ECHOCARD_HAS_VMIXER */
924 
925 	/* The card can manage substreams formed by analog and digital channels
926 	at the same time, but I prefer to keep analog and digital channels
927 	separated, because that mixed thing is confusing and useless. So we
928 	register two PCM devices: */
929 
930 	/* PCM#0 Analog i/o */
931 	if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
932 			       num_analog_busses_out(chip),
933 			       num_analog_busses_in(chip), &pcm)) < 0)
934 		return err;
935 	pcm->private_data = chip;
936 	chip->analog_pcm = pcm;
937 	strcpy(pcm->name, chip->card->shortname);
938 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
939 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
940 	if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
941 		return err;
942 
943 #ifdef ECHOCARD_HAS_DIGITAL_IO
944 	/* PCM#1 Digital i/o */
945 	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
946 			       num_digital_busses_out(chip),
947 			       num_digital_busses_in(chip), &pcm)) < 0)
948 		return err;
949 	pcm->private_data = chip;
950 	chip->digital_pcm = pcm;
951 	strcpy(pcm->name, chip->card->shortname);
952 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
953 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
954 	if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
955 		return err;
956 #endif /* ECHOCARD_HAS_DIGITAL_IO */
957 
958 #endif /* ECHOCARD_HAS_VMIXER */
959 
960 	return 0;
961 }
962 
963 
964 
965 
966 /******************************************************************************
967 	Control interface
968 ******************************************************************************/
969 
970 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
971 
972 /******************* PCM output volume *******************/
973 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
974 				     struct snd_ctl_elem_info *uinfo)
975 {
976 	struct echoaudio *chip;
977 
978 	chip = snd_kcontrol_chip(kcontrol);
979 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
980 	uinfo->count = num_busses_out(chip);
981 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
982 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
983 	return 0;
984 }
985 
986 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
987 				    struct snd_ctl_elem_value *ucontrol)
988 {
989 	struct echoaudio *chip;
990 	int c;
991 
992 	chip = snd_kcontrol_chip(kcontrol);
993 	for (c = 0; c < num_busses_out(chip); c++)
994 		ucontrol->value.integer.value[c] = chip->output_gain[c];
995 	return 0;
996 }
997 
998 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
999 				    struct snd_ctl_elem_value *ucontrol)
1000 {
1001 	struct echoaudio *chip;
1002 	int c, changed, gain;
1003 
1004 	changed = 0;
1005 	chip = snd_kcontrol_chip(kcontrol);
1006 	spin_lock_irq(&chip->lock);
1007 	for (c = 0; c < num_busses_out(chip); c++) {
1008 		gain = ucontrol->value.integer.value[c];
1009 		/* Ignore out of range values */
1010 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1011 			continue;
1012 		if (chip->output_gain[c] != gain) {
1013 			set_output_gain(chip, c, gain);
1014 			changed = 1;
1015 		}
1016 	}
1017 	if (changed)
1018 		update_output_line_level(chip);
1019 	spin_unlock_irq(&chip->lock);
1020 	return changed;
1021 }
1022 
1023 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1024 /* On the Mia this one controls the line-out volume */
1025 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1026 	.name = "Line Playback Volume",
1027 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1028 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1029 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1030 	.info = snd_echo_output_gain_info,
1031 	.get = snd_echo_output_gain_get,
1032 	.put = snd_echo_output_gain_put,
1033 	.tlv = {.p = db_scale_output_gain},
1034 };
1035 #else
1036 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1037 	.name = "PCM Playback Volume",
1038 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1039 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1040 	.info = snd_echo_output_gain_info,
1041 	.get = snd_echo_output_gain_get,
1042 	.put = snd_echo_output_gain_put,
1043 	.tlv = {.p = db_scale_output_gain},
1044 };
1045 #endif
1046 
1047 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1048 
1049 
1050 
1051 #ifdef ECHOCARD_HAS_INPUT_GAIN
1052 
1053 /******************* Analog input volume *******************/
1054 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1055 				    struct snd_ctl_elem_info *uinfo)
1056 {
1057 	struct echoaudio *chip;
1058 
1059 	chip = snd_kcontrol_chip(kcontrol);
1060 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1061 	uinfo->count = num_analog_busses_in(chip);
1062 	uinfo->value.integer.min = ECHOGAIN_MININP;
1063 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1064 	return 0;
1065 }
1066 
1067 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1068 				   struct snd_ctl_elem_value *ucontrol)
1069 {
1070 	struct echoaudio *chip;
1071 	int c;
1072 
1073 	chip = snd_kcontrol_chip(kcontrol);
1074 	for (c = 0; c < num_analog_busses_in(chip); c++)
1075 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1076 	return 0;
1077 }
1078 
1079 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1080 				   struct snd_ctl_elem_value *ucontrol)
1081 {
1082 	struct echoaudio *chip;
1083 	int c, gain, changed;
1084 
1085 	changed = 0;
1086 	chip = snd_kcontrol_chip(kcontrol);
1087 	spin_lock_irq(&chip->lock);
1088 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1089 		gain = ucontrol->value.integer.value[c];
1090 		/* Ignore out of range values */
1091 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1092 			continue;
1093 		if (chip->input_gain[c] != gain) {
1094 			set_input_gain(chip, c, gain);
1095 			changed = 1;
1096 		}
1097 	}
1098 	if (changed)
1099 		update_input_line_level(chip);
1100 	spin_unlock_irq(&chip->lock);
1101 	return changed;
1102 }
1103 
1104 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1105 
1106 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1107 	.name = "Line Capture Volume",
1108 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1109 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1110 	.info = snd_echo_input_gain_info,
1111 	.get = snd_echo_input_gain_get,
1112 	.put = snd_echo_input_gain_put,
1113 	.tlv = {.p = db_scale_input_gain},
1114 };
1115 
1116 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1117 
1118 
1119 
1120 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1121 
1122 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1123 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1124 					 struct snd_ctl_elem_info *uinfo)
1125 {
1126 	struct echoaudio *chip;
1127 
1128 	chip = snd_kcontrol_chip(kcontrol);
1129 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1130 	uinfo->count = num_analog_busses_out(chip);
1131 	uinfo->value.integer.min = 0;
1132 	uinfo->value.integer.max = 1;
1133 	return 0;
1134 }
1135 
1136 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1137 				       struct snd_ctl_elem_value *ucontrol)
1138 {
1139 	struct echoaudio *chip;
1140 	int c;
1141 
1142 	chip = snd_kcontrol_chip(kcontrol);
1143 	for (c = 0; c < num_analog_busses_out(chip); c++)
1144 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1145 	return 0;
1146 }
1147 
1148 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1149 				       struct snd_ctl_elem_value *ucontrol)
1150 {
1151 	struct echoaudio *chip;
1152 	int c, changed;
1153 
1154 	changed = 0;
1155 	chip = snd_kcontrol_chip(kcontrol);
1156 	spin_lock_irq(&chip->lock);
1157 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1158 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1159 			set_nominal_level(chip, c,
1160 					  ucontrol->value.integer.value[c]);
1161 			changed = 1;
1162 		}
1163 	}
1164 	if (changed)
1165 		update_output_line_level(chip);
1166 	spin_unlock_irq(&chip->lock);
1167 	return changed;
1168 }
1169 
1170 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1171 	.name = "Line Playback Switch (-10dBV)",
1172 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1173 	.info = snd_echo_output_nominal_info,
1174 	.get = snd_echo_output_nominal_get,
1175 	.put = snd_echo_output_nominal_put,
1176 };
1177 
1178 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1179 
1180 
1181 
1182 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1183 
1184 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1185 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1186 				       struct snd_ctl_elem_info *uinfo)
1187 {
1188 	struct echoaudio *chip;
1189 
1190 	chip = snd_kcontrol_chip(kcontrol);
1191 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1192 	uinfo->count = num_analog_busses_in(chip);
1193 	uinfo->value.integer.min = 0;
1194 	uinfo->value.integer.max = 1;
1195 	return 0;
1196 }
1197 
1198 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1199 				      struct snd_ctl_elem_value *ucontrol)
1200 {
1201 	struct echoaudio *chip;
1202 	int c;
1203 
1204 	chip = snd_kcontrol_chip(kcontrol);
1205 	for (c = 0; c < num_analog_busses_in(chip); c++)
1206 		ucontrol->value.integer.value[c] =
1207 			chip->nominal_level[bx_analog_in(chip) + c];
1208 	return 0;
1209 }
1210 
1211 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1212 				      struct snd_ctl_elem_value *ucontrol)
1213 {
1214 	struct echoaudio *chip;
1215 	int c, changed;
1216 
1217 	changed = 0;
1218 	chip = snd_kcontrol_chip(kcontrol);
1219 	spin_lock_irq(&chip->lock);
1220 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1221 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1222 		    ucontrol->value.integer.value[c]) {
1223 			set_nominal_level(chip, bx_analog_in(chip) + c,
1224 					  ucontrol->value.integer.value[c]);
1225 			changed = 1;
1226 		}
1227 	}
1228 	if (changed)
1229 		update_output_line_level(chip);	/* "Output" is not a mistake
1230 						 * here.
1231 						 */
1232 	spin_unlock_irq(&chip->lock);
1233 	return changed;
1234 }
1235 
1236 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1237 	.name = "Line Capture Switch (-10dBV)",
1238 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1239 	.info = snd_echo_input_nominal_info,
1240 	.get = snd_echo_input_nominal_get,
1241 	.put = snd_echo_input_nominal_put,
1242 };
1243 
1244 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1245 
1246 
1247 
1248 #ifdef ECHOCARD_HAS_MONITOR
1249 
1250 /******************* Monitor mixer *******************/
1251 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1252 			       struct snd_ctl_elem_info *uinfo)
1253 {
1254 	struct echoaudio *chip;
1255 
1256 	chip = snd_kcontrol_chip(kcontrol);
1257 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1258 	uinfo->count = 1;
1259 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1260 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1261 	uinfo->dimen.d[0] = num_busses_out(chip);
1262 	uinfo->dimen.d[1] = num_busses_in(chip);
1263 	return 0;
1264 }
1265 
1266 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1267 			      struct snd_ctl_elem_value *ucontrol)
1268 {
1269 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1270 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1271 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1272 
1273 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1274 		return -EINVAL;
1275 
1276 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1277 	return 0;
1278 }
1279 
1280 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1281 			      struct snd_ctl_elem_value *ucontrol)
1282 {
1283 	struct echoaudio *chip;
1284 	int changed,  gain;
1285 	unsigned int out, in;
1286 
1287 	changed = 0;
1288 	chip = snd_kcontrol_chip(kcontrol);
1289 	out = ucontrol->id.index / num_busses_in(chip);
1290 	in = ucontrol->id.index % num_busses_in(chip);
1291 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1292 		return -EINVAL;
1293 	gain = ucontrol->value.integer.value[0];
1294 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1295 		return -EINVAL;
1296 	if (chip->monitor_gain[out][in] != gain) {
1297 		spin_lock_irq(&chip->lock);
1298 		set_monitor_gain(chip, out, in, gain);
1299 		update_output_line_level(chip);
1300 		spin_unlock_irq(&chip->lock);
1301 		changed = 1;
1302 	}
1303 	return changed;
1304 }
1305 
1306 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1307 	.name = "Monitor Mixer Volume",
1308 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1309 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1310 	.info = snd_echo_mixer_info,
1311 	.get = snd_echo_mixer_get,
1312 	.put = snd_echo_mixer_put,
1313 	.tlv = {.p = db_scale_output_gain},
1314 };
1315 
1316 #endif /* ECHOCARD_HAS_MONITOR */
1317 
1318 
1319 
1320 #ifdef ECHOCARD_HAS_VMIXER
1321 
1322 /******************* Vmixer *******************/
1323 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1324 				struct snd_ctl_elem_info *uinfo)
1325 {
1326 	struct echoaudio *chip;
1327 
1328 	chip = snd_kcontrol_chip(kcontrol);
1329 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1330 	uinfo->count = 1;
1331 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1332 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1333 	uinfo->dimen.d[0] = num_busses_out(chip);
1334 	uinfo->dimen.d[1] = num_pipes_out(chip);
1335 	return 0;
1336 }
1337 
1338 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1339 			       struct snd_ctl_elem_value *ucontrol)
1340 {
1341 	struct echoaudio *chip;
1342 
1343 	chip = snd_kcontrol_chip(kcontrol);
1344 	ucontrol->value.integer.value[0] =
1345 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1346 			[ucontrol->id.index % num_pipes_out(chip)];
1347 	return 0;
1348 }
1349 
1350 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1351 			       struct snd_ctl_elem_value *ucontrol)
1352 {
1353 	struct echoaudio *chip;
1354 	int gain, changed;
1355 	short vch, out;
1356 
1357 	changed = 0;
1358 	chip = snd_kcontrol_chip(kcontrol);
1359 	out = ucontrol->id.index / num_pipes_out(chip);
1360 	vch = ucontrol->id.index % num_pipes_out(chip);
1361 	gain = ucontrol->value.integer.value[0];
1362 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1363 		return -EINVAL;
1364 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1365 		spin_lock_irq(&chip->lock);
1366 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1367 		update_vmixer_level(chip);
1368 		spin_unlock_irq(&chip->lock);
1369 		changed = 1;
1370 	}
1371 	return changed;
1372 }
1373 
1374 static struct snd_kcontrol_new snd_echo_vmixer = {
1375 	.name = "VMixer Volume",
1376 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1377 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1378 	.info = snd_echo_vmixer_info,
1379 	.get = snd_echo_vmixer_get,
1380 	.put = snd_echo_vmixer_put,
1381 	.tlv = {.p = db_scale_output_gain},
1382 };
1383 
1384 #endif /* ECHOCARD_HAS_VMIXER */
1385 
1386 
1387 
1388 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1389 
1390 /******************* Digital mode switch *******************/
1391 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1392 				      struct snd_ctl_elem_info *uinfo)
1393 {
1394 	static const char * const names[4] = {
1395 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1396 		"S/PDIF Cdrom"
1397 	};
1398 	struct echoaudio *chip;
1399 
1400 	chip = snd_kcontrol_chip(kcontrol);
1401 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1402 }
1403 
1404 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1405 				     struct snd_ctl_elem_value *ucontrol)
1406 {
1407 	struct echoaudio *chip;
1408 	int i, mode;
1409 
1410 	chip = snd_kcontrol_chip(kcontrol);
1411 	mode = chip->digital_mode;
1412 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1413 		if (mode == chip->digital_mode_list[i]) {
1414 			ucontrol->value.enumerated.item[0] = i;
1415 			break;
1416 		}
1417 	return 0;
1418 }
1419 
1420 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1421 				     struct snd_ctl_elem_value *ucontrol)
1422 {
1423 	struct echoaudio *chip;
1424 	int changed;
1425 	unsigned short emode, dmode;
1426 
1427 	changed = 0;
1428 	chip = snd_kcontrol_chip(kcontrol);
1429 
1430 	emode = ucontrol->value.enumerated.item[0];
1431 	if (emode >= chip->num_digital_modes)
1432 		return -EINVAL;
1433 	dmode = chip->digital_mode_list[emode];
1434 
1435 	if (dmode != chip->digital_mode) {
1436 		/* mode_mutex is required to make this operation atomic wrt
1437 		pcm_digital_*_open() and set_input_clock() functions. */
1438 		mutex_lock(&chip->mode_mutex);
1439 
1440 		/* Do not allow the user to change the digital mode when a pcm
1441 		device is open because it also changes the number of channels
1442 		and the allowed sample rates */
1443 		if (atomic_read(&chip->opencount)) {
1444 			changed = -EAGAIN;
1445 		} else {
1446 			changed = set_digital_mode(chip, dmode);
1447 			/* If we had to change the clock source, report it */
1448 			if (changed > 0 && chip->clock_src_ctl) {
1449 				snd_ctl_notify(chip->card,
1450 					       SNDRV_CTL_EVENT_MASK_VALUE,
1451 					       &chip->clock_src_ctl->id);
1452 				dev_dbg(chip->card->dev,
1453 					"SDM() =%d\n", changed);
1454 			}
1455 			if (changed >= 0)
1456 				changed = 1;	/* No errors */
1457 		}
1458 		mutex_unlock(&chip->mode_mutex);
1459 	}
1460 	return changed;
1461 }
1462 
1463 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1464 	.name = "Digital mode Switch",
1465 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1466 	.info = snd_echo_digital_mode_info,
1467 	.get = snd_echo_digital_mode_get,
1468 	.put = snd_echo_digital_mode_put,
1469 };
1470 
1471 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1472 
1473 
1474 
1475 #ifdef ECHOCARD_HAS_DIGITAL_IO
1476 
1477 /******************* S/PDIF mode switch *******************/
1478 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1479 				    struct snd_ctl_elem_info *uinfo)
1480 {
1481 	static const char * const names[2] = {"Consumer", "Professional"};
1482 
1483 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1484 }
1485 
1486 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1487 				   struct snd_ctl_elem_value *ucontrol)
1488 {
1489 	struct echoaudio *chip;
1490 
1491 	chip = snd_kcontrol_chip(kcontrol);
1492 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1493 	return 0;
1494 }
1495 
1496 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1497 				   struct snd_ctl_elem_value *ucontrol)
1498 {
1499 	struct echoaudio *chip;
1500 	int mode;
1501 
1502 	chip = snd_kcontrol_chip(kcontrol);
1503 	mode = !!ucontrol->value.enumerated.item[0];
1504 	if (mode != chip->professional_spdif) {
1505 		spin_lock_irq(&chip->lock);
1506 		set_professional_spdif(chip, mode);
1507 		spin_unlock_irq(&chip->lock);
1508 		return 1;
1509 	}
1510 	return 0;
1511 }
1512 
1513 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1514 	.name = "S/PDIF mode Switch",
1515 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1516 	.info = snd_echo_spdif_mode_info,
1517 	.get = snd_echo_spdif_mode_get,
1518 	.put = snd_echo_spdif_mode_put,
1519 };
1520 
1521 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1522 
1523 
1524 
1525 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1526 
1527 /******************* Select input clock source *******************/
1528 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1529 				      struct snd_ctl_elem_info *uinfo)
1530 {
1531 	static const char * const names[8] = {
1532 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1533 		"ESync96", "MTC"
1534 	};
1535 	struct echoaudio *chip;
1536 
1537 	chip = snd_kcontrol_chip(kcontrol);
1538 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1539 }
1540 
1541 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1542 				     struct snd_ctl_elem_value *ucontrol)
1543 {
1544 	struct echoaudio *chip;
1545 	int i, clock;
1546 
1547 	chip = snd_kcontrol_chip(kcontrol);
1548 	clock = chip->input_clock;
1549 
1550 	for (i = 0; i < chip->num_clock_sources; i++)
1551 		if (clock == chip->clock_source_list[i])
1552 			ucontrol->value.enumerated.item[0] = i;
1553 
1554 	return 0;
1555 }
1556 
1557 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1558 				     struct snd_ctl_elem_value *ucontrol)
1559 {
1560 	struct echoaudio *chip;
1561 	int changed;
1562 	unsigned int eclock, dclock;
1563 
1564 	changed = 0;
1565 	chip = snd_kcontrol_chip(kcontrol);
1566 	eclock = ucontrol->value.enumerated.item[0];
1567 	if (eclock >= chip->input_clock_types)
1568 		return -EINVAL;
1569 	dclock = chip->clock_source_list[eclock];
1570 	if (chip->input_clock != dclock) {
1571 		mutex_lock(&chip->mode_mutex);
1572 		spin_lock_irq(&chip->lock);
1573 		if ((changed = set_input_clock(chip, dclock)) == 0)
1574 			changed = 1;	/* no errors */
1575 		spin_unlock_irq(&chip->lock);
1576 		mutex_unlock(&chip->mode_mutex);
1577 	}
1578 
1579 	if (changed < 0)
1580 		dev_dbg(chip->card->dev,
1581 			"seticlk val%d err 0x%x\n", dclock, changed);
1582 
1583 	return changed;
1584 }
1585 
1586 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1587 	.name = "Sample Clock Source",
1588 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1589 	.info = snd_echo_clock_source_info,
1590 	.get = snd_echo_clock_source_get,
1591 	.put = snd_echo_clock_source_put,
1592 };
1593 
1594 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1595 
1596 
1597 
1598 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1599 
1600 /******************* Phantom power switch *******************/
1601 #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1602 
1603 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1604 				      struct snd_ctl_elem_value *ucontrol)
1605 {
1606 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1607 
1608 	ucontrol->value.integer.value[0] = chip->phantom_power;
1609 	return 0;
1610 }
1611 
1612 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1613 				      struct snd_ctl_elem_value *ucontrol)
1614 {
1615 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1616 	int power, changed = 0;
1617 
1618 	power = !!ucontrol->value.integer.value[0];
1619 	if (chip->phantom_power != power) {
1620 		spin_lock_irq(&chip->lock);
1621 		changed = set_phantom_power(chip, power);
1622 		spin_unlock_irq(&chip->lock);
1623 		if (changed == 0)
1624 			changed = 1;	/* no errors */
1625 	}
1626 	return changed;
1627 }
1628 
1629 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1630 	.name = "Phantom power Switch",
1631 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1632 	.info = snd_echo_phantom_power_info,
1633 	.get = snd_echo_phantom_power_get,
1634 	.put = snd_echo_phantom_power_put,
1635 };
1636 
1637 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1638 
1639 
1640 
1641 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1642 
1643 /******************* Digital input automute switch *******************/
1644 #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1645 
1646 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1647 				 struct snd_ctl_elem_value *ucontrol)
1648 {
1649 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1650 
1651 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1652 	return 0;
1653 }
1654 
1655 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1656 				 struct snd_ctl_elem_value *ucontrol)
1657 {
1658 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1659 	int automute, changed = 0;
1660 
1661 	automute = !!ucontrol->value.integer.value[0];
1662 	if (chip->digital_in_automute != automute) {
1663 		spin_lock_irq(&chip->lock);
1664 		changed = set_input_auto_mute(chip, automute);
1665 		spin_unlock_irq(&chip->lock);
1666 		if (changed == 0)
1667 			changed = 1;	/* no errors */
1668 	}
1669 	return changed;
1670 }
1671 
1672 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1673 	.name = "Digital Capture Switch (automute)",
1674 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1675 	.info = snd_echo_automute_info,
1676 	.get = snd_echo_automute_get,
1677 	.put = snd_echo_automute_put,
1678 };
1679 
1680 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1681 
1682 
1683 
1684 /******************* VU-meters switch *******************/
1685 #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1686 
1687 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1688 					struct snd_ctl_elem_value *ucontrol)
1689 {
1690 	struct echoaudio *chip;
1691 
1692 	chip = snd_kcontrol_chip(kcontrol);
1693 	spin_lock_irq(&chip->lock);
1694 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1695 	spin_unlock_irq(&chip->lock);
1696 	return 1;
1697 }
1698 
1699 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1700 	.name = "VU-meters Switch",
1701 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1702 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1703 	.info = snd_echo_vumeters_switch_info,
1704 	.put = snd_echo_vumeters_switch_put,
1705 };
1706 
1707 
1708 
1709 /***** Read VU-meters (input, output, analog and digital together) *****/
1710 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1711 				  struct snd_ctl_elem_info *uinfo)
1712 {
1713 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1714 	uinfo->count = 96;
1715 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1716 	uinfo->value.integer.max = 0;
1717 #ifdef ECHOCARD_HAS_VMIXER
1718 	uinfo->dimen.d[0] = 3;	/* Out, In, Virt */
1719 #else
1720 	uinfo->dimen.d[0] = 2;	/* Out, In */
1721 #endif
1722 	uinfo->dimen.d[1] = 16;	/* 16 channels */
1723 	uinfo->dimen.d[2] = 2;	/* 0=level, 1=peak */
1724 	return 0;
1725 }
1726 
1727 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1728 				 struct snd_ctl_elem_value *ucontrol)
1729 {
1730 	struct echoaudio *chip;
1731 
1732 	chip = snd_kcontrol_chip(kcontrol);
1733 	get_audio_meters(chip, ucontrol->value.integer.value);
1734 	return 0;
1735 }
1736 
1737 static const struct snd_kcontrol_new snd_echo_vumeters = {
1738 	.name = "VU-meters",
1739 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1740 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1741 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1742 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1743 	.info = snd_echo_vumeters_info,
1744 	.get = snd_echo_vumeters_get,
1745 	.tlv = {.p = db_scale_output_gain},
1746 };
1747 
1748 
1749 
1750 /*** Channels info - it exports informations about the number of channels ***/
1751 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1752 				       struct snd_ctl_elem_info *uinfo)
1753 {
1754 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1755 	uinfo->count = 6;
1756 	uinfo->value.integer.min = 0;
1757 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1758 	return 0;
1759 }
1760 
1761 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1762 				      struct snd_ctl_elem_value *ucontrol)
1763 {
1764 	struct echoaudio *chip;
1765 	int detected, clocks, bit, src;
1766 
1767 	chip = snd_kcontrol_chip(kcontrol);
1768 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1769 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1770 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1771 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1772 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1773 
1774 	/* Compute the bitmask of the currently valid input clocks */
1775 	detected = detect_input_clocks(chip);
1776 	clocks = 0;
1777 	src = chip->num_clock_sources - 1;
1778 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1779 		if (detected & (1 << bit))
1780 			for (; src >= 0; src--)
1781 				if (bit == chip->clock_source_list[src]) {
1782 					clocks |= 1 << src;
1783 					break;
1784 				}
1785 	ucontrol->value.integer.value[5] = clocks;
1786 
1787 	return 0;
1788 }
1789 
1790 static const struct snd_kcontrol_new snd_echo_channels_info = {
1791 	.name = "Channels info",
1792 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1793 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1794 	.info = snd_echo_channels_info_info,
1795 	.get = snd_echo_channels_info_get,
1796 };
1797 
1798 
1799 
1800 
1801 /******************************************************************************
1802 	IRQ Handler
1803 ******************************************************************************/
1804 
1805 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1806 {
1807 	struct echoaudio *chip = dev_id;
1808 	struct snd_pcm_substream *substream;
1809 	int period, ss, st;
1810 
1811 	spin_lock(&chip->lock);
1812 	st = service_irq(chip);
1813 	if (st < 0) {
1814 		spin_unlock(&chip->lock);
1815 		return IRQ_NONE;
1816 	}
1817 	/* The hardware doesn't tell us which substream caused the irq,
1818 	thus we have to check all running substreams. */
1819 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1820 		substream = chip->substream[ss];
1821 		if (substream && ((struct audiopipe *)substream->runtime->
1822 				private_data)->state == PIPE_STATE_STARTED) {
1823 			period = pcm_pointer(substream) /
1824 				substream->runtime->period_size;
1825 			if (period != chip->last_period[ss]) {
1826 				chip->last_period[ss] = period;
1827 				spin_unlock(&chip->lock);
1828 				snd_pcm_period_elapsed(substream);
1829 				spin_lock(&chip->lock);
1830 			}
1831 		}
1832 	}
1833 	spin_unlock(&chip->lock);
1834 
1835 #ifdef ECHOCARD_HAS_MIDI
1836 	if (st > 0 && chip->midi_in) {
1837 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1838 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1839 	}
1840 #endif
1841 	return IRQ_HANDLED;
1842 }
1843 
1844 
1845 
1846 
1847 /******************************************************************************
1848 	Module construction / destruction
1849 ******************************************************************************/
1850 
1851 static int snd_echo_free(struct echoaudio *chip)
1852 {
1853 	if (chip->comm_page)
1854 		rest_in_peace(chip);
1855 
1856 	if (chip->irq >= 0)
1857 		free_irq(chip->irq, chip);
1858 
1859 	if (chip->comm_page)
1860 		snd_dma_free_pages(&chip->commpage_dma_buf);
1861 
1862 	iounmap(chip->dsp_registers);
1863 	release_and_free_resource(chip->iores);
1864 	pci_disable_device(chip->pci);
1865 
1866 	/* release chip data */
1867 	free_firmware_cache(chip);
1868 	kfree(chip);
1869 	return 0;
1870 }
1871 
1872 
1873 
1874 static int snd_echo_dev_free(struct snd_device *device)
1875 {
1876 	struct echoaudio *chip = device->device_data;
1877 
1878 	return snd_echo_free(chip);
1879 }
1880 
1881 
1882 
1883 /* <--snd_echo_probe() */
1884 static int snd_echo_create(struct snd_card *card,
1885 			   struct pci_dev *pci,
1886 			   struct echoaudio **rchip)
1887 {
1888 	struct echoaudio *chip;
1889 	int err;
1890 	size_t sz;
1891 	static struct snd_device_ops ops = {
1892 		.dev_free = snd_echo_dev_free,
1893 	};
1894 
1895 	*rchip = NULL;
1896 
1897 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1898 
1899 	if ((err = pci_enable_device(pci)) < 0)
1900 		return err;
1901 	pci_set_master(pci);
1902 
1903 	/* Allocate chip if needed */
1904 	if (!*rchip) {
1905 		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1906 		if (!chip) {
1907 			pci_disable_device(pci);
1908 			return -ENOMEM;
1909 		}
1910 		dev_dbg(card->dev, "chip=%p\n", chip);
1911 		spin_lock_init(&chip->lock);
1912 		chip->card = card;
1913 		chip->pci = pci;
1914 		chip->irq = -1;
1915 		atomic_set(&chip->opencount, 0);
1916 		mutex_init(&chip->mode_mutex);
1917 		chip->can_set_rate = 1;
1918 	} else {
1919 		/* If this was called from the resume function, chip is
1920 		 * already allocated and it contains current card settings.
1921 		 */
1922 		chip = *rchip;
1923 	}
1924 
1925 	/* PCI resource allocation */
1926 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1927 	sz = pci_resource_len(pci, 0);
1928 	if (sz > PAGE_SIZE)
1929 		sz = PAGE_SIZE;		/* We map only the required part */
1930 
1931 	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1932 					      ECHOCARD_NAME)) == NULL) {
1933 		dev_err(chip->card->dev, "cannot get memory region\n");
1934 		snd_echo_free(chip);
1935 		return -EBUSY;
1936 	}
1937 	chip->dsp_registers = (volatile u32 __iomem *)
1938 		ioremap_nocache(chip->dsp_registers_phys, sz);
1939 	if (!chip->dsp_registers) {
1940 		dev_err(chip->card->dev, "ioremap failed\n");
1941 		snd_echo_free(chip);
1942 		return -ENOMEM;
1943 	}
1944 
1945 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1946 			KBUILD_MODNAME, chip)) {
1947 		dev_err(chip->card->dev, "cannot grab irq\n");
1948 		snd_echo_free(chip);
1949 		return -EBUSY;
1950 	}
1951 	chip->irq = pci->irq;
1952 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1953 		chip->pci, chip->irq, chip->pci->subsystem_device);
1954 
1955 	/* Create the DSP comm page - this is the area of memory used for most
1956 	of the communication with the DSP, which accesses it via bus mastering */
1957 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1958 				sizeof(struct comm_page),
1959 				&chip->commpage_dma_buf) < 0) {
1960 		dev_err(chip->card->dev, "cannot allocate the comm page\n");
1961 		snd_echo_free(chip);
1962 		return -ENOMEM;
1963 	}
1964 	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1965 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1966 
1967 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1968 	if (err >= 0)
1969 		err = set_mixer_defaults(chip);
1970 	if (err < 0) {
1971 		dev_err(card->dev, "init_hw err=%d\n", err);
1972 		snd_echo_free(chip);
1973 		return err;
1974 	}
1975 
1976 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1977 		snd_echo_free(chip);
1978 		return err;
1979 	}
1980 	*rchip = chip;
1981 	/* Init done ! */
1982 	return 0;
1983 }
1984 
1985 
1986 
1987 /* constructor */
1988 static int snd_echo_probe(struct pci_dev *pci,
1989 			  const struct pci_device_id *pci_id)
1990 {
1991 	static int dev;
1992 	struct snd_card *card;
1993 	struct echoaudio *chip;
1994 	char *dsp;
1995 	int i, err;
1996 
1997 	if (dev >= SNDRV_CARDS)
1998 		return -ENODEV;
1999 	if (!enable[dev]) {
2000 		dev++;
2001 		return -ENOENT;
2002 	}
2003 
2004 	i = 0;
2005 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2006 			   0, &card);
2007 	if (err < 0)
2008 		return err;
2009 
2010 	chip = NULL;	/* Tells snd_echo_create to allocate chip */
2011 	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2012 		snd_card_free(card);
2013 		return err;
2014 	}
2015 
2016 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2017 	strcpy(card->shortname, chip->card_name);
2018 
2019 	dsp = "56301";
2020 	if (pci_id->device == 0x3410)
2021 		dsp = "56361";
2022 
2023 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2024 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2025 		chip->dsp_registers_phys, chip->irq);
2026 
2027 	if ((err = snd_echo_new_pcm(chip)) < 0) {
2028 		dev_err(chip->card->dev, "new pcm error %d\n", err);
2029 		snd_card_free(card);
2030 		return err;
2031 	}
2032 
2033 #ifdef ECHOCARD_HAS_MIDI
2034 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2035 		if ((err = snd_echo_midi_create(card, chip)) < 0) {
2036 			dev_err(chip->card->dev, "new midi error %d\n", err);
2037 			snd_card_free(card);
2038 			return err;
2039 		}
2040 	}
2041 #endif
2042 
2043 #ifdef ECHOCARD_HAS_VMIXER
2044 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2045 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2046 		goto ctl_error;
2047 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2048 	err = snd_ctl_add(chip->card,
2049 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2050 	if (err < 0)
2051 		goto ctl_error;
2052 #endif
2053 #else /* ECHOCARD_HAS_VMIXER */
2054 	err = snd_ctl_add(chip->card,
2055 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2056 	if (err < 0)
2057 		goto ctl_error;
2058 #endif /* ECHOCARD_HAS_VMIXER */
2059 
2060 #ifdef ECHOCARD_HAS_INPUT_GAIN
2061 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2062 		goto ctl_error;
2063 #endif
2064 
2065 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2066 	if (!chip->hasnt_input_nominal_level)
2067 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2068 			goto ctl_error;
2069 #endif
2070 
2071 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2072 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2073 		goto ctl_error;
2074 #endif
2075 
2076 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2077 		goto ctl_error;
2078 
2079 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2080 		goto ctl_error;
2081 
2082 #ifdef ECHOCARD_HAS_MONITOR
2083 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2084 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2085 		goto ctl_error;
2086 #endif
2087 
2088 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2089 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2090 		goto ctl_error;
2091 #endif
2092 
2093 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2094 		goto ctl_error;
2095 
2096 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2097 	/* Creates a list of available digital modes */
2098 	chip->num_digital_modes = 0;
2099 	for (i = 0; i < 6; i++)
2100 		if (chip->digital_modes & (1 << i))
2101 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2102 
2103 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2104 		goto ctl_error;
2105 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2106 
2107 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2108 	/* Creates a list of available clock sources */
2109 	chip->num_clock_sources = 0;
2110 	for (i = 0; i < 10; i++)
2111 		if (chip->input_clock_types & (1 << i))
2112 			chip->clock_source_list[chip->num_clock_sources++] = i;
2113 
2114 	if (chip->num_clock_sources > 1) {
2115 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2116 		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2117 			goto ctl_error;
2118 	}
2119 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2120 
2121 #ifdef ECHOCARD_HAS_DIGITAL_IO
2122 	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2123 		goto ctl_error;
2124 #endif
2125 
2126 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2127 	if (chip->has_phantom_power)
2128 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2129 			goto ctl_error;
2130 #endif
2131 
2132 	err = snd_card_register(card);
2133 	if (err < 0)
2134 		goto ctl_error;
2135 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2136 
2137 	pci_set_drvdata(pci, chip);
2138 	dev++;
2139 	return 0;
2140 
2141 ctl_error:
2142 	dev_err(card->dev, "new control error %d\n", err);
2143 	snd_card_free(card);
2144 	return err;
2145 }
2146 
2147 
2148 
2149 #if defined(CONFIG_PM_SLEEP)
2150 
2151 static int snd_echo_suspend(struct device *dev)
2152 {
2153 	struct echoaudio *chip = dev_get_drvdata(dev);
2154 
2155 #ifdef ECHOCARD_HAS_MIDI
2156 	/* This call can sleep */
2157 	if (chip->midi_out)
2158 		snd_echo_midi_output_trigger(chip->midi_out, 0);
2159 #endif
2160 	spin_lock_irq(&chip->lock);
2161 	if (wait_handshake(chip)) {
2162 		spin_unlock_irq(&chip->lock);
2163 		return -EIO;
2164 	}
2165 	clear_handshake(chip);
2166 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2167 		spin_unlock_irq(&chip->lock);
2168 		return -EIO;
2169 	}
2170 	spin_unlock_irq(&chip->lock);
2171 
2172 	chip->dsp_code = NULL;
2173 	free_irq(chip->irq, chip);
2174 	chip->irq = -1;
2175 	return 0;
2176 }
2177 
2178 
2179 
2180 static int snd_echo_resume(struct device *dev)
2181 {
2182 	struct pci_dev *pci = to_pci_dev(dev);
2183 	struct echoaudio *chip = dev_get_drvdata(dev);
2184 	struct comm_page *commpage, *commpage_bak;
2185 	u32 pipe_alloc_mask;
2186 	int err;
2187 
2188 	commpage = chip->comm_page;
2189 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2190 	if (commpage_bak == NULL)
2191 		return -ENOMEM;
2192 
2193 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2194 	if (err < 0) {
2195 		kfree(commpage_bak);
2196 		dev_err(dev, "resume init_hw err=%d\n", err);
2197 		snd_echo_free(chip);
2198 		return err;
2199 	}
2200 
2201 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2202 	 * restore_dsp_settings() fails.
2203 	 */
2204 	pipe_alloc_mask = chip->pipe_alloc_mask;
2205 	chip->pipe_alloc_mask = 0;
2206 	err = restore_dsp_rettings(chip);
2207 	chip->pipe_alloc_mask = pipe_alloc_mask;
2208 	if (err < 0) {
2209 		kfree(commpage_bak);
2210 		return err;
2211 	}
2212 
2213 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2214 		sizeof(commpage->audio_format));
2215 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2216 		sizeof(commpage->sglist_addr));
2217 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2218 		sizeof(commpage->midi_output));
2219 	kfree(commpage_bak);
2220 
2221 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2222 			KBUILD_MODNAME, chip)) {
2223 		dev_err(chip->card->dev, "cannot grab irq\n");
2224 		snd_echo_free(chip);
2225 		return -EBUSY;
2226 	}
2227 	chip->irq = pci->irq;
2228 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2229 
2230 #ifdef ECHOCARD_HAS_MIDI
2231 	if (chip->midi_input_enabled)
2232 		enable_midi_input(chip, true);
2233 	if (chip->midi_out)
2234 		snd_echo_midi_output_trigger(chip->midi_out, 1);
2235 #endif
2236 
2237 	return 0;
2238 }
2239 
2240 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2241 #define SND_ECHO_PM_OPS	&snd_echo_pm
2242 #else
2243 #define SND_ECHO_PM_OPS	NULL
2244 #endif /* CONFIG_PM_SLEEP */
2245 
2246 
2247 static void snd_echo_remove(struct pci_dev *pci)
2248 {
2249 	struct echoaudio *chip;
2250 
2251 	chip = pci_get_drvdata(pci);
2252 	if (chip)
2253 		snd_card_free(chip->card);
2254 }
2255 
2256 
2257 
2258 /******************************************************************************
2259 	Everything starts and ends here
2260 ******************************************************************************/
2261 
2262 /* pci_driver definition */
2263 static struct pci_driver echo_driver = {
2264 	.name = KBUILD_MODNAME,
2265 	.id_table = snd_echo_ids,
2266 	.probe = snd_echo_probe,
2267 	.remove = snd_echo_remove,
2268 	.driver = {
2269 		.pm = SND_ECHO_PM_OPS,
2270 	},
2271 };
2272 
2273 module_pci_driver(echo_driver);
2274