Import 2.3.15pre2
[davej-history.git] / drivers / sound / sb_common.c
blobe0335e348c8d17a85dce0a90faddbcfbf2b94c81
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 #include <linux/config.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
23 #include "sound_config.h"
24 #include "sound_firmware.h"
26 #ifdef CONFIG_SBDSP
28 #ifndef CONFIG_AUDIO
29 #error You will need to configure the sound driver with CONFIG_AUDIO option.
30 #endif
32 #include "sb_mixer.h"
33 #include "sb.h"
35 #include "sb_ess.h"
37 static sb_devc *detected_devc = NULL; /* For communication from probe to init */
38 static sb_devc *last_devc = NULL; /* For MPU401 initialization */
40 static unsigned char jazz_irq_bits[] = {
41 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
44 static unsigned char jazz_dma_bits[] = {
45 0, 1, 0, 2, 0, 3, 0, 4
49 * Jazz16 chipset specific control variables
52 static int jazz16_base = 0; /* Not detected */
53 static unsigned char jazz16_bits = 0; /* I/O relocation bits */
56 * Logitech Soundman Wave specific initialization code
59 #ifdef SMW_MIDI0001_INCLUDED
60 #include "smw-midi0001.h"
61 #else
62 static unsigned char *smw_ucode = NULL;
63 static int smw_ucodeLen = 0;
65 #endif
67 sb_devc *last_sb = NULL; /* Last sb loaded */
69 int sb_dsp_command(sb_devc * devc, unsigned char val)
71 int i;
72 unsigned long limit;
74 limit = jiffies + HZ / 10; /* Timeout */
77 * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
78 * called while interrupts are disabled. This means that the timer is
79 * disabled also. However the timeout situation is a abnormal condition.
80 * Normally the DSP should be ready to accept commands after just couple of
81 * loops.
84 for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
86 if ((inb(DSP_STATUS) & 0x80) == 0)
88 outb((val), DSP_COMMAND);
89 return 1;
92 printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
93 return 0;
96 int sb_dsp_get_byte(sb_devc * devc)
98 int i;
100 for (i = 1000; i; i--)
102 if (inb(DSP_DATA_AVAIL) & 0x80)
103 return inb(DSP_READ);
105 return 0xffff;
108 static void sb_intr (sb_devc *devc)
110 int status;
111 unsigned char src = 0xff;
113 if (devc->model == MDL_SB16)
115 src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
117 #if defined(CONFIG_MIDI)&& defined(CONFIG_UART401)
118 if (src & 4) /* MPU401 interrupt */
119 uart401intr(devc->irq, devc->midi_irq_cookie, NULL);
120 #endif
122 if (!(src & 3))
123 return; /* Not a DSP interrupt */
125 if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
127 switch (devc->irq_mode)
129 case IMODE_OUTPUT:
130 DMAbuf_outputintr(devc->dev, 1);
131 break;
133 case IMODE_INPUT:
134 DMAbuf_inputintr(devc->dev);
135 break;
137 case IMODE_INIT:
138 break;
140 case IMODE_MIDI:
141 #ifdef CONFIG_MIDI
142 sb_midi_interrupt(devc);
143 #endif
144 break;
146 default:
147 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
151 else if (devc->intr_active_16 && (src & 0x02))
153 switch (devc->irq_mode_16)
155 case IMODE_OUTPUT:
156 DMAbuf_outputintr(devc->dev, 1);
157 break;
159 case IMODE_INPUT:
160 DMAbuf_inputintr(devc->dev);
161 break;
163 case IMODE_INIT:
164 break;
166 default:
167 /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
172 * Acknowledge interrupts
175 if (src & 0x01)
176 status = inb(DSP_DATA_AVAIL);
178 if (devc->model == MDL_SB16 && src & 0x02)
179 status = inb(DSP_DATA_AVL16);
182 static void pci_intr(sb_devc *devc)
184 int src = inb(devc->pcibase+0x1A);
185 src&=3;
186 if(src)
187 sb_intr(devc);
190 static void sbintr(int irq, void *dev_id, struct pt_regs *dummy)
192 sb_devc *devc = dev_id;
194 devc->irq_ok = 1;
196 switch (devc->model) {
197 case MDL_ESSPCI:
198 pci_intr (devc);
199 break;
201 case MDL_ESS:
202 ess_intr (devc);
203 break;
204 default:
205 sb_intr (devc);
206 break;
210 int sb_dsp_reset(sb_devc * devc)
212 int loopc;
214 DEB(printk("Entered sb_dsp_reset()\n"));
216 if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
218 /* This is only for non-ESS chips */
220 outb(1, DSP_RESET);
222 udelay(10);
223 outb(0, DSP_RESET);
224 udelay(30);
226 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
228 if (inb(DSP_READ) != 0xAA)
230 DDB(printk("sb: No response to RESET\n"));
231 return 0; /* Sorry */
234 DEB(printk("sb_dsp_reset() OK\n"));
236 return 1;
239 static void dsp_get_vers(sb_devc * devc)
241 int i;
243 unsigned long flags;
245 DDB(printk("Entered dsp_get_vers()\n"));
246 save_flags(flags);
247 cli();
248 devc->major = devc->minor = 0;
249 sb_dsp_command(devc, 0xe1); /* Get version */
251 for (i = 100000; i; i--)
253 if (inb(DSP_DATA_AVAIL) & 0x80)
255 if (devc->major == 0)
256 devc->major = inb(DSP_READ);
257 else
259 devc->minor = inb(DSP_READ);
260 break;
264 DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
265 restore_flags(flags);
268 static int sb16_set_dma_hw(sb_devc * devc)
270 int bits;
272 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
274 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
275 return 0;
277 bits = (1 << devc->dma8);
279 if (devc->dma16 >= 5 && devc->dma16 <= 7)
280 bits |= (1 << devc->dma16);
282 sb_setmixer(devc, DMA_NR, bits);
283 return 1;
286 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
287 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
290 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
292 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
294 switch (hw_config->io_base)
296 case 0x300:
297 sb_setmixer(devc, 0x84, bits | 0x04);
298 break;
300 case 0x330:
301 sb_setmixer(devc, 0x84, bits | 0x00);
302 break;
304 default:
305 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
306 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
309 #endif
311 static int sb16_set_irq_hw(sb_devc * devc, int level)
313 int ival;
315 switch (level)
317 case 5:
318 ival = 2;
319 break;
320 case 7:
321 ival = 4;
322 break;
323 case 9:
324 ival = 1;
325 break;
326 case 10:
327 ival = 8;
328 break;
329 default:
330 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
331 return 0;
333 sb_setmixer(devc, IRQ_NR, ival);
334 return 1;
337 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
339 unsigned char bits = 0;
340 unsigned long flags;
342 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
343 return;
345 switch (hw_config->io_base)
347 case 0x220:
348 bits = 1;
349 break;
350 case 0x240:
351 bits = 2;
352 break;
353 case 0x260:
354 bits = 3;
355 break;
356 default:
357 return;
359 bits = jazz16_bits = bits << 5;
360 jazz16_base = hw_config->io_base;
363 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
365 save_flags(flags);
366 cli();
367 outb((0xAF), 0x201);
368 outb((0x50), 0x201);
369 outb((bits), 0x201);
370 restore_flags(flags);
373 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
375 char name[100];
377 * First try to check that the card has Jazz16 chip. It identifies itself
378 * by returning 0x12 as response to DSP command 0xfa.
381 if (!sb_dsp_command(devc, 0xfa))
382 return 0;
384 if (sb_dsp_get_byte(devc) != 0x12)
385 return 0;
388 * OK so far. Now configure the IRQ and DMA channel used by the card.
390 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
392 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
393 return 0;
395 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
397 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
398 return 0;
400 if (hw_config->dma2 < 0)
402 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
403 return 0;
405 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
407 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
408 return 0;
410 devc->dma16 = hw_config->dma2;
412 if (!sb_dsp_command(devc, 0xfb))
413 return 0;
415 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
416 (jazz_dma_bits[hw_config->dma2] << 4)))
417 return 0;
419 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
420 return 0;
423 * Now we have configured a standard Jazz16 device.
425 devc->model = MDL_JAZZ;
426 strcpy(name, "Jazz16");
428 hw_config->name = "Jazz16";
429 devc->caps |= SB_NO_MIDI;
430 return 1;
433 static void relocate_ess1688(sb_devc * devc)
435 unsigned char bits;
437 switch (devc->base)
439 case 0x220:
440 bits = 0x04;
441 break;
442 case 0x230:
443 bits = 0x05;
444 break;
445 case 0x240:
446 bits = 0x06;
447 break;
448 case 0x250:
449 bits = 0x07;
450 break;
451 default:
452 return; /* Wrong port */
455 DDB(printk("Doing ESS1688 address selection\n"));
458 * ES1688 supports two alternative ways for software address config.
459 * First try the so called Read-Sequence-Key method.
462 /* Reset the sequence logic */
463 inb(0x229);
464 inb(0x229);
465 inb(0x229);
467 /* Perform the read sequence */
468 inb(0x22b);
469 inb(0x229);
470 inb(0x22b);
471 inb(0x229);
472 inb(0x229);
473 inb(0x22b);
474 inb(0x229);
476 /* Select the base address by reading from it. Then probe using the port. */
477 inb(devc->base);
478 if (sb_dsp_reset(devc)) /* Bingo */
479 return;
481 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
483 * The last resort is the system control register method.
486 outb((0x00), 0xfb); /* 0xFB is the unlock register */
487 outb((0x00), 0xe0); /* Select index 0 */
488 outb((bits), 0xe1); /* Write the config bits */
489 outb((0x00), 0xf9); /* 0xFB is the lock register */
490 #endif
493 int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio)
495 sb_devc sb_info;
496 sb_devc *devc = &sb_info;
498 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
499 sb_info.my_mididev = -1;
500 sb_info.my_mixerdev = -1;
501 sb_info.dev = -1;
504 * Initialize variables
507 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
508 if (check_region(hw_config->io_base, 16))
510 #ifdef MODULE
511 printk(KERN_INFO "sb: I/O region in use.\n");
512 #endif
513 return 0;
516 devc->type = hw_config->card_subtype;
518 devc->base = hw_config->io_base;
519 devc->irq = hw_config->irq;
520 devc->dma8 = hw_config->dma;
522 devc->dma16 = -1;
523 devc->pcibase = pciio;
525 if(pci == SB_PCI_ESSMAESTRO)
527 devc->model = MDL_ESSPCI;
528 devc->caps |= SB_PCI_IRQ;
529 hw_config->driver_use_1 |= SB_PCI_IRQ;
530 hw_config->card_subtype = MDL_ESSPCI;
533 if(pci == SB_PCI_YAMAHA)
535 devc->model = MDL_YMPCI;
536 devc->caps |= SB_PCI_IRQ;
537 hw_config->driver_use_1 |= SB_PCI_IRQ;
538 hw_config->card_subtype = MDL_YMPCI;
540 printk("Yamaha PCI mode.\n");
543 if (acer)
545 cli();
546 inb(devc->base + 0x09);
547 inb(devc->base + 0x09);
548 inb(devc->base + 0x09);
549 inb(devc->base + 0x0b);
550 inb(devc->base + 0x09);
551 inb(devc->base + 0x0b);
552 inb(devc->base + 0x09);
553 inb(devc->base + 0x09);
554 inb(devc->base + 0x0b);
555 inb(devc->base + 0x09);
556 inb(devc->base + 0x00);
557 sti();
560 * Detect the device
563 if (sb_dsp_reset(devc))
564 dsp_get_vers(devc);
565 else
566 devc->major = 0;
568 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
569 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
570 relocate_Jazz16(devc, hw_config);
572 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
573 relocate_ess1688(devc);
575 if (!sb_dsp_reset(devc))
577 DDB(printk("SB reset failed\n"));
578 #ifdef MODULE
579 printk(KERN_INFO "sb: dsp reset failed.\n");
580 #endif
581 return 0;
583 if (devc->major == 0)
584 dsp_get_vers(devc);
586 if (devc->major == 3 && devc->minor == 1)
588 if (devc->type == MDL_AZTECH) /* SG Washington? */
590 if (sb_dsp_command(devc, 0x09))
591 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
593 int i;
595 /* Have some delay */
596 for (i = 0; i < 10000; i++)
597 inb(DSP_DATA_AVAIL);
598 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
599 devc->model = MDL_AZTECH;
604 if(devc->type == MDL_ESSPCI)
605 devc->model = MDL_ESSPCI;
607 if(devc->type == MDL_YMPCI)
609 printk("YMPCI selected\n");
610 devc->model = MDL_YMPCI;
614 * Save device information for sb_dsp_init()
618 detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
619 if (detected_devc == NULL)
621 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
622 return 0;
624 memcpy((char *) detected_devc, (char *) devc, sizeof(sb_devc));
625 MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
626 return 1;
629 int sb_dsp_init(struct address_info *hw_config)
631 sb_devc *devc;
632 char name[100];
633 extern int sb_be_quiet;
634 int mixer22, mixer30;
637 * Check if we had detected a SB device earlier
639 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
640 name[0] = 0;
642 if (detected_devc == NULL)
644 MDB(printk("No detected device\n"));
645 return 0;
647 devc = detected_devc;
648 detected_devc = NULL;
650 if (devc->base != hw_config->io_base)
652 DDB(printk("I/O port mismatch\n"));
653 return 0;
656 * Now continue initialization of the device
659 devc->caps = hw_config->driver_use_1;
661 if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
662 { /* IRQ setup */
665 * ESS PCI cards do shared PCI IRQ stuff. Since they
666 * will get shared PCI irq lines we must cope.
669 int i=(devc->caps&SB_PCI_IRQ)?SA_SHIRQ:0;
671 if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
673 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
674 return 0;
676 devc->irq_ok = 0;
678 if (devc->major == 4)
679 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
681 free_irq(devc->irq, devc);
682 return 0;
684 if ((devc->type == 0 || devc->type == MDL_ESS) &&
685 devc->major == 3 && devc->minor == 1)
686 { /* Handle various chipsets which claim they are SB Pro compatible */
687 if ((devc->type != 0 && devc->type != MDL_ESS) ||
688 !ess_init(devc, hw_config))
690 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
691 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
693 DDB(printk("This is a genuine SB Pro\n"));
697 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
698 devc->irq_ok = 1;
699 else
701 int n;
703 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
705 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
707 int i;
709 for (i = 0; !devc->irq_ok && i < 10000; i++);
712 if (!devc->irq_ok)
713 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
714 else
716 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
719 } /* IRQ setup */
720 request_region(hw_config->io_base, 16, "soundblaster");
722 last_sb = devc;
724 switch (devc->major)
726 case 1: /* SB 1.0 or 1.5 */
727 devc->model = hw_config->card_subtype = MDL_SB1;
728 break;
730 case 2: /* SB 2.x */
731 if (devc->minor == 0)
732 devc->model = hw_config->card_subtype = MDL_SB2;
733 else
734 devc->model = hw_config->card_subtype = MDL_SB201;
735 break;
737 case 3: /* SB Pro and most clones */
738 switch (devc->model) {
739 case 0:
740 devc->model = hw_config->card_subtype = MDL_SBPRO;
741 if (hw_config->name == NULL)
742 hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
743 break;
744 case MDL_ESS:
745 ess_dsp_init(devc, hw_config);
746 break;
748 break;
750 case 4:
751 devc->model = hw_config->card_subtype = MDL_SB16;
753 * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
754 * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
755 * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
756 * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
757 * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
759 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
761 mixer30 = sb_getmixer(devc,0x30);
762 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
763 sb_setmixer(devc,0x30,0xff);
764 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
765 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
766 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
768 devc->submodel = SUBMDL_ALS100;
769 if (hw_config->name == NULL)
770 hw_config->name = "Sound Blaster 16 (ALS-100)";
772 else
774 sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
775 sb_setmixer(devc,0x4c,0x1f);
776 sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
777 devc->submodel = SUBMDL_ALS007;
778 if (hw_config->name == NULL)
779 hw_config->name = "Sound Blaster 16 (ALS-007)";
781 sb_setmixer(devc,0x30,mixer30);
783 else if (hw_config->name == NULL)
784 hw_config->name = "Sound Blaster 16";
786 if (hw_config->dma2 == -1)
787 devc->dma16 = devc->dma8;
788 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
790 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
791 devc->dma16 = devc->dma8;
793 else
794 devc->dma16 = hw_config->dma2;
796 if(!sb16_set_dma_hw(devc)) {
797 free_irq(devc->irq, devc);
798 release_region(hw_config->io_base, 16);
799 return 0;
802 devc->caps |= SB_NO_MIDI;
805 if (!(devc->caps & SB_NO_MIXER))
806 if (devc->major == 3 || devc->major == 4)
807 sb_mixer_init(devc);
809 #ifdef CONFIG_MIDI
810 if (!(devc->caps & SB_NO_MIDI))
811 sb_dsp_midi_init(devc);
812 #endif
814 if (hw_config->name == NULL)
815 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
817 sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
818 conf_printf(name, hw_config);
821 * Assuming that a sound card is Sound Blaster (compatible) is the most common
822 * configuration error and the mother of all problems. Usually sound cards
823 * emulate SB Pro but in addition they have a 16 bit native mode which should be
824 * used in Unix. See Readme.cards for more information about configuring OSS/Free
825 * properly.
827 if (devc->model <= MDL_SBPRO)
829 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
831 printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
832 printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
833 printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
834 printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
836 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
838 printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
839 printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
840 printk(KERN_INFO "is incorrectly configured.\n");
843 hw_config->card_subtype = devc->model;
844 hw_config->slots[0]=devc->dev;
845 last_devc = devc; /* For SB MPU detection */
847 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
849 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
851 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
853 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
855 if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
856 printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
858 sb_audio_init(devc, name);
859 hw_config->slots[0]=devc->dev;
861 else
863 MDB(printk("Sound Blaster: no audio devices found.\n"));
865 return 1;
868 void sb_dsp_disable_midi(int io_base)
872 void sb_dsp_disable_recording(int io_base)
876 /* if (sbmpu) below we allow mpu401 to manage the midi devs
877 otherwise we have to unload them. (Andrzej Krzysztofowicz) */
879 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
881 sb_devc *devc;
883 devc = audio_devs[hw_config->slots[0]]->devc;
885 if (devc && devc->base == hw_config->io_base)
887 if ((devc->model & MDL_ESS) && devc->pcibase)
888 release_region(devc->pcibase, 8);
890 release_region(devc->base, 16);
892 if (!(devc->caps & SB_NO_AUDIO))
894 sound_free_dma(devc->dma8);
895 if (devc->dma16 >= 0)
896 sound_free_dma(devc->dma16);
898 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
900 if (devc->irq > 0);
901 free_irq(devc->irq, devc);
903 sound_unload_mixerdev(devc->my_mixerdev);
904 /* We don't have to do this bit any more the UART401 is its own
905 master -- Krzysztof Halasa */
906 /* But we have to do it, if UART401 is not detected */
907 if (!sbmpu)
908 sound_unload_mididev(devc->my_mididev);
909 sound_unload_audiodev(devc->dev);
911 kfree(devc);
913 else
914 release_region(hw_config->io_base, 16);
915 if(detected_devc)
916 kfree(detected_devc);
920 * Mixer access routines
922 * ES1887 modifications: some mixer registers reside in the
923 * range above 0xa0. These must be accessed in another way.
926 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
928 unsigned long flags;
930 if (devc->model == MDL_ESS) return ess_setmixer (devc, port, value);
932 save_flags(flags);
933 cli();
935 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
936 udelay(20);
937 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
938 udelay(20);
940 restore_flags(flags);
943 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
945 unsigned int val;
946 unsigned long flags;
948 if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
950 save_flags(flags);
951 cli();
953 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
954 udelay(20);
955 val = inb(MIXER_DATA);
956 udelay(20);
958 restore_flags(flags);
960 return val;
963 void sb_chgmixer
964 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
966 int value;
968 value = sb_getmixer(devc, reg);
969 value = (value & ~mask) | (val & mask);
970 sb_setmixer(devc, reg, value);
973 #ifdef CONFIG_MIDI
976 * MPU401 MIDI initialization.
979 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
981 unsigned long flags;
983 save_flags(flags);
984 cli();
986 outb((addr & 0xff), base + 1); /* Low address bits */
987 outb((addr >> 8), base + 2); /* High address bits */
988 outb((val), base); /* Data */
990 restore_flags(flags);
993 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
995 unsigned long flags;
996 unsigned char val;
998 save_flags(flags);
999 cli();
1001 outb((addr & 0xff), base + 1); /* Low address bits */
1002 outb((addr >> 8), base + 2); /* High address bits */
1003 val = inb(base); /* Data */
1005 restore_flags(flags);
1006 return val;
1009 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
1011 int mpu_base = hw_config->io_base;
1012 int mp_base = mpu_base + 4; /* Microcontroller base */
1013 int i;
1014 unsigned char control;
1018 * Reset the microcontroller so that the RAM can be accessed
1021 control = inb(mpu_base + 7);
1022 outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
1023 outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
1025 mdelay(3); /* Wait at least 1ms */
1027 outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
1030 * Detect microcontroller by probing the 8k RAM area
1032 smw_putmem(devc, mp_base, 0, 0x00);
1033 smw_putmem(devc, mp_base, 1, 0xff);
1034 udelay(10);
1036 if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
1038 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
1039 return 0; /* No RAM */
1042 * There is RAM so assume it's really a SM Wave
1045 devc->model = MDL_SMW;
1046 smw_mixer_init(devc);
1048 #ifdef MODULE
1049 if (!smw_ucode)
1051 extern void *smw_free;
1053 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1054 smw_free = smw_ucode;
1056 #endif
1057 if (smw_ucodeLen > 0)
1059 if (smw_ucodeLen != 8192)
1061 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1062 return 1;
1065 * Download microcode
1068 for (i = 0; i < 8192; i++)
1069 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1072 * Verify microcode
1075 for (i = 0; i < 8192; i++)
1076 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1078 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1079 return 0;
1082 control = 0;
1083 #ifdef SMW_SCSI_IRQ
1085 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1086 * is disabled by default.
1088 * FIXME - make this a module option
1090 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1093 static unsigned char scsi_irq_bits[] = {
1094 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1096 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1098 #endif
1100 #ifdef SMW_OPL4_ENABLE
1102 * Make the OPL4 chip visible on the PC bus at 0x380.
1104 * There is no need to enable this feature since this driver
1105 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1106 * enabling OPL4 is pretty useless.
1108 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1109 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1110 #endif
1111 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1112 hw_config->name = "SoundMan Wave";
1113 return 1;
1116 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1118 int mpu_base = hw_config->io_base;
1119 int sb_base = devc->base;
1120 int irq = hw_config->irq;
1122 unsigned char bits = 0;
1123 unsigned long flags;
1125 if (irq < 0)
1126 irq *= -1;
1128 if (irq < 1 || irq > 15 ||
1129 jazz_irq_bits[irq] == 0)
1131 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1132 return 0;
1134 switch (sb_base)
1136 case 0x220:
1137 bits = 1;
1138 break;
1139 case 0x240:
1140 bits = 2;
1141 break;
1142 case 0x260:
1143 bits = 3;
1144 break;
1145 default:
1146 return 0;
1148 bits = jazz16_bits = bits << 5;
1149 switch (mpu_base)
1151 case 0x310:
1152 bits |= 1;
1153 break;
1154 case 0x320:
1155 bits |= 2;
1156 break;
1157 case 0x330:
1158 bits |= 3;
1159 break;
1160 default:
1161 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1162 return 0;
1165 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1167 save_flags(flags);
1168 cli();
1169 outb(0xAF, 0x201);
1170 outb(0x50, 0x201);
1171 outb(bits, 0x201);
1172 restore_flags(flags);
1174 hw_config->name = "Jazz16";
1175 smw_midi_init(devc, hw_config);
1177 if (!sb_dsp_command(devc, 0xfb))
1178 return 0;
1180 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1181 (jazz_dma_bits[devc->dma16] << 4)))
1182 return 0;
1184 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1185 (jazz_irq_bits[irq] << 4)))
1186 return 0;
1188 return 1;
1191 void attach_sbmpu(struct address_info *hw_config)
1193 if (last_sb->model == MDL_ESS) {
1194 #if defined(CONFIG_SOUND_MPU401)
1195 attach_mpu401(hw_config);
1196 if (last_sb->irq == -hw_config->irq) {
1197 last_sb->midi_irq_cookie=(void *)hw_config->slots[1];
1199 #endif
1200 return;
1202 #if defined(CONFIG_UART401)
1203 attach_uart401(hw_config);
1204 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1205 #endif
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)
1218 return 0;
1220 #if defined(CONFIG_SOUND_MPU401)
1221 if (devc->model == MDL_ESS)
1223 if (check_region(hw_config->io_base, 2))
1225 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1226 return 0;
1228 if (!ess_midi_init(devc, hw_config))
1229 return 0;
1230 hw_config->name = "ESS1xxx MPU";
1231 devc->midi_irq_cookie = -1;
1232 return probe_mpu401(hw_config);
1234 #endif
1236 #if defined(CONFIG_UART401)
1237 if (check_region(hw_config->io_base, 4))
1239 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1240 return 0;
1242 switch (devc->model)
1244 case MDL_SB16:
1245 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1247 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1248 return 0;
1250 hw_config->name = "Sound Blaster 16";
1251 hw_config->irq = -devc->irq;
1252 if (devc->minor > 12) /* What is Vibra's version??? */
1253 sb16_set_mpu_port(devc, hw_config);
1254 break;
1256 case MDL_JAZZ:
1257 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1258 hw_config->irq = -devc->irq;
1259 if (!init_Jazz16_midi(devc, hw_config))
1260 return 0;
1261 break;
1263 case MDL_YMPCI:
1264 hw_config->name = "Yamaha PCI Legacy";
1265 printk("Yamaha PCI legacy UART401 check.\n");
1266 break;
1267 default:
1268 return 0;
1270 return probe_uart401(hw_config);
1271 #else
1272 return 0;
1273 #endif
1276 void unload_sbmpu(struct address_info *hw_config)
1278 #if defined(CONFIG_SOUND_MPU401)
1279 if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
1280 unload_mpu401(hw_config);
1281 return;
1283 #endif
1284 #if defined(CONFIG_UART401)
1285 unload_uart401(hw_config);
1286 #endif
1288 #else /* !CONFIG_MIDI */
1290 void unload_sbmpu(struct address_info *hw_config)
1294 int probe_sbmpu(struct address_info *hw_config)
1296 return 0;
1299 void attach_sbmpu(struct address_info *hw_config)
1302 #endif
1303 #endif