Merge with Linux 2.3.99-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / sb_common.c
blobad98080491f4c0a74791e8777b5ba5ee319d8490
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"
31 #include "soundmodule.h"
33 #include "mpu401.h"
35 #include "sb_mixer.h"
36 #include "sb.h"
37 #include "sb_ess.h"
40 * global module flag
43 int sb_be_quiet = 0;
45 static sb_devc *detected_devc = NULL; /* For communication from probe to init */
46 static sb_devc *last_devc = NULL; /* For MPU401 initialization */
48 static unsigned char jazz_irq_bits[] = {
49 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
52 static unsigned char jazz_dma_bits[] = {
53 0, 1, 0, 2, 0, 3, 0, 4
56 void *smw_free = NULL;
59 * Jazz16 chipset specific control variables
62 static int jazz16_base = 0; /* Not detected */
63 static unsigned char jazz16_bits = 0; /* I/O relocation bits */
66 * Logitech Soundman Wave specific initialization code
69 #ifdef SMW_MIDI0001_INCLUDED
70 #include "smw-midi0001.h"
71 #else
72 static unsigned char *smw_ucode = NULL;
73 static int smw_ucodeLen = 0;
75 #endif
77 sb_devc *last_sb = NULL; /* Last sb loaded */
79 int sb_dsp_command(sb_devc * devc, unsigned char val)
81 int i;
82 unsigned long limit;
84 limit = jiffies + HZ / 10; /* Timeout */
87 * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
88 * called while interrupts are disabled. This means that the timer is
89 * disabled also. However the timeout situation is a abnormal condition.
90 * Normally the DSP should be ready to accept commands after just couple of
91 * loops.
94 for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
96 if ((inb(DSP_STATUS) & 0x80) == 0)
98 outb((val), DSP_COMMAND);
99 return 1;
102 printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
103 return 0;
106 int sb_dsp_get_byte(sb_devc * devc)
108 int i;
110 for (i = 1000; i; i--)
112 if (inb(DSP_DATA_AVAIL) & 0x80)
113 return inb(DSP_READ);
115 return 0xffff;
118 static void sb_intr (sb_devc *devc)
120 int status;
121 unsigned char src = 0xff;
123 if (devc->model == MDL_SB16)
125 src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
127 if (src & 4) /* MPU401 interrupt */
128 if(devc->midi_irq_cookie)
129 uart401intr(devc->irq, devc->midi_irq_cookie, NULL);
131 if (!(src & 3))
132 return; /* Not a DSP interrupt */
134 if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
136 switch (devc->irq_mode)
138 case IMODE_OUTPUT:
139 DMAbuf_outputintr(devc->dev, 1);
140 break;
142 case IMODE_INPUT:
143 DMAbuf_inputintr(devc->dev);
144 break;
146 case IMODE_INIT:
147 break;
149 case IMODE_MIDI:
150 sb_midi_interrupt(devc);
151 break;
153 default:
154 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
158 else if (devc->intr_active_16 && (src & 0x02))
160 switch (devc->irq_mode_16)
162 case IMODE_OUTPUT:
163 DMAbuf_outputintr(devc->dev, 1);
164 break;
166 case IMODE_INPUT:
167 DMAbuf_inputintr(devc->dev);
168 break;
170 case IMODE_INIT:
171 break;
173 default:
174 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
179 * Acknowledge interrupts
182 if (src & 0x01)
183 status = inb(DSP_DATA_AVAIL);
185 if (devc->model == MDL_SB16 && src & 0x02)
186 status = inb(DSP_DATA_AVL16);
189 static void pci_intr(sb_devc *devc)
191 int src = inb(devc->pcibase+0x1A);
192 src&=3;
193 if(src)
194 sb_intr(devc);
197 static void sbintr(int irq, void *dev_id, struct pt_regs *dummy)
199 sb_devc *devc = dev_id;
201 devc->irq_ok = 1;
203 switch (devc->model) {
204 case MDL_ESSPCI:
205 pci_intr (devc);
206 break;
208 case MDL_ESS:
209 ess_intr (devc);
210 break;
211 default:
212 sb_intr (devc);
213 break;
217 int sb_dsp_reset(sb_devc * devc)
219 int loopc;
221 DEB(printk("Entered sb_dsp_reset()\n"));
223 if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
225 /* This is only for non-ESS chips */
227 outb(1, DSP_RESET);
229 udelay(10);
230 outb(0, DSP_RESET);
231 udelay(30);
233 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
235 if (inb(DSP_READ) != 0xAA)
237 DDB(printk("sb: No response to RESET\n"));
238 return 0; /* Sorry */
241 DEB(printk("sb_dsp_reset() OK\n"));
243 return 1;
246 static void dsp_get_vers(sb_devc * devc)
248 int i;
250 unsigned long flags;
252 DDB(printk("Entered dsp_get_vers()\n"));
253 save_flags(flags);
254 cli();
255 devc->major = devc->minor = 0;
256 sb_dsp_command(devc, 0xe1); /* Get version */
258 for (i = 100000; i; i--)
260 if (inb(DSP_DATA_AVAIL) & 0x80)
262 if (devc->major == 0)
263 devc->major = inb(DSP_READ);
264 else
266 devc->minor = inb(DSP_READ);
267 break;
271 DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
272 restore_flags(flags);
275 static int sb16_set_dma_hw(sb_devc * devc)
277 int bits;
279 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
281 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
282 return 0;
284 bits = (1 << devc->dma8);
286 if (devc->dma16 >= 5 && devc->dma16 <= 7)
287 bits |= (1 << devc->dma16);
289 sb_setmixer(devc, DMA_NR, bits);
290 return 1;
293 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
296 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
298 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
300 switch (hw_config->io_base)
302 case 0x300:
303 sb_setmixer(devc, 0x84, bits | 0x04);
304 break;
306 case 0x330:
307 sb_setmixer(devc, 0x84, bits | 0x00);
308 break;
310 default:
311 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
312 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
316 static int sb16_set_irq_hw(sb_devc * devc, int level)
318 int ival;
320 switch (level)
322 case 5:
323 ival = 2;
324 break;
325 case 7:
326 ival = 4;
327 break;
328 case 9:
329 ival = 1;
330 break;
331 case 10:
332 ival = 8;
333 break;
334 default:
335 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
336 return 0;
338 sb_setmixer(devc, IRQ_NR, ival);
339 return 1;
342 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
344 unsigned char bits = 0;
345 unsigned long flags;
347 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
348 return;
350 switch (hw_config->io_base)
352 case 0x220:
353 bits = 1;
354 break;
355 case 0x240:
356 bits = 2;
357 break;
358 case 0x260:
359 bits = 3;
360 break;
361 default:
362 return;
364 bits = jazz16_bits = bits << 5;
365 jazz16_base = hw_config->io_base;
368 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
370 save_flags(flags);
371 cli();
372 outb((0xAF), 0x201);
373 outb((0x50), 0x201);
374 outb((bits), 0x201);
375 restore_flags(flags);
378 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
380 char name[100];
382 * First try to check that the card has Jazz16 chip. It identifies itself
383 * by returning 0x12 as response to DSP command 0xfa.
386 if (!sb_dsp_command(devc, 0xfa))
387 return 0;
389 if (sb_dsp_get_byte(devc) != 0x12)
390 return 0;
393 * OK so far. Now configure the IRQ and DMA channel used by the card.
395 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
397 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
398 return 0;
400 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
402 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
403 return 0;
405 if (hw_config->dma2 < 0)
407 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
408 return 0;
410 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
412 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
413 return 0;
415 devc->dma16 = hw_config->dma2;
417 if (!sb_dsp_command(devc, 0xfb))
418 return 0;
420 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
421 (jazz_dma_bits[hw_config->dma2] << 4)))
422 return 0;
424 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
425 return 0;
428 * Now we have configured a standard Jazz16 device.
430 devc->model = MDL_JAZZ;
431 strcpy(name, "Jazz16");
433 hw_config->name = "Jazz16";
434 devc->caps |= SB_NO_MIDI;
435 return 1;
438 static void relocate_ess1688(sb_devc * devc)
440 unsigned char bits;
442 switch (devc->base)
444 case 0x220:
445 bits = 0x04;
446 break;
447 case 0x230:
448 bits = 0x05;
449 break;
450 case 0x240:
451 bits = 0x06;
452 break;
453 case 0x250:
454 bits = 0x07;
455 break;
456 default:
457 return; /* Wrong port */
460 DDB(printk("Doing ESS1688 address selection\n"));
463 * ES1688 supports two alternative ways for software address config.
464 * First try the so called Read-Sequence-Key method.
467 /* Reset the sequence logic */
468 inb(0x229);
469 inb(0x229);
470 inb(0x229);
472 /* Perform the read sequence */
473 inb(0x22b);
474 inb(0x229);
475 inb(0x22b);
476 inb(0x229);
477 inb(0x229);
478 inb(0x22b);
479 inb(0x229);
481 /* Select the base address by reading from it. Then probe using the port. */
482 inb(devc->base);
483 if (sb_dsp_reset(devc)) /* Bingo */
484 return;
486 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
488 * The last resort is the system control register method.
491 outb((0x00), 0xfb); /* 0xFB is the unlock register */
492 outb((0x00), 0xe0); /* Select index 0 */
493 outb((bits), 0xe1); /* Write the config bits */
494 outb((0x00), 0xf9); /* 0xFB is the lock register */
495 #endif
498 int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
500 sb_devc sb_info;
501 sb_devc *devc = &sb_info;
503 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
505 /* Copy module options in place */
506 if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
508 sb_info.my_mididev = -1;
509 sb_info.my_mixerdev = -1;
510 sb_info.dev = -1;
513 * Initialize variables
516 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
517 if (check_region(hw_config->io_base, 16))
519 #ifdef MODULE
520 printk(KERN_INFO "sb: I/O region in use.\n");
521 #endif
522 return 0;
525 devc->type = hw_config->card_subtype;
527 devc->base = hw_config->io_base;
528 devc->irq = hw_config->irq;
529 devc->dma8 = hw_config->dma;
531 devc->dma16 = -1;
532 devc->pcibase = pciio;
534 if(pci == SB_PCI_ESSMAESTRO)
536 devc->model = MDL_ESSPCI;
537 devc->caps |= SB_PCI_IRQ;
538 hw_config->driver_use_1 |= SB_PCI_IRQ;
539 hw_config->card_subtype = MDL_ESSPCI;
542 if(pci == SB_PCI_YAMAHA)
544 devc->model = MDL_YMPCI;
545 devc->caps |= SB_PCI_IRQ;
546 hw_config->driver_use_1 |= SB_PCI_IRQ;
547 hw_config->card_subtype = MDL_YMPCI;
549 printk("Yamaha PCI mode.\n");
552 if (devc->sbmo.acer)
554 cli();
555 inb(devc->base + 0x09);
556 inb(devc->base + 0x09);
557 inb(devc->base + 0x09);
558 inb(devc->base + 0x0b);
559 inb(devc->base + 0x09);
560 inb(devc->base + 0x0b);
561 inb(devc->base + 0x09);
562 inb(devc->base + 0x09);
563 inb(devc->base + 0x0b);
564 inb(devc->base + 0x09);
565 inb(devc->base + 0x00);
566 sti();
569 * Detect the device
572 if (sb_dsp_reset(devc))
573 dsp_get_vers(devc);
574 else
575 devc->major = 0;
577 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
578 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
579 relocate_Jazz16(devc, hw_config);
581 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
582 relocate_ess1688(devc);
584 if (!sb_dsp_reset(devc))
586 DDB(printk("SB reset failed\n"));
587 #ifdef MODULE
588 printk(KERN_INFO "sb: dsp reset failed.\n");
589 #endif
590 return 0;
592 if (devc->major == 0)
593 dsp_get_vers(devc);
595 if (devc->major == 3 && devc->minor == 1)
597 if (devc->type == MDL_AZTECH) /* SG Washington? */
599 if (sb_dsp_command(devc, 0x09))
600 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
602 int i;
604 /* Have some delay */
605 for (i = 0; i < 10000; i++)
606 inb(DSP_DATA_AVAIL);
607 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
608 devc->model = MDL_AZTECH;
613 if(devc->type == MDL_ESSPCI)
614 devc->model = MDL_ESSPCI;
616 if(devc->type == MDL_YMPCI)
618 printk("YMPCI selected\n");
619 devc->model = MDL_YMPCI;
623 * Save device information for sb_dsp_init()
627 detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
628 if (detected_devc == NULL)
630 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
631 return 0;
633 memcpy((char *) detected_devc, (char *) devc, sizeof(sb_devc));
634 MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
635 return 1;
638 int sb_dsp_init(struct address_info *hw_config)
640 sb_devc *devc;
641 char name[100];
642 extern int sb_be_quiet;
643 int mixer22, mixer30;
646 * Check if we had detected a SB device earlier
648 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
649 name[0] = 0;
651 if (detected_devc == NULL)
653 MDB(printk("No detected device\n"));
654 return 0;
656 devc = detected_devc;
657 detected_devc = NULL;
659 if (devc->base != hw_config->io_base)
661 DDB(printk("I/O port mismatch\n"));
662 return 0;
665 * Now continue initialization of the device
668 devc->caps = hw_config->driver_use_1;
670 if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
671 { /* IRQ setup */
674 * ESS PCI cards do shared PCI IRQ stuff. Since they
675 * will get shared PCI irq lines we must cope.
678 int i=(devc->caps&SB_PCI_IRQ)?SA_SHIRQ:0;
680 if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
682 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
683 return 0;
685 devc->irq_ok = 0;
687 if (devc->major == 4)
688 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
690 free_irq(devc->irq, devc);
691 return 0;
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))
699 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
700 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
702 DDB(printk("This is a genuine SB Pro\n"));
706 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
707 devc->irq_ok = 1;
708 else
710 int n;
712 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
714 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
716 int i;
718 for (i = 0; !devc->irq_ok && i < 10000; i++);
721 if (!devc->irq_ok)
722 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
723 else
725 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
728 } /* IRQ setup */
729 request_region(hw_config->io_base, 16, "soundblaster");
731 last_sb = devc;
733 switch (devc->major)
735 case 1: /* SB 1.0 or 1.5 */
736 devc->model = hw_config->card_subtype = MDL_SB1;
737 break;
739 case 2: /* SB 2.x */
740 if (devc->minor == 0)
741 devc->model = hw_config->card_subtype = MDL_SB2;
742 else
743 devc->model = hw_config->card_subtype = MDL_SB201;
744 break;
746 case 3: /* SB Pro and most clones */
747 switch (devc->model) {
748 case 0:
749 devc->model = hw_config->card_subtype = MDL_SBPRO;
750 if (hw_config->name == NULL)
751 hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
752 break;
753 case MDL_ESS:
754 ess_dsp_init(devc, hw_config);
755 break;
757 break;
759 case 4:
760 devc->model = hw_config->card_subtype = MDL_SB16;
762 * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
763 * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
764 * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
765 * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
766 * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
768 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
770 mixer30 = sb_getmixer(devc,0x30);
771 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
772 sb_setmixer(devc,0x30,0xff);
773 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
774 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
775 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
777 devc->submodel = SUBMDL_ALS100;
778 if (hw_config->name == NULL)
779 hw_config->name = "Sound Blaster 16 (ALS-100)";
781 else
783 sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
784 sb_setmixer(devc,0x4c,0x1f);
785 sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
786 devc->submodel = SUBMDL_ALS007;
787 if (hw_config->name == NULL)
788 hw_config->name = "Sound Blaster 16 (ALS-007)";
790 sb_setmixer(devc,0x30,mixer30);
792 else if (hw_config->name == NULL)
793 hw_config->name = "Sound Blaster 16";
795 if (hw_config->dma2 == -1)
796 devc->dma16 = devc->dma8;
797 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
799 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
800 devc->dma16 = devc->dma8;
802 else
803 devc->dma16 = hw_config->dma2;
805 if(!sb16_set_dma_hw(devc)) {
806 free_irq(devc->irq, devc);
807 release_region(hw_config->io_base, 16);
808 return 0;
811 devc->caps |= SB_NO_MIDI;
814 if (!(devc->caps & SB_NO_MIXER))
815 if (devc->major == 3 || devc->major == 4)
816 sb_mixer_init(devc);
818 if (!(devc->caps & SB_NO_MIDI))
819 sb_dsp_midi_init(devc);
821 if (hw_config->name == NULL)
822 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
824 sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
825 conf_printf(name, hw_config);
828 * Assuming that a sound card is Sound Blaster (compatible) is the most common
829 * configuration error and the mother of all problems. Usually sound cards
830 * emulate SB Pro but in addition they have a 16 bit native mode which should be
831 * used in Unix. See Readme.cards for more information about configuring OSS/Free
832 * properly.
834 if (devc->model <= MDL_SBPRO)
836 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
838 printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
839 printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
840 printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
841 printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
843 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
845 printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
846 printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
847 printk(KERN_INFO "is incorrectly configured.\n");
850 hw_config->card_subtype = devc->model;
851 hw_config->slots[0]=devc->dev;
852 last_devc = devc; /* For SB MPU detection */
854 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
856 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
858 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
860 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
862 if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
863 printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
865 sb_audio_init(devc, name);
866 hw_config->slots[0]=devc->dev;
868 else
870 MDB(printk("Sound Blaster: no audio devices found.\n"));
872 return 1;
875 void sb_dsp_disable_midi(int io_base)
879 void sb_dsp_disable_recording(int io_base)
883 /* if (sbmpu) below we allow mpu401 to manage the midi devs
884 otherwise we have to unload them. (Andrzej Krzysztofowicz) */
886 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
888 sb_devc *devc;
890 devc = audio_devs[hw_config->slots[0]]->devc;
892 if (devc && devc->base == hw_config->io_base)
894 if ((devc->model & MDL_ESS) && devc->pcibase)
895 release_region(devc->pcibase, 8);
897 release_region(devc->base, 16);
899 if (!(devc->caps & SB_NO_AUDIO))
901 sound_free_dma(devc->dma8);
902 if (devc->dma16 >= 0)
903 sound_free_dma(devc->dma16);
905 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
907 if (devc->irq > 0)
908 free_irq(devc->irq, devc);
910 sb_mixer_unload(devc);
911 /* We don't have to do this bit any more the UART401 is its own
912 master -- Krzysztof Halasa */
913 /* But we have to do it, if UART401 is not detected */
914 if (!sbmpu)
915 sound_unload_mididev(devc->my_mididev);
916 sound_unload_audiodev(devc->dev);
918 kfree(devc);
920 else
921 release_region(hw_config->io_base, 16);
922 if(detected_devc)
923 kfree(detected_devc);
927 * Mixer access routines
929 * ES1887 modifications: some mixer registers reside in the
930 * range above 0xa0. These must be accessed in another way.
933 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
935 unsigned long flags;
937 if (devc->model == MDL_ESS) return ess_setmixer (devc, port, value);
939 save_flags(flags);
940 cli();
942 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
943 udelay(20);
944 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
945 udelay(20);
947 restore_flags(flags);
950 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
952 unsigned int val;
953 unsigned long flags;
955 if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
957 save_flags(flags);
958 cli();
960 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
961 udelay(20);
962 val = inb(MIXER_DATA);
963 udelay(20);
965 restore_flags(flags);
967 return val;
970 void sb_chgmixer
971 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
973 int value;
975 value = sb_getmixer(devc, reg);
976 value = (value & ~mask) | (val & mask);
977 sb_setmixer(devc, reg, value);
981 * MPU401 MIDI initialization.
984 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
986 unsigned long flags;
988 save_flags(flags);
989 cli();
991 outb((addr & 0xff), base + 1); /* Low address bits */
992 outb((addr >> 8), base + 2); /* High address bits */
993 outb((val), base); /* Data */
995 restore_flags(flags);
998 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
1000 unsigned long flags;
1001 unsigned char val;
1003 save_flags(flags);
1004 cli();
1006 outb((addr & 0xff), base + 1); /* Low address bits */
1007 outb((addr >> 8), base + 2); /* High address bits */
1008 val = inb(base); /* Data */
1010 restore_flags(flags);
1011 return val;
1014 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
1016 int mpu_base = hw_config->io_base;
1017 int mp_base = mpu_base + 4; /* Microcontroller base */
1018 int i;
1019 unsigned char control;
1023 * Reset the microcontroller so that the RAM can be accessed
1026 control = inb(mpu_base + 7);
1027 outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
1028 outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
1030 mdelay(3); /* Wait at least 1ms */
1032 outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
1035 * Detect microcontroller by probing the 8k RAM area
1037 smw_putmem(devc, mp_base, 0, 0x00);
1038 smw_putmem(devc, mp_base, 1, 0xff);
1039 udelay(10);
1041 if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
1043 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
1044 return 0; /* No RAM */
1047 * There is RAM so assume it's really a SM Wave
1050 devc->model = MDL_SMW;
1051 smw_mixer_init(devc);
1053 #ifdef MODULE
1054 if (!smw_ucode)
1056 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1057 smw_free = smw_ucode;
1059 #endif
1060 if (smw_ucodeLen > 0)
1062 if (smw_ucodeLen != 8192)
1064 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1065 return 1;
1068 * Download microcode
1071 for (i = 0; i < 8192; i++)
1072 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1075 * Verify microcode
1078 for (i = 0; i < 8192; i++)
1079 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1081 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1082 return 0;
1085 control = 0;
1086 #ifdef SMW_SCSI_IRQ
1088 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1089 * is disabled by default.
1091 * FIXME - make this a module option
1093 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1096 static unsigned char scsi_irq_bits[] = {
1097 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1099 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1101 #endif
1103 #ifdef SMW_OPL4_ENABLE
1105 * Make the OPL4 chip visible on the PC bus at 0x380.
1107 * There is no need to enable this feature since this driver
1108 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1109 * enabling OPL4 is pretty useless.
1111 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1112 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1113 #endif
1114 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1115 hw_config->name = "SoundMan Wave";
1116 return 1;
1119 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1121 int mpu_base = hw_config->io_base;
1122 int sb_base = devc->base;
1123 int irq = hw_config->irq;
1125 unsigned char bits = 0;
1126 unsigned long flags;
1128 if (irq < 0)
1129 irq *= -1;
1131 if (irq < 1 || irq > 15 ||
1132 jazz_irq_bits[irq] == 0)
1134 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1135 return 0;
1137 switch (sb_base)
1139 case 0x220:
1140 bits = 1;
1141 break;
1142 case 0x240:
1143 bits = 2;
1144 break;
1145 case 0x260:
1146 bits = 3;
1147 break;
1148 default:
1149 return 0;
1151 bits = jazz16_bits = bits << 5;
1152 switch (mpu_base)
1154 case 0x310:
1155 bits |= 1;
1156 break;
1157 case 0x320:
1158 bits |= 2;
1159 break;
1160 case 0x330:
1161 bits |= 3;
1162 break;
1163 default:
1164 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1165 return 0;
1168 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1170 save_flags(flags);
1171 cli();
1172 outb(0xAF, 0x201);
1173 outb(0x50, 0x201);
1174 outb(bits, 0x201);
1175 restore_flags(flags);
1177 hw_config->name = "Jazz16";
1178 smw_midi_init(devc, hw_config);
1180 if (!sb_dsp_command(devc, 0xfb))
1181 return 0;
1183 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1184 (jazz_dma_bits[devc->dma16] << 4)))
1185 return 0;
1187 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1188 (jazz_irq_bits[irq] << 4)))
1189 return 0;
1191 return 1;
1194 void attach_sbmpu(struct address_info *hw_config)
1196 if (last_sb->model == MDL_ESS) {
1197 #if defined(CONFIG_SOUND_MPU401)
1198 attach_mpu401(hw_config);
1199 if (last_sb->irq == -hw_config->irq) {
1200 last_sb->midi_irq_cookie=(void *)hw_config->slots[1];
1202 #endif
1203 return;
1205 attach_uart401(hw_config);
1206 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1209 int probe_sbmpu(struct address_info *hw_config)
1211 sb_devc *devc = last_devc;
1213 if (last_devc == NULL)
1214 return 0;
1216 last_devc = 0;
1218 if (hw_config->io_base <= 0)
1220 /* The real vibra16 is fine about this, but we have to go
1221 wipe up after Cyrix again */
1223 if(devc->model == MDL_SB16 && devc->minor >= 12)
1225 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
1226 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
1228 return 0;
1231 #if defined(CONFIG_SOUND_MPU401)
1232 if (devc->model == MDL_ESS)
1234 if (check_region(hw_config->io_base, 2))
1236 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1237 return 0;
1239 if (!ess_midi_init(devc, hw_config))
1240 return 0;
1241 hw_config->name = "ESS1xxx MPU";
1242 devc->midi_irq_cookie = -1;
1243 return probe_mpu401(hw_config);
1245 #endif
1247 if (check_region(hw_config->io_base, 4))
1249 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1250 return 0;
1252 switch (devc->model)
1254 case MDL_SB16:
1255 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1257 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1258 return 0;
1260 hw_config->name = "Sound Blaster 16";
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);