sb16.c revision 41514
1/*
2 * sound/sb_dsp.c
3 *
4 * driver for the SoundBlaster and clones.
5 *
6 * Copyright 1997,1998 Luigi Rizzo.
7 *
8 * Derived from files in the Voxware 3.5 distribution,
9 * Copyright by Hannu Savolainen 1994, under the same copyright
10 * conditions.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in
19 *    the documentation and/or other materials provided with the
20 *    distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37/*
38 * use this as a template file for board-specific drivers.
39 * The next two lines (and the final #endif) are in all drivers:
40 */
41
42#include <i386/isa/snd/sound.h>
43#if NPCM > 0
44
45/*
46 * Begin with the board-specific include files...
47 */
48
49#define __SB_MIXER_C__	/* XXX warning... */
50#include  <i386/isa/snd/sbcard.h>
51
52/*
53 * then prototypes of functions which go in the snddev_info
54 * (usually static, unless they are shared by other modules)...
55 */
56
57static	int sb_probe(struct isa_device *dev);
58static	int sb_attach(struct isa_device *dev);
59
60static	d_open_t	sb_dsp_open;
61static	d_close_t	sb_dsp_close;
62static	d_ioctl_t	sb_dsp_ioctl;
63static	irq_proc_t	sb_intr;
64static	snd_callback_t	sb_callback;
65
66/*
67 * and prototypes for other private functions defined in this module.
68 */
69
70static	void sb_dsp_init(snddev_info *d, struct isa_device *dev);
71static	void sb_mix_init(snddev_info *d);
72static int sb_mixer_set(snddev_info *d, int dev, int value);
73static int dsp_speed(snddev_info *d);
74static void sb_mixer_reset(snddev_info *d);
75
76u_int sb_get_byte(int io_base);
77int ess_write(int io_base, u_char reg, int val);
78int ess_read(int io_base, u_char reg);
79
80/*
81 * Then put here the descriptors for the various boards supported
82 * by this module, properly initialized.
83 */
84
85snddev_info sb_op_desc = {
86    "basic soundblaster",
87
88    SNDCARD_SB,
89    sb_probe,
90    sb_attach,
91
92    sb_dsp_open,
93    sb_dsp_close /* sb_close */,
94    NULL /* use generic sndread */,
95    NULL /* use generic sndwrite */,
96    sb_dsp_ioctl,
97    sndselect,
98
99    sb_intr,
100    sb_callback,
101
102    DSP_BUFFSIZE,	/* bufsize */
103
104    AFMT_STEREO | AFMT_U8,		/* audio format */
105
106} ;
107
108/*
109 * Then the file continues with the body of all functions
110 * directly referenced in the descriptor.
111 */
112
113/*
114 * the probe routine for the SoundBlaster only consists in
115 * resetting the dsp and testing if it is there.
116 * Version detection etc. will be done at attach time.
117 *
118 * Remember, ISA probe routines are supposed to return the
119 * size of io space used.
120 */
121
122static int
123sb_probe(struct isa_device *dev)
124{
125    bzero(&pcm_info[dev->id_unit], sizeof(pcm_info[dev->id_unit]) );
126    if (dev->id_iobase == -1) {
127	dev->id_iobase = 0x220;
128	BVDDB(printf("sb_probe: no address supplied, try defaults (0x220,0x240)\n");)
129        if (snd_conflict(dev->id_iobase))
130	    dev->id_iobase = 0x240;
131    }
132    if (snd_conflict(dev->id_iobase))
133	return 0 ;
134
135    if (sb_reset_dsp(dev->id_iobase))
136	return 16 ; /* the SB uses 16 registers... */
137    else
138	return 0;
139}
140
141static int
142sb_attach(struct isa_device *dev)
143{
144    snddev_info *d = &pcm_info[dev->id_unit] ;
145
146    dev->id_alive = 16 ; /* number of io ports */
147    /* should be already set but just in case... */
148    sb_dsp_init(d, dev);
149    return 0 ;
150}
151
152/*
153 * here are the main routines from the switches.
154 */
155
156/*
157 * Unlike MSS, the sb only supports a single open (does not mean
158 * that only a single process is using it, since it can fork
159 * afterwards, or pass the descriptor to another process).
160 *
161 */
162static int
163sb_dsp_open(dev_t dev, int flags, int mode, struct proc * p)
164{
165    snddev_info *d;
166    int unit ;
167
168    dev = minor(dev);
169    unit = dev >> 4 ;
170    d = &pcm_info[unit] ;
171
172    DEB(printf("<%s>%d : open\n", d->name, unit));
173
174    if (d->flags & SND_F_BUSY) {
175	DEB(printf("<%s>%d open: device busy\n", d->name, unit));
176	return EBUSY ;
177    }
178
179    d->wsel.si_pid = 0;
180    d->wsel.si_flags = 0;
181
182    d->rsel.si_pid = 0;
183    d->rsel.si_flags = 0;
184
185    d->dbuf_out.total = d->dbuf_out.prev_total = 0 ;
186    d->dbuf_in.total = d->dbuf_in.prev_total = 0 ;
187
188    d->flags = 0 ;
189    d->bd_flags &= ~BD_F_HISPEED ;
190
191    switch ( dev & 0xf ) {
192    case SND_DEV_DSP16 :
193	if ((d->audio_fmt & AFMT_S16_LE) == 0) {
194	    printf("sorry, 16-bit not supported on SB %d.%02d\n",
195		(d->bd_id >>8) & 0xff, d->bd_id & 0xff);
196	    return ENXIO;
197	}
198	d->play_fmt = d->rec_fmt = AFMT_S16_LE ;
199	break;
200    case SND_DEV_AUDIO :
201	d->play_fmt = d->rec_fmt = AFMT_MU_LAW ;
202	break ;
203    case SND_DEV_DSP :
204	d->play_fmt = d->rec_fmt = AFMT_U8 ;
205	break ;
206    }
207    /*
208     * since the SB is not simmetric, I use the open mode to select
209     * which channel should be privileged, and disable I/O in the
210     * other direction.
211     * In case the board is opened RW, we don't have enough
212     * information on what to do. Temporarily, privilege the
213     * playback channel, which is used more often, and set the other
214     * one to U8.
215     */
216    if ( (flags & FREAD) == 0)		/* opened write only	*/
217	d->rec_fmt = 0 ;
218    else if ( (flags & FWRITE) == 0)	/* opened read only	*/
219	d->play_fmt = 0 ;
220    else				/* opened read/write	*/
221	d->rec_fmt = (d->play_fmt == AFMT_S16_LE) ? AFMT_U8 : AFMT_S16_LE ;
222
223    d->flags |= SND_F_BUSY ;
224    d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED ;
225
226    if (flags & O_NONBLOCK)
227	d->flags |= SND_F_NBIO ;
228
229    sb_reset_dsp(d->io_base);
230    if (d->bd_flags & BD_F_ESS)
231	sb_cmd(d->io_base, 0xc6 ); /* enable extended ESS mode */
232    ask_init(d);
233
234    return 0;
235}
236
237static int
238sb_dsp_close(dev_t dev, int flags, int mode, struct proc * p)
239{
240    int unit;
241    snddev_info *d;
242    u_long s;
243
244    dev = minor(dev);
245    unit = dev >> 4 ;
246    d = &pcm_info[unit] ;
247
248    s = spltty();
249    d->flags |= SND_F_CLOSING ;
250    splx(s);
251    snd_flush(d);
252
253    sb_cmd(d->io_base, DSP_CMD_SPKOFF ); /* XXX useless ? */
254
255    d->flags = 0 ;
256    return 0 ;
257}
258
259static int
260sb_dsp_ioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct proc * p)
261{
262    int unit;
263    snddev_info *d;
264
265    dev = minor(dev);
266    unit = dev >> 4 ;
267    d = &pcm_info[unit] ;
268
269    /*
270     * handle mixer calls first. Reads are in the default handler,
271     * so do not bother about them.
272     */
273    if ( (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0) )
274	return sb_mixer_set(d, cmd & 0xff, *(int *)arg) ;
275
276    /*
277     * for the remaining functions, use the default handler.
278     * ENOSYS means that the default handler should take care
279     * of implementing the ioctl.
280     */
281
282    return ENOSYS ;
283}
284
285static void
286sb_intr(int unit)
287{
288    snddev_info *d = &pcm_info[unit];
289    int reason = 3, c=1, io_base = d->io_base;
290
291    DEB(printf("got sb_intr for unit %d, flags 0x%08lx\n", unit, d->flags));
292
293    /*
294     * SB < 4.0 is half duplex and has only 1 bit for int source,
295     * so we fake it. SB 4.x (SB16) has the int source in a separate
296     * register.
297     * The Vibra16X has separate flags for 8 and 16 bit transfers, but
298     * I have no idea how to tell capture from playback interrupts...
299     */
300#define PLAIN_SB16(x) ( ( (x) & (BD_F_SB16|BD_F_SB16X) ) == BD_F_SB16)
301again:
302    if (d->bd_flags & BD_F_SB16) {
303	c = sb_getmixer(io_base, IRQ_STAT);
304	/* this tells us if the source is 8-bit or 16-bit dma. We
305	 * have to check the io channel to map it to read or write...
306	 */
307	reason = 0 ;
308	if ( c & 1 ) { /* 8-bit dma */
309	    if (d->play_fmt == AFMT_U8 || d->play_fmt == AFMT_MU_LAW )
310		reason |= 1;
311	    if (d->rec_fmt == AFMT_U8 || d->rec_fmt == AFMT_MU_LAW )
312		reason |= 2;
313	}
314	if ( c & 2 ) { /* 16-bit dma */
315	    if (d->play_fmt == AFMT_S16_LE)
316		reason |= 1;
317	    if (d->rec_fmt == AFMT_S16_LE)
318		reason |= 2;
319	}
320    }
321    /* XXX previous location of ack... */
322    DEB(printf("sb_intr, flags 0x%08lx reason %d c 0x%x\n",
323	d->flags, reason, c));
324    if ( reason & 1 ) { /* possibly a write interrupt */
325	if ( d->dbuf_out.dl )
326	    dsp_wrintr(d);
327    }
328    if ( reason & 2 ) {
329	if ( d->dbuf_in.dl )
330	    dsp_rdintr(d);
331    }
332    if ( c & 2 )
333	inb(DSP_DATA_AVL16); /* 16-bit int ack */
334    if (c & 1)
335	inb(DSP_DATA_AVAIL);	/* 8-bit int ack */
336
337    /*
338     * the sb16 might have multiple sources etc.
339     */
340    if (d->bd_flags & BD_F_SB16 && (c & 3) )
341	goto again;
342}
343
344/*
345 * device-specific function called back from the dma module.
346 * The reason of the callback is the second argument.
347 * NOTE: during operations, some ioctl can be called to change
348 * settings (e.g. speed, channels, format), and the default
349 * ioctl handler will just record the change and set the
350 * flag SND_F_INIT. The callback routine is in charge of applying
351 * the changes at the next convenient time (typically, at the
352 * start of operations). For full duplex devices, in some cases the
353 * init requires both channels to be idle.
354 */
355static int
356sb_callback(snddev_info *d, int reason)
357{
358    int rd = reason & SND_CB_RD ;
359    snd_dbuf *b = (rd) ? & (d->dbuf_in) : & (d->dbuf_out) ;
360    int l = b->dl ;
361
362    switch (reason & SND_CB_REASON_MASK) {
363    case SND_CB_INIT : /* called with int enabled and no pending io */
364	/*
365	 * set the speed
366	 */
367	dsp_speed(d);
368	/*
369	 * set the desired DMA blocksize (influences select behaviour)
370	 */
371	snd_set_blocksize(d);
372	/*
373	 * since native mulaw is not present, emulate it.
374	 */
375	if ( (d->play_fmt & AFMT_MU_LAW) || (d->rec_fmt & AFMT_MU_LAW) )
376	    d->flags |= SND_F_XLAT8 ;
377	else
378	    d->flags &= ~SND_F_XLAT8 ;
379
380	/*
381	 * there are too many flavours of SB for my taste... here i try to do
382	 * the proper initialization for each one.
383	 */
384	if (PLAIN_SB16(d->bd_flags)) {
385	    u_char c, c1 ;
386
387	    /* the original SB16 (non-PnP, or PnP, or Vibra16C)
388	     * can do full duplex using one 16-bit channel
389	     * and one 8-bit channel. It needs to be programmed to
390	     * use split format though.
391	     * I DON'T do this for the Vibra16X because I have no idea
392	     * of what needs to be done there...
393	     *
394	     * I use the following algorithm:
395	     * 1. check which direction(s) are active;
396	     * 2. check if we should swap dma channels
397	     * 3. check if we can do the swap.
398	     */
399	    int swap = 1 ; /* default... */
400
401	    if (d->play_fmt == 0) {
402		/* do whatever the read channel wants */
403		if ( d->rec_fmt == AFMT_S16_LE && d->dbuf_in.chan > 4 )
404		    swap = 0;
405		if ( d->rec_fmt != AFMT_S16_LE && d->dbuf_in.chan < 4 )
406		    swap = 0;
407	    } else {
408		/* privilege the write channel */
409		if ( d->play_fmt == AFMT_S16_LE && d->dbuf_out.chan > 4 )
410		    swap = 0;
411		if ( d->play_fmt != AFMT_S16_LE && d->dbuf_out.chan < 4 )
412		    swap = 0;
413		if ( d->rec_fmt ) {
414		    /* check for possible config errors.
415		     * This cannot happen at open time since even in
416		     * case of opening rw we privilege the play
417		     * channel.
418		     */
419		    if (d->rec_fmt == d->play_fmt) {
420			DDB(printf("sorry, read DMA channel unavailable\n"));
421		    }
422		}
423	    }
424	    DEB(printf("sb16: play_fmt %d, rec_fmt %x, swap %d\n",
425		d->play_fmt, d->rec_fmt, swap);)
426	    if (swap) {
427	        int c = d->dbuf_in.chan ;
428		d->dbuf_in.chan = d->dbuf_out.chan;
429		d->dbuf_out.chan = c ;
430	    }
431	} else if (d->bd_flags & BD_F_ESS) {
432	    u_char c ;
433	    if (d->play_fmt == 0) {
434		/* initialize for record */
435		static u_char cmd[] = {
436		    0x51,0xd0,0x71,0xf4,0x51,0x98,0x71,0xbc
437		};
438		ess_write(d->io_base, 0xb8, 0x0e);
439		c = ( ess_read(d->io_base, 0xa8) & 0xfc ) | 1 ;
440		if (d->flags & SND_F_STEREO)
441		    c++ ;
442		ess_write(d->io_base, 0xa8, c);
443		ess_write(d->io_base, 0xb9, 2); /* 4bytes/transfer */
444		/*
445		 * set format in b6, b7
446		 */
447	    } else {
448		/* initialize for play */
449		static u_char cmd[] = {
450		    0x80,0x51,0xd0,0x00,0x71,0xf4,
451		    0x80,0x51,0x98,0x00,0x71,0xbc
452		};
453	    }
454	}
455	reset_dbuf(& (d->dbuf_in), SND_CHAN_RD );
456	reset_dbuf(& (d->dbuf_out), SND_CHAN_WR );
457	break ;
458
459    case SND_CB_START : /* called with int disabled */
460	if (d->bd_flags & BD_F_SB16) {
461	    u_char c, c1 ;
462
463            if (d->bd_flags & BD_F_SB16X) {
464                /* just a guess: on the Vibra16X, the first
465                 * op started takes the first dma channel,
466                 * the second one takes the next...
467                 * The default is to be ready for play.
468                 */
469                int swap = 0 ;
470		DEB(printf("start %s -- now dma %d:%d\n",
471			rd ? "rd" : "wr",
472			d->dbuf_out.chan, d->dbuf_in.chan););
473		/* swap only if both channels are idle
474		 *   play: dl=0, since there is no pause;
475		 *   rec: rl=0
476		 */
477                if ( rd && d->dbuf_out.dl == 0 && d->dbuf_in.rl == 0 ) {
478		    /* must swap channels, but also save dl */
479		    int c = d->dbuf_in.chan ;
480		    int dl = d->dbuf_in.dl ;
481		    d->dbuf_in.chan = d->dbuf_out.chan;
482		    d->dbuf_out.chan = c ;
483		    reset_dbuf(& (d->dbuf_in), SND_CHAN_RD );
484		    reset_dbuf(& (d->dbuf_out), SND_CHAN_WR );
485		    d->dbuf_in.dl = dl ;
486		    printf("swapped -- now dma %d:%d\n",
487			d->dbuf_out.chan, d->dbuf_in.chan);
488                }
489            }
490
491	    /*
492	     * XXX note: c1 and l should be set basing on d->rec_fmt,
493	     * but there is no choice once a 16 or 8-bit channel
494	     * is assigned. This means that if the application
495	     * tries to use a bad format, the sound will not be nice.
496	     */
497	    if ( b->chan > 4
498		 || (rd && d->rec_fmt == AFMT_S16_LE)
499		 || (!rd && d->play_fmt == AFMT_S16_LE)
500	       ) {
501		c = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_DMA16 ;
502		c1 = DSP_F16_SIGNED ;
503		l /= 2 ;
504	    } else {
505		c = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_DMA8 ;
506		c1 = 0 ;
507	    }
508	    c |=  (rd) ? DSP_F16_ADC : DSP_F16_DAC ;
509	    if (d->flags & SND_F_STEREO)
510		c1 |= DSP_F16_STEREO ;
511
512	    sb_cmd(d->io_base, c );
513	    sb_cmd3(d->io_base, c1 , l - 1) ;
514	} else if (d->bd_flags & BD_F_ESS) {
515	    /* XXX this code is still incomplete */
516	    sb_cmd2(d->io_base,  0xb8, rd ? 4 : 0xe ) ; /* auto dma */
517	    sb_cmd2(d->io_base,  0xa8, d->flags & SND_F_STEREO ? 1 : 2) ;
518	    sb_cmd2(d->io_base,  0xb9, 2) ; /* demand dma */
519	} else { /* SBPro -- stereo not supported */
520	    u_char c ;
521	    if (!rd)
522		sb_cmd(d->io_base, DSP_CMD_SPKON);
523	    /* code for the SB2 and SB3, only MONO */
524	    if (d->bd_flags & BD_F_HISPEED)
525		c = (rd) ? 0x98 : 0x90 ;
526	    else
527		c = (rd) ? 0x2c : 0x1c ;
528	    if (d->flags & SND_F_STEREO)
529	        sb_setmixer(d->io_base, 0xe, 2 );
530	    else
531	        sb_setmixer(d->io_base, 0xe, 0 );
532	    /*
533	     * some ESS extensions -- they can do 16 bits
534	     */
535	    if ( (rd && d->rec_fmt == AFMT_S16_LE) ||
536	         (!rd && d->play_fmt == AFMT_S16_LE) ) {
537		c |= 1;
538		l /= 2 ;
539	    }
540	    sb_cmd3(d->io_base, 0x48 , l - 1) ;
541	    sb_cmd(d->io_base, c ) ;
542	}
543	break;
544
545    case SND_CB_ABORT : /* XXX */
546    case SND_CB_STOP :
547	{
548	    int cmd = DSP_CMD_DMAPAUSE_8 ; /* default: halt 8 bit chan */
549	    if ( b->chan > 4
550		 || (rd && d->rec_fmt == AFMT_S16_LE)
551		 || (!rd && d->play_fmt == AFMT_S16_LE)
552	       )
553		cmd = DSP_CMD_DMAPAUSE_16 ;
554	    if (d->bd_flags & BD_F_HISPEED) {
555		sb_reset_dsp(d->io_base);
556		if (d->bd_flags & BD_F_ESS)
557		    sb_cmd(d->io_base, 0xc6 ); /* enable extended ESS mode */
558		d->flags |= SND_F_INIT ;
559	    } else {
560		sb_cmd(d->io_base, cmd); /* pause dma. */
561	       /*
562		* The above seems to have the undocumented side effect of
563		* blocking the other side as well. If the other
564		* channel was active (SB16) I have to re-enable it :(
565		*/
566		if ( (rd && d->dbuf_out.dl) ||
567		     (!rd && d->dbuf_in.dl) )
568		    sb_cmd(d->io_base, cmd == DSP_CMD_DMAPAUSE_8 ?
569			0xd6 : 0xd4); /* continue other dma */
570	    }
571            if (d->bd_flags & BD_F_SB16X) {
572                /* restore possible swapped channels.
573                 * The default is to be ready for play.
574		 * XXX right now, it kills all input on overflow
575                 */
576                if (  rd && d->dbuf_out.dl == 0 ) {
577                        /* must swap channels ? */
578                        int c = d->dbuf_in.chan ;
579                        d->dbuf_in.chan = d->dbuf_out.chan;
580                        d->dbuf_out.chan = c ;
581                        reset_dbuf(& (d->dbuf_in), SND_CHAN_RD );
582                        reset_dbuf(& (d->dbuf_out), SND_CHAN_WR );
583                    printf("restored -- now dma %d:%d\n",
584                        d->dbuf_out.chan, d->dbuf_in.chan);
585                }
586            }
587	}
588	DEB( sb_cmd(d->io_base, DSP_CMD_SPKOFF) ); /* speaker off */
589	break ;
590
591    }
592    return 0 ;
593}
594
595/*
596 * The second part of the file contains all functions specific to
597 * the board and (usually) not exported to other modules.
598 */
599
600int
601sb_reset_dsp(int io_base)
602{
603    int loopc;
604
605    outb(io_base + SBDSP_RST, 3);
606    DELAY(100);
607    outb(io_base + SBDSP_RST, 0);
608    for (loopc = 0; loopc<100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
609	DELAY(30);
610
611    if (inb(DSP_READ) != 0xAA) {
612        DEB(printf("sb_reset_dsp 0x%x failed\n", io_base));
613	return 0;	/* Sorry */
614    }
615    return 1;
616}
617
618/*
619 * only used in sb_attach from here.
620 */
621
622static void
623sb_dsp_init(snddev_info *d, struct isa_device *dev)
624{
625    int i, x;
626    char *fmt = NULL ;
627    int	io_base = dev->id_iobase ;
628
629    d->bd_id = 0 ;
630
631    sb_reset_dsp(io_base);
632    sb_cmd(io_base, DSP_CMD_GETVER);	/* Get version */
633
634    for (i = 10000; i; i--) { /* perhaps wait longer on a fast machine ? */
635	if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
636	    if ( (d->bd_id & 0xff00) == 0)
637		d->bd_id = inb(DSP_READ) << 8; /* major */
638	    else {
639		d->bd_id |= inb(DSP_READ); /* minor */
640		break;
641	    }
642	} else
643	    DELAY(20);
644    }
645
646    /*
647     * now do various initializations depending on board id.
648     */
649
650    fmt = "SoundBlaster %d.%d" ; /* default */
651
652    switch ( d->bd_id >> 8 ) {
653    case 0 :
654	printf("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
655	       inb(DSP_DATA_AVAIL));
656	d->bd_id = 0x100;
657    case 1 : /* old sound blaster has nothing... */
658	break ;
659
660    case 2 :
661	d->dbuf_in.chan = d->dbuf_out.chan ; /* half duplex */
662	d->bd_flags |= BD_F_DUP_MIDI ;
663
664	if (d->bd_id == 0x200)
665	    break ; /* no mixer on the 2.0 */
666	d->bd_flags &= ~BD_F_MIX_MASK ;
667	d->bd_flags |= BD_F_MIX_CT1335 ;
668
669	break ;
670    case 4 :
671	fmt = "SoundBlaster 16 %d.%d";
672	d->audio_fmt |= AFMT_FULLDUPLEX | AFMT_WEIRD | AFMT_S8 | AFMT_S16_LE;
673	d->bd_flags |= BD_F_SB16;
674	d->bd_flags &= ~BD_F_MIX_MASK ;
675	d->bd_flags |= BD_F_MIX_CT1745 ;
676
677	/* soft irq/dma configuration */
678	x = -1 ;
679	if (d->irq == 5) x = 2;
680	else if (d->irq == 7) x = 4;
681	else if (d->irq == 9) x = 1;
682	else if (d->irq == 10) x = 8;
683	if (x == -1)
684	    printf("<%s>%d: bad irq %d (only 5,7,9,10 allowed)\n",
685		d->name, dev->id_unit, d->irq);
686	else
687	    sb_setmixer(io_base, IRQ_NR, x);
688	if (d->dbuf_out.chan == d->dbuf_in.chan) {
689	    printf("WARNING: sb: misconfigured secondary DMA channel\n");
690	}
691	sb_setmixer(io_base, DMA_NR, (1 << d->dbuf_out.chan) | (1 << d->dbuf_in.chan));
692	break ;
693
694    case 3 :
695	d->dbuf_in.chan = d->dbuf_out.chan ; /* half duplex */
696	fmt = "SoundBlaster Pro %d.%d";
697	d->bd_flags |= BD_F_DUP_MIDI ;
698	d->bd_flags &= ~BD_F_MIX_MASK ;
699	d->bd_flags |= BD_F_MIX_CT1345 ;
700	if (d->bd_id == 0x301) {
701	    int ess_major = 0, ess_minor = 0;
702
703	    /*
704	     * Try to detect ESS chips.
705	     */
706
707	    sb_cmd(io_base, DSP_CMD_GETID);	/* Return ident. bytes. */
708
709	    for (i = 1000; i; i--) {
710		if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
711		    if (ess_major == 0)
712			ess_major = inb(DSP_READ);
713		    else {
714			ess_minor = inb(DSP_READ);
715			break;
716		    }
717		} else
718		    DELAY(20);
719	    }
720
721	    if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80) {
722		/* the ESS488 can be treated as an SBPRO */
723		printf("ESS488 (rev %d)\n", ess_minor & 0x0f);
724		break ;
725	    } else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80) {
726		int rev = ess_minor & 0xf ;
727		if ( rev >= 8 )
728		    printf("ESS1868 (rev %d)\n", rev);
729		else
730		    printf("ESS688 (rev %d)\n", rev);
731		/* d->audio_fmt |= AFMT_S16_LE; */ /* not yet... */
732		break ; /* XXX */
733	    } else {
734		printf("Unknown card 0x%x 0x%x -- hope it is SBPRO\n",
735			ess_major, ess_minor);
736		break ;
737	    }
738	}
739
740    }
741
742    snprintf(d->name, sizeof(d->name),
743	fmt, (d->bd_id >> 8) &0xff, d->bd_id & 0xff);
744
745    sb_mix_init(d);
746}
747
748static void
749sb_mix_init(snddev_info *d)
750{
751    switch (d->bd_flags & BD_F_MIX_MASK) {
752    case BD_F_MIX_CT1345 : /* SB 3.0 has 1345 mixer */
753
754	d->mix_devs = SBPRO_MIXER_DEVICES ;
755	d->mix_rec_devs = SBPRO_RECORDING_DEVICES ;
756	d->mix_recsrc = SOUND_MASK_MIC ;
757
758	sb_setmixer(d->io_base, 0, 1 ); /* reset mixer */
759	sb_setmixer(d->io_base, MIC_VOL , 0x6 ); /* mic volume max */
760	sb_setmixer(d->io_base, RECORD_SRC , 0x0 ); /* mic source */
761	sb_setmixer(d->io_base, FM_VOL , 0x0 ); /* no midi */
762	break ;
763
764    case BD_F_MIX_CT1745 : /* SB16 mixer ... */
765
766	d->mix_devs = SB16_MIXER_DEVICES ;
767	d->mix_rec_devs = SB16_RECORDING_DEVICES ;
768	d->mix_recsrc = SOUND_MASK_MIC ;
769    }
770    sb_mixer_reset(d);
771}
772
773/*
774 * Common code for the midi and pcm functions
775 *
776 * sb_cmd write a single byte to the CMD port.
777 * sb_cmd2 write a CMD + 1 byte arg
778 * sb_cmd3 write a CMD + 2 byte arg
779 * sb_get_byte returns a single byte from the DSP data port
780 *
781 * ess_write is actually sb_cmd2
782 * ess_read access ext. regs via sb_cmd(0xc0, reg) followed by sb_get_byte
783 */
784
785int
786sb_cmd(int io_base, u_char val)
787{
788    int  i;
789
790    for (i = 0; i < 1000 ; i++) {
791	if ((inb(io_base + SBDSP_STATUS) & 0x80) == 0) {
792	    outb(io_base + SBDSP_CMD, val);
793	    return 1;
794	}
795	if (i > 10)
796	    DELAY (i > 100 ? 1000 : 10 );
797    }
798
799    printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val);
800    return 0;
801}
802
803int
804sb_cmd3(int io_base, u_char cmd, int val)
805{
806    if (sb_cmd(io_base, cmd)) {
807	sb_cmd(io_base, val & 0xff );
808	sb_cmd(io_base, (val>>8) & 0xff );
809	return 1 ;
810    } else
811	return 0;
812}
813
814int
815sb_cmd2(int io_base, u_char cmd, int val)
816{
817    if (sb_cmd(io_base, cmd)) {
818	sb_cmd(io_base, val & 0xff );
819	return 1 ;
820    } else
821	return 0;
822}
823
824/*
825 * in the SB, there is a set of indirect "mixer" registers with
826 * address at offset 4, data at offset 5
827 */
828void
829sb_setmixer(int io_base, u_int port, u_int value)
830{
831    u_long   flags;
832
833    flags = spltty();
834    outb(io_base + 4, (u_char) (port & 0xff));   /* Select register */
835    DELAY(10);
836    outb(io_base + 5, (u_char) (value & 0xff));
837    DELAY(10);
838    splx(flags);
839}
840
841int
842sb_getmixer(int io_base, u_int port)
843{
844    int             val;
845    u_long   flags;
846
847    flags = spltty();
848    outb(io_base + SB_MIX_ADDR, (u_char) (port & 0xff));   /* Select register */
849    DELAY(10);
850    val = inb(io_base + SB_MIX_DATA);
851    DELAY(10);
852    splx(flags);
853
854    return val;
855}
856
857u_int
858sb_get_byte(int io_base)
859{
860    int             i;
861
862    for (i = 1000; i; i--)
863	if (inb(DSP_DATA_AVAIL) & 0x80)
864	    return inb(DSP_READ);
865	else
866	    DELAY(20);
867    return 0xffff;
868}
869
870int
871ess_write(int io_base, u_char reg, int val)
872{
873    return sb_cmd2(io_base, reg, val);
874}
875
876int
877ess_read(int io_base, u_char reg)
878{
879    if (!sb_cmd(io_base, 0xc0) || !sb_cmd(io_base, reg) )
880	return 0xffff ;
881    return sb_get_byte(io_base);
882}
883
884
885/*
886 * various utility functions for the DSP
887 */
888
889/*
890 * dsp_speed updates the speed setting from the descriptor. make sure
891 * it is called at spltty().
892 * Besides, it takes care of stereo setting.
893 */
894static int
895dsp_speed(snddev_info *d)
896{
897    u_char   tconst;
898    u_long   flags;
899    int max_speed = 44100, speed = d->play_speed ;
900
901    /*
902     * special code for the SB16
903     */
904    if (d->bd_flags & BD_F_SB16) {
905	RANGE (speed, 5000, 45000);
906	d->play_speed = d->rec_speed = speed ;
907	sb_cmd(d->io_base, 0x41);
908	sb_cmd(d->io_base, d->play_speed >> 8 );
909	sb_cmd(d->io_base, d->play_speed & 0xff );
910	sb_cmd(d->io_base, 0x42);
911	sb_cmd(d->io_base, d->rec_speed >> 8 );
912	sb_cmd(d->io_base, d->rec_speed & 0xff );
913	return speed ;
914    }
915
916    /*
917     * special code for the ESS ...
918     */
919    if (d->bd_flags & BD_F_ESS) {
920	int t;
921	RANGE (speed, 5000, 49000);
922	if (speed > 22000) {
923	    t = (795500 + speed / 2) / speed;
924	    speed = (795500 + t / 2) / t ;
925	    t = (256 - t ) | 0x80 ;
926	} else {
927	    t = (397700 + speed / 2) / speed;
928	    speed = (397700 + t / 2) / t ;
929	    t = 128 - t ;
930	}
931	ess_write(d->io_base, 0xa1, t); /* set time constant */
932	d->play_speed = d->rec_speed = speed ;
933	speed = (speed * 9 ) / 20 ;
934	t = 256-7160000/(speed*82);
935	ess_write(d->io_base,0xa2,t);
936	return speed ;
937    }
938
939    /*
940     * This is code for the SB3.x and lower.
941     * Only some models can do stereo, and only if not
942     * simultaneously using midi.
943     * At the moment we do not support either...
944     */
945#if 0
946    d->flags &= ~SND_F_STEREO;
947#endif
948
949    /*
950     * here enforce speed limitations.
951     */
952    if (d->bd_id <= 0x200)
953	max_speed = 22050; /* max 22050 on SB 1.X */
954
955    /*
956     * SB models earlier than SB Pro have low limit for the
957     * input rate. Note that this is only for input, but since
958     * we do not support separate values for rec & play....
959     */
960    if (d->bd_id <= 0x200)
961	max_speed = 13000;
962    else if (d->bd_id < 0x300)
963	max_speed = 15000;
964
965    RANGE(speed, 4000, max_speed);
966
967    if (d->flags & SND_F_STEREO) /* really unused right now... */
968	speed *= 2;
969
970    /*
971     * Now the speed should be valid. Compute the value to be
972     * programmed into the board.
973     */
974
975    if (speed > 22050) { /* High speed mode on 2.01/3.xx */
976	int tmp;
977
978	tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8) ;
979	d->bd_flags |= BD_F_HISPEED ;
980
981	flags = spltty();
982	sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */
983	splx(flags);
984
985	tmp = 65536 - (tconst << 8);
986	speed = (256000000 + tmp / 2) / tmp;
987    } else {
988	int             tmp;
989
990	d->bd_flags &= ~BD_F_HISPEED ;
991	tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
992
993	flags = spltty();
994	sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */
995	splx(flags);
996
997	tmp = 256 - tconst;
998	speed = (1000000 + tmp / 2) / tmp;
999    }
1000
1001    if (d->flags & SND_F_STEREO) /* really unused right now... */
1002	speed /= 2;
1003
1004    d->play_speed = d->rec_speed = speed;
1005    return speed;
1006}
1007
1008/*
1009 * mixer support, originally in sb_mixer.c
1010 */
1011
1012static void
1013sb_set_recsrc(snddev_info *d, int mask)
1014{
1015    u_char recdev ;
1016
1017    mask &= d->mix_rec_devs;
1018    switch (d->bd_flags & BD_F_MIX_MASK) {
1019    case BD_F_MIX_CT1345 :
1020	if (mask == SOUND_MASK_LINE)
1021	    recdev = 6 ;
1022	else if (mask == SOUND_MASK_CD)
1023	    recdev = 2 ;
1024	else { /* default: mic */
1025	    mask =  SOUND_MASK_MIC ;
1026	    recdev = 0 ;
1027	}
1028	sb_setmixer(d->io_base, RECORD_SRC,
1029	    recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 ));
1030	break ;
1031    case BD_F_MIX_CT1745 : /* sb16 */
1032	if (mask == 0)
1033	    mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */
1034	recdev = 0 ;
1035	if (mask & SOUND_MASK_MIC)
1036	    recdev |= 1 ;
1037	if (mask & SOUND_MASK_CD)
1038	    recdev |= 6 ; /* l+r cd */
1039	if (mask & SOUND_MASK_LINE)
1040	    recdev |= 0x18 ; /* l+r line */
1041	if (mask & SOUND_MASK_SYNTH)
1042	    recdev |= 0x60 ; /* l+r midi */
1043	sb_setmixer(d->io_base, SB16_IMASK_L, recdev);
1044	sb_setmixer(d->io_base, SB16_IMASK_R, recdev);
1045	/*
1046	 * since the same volume controls apply to the input and
1047	 * output sections, the best approach to have a consistent
1048	 * behaviour among cards would be to disable the output path
1049	 * on devices which are used to record.
1050	 * However, since users like to have feedback, we only disable
1051	 * the mike -- permanently.
1052	 */
1053        sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1);
1054	break ;
1055    }
1056    d->mix_recsrc = mask;
1057}
1058
1059static void
1060sb_mixer_reset(snddev_info *d)
1061{
1062    int             i;
1063
1064    for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1065	sb_mixer_set(d, i, levels[i]);
1066    if (d->bd_flags & BD_F_SB16) {
1067	sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */
1068	sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */
1069	sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */
1070    }
1071    sb_set_recsrc(d, SOUND_MASK_MIC);
1072}
1073
1074static int
1075sb_mixer_set(snddev_info *d, int dev, int value)
1076{
1077    int left = value & 0x000000ff;
1078    int right = (value & 0x0000ff00) >> 8;
1079    int regoffs;
1080    u_char   val;
1081    mixer_tab *iomap;
1082
1083#ifdef JAZZ16
1084    if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2)
1085        return smw_mixer_set(dev, value);
1086#endif
1087
1088    if (dev == SOUND_MIXER_RECSRC) {
1089	sb_set_recsrc(d, value);
1090	return 0 ;
1091    }
1092    if (left > 100)
1093        left = 100;
1094    if (right > 100)
1095        right = 100;
1096
1097    if (dev > 31)
1098        return EINVAL ;
1099
1100    if (!(d->mix_devs & (1 << dev)))      /* Not supported */
1101        return EINVAL;
1102
1103    switch ( d->bd_flags & BD_F_MIX_MASK ) {
1104    default:
1105	/* mixer unknown, fail... */
1106	return EINVAL ;/* XXX change this */
1107    case BD_F_MIX_CT1345 :
1108	iomap = &sbpro_mix ;
1109	break;
1110    case BD_F_MIX_CT1745 :
1111	iomap = &sb16_mix ;
1112	break;
1113    /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
1114    }
1115    regoffs = (*iomap)[dev][LEFT_CHN].regno;
1116    if (regoffs == 0)
1117        return EINVAL;
1118
1119    val = sb_getmixer(d->io_base, regoffs);
1120
1121    change_bits(iomap, &val, dev, LEFT_CHN, left);
1122
1123    d->mix_levels[dev] = left | (left << 8);
1124
1125    if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) {    /* Change register */
1126        sb_setmixer(d->io_base, regoffs, val);     /* Save the old one */
1127        regoffs = (*iomap)[dev][RIGHT_CHN].regno;
1128
1129        if (regoffs == 0)
1130            return 0 ;  /* Just left channel present */
1131
1132        val = sb_getmixer(d->io_base, regoffs);    /* Read the new one */
1133    }
1134    change_bits(iomap, &val, dev, RIGHT_CHN, right);
1135
1136    sb_setmixer(d->io_base, regoffs, val);
1137
1138    d->mix_levels[dev] = left | (right << 8);
1139    return 0 ; /* ok */
1140}
1141
1142/*
1143 * now support for some PnP boards.
1144 */
1145
1146#if NPNP > 0
1147static char *ess1868_probe(u_long csn, u_long vend_id);
1148static void ess1868_attach(u_long csn, u_long vend_id, char *name,
1149        struct isa_device *dev);
1150
1151static struct pnp_device ess1868 = {
1152        "ESS1868",
1153        ess1868_probe,
1154        ess1868_attach,
1155        &nsnd,  /* use this for all sound cards */
1156        &tty_imask      /* imask */
1157};
1158DATA_SET (pnpdevice_set, ess1868);
1159
1160static char *
1161ess1868_probe(u_long csn, u_long vend_id)
1162{
1163    /*
1164     * pnp X 1 os enable drq0 3 irq0 12 port0 0x240
1165     */
1166    if (vend_id == 0x68187316) {
1167	struct pnp_cinfo d ;
1168	read_pnp_parms ( &d , 1 ) ;
1169	if (d.enable == 0) {
1170	    printf("This is an ESS1868, but LDN 1 is disabled\n");
1171	    return NULL;
1172	}
1173        return "ESS1868" ;
1174    }
1175    return NULL ;
1176}
1177
1178static void
1179ess1868_attach(u_long csn, u_long vend_id, char *name,
1180        struct isa_device *dev)
1181{
1182    struct pnp_cinfo d ;
1183    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1184    int the_irq = 0 ;
1185
1186    tmp_d = sb_op_desc;
1187    snddev_last_probed = &tmp_d;
1188
1189#if 0
1190    read_pnp_parms ( &d , 3 );  /* disable LDN 3 */
1191    d.port[0] = 0 ;
1192    d.enable = 0 ;
1193    write_pnp_parms ( &d , 3 );
1194
1195    read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
1196    d.port[0] = 0 ;
1197    d.enable = 0 ;
1198    write_pnp_parms ( &d , 2 );
1199    read_pnp_parms ( &d , 0 ); /* read config base */
1200    tmp_d.conf_base = d.port[0];
1201    write_pnp_parms ( &d , 0 );
1202#endif
1203
1204    read_pnp_parms ( &d , 1 ) ;
1205    dev->id_iobase = d.port[0];
1206    d.port[1] = 0 ;
1207    d.port[2] = 0 ;
1208    write_pnp_parms ( &d , 1 );
1209    enable_pnp_card();
1210
1211    dev->id_drq = d.drq[0] ; /* primary dma */
1212    dev->id_irq = (1 << d.irq[0] ) ;
1213    dev->id_intr = pcmintr ;
1214    dev->id_flags = 0 /* DV_F_DUAL_DMA | (d.drq[1] ) */;
1215
1216    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1217    pcmattach(dev);
1218}
1219
1220/*
1221 * A driver for some SB16pnp and compatibles...
1222 *
1223 * Avance Asound 100 -- 0x01009305
1224 * Avance Logic ALS100+ -- 0x10019305
1225 * xxx               -- 0x2b008c0e
1226 *
1227 */
1228
1229static char *sb16pnp_probe(u_long csn, u_long vend_id);
1230static void sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1231        struct isa_device *dev);
1232
1233static struct pnp_device sb16pnp = {
1234        "SB16pnp",
1235        sb16pnp_probe,
1236        sb16pnp_attach,
1237        &nsnd,  /* use this for all sound cards */
1238        &tty_imask      /* imask */
1239};
1240DATA_SET (pnpdevice_set, sb16pnp);
1241
1242static char *
1243sb16pnp_probe(u_long csn, u_long vend_id)
1244{
1245    char *s = NULL ;
1246
1247    /*
1248     * The SB16/AWExx cards seem to differ in the fourth byte of
1249     * the vendor id, so I have just masked it for the time being...
1250     * Reported values are:
1251     * SB16 Value PnP:	0x2b008c0e
1252     * SB AWExx PnP:	0x39008c0e 0x9d008c0e 0xc3008c0e
1253     * Vibra16X:        0xf0008c0e
1254     */
1255    if (vend_id == 0xf0008c0e)
1256	s = "Vibra16X" ;
1257    else if ( (vend_id & 0xffffff)  == (0x9d008c0e & 0xffffff) )
1258	s = "SB16 PnP";
1259    else if (vend_id == 0x01009305)
1260        s = "Avance Asound 100" ;
1261    else if (vend_id == 0x10019305)
1262        s = "Avance Logic 100+" ; /* Vibra16X-class */
1263    if (s) {
1264	struct pnp_cinfo d;
1265	read_pnp_parms(&d, 0);
1266	if (d.enable == 0) {
1267	    printf("This is a %s, but LDN 0 is disabled\n", s);
1268	    return NULL ;
1269	}
1270	return s ;
1271    }
1272    return NULL ;
1273}
1274
1275static void
1276sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1277        struct isa_device *dev)
1278{
1279    struct pnp_cinfo d ;
1280    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1281
1282    tmp_d = sb_op_desc;
1283    snddev_last_probed = &tmp_d;
1284
1285    read_pnp_parms ( &d , 0 ) ;
1286    d.port[1] = 0 ; /* only the first address is used */
1287    dev->id_iobase = d.port[0];
1288    tmp_d.synth_base = d.port[2];
1289    write_pnp_parms ( &d , 0 );
1290    enable_pnp_card();
1291
1292    dev->id_drq = d.drq[0] ; /* primary dma */
1293    dev->id_irq = (1 << d.irq[0] ) ;
1294    dev->id_intr = pcmintr ;
1295    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1296
1297    pcm_info[dev->id_unit] = tmp_d; /* pcm_info[] will be reinitialized after */
1298    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1299
1300    if (vend_id == 0x10019305 || vend_id == 0xf0008c0e) {
1301	/*
1302	 * XXX please add here the vend_id for other vibra16X cards...
1303	 * And remember, must change tmp_d, not
1304	 */
1305	tmp_d.bd_flags |= BD_F_SB16X ;
1306    }
1307    pcmattach(dev);
1308}
1309#endif /* NPNP */
1310
1311#endif
1312