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