~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/emu10k1/emu10k1_main.c

Version: ~ [ linux-5.12-rc1 ] ~ [ linux-5.11.2 ] ~ [ linux-5.10.19 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.101 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.177 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.222 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.258 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.258 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3  *                   Creative Labs, Inc.
  4  *  Routines for control of EMU10K1 chips
  5  *
  6  *  Copyright (c) by James Courtier-Dutton <James@superbug.co.uk>
  7  *      Added support for Audigy 2 Value.
  8  *      Added EMU 1010 support.
  9  *      General bug fixes and enhancements.
 10  *
 11  *
 12  *  BUGS:
 13  *    --
 14  *
 15  *  TODO:
 16  *    --
 17  *
 18  *   This program is free software; you can redistribute it and/or modify
 19  *   it under the terms of the GNU General Public License as published by
 20  *   the Free Software Foundation; either version 2 of the License, or
 21  *   (at your option) any later version.
 22  *
 23  *   This program is distributed in the hope that it will be useful,
 24  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  *   GNU General Public License for more details.
 27  *
 28  *   You should have received a copy of the GNU General Public License
 29  *   along with this program; if not, write to the Free Software
 30  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 31  *
 32  */
 33 
 34 #include <linux/sched.h>
 35 #include <linux/kthread.h>
 36 #include <linux/delay.h>
 37 #include <linux/init.h>
 38 #include <linux/module.h>
 39 #include <linux/interrupt.h>
 40 #include <linux/pci.h>
 41 #include <linux/slab.h>
 42 #include <linux/vmalloc.h>
 43 #include <linux/mutex.h>
 44 
 45 
 46 #include <sound/core.h>
 47 #include <sound/emu10k1.h>
 48 #include <linux/firmware.h>
 49 #include "p16v.h"
 50 #include "tina2.h"
 51 #include "p17v.h"
 52 
 53 
 54 #define HANA_FILENAME "emu/hana.fw"
 55 #define DOCK_FILENAME "emu/audio_dock.fw"
 56 #define EMU1010B_FILENAME "emu/emu1010b.fw"
 57 #define MICRO_DOCK_FILENAME "emu/micro_dock.fw"
 58 #define EMU0404_FILENAME "emu/emu0404.fw"
 59 #define EMU1010_NOTEBOOK_FILENAME "emu/emu1010_notebook.fw"
 60 
 61 MODULE_FIRMWARE(HANA_FILENAME);
 62 MODULE_FIRMWARE(DOCK_FILENAME);
 63 MODULE_FIRMWARE(EMU1010B_FILENAME);
 64 MODULE_FIRMWARE(MICRO_DOCK_FILENAME);
 65 MODULE_FIRMWARE(EMU0404_FILENAME);
 66 MODULE_FIRMWARE(EMU1010_NOTEBOOK_FILENAME);
 67 
 68 
 69 /*************************************************************************
 70  * EMU10K1 init / done
 71  *************************************************************************/
 72 
 73 void snd_emu10k1_voice_init(struct snd_emu10k1 *emu, int ch)
 74 {
 75         snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
 76         snd_emu10k1_ptr_write(emu, IP, ch, 0);
 77         snd_emu10k1_ptr_write(emu, VTFT, ch, 0xffff);
 78         snd_emu10k1_ptr_write(emu, CVCF, ch, 0xffff);
 79         snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
 80         snd_emu10k1_ptr_write(emu, CPF, ch, 0);
 81         snd_emu10k1_ptr_write(emu, CCR, ch, 0);
 82 
 83         snd_emu10k1_ptr_write(emu, PSST, ch, 0);
 84         snd_emu10k1_ptr_write(emu, DSL, ch, 0x10);
 85         snd_emu10k1_ptr_write(emu, CCCA, ch, 0);
 86         snd_emu10k1_ptr_write(emu, Z1, ch, 0);
 87         snd_emu10k1_ptr_write(emu, Z2, ch, 0);
 88         snd_emu10k1_ptr_write(emu, FXRT, ch, 0x32100000);
 89 
 90         snd_emu10k1_ptr_write(emu, ATKHLDM, ch, 0);
 91         snd_emu10k1_ptr_write(emu, DCYSUSM, ch, 0);
 92         snd_emu10k1_ptr_write(emu, IFATN, ch, 0xffff);
 93         snd_emu10k1_ptr_write(emu, PEFE, ch, 0);
 94         snd_emu10k1_ptr_write(emu, FMMOD, ch, 0);
 95         snd_emu10k1_ptr_write(emu, TREMFRQ, ch, 24);    /* 1 Hz */
 96         snd_emu10k1_ptr_write(emu, FM2FRQ2, ch, 24);    /* 1 Hz */
 97         snd_emu10k1_ptr_write(emu, TEMPENV, ch, 0);
 98 
 99         /*** these are last so OFF prevents writing ***/
100         snd_emu10k1_ptr_write(emu, LFOVAL2, ch, 0);
101         snd_emu10k1_ptr_write(emu, LFOVAL1, ch, 0);
102         snd_emu10k1_ptr_write(emu, ATKHLDV, ch, 0);
103         snd_emu10k1_ptr_write(emu, ENVVOL, ch, 0);
104         snd_emu10k1_ptr_write(emu, ENVVAL, ch, 0);
105 
106         /* Audigy extra stuffs */
107         if (emu->audigy) {
108                 snd_emu10k1_ptr_write(emu, 0x4c, ch, 0); /* ?? */
109                 snd_emu10k1_ptr_write(emu, 0x4d, ch, 0); /* ?? */
110                 snd_emu10k1_ptr_write(emu, 0x4e, ch, 0); /* ?? */
111                 snd_emu10k1_ptr_write(emu, 0x4f, ch, 0); /* ?? */
112                 snd_emu10k1_ptr_write(emu, A_FXRT1, ch, 0x03020100);
113                 snd_emu10k1_ptr_write(emu, A_FXRT2, ch, 0x3f3f3f3f);
114                 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, ch, 0);
115         }
116 }
117 
118 static unsigned int spi_dac_init[] = {
119                 0x00ff,
120                 0x02ff,
121                 0x0400,
122                 0x0520,
123                 0x0600,
124                 0x08ff,
125                 0x0aff,
126                 0x0cff,
127                 0x0eff,
128                 0x10ff,
129                 0x1200,
130                 0x1400,
131                 0x1480,
132                 0x1800,
133                 0x1aff,
134                 0x1cff,
135                 0x1e00,
136                 0x0530,
137                 0x0602,
138                 0x0622,
139                 0x1400,
140 };
141 
142 static unsigned int i2c_adc_init[][2] = {
143         { 0x17, 0x00 }, /* Reset */
144         { 0x07, 0x00 }, /* Timeout */
145         { 0x0b, 0x22 },  /* Interface control */
146         { 0x0c, 0x22 },  /* Master mode control */
147         { 0x0d, 0x08 },  /* Powerdown control */
148         { 0x0e, 0xcf },  /* Attenuation Left  0x01 = -103dB, 0xff = 24dB */
149         { 0x0f, 0xcf },  /* Attenuation Right 0.5dB steps */
150         { 0x10, 0x7b },  /* ALC Control 1 */
151         { 0x11, 0x00 },  /* ALC Control 2 */
152         { 0x12, 0x32 },  /* ALC Control 3 */
153         { 0x13, 0x00 },  /* Noise gate control */
154         { 0x14, 0xa6 },  /* Limiter control */
155         { 0x15, ADC_MUX_2 },  /* ADC Mixer control. Mic for A2ZS Notebook */
156 };
157 
158 static int snd_emu10k1_init(struct snd_emu10k1 *emu, int enable_ir, int resume)
159 {
160         unsigned int silent_page;
161         int ch;
162         u32 tmp;
163 
164         /* disable audio and lock cache */
165         outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK |
166                 HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
167 
168         /* reset recording buffers */
169         snd_emu10k1_ptr_write(emu, MICBS, 0, ADCBS_BUFSIZE_NONE);
170         snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
171         snd_emu10k1_ptr_write(emu, FXBS, 0, ADCBS_BUFSIZE_NONE);
172         snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
173         snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
174         snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
175 
176         /* disable channel interrupt */
177         outl(0, emu->port + INTE);
178         snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
179         snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
180         snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
181         snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
182 
183         if (emu->audigy) {
184                 /* set SPDIF bypass mode */
185                 snd_emu10k1_ptr_write(emu, SPBYPASS, 0, SPBYPASS_FORMAT);
186                 /* enable rear left + rear right AC97 slots */
187                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_REAR_RIGHT |
188                                       AC97SLOT_REAR_LEFT);
189         }
190 
191         /* init envelope engine */
192         for (ch = 0; ch < NUM_G; ch++)
193                 snd_emu10k1_voice_init(emu, ch);
194 
195         snd_emu10k1_ptr_write(emu, SPCS0, 0, emu->spdif_bits[0]);
196         snd_emu10k1_ptr_write(emu, SPCS1, 0, emu->spdif_bits[1]);
197         snd_emu10k1_ptr_write(emu, SPCS2, 0, emu->spdif_bits[2]);
198 
199         if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
200                 /* Hacks for Alice3 to work independent of haP16V driver */
201                 /* Setup SRCMulti_I2S SamplingRate */
202                 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
203                 tmp &= 0xfffff1ff;
204                 tmp |= (0x2<<9);
205                 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
206 
207                 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
208                 snd_emu10k1_ptr20_write(emu, SRCSel, 0, 0x14);
209                 /* Setup SRCMulti Input Audio Enable */
210                 /* Use 0xFFFFFFFF to enable P16V sounds. */
211                 snd_emu10k1_ptr20_write(emu, SRCMULTI_ENABLE, 0, 0xFFFFFFFF);
212 
213                 /* Enabled Phased (8-channel) P16V playback */
214                 outl(0x0201, emu->port + HCFG2);
215                 /* Set playback routing. */
216                 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, 0x78e4);
217         }
218         if (emu->card_capabilities->ca0108_chip) { /* audigy2 Value */
219                 /* Hacks for Alice3 to work independent of haP16V driver */
220                 dev_info(emu->card->dev, "Audigy2 value: Special config.\n");
221                 /* Setup SRCMulti_I2S SamplingRate */
222                 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
223                 tmp &= 0xfffff1ff;
224                 tmp |= (0x2<<9);
225                 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
226 
227                 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
228                 outl(0x600000, emu->port + 0x20);
229                 outl(0x14, emu->port + 0x24);
230 
231                 /* Setup SRCMulti Input Audio Enable */
232                 outl(0x7b0000, emu->port + 0x20);
233                 outl(0xFF000000, emu->port + 0x24);
234 
235                 /* Setup SPDIF Out Audio Enable */
236                 /* The Audigy 2 Value has a separate SPDIF out,
237                  * so no need for a mixer switch
238                  */
239                 outl(0x7a0000, emu->port + 0x20);
240                 outl(0xFF000000, emu->port + 0x24);
241                 tmp = inl(emu->port + A_IOCFG) & ~0x8; /* Clear bit 3 */
242                 outl(tmp, emu->port + A_IOCFG);
243         }
244         if (emu->card_capabilities->spi_dac) { /* Audigy 2 ZS Notebook with DAC Wolfson WM8768/WM8568 */
245                 int size, n;
246 
247                 size = ARRAY_SIZE(spi_dac_init);
248                 for (n = 0; n < size; n++)
249                         snd_emu10k1_spi_write(emu, spi_dac_init[n]);
250 
251                 snd_emu10k1_ptr20_write(emu, 0x60, 0, 0x10);
252                 /* Enable GPIOs
253                  * GPIO0: Unknown
254                  * GPIO1: Speakers-enabled.
255                  * GPIO2: Unknown
256                  * GPIO3: Unknown
257                  * GPIO4: IEC958 Output on.
258                  * GPIO5: Unknown
259                  * GPIO6: Unknown
260                  * GPIO7: Unknown
261                  */
262                 outl(0x76, emu->port + A_IOCFG); /* Windows uses 0x3f76 */
263         }
264         if (emu->card_capabilities->i2c_adc) { /* Audigy 2 ZS Notebook with ADC Wolfson WM8775 */
265                 int size, n;
266 
267                 snd_emu10k1_ptr20_write(emu, P17V_I2S_SRC_SEL, 0, 0x2020205f);
268                 tmp = inl(emu->port + A_IOCFG);
269                 outl(tmp | 0x4, emu->port + A_IOCFG);  /* Set bit 2 for mic input */
270                 tmp = inl(emu->port + A_IOCFG);
271                 size = ARRAY_SIZE(i2c_adc_init);
272                 for (n = 0; n < size; n++)
273                         snd_emu10k1_i2c_write(emu, i2c_adc_init[n][0], i2c_adc_init[n][1]);
274                 for (n = 0; n < 4; n++) {
275                         emu->i2c_capture_volume[n][0] = 0xcf;
276                         emu->i2c_capture_volume[n][1] = 0xcf;
277                 }
278         }
279 
280 
281         snd_emu10k1_ptr_write(emu, PTB, 0, emu->ptb_pages.addr);
282         snd_emu10k1_ptr_write(emu, TCB, 0, 0);  /* taken from original driver */
283         snd_emu10k1_ptr_write(emu, TCBS, 0, 4); /* taken from original driver */
284 
285         silent_page = (emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
286         for (ch = 0; ch < NUM_G; ch++) {
287                 snd_emu10k1_ptr_write(emu, MAPA, ch, silent_page);
288                 snd_emu10k1_ptr_write(emu, MAPB, ch, silent_page);
289         }
290 
291         if (emu->card_capabilities->emu_model) {
292                 outl(HCFG_AUTOMUTE_ASYNC |
293                         HCFG_EMU32_SLAVE |
294                         HCFG_AUDIOENABLE, emu->port + HCFG);
295         /*
296          *  Hokay, setup HCFG
297          *   Mute Disable Audio = 0
298          *   Lock Tank Memory = 1
299          *   Lock Sound Memory = 0
300          *   Auto Mute = 1
301          */
302         } else if (emu->audigy) {
303                 if (emu->revision == 4) /* audigy2 */
304                         outl(HCFG_AUDIOENABLE |
305                              HCFG_AC3ENABLE_CDSPDIF |
306                              HCFG_AC3ENABLE_GPSPDIF |
307                              HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
308                 else
309                         outl(HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
310         /* FIXME: Remove all these emu->model and replace it with a card recognition parameter,
311          * e.g. card_capabilities->joystick */
312         } else if (emu->model == 0x20 ||
313             emu->model == 0xc400 ||
314             (emu->model == 0x21 && emu->revision < 6))
315                 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE, emu->port + HCFG);
316         else
317                 /* With on-chip joystick */
318                 outl(HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE, emu->port + HCFG);
319 
320         if (enable_ir) {        /* enable IR for SB Live */
321                 if (emu->card_capabilities->emu_model) {
322                         ;  /* Disable all access to A_IOCFG for the emu1010 */
323                 } else if (emu->card_capabilities->i2c_adc) {
324                         ;  /* Disable A_IOCFG for Audigy 2 ZS Notebook */
325                 } else if (emu->audigy) {
326                         unsigned int reg = inl(emu->port + A_IOCFG);
327                         outl(reg | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
328                         udelay(500);
329                         outl(reg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, emu->port + A_IOCFG);
330                         udelay(100);
331                         outl(reg, emu->port + A_IOCFG);
332                 } else {
333                         unsigned int reg = inl(emu->port + HCFG);
334                         outl(reg | HCFG_GPOUT2, emu->port + HCFG);
335                         udelay(500);
336                         outl(reg | HCFG_GPOUT1 | HCFG_GPOUT2, emu->port + HCFG);
337                         udelay(100);
338                         outl(reg, emu->port + HCFG);
339                 }
340         }
341 
342         if (emu->card_capabilities->emu_model) {
343                 ;  /* Disable all access to A_IOCFG for the emu1010 */
344         } else if (emu->card_capabilities->i2c_adc) {
345                 ;  /* Disable A_IOCFG for Audigy 2 ZS Notebook */
346         } else if (emu->audigy) {       /* enable analog output */
347                 unsigned int reg = inl(emu->port + A_IOCFG);
348                 outl(reg | A_IOCFG_GPOUT0, emu->port + A_IOCFG);
349         }
350 
351         if (emu->address_mode == 0) {
352                 /* use 16M in 4G */
353                 outl(inl(emu->port + HCFG) | HCFG_EXPANDED_MEM, emu->port + HCFG);
354         }
355 
356         return 0;
357 }
358 
359 static void snd_emu10k1_audio_enable(struct snd_emu10k1 *emu)
360 {
361         /*
362          *  Enable the audio bit
363          */
364         outl(inl(emu->port + HCFG) | HCFG_AUDIOENABLE, emu->port + HCFG);
365 
366         /* Enable analog/digital outs on audigy */
367         if (emu->card_capabilities->emu_model) {
368                 ;  /* Disable all access to A_IOCFG for the emu1010 */
369         } else if (emu->card_capabilities->i2c_adc) {
370                 ;  /* Disable A_IOCFG for Audigy 2 ZS Notebook */
371         } else if (emu->audigy) {
372                 outl(inl(emu->port + A_IOCFG) & ~0x44, emu->port + A_IOCFG);
373 
374                 if (emu->card_capabilities->ca0151_chip) { /* audigy2 */
375                         /* Unmute Analog now.  Set GPO6 to 1 for Apollo.
376                          * This has to be done after init ALice3 I2SOut beyond 48KHz.
377                          * So, sequence is important. */
378                         outl(inl(emu->port + A_IOCFG) | 0x0040, emu->port + A_IOCFG);
379                 } else if (emu->card_capabilities->ca0108_chip) { /* audigy2 value */
380                         /* Unmute Analog now. */
381                         outl(inl(emu->port + A_IOCFG) | 0x0060, emu->port + A_IOCFG);
382                 } else {
383                         /* Disable routing from AC97 line out to Front speakers */
384                         outl(inl(emu->port + A_IOCFG) | 0x0080, emu->port + A_IOCFG);
385                 }
386         }
387 
388 #if 0
389         {
390         unsigned int tmp;
391         /* FIXME: the following routine disables LiveDrive-II !! */
392         /* TOSLink detection */
393         emu->tos_link = 0;
394         tmp = inl(emu->port + HCFG);
395         if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
396                 outl(tmp|0x800, emu->port + HCFG);
397                 udelay(50);
398                 if (tmp != (inl(emu->port + HCFG) & ~0x800)) {
399                         emu->tos_link = 1;
400                         outl(tmp, emu->port + HCFG);
401                 }
402         }
403         }
404 #endif
405 
406         snd_emu10k1_intr_enable(emu, INTE_PCIERRORENABLE);
407 }
408 
409 int snd_emu10k1_done(struct snd_emu10k1 *emu)
410 {
411         int ch;
412 
413         outl(0, emu->port + INTE);
414 
415         /*
416          *  Shutdown the chip
417          */
418         for (ch = 0; ch < NUM_G; ch++)
419                 snd_emu10k1_ptr_write(emu, DCYSUSV, ch, 0);
420         for (ch = 0; ch < NUM_G; ch++) {
421                 snd_emu10k1_ptr_write(emu, VTFT, ch, 0);
422                 snd_emu10k1_ptr_write(emu, CVCF, ch, 0);
423                 snd_emu10k1_ptr_write(emu, PTRX, ch, 0);
424                 snd_emu10k1_ptr_write(emu, CPF, ch, 0);
425         }
426 
427         /* reset recording buffers */
428         snd_emu10k1_ptr_write(emu, MICBS, 0, 0);
429         snd_emu10k1_ptr_write(emu, MICBA, 0, 0);
430         snd_emu10k1_ptr_write(emu, FXBS, 0, 0);
431         snd_emu10k1_ptr_write(emu, FXBA, 0, 0);
432         snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
433         snd_emu10k1_ptr_write(emu, ADCBS, 0, ADCBS_BUFSIZE_NONE);
434         snd_emu10k1_ptr_write(emu, ADCBA, 0, 0);
435         snd_emu10k1_ptr_write(emu, TCBS, 0, TCBS_BUFFSIZE_16K);
436         snd_emu10k1_ptr_write(emu, TCB, 0, 0);
437         if (emu->audigy)
438                 snd_emu10k1_ptr_write(emu, A_DBG, 0, A_DBG_SINGLE_STEP);
439         else
440                 snd_emu10k1_ptr_write(emu, DBG, 0, EMU10K1_DBG_SINGLE_STEP);
441 
442         /* disable channel interrupt */
443         snd_emu10k1_ptr_write(emu, CLIEL, 0, 0);
444         snd_emu10k1_ptr_write(emu, CLIEH, 0, 0);
445         snd_emu10k1_ptr_write(emu, SOLEL, 0, 0);
446         snd_emu10k1_ptr_write(emu, SOLEH, 0, 0);
447 
448         /* disable audio and lock cache */
449         outl(HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, emu->port + HCFG);
450         snd_emu10k1_ptr_write(emu, PTB, 0, 0);
451 
452         return 0;
453 }
454 
455 /*************************************************************************
456  * ECARD functional implementation
457  *************************************************************************/
458 
459 /* In A1 Silicon, these bits are in the HC register */
460 #define HOOKN_BIT               (1L << 12)
461 #define HANDN_BIT               (1L << 11)
462 #define PULSEN_BIT              (1L << 10)
463 
464 #define EC_GDI1                 (1 << 13)
465 #define EC_GDI0                 (1 << 14)
466 
467 #define EC_NUM_CONTROL_BITS     20
468 
469 #define EC_AC3_DATA_SELN        0x0001L
470 #define EC_EE_DATA_SEL          0x0002L
471 #define EC_EE_CNTRL_SELN        0x0004L
472 #define EC_EECLK                0x0008L
473 #define EC_EECS                 0x0010L
474 #define EC_EESDO                0x0020L
475 #define EC_TRIM_CSN             0x0040L
476 #define EC_TRIM_SCLK            0x0080L
477 #define EC_TRIM_SDATA           0x0100L
478 #define EC_TRIM_MUTEN           0x0200L
479 #define EC_ADCCAL               0x0400L
480 #define EC_ADCRSTN              0x0800L
481 #define EC_DACCAL               0x1000L
482 #define EC_DACMUTEN             0x2000L
483 #define EC_LEDN                 0x4000L
484 
485 #define EC_SPDIF0_SEL_SHIFT     15
486 #define EC_SPDIF1_SEL_SHIFT     17
487 #define EC_SPDIF0_SEL_MASK      (0x3L << EC_SPDIF0_SEL_SHIFT)
488 #define EC_SPDIF1_SEL_MASK      (0x7L << EC_SPDIF1_SEL_SHIFT)
489 #define EC_SPDIF0_SELECT(_x)    (((_x) << EC_SPDIF0_SEL_SHIFT) & EC_SPDIF0_SEL_MASK)
490 #define EC_SPDIF1_SELECT(_x)    (((_x) << EC_SPDIF1_SEL_SHIFT) & EC_SPDIF1_SEL_MASK)
491 #define EC_CURRENT_PROM_VERSION 0x01    /* Self-explanatory.  This should
492                                          * be incremented any time the EEPROM's
493                                          * format is changed.  */
494 
495 #define EC_EEPROM_SIZE          0x40    /* ECARD EEPROM has 64 16-bit words */
496 
497 /* Addresses for special values stored in to EEPROM */
498 #define EC_PROM_VERSION_ADDR    0x20    /* Address of the current prom version */
499 #define EC_BOARDREV0_ADDR       0x21    /* LSW of board rev */
500 #define EC_BOARDREV1_ADDR       0x22    /* MSW of board rev */
501 
502 #define EC_LAST_PROMFILE_ADDR   0x2f
503 
504 #define EC_SERIALNUM_ADDR       0x30    /* First word of serial number.  The
505                                          * can be up to 30 characters in length
506                                          * and is stored as a NULL-terminated
507                                          * ASCII string.  Any unused bytes must be
508                                          * filled with zeros */
509 #define EC_CHECKSUM_ADDR        0x3f    /* Location at which checksum is stored */
510 
511 
512 /* Most of this stuff is pretty self-evident.  According to the hardware
513  * dudes, we need to leave the ADCCAL bit low in order to avoid a DC
514  * offset problem.  Weird.
515  */
516 #define EC_RAW_RUN_MODE         (EC_DACMUTEN | EC_ADCRSTN | EC_TRIM_MUTEN | \
517                                  EC_TRIM_CSN)
518 
519 
520 #define EC_DEFAULT_ADC_GAIN     0xC4C4
521 #define EC_DEFAULT_SPDIF0_SEL   0x0
522 #define EC_DEFAULT_SPDIF1_SEL   0x4
523 
524 /**************************************************************************
525  * @func Clock bits into the Ecard's control latch.  The Ecard uses a
526  *  control latch will is loaded bit-serially by toggling the Modem control
527  *  lines from function 2 on the E8010.  This function hides these details
528  *  and presents the illusion that we are actually writing to a distinct
529  *  register.
530  */
531 
532 static void snd_emu10k1_ecard_write(struct snd_emu10k1 *emu, unsigned int value)
533 {
534         unsigned short count;
535         unsigned int data;
536         unsigned long hc_port;
537         unsigned int hc_value;
538 
539         hc_port = emu->port + HCFG;
540         hc_value = inl(hc_port) & ~(HOOKN_BIT | HANDN_BIT | PULSEN_BIT);
541         outl(hc_value, hc_port);
542 
543         for (count = 0; count < EC_NUM_CONTROL_BITS; count++) {
544 
545                 /* Set up the value */
546                 data = ((value & 0x1) ? PULSEN_BIT : 0);
547                 value >>= 1;
548 
549                 outl(hc_value | data, hc_port);
550 
551                 /* Clock the shift register */
552                 outl(hc_value | data | HANDN_BIT, hc_port);
553                 outl(hc_value | data, hc_port);
554         }
555 
556         /* Latch the bits */
557         outl(hc_value | HOOKN_BIT, hc_port);
558         outl(hc_value, hc_port);
559 }
560 
561 /**************************************************************************
562  * @func Set the gain of the ECARD's CS3310 Trim/gain controller.  The
563  * trim value consists of a 16bit value which is composed of two
564  * 8 bit gain/trim values, one for the left channel and one for the
565  * right channel.  The following table maps from the Gain/Attenuation
566  * value in decibels into the corresponding bit pattern for a single
567  * channel.
568  */
569 
570 static void snd_emu10k1_ecard_setadcgain(struct snd_emu10k1 *emu,
571                                          unsigned short gain)
572 {
573         unsigned int bit;
574 
575         /* Enable writing to the TRIM registers */
576         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
577 
578         /* Do it again to insure that we meet hold time requirements */
579         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl & ~EC_TRIM_CSN);
580 
581         for (bit = (1 << 15); bit; bit >>= 1) {
582                 unsigned int value;
583 
584                 value = emu->ecard_ctrl & ~(EC_TRIM_CSN | EC_TRIM_SDATA);
585 
586                 if (gain & bit)
587                         value |= EC_TRIM_SDATA;
588 
589                 /* Clock the bit */
590                 snd_emu10k1_ecard_write(emu, value);
591                 snd_emu10k1_ecard_write(emu, value | EC_TRIM_SCLK);
592                 snd_emu10k1_ecard_write(emu, value);
593         }
594 
595         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
596 }
597 
598 static int snd_emu10k1_ecard_init(struct snd_emu10k1 *emu)
599 {
600         unsigned int hc_value;
601 
602         /* Set up the initial settings */
603         emu->ecard_ctrl = EC_RAW_RUN_MODE |
604                           EC_SPDIF0_SELECT(EC_DEFAULT_SPDIF0_SEL) |
605                           EC_SPDIF1_SELECT(EC_DEFAULT_SPDIF1_SEL);
606 
607         /* Step 0: Set the codec type in the hardware control register
608          * and enable audio output */
609         hc_value = inl(emu->port + HCFG);
610         outl(hc_value | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S, emu->port + HCFG);
611         inl(emu->port + HCFG);
612 
613         /* Step 1: Turn off the led and deassert TRIM_CS */
614         snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
615 
616         /* Step 2: Calibrate the ADC and DAC */
617         snd_emu10k1_ecard_write(emu, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
618 
619         /* Step 3: Wait for awhile;   XXX We can't get away with this
620          * under a real operating system; we'll need to block and wait that
621          * way. */
622         snd_emu10k1_wait(emu, 48000);
623 
624         /* Step 4: Switch off the DAC and ADC calibration.  Note
625          * That ADC_CAL is actually an inverted signal, so we assert
626          * it here to stop calibration.  */
627         snd_emu10k1_ecard_write(emu, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
628 
629         /* Step 4: Switch into run mode */
630         snd_emu10k1_ecard_write(emu, emu->ecard_ctrl);
631 
632         /* Step 5: Set the analog input gain */
633         snd_emu10k1_ecard_setadcgain(emu, EC_DEFAULT_ADC_GAIN);
634 
635         return 0;
636 }
637 
638 static int snd_emu10k1_cardbus_init(struct snd_emu10k1 *emu)
639 {
640         unsigned long special_port;
641         unsigned int value;
642 
643         /* Special initialisation routine
644          * before the rest of the IO-Ports become active.
645          */
646         special_port = emu->port + 0x38;
647         value = inl(special_port);
648         outl(0x00d00000, special_port);
649         value = inl(special_port);
650         outl(0x00d00001, special_port);
651         value = inl(special_port);
652         outl(0x00d0005f, special_port);
653         value = inl(special_port);
654         outl(0x00d0007f, special_port);
655         value = inl(special_port);
656         outl(0x0090007f, special_port);
657         value = inl(special_port);
658 
659         snd_emu10k1_ptr20_write(emu, TINA2_VOLUME, 0, 0xfefefefe); /* Defaults to 0x30303030 */
660         /* Delay to give time for ADC chip to switch on. It needs 113ms */
661         msleep(200);
662         return 0;
663 }
664 
665 static int snd_emu1010_load_firmware(struct snd_emu10k1 *emu,
666                                      const struct firmware *fw_entry)
667 {
668         int n, i;
669         int reg;
670         int value;
671         unsigned int write_post;
672         unsigned long flags;
673 
674         if (!fw_entry)
675                 return -EIO;
676 
677         /* The FPGA is a Xilinx Spartan IIE XC2S50E */
678         /* GPIO7 -> FPGA PGMN
679          * GPIO6 -> FPGA CCLK
680          * GPIO5 -> FPGA DIN
681          * FPGA CONFIG OFF -> FPGA PGMN
682          */
683         spin_lock_irqsave(&emu->emu_lock, flags);
684         outl(0x00, emu->port + A_IOCFG); /* Set PGMN low for 1uS. */
685         write_post = inl(emu->port + A_IOCFG);
686         udelay(100);
687         outl(0x80, emu->port + A_IOCFG); /* Leave bit 7 set during netlist setup. */
688         write_post = inl(emu->port + A_IOCFG);
689         udelay(100); /* Allow FPGA memory to clean */
690         for (n = 0; n < fw_entry->size; n++) {
691                 value = fw_entry->data[n];
692                 for (i = 0; i < 8; i++) {
693                         reg = 0x80;
694                         if (value & 0x1)
695                                 reg = reg | 0x20;
696                         value = value >> 1;
697                         outl(reg, emu->port + A_IOCFG);
698                         write_post = inl(emu->port + A_IOCFG);
699                         outl(reg | 0x40, emu->port + A_IOCFG);
700                         write_post = inl(emu->port + A_IOCFG);
701                 }
702         }
703         /* After programming, set GPIO bit 4 high again. */
704         outl(0x10, emu->port + A_IOCFG);
705         write_post = inl(emu->port + A_IOCFG);
706         spin_unlock_irqrestore(&emu->emu_lock, flags);
707 
708         return 0;
709 }
710 
711 static int emu1010_firmware_thread(void *data)
712 {
713         struct snd_emu10k1 *emu = data;
714         u32 tmp, tmp2, reg;
715         int err;
716 
717         for (;;) {
718                 /* Delay to allow Audio Dock to settle */
719                 msleep_interruptible(1000);
720                 if (kthread_should_stop())
721                         break;
722 #ifdef CONFIG_PM_SLEEP
723                 if (emu->suspend)
724                         continue;
725 #endif
726                 snd_emu1010_fpga_read(emu, EMU_HANA_IRQ_STATUS, &tmp); /* IRQ Status */
727                 snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &reg); /* OPTIONS: Which cards are attached to the EMU */
728                 if (reg & EMU_HANA_OPTION_DOCK_OFFLINE) {
729                         /* Audio Dock attached */
730                         /* Return to Audio Dock programming mode */
731                         dev_info(emu->card->dev,
732                                  "emu1010: Loading Audio Dock Firmware\n");
733                         snd_emu1010_fpga_write(emu, EMU_HANA_FPGA_CONFIG, EMU_HANA_FPGA_CONFIG_AUDIODOCK);
734 
735                         if (!emu->dock_fw) {
736                                 const char *filename = NULL;
737                                 switch (emu->card_capabilities->emu_model) {
738                                 case EMU_MODEL_EMU1010:
739                                         filename = DOCK_FILENAME;
740                                         break;
741                                 case EMU_MODEL_EMU1010B:
742                                         filename = MICRO_DOCK_FILENAME;
743                                         break;
744                                 case EMU_MODEL_EMU1616:
745                                         filename = MICRO_DOCK_FILENAME;
746                                         break;
747                                 }
748                                 if (filename) {
749                                         err = request_firmware(&emu->dock_fw,
750                                                                filename,
751                                                                &emu->pci->dev);
752                                         if (err)
753                                                 continue;
754                                 }
755                         }
756 
757                         if (emu->dock_fw) {
758                                 err = snd_emu1010_load_firmware(emu, emu->dock_fw);
759                                 if (err)
760                                         continue;
761                         }
762 
763                         snd_emu1010_fpga_write(emu, EMU_HANA_FPGA_CONFIG, 0);
764                         snd_emu1010_fpga_read(emu, EMU_HANA_IRQ_STATUS, &reg);
765                         dev_info(emu->card->dev,
766                                  "emu1010: EMU_HANA+DOCK_IRQ_STATUS = 0x%x\n",
767                                  reg);
768                         /* ID, should read & 0x7f = 0x55 when FPGA programmed. */
769                         snd_emu1010_fpga_read(emu, EMU_HANA_ID, &reg);
770                         dev_info(emu->card->dev,
771                                  "emu1010: EMU_HANA+DOCK_ID = 0x%x\n", reg);
772                         if ((reg & 0x1f) != 0x15) {
773                                 /* FPGA failed to be programmed */
774                                 dev_info(emu->card->dev,
775                                          "emu1010: Loading Audio Dock Firmware file failed, reg = 0x%x\n",
776                                          reg);
777                                 continue;
778                         }
779                         dev_info(emu->card->dev,
780                                  "emu1010: Audio Dock Firmware loaded\n");
781                         snd_emu1010_fpga_read(emu, EMU_DOCK_MAJOR_REV, &tmp);
782                         snd_emu1010_fpga_read(emu, EMU_DOCK_MINOR_REV, &tmp2);
783                         dev_info(emu->card->dev, "Audio Dock ver: %u.%u\n",
784                                    tmp, tmp2);
785                         /* Sync clocking between 1010 and Dock */
786                         /* Allow DLL to settle */
787                         msleep(10);
788                         /* Unmute all. Default is muted after a firmware load */
789                         snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE);
790                 }
791         }
792         dev_info(emu->card->dev, "emu1010: firmware thread stopping\n");
793         return 0;
794 }
795 
796 /*
797  * EMU-1010 - details found out from this driver, official MS Win drivers,
798  * testing the card:
799  *
800  * Audigy2 (aka Alice2):
801  * ---------------------
802  *      * communication over PCI
803  *      * conversion of 32-bit data coming over EMU32 links from HANA FPGA
804  *        to 2 x 16-bit, using internal DSP instructions
805  *      * slave mode, clock supplied by HANA
806  *      * linked to HANA using:
807  *              32 x 32-bit serial EMU32 output channels
808  *              16 x EMU32 input channels
809  *              (?) x I2S I/O channels (?)
810  *
811  * FPGA (aka HANA):
812  * ---------------
813  *      * provides all (?) physical inputs and outputs of the card
814  *              (ADC, DAC, SPDIF I/O, ADAT I/O, etc.)
815  *      * provides clock signal for the card and Alice2
816  *      * two crystals - for 44.1kHz and 48kHz multiples
817  *      * provides internal routing of signal sources to signal destinations
818  *      * inputs/outputs to Alice2 - see above
819  *
820  * Current status of the driver:
821  * ----------------------------
822  *      * only 44.1/48kHz supported (the MS Win driver supports up to 192 kHz)
823  *      * PCM device nb. 2:
824  *              16 x 16-bit playback - snd_emu10k1_fx8010_playback_ops
825  *              16 x 32-bit capture - snd_emu10k1_capture_efx_ops
826  */
827 static int snd_emu10k1_emu1010_init(struct snd_emu10k1 *emu)
828 {
829         unsigned int i;
830         u32 tmp, tmp2, reg;
831         int err;
832 
833         dev_info(emu->card->dev, "emu1010: Special config.\n");
834         /* AC97 2.1, Any 16Meg of 4Gig address, Auto-Mute, EMU32 Slave,
835          * Lock Sound Memory Cache, Lock Tank Memory Cache,
836          * Mute all codecs.
837          */
838         outl(0x0005a00c, emu->port + HCFG);
839         /* AC97 2.1, Any 16Meg of 4Gig address, Auto-Mute, EMU32 Slave,
840          * Lock Tank Memory Cache,
841          * Mute all codecs.
842          */
843         outl(0x0005a004, emu->port + HCFG);
844         /* AC97 2.1, Any 16Meg of 4Gig address, Auto-Mute, EMU32 Slave,
845          * Mute all codecs.
846          */
847         outl(0x0005a000, emu->port + HCFG);
848         /* AC97 2.1, Any 16Meg of 4Gig address, Auto-Mute, EMU32 Slave,
849          * Mute all codecs.
850          */
851         outl(0x0005a000, emu->port + HCFG);
852 
853         /* Disable 48Volt power to Audio Dock */
854         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_PWR, 0);
855 
856         /* ID, should read & 0x7f = 0x55. (Bit 7 is the IRQ bit) */
857         snd_emu1010_fpga_read(emu, EMU_HANA_ID, &reg);
858         dev_dbg(emu->card->dev, "reg1 = 0x%x\n", reg);
859         if ((reg & 0x3f) == 0x15) {
860                 /* FPGA netlist already present so clear it */
861                 /* Return to programming mode */
862 
863                 snd_emu1010_fpga_write(emu, EMU_HANA_FPGA_CONFIG, 0x02);
864         }
865         snd_emu1010_fpga_read(emu, EMU_HANA_ID, &reg);
866         dev_dbg(emu->card->dev, "reg2 = 0x%x\n", reg);
867         if ((reg & 0x3f) == 0x15) {
868                 /* FPGA failed to return to programming mode */
869                 dev_info(emu->card->dev,
870                          "emu1010: FPGA failed to return to programming mode\n");
871                 return -ENODEV;
872         }
873         dev_info(emu->card->dev, "emu1010: EMU_HANA_ID = 0x%x\n", reg);
874 
875         if (!emu->firmware) {
876                 const char *filename;
877                 switch (emu->card_capabilities->emu_model) {
878                 case EMU_MODEL_EMU1010:
879                         filename = HANA_FILENAME;
880                         break;
881                 case EMU_MODEL_EMU1010B:
882                         filename = EMU1010B_FILENAME;
883                         break;
884                 case EMU_MODEL_EMU1616:
885                         filename = EMU1010_NOTEBOOK_FILENAME;
886                         break;
887                 case EMU_MODEL_EMU0404:
888                         filename = EMU0404_FILENAME;
889                         break;
890                 default:
891                         return -ENODEV;
892                 }
893 
894                 err = request_firmware(&emu->firmware, filename, &emu->pci->dev);
895                 if (err != 0) {
896                         dev_info(emu->card->dev,
897                                  "emu1010: firmware: %s not found. Err = %d\n",
898                                  filename, err);
899                         return err;
900                 }
901                 dev_info(emu->card->dev,
902                          "emu1010: firmware file = %s, size = 0x%zx\n",
903                            filename, emu->firmware->size);
904         }
905 
906         err = snd_emu1010_load_firmware(emu, emu->firmware);
907         if (err != 0) {
908                 dev_info(emu->card->dev, "emu1010: Loading Firmware failed\n");
909                 return err;
910         }
911 
912         /* ID, should read & 0x7f = 0x55 when FPGA programmed. */
913         snd_emu1010_fpga_read(emu, EMU_HANA_ID, &reg);
914         if ((reg & 0x3f) != 0x15) {
915                 /* FPGA failed to be programmed */
916                 dev_info(emu->card->dev,
917                          "emu1010: Loading Hana Firmware file failed, reg = 0x%x\n",
918                          reg);
919                 return -ENODEV;
920         }
921 
922         dev_info(emu->card->dev, "emu1010: Hana Firmware loaded\n");
923         snd_emu1010_fpga_read(emu, EMU_HANA_MAJOR_REV, &tmp);
924         snd_emu1010_fpga_read(emu, EMU_HANA_MINOR_REV, &tmp2);
925         dev_info(emu->card->dev, "emu1010: Hana version: %u.%u\n", tmp, tmp2);
926         /* Enable 48Volt power to Audio Dock */
927         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_PWR, EMU_HANA_DOCK_PWR_ON);
928 
929         snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &reg);
930         dev_info(emu->card->dev, "emu1010: Card options = 0x%x\n", reg);
931         snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &reg);
932         dev_info(emu->card->dev, "emu1010: Card options = 0x%x\n", reg);
933         snd_emu1010_fpga_read(emu, EMU_HANA_OPTICAL_TYPE, &tmp);
934         /* Optical -> ADAT I/O  */
935         /* 0 : SPDIF
936          * 1 : ADAT
937          */
938         emu->emu1010.optical_in = 1; /* IN_ADAT */
939         emu->emu1010.optical_out = 1; /* IN_ADAT */
940         tmp = 0;
941         tmp = (emu->emu1010.optical_in ? EMU_HANA_OPTICAL_IN_ADAT : 0) |
942                 (emu->emu1010.optical_out ? EMU_HANA_OPTICAL_OUT_ADAT : 0);
943         snd_emu1010_fpga_write(emu, EMU_HANA_OPTICAL_TYPE, tmp);
944         snd_emu1010_fpga_read(emu, EMU_HANA_ADC_PADS, &tmp);
945         /* Set no attenuation on Audio Dock pads. */
946         snd_emu1010_fpga_write(emu, EMU_HANA_ADC_PADS, 0x00);
947         emu->emu1010.adc_pads = 0x00;
948         snd_emu1010_fpga_read(emu, EMU_HANA_DOCK_MISC, &tmp);
949         /* Unmute Audio dock DACs, Headphone source DAC-4. */
950         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_MISC, 0x30);
951         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2, 0x12);
952         snd_emu1010_fpga_read(emu, EMU_HANA_DAC_PADS, &tmp);
953         /* DAC PADs. */
954         snd_emu1010_fpga_write(emu, EMU_HANA_DAC_PADS, 0x0f);
955         emu->emu1010.dac_pads = 0x0f;
956         snd_emu1010_fpga_read(emu, EMU_HANA_DOCK_MISC, &tmp);
957         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_MISC, 0x30);
958         snd_emu1010_fpga_read(emu, EMU_HANA_SPDIF_MODE, &tmp);
959         /* SPDIF Format. Set Consumer mode, 24bit, copy enable */
960         snd_emu1010_fpga_write(emu, EMU_HANA_SPDIF_MODE, 0x10);
961         /* MIDI routing */
962         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_IN, 0x19);
963         /* Unknown. */
964         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_OUT, 0x0c);
965         /* IRQ Enable: All on */
966         /* snd_emu1010_fpga_write(emu, 0x09, 0x0f ); */
967         /* IRQ Enable: All off */
968         snd_emu1010_fpga_write(emu, EMU_HANA_IRQ_ENABLE, 0x00);
969 
970         snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &reg);
971         dev_info(emu->card->dev, "emu1010: Card options3 = 0x%x\n", reg);
972         /* Default WCLK set to 48kHz. */
973         snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, 0x00);
974         /* Word Clock source, Internal 48kHz x1 */
975         snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK, EMU_HANA_WCLOCK_INT_48K);
976         /* snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK, EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_4X); */
977         /* Audio Dock LEDs. */
978         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2, 0x12);
979 
980 #if 0
981         /* For 96kHz */
982         snd_emu1010_fpga_link_dst_src_write(emu,
983                 EMU_DST_ALICE2_EMU32_0, EMU_SRC_HAMOA_ADC_LEFT1);
984         snd_emu1010_fpga_link_dst_src_write(emu,
985                 EMU_DST_ALICE2_EMU32_1, EMU_SRC_HAMOA_ADC_RIGHT1);
986         snd_emu1010_fpga_link_dst_src_write(emu,
987                 EMU_DST_ALICE2_EMU32_4, EMU_SRC_HAMOA_ADC_LEFT2);
988         snd_emu1010_fpga_link_dst_src_write(emu,
989                 EMU_DST_ALICE2_EMU32_5, EMU_SRC_HAMOA_ADC_RIGHT2);
990 #endif
991 #if 0
992         /* For 192kHz */
993         snd_emu1010_fpga_link_dst_src_write(emu,
994                 EMU_DST_ALICE2_EMU32_0, EMU_SRC_HAMOA_ADC_LEFT1);
995         snd_emu1010_fpga_link_dst_src_write(emu,
996                 EMU_DST_ALICE2_EMU32_1, EMU_SRC_HAMOA_ADC_RIGHT1);
997         snd_emu1010_fpga_link_dst_src_write(emu,
998                 EMU_DST_ALICE2_EMU32_2, EMU_SRC_HAMOA_ADC_LEFT2);
999         snd_emu1010_fpga_link_dst_src_write(emu,
1000                 EMU_DST_ALICE2_EMU32_3, EMU_SRC_HAMOA_ADC_RIGHT2);
1001         snd_emu1010_fpga_link_dst_src_write(emu,
1002                 EMU_DST_ALICE2_EMU32_4, EMU_SRC_HAMOA_ADC_LEFT3);
1003         snd_emu1010_fpga_link_dst_src_write(emu,
1004                 EMU_DST_ALICE2_EMU32_5, EMU_SRC_HAMOA_ADC_RIGHT3);
1005         snd_emu1010_fpga_link_dst_src_write(emu,
1006                 EMU_DST_ALICE2_EMU32_6, EMU_SRC_HAMOA_ADC_LEFT4);
1007         snd_emu1010_fpga_link_dst_src_write(emu,
1008                 EMU_DST_ALICE2_EMU32_7, EMU_SRC_HAMOA_ADC_RIGHT4);
1009 #endif
1010 #if 1
1011         /* For 48kHz */
1012         snd_emu1010_fpga_link_dst_src_write(emu,
1013                 EMU_DST_ALICE2_EMU32_0, EMU_SRC_DOCK_MIC_A1);
1014         snd_emu1010_fpga_link_dst_src_write(emu,
1015                 EMU_DST_ALICE2_EMU32_1, EMU_SRC_DOCK_MIC_B1);
1016         snd_emu1010_fpga_link_dst_src_write(emu,
1017                 EMU_DST_ALICE2_EMU32_2, EMU_SRC_HAMOA_ADC_LEFT2);
1018         snd_emu1010_fpga_link_dst_src_write(emu,
1019                 EMU_DST_ALICE2_EMU32_3, EMU_SRC_HAMOA_ADC_LEFT2);
1020         snd_emu1010_fpga_link_dst_src_write(emu,
1021                 EMU_DST_ALICE2_EMU32_4, EMU_SRC_DOCK_ADC1_LEFT1);
1022         snd_emu1010_fpga_link_dst_src_write(emu,
1023                 EMU_DST_ALICE2_EMU32_5, EMU_SRC_DOCK_ADC1_RIGHT1);
1024         snd_emu1010_fpga_link_dst_src_write(emu,
1025                 EMU_DST_ALICE2_EMU32_6, EMU_SRC_DOCK_ADC2_LEFT1);
1026         snd_emu1010_fpga_link_dst_src_write(emu,
1027                 EMU_DST_ALICE2_EMU32_7, EMU_SRC_DOCK_ADC2_RIGHT1);
1028         /* Pavel Hofman - setting defaults for 8 more capture channels
1029          * Defaults only, users will set their own values anyways, let's
1030          * just copy/paste.
1031          */
1032 
1033         snd_emu1010_fpga_link_dst_src_write(emu,
1034                 EMU_DST_ALICE2_EMU32_8, EMU_SRC_DOCK_MIC_A1);
1035         snd_emu1010_fpga_link_dst_src_write(emu,
1036                 EMU_DST_ALICE2_EMU32_9, EMU_SRC_DOCK_MIC_B1);
1037         snd_emu1010_fpga_link_dst_src_write(emu,
1038                 EMU_DST_ALICE2_EMU32_A, EMU_SRC_HAMOA_ADC_LEFT2);
1039         snd_emu1010_fpga_link_dst_src_write(emu,
1040                 EMU_DST_ALICE2_EMU32_B, EMU_SRC_HAMOA_ADC_LEFT2);
1041         snd_emu1010_fpga_link_dst_src_write(emu,
1042                 EMU_DST_ALICE2_EMU32_C, EMU_SRC_DOCK_ADC1_LEFT1);
1043         snd_emu1010_fpga_link_dst_src_write(emu,
1044                 EMU_DST_ALICE2_EMU32_D, EMU_SRC_DOCK_ADC1_RIGHT1);
1045         snd_emu1010_fpga_link_dst_src_write(emu,
1046                 EMU_DST_ALICE2_EMU32_E, EMU_SRC_DOCK_ADC2_LEFT1);
1047         snd_emu1010_fpga_link_dst_src_write(emu,
1048                 EMU_DST_ALICE2_EMU32_F, EMU_SRC_DOCK_ADC2_RIGHT1);
1049 #endif
1050 #if 0
1051         /* Original */
1052         snd_emu1010_fpga_link_dst_src_write(emu,
1053                 EMU_DST_ALICE2_EMU32_4, EMU_SRC_HANA_ADAT);
1054         snd_emu1010_fpga_link_dst_src_write(emu,
1055                 EMU_DST_ALICE2_EMU32_5, EMU_SRC_HANA_ADAT + 1);
1056         snd_emu1010_fpga_link_dst_src_write(emu,
1057                 EMU_DST_ALICE2_EMU32_6, EMU_SRC_HANA_ADAT + 2);
1058         snd_emu1010_fpga_link_dst_src_write(emu,
1059                 EMU_DST_ALICE2_EMU32_7, EMU_SRC_HANA_ADAT + 3);
1060         snd_emu1010_fpga_link_dst_src_write(emu,
1061                 EMU_DST_ALICE2_EMU32_8, EMU_SRC_HANA_ADAT + 4);
1062         snd_emu1010_fpga_link_dst_src_write(emu,
1063                 EMU_DST_ALICE2_EMU32_9, EMU_SRC_HANA_ADAT + 5);
1064         snd_emu1010_fpga_link_dst_src_write(emu,
1065                 EMU_DST_ALICE2_EMU32_A, EMU_SRC_HANA_ADAT + 6);
1066         snd_emu1010_fpga_link_dst_src_write(emu,
1067                 EMU_DST_ALICE2_EMU32_B, EMU_SRC_HANA_ADAT + 7);
1068         snd_emu1010_fpga_link_dst_src_write(emu,
1069                 EMU_DST_ALICE2_EMU32_C, EMU_SRC_DOCK_MIC_A1);
1070         snd_emu1010_fpga_link_dst_src_write(emu,
1071                 EMU_DST_ALICE2_EMU32_D, EMU_SRC_DOCK_MIC_B1);
1072         snd_emu1010_fpga_link_dst_src_write(emu,
1073                 EMU_DST_ALICE2_EMU32_E, EMU_SRC_HAMOA_ADC_LEFT2);
1074         snd_emu1010_fpga_link_dst_src_write(emu,
1075                 EMU_DST_ALICE2_EMU32_F, EMU_SRC_HAMOA_ADC_LEFT2);
1076 #endif
1077         for (i = 0; i < 0x20; i++) {
1078                 /* AudioDock Elink <- Silence */
1079                 snd_emu1010_fpga_link_dst_src_write(emu, 0x0100 + i, EMU_SRC_SILENCE);
1080         }
1081         for (i = 0; i < 4; i++) {
1082                 /* Hana SPDIF Out <- Silence */
1083                 snd_emu1010_fpga_link_dst_src_write(emu, 0x0200 + i, EMU_SRC_SILENCE);
1084         }
1085         for (i = 0; i < 7; i++) {
1086                 /* Hamoa DAC <- Silence */
1087                 snd_emu1010_fpga_link_dst_src_write(emu, 0x0300 + i, EMU_SRC_SILENCE);
1088         }
1089         for (i = 0; i < 7; i++) {
1090                 /* Hana ADAT Out <- Silence */
1091                 snd_emu1010_fpga_link_dst_src_write(emu, EMU_DST_HANA_ADAT + i, EMU_SRC_SILENCE);
1092         }
1093         snd_emu1010_fpga_link_dst_src_write(emu,
1094                 EMU_DST_ALICE_I2S0_LEFT, EMU_SRC_DOCK_ADC1_LEFT1);
1095         snd_emu1010_fpga_link_dst_src_write(emu,
1096                 EMU_DST_ALICE_I2S0_RIGHT, EMU_SRC_DOCK_ADC1_RIGHT1);
1097         snd_emu1010_fpga_link_dst_src_write(emu,
1098                 EMU_DST_ALICE_I2S1_LEFT, EMU_SRC_DOCK_ADC2_LEFT1);
1099         snd_emu1010_fpga_link_dst_src_write(emu,
1100                 EMU_DST_ALICE_I2S1_RIGHT, EMU_SRC_DOCK_ADC2_RIGHT1);
1101         snd_emu1010_fpga_link_dst_src_write(emu,
1102                 EMU_DST_ALICE_I2S2_LEFT, EMU_SRC_DOCK_ADC3_LEFT1);
1103         snd_emu1010_fpga_link_dst_src_write(emu,
1104                 EMU_DST_ALICE_I2S2_RIGHT, EMU_SRC_DOCK_ADC3_RIGHT1);
1105         snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, 0x01); /* Unmute all */
1106 
1107         snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &tmp);
1108 
1109         /* AC97 1.03, Any 32Meg of 2Gig address, Auto-Mute, EMU32 Slave,
1110          * Lock Sound Memory Cache, Lock Tank Memory Cache,
1111          * Mute all codecs.
1112          */
1113         outl(0x0000a000, emu->port + HCFG);
1114         /* AC97 1.03, Any 32Meg of 2Gig address, Auto-Mute, EMU32 Slave,
1115          * Lock Sound Memory Cache, Lock Tank Memory Cache,
1116          * Un-Mute all codecs.
1117          */
1118         outl(0x0000a001, emu->port + HCFG);
1119 
1120         /* Initial boot complete. Now patches */
1121 
1122         snd_emu1010_fpga_read(emu, EMU_HANA_OPTION_CARDS, &tmp);
1123         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_IN, 0x19); /* MIDI Route */
1124         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_OUT, 0x0c); /* Unknown */
1125         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_IN, 0x19); /* MIDI Route */
1126         snd_emu1010_fpga_write(emu, EMU_HANA_MIDI_OUT, 0x0c); /* Unknown */
1127         snd_emu1010_fpga_read(emu, EMU_HANA_SPDIF_MODE, &tmp);
1128         snd_emu1010_fpga_write(emu, EMU_HANA_SPDIF_MODE, 0x10); /* SPDIF Format spdif  (or 0x11 for aes/ebu) */
1129 
1130         /* Start Micro/Audio Dock firmware loader thread */
1131         if (!emu->emu1010.firmware_thread) {
1132                 emu->emu1010.firmware_thread =
1133                         kthread_create(emu1010_firmware_thread, emu,
1134                                        "emu1010_firmware");
1135                 wake_up_process(emu->emu1010.firmware_thread);
1136         }
1137 
1138 #if 0
1139         snd_emu1010_fpga_link_dst_src_write(emu,
1140                 EMU_DST_HAMOA_DAC_LEFT1, EMU_SRC_ALICE_EMU32B + 2); /* ALICE2 bus 0xa2 */
1141         snd_emu1010_fpga_link_dst_src_write(emu,
1142                 EMU_DST_HAMOA_DAC_RIGHT1, EMU_SRC_ALICE_EMU32B + 3); /* ALICE2 bus 0xa3 */
1143         snd_emu1010_fpga_link_dst_src_write(emu,
1144                 EMU_DST_HANA_SPDIF_LEFT1, EMU_SRC_ALICE_EMU32A + 2); /* ALICE2 bus 0xb2 */
1145         snd_emu1010_fpga_link_dst_src_write(emu,
1146                 EMU_DST_HANA_SPDIF_RIGHT1, EMU_SRC_ALICE_EMU32A + 3); /* ALICE2 bus 0xb3 */
1147 #endif
1148         /* Default outputs */
1149         if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616) {
1150                 /* 1616(M) cardbus default outputs */
1151                 /* ALICE2 bus 0xa0 */
1152                 snd_emu1010_fpga_link_dst_src_write(emu,
1153                         EMU_DST_DOCK_DAC1_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1154                 emu->emu1010.output_source[0] = 17;
1155                 snd_emu1010_fpga_link_dst_src_write(emu,
1156                         EMU_DST_DOCK_DAC1_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1157                 emu->emu1010.output_source[1] = 18;
1158                 snd_emu1010_fpga_link_dst_src_write(emu,
1159                         EMU_DST_DOCK_DAC2_LEFT1, EMU_SRC_ALICE_EMU32A + 2);
1160                 emu->emu1010.output_source[2] = 19;
1161                 snd_emu1010_fpga_link_dst_src_write(emu,
1162                         EMU_DST_DOCK_DAC2_RIGHT1, EMU_SRC_ALICE_EMU32A + 3);
1163                 emu->emu1010.output_source[3] = 20;
1164                 snd_emu1010_fpga_link_dst_src_write(emu,
1165                         EMU_DST_DOCK_DAC3_LEFT1, EMU_SRC_ALICE_EMU32A + 4);
1166                 emu->emu1010.output_source[4] = 21;
1167                 snd_emu1010_fpga_link_dst_src_write(emu,
1168                         EMU_DST_DOCK_DAC3_RIGHT1, EMU_SRC_ALICE_EMU32A + 5);
1169                 emu->emu1010.output_source[5] = 22;
1170                 /* ALICE2 bus 0xa0 */
1171                 snd_emu1010_fpga_link_dst_src_write(emu,
1172                         EMU_DST_MANA_DAC_LEFT, EMU_SRC_ALICE_EMU32A + 0);
1173                 emu->emu1010.output_source[16] = 17;
1174                 snd_emu1010_fpga_link_dst_src_write(emu,
1175                         EMU_DST_MANA_DAC_RIGHT, EMU_SRC_ALICE_EMU32A + 1);
1176                 emu->emu1010.output_source[17] = 18;
1177         } else {
1178                 /* ALICE2 bus 0xa0 */
1179                 snd_emu1010_fpga_link_dst_src_write(emu,
1180                         EMU_DST_DOCK_DAC1_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1181                 emu->emu1010.output_source[0] = 21;
1182                 snd_emu1010_fpga_link_dst_src_write(emu,
1183                         EMU_DST_DOCK_DAC1_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1184                 emu->emu1010.output_source[1] = 22;
1185                 snd_emu1010_fpga_link_dst_src_write(emu,
1186                         EMU_DST_DOCK_DAC2_LEFT1, EMU_SRC_ALICE_EMU32A + 2);
1187                 emu->emu1010.output_source[2] = 23;
1188                 snd_emu1010_fpga_link_dst_src_write(emu,
1189                         EMU_DST_DOCK_DAC2_RIGHT1, EMU_SRC_ALICE_EMU32A + 3);
1190                 emu->emu1010.output_source[3] = 24;
1191                 snd_emu1010_fpga_link_dst_src_write(emu,
1192                         EMU_DST_DOCK_DAC3_LEFT1, EMU_SRC_ALICE_EMU32A + 4);
1193                 emu->emu1010.output_source[4] = 25;
1194                 snd_emu1010_fpga_link_dst_src_write(emu,
1195                         EMU_DST_DOCK_DAC3_RIGHT1, EMU_SRC_ALICE_EMU32A + 5);
1196                 emu->emu1010.output_source[5] = 26;
1197                 snd_emu1010_fpga_link_dst_src_write(emu,
1198                         EMU_DST_DOCK_DAC4_LEFT1, EMU_SRC_ALICE_EMU32A + 6);
1199                 emu->emu1010.output_source[6] = 27;
1200                 snd_emu1010_fpga_link_dst_src_write(emu,
1201                         EMU_DST_DOCK_DAC4_RIGHT1, EMU_SRC_ALICE_EMU32A + 7);
1202                 emu->emu1010.output_source[7] = 28;
1203                 /* ALICE2 bus 0xa0 */
1204                 snd_emu1010_fpga_link_dst_src_write(emu,
1205                         EMU_DST_DOCK_PHONES_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1206                 emu->emu1010.output_source[8] = 21;
1207                 snd_emu1010_fpga_link_dst_src_write(emu,
1208                         EMU_DST_DOCK_PHONES_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1209                 emu->emu1010.output_source[9] = 22;
1210                 /* ALICE2 bus 0xa0 */
1211                 snd_emu1010_fpga_link_dst_src_write(emu,
1212                         EMU_DST_DOCK_SPDIF_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1213                 emu->emu1010.output_source[10] = 21;
1214                 snd_emu1010_fpga_link_dst_src_write(emu,
1215                         EMU_DST_DOCK_SPDIF_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1216                 emu->emu1010.output_source[11] = 22;
1217                 /* ALICE2 bus 0xa0 */
1218                 snd_emu1010_fpga_link_dst_src_write(emu,
1219                         EMU_DST_HANA_SPDIF_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1220                 emu->emu1010.output_source[12] = 21;
1221                 snd_emu1010_fpga_link_dst_src_write(emu,
1222                         EMU_DST_HANA_SPDIF_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1223                 emu->emu1010.output_source[13] = 22;
1224                 /* ALICE2 bus 0xa0 */
1225                 snd_emu1010_fpga_link_dst_src_write(emu,
1226                         EMU_DST_HAMOA_DAC_LEFT1, EMU_SRC_ALICE_EMU32A + 0);
1227                 emu->emu1010.output_source[14] = 21;
1228                 snd_emu1010_fpga_link_dst_src_write(emu,
1229                         EMU_DST_HAMOA_DAC_RIGHT1, EMU_SRC_ALICE_EMU32A + 1);
1230                 emu->emu1010.output_source[15] = 22;
1231                 /* ALICE2 bus 0xa0 */
1232                 snd_emu1010_fpga_link_dst_src_write(emu,
1233                         EMU_DST_HANA_ADAT, EMU_SRC_ALICE_EMU32A + 0);
1234                 emu->emu1010.output_source[16] = 21;
1235                 snd_emu1010_fpga_link_dst_src_write(emu,
1236                         EMU_DST_HANA_ADAT + 1, EMU_SRC_ALICE_EMU32A + 1);
1237                 emu->emu1010.output_source[17] = 22;
1238                 snd_emu1010_fpga_link_dst_src_write(emu,
1239                         EMU_DST_HANA_ADAT + 2, EMU_SRC_ALICE_EMU32A + 2);
1240                 emu->emu1010.output_source[18] = 23;
1241                 snd_emu1010_fpga_link_dst_src_write(emu,
1242                         EMU_DST_HANA_ADAT + 3, EMU_SRC_ALICE_EMU32A + 3);
1243                 emu->emu1010.output_source[19] = 24;
1244                 snd_emu1010_fpga_link_dst_src_write(emu,
1245                         EMU_DST_HANA_ADAT + 4, EMU_SRC_ALICE_EMU32A + 4);
1246                 emu->emu1010.output_source[20] = 25;
1247                 snd_emu1010_fpga_link_dst_src_write(emu,
1248                         EMU_DST_HANA_ADAT + 5, EMU_SRC_ALICE_EMU32A + 5);
1249                 emu->emu1010.output_source[21] = 26;
1250                 snd_emu1010_fpga_link_dst_src_write(emu,
1251                         EMU_DST_HANA_ADAT + 6, EMU_SRC_ALICE_EMU32A + 6);
1252                 emu->emu1010.output_source[22] = 27;
1253                 snd_emu1010_fpga_link_dst_src_write(emu,
1254                         EMU_DST_HANA_ADAT + 7, EMU_SRC_ALICE_EMU32A + 7);
1255                 emu->emu1010.output_source[23] = 28;
1256         }
1257         /* TEMP: Select SPDIF in/out */
1258         /* snd_emu1010_fpga_write(emu, EMU_HANA_OPTICAL_TYPE, 0x0); */ /* Output spdif */
1259 
1260         /* TEMP: Select 48kHz SPDIF out */
1261         snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, 0x0); /* Mute all */
1262         snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, 0x0); /* Default fallback clock 48kHz */
1263         /* Word Clock source, Internal 48kHz x1 */
1264         snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK, EMU_HANA_WCLOCK_INT_48K);
1265         /* snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK, EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_4X); */
1266         emu->emu1010.internal_clock = 1; /* 48000 */
1267         snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2, 0x12); /* Set LEDs on Audio Dock */
1268         snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, 0x1); /* Unmute all */
1269         /* snd_emu1010_fpga_write(emu, 0x7, 0x0); */ /* Mute all */
1270         /* snd_emu1010_fpga_write(emu, 0x7, 0x1); */ /* Unmute all */
1271         /* snd_emu1010_fpga_write(emu, 0xe, 0x12); */ /* Set LEDs on Audio Dock */
1272 
1273         return 0;
1274 }
1275 /*
1276  *  Create the EMU10K1 instance
1277  */
1278 
1279 #ifdef CONFIG_PM_SLEEP
1280 static int alloc_pm_buffer(struct snd_emu10k1 *emu);
1281 static void free_pm_buffer(struct snd_emu10k1 *emu);
1282 #endif
1283 
1284 static int snd_emu10k1_free(struct snd_emu10k1 *emu)
1285 {
1286         if (emu->port) {        /* avoid access to already used hardware */
1287                 snd_emu10k1_fx8010_tram_setup(emu, 0);
1288                 snd_emu10k1_done(emu);
1289                 snd_emu10k1_free_efx(emu);
1290         }
1291         if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1010) {
1292                 /* Disable 48Volt power to Audio Dock */
1293                 snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_PWR, 0);
1294         }
1295         if (emu->emu1010.firmware_thread)
1296                 kthread_stop(emu->emu1010.firmware_thread);
1297         release_firmware(emu->firmware);
1298         release_firmware(emu->dock_fw);
1299         if (emu->irq >= 0)
1300                 free_irq(emu->irq, emu);
1301         /* remove reserved page */
1302         if (emu->reserved_page) {
1303                 snd_emu10k1_synth_free(emu,
1304                         (struct snd_util_memblk *)emu->reserved_page);
1305                 emu->reserved_page = NULL;
1306         }
1307         snd_util_memhdr_free(emu->memhdr);
1308         if (emu->silent_page.area)
1309                 snd_dma_free_pages(&emu->silent_page);
1310         if (emu->ptb_pages.area)
1311                 snd_dma_free_pages(&emu->ptb_pages);
1312         vfree(emu->page_ptr_table);
1313         vfree(emu->page_addr_table);
1314 #ifdef CONFIG_PM_SLEEP
1315         free_pm_buffer(emu);
1316 #endif
1317         if (emu->port)
1318                 pci_release_regions(emu->pci);
1319         if (emu->card_capabilities->ca0151_chip) /* P16V */
1320                 snd_p16v_free(emu);
1321         pci_disable_device(emu->pci);
1322         kfree(emu);
1323         return 0;
1324 }
1325 
1326 static int snd_emu10k1_dev_free(struct snd_device *device)
1327 {
1328         struct snd_emu10k1 *emu = device->device_data;
1329         return snd_emu10k1_free(emu);
1330 }
1331 
1332 static struct snd_emu_chip_details emu_chip_details[] = {
1333         /* Audigy4 (Not PRO) SB0610 */
1334         /* Tested by James@superbug.co.uk 4th April 2006 */
1335         /* A_IOCFG bits
1336          * Output
1337          * 0: ?
1338          * 1: ?
1339          * 2: ?
1340          * 3: 0 - Digital Out, 1 - Line in
1341          * 4: ?
1342          * 5: ?
1343          * 6: ?
1344          * 7: ?
1345          * Input
1346          * 8: ?
1347          * 9: ?
1348          * A: Green jack sense (Front)
1349          * B: ?
1350          * C: Black jack sense (Rear/Side Right)
1351          * D: Yellow jack sense (Center/LFE/Side Left)
1352          * E: ?
1353          * F: ?
1354          *
1355          * Digital Out/Line in switch using A_IOCFG bit 3 (0x08)
1356          * 0 - Digital Out
1357          * 1 - Line in
1358          */
1359         /* Mic input not tested.
1360          * Analog CD input not tested
1361          * Digital Out not tested.
1362          * Line in working.
1363          * Audio output 5.1 working. Side outputs not working.
1364          */
1365         /* DSP: CA10300-IAT LF
1366          * DAC: Cirrus Logic CS4382-KQZ
1367          * ADC: Philips 1361T
1368          * AC97: Sigmatel STAC9750
1369          * CA0151: None
1370          */
1371         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x10211102,
1372          .driver = "Audigy2", .name = "SB Audigy 4 [SB0610]",
1373          .id = "Audigy2",
1374          .emu10k2_chip = 1,
1375          .ca0108_chip = 1,
1376          .spk71 = 1,
1377          .adc_1361t = 1,  /* 24 bit capture instead of 16bit */
1378          .ac97_chip = 1} ,
1379         /* Audigy 2 Value AC3 out does not work yet.
1380          * Need to find out how to turn off interpolators.
1381          */
1382         /* Tested by James@superbug.co.uk 3rd July 2005 */
1383         /* DSP: CA0108-IAT
1384          * DAC: CS4382-KQ
1385          * ADC: Philips 1361T
1386          * AC97: STAC9750
1387          * CA0151: None
1388          */
1389         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x10011102,
1390          .driver = "Audigy2", .name = "SB Audigy 2 Value [SB0400]",
1391          .id = "Audigy2",
1392          .emu10k2_chip = 1,
1393          .ca0108_chip = 1,
1394          .spk71 = 1,
1395          .ac97_chip = 1} ,
1396         /* Audigy 2 ZS Notebook Cardbus card.*/
1397         /* Tested by James@superbug.co.uk 6th November 2006 */
1398         /* Audio output 7.1/Headphones working.
1399          * Digital output working. (AC3 not checked, only PCM)
1400          * Audio Mic/Line inputs working.
1401          * Digital input not tested.
1402          */
1403         /* DSP: Tina2
1404          * DAC: Wolfson WM8768/WM8568
1405          * ADC: Wolfson WM8775
1406          * AC97: None
1407          * CA0151: None
1408          */
1409         /* Tested by James@superbug.co.uk 4th April 2006 */
1410         /* A_IOCFG bits
1411          * Output
1412          * 0: Not Used
1413          * 1: 0 = Mute all the 7.1 channel out. 1 = unmute.
1414          * 2: Analog input 0 = line in, 1 = mic in
1415          * 3: Not Used
1416          * 4: Digital output 0 = off, 1 = on.
1417          * 5: Not Used
1418          * 6: Not Used
1419          * 7: Not Used
1420          * Input
1421          *      All bits 1 (0x3fxx) means nothing plugged in.
1422          * 8-9: 0 = Line in/Mic, 2 = Optical in, 3 = Nothing.
1423          * A-B: 0 = Headphones, 2 = Optical out, 3 = Nothing.
1424          * C-D: 2 = Front/Rear/etc, 3 = nothing.
1425          * E-F: Always 0
1426          *
1427          */
1428         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x20011102,
1429          .driver = "Audigy2", .name = "Audigy 2 ZS Notebook [SB0530]",
1430          .id = "Audigy2",
1431          .emu10k2_chip = 1,
1432          .ca0108_chip = 1,
1433          .ca_cardbus_chip = 1,
1434          .spi_dac = 1,
1435          .i2c_adc = 1,
1436          .spk71 = 1} ,
1437         /* Tested by James@superbug.co.uk 4th Nov 2007. */
1438         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x42011102,
1439          .driver = "Audigy2", .name = "E-mu 1010 Notebook [MAEM8950]",
1440          .id = "EMU1010",
1441          .emu10k2_chip = 1,
1442          .ca0108_chip = 1,
1443          .ca_cardbus_chip = 1,
1444          .spk71 = 1 ,
1445          .emu_model = EMU_MODEL_EMU1616},
1446         /* Tested by James@superbug.co.uk 4th Nov 2007. */
1447         /* This is MAEM8960, 0202 is MAEM 8980 */
1448         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x40041102,
1449          .driver = "Audigy2", .name = "E-mu 1010b PCI [MAEM8960]",
1450          .id = "EMU1010",
1451          .emu10k2_chip = 1,
1452          .ca0108_chip = 1,
1453          .spk71 = 1,
1454          .emu_model = EMU_MODEL_EMU1010B}, /* EMU 1010 new revision */
1455         /* Tested by Maxim Kachur <mcdebugger@duganet.ru> 17th Oct 2012. */
1456         /* This is MAEM8986, 0202 is MAEM8980 */
1457         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x40071102,
1458          .driver = "Audigy2", .name = "E-mu 1010 PCIe [MAEM8986]",
1459          .id = "EMU1010",
1460          .emu10k2_chip = 1,
1461          .ca0108_chip = 1,
1462          .spk71 = 1,
1463          .emu_model = EMU_MODEL_EMU1010B}, /* EMU 1010 PCIe */
1464         /* Tested by James@superbug.co.uk 8th July 2005. */
1465         /* This is MAEM8810, 0202 is MAEM8820 */
1466         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x40011102,
1467          .driver = "Audigy2", .name = "E-mu 1010 [MAEM8810]",
1468          .id = "EMU1010",
1469          .emu10k2_chip = 1,
1470          .ca0102_chip = 1,
1471          .spk71 = 1,
1472          .emu_model = EMU_MODEL_EMU1010}, /* EMU 1010 old revision */
1473         /* EMU0404b */
1474         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x40021102,
1475          .driver = "Audigy2", .name = "E-mu 0404b PCI [MAEM8852]",
1476          .id = "EMU0404",
1477          .emu10k2_chip = 1,
1478          .ca0108_chip = 1,
1479          .spk71 = 1,
1480          .emu_model = EMU_MODEL_EMU0404}, /* EMU 0404 new revision */
1481         /* Tested by James@superbug.co.uk 20-3-2007. */
1482         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x40021102,
1483          .driver = "Audigy2", .name = "E-mu 0404 [MAEM8850]",
1484          .id = "EMU0404",
1485          .emu10k2_chip = 1,
1486          .ca0102_chip = 1,
1487          .spk71 = 1,
1488          .emu_model = EMU_MODEL_EMU0404}, /* EMU 0404 */
1489         /* EMU0404 PCIe */
1490         {.vendor = 0x1102, .device = 0x0008, .subsystem = 0x40051102,
1491          .driver = "Audigy2", .name = "E-mu 0404 PCIe [MAEM8984]",
1492          .id = "EMU0404",
1493          .emu10k2_chip = 1,
1494          .ca0108_chip = 1,
1495          .spk71 = 1,
1496          .emu_model = EMU_MODEL_EMU0404}, /* EMU 0404 PCIe ver_03 */
1497         /* Note that all E-mu cards require kernel 2.6 or newer. */
1498         {.vendor = 0x1102, .device = 0x0008,
1499          .driver = "Audigy2", .name = "SB Audigy 2 Value [Unknown]",
1500          .id = "Audigy2",
1501          .emu10k2_chip = 1,
1502          .ca0108_chip = 1,
1503          .ac97_chip = 1} ,
1504         /* Tested by James@superbug.co.uk 3rd July 2005 */
1505         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20071102,
1506          .driver = "Audigy2", .name = "SB Audigy 4 PRO [SB0380]",
1507          .id = "Audigy2",
1508          .emu10k2_chip = 1,
1509          .ca0102_chip = 1,
1510          .ca0151_chip = 1,
1511          .spk71 = 1,
1512          .spdif_bug = 1,
1513          .ac97_chip = 1} ,
1514         /* Tested by shane-alsa@cm.nu 5th Nov 2005 */
1515         /* The 0x20061102 does have SB0350 written on it
1516          * Just like 0x20021102
1517          */
1518         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20061102,
1519          .driver = "Audigy2", .name = "SB Audigy 2 [SB0350b]",
1520          .id = "Audigy2",
1521          .emu10k2_chip = 1,
1522          .ca0102_chip = 1,
1523          .ca0151_chip = 1,
1524          .spk71 = 1,
1525          .spdif_bug = 1,
1526          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1527          .ac97_chip = 1} ,
1528         /* 0x20051102 also has SB0350 written on it, treated as Audigy 2 ZS by
1529            Creative's Windows driver */
1530         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20051102,
1531          .driver = "Audigy2", .name = "SB Audigy 2 ZS [SB0350a]",
1532          .id = "Audigy2",
1533          .emu10k2_chip = 1,
1534          .ca0102_chip = 1,
1535          .ca0151_chip = 1,
1536          .spk71 = 1,
1537          .spdif_bug = 1,
1538          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1539          .ac97_chip = 1} ,
1540         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20021102,
1541          .driver = "Audigy2", .name = "SB Audigy 2 ZS [SB0350]",
1542          .id = "Audigy2",
1543          .emu10k2_chip = 1,
1544          .ca0102_chip = 1,
1545          .ca0151_chip = 1,
1546          .spk71 = 1,
1547          .spdif_bug = 1,
1548          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1549          .ac97_chip = 1} ,
1550         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x20011102,
1551          .driver = "Audigy2", .name = "SB Audigy 2 ZS [SB0360]",
1552          .id = "Audigy2",
1553          .emu10k2_chip = 1,
1554          .ca0102_chip = 1,
1555          .ca0151_chip = 1,
1556          .spk71 = 1,
1557          .spdif_bug = 1,
1558          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1559          .ac97_chip = 1} ,
1560         /* Audigy 2 */
1561         /* Tested by James@superbug.co.uk 3rd July 2005 */
1562         /* DSP: CA0102-IAT
1563          * DAC: CS4382-KQ
1564          * ADC: Philips 1361T
1565          * AC97: STAC9721
1566          * CA0151: Yes
1567          */
1568         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10071102,
1569          .driver = "Audigy2", .name = "SB Audigy 2 [SB0240]",
1570          .id = "Audigy2",
1571          .emu10k2_chip = 1,
1572          .ca0102_chip = 1,
1573          .ca0151_chip = 1,
1574          .spk71 = 1,
1575          .spdif_bug = 1,
1576          .adc_1361t = 1,  /* 24 bit capture instead of 16bit */
1577          .ac97_chip = 1} ,
1578         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10051102,
1579          .driver = "Audigy2", .name = "Audigy 2 Platinum EX [SB0280]",
1580          .id = "Audigy2",
1581          .emu10k2_chip = 1,
1582          .ca0102_chip = 1,
1583          .ca0151_chip = 1,
1584          .spk71 = 1,
1585          .spdif_bug = 1} ,
1586         /* Dell OEM/Creative Labs Audigy 2 ZS */
1587         /* See ALSA bug#1365 */
1588         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10031102,
1589          .driver = "Audigy2", .name = "SB Audigy 2 ZS [SB0353]",
1590          .id = "Audigy2",
1591          .emu10k2_chip = 1,
1592          .ca0102_chip = 1,
1593          .ca0151_chip = 1,
1594          .spk71 = 1,
1595          .spdif_bug = 1,
1596          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1597          .ac97_chip = 1} ,
1598         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x10021102,
1599          .driver = "Audigy2", .name = "SB Audigy 2 Platinum [SB0240P]",
1600          .id = "Audigy2",
1601          .emu10k2_chip = 1,
1602          .ca0102_chip = 1,
1603          .ca0151_chip = 1,
1604          .spk71 = 1,
1605          .spdif_bug = 1,
1606          .invert_shared_spdif = 1,      /* digital/analog switch swapped */
1607          .adc_1361t = 1,  /* 24 bit capture instead of 16bit. Fixes ALSA bug#324 */
1608          .ac97_chip = 1} ,
1609         {.vendor = 0x1102, .device = 0x0004, .revision = 0x04,
1610          .driver = "Audigy2", .name = "SB Audigy 2 [Unknown]",
1611          .id = "Audigy2",
1612          .emu10k2_chip = 1,
1613          .ca0102_chip = 1,
1614          .ca0151_chip = 1,
1615          .spdif_bug = 1,
1616          .ac97_chip = 1} ,
1617         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00531102,
1618          .driver = "Audigy", .name = "SB Audigy 1 [SB0092]",
1619          .id = "Audigy",
1620          .emu10k2_chip = 1,
1621          .ca0102_chip = 1,
1622          .ac97_chip = 1} ,
1623         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00521102,
1624          .driver = "Audigy", .name = "SB Audigy 1 ES [SB0160]",
1625          .id = "Audigy",
1626          .emu10k2_chip = 1,
1627          .ca0102_chip = 1,
1628          .spdif_bug = 1,
1629          .ac97_chip = 1} ,
1630         {.vendor = 0x1102, .device = 0x0004, .subsystem = 0x00511102,
1631          .driver = "Audigy", .name = "SB Audigy 1 [SB0090]",
1632          .id = "Audigy",
1633          .emu10k2_chip = 1,
1634          .ca0102_chip = 1,
1635          .ac97_chip = 1} ,
1636         {.vendor = 0x1102, .device = 0x0004,
1637          .driver = "Audigy", .name = "Audigy 1 [Unknown]",
1638          .id = "Audigy",
1639          .emu10k2_chip = 1,
1640          .ca0102_chip = 1,
1641          .ac97_chip = 1} ,
1642         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x100a1102,
1643          .driver = "EMU10K1", .name = "SB Live! 5.1 [SB0220]",
1644          .id = "Live",
1645          .emu10k1_chip = 1,
1646          .ac97_chip = 1,
1647          .sblive51 = 1} ,
1648         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806b1102,
1649          .driver = "EMU10K1", .name = "SB Live! [SB0105]",
1650          .id = "Live",
1651          .emu10k1_chip = 1,
1652          .ac97_chip = 1,
1653          .sblive51 = 1} ,
1654         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x806a1102,
1655          .driver = "EMU10K1", .name = "SB Live! Value [SB0103]",
1656          .id = "Live",
1657          .emu10k1_chip = 1,
1658          .ac97_chip = 1,
1659          .sblive51 = 1} ,
1660         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80691102,
1661          .driver = "EMU10K1", .name = "SB Live! Value [SB0101]",
1662          .id = "Live",
1663          .emu10k1_chip = 1,
1664          .ac97_chip = 1,
1665          .sblive51 = 1} ,
1666         /* Tested by ALSA bug#1680 26th December 2005 */
1667         /* note: It really has SB0220 written on the card, */
1668         /* but it's SB0228 according to kx.inf */
1669         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80661102,
1670          .driver = "EMU10K1", .name = "SB Live! 5.1 Dell OEM [SB0228]",
1671          .id = "Live",
1672          .emu10k1_chip = 1,
1673          .ac97_chip = 1,
1674          .sblive51 = 1} ,
1675         /* Tested by Thomas Zehetbauer 27th Aug 2005 */
1676         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80651102,
1677          .driver = "EMU10K1", .name = "SB Live! 5.1 [SB0220]",
1678          .id = "Live",
1679          .emu10k1_chip = 1,
1680          .ac97_chip = 1,
1681          .sblive51 = 1} ,
1682         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80641102,
1683          .driver = "EMU10K1", .name = "SB Live! 5.1",
1684          .id = "Live",
1685          .emu10k1_chip = 1,
1686          .ac97_chip = 1,
1687          .sblive51 = 1} ,
1688         /* Tested by alsa bugtrack user "hus" bug #1297 12th Aug 2005 */
1689         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80611102,
1690          .driver = "EMU10K1", .name = "SB Live! 5.1 [SB0060]",
1691          .id = "Live",
1692          .emu10k1_chip = 1,
1693          .ac97_chip = 2, /* ac97 is optional; both SBLive 5.1 and platinum
1694                           * share the same IDs!
1695                           */
1696          .sblive51 = 1} ,
1697         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80511102,
1698          .driver = "EMU10K1", .name = "SB Live! Value [CT4850]",
1699          .id = "Live",
1700          .emu10k1_chip = 1,
1701          .ac97_chip = 1,
1702          .sblive51 = 1} ,
1703         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80401102,
1704          .driver = "EMU10K1", .name = "SB Live! Platinum [CT4760P]",
1705          .id = "Live",
1706          .emu10k1_chip = 1,
1707          .ac97_chip = 1} ,
1708         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80321102,
1709          .driver = "EMU10K1", .name = "SB Live! Value [CT4871]",
1710          .id = "Live",
1711          .emu10k1_chip = 1,
1712          .ac97_chip = 1,
1713          .sblive51 = 1} ,
1714         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80311102,
1715          .driver = "EMU10K1", .name = "SB Live! Value [CT4831]",
1716          .id = "Live",
1717          .emu10k1_chip = 1,
1718          .ac97_chip = 1,
1719          .sblive51 = 1} ,
1720         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80281102,
1721          .driver = "EMU10K1", .name = "SB Live! Value [CT4870]",
1722          .id = "Live",
1723          .emu10k1_chip = 1,
1724          .ac97_chip = 1,
1725          .sblive51 = 1} ,
1726         /* Tested by James@superbug.co.uk 3rd July 2005 */
1727         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80271102,
1728          .driver = "EMU10K1", .name = "SB Live! Value [CT4832]",
1729          .id = "Live",
1730          .emu10k1_chip = 1,
1731          .ac97_chip = 1,
1732          .sblive51 = 1} ,
1733         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80261102,
1734          .driver = "EMU10K1", .name = "SB Live! Value [CT4830]",
1735          .id = "Live",
1736          .emu10k1_chip = 1,
1737          .ac97_chip = 1,
1738          .sblive51 = 1} ,
1739         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80231102,
1740          .driver = "EMU10K1", .name = "SB PCI512 [CT4790]",
1741          .id = "Live",
1742          .emu10k1_chip = 1,
1743          .ac97_chip = 1,
1744          .sblive51 = 1} ,
1745         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x80221102,
1746          .driver = "EMU10K1", .name = "SB Live! Value [CT4780]",
1747          .id = "Live",
1748          .emu10k1_chip = 1,
1749          .ac97_chip = 1,
1750          .sblive51 = 1} ,
1751         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x40011102,
1752          .driver = "EMU10K1", .name = "E-mu APS [PC545]",
1753          .id = "APS",
1754          .emu10k1_chip = 1,
1755          .ecard = 1} ,
1756         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00211102,
1757          .driver = "EMU10K1", .name = "SB Live! [CT4620]",
1758          .id = "Live",
1759          .emu10k1_chip = 1,
1760          .ac97_chip = 1,
1761          .sblive51 = 1} ,
1762         {.vendor = 0x1102, .device = 0x0002, .subsystem = 0x00201102,
1763          .driver = "EMU10K1", .name = "SB Live! Value [CT4670]",
1764          .id = "Live",
1765          .emu10k1_chip = 1,
1766          .ac97_chip = 1,
1767          .sblive51 = 1} ,
1768         {.vendor = 0x1102, .device = 0x0002,
1769          .driver = "EMU10K1", .name = "SB Live! [Unknown]",
1770          .id = "Live",
1771          .emu10k1_chip = 1,
1772          .ac97_chip = 1,
1773          .sblive51 = 1} ,
1774         { } /* terminator */
1775 };
1776 
1777 int snd_emu10k1_create(struct snd_card *card,
1778                        struct pci_dev *pci,
1779                        unsigned short extin_mask,
1780                        unsigned short extout_mask,
1781                        long max_cache_bytes,
1782                        int enable_ir,
1783                        uint subsystem,
1784                        struct snd_emu10k1 **remu)
1785 {
1786         struct snd_emu10k1 *emu;
1787         int idx, err;
1788         int is_audigy;
1789         unsigned int silent_page;
1790         const struct snd_emu_chip_details *c;
1791         static struct snd_device_ops ops = {
1792                 .dev_free =     snd_emu10k1_dev_free,
1793         };
1794 
1795         *remu = NULL;
1796 
1797         /* enable PCI device */
1798         err = pci_enable_device(pci);
1799         if (err < 0)
1800                 return err;
1801 
1802         emu = kzalloc(sizeof(*emu), GFP_KERNEL);
1803         if (emu == NULL) {
1804                 pci_disable_device(pci);
1805                 return -ENOMEM;
1806         }
1807         emu->card = card;
1808         spin_lock_init(&emu->reg_lock);
1809         spin_lock_init(&emu->emu_lock);
1810         spin_lock_init(&emu->spi_lock);
1811         spin_lock_init(&emu->i2c_lock);
1812         spin_lock_init(&emu->voice_lock);
1813         spin_lock_init(&emu->synth_lock);
1814         spin_lock_init(&emu->memblk_lock);
1815         mutex_init(&emu->fx8010.lock);
1816         INIT_LIST_HEAD(&emu->mapped_link_head);
1817         INIT_LIST_HEAD(&emu->mapped_order_link_head);
1818         emu->pci = pci;
1819         emu->irq = -1;
1820         emu->synth = NULL;
1821         emu->get_synth_voice = NULL;
1822         /* read revision & serial */
1823         emu->revision = pci->revision;
1824         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &emu->serial);
1825         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &emu->model);
1826         dev_dbg(card->dev,
1827                 "vendor = 0x%x, device = 0x%x, subsystem_vendor_id = 0x%x, subsystem_id = 0x%x\n",
1828                 pci->vendor, pci->device, emu->serial, emu->model);
1829 
1830         for (c = emu_chip_details; c->vendor; c++) {
1831                 if (c->vendor == pci->vendor && c->device == pci->device) {
1832                         if (subsystem) {
1833                                 if (c->subsystem && (c->subsystem == subsystem))
1834                                         break;
1835                                 else
1836                                         continue;
1837                         } else {
1838                                 if (c->subsystem && (c->subsystem != emu->serial))
1839                                         continue;
1840                                 if (c->revision && c->revision != emu->revision)
1841                                         continue;
1842                         }
1843                         break;
1844                 }
1845         }
1846         if (c->vendor == 0) {
1847                 dev_err(card->dev, "emu10k1: Card not recognised\n");
1848                 kfree(emu);
1849                 pci_disable_device(pci);
1850                 return -ENOENT;
1851         }
1852         emu->card_capabilities = c;
1853         if (c->subsystem && !subsystem)
1854                 dev_dbg(card->dev, "Sound card name = %s\n", c->name);
1855         else if (subsystem)
1856                 dev_dbg(card->dev, "Sound card name = %s, "
1857                         "vendor = 0x%x, device = 0x%x, subsystem = 0x%x. "
1858                         "Forced to subsystem = 0x%x\n", c->name,
1859                         pci->vendor, pci->device, emu->serial, c->subsystem);
1860         else
1861                 dev_dbg(card->dev, "Sound card name = %s, "
1862                         "vendor = 0x%x, device = 0x%x, subsystem = 0x%x.\n",
1863                         c->name, pci->vendor, pci->device,
1864                         emu->serial);
1865 
1866         if (!*card->id && c->id) {
1867                 int i, n = 0;
1868                 strlcpy(card->id, c->id, sizeof(card->id));
1869                 for (;;) {
1870                         for (i = 0; i < snd_ecards_limit; i++) {
1871                                 if (snd_cards[i] && !strcmp(snd_cards[i]->id, card->id))
1872                                         break;
1873                         }
1874                         if (i >= snd_ecards_limit)
1875                                 break;
1876                         n++;
1877                         if (n >= SNDRV_CARDS)
1878                                 break;
1879                         snprintf(card->id, sizeof(card->id), "%s_%d", c->id, n);
1880                 }
1881         }
1882 
1883         is_audigy = emu->audigy = c->emu10k2_chip;
1884 
1885         /* set addressing mode */
1886         emu->address_mode = is_audigy ? 0 : 1;
1887         /* set the DMA transfer mask */
1888         emu->dma_mask = emu->address_mode ? EMU10K1_DMA_MASK : AUDIGY_DMA_MASK;
1889         if (pci_set_dma_mask(pci, emu->dma_mask) < 0 ||
1890             pci_set_consistent_dma_mask(pci, emu->dma_mask) < 0) {
1891                 dev_err(card->dev,
1892                         "architecture does not support PCI busmaster DMA with mask 0x%lx\n",
1893                         emu->dma_mask);
1894                 kfree(emu);
1895                 pci_disable_device(pci);
1896                 return -ENXIO;
1897         }
1898         if (is_audigy)
1899                 emu->gpr_base = A_FXGPREGBASE;
1900         else
1901                 emu->gpr_base = FXGPREGBASE;
1902 
1903         err = pci_request_regions(pci, "EMU10K1");
1904         if (err < 0) {
1905                 kfree(emu);
1906                 pci_disable_device(pci);
1907                 return err;
1908         }
1909         emu->port = pci_resource_start(pci, 0);
1910 
1911         emu->max_cache_pages = max_cache_bytes >> PAGE_SHIFT;
1912         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1913                                 (emu->address_mode ? 32 : 16) * 1024, &emu->ptb_pages) < 0) {
1914                 err = -ENOMEM;
1915                 goto error;
1916         }
1917 
1918         emu->page_ptr_table = vmalloc(emu->max_cache_pages * sizeof(void *));
1919         emu->page_addr_table = vmalloc(emu->max_cache_pages *
1920                                        sizeof(unsigned long));
1921         if (emu->page_ptr_table == NULL || emu->page_addr_table == NULL) {
1922                 err = -ENOMEM;
1923                 goto error;
1924         }
1925 
1926         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1927                                 EMUPAGESIZE, &emu->silent_page) < 0) {
1928                 err = -ENOMEM;
1929                 goto error;
1930         }
1931         emu->memhdr = snd_util_memhdr_new(emu->max_cache_pages * PAGE_SIZE);
1932         if (emu->memhdr == NULL) {
1933                 err = -ENOMEM;
1934                 goto error;
1935         }
1936         emu->memhdr->block_extra_size = sizeof(struct snd_emu10k1_memblk) -
1937                 sizeof(struct snd_util_memblk);
1938 
1939         pci_set_master(pci);
1940 
1941         emu->fx8010.fxbus_mask = 0x303f;
1942         if (extin_mask == 0)
1943                 extin_mask = 0x3fcf;
1944         if (extout_mask == 0)
1945                 extout_mask = 0x7fff;
1946         emu->fx8010.extin_mask = extin_mask;
1947         emu->fx8010.extout_mask = extout_mask;
1948         emu->enable_ir = enable_ir;
1949 
1950         if (emu->card_capabilities->ca_cardbus_chip) {
1951                 err = snd_emu10k1_cardbus_init(emu);
1952                 if (err < 0)
1953                         goto error;
1954         }
1955         if (emu->card_capabilities->ecard) {
1956                 err = snd_emu10k1_ecard_init(emu);
1957                 if (err < 0)
1958                         goto error;
1959         } else if (emu->card_capabilities->emu_model) {
1960                 err = snd_emu10k1_emu1010_init(emu);
1961                 if (err < 0) {
1962                         snd_emu10k1_free(emu);
1963                         return err;
1964                 }
1965         } else {
1966                 /* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
1967                         does not support this, it shouldn't do any harm */
1968                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0,
1969                                         AC97SLOT_CNTR|AC97SLOT_LFE);
1970         }
1971 
1972         /* initialize TRAM setup */
1973         emu->fx8010.itram_size = (16 * 1024)/2;
1974         emu->fx8010.etram_pages.area = NULL;
1975         emu->fx8010.etram_pages.bytes = 0;
1976 
1977         /* irq handler must be registered after I/O ports are activated */
1978         if (request_irq(pci->irq, snd_emu10k1_interrupt, IRQF_SHARED,
1979                         KBUILD_MODNAME, emu)) {
1980                 err = -EBUSY;
1981                 goto error;
1982         }
1983         emu->irq = pci->irq;
1984 
1985         /*
1986          *  Init to 0x02109204 :
1987          *  Clock accuracy    = 0     (1000ppm)
1988          *  Sample Rate       = 2     (48kHz)
1989          *  Audio Channel     = 1     (Left of 2)
1990          *  Source Number     = 0     (Unspecified)
1991          *  Generation Status = 1     (Original for Cat Code 12)
1992          *  Cat Code          = 12    (Digital Signal Mixer)
1993          *  Mode              = 0     (Mode 0)
1994          *  Emphasis          = 0     (None)
1995          *  CP                = 1     (Copyright unasserted)
1996          *  AN                = 0     (Audio data)
1997          *  P                 = 0     (Consumer)
1998          */
1999         emu->spdif_bits[0] = emu->spdif_bits[1] =
2000                 emu->spdif_bits[2] = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
2001                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
2002                 SPCS_GENERATIONSTATUS | 0x00001200 |
2003                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
2004 
2005         emu->reserved_page = (struct snd_emu10k1_memblk *)
2006                 snd_emu10k1_synth_alloc(emu, 4096);
2007         if (emu->reserved_page)
2008                 emu->reserved_page->map_locked = 1;
2009 
2010         /* Clear silent pages and set up pointers */
2011         memset(emu->silent_page.area, 0, PAGE_SIZE);
2012         silent_page = emu->silent_page.addr << emu->address_mode;
2013         for (idx = 0; idx < (emu->address_mode ? MAXPAGES1 : MAXPAGES0); idx++)
2014                 ((u32 *)emu->ptb_pages.area)[idx] = cpu_to_le32(silent_page | idx);
2015 
2016         /* set up voice indices */
2017         for (idx = 0; idx < NUM_G; idx++) {
2018                 emu->voices[idx].emu = emu;
2019                 emu->voices[idx].number = idx;
2020         }
2021 
2022         err = snd_emu10k1_init(emu, enable_ir, 0);
2023         if (err < 0)
2024                 goto error;
2025 #ifdef CONFIG_PM_SLEEP
2026         err = alloc_pm_buffer(emu);
2027         if (err < 0)
2028                 goto error;
2029 #endif
2030 
2031         /*  Initialize the effect engine */
2032         err = snd_emu10k1_init_efx(emu);
2033         if (err < 0)
2034                 goto error;
2035         snd_emu10k1_audio_enable(emu);
2036 
2037         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, emu, &ops);
2038         if (err < 0)
2039                 goto error;
2040 
2041 #ifdef CONFIG_PROC_FS
2042         snd_emu10k1_proc_init(emu);
2043 #endif
2044 
2045         *remu = emu;
2046         return 0;
2047 
2048  error:
2049         snd_emu10k1_free(emu);
2050         return err;
2051 }
2052 
2053 #ifdef CONFIG_PM_SLEEP
2054 static unsigned char saved_regs[] = {
2055         CPF, PTRX, CVCF, VTFT, Z1, Z2, PSST, DSL, CCCA, CCR, CLP,
2056         FXRT, MAPA, MAPB, ENVVOL, ATKHLDV, DCYSUSV, LFOVAL1, ENVVAL,
2057         ATKHLDM, DCYSUSM, LFOVAL2, IP, IFATN, PEFE, FMMOD, TREMFRQ, FM2FRQ2,
2058         TEMPENV, ADCCR, FXWC, MICBA, ADCBA, FXBA,
2059         MICBS, ADCBS, FXBS, CDCS, GPSCS, SPCS0, SPCS1, SPCS2,
2060         SPBYPASS, AC97SLOT, CDSRCS, GPSRCS, ZVSRCS, MICIDX, ADCIDX, FXIDX,
2061         0xff /* end */
2062 };
2063 static unsigned char saved_regs_audigy[] = {
2064         A_ADCIDX, A_MICIDX, A_FXWC1, A_FXWC2, A_SAMPLE_RATE,
2065         A_FXRT2, A_SENDAMOUNTS, A_FXRT1,
2066         0xff /* end */
2067 };
2068 
2069 static int alloc_pm_buffer(struct snd_emu10k1 *emu)
2070 {
2071         int size;
2072 
2073         size = ARRAY_SIZE(saved_regs);
2074         if (emu->audigy)
2075                 size += ARRAY_SIZE(saved_regs_audigy);
2076         emu->saved_ptr = vmalloc(4 * NUM_G * size);
2077         if (!emu->saved_ptr)
2078                 return -ENOMEM;
2079         if (snd_emu10k1_efx_alloc_pm_buffer(emu) < 0)
2080                 return -ENOMEM;
2081         if (emu->card_capabilities->ca0151_chip &&
2082             snd_p16v_alloc_pm_buffer(emu) < 0)
2083                 return -ENOMEM;
2084         return 0;
2085 }
2086 
2087 static void free_pm_buffer(struct snd_emu10k1 *emu)
2088 {
2089         vfree(emu->saved_ptr);
2090         snd_emu10k1_efx_free_pm_buffer(emu);
2091         if (emu->card_capabilities->ca0151_chip)
2092                 snd_p16v_free_pm_buffer(emu);
2093 }
2094 
2095 void snd_emu10k1_suspend_regs(struct snd_emu10k1 *emu)
2096 {
2097         int i;
2098         unsigned char *reg;
2099         unsigned int *val;
2100 
2101         val = emu->saved_ptr;
2102         for (reg = saved_regs; *reg != 0xff; reg++)
2103                 for (i = 0; i < NUM_G; i++, val++)
2104                         *val = snd_emu10k1_ptr_read(emu, *reg, i);
2105         if (emu->audigy) {
2106                 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
2107                         for (i = 0; i < NUM_G; i++, val++)
2108                                 *val = snd_emu10k1_ptr_read(emu, *reg, i);
2109         }
2110         if (emu->audigy)
2111                 emu->saved_a_iocfg = inl(emu->port + A_IOCFG);
2112         emu->saved_hcfg = inl(emu->port + HCFG);
2113 }
2114 
2115 void snd_emu10k1_resume_init(struct snd_emu10k1 *emu)
2116 {
2117         if (emu->card_capabilities->ca_cardbus_chip)
2118                 snd_emu10k1_cardbus_init(emu);
2119         if (emu->card_capabilities->ecard)
2120                 snd_emu10k1_ecard_init(emu);
2121         else if (emu->card_capabilities->emu_model)
2122                 snd_emu10k1_emu1010_init(emu);
2123         else
2124                 snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
2125         snd_emu10k1_init(emu, emu->enable_ir, 1);
2126 }
2127 
2128 void snd_emu10k1_resume_regs(struct snd_emu10k1 *emu)
2129 {
2130         int i;
2131         unsigned char *reg;
2132         unsigned int *val;
2133 
2134         snd_emu10k1_audio_enable(emu);
2135 
2136         /* resore for spdif */
2137         if (emu->audigy)
2138                 outl(emu->saved_a_iocfg, emu->port + A_IOCFG);
2139         outl(emu->saved_hcfg, emu->port + HCFG);
2140 
2141         val = emu->saved_ptr;
2142         for (reg = saved_regs; *reg != 0xff; reg++)
2143                 for (i = 0; i < NUM_G; i++, val++)
2144                         snd_emu10k1_ptr_write(emu, *reg, i, *val);
2145         if (emu->audigy) {
2146                 for (reg = saved_regs_audigy; *reg != 0xff; reg++)
2147                         for (i = 0; i < NUM_G; i++, val++)
2148                                 snd_emu10k1_ptr_write(emu, *reg, i, *val);
2149         }
2150 }
2151 #endif
2152 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp