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