v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / sound / 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  *          
70  *********************************************************************/
71
72 /* These are only included once per module */
73 #include <linux/version.h>
74 #include <linux/module.h>
75 #include <linux/slab.h>
76 #include <linux/init.h>
77 #include <linux/delay.h>
78 #include <linux/proc_fs.h>
79
80 #include "hwaccess.h"
81 #include "8010.h"
82 #include "efxmgr.h"
83 #include "cardwo.h"
84 #include "cardwi.h"
85 #include "cardmo.h"
86 #include "cardmi.h"
87 #include "recmgr.h"
88 #include "ecard.h"
89
90
91 #ifdef EMU10K1_SEQUENCER
92 #define MIDI_SYNTH_NAME "EMU10K1 MIDI"
93 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
94  
95 #include "../sound_config.h"
96 #include "../midi_synth.h"
97
98 /* this should be in dev_table.h */
99 #define SNDCARD_EMU10K1 46
100 #endif
101  
102 #define DRIVER_VERSION "0.15"
103
104 /* FIXME: is this right? */
105 /* does the card support 32 bit bus master?*/
106 #define EMU10K1_DMA_MASK                0xffffffff      /* DMA buffer mask for pci_alloc_consist */
107
108 #ifndef PCI_VENDOR_ID_CREATIVE
109 #define PCI_VENDOR_ID_CREATIVE 0x1102
110 #endif
111
112 #ifndef PCI_DEVICE_ID_CREATIVE_EMU10K1
113 #define PCI_DEVICE_ID_CREATIVE_EMU10K1 0x0002
114 #endif
115
116 #define EMU_APS_SUBID   0x40011102
117  
118 enum {
119         EMU10K1 = 0,
120 };
121
122 static char *card_names[] __devinitdata = {
123         "EMU10K1",
124 };
125
126 static struct pci_device_id emu10k1_pci_tbl[] = {
127         {PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_EMU10K1,
128          PCI_ANY_ID, PCI_ANY_ID, 0, 0, EMU10K1},
129         {0,}
130 };
131
132 MODULE_DEVICE_TABLE(pci, emu10k1_pci_tbl);
133
134 /* Global var instantiation */
135
136 LIST_HEAD(emu10k1_devs);
137
138 extern struct file_operations emu10k1_audio_fops;
139 extern struct file_operations emu10k1_mixer_fops;
140 extern struct file_operations emu10k1_midi_fops;
141
142 #ifdef EMU10K1_SEQUENCER
143 static struct midi_operations emu10k1_midi_operations;
144 #endif
145
146 extern void emu10k1_interrupt(int, void *, struct pt_regs *s);
147
148 static int __devinit emu10k1_audio_init(struct emu10k1_card *card)
149 {
150         card->audio_dev = register_sound_dsp(&emu10k1_audio_fops, -1);
151         if (card->audio_dev < 0) {
152                 printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
153                 goto err_dev;
154         }
155
156         card->audio_dev1 = register_sound_dsp(&emu10k1_audio_fops, -1);
157         if (card->audio_dev1 < 0) {
158                 printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
159                 goto err_dev1;
160         }
161
162         /* Assign default playback voice parameters */
163         card->mchannel_fx = 8;
164         /* mono voice */
165         card->waveout.send_a[0] = 0xff;
166         card->waveout.send_b[0] = 0xff;
167         card->waveout.send_c[0] = 0x00;
168         card->waveout.send_d[0] = 0x00;
169         card->waveout.send_routing[0] = 0x3210;
170
171         /* stereo voice */
172         /* left */
173         card->waveout.send_a[1] = 0xff;
174         card->waveout.send_b[1] = 0x00;
175         card->waveout.send_c[1] = 0x00;
176         card->waveout.send_d[1] = 0x00;
177         card->waveout.send_routing[1] = 0x3210;
178
179         /* right */
180         card->waveout.send_a[2] = 0x00;
181         card->waveout.send_b[2] = 0xff;
182         card->waveout.send_c[2] = 0x00;
183         card->waveout.send_d[2] = 0x00;
184         card->waveout.send_routing[2] = 0x3210;
185
186         /* Assign default recording parameters */
187         /* FIXME */
188         if(card->isaps)
189                 card->wavein.recsrc = WAVERECORD_FX;
190         else
191                 card->wavein.recsrc = WAVERECORD_AC97;
192
193         card->wavein.fxwc = 0x0003;
194         return 0;
195
196 err_dev1:
197         unregister_sound_dsp(card->audio_dev);
198 err_dev:
199         return -ENODEV;
200 }
201
202 static void __devinit emu10k1_audio_cleanup(struct emu10k1_card *card)
203 {
204         unregister_sound_dsp(card->audio_dev1);
205         unregister_sound_dsp(card->audio_dev);
206 }
207
208 static int __devinit emu10k1_mixer_init(struct emu10k1_card *card)
209 {
210         char s[32];
211         card->ac97.dev_mixer = register_sound_mixer(&emu10k1_mixer_fops, -1);
212         if (card->ac97.dev_mixer < 0) {
213                 printk(KERN_ERR "emu10k1: cannot register mixer device\n");
214                 return -EIO;
215         }
216
217         card->ac97.private_data = card;
218
219         if(!card->isaps) {
220                 card->ac97.id = 0;
221                 card->ac97.codec_read = emu10k1_ac97_read;
222                 card->ac97.codec_write = emu10k1_ac97_write;
223
224                 if (ac97_probe_codec (&card->ac97) == 0) {
225                         printk(KERN_ERR "emu10k1: unable to probe AC97 codec\n");
226                         goto err_out;
227                 }
228                 /* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
229                         does not support this, it shouldn't do any harm */
230                 sblive_writeptr(card, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
231                 
232
233                 if (!proc_mkdir ("driver/emu10k1", 0)) {
234                         printk(KERN_ERR "emu10k1: unable to create proc directory driver/emu10k1\n");
235                         goto err_out;
236                 }
237
238                 sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
239                 if (!proc_mkdir (s, 0)) {
240                         printk(KERN_ERR "emu10k1: unable to create proc directory %s\n", s);
241                         goto err_emu10k1_proc;
242                 }
243         
244                 sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
245                 if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, &card->ac97)) {
246                         printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
247                         goto err_ac97_proc;
248                 }
249
250                 card->ac97_supported_mixers = card->ac97.supported_mixers;
251                 card->ac97_stereo_mixers = card->ac97.stereo_mixers;
252         }
253
254         return 0;
255
256  err_ac97_proc:
257         sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
258         remove_proc_entry(s, NULL);
259
260  err_emu10k1_proc:
261         remove_proc_entry("driver/emu10k1", NULL);
262  err_out:
263         unregister_sound_mixer (card->ac97.dev_mixer);
264         return -EIO;
265 }
266
267 static void __devinit emu10k1_mixer_cleanup(struct emu10k1_card *card)
268 {
269         char s[32];
270
271         if(!card->isaps) {
272                 sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
273                 remove_proc_entry(s, NULL);
274
275                 sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
276                 remove_proc_entry(s, NULL);
277
278                 remove_proc_entry("driver/emu10k1", NULL);
279         }
280
281         unregister_sound_mixer (card->ac97.dev_mixer);
282 }
283
284 static int __devinit emu10k1_midi_init(struct emu10k1_card *card)
285 {
286         int ret;
287
288         card->midi_dev = register_sound_midi(&emu10k1_midi_fops, -1);
289         if (card->midi_dev < 0) {
290                 printk(KERN_ERR "emu10k1: cannot register midi device!\n");
291                 return -ENODEV;
292         }
293
294
295         card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL);
296         if (card->mpuout == NULL) {
297                 printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuout: out of memory\n");
298                 ret = -ENOMEM;
299                 goto err_out1;
300         }
301
302         memset(card->mpuout, 0, sizeof(struct emu10k1_mpuout));
303
304         card->mpuout->intr = 1;
305         card->mpuout->status = FLAGS_AVAILABLE;
306         card->mpuout->state = CARDMIDIOUT_STATE_DEFAULT;
307
308         tasklet_init(&card->mpuout->tasklet, emu10k1_mpuout_bh, (unsigned long) card);
309
310         spin_lock_init(&card->mpuout->lock);
311
312         card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL);
313         if (card->mpuin == NULL) {
314                 printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuin: out of memory\n");
315                 ret = -ENOMEM;
316                 goto err_out2;
317         }
318
319         memset(card->mpuin, 0, sizeof(struct emu10k1_mpuin));
320
321         card->mpuin->status = FLAGS_AVAILABLE;
322
323         tasklet_init(&card->mpuin->tasklet, emu10k1_mpuin_bh, (unsigned long) card->mpuin);
324
325         spin_lock_init(&card->mpuin->lock);
326
327         /* Reset the MPU port */
328         if (emu10k1_mpu_reset(card) < 0) {
329                 ERROR();
330                 ret = -EIO;
331                 goto err_out3;
332         }
333
334 #ifdef EMU10K1_SEQUENCER
335         card->seq_dev = sound_alloc_mididev();
336         if(card->seq_dev == -1)
337                         printk(KERN_WARNING "emu10k1: unable to register sequencer device!");
338         else {
339                         std_midi_synth.midi_dev = card->seq_dev;
340                         midi_devs[card->seq_dev] = 
341                                         (struct midi_operations *)
342                                         kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
343                         
344                         if(midi_devs[card->seq_dev] == NULL) {
345                                         printk(KERN_ERR "emu10k1: unable to allocate memory!");
346                                         sound_unload_mididev(card->seq_dev);
347                                         card->seq_dev = -1;
348                                         return 0;
349                         } else {
350                                         memcpy((char *)midi_devs[card->seq_dev], 
351                                                    (char *)&emu10k1_midi_operations, 
352                                                    sizeof(struct midi_operations));
353                                         midi_devs[card->seq_dev]->devc = card;
354                                         sequencer_init();
355                         }
356         }
357         card->seq_mididev = 0;
358 #endif
359
360         return 0;
361
362 err_out3:
363         kfree(card->mpuin);
364 err_out2:
365         kfree(card->mpuout);
366 err_out1:
367         unregister_sound_midi(card->midi_dev);
368         return ret;
369 }
370
371 static void __devinit emu10k1_midi_cleanup(struct emu10k1_card *card)
372 {
373     tasklet_unlock_wait(&card->mpuout->tasklet);
374     kfree(card->mpuout);
375
376     tasklet_unlock_wait(&card->mpuin->tasklet);
377     kfree(card->mpuin);
378
379 #ifdef EMU10K1_SEQUENCER
380     if(card->seq_dev > -1) {
381                 kfree(midi_devs[card->seq_dev]);
382                         midi_devs[card->seq_dev] = NULL;
383             sound_unload_mididev(card->seq_dev);
384                         card->seq_dev = -1;
385     }
386 #endif
387
388         unregister_sound_midi(card->midi_dev);
389 }
390
391 static void __devinit voice_init(struct emu10k1_card *card)
392 {
393         int i;
394
395         for (i = 0; i < NUM_G; i++)
396                 card->voicetable[i] = VOICE_USAGE_FREE;
397 }
398
399 static void __devinit timer_init(struct emu10k1_card *card)
400 {
401         INIT_LIST_HEAD(&card->timers);
402         card->timer_delay = TIMER_STOPPED;
403         card->timer_lock = SPIN_LOCK_UNLOCKED;
404 }
405
406 static void __devinit addxmgr_init(struct emu10k1_card *card)
407 {
408         u32 count;
409
410         for (count = 0; count < MAXPAGES; count++)
411                 card->emupagetable[count] = 0;
412
413         /* Mark first page as used */
414         /* This page is reserved by the driver */
415         card->emupagetable[0] = 0x8001;
416         card->emupagetable[1] = MAXPAGES - 1;
417 }
418
419 static void __devinit fx_cleanup(struct patch_manager *mgr)
420 {
421         int i;
422         for(i = 0; i < mgr->current_pages; i++)
423                 free_page((unsigned long) mgr->patch[i]);
424 }
425
426 static int __devinit fx_init(struct emu10k1_card *card)
427 {
428         struct patch_manager *mgr = &card->mgr;
429         struct dsp_patch *patch;
430         struct dsp_rpatch *rpatch;
431         s32 left, right;
432         int i;
433         u32 pc = 0;
434         u32 patch_n;
435
436         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
437                 mgr->ctrl_gpr[i][0] = -1;
438                 mgr->ctrl_gpr[i][1] = -1;
439         }
440
441         for (i = 0; i < 512; i++)
442                 OP(6, 0x40, 0x40, 0x40, 0x40);
443
444         for (i = 0; i < 256; i++)
445                 sblive_writeptr_tag(card, 0,
446                                     FXGPREGBASE + i, 0,
447                                     TANKMEMADDRREGBASE + i, 0,
448                                     TAGLIST_END);
449
450         mgr->current_pages = 5 / PATCHES_PER_PAGE + 1;
451         for(i = 0; i < mgr->current_pages; i++) {
452                 mgr->patch[i] = (void *)__get_free_pages(GFP_KERNEL, 1);
453                 if (mgr->patch[i] == NULL) {
454                         mgr->current_pages = i;
455                         fx_cleanup(mgr);
456                         return -ENOMEM;
457                 }
458                 memset(mgr->patch[i], 0, PAGE_SIZE);
459         }
460
461         pc = 0;
462         patch_n = 0;
463
464         /* FX volume correction */
465         INPUT_PATCH_START(patch, "Pcm L vol correction", 0x0, 0);
466         GET_OUTPUT_GPR(patch, 0x100, 0x0);
467         
468         OP(4, 0x100, 0x40, PCM_IN_L, 0x44);
469         INPUT_PATCH_END(patch);
470
471
472         INPUT_PATCH_START(patch, "Pcm R vol correction", 0x1, 0);
473         GET_OUTPUT_GPR(patch, 0x101, 0x1);
474
475         OP(4, 0x101, 0x40, PCM_IN_R, 0x44);
476         INPUT_PATCH_END(patch);
477
478
479         ROUTING_PATCH_START(rpatch, "Routing");
480         GET_INPUT_GPR(rpatch, 0x100, 0x0);
481         GET_INPUT_GPR(rpatch, 0x101, 0x1);
482
483         GET_DYNAMIC_GPR(rpatch, 0x102);
484         GET_DYNAMIC_GPR(rpatch, 0x103);
485
486         GET_OUTPUT_GPR(rpatch, 0x104, 0x8);
487         GET_OUTPUT_GPR(rpatch, 0x105, 0x9);
488         GET_OUTPUT_GPR(rpatch, 0x10a, 0x2);
489         GET_OUTPUT_GPR(rpatch, 0x10b, 0x3);
490
491         GET_CONTROL_GPR(rpatch, 0x106, "Vol Pcm L:Rear L", 0, 0x7fffffff);
492         GET_CONTROL_GPR(rpatch, 0x107, "Vol Pcm R:Rear R", 0, 0x7fffffff);
493
494         /* input buffer */
495         OP(6, 0x102, AC97_IN_L, 0x40, 0x40);
496         OP(6, 0x103, AC97_IN_R, 0x40, 0x40);
497
498         /* Digital In + PCM --> AC97 out (front speakers)*/
499         OP(6, AC97_FRONT_L, 0x100, SPDIF_CD_L, 0x40);
500
501         CONNECT(PCM_IN_L, AC97_FRONT_L);
502         CONNECT(SPDIF_CD_L, AC97_FRONT_L);
503
504         OP(6, AC97_FRONT_R, 0x101, SPDIF_CD_R, 0x40);
505
506         CONNECT(PCM_IN_R, AC97_FRONT_R);
507         CONNECT(SPDIF_CD_R, AC97_FRONT_R);
508
509         /* Digital In + PCM + AC97 In + PCM1 --> Rear Channel */ 
510         OP(0, 0x104, PCM1_IN_L, 0x100, 0x106);
511         OP(6, 0x104, 0x104, SPDIF_CD_L, 0x102);
512
513         CONNECT(AC97_IN_L, ANALOG_REAR_L);
514         CONNECT_V(PCM_IN_L, ANALOG_REAR_L);
515         CONNECT(SPDIF_CD_L, ANALOG_REAR_L);
516         CONNECT(PCM1_IN_L, ANALOG_REAR_L);
517
518         OP(0, 0x105, PCM1_IN_R, 0x101, 0x107);
519         OP(6, 0x105, 0x105, SPDIF_CD_R, 0x103);
520
521         CONNECT(AC97_IN_R, ANALOG_REAR_R);
522         CONNECT_V(PCM_IN_R, ANALOG_REAR_R);
523         CONNECT(SPDIF_CD_R, ANALOG_REAR_R);
524         CONNECT(PCM1_IN_R, ANALOG_REAR_R);
525
526         /* Digital In + PCM + AC97 In --> Digital out */
527         OP(6, 0x10b, 0x100, 0x102, SPDIF_CD_L);
528
529         CONNECT(PCM_IN_L, DIGITAL_OUT_L);
530         CONNECT(AC97_IN_L, DIGITAL_OUT_L);
531         CONNECT(SPDIF_CD_L, DIGITAL_OUT_L);
532
533         OP(6, 0x10a, 0x101, 0x103, SPDIF_CD_R);
534
535         CONNECT(PCM_IN_R, DIGITAL_OUT_R);
536         CONNECT(AC97_IN_R, DIGITAL_OUT_R);
537         CONNECT(SPDIF_CD_R, DIGITAL_OUT_R);
538
539         /* AC97 In --> ADC Recording Buffer */
540         OP(6, ADC_REC_L, 0x102, 0x40, 0x40);
541
542         CONNECT(AC97_IN_L, ADC_REC_L);
543
544         OP(6, ADC_REC_R, 0x103, 0x40, 0x40);
545
546         CONNECT(AC97_IN_R, ADC_REC_R);
547
548         ROUTING_PATCH_END(rpatch);
549
550
551         // Master volume control on rear
552         OUTPUT_PATCH_START(patch, "Vol Master L", 0x8, 0);
553         GET_INPUT_GPR(patch, 0x104, 0x8);
554         GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
555
556         OP(0, ANALOG_REAR_L, 0x040, 0x104, 0x108);
557         OUTPUT_PATCH_END(patch);
558
559
560         OUTPUT_PATCH_START(patch, "Vol Master R", 0x9, 0);
561         GET_INPUT_GPR(patch, 0x105, 0x9);
562         GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
563
564         OP(0, ANALOG_REAR_R, 0x040, 0x105, 0x109);
565         OUTPUT_PATCH_END(patch);
566
567
568         //Master volume control on front-digital
569         OUTPUT_PATCH_START(patch, "Vol Master L", 0x2, 1);
570         GET_INPUT_GPR(patch, 0x10a, 0x2);
571         GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
572
573         OP(0, DIGITAL_OUT_L, 0x040, 0x10a, 0x108);
574         OUTPUT_PATCH_END(patch);
575
576         
577         OUTPUT_PATCH_START(patch, "Vol Master R", 0x3, 1);
578         GET_INPUT_GPR(patch, 0x10b, 0x3);
579         GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
580
581         OP(0, DIGITAL_OUT_R, 0x040, 0x10b, 0x109);
582         OUTPUT_PATCH_END(patch);
583
584
585         /* delimiter patch */
586         patch = PATCH(mgr, patch_n);
587         patch->code_size = 0;
588
589         sblive_writeptr(card, DBG, 0, 0);
590
591         mgr->lock = SPIN_LOCK_UNLOCKED;
592
593         mgr->ctrl_gpr[SOUND_MIXER_VOLUME][0] = 8;
594         mgr->ctrl_gpr[SOUND_MIXER_VOLUME][1] = 9;
595
596         left = card->ac97.mixer_state[SOUND_MIXER_VOLUME] & 0xff;
597         right = (card->ac97.mixer_state[SOUND_MIXER_VOLUME] >> 8) & 0xff;
598
599         emu10k1_set_volume_gpr(card, 8, left,  VOL_6BIT);
600         emu10k1_set_volume_gpr(card, 9, right, VOL_6BIT);
601
602         mgr->ctrl_gpr[SOUND_MIXER_PCM][0] = 6;
603         mgr->ctrl_gpr[SOUND_MIXER_PCM][1] = 7;
604
605         left = card->ac97.mixer_state[SOUND_MIXER_PCM] & 0xff;
606         right = (card->ac97.mixer_state[SOUND_MIXER_PCM] >> 8) & 0xff;
607
608         emu10k1_set_volume_gpr(card, 6, left,  VOL_5BIT);
609         emu10k1_set_volume_gpr(card, 7, right, VOL_5BIT);
610
611         return 0;
612 }
613
614 static int __devinit hw_init(struct emu10k1_card *card)
615 {
616         int nCh;
617         u32 pagecount; /* tmp */
618         int ret;
619
620         /* Disable audio and lock cache */
621         emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
622
623         /* Reset recording buffers */
624         sblive_writeptr_tag(card, 0,
625                             MICBS, ADCBS_BUFSIZE_NONE,
626                             MICBA, 0,
627                             FXBS, ADCBS_BUFSIZE_NONE,
628                             FXBA, 0,
629                             ADCBS, ADCBS_BUFSIZE_NONE,
630                             ADCBA, 0,
631                             TAGLIST_END);
632
633         /* Disable channel interrupt */
634         emu10k1_writefn0(card, INTE, 0);
635         sblive_writeptr_tag(card, 0,
636                             CLIEL, 0,
637                             CLIEH, 0,
638                             SOLEL, 0,
639                             SOLEH, 0,
640                             TAGLIST_END);
641
642         /* Init envelope engine */
643         for (nCh = 0; nCh < NUM_G; nCh++) {
644                 sblive_writeptr_tag(card, nCh,
645                                     DCYSUSV, 0,
646                                     IP, 0,
647                                     VTFT, 0xffff,
648                                     CVCF, 0xffff,
649                                     PTRX, 0,
650                                     CPF, 0,
651                                     CCR, 0,
652
653                                     PSST, 0,
654                                     DSL, 0x10,
655                                     CCCA, 0,
656                                     Z1, 0,
657                                     Z2, 0,
658                                     FXRT, 0xd01c0000,
659
660                                     ATKHLDM, 0,
661                                     DCYSUSM, 0,
662                                     IFATN, 0xffff,
663                                     PEFE, 0,
664                                     FMMOD, 0,
665                                     TREMFRQ, 24,        /* 1 Hz */
666                                     FM2FRQ2, 24,        /* 1 Hz */
667                                     TEMPENV, 0,
668
669                                     /*** These are last so OFF prevents writing ***/
670                                     LFOVAL2, 0,
671                                     LFOVAL1, 0,
672                                     ATKHLDV, 0,
673                                     ENVVOL, 0,
674                                     ENVVAL, 0,
675                                     TAGLIST_END);
676         }
677
678         /*
679          ** Init to 0x02109204 :
680          ** Clock accuracy    = 0     (1000ppm)
681          ** Sample Rate       = 2     (48kHz)
682          ** Audio Channel     = 1     (Left of 2)
683          ** Source Number     = 0     (Unspecified)
684          ** Generation Status = 1     (Original for Cat Code 12)
685          ** Cat Code          = 12    (Digital Signal Mixer)
686          ** Mode              = 0     (Mode 0)
687          ** Emphasis          = 0     (None)
688          ** CP                = 1     (Copyright unasserted)
689          ** AN                = 0     (Digital audio)
690          ** P                 = 0     (Consumer)
691          */
692
693         sblive_writeptr_tag(card, 0,
694
695                             /* SPDIF0 */
696                             SPCS0, (SPCS_CLKACCY_1000PPM | 0x002000000 |
697                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
698
699                             /* SPDIF1 */
700                             SPCS1, (SPCS_CLKACCY_1000PPM | 0x002000000 |
701                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
702
703                             /* SPDIF2 & SPDIF3 */
704                             SPCS2, (SPCS_CLKACCY_1000PPM | 0x002000000 |
705                                     SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
706
707                             TAGLIST_END);
708
709         ret = fx_init(card);            /* initialize effects engine */
710         if (ret < 0)
711                 return ret;
712
713         card->tankmem.size = 0;
714
715         card->virtualpagetable.size = MAXPAGES * sizeof(u32);
716
717         card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle);
718         if (card->virtualpagetable.addr == NULL) {
719                 ERROR();
720                 ret = -ENOMEM;
721                 goto err0;
722         }
723
724         card->silentpage.size = EMUPAGESIZE;
725
726         card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle);
727         if (card->silentpage.addr == NULL) {
728                 ERROR();
729                 ret = -ENOMEM;
730                 goto err1;
731         }
732
733         for (pagecount = 0; pagecount < MAXPAGES; pagecount++)
734                 ((u32 *) card->virtualpagetable.addr)[pagecount] = cpu_to_le32((card->silentpage.dma_handle * 2) | pagecount);
735
736         /* Init page table & tank memory base register */
737         sblive_writeptr_tag(card, 0,
738                             PTB, card->virtualpagetable.dma_handle,
739                             TCB, 0,
740                             TCBS, 0,
741                             TAGLIST_END);
742
743         for (nCh = 0; nCh < NUM_G; nCh++) {
744                 sblive_writeptr_tag(card, nCh,
745                                     MAPA, MAP_PTI_MASK | (card->silentpage.dma_handle * 2),
746                                     MAPB, MAP_PTI_MASK | (card->silentpage.dma_handle * 2),
747                                     TAGLIST_END);
748         }
749
750         /* Hokay, now enable the AUD bit */
751         /* Enable Audio = 1 */
752         /* Mute Disable Audio = 0 */
753         /* Lock Tank Memory = 1 */
754         /* Lock Sound Memory = 0 */
755         /* Auto Mute = 1 */
756
757         if (card->model == 0x20 || card->model == 0xc400 ||
758           (card->model == 0x21 && card->chiprev < 6))
759                 emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE);
760         else
761                 emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE);
762
763         /* Enable Vol_Ctrl irqs */
764         emu10k1_irq_enable(card, INTE_VOLINCRENABLE | INTE_VOLDECRENABLE | INTE_MUTEENABLE | INTE_FXDSPENABLE);
765
766         /* FIXME: TOSLink detection */
767         card->has_toslink = 0;
768
769         /* Initialize digital passthrough variables */
770         card->pt.pos_gpr = card->pt.intr_gpr = card->pt.enable_gpr = -1;
771         card->pt.selected = 0;
772         card->pt.state = PT_STATE_INACTIVE;
773         card->pt.spcs_to_use = 0x01;
774         card->pt.patch_name = "AC3pass";
775         card->pt.intr_gpr_name = "count";
776         card->pt.enable_gpr_name = "enable";
777         card->pt.pos_gpr_name = "ptr";
778         init_waitqueue_head(&card->pt.wait);
779
780 /*      tmp = sblive_readfn0(card, HCFG);
781         if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
782                 sblive_writefn0(card, HCFG, tmp | 0x800);
783
784                 udelay(512);
785
786                 if (tmp != (sblive_readfn0(card, HCFG) & ~0x800)) {
787                         card->has_toslink = 1;
788                         sblive_writefn0(card, HCFG, tmp);
789                 }
790         }
791 */
792         return 0;
793
794   err1:
795         pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
796   err0:
797         fx_cleanup(&card->mgr);
798
799         return ret;
800 }
801
802 static int __devinit emu10k1_init(struct emu10k1_card *card)
803 {
804         /* Init Card */
805         if (hw_init(card) < 0)
806                 return -1;
807
808         voice_init(card);
809         timer_init(card);
810         addxmgr_init(card);
811
812         DPD(2, "  hw control register -> %#x\n", emu10k1_readfn0(card, HCFG));
813
814         return 0;
815 }
816
817 static void __devinit emu10k1_cleanup(struct emu10k1_card *card)
818 {
819         int ch;
820
821         emu10k1_writefn0(card, INTE, 0);
822
823         /** Shutdown the chip **/
824         for (ch = 0; ch < NUM_G; ch++)
825                 sblive_writeptr(card, DCYSUSV, ch, 0);
826
827         for (ch = 0; ch < NUM_G; ch++) {
828                 sblive_writeptr_tag(card, ch,
829                                     VTFT, 0,
830                                     CVCF, 0,
831                                     PTRX, 0,
832                                     CPF, 0,
833                                     TAGLIST_END);
834         }
835
836         /* Disable audio and lock cache */
837         emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
838
839         sblive_writeptr_tag(card, 0,
840                             PTB, 0,
841
842                             /* Reset recording buffers */
843                             MICBS, ADCBS_BUFSIZE_NONE,
844                             MICBA, 0,
845                             FXBS, ADCBS_BUFSIZE_NONE,
846                             FXBA, 0,
847                             FXWC, 0,
848                             ADCBS, ADCBS_BUFSIZE_NONE,
849                             ADCBA, 0,
850                             TCBS, 0,
851                             TCB, 0,
852                             DBG, 0x8000,
853
854                             /* Disable channel interrupt */
855                             CLIEL, 0,
856                             CLIEH, 0,
857                             SOLEL, 0,
858                             SOLEH, 0,
859                             TAGLIST_END);
860
861
862         pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
863         pci_free_consistent(card->pci_dev, card->silentpage.size, card->silentpage.addr, card->silentpage.dma_handle);
864         
865         if(card->tankmem.size != 0)
866                 pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);
867
868         /* release patch storage memory */
869         fx_cleanup(&card->mgr);
870 }
871
872 /* Driver initialization routine */
873 static int __devinit emu10k1_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
874 {
875         struct emu10k1_card *card;
876         u32 subsysvid;
877         int ret;
878
879         if (pci_set_dma_mask(pci_dev, EMU10K1_DMA_MASK)) {
880                 printk(KERN_ERR "emu10k1: architecture does not support 32bit PCI busmaster DMA\n");
881                 return -ENODEV;
882         }
883
884         if (pci_enable_device(pci_dev))
885                 return -EIO;
886
887         pci_set_master(pci_dev);
888
889         if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
890                 printk(KERN_ERR "emu10k1: out of memory\n");
891                 return -ENOMEM;
892         }
893         memset(card, 0, sizeof(struct emu10k1_card));
894
895         card->iobase = pci_resource_start(pci_dev, 0);
896         card->length = pci_resource_len(pci_dev, 0); 
897
898         if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
899                 printk(KERN_ERR "emu10k1: IO space in use\n");
900                 ret = -EBUSY;
901                 goto err_region;
902         }
903
904         pci_set_drvdata(pci_dev, card);
905
906         card->irq = pci_dev->irq;
907         card->pci_dev = pci_dev;
908
909         /* Reserve IRQ Line */
910         if (request_irq(card->irq, emu10k1_interrupt, SA_SHIRQ, card_names[pci_id->driver_data], card)) {
911                 printk(KERN_ERR "emu10k1: IRQ in use\n");
912                 ret = -EBUSY;
913                 goto err_irq;
914         }
915
916         pci_read_config_byte(pci_dev, PCI_REVISION_ID, &card->chiprev);
917         pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &card->model);
918
919         printk(KERN_INFO "emu10k1: %s rev %d model 0x%x found, IO at 0x%04lx-0x%04lx, IRQ %d\n",
920                 card_names[pci_id->driver_data], card->chiprev, card->model, card->iobase,
921                 card->iobase + card->length - 1, card->irq);
922
923         pci_read_config_dword(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &subsysvid);
924         card->isaps = (subsysvid == EMU_APS_SUBID);
925
926         spin_lock_init(&card->lock);
927         init_MUTEX(&card->open_sem);
928         card->open_mode = 0;
929         init_waitqueue_head(&card->open_wait);
930
931         ret = emu10k1_audio_init(card);
932         if(ret < 0) {
933                 printk(KERN_ERR "emu10k1: cannot initialize audio devices\n");
934                 goto err_audio;
935         }
936
937         ret = emu10k1_mixer_init(card);
938         if(ret < 0) {
939                 printk(KERN_ERR "emu10k1: cannot initialize AC97 codec\n");
940                 goto err_mixer;
941         }
942
943         ret = emu10k1_midi_init(card);
944         if (ret < 0) {
945                 printk(KERN_ERR "emu10k1: cannot register midi device\n");
946                 goto err_midi;
947         }
948
949         ret = emu10k1_init(card);
950         if (ret < 0) {
951                 printk(KERN_ERR "emu10k1: cannot initialize device\n");
952                 goto err_emu10k1_init;
953         }
954
955         if (card->isaps)
956                 emu10k1_ecard_init(card);
957
958         list_add(&card->list, &emu10k1_devs);
959
960         return 0;
961
962 err_emu10k1_init:
963         emu10k1_midi_cleanup(card);
964
965 err_midi:
966         emu10k1_mixer_cleanup(card);
967
968 err_mixer:
969         emu10k1_audio_cleanup(card);
970
971 err_audio:
972         free_irq(card->irq, card);
973
974 err_irq:
975         release_region(card->iobase, card->length);
976         pci_set_drvdata(pci_dev, NULL);
977
978 err_region:
979         kfree(card);
980
981         return ret;
982 }
983
984 static void __devexit emu10k1_remove(struct pci_dev *pci_dev)
985 {
986         struct emu10k1_card *card = pci_get_drvdata(pci_dev);
987
988         list_del(&card->list);
989
990         emu10k1_cleanup(card);
991         emu10k1_midi_cleanup(card);
992         emu10k1_mixer_cleanup(card);
993         emu10k1_audio_cleanup(card);    
994         free_irq(card->irq, card);
995         release_region(card->iobase, card->length);
996         kfree(card);
997         pci_set_drvdata(pci_dev, NULL);
998 }
999
1000 MODULE_AUTHOR("Bertrand Lee, Cai Ying. (Email to: emu10k1-devel@opensource.creative.com)");
1001 MODULE_DESCRIPTION("Creative EMU10K1 PCI Audio Driver v" DRIVER_VERSION "\nCopyright (C) 1999 Creative Technology Ltd.");
1002
1003 static struct pci_driver emu10k1_pci_driver = {
1004         name:           "emu10k1",
1005         id_table:       emu10k1_pci_tbl,
1006         probe:          emu10k1_probe,
1007         remove:         emu10k1_remove,
1008 };
1009
1010 static int __init emu10k1_init_module(void)
1011 {
1012         printk(KERN_INFO "Creative EMU10K1 PCI Audio Driver, version " DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
1013
1014         return pci_module_init(&emu10k1_pci_driver);
1015 }
1016
1017 static void __exit emu10k1_cleanup_module(void)
1018 {
1019         pci_unregister_driver(&emu10k1_pci_driver);
1020
1021         return;
1022 }
1023
1024 module_init(emu10k1_init_module);
1025 module_exit(emu10k1_cleanup_module);
1026
1027 #ifdef EMU10K1_SEQUENCER
1028
1029 /* in midi.c */
1030 extern int emu10k1_seq_midi_open(int dev, int mode, 
1031                                 void (*input)(int dev, unsigned char midi_byte),
1032                                 void (*output)(int dev));
1033 extern void emu10k1_seq_midi_close(int dev);
1034 extern int emu10k1_seq_midi_out(int dev, unsigned char midi_byte);
1035 extern int emu10k1_seq_midi_start_read(int dev);
1036 extern int emu10k1_seq_midi_end_read(int dev);
1037 extern void emu10k1_seq_midi_kick(int dev);
1038 extern int emu10k1_seq_midi_buffer_status(int dev);
1039
1040 static struct midi_operations emu10k1_midi_operations =
1041 {
1042     THIS_MODULE,
1043     {"EMU10K1 MIDI", 0, 0, SNDCARD_EMU10K1},
1044         &std_midi_synth,
1045         {0},
1046         emu10k1_seq_midi_open,
1047         emu10k1_seq_midi_close,
1048         NULL,
1049         emu10k1_seq_midi_out,
1050         emu10k1_seq_midi_start_read,
1051         emu10k1_seq_midi_end_read,
1052         emu10k1_seq_midi_kick,
1053         NULL,
1054         emu10k1_seq_midi_buffer_status,
1055         NULL
1056 };
1057
1058 #endif
1059