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