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