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