Import 2.4.0-test6pre3
[davej-history.git] / drivers / sound / sb_common.c
blob58d5ef474c02ea0a6b5349f229d65e75f13bbe5b
1 /*
2 * sound/sb_common.c
4 * Common routines for Sound Blaster compatible cards.
7 * Copyright (C) by Hannu Savolainen 1993-1997
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.
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.
19 * 2000/01/18 - separated sb_card and sb_common -
20 * Jeff Garzik <jgarzik@mandrakesoft.com>
24 #include <linux/config.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
29 #include "sound_config.h"
30 #include "sound_firmware.h"
32 #include "mpu401.h"
34 #include "sb_mixer.h"
35 #include "sb.h"
36 #include "sb_ess.h"
39 * global module flag
42 int sb_be_quiet = 0;
44 static sb_devc *detected_devc = NULL; /* For communication from probe to init */
45 static sb_devc *last_devc = NULL; /* For MPU401 initialization */
47 static unsigned char jazz_irq_bits[] = {
48 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
51 static unsigned char jazz_dma_bits[] = {
52 0, 1, 0, 2, 0, 3, 0, 4
55 void *smw_free = NULL;
58 * Jazz16 chipset specific control variables
61 static int jazz16_base = 0; /* Not detected */
62 static unsigned char jazz16_bits = 0; /* I/O relocation bits */
65 * Logitech Soundman Wave specific initialization code
68 #ifdef SMW_MIDI0001_INCLUDED
69 #include "smw-midi0001.h"
70 #else
71 static unsigned char *smw_ucode = NULL;
72 static int smw_ucodeLen = 0;
74 #endif
76 sb_devc *last_sb = NULL; /* Last sb loaded */
78 int sb_dsp_command(sb_devc * devc, unsigned char val)
80 int i;
81 unsigned long limit;
83 limit = jiffies + HZ / 10; /* Timeout */
86 * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
87 * called while interrupts are disabled. This means that the timer is
88 * disabled also. However the timeout situation is a abnormal condition.
89 * Normally the DSP should be ready to accept commands after just couple of
90 * loops.
93 for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
95 if ((inb(DSP_STATUS) & 0x80) == 0)
97 outb((val), DSP_COMMAND);
98 return 1;
101 printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
102 return 0;
105 int sb_dsp_get_byte(sb_devc * devc)
107 int i;
109 for (i = 1000; i; i--)
111 if (inb(DSP_DATA_AVAIL) & 0x80)
112 return inb(DSP_READ);
114 return 0xffff;
117 static void sb_intr (sb_devc *devc)
119 int status;
120 unsigned char src = 0xff;
122 if (devc->model == MDL_SB16)
124 src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
126 if (src & 4) /* MPU401 interrupt */
127 if(devc->midi_irq_cookie)
128 uart401intr(devc->irq, devc->midi_irq_cookie, NULL);
130 if (!(src & 3))
131 return; /* Not a DSP interrupt */
133 if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
135 switch (devc->irq_mode)
137 case IMODE_OUTPUT:
138 DMAbuf_outputintr(devc->dev, 1);
139 break;
141 case IMODE_INPUT:
142 DMAbuf_inputintr(devc->dev);
143 break;
145 case IMODE_INIT:
146 break;
148 case IMODE_MIDI:
149 sb_midi_interrupt(devc);
150 break;
152 default:
153 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
157 else if (devc->intr_active_16 && (src & 0x02))
159 switch (devc->irq_mode_16)
161 case IMODE_OUTPUT:
162 DMAbuf_outputintr(devc->dev, 1);
163 break;
165 case IMODE_INPUT:
166 DMAbuf_inputintr(devc->dev);
167 break;
169 case IMODE_INIT:
170 break;
172 default:
173 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
178 * Acknowledge interrupts
181 if (src & 0x01)
182 status = inb(DSP_DATA_AVAIL);
184 if (devc->model == MDL_SB16 && src & 0x02)
185 status = inb(DSP_DATA_AVL16);
188 static void pci_intr(sb_devc *devc)
190 int src = inb(devc->pcibase+0x1A);
191 src&=3;
192 if(src)
193 sb_intr(devc);
196 static void sbintr(int irq, void *dev_id, struct pt_regs *dummy)
198 sb_devc *devc = dev_id;
200 devc->irq_ok = 1;
202 switch (devc->model) {
203 case MDL_ESSPCI:
204 pci_intr (devc);
205 break;
207 case MDL_ESS:
208 ess_intr (devc);
209 break;
210 default:
211 sb_intr (devc);
212 break;
216 int sb_dsp_reset(sb_devc * devc)
218 int loopc;
220 DEB(printk("Entered sb_dsp_reset()\n"));
222 if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
224 /* This is only for non-ESS chips */
226 outb(1, DSP_RESET);
228 udelay(10);
229 outb(0, DSP_RESET);
230 udelay(30);
232 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
234 if (inb(DSP_READ) != 0xAA)
236 DDB(printk("sb: No response to RESET\n"));
237 return 0; /* Sorry */
240 DEB(printk("sb_dsp_reset() OK\n"));
242 return 1;
245 static void dsp_get_vers(sb_devc * devc)
247 int i;
249 unsigned long flags;
251 DDB(printk("Entered dsp_get_vers()\n"));
252 save_flags(flags);
253 cli();
254 devc->major = devc->minor = 0;
255 sb_dsp_command(devc, 0xe1); /* Get version */
257 for (i = 100000; i; i--)
259 if (inb(DSP_DATA_AVAIL) & 0x80)
261 if (devc->major == 0)
262 devc->major = inb(DSP_READ);
263 else
265 devc->minor = inb(DSP_READ);
266 break;
270 DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
271 restore_flags(flags);
274 static int sb16_set_dma_hw(sb_devc * devc)
276 int bits;
278 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
280 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
281 return 0;
283 bits = (1 << devc->dma8);
285 if (devc->dma16 >= 5 && devc->dma16 <= 7)
286 bits |= (1 << devc->dma16);
288 sb_setmixer(devc, DMA_NR, bits);
289 return 1;
292 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
295 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
297 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
299 switch (hw_config->io_base)
301 case 0x300:
302 sb_setmixer(devc, 0x84, bits | 0x04);
303 break;
305 case 0x330:
306 sb_setmixer(devc, 0x84, bits | 0x00);
307 break;
309 default:
310 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
311 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
315 static int sb16_set_irq_hw(sb_devc * devc, int level)
317 int ival;
319 switch (level)
321 case 5:
322 ival = 2;
323 break;
324 case 7:
325 ival = 4;
326 break;
327 case 9:
328 ival = 1;
329 break;
330 case 10:
331 ival = 8;
332 break;
333 default:
334 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
335 return 0;
337 sb_setmixer(devc, IRQ_NR, ival);
338 return 1;
341 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
343 unsigned char bits = 0;
344 unsigned long flags;
346 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
347 return;
349 switch (hw_config->io_base)
351 case 0x220:
352 bits = 1;
353 break;
354 case 0x240:
355 bits = 2;
356 break;
357 case 0x260:
358 bits = 3;
359 break;
360 default:
361 return;
363 bits = jazz16_bits = bits << 5;
364 jazz16_base = hw_config->io_base;
367 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
369 save_flags(flags);
370 cli();
371 outb((0xAF), 0x201);
372 outb((0x50), 0x201);
373 outb((bits), 0x201);
374 restore_flags(flags);
377 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
379 char name[100];
381 * First try to check that the card has Jazz16 chip. It identifies itself
382 * by returning 0x12 as response to DSP command 0xfa.
385 if (!sb_dsp_command(devc, 0xfa))
386 return 0;
388 if (sb_dsp_get_byte(devc) != 0x12)
389 return 0;
392 * OK so far. Now configure the IRQ and DMA channel used by the card.
394 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
396 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
397 return 0;
399 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
401 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
402 return 0;
404 if (hw_config->dma2 < 0)
406 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
407 return 0;
409 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
411 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
412 return 0;
414 devc->dma16 = hw_config->dma2;
416 if (!sb_dsp_command(devc, 0xfb))
417 return 0;
419 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
420 (jazz_dma_bits[hw_config->dma2] << 4)))
421 return 0;
423 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
424 return 0;
427 * Now we have configured a standard Jazz16 device.
429 devc->model = MDL_JAZZ;
430 strcpy(name, "Jazz16");
432 hw_config->name = "Jazz16";
433 devc->caps |= SB_NO_MIDI;
434 return 1;
437 static void relocate_ess1688(sb_devc * devc)
439 unsigned char bits;
441 switch (devc->base)
443 case 0x220:
444 bits = 0x04;
445 break;
446 case 0x230:
447 bits = 0x05;
448 break;
449 case 0x240:
450 bits = 0x06;
451 break;
452 case 0x250:
453 bits = 0x07;
454 break;
455 default:
456 return; /* Wrong port */
459 DDB(printk("Doing ESS1688 address selection\n"));
462 * ES1688 supports two alternative ways for software address config.
463 * First try the so called Read-Sequence-Key method.
466 /* Reset the sequence logic */
467 inb(0x229);
468 inb(0x229);
469 inb(0x229);
471 /* Perform the read sequence */
472 inb(0x22b);
473 inb(0x229);
474 inb(0x22b);
475 inb(0x229);
476 inb(0x229);
477 inb(0x22b);
478 inb(0x229);
480 /* Select the base address by reading from it. Then probe using the port. */
481 inb(devc->base);
482 if (sb_dsp_reset(devc)) /* Bingo */
483 return;
485 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
487 * The last resort is the system control register method.
490 outb((0x00), 0xfb); /* 0xFB is the unlock register */
491 outb((0x00), 0xe0); /* Select index 0 */
492 outb((bits), 0xe1); /* Write the config bits */
493 outb((0x00), 0xf9); /* 0xFB is the lock register */
494 #endif
497 int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
499 sb_devc sb_info;
500 sb_devc *devc = &sb_info;
502 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
504 /* Copy module options in place */
505 if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
507 sb_info.my_mididev = -1;
508 sb_info.my_mixerdev = -1;
509 sb_info.dev = -1;
512 * Initialize variables
515 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
516 if (check_region(hw_config->io_base, 16))
518 #ifdef MODULE
519 printk(KERN_INFO "sb: I/O region in use.\n");
520 #endif
521 return 0;
524 devc->type = hw_config->card_subtype;
526 devc->base = hw_config->io_base;
527 devc->irq = hw_config->irq;
528 devc->dma8 = hw_config->dma;
530 devc->dma16 = -1;
531 devc->pcibase = pciio;
533 if(pci == SB_PCI_ESSMAESTRO)
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;
541 if(pci == SB_PCI_YAMAHA)
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;
548 printk("Yamaha PCI mode.\n");
551 if (devc->sbmo.acer)
553 cli();
554 inb(devc->base + 0x09);
555 inb(devc->base + 0x09);
556 inb(devc->base + 0x09);
557 inb(devc->base + 0x0b);
558 inb(devc->base + 0x09);
559 inb(devc->base + 0x0b);
560 inb(devc->base + 0x09);
561 inb(devc->base + 0x09);
562 inb(devc->base + 0x0b);
563 inb(devc->base + 0x09);
564 inb(devc->base + 0x00);
565 sti();
568 * Detect the device
571 if (sb_dsp_reset(devc))
572 dsp_get_vers(devc);
573 else
574 devc->major = 0;
576 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
577 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
578 relocate_Jazz16(devc, hw_config);
580 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
581 relocate_ess1688(devc);
583 if (!sb_dsp_reset(devc))
585 DDB(printk("SB reset failed\n"));
586 #ifdef MODULE
587 printk(KERN_INFO "sb: dsp reset failed.\n");
588 #endif
589 return 0;
591 if (devc->major == 0)
592 dsp_get_vers(devc);
594 if (devc->major == 3 && devc->minor == 1)
596 if (devc->type == MDL_AZTECH) /* SG Washington? */
598 if (sb_dsp_command(devc, 0x09))
599 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
601 int i;
603 /* Have some delay */
604 for (i = 0; i < 10000; i++)
605 inb(DSP_DATA_AVAIL);
606 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
607 devc->model = MDL_AZTECH;
612 if(devc->type == MDL_ESSPCI)
613 devc->model = MDL_ESSPCI;
615 if(devc->type == MDL_YMPCI)
617 printk("YMPCI selected\n");
618 devc->model = MDL_YMPCI;
622 * Save device information for sb_dsp_init()
626 detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
627 if (detected_devc == NULL)
629 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
630 return 0;
632 memcpy((char *) detected_devc, (char *) devc, sizeof(sb_devc));
633 MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
634 return 1;
637 int sb_dsp_init(struct address_info *hw_config)
639 sb_devc *devc;
640 char name[100];
641 extern int sb_be_quiet;
642 int mixer22, mixer30;
645 * Check if we had detected a SB device earlier
647 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
648 name[0] = 0;
650 if (detected_devc == NULL)
652 MDB(printk("No detected device\n"));
653 return 0;
655 devc = detected_devc;
656 detected_devc = NULL;
658 if (devc->base != hw_config->io_base)
660 DDB(printk("I/O port mismatch\n"));
661 return 0;
664 * Now continue initialization of the device
667 devc->caps = hw_config->driver_use_1;
669 if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
670 { /* IRQ setup */
673 * ESS PCI cards do shared PCI IRQ stuff. Since they
674 * will get shared PCI irq lines we must cope.
677 int i=(devc->caps&SB_PCI_IRQ)?SA_SHIRQ:0;
679 if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
681 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
682 return 0;
684 devc->irq_ok = 0;
686 if (devc->major == 4)
687 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
689 free_irq(devc->irq, devc);
690 return 0;
692 if ((devc->type == 0 || devc->type == MDL_ESS) &&
693 devc->major == 3 && devc->minor == 1)
694 { /* Handle various chipsets which claim they are SB Pro compatible */
695 if ((devc->type != 0 && devc->type != MDL_ESS) ||
696 !ess_init(devc, hw_config))
698 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
699 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
701 DDB(printk("This is a genuine SB Pro\n"));
705 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
706 devc->irq_ok = 1;
707 else
709 int n;
711 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
713 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
715 int i;
717 for (i = 0; !devc->irq_ok && i < 10000; i++);
720 if (!devc->irq_ok)
721 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
722 else
724 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
727 } /* IRQ setup */
728 request_region(hw_config->io_base, 16, "soundblaster");
730 last_sb = devc;
732 switch (devc->major)
734 case 1: /* SB 1.0 or 1.5 */
735 devc->model = hw_config->card_subtype = MDL_SB1;
736 break;
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;
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;
756 break;
758 case 4:
759 devc->model = hw_config->card_subtype = MDL_SB16;
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.
767 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
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))
776 devc->submodel = SUBMDL_ALS100;
777 if (hw_config->name == NULL)
778 hw_config->name = "Sound Blaster 16 (ALS-100)";
780 else
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)";
789 sb_setmixer(devc,0x30,mixer30);
791 else if (hw_config->name == NULL)
792 hw_config->name = "Sound Blaster 16";
794 if (hw_config->dma2 == -1)
795 devc->dma16 = devc->dma8;
796 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
798 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
799 devc->dma16 = devc->dma8;
801 else
802 devc->dma16 = hw_config->dma2;
804 if(!sb16_set_dma_hw(devc)) {
805 free_irq(devc->irq, devc);
806 release_region(hw_config->io_base, 16);
807 return 0;
810 devc->caps |= SB_NO_MIDI;
813 if (!(devc->caps & SB_NO_MIXER))
814 if (devc->major == 3 || devc->major == 4)
815 sb_mixer_init(devc);
817 if (!(devc->caps & SB_NO_MIDI))
818 sb_dsp_midi_init(devc);
820 if (hw_config->name == NULL)
821 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
823 sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
824 conf_printf(name, hw_config);
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.
833 if (devc->model <= MDL_SBPRO)
835 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
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");
842 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
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");
849 hw_config->card_subtype = devc->model;
850 hw_config->slots[0]=devc->dev;
851 last_devc = devc; /* For SB MPU detection */
853 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
855 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
857 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
859 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
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);
864 sb_audio_init(devc, name);
865 hw_config->slots[0]=devc->dev;
867 else
869 MDB(printk("Sound Blaster: no audio devices found.\n"));
871 return 1;
874 void sb_dsp_disable_midi(int io_base)
878 void sb_dsp_disable_recording(int io_base)
882 /* if (sbmpu) below we allow mpu401 to manage the midi devs
883 otherwise we have to unload them. (Andrzej Krzysztofowicz) */
885 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
887 sb_devc *devc;
889 devc = audio_devs[hw_config->slots[0]]->devc;
891 if (devc && devc->base == hw_config->io_base)
893 if ((devc->model & MDL_ESS) && devc->pcibase)
894 release_region(devc->pcibase, 8);
896 release_region(devc->base, 16);
898 if (!(devc->caps & SB_NO_AUDIO))
900 sound_free_dma(devc->dma8);
901 if (devc->dma16 >= 0)
902 sound_free_dma(devc->dma16);
904 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
906 if (devc->irq > 0)
907 free_irq(devc->irq, devc);
909 sb_mixer_unload(devc);
910 /* We don't have to do this bit any more the UART401 is its own
911 master -- Krzysztof Halasa */
912 /* But we have to do it, if UART401 is not detected */
913 if (!sbmpu)
914 sound_unload_mididev(devc->my_mididev);
915 sound_unload_audiodev(devc->dev);
917 kfree(devc);
919 else
920 release_region(hw_config->io_base, 16);
921 if(detected_devc)
922 kfree(detected_devc);
926 * Mixer access routines
928 * ES1887 modifications: some mixer registers reside in the
929 * range above 0xa0. These must be accessed in another way.
932 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
934 unsigned long flags;
936 if (devc->model == MDL_ESS) return ess_setmixer (devc, port, value);
938 save_flags(flags);
939 cli();
941 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
942 udelay(20);
943 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
944 udelay(20);
946 restore_flags(flags);
949 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
951 unsigned int val;
952 unsigned long flags;
954 if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
956 save_flags(flags);
957 cli();
959 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
960 udelay(20);
961 val = inb(MIXER_DATA);
962 udelay(20);
964 restore_flags(flags);
966 return val;
969 void sb_chgmixer
970 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
972 int value;
974 value = sb_getmixer(devc, reg);
975 value = (value & ~mask) | (val & mask);
976 sb_setmixer(devc, reg, value);
980 * MPU401 MIDI initialization.
983 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
985 unsigned long flags;
987 save_flags(flags);
988 cli();
990 outb((addr & 0xff), base + 1); /* Low address bits */
991 outb((addr >> 8), base + 2); /* High address bits */
992 outb((val), base); /* Data */
994 restore_flags(flags);
997 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
999 unsigned long flags;
1000 unsigned char val;
1002 save_flags(flags);
1003 cli();
1005 outb((addr & 0xff), base + 1); /* Low address bits */
1006 outb((addr >> 8), base + 2); /* High address bits */
1007 val = inb(base); /* Data */
1009 restore_flags(flags);
1010 return val;
1013 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
1015 int mpu_base = hw_config->io_base;
1016 int mp_base = mpu_base + 4; /* Microcontroller base */
1017 int i;
1018 unsigned char control;
1022 * Reset the microcontroller so that the RAM can be accessed
1025 control = inb(mpu_base + 7);
1026 outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
1027 outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
1029 mdelay(3); /* Wait at least 1ms */
1031 outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
1034 * Detect microcontroller by probing the 8k RAM area
1036 smw_putmem(devc, mp_base, 0, 0x00);
1037 smw_putmem(devc, mp_base, 1, 0xff);
1038 udelay(10);
1040 if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
1042 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
1043 return 0; /* No RAM */
1046 * There is RAM so assume it's really a SM Wave
1049 devc->model = MDL_SMW;
1050 smw_mixer_init(devc);
1052 #ifdef MODULE
1053 if (!smw_ucode)
1055 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1056 smw_free = smw_ucode;
1058 #endif
1059 if (smw_ucodeLen > 0)
1061 if (smw_ucodeLen != 8192)
1063 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1064 return 1;
1067 * Download microcode
1070 for (i = 0; i < 8192; i++)
1071 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1074 * Verify microcode
1077 for (i = 0; i < 8192; i++)
1078 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1080 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1081 return 0;
1084 control = 0;
1085 #ifdef SMW_SCSI_IRQ
1087 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1088 * is disabled by default.
1090 * FIXME - make this a module option
1092 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1095 static unsigned char scsi_irq_bits[] = {
1096 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1098 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1100 #endif
1102 #ifdef SMW_OPL4_ENABLE
1104 * Make the OPL4 chip visible on the PC bus at 0x380.
1106 * There is no need to enable this feature since this driver
1107 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1108 * enabling OPL4 is pretty useless.
1110 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1111 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1112 #endif
1113 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1114 hw_config->name = "SoundMan Wave";
1115 return 1;
1118 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1120 int mpu_base = hw_config->io_base;
1121 int sb_base = devc->base;
1122 int irq = hw_config->irq;
1124 unsigned char bits = 0;
1125 unsigned long flags;
1127 if (irq < 0)
1128 irq *= -1;
1130 if (irq < 1 || irq > 15 ||
1131 jazz_irq_bits[irq] == 0)
1133 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1134 return 0;
1136 switch (sb_base)
1138 case 0x220:
1139 bits = 1;
1140 break;
1141 case 0x240:
1142 bits = 2;
1143 break;
1144 case 0x260:
1145 bits = 3;
1146 break;
1147 default:
1148 return 0;
1150 bits = jazz16_bits = bits << 5;
1151 switch (mpu_base)
1153 case 0x310:
1154 bits |= 1;
1155 break;
1156 case 0x320:
1157 bits |= 2;
1158 break;
1159 case 0x330:
1160 bits |= 3;
1161 break;
1162 default:
1163 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1164 return 0;
1167 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1169 save_flags(flags);
1170 cli();
1171 outb(0xAF, 0x201);
1172 outb(0x50, 0x201);
1173 outb(bits, 0x201);
1174 restore_flags(flags);
1176 hw_config->name = "Jazz16";
1177 smw_midi_init(devc, hw_config);
1179 if (!sb_dsp_command(devc, 0xfb))
1180 return 0;
1182 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1183 (jazz_dma_bits[devc->dma16] << 4)))
1184 return 0;
1186 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1187 (jazz_irq_bits[irq] << 4)))
1188 return 0;
1190 return 1;
1193 void attach_sbmpu(struct address_info *hw_config)
1195 if (last_sb->model == MDL_ESS) {
1196 #if defined(CONFIG_SOUND_MPU401)
1197 attach_mpu401(hw_config);
1198 if (last_sb->irq == -hw_config->irq) {
1199 last_sb->midi_irq_cookie=(void *)hw_config->slots[1];
1201 #endif
1202 return;
1204 attach_uart401(hw_config);
1205 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1208 int probe_sbmpu(struct address_info *hw_config)
1210 sb_devc *devc = last_devc;
1212 if (last_devc == NULL)
1213 return 0;
1215 last_devc = 0;
1217 if (hw_config->io_base <= 0)
1219 /* The real vibra16 is fine about this, but we have to go
1220 wipe up after Cyrix again */
1222 if(devc->model == MDL_SB16 && devc->minor >= 12)
1224 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
1225 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
1227 return 0;
1230 #if defined(CONFIG_SOUND_MPU401)
1231 if (devc->model == MDL_ESS)
1233 if (check_region(hw_config->io_base, 2))
1235 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1236 return 0;
1238 if (!ess_midi_init(devc, hw_config))
1239 return 0;
1240 hw_config->name = "ESS1xxx MPU";
1241 devc->midi_irq_cookie = -1;
1242 return probe_mpu401(hw_config);
1244 #endif
1246 if (check_region(hw_config->io_base, 4))
1248 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1249 return 0;
1251 switch (devc->model)
1253 case MDL_SB16:
1254 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1256 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1257 return 0;
1259 hw_config->name = "Sound Blaster 16";
1260 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1261 hw_config->irq = -devc->irq;
1262 if (devc->minor > 12) /* What is Vibra's version??? */
1263 sb16_set_mpu_port(devc, hw_config);
1264 break;
1266 case MDL_JAZZ:
1267 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1268 hw_config->irq = -devc->irq;
1269 if (!init_Jazz16_midi(devc, hw_config))
1270 return 0;
1271 break;
1273 case MDL_YMPCI:
1274 hw_config->name = "Yamaha PCI Legacy";
1275 printk("Yamaha PCI legacy UART401 check.\n");
1276 break;
1277 default:
1278 return 0;
1280 return probe_uart401(hw_config);
1283 void unload_sbmpu(struct address_info *hw_config)
1285 #if defined(CONFIG_SOUND_MPU401)
1286 if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
1287 unload_mpu401(hw_config);
1288 return;
1290 #endif
1291 unload_uart401(hw_config);
1294 EXPORT_SYMBOL(sb_dsp_init);
1295 EXPORT_SYMBOL(sb_dsp_detect);
1296 EXPORT_SYMBOL(sb_dsp_unload);
1297 EXPORT_SYMBOL(sb_dsp_disable_midi);
1298 EXPORT_SYMBOL(sb_be_quiet);
1299 EXPORT_SYMBOL(attach_sbmpu);
1300 EXPORT_SYMBOL(probe_sbmpu);
1301 EXPORT_SYMBOL(unload_sbmpu);
1302 EXPORT_SYMBOL(smw_free);