[ALSA] add codec-specific controls for UCB1400
[pandora-kernel.git] / sound / pci / ac97 / ac97_codec.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com).
7  *
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24
25 #include <sound/driver.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mutex.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/asoundef.h>
36 #include <sound/initval.h>
37 #include "ac97_local.h"
38 #include "ac97_id.h"
39 #include "ac97_patch.h"
40
41 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
42 MODULE_DESCRIPTION("Universal interface for Audio Codec '97");
43 MODULE_LICENSE("GPL");
44
45 static int enable_loopback;
46
47 module_param(enable_loopback, bool, 0444);
48 MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control");
49
50 #ifdef CONFIG_SND_AC97_POWER_SAVE
51 static int power_save;
52 module_param(power_save, bool, 0644);
53 MODULE_PARM_DESC(power_save, "Enable AC97 power-saving control");
54 #endif
55 /*
56
57  */
58
59 struct ac97_codec_id {
60         unsigned int id;
61         unsigned int mask;
62         const char *name;
63         int (*patch)(struct snd_ac97 *ac97);
64         int (*mpatch)(struct snd_ac97 *ac97);
65         unsigned int flags;
66 };
67
68 static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = {
69 { 0x414b4d00, 0xffffff00, "Asahi Kasei",        NULL,   NULL },
70 { 0x41445300, 0xffffff00, "Analog Devices",     NULL,   NULL },
71 { 0x414c4300, 0xffffff00, "Realtek",            NULL,   NULL },
72 { 0x414c4700, 0xffffff00, "Realtek",            NULL,   NULL },
73 { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL,  NULL },
74 { 0x43525900, 0xffffff00, "Cirrus Logic",       NULL,   NULL },
75 { 0x43585400, 0xffffff00, "Conexant",           NULL,   NULL },
76 { 0x44543000, 0xffffff00, "Diamond Technology", NULL,   NULL },
77 { 0x454d4300, 0xffffff00, "eMicro",             NULL,   NULL },
78 { 0x45838300, 0xffffff00, "ESS Technology",     NULL,   NULL },
79 { 0x48525300, 0xffffff00, "Intersil",           NULL,   NULL },
80 { 0x49434500, 0xffffff00, "ICEnsemble",         NULL,   NULL },
81 { 0x49544500, 0xffffff00, "ITE Tech.Inc",       NULL,   NULL },
82 { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
83 { 0x50534300, 0xffffff00, "Philips",            NULL,   NULL },
84 { 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL,   NULL },
85 { 0x54524100, 0xffffff00, "TriTech",            NULL,   NULL },
86 { 0x54584e00, 0xffffff00, "Texas Instruments",  NULL,   NULL },
87 { 0x56494100, 0xffffff00, "VIA Technologies",   NULL,   NULL },
88 { 0x57454300, 0xffffff00, "Winbond",            NULL,   NULL },
89 { 0x574d4c00, 0xffffff00, "Wolfson",            NULL,   NULL },
90 { 0x594d4800, 0xffffff00, "Yamaha",             NULL,   NULL },
91 { 0x83847600, 0xffffff00, "SigmaTel",           NULL,   NULL },
92 { 0,          0,          NULL,                 NULL,   NULL }
93 };
94
95 static const struct ac97_codec_id snd_ac97_codec_ids[] = {
96 { 0x414b4d00, 0xffffffff, "AK4540",             NULL,           NULL },
97 { 0x414b4d01, 0xffffffff, "AK4542",             NULL,           NULL },
98 { 0x414b4d02, 0xffffffff, "AK4543",             NULL,           NULL },
99 { 0x414b4d06, 0xffffffff, "AK4544A",            NULL,           NULL },
100 { 0x414b4d07, 0xffffffff, "AK4545",             NULL,           NULL },
101 { 0x41445303, 0xffffffff, "AD1819",             patch_ad1819,   NULL },
102 { 0x41445340, 0xffffffff, "AD1881",             patch_ad1881,   NULL },
103 { 0x41445348, 0xffffffff, "AD1881A",            patch_ad1881,   NULL },
104 { 0x41445360, 0xffffffff, "AD1885",             patch_ad1885,   NULL },
105 { 0x41445361, 0xffffffff, "AD1886",             patch_ad1886,   NULL },
106 { 0x41445362, 0xffffffff, "AD1887",             patch_ad1881,   NULL },
107 { 0x41445363, 0xffffffff, "AD1886A",            patch_ad1881,   NULL },
108 { 0x41445368, 0xffffffff, "AD1888",             patch_ad1888,   NULL },
109 { 0x41445370, 0xffffffff, "AD1980",             patch_ad1980,   NULL },
110 { 0x41445372, 0xffffffff, "AD1981A",            patch_ad1981a,  NULL },
111 { 0x41445374, 0xffffffff, "AD1981B",            patch_ad1981b,  NULL },
112 { 0x41445375, 0xffffffff, "AD1985",             patch_ad1985,   NULL },
113 { 0x41445378, 0xffffffff, "AD1986",             patch_ad1985,   NULL },
114 { 0x414c4300, 0xffffff00, "ALC100,100P",        NULL,           NULL },
115 { 0x414c4710, 0xfffffff0, "ALC200,200P",        NULL,           NULL },
116 { 0x414c4721, 0xffffffff, "ALC650D",            NULL,   NULL }, /* already patched */
117 { 0x414c4722, 0xffffffff, "ALC650E",            NULL,   NULL }, /* already patched */
118 { 0x414c4723, 0xffffffff, "ALC650F",            NULL,   NULL }, /* already patched */
119 { 0x414c4720, 0xfffffff0, "ALC650",             patch_alc650,   NULL },
120 { 0x414c4760, 0xfffffff0, "ALC655",             patch_alc655,   NULL },
121 { 0x414c4781, 0xffffffff, "ALC658D",            NULL,   NULL }, /* already patched */
122 { 0x414c4780, 0xfffffff0, "ALC658",             patch_alc655,   NULL },
123 { 0x414c4790, 0xfffffff0, "ALC850",             patch_alc850,   NULL },
124 { 0x414c4730, 0xffffffff, "ALC101",             NULL,           NULL },
125 { 0x414c4740, 0xfffffff0, "ALC202",             NULL,           NULL },
126 { 0x414c4750, 0xfffffff0, "ALC250",             NULL,           NULL },
127 { 0x414c4770, 0xfffffff0, "ALC203",             NULL,           NULL },
128 { 0x434d4941, 0xffffffff, "CMI9738",            patch_cm9738,   NULL },
129 { 0x434d4961, 0xffffffff, "CMI9739",            patch_cm9739,   NULL },
130 { 0x434d4969, 0xffffffff, "CMI9780",            patch_cm9780,   NULL },
131 { 0x434d4978, 0xffffffff, "CMI9761",            patch_cm9761,   NULL },
132 { 0x434d4982, 0xffffffff, "CMI9761",            patch_cm9761,   NULL },
133 { 0x434d4983, 0xffffffff, "CMI9761",            patch_cm9761,   NULL },
134 { 0x43525900, 0xfffffff8, "CS4297",             NULL,           NULL },
135 { 0x43525910, 0xfffffff8, "CS4297A",            patch_cirrus_spdif,     NULL },
136 { 0x43525920, 0xfffffff8, "CS4298",             patch_cirrus_spdif,             NULL },
137 { 0x43525928, 0xfffffff8, "CS4294",             NULL,           NULL },
138 { 0x43525930, 0xfffffff8, "CS4299",             patch_cirrus_cs4299,    NULL },
139 { 0x43525948, 0xfffffff8, "CS4201",             NULL,           NULL },
140 { 0x43525958, 0xfffffff8, "CS4205",             patch_cirrus_spdif,     NULL },
141 { 0x43525960, 0xfffffff8, "CS4291",             NULL,           NULL },
142 { 0x43525970, 0xfffffff8, "CS4202",             NULL,           NULL },
143 { 0x43585421, 0xffffffff, "HSD11246",           NULL,           NULL }, // SmartMC II
144 { 0x43585428, 0xfffffff8, "Cx20468",            patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
145 { 0x44543031, 0xfffffff0, "DT0398",             NULL,           NULL },
146 { 0x454d4328, 0xffffffff, "EM28028",            NULL,           NULL },  // same as TR28028?
147 { 0x45838308, 0xffffffff, "ESS1988",            NULL,           NULL },
148 { 0x48525300, 0xffffff00, "HMP9701",            NULL,           NULL },
149 { 0x49434501, 0xffffffff, "ICE1230",            NULL,           NULL },
150 { 0x49434511, 0xffffffff, "ICE1232",            NULL,           NULL }, // alias VIA VT1611A?
151 { 0x49434514, 0xffffffff, "ICE1232A",           NULL,           NULL },
152 { 0x49434551, 0xffffffff, "VT1616",             patch_vt1616,   NULL }, 
153 { 0x49434552, 0xffffffff, "VT1616i",            patch_vt1616,   NULL }, // VT1616 compatible (chipset integrated)
154 { 0x49544520, 0xffffffff, "IT2226E",            NULL,           NULL },
155 { 0x49544561, 0xffffffff, "IT2646E",            patch_it2646,   NULL },
156 { 0x4e534300, 0xffffffff, "LM4540,43,45,46,48", NULL,           NULL }, // only guess --jk
157 { 0x4e534331, 0xffffffff, "LM4549",             NULL,           NULL },
158 { 0x4e534350, 0xffffffff, "LM4550",             patch_lm4550,   NULL }, // volume wrap fix 
159 { 0x50534304, 0xffffffff, "UCB1400",            patch_ucb1400,  NULL },
160 { 0x53494c20, 0xffffffe0, "Si3036,8",           mpatch_si3036,  mpatch_si3036, AC97_MODEM_PATCH },
161 { 0x54524102, 0xffffffff, "TR28022",            NULL,           NULL },
162 { 0x54524106, 0xffffffff, "TR28026",            NULL,           NULL },
163 { 0x54524108, 0xffffffff, "TR28028",            patch_tritech_tr28028,  NULL }, // added by xin jin [07/09/99]
164 { 0x54524123, 0xffffffff, "TR28602",            NULL,           NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
165 { 0x54584e20, 0xffffffff, "TLC320AD9xC",        NULL,           NULL },
166 { 0x56494161, 0xffffffff, "VIA1612A",           NULL,           NULL }, // modified ICE1232 with S/PDIF
167 { 0x56494170, 0xffffffff, "VIA1617A",           patch_vt1617a,  NULL }, // modified VT1616 with S/PDIF
168 { 0x56494182, 0xffffffff, "VIA1618",            NULL,           NULL },
169 { 0x57454301, 0xffffffff, "W83971D",            NULL,           NULL },
170 { 0x574d4c00, 0xffffffff, "WM9701A",            NULL,           NULL },
171 { 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
172 { 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q",    patch_wolfson04, NULL},
173 { 0x574d4C05, 0xffffffff, "WM9705,WM9710",      patch_wolfson05, NULL},
174 { 0x574d4C09, 0xffffffff, "WM9709",             NULL,           NULL},
175 { 0x574d4C12, 0xffffffff, "WM9711,WM9712",      patch_wolfson11, NULL},
176 { 0x574d4c13, 0xffffffff, "WM9713,WM9714",      patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
177 { 0x594d4800, 0xffffffff, "YMF743",             NULL,           NULL },
178 { 0x594d4802, 0xffffffff, "YMF752",             NULL,           NULL },
179 { 0x594d4803, 0xffffffff, "YMF753",             patch_yamaha_ymf753,    NULL },
180 { 0x83847600, 0xffffffff, "STAC9700,83,84",     patch_sigmatel_stac9700,        NULL },
181 { 0x83847604, 0xffffffff, "STAC9701,3,4,5",     NULL,           NULL },
182 { 0x83847605, 0xffffffff, "STAC9704",           NULL,           NULL },
183 { 0x83847608, 0xffffffff, "STAC9708,11",        patch_sigmatel_stac9708,        NULL },
184 { 0x83847609, 0xffffffff, "STAC9721,23",        patch_sigmatel_stac9721,        NULL },
185 { 0x83847644, 0xffffffff, "STAC9744",           patch_sigmatel_stac9744,        NULL },
186 { 0x83847650, 0xffffffff, "STAC9750,51",        NULL,           NULL }, // patch?
187 { 0x83847652, 0xffffffff, "STAC9752,53",        NULL,           NULL }, // patch?
188 { 0x83847656, 0xffffffff, "STAC9756,57",        patch_sigmatel_stac9756,        NULL },
189 { 0x83847658, 0xffffffff, "STAC9758,59",        patch_sigmatel_stac9758,        NULL },
190 { 0x83847666, 0xffffffff, "STAC9766,67",        NULL,           NULL }, // patch?
191 { 0,          0,          NULL,                 NULL,           NULL }
192 };
193
194
195 static void update_power_regs(struct snd_ac97 *ac97);
196
197 /*
198  *  I/O routines
199  */
200
201 static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg)
202 {
203         /* filter some registers for buggy codecs */
204         switch (ac97->id) {
205         case AC97_ID_AK4540:
206         case AC97_ID_AK4542:
207                 if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
208                         return 1;
209                 return 0;
210         case AC97_ID_AD1819:    /* AD1819 */
211         case AC97_ID_AD1881:    /* AD1881 */
212         case AC97_ID_AD1881A:   /* AD1881A */
213                 if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
214                         return 0;
215                 return 1;
216         case AC97_ID_AD1885:    /* AD1885 */
217         case AC97_ID_AD1886:    /* AD1886 */
218         case AC97_ID_AD1886A:   /* AD1886A - !!verify!! --jk */
219         case AC97_ID_AD1887:    /* AD1887 - !!verify!! --jk */
220                 if (reg == 0x5a)
221                         return 1;
222                 if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
223                         return 0;
224                 return 1;
225         case AC97_ID_STAC9700:
226         case AC97_ID_STAC9704:
227         case AC97_ID_STAC9705:
228         case AC97_ID_STAC9708:
229         case AC97_ID_STAC9721:
230         case AC97_ID_STAC9744:
231         case AC97_ID_STAC9756:
232                 if (reg <= 0x3a || reg >= 0x5a)
233                         return 1;
234                 return 0;
235         }
236         return 1;
237 }
238
239 /**
240  * snd_ac97_write - write a value on the given register
241  * @ac97: the ac97 instance
242  * @reg: the register to change
243  * @value: the value to set
244  *
245  * Writes a value on the given register.  This will invoke the write
246  * callback directly after the register check.
247  * This function doesn't change the register cache unlike
248  * #snd_ca97_write_cache(), so use this only when you don't want to
249  * reflect the change to the suspend/resume state.
250  */
251 void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
252 {
253         if (!snd_ac97_valid_reg(ac97, reg))
254                 return;
255         if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
256                 /* Fix H/W bug of ALC100/100P */
257                 if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
258                         ac97->bus->ops->write(ac97, AC97_RESET, 0);     /* reset audio codec */
259         }
260         ac97->bus->ops->write(ac97, reg, value);
261 }
262
263 EXPORT_SYMBOL(snd_ac97_write);
264
265 /**
266  * snd_ac97_read - read a value from the given register
267  * 
268  * @ac97: the ac97 instance
269  * @reg: the register to read
270  *
271  * Reads a value from the given register.  This will invoke the read
272  * callback directly after the register check.
273  *
274  * Returns the read value.
275  */
276 unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
277 {
278         if (!snd_ac97_valid_reg(ac97, reg))
279                 return 0;
280         return ac97->bus->ops->read(ac97, reg);
281 }
282
283 /* read a register - return the cached value if already read */
284 static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg)
285 {
286         if (! test_bit(reg, ac97->reg_accessed)) {
287                 ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
288                 // set_bit(reg, ac97->reg_accessed);
289         }
290         return ac97->regs[reg];
291 }
292
293 EXPORT_SYMBOL(snd_ac97_read);
294
295 /**
296  * snd_ac97_write_cache - write a value on the given register and update the cache
297  * @ac97: the ac97 instance
298  * @reg: the register to change
299  * @value: the value to set
300  *
301  * Writes a value on the given register and updates the register
302  * cache.  The cached values are used for the cached-read and the
303  * suspend/resume.
304  */
305 void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
306 {
307         if (!snd_ac97_valid_reg(ac97, reg))
308                 return;
309         mutex_lock(&ac97->reg_mutex);
310         ac97->regs[reg] = value;
311         ac97->bus->ops->write(ac97, reg, value);
312         set_bit(reg, ac97->reg_accessed);
313         mutex_unlock(&ac97->reg_mutex);
314 }
315
316 EXPORT_SYMBOL(snd_ac97_write_cache);
317
318 /**
319  * snd_ac97_update - update the value on the given register
320  * @ac97: the ac97 instance
321  * @reg: the register to change
322  * @value: the value to set
323  *
324  * Compares the value with the register cache and updates the value
325  * only when the value is changed.
326  *
327  * Returns 1 if the value is changed, 0 if no change, or a negative
328  * code on failure.
329  */
330 int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
331 {
332         int change;
333
334         if (!snd_ac97_valid_reg(ac97, reg))
335                 return -EINVAL;
336         mutex_lock(&ac97->reg_mutex);
337         change = ac97->regs[reg] != value;
338         if (change) {
339                 ac97->regs[reg] = value;
340                 ac97->bus->ops->write(ac97, reg, value);
341         }
342         set_bit(reg, ac97->reg_accessed);
343         mutex_unlock(&ac97->reg_mutex);
344         return change;
345 }
346
347 EXPORT_SYMBOL(snd_ac97_update);
348
349 /**
350  * snd_ac97_update_bits - update the bits on the given register
351  * @ac97: the ac97 instance
352  * @reg: the register to change
353  * @mask: the bit-mask to change
354  * @value: the value to set
355  *
356  * Updates the masked-bits on the given register only when the value
357  * is changed.
358  *
359  * Returns 1 if the bits are changed, 0 if no change, or a negative
360  * code on failure.
361  */
362 int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value)
363 {
364         int change;
365
366         if (!snd_ac97_valid_reg(ac97, reg))
367                 return -EINVAL;
368         mutex_lock(&ac97->reg_mutex);
369         change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
370         mutex_unlock(&ac97->reg_mutex);
371         return change;
372 }
373
374 EXPORT_SYMBOL(snd_ac97_update_bits);
375
376 /* no lock version - see snd_ac97_updat_bits() */
377 int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg,
378                                 unsigned short mask, unsigned short value)
379 {
380         int change;
381         unsigned short old, new;
382
383         old = snd_ac97_read_cache(ac97, reg);
384         new = (old & ~mask) | value;
385         change = old != new;
386         if (change) {
387                 ac97->regs[reg] = new;
388                 ac97->bus->ops->write(ac97, reg, new);
389         }
390         set_bit(reg, ac97->reg_accessed);
391         return change;
392 }
393
394 static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value)
395 {
396         int change;
397         unsigned short old, new, cfg;
398
399         mutex_lock(&ac97->page_mutex);
400         old = ac97->spec.ad18xx.pcmreg[codec];
401         new = (old & ~mask) | value;
402         change = old != new;
403         if (change) {
404                 mutex_lock(&ac97->reg_mutex);
405                 cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
406                 ac97->spec.ad18xx.pcmreg[codec] = new;
407                 /* select single codec */
408                 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
409                                  (cfg & ~0x7000) |
410                                  ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
411                 /* update PCM bits */
412                 ac97->bus->ops->write(ac97, AC97_PCM, new);
413                 /* select all codecs */
414                 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
415                                  cfg | 0x7000);
416                 mutex_unlock(&ac97->reg_mutex);
417         }
418         mutex_unlock(&ac97->page_mutex);
419         return change;
420 }
421
422 /*
423  * Controls
424  */
425
426 int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
427 {
428         struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
429         
430         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
431         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
432         uinfo->value.enumerated.items = e->mask;
433         
434         if (uinfo->value.enumerated.item > e->mask - 1)
435                 uinfo->value.enumerated.item = e->mask - 1;
436         strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
437         return 0;
438 }
439
440 int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
441 {
442         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
443         struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
444         unsigned short val, bitmask;
445         
446         for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
447                 ;
448         val = snd_ac97_read_cache(ac97, e->reg);
449         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
450         if (e->shift_l != e->shift_r)
451                 ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
452
453         return 0;
454 }
455
456 int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
457 {
458         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
459         struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
460         unsigned short val;
461         unsigned short mask, bitmask;
462         
463         for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
464                 ;
465         if (ucontrol->value.enumerated.item[0] > e->mask - 1)
466                 return -EINVAL;
467         val = ucontrol->value.enumerated.item[0] << e->shift_l;
468         mask = (bitmask - 1) << e->shift_l;
469         if (e->shift_l != e->shift_r) {
470                 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
471                         return -EINVAL;
472                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
473                 mask |= (bitmask - 1) << e->shift_r;
474         }
475         return snd_ac97_update_bits(ac97, e->reg, mask, val);
476 }
477
478 /* save/restore ac97 v2.3 paging */
479 static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol)
480 {
481         int page_save = -1;
482         if ((kcontrol->private_value & (1<<25)) &&
483             (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
484             (reg >= 0x60 && reg < 0x70)) {
485                 unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
486                 mutex_lock(&ac97->page_mutex); /* lock paging */
487                 page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
488                 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
489         }
490         return page_save;
491 }
492
493 static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save)
494 {
495         if (page_save >= 0) {
496                 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
497                 mutex_unlock(&ac97->page_mutex); /* unlock paging */
498         }
499 }
500
501 /* volume and switch controls */
502 int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
503 {
504         int mask = (kcontrol->private_value >> 16) & 0xff;
505         int shift = (kcontrol->private_value >> 8) & 0x0f;
506         int rshift = (kcontrol->private_value >> 12) & 0x0f;
507
508         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
509         uinfo->count = shift == rshift ? 1 : 2;
510         uinfo->value.integer.min = 0;
511         uinfo->value.integer.max = mask;
512         return 0;
513 }
514
515 int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
516 {
517         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
518         int reg = kcontrol->private_value & 0xff;
519         int shift = (kcontrol->private_value >> 8) & 0x0f;
520         int rshift = (kcontrol->private_value >> 12) & 0x0f;
521         int mask = (kcontrol->private_value >> 16) & 0xff;
522         int invert = (kcontrol->private_value >> 24) & 0x01;
523         int page_save;
524
525         page_save = snd_ac97_page_save(ac97, reg, kcontrol);
526         ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
527         if (shift != rshift)
528                 ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
529         if (invert) {
530                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
531                 if (shift != rshift)
532                         ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
533         }
534         snd_ac97_page_restore(ac97, page_save);
535         return 0;
536 }
537
538 int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
539 {
540         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
541         int reg = kcontrol->private_value & 0xff;
542         int shift = (kcontrol->private_value >> 8) & 0x0f;
543         int rshift = (kcontrol->private_value >> 12) & 0x0f;
544         int mask = (kcontrol->private_value >> 16) & 0xff;
545         int invert = (kcontrol->private_value >> 24) & 0x01;
546         int err, page_save;
547         unsigned short val, val2, val_mask;
548         
549         page_save = snd_ac97_page_save(ac97, reg, kcontrol);
550         val = (ucontrol->value.integer.value[0] & mask);
551         if (invert)
552                 val = mask - val;
553         val_mask = mask << shift;
554         val = val << shift;
555         if (shift != rshift) {
556                 val2 = (ucontrol->value.integer.value[1] & mask);
557                 if (invert)
558                         val2 = mask - val2;
559                 val_mask |= mask << rshift;
560                 val |= val2 << rshift;
561         }
562         err = snd_ac97_update_bits(ac97, reg, val_mask, val);
563         snd_ac97_page_restore(ac97, page_save);
564 #ifdef CONFIG_SND_AC97_POWER_SAVE
565         /* check analog mixer power-down */
566         if ((val_mask & 0x8000) &&
567             (kcontrol->private_value & (1<<30))) {
568                 if (val & 0x8000)
569                         ac97->power_up &= ~(1 << (reg>>1));
570                 else
571                         ac97->power_up |= 1 << (reg>>1);
572                 if (power_save)
573                         update_power_regs(ac97);
574         }
575 #endif
576         return err;
577 }
578
579 static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = {
580 AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
581 AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
582 };
583
584 static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = {
585 AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
586 AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
587 };
588
589 static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = {
590 AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
591 AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
592 };
593
594 static const struct snd_kcontrol_new snd_ac97_controls_mic_boost =
595         AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
596
597
598 static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
599 static const char* std_3d_path[] = {"pre 3D", "post 3D"};
600 static const char* std_mix[] = {"Mix", "Mic"};
601 static const char* std_mic[] = {"Mic1", "Mic2"};
602
603 static const struct ac97_enum std_enum[] = {
604 AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
605 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
606 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
607 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
608 };
609
610 static const struct snd_kcontrol_new snd_ac97_control_capture_src = 
611 AC97_ENUM("Capture Source", std_enum[0]); 
612
613 static const struct snd_kcontrol_new snd_ac97_control_capture_vol =
614 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
615
616 static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = {
617 AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
618 AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
619 };
620
621 enum {
622         AC97_GENERAL_PCM_OUT = 0,
623         AC97_GENERAL_STEREO_ENHANCEMENT,
624         AC97_GENERAL_3D,
625         AC97_GENERAL_LOUDNESS,
626         AC97_GENERAL_MONO,
627         AC97_GENERAL_MIC,
628         AC97_GENERAL_LOOPBACK
629 };
630
631 static const struct snd_kcontrol_new snd_ac97_controls_general[7] = {
632 AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
633 AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
634 AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
635 AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
636 AC97_ENUM("Mono Output Select", std_enum[2]),
637 AC97_ENUM("Mic Select", std_enum[3]),
638 AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
639 };
640
641 const struct snd_kcontrol_new snd_ac97_controls_3d[2] = {
642 AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
643 AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
644 };
645
646 static const struct snd_kcontrol_new snd_ac97_controls_center[2] = {
647 AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
648 AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
649 };
650
651 static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = {
652 AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
653 AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
654 };
655
656 static const struct snd_kcontrol_new snd_ac97_control_eapd =
657 AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
658
659 static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = {
660 AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
661 AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
662 };
663
664 /* change the existing EAPD control as inverted */
665 static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl)
666 {
667         kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
668         snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
669         ac97->scaps |= AC97_SCAP_INV_EAPD;
670 }
671
672 static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
673 {
674         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
675         uinfo->count = 1;
676         return 0;
677 }
678                         
679 static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
680 {
681         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
682                                            IEC958_AES0_NONAUDIO |
683                                            IEC958_AES0_CON_EMPHASIS_5015 |
684                                            IEC958_AES0_CON_NOT_COPYRIGHT;
685         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
686                                            IEC958_AES1_CON_ORIGINAL;
687         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
688         return 0;
689 }
690                         
691 static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
692 {
693         /* FIXME: AC'97 spec doesn't say which bits are used for what */
694         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
695                                            IEC958_AES0_NONAUDIO |
696                                            IEC958_AES0_PRO_FS |
697                                            IEC958_AES0_PRO_EMPHASIS_5015;
698         return 0;
699 }
700
701 static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
702 {
703         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
704
705         mutex_lock(&ac97->reg_mutex);
706         ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
707         ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
708         ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
709         ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
710         mutex_unlock(&ac97->reg_mutex);
711         return 0;
712 }
713                         
714 static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
715 {
716         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
717         unsigned int new = 0;
718         unsigned short val = 0;
719         int change;
720
721         new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
722         if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
723                 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
724                 switch (new & IEC958_AES0_PRO_FS) {
725                 case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
726                 case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
727                 case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
728                 default:                       val |= 1<<12; break;
729                 }
730                 if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
731                         val |= 1<<3;
732         } else {
733                 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
734                 new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
735                 new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
736                 if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
737                         val |= 1<<3;
738                 if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
739                         val |= 1<<2;
740                 val |= ((new >> 8) & 0xff) << 4;        // category + original
741                 switch ((new >> 24) & 0xff) {
742                 case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
743                 case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
744                 case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
745                 default:                       val |= 1<<12; break;
746                 }
747         }
748
749         mutex_lock(&ac97->reg_mutex);
750         change = ac97->spdif_status != new;
751         ac97->spdif_status = new;
752
753         if (ac97->flags & AC97_CS_SPDIF) {
754                 int x = (val >> 12) & 0x03;
755                 switch (x) {
756                 case 0: x = 1; break;  // 44.1
757                 case 2: x = 0; break;  // 48.0
758                 default: x = 0; break; // illegal.
759                 }
760                 change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
761         } else if (ac97->flags & AC97_CX_SPDIF) {
762                 int v;
763                 v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
764                 v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
765                 change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC, 
766                                                       AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
767                                                       v);
768         } else {
769                 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
770                 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
771
772                 change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
773                 if (extst & AC97_EA_SPDIF) {
774                         snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
775                 }
776         }
777         mutex_unlock(&ac97->reg_mutex);
778
779         return change;
780 }
781
782 static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
783 {
784         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
785         int reg = kcontrol->private_value & 0xff;
786         int shift = (kcontrol->private_value >> 8) & 0xff;
787         int mask = (kcontrol->private_value >> 16) & 0xff;
788         // int invert = (kcontrol->private_value >> 24) & 0xff;
789         unsigned short value, old, new;
790         int change;
791
792         value = (ucontrol->value.integer.value[0] & mask);
793
794         mutex_lock(&ac97->reg_mutex);
795         mask <<= shift;
796         value <<= shift;
797         old = snd_ac97_read_cache(ac97, reg);
798         new = (old & ~mask) | value;
799         change = old != new;
800
801         if (change) {
802                 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
803                 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
804                 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
805                 if (extst & AC97_EA_SPDIF)
806                         snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
807         }
808         mutex_unlock(&ac97->reg_mutex);
809         return change;
810 }
811
812 const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = {
813         {
814                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
815                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
816                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
817                 .info = snd_ac97_spdif_mask_info,
818                 .get = snd_ac97_spdif_cmask_get,
819         },
820         {
821                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
822                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
823                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
824                 .info = snd_ac97_spdif_mask_info,
825                 .get = snd_ac97_spdif_pmask_get,
826         },
827         {
828                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
830                 .info = snd_ac97_spdif_mask_info,
831                 .get = snd_ac97_spdif_default_get,
832                 .put = snd_ac97_spdif_default_put,
833         },
834
835         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
836         {
837                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
838                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
839                 .info = snd_ac97_info_volsw,
840                 .get = snd_ac97_get_volsw,
841                 .put = snd_ac97_put_spsa,
842                 .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
843         },
844 };
845
846 #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
847 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
848   .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
849   .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
850
851 static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
852 {
853         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
854         int mask = (kcontrol->private_value >> 16) & 0x0f;
855         int lshift = (kcontrol->private_value >> 8) & 0x0f;
856         int rshift = (kcontrol->private_value >> 12) & 0x0f;
857
858         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
859         if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
860                 uinfo->count = 2;
861         else
862                 uinfo->count = 1;
863         uinfo->value.integer.min = 0;
864         uinfo->value.integer.max = mask;
865         return 0;
866 }
867
868 static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
869 {
870         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
871         int codec = kcontrol->private_value & 3;
872         int lshift = (kcontrol->private_value >> 8) & 0x0f;
873         int rshift = (kcontrol->private_value >> 12) & 0x0f;
874         int mask = (kcontrol->private_value >> 16) & 0xff;
875         
876         ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
877         if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
878                 ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
879         return 0;
880 }
881
882 static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
883 {
884         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
885         int codec = kcontrol->private_value & 3;
886         int lshift = (kcontrol->private_value >> 8) & 0x0f;
887         int rshift = (kcontrol->private_value >> 12) & 0x0f;
888         int mask = (kcontrol->private_value >> 16) & 0xff;
889         unsigned short val, valmask;
890         
891         val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
892         valmask = mask << lshift;
893         if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
894                 val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
895                 valmask |= mask << rshift;
896         }
897         return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
898 }
899
900 #define AD18XX_PCM_VOLUME(xname, codec) \
901 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
902   .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
903   .private_value = codec }
904
905 static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
906 {
907         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
908         uinfo->count = 2;
909         uinfo->value.integer.min = 0;
910         uinfo->value.integer.max = 31;
911         return 0;
912 }
913
914 static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
915 {
916         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
917         int codec = kcontrol->private_value & 3;
918         
919         mutex_lock(&ac97->page_mutex);
920         ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
921         ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
922         mutex_unlock(&ac97->page_mutex);
923         return 0;
924 }
925
926 static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
927 {
928         struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
929         int codec = kcontrol->private_value & 3;
930         unsigned short val1, val2;
931         
932         val1 = 31 - (ucontrol->value.integer.value[0] & 31);
933         val2 = 31 - (ucontrol->value.integer.value[1] & 31);
934         return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
935 }
936
937 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = {
938 AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
939 AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
940 };
941
942 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = {
943 AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
944 AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
945 };
946
947 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = {
948 AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
949 AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
950 };
951
952 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = {
953 AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
954 AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
955 };
956
957 /*
958  *
959  */
960
961 static void snd_ac97_powerdown(struct snd_ac97 *ac97);
962
963 static int snd_ac97_bus_free(struct snd_ac97_bus *bus)
964 {
965         if (bus) {
966                 snd_ac97_bus_proc_done(bus);
967                 kfree(bus->pcms);
968                 if (bus->private_free)
969                         bus->private_free(bus);
970                 kfree(bus);
971         }
972         return 0;
973 }
974
975 static int snd_ac97_bus_dev_free(struct snd_device *device)
976 {
977         struct snd_ac97_bus *bus = device->device_data;
978         return snd_ac97_bus_free(bus);
979 }
980
981 static int snd_ac97_free(struct snd_ac97 *ac97)
982 {
983         if (ac97) {
984 #ifdef CONFIG_SND_AC97_POWER_SAVE
985                 if (ac97->power_workq)
986                         destroy_workqueue(ac97->power_workq);
987 #endif
988                 snd_ac97_proc_done(ac97);
989                 if (ac97->bus)
990                         ac97->bus->codec[ac97->num] = NULL;
991                 if (ac97->private_free)
992                         ac97->private_free(ac97);
993                 kfree(ac97);
994         }
995         return 0;
996 }
997
998 static int snd_ac97_dev_free(struct snd_device *device)
999 {
1000         struct snd_ac97 *ac97 = device->device_data;
1001         snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
1002         return snd_ac97_free(ac97);
1003 }
1004
1005 static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg)
1006 {
1007         unsigned short val, mask = 0x8000;
1008
1009         if (! snd_ac97_valid_reg(ac97, reg))
1010                 return 0;
1011
1012         switch (reg) {
1013         case AC97_MASTER_TONE:
1014                 return ac97->caps & 0x04 ? 1 : 0;
1015         case AC97_HEADPHONE:
1016                 return ac97->caps & 0x10 ? 1 : 0;
1017         case AC97_REC_GAIN_MIC:
1018                 return ac97->caps & 0x01 ? 1 : 0;
1019         case AC97_3D_CONTROL:
1020                 if (ac97->caps & 0x7c00) {
1021                         val = snd_ac97_read(ac97, reg);
1022                         /* if nonzero - fixed and we can't set it */
1023                         return val == 0;
1024                 }
1025                 return 0;
1026         case AC97_CENTER_LFE_MASTER:    /* center */
1027                 if ((ac97->ext_id & AC97_EI_CDAC) == 0)
1028                         return 0;
1029                 break;
1030         case AC97_CENTER_LFE_MASTER+1:  /* lfe */
1031                 if ((ac97->ext_id & AC97_EI_LDAC) == 0)
1032                         return 0;
1033                 reg = AC97_CENTER_LFE_MASTER;
1034                 mask = 0x0080;
1035                 break;
1036         case AC97_SURROUND_MASTER:
1037                 if ((ac97->ext_id & AC97_EI_SDAC) == 0)
1038                         return 0;
1039                 break;
1040         }
1041
1042         val = snd_ac97_read(ac97, reg);
1043         if (!(val & mask)) {
1044                 /* nothing seems to be here - mute flag is not set */
1045                 /* try another test */
1046                 snd_ac97_write_cache(ac97, reg, val | mask);
1047                 val = snd_ac97_read(ac97, reg);
1048                 val = snd_ac97_read(ac97, reg);
1049                 if (!(val & mask))
1050                         return 0;       /* nothing here */
1051         }
1052         return 1;               /* success, useable */
1053 }
1054
1055 static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
1056 {
1057         unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
1058         unsigned char max[3] = { 63, 31, 15 };
1059         int i;
1060
1061         /* first look up the static resolution table */
1062         if (ac97->res_table) {
1063                 const struct snd_ac97_res_table *tbl;
1064                 for (tbl = ac97->res_table; tbl->reg; tbl++) {
1065                         if (tbl->reg == reg) {
1066                                 *lo_max = tbl->bits & 0xff;
1067                                 *hi_max = (tbl->bits >> 8) & 0xff;
1068                                 return;
1069                         }
1070                 }
1071         }
1072
1073         *lo_max = *hi_max = 0;
1074         for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
1075                 unsigned short val;
1076                 snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
1077                 /* Do the read twice due to buffers on some ac97 codecs.
1078                  * e.g. The STAC9704 returns exactly what you wrote the the register
1079                  * if you read it immediately. This causes the detect routine to fail.
1080                  */
1081                 val = snd_ac97_read(ac97, reg);
1082                 val = snd_ac97_read(ac97, reg);
1083                 if (! *lo_max && (val & 0x7f) == cbit[i])
1084                         *lo_max = max[i];
1085                 if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
1086                         *hi_max = max[i];
1087                 if (*lo_max && *hi_max)
1088                         break;
1089         }
1090 }
1091
1092 int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit)
1093 {
1094         unsigned short mask, val, orig, res;
1095
1096         mask = 1 << bit;
1097         orig = snd_ac97_read(ac97, reg);
1098         val = orig ^ mask;
1099         snd_ac97_write(ac97, reg, val);
1100         res = snd_ac97_read(ac97, reg);
1101         snd_ac97_write_cache(ac97, reg, orig);
1102         return res == val;
1103 }
1104
1105 /* check the volume resolution of center/lfe */
1106 static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max)
1107 {
1108         unsigned short val, val1;
1109
1110         *max = 63;
1111         val = 0x8080 | (0x20 << shift);
1112         snd_ac97_write(ac97, reg, val);
1113         val1 = snd_ac97_read(ac97, reg);
1114         if (val != val1) {
1115                 *max = 31;
1116         }
1117         /* reset volume to zero */
1118         snd_ac97_write_cache(ac97, reg, 0x8080);
1119 }
1120
1121 static inline int printable(unsigned int x)
1122 {
1123         x &= 0xff;
1124         if (x < ' ' || x >= 0x71) {
1125                 if (x <= 0x89)
1126                         return x - 0x71 + 'A';
1127                 return '?';
1128         }
1129         return x;
1130 }
1131
1132 struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template, struct snd_ac97 * ac97)
1133 {
1134         struct snd_kcontrol_new template;
1135         memcpy(&template, _template, sizeof(template));
1136         template.index = ac97->num;
1137         return snd_ctl_new1(&template, ac97);
1138 }
1139
1140 /*
1141  * create mute switch(es) for normal stereo controls
1142  */
1143 static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg,
1144                                      int check_stereo, int check_amix,
1145                                      struct snd_ac97 *ac97)
1146 {
1147         struct snd_kcontrol *kctl;
1148         int err;
1149         unsigned short val, val1, mute_mask;
1150
1151         if (! snd_ac97_valid_reg(ac97, reg))
1152                 return 0;
1153
1154         mute_mask = 0x8000;
1155         val = snd_ac97_read(ac97, reg);
1156         if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
1157                 /* check whether both mute bits work */
1158                 val1 = val | 0x8080;
1159                 snd_ac97_write(ac97, reg, val1);
1160                 if (val1 == snd_ac97_read(ac97, reg))
1161                         mute_mask = 0x8080;
1162         }
1163         if (mute_mask == 0x8080) {
1164                 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
1165                 if (check_amix)
1166                         tmp.private_value |= (1 << 30);
1167                 tmp.index = ac97->num;
1168                 kctl = snd_ctl_new1(&tmp, ac97);
1169         } else {
1170                 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1);
1171                 if (check_amix)
1172                         tmp.private_value |= (1 << 30);
1173                 tmp.index = ac97->num;
1174                 kctl = snd_ctl_new1(&tmp, ac97);
1175         }
1176         err = snd_ctl_add(card, kctl);
1177         if (err < 0)
1178                 return err;
1179         /* mute as default */
1180         snd_ac97_write_cache(ac97, reg, val | mute_mask);
1181         return 0;
1182 }
1183
1184 /*
1185  * create a volume for normal stereo/mono controls
1186  */
1187 static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max,
1188                              unsigned int hi_max, struct snd_ac97 *ac97)
1189 {
1190         int err;
1191         struct snd_kcontrol *kctl;
1192
1193         if (! snd_ac97_valid_reg(ac97, reg))
1194                 return 0;
1195         if (hi_max) {
1196                 /* invert */
1197                 struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
1198                 tmp.index = ac97->num;
1199                 kctl = snd_ctl_new1(&tmp, ac97);
1200         } else {
1201                 /* invert */
1202                 struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
1203                 tmp.index = ac97->num;
1204                 kctl = snd_ctl_new1(&tmp, ac97);
1205         }
1206         err = snd_ctl_add(card, kctl);
1207         if (err < 0)
1208                 return err;
1209         snd_ac97_write_cache(ac97, reg,
1210                              (snd_ac97_read(ac97, reg) & 0x8080) |
1211                              lo_max | (hi_max << 8));
1212         return 0;
1213 }
1214
1215 /*
1216  * create a mute-switch and a volume for normal stereo/mono controls
1217  */
1218 static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx,
1219                                     int reg, int check_stereo, int check_amix,
1220                                     struct snd_ac97 *ac97)
1221 {
1222         int err;
1223         char name[44];
1224         unsigned char lo_max, hi_max;
1225
1226         if (! snd_ac97_valid_reg(ac97, reg))
1227                 return 0;
1228
1229         if (snd_ac97_try_bit(ac97, reg, 15)) {
1230                 sprintf(name, "%s Switch", pfx);
1231                 if ((err = snd_ac97_cmute_new_stereo(card, name, reg,
1232                                                      check_stereo, check_amix,
1233                                                      ac97)) < 0)
1234                         return err;
1235         }
1236         check_volume_resolution(ac97, reg, &lo_max, &hi_max);
1237         if (lo_max) {
1238                 sprintf(name, "%s Volume", pfx);
1239                 if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
1240                         return err;
1241         }
1242         return 0;
1243 }
1244
1245 #define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \
1246         snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97)
1247 #define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \
1248         snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97)
1249
1250 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97);
1251
1252 static int snd_ac97_mixer_build(struct snd_ac97 * ac97)
1253 {
1254         struct snd_card *card = ac97->bus->card;
1255         struct snd_kcontrol *kctl;
1256         int err;
1257         unsigned int idx;
1258         unsigned char max;
1259
1260         /* build master controls */
1261         /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
1262         if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
1263                 if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
1264                         err = snd_ac97_cmute_new(card, "Master Playback Switch",
1265                                                  AC97_MASTER, 0, ac97);
1266                 else
1267                         err = snd_ac97_cmix_new(card, "Master Playback",
1268                                                 AC97_MASTER, 0, ac97);
1269                 if (err < 0)
1270                         return err;
1271         }
1272
1273         ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
1274
1275         /* build center controls */
1276         if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) 
1277                 && !(ac97->flags & AC97_AD_MULTI)) {
1278                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
1279                         return err;
1280                 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
1281                         return err;
1282                 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
1283                 kctl->private_value &= ~(0xff << 16);
1284                 kctl->private_value |= (int)max << 16;
1285                 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
1286         }
1287
1288         /* build LFE controls */
1289         if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1))
1290                 && !(ac97->flags & AC97_AD_MULTI)) {
1291                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
1292                         return err;
1293                 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
1294                         return err;
1295                 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
1296                 kctl->private_value &= ~(0xff << 16);
1297                 kctl->private_value |= (int)max << 16;
1298                 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
1299         }
1300
1301         /* build surround controls */
1302         if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) 
1303                 && !(ac97->flags & AC97_AD_MULTI)) {
1304                 /* Surround Master (0x38) is with stereo mutes */
1305                 if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback",
1306                                                     AC97_SURROUND_MASTER, 1, 0,
1307                                                     ac97)) < 0)
1308                         return err;
1309         }
1310
1311         /* build headphone controls */
1312         if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
1313                 if ((err = snd_ac97_cmix_new(card, "Headphone Playback",
1314                                              AC97_HEADPHONE, 0, ac97)) < 0)
1315                         return err;
1316         }
1317         
1318         /* build master mono controls */
1319         if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
1320                 if ((err = snd_ac97_cmix_new(card, "Master Mono Playback",
1321                                              AC97_MASTER_MONO, 0, ac97)) < 0)
1322                         return err;
1323         }
1324         
1325         /* build master tone controls */
1326         if (!(ac97->flags & AC97_HAS_NO_TONE)) {
1327                 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
1328                         for (idx = 0; idx < 2; idx++) {
1329                                 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
1330                                         return err;
1331                                 if (ac97->id == AC97_ID_YMF753) {
1332                                         kctl->private_value &= ~(0xff << 16);
1333                                         kctl->private_value |= 7 << 16;
1334                                 }
1335                         }
1336                         snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
1337                 }
1338         }
1339         
1340         /* build PC Speaker controls */
1341         if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) && 
1342                 ((ac97->flags & AC97_HAS_PC_BEEP) ||
1343             snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
1344                 for (idx = 0; idx < 2; idx++)
1345                         if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
1346                                 return err;
1347                 snd_ac97_write_cache(ac97, AC97_PC_BEEP,
1348                                      snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
1349         }
1350         
1351         /* build Phone controls */
1352         if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
1353                 if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
1354                         if ((err = snd_ac97_cmix_new(card, "Phone Playback",
1355                                                      AC97_PHONE, 1, ac97)) < 0)
1356                                 return err;
1357                 }
1358         }
1359         
1360         /* build MIC controls */
1361         if (!(ac97->flags & AC97_HAS_NO_MIC)) {
1362                 if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
1363                         if ((err = snd_ac97_cmix_new(card, "Mic Playback",
1364                                                      AC97_MIC, 1, ac97)) < 0)
1365                                 return err;
1366                         if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
1367                                 return err;
1368                 }
1369         }
1370
1371         /* build Line controls */
1372         if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
1373                 if ((err = snd_ac97_cmix_new(card, "Line Playback",
1374                                              AC97_LINE, 1, ac97)) < 0)
1375                         return err;
1376         }
1377         
1378         /* build CD controls */
1379         if (!(ac97->flags & AC97_HAS_NO_CD)) {
1380                 if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
1381                         if ((err = snd_ac97_cmix_new(card, "CD Playback",
1382                                                      AC97_CD, 1, ac97)) < 0)
1383                                 return err;
1384                 }
1385         }
1386         
1387         /* build Video controls */
1388         if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
1389                 if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
1390                         if ((err = snd_ac97_cmix_new(card, "Video Playback",
1391                                                      AC97_VIDEO, 1, ac97)) < 0)
1392                                 return err;
1393                 }
1394         }
1395
1396         /* build Aux controls */
1397         if (!(ac97->flags & AC97_HAS_NO_AUX)) {
1398                 if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
1399                         if ((err = snd_ac97_cmix_new(card, "Aux Playback",
1400                                                      AC97_AUX, 1, ac97)) < 0)
1401                                 return err;
1402                 }
1403         }
1404
1405         /* build PCM controls */
1406         if (ac97->flags & AC97_AD_MULTI) {
1407                 unsigned short init_val;
1408                 if (ac97->flags & AC97_STEREO_MUTES)
1409                         init_val = 0x9f9f;
1410                 else
1411                         init_val = 0x9f1f;
1412                 for (idx = 0; idx < 2; idx++)
1413                         if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
1414                                 return err;
1415                 ac97->spec.ad18xx.pcmreg[0] = init_val;
1416                 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
1417                         for (idx = 0; idx < 2; idx++)
1418                                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
1419                                         return err;
1420                         ac97->spec.ad18xx.pcmreg[1] = init_val;
1421                 }
1422                 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
1423                         for (idx = 0; idx < 2; idx++)
1424                                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
1425                                         return err;
1426                         for (idx = 0; idx < 2; idx++)
1427                                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
1428                                         return err;
1429                         ac97->spec.ad18xx.pcmreg[2] = init_val;
1430                 }
1431                 snd_ac97_write_cache(ac97, AC97_PCM, init_val);
1432         } else {
1433                 if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
1434                         if (ac97->flags & AC97_HAS_NO_PCM_VOL)
1435                                 err = snd_ac97_cmute_new(card,
1436                                                          "PCM Playback Switch",
1437                                                          AC97_PCM, 0, ac97);
1438                         else
1439                                 err = snd_ac97_cmix_new(card, "PCM Playback",
1440                                                         AC97_PCM, 0, ac97);
1441                         if (err < 0)
1442                                 return err;
1443                 }
1444         }
1445
1446         /* build Capture controls */
1447         if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
1448                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
1449                         return err;
1450                 if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
1451                         err = snd_ac97_cmute_new(card, "Capture Switch",
1452                                                  AC97_REC_GAIN, 0, ac97);
1453                         if (err < 0)
1454                                 return err;
1455                 }
1456                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
1457                         return err;
1458                 snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
1459                 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
1460         }
1461         /* build MIC Capture controls */
1462         if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
1463                 for (idx = 0; idx < 2; idx++)
1464                         if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
1465                                 return err;
1466                 snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
1467         }
1468
1469         /* build PCM out path & mute control */
1470         if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
1471                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
1472                         return err;
1473         }
1474
1475         /* build Simulated Stereo Enhancement control */
1476         if (ac97->caps & 0x0008) {
1477                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
1478                         return err;
1479         }
1480
1481         /* build 3D Stereo Enhancement control */
1482         if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
1483                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
1484                         return err;
1485         }
1486
1487         /* build Loudness control */
1488         if (ac97->caps & 0x0020) {
1489                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
1490                         return err;
1491         }
1492
1493         /* build Mono output select control */
1494         if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
1495                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
1496                         return err;
1497         }
1498
1499         /* build Mic select control */
1500         if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
1501                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
1502                         return err;
1503         }
1504
1505         /* build ADC/DAC loopback control */
1506         if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
1507                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
1508                         return err;
1509         }
1510
1511         snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
1512
1513         /* build 3D controls */
1514         if (ac97->build_ops->build_3d) {
1515                 ac97->build_ops->build_3d(ac97);
1516         } else {
1517                 if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
1518                         unsigned short val;
1519                         val = 0x0707;
1520                         snd_ac97_write(ac97, AC97_3D_CONTROL, val);
1521                         val = snd_ac97_read(ac97, AC97_3D_CONTROL);
1522                         val = val == 0x0606;
1523                         if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
1524                                 return err;
1525                         if (val)
1526                                 kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
1527                         if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
1528                                 return err;
1529                         if (val)
1530                                 kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
1531                         snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
1532                 }
1533         }
1534
1535         /* build S/PDIF controls */
1536         if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
1537                 if (ac97->build_ops->build_spdif) {
1538                         if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
1539                                 return err;
1540                 } else {
1541                         for (idx = 0; idx < 5; idx++)
1542                                 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
1543                                         return err;
1544                         if (ac97->build_ops->build_post_spdif) {
1545                                 if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
1546                                         return err;
1547                         }
1548                         /* set default PCM S/PDIF params */
1549                         /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1550                         snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
1551                         ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
1552                 }
1553                 ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
1554         }
1555         
1556         /* build chip specific controls */
1557         if (ac97->build_ops->build_specific)
1558                 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1559                         return err;
1560
1561         if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
1562                 kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
1563                 if (! kctl)
1564                         return -ENOMEM;
1565                 if (ac97->scaps & AC97_SCAP_INV_EAPD)
1566                         set_inv_eapd(ac97, kctl);
1567                 if ((err = snd_ctl_add(card, kctl)) < 0)
1568                         return err;
1569         }
1570
1571         return 0;
1572 }
1573
1574 static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97)
1575 {
1576         int err, idx;
1577
1578         //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
1579         snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
1580         snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
1581         snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
1582         snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
1583         snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
1584
1585         /* build modem switches */
1586         for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
1587                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
1588                         return err;
1589
1590         /* build chip specific controls */
1591         if (ac97->build_ops->build_specific)
1592                 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1593                         return err;
1594
1595         return 0;
1596 }
1597
1598 static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate)
1599 {
1600         unsigned short val;
1601         unsigned int tmp;
1602
1603         tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
1604         snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
1605         if (shadow_reg)
1606                 snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
1607         val = snd_ac97_read(ac97, reg);
1608         return val == (tmp & 0xffff);
1609 }
1610
1611 static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result)
1612 {
1613         unsigned int result = 0;
1614         unsigned short saved;
1615
1616         if (ac97->bus->no_vra) {
1617                 *r_result = SNDRV_PCM_RATE_48000;
1618                 if ((ac97->flags & AC97_DOUBLE_RATE) &&
1619                     reg == AC97_PCM_FRONT_DAC_RATE)
1620                         *r_result |= SNDRV_PCM_RATE_96000;
1621                 return;
1622         }
1623
1624         saved = snd_ac97_read(ac97, reg);
1625         if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
1626                 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1627                                      AC97_EA_DRA, 0);
1628         /* test a non-standard rate */
1629         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
1630                 result |= SNDRV_PCM_RATE_CONTINUOUS;
1631         /* let's try to obtain standard rates */
1632         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
1633                 result |= SNDRV_PCM_RATE_8000;
1634         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
1635                 result |= SNDRV_PCM_RATE_11025;
1636         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
1637                 result |= SNDRV_PCM_RATE_16000;
1638         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
1639                 result |= SNDRV_PCM_RATE_22050;
1640         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
1641                 result |= SNDRV_PCM_RATE_32000;
1642         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
1643                 result |= SNDRV_PCM_RATE_44100;
1644         if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
1645                 result |= SNDRV_PCM_RATE_48000;
1646         if ((ac97->flags & AC97_DOUBLE_RATE) &&
1647             reg == AC97_PCM_FRONT_DAC_RATE) {
1648                 /* test standard double rates */
1649                 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1650                                      AC97_EA_DRA, AC97_EA_DRA);
1651                 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
1652                         result |= SNDRV_PCM_RATE_64000;
1653                 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
1654                         result |= SNDRV_PCM_RATE_88200;
1655                 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
1656                         result |= SNDRV_PCM_RATE_96000;
1657                 /* some codecs don't support variable double rates */
1658                 if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
1659                         result &= ~SNDRV_PCM_RATE_CONTINUOUS;
1660                 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1661                                      AC97_EA_DRA, 0);
1662         }
1663         /* restore the default value */
1664         snd_ac97_write_cache(ac97, reg, saved);
1665         if (shadow_reg)
1666                 snd_ac97_write_cache(ac97, shadow_reg, saved);
1667         *r_result = result;
1668 }
1669
1670 /* check AC97_SPDIF register to accept which sample rates */
1671 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97)
1672 {
1673         unsigned int result = 0;
1674         int i;
1675         static unsigned short ctl_bits[] = {
1676                 AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
1677         };
1678         static unsigned int rate_bits[] = {
1679                 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
1680         };
1681
1682         for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
1683                 snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
1684                 if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
1685                         result |= rate_bits[i];
1686         }
1687         return result;
1688 }
1689
1690 /* look for the codec id table matching with the given id */
1691 static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table,
1692                                                      unsigned int id)
1693 {
1694         const struct ac97_codec_id *pid;
1695
1696         for (pid = table; pid->id; pid++)
1697                 if (pid->id == (id & pid->mask))
1698                         return pid;
1699         return NULL;
1700 }
1701
1702 void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem)
1703 {
1704         const struct ac97_codec_id *pid;
1705
1706         sprintf(name, "0x%x %c%c%c", id,
1707                 printable(id >> 24),
1708                 printable(id >> 16),
1709                 printable(id >> 8));
1710         pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
1711         if (! pid)
1712                 return;
1713
1714         strcpy(name, pid->name);
1715         if (ac97 && pid->patch) {
1716                 if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1717                     (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1718                         pid->patch(ac97);
1719         } 
1720
1721         pid = look_for_codec_id(snd_ac97_codec_ids, id);
1722         if (pid) {
1723                 strcat(name, " ");
1724                 strcat(name, pid->name);
1725                 if (pid->mask != 0xffffffff)
1726                         sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
1727                 if (ac97 && pid->patch) {
1728                         if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1729                             (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1730                                 pid->patch(ac97);
1731                 }
1732         } else
1733                 sprintf(name + strlen(name), " id %x", id & 0xff);
1734 }
1735
1736 /**
1737  * snd_ac97_get_short_name - retrieve codec name
1738  * @ac97: the codec instance
1739  *
1740  * Returns the short identifying name of the codec.
1741  */
1742 const char *snd_ac97_get_short_name(struct snd_ac97 *ac97)
1743 {
1744         const struct ac97_codec_id *pid;
1745
1746         for (pid = snd_ac97_codec_ids; pid->id; pid++)
1747                 if (pid->id == (ac97->id & pid->mask))
1748                         return pid->name;
1749         return "unknown codec";
1750 }
1751
1752 EXPORT_SYMBOL(snd_ac97_get_short_name);
1753
1754 /* wait for a while until registers are accessible after RESET
1755  * return 0 if ok, negative not ready
1756  */
1757 static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem)
1758 {
1759         unsigned long end_time;
1760         unsigned short val;
1761
1762         end_time = jiffies + timeout;
1763         do {
1764                 
1765                 /* use preliminary reads to settle the communication */
1766                 snd_ac97_read(ac97, AC97_RESET);
1767                 snd_ac97_read(ac97, AC97_VENDOR_ID1);
1768                 snd_ac97_read(ac97, AC97_VENDOR_ID2);
1769                 /* modem? */
1770                 if (with_modem) {
1771                         val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1772                         if (val != 0xffff && (val & 1) != 0)
1773                                 return 0;
1774                 }
1775                 if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
1776                         /* probably only Xbox issue - all registers are read as zero */
1777                         val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
1778                         if (val != 0 && val != 0xffff)
1779                                 return 0;
1780                 } else {
1781                         /* because the PCM or MASTER volume registers can be modified,
1782                          * the REC_GAIN register is used for tests
1783                          */
1784                         /* test if we can write to the record gain volume register */
1785                         snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
1786                         if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
1787                                 return 0;
1788                 }
1789                 schedule_timeout_uninterruptible(1);
1790         } while (time_after_eq(end_time, jiffies));
1791         return -ENODEV;
1792 }
1793
1794 /**
1795  * snd_ac97_bus - create an AC97 bus component
1796  * @card: the card instance
1797  * @num: the bus number
1798  * @ops: the bus callbacks table
1799  * @private_data: private data pointer for the new instance
1800  * @rbus: the pointer to store the new AC97 bus instance.
1801  *
1802  * Creates an AC97 bus component.  An struct snd_ac97_bus instance is newly
1803  * allocated and initialized.
1804  *
1805  * The ops table must include valid callbacks (at least read and
1806  * write).  The other callbacks, wait and reset, are not mandatory.
1807  * 
1808  * The clock is set to 48000.  If another clock is needed, set
1809  * (*rbus)->clock manually.
1810  *
1811  * The AC97 bus instance is registered as a low-level device, so you don't
1812  * have to release it manually.
1813  *
1814  * Returns zero if successful, or a negative error code on failure.
1815  */
1816 int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops,
1817                  void *private_data, struct snd_ac97_bus **rbus)
1818 {
1819         int err;
1820         struct snd_ac97_bus *bus;
1821         static struct snd_device_ops dev_ops = {
1822                 .dev_free =     snd_ac97_bus_dev_free,
1823         };
1824
1825         snd_assert(card != NULL, return -EINVAL);
1826         snd_assert(rbus != NULL, return -EINVAL);
1827         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1828         if (bus == NULL)
1829                 return -ENOMEM;
1830         bus->card = card;
1831         bus->num = num;
1832         bus->ops = ops;
1833         bus->private_data = private_data;
1834         bus->clock = 48000;
1835         spin_lock_init(&bus->bus_lock);
1836         snd_ac97_bus_proc_init(bus);
1837         if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
1838                 snd_ac97_bus_free(bus);
1839                 return err;
1840         }
1841         *rbus = bus;
1842         return 0;
1843 }
1844
1845 EXPORT_SYMBOL(snd_ac97_bus);
1846
1847 /* stop no dev release warning */
1848 static void ac97_device_release(struct device * dev)
1849 {
1850 }
1851
1852 /* register ac97 codec to bus */
1853 static int snd_ac97_dev_register(struct snd_device *device)
1854 {
1855         struct snd_ac97 *ac97 = device->device_data;
1856         int err;
1857
1858         ac97->dev.bus = &ac97_bus_type;
1859         ac97->dev.parent = ac97->bus->card->dev;
1860         ac97->dev.release = ac97_device_release;
1861         snprintf(ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
1862                  ac97->bus->card->number, ac97->num,
1863                  snd_ac97_get_short_name(ac97));
1864         if ((err = device_register(&ac97->dev)) < 0) {
1865                 snd_printk(KERN_ERR "Can't register ac97 bus\n");
1866                 ac97->dev.bus = NULL;
1867                 return err;
1868         }
1869         return 0;
1870 }
1871
1872 /* disconnect ac97 codec */
1873 static int snd_ac97_dev_disconnect(struct snd_device *device)
1874 {
1875         struct snd_ac97 *ac97 = device->device_data;
1876         if (ac97->dev.bus)
1877                 device_unregister(&ac97->dev);
1878         return 0;
1879 }
1880
1881 /* build_ops to do nothing */
1882 static struct snd_ac97_build_ops null_build_ops;
1883
1884 #ifdef CONFIG_SND_AC97_POWER_SAVE
1885 static void do_update_power(void *data)
1886 {
1887         update_power_regs(data);
1888 }
1889 #endif
1890
1891 /**
1892  * snd_ac97_mixer - create an Codec97 component
1893  * @bus: the AC97 bus which codec is attached to
1894  * @template: the template of ac97, including index, callbacks and
1895  *         the private data.
1896  * @rac97: the pointer to store the new ac97 instance.
1897  *
1898  * Creates an Codec97 component.  An struct snd_ac97 instance is newly
1899  * allocated and initialized from the template.  The codec
1900  * is then initialized by the standard procedure.
1901  *
1902  * The template must include the codec number (num) and address (addr),
1903  * and the private data (private_data).
1904  * 
1905  * The ac97 instance is registered as a low-level device, so you don't
1906  * have to release it manually.
1907  *
1908  * Returns zero if successful, or a negative error code on failure.
1909  */
1910 int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97)
1911 {
1912         int err;
1913         struct snd_ac97 *ac97;
1914         struct snd_card *card;
1915         char name[64];
1916         unsigned long end_time;
1917         unsigned int reg;
1918         const struct ac97_codec_id *pid;
1919         static struct snd_device_ops ops = {
1920                 .dev_free =     snd_ac97_dev_free,
1921                 .dev_register = snd_ac97_dev_register,
1922                 .dev_disconnect =       snd_ac97_dev_disconnect,
1923         };
1924
1925         snd_assert(rac97 != NULL, return -EINVAL);
1926         *rac97 = NULL;
1927         snd_assert(bus != NULL && template != NULL, return -EINVAL);
1928         snd_assert(template->num < 4 && bus->codec[template->num] == NULL, return -EINVAL);
1929
1930         card = bus->card;
1931         ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL);
1932         if (ac97 == NULL)
1933                 return -ENOMEM;
1934         ac97->private_data = template->private_data;
1935         ac97->private_free = template->private_free;
1936         ac97->bus = bus;
1937         ac97->pci = template->pci;
1938         ac97->num = template->num;
1939         ac97->addr = template->addr;
1940         ac97->scaps = template->scaps;
1941         ac97->res_table = template->res_table;
1942         bus->codec[ac97->num] = ac97;
1943         mutex_init(&ac97->reg_mutex);
1944         mutex_init(&ac97->page_mutex);
1945 #ifdef CONFIG_SND_AC97_POWER_SAVE
1946         ac97->power_workq = create_workqueue("ac97");
1947         INIT_WORK(&ac97->power_work, do_update_power, ac97);
1948 #endif
1949
1950 #ifdef CONFIG_PCI
1951         if (ac97->pci) {
1952                 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
1953                 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
1954         }
1955 #endif
1956         if (bus->ops->reset) {
1957                 bus->ops->reset(ac97);
1958                 goto __access_ok;
1959         }
1960
1961         ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
1962         ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
1963         if (ac97->id && ac97->id != (unsigned int)-1) {
1964                 pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
1965                 if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF))
1966                         goto __access_ok;
1967         }
1968
1969         /* reset to defaults */
1970         if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
1971                 snd_ac97_write(ac97, AC97_RESET, 0);
1972         if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
1973                 snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
1974         if (bus->ops->wait)
1975                 bus->ops->wait(ac97);
1976         else {
1977                 udelay(50);
1978                 if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
1979                         err = ac97_reset_wait(ac97, HZ/2, 1);
1980                 else {
1981                         err = ac97_reset_wait(ac97, HZ/2, 0);
1982                         if (err < 0)
1983                                 err = ac97_reset_wait(ac97, HZ/2, 1);
1984                 }
1985                 if (err < 0) {
1986                         snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
1987                         /* proceed anyway - it's often non-critical */
1988                 }
1989         }
1990       __access_ok:
1991         ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
1992         ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
1993         if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
1994             (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
1995                 snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
1996                 snd_ac97_free(ac97);
1997                 return -EIO;
1998         }
1999         pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
2000         if (pid)
2001                 ac97->flags |= pid->flags;
2002         
2003         /* test for AC'97 */
2004         if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
2005                 /* test if we can write to the record gain volume register */
2006                 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
2007                 if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
2008                         ac97->scaps |= AC97_SCAP_AUDIO;
2009         }
2010         if (ac97->scaps & AC97_SCAP_AUDIO) {
2011                 ac97->caps = snd_ac97_read(ac97, AC97_RESET);
2012                 ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
2013                 if (ac97->ext_id == 0xffff)     /* invalid combination */
2014                         ac97->ext_id = 0;
2015         }
2016
2017         /* test for MC'97 */
2018         if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
2019                 ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2020                 if (ac97->ext_mid == 0xffff)    /* invalid combination */
2021                         ac97->ext_mid = 0;
2022                 if (ac97->ext_mid & 1)
2023                         ac97->scaps |= AC97_SCAP_MODEM;
2024         }
2025
2026         if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
2027                 if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
2028                         snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
2029                 snd_ac97_free(ac97);
2030                 return -EACCES;
2031         }
2032
2033         if (bus->ops->reset) // FIXME: always skipping?
2034                 goto __ready_ok;
2035
2036         /* FIXME: add powerdown control */
2037         if (ac97_is_audio(ac97)) {
2038                 /* nothing should be in powerdown mode */
2039                 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2040                 if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
2041                         snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
2042                         udelay(100);
2043                         snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2044                 }
2045                 /* nothing should be in powerdown mode */
2046                 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
2047                 end_time = jiffies + (HZ / 10);
2048                 do {
2049                         if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
2050                                 goto __ready_ok;
2051                         schedule_timeout_uninterruptible(1);
2052                 } while (time_after_eq(end_time, jiffies));
2053                 snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num);
2054         }
2055
2056         /* FIXME: add powerdown control */
2057         if (ac97_is_modem(ac97)) {
2058                 unsigned char tmp;
2059
2060                 /* nothing should be in powerdown mode */
2061                 /* note: it's important to set the rate at first */
2062                 tmp = AC97_MEA_GPIO;
2063                 if (ac97->ext_mid & AC97_MEI_LINE1) {
2064                         snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000);
2065                         tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
2066                 }
2067                 if (ac97->ext_mid & AC97_MEI_LINE2) {
2068                         snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000);
2069                         tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
2070                 }
2071                 if (ac97->ext_mid & AC97_MEI_HANDSET) {
2072                         snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000);
2073                         tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
2074                 }
2075                 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
2076                 udelay(100);
2077                 /* nothing should be in powerdown mode */
2078                 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
2079                 end_time = jiffies + (HZ / 10);
2080                 do {
2081                         if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
2082                                 goto __ready_ok;
2083                         schedule_timeout_uninterruptible(1);
2084                 } while (time_after_eq(end_time, jiffies));
2085                 snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
2086         }
2087         
2088       __ready_ok:
2089         if (ac97_is_audio(ac97))
2090                 ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
2091         else
2092                 ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
2093         if (ac97->ext_id & 0x01c9) {    /* L/R, MIC, SDAC, LDAC VRA support */
2094                 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2095                 reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
2096                 if (! bus->no_vra)
2097                         reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
2098                 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
2099         }
2100         if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
2101                 /* Intel controllers require double rate data to be put in
2102                  * slots 7+8, so let's hope the codec supports it. */
2103                 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
2104                 if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
2105                         ac97->flags |= AC97_DOUBLE_RATE;
2106                 /* restore to slots 10/11 to avoid the confliction with surrounds */
2107                 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0);
2108         }
2109         if (ac97->ext_id & AC97_EI_VRA) {       /* VRA support */
2110                 snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
2111                 snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
2112         } else {
2113                 ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
2114                 if (ac97->flags & AC97_DOUBLE_RATE)
2115                         ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
2116                 ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
2117         }
2118         if (ac97->ext_id & AC97_EI_SPDIF) {
2119                 /* codec specific code (patch) should override these values */
2120                 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
2121         }
2122         if (ac97->ext_id & AC97_EI_VRM) {       /* MIC VRA support */
2123                 snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
2124         } else {
2125                 ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
2126         }
2127         if (ac97->ext_id & AC97_EI_SDAC) {      /* SDAC support */
2128                 snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
2129                 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
2130         }
2131         if (ac97->ext_id & AC97_EI_LDAC) {      /* LDAC support */
2132                 snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
2133                 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
2134         }
2135         /* additional initializations */
2136         if (bus->ops->init)
2137                 bus->ops->init(ac97);
2138         snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
2139         snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97));  // ac97->id might be changed in the special setup code
2140         if (! ac97->build_ops)
2141                 ac97->build_ops = &null_build_ops;
2142
2143         if (ac97_is_audio(ac97)) {
2144                 char comp[16];
2145                 if (card->mixername[0] == '\0') {
2146                         strcpy(card->mixername, name);
2147                 } else {
2148                         if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2149                                 strcat(card->mixername, ",");
2150                                 strcat(card->mixername, name);
2151                         }
2152                 }
2153                 sprintf(comp, "AC97a:%08x", ac97->id);
2154                 if ((err = snd_component_add(card, comp)) < 0) {
2155                         snd_ac97_free(ac97);
2156                         return err;
2157                 }
2158                 if (snd_ac97_mixer_build(ac97) < 0) {
2159                         snd_ac97_free(ac97);
2160                         return -ENOMEM;
2161                 }
2162         }
2163         if (ac97_is_modem(ac97)) {
2164                 char comp[16];
2165                 if (card->mixername[0] == '\0') {
2166                         strcpy(card->mixername, name);
2167                 } else {
2168                         if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2169                                 strcat(card->mixername, ",");
2170                                 strcat(card->mixername, name);
2171                         }
2172                 }
2173                 sprintf(comp, "AC97m:%08x", ac97->id);
2174                 if ((err = snd_component_add(card, comp)) < 0) {
2175                         snd_ac97_free(ac97);
2176                         return err;
2177                 }
2178                 if (snd_ac97_modem_build(card, ac97) < 0) {
2179                         snd_ac97_free(ac97);
2180                         return -ENOMEM;
2181                 }
2182         }
2183         if (ac97_is_audio(ac97))
2184                 update_power_regs(ac97);
2185         snd_ac97_proc_init(ac97);
2186         if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
2187                 snd_ac97_free(ac97);
2188                 return err;
2189         }
2190         *rac97 = ac97;
2191         return 0;
2192 }
2193
2194 EXPORT_SYMBOL(snd_ac97_mixer);
2195
2196 /*
2197  * Power down the chip.
2198  *
2199  * MASTER and HEADPHONE registers are muted but the register cache values
2200  * are not changed, so that the values can be restored in snd_ac97_resume().
2201  */
2202 static void snd_ac97_powerdown(struct snd_ac97 *ac97)
2203 {
2204         unsigned short power;
2205
2206         if (ac97_is_audio(ac97)) {
2207                 /* some codecs have stereo mute bits */
2208                 snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
2209                 snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
2210         }
2211
2212         /* surround, CLFE, mic powerdown */
2213         power = ac97->regs[AC97_EXTENDED_STATUS];
2214         if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2215                 power |= AC97_EA_PRJ;
2216         if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2217                 power |= AC97_EA_PRI | AC97_EA_PRK;
2218         power |= AC97_EA_PRL;
2219         snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power);
2220
2221         /* powerdown external amplifier */
2222         if (ac97->scaps & AC97_SCAP_INV_EAPD)
2223                 power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD;
2224         else if (! (ac97->scaps & AC97_SCAP_EAPD_LED))
2225                 power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD;
2226         power |= AC97_PD_PR6;   /* Headphone amplifier powerdown */
2227         power |= AC97_PD_PR0 | AC97_PD_PR1;     /* ADC & DAC powerdown */
2228         snd_ac97_write(ac97, AC97_POWERDOWN, power);
2229         udelay(100);
2230         power |= AC97_PD_PR2 | AC97_PD_PR3;     /* Analog Mixer powerdown */
2231         snd_ac97_write(ac97, AC97_POWERDOWN, power);
2232 #ifdef CONFIG_SND_AC97_POWER_SAVE
2233         if (power_save) {
2234                 udelay(100);
2235                 /* AC-link powerdown, internal Clk disable */
2236                 /* FIXME: this may cause click noises on some boards */
2237                 power |= AC97_PD_PR4 | AC97_PD_PR5;
2238                 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2239         }
2240 #endif
2241 }
2242
2243
2244 struct ac97_power_reg {
2245         unsigned short reg;
2246         unsigned short power_reg;
2247         unsigned short mask;
2248 };
2249
2250 enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE };
2251
2252 static struct ac97_power_reg power_regs[PWIDX_SIZE] = {
2253         [PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0},
2254         [PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1},
2255         [PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS,
2256                          AC97_EA_PRI | AC97_EA_PRK},
2257         [PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS,
2258                          AC97_EA_PRJ},
2259         [PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS,
2260                         AC97_EA_PRL},
2261 };
2262
2263 #ifdef CONFIG_SND_AC97_POWER_SAVE
2264 /**
2265  * snd_ac97_update_power - update the powerdown register
2266  * @ac97: the codec instance
2267  * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE
2268  * @powerup: non-zero when power up the part
2269  *
2270  * Update the AC97 powerdown register bits of the given part.
2271  */
2272 int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup)
2273 {
2274         int i;
2275
2276         if (! ac97)
2277                 return 0;
2278
2279         if (reg) {
2280                 /* SPDIF requires DAC power, too */
2281                 if (reg == AC97_SPDIF)
2282                         reg = AC97_PCM_FRONT_DAC_RATE;
2283                 for (i = 0; i < PWIDX_SIZE; i++) {
2284                         if (power_regs[i].reg == reg) {
2285                                 if (powerup)
2286                                         ac97->power_up |= (1 << i);
2287                                 else
2288                                         ac97->power_up &= ~(1 << i);
2289                                 break;
2290                         }
2291                 }
2292         }
2293
2294         if (! power_save)
2295                 return 0;
2296
2297         if (! powerup && ac97->power_workq)
2298                 /* adjust power-down bits after two seconds delay
2299                  * (for avoiding loud click noises for many (OSS) apps
2300                  *  that open/close frequently)
2301                  */
2302                 queue_delayed_work(ac97->power_workq, &ac97->power_work, HZ*2);
2303         else
2304                 update_power_regs(ac97);
2305
2306         return 0;
2307 }
2308
2309 EXPORT_SYMBOL(snd_ac97_update_power);
2310 #endif /* CONFIG_SND_AC97_POWER_SAVE */
2311
2312 static void update_power_regs(struct snd_ac97 *ac97)
2313 {
2314         unsigned int power_up, bits;
2315         int i;
2316
2317 #ifdef CONFIG_SND_AC97_POWER_SAVE
2318         if (power_save)
2319                 power_up = ac97->power_up;
2320         else {
2321 #endif
2322                 power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC);
2323                 power_up |= (1 << PWIDX_MIC);
2324                 if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2325                         power_up |= (1 << PWIDX_SURR);
2326                 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2327                         power_up |= (1 << PWIDX_CLFE);
2328 #ifdef CONFIG_SND_AC97_POWER_SAVE
2329         }
2330 #endif
2331         if (power_up) {
2332                 if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) {
2333                         /* needs power-up analog mix and vref */
2334                         snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2335                                              AC97_PD_PR3, 0);
2336                         msleep(1);
2337                         snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2338                                              AC97_PD_PR2, 0);
2339                 }
2340         }
2341         for (i = 0; i < PWIDX_SIZE; i++) {
2342                 if (power_up & (1 << i))
2343                         bits = 0;
2344                 else
2345                         bits = power_regs[i].mask;
2346                 snd_ac97_update_bits(ac97, power_regs[i].power_reg,
2347                                      power_regs[i].mask, bits);
2348         }
2349         if (! power_up) {
2350                 if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) {
2351                         /* power down analog mix and vref */
2352                         snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2353                                              AC97_PD_PR2, AC97_PD_PR2);
2354                         snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2355                                              AC97_PD_PR3, AC97_PD_PR3);
2356                 }
2357         }
2358 }
2359
2360
2361 #ifdef CONFIG_PM
2362 /**
2363  * snd_ac97_suspend - General suspend function for AC97 codec
2364  * @ac97: the ac97 instance
2365  *
2366  * Suspends the codec, power down the chip.
2367  */
2368 void snd_ac97_suspend(struct snd_ac97 *ac97)
2369 {
2370         if (! ac97)
2371                 return;
2372         if (ac97->build_ops->suspend)
2373                 ac97->build_ops->suspend(ac97);
2374         snd_ac97_powerdown(ac97);
2375 }
2376
2377 EXPORT_SYMBOL(snd_ac97_suspend);
2378
2379 /*
2380  * restore ac97 status
2381  */
2382 void snd_ac97_restore_status(struct snd_ac97 *ac97)
2383 {
2384         int i;
2385
2386         for (i = 2; i < 0x7c ; i += 2) {
2387                 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
2388                         continue;
2389                 /* restore only accessible registers
2390                  * some chip (e.g. nm256) may hang up when unsupported registers
2391                  * are accessed..!
2392                  */
2393                 if (test_bit(i, ac97->reg_accessed)) {
2394                         snd_ac97_write(ac97, i, ac97->regs[i]);
2395                         snd_ac97_read(ac97, i);
2396                 }
2397         }
2398 }
2399
2400 /*
2401  * restore IEC958 status
2402  */
2403 void snd_ac97_restore_iec958(struct snd_ac97 *ac97)
2404 {
2405         if (ac97->ext_id & AC97_EI_SPDIF) {
2406                 if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
2407                         /* reset spdif status */
2408                         snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2409                         snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
2410                         if (ac97->flags & AC97_CS_SPDIF)
2411                                 snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
2412                         else
2413                                 snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
2414                         snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
2415                 }
2416         }
2417 }
2418
2419 /**
2420  * snd_ac97_resume - General resume function for AC97 codec
2421  * @ac97: the ac97 instance
2422  *
2423  * Do the standard resume procedure, power up and restoring the
2424  * old register values.
2425  */
2426 void snd_ac97_resume(struct snd_ac97 *ac97)
2427 {
2428         unsigned long end_time;
2429
2430         if (! ac97)
2431                 return;
2432
2433         if (ac97->bus->ops->reset) {
2434                 ac97->bus->ops->reset(ac97);
2435                 goto  __reset_ready;
2436         }
2437
2438         snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2439         if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
2440                 snd_ac97_write(ac97, AC97_RESET, 0);
2441                 udelay(100);
2442                 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2443         }
2444         snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
2445
2446         snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
2447         if (ac97_is_audio(ac97)) {
2448                 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
2449                 end_time = jiffies + msecs_to_jiffies(100);
2450                 do {
2451                         if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
2452                                 break;
2453                         schedule_timeout_uninterruptible(1);
2454                 } while (time_after_eq(end_time, jiffies));
2455                 /* FIXME: extra delay */
2456                 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000);
2457                 if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000)
2458                         msleep(250);
2459         } else {
2460                 end_time = jiffies + msecs_to_jiffies(100);
2461                 do {
2462                         unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2463                         if (val != 0xffff && (val & 1) != 0)
2464                                 break;
2465                         schedule_timeout_uninterruptible(1);
2466                 } while (time_after_eq(end_time, jiffies));
2467         }
2468 __reset_ready:
2469
2470         if (ac97->bus->ops->init)
2471                 ac97->bus->ops->init(ac97);
2472
2473         if (ac97->build_ops->resume)
2474                 ac97->build_ops->resume(ac97);
2475         else {
2476                 snd_ac97_restore_status(ac97);
2477                 snd_ac97_restore_iec958(ac97);
2478         }
2479 }
2480
2481 EXPORT_SYMBOL(snd_ac97_resume);
2482 #endif
2483
2484
2485 /*
2486  * Hardware tuning
2487  */
2488 static void set_ctl_name(char *dst, const char *src, const char *suffix)
2489 {
2490         if (suffix)
2491                 sprintf(dst, "%s %s", src, suffix);
2492         else
2493                 strcpy(dst, src);
2494 }       
2495
2496 /* remove the control with the given name and optional suffix */
2497 int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name, const char *suffix)
2498 {
2499         struct snd_ctl_elem_id id;
2500         memset(&id, 0, sizeof(id));
2501         set_ctl_name(id.name, name, suffix);
2502         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2503         return snd_ctl_remove_id(ac97->bus->card, &id);
2504 }
2505
2506 static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix)
2507 {
2508         struct snd_ctl_elem_id sid;
2509         memset(&sid, 0, sizeof(sid));
2510         set_ctl_name(sid.name, name, suffix);
2511         sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2512         return snd_ctl_find_id(ac97->bus->card, &sid);
2513 }
2514
2515 /* rename the control with the given name and optional suffix */
2516 int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src, const char *dst, const char *suffix)
2517 {
2518         struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
2519         if (kctl) {
2520                 set_ctl_name(kctl->id.name, dst, suffix);
2521                 return 0;
2522         }
2523         return -ENOENT;
2524 }
2525
2526 /* rename both Volume and Switch controls - don't check the return value */
2527 void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src, const char *dst)
2528 {
2529         snd_ac97_rename_ctl(ac97, src, dst, "Switch");
2530         snd_ac97_rename_ctl(ac97, src, dst, "Volume");
2531 }
2532
2533 /* swap controls */
2534 int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1, const char *s2, const char *suffix)
2535 {
2536         struct snd_kcontrol *kctl1, *kctl2;
2537         kctl1 = ctl_find(ac97, s1, suffix);
2538         kctl2 = ctl_find(ac97, s2, suffix);
2539         if (kctl1 && kctl2) {
2540                 set_ctl_name(kctl1->id.name, s2, suffix);
2541                 set_ctl_name(kctl2->id.name, s1, suffix);
2542                 return 0;
2543         }
2544         return -ENOENT;
2545 }
2546
2547 #if 1
2548 /* bind hp and master controls instead of using only hp control */
2549 static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2550 {
2551         int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2552         if (err > 0) {
2553                 unsigned long priv_saved = kcontrol->private_value;
2554                 kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE;
2555                 snd_ac97_put_volsw(kcontrol, ucontrol);
2556                 kcontrol->private_value = priv_saved;
2557         }
2558         return err;
2559 }
2560
2561 /* ac97 tune: bind Master and Headphone controls */
2562 static int tune_hp_only(struct snd_ac97 *ac97)
2563 {
2564         struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2565         struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
2566         if (! msw || ! mvol)
2567                 return -ENOENT;
2568         msw->put = bind_hp_volsw_put;
2569         mvol->put = bind_hp_volsw_put;
2570         snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2571         snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2572         return 0;
2573 }
2574
2575 #else
2576 /* ac97 tune: use Headphone control as master */
2577 static int tune_hp_only(struct snd_ac97 *ac97)
2578 {
2579         if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2580                 return -ENOENT;
2581         snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
2582         snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
2583         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2584         return 0;
2585 }
2586 #endif
2587
2588 /* ac97 tune: swap Headphone and Master controls */
2589 static int tune_swap_hp(struct snd_ac97 *ac97)
2590 {
2591         if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2592                 return -ENOENT;
2593         snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
2594         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2595         return 0;
2596 }
2597
2598 /* ac97 tune: swap Surround and Master controls */
2599 static int tune_swap_surround(struct snd_ac97 *ac97)
2600 {
2601         if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
2602             snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
2603                 return -ENOENT;
2604         return 0;
2605 }
2606
2607 /* ac97 tune: set up mic sharing for AD codecs */
2608 static int tune_ad_sharing(struct snd_ac97 *ac97)
2609 {
2610         unsigned short scfg;
2611         if ((ac97->id & 0xffffff00) != 0x41445300) {
2612                 snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
2613                 return -EINVAL;
2614         }
2615         /* Turn on OMS bit to route microphone to back panel */
2616         scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
2617         snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
2618         return 0;
2619 }
2620
2621 static const struct snd_kcontrol_new snd_ac97_alc_jack_detect = 
2622 AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
2623
2624 /* ac97 tune: set up ALC jack-select */
2625 static int tune_alc_jack(struct snd_ac97 *ac97)
2626 {
2627         if ((ac97->id & 0xffffff00) != 0x414c4700) {
2628                 snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
2629                 return -EINVAL;
2630         }
2631         snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
2632         snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
2633         if (ac97->id == AC97_ID_ALC658D)
2634                 snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
2635         return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
2636 }
2637
2638 /* ac97 tune: inversed EAPD bit */
2639 static int tune_inv_eapd(struct snd_ac97 *ac97)
2640 {
2641         struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL);
2642         if (! kctl)
2643                 return -ENOENT;
2644         set_inv_eapd(ac97, kctl);
2645         return 0;
2646 }
2647
2648 static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2649 {
2650         int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2651         if (err > 0) {
2652                 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2653                 int shift = (kcontrol->private_value >> 8) & 0x0f;
2654                 int rshift = (kcontrol->private_value >> 12) & 0x0f;
2655                 unsigned short mask;
2656                 if (shift != rshift)
2657                         mask = 0x8080;
2658                 else
2659                         mask = 0x8000;
2660                 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
2661                                      (ac97->regs[AC97_MASTER] & mask) == mask ?
2662                                      0x8000 : 0);
2663         }
2664         return err;
2665 }
2666
2667 /* ac97 tune: EAPD controls mute LED bound with the master mute */
2668 static int tune_mute_led(struct snd_ac97 *ac97)
2669 {
2670         struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2671         if (! msw)
2672                 return -ENOENT;
2673         msw->put = master_mute_sw_put;
2674         snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2675         snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
2676         ac97->scaps |= AC97_SCAP_EAPD_LED;
2677         return 0;
2678 }
2679
2680 static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol,
2681                                  struct snd_ctl_elem_value *ucontrol)
2682 {
2683         int err = bind_hp_volsw_put(kcontrol, ucontrol);
2684         if (err > 0) {
2685                 struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2686                 int shift = (kcontrol->private_value >> 8) & 0x0f;
2687                 int rshift = (kcontrol->private_value >> 12) & 0x0f;
2688                 unsigned short mask;
2689                 if (shift != rshift)
2690                         mask = 0x8080;
2691                 else
2692                         mask = 0x8000;
2693                 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
2694                                      (ac97->regs[AC97_MASTER] & mask) == mask ?
2695                                      0x8000 : 0);
2696         }
2697         return err;
2698 }
2699
2700 static int tune_hp_mute_led(struct snd_ac97 *ac97)
2701 {
2702         struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2703         struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
2704         if (! msw || ! mvol)
2705                 return -ENOENT;
2706         msw->put = hp_master_mute_sw_put;
2707         mvol->put = bind_hp_volsw_put;
2708         snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2709         snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2710         snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2711         snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
2712         return 0;
2713 }
2714
2715 struct quirk_table {
2716         const char *name;
2717         int (*func)(struct snd_ac97 *);
2718 };
2719
2720 static struct quirk_table applicable_quirks[] = {
2721         { "none", NULL },
2722         { "hp_only", tune_hp_only },
2723         { "swap_hp", tune_swap_hp },
2724         { "swap_surround", tune_swap_surround },
2725         { "ad_sharing", tune_ad_sharing },
2726         { "alc_jack", tune_alc_jack },
2727         { "inv_eapd", tune_inv_eapd },
2728         { "mute_led", tune_mute_led },
2729         { "hp_mute_led", tune_hp_mute_led },
2730 };
2731
2732 /* apply the quirk with the given type */
2733 static int apply_quirk(struct snd_ac97 *ac97, int type)
2734 {
2735         if (type <= 0)
2736                 return 0;
2737         else if (type >= ARRAY_SIZE(applicable_quirks))
2738                 return -EINVAL;
2739         if (applicable_quirks[type].func)
2740                 return applicable_quirks[type].func(ac97);
2741         return 0;
2742 }
2743
2744 /* apply the quirk with the given name */
2745 static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr)
2746 {
2747         int i;
2748         struct quirk_table *q;
2749
2750         for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
2751                 q = &applicable_quirks[i];
2752                 if (q->name && ! strcmp(typestr, q->name))
2753                         return apply_quirk(ac97, i);
2754         }
2755         /* for compatibility, accept the numbers, too */
2756         if (*typestr >= '0' && *typestr <= '9')
2757                 return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10));
2758         return -EINVAL;
2759 }
2760
2761 /**
2762  * snd_ac97_tune_hardware - tune up the hardware
2763  * @ac97: the ac97 instance
2764  * @quirk: quirk list
2765  * @override: explicit quirk value (overrides the list if non-NULL)
2766  *
2767  * Do some workaround for each pci device, such as renaming of the
2768  * headphone (true line-out) control as "Master".
2769  * The quirk-list must be terminated with a zero-filled entry.
2770  *
2771  * Returns zero if successful, or a negative error code on failure.
2772  */
2773
2774 int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override)
2775 {
2776         int result;
2777
2778         /* quirk overriden? */
2779         if (override && strcmp(override, "-1") && strcmp(override, "default")) {
2780                 result = apply_quirk_str(ac97, override);
2781                 if (result < 0)
2782                         snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result);
2783                 return result;
2784         }
2785
2786         if (! quirk)
2787                 return -EINVAL;
2788
2789         for (; quirk->subvendor; quirk++) {
2790                 if (quirk->subvendor != ac97->subsystem_vendor)
2791                         continue;
2792                 if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) ||
2793                     quirk->subdevice == (quirk->mask & ac97->subsystem_device)) {
2794                         if (quirk->codec_id && quirk->codec_id != ac97->id)
2795                                 continue;
2796                         snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
2797                         result = apply_quirk(ac97, quirk->type);
2798                         if (result < 0)
2799                                 snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
2800                         return result;
2801                 }
2802         }
2803         return 0;
2804 }
2805
2806 EXPORT_SYMBOL(snd_ac97_tune_hardware);
2807
2808 /*
2809  *  INIT part
2810  */
2811
2812 static int __init alsa_ac97_init(void)
2813 {
2814         return 0;
2815 }
2816
2817 static void __exit alsa_ac97_exit(void)
2818 {
2819 }
2820
2821 module_init(alsa_ac97_init)
2822 module_exit(alsa_ac97_exit)