xref: /openbmc/linux/sound/pci/echoaudio/echoaudio.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dd7b254dSGiuliano Pochini /*
3dd7b254dSGiuliano Pochini  *  ALSA driver for Echoaudio soundcards.
4dd7b254dSGiuliano Pochini  *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5a0b224b9SMark Hills  *  Copyright (C) 2020 Mark Hills <mark@xwax.org>
6dd7b254dSGiuliano Pochini  */
7dd7b254dSGiuliano Pochini 
8da155d5bSPaul Gortmaker #include <linux/module.h>
9da155d5bSPaul Gortmaker 
10dd7b254dSGiuliano Pochini MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
11dd7b254dSGiuliano Pochini MODULE_LICENSE("GPL v2");
12dd7b254dSGiuliano Pochini MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
13dd7b254dSGiuliano Pochini MODULE_DEVICE_TABLE(pci, snd_echo_ids);
14dd7b254dSGiuliano Pochini 
15dd7b254dSGiuliano Pochini static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
16dd7b254dSGiuliano Pochini static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
17a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
18dd7b254dSGiuliano Pochini 
19dd7b254dSGiuliano Pochini module_param_array(index, int, NULL, 0444);
20dd7b254dSGiuliano Pochini MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
21dd7b254dSGiuliano Pochini module_param_array(id, charp, NULL, 0444);
22dd7b254dSGiuliano Pochini MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
23dd7b254dSGiuliano Pochini module_param_array(enable, bool, NULL, 0444);
24dd7b254dSGiuliano Pochini MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
25dd7b254dSGiuliano Pochini 
269bca0907STakashi Iwai static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
270cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
28dd7b254dSGiuliano Pochini 
2919b50063SGiuliano Pochini 
3019b50063SGiuliano Pochini 
get_firmware(const struct firmware ** fw_entry,struct echoaudio * chip,const short fw_index)31dd7b254dSGiuliano Pochini static int get_firmware(const struct firmware **fw_entry,
3219b50063SGiuliano Pochini 			struct echoaudio *chip, const short fw_index)
33dd7b254dSGiuliano Pochini {
34dd7b254dSGiuliano Pochini 	int err;
35dd7b254dSGiuliano Pochini 	char name[30];
3619b50063SGiuliano Pochini 
37c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
384f8ada44SGiuliano Pochini 	if (chip->fw_cache[fw_index]) {
39b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev,
40b5b4a41bSSudip Mukherjee 			"firmware requested: %s is cached\n",
41b5b4a41bSSudip Mukherjee 			card_fw[fw_index].data);
424f8ada44SGiuliano Pochini 		*fw_entry = chip->fw_cache[fw_index];
434f8ada44SGiuliano Pochini 		return 0;
444f8ada44SGiuliano Pochini 	}
454f8ada44SGiuliano Pochini #endif
464f8ada44SGiuliano Pochini 
47b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
48b5b4a41bSSudip Mukherjee 		"firmware requested: %s\n", card_fw[fw_index].data);
494f8ada44SGiuliano Pochini 	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
50afe5da3eSTakashi Iwai 	err = request_firmware(fw_entry, name, &chip->pci->dev);
514f8ada44SGiuliano Pochini 	if (err < 0)
52ece7a36dSTakashi Iwai 		dev_err(chip->card->dev,
53ece7a36dSTakashi Iwai 			"get_firmware(): Firmware not available (%d)\n", err);
54c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
554f8ada44SGiuliano Pochini 	else
564f8ada44SGiuliano Pochini 		chip->fw_cache[fw_index] = *fw_entry;
574f8ada44SGiuliano Pochini #endif
58dd7b254dSGiuliano Pochini 	return err;
59dd7b254dSGiuliano Pochini }
60dd7b254dSGiuliano Pochini 
6119b50063SGiuliano Pochini 
6219b50063SGiuliano Pochini 
free_firmware(const struct firmware * fw_entry,struct echoaudio * chip)63e3690869SSudip Mukherjee static void free_firmware(const struct firmware *fw_entry,
64e3690869SSudip Mukherjee 			  struct echoaudio *chip)
65dd7b254dSGiuliano Pochini {
66c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
67b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
684f8ada44SGiuliano Pochini #else
69dd7b254dSGiuliano Pochini 	release_firmware(fw_entry);
704f8ada44SGiuliano Pochini #endif
714f8ada44SGiuliano Pochini }
724f8ada44SGiuliano Pochini 
734f8ada44SGiuliano Pochini 
744f8ada44SGiuliano Pochini 
free_firmware_cache(struct echoaudio * chip)754f8ada44SGiuliano Pochini static void free_firmware_cache(struct echoaudio *chip)
764f8ada44SGiuliano Pochini {
77c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
784f8ada44SGiuliano Pochini 	int i;
794f8ada44SGiuliano Pochini 
804f8ada44SGiuliano Pochini 	for (i = 0; i < 8 ; i++)
814f8ada44SGiuliano Pochini 		if (chip->fw_cache[i]) {
824f8ada44SGiuliano Pochini 			release_firmware(chip->fw_cache[i]);
83b5b4a41bSSudip Mukherjee 			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
844f8ada44SGiuliano Pochini 		}
854f8ada44SGiuliano Pochini 
864f8ada44SGiuliano Pochini #endif
87dd7b254dSGiuliano Pochini }
88dd7b254dSGiuliano Pochini 
89dd7b254dSGiuliano Pochini 
90dd7b254dSGiuliano Pochini 
91dd7b254dSGiuliano Pochini /******************************************************************************
92dd7b254dSGiuliano Pochini 	PCM interface
93dd7b254dSGiuliano Pochini ******************************************************************************/
94dd7b254dSGiuliano Pochini 
audiopipe_free(struct snd_pcm_runtime * runtime)95dd7b254dSGiuliano Pochini static void audiopipe_free(struct snd_pcm_runtime *runtime)
96dd7b254dSGiuliano Pochini {
97dd7b254dSGiuliano Pochini 	struct audiopipe *pipe = runtime->private_data;
98dd7b254dSGiuliano Pochini 
99dd7b254dSGiuliano Pochini 	if (pipe->sgpage.area)
100dd7b254dSGiuliano Pochini 		snd_dma_free_pages(&pipe->sgpage);
101dd7b254dSGiuliano Pochini 	kfree(pipe);
102dd7b254dSGiuliano Pochini }
103dd7b254dSGiuliano Pochini 
104dd7b254dSGiuliano Pochini 
105dd7b254dSGiuliano Pochini 
hw_rule_capture_format_by_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)106dd7b254dSGiuliano Pochini static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
107dd7b254dSGiuliano Pochini 					      struct snd_pcm_hw_rule *rule)
108dd7b254dSGiuliano Pochini {
109dd7b254dSGiuliano Pochini 	struct snd_interval *c = hw_param_interval(params,
110dd7b254dSGiuliano Pochini 						   SNDRV_PCM_HW_PARAM_CHANNELS);
111dd7b254dSGiuliano Pochini 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
112dd7b254dSGiuliano Pochini 	struct snd_mask fmt;
113dd7b254dSGiuliano Pochini 
114dd7b254dSGiuliano Pochini 	snd_mask_any(&fmt);
115dd7b254dSGiuliano Pochini 
116dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
117dd7b254dSGiuliano Pochini 	/* >=2 channels cannot be S32_BE */
118dd7b254dSGiuliano Pochini 	if (c->min == 2) {
119dd7b254dSGiuliano Pochini 		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
120dd7b254dSGiuliano Pochini 		return snd_mask_refine(f, &fmt);
121dd7b254dSGiuliano Pochini 	}
122dd7b254dSGiuliano Pochini #endif
123dd7b254dSGiuliano Pochini 	/* > 2 channels cannot be U8 and S32_BE */
124dd7b254dSGiuliano Pochini 	if (c->min > 2) {
125dd7b254dSGiuliano Pochini 		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
126dd7b254dSGiuliano Pochini 		return snd_mask_refine(f, &fmt);
127dd7b254dSGiuliano Pochini 	}
128dd7b254dSGiuliano Pochini 	/* Mono is ok with any format */
129dd7b254dSGiuliano Pochini 	return 0;
130dd7b254dSGiuliano Pochini }
131dd7b254dSGiuliano Pochini 
132dd7b254dSGiuliano Pochini 
133dd7b254dSGiuliano Pochini 
hw_rule_capture_channels_by_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)134dd7b254dSGiuliano Pochini static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
135dd7b254dSGiuliano Pochini 					      struct snd_pcm_hw_rule *rule)
136dd7b254dSGiuliano Pochini {
137dd7b254dSGiuliano Pochini 	struct snd_interval *c = hw_param_interval(params,
138dd7b254dSGiuliano Pochini 						   SNDRV_PCM_HW_PARAM_CHANNELS);
139dd7b254dSGiuliano Pochini 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
140dd7b254dSGiuliano Pochini 	struct snd_interval ch;
141dd7b254dSGiuliano Pochini 
142dd7b254dSGiuliano Pochini 	snd_interval_any(&ch);
143dd7b254dSGiuliano Pochini 
144dd7b254dSGiuliano Pochini 	/* S32_BE is mono (and stereo) only */
145dd7b254dSGiuliano Pochini 	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
146dd7b254dSGiuliano Pochini 		ch.min = 1;
147dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
148dd7b254dSGiuliano Pochini 		ch.max = 2;
149dd7b254dSGiuliano Pochini #else
150dd7b254dSGiuliano Pochini 		ch.max = 1;
151dd7b254dSGiuliano Pochini #endif
152dd7b254dSGiuliano Pochini 		ch.integer = 1;
153dd7b254dSGiuliano Pochini 		return snd_interval_refine(c, &ch);
154dd7b254dSGiuliano Pochini 	}
155dd7b254dSGiuliano Pochini 	/* U8 can be only mono or stereo */
156dd7b254dSGiuliano Pochini 	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
157dd7b254dSGiuliano Pochini 		ch.min = 1;
158dd7b254dSGiuliano Pochini 		ch.max = 2;
159dd7b254dSGiuliano Pochini 		ch.integer = 1;
160dd7b254dSGiuliano Pochini 		return snd_interval_refine(c, &ch);
161dd7b254dSGiuliano Pochini 	}
162dd7b254dSGiuliano Pochini 	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
163dd7b254dSGiuliano Pochini 	return 0;
164dd7b254dSGiuliano Pochini }
165dd7b254dSGiuliano Pochini 
166dd7b254dSGiuliano Pochini 
167dd7b254dSGiuliano Pochini 
hw_rule_playback_format_by_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)168dd7b254dSGiuliano Pochini static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
169dd7b254dSGiuliano Pochini 					       struct snd_pcm_hw_rule *rule)
170dd7b254dSGiuliano Pochini {
171dd7b254dSGiuliano Pochini 	struct snd_interval *c = hw_param_interval(params,
172dd7b254dSGiuliano Pochini 						   SNDRV_PCM_HW_PARAM_CHANNELS);
173dd7b254dSGiuliano Pochini 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
174dd7b254dSGiuliano Pochini 	struct snd_mask fmt;
175dd7b254dSGiuliano Pochini 	u64 fmask;
176dd7b254dSGiuliano Pochini 	snd_mask_any(&fmt);
177dd7b254dSGiuliano Pochini 
178dd7b254dSGiuliano Pochini 	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
179dd7b254dSGiuliano Pochini 
180dd7b254dSGiuliano Pochini 	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
181dd7b254dSGiuliano Pochini 	if (c->min > 2) {
182dd7b254dSGiuliano Pochini 		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
183dd7b254dSGiuliano Pochini 			 SNDRV_PCM_FMTBIT_S24_3LE |
184dd7b254dSGiuliano Pochini 			 SNDRV_PCM_FMTBIT_S32_LE;
185dd7b254dSGiuliano Pochini 	/* 1 channel must be S32_BE or S32_LE */
186dd7b254dSGiuliano Pochini 	} else if (c->max == 1)
187dd7b254dSGiuliano Pochini 		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
188dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
189dd7b254dSGiuliano Pochini 	/* 2 channels cannot be S32_BE */
190dd7b254dSGiuliano Pochini 	else if (c->min == 2 && c->max == 2)
191dd7b254dSGiuliano Pochini 		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
192dd7b254dSGiuliano Pochini #endif
193dd7b254dSGiuliano Pochini 	else
194dd7b254dSGiuliano Pochini 		return 0;
195dd7b254dSGiuliano Pochini 
196dd7b254dSGiuliano Pochini 	fmt.bits[0] &= (u32)fmask;
197dd7b254dSGiuliano Pochini 	fmt.bits[1] &= (u32)(fmask >> 32);
198dd7b254dSGiuliano Pochini 	return snd_mask_refine(f, &fmt);
199dd7b254dSGiuliano Pochini }
200dd7b254dSGiuliano Pochini 
201dd7b254dSGiuliano Pochini 
202dd7b254dSGiuliano Pochini 
hw_rule_playback_channels_by_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)203dd7b254dSGiuliano Pochini static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
204dd7b254dSGiuliano Pochini 					       struct snd_pcm_hw_rule *rule)
205dd7b254dSGiuliano Pochini {
206dd7b254dSGiuliano Pochini 	struct snd_interval *c = hw_param_interval(params,
207dd7b254dSGiuliano Pochini 						   SNDRV_PCM_HW_PARAM_CHANNELS);
208dd7b254dSGiuliano Pochini 	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
209dd7b254dSGiuliano Pochini 	struct snd_interval ch;
210dd7b254dSGiuliano Pochini 	u64 fmask;
211dd7b254dSGiuliano Pochini 
212dd7b254dSGiuliano Pochini 	snd_interval_any(&ch);
213dd7b254dSGiuliano Pochini 	ch.integer = 1;
214dd7b254dSGiuliano Pochini 	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
215dd7b254dSGiuliano Pochini 
216dd7b254dSGiuliano Pochini 	/* S32_BE is mono (and stereo) only */
217dd7b254dSGiuliano Pochini 	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
218dd7b254dSGiuliano Pochini 		ch.min = 1;
219dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
220dd7b254dSGiuliano Pochini 		ch.max = 2;
221dd7b254dSGiuliano Pochini #else
222dd7b254dSGiuliano Pochini 		ch.max = 1;
223dd7b254dSGiuliano Pochini #endif
224dd7b254dSGiuliano Pochini 	/* U8 is stereo only */
225dd7b254dSGiuliano Pochini 	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
226dd7b254dSGiuliano Pochini 		ch.min = ch.max = 2;
227dd7b254dSGiuliano Pochini 	/* S16_LE and S24_3LE must be at least stereo */
228dd7b254dSGiuliano Pochini 	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
229dd7b254dSGiuliano Pochini 			       SNDRV_PCM_FMTBIT_S24_3LE)))
230dd7b254dSGiuliano Pochini 		ch.min = 2;
231dd7b254dSGiuliano Pochini 	else
232dd7b254dSGiuliano Pochini 		return 0;
233dd7b254dSGiuliano Pochini 
234dd7b254dSGiuliano Pochini 	return snd_interval_refine(c, &ch);
235dd7b254dSGiuliano Pochini }
236dd7b254dSGiuliano Pochini 
237dd7b254dSGiuliano Pochini 
238dd7b254dSGiuliano Pochini 
239dd7b254dSGiuliano Pochini /* Since the sample rate is a global setting, do allow the user to change the
240dd7b254dSGiuliano Pochini sample rate only if there is only one pcm device open. */
hw_rule_sample_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)241dd7b254dSGiuliano Pochini static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
242dd7b254dSGiuliano Pochini 			       struct snd_pcm_hw_rule *rule)
243dd7b254dSGiuliano Pochini {
244dd7b254dSGiuliano Pochini 	struct snd_interval *rate = hw_param_interval(params,
245dd7b254dSGiuliano Pochini 						      SNDRV_PCM_HW_PARAM_RATE);
246dd7b254dSGiuliano Pochini 	struct echoaudio *chip = rule->private;
247dd7b254dSGiuliano Pochini 	struct snd_interval fixed;
248027c7002SMark Hills 	int err;
249dd7b254dSGiuliano Pochini 
250027c7002SMark Hills 	mutex_lock(&chip->mode_mutex);
251027c7002SMark Hills 
252027c7002SMark Hills 	if (chip->can_set_rate) {
253027c7002SMark Hills 		err = 0;
254027c7002SMark Hills 	} else {
255dd7b254dSGiuliano Pochini 		snd_interval_any(&fixed);
256dd7b254dSGiuliano Pochini 		fixed.min = fixed.max = chip->sample_rate;
257027c7002SMark Hills 		err = snd_interval_refine(rate, &fixed);
258dd7b254dSGiuliano Pochini 	}
259027c7002SMark Hills 
260027c7002SMark Hills 	mutex_unlock(&chip->mode_mutex);
261027c7002SMark Hills 	return err;
262dd7b254dSGiuliano Pochini }
263dd7b254dSGiuliano Pochini 
264dd7b254dSGiuliano Pochini 
pcm_open(struct snd_pcm_substream * substream,signed char max_channels)265dd7b254dSGiuliano Pochini static int pcm_open(struct snd_pcm_substream *substream,
266dd7b254dSGiuliano Pochini 		    signed char max_channels)
267dd7b254dSGiuliano Pochini {
268dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
269dd7b254dSGiuliano Pochini 	struct snd_pcm_runtime *runtime;
270dd7b254dSGiuliano Pochini 	struct audiopipe *pipe;
271dd7b254dSGiuliano Pochini 	int err, i;
272dd7b254dSGiuliano Pochini 
273dd7b254dSGiuliano Pochini 	if (max_channels <= 0)
274dd7b254dSGiuliano Pochini 		return -EAGAIN;
275dd7b254dSGiuliano Pochini 
276dd7b254dSGiuliano Pochini 	chip = snd_pcm_substream_chip(substream);
277dd7b254dSGiuliano Pochini 	runtime = substream->runtime;
278dd7b254dSGiuliano Pochini 
27959feddb2SPanagiotis Issaris 	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
28059feddb2SPanagiotis Issaris 	if (!pipe)
281dd7b254dSGiuliano Pochini 		return -ENOMEM;
282dd7b254dSGiuliano Pochini 	pipe->index = -1;		/* Not configured yet */
283dd7b254dSGiuliano Pochini 
284dd7b254dSGiuliano Pochini 	/* Set up hw capabilities and contraints */
285dd7b254dSGiuliano Pochini 	memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
286b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
287dd7b254dSGiuliano Pochini 	pipe->constr.list = channels_list;
288dd7b254dSGiuliano Pochini 	pipe->constr.mask = 0;
289dd7b254dSGiuliano Pochini 	for (i = 0; channels_list[i] <= max_channels; i++);
290dd7b254dSGiuliano Pochini 	pipe->constr.count = i;
291dd7b254dSGiuliano Pochini 	if (pipe->hw.channels_max > max_channels)
292dd7b254dSGiuliano Pochini 		pipe->hw.channels_max = max_channels;
293dd7b254dSGiuliano Pochini 	if (chip->digital_mode == DIGITAL_MODE_ADAT) {
294dd7b254dSGiuliano Pochini 		pipe->hw.rate_max = 48000;
295dd7b254dSGiuliano Pochini 		pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
296dd7b254dSGiuliano Pochini 	}
297dd7b254dSGiuliano Pochini 
298dd7b254dSGiuliano Pochini 	runtime->hw = pipe->hw;
299dd7b254dSGiuliano Pochini 	runtime->private_data = pipe;
300dd7b254dSGiuliano Pochini 	runtime->private_free = audiopipe_free;
301dd7b254dSGiuliano Pochini 	snd_pcm_set_sync(substream);
302dd7b254dSGiuliano Pochini 
303dd7b254dSGiuliano Pochini 	/* Only mono and any even number of channels are allowed */
304549717fcSTakashi Iwai 	err = snd_pcm_hw_constraint_list(runtime, 0,
305dd7b254dSGiuliano Pochini 					 SNDRV_PCM_HW_PARAM_CHANNELS,
306549717fcSTakashi Iwai 					 &pipe->constr);
307549717fcSTakashi Iwai 	if (err < 0)
308dd7b254dSGiuliano Pochini 		return err;
309dd7b254dSGiuliano Pochini 
310dd7b254dSGiuliano Pochini 	/* All periods should have the same size */
311549717fcSTakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime,
312549717fcSTakashi Iwai 					    SNDRV_PCM_HW_PARAM_PERIODS);
313549717fcSTakashi Iwai 	if (err < 0)
314dd7b254dSGiuliano Pochini 		return err;
315dd7b254dSGiuliano Pochini 
316dd7b254dSGiuliano Pochini 	/* The hw accesses memory in chunks 32 frames long and they should be
317dd7b254dSGiuliano Pochini 	32-bytes-aligned. It's not a requirement, but it seems that IRQs are
318dd7b254dSGiuliano Pochini 	generated with a resolution of 32 frames. Thus we need the following */
319549717fcSTakashi Iwai 	err = snd_pcm_hw_constraint_step(runtime, 0,
320549717fcSTakashi Iwai 					 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
321549717fcSTakashi Iwai 	if (err < 0)
322dd7b254dSGiuliano Pochini 		return err;
323549717fcSTakashi Iwai 	err = snd_pcm_hw_constraint_step(runtime, 0,
324549717fcSTakashi Iwai 					 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
325549717fcSTakashi Iwai 	if (err < 0)
326dd7b254dSGiuliano Pochini 		return err;
327dd7b254dSGiuliano Pochini 
328549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
329dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_RATE,
330dd7b254dSGiuliano Pochini 				  hw_rule_sample_rate, chip,
331549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_RATE, -1);
332549717fcSTakashi Iwai 	if (err < 0)
333dd7b254dSGiuliano Pochini 		return err;
334dd7b254dSGiuliano Pochini 
335027c7002SMark Hills 	/* Allocate a page for the scatter-gather list */
336549717fcSTakashi Iwai 	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3377564d3b6STakashi Iwai 				  &chip->pci->dev,
338549717fcSTakashi Iwai 				  PAGE_SIZE, &pipe->sgpage);
339549717fcSTakashi Iwai 	if (err < 0) {
340b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "s-g list allocation failed\n");
341dd7b254dSGiuliano Pochini 		return err;
342dd7b254dSGiuliano Pochini 	}
343dd7b254dSGiuliano Pochini 
344027c7002SMark Hills 	/*
345027c7002SMark Hills 	 * Sole ownership required to set the rate
346027c7002SMark Hills 	 */
347027c7002SMark Hills 
348027c7002SMark Hills 	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
349027c7002SMark Hills 		chip->opencount, chip->can_set_rate, chip->rate_set);
350027c7002SMark Hills 
351027c7002SMark Hills 	chip->opencount++;
352027c7002SMark Hills 	if (chip->opencount > 1 && chip->rate_set)
353027c7002SMark Hills 		chip->can_set_rate = 0;
354027c7002SMark Hills 
355dd7b254dSGiuliano Pochini 	return 0;
356dd7b254dSGiuliano Pochini }
357dd7b254dSGiuliano Pochini 
358dd7b254dSGiuliano Pochini 
359dd7b254dSGiuliano Pochini 
pcm_analog_in_open(struct snd_pcm_substream * substream)360dd7b254dSGiuliano Pochini static int pcm_analog_in_open(struct snd_pcm_substream *substream)
361dd7b254dSGiuliano Pochini {
362dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
363dd7b254dSGiuliano Pochini 	int err;
364dd7b254dSGiuliano Pochini 
365549717fcSTakashi Iwai 	err = pcm_open(substream,
366549717fcSTakashi Iwai 		       num_analog_busses_in(chip) - substream->number);
367549717fcSTakashi Iwai 	if (err < 0)
368dd7b254dSGiuliano Pochini 		return err;
369549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
370dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_CHANNELS,
371dd7b254dSGiuliano Pochini 				  hw_rule_capture_channels_by_format, NULL,
372549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
373549717fcSTakashi Iwai 	if (err < 0)
374dd7b254dSGiuliano Pochini 		return err;
375549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
376dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_FORMAT,
377dd7b254dSGiuliano Pochini 				  hw_rule_capture_format_by_channels, NULL,
378549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
379549717fcSTakashi Iwai 	if (err < 0)
380dd7b254dSGiuliano Pochini 		return err;
381027c7002SMark Hills 
382dd7b254dSGiuliano Pochini 	return 0;
383dd7b254dSGiuliano Pochini }
384dd7b254dSGiuliano Pochini 
385dd7b254dSGiuliano Pochini 
386dd7b254dSGiuliano Pochini 
pcm_analog_out_open(struct snd_pcm_substream * substream)387dd7b254dSGiuliano Pochini static int pcm_analog_out_open(struct snd_pcm_substream *substream)
388dd7b254dSGiuliano Pochini {
389dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
390dd7b254dSGiuliano Pochini 	int max_channels, err;
391dd7b254dSGiuliano Pochini 
392dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
393dd7b254dSGiuliano Pochini 	max_channels = num_pipes_out(chip);
394dd7b254dSGiuliano Pochini #else
395dd7b254dSGiuliano Pochini 	max_channels = num_analog_busses_out(chip);
396dd7b254dSGiuliano Pochini #endif
397549717fcSTakashi Iwai 	err = pcm_open(substream, max_channels - substream->number);
398549717fcSTakashi Iwai 	if (err < 0)
399dd7b254dSGiuliano Pochini 		return err;
400549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
401dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_CHANNELS,
402dd7b254dSGiuliano Pochini 				  hw_rule_playback_channels_by_format,
403dd7b254dSGiuliano Pochini 				  NULL,
404549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
405549717fcSTakashi Iwai 	if (err < 0)
406dd7b254dSGiuliano Pochini 		return err;
407549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
408dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_FORMAT,
409dd7b254dSGiuliano Pochini 				  hw_rule_playback_format_by_channels,
410dd7b254dSGiuliano Pochini 				  NULL,
411549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
412549717fcSTakashi Iwai 	if (err < 0)
413dd7b254dSGiuliano Pochini 		return err;
414027c7002SMark Hills 
415dd7b254dSGiuliano Pochini 	return 0;
416dd7b254dSGiuliano Pochini }
417dd7b254dSGiuliano Pochini 
418dd7b254dSGiuliano Pochini 
419dd7b254dSGiuliano Pochini 
420dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
421dd7b254dSGiuliano Pochini 
pcm_digital_in_open(struct snd_pcm_substream * substream)422dd7b254dSGiuliano Pochini static int pcm_digital_in_open(struct snd_pcm_substream *substream)
423dd7b254dSGiuliano Pochini {
424dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
425dd7b254dSGiuliano Pochini 	int err, max_channels;
426dd7b254dSGiuliano Pochini 
427dd7b254dSGiuliano Pochini 	max_channels = num_digital_busses_in(chip) - substream->number;
428befceea9STakashi Iwai 	mutex_lock(&chip->mode_mutex);
429dd7b254dSGiuliano Pochini 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
430dd7b254dSGiuliano Pochini 		err = pcm_open(substream, max_channels);
431dd7b254dSGiuliano Pochini 	else	/* If the card has ADAT, subtract the 6 channels
432dd7b254dSGiuliano Pochini 		 * that S/PDIF doesn't have
433dd7b254dSGiuliano Pochini 		 */
434dd7b254dSGiuliano Pochini 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
435dd7b254dSGiuliano Pochini 
436dd7b254dSGiuliano Pochini 	if (err < 0)
437dd7b254dSGiuliano Pochini 		goto din_exit;
438dd7b254dSGiuliano Pochini 
439549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
440dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_CHANNELS,
441dd7b254dSGiuliano Pochini 				  hw_rule_capture_channels_by_format, NULL,
442549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
443549717fcSTakashi Iwai 	if (err < 0)
444dd7b254dSGiuliano Pochini 		goto din_exit;
445549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
446dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_FORMAT,
447dd7b254dSGiuliano Pochini 				  hw_rule_capture_format_by_channels, NULL,
448549717fcSTakashi Iwai 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
449549717fcSTakashi Iwai 	if (err < 0)
450dd7b254dSGiuliano Pochini 		goto din_exit;
451dd7b254dSGiuliano Pochini 
452dd7b254dSGiuliano Pochini din_exit:
453befceea9STakashi Iwai 	mutex_unlock(&chip->mode_mutex);
454dd7b254dSGiuliano Pochini 	return err;
455dd7b254dSGiuliano Pochini }
456dd7b254dSGiuliano Pochini 
457dd7b254dSGiuliano Pochini 
458dd7b254dSGiuliano Pochini 
459dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
460dd7b254dSGiuliano Pochini 
pcm_digital_out_open(struct snd_pcm_substream * substream)461dd7b254dSGiuliano Pochini static int pcm_digital_out_open(struct snd_pcm_substream *substream)
462dd7b254dSGiuliano Pochini {
463dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
464dd7b254dSGiuliano Pochini 	int err, max_channels;
465dd7b254dSGiuliano Pochini 
466dd7b254dSGiuliano Pochini 	max_channels = num_digital_busses_out(chip) - substream->number;
467befceea9STakashi Iwai 	mutex_lock(&chip->mode_mutex);
468dd7b254dSGiuliano Pochini 	if (chip->digital_mode == DIGITAL_MODE_ADAT)
469dd7b254dSGiuliano Pochini 		err = pcm_open(substream, max_channels);
470dd7b254dSGiuliano Pochini 	else	/* If the card has ADAT, subtract the 6 channels
471dd7b254dSGiuliano Pochini 		 * that S/PDIF doesn't have
472dd7b254dSGiuliano Pochini 		 */
473dd7b254dSGiuliano Pochini 		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
474dd7b254dSGiuliano Pochini 
475dd7b254dSGiuliano Pochini 	if (err < 0)
476dd7b254dSGiuliano Pochini 		goto dout_exit;
477dd7b254dSGiuliano Pochini 
478549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
479dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_CHANNELS,
480dd7b254dSGiuliano Pochini 				  hw_rule_playback_channels_by_format,
481dd7b254dSGiuliano Pochini 				  NULL, SNDRV_PCM_HW_PARAM_FORMAT,
482549717fcSTakashi Iwai 				  -1);
483549717fcSTakashi Iwai 	if (err < 0)
484dd7b254dSGiuliano Pochini 		goto dout_exit;
485549717fcSTakashi Iwai 	err = snd_pcm_hw_rule_add(substream->runtime, 0,
486dd7b254dSGiuliano Pochini 				  SNDRV_PCM_HW_PARAM_FORMAT,
487dd7b254dSGiuliano Pochini 				  hw_rule_playback_format_by_channels,
488dd7b254dSGiuliano Pochini 				  NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
489549717fcSTakashi Iwai 				  -1);
490549717fcSTakashi Iwai 	if (err < 0)
491dd7b254dSGiuliano Pochini 		goto dout_exit;
492027c7002SMark Hills 
493dd7b254dSGiuliano Pochini dout_exit:
494befceea9STakashi Iwai 	mutex_unlock(&chip->mode_mutex);
495dd7b254dSGiuliano Pochini 	return err;
496dd7b254dSGiuliano Pochini }
497dd7b254dSGiuliano Pochini 
498dd7b254dSGiuliano Pochini #endif /* !ECHOCARD_HAS_VMIXER */
499dd7b254dSGiuliano Pochini 
500dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
501dd7b254dSGiuliano Pochini 
502dd7b254dSGiuliano Pochini 
503dd7b254dSGiuliano Pochini 
pcm_close(struct snd_pcm_substream * substream)504dd7b254dSGiuliano Pochini static int pcm_close(struct snd_pcm_substream *substream)
505dd7b254dSGiuliano Pochini {
506dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
507dd7b254dSGiuliano Pochini 
508dd7b254dSGiuliano Pochini 	/* Nothing to do here. Audio is already off and pipe will be
509dd7b254dSGiuliano Pochini 	 * freed by its callback
510dd7b254dSGiuliano Pochini 	 */
511dd7b254dSGiuliano Pochini 
512027c7002SMark Hills 	mutex_lock(&chip->mode_mutex);
513dd7b254dSGiuliano Pochini 
514027c7002SMark Hills 	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
515027c7002SMark Hills 		chip->opencount, chip->can_set_rate, chip->rate_set);
516027c7002SMark Hills 
517027c7002SMark Hills 	chip->opencount--;
518027c7002SMark Hills 
519027c7002SMark Hills 	switch (chip->opencount) {
520027c7002SMark Hills 	case 1:
521027c7002SMark Hills 		chip->can_set_rate = 1;
522027c7002SMark Hills 		break;
523027c7002SMark Hills 
524027c7002SMark Hills 	case 0:
525027c7002SMark Hills 		chip->rate_set = 0;
526027c7002SMark Hills 		break;
527027c7002SMark Hills 	}
528027c7002SMark Hills 
529027c7002SMark Hills 	mutex_unlock(&chip->mode_mutex);
530dd7b254dSGiuliano Pochini 	return 0;
531dd7b254dSGiuliano Pochini }
532dd7b254dSGiuliano Pochini 
533dd7b254dSGiuliano Pochini 
534dd7b254dSGiuliano Pochini 
535dd7b254dSGiuliano Pochini /* Channel allocation and scatter-gather list setup */
init_engine(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params,int pipe_index,int interleave)536dd7b254dSGiuliano Pochini static int init_engine(struct snd_pcm_substream *substream,
537dd7b254dSGiuliano Pochini 		       struct snd_pcm_hw_params *hw_params,
538dd7b254dSGiuliano Pochini 		       int pipe_index, int interleave)
539dd7b254dSGiuliano Pochini {
540dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
541dd7b254dSGiuliano Pochini 	int err, per, rest, page, edge, offs;
542dd7b254dSGiuliano Pochini 	struct audiopipe *pipe;
543dd7b254dSGiuliano Pochini 
544dd7b254dSGiuliano Pochini 	chip = snd_pcm_substream_chip(substream);
545dd7b254dSGiuliano Pochini 	pipe = (struct audiopipe *) substream->runtime->private_data;
546dd7b254dSGiuliano Pochini 
547dd7b254dSGiuliano Pochini 	/* Sets up che hardware. If it's already initialized, reset and
548dd7b254dSGiuliano Pochini 	 * redo with the new parameters
549dd7b254dSGiuliano Pochini 	 */
550dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
551dd7b254dSGiuliano Pochini 	if (pipe->index >= 0) {
552b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
553dd7b254dSGiuliano Pochini 		err = free_pipes(chip, pipe);
554da3cec35STakashi Iwai 		snd_BUG_ON(err);
555dd7b254dSGiuliano Pochini 		chip->substream[pipe->index] = NULL;
556dd7b254dSGiuliano Pochini 	}
557dd7b254dSGiuliano Pochini 
558dd7b254dSGiuliano Pochini 	err = allocate_pipes(chip, pipe, pipe_index, interleave);
559dd7b254dSGiuliano Pochini 	if (err < 0) {
560dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
561b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
562b5b4a41bSSudip Mukherjee 			pipe_index, err);
563dd7b254dSGiuliano Pochini 		return err;
564dd7b254dSGiuliano Pochini 	}
565dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
566b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
567dd7b254dSGiuliano Pochini 
568b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
569b5b4a41bSSudip Mukherjee 		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
570dd7b254dSGiuliano Pochini 		params_buffer_bytes(hw_params), params_periods(hw_params),
571b5b4a41bSSudip Mukherjee 		params_period_bytes(hw_params));
572dd7b254dSGiuliano Pochini 
573dd7b254dSGiuliano Pochini 	sglist_init(chip, pipe);
574dd7b254dSGiuliano Pochini 	edge = PAGE_SIZE;
575dd7b254dSGiuliano Pochini 	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
576dd7b254dSGiuliano Pochini 	     per++) {
577dd7b254dSGiuliano Pochini 		rest = params_period_bytes(hw_params);
578dd7b254dSGiuliano Pochini 		if (offs + rest > params_buffer_bytes(hw_params))
579dd7b254dSGiuliano Pochini 			rest = params_buffer_bytes(hw_params) - offs;
580dd7b254dSGiuliano Pochini 		while (rest) {
58177a23f26STakashi Iwai 			dma_addr_t addr;
58277a23f26STakashi Iwai 			addr = snd_pcm_sgbuf_get_addr(substream, offs);
583dd7b254dSGiuliano Pochini 			if (rest <= edge - offs) {
58477a23f26STakashi Iwai 				sglist_add_mapping(chip, pipe, addr, rest);
585dd7b254dSGiuliano Pochini 				sglist_add_irq(chip, pipe);
586dd7b254dSGiuliano Pochini 				offs += rest;
587dd7b254dSGiuliano Pochini 				rest = 0;
588dd7b254dSGiuliano Pochini 			} else {
58977a23f26STakashi Iwai 				sglist_add_mapping(chip, pipe, addr,
590dd7b254dSGiuliano Pochini 						   edge - offs);
591dd7b254dSGiuliano Pochini 				rest -= edge - offs;
592dd7b254dSGiuliano Pochini 				offs = edge;
593dd7b254dSGiuliano Pochini 			}
594dd7b254dSGiuliano Pochini 			if (offs == edge) {
595dd7b254dSGiuliano Pochini 				edge += PAGE_SIZE;
596dd7b254dSGiuliano Pochini 				page++;
597dd7b254dSGiuliano Pochini 			}
598dd7b254dSGiuliano Pochini 		}
599dd7b254dSGiuliano Pochini 	}
600dd7b254dSGiuliano Pochini 
601dd7b254dSGiuliano Pochini 	/* Close the ring buffer */
602dd7b254dSGiuliano Pochini 	sglist_wrap(chip, pipe);
603dd7b254dSGiuliano Pochini 
604dd7b254dSGiuliano Pochini 	/* This stuff is used by the irq handler, so it must be
605dd7b254dSGiuliano Pochini 	 * initialized before chip->substream
606dd7b254dSGiuliano Pochini 	 */
607a0b224b9SMark Hills 	pipe->last_period = 0;
608dd7b254dSGiuliano Pochini 	pipe->last_counter = 0;
609dd7b254dSGiuliano Pochini 	pipe->position = 0;
610dd7b254dSGiuliano Pochini 	smp_wmb();
611dd7b254dSGiuliano Pochini 	chip->substream[pipe_index] = substream;
612dd7b254dSGiuliano Pochini 	chip->rate_set = 1;
613dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
614dd7b254dSGiuliano Pochini 	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
615dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
616dd7b254dSGiuliano Pochini 	return 0;
617dd7b254dSGiuliano Pochini }
618dd7b254dSGiuliano Pochini 
619dd7b254dSGiuliano Pochini 
620dd7b254dSGiuliano Pochini 
pcm_analog_in_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)621dd7b254dSGiuliano Pochini static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
622dd7b254dSGiuliano Pochini 				   struct snd_pcm_hw_params *hw_params)
623dd7b254dSGiuliano Pochini {
624dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
625dd7b254dSGiuliano Pochini 
626dd7b254dSGiuliano Pochini 	return init_engine(substream, hw_params, px_analog_in(chip) +
627dd7b254dSGiuliano Pochini 			substream->number, params_channels(hw_params));
628dd7b254dSGiuliano Pochini }
629dd7b254dSGiuliano Pochini 
630dd7b254dSGiuliano Pochini 
631dd7b254dSGiuliano Pochini 
pcm_analog_out_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)632dd7b254dSGiuliano Pochini static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
633dd7b254dSGiuliano Pochini 				    struct snd_pcm_hw_params *hw_params)
634dd7b254dSGiuliano Pochini {
635dd7b254dSGiuliano Pochini 	return init_engine(substream, hw_params, substream->number,
636dd7b254dSGiuliano Pochini 			   params_channels(hw_params));
637dd7b254dSGiuliano Pochini }
638dd7b254dSGiuliano Pochini 
639dd7b254dSGiuliano Pochini 
640dd7b254dSGiuliano Pochini 
641dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
642dd7b254dSGiuliano Pochini 
pcm_digital_in_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)643dd7b254dSGiuliano Pochini static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
644dd7b254dSGiuliano Pochini 				    struct snd_pcm_hw_params *hw_params)
645dd7b254dSGiuliano Pochini {
646dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
647dd7b254dSGiuliano Pochini 
648dd7b254dSGiuliano Pochini 	return init_engine(substream, hw_params, px_digital_in(chip) +
649dd7b254dSGiuliano Pochini 			substream->number, params_channels(hw_params));
650dd7b254dSGiuliano Pochini }
651dd7b254dSGiuliano Pochini 
652dd7b254dSGiuliano Pochini 
653dd7b254dSGiuliano Pochini 
654dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
pcm_digital_out_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)655dd7b254dSGiuliano Pochini static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
656dd7b254dSGiuliano Pochini 				     struct snd_pcm_hw_params *hw_params)
657dd7b254dSGiuliano Pochini {
658dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
659dd7b254dSGiuliano Pochini 
660dd7b254dSGiuliano Pochini 	return init_engine(substream, hw_params, px_digital_out(chip) +
661dd7b254dSGiuliano Pochini 			substream->number, params_channels(hw_params));
662dd7b254dSGiuliano Pochini }
663dd7b254dSGiuliano Pochini #endif /* !ECHOCARD_HAS_VMIXER */
664dd7b254dSGiuliano Pochini 
665dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
666dd7b254dSGiuliano Pochini 
667dd7b254dSGiuliano Pochini 
668dd7b254dSGiuliano Pochini 
pcm_hw_free(struct snd_pcm_substream * substream)669dd7b254dSGiuliano Pochini static int pcm_hw_free(struct snd_pcm_substream *substream)
670dd7b254dSGiuliano Pochini {
671dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
672dd7b254dSGiuliano Pochini 	struct audiopipe *pipe;
673dd7b254dSGiuliano Pochini 
674dd7b254dSGiuliano Pochini 	chip = snd_pcm_substream_chip(substream);
675dd7b254dSGiuliano Pochini 	pipe = (struct audiopipe *) substream->runtime->private_data;
676dd7b254dSGiuliano Pochini 
677dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
678dd7b254dSGiuliano Pochini 	if (pipe->index >= 0) {
679b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
680dd7b254dSGiuliano Pochini 		free_pipes(chip, pipe);
681dd7b254dSGiuliano Pochini 		chip->substream[pipe->index] = NULL;
682dd7b254dSGiuliano Pochini 		pipe->index = -1;
683dd7b254dSGiuliano Pochini 	}
684dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
685dd7b254dSGiuliano Pochini 
686dd7b254dSGiuliano Pochini 	return 0;
687dd7b254dSGiuliano Pochini }
688dd7b254dSGiuliano Pochini 
689dd7b254dSGiuliano Pochini 
690dd7b254dSGiuliano Pochini 
pcm_prepare(struct snd_pcm_substream * substream)691dd7b254dSGiuliano Pochini static int pcm_prepare(struct snd_pcm_substream *substream)
692dd7b254dSGiuliano Pochini {
693dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
694dd7b254dSGiuliano Pochini 	struct snd_pcm_runtime *runtime = substream->runtime;
695dd7b254dSGiuliano Pochini 	struct audioformat format;
696dd7b254dSGiuliano Pochini 	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
697dd7b254dSGiuliano Pochini 
698b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
699b5b4a41bSSudip Mukherjee 		runtime->rate, runtime->format, runtime->channels);
700dd7b254dSGiuliano Pochini 	format.interleave = runtime->channels;
701dd7b254dSGiuliano Pochini 	format.data_are_bigendian = 0;
702dd7b254dSGiuliano Pochini 	format.mono_to_stereo = 0;
703dd7b254dSGiuliano Pochini 	switch (runtime->format) {
704dd7b254dSGiuliano Pochini 	case SNDRV_PCM_FORMAT_U8:
705dd7b254dSGiuliano Pochini 		format.bits_per_sample = 8;
706dd7b254dSGiuliano Pochini 		break;
707dd7b254dSGiuliano Pochini 	case SNDRV_PCM_FORMAT_S16_LE:
708dd7b254dSGiuliano Pochini 		format.bits_per_sample = 16;
709dd7b254dSGiuliano Pochini 		break;
710dd7b254dSGiuliano Pochini 	case SNDRV_PCM_FORMAT_S24_3LE:
711dd7b254dSGiuliano Pochini 		format.bits_per_sample = 24;
712dd7b254dSGiuliano Pochini 		break;
713dd7b254dSGiuliano Pochini 	case SNDRV_PCM_FORMAT_S32_BE:
714dd7b254dSGiuliano Pochini 		format.data_are_bigendian = 1;
715c0dbbdadSGustavo A. R. Silva 		fallthrough;
716dd7b254dSGiuliano Pochini 	case SNDRV_PCM_FORMAT_S32_LE:
717dd7b254dSGiuliano Pochini 		format.bits_per_sample = 32;
718dd7b254dSGiuliano Pochini 		break;
719dd7b254dSGiuliano Pochini 	default:
720b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
721b5b4a41bSSudip Mukherjee 			"Prepare error: unsupported format %d\n",
722b5b4a41bSSudip Mukherjee 			runtime->format);
723dd7b254dSGiuliano Pochini 		return -EINVAL;
724dd7b254dSGiuliano Pochini 	}
725dd7b254dSGiuliano Pochini 
726da3cec35STakashi Iwai 	if (snd_BUG_ON(pipe_index >= px_num(chip)))
727da3cec35STakashi Iwai 		return -EINVAL;
7286c331254SMark Hills 
7296c331254SMark Hills 	/*
7306c331254SMark Hills 	 * We passed checks we can do independently; now take
7316c331254SMark Hills 	 * exclusive control
7326c331254SMark Hills 	 */
7336c331254SMark Hills 
7346c331254SMark Hills 	spin_lock_irq(&chip->lock);
7356c331254SMark Hills 
7366c331254SMark Hills 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
737b91c9cb9SDan Carpenter 		spin_unlock_irq(&chip->lock);
738da3cec35STakashi Iwai 		return -EINVAL;
7396c331254SMark Hills 	}
7406c331254SMark Hills 
741dd7b254dSGiuliano Pochini 	set_audio_format(chip, pipe_index, &format);
7426c331254SMark Hills 	spin_unlock_irq(&chip->lock);
7436c331254SMark Hills 
744dd7b254dSGiuliano Pochini 	return 0;
745dd7b254dSGiuliano Pochini }
746dd7b254dSGiuliano Pochini 
747dd7b254dSGiuliano Pochini 
748dd7b254dSGiuliano Pochini 
pcm_trigger(struct snd_pcm_substream * substream,int cmd)749dd7b254dSGiuliano Pochini static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
750dd7b254dSGiuliano Pochini {
751dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_pcm_substream_chip(substream);
7520bc66fd3SColin Ian King 	struct audiopipe *pipe;
753dd7b254dSGiuliano Pochini 	int i, err;
754dd7b254dSGiuliano Pochini 	u32 channelmask = 0;
755dd7b254dSGiuliano Pochini 	struct snd_pcm_substream *s;
756dd7b254dSGiuliano Pochini 
757ef991b95STakashi Iwai 	snd_pcm_group_for_each_entry(s, substream) {
758dd7b254dSGiuliano Pochini 		for (i = 0; i < DSP_MAXPIPES; i++) {
759dd7b254dSGiuliano Pochini 			if (s == chip->substream[i]) {
760dd7b254dSGiuliano Pochini 				channelmask |= 1 << i;
761dd7b254dSGiuliano Pochini 				snd_pcm_trigger_done(s, substream);
762dd7b254dSGiuliano Pochini 			}
763dd7b254dSGiuliano Pochini 		}
764dd7b254dSGiuliano Pochini 	}
765dd7b254dSGiuliano Pochini 
766dd7b254dSGiuliano Pochini 	spin_lock(&chip->lock);
767dd7b254dSGiuliano Pochini 	switch (cmd) {
76847b5d028SGiuliano Pochini 	case SNDRV_PCM_TRIGGER_RESUME:
769dd7b254dSGiuliano Pochini 	case SNDRV_PCM_TRIGGER_START:
770dd7b254dSGiuliano Pochini 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
771dd7b254dSGiuliano Pochini 		for (i = 0; i < DSP_MAXPIPES; i++) {
772dd7b254dSGiuliano Pochini 			if (channelmask & (1 << i)) {
773dd7b254dSGiuliano Pochini 				pipe = chip->substream[i]->runtime->private_data;
774dd7b254dSGiuliano Pochini 				switch (pipe->state) {
775dd7b254dSGiuliano Pochini 				case PIPE_STATE_STOPPED:
776a0b224b9SMark Hills 					pipe->last_period = 0;
777dd7b254dSGiuliano Pochini 					pipe->last_counter = 0;
778dd7b254dSGiuliano Pochini 					pipe->position = 0;
779dd7b254dSGiuliano Pochini 					*pipe->dma_counter = 0;
780c0dbbdadSGustavo A. R. Silva 					fallthrough;
781dd7b254dSGiuliano Pochini 				case PIPE_STATE_PAUSED:
782dd7b254dSGiuliano Pochini 					pipe->state = PIPE_STATE_STARTED;
783dd7b254dSGiuliano Pochini 					break;
784dd7b254dSGiuliano Pochini 				case PIPE_STATE_STARTED:
785dd7b254dSGiuliano Pochini 					break;
786dd7b254dSGiuliano Pochini 				}
787dd7b254dSGiuliano Pochini 			}
788dd7b254dSGiuliano Pochini 		}
789dd7b254dSGiuliano Pochini 		err = start_transport(chip, channelmask,
790dd7b254dSGiuliano Pochini 				      chip->pipe_cyclic_mask);
791dd7b254dSGiuliano Pochini 		break;
79247b5d028SGiuliano Pochini 	case SNDRV_PCM_TRIGGER_SUSPEND:
793dd7b254dSGiuliano Pochini 	case SNDRV_PCM_TRIGGER_STOP:
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:
803dd7b254dSGiuliano Pochini 		for (i = 0; i < DSP_MAXPIPES; i++) {
804dd7b254dSGiuliano Pochini 			if (channelmask & (1 << i)) {
805dd7b254dSGiuliano Pochini 				pipe = chip->substream[i]->runtime->private_data;
806dd7b254dSGiuliano Pochini 				pipe->state = PIPE_STATE_PAUSED;
807dd7b254dSGiuliano Pochini 			}
808dd7b254dSGiuliano Pochini 		}
809dd7b254dSGiuliano Pochini 		err = pause_transport(chip, channelmask);
810dd7b254dSGiuliano Pochini 		break;
811dd7b254dSGiuliano Pochini 	default:
812dd7b254dSGiuliano Pochini 		err = -EINVAL;
813dd7b254dSGiuliano Pochini 	}
814dd7b254dSGiuliano Pochini 	spin_unlock(&chip->lock);
815dd7b254dSGiuliano Pochini 	return err;
816dd7b254dSGiuliano Pochini }
817dd7b254dSGiuliano Pochini 
818dd7b254dSGiuliano Pochini 
819dd7b254dSGiuliano Pochini 
pcm_pointer(struct snd_pcm_substream * substream)820dd7b254dSGiuliano Pochini static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
821dd7b254dSGiuliano Pochini {
822dd7b254dSGiuliano Pochini 	struct snd_pcm_runtime *runtime = substream->runtime;
823dd7b254dSGiuliano Pochini 	struct audiopipe *pipe = runtime->private_data;
824a0b224b9SMark Hills 	u32 counter, step;
825dd7b254dSGiuliano Pochini 
826a0b224b9SMark Hills 	/*
827a0b224b9SMark Hills 	 * IRQ handling runs concurrently. Do not share tracking of
828a0b224b9SMark Hills 	 * counter with it, which would race or require locking
829a0b224b9SMark Hills 	 */
830dd7b254dSGiuliano Pochini 
831a0b224b9SMark Hills 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
832a0b224b9SMark Hills 
833a0b224b9SMark Hills 	step = counter - pipe->last_counter;  /* handles wrapping */
834a0b224b9SMark Hills 	pipe->last_counter = counter;
835a0b224b9SMark Hills 
836a0b224b9SMark Hills 	/* counter doesn't neccessarily wrap on a multiple of
837a0b224b9SMark Hills 	 * buffer_size, so can't derive the position; must
838a0b224b9SMark Hills 	 * accumulate */
839a0b224b9SMark Hills 
840a0b224b9SMark Hills 	pipe->position += step;
841a0b224b9SMark Hills 	pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
842a0b224b9SMark Hills 
843a0b224b9SMark Hills 	return bytes_to_frames(runtime, pipe->position);
844dd7b254dSGiuliano Pochini }
845dd7b254dSGiuliano Pochini 
846dd7b254dSGiuliano Pochini 
847dd7b254dSGiuliano Pochini 
848dd7b254dSGiuliano Pochini /* pcm *_ops structures */
849afa04880SArvind Yadav static const struct snd_pcm_ops analog_playback_ops = {
850dd7b254dSGiuliano Pochini 	.open = pcm_analog_out_open,
851dd7b254dSGiuliano Pochini 	.close = pcm_close,
852dd7b254dSGiuliano Pochini 	.hw_params = pcm_analog_out_hw_params,
853dd7b254dSGiuliano Pochini 	.hw_free = pcm_hw_free,
854dd7b254dSGiuliano Pochini 	.prepare = pcm_prepare,
855dd7b254dSGiuliano Pochini 	.trigger = pcm_trigger,
856dd7b254dSGiuliano Pochini 	.pointer = pcm_pointer,
857dd7b254dSGiuliano Pochini };
858afa04880SArvind Yadav static const struct snd_pcm_ops analog_capture_ops = {
859dd7b254dSGiuliano Pochini 	.open = pcm_analog_in_open,
860dd7b254dSGiuliano Pochini 	.close = pcm_close,
861dd7b254dSGiuliano Pochini 	.hw_params = pcm_analog_in_hw_params,
862dd7b254dSGiuliano Pochini 	.hw_free = pcm_hw_free,
863dd7b254dSGiuliano Pochini 	.prepare = pcm_prepare,
864dd7b254dSGiuliano Pochini 	.trigger = pcm_trigger,
865dd7b254dSGiuliano Pochini 	.pointer = pcm_pointer,
866dd7b254dSGiuliano Pochini };
867dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
868dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_VMIXER
869afa04880SArvind Yadav static const struct snd_pcm_ops digital_playback_ops = {
870dd7b254dSGiuliano Pochini 	.open = pcm_digital_out_open,
871dd7b254dSGiuliano Pochini 	.close = pcm_close,
872dd7b254dSGiuliano Pochini 	.hw_params = pcm_digital_out_hw_params,
873dd7b254dSGiuliano Pochini 	.hw_free = pcm_hw_free,
874dd7b254dSGiuliano Pochini 	.prepare = pcm_prepare,
875dd7b254dSGiuliano Pochini 	.trigger = pcm_trigger,
876dd7b254dSGiuliano Pochini 	.pointer = pcm_pointer,
877dd7b254dSGiuliano Pochini };
878dd7b254dSGiuliano Pochini #endif /* !ECHOCARD_HAS_VMIXER */
879afa04880SArvind Yadav static const struct snd_pcm_ops digital_capture_ops = {
880dd7b254dSGiuliano Pochini 	.open = pcm_digital_in_open,
881dd7b254dSGiuliano Pochini 	.close = pcm_close,
882dd7b254dSGiuliano Pochini 	.hw_params = pcm_digital_in_hw_params,
883dd7b254dSGiuliano Pochini 	.hw_free = pcm_hw_free,
884dd7b254dSGiuliano Pochini 	.prepare = pcm_prepare,
885dd7b254dSGiuliano Pochini 	.trigger = pcm_trigger,
886dd7b254dSGiuliano Pochini 	.pointer = pcm_pointer,
887dd7b254dSGiuliano Pochini };
888dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
889dd7b254dSGiuliano Pochini 
890dd7b254dSGiuliano Pochini 
891dd7b254dSGiuliano Pochini 
892dd7b254dSGiuliano Pochini /* Preallocate memory only for the first substream because it's the most
893dd7b254dSGiuliano Pochini  * used one
894dd7b254dSGiuliano Pochini  */
snd_echo_preallocate_pages(struct snd_pcm * pcm,struct device * dev)8957564d3b6STakashi Iwai static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
896dd7b254dSGiuliano Pochini {
897dd7b254dSGiuliano Pochini 	struct snd_pcm_substream *ss;
8985116b94aSTakashi Iwai 	int stream;
899dd7b254dSGiuliano Pochini 
900dd7b254dSGiuliano Pochini 	for (stream = 0; stream < 2; stream++)
9015116b94aSTakashi Iwai 		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
90211f63ca3STakashi Iwai 			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
903dd7b254dSGiuliano Pochini 						   dev,
904dd7b254dSGiuliano Pochini 						   ss->number ? 0 : 128<<10,
905dd7b254dSGiuliano Pochini 						   256<<10);
906dd7b254dSGiuliano Pochini }
907dd7b254dSGiuliano Pochini 
908dd7b254dSGiuliano Pochini 
909dd7b254dSGiuliano Pochini 
910dd7b254dSGiuliano Pochini /*<--snd_echo_probe() */
snd_echo_new_pcm(struct echoaudio * chip)911e23e7a14SBill Pemberton static int snd_echo_new_pcm(struct echoaudio *chip)
912dd7b254dSGiuliano Pochini {
913dd7b254dSGiuliano Pochini 	struct snd_pcm *pcm;
914dd7b254dSGiuliano Pochini 	int err;
915dd7b254dSGiuliano Pochini 
916dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
917dd7b254dSGiuliano Pochini 	/* This card has a Vmixer, that is there is no direct mapping from PCM
918dd7b254dSGiuliano Pochini 	streams to physical outputs. The user can mix the streams as he wishes
919dd7b254dSGiuliano Pochini 	via control interface and it's possible to send any stream to any
920dd7b254dSGiuliano Pochini 	output, thus it makes no sense to keep analog and digital outputs
921dd7b254dSGiuliano Pochini 	separated */
922dd7b254dSGiuliano Pochini 
923dd7b254dSGiuliano Pochini 	/* PCM#0 Virtual outputs and analog inputs */
924549717fcSTakashi Iwai 	err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
925549717fcSTakashi Iwai 			  num_analog_busses_in(chip), &pcm);
926549717fcSTakashi Iwai 	if (err < 0)
927dd7b254dSGiuliano Pochini 		return err;
928dd7b254dSGiuliano Pochini 	pcm->private_data = chip;
929dd7b254dSGiuliano Pochini 	chip->analog_pcm = pcm;
930dd7b254dSGiuliano Pochini 	strcpy(pcm->name, chip->card->shortname);
931dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
932dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
9337564d3b6STakashi Iwai 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
934dd7b254dSGiuliano Pochini 
935dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
936dd7b254dSGiuliano Pochini 	/* PCM#1 Digital inputs, no outputs */
937549717fcSTakashi Iwai 	err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
938549717fcSTakashi Iwai 			  num_digital_busses_in(chip), &pcm);
939549717fcSTakashi Iwai 	if (err < 0)
940dd7b254dSGiuliano Pochini 		return err;
941dd7b254dSGiuliano Pochini 	pcm->private_data = chip;
942dd7b254dSGiuliano Pochini 	chip->digital_pcm = pcm;
943dd7b254dSGiuliano Pochini 	strcpy(pcm->name, chip->card->shortname);
944dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
9457564d3b6STakashi Iwai 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
946dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
947dd7b254dSGiuliano Pochini 
948dd7b254dSGiuliano Pochini #else /* ECHOCARD_HAS_VMIXER */
949dd7b254dSGiuliano Pochini 
950dd7b254dSGiuliano Pochini 	/* The card can manage substreams formed by analog and digital channels
951dd7b254dSGiuliano Pochini 	at the same time, but I prefer to keep analog and digital channels
952dd7b254dSGiuliano Pochini 	separated, because that mixed thing is confusing and useless. So we
953dd7b254dSGiuliano Pochini 	register two PCM devices: */
954dd7b254dSGiuliano Pochini 
955dd7b254dSGiuliano Pochini 	/* PCM#0 Analog i/o */
956549717fcSTakashi Iwai 	err = snd_pcm_new(chip->card, "Analog PCM", 0,
957dd7b254dSGiuliano Pochini 			  num_analog_busses_out(chip),
958549717fcSTakashi Iwai 			  num_analog_busses_in(chip), &pcm);
959549717fcSTakashi Iwai 	if (err < 0)
960dd7b254dSGiuliano Pochini 		return err;
961dd7b254dSGiuliano Pochini 	pcm->private_data = chip;
962dd7b254dSGiuliano Pochini 	chip->analog_pcm = pcm;
963dd7b254dSGiuliano Pochini 	strcpy(pcm->name, chip->card->shortname);
964dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
965dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
9667564d3b6STakashi Iwai 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
967dd7b254dSGiuliano Pochini 
968dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
969dd7b254dSGiuliano Pochini 	/* PCM#1 Digital i/o */
970549717fcSTakashi Iwai 	err = snd_pcm_new(chip->card, "Digital PCM", 1,
971dd7b254dSGiuliano Pochini 			  num_digital_busses_out(chip),
972549717fcSTakashi Iwai 			  num_digital_busses_in(chip), &pcm);
973549717fcSTakashi Iwai 	if (err < 0)
974dd7b254dSGiuliano Pochini 		return err;
975dd7b254dSGiuliano Pochini 	pcm->private_data = chip;
976dd7b254dSGiuliano Pochini 	chip->digital_pcm = pcm;
977dd7b254dSGiuliano Pochini 	strcpy(pcm->name, chip->card->shortname);
978dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
979dd7b254dSGiuliano Pochini 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
9807564d3b6STakashi Iwai 	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
981dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
982dd7b254dSGiuliano Pochini 
983dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_VMIXER */
984dd7b254dSGiuliano Pochini 
985dd7b254dSGiuliano Pochini 	return 0;
986dd7b254dSGiuliano Pochini }
987dd7b254dSGiuliano Pochini 
988dd7b254dSGiuliano Pochini 
989dd7b254dSGiuliano Pochini 
990dd7b254dSGiuliano Pochini 
991dd7b254dSGiuliano Pochini /******************************************************************************
992dd7b254dSGiuliano Pochini 	Control interface
993dd7b254dSGiuliano Pochini ******************************************************************************/
994dd7b254dSGiuliano Pochini 
995392bf2f1SGiuliano Pochini #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
9969f5d790dSGiuliano Pochini 
997dd7b254dSGiuliano Pochini /******************* PCM output volume *******************/
snd_echo_output_gain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)998dd7b254dSGiuliano Pochini static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
999dd7b254dSGiuliano Pochini 				     struct snd_ctl_elem_info *uinfo)
1000dd7b254dSGiuliano Pochini {
1001dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1002dd7b254dSGiuliano Pochini 
1003dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1004dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1005dd7b254dSGiuliano Pochini 	uinfo->count = num_busses_out(chip);
1006dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1007dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1008dd7b254dSGiuliano Pochini 	return 0;
1009dd7b254dSGiuliano Pochini }
1010dd7b254dSGiuliano Pochini 
snd_echo_output_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1011dd7b254dSGiuliano Pochini static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1012dd7b254dSGiuliano Pochini 				    struct snd_ctl_elem_value *ucontrol)
1013dd7b254dSGiuliano Pochini {
1014dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1015dd7b254dSGiuliano Pochini 	int c;
1016dd7b254dSGiuliano Pochini 
1017dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1018dd7b254dSGiuliano Pochini 	for (c = 0; c < num_busses_out(chip); c++)
1019dd7b254dSGiuliano Pochini 		ucontrol->value.integer.value[c] = chip->output_gain[c];
1020dd7b254dSGiuliano Pochini 	return 0;
1021dd7b254dSGiuliano Pochini }
1022dd7b254dSGiuliano Pochini 
snd_echo_output_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1023dd7b254dSGiuliano Pochini static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1024dd7b254dSGiuliano Pochini 				    struct snd_ctl_elem_value *ucontrol)
1025dd7b254dSGiuliano Pochini {
1026dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1027dd7b254dSGiuliano Pochini 	int c, changed, gain;
1028dd7b254dSGiuliano Pochini 
1029dd7b254dSGiuliano Pochini 	changed = 0;
1030dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1031dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
1032dd7b254dSGiuliano Pochini 	for (c = 0; c < num_busses_out(chip); c++) {
1033dd7b254dSGiuliano Pochini 		gain = ucontrol->value.integer.value[c];
1034dd7b254dSGiuliano Pochini 		/* Ignore out of range values */
1035dd7b254dSGiuliano Pochini 		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1036dd7b254dSGiuliano Pochini 			continue;
1037dd7b254dSGiuliano Pochini 		if (chip->output_gain[c] != gain) {
1038dd7b254dSGiuliano Pochini 			set_output_gain(chip, c, gain);
1039dd7b254dSGiuliano Pochini 			changed = 1;
1040dd7b254dSGiuliano Pochini 		}
1041dd7b254dSGiuliano Pochini 	}
1042dd7b254dSGiuliano Pochini 	if (changed)
1043dd7b254dSGiuliano Pochini 		update_output_line_level(chip);
1044dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
1045dd7b254dSGiuliano Pochini 	return changed;
1046dd7b254dSGiuliano Pochini }
1047dd7b254dSGiuliano Pochini 
1048392bf2f1SGiuliano Pochini #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1049392bf2f1SGiuliano Pochini /* On the Mia this one controls the line-out volume */
1050f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1051392bf2f1SGiuliano Pochini 	.name = "Line Playback Volume",
1052392bf2f1SGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1053392bf2f1SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1054392bf2f1SGiuliano Pochini 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1055392bf2f1SGiuliano Pochini 	.info = snd_echo_output_gain_info,
1056392bf2f1SGiuliano Pochini 	.get = snd_echo_output_gain_get,
1057392bf2f1SGiuliano Pochini 	.put = snd_echo_output_gain_put,
1058392bf2f1SGiuliano Pochini 	.tlv = {.p = db_scale_output_gain},
1059392bf2f1SGiuliano Pochini };
1060392bf2f1SGiuliano Pochini #else
1061f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1062dd7b254dSGiuliano Pochini 	.name = "PCM Playback Volume",
1063dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1064048b9450SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1065dd7b254dSGiuliano Pochini 	.info = snd_echo_output_gain_info,
1066dd7b254dSGiuliano Pochini 	.get = snd_echo_output_gain_get,
1067dd7b254dSGiuliano Pochini 	.put = snd_echo_output_gain_put,
1068048b9450SGiuliano Pochini 	.tlv = {.p = db_scale_output_gain},
1069dd7b254dSGiuliano Pochini };
1070dd7b254dSGiuliano Pochini #endif
1071dd7b254dSGiuliano Pochini 
1072392bf2f1SGiuliano Pochini #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1073392bf2f1SGiuliano Pochini 
1074dd7b254dSGiuliano Pochini 
1075dd7b254dSGiuliano Pochini 
1076dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_GAIN
1077dd7b254dSGiuliano Pochini 
1078dd7b254dSGiuliano Pochini /******************* Analog input volume *******************/
snd_echo_input_gain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1079dd7b254dSGiuliano Pochini static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1080dd7b254dSGiuliano Pochini 				    struct snd_ctl_elem_info *uinfo)
1081dd7b254dSGiuliano Pochini {
1082dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1083dd7b254dSGiuliano Pochini 
1084dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1085dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1086dd7b254dSGiuliano Pochini 	uinfo->count = num_analog_busses_in(chip);
1087dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = ECHOGAIN_MININP;
1088dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1089dd7b254dSGiuliano Pochini 	return 0;
1090dd7b254dSGiuliano Pochini }
1091dd7b254dSGiuliano Pochini 
snd_echo_input_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1092dd7b254dSGiuliano Pochini static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1093dd7b254dSGiuliano Pochini 				   struct snd_ctl_elem_value *ucontrol)
1094dd7b254dSGiuliano Pochini {
1095dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1096dd7b254dSGiuliano Pochini 	int c;
1097dd7b254dSGiuliano Pochini 
1098dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1099dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_in(chip); c++)
1100dd7b254dSGiuliano Pochini 		ucontrol->value.integer.value[c] = chip->input_gain[c];
1101dd7b254dSGiuliano Pochini 	return 0;
1102dd7b254dSGiuliano Pochini }
1103dd7b254dSGiuliano Pochini 
snd_echo_input_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1104dd7b254dSGiuliano Pochini static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1105dd7b254dSGiuliano Pochini 				   struct snd_ctl_elem_value *ucontrol)
1106dd7b254dSGiuliano Pochini {
1107dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1108dd7b254dSGiuliano Pochini 	int c, gain, changed;
1109dd7b254dSGiuliano Pochini 
1110dd7b254dSGiuliano Pochini 	changed = 0;
1111dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1112dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
1113dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1114dd7b254dSGiuliano Pochini 		gain = ucontrol->value.integer.value[c];
1115dd7b254dSGiuliano Pochini 		/* Ignore out of range values */
1116dd7b254dSGiuliano Pochini 		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1117dd7b254dSGiuliano Pochini 			continue;
1118dd7b254dSGiuliano Pochini 		if (chip->input_gain[c] != gain) {
1119dd7b254dSGiuliano Pochini 			set_input_gain(chip, c, gain);
1120dd7b254dSGiuliano Pochini 			changed = 1;
1121dd7b254dSGiuliano Pochini 		}
1122dd7b254dSGiuliano Pochini 	}
1123dd7b254dSGiuliano Pochini 	if (changed)
1124dd7b254dSGiuliano Pochini 		update_input_line_level(chip);
1125dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
1126dd7b254dSGiuliano Pochini 	return changed;
1127dd7b254dSGiuliano Pochini }
1128dd7b254dSGiuliano Pochini 
11290cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1130048b9450SGiuliano Pochini 
1131f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1132dd7b254dSGiuliano Pochini 	.name = "Line Capture Volume",
1133dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134048b9450SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1135dd7b254dSGiuliano Pochini 	.info = snd_echo_input_gain_info,
1136dd7b254dSGiuliano Pochini 	.get = snd_echo_input_gain_get,
1137dd7b254dSGiuliano Pochini 	.put = snd_echo_input_gain_put,
1138048b9450SGiuliano Pochini 	.tlv = {.p = db_scale_input_gain},
1139dd7b254dSGiuliano Pochini };
1140dd7b254dSGiuliano Pochini 
1141dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_INPUT_GAIN */
1142dd7b254dSGiuliano Pochini 
1143dd7b254dSGiuliano Pochini 
1144dd7b254dSGiuliano Pochini 
1145dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1146dd7b254dSGiuliano Pochini 
1147dd7b254dSGiuliano Pochini /************ Analog output nominal level (+4dBu / -10dBV) ***************/
snd_echo_output_nominal_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1148dd7b254dSGiuliano Pochini static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1149dd7b254dSGiuliano Pochini 					 struct snd_ctl_elem_info *uinfo)
1150dd7b254dSGiuliano Pochini {
1151dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1152dd7b254dSGiuliano Pochini 
1153dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1154dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1155dd7b254dSGiuliano Pochini 	uinfo->count = num_analog_busses_out(chip);
1156dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = 0;
1157dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = 1;
1158dd7b254dSGiuliano Pochini 	return 0;
1159dd7b254dSGiuliano Pochini }
1160dd7b254dSGiuliano Pochini 
snd_echo_output_nominal_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1161dd7b254dSGiuliano Pochini static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1162dd7b254dSGiuliano Pochini 				       struct snd_ctl_elem_value *ucontrol)
1163dd7b254dSGiuliano Pochini {
1164dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1165dd7b254dSGiuliano Pochini 	int c;
1166dd7b254dSGiuliano Pochini 
1167dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1168dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_out(chip); c++)
1169dd7b254dSGiuliano Pochini 		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1170dd7b254dSGiuliano Pochini 	return 0;
1171dd7b254dSGiuliano Pochini }
1172dd7b254dSGiuliano Pochini 
snd_echo_output_nominal_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1173dd7b254dSGiuliano Pochini static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1174dd7b254dSGiuliano Pochini 				       struct snd_ctl_elem_value *ucontrol)
1175dd7b254dSGiuliano Pochini {
1176dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1177dd7b254dSGiuliano Pochini 	int c, changed;
1178dd7b254dSGiuliano Pochini 
1179dd7b254dSGiuliano Pochini 	changed = 0;
1180dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1181dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
1182dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_out(chip); c++) {
1183dd7b254dSGiuliano Pochini 		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1184dd7b254dSGiuliano Pochini 			set_nominal_level(chip, c,
1185dd7b254dSGiuliano Pochini 					  ucontrol->value.integer.value[c]);
1186dd7b254dSGiuliano Pochini 			changed = 1;
1187dd7b254dSGiuliano Pochini 		}
1188dd7b254dSGiuliano Pochini 	}
1189dd7b254dSGiuliano Pochini 	if (changed)
1190dd7b254dSGiuliano Pochini 		update_output_line_level(chip);
1191dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
1192dd7b254dSGiuliano Pochini 	return changed;
1193dd7b254dSGiuliano Pochini }
1194dd7b254dSGiuliano Pochini 
1195f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1196dd7b254dSGiuliano Pochini 	.name = "Line Playback Switch (-10dBV)",
1197dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1198dd7b254dSGiuliano Pochini 	.info = snd_echo_output_nominal_info,
1199dd7b254dSGiuliano Pochini 	.get = snd_echo_output_nominal_get,
1200dd7b254dSGiuliano Pochini 	.put = snd_echo_output_nominal_put,
1201dd7b254dSGiuliano Pochini };
1202dd7b254dSGiuliano Pochini 
1203dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1204dd7b254dSGiuliano Pochini 
1205dd7b254dSGiuliano Pochini 
1206dd7b254dSGiuliano Pochini 
1207dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1208dd7b254dSGiuliano Pochini 
1209dd7b254dSGiuliano Pochini /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
snd_echo_input_nominal_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1210dd7b254dSGiuliano Pochini static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1211dd7b254dSGiuliano Pochini 				       struct snd_ctl_elem_info *uinfo)
1212dd7b254dSGiuliano Pochini {
1213dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1214dd7b254dSGiuliano Pochini 
1215dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1216dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1217dd7b254dSGiuliano Pochini 	uinfo->count = num_analog_busses_in(chip);
1218dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = 0;
1219dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = 1;
1220dd7b254dSGiuliano Pochini 	return 0;
1221dd7b254dSGiuliano Pochini }
1222dd7b254dSGiuliano Pochini 
snd_echo_input_nominal_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1223dd7b254dSGiuliano Pochini static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1224dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_value *ucontrol)
1225dd7b254dSGiuliano Pochini {
1226dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1227dd7b254dSGiuliano Pochini 	int c;
1228dd7b254dSGiuliano Pochini 
1229dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1230dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_in(chip); c++)
1231dd7b254dSGiuliano Pochini 		ucontrol->value.integer.value[c] =
1232dd7b254dSGiuliano Pochini 			chip->nominal_level[bx_analog_in(chip) + c];
1233dd7b254dSGiuliano Pochini 	return 0;
1234dd7b254dSGiuliano Pochini }
1235dd7b254dSGiuliano Pochini 
snd_echo_input_nominal_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1236dd7b254dSGiuliano Pochini static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1237dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_value *ucontrol)
1238dd7b254dSGiuliano Pochini {
1239dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1240dd7b254dSGiuliano Pochini 	int c, changed;
1241dd7b254dSGiuliano Pochini 
1242dd7b254dSGiuliano Pochini 	changed = 0;
1243dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1244dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
1245dd7b254dSGiuliano Pochini 	for (c = 0; c < num_analog_busses_in(chip); c++) {
1246dd7b254dSGiuliano Pochini 		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1247dd7b254dSGiuliano Pochini 		    ucontrol->value.integer.value[c]) {
1248dd7b254dSGiuliano Pochini 			set_nominal_level(chip, bx_analog_in(chip) + c,
1249dd7b254dSGiuliano Pochini 					  ucontrol->value.integer.value[c]);
1250dd7b254dSGiuliano Pochini 			changed = 1;
1251dd7b254dSGiuliano Pochini 		}
1252dd7b254dSGiuliano Pochini 	}
1253dd7b254dSGiuliano Pochini 	if (changed)
1254dd7b254dSGiuliano Pochini 		update_output_line_level(chip);	/* "Output" is not a mistake
1255dd7b254dSGiuliano Pochini 						 * here.
1256dd7b254dSGiuliano Pochini 						 */
1257dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
1258dd7b254dSGiuliano Pochini 	return changed;
1259dd7b254dSGiuliano Pochini }
1260dd7b254dSGiuliano Pochini 
1261f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1262dd7b254dSGiuliano Pochini 	.name = "Line Capture Switch (-10dBV)",
1263dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1264dd7b254dSGiuliano Pochini 	.info = snd_echo_input_nominal_info,
1265dd7b254dSGiuliano Pochini 	.get = snd_echo_input_nominal_get,
1266dd7b254dSGiuliano Pochini 	.put = snd_echo_input_nominal_put,
1267dd7b254dSGiuliano Pochini };
1268dd7b254dSGiuliano Pochini 
1269dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1270dd7b254dSGiuliano Pochini 
1271dd7b254dSGiuliano Pochini 
1272dd7b254dSGiuliano Pochini 
1273dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
1274dd7b254dSGiuliano Pochini 
1275dd7b254dSGiuliano Pochini /******************* Monitor mixer *******************/
snd_echo_mixer_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1276dd7b254dSGiuliano Pochini static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1277dd7b254dSGiuliano Pochini 			       struct snd_ctl_elem_info *uinfo)
1278dd7b254dSGiuliano Pochini {
1279dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
128051db452dSTakashi Sakamoto 	uinfo->count = 1;
1281dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1282dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1283dd7b254dSGiuliano Pochini 	return 0;
1284dd7b254dSGiuliano Pochini }
1285dd7b254dSGiuliano Pochini 
snd_echo_mixer_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1286dd7b254dSGiuliano Pochini static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1287dd7b254dSGiuliano Pochini 			      struct snd_ctl_elem_value *ucontrol)
1288dd7b254dSGiuliano Pochini {
128977008b70SDan Carpenter 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
129077008b70SDan Carpenter 	unsigned int out = ucontrol->id.index / num_busses_in(chip);
129177008b70SDan Carpenter 	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1292dd7b254dSGiuliano Pochini 
129377008b70SDan Carpenter 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
129477008b70SDan Carpenter 		return -EINVAL;
129577008b70SDan Carpenter 
129677008b70SDan Carpenter 	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1297dd7b254dSGiuliano Pochini 	return 0;
1298dd7b254dSGiuliano Pochini }
1299dd7b254dSGiuliano Pochini 
snd_echo_mixer_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1300dd7b254dSGiuliano Pochini static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1301dd7b254dSGiuliano Pochini 			      struct snd_ctl_elem_value *ucontrol)
1302dd7b254dSGiuliano Pochini {
1303dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1304dd7b254dSGiuliano Pochini 	int changed,  gain;
130577008b70SDan Carpenter 	unsigned int out, in;
1306dd7b254dSGiuliano Pochini 
1307dd7b254dSGiuliano Pochini 	changed = 0;
1308dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1309dd7b254dSGiuliano Pochini 	out = ucontrol->id.index / num_busses_in(chip);
1310dd7b254dSGiuliano Pochini 	in = ucontrol->id.index % num_busses_in(chip);
131177008b70SDan Carpenter 	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
131277008b70SDan Carpenter 		return -EINVAL;
1313dd7b254dSGiuliano Pochini 	gain = ucontrol->value.integer.value[0];
1314dd7b254dSGiuliano Pochini 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1315dd7b254dSGiuliano Pochini 		return -EINVAL;
1316dd7b254dSGiuliano Pochini 	if (chip->monitor_gain[out][in] != gain) {
1317dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1318dd7b254dSGiuliano Pochini 		set_monitor_gain(chip, out, in, gain);
1319dd7b254dSGiuliano Pochini 		update_output_line_level(chip);
1320dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1321dd7b254dSGiuliano Pochini 		changed = 1;
1322dd7b254dSGiuliano Pochini 	}
1323dd7b254dSGiuliano Pochini 	return changed;
1324dd7b254dSGiuliano Pochini }
1325dd7b254dSGiuliano Pochini 
1326e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1327dd7b254dSGiuliano Pochini 	.name = "Monitor Mixer Volume",
1328dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1329048b9450SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1330dd7b254dSGiuliano Pochini 	.info = snd_echo_mixer_info,
1331dd7b254dSGiuliano Pochini 	.get = snd_echo_mixer_get,
1332dd7b254dSGiuliano Pochini 	.put = snd_echo_mixer_put,
1333048b9450SGiuliano Pochini 	.tlv = {.p = db_scale_output_gain},
1334dd7b254dSGiuliano Pochini };
1335dd7b254dSGiuliano Pochini 
1336dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_MONITOR */
1337dd7b254dSGiuliano Pochini 
1338dd7b254dSGiuliano Pochini 
1339dd7b254dSGiuliano Pochini 
1340dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
1341dd7b254dSGiuliano Pochini 
1342dd7b254dSGiuliano Pochini /******************* Vmixer *******************/
snd_echo_vmixer_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1343dd7b254dSGiuliano Pochini static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1344dd7b254dSGiuliano Pochini 				struct snd_ctl_elem_info *uinfo)
1345dd7b254dSGiuliano Pochini {
1346dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
134751db452dSTakashi Sakamoto 	uinfo->count = 1;
1348dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1349dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1350dd7b254dSGiuliano Pochini 	return 0;
1351dd7b254dSGiuliano Pochini }
1352dd7b254dSGiuliano Pochini 
snd_echo_vmixer_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1353dd7b254dSGiuliano Pochini static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1354dd7b254dSGiuliano Pochini 			       struct snd_ctl_elem_value *ucontrol)
1355dd7b254dSGiuliano Pochini {
1356dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1357dd7b254dSGiuliano Pochini 
1358dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1359dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[0] =
1360dd7b254dSGiuliano Pochini 		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1361dd7b254dSGiuliano Pochini 			[ucontrol->id.index % num_pipes_out(chip)];
1362dd7b254dSGiuliano Pochini 	return 0;
1363dd7b254dSGiuliano Pochini }
1364dd7b254dSGiuliano Pochini 
snd_echo_vmixer_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1365dd7b254dSGiuliano Pochini static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1366dd7b254dSGiuliano Pochini 			       struct snd_ctl_elem_value *ucontrol)
1367dd7b254dSGiuliano Pochini {
1368dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1369dd7b254dSGiuliano Pochini 	int gain, changed;
1370dd7b254dSGiuliano Pochini 	short vch, out;
1371dd7b254dSGiuliano Pochini 
1372dd7b254dSGiuliano Pochini 	changed = 0;
1373dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1374dd7b254dSGiuliano Pochini 	out = ucontrol->id.index / num_pipes_out(chip);
1375dd7b254dSGiuliano Pochini 	vch = ucontrol->id.index % num_pipes_out(chip);
1376dd7b254dSGiuliano Pochini 	gain = ucontrol->value.integer.value[0];
1377dd7b254dSGiuliano Pochini 	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1378dd7b254dSGiuliano Pochini 		return -EINVAL;
1379dd7b254dSGiuliano Pochini 	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1380dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1381dd7b254dSGiuliano Pochini 		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1382dd7b254dSGiuliano Pochini 		update_vmixer_level(chip);
1383dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1384dd7b254dSGiuliano Pochini 		changed = 1;
1385dd7b254dSGiuliano Pochini 	}
1386dd7b254dSGiuliano Pochini 	return changed;
1387dd7b254dSGiuliano Pochini }
1388dd7b254dSGiuliano Pochini 
1389e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_echo_vmixer = {
1390dd7b254dSGiuliano Pochini 	.name = "VMixer Volume",
1391dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1392048b9450SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1393dd7b254dSGiuliano Pochini 	.info = snd_echo_vmixer_info,
1394dd7b254dSGiuliano Pochini 	.get = snd_echo_vmixer_get,
1395dd7b254dSGiuliano Pochini 	.put = snd_echo_vmixer_put,
1396048b9450SGiuliano Pochini 	.tlv = {.p = db_scale_output_gain},
1397dd7b254dSGiuliano Pochini };
1398dd7b254dSGiuliano Pochini 
1399dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_VMIXER */
1400dd7b254dSGiuliano Pochini 
1401dd7b254dSGiuliano Pochini 
1402dd7b254dSGiuliano Pochini 
1403dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1404dd7b254dSGiuliano Pochini 
1405dd7b254dSGiuliano Pochini /******************* Digital mode switch *******************/
snd_echo_digital_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1406dd7b254dSGiuliano Pochini static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1407dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_info *uinfo)
1408dd7b254dSGiuliano Pochini {
1409c69a4f30STakashi Iwai 	static const char * const names[4] = {
1410dd7b254dSGiuliano Pochini 		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1411dd7b254dSGiuliano Pochini 		"S/PDIF Cdrom"
1412dd7b254dSGiuliano Pochini 	};
1413dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1414dd7b254dSGiuliano Pochini 
1415dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1416c69a4f30STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1417dd7b254dSGiuliano Pochini }
1418dd7b254dSGiuliano Pochini 
snd_echo_digital_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1419dd7b254dSGiuliano Pochini static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1420dd7b254dSGiuliano Pochini 				     struct snd_ctl_elem_value *ucontrol)
1421dd7b254dSGiuliano Pochini {
1422dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1423dd7b254dSGiuliano Pochini 	int i, mode;
1424dd7b254dSGiuliano Pochini 
1425dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1426dd7b254dSGiuliano Pochini 	mode = chip->digital_mode;
1427dd7b254dSGiuliano Pochini 	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1428dd7b254dSGiuliano Pochini 		if (mode == chip->digital_mode_list[i]) {
1429dd7b254dSGiuliano Pochini 			ucontrol->value.enumerated.item[0] = i;
1430dd7b254dSGiuliano Pochini 			break;
1431dd7b254dSGiuliano Pochini 		}
1432dd7b254dSGiuliano Pochini 	return 0;
1433dd7b254dSGiuliano Pochini }
1434dd7b254dSGiuliano Pochini 
snd_echo_digital_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1435dd7b254dSGiuliano Pochini static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1436dd7b254dSGiuliano Pochini 				     struct snd_ctl_elem_value *ucontrol)
1437dd7b254dSGiuliano Pochini {
1438dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1439dd7b254dSGiuliano Pochini 	int changed;
1440dd7b254dSGiuliano Pochini 	unsigned short emode, dmode;
1441dd7b254dSGiuliano Pochini 
1442dd7b254dSGiuliano Pochini 	changed = 0;
1443dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1444dd7b254dSGiuliano Pochini 
1445dd7b254dSGiuliano Pochini 	emode = ucontrol->value.enumerated.item[0];
1446dd7b254dSGiuliano Pochini 	if (emode >= chip->num_digital_modes)
1447dd7b254dSGiuliano Pochini 		return -EINVAL;
1448dd7b254dSGiuliano Pochini 	dmode = chip->digital_mode_list[emode];
1449dd7b254dSGiuliano Pochini 
1450dd7b254dSGiuliano Pochini 	if (dmode != chip->digital_mode) {
1451dd7b254dSGiuliano Pochini 		/* mode_mutex is required to make this operation atomic wrt
1452dd7b254dSGiuliano Pochini 		pcm_digital_*_open() and set_input_clock() functions. */
1453befceea9STakashi Iwai 		mutex_lock(&chip->mode_mutex);
1454dd7b254dSGiuliano Pochini 
1455dd7b254dSGiuliano Pochini 		/* Do not allow the user to change the digital mode when a pcm
1456dd7b254dSGiuliano Pochini 		device is open because it also changes the number of channels
1457dd7b254dSGiuliano Pochini 		and the allowed sample rates */
1458027c7002SMark Hills 		if (chip->opencount) {
1459dd7b254dSGiuliano Pochini 			changed = -EAGAIN;
1460dd7b254dSGiuliano Pochini 		} else {
1461dd7b254dSGiuliano Pochini 			changed = set_digital_mode(chip, dmode);
1462dd7b254dSGiuliano Pochini 			/* If we had to change the clock source, report it */
1463dd7b254dSGiuliano Pochini 			if (changed > 0 && chip->clock_src_ctl) {
1464dd7b254dSGiuliano Pochini 				snd_ctl_notify(chip->card,
1465dd7b254dSGiuliano Pochini 					       SNDRV_CTL_EVENT_MASK_VALUE,
1466dd7b254dSGiuliano Pochini 					       &chip->clock_src_ctl->id);
1467b5b4a41bSSudip Mukherjee 				dev_dbg(chip->card->dev,
1468b5b4a41bSSudip Mukherjee 					"SDM() =%d\n", changed);
1469dd7b254dSGiuliano Pochini 			}
1470dd7b254dSGiuliano Pochini 			if (changed >= 0)
1471dd7b254dSGiuliano Pochini 				changed = 1;	/* No errors */
1472dd7b254dSGiuliano Pochini 		}
1473befceea9STakashi Iwai 		mutex_unlock(&chip->mode_mutex);
1474dd7b254dSGiuliano Pochini 	}
1475dd7b254dSGiuliano Pochini 	return changed;
1476dd7b254dSGiuliano Pochini }
1477dd7b254dSGiuliano Pochini 
1478f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1479dd7b254dSGiuliano Pochini 	.name = "Digital mode Switch",
1480dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1481dd7b254dSGiuliano Pochini 	.info = snd_echo_digital_mode_info,
1482dd7b254dSGiuliano Pochini 	.get = snd_echo_digital_mode_get,
1483dd7b254dSGiuliano Pochini 	.put = snd_echo_digital_mode_put,
1484dd7b254dSGiuliano Pochini };
1485dd7b254dSGiuliano Pochini 
1486dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1487dd7b254dSGiuliano Pochini 
1488dd7b254dSGiuliano Pochini 
1489dd7b254dSGiuliano Pochini 
1490dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
1491dd7b254dSGiuliano Pochini 
1492dd7b254dSGiuliano Pochini /******************* S/PDIF mode switch *******************/
snd_echo_spdif_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1493dd7b254dSGiuliano Pochini static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1494dd7b254dSGiuliano Pochini 				    struct snd_ctl_elem_info *uinfo)
1495dd7b254dSGiuliano Pochini {
1496c69a4f30STakashi Iwai 	static const char * const names[2] = {"Consumer", "Professional"};
1497dd7b254dSGiuliano Pochini 
1498c69a4f30STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 2, names);
1499dd7b254dSGiuliano Pochini }
1500dd7b254dSGiuliano Pochini 
snd_echo_spdif_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1501dd7b254dSGiuliano Pochini static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1502dd7b254dSGiuliano Pochini 				   struct snd_ctl_elem_value *ucontrol)
1503dd7b254dSGiuliano Pochini {
1504dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1505dd7b254dSGiuliano Pochini 
1506dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1507dd7b254dSGiuliano Pochini 	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1508dd7b254dSGiuliano Pochini 	return 0;
1509dd7b254dSGiuliano Pochini }
1510dd7b254dSGiuliano Pochini 
snd_echo_spdif_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1511dd7b254dSGiuliano Pochini static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1512dd7b254dSGiuliano Pochini 				   struct snd_ctl_elem_value *ucontrol)
1513dd7b254dSGiuliano Pochini {
1514dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1515dd7b254dSGiuliano Pochini 	int mode;
1516dd7b254dSGiuliano Pochini 
1517dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1518dd7b254dSGiuliano Pochini 	mode = !!ucontrol->value.enumerated.item[0];
1519dd7b254dSGiuliano Pochini 	if (mode != chip->professional_spdif) {
1520dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1521dd7b254dSGiuliano Pochini 		set_professional_spdif(chip, mode);
1522dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1523dd7b254dSGiuliano Pochini 		return 1;
1524dd7b254dSGiuliano Pochini 	}
1525dd7b254dSGiuliano Pochini 	return 0;
1526dd7b254dSGiuliano Pochini }
1527dd7b254dSGiuliano Pochini 
1528f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1529dd7b254dSGiuliano Pochini 	.name = "S/PDIF mode Switch",
1530dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1531dd7b254dSGiuliano Pochini 	.info = snd_echo_spdif_mode_info,
1532dd7b254dSGiuliano Pochini 	.get = snd_echo_spdif_mode_get,
1533dd7b254dSGiuliano Pochini 	.put = snd_echo_spdif_mode_put,
1534dd7b254dSGiuliano Pochini };
1535dd7b254dSGiuliano Pochini 
1536dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IO */
1537dd7b254dSGiuliano Pochini 
1538dd7b254dSGiuliano Pochini 
1539dd7b254dSGiuliano Pochini 
1540dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1541dd7b254dSGiuliano Pochini 
1542dd7b254dSGiuliano Pochini /******************* Select input clock source *******************/
snd_echo_clock_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1543dd7b254dSGiuliano Pochini static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1544dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_info *uinfo)
1545dd7b254dSGiuliano Pochini {
1546c69a4f30STakashi Iwai 	static const char * const names[8] = {
1547dd7b254dSGiuliano Pochini 		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1548dd7b254dSGiuliano Pochini 		"ESync96", "MTC"
1549dd7b254dSGiuliano Pochini 	};
1550dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1551dd7b254dSGiuliano Pochini 
1552dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1553c69a4f30STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1554dd7b254dSGiuliano Pochini }
1555dd7b254dSGiuliano Pochini 
snd_echo_clock_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1556dd7b254dSGiuliano Pochini static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1557dd7b254dSGiuliano Pochini 				     struct snd_ctl_elem_value *ucontrol)
1558dd7b254dSGiuliano Pochini {
1559dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1560dd7b254dSGiuliano Pochini 	int i, clock;
1561dd7b254dSGiuliano Pochini 
1562dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1563dd7b254dSGiuliano Pochini 	clock = chip->input_clock;
1564dd7b254dSGiuliano Pochini 
1565dd7b254dSGiuliano Pochini 	for (i = 0; i < chip->num_clock_sources; i++)
1566dd7b254dSGiuliano Pochini 		if (clock == chip->clock_source_list[i])
1567dd7b254dSGiuliano Pochini 			ucontrol->value.enumerated.item[0] = i;
1568dd7b254dSGiuliano Pochini 
1569dd7b254dSGiuliano Pochini 	return 0;
1570dd7b254dSGiuliano Pochini }
1571dd7b254dSGiuliano Pochini 
snd_echo_clock_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1572dd7b254dSGiuliano Pochini static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1573dd7b254dSGiuliano Pochini 				     struct snd_ctl_elem_value *ucontrol)
1574dd7b254dSGiuliano Pochini {
1575dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1576dd7b254dSGiuliano Pochini 	int changed;
1577dd7b254dSGiuliano Pochini 	unsigned int eclock, dclock;
1578dd7b254dSGiuliano Pochini 
1579dd7b254dSGiuliano Pochini 	changed = 0;
1580dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1581dd7b254dSGiuliano Pochini 	eclock = ucontrol->value.enumerated.item[0];
1582dd7b254dSGiuliano Pochini 	if (eclock >= chip->input_clock_types)
1583dd7b254dSGiuliano Pochini 		return -EINVAL;
1584dd7b254dSGiuliano Pochini 	dclock = chip->clock_source_list[eclock];
1585dd7b254dSGiuliano Pochini 	if (chip->input_clock != dclock) {
1586befceea9STakashi Iwai 		mutex_lock(&chip->mode_mutex);
1587dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1588549717fcSTakashi Iwai 		changed = set_input_clock(chip, dclock);
1589549717fcSTakashi Iwai 		if (!changed)
1590dd7b254dSGiuliano Pochini 			changed = 1;	/* no errors */
1591dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1592befceea9STakashi Iwai 		mutex_unlock(&chip->mode_mutex);
1593dd7b254dSGiuliano Pochini 	}
1594dd7b254dSGiuliano Pochini 
1595dd7b254dSGiuliano Pochini 	if (changed < 0)
1596b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev,
1597b5b4a41bSSudip Mukherjee 			"seticlk val%d err 0x%x\n", dclock, changed);
1598dd7b254dSGiuliano Pochini 
1599dd7b254dSGiuliano Pochini 	return changed;
1600dd7b254dSGiuliano Pochini }
1601dd7b254dSGiuliano Pochini 
1602f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1603dd7b254dSGiuliano Pochini 	.name = "Sample Clock Source",
1604dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1605dd7b254dSGiuliano Pochini 	.info = snd_echo_clock_source_info,
1606dd7b254dSGiuliano Pochini 	.get = snd_echo_clock_source_get,
1607dd7b254dSGiuliano Pochini 	.put = snd_echo_clock_source_put,
1608dd7b254dSGiuliano Pochini };
1609dd7b254dSGiuliano Pochini 
1610dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1611dd7b254dSGiuliano Pochini 
1612dd7b254dSGiuliano Pochini 
1613dd7b254dSGiuliano Pochini 
1614dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_PHANTOM_POWER
1615dd7b254dSGiuliano Pochini 
1616dd7b254dSGiuliano Pochini /******************* Phantom power switch *******************/
1617a5ce8890STakashi Iwai #define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1618dd7b254dSGiuliano Pochini 
snd_echo_phantom_power_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1619dd7b254dSGiuliano Pochini static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1620dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_value *ucontrol)
1621dd7b254dSGiuliano Pochini {
1622dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1623dd7b254dSGiuliano Pochini 
1624dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[0] = chip->phantom_power;
1625dd7b254dSGiuliano Pochini 	return 0;
1626dd7b254dSGiuliano Pochini }
1627dd7b254dSGiuliano Pochini 
snd_echo_phantom_power_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1628dd7b254dSGiuliano Pochini static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1629dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_value *ucontrol)
1630dd7b254dSGiuliano Pochini {
1631dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632dd7b254dSGiuliano Pochini 	int power, changed = 0;
1633dd7b254dSGiuliano Pochini 
1634dd7b254dSGiuliano Pochini 	power = !!ucontrol->value.integer.value[0];
1635dd7b254dSGiuliano Pochini 	if (chip->phantom_power != power) {
1636dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1637dd7b254dSGiuliano Pochini 		changed = set_phantom_power(chip, power);
1638dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1639dd7b254dSGiuliano Pochini 		if (changed == 0)
1640dd7b254dSGiuliano Pochini 			changed = 1;	/* no errors */
1641dd7b254dSGiuliano Pochini 	}
1642dd7b254dSGiuliano Pochini 	return changed;
1643dd7b254dSGiuliano Pochini }
1644dd7b254dSGiuliano Pochini 
1645f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1646dd7b254dSGiuliano Pochini 	.name = "Phantom power Switch",
1647dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1648dd7b254dSGiuliano Pochini 	.info = snd_echo_phantom_power_info,
1649dd7b254dSGiuliano Pochini 	.get = snd_echo_phantom_power_get,
1650dd7b254dSGiuliano Pochini 	.put = snd_echo_phantom_power_put,
1651dd7b254dSGiuliano Pochini };
1652dd7b254dSGiuliano Pochini 
1653dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1654dd7b254dSGiuliano Pochini 
1655dd7b254dSGiuliano Pochini 
1656dd7b254dSGiuliano Pochini 
1657dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1658dd7b254dSGiuliano Pochini 
1659dd7b254dSGiuliano Pochini /******************* Digital input automute switch *******************/
1660a5ce8890STakashi Iwai #define snd_echo_automute_info		snd_ctl_boolean_mono_info
1661dd7b254dSGiuliano Pochini 
snd_echo_automute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1662dd7b254dSGiuliano Pochini static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1663dd7b254dSGiuliano Pochini 				 struct snd_ctl_elem_value *ucontrol)
1664dd7b254dSGiuliano Pochini {
1665dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1666dd7b254dSGiuliano Pochini 
1667dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1668dd7b254dSGiuliano Pochini 	return 0;
1669dd7b254dSGiuliano Pochini }
1670dd7b254dSGiuliano Pochini 
snd_echo_automute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1671dd7b254dSGiuliano Pochini static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1672dd7b254dSGiuliano Pochini 				 struct snd_ctl_elem_value *ucontrol)
1673dd7b254dSGiuliano Pochini {
1674dd7b254dSGiuliano Pochini 	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1675dd7b254dSGiuliano Pochini 	int automute, changed = 0;
1676dd7b254dSGiuliano Pochini 
1677dd7b254dSGiuliano Pochini 	automute = !!ucontrol->value.integer.value[0];
1678dd7b254dSGiuliano Pochini 	if (chip->digital_in_automute != automute) {
1679dd7b254dSGiuliano Pochini 		spin_lock_irq(&chip->lock);
1680dd7b254dSGiuliano Pochini 		changed = set_input_auto_mute(chip, automute);
1681dd7b254dSGiuliano Pochini 		spin_unlock_irq(&chip->lock);
1682dd7b254dSGiuliano Pochini 		if (changed == 0)
1683dd7b254dSGiuliano Pochini 			changed = 1;	/* no errors */
1684dd7b254dSGiuliano Pochini 	}
1685dd7b254dSGiuliano Pochini 	return changed;
1686dd7b254dSGiuliano Pochini }
1687dd7b254dSGiuliano Pochini 
1688f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_automute_switch = {
1689dd7b254dSGiuliano Pochini 	.name = "Digital Capture Switch (automute)",
1690dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1691dd7b254dSGiuliano Pochini 	.info = snd_echo_automute_info,
1692dd7b254dSGiuliano Pochini 	.get = snd_echo_automute_get,
1693dd7b254dSGiuliano Pochini 	.put = snd_echo_automute_put,
1694dd7b254dSGiuliano Pochini };
1695dd7b254dSGiuliano Pochini 
1696dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1697dd7b254dSGiuliano Pochini 
1698dd7b254dSGiuliano Pochini 
1699dd7b254dSGiuliano Pochini 
1700dd7b254dSGiuliano Pochini /******************* VU-meters switch *******************/
1701a5ce8890STakashi Iwai #define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1702dd7b254dSGiuliano Pochini 
snd_echo_vumeters_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1703dd7b254dSGiuliano Pochini static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1704dd7b254dSGiuliano Pochini 					struct snd_ctl_elem_value *ucontrol)
1705dd7b254dSGiuliano Pochini {
1706dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1707dd7b254dSGiuliano Pochini 
1708dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1709dd7b254dSGiuliano Pochini 	spin_lock_irq(&chip->lock);
1710dd7b254dSGiuliano Pochini 	set_meters_on(chip, ucontrol->value.integer.value[0]);
1711dd7b254dSGiuliano Pochini 	spin_unlock_irq(&chip->lock);
1712dd7b254dSGiuliano Pochini 	return 1;
1713dd7b254dSGiuliano Pochini }
1714dd7b254dSGiuliano Pochini 
1715f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1716dd7b254dSGiuliano Pochini 	.name = "VU-meters Switch",
1717dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1718dd7b254dSGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1719dd7b254dSGiuliano Pochini 	.info = snd_echo_vumeters_switch_info,
1720dd7b254dSGiuliano Pochini 	.put = snd_echo_vumeters_switch_put,
1721dd7b254dSGiuliano Pochini };
1722dd7b254dSGiuliano Pochini 
1723dd7b254dSGiuliano Pochini 
1724dd7b254dSGiuliano Pochini 
1725dd7b254dSGiuliano Pochini /***** Read VU-meters (input, output, analog and digital together) *****/
snd_echo_vumeters_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1726dd7b254dSGiuliano Pochini static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1727dd7b254dSGiuliano Pochini 				  struct snd_ctl_elem_info *uinfo)
1728dd7b254dSGiuliano Pochini {
1729dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
173051db452dSTakashi Sakamoto 	uinfo->count = 96;
1731dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1732dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = 0;
1733dd7b254dSGiuliano Pochini 	return 0;
1734dd7b254dSGiuliano Pochini }
1735dd7b254dSGiuliano Pochini 
snd_echo_vumeters_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1736dd7b254dSGiuliano Pochini static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1737dd7b254dSGiuliano Pochini 				 struct snd_ctl_elem_value *ucontrol)
1738dd7b254dSGiuliano Pochini {
1739dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1740dd7b254dSGiuliano Pochini 
1741dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1742dd7b254dSGiuliano Pochini 	get_audio_meters(chip, ucontrol->value.integer.value);
1743dd7b254dSGiuliano Pochini 	return 0;
1744dd7b254dSGiuliano Pochini }
1745dd7b254dSGiuliano Pochini 
1746f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_vumeters = {
1747dd7b254dSGiuliano Pochini 	.name = "VU-meters",
1748dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749048b9450SGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1750048b9450SGiuliano Pochini 		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1751048b9450SGiuliano Pochini 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1752dd7b254dSGiuliano Pochini 	.info = snd_echo_vumeters_info,
1753dd7b254dSGiuliano Pochini 	.get = snd_echo_vumeters_get,
1754048b9450SGiuliano Pochini 	.tlv = {.p = db_scale_output_gain},
1755dd7b254dSGiuliano Pochini };
1756dd7b254dSGiuliano Pochini 
1757dd7b254dSGiuliano Pochini 
1758dd7b254dSGiuliano Pochini 
1759dd7b254dSGiuliano Pochini /*** Channels info - it exports informations about the number of channels ***/
snd_echo_channels_info_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1760dd7b254dSGiuliano Pochini static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1761dd7b254dSGiuliano Pochini 				       struct snd_ctl_elem_info *uinfo)
1762dd7b254dSGiuliano Pochini {
1763dd7b254dSGiuliano Pochini 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1764dd7b254dSGiuliano Pochini 	uinfo->count = 6;
1765dd7b254dSGiuliano Pochini 	uinfo->value.integer.min = 0;
1766dd7b254dSGiuliano Pochini 	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1767dd7b254dSGiuliano Pochini 	return 0;
1768dd7b254dSGiuliano Pochini }
1769dd7b254dSGiuliano Pochini 
snd_echo_channels_info_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1770dd7b254dSGiuliano Pochini static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1771dd7b254dSGiuliano Pochini 				      struct snd_ctl_elem_value *ucontrol)
1772dd7b254dSGiuliano Pochini {
1773dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1774dd7b254dSGiuliano Pochini 	int detected, clocks, bit, src;
1775dd7b254dSGiuliano Pochini 
1776dd7b254dSGiuliano Pochini 	chip = snd_kcontrol_chip(kcontrol);
1777dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[0] = num_busses_in(chip);
1778dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1779dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[2] = num_busses_out(chip);
1780dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1781dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1782dd7b254dSGiuliano Pochini 
1783dd7b254dSGiuliano Pochini 	/* Compute the bitmask of the currently valid input clocks */
1784dd7b254dSGiuliano Pochini 	detected = detect_input_clocks(chip);
1785dd7b254dSGiuliano Pochini 	clocks = 0;
1786dd7b254dSGiuliano Pochini 	src = chip->num_clock_sources - 1;
1787dd7b254dSGiuliano Pochini 	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1788dd7b254dSGiuliano Pochini 		if (detected & (1 << bit))
1789dd7b254dSGiuliano Pochini 			for (; src >= 0; src--)
1790dd7b254dSGiuliano Pochini 				if (bit == chip->clock_source_list[src]) {
1791dd7b254dSGiuliano Pochini 					clocks |= 1 << src;
1792dd7b254dSGiuliano Pochini 					break;
1793dd7b254dSGiuliano Pochini 				}
1794dd7b254dSGiuliano Pochini 	ucontrol->value.integer.value[5] = clocks;
1795dd7b254dSGiuliano Pochini 
1796dd7b254dSGiuliano Pochini 	return 0;
1797dd7b254dSGiuliano Pochini }
1798dd7b254dSGiuliano Pochini 
1799f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_echo_channels_info = {
1800dd7b254dSGiuliano Pochini 	.name = "Channels info",
1801dd7b254dSGiuliano Pochini 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1802dd7b254dSGiuliano Pochini 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1803dd7b254dSGiuliano Pochini 	.info = snd_echo_channels_info_info,
1804dd7b254dSGiuliano Pochini 	.get = snd_echo_channels_info_get,
1805dd7b254dSGiuliano Pochini };
1806dd7b254dSGiuliano Pochini 
1807dd7b254dSGiuliano Pochini 
1808dd7b254dSGiuliano Pochini 
1809dd7b254dSGiuliano Pochini 
1810dd7b254dSGiuliano Pochini /******************************************************************************
1811a0b224b9SMark Hills 	IRQ Handling
1812dd7b254dSGiuliano Pochini ******************************************************************************/
1813a0b224b9SMark Hills /* Check if a period has elapsed since last interrupt
1814a0b224b9SMark Hills  *
1815a0b224b9SMark Hills  * Don't make any updates to state; PCM core handles this with the
1816a0b224b9SMark Hills  * correct locks.
1817a0b224b9SMark Hills  *
1818a0b224b9SMark Hills  * \return true if a period has elapsed, otherwise false
1819a0b224b9SMark Hills  */
period_has_elapsed(struct snd_pcm_substream * substream)1820a0b224b9SMark Hills static bool period_has_elapsed(struct snd_pcm_substream *substream)
1821a0b224b9SMark Hills {
1822a0b224b9SMark Hills 	struct snd_pcm_runtime *runtime = substream->runtime;
1823a0b224b9SMark Hills 	struct audiopipe *pipe = runtime->private_data;
1824a0b224b9SMark Hills 	u32 counter, step;
1825a0b224b9SMark Hills 	size_t period_bytes;
1826a0b224b9SMark Hills 
1827a0b224b9SMark Hills 	if (pipe->state != PIPE_STATE_STARTED)
1828a0b224b9SMark Hills 		return false;
1829a0b224b9SMark Hills 
1830a0b224b9SMark Hills 	period_bytes = frames_to_bytes(runtime, runtime->period_size);
1831a0b224b9SMark Hills 
1832a0b224b9SMark Hills 	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1833a0b224b9SMark Hills 
1834a0b224b9SMark Hills 	step = counter - pipe->last_period;  /* handles wrapping */
1835a0b224b9SMark Hills 	step -= step % period_bytes;  /* acknowledge whole periods only */
1836a0b224b9SMark Hills 
1837a0b224b9SMark Hills 	if (step == 0)
1838a0b224b9SMark Hills 		return false;  /* haven't advanced a whole period yet */
1839a0b224b9SMark Hills 
1840a0b224b9SMark Hills 	pipe->last_period += step;  /* used exclusively by us */
1841a0b224b9SMark Hills 	return true;
1842a0b224b9SMark Hills }
1843dd7b254dSGiuliano Pochini 
snd_echo_interrupt(int irq,void * dev_id)18447d12e780SDavid Howells static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1845dd7b254dSGiuliano Pochini {
1846dd7b254dSGiuliano Pochini 	struct echoaudio *chip = dev_id;
1847a0b224b9SMark Hills 	int ss, st;
1848dd7b254dSGiuliano Pochini 
1849dd7b254dSGiuliano Pochini 	spin_lock(&chip->lock);
1850dd7b254dSGiuliano Pochini 	st = service_irq(chip);
1851dd7b254dSGiuliano Pochini 	if (st < 0) {
1852dd7b254dSGiuliano Pochini 		spin_unlock(&chip->lock);
1853dd7b254dSGiuliano Pochini 		return IRQ_NONE;
1854dd7b254dSGiuliano Pochini 	}
1855dd7b254dSGiuliano Pochini 	/* The hardware doesn't tell us which substream caused the irq,
1856dd7b254dSGiuliano Pochini 	thus we have to check all running substreams. */
1857dd7b254dSGiuliano Pochini 	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1858a0b224b9SMark Hills 		struct snd_pcm_substream *substream;
1859a0b224b9SMark Hills 
1860b721e68bSGiuliano Pochini 		substream = chip->substream[ss];
1861a0b224b9SMark Hills 		if (substream && period_has_elapsed(substream)) {
1862dd7b254dSGiuliano Pochini 			spin_unlock(&chip->lock);
1863dd7b254dSGiuliano Pochini 			snd_pcm_period_elapsed(substream);
1864dd7b254dSGiuliano Pochini 			spin_lock(&chip->lock);
1865dd7b254dSGiuliano Pochini 		}
1866dd7b254dSGiuliano Pochini 	}
1867dd7b254dSGiuliano Pochini 	spin_unlock(&chip->lock);
1868dd7b254dSGiuliano Pochini 
1869dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
1870dd7b254dSGiuliano Pochini 	if (st > 0 && chip->midi_in) {
1871dd7b254dSGiuliano Pochini 		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1872b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1873dd7b254dSGiuliano Pochini 	}
1874dd7b254dSGiuliano Pochini #endif
1875dd7b254dSGiuliano Pochini 	return IRQ_HANDLED;
1876dd7b254dSGiuliano Pochini }
1877dd7b254dSGiuliano Pochini 
1878dd7b254dSGiuliano Pochini 
1879dd7b254dSGiuliano Pochini 
1880dd7b254dSGiuliano Pochini 
1881dd7b254dSGiuliano Pochini /******************************************************************************
1882dd7b254dSGiuliano Pochini 	Module construction / destruction
1883dd7b254dSGiuliano Pochini ******************************************************************************/
1884dd7b254dSGiuliano Pochini 
snd_echo_free(struct snd_card * card)18859c211bf3STakashi Iwai static void snd_echo_free(struct snd_card *card)
1886dd7b254dSGiuliano Pochini {
18879c211bf3STakashi Iwai 	struct echoaudio *chip = card->private_data;
18889c211bf3STakashi Iwai 
1889ebf029daSTakashi Iwai 	if (chip->comm_page)
1890dd7b254dSGiuliano Pochini 		rest_in_peace(chip);
1891dd7b254dSGiuliano Pochini 
1892dd7b254dSGiuliano Pochini 	if (chip->irq >= 0)
1893437a5a46STakashi Iwai 		free_irq(chip->irq, chip);
1894dd7b254dSGiuliano Pochini 
1895dd7b254dSGiuliano Pochini 	/* release chip data */
18964f8ada44SGiuliano Pochini 	free_firmware_cache(chip);
1897dd7b254dSGiuliano Pochini }
1898dd7b254dSGiuliano Pochini 
1899dd7b254dSGiuliano Pochini /* <--snd_echo_probe() */
snd_echo_create(struct snd_card * card,struct pci_dev * pci)1900e23e7a14SBill Pemberton static int snd_echo_create(struct snd_card *card,
19019c211bf3STakashi Iwai 			   struct pci_dev *pci)
1902dd7b254dSGiuliano Pochini {
19039c211bf3STakashi Iwai 	struct echoaudio *chip = card->private_data;
1904dd7b254dSGiuliano Pochini 	int err;
1905dd7b254dSGiuliano Pochini 	size_t sz;
1906dd7b254dSGiuliano Pochini 
1907dd7b254dSGiuliano Pochini 	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1908dd7b254dSGiuliano Pochini 
19099c211bf3STakashi Iwai 	err = pcim_enable_device(pci);
1910549717fcSTakashi Iwai 	if (err < 0)
1911dd7b254dSGiuliano Pochini 		return err;
1912dd7b254dSGiuliano Pochini 	pci_set_master(pci);
1913dd7b254dSGiuliano Pochini 
191447b5d028SGiuliano Pochini 	/* Allocate chip if needed */
1915dd7b254dSGiuliano Pochini 	spin_lock_init(&chip->lock);
1916dd7b254dSGiuliano Pochini 	chip->card = card;
1917dd7b254dSGiuliano Pochini 	chip->pci = pci;
1918dd7b254dSGiuliano Pochini 	chip->irq = -1;
1919027c7002SMark Hills 	chip->opencount = 0;
192047b5d028SGiuliano Pochini 	mutex_init(&chip->mode_mutex);
192147b5d028SGiuliano Pochini 	chip->can_set_rate = 1;
1922dd7b254dSGiuliano Pochini 
1923dd7b254dSGiuliano Pochini 	/* PCI resource allocation */
19249c211bf3STakashi Iwai 	err = pci_request_regions(pci, ECHOCARD_NAME);
19259c211bf3STakashi Iwai 	if (err < 0)
19269c211bf3STakashi Iwai 		return err;
19279c211bf3STakashi Iwai 
1928dd7b254dSGiuliano Pochini 	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929dd7b254dSGiuliano Pochini 	sz = pci_resource_len(pci, 0);
1930dd7b254dSGiuliano Pochini 	if (sz > PAGE_SIZE)
1931dd7b254dSGiuliano Pochini 		sz = PAGE_SIZE;		/* We map only the required part */
1932dd7b254dSGiuliano Pochini 
19339c211bf3STakashi Iwai 	chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
19346ade657dSKangjie Lu 	if (!chip->dsp_registers) {
19356ade657dSKangjie Lu 		dev_err(chip->card->dev, "ioremap failed\n");
19366ade657dSKangjie Lu 		return -ENOMEM;
19376ade657dSKangjie Lu 	}
1938dd7b254dSGiuliano Pochini 
1939437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1940934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
1941ece7a36dSTakashi Iwai 		dev_err(chip->card->dev, "cannot grab irq\n");
1942dd7b254dSGiuliano Pochini 		return -EBUSY;
1943dd7b254dSGiuliano Pochini 	}
1944dd7b254dSGiuliano Pochini 	chip->irq = pci->irq;
1945d4cad99fSTakashi Iwai 	card->sync_irq = chip->irq;
1946b5b4a41bSSudip Mukherjee 	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1947b5b4a41bSSudip Mukherjee 		chip->pci, chip->irq, chip->pci->subsystem_device);
1948dd7b254dSGiuliano Pochini 
19499c211bf3STakashi Iwai 	card->private_free = snd_echo_free;
19509c211bf3STakashi Iwai 
1951dd7b254dSGiuliano Pochini 	/* Create the DSP comm page - this is the area of memory used for most
1952dd7b254dSGiuliano Pochini 	of the communication with the DSP, which accesses it via bus mastering */
19539c211bf3STakashi Iwai 	chip->commpage_dma_buf =
19549c211bf3STakashi Iwai 		snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
19559c211bf3STakashi Iwai 				     sizeof(struct comm_page));
19569c211bf3STakashi Iwai 	if (!chip->commpage_dma_buf)
1957dd7b254dSGiuliano Pochini 		return -ENOMEM;
19589c211bf3STakashi Iwai 	chip->comm_page_phys = chip->commpage_dma_buf->addr;
19599c211bf3STakashi Iwai 	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1960dd7b254dSGiuliano Pochini 
1961dd7b254dSGiuliano Pochini 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
196247b5d028SGiuliano Pochini 	if (err >= 0)
196347b5d028SGiuliano Pochini 		err = set_mixer_defaults(chip);
196447b5d028SGiuliano Pochini 	if (err < 0) {
1965b5b4a41bSSudip Mukherjee 		dev_err(card->dev, "init_hw err=%d\n", err);
1966dd7b254dSGiuliano Pochini 		return err;
1967dd7b254dSGiuliano Pochini 	}
1968dd7b254dSGiuliano Pochini 
1969dd7b254dSGiuliano Pochini 	return 0;
1970dd7b254dSGiuliano Pochini }
1971dd7b254dSGiuliano Pochini 
1972dd7b254dSGiuliano Pochini /* constructor */
__snd_echo_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1973*313c7e57STakashi Iwai static int __snd_echo_probe(struct pci_dev *pci,
1974dd7b254dSGiuliano Pochini 			    const struct pci_device_id *pci_id)
1975dd7b254dSGiuliano Pochini {
1976dd7b254dSGiuliano Pochini 	static int dev;
1977dd7b254dSGiuliano Pochini 	struct snd_card *card;
1978dd7b254dSGiuliano Pochini 	struct echoaudio *chip;
1979dd7b254dSGiuliano Pochini 	char *dsp;
19808f53cb8eSPierre-Louis Bossart 	__maybe_unused int i;
19818f53cb8eSPierre-Louis Bossart 	int err;
1982dd7b254dSGiuliano Pochini 
1983dd7b254dSGiuliano Pochini 	if (dev >= SNDRV_CARDS)
1984dd7b254dSGiuliano Pochini 		return -ENODEV;
1985dd7b254dSGiuliano Pochini 	if (!enable[dev]) {
1986dd7b254dSGiuliano Pochini 		dev++;
1987dd7b254dSGiuliano Pochini 		return -ENOENT;
1988dd7b254dSGiuliano Pochini 	}
1989dd7b254dSGiuliano Pochini 
1990dd7b254dSGiuliano Pochini 	i = 0;
19919c211bf3STakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
19929c211bf3STakashi Iwai 				sizeof(*chip), &card);
1993e58de7baSTakashi Iwai 	if (err < 0)
1994e58de7baSTakashi Iwai 		return err;
19959c211bf3STakashi Iwai 	chip = card->private_data;
1996dd7b254dSGiuliano Pochini 
19979c211bf3STakashi Iwai 	err = snd_echo_create(card, pci);
19989c211bf3STakashi Iwai 	if (err < 0)
1999dd7b254dSGiuliano Pochini 		return err;
2000dd7b254dSGiuliano Pochini 
2001dd7b254dSGiuliano Pochini 	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2002dd7b254dSGiuliano Pochini 	strcpy(card->shortname, chip->card_name);
2003dd7b254dSGiuliano Pochini 
2004dd7b254dSGiuliano Pochini 	dsp = "56301";
2005dd7b254dSGiuliano Pochini 	if (pci_id->device == 0x3410)
2006dd7b254dSGiuliano Pochini 		dsp = "56361";
2007dd7b254dSGiuliano Pochini 
2008dd7b254dSGiuliano Pochini 	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2009dd7b254dSGiuliano Pochini 		card->shortname, pci_id->subdevice & 0x000f, dsp,
2010dd7b254dSGiuliano Pochini 		chip->dsp_registers_phys, chip->irq);
2011dd7b254dSGiuliano Pochini 
2012549717fcSTakashi Iwai 	err = snd_echo_new_pcm(chip);
2013549717fcSTakashi Iwai 	if (err < 0) {
2014ece7a36dSTakashi Iwai 		dev_err(chip->card->dev, "new pcm error %d\n", err);
2015dd7b254dSGiuliano Pochini 		return err;
2016dd7b254dSGiuliano Pochini 	}
2017dd7b254dSGiuliano Pochini 
2018dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
2019dd7b254dSGiuliano Pochini 	if (chip->has_midi) {	/* Some Mia's do not have midi */
2020549717fcSTakashi Iwai 		err = snd_echo_midi_create(card, chip);
2021549717fcSTakashi Iwai 		if (err < 0) {
2022ece7a36dSTakashi Iwai 			dev_err(chip->card->dev, "new midi error %d\n", err);
2023dd7b254dSGiuliano Pochini 			return err;
2024dd7b254dSGiuliano Pochini 		}
2025dd7b254dSGiuliano Pochini 	}
2026dd7b254dSGiuliano Pochini #endif
2027dd7b254dSGiuliano Pochini 
2028dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
2029dd7b254dSGiuliano Pochini 	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2030549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
2031549717fcSTakashi Iwai 	if (err < 0)
20329c211bf3STakashi Iwai 		return err;
2033392bf2f1SGiuliano Pochini #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2034392bf2f1SGiuliano Pochini 	err = snd_ctl_add(chip->card,
2035392bf2f1SGiuliano Pochini 			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2036392bf2f1SGiuliano Pochini 	if (err < 0)
20379c211bf3STakashi Iwai 		return err;
2038dd7b254dSGiuliano Pochini #endif
2039392bf2f1SGiuliano Pochini #else /* ECHOCARD_HAS_VMIXER */
2040392bf2f1SGiuliano Pochini 	err = snd_ctl_add(chip->card,
2041392bf2f1SGiuliano Pochini 			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2042392bf2f1SGiuliano Pochini 	if (err < 0)
20439c211bf3STakashi Iwai 		return err;
2044392bf2f1SGiuliano Pochini #endif /* ECHOCARD_HAS_VMIXER */
2045dd7b254dSGiuliano Pochini 
2046dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_GAIN
2047549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2048549717fcSTakashi Iwai 	if (err < 0)
20499c211bf3STakashi Iwai 		return err;
2050dd7b254dSGiuliano Pochini #endif
2051dd7b254dSGiuliano Pochini 
2052dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2053549717fcSTakashi Iwai 	if (!chip->hasnt_input_nominal_level) {
2054549717fcSTakashi Iwai 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2055549717fcSTakashi Iwai 		if (err < 0)
20569c211bf3STakashi Iwai 			return err;
2057549717fcSTakashi Iwai 	}
2058dd7b254dSGiuliano Pochini #endif
2059dd7b254dSGiuliano Pochini 
2060dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2061549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2062549717fcSTakashi Iwai 	if (err < 0)
20639c211bf3STakashi Iwai 		return err;
2064dd7b254dSGiuliano Pochini #endif
2065dd7b254dSGiuliano Pochini 
2066549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2067549717fcSTakashi Iwai 	if (err < 0)
20689c211bf3STakashi Iwai 		return err;
2069dd7b254dSGiuliano Pochini 
2070549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2071549717fcSTakashi Iwai 	if (err < 0)
20729c211bf3STakashi Iwai 		return err;
2073dd7b254dSGiuliano Pochini 
2074dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
2075dd7b254dSGiuliano Pochini 	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2076549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2077549717fcSTakashi Iwai 	if (err < 0)
20789c211bf3STakashi Iwai 		return err;
2079dd7b254dSGiuliano Pochini #endif
2080dd7b254dSGiuliano Pochini 
2081dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2082549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2083549717fcSTakashi Iwai 	if (err < 0)
20849c211bf3STakashi Iwai 		return err;
2085dd7b254dSGiuliano Pochini #endif
2086dd7b254dSGiuliano Pochini 
2087549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2088549717fcSTakashi Iwai 	if (err < 0)
20899c211bf3STakashi Iwai 		return err;
2090dd7b254dSGiuliano Pochini 
2091dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2092dd7b254dSGiuliano Pochini 	/* Creates a list of available digital modes */
2093dd7b254dSGiuliano Pochini 	chip->num_digital_modes = 0;
2094dd7b254dSGiuliano Pochini 	for (i = 0; i < 6; i++)
2095dd7b254dSGiuliano Pochini 		if (chip->digital_modes & (1 << i))
2096dd7b254dSGiuliano Pochini 			chip->digital_mode_list[chip->num_digital_modes++] = i;
2097dd7b254dSGiuliano Pochini 
2098549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2099549717fcSTakashi Iwai 	if (err < 0)
21009c211bf3STakashi Iwai 		return err;
2101dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2102dd7b254dSGiuliano Pochini 
2103dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2104dd7b254dSGiuliano Pochini 	/* Creates a list of available clock sources */
2105dd7b254dSGiuliano Pochini 	chip->num_clock_sources = 0;
2106dd7b254dSGiuliano Pochini 	for (i = 0; i < 10; i++)
2107dd7b254dSGiuliano Pochini 		if (chip->input_clock_types & (1 << i))
2108dd7b254dSGiuliano Pochini 			chip->clock_source_list[chip->num_clock_sources++] = i;
2109dd7b254dSGiuliano Pochini 
2110dd7b254dSGiuliano Pochini 	if (chip->num_clock_sources > 1) {
2111dd7b254dSGiuliano Pochini 		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2112549717fcSTakashi Iwai 		err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2113549717fcSTakashi Iwai 		if (err < 0)
21149c211bf3STakashi Iwai 			return err;
2115dd7b254dSGiuliano Pochini 	}
2116dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2117dd7b254dSGiuliano Pochini 
2118dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
2119549717fcSTakashi Iwai 	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2120549717fcSTakashi Iwai 	if (err < 0)
21219c211bf3STakashi Iwai 		return err;
2122dd7b254dSGiuliano Pochini #endif
2123dd7b254dSGiuliano Pochini 
2124dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_PHANTOM_POWER
2125549717fcSTakashi Iwai 	if (chip->has_phantom_power) {
2126549717fcSTakashi Iwai 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2127549717fcSTakashi Iwai 		if (err < 0)
21289c211bf3STakashi Iwai 			return err;
2129549717fcSTakashi Iwai 	}
2130dd7b254dSGiuliano Pochini #endif
2131dd7b254dSGiuliano Pochini 
2132a0fd4345SJulia Lawall 	err = snd_card_register(card);
2133a0fd4345SJulia Lawall 	if (err < 0)
21349c211bf3STakashi Iwai 		return err;
2135ece7a36dSTakashi Iwai 	dev_info(card->dev, "Card registered: %s\n", card->longname);
2136dd7b254dSGiuliano Pochini 
2137dd7b254dSGiuliano Pochini 	pci_set_drvdata(pci, chip);
2138dd7b254dSGiuliano Pochini 	dev++;
2139dd7b254dSGiuliano Pochini 	return 0;
2140dd7b254dSGiuliano Pochini }
2141dd7b254dSGiuliano Pochini 
snd_echo_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2142*313c7e57STakashi Iwai static int snd_echo_probe(struct pci_dev *pci,
2143*313c7e57STakashi Iwai 			  const struct pci_device_id *pci_id)
2144*313c7e57STakashi Iwai {
2145*313c7e57STakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2146*313c7e57STakashi Iwai }
2147dd7b254dSGiuliano Pochini 
2148dd7b254dSGiuliano Pochini 
2149c7561cd8STakashi Iwai #if defined(CONFIG_PM_SLEEP)
215047b5d028SGiuliano Pochini 
snd_echo_suspend(struct device * dev)215168cb2b55STakashi Iwai static int snd_echo_suspend(struct device *dev)
215247b5d028SGiuliano Pochini {
215368cb2b55STakashi Iwai 	struct echoaudio *chip = dev_get_drvdata(dev);
215447b5d028SGiuliano Pochini 
215547b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
215647b5d028SGiuliano Pochini 	/* This call can sleep */
215747b5d028SGiuliano Pochini 	if (chip->midi_out)
215847b5d028SGiuliano Pochini 		snd_echo_midi_output_trigger(chip->midi_out, 0);
215947b5d028SGiuliano Pochini #endif
216047b5d028SGiuliano Pochini 	spin_lock_irq(&chip->lock);
216147b5d028SGiuliano Pochini 	if (wait_handshake(chip)) {
216247b5d028SGiuliano Pochini 		spin_unlock_irq(&chip->lock);
216347b5d028SGiuliano Pochini 		return -EIO;
216447b5d028SGiuliano Pochini 	}
216547b5d028SGiuliano Pochini 	clear_handshake(chip);
216647b5d028SGiuliano Pochini 	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
216747b5d028SGiuliano Pochini 		spin_unlock_irq(&chip->lock);
216847b5d028SGiuliano Pochini 		return -EIO;
216947b5d028SGiuliano Pochini 	}
217047b5d028SGiuliano Pochini 	spin_unlock_irq(&chip->lock);
217147b5d028SGiuliano Pochini 
217247b5d028SGiuliano Pochini 	chip->dsp_code = NULL;
217347b5d028SGiuliano Pochini 	free_irq(chip->irq, chip);
217447b5d028SGiuliano Pochini 	chip->irq = -1;
2175d4cad99fSTakashi Iwai 	chip->card->sync_irq = -1;
217647b5d028SGiuliano Pochini 	return 0;
217747b5d028SGiuliano Pochini }
217847b5d028SGiuliano Pochini 
217947b5d028SGiuliano Pochini 
218047b5d028SGiuliano Pochini 
snd_echo_resume(struct device * dev)218168cb2b55STakashi Iwai static int snd_echo_resume(struct device *dev)
218247b5d028SGiuliano Pochini {
218368cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
218468cb2b55STakashi Iwai 	struct echoaudio *chip = dev_get_drvdata(dev);
218547b5d028SGiuliano Pochini 	struct comm_page *commpage, *commpage_bak;
218647b5d028SGiuliano Pochini 	u32 pipe_alloc_mask;
218747b5d028SGiuliano Pochini 	int err;
218847b5d028SGiuliano Pochini 
21895e291a90SChuhong Yuan 	commpage = chip->comm_page;
21905e291a90SChuhong Yuan 	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
21910b6d092cSKulikov Vasiliy 	if (commpage_bak == NULL)
21920b6d092cSKulikov Vasiliy 		return -ENOMEM;
219347b5d028SGiuliano Pochini 
219447b5d028SGiuliano Pochini 	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
219547b5d028SGiuliano Pochini 	if (err < 0) {
219647b5d028SGiuliano Pochini 		kfree(commpage_bak);
2197b5b4a41bSSudip Mukherjee 		dev_err(dev, "resume init_hw err=%d\n", err);
219847b5d028SGiuliano Pochini 		return err;
219947b5d028SGiuliano Pochini 	}
220047b5d028SGiuliano Pochini 
220147b5d028SGiuliano Pochini 	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
220247b5d028SGiuliano Pochini 	 * restore_dsp_settings() fails.
220347b5d028SGiuliano Pochini 	 */
220447b5d028SGiuliano Pochini 	pipe_alloc_mask = chip->pipe_alloc_mask;
220547b5d028SGiuliano Pochini 	chip->pipe_alloc_mask = 0;
220647b5d028SGiuliano Pochini 	err = restore_dsp_rettings(chip);
220747b5d028SGiuliano Pochini 	chip->pipe_alloc_mask = pipe_alloc_mask;
220847b5d028SGiuliano Pochini 	if (err < 0) {
220947b5d028SGiuliano Pochini 		kfree(commpage_bak);
221047b5d028SGiuliano Pochini 		return err;
221147b5d028SGiuliano Pochini 	}
221247b5d028SGiuliano Pochini 
221347b5d028SGiuliano Pochini 	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
221447b5d028SGiuliano Pochini 		sizeof(commpage->audio_format));
221547b5d028SGiuliano Pochini 	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
221647b5d028SGiuliano Pochini 		sizeof(commpage->sglist_addr));
221747b5d028SGiuliano Pochini 	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
221847b5d028SGiuliano Pochini 		sizeof(commpage->midi_output));
221947b5d028SGiuliano Pochini 	kfree(commpage_bak);
222047b5d028SGiuliano Pochini 
222147b5d028SGiuliano Pochini 	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2222934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
2223ece7a36dSTakashi Iwai 		dev_err(chip->card->dev, "cannot grab irq\n");
222447b5d028SGiuliano Pochini 		return -EBUSY;
222547b5d028SGiuliano Pochini 	}
222647b5d028SGiuliano Pochini 	chip->irq = pci->irq;
2227d4cad99fSTakashi Iwai 	chip->card->sync_irq = chip->irq;
2228b5b4a41bSSudip Mukherjee 	dev_dbg(dev, "resume irq=%d\n", chip->irq);
222947b5d028SGiuliano Pochini 
223047b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
223147b5d028SGiuliano Pochini 	if (chip->midi_input_enabled)
22323f6175ecSMark Brown 		enable_midi_input(chip, true);
223347b5d028SGiuliano Pochini 	if (chip->midi_out)
223447b5d028SGiuliano Pochini 		snd_echo_midi_output_trigger(chip->midi_out, 1);
223547b5d028SGiuliano Pochini #endif
223647b5d028SGiuliano Pochini 
223747b5d028SGiuliano Pochini 	return 0;
223847b5d028SGiuliano Pochini }
223947b5d028SGiuliano Pochini 
224068cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
224168cb2b55STakashi Iwai #define SND_ECHO_PM_OPS	&snd_echo_pm
224268cb2b55STakashi Iwai #else
224368cb2b55STakashi Iwai #define SND_ECHO_PM_OPS	NULL
2244c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
224547b5d028SGiuliano Pochini 
2246dd7b254dSGiuliano Pochini /******************************************************************************
2247dd7b254dSGiuliano Pochini 	Everything starts and ends here
2248dd7b254dSGiuliano Pochini ******************************************************************************/
2249dd7b254dSGiuliano Pochini 
2250dd7b254dSGiuliano Pochini /* pci_driver definition */
2251e9f66d9bSTakashi Iwai static struct pci_driver echo_driver = {
22523733e424STakashi Iwai 	.name = KBUILD_MODNAME,
2253dd7b254dSGiuliano Pochini 	.id_table = snd_echo_ids,
2254dd7b254dSGiuliano Pochini 	.probe = snd_echo_probe,
225568cb2b55STakashi Iwai 	.driver = {
225668cb2b55STakashi Iwai 		.pm = SND_ECHO_PM_OPS,
225768cb2b55STakashi Iwai 	},
2258dd7b254dSGiuliano Pochini };
2259dd7b254dSGiuliano Pochini 
2260e9f66d9bSTakashi Iwai module_pci_driver(echo_driver);
2261