Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / sound / isa / i386 / sb / sb_dsp.c
1 /*
2  * sound/sb_dsp.c
3  * 
4  * The low level driver for the SoundBlaster DSP chip (SB1.0 to 2.1, SB Pro).
5  * 
6  * Copyright by Hannu Savolainen 1994
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer. 2.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  * 
28  * Modified: Hunyue Yau      Jan 6 1994 Added code to support Sound Galaxy NX
29  * Pro
30  * 
31  * JRA Gibson      April 1995 Code added for MV ProSonic/Jazz 16 in 16 bit mode
32  *
33  * $FreeBSD: src/sys/i386/isa/sound/sb_dsp.c,v 1.42 1999/12/27 04:37:19 tanimura Exp $
34  */
35
36 #include <i386/isa/sound/sound_config.h>
37
38 #if (NSB > 0)
39
40 #ifdef SM_WAVE
41 #define JAZZ16
42 #endif
43
44 #include  <i386/isa/sound/sbcard.h>
45 #include <i386/isa/sound/sb_mixer.h>
46 #include <machine/clock.h>
47
48 #undef SB_TEST_IRQ
49
50 /*
51  * XXX note -- only one sb-like device is supported until these
52  * variables are put in a struct sb_unit[] array
53  */
54
55
56
57 int             sbc_base = 0;
58 static int      sbc_irq = 0;
59 static int      open_mode = 0;  /* Read, write or both */
60 int             Jazz16_detected = 0;
61 int             sb_no_recording = 0;
62 static int      dsp_count = 0;
63 static int      trigger_bits;
64
65 /*
66  * The DSP channel can be used either for input or output. Variable
67  * 'sb_irq_mode' will be set when the program calls read or write first time
68  * after open. Current version doesn't support mode changes without closing
69  * and reopening the device. Support for this feature may be implemented in a
70  * future version of this driver.
71  */
72
73 int             sb_dsp_ok = 0;  /* Set to 1 after successful init */
74 static int      midi_disabled = 0;
75 int             sb_dsp_highspeed = 0;
76 int             sbc_major = 1, sbc_minor = 0;   /* DSP version   */
77 static int      dsp_stereo = 0;
78 static int      dsp_current_speed = DSP_DEFAULT_SPEED;
79 static int      sb16 = 0;
80
81 int             sb_midi_mode = NORMAL_MIDI;
82 int             sb_midi_busy = 0;       /* 1 if the process has output to *  *
83                                          * MIDI   */
84 int             sb_dsp_busy = 0;
85
86 volatile int    sb_irq_mode = IMODE_NONE; /* or IMODE_INPUT or IMODE_OUTPUT */
87
88 static int      dma8 = 1;
89
90 #ifdef JAZZ16 /* 16 bit support for JAZZ16 */
91
92 static int      dsp_16bit = 0;
93 static int      dma16 = 5;
94
95 static int      dsp_set_bits(int arg);
96 static int      initialize_ProSonic16(void);
97 #endif          /* end of 16 bit support for JAZZ16 */
98
99 int             sb_duplex_midi = 0;
100 static int      my_dev = 0;
101
102 volatile int    sb_intr_active = 0;
103
104 static int      dsp_speed(int);
105 static int      dsp_set_stereo(int mode);
106 static void     sb_dsp_reset(int dev);
107 sound_os_info  *sb_osp = NULL;
108
109 #if defined(CONFIG_MIDI) || defined(CONFIG_AUDIO)
110 static void dsp_speaker(char state);
111
112 /*
113  * Common code for the midi and pcm functions
114  */
115
116 int
117 sb_dsp_command(u_char val)
118 {
119     int             i;
120     u_long   limit;
121
122     limit = get_time() + hz / 10;       /* The timeout is 0.1 secods */
123
124     /*
125      * Note! the i<500000 is an emergency exit. The sb_dsp_command() is
126      * sometimes called while interrupts are disabled. This means that
127      * the timer is disabled also. However the timeout situation is a
128      * abnormal condition. Normally the DSP should be ready to accept
129      * commands after just couple of loops.
130      */
131
132     for (i = 0; i < 500000 && get_time() < limit; i++) {
133         if ((inb(DSP_STATUS) & 0x80) == 0) {
134             outb(DSP_COMMAND, val);
135             return 1;
136         }
137     }
138
139     printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val);
140     return 0;
141 }
142
143 void
144 sbintr(int irq)
145 {
146     int             status;
147
148 #ifdef CONFIG_SBPRO
149     if (sb16) {
150         u_char   src = sb_getmixer(IRQ_STAT); /* Interrupt source register */
151 #ifdef CONFIG_SB16
152         if (src & 3)
153             sb16_dsp_interrupt(irq);
154 #ifdef CONFIG_MIDI
155         if (src & 4)
156             sb16midiintr(irq);  /* SB MPU401 interrupt */
157 #endif  /* CONFIG_MIDI */
158 #endif  /* CONFIG_SB16 */
159         if (!(src & 1))
160             return;     /* Not a DSP interupt */
161     }
162 #endif  /* CONFIG_SBPRO */
163
164     status = inb(DSP_DATA_AVAIL);       /* Clear interrupt */
165
166     if (sb_intr_active)
167         switch (sb_irq_mode) {
168         case IMODE_OUTPUT:
169             sb_intr_active = 0;
170             DMAbuf_outputintr(my_dev, 1);
171             break;
172
173         case IMODE_INPUT:
174             sb_intr_active = 0;
175             DMAbuf_inputintr(my_dev);
176             /*
177              * A complete buffer has been input. Let's start new one
178              */
179             break;
180
181         case IMODE_INIT:
182             sb_intr_active = 0;
183             break;
184
185         case IMODE_MIDI:
186 #ifdef CONFIG_MIDI
187             sb_midi_interrupt(irq);
188 #endif
189             break;
190
191         default:
192             printf("SoundBlaster: Unexpected interrupt\n");
193         }
194 }
195
196
197 int
198 sb_reset_dsp(void)
199 {
200     int             loopc;
201
202     outb(DSP_RESET, 1);
203     DELAY(10);
204     outb(DSP_RESET, 0);
205     DELAY(30);
206
207
208     for (loopc = 0; loopc < 100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
209         DELAY(10);
210
211     if (inb(DSP_READ) != 0xAA) {
212         printf("sb_reset_dsp failed\n");
213         return 0;       /* Sorry */
214     }
215
216     return 1;
217 }
218
219 #endif
220
221 #ifdef CONFIG_AUDIO
222
223 static void
224 dsp_speaker(char state)
225 {
226     if (state)
227         sb_dsp_command(DSP_CMD_SPKON);
228     else
229         sb_dsp_command(DSP_CMD_SPKOFF);
230 }
231
232 static int
233 dsp_speed(int speed)
234 {
235     u_char   tconst;
236     u_long   flags;
237     int             max_speed = 44100;
238
239     if (speed < 4000)
240             speed = 4000;
241
242     /*
243      * Older SB models don't support higher speeds than 22050.
244      */
245
246     if (sbc_major < 2 || (sbc_major == 2 && sbc_minor == 0))
247         max_speed = 22050;
248
249     /*
250      * SB models earlier than SB Pro have low limit for the input speed.
251      */
252     if (open_mode != OPEN_WRITE) {      /* Recording is possible */
253         if (sbc_major < 3) { /* Limited input speed with these cards */
254             if (sbc_major == 2 && sbc_minor > 0)
255                 max_speed = 15000;
256             else
257                 max_speed = 13000;
258         }
259     }
260     if (speed > max_speed)
261         speed = max_speed;      /* Invalid speed */
262
263     /*
264      * Logitech SoundMan Games and Jazz16 cards can support 44.1kHz
265      * stereo
266      */
267 #if !defined (SM_GAMES)
268     /*
269      * Max. stereo speed is 22050
270      */
271     if (dsp_stereo && speed > 22050 && Jazz16_detected == 0)
272         speed = 22050;
273 #endif
274
275     if ((speed > 22050) && sb_midi_busy) {
276         printf("SB Warning: High speed DSP not possible simultaneously with MIDI output\n");
277         speed = 22050;
278     }
279     if (dsp_stereo)
280         speed *= 2;
281
282     /*
283      * Now the speed should be valid
284      */
285
286     if (speed > 22050) {        /* High speed mode */
287         int             tmp;
288
289         tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8);
290         sb_dsp_highspeed = 1;
291
292         flags = splhigh();
293         if (sb_dsp_command(DSP_CMD_TCONST))
294             sb_dsp_command(tconst);
295         splx(flags);
296
297         tmp = 65536 - (tconst << 8);
298         speed = (256000000 + tmp / 2) / tmp;
299     } else {
300         int             tmp;
301
302         sb_dsp_highspeed = 0;
303         tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
304
305         flags = splhigh();
306         if (sb_dsp_command(DSP_CMD_TCONST))     /* Set time constant */
307             sb_dsp_command(tconst);
308         splx(flags);
309
310         tmp = 256 - tconst;
311         speed = (1000000 + tmp / 2) / tmp;
312     }
313
314     if (dsp_stereo)
315         speed /= 2;
316
317     dsp_current_speed = speed;
318     return speed;
319 }
320
321 static int
322 dsp_set_stereo(int mode)
323 {
324     dsp_stereo = 0;
325
326 #ifndef CONFIG_SBPRO
327     return 0;
328 #else
329     if (sbc_major < 3 || sb16)
330         return 0;       /* Sorry no stereo */
331
332     if (mode && sb_midi_busy) {
333         printf("SB Warning: Stereo DSP not possible simultaneously with MIDI output\n");
334         return 0;
335     }
336     dsp_stereo = !!mode;
337     return dsp_stereo;
338 #endif
339 }
340
341 static void
342 sb_dsp_output_block(int dev, u_long buf, int count,
343                     int intrflag, int restart_dma)
344 {
345     u_long   flags;
346
347     if (!sb_irq_mode)
348         dsp_speaker(ON);
349
350     DMAbuf_start_dma(dev, buf, count, 1);
351
352     sb_irq_mode = 0;
353
354     if (audio_devs[dev]->dmachan1 > 3)
355         count >>= 1;
356     count--;
357     dsp_count = count;
358
359     sb_irq_mode = IMODE_OUTPUT;
360     if (sb_dsp_highspeed) {
361         flags = splhigh();
362         if (sb_dsp_command(DSP_CMD_HSSIZE)) {   /* High speed size */
363             sb_dsp_command((u_char) (dsp_count & 0xff));
364             sb_dsp_command((u_char) ((dsp_count >> 8) & 0xff));
365             sb_dsp_command(DSP_CMD_HSDAC);      /* High speed 8 bit DAC */
366         } else
367             printf("SB Error: Unable to start (high speed) DAC\n");
368         splx(flags);
369     } else {
370         flags = splhigh();
371         if (sb_dsp_command(DSP_CMD_DAC8)) {     /* 8-bit DAC (DMA) */
372             sb_dsp_command((u_char) (dsp_count & 0xff));
373             sb_dsp_command((u_char) ((dsp_count >> 8) & 0xff));
374         } else
375             printf("SB Error: Unable to start DAC\n");
376         splx(flags);
377     }
378     sb_intr_active = 1;
379 }
380
381 static void
382 sb_dsp_start_input(int dev, u_long buf, int count, int intrflag,
383                    int restart_dma)
384 {
385     u_long   flags;
386
387     if (sb_no_recording) {
388         printf("SB Error: This device doesn't support recording\n");
389         return;
390     }
391     /*
392      * Start a DMA input to the buffer pointed by dmaqtail
393      */
394
395     if (!sb_irq_mode)
396         dsp_speaker(OFF);
397
398     DMAbuf_start_dma(dev, buf, count, 0);
399     sb_irq_mode = 0;
400
401     if (audio_devs[dev]->dmachan1 > 3)
402         count >>= 1;
403     count--;
404     dsp_count = count;
405
406     sb_irq_mode = IMODE_INPUT;
407     if (sb_dsp_highspeed) {
408         flags = splhigh();
409         if (sb_dsp_command(DSP_CMD_HSSIZE)) {   /* High speed size */
410             sb_dsp_command((u_char) (dsp_count & 0xff));
411             sb_dsp_command((u_char) ((dsp_count >> 8) & 0xff));
412             sb_dsp_command(DSP_CMD_HSADC);      /* High speed 8 bit ADC */
413         } else
414             printf("SB Error: Unable to start (high speed) ADC\n");
415         splx(flags);
416     } else {
417         flags = splhigh();
418         if (sb_dsp_command(DSP_CMD_ADC8)) {     /* 8-bit ADC (DMA) */
419             sb_dsp_command((u_char) (dsp_count & 0xff));
420             sb_dsp_command((u_char) ((dsp_count >> 8) & 0xff));
421         } else
422             printf("SB Error: Unable to start ADC\n");
423         splx(flags);
424     }
425
426     sb_intr_active = 1;
427 }
428
429 static void
430 sb_dsp_trigger(int dev, int bits)
431 {
432     if (bits == trigger_bits)
433         return;
434
435     if (!bits)
436         sb_dsp_command(0xd0);   /* Halt DMA */
437     else if (bits & sb_irq_mode)
438         sb_dsp_command(0xd4);   /* Continue DMA */
439
440     trigger_bits = bits;
441 }
442
443 static void
444 dsp_cleanup(void)
445 {
446     sb_intr_active = 0;
447 }
448
449 static int
450 sb_dsp_prepare_for_input(int dev, int bsize, int bcount)
451 {
452     int fudge = -1;
453     struct dma_buffparms *dmap =  audio_devs[dev]->dmap_in;
454
455     dsp_cleanup();
456     dsp_speaker(OFF);
457
458     if (sbc_major == 3) {       /* SB Pro */
459 #ifdef JAZZ16
460         /*
461          * Select correct dma channel for 16/8 bit acccess
462          */
463         audio_devs[my_dev]->dmachan1 = dsp_16bit ? dma16 : dma8;
464         if (dsp_stereo)
465             sb_dsp_command(dsp_16bit ? 0xac : 0xa8);
466         else
467             sb_dsp_command(dsp_16bit ? 0xa4 : 0xa0);
468 #else
469         /*
470          * 8 bit only cards use this
471          */
472         if (dsp_stereo)
473             sb_dsp_command(0xa8);
474         else
475             sb_dsp_command(0xa0);
476 #endif
477         dsp_speed(dsp_current_speed);   /* Speed must be recalculated
478                                          * if #channels * changes */
479     }
480
481     fudge = audio_devs[my_dev]->dmachan1;
482     if (dmap->dma_chan != fudge ) {
483       isa_dma_release( dmap->dma_chan);
484       isa_dma_acquire(fudge);
485       dmap->dma_chan = fudge;
486     }
487
488     trigger_bits = 0;
489     sb_dsp_command(DSP_CMD_DMAHALT);    /* Halt DMA */
490     return 0;
491 }
492
493 static int
494 sb_dsp_prepare_for_output(int dev, int bsize, int bcount)
495 {
496
497     int fudge;
498     struct dma_buffparms *dmap =  audio_devs[dev]->dmap_out;
499
500     dsp_cleanup();
501     dsp_speaker(ON);
502
503 #ifdef CONFIG_SBPRO
504     if (sbc_major == 3) {       /* SB Pro */
505 #ifdef JAZZ16
506         /*
507          * 16 bit specific instructions
508          */
509         audio_devs[my_dev]->dmachan1 = dsp_16bit ? dma16 : dma8;
510
511         if (Jazz16_detected != 2)       /* SM Wave */
512             sb_mixer_set_stereo(dsp_stereo);
513         if (dsp_stereo)
514             sb_dsp_command(dsp_16bit ? 0xac : 0xa8);
515         else
516             sb_dsp_command(dsp_16bit ? 0xa4 : 0xa0);
517 #else
518         sb_mixer_set_stereo(dsp_stereo);
519 #endif
520         dsp_speed(dsp_current_speed);   /* Speed must be recalculated
521                                          * if #channels * changes */
522     }
523 #endif
524     fudge = audio_devs[my_dev]->dmachan1;
525
526     if (dmap->dma_chan != fudge ) {
527       isa_dma_release( dmap->dma_chan);
528       isa_dma_acquire(fudge);
529       dmap->dma_chan = fudge;
530     }
531
532     trigger_bits = 0;
533     sb_dsp_command(DSP_CMD_DMAHALT);    /* Halt DMA */
534     return 0;
535 }
536
537 static void
538 sb_dsp_halt_xfer(int dev)
539 {
540 }
541
542 static int
543 sb_dsp_open(int dev, int mode)
544 {
545     if (!sb_dsp_ok) {
546         printf("SB Error: SoundBlaster board not installed\n");
547         return -(ENXIO);
548     }
549     if (sb_no_recording && mode & OPEN_READ) {
550         printf("SB Warning: Recording not supported by this device\n");
551     }
552     if (sb_intr_active || (sb_midi_busy && sb_midi_mode == UART_MIDI)) {
553         printf("SB: PCM not possible during MIDI input\n");
554         return -(EBUSY);
555     }
556     /*
557      * Allocate 8 bit dma
558      */
559 #ifdef JAZZ16
560     audio_devs[my_dev]->dmachan1 = dma8;
561     /*
562      * Allocate 16 bit dma
563      */
564     if (Jazz16_detected != 0)
565         if (dma16 != dma8) {
566             if (0) {
567                 return -(EBUSY);
568             }
569         }
570 #endif
571
572     sb_irq_mode = IMODE_NONE;
573
574     sb_dsp_busy = 1;
575     open_mode = mode;
576
577
578     return 0;
579 }
580
581 static void
582 sb_dsp_close(int dev)
583 {
584 #ifdef JAZZ16
585     /*
586      * Release 16 bit dma channel
587      */
588     if (Jazz16_detected) {
589         audio_devs[my_dev]->dmachan1 = dma8;
590
591     }
592 #endif
593
594     dsp_cleanup();
595     dsp_speaker(OFF);
596     sb_dsp_busy = 0;
597     sb_dsp_highspeed = 0;
598     open_mode = 0;
599
600 }
601
602 #ifdef JAZZ16
603 /*
604  * Function dsp_set_bits() only required for 16 bit cards
605  */
606 static int
607 dsp_set_bits(int arg)
608 {
609     if (arg)
610         if (Jazz16_detected == 0)
611             dsp_16bit = 0;
612         else
613             switch (arg) {
614             case 8:
615                 dsp_16bit = 0;
616                 break;
617             case 16:
618                 dsp_16bit = 1;
619                 break;
620             default:
621                 dsp_16bit = 0;
622             }
623     return dsp_16bit ? 16 : 8;
624 }
625
626 #endif                          /* ifdef JAZZ16 */
627
628 static int
629 sb_dsp_ioctl(int dev, u_int cmd, ioctl_arg arg, int local)
630 {
631     switch (cmd) {
632     case SOUND_PCM_WRITE_RATE:
633         if (local)
634             return dsp_speed((int) arg);
635         return *(int *) arg = dsp_speed((*(int *) arg));
636         break;
637
638     case SOUND_PCM_READ_RATE:
639         if (local)
640             return dsp_current_speed;
641         return *(int *) arg = dsp_current_speed;
642         break;
643
644     case SOUND_PCM_WRITE_CHANNELS:
645         if (local)
646             return dsp_set_stereo((int) arg - 1) + 1;
647         return *(int *) arg = dsp_set_stereo((*(int *) arg) - 1) + 1;
648         break;
649
650     case SOUND_PCM_READ_CHANNELS:
651         if (local)
652             return dsp_stereo + 1;
653         return *(int *) arg = dsp_stereo + 1;
654         break;
655
656     case SNDCTL_DSP_STEREO:
657         if (local)
658             return dsp_set_stereo((int) arg);
659         return *(int *) arg = dsp_set_stereo((*(int *) arg));
660         break;
661
662 #ifdef JAZZ16
663     /*
664      * Word size specific cases here.
665      * SNDCTL_DSP_SETFMT=SOUND_PCM_WRITE_BITS
666      */
667     case SNDCTL_DSP_SETFMT:
668         if (local)
669             return dsp_set_bits((int) arg);
670         return *(int *) arg = dsp_set_bits((*(int *) arg));
671         break;
672
673     case SOUND_PCM_READ_BITS:
674         if (local)
675             return dsp_16bit ? 16 : 8;
676         return *(int *) arg = dsp_16bit ? 16 : 8;
677         break;
678 #else
679     case SOUND_PCM_WRITE_BITS:
680     case SOUND_PCM_READ_BITS:
681         if (local)
682             return 8;
683         return *(int *) (int) arg = 8;  /* Only 8 bits/sample supported */
684         break;
685 #endif                          /* ifdef JAZZ16  */
686
687     case SOUND_PCM_WRITE_FILTER:
688     case SOUND_PCM_READ_FILTER:
689         return -(EINVAL);
690         break;
691
692     default:;
693     }
694
695     return -(EINVAL);
696 }
697
698 static void
699 sb_dsp_reset(int dev)
700 {
701     u_long   flags;
702
703     flags = splhigh();
704
705     sb_reset_dsp();
706     dsp_speed(dsp_current_speed);
707     dsp_cleanup();
708
709     splx(flags);
710 }
711
712 #endif
713
714
715 #ifdef JAZZ16
716
717 /*
718  * Initialization of a Media Vision ProSonic 16 Soundcard. The function
719  * initializes a ProSonic 16 like PROS.EXE does for DOS. It sets the base
720  * address, the DMA-channels, interrupts and enables the joystickport.
721  * 
722  * Also used by Jazz 16 (same card, different name)
723  * 
724  * written 1994 by Rainer Vranken E-Mail:
725  * rvranken@polaris.informatik.uni-essen.de
726  */
727
728 u_int
729 get_sb_byte(void)
730 {
731     int             i;
732
733     for (i = 1000; i; i--)
734         if (inb(DSP_DATA_AVAIL) & 0x80) {
735             return inb(DSP_READ);
736         }
737     return 0xffff;
738 }
739
740 #ifdef SM_WAVE
741 /*
742  * Logitech Soundman Wave detection and initialization by Hannu Savolainen.
743  * 
744  * There is a microcontroller (8031) in the SM Wave card for MIDI emulation.
745  * it's located at address MPU_BASE+4.  MPU_BASE+7 is a SM Wave specific
746  * control register for MC reset, SCSI, OPL4 and DSP (future expansion)
747  * address decoding. Otherwise the SM Wave is just a ordinary MV Jazz16 based
748  * soundcard.
749  */
750
751 static void
752 smw_putmem(int base, int addr, u_char val)
753 {
754     u_long   flags;
755
756     flags = splhigh();
757
758     outb(base + 1, addr & 0xff);        /* Low address bits */
759     outb(base + 2, addr >> 8);  /* High address bits */
760     outb(base, val);    /* Data */
761
762     splx(flags);
763 }
764
765 static u_char
766 smw_getmem(int base, int addr)
767 {
768     u_long   flags;
769     u_char   val;
770
771     flags = splhigh();
772
773     outb(base + 1, addr & 0xff);        /* Low address bits */
774     outb(base + 2, addr >> 8);  /* High address bits */
775     val = inb(base);    /* Data */
776
777     splx(flags);
778     return val;
779 }
780
781 #ifdef SMW_MIDI0001_INCLUDED
782 #include </sys/i386/isa/sound/smw-midi0001.h>
783 #else
784 u_char  *smw_ucode = NULL;
785 int             smw_ucodeLen = 0;
786
787 #endif
788
789 static int
790 initialize_smw(int mpu_base)
791 {
792
793     int             mp_base = mpu_base + 4;     /* Microcontroller base */
794     int             i;
795     u_char   control;
796
797
798     /*
799      * Reset the microcontroller so that the RAM can be accessed
800      */
801
802     control = inb(mpu_base + 7);
803     outb(mpu_base + 7, control | 3);    /* Set last two bits to 1 (?) */
804     outb(mpu_base + 7, (control & 0xfe) | 2);   /* xxxxxxx0 resets the mc */
805     DELAY(3000); /* Wait at least 1ms */
806         
807     outb(mpu_base + 7, control & 0xfc); /* xxxxxx00 enables RAM */
808
809     /*
810      * Detect microcontroller by probing the 8k RAM area
811      */
812     smw_putmem(mp_base, 0, 0x00);
813     smw_putmem(mp_base, 1, 0xff);
814     DELAY(10);
815
816     if (smw_getmem(mp_base, 0) != 0x00 || smw_getmem(mp_base, 1) != 0xff) {
817         printf("\nSM Wave: No microcontroller RAM detected (%02x, %02x)\n",
818                smw_getmem(mp_base, 0), smw_getmem(mp_base, 1));
819         return 0;       /* No RAM */
820     }
821     /*
822      * There is RAM so assume it's really a SM Wave
823      */
824
825     if (smw_ucodeLen > 0) {
826         if (smw_ucodeLen != 8192) {
827             printf("\nSM Wave: Invalid microcode (MIDI0001.BIN) length\n");
828             return 1;
829         }
830         /*
831          * Download microcode
832          */
833
834         for (i = 0; i < 8192; i++)
835             smw_putmem(mp_base, i, smw_ucode[i]);
836
837         /*
838          * Verify microcode
839          */
840
841         for (i = 0; i < 8192; i++)
842             if (smw_getmem(mp_base, i) != smw_ucode[i]) {
843                 printf("SM Wave: Microcode verification failed\n");
844                 return 0;
845             }
846     }
847     control = 0;
848 #ifdef SMW_SCSI_IRQ
849     /*
850      * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
851      * is disabled by default.
852      * 
853      * Btw the Zilog 5380 SCSI controller is located at MPU base + 0x10.
854      */
855     {
856         static u_char scsi_irq_bits[] =
857             {0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0};
858
859         control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
860     }
861 #endif
862
863 #ifdef SMW_OPL4_ENABLE
864     /*
865      * Make the OPL4 chip visible on the PC bus at 0x380.
866      * 
867      * There is no need to enable this feature since VoxWare doesn't support
868      * OPL4 yet. Also there is no RAM in SM Wave so enabling OPL4 is
869      * pretty useless.
870      */
871     control |= 0x10;    /* Uses IRQ12 if bit 0x20 == 0 */
872     /* control |= 0x20;      Uncomment this if you want to use IRQ7 */
873 #endif
874
875     outb(mpu_base + 7, control | 0x03); /* xxxxxx11 restarts */
876     return 1;
877 }
878
879 #endif
880
881 static int
882 initialize_ProSonic16(void)
883 {
884     int             x;
885     static u_char int_translat[16] =
886             {0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6},
887         dma_translat[8] =
888             {0, 1, 0, 2, 0, 3, 0, 4};
889
890     struct address_info *mpu_config;
891
892     int             mpu_base, mpu_irq;
893
894     if ((mpu_config = sound_getconf(SNDCARD_MPU401))) {
895         mpu_base = mpu_config->io_base;
896         mpu_irq = mpu_config->irq;
897     } else {
898         mpu_base = mpu_irq = 0;
899     }
900
901     outb(0x201, 0xAF);  /* ProSonic/Jazz16 wakeup */
902     DELAY(15000);       /* wait at least 10 milliseconds */
903     outb(0x201, 0x50);
904     outb(0x201, (sbc_base & 0x70) | ((mpu_base & 0x30) >> 4));
905
906     if (sb_reset_dsp()) {       /* OK. We have at least a SB */
907
908         /* Check the version number of ProSonic (I guess) */
909
910         if (!sb_dsp_command(0xFA))
911             return 1;
912         if (get_sb_byte() != 0x12)
913             return 1;
914
915         if (sb_dsp_command(0xFB) &&     /* set DMA-channels and Interrupts */
916             sb_dsp_command((dma_translat[JAZZ_DMA16]<<4)|dma_translat[dma8]) &&
917             sb_dsp_command((int_translat[mpu_irq]<<4)|int_translat[sbc_irq])) {
918             Jazz16_detected = 1;
919             if (mpu_base == 0)
920                 printf("Jazz16: No MPU401 devices configured - MIDI port not initialized\n");
921
922 #ifdef SM_WAVE
923                 if (mpu_base != 0)
924                     if (initialize_smw(mpu_base))
925                         Jazz16_detected = 2;
926 #endif
927             sb_dsp_disable_midi();
928         }
929         return 1;       /* There was at least a SB */
930     }
931     return 0;           /* No SB or ProSonic16 detected */
932 }
933
934 #endif                          /* ifdef JAZZ16  */
935
936 int
937 sb_dsp_detect(struct address_info * hw_config)
938 {
939     sbc_base = hw_config->io_base;
940     sbc_irq = hw_config->irq;
941     sb_osp = hw_config->osp;
942
943
944     if (sb_dsp_ok)
945         return 0;       /* Already initialized */
946     dma8 = hw_config->dma;
947
948 #ifdef JAZZ16
949     dma16 = JAZZ_DMA16;
950
951     if (!initialize_ProSonic16())
952         return 0;
953 #else
954     if (!sb_reset_dsp())
955         return 0;
956 #endif
957 #ifdef PC98
958     switch (sbc_irq) {
959     case 3:
960         sb_setmixer (IRQ_NR, 1);
961         break;
962     case 5:
963         sb_setmixer (IRQ_NR, 8);
964         break;
965     case 10:
966         sb_setmixer (IRQ_NR, 2);
967         break;
968     }
969     switch (hw_config->dma) {
970     case 0:
971         sb_setmixer (DMA_NR, 1);
972         break;
973     case 3:
974         sb_setmixer (DMA_NR, 2);
975         break;
976     }
977 #endif 
978
979     return 1;           /* Detected */
980 }
981
982 #ifdef CONFIG_AUDIO
983 static struct audio_operations sb_dsp_operations =
984 {
985     "SoundBlaster",
986     NOTHING_SPECIAL,
987     AFMT_U8,            /* Just 8 bits. Poor old SB */
988     NULL,
989     sb_dsp_open,
990     sb_dsp_close,
991     sb_dsp_output_block,
992     sb_dsp_start_input,
993     sb_dsp_ioctl,
994     sb_dsp_prepare_for_input,
995     sb_dsp_prepare_for_output,
996     sb_dsp_reset,
997     sb_dsp_halt_xfer,
998     NULL,                       /* local_qlen */
999     NULL,                       /* copy_from_user */
1000     NULL,
1001     NULL,
1002     sb_dsp_trigger
1003 };
1004
1005 #endif
1006
1007 void
1008 sb_dsp_init(struct address_info * hw_config)
1009 {
1010     int             i;
1011     char *fmt = NULL ;
1012
1013 #ifdef CONFIG_SBPRO
1014     int             mixer_type = 0;
1015
1016 #endif
1017
1018     sb_osp = hw_config->osp;
1019     sbc_major = sbc_minor = 0;
1020     sb_dsp_command(DSP_CMD_GETVER);     /* Get version */
1021
1022     for (i = 10000; i; i--) { /* perhaps wait longer on a fast machine ? */
1023         if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
1024             if (sbc_major == 0)
1025                 sbc_major = inb(DSP_READ);
1026             else {
1027                 sbc_minor = inb(DSP_READ);
1028                 break;
1029             }
1030         } else
1031             DELAY(20);
1032     }
1033
1034     if (sbc_major == 0) {
1035         printf("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
1036                inb(DSP_DATA_AVAIL));
1037         sbc_major = 1;
1038     }
1039     if (sbc_major == 2 || sbc_major == 3)
1040         sb_duplex_midi = 1;
1041
1042     if (sbc_major == 4)
1043         sb16 = 1;
1044
1045     if (sbc_major == 3 && sbc_minor == 1) {
1046         int             ess_major = 0, ess_minor = 0;
1047
1048         /*
1049          * Try to detect ESS chips.
1050          */
1051
1052         sb_dsp_command(DSP_CMD_GETID);  /* Return identification bytes. */
1053
1054         for (i = 1000; i; i--) {
1055             if (inb(DSP_DATA_AVAIL) & 0x80) {   /* wait for Data Ready */
1056                 if (ess_major == 0)
1057                     ess_major = inb(DSP_READ);
1058                 else {
1059                     ess_minor = inb(DSP_READ);
1060                     break;
1061                 }
1062             }
1063         }
1064
1065         if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
1066             printf("Hmm... Could this be an ESS488 based card (rev %d)\n",
1067                ess_minor & 0x0f);
1068         else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
1069             printf("Hmm... Could this be an ESS688 based card (rev %d)\n",
1070                ess_minor & 0x0f);
1071     }
1072     if (snd_set_irq_handler(sbc_irq, sbintr, sb_osp) < 0)
1073         printf("sb_dsp: Can't allocate IRQ\n");;
1074
1075 #ifdef CONFIG_SBPRO
1076     if (sbc_major >= 3)
1077         mixer_type = sb_mixer_init(sbc_major);
1078 #else
1079     if (sbc_major >= 3)
1080         printf("\nNOTE! SB Pro support required with your soundcard!\n");
1081 #endif
1082
1083
1084 #ifdef CONFIG_AUDIO
1085     if (sbc_major >= 3) {
1086         if (Jazz16_detected) {
1087             if (Jazz16_detected == 2)
1088                 fmt = "SoundMan Wave %d.%d";
1089             else
1090                 fmt = "MV Jazz16 %d.%d";
1091             sb_dsp_operations.format_mask |= AFMT_S16_LE;       /* 16 bits */
1092         } else
1093 #ifdef __SGNXPRO__
1094         if (mixer_type == 2)
1095             fmt = "Sound Galaxy NX Pro %d.%d" ;
1096         else
1097 #endif  /* __SGNXPRO__ */
1098         if (sbc_major == 4)
1099             fmt = "SoundBlaster 16 %d.%d";
1100         else
1101             fmt = "SoundBlaster Pro %d.%d";
1102     } else {
1103         fmt = "SoundBlaster %d.%d" ;
1104     }
1105
1106     snprintf(sb_dsp_operations.name, sizeof(sb_dsp_operations.name),
1107         fmt, sbc_major, sbc_minor);
1108     conf_printf(sb_dsp_operations.name, hw_config);
1109
1110 #if defined(CONFIG_SB16) && defined(CONFIG_SBPRO)
1111     if (!sb16) {                /* There is a better driver for SB16 */
1112 #endif  /* CONFIG_SB16 && CONFIG_SBPRO */
1113         if (num_audiodevs < MAX_AUDIO_DEV) {
1114             audio_devs[my_dev = num_audiodevs++] = &sb_dsp_operations;
1115             audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
1116             dma8 = audio_devs[my_dev]->dmachan1 = hw_config->dma;
1117             audio_devs[my_dev]->dmachan2 = -1;
1118 #ifdef JAZZ16
1119             /*
1120              * Allocate 16 bit dma
1121              */
1122             if (Jazz16_detected != 0)
1123                 if (dma16 != dma8) {
1124                     if (0) {
1125                         printf("Jazz16: Can't allocate 16 bit DMA channel\n");
1126                     }
1127                 }
1128 #endif  /* JAZZ16 */
1129         } else
1130             printf("SB: Too many DSP devices available\n");
1131 #if defined(CONFIG_SB16) && defined(CONFIG_SBPRO)
1132     }
1133 #endif  /* CONFIG_SB16 && CONFIG_SBPRO */
1134 #else
1135     conf_printf("SoundBlaster (configured without audio support)", hw_config);
1136 #endif
1137
1138 #ifdef CONFIG_MIDI
1139     if (!midi_disabled && !sb16) {
1140         /*
1141          * Midi don't work in the SB emulation mode of PAS,
1142          * SB16 has better midi interface
1143          */
1144         sb_midi_init(sbc_major);
1145     }
1146 #endif  /* CONFIG_MIDI */
1147     sb_dsp_ok = 1;
1148 }
1149
1150 void
1151 sb_dsp_disable_midi(void)
1152 {
1153     midi_disabled = 1;
1154 }
1155 #endif