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