make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / sound / sb_common.c
1 /*
2  * sound/sb_common.c
3  *
4  * Common routines for Sound Blaster compatible cards.
5  *
6  *
7  * Copyright (C) by Hannu Savolainen 1993-1997
8  *
9  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10  * Version 2 (June 1991). See the "COPYING" file distributed with this software
11  * for more info.
12  *
13  *
14  * Daniel J. Rodriksson: Modified sbintr to handle 8 and 16 bit interrupts
15  *                       for full duplex support ( only sb16 by now )
16  * Rolf Fokkens:         Added (BETA?) support for ES1887 chips.
17  * (fokkensr@vertis.nl)  Which means: You can adjust the recording levels.
18  *
19  * 2000/01/18 - separated sb_card and sb_common -
20  * Jeff Garzik <jgarzik@pobox.com>
21  *
22  * 2000/09/18 - got rid of attach_uart401
23  * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
24  *
25  * 2001/01/26 - replaced CLI/STI with spinlocks
26  * Chris Rankin <rankinc@zipworld.com.au>
27  */
28
29 #include <linux/config.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/spinlock.h>
34
35 #include "sound_config.h"
36 #include "sound_firmware.h"
37
38 #include "mpu401.h"
39
40 #include "sb_mixer.h"
41 #include "sb.h"
42 #include "sb_ess.h"
43
44 /*
45  * global module flag
46  */
47
48 int sb_be_quiet;
49
50 static sb_devc *detected_devc;  /* For communication from probe to init */
51 static sb_devc *last_devc;      /* For MPU401 initialization */
52
53 static unsigned char jazz_irq_bits[] = {
54         0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
55 };
56
57 static unsigned char jazz_dma_bits[] = {
58         0, 1, 0, 2, 0, 3, 0, 4
59 };
60
61 void *smw_free;
62
63 /*
64  * Jazz16 chipset specific control variables
65  */
66
67 static int jazz16_base;                 /* Not detected */
68 static unsigned char jazz16_bits;       /* I/O relocation bits */
69 static spinlock_t jazz16_lock = SPIN_LOCK_UNLOCKED;
70
71 /*
72  * Logitech Soundman Wave specific initialization code
73  */
74
75 #ifdef SMW_MIDI0001_INCLUDED
76 #include "smw-midi0001.h"
77 #else
78 static unsigned char *smw_ucode;
79 static int      smw_ucodeLen;
80
81 #endif
82
83 sb_devc *last_sb;               /* Last sb loaded */
84
85 int sb_dsp_command(sb_devc * devc, unsigned char val)
86 {
87         int i;
88         unsigned long limit;
89
90         limit = jiffies + HZ / 10;      /* Timeout */
91         
92         /*
93          * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
94          * called while interrupts are disabled. This means that the timer is
95          * disabled also. However the timeout situation is a abnormal condition.
96          * Normally the DSP should be ready to accept commands after just couple of
97          * loops.
98          */
99
100         for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
101         {
102                 if ((inb(DSP_STATUS) & 0x80) == 0)
103                 {
104                         outb((val), DSP_COMMAND);
105                         return 1;
106                 }
107         }
108         printk(KERN_WARNING "Sound Blaster:  DSP command(%x) timeout.\n", val);
109         return 0;
110 }
111
112 int sb_dsp_get_byte(sb_devc * devc)
113 {
114         int i;
115
116         for (i = 1000; i; i--)
117         {
118                 if (inb(DSP_DATA_AVAIL) & 0x80)
119                         return inb(DSP_READ);
120         }
121         return 0xffff;
122 }
123
124 static void sb_intr (sb_devc *devc)
125 {
126         int status;
127         unsigned char   src = 0xff;
128
129         if (devc->model == MDL_SB16)
130         {
131                 src = sb_getmixer(devc, IRQ_STAT);      /* Interrupt source register */
132
133                 if (src & 4)                                            /* MPU401 interrupt */
134                         if(devc->midi_irq_cookie)
135                                 uart401intr(devc->irq, devc->midi_irq_cookie, NULL);
136
137                 if (!(src & 3))
138                         return; /* Not a DSP interrupt */
139         }
140         if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
141         {
142                 switch (devc->irq_mode)
143                 {
144                         case IMODE_OUTPUT:
145                                 DMAbuf_outputintr(devc->dev, 1);
146                                 break;
147
148                         case IMODE_INPUT:
149                                 DMAbuf_inputintr(devc->dev);
150                                 break;
151
152                         case IMODE_INIT:
153                                 break;
154
155                         case IMODE_MIDI:
156                                 sb_midi_interrupt(devc);
157                                 break;
158
159                         default:
160                                 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
161                                 ;
162                 }
163         }
164         else if (devc->intr_active_16 && (src & 0x02))
165         {
166                 switch (devc->irq_mode_16)
167                 {
168                         case IMODE_OUTPUT:
169                                 DMAbuf_outputintr(devc->dev, 1);
170                                 break;
171
172                         case IMODE_INPUT:
173                                 DMAbuf_inputintr(devc->dev);
174                                 break;
175
176                         case IMODE_INIT:
177                                 break;
178
179                         default:
180                                 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
181                                 ;
182                 }
183         }
184         /*
185          * Acknowledge interrupts 
186          */
187
188         if (src & 0x01)
189                 status = inb(DSP_DATA_AVAIL);
190
191         if (devc->model == MDL_SB16 && src & 0x02)
192                 status = inb(DSP_DATA_AVL16);
193 }
194
195 static void pci_intr(sb_devc *devc)
196 {
197         int src = inb(devc->pcibase+0x1A);
198         src&=3;
199         if(src)
200                 sb_intr(devc);
201 }
202
203 static void sbintr(int irq, void *dev_id, struct pt_regs *dummy)
204 {
205         sb_devc *devc = dev_id;
206
207         devc->irq_ok = 1;
208
209         switch (devc->model) {
210         case MDL_ESSPCI:
211                 pci_intr (devc);
212                 break;
213                 
214         case MDL_ESS:
215                 ess_intr (devc);
216                 break;
217         default:
218                 sb_intr (devc);
219                 break;
220         }
221 }
222
223 int sb_dsp_reset(sb_devc * devc)
224 {
225         int loopc;
226
227         DEB(printk("Entered sb_dsp_reset()\n"));
228
229         if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
230
231         /* This is only for non-ESS chips */
232
233         outb(1, DSP_RESET);
234
235         udelay(10);
236         outb(0, DSP_RESET);
237         udelay(30);
238
239         for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
240
241         if (inb(DSP_READ) != 0xAA)
242         {
243                 DDB(printk("sb: No response to RESET\n"));
244                 return 0;       /* Sorry */
245         }
246
247         DEB(printk("sb_dsp_reset() OK\n"));
248
249         return 1;
250 }
251
252 static void dsp_get_vers(sb_devc * devc)
253 {
254         int i;
255
256         unsigned long   flags;
257
258         DDB(printk("Entered dsp_get_vers()\n"));
259         spin_lock_irqsave(&devc->lock, flags);
260         devc->major = devc->minor = 0;
261         sb_dsp_command(devc, 0xe1);     /* Get version */
262
263         for (i = 100000; i; i--)
264         {
265                 if (inb(DSP_DATA_AVAIL) & 0x80)
266                 {
267                         if (devc->major == 0)
268                                 devc->major = inb(DSP_READ);
269                         else
270                         {
271                                 devc->minor = inb(DSP_READ);
272                                 break;
273                         }
274                 }
275         }
276         spin_unlock_irqrestore(&devc->lock, flags);
277         DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
278 }
279
280 static int sb16_set_dma_hw(sb_devc * devc)
281 {
282         int bits;
283
284         if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
285         {
286                 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
287                 return 0;
288         }
289         bits = (1 << devc->dma8);
290
291         if (devc->dma16 >= 5 && devc->dma16 <= 7)
292                 bits |= (1 << devc->dma16);
293
294         sb_setmixer(devc, DMA_NR, bits);
295         return 1;
296 }
297
298 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
299 {
300         /*
301          * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
302          */
303         unsigned char   bits = sb_getmixer(devc, 0x84) & ~0x06;
304
305         switch (hw_config->io_base)
306         {
307                 case 0x300:
308                         sb_setmixer(devc, 0x84, bits | 0x04);
309                         break;
310
311                 case 0x330:
312                         sb_setmixer(devc, 0x84, bits | 0x00);
313                         break;
314
315                 default:
316                         sb_setmixer(devc, 0x84, bits | 0x02);           /* Disable MPU */
317                         printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
318         }
319 }
320
321 static int sb16_set_irq_hw(sb_devc * devc, int level)
322 {
323         int ival;
324
325         switch (level)
326         {
327                 case 5:
328                         ival = 2;
329                         break;
330                 case 7:
331                         ival = 4;
332                         break;
333                 case 9:
334                         ival = 1;
335                         break;
336                 case 10:
337                         ival = 8;
338                         break;
339                 default:
340                         printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
341                         return 0;
342         }
343         sb_setmixer(devc, IRQ_NR, ival);
344         return 1;
345 }
346
347 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
348 {
349         unsigned char bits = 0;
350         unsigned long flags;
351
352         if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
353                 return;
354
355         switch (hw_config->io_base)
356         {
357                 case 0x220:
358                         bits = 1;
359                         break;
360                 case 0x240:
361                         bits = 2;
362                         break;
363                 case 0x260:
364                         bits = 3;
365                         break;
366                 default:
367                         return;
368         }
369         bits = jazz16_bits = bits << 5;
370         jazz16_base = hw_config->io_base;
371
372         /*
373          *      Magic wake up sequence by writing to 0x201 (aka Joystick port)
374          */
375         spin_lock_irqsave(&jazz16_lock, flags);
376         outb((0xAF), 0x201);
377         outb((0x50), 0x201);
378         outb((bits), 0x201);
379         spin_unlock_irqrestore(&jazz16_lock, flags);
380 }
381
382 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
383 {
384         char name[100];
385         /*
386          * First try to check that the card has Jazz16 chip. It identifies itself
387          * by returning 0x12 as response to DSP command 0xfa.
388          */
389
390         if (!sb_dsp_command(devc, 0xfa))
391                 return 0;
392
393         if (sb_dsp_get_byte(devc) != 0x12)
394                 return 0;
395
396         /*
397          * OK so far. Now configure the IRQ and DMA channel used by the card.
398          */
399         if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
400         {
401                 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
402                 return 0;
403         }
404         if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
405         {
406                   printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
407                   return 0;
408         }
409         if (hw_config->dma2 < 0)
410         {
411                 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
412                 return 0;
413         }
414         if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
415         {
416                 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
417                 return 0;
418         }
419         devc->dma16 = hw_config->dma2;
420
421         if (!sb_dsp_command(devc, 0xfb))
422                 return 0;
423
424         if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
425                         (jazz_dma_bits[hw_config->dma2] << 4)))
426                 return 0;
427
428         if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
429                 return 0;
430
431         /*
432          * Now we have configured a standard Jazz16 device. 
433          */
434         devc->model = MDL_JAZZ;
435         strcpy(name, "Jazz16");
436
437         hw_config->name = "Jazz16";
438         devc->caps |= SB_NO_MIDI;
439         return 1;
440 }
441
442 static void relocate_ess1688(sb_devc * devc)
443 {
444         unsigned char bits;
445
446         switch (devc->base)
447         {
448                 case 0x220:
449                         bits = 0x04;
450                         break;
451                 case 0x230:
452                         bits = 0x05;
453                         break;
454                 case 0x240:
455                         bits = 0x06;
456                         break;
457                 case 0x250:
458                         bits = 0x07;
459                         break;
460                 default:
461                         return; /* Wrong port */
462         }
463
464         DDB(printk("Doing ESS1688 address selection\n"));
465         
466         /*
467          * ES1688 supports two alternative ways for software address config.
468          * First try the so called Read-Sequence-Key method.
469          */
470
471         /* Reset the sequence logic */
472         inb(0x229);
473         inb(0x229);
474         inb(0x229);
475
476         /* Perform the read sequence */
477         inb(0x22b);
478         inb(0x229);
479         inb(0x22b);
480         inb(0x229);
481         inb(0x229);
482         inb(0x22b);
483         inb(0x229);
484
485         /* Select the base address by reading from it. Then probe using the port. */
486         inb(devc->base);
487         if (sb_dsp_reset(devc)) /* Bingo */
488                 return;
489
490 #if 0                           /* This causes system lockups (Nokia 386/25 at least) */
491         /*
492          * The last resort is the system control register method.
493          */
494
495         outb((0x00), 0xfb);     /* 0xFB is the unlock register */
496         outb((0x00), 0xe0);     /* Select index 0 */
497         outb((bits), 0xe1);     /* Write the config bits */
498         outb((0x00), 0xf9);     /* 0xFB is the lock register */
499 #endif
500 }
501
502 int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
503 {
504         sb_devc sb_info;
505         sb_devc *devc = &sb_info;
506
507         memset((char *) &sb_info, 0, sizeof(sb_info));  /* Zero everything */
508
509         /* Copy module options in place */
510         if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
511
512         sb_info.my_mididev = -1;
513         sb_info.my_mixerdev = -1;
514         sb_info.dev = -1;
515
516         /*
517          * Initialize variables 
518          */
519         
520         DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
521         if (check_region(hw_config->io_base, 16))
522         {
523 #ifdef MODULE
524                 printk(KERN_INFO "sb: I/O region in use.\n");
525 #endif
526                 return 0;
527         }
528
529         devc->lock = SPIN_LOCK_UNLOCKED;
530         devc->type = hw_config->card_subtype;
531
532         devc->base = hw_config->io_base;
533         devc->irq = hw_config->irq;
534         devc->dma8 = hw_config->dma;
535
536         devc->dma16 = -1;
537         devc->pcibase = pciio;
538         
539         if(pci == SB_PCI_ESSMAESTRO)
540         {
541                 devc->model = MDL_ESSPCI;
542                 devc->caps |= SB_PCI_IRQ;
543                 hw_config->driver_use_1 |= SB_PCI_IRQ;
544                 hw_config->card_subtype = MDL_ESSPCI;
545         }
546         
547         if(pci == SB_PCI_YAMAHA)
548         {
549                 devc->model = MDL_YMPCI;
550                 devc->caps |= SB_PCI_IRQ;
551                 hw_config->driver_use_1 |= SB_PCI_IRQ;
552                 hw_config->card_subtype = MDL_YMPCI;
553                 
554                 printk("Yamaha PCI mode.\n");
555         }
556         
557         if (devc->sbmo.acer)
558         {
559                 unsigned long flags;
560
561                 spin_lock_irqsave(&devc->lock, flags);
562                 inb(devc->base + 0x09);
563                 inb(devc->base + 0x09);
564                 inb(devc->base + 0x09);
565                 inb(devc->base + 0x0b);
566                 inb(devc->base + 0x09);
567                 inb(devc->base + 0x0b);
568                 inb(devc->base + 0x09);
569                 inb(devc->base + 0x09);
570                 inb(devc->base + 0x0b);
571                 inb(devc->base + 0x09);
572                 inb(devc->base + 0x00);
573                 spin_unlock_irqrestore(&devc->lock, flags);
574         }
575         /*
576          * Detect the device
577          */
578
579         if (sb_dsp_reset(devc))
580                 dsp_get_vers(devc);
581         else
582                 devc->major = 0;
583
584         if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
585                 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
586                         relocate_Jazz16(devc, hw_config);
587
588         if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
589                 relocate_ess1688(devc);
590
591         if (!sb_dsp_reset(devc))
592         {
593                 DDB(printk("SB reset failed\n"));
594 #ifdef MODULE
595                 printk(KERN_INFO "sb: dsp reset failed.\n");
596 #endif
597                 return 0;
598         }
599         if (devc->major == 0)
600                 dsp_get_vers(devc);
601
602         if (devc->major == 3 && devc->minor == 1)
603         {
604                 if (devc->type == MDL_AZTECH)           /* SG Washington? */
605                 {
606                         if (sb_dsp_command(devc, 0x09))
607                                 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
608                                 {
609                                         int i;
610
611                                         /* Have some delay */
612                                         for (i = 0; i < 10000; i++)
613                                                 inb(DSP_DATA_AVAIL);
614                                         devc->caps = SB_NO_AUDIO | SB_NO_MIDI;  /* Mixer only */
615                                         devc->model = MDL_AZTECH;
616                                 }
617                 }
618         }
619         
620         if(devc->type == MDL_ESSPCI)
621                 devc->model = MDL_ESSPCI;
622                 
623         if(devc->type == MDL_YMPCI)
624         {
625                 printk("YMPCI selected\n");
626                 devc->model = MDL_YMPCI;
627         }
628                 
629         /*
630          * Save device information for sb_dsp_init()
631          */
632
633
634         detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
635         if (detected_devc == NULL)
636         {
637                 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
638                 return 0;
639         }
640         memcpy(detected_devc, devc, sizeof(sb_devc));
641         MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
642         return 1;
643 }
644
645 int sb_dsp_init(struct address_info *hw_config, struct module *owner)
646 {
647         sb_devc *devc;
648         char name[100];
649         extern int sb_be_quiet;
650         int     mixer22, mixer30;
651         
652 /*
653  * Check if we had detected a SB device earlier
654  */
655         DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
656         name[0] = 0;
657
658         if (detected_devc == NULL)
659         {
660                 MDB(printk("No detected device\n"));
661                 return 0;
662         }
663         devc = detected_devc;
664         detected_devc = NULL;
665
666         if (devc->base != hw_config->io_base)
667         {
668                 DDB(printk("I/O port mismatch\n"));
669                 return 0;
670         }
671         /*
672          * Now continue initialization of the device
673          */
674
675         devc->caps = hw_config->driver_use_1;
676
677         if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
678         {                       /* IRQ setup */
679                 
680                 /*
681                  *      ESS PCI cards do shared PCI IRQ stuff. Since they
682                  *      will get shared PCI irq lines we must cope.
683                  */
684                  
685                 int i=(devc->caps&SB_PCI_IRQ)?SA_SHIRQ:0;
686                 
687                 if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
688                 {
689                         printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
690                         return 0;
691                 }
692                 devc->irq_ok = 0;
693
694                 if (devc->major == 4)
695                         if (!sb16_set_irq_hw(devc, devc->irq))  /* Unsupported IRQ */
696                         {
697                                 free_irq(devc->irq, devc);
698                                 return 0;
699                         }
700                 if ((devc->type == 0 || devc->type == MDL_ESS) &&
701                         devc->major == 3 && devc->minor == 1)
702                 {               /* Handle various chipsets which claim they are SB Pro compatible */
703                         if ((devc->type != 0 && devc->type != MDL_ESS) ||
704                                 !ess_init(devc, hw_config))
705                         {
706                                 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
707                                          devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
708                                 {
709                                         DDB(printk("This is a genuine SB Pro\n"));
710                                 }
711                         }
712                 }
713                 if (devc->major == 4 && devc->minor <= 11 )     /* Won't work */
714                         devc->irq_ok = 1;
715                 else
716                 {
717                         int n;
718
719                         for (n = 0; n < 3 && devc->irq_ok == 0; n++)
720                         {
721                                 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
722                                 {
723                                         int i;
724
725                                         for (i = 0; !devc->irq_ok && i < 10000; i++);
726                                 }
727                         }
728                         if (!devc->irq_ok)
729                                 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
730                         else
731                         {
732                                 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
733                         }
734                 }
735         }                       /* IRQ setup */
736         request_region(hw_config->io_base, 16, "soundblaster");
737
738         last_sb = devc;
739         
740         switch (devc->major)
741         {
742                 case 1:         /* SB 1.0 or 1.5 */
743                         devc->model = hw_config->card_subtype = MDL_SB1;
744                         break;
745
746                 case 2:         /* SB 2.x */
747                         if (devc->minor == 0)
748                                 devc->model = hw_config->card_subtype = MDL_SB2;
749                         else
750                                 devc->model = hw_config->card_subtype = MDL_SB201;
751                         break;
752
753                 case 3:         /* SB Pro and most clones */
754                         switch (devc->model) {
755                         case 0:
756                                 devc->model = hw_config->card_subtype = MDL_SBPRO;
757                                 if (hw_config->name == NULL)
758                                         hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
759                                 break;
760                         case MDL_ESS:
761                                 ess_dsp_init(devc, hw_config);
762                                 break;
763                         }
764                         break;
765
766                 case 4:
767                         devc->model = hw_config->card_subtype = MDL_SB16;
768                         /* 
769                          * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
770                          * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
771                          * a "standard" SB16 doesn't have a register at 0x4c.  ALS100 actively
772                          * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
773                          * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
774                          */
775                         if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c)) 
776                         {
777                                 mixer30 = sb_getmixer(devc,0x30);
778                                 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
779                                 sb_setmixer(devc,0x30,0xff);
780                                 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
781                                 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10.     */
782                                 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10)) 
783                                 {
784                                         devc->submodel = SUBMDL_ALS100;
785                                         if (hw_config->name == NULL)
786                                                 hw_config->name = "Sound Blaster 16 (ALS-100)";
787                                 }
788                                 else
789                                 {
790                                         sb_setmixer(devc,0x3c,0x1f);    /* Enable all inputs */
791                                         sb_setmixer(devc,0x4c,0x1f);
792                                         sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
793                                         devc->submodel = SUBMDL_ALS007;
794                                         if (hw_config->name == NULL)
795                                                 hw_config->name = "Sound Blaster 16 (ALS-007)";
796                                 }
797                                 sb_setmixer(devc,0x30,mixer30);
798                         }
799                         else if (hw_config->name == NULL)
800                                 hw_config->name = "Sound Blaster 16";
801
802                         if (hw_config->dma2 == -1)
803                                 devc->dma16 = devc->dma8;
804                         else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
805                         {
806                                 printk(KERN_WARNING  "SB16: Bad or missing 16 bit DMA channel\n");
807                                 devc->dma16 = devc->dma8;
808                         }
809                         else
810                                 devc->dma16 = hw_config->dma2;
811
812                         if(!sb16_set_dma_hw(devc)) {
813                                 free_irq(devc->irq, devc);
814                                 release_region(hw_config->io_base, 16);
815                                 return 0;
816                         }
817
818                         devc->caps |= SB_NO_MIDI;
819         }
820
821         if (!(devc->caps & SB_NO_MIXER))
822                 if (devc->major == 3 || devc->major == 4)
823                         sb_mixer_init(devc, owner);
824
825         if (!(devc->caps & SB_NO_MIDI))
826                 sb_dsp_midi_init(devc, owner);
827
828         if (hw_config->name == NULL)
829                 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
830
831         sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
832         conf_printf(name, hw_config);
833
834         /*
835          * Assuming that a sound card is Sound Blaster (compatible) is the most common
836          * configuration error and the mother of all problems. Usually sound cards
837          * emulate SB Pro but in addition they have a 16 bit native mode which should be
838          * used in Unix. See Readme.cards for more information about configuring OSS/Free
839          * properly.
840          */
841         if (devc->model <= MDL_SBPRO)
842         {
843                 if (devc->major == 3 && devc->minor != 1)       /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
844                 {
845                         printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
846                         printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
847                         printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
848                         printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
849                 }
850                 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
851                 {
852                         printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
853                         printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
854                         printk(KERN_INFO "is incorrectly configured.\n");
855                 }
856         }
857         hw_config->card_subtype = devc->model;
858         hw_config->slots[0]=devc->dev;
859         last_devc = devc;       /* For SB MPU detection */
860
861         if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
862         {
863                 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
864                 {
865                         printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
866                 }
867                 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
868                 {
869                         if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
870                                 printk(KERN_WARNING "Sound Blaster:  can't allocate 16 bit DMA channel %d.\n", devc->dma16);
871                 }
872                 sb_audio_init(devc, name, owner);
873                 hw_config->slots[0]=devc->dev;
874         }
875         else
876         {
877                 MDB(printk("Sound Blaster:  no audio devices found.\n"));
878         }
879         return 1;
880 }
881
882 void sb_dsp_disable_midi(int io_base)
883 {
884 }
885
886 void sb_dsp_disable_recording(int io_base)
887 {
888 }
889
890 /* if (sbmpu) below we allow mpu401 to manage the midi devs
891    otherwise we have to unload them. (Andrzej Krzysztofowicz) */
892    
893 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
894 {
895         sb_devc *devc;
896
897         devc = audio_devs[hw_config->slots[0]]->devc;
898
899         if (devc && devc->base == hw_config->io_base)
900         {
901                 if ((devc->model & MDL_ESS) && devc->pcibase)
902                         release_region(devc->pcibase, 8);
903
904                 release_region(devc->base, 16);
905
906                 if (!(devc->caps & SB_NO_AUDIO))
907                 {
908                         sound_free_dma(devc->dma8);
909                         if (devc->dma16 >= 0)
910                                 sound_free_dma(devc->dma16);
911                 }
912                 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
913                 {
914                         if (devc->irq > 0)
915                                 free_irq(devc->irq, devc);
916
917                         sb_mixer_unload(devc);
918                         /* We don't have to do this bit any more the UART401 is its own
919                                 master  -- Krzysztof Halasa */
920                         /* But we have to do it, if UART401 is not detected */
921                         if (!sbmpu)
922                                 sound_unload_mididev(devc->my_mididev);
923                         sound_unload_audiodev(devc->dev);
924                 }
925                 kfree(devc);
926         }
927         else
928                 release_region(hw_config->io_base, 16);
929         if(detected_devc)
930                 kfree(detected_devc);
931 }
932
933 /*
934  *      Mixer access routines
935  *
936  *      ES1887 modifications: some mixer registers reside in the
937  *      range above 0xa0. These must be accessed in another way.
938  */
939
940 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
941 {
942         unsigned long flags;
943
944         if (devc->model == MDL_ESS) return ess_setmixer (devc, port, value);
945
946         spin_lock_irqsave(&devc->lock, flags);
947
948         outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
949         udelay(20);
950         outb(((unsigned char) (value & 0xff)), MIXER_DATA);
951         udelay(20);
952
953         spin_unlock_irqrestore(&devc->lock, flags);
954 }
955
956 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
957 {
958         unsigned int val;
959         unsigned long flags;
960
961         if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
962
963         spin_lock_irqsave(&devc->lock, flags);
964
965         outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
966         udelay(20);
967         val = inb(MIXER_DATA);
968         udelay(20);
969
970         spin_unlock_irqrestore(&devc->lock, flags);
971
972         return val;
973 }
974
975 void sb_chgmixer
976         (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
977 {
978         int value;
979
980         value = sb_getmixer(devc, reg);
981         value = (value & ~mask) | (val & mask);
982         sb_setmixer(devc, reg, value);
983 }
984
985 /*
986  *      MPU401 MIDI initialization.
987  */
988
989 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
990 {
991         unsigned long flags;
992
993         spin_lock_irqsave(&jazz16_lock, flags);  /* NOT the SB card? */
994
995         outb((addr & 0xff), base + 1);  /* Low address bits */
996         outb((addr >> 8), base + 2);    /* High address bits */
997         outb((val), base);      /* Data */
998
999         spin_unlock_irqrestore(&jazz16_lock, flags);
1000 }
1001
1002 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
1003 {
1004         unsigned long flags;
1005         unsigned char val;
1006
1007         spin_lock_irqsave(&jazz16_lock, flags);  /* NOT the SB card? */
1008
1009         outb((addr & 0xff), base + 1);  /* Low address bits */
1010         outb((addr >> 8), base + 2);    /* High address bits */
1011         val = inb(base);        /* Data */
1012
1013         spin_unlock_irqrestore(&jazz16_lock, flags);
1014         return val;
1015 }
1016
1017 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
1018 {
1019         int mpu_base = hw_config->io_base;
1020         int mp_base = mpu_base + 4;             /* Microcontroller base */
1021         int i;
1022         unsigned char control;
1023
1024
1025         /*
1026          *  Reset the microcontroller so that the RAM can be accessed
1027          */
1028
1029         control = inb(mpu_base + 7);
1030         outb((control | 3), mpu_base + 7);      /* Set last two bits to 1 (?) */
1031         outb(((control & 0xfe) | 2), mpu_base + 7);     /* xxxxxxx0 resets the mc */
1032
1033         mdelay(3);      /* Wait at least 1ms */
1034
1035         outb((control & 0xfc), mpu_base + 7);   /* xxxxxx00 enables RAM */
1036
1037         /*
1038          *  Detect microcontroller by probing the 8k RAM area
1039          */
1040         smw_putmem(devc, mp_base, 0, 0x00);
1041         smw_putmem(devc, mp_base, 1, 0xff);
1042         udelay(10);
1043
1044         if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
1045         {
1046                 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
1047                 return 0;       /* No RAM */
1048         }
1049         /*
1050          *  There is RAM so assume it's really a SM Wave
1051          */
1052
1053         devc->model = MDL_SMW;
1054         smw_mixer_init(devc);
1055
1056 #ifdef MODULE
1057         if (!smw_ucode)
1058         {
1059                 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1060                 smw_free = smw_ucode;
1061         }
1062 #endif
1063         if (smw_ucodeLen > 0)
1064         {
1065                 if (smw_ucodeLen != 8192)
1066                 {
1067                         printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1068                         return 1;
1069                 }
1070                 /*
1071                  *  Download microcode
1072                  */
1073
1074                 for (i = 0; i < 8192; i++)
1075                         smw_putmem(devc, mp_base, i, smw_ucode[i]);
1076
1077                 /*
1078                  *  Verify microcode
1079                  */
1080
1081                 for (i = 0; i < 8192; i++)
1082                         if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1083                         {
1084                                 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1085                                 return 0;
1086                         }
1087         }
1088         control = 0;
1089 #ifdef SMW_SCSI_IRQ
1090         /*
1091          * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1092          * is disabled by default.
1093          *
1094          * FIXME - make this a module option
1095          *
1096          * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1097          */
1098         {
1099                 static unsigned char scsi_irq_bits[] = {
1100                         0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1101                 };
1102                 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1103         }
1104 #endif
1105
1106 #ifdef SMW_OPL4_ENABLE
1107         /*
1108          *  Make the OPL4 chip visible on the PC bus at 0x380.
1109          *
1110          *  There is no need to enable this feature since this driver
1111          *  doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1112          *  enabling OPL4 is pretty useless.
1113          */
1114         control |= 0x10;        /* Uses IRQ12 if bit 0x20 == 0 */
1115         /* control |= 0x20;      Uncomment this if you want to use IRQ7 */
1116 #endif
1117         outb((control | 0x03), mpu_base + 7);   /* xxxxxx11 restarts */
1118         hw_config->name = "SoundMan Wave";
1119         return 1;
1120 }
1121
1122 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1123 {
1124         int mpu_base = hw_config->io_base;
1125         int sb_base = devc->base;
1126         int irq = hw_config->irq;
1127
1128         unsigned char bits = 0;
1129         unsigned long flags;
1130
1131         if (irq < 0)
1132                 irq *= -1;
1133
1134         if (irq < 1 || irq > 15 ||
1135             jazz_irq_bits[irq] == 0)
1136         {
1137                 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1138                 return 0;
1139         }
1140         switch (sb_base)
1141         {
1142                 case 0x220:
1143                         bits = 1;
1144                         break;
1145                 case 0x240:
1146                         bits = 2;
1147                         break;
1148                 case 0x260:
1149                         bits = 3;
1150                         break;
1151                 default:
1152                         return 0;
1153         }
1154         bits = jazz16_bits = bits << 5;
1155         switch (mpu_base)
1156         {
1157                 case 0x310:
1158                         bits |= 1;
1159                         break;
1160                 case 0x320:
1161                         bits |= 2;
1162                         break;
1163                 case 0x330:
1164                         bits |= 3;
1165                         break;
1166                 default:
1167                         printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1168                         return 0;
1169         }
1170         /*
1171          *      Magic wake up sequence by writing to 0x201 (aka Joystick port)
1172          */
1173         spin_lock_irqsave(&jazz16_lock, flags);
1174         outb(0xAF, 0x201);
1175         outb(0x50, 0x201);
1176         outb(bits, 0x201);
1177         spin_unlock_irqrestore(&jazz16_lock, flags);
1178
1179         hw_config->name = "Jazz16";
1180         smw_midi_init(devc, hw_config);
1181
1182         if (!sb_dsp_command(devc, 0xfb))
1183                 return 0;
1184
1185         if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1186                             (jazz_dma_bits[devc->dma16] << 4)))
1187                 return 0;
1188
1189         if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1190                             (jazz_irq_bits[irq] << 4)))
1191                 return 0;
1192
1193         return 1;
1194 }
1195
1196 int probe_sbmpu(struct address_info *hw_config, struct module *owner)
1197 {
1198         sb_devc *devc = last_devc;
1199         int ret;
1200
1201         if (last_devc == NULL)
1202                 return 0;
1203
1204         last_devc = 0;
1205
1206         if (hw_config->io_base <= 0)
1207         {
1208                 /* The real vibra16 is fine about this, but we have to go
1209                    wipe up after Cyrix again */
1210                            
1211                 if(devc->model == MDL_SB16 && devc->minor >= 12)
1212                 {
1213                         unsigned char   bits = sb_getmixer(devc, 0x84) & ~0x06;
1214                         sb_setmixer(devc, 0x84, bits | 0x02);           /* Disable MPU */
1215                 }
1216                 return 0;
1217         }
1218
1219 #if defined(CONFIG_SOUND_MPU401)
1220         if (devc->model == MDL_ESS)
1221         {
1222                 if (check_region(hw_config->io_base, 2))
1223                 {
1224                         printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1225                         return 0;
1226                 }
1227                 if (!ess_midi_init(devc, hw_config))
1228                         return 0;
1229                 hw_config->name = "ESS1xxx MPU";
1230                 devc->midi_irq_cookie = NULL;
1231                 if (!probe_mpu401(hw_config))
1232                         return 0;
1233                 attach_mpu401(hw_config, owner);
1234                 if (last_sb->irq == -hw_config->irq)
1235                         last_sb->midi_irq_cookie=(void *)hw_config->slots[1];
1236                 return 1;
1237         }
1238 #endif
1239
1240         switch (devc->model)
1241         {
1242                 case MDL_SB16:
1243                         if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1244                         {
1245                                 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1246                                 return 0;
1247                         }
1248                         hw_config->name = "Sound Blaster 16";
1249                         if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1250                                 hw_config->irq = -devc->irq;
1251                         if (devc->minor > 12)           /* What is Vibra's version??? */
1252                                 sb16_set_mpu_port(devc, hw_config);
1253                         break;
1254
1255                 case MDL_JAZZ:
1256                         if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1257                                 hw_config->irq = -devc->irq;
1258                         if (!init_Jazz16_midi(devc, hw_config))
1259                                 return 0;
1260                         break;
1261
1262                 case MDL_YMPCI:
1263                         hw_config->name = "Yamaha PCI Legacy";
1264                         printk("Yamaha PCI legacy UART401 check.\n");
1265                         break;
1266                 default:
1267                         return 0;
1268         }
1269         
1270         ret = probe_uart401(hw_config, owner);
1271         if (ret)
1272                 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1273         return ret;
1274 }
1275
1276 void unload_sbmpu(struct address_info *hw_config)
1277 {
1278 #if defined(CONFIG_SOUND_MPU401)
1279         if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
1280                 unload_mpu401(hw_config);
1281                 return;
1282         }
1283 #endif
1284         unload_uart401(hw_config);
1285 }
1286
1287 EXPORT_SYMBOL(sb_dsp_init);
1288 EXPORT_SYMBOL(sb_dsp_detect);
1289 EXPORT_SYMBOL(sb_dsp_unload);
1290 EXPORT_SYMBOL(sb_dsp_disable_midi);
1291 EXPORT_SYMBOL(sb_be_quiet);
1292 EXPORT_SYMBOL(probe_sbmpu);
1293 EXPORT_SYMBOL(unload_sbmpu);
1294 EXPORT_SYMBOL(smw_free);
1295 MODULE_LICENSE("GPL");