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