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