Import 2.1.118
[davej-history.git] / drivers / sound / sb_common.c
blob04b3d785414b6d41e9511c24e113380be6826400
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 #include <linux/config.h>
15 #include <linux/delay.h>
16 #include <asm/init.h>
18 #include "sound_config.h"
19 #include "sound_firmware.h"
21 #ifdef CONFIG_SBDSP
23 #ifndef CONFIG_AUDIO
24 #error You will need to configure the sound driver with CONFIG_AUDIO option.
25 #endif
27 #include "sb_mixer.h"
28 #include "sb.h"
30 static sb_devc *detected_devc = NULL; /* For communication from probe to init */
31 static sb_devc *last_devc = NULL; /* For MPU401 initialization */
33 static unsigned char jazz_irq_bits[] = {
34 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
37 static unsigned char jazz_dma_bits[] = {
38 0, 1, 0, 2, 0, 3, 0, 4
42 * Jazz16 chipset specific control variables
45 static int jazz16_base = 0; /* Not detected */
46 static unsigned char jazz16_bits = 0; /* I/O relocation bits */
49 * Logitech Soundman Wave specific initialization code
52 #ifdef SMW_MIDI0001_INCLUDED
53 #include "smw-midi0001.h"
54 #else
55 static unsigned char *smw_ucode = NULL;
56 static int smw_ucodeLen = 0;
58 #endif
60 sb_devc *last_sb = NULL; /* Last sb loaded */
62 int sb_dsp_command(sb_devc * devc, unsigned char val)
64 int i;
65 unsigned long limit;
67 limit = jiffies + HZ / 10; /* Timeout */
70 * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
71 * called while interrupts are disabled. This means that the timer is
72 * disabled also. However the timeout situation is a abnormal condition.
73 * Normally the DSP should be ready to accept commands after just couple of
74 * loops.
77 for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
79 if ((inb(DSP_STATUS) & 0x80) == 0)
81 outb((val), DSP_COMMAND);
82 return 1;
85 printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
86 return 0;
89 static int sb_dsp_get_byte(sb_devc * devc)
91 int i;
93 for (i = 1000; i; i--)
95 if (inb(DSP_DATA_AVAIL) & 0x80)
96 return inb(DSP_READ);
98 return 0xffff;
101 int ess_write(sb_devc * devc, unsigned char reg, unsigned char data)
103 /* Write a byte to an extended mode register of ES1688 */
105 if (!sb_dsp_command(devc, reg))
106 return 0;
108 return sb_dsp_command(devc, data);
111 int ess_read(sb_devc * devc, unsigned char reg)
113 /* Read a byte from an extended mode register of ES1688 */
114 if (!sb_dsp_command(devc, 0xc0)) /* Read register command */
115 return -1;
117 if (!sb_dsp_command(devc, reg))
118 return -1;
120 return sb_dsp_get_byte(devc);
123 static void sbintr(int irq, void *dev_id, struct pt_regs *dummy)
125 int status;
126 unsigned char src = 0xff;
128 sb_devc *devc = dev_id;
130 devc->irq_ok = 1;
131 if (devc->model == MDL_SB16)
133 src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
135 #if defined(CONFIG_MIDI)&& defined(CONFIG_UART401)
136 if (src & 4)
137 uart401intr(devc->irq, devc->midi_irq_cookie, NULL); /* MPU401 interrupt */
138 #endif
140 if (!(src & 3))
141 return; /* Not a DSP interrupt */
143 if (devc->intr_active)
145 switch (devc->irq_mode)
147 case IMODE_OUTPUT:
148 DMAbuf_outputintr(devc->dev, 1);
149 break;
151 case IMODE_INPUT:
152 DMAbuf_inputintr(devc->dev);
153 break;
155 case IMODE_INIT:
156 break;
158 case IMODE_MIDI:
159 #ifdef CONFIG_MIDI
160 sb_midi_interrupt(devc);
161 #endif
162 break;
164 default:
165 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
170 * Acknowledge interrupts
173 if (src & 0x01)
174 status = inb(DSP_DATA_AVAIL);
176 if (devc->model == MDL_SB16 && src & 0x02)
177 status = inb(DSP_DATA_AVL16);
181 int sb_dsp_reset(sb_devc * devc)
183 int loopc;
185 DEB(printk("Entered sb_dsp_reset()\n"));
187 if (devc->model == MDL_ESS)
188 outb(3, DSP_RESET); /* Reset FIFO too */
189 else
190 outb(1, DSP_RESET);
192 udelay(10);
193 outb(0, DSP_RESET);
194 udelay(30);
196 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
198 if (inb(DSP_READ) != 0xAA)
200 DDB(printk("sb: No response to RESET\n"));
201 return 0; /* Sorry */
203 if (devc->model == MDL_ESS)
204 sb_dsp_command(devc, 0xc6); /* Enable extended mode */
206 DEB(printk("sb_dsp_reset() OK\n"));
207 return 1;
210 static void dsp_get_vers(sb_devc * devc)
212 int i;
214 unsigned long flags;
216 DDB(printk("Entered dsp_get_vers()\n"));
217 save_flags(flags);
218 cli();
219 devc->major = devc->minor = 0;
220 sb_dsp_command(devc, 0xe1); /* Get version */
222 for (i = 100000; i; i--)
224 if (inb(DSP_DATA_AVAIL) & 0x80)
226 if (devc->major == 0)
227 devc->major = inb(DSP_READ);
228 else
230 devc->minor = inb(DSP_READ);
231 break;
235 DDB(printk("DSP version %d.%d\n", devc->major, devc->minor));
236 restore_flags(flags);
239 static int sb16_set_dma_hw(sb_devc * devc)
241 int bits;
243 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
245 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
246 return 0;
248 bits = (1 << devc->dma8);
250 if (devc->dma16 >= 5 && devc->dma16 <= 7)
251 bits |= (1 << devc->dma16);
253 sb_setmixer(devc, DMA_NR, bits);
254 return 1;
257 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
258 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
261 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
263 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
265 switch (hw_config->io_base)
267 case 0x300:
268 sb_setmixer(devc, 0x84, bits | 0x04);
269 break;
271 case 0x330:
272 sb_setmixer(devc, 0x84, bits | 0x00);
273 break;
275 default:
276 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
277 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
280 #endif
282 static int sb16_set_irq_hw(sb_devc * devc, int level)
284 int ival;
286 switch (level)
288 case 5:
289 ival = 2;
290 break;
291 case 7:
292 ival = 4;
293 break;
294 case 9:
295 ival = 1;
296 break;
297 case 10:
298 ival = 8;
299 break;
300 default:
301 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
302 return 0;
304 sb_setmixer(devc, IRQ_NR, ival);
305 return 1;
308 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
310 unsigned char bits = 0;
311 unsigned long flags;
313 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
314 return;
316 switch (hw_config->io_base)
318 case 0x220:
319 bits = 1;
320 break;
321 case 0x240:
322 bits = 2;
323 break;
324 case 0x260:
325 bits = 3;
326 break;
327 default:
328 return;
330 bits = jazz16_bits = bits << 5;
331 jazz16_base = hw_config->io_base;
334 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
336 save_flags(flags);
337 cli();
338 outb((0xAF), 0x201);
339 outb((0x50), 0x201);
340 outb((bits), 0x201);
341 restore_flags(flags);
344 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
346 char name[100];
348 * First try to check that the card has Jazz16 chip. It identifies itself
349 * by returning 0x12 as response to DSP command 0xfa.
352 if (!sb_dsp_command(devc, 0xfa))
353 return 0;
355 if (sb_dsp_get_byte(devc) != 0x12)
356 return 0;
359 * OK so far. Now configure the IRQ and DMA channel used by the card.
361 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
363 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
364 return 0;
366 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
368 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
369 return 0;
371 if (hw_config->dma2 < 0)
373 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
374 return 0;
376 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
378 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
379 return 0;
381 devc->dma16 = hw_config->dma2;
383 if (!sb_dsp_command(devc, 0xfb))
384 return 0;
386 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
387 (jazz_dma_bits[hw_config->dma2] << 4)))
388 return 0;
390 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
391 return 0;
394 * Now we have configured a standard Jazz16 device.
396 devc->model = MDL_JAZZ;
397 strcpy(name, "Jazz16");
399 hw_config->name = "Jazz16";
400 devc->caps |= SB_NO_MIDI;
401 return 1;
404 static void relocate_ess1688(sb_devc * devc)
406 unsigned char bits;
408 switch (devc->base)
410 case 0x220:
411 bits = 0x04;
412 break;
413 case 0x230:
414 bits = 0x05;
415 break;
416 case 0x240:
417 bits = 0x06;
418 break;
419 case 0x250:
420 bits = 0x07;
421 break;
422 default:
423 return; /* Wrong port */
426 DDB(printk("Doing ESS1688 address selection\n"));
429 * ES1688 supports two alternative ways for software address config.
430 * First try the so called Read-Sequence-Key method.
433 /* Reset the sequence logic */
434 inb(0x229);
435 inb(0x229);
436 inb(0x229);
438 /* Perform the read sequence */
439 inb(0x22b);
440 inb(0x229);
441 inb(0x22b);
442 inb(0x229);
443 inb(0x229);
444 inb(0x22b);
445 inb(0x229);
447 /* Select the base address by reading from it. Then probe using the port. */
448 inb(devc->base);
449 if (sb_dsp_reset(devc)) /* Bingo */
450 return;
452 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
454 * The last resort is the system control register method.
457 outb((0x00), 0xfb); /* 0xFB is the unlock register */
458 outb((0x00), 0xe0); /* Select index 0 */
459 outb((bits), 0xe1); /* Write the config bits */
460 outb((0x00), 0xf9); /* 0xFB is the lock register */
461 #endif
464 static int ess_init(sb_devc * devc, struct address_info *hw_config)
466 unsigned char cfg, irq_bits = 0, dma_bits = 0;
467 int ess_major = 0, ess_minor = 0;
468 int i;
469 static char name[100];
472 * Try to detect ESS chips.
475 sb_dsp_command(devc, 0xe7); /* Return identification */
477 for (i = 1000; i; i--)
479 if (inb(DSP_DATA_AVAIL) & 0x80)
481 if (ess_major == 0)
482 ess_major = inb(DSP_READ);
483 else
485 ess_minor = inb(DSP_READ);
486 break;
491 if (ess_major == 0)
492 return 0;
494 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
496 sprintf(name, "ESS ES488 AudioDrive (rev %d)",
497 ess_minor & 0x0f);
498 hw_config->name = name;
499 devc->model = MDL_SBPRO;
500 return 1;
502 else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
504 char *chip = "ES688";
506 if ((ess_minor & 0x0f) >= 8)
507 chip = "ES1688";
509 sprintf(name,"ESS %s AudioDrive (rev %d)",
510 chip, ess_minor & 0x0f);
512 else
513 strcpy(name, "Jazz16");
515 devc->model = MDL_ESS;
516 devc->submodel = ess_minor & 0x0f;
517 hw_config->name = name;
518 sb_dsp_reset(devc); /* Turn on extended mode */
521 * Set IRQ configuration register
524 cfg = 0x50; /* Enable only DMA counter interrupt */
526 switch (devc->irq)
528 case 2:
529 case 9:
530 irq_bits = 0;
531 break;
533 case 5:
534 irq_bits = 1;
535 break;
537 case 7:
538 irq_bits = 2;
539 break;
541 case 10:
542 irq_bits = 3;
543 break;
545 default:
546 irq_bits = 0;
547 cfg = 0x10; /* Disable all interrupts */
548 printk(KERN_ERR "ESS1688: Invalid IRQ %d\n", devc->irq);
549 return 0;
552 if (!ess_write(devc, 0xb1, cfg | (irq_bits << 2)))
553 printk(KERN_ERR "ESS1688: Failed to write to IRQ config register\n");
556 * Set DMA configuration register
559 cfg = 0x50; /* Extended mode DMA enable */
561 if (devc->dma8 > 3 || devc->dma8 < 0 || devc->dma8 == 2)
563 dma_bits = 0;
564 cfg = 0x00; /* Disable all DMA */
565 printk(KERN_ERR "ESS1688: Invalid DMA %d\n", devc->dma8);
567 else
569 if (devc->dma8 == 3)
570 dma_bits = 3;
571 else
572 dma_bits = devc->dma8 + 1;
575 if (!ess_write(devc, 0xb2, cfg | (dma_bits << 2)))
576 printk(KERN_ERR "ESS1688: Failed to write to DMA config register\n");
579 * Enable joystick and OPL3
582 cfg = sb_getmixer(devc, 0x40);
583 sb_setmixer(devc, 0x40, cfg | 0x03);
584 if (devc->submodel >= 8) /* ES1688 */
585 devc->caps |= SB_NO_MIDI; /* ES1688 uses MPU401 MIDI mode */
586 sb_dsp_reset(devc);
587 return 1;
590 int sb_dsp_detect(struct address_info *hw_config)
592 sb_devc sb_info;
593 sb_devc *devc = &sb_info;
595 sb_info.my_mididev = -1;
596 sb_info.my_mixerdev = -1;
597 sb_info.my_dev = -1;
600 * Initialize variables
603 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
604 if (check_region(hw_config->io_base, 16))
606 #ifdef MODULE
607 printk(KERN_INFO "sb: I/O region in use.\n");
608 #endif
609 return 0;
611 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
613 devc->type = hw_config->card_subtype;
615 devc->base = hw_config->io_base;
616 devc->irq = hw_config->irq;
617 devc->dma8 = hw_config->dma;
619 devc->dma16 = -1;
621 if (acer)
623 cli();
624 inb(devc->base + 0x09);
625 inb(devc->base + 0x09);
626 inb(devc->base + 0x09);
627 inb(devc->base + 0x0b);
628 inb(devc->base + 0x09);
629 inb(devc->base + 0x0b);
630 inb(devc->base + 0x09);
631 inb(devc->base + 0x09);
632 inb(devc->base + 0x0b);
633 inb(devc->base + 0x09);
634 inb(devc->base + 0x00);
635 sti();
638 * Detect the device
641 if (sb_dsp_reset(devc))
642 dsp_get_vers(devc);
643 else
644 devc->major = 0;
646 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
647 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
648 relocate_Jazz16(devc, hw_config);
650 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
651 relocate_ess1688(devc);
653 if (!sb_dsp_reset(devc))
655 DDB(printk("SB reset failed\n"));
656 #ifdef MODULE
657 printk(KERN_INFO "sb: dsp reset failed.\n");
658 #endif
659 return 0;
661 if (devc->major == 0)
662 dsp_get_vers(devc);
664 if (devc->major == 3 && devc->minor == 1)
666 if (devc->type == MDL_AZTECH) /* SG Washington? */
668 if (sb_dsp_command(devc, 0x09))
669 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
671 int i;
673 /* Have some delay */
674 for (i = 0; i < 10000; i++)
675 inb(DSP_DATA_AVAIL);
676 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
677 devc->model = MDL_AZTECH;
682 * Save device information for sb_dsp_init()
686 detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
687 if (detected_devc == NULL)
689 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
690 return 0;
692 memcpy((char *) detected_devc, (char *) devc, sizeof(sb_devc));
693 MDB(printk("SB %d.%d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
694 return 1;
697 int sb_dsp_init(struct address_info *hw_config)
699 sb_devc *devc;
700 char name[100];
701 extern int sb_be_quiet;
702 int mixer22, mixer30;
705 * Check if we had detected a SB device earlier
707 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
708 name[0] = 0;
710 if (detected_devc == NULL)
712 MDB(printk("No detected device\n"));
713 return 0;
715 devc = detected_devc;
716 detected_devc = NULL;
718 if (devc->base != hw_config->io_base)
720 DDB(printk("I/O port mismatch\n"));
721 return 0;
724 * Now continue initialization of the device
727 devc->caps = hw_config->driver_use_1;
729 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI) && hw_config->irq > 0)
730 { /* IRQ setup */
731 if (request_irq(hw_config->irq, sbintr, 0, "soundblaster", devc) < 0)
733 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
734 return 0;
736 devc->irq_ok = 0;
738 if (devc->major == 4)
739 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
741 free_irq(devc->irq, devc);
742 return 0;
744 if ((devc->type == 0 || devc->type == MDL_ESS) &&
745 devc->major == 3 && devc->minor == 1)
746 { /* Handle various chipsets which claim they are SB Pro compatible */
747 if ((devc->type != 0 && devc->type != MDL_ESS) ||
748 !ess_init(devc, hw_config))
750 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
751 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
753 DDB(printk("This is a genuine SB Pro\n"));
757 #if defined(__SMP__)
758 /* Skip IRQ detection if SMP (doesn't work) */
759 devc->irq_ok = 1;
760 #else
761 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
762 devc->irq_ok = 1;
763 else
765 int n;
767 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
769 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
771 int i;
773 for (i = 0; !devc->irq_ok && i < 10000; i++);
776 if (!devc->irq_ok)
777 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
778 else
780 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
783 #endif /* __SMP__ */
784 } /* IRQ setup */
785 request_region(hw_config->io_base, 16, "soundblaster");
787 last_sb = devc;
789 switch (devc->major)
791 case 1: /* SB 1.0 or 1.5 */
792 devc->model = hw_config->card_subtype = MDL_SB1;
793 break;
795 case 2: /* SB 2.x */
796 if (devc->minor == 0)
797 devc->model = hw_config->card_subtype = MDL_SB2;
798 else
799 devc->model = hw_config->card_subtype = MDL_SB201;
800 break;
802 case 3: /* SB Pro and most clones */
803 if (devc->model == 0)
805 devc->model = hw_config->card_subtype = MDL_SBPRO;
806 if (hw_config->name == NULL)
807 hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
809 break;
811 case 4:
812 devc->model = hw_config->card_subtype = MDL_SB16;
814 * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
815 * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
816 * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
817 * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
818 * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
820 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
822 mixer30 = sb_getmixer(devc,0x30);
823 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
824 sb_setmixer(devc,0x30,0xff);
825 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
826 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
827 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
829 if (hw_config->name == NULL)
830 hw_config->name = "Sound Blaster 16 (ALS-100)";
832 else
834 sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
835 sb_setmixer(devc,0x4c,0x1f);
836 sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
837 devc->submodel = SUBMDL_ALS007;
838 if (hw_config->name == NULL)
839 hw_config->name = "Sound Blaster 16 (ALS-007)";
841 sb_setmixer(devc,0x30,mixer30);
843 else if (hw_config->name == NULL)
844 hw_config->name = "Sound Blaster 16";
846 if (hw_config->dma2 == -1)
847 devc->dma16 = devc->dma8;
848 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
850 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
851 devc->dma16 = devc->dma8;
853 else
854 devc->dma16 = hw_config->dma2;
856 if(!sb16_set_dma_hw(devc)) {
857 free_irq(devc->irq, devc);
858 return 0;
861 devc->caps |= SB_NO_MIDI;
864 if (!(devc->caps & SB_NO_MIXER))
865 if (devc->major == 3 || devc->major == 4)
866 sb_mixer_init(devc);
868 #ifdef CONFIG_MIDI
869 if (!(devc->caps & SB_NO_MIDI))
870 sb_dsp_midi_init(devc);
871 #endif
873 if (hw_config->name == NULL)
874 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
876 sprintf(name, "%s (%d.%d)", hw_config->name, devc->major, devc->minor);
877 conf_printf(name, hw_config);
880 * Assuming that a sound card is Sound Blaster (compatible) is the most common
881 * configuration error and the mother of all problems. Usually sound cards
882 * emulate SB Pro but in addition they have a 16 bit native mode which should be
883 * used in Unix. See Readme.cards for more information about configuring OSS/Free
884 * properly.
886 if (devc->model <= MDL_SBPRO)
888 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
890 printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
891 printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
892 printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
893 printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
895 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
897 printk(KERN_INFO "SB DSP version is just %d.%d which means that your card is\n", devc->major, devc->minor);
898 printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
899 printk(KERN_INFO "is incorrectly configured.\n");
902 hw_config->card_subtype = devc->model;
903 hw_config->slots[0]=devc->dev;
904 last_devc = devc; /* For SB MPU detection */
906 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
908 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
910 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
912 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
914 if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
915 printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
917 sb_audio_init(devc, name);
918 hw_config->slots[0]=devc->dev;
920 else
922 MDB(printk("Sound Blaster: no audio devices found.\n"));
924 return 1;
927 void sb_dsp_disable_midi(int io_base)
931 void sb_dsp_disable_recording(int io_base)
935 void sb_dsp_unload(struct address_info *hw_config)
937 sb_devc *devc;
939 devc = audio_devs[hw_config->slots[0]]->devc;
941 if (devc && devc->base == hw_config->io_base)
943 release_region(devc->base, 16);
945 if (!(devc->caps & SB_NO_AUDIO))
947 sound_free_dma(devc->dma8);
948 if (devc->dma16 >= 0)
949 sound_free_dma(devc->dma16);
951 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI) && devc->irq > 0)
953 free_irq(devc->irq, devc);
954 sound_unload_mixerdev(devc->my_mixerdev);
955 /* We don't have to do this bit any more the UART401 is its own
956 master -- Krzysztof Halasa */
957 /* sound_unload_mididev(devc->my_mididev); */
958 sound_unload_audiodev(devc->my_dev);
960 kfree(devc);
962 else
963 release_region(hw_config->io_base, 16);
964 if(detected_devc)
965 kfree(detected_devc);
969 * Mixer access routines
972 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
974 unsigned long flags;
976 save_flags(flags);
977 cli();
978 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
980 udelay(20);
981 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
982 udelay(20);
983 restore_flags(flags);
986 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
988 unsigned int val;
989 unsigned long flags;
991 save_flags(flags);
992 cli();
993 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
995 udelay(20);
996 val = inb(MIXER_DATA);
997 udelay(20);
998 restore_flags(flags);
1000 return val;
1003 #ifdef CONFIG_MIDI
1006 * MPU401 MIDI initialization.
1009 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
1011 unsigned long flags;
1013 save_flags(flags);
1014 cli();
1016 outb((addr & 0xff), base + 1); /* Low address bits */
1017 outb((addr >> 8), base + 2); /* High address bits */
1018 outb((val), base); /* Data */
1020 restore_flags(flags);
1023 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
1025 unsigned long flags;
1026 unsigned char val;
1028 save_flags(flags);
1029 cli();
1031 outb((addr & 0xff), base + 1); /* Low address bits */
1032 outb((addr >> 8), base + 2); /* High address bits */
1033 val = inb(base); /* Data */
1035 restore_flags(flags);
1036 return val;
1039 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
1041 int mpu_base = hw_config->io_base;
1042 int mp_base = mpu_base + 4; /* Microcontroller base */
1043 int i;
1044 unsigned char control;
1048 * Reset the microcontroller so that the RAM can be accessed
1051 control = inb(mpu_base + 7);
1052 outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
1053 outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
1055 mdelay(3); /* Wait at least 1ms */
1057 outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
1060 * Detect microcontroller by probing the 8k RAM area
1062 smw_putmem(devc, mp_base, 0, 0x00);
1063 smw_putmem(devc, mp_base, 1, 0xff);
1064 udelay(10);
1066 if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
1068 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
1069 return 0; /* No RAM */
1072 * There is RAM so assume it's really a SM Wave
1075 devc->model = MDL_SMW;
1076 smw_mixer_init(devc);
1078 #ifdef MODULE
1079 if (!smw_ucode)
1081 extern void *smw_free;
1083 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1084 smw_free = smw_ucode;
1086 #endif
1087 if (smw_ucodeLen > 0)
1089 if (smw_ucodeLen != 8192)
1091 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1092 return 1;
1095 * Download microcode
1098 for (i = 0; i < 8192; i++)
1099 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1102 * Verify microcode
1105 for (i = 0; i < 8192; i++)
1106 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1108 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1109 return 0;
1112 control = 0;
1113 #ifdef SMW_SCSI_IRQ
1115 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1116 * is disabled by default.
1118 * FIXME - make this a module option
1120 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1123 static unsigned char scsi_irq_bits[] = {
1124 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1126 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1128 #endif
1130 #ifdef SMW_OPL4_ENABLE
1132 * Make the OPL4 chip visible on the PC bus at 0x380.
1134 * There is no need to enable this feature since this driver
1135 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1136 * enabling OPL4 is pretty useless.
1138 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1139 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1140 #endif
1141 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1142 hw_config->name = "SoundMan Wave";
1143 return 1;
1146 static int ess_midi_init(sb_devc * devc, struct address_info *hw_config)
1148 unsigned char cfg, tmp;
1150 cfg = sb_getmixer(devc, 0x40) & 0x03;
1152 if (devc->submodel < 8)
1154 sb_setmixer(devc, 0x40, cfg | 0x03); /* Enable OPL3 & joystick */
1155 return 0; /* ES688 doesn't support MPU401 mode */
1157 tmp = (hw_config->io_base & 0x0f0) >> 4;
1159 if (tmp > 3)
1161 sb_setmixer(devc, 0x40, cfg);
1162 return 0;
1164 cfg |= tmp << 3;
1166 tmp = 1; /* MPU enabled without interrupts */
1168 /* May be shared: if so the value is -ve */
1170 switch(abs(hw_config->irq))
1172 case 9:
1173 tmp = 0x4;
1174 break;
1175 case 5:
1176 tmp = 0x5;
1177 break;
1178 case 7:
1179 tmp = 0x6;
1180 break;
1181 case 10:
1182 tmp = 0x7;
1183 break;
1184 default:
1185 return 0;
1188 cfg |= tmp << 5;
1189 sb_setmixer(devc, 0x40, cfg | 0x03);
1190 return 1;
1193 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1195 int mpu_base = hw_config->io_base;
1196 int sb_base = devc->base;
1197 int irq = hw_config->irq;
1199 unsigned char bits = 0;
1200 unsigned long flags;
1202 if (irq < 0)
1203 irq *= -1;
1205 if (irq < 1 || irq > 15 ||
1206 jazz_irq_bits[irq] == 0)
1208 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1209 return 0;
1211 switch (sb_base)
1213 case 0x220:
1214 bits = 1;
1215 break;
1216 case 0x240:
1217 bits = 2;
1218 break;
1219 case 0x260:
1220 bits = 3;
1221 break;
1222 default:
1223 return 0;
1225 bits = jazz16_bits = bits << 5;
1226 switch (mpu_base)
1228 case 0x310:
1229 bits |= 1;
1230 break;
1231 case 0x320:
1232 bits |= 2;
1233 break;
1234 case 0x330:
1235 bits |= 3;
1236 break;
1237 default:
1238 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1239 return 0;
1242 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1244 save_flags(flags);
1245 cli();
1246 outb(0xAF, 0x201);
1247 outb(0x50, 0x201);
1248 outb(bits, 0x201);
1249 restore_flags(flags);
1251 hw_config->name = "Jazz16";
1252 smw_midi_init(devc, hw_config);
1254 if (!sb_dsp_command(devc, 0xfb))
1255 return 0;
1257 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1258 (jazz_dma_bits[devc->dma16] << 4)))
1259 return 0;
1261 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1262 (jazz_irq_bits[irq] << 4)))
1263 return 0;
1265 return 1;
1268 void attach_sbmpu(struct address_info *hw_config)
1270 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1271 attach_uart401(hw_config);
1272 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1273 #endif
1276 int probe_sbmpu(struct address_info *hw_config)
1278 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1279 sb_devc *devc = last_devc;
1281 if (last_devc == NULL)
1282 return 0;
1284 last_devc = 0;
1286 if (hw_config->io_base <= 0)
1287 return 0;
1289 if (check_region(hw_config->io_base, 4))
1291 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1292 return 0;
1294 switch (devc->model)
1296 case MDL_SB16:
1297 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1299 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1300 return 0;
1302 hw_config->name = "Sound Blaster 16";
1303 hw_config->irq = -devc->irq;
1304 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1305 if (devc->minor > 12) /* What is Vibra's version??? */
1306 sb16_set_mpu_port(devc, hw_config);
1307 #endif
1308 break;
1310 case MDL_ESS:
1311 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1312 hw_config->irq = -devc->irq;
1313 if (!ess_midi_init(devc, hw_config))
1314 return 0;
1315 hw_config->name = "ESS ES1688";
1316 break;
1318 case MDL_JAZZ:
1319 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1320 hw_config->irq = -devc->irq;
1321 if (!init_Jazz16_midi(devc, hw_config))
1322 return 0;
1323 break;
1325 default:
1326 return 0;
1328 return probe_uart401(hw_config);
1329 #else
1330 return 0;
1331 #endif
1334 void unload_sbmpu(struct address_info *hw_config)
1336 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1337 unload_uart401(hw_config);
1338 #endif
1340 #else /* !CONFIG_MIDI */
1342 void unload_sbmpu(struct address_info *hw_config)
1346 int probe_sbmpu(struct address_info *hw_config)
1348 return 0;
1351 void attach_sbmpu(struct address_info *hw_config)
1354 #endif
1355 #endif