Home | History | Annotate | Line # | Download | only in isa
ad1848_isa.c revision 1.25
      1 /*	$NetBSD: ad1848_isa.c,v 1.25 2004/07/09 02:42:45 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ken Hornstein and John Kohl.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *	  Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1994 John Brezak
     40  * Copyright (c) 1991-1993 Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the Computer Systems
     54  *	Engineering Group at Lawrence Berkeley Laboratory.
     55  * 4. Neither the name of the University nor of the Laboratory may be used
     56  *    to endorse or promote products derived from this software without
     57  *    specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  */
     72 
     73 /*
     74  * Copyright by Hannu Savolainen 1994
     75  *
     76  * Redistribution and use in source and binary forms, with or without
     77  * modification, are permitted provided that the following conditions are
     78  * met: 1. Redistributions of source code must retain the above copyright
     79  * notice, this list of conditions and the following disclaimer. 2.
     80  * Redistributions in binary form must reproduce the above copyright notice,
     81  * this list of conditions and the following disclaimer in the documentation
     82  * and/or other materials provided with the distribution.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     85  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     86  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     87  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     88  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     90  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     91  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  */
     97 /*
     98  * Portions of this code are from the VOXware support for the ad1848
     99  * by Hannu Savolainen <hannu (at) voxware.pp.fi>
    100  *
    101  * Portions also supplied from the SoundBlaster driver for NetBSD.
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: ad1848_isa.c,v 1.25 2004/07/09 02:42:45 mycroft Exp $");
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/errno.h>
    110 #include <sys/ioctl.h>
    111 #include <sys/syslog.h>
    112 #include <sys/device.h>
    113 #include <sys/proc.h>
    114 #include <sys/buf.h>
    115 
    116 #include <machine/cpu.h>
    117 #include <machine/bus.h>
    118 
    119 #include <sys/audioio.h>
    120 
    121 #include <dev/audio_if.h>
    122 #include <dev/auconv.h>
    123 
    124 #include <dev/isa/isavar.h>
    125 #include <dev/isa/isadmavar.h>
    126 
    127 #include <dev/ic/ad1848reg.h>
    128 #include <dev/ic/cs4231reg.h>
    129 #include <dev/ic/cs4237reg.h>
    130 #include <dev/isa/ad1848var.h>
    131 #include <dev/isa/cs4231var.h>
    132 
    133 #ifdef AUDIO_DEBUG
    134 #define DPRINTF(x)	if (ad1848debug) printf x
    135 extern int	ad1848debug;
    136 #else
    137 #define DPRINTF(x)
    138 #endif
    139 
    140 static int ad1848_isa_read __P(( struct ad1848_softc *, int));
    141 static void ad1848_isa_write __P(( struct ad1848_softc *, int, int));
    142 
    143 int
    144 ad1848_isa_read(sc, index)
    145 	struct ad1848_softc *sc;
    146 	int index;
    147 {
    148 	return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, index));
    149 }
    150 
    151 void
    152 ad1848_isa_write(sc, index, value)
    153 	struct ad1848_softc *sc;
    154 	int index;
    155 	int value;
    156 {
    157 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, index, value);
    158 }
    159 
    160 /*
    161  * Map and probe for the ad1848 chip
    162  */
    163 int
    164 ad1848_isa_mapprobe(isc, iobase)
    165 	struct ad1848_isa_softc *isc;
    166 	int iobase;
    167 {
    168 	struct ad1848_softc *sc = &isc->sc_ad1848;
    169 
    170 	if (!AD1848_BASE_VALID(iobase)) {
    171 #ifdef AUDIO_DEBUG
    172 		printf("ad1848: configured iobase %04x invalid\n", iobase);
    173 #endif
    174 		return 0;
    175 	}
    176 
    177 	/* Map the AD1848 ports */
    178 	if (bus_space_map(sc->sc_iot, iobase, AD1848_NPORT, 0, &sc->sc_ioh))
    179 		return 0;
    180 
    181 	if (!ad1848_isa_probe(isc)) {
    182 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, AD1848_NPORT);
    183 		return 0;
    184 	} else
    185 		return 1;
    186 }
    187 
    188 /*
    189  * Probe for the ad1848 chip
    190  */
    191 int
    192 ad1848_isa_probe(isc)
    193 	struct ad1848_isa_softc *isc;
    194 {
    195 	struct ad1848_softc *sc = &isc->sc_ad1848;
    196 	u_char tmp, tmp1 = 0xff, tmp2 = 0xff;
    197 	int i, t;
    198 
    199 	sc->sc_readreg = ad1848_isa_read;
    200 	sc->sc_writereg = ad1848_isa_write;
    201 
    202 	/* Is there an ad1848 chip ? */
    203 	sc->MCE_bit = MODE_CHANGE_ENABLE;
    204 	sc->mode = 1;	/* MODE 1 = original ad1848/ad1846/cs4248 */
    205 
    206 	/*
    207 	 * Check that the I/O address is in use.
    208 	 *
    209 	 * The SP_IN_INIT bit of the base I/O port is known to be 0 after the
    210 	 * chip has performed its power-on initialization. Just assume
    211 	 * this has happened before the OS is starting.
    212 	 *
    213 	 * If the I/O address is unused, inb() typically returns 0xff.
    214 	 */
    215 	tmp = ADREAD(sc, AD1848_IADDR);
    216 	if (tmp & SP_IN_INIT) { /* Not a AD1848 */
    217 		DPRINTF(("ad_detect_A %x\n", tmp));
    218 		goto bad;
    219 	}
    220 
    221 	/*
    222 	 * Test if it's possible to change contents of the indirect registers.
    223 	 * Registers 0 and 1 are ADC volume registers.  The bit 0x10 is read
    224 	 * only so try to avoid using it.  The bit 0x20 is the mic preamp
    225 	 * enable; on some chips it is always the same in both registers, so
    226 	 * we avoid tests where they are different.
    227 	 */
    228 	ad_write(sc, 0, 0x8a);
    229 	ad_write(sc, 1, 0x45);	/* 0x55 with bit 0x10 clear */
    230 	tmp1 = ad_read(sc, 0);
    231 	tmp2 = ad_read(sc, 1);
    232 
    233 	if (tmp1 != 0x8a || tmp2 != 0x45) {
    234 		DPRINTF(("ad_detect_B (%x/%x)\n", tmp1, tmp2));
    235 		goto bad;
    236 	}
    237 
    238 	ad_write(sc, 0, 0x65);
    239 	ad_write(sc, 1, 0xaa);
    240 	tmp1 = ad_read(sc, 0);
    241 	tmp2 = ad_read(sc, 1);
    242 
    243 	if (tmp1 != 0x65 || tmp2 != 0xaa) {
    244 		DPRINTF(("ad_detect_C (%x/%x)\n", tmp1, tmp2));
    245 		goto bad;
    246 	}
    247 
    248 	/*
    249 	 * The indirect register I12 has some read only bits. Lets
    250 	 * try to change them.
    251 	 */
    252 	tmp = ad_read(sc, SP_MISC_INFO);
    253 	ad_write(sc, SP_MISC_INFO, (~tmp) & 0x0f);
    254 
    255 	/* Here, AD1845 may sometimes be busy.  Wait til it becomes ready. */
    256 	for (t = 0; t < 100000 && ADREAD(sc, AD1848_IADDR) & SP_IN_INIT; t++)
    257 		;
    258 #ifdef AUDIO_DEBUG
    259 	if (t)
    260 		DPRINTF(("ad1848_isa_probe: t %d\n", t));
    261 #endif
    262 
    263 	if ((tmp & 0x0f) != ((tmp1 = ad_read(sc, SP_MISC_INFO)) & 0x0f)) {
    264 		DPRINTF(("ad_detect_D (%x)\n", tmp1));
    265 		goto bad;
    266 	}
    267 
    268 	/*
    269 	 * MSB and 4 LSBs of the reg I12 tell the chip revision.
    270 	 *
    271 	 * A preliminary version of the AD1846 data sheet stated that it
    272 	 * used an ID field of 0x0B.  The current version, however,
    273 	 * states that the AD1846 uses ID 0x0A, just like the AD1848K.
    274 	 *
    275 	 * this switch statement will need updating as newer clones arrive....
    276 	 */
    277 	switch (tmp1 & 0x8f) {
    278 	case 0x09:
    279 		sc->chip_name = "AD1848J";
    280 		break;
    281 	case 0x0A:
    282 		sc->chip_name = "AD1848K";
    283 		break;
    284 #if 0	/* See above */
    285 	case 0x0B:
    286 		sc->chip_name = "AD1846";
    287 		break;
    288 #endif
    289 	case 0x81:
    290 		sc->chip_name = "CS4248revB"; /* or CS4231 rev B; see below */
    291 		break;
    292 	case 0x89:
    293 		sc->chip_name = "CS4248";
    294 		break;
    295 	case 0x8A:
    296 		sc->chip_name = "broken"; /* CS4231/AD1845; see below */
    297 		break;
    298 	default:
    299 		sc->chip_name = "unknown";
    300 		DPRINTF(("ad1848: unknown codec version 0x%02x\n",
    301 			 tmp1 & 0x8f));
    302 		break;
    303 	}
    304 
    305 	/*
    306 	 * The original AD1848/CS4248 has just 16 indirect registers. This
    307 	 * means that I0 and I16 should return the same value (etc.).
    308 	 * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test
    309 	 * fails with CS4231, AD1845, etc.
    310 	 */
    311 	ad_write(sc, SP_MISC_INFO, 0);	/* Mode2 = disabled */
    312 
    313 	for (i = 0; i < 16; i++)
    314 		if ((tmp1 = ad_read(sc, i)) != (tmp2 = ad_read(sc, i + 16))) {
    315 			if (i != SP_TEST_AND_INIT) {
    316 				DPRINTF(("ad_detect_F(%d/%x/%x)\n", i, tmp1, tmp2));
    317 				goto bad;
    318 			}
    319 		}
    320 
    321 	/*
    322 	 * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit
    323 	 * The bit 0x80 is always 1 in CS4248, CS4231, and AD1845.
    324 	 */
    325 	ad_write(sc, SP_MISC_INFO, MODE2);	/* Set mode2, clear 0x80 */
    326 
    327 	tmp1 = ad_read(sc, SP_MISC_INFO);
    328 	if ((tmp1 & 0xc0) == (0x80 | MODE2)) {
    329 		/*
    330 		 *      CS4231 or AD1845 detected - is it?
    331 		 *
    332 		 *	Verify that setting I2 doesn't change I18.
    333 		 */
    334 		ad_write(sc, 18, 0x88); /* Set I18 to known value */
    335 
    336 		ad_write(sc, 2, 0x45);
    337 		if ((tmp2 = ad_read(sc, 18)) != 0x45) { /* No change -> CS4231? */
    338 			ad_write(sc, 2, 0xaa);
    339 			if ((tmp2 = ad_read(sc, 18)) == 0xaa) {     /* Rotten bits? */
    340 				DPRINTF(("ad_detect_H(%x)\n", tmp2));
    341 				goto bad;
    342 			}
    343 
    344 			sc->mode = 2;
    345 
    346 			/*
    347 			 *  It's a CS4231, or another clone with 32 registers.
    348 			 *  Let's find out which by checking I25.
    349 			 */
    350 			if ((tmp1 & 0x8f) == 0x8a) {
    351 				tmp1 = ad_read(sc, CS_VERSION_ID);
    352 				switch (tmp1 & 0xe7) {
    353 				case 0xA0:
    354 					sc->chip_name = "CS4231A";
    355 					break;
    356 				case 0x80:
    357 					/*  XXX I25 no good, AD1845 same as CS4231 */
    358 					/*
    359 					 * XXX
    360 					 * This test is correct only after reset
    361 					 */
    362 					if (ad_read(sc, 17) & 0xf0) {
    363 						sc->chip_name = "AD1845";
    364 						sc->is_ad1845 = 1;
    365 					} else
    366 						sc->chip_name = "CS4231";
    367 					break;
    368 				case 0x82:
    369 					sc->chip_name = "CS4232";
    370 					break;
    371 				case 0x03:
    372 				case 0x83:
    373 					sc->chip_name = "CS4236";
    374 
    375 					/*
    376 					 * Try to switch to mode3 (CS4236B or
    377 					 * CS4237B) by setting CMS to 3.  A
    378 					 * plain CS4236 will not react to
    379 					 * LLBM settings.
    380 					 */
    381 					ad_write(sc, SP_MISC_INFO, MODE3);
    382 
    383 					tmp1 = ad_read(sc, CS_LEFT_LINE_CONTROL);
    384 					ad_write(sc, CS_LEFT_LINE_CONTROL, 0xe0);
    385 					tmp2 = ad_read(sc, CS_LEFT_LINE_CONTROL);
    386 					if (tmp2 == 0xe0) {
    387 						/*
    388 						 * it's a CS4237B or another
    389 						 * clone supporting mode 3.
    390 						 * Let's determine which by
    391 						 * enabling extended registers
    392 						 * and checking X25.
    393 						 */
    394 						tmp2 = ad_xread(sc, CS_X_CHIP_VERSION);
    395 						switch (tmp2 & X_CHIP_VERSIONF_CID) {
    396 						case X_CHIP_CID_CS4236BB:
    397 							sc->chip_name = "CS4236BrevB";
    398 							break;
    399 						case X_CHIP_CID_CS4236B:
    400 							sc->chip_name = "CS4236B";
    401 							break;
    402 						case X_CHIP_CID_CS4237B:
    403 							sc->chip_name = "CS4237B";
    404 							break;
    405 						default:
    406 							sc->chip_name = "CS4236B compatible";
    407 							DPRINTF(("cs4236: unknown mode 3 compatible codec, version 0x%02x\n", tmp2));
    408 							break;
    409 						}
    410 						sc->mode = 3;
    411 					}
    412 
    413 					/* restore volume control information */
    414 					ad_write(sc, CS_LEFT_LINE_CONTROL, tmp1);
    415 					break;
    416 				}
    417 			}
    418 		}
    419 	}
    420 
    421 	/* Wait for 1848 to init */
    422 	while(ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
    423 		;
    424 
    425 	/* Wait for 1848 to autocal */
    426 	ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
    427 	while(ADREAD(sc, AD1848_IDATA) & AUTO_CAL_IN_PROG)
    428 		;
    429 
    430 	return 1;
    431 bad:
    432 	return 0;
    433 }
    434 
    435 /* Unmap the I/O ports */
    436 void
    437 ad1848_isa_unmap(isc)
    438 	struct ad1848_isa_softc *isc;
    439 {
    440 	struct ad1848_softc *sc = &isc->sc_ad1848;
    441 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, AD1848_NPORT);
    442 }
    443 
    444 /*
    445  * Attach hardware to driver, attach hardware driver to audio
    446  * pseudo-device driver .
    447  */
    448 void
    449 ad1848_isa_attach(isc)
    450 	struct ad1848_isa_softc *isc;
    451 {
    452 	struct ad1848_softc *sc = &isc->sc_ad1848;
    453 	int error;
    454 
    455 	sc->sc_readreg = ad1848_isa_read;
    456 	sc->sc_writereg = ad1848_isa_write;
    457 
    458 	if (isc->sc_playdrq != -1) {
    459 		isc->sc_play_maxsize = isa_dmamaxsize(isc->sc_ic,
    460 		    isc->sc_playdrq);
    461 		error = isa_dmamap_create(isc->sc_ic, isc->sc_playdrq,
    462 		    isc->sc_play_maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
    463 		if (error) {
    464 			printf("%s: can't create map for drq %d\n",
    465 			    sc->sc_dev.dv_xname, isc->sc_playdrq);
    466 			return;
    467 		}
    468 	}
    469 	if (isc->sc_recdrq != -1 && isc->sc_recdrq != isc->sc_playdrq) {
    470 		isc->sc_rec_maxsize = isa_dmamaxsize(isc->sc_ic,
    471 		    isc->sc_recdrq);
    472 		error = isa_dmamap_create(isc->sc_ic, isc->sc_recdrq,
    473 		    isc->sc_rec_maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
    474 		if (error) {
    475 			printf("%s: can't create map for drq %d\n",
    476 			    sc->sc_dev.dv_xname, isc->sc_recdrq);
    477 			isa_dmamap_destroy(isc->sc_ic, isc->sc_playdrq);
    478 			return;
    479 		}
    480 	}
    481 
    482 	ad1848_attach(sc);
    483 }
    484 
    485 int
    486 ad1848_isa_open(addr, flags)
    487 	void *addr;
    488 	int flags;
    489 {
    490 	struct ad1848_isa_softc *isc = addr;
    491 	struct ad1848_softc *sc = &isc->sc_ad1848;
    492 	int error, state;
    493 
    494 	DPRINTF(("ad1848_isa_open: sc=%p\n", isc));
    495 	state = 0;
    496 
    497 	if (isc->sc_playdrq != -1) {
    498 		error = isa_drq_alloc(isc->sc_ic, isc->sc_playdrq);
    499 		if (error != 0)
    500 			return EBUSY;
    501 		state |= 1;
    502 	}
    503 	if (isc->sc_recdrq != -1 && isc->sc_recdrq != isc->sc_playdrq) {
    504 		error = isa_drq_alloc(isc->sc_ic, isc->sc_recdrq);
    505 		if (error != 0)
    506 			goto bad;
    507 		state |= 2;
    508 	}
    509 
    510 #ifndef AUDIO_NO_POWER_CTL
    511 	/* Power-up chip */
    512 	if (isc->powerctl)
    513 		isc->powerctl(isc->powerarg, flags);
    514 #endif
    515 
    516 	/* Init and mute wave output */
    517 	ad1848_mute_wave_output(sc, WAVE_MUTE2_INIT, 1);
    518 
    519 	error = ad1848_open(sc, flags);
    520 	if (error) {
    521 #ifndef AUDIO_NO_POWER_CTL
    522 		if (isc->powerctl)
    523 			isc->powerctl(isc->powerarg, 0);
    524 #endif
    525 		goto bad;
    526 	}
    527 
    528 	DPRINTF(("ad1848_isa_open: opened\n"));
    529 	return (0);
    530 
    531 bad:
    532 	if (state & 1)
    533 		isa_drq_free(isc->sc_ic, isc->sc_playdrq);
    534 	if (state & 2)
    535 		isa_drq_free(isc->sc_ic, isc->sc_recdrq);
    536 
    537 	return (error);
    538 }
    539 
    540 /*
    541  * Close function is called at splaudio().
    542  */
    543 void
    544 ad1848_isa_close(addr)
    545 	void *addr;
    546 {
    547 	struct ad1848_isa_softc *isc = addr;
    548 	struct ad1848_softc *sc = &isc->sc_ad1848;
    549 
    550 	if (isc->sc_playdrq != -1)
    551 		isa_drq_free(isc->sc_ic, isc->sc_playdrq);
    552 	if (isc->sc_recdrq != -1 && isc->sc_recdrq != isc->sc_playdrq)
    553 		isa_drq_free(isc->sc_ic, isc->sc_recdrq);
    554 
    555 	DPRINTF(("ad1848_isa_close: stop DMA\n"));
    556 	ad1848_close(sc);
    557 
    558 #ifndef AUDIO_NO_POWER_CTL
    559 	/* Power-down chip */
    560 	if (isc->powerctl)
    561 		isc->powerctl(isc->powerarg, 0);
    562 #endif
    563 }
    564 
    565 int
    566 ad1848_isa_trigger_input(addr, start, end, blksize, intr, arg, param)
    567 	void *addr;
    568 	void *start, *end;
    569 	int blksize;
    570 	void (*intr) __P((void *));
    571 	void *arg;
    572 	struct audio_params *param;
    573 {
    574 	struct ad1848_isa_softc *isc = addr;
    575 	struct ad1848_softc *sc = &isc->sc_ad1848;
    576 	u_int8_t reg;
    577 
    578 	isa_dmastart(isc->sc_ic, isc->sc_recdrq, start,
    579 	    (char *)end - (char *)start, NULL,
    580 	    DMAMODE_READ | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
    581 
    582 	isc->sc_recrun = 1;
    583 	if (sc->mode == 2 && isc->sc_playdrq != isc->sc_recdrq) {
    584 		isc->sc_rintr = intr;
    585 		isc->sc_rarg = arg;
    586 	} else {
    587 		isc->sc_pintr = intr;
    588 		isc->sc_parg = arg;
    589 	}
    590 
    591 
    592 	/*
    593 	 * Calculate number of transfers.
    594 	 * Note that ADPCM is always transferred 4 bytes at at a time.
    595 	 */
    596 	blksize = (param->encoding == AUDIO_ENCODING_ADPCM) ? blksize / 4 - 1 :
    597 	    (blksize * 8) / (param->precision * param->factor * param->channels) - 1;
    598 
    599 	if (sc->mode >= 2) {
    600 		ad_write(sc, CS_LOWER_REC_CNT, blksize & 0xff);
    601 		ad_write(sc, CS_UPPER_REC_CNT, blksize >> 8);
    602 	} else {
    603 		ad_write(sc, SP_LOWER_BASE_COUNT, blksize & 0xff);
    604 		ad_write(sc, SP_UPPER_BASE_COUNT, blksize >> 8);
    605 	}
    606 
    607 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
    608 	ad_write(sc, SP_INTERFACE_CONFIG, CAPTURE_ENABLE|reg);
    609 
    610 	return (0);
    611 }
    612 
    613 int
    614 ad1848_isa_trigger_output(addr, start, end, blksize, intr, arg, param)
    615 	void *addr;
    616 	void *start, *end;
    617 	int blksize;
    618 	void (*intr) __P((void *));
    619 	void *arg;
    620 	struct audio_params *param;
    621 {
    622 	struct ad1848_isa_softc *isc = addr;
    623 	struct ad1848_softc *sc = &isc->sc_ad1848;
    624 	u_int8_t reg;
    625 
    626 	isa_dmastart(isc->sc_ic, isc->sc_playdrq, start,
    627 	    (char *)end - (char *)start, NULL,
    628 	    DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
    629 
    630 	isc->sc_playrun = 1;
    631 	isc->sc_pintr = intr;
    632 	isc->sc_parg = arg;
    633 
    634 	/*
    635 	 * Calculate number of transfers.
    636 	 * Note that ADPCM is always transferred 4 bytes at at a time.
    637 	 */
    638 	blksize = (param->encoding == AUDIO_ENCODING_ADPCM) ? blksize / 4 - 1 :
    639 	    (blksize * 8) / (param->precision * param->factor * param->channels) - 1;
    640 
    641 	ad_write(sc, SP_LOWER_BASE_COUNT, blksize & 0xff);
    642 	ad_write(sc, SP_UPPER_BASE_COUNT, blksize >> 8);
    643 
    644 	/* Unmute wave output */
    645 	ad1848_mute_wave_output(sc, WAVE_MUTE2, 0);
    646 
    647 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
    648 	ad_write(sc, SP_INTERFACE_CONFIG, PLAYBACK_ENABLE|reg);
    649 
    650 	return (0);
    651 }
    652 
    653 int
    654 ad1848_isa_halt_input(addr)
    655 	void *addr;
    656 {
    657 	struct ad1848_isa_softc *isc = addr;
    658 	struct ad1848_softc *sc = &isc->sc_ad1848;
    659 
    660 	if (isc->sc_recrun) {
    661 		ad1848_halt_input(sc);
    662 		isa_dmaabort(isc->sc_ic, isc->sc_recdrq);
    663 		isc->sc_recrun = 0;
    664 	}
    665 
    666 	return (0);
    667 }
    668 
    669 int
    670 ad1848_isa_halt_output(addr)
    671 	void *addr;
    672 {
    673 	struct ad1848_isa_softc *isc = addr;
    674 	struct ad1848_softc *sc = &isc->sc_ad1848;
    675 
    676 	if (isc->sc_playrun) {
    677 		/* Mute wave output */
    678 		ad1848_mute_wave_output(sc, WAVE_MUTE2, 1);
    679 
    680 		ad1848_halt_output(sc);
    681 		isa_dmaabort(isc->sc_ic, isc->sc_playdrq);
    682 		isc->sc_playrun = 0;
    683 	}
    684 
    685 	return (0);
    686 }
    687 
    688 int
    689 ad1848_isa_intr(arg)
    690 	void *arg;
    691 {
    692 	struct ad1848_isa_softc *isc = arg;
    693 	struct ad1848_softc *sc = &isc->sc_ad1848;
    694 	int retval = 0;
    695 	u_char status;
    696 
    697 	/* Get intr status */
    698 	status = ADREAD(sc, AD1848_STATUS);
    699 
    700 #ifdef AUDIO_DEBUG
    701 	if (ad1848debug > 1)
    702 		printf("ad1848_isa_intr: pintr=%p rintr=%p status=%x\n",
    703 		    isc->sc_pintr, isc->sc_rintr, status);
    704 #endif
    705 	isc->sc_interrupts++;
    706 
    707 	/* Handle interrupt */
    708 	if ((status & INTERRUPT_STATUS) != 0) {
    709 		if (sc->mode == 2 && isc->sc_playdrq != isc->sc_recdrq) {
    710 			status = ad_read(sc, CS_IRQ_STATUS);
    711 			if ((status & CS_IRQ_PI) && isc->sc_playrun) {
    712 				(*isc->sc_pintr)(isc->sc_parg);
    713 				retval = 1;
    714 			}
    715 			if ((status & CS_IRQ_CI) && isc->sc_recrun) {
    716 				(*isc->sc_rintr)(isc->sc_rarg);
    717 				retval = 1;
    718 			}
    719 		} else {
    720 			if (isc->sc_playrun) {
    721 				(*isc->sc_pintr)(isc->sc_parg);
    722 				retval = 1;
    723 			}
    724 		}
    725 
    726 		/* Clear interrupt */
    727 		ADWRITE(sc, AD1848_STATUS, 0);
    728 	}
    729 	return(retval);
    730 }
    731 
    732 void *
    733 ad1848_isa_malloc(addr, direction, size, pool, flags)
    734 	void *addr;
    735 	int direction;
    736 	size_t size;
    737 	struct malloc_type *pool;
    738 	int flags;
    739 {
    740 	struct ad1848_isa_softc *isc = addr;
    741 	int drq;
    742 
    743 	if (direction == AUMODE_PLAY)
    744 		drq = isc->sc_playdrq;
    745 	else
    746 		drq = isc->sc_recdrq;
    747 	return (isa_malloc(isc->sc_ic, drq, size, pool, flags));
    748 }
    749 
    750 void
    751 ad1848_isa_free(addr, ptr, pool)
    752 	void *addr;
    753 	void *ptr;
    754 	struct malloc_type *pool;
    755 {
    756 	isa_free(ptr, pool);
    757 }
    758 
    759 size_t
    760 ad1848_isa_round_buffersize(addr, direction, size)
    761 	void *addr;
    762 	int direction;
    763 	size_t size;
    764 {
    765 	struct ad1848_isa_softc *isc = addr;
    766 	bus_size_t maxsize;
    767 
    768 	if (direction == AUMODE_PLAY)
    769 		maxsize = isc->sc_play_maxsize;
    770 	else if (isc->sc_recdrq == isc->sc_playdrq)
    771 		maxsize = isc->sc_play_maxsize;
    772 	else
    773 		maxsize = isc->sc_rec_maxsize;
    774 
    775 	if (size > maxsize)
    776 		size = maxsize;
    777 	return (size);
    778 }
    779 
    780 paddr_t
    781 ad1848_isa_mappage(addr, mem, off, prot)
    782 	void *addr;
    783         void *mem;
    784         off_t off;
    785 	int prot;
    786 {
    787 	return isa_mappage(mem, off, prot);
    788 }
    789 
    790 int
    791 ad1848_isa_get_props(addr)
    792 	void *addr;
    793 {
    794 	struct ad1848_isa_softc *isc = addr;
    795 
    796 	return (AUDIO_PROP_MMAP |
    797 		(isc->sc_playdrq != isc->sc_recdrq ? AUDIO_PROP_FULLDUPLEX : 0));
    798 }
    799