sb16.c revision 29651
1/*
2 * sound/sb_dsp.c
3 *
4 * driver for the SoundBlaster and clones.
5 *
6 * Copyright 1997 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: 1. Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer. 2.  Redistributions in binary form must reproduce the
17 * above copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36/*
37 * use this as a template file for board-specific drivers.
38 * The next two lines (and the final #endif) are in all drivers:
39 */
40
41#include <i386/isa/snd/sound.h>
42#if NPCM > 0
43
44/*
45 * Begin with the board-specific include files...
46 */
47
48#define __SB_MIXER_C__	/* XXX warning... */
49#include  <i386/isa/snd/sbcard.h>
50
51/*
52 * then prototypes of functions which go in the snddev_info
53 * (usually static, unless they are shared by other modules)...
54 */
55
56static	int sb_probe(struct isa_device *dev);
57static	int sb_attach(struct isa_device *dev);
58
59static	d_open_t	sb_dsp_open;
60static	d_close_t	sb_dsp_close;
61static	d_ioctl_t	sb_dsp_ioctl;
62static	irq_proc_t	sbintr;
63static	snd_callback_t	sb_callback;
64
65/*
66 * and prototypes for other private functions defined in this module.
67 */
68
69static	void sb_dsp_init(snddev_info *d, struct isa_device *dev);
70static	void sb_mix_init(snddev_info *d);
71static int sb_mixer_set(snddev_info *d, int dev, int value);
72static int dsp_speed(snddev_info *d);
73static void sb_mixer_reset(snddev_info *d);
74
75u_int sb_get_byte(int io_base);
76
77/*
78 * Then put here the descriptors for the various boards supported
79 * by this module, properly initialized.
80 */
81
82snddev_info sb_op_desc = {
83    "basic soundblaster",
84
85    SNDCARD_SB,
86    sb_probe,
87    sb_attach,
88
89    sb_dsp_open,
90    sb_dsp_close /* sb_close */,
91    NULL /* use generic sndread */,
92    NULL /* use generic sndwrite */,
93    sb_dsp_ioctl,
94    sndpoll,
95
96    sbintr,
97    sb_callback,
98
99    DSP_BUFFSIZE,	/* bufsize */
100
101    AFMT_STEREO | AFMT_U8,		/* audio format */
102
103} ;
104
105/*
106 * Then the file continues with the body of all functions
107 * directly referenced in the descriptor.
108 */
109
110/*
111 * the probe routine for the SoundBlaster only consists in
112 * resetting the dsp and testing if it is there.
113 * Version detection etc. will be done at attach time.
114 *
115 * Remebber, isa probe routines are supposed to return the
116 * size of io space used.
117 */
118
119static int
120sb_probe(struct isa_device *dev)
121{
122    bzero(&pcm_info[dev->id_unit], sizeof(pcm_info[dev->id_unit]) );
123    if (dev->id_iobase == -1) {
124	dev->id_iobase = 0x220;
125	printf("sb_probe: no address supplied, try defaults (0x220,0x240)\n");
126        if (snd_conflict(dev->id_iobase))
127	    dev->id_iobase = 0x240;
128    }
129    if (snd_conflict(dev->id_iobase))
130	return 0 ;
131
132    if (sb_reset_dsp(dev->id_iobase))
133	return 16 ; /* the SB uses 16 registers... */
134    else
135	return 0;
136}
137
138static int
139sb_attach(struct isa_device *dev)
140{
141    snddev_info *d = &pcm_info[dev->id_unit] ;
142
143    sb_dsp_init(d, dev);
144    return 0 ;
145}
146
147/*
148 * here are the main routines from the switches.
149 */
150
151static int
152sb_dsp_open(dev_t dev, int flags, int mode, struct proc * p)
153{
154    snddev_info *d;
155    int unit ;
156
157    dev = minor(dev);
158    unit = dev >> 4 ;
159    d = &pcm_info[unit] ;
160
161    DEB(printf("<%s>%d : open\n", d->name, unit));
162
163    if (d->flags & SND_F_BUSY) {
164	printf("<%s>%d open: device busy\n", d->name, unit);
165	return EBUSY ;
166    }
167
168    d->wsel.si_pid = 0;
169    d->wsel.si_flags = 0;
170
171    d->rsel.si_pid = 0;
172    d->rsel.si_flags = 0;
173
174    d->esel.si_pid = 0;
175    d->esel.si_flags = 0;
176
177    d->flags = 0 ;
178    d->bd_flags &= ~BD_F_HISPEED ;
179
180    switch ( dev & 0xf ) {
181    case SND_DEV_DSP16 :
182	if ((d->audio_fmt & AFMT_S16_LE) == 0) {
183	    printf("sorry, 16-bit not supported on SB %d.%02d\n",
184		(d->bd_id >>8) & 0xff, d->bd_id & 0xff);
185	    return ENXIO;
186	}
187	d->play_fmt = d->rec_fmt = AFMT_S16_LE ;
188	break;
189    case SND_DEV_AUDIO :
190	d->play_fmt = d->rec_fmt = AFMT_MU_LAW ;
191	break ;
192    case SND_DEV_DSP :
193	d->play_fmt = d->rec_fmt = AFMT_U8 ;
194	break ;
195    }
196
197    d->flags |= SND_F_BUSY ;
198    d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED ;
199
200    if (flags & O_NONBLOCK)
201	d->flags |= SND_F_NBIO ;
202
203    reset_dbuf(& (d->dbuf_in) );
204    reset_dbuf(& (d->dbuf_out) );
205    sb_reset_dsp(d->io_base);
206    ask_init(d);
207
208    return 0;
209}
210
211static int
212sb_dsp_close(dev_t dev, int flags, int mode, struct proc * p)
213{
214    int unit;
215    snddev_info *d;
216    u_long s;
217
218    dev = minor(dev);
219    unit = dev >> 4 ;
220    d = &pcm_info[unit] ;
221
222    s = spltty();
223    d->flags |= SND_F_CLOSING ;
224    splx(s);
225    snd_flush(d);
226
227    sb_cmd(d->io_base, DSP_CMD_SPKOFF ); /* XXX useless ? */
228
229    d->flags = 0 ;
230    return 0 ;
231}
232
233static int
234sb_dsp_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, struct proc * p)
235{
236    int unit;
237    snddev_info *d;
238
239    dev = minor(dev);
240    unit = dev >> 4 ;
241    d = &pcm_info[unit] ;
242
243    /*
244     * handle mixer calls first. Reads are in the default handler,
245     * so do not bother about them.
246     */
247    if ( (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0) )
248	return sb_mixer_set(d, cmd & 0xff, *(int *)arg) ;
249
250    /*
251     * for the remaining functions, use the default handler.
252     */
253
254    return ENOSYS ;
255}
256
257static void
258sbintr(int unit)
259{
260    snddev_info *d = &pcm_info[unit];
261    int reason = 3, c=1, io_base = d->io_base;
262
263    DEB(printf("got sbintr for unit %d, flags 0x%08lx\n", unit, d->flags));
264
265    /*
266     * SB < 4.0 is half duplex and has only 1 bit for int source,
267     * so we fake it. SB 4.x (SB16) has the int source in a separate
268     * register.
269     */
270again:
271    if (d->bd_flags & BD_F_SB16) {
272	c = sb_getmixer(io_base, IRQ_STAT);
273	/* this tells us if the source is 8-bit or 16-bit dma. We
274	 * have to check the io channel to map it to read or write...
275	 */
276	reason = 0 ;
277	if ( c & 1 ) { /* 8-bit dma */
278	    if (d->dma1 < 4)
279		reason |= 1;
280	    if (d->dma2 < 4)
281		reason |= 2;
282	}
283	if ( c & 2 ) { /* 16-bit dma */
284	    if (d->dma1 >= 4)
285		reason |= 1;
286	    if (d->dma2 >= 4)
287		reason |= 2;
288	}
289    }
290    if ( c & 2 )
291	inb(DSP_DATA_AVL16); /* 16-bit int ack */
292    if (c & 1)
293	inb(DSP_DATA_AVAIL);	/* 8-bit int ack */
294
295DEB(printf("sbintr, flags 0x%08lx reason %d\n", d->flags, reason));
296    if ( (d->flags & SND_F_WR_DMA) && (reason & 1) )
297	dsp_wrintr(d);
298    if ( (d->flags & SND_F_RD_DMA) && (reason & 2) )
299	dsp_rdintr(d);
300
301    /*
302     * the sb16 might have multiple sources etc.
303     */
304    if (d->bd_flags & BD_F_SB16 && (c & 3) )
305	goto again;
306}
307
308/*
309 * device-specific function called back from the dma module.
310 * The reason of the callback is the second argument.
311 * NOTE: during operations, some ioctl can be done to change
312 * settings (e.g. speed, channels, format), and the default
313 * ioctl handler will just record the change and set the
314 * flag SND_F_INIT. The callback routine is in charge of applying
315 * the changes at the next convenient time (typically, at the
316 * start of operations). For full duplex devices, in some cases the
317 * init requires both channels to be idle.
318 */
319static int
320sb_callback(snddev_info *d, int reason)
321{
322    int rd = reason & SND_CB_RD ;
323    int l = (rd) ? d->dbuf_in.dl0 : d->dbuf_out.dl0 ;
324
325    switch (reason & SND_CB_REASON_MASK) {
326    case SND_CB_INIT : /* called with int enabled and no pending io */
327	dsp_speed(d);
328	snd_set_blocksize(d);
329	if (d->play_fmt & AFMT_MU_LAW)
330	    d->flags |= SND_F_XLAT8 ;
331	else
332	    d->flags &= ~SND_F_XLAT8 ;
333	return 1;
334	break ;
335
336    case SND_CB_START : /* called with int disabled */
337        sb_cmd(d->io_base, rd ? DSP_CMD_SPKOFF : DSP_CMD_SPKON);
338	d->flags &= ~SND_F_INIT ;
339	if (d->bd_flags & BD_F_SB16) {
340	    /* the SB16 can do full duplex using one 16-bit channel
341	     * and one 8-bit channel. It needs to be programmed to
342	     * use split format though.
343	     */
344	    int b16 ;
345	    int swap = 0 ;
346
347	    b16 = (rd) ? d->rec_fmt : d->play_fmt ;
348	    b16 = (b16 == AFMT_S16_LE) ? 1 : 0;
349	    /*
350	     * check if I have to swap dma channels. Swap if
351	     * - !rd, dma1 <4,  b16
352	     * - !rd, dma1 >=4, !b16
353	     * - rd,  dma2 <4,  b16
354	     * - rd,  dma2 >=4, !b16
355	     */
356	    if (!rd) {
357		if ( (d->dma1 <4 && b16) || (d->dma1 >=4 && !b16) ) swap = 1;
358	    } else {
359		if ( (d->dma2 <4 && b16) || (d->dma2 >=4 && !b16) ) swap = 1;
360	    }
361	    /*
362	     * before swapping should make sure that there is no
363	     * pending DMA on the other channel...
364	     */
365	    if (swap) {
366	        int c = d->dma2 ;
367		d->dma2 = d->dma1;
368		d->dma1 = c ;
369	    }
370	    DEB(printf("sb_init: play %ld rec %ld dma1 %d dma2 %d\n",
371		    d->play_fmt, d->rec_fmt, d->dma1, d->dma2));
372	}
373	/* fallthrough */
374    case SND_CB_RESTART:
375	if (d->bd_flags & BD_F_SB16) {
376	    u_char c, c1 ;
377	    /*
378	     * SB16 support still not completely working!!!
379	     *
380	     * in principle, on the SB16, I could support simultaneous
381	     * play & rec.
382	     * However, there is no way to ask explicitly for 8 or
383	     * 16 bit transfer. As a consequence, if we do 8-bit,
384	     * we need to use the 8-bit channel, and if we do 16-bit,
385	     * we need to use the other one. The only way I find to
386	     * do this is to swap d->dma1 and d->dma2 ...
387	     *
388	     */
389
390	    if (rd) {
391		c = ((d->dma2 > 3) ? DSP_DMA16 : DSP_DMA8) |
392			DSP_F16_FIFO_ON | DSP_F16_ADC ;
393		c1 = (d->play_fmt == AFMT_U8) ? 0 : DSP_F16_SIGNED ;
394		if (d->play_fmt == AFMT_MU_LAW) c1 = 0 ;
395		if (d->rec_fmt == AFMT_S16_LE)
396		    l /= 2 ;
397	    } else {
398		c = ((d->dma1 > 3) ? DSP_DMA16 : DSP_DMA8) |
399			DSP_F16_FIFO_ON | DSP_F16_DAC ;
400		c1 = (d->rec_fmt == AFMT_U8) ? 0 : DSP_F16_SIGNED ;
401		if (d->play_fmt == AFMT_MU_LAW) c1 = 0 ;
402		if (d->play_fmt == AFMT_S16_LE)
403		    l /= 2 ;
404	    }
405
406	    if (d->flags & SND_F_STEREO)
407		c1 |= DSP_F16_STEREO ;
408
409	    sb_cmd(d->io_base, c );
410	    sb_cmd3(d->io_base, c1 , l - 1) ;
411	} else {
412	    u_char c ;
413	    if (d->bd_flags & BD_F_HISPEED)
414		c = (rd) ? DSP_CMD_HSADC : DSP_CMD_HSDAC ;
415	    else
416		c = (rd) ? DSP_CMD_ADC8 : DSP_CMD_DAC8 ;
417	    sb_cmd3(d->io_base, c , l - 1) ;
418	}
419	break;
420
421    case SND_CB_STOP :
422	/* XXX ??? sb_cmd(d->io_base, DSP_CMD_SPKOFF);*/ /* speaker off */
423	break ;
424
425    }
426    return 0 ;
427}
428
429/*
430 * The second part of the file contains all functions specific to
431 * the board and (usually) not exported to other modules.
432 */
433
434int
435sb_reset_dsp(int io_base)
436{
437    int loopc;
438
439    outb(DSP_RESET, 1);
440    DELAY(100);
441    outb(DSP_RESET, 0);
442    for (loopc = 0; loopc<100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
443	DELAY(30);
444
445    if (inb(DSP_READ) != 0xAA) {
446        DEB(printf("sb_reset_dsp failed\n"));
447	return 0;	/* Sorry */
448    }
449    return 1;
450}
451
452/*
453 * only used in sb_attach from here.
454 */
455
456static void
457sb_dsp_init(snddev_info *d, struct isa_device *dev)
458{
459    int i, x;
460    char *fmt = NULL ;
461    int	io_base = dev->id_iobase ;
462
463    d->bd_id = 0 ;
464
465    sb_reset_dsp(io_base);
466    sb_cmd(io_base, DSP_CMD_GETVER);	/* Get version */
467
468    for (i = 10000; i; i--) { /* perhaps wait longer on a fast machine ? */
469	if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
470	    if ( (d->bd_id & 0xff00) == 0)
471		d->bd_id = inb(DSP_READ) << 8; /* major */
472	    else {
473		d->bd_id |= inb(DSP_READ); /* minor */
474		break;
475	    }
476	} else
477	    DELAY(20);
478    }
479
480    /*
481     * now do various initializations depending on board id.
482     */
483
484    fmt = "SoundBlaster %d.%d" ; /* default */
485
486    switch ( d->bd_id >> 8 ) {
487    case 0 :
488	printf("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
489	       inb(DSP_DATA_AVAIL));
490	d->bd_id = 0x100;
491    case 1 : /* old sound blaster has nothing... */
492	break ;
493
494    case 2 :
495	d->dma2 = d->dma1 ; /* half duplex */
496	d->bd_flags |= BD_F_DUP_MIDI ;
497
498	if (d->bd_id == 0x200)
499	    break ; /* no mixer on the 2.0 */
500	d->bd_flags &= ~BD_F_MIX_MASK ;
501	d->bd_flags |= BD_F_MIX_CT1335 ;
502
503	break ;
504    case 4 :
505	fmt = "SoundBlaster 16 %d.%d";
506	d->audio_fmt |= AFMT_FULLDUPLEX | AFMT_WEIRD | AFMT_S8 | AFMT_S16_LE;
507	d->bd_flags |= BD_F_SB16;
508	d->bd_flags &= ~BD_F_MIX_MASK ;
509	d->bd_flags |= BD_F_MIX_CT1745 ;
510
511	/* soft irq/dma configuration */
512	x = -1 ;
513	if (d->irq == 5) x = 2;
514	else if (d->irq == 7) x = 4;
515	else if (d->irq == 9) x = 1;
516	else if (d->irq == 10) x = 8;
517	if (x == -1)
518	    printf("<%s>%d: bad irq %d (only 5,7,9,10 allowed)\n",
519		d->name, dev->id_unit, d->irq);
520	else
521	    sb_setmixer(io_base, IRQ_NR, x);
522
523	sb_setmixer(io_base, DMA_NR, (1 << d->dma1) | (1 << d->dma2));
524	break ;
525
526    case 3 :
527	d->dma2 = d->dma1 ; /* half duplex */
528	fmt = "SoundBlaster Pro %d.%d";
529	d->bd_flags |= BD_F_DUP_MIDI ;
530	d->bd_flags &= ~BD_F_MIX_MASK ;
531	d->bd_flags |= BD_F_MIX_CT1345 ;
532	if (d->bd_id == 0x301) {
533	    int ess_major = 0, ess_minor = 0;
534
535	    /*
536	     * Try to detect ESS chips.
537	     */
538
539	    sb_cmd(io_base, DSP_CMD_GETID);	/* Return ident. bytes. */
540
541	    for (i = 1000; i; i--) {
542		if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
543		    if (ess_major == 0)
544			ess_major = inb(DSP_READ);
545		    else {
546			ess_minor = inb(DSP_READ);
547			break;
548		    }
549		}
550	    }
551
552	    if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
553		printf("Hmm... Could this be an ESS488 based card (rev %d)\n",
554		   ess_minor & 0x0f);
555	    else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
556		printf("Hmm... Could this be an ESS688 based card (rev %d)\n",
557		   ess_minor & 0x0f);
558	}
559
560	if (d->bd_flags & BD_F_JAZZ16) {
561	    if (d->bd_flags & BD_F_JAZZ16_2)
562		fmt = "SoundMan Wave %d.%d";
563	    else
564		fmt = "MV Jazz16 %d.%d";
565	    d->audio_fmt |= AFMT_S16_LE;	/* 16 bits */
566	}
567    }
568
569    sprintf(d->name, fmt, (d->bd_id >> 8) &0xff, d->bd_id & 0xff);
570
571    sb_mix_init(d);
572}
573
574static void
575sb_mix_init(snddev_info *d)
576{
577    switch (d->bd_flags & BD_F_MIX_MASK) {
578    case BD_F_MIX_CT1345 : /* SB 3.0 has 1345 mixer */
579
580	d->mix_devs = SBPRO_MIXER_DEVICES ;
581	d->mix_rec_devs = SBPRO_RECORDING_DEVICES ;
582	d->mix_recsrc = SOUND_MASK_MIC ;
583
584	sb_setmixer(d->io_base, 0, 1 ); /* reset mixer */
585	sb_setmixer(d->io_base, MIC_VOL , 0x6 ); /* mic volume max */
586	sb_setmixer(d->io_base, RECORD_SRC , 0x0 ); /* mic source */
587	sb_setmixer(d->io_base, FM_VOL , 0x0 ); /* no midi */
588	break ;
589
590    case BD_F_MIX_CT1745 : /* SB16 mixer ... */
591
592	d->mix_devs = SB16_MIXER_DEVICES ;
593	d->mix_rec_devs = SB16_RECORDING_DEVICES ;
594	d->mix_recsrc = SOUND_MASK_MIC ;
595    }
596    sb_mixer_reset(d);
597}
598
599/*
600 * Common code for the midi and pcm functions
601 */
602
603int
604sb_cmd(int io_base, u_char val)
605{
606    int  i;
607
608    for (i = 0; i < 1000 ; i++) {
609	if ((inb(DSP_STATUS) & 0x80) == 0) {
610	    outb(DSP_COMMAND, val);
611	    return 1;
612	}
613	if (i > 10)
614	    DELAY (i > 100 ? 1000 : 10 );
615    }
616
617    printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val);
618    return 0;
619}
620
621int
622sb_cmd3(int io_base, u_char cmd, int val)
623{
624    if (sb_cmd(io_base, cmd)) {
625	sb_cmd(io_base, val & 0xff );
626	sb_cmd(io_base, (val>>8) & 0xff );
627	return 1 ;
628    } else
629	return 0;
630}
631
632int
633sb_cmd2(int io_base, u_char cmd, int val)
634{
635    if (sb_cmd(io_base, cmd)) {
636	sb_cmd(io_base, val & 0xff );
637	return 1 ;
638    } else
639	return 0;
640}
641
642void
643sb_setmixer(int io_base, u_int port, u_int value)
644{
645    u_long   flags;
646
647    flags = spltty();
648    outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
649    DELAY(10);
650    outb(MIXER_DATA, (u_char) (value & 0xff));
651    DELAY(10);
652    splx(flags);
653}
654
655u_int
656sb_get_byte(int io_base)
657{
658    int             i;
659
660    for (i = 1000; i; i--)
661	if (inb(DSP_DATA_AVAIL) & 0x80)
662	    return inb(DSP_READ);
663	else
664	    DELAY(20);
665    return 0xffff;
666}
667
668int
669sb_getmixer(int io_base, u_int port)
670{
671    int             val;
672    u_long   flags;
673
674    flags = spltty();
675    outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
676    DELAY(10);
677    val = inb(MIXER_DATA);
678    DELAY(10);
679    splx(flags);
680
681    return val;
682}
683
684
685/*
686 * various utility functions for the DSP
687 */
688
689/*
690 * dsp_speed updates the speed setting from the descriptor. make sure
691 * it is called at spltty().
692 * Besides, it takes care of stereo setting.
693 */
694static int
695dsp_speed(snddev_info *d)
696{
697    u_char   tconst;
698    u_long   flags;
699    int max_speed = 44100, speed = d->play_speed ;
700
701    if (d->bd_flags & BD_F_SB16) {
702	RANGE (speed, 5000, 45000);
703	d->play_speed = d->rec_speed = speed ;
704	sb_cmd(d->io_base, 0x41);
705	sb_cmd(d->io_base, d->play_speed >> 8 );
706	sb_cmd(d->io_base, d->play_speed & 0xff );
707	sb_cmd(d->io_base, 0x42);
708	sb_cmd(d->io_base, d->rec_speed >> 8 );
709	sb_cmd(d->io_base, d->rec_speed & 0xff );
710	return speed ;
711    }
712    /*
713     * only some models can do stereo, and only if not
714     * simultaneously using midi.
715     */
716    if ( (d->bd_id & 0xff00) < 0x300 || d->bd_flags & BD_F_MIDIBUSY)
717	d->flags &= ~SND_F_STEREO;
718
719    /*
720     * here enforce speed limitations.
721     */
722    if (d->bd_id <= 0x200)
723	max_speed = 22050; /* max 22050 on SB 1.X */
724
725    /*
726     * SB models earlier than SB Pro have low limit for the
727     * input rate. Note that this is only for input, but since
728     * we do not support separate values for rec & play....
729     */
730    if (d->bd_id <= 0x200)
731	max_speed = 13000;
732    else if (d->bd_id < 0x300)
733	max_speed = 15000;
734
735    RANGE(speed, 4000, max_speed);
736
737    /*
738     * Logitech SoundMan Games and Jazz16 cards can support 44.1kHz
739     * stereo
740     */
741#if !defined (SM_GAMES)
742    /*
743     * Max. stereo speed is 22050
744     */
745    if (d->flags & SND_F_STEREO && speed > 22050 && !(d->bd_flags & BD_F_JAZZ16))
746	speed = 22050;
747#endif
748
749    if (d->flags & SND_F_STEREO)
750	speed *= 2;
751
752    /*
753     * Now the speed should be valid. Compute the value to be
754     * programmed into the board.
755     *
756     * XXX check this code...
757     */
758
759    if (speed > 22050) { /* High speed mode on 2.01/3.xx */
760	int tmp;
761
762	tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8);
763	d->bd_flags |= BD_F_HISPEED ;
764
765	flags = spltty();
766	sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
767	splx(flags);
768
769	tmp = 65536 - (tconst << 8);
770	speed = (256000000 + tmp / 2) / tmp;
771    } else {
772	int             tmp;
773
774	d->bd_flags &= ~BD_F_HISPEED ;
775	tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
776
777	flags = spltty();
778	sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
779	splx(flags);
780
781	tmp = 256 - tconst;
782	speed = (1000000 + tmp / 2) / tmp;
783    }
784
785    if (d->flags & SND_F_STEREO)
786	speed /= 2;
787
788    d->play_speed = d->rec_speed = speed;
789    return speed;
790}
791
792/*
793 * mixer support, originally in sb_mixer.c
794 */
795
796static void
797sb_set_recsrc(snddev_info *d, int mask)
798{
799    u_char recdev ;
800
801    mask &= d->mix_rec_devs;
802    switch (d->bd_flags & BD_F_MIX_MASK) {
803    case BD_F_MIX_CT1345 :
804	if (mask == SOUND_MASK_LINE)
805	    recdev = 6 ;
806	else if (mask == SOUND_MASK_CD)
807	    recdev = 2 ;
808	else { /* default: mic */
809	    mask =  SOUND_MASK_MIC ;
810	    recdev = 0 ;
811	}
812	sb_setmixer(d->io_base, RECORD_SRC,
813	    recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 ));
814	break ;
815    case BD_F_MIX_CT1745 : /* sb16 */
816	if (mask == 0)
817	    mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */
818	recdev = 0 ;
819	if (mask & SOUND_MASK_MIC)
820	    recdev |= 1 ;
821	if (mask & SOUND_MASK_CD)
822	    recdev |= 6 ; /* l+r cd */
823	if (mask & SOUND_MASK_LINE)
824	    recdev |= 0x18 ; /* l+r line */
825	if (mask & SOUND_MASK_SYNTH)
826	    recdev |= 0x60 ; /* l+r midi */
827	sb_setmixer(d->io_base, SB16_IMASK_L, recdev);
828	sb_setmixer(d->io_base, SB16_IMASK_R, recdev);
829	/*
830	 * since the same volume controls apply to the input and
831	 * output sections, the best approach to have a consistent
832	 * behaviour among cards would be to disable the output path
833	 * on devices which are used to record.
834	 * However, since users like to have feedback, we only disable
835	 * the mike -- permanently.
836	 */
837        sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1);
838	break ;
839    }
840    d->mix_recsrc = mask;
841}
842
843static void
844sb_mixer_reset(snddev_info *d)
845{
846    int             i;
847
848    for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
849	sb_mixer_set(d, i, levels[i]);
850    if (d->bd_flags & BD_F_SB16) {
851	sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */
852	sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */
853	sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */
854    }
855    sb_set_recsrc(d, SOUND_MASK_MIC);
856}
857
858static int
859sb_mixer_set(snddev_info *d, int dev, int value)
860{
861    int left = value & 0x000000ff;
862    int right = (value & 0x0000ff00) >> 8;
863    int regoffs;
864    u_char   val;
865    mixer_tab *iomap;
866
867#ifdef JAZZ16
868    if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2)
869        return smw_mixer_set(dev, value);
870#endif
871
872    if (dev == SOUND_MIXER_RECSRC) {
873	sb_set_recsrc(d, value);
874	return 0 ;
875    }
876    if (left > 100)
877        left = 100;
878    if (right > 100)
879        right = 100;
880
881    if (dev > 31)
882        return EINVAL ;
883
884    if (!(d->mix_devs & (1 << dev)))      /* Not supported */
885        return EINVAL;
886
887    switch ( d->bd_flags & BD_F_MIX_MASK ) {
888    default:
889	/* mixer unknown, fail... */
890	return EINVAL ;/* XXX change this */
891    case BD_F_MIX_CT1345 :
892	iomap = &sbpro_mix ;
893	break;
894    case BD_F_MIX_CT1745 :
895	iomap = &sb16_mix ;
896	break;
897    /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
898    }
899    regoffs = (*iomap)[dev][LEFT_CHN].regno;
900    if (regoffs == 0)
901        return EINVAL;
902
903    val = sb_getmixer(d->io_base, regoffs);
904
905    change_bits(iomap, &val, dev, LEFT_CHN, left);
906
907    d->mix_levels[dev] = left | (left << 8);
908
909    if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) {    /* Change register */
910        sb_setmixer(d->io_base, regoffs, val);     /* Save the old one */
911        regoffs = (*iomap)[dev][RIGHT_CHN].regno;
912
913        if (regoffs == 0)
914            return 0 ;  /* Just left channel present */
915
916        val = sb_getmixer(d->io_base, regoffs);    /* Read the new one */
917    }
918    change_bits(iomap, &val, dev, RIGHT_CHN, right);
919
920    sb_setmixer(d->io_base, regoffs, val);
921
922    d->mix_levels[dev] = left | (right << 8);
923    return 0 ; /* ok */
924}
925
926/*
927 * now support for some PnP boards.
928 */
929
930#if NPNP > 0
931static char *opti925_probe(u_long csn, u_long vend_id);
932static void opti925_attach(u_long csn, u_long vend_id, char *name,
933        struct isa_device *dev);
934
935static struct pnp_device opti925 = {
936        "opti925",
937        opti925_probe,
938        opti925_attach,
939        &nsnd,  /* use this for all sound cards */
940        &tty_imask      /* imask */
941};
942DATA_SET (pnpdevice_set, opti925);
943
944static char *
945opti925_probe(u_long csn, u_long vend_id)
946{
947    if (vend_id == 0x2509143e) {
948	struct pnp_cinfo d ;
949	read_pnp_parms ( &d , 1 ) ;
950	if (d.enable == 0) {
951	    printf("This is an OPTi925, but LDN 1 is disabled\n");
952	    return NULL;
953	}
954        return "OPTi925" ;
955    }
956    return NULL ;
957}
958
959static void
960opti925_attach(u_long csn, u_long vend_id, char *name,
961        struct isa_device *dev)
962{
963    struct pnp_cinfo d ;
964    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
965    int the_irq = 0 ;
966
967    tmp_d = sb_op_desc;
968    snddev_last_probed = &tmp_d;
969
970    read_pnp_parms ( &d , 3 );  /* disable LDN 3 */
971    the_irq = d.irq[0];
972    d.port[0] = 0 ;
973    d.enable = 0 ;
974    write_pnp_parms ( &d , 3 );
975
976    read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
977    d.port[0] = 0 ;
978    d.enable = 0 ;
979    write_pnp_parms ( &d , 2 );
980
981    read_pnp_parms ( &d , 1 ) ;
982    d.irq[0] = the_irq ;
983    dev->id_iobase = d.port[0];
984    write_pnp_parms ( &d , 1 );
985    enable_pnp_card();
986
987    tmp_d.conf_base = d.port[3];
988
989    dev->id_drq = d.drq[0] ; /* primary dma */
990    dev->id_irq = (1 << d.irq[0] ) ;
991    dev->id_intr = pcmintr ;
992    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
993
994    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
995
996    pcmattach(dev);
997
998}
999
1000/*
1001 * A driver for some SB16pnp and compatibles...
1002 *
1003 * Avance Asound 100 -- 0x01009305
1004 * xxx               -- 0x2b008c0e
1005 *
1006 */
1007
1008static char *sb16pnp_probe(u_long csn, u_long vend_id);
1009static void sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1010        struct isa_device *dev);
1011
1012static struct pnp_device sb16pnp = {
1013        "SB16pnp",
1014        sb16pnp_probe,
1015        sb16pnp_attach,
1016        &nsnd,  /* use this for all sound cards */
1017        &tty_imask      /* imask */
1018};
1019DATA_SET (pnpdevice_set, sb16pnp);
1020
1021static char *
1022sb16pnp_probe(u_long csn, u_long vend_id)
1023{
1024    char *s = NULL ;
1025    if (vend_id == 0x01009305)
1026        s = "Avance Asound 100" ;
1027    if (vend_id == 0x2b008c0e)
1028        s = "SB16 Value PnP" ;
1029    /*
1030     * The SB16/AWE64 cards seem to differ in the fourth byte of
1031     * the vendor id, so I have just masked it for the time being...
1032     * Reported values are:
1033     * SB16 Value PnP:	0x2b008c0e
1034     * SB AWE64 PnP:	0x39008c0e 0x9d008c0e 0xc3008c0e
1035     */
1036    if ( (vend_id & 0xffffff)  == (0x9d008c0e & 0xffffff) )
1037	s = "SB AWE64 PnP";
1038    if (s) {
1039	struct pnp_cinfo d;
1040	read_pnp_parms(&d, 0);
1041	if (d.enable == 0) {
1042	    printf("This is a %s, but LDN 0 is disabled\n", s);
1043	    return NULL ;
1044	}
1045	return s ;
1046    }
1047    return NULL ;
1048}
1049
1050static void
1051sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1052        struct isa_device *dev)
1053{
1054    struct pnp_cinfo d ;
1055    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1056
1057    tmp_d = sb_op_desc;
1058    snddev_last_probed = &tmp_d;
1059
1060    read_pnp_parms ( &d , 0 ) ;
1061    d.port[1] = 0 ; /* only the first address is used */
1062    dev->id_iobase = d.port[0];
1063    write_pnp_parms ( &d , 0 );
1064    enable_pnp_card();
1065
1066    dev->id_drq = d.drq[0] ; /* primary dma */
1067    dev->id_irq = (1 << d.irq[0] ) ;
1068    dev->id_intr = pcmintr ;
1069    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1070
1071    pcm_info[dev->id_unit] = tmp_d;
1072    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1073
1074    pcmattach(dev);
1075}
1076#endif /* NPNP */
1077
1078#endif
1079