Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.45.2.2
      1 /*	$NetBSD: sbdsp.c,v 1.45.2.2 1997/05/19 00:14:42 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991-1993 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the Computer Systems
     18  *	Engineering Group at Lawrence Berkeley Laboratory.
     19  * 4. Neither the name of the University nor of the Laboratory may be used
     20  *    to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 /*
     38  * SoundBlaster Pro code provided by John Kohl, based on lots of
     39  * information he gleaned from Steve Haehnichen <steve (at) vigra.com>'s
     40  * SBlast driver for 386BSD and DOS driver code from Daniel Sachs
     41  * <sachs (at) meibm15.cen.uiuc.edu>.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/errno.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/syslog.h>
     49 #include <sys/device.h>
     50 #include <sys/proc.h>
     51 #include <sys/buf.h>
     52 #include <vm/vm.h>
     53 
     54 #include <machine/cpu.h>
     55 #include <machine/intr.h>
     56 #include <machine/bus.h>
     57 
     58 #include <sys/audioio.h>
     59 #include <dev/audio_if.h>
     60 #include <dev/mulaw.h>
     61 
     62 #include <dev/isa/isavar.h>
     63 #include <dev/isa/isadmavar.h>
     64 #include <i386/isa/icu.h>			/* XXX BROKEN; WHY? */
     65 
     66 #include <dev/isa/sbreg.h>
     67 #include <dev/isa/sbdspvar.h>
     68 
     69 #ifdef AUDIO_DEBUG
     70 extern void Dprintf __P((const char *, ...));
     71 #define DPRINTF(x)	if (sbdspdebug) Dprintf x
     72 int	sbdspdebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #endif
     76 
     77 #ifndef SBDSP_NPOLL
     78 #define SBDSP_NPOLL 3000
     79 #endif
     80 
     81 struct {
     82 	int wdsp;
     83 	int rdsp;
     84 	int wmidi;
     85 } sberr;
     86 
     87 void sbdsp_srtotc __P((struct sbdsp_softc *sc, int sr, int isdac,
     88 		      int *tcp, int *modep));
     89 u_int sbdsp_jazz16_probe __P((struct sbdsp_softc *));
     90 
     91 /*
     92  * Time constant routines follow.  See SBK, section 12.
     93  * Although they don't come out and say it (in the docs),
     94  * the card clearly uses a 1MHz countdown timer, as the
     95  * low-speed formula (p. 12-4) is:
     96  *	tc = 256 - 10^6 / sr
     97  * In high-speed mode, the constant is the upper byte of a 16-bit counter,
     98  * and a 256MHz clock is used:
     99  *	tc = 65536 - 256 * 10^ 6 / sr
    100  * Since we can only use the upper byte of the HS TC, the two formulae
    101  * are equivalent.  (Why didn't they say so?)  E.g.,
    102  * 	(65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
    103  *
    104  * The crossover point (from low- to high-speed modes) is different
    105  * for the SBPRO and SB20.  The table on p. 12-5 gives the following data:
    106  *
    107  *				SBPRO			SB20
    108  *				-----			--------
    109  * input ls min			4	KHz		4	KHz
    110  * input ls max			23	KHz		13	KHz
    111  * input hs max			44.1	KHz		15	KHz
    112  * output ls min		4	KHz		4	KHz
    113  * output ls max		23	KHz		23	KHz
    114  * output hs max		44.1	KHz		44.1	KHz
    115  */
    116 #define SB_LS_MIN	0x06	/* 4000 Hz */
    117 #define	SB_8K		0x83	/* 8000 Hz */
    118 #define SBPRO_ADC_LS_MAX	0xd4	/* 22727 Hz */
    119 #define SBPRO_ADC_HS_MAX	0xea	/* 45454 Hz */
    120 #define SBCLA_ADC_LS_MAX	0xb3	/* 12987 Hz */
    121 #define SBCLA_ADC_HS_MAX	0xbd	/* 14925 Hz */
    122 #define SB_DAC_LS_MAX	0xd4	/* 22727 Hz */
    123 #define SB_DAC_HS_MAX	0xea	/* 45454 Hz */
    124 
    125 int	sbdsp16_wait __P((struct sbdsp_softc *));
    126 void	sbdsp_to __P((void *));
    127 void	sbdsp_pause __P((struct sbdsp_softc *));
    128 int	sbdsp16_setrate __P((struct sbdsp_softc *, int, int, int *));
    129 int	sbdsp_tctosr __P((struct sbdsp_softc *, int));
    130 int	sbdsp_set_timeconst __P((struct sbdsp_softc *, int));
    131 
    132 #ifdef AUDIO_DEBUG
    133 void sb_printsc __P((struct sbdsp_softc *));
    134 
    135 void
    136 sb_printsc(sc)
    137 	struct sbdsp_softc *sc;
    138 {
    139 	int i;
    140 
    141 	printf("open %d dmachan %d/%d/%d iobase 0x%x irq %d\n",
    142 	    (int)sc->sc_open, sc->dmachan, sc->sc_drq8, sc->sc_drq16,
    143 	    sc->sc_iobase, sc->sc_irq);
    144 	printf("irate %d itc %d imode %d orate %d otc %d omode %d\n",
    145 	    sc->sc_irate, sc->sc_itc, sc->sc_imode,
    146 	    sc->sc_orate, sc->sc_otc, sc->sc_omode);
    147 	printf("outport %u inport %u spkron %u nintr %lu\n",
    148 	    sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
    149 	printf("precision %u channels %d intr %p arg %p\n",
    150 	    sc->sc_precision, sc->sc_channels, sc->sc_intr, sc->sc_arg);
    151 	printf("gain:");
    152 	for (i = 0; i < SB_NDEVS; i++)
    153 		printf(" %u", sc->gain[i]);
    154 	printf("\n");
    155 }
    156 #endif /* AUDIO_DEBUG */
    157 
    158 /*
    159  * Probe / attach routines.
    160  */
    161 
    162 /*
    163  * Probe for the soundblaster hardware.
    164  */
    165 int
    166 sbdsp_probe(sc)
    167 	struct sbdsp_softc *sc;
    168 {
    169 
    170 	if (sbdsp_reset(sc) < 0) {
    171 		DPRINTF(("sbdsp: couldn't reset card\n"));
    172 		return 0;
    173 	}
    174 	/* if flags set, go and probe the jazz16 stuff */
    175 	if (sc->sc_dev.dv_cfdata->cf_flags != 0) {
    176 		sc->sc_model = sbdsp_jazz16_probe(sc);
    177 	} else {
    178 		sc->sc_model = sbversion(sc);
    179 	}
    180 
    181 #if 0
    182 	sc->sc_model = 0x100;	/* XXX pretend to be just a tired old SB XXX */
    183 #endif
    184 
    185 	return 1;
    186 }
    187 
    188 /*
    189  * Try add-on stuff for Jazz16.
    190  */
    191 u_int
    192 sbdsp_jazz16_probe(sc)
    193 	struct sbdsp_softc *sc;
    194 {
    195 	static u_char jazz16_irq_conf[16] = {
    196 	    -1, -1, 0x02, 0x03,
    197 	    -1, 0x01, -1, 0x04,
    198 	    -1, 0x02, 0x05, -1,
    199 	    -1, -1, -1, 0x06};
    200 	static u_char jazz16_drq_conf[8] = {
    201 	    -1, 0x01, -1, 0x02,
    202 	    -1, 0x03, -1, 0x04};
    203 
    204 	u_int rval = sbversion(sc);
    205 	bus_space_tag_t iot = sc->sc_iot;
    206 	bus_space_handle_t ioh;
    207 
    208 	DPRINTF(("jazz16 probe\n"));
    209 
    210 	if (bus_space_map(iot, JAZZ16_CONFIG_PORT, 1, 0, &ioh)) {
    211 		DPRINTF(("bus map failed\n"));
    212 		return rval;
    213 	}
    214 
    215 	if (jazz16_drq_conf[sc->sc_drq8] == (u_char)-1 ||
    216 	    jazz16_irq_conf[sc->sc_irq] == (u_char)-1) {
    217 		DPRINTF(("drq/irq check failed\n"));
    218 		goto done;		/* give up, we can't do it. */
    219 	}
    220 
    221 	bus_space_write_1(iot, ioh, 0, JAZZ16_WAKEUP);
    222 	delay(10000);			/* delay 10 ms */
    223 	bus_space_write_1(iot, ioh, 0, JAZZ16_SETBASE);
    224 	bus_space_write_1(iot, ioh, 0, sc->sc_iobase & 0x70);
    225 
    226 	if (sbdsp_reset(sc) < 0) {
    227 		DPRINTF(("sbdsp_reset check failed\n"));
    228 		goto done;		/* XXX? what else could we do? */
    229 	}
    230 
    231 	if (sbdsp_wdsp(sc, JAZZ16_READ_VER)) {
    232 		DPRINTF(("read16 setup failed\n"));
    233 		goto done;
    234 	}
    235 
    236 	if (sbdsp_rdsp(sc) != JAZZ16_VER_JAZZ) {
    237 		DPRINTF(("read16 failed\n"));
    238 		goto done;
    239 	}
    240 
    241 	/* XXX set both 8 & 16-bit drq to same channel, it works fine. */
    242 	sc->sc_drq16 = sc->sc_drq8;
    243 	if (sbdsp_wdsp(sc, JAZZ16_SET_DMAINTR) ||
    244 	    sbdsp_wdsp(sc, (jazz16_drq_conf[sc->sc_drq16] << 4) |
    245 		jazz16_drq_conf[sc->sc_drq8]) ||
    246 	    sbdsp_wdsp(sc, jazz16_irq_conf[sc->sc_irq])) {
    247 		DPRINTF(("sbdsp: can't write jazz16 probe stuff\n"));
    248 	} else {
    249 		DPRINTF(("jazz16 detected!\n"));
    250 		rval |= MODEL_JAZZ16;
    251 	}
    252 
    253 done:
    254 	bus_space_unmap(iot, ioh, 1);
    255 	return rval;
    256 }
    257 
    258 /*
    259  * Attach hardware to driver, attach hardware driver to audio
    260  * pseudo-device driver .
    261  */
    262 void
    263 sbdsp_attach(sc)
    264 	struct sbdsp_softc *sc;
    265 {
    266 
    267 	/* Set defaults */
    268 	if (ISSB16CLASS(sc))
    269 		sc->sc_irate = sc->sc_orate = 8000;
    270 	else if (ISSBPROCLASS(sc))
    271 		sc->sc_itc = sc->sc_otc = SB_8K;
    272   	else
    273 		sc->sc_itc = sc->sc_otc = SB_8K;
    274 	sc->sc_precision = 8;
    275 	sc->sc_channels = 1;
    276 
    277 	if (sc->sc_drq8 != -1) {
    278 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq8,
    279 		    MAXPHYS /* XXX */, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    280 			printf("%s: can't create map for drq %d\n",
    281 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    282 			return;
    283 		}
    284 	}
    285 	if (sc->sc_drq16 != -1 && sc->sc_drq16 != sc->sc_drq8) {
    286 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq16,
    287 		    MAXPHYS /* XXX */, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    288 			printf("%s: can't create map for drq %d\n",
    289 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    290 			return;
    291 		}
    292 	}
    293 	(void) sbdsp_set_in_port(sc, SB_MIC_PORT);
    294 	(void) sbdsp_set_out_port(sc, SB_SPEAKER);
    295 
    296 	if (ISSBPROCLASS(sc)) {
    297 		int i;
    298 
    299 		/* set mixer to default levels, by sending a mixer
    300                    reset command. */
    301 		sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
    302 		/* then some adjustments :) */
    303 		sbdsp_mix_write(sc, SBP_CD_VOL,
    304 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    305 		sbdsp_mix_write(sc, SBP_DAC_VOL,
    306 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    307 		sbdsp_mix_write(sc, SBP_MASTER_VOL,
    308 				sbdsp_stereo_vol(SBP_MAXVOL/2, SBP_MAXVOL/2));
    309 		sbdsp_mix_write(sc, SBP_LINE_VOL,
    310 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    311 		for (i = 0; i < SB_NDEVS; i++)
    312 			sc->gain[i] = sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL);
    313 		sc->in_filter = 0;	/* no filters turned on, please */
    314 	}
    315 
    316 	printf(": dsp v%d.%02d%s\n",
    317 	       SBVER_MAJOR(sc->sc_model), SBVER_MINOR(sc->sc_model),
    318 	       ISJAZZ16(sc) ? ": <Jazz16>" : "");
    319 }
    320 
    321 /*
    322  * Various routines to interface to higher level audio driver
    323  */
    324 
    325 void
    326 sbdsp_mix_write(sc, mixerport, val)
    327 	struct sbdsp_softc *sc;
    328 	int mixerport;
    329 	int val;
    330 {
    331 	bus_space_tag_t iot = sc->sc_iot;
    332 	bus_space_handle_t ioh = sc->sc_ioh;
    333 	int s;
    334 
    335 	s = splaudio();
    336 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    337 	delay(20);
    338 	bus_space_write_1(iot, ioh, SBP_MIXER_DATA, val);
    339 	delay(30);
    340 	splx(s);
    341 }
    342 
    343 int
    344 sbdsp_mix_read(sc, mixerport)
    345 	struct sbdsp_softc *sc;
    346 	int mixerport;
    347 {
    348 	bus_space_tag_t iot = sc->sc_iot;
    349 	bus_space_handle_t ioh = sc->sc_ioh;
    350 	int val;
    351 	int s;
    352 
    353 	s = splaudio();
    354 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    355 	delay(20);
    356 	val = bus_space_read_1(iot, ioh, SBP_MIXER_DATA);
    357 	delay(30);
    358 	splx(s);
    359 	return val;
    360 }
    361 
    362 int
    363 sbdsp_query_encoding(addr, fp)
    364 	void *addr;
    365 	struct audio_encoding *fp;
    366 {
    367 	struct sbdsp_softc *sc = addr;
    368 
    369 	switch (fp->index) {
    370 	case 0:
    371 		strcpy(fp->name, AudioEulinear);
    372 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    373 		fp->precision = 8;
    374 		fp->flags = 0;
    375 		break;
    376 	case 1:
    377 		strcpy(fp->name, AudioEmulaw);
    378 		fp->encoding = AUDIO_ENCODING_ULAW;
    379 		fp->precision = 8;
    380 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    381 		break;
    382 	case 2:
    383 		strcpy(fp->name, AudioElinear);
    384 		fp->encoding = AUDIO_ENCODING_LINEAR;
    385 		fp->precision = 8;
    386 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    387 		break;
    388 	default:
    389 		if (!(ISSB16CLASS(sc) || ISJAZZ16(sc)))
    390 			return (EINVAL);
    391 		switch(fp->index) {
    392 		case 3:
    393 			strcpy(fp->name, AudioElinear_le);
    394 			fp->encoding = AUDIO_ENCODING_LINEAR_LE;
    395 			fp->precision = 16;
    396 			fp->flags = 0;
    397 			break;
    398 		case 4:
    399 			strcpy(fp->name, AudioEulinear_le);
    400 			fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    401 			fp->precision = 16;
    402 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    403 			break;
    404 		case 5:
    405 			strcpy(fp->name, AudioElinear_be);
    406 			fp->encoding = AUDIO_ENCODING_LINEAR_BE;
    407 			fp->precision = 16;
    408 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    409 			break;
    410 		case 6:
    411 			strcpy(fp->name, AudioEulinear_be);
    412 			fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    413 			fp->precision = 16;
    414 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    415 			break;
    416 		default:
    417 			return (EINVAL);
    418 		}
    419 	}
    420 	return (0);
    421 }
    422 
    423 int
    424 sbdsp_set_params(addr, mode, p, q)
    425 	void *addr;
    426 	int mode;
    427 	struct audio_params *p, *q;
    428 {
    429 	struct sbdsp_softc *sc = addr;
    430 	int maxspeed;
    431 	void (*swcode) __P((void *, u_char *buf, int cnt));
    432 	int can16 = ISSB16CLASS(sc) || ISJAZZ16(sc);
    433 
    434 	switch (p->encoding) {
    435 	case AUDIO_ENCODING_LINEAR_LE:
    436 		if (p->precision == 8)
    437 			swcode = change_sign8;
    438 		else if (can16)
    439 			swcode = 0;
    440 		else
    441 			return EINVAL;
    442 		break;
    443 	case AUDIO_ENCODING_ULINEAR_LE:
    444 		if (p->precision == 8)
    445 			swcode = 0;
    446 		else if (can16)
    447 			swcode = change_sign16;
    448 		else
    449 			return EINVAL;
    450 		break;
    451 	case AUDIO_ENCODING_LINEAR_BE:
    452 		if (p->precision == 8)
    453 			swcode = change_sign8;
    454 		else if (can16)
    455 			swcode = swap_bytes;
    456 		else
    457 			return EINVAL;
    458 		break;
    459 	case AUDIO_ENCODING_ULINEAR_BE:
    460 		if (p->precision == 8)
    461 			swcode = 0;
    462 		else if (can16)
    463 			swcode = swap_bytes_change_sign16;
    464 		else
    465 			return EINVAL;
    466 		break;
    467 	case AUDIO_ENCODING_ULAW:
    468 		swcode = mode == AUMODE_PLAY ?
    469 			  mulaw_to_ulinear8 : ulinear8_to_mulaw;
    470 		break;
    471 	default:
    472 		return EINVAL;
    473 	}
    474 
    475 
    476 	if (!ISSBPROCLASS(sc)) {
    477 		/* v 1.x or v 2.x */
    478 		if (mode == AUMODE_PLAY) {
    479 			if (ISSB2CLASS(sc) && SBVER_MINOR(sc->sc_model) > 0)
    480 				maxspeed = 44100;
    481 			else
    482 				maxspeed = 22727;
    483 		} else
    484 			maxspeed = 12987;
    485 		if (p->sample_rate < 4000 || p->sample_rate > maxspeed)
    486 			return EINVAL;
    487 		if (p->channels != 1)
    488 			return EINVAL;
    489 	} else if (!can16) {
    490 		/* v 3.x (SBPRO) */
    491 		if (p->channels == 1)
    492 			maxspeed = 45454;
    493 		else
    494 			maxspeed = 22727;
    495 		if (p->sample_rate < 4000 || p->sample_rate > maxspeed)
    496 			return EINVAL;
    497 	} else {
    498 		/* >= v 4.x */
    499 		if (p->sample_rate < 4000 || p->sample_rate > 44100)
    500 			return EINVAL;
    501 	}
    502 
    503 	if (ISSB16CLASS(sc)) {
    504 		if (mode == AUMODE_RECORD)
    505 			sc->sc_irate = p->sample_rate;
    506 		else
    507 			sc->sc_orate = p->sample_rate;
    508 	} else {
    509 		sbdsp_srtotc(sc, p->sample_rate, SB_OUTPUT_RATE,
    510 			     &sc->sc_otc, &sc->sc_omode);
    511 		p->sample_rate = sbdsp_tctosr(sc, sc->sc_otc);
    512 	}
    513 
    514 	sc->sc_precision = p->precision;
    515 	sc->sc_channels = p->channels;
    516 
    517 	p->sw_code = swcode;
    518 
    519 	/* Update setting for the other mode. */
    520 	q->encoding = p->encoding;
    521 	q->channels = p->channels;
    522 	q->precision = p->precision;
    523 
    524 	/*
    525 	 * XXX
    526 	 * Should wait for chip to be idle.
    527 	 */
    528 	sc->sc_dmadir = SB_DMA_NONE;
    529 
    530 	return 0;
    531 }
    532 
    533 int
    534 sbdsp_set_ifilter(addr, which)
    535 	void *addr;
    536 	int which;
    537 {
    538 	register struct sbdsp_softc *sc = addr;
    539 	int mixval;
    540 
    541 	/* XXXX SB16 */
    542 	if (ISSBPROCLASS(sc)) {
    543 		mixval = sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_IFILTER_MASK;
    544 		switch (which) {
    545 		case 0:
    546 			mixval |= SBP_FILTER_OFF;
    547 			break;
    548 		case SBP_TREBLE_EQ:
    549 			mixval |= SBP_FILTER_ON | SBP_IFILTER_HIGH;
    550 			break;
    551 		case SBP_BASS_EQ:
    552 			mixval |= SBP_FILTER_ON | SBP_IFILTER_LOW;
    553 			break;
    554 		default:
    555 			return (EINVAL);
    556 		}
    557 		sc->in_filter = mixval & SBP_IFILTER_MASK;
    558 		sbdsp_mix_write(sc, SBP_INFILTER, mixval);
    559 		return (0);
    560 	} else
    561 		return (EINVAL);
    562 }
    563 
    564 int
    565 sbdsp_get_ifilter(addr)
    566 	void *addr;
    567 {
    568 	register struct sbdsp_softc *sc = addr;
    569 
    570 	/* XXXX SB16 */
    571 	if (ISSBPROCLASS(sc)) {
    572 		sc->in_filter =
    573 		    sbdsp_mix_read(sc, SBP_INFILTER) & SBP_IFILTER_MASK;
    574 		switch (sc->in_filter) {
    575 		case SBP_FILTER_ON|SBP_IFILTER_HIGH:
    576 			return (SBP_TREBLE_EQ);
    577 		case SBP_FILTER_ON|SBP_IFILTER_LOW:
    578 			return (SBP_BASS_EQ);
    579 		case SBP_FILTER_OFF:
    580 		default:
    581 			return (0);
    582 		}
    583 	} else
    584 		return (0);
    585 }
    586 
    587 int
    588 sbdsp_set_out_port(addr, port)
    589 	void *addr;
    590 	int port;
    591 {
    592 	register struct sbdsp_softc *sc = addr;
    593 
    594 	sc->out_port = port; /* Just record it */
    595 
    596 	return (0);
    597 }
    598 
    599 int
    600 sbdsp_get_out_port(addr)
    601 	void *addr;
    602 {
    603 	register struct sbdsp_softc *sc = addr;
    604 
    605 	return (sc->out_port);
    606 }
    607 
    608 
    609 int
    610 sbdsp_set_in_port(addr, port)
    611 	void *addr;
    612 	int port;
    613 {
    614 	register struct sbdsp_softc *sc = addr;
    615 	int mixport, sbport;
    616 
    617 	if (ISSBPROCLASS(sc)) {
    618 		switch (port) {
    619 		case SB_MIC_PORT:
    620 			sbport = SBP_FROM_MIC;
    621 			mixport = SBP_MIC_VOL;
    622 			break;
    623 		case SB_LINE_IN_PORT:
    624 			sbport = SBP_FROM_LINE;
    625 			mixport = SBP_LINE_VOL;
    626 			break;
    627 		case SB_CD_PORT:
    628 			sbport = SBP_FROM_CD;
    629 			mixport = SBP_CD_VOL;
    630 			break;
    631 		case SB_DAC_PORT:
    632 		case SB_FM_PORT:
    633 		default:
    634 			return (EINVAL);
    635 		}
    636 	} else {
    637 		switch (port) {
    638 		case SB_MIC_PORT:
    639 			sbport = SBP_FROM_MIC;
    640 			mixport = SBP_MIC_VOL;
    641 			break;
    642 		default:
    643 			return (EINVAL);
    644 		}
    645 	}
    646 
    647 	sc->in_port = port;	/* Just record it */
    648 
    649 	/* XXXX SB16 */
    650 	if (ISSBPROCLASS(sc)) {
    651 		/* record from that port */
    652 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
    653 		    SBP_RECORD_FROM(sbport, SBP_FILTER_OFF, SBP_IFILTER_HIGH));
    654 		/* fetch gain from that port */
    655 		sc->gain[port] = sbdsp_mix_read(sc, mixport);
    656 	}
    657 
    658 	return (0);
    659 }
    660 
    661 int
    662 sbdsp_get_in_port(addr)
    663 	void *addr;
    664 {
    665 	register struct sbdsp_softc *sc = addr;
    666 
    667 	return (sc->in_port);
    668 }
    669 
    670 
    671 int
    672 sbdsp_speaker_ctl(addr, newstate)
    673 	void *addr;
    674 	int newstate;
    675 {
    676 	register struct sbdsp_softc *sc = addr;
    677 
    678 	if ((newstate == SPKR_ON) &&
    679 	    (sc->spkr_state == SPKR_OFF)) {
    680 		sbdsp_spkron(sc);
    681 		sc->spkr_state = SPKR_ON;
    682 	}
    683 	if ((newstate == SPKR_OFF) &&
    684 	    (sc->spkr_state == SPKR_ON)) {
    685 		sbdsp_spkroff(sc);
    686 		sc->spkr_state = SPKR_OFF;
    687 	}
    688 	return(0);
    689 }
    690 
    691 int
    692 sbdsp_round_blocksize(addr, blk)
    693 	void *addr;
    694 	int blk;
    695 {
    696 	register struct sbdsp_softc *sc = addr;
    697 
    698 	sc->sc_last_hs_size = 0;
    699 
    700 	/* Don't try to DMA too much at once. */
    701 	if (blk > NBPG)
    702 		blk = NBPG;
    703 
    704 	/* Round to a multiple of the sample size. */
    705 	blk &= -(sc->sc_channels * sc->sc_precision / 8);
    706 
    707 	if (blk > 1364)
    708 		blk = 1364;	/* XXX */
    709 
    710 	return (blk);
    711 }
    712 
    713 int
    714 sbdsp_commit_settings(addr)
    715 	void *addr;
    716 {
    717 	return 0;
    718 }
    719 
    720 
    721 int
    722 sbdsp_open(sc, dev, flags)
    723 	register struct sbdsp_softc *sc;
    724 	dev_t dev;
    725 	int flags;
    726 {
    727         DPRINTF(("sbdsp_open: sc=0x%x\n", sc));
    728 
    729 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
    730 		return ENXIO;
    731 
    732 	sc->sc_open = 1;
    733 	sc->sc_mintr = 0;
    734 	if (ISSBPROCLASS(sc) &&
    735 	    sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
    736 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
    737 		/* we'll readjust when it's time for DMA. */
    738 	}
    739 
    740 	/*
    741 	 * Leave most things as they were; users must change things if
    742 	 * the previous process didn't leave it they way they wanted.
    743 	 * Looked at another way, it's easy to set up a configuration
    744 	 * in one program and leave it for another to inherit.
    745 	 */
    746 	DPRINTF(("sbdsp_open: opened\n"));
    747 
    748 	return 0;
    749 }
    750 
    751 void
    752 sbdsp_close(addr)
    753 	void *addr;
    754 {
    755 	struct sbdsp_softc *sc = addr;
    756 
    757         DPRINTF(("sbdsp_close: sc=0x%x\n", sc));
    758 
    759 	sc->sc_open = 0;
    760 	sbdsp_spkroff(sc);
    761 	sc->spkr_state = SPKR_OFF;
    762 	sc->sc_mintr = 0;
    763 	sbdsp_haltdma(sc);
    764 
    765 	DPRINTF(("sbdsp_close: closed\n"));
    766 }
    767 
    768 /*
    769  * Lower-level routines
    770  */
    771 
    772 /*
    773  * Reset the card.
    774  * Return non-zero if the card isn't detected.
    775  */
    776 int
    777 sbdsp_reset(sc)
    778 	register struct sbdsp_softc *sc;
    779 {
    780 	bus_space_tag_t iot = sc->sc_iot;
    781 	bus_space_handle_t ioh = sc->sc_ioh;
    782 
    783 	sc->sc_intr = 0;
    784 	if (sc->sc_dmadir != SB_DMA_NONE) {
    785 		isa_dmaabort(sc->sc_isa, sc->dmachan);
    786 		sc->sc_dmadir = SB_DMA_NONE;
    787 	}
    788 	sc->sc_last_hs_size = 0;
    789 
    790 	/*
    791 	 * See SBK, section 11.3.
    792 	 * We pulse a reset signal into the card.
    793 	 * Gee, what a brilliant hardware design.
    794 	 */
    795 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
    796 	delay(10);
    797 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
    798 	delay(30);
    799 	if (sbdsp_rdsp(sc) != SB_MAGIC)
    800 		return -1;
    801 
    802 	return 0;
    803 }
    804 
    805 int
    806 sbdsp16_wait(sc)
    807 	struct sbdsp_softc *sc;
    808 {
    809 	bus_space_tag_t iot = sc->sc_iot;
    810 	bus_space_handle_t ioh = sc->sc_ioh;
    811 	register int i;
    812 
    813 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    814 		register u_char x;
    815 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    816 		delay(10);
    817 		if ((x & SB_DSP_BUSY) == 0)
    818 			continue;
    819 		return 0;
    820 	}
    821 	++sberr.wdsp;
    822 	return -1;
    823 }
    824 
    825 /*
    826  * Write a byte to the dsp.
    827  * XXX We are at the mercy of the card as we use a
    828  * polling loop and wait until it can take the byte.
    829  */
    830 int
    831 sbdsp_wdsp(sc, v)
    832 	struct sbdsp_softc *sc;
    833 	int v;
    834 {
    835 	bus_space_tag_t iot = sc->sc_iot;
    836 	bus_space_handle_t ioh = sc->sc_ioh;
    837 	register int i;
    838 
    839 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    840 		register u_char x;
    841 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    842 		delay(10);
    843 		if ((x & SB_DSP_BUSY) != 0)
    844 			continue;
    845 		bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
    846 		delay(10);
    847 		return 0;
    848 	}
    849 	++sberr.wdsp;
    850 	return -1;
    851 }
    852 
    853 /*
    854  * Read a byte from the DSP, using polling.
    855  */
    856 int
    857 sbdsp_rdsp(sc)
    858 	struct sbdsp_softc *sc;
    859 {
    860 	bus_space_tag_t iot = sc->sc_iot;
    861 	bus_space_handle_t ioh = sc->sc_ioh;
    862 	register int i;
    863 
    864 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    865 		register u_char x;
    866 		x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
    867 		delay(10);
    868 		if ((x & SB_DSP_READY) == 0)
    869 			continue;
    870 		x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
    871 		delay(10);
    872 		return x;
    873 	}
    874 	++sberr.rdsp;
    875 	return -1;
    876 }
    877 
    878 /*
    879  * Doing certain things (like toggling the speaker) make
    880  * the SB hardware go away for a while, so pause a little.
    881  */
    882 void
    883 sbdsp_to(arg)
    884 	void *arg;
    885 {
    886 	wakeup(arg);
    887 }
    888 
    889 void
    890 sbdsp_pause(sc)
    891 	struct sbdsp_softc *sc;
    892 {
    893 	extern int hz;
    894 
    895 	timeout(sbdsp_to, sbdsp_to, hz/8);
    896 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
    897 }
    898 
    899 /*
    900  * Turn on the speaker.  The SBK documention says this operation
    901  * can take up to 1/10 of a second.  Higher level layers should
    902  * probably let the task sleep for this amount of time after
    903  * calling here.  Otherwise, things might not work (because
    904  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
    905  *
    906  * These engineers had their heads up their ass when
    907  * they designed this card.
    908  */
    909 void
    910 sbdsp_spkron(sc)
    911 	struct sbdsp_softc *sc;
    912 {
    913 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
    914 	sbdsp_pause(sc);
    915 }
    916 
    917 /*
    918  * Turn off the speaker; see comment above.
    919  */
    920 void
    921 sbdsp_spkroff(sc)
    922 	struct sbdsp_softc *sc;
    923 {
    924 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
    925 	sbdsp_pause(sc);
    926 }
    927 
    928 /*
    929  * Read the version number out of the card.  Return major code
    930  * in high byte, and minor code in low byte.
    931  */
    932 short
    933 sbversion(sc)
    934 	struct sbdsp_softc *sc;
    935 {
    936 	short v;
    937 
    938 	if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
    939 		return 0;
    940 	v = sbdsp_rdsp(sc) << 8;
    941 	v |= sbdsp_rdsp(sc);
    942 	return ((v >= 0) ? v : 0);
    943 }
    944 
    945 /*
    946  * Halt a DMA in progress.  A low-speed transfer can be
    947  * resumed with sbdsp_contdma().
    948  */
    949 int
    950 sbdsp_haltdma(addr)
    951 	void *addr;
    952 {
    953 	register struct sbdsp_softc *sc = addr;
    954 
    955 	DPRINTF(("sbdsp_haltdma: sc=0x%x\n", sc));
    956 
    957 	sbdsp_reset(sc);
    958 	return 0;
    959 }
    960 
    961 int
    962 sbdsp_contdma(addr)
    963 	void *addr;
    964 {
    965 	register struct sbdsp_softc *sc = addr;
    966 
    967 	DPRINTF(("sbdsp_contdma: sc=0x%x\n", sc));
    968 
    969 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
    970 	(void)sbdsp_wdsp(sc, SB_DSP_CONT);
    971 	return(0);
    972 }
    973 
    974 /*
    975  * Convert a linear sampling rate into the DAC time constant.
    976  * Set *mode to indicate the high/low-speed DMA operation.
    977  * Because of limitations of the card, not all rates are possible.
    978  * We return the time constant of the closest possible rate.
    979  * The sampling rate limits are different for the DAC and ADC,
    980  * so isdac indicates output, and !isdac indicates input.
    981  */
    982 void
    983 sbdsp_srtotc(sc, sr, isdac, tcp, modep)
    984 	register struct sbdsp_softc *sc;
    985 	int sr;
    986 	int isdac;
    987 	int *tcp, *modep;
    988 {
    989 	int tc, realtc, mode;
    990 
    991 	/*
    992 	 * Don't forget to compute which mode we'll be in based on whether
    993 	 * we need to double the rate for stereo on SBPRO.
    994 	 */
    995 
    996 	if (sr == 0) {
    997 		tc = SB_LS_MIN;
    998 		mode = SB_ADAC_LS;
    999 		goto out;
   1000 	}
   1001 
   1002 	tc = 256 - (1000000 / sr);
   1003 
   1004 	if (sc->sc_channels == 2 && ISSBPRO(sc))
   1005 		/* compute based on 2x sample rate when needed */
   1006 		realtc = 256 - ( 500000 / sr);
   1007 	else
   1008 		realtc = tc;
   1009 
   1010 	if (tc < SB_LS_MIN) {
   1011 		tc = SB_LS_MIN;
   1012 		mode = SB_ADAC_LS;	/* NB: 2x minimum speed is still low
   1013 					 * speed mode. */
   1014 		goto out;
   1015 	} else if (isdac) {
   1016 		if (realtc <= SB_DAC_LS_MAX)
   1017 			mode = SB_ADAC_LS;
   1018 		else {
   1019 			mode = SB_ADAC_HS;
   1020 			if (tc > SB_DAC_HS_MAX)
   1021 				tc = SB_DAC_HS_MAX;
   1022 		}
   1023 	} else {
   1024 		int adc_ls_max, adc_hs_max;
   1025 
   1026 		/* XXX use better rounding--compare distance to nearest tc on both
   1027 		   sides of requested speed */
   1028 		if (ISSBPROCLASS(sc)) {
   1029 			adc_ls_max = SBPRO_ADC_LS_MAX;
   1030 			adc_hs_max = SBPRO_ADC_HS_MAX;
   1031 		} else {
   1032 			adc_ls_max = SBCLA_ADC_LS_MAX;
   1033 			adc_hs_max = SBCLA_ADC_HS_MAX;
   1034 		}
   1035 
   1036 		if (realtc <= adc_ls_max)
   1037 			mode = SB_ADAC_LS;
   1038 		else {
   1039 			mode = SB_ADAC_HS;
   1040 			if (tc > adc_hs_max)
   1041 				tc = adc_hs_max;
   1042 		}
   1043 	}
   1044 
   1045 out:
   1046 	*tcp = tc;
   1047 	*modep = mode;
   1048 }
   1049 
   1050 /*
   1051  * Convert a DAC time constant to a sampling rate.
   1052  * See SBK, section 12.
   1053  */
   1054 int
   1055 sbdsp_tctosr(sc, tc)
   1056 	register struct sbdsp_softc *sc;
   1057 	int tc;
   1058 {
   1059 	int adc;
   1060 
   1061 	if (ISSBPROCLASS(sc))
   1062 		adc = SBPRO_ADC_HS_MAX;
   1063 	else
   1064 		adc = SBCLA_ADC_HS_MAX;
   1065 
   1066 	if (tc > adc)
   1067 		tc = adc;
   1068 
   1069 	return (1000000 / (256 - tc));
   1070 }
   1071 
   1072 int
   1073 sbdsp_set_timeconst(sc, tc)
   1074 	register struct sbdsp_softc *sc;
   1075 	int tc;
   1076 {
   1077 	/*
   1078 	 * A SBPro in stereo mode uses time constants at double the
   1079 	 * actual rate.
   1080 	 */
   1081 	if (ISSBPRO(sc) && sc->sc_channels == 2)
   1082 		tc = 256 - ((256 - tc) / 2);
   1083 
   1084 	DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
   1085 
   1086 	if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
   1087 	    sbdsp_wdsp(sc, tc) < 0)
   1088 		return (EIO);
   1089 
   1090 	return (0);
   1091 }
   1092 
   1093 int
   1094 sbdsp_dma_input(addr, p, cc, intr, arg)
   1095 	void *addr;
   1096 	void *p;
   1097 	int cc;
   1098 	void (*intr) __P((void *));
   1099 	void *arg;
   1100 {
   1101 	register struct sbdsp_softc *sc = addr;
   1102 
   1103 #ifdef AUDIO_DEBUG
   1104 	if (sbdspdebug > 1)
   1105 		Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1106 #endif
   1107 	if (sc->sc_channels == 2 && (cc & 1)) {
   1108 		DPRINTF(("sbdsp_dma_input: stereo input, odd bytecnt\n"));
   1109 		return EIO;
   1110 	}
   1111 
   1112 	if (sc->sc_dmadir != SB_DMA_IN) {
   1113 		if (ISSBPRO(sc)) {
   1114 			if (sc->sc_channels == 2) {
   1115 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1116 					if (sbdsp_wdsp(sc,
   1117 						       JAZZ16_RECORD_STEREO) < 0) {
   1118 						goto badmode;
   1119 					}
   1120 				} else if (sbdsp_wdsp(sc,
   1121 						      SB_DSP_RECORD_STEREO) < 0)
   1122 					goto badmode;
   1123 				sbdsp_mix_write(sc, SBP_INFILTER,
   1124 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1125 				    ~SBP_IFILTER_MASK) | SBP_FILTER_OFF);
   1126 			} else {
   1127 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1128 					if (sbdsp_wdsp(sc,
   1129 						       JAZZ16_RECORD_MONO) < 0)
   1130 					{
   1131 						goto badmode;
   1132 					}
   1133 				} else if (sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0)
   1134 					goto badmode;
   1135 				sbdsp_mix_write(sc, SBP_INFILTER,
   1136 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1137 				    ~SBP_IFILTER_MASK) | sc->in_filter);
   1138 			}
   1139 		}
   1140 
   1141 		if (ISSB16CLASS(sc)) {
   1142 			if (sbdsp_wdsp(sc, SB_DSP16_INPUTRATE) < 0 ||
   1143 			    sbdsp_wdsp(sc, sc->sc_irate >> 8) < 0 ||
   1144 			    sbdsp_wdsp(sc, sc->sc_irate) < 0)
   1145 				goto giveup;
   1146 		} else
   1147 			sbdsp_set_timeconst(sc, sc->sc_itc);
   1148 
   1149 		sc->sc_dmadir = SB_DMA_IN;
   1150 		sc->dmaflags = DMAMODE_READ;
   1151 		if (ISSB2CLASS(sc))
   1152 			sc->dmaflags |= DMAMODE_LOOP;
   1153 	} else {
   1154 		/* Already started; just return. */
   1155 		if (ISSB2CLASS(sc))
   1156 			return 0;
   1157 	}
   1158 
   1159 	sc->dmaaddr = p;
   1160 	sc->dmacnt = ISSB2CLASS(sc) ? (NBPG/cc)*cc : cc;
   1161 	sc->dmachan = sc->sc_precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1162 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1163 	    sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1164 	sc->sc_intr = intr;
   1165 	sc->sc_arg = arg;
   1166 
   1167 	if (sc->sc_precision == 16)
   1168 		cc >>= 1;
   1169 	--cc;
   1170 	if (ISSB16CLASS(sc)) {
   1171 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_RDMA_16 :
   1172 							    SB_DSP16_RDMA_8) < 0 ||
   1173 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1174 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1175 		    sbdsp16_wait(sc) ||
   1176 		    sbdsp_wdsp(sc, cc) < 0 ||
   1177 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1178 			DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
   1179 			goto giveup;
   1180 		}
   1181 	} else if (ISSB2CLASS(sc)) {
   1182 		if (cc != sc->sc_last_hs_size) {
   1183 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1184 			    sbdsp_wdsp(sc, cc) < 0 ||
   1185 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1186 				DPRINTF(("sbdsp_dma_input: SB2 DMA start failed\n"));
   1187 				goto giveup;
   1188 			}
   1189 			sc->sc_last_hs_size = cc;
   1190 		}
   1191 		if (sbdsp_wdsp(sc,
   1192 		    sc->sc_imode == SB_ADAC_LS ? SB_DSP_RDMA_LOOP :
   1193 						 SB_DSP_HS_INPUT) < 0) {
   1194 			DPRINTF(("sbdsp_dma_input: SB2 DMA restart failed\n"));
   1195 			goto giveup;
   1196 		}
   1197 	} else {
   1198 		if (sbdsp_wdsp(sc, SB_DSP_RDMA) < 0 ||
   1199 		    sbdsp_wdsp(sc, cc) < 0 ||
   1200 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1201 		        DPRINTF(("sbdsp_dma_input: SB1 DMA start failed\n"));
   1202 			goto giveup;
   1203 		}
   1204 	}
   1205 	return 0;
   1206 
   1207 giveup:
   1208 	sbdsp_reset(sc);
   1209 	return EIO;
   1210 
   1211 badmode:
   1212 	DPRINTF(("sbdsp_dma_input: can't set %s mode\n",
   1213 		 sc->sc_channels == 2 ? "stereo" : "mono"));
   1214 	return EIO;
   1215 }
   1216 
   1217 int
   1218 sbdsp_dma_output(addr, p, cc, intr, arg)
   1219 	void *addr;
   1220 	void *p;
   1221 	int cc;
   1222 	void (*intr) __P((void *));
   1223 	void *arg;
   1224 {
   1225 	register struct sbdsp_softc *sc = addr;
   1226 
   1227 #ifdef AUDIO_DEBUG
   1228 	if (sbdspdebug > 1)
   1229 		Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1230 #endif
   1231 	if (sc->sc_channels == 2 && (cc & 1)) {
   1232 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
   1233 		return EIO;
   1234 	}
   1235 
   1236 	if (sc->sc_dmadir != SB_DMA_OUT) {
   1237 		if (ISSBPRO(sc)) {
   1238 			/* make sure we re-set stereo mixer bit when we start
   1239 			   output. */
   1240 			sbdsp_mix_write(sc, SBP_STEREO,
   1241 			    (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
   1242 			    (sc->sc_channels == 2 ?  SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
   1243 			if (ISJAZZ16(sc)) {
   1244 				/* Yes, we write the record mode to set
   1245 				   16-bit playback mode. weird, huh? */
   1246 				if (sc->sc_precision == 16) {
   1247 					sbdsp_wdsp(sc,
   1248 						   sc->sc_channels == 2 ?
   1249 						   JAZZ16_RECORD_STEREO :
   1250 						   JAZZ16_RECORD_MONO);
   1251 				} else {
   1252 					sbdsp_wdsp(sc,
   1253 						   sc->sc_channels == 2 ?
   1254 						   SB_DSP_RECORD_STEREO :
   1255 						   SB_DSP_RECORD_MONO);
   1256 				}
   1257 			}
   1258 		}
   1259 
   1260 		if (ISSB16CLASS(sc)) {
   1261 			if (sbdsp_wdsp(sc, SB_DSP16_OUTPUTRATE) < 0 ||
   1262 			    sbdsp_wdsp(sc, sc->sc_orate >> 8) < 0 ||
   1263 			    sbdsp_wdsp(sc, sc->sc_orate) < 0)
   1264 				goto giveup;
   1265 		} else
   1266 			sbdsp_set_timeconst(sc, sc->sc_otc);
   1267 
   1268 		sc->sc_dmadir = SB_DMA_OUT;
   1269 		sc->dmaflags = DMAMODE_WRITE;
   1270 		if (ISSB2CLASS(sc))
   1271 			sc->dmaflags |= DMAMODE_LOOP;
   1272 	} else {
   1273 		/* Already started; just return. */
   1274 		if (ISSB2CLASS(sc))
   1275 			return 0;
   1276 	}
   1277 
   1278 	sc->dmaaddr = p;
   1279 	sc->dmacnt = ISSB2CLASS(sc) ? (NBPG/cc)*cc : cc;
   1280 	sc->dmachan = sc->sc_precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1281 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1282 	    sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1283 	sc->sc_intr = intr;
   1284 	sc->sc_arg = arg;
   1285 
   1286 	if (sc->sc_precision == 16)
   1287 		cc >>= 1;
   1288 	--cc;
   1289 	if (ISSB16CLASS(sc)) {
   1290 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_WDMA_16 :
   1291 							    SB_DSP16_WDMA_8) < 0 ||
   1292 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1293 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1294 		    sbdsp16_wait(sc) ||
   1295 		    sbdsp_wdsp(sc, cc) < 0 ||
   1296 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1297 			DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
   1298 			goto giveup;
   1299 		}
   1300 	} else if (ISSB2CLASS(sc)) {
   1301 		if (cc != sc->sc_last_hs_size) {
   1302 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1303 			    sbdsp_wdsp(sc, cc) < 0 ||
   1304 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1305 				DPRINTF(("sbdsp_dma_output: SB2 DMA start failed\n"));
   1306 				goto giveup;
   1307 			}
   1308 			sc->sc_last_hs_size = cc;
   1309 		}
   1310 		if (sbdsp_wdsp(sc,
   1311 		    sc->sc_omode == SB_ADAC_LS ? SB_DSP_WDMA_LOOP :
   1312 						 SB_DSP_HS_OUTPUT) < 0) {
   1313 			DPRINTF(("sbdsp_dma_output: SB2 DMA restart failed\n"));
   1314 			goto giveup;
   1315 		}
   1316 	} else {
   1317 		if (sbdsp_wdsp(sc, SB_DSP_WDMA) < 0 ||
   1318 		    sbdsp_wdsp(sc, cc) < 0 ||
   1319 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1320 		        DPRINTF(("sbdsp_dma_output: SB1 DMA start failed\n"));
   1321 			goto giveup;
   1322 		}
   1323 	}
   1324 	return 0;
   1325 
   1326 giveup:
   1327 	sbdsp_reset(sc);
   1328 	return EIO;
   1329 }
   1330 
   1331 /*
   1332  * Only the DSP unit on the sound blaster generates interrupts.
   1333  * There are three cases of interrupt: reception of a midi byte
   1334  * (when mode is enabled), completion of dma transmission, or
   1335  * completion of a dma reception.  The three modes are mutually
   1336  * exclusive so we know a priori which event has occurred.
   1337  */
   1338 int
   1339 sbdsp_intr(arg)
   1340 	void *arg;
   1341 {
   1342 	register struct sbdsp_softc *sc = arg;
   1343 	u_char x;
   1344 
   1345 #ifdef AUDIO_DEBUG
   1346 	if (sbdspdebug > 1)
   1347 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
   1348 #endif
   1349 	if (ISSB16CLASS(sc)) {
   1350 		x = sbdsp_mix_read(sc, SBP_IRQ_STATUS);
   1351 		if ((x & 3) == 0)
   1352 			return 0;
   1353 	} else {
   1354 		if (isa_dmafinished(sc->sc_isa, sc->dmachan) == 0)
   1355 			return 0;
   1356 	}
   1357 	sc->sc_interrupts++;
   1358 	delay(10);
   1359 #if 0
   1360 	if (sc->sc_mintr != 0) {
   1361 		x = sbdsp_rdsp(sc);
   1362 		(*sc->sc_mintr)(sc->sc_arg, x);
   1363 	} else
   1364 #endif
   1365 	if (sc->sc_intr != 0) {
   1366 		/* clear interrupt */
   1367 		bus_space_read_1(sc->sc_iot, sc->sc_ioh,
   1368 		    sc->sc_precision == 16 ? SBP_DSP_IRQACK16 :
   1369 					     SBP_DSP_IRQACK8);
   1370 		if (!ISSB2CLASS(sc))
   1371 			isa_dmadone(sc->sc_isa, sc->dmachan);
   1372 		(*sc->sc_intr)(sc->sc_arg);
   1373 	} else {
   1374 		return 0;
   1375 	}
   1376 	return 1;
   1377 }
   1378 
   1379 #if 0
   1380 /*
   1381  * Enter midi uart mode and arrange for read interrupts
   1382  * to vector to `intr'.  This puts the card in a mode
   1383  * which allows only midi I/O; the card must be reset
   1384  * to leave this mode.  Unfortunately, the card does not
   1385  * use transmit interrupts, so bytes must be output
   1386  * using polling.  To keep the polling overhead to a
   1387  * minimum, output should be driven off a timer.
   1388  * This is a little tricky since only 320us separate
   1389  * consecutive midi bytes.
   1390  */
   1391 void
   1392 sbdsp_set_midi_mode(sc, intr, arg)
   1393 	struct sbdsp_softc *sc;
   1394 	void (*intr)();
   1395 	void *arg;
   1396 {
   1397 
   1398 	sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
   1399 	sc->sc_mintr = intr;
   1400 	sc->sc_intr = 0;
   1401 	sc->sc_arg = arg;
   1402 }
   1403 
   1404 /*
   1405  * Write a byte to the midi port, when in midi uart mode.
   1406  */
   1407 void
   1408 sbdsp_midi_output(sc, v)
   1409 	struct sbdsp_softc *sc;
   1410 	int v;
   1411 {
   1412 
   1413 	if (sbdsp_wdsp(sc, v) < 0)
   1414 		++sberr.wmidi;
   1415 }
   1416 #endif
   1417 
   1418 int
   1419 sbdsp_setfd(addr, flag)
   1420 	void *addr;
   1421 	int flag;
   1422 {
   1423 	/* Can't do full-duplex */
   1424 	return(ENOTTY);
   1425 }
   1426 
   1427 int
   1428 sbdsp_mixer_set_port(addr, cp)
   1429 	void *addr;
   1430 	mixer_ctrl_t *cp;
   1431 {
   1432 	register struct sbdsp_softc *sc = addr;
   1433 	int src, gain;
   1434 
   1435 	DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
   1436 	    cp->un.value.num_channels));
   1437 
   1438 	if (!ISSBPROCLASS(sc))
   1439 		return EINVAL;
   1440 
   1441 	/*
   1442 	 * Everything is a value except for SBPro BASS/TREBLE and
   1443 	 * RECORD_SOURCE
   1444 	 */
   1445 	switch (cp->dev) {
   1446 	case SB_SPEAKER:
   1447 		cp->dev = SB_MASTER_VOL;
   1448 	case SB_MIC_PORT:
   1449 	case SB_LINE_IN_PORT:
   1450 	case SB_DAC_PORT:
   1451 	case SB_FM_PORT:
   1452 	case SB_CD_PORT:
   1453 	case SB_MASTER_VOL:
   1454 		if (cp->type != AUDIO_MIXER_VALUE)
   1455 			return EINVAL;
   1456 
   1457 		/*
   1458 		 * All the mixer ports are stereo except for the microphone.
   1459 		 * If we get a single-channel gain value passed in, then we
   1460 		 * duplicate it to both left and right channels.
   1461 		 */
   1462 
   1463 		switch (cp->dev) {
   1464 		case SB_MIC_PORT:
   1465 			if (cp->un.value.num_channels != 1)
   1466 				return EINVAL;
   1467 
   1468 			/* handle funny microphone gain */
   1469 			gain = SBP_AGAIN_TO_MICGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1470 			break;
   1471 		case SB_LINE_IN_PORT:
   1472 		case SB_DAC_PORT:
   1473 		case SB_FM_PORT:
   1474 		case SB_CD_PORT:
   1475 		case SB_MASTER_VOL:
   1476 			switch (cp->un.value.num_channels) {
   1477 			case 1:
   1478 				gain = sbdsp_mono_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]));
   1479 				break;
   1480 			case 2:
   1481 				gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]),
   1482 							SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]));
   1483 				break;
   1484 			default:
   1485 				return EINVAL;
   1486 			}
   1487 			break;
   1488 		default:
   1489 			return EINVAL;
   1490 		}
   1491 
   1492 		switch (cp->dev) {
   1493 		case SB_MIC_PORT:
   1494 			src = SBP_MIC_VOL;
   1495 			break;
   1496 		case SB_MASTER_VOL:
   1497 			src = SBP_MASTER_VOL;
   1498 			break;
   1499 		case SB_LINE_IN_PORT:
   1500 			src = SBP_LINE_VOL;
   1501 			break;
   1502 		case SB_DAC_PORT:
   1503 			src = SBP_DAC_VOL;
   1504 			break;
   1505 		case SB_FM_PORT:
   1506 			src = SBP_FM_VOL;
   1507 			break;
   1508 		case SB_CD_PORT:
   1509 			src = SBP_CD_VOL;
   1510 			break;
   1511 		default:
   1512 			return EINVAL;
   1513 		}
   1514 
   1515 		sbdsp_mix_write(sc, src, gain);
   1516 		sc->gain[cp->dev] = gain;
   1517 		break;
   1518 
   1519 	case SB_TREBLE:
   1520 	case SB_BASS:
   1521 	case SB_RECORD_SOURCE:
   1522 		if (cp->type != AUDIO_MIXER_ENUM)
   1523 			return EINVAL;
   1524 
   1525 		switch (cp->dev) {
   1526 		case SB_TREBLE:
   1527 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_TREBLE_EQ : 0);
   1528 		case SB_BASS:
   1529 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_BASS_EQ : 0);
   1530 		case SB_RECORD_SOURCE:
   1531 			return sbdsp_set_in_port(addr, cp->un.ord);
   1532 		}
   1533 
   1534 		break;
   1535 
   1536 	default:
   1537 		return EINVAL;
   1538 	}
   1539 
   1540 	return (0);
   1541 }
   1542 
   1543 int
   1544 sbdsp_mixer_get_port(addr, cp)
   1545 	void *addr;
   1546 	mixer_ctrl_t *cp;
   1547 {
   1548 	register struct sbdsp_softc *sc = addr;
   1549 	int gain;
   1550 
   1551 	DPRINTF(("sbdsp_mixer_get_port: port=%d\n", cp->dev));
   1552 
   1553 	if (!ISSBPROCLASS(sc))
   1554 		return EINVAL;
   1555 
   1556 	switch (cp->dev) {
   1557 	case SB_SPEAKER:
   1558 		cp->dev = SB_MASTER_VOL;
   1559 	case SB_MIC_PORT:
   1560 	case SB_LINE_IN_PORT:
   1561 	case SB_DAC_PORT:
   1562 	case SB_FM_PORT:
   1563 	case SB_CD_PORT:
   1564 	case SB_MASTER_VOL:
   1565 		gain = sc->gain[cp->dev];
   1566 
   1567 		switch (cp->dev) {
   1568 		case SB_MIC_PORT:
   1569 			if (cp->un.value.num_channels != 1)
   1570 				return EINVAL;
   1571 
   1572 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_MICGAIN_TO_AGAIN(gain);
   1573 			break;
   1574 		case SB_LINE_IN_PORT:
   1575 		case SB_DAC_PORT:
   1576 		case SB_FM_PORT:
   1577 		case SB_CD_PORT:
   1578 		case SB_MASTER_VOL:
   1579 			switch (cp->un.value.num_channels) {
   1580 			case 1:
   1581 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_SBGAIN_TO_AGAIN(gain);
   1582 				break;
   1583 			case 2:
   1584 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = SBP_LEFTGAIN(gain);
   1585 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = SBP_RIGHTGAIN(gain);
   1586 				break;
   1587 			default:
   1588 				return EINVAL;
   1589 			}
   1590 			break;
   1591 		}
   1592 
   1593 		break;
   1594 
   1595 	case SB_TREBLE:
   1596 	case SB_BASS:
   1597 	case SB_RECORD_SOURCE:
   1598 		switch (cp->dev) {
   1599 		case SB_TREBLE:
   1600 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_TREBLE_EQ;
   1601 			return 0;
   1602 		case SB_BASS:
   1603 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_BASS_EQ;
   1604 			return 0;
   1605 		case SB_RECORD_SOURCE:
   1606 			cp->un.ord = sbdsp_get_in_port(addr);
   1607 			return 0;
   1608 		}
   1609 
   1610 		break;
   1611 
   1612 	default:
   1613 		return EINVAL;
   1614 	}
   1615 
   1616 	return (0);
   1617 }
   1618 
   1619 int
   1620 sbdsp_mixer_query_devinfo(addr, dip)
   1621 	void *addr;
   1622 	register mixer_devinfo_t *dip;
   1623 {
   1624 	register struct sbdsp_softc *sc = addr;
   1625 
   1626 	DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
   1627 
   1628 	switch (dip->index) {
   1629 	case SB_MIC_PORT:
   1630 		dip->type = AUDIO_MIXER_VALUE;
   1631 		dip->mixer_class = SB_INPUT_CLASS;
   1632 		dip->prev = AUDIO_MIXER_LAST;
   1633 		dip->next = AUDIO_MIXER_LAST;
   1634 		strcpy(dip->label.name, AudioNmicrophone);
   1635 		dip->un.v.num_channels = 1;
   1636 		strcpy(dip->un.v.units.name, AudioNvolume);
   1637 		return 0;
   1638 
   1639 	case SB_SPEAKER:
   1640 		dip->type = AUDIO_MIXER_VALUE;
   1641 		dip->mixer_class = SB_OUTPUT_CLASS;
   1642 		dip->prev = AUDIO_MIXER_LAST;
   1643 		dip->next = AUDIO_MIXER_LAST;
   1644 		strcpy(dip->label.name, AudioNspeaker);
   1645 		dip->un.v.num_channels = 1;
   1646 		strcpy(dip->un.v.units.name, AudioNvolume);
   1647 		return 0;
   1648 
   1649 	case SB_INPUT_CLASS:
   1650 		dip->type = AUDIO_MIXER_CLASS;
   1651 		dip->mixer_class = SB_INPUT_CLASS;
   1652 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1653 		strcpy(dip->label.name, AudioCInputs);
   1654 		return 0;
   1655 
   1656 	case SB_OUTPUT_CLASS:
   1657 		dip->type = AUDIO_MIXER_CLASS;
   1658 		dip->mixer_class = SB_OUTPUT_CLASS;
   1659 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1660 		strcpy(dip->label.name, AudioCOutputs);
   1661 		return 0;
   1662 	}
   1663 
   1664 	if (ISSBPROCLASS(sc)) {
   1665 		switch (dip->index) {
   1666 		case SB_LINE_IN_PORT:
   1667 			dip->type = AUDIO_MIXER_VALUE;
   1668 			dip->mixer_class = SB_INPUT_CLASS;
   1669 			dip->prev = AUDIO_MIXER_LAST;
   1670 			dip->next = AUDIO_MIXER_LAST;
   1671 			strcpy(dip->label.name, AudioNline);
   1672 			dip->un.v.num_channels = 2;
   1673 			strcpy(dip->un.v.units.name, AudioNvolume);
   1674 			return 0;
   1675 
   1676 		case SB_DAC_PORT:
   1677 			dip->type = AUDIO_MIXER_VALUE;
   1678 			dip->mixer_class = SB_INPUT_CLASS;
   1679 			dip->prev = AUDIO_MIXER_LAST;
   1680 			dip->next = AUDIO_MIXER_LAST;
   1681 			strcpy(dip->label.name, AudioNdac);
   1682 			dip->un.v.num_channels = 2;
   1683 			strcpy(dip->un.v.units.name, AudioNvolume);
   1684 			return 0;
   1685 
   1686 		case SB_CD_PORT:
   1687 			dip->type = AUDIO_MIXER_VALUE;
   1688 			dip->mixer_class = SB_INPUT_CLASS;
   1689 			dip->prev = AUDIO_MIXER_LAST;
   1690 			dip->next = AUDIO_MIXER_LAST;
   1691 			strcpy(dip->label.name, AudioNcd);
   1692 			dip->un.v.num_channels = 2;
   1693 			strcpy(dip->un.v.units.name, AudioNvolume);
   1694 			return 0;
   1695 
   1696 		case SB_FM_PORT:
   1697 			dip->type = AUDIO_MIXER_VALUE;
   1698 			dip->mixer_class = SB_INPUT_CLASS;
   1699 			dip->prev = AUDIO_MIXER_LAST;
   1700 			dip->next = AUDIO_MIXER_LAST;
   1701 			strcpy(dip->label.name, AudioNfmsynth);
   1702 			dip->un.v.num_channels = 2;
   1703 			strcpy(dip->un.v.units.name, AudioNvolume);
   1704 			return 0;
   1705 
   1706 		case SB_MASTER_VOL:
   1707 			dip->type = AUDIO_MIXER_VALUE;
   1708 			dip->mixer_class = SB_OUTPUT_CLASS;
   1709 			dip->prev = AUDIO_MIXER_LAST;
   1710 			dip->next = AUDIO_MIXER_LAST;
   1711 			strcpy(dip->label.name, AudioNvolume);
   1712 			dip->un.v.num_channels = 2;
   1713 			strcpy(dip->un.v.units.name, AudioNvolume);
   1714 			return 0;
   1715 
   1716 		case SB_RECORD_SOURCE:
   1717 			dip->mixer_class = SB_RECORD_CLASS;
   1718 			dip->type = AUDIO_MIXER_ENUM;
   1719 			dip->prev = AUDIO_MIXER_LAST;
   1720 			dip->next = AUDIO_MIXER_LAST;
   1721 			strcpy(dip->label.name, AudioNsource);
   1722 			dip->un.e.num_mem = 3;
   1723 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1724 			dip->un.e.member[0].ord = SB_MIC_PORT;
   1725 			strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1726 			dip->un.e.member[1].ord = SB_CD_PORT;
   1727 			strcpy(dip->un.e.member[2].label.name, AudioNline);
   1728 			dip->un.e.member[2].ord = SB_LINE_IN_PORT;
   1729 			return 0;
   1730 
   1731 		case SB_BASS:
   1732 			dip->type = AUDIO_MIXER_ENUM;
   1733 			dip->mixer_class = SB_INPUT_CLASS;
   1734 			dip->prev = AUDIO_MIXER_LAST;
   1735 			dip->next = AUDIO_MIXER_LAST;
   1736 			strcpy(dip->label.name, AudioNbass);
   1737 			dip->un.e.num_mem = 2;
   1738 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1739 			dip->un.e.member[0].ord = 0;
   1740 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1741 			dip->un.e.member[1].ord = 1;
   1742 			return 0;
   1743 
   1744 		case SB_TREBLE:
   1745 			dip->type = AUDIO_MIXER_ENUM;
   1746 			dip->mixer_class = SB_INPUT_CLASS;
   1747 			dip->prev = AUDIO_MIXER_LAST;
   1748 			dip->next = AUDIO_MIXER_LAST;
   1749 			strcpy(dip->label.name, AudioNtreble);
   1750 			dip->un.e.num_mem = 2;
   1751 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1752 			dip->un.e.member[0].ord = 0;
   1753 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1754 			dip->un.e.member[1].ord = 1;
   1755 			return 0;
   1756 
   1757 		case SB_RECORD_CLASS:			/* record source class */
   1758 			dip->type = AUDIO_MIXER_CLASS;
   1759 			dip->mixer_class = SB_RECORD_CLASS;
   1760 			dip->next = dip->prev = AUDIO_MIXER_LAST;
   1761 			strcpy(dip->label.name, AudioCRecord);
   1762 			return 0;
   1763 		}
   1764 	}
   1765 
   1766 	return ENXIO;
   1767 }
   1768