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