Home | History | Annotate | Line # | Download | only in pci
autri.c revision 1.22
      1 /*	$NetBSD: autri.c,v 1.22 2004/11/09 16:28:14 kent Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 SOMEYA Yoshihiko and KUROSAWA Takahiro.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Trident 4DWAVE-DX/NX, SiS 7018, ALi M5451 Sound Driver
     30  *
     31  * The register information is taken from the ALSA driver.
     32  *
     33  * Documentation links:
     34  * - ftp://ftp.alsa-project.org/pub/manuals/trident/
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: autri.c,v 1.22 2004/11/09 16:28:14 kent Exp $");
     39 
     40 #include "midi.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/malloc.h>
     47 #include <sys/device.h>
     48 #include <sys/proc.h>
     49 
     50 #include <dev/pci/pcidevs.h>
     51 #include <dev/pci/pcireg.h>
     52 #include <dev/pci/pcivar.h>
     53 
     54 #include <sys/audioio.h>
     55 #include <dev/audio_if.h>
     56 #include <dev/midi_if.h>
     57 #include <dev/mulaw.h>
     58 #include <dev/auconv.h>
     59 #include <dev/ic/ac97reg.h>
     60 #include <dev/ic/ac97var.h>
     61 #include <dev/ic/mpuvar.h>
     62 
     63 #include <machine/bus.h>
     64 #include <machine/intr.h>
     65 
     66 #include <dev/pci/autrireg.h>
     67 #include <dev/pci/autrivar.h>
     68 
     69 #ifdef AUDIO_DEBUG
     70 # define DPRINTF(x)	if (autridebug) printf x
     71 # define DPRINTFN(n,x)	if (autridebug > (n)) printf x
     72 int autridebug = 0;
     73 #else
     74 # define DPRINTF(x)
     75 # define DPRINTFN(n,x)
     76 #endif
     77 
     78 int	autri_match(struct device *, struct cfdata *, void *);
     79 void	autri_attach(struct device *, struct device *, void *);
     80 int	autri_intr(void *);
     81 
     82 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
     83 #define KERNADDR(p) ((void *)((p)->addr))
     84 
     85 int     autri_allocmem(struct autri_softc *, size_t,
     86 		       size_t, struct autri_dma *);
     87 int     autri_freemem(struct autri_softc *, struct autri_dma *);
     88 
     89 #define TWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
     90 #define TWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
     91 #define TWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
     92 #define TREAD1(sc, r) bus_space_read_1((sc)->memt, (sc)->memh, (r))
     93 #define TREAD2(sc, r) bus_space_read_2((sc)->memt, (sc)->memh, (r))
     94 #define TREAD4(sc, r) bus_space_read_4((sc)->memt, (sc)->memh, (r))
     95 
     96 static __inline void autri_reg_set_1(struct autri_softc *, int, uint8_t);
     97 static __inline void autri_reg_clear_1(struct autri_softc *, int, uint8_t);
     98 static __inline void autri_reg_set_4(struct autri_softc *, int, uint32_t);
     99 static __inline void autri_reg_clear_4(struct autri_softc *, int, uint32_t);
    100 
    101 int	autri_attach_codec(void *sc, struct ac97_codec_if *);
    102 int	autri_read_codec(void *sc, u_int8_t a, u_int16_t *d);
    103 int	autri_write_codec(void *sc, u_int8_t a, u_int16_t d);
    104 int	autri_reset_codec(void *sc);
    105 enum ac97_host_flags	autri_flags_codec(void *sc);
    106 
    107 static void autri_powerhook(int why,void *addr);
    108 static int  autri_init(void *sc);
    109 static struct autri_dma *autri_find_dma(struct autri_softc *, void *);
    110 static void autri_setup_channel(struct autri_softc *sc,int mode,
    111 				struct audio_params *param);
    112 static void autri_enable_interrupt(struct autri_softc *sc, int ch);
    113 static void autri_disable_interrupt(struct autri_softc *sc, int ch);
    114 static void autri_startch(struct autri_softc *sc, int ch, int ch_intr);
    115 static void autri_stopch(struct autri_softc *sc, int ch, int ch_intr);
    116 static void autri_enable_loop_interrupt(void *sc);
    117 #if 0
    118 static void autri_disable_loop_interrupt(void *sc);
    119 #endif
    120 
    121 CFATTACH_DECL(autri, sizeof(struct autri_softc),
    122     autri_match, autri_attach, NULL, NULL);
    123 
    124 int	autri_open(void *, int);
    125 void	autri_close(void *);
    126 int	autri_query_encoding(void *, struct audio_encoding *);
    127 int	autri_set_params(void *, int, int,
    128 			 struct audio_params *, struct audio_params *);
    129 int	autri_round_blocksize(void *, int);
    130 int	autri_trigger_output(void *, void *, void *, int, void (*)(void *),
    131 			     void *, struct audio_params *);
    132 int	autri_trigger_input(void *, void *, void *, int, void (*)(void *),
    133 			    void *, struct audio_params *);
    134 int	autri_halt_output(void *);
    135 int	autri_halt_input(void *);
    136 int	autri_getdev(void *, struct audio_device *);
    137 int	autri_mixer_set_port(void *, mixer_ctrl_t *);
    138 int	autri_mixer_get_port(void *, mixer_ctrl_t *);
    139 void*	autri_malloc(void *, int, size_t, struct malloc_type *, int);
    140 void	autri_free(void *, void *, struct malloc_type *);
    141 size_t	autri_round_buffersize(void *, int, size_t);
    142 paddr_t autri_mappage(void *, void *, off_t, int);
    143 int	autri_get_props(void *);
    144 int	autri_query_devinfo(void *addr, mixer_devinfo_t *dip);
    145 
    146 int     autri_get_portnum_by_name(struct autri_softc *, char *, char *, char *);
    147 
    148 static const struct audio_hw_if autri_hw_if = {
    149         autri_open,
    150 	autri_close,
    151 	NULL,			/* drain */
    152 	autri_query_encoding,
    153 	autri_set_params,
    154 	autri_round_blocksize,
    155 	NULL,			/* commit_settings */
    156 	NULL,			/* init_output */
    157 	NULL,			/* init_input */
    158 	NULL,			/* start_output */
    159 	NULL,			/* start_input */
    160 	autri_halt_output,
    161 	autri_halt_input,
    162 	NULL,			/* speaker_ctl */
    163 	autri_getdev,
    164 	NULL,			/* setfd */
    165 	autri_mixer_set_port,
    166 	autri_mixer_get_port,
    167 	autri_query_devinfo,
    168 	autri_malloc,
    169 	autri_free,
    170 	autri_round_buffersize,
    171 	autri_mappage,
    172 	autri_get_props,
    173 	autri_trigger_output,
    174 	autri_trigger_input,
    175 	NULL,			/* dev_ioctl */
    176 };
    177 
    178 #if NMIDI > 0
    179 void	autri_midi_close(void *);
    180 void	autri_midi_getinfo(void *, struct midi_info *);
    181 int	autri_midi_open(void *, int, void (*)(void *, int),
    182 			   void (*)(void *), void *);
    183 int	autri_midi_output(void *, int);
    184 
    185 const struct midi_hw_if autri_midi_hw_if = {
    186 	autri_midi_open,
    187 	autri_midi_close,
    188 	autri_midi_output,
    189 	autri_midi_getinfo,
    190 	NULL,			/* ioctl */
    191 };
    192 #endif
    193 
    194 /*
    195  * register set/clear bit
    196  */
    197 static __inline void
    198 autri_reg_set_1(struct autri_softc *sc, int no, uint8_t mask)
    199 {
    200 	bus_space_write_1(sc->memt, sc->memh, no,
    201 	    (bus_space_read_1(sc->memt, sc->memh, no) | mask));
    202 }
    203 
    204 static __inline void
    205 autri_reg_clear_1(struct autri_softc *sc, int no, uint8_t mask)
    206 {
    207 	bus_space_write_1(sc->memt, sc->memh, no,
    208 	    (bus_space_read_1(sc->memt, sc->memh, no) & ~mask));
    209 }
    210 
    211 static __inline void
    212 autri_reg_set_4(struct autri_softc *sc, int no, uint32_t mask)
    213 {
    214 	bus_space_write_4(sc->memt, sc->memh, no,
    215 	    (bus_space_read_4(sc->memt, sc->memh, no) | mask));
    216 }
    217 
    218 static __inline void
    219 autri_reg_clear_4(struct autri_softc *sc, int no, uint32_t mask)
    220 {
    221 	bus_space_write_4(sc->memt, sc->memh, no,
    222 	    (bus_space_read_4(sc->memt, sc->memh, no) & ~mask));
    223 }
    224 
    225 /*
    226  * AC'97 codec
    227  */
    228 int
    229 autri_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    230 {
    231 	struct autri_codec_softc *sc = sc_;
    232 
    233 	DPRINTF(("autri_attach_codec()\n"));
    234 
    235 	sc->codec_if = codec_if;
    236 	return 0;
    237 }
    238 
    239 int
    240 autri_read_codec(void *sc_, u_int8_t index, u_int16_t *data)
    241 {
    242 	struct autri_codec_softc *codec = sc_;
    243 	struct autri_softc *sc = codec->sc;
    244 	u_int32_t status, addr, cmd, busy;
    245 	u_int16_t count;
    246 
    247 	/*DPRINTF(("sc->sc->type : 0x%X",sc->sc->type));*/
    248 
    249 	switch (sc->sc_devid) {
    250 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    251 		addr = AUTRI_DX_ACR1;
    252 		cmd  = AUTRI_DX_ACR1_CMD_READ;
    253 		busy = AUTRI_DX_ACR1_BUSY_READ;
    254 		break;
    255 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    256 		addr = AUTRI_NX_ACR2;
    257 		cmd  = AUTRI_NX_ACR2_CMD_READ;
    258 		busy = AUTRI_NX_ACR2_BUSY_READ | AUTRI_NX_ACR2_RECV_WAIT;
    259 		break;
    260 	case AUTRI_DEVICE_ID_SIS_7018:
    261 		addr = AUTRI_SIS_ACRD;
    262 		cmd  = AUTRI_SIS_ACRD_CMD_READ;
    263 		busy = AUTRI_SIS_ACRD_BUSY_READ | AUTRI_SIS_ACRD_AUDIO_BUSY;
    264 		break;
    265 	case AUTRI_DEVICE_ID_ALI_M5451:
    266 		if (sc->sc_revision > 0x01)
    267 			addr = AUTRI_ALI_ACWR;
    268 		else
    269 			addr = AUTRI_ALI_ACRD;
    270 		cmd  = AUTRI_ALI_ACRD_CMD_READ;
    271 		busy = AUTRI_ALI_ACRD_BUSY_READ;
    272 		break;
    273 	default:
    274 		printf("%s: autri_read_codec : unknown device\n",
    275 		       sc->sc_dev.dv_xname);
    276 		return -1;
    277 	}
    278 
    279 	/* wait for 'Ready to Read' */
    280 	for (count=0; count<0xffff; count++) {
    281 		if ((TREAD4(sc, addr) & busy) == 0)
    282 			break;
    283 	}
    284 
    285 	if (count == 0xffff) {
    286 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    287 		       sc->sc_dev.dv_xname);
    288 		return -1;
    289 	}
    290 
    291 	/* send Read Command to AC'97 */
    292 	TWRITE4(sc, addr, (index & 0x7f) | cmd);
    293 
    294 	/* wait for 'Returned data is avalable' */
    295 	for (count=0; count<0xffff; count++) {
    296 		status = TREAD4(sc, addr);
    297 		if ((status & busy) == 0)
    298 			break;
    299 	}
    300 
    301 	if (count == 0xffff) {
    302 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    303 		       sc->sc_dev.dv_xname);
    304 		return -1;
    305 	}
    306 
    307 	*data =  (status >> 16) & 0x0000ffff;
    308 	/*DPRINTF(("autri_read_codec(0x%X) return 0x%X\n",reg,*data));*/
    309 	return 0;
    310 }
    311 
    312 int
    313 autri_write_codec(void *sc_, u_int8_t index, u_int16_t data)
    314 {
    315 	struct autri_codec_softc *codec = sc_;
    316 	struct autri_softc *sc = codec->sc;
    317 	u_int32_t addr, cmd, busy;
    318 	u_int16_t count;
    319 
    320 	/*DPRINTF(("autri_write_codec(0x%X,0x%X)\n",index,data));*/
    321 
    322 	switch (sc->sc_devid) {
    323 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    324 		addr = AUTRI_DX_ACR0;
    325 		cmd  = AUTRI_DX_ACR0_CMD_WRITE;
    326 		busy = AUTRI_DX_ACR0_BUSY_WRITE;
    327 		break;
    328 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    329 		addr = AUTRI_NX_ACR1;
    330 		cmd  = AUTRI_NX_ACR1_CMD_WRITE;
    331 		busy = AUTRI_NX_ACR1_BUSY_WRITE;
    332 		break;
    333 	case AUTRI_DEVICE_ID_SIS_7018:
    334 		addr = AUTRI_SIS_ACWR;
    335 		cmd  = AUTRI_SIS_ACWR_CMD_WRITE;
    336 		busy = AUTRI_SIS_ACWR_BUSY_WRITE | AUTRI_SIS_ACWR_AUDIO_BUSY;
    337 		break;
    338 	case AUTRI_DEVICE_ID_ALI_M5451:
    339 		addr = AUTRI_ALI_ACWR;
    340 		cmd  = AUTRI_ALI_ACWR_CMD_WRITE;
    341 		if (sc->sc_revision > 0x01)
    342 			cmd  |= 0x0100;
    343 		busy = AUTRI_ALI_ACWR_BUSY_WRITE;
    344 		break;
    345 	default:
    346 		printf("%s: autri_write_codec : unknown device.\n",
    347 		       sc->sc_dev.dv_xname);
    348 		return -1;
    349 	}
    350 
    351 	/* wait for 'Ready to Write' */
    352 	for (count=0; count<0xffff; count++) {
    353 		if ((TREAD4(sc, addr) & busy) == 0)
    354 			break;
    355 	}
    356 
    357 	if (count == 0xffff) {
    358 		printf("%s: Codec timeout. Busy writing AC'97 codec\n",
    359 		       sc->sc_dev.dv_xname);
    360 		return -1;
    361 	}
    362 
    363 	/* send Write Command to AC'97 */
    364 	TWRITE4(sc, addr, (data << 16) | (index & 0x7f) | cmd);
    365 
    366 	return 0;
    367 }
    368 
    369 int
    370 autri_reset_codec(void *sc_)
    371 {
    372 	struct autri_codec_softc *codec = sc_;
    373 	struct autri_softc *sc = codec->sc;
    374 	u_int32_t reg, ready;
    375 	int addr, count = 200;
    376 
    377 	DPRINTF(("autri_reset_codec(codec=%p,sc=%p)\n",codec,sc));
    378 	DPRINTF(("sc->sc_devid=%X\n",sc->sc_devid));
    379 
    380 	switch (sc->sc_devid) {
    381 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    382 		/* warm reset AC'97 codec */
    383 		autri_reg_set_4(sc, AUTRI_DX_ACR2, 1);
    384 		delay(100);
    385 		/* release reset */
    386 		autri_reg_clear_4(sc, AUTRI_DX_ACR2, 1);
    387 		delay(100);
    388 
    389 		addr = AUTRI_DX_ACR2;
    390 		ready = AUTRI_DX_ACR2_CODEC_READY;
    391 		break;
    392 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    393 		/* warm reset AC'97 codec */
    394 		autri_reg_set_4(sc, AUTRI_NX_ACR0, 1);
    395 		delay(100);
    396 		/* release reset */
    397 		autri_reg_clear_4(sc, AUTRI_NX_ACR0, 1);
    398 		delay(100);
    399 
    400 		addr = AUTRI_NX_ACR0;
    401 		ready = AUTRI_NX_ACR0_CODEC_READY;
    402 		break;
    403 	case AUTRI_DEVICE_ID_SIS_7018:
    404 		/* cold reset AC'97 codec */
    405 		autri_reg_set_4(sc, AUTRI_SIS_SCTRL, 2);
    406 		delay(1000);
    407 		/* release reset (warm & cold) */
    408 		autri_reg_clear_4(sc, AUTRI_SIS_SCTRL, 3);
    409 		delay(2000);
    410 
    411 		addr = AUTRI_SIS_SCTRL;
    412 		ready = AUTRI_SIS_SCTRL_CODEC_READY;
    413 		break;
    414 	case AUTRI_DEVICE_ID_ALI_M5451:
    415 		/* warm reset AC'97 codec */
    416 		autri_reg_set_4(sc, AUTRI_ALI_SCTRL, 1);
    417 		delay(100);
    418 		/* release reset (warm & cold) */
    419 		autri_reg_clear_4(sc, AUTRI_ALI_SCTRL, 3);
    420 		delay(100);
    421 
    422 		addr = AUTRI_ALI_SCTRL;
    423 		ready = AUTRI_ALI_SCTRL_CODEC_READY;
    424 		break;
    425 	default:
    426 		printf("%s: autri_reset_codec : unknown device\n",
    427 		       sc->sc_dev.dv_xname);
    428 		return EOPNOTSUPP;
    429 	}
    430 
    431 	/* wait for 'Codec Ready' */
    432 	while (count--) {
    433 		reg = TREAD4(sc, addr);
    434 		if (reg & ready)
    435 			break;
    436 		delay(1000);
    437 	}
    438 
    439 	if (count == 0) {
    440 		printf("%s: Codec timeout. AC'97 is not ready for operation.\n",
    441 		       sc->sc_dev.dv_xname);
    442 		return ETIMEDOUT;
    443 	}
    444 	return 0;
    445 }
    446 
    447 enum ac97_host_flags
    448 autri_flags_codec(void *sc_)
    449 {
    450 	return AC97_HOST_DONT_READ;
    451 }
    452 
    453 /*
    454  *
    455  */
    456 
    457 int
    458 autri_match(struct device *parent, struct cfdata *match, void *aux)
    459 {
    460 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    461 
    462 	switch (PCI_VENDOR(pa->pa_id)) {
    463 	case PCI_VENDOR_TRIDENT:
    464 		switch (PCI_PRODUCT(pa->pa_id)) {
    465 		case PCI_PRODUCT_TRIDENT_4DWAVE_DX:
    466 		case PCI_PRODUCT_TRIDENT_4DWAVE_NX:
    467 			return 1;
    468 		}
    469 		break;
    470 	case PCI_VENDOR_SIS:
    471 		switch (PCI_PRODUCT(pa->pa_id)) {
    472 		case PCI_PRODUCT_SIS_7018:
    473 			return 1;
    474 		}
    475 		break;
    476 	case PCI_VENDOR_ALI:
    477 		switch (PCI_PRODUCT(pa->pa_id)) {
    478 		case PCI_PRODUCT_ALI_M5451:
    479 			return 1;
    480 		}
    481 		break;
    482 	}
    483 
    484 	return 0;
    485 }
    486 
    487 void
    488 autri_attach(struct device *parent, struct device *self, void *aux)
    489 {
    490 	struct autri_softc *sc = (struct autri_softc *)self;
    491 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    492 	pci_chipset_tag_t pc = pa->pa_pc;
    493 	struct autri_codec_softc *codec;
    494 	pci_intr_handle_t ih;
    495 	char const *intrstr;
    496 	char devinfo[256];
    497 	int r;
    498 	u_int32_t reg;
    499 
    500 	aprint_naive(": Audio controller\n");
    501 
    502 	sc->sc_devid = pa->pa_id;
    503 	sc->sc_class = pa->pa_class;
    504 
    505 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    506 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    507 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_revision);
    508 
    509 	/* map register to memory */
    510 	if (pci_mapreg_map(pa, AUTRI_PCI_MEMORY_BASE,
    511 	    PCI_MAPREG_TYPE_MEM, 0, &sc->memt, &sc->memh, NULL, NULL)) {
    512 		aprint_error("%s: can't map memory space\n",
    513 		    sc->sc_dev.dv_xname);
    514 		return;
    515 	}
    516 
    517 	/* map and establish the interrupt */
    518 	if (pci_intr_map(pa, &ih)) {
    519 		aprint_error("%s: couldn't map interrupt\n",
    520 		    sc->sc_dev.dv_xname);
    521 		return;
    522 	}
    523 	intrstr = pci_intr_string(pc, ih);
    524 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, autri_intr, sc);
    525 	if (sc->sc_ih == NULL) {
    526 		aprint_error("%s: couldn't establish interrupt",
    527 		    sc->sc_dev.dv_xname);
    528 		if (intrstr != NULL)
    529 			aprint_normal(" at %s", intrstr);
    530 		aprint_normal("\n");
    531 		return;
    532 	}
    533 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    534 
    535 	sc->sc_dmatag = pa->pa_dmat;
    536 	sc->sc_pc = pc;
    537 	sc->sc_pt = pa->pa_tag;
    538 
    539 	/* enable the device */
    540 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    541 	reg |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    542 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    543 
    544 	/* initialize the device */
    545 	autri_init(sc);
    546 
    547 	/* attach AC'97 codec */
    548 	codec = &sc->sc_codec;
    549 	memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
    550 	codec->sc = sc;
    551 
    552 	codec->host_if.arg = codec;
    553 	codec->host_if.attach = autri_attach_codec;
    554 	codec->host_if.reset = autri_reset_codec;
    555 	codec->host_if.read = autri_read_codec;
    556 	codec->host_if.write = autri_write_codec;
    557 	codec->host_if.flags = autri_flags_codec;
    558 
    559 	if ((r = ac97_attach(&codec->host_if)) != 0) {
    560 		aprint_error("%s: can't attach codec (error 0x%X)\n",
    561 		    sc->sc_dev.dv_xname, r);
    562 		return;
    563 	}
    564 
    565 	audio_attach_mi(&autri_hw_if, sc, &sc->sc_dev);
    566 
    567 #if NMIDI > 0
    568 	midi_attach_mi(&autri_midi_hw_if, sc, &sc->sc_dev);
    569 #endif
    570 
    571 	sc->sc_old_power = PWR_RESUME;
    572 	powerhook_establish(autri_powerhook, sc);
    573 }
    574 
    575 static void
    576 autri_powerhook(int why, void *addr)
    577 {
    578 	struct autri_softc *sc = addr;
    579 
    580 	if (why == PWR_RESUME && sc->sc_old_power == PWR_SUSPEND) {
    581 		DPRINTF(("PWR_RESUME\n"));
    582 		autri_init(sc);
    583 		/*autri_reset_codec(&sc->sc_codec);*/
    584 		(sc->sc_codec.codec_if->vtbl->restore_ports)(sc->sc_codec.codec_if);
    585 	}
    586 	sc->sc_old_power = why;
    587 }
    588 
    589 int
    590 autri_init(void *sc_)
    591 {
    592 	struct autri_softc *sc = sc_;
    593 	u_int32_t reg;
    594 
    595 	pci_chipset_tag_t pc = sc->sc_pc;
    596 	pcitag_t pt = sc->sc_pt;
    597 
    598 	DPRINTF(("in autri_init()\n"));
    599 	DPRINTFN(5,("pci_conf_read(0x40) : 0x%X\n",pci_conf_read(pc,pt,0x40)));
    600 	DPRINTFN(5,("pci_conf_read(0x44) : 0x%X\n",pci_conf_read(pc,pt,0x44)));
    601 
    602 	switch (sc->sc_devid) {
    603 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    604 		/* disable Legacy Control */
    605 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    606 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    607 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    608 		delay(100);
    609 		/* audio engine reset */
    610 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    611 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00040000);
    612 		delay(100);
    613 		/* release reset */
    614 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    615 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    616 		delay(100);
    617 		/* DAC on */
    618 		autri_reg_set_4(sc,AUTRI_DX_ACR2,0x02);
    619 		break;
    620 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    621 		/* disable Legacy Control */
    622 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    623 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    624 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    625 		delay(100);
    626 		/* audio engine reset */
    627 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    628 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00010000);
    629 		delay(100);
    630 		/* release reset */
    631 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    632 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00010000);
    633 		delay(100);
    634 		/* DAC on */
    635 		autri_reg_set_4(sc,AUTRI_NX_ACR0,0x02);
    636 		break;
    637 	case AUTRI_DEVICE_ID_SIS_7018:
    638 		/* disable Legacy Control */
    639 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    640 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    641 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    642 		delay(100);
    643 		/* reset Digital Controller */
    644 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    645 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    646 		delay(100);
    647 		/* release reset */
    648 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    649 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    650 		delay(100);
    651 		/* disable AC97 GPIO interrupt */
    652 		TWRITE1(sc, AUTRI_SIS_ACGPIO, 0);
    653 		/* enable 64 channel mode */
    654 		autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, BANK_B_EN);
    655 		break;
    656 	case AUTRI_DEVICE_ID_ALI_M5451:
    657 		/* disable Legacy Control */
    658 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    659 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    660 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    661 		delay(100);
    662 		/* reset Digital Controller */
    663 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    664 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    665 		delay(100);
    666 		/* release reset */
    667 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    668 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    669 		delay(100);
    670 		/* enable PCM input */
    671 		autri_reg_set_4(sc, AUTRI_ALI_GCONTROL, AUTRI_ALI_GCONTROL_PCM_IN);
    672 		break;
    673 	}
    674 
    675 	if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    676 		sc->sc_play.ch      = 0;
    677 		sc->sc_play.ch_intr = 1;
    678 		sc->sc_rec.ch       = 31;
    679 		sc->sc_rec.ch_intr  = 2;
    680 	} else {
    681 		sc->sc_play.ch      = 0x20;
    682 		sc->sc_play.ch_intr = 0x21;
    683 		sc->sc_rec.ch       = 0x22;
    684 		sc->sc_rec.ch_intr  = 0x23;
    685 	}
    686 
    687 	/* clear channel status */
    688 	TWRITE4(sc, AUTRI_STOP_A, 0xffffffff);
    689 	TWRITE4(sc, AUTRI_STOP_B, 0xffffffff);
    690 
    691 	/* disable channel interrupt */
    692 	TWRITE4(sc, AUTRI_AINTEN_A, 0);
    693 	TWRITE4(sc, AUTRI_AINTEN_B, 0);
    694 
    695 #if 0
    696 	/* TLB */
    697 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    698 		TWRITE4(sc,AUTRI_NX_TLBC,0);
    699 	}
    700 #endif
    701 
    702 	autri_enable_loop_interrupt(sc);
    703 
    704 	DPRINTF(("out autri_init()\n"));
    705 	return 0;
    706 }
    707 
    708 static void
    709 autri_enable_loop_interrupt(void *sc_)
    710 {
    711 	struct autri_softc *sc = sc_;
    712 	u_int32_t reg;
    713 
    714 	/*reg = (ENDLP_IE | MIDLP_IE);*/
    715 	reg = ENDLP_IE;
    716 
    717 	if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
    718 		reg |= BANK_B_EN;
    719 
    720 	autri_reg_set_4(sc,AUTRI_LFO_GC_CIR,reg);
    721 }
    722 
    723 #if 0
    724 static void
    725 autri_disable_loop_interrupt(void *sc_)
    726 {
    727 	struct autri_softc *sc = sc_;
    728 	u_int32_t reg;
    729 
    730 	reg = (ENDLP_IE | MIDLP_IE);
    731 	autri_reg_clear_4(sc,AUTRI_LFO_GC_CIR,reg);
    732 }
    733 #endif
    734 
    735 int
    736 autri_intr(void *p)
    737 {
    738 	struct autri_softc *sc = p;
    739 	u_int32_t intsrc;
    740 	u_int32_t mask, active[2];
    741 	int ch, endch;
    742 /*
    743 	u_int32_t reg;
    744 	u_int32_t cso,eso;
    745 */
    746 
    747 	intsrc = TREAD4(sc,AUTRI_MISCINT);
    748 	if ((intsrc & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
    749 		return 0;
    750 
    751 	if (intsrc & ADDRESS_IRQ) {
    752 
    753 		active[0] = TREAD4(sc,AUTRI_AIN_A);
    754 		active[1] = TREAD4(sc,AUTRI_AIN_B);
    755 
    756 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    757 			endch = 32;
    758 		} else {
    759 			endch = 64;
    760 		}
    761 
    762 		for (ch=0; ch<endch; ch++) {
    763 			mask = 1 << (ch & 0x1f);
    764 			if (active[(ch & 0x20) ? 1 : 0] & mask) {
    765 
    766 				/* clear interrupt */
    767 				TWRITE4(sc, (ch & 0x20) ? AUTRI_AIN_B : AUTRI_AIN_A, mask);
    768 				/* disable interrupt */
    769 				autri_reg_clear_4(sc,(ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    770 #if 0
    771 				reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
    772 				TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | ch);
    773 
    774 				if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    775 				  cso = TREAD4(sc, 0xe0) & 0x00ffffff;
    776 				  eso = TREAD4(sc, 0xe8) & 0x00ffffff;
    777 				} else {
    778 				  cso = (TREAD4(sc, 0xe0) >> 16) & 0x0000ffff;
    779 				  eso = (TREAD4(sc, 0xe8) >> 16) & 0x0000ffff;
    780 				}
    781 				/*printf("cso=%d, eso=%d\n",cso,eso);*/
    782 #endif
    783 				if (ch == sc->sc_play.ch_intr) {
    784 					if (sc->sc_play.intr)
    785 						sc->sc_play.intr(sc->sc_play.intr_arg);
    786 				}
    787 
    788 				if (ch == sc->sc_rec.ch_intr) {
    789 					if (sc->sc_rec.intr)
    790 						sc->sc_rec.intr(sc->sc_rec.intr_arg);
    791 				}
    792 
    793 				/* enable interrupt */
    794 				autri_reg_set_4(sc, (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    795 			}
    796 		}
    797 	}
    798 
    799 	if (intsrc & MPU401_IRQ) {
    800 		/* XXX */
    801 	}
    802 
    803 	autri_reg_set_4(sc,AUTRI_MISCINT,
    804 		ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW);
    805 
    806 	return 1;
    807 }
    808 
    809 /*
    810  *
    811  */
    812 
    813 int
    814 autri_allocmem(struct autri_softc *sc, size_t size, size_t align,
    815 	       struct autri_dma *p)
    816 {
    817 	int error;
    818 
    819 	p->size = size;
    820 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    821 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    822 	    &p->nsegs, BUS_DMA_NOWAIT);
    823 	if (error)
    824 		return (error);
    825 
    826 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    827 	    &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    828 	if (error)
    829 		goto free;
    830 
    831 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    832 	    0, BUS_DMA_NOWAIT, &p->map);
    833 	if (error)
    834 		goto unmap;
    835 
    836 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    837 	    BUS_DMA_NOWAIT);
    838 	if (error)
    839 		goto destroy;
    840 	return (0);
    841 
    842 destroy:
    843 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    844 unmap:
    845 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    846 free:
    847 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    848 	return (error);
    849 }
    850 
    851 int
    852 autri_freemem(struct autri_softc *sc, struct autri_dma *p)
    853 {
    854 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    855 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    856 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    857 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    858 	return 0;
    859 }
    860 
    861 int
    862 autri_open(void *addr, int flags)
    863 {
    864 	DPRINTF(("autri_open()\n"));
    865 	DPRINTFN(5,("MISCINT    : 0x%08X\n",
    866 		    TREAD4((struct autri_softc *)addr, AUTRI_MISCINT)));
    867 	DPRINTFN(5,("LFO_GC_CIR : 0x%08X\n",
    868 		    TREAD4((struct autri_softc *)addr, AUTRI_LFO_GC_CIR)));
    869         return 0;
    870 }
    871 
    872 void
    873 autri_close(void *addr)
    874 {
    875 	DPRINTF(("autri_close()\n"));
    876 }
    877 
    878 int
    879 autri_query_encoding(void *addr, struct audio_encoding *fp)
    880 {
    881 	switch (fp->index) {
    882 	case 0:
    883 		strcpy(fp->name, AudioEulinear);
    884 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    885 		fp->precision = 8;
    886 		fp->flags = 0;
    887 		break;
    888 	case 1:
    889 		strcpy(fp->name, AudioEmulaw);
    890 		fp->encoding = AUDIO_ENCODING_ULAW;
    891 		fp->precision = 8;
    892 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    893 		break;
    894 	case 2:
    895 		strcpy(fp->name, AudioEalaw);
    896 		fp->encoding = AUDIO_ENCODING_ALAW;
    897 		fp->precision = 8;
    898 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    899 		break;
    900 	case 3:
    901 		strcpy(fp->name, AudioEslinear);
    902 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    903 		fp->precision = 8;
    904 		fp->flags = 0;
    905 		break;
    906 	case 4:
    907 		strcpy(fp->name, AudioEslinear_le);
    908 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    909 		fp->precision = 16;
    910 		fp->flags = 0;
    911 		break;
    912 	case 5:
    913 		strcpy(fp->name, AudioEulinear_le);
    914 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    915 		fp->precision = 16;
    916 		fp->flags = 0;
    917 		break;
    918 	case 6:
    919 		strcpy(fp->name, AudioEslinear_be);
    920 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    921 		fp->precision = 16;
    922 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    923 		break;
    924 	case 7:
    925 		strcpy(fp->name, AudioEulinear_be);
    926 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    927 		fp->precision = 16;
    928 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    929 		break;
    930 	default:
    931 		return (EINVAL);
    932 	}
    933 
    934 	return 0;
    935 }
    936 
    937 int
    938 autri_set_params(void *addr, int setmode, int usemode,
    939 		 struct audio_params *play, struct audio_params *rec)
    940 {
    941 	struct audio_params *p;
    942 	int mode;
    943 
    944 	for (mode = AUMODE_RECORD; mode != -1;
    945 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    946 		if ((setmode & mode) == 0)
    947 			continue;
    948 
    949 		p = mode == AUMODE_PLAY ? play : rec;
    950 
    951 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    952 		    (p->precision != 8 && p->precision != 16) ||
    953 		    (p->channels != 1 && p->channels != 2))
    954 			return (EINVAL);
    955 
    956 		p->factor = 1;
    957 		p->sw_code = 0;
    958 		switch (p->encoding) {
    959 		case AUDIO_ENCODING_SLINEAR_BE:
    960 		case AUDIO_ENCODING_ULINEAR_BE:
    961 			if (p->precision == 16)
    962 				p->sw_code = swap_bytes;
    963 			break;
    964 		case AUDIO_ENCODING_SLINEAR_LE:
    965 		case AUDIO_ENCODING_ULINEAR_LE:
    966 			break;
    967 		case AUDIO_ENCODING_ULAW:
    968 			if (mode == AUMODE_PLAY)
    969 				p->sw_code = mulaw_to_ulinear8;
    970 			else
    971 				p->sw_code = ulinear8_to_mulaw;
    972 
    973 			break;
    974 		case AUDIO_ENCODING_ALAW:
    975 			if (mode == AUMODE_PLAY)
    976 				p->sw_code = alaw_to_ulinear8;
    977 			else
    978 				p->sw_code = ulinear8_to_alaw;
    979 
    980 			break;
    981 		default:
    982 			return (EINVAL);
    983 		}
    984 	}
    985 
    986 	return 0;
    987 }
    988 
    989 int
    990 autri_round_blocksize(void *addr, int block)
    991 {
    992 	return (block & -4);
    993 }
    994 
    995 int
    996 autri_halt_output(void *addr)
    997 {
    998 	struct autri_softc *sc = addr;
    999 
   1000 	DPRINTF(("autri_halt_output()\n"));
   1001 
   1002 	sc->sc_play.intr = NULL;
   1003 	autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
   1004 	autri_disable_interrupt(sc, sc->sc_play.ch_intr);
   1005 
   1006 	return 0;
   1007 }
   1008 
   1009 int
   1010 autri_halt_input(void *addr)
   1011 {
   1012 	struct autri_softc *sc = addr;
   1013 
   1014 	DPRINTF(("autri_halt_input()\n"));
   1015 
   1016 	sc->sc_rec.intr = NULL;
   1017 	autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1018 	autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
   1019 
   1020 	return 0;
   1021 }
   1022 
   1023 int
   1024 autri_getdev(void *addr, struct audio_device *retp)
   1025 {
   1026 	struct autri_softc *sc = addr;
   1027 
   1028 	DPRINTF(("autri_getdev().\n"));
   1029 
   1030 	strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
   1031 	snprintf(retp->version, sizeof(retp->version), "0x%02x",
   1032 	     PCI_REVISION(sc->sc_class));
   1033 
   1034 	switch (sc->sc_devid) {
   1035 	case AUTRI_DEVICE_ID_4DWAVE_DX:
   1036 		strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
   1037 		break;
   1038 	case AUTRI_DEVICE_ID_4DWAVE_NX:
   1039 		strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
   1040 		break;
   1041 	case AUTRI_DEVICE_ID_SIS_7018:
   1042 		strncpy(retp->config, "SiS 7018", sizeof(retp->config));
   1043 		break;
   1044 	case AUTRI_DEVICE_ID_ALI_M5451:
   1045 		strncpy(retp->config, "ALi M5451", sizeof(retp->config));
   1046 		break;
   1047 	default:
   1048 		strncpy(retp->config, "unknown", sizeof(retp->config));
   1049 	}
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 int
   1055 autri_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1056 {
   1057 	struct autri_softc *sc = addr;
   1058 
   1059 	return (sc->sc_codec.codec_if->vtbl->mixer_set_port(
   1060 	    sc->sc_codec.codec_if, cp));
   1061 }
   1062 
   1063 int
   1064 autri_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1065 {
   1066 	struct autri_softc *sc = addr;
   1067 
   1068 	return (sc->sc_codec.codec_if->vtbl->mixer_get_port(
   1069 	    sc->sc_codec.codec_if, cp));
   1070 }
   1071 
   1072 int
   1073 autri_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1074 {
   1075 	struct autri_softc *sc = addr;
   1076 
   1077 	return (sc->sc_codec.codec_if->vtbl->query_devinfo(
   1078 	    sc->sc_codec.codec_if, dip));
   1079 }
   1080 
   1081 int
   1082 autri_get_portnum_by_name(struct autri_softc *sc, char *class,
   1083 			  char *device, char *qualifier)
   1084 {
   1085 	return (sc->sc_codec.codec_if->vtbl->get_portnum_by_name(
   1086 	    sc->sc_codec.codec_if, class, device, qualifier));
   1087 }
   1088 
   1089 void *
   1090 autri_malloc(void *addr, int direction, size_t size,
   1091     struct malloc_type *pool, int flags)
   1092 {
   1093 	struct autri_softc *sc = addr;
   1094 	struct autri_dma *p;
   1095 	int error;
   1096 
   1097 	p = malloc(sizeof(*p), pool, flags);
   1098 	if (!p)
   1099 		return NULL;
   1100 
   1101 #if 0
   1102 	error = autri_allocmem(sc, size, 16, p);
   1103 #endif
   1104 	error = autri_allocmem(sc, size, 0x10000, p);
   1105 	if (error) {
   1106 		free(p, pool);
   1107 		return NULL;
   1108 	}
   1109 
   1110 	p->next = sc->sc_dmas;
   1111 	sc->sc_dmas = p;
   1112 	return KERNADDR(p);
   1113 }
   1114 
   1115 void
   1116 autri_free(void *addr, void *ptr, struct malloc_type *pool)
   1117 {
   1118 	struct autri_softc *sc = addr;
   1119 	struct autri_dma **pp, *p;
   1120 
   1121 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1122 		if (KERNADDR(p) == ptr) {
   1123 			autri_freemem(sc, p);
   1124 			*pp = p->next;
   1125 			free(p, pool);
   1126 			return;
   1127 		}
   1128 	}
   1129 }
   1130 
   1131 static struct autri_dma *
   1132 autri_find_dma(struct autri_softc *sc, void *addr)
   1133 {
   1134 	struct autri_dma *p;
   1135 
   1136 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1137 		;
   1138 
   1139 	return p;
   1140 }
   1141 
   1142 size_t
   1143 autri_round_buffersize(void *addr, int direction, size_t size)
   1144 {
   1145 	return size;
   1146 }
   1147 
   1148 paddr_t
   1149 autri_mappage(void *addr, void *mem, off_t off, int prot)
   1150 {
   1151 	struct autri_softc *sc = addr;
   1152 	struct autri_dma *p;
   1153 
   1154 	if (off < 0)
   1155 		return (-1);
   1156 
   1157 	p = autri_find_dma(sc, mem);
   1158 	if (!p)
   1159 		return (-1);
   1160 
   1161 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1162 	    off, prot, BUS_DMA_WAITOK));
   1163 }
   1164 
   1165 int
   1166 autri_get_props(void *addr)
   1167 {
   1168 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1169 		AUDIO_PROP_FULLDUPLEX);
   1170 }
   1171 
   1172 static void
   1173 autri_setup_channel(struct autri_softc *sc, int mode,
   1174 		    struct audio_params *param)
   1175 {
   1176 	int i, ch, channel;
   1177 	u_int32_t reg, cr[5];
   1178 	u_int32_t cso, eso;
   1179 	u_int32_t delta, dch[2], ctrl;
   1180 	u_int32_t alpha_fms, fm_vol, attribute;
   1181 
   1182 	u_int32_t dmaaddr, dmalen;
   1183 	int factor, rvol, cvol;
   1184 	struct autri_chstatus *chst;
   1185 
   1186 	ctrl = AUTRI_CTRL_LOOPMODE;
   1187 	switch (param->encoding) {
   1188 	case AUDIO_ENCODING_SLINEAR_BE:
   1189 	case AUDIO_ENCODING_SLINEAR_LE:
   1190 		ctrl |= AUTRI_CTRL_SIGNED;
   1191 		break;
   1192 	}
   1193 
   1194 	factor = 0;
   1195 	if (param->precision == 16) {
   1196 		ctrl |= AUTRI_CTRL_16BIT;
   1197 		factor++;
   1198 	}
   1199 
   1200 	if (param->channels == 2) {
   1201 		ctrl |= AUTRI_CTRL_STEREO;
   1202 		factor++;
   1203 	}
   1204 
   1205 	delta = (u_int32_t)param->sample_rate;
   1206 	if (delta < 4000)
   1207 		delta = 4000;
   1208 	if (delta > 48000)
   1209 		delta = 48000;
   1210 
   1211 	attribute = 0;
   1212 
   1213 	dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
   1214 	if (mode == AUMODE_PLAY) {
   1215 		chst = &sc->sc_play;
   1216 		dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
   1217 		ctrl |= AUTRI_CTRL_WAVEVOL;
   1218 	} else {
   1219 		chst = &sc->sc_rec;
   1220 		dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
   1221 		if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018) {
   1222 			ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1223 			attribute = AUTRI_ATTR_PCMREC_SIS;
   1224 			if (delta != 48000)
   1225 				attribute |= AUTRI_ATTR_ENASRC_SIS;
   1226 		} else
   1227 			ctrl |= AUTRI_CTRL_MUTEVOL;
   1228 	}
   1229 
   1230 	dmaaddr = DMAADDR(chst->dma);
   1231 	cso = alpha_fms = 0;
   1232 	rvol = cvol = 0x7f;
   1233 	fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
   1234 
   1235 	for (ch=0; ch<2; ch++) {
   1236 
   1237 		if (ch == 0)
   1238 			dmalen = (chst->length >> factor);
   1239 		else {
   1240 			/* channel for interrupt */
   1241 			dmalen = (chst->blksize >> factor);
   1242 			if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
   1243 				ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1244 			else
   1245 				ctrl |= AUTRI_CTRL_MUTEVOL;
   1246 			attribute = 0;
   1247 		}
   1248 
   1249 		eso = dmalen - 1;
   1250 
   1251 		switch (sc->sc_devid) {
   1252 		case AUTRI_DEVICE_ID_4DWAVE_DX:
   1253 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1254 			cr[1] = dmaaddr;
   1255 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1256 			cr[3] = fm_vol;
   1257 			cr[4] = ctrl;
   1258 			break;
   1259 		case AUTRI_DEVICE_ID_4DWAVE_NX:
   1260 			cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
   1261 			cr[1] = dmaaddr;
   1262 			cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
   1263 			cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
   1264 			cr[4] = ctrl;
   1265 			break;
   1266 		case AUTRI_DEVICE_ID_SIS_7018:
   1267 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1268 			cr[1] = dmaaddr;
   1269 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1270 			cr[3] = attribute;
   1271 			cr[4] = ctrl;
   1272 			break;
   1273 		case AUTRI_DEVICE_ID_ALI_M5451:
   1274 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1275 			cr[1] = dmaaddr;
   1276 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1277 			cr[3] = 0;
   1278 			cr[4] = ctrl;
   1279 			break;
   1280 		}
   1281 
   1282 		/* write channel data */
   1283 		channel = (ch == 0) ? chst->ch : chst->ch_intr;
   1284 
   1285 		reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
   1286 		TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
   1287 
   1288 		for (i=0; i<5; i++) {
   1289 			TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
   1290 			DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
   1291 		}
   1292 
   1293 		/* Bank A only */
   1294 		if (channel < 0x20) {
   1295 			TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
   1296 			TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
   1297 		}
   1298 	}
   1299 
   1300 }
   1301 
   1302 int
   1303 autri_trigger_output(void *addr, void *start, void *end, int blksize,
   1304 		     void (*intr)(void *), void *arg,
   1305 		     struct audio_params *param)
   1306 {
   1307 	struct autri_softc *sc = addr;
   1308         struct autri_dma *p;
   1309 
   1310 	DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
   1311 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1312 
   1313 	sc->sc_play.intr = intr;
   1314 	sc->sc_play.intr_arg = arg;
   1315 	sc->sc_play.offset = 0;
   1316 	sc->sc_play.blksize = blksize;
   1317 	sc->sc_play.length = (char *)end - (char *)start;
   1318 
   1319 	p = autri_find_dma(sc, start);
   1320 	if (!p) {
   1321 		printf("autri_trigger_output: bad addr %p\n", start);
   1322 		return (EINVAL);
   1323 	}
   1324 
   1325 	sc->sc_play.dma = p;
   1326 
   1327 	/* */
   1328 	autri_setup_channel(sc, AUMODE_PLAY, param);
   1329 
   1330 	/* volume set to no attenuation */
   1331 	TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
   1332 
   1333 	/* enable interrupt */
   1334 	autri_enable_interrupt(sc, sc->sc_play.ch_intr);
   1335 
   1336 	/* start channel */
   1337 	autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
   1338 
   1339 	return 0;
   1340 }
   1341 
   1342 int
   1343 autri_trigger_input(void *addr, void *start, void *end, int blksize,
   1344 		    void (*intr)(void *), void *arg,
   1345 		    struct audio_params *param)
   1346 {
   1347 	struct autri_softc *sc = addr;
   1348         struct autri_dma *p;
   1349 
   1350 	DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
   1351 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1352 
   1353 	sc->sc_rec.intr = intr;
   1354 	sc->sc_rec.intr_arg = arg;
   1355 	sc->sc_rec.offset = 0;
   1356 	sc->sc_rec.blksize = blksize;
   1357 	sc->sc_rec.length = (char *)end - (char *)start;
   1358 
   1359 	/* */
   1360 	p = autri_find_dma(sc, start);
   1361 	if (!p) {
   1362 		printf("autri_trigger_input: bad addr %p\n", start);
   1363 		return (EINVAL);
   1364 	}
   1365 
   1366 	sc->sc_rec.dma = p;
   1367 
   1368 	/* */
   1369 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
   1370 		autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
   1371 		TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
   1372 	}
   1373 
   1374 #if 0
   1375 	/* 4DWAVE only allows capturing at a 48KHz rate */
   1376 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
   1377 	    sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
   1378 		param->sample_rate = 48000;
   1379 #endif
   1380 
   1381 	autri_setup_channel(sc, AUMODE_RECORD, param);
   1382 
   1383 	/* enable interrupt */
   1384 	autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
   1385 
   1386 	/* start channel */
   1387 	autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1388 
   1389 	return 0;
   1390 }
   1391 
   1392 #if 0
   1393 static int
   1394 autri_halt(struct autri_softc *sc)
   1395 {
   1396 	DPRINTF(("autri_halt().\n"));
   1397 	/*autri_stopch(sc);*/
   1398 	autri_disable_interrupt(sc, sc->sc_play.channel);
   1399 	autri_disable_interrupt(sc, sc->sc_rec.channel);
   1400 	return 0;
   1401 }
   1402 #endif
   1403 
   1404 static void
   1405 autri_enable_interrupt(struct autri_softc *sc, int ch)
   1406 {
   1407 	int reg;
   1408 
   1409 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1410 	ch &= 0x1f;
   1411 
   1412 	autri_reg_set_4(sc, reg, 1 << ch);
   1413 }
   1414 
   1415 static void
   1416 autri_disable_interrupt(struct autri_softc *sc, int ch)
   1417 {
   1418 	int reg;
   1419 
   1420 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1421 	ch &= 0x1f;
   1422 
   1423 	autri_reg_clear_4(sc, reg, 1 << ch);
   1424 }
   1425 
   1426 static void
   1427 autri_startch(struct autri_softc *sc, int ch, int ch_intr)
   1428 {
   1429 	int reg;
   1430 	u_int32_t chmask;
   1431 
   1432 	reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
   1433 	ch &= 0x1f;
   1434 	ch_intr &= 0x1f;
   1435 	chmask = (1 << ch) | (1 << ch_intr);
   1436 
   1437 	autri_reg_set_4(sc, reg, chmask);
   1438 }
   1439 
   1440 static void
   1441 autri_stopch(struct autri_softc *sc, int ch, int ch_intr)
   1442 {
   1443 	int reg;
   1444 	u_int32_t chmask;
   1445 
   1446 	reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
   1447 	ch &= 0x1f;
   1448 	ch_intr &= 0x1f;
   1449 	chmask = (1 << ch) | (1 << ch_intr);
   1450 
   1451 	autri_reg_set_4(sc, reg, chmask);
   1452 }
   1453 
   1454 #if NMIDI > 0
   1455 int
   1456 autri_midi_open(void *addr, int flags, void (*iintr)(void *, int),
   1457 		void (*ointr)(void *), void *arg)
   1458 {
   1459 	struct autri_softc *sc = addr;
   1460 
   1461 	DPRINTF(("autri_midi_open()\n"));
   1462 
   1463 	DPRINTFN(5,("MPUR1 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR1)));
   1464 	DPRINTFN(5,("MPUR2 : 0x%02X\n",TREAD1(sc,AUTRI_MPUR2)));
   1465 
   1466 	sc->sc_iintr = iintr;
   1467 	sc->sc_ointr = ointr;
   1468 	sc->sc_arg = arg;
   1469 
   1470 	if (flags & FREAD)
   1471 		autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
   1472 
   1473 	if (flags & FWRITE)
   1474 		autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
   1475 
   1476 	return (0);
   1477 }
   1478 
   1479 void
   1480 autri_midi_close(void *addr)
   1481 {
   1482 	struct autri_softc *sc = addr;
   1483 
   1484 	DPRINTF(("autri_midi_close()\n"));
   1485 
   1486 	tsleep(sc, PWAIT, "autri", hz/10); /* give uart a chance to drain */
   1487 
   1488 	sc->sc_iintr = NULL;
   1489 	sc->sc_ointr = NULL;
   1490 }
   1491 
   1492 int
   1493 autri_midi_output(void *addr, int d)
   1494 {
   1495 	struct autri_softc *sc = addr;
   1496 	int x;
   1497 
   1498 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1499 		if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
   1500 			TWRITE1(sc, AUTRI_MPUR0, d);
   1501 			return (0);
   1502 		}
   1503 		delay(MIDI_BUSY_DELAY);
   1504 	}
   1505 	return (EIO);
   1506 }
   1507 
   1508 void
   1509 autri_midi_getinfo(void *addr, struct midi_info *mi)
   1510 {
   1511 	mi->name = "4DWAVE MIDI UART";
   1512 	mi->props = MIDI_PROP_CAN_INPUT;
   1513 }
   1514 
   1515 #endif
   1516