Home | History | Annotate | Line # | Download | only in ic
interwave.c revision 1.20
      1 /*	$NetBSD: interwave.c,v 1.20 2004/04/22 00:17:11 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Kari Mettinen
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: interwave.c,v 1.20 2004/04/22 00:17:11 itojun Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/errno.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/syslog.h>
     46 #include <sys/device.h>
     47 #include <sys/proc.h>
     48 #include <sys/buf.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <machine/cpu.h>
     54 #include <machine/intr.h>
     55 #include <machine/pio.h>
     56 #include <sys/audioio.h>
     57 #include <dev/audio_if.h>
     58 #include <dev/mulaw.h>
     59 
     60 #include <dev/isa/isavar.h>
     61 #include <dev/isa/isadmavar.h>
     62 
     63 #include <dev/ic/interwavereg.h>
     64 #include <dev/ic/interwavevar.h>
     65 
     66 
     67 static void iwreset __P((struct iw_softc *, int));
     68 
     69 static int iw_set_speed __P((struct iw_softc *, u_long, char));
     70 static u_long iw_set_format __P((struct iw_softc *, u_long, int));
     71 static void iw_mixer_line_level __P((struct iw_softc *, int, int, int));
     72 static void iw_trigger_dma __P((struct iw_softc *, u_char));
     73 static void iw_stop_dma __P((struct iw_softc *, u_char, u_char));
     74 static void iw_dma_count __P((struct iw_softc *, u_short, int));
     75 static int iwintr __P((void *));
     76 static void iw_meminit __P((struct iw_softc *));
     77 static void iw_mempoke __P((struct iw_softc *, u_long, u_char));
     78 static u_char iw_mempeek __P((struct iw_softc *, u_long));
     79 
     80 #ifdef USE_WAVETABLE
     81 static void iw_set_voice_place __P((struct iw_softc *, u_char, u_long));
     82 static void iw_voice_pan __P((struct iw_softc *, u_char, u_short, u_short));
     83 static void iw_voice_freq __P((struct iw_softc *, u_char, u_long));
     84 static void iw_set_loopmode __P((struct iw_softc *, u_char, u_char, u_char));
     85 static void iw_set_voice_pos __P((struct iw_softc *, u_short, u_long, u_long));
     86 static void iw_start_voice __P((struct iw_softc *, u_char));
     87 static void iw_play_voice __P((struct iw_softc *, u_long, u_long, u_short));
     88 static void iw_stop_voice __P((struct iw_softc *, u_char));
     89 static void iw_move_voice_end __P((struct iw_softc *, u_short, u_long));
     90 static void iw_initvoices __P((struct iw_softc *));
     91 #endif
     92 
     93 struct audio_device iw_device = {
     94 	"Am78C201",
     95 	"0.1",
     96 	"guspnp"
     97 };
     98 
     99 #ifdef AUDIO_DEBUG
    100 int iw_debug;
    101 #define DPRINTF(p)       if (iw_debug) printf p
    102 #else
    103 #define DPRINTF(p)
    104 #endif
    105 
    106 static int      iw_cc = 1;
    107 #ifdef DIAGNOSTIC
    108 static int      outputs = 0;
    109 static int      iw_ints = 0;
    110 static int      inputs = 0;
    111 static int      iw_inints = 0;
    112 #endif
    113 
    114 int
    115 iwintr(arg)
    116 	void	*arg;
    117 {
    118 	struct	iw_softc *sc = arg;
    119 	int	val = 0;
    120 	u_char	intrs = 0;
    121 
    122 	IW_READ_DIRECT_1(6, sc->p2xr_h, intrs);	/* UISR */
    123 
    124 	/* codec ints */
    125 
    126 	/*
    127 	 * The proper order to do this seems to be to read CSR3 to get the
    128 	 * int cause and fifo over underrrun status, then deal with the ints
    129 	 * (new DMA set up), and to clear ints by writing the respective bit
    130 	 * to 0.
    131 	 */
    132 
    133 	/* read what ints happened */
    134 
    135 	IW_READ_CODEC_1(CSR3I, intrs);
    136 
    137 	/* clear them */
    138 
    139 	IW_WRITE_DIRECT_1(2, sc->codec_index_h, 0x00);
    140 
    141 	/* and process them */
    142 
    143 	if (intrs & 0x20) {
    144 #ifdef DIAGNOSTIC
    145 		iw_inints++;
    146 #endif
    147 		sc->sc_reclocked = 0;
    148 		if (sc->sc_recintr != 0)
    149 			sc->sc_recintr(sc->sc_recarg);
    150 		val = 1;
    151 	}
    152 	if (intrs & 0x10) {
    153 #ifdef DIAGNOSTIC
    154 		iw_ints++;
    155 #endif
    156 		sc->sc_playlocked = 0;
    157 		if (sc->sc_playintr != 0)
    158 			sc->sc_playintr(sc->sc_playarg);
    159 		val = 1;
    160 	}
    161 	return val;
    162 
    163 }
    164 
    165 void
    166 iwattach(sc)
    167 	struct	iw_softc *sc;
    168 {
    169 	int	got_irq = 0;
    170 
    171 	DPRINTF(("iwattach sc %p\n", sc));
    172 
    173 	sc->cdatap = 1;		/* relative offsets in region */
    174 	sc->csr1r = 2;
    175 	sc->cxdr = 3;		/* CPDR or CRDR */
    176 
    177 	sc->gmxr = 0;		/* sc->p3xr */
    178 	sc->gmxdr = 1;		/* GMTDR or GMRDR */
    179 	sc->svsr = 2;
    180 	sc->igidxr = 3;
    181 	sc->i16dp = 4;
    182 	sc->i8dp = 5;
    183 	sc->lmbdr = 7;
    184 
    185 	sc->rec_precision = sc->play_precision = 8;
    186 	sc->rec_channels = sc->play_channels = 1;
    187 	sc->rec_encoding = sc->play_encoding = AUDIO_ENCODING_ULAW;
    188 	sc->sc_irate = 8000;
    189 	sc->sc_orate = 8000;
    190 
    191 	sc->sc_fullduplex = 1;
    192 
    193 	sc->sc_reclocked = 0;
    194 	sc->sc_playlocked = 0;
    195 
    196 	sc->sc_dma_flags = 0;
    197 
    198 	/*
    199 	 * We can only use a few selected irqs, see if we got one from pnp
    200 	 * code that suits us.
    201 	 */
    202 
    203 	if (sc->sc_irq > 0) {
    204 		sc->sc_ih = isa_intr_establish(sc->sc_p2xr_ic,
    205 					       sc->sc_irq,
    206 					   IST_EDGE, IPL_AUDIO, iwintr, sc);
    207 		got_irq = 1;
    208 	}
    209 	if (!got_irq) {
    210 		printf("\niwattach: couldn't get a suitable irq\n");
    211 		return;
    212 	}
    213 	printf("\n");
    214 	iwreset(sc, 0);
    215 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 0);
    216 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 1);
    217 	printf("%s: interwave version %s\n",
    218 		sc->sc_dev.dv_xname, iw_device.version);
    219 	audio_attach_mi(sc->iw_hw_if, sc, &sc->sc_dev);
    220  }
    221 
    222 int
    223 iwopen(sc, flags)
    224 	struct	iw_softc *sc;
    225 	int	flags;
    226 {
    227 	int	s;
    228 
    229 	s = splaudio();
    230 	if (sc->sc_open) {
    231 		splx(s);
    232 		DPRINTF(("iwopen: open %x sc %p\n", sc->sc_open, sc));
    233 		return EBUSY;
    234 	} else
    235 		sc->sc_open = 1;
    236 	splx(s);
    237 
    238 	DPRINTF(("iwopen: open %x sc %p\n", sc->sc_open, sc));
    239 
    240 #ifdef DIAGNOSTIC
    241 	outputs = 0;
    242 	iw_ints = 0;
    243 	inputs = 0;
    244 	iw_inints = 0;
    245 #endif
    246 
    247 	iwreset(sc, 1);
    248 
    249 	/* READ/WRITE or both */
    250 
    251 	if (flags == FREAD) {
    252 		sc->sc_mode |= IW_READ;
    253 		sc->sc_reclocked = 0;
    254 	}
    255 	if (flags == FWRITE) {
    256 		sc->sc_mode |= IW_WRITE;
    257 		sc->sc_playlocked = 0;
    258 	}
    259 	sc->sc_playdma_cnt = 0;
    260 	sc->sc_recdma_cnt = 0;
    261 	sc->playfirst = 1;
    262 	sc->sc_playintr = 0;
    263 	sc->sc_recintr = 0;
    264 
    265 	return 0;
    266 }
    267 
    268 
    269 
    270 void
    271 iwclose(addr)
    272 	void	*addr;
    273 {
    274 	struct	iw_softc *sc = addr;
    275 
    276 	DPRINTF(("iwclose sc %p\n", sc));
    277 
    278 #ifdef DIAGNOSTIC
    279 	DPRINTF(("iwclose: outputs %d ints %d inputs %d in_ints %d\n",
    280 		outputs, iw_ints, inputs, iw_inints));
    281 #endif
    282 
    283 	/* close hardware */
    284 	sc->sc_open = 0;
    285 	sc->sc_flags = 0;
    286 	sc->sc_mode = 0;
    287 	sc->sc_playlocked = 0;
    288 	sc->sc_reclocked = 0;
    289 
    290 	iw_stop_dma(sc, IW_DMA_PLAYBACK, 1);
    291 	iw_stop_dma(sc, IW_DMA_RECORD, 1);
    292 
    293 	sc->sc_playdma_cnt = 0;
    294 	sc->sc_recdma_cnt = 0;
    295 }
    296 
    297 #define RAM_STEP          64*1024
    298 
    299 static void
    300 iw_mempoke(sc, addy, val)
    301 	struct	iw_softc *sc;
    302 	u_long	addy;
    303 	u_char	val;
    304 {
    305 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
    306 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
    307 
    308 	/* Write byte to LMBDR */
    309 	IW_WRITE_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, val);
    310 }
    311 
    312 static u_char
    313 iw_mempeek(sc, addy)
    314 	struct	iw_softc *sc;
    315 	u_long	addy;
    316 {
    317 	u_char	ret;
    318 
    319 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
    320 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
    321 
    322 	IW_READ_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, ret);
    323 	return ret;		/* return byte from LMBDR */
    324 }
    325 
    326 static void
    327 iw_meminit(sc)
    328 	struct iw_softc *sc;
    329 {
    330 	u_long          bank[4] = {0L, 0L, 0L, 0L};
    331 	u_long          addr = 0L, base = 0L, cnt = 0L;
    332 	u_char          i, ram = 0 /* ,memval=0 */ ;
    333 	u_short         lmcfi;
    334 	u_long          temppi;
    335 	u_long         *lpbanks = &temppi;
    336 
    337 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
    338 
    339 	IW_READ_GENERAL_2(LMCFI, lmcfi);	/* 0x52 */
    340 	lmcfi |= 0x0A0C;
    341 	IW_WRITE_GENERAL_2(LMCFI, lmcfi);	/* max addr span */
    342 	IW_WRITE_GENERAL_1(LMCI, 0x00);
    343 
    344 	/* fifo addresses */
    345 
    346 	IW_WRITE_GENERAL_2(LMRFAI, ((4 * 1024 * 1024) >> 8));
    347 	IW_WRITE_GENERAL_2(LMPFAI, ((4 * 1024 * 1024 + 16 * 1024) >> 8));
    348 
    349 	IW_WRITE_GENERAL_2(LMFSI, 0x000);
    350 
    351 	IW_WRITE_GENERAL_2(LDICI, 0x0000);
    352 
    353 	while (addr < (16 * 1024 * 1024)) {
    354 		iw_mempoke(sc, addr, 0x00);
    355 		addr += RAM_STEP;
    356 	}
    357 
    358 	printf("%s:", sc->sc_dev.dv_xname);
    359 
    360 	for (i = 0; i < 4; i++) {
    361 		iw_mempoke(sc, base, 0xAA);	/* mark start of bank */
    362 		iw_mempoke(sc, base + 1L, 0x55);
    363 		if (iw_mempeek(sc, base) == 0xAA  &&
    364 		    iw_mempeek(sc, base + 1L) == 0x55)
    365 			ram = 1;
    366 		if (ram) {
    367 			while (cnt < (4 * 1024 * 1024)) {
    368 				bank[i] += RAM_STEP;
    369 				cnt += RAM_STEP;
    370 				addr = base + cnt;
    371 				if (iw_mempeek(sc, addr) == 0xAA)
    372 					break;
    373 			}
    374 		}
    375 		if (lpbanks != NULL) {
    376 			*lpbanks = bank[i];
    377 			lpbanks++;
    378 		}
    379 		bank[i] = bank[i] >> 10;
    380 		printf("%s bank[%d]: %ldK", i ? "," : "", i, bank[i]);
    381 		base += 4 * 1024 * 1024;
    382 		cnt = 0L;
    383 		ram = 0;
    384 	}
    385 
    386 	printf("\n");
    387 
    388 	/*
    389 	 * this is not really useful since GUS PnP supports memory
    390 	 * configurations that aren't really supported by Interwave...beware
    391 	 * of holes! Also, we don't use the memory for anything in this
    392 	 * version of the driver.
    393 	 *
    394 	 * we've configured for 4M-4M-4M-4M
    395 	 */
    396 }
    397 
    398 
    399 static
    400 void
    401 iwreset(sc, warm)
    402 	struct iw_softc *sc;
    403 	int             warm;
    404 {
    405 	u_char          reg, cmode, val = 0, mixer_image = 0;
    406 
    407 	reg = 0;		/* XXX gcc -Wall */
    408 
    409 	cmode = 0x6c;		/* enhanced codec mode (full duplex) */
    410 
    411 	/* reset */
    412 
    413 	IW_WRITE_GENERAL_1(URSTI, 0x00);
    414 	delay(10);
    415 	IW_WRITE_GENERAL_1(URSTI, 0x07);
    416 	IW_WRITE_GENERAL_1(ICMPTI, 0x1f);	/* disable DSP and uici and
    417 						 * udci writes */
    418 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* enable ints to ISA and
    419 						 * codec access */
    420 	IW_READ_GENERAL_1(IVERI, reg);
    421 	IW_WRITE_GENERAL_1(IVERI, reg | 0x01);	/* hidden reg lock disable */
    422 	IW_WRITE_GENERAL_1(UASBCI, 0x00);
    423 
    424 	/* synth enhanced mode (default), 0 active voices, disable ints */
    425 
    426 	IW_WRITE_GENERAL_1(SGMI_WR, 0x01);	/* enhanced mode, LFOs
    427 						 * disabled */
    428 	for (val = 0; val < 32; val++) {
    429 		/* set each synth sound volume to 0 */
    430 		IW_WRITE_DIRECT_1(sc->p3xr + 2, sc->p3xr_h, val);
    431 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
    432 		IW_WRITE_GENERAL_2(SASLI_WR, 0x0000);
    433 		IW_WRITE_GENERAL_2(SASHI_WR, 0x0000);
    434 		IW_WRITE_GENERAL_2(SAELI_WR, 0x0000);
    435 		IW_WRITE_GENERAL_2(SAEHI_WR, 0x0000);
    436 		IW_WRITE_GENERAL_2(SFCI_WR, 0x0000);
    437 		IW_WRITE_GENERAL_1(SACI_WR, 0x02);
    438 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
    439 		IW_WRITE_GENERAL_1(SVEI_WR, 0x00);
    440 		IW_WRITE_GENERAL_2(SVLI_WR, 0x0000);
    441 		IW_WRITE_GENERAL_1(SVCI_WR, 0x02);
    442 		IW_WRITE_GENERAL_1(SMSI_WR, 0x02);
    443 	}
    444 
    445 	IW_WRITE_GENERAL_1(SAVI_WR, 0x00);
    446 
    447 	/* codec mode/init */
    448 
    449 	/* first change mode to 1 */
    450 
    451 	IW_WRITE_CODEC_1(CMODEI, 0x00);
    452 
    453 	/* and mode 3 */
    454 
    455 	IW_WRITE_CODEC_1(CMODEI, cmode);
    456 
    457 	IW_READ_CODEC_1(CMODEI, reg);
    458 
    459 	DPRINTF(("cmode %x\n", reg));
    460 
    461 	sc->revision = ((reg & 0x80) >> 3) | (reg & 0x0f);
    462 
    463 	IW_WRITE_DIRECT_1(sc->codec_index + 2, sc->p2xr_h, 0x00);
    464 
    465 	IW_WRITE_CODEC_1(CFIG1I | IW_MCE, 0x00);	/* DMA 2 chan access */
    466 	IW_WRITE_CODEC_1(CEXTI, 0x00);	/* disable ints for now */
    467 
    468 
    469 	IW_WRITE_CODEC_1(CLPCTI, 0x00);	/* reset playback sample counters */
    470 	IW_WRITE_CODEC_1(CUPCTI, 0x00);	/* always upper byte last */
    471 	IW_WRITE_CODEC_1(CFIG2I, 0x80);	/* full voltage range, enable record
    472 					 * and playback sample counters, and
    473 					 * don't center output in case or
    474 					 * FIFO underrun */
    475 	IW_WRITE_CODEC_1(CFIG3I, 0xc0);	/* enable record/playback irq (still
    476 					 * turned off from CEXTI), max DMA
    477 					 * rate */
    478 	IW_WRITE_CODEC_1(CSR3I, 0x00);	/* clear status 3 reg */
    479 
    480 
    481 	IW_WRITE_CODEC_1(CLRCTI, 0x00);	/* reset record sample counters */
    482 	IW_WRITE_CODEC_1(CURCTI, 0x00);	/* always upper byte last */
    483 
    484 
    485 	IW_READ_GENERAL_1(IVERI, reg);
    486 
    487 	sc->vers = reg >> 4;
    488 	if (!warm)
    489 		snprintf(iw_device.version, sizeof(iw_device.version), "%d.%d",
    490 		    sc->vers, sc->revision);
    491 
    492 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* irqs and codec decode
    493 						 * enable */
    494 
    495 
    496 	/* ports */
    497 
    498 	if (!warm) {
    499 		iw_mixer_line_level(sc, IW_LINE_OUT, 255, 255);
    500 		iw_mixer_line_level(sc, IW_LINE_IN, 0, 0);
    501 		iw_mixer_line_level(sc, IW_AUX1, 0, 0);
    502 		iw_mixer_line_level(sc, IW_AUX2, 200, 200); /* CD */
    503 		sc->sc_dac.off = 0;
    504 		iw_mixer_line_level(sc, IW_DAC, 200, 200);
    505 
    506 		iw_mixer_line_level(sc, IW_MIC_IN, 0, 0);
    507 		iw_mixer_line_level(sc, IW_REC, 0, 0);
    508 		iw_mixer_line_level(sc, IW_LOOPBACK, 0, 0);
    509 		iw_mixer_line_level(sc, IW_MONO_IN, 0, 0);
    510 
    511 		/* mem stuff */
    512 		iw_meminit(sc);
    513 
    514 	}
    515 	IW_WRITE_CODEC_1(CEXTI, 0x02);	/* codec int enable */
    516 
    517 	/* clear _LDMACI */
    518 
    519 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
    520 
    521 	/* enable mixer paths */
    522 	mixer_image = 0x0c;
    523 	IW_WRITE_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
    524 	/*
    525 	 * enable output, line in. disable mic in bit 0 = 0 -> line in on
    526 	 * (from codec?) bit 1 = 0 -> output on bit 2 = 1 -> mic in on bit 3
    527 	 * = 1 -> irq&drq pin enable bit 4 = 1 -> channel interrupts to chan
    528 	 * 1 bit 5 = 1 -> enable midi loop back bit 6 = 0 -> irq latches
    529 	 * URCR[2:0] bit 6 = 1 -> DMA latches URCR[2:0]
    530 	 */
    531 
    532 
    533 	IW_READ_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
    534 #ifdef AUDIO_DEBUG
    535 	if (!warm)
    536 		DPRINTF(("mix image %x \n", mixer_image));
    537 #endif
    538 }
    539 
    540 struct iw_codec_freq {
    541 	u_long	freq;
    542 	u_char	bits;
    543 };
    544 
    545 int
    546 iw_set_speed(sc, freq, in)
    547 	struct	iw_softc *sc;
    548 	u_long	freq;
    549 	char	in;
    550 {
    551 	u_char	var, cfig3, reg;
    552 
    553 	static struct iw_codec_freq iw_cf[17] = {
    554 #define FREQ_1 24576000
    555 #define FREQ_2 16934400
    556 #define XTAL1 0
    557 #define XTAL2 1
    558 		{5510, 0x00 | XTAL2}, {6620, 0x0E | XTAL2},
    559 		{8000, 0x00 | XTAL1}, {9600, 0x0E | XTAL1},
    560 		{11025, 0x02 | XTAL2}, {16000, 0x02 | XTAL1},
    561 		{18900, 0x04 | XTAL2}, {22050, 0x06 | XTAL2},
    562 		{27420, 0x04 | XTAL1}, {32000, 0x06 | XTAL1},
    563 		{33075, 0x0C | XTAL2}, {37800, 0x08 | XTAL2},
    564 		{38400, 0x0A | XTAL1}, {44100, 0x0A | XTAL2},
    565 		{44800, 0x08 | XTAL1}, {48000, 0x0C | XTAL1},
    566 		{48000, 0x0C | XTAL1}	/* really a dummy for indexing later */
    567 #undef XTAL1
    568 #undef XTAL2
    569 	};
    570 
    571 	cfig3 = 0;		/* XXX gcc -Wall */
    572 
    573 	/*
    574 	 * if the frequency is between 3493Hz and 32KHz we can use a more
    575 	 * accurate frequency than the ones listed above base on the formula
    576 	 * FREQ/((16*(48+x))) where FREQ is either FREQ_1 (24576000Hz) or
    577 	 * FREQ_2 (16934400Hz) and x is the value to be written to either
    578 	 * CPVFI or CRVFI. To enable this option, bit 2 in CFIG3 needs to be
    579 	 * set high
    580 	 *
    581 	 * NOT IMPLEMENTED!
    582 	 *
    583 	 * Note that if you have a 'bad' XTAL_1 (higher than 18.5 MHz), 44.8KHz
    584 	 * and 38.4KHz modes will provide wrong frequencies to output.
    585 	 */
    586 
    587 
    588 	if (freq > 48000)
    589 		freq = 48000;
    590 	if (freq < 5510)
    591 		freq = 5510;
    592 
    593 	/* reset CFIG3[2] */
    594 
    595 	IW_READ_CODEC_1(CFIG3I, cfig3);
    596 
    597 	cfig3 |= 0xc0;		/* not full fifo treshhold */
    598 
    599 	DPRINTF(("cfig3i = %x -> ", cfig3));
    600 
    601 	cfig3 &= ~0x04;
    602 	IW_WRITE_CODEC_1(CFIG3I, cfig3);
    603 	IW_READ_CODEC_1(CFIG3I, cfig3);
    604 
    605 	DPRINTF(("%x\n", cfig3));
    606 
    607 	for (var = 0; var < 16; var++)	/* select closest frequency */
    608 		if (freq <= iw_cf[var].freq)
    609 			break;
    610 	if (var != 16)
    611 		if (abs(freq - iw_cf[var].freq) > abs(iw_cf[var + 1].freq - freq))
    612 			var++;
    613 
    614 	if (in)
    615 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, sc->recfmtbits | iw_cf[var].bits);
    616 	else
    617 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, sc->playfmtbits | iw_cf[var].bits);
    618 	freq = iw_cf[var].freq;
    619 	DPRINTF(("setting %s frequency to %d bits %x \n",
    620 	       in ? "in" : "out", (int) freq, iw_cf[var].bits));
    621 
    622 	IW_READ_CODEC_1(CPDFI, reg);
    623 
    624 	DPRINTF((" CPDFI %x ", reg));
    625 
    626 	IW_READ_CODEC_1(CRDFI, reg);
    627 
    628 	DPRINTF((" CRDFI %x ", reg));
    629 
    630 	return freq;
    631 }
    632 
    633 /* Encoding. */
    634 int
    635 iw_query_encoding(addr, fp)
    636 	void	*addr;
    637 	struct	audio_encoding *fp;
    638 {
    639 	/*
    640 	 * LINEAR, ALAW, ULAW, ADPCM in HW, we'll use linear unsigned
    641 	 * hardware mode for all 8-bit modes due to buggy (?) codec.
    642 	 */
    643 
    644 	/*
    645 	 * except in wavetable synth. there we have only mu-law and 8 and 16
    646 	 * bit linear data
    647 	 */
    648 
    649 	switch (fp->index) {
    650 	case 0:
    651 		strcpy(fp->name, AudioEulinear);
    652 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    653 		fp->precision = 8;
    654 		fp->flags = 0;
    655 		break;
    656 	case 1:
    657 		strcpy(fp->name, AudioEmulaw);
    658 		fp->encoding = AUDIO_ENCODING_ULAW;
    659 		fp->precision = 8;
    660 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    661 		break;
    662 	case 2:
    663 		strcpy(fp->name, AudioEalaw);
    664 		fp->encoding = AUDIO_ENCODING_ALAW;
    665 		fp->precision = 8;
    666 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    667 		break;
    668 	case 3:
    669 		strcpy(fp->name, AudioEadpcm);
    670 		fp->encoding = AUDIO_ENCODING_ADPCM;
    671 		fp->precision = 8;	/* really 4 bit */
    672 		fp->flags = 0;
    673 		break;
    674 	case 4:
    675 		strcpy(fp->name, AudioEslinear_le);
    676 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    677 		fp->precision = 16;
    678 		fp->flags = 0;
    679 		break;
    680 	case 5:
    681 		strcpy(fp->name, AudioEslinear_be);
    682 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    683 		fp->precision = 16;
    684 		fp->flags = 0;
    685 		break;
    686 	default:
    687 		return (EINVAL);
    688 		/* NOTREACHED */
    689 	}
    690 	return (0);
    691 }
    692 
    693 
    694 
    695 u_long
    696 iw_set_format(sc, precision, in)
    697 	struct	iw_softc *sc;
    698 	u_long	precision;
    699 	int	in;
    700 {
    701 	u_char	data;
    702 	int	encoding, channels;
    703 
    704 	encoding = in ? sc->rec_encoding : sc->play_encoding;
    705 	channels = in ? sc->rec_channels : sc->play_channels;
    706 
    707 	DPRINTF(("iw_set_format\n"));
    708 
    709 	switch (encoding) {
    710 	case AUDIO_ENCODING_ULAW:
    711 		data = 0x00;
    712 		break;
    713 
    714 	case AUDIO_ENCODING_ALAW:
    715 		data = 0x00;
    716 		break;
    717 
    718 	case AUDIO_ENCODING_SLINEAR_LE:
    719 		if (precision == 16)
    720 			data = 0x40;	/* little endian. 0xc0 is big endian */
    721 		else
    722 			data = 0x00;
    723 		break;
    724 
    725 	case AUDIO_ENCODING_SLINEAR_BE:
    726 		if (precision == 16)
    727 			data = 0xc0;
    728 		else
    729 			data = 0x00;
    730 		break;
    731 
    732 	case AUDIO_ENCODING_ADPCM:
    733 		data = 0xa0;
    734 		break;
    735 
    736 	default:
    737 		return -1;
    738 	}
    739 
    740 	if (channels == 2)
    741 		data |= 0x10;	/* stereo */
    742 
    743 	if (in) {
    744 		/* in */
    745 		sc->recfmtbits = data;
    746 		/* This will zero the normal codec frequency,
    747 		 * iw_set_speed should always be called afterwards.
    748 		 */
    749 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, data);
    750 	} else {
    751 		/* out */
    752 		sc->playfmtbits = data;
    753 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, data);
    754 	}
    755 
    756 	DPRINTF(("formatbits %s %x", in ? "in" : "out", data));
    757 
    758 	return encoding;
    759 }
    760 
    761 
    762 
    763 int
    764 iw_set_params(addr, setmode, usemode, p, q)
    765 	void	*addr;
    766 	int	setmode;
    767 	int	usemode;
    768 	struct	audio_params *p;
    769 	struct	audio_params *q;
    770 {
    771 	struct	iw_softc *sc = addr;
    772 	void	(*swcode)__P((void *, u_char * buf, int cnt)) = NULL;
    773 	int	factor = 1;
    774 	DPRINTF(("iw_setparams: code %d, prec %d, rate %d, chan %d\n",
    775 		(int) p->encoding, (int) p->precision, (int) p->sample_rate,
    776 		(int) p->channels));
    777 
    778 
    779 	switch (p->encoding) {
    780 	case AUDIO_ENCODING_ULAW:
    781 		if (p->precision != 8)
    782 			return EINVAL;
    783 		swcode = setmode & AUMODE_PLAY ? mulaw_to_ulinear8 : ulinear8_to_mulaw;
    784 		factor = 1;
    785 		break;
    786 	case AUDIO_ENCODING_ALAW:
    787 		if (p->precision != 8)
    788 			return EINVAL;
    789 		swcode = setmode & AUMODE_PLAY ? alaw_to_ulinear8 : ulinear8_to_alaw;
    790 		factor = 1;
    791 		break;
    792 	case AUDIO_ENCODING_ADPCM:
    793 		if (p->precision != 8)
    794 			return EINVAL;
    795 		else
    796 			break;
    797 
    798 	case AUDIO_ENCODING_SLINEAR_LE:
    799 	case AUDIO_ENCODING_SLINEAR_BE:
    800 		if (p->precision != 8 && p->precision != 16)
    801 			return EINVAL;
    802 		else
    803 			break;
    804 
    805 	default:
    806 		return EINVAL;
    807 
    808 	}
    809 
    810 	if (setmode & AUMODE_PLAY) {
    811 		sc->play_channels = p->channels;
    812 	        sc->play_encoding = p->encoding;
    813 		sc->play_precision = p->precision;
    814 		p->factor = factor;
    815 		p->sw_code = swcode;
    816 		iw_set_format(sc, p->precision, 0);
    817 		q->sample_rate = p->sample_rate = sc->sc_orate =
    818 			iw_set_speed(sc, p->sample_rate, 0);
    819 	} else {
    820 #if 0
    821 		q->channels = sc->rec_channels = p->channels;
    822 		q->encoding = sc->rec_encoding = p->encoding;
    823 		q->precision = sc->rec_precision = p->precision;
    824 #endif
    825 		sc->rec_channels = q->channels;
    826 		sc->rec_encoding = q->encoding;
    827 		sc->rec_precision = q->precision;
    828 		q->factor = factor;
    829 		q->sw_code = swcode;
    830 
    831 		iw_set_format(sc, p->precision, 1);
    832 		q->sample_rate = sc->sc_irate =
    833 			iw_set_speed(sc, q->sample_rate, 1);
    834 	}
    835 	return 0;
    836 }
    837 
    838 
    839 int
    840 iw_round_blocksize(addr, blk)
    841 	void	*addr;
    842 	int	blk;
    843 {
    844 	/* Round to a multiple of the biggest sample size. */
    845 	return blk &= -4;
    846 }
    847 
    848 void
    849 iw_mixer_line_level(sc, line, levl, levr)
    850 	struct	iw_softc *sc;
    851 	int	line;
    852 	int	levl, levr;
    853 {
    854 	u_char	gainl, gainr, attenl, attenr;
    855 
    856 	switch (line) {
    857 	case IW_REC:
    858 		gainl = sc->sc_recsrcbits | (levl >> 4);
    859 		gainr = sc->sc_recsrcbits | (levr >> 4);
    860 		DPRINTF(("recording with %x", gainl));
    861 		IW_WRITE_CODEC_1(CLICI, gainl);
    862 		IW_WRITE_CODEC_1(CRICI, gainr);
    863 		sc->sc_rec.voll = levl & 0xf0;
    864 		sc->sc_rec.volr = levr & 0xf0;
    865 		break;
    866 
    867 	case IW_AUX1:
    868 
    869 		gainl = (255 - levl) >> 3;
    870 		gainr = (255 - levr) >> 3;
    871 
    872 		/* mute if 0 level */
    873 		if (levl == 0)
    874 			gainl |= 0x80;
    875 		if (levr == 0)
    876 			gainr |= 0x80;
    877 
    878 		IW_WRITE_CODEC_1(IW_LEFT_AUX1_PORT, gainl);
    879 		IW_WRITE_CODEC_1(IW_RIGHT_AUX1_PORT, gainr);
    880 		sc->sc_aux1.voll = levl & 0xf8;
    881 		sc->sc_aux1.volr = levr & 0xf8;
    882 
    883 		break;
    884 
    885 	case IW_AUX2:
    886 
    887 		gainl = (255 - levl) >> 3;
    888 		gainr = (255 - levr) >> 3;
    889 
    890 		/* mute if 0 level */
    891 		if (levl == 0)
    892 			gainl |= 0x80;
    893 		if (levr == 0)
    894 			gainr |= 0x80;
    895 
    896 		IW_WRITE_CODEC_1(IW_LEFT_AUX2_PORT, gainl);
    897 		IW_WRITE_CODEC_1(IW_RIGHT_AUX2_PORT, gainr);
    898 		sc->sc_aux2.voll = levl & 0xf8;
    899 		sc->sc_aux2.volr = levr & 0xf8;
    900 		break;
    901 	case IW_DAC:
    902 		attenl = ((255 - levl) >> 2) | ((levl && !sc->sc_dac.off) ? 0 : 0x80);
    903 		attenr = ((255 - levr) >> 2) | ((levr && !sc->sc_dac.off) ? 0 : 0x80);
    904 		IW_WRITE_CODEC_1(CLDACI, attenl);
    905 		IW_WRITE_CODEC_1(CRDACI, attenr);
    906 		sc->sc_dac.voll = levl & 0xfc;
    907 		sc->sc_dac.volr = levr & 0xfc;
    908 		break;
    909 	case IW_LOOPBACK:
    910 		attenl = ((255 - levl) & 0xfc) | (levl ? 0x01 : 0);
    911 		IW_WRITE_CODEC_1(CLCI, attenl);
    912 		sc->sc_loopback.voll = levl & 0xfc;
    913 		break;
    914 	case IW_LINE_IN:
    915 		gainl = (levl >> 3) | (levl ? 0 : 0x80);
    916 		gainr = (levr >> 3) | (levr ? 0 : 0x80);
    917 		IW_WRITE_CODEC_1(CLLICI, gainl);
    918 		IW_WRITE_CODEC_1(CRLICI, gainr);
    919 		sc->sc_linein.voll = levl & 0xf8;
    920 		sc->sc_linein.volr = levr & 0xf8;
    921 		break;
    922 	case IW_MIC_IN:
    923 		gainl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
    924 		gainr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
    925 		IW_WRITE_CODEC_1(CLMICI, gainl);
    926 		IW_WRITE_CODEC_1(CRMICI, gainr);
    927 		sc->sc_mic.voll = levl & 0xf8;
    928 		sc->sc_mic.volr = levr & 0xf8;
    929 		break;
    930 	case IW_LINE_OUT:
    931 		attenl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
    932 		attenr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
    933 		IW_WRITE_CODEC_1(CLOAI, attenl);
    934 		IW_WRITE_CODEC_1(CROAI, attenr);
    935 		sc->sc_lineout.voll = levl & 0xf8;
    936 		sc->sc_lineout.volr = levr & 0xf8;
    937 		break;
    938 	case IW_MONO_IN:
    939 		attenl = ((255 - levl) >> 4) | (levl ? 0 : 0xc0);	/* in/out mute */
    940 		IW_WRITE_CODEC_1(CMONOI, attenl);
    941 		sc->sc_monoin.voll = levl & 0xf0;
    942 		break;
    943 	}
    944 }
    945 
    946 int
    947 iw_commit_settings(addr)
    948 	void	*addr;
    949 {
    950 	return 0;
    951 }
    952 
    953 
    954 void
    955 iw_trigger_dma(sc, io)
    956 	struct	iw_softc *sc;
    957 	u_char	io;
    958 {
    959 	u_char	reg;
    960 	int	s;
    961 
    962 	s = splaudio();
    963 
    964 	IW_READ_CODEC_1(CSR3I, reg);
    965 	IW_WRITE_CODEC_1(CSR3I, reg & ~(io == IW_DMA_PLAYBACK ? 0x10 : 0x20));
    966 
    967 	IW_READ_CODEC_1(CFIG1I, reg);
    968 
    969 	IW_WRITE_CODEC_1(CFIG1I, reg | io);
    970 
    971 	/* let the counter run */
    972 	IW_READ_CODEC_1(CFIG2I, reg);
    973 	IW_WRITE_CODEC_1(CFIG2I, reg & ~(io << 4));
    974 
    975 	splx(s);
    976 }
    977 
    978 void
    979 iw_stop_dma(sc, io, hard)
    980 	struct	iw_softc *sc;
    981 	u_char	io, hard;
    982 {
    983 	u_char	reg;
    984 
    985 	/* just stop the counter, no need to flush the fifo */
    986 	IW_READ_CODEC_1(CFIG2I, reg);
    987 	IW_WRITE_CODEC_1(CFIG2I, (reg | (io << 4)));
    988 
    989 	if (hard) {
    990 		/* unless we're closing the device */
    991 		IW_READ_CODEC_1(CFIG1I, reg);
    992 		IW_WRITE_CODEC_1(CFIG1I, reg & ~io);
    993 	}
    994 }
    995 
    996 void
    997 iw_dma_count(sc, count, io)
    998 	struct	iw_softc *sc;
    999 	u_short	count;
   1000 	int	io;
   1001 {
   1002 	if (io == IW_DMA_PLAYBACK) {
   1003 		IW_WRITE_CODEC_1(CLPCTI, (u_char) (count & 0x00ff));
   1004 		IW_WRITE_CODEC_1(CUPCTI, (u_char) ((count >> 8) & 0x00ff));
   1005 	} else {
   1006 		IW_WRITE_CODEC_1(CLRCTI, (u_char) (count & 0x00ff));
   1007 		IW_WRITE_CODEC_1(CURCTI, (u_char) ((count >> 8) & 0x00ff));
   1008 	}
   1009 }
   1010 
   1011 int
   1012 iw_init_output(addr, buf, cc)
   1013 	void	*addr;
   1014 	void	*buf;
   1015 	int	cc;
   1016 {
   1017 	struct iw_softc *sc = (struct iw_softc *) addr;
   1018 
   1019 	DPRINTF(("iw_init_output\n"));
   1020 
   1021 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, buf,
   1022 		     cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1023 	return 0;
   1024 }
   1025 
   1026 int
   1027 iw_init_input(addr, buf, cc)
   1028 	void	*addr;
   1029 	void	*buf;
   1030 	int	cc;
   1031 {
   1032 	struct	iw_softc *sc = (struct iw_softc *) addr;
   1033 
   1034 	DPRINTF(("iw_init_input\n"));
   1035 
   1036 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, buf,
   1037 		     cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1038 	return 0;
   1039 }
   1040 
   1041 
   1042 int
   1043 iw_start_output(addr, p, cc, intr, arg)
   1044 	void	*addr;
   1045 	void	*p;
   1046 	int	cc;
   1047 	void	(*intr)__P((void *));
   1048 	void	*arg;
   1049 {
   1050 	struct	iw_softc *sc = addr;
   1051 
   1052 #ifdef AUDIO_DEBUG
   1053 	if (sc->sc_playlocked) {
   1054 		DPRINTF(("iw_start_output: playback DMA already going on\n"));
   1055 		/* return 0; */
   1056 	}
   1057 #endif
   1058 
   1059 	sc->sc_playlocked = 1;
   1060 #ifdef DIAGNOSTIC
   1061 	if (!intr) {
   1062 		printf("iw_start_output: no callback!\n");
   1063 		return 1;
   1064 	}
   1065 #endif
   1066 
   1067 	sc->sc_playintr = intr;
   1068 	sc->sc_playarg = arg;
   1069 	sc->sc_dma_flags |= DMAMODE_WRITE;
   1070 	sc->sc_playdma_bp = p;
   1071 
   1072 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sc->sc_playdma_bp,
   1073 		     cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);
   1074 
   1075 
   1076 	if (sc->play_encoding == AUDIO_ENCODING_ADPCM)
   1077 		cc >>= 2;
   1078 	if (sc->play_precision == 16)
   1079 		cc >>= 1;
   1080 
   1081 	if (sc->play_channels == 2 && sc->play_encoding != AUDIO_ENCODING_ADPCM)
   1082 		cc >>= 1;
   1083 
   1084 	cc -= iw_cc;
   1085 
   1086 
   1087 	/* iw_dma_access(sc,1); */
   1088 	if (cc != sc->sc_playdma_cnt) {
   1089 		iw_dma_count(sc, (u_short) cc, IW_DMA_PLAYBACK);
   1090 		sc->sc_playdma_cnt = cc;
   1091 
   1092 		iw_trigger_dma(sc, IW_DMA_PLAYBACK);
   1093 	}
   1094 
   1095 #ifdef DIAGNOSTIC
   1096 	if (outputs != iw_ints)
   1097 		printf("iw_start_output: out %d, int %d\n", outputs, iw_ints);
   1098 	outputs++;
   1099 #endif
   1100 	return 0;
   1101 }
   1102 
   1103 
   1104 int
   1105 iw_start_input(addr, p, cc, intr, arg)
   1106 	void	*addr;
   1107 	void	*p;
   1108 	int	cc;
   1109 	void	(*intr)__P((void *));
   1110 	void	*arg;
   1111 {
   1112 	struct	iw_softc *sc = addr;
   1113 
   1114 #if AUDIO_DEBUG
   1115 	if (sc->sc_reclocked) {
   1116 		DPRINTF(("iw_start_input: record DMA already going on\n"));
   1117 		/* return 0; */
   1118 	}
   1119 #endif
   1120 
   1121 	sc->sc_reclocked = 1;
   1122 #ifdef DIAGNOSTIC
   1123 	if (!intr) {
   1124 		printf("iw_start_input: no callback!\n");
   1125 		return 1;
   1126 	}
   1127 #endif
   1128 
   1129 
   1130 	sc->sc_recintr = intr;
   1131 	sc->sc_recarg = arg;
   1132 	sc->sc_dma_flags |= DMAMODE_READ;
   1133 	sc->sc_recdma_bp = p;
   1134 
   1135 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sc->sc_recdma_bp,
   1136 		     cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);
   1137 
   1138 
   1139 	if (sc->rec_encoding == AUDIO_ENCODING_ADPCM)
   1140 		cc >>= 2;
   1141 	if (sc->rec_precision == 16)
   1142 		cc >>= 1;
   1143 
   1144 	if (sc->rec_channels == 2 && sc->rec_encoding != AUDIO_ENCODING_ADPCM)
   1145 		cc >>= 1;
   1146 
   1147 	cc -= iw_cc;
   1148 
   1149 	/* iw_dma_access(sc,0); */
   1150 	if (sc->sc_recdma_cnt != cc) {
   1151 		iw_dma_count(sc, (u_short) cc, IW_DMA_RECORD);
   1152 		sc->sc_recdma_cnt = cc;
   1153 		/* iw_dma_ctrl(sc, IW_DMA_RECORD); */
   1154 		iw_trigger_dma(sc, IW_DMA_RECORD);
   1155 	}
   1156 
   1157 #ifdef DIAGNOSTIC
   1158 	if ((inputs != iw_inints))
   1159 		printf("iw_start_input: in %d, inints %d\n", inputs, iw_inints);
   1160 	inputs++;
   1161 #endif
   1162 
   1163 	return 0;
   1164 }
   1165 
   1166 
   1167 int
   1168 iw_halt_output(addr)
   1169 	void	*addr;
   1170 {
   1171 	struct	iw_softc *sc = addr;
   1172 	iw_stop_dma(sc, IW_DMA_PLAYBACK, 0);
   1173 	/* sc->sc_playlocked = 0; */
   1174 	return 0;
   1175 }
   1176 
   1177 
   1178 int
   1179 iw_halt_input(addr)
   1180 	void	*addr;
   1181 {
   1182 	struct	iw_softc *sc = addr;
   1183 	iw_stop_dma(sc, IW_DMA_RECORD, 0);
   1184 	/* sc->sc_reclocked = 0; */
   1185 	return 0;
   1186 }
   1187 
   1188 
   1189 int
   1190 iw_speaker_ctl(addr, newstate)
   1191 	void	*addr;
   1192 	int	newstate;
   1193 {
   1194 	struct	iw_softc *sc = addr;
   1195 	u_char          reg;
   1196 	if (newstate == SPKR_ON) {
   1197 		sc->sc_dac.off = 0;
   1198 		IW_READ_CODEC_1(CLDACI, reg);
   1199 		IW_WRITE_CODEC_1(CLDACI, reg & 0x7f);
   1200 		IW_READ_CODEC_1(CRDACI, reg);
   1201 		IW_WRITE_CODEC_1(CRDACI, reg & 0x7f);
   1202 	} else {
   1203 		/* SPKR_OFF */
   1204 		sc->sc_dac.off = 1;
   1205 		IW_READ_CODEC_1(CLDACI, reg);
   1206 		IW_WRITE_CODEC_1(CLDACI, reg | 0x80);
   1207 		IW_READ_CODEC_1(CRDACI, reg);
   1208 		IW_WRITE_CODEC_1(CRDACI, reg | 0x80);
   1209 	}
   1210 	return 0;
   1211 }
   1212 
   1213 
   1214 int
   1215 iw_getdev(addr, retp)
   1216 	void	*addr;
   1217 	struct	audio_device *retp;
   1218 {
   1219 	*retp = iw_device;
   1220 	return 0;
   1221 }
   1222 
   1223 
   1224 int
   1225 iw_setfd(addr, flag)
   1226 	void	*addr;
   1227 	int	flag;
   1228 {
   1229 	return 0;
   1230 }
   1231 
   1232 
   1233 /* Mixer (in/out ports) */
   1234 int
   1235 iw_set_port(addr, cp)
   1236 	void	*addr;
   1237 	mixer_ctrl_t *cp;
   1238 {
   1239 	struct	iw_softc *sc = addr;
   1240 	u_char	vall = 0, valr = 0;
   1241 	int	error = EINVAL;
   1242 
   1243 	switch (cp->dev) {
   1244 	case IW_MIC_IN_LVL:
   1245 		if (cp->type == AUDIO_MIXER_VALUE) {
   1246 			error = 0;
   1247 			if (cp->un.value.num_channels == 1) {
   1248 				vall = valr = cp->un.value.level[0];
   1249 			} else {
   1250 				vall = cp->un.value.level[0];
   1251 				valr = cp->un.value.level[1];
   1252 			}
   1253 			sc->sc_mic.voll = vall;
   1254 			sc->sc_mic.volr = valr;
   1255 			iw_mixer_line_level(sc, IW_MIC_IN, vall, valr);
   1256 		}
   1257 		break;
   1258 	case IW_AUX1_LVL:
   1259 		if (cp->type == AUDIO_MIXER_VALUE) {
   1260 			error = 0;
   1261 			if (cp->un.value.num_channels == 1) {
   1262 				vall = valr = cp->un.value.level[0];
   1263 			} else {
   1264 				vall = cp->un.value.level[0];
   1265 				valr = cp->un.value.level[1];
   1266 			}
   1267 			sc->sc_aux1.voll = vall;
   1268 			sc->sc_aux1.volr = valr;
   1269 			iw_mixer_line_level(sc, IW_AUX1, vall, valr);
   1270 		}
   1271 		break;
   1272 	case IW_AUX2_LVL:
   1273 		if (cp->type == AUDIO_MIXER_VALUE) {
   1274 			error = 0;
   1275 			if (cp->un.value.num_channels == 1) {
   1276 				vall = valr = cp->un.value.level[0];
   1277 			} else {
   1278 				vall = cp->un.value.level[0];
   1279 				valr = cp->un.value.level[1];
   1280 			}
   1281 			sc->sc_aux2.voll = vall;
   1282 			sc->sc_aux2.volr = valr;
   1283 			iw_mixer_line_level(sc, IW_AUX2, vall, valr);
   1284 		}
   1285 		break;
   1286 	case IW_LINE_IN_LVL:
   1287 		if (cp->type == AUDIO_MIXER_VALUE) {
   1288 			error = 0;
   1289 			if (cp->un.value.num_channels == 1) {
   1290 				vall = valr = cp->un.value.level[0];
   1291 			} else {
   1292 				vall = cp->un.value.level[0];
   1293 				valr = cp->un.value.level[1];
   1294 			}
   1295 			sc->sc_linein.voll = vall;
   1296 			sc->sc_linein.volr = valr;
   1297 			iw_mixer_line_level(sc, IW_LINE_IN, vall, valr);
   1298 		}
   1299 		break;
   1300 	case IW_LINE_OUT_LVL:
   1301 		if (cp->type == AUDIO_MIXER_VALUE) {
   1302 			error = 0;
   1303 			if (cp->un.value.num_channels == 1) {
   1304 				vall = valr = cp->un.value.level[0];
   1305 			} else {
   1306 				vall = cp->un.value.level[0];
   1307 				valr = cp->un.value.level[1];
   1308 			}
   1309 			sc->sc_lineout.voll = vall;
   1310 			sc->sc_lineout.volr = valr;
   1311 			iw_mixer_line_level(sc, IW_LINE_OUT, vall, valr);
   1312 		}
   1313 		break;
   1314 	case IW_REC_LVL:
   1315 		if (cp->type == AUDIO_MIXER_VALUE) {
   1316 			error = 0;
   1317 			if (cp->un.value.num_channels == 1) {
   1318 				vall = valr = cp->un.value.level[0];
   1319 			} else {
   1320 				vall = cp->un.value.level[0];
   1321 				valr = cp->un.value.level[1];
   1322 			}
   1323 			sc->sc_rec.voll = vall;
   1324 			sc->sc_rec.volr = valr;
   1325 			iw_mixer_line_level(sc, IW_REC, vall, valr);
   1326 		}
   1327 		break;
   1328 
   1329 	case IW_DAC_LVL:
   1330 		if (cp->type == AUDIO_MIXER_VALUE) {
   1331 			error = 0;
   1332 			if (cp->un.value.num_channels == 1) {
   1333 				vall = valr = cp->un.value.level[0];
   1334 			} else {
   1335 				vall = cp->un.value.level[0];
   1336 				valr = cp->un.value.level[1];
   1337 			}
   1338 			sc->sc_dac.voll = vall;
   1339 			sc->sc_dac.volr = valr;
   1340 			iw_mixer_line_level(sc, IW_DAC, vall, valr);
   1341 		}
   1342 		break;
   1343 
   1344 	case IW_LOOPBACK_LVL:
   1345 		if (cp->type == AUDIO_MIXER_VALUE) {
   1346 			error = 0;
   1347 			if (cp->un.value.num_channels != 1) {
   1348 				return EINVAL;
   1349 			} else {
   1350 				valr = vall = cp->un.value.level[0];
   1351 			}
   1352 			sc->sc_loopback.voll = vall;
   1353 			sc->sc_loopback.volr = valr;
   1354 			iw_mixer_line_level(sc, IW_LOOPBACK, vall, valr);
   1355 		}
   1356 		break;
   1357 
   1358 	case IW_MONO_IN_LVL:
   1359 		if (cp->type == AUDIO_MIXER_VALUE) {
   1360 			error = 0;
   1361 			if (cp->un.value.num_channels != 1) {
   1362 				return EINVAL;
   1363 			} else {
   1364 				valr = vall = cp->un.value.level[0];
   1365 			}
   1366 			sc->sc_monoin.voll = vall;
   1367 			sc->sc_monoin.volr = valr;
   1368 			iw_mixer_line_level(sc, IW_MONO_IN, vall, valr);
   1369 		}
   1370 		break;
   1371 	case IW_RECORD_SOURCE:
   1372 		error = 0;
   1373 		sc->sc_recsrcbits = cp->un.ord << 6;
   1374 		DPRINTF(("record source %d bits %x\n", cp->un.ord, sc->sc_recsrcbits));
   1375 		iw_mixer_line_level(sc, IW_REC, sc->sc_rec.voll, sc->sc_rec.volr);
   1376 		break;
   1377 	}
   1378 
   1379 	return error;
   1380 }
   1381 
   1382 
   1383 int
   1384 iw_get_port(addr, cp)
   1385 	void	*addr;
   1386 	mixer_ctrl_t *cp;
   1387 {
   1388 	struct	iw_softc *sc = addr;
   1389 
   1390 	int	error = EINVAL;
   1391 
   1392 	switch (cp->dev) {
   1393 	case IW_MIC_IN_LVL:
   1394 		if (cp->type == AUDIO_MIXER_VALUE) {
   1395 			cp->un.value.num_channels = 2;
   1396 			cp->un.value.level[0] = sc->sc_mic.voll;
   1397 			cp->un.value.level[1] = sc->sc_mic.volr;
   1398 			error = 0;
   1399 		}
   1400 		break;
   1401 	case IW_AUX1_LVL:
   1402 		if (cp->type == AUDIO_MIXER_VALUE) {
   1403 			cp->un.value.num_channels = 2;
   1404 			cp->un.value.level[0] = sc->sc_aux1.voll;
   1405 			cp->un.value.level[1] = sc->sc_aux1.volr;
   1406 			error = 0;
   1407 		}
   1408 		break;
   1409 	case IW_AUX2_LVL:
   1410 		if (cp->type == AUDIO_MIXER_VALUE) {
   1411 			cp->un.value.num_channels = 2;
   1412 			cp->un.value.level[0] = sc->sc_aux2.voll;
   1413 			cp->un.value.level[1] = sc->sc_aux2.volr;
   1414 			error = 0;
   1415 		}
   1416 		break;
   1417 	case IW_LINE_OUT_LVL:
   1418 		if (cp->type == AUDIO_MIXER_VALUE) {
   1419 			cp->un.value.num_channels = 2;
   1420 			cp->un.value.level[0] = sc->sc_lineout.voll;
   1421 			cp->un.value.level[1] = sc->sc_lineout.volr;
   1422 			error = 0;
   1423 		}
   1424 		break;
   1425 	case IW_LINE_IN_LVL:
   1426 		if (cp->type == AUDIO_MIXER_VALUE) {
   1427 			cp->un.value.num_channels = 2;
   1428 			cp->un.value.level[0] = sc->sc_linein.voll;
   1429 			cp->un.value.level[1] = sc->sc_linein.volr;
   1430 			error = 0;
   1431 		}
   1432 	case IW_REC_LVL:
   1433 		if (cp->type == AUDIO_MIXER_VALUE) {
   1434 			cp->un.value.num_channels = 2;
   1435 			cp->un.value.level[0] = sc->sc_rec.voll;
   1436 			cp->un.value.level[1] = sc->sc_rec.volr;
   1437 			error = 0;
   1438 		}
   1439 		break;
   1440 
   1441 	case IW_DAC_LVL:
   1442 		if (cp->type == AUDIO_MIXER_VALUE) {
   1443 			cp->un.value.num_channels = 2;
   1444 			cp->un.value.level[0] = sc->sc_dac.voll;
   1445 			cp->un.value.level[1] = sc->sc_dac.volr;
   1446 			error = 0;
   1447 		}
   1448 		break;
   1449 
   1450 	case IW_LOOPBACK_LVL:
   1451 		if (cp->type == AUDIO_MIXER_VALUE) {
   1452 			cp->un.value.num_channels = 1;
   1453 			cp->un.value.level[0] = sc->sc_loopback.voll;
   1454 			error = 0;
   1455 		}
   1456 		break;
   1457 
   1458 	case IW_MONO_IN_LVL:
   1459 		if (cp->type == AUDIO_MIXER_VALUE) {
   1460 			cp->un.value.num_channels = 1;
   1461 			cp->un.value.level[0] = sc->sc_monoin.voll;
   1462 			error = 0;
   1463 		}
   1464 		break;
   1465 	case IW_RECORD_SOURCE:
   1466 		cp->un.ord = sc->sc_recsrcbits >> 6;
   1467 		error = 0;
   1468 		break;
   1469 	}
   1470 
   1471 	return error;
   1472 }
   1473 
   1474 
   1475 
   1476 int
   1477 iw_query_devinfo(addr, dip)
   1478 	void	*addr;
   1479 	mixer_devinfo_t *dip;
   1480 {
   1481 
   1482 	switch (dip->index) {
   1483 	case IW_MIC_IN_LVL:	/* Microphone */
   1484 		dip->type = AUDIO_MIXER_VALUE;
   1485 		dip->mixer_class = IW_INPUT_CLASS;
   1486 		dip->prev = AUDIO_MIXER_LAST;
   1487 		dip->next = AUDIO_MIXER_LAST;
   1488 		strcpy(dip->label.name, AudioNmicrophone);
   1489 		dip->un.v.num_channels = 2;
   1490 		strcpy(dip->un.v.units.name, AudioNvolume);
   1491 		break;
   1492 	case IW_AUX1_LVL:
   1493 		dip->type = AUDIO_MIXER_VALUE;
   1494 		dip->mixer_class = IW_INPUT_CLASS;
   1495 		dip->prev = AUDIO_MIXER_LAST;
   1496 		dip->next = AUDIO_MIXER_LAST;
   1497 		strcpy(dip->label.name, AudioNline);
   1498 		dip->un.v.num_channels = 2;
   1499 		strcpy(dip->un.v.units.name, AudioNvolume);
   1500 		break;
   1501 	case IW_AUX2_LVL:
   1502 		dip->type = AUDIO_MIXER_VALUE;
   1503 		dip->mixer_class = IW_INPUT_CLASS;
   1504 		dip->prev = AUDIO_MIXER_LAST;
   1505 		dip->next = AUDIO_MIXER_LAST;
   1506 		strcpy(dip->label.name, AudioNcd);
   1507 		dip->un.v.num_channels = 2;
   1508 		strcpy(dip->un.v.units.name, AudioNvolume);
   1509 		break;
   1510 	case IW_LINE_OUT_LVL:
   1511 		dip->type = AUDIO_MIXER_VALUE;
   1512 		dip->mixer_class = IW_OUTPUT_CLASS;
   1513 		dip->prev = AUDIO_MIXER_LAST;
   1514 		dip->next = AUDIO_MIXER_LAST;
   1515 		strcpy(dip->label.name, AudioNline);
   1516 		dip->un.v.num_channels = 2;
   1517 		strcpy(dip->un.v.units.name, AudioNvolume);
   1518 		break;
   1519 	case IW_DAC_LVL:
   1520 		dip->type = AUDIO_MIXER_VALUE;
   1521 		dip->mixer_class = IW_OUTPUT_CLASS;
   1522 		dip->prev = AUDIO_MIXER_LAST;
   1523 		dip->next = AUDIO_MIXER_LAST;
   1524 		strcpy(dip->label.name, AudioNdac);
   1525 		dip->un.v.num_channels = 2;
   1526 		strcpy(dip->un.v.units.name, AudioNvolume);
   1527 		break;
   1528 	case IW_LINE_IN_LVL:
   1529 		dip->type = AUDIO_MIXER_VALUE;
   1530 		dip->mixer_class = IW_INPUT_CLASS;
   1531 		dip->prev = AUDIO_MIXER_LAST;
   1532 		dip->next = AUDIO_MIXER_LAST;
   1533 		strcpy(dip->label.name, AudioNinput);
   1534 		dip->un.v.num_channels = 2;
   1535 		strcpy(dip->un.v.units.name, AudioNvolume);
   1536 		break;
   1537 	case IW_MONO_IN_LVL:
   1538 		dip->type = AUDIO_MIXER_VALUE;
   1539 		dip->mixer_class = IW_INPUT_CLASS;
   1540 		dip->prev = AUDIO_MIXER_LAST;
   1541 		dip->next = AUDIO_MIXER_LAST;
   1542 		strcpy(dip->label.name, AudioNmono);
   1543 		dip->un.v.num_channels = 1;
   1544 		strcpy(dip->un.v.units.name, AudioNvolume);
   1545 		break;
   1546 
   1547 	case IW_REC_LVL:	/* record level */
   1548 		dip->type = AUDIO_MIXER_VALUE;
   1549 		dip->mixer_class = IW_RECORD_CLASS;
   1550 		dip->prev = AUDIO_MIXER_LAST;
   1551 		dip->next = AUDIO_MIXER_LAST;
   1552 		strcpy(dip->label.name, AudioNrecord);
   1553 		dip->un.v.num_channels = 2;
   1554 		strcpy(dip->un.v.units.name, AudioNvolume);
   1555 		break;
   1556 
   1557 	case IW_LOOPBACK_LVL:
   1558 		dip->type = AUDIO_MIXER_VALUE;
   1559 		dip->mixer_class = IW_RECORD_CLASS;
   1560 		dip->prev = AUDIO_MIXER_LAST;
   1561 		dip->next = AUDIO_MIXER_LAST;
   1562 		strcpy(dip->label.name, "filter");
   1563 		dip->un.v.num_channels = 1;
   1564 		strcpy(dip->un.v.units.name, AudioNvolume);
   1565 		break;
   1566 
   1567 	case IW_RECORD_SOURCE:
   1568 		dip->mixer_class = IW_RECORD_CLASS;
   1569 		dip->type = AUDIO_MIXER_ENUM;
   1570 		dip->prev = AUDIO_MIXER_LAST;
   1571 		dip->next = AUDIO_MIXER_LAST;
   1572 		strcpy(dip->label.name, AudioNsource);
   1573 		dip->un.e.num_mem = 4;
   1574 		strcpy(dip->un.e.member[0].label.name, AudioNline);
   1575 		dip->un.e.member[0].ord = IW_LINE_IN_SRC;
   1576 		strcpy(dip->un.e.member[1].label.name, "aux1");
   1577 		dip->un.e.member[1].ord = IW_AUX1_SRC;
   1578 		strcpy(dip->un.e.member[2].label.name, AudioNmicrophone);
   1579 		dip->un.e.member[2].ord = IW_MIC_IN_SRC;
   1580 		strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
   1581 		dip->un.e.member[3].ord = IW_MIX_OUT_SRC;
   1582 		break;
   1583 	case IW_INPUT_CLASS:
   1584 		dip->type = AUDIO_MIXER_CLASS;
   1585 		dip->mixer_class = IW_INPUT_CLASS;
   1586 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1587 		strcpy(dip->label.name, AudioCinputs);
   1588 		break;
   1589 	case IW_OUTPUT_CLASS:
   1590 		dip->type = AUDIO_MIXER_CLASS;
   1591 		dip->mixer_class = IW_OUTPUT_CLASS;
   1592 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1593 		strcpy(dip->label.name, AudioCoutputs);
   1594 		break;
   1595 	case IW_RECORD_CLASS:	/* record source class */
   1596 		dip->type = AUDIO_MIXER_CLASS;
   1597 		dip->mixer_class = IW_RECORD_CLASS;
   1598 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1599 		strcpy(dip->label.name, AudioCrecord);
   1600 		return 0;
   1601 	default:
   1602 		return ENXIO;
   1603 	}
   1604 	return 0;
   1605 }
   1606 
   1607 
   1608 void *
   1609 iw_malloc(addr, direction, size, pool, flags)
   1610 	void	*addr;
   1611 	int	direction;
   1612 	size_t	size;
   1613 	struct malloc_type *pool;
   1614 	int	flags;
   1615 {
   1616 	struct iw_softc *sc = addr;
   1617 	int drq;
   1618 
   1619 	if (direction == AUMODE_PLAY)
   1620 		drq = sc->sc_playdrq;
   1621 	else
   1622 		drq = sc->sc_recdrq;
   1623 	return (isa_malloc(sc->sc_ic, drq, size, pool, flags));
   1624 }
   1625 
   1626 void
   1627 iw_free(addr, ptr, pool)
   1628 	void	*addr;
   1629 	void	*ptr;
   1630 	struct malloc_type *pool;
   1631 {
   1632 	isa_free(ptr, pool);
   1633 }
   1634 
   1635 size_t
   1636 iw_round_buffersize(addr, direction, size)
   1637 	void	*addr;
   1638 	int	direction;
   1639 	size_t	size;
   1640 {
   1641 	struct iw_softc *sc = addr;
   1642 	bus_size_t maxsize;
   1643 
   1644 	if (direction == AUMODE_PLAY)
   1645 		maxsize = sc->sc_play_maxsize;
   1646 	else
   1647 		maxsize = sc->sc_rec_maxsize;
   1648 
   1649 	if (size > maxsize)
   1650 		size = maxsize;
   1651 	return (size);
   1652 }
   1653 
   1654 paddr_t
   1655 iw_mappage(addr, mem, off, prot)
   1656 	void	*addr;
   1657 	void	*mem;
   1658 	off_t	off;
   1659 	int	prot;
   1660 {
   1661 	return isa_mappage(mem, off, prot);
   1662 }
   1663 
   1664 int
   1665 iw_get_props(addr)
   1666 	void	*addr;
   1667 {
   1668 	struct iw_softc *sc = addr;
   1669 	return AUDIO_PROP_MMAP |
   1670 		(sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
   1671 }
   1672