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