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