sb16.c revision 29415
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    sndselect,
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    if (dev->id_iobase == -1) {
123	dev->id_iobase = 0x220;
124	printf("sb_probe: no address supplied, try defaults (0x220,0x240)\n");
125        if (snd_conflict(dev->id_iobase))
126	    dev->id_iobase = 0x240;
127    }
128    if (snd_conflict(dev->id_iobase))
129	return 0 ;
130
131    if (sb_reset_dsp(dev->id_iobase))
132	return 16 ; /* the SB uses 16 registers... */
133    else
134	return 0;
135}
136
137static int
138sb_attach(struct isa_device *dev)
139{
140    snddev_info *d = &pcm_info[dev->id_unit] ;
141
142    sb_dsp_init(d, dev);
143    return 0 ;
144}
145
146/*
147 * here are the main routines from the switches.
148 */
149
150static int
151sb_dsp_open(dev_t dev, int flags, int mode, struct proc * p)
152{
153    snddev_info *d;
154    int unit ;
155
156    dev = minor(dev);
157    unit = dev >> 4 ;
158    d = &pcm_info[unit] ;
159
160    DEB(printf("<%s>%d : open\n", d->name, unit));
161
162    if (d->flags & SND_F_BUSY) {
163	printf("<%s>%d open: device busy\n", d->name, unit);
164	return EBUSY ;
165    }
166
167    d->wsel.si_pid = 0;
168    d->wsel.si_flags = 0;
169
170    d->rsel.si_pid = 0;
171    d->rsel.si_flags = 0;
172
173    d->esel.si_pid = 0;
174    d->esel.si_flags = 0;
175
176    d->flags = 0 ;
177    d->bd_flags &= ~BD_F_HISPEED ;
178
179    switch ( dev & 0xf ) {
180    case SND_DEV_DSP16 :
181	if ((d->audio_fmt & AFMT_S16_LE) == 0) {
182	    printf("sorry, 16-bit not supported on SB %d.%02d\n",
183		(d->bd_id >>8) & 0xff, d->bd_id & 0xff);
184	    return ENXIO;
185	}
186	d->play_fmt = d->rec_fmt = AFMT_S16_LE ;
187	break;
188    case SND_DEV_AUDIO :
189	d->play_fmt = d->rec_fmt = AFMT_MU_LAW ;
190	break ;
191    case SND_DEV_DSP :
192	d->play_fmt = d->rec_fmt = AFMT_U8 ;
193	break ;
194    }
195
196    d->flags |= SND_F_BUSY ;
197    d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED ;
198
199    if (flags & O_NONBLOCK)
200	d->flags |= SND_F_NBIO ;
201
202    reset_dbuf(& (d->dbuf_in) );
203    reset_dbuf(& (d->dbuf_out) );
204    sb_reset_dsp(d->io_base);
205    ask_init(d);
206
207    return 0;
208}
209
210static int
211sb_dsp_close(dev_t dev, int flags, int mode, struct proc * p)
212{
213    int unit;
214    snddev_info *d;
215    u_long s;
216
217    dev = minor(dev);
218    unit = dev >> 4 ;
219    d = &pcm_info[unit] ;
220
221    s = spltty();
222    d->flags |= SND_F_CLOSING ;
223    splx(s);
224    snd_flush(d);
225
226    sb_cmd(d->io_base, DSP_CMD_SPKOFF ); /* XXX useless ? */
227
228    d->flags = 0 ;
229    return 0 ;
230}
231
232static int
233sb_dsp_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, struct proc * p)
234{
235    int unit;
236    snddev_info *d;
237
238    dev = minor(dev);
239    unit = dev >> 4 ;
240    d = &pcm_info[unit] ;
241
242    /*
243     * handle mixer calls first. Reads are in the default handler,
244     * so do not bother about them.
245     */
246    if ( (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0) )
247	return sb_mixer_set(d, cmd & 0xff, *(int *)arg) ;
248
249    /*
250     * for the remaining functions, use the default handler.
251     */
252
253    return ENOSYS ;
254}
255
256static void
257sbintr(int unit)
258{
259    snddev_info *d = &pcm_info[unit];
260    int reason = 3, c=1, io_base = d->io_base;
261
262    DEB(printf("got sbintr for unit %d, flags 0x%08x\n", unit, d->flags));
263
264    /*
265     * SB < 4.0 is half duplex and has only 1 bit for int source,
266     * so we fake it. SB 4.x (SB16) has the int source in a separate
267     * register.
268     */
269again:
270    if (d->bd_flags & BD_F_SB16) {
271	c = sb_getmixer(io_base, IRQ_STAT);
272	/* this tells us if the source is 8-bit or 16-bit dma. We
273	 * have to check the io channel to map it to read or write...
274	 */
275	reason = 0 ;
276	if ( c & 1 ) { /* 8-bit dma */
277	    if (d->dma1 < 4)
278		reason |= 1;
279	    if (d->dma2 < 4)
280		reason |= 2;
281	}
282	if ( c & 2 ) { /* 16-bit dma */
283	    if (d->dma1 >= 4)
284		reason |= 1;
285	    if (d->dma2 >= 4)
286		reason |= 2;
287	}
288    }
289    if ( c & 2 )
290	inb(DSP_DATA_AVL16); /* 16-bit int ack */
291    if (c & 1)
292	inb(DSP_DATA_AVAIL);	/* 8-bit int ack */
293
294DEB(printf("sbintr, flags 0x%08x reason %d\n", d->flags, reason));
295    if ( (d->flags & SND_F_WR_DMA) && (reason & 1) )
296	dsp_wrintr(d);
297    if ( (d->flags & SND_F_RD_DMA) && (reason & 2) )
298	dsp_rdintr(d);
299
300    /*
301     * the sb16 might have multiple sources etc.
302     */
303    if (d->bd_flags & BD_F_SB16 && (c & 3) )
304	goto again;
305}
306
307/*
308 * device-specific function called back from the dma module.
309 * The reason of the callback is the second argument.
310 * NOTE: during operations, some ioctl can be done to change
311 * settings (e.g. speed, channels, format), and the default
312 * ioctl handler will just record the change and set the
313 * flag SND_F_INIT. The callback routine is in charge of applying
314 * the changes at the next convenient time (typically, at the
315 * start of operations). For full duplex devices, in some cases the
316 * init requires both channels to be idle.
317 */
318static int
319sb_callback(snddev_info *d, int reason)
320{
321    u_long s ;
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 %d rec %d 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	/* 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 ((speed > 22050) && d->bd_flags & BD_F_MIDIBUSY)
750	speed = 22050;
751
752    if (d->flags & SND_F_STEREO)
753	speed *= 2;
754
755    /*
756     * Now the speed should be valid. Compute the value to be
757     * programmed into the board.
758     */
759
760    if (speed > 22050) { /* High speed mode on 2.01/3.xx */
761	int tmp;
762
763	tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8);
764	d->bd_flags |= BD_F_HISPEED ;
765
766	flags = spltty();
767	sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
768	splx(flags);
769
770	tmp = 65536 - (tconst << 8);
771	speed = (256000000 + tmp / 2) / tmp;
772    } else {
773	int             tmp;
774
775	d->bd_flags &= ~BD_F_HISPEED ;
776	tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
777
778	flags = spltty();
779	sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
780	splx(flags);
781
782	tmp = 256 - tconst;
783	speed = (1000000 + tmp / 2) / tmp;
784    }
785
786    if (d->flags & SND_F_STEREO)
787	speed /= 2;
788
789    d->play_speed = d->rec_speed = speed;
790    return speed;
791}
792
793/*
794 * mixer support, originally in sb_mixer.c
795 */
796
797static void
798sb_set_recsrc(snddev_info *d, int mask)
799{
800    u_char outmask;
801    u_char recdev ;
802
803    mask &= d->mix_rec_devs;
804    switch (d->bd_flags & BD_F_MIX_MASK) {
805    case BD_F_MIX_CT1345 :
806	if (mask == SOUND_MASK_LINE)
807	    recdev = 6 ;
808	else if (mask == SOUND_MASK_CD)
809	    recdev = 2 ;
810	else { /* default: mic */
811	    mask =  SOUND_MASK_MIC ;
812	    recdev = 0 ;
813	}
814	sb_setmixer(d->io_base, RECORD_SRC,
815	    recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 ));
816	break ;
817    case BD_F_MIX_CT1745 : /* sb16 */
818	if (mask == 0)
819	    mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */
820	recdev = 0 ;
821	if (mask & SOUND_MASK_MIC)
822	    recdev |= 1 ;
823	if (mask & SOUND_MASK_CD)
824	    recdev |= 6 ; /* l+r cd */
825	if (mask & SOUND_MASK_LINE)
826	    recdev |= 0x18 ; /* l+r line */
827	if (mask & SOUND_MASK_SYNTH)
828	    recdev |= 0x60 ; /* l+r midi */
829	sb_setmixer(d->io_base, SB16_IMASK_L, recdev);
830	sb_setmixer(d->io_base, SB16_IMASK_R, recdev);
831	/*
832	 * since the same volume controls apply to the input and
833	 * output sections, the best approach to have a consistent
834	 * behaviour among cards would be to disable the output path
835	 * on devices which are used to record.
836	 * However, since users like to have feedback, we only disable
837	 * the mike -- permanently.
838	 */
839        sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1);
840	break ;
841    }
842    d->mix_recsrc = mask;
843}
844
845static void
846sb_mixer_reset(snddev_info *d)
847{
848    int             i;
849
850    for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
851	sb_mixer_set(d, i, levels[i]);
852    if (d->bd_flags & BD_F_SB16) {
853	sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */
854	sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */
855	sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */
856    }
857    sb_set_recsrc(d, SOUND_MASK_MIC);
858}
859
860static int
861sb_mixer_set(snddev_info *d, int dev, int value)
862{
863    int left = value & 0x000000ff;
864    int right = (value & 0x0000ff00) >> 8;
865    int regoffs;
866    u_char   val;
867    mixer_tab *iomap;
868
869#ifdef JAZZ16
870    if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2)
871        return smw_mixer_set(dev, value);
872#endif
873
874    if (dev == SOUND_MIXER_RECSRC) {
875	sb_set_recsrc(d, value);
876	return 0 ;
877    }
878    if (left > 100)
879        left = 100;
880    if (right > 100)
881        right = 100;
882
883    if (dev > 31)
884        return EINVAL ;
885
886    if (!(d->mix_devs & (1 << dev)))      /* Not supported */
887        return EINVAL;
888
889    switch ( d->bd_flags & BD_F_MIX_MASK ) {
890    default:
891	/* mixer unknown, fail... */
892	return EINVAL ;/* XXX change this */
893    case BD_F_MIX_CT1345 :
894	iomap = &sbpro_mix ;
895	break;
896    case BD_F_MIX_CT1745 :
897	iomap = &sb16_mix ;
898	break;
899    /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
900    }
901    regoffs = (*iomap)[dev][LEFT_CHN].regno;
902    if (regoffs == 0)
903        return EINVAL;
904
905    val = sb_getmixer(d->io_base, regoffs);
906
907    change_bits(iomap, &val, dev, LEFT_CHN, left);
908
909    d->mix_levels[dev] = left | (left << 8);
910
911    if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) {    /* Change register */
912        sb_setmixer(d->io_base, regoffs, val);     /* Save the old one */
913        regoffs = (*iomap)[dev][RIGHT_CHN].regno;
914
915        if (regoffs == 0)
916            return 0 ;  /* Just left channel present */
917
918        val = sb_getmixer(d->io_base, regoffs);    /* Read the new one */
919    }
920    change_bits(iomap, &val, dev, RIGHT_CHN, right);
921
922    sb_setmixer(d->io_base, regoffs, val);
923
924    d->mix_levels[dev] = left | (right << 8);
925    return 0 ; /* ok */
926}
927
928/*
929 * now support for some PnP boards.
930 */
931
932#if NPNP > 0
933static char *opti925_probe(u_long csn, u_long vend_id);
934static void opti925_attach(u_long csn, u_long vend_id, char *name,
935        struct isa_device *dev);
936
937static struct pnp_device opti925 = {
938        "opti925",
939        opti925_probe,
940        opti925_attach,
941        &nsnd,  /* use this for all sound cards */
942        &tty_imask      /* imask */
943};
944DATA_SET (pnpdevice_set, opti925);
945
946static char *
947opti925_probe(u_long csn, u_long vend_id)
948{
949    if (vend_id == 0x2509143e) {
950	struct pnp_cinfo d ;
951	read_pnp_parms ( &d , 1 ) ;
952	if (d.enable == 0) {
953	    printf("This is an OPTi925, but LDN 1 is disabled\n");
954	    return NULL;
955	}
956        return "OPTi925" ;
957    }
958    return NULL ;
959}
960
961static void
962opti925_attach(u_long csn, u_long vend_id, char *name,
963        struct isa_device *dev)
964{
965    struct pnp_cinfo d ;
966    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
967    int the_irq = 0 ;
968
969    tmp_d = sb_op_desc;
970    snddev_last_probed = &tmp_d;
971
972    read_pnp_parms ( &d , 3 );  /* disable LDN 3 */
973    the_irq = d.irq[0];
974    d.port[0] = 0 ;
975    d.enable = 0 ;
976    write_pnp_parms ( &d , 3 );
977
978    read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
979    d.port[0] = 0 ;
980    d.enable = 0 ;
981    write_pnp_parms ( &d , 2 );
982
983    read_pnp_parms ( &d , 1 ) ;
984    d.irq[0] = the_irq ;
985    dev->id_iobase = d.port[0];
986    write_pnp_parms ( &d , 1 );
987    enable_pnp_card();
988
989    tmp_d.conf_base = d.port[3];
990
991    dev->id_drq = d.drq[0] ; /* primary dma */
992    dev->id_irq = (1 << d.irq[0] ) ;
993    dev->id_intr = pcmintr ;
994    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
995
996    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
997
998    pcmattach(dev);
999
1000}
1001
1002/*
1003 * A driver for some SB16pnp and compatibles...
1004 *
1005 * Avance Asound 100 -- 0x01009305
1006 * xxx               -- 0x2b008c0e
1007 *
1008 */
1009
1010static char *sb16pnp_probe(u_long csn, u_long vend_id);
1011static void sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1012        struct isa_device *dev);
1013
1014static struct pnp_device sb16pnp = {
1015        "SB16pnp",
1016        sb16pnp_probe,
1017        sb16pnp_attach,
1018        &nsnd,  /* use this for all sound cards */
1019        &tty_imask      /* imask */
1020};
1021DATA_SET (pnpdevice_set, sb16pnp);
1022
1023static char *
1024sb16pnp_probe(u_long csn, u_long vend_id)
1025{
1026    char *s = NULL ;
1027    if (vend_id == 0x01009305)
1028        s = "Avance Asound 100" ;
1029    if (vend_id == 0x2b008c0e)
1030        s = "SB16 Value PnP" ;
1031    /*
1032     * The SB16/AWE64 cards seem to differ in the fourth byte of
1033     * the vendor id, so I have just masked it for the time being...
1034     * Reported values are:
1035     * SB16 Value PnP:	0x2b008c0e
1036     * SB AWE64 PnP:	0x39008c0e 0x9d008c0e 0xc3008c0e
1037     */
1038    if ( (vend_id & 0xffffff)  == (0x9d008c0e & 0xffffff) )
1039	s = "SB AWE64 PnP";
1040    if (s) {
1041	struct pnp_cinfo d;
1042	read_pnp_parms(&d, 0);
1043	if (d.enable == 0) {
1044	    printf("This is a %s, but LDN 0 is disabled\n", s);
1045	    return NULL ;
1046	}
1047	return s ;
1048    }
1049    return NULL ;
1050}
1051
1052static void
1053sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1054        struct isa_device *dev)
1055{
1056    struct pnp_cinfo d ;
1057    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1058
1059    tmp_d = sb_op_desc;
1060    snddev_last_probed = &tmp_d;
1061
1062    read_pnp_parms ( &d , 0 ) ;
1063    d.port[1] = 0 ; /* only the first address is used */
1064    dev->id_iobase = d.port[0];
1065    write_pnp_parms ( &d , 0 );
1066    enable_pnp_card();
1067
1068    dev->id_drq = d.drq[0] ; /* primary dma */
1069    dev->id_irq = (1 << d.irq[0] ) ;
1070    dev->id_intr = pcmintr ;
1071    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1072
1073    pcm_info[dev->id_unit] = tmp_d;
1074    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1075
1076    pcmattach(dev);
1077}
1078#endif /* NPNP */
1079
1080#endif
1081