Merge branch '83xx' into for_paulus
[linux-2.6] / sound / oss / emu10k1 / main.c
1  /*
2  **********************************************************************
3  *     main.c - Creative EMU10K1 audio driver
4  *     Copyright 1999, 2000 Creative Labs, Inc.
5  *
6  **********************************************************************
7  *
8  *     Date                 Author          Summary of changes
9  *     ----                 ------          ------------------
10  *     October 20, 1999     Bertrand Lee    base code release
11  *     November 2, 1999     Alan Cox        cleaned up stuff
12  *
13  **********************************************************************
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public
26  *     License along with this program; if not, write to the Free
27  *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
28  *     USA.
29  *
30  **********************************************************************
31  *
32  *      Supported devices:
33  *      /dev/dsp:        Standard /dev/dsp device, OSS-compatible
34  *      /dev/dsp1:       Routes to rear speakers only    
35  *      /dev/mixer:      Standard /dev/mixer device, OSS-compatible
36  *      /dev/midi:       Raw MIDI UART device, mostly OSS-compatible
37  *      /dev/sequencer:  Sequencer Interface (requires sound.o)
38  *
39  *      Revision history:
40  *      0.1 beta Initial release
41  *      0.2 Lowered initial mixer vol. Improved on stuttering wave playback. Added MIDI UART support.
42  *      0.3 Fixed mixer routing bug, added APS, joystick support.
43  *      0.4 Added rear-channel, SPDIF support.
44  *      0.5 Source cleanup, SMP fixes, multiopen support, 64 bit arch fixes,
45  *          moved bh's to tasklets, moved to the new PCI driver initialization style.
46  *      0.6 Make use of pci_alloc_consistent, improve compatibility layer for 2.2 kernels,
47  *          code reorganization and cleanup.
48  *      0.7 Support for the Emu-APS. Bug fixes for voice cache setup, mmaped sound + poll().
49  *          Support for setting external TRAM size.
50  *      0.8 Make use of the kernel ac97 interface. Support for a dsp patch manager.
51  *      0.9 Re-enables rear speakers volume controls
52  *     0.10 Initializes rear speaker volume.
53  *          Dynamic patch storage allocation.
54  *          New private ioctls to change control gpr values.
55  *          Enable volume control interrupts.
56  *          By default enable dsp routes to digital out. 
57  *     0.11 Fixed fx / 4 problem.
58  *     0.12 Implemented mmaped for recording.
59  *          Fixed bug: not unreserving mmaped buffer pages.
60  *          IRQ handler cleanup.
61  *     0.13 Fixed problem with dsp1
62  *          Simplified dsp patch writing (inside the driver)
63  *          Fixed several bugs found by the Stanford tools
64  *     0.14 New control gpr to oss mixer mapping feature (Chris Purnell)
65  *          Added AC3 Passthrough Support (Juha Yrjola)
66  *          Added Support for 5.1 cards (digital out and the third analog out)
67  *     0.15 Added Sequencer Support (Daniel Mack)
68  *          Support for multichannel pcm playback (Eduard Hasenleithner)
69  *     0.16 Mixer improvements, added old treble/bass support (Daniel Bertrand)
70  *          Small code format cleanup.
71  *          Deadlock bug fix for emu10k1_volxxx_irqhandler().
72  *     0.17 Fix for mixer SOUND_MIXER_INFO ioctl.
73  *          Fix for HIGHMEM machines (emu10k1 can only do 31 bit bus master) 
74  *          midi poll initial implementation.
75  *          Small mixer fixes/cleanups.
76  *          Improved support for 5.1 cards.
77  *     0.18 Fix for possible leak in pci_alloc_consistent()
78  *          Cleaned up poll() functions (audio and midi). Don't start input.
79  *          Restrict DMA pages used to 512Mib range.
80  *          New AC97_BOOST mixer ioctl.
81  *    0.19a Added Support for Audigy Cards
82  *          Real fix for kernel with highmem support (cast dma_handle to u32).
83  *          Fix recording buffering parameters calculation.
84  *          Use unsigned long for variables in bit ops.
85  *    0.20a Fixed recording startup
86  *          Fixed timer rate setting (it's a 16-bit register)
87  *      0.21 Converted code to use pci_name() instead of accessing slot_name
88  *          directly (Eugene Teo)
89  *********************************************************************/
90
91 /* These are only included once per module */
92 #include <linux/module.h>
93 #include <linux/slab.h>
94 #include <linux/init.h>
95 #include <linux/delay.h>
96 #include <linux/proc_fs.h>
97 #include <linux/dma-mapping.h>
98
99 #include "hwaccess.h"
100 #include "8010.h"
101 #include "efxmgr.h"
102 #include "cardwo.h"
103 #include "cardwi.h"
104 #include "cardmo.h"
105 #include "cardmi.h"
106 #include "recmgr.h"
107 #include "ecard.h"
108
109
110 #ifdef EMU10K1_SEQUENCER
111 #define MIDI_SYNTH_NAME "EMU10K1 MIDI"
112 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
113  
114 #include "../sound_config.h"
115 #include "../midi_synth.h"
116
117 /* this should be in dev_table.h */
118 #define SNDCARD_EMU10K1 46
119 #endif
120  
121
122 /* the emu10k1 _seems_ to only supports 29 bit (512MiB) bit bus master */
123 #define EMU10K1_DMA_MASK DMA_29BIT_MASK /* DMA buffer mask for pci_alloc_consist */
124
125 #ifndef PCI_VENDOR_ID_CREATIVE
126 #define PCI_VENDOR_ID_CREATIVE 0x1102
127 #endif
128
129 #ifndef PCI_DEVICE_ID_CREATIVE_EMU10K1
130 #define PCI_DEVICE_ID_CREATIVE_EMU10K1 0x0002
131 #endif
132 #ifndef PCI_DEVICE_ID_CREATIVE_AUDIGY
133 #define PCI_DEVICE_ID_CREATIVE_AUDIGY 0x0004
134 #endif
135
136 #define EMU_APS_SUBID   0x40011102
137  
138 enum {
139         EMU10K1 = 0,
140         AUDIGY,
141 };
142
143 static char *card_names[] __devinitdata = {
144         "EMU10K1",
145         "Audigy",
146 };
147
148 static struct pci_device_id emu10k1_pci_tbl[] = {
149         {PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_EMU10K1,
150          PCI_ANY_ID, PCI_ANY_ID, 0, 0, EMU10K1},
151         {PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_AUDIGY,
152          PCI_ANY_ID, PCI_ANY_ID, 0, 0, AUDIGY},
153         {0,}
154 };
155
156 MODULE_DEVICE_TABLE(pci, emu10k1_pci_tbl);
157
158 /* Global var instantiation */
159
160 LIST_HEAD(emu10k1_devs);
161
162 extern struct file_operations emu10k1_audio_fops;
163 extern struct file_operations emu10k1_mixer_fops;
164 extern struct file_operations emu10k1_midi_fops;
165
166 #ifdef EMU10K1_SEQUENCER
167 static struct midi_operations emu10k1_midi_operations;
168 #endif
169
170 extern irqreturn_t emu10k1_interrupt(int, void *, struct pt_regs *s);
171
172 static int __devinit emu10k1_audio_init(struct emu10k1_card *card)
173 {
174         /* Assign default playback voice parameters */
175         if (card->is_audigy)
176                 card->mchannel_fx = 0;
177         else
178                 card->mchannel_fx = 8;
179
180
181         if (card->is_audigy) {
182                 /* mono voice */
183                 card->waveout.send_dcba[SEND_MONO] = 0xffffffff;
184                 card->waveout.send_hgfe[SEND_MONO] = 0x0000ffff;
185         
186                 /* stereo voice */
187                 /* left */
188                 card->waveout.send_dcba[SEND_LEFT] = 0x00ff00ff;
189                 card->waveout.send_hgfe[SEND_LEFT] = 0x00007f7f;        
190                 /* right */
191                 card->waveout.send_dcba[SEND_RIGHT] = 0xff00ff00;
192                 card->waveout.send_hgfe[SEND_RIGHT] = 0x00007f7f;
193
194                 card->waveout.send_routing[ROUTE_PCM] = 0x03020100; // Regular pcm
195                 card->waveout.send_routing2[ROUTE_PCM] = 0x07060504;
196
197                 card->waveout.send_routing[ROUTE_PT] = 0x3f3f3d3c; // Passthrough
198                 card->waveout.send_routing2[ROUTE_PT] = 0x3f3f3f3f;
199                 
200                 card->waveout.send_routing[ROUTE_PCM1] = 0x03020100; // Spare
201                 card->waveout.send_routing2[ROUTE_PCM1] = 0x07060404;
202                 
203         } else {
204                 /* mono voice */
205                 card->waveout.send_dcba[SEND_MONO] = 0x0000ffff;
206         
207                 /* stereo voice */
208                 /* left */
209                 card->waveout.send_dcba[SEND_LEFT] = 0x000000ff;
210                 /* right */
211                 card->waveout.send_dcba[SEND_RIGHT] = 0x0000ff00;
212
213                 card->waveout.send_routing[ROUTE_PCM] = 0x3210; // pcm
214                 card->waveout.send_routing[ROUTE_PT] = 0x3210; // passthrough
215                 card->waveout.send_routing[ROUTE_PCM1] = 0x7654; // /dev/dsp1
216         }
217
218         /* Assign default recording parameters */
219         /* FIXME */
220         if (card->is_aps)
221                 card->wavein.recsrc = WAVERECORD_FX;
222         else
223                 card->wavein.recsrc = WAVERECORD_AC97;
224
225         card->wavein.fxwc = 0x0003;
226         return 0;
227 }
228
229 static void emu10k1_audio_cleanup(struct emu10k1_card *card)
230 {
231 }
232
233 static int __devinit emu10k1_register_devices(struct emu10k1_card *card)
234 {
235         card->audio_dev = register_sound_dsp(&emu10k1_audio_fops, -1);
236         if (card->audio_dev < 0) {
237                 printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
238                 goto err_dev;
239         }
240
241         card->audio_dev1 = register_sound_dsp(&emu10k1_audio_fops, -1);
242         if (card->audio_dev1 < 0) {
243                 printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
244                 goto err_dev1;
245         }
246
247         card->ac97->dev_mixer = register_sound_mixer(&emu10k1_mixer_fops, -1);
248         if (card->ac97->dev_mixer < 0) {
249                 printk(KERN_ERR "emu10k1: cannot register mixer device\n");
250                 goto err_mixer;
251         }
252
253         card->midi_dev = register_sound_midi(&emu10k1_midi_fops, -1);
254         if (card->midi_dev < 0) {
255                 printk(KERN_ERR "emu10k1: cannot register midi device!\n");
256                 goto err_midi;
257         }
258
259 #ifdef EMU10K1_SEQUENCER
260         card->seq_dev = sound_alloc_mididev();
261         if (card->seq_dev == -1)
262                 printk(KERN_WARNING "emu10k1: unable to register sequencer device!");
263         else {
264                 std_midi_synth.midi_dev = card->seq_dev;
265                 midi_devs[card->seq_dev] = 
266                         (struct midi_operations *)
267                         kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
268
269                 if (midi_devs[card->seq_dev] == NULL) {
270                         printk(KERN_ERR "emu10k1: unable to allocate memory!");
271                         sound_unload_mididev(card->seq_dev);
272                         card->seq_dev = -1;
273                         /* return without error */
274                 } else {
275                         memcpy((char *)midi_devs[card->seq_dev], 
276                                 (char *)&emu10k1_midi_operations, 
277                                 sizeof(struct midi_operations));
278                         midi_devs[card->seq_dev]->devc = card;
279                         sequencer_init();
280                         card->seq_mididev = NULL;
281                 }
282         }
283 #endif
284         return 0;
285
286 err_midi:
287         unregister_sound_mixer(card->ac97->dev_mixer);
288 err_mixer:
289         unregister_sound_dsp(card->audio_dev);
290 err_dev1:
291         unregister_sound_dsp(card->audio_dev);
292 err_dev:
293         return -ENODEV;
294 }
295
296 static void emu10k1_unregister_devices(struct emu10k1_card *card)
297 {
298 #ifdef EMU10K1_SEQUENCER
299         if (card->seq_dev > -1) {
300                 kfree(midi_devs[card->seq_dev]);
301                 midi_devs[card->seq_dev] = NULL;
302                 sound_unload_mididev(card->seq_dev);
303                 card->seq_dev = -1;
304         }
305 #endif
306
307         unregister_sound_midi(card->midi_dev);
308         unregister_sound_mixer(card->ac97->dev_mixer);
309         unregister_sound_dsp(card->audio_dev1);
310         unregister_sound_dsp(card->audio_dev);
311 }
312
313 static int emu10k1_info_proc (char *page, char **start, off_t off,
314                               int count, int *eof, void *data)
315 {
316         struct emu10k1_card *card = data;
317         int len = 0;
318         
319         if (card == NULL)
320                 return -ENODEV;
321
322         len += sprintf (page + len, "Driver Version : %s\n", DRIVER_VERSION);
323         len += sprintf (page + len, "Card type      : %s\n", card->is_aps ? "Aps" : (card->is_audigy ? "Audigy" : "Emu10k1"));
324         len += sprintf (page + len, "Revision       : %d\n", card->chiprev);
325         len += sprintf (page + len, "Model          : %#06x\n", card->model);
326         len += sprintf (page + len, "IO             : %#06lx-%#06lx\n", card->iobase, card->iobase + card->length - 1);
327         len += sprintf (page + len, "IRQ            : %d\n\n", card->irq);
328         
329         len += sprintf (page + len, "Registered /dev Entries:\n");
330         len += sprintf (page + len, "/dev/dsp%d\n", card->audio_dev / 16);
331         len += sprintf (page + len, "/dev/dsp%d\n", card->audio_dev1 / 16);
332         len += sprintf (page + len, "/dev/mixer%d\n", card->ac97->dev_mixer / 16);
333         len += sprintf (page + len, "/dev/midi%d\n", card->midi_dev / 16);
334
335 #ifdef EMU10K1_SEQUENCER
336         len += sprintf (page + len, "/dev/sequencer\n");
337 #endif
338
339         return len;
340 }
341
342 static int __devinit emu10k1_proc_init(struct emu10k1_card *card)
343 {
344         char s[48];
345
346         if (!proc_mkdir ("driver/emu10k1", NULL)) {
347                 printk(KERN_ERR "emu10k1: unable to create proc directory driver/emu10k1\n");
348                 goto err_out;
349         }
350
351         sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
352         if (!proc_mkdir (s, NULL)) {
353                 printk(KERN_ERR "emu10k1: unable to create proc directory %s\n", s);
354                 goto err_emu10k1_proc;
355         }
356
357         sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
358         if (!create_proc_read_entry (s, 0, NULL, emu10k1_info_proc, card)) {
359                 printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
360                 goto err_dev_proc;
361         }
362
363         if (!card->is_aps) {
364                 sprintf(s, "driver/emu10k1/%s/ac97", pci_name(card->pci_dev));
365                 if (!create_proc_read_entry (s, 0, NULL, ac97_read_proc, card->ac97)) {
366                         printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
367                         goto err_proc_ac97;
368                 }
369         }
370
371         return 0;
372
373 err_proc_ac97:
374         sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
375         remove_proc_entry(s, NULL);
376
377 err_dev_proc:
378         sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
379         remove_proc_entry(s, NULL);
380
381 err_emu10k1_proc:
382         remove_proc_entry("driver/emu10k1", NULL);
383
384 err_out:
385         return -EIO;
386 }
387
388 static void emu10k1_proc_cleanup(struct emu10k1_card *card)
389 {
390         char s[48];
391
392         if (!card->is_aps) {
393                 sprintf(s, "driver/emu10k1/%s/ac97", pci_name(card->pci_dev));
394                 remove_proc_entry(s, NULL);
395         }
396
397         sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
398         remove_proc_entry(s, NULL);
399
400         sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
401         remove_proc_entry(s, NULL);
402                 
403         remove_proc_entry("driver/emu10k1", NULL);
404 }
405
406 static int __devinit emu10k1_mixer_init(struct emu10k1_card *card)
407 {
408         struct ac97_codec *codec  = ac97_alloc_codec();
409         
410         if(codec == NULL)
411         {
412                 printk(KERN_ERR "emu10k1: cannot allocate mixer\n");
413                 return -EIO;
414         }
415         card->ac97 = codec;
416         card->ac97->private_data = card;
417
418         if (!card->is_aps) {
419                 card->ac97->id = 0;
420                 card->ac97->codec_read = emu10k1_ac97_read;
421                 card->ac97->codec_write = emu10k1_ac97_write;
422
423                 if (ac97_probe_codec (card->ac97) == 0) {
424                         printk(KERN_ERR "emu10k1: unable to probe AC97 codec\n");
425                         goto err_out;
426                 }
427                 /* 5.1: Enable the additional AC97 Slots and unmute extra channels on AC97 codec */
428                 if (codec->codec_read(codec, AC97_EXTENDED_ID) & 0x0080){
429                         printk(KERN_INFO "emu10k1: SBLive! 5.1 card detected\n"); 
430                         sblive_writeptr(card, AC97SLOT, 0, AC97SLOT_CNTR | AC97SLOT_LFE);
431                         codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0);
432                 }
433
434                 // Force 5bit:              
435                 //card->ac97->bit_resolution=5;
436
437                 /* these will store the original values and never be modified */
438                 card->ac97_supported_mixers = card->ac97->supported_mixers;
439                 card->ac97_stereo_mixers = card->ac97->stereo_mixers;
440         }
441
442         return 0;
443
444  err_out:
445         ac97_release_codec(card->ac97);
446         return -EIO;
447 }
448
449 static void emu10k1_mixer_cleanup(struct emu10k1_card *card)
450 {
451         ac97_release_codec(card->ac97);
452 }
453
454 static int __devinit emu10k1_midi_init(struct emu10k1_card *card)
455 {
456         int ret;
457
458         card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL);
459         if (card->mpuout == NULL) {
460                 printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuout: out of memory\n");
461                 ret = -ENOMEM;
462                 goto err_out1;
463         }
464
465         memset(card->mpuout, 0, sizeof(struct emu10k1_mpuout));
466
467         card->mpuout->intr = 1;
468         card->mpuout->status = FLAGS_AVAILABLE;
469         card->mpuout->state = CARDMIDIOUT_STATE_DEFAULT;
470
471         tasklet_init(&card->mpuout->tasklet, emu10k1_mpuout_bh, (unsigned long) card);
472
473         spin_lock_init(&card->mpuout->lock);
474
475         card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL);
476         if (card->mpuin == NULL) {
477                 printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuin: out of memory\n");
478                 ret = -ENOMEM;
479                 goto err_out2;
480         }
481
482         memset(card->mpuin, 0, sizeof(struct emu10k1_mpuin));
483
484         card->mpuin->status = FLAGS_AVAILABLE;
485
486         tasklet_init(&card->mpuin->tasklet, emu10k1_mpuin_bh, (unsigned long) card->mpuin);
487
488         spin_lock_init(&card->mpuin->lock);
489
490         /* Reset the MPU port */
491         if (emu10k1_mpu_reset(card) < 0) {
492                 ERROR();
493                 ret = -EIO;
494                 goto err_out3;
495         }
496
497         return 0;
498
499 err_out3:
500         kfree(card->mpuin);
501 err_out2:
502         kfree(card->mpuout);
503 err_out1:
504         return ret;
505 }
506
507 static void emu10k1_midi_cleanup(struct emu10k1_card *card)
508 {
509         tasklet_kill(&card->mpuout->tasklet);
510         kfree(card->mpuout);
511
512         tasklet_kill(&card->mpuin->tasklet);
513         kfree(card->mpuin);
514 }
515
516 static void __devinit voice_init(struct emu10k1_card *card)
517 {
518         int i;
519
520         for (i = 0; i < NUM_G; i++)
521                 card->voicetable[i] = VOICE_USAGE_FREE;
522 }
523
524 static void __devinit timer_init(struct emu10k1_card *card)
525 {
526         INIT_LIST_HEAD(&card->timers);
527         card->timer_delay = TIMER_STOPPED;
528         spin_lock_init(&card->timer_lock);
529 }
530
531 static void __devinit addxmgr_init(struct emu10k1_card *card)
532 {
533         u32 count;
534
535         for (count = 0; count < MAXPAGES; count++)
536                 card->emupagetable[count] = 0;
537
538         /* Mark first page as used */
539         /* This page is reserved by the driver */
540         card->emupagetable[0] = 0x8001;
541         card->emupagetable[1] = MAXPAGES - 1;
542 }
543
544 static void fx_cleanup(struct patch_manager *mgr)
545 {
546         int i;
547         for(i = 0; i < mgr->current_pages; i++)
548                 free_page((unsigned long) mgr->patch[i]);
549 }
550
551 static int __devinit fx_init(struct emu10k1_card *card)
552 {
553         struct patch_manager *mgr = &card->mgr;
554         struct dsp_patch *patch;
555         struct dsp_rpatch *rpatch;
556         s32 left, right;
557         int i;
558         u32 pc = 0;
559         u32 patch_n=0;
560         struct emu_efx_info_t emu_efx_info[2]=
561                 {{ 20, 10, 0x400, 0x100, 0x20 },
562                  { 24, 12, 0x600, 0x400, 0x60 },
563                 }; 
564                         
565
566         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
567                 mgr->ctrl_gpr[i][0] = -1;
568                 mgr->ctrl_gpr[i][1] = -1;
569         }
570
571
572         if (card->is_audigy)
573                 mgr->current_pages = (2 + PATCHES_PER_PAGE - 1) / PATCHES_PER_PAGE;
574         else
575                 /* !! The number below must equal the number of patches, currently 11 !! */
576                 mgr->current_pages = (11 + PATCHES_PER_PAGE - 1) / PATCHES_PER_PAGE;
577         
578         for (i = 0; i < mgr->current_pages; i++) {
579                 mgr->patch[i] = (void *)__get_free_page(GFP_KERNEL);
580                 if (mgr->patch[i] == NULL) {
581                         mgr->current_pages = i;
582                         fx_cleanup(mgr);
583                         return -ENOMEM;
584                 }
585                 memset(mgr->patch[i], 0, PAGE_SIZE);
586         }
587
588         if (card->is_audigy) {
589                 for (i = 0; i < 1024; i++)
590                         OP(0xf, 0x0c0, 0x0c0, 0x0cf, 0x0c0);
591
592                 for (i = 0; i < 512 ; i++)
593                         sblive_writeptr(card, A_GPR_BASE+i,0,0);
594
595                 pc=0;
596
597                 //Pcm input volume
598                 OP(0, 0x402, 0x0c0, 0x406, 0x000);
599                 OP(0, 0x403, 0x0c0, 0x407, 0x001);
600
601                 //CD-Digital input Volume
602                 OP(0, 0x404, 0x0c0, 0x40d, 0x42);
603                 OP(0, 0x405, 0x0c0, 0x40f, 0x43);
604
605                 // CD + PCM 
606                 OP(6, 0x400, 0x0c0, 0x402, 0x404);
607                 OP(6, 0x401, 0x0c0, 0x403, 0x405);
608                 
609                 // Front Output + Master Volume
610                 OP(0, 0x68, 0x0c0, 0x408, 0x400);
611                 OP(0, 0x69, 0x0c0, 0x409, 0x401);
612
613                 // Add-in analog inputs for other speakers
614                 OP(6, 0x400, 0x40, 0x400, 0xc0);
615                 OP(6, 0x401, 0x41, 0x401, 0xc0);
616
617                 // Digital Front + Master Volume
618                 OP(0, 0x60, 0x0c0, 0x408, 0x400);
619                 OP(0, 0x61, 0x0c0, 0x409, 0x401);
620
621                 // Rear Output + Rear Volume
622                 OP(0, 0x06e, 0x0c0, 0x419, 0x400);
623                 OP(0, 0x06f, 0x0c0, 0x41a, 0x401);              
624
625                 // Digital Rear Output + Rear Volume
626                 OP(0, 0x066, 0x0c0, 0x419, 0x400);
627                 OP(0, 0x067, 0x0c0, 0x41a, 0x401);              
628
629                 // Audigy Drive, Headphone out
630                 OP(6, 0x64, 0x0c0, 0x0c0, 0x400);
631                 OP(6, 0x65, 0x0c0, 0x0c0, 0x401);
632
633                 // ac97 Recording
634                 OP(6, 0x76, 0x0c0, 0x0c0, 0x40);
635                 OP(6, 0x77, 0x0c0, 0x0c0, 0x41);
636                 
637                 // Center = sub = Left/2 + Right/2
638                 OP(0xe, 0x400, 0x401, 0xcd, 0x400);
639
640                 // center/sub  Volume (master)
641                 OP(0, 0x06a, 0x0c0, 0x408, 0x400);
642                 OP(0, 0x06b, 0x0c0, 0x409, 0x400);
643
644                 // Digital center/sub  Volume (master)
645                 OP(0, 0x062, 0x0c0, 0x408, 0x400);
646                 OP(0, 0x063, 0x0c0, 0x409, 0x400);
647
648                 ROUTING_PATCH_START(rpatch, "Routing");
649                 ROUTING_PATCH_END(rpatch);
650
651                 /* delimiter patch */
652                 patch = PATCH(mgr, patch_n);
653                 patch->code_size = 0;
654
655         
656                 sblive_writeptr(card, 0x53, 0, 0);
657         } else {
658                 for (i = 0; i < 512 ; i++)
659                         OP(6, 0x40, 0x40, 0x40, 0x40);
660
661                 for (i = 0; i < 256; i++)
662                         sblive_writeptr_tag(card, 0,
663                                             FXGPREGBASE + i, 0,
664                                             TANKMEMADDRREGBASE + i, 0,
665                                             TAGLIST_END);
666
667                 
668                 pc = 0;
669
670                 //first free GPR = 0x11b
671         
672                 
673                 /* FX volume correction and Volume control*/
674                 INPUT_PATCH_START(patch, "Pcm L vol", 0x0, 0);
675                 GET_OUTPUT_GPR(patch, 0x100, 0x0);
676                 GET_CONTROL_GPR(patch, 0x106, "Vol", 0, 0x7fffffff);
677                 GET_DYNAMIC_GPR(patch, 0x112);
678
679                 OP(4, 0x112, 0x40, PCM_IN_L, 0x44); //*4        
680                 OP(0, 0x100, 0x040, 0x112, 0x106);  //*vol      
681                 INPUT_PATCH_END(patch);
682
683
684                 INPUT_PATCH_START(patch, "Pcm R vol", 0x1, 0);
685                 GET_OUTPUT_GPR(patch, 0x101, 0x1);
686                 GET_CONTROL_GPR(patch, 0x107, "Vol", 0, 0x7fffffff);
687                 GET_DYNAMIC_GPR(patch, 0x112);
688
689                 OP(4, 0x112, 0x40, PCM_IN_R, 0x44); 
690                 OP(0, 0x101, 0x040, 0x112, 0x107);
691
692                 INPUT_PATCH_END(patch);
693
694
695                 // CD-Digital In Volume control 
696                 INPUT_PATCH_START(patch, "CD-Digital Vol L", 0x12, 0);
697                 GET_OUTPUT_GPR(patch, 0x10c, 0x12);
698                 GET_CONTROL_GPR(patch, 0x10d, "Vol", 0, 0x7fffffff);
699
700                 OP(0, 0x10c, 0x040, SPDIF_CD_L, 0x10d);
701                 INPUT_PATCH_END(patch);
702
703                 INPUT_PATCH_START(patch, "CD-Digital Vol R", 0x13, 0);
704                 GET_OUTPUT_GPR(patch, 0x10e, 0x13);
705                 GET_CONTROL_GPR(patch, 0x10f, "Vol", 0, 0x7fffffff);
706
707                 OP(0, 0x10e, 0x040, SPDIF_CD_R, 0x10f);
708                 INPUT_PATCH_END(patch);
709
710                 //Volume Correction for Multi-channel Inputs    
711                 INPUT_PATCH_START(patch, "Multi-Channel Gain", 0x08, 0);
712                 patch->input=patch->output=0x3F00;
713
714                 GET_OUTPUT_GPR(patch, 0x113, MULTI_FRONT_L);
715                 GET_OUTPUT_GPR(patch, 0x114, MULTI_FRONT_R);
716                 GET_OUTPUT_GPR(patch, 0x115, MULTI_REAR_L);
717                 GET_OUTPUT_GPR(patch, 0x116, MULTI_REAR_R);
718                 GET_OUTPUT_GPR(patch, 0x117, MULTI_CENTER);
719                 GET_OUTPUT_GPR(patch, 0x118, MULTI_LFE);
720
721                 OP(4, 0x113, 0x40, MULTI_FRONT_L, 0x44);
722                 OP(4, 0x114, 0x40, MULTI_FRONT_R, 0x44);
723                 OP(4, 0x115, 0x40, MULTI_REAR_L, 0x44);
724                 OP(4, 0x116, 0x40, MULTI_REAR_R, 0x44);
725                 OP(4, 0x117, 0x40, MULTI_CENTER, 0x44);
726                 OP(4, 0x118, 0x40, MULTI_LFE, 0x44);
727         
728                 INPUT_PATCH_END(patch);
729
730
731                 //Routing patch start   
732                 ROUTING_PATCH_START(rpatch, "Routing");
733                 GET_INPUT_GPR(rpatch, 0x100, 0x0);
734                 GET_INPUT_GPR(rpatch, 0x101, 0x1);
735                 GET_INPUT_GPR(rpatch, 0x10c, 0x12);
736                 GET_INPUT_GPR(rpatch, 0x10e, 0x13);
737                 GET_INPUT_GPR(rpatch, 0x113, MULTI_FRONT_L);
738                 GET_INPUT_GPR(rpatch, 0x114, MULTI_FRONT_R);
739                 GET_INPUT_GPR(rpatch, 0x115, MULTI_REAR_L);
740                 GET_INPUT_GPR(rpatch, 0x116, MULTI_REAR_R);
741                 GET_INPUT_GPR(rpatch, 0x117, MULTI_CENTER);
742                 GET_INPUT_GPR(rpatch, 0x118, MULTI_LFE);
743
744                 GET_DYNAMIC_GPR(rpatch, 0x102);
745                 GET_DYNAMIC_GPR(rpatch, 0x103);
746
747                 GET_OUTPUT_GPR(rpatch, 0x104, 0x8);
748                 GET_OUTPUT_GPR(rpatch, 0x105, 0x9);
749                 GET_OUTPUT_GPR(rpatch, 0x10a, 0x2);
750                 GET_OUTPUT_GPR(rpatch, 0x10b, 0x3);
751                 
752                 
753                 /* input buffer */
754                 OP(6, 0x102, AC97_IN_L, 0x40, 0x40);
755                 OP(6, 0x103, AC97_IN_R, 0x40, 0x40);
756
757
758                 /* Digital In + PCM + MULTI_FRONT-> AC97 out (front speakers)*/
759                 OP(6, AC97_FRONT_L, 0x100, 0x10c, 0x113);
760
761                 CONNECT(MULTI_FRONT_L, AC97_FRONT_L);
762                 CONNECT(PCM_IN_L, AC97_FRONT_L);
763                 CONNECT(SPDIF_CD_L, AC97_FRONT_L);
764
765                 OP(6, AC97_FRONT_R, 0x101, 0x10e, 0x114);
766
767                 CONNECT(MULTI_FRONT_R, AC97_FRONT_R);
768                 CONNECT(PCM_IN_R, AC97_FRONT_R);
769                 CONNECT(SPDIF_CD_R, AC97_FRONT_R);
770
771                 /* Digital In + PCM + AC97 In + PCM1 + MULTI_REAR --> Rear Channel */ 
772                 OP(6, 0x104, PCM1_IN_L, 0x100, 0x115);
773                 OP(6, 0x104, 0x104, 0x10c, 0x102);
774
775                 CONNECT(MULTI_REAR_L, ANALOG_REAR_L);
776                 CONNECT(AC97_IN_L, ANALOG_REAR_L);
777                 CONNECT(PCM_IN_L, ANALOG_REAR_L);
778                 CONNECT(SPDIF_CD_L, ANALOG_REAR_L);
779                 CONNECT(PCM1_IN_L, ANALOG_REAR_L);
780
781                 OP(6, 0x105, PCM1_IN_R, 0x101, 0x116);
782                 OP(6, 0x105, 0x105, 0x10e, 0x103);
783
784                 CONNECT(MULTI_REAR_R, ANALOG_REAR_R);
785                 CONNECT(AC97_IN_R, ANALOG_REAR_R);
786                 CONNECT(PCM_IN_R, ANALOG_REAR_R);
787                 CONNECT(SPDIF_CD_R, ANALOG_REAR_R);
788                 CONNECT(PCM1_IN_R, ANALOG_REAR_R);
789
790                 /* Digital In + PCM + AC97 In + MULTI_FRONT --> Digital out */
791                 OP(6, 0x10b, 0x100, 0x102, 0x10c);
792                 OP(6, 0x10b, 0x10b, 0x113, 0x40);
793
794                 CONNECT(MULTI_FRONT_L, DIGITAL_OUT_L);
795                 CONNECT(PCM_IN_L, DIGITAL_OUT_L);
796                 CONNECT(AC97_IN_L, DIGITAL_OUT_L);
797                 CONNECT(SPDIF_CD_L, DIGITAL_OUT_L);
798
799                 OP(6, 0x10a, 0x101, 0x103, 0x10e);
800                 OP(6, 0x10b, 0x10b, 0x114, 0x40);
801
802                 CONNECT(MULTI_FRONT_R, DIGITAL_OUT_R);
803                 CONNECT(PCM_IN_R, DIGITAL_OUT_R);
804                 CONNECT(AC97_IN_R, DIGITAL_OUT_R);
805                 CONNECT(SPDIF_CD_R, DIGITAL_OUT_R);
806
807                 /* AC97 In --> ADC Recording Buffer */
808                 OP(6, ADC_REC_L, 0x102, 0x40, 0x40);
809
810                 CONNECT(AC97_IN_L, ADC_REC_L);
811
812                 OP(6, ADC_REC_R, 0x103, 0x40, 0x40);
813
814                 CONNECT(AC97_IN_R, ADC_REC_R);
815
816
817                 /* fx12:Analog-Center */
818                 OP(6, ANALOG_CENTER, 0x117, 0x40, 0x40);
819                 CONNECT(MULTI_CENTER, ANALOG_CENTER);
820
821                 /* fx11:Analog-LFE */
822                 OP(6, ANALOG_LFE, 0x118, 0x40, 0x40);
823                 CONNECT(MULTI_LFE, ANALOG_LFE);
824
825                 /* fx12:Digital-Center */
826                 OP(6, DIGITAL_CENTER, 0x117, 0x40, 0x40);
827                 CONNECT(MULTI_CENTER, DIGITAL_CENTER);
828                 
829                 /* fx11:Analog-LFE */
830                 OP(6, DIGITAL_LFE, 0x118, 0x40, 0x40);
831                 CONNECT(MULTI_LFE, DIGITAL_LFE);
832         
833                 ROUTING_PATCH_END(rpatch);
834
835
836                 // Rear volume control  
837                 OUTPUT_PATCH_START(patch, "Vol Rear L", 0x8, 0);
838                 GET_INPUT_GPR(patch, 0x104, 0x8);
839                 GET_CONTROL_GPR(patch, 0x119, "Vol", 0, 0x7fffffff);
840
841                 OP(0, ANALOG_REAR_L, 0x040, 0x104, 0x119);
842                 OUTPUT_PATCH_END(patch);
843
844                 OUTPUT_PATCH_START(patch, "Vol Rear R", 0x9, 0);
845                 GET_INPUT_GPR(patch, 0x105, 0x9);
846                 GET_CONTROL_GPR(patch, 0x11a, "Vol", 0, 0x7fffffff);
847
848                 OP(0, ANALOG_REAR_R, 0x040, 0x105, 0x11a);
849                 OUTPUT_PATCH_END(patch);
850
851
852                 //Master volume control on front-digital        
853                 OUTPUT_PATCH_START(patch, "Vol Master L", 0x2, 1);
854                 GET_INPUT_GPR(patch, 0x10a, 0x2);
855                 GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
856
857                 OP(0, DIGITAL_OUT_L, 0x040, 0x10a, 0x108);
858                 OUTPUT_PATCH_END(patch);
859
860
861                 OUTPUT_PATCH_START(patch, "Vol Master R", 0x3, 1);
862                 GET_INPUT_GPR(patch, 0x10b, 0x3);
863                 GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
864
865                 OP(0, DIGITAL_OUT_R, 0x040, 0x10b, 0x109);
866                 OUTPUT_PATCH_END(patch);
867
868
869                 /* delimiter patch */
870                 patch = PATCH(mgr, patch_n);
871                 patch->code_size = 0;
872
873         
874                 sblive_writeptr(card, DBG, 0, 0);
875         }
876
877         spin_lock_init(&mgr->lock);
878
879         // Set up Volume controls, try to keep this the same for both Audigy and Live
880
881         //Master volume
882         mgr->ctrl_gpr[SOUND_MIXER_VOLUME][0] = 8;
883         mgr->ctrl_gpr[SOUND_MIXER_VOLUME][1] = 9;
884
885         left = card->ac97->mixer_state[SOUND_MIXER_VOLUME] & 0xff;
886         right = (card->ac97->mixer_state[SOUND_MIXER_VOLUME] >> 8) & 0xff;
887
888         emu10k1_set_volume_gpr(card, 8, left, 1 << card->ac97->bit_resolution);
889         emu10k1_set_volume_gpr(card, 9, right, 1 << card->ac97->bit_resolution);
890
891         //Rear volume
892         mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][0] = 0x19;
893         mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][1] = 0x1a;
894
895         left = right = 67;
896         card->ac97->mixer_state[SOUND_MIXER_OGAIN] = (right << 8) | left;
897
898         card->ac97->supported_mixers |= SOUND_MASK_OGAIN;
899         card->ac97->stereo_mixers |= SOUND_MASK_OGAIN;
900
901         emu10k1_set_volume_gpr(card, 0x19, left, VOL_5BIT);
902         emu10k1_set_volume_gpr(card, 0x1a, right, VOL_5BIT);
903
904         //PCM Volume
905         mgr->ctrl_gpr[SOUND_MIXER_PCM][0] = 6;
906         mgr->ctrl_gpr[SOUND_MIXER_PCM][1] = 7;
907
908         left = card->ac97->mixer_state[SOUND_MIXER_PCM] & 0xff;
909         right = (card->ac97->mixer_state[SOUND_MIXER_PCM] >> 8) & 0xff;
910
911         emu10k1_set_volume_gpr(card, 6, left, VOL_5BIT);
912         emu10k1_set_volume_gpr(card, 7, right, VOL_5BIT);
913
914         //CD-Digital Volume
915         mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][0] = 0xd;
916         mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][1] = 0xf;
917
918         left = right = 67;
919         card->ac97->mixer_state[SOUND_MIXER_DIGITAL1] = (right << 8) | left; 
920
921         card->ac97->supported_mixers |= SOUND_MASK_DIGITAL1;
922         card->ac97->stereo_mixers |= SOUND_MASK_DIGITAL1;
923
924         emu10k1_set_volume_gpr(card, 0xd, left, VOL_5BIT);
925         emu10k1_set_volume_gpr(card, 0xf, right, VOL_5BIT);
926
927
928         //hard wire the ac97's pcm, pcm volume is done above using dsp code.
929         if (card->is_audigy)
930                 //for Audigy, we mute it and use the philips 6 channel DAC instead
931                 emu10k1_ac97_write(card->ac97, 0x18, 0x8000);
932         else
933                 //For the Live we hardwire it to full volume
934                 emu10k1_ac97_write(card->ac97, 0x18, 0x0);
935
936         //remove it from the ac97_codec's control
937         card->ac97_supported_mixers &= ~SOUND_MASK_PCM;
938         card->ac97_stereo_mixers &= ~SOUND_MASK_PCM;
939
940         //set Igain to 0dB by default, maybe consider hardwiring it here.
941         emu10k1_ac97_write(card->ac97, AC97_RECORD_GAIN, 0x0000);
942         card->ac97->mixer_state[SOUND_MIXER_IGAIN] = 0x101; 
943
944         return 0;
945 }
946
947 static int __devinit hw_init(struct emu10k1_card *card)
948 {
949         int nCh;
950         u32 pagecount; /* tmp */
951         int ret;
952
953         /* Disable audio and lock cache */
954         emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
955
956         /* Reset recording buffers */
957         sblive_writeptr_tag(card, 0,
958                             MICBS, ADCBS_BUFSIZE_NONE,
959                             MICBA, 0,
960                             FXBS, ADCBS_BUFSIZE_NONE,
961                             FXBA, 0,
962                             ADCBS, ADCBS_BUFSIZE_NONE,
963                             ADCBA, 0,
964                             TAGLIST_END);
965
966         /* Disable channel interrupt */
967         emu10k1_writefn0(card, INTE, 0);
968         sblive_writeptr_tag(card, 0,
969                             CLIEL, 0,
970                             CLIEH, 0,
971                             SOLEL, 0,
972                             SOLEH, 0,
973                             TAGLIST_END);
974
975         if (card->is_audigy) {
976                 sblive_writeptr_tag(card,0,
977                                     0x5e,0xf00,
978                                     0x5f,0x3,
979                                     TAGLIST_END);
980         }
981
982         /* Init envelope engine */
983         for (nCh = 0; nCh < NUM_G; nCh++) {
984                 sblive_writeptr_tag(card, nCh,
985                                     DCYSUSV, 0,
986                                     IP, 0,
987                                     VTFT, 0xffff,
988                                     CVCF, 0xffff,
989                                     PTRX, 0,
990                                     //CPF, 0,
991                                     CCR, 0,
992
993                                     PSST, 0,
994                                     DSL, 0x10,
995                                     CCCA, 0,
996                                     Z1, 0,
997                                     Z2, 0,
998                                     FXRT, 0xd01c0000,
999
1000                                     ATKHLDM, 0,
1001                                     DCYSUSM, 0,
1002                                     IFATN, 0xffff,
1003                                     PEFE, 0,
1004                                     FMMOD, 0,
1005                                     TREMFRQ, 24,        /* 1 Hz */
1006                                     FM2FRQ2, 24,        /* 1 Hz */
1007                                     TEMPENV, 0,
1008
1009                                     /*** These are last so OFF prevents writing ***/
1010                                     LFOVAL2, 0,
1011                                     LFOVAL1, 0,
1012                                     ATKHLDV, 0,
1013                                     ENVVOL, 0,
1014                                     ENVVAL, 0,
1015                                     TAGLIST_END);
1016                 sblive_writeptr(card, CPF, nCh, 0);
1017                 /*
1018                   Audigy FXRT initialization
1019                   reversed eng'd, may not be accurate.
1020                  */
1021                 if (card->is_audigy) {
1022                         sblive_writeptr_tag(card,nCh,
1023                                             0x4c,0x0,
1024                                             0x4d,0x0,
1025                                             0x4e,0x0,
1026                                             0x4f,0x0,
1027                                             A_FXRT1, 0x3f3f3f3f,
1028                                             A_FXRT2, 0x3f3f3f3f,
1029                                             A_SENDAMOUNTS, 0,
1030                                             TAGLIST_END);
1031                 }
1032         }
1033         
1034
1035         /*
1036          ** Init to 0x02109204 :
1037          ** Clock accuracy    = 0     (1000ppm)
1038          ** Sample Rate       = 2     (48kHz)
1039          ** Audio Channel     = 1     (Left of 2)
1040          ** Source Number     = 0     (Unspecified)
1041          ** Generation Status = 1     (Original for Cat Code 12)
1042          ** Cat Code          = 12    (Digital Signal Mixer)
1043          ** Mode              = 0     (Mode 0)
1044          ** Emphasis          = 0     (None)
1045          ** CP                = 1     (Copyright unasserted)
1046          ** AN                = 0     (Digital audio)
1047          ** P                 = 0     (Consumer)
1048          */
1049
1050         sblive_writeptr_tag(card, 0,
1051
1052                             /* SPDIF0 */
1053                             SPCS0, (SPCS_CLKACCY_1000PPM | 0x002000000 |
1054                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
1055
1056                             /* SPDIF1 */
1057                             SPCS1, (SPCS_CLKACCY_1000PPM | 0x002000000 |
1058                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
1059
1060                             /* SPDIF2 & SPDIF3 */
1061                             SPCS2, (SPCS_CLKACCY_1000PPM | 0x002000000 |
1062                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
1063
1064                             TAGLIST_END);
1065
1066         if (card->is_audigy && (card->chiprev == 4)) {
1067                 /* Hacks for Alice3 to work independent of haP16V driver */
1068                 u32 tmp;
1069
1070                 //Setup SRCMulti_I2S SamplingRate
1071                 tmp = sblive_readptr(card, A_SPDIF_SAMPLERATE, 0);
1072                 tmp &= 0xfffff1ff;
1073                 tmp |= (0x2<<9);
1074                 sblive_writeptr(card, A_SPDIF_SAMPLERATE, 0, tmp);
1075
1076                 /* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
1077                 emu10k1_writefn0(card, 0x20, 0x600000);
1078                 emu10k1_writefn0(card, 0x24, 0x14);
1079
1080                 /* Setup SRCMulti Input Audio Enable */
1081                 emu10k1_writefn0(card, 0x20, 0x6E0000);
1082                 emu10k1_writefn0(card, 0x24, 0xFF00FF00);
1083         }
1084
1085         ret = fx_init(card);            /* initialize effects engine */
1086         if (ret < 0)
1087                 return ret;
1088
1089         card->tankmem.size = 0;
1090
1091         card->virtualpagetable.size = MAXPAGES * sizeof(u32);
1092
1093         card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle);
1094         if (card->virtualpagetable.addr == NULL) {
1095                 ERROR();
1096                 ret = -ENOMEM;
1097                 goto err0;
1098         }
1099
1100         card->silentpage.size = EMUPAGESIZE;
1101
1102         card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle);
1103         if (card->silentpage.addr == NULL) {
1104                 ERROR();
1105                 ret = -ENOMEM;
1106                 goto err1;
1107         }
1108
1109         for (pagecount = 0; pagecount < MAXPAGES; pagecount++)
1110                 ((u32 *) card->virtualpagetable.addr)[pagecount] = cpu_to_le32(((u32) card->silentpage.dma_handle * 2) | pagecount);
1111
1112         /* Init page table & tank memory base register */
1113         sblive_writeptr_tag(card, 0,
1114                             PTB, (u32) card->virtualpagetable.dma_handle,
1115                             TCB, 0,
1116                             TCBS, 0,
1117                             TAGLIST_END);
1118
1119         for (nCh = 0; nCh < NUM_G; nCh++) {
1120                 sblive_writeptr_tag(card, nCh,
1121                                     MAPA, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
1122                                     MAPB, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
1123                                     TAGLIST_END);
1124         }
1125
1126         /* Hokay, now enable the AUD bit */
1127         /* Enable Audio = 1 */
1128         /* Mute Disable Audio = 0 */
1129         /* Lock Tank Memory = 1 */
1130         /* Lock Sound Memory = 0 */
1131         /* Auto Mute = 1 */
1132         if (card->is_audigy) {
1133                 if (card->chiprev == 4)
1134                         emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_AC3ENABLE_CDSPDIF | HCFG_AC3ENABLE_GPSPDIF | HCFG_AUTOMUTE | HCFG_JOYENABLE);
1135                 else
1136                         emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_AUTOMUTE | HCFG_JOYENABLE);
1137         } else {
1138                 if (card->model == 0x20 || card->model == 0xc400 ||
1139                  (card->model == 0x21 && card->chiprev < 6))
1140                         emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE);
1141                 else
1142                         emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE);
1143         }
1144         /* Enable Vol_Ctrl irqs */
1145         emu10k1_irq_enable(card, INTE_VOLINCRENABLE | INTE_VOLDECRENABLE | INTE_MUTEENABLE | INTE_FXDSPENABLE);
1146
1147         if (card->is_audigy && (card->chiprev == 4)) {
1148                 /* Unmute Analog now.  Set GPO6 to 1 for Apollo.
1149                  * This has to be done after init ALice3 I2SOut beyond 48KHz.
1150                  * So, sequence is important. */
1151                 u32 tmp = emu10k1_readfn0(card, A_IOCFG);
1152                 tmp |= 0x0040;
1153                 emu10k1_writefn0(card, A_IOCFG, tmp);
1154         }
1155         
1156         /* FIXME: TOSLink detection */
1157         card->has_toslink = 0;
1158
1159         /* Initialize digital passthrough variables */
1160         card->pt.pos_gpr = card->pt.intr_gpr = card->pt.enable_gpr = -1;
1161         card->pt.selected = 0;
1162         card->pt.state = PT_STATE_INACTIVE;
1163         card->pt.spcs_to_use = 0x01;
1164         card->pt.patch_name = "AC3pass";
1165         card->pt.intr_gpr_name = "count";
1166         card->pt.enable_gpr_name = "enable";
1167         card->pt.pos_gpr_name = "ptr";
1168         spin_lock_init(&card->pt.lock);
1169         init_waitqueue_head(&card->pt.wait);
1170
1171 /*      tmp = sblive_readfn0(card, HCFG);
1172         if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
1173                 sblive_writefn0(card, HCFG, tmp | 0x800);
1174
1175                 udelay(512);
1176
1177                 if (tmp != (sblive_readfn0(card, HCFG) & ~0x800)) {
1178                         card->has_toslink = 1;
1179                         sblive_writefn0(card, HCFG, tmp);
1180                 }
1181         }
1182 */
1183         return 0;
1184
1185   err1:
1186         pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
1187   err0:
1188         fx_cleanup(&card->mgr);
1189
1190         return ret;
1191 }
1192
1193 static int __devinit emu10k1_init(struct emu10k1_card *card)
1194 {
1195         /* Init Card */
1196         if (hw_init(card) < 0)
1197                 return -1;
1198
1199         voice_init(card);
1200         timer_init(card);
1201         addxmgr_init(card);
1202
1203         DPD(2, "  hw control register -> %#x\n", emu10k1_readfn0(card, HCFG));
1204
1205         return 0;
1206 }
1207
1208 static void emu10k1_cleanup(struct emu10k1_card *card)
1209 {
1210         int ch;
1211
1212         emu10k1_writefn0(card, INTE, 0);
1213
1214         /** Shutdown the chip **/
1215         for (ch = 0; ch < NUM_G; ch++)
1216                 sblive_writeptr(card, DCYSUSV, ch, 0);
1217
1218         for (ch = 0; ch < NUM_G; ch++) {
1219                 sblive_writeptr_tag(card, ch,
1220                                     VTFT, 0,
1221                                     CVCF, 0,
1222                                     PTRX, 0,
1223                                     //CPF, 0,
1224                                     TAGLIST_END);
1225                 sblive_writeptr(card, CPF, ch, 0);
1226         }
1227
1228         /* Disable audio and lock cache */
1229         emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
1230
1231         sblive_writeptr_tag(card, 0,
1232                             PTB, 0,
1233
1234                             /* Reset recording buffers */
1235                             MICBS, ADCBS_BUFSIZE_NONE,
1236                             MICBA, 0,
1237                             FXBS, ADCBS_BUFSIZE_NONE,
1238                             FXBA, 0,
1239                             FXWC, 0,
1240                             ADCBS, ADCBS_BUFSIZE_NONE,
1241                             ADCBA, 0,
1242                             TCBS, 0,
1243                             TCB, 0,
1244                             DBG, 0x8000,
1245
1246                             /* Disable channel interrupt */
1247                             CLIEL, 0,
1248                             CLIEH, 0,
1249                             SOLEL, 0,
1250                             SOLEH, 0,
1251                             TAGLIST_END);
1252
1253         if (card->is_audigy)
1254                 sblive_writeptr(card, 0, A_DBG,  A_DBG_SINGLE_STEP);
1255
1256         pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
1257         pci_free_consistent(card->pci_dev, card->silentpage.size, card->silentpage.addr, card->silentpage.dma_handle);
1258         
1259         if(card->tankmem.size != 0)
1260                 pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);
1261
1262         /* release patch storage memory */
1263         fx_cleanup(&card->mgr);
1264 }
1265
1266 /* Driver initialization routine */
1267 static int __devinit emu10k1_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
1268 {
1269         struct emu10k1_card *card;
1270         u32 subsysvid;
1271         int ret;
1272
1273         if (pci_set_dma_mask(pci_dev, EMU10K1_DMA_MASK)) {
1274                 printk(KERN_ERR "emu10k1: architecture does not support 29bit PCI busmaster DMA\n");
1275                 return -ENODEV;
1276         }
1277
1278         if (pci_enable_device(pci_dev))
1279                 return -EIO;
1280
1281         pci_set_master(pci_dev);
1282
1283         if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
1284                 printk(KERN_ERR "emu10k1: out of memory\n");
1285                 return -ENOMEM;
1286         }
1287         memset(card, 0, sizeof(struct emu10k1_card));
1288
1289         card->iobase = pci_resource_start(pci_dev, 0);
1290         card->length = pci_resource_len(pci_dev, 0); 
1291
1292         if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
1293                 printk(KERN_ERR "emu10k1: IO space in use\n");
1294                 ret = -EBUSY;
1295                 goto err_region;
1296         }
1297
1298         pci_set_drvdata(pci_dev, card);
1299
1300         card->irq = pci_dev->irq;
1301         card->pci_dev = pci_dev;
1302
1303         /* Reserve IRQ Line */
1304         if (request_irq(card->irq, emu10k1_interrupt, IRQF_SHARED, card_names[pci_id->driver_data], card)) {
1305                 printk(KERN_ERR "emu10k1: IRQ in use\n");
1306                 ret = -EBUSY;
1307                 goto err_irq;
1308         }
1309
1310         pci_read_config_byte(pci_dev, PCI_REVISION_ID, &card->chiprev);
1311         pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &card->model);
1312
1313         printk(KERN_INFO "emu10k1: %s rev %d model %#04x found, IO at %#04lx-%#04lx, IRQ %d\n",
1314                 card_names[pci_id->driver_data], card->chiprev, card->model, card->iobase,
1315                 card->iobase + card->length - 1, card->irq);
1316
1317         if (pci_id->device == PCI_DEVICE_ID_CREATIVE_AUDIGY)
1318                 card->is_audigy = 1;
1319
1320         pci_read_config_dword(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &subsysvid);
1321         card->is_aps = (subsysvid == EMU_APS_SUBID);
1322
1323         spin_lock_init(&card->lock);
1324         mutex_init(&card->open_sem);
1325         card->open_mode = 0;
1326         init_waitqueue_head(&card->open_wait);
1327
1328         ret = emu10k1_audio_init(card);
1329         if (ret < 0) {
1330                 printk(KERN_ERR "emu10k1: cannot initialize audio devices\n");
1331                 goto err_audio;
1332         }
1333
1334         ret = emu10k1_mixer_init(card);
1335         if (ret < 0) {
1336                 printk(KERN_ERR "emu10k1: cannot initialize AC97 codec\n");
1337                 goto err_mixer;
1338         }
1339
1340         ret = emu10k1_midi_init(card);
1341         if (ret < 0) {
1342                 printk(KERN_ERR "emu10k1: cannot register midi device\n");
1343                 goto err_midi;
1344         }
1345
1346         ret = emu10k1_init(card);
1347         if (ret < 0) {
1348                 printk(KERN_ERR "emu10k1: cannot initialize device\n");
1349                 goto err_emu10k1_init;
1350         }
1351
1352         if (card->is_aps)
1353                 emu10k1_ecard_init(card);
1354
1355         ret = emu10k1_register_devices(card);
1356         if (ret < 0)
1357                 goto err_register;
1358
1359         /* proc entries must be created after registering devices, as
1360          * emu10k1_info_proc prints card->audio_dev &co. */
1361         ret = emu10k1_proc_init(card);
1362         if (ret < 0) {
1363                 printk(KERN_ERR "emu10k1: cannot initialize proc directory\n");
1364                 goto err_proc;
1365         }
1366         
1367         list_add(&card->list, &emu10k1_devs);
1368
1369         return 0;
1370
1371 err_proc:
1372         emu10k1_unregister_devices(card);
1373
1374 err_register:
1375         emu10k1_cleanup(card);
1376         
1377 err_emu10k1_init:
1378         emu10k1_midi_cleanup(card);
1379
1380 err_midi:
1381         emu10k1_mixer_cleanup(card);
1382
1383 err_mixer:
1384         emu10k1_audio_cleanup(card);
1385
1386 err_audio:
1387         free_irq(card->irq, card);
1388
1389 err_irq:
1390         release_region(card->iobase, card->length);
1391         pci_set_drvdata(pci_dev, NULL);
1392
1393 err_region:
1394         kfree(card);
1395
1396         return ret;
1397 }
1398
1399 static void __devexit emu10k1_remove(struct pci_dev *pci_dev)
1400 {
1401         struct emu10k1_card *card = pci_get_drvdata(pci_dev);
1402
1403         list_del(&card->list);
1404
1405         emu10k1_unregister_devices(card);
1406         emu10k1_cleanup(card);
1407         emu10k1_midi_cleanup(card);
1408         emu10k1_mixer_cleanup(card);
1409         emu10k1_proc_cleanup(card);
1410         emu10k1_audio_cleanup(card);    
1411         free_irq(card->irq, card);
1412         release_region(card->iobase, card->length);
1413         kfree(card);
1414         pci_set_drvdata(pci_dev, NULL);
1415 }
1416
1417 MODULE_AUTHOR("Bertrand Lee, Cai Ying. (Email to: emu10k1-devel@lists.sourceforge.net)");
1418 MODULE_DESCRIPTION("Creative EMU10K1 PCI Audio Driver v" DRIVER_VERSION "\nCopyright (C) 1999 Creative Technology Ltd.");
1419 MODULE_LICENSE("GPL");
1420
1421 static struct pci_driver emu10k1_pci_driver = {
1422         .name           = "emu10k1",
1423         .id_table       = emu10k1_pci_tbl,
1424         .probe          = emu10k1_probe,
1425         .remove         = __devexit_p(emu10k1_remove),
1426 };
1427
1428 static int __init emu10k1_init_module(void)
1429 {
1430         printk(KERN_INFO "Creative EMU10K1 PCI Audio Driver, version " DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
1431
1432         return pci_register_driver(&emu10k1_pci_driver);
1433 }
1434
1435 static void __exit emu10k1_cleanup_module(void)
1436 {
1437         pci_unregister_driver(&emu10k1_pci_driver);
1438
1439         return;
1440 }
1441
1442 module_init(emu10k1_init_module);
1443 module_exit(emu10k1_cleanup_module);
1444
1445 #ifdef EMU10K1_SEQUENCER
1446
1447 /* in midi.c */
1448 extern int emu10k1_seq_midi_open(int dev, int mode, 
1449                                 void (*input)(int dev, unsigned char midi_byte),
1450                                 void (*output)(int dev));
1451 extern void emu10k1_seq_midi_close(int dev);
1452 extern int emu10k1_seq_midi_out(int dev, unsigned char midi_byte);
1453 extern int emu10k1_seq_midi_start_read(int dev);
1454 extern int emu10k1_seq_midi_end_read(int dev);
1455 extern void emu10k1_seq_midi_kick(int dev);
1456 extern int emu10k1_seq_midi_buffer_status(int dev);
1457
1458 static struct midi_operations emu10k1_midi_operations =
1459 {
1460         THIS_MODULE,
1461         {"EMU10K1 MIDI", 0, 0, SNDCARD_EMU10K1},
1462         &std_midi_synth,
1463         {0},
1464         emu10k1_seq_midi_open,
1465         emu10k1_seq_midi_close,
1466         NULL,
1467         emu10k1_seq_midi_out,
1468         emu10k1_seq_midi_start_read,
1469         emu10k1_seq_midi_end_read,
1470         emu10k1_seq_midi_kick,
1471         NULL,
1472         emu10k1_seq_midi_buffer_status,
1473         NULL
1474 };
1475
1476 #endif