[ALSA] snd-emu10k1: Add some comments regarding chip types.
[powerpc.git] / sound / pci / emu10k1 / emu10k1_main.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *                   Creative Labs, Inc.
4  *  Routines for control of EMU10K1 chips
5  *
6  *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
7  *      Added support for Audigy 2 Value.
8  *
9  *
10  *  BUGS:
11  *    --
12  *
13  *  TODO:
14  *    --
15  *
16  *   This program is free software; you can redistribute it and/or modify
17  *   it under the terms of the GNU General Public License as published by
18  *   the Free Software Foundation; either version 2 of the License, or
19  *   (at your option) any later version.
20  *
21  *   This program is distributed in the hope that it will be useful,
22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   GNU General Public License for more details.
25  *
26  *   You should have received a copy of the GNU General Public License
27  *   along with this program; if not, write to the Free Software
28  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
29  *
30  */
31
32 #include <sound/driver.h>
33 #include <linux/delay.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/pci.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39
40 #include <sound/core.h>
41 #include <sound/emu10k1.h>
42 #include "p16v.h"
43 #include "tina2.h"
44
45
46 /*************************************************************************
47  * EMU10K1 init / done
48  *************************************************************************/
49
50 void snd_emu10k1_voice_init(struct snd_emu10k1 * emu, int ch)
51 {
52         snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
53         snd_emu10k1_ptr_write(emu, IP, ch, 0);
54         snd_emu10k1_ptr_write(emu, VTFT, ch, 0xffff);
55         snd_emu10k1_ptr_write(emu, CVCF, ch, 0xffff);
56         snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
57         snd_emu10k1_ptr_write(emu, CPF, ch, 0);
58         snd_emu10k1_ptr_write(emu, CCR, ch, 0);
59
60         snd_emu10k1_ptr_write(emu, PSST, ch, 0);
61         snd_emu10k1_ptr_write(emu, DSL, ch, 0x10);
62         snd_emu10k1_ptr_write(emu, CCCA, ch, 0);
63         snd_emu10k1_ptr_write(emu, Z1, ch, 0);
64         snd_emu10k1_ptr_write(emu, Z2, ch, 0);
65         snd_emu10k1_ptr_write(emu, FXRT, ch, 0x32100000);
66
67         snd_emu10k1_ptr_write(emu, ATKHLDM, ch, 0);
68         snd_emu10k1_ptr_write(emu, DCYSUSM, ch, 0);
69         snd_emu10k1_ptr_write(emu, IFATN, ch, 0xffff);
70         snd_emu10k1_ptr_write(emu, PEFE, ch, 0);
71         snd_emu10k1_ptr_write(emu, FMMOD, ch, 0);
72         snd_emu10k1_ptr_write(emu, TREMFRQ, ch, 24);    /* 1 Hz */
73         snd_emu10k1_ptr_write(emu, FM2FRQ2, ch, 24);    /* 1 Hz */
74         snd_emu10k1_ptr_write(emu, TEMPENV, ch, 0);
75
76         /*** these are last so OFF prevents writing ***/
77         snd_emu10k1_ptr_write(emu, LFOVAL2, ch, 0);
78         snd_emu10k1_ptr_write(emu, LFOVAL1, ch, 0);
79         snd_emu10k1_ptr_write(emu, ATKHLDV, ch, 0);
80         snd_emu10k1_ptr_write(emu, ENVVOL, ch, 0);
81         snd_emu10k1_ptr_write(emu, ENVVAL, ch, 0);
82
83         /* Audigy extra stuffs */
84         if (emu->audigy) {
85                 snd_emu10k1_ptr_write(emu, 0x4c, ch, 0); /* ?? */
86                 snd_emu10k1_ptr_write(emu, 0x4d, ch, 0); /* ?? */
87                 snd_emu10k1_ptr_write(emu, 0x4e, ch, 0); /* ?? */
88                 snd_emu10k1_ptr_write(emu, 0x4f, ch, 0); /* ?? */
89                 snd_emu10k1_ptr_write(emu, A_FXRT1, ch, 0x03020100);
90                 snd_emu10k1_ptr_write(emu, A_FXRT2, ch, 0x3f3f3f3f);
91                 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, ch, 0);
92         }
93 }
94
95 static unsigned int spi_dac_init[] = {
96                 0x00ff,
97                 0x02ff,
98                 0x0400,
99                 0x0520,
100                 0x0600,
101                 0x08ff,
102                 0x0aff,
103                 0x0cff,
104                 0x0eff,
105                 0x10ff,
106                 0x1200,
107                 0x1400,
108                 0x1480,
109                 0x1800,
110                 0x1aff,
111                 0x1cff,
112                 0x1e00,
113                 0x0530,
114                 0x0602,
115                 0x0622,
116                 0x1400,
117 };
118         
119 static int snd_emu10k1_init(struct snd_emu10k1 *emu, int enable_ir, int resume)
120 {
121         unsigned int silent_page;
122         int ch;
123
124         /* disable audio and lock cache */
125         outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE,
126              emu->port + HCFG);
127
128         /* reset recording buffers */
129         snd_emu10k1_ptr_write(emu, MICBS, 0, ADCBS_BUFSIZE_NONE);
130         snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
131         snd_emu10k1_ptr_write(emu, FXBS, 0, ADCBS_BUFSIZE_NONE);
132         snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
133         snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
134         snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
135
136         /* disable channel interrupt */
137         outl(0, emu->port + INTE);
138         snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
139         snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
140         snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
141         snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
142
143         if (emu->audigy){
144                 /* set SPDIF bypass mode */
145                 snd_emu10k1_ptr_write(emu, SPBYPASS, 0, SPBYPASS_FORMAT);
146                 /* enable rear left + rear right AC97 slots */
147                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_REAR_RIGHT |
148                                       AC97SLOT_REAR_LEFT);
149         }
150
151         /* init envelope engine */
152         for (ch = 0; ch < NUM_G; ch++)
153                 snd_emu10k1_voice_init(emu, ch);
154
155         snd_emu10k1_ptr_write(emu, SPCS0, 0, emu->spdif_bits[0]);
156         snd_emu10k1_ptr_write(emu, SPCS1, 0, emu->spdif_bits[1]);
157         snd_emu10k1_ptr_write(emu, SPCS2, 0, emu->spdif_bits[2]);
158
159         if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
160                 /* Hacks for Alice3 to work independent of haP16V driver */
161                 u32 tmp;
162
163                 //Setup SRCMulti_I2S SamplingRate
164                 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
165                 tmp &= 0xfffff1ff;
166                 tmp |= (0x2<<9);
167                 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
168                 
169                 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
170                 snd_emu10k1_ptr20_write(emu, SRCSel, 0, 0x14);
171                 /* Setup SRCMulti Input Audio Enable */
172                 /* Use 0xFFFFFFFF to enable P16V sounds. */
173                 snd_emu10k1_ptr20_write(emu, SRCMULTI_ENABLE, 0, 0xFFFFFFFF);
174
175                 /* Enabled Phased (8-channel) P16V playback */
176                 outl(0x0201, emu->port + HCFG2);
177                 /* Set playback routing. */
178                 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, 0x78e4);
179         }
180         if (emu->card_capabilities->ca0108_chip) { /* audigy2 Value */
181                 /* Hacks for Alice3 to work independent of haP16V driver */
182                 u32 tmp;
183
184                 snd_printk(KERN_INFO "Audigy2 value: Special config.\n");
185                 //Setup SRCMulti_I2S SamplingRate
186                 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
187                 tmp &= 0xfffff1ff;
188                 tmp |= (0x2<<9);
189                 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
190
191                 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
192                 outl(0x600000, emu->port + 0x20);
193                 outl(0x14, emu->port + 0x24);
194
195                 /* Setup SRCMulti Input Audio Enable */
196                 outl(0x7b0000, emu->port + 0x20);
197                 outl(0xFF000000, emu->port + 0x24);
198
199                 /* Setup SPDIF Out Audio Enable */
200                 /* The Audigy 2 Value has a separate SPDIF out,
201                  * so no need for a mixer switch
202                  */
203                 outl(0x7a0000, emu->port + 0x20);
204                 outl(0xFF000000, emu->port + 0x24);
205                 tmp = inl(emu->port + A_IOCFG) & ~0x8; /* Clear bit 3 */
206                 outl(tmp, emu->port + A_IOCFG);
207         }
208         if (emu->card_capabilities->spi_dac) { /* Audigy 2 ZS Notebook with DAC Wolfson WM8768/WM8568 */
209                 int size, n;
210
211                 size = ARRAY_SIZE(spi_dac_init);
212                 for (n=0; n < size; n++)
213                         snd_emu10k1_spi_write(emu, spi_dac_init[n]);
214
215                 snd_emu10k1_ptr20_write(emu, 0x60, 0, 0x10);
216                 /* Enable GPIOs
217                  * GPIO0: Unknown
218                  * GPIO1: Speakers-enabled.
219                  * GPIO2: Unknown
220                  * GPIO3: Unknown
221                  * GPIO4: IEC958 Output on.
222                  * GPIO5: Unknown
223                  * GPIO6: Unknown
224                  * GPIO7: Unknown
225                  */
226                 outl(0x76, emu->port + A_IOCFG); /* Windows uses 0x3f76 */
227
228         }
229         
230         snd_emu10k1_ptr_write(emu, PTB, 0, emu->ptb_pages.addr);
231         snd_emu10k1_ptr_write(emu, TCB, 0, 0);  /* taken from original driver */
232         snd_emu10k1_ptr_write(emu, TCBS, 0, 4); /* taken from original driver */
233
234         silent_page = (emu->silent_page.addr << 1) | MAP_PTI_MASK;
235         for (ch = 0; ch < NUM_G; ch++) {
236                 snd_emu10k1_ptr_write(emu, MAPA, ch, silent_page);
237                 snd_emu10k1_ptr_write(emu, MAPB, ch, silent_page);
238         }
239
240         /*
241          *  Hokay, setup HCFG
242          *   Mute Disable Audio = 0
243          *   Lock Tank Memory = 1
244          *   Lock Sound Memory = 0
245          *   Auto Mute = 1
246          */
247         if (emu->audigy) {
248                 if (emu->revision == 4) /* audigy2 */
249                         outl(HCFG_AUDIOENABLE |
250                              HCFG_AC3ENABLE_CDSPDIF |
251                              HCFG_AC3ENABLE_GPSPDIF |
252                              HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
253                 else
254                         outl(HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
255         /* FIXME: Remove all these emu->model and replace it with a card recognition parameter,
256          * e.g. card_capabilities->joystick */
257         } else if (emu->model == 0x20 ||
258             emu->model == 0xc400 ||
259             (emu->model == 0x21 && emu->revision < 6))
260                 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE, emu->port + HCFG);
261         else
262                 // With on-chip joystick
263                 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
264
265         if (enable_ir) {        /* enable IR for SB Live */
266                 if ( emu->card_capabilities->emu1212m) {
267                         ;  /* Disable all access to A_IOCFG for the emu1212m */
268                 } else if (emu->audigy) {
269                         unsigned int reg = inl(emu->port + A_IOCFG);
270                         outl(reg | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
271                         udelay(500);
272                         outl(reg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
273                         udelay(100);
274                         outl(reg, emu->port + A_IOCFG);
275                 } else {
276                         unsigned int reg = inl(emu->port + HCFG);
277                         outl(reg | HCFG_GPOUT2, emu->port + HCFG);
278                         udelay(500);
279                         outl(reg | HCFG_GPOUT1 | HCFG_GPOUT2, emu->port + HCFG);
280                         udelay(100);
281                         outl(reg, emu->port + HCFG);
282                 }
283         }
284         
285         if ( emu->card_capabilities->emu1212m) {
286                 ;  /* Disable all access to A_IOCFG for the emu1212m */
287         } else if (emu->audigy) {       /* enable analog output */
288                 unsigned int reg = inl(emu->port + A_IOCFG);
289                 outl(reg | A_IOCFG_GPOUT0, emu->port + A_IOCFG);
290         }
291
292         return 0;
293 }
294
295 static void snd_emu10k1_audio_enable(struct snd_emu10k1 *emu)
296 {
297         /*
298          *  Enable the audio bit
299          */
300         outl(inl(emu->port + HCFG) | HCFG_AUDIOENABLE, emu->port + HCFG);
301
302         /* Enable analog/digital outs on audigy */
303         if ( emu->card_capabilities->emu1212m) {
304                 ;  /* Disable all access to A_IOCFG for the emu1212m */
305         } else if (emu->audigy) {
306                 outl(inl(emu->port + A_IOCFG) & ~0x44, emu->port + A_IOCFG);
307  
308                 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
309                         /* Unmute Analog now.  Set GPO6 to 1 for Apollo.
310                          * This has to be done after init ALice3 I2SOut beyond 48KHz.
311                          * So, sequence is important. */
312                         outl(inl(emu->port + A_IOCFG) | 0x0040, emu->port + A_IOCFG);
313                 } else if (emu->card_capabilities->ca0108_chip) { /* audigy2 value */
314                         /* Unmute Analog now. */
315                         outl(inl(emu->port + A_IOCFG) | 0x0060, emu->port + A_IOCFG);
316                 } else {
317                         /* Disable routing from AC97 line out to Front speakers */
318                         outl(inl(emu->port + A_IOCFG) | 0x0080, emu->port + A_IOCFG);
319                 }
320         }
321         
322 #if 0
323         {
324         unsigned int tmp;
325         /* FIXME: the following routine disables LiveDrive-II !! */
326         // TOSLink detection
327         emu->tos_link = 0;
328         tmp = inl(emu->port + HCFG);
329         if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
330                 outl(tmp|0x800, emu->port + HCFG);
331                 udelay(50);
332                 if (tmp != (inl(emu->port + HCFG) & ~0x800)) {
333                         emu->tos_link = 1;
334                         outl(tmp, emu->port + HCFG);
335                 }
336         }
337         }
338 #endif
339
340         snd_emu10k1_intr_enable(emu, INTE_PCIERRORENABLE);
341 }
342
343 int snd_emu10k1_done(struct snd_emu10k1 * emu)
344 {
345         int ch;
346
347         outl(0, emu->port + INTE);
348
349         /*
350          *  Shutdown the chip
351          */
352         for (ch = 0; ch < NUM_G; ch++)
353                 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
354         for (ch = 0; ch < NUM_G; ch++) {
355                 snd_emu10k1_ptr_write(emu, VTFT, ch, 0);
356                 snd_emu10k1_ptr_write(emu, CVCF, ch, 0);
357                 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
358                 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
359         }
360
361         /* reset recording buffers */
362         snd_emu10k1_ptr_write(emu, MICBS, 0, 0);
363         snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
364         snd_emu10k1_ptr_write(emu, FXBS, 0, 0);
365         snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
366         snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
367         snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
368         snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
369         snd_emu10k1_ptr_write(emu, TCBS, 0, TCBS_BUFFSIZE_16K);
370         snd_emu10k1_ptr_write(emu, TCB, 0, 0);
371         if (emu->audigy)
372                 snd_emu10k1_ptr_write(emu, A_DBG, 0, A_DBG_SINGLE_STEP);
373         else
374                 snd_emu10k1_ptr_write(emu, DBG, 0, EMU10K1_DBG_SINGLE_STEP);
375
376         /* disable channel interrupt */
377         snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
378         snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
379         snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
380         snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
381
382         /* disable audio and lock cache */
383         outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
384         snd_emu10k1_ptr_write(emu, PTB, 0, 0);
385
386         return 0;
387 }
388
389 /*************************************************************************
390  * ECARD functional implementation
391  *************************************************************************/
392
393 /* In A1 Silicon, these bits are in the HC register */
394 #define HOOKN_BIT               (1L << 12)
395 #define HANDN_BIT               (1L << 11)
396 #define PULSEN_BIT              (1L << 10)
397
398 #define EC_GDI1                 (1 << 13)
399 #define EC_GDI0                 (1 << 14)
400
401 #define EC_NUM_CONTROL_BITS     20
402
403 #define EC_AC3_DATA_SELN        0x0001L
404 #define EC_EE_DATA_SEL          0x0002L
405 #define EC_EE_CNTRL_SELN        0x0004L
406 #define EC_EECLK                0x0008L
407 #define EC_EECS                 0x0010L
408 #define EC_EESDO                0x0020L
409 #define EC_TRIM_CSN             0x0040L
410 #define EC_TRIM_SCLK            0x0080L
411 #define EC_TRIM_SDATA           0x0100L
412 #define EC_TRIM_MUTEN           0x0200L
413 #define EC_ADCCAL               0x0400L
414 #define EC_ADCRSTN              0x0800L
415 #define EC_DACCAL               0x1000L
416 #define EC_DACMUTEN             0x2000L
417 #define EC_LEDN                 0x4000L
418
419 #define EC_SPDIF0_SEL_SHIFT     15
420 #define EC_SPDIF1_SEL_SHIFT     17
421 #define EC_SPDIF0_SEL_MASK      (0x3L << EC_SPDIF0_SEL_SHIFT)
422 #define EC_SPDIF1_SEL_MASK      (0x7L << EC_SPDIF1_SEL_SHIFT)
423 #define EC_SPDIF0_SELECT(_x)    (((_x) << EC_SPDIF0_SEL_SHIFT) & EC_SPDIF0_SEL_MASK)
424 #define EC_SPDIF1_SELECT(_x)    (((_x) << EC_SPDIF1_SEL_SHIFT) & EC_SPDIF1_SEL_MASK)
425 #define EC_CURRENT_PROM_VERSION 0x01    /* Self-explanatory.  This should
426                                          * be incremented any time the EEPROM's
427                                          * format is changed.  */
428
429 #define EC_EEPROM_SIZE          0x40    /* ECARD EEPROM has 64 16-bit words */
430
431 /* Addresses for special values stored in to EEPROM */
432 #define EC_PROM_VERSION_ADDR    0x20    /* Address of the current prom version */
433 #define EC_BOARDREV0_ADDR       0x21    /* LSW of board rev */
434 #define EC_BOARDREV1_ADDR       0x22    /* MSW of board rev */
435
436 #define EC_LAST_PROMFILE_ADDR   0x2f
437
438 #define EC_SERIALNUM_ADDR       0x30    /* First word of serial number.  The 
439                                          * can be up to 30 characters in length
440                                          * and is stored as a NULL-terminated
441                                          * ASCII string.  Any unused bytes must be
442                                          * filled with zeros */
443 #define EC_CHECKSUM_ADDR        0x3f    /* Location at which checksum is stored */
444
445
446 /* Most of this stuff is pretty self-evident.  According to the hardware 
447  * dudes, we need to leave the ADCCAL bit low in order to avoid a DC 
448  * offset problem.  Weird.
449  */
450 #define EC_RAW_RUN_MODE         (EC_DACMUTEN | EC_ADCRSTN | EC_TRIM_MUTEN | \
451                                  EC_TRIM_CSN)
452
453
454 #define EC_DEFAULT_ADC_GAIN     0xC4C4
455 #define EC_DEFAULT_SPDIF0_SEL   0x0
456 #define EC_DEFAULT_SPDIF1_SEL   0x4
457
458 /**************************************************************************
459  * @func Clock bits into the Ecard's control latch.  The Ecard uses a
460  *  control latch will is loaded bit-serially by toggling the Modem control
461  *  lines from function 2 on the E8010.  This function hides these details
462  *  and presents the illusion that we are actually writing to a distinct
463  *  register.
464  */
465
466 static void snd_emu10k1_ecard_write(struct snd_emu10k1 * emu, unsigned int value)
467 {
468         unsigned short count;
469         unsigned int data;
470         unsigned long hc_port;
471         unsigned int hc_value;
472
473         hc_port = emu->port + HCFG;
474         hc_value = inl(hc_port) & ~(HOOKN_BIT | HANDN_BIT | PULSEN_BIT);
475         outl(hc_value, hc_port);
476
477         for (count = 0; count < EC_NUM_CONTROL_BITS; count++) {
478
479                 /* Set up the value */
480                 data = ((value & 0x1) ? PULSEN_BIT : 0);
481                 value >>= 1;
482
483                 outl(hc_value | data, hc_port);
484
485                 /* Clock the shift register */
486                 outl(hc_value | data | HANDN_BIT, hc_port);
487                 outl(hc_value | data, hc_port);
488         }
489
490         /* Latch the bits */
491         outl(hc_value | HOOKN_BIT, hc_port);
492         outl(hc_value, hc_port);
493 }
494
495 /**************************************************************************
496  * @func Set the gain of the ECARD's CS3310 Trim/gain controller.  The
497  * trim value consists of a 16bit value which is composed of two
498  * 8 bit gain/trim values, one for the left channel and one for the
499  * right channel.  The following table maps from the Gain/Attenuation
500  * value in decibels into the corresponding bit pattern for a single
501  * channel.
502  */
503
504 static void snd_emu10k1_ecard_setadcgain(struct snd_emu10k1 * emu,
505                                          unsigned short gain)
506 {
507         unsigned int bit;
508
509         /* Enable writing to the TRIM registers */
510         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
511
512         /* Do it again to insure that we meet hold time requirements */
513         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
514
515         for (bit = (1 << 15); bit; bit >>= 1) {
516                 unsigned int value;
517                 
518                 value = emu->ecard_ctrl & ~(EC_TRIM_CSN | EC_TRIM_SDATA);
519
520                 if (gain & bit)
521                         value |= EC_TRIM_SDATA;
522
523                 /* Clock the bit */
524                 snd_emu10k1_ecard_write(emu, value);
525                 snd_emu10k1_ecard_write(emu, value | EC_TRIM_SCLK);
526                 snd_emu10k1_ecard_write(emu, value);
527         }
528
529         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
530 }
531
532 static int __devinit snd_emu10k1_ecard_init(struct snd_emu10k1 * emu)
533 {
534         unsigned int hc_value;
535
536         /* Set up the initial settings */
537         emu->ecard_ctrl = EC_RAW_RUN_MODE |
538                           EC_SPDIF0_SELECT(EC_DEFAULT_SPDIF0_SEL) |
539                           EC_SPDIF1_SELECT(EC_DEFAULT_SPDIF1_SEL);
540
541         /* Step 0: Set the codec type in the hardware control register 
542          * and enable audio output */
543         hc_value = inl(emu->port + HCFG);
544         outl(hc_value | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S, emu->port + HCFG);
545         inl(emu->port + HCFG);
546
547         /* Step 1: Turn off the led and deassert TRIM_CS */
548         snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
549
550         /* Step 2: Calibrate the ADC and DAC */
551         snd_emu10k1_ecard_write(emu, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
552
553         /* Step 3: Wait for awhile;   XXX We can't get away with this
554          * under a real operating system; we'll need to block and wait that
555          * way. */
556         snd_emu10k1_wait(emu, 48000);
557
558         /* Step 4: Switch off the DAC and ADC calibration.  Note
559          * That ADC_CAL is actually an inverted signal, so we assert
560          * it here to stop calibration.  */
561         snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
562
563         /* Step 4: Switch into run mode */
564         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
565
566         /* Step 5: Set the analog input gain */
567         snd_emu10k1_ecard_setadcgain(emu, EC_DEFAULT_ADC_GAIN);
568
569         return 0;
570 }
571
572 static int __devinit snd_emu10k1_cardbus_init(struct snd_emu10k1 * emu)
573 {
574         unsigned long special_port;
575         unsigned int value;
576
577         /* Special initialisation routine
578          * before the rest of the IO-Ports become active.
579          */
580         special_port = emu->port + 0x38;
581         value = inl(special_port);
582         outl(0x00d00000, special_port);
583         value = inl(special_port);
584         outl(0x00d00001, special_port);
585         value = inl(special_port);
586         outl(0x00d0005f, special_port);
587         value = inl(special_port);
588         outl(0x00d0007f, special_port);
589         value = inl(special_port);
590         outl(0x0090007f, special_port);
591         value = inl(special_port);
592
593         snd_emu10k1_ptr20_write(emu, TINA2_VOLUME, 0, 0xfefefefe); /* Defaults to 0x30303030 */
594         return 0;
595 }
596
597 static int snd_emu1212m_fpga_write(struct snd_emu10k1 * emu, int reg, int value)
598 {
599         if (reg<0 || reg>0x3f)
600                 return 1;
601         reg+=0x40; /* 0x40 upwards are registers. */
602         if (value<0 || value>0x3f) /* 0 to 0x3f are values */
603                 return 1;
604         outl(reg, emu->port + A_IOCFG);
605         outl(reg | 0x80, emu->port + A_IOCFG);  /* High bit clocks the value into the fpga. */
606         outl(value, emu->port + A_IOCFG);
607         outl(value | 0x80 , emu->port + A_IOCFG);  /* High bit clocks the value into the fpga. */
608
609         return 0;
610 }
611
612 static int snd_emu1212m_fpga_read(struct snd_emu10k1 * emu, int reg, int *value)
613 {
614         if (reg<0 || reg>0x3f)
615                 return 1;
616         reg+=0x40; /* 0x40 upwards are registers. */
617         outl(reg, emu->port + A_IOCFG);
618         outl(reg | 0x80, emu->port + A_IOCFG);  /* High bit clocks the value into the fpga. */
619         *value = inl(emu->port + A_IOCFG);
620
621         return 0;
622 }
623
624 static int snd_emu1212m_fpga_netlist_write(struct snd_emu10k1 * emu, int reg, int value)
625 {
626         snd_emu1212m_fpga_write(emu, 0x00, ((reg >> 8) & 0x3f) );
627         snd_emu1212m_fpga_write(emu, 0x01, (reg & 0x3f) );
628         snd_emu1212m_fpga_write(emu, 0x02, ((value >> 8) & 0x3f) );
629         snd_emu1212m_fpga_write(emu, 0x03, (value & 0x3f) );
630
631         return 0;
632 }
633
634 static int __devinit snd_emu10k1_emu1212m_init(struct snd_emu10k1 * emu)
635 {
636         unsigned int i;
637         int tmp;
638
639         snd_printk(KERN_ERR "emu1212m: Special config.\n");
640         outl(0x0005a00c, emu->port + HCFG);
641         outl(0x0005a004, emu->port + HCFG);
642         outl(0x0005a000, emu->port + HCFG);
643         outl(0x0005a000, emu->port + HCFG);
644
645         snd_emu1212m_fpga_read(emu, 0x22, &tmp );
646         snd_emu1212m_fpga_read(emu, 0x23, &tmp );
647         snd_emu1212m_fpga_read(emu, 0x24, &tmp );
648         snd_emu1212m_fpga_write(emu, 0x04, 0x01 );
649         snd_emu1212m_fpga_read(emu, 0x0b, &tmp );
650         snd_emu1212m_fpga_write(emu, 0x0b, 0x01 );
651         snd_emu1212m_fpga_read(emu, 0x10, &tmp );
652         snd_emu1212m_fpga_write(emu, 0x10, 0x00 );
653         snd_emu1212m_fpga_read(emu, 0x11, &tmp );
654         snd_emu1212m_fpga_write(emu, 0x11, 0x30 );
655         snd_emu1212m_fpga_read(emu, 0x13, &tmp );
656         snd_emu1212m_fpga_write(emu, 0x13, 0x0f );
657         snd_emu1212m_fpga_read(emu, 0x11, &tmp );
658         snd_emu1212m_fpga_write(emu, 0x11, 0x30 );
659         snd_emu1212m_fpga_read(emu, 0x0a, &tmp );
660         snd_emu1212m_fpga_write(emu, 0x0a, 0x10 );
661         snd_emu1212m_fpga_write(emu, 0x0c, 0x19 );
662         snd_emu1212m_fpga_write(emu, 0x12, 0x0c );
663         snd_emu1212m_fpga_write(emu, 0x09, 0x0f );
664         snd_emu1212m_fpga_write(emu, 0x06, 0x00 );
665         snd_emu1212m_fpga_write(emu, 0x05, 0x00 );
666         snd_emu1212m_fpga_write(emu, 0x0e, 0x12 );
667         snd_emu1212m_fpga_netlist_write(emu, 0x0000, 0x0200);
668         snd_emu1212m_fpga_netlist_write(emu, 0x0001, 0x0201);
669         snd_emu1212m_fpga_netlist_write(emu, 0x0002, 0x0500);
670         snd_emu1212m_fpga_netlist_write(emu, 0x0003, 0x0501);
671         snd_emu1212m_fpga_netlist_write(emu, 0x0004, 0x0400);
672         snd_emu1212m_fpga_netlist_write(emu, 0x0005, 0x0401);
673         snd_emu1212m_fpga_netlist_write(emu, 0x0006, 0x0402);
674         snd_emu1212m_fpga_netlist_write(emu, 0x0007, 0x0403);
675         snd_emu1212m_fpga_netlist_write(emu, 0x0008, 0x0404);
676         snd_emu1212m_fpga_netlist_write(emu, 0x0009, 0x0405);
677         snd_emu1212m_fpga_netlist_write(emu, 0x000a, 0x0406);
678         snd_emu1212m_fpga_netlist_write(emu, 0x000b, 0x0407);
679         snd_emu1212m_fpga_netlist_write(emu, 0x000c, 0x0100);
680         snd_emu1212m_fpga_netlist_write(emu, 0x000d, 0x0104);
681         snd_emu1212m_fpga_netlist_write(emu, 0x000e, 0x0200);
682         snd_emu1212m_fpga_netlist_write(emu, 0x000f, 0x0201);
683         for (i=0;i < 0x20;i++) {
684                 snd_emu1212m_fpga_netlist_write(emu, 0x0100+i, 0x0000);
685         }
686         for (i=0;i < 4;i++) {
687                 snd_emu1212m_fpga_netlist_write(emu, 0x0200+i, 0x0000);
688         }
689         for (i=0;i < 7;i++) {
690                 snd_emu1212m_fpga_netlist_write(emu, 0x0300+i, 0x0000);
691         }
692         for (i=0;i < 7;i++) {
693                 snd_emu1212m_fpga_netlist_write(emu, 0x0400+i, 0x0000);
694         }
695         snd_emu1212m_fpga_netlist_write(emu, 0x0500, 0x0108);
696         snd_emu1212m_fpga_netlist_write(emu, 0x0501, 0x010c);
697         snd_emu1212m_fpga_netlist_write(emu, 0x0600, 0x0110);
698         snd_emu1212m_fpga_netlist_write(emu, 0x0601, 0x0114);
699         snd_emu1212m_fpga_netlist_write(emu, 0x0700, 0x0118);
700         snd_emu1212m_fpga_netlist_write(emu, 0x0701, 0x011c);
701         snd_emu1212m_fpga_write(emu, 0x07, 0x01 );
702
703         snd_emu1212m_fpga_read(emu, 0x21, &tmp );
704
705         outl(0x0000a000, emu->port + HCFG);
706         outl(0x0000a001, emu->port + HCFG);
707         /* Initial boot complete. Now patches */
708
709         snd_emu1212m_fpga_read(emu, 0x21, &tmp );
710         snd_emu1212m_fpga_write(emu, 0x0c, 0x19 );
711         snd_emu1212m_fpga_write(emu, 0x12, 0x0c );
712         snd_emu1212m_fpga_write(emu, 0x0c, 0x19 );
713         snd_emu1212m_fpga_write(emu, 0x12, 0x0c );
714         snd_emu1212m_fpga_read(emu, 0x0a, &tmp );
715         snd_emu1212m_fpga_write(emu, 0x0a, 0x10 );
716
717         snd_emu1212m_fpga_read(emu, 0x20, &tmp );
718         snd_emu1212m_fpga_read(emu, 0x21, &tmp );
719
720         snd_emu1212m_fpga_netlist_write(emu, 0x0300, 0x0312);
721         snd_emu1212m_fpga_netlist_write(emu, 0x0301, 0x0313);
722         snd_emu1212m_fpga_netlist_write(emu, 0x0200, 0x0302);
723         snd_emu1212m_fpga_netlist_write(emu, 0x0201, 0x0303);
724
725         return 0;
726 }
727 /*
728  *  Create the EMU10K1 instance
729  */
730
731 #ifdef CONFIG_PM
732 static int alloc_pm_buffer(struct snd_emu10k1 *emu);
733 static void free_pm_buffer(struct snd_emu10k1 *emu);
734 #endif
735
736 static int snd_emu10k1_free(struct snd_emu10k1 *emu)
737 {
738         if (emu->port) {        /* avoid access to already used hardware */
739                 snd_emu10k1_fx8010_tram_setup(emu, 0);
740                 snd_emu10k1_done(emu);
741                 /* remove reserved page */
742                 if (emu->reserved_page) {
743                         snd_emu10k1_synth_free(emu, (struct snd_util_memblk *)emu->reserved_page);
744                         emu->reserved_page = NULL;
745                 }
746                 snd_emu10k1_free_efx(emu);
747         }
748         if (emu->memhdr)
749                 snd_util_memhdr_free(emu->memhdr);
750         if (emu->silent_page.area)
751                 snd_dma_free_pages(&emu->silent_page);
752         if (emu->ptb_pages.area)
753                 snd_dma_free_pages(&emu->ptb_pages);
754         vfree(emu->page_ptr_table);
755         vfree(emu->page_addr_table);
756 #ifdef CONFIG_PM
757         free_pm_buffer(emu);
758 #endif
759         if (emu->irq >= 0)
760                 free_irq(emu->irq, (void *)emu);
761         if (emu->port)
762                 pci_release_regions(emu->pci);
763         if (emu->card_capabilities->ca0151_chip) /* P16V */     
764                 snd_p16v_free(emu);
765         pci_disable_device(emu->pci);
766         kfree(emu);
767         return 0;
768 }
769
770 static int snd_emu10k1_dev_free(struct snd_device *device)
771 {
772         struct snd_emu10k1 *emu = device->device_data;
773         return snd_emu10k1_free(emu);
774 }
775
776 static struct snd_emu_chip_details emu_chip_details[] = {
777         /* Audigy 2 Value AC3 out does not work yet. Need to find out how to turn off interpolators.*/
778         /* Tested by James@superbug.co.uk 3rd July 2005 */
779         /* DSP: CA0108-IAT
780          * DAC: CS4382-KQ
781          * ADC: Philips 1361T
782          * AC97: STAC9750
783          * CA0151: None
784          */
785         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x10011102,
786          .driver = "Audigy2", .name = "Audigy 2 Value [SB0400]", 
787          .id = "Audigy2",
788          .emu10k2_chip = 1,
789          .ca0108_chip = 1,
790          .spk71 = 1,
791          .ac97_chip = 1} ,
792         /* Audigy 2 ZS Notebook Cardbus card.*/
793         /* Tested by James@superbug.co.uk 30th October 2005 */
794         /* Not working yet, but progressing. */
795         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x20011102,
796          .driver = "Audigy2", .name = "Audigy 2 ZS Notebook [SB0530]", 
797          .id = "Audigy2",
798          .emu10k2_chip = 1,
799          .ca0108_chip = 1,
800          .ca_cardbus_chip = 1,
801          .spi_dac = 1,
802          .spk71 = 1} ,
803         {.vendor = 0x1102, .device = 0x0008, 
804          .driver = "Audigy2", .name = "Audigy 2 Value [Unknown]", 
805          .id = "Audigy2",
806          .emu10k2_chip = 1,
807          .ca0108_chip = 1,
808          .ac97_chip = 1} ,
809         /* Tested by James@superbug.co.uk 8th July 2005. No sound available yet. */
810         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x40011102,
811          .driver = "Audigy2", .name = "E-mu 1212m [4001]", 
812          .id = "EMU1212m",
813          .emu10k2_chip = 1,
814          .ca0102_chip = 1,
815          .emu1212m = 1} ,
816         /* Tested by James@superbug.co.uk 3rd July 2005 */
817         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20071102,
818          .driver = "Audigy2", .name = "Audigy 4 PRO [SB0380]", 
819          .id = "Audigy2",
820          .emu10k2_chip = 1,
821          .ca0102_chip = 1,
822          .ca0151_chip = 1,
823          .spk71 = 1,
824          .spdif_bug = 1,
825          .ac97_chip = 1} ,
826         /* Tested by shane-alsa@cm.nu 5th Nov 2005 */
827         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20061102,
828          .driver = "Audigy2", .name = "Audigy 2 [2006]", 
829          .id = "Audigy2",
830          .emu10k2_chip = 1,
831          .ca0102_chip = 1,
832          .ca0151_chip = 1,
833          .spk71 = 1,
834          .spdif_bug = 1,
835          .ac97_chip = 1} ,
836         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20021102,
837          .driver = "Audigy2", .name = "Audigy 2 ZS [SB0350]", 
838          .id = "Audigy2",
839          .emu10k2_chip = 1,
840          .ca0102_chip = 1,
841          .ca0151_chip = 1,
842          .spk71 = 1,
843          .spdif_bug = 1,
844          .ac97_chip = 1} ,
845         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20011102,
846          .driver = "Audigy2", .name = "Audigy 2 ZS [2001]", 
847          .id = "Audigy2",
848          .emu10k2_chip = 1,
849          .ca0102_chip = 1,
850          .ca0151_chip = 1,
851          .spk71 = 1,
852          .spdif_bug = 1,
853          .ac97_chip = 1} ,
854         /* Audigy 2 */
855         /* Tested by James@superbug.co.uk 3rd July 2005 */
856         /* DSP: CA0102-IAT
857          * DAC: CS4382-KQ
858          * ADC: Philips 1361T
859          * AC97: STAC9721
860          * CA0151: Yes
861          */
862         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10071102,
863          .driver = "Audigy2", .name = "Audigy 2 [SB0240]", 
864          .id = "Audigy2",
865          .emu10k2_chip = 1,
866          .ca0102_chip = 1,
867          .ca0151_chip = 1,
868          .spk71 = 1,
869          .spdif_bug = 1,
870          .ac97_chip = 1} ,
871         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10051102,
872          .driver = "Audigy2", .name = "Audigy 2 EX [1005]", 
873          .id = "Audigy2",
874          .emu10k2_chip = 1,
875          .ca0102_chip = 1,
876          .ca0151_chip = 1,
877          .spk71 = 1,
878          .spdif_bug = 1} ,
879         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10021102,
880          .driver = "Audigy2", .name = "Audigy 2 Platinum [SB0240P]", 
881          .id = "Audigy2",
882          .emu10k2_chip = 1,
883          .ca0102_chip = 1,
884          .ca0151_chip = 1,
885          .spk71 = 1,
886          .spdif_bug = 1,
887          .ac97_chip = 1} ,
888         {.vendor = 0x1102, .device = 0x0004, .revision = 0x04,
889          .driver = "Audigy2", .name = "Audigy 2 [Unknown]",
890          .id = "Audigy2",
891          .emu10k2_chip = 1,
892          .ca0102_chip = 1,
893          .ca0151_chip = 1,
894          .spdif_bug = 1,
895          .ac97_chip = 1} ,
896         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00531102,
897          .driver = "Audigy", .name = "Audigy 1 [SB0090]", 
898          .id = "Audigy",
899          .emu10k2_chip = 1,
900          .ca0102_chip = 1,
901          .ac97_chip = 1} ,
902         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00521102,
903          .driver = "Audigy", .name = "Audigy 1 ES [SB0160]", 
904          .id = "Audigy",
905          .emu10k2_chip = 1,
906          .ca0102_chip = 1,
907          .spdif_bug = 1,
908          .ac97_chip = 1} ,
909         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00511102,
910          .driver = "Audigy", .name = "Audigy 1 [SB0090]", 
911          .id = "Audigy",
912          .emu10k2_chip = 1,
913          .ca0102_chip = 1,
914          .ac97_chip = 1} ,
915         {.vendor = 0x1102, .device = 0x0004,
916          .driver = "Audigy", .name = "Audigy 1 [Unknown]", 
917          .id = "Audigy",
918          .emu10k2_chip = 1,
919          .ca0102_chip = 1,
920          .ac97_chip = 1} ,
921         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806B1102,
922          .driver = "EMU10K1", .name = "SBLive! [SB0105]", 
923          .id = "Live",
924          .emu10k1_chip = 1,
925          .ac97_chip = 1,
926          .sblive51 = 1} ,
927         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806A1102,
928          .driver = "EMU10K1", .name = "SBLive! Value [SB0103]", 
929          .id = "Live",
930          .emu10k1_chip = 1,
931          .ac97_chip = 1,
932          .sblive51 = 1} ,
933         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80691102,
934          .driver = "EMU10K1", .name = "SBLive! Value [SB0101]", 
935          .id = "Live",
936          .emu10k1_chip = 1,
937          .ac97_chip = 1,
938          .sblive51 = 1} ,
939         /* Tested by Thomas Zehetbauer 27th Aug 2005 */
940         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80651102,
941          .driver = "EMU10K1", .name = "SB Live 5.1 [SB0220]", 
942          .id = "Live",
943          .emu10k1_chip = 1,
944          .ac97_chip = 1,
945          .sblive51 = 1} ,
946         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x100a1102,
947          .driver = "EMU10K1", .name = "SB Live 5.1 [SB0220]", 
948          .id = "Live",
949          .emu10k1_chip = 1,
950          .ac97_chip = 1,
951          .sblive51 = 1} ,
952         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80641102,
953          .driver = "EMU10K1", .name = "SB Live 5.1", 
954          .id = "Live",
955          .emu10k1_chip = 1,
956          .ac97_chip = 1,
957          .sblive51 = 1} ,
958         /* Tested by alsa bugtrack user "hus" bug #1297 12th Aug 2005 */
959         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80611102,
960          .driver = "EMU10K1", .name = "SBLive 5.1 [SB0060]",
961          .id = "Live",
962          .emu10k1_chip = 1,
963          .ac97_chip = 2, /* ac97 is optional; both SBLive 5.1 and platinum
964                           * share the same IDs!
965                           */
966          .sblive51 = 1} ,
967         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80511102,
968          .driver = "EMU10K1", .name = "SBLive! Value [CT4850]", 
969          .id = "Live",
970          .emu10k1_chip = 1,
971          .ac97_chip = 1,
972          .sblive51 = 1} ,
973         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80401102,
974          .driver = "EMU10K1", .name = "SBLive! Platinum [CT4760P]", 
975          .id = "Live",
976          .emu10k1_chip = 1,
977          .ac97_chip = 1} ,
978         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80321102,
979          .driver = "EMU10K1", .name = "SBLive! Value [CT4871]", 
980          .id = "Live",
981          .emu10k1_chip = 1,
982          .ac97_chip = 1,
983          .sblive51 = 1} ,
984         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80311102,
985          .driver = "EMU10K1", .name = "SBLive! Value [CT4831]", 
986          .id = "Live",
987          .emu10k1_chip = 1,
988          .ac97_chip = 1,
989          .sblive51 = 1} ,
990         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80281102,
991          .driver = "EMU10K1", .name = "SBLive! Value [CT4870]", 
992          .id = "Live",
993          .emu10k1_chip = 1,
994          .ac97_chip = 1,
995          .sblive51 = 1} ,
996         /* Tested by James@superbug.co.uk 3rd July 2005 */
997         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80271102,
998          .driver = "EMU10K1", .name = "SBLive! Value [CT4832]", 
999          .id = "Live",
1000          .emu10k1_chip = 1,
1001          .ac97_chip = 1,
1002          .sblive51 = 1} ,
1003         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80261102,
1004          .driver = "EMU10K1", .name = "SBLive! Value [CT4830]", 
1005          .id = "Live",
1006          .emu10k1_chip = 1,
1007          .ac97_chip = 1,
1008          .sblive51 = 1} ,
1009         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80231102,
1010          .driver = "EMU10K1", .name = "SB PCI512 [CT4790]", 
1011          .id = "Live",
1012          .emu10k1_chip = 1,
1013          .ac97_chip = 1,
1014          .sblive51 = 1} ,
1015         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80221102,
1016          .driver = "EMU10K1", .name = "SBLive! Value [CT4780]", 
1017          .id = "Live",
1018          .emu10k1_chip = 1,
1019          .ac97_chip = 1,
1020          .sblive51 = 1} ,
1021         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x40011102,
1022          .driver = "EMU10K1", .name = "E-mu APS [4001]", 
1023          .id = "APS",
1024          .emu10k1_chip = 1,
1025          .ecard = 1} ,
1026         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00211102,
1027          .driver = "EMU10K1", .name = "SBLive! [CT4620]", 
1028          .id = "Live",
1029          .emu10k1_chip = 1,
1030          .ac97_chip = 1,
1031          .sblive51 = 1} ,
1032         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00201102,
1033          .driver = "EMU10K1", .name = "SBLive! Value [CT4670]", 
1034          .id = "Live",
1035          .emu10k1_chip = 1,
1036          .ac97_chip = 1,
1037          .sblive51 = 1} ,
1038         {.vendor = 0x1102, .device = 0x0002,
1039          .driver = "EMU10K1", .name = "SB Live [Unknown]", 
1040          .id = "Live",
1041          .emu10k1_chip = 1,
1042          .ac97_chip = 1,
1043          .sblive51 = 1} ,
1044         { } /* terminator */
1045 };
1046
1047 int __devinit snd_emu10k1_create(struct snd_card *card,
1048                        struct pci_dev * pci,
1049                        unsigned short extin_mask,
1050                        unsigned short extout_mask,
1051                        long max_cache_bytes,
1052                        int enable_ir,
1053                        uint subsystem,
1054                        struct snd_emu10k1 ** remu)
1055 {
1056         struct snd_emu10k1 *emu;
1057         int idx, err;
1058         int is_audigy;
1059         unsigned char revision;
1060         unsigned int silent_page;
1061         const struct snd_emu_chip_details *c;
1062         static struct snd_device_ops ops = {
1063                 .dev_free =     snd_emu10k1_dev_free,
1064         };
1065         
1066         *remu = NULL;
1067
1068         /* enable PCI device */
1069         if ((err = pci_enable_device(pci)) < 0)
1070                 return err;
1071
1072         emu = kzalloc(sizeof(*emu), GFP_KERNEL);
1073         if (emu == NULL) {
1074                 pci_disable_device(pci);
1075                 return -ENOMEM;
1076         }
1077         emu->card = card;
1078         spin_lock_init(&emu->reg_lock);
1079         spin_lock_init(&emu->emu_lock);
1080         spin_lock_init(&emu->voice_lock);
1081         spin_lock_init(&emu->synth_lock);
1082         spin_lock_init(&emu->memblk_lock);
1083         init_MUTEX(&emu->ptb_lock);
1084         init_MUTEX(&emu->fx8010.lock);
1085         INIT_LIST_HEAD(&emu->mapped_link_head);
1086         INIT_LIST_HEAD(&emu->mapped_order_link_head);
1087         emu->pci = pci;
1088         emu->irq = -1;
1089         emu->synth = NULL;
1090         emu->get_synth_voice = NULL;
1091         /* read revision & serial */
1092         pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
1093         emu->revision = revision;
1094         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &emu->serial);
1095         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &emu->model);
1096         snd_printdd("vendor=0x%x, device=0x%x, subsystem_vendor_id=0x%x, subsystem_id=0x%x\n",pci->vendor, pci->device, emu->serial, emu->model);
1097
1098         for (c = emu_chip_details; c->vendor; c++) {
1099                 if (c->vendor == pci->vendor && c->device == pci->device) {
1100                         if (subsystem) {
1101                                 if (c->subsystem && (c->subsystem == subsystem) ) {
1102                                         break;
1103                                 } else continue;
1104                         } else {
1105                                 if (c->subsystem && (c->subsystem != emu->serial) )
1106                                         continue;
1107                                 if (c->revision && c->revision != emu->revision)
1108                                         continue;
1109                         }
1110                         break;
1111                 }
1112         }
1113         if (c->vendor == 0) {
1114                 snd_printk(KERN_ERR "emu10k1: Card not recognised\n");
1115                 kfree(emu);
1116                 pci_disable_device(pci);
1117                 return -ENOENT;
1118         }
1119         emu->card_capabilities = c;
1120         if (c->subsystem && !subsystem)
1121                 snd_printdd("Sound card name=%s\n", c->name);
1122         else if (subsystem) 
1123                 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x. Forced to subsytem=0x%x\n",
1124                         c->name, pci->vendor, pci->device, emu->serial, c->subsystem);
1125         else 
1126                 snd_printdd("Sound card name=%s, vendor=0x%x, device=0x%x, subsystem=0x%x.\n",
1127                         c->name, pci->vendor, pci->device, emu->serial);
1128         
1129         if (!*card->id && c->id) {
1130                 int i, n = 0;
1131                 strlcpy(card->id, c->id, sizeof(card->id));
1132                 for (;;) {
1133                         for (i = 0; i < snd_ecards_limit; i++) {
1134                                 if (snd_cards[i] && !strcmp(snd_cards[i]->id, card->id))
1135                                         break;
1136                         }
1137                         if (i >= snd_ecards_limit)
1138                                 break;
1139                         n++;
1140                         if (n >= SNDRV_CARDS)
1141                                 break;
1142                         snprintf(card->id, sizeof(card->id), "%s_%d", c->id, n);
1143                 }
1144         }
1145
1146         is_audigy = emu->audigy = c->emu10k2_chip;
1147
1148         /* set the DMA transfer mask */
1149         emu->dma_mask = is_audigy ? AUDIGY_DMA_MASK : EMU10K1_DMA_MASK;
1150         if (pci_set_dma_mask(pci, emu->dma_mask) < 0 ||
1151             pci_set_consistent_dma_mask(pci, emu->dma_mask) < 0) {
1152                 snd_printk(KERN_ERR "architecture does not support PCI busmaster DMA with mask 0x%lx\n", emu->dma_mask);
1153                 kfree(emu);
1154                 pci_disable_device(pci);
1155                 return -ENXIO;
1156         }
1157         if (is_audigy)
1158                 emu->gpr_base = A_FXGPREGBASE;
1159         else
1160                 emu->gpr_base = FXGPREGBASE;
1161
1162         if ((err = pci_request_regions(pci, "EMU10K1")) < 0) {
1163                 kfree(emu);
1164                 pci_disable_device(pci);
1165                 return err;
1166         }
1167         emu->port = pci_resource_start(pci, 0);
1168
1169         if (request_irq(pci->irq, snd_emu10k1_interrupt, SA_INTERRUPT|SA_SHIRQ, "EMU10K1", (void *)emu)) {
1170                 err = -EBUSY;
1171                 goto error;
1172         }
1173         emu->irq = pci->irq;
1174
1175         emu->max_cache_pages = max_cache_bytes >> PAGE_SHIFT;
1176         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1177                                 32 * 1024, &emu->ptb_pages) < 0) {
1178                 err = -ENOMEM;
1179                 goto error;
1180         }
1181
1182         emu->page_ptr_table = (void **)vmalloc(emu->max_cache_pages * sizeof(void*));
1183         emu->page_addr_table = (unsigned long*)vmalloc(emu->max_cache_pages * sizeof(unsigned long));
1184         if (emu->page_ptr_table == NULL || emu->page_addr_table == NULL) {
1185                 err = -ENOMEM;
1186                 goto error;
1187         }
1188
1189         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1190                                 EMUPAGESIZE, &emu->silent_page) < 0) {
1191                 err = -ENOMEM;
1192                 goto error;
1193         }
1194         emu->memhdr = snd_util_memhdr_new(emu->max_cache_pages * PAGE_SIZE);
1195         if (emu->memhdr == NULL) {
1196                 err = -ENOMEM;
1197                 goto error;
1198         }
1199         emu->memhdr->block_extra_size = sizeof(struct snd_emu10k1_memblk) -
1200                 sizeof(struct snd_util_memblk);
1201
1202         pci_set_master(pci);
1203
1204         emu->fx8010.fxbus_mask = 0x303f;
1205         if (extin_mask == 0)
1206                 extin_mask = 0x3fcf;
1207         if (extout_mask == 0)
1208                 extout_mask = 0x7fff;
1209         emu->fx8010.extin_mask = extin_mask;
1210         emu->fx8010.extout_mask = extout_mask;
1211         emu->enable_ir = enable_ir;
1212
1213         if (emu->card_capabilities->ecard) {
1214                 if ((err = snd_emu10k1_ecard_init(emu)) < 0)
1215                         goto error;
1216         } else if (emu->card_capabilities->ca_cardbus_chip) {
1217                 if ((err = snd_emu10k1_cardbus_init(emu)) < 0)
1218                         goto error;
1219         } else if (emu->card_capabilities->emu1212m) {
1220                 if ((err = snd_emu10k1_emu1212m_init(emu)) < 0) {
1221                         snd_emu10k1_free(emu);
1222                         return err;
1223                 }
1224         } else {
1225                 /* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
1226                         does not support this, it shouldn't do any harm */
1227                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
1228         }
1229
1230         /* initialize TRAM setup */
1231         emu->fx8010.itram_size = (16 * 1024)/2;
1232         emu->fx8010.etram_pages.area = NULL;
1233         emu->fx8010.etram_pages.bytes = 0;
1234
1235         /*
1236          *  Init to 0x02109204 :
1237          *  Clock accuracy    = 0     (1000ppm)
1238          *  Sample Rate       = 2     (48kHz)
1239          *  Audio Channel     = 1     (Left of 2)
1240          *  Source Number     = 0     (Unspecified)
1241          *  Generation Status = 1     (Original for Cat Code 12)
1242          *  Cat Code          = 12    (Digital Signal Mixer)
1243          *  Mode              = 0     (Mode 0)
1244          *  Emphasis          = 0     (None)
1245          *  CP                = 1     (Copyright unasserted)
1246          *  AN                = 0     (Audio data)
1247          *  P                 = 0     (Consumer)
1248          */
1249         emu->spdif_bits[0] = emu->spdif_bits[1] =
1250                 emu->spdif_bits[2] = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1251                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1252                 SPCS_GENERATIONSTATUS | 0x00001200 |
1253                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
1254
1255         emu->reserved_page = (struct snd_emu10k1_memblk *)
1256                 snd_emu10k1_synth_alloc(emu, 4096);
1257         if (emu->reserved_page)
1258                 emu->reserved_page->map_locked = 1;
1259         
1260         /* Clear silent pages and set up pointers */
1261         memset(emu->silent_page.area, 0, PAGE_SIZE);
1262         silent_page = emu->silent_page.addr << 1;
1263         for (idx = 0; idx < MAXPAGES; idx++)
1264                 ((u32 *)emu->ptb_pages.area)[idx] = cpu_to_le32(silent_page | idx);
1265
1266         /* set up voice indices */
1267         for (idx = 0; idx < NUM_G; idx++) {
1268                 emu->voices[idx].emu = emu;
1269                 emu->voices[idx].number = idx;
1270         }
1271
1272         if ((err = snd_emu10k1_init(emu, enable_ir, 0)) < 0)
1273                 goto error;
1274 #ifdef CONFIG_PM
1275         if ((err = alloc_pm_buffer(emu)) < 0)
1276                 goto error;
1277 #endif
1278
1279         /*  Initialize the effect engine */
1280         if ((err = snd_emu10k1_init_efx(emu)) < 0)
1281                 goto error;
1282         snd_emu10k1_audio_enable(emu);
1283
1284         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, emu, &ops)) < 0)
1285                 goto error;
1286
1287 #ifdef CONFIG_PROC_FS
1288         snd_emu10k1_proc_init(emu);
1289 #endif
1290
1291         snd_card_set_dev(card, &pci->dev);
1292         *remu = emu;
1293         return 0;
1294
1295  error:
1296         snd_emu10k1_free(emu);
1297         return err;
1298 }
1299
1300 #ifdef CONFIG_PM
1301 static unsigned char saved_regs[] = {
1302         CPF, PTRX, CVCF, VTFT, Z1, Z2, PSST, DSL, CCCA, CCR, CLP,
1303         FXRT, MAPA, MAPB, ENVVOL, ATKHLDV, DCYSUSV, LFOVAL1, ENVVAL,
1304         ATKHLDM, DCYSUSM, LFOVAL2, IP, IFATN, PEFE, FMMOD, TREMFRQ, FM2FRQ2,
1305         TEMPENV, ADCCR, FXWC, MICBA, ADCBA, FXBA,
1306         MICBS, ADCBS, FXBS, CDCS, GPSCS, SPCS0, SPCS1, SPCS2,
1307         SPBYPASS, AC97SLOT, CDSRCS, GPSRCS, ZVSRCS, MICIDX, ADCIDX, FXIDX,
1308         0xff /* end */
1309 };
1310 static unsigned char saved_regs_audigy[] = {
1311         A_ADCIDX, A_MICIDX, A_FXWC1, A_FXWC2, A_SAMPLE_RATE,
1312         A_FXRT2, A_SENDAMOUNTS, A_FXRT1,
1313         0xff /* end */
1314 };
1315
1316 static int __devinit alloc_pm_buffer(struct snd_emu10k1 *emu)
1317 {
1318         int size;
1319
1320         size = ARRAY_SIZE(saved_regs);
1321         if (emu->audigy)
1322                 size += ARRAY_SIZE(saved_regs_audigy);
1323         emu->saved_ptr = vmalloc(4 * NUM_G * size);
1324         if (! emu->saved_ptr)
1325                 return -ENOMEM;
1326         if (snd_emu10k1_efx_alloc_pm_buffer(emu) < 0)
1327                 return -ENOMEM;
1328         if (emu->card_capabilities->ca0151_chip &&
1329             snd_p16v_alloc_pm_buffer(emu) < 0)
1330                 return -ENOMEM;
1331         return 0;
1332 }
1333
1334 static void free_pm_buffer(struct snd_emu10k1 *emu)
1335 {
1336         vfree(emu->saved_ptr);
1337         snd_emu10k1_efx_free_pm_buffer(emu);
1338         if (emu->card_capabilities->ca0151_chip)
1339                 snd_p16v_free_pm_buffer(emu);
1340 }
1341
1342 void snd_emu10k1_suspend_regs(struct snd_emu10k1 *emu)
1343 {
1344         int i;
1345         unsigned char *reg;
1346         unsigned int *val;
1347
1348         val = emu->saved_ptr;
1349         for (reg = saved_regs; *reg != 0xff; reg++)
1350                 for (i = 0; i < NUM_G; i++, val++)
1351                         *val = snd_emu10k1_ptr_read(emu, *reg, i);
1352         if (emu->audigy) {
1353                 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
1354                         for (i = 0; i < NUM_G; i++, val++)
1355                                 *val = snd_emu10k1_ptr_read(emu, *reg, i);
1356         }
1357         if (emu->audigy)
1358                 emu->saved_a_iocfg = inl(emu->port + A_IOCFG);
1359         emu->saved_hcfg = inl(emu->port + HCFG);
1360 }
1361
1362 void snd_emu10k1_resume_init(struct snd_emu10k1 *emu)
1363 {
1364         if (emu->card_capabilities->ecard)
1365                 snd_emu10k1_ecard_init(emu);
1366         else
1367                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
1368         snd_emu10k1_init(emu, emu->enable_ir, 1);
1369 }
1370
1371 void snd_emu10k1_resume_regs(struct snd_emu10k1 *emu)
1372 {
1373         int i;
1374         unsigned char *reg;
1375         unsigned int *val;
1376
1377         snd_emu10k1_audio_enable(emu);
1378
1379         /* resore for spdif */
1380         if (emu->audigy)
1381                 outl(emu->port + A_IOCFG, emu->saved_a_iocfg);
1382         outl(emu->port + HCFG, emu->saved_hcfg);
1383
1384         val = emu->saved_ptr;
1385         for (reg = saved_regs; *reg != 0xff; reg++)
1386                 for (i = 0; i < NUM_G; i++, val++)
1387                         snd_emu10k1_ptr_write(emu, *reg, i, *val);
1388         if (emu->audigy) {
1389                 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
1390                         for (i = 0; i < NUM_G; i++, val++)
1391                                 snd_emu10k1_ptr_write(emu, *reg, i, *val);
1392         }
1393 }
1394 #endif
1395
1396 /* memory.c */
1397 EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
1398 EXPORT_SYMBOL(snd_emu10k1_synth_free);
1399 EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
1400 EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);
1401 EXPORT_SYMBOL(snd_emu10k1_memblk_map);
1402 /* voice.c */
1403 EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
1404 EXPORT_SYMBOL(snd_emu10k1_voice_free);
1405 /* io.c */
1406 EXPORT_SYMBOL(snd_emu10k1_ptr_read);
1407 EXPORT_SYMBOL(snd_emu10k1_ptr_write);