Home | History | Annotate | Line # | Download | only in isa
ad1848_isa.c revision 1.35
      1 /*	$NetBSD: ad1848_isa.c,v 1.35 2008/04/28 18:49:27 garbled 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.35 2008/04/28 18:49:27 garbled 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 <sys/cpu.h>
    117 #include <sys/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( struct ad1848_softc *, int);
    141 static void ad1848_isa_write( struct ad1848_softc *, int, int);
    142 
    143 int
    144 ad1848_isa_read(struct ad1848_softc *sc, int index)
    145 {
    146 
    147 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, index);
    148 }
    149 
    150 void
    151 ad1848_isa_write(struct ad1848_softc *sc, int index, int value)
    152 {
    153 
    154 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, index, value);
    155 }
    156 
    157 /*
    158  * Map and probe for the ad1848 chip
    159  */
    160 int
    161 ad1848_isa_mapprobe(struct ad1848_isa_softc *isc, int iobase)
    162 {
    163 	struct ad1848_softc *sc;
    164 
    165 	sc = &isc->sc_ad1848;
    166 	if (!AD1848_BASE_VALID(iobase)) {
    167 #ifdef AUDIO_DEBUG
    168 		printf("ad1848: configured iobase %04x invalid\n", iobase);
    169 #endif
    170 		return 0;
    171 	}
    172 
    173 	/* Map the AD1848 ports */
    174 	if (bus_space_map(sc->sc_iot, iobase, AD1848_NPORT, 0, &sc->sc_ioh))
    175 		return 0;
    176 
    177 	if (!ad1848_isa_probe(isc)) {
    178 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, AD1848_NPORT);
    179 		return 0;
    180 	} else
    181 		return 1;
    182 }
    183 
    184 /*
    185  * Probe for the ad1848 chip
    186  */
    187 int
    188 ad1848_isa_probe(struct ad1848_isa_softc *isc)
    189 {
    190 	struct ad1848_softc *sc;
    191 	u_char tmp, tmp1 = 0xff, tmp2 = 0xff;
    192 	int i, t;
    193 
    194 	sc = &isc->sc_ad1848;
    195 	sc->sc_readreg = ad1848_isa_read;
    196 	sc->sc_writereg = ad1848_isa_write;
    197 
    198 	/* Is there an ad1848 chip ? */
    199 	sc->MCE_bit = MODE_CHANGE_ENABLE;
    200 	sc->mode = 1;	/* MODE 1 = original ad1848/ad1846/cs4248 */
    201 
    202 	/*
    203 	 * Check that the I/O address is in use.
    204 	 *
    205 	 * The SP_IN_INIT bit of the base I/O port is known to be 0 after the
    206 	 * chip has performed its power-on initialization. Just assume
    207 	 * this has happened before the OS is starting.
    208 	 *
    209 	 * If the I/O address is unused, inb() typically returns 0xff.
    210 	 */
    211 	tmp = ADREAD(sc, AD1848_IADDR);
    212 	if (tmp & SP_IN_INIT) { /* Not a AD1848 */
    213 		DPRINTF(("ad_detect_A %x\n", tmp));
    214 		goto bad;
    215 	}
    216 
    217 	/*
    218 	 * Test if it's possible to change contents of the indirect registers.
    219 	 * Registers 0 and 1 are ADC volume registers.  The bit 0x10 is read
    220 	 * only so try to avoid using it.  The bit 0x20 is the mic preamp
    221 	 * enable; on some chips it is always the same in both registers, so
    222 	 * we avoid tests where they are different.
    223 	 */
    224 	ad_write(sc, 0, 0x8a);
    225 	ad_write(sc, 1, 0x45);	/* 0x55 with bit 0x10 clear */
    226 	tmp1 = ad_read(sc, 0);
    227 	tmp2 = ad_read(sc, 1);
    228 
    229 	if (tmp1 != 0x8a || tmp2 != 0x45) {
    230 		DPRINTF(("ad_detect_B (%x/%x)\n", tmp1, tmp2));
    231 		goto bad;
    232 	}
    233 
    234 	ad_write(sc, 0, 0x65);
    235 	ad_write(sc, 1, 0xaa);
    236 	tmp1 = ad_read(sc, 0);
    237 	tmp2 = ad_read(sc, 1);
    238 
    239 	if (tmp1 != 0x65 || tmp2 != 0xaa) {
    240 		DPRINTF(("ad_detect_C (%x/%x)\n", tmp1, tmp2));
    241 		goto bad;
    242 	}
    243 
    244 	/*
    245 	 * The indirect register I12 has some read only bits. Lets
    246 	 * try to change them.
    247 	 */
    248 	tmp = ad_read(sc, SP_MISC_INFO);
    249 	ad_write(sc, SP_MISC_INFO, (~tmp) & 0x0f);
    250 
    251 	/* Here, AD1845 may sometimes be busy.  Wait til it becomes ready. */
    252 	for (t = 0; t < 100000 && ADREAD(sc, AD1848_IADDR) & SP_IN_INIT; t++)
    253 		;
    254 #ifdef AUDIO_DEBUG
    255 	if (t)
    256 		DPRINTF(("ad1848_isa_probe: t %d\n", t));
    257 #endif
    258 
    259 	if ((tmp & 0x0f) != ((tmp1 = ad_read(sc, SP_MISC_INFO)) & 0x0f)) {
    260 		DPRINTF(("ad_detect_D (%x)\n", tmp1));
    261 		goto bad;
    262 	}
    263 
    264 	/*
    265 	 * MSB and 4 LSBs of the reg I12 tell the chip revision.
    266 	 *
    267 	 * A preliminary version of the AD1846 data sheet stated that it
    268 	 * used an ID field of 0x0B.  The current version, however,
    269 	 * states that the AD1846 uses ID 0x0A, just like the AD1848K.
    270 	 *
    271 	 * this switch statement will need updating as newer clones arrive....
    272 	 */
    273 	switch (tmp1 & 0x8f) {
    274 	case 0x09:
    275 		sc->chip_name = "AD1848J";
    276 		break;
    277 	case 0x0A:
    278 		sc->chip_name = "AD1848K";
    279 		break;
    280 #if 0	/* See above */
    281 	case 0x0B:
    282 		sc->chip_name = "AD1846";
    283 		break;
    284 #endif
    285 	case 0x81:
    286 		sc->chip_name = "CS4248revB"; /* or CS4231 rev B; see below */
    287 		break;
    288 	case 0x89:
    289 		sc->chip_name = "CS4248";
    290 		break;
    291 	case 0x8A:
    292 		sc->chip_name = "broken"; /* CS4231/AD1845; see below */
    293 		break;
    294 	default:
    295 		sc->chip_name = "unknown";
    296 		DPRINTF(("ad1848: unknown codec version 0x%02x\n",
    297 			 tmp1 & 0x8f));
    298 		break;
    299 	}
    300 
    301 	/*
    302 	 * The original AD1848/CS4248 has just 16 indirect registers. This
    303 	 * means that I0 and I16 should return the same value (etc.).
    304 	 * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test
    305 	 * fails with CS4231, AD1845, etc.
    306 	 */
    307 	ad_write(sc, SP_MISC_INFO, 0);	/* Mode2 = disabled */
    308 
    309 	for (i = 0; i < 16; i++)
    310 		if ((tmp1 = ad_read(sc, i)) != (tmp2 = ad_read(sc, i + 16))) {
    311 			if (i != SP_TEST_AND_INIT) {
    312 				DPRINTF(("ad_detect_F(%d/%x/%x)\n", i, tmp1, tmp2));
    313 				goto bad;
    314 			}
    315 		}
    316 
    317 	/*
    318 	 * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit
    319 	 * The bit 0x80 is always 1 in CS4248, CS4231, and AD1845.
    320 	 */
    321 	ad_write(sc, SP_MISC_INFO, MODE2);	/* Set mode2, clear 0x80 */
    322 
    323 	tmp1 = ad_read(sc, SP_MISC_INFO);
    324 	if ((tmp1 & 0xc0) == (0x80 | MODE2)) {
    325 		/*
    326 		 *      CS4231 or AD1845 detected - is it?
    327 		 *
    328 		 *	Verify that setting I2 doesn't change I18.
    329 		 */
    330 		ad_write(sc, 18, 0x88); /* Set I18 to known value */
    331 
    332 		ad_write(sc, 2, 0x45);
    333 		if ((tmp2 = ad_read(sc, 18)) != 0x45) { /* No change -> CS4231? */
    334 			ad_write(sc, 2, 0xaa);
    335 			if ((tmp2 = ad_read(sc, 18)) == 0xaa) {     /* Rotten bits? */
    336 				DPRINTF(("ad_detect_H(%x)\n", tmp2));
    337 				goto bad;
    338 			}
    339 
    340 			sc->mode = 2;
    341 
    342 			/*
    343 			 *  It's a CS4231, or another clone with 32 registers.
    344 			 *  Let's find out which by checking I25.
    345 			 */
    346 			if ((tmp1 & 0x8f) == 0x8a) {
    347 				tmp1 = ad_read(sc, CS_VERSION_ID);
    348 				switch (tmp1 & 0xe7) {
    349 				case 0xA0:
    350 					sc->chip_name = "CS4231A";
    351 					break;
    352 				case 0x80:
    353 					/*  XXX I25 no good, AD1845 same as CS4231 */
    354 					/*
    355 					 * XXX
    356 					 * This test is correct only after reset
    357 					 */
    358 					if (ad_read(sc, 17) & 0xf0) {
    359 						sc->chip_name = "AD1845";
    360 						sc->is_ad1845 = 1;
    361 					} else
    362 						sc->chip_name = "CS4231";
    363 					break;
    364 				case 0x82:
    365 					sc->chip_name = "CS4232";
    366 					break;
    367 				case 0xa2:
    368 					sc->chip_name = "CS4232C";
    369 					break;
    370 				case 0x03:
    371 				case 0x83:
    372 					sc->chip_name = "CS4236";
    373 
    374 					/*
    375 					 * Try to switch to mode3 (CS4236B or
    376 					 * CS4237B) by setting CMS to 3.  A
    377 					 * plain CS4236 will not react to
    378 					 * LLBM settings.
    379 					 */
    380 					ad_write(sc, SP_MISC_INFO, MODE3);
    381 
    382 					tmp1 = ad_read(sc, CS_LEFT_LINE_CONTROL);
    383 					ad_write(sc, CS_LEFT_LINE_CONTROL, 0xe0);
    384 					tmp2 = ad_read(sc, CS_LEFT_LINE_CONTROL);
    385 					if (tmp2 == 0xe0) {
    386 						/*
    387 						 * it's a CS4237B or another
    388 						 * clone supporting mode 3.
    389 						 * Let's determine which by
    390 						 * enabling extended registers
    391 						 * and checking X25.
    392 						 */
    393 						tmp2 = ad_xread(sc, CS_X_CHIP_VERSION);
    394 						switch (tmp2 & X_CHIP_VERSIONF_CID) {
    395 						case X_CHIP_CID_CS4236BB:
    396 							sc->chip_name = "CS4236BrevB";
    397 							break;
    398 						case X_CHIP_CID_CS4236B:
    399 							sc->chip_name = "CS4236B";
    400 							break;
    401 						case X_CHIP_CID_CS4237B:
    402 							sc->chip_name = "CS4237B";
    403 							break;
    404 						default:
    405 							sc->chip_name = "CS4236B compatible";
    406 							DPRINTF(("cs4236: unknown mode 3 compatible codec, version 0x%02x\n", tmp2));
    407 							break;
    408 						}
    409 						sc->mode = 3;
    410 					}
    411 
    412 					/* restore volume control information */
    413 					ad_write(sc, CS_LEFT_LINE_CONTROL, tmp1);
    414 					break;
    415 				}
    416 			}
    417 		}
    418 	}
    419 
    420 	/* Wait for 1848 to init */
    421 	while (ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
    422 		;
    423 
    424 	/* Wait for 1848 to autocal */
    425 	ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
    426 	while (ADREAD(sc, AD1848_IDATA) & AUTO_CAL_IN_PROG)
    427 		;
    428 
    429 	return 1;
    430 bad:
    431 	return 0;
    432 }
    433 
    434 /* Unmap the I/O ports */
    435 void
    436 ad1848_isa_unmap(struct ad1848_isa_softc *isc)
    437 {
    438 	struct ad1848_softc *sc;
    439 
    440 	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(struct ad1848_isa_softc *isc)
    450 {
    451 	struct ad1848_softc *sc;
    452 	int error;
    453 
    454 	sc = &isc->sc_ad1848;
    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 			aprint_error_dev(&sc->sc_dev, "can't create map for drq %d\n",
    465 			    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 			aprint_error_dev(&sc->sc_dev, "can't create map for drq %d\n",
    476 			    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(void *addr, int flags)
    487 {
    488 	struct ad1848_isa_softc *isc;
    489 	struct ad1848_softc *sc;
    490 	int error, state;
    491 
    492 	isc = addr;
    493 	sc = &isc->sc_ad1848;
    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(void *addr)
    545 {
    546 	struct ad1848_isa_softc *isc;
    547 	struct ad1848_softc *sc;
    548 
    549 	DPRINTF(("ad1848_isa_close: stop DMA\n"));
    550 	isc = addr;
    551 	sc = &isc->sc_ad1848;
    552 	ad1848_close(sc);
    553 
    554 #ifndef AUDIO_NO_POWER_CTL
    555 	/* Power-down chip */
    556 	if (isc->powerctl)
    557 		isc->powerctl(isc->powerarg, 0);
    558 #endif
    559 
    560 	if (isc->sc_playdrq != -1)
    561 		isa_drq_free(isc->sc_ic, isc->sc_playdrq);
    562 	if (isc->sc_recdrq != -1 && isc->sc_recdrq != isc->sc_playdrq)
    563 		isa_drq_free(isc->sc_ic, isc->sc_recdrq);
    564 }
    565 
    566 int
    567 ad1848_isa_trigger_input(
    568 	void *addr,
    569 	void *start, void *end,
    570 	int blksize,
    571 	void (*intr)(void *),
    572 	void *arg,
    573 	const audio_params_t *param)
    574 {
    575 	struct ad1848_isa_softc *isc;
    576 	struct ad1848_softc *sc;
    577 	uint8_t reg;
    578 
    579 	isc = addr;
    580 	sc = &isc->sc_ad1848;
    581 	isa_dmastart(isc->sc_ic, isc->sc_recdrq, start,
    582 	    (char *)end - (char *)start, NULL,
    583 	    DMAMODE_READ | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
    584 
    585 	isc->sc_recrun = 1;
    586 	if (sc->mode == 2 && isc->sc_playdrq != isc->sc_recdrq) {
    587 		isc->sc_rintr = intr;
    588 		isc->sc_rarg = arg;
    589 	} else {
    590 		isc->sc_pintr = intr;
    591 		isc->sc_parg = arg;
    592 	}
    593 
    594 	/*
    595 	 * Calculate number of transfers.
    596 	 * Note that ADPCM is always transferred 4 bytes at at a time.
    597 	 */
    598 	blksize = (param->encoding == AUDIO_ENCODING_ADPCM) ? blksize / 4 - 1 :
    599 	    (blksize * 8) / (param->precision * param->channels) - 1;
    600 
    601 	if (sc->mode >= 2) {
    602 		ad_write(sc, CS_LOWER_REC_CNT, blksize & 0xff);
    603 		ad_write(sc, CS_UPPER_REC_CNT, blksize >> 8);
    604 	} else {
    605 		ad_write(sc, SP_LOWER_BASE_COUNT, blksize & 0xff);
    606 		ad_write(sc, SP_UPPER_BASE_COUNT, blksize >> 8);
    607 	}
    608 
    609 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
    610 	ad_write(sc, SP_INTERFACE_CONFIG, CAPTURE_ENABLE|reg);
    611 
    612 	return 0;
    613 }
    614 
    615 int
    616 ad1848_isa_trigger_output(
    617 	void *addr,
    618 	void *start, void *end,
    619 	int blksize,
    620 	void (*intr)(void *),
    621 	void *arg,
    622 	const audio_params_t *param)
    623 {
    624 	struct ad1848_isa_softc *isc;
    625 	struct ad1848_softc *sc;
    626 	uint8_t reg;
    627 
    628 	isc = addr;
    629 	sc = &isc->sc_ad1848;
    630 	isa_dmastart(isc->sc_ic, isc->sc_playdrq, start,
    631 	    (char *)end - (char *)start, NULL,
    632 	    DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
    633 
    634 	isc->sc_playrun = 1;
    635 	isc->sc_pintr = intr;
    636 	isc->sc_parg = arg;
    637 
    638 	/*
    639 	 * Calculate number of transfers.
    640 	 * Note that ADPCM is always transferred 4 bytes at at a time.
    641 	 */
    642 	blksize = (param->encoding == AUDIO_ENCODING_ADPCM) ? blksize / 4 - 1 :
    643 	    (blksize * 8) / (param->precision * param->channels) - 1;
    644 
    645 	ad_write(sc, SP_LOWER_BASE_COUNT, blksize & 0xff);
    646 	ad_write(sc, SP_UPPER_BASE_COUNT, blksize >> 8);
    647 
    648 	/* Unmute wave output */
    649 	ad1848_mute_wave_output(sc, WAVE_MUTE2, 0);
    650 
    651 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
    652 	ad_write(sc, SP_INTERFACE_CONFIG, PLAYBACK_ENABLE|reg);
    653 
    654 	return 0;
    655 }
    656 
    657 int
    658 ad1848_isa_halt_input(void *addr)
    659 {
    660 	struct ad1848_isa_softc *isc;
    661 	struct ad1848_softc *sc;
    662 
    663 	isc = addr;
    664 	sc = &isc->sc_ad1848;
    665 	if (isc->sc_recrun) {
    666 		ad1848_halt_input(sc);
    667 		isa_dmaabort(isc->sc_ic, isc->sc_recdrq);
    668 		isc->sc_recrun = 0;
    669 	}
    670 
    671 	return 0;
    672 }
    673 
    674 int
    675 ad1848_isa_halt_output(void *addr)
    676 {
    677 	struct ad1848_isa_softc *isc;
    678 	struct ad1848_softc *sc;
    679 
    680 	isc = addr;
    681 	sc = &isc->sc_ad1848;
    682 	if (isc->sc_playrun) {
    683 		/* Mute wave output */
    684 		ad1848_mute_wave_output(sc, WAVE_MUTE2, 1);
    685 
    686 		ad1848_halt_output(sc);
    687 		isa_dmaabort(isc->sc_ic, isc->sc_playdrq);
    688 		isc->sc_playrun = 0;
    689 	}
    690 
    691 	return 0;
    692 }
    693 
    694 int
    695 ad1848_isa_intr(void *arg)
    696 {
    697 	struct ad1848_isa_softc *isc;
    698 	struct ad1848_softc *sc;
    699 	int retval;
    700 	u_char status;
    701 
    702 	isc = arg;
    703 	sc = &isc->sc_ad1848;
    704 	retval = 0;
    705 	/* Get intr status */
    706 	status = ADREAD(sc, AD1848_STATUS);
    707 
    708 #ifdef AUDIO_DEBUG
    709 	if (ad1848debug > 1)
    710 		printf("ad1848_isa_intr: pintr=%p rintr=%p status=%x\n",
    711 		    isc->sc_pintr, isc->sc_rintr, status);
    712 #endif
    713 	isc->sc_interrupts++;
    714 
    715 	/* Handle interrupt */
    716 	if ((status & INTERRUPT_STATUS) != 0) {
    717 		if (sc->mode == 2 && isc->sc_playdrq != isc->sc_recdrq) {
    718 			status = ad_read(sc, CS_IRQ_STATUS);
    719 			if ((status & CS_IRQ_PI) && isc->sc_playrun) {
    720 				(*isc->sc_pintr)(isc->sc_parg);
    721 				retval = 1;
    722 			}
    723 			if ((status & CS_IRQ_CI) && isc->sc_recrun) {
    724 				(*isc->sc_rintr)(isc->sc_rarg);
    725 				retval = 1;
    726 			}
    727 		} else {
    728 			if (isc->sc_playrun) {
    729 				(*isc->sc_pintr)(isc->sc_parg);
    730 				retval = 1;
    731 			}
    732 		}
    733 
    734 		/* Clear interrupt */
    735 		ADWRITE(sc, AD1848_STATUS, 0);
    736 	}
    737 	return retval;
    738 }
    739 
    740 void *
    741 ad1848_isa_malloc(
    742 	void *addr,
    743 	int direction,
    744 	size_t size,
    745 	struct malloc_type *pool,
    746 	int flags)
    747 {
    748 	struct ad1848_isa_softc *isc;
    749 	int drq;
    750 
    751 	isc = addr;
    752 	if (direction == AUMODE_PLAY)
    753 		drq = isc->sc_playdrq;
    754 	else
    755 		drq = isc->sc_recdrq;
    756 	return isa_malloc(isc->sc_ic, drq, size, pool, flags);
    757 }
    758 
    759 void
    760 ad1848_isa_free(void *addr, void *ptr, struct malloc_type *pool)
    761 {
    762 
    763 	isa_free(ptr, pool);
    764 }
    765 
    766 size_t
    767 ad1848_isa_round_buffersize(void *addr, int direction, size_t size)
    768 {
    769 	struct ad1848_isa_softc *isc;
    770 	bus_size_t maxsize;
    771 
    772 	isc = addr;
    773 	if (direction == AUMODE_PLAY)
    774 		maxsize = isc->sc_play_maxsize;
    775 	else if (isc->sc_recdrq == isc->sc_playdrq)
    776 		maxsize = isc->sc_play_maxsize;
    777 	else
    778 		maxsize = isc->sc_rec_maxsize;
    779 
    780 	if (size > maxsize)
    781 		size = maxsize;
    782 	return size;
    783 }
    784 
    785 paddr_t
    786 ad1848_isa_mappage(void *addr, void *mem, off_t off, int prot)
    787 {
    788 	return isa_mappage(mem, off, prot);
    789 }
    790 
    791 int
    792 ad1848_isa_get_props(void *addr)
    793 {
    794 	struct ad1848_isa_softc *isc;
    795 
    796 	isc = addr;
    797 	return AUDIO_PROP_MMAP |
    798 		(isc->sc_playdrq != isc->sc_recdrq ? AUDIO_PROP_FULLDUPLEX : 0);
    799 }
    800