thp: comment reminder in destroy_compound_page
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / oss / sb_common.c
blob7d42c5418d1bb42ea6407ca284a6a0b498e7ec61
1 /*
2 * sound/oss/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@pobox.com>
22 * 2000/09/18 - got rid of attach_uart401
23 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
25 * 2001/01/26 - replaced CLI/STI with spinlocks
26 * Chris Rankin <rankinc@zipworld.com.au>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
36 #include "sound_config.h"
37 #include "sound_firmware.h"
39 #include "mpu401.h"
41 #include "sb_mixer.h"
42 #include "sb.h"
43 #include "sb_ess.h"
46 * global module flag
49 int sb_be_quiet;
51 static sb_devc *detected_devc; /* For communication from probe to init */
52 static sb_devc *last_devc; /* For MPU401 initialization */
54 static unsigned char jazz_irq_bits[] = {
55 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
58 static unsigned char jazz_dma_bits[] = {
59 0, 1, 0, 2, 0, 3, 0, 4
62 void *smw_free;
65 * Jazz16 chipset specific control variables
68 static int jazz16_base; /* Not detected */
69 static unsigned char jazz16_bits; /* I/O relocation bits */
70 static DEFINE_SPINLOCK(jazz16_lock);
73 * Logitech Soundman Wave specific initialization code
76 #ifdef SMW_MIDI0001_INCLUDED
77 #include "smw-midi0001.h"
78 #else
79 static unsigned char *smw_ucode;
80 static int smw_ucodeLen;
82 #endif
84 static sb_devc *last_sb; /* Last sb loaded */
86 int sb_dsp_command(sb_devc * devc, unsigned char val)
88 int i;
89 unsigned long limit;
91 limit = jiffies + HZ / 10; /* Timeout */
94 * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
95 * called while interrupts are disabled. This means that the timer is
96 * disabled also. However the timeout situation is a abnormal condition.
97 * Normally the DSP should be ready to accept commands after just couple of
98 * loops.
101 for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
103 if ((inb(DSP_STATUS) & 0x80) == 0)
105 outb((val), DSP_COMMAND);
106 return 1;
109 printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
110 return 0;
113 int sb_dsp_get_byte(sb_devc * devc)
115 int i;
117 for (i = 1000; i; i--)
119 if (inb(DSP_DATA_AVAIL) & 0x80)
120 return inb(DSP_READ);
122 return 0xffff;
125 static void sb_intr (sb_devc *devc)
127 int status;
128 unsigned char src = 0xff;
130 if (devc->model == MDL_SB16)
132 src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
134 if (src & 4) /* MPU401 interrupt */
135 if(devc->midi_irq_cookie)
136 uart401intr(devc->irq, devc->midi_irq_cookie);
138 if (!(src & 3))
139 return; /* Not a DSP interrupt */
141 if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
143 switch (devc->irq_mode)
145 case IMODE_OUTPUT:
146 DMAbuf_outputintr(devc->dev, 1);
147 break;
149 case IMODE_INPUT:
150 DMAbuf_inputintr(devc->dev);
151 break;
153 case IMODE_INIT:
154 break;
156 case IMODE_MIDI:
157 sb_midi_interrupt(devc);
158 break;
160 default:
161 /* printk(KERN_WARNING "Sound Blaster: Unexpected interrupt\n"); */
165 else if (devc->intr_active_16 && (src & 0x02))
167 switch (devc->irq_mode_16)
169 case IMODE_OUTPUT:
170 DMAbuf_outputintr(devc->dev, 1);
171 break;
173 case IMODE_INPUT:
174 DMAbuf_inputintr(devc->dev);
175 break;
177 case IMODE_INIT:
178 break;
180 default:
181 /* printk(KERN_WARNING "Sound Blaster: Unexpected interrupt\n"); */
186 * Acknowledge interrupts
189 if (src & 0x01)
190 status = inb(DSP_DATA_AVAIL);
192 if (devc->model == MDL_SB16 && src & 0x02)
193 status = inb(DSP_DATA_AVL16);
196 static void pci_intr(sb_devc *devc)
198 int src = inb(devc->pcibase+0x1A);
199 src&=3;
200 if(src)
201 sb_intr(devc);
204 static irqreturn_t sbintr(int irq, void *dev_id)
206 sb_devc *devc = dev_id;
208 devc->irq_ok = 1;
210 switch (devc->model) {
211 case MDL_ESSPCI:
212 pci_intr (devc);
213 break;
215 case MDL_ESS:
216 ess_intr (devc);
217 break;
218 default:
219 sb_intr (devc);
220 break;
222 return IRQ_HANDLED;
225 int sb_dsp_reset(sb_devc * devc)
227 int loopc;
229 DEB(printk("Entered sb_dsp_reset()\n"));
231 if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
233 /* This is only for non-ESS chips */
235 outb(1, DSP_RESET);
237 udelay(10);
238 outb(0, DSP_RESET);
239 udelay(30);
241 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
243 if (inb(DSP_READ) != 0xAA)
245 DDB(printk("sb: No response to RESET\n"));
246 return 0; /* Sorry */
249 DEB(printk("sb_dsp_reset() OK\n"));
251 return 1;
254 static void dsp_get_vers(sb_devc * devc)
256 int i;
258 unsigned long flags;
260 DDB(printk("Entered dsp_get_vers()\n"));
261 spin_lock_irqsave(&devc->lock, flags);
262 devc->major = devc->minor = 0;
263 sb_dsp_command(devc, 0xe1); /* Get version */
265 for (i = 100000; i; i--)
267 if (inb(DSP_DATA_AVAIL) & 0x80)
269 if (devc->major == 0)
270 devc->major = inb(DSP_READ);
271 else
273 devc->minor = inb(DSP_READ);
274 break;
278 spin_unlock_irqrestore(&devc->lock, flags);
279 DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
282 static int sb16_set_dma_hw(sb_devc * devc)
284 int bits;
286 if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
288 printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
289 return 0;
291 bits = (1 << devc->dma8);
293 if (devc->dma16 >= 5 && devc->dma16 <= 7)
294 bits |= (1 << devc->dma16);
296 sb_setmixer(devc, DMA_NR, bits);
297 return 1;
300 static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
303 * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
305 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
307 switch (hw_config->io_base)
309 case 0x300:
310 sb_setmixer(devc, 0x84, bits | 0x04);
311 break;
313 case 0x330:
314 sb_setmixer(devc, 0x84, bits | 0x00);
315 break;
317 default:
318 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
319 printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
323 static int sb16_set_irq_hw(sb_devc * devc, int level)
325 int ival;
327 switch (level)
329 case 5:
330 ival = 2;
331 break;
332 case 7:
333 ival = 4;
334 break;
335 case 9:
336 ival = 1;
337 break;
338 case 10:
339 ival = 8;
340 break;
341 default:
342 printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
343 return 0;
345 sb_setmixer(devc, IRQ_NR, ival);
346 return 1;
349 static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
351 unsigned char bits = 0;
352 unsigned long flags;
354 if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
355 return;
357 switch (hw_config->io_base)
359 case 0x220:
360 bits = 1;
361 break;
362 case 0x240:
363 bits = 2;
364 break;
365 case 0x260:
366 bits = 3;
367 break;
368 default:
369 return;
371 bits = jazz16_bits = bits << 5;
372 jazz16_base = hw_config->io_base;
375 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
377 spin_lock_irqsave(&jazz16_lock, flags);
378 outb((0xAF), 0x201);
379 outb((0x50), 0x201);
380 outb((bits), 0x201);
381 spin_unlock_irqrestore(&jazz16_lock, flags);
384 static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
386 char name[100];
388 * First try to check that the card has Jazz16 chip. It identifies itself
389 * by returning 0x12 as response to DSP command 0xfa.
392 if (!sb_dsp_command(devc, 0xfa))
393 return 0;
395 if (sb_dsp_get_byte(devc) != 0x12)
396 return 0;
399 * OK so far. Now configure the IRQ and DMA channel used by the card.
401 if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
403 printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
404 return 0;
406 if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
408 printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
409 return 0;
411 if (hw_config->dma2 < 0)
413 printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
414 return 0;
416 if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
418 printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
419 return 0;
421 devc->dma16 = hw_config->dma2;
423 if (!sb_dsp_command(devc, 0xfb))
424 return 0;
426 if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
427 (jazz_dma_bits[hw_config->dma2] << 4)))
428 return 0;
430 if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
431 return 0;
434 * Now we have configured a standard Jazz16 device.
436 devc->model = MDL_JAZZ;
437 strcpy(name, "Jazz16");
439 hw_config->name = "Jazz16";
440 devc->caps |= SB_NO_MIDI;
441 return 1;
444 static void relocate_ess1688(sb_devc * devc)
446 unsigned char bits;
448 switch (devc->base)
450 case 0x220:
451 bits = 0x04;
452 break;
453 case 0x230:
454 bits = 0x05;
455 break;
456 case 0x240:
457 bits = 0x06;
458 break;
459 case 0x250:
460 bits = 0x07;
461 break;
462 default:
463 return; /* Wrong port */
466 DDB(printk("Doing ESS1688 address selection\n"));
469 * ES1688 supports two alternative ways for software address config.
470 * First try the so called Read-Sequence-Key method.
473 /* Reset the sequence logic */
474 inb(0x229);
475 inb(0x229);
476 inb(0x229);
478 /* Perform the read sequence */
479 inb(0x22b);
480 inb(0x229);
481 inb(0x22b);
482 inb(0x229);
483 inb(0x229);
484 inb(0x22b);
485 inb(0x229);
487 /* Select the base address by reading from it. Then probe using the port. */
488 inb(devc->base);
489 if (sb_dsp_reset(devc)) /* Bingo */
490 return;
492 #if 0 /* This causes system lockups (Nokia 386/25 at least) */
494 * The last resort is the system control register method.
497 outb((0x00), 0xfb); /* 0xFB is the unlock register */
498 outb((0x00), 0xe0); /* Select index 0 */
499 outb((bits), 0xe1); /* Write the config bits */
500 outb((0x00), 0xf9); /* 0xFB is the lock register */
501 #endif
504 int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
506 sb_devc sb_info;
507 sb_devc *devc = &sb_info;
509 memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
511 /* Copy module options in place */
512 if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
514 sb_info.my_mididev = -1;
515 sb_info.my_mixerdev = -1;
516 sb_info.dev = -1;
519 * Initialize variables
522 DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
524 spin_lock_init(&devc->lock);
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 unsigned long flags;
556 spin_lock_irqsave(&devc->lock, flags);
557 inb(devc->base + 0x09);
558 inb(devc->base + 0x09);
559 inb(devc->base + 0x09);
560 inb(devc->base + 0x0b);
561 inb(devc->base + 0x09);
562 inb(devc->base + 0x0b);
563 inb(devc->base + 0x09);
564 inb(devc->base + 0x09);
565 inb(devc->base + 0x0b);
566 inb(devc->base + 0x09);
567 inb(devc->base + 0x00);
568 spin_unlock_irqrestore(&devc->lock, flags);
571 * Detect the device
574 if (sb_dsp_reset(devc))
575 dsp_get_vers(devc);
576 else
577 devc->major = 0;
579 if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
580 if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
581 relocate_Jazz16(devc, hw_config);
583 if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
584 relocate_ess1688(devc);
586 if (!sb_dsp_reset(devc))
588 DDB(printk("SB reset failed\n"));
589 #ifdef MODULE
590 printk(KERN_INFO "sb: dsp reset failed.\n");
591 #endif
592 return 0;
594 if (devc->major == 0)
595 dsp_get_vers(devc);
597 if (devc->major == 3 && devc->minor == 1)
599 if (devc->type == MDL_AZTECH) /* SG Washington? */
601 if (sb_dsp_command(devc, 0x09))
602 if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
604 int i;
606 /* Have some delay */
607 for (i = 0; i < 10000; i++)
608 inb(DSP_DATA_AVAIL);
609 devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
610 devc->model = MDL_AZTECH;
615 if(devc->type == MDL_ESSPCI)
616 devc->model = MDL_ESSPCI;
618 if(devc->type == MDL_YMPCI)
620 printk("YMPCI selected\n");
621 devc->model = MDL_YMPCI;
625 * Save device information for sb_dsp_init()
629 detected_devc = kmalloc(sizeof(sb_devc), GFP_KERNEL);
630 if (detected_devc == NULL)
632 printk(KERN_ERR "sb: Can't allocate memory for device information\n");
633 return 0;
635 memcpy(detected_devc, devc, sizeof(sb_devc));
636 MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
637 return 1;
640 int sb_dsp_init(struct address_info *hw_config, struct module *owner)
642 sb_devc *devc;
643 char name[100];
644 extern int sb_be_quiet;
645 int mixer22, mixer30;
648 * Check if we had detected a SB device earlier
650 DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
651 name[0] = 0;
653 if (detected_devc == NULL)
655 MDB(printk("No detected device\n"));
656 return 0;
658 devc = detected_devc;
659 detected_devc = NULL;
661 if (devc->base != hw_config->io_base)
663 DDB(printk("I/O port mismatch\n"));
664 release_region(devc->base, 16);
665 return 0;
668 * Now continue initialization of the device
671 devc->caps = hw_config->driver_use_1;
673 if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
674 { /* IRQ setup */
677 * ESS PCI cards do shared PCI IRQ stuff. Since they
678 * will get shared PCI irq lines we must cope.
681 int i=(devc->caps&SB_PCI_IRQ)?IRQF_SHARED:0;
683 if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
685 printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
686 release_region(devc->base, 16);
687 return 0;
689 devc->irq_ok = 0;
691 if (devc->major == 4)
692 if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
694 free_irq(devc->irq, devc);
695 release_region(devc->base, 16);
696 return 0;
698 if ((devc->type == 0 || devc->type == MDL_ESS) &&
699 devc->major == 3 && devc->minor == 1)
700 { /* Handle various chipsets which claim they are SB Pro compatible */
701 if ((devc->type != 0 && devc->type != MDL_ESS) ||
702 !ess_init(devc, hw_config))
704 if ((devc->type != 0 && devc->type != MDL_JAZZ &&
705 devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
707 DDB(printk("This is a genuine SB Pro\n"));
711 if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
712 devc->irq_ok = 1;
713 else
715 int n;
717 for (n = 0; n < 3 && devc->irq_ok == 0; n++)
719 if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
721 int i;
723 for (i = 0; !devc->irq_ok && i < 10000; i++);
726 if (!devc->irq_ok)
727 printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
728 else
730 DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
733 } /* IRQ setup */
735 last_sb = devc;
737 switch (devc->major)
739 case 1: /* SB 1.0 or 1.5 */
740 devc->model = hw_config->card_subtype = MDL_SB1;
741 break;
743 case 2: /* SB 2.x */
744 if (devc->minor == 0)
745 devc->model = hw_config->card_subtype = MDL_SB2;
746 else
747 devc->model = hw_config->card_subtype = MDL_SB201;
748 break;
750 case 3: /* SB Pro and most clones */
751 switch (devc->model) {
752 case 0:
753 devc->model = hw_config->card_subtype = MDL_SBPRO;
754 if (hw_config->name == NULL)
755 hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
756 break;
757 case MDL_ESS:
758 ess_dsp_init(devc, hw_config);
759 break;
761 break;
763 case 4:
764 devc->model = hw_config->card_subtype = MDL_SB16;
766 * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
767 * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
768 * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
769 * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
770 * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
772 if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
774 mixer30 = sb_getmixer(devc,0x30);
775 sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
776 sb_setmixer(devc,0x30,0xff);
777 /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
778 /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
779 if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
781 devc->submodel = SUBMDL_ALS100;
782 if (hw_config->name == NULL)
783 hw_config->name = "Sound Blaster 16 (ALS-100)";
785 else
787 sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
788 sb_setmixer(devc,0x4c,0x1f);
789 sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
790 devc->submodel = SUBMDL_ALS007;
791 if (hw_config->name == NULL)
792 hw_config->name = "Sound Blaster 16 (ALS-007)";
794 sb_setmixer(devc,0x30,mixer30);
796 else if (hw_config->name == NULL)
797 hw_config->name = "Sound Blaster 16";
799 if (hw_config->dma2 == -1)
800 devc->dma16 = devc->dma8;
801 else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
803 printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
804 devc->dma16 = devc->dma8;
806 else
807 devc->dma16 = hw_config->dma2;
809 if(!sb16_set_dma_hw(devc)) {
810 free_irq(devc->irq, devc);
811 release_region(hw_config->io_base, 16);
812 return 0;
815 devc->caps |= SB_NO_MIDI;
818 if (!(devc->caps & SB_NO_MIXER))
819 if (devc->major == 3 || devc->major == 4)
820 sb_mixer_init(devc, owner);
822 if (!(devc->caps & SB_NO_MIDI))
823 sb_dsp_midi_init(devc, owner);
825 if (hw_config->name == NULL)
826 hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
828 sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
829 conf_printf(name, hw_config);
832 * Assuming that a sound card is Sound Blaster (compatible) is the most common
833 * configuration error and the mother of all problems. Usually sound cards
834 * emulate SB Pro but in addition they have a 16 bit native mode which should be
835 * used in Unix. See Readme.cards for more information about configuring OSS/Free
836 * properly.
838 if (devc->model <= MDL_SBPRO)
840 if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
842 printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
843 printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
844 printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
845 printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
847 else if (!sb_be_quiet && devc->model == MDL_SBPRO)
849 printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
850 printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
851 printk(KERN_INFO "is incorrectly configured.\n");
854 hw_config->card_subtype = devc->model;
855 hw_config->slots[0]=devc->dev;
856 last_devc = devc; /* For SB MPU detection */
858 if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
860 if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
862 printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
864 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
866 if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
867 printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
869 sb_audio_init(devc, name, owner);
870 hw_config->slots[0]=devc->dev;
872 else
874 MDB(printk("Sound Blaster: no audio devices found.\n"));
876 return 1;
879 /* if (sbmpu) below we allow mpu401 to manage the midi devs
880 otherwise we have to unload them. (Andrzej Krzysztofowicz) */
882 void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
884 sb_devc *devc;
886 devc = audio_devs[hw_config->slots[0]]->devc;
888 if (devc && devc->base == hw_config->io_base)
890 if ((devc->model & MDL_ESS) && devc->pcibase)
891 release_region(devc->pcibase, 8);
893 release_region(devc->base, 16);
895 if (!(devc->caps & SB_NO_AUDIO))
897 sound_free_dma(devc->dma8);
898 if (devc->dma16 >= 0)
899 sound_free_dma(devc->dma16);
901 if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
903 if (devc->irq > 0)
904 free_irq(devc->irq, devc);
906 sb_mixer_unload(devc);
907 /* We don't have to do this bit any more the UART401 is its own
908 master -- Krzysztof Halasa */
909 /* But we have to do it, if UART401 is not detected */
910 if (!sbmpu)
911 sound_unload_mididev(devc->my_mididev);
912 sound_unload_audiodev(devc->dev);
914 kfree(devc);
916 else
917 release_region(hw_config->io_base, 16);
919 kfree(detected_devc);
923 * Mixer access routines
925 * ES1887 modifications: some mixer registers reside in the
926 * range above 0xa0. These must be accessed in another way.
929 void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
931 unsigned long flags;
933 if (devc->model == MDL_ESS) {
934 ess_setmixer (devc, port, value);
935 return;
938 spin_lock_irqsave(&devc->lock, flags);
940 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
941 udelay(20);
942 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
943 udelay(20);
945 spin_unlock_irqrestore(&devc->lock, flags);
948 unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
950 unsigned int val;
951 unsigned long flags;
953 if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
955 spin_lock_irqsave(&devc->lock, flags);
957 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
958 udelay(20);
959 val = inb(MIXER_DATA);
960 udelay(20);
962 spin_unlock_irqrestore(&devc->lock, flags);
964 return val;
967 void sb_chgmixer
968 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
970 int value;
972 value = sb_getmixer(devc, reg);
973 value = (value & ~mask) | (val & mask);
974 sb_setmixer(devc, reg, value);
978 * MPU401 MIDI initialization.
981 static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
983 unsigned long flags;
985 spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
987 outb((addr & 0xff), base + 1); /* Low address bits */
988 outb((addr >> 8), base + 2); /* High address bits */
989 outb((val), base); /* Data */
991 spin_unlock_irqrestore(&jazz16_lock, flags);
994 static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
996 unsigned long flags;
997 unsigned char val;
999 spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
1001 outb((addr & 0xff), base + 1); /* Low address bits */
1002 outb((addr >> 8), base + 2); /* High address bits */
1003 val = inb(base); /* Data */
1005 spin_unlock_irqrestore(&jazz16_lock, 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 smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
1052 smw_free = smw_ucode;
1054 #endif
1055 if (smw_ucodeLen > 0)
1057 if (smw_ucodeLen != 8192)
1059 printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1060 return 1;
1063 * Download microcode
1066 for (i = 0; i < 8192; i++)
1067 smw_putmem(devc, mp_base, i, smw_ucode[i]);
1070 * Verify microcode
1073 for (i = 0; i < 8192; i++)
1074 if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
1076 printk(KERN_ERR "SM Wave: Microcode verification failed\n");
1077 return 0;
1080 control = 0;
1081 #ifdef SMW_SCSI_IRQ
1083 * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
1084 * is disabled by default.
1086 * FIXME - make this a module option
1088 * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
1091 static unsigned char scsi_irq_bits[] = {
1092 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
1094 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1096 #endif
1098 #ifdef SMW_OPL4_ENABLE
1100 * Make the OPL4 chip visible on the PC bus at 0x380.
1102 * There is no need to enable this feature since this driver
1103 * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
1104 * enabling OPL4 is pretty useless.
1106 control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
1107 /* control |= 0x20; Uncomment this if you want to use IRQ7 */
1108 #endif
1109 outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
1110 hw_config->name = "SoundMan Wave";
1111 return 1;
1114 static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
1116 int mpu_base = hw_config->io_base;
1117 int sb_base = devc->base;
1118 int irq = hw_config->irq;
1120 unsigned char bits = 0;
1121 unsigned long flags;
1123 if (irq < 0)
1124 irq *= -1;
1126 if (irq < 1 || irq > 15 ||
1127 jazz_irq_bits[irq] == 0)
1129 printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
1130 return 0;
1132 switch (sb_base)
1134 case 0x220:
1135 bits = 1;
1136 break;
1137 case 0x240:
1138 bits = 2;
1139 break;
1140 case 0x260:
1141 bits = 3;
1142 break;
1143 default:
1144 return 0;
1146 bits = jazz16_bits = bits << 5;
1147 switch (mpu_base)
1149 case 0x310:
1150 bits |= 1;
1151 break;
1152 case 0x320:
1153 bits |= 2;
1154 break;
1155 case 0x330:
1156 bits |= 3;
1157 break;
1158 default:
1159 printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
1160 return 0;
1163 * Magic wake up sequence by writing to 0x201 (aka Joystick port)
1165 spin_lock_irqsave(&jazz16_lock, flags);
1166 outb(0xAF, 0x201);
1167 outb(0x50, 0x201);
1168 outb(bits, 0x201);
1169 spin_unlock_irqrestore(&jazz16_lock, flags);
1171 hw_config->name = "Jazz16";
1172 smw_midi_init(devc, hw_config);
1174 if (!sb_dsp_command(devc, 0xfb))
1175 return 0;
1177 if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
1178 (jazz_dma_bits[devc->dma16] << 4)))
1179 return 0;
1181 if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
1182 (jazz_irq_bits[irq] << 4)))
1183 return 0;
1185 return 1;
1188 int probe_sbmpu(struct address_info *hw_config, struct module *owner)
1190 sb_devc *devc = last_devc;
1191 int ret;
1193 if (last_devc == NULL)
1194 return 0;
1196 last_devc = NULL;
1198 if (hw_config->io_base <= 0)
1200 /* The real vibra16 is fine about this, but we have to go
1201 wipe up after Cyrix again */
1203 if(devc->model == MDL_SB16 && devc->minor >= 12)
1205 unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
1206 sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
1208 return 0;
1211 #if defined(CONFIG_SOUND_MPU401)
1212 if (devc->model == MDL_ESS)
1214 struct resource *ports;
1215 ports = request_region(hw_config->io_base, 2, "mpu401");
1216 if (!ports) {
1217 printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
1218 return 0;
1220 if (!ess_midi_init(devc, hw_config)) {
1221 release_region(hw_config->io_base, 2);
1222 return 0;
1224 hw_config->name = "ESS1xxx MPU";
1225 devc->midi_irq_cookie = NULL;
1226 if (!probe_mpu401(hw_config, ports)) {
1227 release_region(hw_config->io_base, 2);
1228 return 0;
1230 attach_mpu401(hw_config, owner);
1231 if (last_sb->irq == -hw_config->irq)
1232 last_sb->midi_irq_cookie =
1233 (void *)(long) hw_config->slots[1];
1234 return 1;
1236 #endif
1238 switch (devc->model)
1240 case MDL_SB16:
1241 if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
1243 printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
1244 return 0;
1246 hw_config->name = "Sound Blaster 16";
1247 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1248 hw_config->irq = -devc->irq;
1249 if (devc->minor > 12) /* What is Vibra's version??? */
1250 sb16_set_mpu_port(devc, hw_config);
1251 break;
1253 case MDL_JAZZ:
1254 if (hw_config->irq < 3 || hw_config->irq == devc->irq)
1255 hw_config->irq = -devc->irq;
1256 if (!init_Jazz16_midi(devc, hw_config))
1257 return 0;
1258 break;
1260 case MDL_YMPCI:
1261 hw_config->name = "Yamaha PCI Legacy";
1262 printk("Yamaha PCI legacy UART401 check.\n");
1263 break;
1264 default:
1265 return 0;
1268 ret = probe_uart401(hw_config, owner);
1269 if (ret)
1270 last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
1271 return ret;
1274 void unload_sbmpu(struct address_info *hw_config)
1276 #if defined(CONFIG_SOUND_MPU401)
1277 if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
1278 unload_mpu401(hw_config);
1279 return;
1281 #endif
1282 unload_uart401(hw_config);
1285 EXPORT_SYMBOL(sb_dsp_init);
1286 EXPORT_SYMBOL(sb_dsp_detect);
1287 EXPORT_SYMBOL(sb_dsp_unload);
1288 EXPORT_SYMBOL(sb_be_quiet);
1289 EXPORT_SYMBOL(probe_sbmpu);
1290 EXPORT_SYMBOL(unload_sbmpu);
1291 EXPORT_SYMBOL(smw_free);
1292 MODULE_LICENSE("GPL");