patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / pci / emu10k1 / emumixer.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
3  *                   Takashi Iwai <tiwai@suse.de>
4  *                   Creative Labs, Inc.
5  *  Routines for control of EMU10K1 chips / mixer routines
6  *
7  *  BUGS:
8  *    --
9  *
10  *  TODO:
11  *    --
12  *
13  *   This program is free software; you can redistribute it and/or modify
14  *   it under the terms of the GNU General Public License as published by
15  *   the Free Software Foundation; either version 2 of the License, or
16  *   (at your option) any later version.
17  *
18  *   This program is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with this program; if not, write to the Free Software
25  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  *
27  */
28
29 #include <sound/driver.h>
30 #include <linux/time.h>
31 #include <linux/init.h>
32 #include <sound/core.h>
33 #include <sound/emu10k1.h>
34
35 #define chip_t emu10k1_t
36
37 #define AC97_ID_STAC9758        0x83847658
38
39 static int snd_emu10k1_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
40 {
41         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
42         uinfo->count = 1;
43         return 0;
44 }
45
46 static int snd_emu10k1_spdif_get(snd_kcontrol_t * kcontrol,
47                                  snd_ctl_elem_value_t * ucontrol)
48 {
49         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
50         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
51         unsigned long flags;
52
53         spin_lock_irqsave(&emu->reg_lock, flags);
54         ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
55         ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
56         ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
57         ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
58         spin_unlock_irqrestore(&emu->reg_lock, flags);
59         return 0;
60 }
61
62 static int snd_emu10k1_spdif_get_mask(snd_kcontrol_t * kcontrol,
63                                       snd_ctl_elem_value_t * ucontrol)
64 {
65         ucontrol->value.iec958.status[0] = 0xff;
66         ucontrol->value.iec958.status[1] = 0xff;
67         ucontrol->value.iec958.status[2] = 0xff;
68         ucontrol->value.iec958.status[3] = 0xff;
69         return 0;
70 }
71
72 static int snd_emu10k1_spdif_put(snd_kcontrol_t * kcontrol,
73                                  snd_ctl_elem_value_t * ucontrol)
74 {
75         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
76         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
77         int change;
78         unsigned int val;
79         unsigned long flags;
80
81         val = (ucontrol->value.iec958.status[0] << 0) |
82               (ucontrol->value.iec958.status[1] << 8) |
83               (ucontrol->value.iec958.status[2] << 16) |
84               (ucontrol->value.iec958.status[3] << 24);
85         spin_lock_irqsave(&emu->reg_lock, flags);
86         change = val != emu->spdif_bits[idx];
87         if (change) {
88                 snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
89                 emu->spdif_bits[idx] = val;
90         }
91         spin_unlock_irqrestore(&emu->reg_lock, flags);
92         return change;
93 }
94
95 static snd_kcontrol_new_t snd_emu10k1_spdif_mask_control =
96 {
97         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
98         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
99         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
100         .count =        4,
101         .info =         snd_emu10k1_spdif_info,
102         .get =          snd_emu10k1_spdif_get_mask
103 };
104
105 static snd_kcontrol_new_t snd_emu10k1_spdif_control =
106 {
107         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
108         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
109         .count =        4,
110         .info =         snd_emu10k1_spdif_info,
111         .get =          snd_emu10k1_spdif_get,
112         .put =          snd_emu10k1_spdif_put
113 };
114
115
116 static void update_emu10k1_fxrt(emu10k1_t *emu, int voice, unsigned char *route)
117 {
118         if (emu->audigy) {
119                 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
120                                       snd_emu10k1_compose_audigy_fxrt1(route));
121                 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
122                                       snd_emu10k1_compose_audigy_fxrt2(route));
123         } else {
124                 snd_emu10k1_ptr_write(emu, FXRT, voice,
125                                       snd_emu10k1_compose_send_routing(route));
126         }
127 }
128
129 static void update_emu10k1_send_volume(emu10k1_t *emu, int voice, unsigned char *volume)
130 {
131         snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
132         snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
133         snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
134         snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
135         if (emu->audigy) {
136                 unsigned int val = ((unsigned int)volume[4] << 24) |
137                         ((unsigned int)volume[5] << 16) |
138                         ((unsigned int)volume[6] << 8) |
139                         (unsigned int)volume[7];
140                 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
141         }
142 }
143
144 static int snd_emu10k1_send_routing_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
145 {
146         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
147         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
148         uinfo->count = emu->audigy ? 3*8 : 3*4;
149         uinfo->value.integer.min = 0;
150         uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
151         return 0;
152 }
153
154 static int snd_emu10k1_send_routing_get(snd_kcontrol_t * kcontrol,
155                                         snd_ctl_elem_value_t * ucontrol)
156 {
157         unsigned long flags;
158         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
159         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
160         int voice, idx;
161         int num_efx = emu->audigy ? 8 : 4;
162         int mask = emu->audigy ? 0x3f : 0x0f;
163
164         spin_lock_irqsave(&emu->reg_lock, flags);
165         for (voice = 0; voice < 3; voice++)
166                 for (idx = 0; idx < num_efx; idx++)
167                         ucontrol->value.integer.value[(voice * num_efx) + idx] = 
168                                 mix->send_routing[voice][idx] & mask;
169         spin_unlock_irqrestore(&emu->reg_lock, flags);
170         return 0;
171 }
172
173 static int snd_emu10k1_send_routing_put(snd_kcontrol_t * kcontrol,
174                                         snd_ctl_elem_value_t * ucontrol)
175 {
176         unsigned long flags;
177         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
178         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
179         int change = 0, voice, idx, val;
180         int num_efx = emu->audigy ? 8 : 4;
181         int mask = emu->audigy ? 0x3f : 0x0f;
182
183         spin_lock_irqsave(&emu->reg_lock, flags);
184         for (voice = 0; voice < 3; voice++)
185                 for (idx = 0; idx < num_efx; idx++) {
186                         val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
187                         if (mix->send_routing[voice][idx] != val) {
188                                 mix->send_routing[voice][idx] = val;
189                                 change = 1;
190                         }
191                 }       
192         if (change && mix->epcm) {
193                 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
194                         update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
195                                             &mix->send_routing[1][0]);
196                         update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
197                                             &mix->send_routing[2][0]);
198                 } else if (mix->epcm->voices[0]) {
199                         update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
200                                             &mix->send_routing[0][0]);
201                 }
202         }
203         spin_unlock_irqrestore(&emu->reg_lock, flags);
204         return change;
205 }
206
207 static snd_kcontrol_new_t snd_emu10k1_send_routing_control =
208 {
209         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
210         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
211         .name =         "EMU10K1 PCM Send Routing",
212         .count =        32,
213         .info =         snd_emu10k1_send_routing_info,
214         .get =          snd_emu10k1_send_routing_get,
215         .put =          snd_emu10k1_send_routing_put
216 };
217
218 static int snd_emu10k1_send_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
219 {
220         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
221         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
222         uinfo->count = emu->audigy ? 3*8 : 3*4;
223         uinfo->value.integer.min = 0;
224         uinfo->value.integer.max = 255;
225         return 0;
226 }
227
228 static int snd_emu10k1_send_volume_get(snd_kcontrol_t * kcontrol,
229                                        snd_ctl_elem_value_t * ucontrol)
230 {
231         unsigned long flags;
232         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
233         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
234         int idx;
235         int num_efx = emu->audigy ? 8 : 4;
236
237         spin_lock_irqsave(&emu->reg_lock, flags);
238         for (idx = 0; idx < 3*num_efx; idx++)
239                 ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
240         spin_unlock_irqrestore(&emu->reg_lock, flags);
241         return 0;
242 }
243
244 static int snd_emu10k1_send_volume_put(snd_kcontrol_t * kcontrol,
245                                        snd_ctl_elem_value_t * ucontrol)
246 {
247         unsigned long flags;
248         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
249         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
250         int change = 0, idx, val;
251         int num_efx = emu->audigy ? 8 : 4;
252
253         spin_lock_irqsave(&emu->reg_lock, flags);
254         for (idx = 0; idx < 3*num_efx; idx++) {
255                 val = ucontrol->value.integer.value[idx] & 255;
256                 if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
257                         mix->send_volume[idx/num_efx][idx%num_efx] = val;
258                         change = 1;
259                 }
260         }
261         if (change && mix->epcm) {
262                 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
263                         update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
264                                                    &mix->send_volume[1][0]);
265                         update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
266                                                    &mix->send_volume[2][0]);
267                 } else if (mix->epcm->voices[0]) {
268                         update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
269                                                    &mix->send_volume[0][0]);
270                 }
271         }
272         spin_unlock_irqrestore(&emu->reg_lock, flags);
273         return change;
274 }
275
276 static snd_kcontrol_new_t snd_emu10k1_send_volume_control =
277 {
278         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
279         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
280         .name =         "EMU10K1 PCM Send Volume",
281         .count =        32,
282         .info =         snd_emu10k1_send_volume_info,
283         .get =          snd_emu10k1_send_volume_get,
284         .put =          snd_emu10k1_send_volume_put
285 };
286
287 static int snd_emu10k1_attn_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
288 {
289         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
290         uinfo->count = 3;
291         uinfo->value.integer.min = 0;
292         uinfo->value.integer.max = 0xffff;
293         return 0;
294 }
295
296 static int snd_emu10k1_attn_get(snd_kcontrol_t * kcontrol,
297                                 snd_ctl_elem_value_t * ucontrol)
298 {
299         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
300         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
301         unsigned long flags;
302         int idx;
303
304         spin_lock_irqsave(&emu->reg_lock, flags);
305         for (idx = 0; idx < 3; idx++)
306                 ucontrol->value.integer.value[idx] = mix->attn[idx];
307         spin_unlock_irqrestore(&emu->reg_lock, flags);
308         return 0;
309 }
310
311 static int snd_emu10k1_attn_put(snd_kcontrol_t * kcontrol,
312                                 snd_ctl_elem_value_t * ucontrol)
313 {
314         unsigned long flags;
315         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
316         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
317         int change = 0, idx, val;
318
319         spin_lock_irqsave(&emu->reg_lock, flags);
320         for (idx = 0; idx < 3; idx++) {
321                 val = ucontrol->value.integer.value[idx] & 0xffff;
322                 if (mix->attn[idx] != val) {
323                         mix->attn[idx] = val;
324                         change = 1;
325                 }
326         }
327         if (change && mix->epcm) {
328                 if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
329                         snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
330                         snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
331                 } else if (mix->epcm->voices[0]) {
332                         snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
333                 }
334         }
335         spin_unlock_irqrestore(&emu->reg_lock, flags);
336         return change;
337 }
338
339 static snd_kcontrol_new_t snd_emu10k1_attn_control =
340 {
341         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
342         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
343         .name =         "EMU10K1 PCM Volume",
344         .count =        32,
345         .info =         snd_emu10k1_attn_info,
346         .get =          snd_emu10k1_attn_get,
347         .put =          snd_emu10k1_attn_put
348 };
349
350 static int snd_emu10k1_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
351 {
352         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
353         uinfo->count = 1;
354         uinfo->value.integer.min = 0;
355         uinfo->value.integer.max = 1;
356         return 0;
357 }
358
359 static int snd_emu10k1_shared_spdif_get(snd_kcontrol_t * kcontrol,
360                                         snd_ctl_elem_value_t * ucontrol)
361 {
362         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
363
364         if (emu->audigy)
365                 ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
366         else
367                 ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
368         return 0;
369 }
370
371 static int snd_emu10k1_shared_spdif_put(snd_kcontrol_t * kcontrol,
372                                         snd_ctl_elem_value_t * ucontrol)
373 {
374         unsigned long flags;
375         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
376         unsigned int reg, val;
377         int change = 0;
378
379         spin_lock_irqsave(&emu->reg_lock, flags);
380         if (emu->audigy) {
381                 reg = inl(emu->port + A_IOCFG);
382                 val = ucontrol->value.integer.value[0] ? A_IOCFG_GPOUT0 : 0;
383                 change = (reg & A_IOCFG_GPOUT0) != val;
384                 if (change) {
385                         reg &= ~A_IOCFG_GPOUT0;
386                         reg |= val;
387                         outl(reg | val, emu->port + A_IOCFG);
388                 }
389         }
390         reg = inl(emu->port + HCFG);
391         val = ucontrol->value.integer.value[0] ? HCFG_GPOUT0 : 0;
392         change |= (reg & HCFG_GPOUT0) != val;
393         if (change) {
394                 reg &= ~HCFG_GPOUT0;
395                 reg |= val;
396                 outl(reg | val, emu->port + HCFG);
397         }
398         spin_unlock_irqrestore(&emu->reg_lock, flags);
399         return change;
400 }
401
402 static snd_kcontrol_new_t snd_emu10k1_shared_spdif __devinitdata =
403 {
404         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
405         .name =         "SB Live Analog/Digital Output Jack",
406         .info =         snd_emu10k1_shared_spdif_info,
407         .get =          snd_emu10k1_shared_spdif_get,
408         .put =          snd_emu10k1_shared_spdif_put
409 };
410
411 static snd_kcontrol_new_t snd_audigy_shared_spdif __devinitdata =
412 {
413         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
414         .name =         "Audigy Analog/Digital Output Jack",
415         .info =         snd_emu10k1_shared_spdif_info,
416         .get =          snd_emu10k1_shared_spdif_get,
417         .put =          snd_emu10k1_shared_spdif_put
418 };
419
420 /*
421  */
422 static void snd_emu10k1_mixer_free_ac97(ac97_t *ac97)
423 {
424         emu10k1_t *emu = snd_magic_cast(emu10k1_t, ac97->private_data, return);
425         emu->ac97 = NULL;
426 }
427
428 /*
429  */
430 static int remove_ctl(snd_card_t *card, const char *name)
431 {
432         snd_ctl_elem_id_t id;
433         memset(&id, 0, sizeof(id));
434         strcpy(id.name, name);
435         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
436         return snd_ctl_remove_id(card, &id);
437 }
438
439 static snd_kcontrol_t *ctl_find(snd_card_t *card, const char *name)
440 {
441         snd_ctl_elem_id_t sid;
442         memset(&sid, 0, sizeof(sid));
443         strcpy(sid.name, name);
444         sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
445         return snd_ctl_find_id(card, &sid);
446 }
447
448 static int rename_ctl(snd_card_t *card, const char *src, const char *dst)
449 {
450         snd_kcontrol_t *kctl = ctl_find(card, src);
451         if (kctl) {
452                 strcpy(kctl->id.name, dst);
453                 return 0;
454         }
455         return -ENOENT;
456 }
457
458 int __devinit snd_emu10k1_mixer(emu10k1_t *emu)
459 {
460         int err, pcm;
461         snd_kcontrol_t *kctl;
462         snd_card_t *card = emu->card;
463         char **c;
464         static char *emu10k1_remove_ctls[] = {
465                 /* no AC97 mono, surround, center/lfe */
466                 "Master Mono Playback Switch",
467                 "Master Mono Playback Volume",
468                 "PCM Out Path & Mute",
469                 "Mono Output Select",
470                 "Surround Playback Switch",
471                 "Surround Playback Volume",
472                 "Center Playback Switch",
473                 "Center Playback Volume",
474                 "LFE Playback Switch",
475                 "LFE Playback Volume",
476                 NULL
477         };
478         static char *emu10k1_rename_ctls[] = {
479                 "Surround Digital Playback Volume", "Surround Playback Volume",
480                 "Center Digital Playback Volume", "Center Playback Volume",
481                 "LFE Digital Playback Volume", "LFE Playback Volume",
482                 NULL
483         };
484         static char *audigy_remove_ctls[] = {
485                 /* Master/PCM controls on ac97 of Audigy has no effect */
486                 "PCM Playback Switch",
487                 "PCM Playback Volume",
488                 "Master Mono Playback Switch",
489                 "Master Mono Playback Volume",
490                 "Master Playback Switch",
491                 "Master Playback Volume",
492                 "PCM Out Path & Mute",
493                 "Mono Output Select",
494                 /* remove unused AC97 capture controls */
495                 "Capture Source",
496                 "Capture Switch",
497                 "Capture Volume",
498                 "Mic Select",
499                 "Video Playback Switch",
500                 "Video Playback Volume",
501                 "Mic Playback Switch",
502                 "Mic Playback Volume",
503                 NULL
504         };
505         static char *audigy_rename_ctls[] = {
506                 /* use conventional names */
507                 "Wave Playback Volume", "PCM Playback Volume",
508                 /* "Wave Capture Volume", "PCM Capture Volume", */
509                 "Wave Master Playback Volume", "Master Playback Volume",
510                 "AMic Playback Volume", "Mic Playback Volume",
511                 NULL
512         };
513
514         if (!emu->no_ac97) {
515                 ac97_bus_t bus, *pbus;
516                 ac97_t ac97;
517
518                 memset(&bus, 0, sizeof(bus));
519                 bus.write = snd_emu10k1_ac97_write;
520                 bus.read = snd_emu10k1_ac97_read;
521                 if ((err = snd_ac97_bus(emu->card, &bus, &pbus)) < 0)
522                         return err;
523                 
524                 memset(&ac97, 0, sizeof(ac97));
525                 ac97.private_data = emu;
526                 ac97.private_free = snd_emu10k1_mixer_free_ac97;
527                 if ((err = snd_ac97_mixer(pbus, &ac97, &emu->ac97)) < 0)
528                         return err;
529                 if (emu->audigy) {
530                         /* set master volume to 0 dB */
531                         snd_ac97_write(emu->ac97, AC97_MASTER, 0x0202);
532                         /* set capture source to mic */
533                         snd_ac97_write(emu->ac97, AC97_REC_SEL, 0x0000);
534                         c = audigy_remove_ctls;
535                 } else {
536                         /*
537                          * Credits for cards based on STAC9758:
538                          *   James Courtier-Dutton <James@superbug.demon.co.uk>
539                          *   Voluspa <voluspa@comhem.se>
540                          */
541                         if (emu->ac97->id == AC97_ID_STAC9758) {
542                                 emu->rear_ac97 = 1;
543                                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
544                         }
545                         /* remove unused AC97 controls */
546                         snd_ac97_write(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
547                         snd_ac97_write(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
548                         c = emu10k1_remove_ctls;
549                 }
550                 for (; *c; c++)
551                         remove_ctl(card, *c);
552         } else {
553                 if (emu->APS)
554                         strcpy(emu->card->mixername, "EMU APS");
555                 else if (emu->audigy)
556                         strcpy(emu->card->mixername, "SB Audigy");
557                 else
558                         strcpy(emu->card->mixername, "Emu10k1");
559         }
560
561         if (emu->audigy)
562                 c = audigy_rename_ctls;
563         else
564                 c = emu10k1_rename_ctls;
565         for (; *c; c += 2)
566                 rename_ctl(card, c[0], c[1]);
567
568         if ((kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu)) == NULL)
569                 return -ENOMEM;
570         if ((err = snd_ctl_add(card, kctl)))
571                 return err;
572         if ((kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu)) == NULL)
573                 return -ENOMEM;
574         if ((err = snd_ctl_add(card, kctl)))
575                 return err;
576         if ((kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu)) == NULL)
577                 return -ENOMEM;
578         if ((err = snd_ctl_add(card, kctl)))
579                 return err;
580
581         /* intiailize the routing and volume table for each pcm playback stream */
582         for (pcm = 0; pcm < 32; pcm++) {
583                 emu10k1_pcm_mixer_t *mix;
584                 int v;
585                 
586                 mix = &emu->pcm_mixer[pcm];
587                 mix->epcm = NULL;
588
589                 for (v = 0; v < 4; v++)
590                         mix->send_routing[0][v] = 
591                                 mix->send_routing[1][v] = 
592                                 mix->send_routing[2][v] = v;
593                 
594                 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
595                 mix->send_volume[0][0] = mix->send_volume[0][1] =
596                 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
597                 
598                 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
599         }
600         
601         if (! emu->APS) { /* FIXME: APS has these controls? */
602                 /* sb live! and audigy */
603                 if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu)) == NULL)
604                         return -ENOMEM;
605                 if ((err = snd_ctl_add(card, kctl)))
606                         return err;
607                 if ((kctl = ctl_find(card, SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT))) != NULL) {
608                         /* already defined by ac97, remove it */
609                         /* FIXME: or do we need both controls? */
610                         remove_ctl(card, SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT));
611                 }
612                 if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu)) == NULL)
613                         return -ENOMEM;
614                 if ((err = snd_ctl_add(card, kctl)))
615                         return err;
616         }
617
618         if (emu->audigy) {
619                 if ((kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu)) == NULL)
620                         return -ENOMEM;
621                 if ((err = snd_ctl_add(card, kctl)))
622                         return err;
623         } else if (! emu->APS) {
624                 /* sb live! */
625                 if ((kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu)) == NULL)
626                         return -ENOMEM;
627                 if ((err = snd_ctl_add(card, kctl)))
628                         return err;
629         }
630
631         return 0;
632 }