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