Linux 2.2.0
[davej-history.git] / drivers / sound / sb_common.c
blob9072509814489efba6c4379ddbaed84b62d12049
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 <asm/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 sbintr(int irq, void *dev_id, struct pt_regs *dummy)
184 sb_devc *devc = dev_id;
186 devc->irq_ok = 1;
188 switch (devc->model) {
189 case MDL_ESS:
190 ess_intr (devc);
191 break;
192 default:
193 sb_intr (devc);
194 break;
198 int sb_dsp_reset(sb_devc * devc)
200 int loopc;
202 DEB(printk("Entered sb_dsp_reset()\n"));
204 if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
206 /* This is only for non-ESS chips */
208 outb(1, DSP_RESET);
210 udelay(10);
211 outb(0, DSP_RESET);
212 udelay(30);
214 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
216 if (inb(DSP_READ) != 0xAA)
218 DDB(printk("sb: No response to RESET\n"));
219 return 0; /* Sorry */
222 DEB(printk("sb_dsp_reset() OK\n"));
224 return 1;
227 static void dsp_get_vers(sb_devc * devc)
229 int i;
231 unsigned long flags;
233 DDB(printk("Entered dsp_get_vers()\n"));
234 save_flags(flags);
235 cli();
236 devc->major = devc->minor = 0;
237 sb_dsp_command(devc, 0xe1); /* Get version */
239 for (i = 100000; i; i--)
241 if (inb(DSP_DATA_AVAIL) & 0x80)
243 if (devc->major == 0)
244 devc->major = inb(DSP_READ);
245 else
247 devc->minor = inb(DSP_READ);
248 break;
252 DDB(printk("DSP version %d.%d\n", devc->major, devc->minor));
253 restore_flags(flags);
256 static int sb16_set_dma_hw(sb_devc * devc)
258 int bits;
260 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
262 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
263 return 0;
265 bits = (1 << devc->dma8);
267 if (devc->dma16 >= 5 && devc->dma16 <= 7)
268 bits |= (1 << devc->dma16);
270 sb_setmixer(devc, DMA_NR, bits);
271 return 1;
274 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
275 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
278 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
280 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
282 switch (hw_config->io_base)
284 case 0x300:
285 sb_setmixer(devc, 0x84, bits | 0x04);
286 break;
288 case 0x330:
289 sb_setmixer(devc, 0x84, bits | 0x00);
290 break;
292 default:
293 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
294 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
297 #endif
299 static int sb16_set_irq_hw(sb_devc * devc, int level)
301 int ival;
303 switch (level)
305 case 5:
306 ival = 2;
307 break;
308 case 7:
309 ival = 4;
310 break;
311 case 9:
312 ival = 1;
313 break;
314 case 10:
315 ival = 8;
316 break;
317 default:
318 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
319 return 0;
321 sb_setmixer(devc, IRQ_NR, ival);
322 return 1;
325 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
327 unsigned char bits = 0;
328 unsigned long flags;
330 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
331 return;
333 switch (hw_config->io_base)
335 case 0x220:
336 bits = 1;
337 break;
338 case 0x240:
339 bits = 2;
340 break;
341 case 0x260:
342 bits = 3;
343 break;
344 default:
345 return;
347 bits = jazz16_bits = bits << 5;
348 jazz16_base = hw_config->io_base;
351 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
353 save_flags(flags);
354 cli();
355 outb((0xAF), 0x201);
356 outb((0x50), 0x201);
357 outb((bits), 0x201);
358 restore_flags(flags);
361 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
363 char name[100];
365 * First try to check that the card has Jazz16 chip. It identifies itself
366 * by returning 0x12 as response to DSP command 0xfa.
369 if (!sb_dsp_command(devc, 0xfa))
370 return 0;
372 if (sb_dsp_get_byte(devc) != 0x12)
373 return 0;
376 * OK so far. Now configure the IRQ and DMA channel used by the card.
378 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
380 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
381 return 0;
383 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
385 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
386 return 0;
388 if (hw_config->dma2 < 0)
390 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
391 return 0;
393 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
395 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
396 return 0;
398 devc->dma16 = hw_config->dma2;
400 if (!sb_dsp_command(devc, 0xfb))
401 return 0;
403 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
404 (jazz_dma_bits[hw_config->dma2] << 4)))
405 return 0;
407 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
408 return 0;
411 * Now we have configured a standard Jazz16 device.
413 devc->model = MDL_JAZZ;
414 strcpy(name, "Jazz16");
416 hw_config->name = "Jazz16";
417 devc->caps |= SB_NO_MIDI;
418 return 1;
421 static void relocate_ess1688(sb_devc * devc)
423 unsigned char bits;
425 switch (devc->base)
427 case 0x220:
428 bits = 0x04;
429 break;
430 case 0x230:
431 bits = 0x05;
432 break;
433 case 0x240:
434 bits = 0x06;
435 break;
436 case 0x250:
437 bits = 0x07;
438 break;
439 default:
440 return; /* Wrong port */
443 DDB(printk("Doing ESS1688 address selection\n"));
446 * ES1688 supports two alternative ways for software address config.
447 * First try the so called Read-Sequence-Key method.
450 /* Reset the sequence logic */
451 inb(0x229);
452 inb(0x229);
453 inb(0x229);
455 /* Perform the read sequence */
456 inb(0x22b);
457 inb(0x229);
458 inb(0x22b);
459 inb(0x229);
460 inb(0x229);
461 inb(0x22b);
462 inb(0x229);
464 /* Select the base address by reading from it. Then probe using the port. */
465 inb(devc->base);
466 if (sb_dsp_reset(devc)) /* Bingo */
467 return;
469 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
471 * The last resort is the system control register method.
474 outb((0x00), 0xfb); /* 0xFB is the unlock register */
475 outb((0x00), 0xe0); /* Select index 0 */
476 outb((bits), 0xe1); /* Write the config bits */
477 outb((0x00), 0xf9); /* 0xFB is the lock register */
478 #endif
481 int sb_dsp_detect(struct address_info *hw_config)
483 sb_devc sb_info;
484 sb_devc *devc = &sb_info;
486 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
487 sb_info.my_mididev = -1;
488 sb_info.my_mixerdev = -1;
489 sb_info.dev = -1;
492 * Initialize variables
495 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
496 if (check_region(hw_config->io_base, 16))
498 #ifdef MODULE
499 printk(KERN_INFO "sb: I/O region in use.\n");
500 #endif
501 return 0;
504 devc->type = hw_config->card_subtype;
506 devc->base = hw_config->io_base;
507 devc->irq = hw_config->irq;
508 devc->dma8 = hw_config->dma;
510 devc->dma16 = -1;
512 if (acer)
514 cli();
515 inb(devc->base + 0x09);
516 inb(devc->base + 0x09);
517 inb(devc->base + 0x09);
518 inb(devc->base + 0x0b);
519 inb(devc->base + 0x09);
520 inb(devc->base + 0x0b);
521 inb(devc->base + 0x09);
522 inb(devc->base + 0x09);
523 inb(devc->base + 0x0b);
524 inb(devc->base + 0x09);
525 inb(devc->base + 0x00);
526 sti();
529 * Detect the device
532 if (sb_dsp_reset(devc))
533 dsp_get_vers(devc);
534 else
535 devc->major = 0;
537 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
538 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
539 relocate_Jazz16(devc, hw_config);
541 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
542 relocate_ess1688(devc);
544 if (!sb_dsp_reset(devc))
546 DDB(printk("SB reset failed\n"));
547 #ifdef MODULE
548 printk(KERN_INFO "sb: dsp reset failed.\n");
549 #endif
550 return 0;
552 if (devc->major == 0)
553 dsp_get_vers(devc);
555 if (devc->major == 3 && devc->minor == 1)
557 if (devc->type == MDL_AZTECH) /* SG Washington? */
559 if (sb_dsp_command(devc, 0x09))
560 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
562 int i;
564 /* Have some delay */
565 for (i = 0; i < 10000; i++)
566 inb(DSP_DATA_AVAIL);
567 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
568 devc->model = MDL_AZTECH;
573 * Save device information for sb_dsp_init()
577 detected_devc = (sb_devc *)kmalloc(sizeof(sb_devc), GFP_KERNEL);
578 if (detected_devc == NULL)
580 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
581 return 0;
583 memcpy((char *) detected_devc, (char *) devc, sizeof(sb_devc));
584 MDB(printk(KERN_INFO "SB %d.%d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
585 return 1;
588 int sb_dsp_init(struct address_info *hw_config)
590 sb_devc *devc;
591 char name[100];
592 extern int sb_be_quiet;
593 int mixer22, mixer30;
596 * Check if we had detected a SB device earlier
598 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
599 name[0] = 0;
601 if (detected_devc == NULL)
603 MDB(printk("No detected device\n"));
604 return 0;
606 devc = detected_devc;
607 detected_devc = NULL;
609 if (devc->base != hw_config->io_base)
611 DDB(printk("I/O port mismatch\n"));
612 return 0;
615 * Now continue initialization of the device
618 devc->caps = hw_config->driver_use_1;
620 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI) && hw_config->irq > 0)
621 { /* IRQ setup */
622 if (request_irq(hw_config->irq, sbintr, 0, "soundblaster", devc) < 0)
624 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
625 return 0;
627 devc->irq_ok = 0;
629 if (devc->major == 4)
630 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
632 free_irq(devc->irq, devc);
633 return 0;
635 if ((devc->type == 0 || devc->type == MDL_ESS) &&
636 devc->major == 3 && devc->minor == 1)
637 { /* Handle various chipsets which claim they are SB Pro compatible */
638 if ((devc->type != 0 && devc->type != MDL_ESS) ||
639 !ess_init(devc, hw_config))
641 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
642 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
644 DDB(printk("This is a genuine SB Pro\n"));
648 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
649 devc->irq_ok = 1;
650 else
652 int n;
654 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
656 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
658 int i;
660 for (i = 0; !devc->irq_ok && i < 10000; i++);
663 if (!devc->irq_ok)
664 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
665 else
667 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
670 } /* IRQ setup */
671 request_region(hw_config->io_base, 16, "soundblaster");
673 last_sb = devc;
675 switch (devc->major)
677 case 1: /* SB 1.0 or 1.5 */
678 devc->model = hw_config->card_subtype = MDL_SB1;
679 break;
681 case 2: /* SB 2.x */
682 if (devc->minor == 0)
683 devc->model = hw_config->card_subtype = MDL_SB2;
684 else
685 devc->model = hw_config->card_subtype = MDL_SB201;
686 break;
688 case 3: /* SB Pro and most clones */
689 switch (devc->model) {
690 case 0:
691 devc->model = hw_config->card_subtype = MDL_SBPRO;
692 if (hw_config->name == NULL)
693 hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
694 break;
695 case MDL_ESS:
696 if (!ess_dsp_init(devc, hw_config)) {
697 release_region (hw_config->io_base, 16);
698 return 0;
700 break;
702 break;
704 case 4:
705 devc->model = hw_config->card_subtype = MDL_SB16;
707 * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
708 * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
709 * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
710 * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
711 * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
713 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
715 mixer30 = sb_getmixer(devc,0x30);
716 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
717 sb_setmixer(devc,0x30,0xff);
718 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
719 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
720 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
722 devc->submodel = SUBMDL_ALS100;
723 if (hw_config->name == NULL)
724 hw_config->name = "Sound Blaster 16 (ALS-100)";
726 else
728 sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
729 sb_setmixer(devc,0x4c,0x1f);
730 sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
731 devc->submodel = SUBMDL_ALS007;
732 if (hw_config->name == NULL)
733 hw_config->name = "Sound Blaster 16 (ALS-007)";
735 sb_setmixer(devc,0x30,mixer30);
737 else if (hw_config->name == NULL)
738 hw_config->name = "Sound Blaster 16";
740 if (hw_config->dma2 == -1)
741 devc->dma16 = devc->dma8;
742 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
744 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
745 devc->dma16 = devc->dma8;
747 else
748 devc->dma16 = hw_config->dma2;
750 if(!sb16_set_dma_hw(devc)) {
751 free_irq(devc->irq, devc);
752 release_region(hw_config->io_base, 16);
753 return 0;
756 devc->caps |= SB_NO_MIDI;
759 if (!(devc->caps & SB_NO_MIXER))
760 if (devc->major == 3 || devc->major == 4)
761 sb_mixer_init(devc);
763 #ifdef CONFIG_MIDI
764 if (!(devc->caps & SB_NO_MIDI))
765 sb_dsp_midi_init(devc);
766 #endif
768 if (hw_config->name == NULL)
769 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
771 sprintf(name, "%s (%d.%d)", hw_config->name, devc->major, devc->minor);
772 conf_printf(name, hw_config);
775 * Assuming that a sound card is Sound Blaster (compatible) is the most common
776 * configuration error and the mother of all problems. Usually sound cards
777 * emulate SB Pro but in addition they have a 16 bit native mode which should be
778 * used in Unix. See Readme.cards for more information about configuring OSS/Free
779 * properly.
781 if (devc->model <= MDL_SBPRO)
783 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
785 printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
786 printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
787 printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
788 printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
790 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
792 printk(KERN_INFO "SB DSP version is just %d.%d which means that your card is\n", devc->major, devc->minor);
793 printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
794 printk(KERN_INFO "is incorrectly configured.\n");
797 hw_config->card_subtype = devc->model;
798 hw_config->slots[0]=devc->dev;
799 last_devc = devc; /* For SB MPU detection */
801 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
803 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
805 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
807 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
809 if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
810 printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
812 sb_audio_init(devc, name);
813 hw_config->slots[0]=devc->dev;
815 else
817 MDB(printk("Sound Blaster: no audio devices found.\n"));
819 return 1;
822 void sb_dsp_disable_midi(int io_base)
826 void sb_dsp_disable_recording(int io_base)
830 /* if (sbmpu) below we allow mpu401 to manage the midi devs
831 otherwise we have to unload them. (Andrzej Krzysztofowicz) */
833 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
835 sb_devc *devc;
837 devc = audio_devs[hw_config->slots[0]]->devc;
839 if (devc && devc->base == hw_config->io_base)
841 release_region(devc->base, 16);
843 if (!(devc->caps & SB_NO_AUDIO))
845 sound_free_dma(devc->dma8);
846 if (devc->dma16 >= 0)
847 sound_free_dma(devc->dma16);
849 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
851 if (devc->irq > 0);
852 free_irq(devc->irq, devc);
854 sound_unload_mixerdev(devc->my_mixerdev);
855 /* We don't have to do this bit any more the UART401 is its own
856 master -- Krzysztof Halasa */
857 /* But we have to do it, if UART401 is not detected */
858 if (!sbmpu)
859 sound_unload_mididev(devc->my_mididev);
860 sound_unload_audiodev(devc->dev);
862 kfree(devc);
864 else
865 release_region(hw_config->io_base, 16);
866 if(detected_devc)
867 kfree(detected_devc);
871 * Mixer access routines
873 * ES1887 modifications: some mixer registers reside in the
874 * range above 0xa0. These must be accessed in another way.
877 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
879 unsigned long flags;
881 if (devc->model == MDL_ESS) return ess_setmixer (devc, port, value);
883 save_flags(flags);
884 cli();
886 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
887 udelay(20);
888 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
889 udelay(20);
891 restore_flags(flags);
894 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
896 unsigned int val;
897 unsigned long flags;
899 if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
901 save_flags(flags);
902 cli();
904 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
905 udelay(20);
906 val = inb(MIXER_DATA);
907 udelay(20);
909 restore_flags(flags);
911 return val;
914 void sb_chgmixer
915 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
917 int value;
919 value = sb_getmixer(devc, reg);
920 value = (value & ~mask) | (val & mask);
921 sb_setmixer(devc, reg, value);
924 #ifdef CONFIG_MIDI
927 * MPU401 MIDI initialization.
930 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
932 unsigned long flags;
934 save_flags(flags);
935 cli();
937 outb((addr & 0xff), base + 1); /* Low address bits */
938 outb((addr >> 8), base + 2); /* High address bits */
939 outb((val), base); /* Data */
941 restore_flags(flags);
944 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
946 unsigned long flags;
947 unsigned char val;
949 save_flags(flags);
950 cli();
952 outb((addr & 0xff), base + 1); /* Low address bits */
953 outb((addr >> 8), base + 2); /* High address bits */
954 val = inb(base); /* Data */
956 restore_flags(flags);
957 return val;
960 static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
962 int mpu_base = hw_config->io_base;
963 int mp_base = mpu_base + 4; /* Microcontroller base */
964 int i;
965 unsigned char control;
969 * Reset the microcontroller so that the RAM can be accessed
972 control = inb(mpu_base + 7);
973 outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
974 outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
976 mdelay(3); /* Wait at least 1ms */
978 outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
981 * Detect microcontroller by probing the 8k RAM area
983 smw_putmem(devc, mp_base, 0, 0x00);
984 smw_putmem(devc, mp_base, 1, 0xff);
985 udelay(10);
987 if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
989 DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
990 return 0; /* No RAM */
993 * There is RAM so assume it's really a SM Wave
996 devc->model = MDL_SMW;
997 smw_mixer_init(devc);
999 #ifdef MODULE
1000 if (!smw_ucode)
1002 extern void *smw_free;
1004 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1005 smw_free = smw_ucode;
1007 #endif
1008 if (smw_ucodeLen > 0)
1010 if (smw_ucodeLen != 8192)
1012 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1013 return 1;
1016 * Download microcode
1019 for (i = 0; i < 8192; i++)
1020 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1023 * Verify microcode
1026 for (i = 0; i < 8192; i++)
1027 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1029 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1030 return 0;
1033 control = 0;
1034 #ifdef SMW_SCSI_IRQ
1036 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1037 * is disabled by default.
1039 * FIXME - make this a module option
1041 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1044 static unsigned char scsi_irq_bits[] = {
1045 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1047 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1049 #endif
1051 #ifdef SMW_OPL4_ENABLE
1053 * Make the OPL4 chip visible on the PC bus at 0x380.
1055 * There is no need to enable this feature since this driver
1056 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1057 * enabling OPL4 is pretty useless.
1059 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1060 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1061 #endif
1062 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1063 hw_config->name = "SoundMan Wave";
1064 return 1;
1067 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1069 int mpu_base = hw_config->io_base;
1070 int sb_base = devc->base;
1071 int irq = hw_config->irq;
1073 unsigned char bits = 0;
1074 unsigned long flags;
1076 if (irq < 0)
1077 irq *= -1;
1079 if (irq < 1 || irq > 15 ||
1080 jazz_irq_bits[irq] == 0)
1082 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1083 return 0;
1085 switch (sb_base)
1087 case 0x220:
1088 bits = 1;
1089 break;
1090 case 0x240:
1091 bits = 2;
1092 break;
1093 case 0x260:
1094 bits = 3;
1095 break;
1096 default:
1097 return 0;
1099 bits = jazz16_bits = bits << 5;
1100 switch (mpu_base)
1102 case 0x310:
1103 bits |= 1;
1104 break;
1105 case 0x320:
1106 bits |= 2;
1107 break;
1108 case 0x330:
1109 bits |= 3;
1110 break;
1111 default:
1112 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1113 return 0;
1116 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1118 save_flags(flags);
1119 cli();
1120 outb(0xAF, 0x201);
1121 outb(0x50, 0x201);
1122 outb(bits, 0x201);
1123 restore_flags(flags);
1125 hw_config->name = "Jazz16";
1126 smw_midi_init(devc, hw_config);
1128 if (!sb_dsp_command(devc, 0xfb))
1129 return 0;
1131 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1132 (jazz_dma_bits[devc->dma16] << 4)))
1133 return 0;
1135 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1136 (jazz_irq_bits[irq] << 4)))
1137 return 0;
1139 return 1;
1142 void attach_sbmpu(struct address_info *hw_config)
1144 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1145 attach_uart401(hw_config);
1146 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1147 #endif
1150 int probe_sbmpu(struct address_info *hw_config)
1152 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1153 sb_devc *devc = last_devc;
1155 if (last_devc == NULL)
1156 return 0;
1158 last_devc = 0;
1160 if (hw_config->io_base <= 0)
1161 return 0;
1163 if (check_region(hw_config->io_base, 4))
1165 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1166 return 0;
1168 switch (devc->model)
1170 case MDL_SB16:
1171 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1173 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1174 return 0;
1176 hw_config->name = "Sound Blaster 16";
1177 hw_config->irq = -devc->irq;
1178 if (devc->minor > 12) /* What is Vibra's version??? */
1179 sb16_set_mpu_port(devc, hw_config);
1180 break;
1182 case MDL_ESS:
1183 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1184 hw_config->irq = -devc->irq;
1185 if (!ess_midi_init(devc, hw_config))
1186 return 0;
1187 hw_config->name = "ESS ES1688";
1188 break;
1190 case MDL_JAZZ:
1191 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1192 hw_config->irq = -devc->irq;
1193 if (!init_Jazz16_midi(devc, hw_config))
1194 return 0;
1195 break;
1197 default:
1198 return 0;
1200 return probe_uart401(hw_config);
1201 #else
1202 return 0;
1203 #endif
1206 void unload_sbmpu(struct address_info *hw_config)
1208 #if defined(CONFIG_MIDI) && defined(CONFIG_UART401)
1209 unload_uart401(hw_config);
1210 #endif
1212 #else /* !CONFIG_MIDI */
1214 void unload_sbmpu(struct address_info *hw_config)
1218 int probe_sbmpu(struct address_info *hw_config)
1220 return 0;
1223 void attach_sbmpu(struct address_info *hw_config)
1226 #endif
1227 #endif