sb16.c revision 36764
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, u_long 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		break ; /* XXX */
622	    } else {
623		printf("Unknown card 0x%x 0x%x -- hope it is SBPRO\n",
624			ess_major, ess_minor);
625		break ;
626	    }
627	}
628
629    }
630
631    sprintf(d->name, fmt, (d->bd_id >> 8) &0xff, d->bd_id & 0xff);
632
633    sb_mix_init(d);
634}
635
636static void
637sb_mix_init(snddev_info *d)
638{
639    switch (d->bd_flags & BD_F_MIX_MASK) {
640    case BD_F_MIX_CT1345 : /* SB 3.0 has 1345 mixer */
641
642	d->mix_devs = SBPRO_MIXER_DEVICES ;
643	d->mix_rec_devs = SBPRO_RECORDING_DEVICES ;
644	d->mix_recsrc = SOUND_MASK_MIC ;
645
646	sb_setmixer(d->io_base, 0, 1 ); /* reset mixer */
647	sb_setmixer(d->io_base, MIC_VOL , 0x6 ); /* mic volume max */
648	sb_setmixer(d->io_base, RECORD_SRC , 0x0 ); /* mic source */
649	sb_setmixer(d->io_base, FM_VOL , 0x0 ); /* no midi */
650	break ;
651
652    case BD_F_MIX_CT1745 : /* SB16 mixer ... */
653
654	d->mix_devs = SB16_MIXER_DEVICES ;
655	d->mix_rec_devs = SB16_RECORDING_DEVICES ;
656	d->mix_recsrc = SOUND_MASK_MIC ;
657    }
658    sb_mixer_reset(d);
659}
660
661/*
662 * Common code for the midi and pcm functions
663 */
664
665int
666sb_cmd(int io_base, u_char val)
667{
668    int  i;
669
670    for (i = 0; i < 1000 ; i++) {
671	if ((inb(io_base + SBDSP_STATUS) & 0x80) == 0) {
672	    outb(io_base + SBDSP_CMD, val);
673	    return 1;
674	}
675	if (i > 10)
676	    DELAY (i > 100 ? 1000 : 10 );
677    }
678
679    printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val);
680    return 0;
681}
682
683int
684sb_cmd3(int io_base, u_char cmd, int val)
685{
686    if (sb_cmd(io_base, cmd)) {
687	sb_cmd(io_base, val & 0xff );
688	sb_cmd(io_base, (val>>8) & 0xff );
689	return 1 ;
690    } else
691	return 0;
692}
693
694int
695sb_cmd2(int io_base, u_char cmd, int val)
696{
697    if (sb_cmd(io_base, cmd)) {
698	sb_cmd(io_base, val & 0xff );
699	return 1 ;
700    } else
701	return 0;
702}
703
704/*
705 * in the SB, there is a set of indirect "mixer" registers with
706 * address at offset 4, data at offset 5
707 */
708void
709sb_setmixer(int io_base, u_int port, u_int value)
710{
711    u_long   flags;
712
713    flags = spltty();
714    outb(io_base + 4, (u_char) (port & 0xff));   /* Select register */
715    DELAY(10);
716    outb(io_base + 5, (u_char) (value & 0xff));
717    DELAY(10);
718    splx(flags);
719}
720
721int
722sb_getmixer(int io_base, u_int port)
723{
724    int             val;
725    u_long   flags;
726
727    flags = spltty();
728    outb(io_base + 4, (u_char) (port & 0xff));   /* Select register */
729    DELAY(10);
730    val = inb(io_base + 5);
731    DELAY(10);
732    splx(flags);
733
734    return val;
735}
736
737u_int
738sb_get_byte(int io_base)
739{
740    int             i;
741
742    for (i = 1000; i; i--)
743	if (inb(DSP_DATA_AVAIL) & 0x80)
744	    return inb(DSP_READ);
745	else
746	    DELAY(20);
747    return 0xffff;
748}
749
750
751
752/*
753 * various utility functions for the DSP
754 */
755
756/*
757 * dsp_speed updates the speed setting from the descriptor. make sure
758 * it is called at spltty().
759 * Besides, it takes care of stereo setting.
760 */
761static int
762dsp_speed(snddev_info *d)
763{
764    u_char   tconst;
765    u_long   flags;
766    int max_speed = 44100, speed = d->play_speed ;
767
768    /*
769     * special code for the SB16
770     */
771    if (d->bd_flags & BD_F_SB16) {
772	RANGE (speed, 5000, 45000);
773	d->play_speed = d->rec_speed = speed ;
774	sb_cmd(d->io_base, 0x41);
775	sb_cmd(d->io_base, d->play_speed >> 8 );
776	sb_cmd(d->io_base, d->play_speed & 0xff );
777	sb_cmd(d->io_base, 0x42);
778	sb_cmd(d->io_base, d->rec_speed >> 8 );
779	sb_cmd(d->io_base, d->rec_speed & 0xff );
780	return speed ;
781    }
782
783    /*
784     * special code for the ESS ...
785     */
786    if (d->bd_flags & BD_F_ESS) {
787	int t;
788	RANGE (speed, 4000, 48000);
789	if (speed > 22000) {
790	    t = (795500 + speed / 2) / speed;
791	    speed = (795500 + t / 2) / t ;
792	    t = ( 256 - (795500 + speed / 2) / speed ) | 0x80 ;
793	} else {
794	    t = (397700 + speed / 2) / speed;
795	    speed = (397700 + t / 2) / t ;
796	    t = 128 - (397700 + speed / 2) / speed ;
797	}
798	sb_cmd2(d->io_base, 0xa1, t); /* set time constant */
799	return speed ;
800    }
801
802    /*
803     * This is code for the SB3.x and lower.
804     * Only some models can do stereo, and only if not
805     * simultaneously using midi.
806     * At the moment we do not support either...
807     */
808#if 0
809    d->flags &= ~SND_F_STEREO;
810#endif
811
812    /*
813     * here enforce speed limitations.
814     */
815    if (d->bd_id <= 0x200)
816	max_speed = 22050; /* max 22050 on SB 1.X */
817
818    /*
819     * SB models earlier than SB Pro have low limit for the
820     * input rate. Note that this is only for input, but since
821     * we do not support separate values for rec & play....
822     */
823    if (d->bd_id <= 0x200)
824	max_speed = 13000;
825    else if (d->bd_id < 0x300)
826	max_speed = 15000;
827
828    RANGE(speed, 4000, max_speed);
829
830    if (d->flags & SND_F_STEREO) /* really unused right now... */
831	speed *= 2;
832
833    /*
834     * Now the speed should be valid. Compute the value to be
835     * programmed into the board.
836     */
837
838    if (speed > 22050) { /* High speed mode on 2.01/3.xx */
839	int tmp;
840
841	tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8) ;
842	d->bd_flags |= BD_F_HISPEED ;
843
844	flags = spltty();
845	sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */
846	splx(flags);
847
848	tmp = 65536 - (tconst << 8);
849	speed = (256000000 + tmp / 2) / tmp;
850    } else {
851	int             tmp;
852
853	d->bd_flags &= ~BD_F_HISPEED ;
854	tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
855
856	flags = spltty();
857	sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */
858	splx(flags);
859
860	tmp = 256 - tconst;
861	speed = (1000000 + tmp / 2) / tmp;
862    }
863
864    if (d->flags & SND_F_STEREO) /* really unused right now... */
865	speed /= 2;
866
867    d->play_speed = d->rec_speed = speed;
868    return speed;
869}
870
871/*
872 * mixer support, originally in sb_mixer.c
873 */
874
875static void
876sb_set_recsrc(snddev_info *d, int mask)
877{
878    u_char recdev ;
879
880    mask &= d->mix_rec_devs;
881    switch (d->bd_flags & BD_F_MIX_MASK) {
882    case BD_F_MIX_CT1345 :
883	if (mask == SOUND_MASK_LINE)
884	    recdev = 6 ;
885	else if (mask == SOUND_MASK_CD)
886	    recdev = 2 ;
887	else { /* default: mic */
888	    mask =  SOUND_MASK_MIC ;
889	    recdev = 0 ;
890	}
891	sb_setmixer(d->io_base, RECORD_SRC,
892	    recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 ));
893	break ;
894    case BD_F_MIX_CT1745 : /* sb16 */
895	if (mask == 0)
896	    mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */
897	recdev = 0 ;
898	if (mask & SOUND_MASK_MIC)
899	    recdev |= 1 ;
900	if (mask & SOUND_MASK_CD)
901	    recdev |= 6 ; /* l+r cd */
902	if (mask & SOUND_MASK_LINE)
903	    recdev |= 0x18 ; /* l+r line */
904	if (mask & SOUND_MASK_SYNTH)
905	    recdev |= 0x60 ; /* l+r midi */
906	sb_setmixer(d->io_base, SB16_IMASK_L, recdev);
907	sb_setmixer(d->io_base, SB16_IMASK_R, recdev);
908	/*
909	 * since the same volume controls apply to the input and
910	 * output sections, the best approach to have a consistent
911	 * behaviour among cards would be to disable the output path
912	 * on devices which are used to record.
913	 * However, since users like to have feedback, we only disable
914	 * the mike -- permanently.
915	 */
916        sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1);
917	break ;
918    }
919    d->mix_recsrc = mask;
920}
921
922static void
923sb_mixer_reset(snddev_info *d)
924{
925    int             i;
926
927    for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
928	sb_mixer_set(d, i, levels[i]);
929    if (d->bd_flags & BD_F_SB16) {
930	sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */
931	sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */
932	sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */
933    }
934    sb_set_recsrc(d, SOUND_MASK_MIC);
935}
936
937static int
938sb_mixer_set(snddev_info *d, int dev, int value)
939{
940    int left = value & 0x000000ff;
941    int right = (value & 0x0000ff00) >> 8;
942    int regoffs;
943    u_char   val;
944    mixer_tab *iomap;
945
946#ifdef JAZZ16
947    if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2)
948        return smw_mixer_set(dev, value);
949#endif
950
951    if (dev == SOUND_MIXER_RECSRC) {
952	sb_set_recsrc(d, value);
953	return 0 ;
954    }
955    if (left > 100)
956        left = 100;
957    if (right > 100)
958        right = 100;
959
960    if (dev > 31)
961        return EINVAL ;
962
963    if (!(d->mix_devs & (1 << dev)))      /* Not supported */
964        return EINVAL;
965
966    switch ( d->bd_flags & BD_F_MIX_MASK ) {
967    default:
968	/* mixer unknown, fail... */
969	return EINVAL ;/* XXX change this */
970    case BD_F_MIX_CT1345 :
971	iomap = &sbpro_mix ;
972	break;
973    case BD_F_MIX_CT1745 :
974	iomap = &sb16_mix ;
975	break;
976    /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
977    }
978    regoffs = (*iomap)[dev][LEFT_CHN].regno;
979    if (regoffs == 0)
980        return EINVAL;
981
982    val = sb_getmixer(d->io_base, regoffs);
983
984    change_bits(iomap, &val, dev, LEFT_CHN, left);
985
986    d->mix_levels[dev] = left | (left << 8);
987
988    if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) {    /* Change register */
989        sb_setmixer(d->io_base, regoffs, val);     /* Save the old one */
990        regoffs = (*iomap)[dev][RIGHT_CHN].regno;
991
992        if (regoffs == 0)
993            return 0 ;  /* Just left channel present */
994
995        val = sb_getmixer(d->io_base, regoffs);    /* Read the new one */
996    }
997    change_bits(iomap, &val, dev, RIGHT_CHN, right);
998
999    sb_setmixer(d->io_base, regoffs, val);
1000
1001    d->mix_levels[dev] = left | (right << 8);
1002    return 0 ; /* ok */
1003}
1004
1005/*
1006 * now support for some PnP boards.
1007 */
1008
1009#if NPNP > 0
1010static char *ess1868_probe(u_long csn, u_long vend_id);
1011static void ess1868_attach(u_long csn, u_long vend_id, char *name,
1012        struct isa_device *dev);
1013
1014static struct pnp_device ess1868 = {
1015        "ESS1868",
1016        ess1868_probe,
1017        ess1868_attach,
1018        &nsnd,  /* use this for all sound cards */
1019        &tty_imask      /* imask */
1020};
1021DATA_SET (pnpdevice_set, ess1868);
1022
1023static char *
1024ess1868_probe(u_long csn, u_long vend_id)
1025{
1026    /*
1027     * pnp X 1 os enable drq0 3 irq0 12 port0 0x240
1028     */
1029    if (vend_id == 0x68187316) {
1030	struct pnp_cinfo d ;
1031	read_pnp_parms ( &d , 1 ) ;
1032	if (d.enable == 0) {
1033	    printf("This is an ESS1868, but LDN 1 is disabled\n");
1034	    return NULL;
1035	}
1036        return "ESS1868" ;
1037    }
1038    return NULL ;
1039}
1040
1041static void
1042ess1868_attach(u_long csn, u_long vend_id, char *name,
1043        struct isa_device *dev)
1044{
1045    struct pnp_cinfo d ;
1046    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1047    int the_irq = 0 ;
1048
1049    tmp_d = sb_op_desc;
1050    snddev_last_probed = &tmp_d;
1051
1052#if 0
1053    read_pnp_parms ( &d , 3 );  /* disable LDN 3 */
1054    d.port[0] = 0 ;
1055    d.enable = 0 ;
1056    write_pnp_parms ( &d , 3 );
1057
1058    read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
1059    d.port[0] = 0 ;
1060    d.enable = 0 ;
1061    write_pnp_parms ( &d , 2 );
1062    read_pnp_parms ( &d , 0 ); /* read config base */
1063    tmp_d.conf_base = d.port[0];
1064    write_pnp_parms ( &d , 0 );
1065#endif
1066
1067    read_pnp_parms ( &d , 1 ) ;
1068    dev->id_iobase = d.port[0];
1069    d.port[1] = 0 ;
1070    d.port[2] = 0 ;
1071    write_pnp_parms ( &d , 1 );
1072    enable_pnp_card();
1073
1074    dev->id_drq = d.drq[0] ; /* primary dma */
1075    dev->id_irq = (1 << d.irq[0] ) ;
1076    dev->id_intr = pcmintr ;
1077    dev->id_flags = 0 /* DV_F_DUAL_DMA | (d.drq[1] ) */;
1078
1079    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1080    pcmattach(dev);
1081}
1082
1083static char *opti925_probe(u_long csn, u_long vend_id);
1084static void opti925_attach(u_long csn, u_long vend_id, char *name,
1085        struct isa_device *dev);
1086
1087static struct pnp_device opti925 = {
1088        "opti925",
1089        opti925_probe,
1090        opti925_attach,
1091        &nsnd,  /* use this for all sound cards */
1092        &tty_imask      /* imask */
1093};
1094DATA_SET (pnpdevice_set, opti925);
1095
1096static char *
1097opti925_probe(u_long csn, u_long vend_id)
1098{
1099    if (vend_id == 0x2509143e) {
1100	struct pnp_cinfo d ;
1101	read_pnp_parms ( &d , 1 ) ;
1102	if (d.enable == 0) {
1103	    printf("This is an OPTi925, but LDN 1 is disabled\n");
1104	    return NULL;
1105	}
1106        return "OPTi925" ;
1107    }
1108    return NULL ;
1109}
1110
1111static void
1112opti925_attach(u_long csn, u_long vend_id, char *name,
1113        struct isa_device *dev)
1114{
1115    struct pnp_cinfo d ;
1116    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1117    int the_irq = 0 ;
1118
1119    tmp_d = sb_op_desc;
1120    snddev_last_probed = &tmp_d;
1121
1122    read_pnp_parms ( &d , 3 );  /* disable LDN 3 */
1123    the_irq = d.irq[0];
1124    d.port[0] = 0 ;
1125    d.enable = 0 ;
1126    write_pnp_parms ( &d , 3 );
1127
1128    read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
1129    d.port[0] = 0 ;
1130    d.enable = 0 ;
1131    write_pnp_parms ( &d , 2 );
1132
1133    read_pnp_parms ( &d , 1 ) ;
1134    d.irq[0] = the_irq ;
1135    dev->id_iobase = d.port[0];
1136    write_pnp_parms ( &d , 1 );
1137    enable_pnp_card();
1138
1139    tmp_d.conf_base = d.port[3];
1140
1141    dev->id_drq = d.drq[0] ; /* primary dma */
1142    dev->id_irq = (1 << d.irq[0] ) ;
1143    dev->id_intr = pcmintr ;
1144    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1145
1146    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1147
1148    pcmattach(dev);
1149
1150}
1151
1152/*
1153 * A driver for some SB16pnp and compatibles...
1154 *
1155 * Avance Asound 100 -- 0x01009305
1156 * xxx               -- 0x2b008c0e
1157 *
1158 */
1159
1160static char *sb16pnp_probe(u_long csn, u_long vend_id);
1161static void sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1162        struct isa_device *dev);
1163
1164static struct pnp_device sb16pnp = {
1165        "SB16pnp",
1166        sb16pnp_probe,
1167        sb16pnp_attach,
1168        &nsnd,  /* use this for all sound cards */
1169        &tty_imask      /* imask */
1170};
1171DATA_SET (pnpdevice_set, sb16pnp);
1172
1173static char *
1174sb16pnp_probe(u_long csn, u_long vend_id)
1175{
1176    char *s = NULL ;
1177
1178    /*
1179     * The SB16/AWExx cards seem to differ in the fourth byte of
1180     * the vendor id, so I have just masked it for the time being...
1181     * Reported values are:
1182     * SB16 Value PnP:	0x2b008c0e
1183     * SB AWExx PnP:	0x39008c0e 0x9d008c0e 0xc3008c0e
1184     */
1185    if ( (vend_id & 0xffffff)  == (0x9d008c0e & 0xffffff) )
1186	s = "SB16 PnP";
1187    else if (vend_id == 0x01009305)
1188        s = "Avance Asound 100" ;
1189    if (s) {
1190	struct pnp_cinfo d;
1191	read_pnp_parms(&d, 0);
1192	if (d.enable == 0) {
1193	    printf("This is a %s, but LDN 0 is disabled\n", s);
1194	    return NULL ;
1195	}
1196	return s ;
1197    }
1198    return NULL ;
1199}
1200
1201static void
1202sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1203        struct isa_device *dev)
1204{
1205    struct pnp_cinfo d ;
1206    snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1207
1208    tmp_d = sb_op_desc;
1209    snddev_last_probed = &tmp_d;
1210
1211    read_pnp_parms ( &d , 0 ) ;
1212    d.port[1] = 0 ; /* only the first address is used */
1213    dev->id_iobase = d.port[0];
1214    tmp_d.synth_base = d.port[2];
1215    write_pnp_parms ( &d , 0 );
1216    enable_pnp_card();
1217
1218    dev->id_drq = d.drq[0] ; /* primary dma */
1219    dev->id_irq = (1 << d.irq[0] ) ;
1220    dev->id_intr = pcmintr ;
1221    dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1222
1223    pcm_info[dev->id_unit] = tmp_d;
1224    snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1225
1226    pcmattach(dev);
1227}
1228#endif /* NPNP */
1229
1230#endif
1231