xref: /linux/sound/pci/emu10k1/emumixer.c (revision 5a4332062e9e71de8e78dc1b389d21e0dd44848b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
4  *                   Takashi Iwai <tiwai@suse.de>
5  *                   Lee Revell <rlrevell@joe-job.com>
6  *                   James Courtier-Dutton <James@superbug.co.uk>
7  *                   Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
8  *                   Creative Labs, Inc.
9  *
10  *  Routines for control of EMU10K1 chips / mixer routines
11  */
12 
13 #include <linux/time.h>
14 #include <linux/init.h>
15 #include <sound/core.h>
16 #include <sound/emu10k1.h>
17 #include <linux/delay.h>
18 #include <sound/tlv.h>
19 
20 #include "p17v.h"
21 
22 #define AC97_ID_STAC9758	0x83847658
23 
24 static const DECLARE_TLV_DB_SCALE(snd_audigy_db_scale2, -10350, 50, 1); /* WM8775 gain scale */
25 
26 
27 static int add_ctls(struct snd_emu10k1 *emu, const struct snd_kcontrol_new *tpl,
28 		    const char * const *ctls, unsigned nctls)
29 {
30 	struct snd_kcontrol_new kctl = *tpl;
31 	int err;
32 
33 	for (unsigned i = 0; i < nctls; i++) {
34 		kctl.name = ctls[i];
35 		kctl.private_value = i;
36 		err = snd_ctl_add(emu->card, snd_ctl_new1(&kctl, emu));
37 		if (err < 0)
38 			return err;
39 	}
40 	return 0;
41 }
42 
43 
44 static int snd_emu10k1_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
45 {
46 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
47 	uinfo->count = 1;
48 	return 0;
49 }
50 
51 static int snd_emu10k1_spdif_get(struct snd_kcontrol *kcontrol,
52                                  struct snd_ctl_elem_value *ucontrol)
53 {
54 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
55 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
56 
57 	/* Limit: emu->spdif_bits */
58 	if (idx >= 3)
59 		return -EINVAL;
60 	ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
61 	ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
62 	ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
63 	ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
64 	return 0;
65 }
66 
67 static int snd_emu10k1_spdif_get_mask(struct snd_kcontrol *kcontrol,
68 				      struct snd_ctl_elem_value *ucontrol)
69 {
70 	ucontrol->value.iec958.status[0] = 0xff;
71 	ucontrol->value.iec958.status[1] = 0xff;
72 	ucontrol->value.iec958.status[2] = 0xff;
73 	ucontrol->value.iec958.status[3] = 0xff;
74 	return 0;
75 }
76 
77 #define PAIR_PS(base, one, two, sfx) base " " one sfx, base " " two sfx
78 #define LR_PS(base, sfx) PAIR_PS(base, "Left", "Right", sfx)
79 
80 #define ADAT_PS(pfx, sfx) \
81 	pfx "ADAT 0" sfx, pfx "ADAT 1" sfx, pfx "ADAT 2" sfx, pfx "ADAT 3" sfx, \
82 	pfx "ADAT 4" sfx, pfx "ADAT 5" sfx, pfx "ADAT 6" sfx, pfx "ADAT 7" sfx
83 
84 #define PAIR_REGS(base, one, two) \
85 	base ## one ## 1, \
86 	base ## two ## 1
87 
88 #define LR_REGS(base) PAIR_REGS(base, _LEFT, _RIGHT)
89 
90 #define ADAT_REGS(base) \
91 	base+0, base+1, base+2, base+3, base+4, base+5, base+6, base+7
92 
93 /*
94  * List of data sources available for each destination
95  */
96 
97 #define DSP_TEXTS \
98 	"DSP 0", "DSP 1", "DSP 2", "DSP 3", "DSP 4", "DSP 5", "DSP 6", "DSP 7", \
99 	"DSP 8", "DSP 9", "DSP 10", "DSP 11", "DSP 12", "DSP 13", "DSP 14", "DSP 15", \
100 	"DSP 16", "DSP 17", "DSP 18", "DSP 19", "DSP 20", "DSP 21", "DSP 22", "DSP 23", \
101 	"DSP 24", "DSP 25", "DSP 26", "DSP 27", "DSP 28", "DSP 29", "DSP 30", "DSP 31"
102 
103 #define PAIR_TEXTS(base, one, two) PAIR_PS(base, one, two, "")
104 #define LR_TEXTS(base) LR_PS(base, "")
105 #define ADAT_TEXTS(pfx) ADAT_PS(pfx, "")
106 
107 #define EMU32_SRC_REGS \
108 	EMU_SRC_ALICE_EMU32A, \
109 	EMU_SRC_ALICE_EMU32A+1, \
110 	EMU_SRC_ALICE_EMU32A+2, \
111 	EMU_SRC_ALICE_EMU32A+3, \
112 	EMU_SRC_ALICE_EMU32A+4, \
113 	EMU_SRC_ALICE_EMU32A+5, \
114 	EMU_SRC_ALICE_EMU32A+6, \
115 	EMU_SRC_ALICE_EMU32A+7, \
116 	EMU_SRC_ALICE_EMU32A+8, \
117 	EMU_SRC_ALICE_EMU32A+9, \
118 	EMU_SRC_ALICE_EMU32A+0xa, \
119 	EMU_SRC_ALICE_EMU32A+0xb, \
120 	EMU_SRC_ALICE_EMU32A+0xc, \
121 	EMU_SRC_ALICE_EMU32A+0xd, \
122 	EMU_SRC_ALICE_EMU32A+0xe, \
123 	EMU_SRC_ALICE_EMU32A+0xf, \
124 	EMU_SRC_ALICE_EMU32B, \
125 	EMU_SRC_ALICE_EMU32B+1, \
126 	EMU_SRC_ALICE_EMU32B+2, \
127 	EMU_SRC_ALICE_EMU32B+3, \
128 	EMU_SRC_ALICE_EMU32B+4, \
129 	EMU_SRC_ALICE_EMU32B+5, \
130 	EMU_SRC_ALICE_EMU32B+6, \
131 	EMU_SRC_ALICE_EMU32B+7, \
132 	EMU_SRC_ALICE_EMU32B+8, \
133 	EMU_SRC_ALICE_EMU32B+9, \
134 	EMU_SRC_ALICE_EMU32B+0xa, \
135 	EMU_SRC_ALICE_EMU32B+0xb, \
136 	EMU_SRC_ALICE_EMU32B+0xc, \
137 	EMU_SRC_ALICE_EMU32B+0xd, \
138 	EMU_SRC_ALICE_EMU32B+0xe, \
139 	EMU_SRC_ALICE_EMU32B+0xf
140 
141 /* 1010 rev1 */
142 
143 #define EMU1010_COMMON_TEXTS \
144 	"Silence", \
145 	PAIR_TEXTS("Dock Mic", "A", "B"), \
146 	LR_TEXTS("Dock ADC1"), \
147 	LR_TEXTS("Dock ADC2"), \
148 	LR_TEXTS("Dock ADC3"), \
149 	LR_TEXTS("0202 ADC"), \
150 	LR_TEXTS("1010 SPDIF"), \
151 	ADAT_TEXTS("1010 ")
152 
153 static const char * const emu1010_src_texts[] = {
154 	EMU1010_COMMON_TEXTS,
155 	DSP_TEXTS,
156 };
157 
158 static const unsigned short emu1010_src_regs[] = {
159 	EMU_SRC_SILENCE,
160 	PAIR_REGS(EMU_SRC_DOCK_MIC, _A, _B),
161 	LR_REGS(EMU_SRC_DOCK_ADC1),
162 	LR_REGS(EMU_SRC_DOCK_ADC2),
163 	LR_REGS(EMU_SRC_DOCK_ADC3),
164 	LR_REGS(EMU_SRC_HAMOA_ADC),
165 	LR_REGS(EMU_SRC_HANA_SPDIF),
166 	ADAT_REGS(EMU_SRC_HANA_ADAT),
167 	EMU32_SRC_REGS,
168 };
169 static_assert(ARRAY_SIZE(emu1010_src_regs) == ARRAY_SIZE(emu1010_src_texts));
170 
171 /* 1010 rev2 */
172 
173 #define EMU1010b_COMMON_TEXTS \
174 	"Silence", \
175 	PAIR_TEXTS("Dock Mic", "A", "B"), \
176 	LR_TEXTS("Dock ADC1"), \
177 	LR_TEXTS("Dock ADC2"), \
178 	LR_TEXTS("0202 ADC"), \
179 	LR_TEXTS("Dock SPDIF"), \
180 	LR_TEXTS("1010 SPDIF"), \
181 	ADAT_TEXTS("Dock "), \
182 	ADAT_TEXTS("1010 ")
183 
184 static const char * const emu1010b_src_texts[] = {
185 	EMU1010b_COMMON_TEXTS,
186 	DSP_TEXTS,
187 };
188 
189 static const unsigned short emu1010b_src_regs[] = {
190 	EMU_SRC_SILENCE,
191 	PAIR_REGS(EMU_SRC_DOCK_MIC, _A, _B),
192 	LR_REGS(EMU_SRC_DOCK_ADC1),
193 	LR_REGS(EMU_SRC_DOCK_ADC2),
194 	LR_REGS(EMU_SRC_HAMOA_ADC),
195 	LR_REGS(EMU_SRC_MDOCK_SPDIF),
196 	LR_REGS(EMU_SRC_HANA_SPDIF),
197 	ADAT_REGS(EMU_SRC_MDOCK_ADAT),
198 	ADAT_REGS(EMU_SRC_HANA_ADAT),
199 	EMU32_SRC_REGS,
200 };
201 static_assert(ARRAY_SIZE(emu1010b_src_regs) == ARRAY_SIZE(emu1010b_src_texts));
202 
203 /* 1616(m) cardbus */
204 
205 #define EMU1616_COMMON_TEXTS \
206 	"Silence", \
207 	PAIR_TEXTS("Mic", "A", "B"), \
208 	LR_TEXTS("ADC1"), \
209 	LR_TEXTS("ADC2"), \
210 	LR_TEXTS("SPDIF"), \
211 	ADAT_TEXTS("")
212 
213 static const char * const emu1616_src_texts[] = {
214 	EMU1616_COMMON_TEXTS,
215 	DSP_TEXTS,
216 };
217 
218 static const unsigned short emu1616_src_regs[] = {
219 	EMU_SRC_SILENCE,
220 	PAIR_REGS(EMU_SRC_DOCK_MIC, _A, _B),
221 	LR_REGS(EMU_SRC_DOCK_ADC1),
222 	LR_REGS(EMU_SRC_DOCK_ADC2),
223 	LR_REGS(EMU_SRC_MDOCK_SPDIF),
224 	ADAT_REGS(EMU_SRC_MDOCK_ADAT),
225 	EMU32_SRC_REGS,
226 };
227 static_assert(ARRAY_SIZE(emu1616_src_regs) == ARRAY_SIZE(emu1616_src_texts));
228 
229 /* 0404 rev1 & rev2 */
230 
231 #define EMU0404_COMMON_TEXTS \
232 	"Silence", \
233 	LR_TEXTS("ADC"), \
234 	LR_TEXTS("SPDIF")
235 
236 static const char * const emu0404_src_texts[] = {
237 	EMU0404_COMMON_TEXTS,
238 	DSP_TEXTS,
239 };
240 
241 static const unsigned short emu0404_src_regs[] = {
242 	EMU_SRC_SILENCE,
243 	LR_REGS(EMU_SRC_HAMOA_ADC),
244 	LR_REGS(EMU_SRC_HANA_SPDIF),
245 	EMU32_SRC_REGS,
246 };
247 static_assert(ARRAY_SIZE(emu0404_src_regs) == ARRAY_SIZE(emu0404_src_texts));
248 
249 /*
250  * Data destinations - physical EMU outputs.
251  * Each destination has an enum mixer control to choose a data source
252  */
253 
254 #define LR_CTLS(base) LR_PS(base, " Playback Enum")
255 #define ADAT_CTLS(pfx) ADAT_PS(pfx, " Playback Enum")
256 
257 /* 1010 rev1 */
258 
259 static const char * const emu1010_output_texts[] = {
260 	LR_CTLS("Dock DAC1"),
261 	LR_CTLS("Dock DAC2"),
262 	LR_CTLS("Dock DAC3"),
263 	LR_CTLS("Dock DAC4"),
264 	LR_CTLS("Dock Phones"),
265 	LR_CTLS("Dock SPDIF"),
266 	LR_CTLS("0202 DAC"),
267 	LR_CTLS("1010 SPDIF"),
268 	ADAT_CTLS("1010 "),
269 };
270 static_assert(ARRAY_SIZE(emu1010_output_texts) <= NUM_OUTPUT_DESTS);
271 
272 static const unsigned short emu1010_output_dst[] = {
273 	LR_REGS(EMU_DST_DOCK_DAC1),
274 	LR_REGS(EMU_DST_DOCK_DAC2),
275 	LR_REGS(EMU_DST_DOCK_DAC3),
276 	LR_REGS(EMU_DST_DOCK_DAC4),
277 	LR_REGS(EMU_DST_DOCK_PHONES),
278 	LR_REGS(EMU_DST_DOCK_SPDIF),
279 	LR_REGS(EMU_DST_HAMOA_DAC),
280 	LR_REGS(EMU_DST_HANA_SPDIF),
281 	ADAT_REGS(EMU_DST_HANA_ADAT),
282 };
283 static_assert(ARRAY_SIZE(emu1010_output_dst) == ARRAY_SIZE(emu1010_output_texts));
284 
285 static const unsigned short emu1010_output_dflt[] = {
286 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
287 	EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
288 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5,
289 	EMU_SRC_ALICE_EMU32A+6, EMU_SRC_ALICE_EMU32A+7,
290 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
291 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
292 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
293 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
294 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1, EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
295 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5, EMU_SRC_ALICE_EMU32A+6, EMU_SRC_ALICE_EMU32A+7,
296 };
297 static_assert(ARRAY_SIZE(emu1010_output_dflt) == ARRAY_SIZE(emu1010_output_dst));
298 
299 /* 1010 rev2 */
300 
301 static const char * const snd_emu1010b_output_texts[] = {
302 	LR_CTLS("Dock DAC1"),
303 	LR_CTLS("Dock DAC2"),
304 	LR_CTLS("Dock DAC3"),
305 	LR_CTLS("Dock SPDIF"),
306 	ADAT_CTLS("Dock "),
307 	LR_CTLS("0202 DAC"),
308 	LR_CTLS("1010 SPDIF"),
309 	ADAT_CTLS("1010 "),
310 };
311 static_assert(ARRAY_SIZE(snd_emu1010b_output_texts) <= NUM_OUTPUT_DESTS);
312 
313 static const unsigned short emu1010b_output_dst[] = {
314 	LR_REGS(EMU_DST_DOCK_DAC1),
315 	LR_REGS(EMU_DST_DOCK_DAC2),
316 	LR_REGS(EMU_DST_DOCK_DAC3),
317 	LR_REGS(EMU_DST_MDOCK_SPDIF),
318 	ADAT_REGS(EMU_DST_MDOCK_ADAT),
319 	LR_REGS(EMU_DST_HAMOA_DAC),
320 	LR_REGS(EMU_DST_HANA_SPDIF),
321 	ADAT_REGS(EMU_DST_HANA_ADAT),
322 };
323 static_assert(ARRAY_SIZE(emu1010b_output_dst) == ARRAY_SIZE(snd_emu1010b_output_texts));
324 
325 static const unsigned short emu1010b_output_dflt[] = {
326 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
327 	EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
328 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5,
329 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
330 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1, EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
331 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5, EMU_SRC_ALICE_EMU32A+6, EMU_SRC_ALICE_EMU32A+7,
332 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
333 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
334 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1, EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
335 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5, EMU_SRC_ALICE_EMU32A+6, EMU_SRC_ALICE_EMU32A+7,
336 };
337 
338 /* 1616(m) cardbus */
339 
340 static const char * const snd_emu1616_output_texts[] = {
341 	LR_CTLS("Dock DAC1"),
342 	LR_CTLS("Dock DAC2"),
343 	LR_CTLS("Dock DAC3"),
344 	LR_CTLS("Dock SPDIF"),
345 	ADAT_CTLS("Dock "),
346 	LR_CTLS("Mana DAC"),
347 };
348 static_assert(ARRAY_SIZE(snd_emu1616_output_texts) <= NUM_OUTPUT_DESTS);
349 
350 static const unsigned short emu1616_output_dst[] = {
351 	LR_REGS(EMU_DST_DOCK_DAC1),
352 	LR_REGS(EMU_DST_DOCK_DAC2),
353 	LR_REGS(EMU_DST_DOCK_DAC3),
354 	LR_REGS(EMU_DST_MDOCK_SPDIF),
355 	ADAT_REGS(EMU_DST_MDOCK_ADAT),
356 	EMU_DST_MANA_DAC_LEFT, EMU_DST_MANA_DAC_RIGHT,
357 };
358 static_assert(ARRAY_SIZE(emu1616_output_dst) == ARRAY_SIZE(snd_emu1616_output_texts));
359 
360 static const unsigned short emu1616_output_dflt[] = {
361 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
362 	EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
363 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5,
364 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
365 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1, EMU_SRC_ALICE_EMU32A+2, EMU_SRC_ALICE_EMU32A+3,
366 	EMU_SRC_ALICE_EMU32A+4, EMU_SRC_ALICE_EMU32A+5, EMU_SRC_ALICE_EMU32A+6, EMU_SRC_ALICE_EMU32A+7,
367 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
368 };
369 static_assert(ARRAY_SIZE(emu1616_output_dflt) == ARRAY_SIZE(emu1616_output_dst));
370 
371 /* 0404 rev1 & rev2 */
372 
373 static const char * const snd_emu0404_output_texts[] = {
374 	LR_CTLS("DAC"),
375 	LR_CTLS("SPDIF"),
376 };
377 static_assert(ARRAY_SIZE(snd_emu0404_output_texts) <= NUM_OUTPUT_DESTS);
378 
379 static const unsigned short emu0404_output_dst[] = {
380 	LR_REGS(EMU_DST_HAMOA_DAC),
381 	LR_REGS(EMU_DST_HANA_SPDIF),
382 };
383 static_assert(ARRAY_SIZE(emu0404_output_dst) == ARRAY_SIZE(snd_emu0404_output_texts));
384 
385 static const unsigned short emu0404_output_dflt[] = {
386 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
387 	EMU_SRC_ALICE_EMU32A+0, EMU_SRC_ALICE_EMU32A+1,
388 };
389 static_assert(ARRAY_SIZE(emu0404_output_dflt) == ARRAY_SIZE(emu0404_output_dst));
390 
391 /*
392  * Data destinations - FPGA outputs going to Alice2 (Audigy) for
393  *   capture (EMU32 + I2S links)
394  * Each destination has an enum mixer control to choose a data source
395  */
396 
397 static const char * const emu1010_input_texts[] = {
398 	"DSP 0 Capture Enum",
399 	"DSP 1 Capture Enum",
400 	"DSP 2 Capture Enum",
401 	"DSP 3 Capture Enum",
402 	"DSP 4 Capture Enum",
403 	"DSP 5 Capture Enum",
404 	"DSP 6 Capture Enum",
405 	"DSP 7 Capture Enum",
406 	"DSP 8 Capture Enum",
407 	"DSP 9 Capture Enum",
408 	"DSP A Capture Enum",
409 	"DSP B Capture Enum",
410 	"DSP C Capture Enum",
411 	"DSP D Capture Enum",
412 	"DSP E Capture Enum",
413 	"DSP F Capture Enum",
414 	/* These exist only on rev1 EMU1010 cards. */
415 	"DSP 10 Capture Enum",
416 	"DSP 11 Capture Enum",
417 	"DSP 12 Capture Enum",
418 	"DSP 13 Capture Enum",
419 	"DSP 14 Capture Enum",
420 	"DSP 15 Capture Enum",
421 };
422 static_assert(ARRAY_SIZE(emu1010_input_texts) <= NUM_INPUT_DESTS);
423 
424 static const unsigned short emu1010_input_dst[] = {
425 	EMU_DST_ALICE2_EMU32_0,
426 	EMU_DST_ALICE2_EMU32_1,
427 	EMU_DST_ALICE2_EMU32_2,
428 	EMU_DST_ALICE2_EMU32_3,
429 	EMU_DST_ALICE2_EMU32_4,
430 	EMU_DST_ALICE2_EMU32_5,
431 	EMU_DST_ALICE2_EMU32_6,
432 	EMU_DST_ALICE2_EMU32_7,
433 	EMU_DST_ALICE2_EMU32_8,
434 	EMU_DST_ALICE2_EMU32_9,
435 	EMU_DST_ALICE2_EMU32_A,
436 	EMU_DST_ALICE2_EMU32_B,
437 	EMU_DST_ALICE2_EMU32_C,
438 	EMU_DST_ALICE2_EMU32_D,
439 	EMU_DST_ALICE2_EMU32_E,
440 	EMU_DST_ALICE2_EMU32_F,
441 	/* These exist only on rev1 EMU1010 cards. */
442 	EMU_DST_ALICE_I2S0_LEFT,
443 	EMU_DST_ALICE_I2S0_RIGHT,
444 	EMU_DST_ALICE_I2S1_LEFT,
445 	EMU_DST_ALICE_I2S1_RIGHT,
446 	EMU_DST_ALICE_I2S2_LEFT,
447 	EMU_DST_ALICE_I2S2_RIGHT,
448 };
449 static_assert(ARRAY_SIZE(emu1010_input_dst) == ARRAY_SIZE(emu1010_input_texts));
450 
451 static const unsigned short emu1010_input_dflt[] = {
452 	EMU_SRC_DOCK_MIC_A1,
453 	EMU_SRC_DOCK_MIC_B1,
454 	EMU_SRC_HAMOA_ADC_LEFT1,
455 	EMU_SRC_HAMOA_ADC_RIGHT1,
456 	EMU_SRC_DOCK_ADC1_LEFT1,
457 	EMU_SRC_DOCK_ADC1_RIGHT1,
458 	EMU_SRC_DOCK_ADC2_LEFT1,
459 	EMU_SRC_DOCK_ADC2_RIGHT1,
460 	/* Pavel Hofman - setting defaults for all capture channels.
461 	 * Defaults only, users will set their own values anyways, let's
462 	 * just copy/paste. */
463 	EMU_SRC_DOCK_MIC_A1,
464 	EMU_SRC_DOCK_MIC_B1,
465 	EMU_SRC_HAMOA_ADC_LEFT1,
466 	EMU_SRC_HAMOA_ADC_RIGHT1,
467 	EMU_SRC_DOCK_ADC1_LEFT1,
468 	EMU_SRC_DOCK_ADC1_RIGHT1,
469 	EMU_SRC_DOCK_ADC2_LEFT1,
470 	EMU_SRC_DOCK_ADC2_RIGHT1,
471 
472 	EMU_SRC_DOCK_ADC1_LEFT1,
473 	EMU_SRC_DOCK_ADC1_RIGHT1,
474 	EMU_SRC_DOCK_ADC2_LEFT1,
475 	EMU_SRC_DOCK_ADC2_RIGHT1,
476 	EMU_SRC_DOCK_ADC3_LEFT1,
477 	EMU_SRC_DOCK_ADC3_RIGHT1,
478 };
479 static_assert(ARRAY_SIZE(emu1010_input_dflt) == ARRAY_SIZE(emu1010_input_dst));
480 
481 static const unsigned short emu0404_input_dflt[] = {
482 	EMU_SRC_HAMOA_ADC_LEFT1,
483 	EMU_SRC_HAMOA_ADC_RIGHT1,
484 	EMU_SRC_SILENCE,
485 	EMU_SRC_SILENCE,
486 	EMU_SRC_SILENCE,
487 	EMU_SRC_SILENCE,
488 	EMU_SRC_SILENCE,
489 	EMU_SRC_SILENCE,
490 	EMU_SRC_HANA_SPDIF_LEFT1,
491 	EMU_SRC_HANA_SPDIF_RIGHT1,
492 	EMU_SRC_SILENCE,
493 	EMU_SRC_SILENCE,
494 	EMU_SRC_SILENCE,
495 	EMU_SRC_SILENCE,
496 	EMU_SRC_SILENCE,
497 	EMU_SRC_SILENCE,
498 };
499 
500 struct snd_emu1010_routing_info {
501 	const char * const *src_texts;
502 	const char * const *out_texts;
503 	const unsigned short *src_regs;
504 	const unsigned short *out_regs;
505 	const unsigned short *in_regs;
506 	const unsigned short *out_dflts;
507 	const unsigned short *in_dflts;
508 	unsigned n_srcs;
509 	unsigned n_outs;
510 	unsigned n_ins;
511 };
512 
513 static const struct snd_emu1010_routing_info emu1010_routing_info[] = {
514 	{
515 		/* rev1 1010 */
516 		.src_regs = emu1010_src_regs,
517 		.src_texts = emu1010_src_texts,
518 		.n_srcs = ARRAY_SIZE(emu1010_src_texts),
519 
520 		.out_dflts = emu1010_output_dflt,
521 		.out_regs = emu1010_output_dst,
522 		.out_texts = emu1010_output_texts,
523 		.n_outs = ARRAY_SIZE(emu1010_output_dst),
524 
525 		.in_dflts = emu1010_input_dflt,
526 		.in_regs = emu1010_input_dst,
527 		.n_ins = ARRAY_SIZE(emu1010_input_dst),
528 	},
529 	{
530 		/* rev2 1010 */
531 		.src_regs = emu1010b_src_regs,
532 		.src_texts = emu1010b_src_texts,
533 		.n_srcs = ARRAY_SIZE(emu1010b_src_texts),
534 
535 		.out_dflts = emu1010b_output_dflt,
536 		.out_regs = emu1010b_output_dst,
537 		.out_texts = snd_emu1010b_output_texts,
538 		.n_outs = ARRAY_SIZE(emu1010b_output_dst),
539 
540 		.in_dflts = emu1010_input_dflt,
541 		.in_regs = emu1010_input_dst,
542 		.n_ins = ARRAY_SIZE(emu1010_input_dst) - 6,
543 	},
544 	{
545 		/* 1616(m) cardbus */
546 		.src_regs = emu1616_src_regs,
547 		.src_texts = emu1616_src_texts,
548 		.n_srcs = ARRAY_SIZE(emu1616_src_texts),
549 
550 		.out_dflts = emu1616_output_dflt,
551 		.out_regs = emu1616_output_dst,
552 		.out_texts = snd_emu1616_output_texts,
553 		.n_outs = ARRAY_SIZE(emu1616_output_dst),
554 
555 		.in_dflts = emu1010_input_dflt,
556 		.in_regs = emu1010_input_dst,
557 		.n_ins = ARRAY_SIZE(emu1010_input_dst) - 6,
558 	},
559 	{
560 		/* 0404 */
561 		.src_regs = emu0404_src_regs,
562 		.src_texts = emu0404_src_texts,
563 		.n_srcs = ARRAY_SIZE(emu0404_src_texts),
564 
565 		.out_dflts = emu0404_output_dflt,
566 		.out_regs = emu0404_output_dst,
567 		.out_texts = snd_emu0404_output_texts,
568 		.n_outs = ARRAY_SIZE(emu0404_output_dflt),
569 
570 		.in_dflts = emu0404_input_dflt,
571 		.in_regs = emu1010_input_dst,
572 		.n_ins = ARRAY_SIZE(emu1010_input_dst) - 6,
573 	},
574 };
575 
576 static unsigned emu1010_idx(struct snd_emu10k1 *emu)
577 {
578 	return emu->card_capabilities->emu_model - 1;
579 }
580 
581 static void snd_emu1010_output_source_apply(struct snd_emu10k1 *emu,
582 					    int channel, int src)
583 {
584 	const struct snd_emu1010_routing_info *emu_ri =
585 		&emu1010_routing_info[emu1010_idx(emu)];
586 
587 	snd_emu1010_fpga_link_dst_src_write(emu,
588 		emu_ri->out_regs[channel], emu_ri->src_regs[src]);
589 }
590 
591 static void snd_emu1010_input_source_apply(struct snd_emu10k1 *emu,
592 					   int channel, int src)
593 {
594 	const struct snd_emu1010_routing_info *emu_ri =
595 		&emu1010_routing_info[emu1010_idx(emu)];
596 
597 	snd_emu1010_fpga_link_dst_src_write(emu,
598 		emu_ri->in_regs[channel], emu_ri->src_regs[src]);
599 }
600 
601 static void snd_emu1010_apply_sources(struct snd_emu10k1 *emu)
602 {
603 	const struct snd_emu1010_routing_info *emu_ri =
604 		&emu1010_routing_info[emu1010_idx(emu)];
605 
606 	for (unsigned i = 0; i < emu_ri->n_outs; i++)
607 		snd_emu1010_output_source_apply(
608 			emu, i, emu->emu1010.output_source[i]);
609 	for (unsigned i = 0; i < emu_ri->n_ins; i++)
610 		snd_emu1010_input_source_apply(
611 			emu, i, emu->emu1010.input_source[i]);
612 }
613 
614 static u8 emu1010_map_source(const struct snd_emu1010_routing_info *emu_ri,
615 			     unsigned val)
616 {
617 	for (unsigned i = 0; i < emu_ri->n_srcs; i++)
618 		if (val == emu_ri->src_regs[i])
619 			return i;
620 	return 0;
621 }
622 
623 static int snd_emu1010_input_output_source_info(struct snd_kcontrol *kcontrol,
624 						struct snd_ctl_elem_info *uinfo)
625 {
626 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
627 	const struct snd_emu1010_routing_info *emu_ri =
628 		&emu1010_routing_info[emu1010_idx(emu)];
629 
630 	return snd_ctl_enum_info(uinfo, 1, emu_ri->n_srcs, emu_ri->src_texts);
631 }
632 
633 static int snd_emu1010_output_source_get(struct snd_kcontrol *kcontrol,
634                                  struct snd_ctl_elem_value *ucontrol)
635 {
636 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
637 	const struct snd_emu1010_routing_info *emu_ri =
638 		&emu1010_routing_info[emu1010_idx(emu)];
639 	unsigned channel = kcontrol->private_value;
640 
641 	if (channel >= emu_ri->n_outs)
642 		return -EINVAL;
643 	ucontrol->value.enumerated.item[0] = emu->emu1010.output_source[channel];
644 	return 0;
645 }
646 
647 static int snd_emu1010_output_source_put(struct snd_kcontrol *kcontrol,
648                                  struct snd_ctl_elem_value *ucontrol)
649 {
650 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
651 	const struct snd_emu1010_routing_info *emu_ri =
652 		&emu1010_routing_info[emu1010_idx(emu)];
653 	unsigned val = ucontrol->value.enumerated.item[0];
654 	unsigned channel = kcontrol->private_value;
655 	int change;
656 
657 	if (val >= emu_ri->n_srcs)
658 		return -EINVAL;
659 	if (channel >= emu_ri->n_outs)
660 		return -EINVAL;
661 	change = (emu->emu1010.output_source[channel] != val);
662 	if (change) {
663 		emu->emu1010.output_source[channel] = val;
664 		snd_emu1010_fpga_lock(emu);
665 		snd_emu1010_output_source_apply(emu, channel, val);
666 		snd_emu1010_fpga_unlock(emu);
667 	}
668 	return change;
669 }
670 
671 static const struct snd_kcontrol_new emu1010_output_source_ctl = {
672 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
673 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
674 	.info = snd_emu1010_input_output_source_info,
675 	.get = snd_emu1010_output_source_get,
676 	.put = snd_emu1010_output_source_put
677 };
678 
679 static int snd_emu1010_input_source_get(struct snd_kcontrol *kcontrol,
680                                  struct snd_ctl_elem_value *ucontrol)
681 {
682 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
683 	const struct snd_emu1010_routing_info *emu_ri =
684 		&emu1010_routing_info[emu1010_idx(emu)];
685 	unsigned channel = kcontrol->private_value;
686 
687 	if (channel >= emu_ri->n_ins)
688 		return -EINVAL;
689 	ucontrol->value.enumerated.item[0] = emu->emu1010.input_source[channel];
690 	return 0;
691 }
692 
693 static int snd_emu1010_input_source_put(struct snd_kcontrol *kcontrol,
694                                  struct snd_ctl_elem_value *ucontrol)
695 {
696 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
697 	const struct snd_emu1010_routing_info *emu_ri =
698 		&emu1010_routing_info[emu1010_idx(emu)];
699 	unsigned val = ucontrol->value.enumerated.item[0];
700 	unsigned channel = kcontrol->private_value;
701 	int change;
702 
703 	if (val >= emu_ri->n_srcs)
704 		return -EINVAL;
705 	if (channel >= emu_ri->n_ins)
706 		return -EINVAL;
707 	change = (emu->emu1010.input_source[channel] != val);
708 	if (change) {
709 		emu->emu1010.input_source[channel] = val;
710 		snd_emu1010_fpga_lock(emu);
711 		snd_emu1010_input_source_apply(emu, channel, val);
712 		snd_emu1010_fpga_unlock(emu);
713 	}
714 	return change;
715 }
716 
717 static const struct snd_kcontrol_new emu1010_input_source_ctl = {
718 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
719 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
720 	.info = snd_emu1010_input_output_source_info,
721 	.get = snd_emu1010_input_source_get,
722 	.put = snd_emu1010_input_source_put
723 };
724 
725 static int add_emu1010_source_mixers(struct snd_emu10k1 *emu)
726 {
727 	const struct snd_emu1010_routing_info *emu_ri =
728 		&emu1010_routing_info[emu1010_idx(emu)];
729 	int err;
730 
731 	err = add_ctls(emu, &emu1010_output_source_ctl,
732 		       emu_ri->out_texts, emu_ri->n_outs);
733 	if (err < 0)
734 		return err;
735 	err = add_ctls(emu, &emu1010_input_source_ctl,
736 		       emu1010_input_texts, emu_ri->n_ins);
737 	return err;
738 }
739 
740 
741 static const char * const snd_emu1010_adc_pads[] = {
742 	"ADC1 14dB PAD 0202 Capture Switch",
743 	"ADC1 14dB PAD Audio Dock Capture Switch",
744 	"ADC2 14dB PAD Audio Dock Capture Switch",
745 	"ADC3 14dB PAD Audio Dock Capture Switch",
746 };
747 
748 static const unsigned short snd_emu1010_adc_pad_regs[] = {
749 	EMU_HANA_0202_ADC_PAD1,
750 	EMU_HANA_DOCK_ADC_PAD1,
751 	EMU_HANA_DOCK_ADC_PAD2,
752 	EMU_HANA_DOCK_ADC_PAD3,
753 };
754 
755 #define snd_emu1010_adc_pads_info	snd_ctl_boolean_mono_info
756 
757 static int snd_emu1010_adc_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
758 {
759 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
760 	unsigned int mask = snd_emu1010_adc_pad_regs[kcontrol->private_value];
761 
762 	ucontrol->value.integer.value[0] = (emu->emu1010.adc_pads & mask) ? 1 : 0;
763 	return 0;
764 }
765 
766 static int snd_emu1010_adc_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
767 {
768 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
769 	unsigned int mask = snd_emu1010_adc_pad_regs[kcontrol->private_value];
770 	unsigned int val, cache;
771 	int change;
772 
773 	val = ucontrol->value.integer.value[0];
774 	cache = emu->emu1010.adc_pads;
775 	if (val == 1)
776 		cache = cache | mask;
777 	else
778 		cache = cache & ~mask;
779 	change = (cache != emu->emu1010.adc_pads);
780 	if (change) {
781 		snd_emu1010_fpga_write_lock(emu, EMU_HANA_ADC_PADS, cache );
782 	        emu->emu1010.adc_pads = cache;
783 	}
784 
785 	return change;
786 }
787 
788 static const struct snd_kcontrol_new emu1010_adc_pads_ctl = {
789 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
790 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
791 	.info = snd_emu1010_adc_pads_info,
792 	.get = snd_emu1010_adc_pads_get,
793 	.put = snd_emu1010_adc_pads_put
794 };
795 
796 
797 static const char * const snd_emu1010_dac_pads[] = {
798 	"DAC1 0202 14dB PAD Playback Switch",
799 	"DAC1 Audio Dock 14dB PAD Playback Switch",
800 	"DAC2 Audio Dock 14dB PAD Playback Switch",
801 	"DAC3 Audio Dock 14dB PAD Playback Switch",
802 	"DAC4 Audio Dock 14dB PAD Playback Switch",
803 };
804 
805 static const unsigned short snd_emu1010_dac_regs[] = {
806 	EMU_HANA_0202_DAC_PAD1,
807 	EMU_HANA_DOCK_DAC_PAD1,
808 	EMU_HANA_DOCK_DAC_PAD2,
809 	EMU_HANA_DOCK_DAC_PAD3,
810 	EMU_HANA_DOCK_DAC_PAD4,
811 };
812 
813 #define snd_emu1010_dac_pads_info	snd_ctl_boolean_mono_info
814 
815 static int snd_emu1010_dac_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
816 {
817 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
818 	unsigned int mask = snd_emu1010_dac_regs[kcontrol->private_value];
819 
820 	ucontrol->value.integer.value[0] = (emu->emu1010.dac_pads & mask) ? 1 : 0;
821 	return 0;
822 }
823 
824 static int snd_emu1010_dac_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
825 {
826 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
827 	unsigned int mask = snd_emu1010_dac_regs[kcontrol->private_value];
828 	unsigned int val, cache;
829 	int change;
830 
831 	val = ucontrol->value.integer.value[0];
832 	cache = emu->emu1010.dac_pads;
833 	if (val == 1)
834 		cache = cache | mask;
835 	else
836 		cache = cache & ~mask;
837 	change = (cache != emu->emu1010.dac_pads);
838 	if (change) {
839 		snd_emu1010_fpga_write_lock(emu, EMU_HANA_DAC_PADS, cache );
840 	        emu->emu1010.dac_pads = cache;
841 	}
842 
843 	return change;
844 }
845 
846 static const struct snd_kcontrol_new emu1010_dac_pads_ctl = {
847 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
848 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
849 	.info = snd_emu1010_dac_pads_info,
850 	.get = snd_emu1010_dac_pads_get,
851 	.put = snd_emu1010_dac_pads_put
852 };
853 
854 
855 struct snd_emu1010_pads_info {
856 	const char * const *adc_ctls, * const *dac_ctls;
857 	unsigned n_adc_ctls, n_dac_ctls;
858 };
859 
860 static const struct snd_emu1010_pads_info emu1010_pads_info[] = {
861 	{
862 		/* rev1 1010 */
863 		.adc_ctls = snd_emu1010_adc_pads,
864 		.n_adc_ctls = ARRAY_SIZE(snd_emu1010_adc_pads),
865 		.dac_ctls = snd_emu1010_dac_pads,
866 		.n_dac_ctls = ARRAY_SIZE(snd_emu1010_dac_pads),
867 	},
868 	{
869 		/* rev2 1010 */
870 		.adc_ctls = snd_emu1010_adc_pads,
871 		.n_adc_ctls = ARRAY_SIZE(snd_emu1010_adc_pads) - 1,
872 		.dac_ctls = snd_emu1010_dac_pads,
873 		.n_dac_ctls = ARRAY_SIZE(snd_emu1010_dac_pads) - 1,
874 	},
875 	{
876 		/* 1616(m) cardbus */
877 		.adc_ctls = snd_emu1010_adc_pads + 1,
878 		.n_adc_ctls = ARRAY_SIZE(snd_emu1010_adc_pads) - 2,
879 		.dac_ctls = snd_emu1010_dac_pads + 1,
880 		.n_dac_ctls = ARRAY_SIZE(snd_emu1010_dac_pads) - 2,
881 	},
882 	{
883 		/* 0404 */
884 		.adc_ctls = NULL,
885 		.n_adc_ctls = 0,
886 		.dac_ctls = NULL,
887 		.n_dac_ctls = 0,
888 	},
889 };
890 
891 static const char * const emu1010_clock_texts[] = {
892 	"44100", "48000", "SPDIF", "ADAT", "Dock", "BNC"
893 };
894 
895 static const u8 emu1010_clock_vals[] = {
896 	EMU_HANA_WCLOCK_INT_44_1K,
897 	EMU_HANA_WCLOCK_INT_48K,
898 	EMU_HANA_WCLOCK_HANA_SPDIF_IN,
899 	EMU_HANA_WCLOCK_HANA_ADAT_IN,
900 	EMU_HANA_WCLOCK_2ND_HANA,
901 	EMU_HANA_WCLOCK_SYNC_BNC,
902 };
903 
904 static const char * const emu0404_clock_texts[] = {
905 	"44100", "48000", "SPDIF", "BNC"
906 };
907 
908 static const u8 emu0404_clock_vals[] = {
909 	EMU_HANA_WCLOCK_INT_44_1K,
910 	EMU_HANA_WCLOCK_INT_48K,
911 	EMU_HANA_WCLOCK_HANA_SPDIF_IN,
912 	EMU_HANA_WCLOCK_SYNC_BNC,
913 };
914 
915 struct snd_emu1010_clock_info {
916 	const char * const *texts;
917 	const u8 *vals;
918 	unsigned num;
919 };
920 
921 static const struct snd_emu1010_clock_info emu1010_clock_info[] = {
922 	{
923 		// rev1 1010
924 		.texts = emu1010_clock_texts,
925 		.vals = emu1010_clock_vals,
926 		.num = ARRAY_SIZE(emu1010_clock_vals),
927 	},
928 	{
929 		// rev2 1010
930 		.texts = emu1010_clock_texts,
931 		.vals = emu1010_clock_vals,
932 		.num = ARRAY_SIZE(emu1010_clock_vals) - 1,
933 	},
934 	{
935 		// 1616(m) CardBus
936 		.texts = emu1010_clock_texts,
937 		// TODO: determine what is actually available.
938 		// Pedantically, *every* source comes from the 2nd FPGA, as the
939 		// card itself has no own (digital) audio ports. The user manual
940 		// claims that ADAT and S/PDIF clock sources are separate, which
941 		// can mean two things: either E-MU mapped the dock's sources to
942 		// the primary ones, or they determine the meaning of the "Dock"
943 		// source depending on how the ports are actually configured
944 		// (which the 2nd FPGA must be doing anyway).
945 		.vals = emu1010_clock_vals,
946 		.num = ARRAY_SIZE(emu1010_clock_vals),
947 	},
948 	{
949 		// 0404
950 		.texts = emu0404_clock_texts,
951 		.vals = emu0404_clock_vals,
952 		.num = ARRAY_SIZE(emu0404_clock_vals),
953 	},
954 };
955 
956 static int snd_emu1010_clock_source_info(struct snd_kcontrol *kcontrol,
957 					  struct snd_ctl_elem_info *uinfo)
958 {
959 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
960 	const struct snd_emu1010_clock_info *emu_ci =
961 		&emu1010_clock_info[emu1010_idx(emu)];
962 
963 	return snd_ctl_enum_info(uinfo, 1, emu_ci->num, emu_ci->texts);
964 }
965 
966 static int snd_emu1010_clock_source_get(struct snd_kcontrol *kcontrol,
967 					struct snd_ctl_elem_value *ucontrol)
968 {
969 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
970 
971 	ucontrol->value.enumerated.item[0] = emu->emu1010.clock_source;
972 	return 0;
973 }
974 
975 static int snd_emu1010_clock_source_put(struct snd_kcontrol *kcontrol,
976 					struct snd_ctl_elem_value *ucontrol)
977 {
978 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
979 	const struct snd_emu1010_clock_info *emu_ci =
980 		&emu1010_clock_info[emu1010_idx(emu)];
981 	unsigned int val;
982 	int change = 0;
983 
984 	val = ucontrol->value.enumerated.item[0] ;
985 	if (val >= emu_ci->num)
986 		return -EINVAL;
987 	snd_emu1010_fpga_lock(emu);
988 	spin_lock_irq(&emu->reg_lock);
989 	change = (emu->emu1010.clock_source != val);
990 	if (change) {
991 		emu->emu1010.clock_source = val;
992 		emu->emu1010.wclock = emu_ci->vals[val];
993 		snd_emu1010_update_clock(emu);
994 
995 		snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE);
996 		snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK, emu->emu1010.wclock);
997 		spin_unlock_irq(&emu->reg_lock);
998 
999 		msleep(10);  // Allow DLL to settle
1000 		snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE);
1001 	} else {
1002 		spin_unlock_irq(&emu->reg_lock);
1003 	}
1004 	snd_emu1010_fpga_unlock(emu);
1005 	return change;
1006 }
1007 
1008 static const struct snd_kcontrol_new snd_emu1010_clock_source =
1009 {
1010 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1011 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1012 	.name = "Clock Source",
1013 	.count = 1,
1014 	.info = snd_emu1010_clock_source_info,
1015 	.get = snd_emu1010_clock_source_get,
1016 	.put = snd_emu1010_clock_source_put
1017 };
1018 
1019 static int snd_emu1010_clock_fallback_info(struct snd_kcontrol *kcontrol,
1020 					  struct snd_ctl_elem_info *uinfo)
1021 {
1022 	static const char * const texts[2] = {
1023 		"44100", "48000"
1024 	};
1025 
1026 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
1027 }
1028 
1029 static int snd_emu1010_clock_fallback_get(struct snd_kcontrol *kcontrol,
1030 					  struct snd_ctl_elem_value *ucontrol)
1031 {
1032 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1033 
1034 	ucontrol->value.enumerated.item[0] = emu->emu1010.clock_fallback;
1035 	return 0;
1036 }
1037 
1038 static int snd_emu1010_clock_fallback_put(struct snd_kcontrol *kcontrol,
1039 					  struct snd_ctl_elem_value *ucontrol)
1040 {
1041 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1042 	unsigned int val = ucontrol->value.enumerated.item[0];
1043 	int change;
1044 
1045 	if (val >= 2)
1046 		return -EINVAL;
1047 	change = (emu->emu1010.clock_fallback != val);
1048 	if (change) {
1049 		emu->emu1010.clock_fallback = val;
1050 		snd_emu1010_fpga_write_lock(emu, EMU_HANA_DEFCLOCK, 1 - val);
1051 	}
1052 	return change;
1053 }
1054 
1055 static const struct snd_kcontrol_new snd_emu1010_clock_fallback =
1056 {
1057 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1058 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1059 	.name = "Clock Fallback",
1060 	.count = 1,
1061 	.info = snd_emu1010_clock_fallback_info,
1062 	.get = snd_emu1010_clock_fallback_get,
1063 	.put = snd_emu1010_clock_fallback_put
1064 };
1065 
1066 static int snd_emu1010_optical_out_info(struct snd_kcontrol *kcontrol,
1067 					  struct snd_ctl_elem_info *uinfo)
1068 {
1069 	static const char * const texts[2] = {
1070 		"SPDIF", "ADAT"
1071 	};
1072 
1073 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
1074 }
1075 
1076 static int snd_emu1010_optical_out_get(struct snd_kcontrol *kcontrol,
1077 					struct snd_ctl_elem_value *ucontrol)
1078 {
1079 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1080 
1081 	ucontrol->value.enumerated.item[0] = emu->emu1010.optical_out;
1082 	return 0;
1083 }
1084 
1085 static int snd_emu1010_optical_out_put(struct snd_kcontrol *kcontrol,
1086 					struct snd_ctl_elem_value *ucontrol)
1087 {
1088 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1089 	unsigned int val;
1090 	u32 tmp;
1091 	int change = 0;
1092 
1093 	val = ucontrol->value.enumerated.item[0];
1094 	/* Limit: uinfo->value.enumerated.items = 2; */
1095 	if (val >= 2)
1096 		return -EINVAL;
1097 	change = (emu->emu1010.optical_out != val);
1098 	if (change) {
1099 		emu->emu1010.optical_out = val;
1100 		tmp = (emu->emu1010.optical_in ? EMU_HANA_OPTICAL_IN_ADAT : EMU_HANA_OPTICAL_IN_SPDIF) |
1101 			(emu->emu1010.optical_out ? EMU_HANA_OPTICAL_OUT_ADAT : EMU_HANA_OPTICAL_OUT_SPDIF);
1102 		snd_emu1010_fpga_write_lock(emu, EMU_HANA_OPTICAL_TYPE, tmp);
1103 	}
1104 	return change;
1105 }
1106 
1107 static const struct snd_kcontrol_new snd_emu1010_optical_out = {
1108 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1109 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1110 	.name =         "Optical Output Mode",
1111 	.count =	1,
1112 	.info =         snd_emu1010_optical_out_info,
1113 	.get =          snd_emu1010_optical_out_get,
1114 	.put =          snd_emu1010_optical_out_put
1115 };
1116 
1117 static int snd_emu1010_optical_in_info(struct snd_kcontrol *kcontrol,
1118 					  struct snd_ctl_elem_info *uinfo)
1119 {
1120 	static const char * const texts[2] = {
1121 		"SPDIF", "ADAT"
1122 	};
1123 
1124 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
1125 }
1126 
1127 static int snd_emu1010_optical_in_get(struct snd_kcontrol *kcontrol,
1128 					struct snd_ctl_elem_value *ucontrol)
1129 {
1130 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1131 
1132 	ucontrol->value.enumerated.item[0] = emu->emu1010.optical_in;
1133 	return 0;
1134 }
1135 
1136 static int snd_emu1010_optical_in_put(struct snd_kcontrol *kcontrol,
1137 					struct snd_ctl_elem_value *ucontrol)
1138 {
1139 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1140 	unsigned int val;
1141 	u32 tmp;
1142 	int change = 0;
1143 
1144 	val = ucontrol->value.enumerated.item[0];
1145 	/* Limit: uinfo->value.enumerated.items = 2; */
1146 	if (val >= 2)
1147 		return -EINVAL;
1148 	change = (emu->emu1010.optical_in != val);
1149 	if (change) {
1150 		emu->emu1010.optical_in = val;
1151 		tmp = (emu->emu1010.optical_in ? EMU_HANA_OPTICAL_IN_ADAT : EMU_HANA_OPTICAL_IN_SPDIF) |
1152 			(emu->emu1010.optical_out ? EMU_HANA_OPTICAL_OUT_ADAT : EMU_HANA_OPTICAL_OUT_SPDIF);
1153 		snd_emu1010_fpga_write_lock(emu, EMU_HANA_OPTICAL_TYPE, tmp);
1154 	}
1155 	return change;
1156 }
1157 
1158 static const struct snd_kcontrol_new snd_emu1010_optical_in = {
1159 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1160 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1161 	.name =         "Optical Input Mode",
1162 	.count =	1,
1163 	.info =         snd_emu1010_optical_in_info,
1164 	.get =          snd_emu1010_optical_in_get,
1165 	.put =          snd_emu1010_optical_in_put
1166 };
1167 
1168 static int snd_audigy_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
1169 					  struct snd_ctl_elem_info *uinfo)
1170 {
1171 #if 0
1172 	static const char * const texts[4] = {
1173 		"Unknown1", "Unknown2", "Mic", "Line"
1174 	};
1175 #endif
1176 	static const char * const texts[2] = {
1177 		"Mic", "Line"
1178 	};
1179 
1180 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
1181 }
1182 
1183 static int snd_audigy_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
1184 					struct snd_ctl_elem_value *ucontrol)
1185 {
1186 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1187 
1188 	ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
1189 	return 0;
1190 }
1191 
1192 static int snd_audigy_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
1193 					struct snd_ctl_elem_value *ucontrol)
1194 {
1195 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1196 	unsigned int source_id;
1197 	unsigned int ngain, ogain;
1198 	u16 gpio;
1199 	int change = 0;
1200 	u32 source;
1201 	/* If the capture source has changed,
1202 	 * update the capture volume from the cached value
1203 	 * for the particular source.
1204 	 */
1205 	source_id = ucontrol->value.enumerated.item[0];
1206 	/* Limit: uinfo->value.enumerated.items = 2; */
1207 	/*        emu->i2c_capture_volume */
1208 	if (source_id >= 2)
1209 		return -EINVAL;
1210 	change = (emu->i2c_capture_source != source_id);
1211 	if (change) {
1212 		snd_emu10k1_i2c_write(emu, ADC_MUX, 0); /* Mute input */
1213 		spin_lock_irq(&emu->emu_lock);
1214 		gpio = inw(emu->port + A_IOCFG);
1215 		if (source_id==0)
1216 			outw(gpio | 0x4, emu->port + A_IOCFG);
1217 		else
1218 			outw(gpio & ~0x4, emu->port + A_IOCFG);
1219 		spin_unlock_irq(&emu->emu_lock);
1220 
1221 		ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
1222 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
1223 		if (ngain != ogain)
1224 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
1225 		ngain = emu->i2c_capture_volume[source_id][1]; /* Right */
1226 		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Right */
1227 		if (ngain != ogain)
1228 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
1229 
1230 		source = 1 << (source_id + 2);
1231 		snd_emu10k1_i2c_write(emu, ADC_MUX, source); /* Set source */
1232 		emu->i2c_capture_source = source_id;
1233 	}
1234         return change;
1235 }
1236 
1237 static const struct snd_kcontrol_new snd_audigy_i2c_capture_source =
1238 {
1239 		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1240 		.name =		"Capture Source",
1241 		.info =		snd_audigy_i2c_capture_source_info,
1242 		.get =		snd_audigy_i2c_capture_source_get,
1243 		.put =		snd_audigy_i2c_capture_source_put
1244 };
1245 
1246 static int snd_audigy_i2c_volume_info(struct snd_kcontrol *kcontrol,
1247 				  struct snd_ctl_elem_info *uinfo)
1248 {
1249 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1250 	uinfo->count = 2;
1251 	uinfo->value.integer.min = 0;
1252 	uinfo->value.integer.max = 255;
1253 	return 0;
1254 }
1255 
1256 static int snd_audigy_i2c_volume_get(struct snd_kcontrol *kcontrol,
1257 				 struct snd_ctl_elem_value *ucontrol)
1258 {
1259 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1260 	unsigned int source_id;
1261 
1262 	source_id = kcontrol->private_value;
1263 	/* Limit: emu->i2c_capture_volume */
1264         /*        capture_source: uinfo->value.enumerated.items = 2 */
1265 	if (source_id >= 2)
1266 		return -EINVAL;
1267 
1268 	ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
1269 	ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
1270 	return 0;
1271 }
1272 
1273 static int snd_audigy_i2c_volume_put(struct snd_kcontrol *kcontrol,
1274 				 struct snd_ctl_elem_value *ucontrol)
1275 {
1276 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1277 	unsigned int ogain;
1278 	unsigned int ngain0, ngain1;
1279 	unsigned int source_id;
1280 	int change = 0;
1281 
1282 	source_id = kcontrol->private_value;
1283 	/* Limit: emu->i2c_capture_volume */
1284         /*        capture_source: uinfo->value.enumerated.items = 2 */
1285 	if (source_id >= 2)
1286 		return -EINVAL;
1287 	ngain0 = ucontrol->value.integer.value[0];
1288 	ngain1 = ucontrol->value.integer.value[1];
1289 	if (ngain0 > 0xff)
1290 		return -EINVAL;
1291 	if (ngain1 > 0xff)
1292 		return -EINVAL;
1293 	ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
1294 	if (ogain != ngain0) {
1295 		if (emu->i2c_capture_source == source_id)
1296 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ngain0);
1297 		emu->i2c_capture_volume[source_id][0] = ngain0;
1298 		change = 1;
1299 	}
1300 	ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
1301 	if (ogain != ngain1) {
1302 		if (emu->i2c_capture_source == source_id)
1303 			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ngain1);
1304 		emu->i2c_capture_volume[source_id][1] = ngain1;
1305 		change = 1;
1306 	}
1307 
1308 	return change;
1309 }
1310 
1311 static const struct snd_kcontrol_new i2c_volume_ctl = {
1312 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1313 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1314 	          SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1315 	.info = snd_audigy_i2c_volume_info,
1316 	.get = snd_audigy_i2c_volume_get,
1317 	.put = snd_audigy_i2c_volume_put,
1318 	.tlv = { .p = snd_audigy_db_scale2 }
1319 };
1320 
1321 static const char * const snd_audigy_i2c_volume_ctls[] = {
1322 	"Mic Capture Volume",
1323 	"Line Capture Volume",
1324 };
1325 
1326 #if 0
1327 static int snd_audigy_spdif_output_rate_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1328 {
1329 	static const char * const texts[] = {"44100", "48000", "96000"};
1330 
1331 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
1332 }
1333 
1334 static int snd_audigy_spdif_output_rate_get(struct snd_kcontrol *kcontrol,
1335                                  struct snd_ctl_elem_value *ucontrol)
1336 {
1337 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1338 	unsigned int tmp;
1339 
1340 	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
1341 	switch (tmp & A_SPDIF_RATE_MASK) {
1342 	case A_SPDIF_44100:
1343 		ucontrol->value.enumerated.item[0] = 0;
1344 		break;
1345 	case A_SPDIF_48000:
1346 		ucontrol->value.enumerated.item[0] = 1;
1347 		break;
1348 	case A_SPDIF_96000:
1349 		ucontrol->value.enumerated.item[0] = 2;
1350 		break;
1351 	default:
1352 		ucontrol->value.enumerated.item[0] = 1;
1353 	}
1354 	return 0;
1355 }
1356 
1357 static int snd_audigy_spdif_output_rate_put(struct snd_kcontrol *kcontrol,
1358                                  struct snd_ctl_elem_value *ucontrol)
1359 {
1360 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1361 	int change;
1362 	unsigned int reg, val, tmp;
1363 
1364 	switch(ucontrol->value.enumerated.item[0]) {
1365 	case 0:
1366 		val = A_SPDIF_44100;
1367 		break;
1368 	case 1:
1369 		val = A_SPDIF_48000;
1370 		break;
1371 	case 2:
1372 		val = A_SPDIF_96000;
1373 		break;
1374 	default:
1375 		val = A_SPDIF_48000;
1376 		break;
1377 	}
1378 
1379 
1380 	spin_lock_irq(&emu->reg_lock);
1381 	reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
1382 	tmp = reg & ~A_SPDIF_RATE_MASK;
1383 	tmp |= val;
1384 	change = (tmp != reg);
1385 	if (change)
1386 		snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
1387 	spin_unlock_irq(&emu->reg_lock);
1388 	return change;
1389 }
1390 
1391 static const struct snd_kcontrol_new snd_audigy_spdif_output_rate =
1392 {
1393 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1394 	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1395 	.name =         "Audigy SPDIF Output Sample Rate",
1396 	.count =	1,
1397 	.info =         snd_audigy_spdif_output_rate_info,
1398 	.get =          snd_audigy_spdif_output_rate_get,
1399 	.put =          snd_audigy_spdif_output_rate_put
1400 };
1401 #endif
1402 
1403 static int snd_emu10k1_spdif_put(struct snd_kcontrol *kcontrol,
1404                                  struct snd_ctl_elem_value *ucontrol)
1405 {
1406 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1407 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1408 	int change;
1409 	unsigned int val;
1410 
1411 	/* Limit: emu->spdif_bits */
1412 	if (idx >= 3)
1413 		return -EINVAL;
1414 	val = (ucontrol->value.iec958.status[0] << 0) |
1415 	      (ucontrol->value.iec958.status[1] << 8) |
1416 	      (ucontrol->value.iec958.status[2] << 16) |
1417 	      (ucontrol->value.iec958.status[3] << 24);
1418 	change = val != emu->spdif_bits[idx];
1419 	if (change) {
1420 		snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
1421 		emu->spdif_bits[idx] = val;
1422 	}
1423 	return change;
1424 }
1425 
1426 static const struct snd_kcontrol_new snd_emu10k1_spdif_mask_control =
1427 {
1428 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1429 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1430 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1431 	.count =	3,
1432 	.info =         snd_emu10k1_spdif_info,
1433 	.get =          snd_emu10k1_spdif_get_mask
1434 };
1435 
1436 static const struct snd_kcontrol_new snd_emu10k1_spdif_control =
1437 {
1438 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1439 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1440 	.count =	3,
1441 	.info =         snd_emu10k1_spdif_info,
1442 	.get =          snd_emu10k1_spdif_get,
1443 	.put =          snd_emu10k1_spdif_put
1444 };
1445 
1446 
1447 static void update_emu10k1_fxrt(struct snd_emu10k1 *emu, int voice, unsigned char *route)
1448 {
1449 	if (emu->audigy) {
1450 		snd_emu10k1_ptr_write_multiple(emu, voice,
1451 			A_FXRT1, snd_emu10k1_compose_audigy_fxrt1(route),
1452 			A_FXRT2, snd_emu10k1_compose_audigy_fxrt2(route),
1453 			REGLIST_END);
1454 	} else {
1455 		snd_emu10k1_ptr_write(emu, FXRT, voice,
1456 				      snd_emu10k1_compose_send_routing(route));
1457 	}
1458 }
1459 
1460 static void update_emu10k1_send_volume(struct snd_emu10k1 *emu, int voice, unsigned char *volume)
1461 {
1462 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
1463 	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
1464 	snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
1465 	snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
1466 	if (emu->audigy) {
1467 		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
1468 				      snd_emu10k1_compose_audigy_sendamounts(volume));
1469 	}
1470 }
1471 
1472 /* PCM stream controls */
1473 
1474 static int snd_emu10k1_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1475 {
1476 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1477 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1478 	uinfo->count = emu->audigy ? 3*8 : 3*4;
1479 	uinfo->value.integer.min = 0;
1480 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
1481 	return 0;
1482 }
1483 
1484 static int snd_emu10k1_send_routing_get(struct snd_kcontrol *kcontrol,
1485                                         struct snd_ctl_elem_value *ucontrol)
1486 {
1487 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1488 	struct snd_emu10k1_pcm_mixer *mix =
1489 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1490 	int voice, idx;
1491 	int num_efx = emu->audigy ? 8 : 4;
1492 	int mask = emu->audigy ? 0x3f : 0x0f;
1493 
1494 	for (voice = 0; voice < 3; voice++)
1495 		for (idx = 0; idx < num_efx; idx++)
1496 			ucontrol->value.integer.value[(voice * num_efx) + idx] =
1497 				mix->send_routing[voice][idx] & mask;
1498 	return 0;
1499 }
1500 
1501 static int snd_emu10k1_send_routing_put(struct snd_kcontrol *kcontrol,
1502                                         struct snd_ctl_elem_value *ucontrol)
1503 {
1504 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1505 	struct snd_emu10k1_pcm_mixer *mix =
1506 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1507 	int change = 0, voice, idx, val;
1508 	int num_efx = emu->audigy ? 8 : 4;
1509 	int mask = emu->audigy ? 0x3f : 0x0f;
1510 
1511 	spin_lock_irq(&emu->reg_lock);
1512 	for (voice = 0; voice < 3; voice++)
1513 		for (idx = 0; idx < num_efx; idx++) {
1514 			val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
1515 			if (mix->send_routing[voice][idx] != val) {
1516 				mix->send_routing[voice][idx] = val;
1517 				change = 1;
1518 			}
1519 		}
1520 	if (change && mix->epcm && mix->epcm->voices[0]) {
1521 		if (!mix->epcm->voices[0]->last) {
1522 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
1523 					    &mix->send_routing[1][0]);
1524 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number + 1,
1525 					    &mix->send_routing[2][0]);
1526 		} else {
1527 			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
1528 					    &mix->send_routing[0][0]);
1529 		}
1530 	}
1531 	spin_unlock_irq(&emu->reg_lock);
1532 	return change;
1533 }
1534 
1535 static const struct snd_kcontrol_new snd_emu10k1_send_routing_control =
1536 {
1537 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1538 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1539 	.name =         "EMU10K1 PCM Send Routing",
1540 	.count =	32,
1541 	.info =         snd_emu10k1_send_routing_info,
1542 	.get =          snd_emu10k1_send_routing_get,
1543 	.put =          snd_emu10k1_send_routing_put
1544 };
1545 
1546 static int snd_emu10k1_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1547 {
1548 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1549 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1550 	uinfo->count = emu->audigy ? 3*8 : 3*4;
1551 	uinfo->value.integer.min = 0;
1552 	uinfo->value.integer.max = 255;
1553 	return 0;
1554 }
1555 
1556 static int snd_emu10k1_send_volume_get(struct snd_kcontrol *kcontrol,
1557                                        struct snd_ctl_elem_value *ucontrol)
1558 {
1559 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1560 	struct snd_emu10k1_pcm_mixer *mix =
1561 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1562 	int idx;
1563 	int num_efx = emu->audigy ? 8 : 4;
1564 
1565 	for (idx = 0; idx < 3*num_efx; idx++)
1566 		ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
1567 	return 0;
1568 }
1569 
1570 static int snd_emu10k1_send_volume_put(struct snd_kcontrol *kcontrol,
1571                                        struct snd_ctl_elem_value *ucontrol)
1572 {
1573 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1574 	struct snd_emu10k1_pcm_mixer *mix =
1575 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1576 	int change = 0, idx, val;
1577 	int num_efx = emu->audigy ? 8 : 4;
1578 
1579 	spin_lock_irq(&emu->reg_lock);
1580 	for (idx = 0; idx < 3*num_efx; idx++) {
1581 		val = ucontrol->value.integer.value[idx] & 255;
1582 		if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
1583 			mix->send_volume[idx/num_efx][idx%num_efx] = val;
1584 			change = 1;
1585 		}
1586 	}
1587 	if (change && mix->epcm && mix->epcm->voices[0]) {
1588 		if (!mix->epcm->voices[0]->last) {
1589 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1590 						   &mix->send_volume[1][0]);
1591 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number + 1,
1592 						   &mix->send_volume[2][0]);
1593 		} else {
1594 			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
1595 						   &mix->send_volume[0][0]);
1596 		}
1597 	}
1598 	spin_unlock_irq(&emu->reg_lock);
1599 	return change;
1600 }
1601 
1602 static const struct snd_kcontrol_new snd_emu10k1_send_volume_control =
1603 {
1604 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1605 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1606 	.name =         "EMU10K1 PCM Send Volume",
1607 	.count =	32,
1608 	.info =         snd_emu10k1_send_volume_info,
1609 	.get =          snd_emu10k1_send_volume_get,
1610 	.put =          snd_emu10k1_send_volume_put
1611 };
1612 
1613 static int snd_emu10k1_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1614 {
1615 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1616 	uinfo->count = 3;
1617 	uinfo->value.integer.min = 0;
1618 	uinfo->value.integer.max = 0x1fffd;
1619 	return 0;
1620 }
1621 
1622 static int snd_emu10k1_attn_get(struct snd_kcontrol *kcontrol,
1623                                 struct snd_ctl_elem_value *ucontrol)
1624 {
1625 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1626 	struct snd_emu10k1_pcm_mixer *mix =
1627 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1628 	int idx;
1629 
1630 	for (idx = 0; idx < 3; idx++)
1631 		ucontrol->value.integer.value[idx] = mix->attn[idx] * 0xffffU / 0x8000U;
1632 	return 0;
1633 }
1634 
1635 static int snd_emu10k1_attn_put(struct snd_kcontrol *kcontrol,
1636 				struct snd_ctl_elem_value *ucontrol)
1637 {
1638 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1639 	struct snd_emu10k1_pcm_mixer *mix =
1640 		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1641 	int change = 0, idx, val;
1642 
1643 	spin_lock_irq(&emu->reg_lock);
1644 	for (idx = 0; idx < 3; idx++) {
1645 		unsigned uval = ucontrol->value.integer.value[idx] & 0x1ffff;
1646 		val = uval * 0x8000U / 0xffffU;
1647 		if (mix->attn[idx] != val) {
1648 			mix->attn[idx] = val;
1649 			change = 1;
1650 		}
1651 	}
1652 	if (change && mix->epcm && mix->epcm->voices[0]) {
1653 		if (!mix->epcm->voices[0]->last) {
1654 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
1655 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number + 1, mix->attn[2]);
1656 		} else {
1657 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
1658 		}
1659 	}
1660 	spin_unlock_irq(&emu->reg_lock);
1661 	return change;
1662 }
1663 
1664 static const struct snd_kcontrol_new snd_emu10k1_attn_control =
1665 {
1666 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1667 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1668 	.name =         "EMU10K1 PCM Volume",
1669 	.count =	32,
1670 	.info =         snd_emu10k1_attn_info,
1671 	.get =          snd_emu10k1_attn_get,
1672 	.put =          snd_emu10k1_attn_put
1673 };
1674 
1675 /* Mutichannel PCM stream controls */
1676 
1677 static int snd_emu10k1_efx_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1678 {
1679 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1680 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1681 	uinfo->count = emu->audigy ? 8 : 4;
1682 	uinfo->value.integer.min = 0;
1683 	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
1684 	return 0;
1685 }
1686 
1687 static int snd_emu10k1_efx_send_routing_get(struct snd_kcontrol *kcontrol,
1688                                         struct snd_ctl_elem_value *ucontrol)
1689 {
1690 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1691 	struct snd_emu10k1_pcm_mixer *mix =
1692 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1693 	int idx;
1694 	int num_efx = emu->audigy ? 8 : 4;
1695 	int mask = emu->audigy ? 0x3f : 0x0f;
1696 
1697 	for (idx = 0; idx < num_efx; idx++)
1698 		ucontrol->value.integer.value[idx] =
1699 			mix->send_routing[0][idx] & mask;
1700 	return 0;
1701 }
1702 
1703 static int snd_emu10k1_efx_send_routing_put(struct snd_kcontrol *kcontrol,
1704                                         struct snd_ctl_elem_value *ucontrol)
1705 {
1706 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1707 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1708 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1709 	int change = 0, idx, val;
1710 	int num_efx = emu->audigy ? 8 : 4;
1711 	int mask = emu->audigy ? 0x3f : 0x0f;
1712 
1713 	spin_lock_irq(&emu->reg_lock);
1714 	for (idx = 0; idx < num_efx; idx++) {
1715 		val = ucontrol->value.integer.value[idx] & mask;
1716 		if (mix->send_routing[0][idx] != val) {
1717 			mix->send_routing[0][idx] = val;
1718 			change = 1;
1719 		}
1720 	}
1721 
1722 	if (change && mix->epcm) {
1723 		if (mix->epcm->voices[ch]) {
1724 			update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
1725 					&mix->send_routing[0][0]);
1726 		}
1727 	}
1728 	spin_unlock_irq(&emu->reg_lock);
1729 	return change;
1730 }
1731 
1732 static const struct snd_kcontrol_new snd_emu10k1_efx_send_routing_control =
1733 {
1734 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1735 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1736 	.name =         "Multichannel PCM Send Routing",
1737 	.count =	16,
1738 	.info =         snd_emu10k1_efx_send_routing_info,
1739 	.get =          snd_emu10k1_efx_send_routing_get,
1740 	.put =          snd_emu10k1_efx_send_routing_put
1741 };
1742 
1743 static int snd_emu10k1_efx_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1744 {
1745 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1746 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1747 	uinfo->count = emu->audigy ? 8 : 4;
1748 	uinfo->value.integer.min = 0;
1749 	uinfo->value.integer.max = 255;
1750 	return 0;
1751 }
1752 
1753 static int snd_emu10k1_efx_send_volume_get(struct snd_kcontrol *kcontrol,
1754                                        struct snd_ctl_elem_value *ucontrol)
1755 {
1756 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1757 	struct snd_emu10k1_pcm_mixer *mix =
1758 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1759 	int idx;
1760 	int num_efx = emu->audigy ? 8 : 4;
1761 
1762 	for (idx = 0; idx < num_efx; idx++)
1763 		ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
1764 	return 0;
1765 }
1766 
1767 static int snd_emu10k1_efx_send_volume_put(struct snd_kcontrol *kcontrol,
1768                                        struct snd_ctl_elem_value *ucontrol)
1769 {
1770 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1771 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1772 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1773 	int change = 0, idx, val;
1774 	int num_efx = emu->audigy ? 8 : 4;
1775 
1776 	spin_lock_irq(&emu->reg_lock);
1777 	for (idx = 0; idx < num_efx; idx++) {
1778 		val = ucontrol->value.integer.value[idx] & 255;
1779 		if (mix->send_volume[0][idx] != val) {
1780 			mix->send_volume[0][idx] = val;
1781 			change = 1;
1782 		}
1783 	}
1784 	if (change && mix->epcm) {
1785 		if (mix->epcm->voices[ch]) {
1786 			update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
1787 						   &mix->send_volume[0][0]);
1788 		}
1789 	}
1790 	spin_unlock_irq(&emu->reg_lock);
1791 	return change;
1792 }
1793 
1794 
1795 static const struct snd_kcontrol_new snd_emu10k1_efx_send_volume_control =
1796 {
1797 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1798 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1799 	.name =         "Multichannel PCM Send Volume",
1800 	.count =	16,
1801 	.info =         snd_emu10k1_efx_send_volume_info,
1802 	.get =          snd_emu10k1_efx_send_volume_get,
1803 	.put =          snd_emu10k1_efx_send_volume_put
1804 };
1805 
1806 static int snd_emu10k1_efx_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1807 {
1808 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1809 	uinfo->count = 1;
1810 	uinfo->value.integer.min = 0;
1811 	uinfo->value.integer.max = 0x1fffd;
1812 	return 0;
1813 }
1814 
1815 static int snd_emu10k1_efx_attn_get(struct snd_kcontrol *kcontrol,
1816                                 struct snd_ctl_elem_value *ucontrol)
1817 {
1818 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1819 	struct snd_emu10k1_pcm_mixer *mix =
1820 		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
1821 
1822 	ucontrol->value.integer.value[0] = mix->attn[0] * 0xffffU / 0x8000U;
1823 	return 0;
1824 }
1825 
1826 static int snd_emu10k1_efx_attn_put(struct snd_kcontrol *kcontrol,
1827 				struct snd_ctl_elem_value *ucontrol)
1828 {
1829 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1830 	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1831 	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
1832 	int change = 0, val;
1833 	unsigned uval;
1834 
1835 	spin_lock_irq(&emu->reg_lock);
1836 	uval = ucontrol->value.integer.value[0] & 0x1ffff;
1837 	val = uval * 0x8000U / 0xffffU;
1838 	if (mix->attn[0] != val) {
1839 		mix->attn[0] = val;
1840 		change = 1;
1841 	}
1842 	if (change && mix->epcm) {
1843 		if (mix->epcm->voices[ch]) {
1844 			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
1845 		}
1846 	}
1847 	spin_unlock_irq(&emu->reg_lock);
1848 	return change;
1849 }
1850 
1851 static const struct snd_kcontrol_new snd_emu10k1_efx_attn_control =
1852 {
1853 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1854 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1855 	.name =         "Multichannel PCM Volume",
1856 	.count =	16,
1857 	.info =         snd_emu10k1_efx_attn_info,
1858 	.get =          snd_emu10k1_efx_attn_get,
1859 	.put =          snd_emu10k1_efx_attn_put
1860 };
1861 
1862 #define snd_emu10k1_shared_spdif_info	snd_ctl_boolean_mono_info
1863 
1864 static int snd_emu10k1_shared_spdif_get(struct snd_kcontrol *kcontrol,
1865 					struct snd_ctl_elem_value *ucontrol)
1866 {
1867 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1868 
1869 	if (emu->audigy)
1870 		ucontrol->value.integer.value[0] = inw(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
1871 	else
1872 		ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
1873 	if (emu->card_capabilities->invert_shared_spdif)
1874 		ucontrol->value.integer.value[0] =
1875 			!ucontrol->value.integer.value[0];
1876 
1877 	return 0;
1878 }
1879 
1880 static int snd_emu10k1_shared_spdif_put(struct snd_kcontrol *kcontrol,
1881 					struct snd_ctl_elem_value *ucontrol)
1882 {
1883 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1884 	unsigned int reg, val, sw;
1885 	int change = 0;
1886 
1887 	sw = ucontrol->value.integer.value[0];
1888 	if (emu->card_capabilities->invert_shared_spdif)
1889 		sw = !sw;
1890 	spin_lock_irq(&emu->emu_lock);
1891 	if ( emu->card_capabilities->i2c_adc) {
1892 		/* Do nothing for Audigy 2 ZS Notebook */
1893 	} else if (emu->audigy) {
1894 		reg = inw(emu->port + A_IOCFG);
1895 		val = sw ? A_IOCFG_GPOUT0 : 0;
1896 		change = (reg & A_IOCFG_GPOUT0) != val;
1897 		if (change) {
1898 			reg &= ~A_IOCFG_GPOUT0;
1899 			reg |= val;
1900 			outw(reg | val, emu->port + A_IOCFG);
1901 		}
1902 	}
1903 	reg = inl(emu->port + HCFG);
1904 	val = sw ? HCFG_GPOUT0 : 0;
1905 	change |= (reg & HCFG_GPOUT0) != val;
1906 	if (change) {
1907 		reg &= ~HCFG_GPOUT0;
1908 		reg |= val;
1909 		outl(reg | val, emu->port + HCFG);
1910 	}
1911 	spin_unlock_irq(&emu->emu_lock);
1912 	return change;
1913 }
1914 
1915 static const struct snd_kcontrol_new snd_emu10k1_shared_spdif =
1916 {
1917 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1918 	.name =		"SB Live Analog/Digital Output Jack",
1919 	.info =		snd_emu10k1_shared_spdif_info,
1920 	.get =		snd_emu10k1_shared_spdif_get,
1921 	.put =		snd_emu10k1_shared_spdif_put
1922 };
1923 
1924 static const struct snd_kcontrol_new snd_audigy_shared_spdif =
1925 {
1926 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1927 	.name =		"Audigy Analog/Digital Output Jack",
1928 	.info =		snd_emu10k1_shared_spdif_info,
1929 	.get =		snd_emu10k1_shared_spdif_get,
1930 	.put =		snd_emu10k1_shared_spdif_put
1931 };
1932 
1933 /* workaround for too low volume on Audigy due to 16bit/24bit conversion */
1934 
1935 #define snd_audigy_capture_boost_info	snd_ctl_boolean_mono_info
1936 
1937 static int snd_audigy_capture_boost_get(struct snd_kcontrol *kcontrol,
1938 					struct snd_ctl_elem_value *ucontrol)
1939 {
1940 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1941 	unsigned int val;
1942 
1943 	/* FIXME: better to use a cached version */
1944 	val = snd_ac97_read(emu->ac97, AC97_REC_GAIN);
1945 	ucontrol->value.integer.value[0] = !!val;
1946 	return 0;
1947 }
1948 
1949 static int snd_audigy_capture_boost_put(struct snd_kcontrol *kcontrol,
1950 					struct snd_ctl_elem_value *ucontrol)
1951 {
1952 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1953 	unsigned int val;
1954 
1955 	if (ucontrol->value.integer.value[0])
1956 		val = 0x0f0f;
1957 	else
1958 		val = 0;
1959 	return snd_ac97_update(emu->ac97, AC97_REC_GAIN, val);
1960 }
1961 
1962 static const struct snd_kcontrol_new snd_audigy_capture_boost =
1963 {
1964 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1965 	.name =		"Mic Extra Boost",
1966 	.info =		snd_audigy_capture_boost_info,
1967 	.get =		snd_audigy_capture_boost_get,
1968 	.put =		snd_audigy_capture_boost_put
1969 };
1970 
1971 
1972 /*
1973  */
1974 static void snd_emu10k1_mixer_free_ac97(struct snd_ac97 *ac97)
1975 {
1976 	struct snd_emu10k1 *emu = ac97->private_data;
1977 	emu->ac97 = NULL;
1978 }
1979 
1980 /*
1981  */
1982 static int remove_ctl(struct snd_card *card, const char *name)
1983 {
1984 	struct snd_ctl_elem_id id;
1985 	memset(&id, 0, sizeof(id));
1986 	strcpy(id.name, name);
1987 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1988 	return snd_ctl_remove_id(card, &id);
1989 }
1990 
1991 static int rename_ctl(struct snd_card *card, const char *src, const char *dst)
1992 {
1993 	struct snd_kcontrol *kctl = snd_ctl_find_id_mixer(card, src);
1994 	if (kctl) {
1995 		snd_ctl_rename(card, kctl, dst);
1996 		return 0;
1997 	}
1998 	return -ENOENT;
1999 }
2000 
2001 int snd_emu10k1_mixer(struct snd_emu10k1 *emu,
2002 		      int pcm_device, int multi_device)
2003 {
2004 	int err;
2005 	struct snd_kcontrol *kctl;
2006 	struct snd_card *card = emu->card;
2007 	const char * const *c;
2008 	static const char * const emu10k1_remove_ctls[] = {
2009 		/* no AC97 mono, surround, center/lfe */
2010 		"Master Mono Playback Switch",
2011 		"Master Mono Playback Volume",
2012 		"PCM Out Path & Mute",
2013 		"Mono Output Select",
2014 		"Surround Playback Switch",
2015 		"Surround Playback Volume",
2016 		"Center Playback Switch",
2017 		"Center Playback Volume",
2018 		"LFE Playback Switch",
2019 		"LFE Playback Volume",
2020 		NULL
2021 	};
2022 	static const char * const emu10k1_rename_ctls[] = {
2023 		"Surround Digital Playback Volume", "Surround Playback Volume",
2024 		"Center Digital Playback Volume", "Center Playback Volume",
2025 		"LFE Digital Playback Volume", "LFE Playback Volume",
2026 		NULL
2027 	};
2028 	static const char * const audigy_remove_ctls[] = {
2029 		/* Master/PCM controls on ac97 of Audigy has no effect */
2030 		/* On the Audigy2 the AC97 playback is piped into
2031 		 * the Philips ADC for 24bit capture */
2032 		"PCM Playback Switch",
2033 		"PCM Playback Volume",
2034 		"Master Playback Switch",
2035 		"Master Playback Volume",
2036 		"PCM Out Path & Mute",
2037 		"Mono Output Select",
2038 		/* remove unused AC97 capture controls */
2039 		"Capture Source",
2040 		"Capture Switch",
2041 		"Capture Volume",
2042 		"Mic Select",
2043 		"Headphone Playback Switch",
2044 		"Headphone Playback Volume",
2045 		"3D Control - Center",
2046 		"3D Control - Depth",
2047 		"3D Control - Switch",
2048 		"Video Playback Switch",
2049 		"Video Playback Volume",
2050 		"Mic Playback Switch",
2051 		"Mic Playback Volume",
2052 		"External Amplifier",
2053 		NULL
2054 	};
2055 	static const char * const audigy_rename_ctls[] = {
2056 		/* use conventional names */
2057 		"Wave Playback Volume", "PCM Playback Volume",
2058 		/* "Wave Capture Volume", "PCM Capture Volume", */
2059 		"Wave Master Playback Volume", "Master Playback Volume",
2060 		"AMic Playback Volume", "Mic Playback Volume",
2061 		"Master Mono Playback Switch", "Phone Output Playback Switch",
2062 		"Master Mono Playback Volume", "Phone Output Playback Volume",
2063 		NULL
2064 	};
2065 	static const char * const audigy_rename_ctls_i2c_adc[] = {
2066 		//"Analog Mix Capture Volume","OLD Analog Mix Capture Volume",
2067 		"Line Capture Volume", "Analog Mix Capture Volume",
2068 		"Wave Playback Volume", "OLD PCM Playback Volume",
2069 		"Wave Master Playback Volume", "Master Playback Volume",
2070 		"AMic Playback Volume", "Old Mic Playback Volume",
2071 		"CD Capture Volume", "IEC958 Optical Capture Volume",
2072 		NULL
2073 	};
2074 	static const char * const audigy_remove_ctls_i2c_adc[] = {
2075 		/* On the Audigy2 ZS Notebook
2076 		 * Capture via WM8775  */
2077 		"Mic Capture Volume",
2078 		"Analog Mix Capture Volume",
2079 		"Aux Capture Volume",
2080 		"IEC958 Optical Capture Volume",
2081 		NULL
2082 	};
2083 	static const char * const audigy_remove_ctls_1361t_adc[] = {
2084 		/* On the Audigy2 the AC97 playback is piped into
2085 		 * the Philips ADC for 24bit capture */
2086 		"PCM Playback Switch",
2087 		"PCM Playback Volume",
2088 		"Capture Source",
2089 		"Capture Switch",
2090 		"Capture Volume",
2091 		"Mic Capture Volume",
2092 		"Headphone Playback Switch",
2093 		"Headphone Playback Volume",
2094 		"3D Control - Center",
2095 		"3D Control - Depth",
2096 		"3D Control - Switch",
2097 		"Line2 Playback Volume",
2098 		"Line2 Capture Volume",
2099 		NULL
2100 	};
2101 	static const char * const audigy_rename_ctls_1361t_adc[] = {
2102 		"Master Playback Switch", "Master Capture Switch",
2103 		"Master Playback Volume", "Master Capture Volume",
2104 		"Wave Master Playback Volume", "Master Playback Volume",
2105 		"Beep Playback Switch", "Beep Capture Switch",
2106 		"Beep Playback Volume", "Beep Capture Volume",
2107 		"Phone Playback Switch", "Phone Capture Switch",
2108 		"Phone Playback Volume", "Phone Capture Volume",
2109 		"Mic Playback Switch", "Mic Capture Switch",
2110 		"Mic Playback Volume", "Mic Capture Volume",
2111 		"Line Playback Switch", "Line Capture Switch",
2112 		"Line Playback Volume", "Line Capture Volume",
2113 		"CD Playback Switch", "CD Capture Switch",
2114 		"CD Playback Volume", "CD Capture Volume",
2115 		"Aux Playback Switch", "Aux Capture Switch",
2116 		"Aux Playback Volume", "Aux Capture Volume",
2117 		"Video Playback Switch", "Video Capture Switch",
2118 		"Video Playback Volume", "Video Capture Volume",
2119 		"Master Mono Playback Switch", "Phone Output Playback Switch",
2120 		"Master Mono Playback Volume", "Phone Output Playback Volume",
2121 		NULL
2122 	};
2123 
2124 	if (emu->card_capabilities->ac97_chip) {
2125 		struct snd_ac97_bus *pbus;
2126 		struct snd_ac97_template ac97;
2127 		static const struct snd_ac97_bus_ops ops = {
2128 			.write = snd_emu10k1_ac97_write,
2129 			.read = snd_emu10k1_ac97_read,
2130 		};
2131 
2132 		err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus);
2133 		if (err < 0)
2134 			return err;
2135 		pbus->no_vra = 1; /* we don't need VRA */
2136 
2137 		memset(&ac97, 0, sizeof(ac97));
2138 		ac97.private_data = emu;
2139 		ac97.private_free = snd_emu10k1_mixer_free_ac97;
2140 		ac97.scaps = AC97_SCAP_NO_SPDIF;
2141 		err = snd_ac97_mixer(pbus, &ac97, &emu->ac97);
2142 		if (err < 0) {
2143 			if (emu->card_capabilities->ac97_chip == 1)
2144 				return err;
2145 			dev_info(emu->card->dev,
2146 				 "AC97 is optional on this board\n");
2147 			dev_info(emu->card->dev,
2148 				 "Proceeding without ac97 mixers...\n");
2149 			snd_device_free(emu->card, pbus);
2150 			goto no_ac97; /* FIXME: get rid of ugly gotos.. */
2151 		}
2152 		if (emu->audigy) {
2153 			/* set master volume to 0 dB */
2154 			snd_ac97_write_cache(emu->ac97, AC97_MASTER, 0x0000);
2155 			/* set capture source to mic */
2156 			snd_ac97_write_cache(emu->ac97, AC97_REC_SEL, 0x0000);
2157 			/* set mono output (TAD) to mic */
2158 			snd_ac97_update_bits(emu->ac97, AC97_GENERAL_PURPOSE,
2159 				0x0200, 0x0200);
2160 			if (emu->card_capabilities->adc_1361t)
2161 				c = audigy_remove_ctls_1361t_adc;
2162 			else
2163 				c = audigy_remove_ctls;
2164 		} else {
2165 			/*
2166 			 * Credits for cards based on STAC9758:
2167 			 *   James Courtier-Dutton <James@superbug.demon.co.uk>
2168 			 *   Voluspa <voluspa@comhem.se>
2169 			 */
2170 			if (emu->ac97->id == AC97_ID_STAC9758) {
2171 				emu->rear_ac97 = 1;
2172 				snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
2173 				snd_ac97_write_cache(emu->ac97, AC97_HEADPHONE, 0x0202);
2174 				remove_ctl(card,"Front Playback Volume");
2175 				remove_ctl(card,"Front Playback Switch");
2176 			}
2177 			/* remove unused AC97 controls */
2178 			snd_ac97_write_cache(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
2179 			snd_ac97_write_cache(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
2180 			c = emu10k1_remove_ctls;
2181 		}
2182 		for (; *c; c++)
2183 			remove_ctl(card, *c);
2184 	} else if (emu->card_capabilities->i2c_adc) {
2185 		c = audigy_remove_ctls_i2c_adc;
2186 		for (; *c; c++)
2187 			remove_ctl(card, *c);
2188 	} else {
2189 	no_ac97:
2190 		if (emu->card_capabilities->ecard)
2191 			strcpy(emu->card->mixername, "EMU APS");
2192 		else if (emu->audigy)
2193 			strcpy(emu->card->mixername, "SB Audigy");
2194 		else
2195 			strcpy(emu->card->mixername, "Emu10k1");
2196 	}
2197 
2198 	if (emu->audigy)
2199 		if (emu->card_capabilities->adc_1361t)
2200 			c = audigy_rename_ctls_1361t_adc;
2201 		else if (emu->card_capabilities->i2c_adc)
2202 			c = audigy_rename_ctls_i2c_adc;
2203 		else
2204 			c = audigy_rename_ctls;
2205 	else
2206 		c = emu10k1_rename_ctls;
2207 	for (; *c; c += 2)
2208 		rename_ctl(card, c[0], c[1]);
2209 
2210 	if (emu->card_capabilities->subsystem == 0x80401102) { /* SB Live! Platinum CT4760P */
2211 		remove_ctl(card, "Center Playback Volume");
2212 		remove_ctl(card, "LFE Playback Volume");
2213 		remove_ctl(card, "Wave Center Playback Volume");
2214 		remove_ctl(card, "Wave LFE Playback Volume");
2215 	}
2216 	if (emu->card_capabilities->subsystem == 0x20071102) {  /* Audigy 4 Pro */
2217 		rename_ctl(card, "Line2 Capture Volume", "Line1/Mic Capture Volume");
2218 		rename_ctl(card, "Analog Mix Capture Volume", "Line2 Capture Volume");
2219 		rename_ctl(card, "Aux2 Capture Volume", "Line3 Capture Volume");
2220 		rename_ctl(card, "Mic Capture Volume", "Unknown1 Capture Volume");
2221 	}
2222 	kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu);
2223 	if (!kctl)
2224 		return -ENOMEM;
2225 	kctl->id.device = pcm_device;
2226 	err = snd_ctl_add(card, kctl);
2227 	if (err)
2228 		return err;
2229 	kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu);
2230 	if (!kctl)
2231 		return -ENOMEM;
2232 	kctl->id.device = pcm_device;
2233 	err = snd_ctl_add(card, kctl);
2234 	if (err)
2235 		return err;
2236 	kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu);
2237 	if (!kctl)
2238 		return -ENOMEM;
2239 	kctl->id.device = pcm_device;
2240 	err = snd_ctl_add(card, kctl);
2241 	if (err)
2242 		return err;
2243 
2244 	kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu);
2245 	if (!kctl)
2246 		return -ENOMEM;
2247 	kctl->id.device = multi_device;
2248 	err = snd_ctl_add(card, kctl);
2249 	if (err)
2250 		return err;
2251 
2252 	kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu);
2253 	if (!kctl)
2254 		return -ENOMEM;
2255 	kctl->id.device = multi_device;
2256 	err = snd_ctl_add(card, kctl);
2257 	if (err)
2258 		return err;
2259 
2260 	kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu);
2261 	if (!kctl)
2262 		return -ENOMEM;
2263 	kctl->id.device = multi_device;
2264 	err = snd_ctl_add(card, kctl);
2265 	if (err)
2266 		return err;
2267 
2268 	if (!emu->card_capabilities->ecard && !emu->card_capabilities->emu_model) {
2269 		/* sb live! and audigy */
2270 		kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu);
2271 		if (!kctl)
2272 			return -ENOMEM;
2273 		if (!emu->audigy)
2274 			kctl->id.device = emu->pcm_efx->device;
2275 		err = snd_ctl_add(card, kctl);
2276 		if (err)
2277 			return err;
2278 		kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu);
2279 		if (!kctl)
2280 			return -ENOMEM;
2281 		if (!emu->audigy)
2282 			kctl->id.device = emu->pcm_efx->device;
2283 		err = snd_ctl_add(card, kctl);
2284 		if (err)
2285 			return err;
2286 	}
2287 
2288 	if (emu->card_capabilities->emu_model) {
2289 		;  /* Disable the snd_audigy_spdif_shared_spdif */
2290 	} else if (emu->audigy) {
2291 		kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu);
2292 		if (!kctl)
2293 			return -ENOMEM;
2294 		err = snd_ctl_add(card, kctl);
2295 		if (err)
2296 			return err;
2297 #if 0
2298 		kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu);
2299 		if (!kctl)
2300 			return -ENOMEM;
2301 		err = snd_ctl_add(card, kctl);
2302 		if (err)
2303 			return err;
2304 #endif
2305 	} else if (! emu->card_capabilities->ecard) {
2306 		/* sb live! */
2307 		kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu);
2308 		if (!kctl)
2309 			return -ENOMEM;
2310 		err = snd_ctl_add(card, kctl);
2311 		if (err)
2312 			return err;
2313 	}
2314 	if (emu->card_capabilities->ca0151_chip) { /* P16V */
2315 		err = snd_p16v_mixer(emu);
2316 		if (err)
2317 			return err;
2318 	}
2319 
2320 	if (emu->card_capabilities->emu_model) {
2321 		unsigned i, emu_idx = emu1010_idx(emu);
2322 		const struct snd_emu1010_routing_info *emu_ri =
2323 			&emu1010_routing_info[emu_idx];
2324 		const struct snd_emu1010_pads_info *emu_pi = &emu1010_pads_info[emu_idx];
2325 
2326 		for (i = 0; i < emu_ri->n_ins; i++)
2327 			emu->emu1010.input_source[i] =
2328 				emu1010_map_source(emu_ri, emu_ri->in_dflts[i]);
2329 		for (i = 0; i < emu_ri->n_outs; i++)
2330 			emu->emu1010.output_source[i] =
2331 				emu1010_map_source(emu_ri, emu_ri->out_dflts[i]);
2332 		snd_emu1010_fpga_lock(emu);
2333 		snd_emu1010_apply_sources(emu);
2334 		snd_emu1010_fpga_unlock(emu);
2335 
2336 		kctl = emu->ctl_clock_source = snd_ctl_new1(&snd_emu1010_clock_source, emu);
2337 		err = snd_ctl_add(card, kctl);
2338 		if (err < 0)
2339 			return err;
2340 		err = snd_ctl_add(card,
2341 			snd_ctl_new1(&snd_emu1010_clock_fallback, emu));
2342 		if (err < 0)
2343 			return err;
2344 
2345 		err = add_ctls(emu, &emu1010_adc_pads_ctl,
2346 			       emu_pi->adc_ctls, emu_pi->n_adc_ctls);
2347 		if (err < 0)
2348 			return err;
2349 		err = add_ctls(emu, &emu1010_dac_pads_ctl,
2350 			       emu_pi->dac_ctls, emu_pi->n_dac_ctls);
2351 		if (err < 0)
2352 			return err;
2353 
2354 		if (!emu->card_capabilities->no_adat) {
2355 			err = snd_ctl_add(card,
2356 				snd_ctl_new1(&snd_emu1010_optical_out, emu));
2357 			if (err < 0)
2358 				return err;
2359 			err = snd_ctl_add(card,
2360 				snd_ctl_new1(&snd_emu1010_optical_in, emu));
2361 			if (err < 0)
2362 				return err;
2363 		}
2364 
2365 		err = add_emu1010_source_mixers(emu);
2366 		if (err < 0)
2367 			return err;
2368 	}
2369 
2370 	if ( emu->card_capabilities->i2c_adc) {
2371 		err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_i2c_capture_source, emu));
2372 		if (err < 0)
2373 			return err;
2374 
2375 		err = add_ctls(emu, &i2c_volume_ctl,
2376 			       snd_audigy_i2c_volume_ctls,
2377 			       ARRAY_SIZE(snd_audigy_i2c_volume_ctls));
2378 		if (err < 0)
2379 			return err;
2380 	}
2381 
2382 	if (emu->card_capabilities->ac97_chip && emu->audigy) {
2383 		err = snd_ctl_add(card, snd_ctl_new1(&snd_audigy_capture_boost,
2384 						     emu));
2385 		if (err < 0)
2386 			return err;
2387 	}
2388 
2389 	return 0;
2390 }
2391